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