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