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 non-utf8 cases(*) not involving the /c, /d, /s flags,
33 * and where search and replacement charlists aren't identical.
34 * (*) i.e. where the search and replacement charlists are non-utf8. sv may
39 S_do_trans_simple(pTHX_ SV * const sv, const OPtrans_map * const tbl)
43 U8 *s = (U8*)SvPV_nomg(sv,len);
44 U8 * const send = s+len;
46 PERL_ARGS_ASSERT_DO_TRANS_SIMPLE;
48 /* First, take care of non-UTF-8 input strings, because they're easy */
51 const short ch = tbl->map[*s];
61 const bool grows = cBOOL(PL_op->op_private & OPpTRANS_GROWS);
65 /* Allow for expansion: $_="a".chr(400); tr/a/\xFE/, FE needs encoding */
75 /* Need to check this, otherwise 128..255 won't match */
76 const UV c = utf8n_to_uvchr(s, send - s, &ulen, UTF8_ALLOW_DEFAULT);
77 if (c < 0x100 && (ch = tbl->map[c]) >= 0) {
79 d = uvchr_to_utf8(d, (UV)ch);
82 else { /* No match -> copy */
89 sv_setpvn(sv, (char*)dstart, d - dstart);
94 SvCUR_set(sv, d - dstart);
103 /* Helper function for do_trans().
104 * Handles non-utf8 cases(*) where search and replacement charlists are
105 * identical: so the string isn't modified, and only a count of modifiable
107 * Note that it doesn't handle /d or /s, since these modify the string
108 * even if the replacement list is empty.
109 * (*) i.e. where the search and replacement charlists are non-utf8. sv may
110 * or may not be utf8.
114 S_do_trans_count(pTHX_ SV * const sv, const OPtrans_map * const tbl)
117 const U8 *s = (const U8*)SvPV_nomg_const(sv, len);
118 const U8 * const send = s + len;
121 PERL_ARGS_ASSERT_DO_TRANS_COUNT;
125 if (tbl->map[*s++] >= 0)
130 const bool complement = cBOOL(PL_op->op_private & OPpTRANS_COMPLEMENT);
133 const UV c = utf8n_to_uvchr(s, send - s, &ulen, UTF8_ALLOW_DEFAULT);
135 if (tbl->map[c] >= 0)
137 } else if (complement)
147 /* Helper function for do_trans().
148 * Handles non-utf8 cases(*) involving the /c, /d, /s flags,
149 * and where search and replacement charlists aren't identical.
150 * (*) i.e. where the search and replacement charlists are non-utf8. sv may
151 * or may not be utf8.
155 S_do_trans_complex(pTHX_ SV * const sv, const OPtrans_map * const tbl)
158 U8 *s = (U8*)SvPV_nomg(sv, len);
159 U8 * const send = s+len;
162 PERL_ARGS_ASSERT_DO_TRANS_COMPLEX;
166 U8 * const dstart = d;
168 if (PL_op->op_private & OPpTRANS_SQUASH) {
171 const short ch = tbl->map[*s];
175 if (p != d - 1 || *p != *d)
178 else if (ch == (short) TR_UNMAPPED)
180 else if (ch == (short) TR_DELETE)
187 const short ch = tbl->map[*s];
192 else if (ch == (short) TR_UNMAPPED)
194 else if (ch == (short) TR_DELETE)
200 SvCUR_set(sv, d - dstart);
203 const bool squash = cBOOL(PL_op->op_private & OPpTRANS_SQUASH);
204 const bool grows = cBOOL(PL_op->op_private & OPpTRANS_GROWS);
207 Size_t size = tbl->size;
211 Newx(d, len*2+1, U8);
218 const UV comp = utf8n_to_uvchr(s, send - s, &len,
223 sch = tbl->map[comp >= size ? size : comp];
229 if (LIKELY(!squash || ch != pch)) {
230 d = uvchr_to_utf8(d, ch);
236 else if (sch == (short) TR_UNMAPPED) {
240 else if (sch == (short) TR_DELETE)
243 assert(sch == (short) TR_R_EMPTY); /* empty replacement */
253 sv_setpvn(sv, (char*)dstart, d - dstart);
258 SvCUR_set(sv, d - dstart);
267 /* Helper function for do_trans().
268 * Handles utf8 cases(*) not involving the /c, /d, /s flags,
269 * and where search and replacement charlists aren't identical.
270 * (*) i.e. where the search or replacement charlists are utf8. sv may
271 * or may not be utf8.
275 S_do_trans_simple_utf8(pTHX_ SV * const sv)
283 const bool grows = cBOOL(PL_op->op_private & OPpTRANS_GROWS);
287 PAD_SVl(cPADOP->op_padix);
289 MUTABLE_SV(cSVOP->op_sv);
291 HV* const hv = MUTABLE_HV(SvRV(rv));
292 SV* const * svp = hv_fetchs(hv, "NONE", FALSE);
293 const UV none = svp ? SvUV(*svp) : 0x7fffffff;
294 const UV extra = none + 1;
298 PERL_ARGS_ASSERT_DO_TRANS_SIMPLE_UTF8;
300 s = (U8*)SvPV_nomg(sv, len);
302 hibit = ! is_utf8_invariant_string(s, len);
304 s = bytes_to_utf8(s, &len);
310 svp = hv_fetchs(hv, "FINAL", FALSE);
315 /* d needs to be bigger than s, in case e.g. upgrading is required */
316 Newx(d, len * 3 + UTF8_MAXBYTES, U8);
326 const UV uv = swash_fetch(rv, s, TRUE);
330 d = uvchr_to_utf8(d, uv);
332 else if (uv == none) {
333 const int i = UTF8SKIP(s);
338 else if (uv == extra) {
341 d = uvchr_to_utf8(d, final);
347 const STRLEN clen = d - dstart;
348 const STRLEN nlen = dend - dstart + len + UTF8_MAXBYTES;
350 Perl_croak(aTHX_ "panic: do_trans_simple_utf8 line %d",__LINE__);
351 Renew(dstart, nlen + UTF8_MAXBYTES, U8);
353 dend = dstart + nlen;
356 if (grows || hibit) {
357 sv_setpvn(sv, (char*)dstart, d - dstart);
364 SvCUR_set(sv, d - dstart);
373 /* Helper function for do_trans().
374 * Handles utf8 cases(*) where search and replacement charlists are
375 * identical: so the string isn't modified, and only a count of modifiable
377 * Note that it doesn't handle /d or /s, since these modify the string
378 * even if the replacement charlist is empty.
379 * (*) i.e. where the search or replacement charlists are utf8. sv may
380 * or may not be utf8.
384 S_do_trans_count_utf8(pTHX_ SV * const sv)
387 const U8 *start = NULL;
393 PAD_SVl(cPADOP->op_padix);
395 MUTABLE_SV(cSVOP->op_sv);
397 HV* const hv = MUTABLE_HV(SvRV(rv));
398 SV* const * const svp = hv_fetchs(hv, "NONE", FALSE);
399 const UV none = svp ? SvUV(*svp) : 0x7fffffff;
400 const UV extra = none + 1;
403 PERL_ARGS_ASSERT_DO_TRANS_COUNT_UTF8;
405 s = (const U8*)SvPV_nomg_const(sv, len);
407 hibit = ! is_utf8_invariant_string(s, len);
409 start = s = bytes_to_utf8(s, &len);
415 const UV uv = swash_fetch(rv, s, TRUE);
416 if (uv < none || uv == extra)
427 /* Helper function for do_trans().
428 * Handles utf8 cases(*) involving the /c, /d, /s flags,
429 * and where search and replacement charlists aren't identical.
430 * (*) i.e. where the search or replacement charlists are utf8. sv may
431 * or may not be utf8.
435 S_do_trans_complex_utf8(pTHX_ SV * const sv)
440 const bool squash = cBOOL(PL_op->op_private & OPpTRANS_SQUASH);
441 const bool del = cBOOL(PL_op->op_private & OPpTRANS_DELETE);
442 const bool grows = cBOOL(PL_op->op_private & OPpTRANS_GROWS);
445 PAD_SVl(cPADOP->op_padix);
447 MUTABLE_SV(cSVOP->op_sv);
449 HV * const hv = MUTABLE_HV(SvRV(rv));
450 SV * const *svp = hv_fetchs(hv, "NONE", FALSE);
451 const UV none = svp ? SvUV(*svp) : 0x7fffffff;
452 const UV extra = none + 1;
454 bool havefinal = FALSE;
458 U8 *s = (U8*)SvPV_nomg(sv, len);
460 PERL_ARGS_ASSERT_DO_TRANS_COMPLEX_UTF8;
463 hibit = ! is_utf8_invariant_string(s, len);
465 s = bytes_to_utf8(s, &len);
471 svp = hv_fetchs(hv, "FINAL", FALSE);
478 /* d needs to be bigger than s, in case e.g. upgrading is required */
479 Newx(d, len * 3 + UTF8_MAXBYTES, U8);
491 UV uv = swash_fetch(rv, s, TRUE);
494 const STRLEN clen = d - dstart;
495 const STRLEN nlen = dend - dstart + len + UTF8_MAXBYTES;
497 Perl_croak(aTHX_ "panic: do_trans_complex_utf8 line %d",__LINE__);
498 Renew(dstart, nlen + UTF8_MAXBYTES, U8);
500 dend = dstart + nlen;
506 d = uvchr_to_utf8(d, uv);
511 else if (uv == none) { /* "none" is unmapped character */
512 const int i = UTF8SKIP(s);
519 else if (uv == extra && !del) {
524 d = uvchr_to_utf8(d, final);
530 uv = utf8n_to_uvchr(s, send - s, &len, UTF8_ALLOW_DEFAULT);
540 matches++; /* "none+1" is delete character */
546 const UV uv = swash_fetch(rv, s, TRUE);
548 const STRLEN clen = d - dstart;
549 const STRLEN nlen = dend - dstart + len + UTF8_MAXBYTES;
551 Perl_croak(aTHX_ "panic: do_trans_complex_utf8 line %d",__LINE__);
552 Renew(dstart, nlen + UTF8_MAXBYTES, U8);
554 dend = dstart + nlen;
559 d = uvchr_to_utf8(d, uv);
562 else if (uv == none) { /* "none" is unmapped character */
563 const int i = UTF8SKIP(s);
569 else if (uv == extra && !del) {
572 d = uvchr_to_utf8(d, final);
575 matches++; /* "none+1" is delete character */
579 if (grows || hibit) {
580 sv_setpvn(sv, (char*)dstart, d - dstart);
587 SvCUR_set(sv, d - dstart);
596 /* Execute a tr//. sv is the value to be translated, while PL_op
597 * should be an OP_TRANS or OP_TRANSR op, whose op_pv field contains a
598 * translation table or whose op_sv field contains a swash.
599 * Returns a count of number of characters translated
603 Perl_do_trans(pTHX_ SV *sv)
606 const U8 flags = PL_op->op_private;
607 const U8 hasutf = flags & (OPpTRANS_FROM_UTF | OPpTRANS_TO_UTF);
609 PERL_ARGS_ASSERT_DO_TRANS;
611 if (SvREADONLY(sv) && !(flags & OPpTRANS_IDENTICAL)) {
612 Perl_croak_no_modify();
614 (void)SvPV_const(sv, len);
617 if (!(flags & OPpTRANS_IDENTICAL)) {
618 if (!SvPOKp(sv) || SvTHINKFIRST(sv))
619 (void)SvPV_force_nomg(sv, len);
620 (void)SvPOK_only_UTF8(sv);
623 /* If we use only OPpTRANS_IDENTICAL to bypass the READONLY check,
624 * we must also rely on it to choose the readonly strategy.
626 if (flags & OPpTRANS_IDENTICAL) {
627 return hasutf ? do_trans_count_utf8(sv) : do_trans_count(sv, (OPtrans_map*)cPVOP->op_pv);
628 } else if (flags & (OPpTRANS_SQUASH|OPpTRANS_DELETE|OPpTRANS_COMPLEMENT)) {
629 return hasutf ? do_trans_complex_utf8(sv) : do_trans_complex(sv, (OPtrans_map*)cPVOP->op_pv);
631 return hasutf ? do_trans_simple_utf8(sv) : do_trans_simple(sv, (OPtrans_map*)cPVOP->op_pv);
636 Perl_do_join(pTHX_ SV *sv, SV *delim, SV **mark, SV **sp)
638 SV ** const oldmark = mark;
639 I32 items = sp - mark;
642 const char * const delims = SvPV_const(delim, delimlen);
644 PERL_ARGS_ASSERT_DO_JOIN;
647 len = (items > 0 ? (delimlen * (items - 1) ) : 0);
648 SvUPGRADE(sv, SVt_PV);
649 if (SvLEN(sv) < len + items) { /* current length is way too short */
650 while (items-- > 0) {
651 if (*mark && !SvGAMAGIC(*mark) && SvOK(*mark)) {
653 SvPV_const(*mark, tmplen);
658 SvGROW(sv, len + 1); /* so try to pre-extend */
666 /* sv_setpv retains old UTF8ness [perl #24846] */
669 if (TAINTING_get && SvMAGICAL(sv))
679 const U32 delimflag = DO_UTF8(delim) ? SV_CATUTF8 : SV_CATBYTES;
680 for (; items > 0; items--,mark++) {
683 sv_catpvn_flags(sv,delims,delimlen,delimflag);
684 s = SvPV_const(*mark,len);
685 sv_catpvn_flags(sv,s,len,
686 DO_UTF8(*mark) ? SV_CATUTF8 : SV_CATBYTES);
690 for (; items > 0; items--,mark++)
693 const char *s = SvPV_const(*mark,len);
694 sv_catpvn_flags(sv,s,len,
695 DO_UTF8(*mark) ? SV_CATUTF8 : SV_CATBYTES);
702 Perl_do_sprintf(pTHX_ SV *sv, SSize_t len, SV **sarg)
705 const char * const pat = SvPV_const(*sarg, patlen);
706 bool do_taint = FALSE;
708 PERL_ARGS_ASSERT_DO_SPRINTF;
711 if (SvTAINTED(*sarg))
713 (PL_op && PL_op->op_type < OP_max)
714 ? (PL_op->op_type == OP_PRTF)
716 : PL_op_name[PL_op->op_type]
722 sv_vsetpvfn(sv, pat, patlen, NULL, sarg + 1, (Size_t)(len - 1), &do_taint);
728 /* currently converts input to bytes if possible, but doesn't sweat failure */
730 Perl_do_vecget(pTHX_ SV *sv, STRLEN offset, int size)
732 STRLEN srclen, len, avail, uoffset, bitoffs = 0;
733 const I32 svpv_flags = ((PL_op->op_flags & OPf_MOD || LVRET)
734 ? SV_UNDEF_RETURNS_NULL : 0);
735 unsigned char *s = (unsigned char *)
736 SvPV_flags(sv, srclen, (svpv_flags|SV_GMAGIC));
740 s = (unsigned char *)"";
743 PERL_ARGS_ASSERT_DO_VECGET;
745 if (size < 1 || (size & (size-1))) /* size < 1 or not a power of two */
746 Perl_croak(aTHX_ "Illegal number of bits in vec");
749 if (Perl_sv_utf8_downgrade_flags(aTHX_ sv, TRUE, 0)) {
750 /* PVX may have changed */
751 s = (unsigned char *) SvPV_flags(sv, srclen, svpv_flags);
754 Perl_croak(aTHX_ "Use of strings with code points over 0xFF as arguments to vec is forbidden");
759 bitoffs = ((offset%8)*size)%8;
760 uoffset = offset/(8/size);
764 if (offset > Size_t_MAX / n - 1) /* would overflow */
771 if (uoffset >= srclen)
774 len = (bitoffs + size + 7)/8; /* required number of bytes */
775 avail = srclen - uoffset; /* available number of bytes */
777 /* Does the byte range overlap the end of the string? If so,
778 * handle specially. */
785 retnum = (UV) s[uoffset] << 8;
787 else if (size == 32) {
788 assert(avail >= 1 && avail <= 3);
791 ((UV) s[uoffset ] << 24);
794 ((UV) s[uoffset ] << 24) +
795 ((UV) s[uoffset + 1] << 16);
798 ((UV) s[uoffset ] << 24) +
799 ((UV) s[uoffset + 1] << 16) +
800 ( s[uoffset + 2] << 8);
803 else if (size == 64) {
804 Perl_ck_warner(aTHX_ packWARN(WARN_PORTABLE),
805 "Bit vector size > 32 non-portable");
806 assert(avail >= 1 && avail <= 7);
809 (UV) s[uoffset ] << 56;
812 ((UV) s[uoffset ] << 56) +
813 ((UV) s[uoffset + 1] << 48);
816 ((UV) s[uoffset ] << 56) +
817 ((UV) s[uoffset + 1] << 48) +
818 ((UV) s[uoffset + 2] << 40);
821 ((UV) s[uoffset ] << 56) +
822 ((UV) s[uoffset + 1] << 48) +
823 ((UV) s[uoffset + 2] << 40) +
824 ((UV) s[uoffset + 3] << 32);
827 ((UV) s[uoffset ] << 56) +
828 ((UV) s[uoffset + 1] << 48) +
829 ((UV) s[uoffset + 2] << 40) +
830 ((UV) s[uoffset + 3] << 32) +
831 ((UV) s[uoffset + 4] << 24);
834 ((UV) s[uoffset ] << 56) +
835 ((UV) s[uoffset + 1] << 48) +
836 ((UV) s[uoffset + 2] << 40) +
837 ((UV) s[uoffset + 3] << 32) +
838 ((UV) s[uoffset + 4] << 24) +
839 ((UV) s[uoffset + 5] << 16);
842 ((UV) s[uoffset ] << 56) +
843 ((UV) s[uoffset + 1] << 48) +
844 ((UV) s[uoffset + 2] << 40) +
845 ((UV) s[uoffset + 3] << 32) +
846 ((UV) s[uoffset + 4] << 24) +
847 ((UV) s[uoffset + 5] << 16) +
848 ((UV) s[uoffset + 6] << 8);
854 retnum = (s[uoffset] >> bitoffs) & ((1 << size) - 1);
860 ((UV) s[uoffset] << 8) +
864 ((UV) s[uoffset ] << 24) +
865 ((UV) s[uoffset + 1] << 16) +
866 ( s[uoffset + 2] << 8) +
869 else if (size == 64) {
870 Perl_ck_warner(aTHX_ packWARN(WARN_PORTABLE),
871 "Bit vector size > 32 non-portable");
873 ((UV) s[uoffset ] << 56) +
874 ((UV) s[uoffset + 1] << 48) +
875 ((UV) s[uoffset + 2] << 40) +
876 ((UV) s[uoffset + 3] << 32) +
877 ((UV) s[uoffset + 4] << 24) +
878 ((UV) s[uoffset + 5] << 16) +
879 ( s[uoffset + 6] << 8) +
888 /* currently converts input to bytes if possible but doesn't sweat failures,
889 * although it does ensure that the string it clobbers is not marked as
890 * utf8-valid any more
893 Perl_do_vecset(pTHX_ SV *sv)
895 STRLEN offset, bitoffs = 0;
902 SV * const targ = LvTARG(sv);
903 char errflags = LvFLAGS(sv);
905 PERL_ARGS_ASSERT_DO_VECSET;
907 /* some out-of-range errors have been deferred if/until the LV is
908 * actually written to: f(vec($s,-1,8)) is not always fatal */
910 assert(!(errflags & ~(LVf_NEG_OFF|LVf_OUT_OF_RANGE)));
911 if (errflags & LVf_NEG_OFF)
912 Perl_croak_nocontext("Negative offset to vec in lvalue context");
913 Perl_croak_nocontext("Out of memory!");
918 s = (unsigned char*)SvPV_force_flags(targ, targlen,
919 SV_GMAGIC | SV_UNDEF_RETURNS_NULL);
921 /* This is handled by the SvPOK_only below...
922 if (!Perl_sv_utf8_downgrade_flags(aTHX_ targ, TRUE, 0))
925 (void) Perl_sv_utf8_downgrade_flags(aTHX_ targ, TRUE, 0);
928 (void)SvPOK_only(targ);
930 offset = LvTARGOFF(sv);
931 size = LvTARGLEN(sv);
933 if (size < 1 || (size & (size-1))) /* size < 1 or not a power of two */
934 Perl_croak(aTHX_ "Illegal number of bits in vec");
937 bitoffs = ((offset%8)*size)%8;
942 if (offset > Size_t_MAX / n - 1) /* would overflow */
943 Perl_croak_nocontext("Out of memory!");
947 len = (bitoffs + size + 7)/8; /* required number of bytes */
948 if (targlen < offset || targlen - offset < len) {
949 STRLEN newlen = offset > Size_t_MAX - len - 1 ? /* avoid overflow */
950 Size_t_MAX : offset + len + 1;
951 s = (unsigned char*)SvGROW(targ, newlen);
952 (void)memzero((char *)(s + targlen), newlen - targlen);
953 SvCUR_set(targ, newlen - 1);
957 mask = (1 << size) - 1;
959 s[offset] &= ~(mask << bitoffs);
960 s[offset] |= lval << bitoffs;
964 s[offset ] = (U8)( lval & 0xff);
965 else if (size == 16) {
966 s[offset ] = (U8)((lval >> 8) & 0xff);
967 s[offset+1] = (U8)( lval & 0xff);
969 else if (size == 32) {
970 s[offset ] = (U8)((lval >> 24) & 0xff);
971 s[offset+1] = (U8)((lval >> 16) & 0xff);
972 s[offset+2] = (U8)((lval >> 8) & 0xff);
973 s[offset+3] = (U8)( lval & 0xff);
976 else if (size == 64) {
977 Perl_ck_warner(aTHX_ packWARN(WARN_PORTABLE),
978 "Bit vector size > 32 non-portable");
979 s[offset ] = (U8)((lval >> 56) & 0xff);
980 s[offset+1] = (U8)((lval >> 48) & 0xff);
981 s[offset+2] = (U8)((lval >> 40) & 0xff);
982 s[offset+3] = (U8)((lval >> 32) & 0xff);
983 s[offset+4] = (U8)((lval >> 24) & 0xff);
984 s[offset+5] = (U8)((lval >> 16) & 0xff);
985 s[offset+6] = (U8)((lval >> 8) & 0xff);
986 s[offset+7] = (U8)( lval & 0xff);
994 Perl_do_vop(pTHX_ I32 optype, SV *sv, SV *left, SV *right)
1009 bool result_needs_to_be_utf8 = FALSE;
1010 bool left_utf8 = FALSE;
1011 bool right_utf8 = FALSE;
1012 U8 * left_non_downgraded = NULL;
1013 U8 * right_non_downgraded = NULL;
1014 Size_t left_non_downgraded_len = 0;
1015 Size_t right_non_downgraded_len = 0;
1016 char * non_downgraded = NULL;
1017 Size_t non_downgraded_len = 0;
1019 PERL_ARGS_ASSERT_DO_VOP;
1021 if (sv != left || (optype != OP_BIT_AND && !SvOK(sv)))
1022 SvPVCLEAR(sv); /* avoid undef warning on |= and ^= */
1024 lc = SvPV_force_nomg(left, leftlen);
1027 lc = SvPV_nomg_const(left, leftlen);
1028 SvPV_force_nomg_nolen(sv);
1030 rc = SvPV_nomg_const(right, rightlen);
1032 /* This needs to come after SvPV to ensure that string overloading has
1035 /* Create downgraded temporaries of any UTF-8 encoded operands */
1036 if (DO_UTF8(left)) {
1037 const U8 * save_lc = (U8 *) lc;
1040 result_needs_to_be_utf8 = TRUE;
1042 left_non_downgraded_len = leftlen;
1043 lc = (char *) bytes_from_utf8_loc((const U8 *) lc, &leftlen,
1045 (const U8 **) &left_non_downgraded);
1046 /* Calculate the number of trailing unconvertible bytes. This quantity
1047 * is the original length minus the length of the converted portion. */
1048 left_non_downgraded_len -= left_non_downgraded - save_lc;
1051 if (DO_UTF8(right)) {
1052 const U8 * save_rc = (U8 *) rc;
1055 result_needs_to_be_utf8 = TRUE;
1057 right_non_downgraded_len = rightlen;
1058 rc = (char *) bytes_from_utf8_loc((const U8 *) rc, &rightlen,
1060 (const U8 **) &right_non_downgraded);
1061 right_non_downgraded_len -= right_non_downgraded - save_rc;
1065 /* We set 'len' to the length that the operation actually operates on. The
1066 * dangling part of the longer operand doesn't actually participate in the
1067 * operation. What happens is that we pretend that the shorter operand has
1068 * been extended to the right by enough imaginary zeros to match the length
1069 * of the longer one. But we know in advance the result of the operation
1070 * on zeros without having to do it. In the case of '&', the result is
1071 * zero, and the dangling portion is simply discarded. For '|' and '^', the
1072 * result is the same as the other operand, so the dangling part is just
1073 * appended to the final result, unchanged. As of perl-5.32, we no longer
1074 * accept above-FF code points in the dangling portion.
1076 if (left_utf8 || right_utf8) {
1077 Perl_croak(aTHX_ FATAL_ABOVE_FF_MSG, PL_op_desc[optype]);
1079 else { /* Neither is UTF-8 */
1080 len = MIN(leftlen, rightlen);
1088 (void)SvPOK_only(sv);
1089 if (SvOK(sv) || SvTYPE(sv) > SVt_PVMG) {
1090 dc = SvPV_force_nomg_nolen(sv);
1091 if (SvLEN(sv) < len + 1) {
1092 dc = SvGROW(sv, len + 1);
1093 (void)memzero(dc + SvCUR(sv), len - SvCUR(sv) + 1);
1097 needlen = optype == OP_BIT_AND
1098 ? len : (leftlen > rightlen ? leftlen : rightlen);
1099 Newxz(dc, needlen + 1, char);
1100 sv_usepvn_flags(sv, dc, needlen, SV_HAS_TRAILING_NUL);
1101 dc = SvPVX(sv); /* sv_usepvn() calls Renew() */
1104 if (len >= sizeof(long)*4 &&
1105 !(PTR2nat(dc) % sizeof(long)) &&
1106 !(PTR2nat(lc) % sizeof(long)) &&
1107 !(PTR2nat(rc) % sizeof(long))) /* It's almost always aligned... */
1109 const STRLEN remainder = len % (sizeof(long)*4);
1110 len /= (sizeof(long)*4);
1119 *dl++ = *ll++ & *rl++;
1120 *dl++ = *ll++ & *rl++;
1121 *dl++ = *ll++ & *rl++;
1122 *dl++ = *ll++ & *rl++;
1127 *dl++ = *ll++ ^ *rl++;
1128 *dl++ = *ll++ ^ *rl++;
1129 *dl++ = *ll++ ^ *rl++;
1130 *dl++ = *ll++ ^ *rl++;
1135 *dl++ = *ll++ | *rl++;
1136 *dl++ = *ll++ | *rl++;
1137 *dl++ = *ll++ | *rl++;
1138 *dl++ = *ll++ | *rl++;
1152 *dc++ = *lc++ & *rc++;
1157 *dc++ = *lc++ ^ *rc++;
1161 *dc++ = *lc++ | *rc++;
1164 if (rightlen > len) {
1166 SvCUR_set(sv, rightlen);
1168 sv_catpvn_nomg(sv, rsave + len, rightlen - len);
1170 else if (leftlen > len) {
1172 SvCUR_set(sv, leftlen);
1174 sv_catpvn_nomg(sv, lsave + len, leftlen - len);
1178 /* If there is trailing stuff that couldn't be converted from UTF-8, it
1179 * is appended as-is for the ^ and | operators. This preserves
1180 * backwards compatibility */
1181 if (right_non_downgraded) {
1182 non_downgraded = (char *) right_non_downgraded;
1183 non_downgraded_len = right_non_downgraded_len;
1185 else if (left_non_downgraded) {
1186 non_downgraded = (char *) left_non_downgraded;
1187 non_downgraded_len = left_non_downgraded_len;
1193 if (result_needs_to_be_utf8) {
1194 sv_utf8_upgrade_nomg(sv);
1196 /* Append any trailing UTF-8 as-is. */
1197 if (non_downgraded) {
1198 sv_catpvn_nomg(sv, non_downgraded, non_downgraded_len);
1206 /* Perl_do_kv() may be:
1207 * * called directly as the pp function for pp_keys() and pp_values();
1208 * * It may also be called directly when the op is OP_AVHVSWITCH, to
1209 * implement CORE::keys(), CORE::values().
1211 * In all cases it expects an HV on the stack and returns a list of keys,
1212 * values, or key-value pairs, depending on PL_op.
1219 HV * const keys = MUTABLE_HV(POPs);
1220 const U8 gimme = GIMME_V;
1222 const I32 dokeys = (PL_op->op_type == OP_KEYS)
1223 || ( PL_op->op_type == OP_AVHVSWITCH
1224 && (PL_op->op_private & OPpAVHVSWITCH_MASK)
1225 + OP_EACH == OP_KEYS);
1227 const I32 dovalues = (PL_op->op_type == OP_VALUES)
1228 || ( PL_op->op_type == OP_AVHVSWITCH
1229 && (PL_op->op_private & OPpAVHVSWITCH_MASK)
1230 + OP_EACH == OP_VALUES);
1232 assert( PL_op->op_type == OP_KEYS
1233 || PL_op->op_type == OP_VALUES
1234 || PL_op->op_type == OP_AVHVSWITCH);
1236 assert(!( PL_op->op_type == OP_VALUES
1237 && (PL_op->op_private & OPpMAYBE_LVSUB)));
1239 (void)hv_iterinit(keys); /* always reset iterator regardless */
1241 if (gimme == G_VOID)
1244 if (gimme == G_SCALAR) {
1245 if (PL_op->op_flags & OPf_MOD || LVRET) { /* lvalue */
1246 SV * const ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
1247 sv_magic(ret, NULL, PERL_MAGIC_nkeys, NULL, 0);
1249 LvTARG(ret) = SvREFCNT_inc_simple(keys);
1256 /* note that in 'scalar(keys %h)' the OP_KEYS is usually
1257 * optimised away and the action is performed directly by the
1258 * padhv or rv2hv op. We now only get here via OP_AVHVSWITCH
1261 if (! SvTIED_mg((const SV *)keys, PERL_MAGIC_tied) ) {
1262 i = HvUSEDKEYS(keys);
1266 while (hv_iternext(keys)) i++;
1273 if (UNLIKELY(PL_op->op_private & OPpMAYBE_LVSUB)) {
1274 const I32 flags = is_lvalue_sub();
1275 if (flags && !(flags & OPpENTERSUB_INARGS))
1276 /* diag_listed_as: Can't modify %s in %s */
1277 Perl_croak(aTHX_ "Can't modify keys in list assignment");
1281 hv_pushkv(keys, (dokeys | (dovalues << 1)));
1286 * ex: set ts=8 sts=4 sw=4 et: