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