LCOV - code coverage report
Current view: top level - source/third_party/tinygettext/src - plural_forms.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 1 61 1.6 %
Date: 2023-01-19 00:18:29 Functions: 2 27 7.4 %

          Line data    Source code
       1             : // tinygettext - A gettext replacement that works directly on .po files
       2             : // Copyright (c) 2006 Ingo Ruhnke <grumbel@gmail.com>
       3             : //
       4             : // This software is provided 'as-is', without any express or implied
       5             : // warranty. In no event will the authors be held liable for any damages
       6             : // arising from the use of this software.
       7             : //
       8             : // Permission is granted to anyone to use this software for any purpose,
       9             : // including commercial applications, and to alter it and redistribute it
      10             : // freely, subject to the following restrictions:
      11             : //
      12             : // 1. The origin of this software must not be misrepresented; you must not
      13             : //    claim that you wrote the original software. If you use this software
      14             : //    in a product, an acknowledgement in the product documentation would be
      15             : //    appreciated but is not required.
      16             : // 2. Altered source versions must be plainly marked as such, and must not be
      17             : //    misrepresented as being the original software.
      18             : // 3. This notice may not be removed or altered from any source distribution.
      19             : 
      20             : #include "precompiled.h"
      21             : 
      22             : #include "tinygettext/plural_forms.hpp"
      23             : 
      24             : #include <unordered_map>
      25             : 
      26             : namespace tinygettext {
      27             : 
      28             : namespace {
      29             : 
      30             : /**
      31             :  *  Plural functions are used to select a string that matches a given
      32             :  *  count. \a n is the count and the return value is the string index
      33             :  *  used in the .po file, for example:
      34             :  *
      35             :  *   msgstr[0] = "You got %d error";
      36             :  *   msgstr[1] = "You got %d errors";
      37             :  *          ^-- return value of plural function
      38             :  */
      39           0 : unsigned int plural1(int )     { return 0; }
      40           0 : unsigned int plural2_1(int n)  { return (n != 1); }
      41           0 : unsigned int plural2_2(int n)  { return (n > 1); }
      42           0 : unsigned int plural2_mk(int n) { return n==1 || n%10==1 ? 0 : 1; }
      43           0 : unsigned int plural2_mk_2(int n) { return static_cast<unsigned int>((n % 10 == 1 && n % 100 != 11) ? 0 : 1);}
      44           0 : unsigned int plural3_lv(int n) { return static_cast<unsigned int>(n%10==1 && n%100!=11 ? 0 : n != 0 ? 1 : 2); }
      45           0 : unsigned int plural3_ga(int n) { return static_cast<unsigned int>(n==1 ? 0 : n==2 ? 1 : 2); }
      46           0 : unsigned int plural3_lt(int n) { return static_cast<unsigned int>(n%10==1 && n%100!=11 ? 0 : n%10>=2 && (n%100<10 || n%100>=20) ? 1 : 2); }
      47           0 : unsigned int plural3_1(int n)  { return static_cast<unsigned int>(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2); }
      48           0 : unsigned int plural3_sk(int n) { return static_cast<unsigned int>( (n==1) ? 0 : (n>=2 && n<=4) ? 1 : 2 ); }
      49           0 : unsigned int plural3_pl(int n) { return static_cast<unsigned int>(n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2); }
      50           0 : unsigned int plural3_ro(int n) { return static_cast<unsigned int>(n==1?0:(((n%100>19)||((n%100==0)&&(n!=0)))?2:1));}
      51           0 : unsigned int plural3_sl(int n) { return static_cast<unsigned int>(n%100==1 ? 0 : n%100==2 ? 1 : n%100==3 || n%100==4 ? 2 : 3); }
      52           0 : unsigned int plural4_be(int n) { return static_cast<unsigned int>(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<12 || n%100>14) ? 1 : n%10==0 || (n%10>=5 && n%10<=9) || (n%100>=11 && n%100<=14)? 2 : 3);}
      53           0 : unsigned int plural4_cs(int n) { return static_cast<unsigned int>((n == 1 && n % 1 == 0) ? 0 : (n >= 2 && n <= 4 && n % 1 == 0) ? 1: (n % 1 != 0 ) ? 2 : 3);}
      54           0 : unsigned int plural4_cy(int n) { return static_cast<unsigned int>((n==1) ? 0 : (n==2) ? 1 : (n != 8 && n != 11) ? 2 : 3);}
      55           0 : unsigned int plural4_gd(int n) { return static_cast<unsigned int>(( n==1 || n==11) ? 0 : (n==2 || n==12) ? 1 : (n > 2 && n < 20) ? 2 : 3); }
      56           0 : unsigned int plural4_he(int n) { return static_cast<unsigned int>((n == 1 && n % 1 == 0) ? 0 : (n == 2 && n % 1 == 0) ? 1: (n % 10 == 0 && n % 1 == 0 && n > 10) ? 2 : 3);}
      57           0 : unsigned int plural4_lt(int n) { return static_cast<unsigned int>(n % 10 == 1 && (n % 100 > 19 || n % 100 < 11) ? 0 : (n % 10 >= 2 && n % 10 <=9) && (n % 100 > 19 || n % 100 < 11) ? 1 : n % 1 != 0 ? 2: 3);}
      58           0 : unsigned int plural4_pl(int n) { return static_cast<unsigned int>(n == 1 ? 0 : (n % 10 >= 2 && n % 10 <= 4) && (n % 100 < 12 || n % 100 > 14) ? 1 : (n != 1 && (n % 10 >= 0 && n % 10 <= 1)) || (n % 10 >= 5 && n % 10 <= 9) || (n % 100 >= 12 && n % 100 <= 14) ? 2 : 3); }
      59           0 : unsigned int plural4_sk(int n) { return static_cast<unsigned int>(n % 1 == 0 && n == 1 ? 0 : n % 1 == 0 && n >= 2 && n <= 4 ? 1 : n % 1 != 0 ? 2: 3);}
      60           0 : unsigned int plural4_uk(int n) { return static_cast<unsigned int>(n % 1 == 0 && n % 10 == 1 && n % 100 != 11 ? 0 : n % 1 == 0 && n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 12 || n % 100 > 14) ? 1 : n % 1 == 0 && (n % 10 ==0 || (n % 10 >=5 && n % 10 <=9) || (n % 100 >=11 && n % 100 <=14 )) ? 2: 3);}
      61           0 : unsigned int plural5_ga(int n) { return static_cast<unsigned int>(n==1 ? 0 : n==2 ? 1 : n<7 ? 2 : n<11 ? 3 : 4);}
      62           0 : unsigned int plural6_ar(int n) { return static_cast<unsigned int>( n==0 ? 0 : n==1 ? 1 : n==2 ? 2 : n%100>=3 && n%100<=10 ? 3 : n%100>=11 ? 4 : 5); }
      63             : 
      64             : } // namespace
      65             : 
      66             : PluralForms
      67           0 : PluralForms::from_string(const std::string& str)
      68             : {
      69             :   typedef std::unordered_map<std::string, PluralForms> PluralFormsMap;
      70           0 :   static PluralFormsMap plural_forms;
      71             : 
      72           0 :   if (plural_forms.empty())
      73             :   {
      74             :     // Note that the plural forms here shouldn't contain any spaces
      75           0 :     plural_forms["Plural-Forms:nplurals=1;plural=0;"] = PluralForms(1, plural1);
      76           0 :     plural_forms["Plural-Forms:nplurals=2;plural=(n!=1);"] = PluralForms(2, plural2_1);
      77           0 :     plural_forms["Plural-Forms:nplurals=2;plural=n!=1;"] = PluralForms(2, plural2_1);
      78           0 :     plural_forms["Plural-Forms:nplurals=2;plural=(n>1);"] = PluralForms(2, plural2_2);
      79           0 :     plural_forms["Plural-Forms:nplurals=2;plural=n==1||n%10==1?0:1;"] = PluralForms(2, plural2_mk);
      80           0 :     plural_forms["Plural-Forms:nplurals=2;plural=(n%10==1&&n%100!=11)?0:1;"] = PluralForms(2, plural2_mk_2);
      81           0 :     plural_forms["Plural-Forms:nplurals=3;plural=n%10==1&&n%100!=11?0:n!=0?1:2);"] = PluralForms(2, plural3_lv);
      82           0 :     plural_forms["Plural-Forms:nplurals=3;plural=n==1?0:n==2?1:2;"] = PluralForms(3, plural3_ga);
      83           0 :     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);
      84           0 :     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);
      85           0 :     plural_forms["Plural-Forms:nplurals=3;plural=(n==1)?0:(n>=2&&n<=4)?1:2;"] = PluralForms(3, plural3_sk);
      86           0 :     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);
      87           0 :     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);
      88           0 :     plural_forms["Plural-Forms:nplurals=3;plural=(n==1?0:(((n%100>19)||((n%100==0)&&(n!=0)))?2:1));"] = PluralForms(3, plural3_ro);
      89           0 :     plural_forms["Plural-Forms:nplurals=4;plural=(n%1==0&&n==1?0:n%1==0&&n>=2&&n<=4?1:n%1!=0?2:3);"] = PluralForms(4, plural4_sk);
      90           0 :     plural_forms["Plural-Forms:nplurals=4;plural=(n==1&&n%1==0)?0:(n>=2&&n<=4&&n%1==0)?1:(n%1!=0)?2:3;"] = PluralForms(4, plural4_cs);
      91           0 :     plural_forms["Plural-Forms:nplurals=4;plural=(n%10==1&&n%100!=11?0:n%10>=2&&n%10<=4&&(n%100<12||n%100>14)?1:n%10==0||(n%10>=5&&n%10<=9)||(n%100>=11&&n%100<=14)?2:3);"] = PluralForms(4, plural4_be);
      92           0 :     plural_forms["Plural-Forms:nplurals=4;plural=(n==1||n==11)?0:(n==2||n==12)?1:(n>2&&n<20)?2:3;"]=PluralForms(4, plural4_gd);
      93           0 :     plural_forms["Plural-Forms:nplurals=4;plural=(n==1)?0:(n==2)?1:(n!=8&&n!=11)?2:3;"] = PluralForms(4, plural4_cy);
      94           0 :     plural_forms["Plural-Forms:nplurals=4;plural=(n%10==1&&(n%100>19||n%100<11)?0:(n%10>=2&&n%10<=9)&&(n%100>19||n%100<11)?1:n%1!=0?2:3);"] = PluralForms(4, plural4_lt);
      95           0 :     plural_forms["Plural-Forms:nplurals=4;plural=(n%1==0&&n%10==1&&n%100!=11?0:n%1==0&&n%10>=2&&n%10<=4&&(n%100<12||n%100>14)?1:n%1==0&&(n%10==0||(n%10>=5&&n%10<=9)||(n%100>=11&&n%100<=14))?2:3);"] = PluralForms(4, plural4_uk);
      96           0 :     plural_forms["Plural-Forms:nplurals=4;plural=(n==1?0:(n%10>=2&&n%10<=4)&&(n%100<12||n%100>14)?1:n!=1&&(n%10>=0&&n%10<=1)||(n%10>=5&&n%10<=9)||(n%100>=12&&n%100<=14)?2:3);"] = PluralForms(4, plural4_pl);
      97           0 :     plural_forms["Plural-Forms:nplurals=4;plural=(n==1&&n%1==0)?0:(n==2&&n%1==0)?1:(n%10==0&&n%1==0&&n>10)?2:3;"] = PluralForms(4, plural4_he);
      98           0 :     plural_forms["Plural-Forms:nplurals=5;plural=(n==1?0:n==2?1:n<7?2:n<11?3:4)"] = PluralForms(5, plural5_ga);
      99           0 :     plural_forms["Plural-Forms:nplurals=6;plural=n==0?0:n==1?1:n==2?2:n%100>=3&&n%100<=10?3:n%100>=11?4:5"]=PluralForms(6, plural6_ar);
     100             :   }
     101             : 
     102             :   // Remove spaces from string before lookup
     103           0 :   std::string space_less_str;
     104           0 :   for(std::string::size_type i = 0; i < str.size(); ++i)
     105           0 :     if (!isspace(str[i]))
     106           0 :       space_less_str += str[i];
     107             : 
     108           0 :   PluralFormsMap::const_iterator it= plural_forms.find(space_less_str);
     109           0 :   if (it != plural_forms.end())
     110             :   {
     111           0 :     return it->second;
     112             :   }
     113             :   else
     114             :   {
     115           0 :     return PluralForms();
     116             :   }
     117             : }
     118             : 
     119           3 : } // namespace tinygettext
     120             : 
     121             : /* EOF */

Generated by: LCOV version 1.13