include/punk/punk_plural.h

filter: Statementstmt 93.2% 69 / 74 Branchbran 89.4% 101 / 114 (3 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 90.9% 170 / 188 (3 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_asset.h ↑ Summary include/punk/punk_route.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 6973 static int pi_rule_tag_eq(const char *a, const char *b, STRLEN n) {
127 STRLEN i;
128 7617 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 7350 267 7617
129 7350 char x = a[i], y = b[i];
130 7350 ▶ if (x >= 'A' && x <= 'Z') x = (char)(x - 'A' + 'a');
Branch: 2 branches
BranchTrueFalseTotal
if (x >= 'A' && x <= 'Z') x = (char)(x - 'A' + 'a'); 7346 4 7350
if (x >= 'A' && x <= 'Z') x = (char)(x - 'A' + 'a'); 8 7338 7346
131 /*
132 # uncoverable branch true
133 # uncoverable condition left count:1 class:ignore_covered_err
134 # uncoverable condition left count:2 class:ignore_covered_err */
135 7350 ▶ if (y >= 'A' && y <= 'Z') y = (char)(y - 'A' + 'a');
Branch: 2 branches
BranchTrueFalseTotal
if (y >= 'A' && y <= 'Z') y = (char)(y - 'A' + 'a'); 7346 4 7350
if (y >= 'A' && y <= 'Z') y = (char)(y - 'A' + 'a'); 0 7346 7346
136 7350 ▶ if (x != y) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (x != y) return 0; 6706 644 7350
137 }
138 267 return 1;
139 }
140  
141 /* The rule for a language tag, or PR_NONE.
142 *
143 * Two passes, and the order is the whole point: the whole tag first so that
144 * pt-PT is not given pt's rule, then the primary subtag so that en-GB is
145 * still given en's. A tag with no hyphen cannot match a hyphenated row, so
146 * the first pass changes no existing answer. */
147 271 static pi_rule pi_rule_for(const char *tag, STRLEN tl) {
148 271 STRLEN prim = 0;
149 size_t i;
150  
151 7772 ▶ 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++) { 7757 15 7772
152 7757 const char *l = PI_RULES[i].lang;
153 7757 ▶ 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)) 6841 916 7757
if (strlen(l) == tl && pi_rule_tag_eq(l, tag, tl)) 256 6585 6841
154 256 return PI_RULES[i].rule;
155 }
156  
157 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
158 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
159 /*
160 # uncoverable branch false
161 # uncoverable condition right count:1 class:ignore_covered_err */
162 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
163 132 const char *l = PI_RULES[i].lang;
164 132 ▶ if (strlen(l) == prim && pi_rule_tag_eq(l, tag, prim)) /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (strlen(l) == prim && pi_rule_tag_eq(l, tag, prim)) /* # uncoverable condition right count:1 class:ignore_covered_err */ 132 0 132
if (strlen(l) == prim && pi_rule_tag_eq(l, tag, prim)) /* # uncoverable condition right count:1 class:ignore_covered_err */ 11 121 132
165 11 return PI_RULES[i].rule;
166 }
167 0 return PR_NONE; /* # uncoverable statement */
168 }
169  
170 /* Which category does `count` take under `rule`?
171 *
172 * `is_int` is the `v = 0` operand: false means the count has visible fraction
173 * digits, which nearly every rule below routes to `other`.
174 */
175 72 static pi_pcat pi_plural(pi_rule rule, double count, int is_int) {
176 /* Rules are written over the ABSOLUTE value: -1 item and 1 item take the
177 * same form in every language here. */
178 72 ▶ double n = count < 0 ? -count : count; /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
double n = count < 0 ? -count : count; /* # uncoverable condition left count:1 class:ignore_covered_err */ 0 72 72
179 /* a NaN or an infinity cast to UV is undefined in C; both take `other` */
180 /*
181 # uncoverable condition right count:1 class:ignore_covered_err
182 # uncoverable condition right count:2 class:ignore_covered_err */
183 72 ▶ 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; 72 0 72
UV i = (Perl_isinfnan(n) || n >= (double)UV_MAX) ? 0 : (UV)n; 72 0 72
184 UV i10, i100;
185  
186 72 ▶ if (!is_int) {
Branch: 1 branch
BranchTrueFalseTotal
if (!is_int) { 3 69 72
187 /* The fractional branch. Only Czech and Romanian name it; everywhere
188 * else a number with a fraction is `other`. */
189 3 switch (rule) { /* # uncoverable branch true count:2 */
190 1 case PR_CZECH: return PI_CAT_MANY;
191 0 case PR_ROMANIAN: return PI_CAT_FEW; /* # uncoverable statement */
192 2 default: return PI_CAT_OTHER;
193 }
194 }
195  
196 69 i10 = i % 10;
197 69 i100 = i % 100;
198  
199 69 switch (rule) { /* # uncoverable branch true count:10 */
200 3 case PR_OTHER:
201 3 return PI_CAT_OTHER;
202  
203 10 case PR_ONE:
204 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
205  
206 6 case PR_ZERO_ONE:
207 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
208  
209 5 case PR_RUSSIAN:
210 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
211 /*
212 # uncoverable condition right count:3 class:ignore_covered_err
213 # uncoverable condition left count:4 class:ignore_covered_err
214 # uncoverable condition right count:4 class:ignore_covered_err */
215 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
216 1 return PI_CAT_FEW;
217 2 return PI_CAT_MANY;
218  
219 11 case PR_POLISH:
220 11 ▶ if (i == 1) return PI_CAT_ONE;
Branch: 1 branch
BranchTrueFalseTotal
if (i == 1) return PI_CAT_ONE; 2 9 11
221 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
222 4 return PI_CAT_FEW;
223 5 return PI_CAT_MANY;
224  
225 9 case PR_CZECH:
226 9 ▶ if (i == 1) return PI_CAT_ONE;
Branch: 1 branch
BranchTrueFalseTotal
if (i == 1) return PI_CAT_ONE; 2 7 9
227 7 ▶ if (i >= 2 && i <= 4) return PI_CAT_FEW;
Branch: 2 branches
BranchTrueFalseTotal
if (i >= 2 && i <= 4) return PI_CAT_FEW; 6 1 7
if (i >= 2 && i <= 4) return PI_CAT_FEW; 3 3 6
228 4 return PI_CAT_OTHER;
229  
230 10 case PR_LITHUANIAN:
231 10 ▶ 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; 4 6 10
if (i10 == 1 && !(i100 >= 11 && i100 <= 19)) return PI_CAT_ONE; 3 1 4
if (i10 == 1 && !(i100 >= 11 && i100 <= 19)) return PI_CAT_ONE; 1 2 3
232 8 ▶ if (i10 >= 2 && i10 <= 9 && !(i100 >= 11 && i100 <= 19)) /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 4 branches
BranchTrueFalseTotal
if (i10 >= 2 && i10 <= 9 && !(i100 >= 11 && i100 <= 19)) /* # uncoverable condition right count:2 class:ignore_covered_err */ 4 4 8
if (i10 >= 2 && i10 <= 9 && !(i100 >= 11 && i100 <= 19)) /* # uncoverable condition right count:2 class:ignore_covered_err */ 4 0 4
if (i10 >= 2 && i10 <= 9 && !(i100 >= 11 && i100 <= 19)) /* # uncoverable condition right count:2 class:ignore_covered_err */ 2 2 4
if (i10 >= 2 && i10 <= 9 && !(i100 >= 11 && i100 <= 19)) /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 1 2
233 3 return PI_CAT_FEW;
234 5 return PI_CAT_OTHER;
235  
236 9 case PR_ROMANIAN:
237 9 ▶ if (i == 1) return PI_CAT_ONE;
Branch: 1 branch
BranchTrueFalseTotal
if (i == 1) return PI_CAT_ONE; 1 8 9
238 8 ▶ 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; 7 1 8
if (i == 0 || (i100 >= 1 && i100 <= 19)) return PI_CAT_FEW; 6 1 7
if (i == 0 || (i100 >= 1 && i100 <= 19)) return PI_CAT_FEW; 4 2 6
239 3 return PI_CAT_OTHER;
240  
241 6 case PR_ARABIC:
242 6 ▶ if (i == 0) return PI_CAT_ZERO;
Branch: 1 branch
BranchTrueFalseTotal
if (i == 0) return PI_CAT_ZERO; 1 5 6
243 5 ▶ if (i == 1) return PI_CAT_ONE;
Branch: 1 branch
BranchTrueFalseTotal
if (i == 1) return PI_CAT_ONE; 1 4 5
244 4 ▶ if (i == 2) return PI_CAT_TWO;
Branch: 1 branch
BranchTrueFalseTotal
if (i == 2) return PI_CAT_TWO; 1 3 4
245 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
246 2 ▶ if (i100 >= 11 && i100 <= 99) return PI_CAT_MANY; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (i100 >= 11 && i100 <= 99) return PI_CAT_MANY; /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 1 2
if (i100 >= 11 && i100 <= 99) return PI_CAT_MANY; /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 0 1
247 1 return PI_CAT_OTHER;
248  
249 0 default: /* # uncoverable statement */
250 0 return PI_CAT_OTHER; /* # uncoverable statement */
251 }
252 }
253  
254 /* Is this key one of the six category names? Used at boot to tell a plural
255 * map from an ordinary nested object. */
256 2 static int pi_cat_name(const char *k, STRLEN kl) {
257 int i;
258 /*
259 # uncoverable branch false
260 # uncoverable condition right count:1 class:ignore_covered_err */
261 4 ▶ for (i = 0; i < PI_CAT_N; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < PI_CAT_N; i++) { 4 0 4
262 4 const char *n = PI_CAT_NAME[i];
263 4 ▶ if (strlen(n) == kl && memcmp(n, k, kl) == 0) return 1; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (strlen(n) == kl && memcmp(n, k, kl) == 0) return 1; /* # uncoverable condition right count:2 class:ignore_covered_err */ 2 2 4
if (strlen(n) == kl && memcmp(n, k, kl) == 0) return 1; /* # uncoverable condition right count:2 class:ignore_covered_err */ 2 0 2
264 }
265 0 return 0; /* # uncoverable statement */
266 }
267  
268 #endif /* PUNK_PLURAL_H */
« include/punk/punk_asset.h ↑ Summary include/punk/punk_route.h »