3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2004, 2005, 2006, 2007, by Larry Wall and others
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
12 * "'So that was the job I felt I had to do when I started,' thought Sam."
15 /* This file contains some common functions needed to carry out certain
16 * ops. For example both pp_schomp() and pp_chomp() - scalar and array
17 * chomp operations - call the function do_chomp() found in this file.
21 #define PERL_IN_DOOP_C
29 S_do_trans_simple(pTHX_ SV * const sv)
34 U8 *s = (U8*)SvPV(sv,len);
35 U8 * const send = s+len;
36 const short * const tbl = (short*)cPVOP->op_pv;
38 PERL_ARGS_ASSERT_DO_TRANS_SIMPLE;
41 Perl_croak(aTHX_ "panic: do_trans_simple line %d",__LINE__);
43 /* First, take care of non-UTF-8 input strings, because they're easy */
46 const I32 ch = tbl[*s];
56 const I32 grows = PL_op->op_private & OPpTRANS_GROWS;
60 /* Allow for expansion: $_="a".chr(400); tr/a/\xFE/, FE needs encoding */
70 /* Need to check this, otherwise 128..255 won't match */
71 const UV c = utf8n_to_uvchr(s, send - s, &ulen, UTF8_ALLOW_DEFAULT);
72 if (c < 0x100 && (ch = tbl[c]) >= 0) {
74 d = uvchr_to_utf8(d, ch);
77 else { /* No match -> copy */
84 sv_setpvn(sv, (char*)dstart, d - dstart);
89 SvCUR_set(sv, d - dstart);
98 S_do_trans_count(pTHX_ SV * const sv)
102 const U8 *s = (const U8*)SvPV_const(sv, len);
103 const U8 * const send = s + len;
105 const short * const tbl = (short*)cPVOP->op_pv;
107 PERL_ARGS_ASSERT_DO_TRANS_COUNT;
110 Perl_croak(aTHX_ "panic: do_trans_count line %d",__LINE__);
119 const I32 complement = PL_op->op_private & OPpTRANS_COMPLEMENT;
122 const UV c = utf8n_to_uvchr(s, send - s, &ulen, UTF8_ALLOW_DEFAULT);
126 } else if (complement)
136 S_do_trans_complex(pTHX_ SV * const sv)
140 U8 *s = (U8*)SvPV(sv, len);
141 U8 * const send = s+len;
143 const short * const tbl = (short*)cPVOP->op_pv;
145 PERL_ARGS_ASSERT_DO_TRANS_COMPLEX;
148 Perl_croak(aTHX_ "panic: do_trans_complex line %d",__LINE__);
152 U8 * const dstart = d;
154 if (PL_op->op_private & OPpTRANS_SQUASH) {
157 const I32 ch = tbl[*s];
161 if (p != d - 1 || *p != *d)
164 else if (ch == -1) /* -1 is unmapped character */
166 else if (ch == -2) /* -2 is delete character */
173 const I32 ch = tbl[*s];
178 else if (ch == -1) /* -1 is unmapped character */
180 else if (ch == -2) /* -2 is delete character */
186 SvCUR_set(sv, d - dstart);
189 const I32 complement = PL_op->op_private & OPpTRANS_COMPLEMENT;
190 const I32 grows = PL_op->op_private & OPpTRANS_GROWS;
191 const I32 del = PL_op->op_private & OPpTRANS_DELETE;
197 Newx(d, len*2+1, U8);
201 if (complement && !del)
204 #ifdef MACOS_TRADITIONAL
205 #define comp CoMP /* "comp" is a keyword in some compilers ... */
208 if (PL_op->op_private & OPpTRANS_SQUASH) {
212 const UV comp = utf8n_to_uvchr(s, send - s, &len,
224 ch = (rlen == 0) ? (I32)comp :
225 (comp - 0x100 < rlen) ?
226 tbl[comp+1] : tbl[0x100+rlen];
228 d = uvchr_to_utf8(d, ch);
236 else if ((ch = tbl[comp]) >= 0) {
239 d = uvchr_to_utf8(d, ch);
245 else if (ch == -1) { /* -1 is unmapped character */
249 else if (ch == -2) /* -2 is delete character */
258 const UV comp = utf8n_to_uvchr(s, send - s, &len,
269 if (comp - 0x100 < rlen)
270 d = uvchr_to_utf8(d, tbl[comp+1]);
272 d = uvchr_to_utf8(d, tbl[0x100+rlen]);
276 else if ((ch = tbl[comp]) >= 0) {
277 d = uvchr_to_utf8(d, ch);
280 else if (ch == -1) { /* -1 is unmapped character */
284 else if (ch == -2) /* -2 is delete character */
290 sv_setpvn(sv, (char*)dstart, d - dstart);
295 SvCUR_set(sv, d - dstart);
304 S_do_trans_simple_utf8(pTHX_ SV * const sv)
313 const I32 grows = PL_op->op_private & OPpTRANS_GROWS;
317 PAD_SVl(cPADOP->op_padix);
321 HV* const hv = (HV*)SvRV(rv);
322 SV* const * svp = hv_fetchs(hv, "NONE", FALSE);
323 const UV none = svp ? SvUV(*svp) : 0x7fffffff;
324 const UV extra = none + 1;
328 PERL_ARGS_ASSERT_DO_TRANS_SIMPLE_UTF8;
330 s = (U8*)SvPV(sv, len);
333 const U8 * const e = s + len;
336 hibit = !NATIVE_IS_INVARIANT(ch);
338 s = bytes_to_utf8(s, &len);
346 svp = hv_fetchs(hv, "FINAL", FALSE);
351 /* d needs to be bigger than s, in case e.g. upgrading is required */
352 Newx(d, len * 3 + UTF8_MAXBYTES, U8);
362 const UV uv = swash_fetch(rv, s, TRUE);
366 d = uvuni_to_utf8(d, uv);
368 else if (uv == none) {
369 const int i = UTF8SKIP(s);
374 else if (uv == extra) {
377 d = uvuni_to_utf8(d, final);
383 const STRLEN clen = d - dstart;
384 const STRLEN nlen = dend - dstart + len + UTF8_MAXBYTES;
386 Perl_croak(aTHX_ "panic: do_trans_simple_utf8 line %d",__LINE__);
387 Renew(dstart, nlen + UTF8_MAXBYTES, U8);
389 dend = dstart + nlen;
392 if (grows || hibit) {
393 sv_setpvn(sv, (char*)dstart, d - dstart);
400 SvCUR_set(sv, d - dstart);
409 S_do_trans_count_utf8(pTHX_ SV * const sv)
413 const U8 *start = NULL;
419 PAD_SVl(cPADOP->op_padix);
423 HV* const hv = (HV*)SvRV(rv);
424 SV* const * const svp = hv_fetchs(hv, "NONE", FALSE);
425 const UV none = svp ? SvUV(*svp) : 0x7fffffff;
426 const UV extra = none + 1;
429 PERL_ARGS_ASSERT_DO_TRANS_COUNT_UTF8;
431 s = (const U8*)SvPV_const(sv, len);
434 const U8 * const e = s + len;
437 hibit = !NATIVE_IS_INVARIANT(ch);
439 start = s = bytes_to_utf8(s, &len);
447 const UV uv = swash_fetch(rv, s, TRUE);
448 if (uv < none || uv == extra)
459 S_do_trans_complex_utf8(pTHX_ SV * const sv)
465 const I32 squash = PL_op->op_private & OPpTRANS_SQUASH;
466 const I32 del = PL_op->op_private & OPpTRANS_DELETE;
467 const I32 grows = PL_op->op_private & OPpTRANS_GROWS;
470 PAD_SVl(cPADOP->op_padix);
474 HV * const hv = (HV*)SvRV(rv);
475 SV * const *svp = hv_fetchs(hv, "NONE", FALSE);
476 const UV none = svp ? SvUV(*svp) : 0x7fffffff;
477 const UV extra = none + 1;
479 bool havefinal = FALSE;
483 U8 *s = (U8*)SvPV(sv, len);
485 PERL_ARGS_ASSERT_DO_TRANS_COMPLEX_UTF8;
489 const U8 * const e = s + len;
492 hibit = !NATIVE_IS_INVARIANT(ch);
494 s = bytes_to_utf8(s, &len);
502 svp = hv_fetchs(hv, "FINAL", FALSE);
509 /* d needs to be bigger than s, in case e.g. upgrading is required */
510 Newx(d, len * 3 + UTF8_MAXBYTES, U8);
522 UV uv = swash_fetch(rv, s, TRUE);
525 const STRLEN clen = d - dstart;
526 const STRLEN nlen = dend - dstart + len + UTF8_MAXBYTES;
528 Perl_croak(aTHX_ "panic: do_trans_complex_utf8 line %d",__LINE__);
529 Renew(dstart, nlen + UTF8_MAXBYTES, U8);
531 dend = dstart + nlen;
537 d = uvuni_to_utf8(d, uv);
542 else if (uv == none) { /* "none" is unmapped character */
543 const int i = UTF8SKIP(s);
550 else if (uv == extra && !del) {
555 d = uvuni_to_utf8(d, final);
561 uv = utf8n_to_uvuni(s, send - s, &len, UTF8_ALLOW_DEFAULT);
571 matches++; /* "none+1" is delete character */
577 const UV uv = swash_fetch(rv, s, TRUE);
579 const STRLEN clen = d - dstart;
580 const STRLEN nlen = dend - dstart + len + UTF8_MAXBYTES;
582 Perl_croak(aTHX_ "panic: do_trans_complex_utf8 line %d",__LINE__);
583 Renew(dstart, nlen + UTF8_MAXBYTES, U8);
585 dend = dstart + nlen;
590 d = uvuni_to_utf8(d, uv);
593 else if (uv == none) { /* "none" is unmapped character */
594 const int i = UTF8SKIP(s);
600 else if (uv == extra && !del) {
603 d = uvuni_to_utf8(d, final);
606 matches++; /* "none+1" is delete character */
610 if (grows || hibit) {
611 sv_setpvn(sv, (char*)dstart, d - dstart);
618 SvCUR_set(sv, d - dstart);
627 Perl_do_trans(pTHX_ SV *sv)
631 const I32 hasutf = (PL_op->op_private &
632 (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF));
634 PERL_ARGS_ASSERT_DO_TRANS;
636 if (SvREADONLY(sv) && !(PL_op->op_private & OPpTRANS_IDENTICAL)) {
638 sv_force_normal_flags(sv, 0);
640 Perl_croak(aTHX_ PL_no_modify);
642 (void)SvPV_const(sv, len);
645 if (!(PL_op->op_private & OPpTRANS_IDENTICAL)) {
647 (void)SvPV_force(sv, len);
648 (void)SvPOK_only_UTF8(sv);
651 DEBUG_t( Perl_deb(aTHX_ "2.TBL\n"));
653 switch (PL_op->op_private & ~hasutf & (
654 OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF|OPpTRANS_IDENTICAL|
655 OPpTRANS_SQUASH|OPpTRANS_DELETE|OPpTRANS_COMPLEMENT)) {
658 return do_trans_simple_utf8(sv);
660 return do_trans_simple(sv);
662 case OPpTRANS_IDENTICAL:
663 case OPpTRANS_IDENTICAL|OPpTRANS_COMPLEMENT:
665 return do_trans_count_utf8(sv);
667 return do_trans_count(sv);
671 return do_trans_complex_utf8(sv);
673 return do_trans_complex(sv);
678 Perl_do_join(pTHX_ register SV *sv, SV *delim, register SV **mark, register SV **sp)
681 SV ** const oldmark = mark;
682 register I32 items = sp - mark;
686 PERL_ARGS_ASSERT_DO_JOIN;
688 (void) SvPV_const(delim, delimlen); /* stringify and get the delimlen */
689 /* SvCUR assumes it's SvPOK() and woe betide you if it's not. */
692 len = (items > 0 ? (delimlen * (items - 1) ) : 0);
693 SvUPGRADE(sv, SVt_PV);
694 if (SvLEN(sv) < len + items) { /* current length is way too short */
695 while (items-- > 0) {
696 if (*mark && !SvGAMAGIC(*mark) && SvOK(*mark)) {
698 SvPV_const(*mark, tmplen);
703 SvGROW(sv, len + 1); /* so try to pre-extend */
710 sv_setpvn(sv, "", 0);
711 /* sv_setpv retains old UTF8ness [perl #24846] */
714 if (PL_tainting && SvMAGICAL(sv))
724 for (; items > 0; items--,mark++) {
730 for (; items > 0; items--,mark++)
737 Perl_do_sprintf(pTHX_ SV *sv, I32 len, SV **sarg)
741 const char * const pat = SvPV_const(*sarg, patlen);
742 bool do_taint = FALSE;
744 PERL_ARGS_ASSERT_DO_SPRINTF;
749 sv_vsetpvfn(sv, pat, patlen, NULL, sarg + 1, len - 1, &do_taint);
755 /* currently converts input to bytes if possible, but doesn't sweat failure */
757 Perl_do_vecget(pTHX_ SV *sv, I32 offset, I32 size)
760 STRLEN srclen, len, uoffset, bitoffs = 0;
761 const unsigned char *s = (const unsigned char *) SvPV_const(sv, srclen);
764 PERL_ARGS_ASSERT_DO_VECGET;
768 if (size < 1 || (size & (size-1))) /* size < 1 or not a power of two */
769 Perl_croak(aTHX_ "Illegal number of bits in vec");
772 (void) Perl_sv_utf8_downgrade(aTHX_ sv, TRUE);
775 bitoffs = ((offset%8)*size)%8;
776 uoffset = offset/(8/size);
779 uoffset = offset*(size/8);
783 len = uoffset + (bitoffs + size + 7)/8; /* required number of bytes */
789 if (uoffset >= srclen)
792 retnum = (UV) s[uoffset] << 8;
794 else if (size == 32) {
795 if (uoffset >= srclen)
797 else if (uoffset + 1 >= srclen)
799 ((UV) s[uoffset ] << 24);
800 else if (uoffset + 2 >= srclen)
802 ((UV) s[uoffset ] << 24) +
803 ((UV) s[uoffset + 1] << 16);
806 ((UV) s[uoffset ] << 24) +
807 ((UV) s[uoffset + 1] << 16) +
808 ( s[uoffset + 2] << 8);
811 else if (size == 64) {
812 if (ckWARN(WARN_PORTABLE))
813 Perl_warner(aTHX_ packWARN(WARN_PORTABLE),
814 "Bit vector size > 32 non-portable");
815 if (uoffset >= srclen)
817 else if (uoffset + 1 >= srclen)
819 (UV) s[uoffset ] << 56;
820 else if (uoffset + 2 >= srclen)
822 ((UV) s[uoffset ] << 56) +
823 ((UV) s[uoffset + 1] << 48);
824 else if (uoffset + 3 >= srclen)
826 ((UV) s[uoffset ] << 56) +
827 ((UV) s[uoffset + 1] << 48) +
828 ((UV) s[uoffset + 2] << 40);
829 else if (uoffset + 4 >= srclen)
831 ((UV) s[uoffset ] << 56) +
832 ((UV) s[uoffset + 1] << 48) +
833 ((UV) s[uoffset + 2] << 40) +
834 ((UV) s[uoffset + 3] << 32);
835 else if (uoffset + 5 >= srclen)
837 ((UV) s[uoffset ] << 56) +
838 ((UV) s[uoffset + 1] << 48) +
839 ((UV) s[uoffset + 2] << 40) +
840 ((UV) s[uoffset + 3] << 32) +
841 ( s[uoffset + 4] << 24);
842 else if (uoffset + 6 >= srclen)
844 ((UV) s[uoffset ] << 56) +
845 ((UV) s[uoffset + 1] << 48) +
846 ((UV) s[uoffset + 2] << 40) +
847 ((UV) s[uoffset + 3] << 32) +
848 ((UV) s[uoffset + 4] << 24) +
849 ((UV) s[uoffset + 5] << 16);
852 ((UV) s[uoffset ] << 56) +
853 ((UV) s[uoffset + 1] << 48) +
854 ((UV) s[uoffset + 2] << 40) +
855 ((UV) s[uoffset + 3] << 32) +
856 ((UV) s[uoffset + 4] << 24) +
857 ((UV) s[uoffset + 5] << 16) +
858 ( s[uoffset + 6] << 8);
864 retnum = (s[uoffset] >> bitoffs) & ((1 << size) - 1);
870 ((UV) s[uoffset] << 8) +
874 ((UV) s[uoffset ] << 24) +
875 ((UV) s[uoffset + 1] << 16) +
876 ( s[uoffset + 2] << 8) +
879 else if (size == 64) {
880 if (ckWARN(WARN_PORTABLE))
881 Perl_warner(aTHX_ packWARN(WARN_PORTABLE),
882 "Bit vector size > 32 non-portable");
884 ((UV) s[uoffset ] << 56) +
885 ((UV) s[uoffset + 1] << 48) +
886 ((UV) s[uoffset + 2] << 40) +
887 ((UV) s[uoffset + 3] << 32) +
888 ((UV) s[uoffset + 4] << 24) +
889 ((UV) s[uoffset + 5] << 16) +
890 ( s[uoffset + 6] << 8) +
899 /* currently converts input to bytes if possible but doesn't sweat failures,
900 * although it does ensure that the string it clobbers is not marked as
901 * utf8-valid any more
904 Perl_do_vecset(pTHX_ SV *sv)
907 register I32 offset, bitoffs = 0;
909 register unsigned char *s;
914 SV * const targ = LvTARG(sv);
916 PERL_ARGS_ASSERT_DO_VECSET;
920 s = (unsigned char*)SvPV_force(targ, targlen);
922 /* This is handled by the SvPOK_only below...
923 if (!Perl_sv_utf8_downgrade(aTHX_ targ, TRUE))
926 (void) Perl_sv_utf8_downgrade(aTHX_ targ, TRUE);
929 (void)SvPOK_only(targ);
931 offset = LvTARGOFF(sv);
933 Perl_croak(aTHX_ "Negative offset to vec in lvalue context");
934 size = LvTARGLEN(sv);
935 if (size < 1 || (size & (size-1))) /* size < 1 or not a power of two */
936 Perl_croak(aTHX_ "Illegal number of bits in vec");
939 bitoffs = ((offset%8)*size)%8;
945 len = offset + (bitoffs + size + 7)/8; /* required number of bytes */
947 s = (unsigned char*)SvGROW(targ, len + 1);
948 (void)memzero((char *)(s + targlen), len - targlen + 1);
949 SvCUR_set(targ, len);
953 mask = (1 << size) - 1;
955 s[offset] &= ~(mask << bitoffs);
956 s[offset] |= lval << bitoffs;
960 s[offset ] = (U8)( lval & 0xff);
961 else if (size == 16) {
962 s[offset ] = (U8)((lval >> 8) & 0xff);
963 s[offset+1] = (U8)( lval & 0xff);
965 else if (size == 32) {
966 s[offset ] = (U8)((lval >> 24) & 0xff);
967 s[offset+1] = (U8)((lval >> 16) & 0xff);
968 s[offset+2] = (U8)((lval >> 8) & 0xff);
969 s[offset+3] = (U8)( lval & 0xff);
972 else if (size == 64) {
973 if (ckWARN(WARN_PORTABLE))
974 Perl_warner(aTHX_ packWARN(WARN_PORTABLE),
975 "Bit vector size > 32 non-portable");
976 s[offset ] = (U8)((lval >> 56) & 0xff);
977 s[offset+1] = (U8)((lval >> 48) & 0xff);
978 s[offset+2] = (U8)((lval >> 40) & 0xff);
979 s[offset+3] = (U8)((lval >> 32) & 0xff);
980 s[offset+4] = (U8)((lval >> 24) & 0xff);
981 s[offset+5] = (U8)((lval >> 16) & 0xff);
982 s[offset+6] = (U8)((lval >> 8) & 0xff);
983 s[offset+7] = (U8)( lval & 0xff);
991 Perl_do_chop(pTHX_ register SV *astr, register SV *sv)
997 PERL_ARGS_ASSERT_DO_CHOP;
999 if (SvTYPE(sv) == SVt_PVAV) {
1001 AV* const av = (AV*)sv;
1002 const I32 max = AvFILL(av);
1004 for (i = 0; i <= max; i++) {
1005 sv = (SV*)av_fetch(av, i, FALSE);
1006 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
1011 else if (SvTYPE(sv) == SVt_PVHV) {
1012 HV* const hv = (HV*)sv;
1014 (void)hv_iterinit(hv);
1015 while ((entry = hv_iternext(hv)))
1016 do_chop(astr,hv_iterval(hv,entry));
1019 else if (SvREADONLY(sv)) {
1021 /* SV is copy-on-write */
1022 sv_force_normal_flags(sv, 0);
1025 Perl_croak(aTHX_ PL_no_modify);
1028 if (PL_encoding && !SvUTF8(sv)) {
1029 /* like in do_chomp(), utf8-ize the sv as a side-effect
1030 * if we're using encoding. */
1031 sv_recode_to_utf8(sv, PL_encoding);
1035 if (len && !SvPOK(sv))
1036 s = SvPV_force_nomg(sv, len);
1039 char * const send = s + len;
1040 char * const start = s;
1042 while (s > start && UTF8_IS_CONTINUATION(*s))
1044 if (is_utf8_string((U8*)s, send - s)) {
1045 sv_setpvn(astr, s, send - s);
1047 SvCUR_set(sv, s - start);
1053 sv_setpvn(astr, "", 0);
1055 else if (s && len) {
1057 sv_setpvn(astr, s, 1);
1064 sv_setpvn(astr, "", 0);
1069 Perl_do_chomp(pTHX_ register SV *sv)
1075 char *temp_buffer = NULL;
1076 SV* svrecode = NULL;
1078 PERL_ARGS_ASSERT_DO_CHOMP;
1082 if (RsRECORD(PL_rs))
1085 if (SvTYPE(sv) == SVt_PVAV) {
1087 AV* const av = (AV*)sv;
1088 const I32 max = AvFILL(av);
1090 for (i = 0; i <= max; i++) {
1091 sv = (SV*)av_fetch(av, i, FALSE);
1092 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
1093 count += do_chomp(sv);
1097 else if (SvTYPE(sv) == SVt_PVHV) {
1098 HV* const hv = (HV*)sv;
1100 (void)hv_iterinit(hv);
1101 while ((entry = hv_iternext(hv)))
1102 count += do_chomp(hv_iterval(hv,entry));
1105 else if (SvREADONLY(sv)) {
1107 /* SV is copy-on-write */
1108 sv_force_normal_flags(sv, 0);
1111 Perl_croak(aTHX_ PL_no_modify);
1116 /* XXX, here sv is utf8-ized as a side-effect!
1117 If encoding.pm is used properly, almost string-generating
1118 operations, including literal strings, chr(), input data, etc.
1119 should have been utf8-ized already, right?
1121 sv_recode_to_utf8(sv, PL_encoding);
1128 if (RsPARA(PL_rs)) {
1132 while (len && s[-1] == '\n') {
1139 STRLEN rslen, rs_charlen;
1140 const char *rsptr = SvPV_const(PL_rs, rslen);
1142 rs_charlen = SvUTF8(PL_rs)
1143 ? sv_len_utf8(PL_rs)
1146 if (SvUTF8(PL_rs) != SvUTF8(sv)) {
1147 /* Assumption is that rs is shorter than the scalar. */
1148 if (SvUTF8(PL_rs)) {
1149 /* RS is utf8, scalar is 8 bit. */
1150 bool is_utf8 = TRUE;
1151 temp_buffer = (char*)bytes_from_utf8((U8*)rsptr,
1154 /* Cannot downgrade, therefore cannot possibly match
1156 assert (temp_buffer == rsptr);
1160 rsptr = temp_buffer;
1162 else if (PL_encoding) {
1163 /* RS is 8 bit, encoding.pm is used.
1164 * Do not recode PL_rs as a side-effect. */
1165 svrecode = newSVpvn(rsptr, rslen);
1166 sv_recode_to_utf8(svrecode, PL_encoding);
1167 rsptr = SvPV_const(svrecode, rslen);
1168 rs_charlen = sv_len_utf8(svrecode);
1171 /* RS is 8 bit, scalar is utf8. */
1172 temp_buffer = (char*)bytes_to_utf8((U8*)rsptr, &rslen);
1173 rsptr = temp_buffer;
1182 if (len < rslen - 1)
1186 if (memNE(s, rsptr, rslen))
1188 count += rs_charlen;
1191 s = SvPV_force_nolen(sv);
1200 SvREFCNT_dec(svrecode);
1202 Safefree(temp_buffer);
1207 Perl_do_vop(pTHX_ I32 optype, SV *sv, SV *left, SV *right)
1218 register const char *lc;
1219 register const char *rc;
1220 register STRLEN len;
1228 PERL_ARGS_ASSERT_DO_VOP;
1230 if (sv != left || (optype != OP_BIT_AND && !SvOK(sv) && !SvGMAGICAL(sv)))
1231 sv_setpvn(sv, "", 0); /* avoid undef warning on |= and ^= */
1232 lsave = lc = SvPV_nomg_const(left, leftlen);
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 = (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) != (SV*)keys) {
1470 SvREFCNT_dec(LvTARG(TARG));
1471 LvTARG(TARG) = SvREFCNT_inc_simple(keys);
1477 if (! SvTIED_mg((SV*)keys, PERL_MAGIC_tied) )
1483 while (hv_iternext(keys)) i++;
1489 EXTEND(SP, HvKEYS(keys) * (dokeys + dovalues));
1491 PUTBACK; /* hv_iternext and hv_iterval might clobber stack_sp */
1492 while ((entry = hv_iternext(keys))) {
1495 SV* const sv = hv_iterkeysv(entry);
1496 XPUSHs(sv); /* won't clobber stack_sp */
1501 tmpstr = hv_iterval(hv,entry);
1502 DEBUG_H(Perl_sv_setpvf(aTHX_ tmpstr, "%lu%%%d=%lu",
1503 (unsigned long)HeHASH(entry),
1505 (unsigned long)(HeHASH(entry) & HvMAX(keys))));
1516 * c-indentation-style: bsd
1518 * indent-tabs-mode: t
1521 * ex: set ts=8 sts=4 sw=4 noet: