This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
(perl #131136) clear padding before writing long doubles
[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
f0fd0980 38STATIC Size_t
c395bd6c 39S_do_trans_simple(pTHX_ SV * const sv)
79072805 40{
f0fd0980 41 Size_t matches = 0;
463ee0b2 42 STRLEN len;
9138d6ca 43 U8 *s = (U8*)SvPV_nomg(sv,len);
c395bd6c 44 U8 * const send = s+len;
c1048fcf 45 const OPtrans_map * const tbl = (OPtrans_map*)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) {
e4ee4c1b 55 const short ch = tbl->map[*s];
f54cb97a 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 {
e4ee4c1b 65 const bool grows = cBOOL(PL_op->op_private & OPpTRANS_GROWS);
c395bd6c
AL
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;
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().
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
f0fd0980 117STATIC Size_t
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 122 const U8 * const send = s + len;
f0fd0980 123 Size_t matches = 0;
c1048fcf 124 const OPtrans_map * const tbl = (OPtrans_map*)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 132 while (s < send) {
c1048fcf 133 if (tbl->map[*s++] >= 0)
036b4402 134 matches++;
1aa99e6b 135 }
c395bd6c 136 }
fabdb6c0 137 else {
e4ee4c1b 138 const bool complement = cBOOL(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 142 if (c < 0x100) {
c1048fcf 143 if (tbl->map[c] >= 0)
8973db79
JH
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
f0fd0980 162STATIC Size_t
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;
f0fd0980 168 Size_t matches = 0;
0b9a13c3 169 const OPtrans_map * const tbl = (OPtrans_map*)cPVOP->op_pv;
7918f24d
NC
170
171 PERL_ARGS_ASSERT_DO_TRANS_COMPLEX;
172
0b9a13c3 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) {
e4ee4c1b 183 const short ch = tbl->map[*s];
f54cb97a 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) {
e4ee4c1b 199 const short ch = tbl->map[*s];
f54cb97a 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 */
e4ee4c1b
DM
215 const bool squash = cBOOL(PL_op->op_private & OPpTRANS_SQUASH);
216 const bool grows = cBOOL(PL_op->op_private & OPpTRANS_GROWS);
c395bd6c
AL
217 U8 *d;
218 U8 *dstart;
0b9a13c3
DM
219 Size_t size = tbl->size;
220 UV pch = 0xfeedface;
fabdb6c0 221
9b877dbb 222 if (grows)
a02a5408 223 Newx(d, len*2+1, U8);
9b877dbb
IH
224 else
225 d = s;
1aa99e6b
IH
226 dstart = d;
227
1aa99e6b
IH
228 while (s < send) {
229 STRLEN len;
9f7f3913
TS
230 const UV comp = utf8n_to_uvchr(s, send - s, &len,
231 UTF8_ALLOW_DEFAULT);
f146bab6
DM
232 UV ch;
233 short sch;
1aa99e6b 234
0b9a13c3
DM
235 sch = tbl->map[comp >= size ? size : comp];
236
237 if (sch >= 0) {
f146bab6 238 ch = (UV)sch;
0b9a13c3 239 replace:
1aa99e6b 240 matches++;
0b9a13c3 241 if (LIKELY(!squash || ch != pch)) {
9041c2e3 242 d = uvchr_to_utf8(d, ch);
f146bab6 243 pch = ch;
1aa99e6b
IH
244 }
245 s += len;
246 continue;
247 }
f146bab6 248 else if (sch == -1) { /* -1 is unmapped character */
640af7f1 249 Move(s, d, len, U8);
9b877dbb
IH
250 d += len;
251 }
0b9a13c3 252 else if (sch == -2) /* -2 is delete character */
1aa99e6b 253 matches++;
0b9a13c3
DM
254 else {
255 assert(sch == -3); /* -3 is empty replacement */
256 ch = comp;
257 goto replace;
258 }
259
1aa99e6b
IH
260 s += len;
261 pch = 0xfeedface;
262 }
0b9a13c3 263
9b877dbb
IH
264 if (grows) {
265 sv_setpvn(sv, (char*)dstart, d - dstart);
266 Safefree(dstart);
267 }
268 else {
269 *d = '\0';
270 SvCUR_set(sv, d - dstart);
271 }
1aa99e6b 272 SvUTF8_on(sv);
4757a243 273 }
5e44153e 274 SvSETMAGIC(sv);
4757a243
LW
275 return matches;
276}
277
334c6444
DM
278
279/* Helper function for do_trans().
280 * Handles utf8 cases(*) not involving the /c, /d, /s flags,
281 * and where search and replacement charlists aren't identical.
282 * (*) i.e. where the search or replacement charlists are utf8. sv may
283 * or may not be utf8.
284 */
285
f0fd0980 286STATIC Size_t
c395bd6c 287S_do_trans_simple_utf8(pTHX_ SV * const sv)
4757a243 288{
4757a243
LW
289 U8 *s;
290 U8 *send;
291 U8 *d;
036b4402 292 U8 *start;
1aa99e6b 293 U8 *dstart, *dend;
f0fd0980 294 Size_t matches = 0;
e4ee4c1b 295 const bool grows = cBOOL(PL_op->op_private & OPpTRANS_GROWS);
4757a243 296 STRLEN len;
043e41b8
DM
297 SV* const rv =
298#ifdef USE_ITHREADS
299 PAD_SVl(cPADOP->op_padix);
300#else
ad64d0ec 301 MUTABLE_SV(cSVOP->op_sv);
043e41b8 302#endif
85fbaab2 303 HV* const hv = MUTABLE_HV(SvRV(rv));
a4fc7abc 304 SV* const * svp = hv_fetchs(hv, "NONE", FALSE);
f54cb97a
AL
305 const UV none = svp ? SvUV(*svp) : 0x7fffffff;
306 const UV extra = none + 1;
b7953727 307 UV final = 0;
1aa99e6b 308 U8 hibit = 0;
4757a243 309
7918f24d
NC
310 PERL_ARGS_ASSERT_DO_TRANS_SIMPLE_UTF8;
311
9138d6ca 312 s = (U8*)SvPV_nomg(sv, len);
c395bd6c 313 if (!SvUTF8(sv)) {
40b85b80
KW
314 hibit = ! is_utf8_invariant_string(s, len);
315 if (hibit) {
316 s = bytes_to_utf8(s, &len);
c4d5f83a 317 }
1aa99e6b 318 }
4757a243 319 send = s + len;
036b4402 320 start = s;
4757a243 321
a4fc7abc 322 svp = hv_fetchs(hv, "FINAL", FALSE);
4757a243
LW
323 if (svp)
324 final = SvUV(*svp);
325
9b877dbb
IH
326 if (grows) {
327 /* d needs to be bigger than s, in case e.g. upgrading is required */
a02a5408 328 Newx(d, len * 3 + UTF8_MAXBYTES, U8);
9b877dbb
IH
329 dend = d + len * 3;
330 dstart = d;
331 }
332 else {
333 dstart = d = s;
334 dend = d + len;
335 }
1aa99e6b 336
4757a243 337 while (s < send) {
0bcc34c2
AL
338 const UV uv = swash_fetch(rv, s, TRUE);
339 if (uv < none) {
4757a243
LW
340 s += UTF8SKIP(s);
341 matches++;
c80e42f3 342 d = uvchr_to_utf8(d, uv);
4757a243
LW
343 }
344 else if (uv == none) {
4373e329 345 const int i = UTF8SKIP(s);
0376ff32 346 Move(s, d, i, U8);
9b877dbb
IH
347 d += i;
348 s += i;
4757a243
LW
349 }
350 else if (uv == extra) {
4373e329 351 s += UTF8SKIP(s);
4757a243 352 matches++;
c80e42f3 353 d = uvchr_to_utf8(d, final);
4757a243
LW
354 }
355 else
356 s += UTF8SKIP(s);
1aa99e6b 357
9b877dbb 358 if (d > dend) {
4373e329
AL
359 const STRLEN clen = d - dstart;
360 const STRLEN nlen = dend - dstart + len + UTF8_MAXBYTES;
9b877dbb 361 if (!grows)
7d85a32c 362 Perl_croak(aTHX_ "panic: do_trans_simple_utf8 line %d",__LINE__);
89ebb4a3 363 Renew(dstart, nlen + UTF8_MAXBYTES, U8);
1aa99e6b
IH
364 d = dstart + clen;
365 dend = dstart + nlen;
366 }
4757a243 367 }
16ec844d 368 if (grows || hibit) {
9b877dbb
IH
369 sv_setpvn(sv, (char*)dstart, d - dstart);
370 Safefree(dstart);
16ec844d
IH
371 if (grows && hibit)
372 Safefree(start);
9b877dbb
IH
373 }
374 else {
375 *d = '\0';
376 SvCUR_set(sv, d - dstart);
377 }
4757a243 378 SvSETMAGIC(sv);
1aa99e6b 379 SvUTF8_on(sv);
4757a243
LW
380
381 return matches;
382}
383
334c6444
DM
384
385/* Helper function for do_trans().
386 * Handles utf8 cases(*) where search and replacement charlists are
387 * identical: so the string isn't modified, and only a count of modifiable
388 * chars is needed.
389 * Note that it doesn't handle /d or /s, since these modify the string
390 * even if the replacement charlist is empty.
391 * (*) i.e. where the search or replacement charlists are utf8. sv may
392 * or may not be utf8.
393 */
394
f0fd0980 395STATIC Size_t
c395bd6c 396S_do_trans_count_utf8(pTHX_ SV * const sv)
4757a243 397{
93524f2b 398 const U8 *s;
cbbf8932
AL
399 const U8 *start = NULL;
400 const U8 *send;
f0fd0980 401 Size_t matches = 0;
4757a243 402 STRLEN len;
043e41b8
DM
403 SV* const rv =
404#ifdef USE_ITHREADS
405 PAD_SVl(cPADOP->op_padix);
406#else
ad64d0ec 407 MUTABLE_SV(cSVOP->op_sv);
043e41b8 408#endif
85fbaab2 409 HV* const hv = MUTABLE_HV(SvRV(rv));
a4fc7abc 410 SV* const * const svp = hv_fetchs(hv, "NONE", FALSE);
f54cb97a
AL
411 const UV none = svp ? SvUV(*svp) : 0x7fffffff;
412 const UV extra = none + 1;
1aa99e6b 413 U8 hibit = 0;
4757a243 414
7918f24d
NC
415 PERL_ARGS_ASSERT_DO_TRANS_COUNT_UTF8;
416
9138d6ca 417 s = (const U8*)SvPV_nomg_const(sv, len);
1aa99e6b 418 if (!SvUTF8(sv)) {
40b85b80
KW
419 hibit = ! is_utf8_invariant_string(s, len);
420 if (hibit) {
421 start = s = bytes_to_utf8(s, &len);
c4d5f83a 422 }
1aa99e6b 423 }
4757a243
LW
424 send = s + len;
425
426 while (s < send) {
c4420975
AL
427 const UV uv = swash_fetch(rv, s, TRUE);
428 if (uv < none || uv == extra)
4757a243 429 matches++;
834a4ddd 430 s += UTF8SKIP(s);
4757a243 431 }
1aa99e6b
IH
432 if (hibit)
433 Safefree(start);
4757a243
LW
434
435 return matches;
436}
437
334c6444
DM
438
439/* Helper function for do_trans().
440 * Handles utf8 cases(*) involving the /c, /d, /s flags,
441 * and where search and replacement charlists aren't identical.
442 * (*) i.e. where the search or replacement charlists are utf8. sv may
443 * or may not be utf8.
444 */
445
f0fd0980 446STATIC Size_t
c395bd6c 447S_do_trans_complex_utf8(pTHX_ SV * const sv)
4757a243 448{
1aa99e6b 449 U8 *start, *send;
4757a243 450 U8 *d;
f0fd0980 451 Size_t matches = 0;
e4ee4c1b
DM
452 const bool squash = cBOOL(PL_op->op_private & OPpTRANS_SQUASH);
453 const bool del = cBOOL(PL_op->op_private & OPpTRANS_DELETE);
454 const bool grows = cBOOL(PL_op->op_private & OPpTRANS_GROWS);
043e41b8
DM
455 SV* const rv =
456#ifdef USE_ITHREADS
457 PAD_SVl(cPADOP->op_padix);
458#else
ad64d0ec 459 MUTABLE_SV(cSVOP->op_sv);
043e41b8 460#endif
85fbaab2 461 HV * const hv = MUTABLE_HV(SvRV(rv));
a4fc7abc 462 SV * const *svp = hv_fetchs(hv, "NONE", FALSE);
f54cb97a
AL
463 const UV none = svp ? SvUV(*svp) : 0x7fffffff;
464 const UV extra = none + 1;
b7953727 465 UV final = 0;
45005bfb 466 bool havefinal = FALSE;
4757a243 467 STRLEN len;
1aa99e6b 468 U8 *dstart, *dend;
1aa99e6b 469 U8 hibit = 0;
9138d6ca 470 U8 *s = (U8*)SvPV_nomg(sv, len);
7918f24d
NC
471
472 PERL_ARGS_ASSERT_DO_TRANS_COMPLEX_UTF8;
473
c395bd6c 474 if (!SvUTF8(sv)) {
40b85b80
KW
475 hibit = ! is_utf8_invariant_string(s, len);
476 if (hibit) {
477 s = bytes_to_utf8(s, &len);
c4d5f83a 478 }
1aa99e6b 479 }
4757a243 480 send = s + len;
1aa99e6b 481 start = s;
4757a243 482
a4fc7abc 483 svp = hv_fetchs(hv, "FINAL", FALSE);
45005bfb 484 if (svp) {
4757a243 485 final = SvUV(*svp);
45005bfb
JH
486 havefinal = TRUE;
487 }
4757a243 488
9b877dbb
IH
489 if (grows) {
490 /* d needs to be bigger than s, in case e.g. upgrading is required */
a02a5408 491 Newx(d, len * 3 + UTF8_MAXBYTES, U8);
9b877dbb
IH
492 dend = d + len * 3;
493 dstart = d;
494 }
495 else {
496 dstart = d = s;
497 dend = d + len;
498 }
4757a243
LW
499
500 if (squash) {
501 UV puv = 0xfeedface;
502 while (s < send) {
f54cb97a 503 UV uv = swash_fetch(rv, s, TRUE);
9041c2e3 504
9b877dbb 505 if (d > dend) {
4373e329
AL
506 const STRLEN clen = d - dstart;
507 const STRLEN nlen = dend - dstart + len + UTF8_MAXBYTES;
9b877dbb 508 if (!grows)
7d85a32c 509 Perl_croak(aTHX_ "panic: do_trans_complex_utf8 line %d",__LINE__);
89ebb4a3 510 Renew(dstart, nlen + UTF8_MAXBYTES, U8);
1aa99e6b
IH
511 d = dstart + clen;
512 dend = dstart + nlen;
4757a243
LW
513 }
514 if (uv < none) {
515 matches++;
94472101 516 s += UTF8SKIP(s);
4757a243 517 if (uv != puv) {
c80e42f3 518 d = uvchr_to_utf8(d, uv);
4757a243
LW
519 puv = uv;
520 }
4757a243
LW
521 continue;
522 }
523 else if (uv == none) { /* "none" is unmapped character */
4373e329 524 const int i = UTF8SKIP(s);
0376ff32 525 Move(s, d, i, U8);
9b877dbb
IH
526 d += i;
527 s += i;
4757a243
LW
528 puv = 0xfeedface;
529 continue;
a0ed51b3 530 }
4757a243
LW
531 else if (uv == extra && !del) {
532 matches++;
45005bfb
JH
533 if (havefinal) {
534 s += UTF8SKIP(s);
535 if (puv != final) {
c80e42f3 536 d = uvchr_to_utf8(d, final);
45005bfb
JH
537 puv = final;
538 }
539 }
540 else {
541 STRLEN len;
c80e42f3 542 uv = utf8n_to_uvchr(s, send - s, &len, UTF8_ALLOW_DEFAULT);
45005bfb 543 if (uv != puv) {
0376ff32 544 Move(s, d, len, U8);
45005bfb
JH
545 d += len;
546 puv = uv;
547 }
548 s += len;
4757a243 549 }
4757a243
LW
550 continue;
551 }
01ec43d0
GS
552 matches++; /* "none+1" is delete character */
553 s += UTF8SKIP(s);
a0ed51b3 554 }
79072805
LW
555 }
556 else {
4757a243 557 while (s < send) {
f54cb97a 558 const UV uv = swash_fetch(rv, s, TRUE);
9b877dbb 559 if (d > dend) {
f54cb97a
AL
560 const STRLEN clen = d - dstart;
561 const STRLEN nlen = dend - dstart + len + UTF8_MAXBYTES;
9b877dbb 562 if (!grows)
7d85a32c 563 Perl_croak(aTHX_ "panic: do_trans_complex_utf8 line %d",__LINE__);
89ebb4a3 564 Renew(dstart, nlen + UTF8_MAXBYTES, U8);
1aa99e6b
IH
565 d = dstart + clen;
566 dend = dstart + nlen;
a0ed51b3 567 }
4757a243
LW
568 if (uv < none) {
569 matches++;
01ec43d0 570 s += UTF8SKIP(s);
c80e42f3 571 d = uvchr_to_utf8(d, uv);
4757a243 572 continue;
a0ed51b3 573 }
4757a243 574 else if (uv == none) { /* "none" is unmapped character */
f54cb97a 575 const int i = UTF8SKIP(s);
0376ff32 576 Move(s, d, i, U8);
9b877dbb
IH
577 d += i;
578 s += i;
4757a243 579 continue;
79072805 580 }
4757a243
LW
581 else if (uv == extra && !del) {
582 matches++;
01ec43d0 583 s += UTF8SKIP(s);
c80e42f3 584 d = uvchr_to_utf8(d, final);
4757a243
LW
585 continue;
586 }
01ec43d0
GS
587 matches++; /* "none+1" is delete character */
588 s += UTF8SKIP(s);
79072805 589 }
4757a243 590 }
16ec844d 591 if (grows || hibit) {
9b877dbb
IH
592 sv_setpvn(sv, (char*)dstart, d - dstart);
593 Safefree(dstart);
16ec844d
IH
594 if (grows && hibit)
595 Safefree(start);
9b877dbb
IH
596 }
597 else {
598 *d = '\0';
599 SvCUR_set(sv, d - dstart);
600 }
1aa99e6b 601 SvUTF8_on(sv);
4757a243
LW
602 SvSETMAGIC(sv);
603
604 return matches;
605}
606
334c6444
DM
607
608/* Execute a tr//. sv is the value to be translated, while PL_op
609 * should be an OP_TRANS or OP_TRANSR op, whose op_pv field contains a
610 * translation table or whose op_sv field contains a swash.
611 * Returns a count of number of characters translated
612 */
613
f0fd0980 614Size_t
864dbfa3 615Perl_do_trans(pTHX_ SV *sv)
4757a243
LW
616{
617 STRLEN len;
e4ee4c1b
DM
618 const U8 flags = PL_op->op_private;
619 const U8 hasutf = flags & (OPpTRANS_FROM_UTF | OPpTRANS_TO_UTF);
4757a243 620
7918f24d
NC
621 PERL_ARGS_ASSERT_DO_TRANS;
622
a53bfdae
HS
623 if (SvREADONLY(sv) && !(flags & OPpTRANS_IDENTICAL)) {
624 Perl_croak_no_modify();
2233f375 625 }
10516c54 626 (void)SvPV_const(sv, len);
4757a243
LW
627 if (!len)
628 return 0;
a53bfdae 629 if (!(flags & OPpTRANS_IDENTICAL)) {
4499db73 630 if (!SvPOKp(sv) || SvTHINKFIRST(sv))
b4cc4d79 631 (void)SvPV_force_nomg(sv, len);
2de7b02f 632 (void)SvPOK_only_UTF8(sv);
d59e14db 633 }
4757a243 634
a53bfdae
HS
635 /* If we use only OPpTRANS_IDENTICAL to bypass the READONLY check,
636 * we must also rely on it to choose the readonly strategy.
637 */
638 if (flags & OPpTRANS_IDENTICAL) {
639 return hasutf ? do_trans_count_utf8(sv) : do_trans_count(sv);
640 } else if (flags & (OPpTRANS_SQUASH|OPpTRANS_DELETE|OPpTRANS_COMPLEMENT)) {
641 return hasutf ? do_trans_complex_utf8(sv) : do_trans_complex(sv);
642 } else {
643 return hasutf ? do_trans_simple_utf8(sv) : do_trans_simple(sv);
79072805 644 }
79072805
LW
645}
646
647void
5aaab254 648Perl_do_join(pTHX_ SV *sv, SV *delim, SV **mark, SV **sp)
79072805 649{
53c1dcc0 650 SV ** const oldmark = mark;
eb578fdb
KW
651 I32 items = sp - mark;
652 STRLEN len;
463ee0b2 653 STRLEN delimlen;
810a07df 654 const char * const delims = SvPV_const(delim, delimlen);
79072805 655
7918f24d
NC
656 PERL_ARGS_ASSERT_DO_JOIN;
657
79072805
LW
658 mark++;
659 len = (items > 0 ? (delimlen * (items - 1) ) : 0);
862a34c6 660 SvUPGRADE(sv, SVt_PV);
79072805
LW
661 if (SvLEN(sv) < len + items) { /* current length is way too short */
662 while (items-- > 0) {
1426bbf4 663 if (*mark && !SvGAMAGIC(*mark) && SvOK(*mark)) {
f54cb97a 664 STRLEN tmplen;
5c144d81 665 SvPV_const(*mark, tmplen);
463ee0b2 666 len += tmplen;
79072805
LW
667 }
668 mark++;
669 }
670 SvGROW(sv, len + 1); /* so try to pre-extend */
671
672 mark = oldmark;
db7c17d7 673 items = sp - mark;
79072805
LW
674 ++mark;
675 }
676
f607343f 677 SvPVCLEAR(sv);
e4803c42 678 /* sv_setpv retains old UTF8ness [perl #24846] */
fb622db0 679 SvUTF8_off(sv);
e4803c42 680
284167a5 681 if (TAINTING_get && SvMAGICAL(sv))
8d6d96c1
HS
682 SvTAINTED_off(sv);
683
463ee0b2 684 if (items-- > 0) {
92d29cee
JH
685 if (*mark)
686 sv_catsv(sv, *mark);
463ee0b2
LW
687 mark++;
688 }
8d6d96c1 689
c512ce4f 690 if (delimlen) {
810a07df 691 const U32 delimflag = DO_UTF8(delim) ? SV_CATUTF8 : SV_CATBYTES;
79072805 692 for (; items > 0; items--,mark++) {
f0ee3863
FC
693 STRLEN len;
694 const char *s;
810a07df 695 sv_catpvn_flags(sv,delims,delimlen,delimflag);
f0ee3863
FC
696 s = SvPV_const(*mark,len);
697 sv_catpvn_flags(sv,s,len,
698 DO_UTF8(*mark) ? SV_CATUTF8 : SV_CATBYTES);
79072805
LW
699 }
700 }
701 else {
702 for (; items > 0; items--,mark++)
f0ee3863
FC
703 {
704 STRLEN len;
705 const char *s = SvPV_const(*mark,len);
706 sv_catpvn_flags(sv,s,len,
707 DO_UTF8(*mark) ? SV_CATUTF8 : SV_CATBYTES);
708 }
79072805
LW
709 }
710 SvSETMAGIC(sv);
711}
712
713void
03a22d83 714Perl_do_sprintf(pTHX_ SV *sv, SSize_t len, SV **sarg)
79072805 715{
46fc3d4c 716 STRLEN patlen;
53c1dcc0 717 const char * const pat = SvPV_const(*sarg, patlen);
46fc3d4c 718 bool do_taint = FALSE;
719
7918f24d 720 PERL_ARGS_ASSERT_DO_SPRINTF;
03a22d83 721 assert(len >= 1);
7918f24d 722
e06d98fb
DM
723 if (SvTAINTED(*sarg))
724 TAINT_PROPER(
725 (PL_op && PL_op->op_type < OP_max)
726 ? (PL_op->op_type == OP_PRTF)
727 ? "printf"
728 : PL_op_name[PL_op->op_type]
729 : "(unknown)"
730 );
5b781b5b 731 SvUTF8_off(sv);
2cf2cfc6
A
732 if (DO_UTF8(*sarg))
733 SvUTF8_on(sv);
03a22d83 734 sv_vsetpvfn(sv, pat, patlen, NULL, sarg + 1, (Size_t)(len - 1), &do_taint);
79072805 735 SvSETMAGIC(sv);
46fc3d4c 736 if (do_taint)
737 SvTAINTED_on(sv);
79072805
LW
738}
739
33b45480 740/* currently converts input to bytes if possible, but doesn't sweat failure */
81e118e0 741UV
d69c4304 742Perl_do_vecget(pTHX_ SV *sv, STRLEN offset, int size)
81e118e0 743{
67dd6f35 744 STRLEN srclen, len, avail, uoffset, bitoffs = 0;
fc9668ae
DM
745 const I32 svpv_flags = ((PL_op->op_flags & OPf_MOD || LVRET)
746 ? SV_UNDEF_RETURNS_NULL : 0);
747 unsigned char *s = (unsigned char *)
748 SvPV_flags(sv, srclen, (svpv_flags|SV_GMAGIC));
81e118e0
JH
749 UV retnum = 0;
750
032061d2 751 if (!s) {
fc9668ae 752 s = (unsigned char *)"";
032061d2
BF
753 }
754
7918f24d
NC
755 PERL_ARGS_ASSERT_DO_VECGET;
756
8e84507e 757 if (size < 1 || (size & (size-1))) /* size < 1 or not a power of two */
a50d7633 758 Perl_croak(aTHX_ "Illegal number of bits in vec");
246fae53 759
fc9668ae 760 if (SvUTF8(sv)) {
315f3fc1
KW
761 if (Perl_sv_utf8_downgrade(aTHX_ sv, TRUE)) {
762 /* PVX may have changed */
763 s = (unsigned char *) SvPV_flags(sv, srclen, svpv_flags);
764 }
765 else {
766 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
767 "Use of strings with code points over 0xFF as"
768 " arguments to vec is deprecated. This will"
769 " be a fatal error in Perl 5.32");
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
SB
936 /* This is handled by the SvPOK_only below...
937 if (!Perl_sv_utf8_downgrade(aTHX_ targ, TRUE))
938 SvUTF8_off(targ);
939 */
940 (void) Perl_sv_utf8_downgrade(aTHX_ targ, TRUE);
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
LW
1010{
1011#ifdef LIBERAL
eb578fdb
KW
1012 long *dl;
1013 long *ll;
1014 long *rl;
79072805 1015#endif
eb578fdb 1016 char *dc;
463ee0b2
LW
1017 STRLEN leftlen;
1018 STRLEN rightlen;
eb578fdb
KW
1019 const char *lc;
1020 const char *rc;
b404a7f5 1021 STRLEN len = 0;
bb7a0f54 1022 STRLEN lensave;
e62f0680
NC
1023 const char *lsave;
1024 const char *rsave;
bb7a0f54 1025 STRLEN needlen = 0;
08b6664b 1026 bool result_needs_to_be_utf8 = FALSE;
b50535da
KW
1027 bool left_utf8 = FALSE;
1028 bool right_utf8 = FALSE;
1029 U8 * left_non_downgraded = NULL;
1030 U8 * right_non_downgraded = NULL;
1031 Size_t left_non_downgraded_len = 0;
1032 Size_t right_non_downgraded_len = 0;
1033 char * non_downgraded = NULL;
1034 Size_t non_downgraded_len = 0;
0c57e439 1035
7918f24d 1036 PERL_ARGS_ASSERT_DO_VOP;
79072805 1037
6b349a5c 1038 if (sv != left || (optype != OP_BIT_AND && !SvOK(sv)))
f607343f 1039 SvPVCLEAR(sv); /* avoid undef warning on |= and ^= */
8c8eee82 1040 if (sv == left) {
08b6664b 1041 lc = SvPV_force_nomg(left, leftlen);
8c8eee82
BM
1042 }
1043 else {
08b6664b 1044 lc = SvPV_nomg_const(left, leftlen);
8c8eee82
BM
1045 SvPV_force_nomg_nolen(sv);
1046 }
08b6664b 1047 rc = SvPV_nomg_const(right, rightlen);
12abf4f0 1048
0e6e7171 1049 /* This needs to come after SvPV to ensure that string overloading has
12abf4f0
NC
1050 fired off. */
1051
08b6664b
KW
1052 /* Create downgraded temporaries of any UTF-8 encoded operands */
1053 if (DO_UTF8(left)) {
b50535da 1054 const U8 * save_lc = (U8 *) lc;
08b6664b 1055
b50535da 1056 left_utf8 = TRUE;
08b6664b
KW
1057 result_needs_to_be_utf8 = TRUE;
1058
b50535da
KW
1059 left_non_downgraded_len = leftlen;
1060 lc = (char *) bytes_from_utf8_loc((const U8 *) lc, &leftlen,
1061 &left_utf8,
1062 (const U8 **) &left_non_downgraded);
1063 /* Calculate the number of trailing unconvertible bytes. This quantity
1064 * is the original length minus the length of the converted portion. */
1065 left_non_downgraded_len -= left_non_downgraded - save_lc;
1066 SAVEFREEPV(lc);
12abf4f0 1067 }
08b6664b 1068 if (DO_UTF8(right)) {
b50535da 1069 const U8 * save_rc = (U8 *) rc;
08b6664b 1070
b50535da 1071 right_utf8 = TRUE;
08b6664b
KW
1072 result_needs_to_be_utf8 = TRUE;
1073
b50535da
KW
1074 right_non_downgraded_len = rightlen;
1075 rc = (char *) bytes_from_utf8_loc((const U8 *) rc, &rightlen,
1076 &right_utf8,
1077 (const U8 **) &right_non_downgraded);
1078 right_non_downgraded_len -= right_non_downgraded - save_rc;
1079 SAVEFREEPV(rc);
1080 }
1081
1082 /* We set 'len' to the length that the operation actually operates on. The
1083 * dangling part of the longer operand doesn't actually participate in the
1084 * operation. What happens is that we pretend that the shorter operand has
1085 * been extended to the right by enough imaginary zeros to match the length
1086 * of the longer one. But we know in advance the result of the operation
1087 * on zeros without having to do it. In the case of '&', the result is
1088 * zero, and the dangling portion is simply discarded. For '|' and '^', the
1089 * result is the same as the other operand, so the dangling part is just
1090 * appended to the final result, unchanged. We currently accept above-FF
1091 * code points in the dangling portion, as that's how it has long worked,
1092 * and code depends on it staying that way. But it is now fatal for
1093 * above-FF to appear in the portion that does get operated on. Hence, any
1094 * above-FF must come only in the longer operand, and only in its dangling
1095 * portion. That means that at least one of the operands has to be
1096 * entirely non-UTF-8, and the length of that operand has to be before the
1097 * first above-FF in the other */
ba52ce15 1098 if (left_utf8 || right_utf8) {
3f04a466
KW
1099 if (left_utf8) {
1100 if (right_utf8 || rightlen > leftlen) {
1101 Perl_croak(aTHX_ fatal_above_ff_msg, PL_op_desc[optype]);
1102 }
1103 len = rightlen;
08b6664b 1104 }
3f04a466
KW
1105 else if (right_utf8) {
1106 if (leftlen > rightlen) {
1107 Perl_croak(aTHX_ fatal_above_ff_msg, PL_op_desc[optype]);
1108 }
1109 len = leftlen;
b50535da 1110 }
ba52ce15
KW
1111
1112 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
1113 deprecated_above_ff_msg, PL_op_desc[optype]);
1114 }
b50535da 1115 else { /* Neither is UTF-8 */
78ba9007 1116 len = MIN(leftlen, rightlen);
12abf4f0
NC
1117 }
1118
b50535da 1119 lensave = len;
08b6664b
KW
1120 lsave = lc;
1121 rsave = rc;
b50535da 1122
9fdd7463
JH
1123 SvCUR_set(sv, len);
1124 (void)SvPOK_only(sv);
08b6664b 1125 if (SvOK(sv) || SvTYPE(sv) > SVt_PVMG) {
2596d9fe 1126 dc = SvPV_force_nomg_nolen(sv);
bb7a0f54
MHM
1127 if (SvLEN(sv) < len + 1) {
1128 dc = SvGROW(sv, len + 1);
ff68c719 1129 (void)memzero(dc + SvCUR(sv), len - SvCUR(sv) + 1);
1130 }
1131 }
1132 else {
bb7a0f54
MHM
1133 needlen = optype == OP_BIT_AND
1134 ? len : (leftlen > rightlen ? leftlen : rightlen);
a02a5408 1135 Newxz(dc, needlen + 1, char);
aa0a69cb 1136 sv_usepvn_flags(sv, dc, needlen, SV_HAS_TRAILING_NUL);
ff68c719 1137 dc = SvPVX(sv); /* sv_usepvn() calls Renew() */
79072805 1138 }
0c57e439 1139
79072805
LW
1140#ifdef LIBERAL
1141 if (len >= sizeof(long)*4 &&
bb7a0f54
MHM
1142 !((unsigned long)dc % sizeof(long)) &&
1143 !((unsigned long)lc % sizeof(long)) &&
1144 !((unsigned long)rc % sizeof(long))) /* It's almost always aligned... */
79072805 1145 {
bb7a0f54 1146 const STRLEN remainder = len % (sizeof(long)*4);
79072805
LW
1147 len /= (sizeof(long)*4);
1148
1149 dl = (long*)dc;
1150 ll = (long*)lc;
1151 rl = (long*)rc;
1152
1153 switch (optype) {
1154 case OP_BIT_AND:
1155 while (len--) {
1156 *dl++ = *ll++ & *rl++;
1157 *dl++ = *ll++ & *rl++;
1158 *dl++ = *ll++ & *rl++;
1159 *dl++ = *ll++ & *rl++;
1160 }
1161 break;
a0d0e21e 1162 case OP_BIT_XOR:
79072805
LW
1163 while (len--) {
1164 *dl++ = *ll++ ^ *rl++;
1165 *dl++ = *ll++ ^ *rl++;
1166 *dl++ = *ll++ ^ *rl++;
1167 *dl++ = *ll++ ^ *rl++;
1168 }
1169 break;
1170 case OP_BIT_OR:
1171 while (len--) {
1172 *dl++ = *ll++ | *rl++;
1173 *dl++ = *ll++ | *rl++;
1174 *dl++ = *ll++ | *rl++;
1175 *dl++ = *ll++ | *rl++;
1176 }
1177 }
1178
1179 dc = (char*)dl;
1180 lc = (char*)ll;
1181 rc = (char*)rl;
1182
1183 len = remainder;
1184 }
1185#endif
27a9d47d
KW
1186 switch (optype) {
1187 case OP_BIT_AND:
1188 while (len--)
1189 *dc++ = *lc++ & *rc++;
1190 *dc = '\0';
1191 break;
1192 case OP_BIT_XOR:
1193 while (len--)
1194 *dc++ = *lc++ ^ *rc++;
1195 goto mop_up;
1196 case OP_BIT_OR:
1197 while (len--)
1198 *dc++ = *lc++ | *rc++;
1199 mop_up:
1200 len = lensave;
1201 if (rightlen > len) {
1202 if (dc == rc)
1203 SvCUR(sv) = rightlen;
1204 else
1205 sv_catpvn_nomg(sv, rsave + len, rightlen - len);
1206 }
1207 else if (leftlen > len) {
1208 if (dc == lc)
1209 SvCUR(sv) = leftlen;
1210 else
1211 sv_catpvn_nomg(sv, lsave + len, leftlen - len);
1212 }
1213 *SvEND(sv) = '\0';
392582f8 1214
b50535da
KW
1215 /* If there is trailing stuff that couldn't be converted from UTF-8, it
1216 * is appended as-is for the ^ and | operators. This preserves
1217 * backwards compatibility */
1218 if (right_non_downgraded) {
1219 non_downgraded = (char *) right_non_downgraded;
1220 non_downgraded_len = right_non_downgraded_len;
1221 }
1222 else if (left_non_downgraded) {
1223 non_downgraded = (char *) left_non_downgraded;
1224 non_downgraded_len = left_non_downgraded_len;
1225 }
1226
27a9d47d
KW
1227 break;
1228 }
08b6664b
KW
1229
1230 if (result_needs_to_be_utf8) {
b50535da
KW
1231 sv_utf8_upgrade_nomg(sv);
1232
1233 /* Append any trailing UTF-8 as-is. */
1234 if (non_downgraded) {
1235 sv_catpvn_nomg(sv, non_downgraded, non_downgraded_len);
1236 }
79072805 1237 }
08b6664b 1238
fb73857a 1239 SvTAINT(sv);
79072805 1240}
463ee0b2 1241
b1c05ba5 1242
a2232057
DM
1243/* Perl_do_kv() may be:
1244 * * called directly as the pp function for pp_keys() and pp_values();
a2232057
DM
1245 * * It may also be called directly when the op is OP_AVHVSWITCH, to
1246 * implement CORE::keys(), CORE::values().
1247 *
1248 * In all cases it expects an HV on the stack and returns a list of keys,
1249 * values, or key-value pairs, depending on PL_op.
1250 */
b1c05ba5 1251
463ee0b2 1252OP *
cea2e8a9 1253Perl_do_kv(pTHX)
463ee0b2 1254{
39644a26 1255 dSP;
73ff03e8 1256 HV * const keys = MUTABLE_HV(POPs);
1c23e2bd 1257 const U8 gimme = GIMME_V;
a2232057 1258
af3b1cba 1259 const I32 dokeys = (PL_op->op_type == OP_KEYS)
a2232057 1260 || ( PL_op->op_type == OP_AVHVSWITCH
94184451
DM
1261 && (PL_op->op_private & OPpAVHVSWITCH_MASK)
1262 + OP_EACH == OP_KEYS);
a2232057 1263
af3b1cba 1264 const I32 dovalues = (PL_op->op_type == OP_VALUES)
a2232057 1265 || ( PL_op->op_type == OP_AVHVSWITCH
94184451
DM
1266 && (PL_op->op_private & OPpAVHVSWITCH_MASK)
1267 + OP_EACH == OP_VALUES);
a2232057 1268
af3b1cba 1269 assert( PL_op->op_type == OP_KEYS
a2232057
DM
1270 || PL_op->op_type == OP_VALUES
1271 || PL_op->op_type == OP_AVHVSWITCH);
463ee0b2 1272
4fa080db
DM
1273 assert(!( PL_op->op_type == OP_VALUES
1274 && (PL_op->op_private & OPpMAYBE_LVSUB)));
1275
800e9ae0 1276 (void)hv_iterinit(keys); /* always reset iterator regardless */
748a9306 1277
54310121 1278 if (gimme == G_VOID)
aa689395 1279 RETURN;
1280
54310121 1281 if (gimme == G_SCALAR) {
78f9721b 1282 if (PL_op->op_flags & OPf_MOD || LVRET) { /* lvalue */
2154eca7
EB
1283 SV * const ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
1284 sv_magic(ret, NULL, PERL_MAGIC_nkeys, NULL, 0);
1285 LvTYPE(ret) = 'k';
1286 LvTARG(ret) = SvREFCNT_inc_simple(keys);
1287 PUSHs(ret);
81714fb9 1288 }
463ee0b2 1289 else {
2154eca7
EB
1290 IV i;
1291 dTARGET;
1292
af3b1cba
DM
1293 /* note that in 'scalar(keys %h)' the OP_KEYS is usually
1294 * optimised away and the action is performed directly by the
1295 * padhv or rv2hv op. We now only get here via OP_AVHVSWITCH
1296 * and \&CORE::keys
1297 */
2154eca7 1298 if (! SvTIED_mg((const SV *)keys, PERL_MAGIC_tied) ) {
1b95d04f 1299 i = HvUSEDKEYS(keys);
2154eca7
EB
1300 }
1301 else {
1302 i = 0;
1303 while (hv_iternext(keys)) i++;
1304 }
1305 PUSHi( i );
463ee0b2 1306 }
463ee0b2
LW
1307 RETURN;
1308 }
1309
a061ab0b
FC
1310 if (UNLIKELY(PL_op->op_private & OPpMAYBE_LVSUB)) {
1311 const I32 flags = is_lvalue_sub();
1312 if (flags && !(flags & OPpENTERSUB_INARGS))
1313 /* diag_listed_as: Can't modify %s in %s */
1314 Perl_croak(aTHX_ "Can't modify keys in list assignment");
1315 }
1316
8dc9003f
DM
1317 PUTBACK;
1318 hv_pushkv(keys, (dokeys | (dovalues << 1)));
1319 return NORMAL;
463ee0b2 1320}
4e35701f 1321
af3babe4 1322/*
14d04a33 1323 * ex: set ts=8 sts=4 sw=4 et:
37442d52 1324 */