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;
312 PAD_SVl(cPADOP->op_padix);
316 HV* const hv = (HV*)SvRV(rv);
317 SV* const * svp = hv_fetchs(hv, "NONE", FALSE);
318 const UV none = svp ? SvUV(*svp) : 0x7fffffff;
319 const UV extra = none + 1;
323 s = (U8*)SvPV(sv, len);
326 const U8 * const e = s + len;
329 hibit = !NATIVE_IS_INVARIANT(ch);
331 s = bytes_to_utf8(s, &len);
339 svp = hv_fetchs(hv, "FINAL", FALSE);
344 /* d needs to be bigger than s, in case e.g. upgrading is required */
345 Newx(d, len * 3 + UTF8_MAXBYTES, U8);
355 const UV uv = swash_fetch(rv, s, TRUE);
359 d = uvuni_to_utf8(d, uv);
361 else if (uv == none) {
362 const int i = UTF8SKIP(s);
367 else if (uv == extra) {
370 d = uvuni_to_utf8(d, final);
376 const STRLEN clen = d - dstart;
377 const STRLEN nlen = dend - dstart + len + UTF8_MAXBYTES;
379 Perl_croak(aTHX_ "panic: do_trans_simple_utf8 line %d",__LINE__);
380 Renew(dstart, nlen + UTF8_MAXBYTES, U8);
382 dend = dstart + nlen;
385 if (grows || hibit) {
386 sv_setpvn(sv, (char*)dstart, d - dstart);
393 SvCUR_set(sv, d - dstart);
402 S_do_trans_count_utf8(pTHX_ SV * const sv)
406 const U8 *start = NULL;
413 PAD_SVl(cPADOP->op_padix);
417 HV* const hv = (HV*)SvRV(rv);
418 SV* const * const svp = hv_fetchs(hv, "NONE", FALSE);
419 const UV none = svp ? SvUV(*svp) : 0x7fffffff;
420 const UV extra = none + 1;
423 s = (const U8*)SvPV_const(sv, len);
426 const U8 * const e = s + len;
429 hibit = !NATIVE_IS_INVARIANT(ch);
431 start = s = bytes_to_utf8(s, &len);
439 const UV uv = swash_fetch(rv, s, TRUE);
440 if (uv < none || uv == extra)
451 S_do_trans_complex_utf8(pTHX_ SV * const sv)
457 const I32 squash = PL_op->op_private & OPpTRANS_SQUASH;
458 const I32 del = PL_op->op_private & OPpTRANS_DELETE;
459 const I32 grows = PL_op->op_private & OPpTRANS_GROWS;
462 PAD_SVl(cPADOP->op_padix);
466 HV * const hv = (HV*)SvRV(rv);
467 SV * const *svp = hv_fetchs(hv, "NONE", FALSE);
468 const UV none = svp ? SvUV(*svp) : 0x7fffffff;
469 const UV extra = none + 1;
471 bool havefinal = FALSE;
476 U8 *s = (U8*)SvPV(sv, len);
479 const U8 * const e = s + len;
482 hibit = !NATIVE_IS_INVARIANT(ch);
484 s = bytes_to_utf8(s, &len);
492 svp = hv_fetchs(hv, "FINAL", FALSE);
499 /* d needs to be bigger than s, in case e.g. upgrading is required */
500 Newx(d, len * 3 + UTF8_MAXBYTES, U8);
512 UV uv = swash_fetch(rv, s, TRUE);
515 const STRLEN clen = d - dstart;
516 const STRLEN nlen = dend - dstart + len + UTF8_MAXBYTES;
518 Perl_croak(aTHX_ "panic: do_trans_complex_utf8 line %d",__LINE__);
519 Renew(dstart, nlen + UTF8_MAXBYTES, U8);
521 dend = dstart + nlen;
527 d = uvuni_to_utf8(d, uv);
532 else if (uv == none) { /* "none" is unmapped character */
533 const int i = UTF8SKIP(s);
540 else if (uv == extra && !del) {
545 d = uvuni_to_utf8(d, final);
551 uv = utf8n_to_uvuni(s, send - s, &len, UTF8_ALLOW_DEFAULT);
561 matches++; /* "none+1" is delete character */
567 const UV uv = swash_fetch(rv, s, TRUE);
569 const STRLEN clen = d - dstart;
570 const STRLEN nlen = dend - dstart + len + UTF8_MAXBYTES;
572 Perl_croak(aTHX_ "panic: do_trans_complex_utf8 line %d",__LINE__);
573 Renew(dstart, nlen + UTF8_MAXBYTES, U8);
575 dend = dstart + nlen;
580 d = uvuni_to_utf8(d, uv);
583 else if (uv == none) { /* "none" is unmapped character */
584 const int i = UTF8SKIP(s);
590 else if (uv == extra && !del) {
593 d = uvuni_to_utf8(d, final);
596 matches++; /* "none+1" is delete character */
600 if (grows || hibit) {
601 sv_setpvn(sv, (char*)dstart, d - dstart);
608 SvCUR_set(sv, d - dstart);
617 Perl_do_trans(pTHX_ SV *sv)
621 const I32 hasutf = (PL_op->op_private &
622 (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF));
624 if (SvREADONLY(sv)) {
626 sv_force_normal_flags(sv, 0);
627 if (SvREADONLY(sv) && !(PL_op->op_private & OPpTRANS_IDENTICAL))
628 Perl_croak(aTHX_ PL_no_modify);
630 (void)SvPV_const(sv, len);
633 if (!(PL_op->op_private & OPpTRANS_IDENTICAL)) {
635 (void)SvPV_force(sv, len);
636 (void)SvPOK_only_UTF8(sv);
639 DEBUG_t( Perl_deb(aTHX_ "2.TBL\n"));
641 switch (PL_op->op_private & ~hasutf & (
642 OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF|OPpTRANS_IDENTICAL|
643 OPpTRANS_SQUASH|OPpTRANS_DELETE|OPpTRANS_COMPLEMENT)) {
646 return do_trans_simple_utf8(sv);
648 return do_trans_simple(sv);
650 case OPpTRANS_IDENTICAL:
651 case OPpTRANS_IDENTICAL|OPpTRANS_COMPLEMENT:
653 return do_trans_count_utf8(sv);
655 return do_trans_count(sv);
659 return do_trans_complex_utf8(sv);
661 return do_trans_complex(sv);
666 Perl_do_join(pTHX_ register SV *sv, SV *delim, register SV **mark, register SV **sp)
669 SV ** const oldmark = mark;
670 register I32 items = sp - mark;
674 (void) SvPV_const(delim, delimlen); /* stringify and get the delimlen */
675 /* SvCUR assumes it's SvPOK() and woe betide you if it's not. */
678 len = (items > 0 ? (delimlen * (items - 1) ) : 0);
679 SvUPGRADE(sv, SVt_PV);
680 if (SvLEN(sv) < len + items) { /* current length is way too short */
681 while (items-- > 0) {
682 if (*mark && !SvGAMAGIC(*mark) && SvOK(*mark)) {
684 SvPV_const(*mark, tmplen);
689 SvGROW(sv, len + 1); /* so try to pre-extend */
696 sv_setpvn(sv, "", 0);
697 /* sv_setpv retains old UTF8ness [perl #24846] */
700 if (PL_tainting && SvMAGICAL(sv))
710 for (; items > 0; items--,mark++) {
716 for (; items > 0; items--,mark++)
723 Perl_do_sprintf(pTHX_ SV *sv, I32 len, SV **sarg)
727 const char * const pat = SvPV_const(*sarg, patlen);
728 bool do_taint = FALSE;
733 sv_vsetpvfn(sv, pat, patlen, NULL, sarg + 1, len - 1, &do_taint);
739 /* currently converts input to bytes if possible, but doesn't sweat failure */
741 Perl_do_vecget(pTHX_ SV *sv, I32 offset, I32 size)
744 STRLEN srclen, len, uoffset, bitoffs = 0;
745 const unsigned char *s = (const unsigned char *) SvPV_const(sv, srclen);
750 if (size < 1 || (size & (size-1))) /* size < 1 or not a power of two */
751 Perl_croak(aTHX_ "Illegal number of bits in vec");
754 (void) Perl_sv_utf8_downgrade(aTHX_ sv, TRUE);
757 bitoffs = ((offset%8)*size)%8;
758 uoffset = offset/(8/size);
761 uoffset = offset*(size/8);
765 len = uoffset + (bitoffs + size + 7)/8; /* required number of bytes */
771 if (uoffset >= srclen)
774 retnum = (UV) s[uoffset] << 8;
776 else if (size == 32) {
777 if (uoffset >= srclen)
779 else if (uoffset + 1 >= srclen)
781 ((UV) s[uoffset ] << 24);
782 else if (uoffset + 2 >= srclen)
784 ((UV) s[uoffset ] << 24) +
785 ((UV) s[uoffset + 1] << 16);
788 ((UV) s[uoffset ] << 24) +
789 ((UV) s[uoffset + 1] << 16) +
790 ( s[uoffset + 2] << 8);
793 else if (size == 64) {
794 if (ckWARN(WARN_PORTABLE))
795 Perl_warner(aTHX_ packWARN(WARN_PORTABLE),
796 "Bit vector size > 32 non-portable");
797 if (uoffset >= srclen)
799 else if (uoffset + 1 >= srclen)
801 (UV) s[uoffset ] << 56;
802 else if (uoffset + 2 >= srclen)
804 ((UV) s[uoffset ] << 56) +
805 ((UV) s[uoffset + 1] << 48);
806 else if (uoffset + 3 >= srclen)
808 ((UV) s[uoffset ] << 56) +
809 ((UV) s[uoffset + 1] << 48) +
810 ((UV) s[uoffset + 2] << 40);
811 else if (uoffset + 4 >= srclen)
813 ((UV) s[uoffset ] << 56) +
814 ((UV) s[uoffset + 1] << 48) +
815 ((UV) s[uoffset + 2] << 40) +
816 ((UV) s[uoffset + 3] << 32);
817 else if (uoffset + 5 >= srclen)
819 ((UV) s[uoffset ] << 56) +
820 ((UV) s[uoffset + 1] << 48) +
821 ((UV) s[uoffset + 2] << 40) +
822 ((UV) s[uoffset + 3] << 32) +
823 ( s[uoffset + 4] << 24);
824 else if (uoffset + 6 >= srclen)
826 ((UV) s[uoffset ] << 56) +
827 ((UV) s[uoffset + 1] << 48) +
828 ((UV) s[uoffset + 2] << 40) +
829 ((UV) s[uoffset + 3] << 32) +
830 ((UV) s[uoffset + 4] << 24) +
831 ((UV) s[uoffset + 5] << 16);
834 ((UV) s[uoffset ] << 56) +
835 ((UV) s[uoffset + 1] << 48) +
836 ((UV) s[uoffset + 2] << 40) +
837 ((UV) s[uoffset + 3] << 32) +
838 ((UV) s[uoffset + 4] << 24) +
839 ((UV) s[uoffset + 5] << 16) +
840 ( s[uoffset + 6] << 8);
846 retnum = (s[uoffset] >> bitoffs) & ((1 << size) - 1);
852 ((UV) s[uoffset] << 8) +
856 ((UV) s[uoffset ] << 24) +
857 ((UV) s[uoffset + 1] << 16) +
858 ( s[uoffset + 2] << 8) +
861 else if (size == 64) {
862 if (ckWARN(WARN_PORTABLE))
863 Perl_warner(aTHX_ packWARN(WARN_PORTABLE),
864 "Bit vector size > 32 non-portable");
866 ((UV) s[uoffset ] << 56) +
867 ((UV) s[uoffset + 1] << 48) +
868 ((UV) s[uoffset + 2] << 40) +
869 ((UV) s[uoffset + 3] << 32) +
870 ((UV) s[uoffset + 4] << 24) +
871 ((UV) s[uoffset + 5] << 16) +
872 ( s[uoffset + 6] << 8) +
881 /* currently converts input to bytes if possible but doesn't sweat failures,
882 * although it does ensure that the string it clobbers is not marked as
883 * utf8-valid any more
886 Perl_do_vecset(pTHX_ SV *sv)
889 register I32 offset, bitoffs = 0;
891 register unsigned char *s;
896 SV * const targ = LvTARG(sv);
900 s = (unsigned char*)SvPV_force(targ, targlen);
902 /* This is handled by the SvPOK_only below...
903 if (!Perl_sv_utf8_downgrade(aTHX_ targ, TRUE))
906 (void) Perl_sv_utf8_downgrade(aTHX_ targ, TRUE);
909 (void)SvPOK_only(targ);
911 offset = LvTARGOFF(sv);
913 Perl_croak(aTHX_ "Negative offset to vec in lvalue context");
914 size = LvTARGLEN(sv);
915 if (size < 1 || (size & (size-1))) /* size < 1 or not a power of two */
916 Perl_croak(aTHX_ "Illegal number of bits in vec");
919 bitoffs = ((offset%8)*size)%8;
925 len = offset + (bitoffs + size + 7)/8; /* required number of bytes */
927 s = (unsigned char*)SvGROW(targ, len + 1);
928 (void)memzero((char *)(s + targlen), len - targlen + 1);
929 SvCUR_set(targ, len);
933 mask = (1 << size) - 1;
935 s[offset] &= ~(mask << bitoffs);
936 s[offset] |= lval << bitoffs;
940 s[offset ] = (U8)( lval & 0xff);
941 else if (size == 16) {
942 s[offset ] = (U8)((lval >> 8) & 0xff);
943 s[offset+1] = (U8)( lval & 0xff);
945 else if (size == 32) {
946 s[offset ] = (U8)((lval >> 24) & 0xff);
947 s[offset+1] = (U8)((lval >> 16) & 0xff);
948 s[offset+2] = (U8)((lval >> 8) & 0xff);
949 s[offset+3] = (U8)( lval & 0xff);
952 else if (size == 64) {
953 if (ckWARN(WARN_PORTABLE))
954 Perl_warner(aTHX_ packWARN(WARN_PORTABLE),
955 "Bit vector size > 32 non-portable");
956 s[offset ] = (U8)((lval >> 56) & 0xff);
957 s[offset+1] = (U8)((lval >> 48) & 0xff);
958 s[offset+2] = (U8)((lval >> 40) & 0xff);
959 s[offset+3] = (U8)((lval >> 32) & 0xff);
960 s[offset+4] = (U8)((lval >> 24) & 0xff);
961 s[offset+5] = (U8)((lval >> 16) & 0xff);
962 s[offset+6] = (U8)((lval >> 8) & 0xff);
963 s[offset+7] = (U8)( lval & 0xff);
971 Perl_do_chop(pTHX_ register SV *astr, register SV *sv)
977 if (SvTYPE(sv) == SVt_PVAV) {
979 AV* const av = (AV*)sv;
980 const I32 max = AvFILL(av);
982 for (i = 0; i <= max; i++) {
983 sv = (SV*)av_fetch(av, i, FALSE);
984 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
989 else if (SvTYPE(sv) == SVt_PVHV) {
990 HV* const hv = (HV*)sv;
992 (void)hv_iterinit(hv);
993 while ((entry = hv_iternext(hv)))
994 do_chop(astr,hv_iterval(hv,entry));
997 else if (SvREADONLY(sv)) {
999 /* SV is copy-on-write */
1000 sv_force_normal_flags(sv, 0);
1003 Perl_croak(aTHX_ PL_no_modify);
1006 if (PL_encoding && !SvUTF8(sv)) {
1007 /* like in do_chomp(), utf8-ize the sv as a side-effect
1008 * if we're using encoding. */
1009 sv_recode_to_utf8(sv, PL_encoding);
1013 if (len && !SvPOK(sv))
1014 s = SvPV_force(sv, len);
1017 char * const send = s + len;
1018 char * const start = s;
1020 while (s > start && UTF8_IS_CONTINUATION(*s))
1022 if (is_utf8_string((U8*)s, send - s)) {
1023 sv_setpvn(astr, s, send - s);
1025 SvCUR_set(sv, s - start);
1031 sv_setpvn(astr, "", 0);
1033 else if (s && len) {
1035 sv_setpvn(astr, s, 1);
1042 sv_setpvn(astr, "", 0);
1047 Perl_do_chomp(pTHX_ register SV *sv)
1053 char *temp_buffer = NULL;
1054 SV* svrecode = NULL;
1058 if (RsRECORD(PL_rs))
1061 if (SvTYPE(sv) == SVt_PVAV) {
1063 AV* const av = (AV*)sv;
1064 const I32 max = AvFILL(av);
1066 for (i = 0; i <= max; i++) {
1067 sv = (SV*)av_fetch(av, i, FALSE);
1068 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
1069 count += do_chomp(sv);
1073 else if (SvTYPE(sv) == SVt_PVHV) {
1074 HV* const hv = (HV*)sv;
1076 (void)hv_iterinit(hv);
1077 while ((entry = hv_iternext(hv)))
1078 count += do_chomp(hv_iterval(hv,entry));
1081 else if (SvREADONLY(sv)) {
1083 /* SV is copy-on-write */
1084 sv_force_normal_flags(sv, 0);
1087 Perl_croak(aTHX_ PL_no_modify);
1092 /* XXX, here sv is utf8-ized as a side-effect!
1093 If encoding.pm is used properly, almost string-generating
1094 operations, including literal strings, chr(), input data, etc.
1095 should have been utf8-ized already, right?
1097 sv_recode_to_utf8(sv, PL_encoding);
1104 if (RsPARA(PL_rs)) {
1108 while (len && s[-1] == '\n') {
1115 STRLEN rslen, rs_charlen;
1116 const char *rsptr = SvPV_const(PL_rs, rslen);
1118 rs_charlen = SvUTF8(PL_rs)
1119 ? sv_len_utf8(PL_rs)
1122 if (SvUTF8(PL_rs) != SvUTF8(sv)) {
1123 /* Assumption is that rs is shorter than the scalar. */
1124 if (SvUTF8(PL_rs)) {
1125 /* RS is utf8, scalar is 8 bit. */
1126 bool is_utf8 = TRUE;
1127 temp_buffer = (char*)bytes_from_utf8((U8*)rsptr,
1130 /* Cannot downgrade, therefore cannot possibly match
1132 assert (temp_buffer == rsptr);
1136 rsptr = temp_buffer;
1138 else if (PL_encoding) {
1139 /* RS is 8 bit, encoding.pm is used.
1140 * Do not recode PL_rs as a side-effect. */
1141 svrecode = newSVpvn(rsptr, rslen);
1142 sv_recode_to_utf8(svrecode, PL_encoding);
1143 rsptr = SvPV_const(svrecode, rslen);
1144 rs_charlen = sv_len_utf8(svrecode);
1147 /* RS is 8 bit, scalar is utf8. */
1148 temp_buffer = (char*)bytes_to_utf8((U8*)rsptr, &rslen);
1149 rsptr = temp_buffer;
1158 if (len < rslen - 1)
1162 if (memNE(s, rsptr, rslen))
1164 count += rs_charlen;
1167 s = SvPV_force_nolen(sv);
1176 SvREFCNT_dec(svrecode);
1178 Safefree(temp_buffer);
1183 Perl_do_vop(pTHX_ I32 optype, SV *sv, SV *left, SV *right)
1194 register const char *lc;
1195 register const char *rc;
1196 register STRLEN len;
1205 if (sv != left || (optype != OP_BIT_AND && !SvOK(sv) && !SvGMAGICAL(sv)))
1206 sv_setpvn(sv, "", 0); /* avoid undef warning on |= and ^= */
1207 lsave = lc = SvPV_nomg_const(left, leftlen);
1208 rsave = rc = SvPV_nomg_const(right, rightlen);
1210 /* This need to come after SvPV to ensure that string overloading has
1213 left_utf = DO_UTF8(left);
1214 right_utf = DO_UTF8(right);
1216 if (left_utf && !right_utf) {
1217 /* Avoid triggering overloading again by using temporaries.
1218 Maybe there should be a variant of sv_utf8_upgrade that takes pvn
1220 right = sv_2mortal(newSVpvn(rsave, rightlen));
1221 sv_utf8_upgrade(right);
1222 rsave = rc = SvPV_nomg_const(right, rightlen);
1225 else if (!left_utf && right_utf) {
1226 left = sv_2mortal(newSVpvn(lsave, leftlen));
1227 sv_utf8_upgrade(left);
1228 lsave = lc = SvPV_nomg_const(left, leftlen);
1232 len = leftlen < rightlen ? leftlen : rightlen;
1235 (void)SvPOK_only(sv);
1236 if ((left_utf || right_utf) && (sv == left || sv == right)) {
1237 needlen = optype == OP_BIT_AND ? len : leftlen + rightlen;
1238 Newxz(dc, needlen + 1, char);
1240 else if (SvOK(sv) || SvTYPE(sv) > SVt_PVMG) {
1241 dc = SvPV_force_nomg_nolen(sv);
1242 if (SvLEN(sv) < len + 1) {
1243 dc = SvGROW(sv, len + 1);
1244 (void)memzero(dc + SvCUR(sv), len - SvCUR(sv) + 1);
1246 if (optype != OP_BIT_AND && (left_utf || right_utf))
1247 dc = SvGROW(sv, leftlen + rightlen + 1);
1250 needlen = optype == OP_BIT_AND
1251 ? len : (leftlen > rightlen ? leftlen : rightlen);
1252 Newxz(dc, needlen + 1, char);
1253 sv_usepvn_flags(sv, dc, needlen, SV_HAS_TRAILING_NUL);
1254 dc = SvPVX(sv); /* sv_usepvn() calls Renew() */
1256 if (left_utf || right_utf) {
1259 char *dcsave = NULL;
1260 STRLEN lulen = leftlen;
1261 STRLEN rulen = rightlen;
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);
1276 if (sv == left || sv == right)
1277 (void)sv_usepvn(sv, dcorig, needlen);
1278 SvCUR_set(sv, dc - dcorig);
1281 while (lulen && rulen) {
1282 luc = utf8n_to_uvchr((U8*)lc, lulen, &ulen, UTF8_ALLOW_ANYUV);
1285 ruc = utf8n_to_uvchr((U8*)rc, rulen, &ulen, UTF8_ALLOW_ANYUV);
1289 dc = (char*)uvchr_to_utf8((U8*)dc, duc);
1293 while (lulen && rulen) {
1294 luc = utf8n_to_uvchr((U8*)lc, lulen, &ulen, UTF8_ALLOW_ANYUV);
1297 ruc = utf8n_to_uvchr((U8*)rc, rulen, &ulen, UTF8_ALLOW_ANYUV);
1301 dc = (char*)uvchr_to_utf8((U8*)dc, duc);
1305 dcsave = savepvn(rc, rulen);
1307 dcsave = savepvn(lc, lulen);
1308 if (sv == left || sv == right)
1309 (void)sv_usepvn(sv, dcorig, needlen); /* Uses Renew(). */
1310 SvCUR_set(sv, dc - dcorig);
1312 sv_catpvn(sv, dcsave, rulen);
1314 sv_catpvn(sv, dcsave, lulen);
1320 if (sv == left || sv == right)
1322 Perl_croak(aTHX_ "panic: do_vop called for op %u (%s)",
1323 (unsigned)optype, PL_op_name[optype]);
1330 if (len >= sizeof(long)*4 &&
1331 !((unsigned long)dc % sizeof(long)) &&
1332 !((unsigned long)lc % sizeof(long)) &&
1333 !((unsigned long)rc % sizeof(long))) /* It's almost always aligned... */
1335 const STRLEN remainder = len % (sizeof(long)*4);
1336 len /= (sizeof(long)*4);
1345 *dl++ = *ll++ & *rl++;
1346 *dl++ = *ll++ & *rl++;
1347 *dl++ = *ll++ & *rl++;
1348 *dl++ = *ll++ & *rl++;
1353 *dl++ = *ll++ ^ *rl++;
1354 *dl++ = *ll++ ^ *rl++;
1355 *dl++ = *ll++ ^ *rl++;
1356 *dl++ = *ll++ ^ *rl++;
1361 *dl++ = *ll++ | *rl++;
1362 *dl++ = *ll++ | *rl++;
1363 *dl++ = *ll++ | *rl++;
1364 *dl++ = *ll++ | *rl++;
1379 *dc++ = *lc++ & *rc++;
1384 *dc++ = *lc++ ^ *rc++;
1388 *dc++ = *lc++ | *rc++;
1392 sv_catpvn(sv, rsave + len, rightlen - len);
1393 else if (leftlen > (STRLEN)len)
1394 sv_catpvn(sv, lsave + len, leftlen - len);
1409 HV * const hv = (HV*)POPs;
1412 const I32 gimme = GIMME_V;
1413 const I32 dokv = (PL_op->op_type == OP_RV2HV || PL_op->op_type == OP_PADHV);
1414 const I32 dokeys = dokv || (PL_op->op_type == OP_KEYS);
1415 const I32 dovalues = dokv || (PL_op->op_type == OP_VALUES);
1418 if (PL_op->op_flags & OPf_MOD || LVRET) { /* lvalue */
1419 dTARGET; /* make sure to clear its target here */
1420 if (SvTYPE(TARG) == SVt_PVLV)
1421 LvTARG(TARG) = NULL;
1428 (void)hv_iterinit(keys); /* always reset iterator regardless */
1430 if (gimme == G_VOID)
1433 if (gimme == G_SCALAR) {
1437 if (PL_op->op_flags & OPf_MOD || LVRET) { /* lvalue */
1438 if (SvTYPE(TARG) < SVt_PVLV) {
1439 sv_upgrade(TARG, SVt_PVLV);
1440 sv_magic(TARG, NULL, PERL_MAGIC_nkeys, NULL, 0);
1443 if (LvTARG(TARG) != (SV*)keys) {
1445 SvREFCNT_dec(LvTARG(TARG));
1446 LvTARG(TARG) = SvREFCNT_inc_simple(keys);
1452 if (! SvTIED_mg((SV*)keys, PERL_MAGIC_tied) )
1458 while (hv_iternext(keys)) i++;
1464 EXTEND(SP, HvKEYS(keys) * (dokeys + dovalues));
1466 PUTBACK; /* hv_iternext and hv_iterval might clobber stack_sp */
1467 while ((entry = hv_iternext(keys))) {
1470 SV* const sv = hv_iterkeysv(entry);
1471 XPUSHs(sv); /* won't clobber stack_sp */
1476 tmpstr = hv_iterval(hv,entry);
1477 DEBUG_H(Perl_sv_setpvf(aTHX_ tmpstr, "%lu%%%d=%lu",
1478 (unsigned long)HeHASH(entry),
1480 (unsigned long)(HeHASH(entry) & HvMAX(keys))));
1491 * c-indentation-style: bsd
1493 * indent-tabs-mode: t
1496 * ex: set ts=8 sts=4 sw=4 noet: