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 Perl_ck_warner(aTHX_ packWARN(WARN_PORTABLE),
811 "Bit vector size > 32 non-portable");
812 if (uoffset >= srclen)
814 else if (uoffset + 1 >= srclen)
816 (UV) s[uoffset ] << 56;
817 else if (uoffset + 2 >= srclen)
819 ((UV) s[uoffset ] << 56) +
820 ((UV) s[uoffset + 1] << 48);
821 else if (uoffset + 3 >= srclen)
823 ((UV) s[uoffset ] << 56) +
824 ((UV) s[uoffset + 1] << 48) +
825 ((UV) s[uoffset + 2] << 40);
826 else if (uoffset + 4 >= srclen)
828 ((UV) s[uoffset ] << 56) +
829 ((UV) s[uoffset + 1] << 48) +
830 ((UV) s[uoffset + 2] << 40) +
831 ((UV) s[uoffset + 3] << 32);
832 else if (uoffset + 5 >= srclen)
834 ((UV) s[uoffset ] << 56) +
835 ((UV) s[uoffset + 1] << 48) +
836 ((UV) s[uoffset + 2] << 40) +
837 ((UV) s[uoffset + 3] << 32) +
838 ( s[uoffset + 4] << 24);
839 else if (uoffset + 6 >= srclen)
841 ((UV) s[uoffset ] << 56) +
842 ((UV) s[uoffset + 1] << 48) +
843 ((UV) s[uoffset + 2] << 40) +
844 ((UV) s[uoffset + 3] << 32) +
845 ((UV) s[uoffset + 4] << 24) +
846 ((UV) s[uoffset + 5] << 16);
849 ((UV) s[uoffset ] << 56) +
850 ((UV) s[uoffset + 1] << 48) +
851 ((UV) s[uoffset + 2] << 40) +
852 ((UV) s[uoffset + 3] << 32) +
853 ((UV) s[uoffset + 4] << 24) +
854 ((UV) s[uoffset + 5] << 16) +
855 ( s[uoffset + 6] << 8);
861 retnum = (s[uoffset] >> bitoffs) & ((1 << size) - 1);
867 ((UV) s[uoffset] << 8) +
871 ((UV) s[uoffset ] << 24) +
872 ((UV) s[uoffset + 1] << 16) +
873 ( s[uoffset + 2] << 8) +
876 else if (size == 64) {
877 Perl_ck_warner(aTHX_ packWARN(WARN_PORTABLE),
878 "Bit vector size > 32 non-portable");
880 ((UV) s[uoffset ] << 56) +
881 ((UV) s[uoffset + 1] << 48) +
882 ((UV) s[uoffset + 2] << 40) +
883 ((UV) s[uoffset + 3] << 32) +
884 ((UV) s[uoffset + 4] << 24) +
885 ((UV) s[uoffset + 5] << 16) +
886 ( s[uoffset + 6] << 8) +
895 /* currently converts input to bytes if possible but doesn't sweat failures,
896 * although it does ensure that the string it clobbers is not marked as
897 * utf8-valid any more
900 Perl_do_vecset(pTHX_ SV *sv)
903 register I32 offset, bitoffs = 0;
905 register unsigned char *s;
910 SV * const targ = LvTARG(sv);
912 PERL_ARGS_ASSERT_DO_VECSET;
916 s = (unsigned char*)SvPV_force(targ, targlen);
918 /* This is handled by the SvPOK_only below...
919 if (!Perl_sv_utf8_downgrade(aTHX_ targ, TRUE))
922 (void) Perl_sv_utf8_downgrade(aTHX_ targ, TRUE);
925 (void)SvPOK_only(targ);
927 offset = LvTARGOFF(sv);
929 Perl_croak(aTHX_ "Negative offset to vec in lvalue context");
930 size = LvTARGLEN(sv);
931 if (size < 1 || (size & (size-1))) /* size < 1 or not a power of two */
932 Perl_croak(aTHX_ "Illegal number of bits in vec");
935 bitoffs = ((offset%8)*size)%8;
941 len = offset + (bitoffs + size + 7)/8; /* required number of bytes */
943 s = (unsigned char*)SvGROW(targ, len + 1);
944 (void)memzero((char *)(s + targlen), len - targlen + 1);
945 SvCUR_set(targ, len);
949 mask = (1 << size) - 1;
951 s[offset] &= ~(mask << bitoffs);
952 s[offset] |= lval << bitoffs;
956 s[offset ] = (U8)( lval & 0xff);
957 else if (size == 16) {
958 s[offset ] = (U8)((lval >> 8) & 0xff);
959 s[offset+1] = (U8)( lval & 0xff);
961 else if (size == 32) {
962 s[offset ] = (U8)((lval >> 24) & 0xff);
963 s[offset+1] = (U8)((lval >> 16) & 0xff);
964 s[offset+2] = (U8)((lval >> 8) & 0xff);
965 s[offset+3] = (U8)( lval & 0xff);
968 else if (size == 64) {
969 Perl_ck_warner(aTHX_ packWARN(WARN_PORTABLE),
970 "Bit vector size > 32 non-portable");
971 s[offset ] = (U8)((lval >> 56) & 0xff);
972 s[offset+1] = (U8)((lval >> 48) & 0xff);
973 s[offset+2] = (U8)((lval >> 40) & 0xff);
974 s[offset+3] = (U8)((lval >> 32) & 0xff);
975 s[offset+4] = (U8)((lval >> 24) & 0xff);
976 s[offset+5] = (U8)((lval >> 16) & 0xff);
977 s[offset+6] = (U8)((lval >> 8) & 0xff);
978 s[offset+7] = (U8)( lval & 0xff);
986 Perl_do_chop(pTHX_ register SV *astr, register SV *sv)
992 PERL_ARGS_ASSERT_DO_CHOP;
994 if (SvTYPE(sv) == SVt_PVAV) {
996 AV *const av = MUTABLE_AV(sv);
997 const I32 max = AvFILL(av);
999 for (i = 0; i <= max; i++) {
1000 sv = MUTABLE_SV(av_fetch(av, i, FALSE));
1001 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
1006 else if (SvTYPE(sv) == SVt_PVHV) {
1007 HV* const hv = MUTABLE_HV(sv);
1009 (void)hv_iterinit(hv);
1010 while ((entry = hv_iternext(hv)))
1011 do_chop(astr,hv_iterval(hv,entry));
1014 else if (SvREADONLY(sv)) {
1016 /* SV is copy-on-write */
1017 sv_force_normal_flags(sv, 0);
1020 Perl_croak(aTHX_ "%s", PL_no_modify);
1023 if (PL_encoding && !SvUTF8(sv)) {
1024 /* like in do_chomp(), utf8-ize the sv as a side-effect
1025 * if we're using encoding. */
1026 sv_recode_to_utf8(sv, PL_encoding);
1030 if (len && !SvPOK(sv))
1031 s = SvPV_force_nomg(sv, len);
1034 char * const send = s + len;
1035 char * const start = s;
1037 while (s > start && UTF8_IS_CONTINUATION(*s))
1039 if (is_utf8_string((U8*)s, send - s)) {
1040 sv_setpvn(astr, s, send - s);
1042 SvCUR_set(sv, s - start);
1048 sv_setpvs(astr, "");
1050 else if (s && len) {
1052 sv_setpvn(astr, s, 1);
1059 sv_setpvs(astr, "");
1064 Perl_do_chomp(pTHX_ register SV *sv)
1070 char *temp_buffer = NULL;
1071 SV* svrecode = NULL;
1073 PERL_ARGS_ASSERT_DO_CHOMP;
1077 if (RsRECORD(PL_rs))
1080 if (SvTYPE(sv) == SVt_PVAV) {
1082 AV *const av = MUTABLE_AV(sv);
1083 const I32 max = AvFILL(av);
1085 for (i = 0; i <= max; i++) {
1086 sv = MUTABLE_SV(av_fetch(av, i, FALSE));
1087 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
1088 count += do_chomp(sv);
1092 else if (SvTYPE(sv) == SVt_PVHV) {
1093 HV* const hv = MUTABLE_HV(sv);
1095 (void)hv_iterinit(hv);
1096 while ((entry = hv_iternext(hv)))
1097 count += do_chomp(hv_iterval(hv,entry));
1100 else if (SvREADONLY(sv)) {
1102 /* SV is copy-on-write */
1103 sv_force_normal_flags(sv, 0);
1106 Perl_croak(aTHX_ "%s", PL_no_modify);
1111 /* XXX, here sv is utf8-ized as a side-effect!
1112 If encoding.pm is used properly, almost string-generating
1113 operations, including literal strings, chr(), input data, etc.
1114 should have been utf8-ized already, right?
1116 sv_recode_to_utf8(sv, PL_encoding);
1123 if (RsPARA(PL_rs)) {
1127 while (len && s[-1] == '\n') {
1134 STRLEN rslen, rs_charlen;
1135 const char *rsptr = SvPV_const(PL_rs, rslen);
1137 rs_charlen = SvUTF8(PL_rs)
1138 ? sv_len_utf8(PL_rs)
1141 if (SvUTF8(PL_rs) != SvUTF8(sv)) {
1142 /* Assumption is that rs is shorter than the scalar. */
1143 if (SvUTF8(PL_rs)) {
1144 /* RS is utf8, scalar is 8 bit. */
1145 bool is_utf8 = TRUE;
1146 temp_buffer = (char*)bytes_from_utf8((U8*)rsptr,
1149 /* Cannot downgrade, therefore cannot possibly match
1151 assert (temp_buffer == rsptr);
1155 rsptr = temp_buffer;
1157 else if (PL_encoding) {
1158 /* RS is 8 bit, encoding.pm is used.
1159 * Do not recode PL_rs as a side-effect. */
1160 svrecode = newSVpvn(rsptr, rslen);
1161 sv_recode_to_utf8(svrecode, PL_encoding);
1162 rsptr = SvPV_const(svrecode, rslen);
1163 rs_charlen = sv_len_utf8(svrecode);
1166 /* RS is 8 bit, scalar is utf8. */
1167 temp_buffer = (char*)bytes_to_utf8((U8*)rsptr, &rslen);
1168 rsptr = temp_buffer;
1177 if (len < rslen - 1)
1181 if (memNE(s, rsptr, rslen))
1183 count += rs_charlen;
1186 s = SvPV_force_nolen(sv);
1194 SvREFCNT_dec(svrecode);
1196 Safefree(temp_buffer);
1201 Perl_do_vop(pTHX_ I32 optype, SV *sv, SV *left, SV *right)
1212 register const char *lc;
1213 register const char *rc;
1214 register STRLEN len;
1222 PERL_ARGS_ASSERT_DO_VOP;
1224 if (sv != left || (optype != OP_BIT_AND && !SvOK(sv) && !SvGMAGICAL(sv)))
1225 sv_setpvs(sv, ""); /* avoid undef warning on |= and ^= */
1227 lsave = lc = SvPV_force_nomg(left, leftlen);
1230 lsave = lc = SvPV_nomg_const(left, leftlen);
1231 SvPV_force_nomg_nolen(sv);
1233 rsave = rc = SvPV_nomg_const(right, rightlen);
1235 /* This need to come after SvPV to ensure that string overloading has
1238 left_utf = DO_UTF8(left);
1239 right_utf = DO_UTF8(right);
1241 if (left_utf && !right_utf) {
1242 /* Avoid triggering overloading again by using temporaries.
1243 Maybe there should be a variant of sv_utf8_upgrade that takes pvn
1245 right = newSVpvn_flags(rsave, rightlen, SVs_TEMP);
1246 sv_utf8_upgrade(right);
1247 rsave = rc = SvPV_nomg_const(right, rightlen);
1250 else if (!left_utf && right_utf) {
1251 left = newSVpvn_flags(lsave, leftlen, SVs_TEMP);
1252 sv_utf8_upgrade(left);
1253 lsave = lc = SvPV_nomg_const(left, leftlen);
1257 len = leftlen < rightlen ? leftlen : rightlen;
1260 (void)SvPOK_only(sv);
1261 if ((left_utf || right_utf) && (sv == left || sv == right)) {
1262 needlen = optype == OP_BIT_AND ? len : leftlen + rightlen;
1263 Newxz(dc, needlen + 1, char);
1265 else if (SvOK(sv) || SvTYPE(sv) > SVt_PVMG) {
1266 dc = SvPV_force_nomg_nolen(sv);
1267 if (SvLEN(sv) < len + 1) {
1268 dc = SvGROW(sv, len + 1);
1269 (void)memzero(dc + SvCUR(sv), len - SvCUR(sv) + 1);
1271 if (optype != OP_BIT_AND && (left_utf || right_utf))
1272 dc = SvGROW(sv, leftlen + rightlen + 1);
1275 needlen = optype == OP_BIT_AND
1276 ? len : (leftlen > rightlen ? leftlen : rightlen);
1277 Newxz(dc, needlen + 1, char);
1278 sv_usepvn_flags(sv, dc, needlen, SV_HAS_TRAILING_NUL);
1279 dc = SvPVX(sv); /* sv_usepvn() calls Renew() */
1281 if (left_utf || right_utf) {
1284 char *dcsave = NULL;
1285 STRLEN lulen = leftlen;
1286 STRLEN rulen = rightlen;
1291 while (lulen && rulen) {
1292 luc = utf8n_to_uvchr((U8*)lc, lulen, &ulen, UTF8_ALLOW_ANYUV);
1295 ruc = utf8n_to_uvchr((U8*)rc, rulen, &ulen, UTF8_ALLOW_ANYUV);
1299 dc = (char*)uvchr_to_utf8((U8*)dc, duc);
1301 if (sv == left || sv == right)
1302 (void)sv_usepvn(sv, dcorig, needlen);
1303 SvCUR_set(sv, dc - dcorig);
1306 while (lulen && rulen) {
1307 luc = utf8n_to_uvchr((U8*)lc, lulen, &ulen, UTF8_ALLOW_ANYUV);
1310 ruc = utf8n_to_uvchr((U8*)rc, rulen, &ulen, UTF8_ALLOW_ANYUV);
1314 dc = (char*)uvchr_to_utf8((U8*)dc, duc);
1318 while (lulen && rulen) {
1319 luc = utf8n_to_uvchr((U8*)lc, lulen, &ulen, UTF8_ALLOW_ANYUV);
1322 ruc = utf8n_to_uvchr((U8*)rc, rulen, &ulen, UTF8_ALLOW_ANYUV);
1326 dc = (char*)uvchr_to_utf8((U8*)dc, duc);
1330 dcsave = savepvn(rc, rulen);
1332 dcsave = savepvn(lc, lulen);
1333 if (sv == left || sv == right)
1334 (void)sv_usepvn(sv, dcorig, needlen); /* Uses Renew(). */
1335 SvCUR_set(sv, dc - dcorig);
1337 sv_catpvn(sv, dcsave, rulen);
1339 sv_catpvn(sv, dcsave, lulen);
1345 if (sv == left || sv == right)
1347 Perl_croak(aTHX_ "panic: do_vop called for op %u (%s)",
1348 (unsigned)optype, PL_op_name[optype]);
1355 if (len >= sizeof(long)*4 &&
1356 !((unsigned long)dc % sizeof(long)) &&
1357 !((unsigned long)lc % sizeof(long)) &&
1358 !((unsigned long)rc % sizeof(long))) /* It's almost always aligned... */
1360 const STRLEN remainder = len % (sizeof(long)*4);
1361 len /= (sizeof(long)*4);
1370 *dl++ = *ll++ & *rl++;
1371 *dl++ = *ll++ & *rl++;
1372 *dl++ = *ll++ & *rl++;
1373 *dl++ = *ll++ & *rl++;
1378 *dl++ = *ll++ ^ *rl++;
1379 *dl++ = *ll++ ^ *rl++;
1380 *dl++ = *ll++ ^ *rl++;
1381 *dl++ = *ll++ ^ *rl++;
1386 *dl++ = *ll++ | *rl++;
1387 *dl++ = *ll++ | *rl++;
1388 *dl++ = *ll++ | *rl++;
1389 *dl++ = *ll++ | *rl++;
1404 *dc++ = *lc++ & *rc++;
1409 *dc++ = *lc++ ^ *rc++;
1413 *dc++ = *lc++ | *rc++;
1417 sv_catpvn(sv, rsave + len, rightlen - len);
1418 else if (leftlen > (STRLEN)len)
1419 sv_catpvn(sv, lsave + len, leftlen - len);
1434 HV * const hv = MUTABLE_HV(POPs);
1437 const I32 gimme = GIMME_V;
1438 const I32 dokv = (PL_op->op_type == OP_RV2HV || PL_op->op_type == OP_PADHV);
1439 const I32 dokeys = dokv || (PL_op->op_type == OP_KEYS);
1440 const I32 dovalues = dokv || (PL_op->op_type == OP_VALUES);
1443 if (PL_op->op_flags & OPf_MOD || LVRET) { /* lvalue */
1444 dTARGET; /* make sure to clear its target here */
1445 if (SvTYPE(TARG) == SVt_PVLV)
1446 LvTARG(TARG) = NULL;
1453 (void)hv_iterinit(keys); /* always reset iterator regardless */
1455 if (gimme == G_VOID)
1458 if (gimme == G_SCALAR) {
1462 if (PL_op->op_flags & OPf_MOD || LVRET) { /* lvalue */
1463 if (SvTYPE(TARG) < SVt_PVLV) {
1464 sv_upgrade(TARG, SVt_PVLV);
1465 sv_magic(TARG, NULL, PERL_MAGIC_nkeys, NULL, 0);
1468 if (LvTARG(TARG) != (const SV *)keys) {
1469 SvREFCNT_dec(LvTARG(TARG));
1470 LvTARG(TARG) = SvREFCNT_inc_simple(keys);
1476 if (! SvTIED_mg((const SV *)keys, PERL_MAGIC_tied) )
1482 while (hv_iternext(keys)) i++;
1488 EXTEND(SP, HvKEYS(keys) * (dokeys + dovalues));
1490 PUTBACK; /* hv_iternext and hv_iterval might clobber stack_sp */
1491 while ((entry = hv_iternext(keys))) {
1494 SV* const sv = hv_iterkeysv(entry);
1495 XPUSHs(sv); /* won't clobber stack_sp */
1500 tmpstr = hv_iterval(hv,entry);
1501 DEBUG_H(Perl_sv_setpvf(aTHX_ tmpstr, "%lu%%%d=%lu",
1502 (unsigned long)HeHASH(entry),
1504 (unsigned long)(HeHASH(entry) & HvMAX(keys))));
1515 * c-indentation-style: bsd
1517 * indent-tabs-mode: t
1520 * ex: set ts=8 sts=4 sw=4 noet: