3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2004, 2005, 2006, 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;
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);
741 uoffset = offset*size; /* turn into bit offset */
742 len = (uoffset + size + 7) / 8; /* required number of bytes */
747 uoffset >>= 3; /* turn into byte offset */
749 if (uoffset >= srclen)
752 retnum = (UV) s[uoffset] << 8;
754 else if (size == 32) {
755 if (uoffset >= srclen)
757 else if (uoffset + 1 >= srclen)
759 ((UV) s[uoffset ] << 24);
760 else if (uoffset + 2 >= srclen)
762 ((UV) s[uoffset ] << 24) +
763 ((UV) s[uoffset + 1] << 16);
766 ((UV) s[uoffset ] << 24) +
767 ((UV) s[uoffset + 1] << 16) +
768 ( s[uoffset + 2] << 8);
771 else if (size == 64) {
772 if (ckWARN(WARN_PORTABLE))
773 Perl_warner(aTHX_ packWARN(WARN_PORTABLE),
774 "Bit vector size > 32 non-portable");
775 if (uoffset >= srclen)
777 else if (uoffset + 1 >= srclen)
779 (UV) s[uoffset ] << 56;
780 else if (uoffset + 2 >= srclen)
782 ((UV) s[uoffset ] << 56) +
783 ((UV) s[uoffset + 1] << 48);
784 else if (uoffset + 3 >= srclen)
786 ((UV) s[uoffset ] << 56) +
787 ((UV) s[uoffset + 1] << 48) +
788 ((UV) s[uoffset + 2] << 40);
789 else if (uoffset + 4 >= srclen)
791 ((UV) s[uoffset ] << 56) +
792 ((UV) s[uoffset + 1] << 48) +
793 ((UV) s[uoffset + 2] << 40) +
794 ((UV) s[uoffset + 3] << 32);
795 else if (uoffset + 5 >= srclen)
797 ((UV) s[uoffset ] << 56) +
798 ((UV) s[uoffset + 1] << 48) +
799 ((UV) s[uoffset + 2] << 40) +
800 ((UV) s[uoffset + 3] << 32) +
801 ( s[uoffset + 4] << 24);
802 else if (uoffset + 6 >= 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 ((UV) s[uoffset + 4] << 24) +
809 ((UV) s[uoffset + 5] << 16);
812 ((UV) s[uoffset ] << 56) +
813 ((UV) s[uoffset + 1] << 48) +
814 ((UV) s[uoffset + 2] << 40) +
815 ((UV) s[uoffset + 3] << 32) +
816 ((UV) s[uoffset + 4] << 24) +
817 ((UV) s[uoffset + 5] << 16) +
818 ( s[uoffset + 6] << 8);
824 retnum = (s[uoffset >> 3] >> (uoffset & 7)) & ((1 << size) - 1);
826 uoffset >>= 3; /* turn into byte offset */
831 ((UV) s[uoffset] << 8) +
835 ((UV) s[uoffset ] << 24) +
836 ((UV) s[uoffset + 1] << 16) +
837 ( s[uoffset + 2] << 8) +
840 else if (size == 64) {
841 if (ckWARN(WARN_PORTABLE))
842 Perl_warner(aTHX_ packWARN(WARN_PORTABLE),
843 "Bit vector size > 32 non-portable");
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 ( s[uoffset + 6] << 8) +
860 /* currently converts input to bytes if possible but doesn't sweat failures,
861 * although it does ensure that the string it clobbers is not marked as
862 * utf8-valid any more
865 Perl_do_vecset(pTHX_ SV *sv)
870 register unsigned char *s;
875 SV * const targ = LvTARG(sv);
879 s = (unsigned char*)SvPV_force(targ, targlen);
881 /* This is handled by the SvPOK_only below...
882 if (!Perl_sv_utf8_downgrade(aTHX_ targ, TRUE))
885 (void) Perl_sv_utf8_downgrade(aTHX_ targ, TRUE);
888 (void)SvPOK_only(targ);
890 offset = LvTARGOFF(sv);
892 Perl_croak(aTHX_ "Negative offset to vec in lvalue context");
893 size = LvTARGLEN(sv);
894 if (size < 1 || (size & (size-1))) /* size < 1 or not a power of two */
895 Perl_croak(aTHX_ "Illegal number of bits in vec");
897 offset *= size; /* turn into bit offset */
898 len = (offset + size + 7) / 8; /* required number of bytes */
900 s = (unsigned char*)SvGROW(targ, len + 1);
901 (void)memzero((char *)(s + targlen), len - targlen + 1);
902 SvCUR_set(targ, len);
906 mask = (1 << size) - 1;
909 offset >>= 3; /* turn into byte offset */
910 s[offset] &= ~(mask << size);
911 s[offset] |= lval << size;
914 offset >>= 3; /* turn into byte offset */
916 s[offset ] = (U8)( lval & 0xff);
917 else if (size == 16) {
918 s[offset ] = (U8)((lval >> 8) & 0xff);
919 s[offset+1] = (U8)( lval & 0xff);
921 else if (size == 32) {
922 s[offset ] = (U8)((lval >> 24) & 0xff);
923 s[offset+1] = (U8)((lval >> 16) & 0xff);
924 s[offset+2] = (U8)((lval >> 8) & 0xff);
925 s[offset+3] = (U8)( lval & 0xff);
928 else if (size == 64) {
929 if (ckWARN(WARN_PORTABLE))
930 Perl_warner(aTHX_ packWARN(WARN_PORTABLE),
931 "Bit vector size > 32 non-portable");
932 s[offset ] = (U8)((lval >> 56) & 0xff);
933 s[offset+1] = (U8)((lval >> 48) & 0xff);
934 s[offset+2] = (U8)((lval >> 40) & 0xff);
935 s[offset+3] = (U8)((lval >> 32) & 0xff);
936 s[offset+4] = (U8)((lval >> 24) & 0xff);
937 s[offset+5] = (U8)((lval >> 16) & 0xff);
938 s[offset+6] = (U8)((lval >> 8) & 0xff);
939 s[offset+7] = (U8)( lval & 0xff);
947 Perl_do_chop(pTHX_ register SV *astr, register SV *sv)
953 if (SvTYPE(sv) == SVt_PVAV) {
955 AV* const av = (AV*)sv;
956 const I32 max = AvFILL(av);
958 for (i = 0; i <= max; i++) {
959 sv = (SV*)av_fetch(av, i, FALSE);
960 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
965 else if (SvTYPE(sv) == SVt_PVHV) {
966 HV* const hv = (HV*)sv;
968 (void)hv_iterinit(hv);
969 while ((entry = hv_iternext(hv)))
970 do_chop(astr,hv_iterval(hv,entry));
973 else if (SvREADONLY(sv)) {
975 /* SV is copy-on-write */
976 sv_force_normal_flags(sv, 0);
979 Perl_croak(aTHX_ PL_no_modify);
982 if (PL_encoding && !SvUTF8(sv)) {
983 /* like in do_chomp(), utf8-ize the sv as a side-effect
984 * if we're using encoding. */
985 sv_recode_to_utf8(sv, PL_encoding);
989 if (len && !SvPOK(sv))
990 s = SvPV_force(sv, len);
993 char * const send = s + len;
994 char * const start = s;
996 while (s > start && UTF8_IS_CONTINUATION(*s))
998 if (is_utf8_string((U8*)s, send - s)) {
999 sv_setpvn(astr, s, send - s);
1001 SvCUR_set(sv, s - start);
1007 sv_setpvn(astr, "", 0);
1009 else if (s && len) {
1011 sv_setpvn(astr, s, 1);
1018 sv_setpvn(astr, "", 0);
1023 Perl_do_chomp(pTHX_ register SV *sv)
1029 char *temp_buffer = NULL;
1030 SV* svrecode = NULL;
1034 if (RsRECORD(PL_rs))
1037 if (SvTYPE(sv) == SVt_PVAV) {
1039 AV* const av = (AV*)sv;
1040 const I32 max = AvFILL(av);
1042 for (i = 0; i <= max; i++) {
1043 sv = (SV*)av_fetch(av, i, FALSE);
1044 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
1045 count += do_chomp(sv);
1049 else if (SvTYPE(sv) == SVt_PVHV) {
1050 HV* const hv = (HV*)sv;
1052 (void)hv_iterinit(hv);
1053 while ((entry = hv_iternext(hv)))
1054 count += do_chomp(hv_iterval(hv,entry));
1057 else if (SvREADONLY(sv)) {
1059 /* SV is copy-on-write */
1060 sv_force_normal_flags(sv, 0);
1063 Perl_croak(aTHX_ PL_no_modify);
1068 /* XXX, here sv is utf8-ized as a side-effect!
1069 If encoding.pm is used properly, almost string-generating
1070 operations, including literal strings, chr(), input data, etc.
1071 should have been utf8-ized already, right?
1073 sv_recode_to_utf8(sv, PL_encoding);
1080 if (RsPARA(PL_rs)) {
1084 while (len && s[-1] == '\n') {
1091 STRLEN rslen, rs_charlen;
1092 const char *rsptr = SvPV_const(PL_rs, rslen);
1094 rs_charlen = SvUTF8(PL_rs)
1095 ? sv_len_utf8(PL_rs)
1098 if (SvUTF8(PL_rs) != SvUTF8(sv)) {
1099 /* Assumption is that rs is shorter than the scalar. */
1100 if (SvUTF8(PL_rs)) {
1101 /* RS is utf8, scalar is 8 bit. */
1102 bool is_utf8 = TRUE;
1103 temp_buffer = (char*)bytes_from_utf8((U8*)rsptr,
1106 /* Cannot downgrade, therefore cannot possibly match
1108 assert (temp_buffer == rsptr);
1112 rsptr = temp_buffer;
1114 else if (PL_encoding) {
1115 /* RS is 8 bit, encoding.pm is used.
1116 * Do not recode PL_rs as a side-effect. */
1117 svrecode = newSVpvn(rsptr, rslen);
1118 sv_recode_to_utf8(svrecode, PL_encoding);
1119 rsptr = SvPV_const(svrecode, rslen);
1120 rs_charlen = sv_len_utf8(svrecode);
1123 /* RS is 8 bit, scalar is utf8. */
1124 temp_buffer = (char*)bytes_to_utf8((U8*)rsptr, &rslen);
1125 rsptr = temp_buffer;
1134 if (len < rslen - 1)
1138 if (memNE(s, rsptr, rslen))
1140 count += rs_charlen;
1143 s = SvPV_force_nolen(sv);
1152 SvREFCNT_dec(svrecode);
1154 Safefree(temp_buffer);
1159 Perl_do_vop(pTHX_ I32 optype, SV *sv, SV *left, SV *right)
1170 register const char *lc;
1171 register const char *rc;
1172 register STRLEN len;
1181 if (sv != left || (optype != OP_BIT_AND && !SvOK(sv) && !SvGMAGICAL(sv)))
1182 sv_setpvn(sv, "", 0); /* avoid undef warning on |= and ^= */
1183 lsave = lc = SvPV_nomg_const(left, leftlen);
1184 rsave = rc = SvPV_nomg_const(right, rightlen);
1186 /* This need to come after SvPV to ensure that string overloading has
1189 left_utf = DO_UTF8(left);
1190 right_utf = DO_UTF8(right);
1192 if (left_utf && !right_utf) {
1193 /* Avoid triggering overloading again by using temporaries.
1194 Maybe there should be a variant of sv_utf8_upgrade that takes pvn
1196 right = sv_2mortal(newSVpvn(rsave, rightlen));
1197 sv_utf8_upgrade(right);
1198 rsave = rc = SvPV_nomg_const(right, rightlen);
1201 else if (!left_utf && right_utf) {
1202 left = sv_2mortal(newSVpvn(lsave, leftlen));
1203 sv_utf8_upgrade(left);
1204 lsave = lc = SvPV_nomg_const(left, leftlen);
1208 len = leftlen < rightlen ? leftlen : rightlen;
1211 (void)SvPOK_only(sv);
1212 if ((left_utf || right_utf) && (sv == left || sv == right)) {
1213 needlen = optype == OP_BIT_AND ? len : leftlen + rightlen;
1214 Newxz(dc, needlen + 1, char);
1216 else if (SvOK(sv) || SvTYPE(sv) > SVt_PVMG) {
1217 dc = SvPV_force_nomg_nolen(sv);
1218 if (SvLEN(sv) < len + 1) {
1219 dc = SvGROW(sv, len + 1);
1220 (void)memzero(dc + SvCUR(sv), len - SvCUR(sv) + 1);
1222 if (optype != OP_BIT_AND && (left_utf || right_utf))
1223 dc = SvGROW(sv, leftlen + rightlen + 1);
1226 needlen = optype == OP_BIT_AND
1227 ? len : (leftlen > rightlen ? leftlen : rightlen);
1228 Newxz(dc, needlen + 1, char);
1229 sv_usepvn_flags(sv, dc, needlen, SV_HAS_TRAILING_NUL);
1230 dc = SvPVX(sv); /* sv_usepvn() calls Renew() */
1232 if (left_utf || right_utf) {
1235 char *dcsave = NULL;
1236 STRLEN lulen = leftlen;
1237 STRLEN rulen = rightlen;
1242 while (lulen && rulen) {
1243 luc = utf8n_to_uvchr((U8*)lc, lulen, &ulen, UTF8_ALLOW_ANYUV);
1246 ruc = utf8n_to_uvchr((U8*)rc, rulen, &ulen, UTF8_ALLOW_ANYUV);
1250 dc = (char*)uvchr_to_utf8((U8*)dc, duc);
1252 if (sv == left || sv == right)
1253 (void)sv_usepvn(sv, dcorig, needlen);
1254 SvCUR_set(sv, dc - dcorig);
1257 while (lulen && rulen) {
1258 luc = utf8n_to_uvchr((U8*)lc, lulen, &ulen, UTF8_ALLOW_ANYUV);
1261 ruc = utf8n_to_uvchr((U8*)rc, rulen, &ulen, UTF8_ALLOW_ANYUV);
1265 dc = (char*)uvchr_to_utf8((U8*)dc, duc);
1269 while (lulen && rulen) {
1270 luc = utf8n_to_uvchr((U8*)lc, lulen, &ulen, UTF8_ALLOW_ANYUV);
1273 ruc = utf8n_to_uvchr((U8*)rc, rulen, &ulen, UTF8_ALLOW_ANYUV);
1277 dc = (char*)uvchr_to_utf8((U8*)dc, duc);
1281 dcsave = savepvn(rc, rulen);
1283 dcsave = savepvn(lc, lulen);
1284 if (sv == left || sv == right)
1285 (void)sv_usepvn(sv, dcorig, needlen); /* Uses Renew(). */
1286 SvCUR_set(sv, dc - dcorig);
1288 sv_catpvn(sv, dcsave, rulen);
1290 sv_catpvn(sv, dcsave, lulen);
1296 if (sv == left || sv == right)
1298 Perl_croak(aTHX_ "panic: do_vop called for op %u (%s)",
1299 (unsigned)optype, PL_op_name[optype]);
1306 if (len >= sizeof(long)*4 &&
1307 !((unsigned long)dc % sizeof(long)) &&
1308 !((unsigned long)lc % sizeof(long)) &&
1309 !((unsigned long)rc % sizeof(long))) /* It's almost always aligned... */
1311 const STRLEN remainder = len % (sizeof(long)*4);
1312 len /= (sizeof(long)*4);
1321 *dl++ = *ll++ & *rl++;
1322 *dl++ = *ll++ & *rl++;
1323 *dl++ = *ll++ & *rl++;
1324 *dl++ = *ll++ & *rl++;
1329 *dl++ = *ll++ ^ *rl++;
1330 *dl++ = *ll++ ^ *rl++;
1331 *dl++ = *ll++ ^ *rl++;
1332 *dl++ = *ll++ ^ *rl++;
1337 *dl++ = *ll++ | *rl++;
1338 *dl++ = *ll++ | *rl++;
1339 *dl++ = *ll++ | *rl++;
1340 *dl++ = *ll++ | *rl++;
1355 *dc++ = *lc++ & *rc++;
1360 *dc++ = *lc++ ^ *rc++;
1364 *dc++ = *lc++ | *rc++;
1368 sv_catpvn(sv, rsave + len, rightlen - len);
1369 else if (leftlen > (STRLEN)len)
1370 sv_catpvn(sv, lsave + len, leftlen - len);
1385 HV * const hv = (HV*)POPs;
1388 const I32 gimme = GIMME_V;
1389 const I32 dokv = (PL_op->op_type == OP_RV2HV || PL_op->op_type == OP_PADHV);
1390 const I32 dokeys = dokv || (PL_op->op_type == OP_KEYS);
1391 const I32 dovalues = dokv || (PL_op->op_type == OP_VALUES);
1394 if (PL_op->op_flags & OPf_MOD || LVRET) { /* lvalue */
1395 dTARGET; /* make sure to clear its target here */
1396 if (SvTYPE(TARG) == SVt_PVLV)
1397 LvTARG(TARG) = NULL;
1404 (void)hv_iterinit(keys); /* always reset iterator regardless */
1406 if (gimme == G_VOID)
1409 if (gimme == G_SCALAR) {
1413 if (PL_op->op_flags & OPf_MOD || LVRET) { /* lvalue */
1414 if (SvTYPE(TARG) < SVt_PVLV) {
1415 sv_upgrade(TARG, SVt_PVLV);
1416 sv_magic(TARG, NULL, PERL_MAGIC_nkeys, NULL, 0);
1419 if (LvTARG(TARG) != (SV*)keys) {
1421 SvREFCNT_dec(LvTARG(TARG));
1422 LvTARG(TARG) = SvREFCNT_inc_simple(keys);
1428 if (! SvTIED_mg((SV*)keys, PERL_MAGIC_tied))
1432 while (hv_iternext(keys)) i++;
1438 EXTEND(SP, HvKEYS(keys) * (dokeys + dovalues));
1440 PUTBACK; /* hv_iternext and hv_iterval might clobber stack_sp */
1441 while ((entry = hv_iternext(keys))) {
1444 SV* const sv = hv_iterkeysv(entry);
1445 XPUSHs(sv); /* won't clobber stack_sp */
1450 tmpstr = hv_iterval(hv,entry);
1451 DEBUG_H(Perl_sv_setpvf(aTHX_ tmpstr, "%lu%%%d=%lu",
1452 (unsigned long)HeHASH(entry),
1454 (unsigned long)(HeHASH(entry) & HvMAX(keys))));
1465 * c-indentation-style: bsd
1467 * indent-tabs-mode: t
1470 * ex: set ts=8 sts=4 sw=4 noet: