xs/i18n.xs

filter: Statementstmt 87.3% 177 / 205 (2 uncoverable) Branchbran 48.0% 125 / 258 (1 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 65.4% 302 / 463 (3 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/validate.xs ↑ Summary xs/upload.xs »
1 MODULE = Punk PACKAGE = Punk::Plugin::I18n
2  
3 PROTOTYPES: DISABLE
4  
5 # Translations and language negotiation. punk_i18n.h holds the catalogue and
6 # says why it is bytes rather than SVs; punk_lang.h holds the negotiation and
7 # says why punk_accept.h could not be reused whole.
8  
9 # _build($app, \%catalogues, \%opts)
10 #
11 # The catalogues arrive DECODED, from register in the .pm - see punk_i18n.h
12 # for why the directory walk and the JSON parse are Perl's job and the arena
13 # is C's.
14 #
15 # Called at boot, in the parent: the arena is read-only at request time, so
16 # building it before the fork shares it across every worker.
17 void
18 _build(self, app, cats, opts = &PL_sv_undef)
19 SV *self
20 SV *app
21 SV *cats
22 SV *opts
23 CODE:
24 {
25 152 HV *h = app_hv(aTHX_ app);
26 152 HV *cfg = newHV();
27 152 HV *o = NULL;
28 SV **dir, **def, **param, **cookie;
29 152 const char *defp = NULL;
30 152 STRLEN defl = 0;
31 pi_arena *ar;
32  
33 PERL_UNUSED_VAR(self);
34  
35 152 ▶ # uncoverable branch false
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch false 152 0 152
36 152 ▶ # uncoverable condition right count:1 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition right count:1 class:ignore_covered_err 152 0 152
# uncoverable condition right count:1 class:ignore_covered_err 0 152 152
37 0 if (SvOK(opts)) {
38 152 # uncoverable branch true
39 # uncoverable condition left count:1 class:ignore_covered_err
40 152 ▶ # uncoverable condition left count:2 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition left count:2 class:ignore_covered_err 152 0 152
# uncoverable condition left count:2 class:ignore_covered_err 0 152 152
41 0 if (!(SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV))
42 croak("Punk::Plugin::I18n: options must be a hashref"); /* # uncoverable statement */
43 152 ▶ o = (HV *)SvRV(opts);
Branch: 1 branch
BranchTrueFalseTotal
o = (HV *)SvRV(opts); 152 0 152
44 152 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
} 152 0 152
45 152 ▶ # uncoverable branch true
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch true 152 0 152
46 152 ▶ # uncoverable condition left count:1 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition left count:1 class:ignore_covered_err 152 0 152
47 # uncoverable condition left count:2 class:ignore_covered_err
48 152 ▶ if (!(SvROK(cats) && SvTYPE(SvRV(cats)) == SVt_PVHV))
Branch: 4 branches
BranchTrueFalseTotal
if (!(SvROK(cats) && SvTYPE(SvRV(cats)) == SVt_PVHV)) 152 0 152
if (!(SvROK(cats) && SvTYPE(SvRV(cats)) == SVt_PVHV)) 152 0 152
if (!(SvROK(cats) && SvTYPE(SvRV(cats)) == SVt_PVHV)) 152 0 152
if (!(SvROK(cats) && SvTYPE(SvRV(cats)) == SVt_PVHV)) 0 152 152
49 0 croak("Punk::Plugin::I18n: catalogues must be a hashref"); /* # uncoverable statement */
50  
51 dir = o ? hv_fetchs(o, "dir", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
52 def = o ? hv_fetchs(o, "default", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
53 152 param = o ? hv_fetchs(o, "param", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
54 152 cookie = o ? hv_fetchs(o, "cookie", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
55  
56 34 ▶ # uncoverable branch true
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable branch true 34 0 34
# uncoverable branch true 34 0 34
# uncoverable branch true 34 0 34
57 # uncoverable condition left count:1 class:ignore_covered_err
58 34 # uncoverable condition left count:2 class:ignore_covered_err
59 34 ▶ # uncoverable condition left count:3 class:ignore_covered_err
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable condition left count:3 class:ignore_covered_err 0 34 34
# uncoverable condition left count:3 class:ignore_covered_err 0 0 0
# uncoverable condition left count:3 class:ignore_covered_err 0 0 0
60 # uncoverable condition left count:4 class:ignore_covered_err
61 if (!(def && *def && SvOK(*def) && SvCUR(*def)))
62 34 ▶ croak("Punk::Plugin::I18n: `default` is required - it is the " /* # uncoverable statement */
Branch: 3 branches
BranchTrueFalseTotal
croak("Punk::Plugin::I18n: `default` is required - it is the " /* # uncoverable statement */ 0 34 34
croak("Punk::Plugin::I18n: `default` is required - it is the " /* # uncoverable statement */ 0 0 0
croak("Punk::Plugin::I18n: `default` is required - it is the " /* # uncoverable statement */ 0 0 0
63 "answer when negotiation finds nothing, and an application "
64 "with no answer to that renders empty pages");
65  
66 defp = SvPV_const(*def, defl);
67 34 ar = pi_arena_build(aTHX_ cats, defp, defl);
68 34  
69 (void)hv_stores(cfg, "dir",
70 (dir && *dir && SvOK(*dir)) ? newSVsv(*dir) : newSVpvs(""));
71 34 (void)hv_stores(cfg, "default", newSVsv(*def));
72 34 ▶ (void)hv_stores(cfg, "param",
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(cfg, "param", 34 0 34
73 (param && *param && SvOK(*param)) ? newSVsv(*param)
74 : newSVpvs("lang"));
75 34 (void)hv_stores(cfg, "cookie",
76 34 (cookie && *cookie && SvOK(*cookie)) ? newSVsv(*cookie)
77 34 : newSVpvs("punk.lang"));
78 34 ▶ /* The arena as an integer, because it is a C pointer and the only
Branch: 1 branch
BranchTrueFalseTotal
/* The arena as an integer, because it is a C pointer and the only 34 0 34
79 * thing Perl should ever do with it is hand it back. */
80 (void)hv_stores(cfg, "_arena", newSViv(PTR2IV(ar)));
81 (void)hv_stores(h, K_I18N, newRV_noinc((SV *)cfg));
82 34  
83 34 ▶ { /* $c->locale($key, %substitutions), and $c->locale for the tag. */
Branch: 1 branch
BranchTrueFalseTotal
{ /* $c->locale($key, %substitutions), and $c->locale for the tag. */ 34 0 34
84 CV *cv = get_cv("Punk::Plugin::I18n::_locale", 0);
85 # uncoverable branch false class:ignore_covered_err
86 34 # uncoverable condition right count:1 class:ignore_covered_err
87 34 if (cv) {
88 34 SV *argv[2];
89 34 ▶ SV *r;
Branch: 1 branch
BranchTrueFalseTotal
SV *r; 34 0 34
90 argv[0] = sv_2mortal(newSVpvs("locale"));
91 argv[1] = sv_2mortal(newRV_inc((SV *)cv));
92 r = pcx_call_meth(aTHX_ app, "helper", argv, 2, 1);
93 # uncoverable branch false
94 # uncoverable condition right count:1 class:ignore_covered_err
95 if (r) SvREFCNT_dec(r);
96 }
97 }
98 { /* $c->locale_html: the same, with the values escaped for markup. */
99 CV *cv = get_cv("Punk::Plugin::I18n::_locale_html", 0);
100 # uncoverable branch false class:ignore_covered_err
101 # uncoverable condition right count:1 class:ignore_covered_err
102 if (cv) {
103 SV *argv[2];
104 SV *r;
105 argv[0] = sv_2mortal(newSVpvs("locale_html"));
106 argv[1] = sv_2mortal(newRV_inc((SV *)cv));
107 r = pcx_call_meth(aTHX_ app, "helper", argv, 2, 1);
108 # uncoverable branch false
109 # uncoverable condition right count:1 class:ignore_covered_err
110 if (r) SvREFCNT_dec(r);
111 }
112 }
113 }
114  
115 # $c->locale - the negotiated tag, e.g. 'en-GB'
116 # $c->locale($key) - the translation
117 # $c->locale($key, %subs) - interpolated
118 #
119 # Resolved ONCE per request and kept in the env, not in a C static: Punk
120 # dispatches asynchronously, so a worker can have several requests in flight
121 # and a "current locale" in a static would hand one request's language to
122 # another - invisibly, and only under load.
123 #
124 # $c->locale_html($key, %subs) is the same call with every substituted value
125 # HTML-escaped, for a handler that is writing markup itself. The translation
126 # is left as written; a key that is in no catalogue comes back escaped too,
127 # since it is the caller's string and may not be the caller's own.
128 SV *
129 _locale(c, ...)
130 SV *c
131 ALIAS:
132 _locale_html = 1
133 CODE:
134 {
135 165 HV *cfg = pi_cfg_of(aTHX_ c);
136 165 pi_arena *ar = pi_arena_of(aTHX_ cfg);
137 const pi_cat *cat;
138 165 RETVAL = NULL;
139  
140 165 ▶ # uncoverable branch true
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch true 0 165 165
141 # uncoverable condition left count:1 class:ignore_covered_err
142 165 if (!ar) croak("Punk::Plugin::I18n: the plugin is not registered on "
143 165 ▶ "this application");
Branch: 1 branch
BranchTrueFalseTotal
"this application"); 0 165 165
144 cat = pi_for_request(aTHX_ c, cfg, ar);
145 165 ▶ # uncoverable branch true
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch true 6 159 165
146 6 # uncoverable condition left count:1 class:ignore_covered_err
147 if (!cat) XSRETURN_UNDEF;
148  
149 if (items < 2) { /* the tag itself */
150 159 RETVAL = newSVpvn(cat->tag, cat->taglen);
151 159 }
152 159 else {
153 STRLEN kl;
154 const char *k = SvPV_const(ST(1), kl);
155 STRLEN vl = 0;
156 159 const char *v = NULL;
157 char pkey[PI_KEY_MAX + 1];
158 int r;
159 int j;
160 int vu = 0;
161  
162 170 ▶ /* `count => N` selects a plural category, so `items` reaches
Branch: 1 branch
BranchTrueFalseTotal
/* `count => N` selects a plural category, so `items` reaches 83 87 170
163 * `items.few` under a rule that has one. The count stays in the
164 83 * substitutions as well, because the string it chose almost
165 83 ▶ * always interpolates it: "{count} produkty". */
Branch: 2 branches
BranchTrueFalseTotal
* always interpolates it: "{count} produkty". */ 72 11 83
* always interpolates it: "{count} produkty". */ 0 72 72
166 for (j = 2; j + 1 < items; j += 2) {
167 72 STRLEN nl;
168 72 ▶ const char *nm = SvPV_const(ST(j), nl);
Branch: 1 branch
BranchTrueFalseTotal
const char *nm = SvPV_const(ST(j), nl); 72 0 72
169 72 if (nl != 5 || memcmp(nm, "count", 5) != 0) continue; /* # uncoverable condition left count:2 class:ignore_covered_err */
170 {
171 SV *cv = ST(j + 1);
172 NV nv = SvOK(cv) ? SvNV(cv) : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */
173 int is_int = 1;
174 /* At the top of the block, not beside their first use.
175 * This header has to compile on every perl from 5.10,
176 * and a declaration after a statement is C99 - MSVC and
177 * -Wdeclaration-after-statement both refuse it. */
178 pi_rule rule;
179 pi_pcat pc;
180 const char *cn;
181 STRLEN cnl, want;
182  
183 /* CLDR's `v` is the number of VISIBLE fraction digits,
184 * which is a property of how the count was written and
185 72 ▶ * not of its value: "1.0" has v = 1 and takes English's
Branch: 1 branch
BranchTrueFalseTotal
* not of its value: "1.0" has v = 1 and takes English's 71 1 72
186 * `other` - "1.0 items" - where 1 takes `one`. So the
187 71 * string form decides when there is one, and the numeric
188 * value only when there is not. */
189 172 ▶ if (SvPOK(cv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvPOK(cv)) { 104 68 172
190 104 ▶ STRLEN sl;
Branch: 1 branch
BranchTrueFalseTotal
STRLEN sl; 3 101 104
191 const char *sp = SvPV_const(cv, sl);
192 STRLEN z;
193 for (z = 0; z < sl; z++)
194 72 ▶ if (sp[z] == '.') { is_int = 0; break; }
Branch: 3 branches
BranchTrueFalseTotal
if (sp[z] == '.') { is_int = 0; break; } 69 3 72
if (sp[z] == '.') { is_int = 0; break; } 69 0 69
if (sp[z] == '.') { is_int = 0; break; } 0 69 69
195 0 }
196 /* I_V, not a bare cast: a NaN or an infinity cast to IV
197 72 * is undefined in C, and a count can be either */
198 144 ▶ # uncoverable branch true
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch true 0 72 72
199 72 # uncoverable condition left count:1 class:ignore_covered_err
200 72 # uncoverable condition left count:2 class:ignore_covered_err
201 72 # uncoverable condition left count:3 class:ignore_covered_err
202 if (is_int && (Perl_isinfnan(nv) || nv != (NV)I_V(nv)))
203 is_int = 0; /* # uncoverable statement */
204 72 ▶ 
Branch: 2 branches
BranchTrueFalseTotal
72 0 72
72 0 72
205 rule = pi_rule_for(cat->tag, cat->taglen);
206 # uncoverable branch true class:ignore_covered_err
207 72 # uncoverable condition left count:1 class:ignore_covered_err
208 72 if (rule == PR_NONE) break; /* checked at boot */
209 72 pc = pi_plural(rule, (double)nv, is_int);
210 72 cn = PI_CAT_NAME[pc];
211 cnl = strlen(cn);
212 72 ▶ /* bounded by the WIDEST category: the `other` fallback
Branch: 1 branch
BranchTrueFalseTotal
/* bounded by the WIDEST category: the `other` fallback 69 3 72
213 69 * below writes five bytes where a three-letter one fit */
214 # uncoverable branch true class:ignore_covered_err
215 # uncoverable branch false class:ignore_covered_err
216 # uncoverable condition left count:1 class:ignore_covered_err
217 # uncoverable condition left count:2 class:ignore_covered_err
218 if (kl + 1 + 5 > PI_KEY_MAX || kl + 1 + cnl > PI_KEY_MAX)
219 break; /* # uncoverable statement */
220 3  
221 3 memcpy(pkey, k, kl);
222 3 ▶ pkey[kl] = '.';
Branch: 1 branch
BranchTrueFalseTotal
pkey[kl] = '.'; 2 1 3
223 memcpy(pkey + kl + 1, cn, cnl);
224 want = kl + 1 + cnl;
225 72  
226 if (pi_get(cat, pkey, want, &vl, NULL)) {
227 k = pkey; kl = want;
228 }
229 else {
230 /* `other` is the category every rule can reach and
231 159 * the one a catalogue must carry. A category the
232 * translator did not write falls back to it rather
233 159 ▶ * than to the key. */
Branch: 1 branch
BranchTrueFalseTotal
* than to the key. */ 49 110 159
234 memcpy(pkey + kl + 1, "other", 5);
235 want = kl + 1 + 5;
236 if (pi_get(cat, pkey, want, &vl, NULL)) { k = pkey; kl = want; }
237 }
238 }
239 break;
240 49 ▶ }
Branch: 2 branches
BranchTrueFalseTotal
} 47 2 49
} 21 26 47
241 21  
242 49 ▶ /* The same lookup the template hash uses - the counters and the
Branch: 1 branch
BranchTrueFalseTotal
/* The same lookup the template hash uses - the counters and the 1 48 49
243 1 * warning live in there, so the two paths cannot disagree about
244 1 * what is missing. */
245 r = pi_lookup(aTHX_ ar, cat, k, kl, &v, &vl, &vu);
246 48  
247 if (r != PI_HIT) {
248 /* The KEY, never the empty string. An empty gap hides the
249 * omission until a user finds it; the key is visible in the
250 49 ▶ * page and greppable in the logs.
Branch: 1 branch
BranchTrueFalseTotal
* page and greppable in the logs. 0 49 49
251 *
252 110 ▶ * A LEVEL asked for as a string is the same answer: there is
Branch: 1 branch
BranchTrueFalseTotal
* A LEVEL asked for as a string is the same answer: there is 83 27 110
253 * no translation at `items`, only below it. */
254 if (r == PI_MISSING && pi_dev(aTHX_ cfg))
255 pi_warn_missing(aTHX_ c, k, kl);
256 if (ix) {
257 83 RETVAL = newSVpvn("", 0);
258 ps_html_esc(aTHX_ RETVAL, k, kl);
259 }
260 else RETVAL = newSVpvn(k, kl);
261 27 /* the key echoed back, so it carries the CALLER's flag and
262 * not the block's - a non-ASCII key must come back the way
263 * it went in */
264 # uncoverable branch true
265 27 ▶ # uncoverable condition left count:1 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition left count:1 class:ignore_covered_err 2 25 27
266 if (SvUTF8(ST(1))) SvUTF8_on(RETVAL);
267 }
268 else if (items > 2) {
269 /* pi_interpolate is told the flag and manages the result
270 * itself, because it is joining two encodings. Forcing the
271 * flag on afterwards is what corrupted a downgraded
272 * substitution. */
273 RETVAL = pi_interpolate(aTHX_ v, vl, vu, &ST(2), items - 2,
274 ix ? 1 : 0);
275 }
276 else {
277 RETVAL = newSVpvn(v, vl);
278 /* Conditional. The block records the flag per string, so a
279 * producer that handed over Latin-1 no longer has it
280 * relabelled as UTF-8 on the way out. */
281 if (vu) SvUTF8_on(RETVAL);
282 }
283 }
284 }
285 OUTPUT:
286 RETVAL
287  
288 # What this process has seen.
289 #
290 # `untranslated` is separate from `missing` on purpose: one is a bug and the
291 # other is a translation that has not been written yet, and an application
292 # that cannot tell them apart cannot measure its own coverage.
293 #
294 # `warned` makes "it did not warn in production" a number. Asserting on the
295 # ABSENCE of a warning also passes when the warning is broken.
296 void
297 stats(class = &PL_sv_undef)
298 SV *class
299 PPCODE:
300 {
301 PERL_UNUSED_VAR(class);
302 6 ▶ EXTEND(SP, 6);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 6); 0 6 6
303 6 mPUSHs(newSVpvs("missing")); mPUSHu(pi_n_missing);
304 6 mPUSHs(newSVpvs("untranslated")); mPUSHu(pi_n_untranslated);
305 6 mPUSHs(newSVpvs("warned")); mPUSHu(pi_n_warned);
306 6 XSRETURN(6);
307 }
308  
309 # The counters and the warned-about set, for tests that need a clean slate.
310 void
311 _reset(class = &PL_sv_undef)
312 SV *class
313 CODE:
314 PERL_UNUSED_VAR(class);
315 5 pi_n_missing = pi_n_untranslated = pi_n_warned = 0;
316 5 ▶ if (pi_seen) { SvREFCNT_dec((SV *)pi_seen); pi_seen = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (pi_seen) { SvREFCNT_dec((SV *)pi_seen); pi_seen = NULL; } 1 4 5
317  
318 MODULE = Punk PACKAGE = Punk::Plugin::I18n::Cat
319  
320 PROTOTYPES: DISABLE
321  
322 # The tied hash behind `{% locale.welcome %}`. punk_i18n.h says why it is tied
323 # rather than built, and what it needed from Template::Stencil 0.10.
324 #
325 # FETCH is almost the whole class: a template only ever reads.
326  
327 SV *
328 FETCH(self, key)
329 SV *self
330 SV *key
331 CODE:
332 {
333 2033 AV *o = (AV *)SvRV(self);
334 2033 SV **arsv = av_fetch(o, PIT_ARENA, 0);
335 2033 SV **idxs = av_fetch(o, PIT_CAT, 0);
336 2033 SV **pfx = av_fetch(o, PIT_PREFIX, 0);
337 2033 SV **cfgs = av_fetch(o, PIT_CFG, 0);
338 2033 SV **ctx = av_fetch(o, PIT_CTX, 0);
339 2033 SV **nds = av_fetch(o, PIT_NODE, 0);
340 2033 SV **dnds = av_fetch(o, PIT_DNODE, 0);
341 2033 ▶ # uncoverable condition right count:1 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition right count:1 class:ignore_covered_err 2033 0 2033
# uncoverable condition right count:1 class:ignore_covered_err 2033 0 2033
342 2033 ▶ # uncoverable condition right count:2 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition right count:2 class:ignore_covered_err 2033 0 2033
# uncoverable condition right count:2 class:ignore_covered_err 2033 0 2033
343 2033 ▶ pi_arena *ar = (arsv && *arsv) ? INT2PTR(pi_arena *, SvIV(*arsv)) : NULL;
Branch: 3 branches
BranchTrueFalseTotal
pi_arena *ar = (arsv && *arsv) ? INT2PTR(pi_arena *, SvIV(*arsv)) : NULL; 2033 0 2033
pi_arena *ar = (arsv && *arsv) ? INT2PTR(pi_arena *, SvIV(*arsv)) : NULL; 2033 0 2033
pi_arena *ar = (arsv && *arsv) ? INT2PTR(pi_arena *, SvIV(*arsv)) : NULL; 2033 0 2033
344 2033 ▶ # uncoverable condition right count:1 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition right count:1 class:ignore_covered_err 2033 0 2033
# uncoverable condition right count:1 class:ignore_covered_err 2033 0 2033
345 2033 ▶ # uncoverable condition right count:2 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition right count:2 class:ignore_covered_err 2033 0 2033
# uncoverable condition right count:2 class:ignore_covered_err 2033 0 2033
346 2033 ▶ int idx = (idxs && *idxs) ? (int)SvIV(*idxs) : -1;
Branch: 2 branches
BranchTrueFalseTotal
int idx = (idxs && *idxs) ? (int)SvIV(*idxs) : -1; 2033 0 2033
int idx = (idxs && *idxs) ? (int)SvIV(*idxs) : -1; 2033 0 2033
347 2033 # uncoverable condition right count:1 class:ignore_covered_err
348 # uncoverable condition right count:2 class:ignore_covered_err
349 2033 # uncoverable condition right count:3 class:ignore_covered_err
350 2033 HV *cfg = (cfgs && *cfgs && SvROK(*cfgs)) ? (HV *)SvRV(*cfgs) : NULL;
351 2033 # uncoverable condition right count:1 class:ignore_covered_err
352 # uncoverable condition right count:2 class:ignore_covered_err
353 2033 ▶ SV *c = (ctx && *ctx) ? *ctx : NULL;
Branch: 3 branches
BranchTrueFalseTotal
SV *c = (ctx && *ctx) ? *ctx : NULL; 2033 0 2033
SV *c = (ctx && *ctx) ? *ctx : NULL; 2033 0 2033
SV *c = (ctx && *ctx) ? *ctx : NULL; 0 2033 2033
354 2033 # uncoverable condition right count:1 class:ignore_covered_err
355 # uncoverable condition right count:2 class:ignore_covered_err
356 uint32_t node = (nds && *nds) ? (uint32_t)SvUV(*nds) : FZ_NOHANDLE;
357 # uncoverable condition right count:1 class:ignore_covered_err
358 2033 # uncoverable condition right count:2 class:ignore_covered_err
359 2033 uint32_t dnode = (dnds && *dnds) ? (uint32_t)SvUV(*dnds) : FZ_NOHANDLE;
360 uint32_t child = FZ_NOHANDLE, dchild = FZ_NOHANDLE;
361 const pi_cat *cat;
362 2033 ▶ STRLEN kl, vl = 0;
Branch: 1 branch
BranchTrueFalseTotal
STRLEN kl, vl = 0; 2024 9 2033
363 2024 const char *k, *vp = NULL;
364 2024 ▶ int r, vu = 0;
Branch: 1 branch
BranchTrueFalseTotal
int r, vu = 0; 0 2024 2024
365  
366 # uncoverable branch true
367 # uncoverable condition left count:1 class:ignore_covered_err
368 # uncoverable condition left count:2 class:ignore_covered_err
369 # uncoverable condition left count:3 class:ignore_covered_err
370 if (!ar || idx < 0 || idx >= ar->ncat) XSRETURN_UNDEF;
371 cat = &ar->cat[idx];
372  
373 /* One probe from where the last FETCH stopped, not a re-walk of the
374 9 ▶ * whole path from the catalogue root. */
Branch: 3 branches
BranchTrueFalseTotal
* whole path from the catalogue root. */ 9 0 9
* whole path from the catalogue root. */ 9 0 9
* whole path from the catalogue root. */ 1 8 9
375 1 k = SvPV_const(key, kl);
376 1 r = pi_step(aTHX_ ar, cat, node, dnode, k, kl,
377 1 &vp, &vl, &vu, &child, &dchild);
378  
379 8 if (r == PI_HIT) {
380 9 RETVAL = newSVpvn(vp, vl);
381 # uncoverable branch true
382 9 ▶ # uncoverable condition left count:1 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition left count:1 class:ignore_covered_err 8 1 9
383 if (vu) SvUTF8_on(RETVAL);
384 }
385 else {
386 8 /* The joined path is built only when it is going to be USED -
387 * to descend, so a nested miss can name itself, or to be the
388 * answer to a miss. A hit never pays for it. */
389 SV *full;
390 STRLEN fl;
391 const char *fp;
392  
393 # uncoverable condition right count:1 class:ignore_covered_err
394 # uncoverable condition right count:2 class:ignore_covered_err
395 1 ▶ if (pfx && *pfx && SvOK(*pfx)) {
Branch: 4 branches
BranchTrueFalseTotal
if (pfx && *pfx && SvOK(*pfx)) { 1 0 1
if (pfx && *pfx && SvOK(*pfx)) { 0 1 1
if (pfx && *pfx && SvOK(*pfx)) { 0 0 0
if (pfx && *pfx && SvOK(*pfx)) { 0 0 0
396 0 full = sv_2mortal(newSVsv(*pfx));
397 1 sv_catpvs(full, ".");
398 1 ▶ sv_catpvn(full, k, kl);
Branch: 1 branch
BranchTrueFalseTotal
sv_catpvn(full, k, kl); 0 1 1
399 }
400 else full = sv_2mortal(newSVpvn(k, kl));
401 fp = SvPV_const(full, fl);
402  
403 if (r == PI_LEVEL) {
404 /* Descend. `items` in a catalogue holding `items.one` is not
405 * a translation and not missing - it is a level, and the
406 * template is part way down a path. */
407 RETVAL = pi_tied_hash(aTHX_ ar, idx, full, cfg, c,
408 child, dchild);
409 }
410 else {
411 /* The KEY, exactly as the handler path renders it. A
412 * template resolves a missing path to the empty string, and
413 * an omission that is visible in a handler and invisible in
414 * a template is the worse half to lose - which is the whole
415 * reason this hash is tied. */
416 # uncoverable branch true
417 # uncoverable condition left count:1 class:ignore_covered_err
418 # uncoverable condition right count:1 class:ignore_covered_err
419 # uncoverable condition left count:2 class:ignore_covered_err
420 # uncoverable condition left count:3 class:ignore_covered_err
421 # uncoverable condition right count:3 class:ignore_covered_err
422 # uncoverable condition left count:4 class:ignore_covered_err
423 # uncoverable condition right count:4 class:ignore_covered_err
424 if (cfg && pi_dev(aTHX_ cfg) && c && SvOK(c))
425 pi_warn_missing(aTHX_ c, fp, fl); /* # uncoverable statement */
426 RETVAL = newSVpvn(fp, fl);
427 # uncoverable branch true
428 # uncoverable condition left count:1 class:ignore_covered_err
429 if (SvUTF8(key)) SvUTF8_on(RETVAL);
430 }
431 }
432 }
433 OUTPUT:
434 RETVAL
435  
436 # A template asking whether a key is there, without building its value.
437 bool
438 # uncoverable statement
439 # uncoverable subroutine
440 EXISTS(self, key)
441 SV *self
442 SV *key
443 CODE:
444 {
445 0 AV *o = (AV *)SvRV(self); /* # uncoverable statement */
446 0 SV **arsv = av_fetch(o, PIT_ARENA, 0); /* # uncoverable statement */
447 0 SV **idxs = av_fetch(o, PIT_CAT, 0); /* # uncoverable statement */
448 0 SV **nds = av_fetch(o, PIT_NODE, 0); /* # uncoverable statement */
449 0 SV **dnds = av_fetch(o, PIT_DNODE, 0); /* # uncoverable statement */
450 0 ▶ # uncoverable statement
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable statement 0 0 0
# uncoverable statement 0 0 0
451 0 ▶ # uncoverable condition left count:1
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition left count:1 0 0 0
# uncoverable condition left count:1 0 0 0
452 0 ▶ # uncoverable condition right count:1
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition right count:1 0 0 0
# uncoverable condition right count:1 0 0 0
453 0 ▶ # uncoverable condition left count:2
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition left count:2 0 0 0
# uncoverable condition left count:2 0 0 0
454 # uncoverable condition right count:2
455 pi_arena *ar = (arsv && *arsv) ? INT2PTR(pi_arena *, SvIV(*arsv)) : NULL;
456 # uncoverable statement
457 0 # uncoverable condition left count:1
458 # uncoverable condition right count:1
459 0 ▶ # uncoverable condition left count:2
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable condition left count:2 0 0 0
# uncoverable condition left count:2 0 0 0
# uncoverable condition left count:2 0 0 0
460 0 # uncoverable condition right count:2
461 0 ▶ int idx = (idxs && *idxs) ? (int)SvIV(*idxs) : -1;
Branch: 1 branch
BranchTrueFalseTotal
int idx = (idxs && *idxs) ? (int)SvIV(*idxs) : -1; 0 0 0
462 0 # uncoverable statement
463 # uncoverable condition left count:1
464 # uncoverable condition right count:1
465 # uncoverable condition left count:2
466 # uncoverable condition right count:2
467 uint32_t node = (nds && *nds) ? (uint32_t)SvUV(*nds) : FZ_NOHANDLE;
468 0 ▶ # uncoverable statement
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable statement 0 0 0
# uncoverable statement 0 0 0
# uncoverable statement 0 0 0
469 0 ▶ # uncoverable condition left count:1
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition left count:1 0 0 0
470 0 # uncoverable condition right count:1
471 0 ▶ # uncoverable condition left count:2
Branch: 5 branches
BranchTrueFalseTotal
# uncoverable condition left count:2 0 0 0
# uncoverable condition left count:2 0 0 0
# uncoverable condition left count:2 0 0 0
# uncoverable condition left count:2 0 0 0
# uncoverable condition left count:2 0 0 0
472 0 ▶ # uncoverable condition right count:2
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition right count:2 0 0 0
473 0 uint32_t dnode = (dnds && *dnds) ? (uint32_t)SvUV(*dnds) : FZ_NOHANDLE;
474 0 STRLEN kl;
475 const char *k;
476 const pi_cat *cat, *d;
477 uint32_t slot = 0; /* # uncoverable statement */
478  
479 # uncoverable statement
480 # uncoverable branch true
481 # uncoverable branch false
482 # uncoverable condition left count:1
483 # uncoverable condition right count:1
484 # uncoverable condition left count:2
485 # uncoverable condition right count:2
486 # uncoverable condition left count:3
487 # uncoverable condition right count:3
488 if (!ar || idx < 0 || idx >= ar->ncat) XSRETURN_NO;
489 cat = &ar->cat[idx]; /* # uncoverable statement */
490 # uncoverable statement
491 # uncoverable condition left count:1
492 # uncoverable condition right count:1
493 d = (ar->def >= 0) ? &ar->cat[ar->def] : NULL;
494 k = SvPV_const(key, kl); /* # uncoverable statement */
495  
496 /* True for a leaf AND for a level: a template descending into
497 * `items` is asking whether it can, not whether `items` is itself a
498 * translation. Not routed through pi_step, because EXISTS must not
499 * move the missing counter - asking is not the same as reading. */
500 # uncoverable statement
501 # uncoverable branch true
502 # uncoverable branch false
503 # uncoverable condition left count:1
504 # uncoverable condition right count:1
505 # uncoverable condition left count:2
506 # uncoverable condition right count:2
507 # uncoverable condition left count:3
508 # uncoverable condition right count:3
509 # uncoverable condition left count:4
510 # uncoverable condition right count:4
511 if (cat->fz && node != FZ_NOHANDLE && PUNK_FZ
512 && (PUNK_FZ->probe)(cat->fz, node, k, kl, &slot) != FZ_ABSENT) /* # uncoverable statement */
513 XSRETURN_YES; /* # uncoverable statement */
514 # uncoverable statement
515 # uncoverable branch true
516 # uncoverable branch false
517 # uncoverable condition left count:1
518 # uncoverable condition right count:1
519 # uncoverable condition left count:2
520 # uncoverable condition right count:2
521 # uncoverable condition left count:3
522 # uncoverable condition right count:3
523 # uncoverable condition left count:4
524 # uncoverable condition right count:4
525 # uncoverable condition left count:5
526 # uncoverable condition right count:5
527 # uncoverable condition left count:6
528 # uncoverable condition right count:6
529 if (d && d != cat && d->fz && dnode != FZ_NOHANDLE && PUNK_FZ
530 && (PUNK_FZ->probe)(d->fz, dnode, k, kl, &slot) != FZ_ABSENT) /* # uncoverable statement */
531 XSRETURN_YES; /* # uncoverable statement */
532 XSRETURN_NO; /* # uncoverable statement */
533 }
534  
535 # ---- iteration --------------------------------------------------------------
536 #
537 # `keys %$locale` used to die: there was no FIRSTKEY, so tie magic had
538 # nothing to call. A template that wants to list a section - every error
539 # message, every menu label - could not.
540 #
541 # The keys are the NEGOTIATED catalogue's, not the union with the default.
542 # A union would be more useful for a partly translated section and is more
543 # expensive and order-unstable, and it would disagree with the missing-key
544 # rule everywhere else: a section that does not exist in this locale reads
545 # as empty, the same way an absent key reads as itself.
546 #
547 # The order is Frozen's, which sorts keys when it builds the block. So it is
548 # stable, and identical in every process reading the same block - which a
549 # Perl hash is not.
550  
551 SV *
552 FIRSTKEY(self)
553 SV *self
554 CODE:
555 {
556 10 AV *o = (AV *)SvRV(self);
557 10 av_store(o, PIT_ITER, newSViv(0));
558 10 RETVAL = pi_key_at(aTHX_ o, 0);
559 }
560 OUTPUT:
561 RETVAL
562  
563 SV *
564 NEXTKEY(self, last = &PL_sv_undef)
565 SV *self
566 SV *last
567 CODE:
568 {
569 50 AV *o = (AV *)SvRV(self);
570 50 SV **it = av_fetch(o, PIT_ITER, 0);
571 50 ▶ # uncoverable condition right count:1 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition right count:1 class:ignore_covered_err 50 0 50
# uncoverable condition right count:1 class:ignore_covered_err 50 0 50
572 # uncoverable condition right count:2 class:ignore_covered_err
573 50 IV i = (it && *it) ? SvIV(*it) + 1 : 1;
574 50 PERL_UNUSED_VAR(last);
575 av_store(o, PIT_ITER, newSViv(i));
576 RETVAL = pi_key_at(aTHX_ o, (uint32_t)i);
577 }
578 OUTPUT:
579 RETVAL
580  
581 # scalar %$locale, and the boolean context a template puts it in.
582 IV
583 SCALAR(self)
584 SV *self
585 CODE:
586 {
587 1 AV *o = (AV *)SvRV(self);
588 1 SV **arsv = av_fetch(o, PIT_ARENA, 0);
589 1 SV **nds = av_fetch(o, PIT_NODE, 0);
590 1 SV **idxs = av_fetch(o, PIT_CAT, 0);
591 1 ▶ # uncoverable condition right count:1 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition right count:1 class:ignore_covered_err 1 0 1
# uncoverable condition right count:1 class:ignore_covered_err 1 0 1
592 1 ▶ # uncoverable condition right count:2 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition right count:2 class:ignore_covered_err 1 0 1
# uncoverable condition right count:2 class:ignore_covered_err 1 0 1
593 1 ▶ pi_arena *ar = (arsv && *arsv) ? INT2PTR(pi_arena *, SvIV(*arsv)) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
pi_arena *ar = (arsv && *arsv) ? INT2PTR(pi_arena *, SvIV(*arsv)) : NULL; 1 0 1
pi_arena *ar = (arsv && *arsv) ? INT2PTR(pi_arena *, SvIV(*arsv)) : NULL; 1 0 1
594 1 # uncoverable condition right count:1 class:ignore_covered_err
595 1 ▶ # uncoverable condition right count:2 class:ignore_covered_err
Branch: 5 branches
BranchTrueFalseTotal
# uncoverable condition right count:2 class:ignore_covered_err 1 0 1
# uncoverable condition right count:2 class:ignore_covered_err 1 0 1
# uncoverable condition right count:2 class:ignore_covered_err 1 0 1
# uncoverable condition right count:2 class:ignore_covered_err 1 0 1
# uncoverable condition right count:2 class:ignore_covered_err 1 0 1
596 1 int idx = (idxs && *idxs) ? (int)SvIV(*idxs) : -1;
597 # uncoverable condition right count:1 class:ignore_covered_err
598 # uncoverable condition right count:2 class:ignore_covered_err
599 uint32_t node = (nds && *nds) ? (uint32_t)SvUV(*nds) : FZ_NOHANDLE;
600 RETVAL = 0;
601 # uncoverable branch false
602 # uncoverable condition right count:1 class:ignore_covered_err
603 # uncoverable condition right count:2 class:ignore_covered_err
604 # uncoverable condition right count:3 class:ignore_covered_err
605 # uncoverable condition right count:4 class:ignore_covered_err
606 # uncoverable condition right count:5 class:ignore_covered_err
607 if (ar && idx >= 0 && idx < ar->ncat && node != FZ_NOHANDLE && PUNK_FZ)
608 RETVAL = (IV)(PUNK_FZ->count)(ar->cat[idx].fz, node);
609 }
610 OUTPUT:
611 RETVAL
612  
613 # ---- writing ----------------------------------------------------------------
614 #
615 # Restored verbatim from 0.48: the wording says WHY, and why is the whole
616 # value of the message. CLEAR is new, and croaks for the same reason - it
617 # was simply missing, so `%$locale = ()` used to die with "Can't locate
618 # object method" instead of saying anything useful.
619  
620 void
621 STORE(self, key, value)
622 SV *self
623 SV *key
624 SV *value
625 CODE:
626 PERL_UNUSED_VAR(self); PERL_UNUSED_VAR(key); PERL_UNUSED_VAR(value);
627 1 croak("Punk::Plugin::I18n: the locale hash is read-only - a "
628 "catalogue is loaded at boot and shared across the pool, so a "
629 "write here would either be lost or be one worker's private "
630 "copy of a translation");
631  
632 void
633 # uncoverable statement
634 # uncoverable subroutine
635 DELETE(self, key)
636 SV *self
637 SV *key
638 CODE:
639 PERL_UNUSED_VAR(self); PERL_UNUSED_VAR(key);
640 0 croak("Punk::Plugin::I18n: the locale hash is read-only"); /* # uncoverable statement */
641  
642 void
643 CLEAR(self)
644 SV *self
645 CODE:
646 PERL_UNUSED_VAR(self);
647 1 croak("Punk::Plugin::I18n: the locale hash is read-only");
648  
649 MODULE = Punk PACKAGE = Punk::Plugin::I18n
650  
651 # ---- the seams the tests need ---------------------------------------------
652 #
653 # Negotiation is a table, and a table is worth testing directly rather than
654 # through twenty forked servers. These take the header and the catalogue tags
655 # and answer the same question the request path asks.
656  
657 # _interpolate($string, %substitutions) -> the string with {name} filled in
658 SV *
659 _interpolate(class, str, ...)
660 SV *class
661 SV *str
662 CODE:
663 {
664 STRLEN vl;
665 const char *v;
666 PERL_UNUSED_VAR(class);
667 9 v = SvPV_const(str, vl);
668 /* The seam's own argument carries the flag the block would have
669 * recorded. It used to pass none, so this answered a different
670 * question from the request path - which is part of why neither
671 * encoding bug was ever caught through it. */
672 9 RETVAL = pi_interpolate(aTHX_ v, vl, SvUTF8(str) ? 1 : 0,
673 9 &ST(2), items - 2, 0);
674 }
675 OUTPUT:
676 RETVAL
677  
678 # _negotiate($header, \@catalogue_tags) -> the chosen tag, or undef
679 SV *
680 _negotiate(class, header, tags)
681 SV *class
682 SV *header
683 SV *tags
684 CODE:
685 {
686 pi_arena ar;
687 AV *av;
688 int i, n, pick;
689 STRLEN hl;
690 const char *hp;
691  
692 PERL_UNUSED_VAR(class);
693 40 ▶ # uncoverable branch true
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable branch true 40 0 40
# uncoverable branch true 0 40 40
694 0 # uncoverable condition left count:1 class:ignore_covered_err
695 40 # uncoverable condition left count:2 class:ignore_covered_err
696 40 if (!(SvROK(tags) && SvTYPE(SvRV(tags)) == SVt_PVAV))
697 croak("Punk::Plugin::I18n::_negotiate: tags must be an arrayref"); /* # uncoverable statement */
698 40 av = (AV *)SvRV(tags);
699 40 n = (int)(av_len(av) + 1);
700 40 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
40 0 40
701 107 ▶ Zero(&ar, 1, pi_arena);
Branch: 1 branch
BranchTrueFalseTotal
Zero(&ar, 1, pi_arena); 67 40 107
702 67 ar.def = -1;
703 # uncoverable branch false
704 134 ▶ # uncoverable condition right count:1 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition right count:1 class:ignore_covered_err 67 0 67
# uncoverable condition right count:1 class:ignore_covered_err 67 0 67
705 if (n) Newxz(ar.cat, n, pi_cat);
706 67 ▶ for (i = 0; i < n; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 67 0 67
for (i = 0; i < n; i++) { 0 67 67
707 216 ▶ SV **e = av_fetch(av, i, 0);
Branch: 1 branch
BranchTrueFalseTotal
SV **e = av_fetch(av, i, 0); 149 67 216
708 67 STRLEN tl;
709 67 # uncoverable condition right count:1 class:ignore_covered_err
710 # uncoverable condition right count:2 class:ignore_covered_err
711 const char *tp = (e && *e) ? SvPV_const(*e, tl) : NULL;
712 STRLEN j;
713 67 # uncoverable branch true
714 67 # uncoverable condition left count:1 class:ignore_covered_err
715 67 # uncoverable condition left count:2 class:ignore_covered_err
716 if (!tp || tl > PI_TAG_MAX) continue;
717 for (j = 0; j < tl; j++) ar.cat[ar.ncat].tag[j] = pi_fold(tp[j]);
718 79 ▶ ar.cat[ar.ncat].tag[tl] = '\0';
Branch: 1 branch
BranchTrueFalseTotal
ar.cat[ar.ncat].tag[tl] = '\0'; 39 1 40
719 40 ▶ ar.cat[ar.ncat].taglen = tl;
Branch: 1 branch
BranchTrueFalseTotal
ar.cat[ar.ncat].taglen = tl; 1 39 40
720 40 /* No block here: this seam tests the Accept-Language rules on
721 40 * tags alone. Said explicitly rather than left to Newxz, so the
722 20 * next person to add a pi_get gets a refusal and not a crash. */
723 40 ▶ ar.cat[ar.ncat].fz = NULL;
Branch: 1 branch
BranchTrueFalseTotal
ar.cat[ar.ncat].fz = NULL; 20 20 40
724 40 ar.cat[ar.ncat].node = FZ_NOHANDLE;
725 ar.ncat++;
726 }
727  
728 hp = SvOK(header) ? SvPV_const(header, hl) : NULL;
729 if (!hp) hl = 0;
730 pick = pl_negotiate(hp, hl, &ar);
731 RETVAL = (pick >= 0)
732 ? newSVpvn(ar.cat[pick].tag, ar.cat[pick].taglen)
733 : newSV(0);
734 Safefree(ar.cat);
735 }
736 OUTPUT:
737 RETVAL
738  
739 MODULE = Punk PACKAGE = Punk
740  
741 # Whether Frozen's ABI table resolved. Nothing on the request path asks -
742 # punk_fz croaks at boot if it is missing, because Frozen is a hard
743 # dependency with no arena to fall back to. This is for the guard test and
744 # for `punk doctor`. PUNK_FAKE_FZ_BAD simulates a version mismatch.
745 int
746 _fz_available()
747 CODE:
748 8 ▶ RETVAL = punk_fz_try(aTHX) ? 1 : 0; /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = punk_fz_try(aTHX) ? 1 : 0; /* # uncoverable condition left count:1 class:ignore_covered_err */ 0 8 8
749 OUTPUT:
750 RETVAL
751  
752 # The ABI version this was compiled against, for the guard test.
753 int
754 _fz_abi_version()
755 CODE:
756 6 ▶ RETVAL = FZ_ABI_VERSION;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = FZ_ABI_VERSION; 0 6 6
757 OUTPUT:
758 RETVAL
« xs/validate.xs ↑ Summary xs/upload.xs »