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
31 /* Helper function for do_trans().
32 * Handles cases where the search and replacement charlists aren't UTF-8,
33 * aren't identical, and neither the /d nor /s flag is present.
35 * sv may or may not be utf8. Note that no code point above 255 can possibly
36 * be in the to-translate set
40 S_do_trans_simple(pTHX_ SV * const sv, const OPtrans_map * const tbl)
44 U8 *s = (U8*)SvPV_nomg(sv,len);
45 U8 * const send = s+len;
47 PERL_ARGS_ASSERT_DO_TRANS_SIMPLE;
49 /* First, take care of non-UTF-8 input strings, because they're easy */
52 const short ch = tbl->map[*s];
62 const bool grows = cBOOL(PL_op->op_private & OPpTRANS_GROWS);
66 /* Allow for worst-case expansion: Each input byte can become 2. For a
67 * given input character, this happens when it occupies a single byte
68 * under UTF-8, but is to be translated to something that occupies two:
69 * $_="a".chr(400); tr/a/\xFE/, FE needs encoding. */
79 /* Need to check this, otherwise 128..255 won't match */
80 const UV c = utf8n_to_uvchr(s, send - s, &ulen, UTF8_ALLOW_DEFAULT);
81 if (c < 0x100 && (ch = tbl->map[c]) >= 0) {
83 d = uvchr_to_utf8(d, (UV)ch);
86 else { /* No match -> copy */
93 sv_setpvn(sv, (char*)dstart, d - dstart);
98 SvCUR_set(sv, d - dstart);
107 /* Helper function for do_trans().
108 * Handles cases where the search and replacement charlists are identical and
109 * non-utf8: so the string isn't modified, and only a count of modifiable
112 * Note that it doesn't handle /d or /s, since these modify the string even if
113 * the replacement list is empty.
115 * sv may or may not be utf8. Note that no code point above 255 can possibly
116 * be in the to-translate set
120 S_do_trans_count(pTHX_ SV * const sv, const OPtrans_map * const tbl)
123 const U8 *s = (const U8*)SvPV_nomg_const(sv, len);
124 const U8 * const send = s + len;
127 PERL_ARGS_ASSERT_DO_TRANS_COUNT;
131 if (tbl->map[*s++] >= 0)
136 const bool complement = cBOOL(PL_op->op_private & OPpTRANS_COMPLEMENT);
139 const UV c = utf8n_to_uvchr(s, send - s, &ulen, UTF8_ALLOW_DEFAULT);
141 if (tbl->map[c] >= 0)
143 } else if (complement)
153 /* Helper function for do_trans().
154 * Handles cases where the search and replacement charlists aren't identical
155 * and both are non-utf8, and one or both of /d, /s is specified.
157 * sv may or may not be utf8. Note that no code point above 255 can possibly
158 * be in the to-translate set
162 S_do_trans_complex(pTHX_ SV * const sv, const OPtrans_map * const tbl)
165 U8 *s = (U8*)SvPV_nomg(sv, len);
166 U8 * const send = s+len;
168 const bool complement = cBOOL(PL_op->op_private & OPpTRANS_COMPLEMENT);
170 PERL_ARGS_ASSERT_DO_TRANS_COMPLEX;
174 U8 * const dstart = d;
176 if (PL_op->op_private & OPpTRANS_SQUASH) {
178 /* What the mapping of the previous character was to. If the new
179 * character has the same mapping, it is squashed from the output
180 * (but still is included in the count) */
181 short previous_map = (short) TR_OOB;
184 const short this_map = tbl->map[*s];
187 if (this_map != previous_map) {
189 previous_map = this_map;
193 if (this_map == (short) TR_UNMAPPED)
196 assert(this_map == (short) TR_DELETE);
199 previous_map = (short) TR_OOB;
205 else { /* Not to squash */
207 const short this_map = tbl->map[*s];
212 else if (this_map == (short) TR_UNMAPPED)
214 else if (this_map == (short) TR_DELETE)
220 SvCUR_set(sv, d - dstart);
223 const bool squash = cBOOL(PL_op->op_private & OPpTRANS_SQUASH);
224 const bool grows = cBOOL(PL_op->op_private & OPpTRANS_GROWS);
227 Size_t size = tbl->size;
229 /* What the mapping of the previous character was to. If the new
230 * character has the same mapping, it is squashed from the output (but
231 * still is included in the count) */
235 /* Allow for worst-case expansion: Each input byte can become 2.
236 * For a given input character, this happens when it occupies a
237 * single byte under UTF-8, but is to be translated to something
238 * that occupies two: */
239 Newx(d, len*2+1, U8);
246 const UV comp = utf8n_to_uvchr(s, send - s, &len,
254 ? (short) TR_UNMAPPED
261 if (LIKELY(!squash || ch != pch)) {
262 d = uvchr_to_utf8(d, ch);
268 else if (sch == (short) TR_UNMAPPED) {
272 else if (sch == (short) TR_DELETE)
275 assert(sch == (short) TR_R_EMPTY); /* empty replacement */
285 sv_setpvn(sv, (char*)dstart, d - dstart);
290 SvCUR_set(sv, d - dstart);
299 /* Helper function for do_trans().
300 * Handles utf8 cases(*) not involving the /c, /d, /s flags,
301 * and where search and replacement charlists aren't identical.
302 * (*) i.e. where the search or replacement charlists are utf8. sv may
303 * or may not be utf8.
307 S_do_trans_simple_utf8(pTHX_ SV * const sv)
315 const bool grows = cBOOL(PL_op->op_private & OPpTRANS_GROWS);
319 PAD_SVl(cPADOP->op_padix);
321 MUTABLE_SV(cSVOP->op_sv);
323 HV* const hv = MUTABLE_HV(SvRV(rv));
324 SV* const * svp = hv_fetchs(hv, "NONE", FALSE);
325 const UV none = svp ? SvUV(*svp) : 0x7fffffff;
326 const UV extra = none + 1;
330 PERL_ARGS_ASSERT_DO_TRANS_SIMPLE_UTF8;
332 s = (U8*)SvPV_nomg(sv, len);
334 hibit = ! is_utf8_invariant_string(s, len);
336 s = bytes_to_utf8(s, &len);
342 svp = hv_fetchs(hv, "FINAL", FALSE);
347 /* d needs to be bigger than s, in case e.g. upgrading is required */
348 Newx(d, len * 3 + UTF8_MAXBYTES, U8);
358 const UV uv = swash_fetch(rv, s, TRUE);
362 d = uvchr_to_utf8(d, uv);
364 else if (uv == none) {
365 const int i = UTF8SKIP(s);
370 else if (uv == extra) {
373 d = uvchr_to_utf8(d, final);
379 const STRLEN clen = d - dstart;
380 const STRLEN nlen = dend - dstart + len + UTF8_MAXBYTES;
382 Perl_croak(aTHX_ "panic: do_trans_simple_utf8 line %d",__LINE__);
383 Renew(dstart, nlen + UTF8_MAXBYTES, U8);
385 dend = dstart + nlen;
388 if (grows || hibit) {
389 sv_setpvn(sv, (char*)dstart, d - dstart);
396 SvCUR_set(sv, d - dstart);
405 /* Helper function for do_trans().
406 * Handles utf8 cases(*) where search and replacement charlists are
407 * identical: so the string isn't modified, and only a count of modifiable
409 * Note that it doesn't handle /d or /s, since these modify the string
410 * even if the replacement charlist is empty.
411 * (*) i.e. where the search or replacement charlists are utf8. sv may
412 * or may not be utf8.
416 S_do_trans_count_utf8(pTHX_ SV * const sv)
419 const U8 *start = NULL;
425 PAD_SVl(cPADOP->op_padix);
427 MUTABLE_SV(cSVOP->op_sv);
429 HV* const hv = MUTABLE_HV(SvRV(rv));
430 SV* const * const svp = hv_fetchs(hv, "NONE", FALSE);
431 const UV none = svp ? SvUV(*svp) : 0x7fffffff;
432 const UV extra = none + 1;
435 PERL_ARGS_ASSERT_DO_TRANS_COUNT_UTF8;
437 s = (const U8*)SvPV_nomg_const(sv, len);
439 hibit = ! is_utf8_invariant_string(s, len);
441 start = s = bytes_to_utf8(s, &len);
447 const UV uv = swash_fetch(rv, s, TRUE);
448 if (uv < none || uv == extra)
459 /* Helper function for do_trans().
460 * Handles utf8 cases(*) involving the /c, /d, /s flags,
461 * and where search and replacement charlists aren't identical.
462 * (*) i.e. where the search or replacement charlists are utf8. sv may
463 * or may not be utf8.
467 S_do_trans_complex_utf8(pTHX_ SV * const sv)
472 const bool squash = cBOOL(PL_op->op_private & OPpTRANS_SQUASH);
473 const bool del = cBOOL(PL_op->op_private & OPpTRANS_DELETE);
474 const bool grows = cBOOL(PL_op->op_private & OPpTRANS_GROWS);
477 PAD_SVl(cPADOP->op_padix);
479 MUTABLE_SV(cSVOP->op_sv);
481 HV * const hv = MUTABLE_HV(SvRV(rv));
482 SV * const *svp = hv_fetchs(hv, "NONE", FALSE);
483 const UV none = svp ? SvUV(*svp) : 0x7fffffff;
484 const UV extra = none + 1;
486 bool havefinal = FALSE;
490 U8 *s = (U8*)SvPV_nomg(sv, len);
492 PERL_ARGS_ASSERT_DO_TRANS_COMPLEX_UTF8;
495 hibit = ! is_utf8_invariant_string(s, len);
497 s = bytes_to_utf8(s, &len);
503 svp = hv_fetchs(hv, "FINAL", FALSE);
510 /* d needs to be bigger than s, in case e.g. upgrading is required */
511 Newx(d, len * 3 + UTF8_MAXBYTES, U8);
523 UV uv = swash_fetch(rv, s, TRUE);
526 const STRLEN clen = d - dstart;
527 const STRLEN nlen = dend - dstart + len + UTF8_MAXBYTES;
529 Perl_croak(aTHX_ "panic: do_trans_complex_utf8 line %d",__LINE__);
530 Renew(dstart, nlen + UTF8_MAXBYTES, U8);
532 dend = dstart + nlen;
538 d = uvchr_to_utf8(d, uv);
543 else if (uv == none) { /* "none" is unmapped character */
544 const int i = UTF8SKIP(s);
551 else if (uv == extra && !del) {
556 d = uvchr_to_utf8(d, final);
562 uv = utf8n_to_uvchr(s, send - s, &len, UTF8_ALLOW_DEFAULT);
572 matches++; /* "none+1" is delete character */
578 const UV uv = swash_fetch(rv, s, TRUE);
580 const STRLEN clen = d - dstart;
581 const STRLEN nlen = dend - dstart + len + UTF8_MAXBYTES;
583 Perl_croak(aTHX_ "panic: do_trans_complex_utf8 line %d",__LINE__);
584 Renew(dstart, nlen + UTF8_MAXBYTES, U8);
586 dend = dstart + nlen;
591 d = uvchr_to_utf8(d, uv);
594 else if (uv == none) { /* "none" is unmapped character */
595 const int i = UTF8SKIP(s);
601 else if (uv == extra && !del) {
604 d = uvchr_to_utf8(d, final);
607 matches++; /* "none+1" is delete character */
611 if (grows || hibit) {
612 sv_setpvn(sv, (char*)dstart, d - dstart);
619 SvCUR_set(sv, d - dstart);
628 /* Execute a tr//. sv is the value to be translated, while PL_op
629 * should be an OP_TRANS or OP_TRANSR op, whose op_pv field contains a
630 * translation table or whose op_sv field contains a swash.
631 * Returns a count of number of characters translated
635 Perl_do_trans(pTHX_ SV *sv)
638 const U8 flags = PL_op->op_private;
639 const U8 hasutf = flags & (OPpTRANS_FROM_UTF | OPpTRANS_TO_UTF);
641 PERL_ARGS_ASSERT_DO_TRANS;
643 if (SvREADONLY(sv) && !(flags & OPpTRANS_IDENTICAL)) {
644 Perl_croak_no_modify();
646 (void)SvPV_const(sv, len);
649 if (!(flags & OPpTRANS_IDENTICAL)) {
650 if (!SvPOKp(sv) || SvTHINKFIRST(sv))
651 (void)SvPV_force_nomg(sv, len);
652 (void)SvPOK_only_UTF8(sv);
655 /* If we use only OPpTRANS_IDENTICAL to bypass the READONLY check,
656 * we must also rely on it to choose the readonly strategy.
658 if (flags & OPpTRANS_IDENTICAL) {
659 return hasutf ? do_trans_count_utf8(sv) : do_trans_count(sv, (OPtrans_map*)cPVOP->op_pv);
660 } else if (flags & (OPpTRANS_SQUASH|OPpTRANS_DELETE|OPpTRANS_COMPLEMENT)) {
661 return hasutf ? do_trans_complex_utf8(sv) : do_trans_complex(sv, (OPtrans_map*)cPVOP->op_pv);
663 return hasutf ? do_trans_simple_utf8(sv) : do_trans_simple(sv, (OPtrans_map*)cPVOP->op_pv);
668 Perl_do_join(pTHX_ SV *sv, SV *delim, SV **mark, SV **sp)
670 SV ** const oldmark = mark;
671 I32 items = sp - mark;
674 const char * const delims = SvPV_const(delim, delimlen);
676 PERL_ARGS_ASSERT_DO_JOIN;
679 len = (items > 0 ? (delimlen * (items - 1) ) : 0);
680 SvUPGRADE(sv, SVt_PV);
681 if (SvLEN(sv) < len + items) { /* current length is way too short */
682 while (items-- > 0) {
683 if (*mark && !SvGAMAGIC(*mark) && SvOK(*mark)) {
685 SvPV_const(*mark, tmplen);
690 SvGROW(sv, len + 1); /* so try to pre-extend */
698 /* sv_setpv retains old UTF8ness [perl #24846] */
701 if (TAINTING_get && SvMAGICAL(sv))
711 const U32 delimflag = DO_UTF8(delim) ? SV_CATUTF8 : SV_CATBYTES;
712 for (; items > 0; items--,mark++) {
715 sv_catpvn_flags(sv,delims,delimlen,delimflag);
716 s = SvPV_const(*mark,len);
717 sv_catpvn_flags(sv,s,len,
718 DO_UTF8(*mark) ? SV_CATUTF8 : SV_CATBYTES);
722 for (; items > 0; items--,mark++)
725 const char *s = SvPV_const(*mark,len);
726 sv_catpvn_flags(sv,s,len,
727 DO_UTF8(*mark) ? SV_CATUTF8 : SV_CATBYTES);
734 Perl_do_sprintf(pTHX_ SV *sv, SSize_t len, SV **sarg)
737 const char * const pat = SvPV_const(*sarg, patlen);
738 bool do_taint = FALSE;
740 PERL_ARGS_ASSERT_DO_SPRINTF;
743 if (SvTAINTED(*sarg))
745 (PL_op && PL_op->op_type < OP_max)
746 ? (PL_op->op_type == OP_PRTF)
748 : PL_op_name[PL_op->op_type]
754 sv_vsetpvfn(sv, pat, patlen, NULL, sarg + 1, (Size_t)(len - 1), &do_taint);
760 /* currently converts input to bytes if possible, but doesn't sweat failure */
762 Perl_do_vecget(pTHX_ SV *sv, STRLEN offset, int size)
764 STRLEN srclen, len, avail, uoffset, bitoffs = 0;
765 const I32 svpv_flags = ((PL_op->op_flags & OPf_MOD || LVRET)
766 ? SV_UNDEF_RETURNS_NULL : 0);
767 unsigned char *s = (unsigned char *)
768 SvPV_flags(sv, srclen, (svpv_flags|SV_GMAGIC));
772 s = (unsigned char *)"";
775 PERL_ARGS_ASSERT_DO_VECGET;
777 if (size < 1 || (size & (size-1))) /* size < 1 or not a power of two */
778 Perl_croak(aTHX_ "Illegal number of bits in vec");
781 if (Perl_sv_utf8_downgrade_flags(aTHX_ sv, TRUE, 0)) {
782 /* PVX may have changed */
783 s = (unsigned char *) SvPV_flags(sv, srclen, svpv_flags);
786 Perl_croak(aTHX_ "Use of strings with code points over 0xFF as arguments to vec is forbidden");
791 bitoffs = ((offset%8)*size)%8;
792 uoffset = offset/(8/size);
796 if (offset > Size_t_MAX / n - 1) /* would overflow */
803 if (uoffset >= srclen)
806 len = (bitoffs + size + 7)/8; /* required number of bytes */
807 avail = srclen - uoffset; /* available number of bytes */
809 /* Does the byte range overlap the end of the string? If so,
810 * handle specially. */
817 retnum = (UV) s[uoffset] << 8;
819 else if (size == 32) {
820 assert(avail >= 1 && avail <= 3);
823 ((UV) s[uoffset ] << 24);
826 ((UV) s[uoffset ] << 24) +
827 ((UV) s[uoffset + 1] << 16);
830 ((UV) s[uoffset ] << 24) +
831 ((UV) s[uoffset + 1] << 16) +
832 ( s[uoffset + 2] << 8);
835 else if (size == 64) {
836 Perl_ck_warner(aTHX_ packWARN(WARN_PORTABLE),
837 "Bit vector size > 32 non-portable");
838 assert(avail >= 1 && avail <= 7);
841 (UV) s[uoffset ] << 56;
844 ((UV) s[uoffset ] << 56) +
845 ((UV) s[uoffset + 1] << 48);
848 ((UV) s[uoffset ] << 56) +
849 ((UV) s[uoffset + 1] << 48) +
850 ((UV) s[uoffset + 2] << 40);
853 ((UV) s[uoffset ] << 56) +
854 ((UV) s[uoffset + 1] << 48) +
855 ((UV) s[uoffset + 2] << 40) +
856 ((UV) s[uoffset + 3] << 32);
859 ((UV) s[uoffset ] << 56) +
860 ((UV) s[uoffset + 1] << 48) +
861 ((UV) s[uoffset + 2] << 40) +
862 ((UV) s[uoffset + 3] << 32) +
863 ((UV) s[uoffset + 4] << 24);
866 ((UV) s[uoffset ] << 56) +
867 ((UV) s[uoffset + 1] << 48) +
868 ((UV) s[uoffset + 2] << 40) +
869 ((UV) s[uoffset + 3] << 32) +
870 ((UV) s[uoffset + 4] << 24) +
871 ((UV) s[uoffset + 5] << 16);
874 ((UV) s[uoffset ] << 56) +
875 ((UV) s[uoffset + 1] << 48) +
876 ((UV) s[uoffset + 2] << 40) +
877 ((UV) s[uoffset + 3] << 32) +
878 ((UV) s[uoffset + 4] << 24) +
879 ((UV) s[uoffset + 5] << 16) +
880 ((UV) s[uoffset + 6] << 8);
886 retnum = (s[uoffset] >> bitoffs) & ((1 << size) - 1);
892 ((UV) s[uoffset] << 8) +
896 ((UV) s[uoffset ] << 24) +
897 ((UV) s[uoffset + 1] << 16) +
898 ( s[uoffset + 2] << 8) +
901 else if (size == 64) {
902 Perl_ck_warner(aTHX_ packWARN(WARN_PORTABLE),
903 "Bit vector size > 32 non-portable");
905 ((UV) s[uoffset ] << 56) +
906 ((UV) s[uoffset + 1] << 48) +
907 ((UV) s[uoffset + 2] << 40) +
908 ((UV) s[uoffset + 3] << 32) +
909 ((UV) s[uoffset + 4] << 24) +
910 ((UV) s[uoffset + 5] << 16) +
911 ( s[uoffset + 6] << 8) +
920 /* currently converts input to bytes if possible but doesn't sweat failures,
921 * although it does ensure that the string it clobbers is not marked as
922 * utf8-valid any more
925 Perl_do_vecset(pTHX_ SV *sv)
927 STRLEN offset, bitoffs = 0;
934 SV * const targ = LvTARG(sv);
935 char errflags = LvFLAGS(sv);
937 PERL_ARGS_ASSERT_DO_VECSET;
939 /* some out-of-range errors have been deferred if/until the LV is
940 * actually written to: f(vec($s,-1,8)) is not always fatal */
942 assert(!(errflags & ~(LVf_NEG_OFF|LVf_OUT_OF_RANGE)));
943 if (errflags & LVf_NEG_OFF)
944 Perl_croak_nocontext("Negative offset to vec in lvalue context");
945 Perl_croak_nocontext("Out of memory!");
950 s = (unsigned char*)SvPV_force_flags(targ, targlen,
951 SV_GMAGIC | SV_UNDEF_RETURNS_NULL);
953 /* This is handled by the SvPOK_only below...
954 if (!Perl_sv_utf8_downgrade_flags(aTHX_ targ, TRUE, 0))
957 (void) Perl_sv_utf8_downgrade_flags(aTHX_ targ, TRUE, 0);
960 (void)SvPOK_only(targ);
962 offset = LvTARGOFF(sv);
963 size = LvTARGLEN(sv);
965 if (size < 1 || (size & (size-1))) /* size < 1 or not a power of two */
966 Perl_croak(aTHX_ "Illegal number of bits in vec");
969 bitoffs = ((offset%8)*size)%8;
974 if (offset > Size_t_MAX / n - 1) /* would overflow */
975 Perl_croak_nocontext("Out of memory!");
979 len = (bitoffs + size + 7)/8; /* required number of bytes */
980 if (targlen < offset || targlen - offset < len) {
981 STRLEN newlen = offset > Size_t_MAX - len - 1 ? /* avoid overflow */
982 Size_t_MAX : offset + len + 1;
983 s = (unsigned char*)SvGROW(targ, newlen);
984 (void)memzero((char *)(s + targlen), newlen - targlen);
985 SvCUR_set(targ, newlen - 1);
989 mask = (1 << size) - 1;
991 s[offset] &= ~(mask << bitoffs);
992 s[offset] |= lval << bitoffs;
996 s[offset ] = (U8)( lval & 0xff);
997 else if (size == 16) {
998 s[offset ] = (U8)((lval >> 8) & 0xff);
999 s[offset+1] = (U8)( lval & 0xff);
1001 else if (size == 32) {
1002 s[offset ] = (U8)((lval >> 24) & 0xff);
1003 s[offset+1] = (U8)((lval >> 16) & 0xff);
1004 s[offset+2] = (U8)((lval >> 8) & 0xff);
1005 s[offset+3] = (U8)( lval & 0xff);
1008 else if (size == 64) {
1009 Perl_ck_warner(aTHX_ packWARN(WARN_PORTABLE),
1010 "Bit vector size > 32 non-portable");
1011 s[offset ] = (U8)((lval >> 56) & 0xff);
1012 s[offset+1] = (U8)((lval >> 48) & 0xff);
1013 s[offset+2] = (U8)((lval >> 40) & 0xff);
1014 s[offset+3] = (U8)((lval >> 32) & 0xff);
1015 s[offset+4] = (U8)((lval >> 24) & 0xff);
1016 s[offset+5] = (U8)((lval >> 16) & 0xff);
1017 s[offset+6] = (U8)((lval >> 8) & 0xff);
1018 s[offset+7] = (U8)( lval & 0xff);
1026 Perl_do_vop(pTHX_ I32 optype, SV *sv, SV *left, SV *right)
1041 bool result_needs_to_be_utf8 = FALSE;
1042 bool left_utf8 = FALSE;
1043 bool right_utf8 = FALSE;
1044 U8 * left_non_downgraded = NULL;
1045 U8 * right_non_downgraded = NULL;
1046 Size_t left_non_downgraded_len = 0;
1047 Size_t right_non_downgraded_len = 0;
1048 char * non_downgraded = NULL;
1049 Size_t non_downgraded_len = 0;
1051 PERL_ARGS_ASSERT_DO_VOP;
1053 if (sv != left || (optype != OP_BIT_AND && !SvOK(sv)))
1054 SvPVCLEAR(sv); /* avoid undef warning on |= and ^= */
1056 lc = SvPV_force_nomg(left, leftlen);
1059 lc = SvPV_nomg_const(left, leftlen);
1060 SvPV_force_nomg_nolen(sv);
1062 rc = SvPV_nomg_const(right, rightlen);
1064 /* This needs to come after SvPV to ensure that string overloading has
1067 /* Create downgraded temporaries of any UTF-8 encoded operands */
1068 if (DO_UTF8(left)) {
1069 const U8 * save_lc = (U8 *) lc;
1072 result_needs_to_be_utf8 = TRUE;
1074 left_non_downgraded_len = leftlen;
1075 lc = (char *) bytes_from_utf8_loc((const U8 *) lc, &leftlen,
1077 (const U8 **) &left_non_downgraded);
1078 /* Calculate the number of trailing unconvertible bytes. This quantity
1079 * is the original length minus the length of the converted portion. */
1080 left_non_downgraded_len -= left_non_downgraded - save_lc;
1083 if (DO_UTF8(right)) {
1084 const U8 * save_rc = (U8 *) rc;
1087 result_needs_to_be_utf8 = TRUE;
1089 right_non_downgraded_len = rightlen;
1090 rc = (char *) bytes_from_utf8_loc((const U8 *) rc, &rightlen,
1092 (const U8 **) &right_non_downgraded);
1093 right_non_downgraded_len -= right_non_downgraded - save_rc;
1097 /* We set 'len' to the length that the operation actually operates on. The
1098 * dangling part of the longer operand doesn't actually participate in the
1099 * operation. What happens is that we pretend that the shorter operand has
1100 * been extended to the right by enough imaginary zeros to match the length
1101 * of the longer one. But we know in advance the result of the operation
1102 * on zeros without having to do it. In the case of '&', the result is
1103 * zero, and the dangling portion is simply discarded. For '|' and '^', the
1104 * result is the same as the other operand, so the dangling part is just
1105 * appended to the final result, unchanged. As of perl-5.32, we no longer
1106 * accept above-FF code points in the dangling portion.
1108 if (left_utf8 || right_utf8) {
1109 Perl_croak(aTHX_ FATAL_ABOVE_FF_MSG, PL_op_desc[optype]);
1111 else { /* Neither is UTF-8 */
1112 len = MIN(leftlen, rightlen);
1120 (void)SvPOK_only(sv);
1121 if (SvOK(sv) || SvTYPE(sv) > SVt_PVMG) {
1122 dc = SvPV_force_nomg_nolen(sv);
1123 if (SvLEN(sv) < len + 1) {
1124 dc = SvGROW(sv, len + 1);
1125 (void)memzero(dc + SvCUR(sv), len - SvCUR(sv) + 1);
1129 needlen = optype == OP_BIT_AND
1130 ? len : (leftlen > rightlen ? leftlen : rightlen);
1131 Newxz(dc, needlen + 1, char);
1132 sv_usepvn_flags(sv, dc, needlen, SV_HAS_TRAILING_NUL);
1133 dc = SvPVX(sv); /* sv_usepvn() calls Renew() */
1136 if (len >= sizeof(long)*4 &&
1137 !(PTR2nat(dc) % sizeof(long)) &&
1138 !(PTR2nat(lc) % sizeof(long)) &&
1139 !(PTR2nat(rc) % sizeof(long))) /* It's almost always aligned... */
1141 const STRLEN remainder = len % (sizeof(long)*4);
1142 len /= (sizeof(long)*4);
1151 *dl++ = *ll++ & *rl++;
1152 *dl++ = *ll++ & *rl++;
1153 *dl++ = *ll++ & *rl++;
1154 *dl++ = *ll++ & *rl++;
1159 *dl++ = *ll++ ^ *rl++;
1160 *dl++ = *ll++ ^ *rl++;
1161 *dl++ = *ll++ ^ *rl++;
1162 *dl++ = *ll++ ^ *rl++;
1167 *dl++ = *ll++ | *rl++;
1168 *dl++ = *ll++ | *rl++;
1169 *dl++ = *ll++ | *rl++;
1170 *dl++ = *ll++ | *rl++;
1184 *dc++ = *lc++ & *rc++;
1189 *dc++ = *lc++ ^ *rc++;
1193 *dc++ = *lc++ | *rc++;
1196 if (rightlen > len) {
1198 SvCUR_set(sv, rightlen);
1200 sv_catpvn_nomg(sv, rsave + len, rightlen - len);
1202 else if (leftlen > len) {
1204 SvCUR_set(sv, leftlen);
1206 sv_catpvn_nomg(sv, lsave + len, leftlen - len);
1210 /* If there is trailing stuff that couldn't be converted from UTF-8, it
1211 * is appended as-is for the ^ and | operators. This preserves
1212 * backwards compatibility */
1213 if (right_non_downgraded) {
1214 non_downgraded = (char *) right_non_downgraded;
1215 non_downgraded_len = right_non_downgraded_len;
1217 else if (left_non_downgraded) {
1218 non_downgraded = (char *) left_non_downgraded;
1219 non_downgraded_len = left_non_downgraded_len;
1225 if (result_needs_to_be_utf8) {
1226 sv_utf8_upgrade_nomg(sv);
1228 /* Append any trailing UTF-8 as-is. */
1229 if (non_downgraded) {
1230 sv_catpvn_nomg(sv, non_downgraded, non_downgraded_len);
1238 /* Perl_do_kv() may be:
1239 * * called directly as the pp function for pp_keys() and pp_values();
1240 * * It may also be called directly when the op is OP_AVHVSWITCH, to
1241 * implement CORE::keys(), CORE::values().
1243 * In all cases it expects an HV on the stack and returns a list of keys,
1244 * values, or key-value pairs, depending on PL_op.
1251 HV * const keys = MUTABLE_HV(POPs);
1252 const U8 gimme = GIMME_V;
1254 const I32 dokeys = (PL_op->op_type == OP_KEYS)
1255 || ( PL_op->op_type == OP_AVHVSWITCH
1256 && (PL_op->op_private & OPpAVHVSWITCH_MASK)
1257 + OP_EACH == OP_KEYS);
1259 const I32 dovalues = (PL_op->op_type == OP_VALUES)
1260 || ( PL_op->op_type == OP_AVHVSWITCH
1261 && (PL_op->op_private & OPpAVHVSWITCH_MASK)
1262 + OP_EACH == OP_VALUES);
1264 assert( PL_op->op_type == OP_KEYS
1265 || PL_op->op_type == OP_VALUES
1266 || PL_op->op_type == OP_AVHVSWITCH);
1268 assert(!( PL_op->op_type == OP_VALUES
1269 && (PL_op->op_private & OPpMAYBE_LVSUB)));
1271 (void)hv_iterinit(keys); /* always reset iterator regardless */
1273 if (gimme == G_VOID)
1276 if (gimme == G_SCALAR) {
1277 if (PL_op->op_flags & OPf_MOD || LVRET) { /* lvalue */
1278 SV * const ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
1279 sv_magic(ret, NULL, PERL_MAGIC_nkeys, NULL, 0);
1281 LvTARG(ret) = SvREFCNT_inc_simple(keys);
1288 /* note that in 'scalar(keys %h)' the OP_KEYS is usually
1289 * optimised away and the action is performed directly by the
1290 * padhv or rv2hv op. We now only get here via OP_AVHVSWITCH
1293 if (! SvTIED_mg((const SV *)keys, PERL_MAGIC_tied) ) {
1294 i = HvUSEDKEYS(keys);
1298 while (hv_iternext(keys)) i++;
1305 if (UNLIKELY(PL_op->op_private & OPpMAYBE_LVSUB)) {
1306 const I32 flags = is_lvalue_sub();
1307 if (flags && !(flags & OPpENTERSUB_INARGS))
1308 /* diag_listed_as: Can't modify %s in %s */
1309 Perl_croak(aTHX_ "Can't modify keys in list assignment");
1313 hv_pushkv(keys, (dokeys | (dovalues << 1)));
1318 * ex: set ts=8 sts=4 sw=4 et: