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 * const 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];
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, 0);
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);
97 S_do_trans_count(pTHX_ SV *sv)
103 const I32 complement = PL_op->op_private & OPpTRANS_COMPLEMENT;
105 const short * const tbl = (short*)cPVOP->op_pv;
107 Perl_croak(aTHX_ "panic: do_trans_count line %d",__LINE__);
109 s = (const U8*)SvPV_const(sv, len);
120 const UV c = utf8n_to_uvchr(s, send - s, &ulen, 0);
124 } else if (complement)
133 S_do_trans_complex(pTHX_ SV *sv)
141 const I32 grows = PL_op->op_private & OPpTRANS_GROWS;
142 const I32 complement = PL_op->op_private & OPpTRANS_COMPLEMENT;
143 const I32 del = PL_op->op_private & OPpTRANS_DELETE;
144 STRLEN len, rlen = 0;
146 const short * const tbl = (short*)cPVOP->op_pv;
148 Perl_croak(aTHX_ "panic: do_trans_complex line %d",__LINE__);
150 s = (U8*)SvPV(sv, len);
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);
192 Newx(d, len*2+1, U8);
196 if (complement && !del)
199 #ifdef MACOS_TRADITIONAL
200 #define comp CoMP /* "comp" is a keyword in some compilers ... */
203 if (PL_op->op_private & OPpTRANS_SQUASH) {
207 const UV comp = utf8_to_uvchr(s, &len);
218 ch = (rlen == 0) ? comp :
219 (comp - 0x100 < rlen) ?
220 tbl[comp+1] : tbl[0x100+rlen];
222 d = uvchr_to_utf8(d, ch);
230 else if ((ch = tbl[comp]) >= 0) {
233 d = uvchr_to_utf8(d, ch);
239 else if (ch == -1) { /* -1 is unmapped character */
243 else if (ch == -2) /* -2 is delete character */
252 const UV comp = utf8_to_uvchr(s, &len);
262 if (comp - 0x100 < rlen)
263 d = uvchr_to_utf8(d, tbl[comp+1]);
265 d = uvchr_to_utf8(d, tbl[0x100+rlen]);
269 else if ((ch = tbl[comp]) >= 0) {
270 d = uvchr_to_utf8(d, ch);
273 else if (ch == -1) { /* -1 is unmapped character */
277 else if (ch == -2) /* -2 is delete character */
283 sv_setpvn(sv, (char*)dstart, d - dstart);
288 SvCUR_set(sv, d - dstart);
297 S_do_trans_simple_utf8(pTHX_ SV *sv)
305 const I32 grows = PL_op->op_private & OPpTRANS_GROWS;
308 SV* const rv = (SV*)cSVOP->op_sv;
309 HV* const hv = (HV*)SvRV(rv);
310 SV* const * svp = hv_fetch(hv, "NONE", 4, FALSE);
311 const UV none = svp ? SvUV(*svp) : 0x7fffffff;
312 const UV extra = none + 1;
317 s = (U8*)SvPV(sv, len);
321 const U8 * const e = s + len;
324 if ((hibit = !NATIVE_IS_INVARIANT(ch)))
328 s = bytes_to_utf8(s, &len);
333 svp = hv_fetch(hv, "FINAL", 5, FALSE);
338 /* d needs to be bigger than s, in case e.g. upgrading is required */
339 Newx(d, len * 3 + UTF8_MAXBYTES, U8);
349 const UV uv = swash_fetch(rv, s, TRUE);
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 * 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 * const e = s + len;
416 if ((hibit = !NATIVE_IS_INVARIANT(ch)))
420 start = s = bytes_to_utf8(s, &len);
425 const UV uv = swash_fetch(rv, s, TRUE);
426 if (uv < 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 * const *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)
860 register unsigned char *s;
865 SV * const targ = LvTARG(sv);
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) {
944 AV* const av = (AV*)sv;
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) {
955 HV* const hv = (HV*)sv;
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 (PL_encoding && !SvUTF8(sv)) {
972 /* like in do_chomp(), utf8-ize the sv as a side-effect
973 * if we're using encoding. */
974 sv_recode_to_utf8(sv, PL_encoding);
978 if (len && !SvPOK(sv))
979 s = SvPV_force(sv, len);
982 char * const send = s + len;
983 char * const start = s;
985 while (s > start && UTF8_IS_CONTINUATION(*s))
987 if (is_utf8_string((U8*)s, send - s)) {
988 sv_setpvn(astr, s, send - s);
990 SvCUR_set(sv, s - start);
996 sv_setpvn(astr, "", 0);
1000 sv_setpvn(astr, s, 1);
1007 sv_setpvn(astr, "", 0);
1012 Perl_do_chomp(pTHX_ register SV *sv)
1017 char *temp_buffer = NULL;
1018 SV* svrecode = Nullsv;
1022 if (RsRECORD(PL_rs))
1025 if (SvTYPE(sv) == SVt_PVAV) {
1027 AV* const av = (AV*)sv;
1028 const I32 max = AvFILL(av);
1030 for (i = 0; i <= max; i++) {
1031 sv = (SV*)av_fetch(av, i, FALSE);
1032 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
1033 count += do_chomp(sv);
1037 else if (SvTYPE(sv) == SVt_PVHV) {
1038 HV* const hv = (HV*)sv;
1040 (void)hv_iterinit(hv);
1041 while ((entry = hv_iternext(hv)))
1042 count += do_chomp(hv_iterval(hv,entry));
1045 else if (SvREADONLY(sv)) {
1047 /* SV is copy-on-write */
1048 sv_force_normal_flags(sv, 0);
1051 Perl_croak(aTHX_ PL_no_modify);
1056 /* XXX, here sv is utf8-ized as a side-effect!
1057 If encoding.pm is used properly, almost string-generating
1058 operations, including literal strings, chr(), input data, etc.
1059 should have been utf8-ized already, right?
1061 sv_recode_to_utf8(sv, PL_encoding);
1068 if (RsPARA(PL_rs)) {
1072 while (len && s[-1] == '\n') {
1079 STRLEN rslen, rs_charlen;
1080 const char *rsptr = SvPV_const(PL_rs, rslen);
1082 rs_charlen = SvUTF8(PL_rs)
1083 ? sv_len_utf8(PL_rs)
1086 if (SvUTF8(PL_rs) != SvUTF8(sv)) {
1087 /* Assumption is that rs is shorter than the scalar. */
1088 if (SvUTF8(PL_rs)) {
1089 /* RS is utf8, scalar is 8 bit. */
1090 bool is_utf8 = TRUE;
1091 temp_buffer = (char*)bytes_from_utf8((U8*)rsptr,
1094 /* Cannot downgrade, therefore cannot possibly match
1096 assert (temp_buffer == rsptr);
1100 rsptr = temp_buffer;
1102 else if (PL_encoding) {
1103 /* RS is 8 bit, encoding.pm is used.
1104 * Do not recode PL_rs as a side-effect. */
1105 svrecode = newSVpvn(rsptr, rslen);
1106 sv_recode_to_utf8(svrecode, PL_encoding);
1107 rsptr = SvPV_const(svrecode, rslen);
1108 rs_charlen = sv_len_utf8(svrecode);
1111 /* RS is 8 bit, scalar is utf8. */
1112 temp_buffer = (char*)bytes_to_utf8((U8*)rsptr, &rslen);
1113 rsptr = temp_buffer;
1122 if (len < rslen - 1)
1126 if (memNE(s, rsptr, rslen))
1128 count += rs_charlen;
1131 s = SvPV_force_nolen(sv);
1140 SvREFCNT_dec(svrecode);
1142 Safefree(temp_buffer);
1147 Perl_do_vop(pTHX_ I32 optype, SV *sv, SV *left, SV *right)
1157 register const char *lc;
1158 register const char *rc;
1163 const bool left_utf = DO_UTF8(left);
1164 const bool right_utf = DO_UTF8(right);
1167 if (left_utf && !right_utf)
1168 sv_utf8_upgrade(right);
1169 else if (!left_utf && right_utf)
1170 sv_utf8_upgrade(left);
1172 if (sv != left || (optype != OP_BIT_AND && !SvOK(sv) && !SvGMAGICAL(sv)))
1173 sv_setpvn(sv, "", 0); /* avoid undef warning on |= and ^= */
1174 lsave = lc = SvPV_nomg_const(left, leftlen);
1175 rsave = rc = SvPV_nomg_const(right, rightlen);
1176 len = leftlen < rightlen ? leftlen : rightlen;
1178 if ((left_utf || right_utf) && (sv == left || sv == right)) {
1179 needlen = optype == OP_BIT_AND ? len : leftlen + rightlen;
1180 Newxz(dc, needlen + 1, char);
1182 else if (SvOK(sv) || SvTYPE(sv) > SVt_PVMG) {
1183 dc = SvPV_force_nomg_nolen(sv);
1184 if (SvLEN(sv) < (STRLEN)(len + 1)) {
1185 dc = SvGROW(sv, (STRLEN)(len + 1));
1186 (void)memzero(dc + SvCUR(sv), len - SvCUR(sv) + 1);
1188 if (optype != OP_BIT_AND && (left_utf || right_utf))
1189 dc = SvGROW(sv, leftlen + rightlen + 1);
1192 needlen = ((optype == OP_BIT_AND)
1193 ? len : (leftlen > rightlen ? leftlen : rightlen));
1194 Newxz(dc, needlen + 1, char);
1195 (void)sv_usepvn(sv, dc, needlen);
1196 dc = SvPVX(sv); /* sv_usepvn() calls Renew() */
1199 (void)SvPOK_only(sv);
1200 if (left_utf || right_utf) {
1202 char * const dcsave = dc;
1203 STRLEN lulen = leftlen;
1204 STRLEN rulen = rightlen;
1209 while (lulen && rulen) {
1210 luc = utf8n_to_uvchr((U8*)lc, lulen, &ulen, UTF8_ALLOW_ANYUV);
1213 ruc = utf8n_to_uvchr((U8*)rc, rulen, &ulen, UTF8_ALLOW_ANYUV);
1217 dc = (char*)uvchr_to_utf8((U8*)dc, duc);
1219 if (sv == left || sv == right)
1220 (void)sv_usepvn(sv, dcsave, needlen);
1221 SvCUR_set(sv, dc - dcsave);
1224 while (lulen && rulen) {
1225 luc = utf8n_to_uvchr((U8*)lc, lulen, &ulen, UTF8_ALLOW_ANYUV);
1228 ruc = utf8n_to_uvchr((U8*)rc, rulen, &ulen, UTF8_ALLOW_ANYUV);
1232 dc = (char*)uvchr_to_utf8((U8*)dc, duc);
1236 while (lulen && rulen) {
1237 luc = utf8n_to_uvchr((U8*)lc, lulen, &ulen, UTF8_ALLOW_ANYUV);
1240 ruc = utf8n_to_uvchr((U8*)rc, rulen, &ulen, UTF8_ALLOW_ANYUV);
1244 dc = (char*)uvchr_to_utf8((U8*)dc, duc);
1247 if (sv == left || sv == right)
1248 (void)sv_usepvn(sv, dcsave, needlen);
1249 SvCUR_set(sv, dc - dcsave);
1251 sv_catpvn(sv, rc, rulen);
1253 sv_catpvn(sv, lc, lulen);
1263 if (len >= sizeof(long)*4 &&
1264 !((long)dc % sizeof(long)) &&
1265 !((long)lc % sizeof(long)) &&
1266 !((long)rc % sizeof(long))) /* It's almost always aligned... */
1268 const I32 remainder = len % (sizeof(long)*4);
1269 len /= (sizeof(long)*4);
1278 *dl++ = *ll++ & *rl++;
1279 *dl++ = *ll++ & *rl++;
1280 *dl++ = *ll++ & *rl++;
1281 *dl++ = *ll++ & *rl++;
1286 *dl++ = *ll++ ^ *rl++;
1287 *dl++ = *ll++ ^ *rl++;
1288 *dl++ = *ll++ ^ *rl++;
1289 *dl++ = *ll++ ^ *rl++;
1294 *dl++ = *ll++ | *rl++;
1295 *dl++ = *ll++ | *rl++;
1296 *dl++ = *ll++ | *rl++;
1297 *dl++ = *ll++ | *rl++;
1312 *dc++ = *lc++ & *rc++;
1317 *dc++ = *lc++ ^ *rc++;
1321 *dc++ = *lc++ | *rc++;
1324 if (rightlen > (STRLEN)len)
1325 sv_catpvn(sv, rsave + len, rightlen - len);
1326 else if (leftlen > (STRLEN)len)
1327 sv_catpvn(sv, lsave + len, leftlen - len);
1341 HV * const hv = (HV*)POPs;
1344 const I32 gimme = GIMME_V;
1345 const I32 dokv = (PL_op->op_type == OP_RV2HV || PL_op->op_type == OP_PADHV);
1346 const I32 dokeys = dokv || (PL_op->op_type == OP_KEYS);
1347 const I32 dovalues = dokv || (PL_op->op_type == OP_VALUES);
1350 if (PL_op->op_flags & OPf_MOD || LVRET) { /* lvalue */
1351 dTARGET; /* make sure to clear its target here */
1352 if (SvTYPE(TARG) == SVt_PVLV)
1353 LvTARG(TARG) = Nullsv;
1360 (void)hv_iterinit(keys); /* always reset iterator regardless */
1362 if (gimme == G_VOID)
1365 if (gimme == G_SCALAR) {
1369 if (PL_op->op_flags & OPf_MOD || LVRET) { /* lvalue */
1370 if (SvTYPE(TARG) < SVt_PVLV) {
1371 sv_upgrade(TARG, SVt_PVLV);
1372 sv_magic(TARG, Nullsv, PERL_MAGIC_nkeys, Nullch, 0);
1375 if (LvTARG(TARG) != (SV*)keys) {
1377 SvREFCNT_dec(LvTARG(TARG));
1378 LvTARG(TARG) = SvREFCNT_inc(keys);
1384 if (! SvTIED_mg((SV*)keys, PERL_MAGIC_tied))
1388 while (hv_iternext(keys)) i++;
1394 EXTEND(SP, HvKEYS(keys) * (dokeys + dovalues));
1396 PUTBACK; /* hv_iternext and hv_iterval might clobber stack_sp */
1397 while ((entry = hv_iternext(keys))) {
1400 SV* const sv = hv_iterkeysv(entry);
1401 XPUSHs(sv); /* won't clobber stack_sp */
1406 tmpstr = hv_iterval(hv,entry);
1407 DEBUG_H(Perl_sv_setpvf(aTHX_ tmpstr, "%lu%%%d=%lu",
1408 (unsigned long)HeHASH(entry),
1410 (unsigned long)(HeHASH(entry) & HvMAX(keys))));
1421 * c-indentation-style: bsd
1423 * indent-tabs-mode: t
1426 * ex: set ts=8 sts=4 sw=4 noet: