Encode.xs

filter: Statementstmt 88.1% 455 / 516 Branchbran 68.4% 316 / 462 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 78.8% 771 / 978 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Encode/Unicode.pm ↑ Summary encengine.c »
1 /*
2 $Id: Encode.xs,v 2.51 2021/10/08 15:29:23 dankogai Exp $
3 */
4  
5 #define PERL_NO_GET_CONTEXT
6 #define IN_ENCODE_XS
7 #include "EXTERN.h"
8 #include "perl.h"
9 #include "XSUB.h"
10 #include "encode.h"
11 #include "def_t.h"
12  
13 # define PERLIO_MODNAME "PerlIO::encoding"
14 # define PERLIO_FILENAME "PerlIO/encoding.pm"
15  
16 /* set 1 or more to profile. t/encoding.t dumps core because of
17 Perl_warner and PerlIO don't work well */
18 #define ENCODE_XS_PROFILE 0
19  
20 /* set 0 to disable floating point to calculate buffer size for
21 encode_method(). 1 is recommended. 2 restores NI-S original */
22 #define ENCODE_XS_USEFP 1
23  
24 #ifndef SvIV_nomg
25 #define SvIV_nomg SvIV
26 #endif
27  
28 #ifndef SvTRUE_nomg
29 #define SvTRUE_nomg SvTRUE
30 #endif
31  
32 #ifndef SVfARG
33 #define SVfARG(p) ((void*)(p))
34 #endif
35  
36 static void
37 260 Encode_XSEncoding(pTHX_ encode_t * enc)
38 {
39 260 dSP;
40 260 HV *stash = gv_stashpv("Encode::XS", TRUE);
41 260 SV *iv = newSViv(PTR2IV(enc));
42 260 SV *sv = sv_bless(newRV_noinc(iv),stash);
43 260 int i = 0;
44 /* with the SvLEN() == 0 hack, PVX won't be freed. We cast away name's
45 constness, in the hope that perl won't mess with it. */
46 assert(SvTYPE(iv) >= SVt_PV); assert(SvLEN(iv) == 0);
47 260 SvFLAGS(iv) |= SVp_POK;
48 260 SvPVX(iv) = (char*) enc->name[0];
49 260 ▶ PUSHMARK(sp);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(sp); 0 260 260
50 260 ▶ XPUSHs(sv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv); 0 260 260
51 520 ▶ while (enc->name[i]) {
Branch: 1 branch
BranchTrueFalseTotal
while (enc->name[i]) { 260 260 520
52 260 const char *name = enc->name[i++];
53 260 ▶ XPUSHs(sv_2mortal(newSVpvn(name, strlen(name))));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvn(name, strlen(name)))); 0 260 260
54 }
55 260 PUTBACK;
56 260 call_pv("Encode::define_encoding", G_DISCARD);
57 260 SvREFCNT_dec(sv);
58 260 }
59  
60 static void
61 23 utf8_safe_downgrade(pTHX_ SV ** src, U8 ** s, STRLEN * slen, bool modify)
62 {
63 23 ▶ if (!modify) {
Branch: 1 branch
BranchTrueFalseTotal
if (!modify) { 15 8 23
64 15 SV *tmp = sv_2mortal(newSVpvn((char *)*s, *slen));
65 15 SvUTF8_on(tmp);
66 15 ▶ if (SvTAINTED(*src))
Branch: 2 branches
BranchTrueFalseTotal
if (SvTAINTED(*src)) 4 11 15
if (SvTAINTED(*src)) 0 4 4
67 0 ▶ SvTAINTED_on(tmp);
Branch: 1 branch
BranchTrueFalseTotal
SvTAINTED_on(tmp); 0 0 0
68 15 *src = tmp;
69 15 *s = (U8 *)SvPVX(*src);
70 }
71 23 ▶ if (*slen) {
Branch: 1 branch
BranchTrueFalseTotal
if (*slen) { 23 0 23
72 23 ▶ if (!utf8_to_bytes(*s, slen))
Branch: 1 branch
BranchTrueFalseTotal
if (!utf8_to_bytes(*s, slen)) 0 23 23
73 0 croak("Wide character");
74 23 SvCUR_set(*src, *slen);
75 }
76 23 SvUTF8_off(*src);
77 23 }
78  
79 static void
80 20 utf8_safe_upgrade(pTHX_ SV ** src, U8 ** s, STRLEN * slen, bool modify)
81 {
82 20 ▶ if (!modify) {
Branch: 1 branch
BranchTrueFalseTotal
if (!modify) { 13 7 20
83 13 SV *tmp = sv_2mortal(newSVpvn((char *)*s, *slen));
84 13 ▶ if (SvTAINTED(*src))
Branch: 2 branches
BranchTrueFalseTotal
if (SvTAINTED(*src)) 3 10 13
if (SvTAINTED(*src)) 0 3 3
85 0 ▶ SvTAINTED_on(tmp);
Branch: 1 branch
BranchTrueFalseTotal
SvTAINTED_on(tmp); 0 0 0
86 13 *src = tmp;
87 }
88 20 sv_utf8_upgrade_nomg(*src);
89 20 *s = (U8 *)SvPV_nomg(*src, *slen);
90 20 }
91  
92 #define ERR_ENCODE_NOMAP "\"\\x{%04" UVxf "}\" does not map to %s"
93 #define ERR_DECODE_NOMAP "%s \"\\x%02" UVXf "\" does not map to Unicode"
94 #define ERR_DECODE_STR_NOMAP "%s \"%s\" does not map to Unicode"
95  
96 static SV *
97 268 do_fallback_cb(pTHX_ UV ch, SV *fallback_cb)
98 {
99 268 dSP;
100 int argc;
101 SV *retval;
102 268 ENTER;
103 268 SAVETMPS;
104 268 ▶ PUSHMARK(sp);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(sp); 0 268 268
105 268 ▶ XPUSHs(sv_2mortal(newSVuv(ch)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVuv(ch))); 0 268 268
106 268 PUTBACK;
107 268 argc = call_sv(fallback_cb, G_SCALAR);
108 268 SPAGAIN;
109 268 ▶ if (argc != 1){
Branch: 1 branch
BranchTrueFalseTotal
if (argc != 1){ 0 268 268
110 0 croak("fallback sub must return scalar!");
111 }
112 268 retval = POPs;
113 268 SvREFCNT_inc(retval);
114 268 PUTBACK;
115 268 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 268 0 268
116 268 LEAVE;
117 268 return retval;
118 }
119  
120 static SV *
121 10 do_bytes_fallback_cb(pTHX_ U8 *s, STRLEN slen, SV *fallback_cb)
122 {
123 10 dSP;
124 int argc;
125 STRLEN i;
126 SV *retval;
127 10 ENTER;
128 10 SAVETMPS;
129 10 ▶ PUSHMARK(sp);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(sp); 0 10 10
130 24 ▶ for (i=0; i<slen; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (i=0; i<slen; ++i) 14 10 24
131 14 ▶ XPUSHs(sv_2mortal(newSVuv(s[i])));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVuv(s[i]))); 0 14 14
132 10 PUTBACK;
133 10 argc = call_sv(fallback_cb, G_SCALAR);
134 10 SPAGAIN;
135 10 ▶ if (argc != 1){
Branch: 1 branch
BranchTrueFalseTotal
if (argc != 1){ 0 10 10
136 0 croak("fallback sub must return scalar!");
137 }
138 10 retval = POPs;
139 10 SvREFCNT_inc(retval);
140 10 PUTBACK;
141 10 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 10 0 10
142 10 LEAVE;
143 10 return retval;
144 }
145  
146 static SV *
147 58862 encode_method(pTHX_ const encode_t * enc, const encpage_t * dir, SV * src, U8 * s, STRLEN slen,
148 IV check, STRLEN * offset, SV * term, int * retcode,
149 SV *fallback_cb)
150 {
151 58862 U8 *sorig = s;
152 58862 STRLEN tlen = slen;
153 58862 STRLEN ddone = 0;
154 58862 STRLEN sdone = 0;
155 /* We allocate slen+1.
156 PerlIO dumps core if this value is smaller than this. */
157 58862 SV *dst = sv_2mortal(newSV(slen+1));
158 58862 U8 *d = (U8 *)SvPVX(dst);
159 58862 STRLEN dlen = SvLEN(dst)-1;
160 58862 int code = 0;
161 58862 STRLEN trmlen = 0;
162 58862 ▶ U8 *trm = term ? (U8*) SvPV(term, trmlen) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
U8 *trm = term ? (U8*) SvPV(term, trmlen) : NULL; 0 58862 58862
163  
164 58862 ▶ if (SvTAINTED(src)) SvTAINTED_on(dst); /* propagate taintedness */
Branch: 3 branches
BranchTrueFalseTotal
if (SvTAINTED(src)) SvTAINTED_on(dst); /* propagate taintedness */ 245 58617 58862
if (SvTAINTED(src)) SvTAINTED_on(dst); /* propagate taintedness */ 217 28 245
if (SvTAINTED(src)) SvTAINTED_on(dst); /* propagate taintedness */ 217 0 217
165  
166 58862 ▶ if (offset) {
Branch: 1 branch
BranchTrueFalseTotal
if (offset) { 0 58862 58862
167 0 s += *offset;
168 0 ▶ if (slen > *offset){ /* safeguard against slen overflow */
Branch: 1 branch
BranchTrueFalseTotal
if (slen > *offset){ /* safeguard against slen overflow */ 0 0 0
169 0 slen -= *offset;
170 }else{
171 0 slen = 0;
172 }
173 0 tlen = slen;
174 }
175  
176 58862 ▶ if (slen == 0){
Branch: 1 branch
BranchTrueFalseTotal
if (slen == 0){ 0 58862 58862
177 0 SvCUR_set(dst, 0);
178 0 SvPOK_only(dst);
179 0 goto ENCODE_END;
180 }
181  
182 62137 ▶ while( (code = do_encode(dir, s, &slen, d, dlen, &dlen, !check,
Branch: 1 branch
BranchTrueFalseTotal
while( (code = do_encode(dir, s, &slen, d, dlen, &dlen, !check, 3428 58709 62137
183 trm, trmlen)) )
184 {
185 3428 SvCUR_set(dst, dlen+ddone);
186 3428 SvPOK_only(dst);
187  
188 3428 ▶ if (code == ENCODE_FALLBACK || code == ENCODE_PARTIAL ||
Branch: 3 branches
BranchTrueFalseTotal
if (code == ENCODE_FALLBACK || code == ENCODE_PARTIAL || 0 3428 3428
if (code == ENCODE_FALLBACK || code == ENCODE_PARTIAL || 111 3317 3428
if (code == ENCODE_FALLBACK || code == ENCODE_PARTIAL || 0 3317 3317
189 code == ENCODE_FOUND_TERM) {
190 break;
191 }
192 3317 switch (code) {
193 1136 case ENCODE_NOSPACE:
194 1136 {
195 1136 STRLEN more = 0; /* make sure you initialize! */
196 STRLEN sleft;
197 1136 sdone += slen;
198 1136 ddone += dlen;
199 1136 sleft = tlen - sdone;
200 #if ENCODE_XS_PROFILE >= 2
201 Perl_warn(aTHX_
202 "more=%d, sdone=%d, sleft=%d, SvLEN(dst)=%d\n",
203 more, sdone, sleft, SvLEN(dst));
204 #endif
205 1136 ▶ if (sdone != 0) { /* has src ever been processed ? */
Branch: 1 branch
BranchTrueFalseTotal
if (sdone != 0) { /* has src ever been processed ? */ 1136 0 1136
206 #if ENCODE_XS_USEFP == 2
207 more = (1.0*tlen*SvLEN(dst)+sdone-1)/sdone
208 - SvLEN(dst);
209 #elif ENCODE_XS_USEFP
210 1136 more = (STRLEN)((1.0*SvLEN(dst)+1)/sdone * sleft);
211 #else
212 /* safe until SvLEN(dst) == MAX_INT/16 */
213 more = (16*SvLEN(dst)+1)/sdone/16 * sleft;
214 #endif
215 }
216 1136 more += UTF8_MAXLEN; /* insurance policy */
217 1136 ▶ d = (U8 *) SvGROW(dst, SvLEN(dst) + more);
Branch: 2 branches
BranchTrueFalseTotal
d = (U8 *) SvGROW(dst, SvLEN(dst) + more); 1136 0 1136
d = (U8 *) SvGROW(dst, SvLEN(dst) + more); 1136 0 1136
218 /* dst need to grow need MORE bytes! */
219 1136 ▶ if (ddone >= SvLEN(dst)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ddone >= SvLEN(dst)) { 0 1136 1136
220 0 Perl_croak(aTHX_ "Destination couldn't be grown.");
221 }
222 1136 dlen = SvLEN(dst)-ddone-1;
223 1136 d += ddone;
224 1136 s += slen;
225 1136 slen = tlen-sdone;
226 1136 continue;
227 }
228  
229 2181 case ENCODE_NOREP:
230 /* encoding */
231 2181 ▶ if (dir == enc->f_utf8) {
Branch: 1 branch
BranchTrueFalseTotal
if (dir == enc->f_utf8) { 1578 603 2181
232 STRLEN clen;
233 UV ch =
234 1578 utf8n_to_uvchr(s+slen, (tlen-sdone-slen),
235 &clen, UTF8_ALLOW_ANY|UTF8_CHECK_ONLY);
236 /* if non-representable multibyte prefix at end of current buffer - break*/
237 1578 ▶ if (clen > tlen - sdone - slen) break;
Branch: 1 branch
BranchTrueFalseTotal
if (clen > tlen - sdone - slen) break; 0 1578 1578
238 1578 ▶ if (check & ENCODE_DIE_ON_ERR) {
Branch: 1 branch
BranchTrueFalseTotal
if (check & ENCODE_DIE_ON_ERR) { 1 1577 1578
239 1 Perl_croak(aTHX_ ERR_ENCODE_NOMAP,
240 1 (UV)ch, enc->name[0]);
241 return &PL_sv_undef; /* never reaches but be safe */
242 }
243 1577 ▶ if (encode_ckWARN(check, WARN_UTF8)) {
Branch: 3 branches
BranchTrueFalseTotal
if (encode_ckWARN(check, WARN_UTF8)) { 129 1448 1577
if (encode_ckWARN(check, WARN_UTF8)) { 0 129 129
if (encode_ckWARN(check, WARN_UTF8)) { 0 0 0
244 129 Perl_warner(aTHX_ packWARN(WARN_UTF8),
245 129 ERR_ENCODE_NOMAP, (UV)ch, enc->name[0]);
246 }
247 1577 ▶ if (check & ENCODE_RETURN_ON_ERR){
Branch: 1 branch
BranchTrueFalseTotal
if (check & ENCODE_RETURN_ON_ERR){ 2 1575 1577
248 2 goto ENCODE_SET_SRC;
249 }
250 1575 ▶ if (check & (ENCODE_PERLQQ|ENCODE_HTMLCREF|ENCODE_XMLCREF)){
Branch: 1 branch
BranchTrueFalseTotal
if (check & (ENCODE_PERLQQ|ENCODE_HTMLCREF|ENCODE_XMLCREF)){ 1035 540 1575
251 STRLEN sublen;
252 char *substr;
253 1035 SV* subchar =
254 (fallback_cb != &PL_sv_undef)
255 136 ? do_fallback_cb(aTHX_ ch, fallback_cb)
256 1035 ▶ : newSVpvf(check & ENCODE_PERLQQ ? "\\x{%04" UVxf "}" :
Branch: 3 branches
BranchTrueFalseTotal
: newSVpvf(check & ENCODE_PERLQQ ? "\\x{%04" UVxf "}" : 136 899 1035
: newSVpvf(check & ENCODE_PERLQQ ? "\\x{%04" UVxf "}" : 261 638 899
: newSVpvf(check & ENCODE_PERLQQ ? "\\x{%04" UVxf "}" : 131 130 261
257 check & ENCODE_HTMLCREF ? "&#%" UVuf ";" :
258 "&#x%" UVxf ";", (UV)ch);
259 1035 substr = SvPV(subchar, sublen);
260 1035 ▶ if (SvUTF8(subchar) && sublen && !utf8_to_bytes((U8 *)substr, &sublen)) { /* make sure no decoded string gets in */
Branch: 3 branches
BranchTrueFalseTotal
if (SvUTF8(subchar) && sublen && !utf8_to_bytes((U8 *)substr, &sublen)) { /* make sure no decoded string gets in */ 4 1031 1035
if (SvUTF8(subchar) && sublen && !utf8_to_bytes((U8 *)substr, &sublen)) { /* make sure no decoded string gets in */ 4 0 4
if (SvUTF8(subchar) && sublen && !utf8_to_bytes((U8 *)substr, &sublen)) { /* make sure no decoded string gets in */ 0 4 4
261 0 SvREFCNT_dec(subchar);
262 0 croak("Wide character");
263 }
264 1035 sdone += slen + clen;
265 1035 ddone += dlen + sublen;
266 1035 sv_catpvn(dst, substr, sublen);
267 1035 SvREFCNT_dec(subchar);
268 } else {
269 /* fallback char */
270 540 sdone += slen + clen;
271 540 ddone += dlen + enc->replen;
272 540 sv_catpvn(dst, (char*)enc->rep, enc->replen);
273 }
274 }
275 /* decoding */
276 else {
277 603 ▶ if (check & ENCODE_DIE_ON_ERR){
Branch: 1 branch
BranchTrueFalseTotal
if (check & ENCODE_DIE_ON_ERR){ 0 603 603
278 0 Perl_croak(aTHX_ ERR_DECODE_NOMAP,
279 0 enc->name[0], (UV)s[slen]);
280 return &PL_sv_undef; /* never reaches but be safe */
281 }
282 603 ▶ if (encode_ckWARN(check, WARN_UTF8)) {
Branch: 3 branches
BranchTrueFalseTotal
if (encode_ckWARN(check, WARN_UTF8)) { 0 603 603
if (encode_ckWARN(check, WARN_UTF8)) { 0 0 0
if (encode_ckWARN(check, WARN_UTF8)) { 0 0 0
283 0 Perl_warner(
284 aTHX_ packWARN(WARN_UTF8),
285 ERR_DECODE_NOMAP,
286 0 enc->name[0], (UV)s[slen]);
287 }
288 603 ▶ if (check & ENCODE_RETURN_ON_ERR){
Branch: 1 branch
BranchTrueFalseTotal
if (check & ENCODE_RETURN_ON_ERR){ 39 564 603
289 39 goto ENCODE_SET_SRC;
290 }
291 564 ▶ if (check &
Branch: 1 branch
BranchTrueFalseTotal
if (check & 514 50 564
292 (ENCODE_PERLQQ|ENCODE_HTMLCREF|ENCODE_XMLCREF)){
293 STRLEN sublen;
294 char *substr;
295 514 SV* subchar =
296 (fallback_cb != &PL_sv_undef)
297 130 ? do_fallback_cb(aTHX_ (UV)s[slen], fallback_cb)
298 514 ▶ : newSVpvf("\\x%02" UVXf, (UV)s[slen]);
Branch: 1 branch
BranchTrueFalseTotal
: newSVpvf("\\x%02" UVXf, (UV)s[slen]); 130 384 514
299 514 substr = SvPVutf8(subchar, sublen);
300 514 sdone += slen + 1;
301 514 ddone += dlen + sublen;
302 514 sv_catpvn(dst, substr, sublen);
303 514 SvREFCNT_dec(subchar);
304 } else {
305 50 sdone += slen + 1;
306 50 ddone += dlen + strlen(FBCHAR_UTF8);
307 50 sv_catpvn(dst, FBCHAR_UTF8, strlen(FBCHAR_UTF8));
308 }
309 }
310 /* settle variables when fallback */
311 2139 d = (U8 *)SvEND(dst);
312 2139 dlen = SvLEN(dst) - ddone - 1;
313 2139 s = sorig + sdone;
314 2139 slen = tlen - sdone;
315 2139 break;
316  
317 0 default:
318 0 Perl_croak(aTHX_ "Unexpected code %d converting %s %s",
319 0 code, (dir == enc->f_utf8) ? "to" : "from",
320 0 ▶ enc->name[0]);
Branch: 1 branch
BranchTrueFalseTotal
enc->name[0]); 0 0 0
321 return &PL_sv_undef;
322 }
323 } /* End of looping through the string */
324 58820 ENCODE_SET_SRC:
325 58861 ▶ if (check && !(check & ENCODE_LEAVE_SRC)){
Branch: 2 branches
BranchTrueFalseTotal
if (check && !(check & ENCODE_LEAVE_SRC)){ 18259 40602 58861
if (check && !(check & ENCODE_LEAVE_SRC)){ 18178 81 18259
326 18178 sdone = tlen - (slen+sdone);
327 18178 sv_setpvn(src, (char*)s+slen, sdone);
328 18178 ▶ SvSETMAGIC(src);
Branch: 1 branch
BranchTrueFalseTotal
SvSETMAGIC(src); 16 18162 18178
329 }
330 /* warn("check = 0x%X, code = 0x%d\n", check, code); */
331  
332 58861 SvCUR_set(dst, dlen+ddone);
333 58861 SvPOK_only(dst);
334  
335 #if ENCODE_XS_PROFILE
336 if (SvCUR(dst) > tlen){
337 Perl_warn(aTHX_
338 "SvLEN(dst)=%d, SvCUR(dst)=%d. %d bytes unused(%f %%)\n",
339 SvLEN(dst), SvCUR(dst), SvLEN(dst) - SvCUR(dst),
340 (SvLEN(dst) - SvCUR(dst))*1.0/SvLEN(dst)*100.0);
341 }
342 #endif
343  
344 58861 ▶ if (offset)
Branch: 1 branch
BranchTrueFalseTotal
if (offset) 58861 0 58861
345 0 *offset += sdone + slen;
346  
347 58861 ENCODE_END:
348 58861 *SvEND(dst) = '\0';
349 58861 ▶ if (retcode) *retcode = code;
Branch: 1 branch
BranchTrueFalseTotal
if (retcode) *retcode = code; 0 58861 58861
350 58861 return dst;
351 }
352  
353 static bool
354 31445 strict_utf8(pTHX_ SV* sv)
355 {
356 HV* hv;
357 SV** svp;
358 31445 sv = SvRV(sv);
359 31445 ▶ if (!sv || SvTYPE(sv) != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (!sv || SvTYPE(sv) != SVt_PVHV) 31445 0 31445
if (!sv || SvTYPE(sv) != SVt_PVHV) 0 31445 31445
360 0 return 0;
361 31445 hv = (HV*)sv;
362 31445 svp = hv_fetch(hv, "strict_utf8", 11, 0);
363 31445 ▶ if (!svp)
Branch: 1 branch
BranchTrueFalseTotal
if (!svp) 76 31369 31445
364 76 return 0;
365 31369 return SvTRUE(*svp);
366 }
367  
368 static U8*
369 31441 process_utf8(pTHX_ SV* dst, U8* s, U8* e, SV *check_sv,
370 bool encode, bool strict, bool stop_at_partial)
371 {
372 /* Copies the purportedly UTF-8 encoded string starting at 's' and ending
373 * at 'e' - 1 to 'dst', checking as it goes along that the string actually
374 * is valid UTF-8. There are two levels of strictness checking. If
375 * 'strict' is FALSE, the string is checked for being well-formed UTF-8, as
376 * extended by Perl. Additionally, if 'strict' is TRUE, above-Unicode code
377 * points, surrogates, and non-character code points are checked for. When
378 * invalid input is encountered, some action is taken, exactly what depends
379 * on the flags in 'check_sv'. 'encode' gives if this is from an encode
380 * operation (if TRUE), or a decode one. This function returns the
381 * position in 's' of the start of the next character beyond where it got
382 * to. If there were no problems, that will be 'e'. If 'stop_at_partial'
383 * is TRUE, if the final character before 'e' is incomplete, but valid as
384 * far as is available, no action will be taken on that partial character,
385 * and the return value will point to its first byte */
386  
387 UV uv;
388 STRLEN ulen;
389 SV *fallback_cb;
390 IV check;
391 U8 *d;
392 STRLEN dlen;
393 char esc[UTF8_MAXLEN * 6 + 1];
394 STRLEN i;
395 31441 const U32 flags = (strict)
396 ? UTF8_DISALLOW_ILLEGAL_INTERCHANGE
397 31441 ▶ : 0;
Branch: 1 branch
BranchTrueFalseTotal
: 0; 31369 72 31441
398  
399 31441 ▶ if (!SvOK(check_sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(check_sv)) { 0 31441 31441
400 0 fallback_cb = &PL_sv_undef;
401 0 check = 0;
402 }
403 31441 ▶ else if (SvROK(check_sv)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (SvROK(check_sv)) { 14 31427 31441
404 /* croak("UTF-8 decoder doesn't support callback CHECK"); */
405 14 fallback_cb = check_sv;
406 14 check = ENCODE_PERLQQ|ENCODE_LEAVE_SRC; /* same as perlqq */
407 }
408 else {
409 31427 fallback_cb = &PL_sv_undef;
410 31427 check = SvIV_nomg(check_sv);
411 }
412  
413 31441 SvPOK_only(dst);
414 31441 SvCUR_set(dst,0);
415  
416 31441 ▶ dlen = (s && e && s < e) ? e-s+1 : 1;
Branch: 3 branches
BranchTrueFalseTotal
dlen = (s && e && s < e) ? e-s+1 : 1; 31441 0 31441
dlen = (s && e && s < e) ? e-s+1 : 1; 31441 0 31441
dlen = (s && e && s < e) ? e-s+1 : 1; 31439 2 31441
417 31441 ▶ d = (U8 *) SvGROW(dst, dlen);
Branch: 2 branches
BranchTrueFalseTotal
d = (U8 *) SvGROW(dst, dlen); 31441 0 31441
d = (U8 *) SvGROW(dst, dlen); 0 31441 31441
418  
419 31441 ▶ stop_at_partial = stop_at_partial || (check & ENCODE_STOP_AT_PARTIAL);
Branch: 2 branches
BranchTrueFalseTotal
stop_at_partial = stop_at_partial || (check & ENCODE_STOP_AT_PARTIAL); 31177 264 31441
stop_at_partial = stop_at_partial || (check & ENCODE_STOP_AT_PARTIAL); 0 31177 31177
420  
421 31745 ▶ while (s < e) {
Branch: 1 branch
BranchTrueFalseTotal
while (s < e) { 31713 32 31745
422  
423 /* If there were no errors, this will be 'e'; otherwise it will point
424 * to the first byte of the erroneous input */
425 const U8* e_or_where_failed;
426 31713 bool valid = is_utf8_string_loc_flags(s, e - s, &e_or_where_failed, flags);
427 31713 STRLEN len = e_or_where_failed - s;
428  
429 /* Copy as far as was successful */
430 31713 Move(s, d, len, U8);
431 31713 d += len;
432 31713 s = (U8 *) e_or_where_failed;
433  
434 /* Are done if it was valid, or we are accepting partial characters and
435 * the only error is that the final bytes form a partial character */
436 31713 ▶ if ( LIKELY(valid)
Branch: 1 branch
BranchTrueFalseTotal
if ( LIKELY(valid) 389 31324 31713
437 389 ▶ || ( stop_at_partial
Branch: 1 branch
BranchTrueFalseTotal
|| ( stop_at_partial 6 383 389
438 6 ▶ && is_utf8_valid_partial_char_flags(s, e, flags)))
Branch: 1 branch
BranchTrueFalseTotal
&& is_utf8_valid_partial_char_flags(s, e, flags))) 6 0 6
439 {
440 break;
441 }
442  
443 /* Here, was not valid. If is 'strict', and is legal extended UTF-8,
444 * we know it is a code point whose value we can calculate, just not
445 * one accepted under strict. Otherwise, it is malformed in some way.
446 * In either case, the system function can calculate either the code
447 * point, or the best substitution for it */
448 389 uv = utf8n_to_uvchr(s, e - s, &ulen, UTF8_ALLOW_ANY);
449  
450 /*
451 * Here, we are looping through the input and found an error.
452 * 'uv' is the code point in error if calculable, or the REPLACEMENT
453 * CHARACTER if not.
454 * 'ulen' is how many bytes of input this iteration of the loop
455 * consumes */
456  
457 389 ▶ if (!encode && (check & (ENCODE_DIE_ON_ERR|ENCODE_WARN_ON_ERR|ENCODE_PERLQQ)))
Branch: 2 branches
BranchTrueFalseTotal
if (!encode && (check & (ENCODE_DIE_ON_ERR|ENCODE_WARN_ON_ERR|ENCODE_PERLQQ))) 370 19 389
if (!encode && (check & (ENCODE_DIE_ON_ERR|ENCODE_WARN_ON_ERR|ENCODE_PERLQQ))) 191 179 370
458 458 ▶ for (i=0; i<ulen; ++i) sprintf(esc+4*i, "\\x%02X", s[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i=0; i<ulen; ++i) sprintf(esc+4*i, "\\x%02X", s[i]); 267 191 458
459 389 ▶ if (check & ENCODE_DIE_ON_ERR){
Branch: 1 branch
BranchTrueFalseTotal
if (check & ENCODE_DIE_ON_ERR){ 65 324 389
460 65 ▶ if (encode)
Branch: 1 branch
BranchTrueFalseTotal
if (encode) 14 51 65
461 14 ▶ Perl_croak(aTHX_ ERR_ENCODE_NOMAP, uv, (strict ? "UTF-8" : "utf8"));
Branch: 1 branch
BranchTrueFalseTotal
Perl_croak(aTHX_ ERR_ENCODE_NOMAP, uv, (strict ? "UTF-8" : "utf8")); 14 0 14
462 else
463 51 ▶ Perl_croak(aTHX_ ERR_DECODE_STR_NOMAP, (strict ? "UTF-8" : "utf8"), esc);
Branch: 1 branch
BranchTrueFalseTotal
Perl_croak(aTHX_ ERR_DECODE_STR_NOMAP, (strict ? "UTF-8" : "utf8"), esc); 47 4 51
464 }
465 324 ▶ if (encode_ckWARN(check, WARN_UTF8)) {
Branch: 3 branches
BranchTrueFalseTotal
if (encode_ckWARN(check, WARN_UTF8)) { 129 195 324
if (encode_ckWARN(check, WARN_UTF8)) { 0 129 129
if (encode_ckWARN(check, WARN_UTF8)) { 0 0 0
466 129 ▶ if (encode)
Branch: 1 branch
BranchTrueFalseTotal
if (encode) 0 129 129
467 0 ▶ Perl_warner(aTHX_ packWARN(WARN_UTF8),
Branch: 1 branch
BranchTrueFalseTotal
Perl_warner(aTHX_ packWARN(WARN_UTF8), 0 0 0
468 ERR_ENCODE_NOMAP, uv, (strict ? "UTF-8" : "utf8"));
469 else
470 129 ▶ Perl_warner(aTHX_ packWARN(WARN_UTF8),
Branch: 1 branch
BranchTrueFalseTotal
Perl_warner(aTHX_ packWARN(WARN_UTF8), 0 129 129
471 ERR_DECODE_STR_NOMAP, (strict ? "UTF-8" : "utf8"), esc);
472 }
473 324 ▶ if (check & ENCODE_RETURN_ON_ERR) {
Branch: 1 branch
BranchTrueFalseTotal
if (check & ENCODE_RETURN_ON_ERR) { 20 304 324
474 20 break;
475 }
476 304 ▶ if (check & (ENCODE_PERLQQ|ENCODE_HTMLCREF|ENCODE_XMLCREF)){
Branch: 1 branch
BranchTrueFalseTotal
if (check & (ENCODE_PERLQQ|ENCODE_HTMLCREF|ENCODE_XMLCREF)){ 14 290 304
477 STRLEN sublen;
478 char *substr;
479 SV* subchar;
480 14 ▶ if (encode) {
Branch: 1 branch
BranchTrueFalseTotal
if (encode) { 3 11 14
481 3 subchar =
482 (fallback_cb != &PL_sv_undef)
483 2 ? do_fallback_cb(aTHX_ uv, fallback_cb)
484 3 ▶ : newSVpvf(check & ENCODE_PERLQQ
Branch: 4 branches
BranchTrueFalseTotal
: newSVpvf(check & ENCODE_PERLQQ 2 1 3
: newSVpvf(check & ENCODE_PERLQQ 1 0 1
: newSVpvf(check & ENCODE_PERLQQ 0 1 1
: newSVpvf(check & ENCODE_PERLQQ 0 0 0
485 ? (ulen == 1 ? "\\x%02" UVXf : "\\x{%04" UVXf "}")
486 : check & ENCODE_HTMLCREF ? "&#%" UVuf ";"
487 : "&#x%" UVxf ";", uv);
488 3 substr = SvPV(subchar, sublen);
489 3 ▶ if (SvUTF8(subchar) && sublen && !utf8_to_bytes((U8 *)substr, &sublen)) { /* make sure no decoded string gets in */
Branch: 3 branches
BranchTrueFalseTotal
if (SvUTF8(subchar) && sublen && !utf8_to_bytes((U8 *)substr, &sublen)) { /* make sure no decoded string gets in */ 0 3 3
if (SvUTF8(subchar) && sublen && !utf8_to_bytes((U8 *)substr, &sublen)) { /* make sure no decoded string gets in */ 0 0 0
if (SvUTF8(subchar) && sublen && !utf8_to_bytes((U8 *)substr, &sublen)) { /* make sure no decoded string gets in */ 0 0 0
490 0 SvREFCNT_dec(subchar);
491 0 croak("Wide character");
492 }
493 } else {
494 11 ▶ if (fallback_cb != &PL_sv_undef) {
Branch: 1 branch
BranchTrueFalseTotal
if (fallback_cb != &PL_sv_undef) { 10 1 11
495 /* in decode mode we have sequence of wrong bytes */
496 10 subchar = do_bytes_fallback_cb(aTHX_ s, ulen, fallback_cb);
497 } else {
498 1 char *ptr = esc;
499 /* ENCODE_PERLQQ is already stored in esc */
500 1 ▶ if (check & (ENCODE_HTMLCREF|ENCODE_XMLCREF))
Branch: 1 branch
BranchTrueFalseTotal
if (check & (ENCODE_HTMLCREF|ENCODE_XMLCREF)) 0 1 1
501 0 ▶ for (i=0; i<ulen; ++i) ptr += sprintf(ptr, ((check & ENCODE_HTMLCREF) ? "&#%u;" : "&#x%02X;"), s[i]);
Branch: 2 branches
BranchTrueFalseTotal
for (i=0; i<ulen; ++i) ptr += sprintf(ptr, ((check & ENCODE_HTMLCREF) ? "&#%u;" : "&#x%02X;"), s[i]); 0 0 0
for (i=0; i<ulen; ++i) ptr += sprintf(ptr, ((check & ENCODE_HTMLCREF) ? "&#%u;" : "&#x%02X;"), s[i]); 0 0 0
502 1 subchar = newSVpvn(esc, strlen(esc));
503 }
504 11 substr = SvPVutf8(subchar, sublen);
505 }
506 14 dlen += sublen - ulen;
507 14 SvCUR_set(dst, d-(U8 *)SvPVX(dst));
508 14 *SvEND(dst) = '\0';
509 14 sv_catpvn(dst, substr, sublen);
510 14 SvREFCNT_dec(subchar);
511 14 ▶ d = (U8 *) SvGROW(dst, dlen) + SvCUR(dst);
Branch: 2 branches
BranchTrueFalseTotal
d = (U8 *) SvGROW(dst, dlen) + SvCUR(dst); 14 0 14
d = (U8 *) SvGROW(dst, dlen) + SvCUR(dst); 0 14 14
512 } else {
513 290 STRLEN fbcharlen = strlen(FBCHAR_UTF8);
514 290 dlen += fbcharlen - ulen;
515 290 ▶ if (SvLEN(dst) < dlen) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvLEN(dst) < dlen) { 40 250 290
516 40 SvCUR_set(dst, d-(U8 *)SvPVX(dst));
517 40 d = (U8 *) sv_grow(dst, dlen) + SvCUR(dst);
518 }
519 290 memcpy(d, FBCHAR_UTF8, fbcharlen);
520 290 d += fbcharlen;
521 }
522 304 s += ulen;
523 }
524 31376 SvCUR_set(dst, d-(U8 *)SvPVX(dst));
525 31376 *SvEND(dst) = '\0';
526  
527 31376 return s;
528 }
529  
530 static SV *
531 8693 find_encoding(pTHX_ SV *enc)
532 {
533 8693 dSP;
534 I32 count;
535 SV *m_enc;
536 8693 SV *obj = &PL_sv_undef;
537 #ifndef SV_NOSTEAL
538 U32 tmp;
539 #endif
540  
541 8693 ENTER;
542 8693 SAVETMPS;
543 8693 ▶ PUSHMARK(sp);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(sp); 0 8693 8693
544  
545 8693 m_enc = sv_newmortal();
546 #ifndef SV_NOSTEAL
547 tmp = SvFLAGS(enc) & SVs_TEMP;
548 SvTEMP_off(enc);
549 sv_setsv_flags(m_enc, enc, 0);
550 SvFLAGS(enc) |= tmp;
551 #else
552 #if SV_NOSTEAL == 0
553 #error You have broken SV_NOSTEAL which cause memory corruption in sv_setsv_flags()
554 #error Most probably broken SV_NOSTEAL was defined by buggy version of ppport.h
555 #else
556 8693 sv_setsv_flags(m_enc, enc, SV_NOSTEAL);
557 #endif
558 #endif
559 8693 ▶ XPUSHs(m_enc);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(m_enc); 0 8693 8693
560  
561 8693 PUTBACK;
562  
563 8693 count = call_pv("Encode::find_encoding", G_SCALAR);
564  
565 8693 SPAGAIN;
566  
567 8693 ▶ if (count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) { 8693 0 8693
568 8693 obj = POPs;
569 8693 SvREFCNT_inc(obj);
570 }
571  
572 8693 PUTBACK;
573 8693 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 8693 0 8693
574 8693 LEAVE;
575 8693 return sv_2mortal(obj);
576 }
577  
578 static SV *
579 8712 call_encoding(pTHX_ const char *method, SV *obj, SV *src, SV *check)
580 {
581 8712 dSP;
582 I32 count;
583 8712 SV *dst = &PL_sv_undef;
584  
585 8712 ▶ PUSHMARK(sp);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(sp); 0 8712 8712
586  
587 8712 ▶ if (check)
Branch: 1 branch
BranchTrueFalseTotal
if (check) 2592 6120 8712
588 2592 check = sv_2mortal(newSVsv(check));
589  
590 8712 ▶ if (!check || SvROK(check) || !SvTRUE_nomg(check) || (SvIV_nomg(check) & ENCODE_LEAVE_SRC))
Branch: 4 branches
BranchTrueFalseTotal
if (!check || SvROK(check) || !SvTRUE_nomg(check) || (SvIV_nomg(check) & ENCODE_LEAVE_SRC)) 2592 6120 8712
if (!check || SvROK(check) || !SvTRUE_nomg(check) || (SvIV_nomg(check) & ENCODE_LEAVE_SRC)) 2574 18 2592
if (!check || SvROK(check) || !SvTRUE_nomg(check) || (SvIV_nomg(check) & ENCODE_LEAVE_SRC)) 2185 389 2574
if (!check || SvROK(check) || !SvTRUE_nomg(check) || (SvIV_nomg(check) & ENCODE_LEAVE_SRC)) 732 1453 2185
591 7259 src = sv_2mortal(newSVsv(src));
592  
593 8712 ▶ XPUSHs(obj);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(obj); 0 8712 8712
594 8712 ▶ XPUSHs(src);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(src); 0 8712 8712
595 8712 ▶ XPUSHs(check ? check : &PL_sv_no);
Branch: 2 branches
BranchTrueFalseTotal
XPUSHs(check ? check : &PL_sv_no); 0 8712 8712
XPUSHs(check ? check : &PL_sv_no); 2592 6120 8712
596  
597 8712 PUTBACK;
598  
599 8712 count = call_method(method, G_SCALAR);
600  
601 8446 SPAGAIN;
602  
603 8446 ▶ if (count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) { 8446 0 8446
604 8446 dst = POPs;
605 8446 SvREFCNT_inc(dst);
606 }
607  
608 8446 PUTBACK;
609 8446 return dst;
610 }
611  
612  
613 MODULE = Encode PACKAGE = Encode::utf8 PREFIX = Method_
614  
615 PROTOTYPES: DISABLE
616  
617 void
618 Method_decode(obj,src,check_sv = &PL_sv_no)
619 SV * obj
620 SV * src
621 SV * check_sv
622 PREINIT:
623 STRLEN slen;
624 U8 *s;
625 U8 *e;
626 SV *dst;
627 30818 bool renewed = 0;
628 IV check;
629 bool modify;
630 30818 ▶ dSP;
Branch: 1 branch
BranchTrueFalseTotal
dSP; 3 30815 30818
631 INIT:
632 30818 SvGETMAGIC(src);
633 30818 SvGETMAGIC(check_sv);
634 30818 ▶ check = SvROK(check_sv) ? ENCODE_PERLQQ|ENCODE_LEAVE_SRC : SvOK(check_sv) ? SvIV_nomg(check_sv) : 0;
Branch: 2 branches
BranchTrueFalseTotal
check = SvROK(check_sv) ? ENCODE_PERLQQ|ENCODE_LEAVE_SRC : SvOK(check_sv) ? SvIV_nomg(check_sv) : 0; 30808 10 30818
check = SvROK(check_sv) ? ENCODE_PERLQQ|ENCODE_LEAVE_SRC : SvOK(check_sv) ? SvIV_nomg(check_sv) : 0; 30808 0 30808
635 30818 ▶ modify = (check && !(check & ENCODE_LEAVE_SRC));
Branch: 2 branches
BranchTrueFalseTotal
modify = (check && !(check & ENCODE_LEAVE_SRC)); 370 30448 30818
modify = (check && !(check & ENCODE_LEAVE_SRC)); 355 15 370
636 PPCODE:
637 30818 ▶ if (!SvOK(src))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(src)) 12 30806 30818
638 12 XSRETURN_UNDEF;
639 61612 ▶ s = modify ? (U8 *)SvPV_force_nomg(src, slen) : (U8 *)SvPV_nomg(src, slen);
Branch: 1 branch
BranchTrueFalseTotal
s = modify ? (U8 *)SvPV_force_nomg(src, slen) : (U8 *)SvPV_nomg(src, slen); 352 30454 30806
640 30806 ▶ if (SvUTF8(src))
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8(src)) 10 30796 30806
641 10 utf8_safe_downgrade(aTHX_ &src, &s, &slen, modify);
642 30806 e = s+slen;
643  
644 /*
645 * PerlIO check -- we assume the object is of PerlIO if renewed
646 */
647 30806 ENTER; SAVETMPS;
648 30806 ▶ PUSHMARK(sp);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(sp); 0 30806 30806
649 30806 ▶ XPUSHs(obj);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(obj); 0 30806 30806
650 30806 PUTBACK;
651 30806 ▶ if (call_method("renewed",G_SCALAR) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (call_method("renewed",G_SCALAR) == 1) { 30806 0 30806
652 30806 SPAGAIN;
653 30806 renewed = (bool)POPi;
654 30806 PUTBACK;
655 #if 0
656 fprintf(stderr, "renewed == %d\n", renewed);
657 #endif
658 }
659 30806 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 30806 0 30806
660 /* end PerlIO check */
661  
662 30806 ▶ dst = sv_2mortal(newSV(slen>0?slen:1)); /* newSV() abhors 0 -- inaba */
Branch: 1 branch
BranchTrueFalseTotal
dst = sv_2mortal(newSV(slen>0?slen:1)); /* newSV() abhors 0 -- inaba */ 30804 2 30806
663 30806 s = process_utf8(aTHX_ dst, s, e, check_sv, 0, strict_utf8(aTHX_ obj), renewed);
664  
665 /* Clear out translated part of source unless asked not to */
666 30755 ▶ if (modify) {
Branch: 1 branch
BranchTrueFalseTotal
if (modify) { 304 30451 30755
667 304 slen = e-s;
668 304 sv_setpvn(src, (char*)s, slen);
669 304 ▶ SvSETMAGIC(src);
Branch: 1 branch
BranchTrueFalseTotal
SvSETMAGIC(src); 6 298 304
670 }
671 30755 SvUTF8_on(dst);
672 30755 ▶ if (SvTAINTED(src)) SvTAINTED_on(dst); /* propagate taintedness */
Branch: 3 branches
BranchTrueFalseTotal
if (SvTAINTED(src)) SvTAINTED_on(dst); /* propagate taintedness */ 10 30745 30755
if (SvTAINTED(src)) SvTAINTED_on(dst); /* propagate taintedness */ 3 7 10
if (SvTAINTED(src)) SvTAINTED_on(dst); /* propagate taintedness */ 3 0 3
673 30755 ST(0) = dst;
674 30755 XSRETURN(1);
675  
676 void
677 Method_encode(obj,src,check_sv = &PL_sv_no)
678 SV * obj
679 SV * src
680 SV * check_sv
681 PREINIT:
682 STRLEN slen;
683 U8 *s;
684 U8 *e;
685 SV *dst;
686 IV check;
687 bool modify;
688 INIT:
689 1388 SvGETMAGIC(src);
690 1388 SvGETMAGIC(check_sv);
691 1388 ▶ check = SvROK(check_sv) ? ENCODE_PERLQQ|ENCODE_LEAVE_SRC : SvOK(check_sv) ? SvIV_nomg(check_sv) : 0;
Branch: 2 branches
BranchTrueFalseTotal
check = SvROK(check_sv) ? ENCODE_PERLQQ|ENCODE_LEAVE_SRC : SvOK(check_sv) ? SvIV_nomg(check_sv) : 0; 1384 4 1388
check = SvROK(check_sv) ? ENCODE_PERLQQ|ENCODE_LEAVE_SRC : SvOK(check_sv) ? SvIV_nomg(check_sv) : 0; 1384 0 1384
692 1388 ▶ modify = (check && !(check & ENCODE_LEAVE_SRC));
Branch: 2 branches
BranchTrueFalseTotal
modify = (check && !(check & ENCODE_LEAVE_SRC)); 93 1295 1388
modify = (check && !(check & ENCODE_LEAVE_SRC)); 79 14 93
693 PPCODE:
694 1388 ▶ if (!SvOK(src))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(src)) 10 1378 1388
695 10 XSRETURN_UNDEF;
696 2756 ▶ s = modify ? (U8 *)SvPV_force_nomg(src, slen) : (U8 *)SvPV_nomg(src, slen);
Branch: 1 branch
BranchTrueFalseTotal
s = modify ? (U8 *)SvPV_force_nomg(src, slen) : (U8 *)SvPV_nomg(src, slen); 76 1302 1378
697 1378 e = s+slen;
698 1378 ▶ dst = sv_2mortal(newSV(slen>0?slen:1)); /* newSV() abhors 0 -- inaba */
Branch: 1 branch
BranchTrueFalseTotal
dst = sv_2mortal(newSV(slen>0?slen:1)); /* newSV() abhors 0 -- inaba */ 1377 1 1378
699 1378 ▶ if (SvUTF8(src)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8(src)) { 639 739 1378
700 /* Already encoded */
701 639 ▶ if (strict_utf8(aTHX_ obj)) {
Branch: 1 branch
BranchTrueFalseTotal
if (strict_utf8(aTHX_ obj)) { 635 4 639
702 635 s = process_utf8(aTHX_ dst, s, e, check_sv, 1, 1, 0);
703 }
704 else {
705 /* trust it and just copy the octets */
706 4 sv_setpvn(dst,(char *)s,(e-s));
707 4 s = e;
708 }
709 }
710 else {
711 /* Native bytes - can always encode */
712 739 ▶ U8 *d = (U8 *) SvGROW(dst, 2*slen+1); /* +1 or assertion will botch */
Branch: 2 branches
BranchTrueFalseTotal
U8 *d = (U8 *) SvGROW(dst, 2*slen+1); /* +1 or assertion will botch */ 739 0 739
U8 *d = (U8 *) SvGROW(dst, 2*slen+1); /* +1 or assertion will botch */ 9 730 739
713 1566 ▶ while (s < e) {
Branch: 1 branch
BranchTrueFalseTotal
while (s < e) { 827 739 1566
714 #ifdef append_utf8_from_native_byte
715 append_utf8_from_native_byte(*s, &d);
716 s++;
717 #else
718 827 UV uv = NATIVE_TO_UNI((UV) *s);
719 827 s++; /* Above expansion of NATIVE_TO_UNI() is safer this way. */
720 827 ▶ if (UNI_IS_INVARIANT(uv))
Branch: 1 branch
BranchTrueFalseTotal
if (UNI_IS_INVARIANT(uv)) 818 9 827
721 818 *d++ = (U8)UTF_TO_NATIVE(uv);
722 else {
723 9 *d++ = (U8)UTF8_EIGHT_BIT_HI(uv);
724 9 *d++ = (U8)UTF8_EIGHT_BIT_LO(uv);
725 }
726 #endif
727 }
728 739 SvCUR_set(dst, d- (U8 *)SvPVX(dst));
729 739 *SvEND(dst) = '\0';
730 }
731  
732 /* Clear out translated part of source unless asked not to */
733 1364 ▶ if (modify) {
Branch: 1 branch
BranchTrueFalseTotal
if (modify) { 63 1301 1364
734 63 slen = e-s;
735 63 sv_setpvn(src, (char*)s, slen);
736 63 ▶ SvSETMAGIC(src);
Branch: 1 branch
BranchTrueFalseTotal
SvSETMAGIC(src); 5 58 63
737 }
738 1364 SvPOK_only(dst);
739 1364 SvUTF8_off(dst);
740 1364 ▶ if (SvTAINTED(src)) SvTAINTED_on(dst); /* propagate taintedness */
Branch: 3 branches
BranchTrueFalseTotal
if (SvTAINTED(src)) SvTAINTED_on(dst); /* propagate taintedness */ 24 1340 1364
if (SvTAINTED(src)) SvTAINTED_on(dst); /* propagate taintedness */ 16 8 24
if (SvTAINTED(src)) SvTAINTED_on(dst); /* propagate taintedness */ 16 0 16
741 1364 ST(0) = dst;
742 1364 XSRETURN(1);
743  
744 MODULE = Encode PACKAGE = Encode::XS PREFIX = Method_
745  
746 PROTOTYPES: DISABLE
747  
748 SV *
749 Method_renew(obj)
750 SV * obj
751 CODE:
752 PERL_UNUSED_VAR(obj);
753 22 RETVAL = newSVsv(obj);
754 OUTPUT:
755 RETVAL
756  
757 int
758 Method_renewed(obj)
759 SV * obj
760 CODE:
761 0 ▶ RETVAL = 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = 0; 0 0 0
762 PERL_UNUSED_VAR(obj);
763 OUTPUT:
764 RETVAL
765  
766 SV *
767 Method_name(obj)
768 SV * obj
769 PREINIT:
770 encode_t *enc;
771 INIT:
772 815 enc = INT2PTR(encode_t *, SvIV(SvRV(obj)));
773 CODE:
774 815 RETVAL = newSVpvn(enc->name[0], strlen(enc->name[0]));
775 OUTPUT:
776 RETVAL
777  
778 bool
779 Method_cat_decode(obj, dst, src, off, term, check_sv = &PL_sv_no)
780 SV * obj
781 SV * dst
782 SV * src
783 SV * off
784 SV * term
785 SV * check_sv
786 PREINIT:
787 IV check;
788 SV *fallback_cb;
789 bool modify;
790 encode_t *enc;
791 STRLEN offset;
792 0 ▶ int code = 0;
Branch: 1 branch
BranchTrueFalseTotal
int code = 0; 0 0 0
793 U8 *s;
794 STRLEN slen;
795 SV *tmp;
796 INIT:
797 0 SvGETMAGIC(src);
798 0 SvGETMAGIC(check_sv);
799 0 ▶ check = SvROK(check_sv) ? ENCODE_PERLQQ|ENCODE_LEAVE_SRC : SvOK(check_sv) ? SvIV_nomg(check_sv) : 0;
Branch: 2 branches
BranchTrueFalseTotal
check = SvROK(check_sv) ? ENCODE_PERLQQ|ENCODE_LEAVE_SRC : SvOK(check_sv) ? SvIV_nomg(check_sv) : 0; 0 0 0
check = SvROK(check_sv) ? ENCODE_PERLQQ|ENCODE_LEAVE_SRC : SvOK(check_sv) ? SvIV_nomg(check_sv) : 0; 0 0 0
800 0 ▶ fallback_cb = SvROK(check_sv) ? check_sv : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
fallback_cb = SvROK(check_sv) ? check_sv : &PL_sv_undef; 0 0 0
801 0 ▶ modify = (check && !(check & ENCODE_LEAVE_SRC));
Branch: 2 branches
BranchTrueFalseTotal
modify = (check && !(check & ENCODE_LEAVE_SRC)); 0 0 0
modify = (check && !(check & ENCODE_LEAVE_SRC)); 0 0 0
802 0 enc = INT2PTR(encode_t *, SvIV(SvRV(obj)));
803 0 offset = (STRLEN)SvIV(off);
804 CODE:
805 0 ▶ if (!SvOK(src))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(src)) 0 0 0
806 0 XSRETURN_NO;
807 0 ▶ s = modify ? (U8 *)SvPV_force_nomg(src, slen) : (U8 *)SvPV_nomg(src, slen);
Branch: 1 branch
BranchTrueFalseTotal
s = modify ? (U8 *)SvPV_force_nomg(src, slen) : (U8 *)SvPV_nomg(src, slen); 0 0 0
808 0 ▶ if (SvUTF8(src))
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8(src)) 0 0 0
809 0 utf8_safe_downgrade(aTHX_ &src, &s, &slen, modify);
810 0 tmp = encode_method(aTHX_ enc, enc->t_utf8, src, s, slen, check,
811 &offset, term, &code, fallback_cb);
812 0 sv_catsv(dst, tmp);
813 0 SvIV_set(off, (IV)offset);
814 0 ▶ RETVAL = (code == ENCODE_FOUND_TERM);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (code == ENCODE_FOUND_TERM); 0 0 0
815 OUTPUT:
816 RETVAL
817  
818 void
819 Method_decode(obj,src,check_sv = &PL_sv_no)
820 SV * obj
821 SV * src
822 SV * check_sv
823 PREINIT:
824 IV check;
825 SV *fallback_cb;
826 bool modify;
827 encode_t *enc;
828 U8 *s;
829 STRLEN slen;
830 SV *ret;
831 INIT:
832 23904 SvGETMAGIC(src);
833 23904 SvGETMAGIC(check_sv);
834 23904 ▶ check = SvROK(check_sv) ? ENCODE_PERLQQ|ENCODE_LEAVE_SRC : SvOK(check_sv) ? SvIV_nomg(check_sv) : 0;
Branch: 2 branches
BranchTrueFalseTotal
check = SvROK(check_sv) ? ENCODE_PERLQQ|ENCODE_LEAVE_SRC : SvOK(check_sv) ? SvIV_nomg(check_sv) : 0; 23898 6 23904
check = SvROK(check_sv) ? ENCODE_PERLQQ|ENCODE_LEAVE_SRC : SvOK(check_sv) ? SvIV_nomg(check_sv) : 0; 23898 0 23898
835 23904 ▶ fallback_cb = SvROK(check_sv) ? check_sv : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
fallback_cb = SvROK(check_sv) ? check_sv : &PL_sv_undef; 6 23898 23904
836 23904 ▶ modify = (check && !(check & ENCODE_LEAVE_SRC));
Branch: 2 branches
BranchTrueFalseTotal
modify = (check && !(check & ENCODE_LEAVE_SRC)); 905 22999 23904
modify = (check && !(check & ENCODE_LEAVE_SRC)); 847 58 905
837 23904 enc = INT2PTR(encode_t *, SvIV(SvRV(obj)));
838 CODE:
839 23904 ▶ if (!SvOK(src))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(src)) 214 23690 23904
840 214 XSRETURN_UNDEF;
841 47380 ▶ s = modify ? (U8 *)SvPV_force_nomg(src, slen) : (U8 *)SvPV_nomg(src, slen);
Branch: 1 branch
BranchTrueFalseTotal
s = modify ? (U8 *)SvPV_force_nomg(src, slen) : (U8 *)SvPV_nomg(src, slen); 843 22847 23690
842 23690 ▶ if (SvUTF8(src))
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8(src)) 13 23677 23690
843 13 utf8_safe_downgrade(aTHX_ &src, &s, &slen, modify);
844 23690 ret = encode_method(aTHX_ enc, enc->t_utf8, src, s, slen, check,
845 NULL, Nullsv, NULL, fallback_cb);
846 23690 SvUTF8_on(ret);
847 23690 ST(0) = ret;
848 23690 XSRETURN(1);
849  
850 void
851 Method_encode(obj,src,check_sv = &PL_sv_no)
852 SV * obj
853 SV * src
854 SV * check_sv
855 PREINIT:
856 IV check;
857 SV *fallback_cb;
858 bool modify;
859 encode_t *enc;
860 U8 *s;
861 STRLEN slen;
862 SV *ret;
863 INIT:
864 35386 SvGETMAGIC(src);
865 35386 SvGETMAGIC(check_sv);
866 35386 ▶ check = SvROK(check_sv) ? ENCODE_PERLQQ|ENCODE_LEAVE_SRC : SvOK(check_sv) ? SvIV_nomg(check_sv) : 0;
Branch: 2 branches
BranchTrueFalseTotal
check = SvROK(check_sv) ? ENCODE_PERLQQ|ENCODE_LEAVE_SRC : SvOK(check_sv) ? SvIV_nomg(check_sv) : 0; 35377 9 35386
check = SvROK(check_sv) ? ENCODE_PERLQQ|ENCODE_LEAVE_SRC : SvOK(check_sv) ? SvIV_nomg(check_sv) : 0; 35058 319 35377
867 35386 ▶ fallback_cb = SvROK(check_sv) ? check_sv : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
fallback_cb = SvROK(check_sv) ? check_sv : &PL_sv_undef; 9 35377 35386
868 35386 ▶ modify = (check && !(check & ENCODE_LEAVE_SRC));
Branch: 2 branches
BranchTrueFalseTotal
modify = (check && !(check & ENCODE_LEAVE_SRC)); 17363 18023 35386
modify = (check && !(check & ENCODE_LEAVE_SRC)); 17340 23 17363
869 35386 enc = INT2PTR(encode_t *, SvIV(SvRV(obj)));
870 CODE:
871 35386 ▶ if (!SvOK(src))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(src)) 214 35172 35386
872 214 XSRETURN_UNDEF;
873 70344 ▶ s = modify ? (U8 *)SvPV_force_nomg(src, slen) : (U8 *)SvPV_nomg(src, slen);
Branch: 1 branch
BranchTrueFalseTotal
s = modify ? (U8 *)SvPV_force_nomg(src, slen) : (U8 *)SvPV_nomg(src, slen); 17336 17836 35172
874 35172 ▶ if (!SvUTF8(src))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvUTF8(src)) 20 35152 35172
875 20 utf8_safe_upgrade(aTHX_ &src, &s, &slen, modify);
876 35172 ret = encode_method(aTHX_ enc, enc->f_utf8, src, s, slen, check,
877 NULL, Nullsv, NULL, fallback_cb);
878 35171 ST(0) = ret;
879 35171 XSRETURN(1);
880  
881 bool
882 Method_needs_lines(obj)
883 SV * obj
884 CODE:
885 PERL_UNUSED_VAR(obj);
886 22 ▶ RETVAL = FALSE;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = FALSE; 0 22 22
887 OUTPUT:
888 RETVAL
889  
890 bool
891 Method_perlio_ok(obj)
892 SV * obj
893 PREINIT:
894 SV *sv;
895 CODE:
896 PERL_UNUSED_VAR(obj);
897 8 sv = eval_pv("require PerlIO::encoding", 0);
898 8 RETVAL = SvTRUE(sv);
899 OUTPUT:
900 RETVAL
901  
902 SV *
903 Method_mime_name(obj)
904 SV * obj
905 PREINIT:
906 encode_t *enc;
907 INIT:
908 110 enc = INT2PTR(encode_t *, SvIV(SvRV(obj)));
909 CODE:
910 110 ENTER;
911 110 SAVETMPS;
912 110 ▶ PUSHMARK(sp);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(sp); 0 110 110
913 110 ▶ XPUSHs(sv_2mortal(newSVpvn(enc->name[0], strlen(enc->name[0]))));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvn(enc->name[0], strlen(enc->name[0])))); 0 110 110
914 110 PUTBACK;
915 110 call_pv("Encode::MIME::Name::get_mime_name", G_SCALAR);
916 110 SPAGAIN;
917 110 RETVAL = newSVsv(POPs);
918 110 PUTBACK;
919 110 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 110 0 110
920 110 LEAVE;
921 OUTPUT:
922 RETVAL
923  
924 MODULE = Encode PACKAGE = Encode
925  
926 PROTOTYPES: ENABLE
927  
928 bool
929 is_utf8(sv, check = 0)
930 SV * sv
931 int check
932 PREINIT:
933 char *str;
934 STRLEN len;
935 CODE:
936 654 SvGETMAGIC(sv); /* SvGETMAGIC() can modify SvOK flag */
937 1303 ▶ str = SvOK(sv) ? SvPV_nomg(sv, len) : NULL; /* SvPV() can modify SvUTF8 flag */
Branch: 1 branch
BranchTrueFalseTotal
str = SvOK(sv) ? SvPV_nomg(sv, len) : NULL; /* SvPV() can modify SvUTF8 flag */ 649 5 654
938 654 RETVAL = SvUTF8(sv) ? TRUE : FALSE;
939 654 ▶ if (RETVAL && check && (!str || !is_utf8_string((U8 *)str, len)))
Branch: 4 branches
BranchTrueFalseTotal
if (RETVAL && check && (!str || !is_utf8_string((U8 *)str, len))) 60 594 654
if (RETVAL && check && (!str || !is_utf8_string((U8 *)str, len))) 0 60 60
if (RETVAL && check && (!str || !is_utf8_string((U8 *)str, len))) 0 0 0
if (RETVAL && check && (!str || !is_utf8_string((U8 *)str, len))) 0 0 0
940 0 RETVAL = FALSE;
941 OUTPUT:
942 RETVAL
943  
944 SV *
945 _utf8_on(sv)
946 SV * sv
947 CODE:
948 8 SvGETMAGIC(sv);
949 8 ▶ if (!SvTAINTED(sv) && SvPOKp(sv)) {
Branch: 3 branches
BranchTrueFalseTotal
if (!SvTAINTED(sv) && SvPOKp(sv)) { 4 4 8
if (!SvTAINTED(sv) && SvPOKp(sv)) { 3 1 4
if (!SvTAINTED(sv) && SvPOKp(sv)) { 6 1 7
950 6 ▶ if (SvTHINKFIRST(sv)) sv_force_normal(sv);
Branch: 1 branch
BranchTrueFalseTotal
if (SvTHINKFIRST(sv)) sv_force_normal(sv); 5 1 6
951 6 ▶ RETVAL = boolSV(SvUTF8(sv));
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = boolSV(SvUTF8(sv)); 1 5 6
952 6 SvUTF8_on(sv);
953 6 ▶ SvSETMAGIC(sv);
Branch: 1 branch
BranchTrueFalseTotal
SvSETMAGIC(sv); 2 4 6
954 } else {
955 2 RETVAL = &PL_sv_undef;
956 }
957 OUTPUT:
958 RETVAL
959  
960 SV *
961 _utf8_off(sv)
962 SV * sv
963 CODE:
964 6 SvGETMAGIC(sv);
965 6 ▶ if (!SvTAINTED(sv) && SvPOKp(sv)) {
Branch: 3 branches
BranchTrueFalseTotal
if (!SvTAINTED(sv) && SvPOKp(sv)) { 4 2 6
if (!SvTAINTED(sv) && SvPOKp(sv)) { 3 1 4
if (!SvTAINTED(sv) && SvPOKp(sv)) { 4 1 5
966 4 ▶ if (SvTHINKFIRST(sv)) sv_force_normal(sv);
Branch: 1 branch
BranchTrueFalseTotal
if (SvTHINKFIRST(sv)) sv_force_normal(sv); 3 1 4
967 4 ▶ RETVAL = boolSV(SvUTF8(sv));
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = boolSV(SvUTF8(sv)); 3 1 4
968 4 SvUTF8_off(sv);
969 4 ▶ SvSETMAGIC(sv);
Branch: 1 branch
BranchTrueFalseTotal
SvSETMAGIC(sv); 2 2 4
970 } else {
971 2 RETVAL = &PL_sv_undef;
972 }
973 OUTPUT:
974 RETVAL
975  
976 SV *
977 decode(encoding, octets, check = NULL)
978 SV * encoding
979 SV * octets
980 SV * check
981 ALIAS:
982 bytes2str = 0
983 PREINIT:
984 SV *obj;
985 INIT:
986 PERL_UNUSED_VAR(ix);
987 1422 SvGETMAGIC(encoding);
988 CODE:
989 1422 ▶ if (!SvOK(encoding))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(encoding)) 0 1422 1422
990 0 croak("Encoding name should not be undef");
991 1422 obj = find_encoding(aTHX_ encoding);
992 1422 ▶ if (!SvOK(obj))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(obj)) 0 1422 1422
993 0 croak("Unknown encoding '%" SVf "'", SVfARG(encoding));
994 1422 RETVAL = call_encoding(aTHX_ "decode", obj, octets, check);
995 OUTPUT:
996 RETVAL
997  
998 SV *
999 encode(encoding, string, check = NULL)
1000 SV * encoding
1001 SV * string
1002 SV * check
1003 ALIAS:
1004 str2bytes = 0
1005 PREINIT:
1006 SV *obj;
1007 INIT:
1008 PERL_UNUSED_VAR(ix);
1009 2343 SvGETMAGIC(encoding);
1010 CODE:
1011 2343 ▶ if (!SvOK(encoding))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(encoding)) 0 2343 2343
1012 0 croak("Encoding name should not be undef");
1013 2343 obj = find_encoding(aTHX_ encoding);
1014 2343 ▶ if (!SvOK(obj))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(obj)) 0 2343 2343
1015 0 croak("Unknown encoding '%" SVf "'", SVfARG(encoding));
1016 2343 RETVAL = call_encoding(aTHX_ "encode", obj, string, check);
1017 OUTPUT:
1018 RETVAL
1019  
1020 SV *
1021 decode_utf8(octets, check = NULL)
1022 SV * octets
1023 SV * check
1024 PREINIT:
1025 HV *hv;
1026 SV **sv;
1027 CODE:
1028 19 hv = get_hv("Encode::Encoding", 0);
1029 19 ▶ if (!hv)
Branch: 1 branch
BranchTrueFalseTotal
if (!hv) 0 19 19
1030 0 croak("utf8 encoding was not found");
1031 19 sv = hv_fetch(hv, "utf8", 4, 0);
1032 19 ▶ if (!sv || !*sv || !SvOK(*sv))
Branch: 3 branches
BranchTrueFalseTotal
if (!sv || !*sv || !SvOK(*sv)) 19 0 19
if (!sv || !*sv || !SvOK(*sv)) 19 0 19
if (!sv || !*sv || !SvOK(*sv)) 0 19 19
1033 0 croak("utf8 encoding was not found");
1034 19 RETVAL = call_encoding(aTHX_ "decode", *sv, octets, check);
1035 OUTPUT:
1036 RETVAL
1037  
1038 SV *
1039 encode_utf8(string)
1040 SV * string
1041 CODE:
1042 387 RETVAL = newSVsv(string);
1043 387 ▶ if (SvOK(RETVAL))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(RETVAL)) 385 2 387
1044 385 sv_utf8_encode(RETVAL);
1045 OUTPUT:
1046 RETVAL
1047  
1048 SV *
1049 from_to(octets, from, to, check = NULL)
1050 SV * octets
1051 SV * from
1052 SV * to
1053 SV * check
1054 PREINIT:
1055 SV *from_obj;
1056 SV *to_obj;
1057 SV *string;
1058 SV *new_octets;
1059 U8 *ptr;
1060 STRLEN len;
1061 INIT:
1062 2464 SvGETMAGIC(from);
1063 2464 SvGETMAGIC(to);
1064 CODE:
1065 2464 ▶ if (!SvOK(from) || !SvOK(to))
Branch: 2 branches
BranchTrueFalseTotal
if (!SvOK(from) || !SvOK(to)) 2464 0 2464
if (!SvOK(from) || !SvOK(to)) 0 2464 2464
1066 0 croak("Encoding name should not be undef");
1067 2464 from_obj = find_encoding(aTHX_ from);
1068 2464 ▶ if (!SvOK(from_obj))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(from_obj)) 0 2464 2464
1069 0 croak("Unknown encoding '%" SVf "'", SVfARG(from));
1070 2464 to_obj = find_encoding(aTHX_ to);
1071 2464 ▶ if (!SvOK(to_obj))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(to_obj)) 0 2464 2464
1072 0 croak("Unknown encoding '%" SVf "'", SVfARG(to));
1073 2464 string = sv_2mortal(call_encoding(aTHX_ "decode", from_obj, octets, NULL));
1074 2464 new_octets = sv_2mortal(call_encoding(aTHX_ "encode", to_obj, string, check));
1075 2464 SvGETMAGIC(new_octets);
1076 2464 ▶ if (SvOK(new_octets) && (!check || SvROK(check) || !SvTRUE_nomg(check) || sv_len(string) == 0)) {
Branch: 5 branches
BranchTrueFalseTotal
if (SvOK(new_octets) && (!check || SvROK(check) || !SvTRUE_nomg(check) || sv_len(string) == 0)) { 2464 0 2464
if (SvOK(new_octets) && (!check || SvROK(check) || !SvTRUE_nomg(check) || sv_len(string) == 0)) { 320 2144 2464
if (SvOK(new_octets) && (!check || SvROK(check) || !SvTRUE_nomg(check) || sv_len(string) == 0)) { 320 0 320
if (SvOK(new_octets) && (!check || SvROK(check) || !SvTRUE_nomg(check) || sv_len(string) == 0)) { 1 319 320
if (SvOK(new_octets) && (!check || SvROK(check) || !SvTRUE_nomg(check) || sv_len(string) == 0)) { 0 1 1
1077 2463 ptr = (U8 *)SvPV_nomg(new_octets, len);
1078 2463 ▶ if (SvUTF8(new_octets))
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8(new_octets)) 0 2463 2463
1079 0 len = utf8_length(ptr, ptr+len);
1080 2463 RETVAL = newSVuv(len);
1081 } else {
1082 1 RETVAL = &PL_sv_undef;
1083 }
1084 2464 sv_setsv_nomg(octets, new_octets);
1085 2464 ▶ SvSETMAGIC(octets);
Branch: 1 branch
BranchTrueFalseTotal
SvSETMAGIC(octets); 2 2462 2464
1086 OUTPUT:
1087 RETVAL
1088  
1089 void
1090 onBOOT()
1091 CODE:
1092 {
1093 #include "def_t.exh"
1094 }
1095  
1096 BOOT:
1097 {
1098 52 HV *stash = gv_stashpvn("Encode", (U32)strlen("Encode"), GV_ADD);
1099 52 newCONSTSUB(stash, "DIE_ON_ERR", newSViv(ENCODE_DIE_ON_ERR));
1100 52 newCONSTSUB(stash, "WARN_ON_ERR", newSViv(ENCODE_WARN_ON_ERR));
1101 52 newCONSTSUB(stash, "RETURN_ON_ERR", newSViv(ENCODE_RETURN_ON_ERR));
1102 52 newCONSTSUB(stash, "LEAVE_SRC", newSViv(ENCODE_LEAVE_SRC));
1103 52 newCONSTSUB(stash, "ONLY_PRAGMA_WARNINGS", newSViv(ENCODE_ONLY_PRAGMA_WARNINGS));
1104 52 newCONSTSUB(stash, "PERLQQ", newSViv(ENCODE_PERLQQ));
1105 52 newCONSTSUB(stash, "HTMLCREF", newSViv(ENCODE_HTMLCREF));
1106 52 newCONSTSUB(stash, "XMLCREF", newSViv(ENCODE_XMLCREF));
1107 52 newCONSTSUB(stash, "STOP_AT_PARTIAL", newSViv(ENCODE_STOP_AT_PARTIAL));
1108 52 newCONSTSUB(stash, "FB_DEFAULT", newSViv(ENCODE_FB_DEFAULT));
1109 52 newCONSTSUB(stash, "FB_CROAK", newSViv(ENCODE_FB_CROAK));
1110 52 newCONSTSUB(stash, "FB_QUIET", newSViv(ENCODE_FB_QUIET));
1111 52 newCONSTSUB(stash, "FB_WARN", newSViv(ENCODE_FB_WARN));
1112 52 newCONSTSUB(stash, "FB_PERLQQ", newSViv(ENCODE_FB_PERLQQ));
1113 52 newCONSTSUB(stash, "FB_HTMLCREF", newSViv(ENCODE_FB_HTMLCREF));
1114 52 newCONSTSUB(stash, "FB_XMLCREF", newSViv(ENCODE_FB_XMLCREF));
1115 }
« blib/lib/Encode/Unicode.pm ↑ Summary encengine.c »