3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2004, 2005, 2006, by Larry Wall and others
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
12 * "'So that was the job I felt I had to do when I started,' thought Sam."
15 /* This file contains some common functions needed to carry out certain
16 * ops. For example both pp_schomp() and pp_chomp() - scalar and array
17 * chomp operations - call the function do_chomp() found in this file.
21 #define PERL_IN_DOOP_C
29 S_do_trans_simple(pTHX_ SV *sv)
37 const I32 grows = PL_op->op_private & OPpTRANS_GROWS;
40 const short * const tbl = (short*)cPVOP->op_pv;
42 Perl_croak(aTHX_ "panic: do_trans_simple line %d",__LINE__);
44 s = (U8*)SvPV(sv, len);
47 /* First, take care of non-UTF-8 input strings, because they're easy */
50 const I32 ch = tbl[*s];
61 /* Allow for expansion: $_="a".chr(400); tr/a/\xFE/, FE needs encoding */
71 /* Need to check this, otherwise 128..255 won't match */
72 const UV c = utf8n_to_uvchr(s, send - s, &ulen, 0);
73 if (c < 0x100 && (ch = tbl[c]) >= 0) {
75 d = uvchr_to_utf8(d, ch);
78 else { /* No match -> copy */
85 sv_setpvn(sv, (char*)dstart, d - dstart);
90 SvCUR_set(sv, d - dstart);
98 S_do_trans_count(pTHX_ SV *sv)
106 const short * const tbl = (short*)cPVOP->op_pv;
108 Perl_croak(aTHX_ "panic: do_trans_count line %d",__LINE__);
110 s = (const U8*)SvPV_const(sv, len);
119 const I32 complement = PL_op->op_private & OPpTRANS_COMPLEMENT;
122 const UV c = utf8n_to_uvchr(s, send - s, &ulen, 0);
126 } else if (complement)
136 S_do_trans_complex(pTHX_ SV *sv)
145 STRLEN len, rlen = 0;
147 const short * const tbl = (short*)cPVOP->op_pv;
149 Perl_croak(aTHX_ "panic: do_trans_complex line %d",__LINE__);
151 s = (U8*)SvPV(sv, len);
157 if (PL_op->op_private & OPpTRANS_SQUASH) {
160 const I32 ch = tbl[*s];
164 if (p != d - 1 || *p != *d)
167 else if (ch == -1) /* -1 is unmapped character */
169 else if (ch == -2) /* -2 is delete character */
176 const I32 ch = tbl[*s];
181 else if (ch == -1) /* -1 is unmapped character */
183 else if (ch == -2) /* -2 is delete character */
189 SvCUR_set(sv, d - dstart);
192 const I32 complement = PL_op->op_private & OPpTRANS_COMPLEMENT;
193 const I32 grows = PL_op->op_private & OPpTRANS_GROWS;
194 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 = utf8_to_uvchr(s, &len);
223 ch = (rlen == 0) ? comp :
224 (comp - 0x100 < rlen) ?
225 tbl[comp+1] : tbl[0x100+rlen];
227 d = uvchr_to_utf8(d, ch);
235 else if ((ch = tbl[comp]) >= 0) {
238 d = uvchr_to_utf8(d, ch);
244 else if (ch == -1) { /* -1 is unmapped character */
248 else if (ch == -2) /* -2 is delete character */
257 const UV comp = utf8_to_uvchr(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 *sv)
311 const I32 grows = PL_op->op_private & OPpTRANS_GROWS;
314 SV* const rv = (SV*)cSVOP->op_sv;
315 HV* const hv = (HV*)SvRV(rv);
316 SV* const * svp = hv_fetchs(hv, "NONE", FALSE);
317 const UV none = svp ? SvUV(*svp) : 0x7fffffff;
318 const UV extra = none + 1;
323 s = (U8*)SvPV(sv, len);
327 const U8 * const e = s + len;
330 hibit = !NATIVE_IS_INVARIANT(ch);
332 s = bytes_to_utf8(s, &len);
340 svp = hv_fetchs(hv, "FINAL", FALSE);
345 /* d needs to be bigger than s, in case e.g. upgrading is required */
346 Newx(d, len * 3 + UTF8_MAXBYTES, U8);
356 const UV uv = swash_fetch(rv, s, TRUE);
360 d = uvuni_to_utf8(d, uv);
362 else if (uv == none) {
363 const int i = UTF8SKIP(s);
368 else if (uv == extra) {
371 d = uvuni_to_utf8(d, final);
377 const STRLEN clen = d - dstart;
378 const STRLEN nlen = dend - dstart + len + UTF8_MAXBYTES;
380 Perl_croak(aTHX_ "panic: do_trans_simple_utf8 line %d",__LINE__);
381 Renew(dstart, nlen + UTF8_MAXBYTES, U8);
383 dend = dstart + nlen;
386 if (grows || hibit) {
387 sv_setpvn(sv, (char*)dstart, d - dstart);
394 SvCUR_set(sv, d - dstart);
403 S_do_trans_count_utf8(pTHX_ SV *sv)
407 const U8 *start = NULL;
412 SV* const rv = (SV*)cSVOP->op_sv;
413 HV* const hv = (HV*)SvRV(rv);
414 SV* const * const svp = hv_fetchs(hv, "NONE", FALSE);
415 const UV none = svp ? SvUV(*svp) : 0x7fffffff;
416 const UV extra = none + 1;
419 s = (const U8*)SvPV_const(sv, len);
422 const U8 * const e = s + len;
425 hibit = !NATIVE_IS_INVARIANT(ch);
427 start = s = bytes_to_utf8(s, &len);
435 const UV uv = swash_fetch(rv, s, TRUE);
436 if (uv < none || uv == extra)
447 S_do_trans_complex_utf8(pTHX_ SV *sv)
453 const I32 squash = PL_op->op_private & OPpTRANS_SQUASH;
454 const I32 del = PL_op->op_private & OPpTRANS_DELETE;
455 const I32 grows = PL_op->op_private & OPpTRANS_GROWS;
456 SV * const rv = (SV*)cSVOP->op_sv;
457 HV * const hv = (HV*)SvRV(rv);
458 SV * const *svp = hv_fetchs(hv, "NONE", FALSE);
459 const UV none = svp ? SvUV(*svp) : 0x7fffffff;
460 const UV extra = none + 1;
462 bool havefinal = FALSE;
467 U8 *s = (U8*)SvPV(sv, len);
468 const I32 isutf8 = SvUTF8(sv);
471 const U8 * const e = s + len;
474 hibit = !NATIVE_IS_INVARIANT(ch);
476 s = bytes_to_utf8(s, &len);
484 svp = hv_fetchs(hv, "FINAL", FALSE);
491 /* d needs to be bigger than s, in case e.g. upgrading is required */
492 Newx(d, len * 3 + UTF8_MAXBYTES, U8);
504 UV uv = swash_fetch(rv, s, TRUE);
507 const STRLEN clen = d - dstart;
508 const STRLEN nlen = dend - dstart + len + UTF8_MAXBYTES;
510 Perl_croak(aTHX_ "panic: do_trans_complex_utf8 line %d",__LINE__);
511 Renew(dstart, nlen + UTF8_MAXBYTES, U8);
513 dend = dstart + nlen;
519 d = uvuni_to_utf8(d, uv);
524 else if (uv == none) { /* "none" is unmapped character */
525 const int i = UTF8SKIP(s);
532 else if (uv == extra && !del) {
537 d = uvuni_to_utf8(d, final);
543 uv = utf8_to_uvuni(s, &len);
553 matches++; /* "none+1" is delete character */
559 const UV uv = swash_fetch(rv, s, TRUE);
561 const STRLEN clen = d - dstart;
562 const STRLEN nlen = dend - dstart + len + UTF8_MAXBYTES;
564 Perl_croak(aTHX_ "panic: do_trans_complex_utf8 line %d",__LINE__);
565 Renew(dstart, nlen + UTF8_MAXBYTES, U8);
567 dend = dstart + nlen;
572 d = uvuni_to_utf8(d, uv);
575 else if (uv == none) { /* "none" is unmapped character */
576 const int i = UTF8SKIP(s);
582 else if (uv == extra && !del) {
585 d = uvuni_to_utf8(d, final);
588 matches++; /* "none+1" is delete character */
592 if (grows || hibit) {
593 sv_setpvn(sv, (char*)dstart, d - dstart);
600 SvCUR_set(sv, d - dstart);
609 Perl_do_trans(pTHX_ SV *sv)
613 const I32 hasutf = (PL_op->op_private &
614 (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF));
616 if (SvREADONLY(sv)) {
618 sv_force_normal_flags(sv, 0);
619 if (SvREADONLY(sv) && !(PL_op->op_private & OPpTRANS_IDENTICAL))
620 Perl_croak(aTHX_ PL_no_modify);
622 (void)SvPV_const(sv, len);
625 if (!(PL_op->op_private & OPpTRANS_IDENTICAL)) {
627 (void)SvPV_force(sv, len);
628 (void)SvPOK_only_UTF8(sv);
631 DEBUG_t( Perl_deb(aTHX_ "2.TBL\n"));
633 switch (PL_op->op_private & ~hasutf & (
634 OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF|OPpTRANS_IDENTICAL|
635 OPpTRANS_SQUASH|OPpTRANS_DELETE|OPpTRANS_COMPLEMENT)) {
638 return do_trans_simple_utf8(sv);
640 return do_trans_simple(sv);
642 case OPpTRANS_IDENTICAL:
643 case OPpTRANS_IDENTICAL|OPpTRANS_COMPLEMENT:
645 return do_trans_count_utf8(sv);
647 return do_trans_count(sv);
651 return do_trans_complex_utf8(sv);
653 return do_trans_complex(sv);
658 Perl_do_join(pTHX_ register SV *sv, SV *del, register SV **mark, register SV **sp)
661 SV ** const oldmark = mark;
662 register I32 items = sp - mark;
666 (void) SvPV_const(del, delimlen); /* stringify and get the delimlen */
667 /* SvCUR assumes it's SvPOK() and woe betide you if it's not. */
670 len = (items > 0 ? (delimlen * (items - 1) ) : 0);
671 SvUPGRADE(sv, SVt_PV);
672 if (SvLEN(sv) < len + items) { /* current length is way too short */
673 while (items-- > 0) {
674 if (*mark && !SvGAMAGIC(*mark) && SvOK(*mark)) {
676 SvPV_const(*mark, tmplen);
681 SvGROW(sv, len + 1); /* so try to pre-extend */
688 sv_setpvn(sv, "", 0);
689 /* sv_setpv retains old UTF8ness [perl #24846] */
692 if (PL_tainting && SvMAGICAL(sv))
702 for (; items > 0; items--,mark++) {
708 for (; items > 0; items--,mark++)
715 Perl_do_sprintf(pTHX_ SV *sv, I32 len, SV **sarg)
719 const char * const pat = SvPV_const(*sarg, patlen);
720 bool do_taint = FALSE;
725 sv_vsetpvfn(sv, pat, patlen, Null(va_list*), sarg + 1, len - 1, &do_taint);
731 /* currently converts input to bytes if possible, but doesn't sweat failure */
733 Perl_do_vecget(pTHX_ SV *sv, I32 offset, I32 size)
737 const unsigned char *s = (const unsigned char *) SvPV_const(sv, srclen);
742 if (size < 1 || (size & (size-1))) /* size < 1 or not a power of two */
743 Perl_croak(aTHX_ "Illegal number of bits in vec");
746 (void) Perl_sv_utf8_downgrade(aTHX_ sv, TRUE);
748 offset *= size; /* turn into bit offset */
749 len = (offset + size + 7) / 8; /* required number of bytes */
754 offset >>= 3; /* turn into byte offset */
756 if ((STRLEN)offset >= srclen)
759 retnum = (UV) s[offset] << 8;
761 else if (size == 32) {
762 if ((STRLEN)offset >= srclen)
764 else if ((STRLEN)(offset + 1) >= srclen)
766 ((UV) s[offset ] << 24);
767 else if ((STRLEN)(offset + 2) >= srclen)
769 ((UV) s[offset ] << 24) +
770 ((UV) s[offset + 1] << 16);
773 ((UV) s[offset ] << 24) +
774 ((UV) s[offset + 1] << 16) +
775 ( s[offset + 2] << 8);
778 else if (size == 64) {
779 if (ckWARN(WARN_PORTABLE))
780 Perl_warner(aTHX_ packWARN(WARN_PORTABLE),
781 "Bit vector size > 32 non-portable");
782 if (offset >= srclen)
784 else if (offset + 1 >= srclen)
786 (UV) s[offset ] << 56;
787 else if (offset + 2 >= srclen)
789 ((UV) s[offset ] << 56) +
790 ((UV) s[offset + 1] << 48);
791 else if (offset + 3 >= srclen)
793 ((UV) s[offset ] << 56) +
794 ((UV) s[offset + 1] << 48) +
795 ((UV) s[offset + 2] << 40);
796 else if (offset + 4 >= srclen)
798 ((UV) s[offset ] << 56) +
799 ((UV) s[offset + 1] << 48) +
800 ((UV) s[offset + 2] << 40) +
801 ((UV) s[offset + 3] << 32);
802 else if (offset + 5 >= srclen)
804 ((UV) s[offset ] << 56) +
805 ((UV) s[offset + 1] << 48) +
806 ((UV) s[offset + 2] << 40) +
807 ((UV) s[offset + 3] << 32) +
808 ( s[offset + 4] << 24);
809 else if (offset + 6 >= srclen)
811 ((UV) s[offset ] << 56) +
812 ((UV) s[offset + 1] << 48) +
813 ((UV) s[offset + 2] << 40) +
814 ((UV) s[offset + 3] << 32) +
815 ((UV) s[offset + 4] << 24) +
816 ((UV) s[offset + 5] << 16);
819 ((UV) s[offset ] << 56) +
820 ((UV) s[offset + 1] << 48) +
821 ((UV) s[offset + 2] << 40) +
822 ((UV) s[offset + 3] << 32) +
823 ((UV) s[offset + 4] << 24) +
824 ((UV) s[offset + 5] << 16) +
825 ( s[offset + 6] << 8);
831 retnum = (s[offset >> 3] >> (offset & 7)) & ((1 << size) - 1);
833 offset >>= 3; /* turn into byte offset */
838 ((UV) s[offset] << 8) +
842 ((UV) s[offset ] << 24) +
843 ((UV) s[offset + 1] << 16) +
844 ( s[offset + 2] << 8) +
847 else if (size == 64) {
848 if (ckWARN(WARN_PORTABLE))
849 Perl_warner(aTHX_ packWARN(WARN_PORTABLE),
850 "Bit vector size > 32 non-portable");
852 ((UV) s[offset ] << 56) +
853 ((UV) s[offset + 1] << 48) +
854 ((UV) s[offset + 2] << 40) +
855 ((UV) s[offset + 3] << 32) +
856 ((UV) s[offset + 4] << 24) +
857 ((UV) s[offset + 5] << 16) +
858 ( s[offset + 6] << 8) +
867 /* currently converts input to bytes if possible but doesn't sweat failures,
868 * although it does ensure that the string it clobbers is not marked as
869 * utf8-valid any more
872 Perl_do_vecset(pTHX_ SV *sv)
877 register unsigned char *s;
882 SV * const targ = LvTARG(sv);
886 s = (unsigned char*)SvPV_force(targ, targlen);
888 /* This is handled by the SvPOK_only below...
889 if (!Perl_sv_utf8_downgrade(aTHX_ targ, TRUE))
892 (void) Perl_sv_utf8_downgrade(aTHX_ targ, TRUE);
895 (void)SvPOK_only(targ);
897 offset = LvTARGOFF(sv);
899 Perl_croak(aTHX_ "Negative offset to vec in lvalue context");
900 size = LvTARGLEN(sv);
901 if (size < 1 || (size & (size-1))) /* size < 1 or not a power of two */
902 Perl_croak(aTHX_ "Illegal number of bits in vec");
904 offset *= size; /* turn into bit offset */
905 len = (offset + size + 7) / 8; /* required number of bytes */
907 s = (unsigned char*)SvGROW(targ, len + 1);
908 (void)memzero((char *)(s + targlen), len - targlen + 1);
909 SvCUR_set(targ, len);
913 mask = (1 << size) - 1;
916 offset >>= 3; /* turn into byte offset */
917 s[offset] &= ~(mask << size);
918 s[offset] |= lval << size;
921 offset >>= 3; /* turn into byte offset */
923 s[offset ] = (U8)( lval & 0xff);
924 else if (size == 16) {
925 s[offset ] = (U8)((lval >> 8) & 0xff);
926 s[offset+1] = (U8)( lval & 0xff);
928 else if (size == 32) {
929 s[offset ] = (U8)((lval >> 24) & 0xff);
930 s[offset+1] = (U8)((lval >> 16) & 0xff);
931 s[offset+2] = (U8)((lval >> 8) & 0xff);
932 s[offset+3] = (U8)( lval & 0xff);
935 else if (size == 64) {
936 if (ckWARN(WARN_PORTABLE))
937 Perl_warner(aTHX_ packWARN(WARN_PORTABLE),
938 "Bit vector size > 32 non-portable");
939 s[offset ] = (U8)((lval >> 56) & 0xff);
940 s[offset+1] = (U8)((lval >> 48) & 0xff);
941 s[offset+2] = (U8)((lval >> 40) & 0xff);
942 s[offset+3] = (U8)((lval >> 32) & 0xff);
943 s[offset+4] = (U8)((lval >> 24) & 0xff);
944 s[offset+5] = (U8)((lval >> 16) & 0xff);
945 s[offset+6] = (U8)((lval >> 8) & 0xff);
946 s[offset+7] = (U8)( lval & 0xff);
954 Perl_do_chop(pTHX_ register SV *astr, register SV *sv)
960 if (SvTYPE(sv) == SVt_PVAV) {
962 AV* const av = (AV*)sv;
963 const I32 max = AvFILL(av);
965 for (i = 0; i <= max; i++) {
966 sv = (SV*)av_fetch(av, i, FALSE);
967 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
972 else if (SvTYPE(sv) == SVt_PVHV) {
973 HV* const hv = (HV*)sv;
975 (void)hv_iterinit(hv);
976 while ((entry = hv_iternext(hv)))
977 do_chop(astr,hv_iterval(hv,entry));
980 else if (SvREADONLY(sv)) {
982 /* SV is copy-on-write */
983 sv_force_normal_flags(sv, 0);
986 Perl_croak(aTHX_ PL_no_modify);
989 if (PL_encoding && !SvUTF8(sv)) {
990 /* like in do_chomp(), utf8-ize the sv as a side-effect
991 * if we're using encoding. */
992 sv_recode_to_utf8(sv, PL_encoding);
996 if (len && !SvPOK(sv))
997 s = SvPV_force(sv, len);
1000 char * const send = s + len;
1001 char * const start = s;
1003 while (s > start && UTF8_IS_CONTINUATION(*s))
1005 if (is_utf8_string((U8*)s, send - s)) {
1006 sv_setpvn(astr, s, send - s);
1008 SvCUR_set(sv, s - start);
1014 sv_setpvn(astr, "", 0);
1016 else if (s && len) {
1018 sv_setpvn(astr, s, 1);
1025 sv_setpvn(astr, "", 0);
1030 Perl_do_chomp(pTHX_ register SV *sv)
1036 char *temp_buffer = NULL;
1037 SV* svrecode = NULL;
1041 if (RsRECORD(PL_rs))
1044 if (SvTYPE(sv) == SVt_PVAV) {
1046 AV* const av = (AV*)sv;
1047 const I32 max = AvFILL(av);
1049 for (i = 0; i <= max; i++) {
1050 sv = (SV*)av_fetch(av, i, FALSE);
1051 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
1052 count += do_chomp(sv);
1056 else if (SvTYPE(sv) == SVt_PVHV) {
1057 HV* const hv = (HV*)sv;
1059 (void)hv_iterinit(hv);
1060 while ((entry = hv_iternext(hv)))
1061 count += do_chomp(hv_iterval(hv,entry));
1064 else if (SvREADONLY(sv)) {
1066 /* SV is copy-on-write */
1067 sv_force_normal_flags(sv, 0);
1070 Perl_croak(aTHX_ PL_no_modify);
1075 /* XXX, here sv is utf8-ized as a side-effect!
1076 If encoding.pm is used properly, almost string-generating
1077 operations, including literal strings, chr(), input data, etc.
1078 should have been utf8-ized already, right?
1080 sv_recode_to_utf8(sv, PL_encoding);
1087 if (RsPARA(PL_rs)) {
1091 while (len && s[-1] == '\n') {
1098 STRLEN rslen, rs_charlen;
1099 const char *rsptr = SvPV_const(PL_rs, rslen);
1101 rs_charlen = SvUTF8(PL_rs)
1102 ? sv_len_utf8(PL_rs)
1105 if (SvUTF8(PL_rs) != SvUTF8(sv)) {
1106 /* Assumption is that rs is shorter than the scalar. */
1107 if (SvUTF8(PL_rs)) {
1108 /* RS is utf8, scalar is 8 bit. */
1109 bool is_utf8 = TRUE;
1110 temp_buffer = (char*)bytes_from_utf8((U8*)rsptr,
1113 /* Cannot downgrade, therefore cannot possibly match
1115 assert (temp_buffer == rsptr);
1119 rsptr = temp_buffer;
1121 else if (PL_encoding) {
1122 /* RS is 8 bit, encoding.pm is used.
1123 * Do not recode PL_rs as a side-effect. */
1124 svrecode = newSVpvn(rsptr, rslen);
1125 sv_recode_to_utf8(svrecode, PL_encoding);
1126 rsptr = SvPV_const(svrecode, rslen);
1127 rs_charlen = sv_len_utf8(svrecode);
1130 /* RS is 8 bit, scalar is utf8. */
1131 temp_buffer = (char*)bytes_to_utf8((U8*)rsptr, &rslen);
1132 rsptr = temp_buffer;
1141 if (len < rslen - 1)
1145 if (memNE(s, rsptr, rslen))
1147 count += rs_charlen;
1150 s = SvPV_force_nolen(sv);
1159 SvREFCNT_dec(svrecode);
1161 Safefree(temp_buffer);
1166 Perl_do_vop(pTHX_ I32 optype, SV *sv, SV *left, SV *right)
1177 register const char *lc;
1178 register const char *rc;
1183 const bool left_utf = DO_UTF8(left);
1184 const bool right_utf = DO_UTF8(right);
1187 if (left_utf && !right_utf)
1188 sv_utf8_upgrade(right);
1189 else if (!left_utf && right_utf)
1190 sv_utf8_upgrade(left);
1192 if (sv != left || (optype != OP_BIT_AND && !SvOK(sv) && !SvGMAGICAL(sv)))
1193 sv_setpvn(sv, "", 0); /* avoid undef warning on |= and ^= */
1194 lsave = lc = SvPV_nomg_const(left, leftlen);
1195 rsave = rc = SvPV_nomg_const(right, rightlen);
1196 len = leftlen < rightlen ? leftlen : rightlen;
1198 if ((left_utf || right_utf) && (sv == left || sv == right)) {
1199 needlen = optype == OP_BIT_AND ? len : leftlen + rightlen;
1200 Newxz(dc, needlen + 1, char);
1202 else if (SvOK(sv) || SvTYPE(sv) > SVt_PVMG) {
1203 dc = SvPV_force_nomg_nolen(sv);
1204 if (SvLEN(sv) < (STRLEN)(len + 1)) {
1205 dc = SvGROW(sv, (STRLEN)(len + 1));
1206 (void)memzero(dc + SvCUR(sv), len - SvCUR(sv) + 1);
1208 if (optype != OP_BIT_AND && (left_utf || right_utf))
1209 dc = SvGROW(sv, leftlen + rightlen + 1);
1212 needlen = ((optype == OP_BIT_AND)
1213 ? len : (leftlen > rightlen ? leftlen : rightlen));
1214 Newxz(dc, needlen + 1, char);
1215 (void)sv_usepvn(sv, dc, needlen);
1216 dc = SvPVX(sv); /* sv_usepvn() calls Renew() */
1219 (void)SvPOK_only(sv);
1220 if (left_utf || right_utf) {
1222 char * const dcsave = dc;
1223 STRLEN lulen = leftlen;
1224 STRLEN rulen = rightlen;
1229 while (lulen && rulen) {
1230 luc = utf8n_to_uvchr((U8*)lc, lulen, &ulen, UTF8_ALLOW_ANYUV);
1233 ruc = utf8n_to_uvchr((U8*)rc, rulen, &ulen, UTF8_ALLOW_ANYUV);
1237 dc = (char*)uvchr_to_utf8((U8*)dc, duc);
1239 if (sv == left || sv == right)
1240 (void)sv_usepvn(sv, dcsave, needlen);
1241 SvCUR_set(sv, dc - dcsave);
1244 while (lulen && rulen) {
1245 luc = utf8n_to_uvchr((U8*)lc, lulen, &ulen, UTF8_ALLOW_ANYUV);
1248 ruc = utf8n_to_uvchr((U8*)rc, rulen, &ulen, UTF8_ALLOW_ANYUV);
1252 dc = (char*)uvchr_to_utf8((U8*)dc, duc);
1256 while (lulen && rulen) {
1257 luc = utf8n_to_uvchr((U8*)lc, lulen, &ulen, UTF8_ALLOW_ANYUV);
1260 ruc = utf8n_to_uvchr((U8*)rc, rulen, &ulen, UTF8_ALLOW_ANYUV);
1264 dc = (char*)uvchr_to_utf8((U8*)dc, duc);
1267 if (sv == left || sv == right)
1268 (void)sv_usepvn(sv, dcsave, needlen);
1269 SvCUR_set(sv, dc - dcsave);
1271 sv_catpvn(sv, rc, rulen);
1273 sv_catpvn(sv, lc, lulen);
1283 if (len >= sizeof(long)*4 &&
1284 !((long)dc % sizeof(long)) &&
1285 !((long)lc % sizeof(long)) &&
1286 !((long)rc % sizeof(long))) /* It's almost always aligned... */
1288 const I32 remainder = len % (sizeof(long)*4);
1289 len /= (sizeof(long)*4);
1298 *dl++ = *ll++ & *rl++;
1299 *dl++ = *ll++ & *rl++;
1300 *dl++ = *ll++ & *rl++;
1301 *dl++ = *ll++ & *rl++;
1306 *dl++ = *ll++ ^ *rl++;
1307 *dl++ = *ll++ ^ *rl++;
1308 *dl++ = *ll++ ^ *rl++;
1309 *dl++ = *ll++ ^ *rl++;
1314 *dl++ = *ll++ | *rl++;
1315 *dl++ = *ll++ | *rl++;
1316 *dl++ = *ll++ | *rl++;
1317 *dl++ = *ll++ | *rl++;
1332 *dc++ = *lc++ & *rc++;
1337 *dc++ = *lc++ ^ *rc++;
1341 *dc++ = *lc++ | *rc++;
1344 if (rightlen > (STRLEN)len)
1345 sv_catpvn(sv, rsave + len, rightlen - len);
1346 else if (leftlen > (STRLEN)len)
1347 sv_catpvn(sv, lsave + len, leftlen - len);
1362 HV * const hv = (HV*)POPs;
1365 const I32 gimme = GIMME_V;
1366 const I32 dokv = (PL_op->op_type == OP_RV2HV || PL_op->op_type == OP_PADHV);
1367 const I32 dokeys = dokv || (PL_op->op_type == OP_KEYS);
1368 const I32 dovalues = dokv || (PL_op->op_type == OP_VALUES);
1371 if (PL_op->op_flags & OPf_MOD || LVRET) { /* lvalue */
1372 dTARGET; /* make sure to clear its target here */
1373 if (SvTYPE(TARG) == SVt_PVLV)
1374 LvTARG(TARG) = NULL;
1381 (void)hv_iterinit(keys); /* always reset iterator regardless */
1383 if (gimme == G_VOID)
1386 if (gimme == G_SCALAR) {
1390 if (PL_op->op_flags & OPf_MOD || LVRET) { /* lvalue */
1391 if (SvTYPE(TARG) < SVt_PVLV) {
1392 sv_upgrade(TARG, SVt_PVLV);
1393 sv_magic(TARG, NULL, PERL_MAGIC_nkeys, NULL, 0);
1396 if (LvTARG(TARG) != (SV*)keys) {
1398 SvREFCNT_dec(LvTARG(TARG));
1399 LvTARG(TARG) = SvREFCNT_inc(keys);
1405 if (! SvTIED_mg((SV*)keys, PERL_MAGIC_tied))
1409 while (hv_iternext(keys)) i++;
1415 EXTEND(SP, HvKEYS(keys) * (dokeys + dovalues));
1417 PUTBACK; /* hv_iternext and hv_iterval might clobber stack_sp */
1418 while ((entry = hv_iternext(keys))) {
1421 SV* const sv = hv_iterkeysv(entry);
1422 XPUSHs(sv); /* won't clobber stack_sp */
1427 tmpstr = hv_iterval(hv,entry);
1428 DEBUG_H(Perl_sv_setpvf(aTHX_ tmpstr, "%lu%%%d=%lu",
1429 (unsigned long)HeHASH(entry),
1431 (unsigned long)(HeHASH(entry) & HvMAX(keys))));
1442 * c-indentation-style: bsd
1444 * indent-tabs-mode: t
1447 * ex: set ts=8 sts=4 sw=4 noet: