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;
169 PERL_ARGS_ASSERT_DO_TRANS_COMPLEX;
173 U8 * const dstart = d;
175 if (PL_op->op_private & OPpTRANS_SQUASH) {
178 const short ch = tbl->map[*s];
182 if (p != d - 1 || *p != *d)
185 else if (ch == (short) TR_UNMAPPED)
187 else if (ch == (short) TR_DELETE)
192 else { /* Not to squash */
194 const short ch = tbl->map[*s];
199 else if (ch == (short) TR_UNMAPPED)
201 else if (ch == (short) TR_DELETE)
207 SvCUR_set(sv, d - dstart);
210 const bool squash = cBOOL(PL_op->op_private & OPpTRANS_SQUASH);
211 const bool grows = cBOOL(PL_op->op_private & OPpTRANS_GROWS);
214 Size_t size = tbl->size;
216 /* What the mapping of the previous character was to. If the new
217 * character has the same mapping, it is squashed from the output (but
218 * still is included in the count) */
222 /* Allow for worst-case expansion: Each input byte can become 2.
223 * For a given input character, this happens when it occupies a
224 * single byte under UTF-8, but is to be translated to something
225 * that occupies two: */
226 Newx(d, len*2+1, U8);
233 const UV comp = utf8n_to_uvchr(s, send - s, &len,
238 sch = tbl->map[comp >= size ? size : comp];
244 if (LIKELY(!squash || ch != pch)) {
245 d = uvchr_to_utf8(d, ch);
251 else if (sch == (short) TR_UNMAPPED) {
255 else if (sch == (short) TR_DELETE)
258 assert(sch == (short) TR_R_EMPTY); /* empty replacement */
268 sv_setpvn(sv, (char*)dstart, d - dstart);
273 SvCUR_set(sv, d - dstart);
282 /* Helper function for do_trans().
283 * Handles utf8 cases(*) not involving the /c, /d, /s flags,
284 * and where search and replacement charlists aren't identical.
285 * (*) i.e. where the search or replacement charlists are utf8. sv may
286 * or may not be utf8.
290 S_do_trans_simple_utf8(pTHX_ SV * const sv)
298 const bool grows = cBOOL(PL_op->op_private & OPpTRANS_GROWS);
302 PAD_SVl(cPADOP->op_padix);
304 MUTABLE_SV(cSVOP->op_sv);
306 HV* const hv = MUTABLE_HV(SvRV(rv));
307 SV* const * svp = hv_fetchs(hv, "NONE", FALSE);
308 const UV none = svp ? SvUV(*svp) : 0x7fffffff;
309 const UV extra = none + 1;
313 PERL_ARGS_ASSERT_DO_TRANS_SIMPLE_UTF8;
315 s = (U8*)SvPV_nomg(sv, len);
317 hibit = ! is_utf8_invariant_string(s, len);
319 s = bytes_to_utf8(s, &len);
325 svp = hv_fetchs(hv, "FINAL", FALSE);
330 /* d needs to be bigger than s, in case e.g. upgrading is required */
331 Newx(d, len * 3 + UTF8_MAXBYTES, U8);
341 const UV uv = swash_fetch(rv, s, TRUE);
345 d = uvchr_to_utf8(d, uv);
347 else if (uv == none) {
348 const int i = UTF8SKIP(s);
353 else if (uv == extra) {
356 d = uvchr_to_utf8(d, final);
362 const STRLEN clen = d - dstart;
363 const STRLEN nlen = dend - dstart + len + UTF8_MAXBYTES;
365 Perl_croak(aTHX_ "panic: do_trans_simple_utf8 line %d",__LINE__);
366 Renew(dstart, nlen + UTF8_MAXBYTES, U8);
368 dend = dstart + nlen;
371 if (grows || hibit) {
372 sv_setpvn(sv, (char*)dstart, d - dstart);
379 SvCUR_set(sv, d - dstart);
388 /* Helper function for do_trans().
389 * Handles utf8 cases(*) where search and replacement charlists are
390 * identical: so the string isn't modified, and only a count of modifiable
392 * Note that it doesn't handle /d or /s, since these modify the string
393 * even if the replacement charlist is empty.
394 * (*) i.e. where the search or replacement charlists are utf8. sv may
395 * or may not be utf8.
399 S_do_trans_count_utf8(pTHX_ SV * const sv)
402 const U8 *start = NULL;
408 PAD_SVl(cPADOP->op_padix);
410 MUTABLE_SV(cSVOP->op_sv);
412 HV* const hv = MUTABLE_HV(SvRV(rv));
413 SV* const * const svp = hv_fetchs(hv, "NONE", FALSE);
414 const UV none = svp ? SvUV(*svp) : 0x7fffffff;
415 const UV extra = none + 1;
418 PERL_ARGS_ASSERT_DO_TRANS_COUNT_UTF8;
420 s = (const U8*)SvPV_nomg_const(sv, len);
422 hibit = ! is_utf8_invariant_string(s, len);
424 start = s = bytes_to_utf8(s, &len);
430 const UV uv = swash_fetch(rv, s, TRUE);
431 if (uv < none || uv == extra)
442 /* Helper function for do_trans().
443 * Handles utf8 cases(*) involving the /c, /d, /s flags,
444 * and where search and replacement charlists aren't identical.
445 * (*) i.e. where the search or replacement charlists are utf8. sv may
446 * or may not be utf8.
450 S_do_trans_complex_utf8(pTHX_ SV * const sv)
455 const bool squash = cBOOL(PL_op->op_private & OPpTRANS_SQUASH);
456 const bool del = cBOOL(PL_op->op_private & OPpTRANS_DELETE);
457 const bool grows = cBOOL(PL_op->op_private & OPpTRANS_GROWS);
460 PAD_SVl(cPADOP->op_padix);
462 MUTABLE_SV(cSVOP->op_sv);
464 HV * const hv = MUTABLE_HV(SvRV(rv));
465 SV * const *svp = hv_fetchs(hv, "NONE", FALSE);
466 const UV none = svp ? SvUV(*svp) : 0x7fffffff;
467 const UV extra = none + 1;
469 bool havefinal = FALSE;
473 U8 *s = (U8*)SvPV_nomg(sv, len);
475 PERL_ARGS_ASSERT_DO_TRANS_COMPLEX_UTF8;
478 hibit = ! is_utf8_invariant_string(s, len);
480 s = bytes_to_utf8(s, &len);
486 svp = hv_fetchs(hv, "FINAL", FALSE);
493 /* d needs to be bigger than s, in case e.g. upgrading is required */
494 Newx(d, len * 3 + UTF8_MAXBYTES, U8);
506 UV uv = swash_fetch(rv, s, TRUE);
509 const STRLEN clen = d - dstart;
510 const STRLEN nlen = dend - dstart + len + UTF8_MAXBYTES;
512 Perl_croak(aTHX_ "panic: do_trans_complex_utf8 line %d",__LINE__);
513 Renew(dstart, nlen + UTF8_MAXBYTES, U8);
515 dend = dstart + nlen;
521 d = uvchr_to_utf8(d, uv);
526 else if (uv == none) { /* "none" is unmapped character */
527 const int i = UTF8SKIP(s);
534 else if (uv == extra && !del) {
539 d = uvchr_to_utf8(d, final);
545 uv = utf8n_to_uvchr(s, send - s, &len, UTF8_ALLOW_DEFAULT);
555 matches++; /* "none+1" is delete character */
561 const UV uv = swash_fetch(rv, s, TRUE);
563 const STRLEN clen = d - dstart;
564 const STRLEN nlen = dend - dstart + len + UTF8_MAXBYTES;
566 Perl_croak(aTHX_ "panic: do_trans_complex_utf8 line %d",__LINE__);
567 Renew(dstart, nlen + UTF8_MAXBYTES, U8);
569 dend = dstart + nlen;
574 d = uvchr_to_utf8(d, uv);
577 else if (uv == none) { /* "none" is unmapped character */
578 const int i = UTF8SKIP(s);
584 else if (uv == extra && !del) {
587 d = uvchr_to_utf8(d, final);
590 matches++; /* "none+1" is delete character */
594 if (grows || hibit) {
595 sv_setpvn(sv, (char*)dstart, d - dstart);
602 SvCUR_set(sv, d - dstart);
611 /* Execute a tr//. sv is the value to be translated, while PL_op
612 * should be an OP_TRANS or OP_TRANSR op, whose op_pv field contains a
613 * translation table or whose op_sv field contains a swash.
614 * Returns a count of number of characters translated
618 Perl_do_trans(pTHX_ SV *sv)
621 const U8 flags = PL_op->op_private;
622 const U8 hasutf = flags & (OPpTRANS_FROM_UTF | OPpTRANS_TO_UTF);
624 PERL_ARGS_ASSERT_DO_TRANS;
626 if (SvREADONLY(sv) && !(flags & OPpTRANS_IDENTICAL)) {
627 Perl_croak_no_modify();
629 (void)SvPV_const(sv, len);
632 if (!(flags & OPpTRANS_IDENTICAL)) {
633 if (!SvPOKp(sv) || SvTHINKFIRST(sv))
634 (void)SvPV_force_nomg(sv, len);
635 (void)SvPOK_only_UTF8(sv);
638 /* If we use only OPpTRANS_IDENTICAL to bypass the READONLY check,
639 * we must also rely on it to choose the readonly strategy.
641 if (flags & OPpTRANS_IDENTICAL) {
642 return hasutf ? do_trans_count_utf8(sv) : do_trans_count(sv, (OPtrans_map*)cPVOP->op_pv);
643 } else if (flags & (OPpTRANS_SQUASH|OPpTRANS_DELETE|OPpTRANS_COMPLEMENT)) {
644 return hasutf ? do_trans_complex_utf8(sv) : do_trans_complex(sv, (OPtrans_map*)cPVOP->op_pv);
646 return hasutf ? do_trans_simple_utf8(sv) : do_trans_simple(sv, (OPtrans_map*)cPVOP->op_pv);
651 Perl_do_join(pTHX_ SV *sv, SV *delim, SV **mark, SV **sp)
653 SV ** const oldmark = mark;
654 I32 items = sp - mark;
657 const char * const delims = SvPV_const(delim, delimlen);
659 PERL_ARGS_ASSERT_DO_JOIN;
662 len = (items > 0 ? (delimlen * (items - 1) ) : 0);
663 SvUPGRADE(sv, SVt_PV);
664 if (SvLEN(sv) < len + items) { /* current length is way too short */
665 while (items-- > 0) {
666 if (*mark && !SvGAMAGIC(*mark) && SvOK(*mark)) {
668 SvPV_const(*mark, tmplen);
673 SvGROW(sv, len + 1); /* so try to pre-extend */
681 /* sv_setpv retains old UTF8ness [perl #24846] */
684 if (TAINTING_get && SvMAGICAL(sv))
694 const U32 delimflag = DO_UTF8(delim) ? SV_CATUTF8 : SV_CATBYTES;
695 for (; items > 0; items--,mark++) {
698 sv_catpvn_flags(sv,delims,delimlen,delimflag);
699 s = SvPV_const(*mark,len);
700 sv_catpvn_flags(sv,s,len,
701 DO_UTF8(*mark) ? SV_CATUTF8 : SV_CATBYTES);
705 for (; items > 0; items--,mark++)
708 const char *s = SvPV_const(*mark,len);
709 sv_catpvn_flags(sv,s,len,
710 DO_UTF8(*mark) ? SV_CATUTF8 : SV_CATBYTES);
717 Perl_do_sprintf(pTHX_ SV *sv, SSize_t len, SV **sarg)
720 const char * const pat = SvPV_const(*sarg, patlen);
721 bool do_taint = FALSE;
723 PERL_ARGS_ASSERT_DO_SPRINTF;
726 if (SvTAINTED(*sarg))
728 (PL_op && PL_op->op_type < OP_max)
729 ? (PL_op->op_type == OP_PRTF)
731 : PL_op_name[PL_op->op_type]
737 sv_vsetpvfn(sv, pat, patlen, NULL, sarg + 1, (Size_t)(len - 1), &do_taint);
743 /* currently converts input to bytes if possible, but doesn't sweat failure */
745 Perl_do_vecget(pTHX_ SV *sv, STRLEN offset, int size)
747 STRLEN srclen, len, avail, uoffset, bitoffs = 0;
748 const I32 svpv_flags = ((PL_op->op_flags & OPf_MOD || LVRET)
749 ? SV_UNDEF_RETURNS_NULL : 0);
750 unsigned char *s = (unsigned char *)
751 SvPV_flags(sv, srclen, (svpv_flags|SV_GMAGIC));
755 s = (unsigned char *)"";
758 PERL_ARGS_ASSERT_DO_VECGET;
760 if (size < 1 || (size & (size-1))) /* size < 1 or not a power of two */
761 Perl_croak(aTHX_ "Illegal number of bits in vec");
764 if (Perl_sv_utf8_downgrade_flags(aTHX_ sv, TRUE, 0)) {
765 /* PVX may have changed */
766 s = (unsigned char *) SvPV_flags(sv, srclen, svpv_flags);
769 Perl_croak(aTHX_ "Use of strings with code points over 0xFF as arguments to vec is forbidden");
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_flags(aTHX_ targ, TRUE, 0))
940 (void) Perl_sv_utf8_downgrade_flags(aTHX_ targ, TRUE, 0);
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)
1024 bool result_needs_to_be_utf8 = FALSE;
1025 bool left_utf8 = FALSE;
1026 bool right_utf8 = FALSE;
1027 U8 * left_non_downgraded = NULL;
1028 U8 * right_non_downgraded = NULL;
1029 Size_t left_non_downgraded_len = 0;
1030 Size_t right_non_downgraded_len = 0;
1031 char * non_downgraded = NULL;
1032 Size_t non_downgraded_len = 0;
1034 PERL_ARGS_ASSERT_DO_VOP;
1036 if (sv != left || (optype != OP_BIT_AND && !SvOK(sv)))
1037 SvPVCLEAR(sv); /* avoid undef warning on |= and ^= */
1039 lc = SvPV_force_nomg(left, leftlen);
1042 lc = SvPV_nomg_const(left, leftlen);
1043 SvPV_force_nomg_nolen(sv);
1045 rc = SvPV_nomg_const(right, rightlen);
1047 /* This needs to come after SvPV to ensure that string overloading has
1050 /* Create downgraded temporaries of any UTF-8 encoded operands */
1051 if (DO_UTF8(left)) {
1052 const U8 * save_lc = (U8 *) lc;
1055 result_needs_to_be_utf8 = TRUE;
1057 left_non_downgraded_len = leftlen;
1058 lc = (char *) bytes_from_utf8_loc((const U8 *) lc, &leftlen,
1060 (const U8 **) &left_non_downgraded);
1061 /* Calculate the number of trailing unconvertible bytes. This quantity
1062 * is the original length minus the length of the converted portion. */
1063 left_non_downgraded_len -= left_non_downgraded - save_lc;
1066 if (DO_UTF8(right)) {
1067 const U8 * save_rc = (U8 *) rc;
1070 result_needs_to_be_utf8 = TRUE;
1072 right_non_downgraded_len = rightlen;
1073 rc = (char *) bytes_from_utf8_loc((const U8 *) rc, &rightlen,
1075 (const U8 **) &right_non_downgraded);
1076 right_non_downgraded_len -= right_non_downgraded - save_rc;
1080 /* We set 'len' to the length that the operation actually operates on. The
1081 * dangling part of the longer operand doesn't actually participate in the
1082 * operation. What happens is that we pretend that the shorter operand has
1083 * been extended to the right by enough imaginary zeros to match the length
1084 * of the longer one. But we know in advance the result of the operation
1085 * on zeros without having to do it. In the case of '&', the result is
1086 * zero, and the dangling portion is simply discarded. For '|' and '^', the
1087 * result is the same as the other operand, so the dangling part is just
1088 * appended to the final result, unchanged. As of perl-5.32, we no longer
1089 * accept above-FF code points in the dangling portion.
1091 if (left_utf8 || right_utf8) {
1092 Perl_croak(aTHX_ FATAL_ABOVE_FF_MSG, PL_op_desc[optype]);
1094 else { /* Neither is UTF-8 */
1095 len = MIN(leftlen, rightlen);
1103 (void)SvPOK_only(sv);
1104 if (SvOK(sv) || SvTYPE(sv) > SVt_PVMG) {
1105 dc = SvPV_force_nomg_nolen(sv);
1106 if (SvLEN(sv) < len + 1) {
1107 dc = SvGROW(sv, len + 1);
1108 (void)memzero(dc + SvCUR(sv), len - SvCUR(sv) + 1);
1112 needlen = optype == OP_BIT_AND
1113 ? len : (leftlen > rightlen ? leftlen : rightlen);
1114 Newxz(dc, needlen + 1, char);
1115 sv_usepvn_flags(sv, dc, needlen, SV_HAS_TRAILING_NUL);
1116 dc = SvPVX(sv); /* sv_usepvn() calls Renew() */
1119 if (len >= sizeof(long)*4 &&
1120 !(PTR2nat(dc) % sizeof(long)) &&
1121 !(PTR2nat(lc) % sizeof(long)) &&
1122 !(PTR2nat(rc) % sizeof(long))) /* It's almost always aligned... */
1124 const STRLEN remainder = len % (sizeof(long)*4);
1125 len /= (sizeof(long)*4);
1134 *dl++ = *ll++ & *rl++;
1135 *dl++ = *ll++ & *rl++;
1136 *dl++ = *ll++ & *rl++;
1137 *dl++ = *ll++ & *rl++;
1142 *dl++ = *ll++ ^ *rl++;
1143 *dl++ = *ll++ ^ *rl++;
1144 *dl++ = *ll++ ^ *rl++;
1145 *dl++ = *ll++ ^ *rl++;
1150 *dl++ = *ll++ | *rl++;
1151 *dl++ = *ll++ | *rl++;
1152 *dl++ = *ll++ | *rl++;
1153 *dl++ = *ll++ | *rl++;
1167 *dc++ = *lc++ & *rc++;
1172 *dc++ = *lc++ ^ *rc++;
1176 *dc++ = *lc++ | *rc++;
1179 if (rightlen > len) {
1181 SvCUR_set(sv, rightlen);
1183 sv_catpvn_nomg(sv, rsave + len, rightlen - len);
1185 else if (leftlen > len) {
1187 SvCUR_set(sv, leftlen);
1189 sv_catpvn_nomg(sv, lsave + len, leftlen - len);
1193 /* If there is trailing stuff that couldn't be converted from UTF-8, it
1194 * is appended as-is for the ^ and | operators. This preserves
1195 * backwards compatibility */
1196 if (right_non_downgraded) {
1197 non_downgraded = (char *) right_non_downgraded;
1198 non_downgraded_len = right_non_downgraded_len;
1200 else if (left_non_downgraded) {
1201 non_downgraded = (char *) left_non_downgraded;
1202 non_downgraded_len = left_non_downgraded_len;
1208 if (result_needs_to_be_utf8) {
1209 sv_utf8_upgrade_nomg(sv);
1211 /* Append any trailing UTF-8 as-is. */
1212 if (non_downgraded) {
1213 sv_catpvn_nomg(sv, non_downgraded, non_downgraded_len);
1221 /* Perl_do_kv() may be:
1222 * * called directly as the pp function for pp_keys() and pp_values();
1223 * * It may also be called directly when the op is OP_AVHVSWITCH, to
1224 * implement CORE::keys(), CORE::values().
1226 * In all cases it expects an HV on the stack and returns a list of keys,
1227 * values, or key-value pairs, depending on PL_op.
1234 HV * const keys = MUTABLE_HV(POPs);
1235 const U8 gimme = GIMME_V;
1237 const I32 dokeys = (PL_op->op_type == OP_KEYS)
1238 || ( PL_op->op_type == OP_AVHVSWITCH
1239 && (PL_op->op_private & OPpAVHVSWITCH_MASK)
1240 + OP_EACH == OP_KEYS);
1242 const I32 dovalues = (PL_op->op_type == OP_VALUES)
1243 || ( PL_op->op_type == OP_AVHVSWITCH
1244 && (PL_op->op_private & OPpAVHVSWITCH_MASK)
1245 + OP_EACH == OP_VALUES);
1247 assert( PL_op->op_type == OP_KEYS
1248 || PL_op->op_type == OP_VALUES
1249 || PL_op->op_type == OP_AVHVSWITCH);
1251 assert(!( PL_op->op_type == OP_VALUES
1252 && (PL_op->op_private & OPpMAYBE_LVSUB)));
1254 (void)hv_iterinit(keys); /* always reset iterator regardless */
1256 if (gimme == G_VOID)
1259 if (gimme == G_SCALAR) {
1260 if (PL_op->op_flags & OPf_MOD || LVRET) { /* lvalue */
1261 SV * const ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
1262 sv_magic(ret, NULL, PERL_MAGIC_nkeys, NULL, 0);
1264 LvTARG(ret) = SvREFCNT_inc_simple(keys);
1271 /* note that in 'scalar(keys %h)' the OP_KEYS is usually
1272 * optimised away and the action is performed directly by the
1273 * padhv or rv2hv op. We now only get here via OP_AVHVSWITCH
1276 if (! SvTIED_mg((const SV *)keys, PERL_MAGIC_tied) ) {
1277 i = HvUSEDKEYS(keys);
1281 while (hv_iternext(keys)) i++;
1288 if (UNLIKELY(PL_op->op_private & OPpMAYBE_LVSUB)) {
1289 const I32 flags = is_lvalue_sub();
1290 if (flags && !(flags & OPpENTERSUB_INARGS))
1291 /* diag_listed_as: Can't modify %s in %s */
1292 Perl_croak(aTHX_ "Can't modify keys in list assignment");
1296 hv_pushkv(keys, (dokeys | (dovalues << 1)));
1301 * ex: set ts=8 sts=4 sw=4 et: