This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
regen/mph.pl - Clean up diagnostics logic, allow DEBUG from env.
[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)) {
1604cfb0
MS
56 while (s < send) {
57 const short ch = tbl->map[*s];
58 if (ch >= 0) {
59 matches++;
60 *s = (U8)ch;
61 }
62 s++;
63 }
64 SvSETMAGIC(sv);
9b877dbb
IH
65 }
66 else {
1604cfb0
MS
67 const bool grows = cBOOL(PL_op->op_private & OPpTRANS_GROWS);
68 U8 *d;
69 U8 *dstart;
c395bd6c 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. */
1604cfb0
MS
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;
82 short ch;
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);
86 if (c < 0x100 && (ch = tbl->map[c]) >= 0) {
87 matches++;
88 d = uvchr_to_utf8(d, (UV)ch);
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)) {
1604cfb0 143 while (s < send) {
c1048fcf 144 if (tbl->map[*s++] >= 0)
036b4402 145 matches++;
1604cfb0 146 }
c395bd6c 147 }
fabdb6c0 148 else {
1604cfb0
MS
149 const bool complement = cBOOL(PL_op->op_private & OPpTRANS_COMPLEMENT);
150 while (s < send) {
151 STRLEN ulen;
152 const UV c = utf8n_to_uvchr(s, send - s, &ulen, UTF8_ALLOW_DEFAULT);
153 if (c < 0x100) {
154 if (tbl->map[c] >= 0)
155 matches++;
156 } else if (complement)
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 192 if (!SvUTF8(sv)) {
1604cfb0
MS
193 U8 *d = s;
194 U8 * const dstart = d;
4757a243 195
1604cfb0 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
1604cfb0
MS
203 while (s < send) {
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 }
1604cfb0
MS
211 }
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
1604cfb0
MS
223 s++;
224 }
225 }
226 else { /* Not to squash */
227 while (s < send) {
228 const short this_map = tbl->map[*s];
229 if (this_map >= 0) {
230 matches++;
231 *d++ = (U8)this_map;
232 }
233 else if (this_map == (short) TR_UNMAPPED)
234 *d++ = *s;
235 else if (this_map == (short) TR_DELETE)
236 matches++;
237 s++;
238 }
239 }
240 *d = '\0';
241 SvCUR_set(sv, d - dstart);
4757a243 242 }
c395bd6c 243 else { /* is utf8 */
1604cfb0
MS
244 const bool squash = cBOOL(PL_op->op_private & OPpTRANS_SQUASH);
245 const bool grows = cBOOL(PL_op->op_private & OPpTRANS_GROWS);
246 U8 *d;
247 U8 *dstart;
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
1604cfb0 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: */
1604cfb0
MS
260 Newx(d, len*2+1, U8);
261 else
262 d = s;
263 dstart = d;
1aa99e6b 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
1604cfb0
MS
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 }
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;
1aa99e6b 445 }
f34acfec
KW
446
447 s = (U8*)SvPV_nomg(sv, len);
5d7580af
KW
448 DEBUG_y(PerlIO_printf(Perl_debug_log, "%s: %d: entering do_trans_invmap:"
449 " input sv:\n",
450 __FILE__, __LINE__));
451 DEBUG_y(sv_dump(sv));
452 DEBUG_y(PerlIO_printf(Perl_debug_log, "mapping:\n"));
453 DEBUG_y(invmap_dump(from_invlist, map));
454
4757a243 455 send = s + len;
f34acfec 456 s0 = s;
4757a243 457
f34acfec
KW
458 /* We know by now if there are some possible input strings whose
459 * transliterations are longer than the input. If none can, we just edit
460 * in place. */
461 if (inplace) {
1604cfb0 462 d0 = d = s;
f34acfec
KW
463 }
464 else {
465 /* Here, we can't edit in place. We have no idea how much, if any,
466 * this particular input string will grow. However, the compilation
8bbdcb37
KW
467 * calculated the maximum expansion possible. Use that to allocate
468 * based on the worst case scenario. (First +1 is to round up; 2nd is
469 * for \0) */
1604cfb0
MS
470 Newx(d, (STRLEN) (len * max_expansion + 1 + 1), U8);
471 d0 = d;
4757a243
LW
472 }
473
f34acfec 474 restart:
4757a243 475
f34acfec
KW
476 /* Do the actual transliteration */
477 while (s < send) {
478 UV from;
479 UV to;
480 SSize_t i;
481 STRLEN s_len;
482
483 /* Get the code point of the next character in the string */
484 if (! SvUTF8(sv) || UTF8_IS_INVARIANT(*s)) {
485 from = *s;
486 s_len = 1;
487 }
488 else {
489 from = utf8_to_uvchr_buf(s, send, &s_len);
490 if (from == 0 && *s != '\0') {
491 _force_out_malformed_utf8_message(s, send, 0, /*die*/TRUE);
492 }
493 }
334c6444 494
f34acfec
KW
495 /* Look the code point up in the data structure for this tr/// to get
496 * what it maps to */
497 i = _invlist_search(from_invlist, from);
498 assert(i >= 0);
334c6444 499
f34acfec 500 to = map[i];
7918f24d 501
f34acfec
KW
502 if (to == (UV) TR_UNLISTED) { /* Just copy the unreplaced character */
503 if (UVCHR_IS_INVARIANT(from) || ! out_is_utf8) {
2077aa94 504 *d++ = (U8) from;
f34acfec
KW
505 }
506 else if (SvUTF8(sv)) {
507 Move(s, d, s_len, U8);
508 d += s_len;
509 }
510 else { /* Convert to UTF-8 */
511 append_utf8_from_native_byte(*s, &d);
512 }
7918f24d 513
f34acfec
KW
514 previous_map = to;
515 s += s_len;
516 continue;
1604cfb0 517 }
4757a243 518
f34acfec
KW
519 /* Everything else is counted as a match */
520 matches++;
4757a243 521
f34acfec
KW
522 if (to == (UV) TR_SPECIAL_HANDLING) {
523 if (delete_unfound) {
f34acfec
KW
524 s += s_len;
525 continue;
526 }
4757a243 527
f34acfec
KW
528 /* Use the final character in the replacement list */
529 to = final_map;
530 }
531 else { /* Here the input code point is to be remapped. The actual
532 value is offset from the base of this entry */
533 to += from - from_array[i];
534 }
535
536 /* If copying all occurrences, or this is the first occurrence, copy it
537 * to the output */
538 if (! squash || to != previous_map) {
539 if (out_is_utf8) {
540 d = uvchr_to_utf8(d, to);
541 }
542 else {
543 if (to >= 256) { /* If need to convert to UTF-8, restart */
544 out_is_utf8 = TRUE;
545 s = s0;
546 d = d0;
547 matches = 0;
548 goto restart;
549 }
2077aa94 550 *d++ = (U8) to;
f34acfec
KW
551 }
552 }
553
554 previous_map = to;
555 s += s_len;
4757a243 556 }
f34acfec
KW
557
558 s_len = 0;
559 s += s_len;
560 if (! inplace) {
1604cfb0 561 sv_setpvn(sv, (char*)d0, d - d0);
ecfd8588 562 Safefree(d0);
9b877dbb
IH
563 }
564 else {
1604cfb0
MS
565 *d = '\0';
566 SvCUR_set(sv, d - d0);
f34acfec
KW
567 }
568
569 if (! SvUTF8(sv) && out_is_utf8) {
570 SvUTF8_on(sv);
9b877dbb 571 }
4757a243
LW
572 SvSETMAGIC(sv);
573
f1d561b5 574 DEBUG_y(PerlIO_printf(Perl_debug_log, "%s: %d: returning %zu\n",
5d7580af
KW
575 __FILE__, __LINE__, matches));
576 DEBUG_y(sv_dump(sv));
4757a243
LW
577 return matches;
578}
579
334c6444
DM
580/* Execute a tr//. sv is the value to be translated, while PL_op
581 * should be an OP_TRANS or OP_TRANSR op, whose op_pv field contains a
f34acfec
KW
582 * translation table or whose op_sv field contains an inversion map.
583 *
334c6444
DM
584 * Returns a count of number of characters translated
585 */
586
f0fd0980 587Size_t
864dbfa3 588Perl_do_trans(pTHX_ SV *sv)
4757a243
LW
589{
590 STRLEN len;
e4ee4c1b 591 const U8 flags = PL_op->op_private;
f34acfec
KW
592 bool use_utf8_fcns = cBOOL(flags & OPpTRANS_USE_SVOP);
593 bool identical = cBOOL(flags & OPpTRANS_IDENTICAL);
4757a243 594
7918f24d
NC
595 PERL_ARGS_ASSERT_DO_TRANS;
596
f34acfec 597 if (SvREADONLY(sv) && ! identical) {
a53bfdae 598 Perl_croak_no_modify();
2233f375 599 }
10516c54 600 (void)SvPV_const(sv, len);
4757a243 601 if (!len)
1604cfb0 602 return 0;
f34acfec 603 if (! identical) {
1604cfb0
MS
604 if (!SvPOKp(sv) || SvTHINKFIRST(sv))
605 (void)SvPV_force_nomg(sv, len);
606 (void)SvPOK_only_UTF8(sv);
d59e14db 607 }
4757a243 608
f34acfec
KW
609 if (use_utf8_fcns) {
610 SV* const map =
611#ifdef USE_ITHREADS
612 PAD_SVl(cPADOP->op_padix);
613#else
614 MUTABLE_SV(cSVOP->op_sv);
615#endif
616
617 if (identical) {
618 return do_trans_count_invmap(sv, (AV *) map);
619 }
620 else {
621 return do_trans_invmap(sv, (AV *) map);
622 }
623 }
624 else {
625 const OPtrans_map * const map = (OPtrans_map*)cPVOP->op_pv;
626
627 if (identical) {
628 return do_trans_count(sv, map);
629 }
630 else if (flags & (OPpTRANS_SQUASH|OPpTRANS_DELETE|OPpTRANS_COMPLEMENT)) {
631 return do_trans_complex(sv, map);
632 }
633 else
634 return do_trans_simple(sv, map);
79072805 635 }
79072805
LW
636}
637
638void
5aaab254 639Perl_do_join(pTHX_ SV *sv, SV *delim, SV **mark, SV **sp)
79072805 640{
53c1dcc0 641 SV ** const oldmark = mark;
eb578fdb
KW
642 I32 items = sp - mark;
643 STRLEN len;
463ee0b2 644 STRLEN delimlen;
810a07df 645 const char * const delims = SvPV_const(delim, delimlen);
79072805 646
7918f24d
NC
647 PERL_ARGS_ASSERT_DO_JOIN;
648
79072805
LW
649 mark++;
650 len = (items > 0 ? (delimlen * (items - 1) ) : 0);
862a34c6 651 SvUPGRADE(sv, SVt_PV);
79072805 652 if (SvLEN(sv) < len + items) { /* current length is way too short */
1604cfb0
MS
653 while (items-- > 0) {
654 if (*mark && !SvGAMAGIC(*mark) && SvOK(*mark)) {
655 STRLEN tmplen;
656 SvPV_const(*mark, tmplen);
657 len += tmplen;
658 }
659 mark++;
660 }
661 SvGROW(sv, len + 1); /* so try to pre-extend */
662
663 mark = oldmark;
664 items = sp - mark;
665 ++mark;
79072805
LW
666 }
667
f607343f 668 SvPVCLEAR(sv);
e4803c42 669 /* sv_setpv retains old UTF8ness [perl #24846] */
fb622db0 670 SvUTF8_off(sv);
e4803c42 671
284167a5 672 if (TAINTING_get && SvMAGICAL(sv))
1604cfb0 673 SvTAINTED_off(sv);
8d6d96c1 674
463ee0b2 675 if (items-- > 0) {
1604cfb0
MS
676 if (*mark)
677 sv_catsv(sv, *mark);
678 mark++;
463ee0b2 679 }
8d6d96c1 680
c512ce4f 681 if (delimlen) {
1604cfb0
MS
682 const U32 delimflag = DO_UTF8(delim) ? SV_CATUTF8 : SV_CATBYTES;
683 for (; items > 0; items--,mark++) {
684 STRLEN len;
685 const char *s;
686 sv_catpvn_flags(sv,delims,delimlen,delimflag);
687 s = SvPV_const(*mark,len);
688 sv_catpvn_flags(sv,s,len,
689 DO_UTF8(*mark) ? SV_CATUTF8 : SV_CATBYTES);
690 }
79072805
LW
691 }
692 else {
1604cfb0
MS
693 for (; items > 0; items--,mark++)
694 {
695 STRLEN len;
696 const char *s = SvPV_const(*mark,len);
697 sv_catpvn_flags(sv,s,len,
698 DO_UTF8(*mark) ? SV_CATUTF8 : SV_CATBYTES);
699 }
79072805
LW
700 }
701 SvSETMAGIC(sv);
702}
703
704void
03a22d83 705Perl_do_sprintf(pTHX_ SV *sv, SSize_t len, SV **sarg)
79072805 706{
46fc3d4c 707 STRLEN patlen;
53c1dcc0 708 const char * const pat = SvPV_const(*sarg, patlen);
46fc3d4c 709 bool do_taint = FALSE;
710
7918f24d 711 PERL_ARGS_ASSERT_DO_SPRINTF;
03a22d83 712 assert(len >= 1);
7918f24d 713
e06d98fb 714 if (SvTAINTED(*sarg))
1604cfb0
MS
715 TAINT_PROPER(
716 (PL_op && PL_op->op_type < OP_max)
717 ? (PL_op->op_type == OP_PRTF)
718 ? "printf"
719 : PL_op_name[PL_op->op_type]
720 : "(unknown)"
721 );
5b781b5b 722 SvUTF8_off(sv);
2cf2cfc6
A
723 if (DO_UTF8(*sarg))
724 SvUTF8_on(sv);
03a22d83 725 sv_vsetpvfn(sv, pat, patlen, NULL, sarg + 1, (Size_t)(len - 1), &do_taint);
79072805 726 SvSETMAGIC(sv);
46fc3d4c 727 if (do_taint)
1604cfb0 728 SvTAINTED_on(sv);
79072805
LW
729}
730
81e118e0 731UV
d69c4304 732Perl_do_vecget(pTHX_ SV *sv, STRLEN offset, int size)
81e118e0 733{
f77b2d0e 734 STRLEN srclen;
fc9668ae
DM
735 const I32 svpv_flags = ((PL_op->op_flags & OPf_MOD || LVRET)
736 ? SV_UNDEF_RETURNS_NULL : 0);
737 unsigned char *s = (unsigned char *)
738 SvPV_flags(sv, srclen, (svpv_flags|SV_GMAGIC));
81e118e0
JH
739 UV retnum = 0;
740
032061d2 741 if (!s) {
fc9668ae 742 s = (unsigned char *)"";
032061d2 743 }
2f96a1b4 744
7918f24d
NC
745 PERL_ARGS_ASSERT_DO_VECGET;
746
958cdd99 747 if (size < 1 || ! isPOWER_OF_2(size))
1604cfb0 748 Perl_croak(aTHX_ "Illegal number of bits in vec");
246fae53 749
fc9668ae 750 if (SvUTF8(sv)) {
1604cfb0 751 if (Perl_sv_utf8_downgrade_flags(aTHX_ sv, TRUE, 0)) {
315f3fc1
KW
752 /* PVX may have changed */
753 s = (unsigned char *) SvPV_flags(sv, srclen, svpv_flags);
754 }
755 else {
d3597f89
KW
756 Perl_croak(aTHX_ "Use of strings with code points over 0xFF"
757 " as arguments to vec is forbidden");
315f3fc1 758 }
fc9668ae 759 }
246fae53 760
6c4aa50c
KW
761 if (size <= 8) {
762 STRLEN bitoffs = ((offset % 8) * size) % 8;
f77b2d0e 763 STRLEN uoffset = offset / (8 / size);
bbb8a7e0 764
6c4aa50c
KW
765 if (uoffset >= srclen)
766 return 0;
67dd6f35 767
1604cfb0 768 retnum = (s[uoffset] >> bitoffs) & nBIT_MASK(size);
6c4aa50c 769 }
81e118e0 770 else {
6c4aa50c 771 int n = size / 8; /* required number of bytes */
f77b2d0e 772 SSize_t uoffset;
6c4aa50c 773
d7d93a81 774#ifdef UV_IS_QUAD
6c4aa50c
KW
775
776 if (size == 64) {
1604cfb0
MS
777 Perl_ck_warner(aTHX_ packWARN(WARN_PORTABLE),
778 "Bit vector size > 32 non-portable");
1604cfb0 779 }
c5a0f51a 780#endif
6c4aa50c
KW
781 if (offset > Size_t_MAX / n - 1) /* would overflow */
782 return 0;
783
784 uoffset = offset * n;
785
f77b2d0e
KW
786 /* Switch on the number of bytes available, but no more than the number
787 * required */
788 switch (MIN(n, (SSize_t) srclen - uoffset)) {
6c4aa50c
KW
789
790#ifdef UV_IS_QUAD
791
792 case 8:
793 retnum += ((UV) s[uoffset + 7]);
794 /* FALLTHROUGH */
795 case 7:
796 retnum += ((UV) s[uoffset + 6] << 8); /* = size - 56 */
797 /* FALLTHROUGH */
798 case 6:
799 retnum += ((UV) s[uoffset + 5] << 16); /* = size - 48 */
800 /* FALLTHROUGH */
801 case 5:
802 retnum += ((UV) s[uoffset + 4] << 24); /* = size - 40 */
803#endif
804 /* FALLTHROUGH */
805 case 4:
806 retnum += ((UV) s[uoffset + 3] << (size - 32));
807 /* FALLTHROUGH */
808 case 3:
809 retnum += ((UV) s[uoffset + 2] << (size - 24));
810 /* FALLTHROUGH */
811 case 2:
812 retnum += ((UV) s[uoffset + 1] << (size - 16));
813 /* FALLTHROUGH */
814 case 1:
815 retnum += ((UV) s[uoffset ] << (size - 8));
816 break;
f77b2d0e
KW
817
818 default:
819 return 0;
6c4aa50c 820 }
81e118e0
JH
821 }
822
823 return retnum;
824}
825
33b45480
SB
826/* currently converts input to bytes if possible but doesn't sweat failures,
827 * although it does ensure that the string it clobbers is not marked as
828 * utf8-valid any more
829 */
79072805 830void
864dbfa3 831Perl_do_vecset(pTHX_ SV *sv)
79072805 832{
67dd6f35 833 STRLEN offset, bitoffs = 0;
eb578fdb
KW
834 int size;
835 unsigned char *s;
836 UV lval;
79072805 837 I32 mask;
a0d0e21e
LW
838 STRLEN targlen;
839 STRLEN len;
c4420975 840 SV * const targ = LvTARG(sv);
1b92e694 841 char errflags = LvFLAGS(sv);
79072805 842
7918f24d
NC
843 PERL_ARGS_ASSERT_DO_VECSET;
844
1b92e694
DM
845 /* some out-of-range errors have been deferred if/until the LV is
846 * actually written to: f(vec($s,-1,8)) is not always fatal */
847 if (errflags) {
b063b0a8
DIM
848 assert(!(errflags & ~(LVf_NEG_OFF|LVf_OUT_OF_RANGE)));
849 if (errflags & LVf_NEG_OFF)
1b92e694
DM
850 Perl_croak_nocontext("Negative offset to vec in lvalue context");
851 Perl_croak_nocontext("Out of memory!");
852 }
853
8990e307 854 if (!targ)
1604cfb0 855 return;
032061d2
BF
856 s = (unsigned char*)SvPV_force_flags(targ, targlen,
857 SV_GMAGIC | SV_UNDEF_RETURNS_NULL);
246fae53 858 if (SvUTF8(targ)) {
1604cfb0
MS
859 /* This is handled by the SvPOK_only below...
860 if (!Perl_sv_utf8_downgrade_flags(aTHX_ targ, TRUE, 0))
861 SvUTF8_off(targ);
862 */
863 (void) Perl_sv_utf8_downgrade_flags(aTHX_ targ, TRUE, 0);
246fae53
MG
864 }
865
4ebbc975 866 (void)SvPOK_only(targ);
81e118e0 867 lval = SvUV(sv);
79072805
LW
868 offset = LvTARGOFF(sv);
869 size = LvTARGLEN(sv);
67dd6f35 870
8e84507e 871 if (size < 1 || (size & (size-1))) /* size < 1 or not a power of two */
1604cfb0 872 Perl_croak(aTHX_ "Illegal number of bits in vec");
8e84507e 873
bbb8a7e0 874 if (size < 8) {
1604cfb0
MS
875 bitoffs = ((offset%8)*size)%8;
876 offset /= 8/size;
bbb8a7e0 877 }
67dd6f35 878 else if (size > 8) {
1604cfb0 879 int n = size/8;
67dd6f35
DM
880 if (offset > Size_t_MAX / n - 1) /* would overflow */
881 Perl_croak_nocontext("Out of memory!");
1604cfb0 882 offset *= n;
67dd6f35 883 }
bbb8a7e0 884
67dd6f35
DM
885 len = (bitoffs + size + 7)/8; /* required number of bytes */
886 if (targlen < offset || targlen - offset < len) {
887 STRLEN newlen = offset > Size_t_MAX - len - 1 ? /* avoid overflow */
888 Size_t_MAX : offset + len + 1;
1604cfb0
MS
889 s = (unsigned char*)SvGROW(targ, newlen);
890 (void)memzero((char *)(s + targlen), newlen - targlen);
891 SvCUR_set(targ, newlen - 1);
a0d0e21e 892 }
8e84507e 893
79072805 894 if (size < 8) {
1604cfb0
MS
895 mask = nBIT_MASK(size);
896 lval &= mask;
897 s[offset] &= ~(mask << bitoffs);
898 s[offset] |= lval << bitoffs;
79072805 899 }
150c6a4b
KW
900 else switch (size) {
901
d7d93a81 902#ifdef UV_IS_QUAD
150c6a4b
KW
903
904 case 64:
905 Perl_ck_warner(aTHX_ packWARN(WARN_PORTABLE),
906 "Bit vector size > 32 non-portable");
907 s[offset+7] = (U8)( lval ); /* = size - 64 */
908 s[offset+6] = (U8)( lval >> 8); /* = size - 56 */
909 s[offset+5] = (U8)( lval >> 16); /* = size - 48 */
910 s[offset+4] = (U8)( lval >> 24); /* = size - 40 */
dc1e3f56 911#endif
150c6a4b
KW
912 /* FALLTHROUGH */
913 case 32:
914 s[offset+3] = (U8)( lval >> (size - 32));
915 s[offset+2] = (U8)( lval >> (size - 24));
916 /* FALLTHROUGH */
917 case 16:
918 s[offset+1] = (U8)( lval >> (size - 16));
919 /* FALLTHROUGH */
920 case 8:
921 s[offset ] = (U8)( lval >> (size - 8));
79072805 922 }
7bb043c3 923 SvSETMAGIC(targ);
79072805
LW
924}
925
926void
864dbfa3 927Perl_do_vop(pTHX_ I32 optype, SV *sv, SV *left, SV *right)
79072805 928{
eb578fdb
KW
929 long *dl;
930 long *ll;
931 long *rl;
eb578fdb 932 char *dc;
463ee0b2
LW
933 STRLEN leftlen;
934 STRLEN rightlen;
eb578fdb
KW
935 const char *lc;
936 const char *rc;
b404a7f5 937 STRLEN len = 0;
bb7a0f54 938 STRLEN lensave;
e62f0680
NC
939 const char *lsave;
940 const char *rsave;
bb7a0f54 941 STRLEN needlen = 0;
08b6664b 942 bool result_needs_to_be_utf8 = FALSE;
b50535da
KW
943 bool left_utf8 = FALSE;
944 bool right_utf8 = FALSE;
945 U8 * left_non_downgraded = NULL;
946 U8 * right_non_downgraded = NULL;
947 Size_t left_non_downgraded_len = 0;
948 Size_t right_non_downgraded_len = 0;
949 char * non_downgraded = NULL;
950 Size_t non_downgraded_len = 0;
0c57e439 951
7918f24d 952 PERL_ARGS_ASSERT_DO_VOP;
79072805 953
6b349a5c 954 if (sv != left || (optype != OP_BIT_AND && !SvOK(sv)))
f607343f 955 SvPVCLEAR(sv); /* avoid undef warning on |= and ^= */
8c8eee82 956 if (sv == left) {
1604cfb0 957 lc = SvPV_force_nomg(left, leftlen);
8c8eee82
BM
958 }
959 else {
1604cfb0
MS
960 lc = SvPV_nomg_const(left, leftlen);
961 SvPV_force_nomg_nolen(sv);
8c8eee82 962 }
08b6664b 963 rc = SvPV_nomg_const(right, rightlen);
12abf4f0 964
0e6e7171 965 /* This needs to come after SvPV to ensure that string overloading has
12abf4f0
NC
966 fired off. */
967
08b6664b
KW
968 /* Create downgraded temporaries of any UTF-8 encoded operands */
969 if (DO_UTF8(left)) {
b50535da 970 const U8 * save_lc = (U8 *) lc;
08b6664b 971
b50535da 972 left_utf8 = TRUE;
08b6664b
KW
973 result_needs_to_be_utf8 = TRUE;
974
b50535da
KW
975 left_non_downgraded_len = leftlen;
976 lc = (char *) bytes_from_utf8_loc((const U8 *) lc, &leftlen,
977 &left_utf8,
978 (const U8 **) &left_non_downgraded);
979 /* Calculate the number of trailing unconvertible bytes. This quantity
980 * is the original length minus the length of the converted portion. */
981 left_non_downgraded_len -= left_non_downgraded - save_lc;
982 SAVEFREEPV(lc);
12abf4f0 983 }
08b6664b 984 if (DO_UTF8(right)) {
b50535da 985 const U8 * save_rc = (U8 *) rc;
08b6664b 986
b50535da 987 right_utf8 = TRUE;
08b6664b
KW
988 result_needs_to_be_utf8 = TRUE;
989
b50535da
KW
990 right_non_downgraded_len = rightlen;
991 rc = (char *) bytes_from_utf8_loc((const U8 *) rc, &rightlen,
992 &right_utf8,
993 (const U8 **) &right_non_downgraded);
994 right_non_downgraded_len -= right_non_downgraded - save_rc;
995 SAVEFREEPV(rc);
996 }
997
998 /* We set 'len' to the length that the operation actually operates on. The
999 * dangling part of the longer operand doesn't actually participate in the
1000 * operation. What happens is that we pretend that the shorter operand has
1001 * been extended to the right by enough imaginary zeros to match the length
1002 * of the longer one. But we know in advance the result of the operation
1003 * on zeros without having to do it. In the case of '&', the result is
1004 * zero, and the dangling portion is simply discarded. For '|' and '^', the
1005 * result is the same as the other operand, so the dangling part is just
c8b94fe0
JK
1006 * appended to the final result, unchanged. As of perl-5.32, we no longer
1007 * accept above-FF code points in the dangling portion.
1008 */
ba52ce15 1009 if (left_utf8 || right_utf8) {
c8b94fe0 1010 Perl_croak(aTHX_ FATAL_ABOVE_FF_MSG, PL_op_desc[optype]);
ba52ce15 1011 }
b50535da 1012 else { /* Neither is UTF-8 */
78ba9007 1013 len = MIN(leftlen, rightlen);
12abf4f0
NC
1014 }
1015
b50535da 1016 lensave = len;
08b6664b
KW
1017 lsave = lc;
1018 rsave = rc;
b50535da 1019
9fdd7463 1020 (void)SvPOK_only(sv);
08b6664b 1021 if (SvOK(sv) || SvTYPE(sv) > SVt_PVMG) {
1604cfb0
MS
1022 dc = SvPV_force_nomg_nolen(sv);
1023 if (SvLEN(sv) < len + 1) {
1024 dc = SvGROW(sv, len + 1);
1025 (void)memzero(dc + SvCUR(sv), len - SvCUR(sv) + 1);
1026 }
ff68c719 1027 }
1028 else {
1604cfb0
MS
1029 needlen = optype == OP_BIT_AND
1030 ? len : (leftlen > rightlen ? leftlen : rightlen);
1031 Newxz(dc, needlen + 1, char);
1032 sv_usepvn_flags(sv, dc, needlen, SV_HAS_TRAILING_NUL);
1033 dc = SvPVX(sv); /* sv_usepvn() calls Renew() */
79072805 1034 }
45f235c1 1035 SvCUR_set(sv, len);
0c57e439 1036
79072805 1037 if (len >= sizeof(long)*4 &&
1604cfb0
MS
1038 !(PTR2nat(dc) % sizeof(long)) &&
1039 !(PTR2nat(lc) % sizeof(long)) &&
1040 !(PTR2nat(rc) % sizeof(long))) /* It's almost always aligned... */
79072805 1041 {
1604cfb0
MS
1042 const STRLEN remainder = len % (sizeof(long)*4);
1043 len /= (sizeof(long)*4);
1044
1045 dl = (long*)dc;
1046 ll = (long*)lc;
1047 rl = (long*)rc;
1048
1049 switch (optype) {
1050 case OP_BIT_AND:
1051 while (len--) {
1052 *dl++ = *ll++ & *rl++;
1053 *dl++ = *ll++ & *rl++;
1054 *dl++ = *ll++ & *rl++;
1055 *dl++ = *ll++ & *rl++;
1056 }
1057 break;
1058 case OP_BIT_XOR:
1059 while (len--) {
1060 *dl++ = *ll++ ^ *rl++;
1061 *dl++ = *ll++ ^ *rl++;
1062 *dl++ = *ll++ ^ *rl++;
1063 *dl++ = *ll++ ^ *rl++;
1064 }
1065 break;
1066 case OP_BIT_OR:
1067 while (len--) {
1068 *dl++ = *ll++ | *rl++;
1069 *dl++ = *ll++ | *rl++;
1070 *dl++ = *ll++ | *rl++;
1071 *dl++ = *ll++ | *rl++;
1072 }
1073 }
1074
1075 dc = (char*)dl;
1076 lc = (char*)ll;
1077 rc = (char*)rl;
1078
1079 len = remainder;
79072805 1080 }
17d44595 1081
27a9d47d
KW
1082 switch (optype) {
1083 case OP_BIT_AND:
1084 while (len--)
1085 *dc++ = *lc++ & *rc++;
1086 *dc = '\0';
1087 break;
1088 case OP_BIT_XOR:
1089 while (len--)
1090 *dc++ = *lc++ ^ *rc++;
1091 goto mop_up;
1092 case OP_BIT_OR:
1093 while (len--)
1094 *dc++ = *lc++ | *rc++;
1095 mop_up:
1096 len = lensave;
1097 if (rightlen > len) {
1098 if (dc == rc)
2324bdb9 1099 SvCUR_set(sv, rightlen);
27a9d47d
KW
1100 else
1101 sv_catpvn_nomg(sv, rsave + len, rightlen - len);
1102 }
1103 else if (leftlen > len) {
1104 if (dc == lc)
2324bdb9 1105 SvCUR_set(sv, leftlen);
27a9d47d
KW
1106 else
1107 sv_catpvn_nomg(sv, lsave + len, leftlen - len);
1108 }
1109 *SvEND(sv) = '\0';
392582f8 1110
b50535da
KW
1111 /* If there is trailing stuff that couldn't be converted from UTF-8, it
1112 * is appended as-is for the ^ and | operators. This preserves
1113 * backwards compatibility */
1114 if (right_non_downgraded) {
1115 non_downgraded = (char *) right_non_downgraded;
1116 non_downgraded_len = right_non_downgraded_len;
1117 }
1118 else if (left_non_downgraded) {
1119 non_downgraded = (char *) left_non_downgraded;
1120 non_downgraded_len = left_non_downgraded_len;
1121 }
1122
27a9d47d
KW
1123 break;
1124 }
08b6664b
KW
1125
1126 if (result_needs_to_be_utf8) {
b50535da
KW
1127 sv_utf8_upgrade_nomg(sv);
1128
1129 /* Append any trailing UTF-8 as-is. */
1130 if (non_downgraded) {
1131 sv_catpvn_nomg(sv, non_downgraded, non_downgraded_len);
1132 }
79072805 1133 }
08b6664b 1134
fb73857a 1135 SvTAINT(sv);
79072805 1136}
463ee0b2 1137
b1c05ba5 1138
a2232057
DM
1139/* Perl_do_kv() may be:
1140 * * called directly as the pp function for pp_keys() and pp_values();
a2232057
DM
1141 * * It may also be called directly when the op is OP_AVHVSWITCH, to
1142 * implement CORE::keys(), CORE::values().
1143 *
1144 * In all cases it expects an HV on the stack and returns a list of keys,
1145 * values, or key-value pairs, depending on PL_op.
1146 */
b1c05ba5 1147
463ee0b2 1148OP *
cea2e8a9 1149Perl_do_kv(pTHX)
463ee0b2 1150{
39644a26 1151 dSP;
73ff03e8 1152 HV * const keys = MUTABLE_HV(POPs);
1c23e2bd 1153 const U8 gimme = GIMME_V;
a2232057 1154
af3b1cba 1155 const I32 dokeys = (PL_op->op_type == OP_KEYS)
a2232057 1156 || ( PL_op->op_type == OP_AVHVSWITCH
94184451
DM
1157 && (PL_op->op_private & OPpAVHVSWITCH_MASK)
1158 + OP_EACH == OP_KEYS);
a2232057 1159
af3b1cba 1160 const I32 dovalues = (PL_op->op_type == OP_VALUES)
a2232057 1161 || ( PL_op->op_type == OP_AVHVSWITCH
94184451
DM
1162 && (PL_op->op_private & OPpAVHVSWITCH_MASK)
1163 + OP_EACH == OP_VALUES);
a2232057 1164
af3b1cba 1165 assert( PL_op->op_type == OP_KEYS
a2232057
DM
1166 || PL_op->op_type == OP_VALUES
1167 || PL_op->op_type == OP_AVHVSWITCH);
463ee0b2 1168
4fa080db
DM
1169 assert(!( PL_op->op_type == OP_VALUES
1170 && (PL_op->op_private & OPpMAYBE_LVSUB)));
1171
800e9ae0 1172 (void)hv_iterinit(keys); /* always reset iterator regardless */
748a9306 1173
54310121 1174 if (gimme == G_VOID)
1604cfb0 1175 RETURN;
aa689395 1176
54310121 1177 if (gimme == G_SCALAR) {
1604cfb0 1178 if (PL_op->op_flags & OPf_MOD || LVRET) { /* lvalue */
7ea8b04b 1179 SV * const ret = newSV_type_mortal(SVt_PVLV); /* Not TARG RT#67838 */
1604cfb0
MS
1180 sv_magic(ret, NULL, PERL_MAGIC_nkeys, NULL, 0);
1181 LvTYPE(ret) = 'k';
1182 LvTARG(ret) = SvREFCNT_inc_simple(keys);
1183 PUSHs(ret);
1184 }
1185 else {
1186 IV i;
1187 dTARGET;
2154eca7 1188
af3b1cba
DM
1189 /* note that in 'scalar(keys %h)' the OP_KEYS is usually
1190 * optimised away and the action is performed directly by the
1191 * padhv or rv2hv op. We now only get here via OP_AVHVSWITCH
1192 * and \&CORE::keys
1193 */
1604cfb0
MS
1194 if (! SvTIED_mg((const SV *)keys, PERL_MAGIC_tied) ) {
1195 i = HvUSEDKEYS(keys);
1196 }
1197 else {
1198 i = 0;
1199 while (hv_iternext(keys)) i++;
1200 }
1201 PUSHi( i );
1202 }
1203 RETURN;
463ee0b2
LW
1204 }
1205
a061ab0b 1206 if (UNLIKELY(PL_op->op_private & OPpMAYBE_LVSUB)) {
1604cfb0
MS
1207 const I32 flags = is_lvalue_sub();
1208 if (flags && !(flags & OPpENTERSUB_INARGS))
1209 /* diag_listed_as: Can't modify %s in %s */
1210 Perl_croak(aTHX_ "Can't modify keys in list assignment");
a061ab0b
FC
1211 }
1212
8dc9003f
DM
1213 PUTBACK;
1214 hv_pushkv(keys, (dokeys | (dovalues << 1)));
1215 return NORMAL;
463ee0b2 1216}
4e35701f 1217
af3babe4 1218/*
14d04a33 1219 * ex: set ts=8 sts=4 sw=4 et:
37442d52 1220 */