include/punk/punk_plural.h

filter: Statementstmt 75.6% 56 / 74 Branchbran 64.9% 74 / 114 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 69.1% 130 / 188 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_ua.h ↑ Summary include/punk/punk_obs.h »
1 #ifndef PUNK_PLURAL_H
2 #define PUNK_PLURAL_H
3  
4 /* CLDR plural categories.
5 *
6 * ---- why this is not `$count == 1` ----------------------------------------
7 *
8 * English has two forms. Polish has three. Arabic has six. Japanese has one.
9 * A system that branches on `$count == 1` is English's grammar wearing a
10 * plural system's name: it is wrong for Polish at 2, 3 and 4 and again at 22,
11 * 23 and 24; it is wrong for Arabic at 0 and 2; and it is wrong SILENTLY,
12 * because the sentence it produces is grammatical, just not for that number.
13 *
14 * So the rules are encoded as rules. Each language names a function from a
15 * number to one of six categories, and a catalogue supplies a string per
16 * category it uses:
17 *
18 * "items": { "one": "{count} item", "other": "{count} items" }
19 *
20 * ---- the operands ----------------------------------------------------------
21 *
22 * CLDR rules are written over operands - n, i, v, w, f, t - where `n` is the
23 * absolute value, `i` its integer part, and `v` the number of visible
24 * fraction digits. This implements the integer case: `v` is 0 for a whole
25 * number and non-zero otherwise, which is what every rule below needs.
26 *
27 * That distinction is not pedantry. In English `1` is `one` and `1.0` is
28 * `other` - "1.0 items" - and the rules say so through `v = 0`. A count that
29 * is not a whole number therefore takes the fractional branch, which in most
30 * languages is `other`, and that is the correct answer rather than a
31 * shortcut.
32 *
33 * ---- an unknown language is an ERROR, not English -------------------------
34 *
35 * There is no default rule. A catalogue that uses plural categories in a
36 * language whose rule is not here fails AT BOOT, naming the locale. The
37 * alternative - falling back to one/other - is precisely the bug this header
38 * exists to prevent, and it would be invisible: Polish pages would read
39 * plausibly and be wrong for a quarter of their numbers.
40 */
41  
42 typedef enum {
43 PI_CAT_ZERO = 0, PI_CAT_ONE, PI_CAT_TWO,
44 PI_CAT_FEW, PI_CAT_MANY, PI_CAT_OTHER,
45 PI_CAT_N
46 } pi_pcat;
47  
48 static const char *const PI_CAT_NAME[PI_CAT_N] = {
49 "zero", "one", "two", "few", "many", "other"
50 };
51  
52 /* The rule families. Languages share a rule far more often than not, which is
53 * why this is a table of families rather than one entry per language. */
54 typedef enum {
55 PR_NONE = -1, /* no rule known - a boot error if plurals are used */
56 PR_OTHER, /* one form: ja, zh, ko, th, vi, id, ms, my */
57 PR_ONE, /* one/other, `one` at exactly 1: en, de, nl, sv... */
58 PR_ZERO_ONE, /* one/other, `one` at 0 and 1: fr, hi */
59 PR_RUSSIAN, /* one/few/many/other: ru, uk, be */
60 PR_POLISH, /* one/few/many/other, different from Russian: pl */
61 PR_CZECH, /* one/few/many/other: cs, sk */
62 PR_ARABIC, /* all six: ar */
63 PR_ROMANIAN, /* one/few/other: ro, mo */
64 PR_LITHUANIAN, /* one/few/many/other: lt */
65 PR_N
66 } pi_rule;
67  
68 /* Language tag to rule. Matched on the WHOLE TAG FIRST, then on the primary
69 * subtag, so en-GB, en-US and en all share English's rule while a tag that
70 * names its own is given it.
71 *
72 * The primary-subtag match is the rule and the whole-tag match is the
73 * exception, because a region almost never changes a grammar. PORTUGUESE IS
74 * THE EXCEPTION THAT PROVES IT WRONG, and it is the reason this lookup grew a
75 * first pass: CLDR gives `pt` one at i = 0..1, so 0 is singular in Brazil,
76 * and `pt_PT` one at i = 1 and v = 0, so 0 is plural in Portugal. Under a
77 * primary-subtag-only match a European Portuguese catalogue would be handed
78 * Brazil's rule and would render "0 peca" where it wants "0 pecas" - which is
79 * exactly the silent, reads-perfectly-well wrongness the error message below
80 * refuses to allow for a language with no rule at all.
81 *
82 * Add a whole-tag row only with a citation. The CLDR plural rules chart is
83 * the source, and a region that merely spells differently does not belong
84 * here: en-GB and en-US are one grammar and share one row. */
85 static const struct { const char *lang; pi_rule rule; } PI_RULES[] = {
86 /* one form */
87 { "ja", PR_OTHER }, { "zh", PR_OTHER }, { "ko", PR_OTHER },
88 { "th", PR_OTHER }, { "vi", PR_OTHER }, { "id", PR_OTHER },
89 { "ms", PR_OTHER }, { "my", PR_OTHER }, { "lo", PR_OTHER },
90 { "km", PR_OTHER }, { "yo", PR_OTHER },
91  
92 /* one at exactly 1 */
93 { "en", PR_ONE }, { "de", PR_ONE }, { "nl", PR_ONE }, { "sv", PR_ONE },
94 { "da", PR_ONE }, { "no", PR_ONE }, { "nb", PR_ONE }, { "nn", PR_ONE },
95 { "fi", PR_ONE }, { "et", PR_ONE }, { "el", PR_ONE }, { "it", PR_ONE },
96 { "es", PR_ONE }, { "hu", PR_ONE }, { "tr", PR_ONE }, { "bg", PR_ONE },
97 { "ca", PR_ONE }, { "eu", PR_ONE }, { "he", PR_ONE }, { "af", PR_ONE },
98 { "sw", PR_ONE }, { "sq", PR_ONE }, { "ka", PR_ONE }, { "az", PR_ONE },
99 { "gl", PR_ONE }, { "is", PR_ONE }, { "ur", PR_ONE }, { "ta", PR_ONE },
100 { "te", PR_ONE }, { "ml", PR_ONE }, { "mr", PR_ONE }, { "ne", PR_ONE },
101  
102 /* one at 0 and 1 */
103 { "fr", PR_ZERO_ONE }, { "hi", PR_ZERO_ONE }, { "pt", PR_ZERO_ONE },
104 { "bn", PR_ZERO_ONE }, { "fa", PR_ZERO_ONE },
105  
106 /* the whole-tag exceptions, each with its CLDR rule quoted */
107 { "pt-PT", PR_ONE }, /* CLDR pt_PT: one is `i = 1 and v = 0`, so 0 is
108 * plural in Portugal while `pt` above has 0 and 1
109 * together for Brazil */
110  
111 /* the Slavic families, which differ from each other */
112 { "ru", PR_RUSSIAN }, { "uk", PR_RUSSIAN }, { "be", PR_RUSSIAN },
113 { "hr", PR_RUSSIAN }, { "sr", PR_RUSSIAN }, { "bs", PR_RUSSIAN },
114 { "pl", PR_POLISH },
115 { "cs", PR_CZECH }, { "sk", PR_CZECH },
116 { "lt", PR_LITHUANIAN },
117 { "ro", PR_ROMANIAN },
118  
119 { "ar", PR_ARABIC }
120 };
121  
122 /* Case-insensitive over ASCII, because a language tag's case carries no
123 * meaning: BCP 47 writes pt-PT and the plugin lowercases to pt-pt, and a
124 * table that matched only one of those would answer a different rule for the
125 * same language depending on who asked. */
126 5113 static int pi_rule_tag_eq(const char *a, const char *b, STRLEN n) {
127 STRLEN i;
128 5647 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 5413 234 5647
129 5413 char x = a[i], y = b[i];
130 5413 ▶ if (x >= 'A' && x <= 'Z') x = (char)(x - 'A' + 'a');
Branch: 2 branches
BranchTrueFalseTotal
if (x >= 'A' && x <= 'Z') x = (char)(x - 'A' + 'a'); 5409 4 5413
if (x >= 'A' && x <= 'Z') x = (char)(x - 'A' + 'a'); 8 5401 5409
131 5413 ▶ if (y >= 'A' && y <= 'Z') y = (char)(y - 'A' + 'a');
Branch: 2 branches
BranchTrueFalseTotal
if (y >= 'A' && y <= 'Z') y = (char)(y - 'A' + 'a'); 5409 4 5413
if (y >= 'A' && y <= 'Z') y = (char)(y - 'A' + 'a'); 0 5409 5409
132 5413 ▶ if (x != y) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (x != y) return 0; 4879 534 5413
133 }
134 234 return 1;
135 }
136  
137 /* The rule for a language tag, or PR_NONE.
138 *
139 * Two passes, and the order is the whole point: the whole tag first so that
140 * pt-PT is not given pt's rule, then the primary subtag so that en-GB is
141 * still given en's. A tag with no hyphen cannot match a hyphenated row, so
142 * the first pass changes no existing answer. */
143 238 static pi_rule pi_rule_for(const char *tag, STRLEN tl) {
144 238 STRLEN prim = 0;
145 size_t i;
146  
147 5880 ▶ for (i = 0; i < sizeof PI_RULES / sizeof PI_RULES[0]; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < sizeof PI_RULES / sizeof PI_RULES[0]; i++) { 5865 15 5880
148 5865 const char *l = PI_RULES[i].lang;
149 5865 ▶ if (strlen(l) == tl && pi_rule_tag_eq(l, tag, tl))
Branch: 2 branches
BranchTrueFalseTotal
if (strlen(l) == tl && pi_rule_tag_eq(l, tag, tl)) 4981 884 5865
if (strlen(l) == tl && pi_rule_tag_eq(l, tag, tl)) 223 4758 4981
150 223 return PI_RULES[i].rule;
151 }
152  
153 45 ▶ while (prim < tl && tag[prim] != '-') prim++;
Branch: 2 branches
BranchTrueFalseTotal
while (prim < tl && tag[prim] != '-') prim++; 41 4 45
while (prim < tl && tag[prim] != '-') prim++; 30 11 41
154 15 ▶ if (prim == tl) return PR_NONE; /* no hyphen: pass one already looked */
Branch: 1 branch
BranchTrueFalseTotal
if (prim == tl) return PR_NONE; /* no hyphen: pass one already looked */ 4 11 15
155 132 ▶ for (i = 0; i < sizeof PI_RULES / sizeof PI_RULES[0]; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < sizeof PI_RULES / sizeof PI_RULES[0]; i++) { 132 0 132
156 132 const char *l = PI_RULES[i].lang;
157 132 ▶ if (strlen(l) == prim && pi_rule_tag_eq(l, tag, prim))
Branch: 2 branches
BranchTrueFalseTotal
if (strlen(l) == prim && pi_rule_tag_eq(l, tag, prim)) 132 0 132
if (strlen(l) == prim && pi_rule_tag_eq(l, tag, prim)) 11 121 132
158 11 return PI_RULES[i].rule;
159 }
160 0 return PR_NONE;
161 }
162  
163 /* Which category does `count` take under `rule`?
164 *
165 * `is_int` is the `v = 0` operand: false means the count has visible fraction
166 * digits, which nearly every rule below routes to `other`.
167 */
168 44 static pi_pcat pi_plural(pi_rule rule, double count, int is_int) {
169 /* Rules are written over the ABSOLUTE value: -1 item and 1 item take the
170 * same form in every language here. */
171 44 ▶ double n = count < 0 ? -count : count;
Branch: 1 branch
BranchTrueFalseTotal
double n = count < 0 ? -count : count; 0 44 44
172 /* a NaN or an infinity cast to UV is undefined in C; both take `other` */
173 44 ▶ UV i = (Perl_isinfnan(n) || n >= (double)UV_MAX) ? 0 : (UV)n;
Branch: 2 branches
BranchTrueFalseTotal
UV i = (Perl_isinfnan(n) || n >= (double)UV_MAX) ? 0 : (UV)n; 44 0 44
UV i = (Perl_isinfnan(n) || n >= (double)UV_MAX) ? 0 : (UV)n; 44 0 44
174 UV i10, i100;
175  
176 44 ▶ if (!is_int) {
Branch: 1 branch
BranchTrueFalseTotal
if (!is_int) { 3 41 44
177 /* The fractional branch. Only Czech and Romanian name it; everywhere
178 * else a number with a fraction is `other`. */
179 3 switch (rule) {
180 1 case PR_CZECH: return PI_CAT_MANY;
181 0 case PR_ROMANIAN: return PI_CAT_FEW;
182 2 default: return PI_CAT_OTHER;
183 }
184 }
185  
186 41 i10 = i % 10;
187 41 i100 = i % 100;
188  
189 41 switch (rule) {
190 3 case PR_OTHER:
191 3 return PI_CAT_OTHER;
192  
193 10 case PR_ONE:
194 10 ▶ return (i == 1) ? PI_CAT_ONE : PI_CAT_OTHER;
Branch: 1 branch
BranchTrueFalseTotal
return (i == 1) ? PI_CAT_ONE : PI_CAT_OTHER; 3 7 10
195  
196 6 case PR_ZERO_ONE:
197 6 ▶ return (i == 0 || i == 1) ? PI_CAT_ONE : PI_CAT_OTHER;
Branch: 2 branches
BranchTrueFalseTotal
return (i == 0 || i == 1) ? PI_CAT_ONE : PI_CAT_OTHER; 4 2 6
return (i == 0 || i == 1) ? PI_CAT_ONE : PI_CAT_OTHER; 2 2 4
198  
199 5 case PR_RUSSIAN:
200 5 ▶ if (i10 == 1 && i100 != 11) return PI_CAT_ONE;
Branch: 2 branches
BranchTrueFalseTotal
if (i10 == 1 && i100 != 11) return PI_CAT_ONE; 3 2 5
if (i10 == 1 && i100 != 11) return PI_CAT_ONE; 2 1 3
201 3 ▶ if (i10 >= 2 && i10 <= 4 && !(i100 >= 12 && i100 <= 14))
Branch: 4 branches
BranchTrueFalseTotal
if (i10 >= 2 && i10 <= 4 && !(i100 >= 12 && i100 <= 14)) 2 1 3
if (i10 >= 2 && i10 <= 4 && !(i100 >= 12 && i100 <= 14)) 1 1 2
if (i10 >= 2 && i10 <= 4 && !(i100 >= 12 && i100 <= 14)) 0 1 1
if (i10 >= 2 && i10 <= 4 && !(i100 >= 12 && i100 <= 14)) 0 0 0
202 1 return PI_CAT_FEW;
203 2 return PI_CAT_MANY;
204  
205 11 case PR_POLISH:
206 11 ▶ if (i == 1) return PI_CAT_ONE;
Branch: 1 branch
BranchTrueFalseTotal
if (i == 1) return PI_CAT_ONE; 2 9 11
207 9 ▶ if (i10 >= 2 && i10 <= 4 && !(i100 >= 12 && i100 <= 14))
Branch: 4 branches
BranchTrueFalseTotal
if (i10 >= 2 && i10 <= 4 && !(i100 >= 12 && i100 <= 14)) 8 1 9
if (i10 >= 2 && i10 <= 4 && !(i100 >= 12 && i100 <= 14)) 6 2 8
if (i10 >= 2 && i10 <= 4 && !(i100 >= 12 && i100 <= 14)) 3 3 6
if (i10 >= 2 && i10 <= 4 && !(i100 >= 12 && i100 <= 14)) 1 2 3
208 4 return PI_CAT_FEW;
209 5 return PI_CAT_MANY;
210  
211 0 case PR_CZECH:
212 0 ▶ if (i == 1) return PI_CAT_ONE;
Branch: 1 branch
BranchTrueFalseTotal
if (i == 1) return PI_CAT_ONE; 0 0 0
213 0 ▶ if (i >= 2 && i <= 4) return PI_CAT_FEW;
Branch: 2 branches
BranchTrueFalseTotal
if (i >= 2 && i <= 4) return PI_CAT_FEW; 0 0 0
if (i >= 2 && i <= 4) return PI_CAT_FEW; 0 0 0
214 0 return PI_CAT_OTHER;
215  
216 0 case PR_LITHUANIAN:
217 0 ▶ if (i10 == 1 && !(i100 >= 11 && i100 <= 19)) return PI_CAT_ONE;
Branch: 3 branches
BranchTrueFalseTotal
if (i10 == 1 && !(i100 >= 11 && i100 <= 19)) return PI_CAT_ONE; 0 0 0
if (i10 == 1 && !(i100 >= 11 && i100 <= 19)) return PI_CAT_ONE; 0 0 0
if (i10 == 1 && !(i100 >= 11 && i100 <= 19)) return PI_CAT_ONE; 0 0 0
218 0 ▶ if (i10 >= 2 && i10 <= 9 && !(i100 >= 11 && i100 <= 19))
Branch: 4 branches
BranchTrueFalseTotal
if (i10 >= 2 && i10 <= 9 && !(i100 >= 11 && i100 <= 19)) 0 0 0
if (i10 >= 2 && i10 <= 9 && !(i100 >= 11 && i100 <= 19)) 0 0 0
if (i10 >= 2 && i10 <= 9 && !(i100 >= 11 && i100 <= 19)) 0 0 0
if (i10 >= 2 && i10 <= 9 && !(i100 >= 11 && i100 <= 19)) 0 0 0
219 0 return PI_CAT_FEW;
220 0 return PI_CAT_OTHER;
221  
222 0 case PR_ROMANIAN:
223 0 ▶ if (i == 1) return PI_CAT_ONE;
Branch: 1 branch
BranchTrueFalseTotal
if (i == 1) return PI_CAT_ONE; 0 0 0
224 0 ▶ if (i == 0 || (i100 >= 1 && i100 <= 19)) return PI_CAT_FEW;
Branch: 3 branches
BranchTrueFalseTotal
if (i == 0 || (i100 >= 1 && i100 <= 19)) return PI_CAT_FEW; 0 0 0
if (i == 0 || (i100 >= 1 && i100 <= 19)) return PI_CAT_FEW; 0 0 0
if (i == 0 || (i100 >= 1 && i100 <= 19)) return PI_CAT_FEW; 0 0 0
225 0 return PI_CAT_OTHER;
226  
227 6 case PR_ARABIC:
228 6 ▶ if (i == 0) return PI_CAT_ZERO;
Branch: 1 branch
BranchTrueFalseTotal
if (i == 0) return PI_CAT_ZERO; 1 5 6
229 5 ▶ if (i == 1) return PI_CAT_ONE;
Branch: 1 branch
BranchTrueFalseTotal
if (i == 1) return PI_CAT_ONE; 1 4 5
230 4 ▶ if (i == 2) return PI_CAT_TWO;
Branch: 1 branch
BranchTrueFalseTotal
if (i == 2) return PI_CAT_TWO; 1 3 4
231 3 ▶ if (i100 >= 3 && i100 <= 10) return PI_CAT_FEW;
Branch: 2 branches
BranchTrueFalseTotal
if (i100 >= 3 && i100 <= 10) return PI_CAT_FEW; 2 1 3
if (i100 >= 3 && i100 <= 10) return PI_CAT_FEW; 1 1 2
232 2 ▶ if (i100 >= 11 && i100 <= 99) return PI_CAT_MANY;
Branch: 2 branches
BranchTrueFalseTotal
if (i100 >= 11 && i100 <= 99) return PI_CAT_MANY; 1 1 2
if (i100 >= 11 && i100 <= 99) return PI_CAT_MANY; 1 0 1
233 1 return PI_CAT_OTHER;
234  
235 0 default:
236 0 return PI_CAT_OTHER;
237 }
238 }
239  
240 /* Is this key one of the six category names? Used at boot to tell a plural
241 * map from an ordinary nested object. */
242 2 static int pi_cat_name(const char *k, STRLEN kl) {
243 int i;
244 4 ▶ for (i = 0; i < PI_CAT_N; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < PI_CAT_N; i++) { 4 0 4
245 4 const char *n = PI_CAT_NAME[i];
246 4 ▶ if (strlen(n) == kl && memcmp(n, k, kl) == 0) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (strlen(n) == kl && memcmp(n, k, kl) == 0) return 1; 2 2 4
if (strlen(n) == kl && memcmp(n, k, kl) == 0) return 1; 2 0 2
247 }
248 0 return 0;
249 }
250  
251 #endif /* PUNK_PLURAL_H */
« include/punk/punk_ua.h ↑ Summary include/punk/punk_obs.h »