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;
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 if (ans <= -(UV)IV_MAX)
780 sv_setiv(TARG, (IV) -ans);
782 sv_setnv(TARG, -(double)ans);
793 dSP; dATARGET; tryAMAGICbin(repeat,opASSIGN);
795 register I32 count = POPi;
796 if (GIMME == G_ARRAY && op->op_private & OPpREPEAT_DOLIST) {
798 I32 items = SP - MARK;
810 repeatcpy((char*)(MARK + items), (char*)MARK,
811 items * sizeof(SV*), count - 1);
817 else { /* Note: mark already snarfed by pp_list */
822 if (TARG == tmpstr && SvTHINKFIRST(tmpstr)) {
823 if (SvREADONLY(tmpstr) && curcop != &compiling)
824 DIE("Can't x= to readonly value");
828 SvSetSV(TARG, tmpstr);
829 SvPV_force(TARG, len);
834 SvGROW(TARG, (count * len) + 1);
835 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
836 SvCUR(TARG) *= count;
840 (void)SvPOK_only(TARG);
849 dSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
852 SETn( left - right );
859 dSP; dATARGET; tryAMAGICbin(lshift,opASSIGN);
862 if (op->op_private & HINT_INTEGER) {
878 dSP; dATARGET; tryAMAGICbin(rshift,opASSIGN);
881 if (op->op_private & HINT_INTEGER) {
897 dSP; tryAMAGICbinSET(lt,0);
900 SETs(boolSV(TOPn < value));
907 dSP; tryAMAGICbinSET(gt,0);
910 SETs(boolSV(TOPn > value));
917 dSP; tryAMAGICbinSET(le,0);
920 SETs(boolSV(TOPn <= value));
927 dSP; tryAMAGICbinSET(ge,0);
930 SETs(boolSV(TOPn >= value));
937 dSP; tryAMAGICbinSET(ne,0);
940 SETs(boolSV(TOPn != value));
947 dSP; dTARGET; tryAMAGICbin(ncmp,0);
954 else if (left < right)
956 else if (left > right)
969 dSP; tryAMAGICbinSET(slt,0);
972 int cmp = ((op->op_private & OPpLOCALE)
973 ? sv_cmp_locale(left, right)
974 : sv_cmp(left, right));
975 SETs(boolSV(cmp < 0));
982 dSP; tryAMAGICbinSET(sgt,0);
985 int cmp = ((op->op_private & OPpLOCALE)
986 ? sv_cmp_locale(left, right)
987 : sv_cmp(left, right));
988 SETs(boolSV(cmp > 0));
995 dSP; tryAMAGICbinSET(sle,0);
998 int cmp = ((op->op_private & OPpLOCALE)
999 ? sv_cmp_locale(left, right)
1000 : sv_cmp(left, right));
1001 SETs(boolSV(cmp <= 0));
1008 dSP; tryAMAGICbinSET(sge,0);
1011 int cmp = ((op->op_private & OPpLOCALE)
1012 ? sv_cmp_locale(left, right)
1013 : sv_cmp(left, right));
1014 SETs(boolSV(cmp >= 0));
1021 dSP; tryAMAGICbinSET(seq,0);
1024 SETs(boolSV(sv_eq(left, right)));
1031 dSP; tryAMAGICbinSET(sne,0);
1034 SETs(boolSV(!sv_eq(left, right)));
1041 dSP; dTARGET; tryAMAGICbin(scmp,0);
1044 int cmp = ((op->op_private & OPpLOCALE)
1045 ? sv_cmp_locale(left, right)
1046 : sv_cmp(left, right));
1054 dSP; dATARGET; tryAMAGICbin(band,opASSIGN);
1057 if (SvNIOKp(left) || SvNIOKp(right)) {
1058 if (op->op_private & HINT_INTEGER) {
1059 IBW value = SvIV(left) & SvIV(right);
1063 UBW value = SvUV(left) & SvUV(right);
1068 do_vop(op->op_type, TARG, left, right);
1077 dSP; dATARGET; tryAMAGICbin(bxor,opASSIGN);
1080 if (SvNIOKp(left) || SvNIOKp(right)) {
1081 if (op->op_private & HINT_INTEGER) {
1082 IBW value = (USE_LEFT(left) ? SvIV(left) : 0) ^ SvIV(right);
1086 UBW value = (USE_LEFT(left) ? SvUV(left) : 0) ^ SvUV(right);
1091 do_vop(op->op_type, TARG, left, right);
1100 dSP; dATARGET; tryAMAGICbin(bor,opASSIGN);
1103 if (SvNIOKp(left) || SvNIOKp(right)) {
1104 if (op->op_private & HINT_INTEGER) {
1105 IBW value = (USE_LEFT(left) ? SvIV(left) : 0) | SvIV(right);
1109 UBW value = (USE_LEFT(left) ? SvUV(left) : 0) | SvUV(right);
1114 do_vop(op->op_type, TARG, left, right);
1123 dSP; dTARGET; tryAMAGICun(neg);
1128 if (SvIOKp(sv) && !SvNOKp(sv) && !SvPOKp(sv) && SvIVX(sv) != IV_MIN)
1130 else if (SvNIOKp(sv))
1132 else if (SvPOKp(sv)) {
1134 char *s = SvPV(sv, len);
1135 if (isIDFIRST(*s)) {
1136 sv_setpvn(TARG, "-", 1);
1139 else if (*s == '+' || *s == '-') {
1141 *SvPV_force(TARG, len) = *s == '-' ? '+' : '-';
1144 sv_setnv(TARG, -SvNV(sv));
1156 dSP; tryAMAGICunSET(not);
1157 #endif /* OVERLOAD */
1158 *stack_sp = boolSV(!SvTRUE(*stack_sp));
1164 dSP; dTARGET; tryAMAGICun(compl);
1168 if (op->op_private & HINT_INTEGER) {
1169 IBW value = ~SvIV(sv);
1173 UBW value = ~SvUV(sv);
1178 register char *tmps;
1179 register long *tmpl;
1184 tmps = SvPV_force(TARG, len);
1187 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
1190 for ( ; anum >= sizeof(long); anum -= sizeof(long), tmpl++)
1194 for ( ; anum > 0; anum--, tmps++)
1203 /* integer versions of some of the above */
1207 dSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
1210 SETi( left * right );
1217 dSP; dATARGET; tryAMAGICbin(div,opASSIGN);
1221 DIE("Illegal division by zero");
1222 value = POPi / value;
1230 dSP; dATARGET; tryAMAGICbin(mod,opASSIGN);
1234 DIE("Illegal modulus zero");
1235 SETi( left % right );
1242 dSP; dATARGET; tryAMAGICbin(add,opASSIGN);
1245 SETi( left + right );
1252 dSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
1255 SETi( left - right );
1262 dSP; tryAMAGICbinSET(lt,0);
1265 SETs(boolSV(left < right));
1272 dSP; tryAMAGICbinSET(gt,0);
1275 SETs(boolSV(left > right));
1282 dSP; tryAMAGICbinSET(le,0);
1285 SETs(boolSV(left <= right));
1292 dSP; tryAMAGICbinSET(ge,0);
1295 SETs(boolSV(left >= right));
1302 dSP; tryAMAGICbinSET(eq,0);
1305 SETs(boolSV(left == right));
1312 dSP; tryAMAGICbinSET(ne,0);
1315 SETs(boolSV(left != right));
1322 dSP; dTARGET; tryAMAGICbin(ncmp,0);
1329 else if (left < right)
1340 dSP; dTARGET; tryAMAGICun(neg);
1345 /* High falutin' math. */
1349 dSP; dTARGET; tryAMAGICbin(atan2,0);
1352 SETn(atan2(left, right));
1359 dSP; dTARGET; tryAMAGICun(sin);
1371 dSP; dTARGET; tryAMAGICun(cos);
1391 if (!srand_called) {
1392 (void)srand((unsigned)seed());
1393 srand_called = TRUE;
1396 value = rand() * value / 2147483648.0;
1399 value = rand() * value / 65536.0;
1402 value = rand() * value / 32768.0;
1404 value = rand() * value / (double)(((unsigned long)1) << RANDBITS);
1420 (void)srand((unsigned)anum);
1421 srand_called = TRUE;
1430 * This is really just a quick hack which grabs various garbage
1431 * values. It really should be a real hash algorithm which
1432 * spreads the effect of every input bit onto every output bit,
1433 * if someone who knows about such tings would bother to write it.
1434 * Might be a good idea to add that function to CORE as well.
1435 * No numbers below come from careful analysis or anyting here,
1436 * except they are primes and SEED_C1 > 1E6 to get a full-width
1437 * value from (tv_sec * SEED_C1 + tv_usec). The multipliers should
1438 * probably be bigger too.
1441 # define SEED_C1 1000003
1442 #define SEED_C4 73819
1444 # define SEED_C1 25747
1445 #define SEED_C4 20639
1449 #define SEED_C5 26107
1453 # include <starlet.h>
1454 /* when[] = (low 32 bits, high 32 bits) of time since epoch
1455 * in 100-ns units, typically incremented ever 10 ms. */
1456 unsigned int when[2];
1457 _ckvmssts(sys$gettim(when));
1458 u = (U32)SEED_C1 * when[0] + (U32)SEED_C2 * when[1];
1460 # ifdef HAS_GETTIMEOFDAY
1461 struct timeval when;
1462 gettimeofday(&when,(struct timezone *) 0);
1463 u = (U32)SEED_C1 * when.tv_sec + (U32)SEED_C2 * when.tv_usec;
1467 u = (U32)SEED_C1 * when;
1470 u += SEED_C3 * (U32)getpid();
1471 u += SEED_C4 * (U32)(UV)stack_sp;
1472 #ifndef PLAN9 /* XXX Plan9 assembler chokes on this; fix needed */
1473 u += SEED_C5 * (U32)(UV)&when;
1480 dSP; dTARGET; tryAMAGICun(exp);
1492 dSP; dTARGET; tryAMAGICun(log);
1497 SET_NUMERIC_STANDARD();
1498 DIE("Can't take log of %g", value);
1508 dSP; dTARGET; tryAMAGICun(sqrt);
1513 SET_NUMERIC_STANDARD();
1514 DIE("Can't take sqrt of %g", value);
1516 value = sqrt(value);
1526 double value = TOPn;
1529 if (SvIOKp(TOPs) && !SvNOKp(TOPs) && !SvPOKp(TOPs)) {
1535 (void)modf(value, &value);
1537 (void)modf(-value, &value);
1552 dSP; dTARGET; tryAMAGICun(abs);
1554 double value = TOPn;
1557 if (SvIOKp(TOPs) && !SvNOKp(TOPs) && !SvPOKp(TOPs) &&
1558 (iv = SvIVX(TOPs)) != IV_MIN) {
1579 XPUSHu(scan_hex(tmps, 99, &argtype));
1591 while (*tmps && isSPACE(*tmps))
1596 value = scan_hex(++tmps, 99, &argtype);
1598 value = scan_oct(tmps, 99, &argtype);
1608 SETi( sv_len(TOPs) );
1620 I32 lvalue = op->op_flags & OPf_MOD;
1622 I32 arybase = curcop->cop_arybase;
1626 pos = POPi - arybase;
1628 tmps = SvPV(sv, curlen);
1630 pos += curlen + arybase;
1631 if (pos < 0 && MAXARG < 3)
1634 if (pos < 0 || pos > curlen) {
1635 if (dowarn || lvalue)
1636 warn("substr outside of string");
1643 len += curlen - pos;
1648 rem = curlen - pos; /* rem=how many bytes left*/
1651 sv_setpvn(TARG, tmps, rem);
1652 if (lvalue) { /* it's an lvalue! */
1653 if (!SvGMAGICAL(sv)) {
1657 warn("Attempt to use reference as lvalue in substr");
1659 if (SvOK(sv)) /* is it defined ? */
1660 (void)SvPOK_only(sv);
1662 sv_setpvn(sv,"",0); /* avoid lexical reincarnation */
1665 if (SvTYPE(TARG) < SVt_PVLV) {
1666 sv_upgrade(TARG, SVt_PVLV);
1667 sv_magic(TARG, Nullsv, 'x', Nullch, 0);
1672 LvTARGOFF(TARG) = pos;
1673 LvTARGLEN(TARG) = rem;
1676 PUSHs(TARG); /* avoid SvSETMAGIC here */
1683 register I32 size = POPi;
1684 register I32 offset = POPi;
1685 register SV *src = POPs;
1686 I32 lvalue = op->op_flags & OPf_MOD;
1688 unsigned char *s = (unsigned char*)SvPV(src, srclen);
1689 unsigned long retnum;
1692 offset *= size; /* turn into bit offset */
1693 len = (offset + size + 7) / 8;
1694 if (offset < 0 || size < 1)
1697 if (lvalue) { /* it's an lvalue! */
1698 if (SvTYPE(TARG) < SVt_PVLV) {
1699 sv_upgrade(TARG, SVt_PVLV);
1700 sv_magic(TARG, Nullsv, 'v', Nullch, 0);
1705 LvTARGOFF(TARG) = offset;
1706 LvTARGLEN(TARG) = size;
1714 if (offset >= srclen)
1717 retnum = (unsigned long) s[offset] << 8;
1719 else if (size == 32) {
1720 if (offset >= srclen)
1722 else if (offset + 1 >= srclen)
1723 retnum = (unsigned long) s[offset] << 24;
1724 else if (offset + 2 >= srclen)
1725 retnum = ((unsigned long) s[offset] << 24) +
1726 ((unsigned long) s[offset + 1] << 16);
1728 retnum = ((unsigned long) s[offset] << 24) +
1729 ((unsigned long) s[offset + 1] << 16) +
1730 (s[offset + 2] << 8);
1735 retnum = (s[offset >> 3] >> (offset & 7)) & ((1 << size) - 1);
1740 else if (size == 16)
1741 retnum = ((unsigned long) s[offset] << 8) + s[offset+1];
1742 else if (size == 32)
1743 retnum = ((unsigned long) s[offset] << 24) +
1744 ((unsigned long) s[offset + 1] << 16) +
1745 (s[offset + 2] << 8) + s[offset+3];
1749 sv_setiv(TARG, (IV)retnum);
1764 I32 arybase = curcop->cop_arybase;
1769 offset = POPi - arybase;
1772 tmps = SvPV(big, biglen);
1775 else if (offset > biglen)
1777 if (!(tmps2 = fbm_instr((unsigned char*)tmps + offset,
1778 (unsigned char*)tmps + biglen, little)))
1779 retval = -1 + arybase;
1781 retval = tmps2 - tmps + arybase;
1798 I32 arybase = curcop->cop_arybase;
1804 tmps2 = SvPV(little, llen);
1805 tmps = SvPV(big, blen);
1809 offset = SvIV(offstr) - arybase + llen;
1812 else if (offset > blen)
1814 if (!(tmps2 = rninstr(tmps, tmps + offset,
1815 tmps2, tmps2 + llen)))
1816 retval = -1 + arybase;
1818 retval = tmps2 - tmps + arybase;
1825 dSP; dMARK; dORIGMARK; dTARGET;
1826 #ifdef USE_LOCALE_NUMERIC
1827 if (op->op_private & OPpLOCALE)
1828 SET_NUMERIC_LOCAL();
1830 SET_NUMERIC_STANDARD();
1832 do_sprintf(TARG, SP-MARK, MARK+1);
1833 TAINT_IF(SvTAINTED(TARG));
1847 value = (I32) (*tmps & 255);
1852 value = (I32) (anum & 255);
1863 (void)SvUPGRADE(TARG,SVt_PV);
1869 (void)SvPOK_only(TARG);
1876 dSP; dTARGET; dPOPTOPssrl;
1878 char *tmps = SvPV(left, na);
1880 sv_setpv(TARG, fcrypt(tmps, SvPV(right, na)));
1882 sv_setpv(TARG, crypt(tmps, SvPV(right, na)));
1886 "The crypt() function is unimplemented due to excessive paranoia.");
1898 if (!SvPADTMP(sv)) {
1904 s = SvPV_force(sv, na);
1906 if (op->op_private & OPpLOCALE) {
1909 *s = toUPPER_LC(*s);
1924 if (!SvPADTMP(sv)) {
1930 s = SvPV_force(sv, na);
1932 if (op->op_private & OPpLOCALE) {
1935 *s = toLOWER_LC(*s);
1952 if (!SvPADTMP(sv)) {
1959 s = SvPV_force(sv, len);
1961 register char *send = s + len;
1963 if (op->op_private & OPpLOCALE) {
1966 for (; s < send; s++)
1967 *s = toUPPER_LC(*s);
1970 for (; s < send; s++)
1984 if (!SvPADTMP(sv)) {
1991 s = SvPV_force(sv, len);
1993 register char *send = s + len;
1995 if (op->op_private & OPpLOCALE) {
1998 for (; s < send; s++)
1999 *s = toLOWER_LC(*s);
2002 for (; s < send; s++)
2014 register char *s = SvPV(sv,len);
2018 (void)SvUPGRADE(TARG, SVt_PV);
2019 SvGROW(TARG, (len * 2) + 1);
2027 SvCUR_set(TARG, d - SvPVX(TARG));
2028 (void)SvPOK_only(TARG);
2031 sv_setpvn(TARG, s, len);
2040 dSP; dMARK; dORIGMARK;
2042 register AV* av = (AV*)POPs;
2043 register I32 lval = op->op_flags & OPf_MOD;
2044 I32 arybase = curcop->cop_arybase;
2047 if (SvTYPE(av) == SVt_PVAV) {
2048 if (lval && op->op_private & OPpLVAL_INTRO) {
2050 for (svp = mark + 1; svp <= sp; svp++) {
2055 if (max > AvMAX(av))
2058 while (++MARK <= SP) {
2059 elem = SvIVx(*MARK);
2063 svp = av_fetch(av, elem, lval);
2065 if (!svp || *svp == &sv_undef)
2066 DIE(no_aelem, elem);
2067 if (op->op_private & OPpLVAL_INTRO)
2070 *MARK = svp ? *svp : &sv_undef;
2073 if (GIMME != G_ARRAY) {
2081 /* Associative arrays. */
2086 HV *hash = (HV*)POPs;
2088 I32 gimme = GIMME_V;
2091 entry = hv_iternext(hash); /* might clobber stack_sp */
2096 PUSHs(hv_iterkeysv(entry)); /* won't clobber stack_sp */
2097 if (gimme == G_ARRAY) {
2099 sv_setsv(TARG, hv_iterval(hash, entry)); /* might hit stack_sp */
2104 else if (gimme == G_SCALAR)
2123 I32 gimme = GIMME_V;
2124 I32 discard = (gimme == G_VOID) ? G_DISCARD : 0;
2128 if (op->op_private & OPpSLICE) {
2131 if (SvTYPE(hv) != SVt_PVHV)
2132 DIE("Not a HASH reference");
2133 while (++MARK <= SP) {
2134 sv = hv_delete_ent(hv, *MARK, discard, 0);
2135 *MARK = sv ? sv : &sv_undef;
2139 else if (gimme == G_SCALAR) {
2148 if (SvTYPE(hv) != SVt_PVHV)
2149 DIE("Not a HASH reference");
2150 sv = hv_delete_ent(hv, keysv, discard, 0);
2165 if (SvTYPE(hv) != SVt_PVHV) {
2166 DIE("Not a HASH reference");
2168 if (hv_exists_ent(hv, tmpsv, 0))
2175 dSP; dMARK; dORIGMARK;
2177 register HV *hv = (HV*)POPs;
2178 register I32 lval = op->op_flags & OPf_MOD;
2180 if (SvTYPE(hv) == SVt_PVHV) {
2181 while (++MARK <= SP) {
2184 he = hv_fetch_ent(hv, keysv, lval, 0);
2186 if (!he || HeVAL(he) == &sv_undef)
2187 DIE(no_helem, SvPV(keysv, na));
2188 if (op->op_private & OPpLVAL_INTRO)
2189 save_svref(&HeVAL(he));
2191 *MARK = he ? HeVAL(he) : &sv_undef;
2194 if (GIMME != G_ARRAY) {
2202 /* List operators. */
2207 if (GIMME != G_ARRAY) {
2209 *MARK = *SP; /* unwanted list, return last item */
2220 SV **lastrelem = stack_sp;
2221 SV **lastlelem = stack_base + POPMARK;
2222 SV **firstlelem = stack_base + POPMARK + 1;
2223 register SV **firstrelem = lastlelem + 1;
2224 I32 arybase = curcop->cop_arybase;
2225 I32 lval = op->op_flags & OPf_MOD;
2226 I32 is_something_there = lval;
2228 register I32 max = lastrelem - lastlelem;
2229 register SV **lelem;
2232 if (GIMME != G_ARRAY) {
2233 ix = SvIVx(*lastlelem);
2238 if (ix < 0 || ix >= max)
2239 *firstlelem = &sv_undef;
2241 *firstlelem = firstrelem[ix];
2247 SP = firstlelem - 1;
2251 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
2257 else if (!(*lelem = firstrelem[ix]))
2262 if (ix >= max || !(*lelem = firstrelem[ix]))
2265 if (!is_something_there && (SvOK(*lelem) || SvGMAGICAL(*lelem)))
2266 is_something_there = TRUE;
2268 if (is_something_there)
2271 SP = firstlelem - 1;
2277 dSP; dMARK; dORIGMARK;
2278 I32 items = SP - MARK;
2279 SV *av = sv_2mortal((SV*)av_make(items, MARK+1));
2280 SP = ORIGMARK; /* av_make() might realloc stack_sp */
2287 dSP; dMARK; dORIGMARK;
2288 HV* hv = (HV*)sv_2mortal((SV*)newHV());
2292 SV *val = NEWSV(46, 0);
2294 sv_setsv(val, *++MARK);
2296 warn("Odd number of elements in hash list");
2297 (void)hv_store_ent(hv,key,val,0);
2306 dSP; dMARK; dORIGMARK;
2307 register AV *ary = (AV*)*++MARK;
2311 register I32 offset;
2312 register I32 length;
2321 offset = SvIVx(*MARK);
2323 offset += AvFILL(ary) + 1;
2325 offset -= curcop->cop_arybase;
2327 length = SvIVx(*MARK++);
2332 length = AvMAX(ary) + 1; /* close enough to infinity */
2336 length = AvMAX(ary) + 1;
2344 if (offset > AvFILL(ary) + 1)
2345 offset = AvFILL(ary) + 1;
2346 after = AvFILL(ary) + 1 - (offset + length);
2347 if (after < 0) { /* not that much array */
2348 length += after; /* offset+length now in array */
2354 /* At this point, MARK .. SP-1 is our new LIST */
2357 diff = newlen - length;
2359 if (diff < 0) { /* shrinking the area */
2361 New(451, tmparyval, newlen, SV*); /* so remember insertion */
2362 Copy(MARK, tmparyval, newlen, SV*);
2365 MARK = ORIGMARK + 1;
2366 if (GIMME == G_ARRAY) { /* copy return vals to stack */
2367 MEXTEND(MARK, length);
2368 Copy(AvARRAY(ary)+offset, MARK, length, SV*);
2370 EXTEND_MORTAL(length);
2371 for (i = length, dst = MARK; i; i--) {
2372 if (!SvIMMORTAL(*dst))
2373 sv_2mortal(*dst); /* free them eventualy */
2380 *MARK = AvARRAY(ary)[offset+length-1];
2382 if (!SvIMMORTAL(*MARK))
2384 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
2385 SvREFCNT_dec(*dst++); /* free them now */
2388 AvFILL(ary) += diff;
2390 /* pull up or down? */
2392 if (offset < after) { /* easier to pull up */
2393 if (offset) { /* esp. if nothing to pull */
2394 src = &AvARRAY(ary)[offset-1];
2395 dst = src - diff; /* diff is negative */
2396 for (i = offset; i > 0; i--) /* can't trust Copy */
2400 SvPVX(ary) = (char*)(AvARRAY(ary) - diff); /* diff is negative */
2404 if (after) { /* anything to pull down? */
2405 src = AvARRAY(ary) + offset + length;
2406 dst = src + diff; /* diff is negative */
2407 Move(src, dst, after, SV*);
2409 dst = &AvARRAY(ary)[AvFILL(ary)+1];
2410 /* avoid later double free */
2414 dst[--i] = &sv_undef;
2417 for (src = tmparyval, dst = AvARRAY(ary) + offset;
2419 *dst = NEWSV(46, 0);
2420 sv_setsv(*dst++, *src++);
2422 Safefree(tmparyval);
2425 else { /* no, expanding (or same) */
2427 New(452, tmparyval, length, SV*); /* so remember deletion */
2428 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
2431 if (diff > 0) { /* expanding */
2433 /* push up or down? */
2435 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
2439 Move(src, dst, offset, SV*);
2441 SvPVX(ary) = (char*)(AvARRAY(ary) - diff);/* diff is positive */
2443 AvFILL(ary) += diff;
2446 if (AvFILL(ary) + diff >= AvMAX(ary)) /* oh, well */
2447 av_extend(ary, AvFILL(ary) + diff);
2448 AvFILL(ary) += diff;
2451 dst = AvARRAY(ary) + AvFILL(ary);
2453 for (i = after; i; i--) {
2460 for (src = MARK, dst = AvARRAY(ary) + offset; newlen; newlen--) {
2461 *dst = NEWSV(46, 0);
2462 sv_setsv(*dst++, *src++);
2464 MARK = ORIGMARK + 1;
2465 if (GIMME == G_ARRAY) { /* copy return vals to stack */
2467 Copy(tmparyval, MARK, length, SV*);
2469 EXTEND_MORTAL(length);
2470 for (i = length, dst = MARK; i; i--) {
2471 if (!SvIMMORTAL(*dst))
2472 sv_2mortal(*dst); /* free them eventualy */
2476 Safefree(tmparyval);
2480 else if (length--) {
2481 *MARK = tmparyval[length];
2483 if (!SvIMMORTAL(*MARK))
2485 while (length-- > 0)
2486 SvREFCNT_dec(tmparyval[length]);
2488 Safefree(tmparyval);
2499 dSP; dMARK; dORIGMARK; dTARGET;
2500 register AV *ary = (AV*)*++MARK;
2501 register SV *sv = &sv_undef;
2503 for (++MARK; MARK <= SP; MARK++) {
2506 sv_setsv(sv, *MARK);
2510 PUSHi( AvFILL(ary) + 1 );
2518 SV *sv = av_pop(av);
2519 if (!SvIMMORTAL(sv) && AvREAL(av))
2520 (void)sv_2mortal(sv);
2529 SV *sv = av_shift(av);
2533 if (!SvIMMORTAL(sv) && AvREAL(av))
2534 (void)sv_2mortal(sv);
2541 dSP; dMARK; dORIGMARK; dTARGET;
2542 register AV *ary = (AV*)*++MARK;
2546 av_unshift(ary, SP - MARK);
2549 sv_setsv(sv, *++MARK);
2550 (void)av_store(ary, i++, sv);
2554 PUSHi( AvFILL(ary) + 1 );
2564 if (GIMME == G_ARRAY) {
2575 register char *down;
2581 do_join(TARG, &sv_no, MARK, SP);
2583 sv_setsv(TARG, (SP > MARK) ? *SP : GvSV(defgv));
2584 up = SvPV_force(TARG, len);
2586 down = SvPVX(TARG) + len - 1;
2592 (void)SvPOK_only(TARG);
2606 char *s = SvPV(sv, len);
2610 if (!strnEQ(s, "0000", 4)) { /* need to grow sv */
2611 SV *new = newSVpv("0000000000", 10);
2614 SvREFCNT_dec(sv); /* free old sv */
2619 while (!*t) /* trailing '\0'? */
2622 i = ((*t - '0') << 7) + m;
2623 *(t--) = '0' + (i % 10);
2629 /* Explosives and implosives. */
2636 I32 gimme = GIMME_V;
2640 register char *pat = SvPV(left, llen);
2641 register char *s = SvPV(right, rlen);
2642 char *strend = s + rlen;
2644 register char *patend = pat + llen;
2649 /* These must not be in registers: */
2660 unsigned Quad_t auquad;
2666 register U32 culong;
2668 static char* bitcount = 0;
2670 if (gimme != G_ARRAY) { /* arrange to do first one only */
2672 for (patend = pat; !isALPHA(*patend) || *patend == 'x'; patend++) ;
2673 if (strchr("aAbBhHP", *patend) || *pat == '%') {
2675 while (isDIGIT(*patend) || *patend == '*')
2681 while (pat < patend) {
2683 datumtype = *pat++ & 0xFF;
2684 if (isSPACE(datumtype))
2688 else if (*pat == '*') {
2689 len = strend - strbeg; /* long enough */
2692 else if (isDIGIT(*pat)) {
2694 while (isDIGIT(*pat))
2695 len = (len * 10) + (*pat++ - '0');
2698 len = (datumtype != '@');
2701 croak("Invalid type in unpack: '%c'", (int)datumtype);
2703 if (len == 1 && pat[-1] != '1')
2712 if (len > strend - strbeg)
2713 DIE("@ outside of string");
2717 if (len > s - strbeg)
2718 DIE("X outside of string");
2722 if (len > strend - s)
2723 DIE("x outside of string");
2728 if (len > strend - s)
2731 goto uchar_checksum;
2732 sv = NEWSV(35, len);
2733 sv_setpvn(sv, s, len);
2735 if (datumtype == 'A') {
2736 aptr = s; /* borrow register */
2737 s = SvPVX(sv) + len - 1;
2738 while (s >= SvPVX(sv) && (!*s || isSPACE(*s)))
2741 SvCUR_set(sv, s - SvPVX(sv));
2742 s = aptr; /* unborrow register */
2744 XPUSHs(sv_2mortal(sv));
2748 if (pat[-1] == '*' || len > (strend - s) * 8)
2749 len = (strend - s) * 8;
2752 Newz(601, bitcount, 256, char);
2753 for (bits = 1; bits < 256; bits++) {
2754 if (bits & 1) bitcount[bits]++;
2755 if (bits & 2) bitcount[bits]++;
2756 if (bits & 4) bitcount[bits]++;
2757 if (bits & 8) bitcount[bits]++;
2758 if (bits & 16) bitcount[bits]++;
2759 if (bits & 32) bitcount[bits]++;
2760 if (bits & 64) bitcount[bits]++;
2761 if (bits & 128) bitcount[bits]++;
2765 culong += bitcount[*(unsigned char*)s++];
2770 if (datumtype == 'b') {
2772 if (bits & 1) culong++;
2778 if (bits & 128) culong++;
2785 sv = NEWSV(35, len + 1);
2788 aptr = pat; /* borrow register */
2790 if (datumtype == 'b') {
2792 for (len = 0; len < aint; len++) {
2793 if (len & 7) /*SUPPRESS 595*/
2797 *pat++ = '0' + (bits & 1);
2802 for (len = 0; len < aint; len++) {
2807 *pat++ = '0' + ((bits & 128) != 0);
2811 pat = aptr; /* unborrow register */
2812 XPUSHs(sv_2mortal(sv));
2816 if (pat[-1] == '*' || len > (strend - s) * 2)
2817 len = (strend - s) * 2;
2818 sv = NEWSV(35, len + 1);
2821 aptr = pat; /* borrow register */
2823 if (datumtype == 'h') {
2825 for (len = 0; len < aint; len++) {
2830 *pat++ = hexdigit[bits & 15];
2835 for (len = 0; len < aint; len++) {
2840 *pat++ = hexdigit[(bits >> 4) & 15];
2844 pat = aptr; /* unborrow register */
2845 XPUSHs(sv_2mortal(sv));
2848 if (len > strend - s)
2853 if (aint >= 128) /* fake up signed chars */
2863 if (aint >= 128) /* fake up signed chars */
2866 sv_setiv(sv, (IV)aint);
2867 PUSHs(sv_2mortal(sv));
2872 if (len > strend - s)
2887 sv_setiv(sv, (IV)auint);
2888 PUSHs(sv_2mortal(sv));
2893 along = (strend - s) / SIZE16;
2910 sv_setiv(sv, (IV)ashort);
2911 PUSHs(sv_2mortal(sv));
2918 along = (strend - s) / SIZE16;
2923 COPY16(s, &aushort);
2926 if (datumtype == 'n')
2927 aushort = ntohs(aushort);
2930 if (datumtype == 'v')
2931 aushort = vtohs(aushort);
2940 COPY16(s, &aushort);
2944 if (datumtype == 'n')
2945 aushort = ntohs(aushort);
2948 if (datumtype == 'v')
2949 aushort = vtohs(aushort);
2951 sv_setiv(sv, (IV)aushort);
2952 PUSHs(sv_2mortal(sv));
2957 along = (strend - s) / sizeof(int);
2962 Copy(s, &aint, 1, int);
2965 cdouble += (double)aint;
2974 Copy(s, &aint, 1, int);
2977 sv_setiv(sv, (IV)aint);
2978 PUSHs(sv_2mortal(sv));
2983 along = (strend - s) / sizeof(unsigned int);
2988 Copy(s, &auint, 1, unsigned int);
2989 s += sizeof(unsigned int);
2991 cdouble += (double)auint;
3000 Copy(s, &auint, 1, unsigned int);
3001 s += sizeof(unsigned int);
3003 sv_setuv(sv, (UV)auint);
3004 PUSHs(sv_2mortal(sv));
3009 along = (strend - s) / SIZE32;
3017 cdouble += (double)along;
3029 sv_setiv(sv, (IV)along);
3030 PUSHs(sv_2mortal(sv));
3037 along = (strend - s) / SIZE32;
3045 if (datumtype == 'N')
3046 aulong = ntohl(aulong);
3049 if (datumtype == 'V')
3050 aulong = vtohl(aulong);
3053 cdouble += (double)aulong;
3065 if (datumtype == 'N')
3066 aulong = ntohl(aulong);
3069 if (datumtype == 'V')
3070 aulong = vtohl(aulong);
3073 sv_setuv(sv, (UV)aulong);
3074 PUSHs(sv_2mortal(sv));
3079 along = (strend - s) / sizeof(char*);
3085 if (sizeof(char*) > strend - s)
3088 Copy(s, &aptr, 1, char*);
3094 PUSHs(sv_2mortal(sv));
3104 while ((len > 0) && (s < strend)) {
3105 auv = (auv << 7) | (*s & 0x7f);
3106 if (!(*s++ & 0x80)) {
3110 PUSHs(sv_2mortal(sv));
3114 else if (++bytes >= sizeof(UV)) { /* promote to string */
3117 sv = newSVpvf("%.*Vu", (int)TYPE_DIGITS(UV), auv);
3118 while (s < strend) {
3119 sv = mul128(sv, *s & 0x7f);
3120 if (!(*s++ & 0x80)) {
3129 PUSHs(sv_2mortal(sv));
3134 if ((s >= strend) && bytes)
3135 croak("Unterminated compressed integer");
3140 if (sizeof(char*) > strend - s)
3143 Copy(s, &aptr, 1, char*);
3148 sv_setpvn(sv, aptr, len);
3149 PUSHs(sv_2mortal(sv));
3156 if (s + sizeof(Quad_t) > strend)
3159 Copy(s, &aquad, 1, Quad_t);
3160 s += sizeof(Quad_t);
3163 if (aquad >= IV_MIN && aquad <= IV_MAX)
3164 sv_setiv(sv, (IV)aquad);
3166 sv_setnv(sv, (double)aquad);
3167 PUSHs(sv_2mortal(sv));
3174 if (s + sizeof(unsigned Quad_t) > strend)
3177 Copy(s, &auquad, 1, unsigned Quad_t);
3178 s += sizeof(unsigned Quad_t);
3181 if (aquad <= UV_MAX)
3182 sv_setuv(sv, (UV)auquad);
3184 sv_setnv(sv, (double)auquad);
3185 PUSHs(sv_2mortal(sv));
3189 /* float and double added gnb@melba.bby.oz.au 22/11/89 */
3192 along = (strend - s) / sizeof(float);
3197 Copy(s, &afloat, 1, float);
3206 Copy(s, &afloat, 1, float);
3209 sv_setnv(sv, (double)afloat);
3210 PUSHs(sv_2mortal(sv));
3216 along = (strend - s) / sizeof(double);
3221 Copy(s, &adouble, 1, double);
3222 s += sizeof(double);
3230 Copy(s, &adouble, 1, double);
3231 s += sizeof(double);
3233 sv_setnv(sv, (double)adouble);
3234 PUSHs(sv_2mortal(sv));
3239 along = (strend - s) * 3 / 4;
3240 sv = NEWSV(42, along);
3243 while (s < strend && *s > ' ' && *s < 'a') {
3248 len = (*s++ - ' ') & 077;
3250 if (s < strend && *s >= ' ')
3251 a = (*s++ - ' ') & 077;
3254 if (s < strend && *s >= ' ')
3255 b = (*s++ - ' ') & 077;
3258 if (s < strend && *s >= ' ')
3259 c = (*s++ - ' ') & 077;
3262 if (s < strend && *s >= ' ')
3263 d = (*s++ - ' ') & 077;
3266 hunk[0] = a << 2 | b >> 4;
3267 hunk[1] = b << 4 | c >> 2;
3268 hunk[2] = c << 6 | d;
3269 sv_catpvn(sv, hunk, len > 3 ? 3 : len);
3274 else if (s[1] == '\n') /* possible checksum byte */
3277 XPUSHs(sv_2mortal(sv));
3282 if (strchr("fFdD", datumtype) ||
3283 (checksum > 32 && strchr("iIlLN", datumtype)) ) {
3287 while (checksum >= 16) {
3291 while (checksum >= 4) {
3297 along = (1 << checksum) - 1;
3298 while (cdouble < 0.0)
3300 cdouble = modf(cdouble / adouble, &trouble) * adouble;
3301 sv_setnv(sv, cdouble);
3304 if (checksum < 32) {
3305 aulong = (1 << checksum) - 1;
3308 sv_setuv(sv, (UV)culong);
3310 XPUSHs(sv_2mortal(sv));
3314 if (sp == oldsp && gimme == G_SCALAR)
3320 doencodes(sv, s, len)
3328 sv_catpvn(sv, hunk, 1);
3331 hunk[0] = ' ' + (077 & (*s >> 2));
3332 hunk[1] = ' ' + (077 & ((*s << 4) & 060 | (s[1] >> 4) & 017));
3333 hunk[2] = ' ' + (077 & ((s[1] << 2) & 074 | (s[2] >> 6) & 03));
3334 hunk[3] = ' ' + (077 & (s[2] & 077));
3335 sv_catpvn(sv, hunk, 4);
3339 for (s = SvPVX(sv); *s; s++) {
3343 sv_catpvn(sv, "\n", 1);
3351 SV *result = newSVpv("", l);
3352 char *result_c = SvPV(result, na); /* convenience */
3353 char *out = result_c;
3363 SvREFCNT_dec(result);
3386 SvREFCNT_dec(result);
3392 SvCUR_set(result, out - result_c);
3398 SV *pnum; /* must be '\0' terminated */
3402 char *s = SvPV(pnum, len);
3411 i = m * 10 + (*t - '0');
3413 r = (i >> 7); /* r < 10 */
3420 SvCUR_set(pnum, (STRLEN) (t - s));
3427 dSP; dMARK; dORIGMARK; dTARGET;
3428 register SV *cat = TARG;
3431 register char *pat = SvPVx(*++MARK, fromlen);
3432 register char *patend = pat + fromlen;
3437 static char null10[] = {0,0,0,0,0,0,0,0,0,0};
3438 static char *space10 = " ";
3440 /* These must not be in registers: */
3449 unsigned Quad_t auquad;
3457 sv_setpvn(cat, "", 0);
3458 while (pat < patend) {
3459 #define NEXTFROM (items-- > 0 ? *MARK++ : &sv_no)
3460 datumtype = *pat++ & 0xFF;
3461 if (isSPACE(datumtype))
3464 len = strchr("@Xxu", datumtype) ? 0 : items;
3467 else if (isDIGIT(*pat)) {
3469 while (isDIGIT(*pat))
3470 len = (len * 10) + (*pat++ - '0');
3476 croak("Invalid type in pack: '%c'", (int)datumtype);
3478 DIE("%% may only be used in unpack");
3489 if (SvCUR(cat) < len)
3490 DIE("X outside of string");
3497 sv_catpvn(cat, null10, 10);
3500 sv_catpvn(cat, null10, len);
3505 aptr = SvPV(fromstr, fromlen);
3509 sv_catpvn(cat, aptr, len);
3511 sv_catpvn(cat, aptr, fromlen);
3513 if (datumtype == 'A') {
3515 sv_catpvn(cat, space10, 10);
3518 sv_catpvn(cat, space10, len);
3522 sv_catpvn(cat, null10, 10);
3525 sv_catpvn(cat, null10, len);
3532 char *savepat = pat;
3537 aptr = SvPV(fromstr, fromlen);
3542 SvCUR(cat) += (len+7)/8;
3543 SvGROW(cat, SvCUR(cat) + 1);
3544 aptr = SvPVX(cat) + aint;
3549 if (datumtype == 'B') {
3550 for (len = 0; len++ < aint;) {
3551 items |= *pat++ & 1;
3555 *aptr++ = items & 0xff;
3561 for (len = 0; len++ < aint;) {
3567 *aptr++ = items & 0xff;
3573 if (datumtype == 'B')
3574 items <<= 7 - (aint & 7);
3576 items >>= 7 - (aint & 7);
3577 *aptr++ = items & 0xff;
3579 pat = SvPVX(cat) + SvCUR(cat);
3590 char *savepat = pat;
3595 aptr = SvPV(fromstr, fromlen);
3600 SvCUR(cat) += (len+1)/2;
3601 SvGROW(cat, SvCUR(cat) + 1);
3602 aptr = SvPVX(cat) + aint;
3607 if (datumtype == 'H') {
3608 for (len = 0; len++ < aint;) {
3610 items |= ((*pat++ & 15) + 9) & 15;
3612 items |= *pat++ & 15;
3616 *aptr++ = items & 0xff;
3622 for (len = 0; len++ < aint;) {
3624 items |= (((*pat++ & 15) + 9) & 15) << 4;
3626 items |= (*pat++ & 15) << 4;
3630 *aptr++ = items & 0xff;
3636 *aptr++ = items & 0xff;
3637 pat = SvPVX(cat) + SvCUR(cat);
3649 aint = SvIV(fromstr);
3651 sv_catpvn(cat, &achar, sizeof(char));
3654 /* Float and double added by gnb@melba.bby.oz.au 22/11/89 */
3659 afloat = (float)SvNV(fromstr);
3660 sv_catpvn(cat, (char *)&afloat, sizeof (float));
3667 adouble = (double)SvNV(fromstr);
3668 sv_catpvn(cat, (char *)&adouble, sizeof (double));
3674 ashort = (I16)SvIV(fromstr);
3676 ashort = htons(ashort);
3678 CAT16(cat, &ashort);
3684 ashort = (I16)SvIV(fromstr);
3686 ashort = htovs(ashort);
3688 CAT16(cat, &ashort);
3695 ashort = (I16)SvIV(fromstr);
3696 CAT16(cat, &ashort);
3702 auint = SvUV(fromstr);
3703 sv_catpvn(cat, (char*)&auint, sizeof(unsigned int));
3709 adouble = floor(SvNV(fromstr));
3712 croak("Cannot compress negative numbers");
3722 char buf[1 + sizeof(UV)];
3723 char *in = buf + sizeof(buf);
3724 UV auv = U_V(adouble);;
3727 *--in = (auv & 0x7f) | 0x80;
3730 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
3731 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
3733 else if (SvPOKp(fromstr)) { /* decimal string arithmetics */
3734 char *from, *result, *in;
3739 /* Copy string and check for compliance */
3740 from = SvPV(fromstr, len);
3741 if ((norm = is_an_int(from, len)) == NULL)
3742 croak("can compress only unsigned integer");
3744 New('w', result, len, char);
3748 *--in = div128(norm, &done) | 0x80;
3749 result[len - 1] &= 0x7F; /* clear continue bit */
3750 sv_catpvn(cat, in, (result + len) - in);
3752 SvREFCNT_dec(norm); /* free norm */
3754 else if (SvNOKp(fromstr)) {
3755 char buf[sizeof(double) * 2]; /* 8/7 <= 2 */
3756 char *in = buf + sizeof(buf);
3759 double next = floor(adouble / 128);
3760 *--in = (unsigned char)(adouble - (next * 128)) | 0x80;
3761 if (--in < buf) /* this cannot happen ;-) */
3762 croak ("Cannot compress integer");
3764 } while (adouble > 0);
3765 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
3766 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
3769 croak("Cannot compress non integer");
3775 aint = SvIV(fromstr);
3776 sv_catpvn(cat, (char*)&aint, sizeof(int));
3782 aulong = SvUV(fromstr);
3784 aulong = htonl(aulong);
3786 CAT32(cat, &aulong);
3792 aulong = SvUV(fromstr);
3794 aulong = htovl(aulong);
3796 CAT32(cat, &aulong);
3802 aulong = SvUV(fromstr);
3803 CAT32(cat, &aulong);
3809 along = SvIV(fromstr);
3817 auquad = (unsigned Quad_t)SvIV(fromstr);
3818 sv_catpvn(cat, (char*)&auquad, sizeof(unsigned Quad_t));
3824 aquad = (Quad_t)SvIV(fromstr);
3825 sv_catpvn(cat, (char*)&aquad, sizeof(Quad_t));
3828 #endif /* HAS_QUAD */
3830 len = 1; /* assume SV is correct length */
3835 aptr = SvPV_force(fromstr, na); /* XXX Error if TEMP? */
3836 sv_catpvn(cat, (char*)&aptr, sizeof(char*));
3841 aptr = SvPV(fromstr, fromlen);
3842 SvGROW(cat, fromlen * 4 / 3);
3847 while (fromlen > 0) {
3854 doencodes(cat, aptr, todo);
3872 register I32 limit = POPi; /* note, negative is forever */
3875 register char *s = SvPV(sv, len);
3876 char *strend = s + len;
3878 register REGEXP *rx;
3882 I32 maxiters = (strend - s) + 10;
3885 I32 origlimit = limit;
3888 AV *oldstack = curstack;
3889 I32 gimme = GIMME_V;
3890 I32 oldsave = savestack_ix;
3893 Copy(&LvTARGOFF(POPs), &pm, 1, PMOP*);
3898 DIE("panic: do_split");
3899 rx = pm->op_pmregexp;
3901 TAINT_IF((pm->op_pmflags & PMf_LOCALE) &&
3902 (pm->op_pmflags & (PMf_WHITE | PMf_SKIPWHITE)));
3904 if (pm->op_pmreplroot)
3905 ary = GvAVn((GV*)pm->op_pmreplroot);
3906 else if (gimme != G_ARRAY)
3910 if (ary && (gimme != G_ARRAY || (pm->op_pmflags & PMf_ONCE))) {
3914 for (i = AvFILL(ary); i >= 0; i--)
3915 AvARRAY(ary)[i] = &sv_undef; /* don't free mere refs */
3919 /* temporarily switch stacks */
3920 SWITCHSTACK(curstack, ary);
3922 base = SP - stack_base;
3924 if (pm->op_pmflags & PMf_SKIPWHITE) {
3925 if (pm->op_pmflags & PMf_LOCALE) {
3926 while (isSPACE_LC(*s))
3934 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
3936 multiline = pm->op_pmflags & PMf_MULTILINE;
3940 limit = maxiters + 2;
3941 if (pm->op_pmflags & PMf_WHITE) {
3944 while (m < strend &&
3945 !((pm->op_pmflags & PMf_LOCALE)
3946 ? isSPACE_LC(*m) : isSPACE(*m)))
3951 dstr = NEWSV(30, m-s);
3952 sv_setpvn(dstr, s, m-s);
3958 while (s < strend &&
3959 ((pm->op_pmflags & PMf_LOCALE)
3960 ? isSPACE_LC(*s) : isSPACE(*s)))
3964 else if (strEQ("^", rx->precomp)) {
3967 for (m = s; m < strend && *m != '\n'; m++) ;
3971 dstr = NEWSV(30, m-s);
3972 sv_setpvn(dstr, s, m-s);
3979 else if (pm->op_pmshort && !rx->nparens) {
3980 i = SvCUR(pm->op_pmshort);
3982 i = *SvPVX(pm->op_pmshort);
3985 for (m = s; m < strend && *m != i; m++) ;
3988 dstr = NEWSV(30, m-s);
3989 sv_setpvn(dstr, s, m-s);
3998 while (s < strend && --limit &&
3999 (m=fbm_instr((unsigned char*)s, (unsigned char*)strend,
4003 dstr = NEWSV(31, m-s);
4004 sv_setpvn(dstr, s, m-s);
4013 maxiters += (strend - s) * rx->nparens;
4014 while (s < strend && --limit &&
4015 pregexec(rx, s, strend, orig, 1, Nullsv, TRUE))
4017 TAINT_IF(rx->exec_tainted);
4019 && rx->subbase != orig) {
4024 strend = s + (strend - m);
4027 dstr = NEWSV(32, m-s);
4028 sv_setpvn(dstr, s, m-s);
4033 for (i = 1; i <= rx->nparens; i++) {
4037 dstr = NEWSV(33, m-s);
4038 sv_setpvn(dstr, s, m-s);
4041 dstr = NEWSV(33, 0);
4050 LEAVE_SCOPE(oldsave);
4051 iters = (SP - stack_base) - base;
4052 if (iters > maxiters)
4055 /* keep field after final delim? */
4056 if (s < strend || (iters && origlimit)) {
4057 dstr = NEWSV(34, strend-s);
4058 sv_setpvn(dstr, s, strend-s);
4064 else if (!origlimit) {
4065 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0))
4069 SWITCHSTACK(ary, oldstack);
4070 if (gimme == G_ARRAY) {
4072 Copy(AvARRAY(ary), SP + 1, iters, SV*);
4078 if (gimme == G_ARRAY)
4081 if (iters || !pm->op_pmreplroot) {