This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
doop.c: Change out-of-bounds value
[perl5.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
LW
5 *
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
8 *
a0d0e21e
LW
9 */
10
11/*
4ac71550
TC
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
LW
15 */
16
166f8a29 17/* This file contains some common functions needed to carry out certain
347f3823 18 * ops. For example, both pp_sprintf() and pp_prtf() call the function
fc7d284e 19 * do_sprintf() found in this file.
166f8a29
DM
20 */
21
79072805 22#include "EXTERN.h"
864dbfa3 23#define PERL_IN_DOOP_C
79072805
LW
24#include "perl.h"
25
64ca3a65 26#ifndef PERL_MICRO
79072805
LW
27#include <signal.h>
28#endif
29
334c6444
DM
30
31/* Helper function for do_trans().
b8e8e0fc
KW
32 * Handles cases where the search and replacement charlists aren't UTF-8,
33 * aren't identical, and neither the /d nor /s flag is present.
34 *
35 * sv may or may not be utf8. Note that no code point above 255 can possibly
36 * be in the to-translate set
334c6444
DM
37 */
38
f0fd0980 39STATIC Size_t
f534d546 40S_do_trans_simple(pTHX_ SV * const sv, const OPtrans_map * const tbl)
79072805 41{
f0fd0980 42 Size_t matches = 0;
463ee0b2 43 STRLEN len;
9138d6ca 44 U8 *s = (U8*)SvPV_nomg(sv,len);
c395bd6c 45 U8 * const send = s+len;
7918f24d
NC
46
47 PERL_ARGS_ASSERT_DO_TRANS_SIMPLE;
48
1e54db1a 49 /* First, take care of non-UTF-8 input strings, because they're easy */
1aa99e6b 50 if (!SvUTF8(sv)) {
01ec43d0 51 while (s < send) {
e4ee4c1b 52 const short ch = tbl->map[*s];
f54cb97a 53 if (ch >= 0) {
01ec43d0 54 matches++;
c4420975 55 *s = (U8)ch;
01ec43d0 56 }
c4420975 57 s++;
01ec43d0
GS
58 }
59 SvSETMAGIC(sv);
9b877dbb
IH
60 }
61 else {
e4ee4c1b 62 const bool grows = cBOOL(PL_op->op_private & OPpTRANS_GROWS);
c395bd6c
AL
63 U8 *d;
64 U8 *dstart;
65
b8e8e0fc
KW
66 /* Allow for worst-case expansion: Each input byte can become 2. For a
67 * given input character, this happens when it occupies a single byte
68 * under UTF-8, but is to be translated to something that occupies two:
69 * $_="a".chr(400); tr/a/\xFE/, FE needs encoding. */
c395bd6c
AL
70 if (grows)
71 Newx(d, len*2+1, U8);
72 else
73 d = s;
74 dstart = d;
75 while (s < send) {
76 STRLEN ulen;
e4ee4c1b 77 short ch;
c395bd6c
AL
78
79 /* Need to check this, otherwise 128..255 won't match */
80 const UV c = utf8n_to_uvchr(s, send - s, &ulen, UTF8_ALLOW_DEFAULT);
c1048fcf 81 if (c < 0x100 && (ch = tbl->map[c]) >= 0) {
c395bd6c 82 matches++;
e4ee4c1b 83 d = uvchr_to_utf8(d, (UV)ch);
c395bd6c
AL
84 s += ulen;
85 }
86 else { /* No match -> copy */
87 Move(s, d, ulen, U8);
88 d += ulen;
89 s += ulen;
90 }
91 }
92 if (grows) {
93 sv_setpvn(sv, (char*)dstart, d - dstart);
94 Safefree(dstart);
95 }
96 else {
97 *d = '\0';
98 SvCUR_set(sv, d - dstart);
99 }
100 SvUTF8_on(sv);
101 SvSETMAGIC(sv);
9b877dbb 102 }
4757a243
LW
103 return matches;
104}
105
334c6444
DM
106
107/* Helper function for do_trans().
b8e8e0fc
KW
108 * Handles cases where the search and replacement charlists are identical and
109 * non-utf8: so the string isn't modified, and only a count of modifiable
334c6444 110 * chars is needed.
b8e8e0fc
KW
111 *
112 * Note that it doesn't handle /d or /s, since these modify the string even if
113 * the replacement list is empty.
114 *
115 * sv may or may not be utf8. Note that no code point above 255 can possibly
116 * be in the to-translate set
334c6444
DM
117 */
118
f0fd0980 119STATIC Size_t
f534d546 120S_do_trans_count(pTHX_ SV * const sv, const OPtrans_map * const tbl)
4757a243 121{
4757a243 122 STRLEN len;
9138d6ca 123 const U8 *s = (const U8*)SvPV_nomg_const(sv, len);
c395bd6c 124 const U8 * const send = s + len;
f0fd0980 125 Size_t matches = 0;
7918f24d
NC
126
127 PERL_ARGS_ASSERT_DO_TRANS_COUNT;
128
c395bd6c 129 if (!SvUTF8(sv)) {
1aa99e6b 130 while (s < send) {
c1048fcf 131 if (tbl->map[*s++] >= 0)
036b4402 132 matches++;
1aa99e6b 133 }
c395bd6c 134 }
fabdb6c0 135 else {
e4ee4c1b 136 const bool complement = cBOOL(PL_op->op_private & OPpTRANS_COMPLEMENT);
1aa99e6b 137 while (s < send) {
1aa99e6b 138 STRLEN ulen;
9f7f3913 139 const UV c = utf8n_to_uvchr(s, send - s, &ulen, UTF8_ALLOW_DEFAULT);
8973db79 140 if (c < 0x100) {
c1048fcf 141 if (tbl->map[c] >= 0)
8973db79
JH
142 matches++;
143 } else if (complement)
1aa99e6b
IH
144 matches++;
145 s += ulen;
146 }
fabdb6c0 147 }
4757a243
LW
148
149 return matches;
150}
151
334c6444
DM
152
153/* Helper function for do_trans().
b8e8e0fc
KW
154 * Handles cases where the search and replacement charlists aren't identical
155 * and both are non-utf8, and one or both of /d, /s is specified.
156 *
157 * sv may or may not be utf8. Note that no code point above 255 can possibly
158 * be in the to-translate set
334c6444
DM
159 */
160
f0fd0980 161STATIC Size_t
f534d546 162S_do_trans_complex(pTHX_ SV * const sv, const OPtrans_map * const tbl)
4757a243 163{
c395bd6c 164 STRLEN len;
9138d6ca 165 U8 *s = (U8*)SvPV_nomg(sv, len);
c395bd6c 166 U8 * const send = s+len;
f0fd0980 167 Size_t matches = 0;
7918f24d
NC
168
169 PERL_ARGS_ASSERT_DO_TRANS_COMPLEX;
170
c395bd6c
AL
171 if (!SvUTF8(sv)) {
172 U8 *d = s;
173 U8 * const dstart = d;
4757a243 174
1aa99e6b 175 if (PL_op->op_private & OPpTRANS_SQUASH) {
f54cb97a 176 const U8* p = send;
1aa99e6b 177 while (s < send) {
e4ee4c1b 178 const short ch = tbl->map[*s];
f54cb97a 179 if (ch >= 0) {
eb160463 180 *d = (U8)ch;
036b4402 181 matches++;
1aa99e6b
IH
182 if (p != d - 1 || *p != *d)
183 p = d++;
184 }
482bf615 185 else if (ch == (short) TR_UNMAPPED)
2f96a1b4 186 *d++ = *s;
482bf615 187 else if (ch == (short) TR_DELETE)
1aa99e6b
IH
188 matches++;
189 s++;
190 }
a0ed51b3 191 }
b8e8e0fc 192 else { /* Not to squash */
1aa99e6b 193 while (s < send) {
e4ee4c1b 194 const short ch = tbl->map[*s];
f54cb97a 195 if (ch >= 0) {
1aa99e6b 196 matches++;
eb160463 197 *d++ = (U8)ch;
1aa99e6b 198 }
482bf615 199 else if (ch == (short) TR_UNMAPPED)
1aa99e6b 200 *d++ = *s;
482bf615 201 else if (ch == (short) TR_DELETE)
1aa99e6b
IH
202 matches++;
203 s++;
204 }
205 }
76ef7183 206 *d = '\0';
1aa99e6b 207 SvCUR_set(sv, d - dstart);
4757a243 208 }
c395bd6c 209 else { /* is utf8 */
e4ee4c1b
DM
210 const bool squash = cBOOL(PL_op->op_private & OPpTRANS_SQUASH);
211 const bool grows = cBOOL(PL_op->op_private & OPpTRANS_GROWS);
c395bd6c
AL
212 U8 *d;
213 U8 *dstart;
0b9a13c3 214 Size_t size = tbl->size;
b8e8e0fc
KW
215
216 /* What the mapping of the previous character was to. If the new
217 * character has the same mapping, it is squashed from the output (but
218 * still is included in the count) */
47279991 219 UV pch = TR_OOB;
fabdb6c0 220
9b877dbb 221 if (grows)
b8e8e0fc
KW
222 /* Allow for worst-case expansion: Each input byte can become 2.
223 * For a given input character, this happens when it occupies a
224 * single byte under UTF-8, but is to be translated to something
225 * that occupies two: */
a02a5408 226 Newx(d, len*2+1, U8);
9b877dbb
IH
227 else
228 d = s;
1aa99e6b
IH
229 dstart = d;
230
4eaf16e8
DM
231 while (s < send) {
232 STRLEN len;
233 const UV comp = utf8n_to_uvchr(s, send - s, &len,
234 UTF8_ALLOW_DEFAULT);
235 UV ch;
236 short sch;
237
238 sch = tbl->map[comp >= size ? size : comp];
239
240 if (sch >= 0) {
241 ch = (UV)sch;
242 replace:
243 matches++;
244 if (LIKELY(!squash || ch != pch)) {
245 d = uvchr_to_utf8(d, ch);
246 pch = ch;
0b9a13c3 247 }
4eaf16e8
DM
248 s += len;
249 continue;
250 }
482bf615 251 else if (sch == (short) TR_UNMAPPED) {
4eaf16e8
DM
252 Move(s, d, len, U8);
253 d += len;
254 }
482bf615 255 else if (sch == (short) TR_DELETE)
4eaf16e8
DM
256 matches++;
257 else {
482bf615 258 assert(sch == (short) TR_R_EMPTY); /* empty replacement */
4eaf16e8
DM
259 ch = comp;
260 goto replace;
261 }
262
263 s += len;
47279991 264 pch = TR_OOB;
4eaf16e8 265 }
0b9a13c3 266
9b877dbb
IH
267 if (grows) {
268 sv_setpvn(sv, (char*)dstart, d - dstart);
269 Safefree(dstart);
270 }
271 else {
272 *d = '\0';
273 SvCUR_set(sv, d - dstart);
274 }
1aa99e6b 275 SvUTF8_on(sv);
4757a243 276 }
5e44153e 277 SvSETMAGIC(sv);
4757a243
LW
278 return matches;
279}
280
334c6444
DM
281
282/* Helper function for do_trans().
283 * Handles utf8 cases(*) not involving the /c, /d, /s flags,
284 * and where search and replacement charlists aren't identical.
285 * (*) i.e. where the search or replacement charlists are utf8. sv may
286 * or may not be utf8.
287 */
288
f0fd0980 289STATIC Size_t
c395bd6c 290S_do_trans_simple_utf8(pTHX_ SV * const sv)
4757a243 291{
4757a243
LW
292 U8 *s;
293 U8 *send;
294 U8 *d;
036b4402 295 U8 *start;
1aa99e6b 296 U8 *dstart, *dend;
f0fd0980 297 Size_t matches = 0;
e4ee4c1b 298 const bool grows = cBOOL(PL_op->op_private & OPpTRANS_GROWS);
4757a243 299 STRLEN len;
043e41b8
DM
300 SV* const rv =
301#ifdef USE_ITHREADS
302 PAD_SVl(cPADOP->op_padix);
303#else
ad64d0ec 304 MUTABLE_SV(cSVOP->op_sv);
043e41b8 305#endif
85fbaab2 306 HV* const hv = MUTABLE_HV(SvRV(rv));
a4fc7abc 307 SV* const * svp = hv_fetchs(hv, "NONE", FALSE);
f54cb97a
AL
308 const UV none = svp ? SvUV(*svp) : 0x7fffffff;
309 const UV extra = none + 1;
b7953727 310 UV final = 0;
1aa99e6b 311 U8 hibit = 0;
4757a243 312
7918f24d
NC
313 PERL_ARGS_ASSERT_DO_TRANS_SIMPLE_UTF8;
314
9138d6ca 315 s = (U8*)SvPV_nomg(sv, len);
c395bd6c 316 if (!SvUTF8(sv)) {
40b85b80
KW
317 hibit = ! is_utf8_invariant_string(s, len);
318 if (hibit) {
319 s = bytes_to_utf8(s, &len);
c4d5f83a 320 }
1aa99e6b 321 }
4757a243 322 send = s + len;
036b4402 323 start = s;
4757a243 324
a4fc7abc 325 svp = hv_fetchs(hv, "FINAL", FALSE);
4757a243
LW
326 if (svp)
327 final = SvUV(*svp);
328
9b877dbb
IH
329 if (grows) {
330 /* d needs to be bigger than s, in case e.g. upgrading is required */
a02a5408 331 Newx(d, len * 3 + UTF8_MAXBYTES, U8);
9b877dbb
IH
332 dend = d + len * 3;
333 dstart = d;
334 }
335 else {
336 dstart = d = s;
337 dend = d + len;
338 }
1aa99e6b 339
4757a243 340 while (s < send) {
0bcc34c2
AL
341 const UV uv = swash_fetch(rv, s, TRUE);
342 if (uv < none) {
4757a243
LW
343 s += UTF8SKIP(s);
344 matches++;
c80e42f3 345 d = uvchr_to_utf8(d, uv);
4757a243
LW
346 }
347 else if (uv == none) {
4373e329 348 const int i = UTF8SKIP(s);
0376ff32 349 Move(s, d, i, U8);
9b877dbb
IH
350 d += i;
351 s += i;
4757a243
LW
352 }
353 else if (uv == extra) {
4373e329 354 s += UTF8SKIP(s);
4757a243 355 matches++;
c80e42f3 356 d = uvchr_to_utf8(d, final);
4757a243
LW
357 }
358 else
359 s += UTF8SKIP(s);
1aa99e6b 360
9b877dbb 361 if (d > dend) {
4373e329
AL
362 const STRLEN clen = d - dstart;
363 const STRLEN nlen = dend - dstart + len + UTF8_MAXBYTES;
9b877dbb 364 if (!grows)
7d85a32c 365 Perl_croak(aTHX_ "panic: do_trans_simple_utf8 line %d",__LINE__);
89ebb4a3 366 Renew(dstart, nlen + UTF8_MAXBYTES, U8);
1aa99e6b
IH
367 d = dstart + clen;
368 dend = dstart + nlen;
369 }
4757a243 370 }
16ec844d 371 if (grows || hibit) {
9b877dbb
IH
372 sv_setpvn(sv, (char*)dstart, d - dstart);
373 Safefree(dstart);
16ec844d
IH
374 if (grows && hibit)
375 Safefree(start);
9b877dbb
IH
376 }
377 else {
378 *d = '\0';
379 SvCUR_set(sv, d - dstart);
380 }
4757a243 381 SvSETMAGIC(sv);
1aa99e6b 382 SvUTF8_on(sv);
4757a243
LW
383
384 return matches;
385}
386
334c6444
DM
387
388/* Helper function for do_trans().
389 * Handles utf8 cases(*) where search and replacement charlists are
390 * identical: so the string isn't modified, and only a count of modifiable
391 * chars is needed.
392 * Note that it doesn't handle /d or /s, since these modify the string
393 * even if the replacement charlist is empty.
394 * (*) i.e. where the search or replacement charlists are utf8. sv may
395 * or may not be utf8.
396 */
397
f0fd0980 398STATIC Size_t
c395bd6c 399S_do_trans_count_utf8(pTHX_ SV * const sv)
4757a243 400{
93524f2b 401 const U8 *s;
cbbf8932
AL
402 const U8 *start = NULL;
403 const U8 *send;
f0fd0980 404 Size_t matches = 0;
4757a243 405 STRLEN len;
043e41b8
DM
406 SV* const rv =
407#ifdef USE_ITHREADS
408 PAD_SVl(cPADOP->op_padix);
409#else
ad64d0ec 410 MUTABLE_SV(cSVOP->op_sv);
043e41b8 411#endif
85fbaab2 412 HV* const hv = MUTABLE_HV(SvRV(rv));
a4fc7abc 413 SV* const * const svp = hv_fetchs(hv, "NONE", FALSE);
f54cb97a
AL
414 const UV none = svp ? SvUV(*svp) : 0x7fffffff;
415 const UV extra = none + 1;
1aa99e6b 416 U8 hibit = 0;
4757a243 417
7918f24d
NC
418 PERL_ARGS_ASSERT_DO_TRANS_COUNT_UTF8;
419
9138d6ca 420 s = (const U8*)SvPV_nomg_const(sv, len);
1aa99e6b 421 if (!SvUTF8(sv)) {
40b85b80
KW
422 hibit = ! is_utf8_invariant_string(s, len);
423 if (hibit) {
424 start = s = bytes_to_utf8(s, &len);
c4d5f83a 425 }
1aa99e6b 426 }
4757a243
LW
427 send = s + len;
428
429 while (s < send) {
c4420975
AL
430 const UV uv = swash_fetch(rv, s, TRUE);
431 if (uv < none || uv == extra)
4757a243 432 matches++;
834a4ddd 433 s += UTF8SKIP(s);
4757a243 434 }
1aa99e6b
IH
435 if (hibit)
436 Safefree(start);
4757a243
LW
437
438 return matches;
439}
440
334c6444
DM
441
442/* Helper function for do_trans().
443 * Handles utf8 cases(*) involving the /c, /d, /s flags,
444 * and where search and replacement charlists aren't identical.
445 * (*) i.e. where the search or replacement charlists are utf8. sv may
446 * or may not be utf8.
447 */
448
f0fd0980 449STATIC Size_t
c395bd6c 450S_do_trans_complex_utf8(pTHX_ SV * const sv)
4757a243 451{
1aa99e6b 452 U8 *start, *send;
4757a243 453 U8 *d;
f0fd0980 454 Size_t matches = 0;
e4ee4c1b
DM
455 const bool squash = cBOOL(PL_op->op_private & OPpTRANS_SQUASH);
456 const bool del = cBOOL(PL_op->op_private & OPpTRANS_DELETE);
457 const bool grows = cBOOL(PL_op->op_private & OPpTRANS_GROWS);
043e41b8
DM
458 SV* const rv =
459#ifdef USE_ITHREADS
460 PAD_SVl(cPADOP->op_padix);
461#else
ad64d0ec 462 MUTABLE_SV(cSVOP->op_sv);
043e41b8 463#endif
85fbaab2 464 HV * const hv = MUTABLE_HV(SvRV(rv));
a4fc7abc 465 SV * const *svp = hv_fetchs(hv, "NONE", FALSE);
f54cb97a
AL
466 const UV none = svp ? SvUV(*svp) : 0x7fffffff;
467 const UV extra = none + 1;
b7953727 468 UV final = 0;
45005bfb 469 bool havefinal = FALSE;
4757a243 470 STRLEN len;
1aa99e6b 471 U8 *dstart, *dend;
1aa99e6b 472 U8 hibit = 0;
9138d6ca 473 U8 *s = (U8*)SvPV_nomg(sv, len);
7918f24d
NC
474
475 PERL_ARGS_ASSERT_DO_TRANS_COMPLEX_UTF8;
476
c395bd6c 477 if (!SvUTF8(sv)) {
40b85b80
KW
478 hibit = ! is_utf8_invariant_string(s, len);
479 if (hibit) {
480 s = bytes_to_utf8(s, &len);
c4d5f83a 481 }
1aa99e6b 482 }
4757a243 483 send = s + len;
1aa99e6b 484 start = s;
4757a243 485
a4fc7abc 486 svp = hv_fetchs(hv, "FINAL", FALSE);
45005bfb 487 if (svp) {
4757a243 488 final = SvUV(*svp);
45005bfb
JH
489 havefinal = TRUE;
490 }
4757a243 491
9b877dbb
IH
492 if (grows) {
493 /* d needs to be bigger than s, in case e.g. upgrading is required */
a02a5408 494 Newx(d, len * 3 + UTF8_MAXBYTES, U8);
9b877dbb
IH
495 dend = d + len * 3;
496 dstart = d;
497 }
498 else {
499 dstart = d = s;
500 dend = d + len;
501 }
4757a243
LW
502
503 if (squash) {
504 UV puv = 0xfeedface;
505 while (s < send) {
f54cb97a 506 UV uv = swash_fetch(rv, s, TRUE);
2f96a1b4 507
9b877dbb 508 if (d > dend) {
4373e329
AL
509 const STRLEN clen = d - dstart;
510 const STRLEN nlen = dend - dstart + len + UTF8_MAXBYTES;
9b877dbb 511 if (!grows)
7d85a32c 512 Perl_croak(aTHX_ "panic: do_trans_complex_utf8 line %d",__LINE__);
89ebb4a3 513 Renew(dstart, nlen + UTF8_MAXBYTES, U8);
1aa99e6b
IH
514 d = dstart + clen;
515 dend = dstart + nlen;
4757a243
LW
516 }
517 if (uv < none) {
518 matches++;
94472101 519 s += UTF8SKIP(s);
4757a243 520 if (uv != puv) {
c80e42f3 521 d = uvchr_to_utf8(d, uv);
4757a243
LW
522 puv = uv;
523 }
4757a243
LW
524 continue;
525 }
526 else if (uv == none) { /* "none" is unmapped character */
4373e329 527 const int i = UTF8SKIP(s);
0376ff32 528 Move(s, d, i, U8);
9b877dbb
IH
529 d += i;
530 s += i;
4757a243
LW
531 puv = 0xfeedface;
532 continue;
a0ed51b3 533 }
4757a243
LW
534 else if (uv == extra && !del) {
535 matches++;
45005bfb
JH
536 if (havefinal) {
537 s += UTF8SKIP(s);
538 if (puv != final) {
c80e42f3 539 d = uvchr_to_utf8(d, final);
45005bfb
JH
540 puv = final;
541 }
542 }
543 else {
544 STRLEN len;
c80e42f3 545 uv = utf8n_to_uvchr(s, send - s, &len, UTF8_ALLOW_DEFAULT);
45005bfb 546 if (uv != puv) {
0376ff32 547 Move(s, d, len, U8);
45005bfb
JH
548 d += len;
549 puv = uv;
550 }
551 s += len;
4757a243 552 }
4757a243
LW
553 continue;
554 }
01ec43d0
GS
555 matches++; /* "none+1" is delete character */
556 s += UTF8SKIP(s);
a0ed51b3 557 }
79072805
LW
558 }
559 else {
4757a243 560 while (s < send) {
f54cb97a 561 const UV uv = swash_fetch(rv, s, TRUE);
9b877dbb 562 if (d > dend) {
f54cb97a
AL
563 const STRLEN clen = d - dstart;
564 const STRLEN nlen = dend - dstart + len + UTF8_MAXBYTES;
9b877dbb 565 if (!grows)
7d85a32c 566 Perl_croak(aTHX_ "panic: do_trans_complex_utf8 line %d",__LINE__);
89ebb4a3 567 Renew(dstart, nlen + UTF8_MAXBYTES, U8);
1aa99e6b
IH
568 d = dstart + clen;
569 dend = dstart + nlen;
a0ed51b3 570 }
4757a243
LW
571 if (uv < none) {
572 matches++;
01ec43d0 573 s += UTF8SKIP(s);
c80e42f3 574 d = uvchr_to_utf8(d, uv);
4757a243 575 continue;
a0ed51b3 576 }
4757a243 577 else if (uv == none) { /* "none" is unmapped character */
f54cb97a 578 const int i = UTF8SKIP(s);
0376ff32 579 Move(s, d, i, U8);
9b877dbb
IH
580 d += i;
581 s += i;
4757a243 582 continue;
79072805 583 }
4757a243
LW
584 else if (uv == extra && !del) {
585 matches++;
01ec43d0 586 s += UTF8SKIP(s);
c80e42f3 587 d = uvchr_to_utf8(d, final);
4757a243
LW
588 continue;
589 }
01ec43d0
GS
590 matches++; /* "none+1" is delete character */
591 s += UTF8SKIP(s);
79072805 592 }
4757a243 593 }
16ec844d 594 if (grows || hibit) {
9b877dbb
IH
595 sv_setpvn(sv, (char*)dstart, d - dstart);
596 Safefree(dstart);
16ec844d
IH
597 if (grows && hibit)
598 Safefree(start);
9b877dbb
IH
599 }
600 else {
601 *d = '\0';
602 SvCUR_set(sv, d - dstart);
603 }
1aa99e6b 604 SvUTF8_on(sv);
4757a243
LW
605 SvSETMAGIC(sv);
606
607 return matches;
608}
609
334c6444
DM
610
611/* Execute a tr//. sv is the value to be translated, while PL_op
612 * should be an OP_TRANS or OP_TRANSR op, whose op_pv field contains a
613 * translation table or whose op_sv field contains a swash.
614 * Returns a count of number of characters translated
615 */
616
f0fd0980 617Size_t
864dbfa3 618Perl_do_trans(pTHX_ SV *sv)
4757a243
LW
619{
620 STRLEN len;
e4ee4c1b
DM
621 const U8 flags = PL_op->op_private;
622 const U8 hasutf = flags & (OPpTRANS_FROM_UTF | OPpTRANS_TO_UTF);
4757a243 623
7918f24d
NC
624 PERL_ARGS_ASSERT_DO_TRANS;
625
a53bfdae
HS
626 if (SvREADONLY(sv) && !(flags & OPpTRANS_IDENTICAL)) {
627 Perl_croak_no_modify();
2233f375 628 }
10516c54 629 (void)SvPV_const(sv, len);
4757a243
LW
630 if (!len)
631 return 0;
a53bfdae 632 if (!(flags & OPpTRANS_IDENTICAL)) {
4499db73 633 if (!SvPOKp(sv) || SvTHINKFIRST(sv))
b4cc4d79 634 (void)SvPV_force_nomg(sv, len);
2de7b02f 635 (void)SvPOK_only_UTF8(sv);
d59e14db 636 }
4757a243 637
a53bfdae
HS
638 /* If we use only OPpTRANS_IDENTICAL to bypass the READONLY check,
639 * we must also rely on it to choose the readonly strategy.
640 */
641 if (flags & OPpTRANS_IDENTICAL) {
f534d546 642 return hasutf ? do_trans_count_utf8(sv) : do_trans_count(sv, (OPtrans_map*)cPVOP->op_pv);
a53bfdae 643 } else if (flags & (OPpTRANS_SQUASH|OPpTRANS_DELETE|OPpTRANS_COMPLEMENT)) {
f534d546 644 return hasutf ? do_trans_complex_utf8(sv) : do_trans_complex(sv, (OPtrans_map*)cPVOP->op_pv);
a53bfdae 645 } else {
f534d546 646 return hasutf ? do_trans_simple_utf8(sv) : do_trans_simple(sv, (OPtrans_map*)cPVOP->op_pv);
79072805 647 }
79072805
LW
648}
649
650void
5aaab254 651Perl_do_join(pTHX_ SV *sv, SV *delim, SV **mark, SV **sp)
79072805 652{
53c1dcc0 653 SV ** const oldmark = mark;
eb578fdb
KW
654 I32 items = sp - mark;
655 STRLEN len;
463ee0b2 656 STRLEN delimlen;
810a07df 657 const char * const delims = SvPV_const(delim, delimlen);
79072805 658
7918f24d
NC
659 PERL_ARGS_ASSERT_DO_JOIN;
660
79072805
LW
661 mark++;
662 len = (items > 0 ? (delimlen * (items - 1) ) : 0);
862a34c6 663 SvUPGRADE(sv, SVt_PV);
79072805
LW
664 if (SvLEN(sv) < len + items) { /* current length is way too short */
665 while (items-- > 0) {
1426bbf4 666 if (*mark && !SvGAMAGIC(*mark) && SvOK(*mark)) {
f54cb97a 667 STRLEN tmplen;
5c144d81 668 SvPV_const(*mark, tmplen);
463ee0b2 669 len += tmplen;
79072805
LW
670 }
671 mark++;
672 }
673 SvGROW(sv, len + 1); /* so try to pre-extend */
674
675 mark = oldmark;
db7c17d7 676 items = sp - mark;
79072805
LW
677 ++mark;
678 }
679
f607343f 680 SvPVCLEAR(sv);
e4803c42 681 /* sv_setpv retains old UTF8ness [perl #24846] */
fb622db0 682 SvUTF8_off(sv);
e4803c42 683
284167a5 684 if (TAINTING_get && SvMAGICAL(sv))
8d6d96c1
HS
685 SvTAINTED_off(sv);
686
463ee0b2 687 if (items-- > 0) {
92d29cee
JH
688 if (*mark)
689 sv_catsv(sv, *mark);
463ee0b2
LW
690 mark++;
691 }
8d6d96c1 692
c512ce4f 693 if (delimlen) {
810a07df 694 const U32 delimflag = DO_UTF8(delim) ? SV_CATUTF8 : SV_CATBYTES;
79072805 695 for (; items > 0; items--,mark++) {
f0ee3863
FC
696 STRLEN len;
697 const char *s;
810a07df 698 sv_catpvn_flags(sv,delims,delimlen,delimflag);
f0ee3863
FC
699 s = SvPV_const(*mark,len);
700 sv_catpvn_flags(sv,s,len,
701 DO_UTF8(*mark) ? SV_CATUTF8 : SV_CATBYTES);
79072805
LW
702 }
703 }
704 else {
705 for (; items > 0; items--,mark++)
f0ee3863
FC
706 {
707 STRLEN len;
708 const char *s = SvPV_const(*mark,len);
709 sv_catpvn_flags(sv,s,len,
710 DO_UTF8(*mark) ? SV_CATUTF8 : SV_CATBYTES);
711 }
79072805
LW
712 }
713 SvSETMAGIC(sv);
714}
715
716void
03a22d83 717Perl_do_sprintf(pTHX_ SV *sv, SSize_t len, SV **sarg)
79072805 718{
46fc3d4c 719 STRLEN patlen;
53c1dcc0 720 const char * const pat = SvPV_const(*sarg, patlen);
46fc3d4c
PP
721 bool do_taint = FALSE;
722
7918f24d 723 PERL_ARGS_ASSERT_DO_SPRINTF;
03a22d83 724 assert(len >= 1);
7918f24d 725
e06d98fb
DM
726 if (SvTAINTED(*sarg))
727 TAINT_PROPER(
728 (PL_op && PL_op->op_type < OP_max)
729 ? (PL_op->op_type == OP_PRTF)
730 ? "printf"
731 : PL_op_name[PL_op->op_type]
732 : "(unknown)"
733 );
5b781b5b 734 SvUTF8_off(sv);
2cf2cfc6
A
735 if (DO_UTF8(*sarg))
736 SvUTF8_on(sv);
03a22d83 737 sv_vsetpvfn(sv, pat, patlen, NULL, sarg + 1, (Size_t)(len - 1), &do_taint);
79072805 738 SvSETMAGIC(sv);
46fc3d4c
PP
739 if (do_taint)
740 SvTAINTED_on(sv);
79072805
LW
741}
742
33b45480 743/* currently converts input to bytes if possible, but doesn't sweat failure */
81e118e0 744UV
d69c4304 745Perl_do_vecget(pTHX_ SV *sv, STRLEN offset, int size)
81e118e0 746{
67dd6f35 747 STRLEN srclen, len, avail, uoffset, bitoffs = 0;
fc9668ae
DM
748 const I32 svpv_flags = ((PL_op->op_flags & OPf_MOD || LVRET)
749 ? SV_UNDEF_RETURNS_NULL : 0);
750 unsigned char *s = (unsigned char *)
751 SvPV_flags(sv, srclen, (svpv_flags|SV_GMAGIC));
81e118e0
JH
752 UV retnum = 0;
753
032061d2 754 if (!s) {
fc9668ae 755 s = (unsigned char *)"";
032061d2 756 }
2f96a1b4 757
7918f24d
NC
758 PERL_ARGS_ASSERT_DO_VECGET;
759
8e84507e 760 if (size < 1 || (size & (size-1))) /* size < 1 or not a power of two */
a50d7633 761 Perl_croak(aTHX_ "Illegal number of bits in vec");
246fae53 762
fc9668ae 763 if (SvUTF8(sv)) {
27c41eac 764 if (Perl_sv_utf8_downgrade_flags(aTHX_ sv, TRUE, 0)) {
315f3fc1
KW
765 /* PVX may have changed */
766 s = (unsigned char *) SvPV_flags(sv, srclen, svpv_flags);
767 }
768 else {
da5a0da2 769 Perl_croak(aTHX_ "Use of strings with code points over 0xFF as arguments to vec is forbidden");
315f3fc1 770 }
fc9668ae 771 }
246fae53 772
bbb8a7e0
MHM
773 if (size < 8) {
774 bitoffs = ((offset%8)*size)%8;
775 uoffset = offset/(8/size);
776 }
67dd6f35
DM
777 else if (size > 8) {
778 int n = size/8;
779 if (offset > Size_t_MAX / n - 1) /* would overflow */
780 return 0;
781 uoffset = offset*n;
782 }
bbb8a7e0
MHM
783 else
784 uoffset = offset;
785
67dd6f35
DM
786 if (uoffset >= srclen)
787 return 0;
788
789 len = (bitoffs + size + 7)/8; /* required number of bytes */
790 avail = srclen - uoffset; /* available number of bytes */
791
792 /* Does the byte range overlap the end of the string? If so,
793 * handle specially. */
794 if (avail < len) {
81e118e0
JH
795 if (size <= 8)
796 retnum = 0;
797 else {
81e118e0 798 if (size == 16) {
67dd6f35
DM
799 assert(avail == 1);
800 retnum = (UV) s[uoffset] << 8;
81e118e0
JH
801 }
802 else if (size == 32) {
67dd6f35
DM
803 assert(avail >= 1 && avail <= 3);
804 if (avail == 1)
81e118e0 805 retnum =
bb7a0f54 806 ((UV) s[uoffset ] << 24);
67dd6f35 807 else if (avail == 2)
81e118e0 808 retnum =
bb7a0f54
MHM
809 ((UV) s[uoffset ] << 24) +
810 ((UV) s[uoffset + 1] << 16);
81e118e0
JH
811 else
812 retnum =
bb7a0f54
MHM
813 ((UV) s[uoffset ] << 24) +
814 ((UV) s[uoffset + 1] << 16) +
815 ( s[uoffset + 2] << 8);
81e118e0 816 }
d7d93a81 817#ifdef UV_IS_QUAD
c5a0f51a 818 else if (size == 64) {
a2a5de95
NC
819 Perl_ck_warner(aTHX_ packWARN(WARN_PORTABLE),
820 "Bit vector size > 32 non-portable");
67dd6f35
DM
821 assert(avail >= 1 && avail <= 7);
822 if (avail == 1)
c5a0f51a 823 retnum =
bb7a0f54 824 (UV) s[uoffset ] << 56;
67dd6f35 825 else if (avail == 2)
c5a0f51a 826 retnum =
bb7a0f54
MHM
827 ((UV) s[uoffset ] << 56) +
828 ((UV) s[uoffset + 1] << 48);
67dd6f35 829 else if (avail == 3)
c5a0f51a 830 retnum =
bb7a0f54
MHM
831 ((UV) s[uoffset ] << 56) +
832 ((UV) s[uoffset + 1] << 48) +
833 ((UV) s[uoffset + 2] << 40);
67dd6f35 834 else if (avail == 4)
c5a0f51a 835 retnum =
bb7a0f54
MHM
836 ((UV) s[uoffset ] << 56) +
837 ((UV) s[uoffset + 1] << 48) +
838 ((UV) s[uoffset + 2] << 40) +
839 ((UV) s[uoffset + 3] << 32);
67dd6f35 840 else if (avail == 5)
c5a0f51a 841 retnum =
bb7a0f54
MHM
842 ((UV) s[uoffset ] << 56) +
843 ((UV) s[uoffset + 1] << 48) +
844 ((UV) s[uoffset + 2] << 40) +
845 ((UV) s[uoffset + 3] << 32) +
e7aca353 846 ((UV) s[uoffset + 4] << 24);
67dd6f35 847 else if (avail == 6)
c5a0f51a 848 retnum =
bb7a0f54
MHM
849 ((UV) s[uoffset ] << 56) +
850 ((UV) s[uoffset + 1] << 48) +
851 ((UV) s[uoffset + 2] << 40) +
852 ((UV) s[uoffset + 3] << 32) +
853 ((UV) s[uoffset + 4] << 24) +
854 ((UV) s[uoffset + 5] << 16);
c5a0f51a 855 else
8e84507e 856 retnum =
bb7a0f54
MHM
857 ((UV) s[uoffset ] << 56) +
858 ((UV) s[uoffset + 1] << 48) +
859 ((UV) s[uoffset + 2] << 40) +
860 ((UV) s[uoffset + 3] << 32) +
861 ((UV) s[uoffset + 4] << 24) +
862 ((UV) s[uoffset + 5] << 16) +
e7aca353 863 ((UV) s[uoffset + 6] << 8);
c5a0f51a
JH
864 }
865#endif
81e118e0
JH
866 }
867 }
868 else if (size < 8)
bbb8a7e0 869 retnum = (s[uoffset] >> bitoffs) & ((1 << size) - 1);
81e118e0 870 else {
81e118e0 871 if (size == 8)
bb7a0f54 872 retnum = s[uoffset];
81e118e0
JH
873 else if (size == 16)
874 retnum =
bb7a0f54
MHM
875 ((UV) s[uoffset] << 8) +
876 s[uoffset + 1];
81e118e0
JH
877 else if (size == 32)
878 retnum =
bb7a0f54
MHM
879 ((UV) s[uoffset ] << 24) +
880 ((UV) s[uoffset + 1] << 16) +
881 ( s[uoffset + 2] << 8) +
882 s[uoffset + 3];
d7d93a81 883#ifdef UV_IS_QUAD
c5a0f51a 884 else if (size == 64) {
a2a5de95
NC
885 Perl_ck_warner(aTHX_ packWARN(WARN_PORTABLE),
886 "Bit vector size > 32 non-portable");
c5a0f51a 887 retnum =
bb7a0f54
MHM
888 ((UV) s[uoffset ] << 56) +
889 ((UV) s[uoffset + 1] << 48) +
890 ((UV) s[uoffset + 2] << 40) +
891 ((UV) s[uoffset + 3] << 32) +
892 ((UV) s[uoffset + 4] << 24) +
893 ((UV) s[uoffset + 5] << 16) +
894 ( s[uoffset + 6] << 8) +
895 s[uoffset + 7];
c5a0f51a
JH
896 }
897#endif
81e118e0
JH
898 }
899
900 return retnum;
901}
902
33b45480
SB
903/* currently converts input to bytes if possible but doesn't sweat failures,
904 * although it does ensure that the string it clobbers is not marked as
905 * utf8-valid any more
906 */
79072805 907void
864dbfa3 908Perl_do_vecset(pTHX_ SV *sv)
79072805 909{
67dd6f35 910 STRLEN offset, bitoffs = 0;
eb578fdb
KW
911 int size;
912 unsigned char *s;
913 UV lval;
79072805 914 I32 mask;
a0d0e21e
LW
915 STRLEN targlen;
916 STRLEN len;
c4420975 917 SV * const targ = LvTARG(sv);
1b92e694 918 char errflags = LvFLAGS(sv);
79072805 919
7918f24d
NC
920 PERL_ARGS_ASSERT_DO_VECSET;
921
1b92e694
DM
922 /* some out-of-range errors have been deferred if/until the LV is
923 * actually written to: f(vec($s,-1,8)) is not always fatal */
924 if (errflags) {
b063b0a8
DIM
925 assert(!(errflags & ~(LVf_NEG_OFF|LVf_OUT_OF_RANGE)));
926 if (errflags & LVf_NEG_OFF)
1b92e694
DM
927 Perl_croak_nocontext("Negative offset to vec in lvalue context");
928 Perl_croak_nocontext("Out of memory!");
929 }
930
8990e307
LW
931 if (!targ)
932 return;
032061d2
BF
933 s = (unsigned char*)SvPV_force_flags(targ, targlen,
934 SV_GMAGIC | SV_UNDEF_RETURNS_NULL);
246fae53 935 if (SvUTF8(targ)) {
33b45480 936 /* This is handled by the SvPOK_only below...
27c41eac 937 if (!Perl_sv_utf8_downgrade_flags(aTHX_ targ, TRUE, 0))
33b45480
SB
938 SvUTF8_off(targ);
939 */
27c41eac 940 (void) Perl_sv_utf8_downgrade_flags(aTHX_ targ, TRUE, 0);
246fae53
MG
941 }
942
4ebbc975 943 (void)SvPOK_only(targ);
81e118e0 944 lval = SvUV(sv);
79072805
LW
945 offset = LvTARGOFF(sv);
946 size = LvTARGLEN(sv);
67dd6f35 947
8e84507e 948 if (size < 1 || (size & (size-1))) /* size < 1 or not a power of two */
a50d7633 949 Perl_croak(aTHX_ "Illegal number of bits in vec");
8e84507e 950
bbb8a7e0
MHM
951 if (size < 8) {
952 bitoffs = ((offset%8)*size)%8;
953 offset /= 8/size;
954 }
67dd6f35
DM
955 else if (size > 8) {
956 int n = size/8;
957 if (offset > Size_t_MAX / n - 1) /* would overflow */
958 Perl_croak_nocontext("Out of memory!");
959 offset *= n;
960 }
bbb8a7e0 961
67dd6f35
DM
962 len = (bitoffs + size + 7)/8; /* required number of bytes */
963 if (targlen < offset || targlen - offset < len) {
964 STRLEN newlen = offset > Size_t_MAX - len - 1 ? /* avoid overflow */
965 Size_t_MAX : offset + len + 1;
966 s = (unsigned char*)SvGROW(targ, newlen);
967 (void)memzero((char *)(s + targlen), newlen - targlen);
968 SvCUR_set(targ, newlen - 1);
a0d0e21e 969 }
8e84507e 970
79072805
LW
971 if (size < 8) {
972 mask = (1 << size) - 1;
79072805 973 lval &= mask;
bbb8a7e0
MHM
974 s[offset] &= ~(mask << bitoffs);
975 s[offset] |= lval << bitoffs;
79072805
LW
976 }
977 else {
978 if (size == 8)
eb160463 979 s[offset ] = (U8)( lval & 0xff);
79072805 980 else if (size == 16) {
eb160463
GS
981 s[offset ] = (U8)((lval >> 8) & 0xff);
982 s[offset+1] = (U8)( lval & 0xff);
79072805
LW
983 }
984 else if (size == 32) {
eb160463
GS
985 s[offset ] = (U8)((lval >> 24) & 0xff);
986 s[offset+1] = (U8)((lval >> 16) & 0xff);
987 s[offset+2] = (U8)((lval >> 8) & 0xff);
988 s[offset+3] = (U8)( lval & 0xff);
c5a0f51a 989 }
d7d93a81 990#ifdef UV_IS_QUAD
c5a0f51a 991 else if (size == 64) {
a2a5de95
NC
992 Perl_ck_warner(aTHX_ packWARN(WARN_PORTABLE),
993 "Bit vector size > 32 non-portable");
eb160463
GS
994 s[offset ] = (U8)((lval >> 56) & 0xff);
995 s[offset+1] = (U8)((lval >> 48) & 0xff);
996 s[offset+2] = (U8)((lval >> 40) & 0xff);
997 s[offset+3] = (U8)((lval >> 32) & 0xff);
998 s[offset+4] = (U8)((lval >> 24) & 0xff);
999 s[offset+5] = (U8)((lval >> 16) & 0xff);
1000 s[offset+6] = (U8)((lval >> 8) & 0xff);
1001 s[offset+7] = (U8)( lval & 0xff);
79072805 1002 }
dc1e3f56 1003#endif
79072805 1004 }
7bb043c3 1005 SvSETMAGIC(targ);
79072805
LW
1006}
1007
1008void
864dbfa3 1009Perl_do_vop(pTHX_ I32 optype, SV *sv, SV *left, SV *right)
79072805 1010{
eb578fdb
KW
1011 long *dl;
1012 long *ll;
1013 long *rl;
eb578fdb 1014 char *dc;
463ee0b2
LW
1015 STRLEN leftlen;
1016 STRLEN rightlen;
eb578fdb
KW
1017 const char *lc;
1018 const char *rc;
b404a7f5 1019 STRLEN len = 0;
bb7a0f54 1020 STRLEN lensave;
e62f0680
NC
1021 const char *lsave;
1022 const char *rsave;
bb7a0f54 1023 STRLEN needlen = 0;
08b6664b 1024 bool result_needs_to_be_utf8 = FALSE;
b50535da
KW
1025 bool left_utf8 = FALSE;
1026 bool right_utf8 = FALSE;
1027 U8 * left_non_downgraded = NULL;
1028 U8 * right_non_downgraded = NULL;
1029 Size_t left_non_downgraded_len = 0;
1030 Size_t right_non_downgraded_len = 0;
1031 char * non_downgraded = NULL;
1032 Size_t non_downgraded_len = 0;
0c57e439 1033
7918f24d 1034 PERL_ARGS_ASSERT_DO_VOP;
79072805 1035
6b349a5c 1036 if (sv != left || (optype != OP_BIT_AND && !SvOK(sv)))
f607343f 1037 SvPVCLEAR(sv); /* avoid undef warning on |= and ^= */
8c8eee82 1038 if (sv == left) {
08b6664b 1039 lc = SvPV_force_nomg(left, leftlen);
8c8eee82
BM
1040 }
1041 else {
08b6664b 1042 lc = SvPV_nomg_const(left, leftlen);
8c8eee82
BM
1043 SvPV_force_nomg_nolen(sv);
1044 }
08b6664b 1045 rc = SvPV_nomg_const(right, rightlen);
12abf4f0 1046
0e6e7171 1047 /* This needs to come after SvPV to ensure that string overloading has
12abf4f0
NC
1048 fired off. */
1049
08b6664b
KW
1050 /* Create downgraded temporaries of any UTF-8 encoded operands */
1051 if (DO_UTF8(left)) {
b50535da 1052 const U8 * save_lc = (U8 *) lc;
08b6664b 1053
b50535da 1054 left_utf8 = TRUE;
08b6664b
KW
1055 result_needs_to_be_utf8 = TRUE;
1056
b50535da
KW
1057 left_non_downgraded_len = leftlen;
1058 lc = (char *) bytes_from_utf8_loc((const U8 *) lc, &leftlen,
1059 &left_utf8,
1060 (const U8 **) &left_non_downgraded);
1061 /* Calculate the number of trailing unconvertible bytes. This quantity
1062 * is the original length minus the length of the converted portion. */
1063 left_non_downgraded_len -= left_non_downgraded - save_lc;
1064 SAVEFREEPV(lc);
12abf4f0 1065 }
08b6664b 1066 if (DO_UTF8(right)) {
b50535da 1067 const U8 * save_rc = (U8 *) rc;
08b6664b 1068
b50535da 1069 right_utf8 = TRUE;
08b6664b
KW
1070 result_needs_to_be_utf8 = TRUE;
1071
b50535da
KW
1072 right_non_downgraded_len = rightlen;
1073 rc = (char *) bytes_from_utf8_loc((const U8 *) rc, &rightlen,
1074 &right_utf8,
1075 (const U8 **) &right_non_downgraded);
1076 right_non_downgraded_len -= right_non_downgraded - save_rc;
1077 SAVEFREEPV(rc);
1078 }
1079
1080 /* We set 'len' to the length that the operation actually operates on. The
1081 * dangling part of the longer operand doesn't actually participate in the
1082 * operation. What happens is that we pretend that the shorter operand has
1083 * been extended to the right by enough imaginary zeros to match the length
1084 * of the longer one. But we know in advance the result of the operation
1085 * on zeros without having to do it. In the case of '&', the result is
1086 * zero, and the dangling portion is simply discarded. For '|' and '^', the
1087 * result is the same as the other operand, so the dangling part is just
c8b94fe0
JK
1088 * appended to the final result, unchanged. As of perl-5.32, we no longer
1089 * accept above-FF code points in the dangling portion.
1090 */
ba52ce15 1091 if (left_utf8 || right_utf8) {
c8b94fe0 1092 Perl_croak(aTHX_ FATAL_ABOVE_FF_MSG, PL_op_desc[optype]);
ba52ce15 1093 }
b50535da 1094 else { /* Neither is UTF-8 */
78ba9007 1095 len = MIN(leftlen, rightlen);
12abf4f0
NC
1096 }
1097
b50535da 1098 lensave = len;
08b6664b
KW
1099 lsave = lc;
1100 rsave = rc;
b50535da 1101
9fdd7463
JH
1102 SvCUR_set(sv, len);
1103 (void)SvPOK_only(sv);
08b6664b 1104 if (SvOK(sv) || SvTYPE(sv) > SVt_PVMG) {
2596d9fe 1105 dc = SvPV_force_nomg_nolen(sv);
bb7a0f54
MHM
1106 if (SvLEN(sv) < len + 1) {
1107 dc = SvGROW(sv, len + 1);
ff68c719
PP
1108 (void)memzero(dc + SvCUR(sv), len - SvCUR(sv) + 1);
1109 }
1110 }
1111 else {
bb7a0f54
MHM
1112 needlen = optype == OP_BIT_AND
1113 ? len : (leftlen > rightlen ? leftlen : rightlen);
a02a5408 1114 Newxz(dc, needlen + 1, char);
aa0a69cb 1115 sv_usepvn_flags(sv, dc, needlen, SV_HAS_TRAILING_NUL);
ff68c719 1116 dc = SvPVX(sv); /* sv_usepvn() calls Renew() */
79072805 1117 }
0c57e439 1118
79072805 1119 if (len >= sizeof(long)*4 &&
d398c6bf
TK
1120 !(PTR2nat(dc) % sizeof(long)) &&
1121 !(PTR2nat(lc) % sizeof(long)) &&
1122 !(PTR2nat(rc) % sizeof(long))) /* It's almost always aligned... */
79072805 1123 {
bb7a0f54 1124 const STRLEN remainder = len % (sizeof(long)*4);
79072805
LW
1125 len /= (sizeof(long)*4);
1126
1127 dl = (long*)dc;
1128 ll = (long*)lc;
1129 rl = (long*)rc;
1130
1131 switch (optype) {
1132 case OP_BIT_AND:
1133 while (len--) {
1134 *dl++ = *ll++ & *rl++;
1135 *dl++ = *ll++ & *rl++;
1136 *dl++ = *ll++ & *rl++;
1137 *dl++ = *ll++ & *rl++;
1138 }
1139 break;
a0d0e21e 1140 case OP_BIT_XOR:
79072805
LW
1141 while (len--) {
1142 *dl++ = *ll++ ^ *rl++;
1143 *dl++ = *ll++ ^ *rl++;
1144 *dl++ = *ll++ ^ *rl++;
1145 *dl++ = *ll++ ^ *rl++;
1146 }
1147 break;
1148 case OP_BIT_OR:
1149 while (len--) {
1150 *dl++ = *ll++ | *rl++;
1151 *dl++ = *ll++ | *rl++;
1152 *dl++ = *ll++ | *rl++;
1153 *dl++ = *ll++ | *rl++;
1154 }
1155 }
1156
1157 dc = (char*)dl;
1158 lc = (char*)ll;
1159 rc = (char*)rl;
1160
1161 len = remainder;
1162 }
17d44595 1163
27a9d47d
KW
1164 switch (optype) {
1165 case OP_BIT_AND:
1166 while (len--)
1167 *dc++ = *lc++ & *rc++;
1168 *dc = '\0';
1169 break;
1170 case OP_BIT_XOR:
1171 while (len--)
1172 *dc++ = *lc++ ^ *rc++;
1173 goto mop_up;
1174 case OP_BIT_OR:
1175 while (len--)
1176 *dc++ = *lc++ | *rc++;
1177 mop_up:
1178 len = lensave;
1179 if (rightlen > len) {
1180 if (dc == rc)
2324bdb9 1181 SvCUR_set(sv, rightlen);
27a9d47d
KW
1182 else
1183 sv_catpvn_nomg(sv, rsave + len, rightlen - len);
1184 }
1185 else if (leftlen > len) {
1186 if (dc == lc)
2324bdb9 1187 SvCUR_set(sv, leftlen);
27a9d47d
KW
1188 else
1189 sv_catpvn_nomg(sv, lsave + len, leftlen - len);
1190 }
1191 *SvEND(sv) = '\0';
392582f8 1192
b50535da
KW
1193 /* If there is trailing stuff that couldn't be converted from UTF-8, it
1194 * is appended as-is for the ^ and | operators. This preserves
1195 * backwards compatibility */
1196 if (right_non_downgraded) {
1197 non_downgraded = (char *) right_non_downgraded;
1198 non_downgraded_len = right_non_downgraded_len;
1199 }
1200 else if (left_non_downgraded) {
1201 non_downgraded = (char *) left_non_downgraded;
1202 non_downgraded_len = left_non_downgraded_len;
1203 }
1204
27a9d47d
KW
1205 break;
1206 }
08b6664b
KW
1207
1208 if (result_needs_to_be_utf8) {
b50535da
KW
1209 sv_utf8_upgrade_nomg(sv);
1210
1211 /* Append any trailing UTF-8 as-is. */
1212 if (non_downgraded) {
1213 sv_catpvn_nomg(sv, non_downgraded, non_downgraded_len);
1214 }
79072805 1215 }
08b6664b 1216
fb73857a 1217 SvTAINT(sv);
79072805 1218}
463ee0b2 1219
b1c05ba5 1220
a2232057
DM
1221/* Perl_do_kv() may be:
1222 * * called directly as the pp function for pp_keys() and pp_values();
a2232057
DM
1223 * * It may also be called directly when the op is OP_AVHVSWITCH, to
1224 * implement CORE::keys(), CORE::values().
1225 *
1226 * In all cases it expects an HV on the stack and returns a list of keys,
1227 * values, or key-value pairs, depending on PL_op.
1228 */
b1c05ba5 1229
463ee0b2 1230OP *
cea2e8a9 1231Perl_do_kv(pTHX)
463ee0b2 1232{
39644a26 1233 dSP;
73ff03e8 1234 HV * const keys = MUTABLE_HV(POPs);
1c23e2bd 1235 const U8 gimme = GIMME_V;
a2232057 1236
af3b1cba 1237 const I32 dokeys = (PL_op->op_type == OP_KEYS)
a2232057 1238 || ( PL_op->op_type == OP_AVHVSWITCH
94184451
DM
1239 && (PL_op->op_private & OPpAVHVSWITCH_MASK)
1240 + OP_EACH == OP_KEYS);
a2232057 1241
af3b1cba 1242 const I32 dovalues = (PL_op->op_type == OP_VALUES)
a2232057 1243 || ( PL_op->op_type == OP_AVHVSWITCH
94184451
DM
1244 && (PL_op->op_private & OPpAVHVSWITCH_MASK)
1245 + OP_EACH == OP_VALUES);
a2232057 1246
af3b1cba 1247 assert( PL_op->op_type == OP_KEYS
a2232057
DM
1248 || PL_op->op_type == OP_VALUES
1249 || PL_op->op_type == OP_AVHVSWITCH);
463ee0b2 1250
4fa080db
DM
1251 assert(!( PL_op->op_type == OP_VALUES
1252 && (PL_op->op_private & OPpMAYBE_LVSUB)));
1253
800e9ae0 1254 (void)hv_iterinit(keys); /* always reset iterator regardless */
748a9306 1255
54310121 1256 if (gimme == G_VOID)
aa689395
PP
1257 RETURN;
1258
54310121 1259 if (gimme == G_SCALAR) {
78f9721b 1260 if (PL_op->op_flags & OPf_MOD || LVRET) { /* lvalue */
2154eca7
EB
1261 SV * const ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
1262 sv_magic(ret, NULL, PERL_MAGIC_nkeys, NULL, 0);
1263 LvTYPE(ret) = 'k';
1264 LvTARG(ret) = SvREFCNT_inc_simple(keys);
1265 PUSHs(ret);
81714fb9 1266 }
463ee0b2 1267 else {
2154eca7
EB
1268 IV i;
1269 dTARGET;
1270
af3b1cba
DM
1271 /* note that in 'scalar(keys %h)' the OP_KEYS is usually
1272 * optimised away and the action is performed directly by the
1273 * padhv or rv2hv op. We now only get here via OP_AVHVSWITCH
1274 * and \&CORE::keys
1275 */
2154eca7 1276 if (! SvTIED_mg((const SV *)keys, PERL_MAGIC_tied) ) {
1b95d04f 1277 i = HvUSEDKEYS(keys);
2154eca7
EB
1278 }
1279 else {
1280 i = 0;
1281 while (hv_iternext(keys)) i++;
1282 }
1283 PUSHi( i );
463ee0b2 1284 }
463ee0b2
LW
1285 RETURN;
1286 }
1287
a061ab0b
FC
1288 if (UNLIKELY(PL_op->op_private & OPpMAYBE_LVSUB)) {
1289 const I32 flags = is_lvalue_sub();
1290 if (flags && !(flags & OPpENTERSUB_INARGS))
1291 /* diag_listed_as: Can't modify %s in %s */
1292 Perl_croak(aTHX_ "Can't modify keys in list assignment");
1293 }
1294
8dc9003f
DM
1295 PUTBACK;
1296 hv_pushkv(keys, (dokeys | (dovalues << 1)));
1297 return NORMAL;
463ee0b2 1298}
4e35701f 1299
af3babe4 1300/*
14d04a33 1301 * ex: set ts=8 sts=4 sw=4 et:
37442d52 1302 */