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 * "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
19 * Types used in bitwise operations.
21 * Normally we'd just use IV and UV. However, some hardware and
22 * software combinations (e.g. Alpha and current OSF/1) don't have a
23 * floating-point type to use for NV that has adequate bits to fully
24 * hold an IV/UV. (In other words, sizeof(long) == sizeof(double).)
26 * It just so happens that "int" is the right size almost everywhere.
32 * Mask used after bitwise operations.
34 * There is at least one realm (Cray word machines) that doesn't
35 * have an integral type (except char) small enough to be represented
36 * in a double without loss; that is, it has no 32-bit type.
38 #if BYTEORDER > 0xFFFF && defined(_CRAY) && !defined(_CRAYMPP)
40 # define BW_MASK ((1 << BW_BITS) - 1)
41 # define BW_SIGN (1 << (BW_BITS - 1))
42 # define BWi(i) (((i) & BW_SIGN) ? ((i) | ~BW_MASK) : ((i) & BW_MASK))
43 # define BWu(u) ((u) & BW_MASK)
50 * Offset for integer pack/unpack.
52 * On architectures where I16 and I32 aren't really 16 and 32 bits,
53 * which for now are all Crays, pack and unpack have to play games.
57 * These values are required for portability of pack() output.
58 * If they're not right on your machine, then pack() and unpack()
59 * wouldn't work right anyway; you'll need to apply the Cray hack.
60 * (I'd like to check them with #if, but you can't use sizeof() in
66 #if BYTEORDER > 0xFFFF && defined(_CRAY) && !defined(_CRAYMPP)
67 # if BYTEORDER == 0x12345678
68 # define OFF16(p) (char*)(p)
69 # define OFF32(p) (char*)(p)
71 # if BYTEORDER == 0x87654321
72 # define OFF16(p) ((char*)(p) + (sizeof(U16) - SIZE16))
73 # define OFF32(p) ((char*)(p) + (sizeof(U32) - SIZE32))
75 }}}} bad cray byte order
78 # define COPY16(s,p) (*(p) = 0, Copy(s, OFF16(p), SIZE16, char))
79 # define COPY32(s,p) (*(p) = 0, Copy(s, OFF32(p), SIZE32, char))
80 # define CAT16(sv,p) sv_catpvn(sv, OFF16(p), SIZE16)
81 # define CAT32(sv,p) sv_catpvn(sv, OFF32(p), SIZE32)
83 # define COPY16(s,p) Copy(s, p, SIZE16, char)
84 # define COPY32(s,p) Copy(s, p, SIZE32, char)
85 # define CAT16(sv,p) sv_catpvn(sv, (char*)(p), SIZE16)
86 # define CAT32(sv,p) sv_catpvn(sv, (char*)(p), SIZE32)
89 static void doencodes _((SV* sv, char* s, I32 len));
90 static SV* refto _((SV* sv));
91 static U32 seed _((void));
93 static bool srand_called = FALSE;
95 /* variations on pp_null */
100 if (GIMME_V == G_SCALAR)
115 if (op->op_private & OPpLVAL_INTRO)
116 SAVECLEARSV(curpad[op->op_targ]);
118 if (op->op_flags & OPf_REF) {
122 if (GIMME == G_ARRAY) {
123 I32 maxarg = AvFILL((AV*)TARG) + 1;
125 Copy(AvARRAY((AV*)TARG), SP+1, maxarg, SV*);
129 SV* sv = sv_newmortal();
130 I32 maxarg = AvFILL((AV*)TARG) + 1;
131 sv_setiv(sv, maxarg);
143 if (op->op_private & OPpLVAL_INTRO)
144 SAVECLEARSV(curpad[op->op_targ]);
145 if (op->op_flags & OPf_REF)
148 if (gimme == G_ARRAY) {
149 RETURNOP(do_kv(ARGS));
151 else if (gimme == G_SCALAR) {
152 SV* sv = sv_newmortal();
153 if (HvFILL((HV*)TARG))
154 sv_setpvf(sv, "%ld/%ld",
155 (long)HvFILL((HV*)TARG), (long)HvMAX((HV*)TARG) + 1);
165 DIE("NOT IMPL LINE %d",__LINE__);
177 if (SvTYPE(sv) == SVt_PVIO) {
178 GV *gv = (GV*) sv_newmortal();
179 gv_init(gv, 0, "", 0, 0);
180 GvIOp(gv) = (IO *)sv;
181 (void)SvREFCNT_inc(sv);
183 } else if (SvTYPE(sv) != SVt_PVGV)
184 DIE("Not a GLOB reference");
187 if (SvTYPE(sv) != SVt_PVGV) {
190 if (SvGMAGICAL(sv)) {
196 if (op->op_flags & OPf_REF ||
197 op->op_private & HINT_STRICT_REFS)
198 DIE(no_usym, "a symbol");
204 if (op->op_private & HINT_STRICT_REFS)
205 DIE(no_symref, sym, "a symbol");
206 sv = (SV*)gv_fetchpv(sym, TRUE, SVt_PVGV);
209 if (op->op_private & OPpLVAL_INTRO)
210 save_gp((GV*)sv, !(op->op_flags & OPf_SPECIAL));
222 switch (SvTYPE(sv)) {
226 DIE("Not a SCALAR reference");
233 if (SvTYPE(gv) != SVt_PVGV) {
234 if (SvGMAGICAL(sv)) {
240 if (op->op_flags & OPf_REF ||
241 op->op_private & HINT_STRICT_REFS)
242 DIE(no_usym, "a SCALAR");
248 if (op->op_private & HINT_STRICT_REFS)
249 DIE(no_symref, sym, "a SCALAR");
250 gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PV);
254 if (op->op_flags & OPf_MOD) {
255 if (op->op_private & OPpLVAL_INTRO)
256 sv = save_scalar((GV*)TOPs);
257 else if (op->op_private & OPpDEREF)
258 vivify_ref(sv, op->op_private & OPpDEREF);
268 SV *sv = AvARYLEN(av);
270 AvARYLEN(av) = sv = NEWSV(0,0);
271 sv_upgrade(sv, SVt_IV);
272 sv_magic(sv, (SV*)av, '#', Nullch, 0);
280 dSP; dTARGET; dPOPss;
282 if (op->op_flags & OPf_MOD) {
283 if (SvTYPE(TARG) < SVt_PVLV) {
284 sv_upgrade(TARG, SVt_PVLV);
285 sv_magic(TARG, Nullsv, '.', Nullch, 0);
290 PUSHs(TARG); /* no SvSETMAGIC */
296 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
297 mg = mg_find(sv, 'g');
298 if (mg && mg->mg_len >= 0) {
299 PUSHi(mg->mg_len + curcop->cop_arybase);
313 /* We usually try to add a non-existent subroutine in case of AUTOLOAD. */
314 /* (But not in defined().) */
315 CV *cv = sv_2cv(TOPs, &stash, &gv, !(op->op_flags & OPf_SPECIAL));
318 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
335 cv = sv_2cv(TOPs, &stash, &gv, FALSE);
337 ret = sv_2mortal(newSVpv(SvPVX(cv), SvCUR(cv)));
345 CV* cv = (CV*)curpad[op->op_targ];
347 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
363 if (GIMME != G_ARRAY) {
367 EXTEND_MORTAL(SP - MARK);
369 *MARK = refto(*MARK);
379 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
382 if (!(sv = LvTARG(sv)))
385 else if (SvPADTMP(sv))
389 (void)SvREFCNT_inc(sv);
392 sv_upgrade(rv, SVt_RV);
406 if (sv && SvGMAGICAL(sv))
409 if (!sv || !SvROK(sv))
413 pv = sv_reftype(sv,TRUE);
414 PUSHp(pv, strlen(pv));
424 stash = curcop->cop_stash;
426 stash = gv_stashsv(POPs, TRUE);
428 (void)sv_bless(TOPs, stash);
432 /* Pattern matching */
437 register unsigned char *s;
440 register I32 *sfirst;
444 if (sv == lastscream) {
450 SvSCREAM_off(lastscream);
451 SvREFCNT_dec(lastscream);
453 lastscream = SvREFCNT_inc(sv);
456 s = (unsigned char*)(SvPV(sv, len));
460 if (pos > maxscream) {
462 maxscream = pos + 80;
463 New(301, screamfirst, 256, I32);
464 New(302, screamnext, maxscream, I32);
467 maxscream = pos + pos / 4;
468 Renew(screamnext, maxscream, I32);
472 sfirst = screamfirst;
475 if (!sfirst || !snext)
476 DIE("do_study: out of memory");
478 for (ch = 256; ch; --ch)
485 snext[pos] = sfirst[ch] - pos;
492 sv_magic(sv, Nullsv, 'g', Nullch, 0); /* piggyback on m//g magic */
501 if (op->op_flags & OPf_STACKED)
507 TARG = sv_newmortal();
508 PUSHi(do_trans(sv, op));
512 /* Lvalue operators. */
534 SETi(do_chomp(TOPs));
541 register I32 count = 0;
544 count += do_chomp(POPs);
555 if (!sv || !SvANY(sv))
557 switch (SvTYPE(sv)) {
559 if (AvMAX(sv) >= 0 || SvRMAGICAL(sv))
563 if (HvARRAY(sv) || SvRMAGICAL(sv))
567 if (CvROOT(sv) || CvXSUB(sv))
584 if (!op->op_private) {
593 if (SvTHINKFIRST(sv)) {
600 switch (SvTYPE(sv)) {
610 if (cv_const_sv((CV*)sv))
611 warn("Constant subroutine %s undefined",
612 CvANON((CV*)sv) ? "(anonymous)" : GvENAME(CvGV((CV*)sv)));
615 { GV* gv = (GV*)SvREFCNT_inc(CvGV((CV*)sv));
617 CvGV((CV*)sv) = gv; } /* let user-undef'd sub keep its identity */
621 sv_setsv(sv, &sv_undef);
624 if (SvTYPE(sv) >= SVt_PV && SvPVX(sv) && SvLEN(sv)) {
627 SvPV_set(sv, Nullch);
640 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
642 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
643 SvIVX(TOPs) != IV_MIN)
646 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
657 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
659 sv_setsv(TARG, TOPs);
660 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
661 SvIVX(TOPs) != IV_MAX)
664 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
678 if(SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
680 sv_setsv(TARG, TOPs);
681 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
682 SvIVX(TOPs) != IV_MIN)
685 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
694 /* Ordinary operators. */
698 dSP; dATARGET; tryAMAGICbin(pow,opASSIGN);
701 SETn( pow( left, right) );
708 dSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
711 SETn( left * right );
718 dSP; dATARGET; tryAMAGICbin(div,opASSIGN);
723 DIE("Illegal division by zero");
725 /* insure that 20./5. == 4. */
728 if ((double)I_V(left) == left &&
729 (double)I_V(right) == right &&
730 (k = I_V(left)/I_V(right))*I_V(right) == I_V(left)) {
733 value = left / right;
737 value = left / right;
746 dSP; dATARGET; tryAMAGICbin(mod,opASSIGN);
754 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)) {
756 right = (right_neg = (i < 0)) ? -i : i;
760 right = U_V((right_neg = (n < 0)) ? -n : n);
763 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)) {
765 left = (left_neg = (i < 0)) ? -i : i;
769 left = U_V((left_neg = (n < 0)) ? -n : n);
773 DIE("Illegal modulus zero");
776 if ((left_neg != right_neg) && ans)
779 /* XXX may warn: unary minus operator applied to unsigned type */
780 /* could change -foo to be (~foo)+1 instead */
781 if (ans <= -(UV)IV_MAX)
782 sv_setiv(TARG, (IV) -ans);
784 sv_setnv(TARG, -(double)ans);
795 dSP; dATARGET; tryAMAGICbin(repeat,opASSIGN);
797 register I32 count = POPi;
798 if (GIMME == G_ARRAY && op->op_private & OPpREPEAT_DOLIST) {
800 I32 items = SP - MARK;
812 repeatcpy((char*)(MARK + items), (char*)MARK,
813 items * sizeof(SV*), count - 1);
819 else { /* Note: mark already snarfed by pp_list */
824 if (TARG == tmpstr && SvTHINKFIRST(tmpstr)) {
825 if (SvREADONLY(tmpstr) && curcop != &compiling)
826 DIE("Can't x= to readonly value");
830 SvSetSV(TARG, tmpstr);
831 SvPV_force(TARG, len);
836 SvGROW(TARG, (count * len) + 1);
837 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
838 SvCUR(TARG) *= count;
842 (void)SvPOK_only(TARG);
851 dSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
854 SETn( left - right );
861 dSP; dATARGET; tryAMAGICbin(lshift,opASSIGN);
864 if (op->op_private & HINT_INTEGER) {
880 dSP; dATARGET; tryAMAGICbin(rshift,opASSIGN);
883 if (op->op_private & HINT_INTEGER) {
899 dSP; tryAMAGICbinSET(lt,0);
902 SETs(boolSV(TOPn < value));
909 dSP; tryAMAGICbinSET(gt,0);
912 SETs(boolSV(TOPn > value));
919 dSP; tryAMAGICbinSET(le,0);
922 SETs(boolSV(TOPn <= value));
929 dSP; tryAMAGICbinSET(ge,0);
932 SETs(boolSV(TOPn >= value));
939 dSP; tryAMAGICbinSET(ne,0);
942 SETs(boolSV(TOPn != value));
949 dSP; dTARGET; tryAMAGICbin(ncmp,0);
956 else if (left < right)
958 else if (left > right)
971 dSP; tryAMAGICbinSET(slt,0);
974 int cmp = ((op->op_private & OPpLOCALE)
975 ? sv_cmp_locale(left, right)
976 : sv_cmp(left, right));
977 SETs(boolSV(cmp < 0));
984 dSP; tryAMAGICbinSET(sgt,0);
987 int cmp = ((op->op_private & OPpLOCALE)
988 ? sv_cmp_locale(left, right)
989 : sv_cmp(left, right));
990 SETs(boolSV(cmp > 0));
997 dSP; tryAMAGICbinSET(sle,0);
1000 int cmp = ((op->op_private & OPpLOCALE)
1001 ? sv_cmp_locale(left, right)
1002 : sv_cmp(left, right));
1003 SETs(boolSV(cmp <= 0));
1010 dSP; tryAMAGICbinSET(sge,0);
1013 int cmp = ((op->op_private & OPpLOCALE)
1014 ? sv_cmp_locale(left, right)
1015 : sv_cmp(left, right));
1016 SETs(boolSV(cmp >= 0));
1023 dSP; tryAMAGICbinSET(seq,0);
1026 SETs(boolSV(sv_eq(left, right)));
1033 dSP; tryAMAGICbinSET(sne,0);
1036 SETs(boolSV(!sv_eq(left, right)));
1043 dSP; dTARGET; tryAMAGICbin(scmp,0);
1046 int cmp = ((op->op_private & OPpLOCALE)
1047 ? sv_cmp_locale(left, right)
1048 : sv_cmp(left, right));
1056 dSP; dATARGET; tryAMAGICbin(band,opASSIGN);
1059 if (SvNIOKp(left) || SvNIOKp(right)) {
1060 if (op->op_private & HINT_INTEGER) {
1061 IBW value = SvIV(left) & SvIV(right);
1065 UBW value = SvUV(left) & SvUV(right);
1070 do_vop(op->op_type, TARG, left, right);
1079 dSP; dATARGET; tryAMAGICbin(bxor,opASSIGN);
1082 if (SvNIOKp(left) || SvNIOKp(right)) {
1083 if (op->op_private & HINT_INTEGER) {
1084 IBW value = (USE_LEFT(left) ? SvIV(left) : 0) ^ SvIV(right);
1088 UBW value = (USE_LEFT(left) ? SvUV(left) : 0) ^ SvUV(right);
1093 do_vop(op->op_type, TARG, left, right);
1102 dSP; dATARGET; tryAMAGICbin(bor,opASSIGN);
1105 if (SvNIOKp(left) || SvNIOKp(right)) {
1106 if (op->op_private & HINT_INTEGER) {
1107 IBW value = (USE_LEFT(left) ? SvIV(left) : 0) | SvIV(right);
1111 UBW value = (USE_LEFT(left) ? SvUV(left) : 0) | SvUV(right);
1116 do_vop(op->op_type, TARG, left, right);
1125 dSP; dTARGET; tryAMAGICun(neg);
1130 if (SvIOKp(sv) && !SvNOKp(sv) && !SvPOKp(sv) && SvIVX(sv) != IV_MIN)
1132 else if (SvNIOKp(sv))
1134 else if (SvPOKp(sv)) {
1136 char *s = SvPV(sv, len);
1137 if (isIDFIRST(*s)) {
1138 sv_setpvn(TARG, "-", 1);
1141 else if (*s == '+' || *s == '-') {
1143 *SvPV_force(TARG, len) = *s == '-' ? '+' : '-';
1146 sv_setnv(TARG, -SvNV(sv));
1158 dSP; tryAMAGICunSET(not);
1159 #endif /* OVERLOAD */
1160 *stack_sp = boolSV(!SvTRUE(*stack_sp));
1166 dSP; dTARGET; tryAMAGICun(compl);
1170 if (op->op_private & HINT_INTEGER) {
1171 IBW value = ~SvIV(sv);
1175 UBW value = ~SvUV(sv);
1180 register char *tmps;
1181 register long *tmpl;
1186 tmps = SvPV_force(TARG, len);
1189 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
1192 for ( ; anum >= sizeof(long); anum -= sizeof(long), tmpl++)
1196 for ( ; anum > 0; anum--, tmps++)
1205 /* integer versions of some of the above */
1209 dSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
1212 SETi( left * right );
1219 dSP; dATARGET; tryAMAGICbin(div,opASSIGN);
1223 DIE("Illegal division by zero");
1224 value = POPi / value;
1232 dSP; dATARGET; tryAMAGICbin(mod,opASSIGN);
1236 DIE("Illegal modulus zero");
1237 SETi( left % right );
1244 dSP; dATARGET; tryAMAGICbin(add,opASSIGN);
1247 SETi( left + right );
1254 dSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
1257 SETi( left - right );
1264 dSP; tryAMAGICbinSET(lt,0);
1267 SETs(boolSV(left < right));
1274 dSP; tryAMAGICbinSET(gt,0);
1277 SETs(boolSV(left > right));
1284 dSP; tryAMAGICbinSET(le,0);
1287 SETs(boolSV(left <= right));
1294 dSP; tryAMAGICbinSET(ge,0);
1297 SETs(boolSV(left >= right));
1304 dSP; tryAMAGICbinSET(eq,0);
1307 SETs(boolSV(left == right));
1314 dSP; tryAMAGICbinSET(ne,0);
1317 SETs(boolSV(left != right));
1324 dSP; dTARGET; tryAMAGICbin(ncmp,0);
1331 else if (left < right)
1342 dSP; dTARGET; tryAMAGICun(neg);
1347 /* High falutin' math. */
1351 dSP; dTARGET; tryAMAGICbin(atan2,0);
1354 SETn(atan2(left, right));
1361 dSP; dTARGET; tryAMAGICun(sin);
1373 dSP; dTARGET; tryAMAGICun(cos);
1393 if (!srand_called) {
1394 (void)srand((unsigned)seed());
1395 srand_called = TRUE;
1398 value = rand() * value / 2147483648.0;
1401 value = rand() * value / 65536.0;
1404 value = rand() * value / 32768.0;
1406 value = rand() * value / (double)(((unsigned long)1) << RANDBITS);
1422 (void)srand((unsigned)anum);
1423 srand_called = TRUE;
1432 * This is really just a quick hack which grabs various garbage
1433 * values. It really should be a real hash algorithm which
1434 * spreads the effect of every input bit onto every output bit,
1435 * if someone who knows about such tings would bother to write it.
1436 * Might be a good idea to add that function to CORE as well.
1437 * No numbers below come from careful analysis or anyting here,
1438 * except they are primes and SEED_C1 > 1E6 to get a full-width
1439 * value from (tv_sec * SEED_C1 + tv_usec). The multipliers should
1440 * probably be bigger too.
1443 # define SEED_C1 1000003
1444 #define SEED_C4 73819
1446 # define SEED_C1 25747
1447 #define SEED_C4 20639
1451 #define SEED_C5 26107
1455 # include <starlet.h>
1456 /* when[] = (low 32 bits, high 32 bits) of time since epoch
1457 * in 100-ns units, typically incremented ever 10 ms. */
1458 unsigned int when[2];
1459 _ckvmssts(sys$gettim(when));
1460 u = (U32)SEED_C1 * when[0] + (U32)SEED_C2 * when[1];
1462 # ifdef HAS_GETTIMEOFDAY
1463 struct timeval when;
1464 gettimeofday(&when,(struct timezone *) 0);
1465 u = (U32)SEED_C1 * when.tv_sec + (U32)SEED_C2 * when.tv_usec;
1469 u = (U32)SEED_C1 * when;
1472 u += SEED_C3 * (U32)getpid();
1473 u += SEED_C4 * (U32)(UV)stack_sp;
1474 #ifndef PLAN9 /* XXX Plan9 assembler chokes on this; fix needed */
1475 u += SEED_C5 * (U32)(UV)&when;
1482 dSP; dTARGET; tryAMAGICun(exp);
1494 dSP; dTARGET; tryAMAGICun(log);
1499 SET_NUMERIC_STANDARD();
1500 DIE("Can't take log of %g", value);
1510 dSP; dTARGET; tryAMAGICun(sqrt);
1515 SET_NUMERIC_STANDARD();
1516 DIE("Can't take sqrt of %g", value);
1518 value = sqrt(value);
1528 double value = TOPn;
1531 if (SvIOKp(TOPs) && !SvNOKp(TOPs) && !SvPOKp(TOPs)) {
1537 (void)modf(value, &value);
1539 (void)modf(-value, &value);
1554 dSP; dTARGET; tryAMAGICun(abs);
1556 double value = TOPn;
1559 if (SvIOKp(TOPs) && !SvNOKp(TOPs) && !SvPOKp(TOPs) &&
1560 (iv = SvIVX(TOPs)) != IV_MIN) {
1581 XPUSHu(scan_hex(tmps, 99, &argtype));
1593 while (*tmps && isSPACE(*tmps))
1598 value = scan_hex(++tmps, 99, &argtype);
1600 value = scan_oct(tmps, 99, &argtype);
1610 SETi( sv_len(TOPs) );
1622 I32 lvalue = op->op_flags & OPf_MOD;
1624 I32 arybase = curcop->cop_arybase;
1628 pos = POPi - arybase;
1630 tmps = SvPV(sv, curlen);
1632 pos += curlen + arybase;
1633 if (pos < 0 || pos > curlen) {
1634 if (dowarn || lvalue)
1635 warn("substr outside of string");
1642 len += curlen - pos;
1647 rem = curlen - pos; /* rem=how many bytes left*/
1650 sv_setpvn(TARG, tmps, rem);
1651 if (lvalue) { /* it's an lvalue! */
1652 if (!SvGMAGICAL(sv)) {
1656 warn("Attempt to use reference as lvalue in substr");
1658 if (SvOK(sv)) /* is it defined ? */
1659 (void)SvPOK_only(sv);
1661 sv_setpvn(sv,"",0); /* avoid lexical reincarnation */
1664 if (SvTYPE(TARG) < SVt_PVLV) {
1665 sv_upgrade(TARG, SVt_PVLV);
1666 sv_magic(TARG, Nullsv, 'x', Nullch, 0);
1671 LvTARGOFF(TARG) = pos;
1672 LvTARGLEN(TARG) = rem;
1675 PUSHs(TARG); /* avoid SvSETMAGIC here */
1682 register I32 size = POPi;
1683 register I32 offset = POPi;
1684 register SV *src = POPs;
1685 I32 lvalue = op->op_flags & OPf_MOD;
1687 unsigned char *s = (unsigned char*)SvPV(src, srclen);
1688 unsigned long retnum;
1691 offset *= size; /* turn into bit offset */
1692 len = (offset + size + 7) / 8;
1693 if (offset < 0 || size < 1)
1696 if (lvalue) { /* it's an lvalue! */
1697 if (SvTYPE(TARG) < SVt_PVLV) {
1698 sv_upgrade(TARG, SVt_PVLV);
1699 sv_magic(TARG, Nullsv, 'v', Nullch, 0);
1704 LvTARGOFF(TARG) = offset;
1705 LvTARGLEN(TARG) = size;
1713 if (offset >= srclen)
1716 retnum = (unsigned long) s[offset] << 8;
1718 else if (size == 32) {
1719 if (offset >= srclen)
1721 else if (offset + 1 >= srclen)
1722 retnum = (unsigned long) s[offset] << 24;
1723 else if (offset + 2 >= srclen)
1724 retnum = ((unsigned long) s[offset] << 24) +
1725 ((unsigned long) s[offset + 1] << 16);
1727 retnum = ((unsigned long) s[offset] << 24) +
1728 ((unsigned long) s[offset + 1] << 16) +
1729 (s[offset + 2] << 8);
1734 retnum = (s[offset >> 3] >> (offset & 7)) & ((1 << size) - 1);
1739 else if (size == 16)
1740 retnum = ((unsigned long) s[offset] << 8) + s[offset+1];
1741 else if (size == 32)
1742 retnum = ((unsigned long) s[offset] << 24) +
1743 ((unsigned long) s[offset + 1] << 16) +
1744 (s[offset + 2] << 8) + s[offset+3];
1748 sv_setiv(TARG, (IV)retnum);
1763 I32 arybase = curcop->cop_arybase;
1768 offset = POPi - arybase;
1771 tmps = SvPV(big, biglen);
1774 else if (offset > biglen)
1776 if (!(tmps2 = fbm_instr((unsigned char*)tmps + offset,
1777 (unsigned char*)tmps + biglen, little)))
1778 retval = -1 + arybase;
1780 retval = tmps2 - tmps + arybase;
1797 I32 arybase = curcop->cop_arybase;
1803 tmps2 = SvPV(little, llen);
1804 tmps = SvPV(big, blen);
1808 offset = SvIV(offstr) - arybase + llen;
1811 else if (offset > blen)
1813 if (!(tmps2 = rninstr(tmps, tmps + offset,
1814 tmps2, tmps2 + llen)))
1815 retval = -1 + arybase;
1817 retval = tmps2 - tmps + arybase;
1824 dSP; dMARK; dORIGMARK; dTARGET;
1825 #ifdef USE_LOCALE_NUMERIC
1826 if (op->op_private & OPpLOCALE)
1827 SET_NUMERIC_LOCAL();
1829 SET_NUMERIC_STANDARD();
1831 do_sprintf(TARG, SP-MARK, MARK+1);
1832 TAINT_IF(SvTAINTED(TARG));
1846 value = (I32) (*tmps & 255);
1851 value = (I32) (anum & 255);
1862 (void)SvUPGRADE(TARG,SVt_PV);
1868 (void)SvPOK_only(TARG);
1875 dSP; dTARGET; dPOPTOPssrl;
1877 char *tmps = SvPV(left, na);
1879 sv_setpv(TARG, fcrypt(tmps, SvPV(right, na)));
1881 sv_setpv(TARG, crypt(tmps, SvPV(right, na)));
1885 "The crypt() function is unimplemented due to excessive paranoia.");
1897 if (!SvPADTMP(sv)) {
1903 s = SvPV_force(sv, na);
1905 if (op->op_private & OPpLOCALE) {
1908 *s = toUPPER_LC(*s);
1923 if (!SvPADTMP(sv)) {
1929 s = SvPV_force(sv, na);
1931 if (op->op_private & OPpLOCALE) {
1934 *s = toLOWER_LC(*s);
1951 if (!SvPADTMP(sv)) {
1958 s = SvPV_force(sv, len);
1960 register char *send = s + len;
1962 if (op->op_private & OPpLOCALE) {
1965 for (; s < send; s++)
1966 *s = toUPPER_LC(*s);
1969 for (; s < send; s++)
1983 if (!SvPADTMP(sv)) {
1990 s = SvPV_force(sv, len);
1992 register char *send = s + len;
1994 if (op->op_private & OPpLOCALE) {
1997 for (; s < send; s++)
1998 *s = toLOWER_LC(*s);
2001 for (; s < send; s++)
2013 register char *s = SvPV(sv,len);
2017 (void)SvUPGRADE(TARG, SVt_PV);
2018 SvGROW(TARG, (len * 2) + 1);
2026 SvCUR_set(TARG, d - SvPVX(TARG));
2027 (void)SvPOK_only(TARG);
2030 sv_setpvn(TARG, s, len);
2039 dSP; dMARK; dORIGMARK;
2041 register AV* av = (AV*)POPs;
2042 register I32 lval = op->op_flags & OPf_MOD;
2043 I32 arybase = curcop->cop_arybase;
2046 if (SvTYPE(av) == SVt_PVAV) {
2047 if (lval && op->op_private & OPpLVAL_INTRO) {
2049 for (svp = mark + 1; svp <= sp; svp++) {
2054 if (max > AvMAX(av))
2057 while (++MARK <= SP) {
2058 elem = SvIVx(*MARK);
2062 svp = av_fetch(av, elem, lval);
2064 if (!svp || *svp == &sv_undef)
2065 DIE(no_aelem, elem);
2066 if (op->op_private & OPpLVAL_INTRO)
2069 *MARK = svp ? *svp : &sv_undef;
2072 if (GIMME != G_ARRAY) {
2080 /* Associative arrays. */
2085 HV *hash = (HV*)POPs;
2087 I32 gimme = GIMME_V;
2090 entry = hv_iternext(hash); /* might clobber stack_sp */
2095 PUSHs(hv_iterkeysv(entry)); /* won't clobber stack_sp */
2096 if (gimme == G_ARRAY) {
2098 sv_setsv(TARG, hv_iterval(hash, entry)); /* might hit stack_sp */
2103 else if (gimme == G_SCALAR)
2122 I32 gimme = GIMME_V;
2123 I32 discard = (gimme == G_VOID) ? G_DISCARD : 0;
2127 if (op->op_private & OPpSLICE) {
2130 if (SvTYPE(hv) != SVt_PVHV)
2131 DIE("Not a HASH reference");
2132 while (++MARK <= SP) {
2133 sv = hv_delete_ent(hv, *MARK, discard, 0);
2134 *MARK = sv ? sv : &sv_undef;
2138 else if (gimme == G_SCALAR) {
2147 if (SvTYPE(hv) != SVt_PVHV)
2148 DIE("Not a HASH reference");
2149 sv = hv_delete_ent(hv, keysv, discard, 0);
2164 if (SvTYPE(hv) != SVt_PVHV) {
2165 DIE("Not a HASH reference");
2167 if (hv_exists_ent(hv, tmpsv, 0))
2174 dSP; dMARK; dORIGMARK;
2176 register HV *hv = (HV*)POPs;
2177 register I32 lval = op->op_flags & OPf_MOD;
2179 if (SvTYPE(hv) == SVt_PVHV) {
2180 while (++MARK <= SP) {
2183 he = hv_fetch_ent(hv, keysv, lval, 0);
2185 if (!he || HeVAL(he) == &sv_undef)
2186 DIE(no_helem, SvPV(keysv, na));
2187 if (op->op_private & OPpLVAL_INTRO)
2188 save_svref(&HeVAL(he));
2190 *MARK = he ? HeVAL(he) : &sv_undef;
2193 if (GIMME != G_ARRAY) {
2201 /* List operators. */
2206 if (GIMME != G_ARRAY) {
2208 *MARK = *SP; /* unwanted list, return last item */
2219 SV **lastrelem = stack_sp;
2220 SV **lastlelem = stack_base + POPMARK;
2221 SV **firstlelem = stack_base + POPMARK + 1;
2222 register SV **firstrelem = lastlelem + 1;
2223 I32 arybase = curcop->cop_arybase;
2224 I32 lval = op->op_flags & OPf_MOD;
2225 I32 is_something_there = lval;
2227 register I32 max = lastrelem - lastlelem;
2228 register SV **lelem;
2231 if (GIMME != G_ARRAY) {
2232 ix = SvIVx(*lastlelem);
2237 if (ix < 0 || ix >= max)
2238 *firstlelem = &sv_undef;
2240 *firstlelem = firstrelem[ix];
2246 SP = firstlelem - 1;
2250 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
2256 else if (!(*lelem = firstrelem[ix]))
2261 if (ix >= max || !(*lelem = firstrelem[ix]))
2264 if (!is_something_there && (SvOK(*lelem) || SvGMAGICAL(*lelem)))
2265 is_something_there = TRUE;
2267 if (is_something_there)
2270 SP = firstlelem - 1;
2276 dSP; dMARK; dORIGMARK;
2277 I32 items = SP - MARK;
2278 SV *av = sv_2mortal((SV*)av_make(items, MARK+1));
2279 SP = ORIGMARK; /* av_make() might realloc stack_sp */
2286 dSP; dMARK; dORIGMARK;
2287 HV* hv = (HV*)sv_2mortal((SV*)newHV());
2291 SV *val = NEWSV(46, 0);
2293 sv_setsv(val, *++MARK);
2295 warn("Odd number of elements in hash list");
2296 (void)hv_store_ent(hv,key,val,0);
2305 dSP; dMARK; dORIGMARK;
2306 register AV *ary = (AV*)*++MARK;
2310 register I32 offset;
2311 register I32 length;
2320 offset = SvIVx(*MARK);
2322 offset += AvFILL(ary) + 1;
2324 offset -= curcop->cop_arybase;
2326 length = SvIVx(*MARK++);
2331 length = AvMAX(ary) + 1; /* close enough to infinity */
2335 length = AvMAX(ary) + 1;
2343 if (offset > AvFILL(ary) + 1)
2344 offset = AvFILL(ary) + 1;
2345 after = AvFILL(ary) + 1 - (offset + length);
2346 if (after < 0) { /* not that much array */
2347 length += after; /* offset+length now in array */
2353 /* At this point, MARK .. SP-1 is our new LIST */
2356 diff = newlen - length;
2358 if (diff < 0) { /* shrinking the area */
2360 New(451, tmparyval, newlen, SV*); /* so remember insertion */
2361 Copy(MARK, tmparyval, newlen, SV*);
2364 MARK = ORIGMARK + 1;
2365 if (GIMME == G_ARRAY) { /* copy return vals to stack */
2366 MEXTEND(MARK, length);
2367 Copy(AvARRAY(ary)+offset, MARK, length, SV*);
2369 EXTEND_MORTAL(length);
2370 for (i = length, dst = MARK; i; i--) {
2371 if (!SvIMMORTAL(*dst))
2372 sv_2mortal(*dst); /* free them eventualy */
2379 *MARK = AvARRAY(ary)[offset+length-1];
2381 if (!SvIMMORTAL(*MARK))
2383 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
2384 SvREFCNT_dec(*dst++); /* free them now */
2387 AvFILL(ary) += diff;
2389 /* pull up or down? */
2391 if (offset < after) { /* easier to pull up */
2392 if (offset) { /* esp. if nothing to pull */
2393 src = &AvARRAY(ary)[offset-1];
2394 dst = src - diff; /* diff is negative */
2395 for (i = offset; i > 0; i--) /* can't trust Copy */
2399 SvPVX(ary) = (char*)(AvARRAY(ary) - diff); /* diff is negative */
2403 if (after) { /* anything to pull down? */
2404 src = AvARRAY(ary) + offset + length;
2405 dst = src + diff; /* diff is negative */
2406 Move(src, dst, after, SV*);
2408 dst = &AvARRAY(ary)[AvFILL(ary)+1];
2409 /* avoid later double free */
2413 dst[--i] = &sv_undef;
2416 for (src = tmparyval, dst = AvARRAY(ary) + offset;
2418 *dst = NEWSV(46, 0);
2419 sv_setsv(*dst++, *src++);
2421 Safefree(tmparyval);
2424 else { /* no, expanding (or same) */
2426 New(452, tmparyval, length, SV*); /* so remember deletion */
2427 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
2430 if (diff > 0) { /* expanding */
2432 /* push up or down? */
2434 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
2438 Move(src, dst, offset, SV*);
2440 SvPVX(ary) = (char*)(AvARRAY(ary) - diff);/* diff is positive */
2442 AvFILL(ary) += diff;
2445 if (AvFILL(ary) + diff >= AvMAX(ary)) /* oh, well */
2446 av_extend(ary, AvFILL(ary) + diff);
2447 AvFILL(ary) += diff;
2450 dst = AvARRAY(ary) + AvFILL(ary);
2452 for (i = after; i; i--) {
2459 for (src = MARK, dst = AvARRAY(ary) + offset; newlen; newlen--) {
2460 *dst = NEWSV(46, 0);
2461 sv_setsv(*dst++, *src++);
2463 MARK = ORIGMARK + 1;
2464 if (GIMME == G_ARRAY) { /* copy return vals to stack */
2466 Copy(tmparyval, MARK, length, SV*);
2468 EXTEND_MORTAL(length);
2469 for (i = length, dst = MARK; i; i--) {
2470 if (!SvIMMORTAL(*dst))
2471 sv_2mortal(*dst); /* free them eventualy */
2475 Safefree(tmparyval);
2479 else if (length--) {
2480 *MARK = tmparyval[length];
2482 if (!SvIMMORTAL(*MARK))
2484 while (length-- > 0)
2485 SvREFCNT_dec(tmparyval[length]);
2487 Safefree(tmparyval);
2498 dSP; dMARK; dORIGMARK; dTARGET;
2499 register AV *ary = (AV*)*++MARK;
2500 register SV *sv = &sv_undef;
2502 for (++MARK; MARK <= SP; MARK++) {
2505 sv_setsv(sv, *MARK);
2509 PUSHi( AvFILL(ary) + 1 );
2517 SV *sv = av_pop(av);
2518 if (!SvIMMORTAL(sv) && AvREAL(av))
2519 (void)sv_2mortal(sv);
2528 SV *sv = av_shift(av);
2532 if (!SvIMMORTAL(sv) && AvREAL(av))
2533 (void)sv_2mortal(sv);
2540 dSP; dMARK; dORIGMARK; dTARGET;
2541 register AV *ary = (AV*)*++MARK;
2545 av_unshift(ary, SP - MARK);
2548 sv_setsv(sv, *++MARK);
2549 (void)av_store(ary, i++, sv);
2553 PUSHi( AvFILL(ary) + 1 );
2563 if (GIMME == G_ARRAY) {
2574 register char *down;
2580 do_join(TARG, &sv_no, MARK, SP);
2582 sv_setsv(TARG, (SP > MARK) ? *SP : GvSV(defgv));
2583 up = SvPV_force(TARG, len);
2585 down = SvPVX(TARG) + len - 1;
2591 (void)SvPOK_only(TARG);
2605 char *s = SvPV(sv, len);
2609 if (!strnEQ(s, "0000", 4)) { /* need to grow sv */
2610 SV *new = newSVpv("0000000000", 10);
2613 SvREFCNT_dec(sv); /* free old sv */
2618 while (!*t) /* trailing '\0'? */
2621 i = ((*t - '0') << 7) + m;
2622 *(t--) = '0' + (i % 10);
2628 /* Explosives and implosives. */
2635 I32 gimme = GIMME_V;
2639 register char *pat = SvPV(left, llen);
2640 register char *s = SvPV(right, rlen);
2641 char *strend = s + rlen;
2643 register char *patend = pat + llen;
2648 /* These must not be in registers: */
2659 unsigned Quad_t auquad;
2665 register U32 culong;
2667 static char* bitcount = 0;
2669 if (gimme != G_ARRAY) { /* arrange to do first one only */
2671 for (patend = pat; !isALPHA(*patend) || *patend == 'x'; patend++) ;
2672 if (strchr("aAbBhHP", *patend) || *pat == '%') {
2674 while (isDIGIT(*patend) || *patend == '*')
2680 while (pat < patend) {
2682 datumtype = *pat++ & 0xFF;
2683 if (isSPACE(datumtype))
2687 else if (*pat == '*') {
2688 len = strend - strbeg; /* long enough */
2691 else if (isDIGIT(*pat)) {
2693 while (isDIGIT(*pat))
2694 len = (len * 10) + (*pat++ - '0');
2697 len = (datumtype != '@');
2700 croak("Invalid type in unpack: '%c'", (int)datumtype);
2702 if (len == 1 && pat[-1] != '1')
2711 if (len > strend - strbeg)
2712 DIE("@ outside of string");
2716 if (len > s - strbeg)
2717 DIE("X outside of string");
2721 if (len > strend - s)
2722 DIE("x outside of string");
2727 if (len > strend - s)
2730 goto uchar_checksum;
2731 sv = NEWSV(35, len);
2732 sv_setpvn(sv, s, len);
2734 if (datumtype == 'A') {
2735 aptr = s; /* borrow register */
2736 s = SvPVX(sv) + len - 1;
2737 while (s >= SvPVX(sv) && (!*s || isSPACE(*s)))
2740 SvCUR_set(sv, s - SvPVX(sv));
2741 s = aptr; /* unborrow register */
2743 XPUSHs(sv_2mortal(sv));
2747 if (pat[-1] == '*' || len > (strend - s) * 8)
2748 len = (strend - s) * 8;
2751 Newz(601, bitcount, 256, char);
2752 for (bits = 1; bits < 256; bits++) {
2753 if (bits & 1) bitcount[bits]++;
2754 if (bits & 2) bitcount[bits]++;
2755 if (bits & 4) bitcount[bits]++;
2756 if (bits & 8) bitcount[bits]++;
2757 if (bits & 16) bitcount[bits]++;
2758 if (bits & 32) bitcount[bits]++;
2759 if (bits & 64) bitcount[bits]++;
2760 if (bits & 128) bitcount[bits]++;
2764 culong += bitcount[*(unsigned char*)s++];
2769 if (datumtype == 'b') {
2771 if (bits & 1) culong++;
2777 if (bits & 128) culong++;
2784 sv = NEWSV(35, len + 1);
2787 aptr = pat; /* borrow register */
2789 if (datumtype == 'b') {
2791 for (len = 0; len < aint; len++) {
2792 if (len & 7) /*SUPPRESS 595*/
2796 *pat++ = '0' + (bits & 1);
2801 for (len = 0; len < aint; len++) {
2806 *pat++ = '0' + ((bits & 128) != 0);
2810 pat = aptr; /* unborrow register */
2811 XPUSHs(sv_2mortal(sv));
2815 if (pat[-1] == '*' || len > (strend - s) * 2)
2816 len = (strend - s) * 2;
2817 sv = NEWSV(35, len + 1);
2820 aptr = pat; /* borrow register */
2822 if (datumtype == 'h') {
2824 for (len = 0; len < aint; len++) {
2829 *pat++ = hexdigit[bits & 15];
2834 for (len = 0; len < aint; len++) {
2839 *pat++ = hexdigit[(bits >> 4) & 15];
2843 pat = aptr; /* unborrow register */
2844 XPUSHs(sv_2mortal(sv));
2847 if (len > strend - s)
2852 if (aint >= 128) /* fake up signed chars */
2862 if (aint >= 128) /* fake up signed chars */
2865 sv_setiv(sv, (IV)aint);
2866 PUSHs(sv_2mortal(sv));
2871 if (len > strend - s)
2886 sv_setiv(sv, (IV)auint);
2887 PUSHs(sv_2mortal(sv));
2892 along = (strend - s) / SIZE16;
2909 sv_setiv(sv, (IV)ashort);
2910 PUSHs(sv_2mortal(sv));
2917 along = (strend - s) / SIZE16;
2922 COPY16(s, &aushort);
2925 if (datumtype == 'n')
2926 aushort = ntohs(aushort);
2929 if (datumtype == 'v')
2930 aushort = vtohs(aushort);
2939 COPY16(s, &aushort);
2943 if (datumtype == 'n')
2944 aushort = ntohs(aushort);
2947 if (datumtype == 'v')
2948 aushort = vtohs(aushort);
2950 sv_setiv(sv, (IV)aushort);
2951 PUSHs(sv_2mortal(sv));
2956 along = (strend - s) / sizeof(int);
2961 Copy(s, &aint, 1, int);
2964 cdouble += (double)aint;
2973 Copy(s, &aint, 1, int);
2976 sv_setiv(sv, (IV)aint);
2977 PUSHs(sv_2mortal(sv));
2982 along = (strend - s) / sizeof(unsigned int);
2987 Copy(s, &auint, 1, unsigned int);
2988 s += sizeof(unsigned int);
2990 cdouble += (double)auint;
2999 Copy(s, &auint, 1, unsigned int);
3000 s += sizeof(unsigned int);
3002 sv_setuv(sv, (UV)auint);
3003 PUSHs(sv_2mortal(sv));
3008 along = (strend - s) / SIZE32;
3016 cdouble += (double)along;
3028 sv_setiv(sv, (IV)along);
3029 PUSHs(sv_2mortal(sv));
3036 along = (strend - s) / SIZE32;
3044 if (datumtype == 'N')
3045 aulong = ntohl(aulong);
3048 if (datumtype == 'V')
3049 aulong = vtohl(aulong);
3052 cdouble += (double)aulong;
3064 if (datumtype == 'N')
3065 aulong = ntohl(aulong);
3068 if (datumtype == 'V')
3069 aulong = vtohl(aulong);
3072 sv_setuv(sv, (UV)aulong);
3073 PUSHs(sv_2mortal(sv));
3078 along = (strend - s) / sizeof(char*);
3084 if (sizeof(char*) > strend - s)
3087 Copy(s, &aptr, 1, char*);
3093 PUSHs(sv_2mortal(sv));
3103 while ((len > 0) && (s < strend)) {
3104 auv = (auv << 7) | (*s & 0x7f);
3105 if (!(*s++ & 0x80)) {
3109 PUSHs(sv_2mortal(sv));
3113 else if (++bytes >= sizeof(UV)) { /* promote to string */
3116 sv = newSVpvf("%.*Vu", (int)TYPE_DIGITS(UV), auv);
3117 while (s < strend) {
3118 sv = mul128(sv, *s & 0x7f);
3119 if (!(*s++ & 0x80)) {
3128 PUSHs(sv_2mortal(sv));
3133 if ((s >= strend) && bytes)
3134 croak("Unterminated compressed integer");
3139 if (sizeof(char*) > strend - s)
3142 Copy(s, &aptr, 1, char*);
3147 sv_setpvn(sv, aptr, len);
3148 PUSHs(sv_2mortal(sv));
3155 if (s + sizeof(Quad_t) > strend)
3158 Copy(s, &aquad, 1, Quad_t);
3159 s += sizeof(Quad_t);
3162 if (aquad >= IV_MIN && aquad <= IV_MAX)
3163 sv_setiv(sv, (IV)aquad);
3165 sv_setnv(sv, (double)aquad);
3166 PUSHs(sv_2mortal(sv));
3173 if (s + sizeof(unsigned Quad_t) > strend)
3176 Copy(s, &auquad, 1, unsigned Quad_t);
3177 s += sizeof(unsigned Quad_t);
3180 if (aquad <= UV_MAX)
3181 sv_setuv(sv, (UV)auquad);
3183 sv_setnv(sv, (double)auquad);
3184 PUSHs(sv_2mortal(sv));
3188 /* float and double added gnb@melba.bby.oz.au 22/11/89 */
3191 along = (strend - s) / sizeof(float);
3196 Copy(s, &afloat, 1, float);
3205 Copy(s, &afloat, 1, float);
3208 sv_setnv(sv, (double)afloat);
3209 PUSHs(sv_2mortal(sv));
3215 along = (strend - s) / sizeof(double);
3220 Copy(s, &adouble, 1, double);
3221 s += sizeof(double);
3229 Copy(s, &adouble, 1, double);
3230 s += sizeof(double);
3232 sv_setnv(sv, (double)adouble);
3233 PUSHs(sv_2mortal(sv));
3238 along = (strend - s) * 3 / 4;
3239 sv = NEWSV(42, along);
3242 while (s < strend && *s > ' ' && *s < 'a') {
3247 len = (*s++ - ' ') & 077;
3249 if (s < strend && *s >= ' ')
3250 a = (*s++ - ' ') & 077;
3253 if (s < strend && *s >= ' ')
3254 b = (*s++ - ' ') & 077;
3257 if (s < strend && *s >= ' ')
3258 c = (*s++ - ' ') & 077;
3261 if (s < strend && *s >= ' ')
3262 d = (*s++ - ' ') & 077;
3265 hunk[0] = a << 2 | b >> 4;
3266 hunk[1] = b << 4 | c >> 2;
3267 hunk[2] = c << 6 | d;
3268 sv_catpvn(sv, hunk, len > 3 ? 3 : len);
3273 else if (s[1] == '\n') /* possible checksum byte */
3276 XPUSHs(sv_2mortal(sv));
3281 if (strchr("fFdD", datumtype) ||
3282 (checksum > 32 && strchr("iIlLN", datumtype)) ) {
3286 while (checksum >= 16) {
3290 while (checksum >= 4) {
3296 along = (1 << checksum) - 1;
3297 while (cdouble < 0.0)
3299 cdouble = modf(cdouble / adouble, &trouble) * adouble;
3300 sv_setnv(sv, cdouble);
3303 if (checksum < 32) {
3304 aulong = (1 << checksum) - 1;
3307 sv_setuv(sv, (UV)culong);
3309 XPUSHs(sv_2mortal(sv));
3313 if (sp == oldsp && gimme == G_SCALAR)
3319 doencodes(sv, s, len)
3327 sv_catpvn(sv, hunk, 1);
3330 hunk[0] = ' ' + (077 & (*s >> 2));
3331 hunk[1] = ' ' + (077 & ((*s << 4) & 060 | (s[1] >> 4) & 017));
3332 hunk[2] = ' ' + (077 & ((s[1] << 2) & 074 | (s[2] >> 6) & 03));
3333 hunk[3] = ' ' + (077 & (s[2] & 077));
3334 sv_catpvn(sv, hunk, 4);
3338 for (s = SvPVX(sv); *s; s++) {
3342 sv_catpvn(sv, "\n", 1);
3350 SV *result = newSVpv("", l);
3351 char *result_c = SvPV(result, na); /* convenience */
3352 char *out = result_c;
3362 SvREFCNT_dec(result);
3385 SvREFCNT_dec(result);
3391 SvCUR_set(result, out - result_c);
3397 SV *pnum; /* must be '\0' terminated */
3401 char *s = SvPV(pnum, len);
3410 i = m * 10 + (*t - '0');
3412 r = (i >> 7); /* r < 10 */
3419 SvCUR_set(pnum, (STRLEN) (t - s));
3426 dSP; dMARK; dORIGMARK; dTARGET;
3427 register SV *cat = TARG;
3430 register char *pat = SvPVx(*++MARK, fromlen);
3431 register char *patend = pat + fromlen;
3436 static char null10[] = {0,0,0,0,0,0,0,0,0,0};
3437 static char *space10 = " ";
3439 /* These must not be in registers: */
3448 unsigned Quad_t auquad;
3456 sv_setpvn(cat, "", 0);
3457 while (pat < patend) {
3458 #define NEXTFROM (items-- > 0 ? *MARK++ : &sv_no)
3459 datumtype = *pat++ & 0xFF;
3460 if (isSPACE(datumtype))
3463 len = strchr("@Xxu", datumtype) ? 0 : items;
3466 else if (isDIGIT(*pat)) {
3468 while (isDIGIT(*pat))
3469 len = (len * 10) + (*pat++ - '0');
3475 croak("Invalid type in pack: '%c'", (int)datumtype);
3477 DIE("%% may only be used in unpack");
3488 if (SvCUR(cat) < len)
3489 DIE("X outside of string");
3496 sv_catpvn(cat, null10, 10);
3499 sv_catpvn(cat, null10, len);
3504 aptr = SvPV(fromstr, fromlen);
3508 sv_catpvn(cat, aptr, len);
3510 sv_catpvn(cat, aptr, fromlen);
3512 if (datumtype == 'A') {
3514 sv_catpvn(cat, space10, 10);
3517 sv_catpvn(cat, space10, len);
3521 sv_catpvn(cat, null10, 10);
3524 sv_catpvn(cat, null10, len);
3531 char *savepat = pat;
3536 aptr = SvPV(fromstr, fromlen);
3541 SvCUR(cat) += (len+7)/8;
3542 SvGROW(cat, SvCUR(cat) + 1);
3543 aptr = SvPVX(cat) + aint;
3548 if (datumtype == 'B') {
3549 for (len = 0; len++ < aint;) {
3550 items |= *pat++ & 1;
3554 *aptr++ = items & 0xff;
3560 for (len = 0; len++ < aint;) {
3566 *aptr++ = items & 0xff;
3572 if (datumtype == 'B')
3573 items <<= 7 - (aint & 7);
3575 items >>= 7 - (aint & 7);
3576 *aptr++ = items & 0xff;
3578 pat = SvPVX(cat) + SvCUR(cat);
3589 char *savepat = pat;
3594 aptr = SvPV(fromstr, fromlen);
3599 SvCUR(cat) += (len+1)/2;
3600 SvGROW(cat, SvCUR(cat) + 1);
3601 aptr = SvPVX(cat) + aint;
3606 if (datumtype == 'H') {
3607 for (len = 0; len++ < aint;) {
3609 items |= ((*pat++ & 15) + 9) & 15;
3611 items |= *pat++ & 15;
3615 *aptr++ = items & 0xff;
3621 for (len = 0; len++ < aint;) {
3623 items |= (((*pat++ & 15) + 9) & 15) << 4;
3625 items |= (*pat++ & 15) << 4;
3629 *aptr++ = items & 0xff;
3635 *aptr++ = items & 0xff;
3636 pat = SvPVX(cat) + SvCUR(cat);
3648 aint = SvIV(fromstr);
3650 sv_catpvn(cat, &achar, sizeof(char));
3653 /* Float and double added by gnb@melba.bby.oz.au 22/11/89 */
3658 afloat = (float)SvNV(fromstr);
3659 sv_catpvn(cat, (char *)&afloat, sizeof (float));
3666 adouble = (double)SvNV(fromstr);
3667 sv_catpvn(cat, (char *)&adouble, sizeof (double));
3673 ashort = (I16)SvIV(fromstr);
3675 ashort = htons(ashort);
3677 CAT16(cat, &ashort);
3683 ashort = (I16)SvIV(fromstr);
3685 ashort = htovs(ashort);
3687 CAT16(cat, &ashort);
3694 ashort = (I16)SvIV(fromstr);
3695 CAT16(cat, &ashort);
3701 auint = SvUV(fromstr);
3702 sv_catpvn(cat, (char*)&auint, sizeof(unsigned int));
3708 adouble = floor(SvNV(fromstr));
3711 croak("Cannot compress negative numbers");
3721 char buf[1 + sizeof(UV)];
3722 char *in = buf + sizeof(buf);
3723 UV auv = U_V(adouble);;
3726 *--in = (auv & 0x7f) | 0x80;
3729 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
3730 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
3732 else if (SvPOKp(fromstr)) { /* decimal string arithmetics */
3733 char *from, *result, *in;
3738 /* Copy string and check for compliance */
3739 from = SvPV(fromstr, len);
3740 if ((norm = is_an_int(from, len)) == NULL)
3741 croak("can compress only unsigned integer");
3743 New('w', result, len, char);
3747 *--in = div128(norm, &done) | 0x80;
3748 result[len - 1] &= 0x7F; /* clear continue bit */
3749 sv_catpvn(cat, in, (result + len) - in);
3751 SvREFCNT_dec(norm); /* free norm */
3753 else if (SvNOKp(fromstr)) {
3754 char buf[sizeof(double) * 2]; /* 8/7 <= 2 */
3755 char *in = buf + sizeof(buf);
3758 double next = floor(adouble / 128);
3759 *--in = (unsigned char)(adouble - (next * 128)) | 0x80;
3760 if (--in < buf) /* this cannot happen ;-) */
3761 croak ("Cannot compress integer");
3763 } while (adouble > 0);
3764 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
3765 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
3768 croak("Cannot compress non integer");
3774 aint = SvIV(fromstr);
3775 sv_catpvn(cat, (char*)&aint, sizeof(int));
3781 aulong = SvUV(fromstr);
3783 aulong = htonl(aulong);
3785 CAT32(cat, &aulong);
3791 aulong = SvUV(fromstr);
3793 aulong = htovl(aulong);
3795 CAT32(cat, &aulong);
3801 aulong = SvUV(fromstr);
3802 CAT32(cat, &aulong);
3808 along = SvIV(fromstr);
3816 auquad = (unsigned Quad_t)SvIV(fromstr);
3817 sv_catpvn(cat, (char*)&auquad, sizeof(unsigned Quad_t));
3823 aquad = (Quad_t)SvIV(fromstr);
3824 sv_catpvn(cat, (char*)&aquad, sizeof(Quad_t));
3827 #endif /* HAS_QUAD */
3829 len = 1; /* assume SV is correct length */
3834 if (fromstr == &sv_undef)
3837 if (SvREADONLY(fromstr) && curcop != &compiling) {
3838 fromstr = sv_mortalcopy(fromstr);
3840 aptr = SvPV_force(fromstr, na);
3842 sv_catpvn(cat, (char*)&aptr, sizeof(char*));
3847 aptr = SvPV(fromstr, fromlen);
3848 SvGROW(cat, fromlen * 4 / 3);
3853 while (fromlen > 0) {
3860 doencodes(cat, aptr, todo);
3878 register I32 limit = POPi; /* note, negative is forever */
3881 register char *s = SvPV(sv, len);
3882 char *strend = s + len;
3884 register REGEXP *rx;
3888 I32 maxiters = (strend - s) + 10;
3891 I32 origlimit = limit;
3894 AV *oldstack = curstack;
3895 I32 gimme = GIMME_V;
3896 I32 oldsave = savestack_ix;
3899 Copy(&LvTARGOFF(POPs), &pm, 1, PMOP*);
3904 DIE("panic: do_split");
3905 rx = pm->op_pmregexp;
3907 TAINT_IF((pm->op_pmflags & PMf_LOCALE) &&
3908 (pm->op_pmflags & (PMf_WHITE | PMf_SKIPWHITE)));
3910 if (pm->op_pmreplroot)
3911 ary = GvAVn((GV*)pm->op_pmreplroot);
3912 else if (gimme != G_ARRAY)
3916 if (ary && (gimme != G_ARRAY || (pm->op_pmflags & PMf_ONCE))) {
3920 for (i = AvFILL(ary); i >= 0; i--)
3921 AvARRAY(ary)[i] = &sv_undef; /* don't free mere refs */
3925 /* temporarily switch stacks */
3926 SWITCHSTACK(curstack, ary);
3928 base = SP - stack_base;
3930 if (pm->op_pmflags & PMf_SKIPWHITE) {
3931 if (pm->op_pmflags & PMf_LOCALE) {
3932 while (isSPACE_LC(*s))
3940 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
3942 multiline = pm->op_pmflags & PMf_MULTILINE;
3946 limit = maxiters + 2;
3947 if (pm->op_pmflags & PMf_WHITE) {
3950 while (m < strend &&
3951 !((pm->op_pmflags & PMf_LOCALE)
3952 ? isSPACE_LC(*m) : isSPACE(*m)))
3957 dstr = NEWSV(30, m-s);
3958 sv_setpvn(dstr, s, m-s);
3964 while (s < strend &&
3965 ((pm->op_pmflags & PMf_LOCALE)
3966 ? isSPACE_LC(*s) : isSPACE(*s)))
3970 else if (strEQ("^", rx->precomp)) {
3973 for (m = s; m < strend && *m != '\n'; m++) ;
3977 dstr = NEWSV(30, m-s);
3978 sv_setpvn(dstr, s, m-s);
3985 else if (pm->op_pmshort && !rx->nparens) {
3986 i = SvCUR(pm->op_pmshort);
3988 i = *SvPVX(pm->op_pmshort);
3991 for (m = s; m < strend && *m != i; m++) ;
3994 dstr = NEWSV(30, m-s);
3995 sv_setpvn(dstr, s, m-s);
4004 while (s < strend && --limit &&
4005 (m=fbm_instr((unsigned char*)s, (unsigned char*)strend,
4009 dstr = NEWSV(31, m-s);
4010 sv_setpvn(dstr, s, m-s);
4019 maxiters += (strend - s) * rx->nparens;
4020 while (s < strend && --limit &&
4021 pregexec(rx, s, strend, orig, 1, Nullsv, TRUE))
4023 TAINT_IF(rx->exec_tainted);
4025 && rx->subbase != orig) {
4030 strend = s + (strend - m);
4033 dstr = NEWSV(32, m-s);
4034 sv_setpvn(dstr, s, m-s);
4039 for (i = 1; i <= rx->nparens; i++) {
4043 dstr = NEWSV(33, m-s);
4044 sv_setpvn(dstr, s, m-s);
4047 dstr = NEWSV(33, 0);
4056 LEAVE_SCOPE(oldsave);
4057 iters = (SP - stack_base) - base;
4058 if (iters > maxiters)
4061 /* keep field after final delim? */
4062 if (s < strend || (iters && origlimit)) {
4063 dstr = NEWSV(34, strend-s);
4064 sv_setpvn(dstr, s, strend-s);
4070 else if (!origlimit) {
4071 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0))
4075 SWITCHSTACK(ary, oldstack);
4076 if (gimme == G_ARRAY) {
4078 Copy(AvARRAY(ary), SP + 1, iters, SV*);
4084 if (gimme == G_ARRAY)
4087 if (iters || !pm->op_pmreplroot) {