3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4 * 2001, 2002, 2004, 2005, 2006, 2007, 2008, 2009 by Larry Wall and others
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.
12 * 'So that was the job I felt I had to do when I started,' thought Sam.
14 * [p.934 of _The Lord of the Rings_, VI/iii: "Mount Doom"]
17 /* This file contains some common functions needed to carry out certain
18 * ops. For example, both pp_sprintf() and pp_prtf() call the function
19 * do_sprintf() found in this file.
23 #define PERL_IN_DOOP_C
25 #include "invlist_inline.h"
32 /* Helper function for do_trans().
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.
36 * sv may or may not be utf8. Note that no code point above 255 can possibly
37 * be in the to-translate set
41 S_do_trans_simple(pTHX_ SV * const sv, const OPtrans_map * const tbl)
45 U8 *s = (U8*)SvPV_nomg(sv,len);
46 U8 * const send = s+len;
48 PERL_ARGS_ASSERT_DO_TRANS_SIMPLE;
49 DEBUG_y(PerlIO_printf(Perl_debug_log, "%s: %d: entering do_trans_simple:"
54 /* First, take care of non-UTF-8 input strings, because they're easy */
57 const short ch = tbl->map[*s];
67 const bool grows = cBOOL(PL_op->op_private & OPpTRANS_GROWS);
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. */
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) {
88 d = uvchr_to_utf8(d, (UV)ch);
91 else { /* No match -> copy */
98 sv_setpvn(sv, (char*)dstart, d - dstart);
103 SvCUR_set(sv, d - dstart);
108 DEBUG_y(PerlIO_printf(Perl_debug_log, "%s: %d: returning %zu\n",
109 __FILE__, __LINE__, matches));
110 DEBUG_y(sv_dump(sv));
115 /* Helper function for do_trans().
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
120 * Note that it doesn't handle /d or /s, since these modify the string even if
121 * the replacement list is empty.
123 * sv may or may not be utf8. Note that no code point above 255 can possibly
124 * be in the to-translate set
128 S_do_trans_count(pTHX_ SV * const sv, const OPtrans_map * const tbl)
131 const U8 *s = (const U8*)SvPV_nomg_const(sv, len);
132 const U8 * const send = s + len;
135 PERL_ARGS_ASSERT_DO_TRANS_COUNT;
137 DEBUG_y(PerlIO_printf(Perl_debug_log, "%s: %d: entering do_trans_count:"
139 __FILE__, __LINE__));
140 DEBUG_y(sv_dump(sv));
144 if (tbl->map[*s++] >= 0)
149 const bool complement = cBOOL(PL_op->op_private & OPpTRANS_COMPLEMENT);
152 const UV c = utf8n_to_uvchr(s, send - s, &ulen, UTF8_ALLOW_DEFAULT);
154 if (tbl->map[c] >= 0)
156 } else if (complement)
162 DEBUG_y(PerlIO_printf(Perl_debug_log, "%s: %d: count returning %zu\n",
163 __FILE__, __LINE__, matches));
168 /* Helper function for do_trans().
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.
172 * sv may or may not be utf8. Note that no code point above 255 can possibly
173 * be in the to-translate set
177 S_do_trans_complex(pTHX_ SV * const sv, const OPtrans_map * const tbl)
180 U8 *s = (U8*)SvPV_nomg(sv, len);
181 U8 * const send = s+len;
183 const bool complement = cBOOL(PL_op->op_private & OPpTRANS_COMPLEMENT);
185 PERL_ARGS_ASSERT_DO_TRANS_COMPLEX;
187 DEBUG_y(PerlIO_printf(Perl_debug_log, "%s: %d: entering do_trans_complex:"
189 __FILE__, __LINE__));
190 DEBUG_y(sv_dump(sv));
194 U8 * const dstart = d;
196 if (PL_op->op_private & OPpTRANS_SQUASH) {
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;
204 const short this_map = tbl->map[*s];
207 if (this_map != previous_map) {
209 previous_map = this_map;
213 if (this_map == (short) TR_UNMAPPED) {
215 previous_map = (short) TR_OOB;
218 assert(this_map == (short) TR_DELETE);
226 else { /* Not to squash */
228 const short this_map = tbl->map[*s];
233 else if (this_map == (short) TR_UNMAPPED)
235 else if (this_map == (short) TR_DELETE)
241 SvCUR_set(sv, d - dstart);
244 const bool squash = cBOOL(PL_op->op_private & OPpTRANS_SQUASH);
245 const bool grows = cBOOL(PL_op->op_private & OPpTRANS_GROWS);
248 Size_t size = tbl->size;
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) */
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: */
260 Newx(d, len*2+1, U8);
267 const UV comp = utf8n_to_uvchr(s, send - s, &len,
275 ? (short) TR_UNMAPPED
282 if (LIKELY(!squash || ch != pch)) {
283 d = uvchr_to_utf8(d, ch);
289 else if (sch == (short) TR_UNMAPPED) {
294 else if (sch == (short) TR_DELETE)
297 assert(sch == (short) TR_R_EMPTY); /* empty replacement */
306 sv_setpvn(sv, (char*)dstart, d - dstart);
311 SvCUR_set(sv, d - dstart);
316 DEBUG_y(PerlIO_printf(Perl_debug_log, "%s: %d: returning %zu\n",
317 __FILE__, __LINE__, matches));
318 DEBUG_y(sv_dump(sv));
323 /* Helper function for do_trans().
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.
328 * Note that it doesn't handle /d nor /s, since these modify the string
329 * even if the replacement charlist is empty.
331 * sv may or may not be utf8.
335 S_do_trans_count_invmap(pTHX_ SV * const sv, AV * const invmap)
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);
347 PERL_ARGS_ASSERT_DO_TRANS_COUNT_INVMAP;
349 DEBUG_y(PerlIO_printf(Perl_debug_log, "%s: %d:"
350 "entering do_trans_count_invmap:"
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)));
357 s = (U8*)SvPV_nomg(sv, len);
366 /* Get the code point of the next character in the string */
367 if (! SvUTF8(sv) || UTF8_IS_INVARIANT(*s)) {
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);
378 /* Look the code point up in the data structure for this tr/// to get
380 i = _invlist_search(from_invlist, from);
383 if (map[i] != (UV) TR_UNLISTED) {
390 DEBUG_y(PerlIO_printf(Perl_debug_log, "%s: %d: returning %zu\n",
391 __FILE__, __LINE__, matches));
395 /* Helper function for do_trans().
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
400 * sv may or may not be utf8.
404 S_do_trans_invmap(pTHX_ SV * const sv, AV * const invmap)
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);
425 UV final_map = TR_OOB;
426 bool out_is_utf8 = cBOOL(SvUTF8(sv));
429 PERL_ARGS_ASSERT_DO_TRANS_INVMAP;
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);
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
443 if (! out_is_utf8 && (PL_op->op_private & OPpTRANS_CAN_FORCE_UTF8)) {
445 if (max_expansion < 2) {
450 s = (U8*)SvPV_nomg(sv, len);
451 DEBUG_y(PerlIO_printf(Perl_debug_log, "%s: %d: entering do_trans_invmap:"
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));
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
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
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
473 Newx(d, (STRLEN) (len * max_expansion + 1 + 1), U8);
479 /* Do the actual transliteration */
486 /* Get the code point of the next character in the string */
487 if (! SvUTF8(sv) || UTF8_IS_INVARIANT(*s)) {
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);
498 /* Look the code point up in the data structure for this tr/// to get
500 i = _invlist_search(from_invlist, from);
505 if (to == (UV) TR_UNLISTED) { /* Just copy the unreplaced character */
506 if (UVCHR_IS_INVARIANT(from) || ! out_is_utf8) {
509 else if (SvUTF8(sv)) {
510 Move(s, d, s_len, U8);
513 else { /* Convert to UTF-8 */
514 append_utf8_from_native_byte(*s, &d);
522 /* Everything else is counted as a match */
525 if (to == (UV) TR_SPECIAL_HANDLING) {
526 if (delete_unfound) {
531 /* Use the final character in the replacement list */
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];
539 /* If copying all occurrences, or this is the first occurrence, copy it
541 if (! squash || to != previous_map) {
543 d = uvchr_to_utf8(d, to);
546 if (to >= 256) { /* If need to convert to UTF-8, restart */
564 sv_setpvn(sv, (char*)d0, d - d0);
569 SvCUR_set(sv, d - d0);
572 if (! SvUTF8(sv) && out_is_utf8) {
577 DEBUG_y(PerlIO_printf(Perl_debug_log, "%s: %d: returning %zu\n",
578 __FILE__, __LINE__, matches));
579 DEBUG_y(sv_dump(sv));
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
585 * translation table or whose op_sv field contains an inversion map.
587 * Returns a count of number of characters translated
591 Perl_do_trans(pTHX_ SV *sv)
594 const U8 flags = PL_op->op_private;
595 bool use_utf8_fcns = cBOOL(flags & OPpTRANS_USE_SVOP);
596 bool identical = cBOOL(flags & OPpTRANS_IDENTICAL);
598 PERL_ARGS_ASSERT_DO_TRANS;
600 if (SvREADONLY(sv) && ! identical) {
601 Perl_croak_no_modify();
603 (void)SvPV_const(sv, len);
607 if (!SvPOKp(sv) || SvTHINKFIRST(sv))
608 (void)SvPV_force_nomg(sv, len);
609 (void)SvPOK_only_UTF8(sv);
615 PAD_SVl(cPADOP->op_padix);
617 MUTABLE_SV(cSVOP->op_sv);
621 return do_trans_count_invmap(sv, (AV *) map);
624 return do_trans_invmap(sv, (AV *) map);
628 const OPtrans_map * const map = (OPtrans_map*)cPVOP->op_pv;
631 return do_trans_count(sv, map);
633 else if (flags & (OPpTRANS_SQUASH|OPpTRANS_DELETE|OPpTRANS_COMPLEMENT)) {
634 return do_trans_complex(sv, map);
637 return do_trans_simple(sv, map);
642 Perl_do_join(pTHX_ SV *sv, SV *delim, SV **mark, SV **sp)
644 SV ** const oldmark = mark;
645 I32 items = sp - mark;
648 const char * const delims = SvPV_const(delim, delimlen);
650 PERL_ARGS_ASSERT_DO_JOIN;
653 len = (items > 0 ? (delimlen * (items - 1) ) : 0);
654 SvUPGRADE(sv, SVt_PV);
655 if (SvLEN(sv) < len + items) { /* current length is way too short */
656 while (items-- > 0) {
657 if (*mark && !SvGAMAGIC(*mark) && SvOK(*mark)) {
659 SvPV_const(*mark, tmplen);
664 SvGROW(sv, len + 1); /* so try to pre-extend */
672 /* sv_setpv retains old UTF8ness [perl #24846] */
675 if (TAINTING_get && SvMAGICAL(sv))
685 const U32 delimflag = DO_UTF8(delim) ? SV_CATUTF8 : SV_CATBYTES;
686 for (; items > 0; items--,mark++) {
689 sv_catpvn_flags(sv,delims,delimlen,delimflag);
690 s = SvPV_const(*mark,len);
691 sv_catpvn_flags(sv,s,len,
692 DO_UTF8(*mark) ? SV_CATUTF8 : SV_CATBYTES);
696 for (; items > 0; items--,mark++)
699 const char *s = SvPV_const(*mark,len);
700 sv_catpvn_flags(sv,s,len,
701 DO_UTF8(*mark) ? SV_CATUTF8 : SV_CATBYTES);
708 Perl_do_sprintf(pTHX_ SV *sv, SSize_t len, SV **sarg)
711 const char * const pat = SvPV_const(*sarg, patlen);
712 bool do_taint = FALSE;
714 PERL_ARGS_ASSERT_DO_SPRINTF;
717 if (SvTAINTED(*sarg))
719 (PL_op && PL_op->op_type < OP_max)
720 ? (PL_op->op_type == OP_PRTF)
722 : PL_op_name[PL_op->op_type]
728 sv_vsetpvfn(sv, pat, patlen, NULL, sarg + 1, (Size_t)(len - 1), &do_taint);
734 /* currently converts input to bytes if possible, but doesn't sweat failure */
736 Perl_do_vecget(pTHX_ SV *sv, STRLEN offset, int size)
738 STRLEN srclen, len, avail, uoffset, bitoffs = 0;
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));
746 s = (unsigned char *)"";
749 PERL_ARGS_ASSERT_DO_VECGET;
751 if (size < 1 || (size & (size-1))) /* size < 1 or not a power of two */
752 Perl_croak(aTHX_ "Illegal number of bits in vec");
755 if (Perl_sv_utf8_downgrade_flags(aTHX_ sv, TRUE, 0)) {
756 /* PVX may have changed */
757 s = (unsigned char *) SvPV_flags(sv, srclen, svpv_flags);
760 Perl_croak(aTHX_ "Use of strings with code points over 0xFF as arguments to vec is forbidden");
765 bitoffs = ((offset%8)*size)%8;
766 uoffset = offset/(8/size);
770 if (offset > Size_t_MAX / n - 1) /* would overflow */
777 if (uoffset >= srclen)
780 len = (bitoffs + size + 7)/8; /* required number of bytes */
781 avail = srclen - uoffset; /* available number of bytes */
783 /* Does the byte range overlap the end of the string? If so,
784 * handle specially. */
791 retnum = (UV) s[uoffset] << 8;
793 else if (size == 32) {
794 assert(avail >= 1 && avail <= 3);
797 ((UV) s[uoffset ] << 24);
800 ((UV) s[uoffset ] << 24) +
801 ((UV) s[uoffset + 1] << 16);
804 ((UV) s[uoffset ] << 24) +
805 ((UV) s[uoffset + 1] << 16) +
806 ( s[uoffset + 2] << 8);
809 else if (size == 64) {
810 Perl_ck_warner(aTHX_ packWARN(WARN_PORTABLE),
811 "Bit vector size > 32 non-portable");
812 assert(avail >= 1 && avail <= 7);
815 (UV) s[uoffset ] << 56;
818 ((UV) s[uoffset ] << 56) +
819 ((UV) s[uoffset + 1] << 48);
822 ((UV) s[uoffset ] << 56) +
823 ((UV) s[uoffset + 1] << 48) +
824 ((UV) s[uoffset + 2] << 40);
827 ((UV) s[uoffset ] << 56) +
828 ((UV) s[uoffset + 1] << 48) +
829 ((UV) s[uoffset + 2] << 40) +
830 ((UV) s[uoffset + 3] << 32);
833 ((UV) s[uoffset ] << 56) +
834 ((UV) s[uoffset + 1] << 48) +
835 ((UV) s[uoffset + 2] << 40) +
836 ((UV) s[uoffset + 3] << 32) +
837 ((UV) s[uoffset + 4] << 24);
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);
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) +
854 ((UV) s[uoffset + 6] << 8);
860 retnum = (s[uoffset] >> bitoffs) & ((1 << size) - 1);
866 ((UV) s[uoffset] << 8) +
870 ((UV) s[uoffset ] << 24) +
871 ((UV) s[uoffset + 1] << 16) +
872 ( s[uoffset + 2] << 8) +
875 else if (size == 64) {
876 Perl_ck_warner(aTHX_ packWARN(WARN_PORTABLE),
877 "Bit vector size > 32 non-portable");
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) +
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
899 Perl_do_vecset(pTHX_ SV *sv)
901 STRLEN offset, bitoffs = 0;
908 SV * const targ = LvTARG(sv);
909 char errflags = LvFLAGS(sv);
911 PERL_ARGS_ASSERT_DO_VECSET;
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 */
916 assert(!(errflags & ~(LVf_NEG_OFF|LVf_OUT_OF_RANGE)));
917 if (errflags & LVf_NEG_OFF)
918 Perl_croak_nocontext("Negative offset to vec in lvalue context");
919 Perl_croak_nocontext("Out of memory!");
924 s = (unsigned char*)SvPV_force_flags(targ, targlen,
925 SV_GMAGIC | SV_UNDEF_RETURNS_NULL);
927 /* This is handled by the SvPOK_only below...
928 if (!Perl_sv_utf8_downgrade_flags(aTHX_ targ, TRUE, 0))
931 (void) Perl_sv_utf8_downgrade_flags(aTHX_ targ, TRUE, 0);
934 (void)SvPOK_only(targ);
936 offset = LvTARGOFF(sv);
937 size = LvTARGLEN(sv);
939 if (size < 1 || (size & (size-1))) /* size < 1 or not a power of two */
940 Perl_croak(aTHX_ "Illegal number of bits in vec");
943 bitoffs = ((offset%8)*size)%8;
948 if (offset > Size_t_MAX / n - 1) /* would overflow */
949 Perl_croak_nocontext("Out of memory!");
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);
963 mask = (1 << size) - 1;
965 s[offset] &= ~(mask << bitoffs);
966 s[offset] |= lval << bitoffs;
970 s[offset ] = (U8)( lval & 0xff);
971 else if (size == 16) {
972 s[offset ] = (U8)((lval >> 8) & 0xff);
973 s[offset+1] = (U8)( lval & 0xff);
975 else if (size == 32) {
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);
982 else if (size == 64) {
983 Perl_ck_warner(aTHX_ packWARN(WARN_PORTABLE),
984 "Bit vector size > 32 non-portable");
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);
1000 Perl_do_vop(pTHX_ I32 optype, SV *sv, SV *left, SV *right)
1015 bool result_needs_to_be_utf8 = FALSE;
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;
1025 PERL_ARGS_ASSERT_DO_VOP;
1027 if (sv != left || (optype != OP_BIT_AND && !SvOK(sv)))
1028 SvPVCLEAR(sv); /* avoid undef warning on |= and ^= */
1030 lc = SvPV_force_nomg(left, leftlen);
1033 lc = SvPV_nomg_const(left, leftlen);
1034 SvPV_force_nomg_nolen(sv);
1036 rc = SvPV_nomg_const(right, rightlen);
1038 /* This needs to come after SvPV to ensure that string overloading has
1041 /* Create downgraded temporaries of any UTF-8 encoded operands */
1042 if (DO_UTF8(left)) {
1043 const U8 * save_lc = (U8 *) lc;
1046 result_needs_to_be_utf8 = TRUE;
1048 left_non_downgraded_len = leftlen;
1049 lc = (char *) bytes_from_utf8_loc((const U8 *) lc, &leftlen,
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;
1057 if (DO_UTF8(right)) {
1058 const U8 * save_rc = (U8 *) rc;
1061 result_needs_to_be_utf8 = TRUE;
1063 right_non_downgraded_len = rightlen;
1064 rc = (char *) bytes_from_utf8_loc((const U8 *) rc, &rightlen,
1066 (const U8 **) &right_non_downgraded);
1067 right_non_downgraded_len -= right_non_downgraded - save_rc;
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
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.
1082 if (left_utf8 || right_utf8) {
1083 Perl_croak(aTHX_ FATAL_ABOVE_FF_MSG, PL_op_desc[optype]);
1085 else { /* Neither is UTF-8 */
1086 len = MIN(leftlen, rightlen);
1094 (void)SvPOK_only(sv);
1095 if (SvOK(sv) || SvTYPE(sv) > SVt_PVMG) {
1096 dc = SvPV_force_nomg_nolen(sv);
1097 if (SvLEN(sv) < len + 1) {
1098 dc = SvGROW(sv, len + 1);
1099 (void)memzero(dc + SvCUR(sv), len - SvCUR(sv) + 1);
1103 needlen = optype == OP_BIT_AND
1104 ? len : (leftlen > rightlen ? leftlen : rightlen);
1105 Newxz(dc, needlen + 1, char);
1106 sv_usepvn_flags(sv, dc, needlen, SV_HAS_TRAILING_NUL);
1107 dc = SvPVX(sv); /* sv_usepvn() calls Renew() */
1110 if (len >= sizeof(long)*4 &&
1111 !(PTR2nat(dc) % sizeof(long)) &&
1112 !(PTR2nat(lc) % sizeof(long)) &&
1113 !(PTR2nat(rc) % sizeof(long))) /* It's almost always aligned... */
1115 const STRLEN remainder = len % (sizeof(long)*4);
1116 len /= (sizeof(long)*4);
1125 *dl++ = *ll++ & *rl++;
1126 *dl++ = *ll++ & *rl++;
1127 *dl++ = *ll++ & *rl++;
1128 *dl++ = *ll++ & *rl++;
1133 *dl++ = *ll++ ^ *rl++;
1134 *dl++ = *ll++ ^ *rl++;
1135 *dl++ = *ll++ ^ *rl++;
1136 *dl++ = *ll++ ^ *rl++;
1141 *dl++ = *ll++ | *rl++;
1142 *dl++ = *ll++ | *rl++;
1143 *dl++ = *ll++ | *rl++;
1144 *dl++ = *ll++ | *rl++;
1158 *dc++ = *lc++ & *rc++;
1163 *dc++ = *lc++ ^ *rc++;
1167 *dc++ = *lc++ | *rc++;
1170 if (rightlen > len) {
1172 SvCUR_set(sv, rightlen);
1174 sv_catpvn_nomg(sv, rsave + len, rightlen - len);
1176 else if (leftlen > len) {
1178 SvCUR_set(sv, leftlen);
1180 sv_catpvn_nomg(sv, lsave + len, leftlen - len);
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;
1191 else if (left_non_downgraded) {
1192 non_downgraded = (char *) left_non_downgraded;
1193 non_downgraded_len = left_non_downgraded_len;
1199 if (result_needs_to_be_utf8) {
1200 sv_utf8_upgrade_nomg(sv);
1202 /* Append any trailing UTF-8 as-is. */
1203 if (non_downgraded) {
1204 sv_catpvn_nomg(sv, non_downgraded, non_downgraded_len);
1212 /* Perl_do_kv() may be:
1213 * * called directly as the pp function for pp_keys() and pp_values();
1214 * * It may also be called directly when the op is OP_AVHVSWITCH, to
1215 * implement CORE::keys(), CORE::values().
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.
1225 HV * const keys = MUTABLE_HV(POPs);
1226 const U8 gimme = GIMME_V;
1228 const I32 dokeys = (PL_op->op_type == OP_KEYS)
1229 || ( PL_op->op_type == OP_AVHVSWITCH
1230 && (PL_op->op_private & OPpAVHVSWITCH_MASK)
1231 + OP_EACH == OP_KEYS);
1233 const I32 dovalues = (PL_op->op_type == OP_VALUES)
1234 || ( PL_op->op_type == OP_AVHVSWITCH
1235 && (PL_op->op_private & OPpAVHVSWITCH_MASK)
1236 + OP_EACH == OP_VALUES);
1238 assert( PL_op->op_type == OP_KEYS
1239 || PL_op->op_type == OP_VALUES
1240 || PL_op->op_type == OP_AVHVSWITCH);
1242 assert(!( PL_op->op_type == OP_VALUES
1243 && (PL_op->op_private & OPpMAYBE_LVSUB)));
1245 (void)hv_iterinit(keys); /* always reset iterator regardless */
1247 if (gimme == G_VOID)
1250 if (gimme == G_SCALAR) {
1251 if (PL_op->op_flags & OPf_MOD || LVRET) { /* lvalue */
1252 SV * const ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
1253 sv_magic(ret, NULL, PERL_MAGIC_nkeys, NULL, 0);
1255 LvTARG(ret) = SvREFCNT_inc_simple(keys);
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
1267 if (! SvTIED_mg((const SV *)keys, PERL_MAGIC_tied) ) {
1268 i = HvUSEDKEYS(keys);
1272 while (hv_iternext(keys)) i++;
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");
1287 hv_pushkv(keys, (dokeys | (dovalues << 1)));
1292 * ex: set ts=8 sts=4 sw=4 et: