3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2004, 2005, 2006, 2007, 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."
15 /* This file contains some common functions needed to carry out certain
16 * ops. For example both pp_schomp() and pp_chomp() - scalar and array
17 * chomp operations - call the function do_chomp() found in this file.
21 #define PERL_IN_DOOP_C
29 S_do_trans_simple(pTHX_ SV * const sv)
34 U8 *s = (U8*)SvPV(sv,len);
35 U8 * const send = s+len;
37 const short * const tbl = (short*)cPVOP->op_pv;
39 Perl_croak(aTHX_ "panic: do_trans_simple line %d",__LINE__);
41 /* First, take care of non-UTF-8 input strings, because they're easy */
44 const I32 ch = tbl[*s];
54 const I32 grows = PL_op->op_private & OPpTRANS_GROWS;
58 /* Allow for expansion: $_="a".chr(400); tr/a/\xFE/, FE needs encoding */
68 /* Need to check this, otherwise 128..255 won't match */
69 const UV c = utf8n_to_uvchr(s, send - s, &ulen, UTF8_ALLOW_DEFAULT);
70 if (c < 0x100 && (ch = tbl[c]) >= 0) {
72 d = uvchr_to_utf8(d, ch);
75 else { /* No match -> copy */
82 sv_setpvn(sv, (char*)dstart, d - dstart);
87 SvCUR_set(sv, d - dstart);
96 S_do_trans_count(pTHX_ SV * const sv)
100 const U8 *s = (const U8*)SvPV_const(sv, len);
101 const U8 * const send = s + len;
104 const short * const tbl = (short*)cPVOP->op_pv;
106 Perl_croak(aTHX_ "panic: do_trans_count line %d",__LINE__);
115 const I32 complement = PL_op->op_private & OPpTRANS_COMPLEMENT;
118 const UV c = utf8n_to_uvchr(s, send - s, &ulen, UTF8_ALLOW_DEFAULT);
122 } else if (complement)
132 S_do_trans_complex(pTHX_ SV * const sv)
136 U8 *s = (U8*)SvPV(sv, len);
137 U8 * const send = s+len;
140 const short * const tbl = (short*)cPVOP->op_pv;
142 Perl_croak(aTHX_ "panic: do_trans_complex line %d",__LINE__);
146 U8 * const dstart = d;
148 if (PL_op->op_private & OPpTRANS_SQUASH) {
151 const I32 ch = tbl[*s];
155 if (p != d - 1 || *p != *d)
158 else if (ch == -1) /* -1 is unmapped character */
160 else if (ch == -2) /* -2 is delete character */
167 const I32 ch = tbl[*s];
172 else if (ch == -1) /* -1 is unmapped character */
174 else if (ch == -2) /* -2 is delete character */
180 SvCUR_set(sv, d - dstart);
183 const I32 complement = PL_op->op_private & OPpTRANS_COMPLEMENT;
184 const I32 grows = PL_op->op_private & OPpTRANS_GROWS;
185 const I32 del = PL_op->op_private & OPpTRANS_DELETE;
191 Newx(d, len*2+1, U8);
195 if (complement && !del)
198 #ifdef MACOS_TRADITIONAL
199 #define comp CoMP /* "comp" is a keyword in some compilers ... */
202 if (PL_op->op_private & OPpTRANS_SQUASH) {
206 const UV comp = utf8n_to_uvchr(s, send - s, &len,
218 ch = (rlen == 0) ? (I32)comp :
219 (comp - 0x100 < rlen) ?
220 tbl[comp+1] : tbl[0x100+rlen];
222 d = uvchr_to_utf8(d, ch);
230 else if ((ch = tbl[comp]) >= 0) {
233 d = uvchr_to_utf8(d, ch);
239 else if (ch == -1) { /* -1 is unmapped character */
243 else if (ch == -2) /* -2 is delete character */
252 const UV comp = utf8n_to_uvchr(s, send - s, &len,
263 if (comp - 0x100 < rlen)
264 d = uvchr_to_utf8(d, tbl[comp+1]);
266 d = uvchr_to_utf8(d, tbl[0x100+rlen]);
270 else if ((ch = tbl[comp]) >= 0) {
271 d = uvchr_to_utf8(d, ch);
274 else if (ch == -1) { /* -1 is unmapped character */
278 else if (ch == -2) /* -2 is delete character */
284 sv_setpvn(sv, (char*)dstart, d - dstart);
289 SvCUR_set(sv, d - dstart);
298 S_do_trans_simple_utf8(pTHX_ SV * const sv)
307 const I32 grows = PL_op->op_private & OPpTRANS_GROWS;
310 SV* const rv = (SV*)cSVOP->op_sv;
311 HV* const hv = (HV*)SvRV(rv);
312 SV* const * svp = hv_fetchs(hv, "NONE", FALSE);
313 const UV none = svp ? SvUV(*svp) : 0x7fffffff;
314 const UV extra = none + 1;
318 s = (U8*)SvPV(sv, len);
321 const U8 * const e = s + len;
324 hibit = !NATIVE_IS_INVARIANT(ch);
326 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 = uvuni_to_utf8(d, uv);
356 else if (uv == none) {
357 const int i = UTF8SKIP(s);
362 else if (uv == extra) {
365 d = uvuni_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)
401 const U8 *start = NULL;
406 SV* const rv = (SV*)cSVOP->op_sv;
407 HV* const hv = (HV*)SvRV(rv);
408 SV* const * const svp = hv_fetchs(hv, "NONE", FALSE);
409 const UV none = svp ? SvUV(*svp) : 0x7fffffff;
410 const UV extra = none + 1;
413 s = (const U8*)SvPV_const(sv, len);
416 const U8 * const e = s + len;
419 hibit = !NATIVE_IS_INVARIANT(ch);
421 start = s = bytes_to_utf8(s, &len);
429 const UV uv = swash_fetch(rv, s, TRUE);
430 if (uv < none || uv == extra)
441 S_do_trans_complex_utf8(pTHX_ SV * const sv)
447 const I32 squash = PL_op->op_private & OPpTRANS_SQUASH;
448 const I32 del = PL_op->op_private & OPpTRANS_DELETE;
449 const I32 grows = PL_op->op_private & OPpTRANS_GROWS;
450 SV * const rv = (SV*)cSVOP->op_sv;
451 HV * const hv = (HV*)SvRV(rv);
452 SV * const *svp = hv_fetchs(hv, "NONE", FALSE);
453 const UV none = svp ? SvUV(*svp) : 0x7fffffff;
454 const UV extra = none + 1;
456 bool havefinal = FALSE;
461 U8 *s = (U8*)SvPV(sv, len);
464 const U8 * const e = s + len;
467 hibit = !NATIVE_IS_INVARIANT(ch);
469 s = bytes_to_utf8(s, &len);
477 svp = hv_fetchs(hv, "FINAL", FALSE);
484 /* d needs to be bigger than s, in case e.g. upgrading is required */
485 Newx(d, len * 3 + UTF8_MAXBYTES, U8);
497 UV uv = swash_fetch(rv, s, TRUE);
500 const STRLEN clen = d - dstart;
501 const STRLEN nlen = dend - dstart + len + UTF8_MAXBYTES;
503 Perl_croak(aTHX_ "panic: do_trans_complex_utf8 line %d",__LINE__);
504 Renew(dstart, nlen + UTF8_MAXBYTES, U8);
506 dend = dstart + nlen;
512 d = uvuni_to_utf8(d, uv);
517 else if (uv == none) { /* "none" is unmapped character */
518 const int i = UTF8SKIP(s);
525 else if (uv == extra && !del) {
530 d = uvuni_to_utf8(d, final);
536 uv = utf8n_to_uvuni(s, send - s, &len, UTF8_ALLOW_DEFAULT);
546 matches++; /* "none+1" is delete character */
552 const UV uv = swash_fetch(rv, s, TRUE);
554 const STRLEN clen = d - dstart;
555 const STRLEN nlen = dend - dstart + len + UTF8_MAXBYTES;
557 Perl_croak(aTHX_ "panic: do_trans_complex_utf8 line %d",__LINE__);
558 Renew(dstart, nlen + UTF8_MAXBYTES, U8);
560 dend = dstart + nlen;
565 d = uvuni_to_utf8(d, uv);
568 else if (uv == none) { /* "none" is unmapped character */
569 const int i = UTF8SKIP(s);
575 else if (uv == extra && !del) {
578 d = uvuni_to_utf8(d, final);
581 matches++; /* "none+1" is delete character */
585 if (grows || hibit) {
586 sv_setpvn(sv, (char*)dstart, d - dstart);
593 SvCUR_set(sv, d - dstart);
602 Perl_do_trans(pTHX_ SV *sv)
606 const I32 hasutf = (PL_op->op_private &
607 (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF));
609 if (SvREADONLY(sv)) {
611 sv_force_normal_flags(sv, 0);
612 if (SvREADONLY(sv) && !(PL_op->op_private & OPpTRANS_IDENTICAL))
613 Perl_croak(aTHX_ PL_no_modify);
615 (void)SvPV_const(sv, len);
618 if (!(PL_op->op_private & OPpTRANS_IDENTICAL)) {
620 (void)SvPV_force(sv, len);
621 (void)SvPOK_only_UTF8(sv);
624 DEBUG_t( Perl_deb(aTHX_ "2.TBL\n"));
626 switch (PL_op->op_private & ~hasutf & (
627 OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF|OPpTRANS_IDENTICAL|
628 OPpTRANS_SQUASH|OPpTRANS_DELETE|OPpTRANS_COMPLEMENT)) {
631 return do_trans_simple_utf8(sv);
633 return do_trans_simple(sv);
635 case OPpTRANS_IDENTICAL:
636 case OPpTRANS_IDENTICAL|OPpTRANS_COMPLEMENT:
638 return do_trans_count_utf8(sv);
640 return do_trans_count(sv);
644 return do_trans_complex_utf8(sv);
646 return do_trans_complex(sv);
651 Perl_do_join(pTHX_ register SV *sv, SV *delim, register SV **mark, register SV **sp)
654 SV ** const oldmark = mark;
655 register I32 items = sp - mark;
659 (void) SvPV_const(delim, delimlen); /* stringify and get the delimlen */
660 /* SvCUR assumes it's SvPOK() and woe betide you if it's not. */
663 len = (items > 0 ? (delimlen * (items - 1) ) : 0);
664 SvUPGRADE(sv, SVt_PV);
665 if (SvLEN(sv) < len + items) { /* current length is way too short */
666 while (items-- > 0) {
667 if (*mark && !SvGAMAGIC(*mark) && SvOK(*mark)) {
669 SvPV_const(*mark, tmplen);
674 SvGROW(sv, len + 1); /* so try to pre-extend */
681 sv_setpvn(sv, "", 0);
682 /* sv_setpv retains old UTF8ness [perl #24846] */
685 if (PL_tainting && SvMAGICAL(sv))
695 for (; items > 0; items--,mark++) {
701 for (; items > 0; items--,mark++)
708 Perl_do_sprintf(pTHX_ SV *sv, I32 len, SV **sarg)
712 const char * const pat = SvPV_const(*sarg, patlen);
713 bool do_taint = FALSE;
718 sv_vsetpvfn(sv, pat, patlen, NULL, sarg + 1, len - 1, &do_taint);
724 /* currently converts input to bytes if possible, but doesn't sweat failure */
726 Perl_do_vecget(pTHX_ SV *sv, I32 offset, I32 size)
729 STRLEN srclen, len, uoffset, bitoffs = 0;
730 const unsigned char *s = (const unsigned char *) SvPV_const(sv, srclen);
735 if (size < 1 || (size & (size-1))) /* size < 1 or not a power of two */
736 Perl_croak(aTHX_ "Illegal number of bits in vec");
739 (void) Perl_sv_utf8_downgrade(aTHX_ sv, TRUE);
742 bitoffs = ((offset%8)*size)%8;
743 uoffset = offset/(8/size);
746 uoffset = offset*(size/8);
750 len = uoffset + (bitoffs + size + 7)/8; /* required number of bytes */
756 if (uoffset >= srclen)
759 retnum = (UV) s[uoffset] << 8;
761 else if (size == 32) {
762 if (uoffset >= srclen)
764 else if (uoffset + 1 >= srclen)
766 ((UV) s[uoffset ] << 24);
767 else if (uoffset + 2 >= srclen)
769 ((UV) s[uoffset ] << 24) +
770 ((UV) s[uoffset + 1] << 16);
773 ((UV) s[uoffset ] << 24) +
774 ((UV) s[uoffset + 1] << 16) +
775 ( s[uoffset + 2] << 8);
778 else if (size == 64) {
779 if (ckWARN(WARN_PORTABLE))
780 Perl_warner(aTHX_ packWARN(WARN_PORTABLE),
781 "Bit vector size > 32 non-portable");
782 if (uoffset >= srclen)
784 else if (uoffset + 1 >= srclen)
786 (UV) s[uoffset ] << 56;
787 else if (uoffset + 2 >= srclen)
789 ((UV) s[uoffset ] << 56) +
790 ((UV) s[uoffset + 1] << 48);
791 else if (uoffset + 3 >= srclen)
793 ((UV) s[uoffset ] << 56) +
794 ((UV) s[uoffset + 1] << 48) +
795 ((UV) s[uoffset + 2] << 40);
796 else if (uoffset + 4 >= srclen)
798 ((UV) s[uoffset ] << 56) +
799 ((UV) s[uoffset + 1] << 48) +
800 ((UV) s[uoffset + 2] << 40) +
801 ((UV) s[uoffset + 3] << 32);
802 else if (uoffset + 5 >= srclen)
804 ((UV) s[uoffset ] << 56) +
805 ((UV) s[uoffset + 1] << 48) +
806 ((UV) s[uoffset + 2] << 40) +
807 ((UV) s[uoffset + 3] << 32) +
808 ( s[uoffset + 4] << 24);
809 else if (uoffset + 6 >= srclen)
811 ((UV) s[uoffset ] << 56) +
812 ((UV) s[uoffset + 1] << 48) +
813 ((UV) s[uoffset + 2] << 40) +
814 ((UV) s[uoffset + 3] << 32) +
815 ((UV) s[uoffset + 4] << 24) +
816 ((UV) s[uoffset + 5] << 16);
819 ((UV) s[uoffset ] << 56) +
820 ((UV) s[uoffset + 1] << 48) +
821 ((UV) s[uoffset + 2] << 40) +
822 ((UV) s[uoffset + 3] << 32) +
823 ((UV) s[uoffset + 4] << 24) +
824 ((UV) s[uoffset + 5] << 16) +
825 ( s[uoffset + 6] << 8);
831 retnum = (s[uoffset] >> bitoffs) & ((1 << size) - 1);
837 ((UV) s[uoffset] << 8) +
841 ((UV) s[uoffset ] << 24) +
842 ((UV) s[uoffset + 1] << 16) +
843 ( s[uoffset + 2] << 8) +
846 else if (size == 64) {
847 if (ckWARN(WARN_PORTABLE))
848 Perl_warner(aTHX_ packWARN(WARN_PORTABLE),
849 "Bit vector size > 32 non-portable");
851 ((UV) s[uoffset ] << 56) +
852 ((UV) s[uoffset + 1] << 48) +
853 ((UV) s[uoffset + 2] << 40) +
854 ((UV) s[uoffset + 3] << 32) +
855 ((UV) s[uoffset + 4] << 24) +
856 ((UV) s[uoffset + 5] << 16) +
857 ( s[uoffset + 6] << 8) +
866 /* currently converts input to bytes if possible but doesn't sweat failures,
867 * although it does ensure that the string it clobbers is not marked as
868 * utf8-valid any more
871 Perl_do_vecset(pTHX_ SV *sv)
874 register I32 offset, bitoffs = 0;
876 register unsigned char *s;
881 SV * const targ = LvTARG(sv);
885 s = (unsigned char*)SvPV_force(targ, targlen);
887 /* This is handled by the SvPOK_only below...
888 if (!Perl_sv_utf8_downgrade(aTHX_ targ, TRUE))
891 (void) Perl_sv_utf8_downgrade(aTHX_ targ, TRUE);
894 (void)SvPOK_only(targ);
896 offset = LvTARGOFF(sv);
898 Perl_croak(aTHX_ "Negative offset to vec in lvalue context");
899 size = LvTARGLEN(sv);
900 if (size < 1 || (size & (size-1))) /* size < 1 or not a power of two */
901 Perl_croak(aTHX_ "Illegal number of bits in vec");
904 bitoffs = ((offset%8)*size)%8;
910 len = offset + (bitoffs + size + 7)/8; /* required number of bytes */
912 s = (unsigned char*)SvGROW(targ, len + 1);
913 (void)memzero((char *)(s + targlen), len - targlen + 1);
914 SvCUR_set(targ, len);
918 mask = (1 << size) - 1;
920 s[offset] &= ~(mask << bitoffs);
921 s[offset] |= lval << bitoffs;
925 s[offset ] = (U8)( lval & 0xff);
926 else if (size == 16) {
927 s[offset ] = (U8)((lval >> 8) & 0xff);
928 s[offset+1] = (U8)( lval & 0xff);
930 else if (size == 32) {
931 s[offset ] = (U8)((lval >> 24) & 0xff);
932 s[offset+1] = (U8)((lval >> 16) & 0xff);
933 s[offset+2] = (U8)((lval >> 8) & 0xff);
934 s[offset+3] = (U8)( lval & 0xff);
937 else if (size == 64) {
938 if (ckWARN(WARN_PORTABLE))
939 Perl_warner(aTHX_ packWARN(WARN_PORTABLE),
940 "Bit vector size > 32 non-portable");
941 s[offset ] = (U8)((lval >> 56) & 0xff);
942 s[offset+1] = (U8)((lval >> 48) & 0xff);
943 s[offset+2] = (U8)((lval >> 40) & 0xff);
944 s[offset+3] = (U8)((lval >> 32) & 0xff);
945 s[offset+4] = (U8)((lval >> 24) & 0xff);
946 s[offset+5] = (U8)((lval >> 16) & 0xff);
947 s[offset+6] = (U8)((lval >> 8) & 0xff);
948 s[offset+7] = (U8)( lval & 0xff);
956 Perl_do_chop(pTHX_ register SV *astr, register SV *sv)
962 if (SvTYPE(sv) == SVt_PVAV) {
964 AV* const av = (AV*)sv;
965 const I32 max = AvFILL(av);
967 for (i = 0; i <= max; i++) {
968 sv = (SV*)av_fetch(av, i, FALSE);
969 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
974 else if (SvTYPE(sv) == SVt_PVHV) {
975 HV* const hv = (HV*)sv;
977 (void)hv_iterinit(hv);
978 while ((entry = hv_iternext(hv)))
979 do_chop(astr,hv_iterval(hv,entry));
982 else if (SvREADONLY(sv)) {
984 /* SV is copy-on-write */
985 sv_force_normal_flags(sv, 0);
988 Perl_croak(aTHX_ PL_no_modify);
991 if (PL_encoding && !SvUTF8(sv)) {
992 /* like in do_chomp(), utf8-ize the sv as a side-effect
993 * if we're using encoding. */
994 sv_recode_to_utf8(sv, PL_encoding);
998 if (len && !SvPOK(sv))
999 s = SvPV_force(sv, len);
1002 char * const send = s + len;
1003 char * const start = s;
1005 while (s > start && UTF8_IS_CONTINUATION(*s))
1007 if (is_utf8_string((U8*)s, send - s)) {
1008 sv_setpvn(astr, s, send - s);
1010 SvCUR_set(sv, s - start);
1016 sv_setpvn(astr, "", 0);
1018 else if (s && len) {
1020 sv_setpvn(astr, s, 1);
1027 sv_setpvn(astr, "", 0);
1032 Perl_do_chomp(pTHX_ register SV *sv)
1038 char *temp_buffer = NULL;
1039 SV* svrecode = NULL;
1043 if (RsRECORD(PL_rs))
1046 if (SvTYPE(sv) == SVt_PVAV) {
1048 AV* const av = (AV*)sv;
1049 const I32 max = AvFILL(av);
1051 for (i = 0; i <= max; i++) {
1052 sv = (SV*)av_fetch(av, i, FALSE);
1053 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
1054 count += do_chomp(sv);
1058 else if (SvTYPE(sv) == SVt_PVHV) {
1059 HV* const hv = (HV*)sv;
1061 (void)hv_iterinit(hv);
1062 while ((entry = hv_iternext(hv)))
1063 count += do_chomp(hv_iterval(hv,entry));
1066 else if (SvREADONLY(sv)) {
1068 /* SV is copy-on-write */
1069 sv_force_normal_flags(sv, 0);
1072 Perl_croak(aTHX_ PL_no_modify);
1077 /* XXX, here sv is utf8-ized as a side-effect!
1078 If encoding.pm is used properly, almost string-generating
1079 operations, including literal strings, chr(), input data, etc.
1080 should have been utf8-ized already, right?
1082 sv_recode_to_utf8(sv, PL_encoding);
1089 if (RsPARA(PL_rs)) {
1093 while (len && s[-1] == '\n') {
1100 STRLEN rslen, rs_charlen;
1101 const char *rsptr = SvPV_const(PL_rs, rslen);
1103 rs_charlen = SvUTF8(PL_rs)
1104 ? sv_len_utf8(PL_rs)
1107 if (SvUTF8(PL_rs) != SvUTF8(sv)) {
1108 /* Assumption is that rs is shorter than the scalar. */
1109 if (SvUTF8(PL_rs)) {
1110 /* RS is utf8, scalar is 8 bit. */
1111 bool is_utf8 = TRUE;
1112 temp_buffer = (char*)bytes_from_utf8((U8*)rsptr,
1115 /* Cannot downgrade, therefore cannot possibly match
1117 assert (temp_buffer == rsptr);
1121 rsptr = temp_buffer;
1123 else if (PL_encoding) {
1124 /* RS is 8 bit, encoding.pm is used.
1125 * Do not recode PL_rs as a side-effect. */
1126 svrecode = newSVpvn(rsptr, rslen);
1127 sv_recode_to_utf8(svrecode, PL_encoding);
1128 rsptr = SvPV_const(svrecode, rslen);
1129 rs_charlen = sv_len_utf8(svrecode);
1132 /* RS is 8 bit, scalar is utf8. */
1133 temp_buffer = (char*)bytes_to_utf8((U8*)rsptr, &rslen);
1134 rsptr = temp_buffer;
1143 if (len < rslen - 1)
1147 if (memNE(s, rsptr, rslen))
1149 count += rs_charlen;
1152 s = SvPV_force_nolen(sv);
1161 SvREFCNT_dec(svrecode);
1163 Safefree(temp_buffer);
1168 Perl_do_vop(pTHX_ I32 optype, SV *sv, SV *left, SV *right)
1179 register const char *lc;
1180 register const char *rc;
1181 register STRLEN len;
1190 if (sv != left || (optype != OP_BIT_AND && !SvOK(sv) && !SvGMAGICAL(sv)))
1191 sv_setpvn(sv, "", 0); /* avoid undef warning on |= and ^= */
1192 lsave = lc = SvPV_nomg_const(left, leftlen);
1193 rsave = rc = SvPV_nomg_const(right, rightlen);
1195 /* This need to come after SvPV to ensure that string overloading has
1198 left_utf = DO_UTF8(left);
1199 right_utf = DO_UTF8(right);
1201 if (left_utf && !right_utf) {
1202 /* Avoid triggering overloading again by using temporaries.
1203 Maybe there should be a variant of sv_utf8_upgrade that takes pvn
1205 right = sv_2mortal(newSVpvn(rsave, rightlen));
1206 sv_utf8_upgrade(right);
1207 rsave = rc = SvPV_nomg_const(right, rightlen);
1210 else if (!left_utf && right_utf) {
1211 left = sv_2mortal(newSVpvn(lsave, leftlen));
1212 sv_utf8_upgrade(left);
1213 lsave = lc = SvPV_nomg_const(left, leftlen);
1217 len = leftlen < rightlen ? leftlen : rightlen;
1220 (void)SvPOK_only(sv);
1221 if ((left_utf || right_utf) && (sv == left || sv == right)) {
1222 needlen = optype == OP_BIT_AND ? len : leftlen + rightlen;
1223 Newxz(dc, needlen + 1, char);
1225 else if (SvOK(sv) || SvTYPE(sv) > SVt_PVMG) {
1226 dc = SvPV_force_nomg_nolen(sv);
1227 if (SvLEN(sv) < len + 1) {
1228 dc = SvGROW(sv, len + 1);
1229 (void)memzero(dc + SvCUR(sv), len - SvCUR(sv) + 1);
1231 if (optype != OP_BIT_AND && (left_utf || right_utf))
1232 dc = SvGROW(sv, leftlen + rightlen + 1);
1235 needlen = optype == OP_BIT_AND
1236 ? len : (leftlen > rightlen ? leftlen : rightlen);
1237 Newxz(dc, needlen + 1, char);
1238 sv_usepvn_flags(sv, dc, needlen, SV_HAS_TRAILING_NUL);
1239 dc = SvPVX(sv); /* sv_usepvn() calls Renew() */
1241 if (left_utf || right_utf) {
1244 char *dcsave = NULL;
1245 STRLEN lulen = leftlen;
1246 STRLEN rulen = rightlen;
1251 while (lulen && rulen) {
1252 luc = utf8n_to_uvchr((U8*)lc, lulen, &ulen, UTF8_ALLOW_ANYUV);
1255 ruc = utf8n_to_uvchr((U8*)rc, rulen, &ulen, UTF8_ALLOW_ANYUV);
1259 dc = (char*)uvchr_to_utf8((U8*)dc, duc);
1261 if (sv == left || sv == right)
1262 (void)sv_usepvn(sv, dcorig, needlen);
1263 SvCUR_set(sv, dc - dcorig);
1266 while (lulen && rulen) {
1267 luc = utf8n_to_uvchr((U8*)lc, lulen, &ulen, UTF8_ALLOW_ANYUV);
1270 ruc = utf8n_to_uvchr((U8*)rc, rulen, &ulen, UTF8_ALLOW_ANYUV);
1274 dc = (char*)uvchr_to_utf8((U8*)dc, duc);
1278 while (lulen && rulen) {
1279 luc = utf8n_to_uvchr((U8*)lc, lulen, &ulen, UTF8_ALLOW_ANYUV);
1282 ruc = utf8n_to_uvchr((U8*)rc, rulen, &ulen, UTF8_ALLOW_ANYUV);
1286 dc = (char*)uvchr_to_utf8((U8*)dc, duc);
1290 dcsave = savepvn(rc, rulen);
1292 dcsave = savepvn(lc, lulen);
1293 if (sv == left || sv == right)
1294 (void)sv_usepvn(sv, dcorig, needlen); /* Uses Renew(). */
1295 SvCUR_set(sv, dc - dcorig);
1297 sv_catpvn(sv, dcsave, rulen);
1299 sv_catpvn(sv, dcsave, lulen);
1305 if (sv == left || sv == right)
1307 Perl_croak(aTHX_ "panic: do_vop called for op %u (%s)",
1308 (unsigned)optype, PL_op_name[optype]);
1315 if (len >= sizeof(long)*4 &&
1316 !((unsigned long)dc % sizeof(long)) &&
1317 !((unsigned long)lc % sizeof(long)) &&
1318 !((unsigned long)rc % sizeof(long))) /* It's almost always aligned... */
1320 const STRLEN remainder = len % (sizeof(long)*4);
1321 len /= (sizeof(long)*4);
1330 *dl++ = *ll++ & *rl++;
1331 *dl++ = *ll++ & *rl++;
1332 *dl++ = *ll++ & *rl++;
1333 *dl++ = *ll++ & *rl++;
1338 *dl++ = *ll++ ^ *rl++;
1339 *dl++ = *ll++ ^ *rl++;
1340 *dl++ = *ll++ ^ *rl++;
1341 *dl++ = *ll++ ^ *rl++;
1346 *dl++ = *ll++ | *rl++;
1347 *dl++ = *ll++ | *rl++;
1348 *dl++ = *ll++ | *rl++;
1349 *dl++ = *ll++ | *rl++;
1364 *dc++ = *lc++ & *rc++;
1369 *dc++ = *lc++ ^ *rc++;
1373 *dc++ = *lc++ | *rc++;
1377 sv_catpvn(sv, rsave + len, rightlen - len);
1378 else if (leftlen > (STRLEN)len)
1379 sv_catpvn(sv, lsave + len, leftlen - len);
1394 HV * const hv = (HV*)POPs;
1397 const I32 gimme = GIMME_V;
1398 const I32 dokv = (PL_op->op_type == OP_RV2HV || PL_op->op_type == OP_PADHV);
1399 const I32 dokeys = dokv || (PL_op->op_type == OP_KEYS);
1400 const I32 dovalues = dokv || (PL_op->op_type == OP_VALUES);
1403 if (PL_op->op_flags & OPf_MOD || LVRET) { /* lvalue */
1404 dTARGET; /* make sure to clear its target here */
1405 if (SvTYPE(TARG) == SVt_PVLV)
1406 LvTARG(TARG) = NULL;
1413 (void)hv_iterinit(keys); /* always reset iterator regardless */
1415 if (gimme == G_VOID)
1418 if (gimme == G_SCALAR) {
1422 if (PL_op->op_flags & OPf_MOD || LVRET) { /* lvalue */
1423 if (SvTYPE(TARG) < SVt_PVLV) {
1424 sv_upgrade(TARG, SVt_PVLV);
1425 sv_magic(TARG, NULL, PERL_MAGIC_nkeys, NULL, 0);
1428 if (LvTARG(TARG) != (SV*)keys) {
1430 SvREFCNT_dec(LvTARG(TARG));
1431 LvTARG(TARG) = SvREFCNT_inc_simple(keys);
1437 if (! SvTIED_mg((SV*)keys, PERL_MAGIC_tied) )
1443 while (hv_iternext(keys)) i++;
1449 EXTEND(SP, HvKEYS(keys) * (dokeys + dovalues));
1451 PUTBACK; /* hv_iternext and hv_iterval might clobber stack_sp */
1452 while ((entry = hv_iternext(keys))) {
1455 SV* const sv = hv_iterkeysv(entry);
1456 XPUSHs(sv); /* won't clobber stack_sp */
1461 tmpstr = hv_iterval(hv,entry);
1462 DEBUG_H(Perl_sv_setpvf(aTHX_ tmpstr, "%lu%%%d=%lu",
1463 (unsigned long)HeHASH(entry),
1465 (unsigned long)(HeHASH(entry) & HvMAX(keys))));
1476 * c-indentation-style: bsd
1478 * indent-tabs-mode: t
1481 * ex: set ts=8 sts=4 sw=4 noet: