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