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