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