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