xs/i18n.xs

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