3 * Copyright (c) 1991-1994, 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 * "It's a big house this, and very peculiar. Always a bit more to discover,
12 * and no knowing what you'll find around a corner. And Elves, sir!" --Samwise
18 static void doencodes _((SV *sv, char *s, I32 len));
20 /* variations on pp_null */
25 if (GIMME != G_ARRAY) {
41 if (op->op_private & OPpLVAL_INTRO)
42 SAVECLEARSV(curpad[op->op_targ]);
44 if (op->op_flags & OPf_REF) {
48 if (GIMME == G_ARRAY) {
49 I32 maxarg = AvFILL((AV*)TARG) + 1;
51 Copy(AvARRAY((AV*)TARG), SP+1, maxarg, SV*);
55 SV* sv = sv_newmortal();
56 I32 maxarg = AvFILL((AV*)TARG) + 1;
67 if (op->op_private & OPpLVAL_INTRO)
68 SAVECLEARSV(curpad[op->op_targ]);
69 if (op->op_flags & OPf_REF)
71 if (GIMME == G_ARRAY) { /* array wanted */
72 RETURNOP(do_kv(ARGS));
75 SV* sv = sv_newmortal();
76 if (HvFILL((HV*)TARG)) {
77 sprintf(buf, "%d/%d", HvFILL((HV*)TARG), HvMAX((HV*)TARG)+1);
89 DIE("NOT IMPL LINE %d",__LINE__);
101 if (SvTYPE(sv) != SVt_PVGV)
102 DIE("Not a GLOB reference");
105 if (SvTYPE(sv) != SVt_PVGV) {
106 if (SvGMAGICAL(sv)) {
112 if (op->op_flags & OPf_REF ||
113 op->op_private & HINT_STRICT_REFS)
114 DIE(no_usym, "a symbol");
117 if (op->op_private & HINT_STRICT_REFS)
118 DIE(no_symref, "a symbol");
119 sv = (SV*)gv_fetchpv(SvPV(sv, na), TRUE, SVt_PVGV);
122 if (op->op_private & OPpLVAL_INTRO) {
130 if (op->op_flags & OPf_SPECIAL) {
131 GvGP(sv)->gp_refcnt++; /* will soon be assigned */
132 GvFLAGS(sv) |= GVf_INTRO;
139 GvSV(sv) = NEWSV(72,0);
140 GvLINE(sv) = curcop->cop_line;
163 switch (SvTYPE(sv)) {
167 DIE("Not a SCALAR reference");
172 if (SvTYPE(gv) != SVt_PVGV) {
173 if (SvGMAGICAL(sv)) {
179 if (op->op_flags & OPf_REF ||
180 op->op_private & HINT_STRICT_REFS)
181 DIE(no_usym, "a SCALAR");
184 if (op->op_private & HINT_STRICT_REFS)
185 DIE(no_symref, "a SCALAR");
186 gv = (SV*)gv_fetchpv(SvPV(sv, na), TRUE, SVt_PV);
190 if (op->op_flags & OPf_MOD) {
191 if (op->op_private & OPpLVAL_INTRO)
192 sv = save_scalar((GV*)TOPs);
193 else if (op->op_private & (OPpDEREF_HV|OPpDEREF_AV)) {
197 (void)SvUPGRADE(sv, SVt_RV);
198 SvRV(sv) = (op->op_private & OPpDEREF_HV ?
199 (SV*)newHV() : (SV*)newAV());
213 SV *sv = AvARYLEN(av);
215 AvARYLEN(av) = sv = NEWSV(0,0);
216 sv_upgrade(sv, SVt_IV);
217 sv_magic(sv, (SV*)av, '#', Nullch, 0);
225 dSP; dTARGET; dPOPss;
227 if (op->op_flags & OPf_MOD) {
230 PUSHs(TARG); /* no SvSETMAGIC */
236 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
237 mg = mg_find(sv, 'g');
238 if (mg && mg->mg_len >= 0) {
239 PUSHi(mg->mg_len + curcop->cop_arybase);
253 /* We always try to add a non-existent subroutine in case of AUTOLOAD. */
254 CV *cv = sv_2cv(TOPs, &stash, &gv, TRUE);
263 XPUSHs(cSVOP->op_sv);
272 sv_upgrade(rv, SVt_RV);
277 (void)SvREFCNT_inc(sv);
290 if (GIMME != G_ARRAY) {
297 sv_upgrade(rv, SVt_RV);
302 (void)SvREFCNT_inc(sv);
318 if (!sv || !SvROK(sv))
322 pv = sv_reftype(sv,TRUE);
323 PUSHp(pv, strlen(pv));
333 stash = curcop->cop_stash;
335 stash = gv_stashsv(POPs, TRUE);
337 (void)sv_bless(TOPs, stash);
341 /* Pattern matching */
346 register unsigned char *s;
349 register I32 *sfirst;
354 s = (unsigned char*)(SvPV(TARG, len));
357 SvSCREAM_off(lastscream);
363 if (pos > maxscream) {
365 maxscream = pos + 80;
366 New(301, screamfirst, 256, I32);
367 New(302, screamnext, maxscream, I32);
370 maxscream = pos + pos / 4;
371 Renew(screamnext, maxscream, I32);
375 sfirst = screamfirst;
378 if (!sfirst || !snext)
379 DIE("do_study: out of memory");
381 for (ch = 256; ch; --ch)
388 snext[pos] = sfirst[ch] - pos;
393 /* If there were any case insensitive searches, we must assume they
394 * all are. This speeds up insensitive searches much more than
395 * it slows down sensitive ones.
398 sfirst[fold[ch]] = pos;
404 XPUSHs(sv_2mortal(newSViv((I32)retval)));
413 if (op->op_flags & OPf_STACKED)
420 PUSHi(do_trans(sv, op));
424 /* Lvalue operators. */
446 SETi(do_chomp(TOPs));
453 register I32 count = 0;
456 count += do_chomp(POPs);
467 if (!sv || !SvANY(sv))
469 switch (SvTYPE(sv)) {
479 if (CvROOT(sv) || CvXSUB(sv))
503 if (SvTHINKFIRST(sv)) {
510 switch (SvTYPE(sv)) {
524 if (sv != GvSV(defgv)) {
525 if (SvPOK(sv) && SvLEN(sv)) {
528 SvPV_set(sv, Nullch);
550 sv_setsv(TARG, TOPs);
562 sv_setsv(TARG, TOPs);
569 /* Ordinary operators. */
573 dSP; dATARGET; tryAMAGICbin(pow,opASSIGN);
576 SETn( pow( left, right) );
583 dSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
586 SETn( left * right );
593 dSP; dATARGET; tryAMAGICbin(div,opASSIGN);
597 DIE("Illegal division by zero");
599 /* insure that 20./5. == 4. */
604 if ((double)I_32(x) == x &&
605 (double)I_32(value) == value &&
606 (k = I_32(x)/I_32(value))*I_32(value) == I_32(x)) {
613 value = POPn / value;
622 dSP; dATARGET; tryAMAGICbin(mod,opASSIGN);
624 register unsigned long tmpulong;
625 register long tmplong;
628 tmpulong = (unsigned long) POPn;
630 DIE("Illegal modulus zero");
633 value = (I32)(((unsigned long)value) % tmpulong);
635 tmplong = (long)value;
636 value = (I32)(tmpulong - ((-tmplong - 1) % tmpulong)) - 1;
646 register I32 count = POPi;
647 if (GIMME == G_ARRAY && op->op_private & OPpREPEAT_DOLIST) {
649 I32 items = SP - MARK;
661 repeatcpy((char*)(MARK + items), (char*)MARK,
662 items * sizeof(SV*), count - 1);
668 else { /* Note: mark already snarfed by pp_list */
673 if (TARG == tmpstr && SvTHINKFIRST(tmpstr)) {
674 if (SvREADONLY(tmpstr) && curcop != &compiling)
675 DIE("Can't x= to readonly value");
679 SvSetSV(TARG, tmpstr);
680 SvPV_force(TARG, len);
682 SvGROW(TARG, (count * len) + 1);
684 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
685 SvCUR(TARG) *= count;
687 (void)SvPOK_only(TARG);
690 sv_setsv(TARG, &sv_no);
698 dSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
701 SETn( left - right );
708 dSP; dATARGET; tryAMAGICbin(lshift,opASSIGN);
711 SETi( left << right );
718 dSP; dATARGET; tryAMAGICbin(rshift,opASSIGN);
721 SETi( left >> right );
728 dSP; tryAMAGICbinSET(lt,0);
731 SETs((TOPn < value) ? &sv_yes : &sv_no);
738 dSP; tryAMAGICbinSET(gt,0);
741 SETs((TOPn > value) ? &sv_yes : &sv_no);
748 dSP; tryAMAGICbinSET(le,0);
751 SETs((TOPn <= value) ? &sv_yes : &sv_no);
758 dSP; tryAMAGICbinSET(ge,0);
761 SETs((TOPn >= value) ? &sv_yes : &sv_no);
768 dSP; tryAMAGICbinSET(ne,0);
771 SETs((TOPn != value) ? &sv_yes : &sv_no);
778 dSP; dTARGET; tryAMAGICbin(ncmp,0);
785 else if (left < right)
796 dSP; tryAMAGICbinSET(slt,0);
799 SETs( sv_cmp(left, right) < 0 ? &sv_yes : &sv_no );
806 dSP; tryAMAGICbinSET(sgt,0);
809 SETs( sv_cmp(left, right) > 0 ? &sv_yes : &sv_no );
816 dSP; tryAMAGICbinSET(sle,0);
819 SETs( sv_cmp(left, right) <= 0 ? &sv_yes : &sv_no );
826 dSP; tryAMAGICbinSET(sge,0);
829 SETs( sv_cmp(left, right) >= 0 ? &sv_yes : &sv_no );
836 dSP; tryAMAGICbinSET(sne,0);
839 SETs( !sv_eq(left, right) ? &sv_yes : &sv_no );
846 dSP; dTARGET; tryAMAGICbin(scmp,0);
849 SETi( sv_cmp(left, right) );
855 dSP; dATARGET; tryAMAGICbin(band,opASSIGN);
858 if (SvNIOK(left) || SvNIOK(right)) {
859 unsigned long value = U_L(SvNV(left));
860 value = value & U_L(SvNV(right));
864 do_vop(op->op_type, TARG, left, right);
873 dSP; dATARGET; tryAMAGICbin(bxor,opASSIGN);
876 if (SvNIOK(left) || SvNIOK(right)) {
877 unsigned long value = U_L(SvNV(left));
878 value = value ^ U_L(SvNV(right));
882 do_vop(op->op_type, TARG, left, right);
891 dSP; dATARGET; tryAMAGICbin(bor,opASSIGN);
894 if (SvNIOK(left) || SvNIOK(right)) {
895 unsigned long value = U_L(SvNV(left));
896 value = value | U_L(SvNV(right));
900 do_vop(op->op_type, TARG, left, right);
909 dSP; dTARGET; tryAMAGICun(neg);
914 else if (SvPOK(sv)) {
916 char *s = SvPV(sv, len);
917 if (isALPHA(*s) || *s == '_') {
918 sv_setpvn(TARG, "-", 1);
921 else if (*s == '+' || *s == '-') {
923 *SvPV_force(TARG, len) = *s == '-' ? '+' : '-';
926 sv_setnv(TARG, -SvNV(sv));
936 dSP; tryAMAGICunSET(not);
937 #endif /* OVERLOAD */
938 *stack_sp = SvTRUE(*stack_sp) ? &sv_no : &sv_yes;
944 dSP; dTARGET; tryAMAGICun(compl);
958 tmps = SvPV_force(TARG, len);
961 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
964 for ( ; anum >= sizeof(long); anum -= sizeof(long), tmpl++)
968 for ( ; anum > 0; anum--, tmps++)
977 /* integer versions of some of the above */
983 sv_setiv(TOPs, value + 1);
987 if (SvAMAGIC(TOPs) ) {
991 sv_setiv(TOPs, value + 1);
994 #endif /* OVERLOAD */
1002 sv_setiv(TOPs, value - 1);
1006 if (SvAMAGIC(TOPs)) {
1010 sv_setiv(TOPs, value - 1);
1013 #endif /* OVERLOAD */
1020 sv_setsv(TARG, TOPs);
1022 sv_setiv(TOPs, SvIV(TOPs) + 1);
1025 if (SvAMAGIC(TOPs) ) {
1028 sv_setiv(TOPs, SvIV(TOPs) + 1);
1031 #endif /* OVERLOAD */
1041 sv_setsv(TARG, TOPs);
1043 sv_setiv(TOPs, SvIV(TOPs) - 1);
1046 if (SvAMAGIC(TOPs) ) {
1049 sv_setiv(TOPs, SvIV(TOPs) - 1);
1052 #endif /* OVERLOAD */
1059 dSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
1062 SETi( left * right );
1069 dSP; dATARGET; tryAMAGICbin(div,opASSIGN);
1073 DIE("Illegal division by zero");
1074 value = POPi / value;
1082 dSP; dATARGET; tryAMAGICbin(mod,opASSIGN);
1085 SETi( left % right );
1092 dSP; dATARGET; tryAMAGICbin(add,opASSIGN);
1095 SETi( left + right );
1102 dSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
1105 SETi( left - right );
1112 dSP; tryAMAGICbinSET(lt,0);
1115 SETs((left < right) ? &sv_yes : &sv_no);
1122 dSP; tryAMAGICbinSET(gt,0);
1125 SETs((left > right) ? &sv_yes : &sv_no);
1132 dSP; tryAMAGICbinSET(le,0);
1135 SETs((left <= right) ? &sv_yes : &sv_no);
1142 dSP; tryAMAGICbinSET(ge,0);
1145 SETs((left >= right) ? &sv_yes : &sv_no);
1152 dSP; tryAMAGICbinSET(eq,0);
1155 SETs((left == right) ? &sv_yes : &sv_no);
1162 dSP; tryAMAGICbinSET(ne,0);
1165 SETs((left != right) ? &sv_yes : &sv_no);
1172 dSP; dTARGET; tryAMAGICbin(ncmp,0);
1179 else if (left < right)
1190 dSP; dTARGET; tryAMAGICun(neg);
1195 /* High falutin' math. */
1199 dSP; dTARGET; tryAMAGICbin(atan2,0);
1202 SETn(atan2(left, right));
1209 dSP; dTARGET; tryAMAGICun(sin);
1221 dSP; dTARGET; tryAMAGICun(cos);
1242 value = rand() * value / 2147483648.0;
1245 value = rand() * value / 65536.0;
1248 value = rand() * value / 32768.0;
1250 value = rand() * value / (double)(((unsigned long)1) << RANDBITS);
1277 dSP; dTARGET; tryAMAGICun(exp);
1289 dSP; dTARGET; tryAMAGICun(log);
1294 DIE("Can't take log of %g", value);
1303 dSP; dTARGET; tryAMAGICun(sqrt);
1308 DIE("Can't take sqrt of %g", value);
1309 value = sqrt(value);
1321 (void)modf(value, &value);
1323 (void)modf(-value, &value);
1332 dSP; dTARGET; tryAMAGICun(abs);
1352 XPUSHi( scan_hex(tmps, 99, &argtype) );
1364 while (*tmps && (isSPACE(*tmps) || *tmps == '0'))
1367 value = (I32)scan_hex(++tmps, 99, &argtype);
1369 value = (I32)scan_oct(tmps, 99, &argtype);
1379 SETi( sv_len(TOPs) );
1391 I32 lvalue = op->op_flags & OPf_MOD;
1393 I32 arybase = curcop->cop_arybase;
1397 pos = POPi - arybase;
1399 tmps = SvPV(sv, curlen);
1401 pos += curlen + arybase;
1402 if (pos < 0 || pos > curlen) {
1403 if (dowarn || lvalue)
1404 warn("substr outside of string");
1416 rem = curlen - pos; /* rem=how many bytes left*/
1419 sv_setpvn(TARG, tmps, rem);
1420 if (lvalue) { /* it's an lvalue! */
1421 if (SvTYPE(TARG) < SVt_PVLV) {
1422 sv_upgrade(TARG, SVt_PVLV);
1423 sv_magic(TARG, Nullsv, 'x', Nullch, 0);
1428 LvTARGOFF(TARG) = pos;
1429 LvTARGLEN(TARG) = rem;
1432 PUSHs(TARG); /* avoid SvSETMAGIC here */
1439 register I32 size = POPi;
1440 register I32 offset = POPi;
1441 register SV *src = POPs;
1442 I32 lvalue = op->op_flags & OPf_MOD;
1444 unsigned char *s = (unsigned char*)SvPV(src, srclen);
1445 unsigned long retnum;
1448 offset *= size; /* turn into bit offset */
1449 len = (offset + size + 7) / 8;
1450 if (offset < 0 || size < 1)
1453 if (lvalue) { /* it's an lvalue! */
1454 if (SvTYPE(TARG) < SVt_PVLV) {
1455 sv_upgrade(TARG, SVt_PVLV);
1456 sv_magic(TARG, Nullsv, 'v', Nullch, 0);
1461 LvTARGOFF(TARG) = offset;
1462 LvTARGLEN(TARG) = size;
1470 retnum = (unsigned long) s[offset] << 8;
1471 else if (size == 32) {
1473 if (offset + 1 < len)
1474 retnum = ((unsigned long) s[offset] << 24) +
1475 ((unsigned long) s[offset + 1] << 16) +
1476 (s[offset + 2] << 8);
1478 retnum = ((unsigned long) s[offset] << 24) +
1479 ((unsigned long) s[offset + 1] << 16);
1482 retnum = (unsigned long) s[offset] << 24;
1487 retnum = (s[offset >> 3] >> (offset & 7)) & ((1 << size) - 1);
1492 else if (size == 16)
1493 retnum = ((unsigned long) s[offset] << 8) + s[offset+1];
1494 else if (size == 32)
1495 retnum = ((unsigned long) s[offset] << 24) +
1496 ((unsigned long) s[offset + 1] << 16) +
1497 (s[offset + 2] << 8) + s[offset+3];
1501 sv_setiv(TARG, (I32)retnum);
1516 I32 arybase = curcop->cop_arybase;
1521 offset = POPi - arybase;
1524 tmps = SvPV(big, biglen);
1527 else if (offset > biglen)
1529 if (!(tmps2 = fbm_instr((unsigned char*)tmps + offset,
1530 (unsigned char*)tmps + biglen, little)))
1531 retval = -1 + arybase;
1533 retval = tmps2 - tmps + arybase;
1550 I32 arybase = curcop->cop_arybase;
1556 tmps2 = SvPV(little, llen);
1557 tmps = SvPV(big, blen);
1561 offset = SvIV(offstr) - arybase + llen;
1564 else if (offset > blen)
1566 if (!(tmps2 = rninstr(tmps, tmps + offset,
1567 tmps2, tmps2 + llen)))
1568 retval = -1 + arybase;
1570 retval = tmps2 - tmps + arybase;
1577 dSP; dMARK; dORIGMARK; dTARGET;
1578 do_sprintf(TARG, SP-MARK, MARK+1);
1592 value = (I32) (*tmps & 255);
1597 value = (I32) (anum & 255);
1609 (void)SvUPGRADE(TARG,SVt_PV);
1615 (void)SvPOK_only(TARG);
1622 dSP; dTARGET; dPOPTOPssrl;
1624 char *tmps = SvPV(left, na);
1626 sv_setpv(TARG, fcrypt(tmps, SvPV(right, na)));
1628 sv_setpv(TARG, crypt(tmps, SvPV(right, na)));
1632 "The crypt() function is unimplemented due to excessive paranoia.");
1644 if (!SvPADTMP(sv)) {
1650 s = SvPV_force(sv, na);
1663 if (!SvPADTMP(sv)) {
1669 s = SvPV_force(sv, na);
1682 register char *send;
1685 if (!SvPADTMP(sv)) {
1691 s = SvPV_force(sv, len);
1706 register char *send;
1709 if (!SvPADTMP(sv)) {
1715 s = SvPV_force(sv, len);
1730 register char *s = SvPV(sv,len);
1734 (void)SvUPGRADE(TARG, SVt_PV);
1735 SvGROW(TARG, len * 2);
1743 SvCUR_set(TARG, d - SvPVX(TARG));
1744 (void)SvPOK_only(TARG);
1747 sv_setpvn(TARG, s, len);
1756 dSP; dMARK; dORIGMARK;
1758 register AV* av = (AV*)POPs;
1759 register I32 lval = op->op_flags & OPf_MOD;
1761 if (SvTYPE(av) == SVt_PVAV) {
1762 while (++MARK <= SP) {
1763 I32 elem = SvIVx(*MARK);
1765 svp = av_fetch(av, elem, lval);
1767 if (!svp || *svp == &sv_undef)
1768 DIE(no_aelem, elem);
1769 if (op->op_private & OPpLVAL_INTRO)
1772 *MARK = svp ? *svp : &sv_undef;
1775 else if (GIMME != G_ARRAY) {
1783 /* Associative arrays. */
1788 HV *hash = (HV*)POPs;
1789 HE *entry = hv_iternext(hash);
1795 tmps = hv_iterkey(entry, &i);
1798 PUSHs(sv_2mortal(newSVpv(tmps, i)));
1799 if (GIMME == G_ARRAY) {
1800 sv_setsv(TARG, hv_iterval(hash, entry));
1804 else if (GIMME == G_SCALAR)
1828 if (SvTYPE(hv) != SVt_PVHV) {
1829 DIE("Not a HASH reference");
1831 tmps = SvPV(tmpsv, len);
1832 sv = hv_delete(hv, tmps, len);
1846 if (SvTYPE(hv) != SVt_PVHV) {
1847 DIE("Not a HASH reference");
1849 tmps = SvPV(tmpsv, len);
1850 if (hv_exists(hv, tmps, len))
1857 dSP; dMARK; dORIGMARK;
1859 register HV *hv = (HV*)POPs;
1860 register I32 lval = op->op_flags & OPf_MOD;
1862 if (SvTYPE(hv) == SVt_PVHV) {
1863 while (++MARK <= SP) {
1865 char *key = SvPV(*MARK, keylen);
1867 svp = hv_fetch(hv, key, keylen, lval);
1869 if (!svp || *svp == &sv_undef)
1871 if (op->op_private & OPpLVAL_INTRO)
1874 *MARK = svp ? *svp : &sv_undef;
1877 if (GIMME != G_ARRAY) {
1885 /* List operators. */
1890 if (GIMME != G_ARRAY) {
1892 *MARK = *SP; /* unwanted list, return last item */
1903 SV **lastrelem = stack_sp;
1904 SV **lastlelem = stack_base + POPMARK;
1905 SV **firstlelem = stack_base + POPMARK + 1;
1906 register SV **firstrelem = lastlelem + 1;
1907 I32 arybase = curcop->cop_arybase;
1909 register I32 max = lastrelem - lastlelem;
1910 register SV **lelem;
1913 if (GIMME != G_ARRAY) {
1914 ix = SvIVx(*lastlelem) - arybase;
1915 if (ix < 0 || ix >= max)
1916 *firstlelem = &sv_undef;
1918 *firstlelem = firstrelem[ix];
1924 SP = firstlelem - 1;
1928 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
1929 ix = SvIVx(*lelem) - arybase;
1934 else if (!(*lelem = firstrelem[ix]))
1937 else if (ix >= max || !(*lelem = firstrelem[ix]))
1947 I32 items = SP - MARK;
1949 XPUSHs((SV*)sv_2mortal((SV*)av_make(items, MARK+1)));
1955 dSP; dMARK; dORIGMARK;
1957 HV* hv = (HV*)sv_2mortal((SV*)newHV());
1962 SV *val = NEWSV(46, 0);
1964 sv_setsv(val, *++MARK);
1966 warn("Odd number of elements in hash list");
1967 tmps = SvPV(key,len);
1968 (void)hv_store(hv,tmps,len,val,0);
1977 dSP; dMARK; dORIGMARK;
1978 register AV *ary = (AV*)*++MARK;
1982 register I32 offset;
1983 register I32 length;
1992 offset = SvIVx(*MARK);
1994 offset += AvFILL(ary) + 1;
1996 offset -= curcop->cop_arybase;
1998 length = SvIVx(*MARK++);
2003 length = AvMAX(ary) + 1; /* close enough to infinity */
2007 length = AvMAX(ary) + 1;
2015 if (offset > AvFILL(ary) + 1)
2016 offset = AvFILL(ary) + 1;
2017 after = AvFILL(ary) + 1 - (offset + length);
2018 if (after < 0) { /* not that much array */
2019 length += after; /* offset+length now in array */
2025 /* At this point, MARK .. SP-1 is our new LIST */
2028 diff = newlen - length;
2030 if (diff < 0) { /* shrinking the area */
2032 New(451, tmparyval, newlen, SV*); /* so remember insertion */
2033 Copy(MARK, tmparyval, newlen, SV*);
2036 MARK = ORIGMARK + 1;
2037 if (GIMME == G_ARRAY) { /* copy return vals to stack */
2038 MEXTEND(MARK, length);
2039 Copy(AvARRAY(ary)+offset, MARK, length, SV*);
2041 for (i = length, dst = MARK; i; i--)
2042 sv_2mortal(*dst++); /* free them eventualy */
2047 *MARK = AvARRAY(ary)[offset+length-1];
2050 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
2051 SvREFCNT_dec(*dst++); /* free them now */
2054 AvFILL(ary) += diff;
2056 /* pull up or down? */
2058 if (offset < after) { /* easier to pull up */
2059 if (offset) { /* esp. if nothing to pull */
2060 src = &AvARRAY(ary)[offset-1];
2061 dst = src - diff; /* diff is negative */
2062 for (i = offset; i > 0; i--) /* can't trust Copy */
2066 SvPVX(ary) = (char*)(AvARRAY(ary) - diff); /* diff is negative */
2070 if (after) { /* anything to pull down? */
2071 src = AvARRAY(ary) + offset + length;
2072 dst = src + diff; /* diff is negative */
2073 Move(src, dst, after, SV*);
2075 dst = &AvARRAY(ary)[AvFILL(ary)+1];
2076 /* avoid later double free */
2080 dst[--i] = &sv_undef;
2083 for (src = tmparyval, dst = AvARRAY(ary) + offset;
2085 *dst = NEWSV(46, 0);
2086 sv_setsv(*dst++, *src++);
2088 Safefree(tmparyval);
2091 else { /* no, expanding (or same) */
2093 New(452, tmparyval, length, SV*); /* so remember deletion */
2094 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
2097 if (diff > 0) { /* expanding */
2099 /* push up or down? */
2101 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
2105 Move(src, dst, offset, SV*);
2107 SvPVX(ary) = (char*)(AvARRAY(ary) - diff);/* diff is positive */
2109 AvFILL(ary) += diff;
2112 if (AvFILL(ary) + diff >= AvMAX(ary)) /* oh, well */
2113 av_extend(ary, AvFILL(ary) + diff);
2114 AvFILL(ary) += diff;
2117 dst = AvARRAY(ary) + AvFILL(ary);
2119 for (i = after; i; i--) {
2126 for (src = MARK, dst = AvARRAY(ary) + offset; newlen; newlen--) {
2127 *dst = NEWSV(46, 0);
2128 sv_setsv(*dst++, *src++);
2130 MARK = ORIGMARK + 1;
2131 if (GIMME == G_ARRAY) { /* copy return vals to stack */
2133 Copy(tmparyval, MARK, length, SV*);
2135 for (i = length, dst = MARK; i; i--)
2136 sv_2mortal(*dst++); /* free them eventualy */
2138 Safefree(tmparyval);
2142 else if (length--) {
2143 *MARK = tmparyval[length];
2146 while (length-- > 0)
2147 SvREFCNT_dec(tmparyval[length]);
2149 Safefree(tmparyval);
2160 dSP; dMARK; dORIGMARK; dTARGET;
2161 register AV *ary = (AV*)*++MARK;
2162 register SV *sv = &sv_undef;
2164 for (++MARK; MARK <= SP; MARK++) {
2167 sv_setsv(sv, *MARK);
2171 PUSHi( AvFILL(ary) + 1 );
2179 SV *sv = av_pop(av);
2180 if (sv != &sv_undef && AvREAL(av))
2181 (void)sv_2mortal(sv);
2190 SV *sv = av_shift(av);
2194 if (sv != &sv_undef && AvREAL(av))
2195 (void)sv_2mortal(sv);
2202 dSP; dMARK; dORIGMARK; dTARGET;
2203 register AV *ary = (AV*)*++MARK;
2207 av_unshift(ary, SP - MARK);
2210 sv_setsv(sv, *++MARK);
2211 (void)av_store(ary, i++, sv);
2215 PUSHi( AvFILL(ary) + 1 );
2225 if (GIMME == G_ARRAY) {
2236 register char *down;
2242 do_join(TARG, &sv_no, MARK, SP);
2244 sv_setsv(TARG, *SP);
2245 up = SvPV_force(TARG, len);
2247 down = SvPVX(TARG) + len - 1;
2253 (void)SvPOK_only(TARG);
2261 /* Explosives and implosives. */
2270 register char *pat = SvPV(left, llen);
2271 register char *s = SvPV(right, rlen);
2272 char *strend = s + rlen;
2274 register char *patend = pat + llen;
2279 /* These must not be in registers: */
2290 unsigned quad auquad;
2296 register U32 culong;
2298 static char* bitcount = 0;
2300 if (GIMME != G_ARRAY) { /* arrange to do first one only */
2302 for (patend = pat; !isALPHA(*patend) || *patend == 'x'; patend++) ;
2303 if (strchr("aAbBhH", *patend) || *pat == '%') {
2305 while (isDIGIT(*patend) || *patend == '*')
2311 while (pat < patend) {
2316 else if (*pat == '*') {
2317 len = strend - strbeg; /* long enough */
2320 else if (isDIGIT(*pat)) {
2322 while (isDIGIT(*pat))
2323 len = (len * 10) + (*pat++ - '0');
2326 len = (datumtype != '@');
2331 if (len == 1 && pat[-1] != '1')
2340 if (len > strend - strbeg)
2341 DIE("@ outside of string");
2345 if (len > s - strbeg)
2346 DIE("X outside of string");
2350 if (len > strend - s)
2351 DIE("x outside of string");
2356 if (len > strend - s)
2359 goto uchar_checksum;
2360 sv = NEWSV(35, len);
2361 sv_setpvn(sv, s, len);
2363 if (datumtype == 'A') {
2364 aptr = s; /* borrow register */
2365 s = SvPVX(sv) + len - 1;
2366 while (s >= SvPVX(sv) && (!*s || isSPACE(*s)))
2369 SvCUR_set(sv, s - SvPVX(sv));
2370 s = aptr; /* unborrow register */
2372 XPUSHs(sv_2mortal(sv));
2376 if (pat[-1] == '*' || len > (strend - s) * 8)
2377 len = (strend - s) * 8;
2380 Newz(601, bitcount, 256, char);
2381 for (bits = 1; bits < 256; bits++) {
2382 if (bits & 1) bitcount[bits]++;
2383 if (bits & 2) bitcount[bits]++;
2384 if (bits & 4) bitcount[bits]++;
2385 if (bits & 8) bitcount[bits]++;
2386 if (bits & 16) bitcount[bits]++;
2387 if (bits & 32) bitcount[bits]++;
2388 if (bits & 64) bitcount[bits]++;
2389 if (bits & 128) bitcount[bits]++;
2393 culong += bitcount[*(unsigned char*)s++];
2398 if (datumtype == 'b') {
2400 if (bits & 1) culong++;
2406 if (bits & 128) culong++;
2413 sv = NEWSV(35, len + 1);
2416 aptr = pat; /* borrow register */
2418 if (datumtype == 'b') {
2420 for (len = 0; len < aint; len++) {
2421 if (len & 7) /*SUPPRESS 595*/
2425 *pat++ = '0' + (bits & 1);
2430 for (len = 0; len < aint; len++) {
2435 *pat++ = '0' + ((bits & 128) != 0);
2439 pat = aptr; /* unborrow register */
2440 XPUSHs(sv_2mortal(sv));
2444 if (pat[-1] == '*' || len > (strend - s) * 2)
2445 len = (strend - s) * 2;
2446 sv = NEWSV(35, len + 1);
2449 aptr = pat; /* borrow register */
2451 if (datumtype == 'h') {
2453 for (len = 0; len < aint; len++) {
2458 *pat++ = hexdigit[bits & 15];
2463 for (len = 0; len < aint; len++) {
2468 *pat++ = hexdigit[(bits >> 4) & 15];
2472 pat = aptr; /* unborrow register */
2473 XPUSHs(sv_2mortal(sv));
2476 if (len > strend - s)
2481 if (aint >= 128) /* fake up signed chars */
2490 if (aint >= 128) /* fake up signed chars */
2493 sv_setiv(sv, (I32)aint);
2494 PUSHs(sv_2mortal(sv));
2499 if (len > strend - s)
2513 sv_setiv(sv, (I32)auint);
2514 PUSHs(sv_2mortal(sv));
2519 along = (strend - s) / sizeof(I16);
2524 Copy(s, &ashort, 1, I16);
2532 Copy(s, &ashort, 1, I16);
2535 sv_setiv(sv, (I32)ashort);
2536 PUSHs(sv_2mortal(sv));
2543 along = (strend - s) / sizeof(U16);
2548 Copy(s, &aushort, 1, U16);
2551 if (datumtype == 'n')
2552 aushort = ntohs(aushort);
2555 if (datumtype == 'v')
2556 aushort = vtohs(aushort);
2564 Copy(s, &aushort, 1, U16);
2568 if (datumtype == 'n')
2569 aushort = ntohs(aushort);
2572 if (datumtype == 'v')
2573 aushort = vtohs(aushort);
2575 sv_setiv(sv, (I32)aushort);
2576 PUSHs(sv_2mortal(sv));
2581 along = (strend - s) / sizeof(int);
2586 Copy(s, &aint, 1, int);
2589 cdouble += (double)aint;
2597 Copy(s, &aint, 1, int);
2600 sv_setiv(sv, (I32)aint);
2601 PUSHs(sv_2mortal(sv));
2606 along = (strend - s) / sizeof(unsigned int);
2611 Copy(s, &auint, 1, unsigned int);
2612 s += sizeof(unsigned int);
2614 cdouble += (double)auint;
2622 Copy(s, &auint, 1, unsigned int);
2623 s += sizeof(unsigned int);
2625 sv_setiv(sv, (I32)auint);
2626 PUSHs(sv_2mortal(sv));
2631 along = (strend - s) / sizeof(I32);
2636 Copy(s, &along, 1, I32);
2639 cdouble += (double)along;
2647 Copy(s, &along, 1, I32);
2650 sv_setiv(sv, (I32)along);
2651 PUSHs(sv_2mortal(sv));
2658 along = (strend - s) / sizeof(U32);
2663 Copy(s, &aulong, 1, U32);
2666 if (datumtype == 'N')
2667 aulong = ntohl(aulong);
2670 if (datumtype == 'V')
2671 aulong = vtohl(aulong);
2674 cdouble += (double)aulong;
2682 Copy(s, &aulong, 1, U32);
2686 if (datumtype == 'N')
2687 aulong = ntohl(aulong);
2690 if (datumtype == 'V')
2691 aulong = vtohl(aulong);
2693 sv_setnv(sv, (double)aulong);
2694 PUSHs(sv_2mortal(sv));
2699 along = (strend - s) / sizeof(char*);
2704 if (sizeof(char*) > strend - s)
2707 Copy(s, &aptr, 1, char*);
2713 PUSHs(sv_2mortal(sv));
2718 if (sizeof(char*) > strend - s)
2721 Copy(s, &aptr, 1, char*);
2726 sv_setpvn(sv, aptr, len);
2727 PUSHs(sv_2mortal(sv));
2733 if (s + sizeof(quad) > strend)
2736 Copy(s, &aquad, 1, quad);
2740 sv_setiv(sv, (IV)aquad);
2741 PUSHs(sv_2mortal(sv));
2747 if (s + sizeof(unsigned quad) > strend)
2750 Copy(s, &auquad, 1, unsigned quad);
2751 s += sizeof(unsigned quad);
2754 sv_setiv(sv, (IV)auquad);
2755 PUSHs(sv_2mortal(sv));
2759 /* float and double added gnb@melba.bby.oz.au 22/11/89 */
2762 along = (strend - s) / sizeof(float);
2767 Copy(s, &afloat, 1, float);
2775 Copy(s, &afloat, 1, float);
2778 sv_setnv(sv, (double)afloat);
2779 PUSHs(sv_2mortal(sv));
2785 along = (strend - s) / sizeof(double);
2790 Copy(s, &adouble, 1, double);
2791 s += sizeof(double);
2798 Copy(s, &adouble, 1, double);
2799 s += sizeof(double);
2801 sv_setnv(sv, (double)adouble);
2802 PUSHs(sv_2mortal(sv));
2807 along = (strend - s) * 3 / 4;
2808 sv = NEWSV(42, along);
2809 while (s < strend && *s > ' ' && *s < 'a') {
2814 len = (*s++ - ' ') & 077;
2816 if (s < strend && *s >= ' ')
2817 a = (*s++ - ' ') & 077;
2820 if (s < strend && *s >= ' ')
2821 b = (*s++ - ' ') & 077;
2824 if (s < strend && *s >= ' ')
2825 c = (*s++ - ' ') & 077;
2828 if (s < strend && *s >= ' ')
2829 d = (*s++ - ' ') & 077;
2832 hunk[0] = a << 2 | b >> 4;
2833 hunk[1] = b << 4 | c >> 2;
2834 hunk[2] = c << 6 | d;
2835 sv_catpvn(sv, hunk, len > 3 ? 3 : len);
2840 else if (s[1] == '\n') /* possible checksum byte */
2843 XPUSHs(sv_2mortal(sv));
2848 if (strchr("fFdD", datumtype) ||
2849 (checksum > 32 && strchr("iIlLN", datumtype)) ) {
2853 while (checksum >= 16) {
2857 while (checksum >= 4) {
2863 along = (1 << checksum) - 1;
2864 while (cdouble < 0.0)
2866 cdouble = modf(cdouble / adouble, &trouble) * adouble;
2867 sv_setnv(sv, cdouble);
2870 if (checksum < 32) {
2871 along = (1 << checksum) - 1;
2872 culong &= (U32)along;
2874 sv_setnv(sv, (double)culong);
2876 XPUSHs(sv_2mortal(sv));
2884 doencodes(sv, s, len)
2892 sv_catpvn(sv, hunk, 1);
2895 hunk[0] = ' ' + (077 & (*s >> 2));
2896 hunk[1] = ' ' + (077 & ((*s << 4) & 060 | (s[1] >> 4) & 017));
2897 hunk[2] = ' ' + (077 & ((s[1] << 2) & 074 | (s[2] >> 6) & 03));
2898 hunk[3] = ' ' + (077 & (s[2] & 077));
2899 sv_catpvn(sv, hunk, 4);
2903 for (s = SvPVX(sv); *s; s++) {
2907 sv_catpvn(sv, "\n", 1);
2912 dSP; dMARK; dORIGMARK; dTARGET;
2913 register SV *cat = TARG;
2916 register char *pat = SvPVx(*++MARK, fromlen);
2917 register char *patend = pat + fromlen;
2922 static char null10[] = {0,0,0,0,0,0,0,0,0,0};
2923 static char *space10 = " ";
2925 /* These must not be in registers: */
2934 unsigned quad auquad;
2942 sv_setpvn(cat, "", 0);
2943 while (pat < patend) {
2944 #define NEXTFROM (items-- > 0 ? *MARK++ : &sv_no)
2947 len = strchr("@Xxu", datumtype) ? 0 : items;
2950 else if (isDIGIT(*pat)) {
2952 while (isDIGIT(*pat))
2953 len = (len * 10) + (*pat++ - '0');
2961 DIE("%% may only be used in unpack");
2972 if (SvCUR(cat) < len)
2973 DIE("X outside of string");
2980 sv_catpvn(cat, null10, 10);
2983 sv_catpvn(cat, null10, len);
2988 aptr = SvPV(fromstr, fromlen);
2992 sv_catpvn(cat, aptr, len);
2994 sv_catpvn(cat, aptr, fromlen);
2996 if (datumtype == 'A') {
2998 sv_catpvn(cat, space10, 10);
3001 sv_catpvn(cat, space10, len);
3005 sv_catpvn(cat, null10, 10);
3008 sv_catpvn(cat, null10, len);
3015 char *savepat = pat;
3020 aptr = SvPV(fromstr, fromlen);
3025 SvCUR(cat) += (len+7)/8;
3026 SvGROW(cat, SvCUR(cat) + 1);
3027 aptr = SvPVX(cat) + aint;
3032 if (datumtype == 'B') {
3033 for (len = 0; len++ < aint;) {
3034 items |= *pat++ & 1;
3038 *aptr++ = items & 0xff;
3044 for (len = 0; len++ < aint;) {
3050 *aptr++ = items & 0xff;
3056 if (datumtype == 'B')
3057 items <<= 7 - (aint & 7);
3059 items >>= 7 - (aint & 7);
3060 *aptr++ = items & 0xff;
3062 pat = SvPVX(cat) + SvCUR(cat);
3073 char *savepat = pat;
3078 aptr = SvPV(fromstr, fromlen);
3083 SvCUR(cat) += (len+1)/2;
3084 SvGROW(cat, SvCUR(cat) + 1);
3085 aptr = SvPVX(cat) + aint;
3090 if (datumtype == 'H') {
3091 for (len = 0; len++ < aint;) {
3093 items |= ((*pat++ & 15) + 9) & 15;
3095 items |= *pat++ & 15;
3099 *aptr++ = items & 0xff;
3105 for (len = 0; len++ < aint;) {
3107 items |= (((*pat++ & 15) + 9) & 15) << 4;
3109 items |= (*pat++ & 15) << 4;
3113 *aptr++ = items & 0xff;
3119 *aptr++ = items & 0xff;
3120 pat = SvPVX(cat) + SvCUR(cat);
3132 aint = SvIV(fromstr);
3134 sv_catpvn(cat, &achar, sizeof(char));
3137 /* Float and double added by gnb@melba.bby.oz.au 22/11/89 */
3142 afloat = (float)SvNV(fromstr);
3143 sv_catpvn(cat, (char *)&afloat, sizeof (float));
3150 adouble = (double)SvNV(fromstr);
3151 sv_catpvn(cat, (char *)&adouble, sizeof (double));
3157 ashort = (I16)SvIV(fromstr);
3159 ashort = htons(ashort);
3161 sv_catpvn(cat, (char*)&ashort, sizeof(I16));
3167 ashort = (I16)SvIV(fromstr);
3169 ashort = htovs(ashort);
3171 sv_catpvn(cat, (char*)&ashort, sizeof(I16));
3178 ashort = (I16)SvIV(fromstr);
3179 sv_catpvn(cat, (char*)&ashort, sizeof(I16));
3185 auint = U_I(SvNV(fromstr));
3186 sv_catpvn(cat, (char*)&auint, sizeof(unsigned int));
3192 aint = SvIV(fromstr);
3193 sv_catpvn(cat, (char*)&aint, sizeof(int));
3199 aulong = U_L(SvNV(fromstr));
3201 aulong = htonl(aulong);
3203 sv_catpvn(cat, (char*)&aulong, sizeof(U32));
3209 aulong = U_L(SvNV(fromstr));
3211 aulong = htovl(aulong);
3213 sv_catpvn(cat, (char*)&aulong, sizeof(U32));
3219 aulong = U_L(SvNV(fromstr));
3220 sv_catpvn(cat, (char*)&aulong, sizeof(U32));
3226 along = SvIV(fromstr);
3227 sv_catpvn(cat, (char*)&along, sizeof(I32));
3234 auquad = (unsigned quad)SvIV(fromstr);
3235 sv_catpvn(cat, (char*)&auquad, sizeof(unsigned quad));
3241 aquad = (quad)SvIV(fromstr);
3242 sv_catpvn(cat, (char*)&aquad, sizeof(quad));
3247 len = 1; /* assume SV is correct length */
3252 aptr = SvPV_force(fromstr, na); /* XXX Error if TEMP? */
3253 sv_catpvn(cat, (char*)&aptr, sizeof(char*));
3258 aptr = SvPV(fromstr, fromlen);
3259 SvGROW(cat, fromlen * 4 / 3);
3264 while (fromlen > 0) {
3271 doencodes(cat, aptr, todo);
3289 register I32 limit = POPi; /* note, negative is forever */
3292 register char *s = SvPV(sv, len);
3293 char *strend = s + len;
3294 register PMOP *pm = (PMOP*)POPs;
3298 I32 maxiters = (strend - s) + 10;
3301 I32 origlimit = limit;
3304 AV *oldstack = stack;
3305 register REGEXP *rx = pm->op_pmregexp;
3309 DIE("panic: do_split");
3310 if (pm->op_pmreplroot)
3311 ary = GvAVn((GV*)pm->op_pmreplroot);
3312 else if (gimme != G_ARRAY)
3316 if (ary && (gimme != G_ARRAY || (pm->op_pmflags & PMf_ONCE))) {
3320 for (i = AvFILL(ary); i >= 0; i--)
3321 AvARRAY(ary)[i] = &sv_undef; /* don't free mere refs */
3325 /* temporarily switch stacks */
3326 SWITCHSTACK(stack, ary);
3328 base = SP - stack_base;
3330 if (pm->op_pmflags & PMf_SKIPWHITE) {
3335 limit = maxiters + 2;
3336 if (pm->op_pmflags & PMf_WHITE) {
3339 for (m = s; m < strend && !isSPACE(*m); m++) ;
3342 dstr = NEWSV(30, m-s);
3343 sv_setpvn(dstr, s, m-s);
3348 for (s = m + 1; s < strend && isSPACE(*s); s++) ;
3351 else if (strEQ("^", rx->precomp)) {
3354 for (m = s; m < strend && *m != '\n'; m++) ;
3358 dstr = NEWSV(30, m-s);
3359 sv_setpvn(dstr, s, m-s);
3366 else if (pm->op_pmshort) {
3367 i = SvCUR(pm->op_pmshort);
3369 I32 fold = (pm->op_pmflags & PMf_FOLD);
3370 i = *SvPVX(pm->op_pmshort);
3371 if (fold && isUPPER(i))
3376 m < strend && *m != i &&
3377 (!isUPPER(*m) || toLOWER(*m) != i);
3378 m++) /*SUPPRESS 530*/
3381 else /*SUPPRESS 530*/
3382 for (m = s; m < strend && *m != i; m++) ;
3385 dstr = NEWSV(30, m-s);
3386 sv_setpvn(dstr, s, m-s);
3395 while (s < strend && --limit &&
3396 (m=fbm_instr((unsigned char*)s, (unsigned char*)strend,
3400 dstr = NEWSV(31, m-s);
3401 sv_setpvn(dstr, s, m-s);
3410 maxiters += (strend - s) * rx->nparens;
3411 while (s < strend && --limit &&
3412 regexec(rx, s, strend, orig, 1, Nullsv, TRUE) ) {
3414 && rx->subbase != orig) {
3419 strend = s + (strend - m);
3422 dstr = NEWSV(32, m-s);
3423 sv_setpvn(dstr, s, m-s);
3428 for (i = 1; i <= rx->nparens; i++) {
3431 dstr = NEWSV(33, m-s);
3432 sv_setpvn(dstr, s, m-s);
3441 iters = (SP - stack_base) - base;
3442 if (iters > maxiters)
3445 /* keep field after final delim? */
3446 if (s < strend || (iters && origlimit)) {
3447 dstr = NEWSV(34, strend-s);
3448 sv_setpvn(dstr, s, strend-s);
3454 else if (!origlimit) {
3455 while (iters > 0 && SvCUR(TOPs) == 0)
3459 SWITCHSTACK(ary, oldstack);
3460 if (gimme == G_ARRAY) {
3462 Copy(AvARRAY(ary), SP + 1, iters, SV*);
3468 if (gimme == G_ARRAY)
3471 if (iters || !pm->op_pmreplroot) {