[Supertux-Commit] [tinygettext commit] r141 - in branches/tinygettext-portable: . src

codesite-noreply at google.com codesite-noreply at google.com
Mon Feb 2 01:28:28 PST 2009


Author: grumbel
Date: Mon Feb  2 01:27:08 2009
New Revision: 141

Modified:
    branches/tinygettext-portable/TODO
    branches/tinygettext-portable/src/dictionary.cpp
    branches/tinygettext-portable/src/plural_forms.cpp
    branches/tinygettext-portable/src/plural_forms.hpp
    branches/tinygettext-portable/src/po_parser.cpp

Log:
Turned PluralForms into a class, some better error checking for Plural-Forms

Modified: branches/tinygettext-portable/TODO
==============================================================================
--- branches/tinygettext-portable/TODO	(original)
+++ branches/tinygettext-portable/TODO	Mon Feb  2 01:27:08 2009
@@ -24,8 +24,6 @@
  tinygettext API related stuff:
  ==============================

-* warn if po contains a msgid_plural but no PluralForms
-
  * Language matching needs a little review/cleanup

  * translate, translate_ctxt, translate_ctxt_plural, ... could be

Modified: branches/tinygettext-portable/src/dictionary.cpp
==============================================================================
--- branches/tinygettext-portable/src/dictionary.cpp	(original)
+++ branches/tinygettext-portable/src/dictionary.cpp	Mon Feb  2 01:27:08  
2009
@@ -24,7 +24,7 @@
  namespace tinygettext {
  
  Dictionary::Dictionary(const std::string& charset_)
-  : charset(charset_), plural_forms(PluralForms::create(0,0))
+  : charset(charset_)
  {
  }

@@ -67,11 +67,7 @@
    if (i != dict.end())
      {
        unsigned int n = 0;
-      if (plural_forms.plural)
-        n = plural_forms.plural(count);
-      else // FIXME: Should give that warning when parsing the po, not at  
runtime
-        log_warning << "warning: Plural-Forms missing" << std::endl;
-
+      n = plural_forms.get_plural(count);
        assert(n >= 0 && n < msgstrs.size());

        if (!msgstrs[n].empty())

Modified: branches/tinygettext-portable/src/plural_forms.cpp
==============================================================================
--- branches/tinygettext-portable/src/plural_forms.cpp	(original)
+++ branches/tinygettext-portable/src/plural_forms.cpp	Mon Feb  2 01:27:08  
2009
@@ -52,20 +52,20 @@
    if (plural_forms.empty())
      {
        // FIXME: Could match some more strings if we cut out all spaces  
before compare
-      plural_forms["Plural-Forms: nplurals=1; plural=0;"]        =  
PluralForms::create(1, plural1);
-      plural_forms["Plural-Forms: nplurals=2; plural=(n != 1);"] =  
PluralForms::create(2, plural2_1);
-      plural_forms["Plural-Forms: nplurals=2; plural=(n > 1);"]  =  
PluralForms::create(2, plural2_2);
-      plural_forms["Plural-Forms: nplurals=2; plural=n==1 || n%10==1 ? 0 :  
1;"] = PluralForms::create(2, plural2_mk);
+      plural_forms["Plural-Forms: nplurals=1; plural=0;"]        =  
PluralForms(1, plural1);
+      plural_forms["Plural-Forms: nplurals=2; plural=(n != 1);"] =  
PluralForms(2, plural2_1);
+      plural_forms["Plural-Forms: nplurals=2; plural=(n > 1);"]  =  
PluralForms(2, plural2_2);
+      plural_forms["Plural-Forms: nplurals=2; plural=n==1 || n%10==1 ? 0 :  
1;"] = PluralForms(2, plural2_mk);

-      plural_forms["Plural-Forms: nplurals=3; plural=n%10==1 &&  
n%100!=11 ? 0 : n != 0 ? 1 : 2);"] = PluralForms::create(2, plural3_lv);
-      plural_forms["Plural-Forms: nplurals=3; plural=n==1 ? 0 : n==2 ? 1 :  
2;"] = PluralForms::create(3, plural3_ga);
-      plural_forms["Plural-Forms: nplurals=3; plural=(n%10==1 &&  
n%100!=11 ? 0 : n%10>=2 && (n%100<10 || n%100>=20) ? 1 : 2);"] =  
PluralForms::create(3, plural3_lt);
-      plural_forms["Plural-Forms: nplurals=3; plural=(n%10==1 &&  
n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);"] =  
PluralForms::create(3, plural3_1);
-      plural_forms["Plural-Forms: nplurals=3; plural=(n==1) ? 0 : (n>=2 &&  
n<=4) ? 1 : 2;"] = PluralForms::create(3, plural3_sk);
-      plural_forms["Plural-Forms: nplurals=3; plural=(n==1 ? 0 : n%10>=2  
&& n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);"] = PluralForms::create(3,  
plural3_pl);
-      plural_forms["Plural-Forms: nplurals=3; plural=(n%100==1 ? 0 :  
n%100==2 ? 1 : n%100==3 || n%100==4 ? 2 : 3);"] = PluralForms::create(3,  
plural3_sl);
+      plural_forms["Plural-Forms: nplurals=3; plural=n%10==1 &&  
n%100!=11 ? 0 : n != 0 ? 1 : 2);"] = PluralForms(2, plural3_lv);
+      plural_forms["Plural-Forms: nplurals=3; plural=n==1 ? 0 : n==2 ? 1 :  
2;"] = PluralForms(3, plural3_ga);
+      plural_forms["Plural-Forms: nplurals=3; plural=(n%10==1 &&  
n%100!=11 ? 0 : n%10>=2 && (n%100<10 || n%100>=20) ? 1 : 2);"] =  
PluralForms(3, plural3_lt);
+      plural_forms["Plural-Forms: nplurals=3; plural=(n%10==1 &&  
n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);"] =  
PluralForms(3, plural3_1);
+      plural_forms["Plural-Forms: nplurals=3; plural=(n==1) ? 0 : (n>=2 &&  
n<=4) ? 1 : 2;"] = PluralForms(3, plural3_sk);
+      plural_forms["Plural-Forms: nplurals=3; plural=(n==1 ? 0 : n%10>=2  
&& n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);"] = PluralForms(3,  
plural3_pl);
+      plural_forms["Plural-Forms: nplurals=3; plural=(n%100==1 ? 0 :  
n%100==2 ? 1 : n%100==3 || n%100==4 ? 2 : 3);"] = PluralForms(3,  
plural3_sl);

-      plural_forms["Plural-Forms: nplurals=4; plural=n==1 ? 0 : n==2 ? 1 :  
n>=3 && n<=10 ? 2 : 3;"] = PluralForms::create(4, plural4_ar);
+      plural_forms["Plural-Forms: nplurals=4; plural=n==1 ? 0 : n==2 ? 1 :  
n>=3 && n<=10 ? 2 : 3;"] = PluralForms(4, plural4_ar);
    }

    std::map<std::string, struct PluralForms>::const_iterator i=  
plural_forms.find(str);
@@ -75,7 +75,7 @@
      }
    else
      {
-      return PluralForms::create(0, 0);
+      return PluralForms();
      }
  }


Modified: branches/tinygettext-portable/src/plural_forms.hpp
==============================================================================
--- branches/tinygettext-portable/src/plural_forms.hpp	(original)
+++ branches/tinygettext-portable/src/plural_forms.hpp	Mon Feb  2 01:27:08  
2009
@@ -26,20 +26,33 @@
  
  typedef unsigned int (*PluralFunc)(int n);

-struct PluralForms
+class PluralForms
  {
-  int         nplural;
-  PluralFunc  plural;
+private:
+  unsigned int nplural;
+  PluralFunc   plural;

+public:
    static PluralForms from_string(const std::string& str);

-  static PluralForms create(int         nplural,
-                            PluralFunc  plural)
-  {
-    PluralForms forms;
-    forms.nplural = nplural;
-    forms.plural  = plural;
-    return forms;
+  PluralForms()
+    : nplural(0),
+      plural(0)
+  {}
+
+  PluralForms(int nplural_, PluralFunc plural_)
+    : nplural(nplural_),
+      plural(plural_)
+  {}
+
+  unsigned int get_nplural() const { return nplural; }
+  unsigned int get_plural(int n) const { if (plural) return plural(n);  
else return 0; }
+
+  bool operator==(const PluralForms& other) { return nplural ==  
other.nplural && plural == other.plural; }
+  bool operator!=(const PluralForms& other) { return !(*this == other); }
+
+  operator bool() const {
+    return plural;
    }
  };
  

Modified: branches/tinygettext-portable/src/po_parser.cpp
==============================================================================
--- branches/tinygettext-portable/src/po_parser.cpp	(original)
+++ branches/tinygettext-portable/src/po_parser.cpp	Mon Feb  2 01:27:08 2009
@@ -251,20 +251,19 @@
            else if (has_prefix(line, "Plural-Forms:"))
              {
                PluralForms plural_forms = PluralForms::from_string(line);
-              if (plural_forms.nplural == 0 || plural_forms.plural == 0)
+              if (!plural_forms)
                  {
                    warning("unknown Plural-Forms given");
                  }
                else
                  {
-                  if (dict.get_plural_forms().nplural == 0 &&  
dict.get_plural_forms().plural == 0)
+                  if (!dict.get_plural_forms())
                      {
                        dict.set_plural_forms(plural_forms);
                      }
                    else
                      {
-                      if (dict.get_plural_forms().nplural !=  
plural_forms.nplural ||
-                          dict.get_plural_forms().plural !=  
plural_forms.plural)
+                      if (dict.get_plural_forms() != plural_forms)
                          {
                            warning("Plural-Forms missmatch");
                          }
@@ -402,6 +401,18 @@

                    if (use_fuzzy || !fuzzy)
                      {
+                      if (!dict.get_plural_forms())
+                        {
+                          warning("msgstr[N] seen, but no Plural-Forms  
given");
+                        }
+                      else
+                        {
+                          if (msgstr_num.size() !=  
dict.get_plural_forms().get_nplural())
+                            {
+                              warning("msgstr[N] count doesn't match  
Plural-Forms.nplural");
+                            }
+                        }
+
                        if (has_msgctxt)
                          dict.add_translation(msgctxt, msgid, msgid_plural,  
msgstr_num);
                        else


More information about the Supertux-Commit mailing list