3 * Copyright (c) 1991-1999, 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."
17 #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
22 static I32 do_trans_CC_simple _((SV *sv));
23 static I32 do_trans_CC_count _((SV *sv));
24 static I32 do_trans_CC_complex _((SV *sv));
25 static I32 do_trans_UU_simple _((SV *sv));
26 static I32 do_trans_UU_count _((SV *sv));
27 static I32 do_trans_UU_complex _((SV *sv));
28 static I32 do_trans_UC_simple _((SV *sv));
29 static I32 do_trans_CU_simple _((SV *sv));
30 static I32 do_trans_UC_trivial _((SV *sv));
31 static I32 do_trans_CU_trivial _((SV *sv));
35 do_trans_CC_simple(SV *sv)
45 tbl = (short*)cPVOP->op_pv;
47 croak("panic: do_trans");
49 s = (U8*)SvPV(sv, len);
53 if ((ch = tbl[*s]) >= 0) {
65 do_trans_CC_count(SV *sv)
74 tbl = (short*)cPVOP->op_pv;
76 croak("panic: do_trans");
78 s = (U8*)SvPV(sv, len);
91 do_trans_CC_complex(SV *sv)
102 tbl = (short*)cPVOP->op_pv;
104 croak("panic: do_trans");
106 s = (U8*)SvPV(sv, len);
110 if (PL_op->op_private & OPpTRANS_SQUASH) {
114 if ((ch = tbl[*s]) >= 0) {
117 if (p == d - 1 && *p == *d)
122 else if (ch == -1) /* -1 is unmapped character */
123 *d++ = *s; /* -2 is delete character */
129 if ((ch = tbl[*s]) >= 0) {
134 else if (ch == -1) /* -1 is unmapped character */
135 *d++ = *s; /* -2 is delete character */
139 matches += send - d; /* account for disappeared chars */
141 SvCUR_set(sv, d - (U8*)SvPVX(sv));
148 do_trans_UU_simple(SV *sv)
157 SV* rv = (SV*)cSVOP->op_sv;
158 HV* hv = (HV*)SvRV(rv);
159 SV** svp = hv_fetch(hv, "NONE", 4, FALSE);
160 UV none = svp ? SvUV(*svp) : 0x7fffffff;
165 s = (U8*)SvPV(sv, len);
168 svp = hv_fetch(hv, "FINAL", 5, FALSE);
174 if ((uv = swash_fetch(rv, s)) < none) {
177 d = uv_to_utf8(d, uv);
179 else if (uv == none) {
181 for (i = UTF8SKIP(s); i; i--)
184 else if (uv == extra) {
187 d = uv_to_utf8(d, final);
193 SvCUR_set(sv, d - (U8*)SvPVX(sv));
200 do_trans_UU_count(SV *sv)
208 SV* rv = (SV*)cSVOP->op_sv;
209 HV* hv = (HV*)SvRV(rv);
210 SV** svp = hv_fetch(hv, "NONE", 4, FALSE);
211 UV none = svp ? SvUV(*svp) : 0x7fffffff;
214 s = (U8*)SvPV(sv, len);
218 if ((uv = swash_fetch(rv, s)) < none)
227 do_trans_UC_simple(SV *sv)
236 SV* rv = (SV*)cSVOP->op_sv;
237 HV* hv = (HV*)SvRV(rv);
238 SV** svp = hv_fetch(hv, "NONE", 4, FALSE);
239 UV none = svp ? SvUV(*svp) : 0x7fffffff;
244 s = (U8*)SvPV(sv, len);
247 svp = hv_fetch(hv, "FINAL", 5, FALSE);
253 if ((uv = swash_fetch(rv, s)) < none) {
258 else if (uv == none) {
260 uv = utf8_to_uv(s, &ulen);
264 else if (uv == extra) {
273 SvCUR_set(sv, d - (U8*)SvPVX(sv));
280 do_trans_CU_simple(SV *sv)
290 SV* rv = (SV*)cSVOP->op_sv;
291 HV* hv = (HV*)SvRV(rv);
292 SV** svp = hv_fetch(hv, "NONE", 4, FALSE);
293 UV none = svp ? SvUV(*svp) : 0x7fffffff;
300 s = (U8*)SvPV(sv, len);
303 svp = hv_fetch(hv, "BITS", 4, FALSE);
305 bits = (I32)SvIV(*svp);
307 svp = hv_fetch(hv, "FINAL", 5, FALSE);
311 Newz(801, d, len * (bits >> 3) + 1, U8);
319 tmpbuf[0] = (( uv >> 6) | 0xc0);
320 tmpbuf[1] = (( uv & 0x3f) | 0x80);
323 if ((uv = swash_fetch(rv, tmpbuf)) < none) {
325 d = uv_to_utf8(d, uv);
328 d = uv_to_utf8(d, s[-1]);
329 else if (uv == extra) {
331 d = uv_to_utf8(d, final);
335 sv_usepvn_mg(sv, (char*)dst, d - dst);
340 /* utf-8 to latin-1 */
343 do_trans_UC_trivial(SV *sv)
351 s = (U8*)SvPV(sv, len);
360 UV uv = utf8_to_uv(s, &ulen);
366 SvCUR_set(sv, d - (U8*)SvPVX(sv));
372 /* latin-1 to utf-8 */
375 do_trans_CU_trivial(SV *sv)
385 s = (U8*)SvPV(sv, len);
388 Newz(801, d, len * 2 + 1, U8);
398 *d++ = (( uv >> 6) | 0xc0);
399 *d++ = (( uv & 0x3f) | 0x80);
403 sv_usepvn_mg(sv, (char*)dst, d - dst);
409 do_trans_UU_complex(SV *sv)
416 I32 squash = PL_op->op_private & OPpTRANS_SQUASH;
417 I32 from_utf = PL_op->op_private & OPpTRANS_FROM_UTF;
418 I32 to_utf = PL_op->op_private & OPpTRANS_TO_UTF;
419 I32 del = PL_op->op_private & OPpTRANS_DELETE;
420 SV* rv = (SV*)cSVOP->op_sv;
421 HV* hv = (HV*)SvRV(rv);
422 SV** svp = hv_fetch(hv, "NONE", 4, FALSE);
423 UV none = svp ? SvUV(*svp) : 0x7fffffff;
430 s = (U8*)SvPV(sv, len);
433 svp = hv_fetch(hv, "FINAL", 5, FALSE);
437 if (PL_op->op_private & OPpTRANS_GROWS) {
440 svp = hv_fetch(hv, "BITS", 4, FALSE);
442 bits = (I32)SvIV(*svp);
444 Newz(801, d, len * (bits >> 3) + 1, U8);
456 uv = swash_fetch(rv, s);
464 tmpbuf[0] = (( uv >> 6) | 0xc0);
465 tmpbuf[1] = (( uv & 0x3f) | 0x80);
467 uv = swash_fetch(rv, tmpbuf);
472 if (uv >= 0x80 && to_utf)
473 d = uv_to_utf8(d, uv);
482 else if (uv == none) { /* "none" is unmapped character */
488 for (i = UTF8SKIP(s); i; --i)
493 *d++ = (U8)utf8_to_uv(s, &ulen);
497 else { /* must be to_utf only */
498 d = uv_to_utf8(d, s[-1]);
503 else if (uv == extra && !del) {
506 if (final >= 0x80 && to_utf)
507 d = uv_to_utf8(d, final);
516 matches++; /* "none+1" is delete character */
524 uv = swash_fetch(rv, s);
532 tmpbuf[0] = (( uv >> 6) | 0xc0);
533 tmpbuf[1] = (( uv & 0x3f) | 0x80);
535 uv = swash_fetch(rv, tmpbuf);
539 if (uv >= 0x80 && to_utf)
540 d = uv_to_utf8(d, uv);
547 else if (uv == none) { /* "none" is unmapped character */
553 for (i = UTF8SKIP(s); i; --i)
558 *d++ = (U8)utf8_to_uv(s, &ulen);
562 else { /* must be to_utf only */
563 d = uv_to_utf8(d, s[-1]);
567 else if (uv == extra && !del) {
569 if (final >= 0x80 && to_utf)
570 d = uv_to_utf8(d, final);
577 matches++; /* "none+1" is delete character */
583 sv_usepvn(sv, (char*)dst, d - dst);
586 SvCUR_set(sv, d - (U8*)SvPVX(sv));
599 if (SvREADONLY(sv) && !(PL_op->op_private & OPpTRANS_IDENTICAL))
606 (void)SvPV_force(sv, len);
607 (void)SvPOK_only(sv);
609 DEBUG_t( deb("2.TBL\n"));
611 switch (PL_op->op_private & 63) {
613 return do_trans_CC_simple(sv);
615 case OPpTRANS_FROM_UTF:
616 return do_trans_UC_simple(sv);
618 case OPpTRANS_TO_UTF:
619 return do_trans_CU_simple(sv);
621 case OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF:
622 return do_trans_UU_simple(sv);
624 case OPpTRANS_IDENTICAL:
625 return do_trans_CC_count(sv);
627 case OPpTRANS_FROM_UTF|OPpTRANS_IDENTICAL:
628 return do_trans_UC_trivial(sv);
630 case OPpTRANS_TO_UTF|OPpTRANS_IDENTICAL:
631 return do_trans_CU_trivial(sv);
633 case OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF|OPpTRANS_IDENTICAL:
634 return do_trans_UU_count(sv);
637 if (PL_op->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF))
638 return do_trans_UU_complex(sv); /* could be UC or CU too */
640 return do_trans_CC_complex(sv);
645 do_join(register SV *sv, SV *del, register SV **mark, register SV **sp)
648 register I32 items = sp - mark;
651 register char *delim = SvPV(del, delimlen);
655 len = (items > 0 ? (delimlen * (items - 1) ) : 0);
656 (void)SvUPGRADE(sv, SVt_PV);
657 if (SvLEN(sv) < len + items) { /* current length is way too short */
658 while (items-- > 0) {
659 if (*mark && !SvGMAGICAL(*mark) && SvOK(*mark)) {
665 SvGROW(sv, len + 1); /* so try to pre-extend */
676 s = SvPV(*mark, tmplen);
677 sv_setpvn(sv, s, tmplen);
687 for (; items > 0; items--,mark++) {
688 sv_catpvn(sv,delim,len);
693 for (; items > 0; items--,mark++)
700 do_sprintf(SV *sv, I32 len, SV **sarg)
703 char *pat = SvPV(*sarg, patlen);
704 bool do_taint = FALSE;
706 sv_vsetpvfn(sv, pat, patlen, Null(va_list*), sarg + 1, len - 1, &do_taint);
715 SV *targ = LvTARG(sv);
718 register unsigned char *s;
719 register unsigned long lval;
726 s = (unsigned char*)SvPV_force(targ, targlen);
727 lval = U_L(SvNV(sv));
728 offset = LvTARGOFF(sv);
729 size = LvTARGLEN(sv);
731 len = (offset + size + 7) / 8;
733 s = (unsigned char*)SvGROW(targ, len + 1);
734 (void)memzero(s + targlen, len - targlen + 1);
735 SvCUR_set(targ, len);
739 mask = (1 << size) - 1;
743 s[offset] &= ~(mask << size);
744 s[offset] |= lval << size;
749 s[offset] = lval & 255;
750 else if (size == 16) {
751 s[offset] = (lval >> 8) & 255;
752 s[offset+1] = lval & 255;
754 else if (size == 32) {
755 s[offset] = (lval >> 24) & 255;
756 s[offset+1] = (lval >> 16) & 255;
757 s[offset+2] = (lval >> 8) & 255;
758 s[offset+3] = lval & 255;
764 do_chop(register SV *astr, register SV *sv)
770 if (SvTYPE(sv) == SVt_PVAV) {
775 for (i = 0; i <= max; i++) {
776 sv = (SV*)av_fetch(av, i, FALSE);
777 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
782 if (SvTYPE(sv) == SVt_PVHV) {
785 (void)hv_iterinit(hv);
787 while (entry = hv_iternext(hv))
788 do_chop(astr,hv_iterval(hv,entry));
792 if (len && !SvPOK(sv))
793 s = SvPV_force(sv, len);
796 char *send = s + len;
799 while ((*s & 0xc0) == 0x80)
801 if (UTF8SKIP(s) != send - s)
802 warn("Malformed UTF-8 character");
803 sv_setpvn(astr, s, send - s);
805 SvCUR_set(sv, s - start);
809 sv_setpvn(astr, "", 0);
814 sv_setpvn(astr, s, 1);
820 sv_setpvn(astr, "", 0);
825 do_chomp(register SV *sv)
837 if (SvTYPE(sv) == SVt_PVAV) {
842 for (i = 0; i <= max; i++) {
843 sv = (SV*)av_fetch(av, i, FALSE);
844 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
845 count += do_chomp(sv);
849 if (SvTYPE(sv) == SVt_PVHV) {
852 (void)hv_iterinit(hv);
854 while (entry = hv_iternext(hv))
855 count += do_chomp(hv_iterval(hv,entry));
859 if (len && !SvPOKp(sv))
860 s = SvPV_force(sv, len);
867 while (len && s[-1] == '\n') {
875 char *rsptr = SvPV(PL_rs, rslen);
886 if (memNE(s, rsptr, rslen))
901 do_vop(I32 optype, SV *sv, SV *left, SV *right)
903 dTHR; /* just for taint */
919 if (sv != left || (optype != OP_BIT_AND && !SvOK(sv) && !SvGMAGICAL(sv)))
920 sv_setpvn(sv, "", 0); /* avoid undef warning on |= and ^= */
921 lsave = lc = SvPV(left, leftlen);
922 rsave = rc = SvPV(right, rightlen);
923 len = leftlen < rightlen ? leftlen : rightlen;
925 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);
934 I32 needlen = ((optype == OP_BIT_AND)
935 ? len : (leftlen > rightlen ? leftlen : rightlen));
936 Newz(801, dc, needlen + 1, char);
937 (void)sv_usepvn(sv, dc, needlen);
938 dc = SvPVX(sv); /* sv_usepvn() calls Renew() */
941 (void)SvPOK_only(sv);
943 if (len >= sizeof(long)*4 &&
944 !((long)dc % sizeof(long)) &&
945 !((long)lc % sizeof(long)) &&
946 !((long)rc % sizeof(long))) /* It's almost always aligned... */
948 I32 remainder = len % (sizeof(long)*4);
949 len /= (sizeof(long)*4);
958 *dl++ = *ll++ & *rl++;
959 *dl++ = *ll++ & *rl++;
960 *dl++ = *ll++ & *rl++;
961 *dl++ = *ll++ & *rl++;
966 *dl++ = *ll++ ^ *rl++;
967 *dl++ = *ll++ ^ *rl++;
968 *dl++ = *ll++ ^ *rl++;
969 *dl++ = *ll++ ^ *rl++;
974 *dl++ = *ll++ | *rl++;
975 *dl++ = *ll++ | *rl++;
976 *dl++ = *ll++ | *rl++;
977 *dl++ = *ll++ | *rl++;
992 *dc++ = *lc++ & *rc++;
996 *dc++ = *lc++ ^ *rc++;
1000 *dc++ = *lc++ | *rc++;
1004 sv_catpvn(sv, rsave + len, rightlen - len);
1005 else if (leftlen > len)
1006 sv_catpvn(sv, lsave + len, leftlen - len);
1023 I32 gimme = GIMME_V;
1024 I32 dokeys = (PL_op->op_type == OP_KEYS);
1025 I32 dovalues = (PL_op->op_type == OP_VALUES);
1026 I32 realhv = (SvTYPE(hv) == SVt_PVHV);
1028 if (PL_op->op_type == OP_RV2HV || PL_op->op_type == OP_PADHV)
1029 dokeys = dovalues = TRUE;
1032 if (PL_op->op_flags & OPf_MOD) { /* lvalue */
1033 dTARGET; /* make sure to clear its target here */
1034 if (SvTYPE(TARG) == SVt_PVLV)
1035 LvTARG(TARG) = Nullsv;
1041 keys = realhv ? hv : avhv_keys((AV*)hv);
1042 (void)hv_iterinit(keys); /* always reset iterator regardless */
1044 if (gimme == G_VOID)
1047 if (gimme == G_SCALAR) {
1051 if (PL_op->op_flags & OPf_MOD) { /* lvalue */
1052 if (SvTYPE(TARG) < SVt_PVLV) {
1053 sv_upgrade(TARG, SVt_PVLV);
1054 sv_magic(TARG, Nullsv, 'k', Nullch, 0);
1057 if (LvTARG(TARG) != (SV*)keys) {
1059 SvREFCNT_dec(LvTARG(TARG));
1060 LvTARG(TARG) = SvREFCNT_inc(keys);
1066 if (! SvTIED_mg((SV*)keys, 'P'))
1071 while (hv_iternext(keys)) i++;
1077 EXTEND(SP, HvKEYS(keys) * (dokeys + dovalues));
1079 PUTBACK; /* hv_iternext and hv_iterval might clobber stack_sp */
1080 while (entry = hv_iternext(keys)) {
1083 XPUSHs(hv_iterkeysv(entry)); /* won't clobber stack_sp */
1087 hv_iterval(hv,entry) : avhv_iterval((AV*)hv,entry);
1088 DEBUG_H(sv_setpvf(tmpstr, "%lu%%%d=%lu",
1089 (unsigned long)HeHASH(entry),
1091 (unsigned long)(HeHASH(entry) & HvMAX(keys))));