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 S_do_trans_simple(pTHX_ SV * const sv)
35 U8 *s = (U8*)SvPV_nomg(sv,len);
36 U8 * const send = s+len;
37 const short * const tbl = (short*)cPVOP->op_pv;
39 PERL_ARGS_ASSERT_DO_TRANS_SIMPLE;
42 Perl_croak(aTHX_ "panic: do_trans_simple line %d",__LINE__);
44 /* First, take care of non-UTF-8 input strings, because they're easy */
47 const I32 ch = tbl[*s];
57 const I32 grows = PL_op->op_private & OPpTRANS_GROWS;
61 /* Allow for expansion: $_="a".chr(400); tr/a/\xFE/, FE needs encoding */
71 /* Need to check this, otherwise 128..255 won't match */
72 const UV c = utf8n_to_uvchr(s, send - s, &ulen, UTF8_ALLOW_DEFAULT);
73 if (c < 0x100 && (ch = tbl[c]) >= 0) {
75 d = uvchr_to_utf8(d, ch);
78 else { /* No match -> copy */
85 sv_setpvn(sv, (char*)dstart, d - dstart);
90 SvCUR_set(sv, d - dstart);
99 S_do_trans_count(pTHX_ SV * const sv)
102 const U8 *s = (const U8*)SvPV_nomg_const(sv, len);
103 const U8 * const send = s + len;
105 const short * const tbl = (short*)cPVOP->op_pv;
107 PERL_ARGS_ASSERT_DO_TRANS_COUNT;
110 Perl_croak(aTHX_ "panic: do_trans_count line %d",__LINE__);
119 const I32 complement = PL_op->op_private & OPpTRANS_COMPLEMENT;
122 const UV c = utf8n_to_uvchr(s, send - s, &ulen, UTF8_ALLOW_DEFAULT);
126 } else if (complement)
136 S_do_trans_complex(pTHX_ SV * const sv)
139 U8 *s = (U8*)SvPV_nomg(sv, len);
140 U8 * const send = s+len;
142 const short * const tbl = (short*)cPVOP->op_pv;
144 PERL_ARGS_ASSERT_DO_TRANS_COMPLEX;
147 Perl_croak(aTHX_ "panic: do_trans_complex line %d",__LINE__);
151 U8 * const dstart = d;
153 if (PL_op->op_private & OPpTRANS_SQUASH) {
156 const I32 ch = tbl[*s];
160 if (p != d - 1 || *p != *d)
163 else if (ch == -1) /* -1 is unmapped character */
165 else if (ch == -2) /* -2 is delete character */
172 const I32 ch = tbl[*s];
177 else if (ch == -1) /* -1 is unmapped character */
179 else if (ch == -2) /* -2 is delete character */
185 SvCUR_set(sv, d - dstart);
188 const I32 complement = PL_op->op_private & OPpTRANS_COMPLEMENT;
189 const I32 grows = PL_op->op_private & OPpTRANS_GROWS;
190 const I32 del = PL_op->op_private & OPpTRANS_DELETE;
196 Newx(d, len*2+1, U8);
200 if (complement && !del)
203 if (PL_op->op_private & OPpTRANS_SQUASH) {
207 const UV comp = utf8n_to_uvchr(s, send - s, &len,
219 ch = (rlen == 0) ? (I32)comp :
220 (comp - 0x100 < rlen) ?
221 tbl[comp+1] : tbl[0x100+rlen];
223 d = uvchr_to_utf8(d, ch);
231 else if ((ch = tbl[comp]) >= 0) {
234 d = uvchr_to_utf8(d, ch);
240 else if (ch == -1) { /* -1 is unmapped character */
244 else if (ch == -2) /* -2 is delete character */
253 const UV comp = utf8n_to_uvchr(s, send - s, &len,
264 if (comp - 0x100 < rlen)
265 d = uvchr_to_utf8(d, tbl[comp+1]);
267 d = uvchr_to_utf8(d, tbl[0x100+rlen]);
271 else if ((ch = tbl[comp]) >= 0) {
272 d = uvchr_to_utf8(d, ch);
275 else if (ch == -1) { /* -1 is unmapped character */
279 else if (ch == -2) /* -2 is delete character */
285 sv_setpvn(sv, (char*)dstart, d - dstart);
290 SvCUR_set(sv, d - dstart);
299 S_do_trans_simple_utf8(pTHX_ SV * const sv)
307 const I32 grows = PL_op->op_private & OPpTRANS_GROWS;
311 PAD_SVl(cPADOP->op_padix);
313 MUTABLE_SV(cSVOP->op_sv);
315 HV* const hv = MUTABLE_HV(SvRV(rv));
316 SV* const * svp = hv_fetchs(hv, "NONE", FALSE);
317 const UV none = svp ? SvUV(*svp) : 0x7fffffff;
318 const UV extra = none + 1;
322 PERL_ARGS_ASSERT_DO_TRANS_SIMPLE_UTF8;
324 s = (U8*)SvPV_nomg(sv, len);
326 hibit = ! is_utf8_invariant_string(s, len);
328 s = bytes_to_utf8(s, &len);
334 svp = hv_fetchs(hv, "FINAL", FALSE);
339 /* d needs to be bigger than s, in case e.g. upgrading is required */
340 Newx(d, len * 3 + UTF8_MAXBYTES, U8);
350 const UV uv = swash_fetch(rv, s, TRUE);
354 d = uvchr_to_utf8(d, uv);
356 else if (uv == none) {
357 const int i = UTF8SKIP(s);
362 else if (uv == extra) {
365 d = uvchr_to_utf8(d, final);
371 const STRLEN clen = d - dstart;
372 const STRLEN nlen = dend - dstart + len + UTF8_MAXBYTES;
374 Perl_croak(aTHX_ "panic: do_trans_simple_utf8 line %d",__LINE__);
375 Renew(dstart, nlen + UTF8_MAXBYTES, U8);
377 dend = dstart + nlen;
380 if (grows || hibit) {
381 sv_setpvn(sv, (char*)dstart, d - dstart);
388 SvCUR_set(sv, d - dstart);
397 S_do_trans_count_utf8(pTHX_ SV * const sv)
400 const U8 *start = NULL;
406 PAD_SVl(cPADOP->op_padix);
408 MUTABLE_SV(cSVOP->op_sv);
410 HV* const hv = MUTABLE_HV(SvRV(rv));
411 SV* const * const svp = hv_fetchs(hv, "NONE", FALSE);
412 const UV none = svp ? SvUV(*svp) : 0x7fffffff;
413 const UV extra = none + 1;
416 PERL_ARGS_ASSERT_DO_TRANS_COUNT_UTF8;
418 s = (const U8*)SvPV_nomg_const(sv, len);
420 hibit = ! is_utf8_invariant_string(s, len);
422 start = s = bytes_to_utf8(s, &len);
428 const UV uv = swash_fetch(rv, s, TRUE);
429 if (uv < none || uv == extra)
440 S_do_trans_complex_utf8(pTHX_ SV * const sv)
445 const I32 squash = PL_op->op_private & OPpTRANS_SQUASH;
446 const I32 del = PL_op->op_private & OPpTRANS_DELETE;
447 const I32 grows = PL_op->op_private & OPpTRANS_GROWS;
450 PAD_SVl(cPADOP->op_padix);
452 MUTABLE_SV(cSVOP->op_sv);
454 HV * const hv = MUTABLE_HV(SvRV(rv));
455 SV * const *svp = hv_fetchs(hv, "NONE", FALSE);
456 const UV none = svp ? SvUV(*svp) : 0x7fffffff;
457 const UV extra = none + 1;
459 bool havefinal = FALSE;
463 U8 *s = (U8*)SvPV_nomg(sv, len);
465 PERL_ARGS_ASSERT_DO_TRANS_COMPLEX_UTF8;
468 hibit = ! is_utf8_invariant_string(s, len);
470 s = bytes_to_utf8(s, &len);
476 svp = hv_fetchs(hv, "FINAL", FALSE);
483 /* d needs to be bigger than s, in case e.g. upgrading is required */
484 Newx(d, len * 3 + UTF8_MAXBYTES, U8);
496 UV uv = swash_fetch(rv, s, TRUE);
499 const STRLEN clen = d - dstart;
500 const STRLEN nlen = dend - dstart + len + UTF8_MAXBYTES;
502 Perl_croak(aTHX_ "panic: do_trans_complex_utf8 line %d",__LINE__);
503 Renew(dstart, nlen + UTF8_MAXBYTES, U8);
505 dend = dstart + nlen;
511 d = uvchr_to_utf8(d, uv);
516 else if (uv == none) { /* "none" is unmapped character */
517 const int i = UTF8SKIP(s);
524 else if (uv == extra && !del) {
529 d = uvchr_to_utf8(d, final);
535 uv = utf8n_to_uvchr(s, send - s, &len, UTF8_ALLOW_DEFAULT);
545 matches++; /* "none+1" is delete character */
551 const UV uv = swash_fetch(rv, s, TRUE);
553 const STRLEN clen = d - dstart;
554 const STRLEN nlen = dend - dstart + len + UTF8_MAXBYTES;
556 Perl_croak(aTHX_ "panic: do_trans_complex_utf8 line %d",__LINE__);
557 Renew(dstart, nlen + UTF8_MAXBYTES, U8);
559 dend = dstart + nlen;
564 d = uvchr_to_utf8(d, uv);
567 else if (uv == none) { /* "none" is unmapped character */
568 const int i = UTF8SKIP(s);
574 else if (uv == extra && !del) {
577 d = uvchr_to_utf8(d, final);
580 matches++; /* "none+1" is delete character */
584 if (grows || hibit) {
585 sv_setpvn(sv, (char*)dstart, d - dstart);
592 SvCUR_set(sv, d - dstart);
601 Perl_do_trans(pTHX_ SV *sv)
604 const I32 flags = PL_op->op_private;
605 const I32 hasutf = flags & (OPpTRANS_FROM_UTF | OPpTRANS_TO_UTF);
607 PERL_ARGS_ASSERT_DO_TRANS;
609 if (SvREADONLY(sv) && !(flags & OPpTRANS_IDENTICAL)) {
610 Perl_croak_no_modify();
612 (void)SvPV_const(sv, len);
615 if (!(flags & OPpTRANS_IDENTICAL)) {
616 if (!SvPOKp(sv) || SvTHINKFIRST(sv))
617 (void)SvPV_force_nomg(sv, len);
618 (void)SvPOK_only_UTF8(sv);
621 DEBUG_t( Perl_deb(aTHX_ "2.TBL\n"));
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);
628 } else if (flags & (OPpTRANS_SQUASH|OPpTRANS_DELETE|OPpTRANS_COMPLEMENT)) {
629 return hasutf ? do_trans_complex_utf8(sv) : do_trans_complex(sv);
631 return hasutf ? do_trans_simple_utf8(sv) : do_trans_simple(sv);
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(aTHX_ sv, TRUE)) {
750 /* PVX may have changed */
751 s = (unsigned char *) SvPV_flags(sv, srclen, svpv_flags);
754 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
755 "Use of strings with code points over 0xFF as"
756 " arguments to vec is deprecated. This will"
757 " be a fatal error in Perl 5.32");
762 bitoffs = ((offset%8)*size)%8;
763 uoffset = offset/(8/size);
767 if (offset > Size_t_MAX / n - 1) /* would overflow */
774 if (uoffset >= srclen)
777 len = (bitoffs + size + 7)/8; /* required number of bytes */
778 avail = srclen - uoffset; /* available number of bytes */
780 /* Does the byte range overlap the end of the string? If so,
781 * handle specially. */
788 retnum = (UV) s[uoffset] << 8;
790 else if (size == 32) {
791 assert(avail >= 1 && avail <= 3);
794 ((UV) s[uoffset ] << 24);
797 ((UV) s[uoffset ] << 24) +
798 ((UV) s[uoffset + 1] << 16);
801 ((UV) s[uoffset ] << 24) +
802 ((UV) s[uoffset + 1] << 16) +
803 ( s[uoffset + 2] << 8);
806 else if (size == 64) {
807 Perl_ck_warner(aTHX_ packWARN(WARN_PORTABLE),
808 "Bit vector size > 32 non-portable");
809 assert(avail >= 1 && avail <= 7);
812 (UV) s[uoffset ] << 56;
815 ((UV) s[uoffset ] << 56) +
816 ((UV) s[uoffset + 1] << 48);
819 ((UV) s[uoffset ] << 56) +
820 ((UV) s[uoffset + 1] << 48) +
821 ((UV) s[uoffset + 2] << 40);
824 ((UV) s[uoffset ] << 56) +
825 ((UV) s[uoffset + 1] << 48) +
826 ((UV) s[uoffset + 2] << 40) +
827 ((UV) s[uoffset + 3] << 32);
830 ((UV) s[uoffset ] << 56) +
831 ((UV) s[uoffset + 1] << 48) +
832 ((UV) s[uoffset + 2] << 40) +
833 ((UV) s[uoffset + 3] << 32) +
834 ((UV) s[uoffset + 4] << 24);
837 ((UV) s[uoffset ] << 56) +
838 ((UV) s[uoffset + 1] << 48) +
839 ((UV) s[uoffset + 2] << 40) +
840 ((UV) s[uoffset + 3] << 32) +
841 ((UV) s[uoffset + 4] << 24) +
842 ((UV) s[uoffset + 5] << 16);
845 ((UV) s[uoffset ] << 56) +
846 ((UV) s[uoffset + 1] << 48) +
847 ((UV) s[uoffset + 2] << 40) +
848 ((UV) s[uoffset + 3] << 32) +
849 ((UV) s[uoffset + 4] << 24) +
850 ((UV) s[uoffset + 5] << 16) +
851 ((UV) s[uoffset + 6] << 8);
857 retnum = (s[uoffset] >> bitoffs) & ((1 << size) - 1);
863 ((UV) s[uoffset] << 8) +
867 ((UV) s[uoffset ] << 24) +
868 ((UV) s[uoffset + 1] << 16) +
869 ( s[uoffset + 2] << 8) +
872 else if (size == 64) {
873 Perl_ck_warner(aTHX_ packWARN(WARN_PORTABLE),
874 "Bit vector size > 32 non-portable");
876 ((UV) s[uoffset ] << 56) +
877 ((UV) s[uoffset + 1] << 48) +
878 ((UV) s[uoffset + 2] << 40) +
879 ((UV) s[uoffset + 3] << 32) +
880 ((UV) s[uoffset + 4] << 24) +
881 ((UV) s[uoffset + 5] << 16) +
882 ( s[uoffset + 6] << 8) +
891 /* currently converts input to bytes if possible but doesn't sweat failures,
892 * although it does ensure that the string it clobbers is not marked as
893 * utf8-valid any more
896 Perl_do_vecset(pTHX_ SV *sv)
898 STRLEN offset, bitoffs = 0;
905 SV * const targ = LvTARG(sv);
906 char errflags = LvFLAGS(sv);
908 PERL_ARGS_ASSERT_DO_VECSET;
910 /* some out-of-range errors have been deferred if/until the LV is
911 * actually written to: f(vec($s,-1,8)) is not always fatal */
913 assert(!(errflags & ~(LVf_NEG_OFF|LVf_OUT_OF_RANGE)));
914 if (errflags & LVf_NEG_OFF)
915 Perl_croak_nocontext("Negative offset to vec in lvalue context");
916 Perl_croak_nocontext("Out of memory!");
921 s = (unsigned char*)SvPV_force_flags(targ, targlen,
922 SV_GMAGIC | SV_UNDEF_RETURNS_NULL);
924 /* This is handled by the SvPOK_only below...
925 if (!Perl_sv_utf8_downgrade(aTHX_ targ, TRUE))
928 (void) Perl_sv_utf8_downgrade(aTHX_ targ, TRUE);
931 (void)SvPOK_only(targ);
933 offset = LvTARGOFF(sv);
934 size = LvTARGLEN(sv);
936 if (size < 1 || (size & (size-1))) /* size < 1 or not a power of two */
937 Perl_croak(aTHX_ "Illegal number of bits in vec");
940 bitoffs = ((offset%8)*size)%8;
945 if (offset > Size_t_MAX / n - 1) /* would overflow */
946 Perl_croak_nocontext("Out of memory!");
950 len = (bitoffs + size + 7)/8; /* required number of bytes */
951 if (targlen < offset || targlen - offset < len) {
952 STRLEN newlen = offset > Size_t_MAX - len - 1 ? /* avoid overflow */
953 Size_t_MAX : offset + len + 1;
954 s = (unsigned char*)SvGROW(targ, newlen);
955 (void)memzero((char *)(s + targlen), newlen - targlen);
956 SvCUR_set(targ, newlen - 1);
960 mask = (1 << size) - 1;
962 s[offset] &= ~(mask << bitoffs);
963 s[offset] |= lval << bitoffs;
967 s[offset ] = (U8)( lval & 0xff);
968 else if (size == 16) {
969 s[offset ] = (U8)((lval >> 8) & 0xff);
970 s[offset+1] = (U8)( lval & 0xff);
972 else if (size == 32) {
973 s[offset ] = (U8)((lval >> 24) & 0xff);
974 s[offset+1] = (U8)((lval >> 16) & 0xff);
975 s[offset+2] = (U8)((lval >> 8) & 0xff);
976 s[offset+3] = (U8)( lval & 0xff);
979 else if (size == 64) {
980 Perl_ck_warner(aTHX_ packWARN(WARN_PORTABLE),
981 "Bit vector size > 32 non-portable");
982 s[offset ] = (U8)((lval >> 56) & 0xff);
983 s[offset+1] = (U8)((lval >> 48) & 0xff);
984 s[offset+2] = (U8)((lval >> 40) & 0xff);
985 s[offset+3] = (U8)((lval >> 32) & 0xff);
986 s[offset+4] = (U8)((lval >> 24) & 0xff);
987 s[offset+5] = (U8)((lval >> 16) & 0xff);
988 s[offset+6] = (U8)((lval >> 8) & 0xff);
989 s[offset+7] = (U8)( lval & 0xff);
997 Perl_do_vop(pTHX_ I32 optype, SV *sv, SV *left, SV *right)
1014 bool result_needs_to_be_utf8 = FALSE;
1015 bool left_utf8 = FALSE;
1016 bool right_utf8 = FALSE;
1017 U8 * left_non_downgraded = NULL;
1018 U8 * right_non_downgraded = NULL;
1019 Size_t left_non_downgraded_len = 0;
1020 Size_t right_non_downgraded_len = 0;
1021 char * non_downgraded = NULL;
1022 Size_t non_downgraded_len = 0;
1024 PERL_ARGS_ASSERT_DO_VOP;
1026 if (sv != left || (optype != OP_BIT_AND && !SvOK(sv)))
1027 SvPVCLEAR(sv); /* avoid undef warning on |= and ^= */
1029 lc = SvPV_force_nomg(left, leftlen);
1032 lc = SvPV_nomg_const(left, leftlen);
1033 SvPV_force_nomg_nolen(sv);
1035 rc = SvPV_nomg_const(right, rightlen);
1037 /* This needs to come after SvPV to ensure that string overloading has
1040 /* Create downgraded temporaries of any UTF-8 encoded operands */
1041 if (DO_UTF8(left)) {
1042 const U8 * save_lc = (U8 *) lc;
1045 result_needs_to_be_utf8 = TRUE;
1047 left_non_downgraded_len = leftlen;
1048 lc = (char *) bytes_from_utf8_loc((const U8 *) lc, &leftlen,
1050 (const U8 **) &left_non_downgraded);
1051 /* Calculate the number of trailing unconvertible bytes. This quantity
1052 * is the original length minus the length of the converted portion. */
1053 left_non_downgraded_len -= left_non_downgraded - save_lc;
1056 if (DO_UTF8(right)) {
1057 const U8 * save_rc = (U8 *) rc;
1060 result_needs_to_be_utf8 = TRUE;
1062 right_non_downgraded_len = rightlen;
1063 rc = (char *) bytes_from_utf8_loc((const U8 *) rc, &rightlen,
1065 (const U8 **) &right_non_downgraded);
1066 right_non_downgraded_len -= right_non_downgraded - save_rc;
1070 /* We set 'len' to the length that the operation actually operates on. The
1071 * dangling part of the longer operand doesn't actually participate in the
1072 * operation. What happens is that we pretend that the shorter operand has
1073 * been extended to the right by enough imaginary zeros to match the length
1074 * of the longer one. But we know in advance the result of the operation
1075 * on zeros without having to do it. In the case of '&', the result is
1076 * zero, and the dangling portion is simply discarded. For '|' and '^', the
1077 * result is the same as the other operand, so the dangling part is just
1078 * appended to the final result, unchanged. We currently accept above-FF
1079 * code points in the dangling portion, as that's how it has long worked,
1080 * and code depends on it staying that way. But it is now fatal for
1081 * above-FF to appear in the portion that does get operated on. Hence, any
1082 * above-FF must come only in the longer operand, and only in its dangling
1083 * portion. That means that at least one of the operands has to be
1084 * entirely non-UTF-8, and the length of that operand has to be before the
1085 * first above-FF in the other */
1087 if (right_utf8 || rightlen > leftlen) {
1088 Perl_croak(aTHX_ fatal_above_ff_msg, PL_op_desc[optype]);
1092 else if (right_utf8) {
1093 if (leftlen > rightlen) {
1094 Perl_croak(aTHX_ fatal_above_ff_msg, PL_op_desc[optype]);
1098 else { /* Neither is UTF-8 */
1099 len = leftlen < rightlen ? leftlen : rightlen;
1107 (void)SvPOK_only(sv);
1108 if (SvOK(sv) || SvTYPE(sv) > SVt_PVMG) {
1109 dc = SvPV_force_nomg_nolen(sv);
1110 if (SvLEN(sv) < len + 1) {
1111 dc = SvGROW(sv, len + 1);
1112 (void)memzero(dc + SvCUR(sv), len - SvCUR(sv) + 1);
1116 needlen = optype == OP_BIT_AND
1117 ? len : (leftlen > rightlen ? leftlen : rightlen);
1118 Newxz(dc, needlen + 1, char);
1119 sv_usepvn_flags(sv, dc, needlen, SV_HAS_TRAILING_NUL);
1120 dc = SvPVX(sv); /* sv_usepvn() calls Renew() */
1124 if (len >= sizeof(long)*4 &&
1125 !((unsigned long)dc % sizeof(long)) &&
1126 !((unsigned long)lc % sizeof(long)) &&
1127 !((unsigned long)rc % sizeof(long))) /* It's almost always aligned... */
1129 const STRLEN remainder = len % (sizeof(long)*4);
1130 len /= (sizeof(long)*4);
1139 *dl++ = *ll++ & *rl++;
1140 *dl++ = *ll++ & *rl++;
1141 *dl++ = *ll++ & *rl++;
1142 *dl++ = *ll++ & *rl++;
1147 *dl++ = *ll++ ^ *rl++;
1148 *dl++ = *ll++ ^ *rl++;
1149 *dl++ = *ll++ ^ *rl++;
1150 *dl++ = *ll++ ^ *rl++;
1155 *dl++ = *ll++ | *rl++;
1156 *dl++ = *ll++ | *rl++;
1157 *dl++ = *ll++ | *rl++;
1158 *dl++ = *ll++ | *rl++;
1172 *dc++ = *lc++ & *rc++;
1177 *dc++ = *lc++ ^ *rc++;
1181 *dc++ = *lc++ | *rc++;
1184 if (rightlen > len) {
1186 SvCUR(sv) = rightlen;
1188 sv_catpvn_nomg(sv, rsave + len, rightlen - len);
1190 else if (leftlen > len) {
1192 SvCUR(sv) = leftlen;
1194 sv_catpvn_nomg(sv, lsave + len, leftlen - len);
1198 /* If there is trailing stuff that couldn't be converted from UTF-8, it
1199 * is appended as-is for the ^ and | operators. This preserves
1200 * backwards compatibility */
1201 if (right_non_downgraded) {
1202 non_downgraded = (char *) right_non_downgraded;
1203 non_downgraded_len = right_non_downgraded_len;
1205 else if (left_non_downgraded) {
1206 non_downgraded = (char *) left_non_downgraded;
1207 non_downgraded_len = left_non_downgraded_len;
1213 if (result_needs_to_be_utf8) {
1214 sv_utf8_upgrade_nomg(sv);
1216 /* Append any trailing UTF-8 as-is. */
1217 if (non_downgraded) {
1218 sv_catpvn_nomg(sv, non_downgraded, non_downgraded_len);
1226 /* Perl_do_kv() may be:
1227 * * called directly as the pp function for pp_keys() and pp_values();
1228 * * It may also be called directly when the op is OP_AVHVSWITCH, to
1229 * implement CORE::keys(), CORE::values().
1231 * In all cases it expects an HV on the stack and returns a list of keys,
1232 * values, or key-value pairs, depending on PL_op.
1239 HV * const keys = MUTABLE_HV(POPs);
1240 const U8 gimme = GIMME_V;
1242 const I32 dokeys = (PL_op->op_type == OP_KEYS)
1243 || ( PL_op->op_type == OP_AVHVSWITCH
1244 && (PL_op->op_private & OPpAVHVSWITCH_MASK)
1245 + OP_EACH == OP_KEYS);
1247 const I32 dovalues = (PL_op->op_type == OP_VALUES)
1248 || ( PL_op->op_type == OP_AVHVSWITCH
1249 && (PL_op->op_private & OPpAVHVSWITCH_MASK)
1250 + OP_EACH == OP_VALUES);
1252 assert( PL_op->op_type == OP_KEYS
1253 || PL_op->op_type == OP_VALUES
1254 || PL_op->op_type == OP_AVHVSWITCH);
1256 assert(!( PL_op->op_type == OP_VALUES
1257 && (PL_op->op_private & OPpMAYBE_LVSUB)));
1259 (void)hv_iterinit(keys); /* always reset iterator regardless */
1261 if (gimme == G_VOID)
1264 if (gimme == G_SCALAR) {
1265 if (PL_op->op_flags & OPf_MOD || LVRET) { /* lvalue */
1266 SV * const ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
1267 sv_magic(ret, NULL, PERL_MAGIC_nkeys, NULL, 0);
1269 LvTARG(ret) = SvREFCNT_inc_simple(keys);
1276 /* note that in 'scalar(keys %h)' the OP_KEYS is usually
1277 * optimised away and the action is performed directly by the
1278 * padhv or rv2hv op. We now only get here via OP_AVHVSWITCH
1281 if (! SvTIED_mg((const SV *)keys, PERL_MAGIC_tied) ) {
1282 i = HvUSEDKEYS(keys);
1286 while (hv_iternext(keys)) i++;
1293 if (UNLIKELY(PL_op->op_private & OPpMAYBE_LVSUB)) {
1294 const I32 flags = is_lvalue_sub();
1295 if (flags && !(flags & OPpENTERSUB_INARGS))
1296 /* diag_listed_as: Can't modify %s in %s */
1297 Perl_croak(aTHX_ "Can't modify keys in list assignment");
1301 hv_pushkv(keys, (dokeys | (dovalues << 1)));
1306 * ex: set ts=8 sts=4 sw=4 et: