3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2004, 2005, 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)
36 const I32 grows = PL_op->op_private & OPpTRANS_GROWS;
39 const short *tbl = (short*)cPVOP->op_pv;
41 Perl_croak(aTHX_ "panic: do_trans_simple line %d",__LINE__);
43 s = (U8*)SvPV(sv, len);
46 /* First, take care of non-UTF-8 input strings, because they're easy */
49 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)
104 const I32 complement = PL_op->op_private & OPpTRANS_COMPLEMENT;
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);
121 const UV c = utf8n_to_uvchr(s, send - s, &ulen, 0);
125 } else if (complement)
134 S_do_trans_complex(pTHX_ SV *sv)
142 const I32 grows = PL_op->op_private & OPpTRANS_GROWS;
143 const I32 complement = PL_op->op_private & OPpTRANS_COMPLEMENT;
144 const I32 del = PL_op->op_private & OPpTRANS_DELETE;
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);
193 Newx(d, len*2+1, U8);
197 if (complement && !del)
200 #ifdef MACOS_TRADITIONAL
201 #define comp CoMP /* "comp" is a keyword in some compilers ... */
204 if (PL_op->op_private & OPpTRANS_SQUASH) {
208 const UV comp = utf8_to_uvchr(s, &len);
219 ch = (rlen == 0) ? comp :
220 (comp - 0x100 < rlen) ?
221 tbl[comp+1] : tbl[0x100+rlen];
223 d = uvchr_to_utf8(d, ch);
231 else if ((ch = tbl[comp]) >= 0) {
234 d = uvchr_to_utf8(d, ch);
240 else if (ch == -1) { /* -1 is unmapped character */
244 else if (ch == -2) /* -2 is delete character */
253 const UV comp = utf8_to_uvchr(s, &len);
263 if (comp - 0x100 < rlen)
264 d = uvchr_to_utf8(d, tbl[comp+1]);
266 d = uvchr_to_utf8(d, tbl[0x100+rlen]);
270 else if ((ch = tbl[comp]) >= 0) {
271 d = uvchr_to_utf8(d, ch);
274 else if (ch == -1) { /* -1 is unmapped character */
278 else if (ch == -2) /* -2 is delete character */
284 sv_setpvn(sv, (char*)dstart, d - dstart);
289 SvCUR_set(sv, d - dstart);
298 S_do_trans_simple_utf8(pTHX_ SV *sv)
306 const I32 grows = PL_op->op_private & OPpTRANS_GROWS;
309 SV* const rv = (SV*)cSVOP->op_sv;
310 HV* const hv = (HV*)SvRV(rv);
311 SV** svp = hv_fetch(hv, "NONE", 4, FALSE);
312 const UV none = svp ? SvUV(*svp) : 0x7fffffff;
313 const UV extra = none + 1;
319 s = (U8*)SvPV(sv, len);
322 const U8 *t = s, *e = s + len;
325 if ((hibit = !NATIVE_IS_INVARIANT(ch)))
329 s = bytes_to_utf8(s, &len);
334 svp = hv_fetch(hv, "FINAL", 5, FALSE);
339 /* d needs to be bigger than s, in case e.g. upgrading is required */
340 Newx(d, len * 3 + UTF8_MAXBYTES, U8);
350 if ((uv = swash_fetch(rv, s, TRUE)) < none) {
353 d = uvuni_to_utf8(d, uv);
355 else if (uv == none) {
356 const int i = UTF8SKIP(s);
361 else if (uv == extra) {
364 d = uvuni_to_utf8(d, final);
370 const STRLEN clen = d - dstart;
371 const STRLEN nlen = dend - dstart + len + UTF8_MAXBYTES;
373 Perl_croak(aTHX_ "panic: do_trans_simple_utf8 line %d",__LINE__);
374 Renew(dstart, nlen + UTF8_MAXBYTES, U8);
376 dend = dstart + nlen;
379 if (grows || hibit) {
380 sv_setpvn(sv, (char*)dstart, d - dstart);
387 SvCUR_set(sv, d - dstart);
396 S_do_trans_count_utf8(pTHX_ SV *sv)
399 const U8 *start = 0, *send;
403 SV* const rv = (SV*)cSVOP->op_sv;
404 HV* const hv = (HV*)SvRV(rv);
405 SV** const svp = hv_fetch(hv, "NONE", 4, FALSE);
406 const UV none = svp ? SvUV(*svp) : 0x7fffffff;
407 const UV extra = none + 1;
410 s = (const U8*)SvPV_const(sv, len);
413 const U8 *e = s + len;
416 if ((hibit = !NATIVE_IS_INVARIANT(ch)))
420 start = s = bytes_to_utf8(s, &len);
426 if ((uv = swash_fetch(rv, s, TRUE)) < none || uv == extra)
437 S_do_trans_complex_utf8(pTHX_ SV *sv)
442 const I32 squash = PL_op->op_private & OPpTRANS_SQUASH;
443 const I32 del = PL_op->op_private & OPpTRANS_DELETE;
444 const I32 grows = PL_op->op_private & OPpTRANS_GROWS;
445 SV * const rv = (SV*)cSVOP->op_sv;
446 HV * const hv = (HV*)SvRV(rv);
447 SV** svp = hv_fetch(hv, "NONE", 4, FALSE);
448 const UV none = svp ? SvUV(*svp) : 0x7fffffff;
449 const UV extra = none + 1;
451 bool havefinal = FALSE;
456 U8 *s = (U8*)SvPV(sv, len);
457 const I32 isutf8 = SvUTF8(sv);
460 const U8 * const e = s + len;
463 if ((hibit = !NATIVE_IS_INVARIANT(ch)))
467 s = bytes_to_utf8(s, &len);
472 svp = hv_fetch(hv, "FINAL", 5, FALSE);
479 /* d needs to be bigger than s, in case e.g. upgrading is required */
480 Newx(d, len * 3 + UTF8_MAXBYTES, U8);
492 UV uv = swash_fetch(rv, s, TRUE);
495 const STRLEN clen = d - dstart;
496 const STRLEN nlen = dend - dstart + len + UTF8_MAXBYTES;
498 Perl_croak(aTHX_ "panic: do_trans_complex_utf8 line %d",__LINE__);
499 Renew(dstart, nlen + UTF8_MAXBYTES, U8);
501 dend = dstart + nlen;
507 d = uvuni_to_utf8(d, uv);
512 else if (uv == none) { /* "none" is unmapped character */
513 const int i = UTF8SKIP(s);
520 else if (uv == extra && !del) {
525 d = uvuni_to_utf8(d, final);
531 uv = utf8_to_uvuni(s, &len);
541 matches++; /* "none+1" is delete character */
547 const UV uv = swash_fetch(rv, s, TRUE);
549 const STRLEN clen = d - dstart;
550 const STRLEN nlen = dend - dstart + len + UTF8_MAXBYTES;
552 Perl_croak(aTHX_ "panic: do_trans_complex_utf8 line %d",__LINE__);
553 Renew(dstart, nlen + UTF8_MAXBYTES, U8);
555 dend = dstart + nlen;
560 d = uvuni_to_utf8(d, uv);
563 else if (uv == none) { /* "none" is unmapped character */
564 const int i = UTF8SKIP(s);
570 else if (uv == extra && !del) {
573 d = uvuni_to_utf8(d, final);
576 matches++; /* "none+1" is delete character */
580 if (grows || hibit) {
581 sv_setpvn(sv, (char*)dstart, d - dstart);
588 SvCUR_set(sv, d - dstart);
597 Perl_do_trans(pTHX_ SV *sv)
600 const I32 hasutf = (PL_op->op_private &
601 (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF));
603 if (SvREADONLY(sv)) {
605 sv_force_normal_flags(sv, 0);
606 if (SvREADONLY(sv) && !(PL_op->op_private & OPpTRANS_IDENTICAL))
607 Perl_croak(aTHX_ PL_no_modify);
609 (void)SvPV_const(sv, len);
612 if (!(PL_op->op_private & OPpTRANS_IDENTICAL)) {
614 (void)SvPV_force(sv, len);
615 (void)SvPOK_only_UTF8(sv);
618 DEBUG_t( Perl_deb(aTHX_ "2.TBL\n"));
620 switch (PL_op->op_private & ~hasutf & (
621 OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF|OPpTRANS_IDENTICAL|
622 OPpTRANS_SQUASH|OPpTRANS_DELETE|OPpTRANS_COMPLEMENT)) {
625 return do_trans_simple_utf8(sv);
627 return do_trans_simple(sv);
629 case OPpTRANS_IDENTICAL:
630 case OPpTRANS_IDENTICAL|OPpTRANS_COMPLEMENT:
632 return do_trans_count_utf8(sv);
634 return do_trans_count(sv);
638 return do_trans_complex_utf8(sv);
640 return do_trans_complex(sv);
645 Perl_do_join(pTHX_ register SV *sv, SV *del, register SV **mark, register SV **sp)
647 SV ** const oldmark = mark;
648 register I32 items = sp - mark;
652 (void) SvPV_const(del, delimlen); /* stringify and get the delimlen */
653 /* SvCUR assumes it's SvPOK() and woe betide you if it's not. */
656 len = (items > 0 ? (delimlen * (items - 1) ) : 0);
657 SvUPGRADE(sv, SVt_PV);
658 if (SvLEN(sv) < len + items) { /* current length is way too short */
659 while (items-- > 0) {
660 if (*mark && !SvGAMAGIC(*mark) && SvOK(*mark)) {
662 SvPV_const(*mark, tmplen);
667 SvGROW(sv, len + 1); /* so try to pre-extend */
674 sv_setpvn(sv, "", 0);
675 /* sv_setpv retains old UTF8ness [perl #24846] */
678 if (PL_tainting && SvMAGICAL(sv))
688 for (; items > 0; items--,mark++) {
694 for (; items > 0; items--,mark++)
701 Perl_do_sprintf(pTHX_ SV *sv, I32 len, SV **sarg)
704 const char * const pat = SvPV_const(*sarg, patlen);
705 bool do_taint = FALSE;
710 sv_vsetpvfn(sv, pat, patlen, Null(va_list*), sarg + 1, len - 1, &do_taint);
716 /* currently converts input to bytes if possible, but doesn't sweat failure */
718 Perl_do_vecget(pTHX_ SV *sv, I32 offset, I32 size)
721 const unsigned char *s = (const unsigned char *) SvPV_const(sv, srclen);
726 if (size < 1 || (size & (size-1))) /* size < 1 or not a power of two */
727 Perl_croak(aTHX_ "Illegal number of bits in vec");
730 (void) Perl_sv_utf8_downgrade(aTHX_ sv, TRUE);
732 offset *= size; /* turn into bit offset */
733 len = (offset + size + 7) / 8; /* required number of bytes */
738 offset >>= 3; /* turn into byte offset */
740 if ((STRLEN)offset >= srclen)
743 retnum = (UV) s[offset] << 8;
745 else if (size == 32) {
746 if ((STRLEN)offset >= srclen)
748 else if ((STRLEN)(offset + 1) >= srclen)
750 ((UV) s[offset ] << 24);
751 else if ((STRLEN)(offset + 2) >= srclen)
753 ((UV) s[offset ] << 24) +
754 ((UV) s[offset + 1] << 16);
757 ((UV) s[offset ] << 24) +
758 ((UV) s[offset + 1] << 16) +
759 ( s[offset + 2] << 8);
762 else if (size == 64) {
763 if (ckWARN(WARN_PORTABLE))
764 Perl_warner(aTHX_ packWARN(WARN_PORTABLE),
765 "Bit vector size > 32 non-portable");
766 if (offset >= srclen)
768 else if (offset + 1 >= srclen)
770 (UV) s[offset ] << 56;
771 else if (offset + 2 >= srclen)
773 ((UV) s[offset ] << 56) +
774 ((UV) s[offset + 1] << 48);
775 else if (offset + 3 >= srclen)
777 ((UV) s[offset ] << 56) +
778 ((UV) s[offset + 1] << 48) +
779 ((UV) s[offset + 2] << 40);
780 else if (offset + 4 >= srclen)
782 ((UV) s[offset ] << 56) +
783 ((UV) s[offset + 1] << 48) +
784 ((UV) s[offset + 2] << 40) +
785 ((UV) s[offset + 3] << 32);
786 else if (offset + 5 >= srclen)
788 ((UV) s[offset ] << 56) +
789 ((UV) s[offset + 1] << 48) +
790 ((UV) s[offset + 2] << 40) +
791 ((UV) s[offset + 3] << 32) +
792 ( s[offset + 4] << 24);
793 else if (offset + 6 >= srclen)
795 ((UV) s[offset ] << 56) +
796 ((UV) s[offset + 1] << 48) +
797 ((UV) s[offset + 2] << 40) +
798 ((UV) s[offset + 3] << 32) +
799 ((UV) s[offset + 4] << 24) +
800 ((UV) s[offset + 5] << 16);
803 ((UV) s[offset ] << 56) +
804 ((UV) s[offset + 1] << 48) +
805 ((UV) s[offset + 2] << 40) +
806 ((UV) s[offset + 3] << 32) +
807 ((UV) s[offset + 4] << 24) +
808 ((UV) s[offset + 5] << 16) +
809 ( s[offset + 6] << 8);
815 retnum = (s[offset >> 3] >> (offset & 7)) & ((1 << size) - 1);
817 offset >>= 3; /* turn into byte offset */
822 ((UV) s[offset] << 8) +
826 ((UV) s[offset ] << 24) +
827 ((UV) s[offset + 1] << 16) +
828 ( s[offset + 2] << 8) +
831 else if (size == 64) {
832 if (ckWARN(WARN_PORTABLE))
833 Perl_warner(aTHX_ packWARN(WARN_PORTABLE),
834 "Bit vector size > 32 non-portable");
836 ((UV) s[offset ] << 56) +
837 ((UV) s[offset + 1] << 48) +
838 ((UV) s[offset + 2] << 40) +
839 ((UV) s[offset + 3] << 32) +
840 ((UV) s[offset + 4] << 24) +
841 ((UV) s[offset + 5] << 16) +
842 ( s[offset + 6] << 8) +
851 /* currently converts input to bytes if possible but doesn't sweat failures,
852 * although it does ensure that the string it clobbers is not marked as
853 * utf8-valid any more
856 Perl_do_vecset(pTHX_ SV *sv)
858 SV *targ = LvTARG(sv);
861 register unsigned char *s;
869 s = (unsigned char*)SvPV_force(targ, targlen);
871 /* This is handled by the SvPOK_only below...
872 if (!Perl_sv_utf8_downgrade(aTHX_ targ, TRUE))
875 (void) Perl_sv_utf8_downgrade(aTHX_ targ, TRUE);
878 (void)SvPOK_only(targ);
880 offset = LvTARGOFF(sv);
882 Perl_croak(aTHX_ "Negative offset to vec in lvalue context");
883 size = LvTARGLEN(sv);
884 if (size < 1 || (size & (size-1))) /* size < 1 or not a power of two */
885 Perl_croak(aTHX_ "Illegal number of bits in vec");
887 offset *= size; /* turn into bit offset */
888 len = (offset + size + 7) / 8; /* required number of bytes */
890 s = (unsigned char*)SvGROW(targ, len + 1);
891 (void)memzero((char *)(s + targlen), len - targlen + 1);
892 SvCUR_set(targ, len);
896 mask = (1 << size) - 1;
899 offset >>= 3; /* turn into byte offset */
900 s[offset] &= ~(mask << size);
901 s[offset] |= lval << size;
904 offset >>= 3; /* turn into byte offset */
906 s[offset ] = (U8)( lval & 0xff);
907 else if (size == 16) {
908 s[offset ] = (U8)((lval >> 8) & 0xff);
909 s[offset+1] = (U8)( lval & 0xff);
911 else if (size == 32) {
912 s[offset ] = (U8)((lval >> 24) & 0xff);
913 s[offset+1] = (U8)((lval >> 16) & 0xff);
914 s[offset+2] = (U8)((lval >> 8) & 0xff);
915 s[offset+3] = (U8)( lval & 0xff);
918 else if (size == 64) {
919 if (ckWARN(WARN_PORTABLE))
920 Perl_warner(aTHX_ packWARN(WARN_PORTABLE),
921 "Bit vector size > 32 non-portable");
922 s[offset ] = (U8)((lval >> 56) & 0xff);
923 s[offset+1] = (U8)((lval >> 48) & 0xff);
924 s[offset+2] = (U8)((lval >> 40) & 0xff);
925 s[offset+3] = (U8)((lval >> 32) & 0xff);
926 s[offset+4] = (U8)((lval >> 24) & 0xff);
927 s[offset+5] = (U8)((lval >> 16) & 0xff);
928 s[offset+6] = (U8)((lval >> 8) & 0xff);
929 s[offset+7] = (U8)( lval & 0xff);
937 Perl_do_chop(pTHX_ register SV *astr, register SV *sv)
942 if (SvTYPE(sv) == SVt_PVAV) {
945 const I32 max = AvFILL(av);
947 for (i = 0; i <= max; i++) {
948 sv = (SV*)av_fetch(av, i, FALSE);
949 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
954 else if (SvTYPE(sv) == SVt_PVHV) {
957 (void)hv_iterinit(hv);
958 while ((entry = hv_iternext(hv)))
959 do_chop(astr,hv_iterval(hv,entry));
962 else if (SvREADONLY(sv)) {
964 /* SV is copy-on-write */
965 sv_force_normal_flags(sv, 0);
968 Perl_croak(aTHX_ PL_no_modify);
971 if (len && !SvPOK(sv))
972 s = SvPV_force(sv, len);
975 char *send = s + len;
978 while (s > start && UTF8_IS_CONTINUATION(*s))
980 if (is_utf8_string((U8*)s, send - s)) {
981 sv_setpvn(astr, s, send - s);
983 SvCUR_set(sv, s - start);
989 sv_setpvn(astr, "", 0);
993 sv_setpvn(astr, s, 1);
1000 sv_setpvn(astr, "", 0);
1005 Perl_do_chomp(pTHX_ register SV *sv)
1010 char *temp_buffer = NULL;
1011 SV* svrecode = Nullsv;
1015 if (RsRECORD(PL_rs))
1018 if (SvTYPE(sv) == SVt_PVAV) {
1020 AV* const av = (AV*)sv;
1021 const I32 max = AvFILL(av);
1023 for (i = 0; i <= max; i++) {
1024 sv = (SV*)av_fetch(av, i, FALSE);
1025 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
1026 count += do_chomp(sv);
1030 else if (SvTYPE(sv) == SVt_PVHV) {
1031 HV* const hv = (HV*)sv;
1033 (void)hv_iterinit(hv);
1034 while ((entry = hv_iternext(hv)))
1035 count += do_chomp(hv_iterval(hv,entry));
1038 else if (SvREADONLY(sv)) {
1040 /* SV is copy-on-write */
1041 sv_force_normal_flags(sv, 0);
1044 Perl_croak(aTHX_ PL_no_modify);
1049 /* XXX, here sv is utf8-ized as a side-effect!
1050 If encoding.pm is used properly, almost string-generating
1051 operations, including literal strings, chr(), input data, etc.
1052 should have been utf8-ized already, right?
1054 sv_recode_to_utf8(sv, PL_encoding);
1061 if (RsPARA(PL_rs)) {
1065 while (len && s[-1] == '\n') {
1072 STRLEN rslen, rs_charlen;
1073 const char *rsptr = SvPV_const(PL_rs, rslen);
1075 rs_charlen = SvUTF8(PL_rs)
1076 ? sv_len_utf8(PL_rs)
1079 if (SvUTF8(PL_rs) != SvUTF8(sv)) {
1080 /* Assumption is that rs is shorter than the scalar. */
1081 if (SvUTF8(PL_rs)) {
1082 /* RS is utf8, scalar is 8 bit. */
1083 bool is_utf8 = TRUE;
1084 temp_buffer = (char*)bytes_from_utf8((U8*)rsptr,
1087 /* Cannot downgrade, therefore cannot possibly match
1089 assert (temp_buffer == rsptr);
1093 rsptr = temp_buffer;
1095 else if (PL_encoding) {
1096 /* RS is 8 bit, encoding.pm is used.
1097 * Do not recode PL_rs as a side-effect. */
1098 svrecode = newSVpvn(rsptr, rslen);
1099 sv_recode_to_utf8(svrecode, PL_encoding);
1100 rsptr = SvPV_const(svrecode, rslen);
1101 rs_charlen = sv_len_utf8(svrecode);
1104 /* RS is 8 bit, scalar is utf8. */
1105 temp_buffer = (char*)bytes_to_utf8((U8*)rsptr, &rslen);
1106 rsptr = temp_buffer;
1115 if (len < rslen - 1)
1119 if (memNE(s, rsptr, rslen))
1121 count += rs_charlen;
1124 s = SvPV_force_nolen(sv);
1133 SvREFCNT_dec(svrecode);
1135 Safefree(temp_buffer);
1140 Perl_do_vop(pTHX_ I32 optype, SV *sv, SV *left, SV *right)
1150 register const char *lc;
1151 register const char *rc;
1156 const bool left_utf = DO_UTF8(left);
1157 const bool right_utf = DO_UTF8(right);
1160 if (left_utf && !right_utf)
1161 sv_utf8_upgrade(right);
1162 else if (!left_utf && right_utf)
1163 sv_utf8_upgrade(left);
1165 if (sv != left || (optype != OP_BIT_AND && !SvOK(sv) && !SvGMAGICAL(sv)))
1166 sv_setpvn(sv, "", 0); /* avoid undef warning on |= and ^= */
1167 lsave = lc = SvPV_nomg_const(left, leftlen);
1168 rsave = rc = SvPV_nomg_const(right, rightlen);
1169 len = leftlen < rightlen ? leftlen : rightlen;
1171 if ((left_utf || right_utf) && (sv == left || sv == right)) {
1172 needlen = optype == OP_BIT_AND ? len : leftlen + rightlen;
1173 Newxz(dc, needlen + 1, char);
1175 else if (SvOK(sv) || SvTYPE(sv) > SVt_PVMG) {
1176 dc = SvPV_force_nomg_nolen(sv);
1177 if (SvCUR(sv) < (STRLEN)len) {
1178 dc = SvGROW(sv, (STRLEN)(len + 1));
1179 (void)memzero(dc + SvCUR(sv), len - SvCUR(sv) + 1);
1181 if (optype != OP_BIT_AND && (left_utf || right_utf))
1182 dc = SvGROW(sv, leftlen + rightlen + 1);
1185 needlen = ((optype == OP_BIT_AND)
1186 ? len : (leftlen > rightlen ? leftlen : rightlen));
1187 Newxz(dc, needlen + 1, char);
1188 (void)sv_usepvn(sv, dc, needlen);
1189 dc = SvPVX(sv); /* sv_usepvn() calls Renew() */
1192 (void)SvPOK_only(sv);
1193 if (left_utf || right_utf) {
1196 STRLEN lulen = leftlen;
1197 STRLEN rulen = rightlen;
1202 while (lulen && rulen) {
1203 luc = utf8n_to_uvchr((U8*)lc, lulen, &ulen, UTF8_ALLOW_ANYUV);
1206 ruc = utf8n_to_uvchr((U8*)rc, rulen, &ulen, UTF8_ALLOW_ANYUV);
1210 dc = (char*)uvchr_to_utf8((U8*)dc, duc);
1212 if (sv == left || sv == right)
1213 (void)sv_usepvn(sv, dcsave, needlen);
1214 SvCUR_set(sv, dc - dcsave);
1217 while (lulen && rulen) {
1218 luc = utf8n_to_uvchr((U8*)lc, lulen, &ulen, UTF8_ALLOW_ANYUV);
1221 ruc = utf8n_to_uvchr((U8*)rc, rulen, &ulen, UTF8_ALLOW_ANYUV);
1225 dc = (char*)uvchr_to_utf8((U8*)dc, duc);
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);
1240 if (sv == left || sv == right)
1241 (void)sv_usepvn(sv, dcsave, needlen);
1242 SvCUR_set(sv, dc - dcsave);
1244 sv_catpvn(sv, rc, rulen);
1246 sv_catpvn(sv, lc, lulen);
1256 if (len >= sizeof(long)*4 &&
1257 !((long)dc % sizeof(long)) &&
1258 !((long)lc % sizeof(long)) &&
1259 !((long)rc % sizeof(long))) /* It's almost always aligned... */
1261 const I32 remainder = len % (sizeof(long)*4);
1262 len /= (sizeof(long)*4);
1271 *dl++ = *ll++ & *rl++;
1272 *dl++ = *ll++ & *rl++;
1273 *dl++ = *ll++ & *rl++;
1274 *dl++ = *ll++ & *rl++;
1279 *dl++ = *ll++ ^ *rl++;
1280 *dl++ = *ll++ ^ *rl++;
1281 *dl++ = *ll++ ^ *rl++;
1282 *dl++ = *ll++ ^ *rl++;
1287 *dl++ = *ll++ | *rl++;
1288 *dl++ = *ll++ | *rl++;
1289 *dl++ = *ll++ | *rl++;
1290 *dl++ = *ll++ | *rl++;
1305 *dc++ = *lc++ & *rc++;
1309 *dc++ = *lc++ ^ *rc++;
1313 *dc++ = *lc++ | *rc++;
1316 if (rightlen > (STRLEN)len)
1317 sv_catpvn(sv, rsave + len, rightlen - len);
1318 else if (leftlen > (STRLEN)len)
1319 sv_catpvn(sv, lsave + len, leftlen - len);
1336 const I32 gimme = GIMME_V;
1337 const I32 dokv = (PL_op->op_type == OP_RV2HV || PL_op->op_type == OP_PADHV);
1338 const I32 dokeys = dokv || (PL_op->op_type == OP_KEYS);
1339 const I32 dovalues = dokv || (PL_op->op_type == OP_VALUES);
1342 if (PL_op->op_flags & OPf_MOD || LVRET) { /* lvalue */
1343 dTARGET; /* make sure to clear its target here */
1344 if (SvTYPE(TARG) == SVt_PVLV)
1345 LvTARG(TARG) = Nullsv;
1352 (void)hv_iterinit(keys); /* always reset iterator regardless */
1354 if (gimme == G_VOID)
1357 if (gimme == G_SCALAR) {
1361 if (PL_op->op_flags & OPf_MOD || LVRET) { /* lvalue */
1362 if (SvTYPE(TARG) < SVt_PVLV) {
1363 sv_upgrade(TARG, SVt_PVLV);
1364 sv_magic(TARG, Nullsv, PERL_MAGIC_nkeys, Nullch, 0);
1367 if (LvTARG(TARG) != (SV*)keys) {
1369 SvREFCNT_dec(LvTARG(TARG));
1370 LvTARG(TARG) = SvREFCNT_inc(keys);
1376 if (! SvTIED_mg((SV*)keys, PERL_MAGIC_tied))
1380 while (hv_iternext(keys)) i++;
1386 EXTEND(SP, HvKEYS(keys) * (dokeys + dovalues));
1388 PUTBACK; /* hv_iternext and hv_iterval might clobber stack_sp */
1389 while ((entry = hv_iternext(keys))) {
1392 SV* const sv = hv_iterkeysv(entry);
1393 XPUSHs(sv); /* won't clobber stack_sp */
1398 tmpstr = hv_iterval(hv,entry);
1399 DEBUG_H(Perl_sv_setpvf(aTHX_ tmpstr, "%lu%%%d=%lu",
1400 (unsigned long)HeHASH(entry),
1402 (unsigned long)(HeHASH(entry) & HvMAX(keys))));
1413 * c-indentation-style: bsd
1415 * indent-tabs-mode: t
1418 * ex: set ts=8 sts=4 sw=4 noet: