Commit | Line | Data |
a0d0e21e |
1 | /* doop.c |
79072805 |
2 | * |
be3c0a43 |
3 | * Copyright (c) 1991-2002, Larry Wall |
79072805 |
4 | * |
5 | * You may distribute under the terms of either the GNU General Public |
6 | * License or the Artistic License, as specified in the README file. |
7 | * |
a0d0e21e |
8 | */ |
9 | |
10 | /* |
11 | * "'So that was the job I felt I had to do when I started,' thought Sam." |
79072805 |
12 | */ |
13 | |
14 | #include "EXTERN.h" |
864dbfa3 |
15 | #define PERL_IN_DOOP_C |
79072805 |
16 | #include "perl.h" |
17 | |
64ca3a65 |
18 | #ifndef PERL_MICRO |
79072805 |
19 | #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX) |
20 | #include <signal.h> |
21 | #endif |
64ca3a65 |
22 | #endif |
79072805 |
23 | |
942e002e |
24 | STATIC I32 |
b250498f |
25 | S_do_trans_simple(pTHX_ SV *sv) |
79072805 |
26 | { |
4757a243 |
27 | U8 *s; |
b250498f |
28 | U8 *d; |
4757a243 |
29 | U8 *send; |
b250498f |
30 | U8 *dstart; |
4757a243 |
31 | I32 matches = 0; |
9b877dbb |
32 | I32 grows = PL_op->op_private & OPpTRANS_GROWS; |
463ee0b2 |
33 | STRLEN len; |
4757a243 |
34 | short *tbl; |
35 | I32 ch; |
79072805 |
36 | |
4757a243 |
37 | tbl = (short*)cPVOP->op_pv; |
38 | if (!tbl) |
7d85a32c |
39 | Perl_croak(aTHX_ "panic: do_trans_simple line %d",__LINE__); |
a0ed51b3 |
40 | |
4757a243 |
41 | s = (U8*)SvPV(sv, len); |
42 | send = s + len; |
43 | |
b250498f |
44 | /* First, take care of non-UTF8 input strings, because they're easy */ |
1aa99e6b |
45 | if (!SvUTF8(sv)) { |
01ec43d0 |
46 | while (s < send) { |
036b4402 |
47 | if ((ch = tbl[*s]) >= 0) { |
01ec43d0 |
48 | matches++; |
eb160463 |
49 | *s++ = (U8)ch; |
01ec43d0 |
50 | } |
51 | else |
52 | s++; |
53 | } |
54 | SvSETMAGIC(sv); |
b250498f |
55 | return matches; |
56 | } |
4757a243 |
57 | |
b250498f |
58 | /* Allow for expansion: $_="a".chr(400); tr/a/\xFE/, FE needs encoding */ |
9b877dbb |
59 | if (grows) |
60 | New(0, d, len*2+1, U8); |
61 | else |
62 | d = s; |
b250498f |
63 | dstart = d; |
64 | while (s < send) { |
ba210ebe |
65 | STRLEN ulen; |
9b877dbb |
66 | UV c; |
b250498f |
67 | |
b250498f |
68 | /* Need to check this, otherwise 128..255 won't match */ |
9041c2e3 |
69 | c = utf8n_to_uvchr(s, send - s, &ulen, 0); |
9b877dbb |
70 | if (c < 0x100 && (ch = tbl[c]) >= 0) { |
b250498f |
71 | matches++; |
9041c2e3 |
72 | d = uvchr_to_utf8(d, ch); |
b250498f |
73 | s += ulen; |
01ec43d0 |
74 | } |
75 | else { /* No match -> copy */ |
9b877dbb |
76 | Copy(s, d, ulen, U8); |
77 | d += ulen; |
78 | s += ulen; |
b250498f |
79 | } |
80 | } |
9b877dbb |
81 | if (grows) { |
82 | sv_setpvn(sv, (char*)dstart, d - dstart); |
83 | Safefree(dstart); |
84 | } |
85 | else { |
86 | *d = '\0'; |
87 | SvCUR_set(sv, d - dstart); |
88 | } |
b250498f |
89 | SvUTF8_on(sv); |
b250498f |
90 | SvSETMAGIC(sv); |
4757a243 |
91 | return matches; |
92 | } |
93 | |
942e002e |
94 | STATIC I32 |
21304a3b |
95 | S_do_trans_count(pTHX_ SV *sv) |
4757a243 |
96 | { |
4757a243 |
97 | U8 *s; |
98 | U8 *send; |
99 | I32 matches = 0; |
100 | STRLEN len; |
101 | short *tbl; |
8973db79 |
102 | I32 complement = PL_op->op_private & OPpTRANS_COMPLEMENT; |
4757a243 |
103 | |
104 | tbl = (short*)cPVOP->op_pv; |
105 | if (!tbl) |
7d85a32c |
106 | Perl_croak(aTHX_ "panic: do_trans_count line %d",__LINE__); |
4757a243 |
107 | |
108 | s = (U8*)SvPV(sv, len); |
109 | send = s + len; |
110 | |
1aa99e6b |
111 | if (!SvUTF8(sv)) |
112 | while (s < send) { |
113 | if (tbl[*s++] >= 0) |
036b4402 |
114 | matches++; |
1aa99e6b |
115 | } |
116 | else |
117 | while (s < send) { |
118 | UV c; |
119 | STRLEN ulen; |
9041c2e3 |
120 | c = utf8n_to_uvchr(s, send - s, &ulen, 0); |
8973db79 |
121 | if (c < 0x100) { |
122 | if (tbl[c] >= 0) |
123 | matches++; |
124 | } else if (complement) |
1aa99e6b |
125 | matches++; |
126 | s += ulen; |
127 | } |
4757a243 |
128 | |
129 | return matches; |
130 | } |
131 | |
942e002e |
132 | STATIC I32 |
21304a3b |
133 | S_do_trans_complex(pTHX_ SV *sv) |
4757a243 |
134 | { |
4757a243 |
135 | U8 *s; |
136 | U8 *send; |
137 | U8 *d; |
5e44153e |
138 | U8 *dstart; |
1aa99e6b |
139 | I32 isutf8; |
4757a243 |
140 | I32 matches = 0; |
9b877dbb |
141 | I32 grows = PL_op->op_private & OPpTRANS_GROWS; |
8973db79 |
142 | I32 complement = PL_op->op_private & OPpTRANS_COMPLEMENT; |
143 | I32 del = PL_op->op_private & OPpTRANS_DELETE; |
b7953727 |
144 | STRLEN len, rlen = 0; |
4757a243 |
145 | short *tbl; |
146 | I32 ch; |
147 | |
148 | tbl = (short*)cPVOP->op_pv; |
149 | if (!tbl) |
7d85a32c |
150 | Perl_croak(aTHX_ "panic: do_trans_complex line %d",__LINE__); |
4757a243 |
151 | |
152 | s = (U8*)SvPV(sv, len); |
1aa99e6b |
153 | isutf8 = SvUTF8(sv); |
4757a243 |
154 | send = s + len; |
155 | |
1aa99e6b |
156 | if (!isutf8) { |
157 | dstart = d = s; |
158 | if (PL_op->op_private & OPpTRANS_SQUASH) { |
159 | U8* p = send; |
160 | while (s < send) { |
161 | if ((ch = tbl[*s]) >= 0) { |
eb160463 |
162 | *d = (U8)ch; |
036b4402 |
163 | matches++; |
1aa99e6b |
164 | if (p != d - 1 || *p != *d) |
165 | p = d++; |
166 | } |
167 | else if (ch == -1) /* -1 is unmapped character */ |
168 | *d++ = *s; |
169 | else if (ch == -2) /* -2 is delete character */ |
170 | matches++; |
171 | s++; |
172 | } |
a0ed51b3 |
173 | } |
1aa99e6b |
174 | else { |
175 | while (s < send) { |
176 | if ((ch = tbl[*s]) >= 0) { |
177 | matches++; |
eb160463 |
178 | *d++ = (U8)ch; |
1aa99e6b |
179 | } |
180 | else if (ch == -1) /* -1 is unmapped character */ |
181 | *d++ = *s; |
182 | else if (ch == -2) /* -2 is delete character */ |
183 | matches++; |
184 | s++; |
185 | } |
186 | } |
76ef7183 |
187 | *d = '\0'; |
1aa99e6b |
188 | SvCUR_set(sv, d - dstart); |
4757a243 |
189 | } |
1aa99e6b |
190 | else { /* isutf8 */ |
9b877dbb |
191 | if (grows) |
192 | New(0, d, len*2+1, U8); |
193 | else |
194 | d = s; |
1aa99e6b |
195 | dstart = d; |
8973db79 |
196 | if (complement && !del) |
197 | rlen = tbl[0x100]; |
1aa99e6b |
198 | |
4aecb5b5 |
199 | #ifdef MACOS_TRADITIONAL |
200 | #define comp CoMP /* "comp" is a keyword in some compilers ... */ |
201 | #endif |
202 | |
1aa99e6b |
203 | if (PL_op->op_private & OPpTRANS_SQUASH) { |
1aa99e6b |
204 | UV pch = 0xfeedface; |
205 | while (s < send) { |
206 | STRLEN len; |
9041c2e3 |
207 | UV comp = utf8_to_uvchr(s, &len); |
1aa99e6b |
208 | |
8973db79 |
209 | if (comp > 0xff) { |
210 | if (!complement) { |
211 | Copy(s, d, len, U8); |
212 | d += len; |
213 | } |
214 | else { |
215 | matches++; |
216 | if (!del) { |
05d340b8 |
217 | ch = (rlen == 0) ? comp : |
218 | (comp - 0x100 < rlen) ? |
8973db79 |
219 | tbl[comp+1] : tbl[0x100+rlen]; |
eb160463 |
220 | if ((UV)ch != pch) { |
9041c2e3 |
221 | d = uvchr_to_utf8(d, ch); |
eb160463 |
222 | pch = (UV)ch; |
8973db79 |
223 | } |
224 | s += len; |
225 | continue; |
226 | } |
227 | } |
9b877dbb |
228 | } |
1aa99e6b |
229 | else if ((ch = tbl[comp]) >= 0) { |
230 | matches++; |
eb160463 |
231 | if ((UV)ch != pch) { |
9041c2e3 |
232 | d = uvchr_to_utf8(d, ch); |
eb160463 |
233 | pch = (UV)ch; |
1aa99e6b |
234 | } |
235 | s += len; |
236 | continue; |
237 | } |
9b877dbb |
238 | else if (ch == -1) { /* -1 is unmapped character */ |
239 | Copy(s, d, len, U8); |
240 | d += len; |
241 | } |
1aa99e6b |
242 | else if (ch == -2) /* -2 is delete character */ |
243 | matches++; |
244 | s += len; |
245 | pch = 0xfeedface; |
246 | } |
5d06d08e |
247 | } |
1aa99e6b |
248 | else { |
249 | while (s < send) { |
250 | STRLEN len; |
9041c2e3 |
251 | UV comp = utf8_to_uvchr(s, &len); |
8973db79 |
252 | if (comp > 0xff) { |
253 | if (!complement) { |
254 | Copy(s, d, len, U8); |
255 | d += len; |
256 | } |
257 | else { |
258 | matches++; |
259 | if (!del) { |
9041c2e3 |
260 | if (comp - 0x100 < rlen) |
261 | d = uvchr_to_utf8(d, tbl[comp+1]); |
8973db79 |
262 | else |
9041c2e3 |
263 | d = uvchr_to_utf8(d, tbl[0x100+rlen]); |
8973db79 |
264 | } |
265 | } |
9b877dbb |
266 | } |
1aa99e6b |
267 | else if ((ch = tbl[comp]) >= 0) { |
9041c2e3 |
268 | d = uvchr_to_utf8(d, ch); |
1aa99e6b |
269 | matches++; |
270 | } |
271 | else if (ch == -1) { /* -1 is unmapped character */ |
9b877dbb |
272 | Copy(s, d, len, U8); |
273 | d += len; |
1aa99e6b |
274 | } |
275 | else if (ch == -2) /* -2 is delete character */ |
276 | matches++; |
277 | s += len; |
278 | } |
279 | } |
9b877dbb |
280 | if (grows) { |
281 | sv_setpvn(sv, (char*)dstart, d - dstart); |
282 | Safefree(dstart); |
283 | } |
284 | else { |
285 | *d = '\0'; |
286 | SvCUR_set(sv, d - dstart); |
287 | } |
1aa99e6b |
288 | SvUTF8_on(sv); |
4757a243 |
289 | } |
5e44153e |
290 | SvSETMAGIC(sv); |
4757a243 |
291 | return matches; |
292 | } |
293 | |
942e002e |
294 | STATIC I32 |
21304a3b |
295 | S_do_trans_simple_utf8(pTHX_ SV *sv) |
4757a243 |
296 | { |
4757a243 |
297 | U8 *s; |
298 | U8 *send; |
299 | U8 *d; |
036b4402 |
300 | U8 *start; |
1aa99e6b |
301 | U8 *dstart, *dend; |
4757a243 |
302 | I32 matches = 0; |
9b877dbb |
303 | I32 grows = PL_op->op_private & OPpTRANS_GROWS; |
4757a243 |
304 | STRLEN len; |
305 | |
306 | SV* rv = (SV*)cSVOP->op_sv; |
307 | HV* hv = (HV*)SvRV(rv); |
308 | SV** svp = hv_fetch(hv, "NONE", 4, FALSE); |
309 | UV none = svp ? SvUV(*svp) : 0x7fffffff; |
310 | UV extra = none + 1; |
b7953727 |
311 | UV final = 0; |
4757a243 |
312 | UV uv; |
1aa99e6b |
313 | I32 isutf8; |
314 | U8 hibit = 0; |
4757a243 |
315 | |
316 | s = (U8*)SvPV(sv, len); |
1aa99e6b |
317 | isutf8 = SvUTF8(sv); |
318 | if (!isutf8) { |
319 | U8 *t = s, *e = s + len; |
c4d5f83a |
320 | while (t < e) { |
321 | U8 ch = *t++; |
322 | if ((hibit = !NATIVE_IS_INVARIANT(ch))) |
1aa99e6b |
323 | break; |
c4d5f83a |
324 | } |
1aa99e6b |
325 | if (hibit) |
326 | s = bytes_to_utf8(s, &len); |
327 | } |
4757a243 |
328 | send = s + len; |
036b4402 |
329 | start = s; |
4757a243 |
330 | |
331 | svp = hv_fetch(hv, "FINAL", 5, FALSE); |
332 | if (svp) |
333 | final = SvUV(*svp); |
334 | |
9b877dbb |
335 | if (grows) { |
336 | /* d needs to be bigger than s, in case e.g. upgrading is required */ |
337 | New(0, d, len*3+UTF8_MAXLEN, U8); |
338 | dend = d + len * 3; |
339 | dstart = d; |
340 | } |
341 | else { |
342 | dstart = d = s; |
343 | dend = d + len; |
344 | } |
1aa99e6b |
345 | |
4757a243 |
346 | while (s < send) { |
3568d838 |
347 | if ((uv = swash_fetch(rv, s, TRUE)) < none) { |
4757a243 |
348 | s += UTF8SKIP(s); |
349 | matches++; |
7d85a32c |
350 | d = uvuni_to_utf8(d, uv); |
4757a243 |
351 | } |
352 | else if (uv == none) { |
1aa99e6b |
353 | int i = UTF8SKIP(s); |
9b877dbb |
354 | Copy(s, d, i, U8); |
355 | d += i; |
356 | s += i; |
4757a243 |
357 | } |
358 | else if (uv == extra) { |
1aa99e6b |
359 | int i = UTF8SKIP(s); |
036b4402 |
360 | s += i; |
4757a243 |
361 | matches++; |
7d85a32c |
362 | d = uvuni_to_utf8(d, final); |
4757a243 |
363 | } |
364 | else |
365 | s += UTF8SKIP(s); |
1aa99e6b |
366 | |
9b877dbb |
367 | if (d > dend) { |
1aa99e6b |
368 | STRLEN clen = d - dstart; |
369 | STRLEN nlen = dend - dstart + len + UTF8_MAXLEN; |
9b877dbb |
370 | if (!grows) |
7d85a32c |
371 | Perl_croak(aTHX_ "panic: do_trans_simple_utf8 line %d",__LINE__); |
1aa99e6b |
372 | Renew(dstart, nlen+UTF8_MAXLEN, U8); |
373 | d = dstart + clen; |
374 | dend = dstart + nlen; |
375 | } |
4757a243 |
376 | } |
16ec844d |
377 | if (grows || hibit) { |
9b877dbb |
378 | sv_setpvn(sv, (char*)dstart, d - dstart); |
379 | Safefree(dstart); |
16ec844d |
380 | if (grows && hibit) |
381 | Safefree(start); |
9b877dbb |
382 | } |
383 | else { |
384 | *d = '\0'; |
385 | SvCUR_set(sv, d - dstart); |
386 | } |
4757a243 |
387 | SvSETMAGIC(sv); |
1aa99e6b |
388 | SvUTF8_on(sv); |
4757a243 |
389 | |
390 | return matches; |
391 | } |
392 | |
942e002e |
393 | STATIC I32 |
21304a3b |
394 | S_do_trans_count_utf8(pTHX_ SV *sv) |
4757a243 |
395 | { |
4757a243 |
396 | U8 *s; |
b7953727 |
397 | U8 *start = 0, *send; |
4757a243 |
398 | I32 matches = 0; |
399 | STRLEN len; |
400 | |
401 | SV* rv = (SV*)cSVOP->op_sv; |
402 | HV* hv = (HV*)SvRV(rv); |
403 | SV** svp = hv_fetch(hv, "NONE", 4, FALSE); |
404 | UV none = svp ? SvUV(*svp) : 0x7fffffff; |
45005bfb |
405 | UV extra = none + 1; |
4757a243 |
406 | UV uv; |
1aa99e6b |
407 | U8 hibit = 0; |
4757a243 |
408 | |
409 | s = (U8*)SvPV(sv, len); |
1aa99e6b |
410 | if (!SvUTF8(sv)) { |
411 | U8 *t = s, *e = s + len; |
c4d5f83a |
412 | while (t < e) { |
413 | U8 ch = *t++; |
414 | if ((hibit = !NATIVE_IS_INVARIANT(ch))) |
1aa99e6b |
415 | break; |
c4d5f83a |
416 | } |
1aa99e6b |
417 | if (hibit) |
418 | start = s = bytes_to_utf8(s, &len); |
419 | } |
4757a243 |
420 | send = s + len; |
421 | |
422 | while (s < send) { |
3568d838 |
423 | if ((uv = swash_fetch(rv, s, TRUE)) < none || uv == extra) |
4757a243 |
424 | matches++; |
834a4ddd |
425 | s += UTF8SKIP(s); |
4757a243 |
426 | } |
1aa99e6b |
427 | if (hibit) |
428 | Safefree(start); |
4757a243 |
429 | |
430 | return matches; |
431 | } |
432 | |
942e002e |
433 | STATIC I32 |
21304a3b |
434 | S_do_trans_complex_utf8(pTHX_ SV *sv) |
4757a243 |
435 | { |
4757a243 |
436 | U8 *s; |
1aa99e6b |
437 | U8 *start, *send; |
4757a243 |
438 | U8 *d; |
439 | I32 matches = 0; |
440 | I32 squash = PL_op->op_private & OPpTRANS_SQUASH; |
4757a243 |
441 | I32 del = PL_op->op_private & OPpTRANS_DELETE; |
9b877dbb |
442 | I32 grows = PL_op->op_private & OPpTRANS_GROWS; |
4757a243 |
443 | SV* rv = (SV*)cSVOP->op_sv; |
444 | HV* hv = (HV*)SvRV(rv); |
445 | SV** svp = hv_fetch(hv, "NONE", 4, FALSE); |
446 | UV none = svp ? SvUV(*svp) : 0x7fffffff; |
447 | UV extra = none + 1; |
b7953727 |
448 | UV final = 0; |
45005bfb |
449 | bool havefinal = FALSE; |
4757a243 |
450 | UV uv; |
451 | STRLEN len; |
1aa99e6b |
452 | U8 *dstart, *dend; |
453 | I32 isutf8; |
454 | U8 hibit = 0; |
4757a243 |
455 | |
456 | s = (U8*)SvPV(sv, len); |
1aa99e6b |
457 | isutf8 = SvUTF8(sv); |
458 | if (!isutf8) { |
459 | U8 *t = s, *e = s + len; |
c4d5f83a |
460 | while (t < e) { |
461 | U8 ch = *t++; |
462 | if ((hibit = !NATIVE_IS_INVARIANT(ch))) |
1aa99e6b |
463 | break; |
c4d5f83a |
464 | } |
1aa99e6b |
465 | if (hibit) |
466 | s = bytes_to_utf8(s, &len); |
467 | } |
4757a243 |
468 | send = s + len; |
1aa99e6b |
469 | start = s; |
4757a243 |
470 | |
471 | svp = hv_fetch(hv, "FINAL", 5, FALSE); |
45005bfb |
472 | if (svp) { |
4757a243 |
473 | final = SvUV(*svp); |
45005bfb |
474 | havefinal = TRUE; |
475 | } |
4757a243 |
476 | |
9b877dbb |
477 | if (grows) { |
478 | /* d needs to be bigger than s, in case e.g. upgrading is required */ |
479 | New(0, d, len*3+UTF8_MAXLEN, U8); |
480 | dend = d + len * 3; |
481 | dstart = d; |
482 | } |
483 | else { |
484 | dstart = d = s; |
485 | dend = d + len; |
486 | } |
4757a243 |
487 | |
488 | if (squash) { |
489 | UV puv = 0xfeedface; |
490 | while (s < send) { |
3568d838 |
491 | uv = swash_fetch(rv, s, TRUE); |
9041c2e3 |
492 | |
9b877dbb |
493 | if (d > dend) { |
494 | STRLEN clen = d - dstart; |
495 | STRLEN nlen = dend - dstart + len + UTF8_MAXLEN; |
496 | if (!grows) |
7d85a32c |
497 | Perl_croak(aTHX_ "panic: do_trans_complex_utf8 line %d",__LINE__); |
1aa99e6b |
498 | Renew(dstart, nlen+UTF8_MAXLEN, U8); |
499 | d = dstart + clen; |
500 | dend = dstart + nlen; |
4757a243 |
501 | } |
502 | if (uv < none) { |
503 | matches++; |
94472101 |
504 | s += UTF8SKIP(s); |
4757a243 |
505 | if (uv != puv) { |
7d85a32c |
506 | d = uvuni_to_utf8(d, uv); |
4757a243 |
507 | puv = uv; |
508 | } |
4757a243 |
509 | continue; |
510 | } |
511 | else if (uv == none) { /* "none" is unmapped character */ |
1aa99e6b |
512 | int i = UTF8SKIP(s); |
9b877dbb |
513 | Copy(s, d, i, U8); |
514 | d += i; |
515 | s += i; |
4757a243 |
516 | puv = 0xfeedface; |
517 | continue; |
a0ed51b3 |
518 | } |
4757a243 |
519 | else if (uv == extra && !del) { |
520 | matches++; |
45005bfb |
521 | if (havefinal) { |
522 | s += UTF8SKIP(s); |
523 | if (puv != final) { |
7d85a32c |
524 | d = uvuni_to_utf8(d, final); |
45005bfb |
525 | puv = final; |
526 | } |
527 | } |
528 | else { |
529 | STRLEN len; |
7d85a32c |
530 | uv = utf8_to_uvuni(s, &len); |
45005bfb |
531 | if (uv != puv) { |
532 | Copy(s, d, len, U8); |
533 | d += len; |
534 | puv = uv; |
535 | } |
536 | s += len; |
4757a243 |
537 | } |
4757a243 |
538 | continue; |
539 | } |
01ec43d0 |
540 | matches++; /* "none+1" is delete character */ |
541 | s += UTF8SKIP(s); |
a0ed51b3 |
542 | } |
79072805 |
543 | } |
544 | else { |
4757a243 |
545 | while (s < send) { |
3568d838 |
546 | uv = swash_fetch(rv, s, TRUE); |
9b877dbb |
547 | if (d > dend) { |
548 | STRLEN clen = d - dstart; |
549 | STRLEN nlen = dend - dstart + len + UTF8_MAXLEN; |
550 | if (!grows) |
7d85a32c |
551 | Perl_croak(aTHX_ "panic: do_trans_complex_utf8 line %d",__LINE__); |
1aa99e6b |
552 | Renew(dstart, nlen+UTF8_MAXLEN, U8); |
553 | d = dstart + clen; |
554 | dend = dstart + nlen; |
a0ed51b3 |
555 | } |
4757a243 |
556 | if (uv < none) { |
557 | matches++; |
01ec43d0 |
558 | s += UTF8SKIP(s); |
7d85a32c |
559 | d = uvuni_to_utf8(d, uv); |
4757a243 |
560 | continue; |
a0ed51b3 |
561 | } |
4757a243 |
562 | else if (uv == none) { /* "none" is unmapped character */ |
1aa99e6b |
563 | int i = UTF8SKIP(s); |
9b877dbb |
564 | Copy(s, d, i, U8); |
565 | d += i; |
566 | s += i; |
4757a243 |
567 | continue; |
79072805 |
568 | } |
4757a243 |
569 | else if (uv == extra && !del) { |
570 | matches++; |
01ec43d0 |
571 | s += UTF8SKIP(s); |
7d85a32c |
572 | d = uvuni_to_utf8(d, final); |
4757a243 |
573 | continue; |
574 | } |
01ec43d0 |
575 | matches++; /* "none+1" is delete character */ |
576 | s += UTF8SKIP(s); |
79072805 |
577 | } |
4757a243 |
578 | } |
16ec844d |
579 | if (grows || hibit) { |
9b877dbb |
580 | sv_setpvn(sv, (char*)dstart, d - dstart); |
581 | Safefree(dstart); |
16ec844d |
582 | if (grows && hibit) |
583 | Safefree(start); |
9b877dbb |
584 | } |
585 | else { |
586 | *d = '\0'; |
587 | SvCUR_set(sv, d - dstart); |
588 | } |
1aa99e6b |
589 | SvUTF8_on(sv); |
4757a243 |
590 | SvSETMAGIC(sv); |
591 | |
592 | return matches; |
593 | } |
594 | |
595 | I32 |
864dbfa3 |
596 | Perl_do_trans(pTHX_ SV *sv) |
4757a243 |
597 | { |
598 | STRLEN len; |
8e84507e |
599 | I32 hasutf = (PL_op->op_private & |
036b4402 |
600 | (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)); |
4757a243 |
601 | |
2233f375 |
602 | if (SvREADONLY(sv)) { |
765f542d |
603 | if (SvIsCOW(sv)) |
604 | sv_force_normal_flags(sv, 0); |
2233f375 |
605 | if (SvREADONLY(sv) && !(PL_op->op_private & OPpTRANS_IDENTICAL)) |
606 | Perl_croak(aTHX_ PL_no_modify); |
607 | } |
4757a243 |
608 | (void)SvPV(sv, len); |
609 | if (!len) |
610 | return 0; |
d59e14db |
611 | if (!(PL_op->op_private & OPpTRANS_IDENTICAL)) { |
612 | if (!SvPOKp(sv)) |
613 | (void)SvPV_force(sv, len); |
2de7b02f |
614 | (void)SvPOK_only_UTF8(sv); |
d59e14db |
615 | } |
4757a243 |
616 | |
cea2e8a9 |
617 | DEBUG_t( Perl_deb(aTHX_ "2.TBL\n")); |
4757a243 |
618 | |
036b4402 |
619 | switch (PL_op->op_private & ~hasutf & 63) { |
4757a243 |
620 | case 0: |
01ec43d0 |
621 | if (hasutf) |
622 | return do_trans_simple_utf8(sv); |
623 | else |
624 | return do_trans_simple(sv); |
4757a243 |
625 | |
626 | case OPpTRANS_IDENTICAL: |
05d340b8 |
627 | case OPpTRANS_IDENTICAL|OPpTRANS_COMPLEMENT: |
01ec43d0 |
628 | if (hasutf) |
629 | return do_trans_count_utf8(sv); |
630 | else |
631 | return do_trans_count(sv); |
4757a243 |
632 | |
633 | default: |
01ec43d0 |
634 | if (hasutf) |
036b4402 |
635 | return do_trans_complex_utf8(sv); |
4757a243 |
636 | else |
036b4402 |
637 | return do_trans_complex(sv); |
79072805 |
638 | } |
79072805 |
639 | } |
640 | |
641 | void |
864dbfa3 |
642 | Perl_do_join(pTHX_ register SV *sv, SV *del, register SV **mark, register SV **sp) |
79072805 |
643 | { |
644 | SV **oldmark = mark; |
645 | register I32 items = sp - mark; |
79072805 |
646 | register STRLEN len; |
463ee0b2 |
647 | STRLEN delimlen; |
463ee0b2 |
648 | STRLEN tmplen; |
79072805 |
649 | |
5ba99574 |
650 | (void) SvPV(del, delimlen); /* stringify and get the delimlen */ |
651 | /* SvCUR assumes it's SvPOK() and woe betide you if it's not. */ |
516a5887 |
652 | |
79072805 |
653 | mark++; |
654 | len = (items > 0 ? (delimlen * (items - 1) ) : 0); |
07f14f54 |
655 | (void)SvUPGRADE(sv, SVt_PV); |
79072805 |
656 | if (SvLEN(sv) < len + items) { /* current length is way too short */ |
657 | while (items-- > 0) { |
1426bbf4 |
658 | if (*mark && !SvGAMAGIC(*mark) && SvOK(*mark)) { |
463ee0b2 |
659 | SvPV(*mark, tmplen); |
660 | len += tmplen; |
79072805 |
661 | } |
662 | mark++; |
663 | } |
664 | SvGROW(sv, len + 1); /* so try to pre-extend */ |
665 | |
666 | mark = oldmark; |
db7c17d7 |
667 | items = sp - mark; |
79072805 |
668 | ++mark; |
669 | } |
670 | |
8d6d96c1 |
671 | sv_setpv(sv, ""); |
672 | if (PL_tainting && SvMAGICAL(sv)) |
673 | SvTAINTED_off(sv); |
674 | |
463ee0b2 |
675 | if (items-- > 0) { |
92d29cee |
676 | if (*mark) |
677 | sv_catsv(sv, *mark); |
463ee0b2 |
678 | mark++; |
679 | } |
8d6d96c1 |
680 | |
c512ce4f |
681 | if (delimlen) { |
79072805 |
682 | for (; items > 0; items--,mark++) { |
c512ce4f |
683 | sv_catsv(sv,del); |
79072805 |
684 | sv_catsv(sv,*mark); |
685 | } |
686 | } |
687 | else { |
688 | for (; items > 0; items--,mark++) |
689 | sv_catsv(sv,*mark); |
690 | } |
691 | SvSETMAGIC(sv); |
692 | } |
693 | |
694 | void |
864dbfa3 |
695 | Perl_do_sprintf(pTHX_ SV *sv, I32 len, SV **sarg) |
79072805 |
696 | { |
46fc3d4c |
697 | STRLEN patlen; |
698 | char *pat = SvPV(*sarg, patlen); |
699 | bool do_taint = FALSE; |
700 | |
5b781b5b |
701 | SvUTF8_off(sv); |
2cf2cfc6 |
702 | if (DO_UTF8(*sarg)) |
703 | SvUTF8_on(sv); |
46fc3d4c |
704 | sv_vsetpvfn(sv, pat, patlen, Null(va_list*), sarg + 1, len - 1, &do_taint); |
79072805 |
705 | SvSETMAGIC(sv); |
46fc3d4c |
706 | if (do_taint) |
707 | SvTAINTED_on(sv); |
79072805 |
708 | } |
709 | |
33b45480 |
710 | /* currently converts input to bytes if possible, but doesn't sweat failure */ |
81e118e0 |
711 | UV |
712 | Perl_do_vecget(pTHX_ SV *sv, I32 offset, I32 size) |
713 | { |
714 | STRLEN srclen, len; |
715 | unsigned char *s = (unsigned char *) SvPV(sv, srclen); |
716 | UV retnum = 0; |
717 | |
a50d7633 |
718 | if (offset < 0) |
81e118e0 |
719 | return retnum; |
8e84507e |
720 | if (size < 1 || (size & (size-1))) /* size < 1 or not a power of two */ |
a50d7633 |
721 | Perl_croak(aTHX_ "Illegal number of bits in vec"); |
246fae53 |
722 | |
dcad2880 |
723 | if (SvUTF8(sv)) |
33b45480 |
724 | (void) Perl_sv_utf8_downgrade(aTHX_ sv, TRUE); |
246fae53 |
725 | |
81e118e0 |
726 | offset *= size; /* turn into bit offset */ |
727 | len = (offset + size + 7) / 8; /* required number of bytes */ |
728 | if (len > srclen) { |
729 | if (size <= 8) |
730 | retnum = 0; |
731 | else { |
732 | offset >>= 3; /* turn into byte offset */ |
733 | if (size == 16) { |
eb160463 |
734 | if ((STRLEN)offset >= srclen) |
81e118e0 |
735 | retnum = 0; |
736 | else |
628e1a40 |
737 | retnum = (UV) s[offset] << 8; |
81e118e0 |
738 | } |
739 | else if (size == 32) { |
eb160463 |
740 | if ((STRLEN)offset >= srclen) |
81e118e0 |
741 | retnum = 0; |
eb160463 |
742 | else if ((STRLEN)(offset + 1) >= srclen) |
81e118e0 |
743 | retnum = |
744 | ((UV) s[offset ] << 24); |
eb160463 |
745 | else if ((STRLEN)(offset + 2) >= srclen) |
81e118e0 |
746 | retnum = |
747 | ((UV) s[offset ] << 24) + |
748 | ((UV) s[offset + 1] << 16); |
749 | else |
750 | retnum = |
751 | ((UV) s[offset ] << 24) + |
752 | ((UV) s[offset + 1] << 16) + |
753 | ( s[offset + 2] << 8); |
754 | } |
d7d93a81 |
755 | #ifdef UV_IS_QUAD |
c5a0f51a |
756 | else if (size == 64) { |
c5a0f51a |
757 | if (ckWARN(WARN_PORTABLE)) |
9014280d |
758 | Perl_warner(aTHX_ packWARN(WARN_PORTABLE), |
c5a0f51a |
759 | "Bit vector size > 32 non-portable"); |
760 | if (offset >= srclen) |
761 | retnum = 0; |
762 | else if (offset + 1 >= srclen) |
763 | retnum = |
764 | (UV) s[offset ] << 56; |
765 | else if (offset + 2 >= srclen) |
766 | retnum = |
767 | ((UV) s[offset ] << 56) + |
768 | ((UV) s[offset + 1] << 48); |
769 | else if (offset + 3 >= srclen) |
770 | retnum = |
771 | ((UV) s[offset ] << 56) + |
772 | ((UV) s[offset + 1] << 48) + |
773 | ((UV) s[offset + 2] << 40); |
774 | else if (offset + 4 >= srclen) |
775 | retnum = |
776 | ((UV) s[offset ] << 56) + |
777 | ((UV) s[offset + 1] << 48) + |
778 | ((UV) s[offset + 2] << 40) + |
779 | ((UV) s[offset + 3] << 32); |
780 | else if (offset + 5 >= srclen) |
781 | retnum = |
782 | ((UV) s[offset ] << 56) + |
783 | ((UV) s[offset + 1] << 48) + |
784 | ((UV) s[offset + 2] << 40) + |
785 | ((UV) s[offset + 3] << 32) + |
786 | ( s[offset + 4] << 24); |
787 | else if (offset + 6 >= srclen) |
788 | retnum = |
789 | ((UV) s[offset ] << 56) + |
790 | ((UV) s[offset + 1] << 48) + |
791 | ((UV) s[offset + 2] << 40) + |
792 | ((UV) s[offset + 3] << 32) + |
793 | ((UV) s[offset + 4] << 24) + |
794 | ((UV) s[offset + 5] << 16); |
795 | else |
8e84507e |
796 | retnum = |
c5a0f51a |
797 | ((UV) s[offset ] << 56) + |
798 | ((UV) s[offset + 1] << 48) + |
799 | ((UV) s[offset + 2] << 40) + |
800 | ((UV) s[offset + 3] << 32) + |
801 | ((UV) s[offset + 4] << 24) + |
802 | ((UV) s[offset + 5] << 16) + |
628e1a40 |
803 | ( s[offset + 6] << 8); |
c5a0f51a |
804 | } |
805 | #endif |
81e118e0 |
806 | } |
807 | } |
808 | else if (size < 8) |
809 | retnum = (s[offset >> 3] >> (offset & 7)) & ((1 << size) - 1); |
810 | else { |
811 | offset >>= 3; /* turn into byte offset */ |
812 | if (size == 8) |
813 | retnum = s[offset]; |
814 | else if (size == 16) |
815 | retnum = |
628e1a40 |
816 | ((UV) s[offset] << 8) + |
81e118e0 |
817 | s[offset + 1]; |
818 | else if (size == 32) |
819 | retnum = |
820 | ((UV) s[offset ] << 24) + |
821 | ((UV) s[offset + 1] << 16) + |
822 | ( s[offset + 2] << 8) + |
823 | s[offset + 3]; |
d7d93a81 |
824 | #ifdef UV_IS_QUAD |
c5a0f51a |
825 | else if (size == 64) { |
c5a0f51a |
826 | if (ckWARN(WARN_PORTABLE)) |
9014280d |
827 | Perl_warner(aTHX_ packWARN(WARN_PORTABLE), |
c5a0f51a |
828 | "Bit vector size > 32 non-portable"); |
829 | retnum = |
830 | ((UV) s[offset ] << 56) + |
831 | ((UV) s[offset + 1] << 48) + |
832 | ((UV) s[offset + 2] << 40) + |
833 | ((UV) s[offset + 3] << 32) + |
834 | ((UV) s[offset + 4] << 24) + |
835 | ((UV) s[offset + 5] << 16) + |
628e1a40 |
836 | ( s[offset + 6] << 8) + |
c5a0f51a |
837 | s[offset + 7]; |
838 | } |
839 | #endif |
81e118e0 |
840 | } |
841 | |
842 | return retnum; |
843 | } |
844 | |
33b45480 |
845 | /* currently converts input to bytes if possible but doesn't sweat failures, |
846 | * although it does ensure that the string it clobbers is not marked as |
847 | * utf8-valid any more |
848 | */ |
79072805 |
849 | void |
864dbfa3 |
850 | Perl_do_vecset(pTHX_ SV *sv) |
79072805 |
851 | { |
852 | SV *targ = LvTARG(sv); |
853 | register I32 offset; |
854 | register I32 size; |
8990e307 |
855 | register unsigned char *s; |
81e118e0 |
856 | register UV lval; |
79072805 |
857 | I32 mask; |
a0d0e21e |
858 | STRLEN targlen; |
859 | STRLEN len; |
79072805 |
860 | |
8990e307 |
861 | if (!targ) |
862 | return; |
a0d0e21e |
863 | s = (unsigned char*)SvPV_force(targ, targlen); |
246fae53 |
864 | if (SvUTF8(targ)) { |
33b45480 |
865 | /* This is handled by the SvPOK_only below... |
866 | if (!Perl_sv_utf8_downgrade(aTHX_ targ, TRUE)) |
867 | SvUTF8_off(targ); |
868 | */ |
869 | (void) Perl_sv_utf8_downgrade(aTHX_ targ, TRUE); |
246fae53 |
870 | } |
871 | |
4ebbc975 |
872 | (void)SvPOK_only(targ); |
81e118e0 |
873 | lval = SvUV(sv); |
79072805 |
874 | offset = LvTARGOFF(sv); |
fe58ced6 |
875 | if (offset < 0) |
ed9aa3b7 |
876 | Perl_croak(aTHX_ "Negative offset to vec in lvalue context"); |
79072805 |
877 | size = LvTARGLEN(sv); |
8e84507e |
878 | if (size < 1 || (size & (size-1))) /* size < 1 or not a power of two */ |
a50d7633 |
879 | Perl_croak(aTHX_ "Illegal number of bits in vec"); |
8e84507e |
880 | |
81e118e0 |
881 | offset *= size; /* turn into bit offset */ |
882 | len = (offset + size + 7) / 8; /* required number of bytes */ |
a0d0e21e |
883 | if (len > targlen) { |
884 | s = (unsigned char*)SvGROW(targ, len + 1); |
12ae5dfc |
885 | (void)memzero((char *)(s + targlen), len - targlen + 1); |
a0d0e21e |
886 | SvCUR_set(targ, len); |
887 | } |
8e84507e |
888 | |
79072805 |
889 | if (size < 8) { |
890 | mask = (1 << size) - 1; |
891 | size = offset & 7; |
892 | lval &= mask; |
81e118e0 |
893 | offset >>= 3; /* turn into byte offset */ |
79072805 |
894 | s[offset] &= ~(mask << size); |
895 | s[offset] |= lval << size; |
896 | } |
897 | else { |
81e118e0 |
898 | offset >>= 3; /* turn into byte offset */ |
79072805 |
899 | if (size == 8) |
eb160463 |
900 | s[offset ] = (U8)( lval & 0xff); |
79072805 |
901 | else if (size == 16) { |
eb160463 |
902 | s[offset ] = (U8)((lval >> 8) & 0xff); |
903 | s[offset+1] = (U8)( lval & 0xff); |
79072805 |
904 | } |
905 | else if (size == 32) { |
eb160463 |
906 | s[offset ] = (U8)((lval >> 24) & 0xff); |
907 | s[offset+1] = (U8)((lval >> 16) & 0xff); |
908 | s[offset+2] = (U8)((lval >> 8) & 0xff); |
909 | s[offset+3] = (U8)( lval & 0xff); |
c5a0f51a |
910 | } |
d7d93a81 |
911 | #ifdef UV_IS_QUAD |
c5a0f51a |
912 | else if (size == 64) { |
c5a0f51a |
913 | if (ckWARN(WARN_PORTABLE)) |
9014280d |
914 | Perl_warner(aTHX_ packWARN(WARN_PORTABLE), |
c5a0f51a |
915 | "Bit vector size > 32 non-portable"); |
eb160463 |
916 | s[offset ] = (U8)((lval >> 56) & 0xff); |
917 | s[offset+1] = (U8)((lval >> 48) & 0xff); |
918 | s[offset+2] = (U8)((lval >> 40) & 0xff); |
919 | s[offset+3] = (U8)((lval >> 32) & 0xff); |
920 | s[offset+4] = (U8)((lval >> 24) & 0xff); |
921 | s[offset+5] = (U8)((lval >> 16) & 0xff); |
922 | s[offset+6] = (U8)((lval >> 8) & 0xff); |
923 | s[offset+7] = (U8)( lval & 0xff); |
79072805 |
924 | } |
dc1e3f56 |
925 | #endif |
79072805 |
926 | } |
7bb043c3 |
927 | SvSETMAGIC(targ); |
79072805 |
928 | } |
929 | |
930 | void |
864dbfa3 |
931 | Perl_do_chop(pTHX_ register SV *astr, register SV *sv) |
79072805 |
932 | { |
463ee0b2 |
933 | STRLEN len; |
a0d0e21e |
934 | char *s; |
8e84507e |
935 | |
79072805 |
936 | if (SvTYPE(sv) == SVt_PVAV) { |
a0d0e21e |
937 | register I32 i; |
938 | I32 max; |
939 | AV* av = (AV*)sv; |
940 | max = AvFILL(av); |
941 | for (i = 0; i <= max; i++) { |
942 | sv = (SV*)av_fetch(av, i, FALSE); |
3280af22 |
943 | if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef)) |
a0d0e21e |
944 | do_chop(astr, sv); |
945 | } |
946 | return; |
79072805 |
947 | } |
aa854799 |
948 | else if (SvTYPE(sv) == SVt_PVHV) { |
a0d0e21e |
949 | HV* hv = (HV*)sv; |
950 | HE* entry; |
951 | (void)hv_iterinit(hv); |
952 | /*SUPPRESS 560*/ |
155aba94 |
953 | while ((entry = hv_iternext(hv))) |
a0d0e21e |
954 | do_chop(astr,hv_iterval(hv,entry)); |
955 | return; |
79072805 |
956 | } |
991e6d41 |
957 | else if (SvREADONLY(sv)) { |
958 | if (SvFAKE(sv)) { |
959 | /* SV is copy-on-write */ |
960 | sv_force_normal_flags(sv, 0); |
961 | } |
962 | if (SvREADONLY(sv)) |
963 | Perl_croak(aTHX_ PL_no_modify); |
964 | } |
a0d0e21e |
965 | s = SvPV(sv, len); |
748a9306 |
966 | if (len && !SvPOK(sv)) |
a0d0e21e |
967 | s = SvPV_force(sv, len); |
7e2040f0 |
968 | if (DO_UTF8(sv)) { |
a0ed51b3 |
969 | if (s && len) { |
970 | char *send = s + len; |
971 | char *start = s; |
972 | s = send - 1; |
a0dbb045 |
973 | while (s > start && UTF8_IS_CONTINUATION(*s)) |
974 | s--; |
9041c2e3 |
975 | if (utf8_to_uvchr((U8*)s, 0)) { |
a0dbb045 |
976 | sv_setpvn(astr, s, send - s); |
977 | *s = '\0'; |
978 | SvCUR_set(sv, s - start); |
979 | SvNIOK_off(sv); |
980 | SvUTF8_on(astr); |
981 | } |
a0ed51b3 |
982 | } |
983 | else |
984 | sv_setpvn(astr, "", 0); |
985 | } |
7e2040f0 |
986 | else if (s && len) { |
a0d0e21e |
987 | s += --len; |
988 | sv_setpvn(astr, s, 1); |
989 | *s = '\0'; |
990 | SvCUR_set(sv, len); |
2c19a612 |
991 | SvUTF8_off(sv); |
a0d0e21e |
992 | SvNIOK_off(sv); |
79072805 |
993 | } |
994 | else |
a0d0e21e |
995 | sv_setpvn(astr, "", 0); |
996 | SvSETMAGIC(sv); |
7e2040f0 |
997 | } |
a0d0e21e |
998 | |
999 | I32 |
864dbfa3 |
1000 | Perl_do_chomp(pTHX_ register SV *sv) |
a0d0e21e |
1001 | { |
c07a80fd |
1002 | register I32 count; |
a0d0e21e |
1003 | STRLEN len; |
9b33ce3b |
1004 | STRLEN n_a; |
a0d0e21e |
1005 | char *s; |
c07a80fd |
1006 | |
3280af22 |
1007 | if (RsSNARF(PL_rs)) |
c07a80fd |
1008 | return 0; |
4c5a6083 |
1009 | if (RsRECORD(PL_rs)) |
1010 | return 0; |
c07a80fd |
1011 | count = 0; |
a0d0e21e |
1012 | if (SvTYPE(sv) == SVt_PVAV) { |
1013 | register I32 i; |
1014 | I32 max; |
1015 | AV* av = (AV*)sv; |
1016 | max = AvFILL(av); |
1017 | for (i = 0; i <= max; i++) { |
1018 | sv = (SV*)av_fetch(av, i, FALSE); |
3280af22 |
1019 | if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef)) |
a0d0e21e |
1020 | count += do_chomp(sv); |
1021 | } |
1022 | return count; |
1023 | } |
aa854799 |
1024 | else if (SvTYPE(sv) == SVt_PVHV) { |
a0d0e21e |
1025 | HV* hv = (HV*)sv; |
1026 | HE* entry; |
1027 | (void)hv_iterinit(hv); |
1028 | /*SUPPRESS 560*/ |
155aba94 |
1029 | while ((entry = hv_iternext(hv))) |
a0d0e21e |
1030 | count += do_chomp(hv_iterval(hv,entry)); |
1031 | return count; |
1032 | } |
991e6d41 |
1033 | else if (SvREADONLY(sv)) { |
1034 | if (SvFAKE(sv)) { |
1035 | /* SV is copy-on-write */ |
1036 | sv_force_normal_flags(sv, 0); |
1037 | } |
1038 | if (SvREADONLY(sv)) |
1039 | Perl_croak(aTHX_ PL_no_modify); |
1040 | } |
a0d0e21e |
1041 | s = SvPV(sv, len); |
a0d0e21e |
1042 | if (s && len) { |
1043 | s += --len; |
3280af22 |
1044 | if (RsPARA(PL_rs)) { |
a0d0e21e |
1045 | if (*s != '\n') |
1046 | goto nope; |
1047 | ++count; |
1048 | while (len && s[-1] == '\n') { |
1049 | --len; |
1050 | --s; |
1051 | ++count; |
1052 | } |
1053 | } |
a0d0e21e |
1054 | else { |
c07a80fd |
1055 | STRLEN rslen; |
3280af22 |
1056 | char *rsptr = SvPV(PL_rs, rslen); |
c07a80fd |
1057 | if (rslen == 1) { |
1058 | if (*s != *rsptr) |
1059 | goto nope; |
1060 | ++count; |
1061 | } |
1062 | else { |
8c2cee6f |
1063 | if (len < rslen - 1) |
c07a80fd |
1064 | goto nope; |
1065 | len -= rslen - 1; |
1066 | s -= rslen - 1; |
36477c24 |
1067 | if (memNE(s, rsptr, rslen)) |
c07a80fd |
1068 | goto nope; |
1069 | count += rslen; |
1070 | } |
a0d0e21e |
1071 | } |
9b33ce3b |
1072 | s = SvPV_force(sv, n_a); |
a0d0e21e |
1073 | SvCUR_set(sv, len); |
9b33ce3b |
1074 | *SvEND(sv) = '\0'; |
a0d0e21e |
1075 | SvNIOK_off(sv); |
9b33ce3b |
1076 | SvSETMAGIC(sv); |
a0d0e21e |
1077 | } |
1078 | nope: |
a0d0e21e |
1079 | return count; |
8e84507e |
1080 | } |
79072805 |
1081 | |
1082 | void |
864dbfa3 |
1083 | Perl_do_vop(pTHX_ I32 optype, SV *sv, SV *left, SV *right) |
79072805 |
1084 | { |
1085 | #ifdef LIBERAL |
1086 | register long *dl; |
1087 | register long *ll; |
1088 | register long *rl; |
1089 | #endif |
1090 | register char *dc; |
463ee0b2 |
1091 | STRLEN leftlen; |
1092 | STRLEN rightlen; |
7a4c00b4 |
1093 | register char *lc; |
1094 | register char *rc; |
79072805 |
1095 | register I32 len; |
a0d0e21e |
1096 | I32 lensave; |
7a4c00b4 |
1097 | char *lsave; |
1098 | char *rsave; |
0c57e439 |
1099 | bool left_utf = DO_UTF8(left); |
1100 | bool right_utf = DO_UTF8(right); |
b7953727 |
1101 | I32 needlen = 0; |
0c57e439 |
1102 | |
1103 | if (left_utf && !right_utf) |
1104 | sv_utf8_upgrade(right); |
a1ca4561 |
1105 | else if (!left_utf && right_utf) |
0c57e439 |
1106 | sv_utf8_upgrade(left); |
79072805 |
1107 | |
1fbd88dc |
1108 | if (sv != left || (optype != OP_BIT_AND && !SvOK(sv) && !SvGMAGICAL(sv))) |
1109 | sv_setpvn(sv, "", 0); /* avoid undef warning on |= and ^= */ |
7a4c00b4 |
1110 | lsave = lc = SvPV(left, leftlen); |
1111 | rsave = rc = SvPV(right, rightlen); |
93a17b20 |
1112 | len = leftlen < rightlen ? leftlen : rightlen; |
a0d0e21e |
1113 | lensave = len; |
c9b3c8d0 |
1114 | if ((left_utf || right_utf) && (sv == left || sv == right)) { |
1115 | needlen = optype == OP_BIT_AND ? len : leftlen + rightlen; |
1116 | Newz(801, dc, needlen + 1, char); |
1117 | } |
1118 | else if (SvOK(sv) || SvTYPE(sv) > SVt_PVMG) { |
2d8e6c8d |
1119 | STRLEN n_a; |
1120 | dc = SvPV_force(sv, n_a); |
eb160463 |
1121 | if (SvCUR(sv) < (STRLEN)len) { |
1122 | dc = SvGROW(sv, (STRLEN)(len + 1)); |
ff68c719 |
1123 | (void)memzero(dc + SvCUR(sv), len - SvCUR(sv) + 1); |
1124 | } |
c9b3c8d0 |
1125 | if (optype != OP_BIT_AND && (left_utf || right_utf)) |
1126 | dc = SvGROW(sv, leftlen + rightlen + 1); |
ff68c719 |
1127 | } |
1128 | else { |
c9b3c8d0 |
1129 | needlen = ((optype == OP_BIT_AND) |
1130 | ? len : (leftlen > rightlen ? leftlen : rightlen)); |
ff68c719 |
1131 | Newz(801, dc, needlen + 1, char); |
1132 | (void)sv_usepvn(sv, dc, needlen); |
1133 | dc = SvPVX(sv); /* sv_usepvn() calls Renew() */ |
79072805 |
1134 | } |
a0d0e21e |
1135 | SvCUR_set(sv, len); |
1136 | (void)SvPOK_only(sv); |
0c57e439 |
1137 | if (left_utf || right_utf) { |
1138 | UV duc, luc, ruc; |
c9b3c8d0 |
1139 | char *dcsave = dc; |
0c57e439 |
1140 | STRLEN lulen = leftlen; |
1141 | STRLEN rulen = rightlen; |
ba210ebe |
1142 | STRLEN ulen; |
0c57e439 |
1143 | |
1144 | switch (optype) { |
1145 | case OP_BIT_AND: |
1146 | while (lulen && rulen) { |
9041c2e3 |
1147 | luc = utf8n_to_uvchr((U8*)lc, lulen, &ulen, UTF8_ALLOW_ANYUV); |
0c57e439 |
1148 | lc += ulen; |
1149 | lulen -= ulen; |
9041c2e3 |
1150 | ruc = utf8n_to_uvchr((U8*)rc, rulen, &ulen, UTF8_ALLOW_ANYUV); |
0c57e439 |
1151 | rc += ulen; |
1152 | rulen -= ulen; |
1153 | duc = luc & ruc; |
9041c2e3 |
1154 | dc = (char*)uvchr_to_utf8((U8*)dc, duc); |
0c57e439 |
1155 | } |
c9b3c8d0 |
1156 | if (sv == left || sv == right) |
1157 | (void)sv_usepvn(sv, dcsave, needlen); |
1158 | SvCUR_set(sv, dc - dcsave); |
0c57e439 |
1159 | break; |
1160 | case OP_BIT_XOR: |
1161 | while (lulen && rulen) { |
9041c2e3 |
1162 | luc = utf8n_to_uvchr((U8*)lc, lulen, &ulen, UTF8_ALLOW_ANYUV); |
0c57e439 |
1163 | lc += ulen; |
1164 | lulen -= ulen; |
9041c2e3 |
1165 | ruc = utf8n_to_uvchr((U8*)rc, rulen, &ulen, UTF8_ALLOW_ANYUV); |
0c57e439 |
1166 | rc += ulen; |
1167 | rulen -= ulen; |
1168 | duc = luc ^ ruc; |
9041c2e3 |
1169 | dc = (char*)uvchr_to_utf8((U8*)dc, duc); |
0c57e439 |
1170 | } |
1171 | goto mop_up_utf; |
1172 | case OP_BIT_OR: |
1173 | while (lulen && rulen) { |
9041c2e3 |
1174 | luc = utf8n_to_uvchr((U8*)lc, lulen, &ulen, UTF8_ALLOW_ANYUV); |
0c57e439 |
1175 | lc += ulen; |
1176 | lulen -= ulen; |
9041c2e3 |
1177 | ruc = utf8n_to_uvchr((U8*)rc, rulen, &ulen, UTF8_ALLOW_ANYUV); |
0c57e439 |
1178 | rc += ulen; |
1179 | rulen -= ulen; |
1180 | duc = luc | ruc; |
9041c2e3 |
1181 | dc = (char*)uvchr_to_utf8((U8*)dc, duc); |
0c57e439 |
1182 | } |
1183 | mop_up_utf: |
c9b3c8d0 |
1184 | if (sv == left || sv == right) |
1185 | (void)sv_usepvn(sv, dcsave, needlen); |
1186 | SvCUR_set(sv, dc - dcsave); |
0c57e439 |
1187 | if (rulen) |
1188 | sv_catpvn(sv, rc, rulen); |
1189 | else if (lulen) |
1190 | sv_catpvn(sv, lc, lulen); |
1191 | else |
1192 | *SvEND(sv) = '\0'; |
1193 | break; |
1194 | } |
1195 | SvUTF8_on(sv); |
1196 | goto finish; |
1197 | } |
1198 | else |
79072805 |
1199 | #ifdef LIBERAL |
1200 | if (len >= sizeof(long)*4 && |
1201 | !((long)dc % sizeof(long)) && |
1202 | !((long)lc % sizeof(long)) && |
1203 | !((long)rc % sizeof(long))) /* It's almost always aligned... */ |
1204 | { |
1205 | I32 remainder = len % (sizeof(long)*4); |
1206 | len /= (sizeof(long)*4); |
1207 | |
1208 | dl = (long*)dc; |
1209 | ll = (long*)lc; |
1210 | rl = (long*)rc; |
1211 | |
1212 | switch (optype) { |
1213 | case OP_BIT_AND: |
1214 | while (len--) { |
1215 | *dl++ = *ll++ & *rl++; |
1216 | *dl++ = *ll++ & *rl++; |
1217 | *dl++ = *ll++ & *rl++; |
1218 | *dl++ = *ll++ & *rl++; |
1219 | } |
1220 | break; |
a0d0e21e |
1221 | case OP_BIT_XOR: |
79072805 |
1222 | while (len--) { |
1223 | *dl++ = *ll++ ^ *rl++; |
1224 | *dl++ = *ll++ ^ *rl++; |
1225 | *dl++ = *ll++ ^ *rl++; |
1226 | *dl++ = *ll++ ^ *rl++; |
1227 | } |
1228 | break; |
1229 | case OP_BIT_OR: |
1230 | while (len--) { |
1231 | *dl++ = *ll++ | *rl++; |
1232 | *dl++ = *ll++ | *rl++; |
1233 | *dl++ = *ll++ | *rl++; |
1234 | *dl++ = *ll++ | *rl++; |
1235 | } |
1236 | } |
1237 | |
1238 | dc = (char*)dl; |
1239 | lc = (char*)ll; |
1240 | rc = (char*)rl; |
1241 | |
1242 | len = remainder; |
1243 | } |
1244 | #endif |
a0d0e21e |
1245 | { |
a0d0e21e |
1246 | switch (optype) { |
1247 | case OP_BIT_AND: |
1248 | while (len--) |
1249 | *dc++ = *lc++ & *rc++; |
1250 | break; |
1251 | case OP_BIT_XOR: |
1252 | while (len--) |
1253 | *dc++ = *lc++ ^ *rc++; |
1254 | goto mop_up; |
1255 | case OP_BIT_OR: |
1256 | while (len--) |
1257 | *dc++ = *lc++ | *rc++; |
1258 | mop_up: |
1259 | len = lensave; |
eb160463 |
1260 | if (rightlen > (STRLEN)len) |
a0d0e21e |
1261 | sv_catpvn(sv, rsave + len, rightlen - len); |
eb160463 |
1262 | else if (leftlen > (STRLEN)len) |
a0d0e21e |
1263 | sv_catpvn(sv, lsave + len, leftlen - len); |
4633a7c4 |
1264 | else |
1265 | *SvEND(sv) = '\0'; |
a0d0e21e |
1266 | break; |
1267 | } |
79072805 |
1268 | } |
0c57e439 |
1269 | finish: |
fb73857a |
1270 | SvTAINT(sv); |
79072805 |
1271 | } |
463ee0b2 |
1272 | |
1273 | OP * |
cea2e8a9 |
1274 | Perl_do_kv(pTHX) |
463ee0b2 |
1275 | { |
39644a26 |
1276 | dSP; |
463ee0b2 |
1277 | HV *hv = (HV*)POPs; |
800e9ae0 |
1278 | HV *keys; |
463ee0b2 |
1279 | register HE *entry; |
463ee0b2 |
1280 | SV *tmpstr; |
54310121 |
1281 | I32 gimme = GIMME_V; |
533c011a |
1282 | I32 dokeys = (PL_op->op_type == OP_KEYS); |
1283 | I32 dovalues = (PL_op->op_type == OP_VALUES); |
8e84507e |
1284 | |
1285 | if (PL_op->op_type == OP_RV2HV || PL_op->op_type == OP_PADHV) |
a0d0e21e |
1286 | dokeys = dovalues = TRUE; |
463ee0b2 |
1287 | |
85581909 |
1288 | if (!hv) { |
78f9721b |
1289 | if (PL_op->op_flags & OPf_MOD || LVRET) { /* lvalue */ |
85581909 |
1290 | dTARGET; /* make sure to clear its target here */ |
1291 | if (SvTYPE(TARG) == SVt_PVLV) |
1292 | LvTARG(TARG) = Nullsv; |
1293 | PUSHs(TARG); |
1294 | } |
463ee0b2 |
1295 | RETURN; |
85581909 |
1296 | } |
748a9306 |
1297 | |
6d822dc4 |
1298 | keys = hv; |
800e9ae0 |
1299 | (void)hv_iterinit(keys); /* always reset iterator regardless */ |
748a9306 |
1300 | |
54310121 |
1301 | if (gimme == G_VOID) |
aa689395 |
1302 | RETURN; |
1303 | |
54310121 |
1304 | if (gimme == G_SCALAR) { |
6ee623d5 |
1305 | IV i; |
463ee0b2 |
1306 | dTARGET; |
1307 | |
78f9721b |
1308 | if (PL_op->op_flags & OPf_MOD || LVRET) { /* lvalue */ |
85581909 |
1309 | if (SvTYPE(TARG) < SVt_PVLV) { |
1310 | sv_upgrade(TARG, SVt_PVLV); |
14befaf4 |
1311 | sv_magic(TARG, Nullsv, PERL_MAGIC_nkeys, Nullch, 0); |
85581909 |
1312 | } |
1313 | LvTYPE(TARG) = 'k'; |
800e9ae0 |
1314 | if (LvTARG(TARG) != (SV*)keys) { |
6ff81951 |
1315 | if (LvTARG(TARG)) |
1316 | SvREFCNT_dec(LvTARG(TARG)); |
800e9ae0 |
1317 | LvTARG(TARG) = SvREFCNT_inc(keys); |
6ff81951 |
1318 | } |
85581909 |
1319 | PUSHs(TARG); |
1320 | RETURN; |
1321 | } |
1322 | |
14befaf4 |
1323 | if (! SvTIED_mg((SV*)keys, PERL_MAGIC_tied)) |
a98face1 |
1324 | i = HvKEYS(keys); |
463ee0b2 |
1325 | else { |
1326 | i = 0; |
463ee0b2 |
1327 | /*SUPPRESS 560*/ |
800e9ae0 |
1328 | while (hv_iternext(keys)) i++; |
463ee0b2 |
1329 | } |
1330 | PUSHi( i ); |
1331 | RETURN; |
1332 | } |
1333 | |
8ed4b672 |
1334 | EXTEND(SP, HvKEYS(keys) * (dokeys + dovalues)); |
463ee0b2 |
1335 | |
463ee0b2 |
1336 | PUTBACK; /* hv_iternext and hv_iterval might clobber stack_sp */ |
155aba94 |
1337 | while ((entry = hv_iternext(keys))) { |
463ee0b2 |
1338 | SPAGAIN; |
574c8022 |
1339 | if (dokeys) { |
1340 | SV* sv = hv_iterkeysv(entry); |
574c8022 |
1341 | XPUSHs(sv); /* won't clobber stack_sp */ |
1342 | } |
463ee0b2 |
1343 | if (dovalues) { |
463ee0b2 |
1344 | PUTBACK; |
6d822dc4 |
1345 | tmpstr = hv_iterval(hv,entry); |
cea2e8a9 |
1346 | DEBUG_H(Perl_sv_setpvf(aTHX_ tmpstr, "%lu%%%d=%lu", |
46fc3d4c |
1347 | (unsigned long)HeHASH(entry), |
800e9ae0 |
1348 | HvMAX(keys)+1, |
1349 | (unsigned long)(HeHASH(entry) & HvMAX(keys)))); |
463ee0b2 |
1350 | SPAGAIN; |
46fc3d4c |
1351 | XPUSHs(tmpstr); |
463ee0b2 |
1352 | } |
1353 | PUTBACK; |
1354 | } |
1355 | return NORMAL; |
1356 | } |
4e35701f |
1357 | |