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_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 if (PL_op->op_private & OPpTRANS_SQUASH) {
210 const UV comp = utf8n_to_uvchr(s, send - s, &len,
222 ch = (rlen == 0) ? (I32)comp :
223 (comp - 0x100 < rlen) ?
224 tbl[comp+1] : tbl[0x100+rlen];
226 d = uvchr_to_utf8(d, ch);
234 else if ((ch = tbl[comp]) >= 0) {
237 d = uvchr_to_utf8(d, ch);
243 else if (ch == -1) { /* -1 is unmapped character */
247 else if (ch == -2) /* -2 is delete character */
256 const UV comp = utf8n_to_uvchr(s, send - s, &len,
267 if (comp - 0x100 < rlen)
268 d = uvchr_to_utf8(d, tbl[comp+1]);
270 d = uvchr_to_utf8(d, tbl[0x100+rlen]);
274 else if ((ch = tbl[comp]) >= 0) {
275 d = uvchr_to_utf8(d, ch);
278 else if (ch == -1) { /* -1 is unmapped character */
282 else if (ch == -2) /* -2 is delete character */
288 sv_setpvn(sv, (char*)dstart, d - dstart);
293 SvCUR_set(sv, d - dstart);
302 S_do_trans_simple_utf8(pTHX_ SV * const sv)
311 const I32 grows = PL_op->op_private & OPpTRANS_GROWS;
315 PAD_SVl(cPADOP->op_padix);
317 MUTABLE_SV(cSVOP->op_sv);
319 HV* const hv = MUTABLE_HV(SvRV(rv));
320 SV* const * svp = hv_fetchs(hv, "NONE", FALSE);
321 const UV none = svp ? SvUV(*svp) : 0x7fffffff;
322 const UV extra = none + 1;
326 PERL_ARGS_ASSERT_DO_TRANS_SIMPLE_UTF8;
328 s = (U8*)SvPV(sv, len);
331 const U8 * const e = s + len;
334 hibit = !NATIVE_IS_INVARIANT(ch);
336 s = bytes_to_utf8(s, &len);
344 svp = hv_fetchs(hv, "FINAL", FALSE);
349 /* d needs to be bigger than s, in case e.g. upgrading is required */
350 Newx(d, len * 3 + UTF8_MAXBYTES, U8);
360 const UV uv = swash_fetch(rv, s, TRUE);
364 d = uvuni_to_utf8(d, uv);
366 else if (uv == none) {
367 const int i = UTF8SKIP(s);
372 else if (uv == extra) {
375 d = uvuni_to_utf8(d, final);
381 const STRLEN clen = d - dstart;
382 const STRLEN nlen = dend - dstart + len + UTF8_MAXBYTES;
384 Perl_croak(aTHX_ "panic: do_trans_simple_utf8 line %d",__LINE__);
385 Renew(dstart, nlen + UTF8_MAXBYTES, U8);
387 dend = dstart + nlen;
390 if (grows || hibit) {
391 sv_setpvn(sv, (char*)dstart, d - dstart);
398 SvCUR_set(sv, d - dstart);
407 S_do_trans_count_utf8(pTHX_ SV * const sv)
411 const U8 *start = NULL;
417 PAD_SVl(cPADOP->op_padix);
419 MUTABLE_SV(cSVOP->op_sv);
421 HV* const hv = MUTABLE_HV(SvRV(rv));
422 SV* const * const svp = hv_fetchs(hv, "NONE", FALSE);
423 const UV none = svp ? SvUV(*svp) : 0x7fffffff;
424 const UV extra = none + 1;
427 PERL_ARGS_ASSERT_DO_TRANS_COUNT_UTF8;
429 s = (const U8*)SvPV_const(sv, len);
432 const U8 * const e = s + len;
435 hibit = !NATIVE_IS_INVARIANT(ch);
437 start = s = bytes_to_utf8(s, &len);
445 const UV uv = swash_fetch(rv, s, TRUE);
446 if (uv < none || uv == extra)
457 S_do_trans_complex_utf8(pTHX_ SV * const sv)
463 const I32 squash = PL_op->op_private & OPpTRANS_SQUASH;
464 const I32 del = PL_op->op_private & OPpTRANS_DELETE;
465 const I32 grows = PL_op->op_private & OPpTRANS_GROWS;
468 PAD_SVl(cPADOP->op_padix);
470 MUTABLE_SV(cSVOP->op_sv);
472 HV * const hv = MUTABLE_HV(SvRV(rv));
473 SV * const *svp = hv_fetchs(hv, "NONE", FALSE);
474 const UV none = svp ? SvUV(*svp) : 0x7fffffff;
475 const UV extra = none + 1;
477 bool havefinal = FALSE;
481 U8 *s = (U8*)SvPV(sv, len);
483 PERL_ARGS_ASSERT_DO_TRANS_COMPLEX_UTF8;
487 const U8 * const e = s + len;
490 hibit = !NATIVE_IS_INVARIANT(ch);
492 s = bytes_to_utf8(s, &len);
500 svp = hv_fetchs(hv, "FINAL", FALSE);
507 /* d needs to be bigger than s, in case e.g. upgrading is required */
508 Newx(d, len * 3 + UTF8_MAXBYTES, U8);
520 UV uv = swash_fetch(rv, s, TRUE);
523 const STRLEN clen = d - dstart;
524 const STRLEN nlen = dend - dstart + len + UTF8_MAXBYTES;
526 Perl_croak(aTHX_ "panic: do_trans_complex_utf8 line %d",__LINE__);
527 Renew(dstart, nlen + UTF8_MAXBYTES, U8);
529 dend = dstart + nlen;
535 d = uvuni_to_utf8(d, uv);
540 else if (uv == none) { /* "none" is unmapped character */
541 const int i = UTF8SKIP(s);
548 else if (uv == extra && !del) {
553 d = uvuni_to_utf8(d, final);
559 uv = utf8n_to_uvuni(s, send - s, &len, UTF8_ALLOW_DEFAULT);
569 matches++; /* "none+1" is delete character */
575 const UV uv = swash_fetch(rv, s, TRUE);
577 const STRLEN clen = d - dstart;
578 const STRLEN nlen = dend - dstart + len + UTF8_MAXBYTES;
580 Perl_croak(aTHX_ "panic: do_trans_complex_utf8 line %d",__LINE__);
581 Renew(dstart, nlen + UTF8_MAXBYTES, U8);
583 dend = dstart + nlen;
588 d = uvuni_to_utf8(d, uv);
591 else if (uv == none) { /* "none" is unmapped character */
592 const int i = UTF8SKIP(s);
598 else if (uv == extra && !del) {
601 d = uvuni_to_utf8(d, final);
604 matches++; /* "none+1" is delete character */
608 if (grows || hibit) {
609 sv_setpvn(sv, (char*)dstart, d - dstart);
616 SvCUR_set(sv, d - dstart);
625 Perl_do_trans(pTHX_ SV *sv)
629 const I32 hasutf = (PL_op->op_private &
630 (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF));
632 PERL_ARGS_ASSERT_DO_TRANS;
634 if (SvREADONLY(sv) && !(PL_op->op_private & OPpTRANS_IDENTICAL)) {
636 sv_force_normal_flags(sv, 0);
638 Perl_croak(aTHX_ "%s", PL_no_modify);
640 (void)SvPV_const(sv, len);
643 if (!(PL_op->op_private & OPpTRANS_IDENTICAL)) {
645 (void)SvPV_force(sv, len);
646 (void)SvPOK_only_UTF8(sv);
649 DEBUG_t( Perl_deb(aTHX_ "2.TBL\n"));
651 switch (PL_op->op_private & ~hasutf & (
652 OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF|OPpTRANS_IDENTICAL|
653 OPpTRANS_SQUASH|OPpTRANS_DELETE|OPpTRANS_COMPLEMENT)) {
656 return do_trans_simple_utf8(sv);
658 return do_trans_simple(sv);
660 case OPpTRANS_IDENTICAL:
661 case OPpTRANS_IDENTICAL|OPpTRANS_COMPLEMENT:
663 return do_trans_count_utf8(sv);
665 return do_trans_count(sv);
669 return do_trans_complex_utf8(sv);
671 return do_trans_complex(sv);
676 Perl_do_join(pTHX_ register SV *sv, SV *delim, register SV **mark, register SV **sp)
679 SV ** const oldmark = mark;
680 register I32 items = sp - mark;
684 PERL_ARGS_ASSERT_DO_JOIN;
686 (void) SvPV_const(delim, delimlen); /* stringify and get the delimlen */
687 /* SvCUR assumes it's SvPOK() and woe betide you if it's not. */
690 len = (items > 0 ? (delimlen * (items - 1) ) : 0);
691 SvUPGRADE(sv, SVt_PV);
692 if (SvLEN(sv) < len + items) { /* current length is way too short */
693 while (items-- > 0) {
694 if (*mark && !SvGAMAGIC(*mark) && SvOK(*mark)) {
696 SvPV_const(*mark, tmplen);
701 SvGROW(sv, len + 1); /* so try to pre-extend */
709 /* sv_setpv retains old UTF8ness [perl #24846] */
712 if (PL_tainting && SvMAGICAL(sv))
722 for (; items > 0; items--,mark++) {
728 for (; items > 0; items--,mark++)
735 Perl_do_sprintf(pTHX_ SV *sv, I32 len, SV **sarg)
739 const char * const pat = SvPV_const(*sarg, patlen);
740 bool do_taint = FALSE;
742 PERL_ARGS_ASSERT_DO_SPRINTF;
747 sv_vsetpvfn(sv, pat, patlen, NULL, sarg + 1, len - 1, &do_taint);
753 /* currently converts input to bytes if possible, but doesn't sweat failure */
755 Perl_do_vecget(pTHX_ SV *sv, I32 offset, I32 size)
758 STRLEN srclen, len, uoffset, bitoffs = 0;
759 const unsigned char *s = (const unsigned char *) SvPV_const(sv, srclen);
762 PERL_ARGS_ASSERT_DO_VECGET;
766 if (size < 1 || (size & (size-1))) /* size < 1 or not a power of two */
767 Perl_croak(aTHX_ "Illegal number of bits in vec");
770 (void) Perl_sv_utf8_downgrade(aTHX_ sv, TRUE);
773 bitoffs = ((offset%8)*size)%8;
774 uoffset = offset/(8/size);
777 uoffset = offset*(size/8);
781 len = uoffset + (bitoffs + size + 7)/8; /* required number of bytes */
787 if (uoffset >= srclen)
790 retnum = (UV) s[uoffset] << 8;
792 else if (size == 32) {
793 if (uoffset >= srclen)
795 else if (uoffset + 1 >= srclen)
797 ((UV) s[uoffset ] << 24);
798 else if (uoffset + 2 >= srclen)
800 ((UV) s[uoffset ] << 24) +
801 ((UV) s[uoffset + 1] << 16);
804 ((UV) s[uoffset ] << 24) +
805 ((UV) s[uoffset + 1] << 16) +
806 ( s[uoffset + 2] << 8);
809 else if (size == 64) {
810 if (ckWARN(WARN_PORTABLE))
811 Perl_warner(aTHX_ packWARN(WARN_PORTABLE),
812 "Bit vector size > 32 non-portable");
813 if (uoffset >= srclen)
815 else if (uoffset + 1 >= srclen)
817 (UV) s[uoffset ] << 56;
818 else if (uoffset + 2 >= srclen)
820 ((UV) s[uoffset ] << 56) +
821 ((UV) s[uoffset + 1] << 48);
822 else if (uoffset + 3 >= srclen)
824 ((UV) s[uoffset ] << 56) +
825 ((UV) s[uoffset + 1] << 48) +
826 ((UV) s[uoffset + 2] << 40);
827 else if (uoffset + 4 >= srclen)
829 ((UV) s[uoffset ] << 56) +
830 ((UV) s[uoffset + 1] << 48) +
831 ((UV) s[uoffset + 2] << 40) +
832 ((UV) s[uoffset + 3] << 32);
833 else if (uoffset + 5 >= srclen)
835 ((UV) s[uoffset ] << 56) +
836 ((UV) s[uoffset + 1] << 48) +
837 ((UV) s[uoffset + 2] << 40) +
838 ((UV) s[uoffset + 3] << 32) +
839 ( s[uoffset + 4] << 24);
840 else if (uoffset + 6 >= srclen)
842 ((UV) s[uoffset ] << 56) +
843 ((UV) s[uoffset + 1] << 48) +
844 ((UV) s[uoffset + 2] << 40) +
845 ((UV) s[uoffset + 3] << 32) +
846 ((UV) s[uoffset + 4] << 24) +
847 ((UV) s[uoffset + 5] << 16);
850 ((UV) s[uoffset ] << 56) +
851 ((UV) s[uoffset + 1] << 48) +
852 ((UV) s[uoffset + 2] << 40) +
853 ((UV) s[uoffset + 3] << 32) +
854 ((UV) s[uoffset + 4] << 24) +
855 ((UV) s[uoffset + 5] << 16) +
856 ( s[uoffset + 6] << 8);
862 retnum = (s[uoffset] >> bitoffs) & ((1 << size) - 1);
868 ((UV) s[uoffset] << 8) +
872 ((UV) s[uoffset ] << 24) +
873 ((UV) s[uoffset + 1] << 16) +
874 ( s[uoffset + 2] << 8) +
877 else if (size == 64) {
878 if (ckWARN(WARN_PORTABLE))
879 Perl_warner(aTHX_ packWARN(WARN_PORTABLE),
880 "Bit vector size > 32 non-portable");
882 ((UV) s[uoffset ] << 56) +
883 ((UV) s[uoffset + 1] << 48) +
884 ((UV) s[uoffset + 2] << 40) +
885 ((UV) s[uoffset + 3] << 32) +
886 ((UV) s[uoffset + 4] << 24) +
887 ((UV) s[uoffset + 5] << 16) +
888 ( s[uoffset + 6] << 8) +
897 /* currently converts input to bytes if possible but doesn't sweat failures,
898 * although it does ensure that the string it clobbers is not marked as
899 * utf8-valid any more
902 Perl_do_vecset(pTHX_ SV *sv)
905 register I32 offset, bitoffs = 0;
907 register unsigned char *s;
912 SV * const targ = LvTARG(sv);
914 PERL_ARGS_ASSERT_DO_VECSET;
918 s = (unsigned char*)SvPV_force(targ, targlen);
920 /* This is handled by the SvPOK_only below...
921 if (!Perl_sv_utf8_downgrade(aTHX_ targ, TRUE))
924 (void) Perl_sv_utf8_downgrade(aTHX_ targ, TRUE);
927 (void)SvPOK_only(targ);
929 offset = LvTARGOFF(sv);
931 Perl_croak(aTHX_ "Negative offset to vec in lvalue context");
932 size = LvTARGLEN(sv);
933 if (size < 1 || (size & (size-1))) /* size < 1 or not a power of two */
934 Perl_croak(aTHX_ "Illegal number of bits in vec");
937 bitoffs = ((offset%8)*size)%8;
943 len = offset + (bitoffs + size + 7)/8; /* required number of bytes */
945 s = (unsigned char*)SvGROW(targ, len + 1);
946 (void)memzero((char *)(s + targlen), len - targlen + 1);
947 SvCUR_set(targ, len);
951 mask = (1 << size) - 1;
953 s[offset] &= ~(mask << bitoffs);
954 s[offset] |= lval << bitoffs;
958 s[offset ] = (U8)( lval & 0xff);
959 else if (size == 16) {
960 s[offset ] = (U8)((lval >> 8) & 0xff);
961 s[offset+1] = (U8)( lval & 0xff);
963 else if (size == 32) {
964 s[offset ] = (U8)((lval >> 24) & 0xff);
965 s[offset+1] = (U8)((lval >> 16) & 0xff);
966 s[offset+2] = (U8)((lval >> 8) & 0xff);
967 s[offset+3] = (U8)( lval & 0xff);
970 else if (size == 64) {
971 if (ckWARN(WARN_PORTABLE))
972 Perl_warner(aTHX_ packWARN(WARN_PORTABLE),
973 "Bit vector size > 32 non-portable");
974 s[offset ] = (U8)((lval >> 56) & 0xff);
975 s[offset+1] = (U8)((lval >> 48) & 0xff);
976 s[offset+2] = (U8)((lval >> 40) & 0xff);
977 s[offset+3] = (U8)((lval >> 32) & 0xff);
978 s[offset+4] = (U8)((lval >> 24) & 0xff);
979 s[offset+5] = (U8)((lval >> 16) & 0xff);
980 s[offset+6] = (U8)((lval >> 8) & 0xff);
981 s[offset+7] = (U8)( lval & 0xff);
989 Perl_do_chop(pTHX_ register SV *astr, register SV *sv)
995 PERL_ARGS_ASSERT_DO_CHOP;
997 if (SvTYPE(sv) == SVt_PVAV) {
999 AV *const av = MUTABLE_AV(sv);
1000 const I32 max = AvFILL(av);
1002 for (i = 0; i <= max; i++) {
1003 sv = MUTABLE_SV(av_fetch(av, i, FALSE));
1004 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
1009 else if (SvTYPE(sv) == SVt_PVHV) {
1010 HV* const hv = MUTABLE_HV(sv);
1012 (void)hv_iterinit(hv);
1013 while ((entry = hv_iternext(hv)))
1014 do_chop(astr,hv_iterval(hv,entry));
1017 else if (SvREADONLY(sv)) {
1019 /* SV is copy-on-write */
1020 sv_force_normal_flags(sv, 0);
1023 Perl_croak(aTHX_ "%s", PL_no_modify);
1026 if (PL_encoding && !SvUTF8(sv)) {
1027 /* like in do_chomp(), utf8-ize the sv as a side-effect
1028 * if we're using encoding. */
1029 sv_recode_to_utf8(sv, PL_encoding);
1033 if (len && !SvPOK(sv))
1034 s = SvPV_force_nomg(sv, len);
1037 char * const send = s + len;
1038 char * const start = s;
1040 while (s > start && UTF8_IS_CONTINUATION(*s))
1042 if (is_utf8_string((U8*)s, send - s)) {
1043 sv_setpvn(astr, s, send - s);
1045 SvCUR_set(sv, s - start);
1051 sv_setpvs(astr, "");
1053 else if (s && len) {
1055 sv_setpvn(astr, s, 1);
1062 sv_setpvs(astr, "");
1067 Perl_do_chomp(pTHX_ register SV *sv)
1073 char *temp_buffer = NULL;
1074 SV* svrecode = NULL;
1076 PERL_ARGS_ASSERT_DO_CHOMP;
1080 if (RsRECORD(PL_rs))
1083 if (SvTYPE(sv) == SVt_PVAV) {
1085 AV *const av = MUTABLE_AV(sv);
1086 const I32 max = AvFILL(av);
1088 for (i = 0; i <= max; i++) {
1089 sv = MUTABLE_SV(av_fetch(av, i, FALSE));
1090 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
1091 count += do_chomp(sv);
1095 else if (SvTYPE(sv) == SVt_PVHV) {
1096 HV* const hv = MUTABLE_HV(sv);
1098 (void)hv_iterinit(hv);
1099 while ((entry = hv_iternext(hv)))
1100 count += do_chomp(hv_iterval(hv,entry));
1103 else if (SvREADONLY(sv)) {
1105 /* SV is copy-on-write */
1106 sv_force_normal_flags(sv, 0);
1109 Perl_croak(aTHX_ "%s", PL_no_modify);
1114 /* XXX, here sv is utf8-ized as a side-effect!
1115 If encoding.pm is used properly, almost string-generating
1116 operations, including literal strings, chr(), input data, etc.
1117 should have been utf8-ized already, right?
1119 sv_recode_to_utf8(sv, PL_encoding);
1126 if (RsPARA(PL_rs)) {
1130 while (len && s[-1] == '\n') {
1137 STRLEN rslen, rs_charlen;
1138 const char *rsptr = SvPV_const(PL_rs, rslen);
1140 rs_charlen = SvUTF8(PL_rs)
1141 ? sv_len_utf8(PL_rs)
1144 if (SvUTF8(PL_rs) != SvUTF8(sv)) {
1145 /* Assumption is that rs is shorter than the scalar. */
1146 if (SvUTF8(PL_rs)) {
1147 /* RS is utf8, scalar is 8 bit. */
1148 bool is_utf8 = TRUE;
1149 temp_buffer = (char*)bytes_from_utf8((U8*)rsptr,
1152 /* Cannot downgrade, therefore cannot possibly match
1154 assert (temp_buffer == rsptr);
1158 rsptr = temp_buffer;
1160 else if (PL_encoding) {
1161 /* RS is 8 bit, encoding.pm is used.
1162 * Do not recode PL_rs as a side-effect. */
1163 svrecode = newSVpvn(rsptr, rslen);
1164 sv_recode_to_utf8(svrecode, PL_encoding);
1165 rsptr = SvPV_const(svrecode, rslen);
1166 rs_charlen = sv_len_utf8(svrecode);
1169 /* RS is 8 bit, scalar is utf8. */
1170 temp_buffer = (char*)bytes_to_utf8((U8*)rsptr, &rslen);
1171 rsptr = temp_buffer;
1180 if (len < rslen - 1)
1184 if (memNE(s, rsptr, rslen))
1186 count += rs_charlen;
1189 s = SvPV_force_nolen(sv);
1198 SvREFCNT_dec(svrecode);
1200 Safefree(temp_buffer);
1205 Perl_do_vop(pTHX_ I32 optype, SV *sv, SV *left, SV *right)
1216 register const char *lc;
1217 register const char *rc;
1218 register STRLEN len;
1226 PERL_ARGS_ASSERT_DO_VOP;
1228 if (sv != left || (optype != OP_BIT_AND && !SvOK(sv) && !SvGMAGICAL(sv)))
1229 sv_setpvs(sv, ""); /* avoid undef warning on |= and ^= */
1231 lsave = lc = SvPV_force_nomg(left, leftlen);
1234 lsave = lc = SvPV_nomg_const(left, leftlen);
1235 SvPV_force_nomg_nolen(sv);
1237 rsave = rc = SvPV_nomg_const(right, rightlen);
1239 /* This need to come after SvPV to ensure that string overloading has
1242 left_utf = DO_UTF8(left);
1243 right_utf = DO_UTF8(right);
1245 if (left_utf && !right_utf) {
1246 /* Avoid triggering overloading again by using temporaries.
1247 Maybe there should be a variant of sv_utf8_upgrade that takes pvn
1249 right = newSVpvn_flags(rsave, rightlen, SVs_TEMP);
1250 sv_utf8_upgrade(right);
1251 rsave = rc = SvPV_nomg_const(right, rightlen);
1254 else if (!left_utf && right_utf) {
1255 left = newSVpvn_flags(lsave, leftlen, SVs_TEMP);
1256 sv_utf8_upgrade(left);
1257 lsave = lc = SvPV_nomg_const(left, leftlen);
1261 len = leftlen < rightlen ? leftlen : rightlen;
1264 (void)SvPOK_only(sv);
1265 if ((left_utf || right_utf) && (sv == left || sv == right)) {
1266 needlen = optype == OP_BIT_AND ? len : leftlen + rightlen;
1267 Newxz(dc, needlen + 1, char);
1269 else if (SvOK(sv) || SvTYPE(sv) > SVt_PVMG) {
1270 dc = SvPV_force_nomg_nolen(sv);
1271 if (SvLEN(sv) < len + 1) {
1272 dc = SvGROW(sv, len + 1);
1273 (void)memzero(dc + SvCUR(sv), len - SvCUR(sv) + 1);
1275 if (optype != OP_BIT_AND && (left_utf || right_utf))
1276 dc = SvGROW(sv, leftlen + rightlen + 1);
1279 needlen = optype == OP_BIT_AND
1280 ? len : (leftlen > rightlen ? leftlen : rightlen);
1281 Newxz(dc, needlen + 1, char);
1282 sv_usepvn_flags(sv, dc, needlen, SV_HAS_TRAILING_NUL);
1283 dc = SvPVX(sv); /* sv_usepvn() calls Renew() */
1285 if (left_utf || right_utf) {
1288 char *dcsave = NULL;
1289 STRLEN lulen = leftlen;
1290 STRLEN rulen = rightlen;
1295 while (lulen && rulen) {
1296 luc = utf8n_to_uvchr((U8*)lc, lulen, &ulen, UTF8_ALLOW_ANYUV);
1299 ruc = utf8n_to_uvchr((U8*)rc, rulen, &ulen, UTF8_ALLOW_ANYUV);
1303 dc = (char*)uvchr_to_utf8((U8*)dc, duc);
1305 if (sv == left || sv == right)
1306 (void)sv_usepvn(sv, dcorig, needlen);
1307 SvCUR_set(sv, dc - dcorig);
1310 while (lulen && rulen) {
1311 luc = utf8n_to_uvchr((U8*)lc, lulen, &ulen, UTF8_ALLOW_ANYUV);
1314 ruc = utf8n_to_uvchr((U8*)rc, rulen, &ulen, UTF8_ALLOW_ANYUV);
1318 dc = (char*)uvchr_to_utf8((U8*)dc, duc);
1322 while (lulen && rulen) {
1323 luc = utf8n_to_uvchr((U8*)lc, lulen, &ulen, UTF8_ALLOW_ANYUV);
1326 ruc = utf8n_to_uvchr((U8*)rc, rulen, &ulen, UTF8_ALLOW_ANYUV);
1330 dc = (char*)uvchr_to_utf8((U8*)dc, duc);
1334 dcsave = savepvn(rc, rulen);
1336 dcsave = savepvn(lc, lulen);
1337 if (sv == left || sv == right)
1338 (void)sv_usepvn(sv, dcorig, needlen); /* Uses Renew(). */
1339 SvCUR_set(sv, dc - dcorig);
1341 sv_catpvn(sv, dcsave, rulen);
1343 sv_catpvn(sv, dcsave, lulen);
1349 if (sv == left || sv == right)
1351 Perl_croak(aTHX_ "panic: do_vop called for op %u (%s)",
1352 (unsigned)optype, PL_op_name[optype]);
1359 if (len >= sizeof(long)*4 &&
1360 !((unsigned long)dc % sizeof(long)) &&
1361 !((unsigned long)lc % sizeof(long)) &&
1362 !((unsigned long)rc % sizeof(long))) /* It's almost always aligned... */
1364 const STRLEN remainder = len % (sizeof(long)*4);
1365 len /= (sizeof(long)*4);
1374 *dl++ = *ll++ & *rl++;
1375 *dl++ = *ll++ & *rl++;
1376 *dl++ = *ll++ & *rl++;
1377 *dl++ = *ll++ & *rl++;
1382 *dl++ = *ll++ ^ *rl++;
1383 *dl++ = *ll++ ^ *rl++;
1384 *dl++ = *ll++ ^ *rl++;
1385 *dl++ = *ll++ ^ *rl++;
1390 *dl++ = *ll++ | *rl++;
1391 *dl++ = *ll++ | *rl++;
1392 *dl++ = *ll++ | *rl++;
1393 *dl++ = *ll++ | *rl++;
1408 *dc++ = *lc++ & *rc++;
1413 *dc++ = *lc++ ^ *rc++;
1417 *dc++ = *lc++ | *rc++;
1421 sv_catpvn(sv, rsave + len, rightlen - len);
1422 else if (leftlen > (STRLEN)len)
1423 sv_catpvn(sv, lsave + len, leftlen - len);
1438 HV * const hv = MUTABLE_HV(POPs);
1441 const I32 gimme = GIMME_V;
1442 const I32 dokv = (PL_op->op_type == OP_RV2HV || PL_op->op_type == OP_PADHV);
1443 const I32 dokeys = dokv || (PL_op->op_type == OP_KEYS);
1444 const I32 dovalues = dokv || (PL_op->op_type == OP_VALUES);
1447 if (PL_op->op_flags & OPf_MOD || LVRET) { /* lvalue */
1448 dTARGET; /* make sure to clear its target here */
1449 if (SvTYPE(TARG) == SVt_PVLV)
1450 LvTARG(TARG) = NULL;
1457 (void)hv_iterinit(keys); /* always reset iterator regardless */
1459 if (gimme == G_VOID)
1462 if (gimme == G_SCALAR) {
1466 if (PL_op->op_flags & OPf_MOD || LVRET) { /* lvalue */
1467 if (SvTYPE(TARG) < SVt_PVLV) {
1468 sv_upgrade(TARG, SVt_PVLV);
1469 sv_magic(TARG, NULL, PERL_MAGIC_nkeys, NULL, 0);
1472 if (LvTARG(TARG) != (const SV *)keys) {
1474 SvREFCNT_dec(LvTARG(TARG));
1475 LvTARG(TARG) = SvREFCNT_inc_simple(keys);
1481 if (! SvTIED_mg((const SV *)keys, PERL_MAGIC_tied) )
1487 while (hv_iternext(keys)) i++;
1493 EXTEND(SP, HvKEYS(keys) * (dokeys + dovalues));
1495 PUTBACK; /* hv_iternext and hv_iterval might clobber stack_sp */
1496 while ((entry = hv_iternext(keys))) {
1499 SV* const sv = hv_iterkeysv(entry);
1500 XPUSHs(sv); /* won't clobber stack_sp */
1505 tmpstr = hv_iterval(hv,entry);
1506 DEBUG_H(Perl_sv_setpvf(aTHX_ tmpstr, "%lu%%%d=%lu",
1507 (unsigned long)HeHASH(entry),
1509 (unsigned long)(HeHASH(entry) & HvMAX(keys))));
1520 * c-indentation-style: bsd
1522 * indent-tabs-mode: t
1525 * ex: set ts=8 sts=4 sw=4 noet: