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