Commit | Line | Data |
a0d0e21e |
1 | /* doop.c |
79072805 |
2 | * |
bc89e66f |
3 | * Copyright (c) 1991-2001, 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++; |
49 | *s++ = ch; |
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 |
036b4402 |
95 | S_do_trans_count(pTHX_ SV *sv)/* SPC - OK */ |
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 |
b250498f |
133 | S_do_trans_complex(pTHX_ SV *sv)/* SPC - NOT OK */ |
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) { |
036b4402 |
162 | *d = ch; |
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++; |
178 | *d++ = ch; |
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]; |
220 | if (ch != pch) { |
9041c2e3 |
221 | d = uvchr_to_utf8(d, ch); |
8973db79 |
222 | pch = ch; |
223 | } |
224 | s += len; |
225 | continue; |
226 | } |
227 | } |
9b877dbb |
228 | } |
1aa99e6b |
229 | else if ((ch = tbl[comp]) >= 0) { |
230 | matches++; |
231 | if (ch != pch) { |
9041c2e3 |
232 | d = uvchr_to_utf8(d, ch); |
1aa99e6b |
233 | pch = ch; |
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 |
036b4402 |
295 | S_do_trans_simple_utf8(pTHX_ SV *sv)/* SPC - OK */ |
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); |
9041c2e3 |
389 | /* Downgrading just 'cos it will is suspect - NI-S */ |
1aa99e6b |
390 | if (!isutf8 && !(PL_hints & HINT_UTF8)) |
391 | sv_utf8_downgrade(sv, TRUE); |
4757a243 |
392 | |
393 | return matches; |
394 | } |
395 | |
942e002e |
396 | STATIC I32 |
036b4402 |
397 | S_do_trans_count_utf8(pTHX_ SV *sv)/* SPC - OK */ |
4757a243 |
398 | { |
4757a243 |
399 | U8 *s; |
b7953727 |
400 | U8 *start = 0, *send; |
4757a243 |
401 | I32 matches = 0; |
402 | STRLEN len; |
403 | |
404 | SV* rv = (SV*)cSVOP->op_sv; |
405 | HV* hv = (HV*)SvRV(rv); |
406 | SV** svp = hv_fetch(hv, "NONE", 4, FALSE); |
407 | UV none = svp ? SvUV(*svp) : 0x7fffffff; |
45005bfb |
408 | UV extra = none + 1; |
4757a243 |
409 | UV uv; |
1aa99e6b |
410 | U8 hibit = 0; |
4757a243 |
411 | |
412 | s = (U8*)SvPV(sv, len); |
1aa99e6b |
413 | if (!SvUTF8(sv)) { |
414 | U8 *t = s, *e = s + len; |
c4d5f83a |
415 | while (t < e) { |
416 | U8 ch = *t++; |
417 | if ((hibit = !NATIVE_IS_INVARIANT(ch))) |
1aa99e6b |
418 | break; |
c4d5f83a |
419 | } |
1aa99e6b |
420 | if (hibit) |
421 | start = s = bytes_to_utf8(s, &len); |
422 | } |
4757a243 |
423 | send = s + len; |
424 | |
425 | while (s < send) { |
3568d838 |
426 | if ((uv = swash_fetch(rv, s, TRUE)) < none || uv == extra) |
4757a243 |
427 | matches++; |
834a4ddd |
428 | s += UTF8SKIP(s); |
4757a243 |
429 | } |
1aa99e6b |
430 | if (hibit) |
431 | Safefree(start); |
4757a243 |
432 | |
433 | return matches; |
434 | } |
435 | |
942e002e |
436 | STATIC I32 |
036b4402 |
437 | S_do_trans_complex_utf8(pTHX_ SV *sv) /* SPC - NOT OK */ |
4757a243 |
438 | { |
4757a243 |
439 | U8 *s; |
1aa99e6b |
440 | U8 *start, *send; |
4757a243 |
441 | U8 *d; |
442 | I32 matches = 0; |
443 | I32 squash = PL_op->op_private & OPpTRANS_SQUASH; |
4757a243 |
444 | I32 del = PL_op->op_private & OPpTRANS_DELETE; |
9b877dbb |
445 | I32 grows = PL_op->op_private & OPpTRANS_GROWS; |
4757a243 |
446 | SV* rv = (SV*)cSVOP->op_sv; |
447 | HV* hv = (HV*)SvRV(rv); |
448 | SV** svp = hv_fetch(hv, "NONE", 4, FALSE); |
449 | UV none = svp ? SvUV(*svp) : 0x7fffffff; |
450 | UV extra = none + 1; |
b7953727 |
451 | UV final = 0; |
45005bfb |
452 | bool havefinal = FALSE; |
4757a243 |
453 | UV uv; |
454 | STRLEN len; |
1aa99e6b |
455 | U8 *dstart, *dend; |
456 | I32 isutf8; |
457 | U8 hibit = 0; |
4757a243 |
458 | |
459 | s = (U8*)SvPV(sv, len); |
1aa99e6b |
460 | isutf8 = SvUTF8(sv); |
461 | if (!isutf8) { |
462 | U8 *t = s, *e = s + len; |
c4d5f83a |
463 | while (t < e) { |
464 | U8 ch = *t++; |
465 | if ((hibit = !NATIVE_IS_INVARIANT(ch))) |
1aa99e6b |
466 | break; |
c4d5f83a |
467 | } |
1aa99e6b |
468 | if (hibit) |
469 | s = bytes_to_utf8(s, &len); |
470 | } |
4757a243 |
471 | send = s + len; |
1aa99e6b |
472 | start = s; |
4757a243 |
473 | |
474 | svp = hv_fetch(hv, "FINAL", 5, FALSE); |
45005bfb |
475 | if (svp) { |
4757a243 |
476 | final = SvUV(*svp); |
45005bfb |
477 | havefinal = TRUE; |
478 | } |
4757a243 |
479 | |
9b877dbb |
480 | if (grows) { |
481 | /* d needs to be bigger than s, in case e.g. upgrading is required */ |
482 | New(0, d, len*3+UTF8_MAXLEN, U8); |
483 | dend = d + len * 3; |
484 | dstart = d; |
485 | } |
486 | else { |
487 | dstart = d = s; |
488 | dend = d + len; |
489 | } |
4757a243 |
490 | |
491 | if (squash) { |
492 | UV puv = 0xfeedface; |
493 | while (s < send) { |
3568d838 |
494 | uv = swash_fetch(rv, s, TRUE); |
9041c2e3 |
495 | |
9b877dbb |
496 | if (d > dend) { |
497 | STRLEN clen = d - dstart; |
498 | STRLEN nlen = dend - dstart + len + UTF8_MAXLEN; |
499 | if (!grows) |
7d85a32c |
500 | Perl_croak(aTHX_ "panic: do_trans_complex_utf8 line %d",__LINE__); |
1aa99e6b |
501 | Renew(dstart, nlen+UTF8_MAXLEN, U8); |
502 | d = dstart + clen; |
503 | dend = dstart + nlen; |
4757a243 |
504 | } |
505 | if (uv < none) { |
506 | matches++; |
94472101 |
507 | s += UTF8SKIP(s); |
4757a243 |
508 | if (uv != puv) { |
7d85a32c |
509 | d = uvuni_to_utf8(d, uv); |
4757a243 |
510 | puv = uv; |
511 | } |
4757a243 |
512 | continue; |
513 | } |
514 | else if (uv == none) { /* "none" is unmapped character */ |
1aa99e6b |
515 | int i = UTF8SKIP(s); |
9b877dbb |
516 | Copy(s, d, i, U8); |
517 | d += i; |
518 | s += i; |
4757a243 |
519 | puv = 0xfeedface; |
520 | continue; |
a0ed51b3 |
521 | } |
4757a243 |
522 | else if (uv == extra && !del) { |
523 | matches++; |
45005bfb |
524 | if (havefinal) { |
525 | s += UTF8SKIP(s); |
526 | if (puv != final) { |
7d85a32c |
527 | d = uvuni_to_utf8(d, final); |
45005bfb |
528 | puv = final; |
529 | } |
530 | } |
531 | else { |
532 | STRLEN len; |
7d85a32c |
533 | uv = utf8_to_uvuni(s, &len); |
45005bfb |
534 | if (uv != puv) { |
535 | Copy(s, d, len, U8); |
536 | d += len; |
537 | puv = uv; |
538 | } |
539 | s += len; |
4757a243 |
540 | } |
4757a243 |
541 | continue; |
542 | } |
01ec43d0 |
543 | matches++; /* "none+1" is delete character */ |
544 | s += UTF8SKIP(s); |
a0ed51b3 |
545 | } |
79072805 |
546 | } |
547 | else { |
4757a243 |
548 | while (s < send) { |
3568d838 |
549 | uv = swash_fetch(rv, s, TRUE); |
9b877dbb |
550 | if (d > dend) { |
551 | STRLEN clen = d - dstart; |
552 | STRLEN nlen = dend - dstart + len + UTF8_MAXLEN; |
553 | if (!grows) |
7d85a32c |
554 | Perl_croak(aTHX_ "panic: do_trans_complex_utf8 line %d",__LINE__); |
1aa99e6b |
555 | Renew(dstart, nlen+UTF8_MAXLEN, U8); |
556 | d = dstart + clen; |
557 | dend = dstart + nlen; |
a0ed51b3 |
558 | } |
4757a243 |
559 | if (uv < none) { |
560 | matches++; |
01ec43d0 |
561 | s += UTF8SKIP(s); |
7d85a32c |
562 | d = uvuni_to_utf8(d, uv); |
4757a243 |
563 | continue; |
a0ed51b3 |
564 | } |
4757a243 |
565 | else if (uv == none) { /* "none" is unmapped character */ |
1aa99e6b |
566 | int i = UTF8SKIP(s); |
9b877dbb |
567 | Copy(s, d, i, U8); |
568 | d += i; |
569 | s += i; |
4757a243 |
570 | continue; |
79072805 |
571 | } |
4757a243 |
572 | else if (uv == extra && !del) { |
573 | matches++; |
01ec43d0 |
574 | s += UTF8SKIP(s); |
7d85a32c |
575 | d = uvuni_to_utf8(d, final); |
4757a243 |
576 | continue; |
577 | } |
01ec43d0 |
578 | matches++; /* "none+1" is delete character */ |
579 | s += UTF8SKIP(s); |
79072805 |
580 | } |
4757a243 |
581 | } |
16ec844d |
582 | if (grows || hibit) { |
9b877dbb |
583 | sv_setpvn(sv, (char*)dstart, d - dstart); |
584 | Safefree(dstart); |
16ec844d |
585 | if (grows && hibit) |
586 | Safefree(start); |
9b877dbb |
587 | } |
588 | else { |
589 | *d = '\0'; |
590 | SvCUR_set(sv, d - dstart); |
591 | } |
1aa99e6b |
592 | SvUTF8_on(sv); |
1aa99e6b |
593 | if (!isutf8 && !(PL_hints & HINT_UTF8)) |
594 | sv_utf8_downgrade(sv, TRUE); |
4757a243 |
595 | SvSETMAGIC(sv); |
596 | |
597 | return matches; |
598 | } |
599 | |
600 | I32 |
864dbfa3 |
601 | Perl_do_trans(pTHX_ SV *sv) |
4757a243 |
602 | { |
603 | STRLEN len; |
8e84507e |
604 | I32 hasutf = (PL_op->op_private & |
036b4402 |
605 | (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)); |
4757a243 |
606 | |
607 | if (SvREADONLY(sv) && !(PL_op->op_private & OPpTRANS_IDENTICAL)) |
cea2e8a9 |
608 | Perl_croak(aTHX_ PL_no_modify); |
4757a243 |
609 | |
610 | (void)SvPV(sv, len); |
611 | if (!len) |
612 | return 0; |
613 | if (!SvPOKp(sv)) |
614 | (void)SvPV_force(sv, len); |
2de7b02f |
615 | if (!(PL_op->op_private & OPpTRANS_IDENTICAL)) |
616 | (void)SvPOK_only_UTF8(sv); |
4757a243 |
617 | |
cea2e8a9 |
618 | DEBUG_t( Perl_deb(aTHX_ "2.TBL\n")); |
4757a243 |
619 | |
036b4402 |
620 | switch (PL_op->op_private & ~hasutf & 63) { |
4757a243 |
621 | case 0: |
01ec43d0 |
622 | if (hasutf) |
623 | return do_trans_simple_utf8(sv); |
624 | else |
625 | return do_trans_simple(sv); |
4757a243 |
626 | |
627 | case OPpTRANS_IDENTICAL: |
05d340b8 |
628 | case OPpTRANS_IDENTICAL|OPpTRANS_COMPLEMENT: |
01ec43d0 |
629 | if (hasutf) |
630 | return do_trans_count_utf8(sv); |
631 | else |
632 | return do_trans_count(sv); |
4757a243 |
633 | |
634 | default: |
01ec43d0 |
635 | if (hasutf) |
036b4402 |
636 | return do_trans_complex_utf8(sv); |
4757a243 |
637 | else |
036b4402 |
638 | return do_trans_complex(sv); |
79072805 |
639 | } |
79072805 |
640 | } |
641 | |
642 | void |
864dbfa3 |
643 | Perl_do_join(pTHX_ register SV *sv, SV *del, register SV **mark, register SV **sp) |
79072805 |
644 | { |
645 | SV **oldmark = mark; |
646 | register I32 items = sp - mark; |
79072805 |
647 | register STRLEN len; |
463ee0b2 |
648 | STRLEN delimlen; |
463ee0b2 |
649 | STRLEN tmplen; |
79072805 |
650 | |
5ba99574 |
651 | (void) SvPV(del, delimlen); /* stringify and get the delimlen */ |
652 | /* SvCUR assumes it's SvPOK() and woe betide you if it's not. */ |
516a5887 |
653 | |
79072805 |
654 | mark++; |
655 | len = (items > 0 ? (delimlen * (items - 1) ) : 0); |
07f14f54 |
656 | (void)SvUPGRADE(sv, SVt_PV); |
79072805 |
657 | if (SvLEN(sv) < len + items) { /* current length is way too short */ |
658 | while (items-- > 0) { |
1426bbf4 |
659 | if (*mark && !SvGAMAGIC(*mark) && SvOK(*mark)) { |
463ee0b2 |
660 | SvPV(*mark, tmplen); |
661 | len += tmplen; |
79072805 |
662 | } |
663 | mark++; |
664 | } |
665 | SvGROW(sv, len + 1); /* so try to pre-extend */ |
666 | |
667 | mark = oldmark; |
db7c17d7 |
668 | items = sp - mark; |
79072805 |
669 | ++mark; |
670 | } |
671 | |
8d6d96c1 |
672 | sv_setpv(sv, ""); |
673 | if (PL_tainting && SvMAGICAL(sv)) |
674 | SvTAINTED_off(sv); |
675 | |
463ee0b2 |
676 | if (items-- > 0) { |
92d29cee |
677 | if (*mark) |
678 | sv_catsv(sv, *mark); |
463ee0b2 |
679 | mark++; |
680 | } |
8d6d96c1 |
681 | |
c512ce4f |
682 | if (delimlen) { |
79072805 |
683 | for (; items > 0; items--,mark++) { |
c512ce4f |
684 | sv_catsv(sv,del); |
79072805 |
685 | sv_catsv(sv,*mark); |
686 | } |
687 | } |
688 | else { |
689 | for (; items > 0; items--,mark++) |
690 | sv_catsv(sv,*mark); |
691 | } |
692 | SvSETMAGIC(sv); |
693 | } |
694 | |
695 | void |
864dbfa3 |
696 | Perl_do_sprintf(pTHX_ SV *sv, I32 len, SV **sarg) |
79072805 |
697 | { |
46fc3d4c |
698 | STRLEN patlen; |
699 | char *pat = SvPV(*sarg, patlen); |
700 | bool do_taint = FALSE; |
701 | |
702 | sv_vsetpvfn(sv, pat, patlen, Null(va_list*), sarg + 1, len - 1, &do_taint); |
79072805 |
703 | SvSETMAGIC(sv); |
46fc3d4c |
704 | if (do_taint) |
705 | SvTAINTED_on(sv); |
79072805 |
706 | } |
707 | |
33b45480 |
708 | /* currently converts input to bytes if possible, but doesn't sweat failure */ |
81e118e0 |
709 | UV |
710 | Perl_do_vecget(pTHX_ SV *sv, I32 offset, I32 size) |
711 | { |
712 | STRLEN srclen, len; |
713 | unsigned char *s = (unsigned char *) SvPV(sv, srclen); |
714 | UV retnum = 0; |
715 | |
a50d7633 |
716 | if (offset < 0) |
81e118e0 |
717 | return retnum; |
8e84507e |
718 | if (size < 1 || (size & (size-1))) /* size < 1 or not a power of two */ |
a50d7633 |
719 | Perl_croak(aTHX_ "Illegal number of bits in vec"); |
246fae53 |
720 | |
dcad2880 |
721 | if (SvUTF8(sv)) |
33b45480 |
722 | (void) Perl_sv_utf8_downgrade(aTHX_ sv, TRUE); |
246fae53 |
723 | |
81e118e0 |
724 | offset *= size; /* turn into bit offset */ |
725 | len = (offset + size + 7) / 8; /* required number of bytes */ |
726 | if (len > srclen) { |
727 | if (size <= 8) |
728 | retnum = 0; |
729 | else { |
730 | offset >>= 3; /* turn into byte offset */ |
731 | if (size == 16) { |
732 | if (offset >= srclen) |
733 | retnum = 0; |
734 | else |
628e1a40 |
735 | retnum = (UV) s[offset] << 8; |
81e118e0 |
736 | } |
737 | else if (size == 32) { |
738 | if (offset >= srclen) |
739 | retnum = 0; |
740 | else if (offset + 1 >= srclen) |
741 | retnum = |
742 | ((UV) s[offset ] << 24); |
743 | else if (offset + 2 >= srclen) |
744 | retnum = |
745 | ((UV) s[offset ] << 24) + |
746 | ((UV) s[offset + 1] << 16); |
747 | else |
748 | retnum = |
749 | ((UV) s[offset ] << 24) + |
750 | ((UV) s[offset + 1] << 16) + |
751 | ( s[offset + 2] << 8); |
752 | } |
d7d93a81 |
753 | #ifdef UV_IS_QUAD |
c5a0f51a |
754 | else if (size == 64) { |
c5a0f51a |
755 | if (ckWARN(WARN_PORTABLE)) |
756 | Perl_warner(aTHX_ WARN_PORTABLE, |
757 | "Bit vector size > 32 non-portable"); |
758 | if (offset >= srclen) |
759 | retnum = 0; |
760 | else if (offset + 1 >= srclen) |
761 | retnum = |
762 | (UV) s[offset ] << 56; |
763 | else if (offset + 2 >= srclen) |
764 | retnum = |
765 | ((UV) s[offset ] << 56) + |
766 | ((UV) s[offset + 1] << 48); |
767 | else if (offset + 3 >= srclen) |
768 | retnum = |
769 | ((UV) s[offset ] << 56) + |
770 | ((UV) s[offset + 1] << 48) + |
771 | ((UV) s[offset + 2] << 40); |
772 | else if (offset + 4 >= srclen) |
773 | retnum = |
774 | ((UV) s[offset ] << 56) + |
775 | ((UV) s[offset + 1] << 48) + |
776 | ((UV) s[offset + 2] << 40) + |
777 | ((UV) s[offset + 3] << 32); |
778 | else if (offset + 5 >= srclen) |
779 | retnum = |
780 | ((UV) s[offset ] << 56) + |
781 | ((UV) s[offset + 1] << 48) + |
782 | ((UV) s[offset + 2] << 40) + |
783 | ((UV) s[offset + 3] << 32) + |
784 | ( s[offset + 4] << 24); |
785 | else if (offset + 6 >= srclen) |
786 | retnum = |
787 | ((UV) s[offset ] << 56) + |
788 | ((UV) s[offset + 1] << 48) + |
789 | ((UV) s[offset + 2] << 40) + |
790 | ((UV) s[offset + 3] << 32) + |
791 | ((UV) s[offset + 4] << 24) + |
792 | ((UV) s[offset + 5] << 16); |
793 | else |
8e84507e |
794 | retnum = |
c5a0f51a |
795 | ((UV) s[offset ] << 56) + |
796 | ((UV) s[offset + 1] << 48) + |
797 | ((UV) s[offset + 2] << 40) + |
798 | ((UV) s[offset + 3] << 32) + |
799 | ((UV) s[offset + 4] << 24) + |
800 | ((UV) s[offset + 5] << 16) + |
628e1a40 |
801 | ( s[offset + 6] << 8); |
c5a0f51a |
802 | } |
803 | #endif |
81e118e0 |
804 | } |
805 | } |
806 | else if (size < 8) |
807 | retnum = (s[offset >> 3] >> (offset & 7)) & ((1 << size) - 1); |
808 | else { |
809 | offset >>= 3; /* turn into byte offset */ |
810 | if (size == 8) |
811 | retnum = s[offset]; |
812 | else if (size == 16) |
813 | retnum = |
628e1a40 |
814 | ((UV) s[offset] << 8) + |
81e118e0 |
815 | s[offset + 1]; |
816 | else if (size == 32) |
817 | retnum = |
818 | ((UV) s[offset ] << 24) + |
819 | ((UV) s[offset + 1] << 16) + |
820 | ( s[offset + 2] << 8) + |
821 | s[offset + 3]; |
d7d93a81 |
822 | #ifdef UV_IS_QUAD |
c5a0f51a |
823 | else if (size == 64) { |
c5a0f51a |
824 | if (ckWARN(WARN_PORTABLE)) |
825 | Perl_warner(aTHX_ WARN_PORTABLE, |
826 | "Bit vector size > 32 non-portable"); |
827 | retnum = |
828 | ((UV) s[offset ] << 56) + |
829 | ((UV) s[offset + 1] << 48) + |
830 | ((UV) s[offset + 2] << 40) + |
831 | ((UV) s[offset + 3] << 32) + |
832 | ((UV) s[offset + 4] << 24) + |
833 | ((UV) s[offset + 5] << 16) + |
628e1a40 |
834 | ( s[offset + 6] << 8) + |
c5a0f51a |
835 | s[offset + 7]; |
836 | } |
837 | #endif |
81e118e0 |
838 | } |
839 | |
840 | return retnum; |
841 | } |
842 | |
33b45480 |
843 | /* currently converts input to bytes if possible but doesn't sweat failures, |
844 | * although it does ensure that the string it clobbers is not marked as |
845 | * utf8-valid any more |
846 | */ |
79072805 |
847 | void |
864dbfa3 |
848 | Perl_do_vecset(pTHX_ SV *sv) |
79072805 |
849 | { |
850 | SV *targ = LvTARG(sv); |
851 | register I32 offset; |
852 | register I32 size; |
8990e307 |
853 | register unsigned char *s; |
81e118e0 |
854 | register UV lval; |
79072805 |
855 | I32 mask; |
a0d0e21e |
856 | STRLEN targlen; |
857 | STRLEN len; |
79072805 |
858 | |
8990e307 |
859 | if (!targ) |
860 | return; |
a0d0e21e |
861 | s = (unsigned char*)SvPV_force(targ, targlen); |
246fae53 |
862 | if (SvUTF8(targ)) { |
33b45480 |
863 | /* This is handled by the SvPOK_only below... |
864 | if (!Perl_sv_utf8_downgrade(aTHX_ targ, TRUE)) |
865 | SvUTF8_off(targ); |
866 | */ |
867 | (void) Perl_sv_utf8_downgrade(aTHX_ targ, TRUE); |
246fae53 |
868 | } |
869 | |
4ebbc975 |
870 | (void)SvPOK_only(targ); |
81e118e0 |
871 | lval = SvUV(sv); |
79072805 |
872 | offset = LvTARGOFF(sv); |
fe58ced6 |
873 | if (offset < 0) |
874 | Perl_croak(aTHX_ "Assigning to negative offset in vec"); |
79072805 |
875 | size = LvTARGLEN(sv); |
8e84507e |
876 | if (size < 1 || (size & (size-1))) /* size < 1 or not a power of two */ |
a50d7633 |
877 | Perl_croak(aTHX_ "Illegal number of bits in vec"); |
8e84507e |
878 | |
81e118e0 |
879 | offset *= size; /* turn into bit offset */ |
880 | len = (offset + size + 7) / 8; /* required number of bytes */ |
a0d0e21e |
881 | if (len > targlen) { |
882 | s = (unsigned char*)SvGROW(targ, len + 1); |
12ae5dfc |
883 | (void)memzero((char *)(s + targlen), len - targlen + 1); |
a0d0e21e |
884 | SvCUR_set(targ, len); |
885 | } |
8e84507e |
886 | |
79072805 |
887 | if (size < 8) { |
888 | mask = (1 << size) - 1; |
889 | size = offset & 7; |
890 | lval &= mask; |
81e118e0 |
891 | offset >>= 3; /* turn into byte offset */ |
79072805 |
892 | s[offset] &= ~(mask << size); |
893 | s[offset] |= lval << size; |
894 | } |
895 | else { |
81e118e0 |
896 | offset >>= 3; /* turn into byte offset */ |
79072805 |
897 | if (size == 8) |
c5a0f51a |
898 | s[offset ] = lval & 0xff; |
79072805 |
899 | else if (size == 16) { |
c5a0f51a |
900 | s[offset ] = (lval >> 8) & 0xff; |
901 | s[offset+1] = lval & 0xff; |
79072805 |
902 | } |
903 | else if (size == 32) { |
c5a0f51a |
904 | s[offset ] = (lval >> 24) & 0xff; |
905 | s[offset+1] = (lval >> 16) & 0xff; |
906 | s[offset+2] = (lval >> 8) & 0xff; |
907 | s[offset+3] = lval & 0xff; |
908 | } |
d7d93a81 |
909 | #ifdef UV_IS_QUAD |
c5a0f51a |
910 | else if (size == 64) { |
c5a0f51a |
911 | if (ckWARN(WARN_PORTABLE)) |
912 | Perl_warner(aTHX_ WARN_PORTABLE, |
913 | "Bit vector size > 32 non-portable"); |
914 | s[offset ] = (lval >> 56) & 0xff; |
915 | s[offset+1] = (lval >> 48) & 0xff; |
916 | s[offset+2] = (lval >> 40) & 0xff; |
917 | s[offset+3] = (lval >> 32) & 0xff; |
918 | s[offset+4] = (lval >> 24) & 0xff; |
919 | s[offset+5] = (lval >> 16) & 0xff; |
920 | s[offset+6] = (lval >> 8) & 0xff; |
921 | s[offset+7] = lval & 0xff; |
79072805 |
922 | } |
dc1e3f56 |
923 | #endif |
79072805 |
924 | } |
7bb043c3 |
925 | SvSETMAGIC(targ); |
79072805 |
926 | } |
927 | |
928 | void |
864dbfa3 |
929 | Perl_do_chop(pTHX_ register SV *astr, register SV *sv) |
79072805 |
930 | { |
463ee0b2 |
931 | STRLEN len; |
a0d0e21e |
932 | char *s; |
8e84507e |
933 | |
79072805 |
934 | if (SvTYPE(sv) == SVt_PVAV) { |
a0d0e21e |
935 | register I32 i; |
936 | I32 max; |
937 | AV* av = (AV*)sv; |
938 | max = AvFILL(av); |
939 | for (i = 0; i <= max; i++) { |
940 | sv = (SV*)av_fetch(av, i, FALSE); |
3280af22 |
941 | if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef)) |
a0d0e21e |
942 | do_chop(astr, sv); |
943 | } |
944 | return; |
79072805 |
945 | } |
aa854799 |
946 | else if (SvTYPE(sv) == SVt_PVHV) { |
a0d0e21e |
947 | HV* hv = (HV*)sv; |
948 | HE* entry; |
949 | (void)hv_iterinit(hv); |
950 | /*SUPPRESS 560*/ |
155aba94 |
951 | while ((entry = hv_iternext(hv))) |
a0d0e21e |
952 | do_chop(astr,hv_iterval(hv,entry)); |
953 | return; |
79072805 |
954 | } |
aa854799 |
955 | else if (SvREADONLY(sv)) |
cea2e8a9 |
956 | Perl_croak(aTHX_ PL_no_modify); |
a0d0e21e |
957 | s = SvPV(sv, len); |
748a9306 |
958 | if (len && !SvPOK(sv)) |
a0d0e21e |
959 | s = SvPV_force(sv, len); |
7e2040f0 |
960 | if (DO_UTF8(sv)) { |
a0ed51b3 |
961 | if (s && len) { |
962 | char *send = s + len; |
963 | char *start = s; |
964 | s = send - 1; |
a0dbb045 |
965 | while (s > start && UTF8_IS_CONTINUATION(*s)) |
966 | s--; |
9041c2e3 |
967 | if (utf8_to_uvchr((U8*)s, 0)) { |
a0dbb045 |
968 | sv_setpvn(astr, s, send - s); |
969 | *s = '\0'; |
970 | SvCUR_set(sv, s - start); |
971 | SvNIOK_off(sv); |
972 | SvUTF8_on(astr); |
973 | } |
a0ed51b3 |
974 | } |
975 | else |
976 | sv_setpvn(astr, "", 0); |
977 | } |
7e2040f0 |
978 | else if (s && len) { |
a0d0e21e |
979 | s += --len; |
980 | sv_setpvn(astr, s, 1); |
981 | *s = '\0'; |
982 | SvCUR_set(sv, len); |
2c19a612 |
983 | SvUTF8_off(sv); |
a0d0e21e |
984 | SvNIOK_off(sv); |
79072805 |
985 | } |
986 | else |
a0d0e21e |
987 | sv_setpvn(astr, "", 0); |
988 | SvSETMAGIC(sv); |
7e2040f0 |
989 | } |
a0d0e21e |
990 | |
991 | I32 |
864dbfa3 |
992 | Perl_do_chomp(pTHX_ register SV *sv) |
a0d0e21e |
993 | { |
c07a80fd |
994 | register I32 count; |
a0d0e21e |
995 | STRLEN len; |
9b33ce3b |
996 | STRLEN n_a; |
a0d0e21e |
997 | char *s; |
c07a80fd |
998 | |
3280af22 |
999 | if (RsSNARF(PL_rs)) |
c07a80fd |
1000 | return 0; |
4c5a6083 |
1001 | if (RsRECORD(PL_rs)) |
1002 | return 0; |
c07a80fd |
1003 | count = 0; |
a0d0e21e |
1004 | if (SvTYPE(sv) == SVt_PVAV) { |
1005 | register I32 i; |
1006 | I32 max; |
1007 | AV* av = (AV*)sv; |
1008 | max = AvFILL(av); |
1009 | for (i = 0; i <= max; i++) { |
1010 | sv = (SV*)av_fetch(av, i, FALSE); |
3280af22 |
1011 | if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef)) |
a0d0e21e |
1012 | count += do_chomp(sv); |
1013 | } |
1014 | return count; |
1015 | } |
aa854799 |
1016 | else if (SvTYPE(sv) == SVt_PVHV) { |
a0d0e21e |
1017 | HV* hv = (HV*)sv; |
1018 | HE* entry; |
1019 | (void)hv_iterinit(hv); |
1020 | /*SUPPRESS 560*/ |
155aba94 |
1021 | while ((entry = hv_iternext(hv))) |
a0d0e21e |
1022 | count += do_chomp(hv_iterval(hv,entry)); |
1023 | return count; |
1024 | } |
aa854799 |
1025 | else if (SvREADONLY(sv)) |
cea2e8a9 |
1026 | Perl_croak(aTHX_ PL_no_modify); |
a0d0e21e |
1027 | s = SvPV(sv, len); |
a0d0e21e |
1028 | if (s && len) { |
1029 | s += --len; |
3280af22 |
1030 | if (RsPARA(PL_rs)) { |
a0d0e21e |
1031 | if (*s != '\n') |
1032 | goto nope; |
1033 | ++count; |
1034 | while (len && s[-1] == '\n') { |
1035 | --len; |
1036 | --s; |
1037 | ++count; |
1038 | } |
1039 | } |
a0d0e21e |
1040 | else { |
c07a80fd |
1041 | STRLEN rslen; |
3280af22 |
1042 | char *rsptr = SvPV(PL_rs, rslen); |
c07a80fd |
1043 | if (rslen == 1) { |
1044 | if (*s != *rsptr) |
1045 | goto nope; |
1046 | ++count; |
1047 | } |
1048 | else { |
8c2cee6f |
1049 | if (len < rslen - 1) |
c07a80fd |
1050 | goto nope; |
1051 | len -= rslen - 1; |
1052 | s -= rslen - 1; |
36477c24 |
1053 | if (memNE(s, rsptr, rslen)) |
c07a80fd |
1054 | goto nope; |
1055 | count += rslen; |
1056 | } |
a0d0e21e |
1057 | } |
9b33ce3b |
1058 | s = SvPV_force(sv, n_a); |
a0d0e21e |
1059 | SvCUR_set(sv, len); |
9b33ce3b |
1060 | *SvEND(sv) = '\0'; |
a0d0e21e |
1061 | SvNIOK_off(sv); |
9b33ce3b |
1062 | SvSETMAGIC(sv); |
a0d0e21e |
1063 | } |
1064 | nope: |
a0d0e21e |
1065 | return count; |
8e84507e |
1066 | } |
79072805 |
1067 | |
1068 | void |
864dbfa3 |
1069 | Perl_do_vop(pTHX_ I32 optype, SV *sv, SV *left, SV *right) |
79072805 |
1070 | { |
1071 | #ifdef LIBERAL |
1072 | register long *dl; |
1073 | register long *ll; |
1074 | register long *rl; |
1075 | #endif |
1076 | register char *dc; |
463ee0b2 |
1077 | STRLEN leftlen; |
1078 | STRLEN rightlen; |
7a4c00b4 |
1079 | register char *lc; |
1080 | register char *rc; |
79072805 |
1081 | register I32 len; |
a0d0e21e |
1082 | I32 lensave; |
7a4c00b4 |
1083 | char *lsave; |
1084 | char *rsave; |
0c57e439 |
1085 | bool left_utf = DO_UTF8(left); |
1086 | bool right_utf = DO_UTF8(right); |
b7953727 |
1087 | I32 needlen = 0; |
0c57e439 |
1088 | |
1089 | if (left_utf && !right_utf) |
1090 | sv_utf8_upgrade(right); |
a1ca4561 |
1091 | else if (!left_utf && right_utf) |
0c57e439 |
1092 | sv_utf8_upgrade(left); |
79072805 |
1093 | |
1fbd88dc |
1094 | if (sv != left || (optype != OP_BIT_AND && !SvOK(sv) && !SvGMAGICAL(sv))) |
1095 | sv_setpvn(sv, "", 0); /* avoid undef warning on |= and ^= */ |
7a4c00b4 |
1096 | lsave = lc = SvPV(left, leftlen); |
1097 | rsave = rc = SvPV(right, rightlen); |
93a17b20 |
1098 | len = leftlen < rightlen ? leftlen : rightlen; |
a0d0e21e |
1099 | lensave = len; |
c9b3c8d0 |
1100 | if ((left_utf || right_utf) && (sv == left || sv == right)) { |
1101 | needlen = optype == OP_BIT_AND ? len : leftlen + rightlen; |
1102 | Newz(801, dc, needlen + 1, char); |
1103 | } |
1104 | else if (SvOK(sv) || SvTYPE(sv) > SVt_PVMG) { |
2d8e6c8d |
1105 | STRLEN n_a; |
1106 | dc = SvPV_force(sv, n_a); |
ff68c719 |
1107 | if (SvCUR(sv) < len) { |
1108 | dc = SvGROW(sv, len + 1); |
1109 | (void)memzero(dc + SvCUR(sv), len - SvCUR(sv) + 1); |
1110 | } |
c9b3c8d0 |
1111 | if (optype != OP_BIT_AND && (left_utf || right_utf)) |
1112 | dc = SvGROW(sv, leftlen + rightlen + 1); |
ff68c719 |
1113 | } |
1114 | else { |
c9b3c8d0 |
1115 | needlen = ((optype == OP_BIT_AND) |
1116 | ? len : (leftlen > rightlen ? leftlen : rightlen)); |
ff68c719 |
1117 | Newz(801, dc, needlen + 1, char); |
1118 | (void)sv_usepvn(sv, dc, needlen); |
1119 | dc = SvPVX(sv); /* sv_usepvn() calls Renew() */ |
79072805 |
1120 | } |
a0d0e21e |
1121 | SvCUR_set(sv, len); |
1122 | (void)SvPOK_only(sv); |
0c57e439 |
1123 | if (left_utf || right_utf) { |
1124 | UV duc, luc, ruc; |
c9b3c8d0 |
1125 | char *dcsave = dc; |
0c57e439 |
1126 | STRLEN lulen = leftlen; |
1127 | STRLEN rulen = rightlen; |
ba210ebe |
1128 | STRLEN ulen; |
0c57e439 |
1129 | |
1130 | switch (optype) { |
1131 | case OP_BIT_AND: |
1132 | while (lulen && rulen) { |
9041c2e3 |
1133 | luc = utf8n_to_uvchr((U8*)lc, lulen, &ulen, UTF8_ALLOW_ANYUV); |
0c57e439 |
1134 | lc += ulen; |
1135 | lulen -= ulen; |
9041c2e3 |
1136 | ruc = utf8n_to_uvchr((U8*)rc, rulen, &ulen, UTF8_ALLOW_ANYUV); |
0c57e439 |
1137 | rc += ulen; |
1138 | rulen -= ulen; |
1139 | duc = luc & ruc; |
9041c2e3 |
1140 | dc = (char*)uvchr_to_utf8((U8*)dc, duc); |
0c57e439 |
1141 | } |
c9b3c8d0 |
1142 | if (sv == left || sv == right) |
1143 | (void)sv_usepvn(sv, dcsave, needlen); |
1144 | SvCUR_set(sv, dc - dcsave); |
0c57e439 |
1145 | break; |
1146 | case OP_BIT_XOR: |
1147 | while (lulen && rulen) { |
9041c2e3 |
1148 | luc = utf8n_to_uvchr((U8*)lc, lulen, &ulen, UTF8_ALLOW_ANYUV); |
0c57e439 |
1149 | lc += ulen; |
1150 | lulen -= ulen; |
9041c2e3 |
1151 | ruc = utf8n_to_uvchr((U8*)rc, rulen, &ulen, UTF8_ALLOW_ANYUV); |
0c57e439 |
1152 | rc += ulen; |
1153 | rulen -= ulen; |
1154 | duc = luc ^ ruc; |
9041c2e3 |
1155 | dc = (char*)uvchr_to_utf8((U8*)dc, duc); |
0c57e439 |
1156 | } |
1157 | goto mop_up_utf; |
1158 | case OP_BIT_OR: |
1159 | while (lulen && rulen) { |
9041c2e3 |
1160 | luc = utf8n_to_uvchr((U8*)lc, lulen, &ulen, UTF8_ALLOW_ANYUV); |
0c57e439 |
1161 | lc += ulen; |
1162 | lulen -= ulen; |
9041c2e3 |
1163 | ruc = utf8n_to_uvchr((U8*)rc, rulen, &ulen, UTF8_ALLOW_ANYUV); |
0c57e439 |
1164 | rc += ulen; |
1165 | rulen -= ulen; |
1166 | duc = luc | ruc; |
9041c2e3 |
1167 | dc = (char*)uvchr_to_utf8((U8*)dc, duc); |
0c57e439 |
1168 | } |
1169 | mop_up_utf: |
c9b3c8d0 |
1170 | if (sv == left || sv == right) |
1171 | (void)sv_usepvn(sv, dcsave, needlen); |
1172 | SvCUR_set(sv, dc - dcsave); |
0c57e439 |
1173 | if (rulen) |
1174 | sv_catpvn(sv, rc, rulen); |
1175 | else if (lulen) |
1176 | sv_catpvn(sv, lc, lulen); |
1177 | else |
1178 | *SvEND(sv) = '\0'; |
1179 | break; |
1180 | } |
1181 | SvUTF8_on(sv); |
1182 | goto finish; |
1183 | } |
1184 | else |
79072805 |
1185 | #ifdef LIBERAL |
1186 | if (len >= sizeof(long)*4 && |
1187 | !((long)dc % sizeof(long)) && |
1188 | !((long)lc % sizeof(long)) && |
1189 | !((long)rc % sizeof(long))) /* It's almost always aligned... */ |
1190 | { |
1191 | I32 remainder = len % (sizeof(long)*4); |
1192 | len /= (sizeof(long)*4); |
1193 | |
1194 | dl = (long*)dc; |
1195 | ll = (long*)lc; |
1196 | rl = (long*)rc; |
1197 | |
1198 | switch (optype) { |
1199 | case OP_BIT_AND: |
1200 | while (len--) { |
1201 | *dl++ = *ll++ & *rl++; |
1202 | *dl++ = *ll++ & *rl++; |
1203 | *dl++ = *ll++ & *rl++; |
1204 | *dl++ = *ll++ & *rl++; |
1205 | } |
1206 | break; |
a0d0e21e |
1207 | case OP_BIT_XOR: |
79072805 |
1208 | while (len--) { |
1209 | *dl++ = *ll++ ^ *rl++; |
1210 | *dl++ = *ll++ ^ *rl++; |
1211 | *dl++ = *ll++ ^ *rl++; |
1212 | *dl++ = *ll++ ^ *rl++; |
1213 | } |
1214 | break; |
1215 | case OP_BIT_OR: |
1216 | while (len--) { |
1217 | *dl++ = *ll++ | *rl++; |
1218 | *dl++ = *ll++ | *rl++; |
1219 | *dl++ = *ll++ | *rl++; |
1220 | *dl++ = *ll++ | *rl++; |
1221 | } |
1222 | } |
1223 | |
1224 | dc = (char*)dl; |
1225 | lc = (char*)ll; |
1226 | rc = (char*)rl; |
1227 | |
1228 | len = remainder; |
1229 | } |
1230 | #endif |
a0d0e21e |
1231 | { |
a0d0e21e |
1232 | switch (optype) { |
1233 | case OP_BIT_AND: |
1234 | while (len--) |
1235 | *dc++ = *lc++ & *rc++; |
1236 | break; |
1237 | case OP_BIT_XOR: |
1238 | while (len--) |
1239 | *dc++ = *lc++ ^ *rc++; |
1240 | goto mop_up; |
1241 | case OP_BIT_OR: |
1242 | while (len--) |
1243 | *dc++ = *lc++ | *rc++; |
1244 | mop_up: |
1245 | len = lensave; |
1246 | if (rightlen > len) |
1247 | sv_catpvn(sv, rsave + len, rightlen - len); |
1248 | else if (leftlen > len) |
1249 | sv_catpvn(sv, lsave + len, leftlen - len); |
4633a7c4 |
1250 | else |
1251 | *SvEND(sv) = '\0'; |
a0d0e21e |
1252 | break; |
1253 | } |
79072805 |
1254 | } |
0c57e439 |
1255 | finish: |
fb73857a |
1256 | SvTAINT(sv); |
79072805 |
1257 | } |
463ee0b2 |
1258 | |
1259 | OP * |
cea2e8a9 |
1260 | Perl_do_kv(pTHX) |
463ee0b2 |
1261 | { |
39644a26 |
1262 | dSP; |
463ee0b2 |
1263 | HV *hv = (HV*)POPs; |
800e9ae0 |
1264 | HV *keys; |
463ee0b2 |
1265 | register HE *entry; |
463ee0b2 |
1266 | SV *tmpstr; |
54310121 |
1267 | I32 gimme = GIMME_V; |
533c011a |
1268 | I32 dokeys = (PL_op->op_type == OP_KEYS); |
1269 | I32 dovalues = (PL_op->op_type == OP_VALUES); |
c750a3ec |
1270 | I32 realhv = (SvTYPE(hv) == SVt_PVHV); |
8e84507e |
1271 | |
1272 | if (PL_op->op_type == OP_RV2HV || PL_op->op_type == OP_PADHV) |
a0d0e21e |
1273 | dokeys = dovalues = TRUE; |
463ee0b2 |
1274 | |
85581909 |
1275 | if (!hv) { |
78f9721b |
1276 | if (PL_op->op_flags & OPf_MOD || LVRET) { /* lvalue */ |
85581909 |
1277 | dTARGET; /* make sure to clear its target here */ |
1278 | if (SvTYPE(TARG) == SVt_PVLV) |
1279 | LvTARG(TARG) = Nullsv; |
1280 | PUSHs(TARG); |
1281 | } |
463ee0b2 |
1282 | RETURN; |
85581909 |
1283 | } |
748a9306 |
1284 | |
800e9ae0 |
1285 | keys = realhv ? hv : avhv_keys((AV*)hv); |
1286 | (void)hv_iterinit(keys); /* always reset iterator regardless */ |
748a9306 |
1287 | |
54310121 |
1288 | if (gimme == G_VOID) |
aa689395 |
1289 | RETURN; |
1290 | |
54310121 |
1291 | if (gimme == G_SCALAR) { |
6ee623d5 |
1292 | IV i; |
463ee0b2 |
1293 | dTARGET; |
1294 | |
78f9721b |
1295 | if (PL_op->op_flags & OPf_MOD || LVRET) { /* lvalue */ |
85581909 |
1296 | if (SvTYPE(TARG) < SVt_PVLV) { |
1297 | sv_upgrade(TARG, SVt_PVLV); |
14befaf4 |
1298 | sv_magic(TARG, Nullsv, PERL_MAGIC_nkeys, Nullch, 0); |
85581909 |
1299 | } |
1300 | LvTYPE(TARG) = 'k'; |
800e9ae0 |
1301 | if (LvTARG(TARG) != (SV*)keys) { |
6ff81951 |
1302 | if (LvTARG(TARG)) |
1303 | SvREFCNT_dec(LvTARG(TARG)); |
800e9ae0 |
1304 | LvTARG(TARG) = SvREFCNT_inc(keys); |
6ff81951 |
1305 | } |
85581909 |
1306 | PUSHs(TARG); |
1307 | RETURN; |
1308 | } |
1309 | |
14befaf4 |
1310 | if (! SvTIED_mg((SV*)keys, PERL_MAGIC_tied)) |
800e9ae0 |
1311 | i = HvKEYS(keys); |
463ee0b2 |
1312 | else { |
1313 | i = 0; |
463ee0b2 |
1314 | /*SUPPRESS 560*/ |
800e9ae0 |
1315 | while (hv_iternext(keys)) i++; |
463ee0b2 |
1316 | } |
1317 | PUSHi( i ); |
1318 | RETURN; |
1319 | } |
1320 | |
8ed4b672 |
1321 | EXTEND(SP, HvKEYS(keys) * (dokeys + dovalues)); |
463ee0b2 |
1322 | |
463ee0b2 |
1323 | PUTBACK; /* hv_iternext and hv_iterval might clobber stack_sp */ |
155aba94 |
1324 | while ((entry = hv_iternext(keys))) { |
463ee0b2 |
1325 | SPAGAIN; |
6bfc225d |
1326 | if (dokeys) |
8c2cee6f |
1327 | XPUSHs(hv_iterkeysv(entry)); /* won't clobber stack_sp */ |
463ee0b2 |
1328 | if (dovalues) { |
463ee0b2 |
1329 | PUTBACK; |
b6429b1b |
1330 | tmpstr = realhv ? |
1331 | hv_iterval(hv,entry) : avhv_iterval((AV*)hv,entry); |
cea2e8a9 |
1332 | DEBUG_H(Perl_sv_setpvf(aTHX_ tmpstr, "%lu%%%d=%lu", |
46fc3d4c |
1333 | (unsigned long)HeHASH(entry), |
800e9ae0 |
1334 | HvMAX(keys)+1, |
1335 | (unsigned long)(HeHASH(entry) & HvMAX(keys)))); |
463ee0b2 |
1336 | SPAGAIN; |
46fc3d4c |
1337 | XPUSHs(tmpstr); |
463ee0b2 |
1338 | } |
1339 | PUTBACK; |
1340 | } |
1341 | return NORMAL; |
1342 | } |
4e35701f |
1343 | |