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)
43 U8 *s = (U8*)SvPV_nomg(sv,len);
44 U8 * const send = s+len;
45 const OPtrans_map * const tbl = (OPtrans_map*)cPVOP->op_pv;
47 PERL_ARGS_ASSERT_DO_TRANS_SIMPLE;
50 Perl_croak(aTHX_ "panic: do_trans_simple line %d",__LINE__);
52 /* First, take care of non-UTF-8 input strings, because they're easy */
55 const short ch = tbl->map[*s];
65 const bool grows = cBOOL(PL_op->op_private & OPpTRANS_GROWS);
69 /* Allow for expansion: $_="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 non-utf8 cases(*) where search and replacement charlists are
109 * identical: so the string isn't modified, and only a count of modifiable
111 * Note that it doesn't handle /d or /s, since these modify the string
112 * even if the replacement list is empty.
113 * (*) i.e. where the search and replacement charlists are non-utf8. sv may
114 * or may not be utf8.
118 S_do_trans_count(pTHX_ SV * const sv)
121 const U8 *s = (const U8*)SvPV_nomg_const(sv, len);
122 const U8 * const send = s + len;
124 const OPtrans_map * const tbl = (OPtrans_map*)cPVOP->op_pv;
126 PERL_ARGS_ASSERT_DO_TRANS_COUNT;
129 Perl_croak(aTHX_ "panic: do_trans_count line %d",__LINE__);
133 if (tbl->map[*s++] >= 0)
138 const bool complement = cBOOL(PL_op->op_private & OPpTRANS_COMPLEMENT);
141 const UV c = utf8n_to_uvchr(s, send - s, &ulen, UTF8_ALLOW_DEFAULT);
143 if (tbl->map[c] >= 0)
145 } else if (complement)
155 /* Helper function for do_trans().
156 * Handles non-utf8 cases(*) involving the /c, /d, /s flags,
157 * and where search and replacement charlists aren't identical.
158 * (*) i.e. where the search and replacement charlists are non-utf8. sv may
159 * or may not be utf8.
163 S_do_trans_complex(pTHX_ SV * const sv)
166 U8 *s = (U8*)SvPV_nomg(sv, len);
167 U8 * const send = s+len;
169 const OPtrans_map * const tbl = (OPtrans_map*)cPVOP->op_pv;
171 PERL_ARGS_ASSERT_DO_TRANS_COMPLEX;
174 Perl_croak(aTHX_ "panic: do_trans_complex line %d",__LINE__);
178 U8 * const dstart = d;
180 if (PL_op->op_private & OPpTRANS_SQUASH) {
183 const short ch = tbl->map[*s];
187 if (p != d - 1 || *p != *d)
190 else if (ch == -1) /* -1 is unmapped character */
192 else if (ch == -2) /* -2 is delete character */
199 const short ch = tbl->map[*s];
204 else if (ch == -1) /* -1 is unmapped character */
206 else if (ch == -2) /* -2 is delete character */
212 SvCUR_set(sv, d - dstart);
215 const bool squash = cBOOL(PL_op->op_private & OPpTRANS_SQUASH);
216 const bool grows = cBOOL(PL_op->op_private & OPpTRANS_GROWS);
219 Size_t size = tbl->size;
223 Newx(d, len*2+1, U8);
230 const UV comp = utf8n_to_uvchr(s, send - s, &len,
235 sch = tbl->map[comp >= size ? size : comp];
241 if (LIKELY(!squash || ch != pch)) {
242 d = uvchr_to_utf8(d, ch);
248 else if (sch == -1) { /* -1 is unmapped character */
252 else if (sch == -2) /* -2 is delete character */
255 assert(sch == -3); /* -3 is empty replacement */
265 sv_setpvn(sv, (char*)dstart, d - dstart);
270 SvCUR_set(sv, d - dstart);
279 /* Helper function for do_trans().
280 * Handles utf8 cases(*) not involving the /c, /d, /s flags,
281 * and where search and replacement charlists aren't identical.
282 * (*) i.e. where the search or replacement charlists are utf8. sv may
283 * or may not be utf8.
287 S_do_trans_simple_utf8(pTHX_ SV * const sv)
295 const bool grows = cBOOL(PL_op->op_private & OPpTRANS_GROWS);
299 PAD_SVl(cPADOP->op_padix);
301 MUTABLE_SV(cSVOP->op_sv);
303 HV* const hv = MUTABLE_HV(SvRV(rv));
304 SV* const * svp = hv_fetchs(hv, "NONE", FALSE);
305 const UV none = svp ? SvUV(*svp) : 0x7fffffff;
306 const UV extra = none + 1;
310 PERL_ARGS_ASSERT_DO_TRANS_SIMPLE_UTF8;
312 s = (U8*)SvPV_nomg(sv, len);
314 hibit = ! is_utf8_invariant_string(s, len);
316 s = bytes_to_utf8(s, &len);
322 svp = hv_fetchs(hv, "FINAL", FALSE);
327 /* d needs to be bigger than s, in case e.g. upgrading is required */
328 Newx(d, len * 3 + UTF8_MAXBYTES, U8);
338 const UV uv = swash_fetch(rv, s, TRUE);
342 d = uvchr_to_utf8(d, uv);
344 else if (uv == none) {
345 const int i = UTF8SKIP(s);
350 else if (uv == extra) {
353 d = uvchr_to_utf8(d, final);
359 const STRLEN clen = d - dstart;
360 const STRLEN nlen = dend - dstart + len + UTF8_MAXBYTES;
362 Perl_croak(aTHX_ "panic: do_trans_simple_utf8 line %d",__LINE__);
363 Renew(dstart, nlen + UTF8_MAXBYTES, U8);
365 dend = dstart + nlen;
368 if (grows || hibit) {
369 sv_setpvn(sv, (char*)dstart, d - dstart);
376 SvCUR_set(sv, d - dstart);
385 /* Helper function for do_trans().
386 * Handles utf8 cases(*) where search and replacement charlists are
387 * identical: so the string isn't modified, and only a count of modifiable
389 * Note that it doesn't handle /d or /s, since these modify the string
390 * even if the replacement charlist is empty.
391 * (*) i.e. where the search or replacement charlists are utf8. sv may
392 * or may not be utf8.
396 S_do_trans_count_utf8(pTHX_ SV * const sv)
399 const U8 *start = NULL;
405 PAD_SVl(cPADOP->op_padix);
407 MUTABLE_SV(cSVOP->op_sv);
409 HV* const hv = MUTABLE_HV(SvRV(rv));
410 SV* const * const svp = hv_fetchs(hv, "NONE", FALSE);
411 const UV none = svp ? SvUV(*svp) : 0x7fffffff;
412 const UV extra = none + 1;
415 PERL_ARGS_ASSERT_DO_TRANS_COUNT_UTF8;
417 s = (const U8*)SvPV_nomg_const(sv, len);
419 hibit = ! is_utf8_invariant_string(s, len);
421 start = s = bytes_to_utf8(s, &len);
427 const UV uv = swash_fetch(rv, s, TRUE);
428 if (uv < none || uv == extra)
439 /* Helper function for do_trans().
440 * Handles utf8 cases(*) involving the /c, /d, /s flags,
441 * and where search and replacement charlists aren't identical.
442 * (*) i.e. where the search or replacement charlists are utf8. sv may
443 * or may not be utf8.
447 S_do_trans_complex_utf8(pTHX_ SV * const sv)
452 const bool squash = cBOOL(PL_op->op_private & OPpTRANS_SQUASH);
453 const bool del = cBOOL(PL_op->op_private & OPpTRANS_DELETE);
454 const bool grows = cBOOL(PL_op->op_private & OPpTRANS_GROWS);
457 PAD_SVl(cPADOP->op_padix);
459 MUTABLE_SV(cSVOP->op_sv);
461 HV * const hv = MUTABLE_HV(SvRV(rv));
462 SV * const *svp = hv_fetchs(hv, "NONE", FALSE);
463 const UV none = svp ? SvUV(*svp) : 0x7fffffff;
464 const UV extra = none + 1;
466 bool havefinal = FALSE;
470 U8 *s = (U8*)SvPV_nomg(sv, len);
472 PERL_ARGS_ASSERT_DO_TRANS_COMPLEX_UTF8;
475 hibit = ! is_utf8_invariant_string(s, len);
477 s = bytes_to_utf8(s, &len);
483 svp = hv_fetchs(hv, "FINAL", FALSE);
490 /* d needs to be bigger than s, in case e.g. upgrading is required */
491 Newx(d, len * 3 + UTF8_MAXBYTES, U8);
503 UV uv = swash_fetch(rv, s, TRUE);
506 const STRLEN clen = d - dstart;
507 const STRLEN nlen = dend - dstart + len + UTF8_MAXBYTES;
509 Perl_croak(aTHX_ "panic: do_trans_complex_utf8 line %d",__LINE__);
510 Renew(dstart, nlen + UTF8_MAXBYTES, U8);
512 dend = dstart + nlen;
518 d = uvchr_to_utf8(d, uv);
523 else if (uv == none) { /* "none" is unmapped character */
524 const int i = UTF8SKIP(s);
531 else if (uv == extra && !del) {
536 d = uvchr_to_utf8(d, final);
542 uv = utf8n_to_uvchr(s, send - s, &len, UTF8_ALLOW_DEFAULT);
552 matches++; /* "none+1" is delete character */
558 const UV uv = swash_fetch(rv, s, TRUE);
560 const STRLEN clen = d - dstart;
561 const STRLEN nlen = dend - dstart + len + UTF8_MAXBYTES;
563 Perl_croak(aTHX_ "panic: do_trans_complex_utf8 line %d",__LINE__);
564 Renew(dstart, nlen + UTF8_MAXBYTES, U8);
566 dend = dstart + nlen;
571 d = uvchr_to_utf8(d, uv);
574 else if (uv == none) { /* "none" is unmapped character */
575 const int i = UTF8SKIP(s);
581 else if (uv == extra && !del) {
584 d = uvchr_to_utf8(d, final);
587 matches++; /* "none+1" is delete character */
591 if (grows || hibit) {
592 sv_setpvn(sv, (char*)dstart, d - dstart);
599 SvCUR_set(sv, d - dstart);
608 /* Execute a tr//. sv is the value to be translated, while PL_op
609 * should be an OP_TRANS or OP_TRANSR op, whose op_pv field contains a
610 * translation table or whose op_sv field contains a swash.
611 * Returns a count of number of characters translated
615 Perl_do_trans(pTHX_ SV *sv)
618 const U8 flags = PL_op->op_private;
619 const U8 hasutf = flags & (OPpTRANS_FROM_UTF | OPpTRANS_TO_UTF);
621 PERL_ARGS_ASSERT_DO_TRANS;
623 if (SvREADONLY(sv) && !(flags & OPpTRANS_IDENTICAL)) {
624 Perl_croak_no_modify();
626 (void)SvPV_const(sv, len);
629 if (!(flags & OPpTRANS_IDENTICAL)) {
630 if (!SvPOKp(sv) || SvTHINKFIRST(sv))
631 (void)SvPV_force_nomg(sv, len);
632 (void)SvPOK_only_UTF8(sv);
635 /* If we use only OPpTRANS_IDENTICAL to bypass the READONLY check,
636 * we must also rely on it to choose the readonly strategy.
638 if (flags & OPpTRANS_IDENTICAL) {
639 return hasutf ? do_trans_count_utf8(sv) : do_trans_count(sv);
640 } else if (flags & (OPpTRANS_SQUASH|OPpTRANS_DELETE|OPpTRANS_COMPLEMENT)) {
641 return hasutf ? do_trans_complex_utf8(sv) : do_trans_complex(sv);
643 return hasutf ? do_trans_simple_utf8(sv) : do_trans_simple(sv);
648 Perl_do_join(pTHX_ SV *sv, SV *delim, SV **mark, SV **sp)
650 SV ** const oldmark = mark;
651 I32 items = sp - mark;
654 const char * const delims = SvPV_const(delim, delimlen);
656 PERL_ARGS_ASSERT_DO_JOIN;
659 len = (items > 0 ? (delimlen * (items - 1) ) : 0);
660 SvUPGRADE(sv, SVt_PV);
661 if (SvLEN(sv) < len + items) { /* current length is way too short */
662 while (items-- > 0) {
663 if (*mark && !SvGAMAGIC(*mark) && SvOK(*mark)) {
665 SvPV_const(*mark, tmplen);
670 SvGROW(sv, len + 1); /* so try to pre-extend */
678 /* sv_setpv retains old UTF8ness [perl #24846] */
681 if (TAINTING_get && SvMAGICAL(sv))
691 const U32 delimflag = DO_UTF8(delim) ? SV_CATUTF8 : SV_CATBYTES;
692 for (; items > 0; items--,mark++) {
695 sv_catpvn_flags(sv,delims,delimlen,delimflag);
696 s = SvPV_const(*mark,len);
697 sv_catpvn_flags(sv,s,len,
698 DO_UTF8(*mark) ? SV_CATUTF8 : SV_CATBYTES);
702 for (; items > 0; items--,mark++)
705 const char *s = SvPV_const(*mark,len);
706 sv_catpvn_flags(sv,s,len,
707 DO_UTF8(*mark) ? SV_CATUTF8 : SV_CATBYTES);
714 Perl_do_sprintf(pTHX_ SV *sv, SSize_t len, SV **sarg)
717 const char * const pat = SvPV_const(*sarg, patlen);
718 bool do_taint = FALSE;
720 PERL_ARGS_ASSERT_DO_SPRINTF;
723 if (SvTAINTED(*sarg))
725 (PL_op && PL_op->op_type < OP_max)
726 ? (PL_op->op_type == OP_PRTF)
728 : PL_op_name[PL_op->op_type]
734 sv_vsetpvfn(sv, pat, patlen, NULL, sarg + 1, (Size_t)(len - 1), &do_taint);
740 /* currently converts input to bytes if possible, but doesn't sweat failure */
742 Perl_do_vecget(pTHX_ SV *sv, STRLEN offset, int size)
744 STRLEN srclen, len, avail, uoffset, bitoffs = 0;
745 const I32 svpv_flags = ((PL_op->op_flags & OPf_MOD || LVRET)
746 ? SV_UNDEF_RETURNS_NULL : 0);
747 unsigned char *s = (unsigned char *)
748 SvPV_flags(sv, srclen, (svpv_flags|SV_GMAGIC));
752 s = (unsigned char *)"";
755 PERL_ARGS_ASSERT_DO_VECGET;
757 if (size < 1 || (size & (size-1))) /* size < 1 or not a power of two */
758 Perl_croak(aTHX_ "Illegal number of bits in vec");
761 if (Perl_sv_utf8_downgrade(aTHX_ sv, TRUE)) {
762 /* PVX may have changed */
763 s = (unsigned char *) SvPV_flags(sv, srclen, svpv_flags);
766 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
767 "Use of strings with code points over 0xFF as"
768 " arguments to vec is deprecated. This will"
769 " be a fatal error in Perl 5.32");
774 bitoffs = ((offset%8)*size)%8;
775 uoffset = offset/(8/size);
779 if (offset > Size_t_MAX / n - 1) /* would overflow */
786 if (uoffset >= srclen)
789 len = (bitoffs + size + 7)/8; /* required number of bytes */
790 avail = srclen - uoffset; /* available number of bytes */
792 /* Does the byte range overlap the end of the string? If so,
793 * handle specially. */
800 retnum = (UV) s[uoffset] << 8;
802 else if (size == 32) {
803 assert(avail >= 1 && avail <= 3);
806 ((UV) s[uoffset ] << 24);
809 ((UV) s[uoffset ] << 24) +
810 ((UV) s[uoffset + 1] << 16);
813 ((UV) s[uoffset ] << 24) +
814 ((UV) s[uoffset + 1] << 16) +
815 ( s[uoffset + 2] << 8);
818 else if (size == 64) {
819 Perl_ck_warner(aTHX_ packWARN(WARN_PORTABLE),
820 "Bit vector size > 32 non-portable");
821 assert(avail >= 1 && avail <= 7);
824 (UV) s[uoffset ] << 56;
827 ((UV) s[uoffset ] << 56) +
828 ((UV) s[uoffset + 1] << 48);
831 ((UV) s[uoffset ] << 56) +
832 ((UV) s[uoffset + 1] << 48) +
833 ((UV) s[uoffset + 2] << 40);
836 ((UV) s[uoffset ] << 56) +
837 ((UV) s[uoffset + 1] << 48) +
838 ((UV) s[uoffset + 2] << 40) +
839 ((UV) s[uoffset + 3] << 32);
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);
849 ((UV) s[uoffset ] << 56) +
850 ((UV) s[uoffset + 1] << 48) +
851 ((UV) s[uoffset + 2] << 40) +
852 ((UV) s[uoffset + 3] << 32) +
853 ((UV) s[uoffset + 4] << 24) +
854 ((UV) s[uoffset + 5] << 16);
857 ((UV) s[uoffset ] << 56) +
858 ((UV) s[uoffset + 1] << 48) +
859 ((UV) s[uoffset + 2] << 40) +
860 ((UV) s[uoffset + 3] << 32) +
861 ((UV) s[uoffset + 4] << 24) +
862 ((UV) s[uoffset + 5] << 16) +
863 ((UV) s[uoffset + 6] << 8);
869 retnum = (s[uoffset] >> bitoffs) & ((1 << size) - 1);
875 ((UV) s[uoffset] << 8) +
879 ((UV) s[uoffset ] << 24) +
880 ((UV) s[uoffset + 1] << 16) +
881 ( s[uoffset + 2] << 8) +
884 else if (size == 64) {
885 Perl_ck_warner(aTHX_ packWARN(WARN_PORTABLE),
886 "Bit vector size > 32 non-portable");
888 ((UV) s[uoffset ] << 56) +
889 ((UV) s[uoffset + 1] << 48) +
890 ((UV) s[uoffset + 2] << 40) +
891 ((UV) s[uoffset + 3] << 32) +
892 ((UV) s[uoffset + 4] << 24) +
893 ((UV) s[uoffset + 5] << 16) +
894 ( s[uoffset + 6] << 8) +
903 /* currently converts input to bytes if possible but doesn't sweat failures,
904 * although it does ensure that the string it clobbers is not marked as
905 * utf8-valid any more
908 Perl_do_vecset(pTHX_ SV *sv)
910 STRLEN offset, bitoffs = 0;
917 SV * const targ = LvTARG(sv);
918 char errflags = LvFLAGS(sv);
920 PERL_ARGS_ASSERT_DO_VECSET;
922 /* some out-of-range errors have been deferred if/until the LV is
923 * actually written to: f(vec($s,-1,8)) is not always fatal */
925 assert(!(errflags & ~(LVf_NEG_OFF|LVf_OUT_OF_RANGE)));
926 if (errflags & LVf_NEG_OFF)
927 Perl_croak_nocontext("Negative offset to vec in lvalue context");
928 Perl_croak_nocontext("Out of memory!");
933 s = (unsigned char*)SvPV_force_flags(targ, targlen,
934 SV_GMAGIC | SV_UNDEF_RETURNS_NULL);
936 /* This is handled by the SvPOK_only below...
937 if (!Perl_sv_utf8_downgrade(aTHX_ targ, TRUE))
940 (void) Perl_sv_utf8_downgrade(aTHX_ targ, TRUE);
943 (void)SvPOK_only(targ);
945 offset = LvTARGOFF(sv);
946 size = LvTARGLEN(sv);
948 if (size < 1 || (size & (size-1))) /* size < 1 or not a power of two */
949 Perl_croak(aTHX_ "Illegal number of bits in vec");
952 bitoffs = ((offset%8)*size)%8;
957 if (offset > Size_t_MAX / n - 1) /* would overflow */
958 Perl_croak_nocontext("Out of memory!");
962 len = (bitoffs + size + 7)/8; /* required number of bytes */
963 if (targlen < offset || targlen - offset < len) {
964 STRLEN newlen = offset > Size_t_MAX - len - 1 ? /* avoid overflow */
965 Size_t_MAX : offset + len + 1;
966 s = (unsigned char*)SvGROW(targ, newlen);
967 (void)memzero((char *)(s + targlen), newlen - targlen);
968 SvCUR_set(targ, newlen - 1);
972 mask = (1 << size) - 1;
974 s[offset] &= ~(mask << bitoffs);
975 s[offset] |= lval << bitoffs;
979 s[offset ] = (U8)( lval & 0xff);
980 else if (size == 16) {
981 s[offset ] = (U8)((lval >> 8) & 0xff);
982 s[offset+1] = (U8)( lval & 0xff);
984 else if (size == 32) {
985 s[offset ] = (U8)((lval >> 24) & 0xff);
986 s[offset+1] = (U8)((lval >> 16) & 0xff);
987 s[offset+2] = (U8)((lval >> 8) & 0xff);
988 s[offset+3] = (U8)( lval & 0xff);
991 else if (size == 64) {
992 Perl_ck_warner(aTHX_ packWARN(WARN_PORTABLE),
993 "Bit vector size > 32 non-portable");
994 s[offset ] = (U8)((lval >> 56) & 0xff);
995 s[offset+1] = (U8)((lval >> 48) & 0xff);
996 s[offset+2] = (U8)((lval >> 40) & 0xff);
997 s[offset+3] = (U8)((lval >> 32) & 0xff);
998 s[offset+4] = (U8)((lval >> 24) & 0xff);
999 s[offset+5] = (U8)((lval >> 16) & 0xff);
1000 s[offset+6] = (U8)((lval >> 8) & 0xff);
1001 s[offset+7] = (U8)( lval & 0xff);
1009 Perl_do_vop(pTHX_ I32 optype, SV *sv, SV *left, SV *right)
1026 bool result_needs_to_be_utf8 = FALSE;
1027 bool left_utf8 = FALSE;
1028 bool right_utf8 = FALSE;
1029 U8 * left_non_downgraded = NULL;
1030 U8 * right_non_downgraded = NULL;
1031 Size_t left_non_downgraded_len = 0;
1032 Size_t right_non_downgraded_len = 0;
1033 char * non_downgraded = NULL;
1034 Size_t non_downgraded_len = 0;
1036 PERL_ARGS_ASSERT_DO_VOP;
1038 if (sv != left || (optype != OP_BIT_AND && !SvOK(sv)))
1039 SvPVCLEAR(sv); /* avoid undef warning on |= and ^= */
1041 lc = SvPV_force_nomg(left, leftlen);
1044 lc = SvPV_nomg_const(left, leftlen);
1045 SvPV_force_nomg_nolen(sv);
1047 rc = SvPV_nomg_const(right, rightlen);
1049 /* This needs to come after SvPV to ensure that string overloading has
1052 /* Create downgraded temporaries of any UTF-8 encoded operands */
1053 if (DO_UTF8(left)) {
1054 const U8 * save_lc = (U8 *) lc;
1057 result_needs_to_be_utf8 = TRUE;
1059 left_non_downgraded_len = leftlen;
1060 lc = (char *) bytes_from_utf8_loc((const U8 *) lc, &leftlen,
1062 (const U8 **) &left_non_downgraded);
1063 /* Calculate the number of trailing unconvertible bytes. This quantity
1064 * is the original length minus the length of the converted portion. */
1065 left_non_downgraded_len -= left_non_downgraded - save_lc;
1068 if (DO_UTF8(right)) {
1069 const U8 * save_rc = (U8 *) rc;
1072 result_needs_to_be_utf8 = TRUE;
1074 right_non_downgraded_len = rightlen;
1075 rc = (char *) bytes_from_utf8_loc((const U8 *) rc, &rightlen,
1077 (const U8 **) &right_non_downgraded);
1078 right_non_downgraded_len -= right_non_downgraded - save_rc;
1082 /* We set 'len' to the length that the operation actually operates on. The
1083 * dangling part of the longer operand doesn't actually participate in the
1084 * operation. What happens is that we pretend that the shorter operand has
1085 * been extended to the right by enough imaginary zeros to match the length
1086 * of the longer one. But we know in advance the result of the operation
1087 * on zeros without having to do it. In the case of '&', the result is
1088 * zero, and the dangling portion is simply discarded. For '|' and '^', the
1089 * result is the same as the other operand, so the dangling part is just
1090 * appended to the final result, unchanged. We currently accept above-FF
1091 * code points in the dangling portion, as that's how it has long worked,
1092 * and code depends on it staying that way. But it is now fatal for
1093 * above-FF to appear in the portion that does get operated on. Hence, any
1094 * above-FF must come only in the longer operand, and only in its dangling
1095 * portion. That means that at least one of the operands has to be
1096 * entirely non-UTF-8, and the length of that operand has to be before the
1097 * first above-FF in the other */
1098 if (left_utf8 || right_utf8) {
1100 if (right_utf8 || rightlen > leftlen) {
1101 Perl_croak(aTHX_ fatal_above_ff_msg, PL_op_desc[optype]);
1105 else if (right_utf8) {
1106 if (leftlen > rightlen) {
1107 Perl_croak(aTHX_ fatal_above_ff_msg, PL_op_desc[optype]);
1112 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
1113 deprecated_above_ff_msg, PL_op_desc[optype]);
1115 else { /* Neither is UTF-8 */
1116 len = MIN(leftlen, rightlen);
1124 (void)SvPOK_only(sv);
1125 if (SvOK(sv) || SvTYPE(sv) > SVt_PVMG) {
1126 dc = SvPV_force_nomg_nolen(sv);
1127 if (SvLEN(sv) < len + 1) {
1128 dc = SvGROW(sv, len + 1);
1129 (void)memzero(dc + SvCUR(sv), len - SvCUR(sv) + 1);
1133 needlen = optype == OP_BIT_AND
1134 ? len : (leftlen > rightlen ? leftlen : rightlen);
1135 Newxz(dc, needlen + 1, char);
1136 sv_usepvn_flags(sv, dc, needlen, SV_HAS_TRAILING_NUL);
1137 dc = SvPVX(sv); /* sv_usepvn() calls Renew() */
1141 if (len >= sizeof(long)*4 &&
1142 !((unsigned long)dc % sizeof(long)) &&
1143 !((unsigned long)lc % sizeof(long)) &&
1144 !((unsigned long)rc % sizeof(long))) /* It's almost always aligned... */
1146 const STRLEN remainder = len % (sizeof(long)*4);
1147 len /= (sizeof(long)*4);
1156 *dl++ = *ll++ & *rl++;
1157 *dl++ = *ll++ & *rl++;
1158 *dl++ = *ll++ & *rl++;
1159 *dl++ = *ll++ & *rl++;
1164 *dl++ = *ll++ ^ *rl++;
1165 *dl++ = *ll++ ^ *rl++;
1166 *dl++ = *ll++ ^ *rl++;
1167 *dl++ = *ll++ ^ *rl++;
1172 *dl++ = *ll++ | *rl++;
1173 *dl++ = *ll++ | *rl++;
1174 *dl++ = *ll++ | *rl++;
1175 *dl++ = *ll++ | *rl++;
1189 *dc++ = *lc++ & *rc++;
1194 *dc++ = *lc++ ^ *rc++;
1198 *dc++ = *lc++ | *rc++;
1201 if (rightlen > len) {
1203 SvCUR(sv) = rightlen;
1205 sv_catpvn_nomg(sv, rsave + len, rightlen - len);
1207 else if (leftlen > len) {
1209 SvCUR(sv) = leftlen;
1211 sv_catpvn_nomg(sv, lsave + len, leftlen - len);
1215 /* If there is trailing stuff that couldn't be converted from UTF-8, it
1216 * is appended as-is for the ^ and | operators. This preserves
1217 * backwards compatibility */
1218 if (right_non_downgraded) {
1219 non_downgraded = (char *) right_non_downgraded;
1220 non_downgraded_len = right_non_downgraded_len;
1222 else if (left_non_downgraded) {
1223 non_downgraded = (char *) left_non_downgraded;
1224 non_downgraded_len = left_non_downgraded_len;
1230 if (result_needs_to_be_utf8) {
1231 sv_utf8_upgrade_nomg(sv);
1233 /* Append any trailing UTF-8 as-is. */
1234 if (non_downgraded) {
1235 sv_catpvn_nomg(sv, non_downgraded, non_downgraded_len);
1243 /* Perl_do_kv() may be:
1244 * * called directly as the pp function for pp_keys() and pp_values();
1245 * * It may also be called directly when the op is OP_AVHVSWITCH, to
1246 * implement CORE::keys(), CORE::values().
1248 * In all cases it expects an HV on the stack and returns a list of keys,
1249 * values, or key-value pairs, depending on PL_op.
1256 HV * const keys = MUTABLE_HV(POPs);
1257 const U8 gimme = GIMME_V;
1259 const I32 dokeys = (PL_op->op_type == OP_KEYS)
1260 || ( PL_op->op_type == OP_AVHVSWITCH
1261 && (PL_op->op_private & OPpAVHVSWITCH_MASK)
1262 + OP_EACH == OP_KEYS);
1264 const I32 dovalues = (PL_op->op_type == OP_VALUES)
1265 || ( PL_op->op_type == OP_AVHVSWITCH
1266 && (PL_op->op_private & OPpAVHVSWITCH_MASK)
1267 + OP_EACH == OP_VALUES);
1269 assert( PL_op->op_type == OP_KEYS
1270 || PL_op->op_type == OP_VALUES
1271 || PL_op->op_type == OP_AVHVSWITCH);
1273 assert(!( PL_op->op_type == OP_VALUES
1274 && (PL_op->op_private & OPpMAYBE_LVSUB)));
1276 (void)hv_iterinit(keys); /* always reset iterator regardless */
1278 if (gimme == G_VOID)
1281 if (gimme == G_SCALAR) {
1282 if (PL_op->op_flags & OPf_MOD || LVRET) { /* lvalue */
1283 SV * const ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
1284 sv_magic(ret, NULL, PERL_MAGIC_nkeys, NULL, 0);
1286 LvTARG(ret) = SvREFCNT_inc_simple(keys);
1293 /* note that in 'scalar(keys %h)' the OP_KEYS is usually
1294 * optimised away and the action is performed directly by the
1295 * padhv or rv2hv op. We now only get here via OP_AVHVSWITCH
1298 if (! SvTIED_mg((const SV *)keys, PERL_MAGIC_tied) ) {
1299 i = HvUSEDKEYS(keys);
1303 while (hv_iternext(keys)) i++;
1310 if (UNLIKELY(PL_op->op_private & OPpMAYBE_LVSUB)) {
1311 const I32 flags = is_lvalue_sub();
1312 if (flags && !(flags & OPpENTERSUB_INARGS))
1313 /* diag_listed_as: Can't modify %s in %s */
1314 Perl_croak(aTHX_ "Can't modify keys in list assignment");
1318 hv_pushkv(keys, (dokeys | (dovalues << 1)));
1323 * ex: set ts=8 sts=4 sw=4 et: