3 * Copyright (c) 1991-2000, Larry Wall
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
11 * "'So that was the job I felt I had to do when I started,' thought Sam."
15 #define PERL_IN_DOOP_C
19 #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
24 #define HALF_UTF8_UPGRADE(start,end) \
26 if ((start)<(end)) { \
28 STRLEN LeN = (end) - (start); \
29 NeWsTr = bytes_to_utf8(start, &LeN); \
32 (end) = (start) + LeN; \
37 S_do_trans_simple(pTHX_ SV *sv)
45 I32 sutf = SvUTF8(sv);
50 tbl = (short*)cPVOP->op_pv;
52 Perl_croak(aTHX_ "panic: do_trans");
54 s = (U8*)SvPV(sv, len);
57 /* First, take care of non-UTF8 input strings, because they're easy */
60 if ((ch = tbl[*s]) >= 0) {
71 /* Allow for expansion: $_="a".chr(400); tr/a/\xFE/, FE needs encoding */
72 Newz(0, d, len*2+1, U8);
79 /* Need to check this, otherwise 128..255 won't match */
80 c = utf8_to_uv(s, &ulen);
81 if (c < 0x100 && (ch = tbl[(short)c]) >= 0) {
89 else { /* No match -> copy */
95 sv_setpvn(sv, (const char*)dstart, d - dstart);
103 S_do_trans_count(pTHX_ SV *sv)/* SPC - OK */
109 I32 hasutf = SvUTF8(sv);
113 tbl = (short*)cPVOP->op_pv;
115 Perl_croak(aTHX_ "panic: do_trans");
117 s = (U8*)SvPV(sv, len);
121 if (hasutf && *s & 0x80)
128 c = utf8_to_uv(s,&ulen);
131 if (c < 0x100 && tbl[c] >= 0)
141 S_do_trans_complex(pTHX_ SV *sv)/* SPC - NOT OK */
147 I32 hasutf = SvUTF8(sv);
153 tbl = (short*)cPVOP->op_pv;
155 Perl_croak(aTHX_ "panic: do_trans");
157 s = (U8*)SvPV(sv, len);
161 if (PL_op->op_private & OPpTRANS_SQUASH) {
165 if (hasutf && *s & 0x80)
168 if ((ch = tbl[*s]) >= 0) {
171 if (p == d - 1 && *p == *d)
176 else if (ch == -1) /* -1 is unmapped character */
177 *d++ = *s; /* -2 is delete character */
184 if (hasutf && *s & 0x80)
187 if ((ch = tbl[*s]) >= 0) {
192 else if (ch == -1) /* -1 is unmapped character */
193 *d++ = *s; /* -2 is delete character */
198 matches += send - d; /* account for disappeared chars */
200 SvCUR_set(sv, d - (U8*)SvPVX(sv));
207 S_do_trans_simple_utf8(pTHX_ SV *sv)/* SPC - OK */
218 SV* rv = (SV*)cSVOP->op_sv;
219 HV* hv = (HV*)SvRV(rv);
220 SV** svp = hv_fetch(hv, "NONE", 4, FALSE);
221 UV none = svp ? SvUV(*svp) : 0x7fffffff;
229 s = (U8*)SvPV(sv, len);
233 svp = hv_fetch(hv, "FINAL", 5, FALSE);
237 /* d needs to be bigger than s, in case e.g. upgrading is required */
238 Newz(0, d, len*2+1, U8);
241 if ((uv = swash_fetch(rv, s)) < none) {
244 if ((uv & 0x80) && !isutf++)
245 HALF_UTF8_UPGRADE(dstart,d);
246 d = uv_to_utf8(d, uv);
248 else if (uv == none) {
251 if (i > 1 && !isutf++)
252 HALF_UTF8_UPGRADE(dstart,d);
256 else if (uv == extra) {
261 if (i > 1 && !isutf++)
262 HALF_UTF8_UPGRADE(dstart,d);
263 d = uv_to_utf8(d, final);
269 sv_setpvn(sv, (const char*)dstart, d - dstart);
278 S_do_trans_count_utf8(pTHX_ SV *sv)/* SPC - OK */
286 SV* rv = (SV*)cSVOP->op_sv;
287 HV* hv = (HV*)SvRV(rv);
288 SV** svp = hv_fetch(hv, "NONE", 4, FALSE);
289 UV none = svp ? SvUV(*svp) : 0x7fffffff;
292 s = (U8*)SvPV(sv, len);
294 s = bytes_to_utf8(s, &len);
298 if ((uv = swash_fetch(rv, s)) < none)
307 S_do_trans_complex_utf8(pTHX_ SV *sv) /* SPC - NOT OK */
314 I32 squash = PL_op->op_private & OPpTRANS_SQUASH;
315 I32 del = PL_op->op_private & OPpTRANS_DELETE;
316 SV* rv = (SV*)cSVOP->op_sv;
317 HV* hv = (HV*)SvRV(rv);
318 SV** svp = hv_fetch(hv, "NONE", 4, FALSE);
319 UV none = svp ? SvUV(*svp) : 0x7fffffff;
325 I32 isutf = SvUTF8(sv);
327 s = (U8*)SvPV(sv, len);
330 svp = hv_fetch(hv, "FINAL", 5, FALSE);
334 Newz(0, d, len*2+1, U8);
341 uv = swash_fetch(rv, s);
348 tmpbuf[0] = (( uv >> 6) | 0xc0);
349 tmpbuf[1] = (( uv & 0x3f) | 0x80);
351 uv = swash_fetch(rv, tmpbuf);
357 if ((uv & 0x80) && !isutf++)
358 HALF_UTF8_UPGRADE(dst,d);
359 d = uv_to_utf8(d, uv);
365 else if (uv == none) { /* "none" is unmapped character */
367 *d++ = (U8)utf8_to_uv(s, &ulen);
372 else if (uv == extra && !del) {
375 d = uv_to_utf8(d, final);
381 matches++; /* "none+1" is delete character */
388 uv = swash_fetch(rv, s);
395 tmpbuf[0] = (( uv >> 6) | 0xc0);
396 tmpbuf[1] = (( uv & 0x3f) | 0x80);
398 uv = swash_fetch(rv, tmpbuf);
402 d = uv_to_utf8(d, uv);
406 else if (uv == none) { /* "none" is unmapped character */
408 *d++ = (U8)utf8_to_uv(s, &ulen);
412 else if (uv == extra && !del) {
414 d = uv_to_utf8(d, final);
418 matches++; /* "none+1" is delete character */
423 sv_usepvn(sv, (char*)dst, d - dst);
426 SvCUR_set(sv, d - (U8*)SvPVX(sv));
434 Perl_do_trans(pTHX_ SV *sv)
438 I32 hasutf = (PL_op->op_private &
439 (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF));
441 if (SvREADONLY(sv) && !(PL_op->op_private & OPpTRANS_IDENTICAL))
442 Perl_croak(aTHX_ PL_no_modify);
448 (void)SvPV_force(sv, len);
449 if (!(PL_op->op_private & OPpTRANS_IDENTICAL))
450 (void)SvPOK_only_UTF8(sv);
452 DEBUG_t( Perl_deb(aTHX_ "2.TBL\n"));
454 switch (PL_op->op_private & ~hasutf & 63) {
457 return do_trans_simple_utf8(sv);
459 return do_trans_simple(sv);
461 case OPpTRANS_IDENTICAL:
463 return do_trans_count_utf8(sv);
465 return do_trans_count(sv);
469 return do_trans_complex_utf8(sv);
471 return do_trans_complex(sv);
476 Perl_do_join(pTHX_ register SV *sv, SV *del, register SV **mark, register SV **sp)
479 register I32 items = sp - mark;
482 register char *delim = SvPV(del, delimlen);
486 len = (items > 0 ? (delimlen * (items - 1) ) : 0);
487 (void)SvUPGRADE(sv, SVt_PV);
488 if (SvLEN(sv) < len + items) { /* current length is way too short */
489 while (items-- > 0) {
490 if (*mark && !SvGAMAGIC(*mark) && SvOK(*mark)) {
496 SvGROW(sv, len + 1); /* so try to pre-extend */
515 for (; items > 0; items--,mark++) {
516 sv_catpvn(sv,delim,len);
521 for (; items > 0; items--,mark++)
528 Perl_do_sprintf(pTHX_ SV *sv, I32 len, SV **sarg)
531 char *pat = SvPV(*sarg, patlen);
532 bool do_taint = FALSE;
534 sv_vsetpvfn(sv, pat, patlen, Null(va_list*), sarg + 1, len - 1, &do_taint);
540 /* XXX SvUTF8 support missing! */
542 Perl_do_vecget(pTHX_ SV *sv, I32 offset, I32 size)
545 unsigned char *s = (unsigned char *) SvPV(sv, srclen);
550 if (size < 1 || (size & (size-1))) /* size < 1 or not a power of two */
551 Perl_croak(aTHX_ "Illegal number of bits in vec");
552 offset *= size; /* turn into bit offset */
553 len = (offset + size + 7) / 8; /* required number of bytes */
558 offset >>= 3; /* turn into byte offset */
560 if (offset >= srclen)
563 retnum = (UV) s[offset] << 8;
565 else if (size == 32) {
566 if (offset >= srclen)
568 else if (offset + 1 >= srclen)
570 ((UV) s[offset ] << 24);
571 else if (offset + 2 >= srclen)
573 ((UV) s[offset ] << 24) +
574 ((UV) s[offset + 1] << 16);
577 ((UV) s[offset ] << 24) +
578 ((UV) s[offset + 1] << 16) +
579 ( s[offset + 2] << 8);
582 else if (size == 64) {
584 if (ckWARN(WARN_PORTABLE))
585 Perl_warner(aTHX_ WARN_PORTABLE,
586 "Bit vector size > 32 non-portable");
587 if (offset >= srclen)
589 else if (offset + 1 >= srclen)
591 (UV) s[offset ] << 56;
592 else if (offset + 2 >= srclen)
594 ((UV) s[offset ] << 56) +
595 ((UV) s[offset + 1] << 48);
596 else if (offset + 3 >= srclen)
598 ((UV) s[offset ] << 56) +
599 ((UV) s[offset + 1] << 48) +
600 ((UV) s[offset + 2] << 40);
601 else if (offset + 4 >= srclen)
603 ((UV) s[offset ] << 56) +
604 ((UV) s[offset + 1] << 48) +
605 ((UV) s[offset + 2] << 40) +
606 ((UV) s[offset + 3] << 32);
607 else if (offset + 5 >= srclen)
609 ((UV) s[offset ] << 56) +
610 ((UV) s[offset + 1] << 48) +
611 ((UV) s[offset + 2] << 40) +
612 ((UV) s[offset + 3] << 32) +
613 ( s[offset + 4] << 24);
614 else if (offset + 6 >= srclen)
616 ((UV) s[offset ] << 56) +
617 ((UV) s[offset + 1] << 48) +
618 ((UV) s[offset + 2] << 40) +
619 ((UV) s[offset + 3] << 32) +
620 ((UV) s[offset + 4] << 24) +
621 ((UV) s[offset + 5] << 16);
624 ((UV) s[offset ] << 56) +
625 ((UV) s[offset + 1] << 48) +
626 ((UV) s[offset + 2] << 40) +
627 ((UV) s[offset + 3] << 32) +
628 ((UV) s[offset + 4] << 24) +
629 ((UV) s[offset + 5] << 16) +
630 ( s[offset + 6] << 8);
636 retnum = (s[offset >> 3] >> (offset & 7)) & ((1 << size) - 1);
638 offset >>= 3; /* turn into byte offset */
643 ((UV) s[offset] << 8) +
647 ((UV) s[offset ] << 24) +
648 ((UV) s[offset + 1] << 16) +
649 ( s[offset + 2] << 8) +
652 else if (size == 64) {
654 if (ckWARN(WARN_PORTABLE))
655 Perl_warner(aTHX_ WARN_PORTABLE,
656 "Bit vector size > 32 non-portable");
658 ((UV) s[offset ] << 56) +
659 ((UV) s[offset + 1] << 48) +
660 ((UV) s[offset + 2] << 40) +
661 ((UV) s[offset + 3] << 32) +
662 ((UV) s[offset + 4] << 24) +
663 ((UV) s[offset + 5] << 16) +
664 ( s[offset + 6] << 8) +
673 /* XXX SvUTF8 support missing! */
675 Perl_do_vecset(pTHX_ SV *sv)
677 SV *targ = LvTARG(sv);
680 register unsigned char *s;
688 s = (unsigned char*)SvPV_force(targ, targlen);
689 (void)SvPOK_only(targ);
691 offset = LvTARGOFF(sv);
692 size = LvTARGLEN(sv);
693 if (size < 1 || (size & (size-1))) /* size < 1 or not a power of two */
694 Perl_croak(aTHX_ "Illegal number of bits in vec");
696 offset *= size; /* turn into bit offset */
697 len = (offset + size + 7) / 8; /* required number of bytes */
699 s = (unsigned char*)SvGROW(targ, len + 1);
700 (void)memzero((char *)(s + targlen), len - targlen + 1);
701 SvCUR_set(targ, len);
705 mask = (1 << size) - 1;
708 offset >>= 3; /* turn into byte offset */
709 s[offset] &= ~(mask << size);
710 s[offset] |= lval << size;
713 offset >>= 3; /* turn into byte offset */
715 s[offset ] = lval & 0xff;
716 else if (size == 16) {
717 s[offset ] = (lval >> 8) & 0xff;
718 s[offset+1] = lval & 0xff;
720 else if (size == 32) {
721 s[offset ] = (lval >> 24) & 0xff;
722 s[offset+1] = (lval >> 16) & 0xff;
723 s[offset+2] = (lval >> 8) & 0xff;
724 s[offset+3] = lval & 0xff;
727 else if (size == 64) {
729 if (ckWARN(WARN_PORTABLE))
730 Perl_warner(aTHX_ WARN_PORTABLE,
731 "Bit vector size > 32 non-portable");
732 s[offset ] = (lval >> 56) & 0xff;
733 s[offset+1] = (lval >> 48) & 0xff;
734 s[offset+2] = (lval >> 40) & 0xff;
735 s[offset+3] = (lval >> 32) & 0xff;
736 s[offset+4] = (lval >> 24) & 0xff;
737 s[offset+5] = (lval >> 16) & 0xff;
738 s[offset+6] = (lval >> 8) & 0xff;
739 s[offset+7] = lval & 0xff;
747 Perl_do_chop(pTHX_ register SV *astr, register SV *sv)
753 if (SvTYPE(sv) == SVt_PVAV) {
758 for (i = 0; i <= max; i++) {
759 sv = (SV*)av_fetch(av, i, FALSE);
760 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
765 else if (SvTYPE(sv) == SVt_PVHV) {
768 (void)hv_iterinit(hv);
770 while ((entry = hv_iternext(hv)))
771 do_chop(astr,hv_iterval(hv,entry));
774 else if (SvREADONLY(sv))
775 Perl_croak(aTHX_ PL_no_modify);
777 if (len && !SvPOK(sv))
778 s = SvPV_force(sv, len);
781 char *send = s + len;
784 while ((*s & 0xc0) == 0x80)
786 if (UTF8SKIP(s) != send - s && ckWARN_d(WARN_UTF8))
787 Perl_warner(aTHX_ WARN_UTF8, "Malformed UTF-8 character");
788 sv_setpvn(astr, s, send - s);
790 SvCUR_set(sv, s - start);
795 sv_setpvn(astr, "", 0);
799 sv_setpvn(astr, s, 1);
806 sv_setpvn(astr, "", 0);
811 Perl_do_chomp(pTHX_ register SV *sv)
823 if (SvTYPE(sv) == SVt_PVAV) {
828 for (i = 0; i <= max; i++) {
829 sv = (SV*)av_fetch(av, i, FALSE);
830 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
831 count += do_chomp(sv);
835 else if (SvTYPE(sv) == SVt_PVHV) {
838 (void)hv_iterinit(hv);
840 while ((entry = hv_iternext(hv)))
841 count += do_chomp(hv_iterval(hv,entry));
844 else if (SvREADONLY(sv))
845 Perl_croak(aTHX_ PL_no_modify);
847 if (len && !SvPOKp(sv))
848 s = SvPV_force(sv, len);
855 while (len && s[-1] == '\n') {
863 char *rsptr = SvPV(PL_rs, rslen);
874 if (memNE(s, rsptr, rslen))
889 Perl_do_vop(pTHX_ I32 optype, SV *sv, SV *left, SV *right)
891 dTHR; /* just for taint */
906 bool left_utf = DO_UTF8(left);
907 bool right_utf = DO_UTF8(right);
910 if (left_utf && !right_utf)
911 sv_utf8_upgrade(right);
912 if (!left_utf && right_utf)
913 sv_utf8_upgrade(left);
915 if (sv != left || (optype != OP_BIT_AND && !SvOK(sv) && !SvGMAGICAL(sv)))
916 sv_setpvn(sv, "", 0); /* avoid undef warning on |= and ^= */
917 lsave = lc = SvPV(left, leftlen);
918 rsave = rc = SvPV(right, rightlen);
919 len = leftlen < rightlen ? leftlen : rightlen;
921 if ((left_utf || right_utf) && (sv == left || sv == right)) {
922 needlen = optype == OP_BIT_AND ? len : leftlen + rightlen;
923 Newz(801, dc, needlen + 1, char);
925 else if (SvOK(sv) || SvTYPE(sv) > SVt_PVMG) {
927 dc = SvPV_force(sv, n_a);
928 if (SvCUR(sv) < len) {
929 dc = SvGROW(sv, len + 1);
930 (void)memzero(dc + SvCUR(sv), len - SvCUR(sv) + 1);
932 if (optype != OP_BIT_AND && (left_utf || right_utf))
933 dc = SvGROW(sv, leftlen + rightlen + 1);
936 needlen = ((optype == OP_BIT_AND)
937 ? len : (leftlen > rightlen ? leftlen : rightlen));
938 Newz(801, dc, needlen + 1, char);
939 (void)sv_usepvn(sv, dc, needlen);
940 dc = SvPVX(sv); /* sv_usepvn() calls Renew() */
943 (void)SvPOK_only(sv);
944 if (left_utf || right_utf) {
947 STRLEN lulen = leftlen;
948 STRLEN rulen = rightlen;
953 while (lulen && rulen) {
954 luc = utf8_to_uv((U8*)lc, &ulen);
957 ruc = utf8_to_uv((U8*)rc, &ulen);
961 dc = (char*)uv_to_utf8((U8*)dc, duc);
963 if (sv == left || sv == right)
964 (void)sv_usepvn(sv, dcsave, needlen);
965 SvCUR_set(sv, dc - dcsave);
968 while (lulen && rulen) {
969 luc = utf8_to_uv((U8*)lc, &ulen);
972 ruc = utf8_to_uv((U8*)rc, &ulen);
976 dc = (char*)uv_to_utf8((U8*)dc, duc);
980 while (lulen && rulen) {
981 luc = utf8_to_uv((U8*)lc, &ulen);
984 ruc = utf8_to_uv((U8*)rc, &ulen);
988 dc = (char*)uv_to_utf8((U8*)dc, duc);
991 if (sv == left || sv == right)
992 (void)sv_usepvn(sv, dcsave, needlen);
993 SvCUR_set(sv, dc - dcsave);
995 sv_catpvn(sv, rc, rulen);
997 sv_catpvn(sv, lc, lulen);
1007 if (len >= sizeof(long)*4 &&
1008 !((long)dc % sizeof(long)) &&
1009 !((long)lc % sizeof(long)) &&
1010 !((long)rc % sizeof(long))) /* It's almost always aligned... */
1012 I32 remainder = len % (sizeof(long)*4);
1013 len /= (sizeof(long)*4);
1022 *dl++ = *ll++ & *rl++;
1023 *dl++ = *ll++ & *rl++;
1024 *dl++ = *ll++ & *rl++;
1025 *dl++ = *ll++ & *rl++;
1030 *dl++ = *ll++ ^ *rl++;
1031 *dl++ = *ll++ ^ *rl++;
1032 *dl++ = *ll++ ^ *rl++;
1033 *dl++ = *ll++ ^ *rl++;
1038 *dl++ = *ll++ | *rl++;
1039 *dl++ = *ll++ | *rl++;
1040 *dl++ = *ll++ | *rl++;
1041 *dl++ = *ll++ | *rl++;
1056 *dc++ = *lc++ & *rc++;
1060 *dc++ = *lc++ ^ *rc++;
1064 *dc++ = *lc++ | *rc++;
1068 sv_catpvn(sv, rsave + len, rightlen - len);
1069 else if (leftlen > len)
1070 sv_catpvn(sv, lsave + len, leftlen - len);
1088 I32 gimme = GIMME_V;
1089 I32 dokeys = (PL_op->op_type == OP_KEYS);
1090 I32 dovalues = (PL_op->op_type == OP_VALUES);
1091 I32 realhv = (SvTYPE(hv) == SVt_PVHV);
1093 if (PL_op->op_type == OP_RV2HV || PL_op->op_type == OP_PADHV)
1094 dokeys = dovalues = TRUE;
1097 if (PL_op->op_flags & OPf_MOD) { /* lvalue */
1098 dTARGET; /* make sure to clear its target here */
1099 if (SvTYPE(TARG) == SVt_PVLV)
1100 LvTARG(TARG) = Nullsv;
1106 keys = realhv ? hv : avhv_keys((AV*)hv);
1107 (void)hv_iterinit(keys); /* always reset iterator regardless */
1109 if (gimme == G_VOID)
1112 if (gimme == G_SCALAR) {
1116 if (PL_op->op_flags & OPf_MOD) { /* lvalue */
1117 if (SvTYPE(TARG) < SVt_PVLV) {
1118 sv_upgrade(TARG, SVt_PVLV);
1119 sv_magic(TARG, Nullsv, 'k', Nullch, 0);
1122 if (LvTARG(TARG) != (SV*)keys) {
1124 SvREFCNT_dec(LvTARG(TARG));
1125 LvTARG(TARG) = SvREFCNT_inc(keys);
1131 if (! SvTIED_mg((SV*)keys, 'P'))
1136 while (hv_iternext(keys)) i++;
1142 EXTEND(SP, HvKEYS(keys) * (dokeys + dovalues));
1144 PUTBACK; /* hv_iternext and hv_iterval might clobber stack_sp */
1145 while ((entry = hv_iternext(keys))) {
1148 XPUSHs(hv_iterkeysv(entry)); /* won't clobber stack_sp */
1152 hv_iterval(hv,entry) : avhv_iterval((AV*)hv,entry);
1153 DEBUG_H(Perl_sv_setpvf(aTHX_ tmpstr, "%lu%%%d=%lu",
1154 (unsigned long)HeHASH(entry),
1156 (unsigned long)(HeHASH(entry) & HvMAX(keys))));