3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4 * 2001, 2002, 2004, 2005, 2006, 2007, 2008, 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_schomp() and pp_chomp() - scalar and array
19 * chomp operations - call the function do_chomp() found in this file.
23 #define PERL_IN_DOOP_C
31 S_do_trans_simple(pTHX_ SV * const sv)
36 U8 *s = (U8*)SvPV(sv,len);
37 U8 * const send = s+len;
38 const short * const tbl = (short*)cPVOP->op_pv;
40 PERL_ARGS_ASSERT_DO_TRANS_SIMPLE;
43 Perl_croak(aTHX_ "panic: do_trans_simple line %d",__LINE__);
45 /* First, take care of non-UTF-8 input strings, because they're easy */
48 const I32 ch = tbl[*s];
58 const I32 grows = PL_op->op_private & OPpTRANS_GROWS;
62 /* Allow for expansion: $_="a".chr(400); tr/a/\xFE/, FE needs encoding */
72 /* Need to check this, otherwise 128..255 won't match */
73 const UV c = utf8n_to_uvchr(s, send - s, &ulen, UTF8_ALLOW_DEFAULT);
74 if (c < 0x100 && (ch = tbl[c]) >= 0) {
76 d = uvchr_to_utf8(d, ch);
79 else { /* No match -> copy */
86 sv_setpvn(sv, (char*)dstart, d - dstart);
91 SvCUR_set(sv, d - dstart);
100 S_do_trans_count(pTHX_ SV * const sv)
104 const U8 *s = (const U8*)SvPV_const(sv, len);
105 const U8 * const send = s + len;
107 const short * const tbl = (short*)cPVOP->op_pv;
109 PERL_ARGS_ASSERT_DO_TRANS_COUNT;
112 Perl_croak(aTHX_ "panic: do_trans_count line %d",__LINE__);
121 const I32 complement = PL_op->op_private & OPpTRANS_COMPLEMENT;
124 const UV c = utf8n_to_uvchr(s, send - s, &ulen, UTF8_ALLOW_DEFAULT);
128 } else if (complement)
138 S_do_trans_complex(pTHX_ SV * const sv)
142 U8 *s = (U8*)SvPV(sv, len);
143 U8 * const send = s+len;
145 const short * const tbl = (short*)cPVOP->op_pv;
147 PERL_ARGS_ASSERT_DO_TRANS_COMPLEX;
150 Perl_croak(aTHX_ "panic: do_trans_complex line %d",__LINE__);
154 U8 * const dstart = d;
156 if (PL_op->op_private & OPpTRANS_SQUASH) {
159 const I32 ch = tbl[*s];
163 if (p != d - 1 || *p != *d)
166 else if (ch == -1) /* -1 is unmapped character */
168 else if (ch == -2) /* -2 is delete character */
175 const I32 ch = tbl[*s];
180 else if (ch == -1) /* -1 is unmapped character */
182 else if (ch == -2) /* -2 is delete character */
188 SvCUR_set(sv, d - dstart);
191 const I32 complement = PL_op->op_private & OPpTRANS_COMPLEMENT;
192 const I32 grows = PL_op->op_private & OPpTRANS_GROWS;
193 const I32 del = PL_op->op_private & OPpTRANS_DELETE;
199 Newx(d, len*2+1, U8);
203 if (complement && !del)
206 #ifdef MACOS_TRADITIONAL
207 #define comp CoMP /* "comp" is a keyword in some compilers ... */
210 if (PL_op->op_private & OPpTRANS_SQUASH) {
214 const UV comp = utf8n_to_uvchr(s, send - s, &len,
226 ch = (rlen == 0) ? (I32)comp :
227 (comp - 0x100 < rlen) ?
228 tbl[comp+1] : tbl[0x100+rlen];
230 d = uvchr_to_utf8(d, ch);
238 else if ((ch = tbl[comp]) >= 0) {
241 d = uvchr_to_utf8(d, ch);
247 else if (ch == -1) { /* -1 is unmapped character */
251 else if (ch == -2) /* -2 is delete character */
260 const UV comp = utf8n_to_uvchr(s, send - s, &len,
271 if (comp - 0x100 < rlen)
272 d = uvchr_to_utf8(d, tbl[comp+1]);
274 d = uvchr_to_utf8(d, tbl[0x100+rlen]);
278 else if ((ch = tbl[comp]) >= 0) {
279 d = uvchr_to_utf8(d, ch);
282 else if (ch == -1) { /* -1 is unmapped character */
286 else if (ch == -2) /* -2 is delete character */
292 sv_setpvn(sv, (char*)dstart, d - dstart);
297 SvCUR_set(sv, d - dstart);
306 S_do_trans_simple_utf8(pTHX_ SV * const sv)
315 const I32 grows = PL_op->op_private & OPpTRANS_GROWS;
319 PAD_SVl(cPADOP->op_padix);
321 MUTABLE_SV(cSVOP->op_sv);
323 HV* const hv = MUTABLE_HV(SvRV(rv));
324 SV* const * svp = hv_fetchs(hv, "NONE", FALSE);
325 const UV none = svp ? SvUV(*svp) : 0x7fffffff;
326 const UV extra = none + 1;
330 PERL_ARGS_ASSERT_DO_TRANS_SIMPLE_UTF8;
332 s = (U8*)SvPV(sv, len);
335 const U8 * const e = s + len;
338 hibit = !NATIVE_IS_INVARIANT(ch);
340 s = bytes_to_utf8(s, &len);
348 svp = hv_fetchs(hv, "FINAL", FALSE);
353 /* d needs to be bigger than s, in case e.g. upgrading is required */
354 Newx(d, len * 3 + UTF8_MAXBYTES, U8);
364 const UV uv = swash_fetch(rv, s, TRUE);
368 d = uvuni_to_utf8(d, uv);
370 else if (uv == none) {
371 const int i = UTF8SKIP(s);
376 else if (uv == extra) {
379 d = uvuni_to_utf8(d, final);
385 const STRLEN clen = d - dstart;
386 const STRLEN nlen = dend - dstart + len + UTF8_MAXBYTES;
388 Perl_croak(aTHX_ "panic: do_trans_simple_utf8 line %d",__LINE__);
389 Renew(dstart, nlen + UTF8_MAXBYTES, U8);
391 dend = dstart + nlen;
394 if (grows || hibit) {
395 sv_setpvn(sv, (char*)dstart, d - dstart);
402 SvCUR_set(sv, d - dstart);
411 S_do_trans_count_utf8(pTHX_ SV * const sv)
415 const U8 *start = NULL;
421 PAD_SVl(cPADOP->op_padix);
423 MUTABLE_SV(cSVOP->op_sv);
425 HV* const hv = MUTABLE_HV(SvRV(rv));
426 SV* const * const svp = hv_fetchs(hv, "NONE", FALSE);
427 const UV none = svp ? SvUV(*svp) : 0x7fffffff;
428 const UV extra = none + 1;
431 PERL_ARGS_ASSERT_DO_TRANS_COUNT_UTF8;
433 s = (const U8*)SvPV_const(sv, len);
436 const U8 * const e = s + len;
439 hibit = !NATIVE_IS_INVARIANT(ch);
441 start = s = bytes_to_utf8(s, &len);
449 const UV uv = swash_fetch(rv, s, TRUE);
450 if (uv < none || uv == extra)
461 S_do_trans_complex_utf8(pTHX_ SV * const sv)
467 const I32 squash = PL_op->op_private & OPpTRANS_SQUASH;
468 const I32 del = PL_op->op_private & OPpTRANS_DELETE;
469 const I32 grows = PL_op->op_private & OPpTRANS_GROWS;
472 PAD_SVl(cPADOP->op_padix);
474 MUTABLE_SV(cSVOP->op_sv);
476 HV * const hv = MUTABLE_HV(SvRV(rv));
477 SV * const *svp = hv_fetchs(hv, "NONE", FALSE);
478 const UV none = svp ? SvUV(*svp) : 0x7fffffff;
479 const UV extra = none + 1;
481 bool havefinal = FALSE;
485 U8 *s = (U8*)SvPV(sv, len);
487 PERL_ARGS_ASSERT_DO_TRANS_COMPLEX_UTF8;
491 const U8 * const e = s + len;
494 hibit = !NATIVE_IS_INVARIANT(ch);
496 s = bytes_to_utf8(s, &len);
504 svp = hv_fetchs(hv, "FINAL", FALSE);
511 /* d needs to be bigger than s, in case e.g. upgrading is required */
512 Newx(d, len * 3 + UTF8_MAXBYTES, U8);
524 UV uv = swash_fetch(rv, s, TRUE);
527 const STRLEN clen = d - dstart;
528 const STRLEN nlen = dend - dstart + len + UTF8_MAXBYTES;
530 Perl_croak(aTHX_ "panic: do_trans_complex_utf8 line %d",__LINE__);
531 Renew(dstart, nlen + UTF8_MAXBYTES, U8);
533 dend = dstart + nlen;
539 d = uvuni_to_utf8(d, uv);
544 else if (uv == none) { /* "none" is unmapped character */
545 const int i = UTF8SKIP(s);
552 else if (uv == extra && !del) {
557 d = uvuni_to_utf8(d, final);
563 uv = utf8n_to_uvuni(s, send - s, &len, UTF8_ALLOW_DEFAULT);
573 matches++; /* "none+1" is delete character */
579 const UV uv = swash_fetch(rv, s, TRUE);
581 const STRLEN clen = d - dstart;
582 const STRLEN nlen = dend - dstart + len + UTF8_MAXBYTES;
584 Perl_croak(aTHX_ "panic: do_trans_complex_utf8 line %d",__LINE__);
585 Renew(dstart, nlen + UTF8_MAXBYTES, U8);
587 dend = dstart + nlen;
592 d = uvuni_to_utf8(d, uv);
595 else if (uv == none) { /* "none" is unmapped character */
596 const int i = UTF8SKIP(s);
602 else if (uv == extra && !del) {
605 d = uvuni_to_utf8(d, final);
608 matches++; /* "none+1" is delete character */
612 if (grows || hibit) {
613 sv_setpvn(sv, (char*)dstart, d - dstart);
620 SvCUR_set(sv, d - dstart);
629 Perl_do_trans(pTHX_ SV *sv)
633 const I32 hasutf = (PL_op->op_private &
634 (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF));
636 PERL_ARGS_ASSERT_DO_TRANS;
638 if (SvREADONLY(sv) && !(PL_op->op_private & OPpTRANS_IDENTICAL)) {
640 sv_force_normal_flags(sv, 0);
642 Perl_croak(aTHX_ "%s", PL_no_modify);
644 (void)SvPV_const(sv, len);
647 if (!(PL_op->op_private & OPpTRANS_IDENTICAL)) {
649 (void)SvPV_force(sv, len);
650 (void)SvPOK_only_UTF8(sv);
653 DEBUG_t( Perl_deb(aTHX_ "2.TBL\n"));
655 switch (PL_op->op_private & ~hasutf & (
656 OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF|OPpTRANS_IDENTICAL|
657 OPpTRANS_SQUASH|OPpTRANS_DELETE|OPpTRANS_COMPLEMENT)) {
660 return do_trans_simple_utf8(sv);
662 return do_trans_simple(sv);
664 case OPpTRANS_IDENTICAL:
665 case OPpTRANS_IDENTICAL|OPpTRANS_COMPLEMENT:
667 return do_trans_count_utf8(sv);
669 return do_trans_count(sv);
673 return do_trans_complex_utf8(sv);
675 return do_trans_complex(sv);
680 Perl_do_join(pTHX_ register SV *sv, SV *delim, register SV **mark, register SV **sp)
683 SV ** const oldmark = mark;
684 register I32 items = sp - mark;
688 PERL_ARGS_ASSERT_DO_JOIN;
690 (void) SvPV_const(delim, delimlen); /* stringify and get the delimlen */
691 /* SvCUR assumes it's SvPOK() and woe betide you if it's not. */
694 len = (items > 0 ? (delimlen * (items - 1) ) : 0);
695 SvUPGRADE(sv, SVt_PV);
696 if (SvLEN(sv) < len + items) { /* current length is way too short */
697 while (items-- > 0) {
698 if (*mark && !SvGAMAGIC(*mark) && SvOK(*mark)) {
700 SvPV_const(*mark, tmplen);
705 SvGROW(sv, len + 1); /* so try to pre-extend */
713 /* sv_setpv retains old UTF8ness [perl #24846] */
716 if (PL_tainting && SvMAGICAL(sv))
726 for (; items > 0; items--,mark++) {
732 for (; items > 0; items--,mark++)
739 Perl_do_sprintf(pTHX_ SV *sv, I32 len, SV **sarg)
743 const char * const pat = SvPV_const(*sarg, patlen);
744 bool do_taint = FALSE;
746 PERL_ARGS_ASSERT_DO_SPRINTF;
751 sv_vsetpvfn(sv, pat, patlen, NULL, sarg + 1, len - 1, &do_taint);
757 /* currently converts input to bytes if possible, but doesn't sweat failure */
759 Perl_do_vecget(pTHX_ SV *sv, I32 offset, I32 size)
762 STRLEN srclen, len, uoffset, bitoffs = 0;
763 const unsigned char *s = (const unsigned char *) SvPV_const(sv, srclen);
766 PERL_ARGS_ASSERT_DO_VECGET;
770 if (size < 1 || (size & (size-1))) /* size < 1 or not a power of two */
771 Perl_croak(aTHX_ "Illegal number of bits in vec");
774 (void) Perl_sv_utf8_downgrade(aTHX_ sv, TRUE);
777 bitoffs = ((offset%8)*size)%8;
778 uoffset = offset/(8/size);
781 uoffset = offset*(size/8);
785 len = uoffset + (bitoffs + size + 7)/8; /* required number of bytes */
791 if (uoffset >= srclen)
794 retnum = (UV) s[uoffset] << 8;
796 else if (size == 32) {
797 if (uoffset >= srclen)
799 else if (uoffset + 1 >= srclen)
801 ((UV) s[uoffset ] << 24);
802 else if (uoffset + 2 >= srclen)
804 ((UV) s[uoffset ] << 24) +
805 ((UV) s[uoffset + 1] << 16);
808 ((UV) s[uoffset ] << 24) +
809 ((UV) s[uoffset + 1] << 16) +
810 ( s[uoffset + 2] << 8);
813 else if (size == 64) {
814 if (ckWARN(WARN_PORTABLE))
815 Perl_warner(aTHX_ packWARN(WARN_PORTABLE),
816 "Bit vector size > 32 non-portable");
817 if (uoffset >= srclen)
819 else if (uoffset + 1 >= srclen)
821 (UV) s[uoffset ] << 56;
822 else if (uoffset + 2 >= srclen)
824 ((UV) s[uoffset ] << 56) +
825 ((UV) s[uoffset + 1] << 48);
826 else if (uoffset + 3 >= srclen)
828 ((UV) s[uoffset ] << 56) +
829 ((UV) s[uoffset + 1] << 48) +
830 ((UV) s[uoffset + 2] << 40);
831 else if (uoffset + 4 >= srclen)
833 ((UV) s[uoffset ] << 56) +
834 ((UV) s[uoffset + 1] << 48) +
835 ((UV) s[uoffset + 2] << 40) +
836 ((UV) s[uoffset + 3] << 32);
837 else if (uoffset + 5 >= srclen)
839 ((UV) s[uoffset ] << 56) +
840 ((UV) s[uoffset + 1] << 48) +
841 ((UV) s[uoffset + 2] << 40) +
842 ((UV) s[uoffset + 3] << 32) +
843 ( s[uoffset + 4] << 24);
844 else if (uoffset + 6 >= srclen)
846 ((UV) s[uoffset ] << 56) +
847 ((UV) s[uoffset + 1] << 48) +
848 ((UV) s[uoffset + 2] << 40) +
849 ((UV) s[uoffset + 3] << 32) +
850 ((UV) s[uoffset + 4] << 24) +
851 ((UV) s[uoffset + 5] << 16);
854 ((UV) s[uoffset ] << 56) +
855 ((UV) s[uoffset + 1] << 48) +
856 ((UV) s[uoffset + 2] << 40) +
857 ((UV) s[uoffset + 3] << 32) +
858 ((UV) s[uoffset + 4] << 24) +
859 ((UV) s[uoffset + 5] << 16) +
860 ( s[uoffset + 6] << 8);
866 retnum = (s[uoffset] >> bitoffs) & ((1 << size) - 1);
872 ((UV) s[uoffset] << 8) +
876 ((UV) s[uoffset ] << 24) +
877 ((UV) s[uoffset + 1] << 16) +
878 ( s[uoffset + 2] << 8) +
881 else if (size == 64) {
882 if (ckWARN(WARN_PORTABLE))
883 Perl_warner(aTHX_ packWARN(WARN_PORTABLE),
884 "Bit vector size > 32 non-portable");
886 ((UV) s[uoffset ] << 56) +
887 ((UV) s[uoffset + 1] << 48) +
888 ((UV) s[uoffset + 2] << 40) +
889 ((UV) s[uoffset + 3] << 32) +
890 ((UV) s[uoffset + 4] << 24) +
891 ((UV) s[uoffset + 5] << 16) +
892 ( s[uoffset + 6] << 8) +
901 /* currently converts input to bytes if possible but doesn't sweat failures,
902 * although it does ensure that the string it clobbers is not marked as
903 * utf8-valid any more
906 Perl_do_vecset(pTHX_ SV *sv)
909 register I32 offset, bitoffs = 0;
911 register unsigned char *s;
916 SV * const targ = LvTARG(sv);
918 PERL_ARGS_ASSERT_DO_VECSET;
922 s = (unsigned char*)SvPV_force(targ, targlen);
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);
935 Perl_croak(aTHX_ "Negative offset to vec in lvalue context");
936 size = LvTARGLEN(sv);
937 if (size < 1 || (size & (size-1))) /* size < 1 or not a power of two */
938 Perl_croak(aTHX_ "Illegal number of bits in vec");
941 bitoffs = ((offset%8)*size)%8;
947 len = offset + (bitoffs + size + 7)/8; /* required number of bytes */
949 s = (unsigned char*)SvGROW(targ, len + 1);
950 (void)memzero((char *)(s + targlen), len - targlen + 1);
951 SvCUR_set(targ, len);
955 mask = (1 << size) - 1;
957 s[offset] &= ~(mask << bitoffs);
958 s[offset] |= lval << bitoffs;
962 s[offset ] = (U8)( lval & 0xff);
963 else if (size == 16) {
964 s[offset ] = (U8)((lval >> 8) & 0xff);
965 s[offset+1] = (U8)( lval & 0xff);
967 else if (size == 32) {
968 s[offset ] = (U8)((lval >> 24) & 0xff);
969 s[offset+1] = (U8)((lval >> 16) & 0xff);
970 s[offset+2] = (U8)((lval >> 8) & 0xff);
971 s[offset+3] = (U8)( lval & 0xff);
974 else if (size == 64) {
975 if (ckWARN(WARN_PORTABLE))
976 Perl_warner(aTHX_ packWARN(WARN_PORTABLE),
977 "Bit vector size > 32 non-portable");
978 s[offset ] = (U8)((lval >> 56) & 0xff);
979 s[offset+1] = (U8)((lval >> 48) & 0xff);
980 s[offset+2] = (U8)((lval >> 40) & 0xff);
981 s[offset+3] = (U8)((lval >> 32) & 0xff);
982 s[offset+4] = (U8)((lval >> 24) & 0xff);
983 s[offset+5] = (U8)((lval >> 16) & 0xff);
984 s[offset+6] = (U8)((lval >> 8) & 0xff);
985 s[offset+7] = (U8)( lval & 0xff);
993 Perl_do_chop(pTHX_ register SV *astr, register SV *sv)
999 PERL_ARGS_ASSERT_DO_CHOP;
1001 if (SvTYPE(sv) == SVt_PVAV) {
1003 AV *const av = MUTABLE_AV(sv);
1004 const I32 max = AvFILL(av);
1006 for (i = 0; i <= max; i++) {
1007 sv = MUTABLE_SV(av_fetch(av, i, FALSE));
1008 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
1013 else if (SvTYPE(sv) == SVt_PVHV) {
1014 HV* const hv = MUTABLE_HV(sv);
1016 (void)hv_iterinit(hv);
1017 while ((entry = hv_iternext(hv)))
1018 do_chop(astr,hv_iterval(hv,entry));
1021 else if (SvREADONLY(sv)) {
1023 /* SV is copy-on-write */
1024 sv_force_normal_flags(sv, 0);
1027 Perl_croak(aTHX_ "%s", PL_no_modify);
1030 if (PL_encoding && !SvUTF8(sv)) {
1031 /* like in do_chomp(), utf8-ize the sv as a side-effect
1032 * if we're using encoding. */
1033 sv_recode_to_utf8(sv, PL_encoding);
1037 if (len && !SvPOK(sv))
1038 s = SvPV_force_nomg(sv, len);
1041 char * const send = s + len;
1042 char * const start = s;
1044 while (s > start && UTF8_IS_CONTINUATION(*s))
1046 if (is_utf8_string((U8*)s, send - s)) {
1047 sv_setpvn(astr, s, send - s);
1049 SvCUR_set(sv, s - start);
1055 sv_setpvs(astr, "");
1057 else if (s && len) {
1059 sv_setpvn(astr, s, 1);
1066 sv_setpvs(astr, "");
1071 Perl_do_chomp(pTHX_ register SV *sv)
1077 char *temp_buffer = NULL;
1078 SV* svrecode = NULL;
1080 PERL_ARGS_ASSERT_DO_CHOMP;
1084 if (RsRECORD(PL_rs))
1087 if (SvTYPE(sv) == SVt_PVAV) {
1089 AV *const av = MUTABLE_AV(sv);
1090 const I32 max = AvFILL(av);
1092 for (i = 0; i <= max; i++) {
1093 sv = MUTABLE_SV(av_fetch(av, i, FALSE));
1094 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
1095 count += do_chomp(sv);
1099 else if (SvTYPE(sv) == SVt_PVHV) {
1100 HV* const hv = MUTABLE_HV(sv);
1102 (void)hv_iterinit(hv);
1103 while ((entry = hv_iternext(hv)))
1104 count += do_chomp(hv_iterval(hv,entry));
1107 else if (SvREADONLY(sv)) {
1109 /* SV is copy-on-write */
1110 sv_force_normal_flags(sv, 0);
1113 Perl_croak(aTHX_ "%s", PL_no_modify);
1118 /* XXX, here sv is utf8-ized as a side-effect!
1119 If encoding.pm is used properly, almost string-generating
1120 operations, including literal strings, chr(), input data, etc.
1121 should have been utf8-ized already, right?
1123 sv_recode_to_utf8(sv, PL_encoding);
1130 if (RsPARA(PL_rs)) {
1134 while (len && s[-1] == '\n') {
1141 STRLEN rslen, rs_charlen;
1142 const char *rsptr = SvPV_const(PL_rs, rslen);
1144 rs_charlen = SvUTF8(PL_rs)
1145 ? sv_len_utf8(PL_rs)
1148 if (SvUTF8(PL_rs) != SvUTF8(sv)) {
1149 /* Assumption is that rs is shorter than the scalar. */
1150 if (SvUTF8(PL_rs)) {
1151 /* RS is utf8, scalar is 8 bit. */
1152 bool is_utf8 = TRUE;
1153 temp_buffer = (char*)bytes_from_utf8((U8*)rsptr,
1156 /* Cannot downgrade, therefore cannot possibly match
1158 assert (temp_buffer == rsptr);
1162 rsptr = temp_buffer;
1164 else if (PL_encoding) {
1165 /* RS is 8 bit, encoding.pm is used.
1166 * Do not recode PL_rs as a side-effect. */
1167 svrecode = newSVpvn(rsptr, rslen);
1168 sv_recode_to_utf8(svrecode, PL_encoding);
1169 rsptr = SvPV_const(svrecode, rslen);
1170 rs_charlen = sv_len_utf8(svrecode);
1173 /* RS is 8 bit, scalar is utf8. */
1174 temp_buffer = (char*)bytes_to_utf8((U8*)rsptr, &rslen);
1175 rsptr = temp_buffer;
1184 if (len < rslen - 1)
1188 if (memNE(s, rsptr, rslen))
1190 count += rs_charlen;
1193 s = SvPV_force_nolen(sv);
1202 SvREFCNT_dec(svrecode);
1204 Safefree(temp_buffer);
1209 Perl_do_vop(pTHX_ I32 optype, SV *sv, SV *left, SV *right)
1220 register const char *lc;
1221 register const char *rc;
1222 register STRLEN len;
1230 PERL_ARGS_ASSERT_DO_VOP;
1232 if (sv != left || (optype != OP_BIT_AND && !SvOK(sv) && !SvGMAGICAL(sv)))
1233 sv_setpvs(sv, ""); /* avoid undef warning on |= and ^= */
1234 lsave = lc = SvPV_nomg_const(left, leftlen);
1235 rsave = rc = SvPV_nomg_const(right, rightlen);
1237 /* This need to come after SvPV to ensure that string overloading has
1240 left_utf = DO_UTF8(left);
1241 right_utf = DO_UTF8(right);
1243 if (left_utf && !right_utf) {
1244 /* Avoid triggering overloading again by using temporaries.
1245 Maybe there should be a variant of sv_utf8_upgrade that takes pvn
1247 right = newSVpvn_flags(rsave, rightlen, SVs_TEMP);
1248 sv_utf8_upgrade(right);
1249 rsave = rc = SvPV_nomg_const(right, rightlen);
1252 else if (!left_utf && right_utf) {
1253 left = newSVpvn_flags(lsave, leftlen, SVs_TEMP);
1254 sv_utf8_upgrade(left);
1255 lsave = lc = SvPV_nomg_const(left, leftlen);
1259 len = leftlen < rightlen ? leftlen : rightlen;
1262 (void)SvPOK_only(sv);
1263 if ((left_utf || right_utf) && (sv == left || sv == right)) {
1264 needlen = optype == OP_BIT_AND ? len : leftlen + rightlen;
1265 Newxz(dc, needlen + 1, char);
1267 else if (SvOK(sv) || SvTYPE(sv) > SVt_PVMG) {
1268 dc = SvPV_force_nomg_nolen(sv);
1269 if (SvLEN(sv) < len + 1) {
1270 dc = SvGROW(sv, len + 1);
1271 (void)memzero(dc + SvCUR(sv), len - SvCUR(sv) + 1);
1273 if (optype != OP_BIT_AND && (left_utf || right_utf))
1274 dc = SvGROW(sv, leftlen + rightlen + 1);
1277 needlen = optype == OP_BIT_AND
1278 ? len : (leftlen > rightlen ? leftlen : rightlen);
1279 Newxz(dc, needlen + 1, char);
1280 sv_usepvn_flags(sv, dc, needlen, SV_HAS_TRAILING_NUL);
1281 dc = SvPVX(sv); /* sv_usepvn() calls Renew() */
1283 if (left_utf || right_utf) {
1286 char *dcsave = NULL;
1287 STRLEN lulen = leftlen;
1288 STRLEN rulen = rightlen;
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);
1303 if (sv == left || sv == right)
1304 (void)sv_usepvn(sv, dcorig, needlen);
1305 SvCUR_set(sv, dc - dcorig);
1308 while (lulen && rulen) {
1309 luc = utf8n_to_uvchr((U8*)lc, lulen, &ulen, UTF8_ALLOW_ANYUV);
1312 ruc = utf8n_to_uvchr((U8*)rc, rulen, &ulen, UTF8_ALLOW_ANYUV);
1316 dc = (char*)uvchr_to_utf8((U8*)dc, duc);
1320 while (lulen && rulen) {
1321 luc = utf8n_to_uvchr((U8*)lc, lulen, &ulen, UTF8_ALLOW_ANYUV);
1324 ruc = utf8n_to_uvchr((U8*)rc, rulen, &ulen, UTF8_ALLOW_ANYUV);
1328 dc = (char*)uvchr_to_utf8((U8*)dc, duc);
1332 dcsave = savepvn(rc, rulen);
1334 dcsave = savepvn(lc, lulen);
1335 if (sv == left || sv == right)
1336 (void)sv_usepvn(sv, dcorig, needlen); /* Uses Renew(). */
1337 SvCUR_set(sv, dc - dcorig);
1339 sv_catpvn(sv, dcsave, rulen);
1341 sv_catpvn(sv, dcsave, lulen);
1347 if (sv == left || sv == right)
1349 Perl_croak(aTHX_ "panic: do_vop called for op %u (%s)",
1350 (unsigned)optype, PL_op_name[optype]);
1357 if (len >= sizeof(long)*4 &&
1358 !((unsigned long)dc % sizeof(long)) &&
1359 !((unsigned long)lc % sizeof(long)) &&
1360 !((unsigned long)rc % sizeof(long))) /* It's almost always aligned... */
1362 const STRLEN remainder = len % (sizeof(long)*4);
1363 len /= (sizeof(long)*4);
1372 *dl++ = *ll++ & *rl++;
1373 *dl++ = *ll++ & *rl++;
1374 *dl++ = *ll++ & *rl++;
1375 *dl++ = *ll++ & *rl++;
1380 *dl++ = *ll++ ^ *rl++;
1381 *dl++ = *ll++ ^ *rl++;
1382 *dl++ = *ll++ ^ *rl++;
1383 *dl++ = *ll++ ^ *rl++;
1388 *dl++ = *ll++ | *rl++;
1389 *dl++ = *ll++ | *rl++;
1390 *dl++ = *ll++ | *rl++;
1391 *dl++ = *ll++ | *rl++;
1406 *dc++ = *lc++ & *rc++;
1411 *dc++ = *lc++ ^ *rc++;
1415 *dc++ = *lc++ | *rc++;
1419 sv_catpvn(sv, rsave + len, rightlen - len);
1420 else if (leftlen > (STRLEN)len)
1421 sv_catpvn(sv, lsave + len, leftlen - len);
1436 HV * const hv = MUTABLE_HV(POPs);
1439 const I32 gimme = GIMME_V;
1440 const I32 dokv = (PL_op->op_type == OP_RV2HV || PL_op->op_type == OP_PADHV);
1441 const I32 dokeys = dokv || (PL_op->op_type == OP_KEYS);
1442 const I32 dovalues = dokv || (PL_op->op_type == OP_VALUES);
1445 if (PL_op->op_flags & OPf_MOD || LVRET) { /* lvalue */
1446 dTARGET; /* make sure to clear its target here */
1447 if (SvTYPE(TARG) == SVt_PVLV)
1448 LvTARG(TARG) = NULL;
1455 (void)hv_iterinit(keys); /* always reset iterator regardless */
1457 if (gimme == G_VOID)
1460 if (gimme == G_SCALAR) {
1464 if (PL_op->op_flags & OPf_MOD || LVRET) { /* lvalue */
1465 if (SvTYPE(TARG) < SVt_PVLV) {
1466 sv_upgrade(TARG, SVt_PVLV);
1467 sv_magic(TARG, NULL, PERL_MAGIC_nkeys, NULL, 0);
1470 if (LvTARG(TARG) != (const SV *)keys) {
1472 SvREFCNT_dec(LvTARG(TARG));
1473 LvTARG(TARG) = SvREFCNT_inc_simple(keys);
1479 if (! SvTIED_mg((const SV *)keys, PERL_MAGIC_tied) )
1485 while (hv_iternext(keys)) i++;
1491 EXTEND(SP, HvKEYS(keys) * (dokeys + dovalues));
1493 PUTBACK; /* hv_iternext and hv_iterval might clobber stack_sp */
1494 while ((entry = hv_iternext(keys))) {
1497 SV* const sv = hv_iterkeysv(entry);
1498 XPUSHs(sv); /* won't clobber stack_sp */
1503 tmpstr = hv_iterval(hv,entry);
1504 DEBUG_H(Perl_sv_setpvf(aTHX_ tmpstr, "%lu%%%d=%lu",
1505 (unsigned long)HeHASH(entry),
1507 (unsigned long)(HeHASH(entry) & HvMAX(keys))));
1518 * c-indentation-style: bsd
1520 * indent-tabs-mode: t
1523 * ex: set ts=8 sts=4 sw=4 noet: