3 * Copyright (c) 1991-1997, 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)
835 if (SvTYPE(sv) == SVt_PVAV) {
840 for (i = 0; i <= max; i++) {
841 sv = (SV*)av_fetch(av, i, FALSE);
842 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
843 count += do_chomp(sv);
847 if (SvTYPE(sv) == SVt_PVHV) {
850 (void)hv_iterinit(hv);
852 while (entry = hv_iternext(hv))
853 count += do_chomp(hv_iterval(hv,entry));
857 if (len && !SvPOKp(sv))
858 s = SvPV_force(sv, len);
865 while (len && s[-1] == '\n') {
873 char *rsptr = SvPV(PL_rs, rslen);
884 if (memNE(s, rsptr, rslen))
899 do_vop(I32 optype, SV *sv, SV *left, SV *right)
901 dTHR; /* just for taint */
917 if (sv != left || (optype != OP_BIT_AND && !SvOK(sv) && !SvGMAGICAL(sv)))
918 sv_setpvn(sv, "", 0); /* avoid undef warning on |= and ^= */
919 lsave = lc = SvPV(left, leftlen);
920 rsave = rc = SvPV(right, rightlen);
921 len = leftlen < rightlen ? leftlen : rightlen;
923 if (SvOK(sv) || SvTYPE(sv) > SVt_PVMG) {
925 dc = SvPV_force(sv, n_a);
926 if (SvCUR(sv) < len) {
927 dc = SvGROW(sv, len + 1);
928 (void)memzero(dc + SvCUR(sv), len - SvCUR(sv) + 1);
932 I32 needlen = ((optype == OP_BIT_AND)
933 ? len : (leftlen > rightlen ? leftlen : rightlen));
934 Newz(801, dc, needlen + 1, char);
935 (void)sv_usepvn(sv, dc, needlen);
936 dc = SvPVX(sv); /* sv_usepvn() calls Renew() */
939 (void)SvPOK_only(sv);
941 if (len >= sizeof(long)*4 &&
942 !((long)dc % sizeof(long)) &&
943 !((long)lc % sizeof(long)) &&
944 !((long)rc % sizeof(long))) /* It's almost always aligned... */
946 I32 remainder = len % (sizeof(long)*4);
947 len /= (sizeof(long)*4);
956 *dl++ = *ll++ & *rl++;
957 *dl++ = *ll++ & *rl++;
958 *dl++ = *ll++ & *rl++;
959 *dl++ = *ll++ & *rl++;
964 *dl++ = *ll++ ^ *rl++;
965 *dl++ = *ll++ ^ *rl++;
966 *dl++ = *ll++ ^ *rl++;
967 *dl++ = *ll++ ^ *rl++;
972 *dl++ = *ll++ | *rl++;
973 *dl++ = *ll++ | *rl++;
974 *dl++ = *ll++ | *rl++;
975 *dl++ = *ll++ | *rl++;
990 *dc++ = *lc++ & *rc++;
994 *dc++ = *lc++ ^ *rc++;
998 *dc++ = *lc++ | *rc++;
1002 sv_catpvn(sv, rsave + len, rightlen - len);
1003 else if (leftlen > len)
1004 sv_catpvn(sv, lsave + len, leftlen - len);
1021 I32 gimme = GIMME_V;
1022 I32 dokeys = (PL_op->op_type == OP_KEYS);
1023 I32 dovalues = (PL_op->op_type == OP_VALUES);
1024 I32 realhv = (SvTYPE(hv) == SVt_PVHV);
1026 if (PL_op->op_type == OP_RV2HV || PL_op->op_type == OP_PADHV)
1027 dokeys = dovalues = TRUE;
1030 if (PL_op->op_flags & OPf_MOD) { /* lvalue */
1031 dTARGET; /* make sure to clear its target here */
1032 if (SvTYPE(TARG) == SVt_PVLV)
1033 LvTARG(TARG) = Nullsv;
1039 keys = realhv ? hv : avhv_keys((AV*)hv);
1040 (void)hv_iterinit(keys); /* always reset iterator regardless */
1042 if (gimme == G_VOID)
1045 if (gimme == G_SCALAR) {
1049 if (PL_op->op_flags & OPf_MOD) { /* lvalue */
1050 if (SvTYPE(TARG) < SVt_PVLV) {
1051 sv_upgrade(TARG, SVt_PVLV);
1052 sv_magic(TARG, Nullsv, 'k', Nullch, 0);
1055 if (LvTARG(TARG) != (SV*)keys) {
1057 SvREFCNT_dec(LvTARG(TARG));
1058 LvTARG(TARG) = SvREFCNT_inc(keys);
1064 if (! SvTIED_mg((SV*)keys, 'P'))
1069 while (hv_iternext(keys)) i++;
1075 EXTEND(SP, HvKEYS(keys) * (dokeys + dovalues));
1077 PUTBACK; /* hv_iternext and hv_iterval might clobber stack_sp */
1078 while (entry = hv_iternext(keys)) {
1081 XPUSHs(hv_iterkeysv(entry)); /* won't clobber stack_sp */
1085 hv_iterval(hv,entry) : avhv_iterval((AV*)hv,entry);
1086 DEBUG_H(sv_setpvf(tmpstr, "%lu%%%d=%lu",
1087 (unsigned long)HeHASH(entry),
1089 (unsigned long)(HeHASH(entry) & HvMAX(keys))));