Improve and restructure t/op/pat.t and split out some unicode related tests into...
[p5sagit/p5-mst-13.2.git] / doop.c
CommitLineData
a0d0e21e 1/* doop.c
79072805 2 *
1129b882 3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4 * 2001, 2002, 2004, 2005, 2006, 2007, 2008, 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/*
4ac71550 12 * 'So that was the job I felt I had to do when I started,' thought Sam.
13 *
14 * [p.934 of _The Lord of the Rings_, VI/iii: "Mount Doom"]
79072805 15 */
16
166f8a29 17/* This file contains some common functions needed to carry out certain
18 * ops. For example both pp_schomp() and pp_chomp() - scalar and array
19 * chomp operations - call the function do_chomp() found in this file.
20 */
21
79072805 22#include "EXTERN.h"
864dbfa3 23#define PERL_IN_DOOP_C
79072805 24#include "perl.h"
25
64ca3a65 26#ifndef PERL_MICRO
79072805 27#include <signal.h>
28#endif
29
942e002e 30STATIC I32
c395bd6c 31S_do_trans_simple(pTHX_ SV * const sv)
79072805 32{
97aff369 33 dVAR;
4757a243 34 I32 matches = 0;
463ee0b2 35 STRLEN len;
c395bd6c 36 U8 *s = (U8*)SvPV(sv,len);
37 U8 * const send = s+len;
c4420975 38 const short * const tbl = (short*)cPVOP->op_pv;
7918f24d 39
40 PERL_ARGS_ASSERT_DO_TRANS_SIMPLE;
41
4757a243 42 if (!tbl)
7d85a32c 43 Perl_croak(aTHX_ "panic: do_trans_simple line %d",__LINE__);
a0ed51b3 44
1e54db1a 45 /* First, take care of non-UTF-8 input strings, because they're easy */
1aa99e6b 46 if (!SvUTF8(sv)) {
01ec43d0 47 while (s < send) {
f54cb97a 48 const I32 ch = tbl[*s];
49 if (ch >= 0) {
01ec43d0 50 matches++;
c4420975 51 *s = (U8)ch;
01ec43d0 52 }
c4420975 53 s++;
01ec43d0 54 }
55 SvSETMAGIC(sv);
9b877dbb 56 }
57 else {
c395bd6c 58 const I32 grows = PL_op->op_private & OPpTRANS_GROWS;
59 U8 *d;
60 U8 *dstart;
61
62 /* Allow for expansion: $_="a".chr(400); tr/a/\xFE/, FE needs encoding */
63 if (grows)
64 Newx(d, len*2+1, U8);
65 else
66 d = s;
67 dstart = d;
68 while (s < send) {
69 STRLEN ulen;
70 I32 ch;
71
72 /* Need to check this, otherwise 128..255 won't match */
73 const UV c = utf8n_to_uvchr(s, send - s, &ulen, UTF8_ALLOW_DEFAULT);
74 if (c < 0x100 && (ch = tbl[c]) >= 0) {
75 matches++;
76 d = uvchr_to_utf8(d, ch);
77 s += ulen;
78 }
79 else { /* No match -> copy */
80 Move(s, d, ulen, U8);
81 d += ulen;
82 s += ulen;
83 }
84 }
85 if (grows) {
86 sv_setpvn(sv, (char*)dstart, d - dstart);
87 Safefree(dstart);
88 }
89 else {
90 *d = '\0';
91 SvCUR_set(sv, d - dstart);
92 }
93 SvUTF8_on(sv);
94 SvSETMAGIC(sv);
9b877dbb 95 }
4757a243 96 return matches;
97}
98
942e002e 99STATIC I32
c395bd6c 100S_do_trans_count(pTHX_ SV * const sv)
4757a243 101{
97aff369 102 dVAR;
4757a243 103 STRLEN len;
c395bd6c 104 const U8 *s = (const U8*)SvPV_const(sv, len);
105 const U8 * const send = s + len;
106 I32 matches = 0;
f54cb97a 107 const short * const tbl = (short*)cPVOP->op_pv;
7918f24d 108
109 PERL_ARGS_ASSERT_DO_TRANS_COUNT;
110
4757a243 111 if (!tbl)
7d85a32c 112 Perl_croak(aTHX_ "panic: do_trans_count line %d",__LINE__);
4757a243 113
c395bd6c 114 if (!SvUTF8(sv)) {
1aa99e6b 115 while (s < send) {
116 if (tbl[*s++] >= 0)
036b4402 117 matches++;
1aa99e6b 118 }
c395bd6c 119 }
fabdb6c0 120 else {
121 const I32 complement = PL_op->op_private & OPpTRANS_COMPLEMENT;
1aa99e6b 122 while (s < send) {
1aa99e6b 123 STRLEN ulen;
9f7f3913 124 const UV c = utf8n_to_uvchr(s, send - s, &ulen, UTF8_ALLOW_DEFAULT);
8973db79 125 if (c < 0x100) {
126 if (tbl[c] >= 0)
127 matches++;
128 } else if (complement)
1aa99e6b 129 matches++;
130 s += ulen;
131 }
fabdb6c0 132 }
4757a243 133
134 return matches;
135}
136
942e002e 137STATIC I32
c395bd6c 138S_do_trans_complex(pTHX_ SV * const sv)
4757a243 139{
97aff369 140 dVAR;
c395bd6c 141 STRLEN len;
142 U8 *s = (U8*)SvPV(sv, len);
143 U8 * const send = s+len;
4757a243 144 I32 matches = 0;
f54cb97a 145 const short * const tbl = (short*)cPVOP->op_pv;
7918f24d 146
147 PERL_ARGS_ASSERT_DO_TRANS_COMPLEX;
148
4757a243 149 if (!tbl)
7d85a32c 150 Perl_croak(aTHX_ "panic: do_trans_complex line %d",__LINE__);
4757a243 151
c395bd6c 152 if (!SvUTF8(sv)) {
153 U8 *d = s;
154 U8 * const dstart = d;
4757a243 155
1aa99e6b 156 if (PL_op->op_private & OPpTRANS_SQUASH) {
f54cb97a 157 const U8* p = send;
1aa99e6b 158 while (s < send) {
f54cb97a 159 const I32 ch = tbl[*s];
160 if (ch >= 0) {
eb160463 161 *d = (U8)ch;
036b4402 162 matches++;
1aa99e6b 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 173 else {
174 while (s < send) {
f54cb97a 175 const I32 ch = tbl[*s];
176 if (ch >= 0) {
1aa99e6b 177 matches++;
eb160463 178 *d++ = (U8)ch;
1aa99e6b 179 }
180 else if (ch == -1) /* -1 is unmapped character */
181 *d++ = *s;
182 else if (ch == -2) /* -2 is delete character */
183 matches++;
184 s++;
185 }
186 }
76ef7183 187 *d = '\0';
1aa99e6b 188 SvCUR_set(sv, d - dstart);
4757a243 189 }
c395bd6c 190 else { /* is utf8 */
fabdb6c0 191 const I32 complement = PL_op->op_private & OPpTRANS_COMPLEMENT;
192 const I32 grows = PL_op->op_private & OPpTRANS_GROWS;
193 const I32 del = PL_op->op_private & OPpTRANS_DELETE;
c395bd6c 194 U8 *d;
195 U8 *dstart;
196 STRLEN rlen = 0;
fabdb6c0 197
9b877dbb 198 if (grows)
a02a5408 199 Newx(d, len*2+1, U8);
9b877dbb 200 else
201 d = s;
1aa99e6b 202 dstart = d;
8973db79 203 if (complement && !del)
204 rlen = tbl[0x100];
1aa99e6b 205
4aecb5b5 206#ifdef MACOS_TRADITIONAL
207#define comp CoMP /* "comp" is a keyword in some compilers ... */
208#endif
209
1aa99e6b 210 if (PL_op->op_private & OPpTRANS_SQUASH) {
1aa99e6b 211 UV pch = 0xfeedface;
212 while (s < send) {
213 STRLEN len;
9f7f3913 214 const UV comp = utf8n_to_uvchr(s, send - s, &len,
215 UTF8_ALLOW_DEFAULT);
f54cb97a 216 I32 ch;
1aa99e6b 217
8973db79 218 if (comp > 0xff) {
219 if (!complement) {
640af7f1 220 Move(s, d, len, U8);
8973db79 221 d += len;
222 }
223 else {
224 matches++;
225 if (!del) {
bb7a0f54 226 ch = (rlen == 0) ? (I32)comp :
05d340b8 227 (comp - 0x100 < rlen) ?
8973db79 228 tbl[comp+1] : tbl[0x100+rlen];
eb160463 229 if ((UV)ch != pch) {
9041c2e3 230 d = uvchr_to_utf8(d, ch);
eb160463 231 pch = (UV)ch;
8973db79 232 }
233 s += len;
234 continue;
235 }
236 }
9b877dbb 237 }
1aa99e6b 238 else if ((ch = tbl[comp]) >= 0) {
239 matches++;
eb160463 240 if ((UV)ch != pch) {
9041c2e3 241 d = uvchr_to_utf8(d, ch);
eb160463 242 pch = (UV)ch;
1aa99e6b 243 }
244 s += len;
245 continue;
246 }
9b877dbb 247 else if (ch == -1) { /* -1 is unmapped character */
640af7f1 248 Move(s, d, len, U8);
9b877dbb 249 d += len;
250 }
1aa99e6b 251 else if (ch == -2) /* -2 is delete character */
252 matches++;
253 s += len;
254 pch = 0xfeedface;
255 }
5d06d08e 256 }
1aa99e6b 257 else {
258 while (s < send) {
259 STRLEN len;
9f7f3913 260 const UV comp = utf8n_to_uvchr(s, send - s, &len,
261 UTF8_ALLOW_DEFAULT);
f54cb97a 262 I32 ch;
8973db79 263 if (comp > 0xff) {
264 if (!complement) {
0376ff32 265 Move(s, d, len, U8);
8973db79 266 d += len;
267 }
268 else {
269 matches++;
270 if (!del) {
9041c2e3 271 if (comp - 0x100 < rlen)
272 d = uvchr_to_utf8(d, tbl[comp+1]);
8973db79 273 else
9041c2e3 274 d = uvchr_to_utf8(d, tbl[0x100+rlen]);
8973db79 275 }
276 }
9b877dbb 277 }
1aa99e6b 278 else if ((ch = tbl[comp]) >= 0) {
9041c2e3 279 d = uvchr_to_utf8(d, ch);
1aa99e6b 280 matches++;
281 }
282 else if (ch == -1) { /* -1 is unmapped character */
640af7f1 283 Move(s, d, len, U8);
9b877dbb 284 d += len;
1aa99e6b 285 }
286 else if (ch == -2) /* -2 is delete character */
287 matches++;
288 s += len;
289 }
290 }
9b877dbb 291 if (grows) {
292 sv_setpvn(sv, (char*)dstart, d - dstart);
293 Safefree(dstart);
294 }
295 else {
296 *d = '\0';
297 SvCUR_set(sv, d - dstart);
298 }
1aa99e6b 299 SvUTF8_on(sv);
4757a243 300 }
5e44153e 301 SvSETMAGIC(sv);
4757a243 302 return matches;
303}
304
942e002e 305STATIC I32
c395bd6c 306S_do_trans_simple_utf8(pTHX_ SV * const sv)
4757a243 307{
97aff369 308 dVAR;
4757a243 309 U8 *s;
310 U8 *send;
311 U8 *d;
036b4402 312 U8 *start;
1aa99e6b 313 U8 *dstart, *dend;
4757a243 314 I32 matches = 0;
f54cb97a 315 const I32 grows = PL_op->op_private & OPpTRANS_GROWS;
4757a243 316 STRLEN len;
043e41b8 317 SV* const rv =
318#ifdef USE_ITHREADS
319 PAD_SVl(cPADOP->op_padix);
320#else
ad64d0ec 321 MUTABLE_SV(cSVOP->op_sv);
043e41b8 322#endif
85fbaab2 323 HV* const hv = MUTABLE_HV(SvRV(rv));
a4fc7abc 324 SV* const * svp = hv_fetchs(hv, "NONE", FALSE);
f54cb97a 325 const UV none = svp ? SvUV(*svp) : 0x7fffffff;
326 const UV extra = none + 1;
b7953727 327 UV final = 0;
1aa99e6b 328 U8 hibit = 0;
4757a243 329
7918f24d 330 PERL_ARGS_ASSERT_DO_TRANS_SIMPLE_UTF8;
331
4757a243 332 s = (U8*)SvPV(sv, len);
c395bd6c 333 if (!SvUTF8(sv)) {
c4420975 334 const U8 *t = s;
335 const U8 * const e = s + len;
c4d5f83a 336 while (t < e) {
f54cb97a 337 const U8 ch = *t++;
fabdb6c0 338 hibit = !NATIVE_IS_INVARIANT(ch);
339 if (hibit) {
340 s = bytes_to_utf8(s, &len);
1aa99e6b 341 break;
fabdb6c0 342 }
c4d5f83a 343 }
1aa99e6b 344 }
4757a243 345 send = s + len;
036b4402 346 start = s;
4757a243 347
a4fc7abc 348 svp = hv_fetchs(hv, "FINAL", FALSE);
4757a243 349 if (svp)
350 final = SvUV(*svp);
351
9b877dbb 352 if (grows) {
353 /* d needs to be bigger than s, in case e.g. upgrading is required */
a02a5408 354 Newx(d, len * 3 + UTF8_MAXBYTES, U8);
9b877dbb 355 dend = d + len * 3;
356 dstart = d;
357 }
358 else {
359 dstart = d = s;
360 dend = d + len;
361 }
1aa99e6b 362
4757a243 363 while (s < send) {
0bcc34c2 364 const UV uv = swash_fetch(rv, s, TRUE);
365 if (uv < none) {
4757a243 366 s += UTF8SKIP(s);
367 matches++;
7d85a32c 368 d = uvuni_to_utf8(d, uv);
4757a243 369 }
370 else if (uv == none) {
4373e329 371 const int i = UTF8SKIP(s);
0376ff32 372 Move(s, d, i, U8);
9b877dbb 373 d += i;
374 s += i;
4757a243 375 }
376 else if (uv == extra) {
4373e329 377 s += UTF8SKIP(s);
4757a243 378 matches++;
7d85a32c 379 d = uvuni_to_utf8(d, final);
4757a243 380 }
381 else
382 s += UTF8SKIP(s);
1aa99e6b 383
9b877dbb 384 if (d > dend) {
4373e329 385 const STRLEN clen = d - dstart;
386 const STRLEN nlen = dend - dstart + len + UTF8_MAXBYTES;
9b877dbb 387 if (!grows)
7d85a32c 388 Perl_croak(aTHX_ "panic: do_trans_simple_utf8 line %d",__LINE__);
89ebb4a3 389 Renew(dstart, nlen + UTF8_MAXBYTES, U8);
1aa99e6b 390 d = dstart + clen;
391 dend = dstart + nlen;
392 }
4757a243 393 }
16ec844d 394 if (grows || hibit) {
9b877dbb 395 sv_setpvn(sv, (char*)dstart, d - dstart);
396 Safefree(dstart);
16ec844d 397 if (grows && hibit)
398 Safefree(start);
9b877dbb 399 }
400 else {
401 *d = '\0';
402 SvCUR_set(sv, d - dstart);
403 }
4757a243 404 SvSETMAGIC(sv);
1aa99e6b 405 SvUTF8_on(sv);
4757a243 406
407 return matches;
408}
409
942e002e 410STATIC I32
c395bd6c 411S_do_trans_count_utf8(pTHX_ SV * const sv)
4757a243 412{
97aff369 413 dVAR;
93524f2b 414 const U8 *s;
cbbf8932 415 const U8 *start = NULL;
416 const U8 *send;
4757a243 417 I32 matches = 0;
418 STRLEN len;
043e41b8 419 SV* const rv =
420#ifdef USE_ITHREADS
421 PAD_SVl(cPADOP->op_padix);
422#else
ad64d0ec 423 MUTABLE_SV(cSVOP->op_sv);
043e41b8 424#endif
85fbaab2 425 HV* const hv = MUTABLE_HV(SvRV(rv));
a4fc7abc 426 SV* const * const svp = hv_fetchs(hv, "NONE", FALSE);
f54cb97a 427 const UV none = svp ? SvUV(*svp) : 0x7fffffff;
428 const UV extra = none + 1;
1aa99e6b 429 U8 hibit = 0;
4757a243 430
7918f24d 431 PERL_ARGS_ASSERT_DO_TRANS_COUNT_UTF8;
432
93524f2b 433 s = (const U8*)SvPV_const(sv, len);
1aa99e6b 434 if (!SvUTF8(sv)) {
4373e329 435 const U8 *t = s;
c4420975 436 const U8 * const e = s + len;
c4d5f83a 437 while (t < e) {
4373e329 438 const U8 ch = *t++;
fabdb6c0 439 hibit = !NATIVE_IS_INVARIANT(ch);
440 if (hibit) {
441 start = s = bytes_to_utf8(s, &len);
1aa99e6b 442 break;
fabdb6c0 443 }
c4d5f83a 444 }
1aa99e6b 445 }
4757a243 446 send = s + len;
447
448 while (s < send) {
c4420975 449 const UV uv = swash_fetch(rv, s, TRUE);
450 if (uv < none || uv == extra)
4757a243 451 matches++;
834a4ddd 452 s += UTF8SKIP(s);
4757a243 453 }
1aa99e6b 454 if (hibit)
455 Safefree(start);
4757a243 456
457 return matches;
458}
459
942e002e 460STATIC I32
c395bd6c 461S_do_trans_complex_utf8(pTHX_ SV * const sv)
4757a243 462{
97aff369 463 dVAR;
1aa99e6b 464 U8 *start, *send;
4757a243 465 U8 *d;
466 I32 matches = 0;
4373e329 467 const I32 squash = PL_op->op_private & OPpTRANS_SQUASH;
468 const I32 del = PL_op->op_private & OPpTRANS_DELETE;
469 const I32 grows = PL_op->op_private & OPpTRANS_GROWS;
043e41b8 470 SV* const rv =
471#ifdef USE_ITHREADS
472 PAD_SVl(cPADOP->op_padix);
473#else
ad64d0ec 474 MUTABLE_SV(cSVOP->op_sv);
043e41b8 475#endif
85fbaab2 476 HV * const hv = MUTABLE_HV(SvRV(rv));
a4fc7abc 477 SV * const *svp = hv_fetchs(hv, "NONE", FALSE);
f54cb97a 478 const UV none = svp ? SvUV(*svp) : 0x7fffffff;
479 const UV extra = none + 1;
b7953727 480 UV final = 0;
45005bfb 481 bool havefinal = FALSE;
4757a243 482 STRLEN len;
1aa99e6b 483 U8 *dstart, *dend;
1aa99e6b 484 U8 hibit = 0;
f54cb97a 485 U8 *s = (U8*)SvPV(sv, len);
7918f24d 486
487 PERL_ARGS_ASSERT_DO_TRANS_COMPLEX_UTF8;
488
c395bd6c 489 if (!SvUTF8(sv)) {
53c1dcc0 490 const U8 *t = s;
491 const U8 * const e = s + len;
c4d5f83a 492 while (t < e) {
f54cb97a 493 const U8 ch = *t++;
fabdb6c0 494 hibit = !NATIVE_IS_INVARIANT(ch);
495 if (hibit) {
496 s = bytes_to_utf8(s, &len);
1aa99e6b 497 break;
fabdb6c0 498 }
c4d5f83a 499 }
1aa99e6b 500 }
4757a243 501 send = s + len;
1aa99e6b 502 start = s;
4757a243 503
a4fc7abc 504 svp = hv_fetchs(hv, "FINAL", FALSE);
45005bfb 505 if (svp) {
4757a243 506 final = SvUV(*svp);
45005bfb 507 havefinal = TRUE;
508 }
4757a243 509
9b877dbb 510 if (grows) {
511 /* d needs to be bigger than s, in case e.g. upgrading is required */
a02a5408 512 Newx(d, len * 3 + UTF8_MAXBYTES, U8);
9b877dbb 513 dend = d + len * 3;
514 dstart = d;
515 }
516 else {
517 dstart = d = s;
518 dend = d + len;
519 }
4757a243 520
521 if (squash) {
522 UV puv = 0xfeedface;
523 while (s < send) {
f54cb97a 524 UV uv = swash_fetch(rv, s, TRUE);
9041c2e3 525
9b877dbb 526 if (d > dend) {
4373e329 527 const STRLEN clen = d - dstart;
528 const STRLEN nlen = dend - dstart + len + UTF8_MAXBYTES;
9b877dbb 529 if (!grows)
7d85a32c 530 Perl_croak(aTHX_ "panic: do_trans_complex_utf8 line %d",__LINE__);
89ebb4a3 531 Renew(dstart, nlen + UTF8_MAXBYTES, U8);
1aa99e6b 532 d = dstart + clen;
533 dend = dstart + nlen;
4757a243 534 }
535 if (uv < none) {
536 matches++;
94472101 537 s += UTF8SKIP(s);
4757a243 538 if (uv != puv) {
7d85a32c 539 d = uvuni_to_utf8(d, uv);
4757a243 540 puv = uv;
541 }
4757a243 542 continue;
543 }
544 else if (uv == none) { /* "none" is unmapped character */
4373e329 545 const int i = UTF8SKIP(s);
0376ff32 546 Move(s, d, i, U8);
9b877dbb 547 d += i;
548 s += i;
4757a243 549 puv = 0xfeedface;
550 continue;
a0ed51b3 551 }
4757a243 552 else if (uv == extra && !del) {
553 matches++;
45005bfb 554 if (havefinal) {
555 s += UTF8SKIP(s);
556 if (puv != final) {
7d85a32c 557 d = uvuni_to_utf8(d, final);
45005bfb 558 puv = final;
559 }
560 }
561 else {
562 STRLEN len;
9f7f3913 563 uv = utf8n_to_uvuni(s, send - s, &len, UTF8_ALLOW_DEFAULT);
45005bfb 564 if (uv != puv) {
0376ff32 565 Move(s, d, len, U8);
45005bfb 566 d += len;
567 puv = uv;
568 }
569 s += len;
4757a243 570 }
4757a243 571 continue;
572 }
01ec43d0 573 matches++; /* "none+1" is delete character */
574 s += UTF8SKIP(s);
a0ed51b3 575 }
79072805 576 }
577 else {
4757a243 578 while (s < send) {
f54cb97a 579 const UV uv = swash_fetch(rv, s, TRUE);
9b877dbb 580 if (d > dend) {
f54cb97a 581 const STRLEN clen = d - dstart;
582 const STRLEN nlen = dend - dstart + len + UTF8_MAXBYTES;
9b877dbb 583 if (!grows)
7d85a32c 584 Perl_croak(aTHX_ "panic: do_trans_complex_utf8 line %d",__LINE__);
89ebb4a3 585 Renew(dstart, nlen + UTF8_MAXBYTES, U8);
1aa99e6b 586 d = dstart + clen;
587 dend = dstart + nlen;
a0ed51b3 588 }
4757a243 589 if (uv < none) {
590 matches++;
01ec43d0 591 s += UTF8SKIP(s);
7d85a32c 592 d = uvuni_to_utf8(d, uv);
4757a243 593 continue;
a0ed51b3 594 }
4757a243 595 else if (uv == none) { /* "none" is unmapped character */
f54cb97a 596 const int i = UTF8SKIP(s);
0376ff32 597 Move(s, d, i, U8);
9b877dbb 598 d += i;
599 s += i;
4757a243 600 continue;
79072805 601 }
4757a243 602 else if (uv == extra && !del) {
603 matches++;
01ec43d0 604 s += UTF8SKIP(s);
7d85a32c 605 d = uvuni_to_utf8(d, final);
4757a243 606 continue;
607 }
01ec43d0 608 matches++; /* "none+1" is delete character */
609 s += UTF8SKIP(s);
79072805 610 }
4757a243 611 }
16ec844d 612 if (grows || hibit) {
9b877dbb 613 sv_setpvn(sv, (char*)dstart, d - dstart);
614 Safefree(dstart);
16ec844d 615 if (grows && hibit)
616 Safefree(start);
9b877dbb 617 }
618 else {
619 *d = '\0';
620 SvCUR_set(sv, d - dstart);
621 }
1aa99e6b 622 SvUTF8_on(sv);
4757a243 623 SvSETMAGIC(sv);
624
625 return matches;
626}
627
628I32
864dbfa3 629Perl_do_trans(pTHX_ SV *sv)
4757a243 630{
97aff369 631 dVAR;
4757a243 632 STRLEN len;
4373e329 633 const I32 hasutf = (PL_op->op_private &
036b4402 634 (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF));
4757a243 635
7918f24d 636 PERL_ARGS_ASSERT_DO_TRANS;
637
3788ef8f 638 if (SvREADONLY(sv) && !(PL_op->op_private & OPpTRANS_IDENTICAL)) {
765f542d 639 if (SvIsCOW(sv))
640 sv_force_normal_flags(sv, 0);
3788ef8f 641 if (SvREADONLY(sv))
f1f66076 642 Perl_croak(aTHX_ "%s", PL_no_modify);
2233f375 643 }
10516c54 644 (void)SvPV_const(sv, len);
4757a243 645 if (!len)
646 return 0;
d59e14db 647 if (!(PL_op->op_private & OPpTRANS_IDENTICAL)) {
648 if (!SvPOKp(sv))
649 (void)SvPV_force(sv, len);
2de7b02f 650 (void)SvPOK_only_UTF8(sv);
d59e14db 651 }
4757a243 652
cea2e8a9 653 DEBUG_t( Perl_deb(aTHX_ "2.TBL\n"));
4757a243 654
a77e643a 655 switch (PL_op->op_private & ~hasutf & (
656 OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF|OPpTRANS_IDENTICAL|
657 OPpTRANS_SQUASH|OPpTRANS_DELETE|OPpTRANS_COMPLEMENT)) {
4757a243 658 case 0:
01ec43d0 659 if (hasutf)
660 return do_trans_simple_utf8(sv);
661 else
662 return do_trans_simple(sv);
4757a243 663
664 case OPpTRANS_IDENTICAL:
05d340b8 665 case OPpTRANS_IDENTICAL|OPpTRANS_COMPLEMENT:
01ec43d0 666 if (hasutf)
667 return do_trans_count_utf8(sv);
668 else
669 return do_trans_count(sv);
4757a243 670
671 default:
01ec43d0 672 if (hasutf)
036b4402 673 return do_trans_complex_utf8(sv);
4757a243 674 else
036b4402 675 return do_trans_complex(sv);
79072805 676 }
79072805 677}
678
679void
b9ac451d 680Perl_do_join(pTHX_ register SV *sv, SV *delim, register SV **mark, register SV **sp)
79072805 681{
97aff369 682 dVAR;
53c1dcc0 683 SV ** const oldmark = mark;
79072805 684 register I32 items = sp - mark;
79072805 685 register STRLEN len;
463ee0b2 686 STRLEN delimlen;
79072805 687
7918f24d 688 PERL_ARGS_ASSERT_DO_JOIN;
689
b9ac451d 690 (void) SvPV_const(delim, delimlen); /* stringify and get the delimlen */
5ba99574 691 /* SvCUR assumes it's SvPOK() and woe betide you if it's not. */
516a5887 692
79072805 693 mark++;
694 len = (items > 0 ? (delimlen * (items - 1) ) : 0);
862a34c6 695 SvUPGRADE(sv, SVt_PV);
79072805 696 if (SvLEN(sv) < len + items) { /* current length is way too short */
697 while (items-- > 0) {
1426bbf4 698 if (*mark && !SvGAMAGIC(*mark) && SvOK(*mark)) {
f54cb97a 699 STRLEN tmplen;
5c144d81 700 SvPV_const(*mark, tmplen);
463ee0b2 701 len += tmplen;
79072805 702 }
703 mark++;
704 }
705 SvGROW(sv, len + 1); /* so try to pre-extend */
706
707 mark = oldmark;
db7c17d7 708 items = sp - mark;
79072805 709 ++mark;
710 }
711
76f68e9b 712 sv_setpvs(sv, "");
e4803c42 713 /* sv_setpv retains old UTF8ness [perl #24846] */
fb622db0 714 SvUTF8_off(sv);
e4803c42 715
8d6d96c1 716 if (PL_tainting && SvMAGICAL(sv))
717 SvTAINTED_off(sv);
718
463ee0b2 719 if (items-- > 0) {
92d29cee 720 if (*mark)
721 sv_catsv(sv, *mark);
463ee0b2 722 mark++;
723 }
8d6d96c1 724
c512ce4f 725 if (delimlen) {
79072805 726 for (; items > 0; items--,mark++) {
b9ac451d 727 sv_catsv(sv,delim);
79072805 728 sv_catsv(sv,*mark);
729 }
730 }
731 else {
732 for (; items > 0; items--,mark++)
733 sv_catsv(sv,*mark);
734 }
735 SvSETMAGIC(sv);
736}
737
738void
864dbfa3 739Perl_do_sprintf(pTHX_ SV *sv, I32 len, SV **sarg)
79072805 740{
97aff369 741 dVAR;
46fc3d4c 742 STRLEN patlen;
53c1dcc0 743 const char * const pat = SvPV_const(*sarg, patlen);
46fc3d4c 744 bool do_taint = FALSE;
745
7918f24d 746 PERL_ARGS_ASSERT_DO_SPRINTF;
747
5b781b5b 748 SvUTF8_off(sv);
2cf2cfc6 749 if (DO_UTF8(*sarg))
750 SvUTF8_on(sv);
4608196e 751 sv_vsetpvfn(sv, pat, patlen, NULL, sarg + 1, len - 1, &do_taint);
79072805 752 SvSETMAGIC(sv);
46fc3d4c 753 if (do_taint)
754 SvTAINTED_on(sv);
79072805 755}
756
33b45480 757/* currently converts input to bytes if possible, but doesn't sweat failure */
81e118e0 758UV
759Perl_do_vecget(pTHX_ SV *sv, I32 offset, I32 size)
760{
97aff369 761 dVAR;
bbb8a7e0 762 STRLEN srclen, len, uoffset, bitoffs = 0;
10516c54 763 const unsigned char *s = (const unsigned char *) SvPV_const(sv, srclen);
81e118e0 764 UV retnum = 0;
765
7918f24d 766 PERL_ARGS_ASSERT_DO_VECGET;
767
a50d7633 768 if (offset < 0)
61f9802b 769 return 0;
8e84507e 770 if (size < 1 || (size & (size-1))) /* size < 1 or not a power of two */
a50d7633 771 Perl_croak(aTHX_ "Illegal number of bits in vec");
246fae53 772
dcad2880 773 if (SvUTF8(sv))
33b45480 774 (void) Perl_sv_utf8_downgrade(aTHX_ sv, TRUE);
246fae53 775
bbb8a7e0 776 if (size < 8) {
777 bitoffs = ((offset%8)*size)%8;
778 uoffset = offset/(8/size);
779 }
780 else if (size > 8)
781 uoffset = offset*(size/8);
782 else
783 uoffset = offset;
784
785 len = uoffset + (bitoffs + size + 7)/8; /* required number of bytes */
81e118e0 786 if (len > srclen) {
787 if (size <= 8)
788 retnum = 0;
789 else {
81e118e0 790 if (size == 16) {
bb7a0f54 791 if (uoffset >= srclen)
81e118e0 792 retnum = 0;
793 else
bb7a0f54 794 retnum = (UV) s[uoffset] << 8;
81e118e0 795 }
796 else if (size == 32) {
bb7a0f54 797 if (uoffset >= srclen)
81e118e0 798 retnum = 0;
bb7a0f54 799 else if (uoffset + 1 >= srclen)
81e118e0 800 retnum =
bb7a0f54 801 ((UV) s[uoffset ] << 24);
802 else if (uoffset + 2 >= srclen)
81e118e0 803 retnum =
bb7a0f54 804 ((UV) s[uoffset ] << 24) +
805 ((UV) s[uoffset + 1] << 16);
81e118e0 806 else
807 retnum =
bb7a0f54 808 ((UV) s[uoffset ] << 24) +
809 ((UV) s[uoffset + 1] << 16) +
810 ( s[uoffset + 2] << 8);
81e118e0 811 }
d7d93a81 812#ifdef UV_IS_QUAD
c5a0f51a 813 else if (size == 64) {
c5a0f51a 814 if (ckWARN(WARN_PORTABLE))
9014280d 815 Perl_warner(aTHX_ packWARN(WARN_PORTABLE),
c5a0f51a 816 "Bit vector size > 32 non-portable");
bb7a0f54 817 if (uoffset >= srclen)
c5a0f51a 818 retnum = 0;
bb7a0f54 819 else if (uoffset + 1 >= srclen)
c5a0f51a 820 retnum =
bb7a0f54 821 (UV) s[uoffset ] << 56;
822 else if (uoffset + 2 >= srclen)
c5a0f51a 823 retnum =
bb7a0f54 824 ((UV) s[uoffset ] << 56) +
825 ((UV) s[uoffset + 1] << 48);
826 else if (uoffset + 3 >= srclen)
c5a0f51a 827 retnum =
bb7a0f54 828 ((UV) s[uoffset ] << 56) +
829 ((UV) s[uoffset + 1] << 48) +
830 ((UV) s[uoffset + 2] << 40);
831 else if (uoffset + 4 >= srclen)
c5a0f51a 832 retnum =
bb7a0f54 833 ((UV) s[uoffset ] << 56) +
834 ((UV) s[uoffset + 1] << 48) +
835 ((UV) s[uoffset + 2] << 40) +
836 ((UV) s[uoffset + 3] << 32);
837 else if (uoffset + 5 >= srclen)
c5a0f51a 838 retnum =
bb7a0f54 839 ((UV) s[uoffset ] << 56) +
840 ((UV) s[uoffset + 1] << 48) +
841 ((UV) s[uoffset + 2] << 40) +
842 ((UV) s[uoffset + 3] << 32) +
843 ( s[uoffset + 4] << 24);
844 else if (uoffset + 6 >= srclen)
c5a0f51a 845 retnum =
bb7a0f54 846 ((UV) s[uoffset ] << 56) +
847 ((UV) s[uoffset + 1] << 48) +
848 ((UV) s[uoffset + 2] << 40) +
849 ((UV) s[uoffset + 3] << 32) +
850 ((UV) s[uoffset + 4] << 24) +
851 ((UV) s[uoffset + 5] << 16);
c5a0f51a 852 else
8e84507e 853 retnum =
bb7a0f54 854 ((UV) s[uoffset ] << 56) +
855 ((UV) s[uoffset + 1] << 48) +
856 ((UV) s[uoffset + 2] << 40) +
857 ((UV) s[uoffset + 3] << 32) +
858 ((UV) s[uoffset + 4] << 24) +
859 ((UV) s[uoffset + 5] << 16) +
860 ( s[uoffset + 6] << 8);
c5a0f51a 861 }
862#endif
81e118e0 863 }
864 }
865 else if (size < 8)
bbb8a7e0 866 retnum = (s[uoffset] >> bitoffs) & ((1 << size) - 1);
81e118e0 867 else {
81e118e0 868 if (size == 8)
bb7a0f54 869 retnum = s[uoffset];
81e118e0 870 else if (size == 16)
871 retnum =
bb7a0f54 872 ((UV) s[uoffset] << 8) +
873 s[uoffset + 1];
81e118e0 874 else if (size == 32)
875 retnum =
bb7a0f54 876 ((UV) s[uoffset ] << 24) +
877 ((UV) s[uoffset + 1] << 16) +
878 ( s[uoffset + 2] << 8) +
879 s[uoffset + 3];
d7d93a81 880#ifdef UV_IS_QUAD
c5a0f51a 881 else if (size == 64) {
c5a0f51a 882 if (ckWARN(WARN_PORTABLE))
9014280d 883 Perl_warner(aTHX_ packWARN(WARN_PORTABLE),
c5a0f51a 884 "Bit vector size > 32 non-portable");
885 retnum =
bb7a0f54 886 ((UV) s[uoffset ] << 56) +
887 ((UV) s[uoffset + 1] << 48) +
888 ((UV) s[uoffset + 2] << 40) +
889 ((UV) s[uoffset + 3] << 32) +
890 ((UV) s[uoffset + 4] << 24) +
891 ((UV) s[uoffset + 5] << 16) +
892 ( s[uoffset + 6] << 8) +
893 s[uoffset + 7];
c5a0f51a 894 }
895#endif
81e118e0 896 }
897
898 return retnum;
899}
900
33b45480 901/* currently converts input to bytes if possible but doesn't sweat failures,
902 * although it does ensure that the string it clobbers is not marked as
903 * utf8-valid any more
904 */
79072805 905void
864dbfa3 906Perl_do_vecset(pTHX_ SV *sv)
79072805 907{
97aff369 908 dVAR;
bbb8a7e0 909 register I32 offset, bitoffs = 0;
79072805 910 register I32 size;
8990e307 911 register unsigned char *s;
81e118e0 912 register UV lval;
79072805 913 I32 mask;
a0d0e21e 914 STRLEN targlen;
915 STRLEN len;
c4420975 916 SV * const targ = LvTARG(sv);
79072805 917
7918f24d 918 PERL_ARGS_ASSERT_DO_VECSET;
919
8990e307 920 if (!targ)
921 return;
a0d0e21e 922 s = (unsigned char*)SvPV_force(targ, targlen);
246fae53 923 if (SvUTF8(targ)) {
33b45480 924 /* This is handled by the SvPOK_only below...
925 if (!Perl_sv_utf8_downgrade(aTHX_ targ, TRUE))
926 SvUTF8_off(targ);
927 */
928 (void) Perl_sv_utf8_downgrade(aTHX_ targ, TRUE);
246fae53 929 }
930
4ebbc975 931 (void)SvPOK_only(targ);
81e118e0 932 lval = SvUV(sv);
79072805 933 offset = LvTARGOFF(sv);
fe58ced6 934 if (offset < 0)
ed9aa3b7 935 Perl_croak(aTHX_ "Negative offset to vec in lvalue context");
79072805 936 size = LvTARGLEN(sv);
8e84507e 937 if (size < 1 || (size & (size-1))) /* size < 1 or not a power of two */
a50d7633 938 Perl_croak(aTHX_ "Illegal number of bits in vec");
8e84507e 939
bbb8a7e0 940 if (size < 8) {
941 bitoffs = ((offset%8)*size)%8;
942 offset /= 8/size;
943 }
944 else if (size > 8)
945 offset *= size/8;
946
947 len = offset + (bitoffs + size + 7)/8; /* required number of bytes */
a0d0e21e 948 if (len > targlen) {
949 s = (unsigned char*)SvGROW(targ, len + 1);
12ae5dfc 950 (void)memzero((char *)(s + targlen), len - targlen + 1);
a0d0e21e 951 SvCUR_set(targ, len);
952 }
8e84507e 953
79072805 954 if (size < 8) {
955 mask = (1 << size) - 1;
79072805 956 lval &= mask;
bbb8a7e0 957 s[offset] &= ~(mask << bitoffs);
958 s[offset] |= lval << bitoffs;
79072805 959 }
960 else {
961 if (size == 8)
eb160463 962 s[offset ] = (U8)( lval & 0xff);
79072805 963 else if (size == 16) {
eb160463 964 s[offset ] = (U8)((lval >> 8) & 0xff);
965 s[offset+1] = (U8)( lval & 0xff);
79072805 966 }
967 else if (size == 32) {
eb160463 968 s[offset ] = (U8)((lval >> 24) & 0xff);
969 s[offset+1] = (U8)((lval >> 16) & 0xff);
970 s[offset+2] = (U8)((lval >> 8) & 0xff);
971 s[offset+3] = (U8)( lval & 0xff);
c5a0f51a 972 }
d7d93a81 973#ifdef UV_IS_QUAD
c5a0f51a 974 else if (size == 64) {
c5a0f51a 975 if (ckWARN(WARN_PORTABLE))
9014280d 976 Perl_warner(aTHX_ packWARN(WARN_PORTABLE),
c5a0f51a 977 "Bit vector size > 32 non-portable");
eb160463 978 s[offset ] = (U8)((lval >> 56) & 0xff);
979 s[offset+1] = (U8)((lval >> 48) & 0xff);
980 s[offset+2] = (U8)((lval >> 40) & 0xff);
981 s[offset+3] = (U8)((lval >> 32) & 0xff);
982 s[offset+4] = (U8)((lval >> 24) & 0xff);
983 s[offset+5] = (U8)((lval >> 16) & 0xff);
984 s[offset+6] = (U8)((lval >> 8) & 0xff);
985 s[offset+7] = (U8)( lval & 0xff);
79072805 986 }
dc1e3f56 987#endif
79072805 988 }
7bb043c3 989 SvSETMAGIC(targ);
79072805 990}
991
992void
864dbfa3 993Perl_do_chop(pTHX_ register SV *astr, register SV *sv)
79072805 994{
97aff369 995 dVAR;
463ee0b2 996 STRLEN len;
a0d0e21e 997 char *s;
8e84507e 998
7918f24d 999 PERL_ARGS_ASSERT_DO_CHOP;
1000
79072805 1001 if (SvTYPE(sv) == SVt_PVAV) {
a0d0e21e 1002 register I32 i;
502c6561 1003 AV *const av = MUTABLE_AV(sv);
f54cb97a 1004 const I32 max = AvFILL(av);
1005
1006 for (i = 0; i <= max; i++) {
ad64d0ec 1007 sv = MUTABLE_SV(av_fetch(av, i, FALSE));
3280af22 1008 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
a0d0e21e 1009 do_chop(astr, sv);
1010 }
1011 return;
79072805 1012 }
aa854799 1013 else if (SvTYPE(sv) == SVt_PVHV) {
85fbaab2 1014 HV* const hv = MUTABLE_HV(sv);
a0d0e21e 1015 HE* entry;
1016 (void)hv_iterinit(hv);
155aba94 1017 while ((entry = hv_iternext(hv)))
a0d0e21e 1018 do_chop(astr,hv_iterval(hv,entry));
1019 return;
79072805 1020 }
991e6d41 1021 else if (SvREADONLY(sv)) {
1022 if (SvFAKE(sv)) {
1023 /* SV is copy-on-write */
1024 sv_force_normal_flags(sv, 0);
1025 }
1026 if (SvREADONLY(sv))
f1f66076 1027 Perl_croak(aTHX_ "%s", PL_no_modify);
991e6d41 1028 }
565a3db3 1029
1030 if (PL_encoding && !SvUTF8(sv)) {
1031 /* like in do_chomp(), utf8-ize the sv as a side-effect
1032 * if we're using encoding. */
1033 sv_recode_to_utf8(sv, PL_encoding);
1034 }
1035
a0d0e21e 1036 s = SvPV(sv, len);
748a9306 1037 if (len && !SvPOK(sv))
1e968d83 1038 s = SvPV_force_nomg(sv, len);
7e2040f0 1039 if (DO_UTF8(sv)) {
a0ed51b3 1040 if (s && len) {
c4420975 1041 char * const send = s + len;
0bcc34c2 1042 char * const start = s;
a0ed51b3 1043 s = send - 1;
a0dbb045 1044 while (s > start && UTF8_IS_CONTINUATION(*s))
1045 s--;
ace7757b 1046 if (is_utf8_string((U8*)s, send - s)) {
a0dbb045 1047 sv_setpvn(astr, s, send - s);
1048 *s = '\0';
1049 SvCUR_set(sv, s - start);
1050 SvNIOK_off(sv);
1051 SvUTF8_on(astr);
1052 }
a0ed51b3 1053 }
1054 else
76f68e9b 1055 sv_setpvs(astr, "");
a0ed51b3 1056 }
7e2040f0 1057 else if (s && len) {
a0d0e21e 1058 s += --len;
1059 sv_setpvn(astr, s, 1);
1060 *s = '\0';
1061 SvCUR_set(sv, len);
2c19a612 1062 SvUTF8_off(sv);
a0d0e21e 1063 SvNIOK_off(sv);
79072805 1064 }
1065 else
76f68e9b 1066 sv_setpvs(astr, "");
a0d0e21e 1067 SvSETMAGIC(sv);
7e2040f0 1068}
a0d0e21e 1069
1070I32
864dbfa3 1071Perl_do_chomp(pTHX_ register SV *sv)
a0d0e21e 1072{
97aff369 1073 dVAR;
c07a80fd 1074 register I32 count;
a0d0e21e 1075 STRLEN len;
1076 char *s;
c4c87a06 1077 char *temp_buffer = NULL;
a0714e2c 1078 SV* svrecode = NULL;
c07a80fd 1079
7918f24d 1080 PERL_ARGS_ASSERT_DO_CHOMP;
1081
3280af22 1082 if (RsSNARF(PL_rs))
c07a80fd 1083 return 0;
4c5a6083 1084 if (RsRECORD(PL_rs))
1085 return 0;
c07a80fd 1086 count = 0;
a0d0e21e 1087 if (SvTYPE(sv) == SVt_PVAV) {
1088 register I32 i;
502c6561 1089 AV *const av = MUTABLE_AV(sv);
f54cb97a 1090 const I32 max = AvFILL(av);
1091
1092 for (i = 0; i <= max; i++) {
ad64d0ec 1093 sv = MUTABLE_SV(av_fetch(av, i, FALSE));
3280af22 1094 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
a0d0e21e 1095 count += do_chomp(sv);
1096 }
1097 return count;
1098 }
aa854799 1099 else if (SvTYPE(sv) == SVt_PVHV) {
85fbaab2 1100 HV* const hv = MUTABLE_HV(sv);
a0d0e21e 1101 HE* entry;
1102 (void)hv_iterinit(hv);
155aba94 1103 while ((entry = hv_iternext(hv)))
a0d0e21e 1104 count += do_chomp(hv_iterval(hv,entry));
1105 return count;
1106 }
991e6d41 1107 else if (SvREADONLY(sv)) {
1108 if (SvFAKE(sv)) {
1109 /* SV is copy-on-write */
1110 sv_force_normal_flags(sv, 0);
1111 }
1112 if (SvREADONLY(sv))
f1f66076 1113 Perl_croak(aTHX_ "%s", PL_no_modify);
991e6d41 1114 }
a6aa349d 1115
1116 if (PL_encoding) {
1117 if (!SvUTF8(sv)) {
1118 /* XXX, here sv is utf8-ized as a side-effect!
1119 If encoding.pm is used properly, almost string-generating
1120 operations, including literal strings, chr(), input data, etc.
1121 should have been utf8-ized already, right?
1122 */
1123 sv_recode_to_utf8(sv, PL_encoding);
1124 }
1125 }
1126
a0d0e21e 1127 s = SvPV(sv, len);
a0d0e21e 1128 if (s && len) {
1129 s += --len;
3280af22 1130 if (RsPARA(PL_rs)) {
a0d0e21e 1131 if (*s != '\n')
1132 goto nope;
1133 ++count;
1134 while (len && s[-1] == '\n') {
1135 --len;
1136 --s;
1137 ++count;
1138 }
1139 }
a0d0e21e 1140 else {
a6aa349d 1141 STRLEN rslen, rs_charlen;
93524f2b 1142 const char *rsptr = SvPV_const(PL_rs, rslen);
a6aa349d 1143
1144 rs_charlen = SvUTF8(PL_rs)
1145 ? sv_len_utf8(PL_rs)
1146 : rslen;
1147
c4c87a06 1148 if (SvUTF8(PL_rs) != SvUTF8(sv)) {
1149 /* Assumption is that rs is shorter than the scalar. */
1150 if (SvUTF8(PL_rs)) {
1151 /* RS is utf8, scalar is 8 bit. */
1152 bool is_utf8 = TRUE;
1153 temp_buffer = (char*)bytes_from_utf8((U8*)rsptr,
1154 &rslen, &is_utf8);
1155 if (is_utf8) {
1156 /* Cannot downgrade, therefore cannot possibly match
1157 */
1158 assert (temp_buffer == rsptr);
1159 temp_buffer = NULL;
1160 goto nope;
1161 }
1162 rsptr = temp_buffer;
a6aa349d 1163 }
1164 else if (PL_encoding) {
1165 /* RS is 8 bit, encoding.pm is used.
1166 * Do not recode PL_rs as a side-effect. */
1167 svrecode = newSVpvn(rsptr, rslen);
1168 sv_recode_to_utf8(svrecode, PL_encoding);
93524f2b 1169 rsptr = SvPV_const(svrecode, rslen);
a6aa349d 1170 rs_charlen = sv_len_utf8(svrecode);
1171 }
1172 else {
c4c87a06 1173 /* RS is 8 bit, scalar is utf8. */
1174 temp_buffer = (char*)bytes_to_utf8((U8*)rsptr, &rslen);
1175 rsptr = temp_buffer;
1176 }
1177 }
c07a80fd 1178 if (rslen == 1) {
1179 if (*s != *rsptr)
1180 goto nope;
1181 ++count;
1182 }
1183 else {
8c2cee6f 1184 if (len < rslen - 1)
c07a80fd 1185 goto nope;
1186 len -= rslen - 1;
1187 s -= rslen - 1;
36477c24 1188 if (memNE(s, rsptr, rslen))
c07a80fd 1189 goto nope;
a6aa349d 1190 count += rs_charlen;
c07a80fd 1191 }
a0d0e21e 1192 }
8b6b16e7 1193 s = SvPV_force_nolen(sv);
a0d0e21e 1194 SvCUR_set(sv, len);
9b33ce3b 1195 *SvEND(sv) = '\0';
a0d0e21e 1196 SvNIOK_off(sv);
9b33ce3b 1197 SvSETMAGIC(sv);
a0d0e21e 1198 }
1199 nope:
a6aa349d 1200
1201 if (svrecode)
1202 SvREFCNT_dec(svrecode);
1203
c4c87a06 1204 Safefree(temp_buffer);
a0d0e21e 1205 return count;
8e84507e 1206}
79072805 1207
1208void
864dbfa3 1209Perl_do_vop(pTHX_ I32 optype, SV *sv, SV *left, SV *right)
79072805 1210{
97aff369 1211 dVAR;
79072805 1212#ifdef LIBERAL
1213 register long *dl;
1214 register long *ll;
1215 register long *rl;
1216#endif
1217 register char *dc;
463ee0b2 1218 STRLEN leftlen;
1219 STRLEN rightlen;
e62f0680 1220 register const char *lc;
1221 register const char *rc;
bb7a0f54 1222 register STRLEN len;
1223 STRLEN lensave;
e62f0680 1224 const char *lsave;
1225 const char *rsave;
12abf4f0 1226 bool left_utf;
1227 bool right_utf;
bb7a0f54 1228 STRLEN needlen = 0;
0c57e439 1229
7918f24d 1230 PERL_ARGS_ASSERT_DO_VOP;
79072805 1231
1fbd88dc 1232 if (sv != left || (optype != OP_BIT_AND && !SvOK(sv) && !SvGMAGICAL(sv)))
76f68e9b 1233 sv_setpvs(sv, ""); /* avoid undef warning on |= and ^= */
e62f0680 1234 lsave = lc = SvPV_nomg_const(left, leftlen);
1235 rsave = rc = SvPV_nomg_const(right, rightlen);
12abf4f0 1236
1237 /* This need to come after SvPV to ensure that string overloading has
1238 fired off. */
1239
1240 left_utf = DO_UTF8(left);
1241 right_utf = DO_UTF8(right);
1242
1243 if (left_utf && !right_utf) {
1244 /* Avoid triggering overloading again by using temporaries.
1245 Maybe there should be a variant of sv_utf8_upgrade that takes pvn
1246 */
59cd0e26 1247 right = newSVpvn_flags(rsave, rightlen, SVs_TEMP);
12abf4f0 1248 sv_utf8_upgrade(right);
1249 rsave = rc = SvPV_nomg_const(right, rightlen);
1250 right_utf = TRUE;
1251 }
1252 else if (!left_utf && right_utf) {
59cd0e26 1253 left = newSVpvn_flags(lsave, leftlen, SVs_TEMP);
12abf4f0 1254 sv_utf8_upgrade(left);
1255 lsave = lc = SvPV_nomg_const(left, leftlen);
1256 left_utf = TRUE;
1257 }
1258
93a17b20 1259 len = leftlen < rightlen ? leftlen : rightlen;
a0d0e21e 1260 lensave = len;
9fdd7463 1261 SvCUR_set(sv, len);
1262 (void)SvPOK_only(sv);
c9b3c8d0 1263 if ((left_utf || right_utf) && (sv == left || sv == right)) {
1264 needlen = optype == OP_BIT_AND ? len : leftlen + rightlen;
a02a5408 1265 Newxz(dc, needlen + 1, char);
c9b3c8d0 1266 }
1267 else if (SvOK(sv) || SvTYPE(sv) > SVt_PVMG) {
2596d9fe 1268 dc = SvPV_force_nomg_nolen(sv);
bb7a0f54 1269 if (SvLEN(sv) < len + 1) {
1270 dc = SvGROW(sv, len + 1);
ff68c719 1271 (void)memzero(dc + SvCUR(sv), len - SvCUR(sv) + 1);
1272 }
c9b3c8d0 1273 if (optype != OP_BIT_AND && (left_utf || right_utf))
1274 dc = SvGROW(sv, leftlen + rightlen + 1);
ff68c719 1275 }
1276 else {
bb7a0f54 1277 needlen = optype == OP_BIT_AND
1278 ? len : (leftlen > rightlen ? leftlen : rightlen);
a02a5408 1279 Newxz(dc, needlen + 1, char);
aa0a69cb 1280 sv_usepvn_flags(sv, dc, needlen, SV_HAS_TRAILING_NUL);
ff68c719 1281 dc = SvPVX(sv); /* sv_usepvn() calls Renew() */
79072805 1282 }
0c57e439 1283 if (left_utf || right_utf) {
1284 UV duc, luc, ruc;
794a0d33 1285 char *dcorig = dc;
1286 char *dcsave = NULL;
0c57e439 1287 STRLEN lulen = leftlen;
1288 STRLEN rulen = rightlen;
ba210ebe 1289 STRLEN ulen;
0c57e439 1290
1291 switch (optype) {
1292 case OP_BIT_AND:
1293 while (lulen && rulen) {
9041c2e3 1294 luc = utf8n_to_uvchr((U8*)lc, lulen, &ulen, UTF8_ALLOW_ANYUV);
0c57e439 1295 lc += ulen;
1296 lulen -= ulen;
9041c2e3 1297 ruc = utf8n_to_uvchr((U8*)rc, rulen, &ulen, UTF8_ALLOW_ANYUV);
0c57e439 1298 rc += ulen;
1299 rulen -= ulen;
1300 duc = luc & ruc;
9041c2e3 1301 dc = (char*)uvchr_to_utf8((U8*)dc, duc);
0c57e439 1302 }
3ce4c532 1303 if (sv == left || sv == right)
1304 (void)sv_usepvn(sv, dcorig, needlen);
794a0d33 1305 SvCUR_set(sv, dc - dcorig);
0c57e439 1306 break;
1307 case OP_BIT_XOR:
1308 while (lulen && rulen) {
9041c2e3 1309 luc = utf8n_to_uvchr((U8*)lc, lulen, &ulen, UTF8_ALLOW_ANYUV);
0c57e439 1310 lc += ulen;
1311 lulen -= ulen;
9041c2e3 1312 ruc = utf8n_to_uvchr((U8*)rc, rulen, &ulen, UTF8_ALLOW_ANYUV);
0c57e439 1313 rc += ulen;
1314 rulen -= ulen;
1315 duc = luc ^ ruc;
9041c2e3 1316 dc = (char*)uvchr_to_utf8((U8*)dc, duc);
0c57e439 1317 }
1318 goto mop_up_utf;
1319 case OP_BIT_OR:
1320 while (lulen && rulen) {
9041c2e3 1321 luc = utf8n_to_uvchr((U8*)lc, lulen, &ulen, UTF8_ALLOW_ANYUV);
0c57e439 1322 lc += ulen;
1323 lulen -= ulen;
9041c2e3 1324 ruc = utf8n_to_uvchr((U8*)rc, rulen, &ulen, UTF8_ALLOW_ANYUV);
0c57e439 1325 rc += ulen;
1326 rulen -= ulen;
1327 duc = luc | ruc;
9041c2e3 1328 dc = (char*)uvchr_to_utf8((U8*)dc, duc);
0c57e439 1329 }
1330 mop_up_utf:
794a0d33 1331 if (rulen)
1332 dcsave = savepvn(rc, rulen);
1333 else if (lulen)
1334 dcsave = savepvn(lc, lulen);
3ce4c532 1335 if (sv == left || sv == right)
794a0d33 1336 (void)sv_usepvn(sv, dcorig, needlen); /* Uses Renew(). */
1337 SvCUR_set(sv, dc - dcorig);
0c57e439 1338 if (rulen)
794a0d33 1339 sv_catpvn(sv, dcsave, rulen);
0c57e439 1340 else if (lulen)
794a0d33 1341 sv_catpvn(sv, dcsave, lulen);
0c57e439 1342 else
1343 *SvEND(sv) = '\0';
794a0d33 1344 Safefree(dcsave);
0c57e439 1345 break;
41547412 1346 default:
1347 if (sv == left || sv == right)
1348 Safefree(dcorig);
c395bd6c 1349 Perl_croak(aTHX_ "panic: do_vop called for op %u (%s)",
1350 (unsigned)optype, PL_op_name[optype]);
0c57e439 1351 }
1352 SvUTF8_on(sv);
1353 goto finish;
1354 }
1355 else
79072805 1356#ifdef LIBERAL
1357 if (len >= sizeof(long)*4 &&
bb7a0f54 1358 !((unsigned long)dc % sizeof(long)) &&
1359 !((unsigned long)lc % sizeof(long)) &&
1360 !((unsigned long)rc % sizeof(long))) /* It's almost always aligned... */
79072805 1361 {
bb7a0f54 1362 const STRLEN remainder = len % (sizeof(long)*4);
79072805 1363 len /= (sizeof(long)*4);
1364
1365 dl = (long*)dc;
1366 ll = (long*)lc;
1367 rl = (long*)rc;
1368
1369 switch (optype) {
1370 case OP_BIT_AND:
1371 while (len--) {
1372 *dl++ = *ll++ & *rl++;
1373 *dl++ = *ll++ & *rl++;
1374 *dl++ = *ll++ & *rl++;
1375 *dl++ = *ll++ & *rl++;
1376 }
1377 break;
a0d0e21e 1378 case OP_BIT_XOR:
79072805 1379 while (len--) {
1380 *dl++ = *ll++ ^ *rl++;
1381 *dl++ = *ll++ ^ *rl++;
1382 *dl++ = *ll++ ^ *rl++;
1383 *dl++ = *ll++ ^ *rl++;
1384 }
1385 break;
1386 case OP_BIT_OR:
1387 while (len--) {
1388 *dl++ = *ll++ | *rl++;
1389 *dl++ = *ll++ | *rl++;
1390 *dl++ = *ll++ | *rl++;
1391 *dl++ = *ll++ | *rl++;
1392 }
1393 }
1394
1395 dc = (char*)dl;
1396 lc = (char*)ll;
1397 rc = (char*)rl;
1398
1399 len = remainder;
1400 }
1401#endif
a0d0e21e 1402 {
a0d0e21e 1403 switch (optype) {
1404 case OP_BIT_AND:
1405 while (len--)
1406 *dc++ = *lc++ & *rc++;
1a787b95 1407 *dc = '\0';
a0d0e21e 1408 break;
1409 case OP_BIT_XOR:
1410 while (len--)
1411 *dc++ = *lc++ ^ *rc++;
1412 goto mop_up;
1413 case OP_BIT_OR:
1414 while (len--)
1415 *dc++ = *lc++ | *rc++;
1416 mop_up:
1417 len = lensave;
bb7a0f54 1418 if (rightlen > len)
a0d0e21e 1419 sv_catpvn(sv, rsave + len, rightlen - len);
eb160463 1420 else if (leftlen > (STRLEN)len)
a0d0e21e 1421 sv_catpvn(sv, lsave + len, leftlen - len);
4633a7c4 1422 else
1423 *SvEND(sv) = '\0';
a0d0e21e 1424 break;
1425 }
79072805 1426 }
0c57e439 1427finish:
fb73857a 1428 SvTAINT(sv);
79072805 1429}
463ee0b2 1430
1431OP *
cea2e8a9 1432Perl_do_kv(pTHX)
463ee0b2 1433{
97aff369 1434 dVAR;
39644a26 1435 dSP;
85fbaab2 1436 HV * const hv = MUTABLE_HV(POPs);
800e9ae0 1437 HV *keys;
463ee0b2 1438 register HE *entry;
f54cb97a 1439 const I32 gimme = GIMME_V;
1440 const I32 dokv = (PL_op->op_type == OP_RV2HV || PL_op->op_type == OP_PADHV);
1441 const I32 dokeys = dokv || (PL_op->op_type == OP_KEYS);
1442 const I32 dovalues = dokv || (PL_op->op_type == OP_VALUES);
463ee0b2 1443
85581909 1444 if (!hv) {
78f9721b 1445 if (PL_op->op_flags & OPf_MOD || LVRET) { /* lvalue */
85581909 1446 dTARGET; /* make sure to clear its target here */
1447 if (SvTYPE(TARG) == SVt_PVLV)
a0714e2c 1448 LvTARG(TARG) = NULL;
85581909 1449 PUSHs(TARG);
1450 }
463ee0b2 1451 RETURN;
85581909 1452 }
748a9306 1453
6d822dc4 1454 keys = hv;
800e9ae0 1455 (void)hv_iterinit(keys); /* always reset iterator regardless */
748a9306 1456
54310121 1457 if (gimme == G_VOID)
aa689395 1458 RETURN;
1459
54310121 1460 if (gimme == G_SCALAR) {
6ee623d5 1461 IV i;
463ee0b2 1462 dTARGET;
1463
78f9721b 1464 if (PL_op->op_flags & OPf_MOD || LVRET) { /* lvalue */
85581909 1465 if (SvTYPE(TARG) < SVt_PVLV) {
1466 sv_upgrade(TARG, SVt_PVLV);
a0714e2c 1467 sv_magic(TARG, NULL, PERL_MAGIC_nkeys, NULL, 0);
85581909 1468 }
1469 LvTYPE(TARG) = 'k';
ad64d0ec 1470 if (LvTARG(TARG) != (const SV *)keys) {
6ff81951 1471 if (LvTARG(TARG))
1472 SvREFCNT_dec(LvTARG(TARG));
b37c2d43 1473 LvTARG(TARG) = SvREFCNT_inc_simple(keys);
6ff81951 1474 }
85581909 1475 PUSHs(TARG);
1476 RETURN;
1477 }
1478
ad64d0ec 1479 if (! SvTIED_mg((const SV *)keys, PERL_MAGIC_tied) )
81714fb9 1480 {
a98face1 1481 i = HvKEYS(keys);
81714fb9 1482 }
463ee0b2 1483 else {
1484 i = 0;
800e9ae0 1485 while (hv_iternext(keys)) i++;
463ee0b2 1486 }
1487 PUSHi( i );
1488 RETURN;
1489 }
1490
8ed4b672 1491 EXTEND(SP, HvKEYS(keys) * (dokeys + dovalues));
463ee0b2 1492
463ee0b2 1493 PUTBACK; /* hv_iternext and hv_iterval might clobber stack_sp */
155aba94 1494 while ((entry = hv_iternext(keys))) {
463ee0b2 1495 SPAGAIN;
574c8022 1496 if (dokeys) {
890ce7af 1497 SV* const sv = hv_iterkeysv(entry);
574c8022 1498 XPUSHs(sv); /* won't clobber stack_sp */
1499 }
463ee0b2 1500 if (dovalues) {
f54cb97a 1501 SV *tmpstr;
463ee0b2 1502 PUTBACK;
6d822dc4 1503 tmpstr = hv_iterval(hv,entry);
cea2e8a9 1504 DEBUG_H(Perl_sv_setpvf(aTHX_ tmpstr, "%lu%%%d=%lu",
46fc3d4c 1505 (unsigned long)HeHASH(entry),
cdd3ba14 1506 (int)HvMAX(keys)+1,
800e9ae0 1507 (unsigned long)(HeHASH(entry) & HvMAX(keys))));
463ee0b2 1508 SPAGAIN;
46fc3d4c 1509 XPUSHs(tmpstr);
463ee0b2 1510 }
1511 PUTBACK;
1512 }
1513 return NORMAL;
1514}
4e35701f 1515
af3babe4 1516/*
1517 * Local variables:
1518 * c-indentation-style: bsd
1519 * c-basic-offset: 4
1520 * indent-tabs-mode: t
1521 * End:
1522 *
37442d52 1523 * ex: set ts=8 sts=4 sw=4 noet:
1524 */