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 * The compiler on Concurrent CX/UX systems has a subtle bug which only
20 * seems to show up when compiling pp.c - it generates the wrong double
21 * precision constant value for (double)UV_MAX when used inline in the body
22 * of the code below, so this makes a static variable up front (which the
23 * compiler seems to get correct) and uses it in place of UV_MAX below.
25 #ifdef CXUX_BROKEN_CONSTANT_CONVERT
26 static double UV_MAX_cxux = ((double)UV_MAX);
30 * Types used in bitwise operations.
32 * Normally we'd just use IV and UV. However, some hardware and
33 * software combinations (e.g. Alpha and current OSF/1) don't have a
34 * floating-point type to use for NV that has adequate bits to fully
35 * hold an IV/UV. (In other words, sizeof(long) == sizeof(double).)
37 * It just so happens that "int" is the right size almost everywhere.
43 * Mask used after bitwise operations.
45 * There is at least one realm (Cray word machines) that doesn't
46 * have an integral type (except char) small enough to be represented
47 * in a double without loss; that is, it has no 32-bit type.
49 #if BYTEORDER > 0xFFFF && defined(_CRAY) && !defined(_CRAYMPP)
51 # define BW_MASK ((1 << BW_BITS) - 1)
52 # define BW_SIGN (1 << (BW_BITS - 1))
53 # define BWi(i) (((i) & BW_SIGN) ? ((i) | ~BW_MASK) : ((i) & BW_MASK))
54 # define BWu(u) ((u) & BW_MASK)
61 * Offset for integer pack/unpack.
63 * On architectures where I16 and I32 aren't really 16 and 32 bits,
64 * which for now are all Crays, pack and unpack have to play games.
68 * These values are required for portability of pack() output.
69 * If they're not right on your machine, then pack() and unpack()
70 * wouldn't work right anyway; you'll need to apply the Cray hack.
71 * (I'd like to check them with #if, but you can't use sizeof() in
77 #if BYTEORDER > 0xFFFF && defined(_CRAY) && !defined(_CRAYMPP)
78 # if BYTEORDER == 0x12345678
79 # define OFF16(p) (char*)(p)
80 # define OFF32(p) (char*)(p)
82 # if BYTEORDER == 0x87654321
83 # define OFF16(p) ((char*)(p) + (sizeof(U16) - SIZE16))
84 # define OFF32(p) ((char*)(p) + (sizeof(U32) - SIZE32))
86 }}}} bad cray byte order
89 # define COPY16(s,p) (*(p) = 0, Copy(s, OFF16(p), SIZE16, char))
90 # define COPY32(s,p) (*(p) = 0, Copy(s, OFF32(p), SIZE32, char))
91 # define CAT16(sv,p) sv_catpvn(sv, OFF16(p), SIZE16)
92 # define CAT32(sv,p) sv_catpvn(sv, OFF32(p), SIZE32)
94 # define COPY16(s,p) Copy(s, p, SIZE16, char)
95 # define COPY32(s,p) Copy(s, p, SIZE32, char)
96 # define CAT16(sv,p) sv_catpvn(sv, (char*)(p), SIZE16)
97 # define CAT32(sv,p) sv_catpvn(sv, (char*)(p), SIZE32)
100 static void doencodes _((SV* sv, char* s, I32 len));
101 static SV* refto _((SV* sv));
102 static U32 seed _((void));
104 static bool srand_called = FALSE;
106 /* variations on pp_null */
111 if (GIMME_V == G_SCALAR)
126 if (op->op_private & OPpLVAL_INTRO)
127 SAVECLEARSV(curpad[op->op_targ]);
129 if (op->op_flags & OPf_REF) {
133 if (GIMME == G_ARRAY) {
134 I32 maxarg = AvFILL((AV*)TARG) + 1;
136 Copy(AvARRAY((AV*)TARG), SP+1, maxarg, SV*);
140 SV* sv = sv_newmortal();
141 I32 maxarg = AvFILL((AV*)TARG) + 1;
142 sv_setiv(sv, maxarg);
154 if (op->op_private & OPpLVAL_INTRO)
155 SAVECLEARSV(curpad[op->op_targ]);
156 if (op->op_flags & OPf_REF)
159 if (gimme == G_ARRAY) {
160 RETURNOP(do_kv(ARGS));
162 else if (gimme == G_SCALAR) {
163 SV* sv = sv_newmortal();
164 if (HvFILL((HV*)TARG))
165 sv_setpvf(sv, "%ld/%ld",
166 (long)HvFILL((HV*)TARG), (long)HvMAX((HV*)TARG) + 1);
176 DIE("NOT IMPL LINE %d",__LINE__);
188 if (SvTYPE(sv) == SVt_PVIO) {
189 GV *gv = (GV*) sv_newmortal();
190 gv_init(gv, 0, "", 0, 0);
191 GvIOp(gv) = (IO *)sv;
192 (void)SvREFCNT_inc(sv);
194 } else if (SvTYPE(sv) != SVt_PVGV)
195 DIE("Not a GLOB reference");
198 if (SvTYPE(sv) != SVt_PVGV) {
201 if (SvGMAGICAL(sv)) {
207 if (op->op_flags & OPf_REF ||
208 op->op_private & HINT_STRICT_REFS)
209 DIE(no_usym, "a symbol");
215 if (op->op_private & HINT_STRICT_REFS)
216 DIE(no_symref, sym, "a symbol");
217 sv = (SV*)gv_fetchpv(sym, TRUE, SVt_PVGV);
220 if (op->op_private & OPpLVAL_INTRO)
221 save_gp((GV*)sv, !(op->op_flags & OPf_SPECIAL));
233 switch (SvTYPE(sv)) {
237 DIE("Not a SCALAR reference");
244 if (SvTYPE(gv) != SVt_PVGV) {
245 if (SvGMAGICAL(sv)) {
251 if (op->op_flags & OPf_REF ||
252 op->op_private & HINT_STRICT_REFS)
253 DIE(no_usym, "a SCALAR");
259 if (op->op_private & HINT_STRICT_REFS)
260 DIE(no_symref, sym, "a SCALAR");
261 gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PV);
265 if (op->op_flags & OPf_MOD) {
266 if (op->op_private & OPpLVAL_INTRO)
267 sv = save_scalar((GV*)TOPs);
268 else if (op->op_private & OPpDEREF)
269 vivify_ref(sv, op->op_private & OPpDEREF);
279 SV *sv = AvARYLEN(av);
281 AvARYLEN(av) = sv = NEWSV(0,0);
282 sv_upgrade(sv, SVt_IV);
283 sv_magic(sv, (SV*)av, '#', Nullch, 0);
291 dSP; dTARGET; dPOPss;
293 if (op->op_flags & OPf_MOD) {
294 if (SvTYPE(TARG) < SVt_PVLV) {
295 sv_upgrade(TARG, SVt_PVLV);
296 sv_magic(TARG, Nullsv, '.', Nullch, 0);
301 PUSHs(TARG); /* no SvSETMAGIC */
307 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
308 mg = mg_find(sv, 'g');
309 if (mg && mg->mg_len >= 0) {
310 PUSHi(mg->mg_len + curcop->cop_arybase);
324 /* We usually try to add a non-existent subroutine in case of AUTOLOAD. */
325 /* (But not in defined().) */
326 CV *cv = sv_2cv(TOPs, &stash, &gv, !(op->op_flags & OPf_SPECIAL));
329 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
346 cv = sv_2cv(TOPs, &stash, &gv, FALSE);
348 ret = sv_2mortal(newSVpv(SvPVX(cv), SvCUR(cv)));
356 CV* cv = (CV*)curpad[op->op_targ];
358 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
374 if (GIMME != G_ARRAY) {
378 EXTEND_MORTAL(SP - MARK);
380 *MARK = refto(*MARK);
390 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
393 if (!(sv = LvTARG(sv)))
396 else if (SvPADTMP(sv))
400 (void)SvREFCNT_inc(sv);
403 sv_upgrade(rv, SVt_RV);
417 if (sv && SvGMAGICAL(sv))
420 if (!sv || !SvROK(sv))
424 pv = sv_reftype(sv,TRUE);
425 PUSHp(pv, strlen(pv));
435 stash = curcop->cop_stash;
437 stash = gv_stashsv(POPs, TRUE);
439 (void)sv_bless(TOPs, stash);
456 switch (elem ? *elem : '\0')
459 if (strEQ(elem, "ARRAY"))
463 if (strEQ(elem, "CODE"))
464 ref = (SV*)GvCVu(gv);
467 if (strEQ(elem, "FILEHANDLE")) /* XXX deprecate in 5.005 */
468 ref = (SV*)GvIOp(gv);
471 if (strEQ(elem, "GLOB"))
475 if (strEQ(elem, "HASH"))
479 if (strEQ(elem, "IO"))
480 ref = (SV*)GvIOp(gv);
483 if (strEQ(elem, "NAME"))
484 sv = newSVpv(GvNAME(gv), GvNAMELEN(gv));
487 if (strEQ(elem, "PACKAGE"))
488 sv = newSVpv(HvNAME(GvSTASH(gv)), 0);
491 if (strEQ(elem, "SCALAR"))
505 /* Pattern matching */
510 register unsigned char *s;
513 register I32 *sfirst;
517 if (sv == lastscream) {
523 SvSCREAM_off(lastscream);
524 SvREFCNT_dec(lastscream);
526 lastscream = SvREFCNT_inc(sv);
529 s = (unsigned char*)(SvPV(sv, len));
533 if (pos > maxscream) {
535 maxscream = pos + 80;
536 New(301, screamfirst, 256, I32);
537 New(302, screamnext, maxscream, I32);
540 maxscream = pos + pos / 4;
541 Renew(screamnext, maxscream, I32);
545 sfirst = screamfirst;
548 if (!sfirst || !snext)
549 DIE("do_study: out of memory");
551 for (ch = 256; ch; --ch)
558 snext[pos] = sfirst[ch] - pos;
565 sv_magic(sv, Nullsv, 'g', Nullch, 0); /* piggyback on m//g magic */
574 if (op->op_flags & OPf_STACKED)
580 TARG = sv_newmortal();
581 PUSHi(do_trans(sv, op));
585 /* Lvalue operators. */
607 SETi(do_chomp(TOPs));
614 register I32 count = 0;
617 count += do_chomp(POPs);
628 if (!sv || !SvANY(sv))
630 switch (SvTYPE(sv)) {
632 if (AvMAX(sv) >= 0 || SvGMAGICAL(sv))
636 if (HvARRAY(sv) || SvGMAGICAL(sv))
640 if (CvROOT(sv) || CvXSUB(sv))
657 if (!op->op_private) {
666 if (SvTHINKFIRST(sv)) {
673 switch (SvTYPE(sv)) {
683 if (cv_const_sv((CV*)sv))
684 warn("Constant subroutine %s undefined",
685 CvANON((CV*)sv) ? "(anonymous)" : GvENAME(CvGV((CV*)sv)));
688 { GV* gv = (GV*)SvREFCNT_inc(CvGV((CV*)sv));
690 CvGV((CV*)sv) = gv; } /* let user-undef'd sub keep its identity */
694 sv_setsv(sv, &sv_undef);
697 if (SvTYPE(sv) >= SVt_PV && SvPVX(sv) && SvLEN(sv)) {
700 SvPV_set(sv, Nullch);
713 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
715 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
716 SvIVX(TOPs) != IV_MIN)
719 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
730 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
732 sv_setsv(TARG, TOPs);
733 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
734 SvIVX(TOPs) != IV_MAX)
737 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
751 if(SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
753 sv_setsv(TARG, TOPs);
754 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
755 SvIVX(TOPs) != IV_MIN)
758 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
767 /* Ordinary operators. */
771 dSP; dATARGET; tryAMAGICbin(pow,opASSIGN);
774 SETn( pow( left, right) );
781 dSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
784 SETn( left * right );
791 dSP; dATARGET; tryAMAGICbin(div,opASSIGN);
796 DIE("Illegal division by zero");
798 /* insure that 20./5. == 4. */
801 if ((double)I_V(left) == left &&
802 (double)I_V(right) == right &&
803 (k = I_V(left)/I_V(right))*I_V(right) == I_V(left)) {
806 value = left / right;
810 value = left / right;
819 dSP; dATARGET; tryAMAGICbin(mod,opASSIGN);
827 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)) {
829 right = (right_neg = (i < 0)) ? -i : i;
833 right = U_V((right_neg = (n < 0)) ? -n : n);
836 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)) {
838 left = (left_neg = (i < 0)) ? -i : i;
842 left = U_V((left_neg = (n < 0)) ? -n : n);
846 DIE("Illegal modulus zero");
849 if ((left_neg != right_neg) && ans)
852 /* XXX may warn: unary minus operator applied to unsigned type */
853 /* could change -foo to be (~foo)+1 instead */
854 if (ans <= -(UV)IV_MAX)
855 sv_setiv(TARG, (IV) -ans);
857 sv_setnv(TARG, -(double)ans);
868 dSP; dATARGET; tryAMAGICbin(repeat,opASSIGN);
870 register I32 count = POPi;
871 if (GIMME == G_ARRAY && op->op_private & OPpREPEAT_DOLIST) {
873 I32 items = SP - MARK;
885 repeatcpy((char*)(MARK + items), (char*)MARK,
886 items * sizeof(SV*), count - 1);
892 else { /* Note: mark already snarfed by pp_list */
897 if (TARG == tmpstr && SvTHINKFIRST(tmpstr)) {
898 if (SvREADONLY(tmpstr) && curcop != &compiling)
899 DIE("Can't x= to readonly value");
903 SvSetSV(TARG, tmpstr);
904 SvPV_force(TARG, len);
909 SvGROW(TARG, (count * len) + 1);
910 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
911 SvCUR(TARG) *= count;
915 (void)SvPOK_only(TARG);
924 dSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
927 SETn( left - right );
934 dSP; dATARGET; tryAMAGICbin(lshift,opASSIGN);
937 if (op->op_private & HINT_INTEGER) {
953 dSP; dATARGET; tryAMAGICbin(rshift,opASSIGN);
956 if (op->op_private & HINT_INTEGER) {
972 dSP; tryAMAGICbinSET(lt,0);
975 SETs(boolSV(TOPn < value));
982 dSP; tryAMAGICbinSET(gt,0);
985 SETs(boolSV(TOPn > value));
992 dSP; tryAMAGICbinSET(le,0);
995 SETs(boolSV(TOPn <= value));
1002 dSP; tryAMAGICbinSET(ge,0);
1005 SETs(boolSV(TOPn >= value));
1012 dSP; tryAMAGICbinSET(ne,0);
1015 SETs(boolSV(TOPn != value));
1022 dSP; dTARGET; tryAMAGICbin(ncmp,0);
1029 else if (left < right)
1031 else if (left > right)
1044 dSP; tryAMAGICbinSET(slt,0);
1047 int cmp = ((op->op_private & OPpLOCALE)
1048 ? sv_cmp_locale(left, right)
1049 : sv_cmp(left, right));
1050 SETs(boolSV(cmp < 0));
1057 dSP; tryAMAGICbinSET(sgt,0);
1060 int cmp = ((op->op_private & OPpLOCALE)
1061 ? sv_cmp_locale(left, right)
1062 : sv_cmp(left, right));
1063 SETs(boolSV(cmp > 0));
1070 dSP; tryAMAGICbinSET(sle,0);
1073 int cmp = ((op->op_private & OPpLOCALE)
1074 ? sv_cmp_locale(left, right)
1075 : sv_cmp(left, right));
1076 SETs(boolSV(cmp <= 0));
1083 dSP; tryAMAGICbinSET(sge,0);
1086 int cmp = ((op->op_private & OPpLOCALE)
1087 ? sv_cmp_locale(left, right)
1088 : sv_cmp(left, right));
1089 SETs(boolSV(cmp >= 0));
1096 dSP; tryAMAGICbinSET(seq,0);
1099 SETs(boolSV(sv_eq(left, right)));
1106 dSP; tryAMAGICbinSET(sne,0);
1109 SETs(boolSV(!sv_eq(left, right)));
1116 dSP; dTARGET; tryAMAGICbin(scmp,0);
1119 int cmp = ((op->op_private & OPpLOCALE)
1120 ? sv_cmp_locale(left, right)
1121 : sv_cmp(left, right));
1129 dSP; dATARGET; tryAMAGICbin(band,opASSIGN);
1132 if (SvNIOKp(left) || SvNIOKp(right)) {
1133 if (op->op_private & HINT_INTEGER) {
1134 IBW value = SvIV(left) & SvIV(right);
1138 UBW value = SvUV(left) & SvUV(right);
1143 do_vop(op->op_type, TARG, left, right);
1152 dSP; dATARGET; tryAMAGICbin(bxor,opASSIGN);
1155 if (SvNIOKp(left) || SvNIOKp(right)) {
1156 if (op->op_private & HINT_INTEGER) {
1157 IBW value = (USE_LEFT(left) ? SvIV(left) : 0) ^ SvIV(right);
1161 UBW value = (USE_LEFT(left) ? SvUV(left) : 0) ^ SvUV(right);
1166 do_vop(op->op_type, TARG, left, right);
1175 dSP; dATARGET; tryAMAGICbin(bor,opASSIGN);
1178 if (SvNIOKp(left) || SvNIOKp(right)) {
1179 if (op->op_private & HINT_INTEGER) {
1180 IBW value = (USE_LEFT(left) ? SvIV(left) : 0) | SvIV(right);
1184 UBW value = (USE_LEFT(left) ? SvUV(left) : 0) | SvUV(right);
1189 do_vop(op->op_type, TARG, left, right);
1198 dSP; dTARGET; tryAMAGICun(neg);
1203 if (SvIOKp(sv) && !SvNOKp(sv) && !SvPOKp(sv) && SvIVX(sv) != IV_MIN)
1205 else if (SvNIOKp(sv))
1207 else if (SvPOKp(sv)) {
1209 char *s = SvPV(sv, len);
1210 if (isIDFIRST(*s)) {
1211 sv_setpvn(TARG, "-", 1);
1214 else if (*s == '+' || *s == '-') {
1216 *SvPV_force(TARG, len) = *s == '-' ? '+' : '-';
1219 sv_setnv(TARG, -SvNV(sv));
1231 dSP; tryAMAGICunSET(not);
1232 #endif /* OVERLOAD */
1233 *stack_sp = boolSV(!SvTRUE(*stack_sp));
1239 dSP; dTARGET; tryAMAGICun(compl);
1243 if (op->op_private & HINT_INTEGER) {
1244 IBW value = ~SvIV(sv);
1248 UBW value = ~SvUV(sv);
1253 register char *tmps;
1254 register long *tmpl;
1259 tmps = SvPV_force(TARG, len);
1262 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
1265 for ( ; anum >= sizeof(long); anum -= sizeof(long), tmpl++)
1269 for ( ; anum > 0; anum--, tmps++)
1278 /* integer versions of some of the above */
1282 dSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
1285 SETi( left * right );
1292 dSP; dATARGET; tryAMAGICbin(div,opASSIGN);
1296 DIE("Illegal division by zero");
1297 value = POPi / value;
1305 dSP; dATARGET; tryAMAGICbin(mod,opASSIGN);
1309 DIE("Illegal modulus zero");
1310 SETi( left % right );
1317 dSP; dATARGET; tryAMAGICbin(add,opASSIGN);
1320 SETi( left + right );
1327 dSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
1330 SETi( left - right );
1337 dSP; tryAMAGICbinSET(lt,0);
1340 SETs(boolSV(left < right));
1347 dSP; tryAMAGICbinSET(gt,0);
1350 SETs(boolSV(left > right));
1357 dSP; tryAMAGICbinSET(le,0);
1360 SETs(boolSV(left <= right));
1367 dSP; tryAMAGICbinSET(ge,0);
1370 SETs(boolSV(left >= right));
1377 dSP; tryAMAGICbinSET(eq,0);
1380 SETs(boolSV(left == right));
1387 dSP; tryAMAGICbinSET(ne,0);
1390 SETs(boolSV(left != right));
1397 dSP; dTARGET; tryAMAGICbin(ncmp,0);
1404 else if (left < right)
1415 dSP; dTARGET; tryAMAGICun(neg);
1420 /* High falutin' math. */
1424 dSP; dTARGET; tryAMAGICbin(atan2,0);
1427 SETn(atan2(left, right));
1434 dSP; dTARGET; tryAMAGICun(sin);
1446 dSP; dTARGET; tryAMAGICun(cos);
1466 if (!srand_called) {
1467 (void)srand((unsigned)seed());
1468 srand_called = TRUE;
1471 value = rand() * value / 2147483648.0;
1474 value = rand() * value / 65536.0;
1477 value = rand() * value / 32768.0;
1479 value = rand() * value / (double)(((unsigned long)1) << RANDBITS);
1495 (void)srand((unsigned)anum);
1496 srand_called = TRUE;
1505 * This is really just a quick hack which grabs various garbage
1506 * values. It really should be a real hash algorithm which
1507 * spreads the effect of every input bit onto every output bit,
1508 * if someone who knows about such tings would bother to write it.
1509 * Might be a good idea to add that function to CORE as well.
1510 * No numbers below come from careful analysis or anyting here,
1511 * except they are primes and SEED_C1 > 1E6 to get a full-width
1512 * value from (tv_sec * SEED_C1 + tv_usec). The multipliers should
1513 * probably be bigger too.
1516 # define SEED_C1 1000003
1517 #define SEED_C4 73819
1519 # define SEED_C1 25747
1520 #define SEED_C4 20639
1524 #define SEED_C5 26107
1528 # include <starlet.h>
1529 /* when[] = (low 32 bits, high 32 bits) of time since epoch
1530 * in 100-ns units, typically incremented ever 10 ms. */
1531 unsigned int when[2];
1532 _ckvmssts(sys$gettim(when));
1533 u = (U32)SEED_C1 * when[0] + (U32)SEED_C2 * when[1];
1535 # ifdef HAS_GETTIMEOFDAY
1536 struct timeval when;
1537 gettimeofday(&when,(struct timezone *) 0);
1538 u = (U32)SEED_C1 * when.tv_sec + (U32)SEED_C2 * when.tv_usec;
1542 u = (U32)SEED_C1 * when;
1545 u += SEED_C3 * (U32)getpid();
1546 u += SEED_C4 * (U32)(UV)stack_sp;
1547 #ifndef PLAN9 /* XXX Plan9 assembler chokes on this; fix needed */
1548 u += SEED_C5 * (U32)(UV)&when;
1555 dSP; dTARGET; tryAMAGICun(exp);
1567 dSP; dTARGET; tryAMAGICun(log);
1572 SET_NUMERIC_STANDARD();
1573 DIE("Can't take log of %g", value);
1583 dSP; dTARGET; tryAMAGICun(sqrt);
1588 SET_NUMERIC_STANDARD();
1589 DIE("Can't take sqrt of %g", value);
1591 value = sqrt(value);
1601 double value = TOPn;
1604 if (SvIOKp(TOPs) && !SvNOKp(TOPs) && !SvPOKp(TOPs)) {
1610 (void)modf(value, &value);
1612 (void)modf(-value, &value);
1627 dSP; dTARGET; tryAMAGICun(abs);
1629 double value = TOPn;
1632 if (SvIOKp(TOPs) && !SvNOKp(TOPs) && !SvPOKp(TOPs) &&
1633 (iv = SvIVX(TOPs)) != IV_MIN) {
1654 XPUSHu(scan_hex(tmps, 99, &argtype));
1666 while (*tmps && isSPACE(*tmps))
1671 value = scan_hex(++tmps, 99, &argtype);
1673 value = scan_oct(tmps, 99, &argtype);
1683 SETi( sv_len(TOPs) );
1696 I32 lvalue = op->op_flags & OPf_MOD;
1698 I32 arybase = curcop->cop_arybase;
1704 tmps = SvPV(sv, curlen);
1705 if (pos >= arybase) {
1723 else if (len >= 0) {
1725 if (rem > (I32)curlen)
1739 if (dowarn || lvalue)
1740 warn("substr outside of string");
1745 sv_setpvn(TARG, tmps, rem);
1746 if (lvalue) { /* it's an lvalue! */
1747 if (!SvGMAGICAL(sv)) {
1751 warn("Attempt to use reference as lvalue in substr");
1753 if (SvOK(sv)) /* is it defined ? */
1754 (void)SvPOK_only(sv);
1756 sv_setpvn(sv,"",0); /* avoid lexical reincarnation */
1759 if (SvTYPE(TARG) < SVt_PVLV) {
1760 sv_upgrade(TARG, SVt_PVLV);
1761 sv_magic(TARG, Nullsv, 'x', Nullch, 0);
1766 LvTARGOFF(TARG) = pos;
1767 LvTARGLEN(TARG) = rem;
1770 PUSHs(TARG); /* avoid SvSETMAGIC here */
1777 register I32 size = POPi;
1778 register I32 offset = POPi;
1779 register SV *src = POPs;
1780 I32 lvalue = op->op_flags & OPf_MOD;
1782 unsigned char *s = (unsigned char*)SvPV(src, srclen);
1783 unsigned long retnum;
1786 offset *= size; /* turn into bit offset */
1787 len = (offset + size + 7) / 8;
1788 if (offset < 0 || size < 1)
1791 if (lvalue) { /* it's an lvalue! */
1792 if (SvTYPE(TARG) < SVt_PVLV) {
1793 sv_upgrade(TARG, SVt_PVLV);
1794 sv_magic(TARG, Nullsv, 'v', Nullch, 0);
1799 LvTARGOFF(TARG) = offset;
1800 LvTARGLEN(TARG) = size;
1808 if (offset >= srclen)
1811 retnum = (unsigned long) s[offset] << 8;
1813 else if (size == 32) {
1814 if (offset >= srclen)
1816 else if (offset + 1 >= srclen)
1817 retnum = (unsigned long) s[offset] << 24;
1818 else if (offset + 2 >= srclen)
1819 retnum = ((unsigned long) s[offset] << 24) +
1820 ((unsigned long) s[offset + 1] << 16);
1822 retnum = ((unsigned long) s[offset] << 24) +
1823 ((unsigned long) s[offset + 1] << 16) +
1824 (s[offset + 2] << 8);
1829 retnum = (s[offset >> 3] >> (offset & 7)) & ((1 << size) - 1);
1834 else if (size == 16)
1835 retnum = ((unsigned long) s[offset] << 8) + s[offset+1];
1836 else if (size == 32)
1837 retnum = ((unsigned long) s[offset] << 24) +
1838 ((unsigned long) s[offset + 1] << 16) +
1839 (s[offset + 2] << 8) + s[offset+3];
1843 sv_setiv(TARG, (IV)retnum);
1858 I32 arybase = curcop->cop_arybase;
1863 offset = POPi - arybase;
1866 tmps = SvPV(big, biglen);
1869 else if (offset > biglen)
1871 if (!(tmps2 = fbm_instr((unsigned char*)tmps + offset,
1872 (unsigned char*)tmps + biglen, little)))
1873 retval = -1 + arybase;
1875 retval = tmps2 - tmps + arybase;
1892 I32 arybase = curcop->cop_arybase;
1898 tmps2 = SvPV(little, llen);
1899 tmps = SvPV(big, blen);
1903 offset = SvIV(offstr) - arybase + llen;
1906 else if (offset > blen)
1908 if (!(tmps2 = rninstr(tmps, tmps + offset,
1909 tmps2, tmps2 + llen)))
1910 retval = -1 + arybase;
1912 retval = tmps2 - tmps + arybase;
1919 dSP; dMARK; dORIGMARK; dTARGET;
1920 #ifdef USE_LOCALE_NUMERIC
1921 if (op->op_private & OPpLOCALE)
1922 SET_NUMERIC_LOCAL();
1924 SET_NUMERIC_STANDARD();
1926 do_sprintf(TARG, SP-MARK, MARK+1);
1927 TAINT_IF(SvTAINTED(TARG));
1941 value = (I32) (*tmps & 255);
1946 value = (I32) (anum & 255);
1957 (void)SvUPGRADE(TARG,SVt_PV);
1963 (void)SvPOK_only(TARG);
1970 dSP; dTARGET; dPOPTOPssrl;
1972 char *tmps = SvPV(left, na);
1974 sv_setpv(TARG, fcrypt(tmps, SvPV(right, na)));
1976 sv_setpv(TARG, crypt(tmps, SvPV(right, na)));
1980 "The crypt() function is unimplemented due to excessive paranoia.");
1992 if (!SvPADTMP(sv)) {
1998 s = SvPV_force(sv, na);
2000 if (op->op_private & OPpLOCALE) {
2003 *s = toUPPER_LC(*s);
2018 if (!SvPADTMP(sv)) {
2024 s = SvPV_force(sv, na);
2026 if (op->op_private & OPpLOCALE) {
2029 *s = toLOWER_LC(*s);
2046 if (!SvPADTMP(sv)) {
2053 s = SvPV_force(sv, len);
2055 register char *send = s + len;
2057 if (op->op_private & OPpLOCALE) {
2060 for (; s < send; s++)
2061 *s = toUPPER_LC(*s);
2064 for (; s < send; s++)
2078 if (!SvPADTMP(sv)) {
2085 s = SvPV_force(sv, len);
2087 register char *send = s + len;
2089 if (op->op_private & OPpLOCALE) {
2092 for (; s < send; s++)
2093 *s = toLOWER_LC(*s);
2096 for (; s < send; s++)
2108 register char *s = SvPV(sv,len);
2112 (void)SvUPGRADE(TARG, SVt_PV);
2113 SvGROW(TARG, (len * 2) + 1);
2121 SvCUR_set(TARG, d - SvPVX(TARG));
2122 (void)SvPOK_only(TARG);
2125 sv_setpvn(TARG, s, len);
2134 dSP; dMARK; dORIGMARK;
2136 register AV* av = (AV*)POPs;
2137 register I32 lval = op->op_flags & OPf_MOD;
2138 I32 arybase = curcop->cop_arybase;
2141 if (SvTYPE(av) == SVt_PVAV) {
2142 if (lval && op->op_private & OPpLVAL_INTRO) {
2144 for (svp = mark + 1; svp <= sp; svp++) {
2149 if (max > AvMAX(av))
2152 while (++MARK <= SP) {
2153 elem = SvIVx(*MARK);
2157 svp = av_fetch(av, elem, lval);
2159 if (!svp || *svp == &sv_undef)
2160 DIE(no_aelem, elem);
2161 if (op->op_private & OPpLVAL_INTRO)
2164 *MARK = svp ? *svp : &sv_undef;
2167 if (GIMME != G_ARRAY) {
2175 /* Associative arrays. */
2180 HV *hash = (HV*)POPs;
2182 I32 gimme = GIMME_V;
2185 entry = hv_iternext(hash); /* might clobber stack_sp */
2190 PUSHs(hv_iterkeysv(entry)); /* won't clobber stack_sp */
2191 if (gimme == G_ARRAY) {
2193 sv_setsv(TARG, hv_iterval(hash, entry)); /* might hit stack_sp */
2198 else if (gimme == G_SCALAR)
2217 I32 gimme = GIMME_V;
2218 I32 discard = (gimme == G_VOID) ? G_DISCARD : 0;
2222 if (op->op_private & OPpSLICE) {
2225 if (SvTYPE(hv) != SVt_PVHV)
2226 DIE("Not a HASH reference");
2227 while (++MARK <= SP) {
2228 sv = hv_delete_ent(hv, *MARK, discard, 0);
2229 *MARK = sv ? sv : &sv_undef;
2233 else if (gimme == G_SCALAR) {
2242 if (SvTYPE(hv) != SVt_PVHV)
2243 DIE("Not a HASH reference");
2244 sv = hv_delete_ent(hv, keysv, discard, 0);
2259 if (SvTYPE(hv) != SVt_PVHV) {
2260 DIE("Not a HASH reference");
2262 if (hv_exists_ent(hv, tmpsv, 0))
2269 dSP; dMARK; dORIGMARK;
2271 register HV *hv = (HV*)POPs;
2272 register I32 lval = op->op_flags & OPf_MOD;
2274 if (SvTYPE(hv) == SVt_PVHV) {
2275 while (++MARK <= SP) {
2278 he = hv_fetch_ent(hv, keysv, lval, 0);
2280 if (!he || HeVAL(he) == &sv_undef)
2281 DIE(no_helem, SvPV(keysv, na));
2282 if (op->op_private & OPpLVAL_INTRO)
2283 save_svref(&HeVAL(he));
2285 *MARK = he ? HeVAL(he) : &sv_undef;
2288 if (GIMME != G_ARRAY) {
2296 /* List operators. */
2301 if (GIMME != G_ARRAY) {
2303 *MARK = *SP; /* unwanted list, return last item */
2314 SV **lastrelem = stack_sp;
2315 SV **lastlelem = stack_base + POPMARK;
2316 SV **firstlelem = stack_base + POPMARK + 1;
2317 register SV **firstrelem = lastlelem + 1;
2318 I32 arybase = curcop->cop_arybase;
2319 I32 lval = op->op_flags & OPf_MOD;
2320 I32 is_something_there = lval;
2322 register I32 max = lastrelem - lastlelem;
2323 register SV **lelem;
2326 if (GIMME != G_ARRAY) {
2327 ix = SvIVx(*lastlelem);
2332 if (ix < 0 || ix >= max)
2333 *firstlelem = &sv_undef;
2335 *firstlelem = firstrelem[ix];
2341 SP = firstlelem - 1;
2345 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
2351 else if (!(*lelem = firstrelem[ix]))
2356 if (ix >= max || !(*lelem = firstrelem[ix]))
2359 if (!is_something_there && (SvOK(*lelem) || SvGMAGICAL(*lelem)))
2360 is_something_there = TRUE;
2362 if (is_something_there)
2365 SP = firstlelem - 1;
2371 dSP; dMARK; dORIGMARK;
2372 I32 items = SP - MARK;
2373 SV *av = sv_2mortal((SV*)av_make(items, MARK+1));
2374 SP = ORIGMARK; /* av_make() might realloc stack_sp */
2381 dSP; dMARK; dORIGMARK;
2382 HV* hv = (HV*)sv_2mortal((SV*)newHV());
2386 SV *val = NEWSV(46, 0);
2388 sv_setsv(val, *++MARK);
2390 warn("Odd number of elements in hash list");
2391 (void)hv_store_ent(hv,key,val,0);
2400 dSP; dMARK; dORIGMARK;
2401 register AV *ary = (AV*)*++MARK;
2405 register I32 offset;
2406 register I32 length;
2415 offset = i = SvIVx(*MARK);
2417 offset += AvFILL(ary) + 1;
2419 offset -= curcop->cop_arybase;
2423 length = SvIVx(*MARK++);
2428 length = AvMAX(ary) + 1; /* close enough to infinity */
2432 length = AvMAX(ary) + 1;
2434 if (offset > AvFILL(ary) + 1)
2435 offset = AvFILL(ary) + 1;
2436 after = AvFILL(ary) + 1 - (offset + length);
2437 if (after < 0) { /* not that much array */
2438 length += after; /* offset+length now in array */
2444 /* At this point, MARK .. SP-1 is our new LIST */
2447 diff = newlen - length;
2448 if (newlen && !AvREAL(ary)) {
2452 assert(AvREAL(ary)); /* would leak, so croak */
2455 if (diff < 0) { /* shrinking the area */
2457 New(451, tmparyval, newlen, SV*); /* so remember insertion */
2458 Copy(MARK, tmparyval, newlen, SV*);
2461 MARK = ORIGMARK + 1;
2462 if (GIMME == G_ARRAY) { /* copy return vals to stack */
2463 MEXTEND(MARK, length);
2464 Copy(AvARRAY(ary)+offset, MARK, length, SV*);
2466 EXTEND_MORTAL(length);
2467 for (i = length, dst = MARK; i; i--) {
2468 if (!SvIMMORTAL(*dst))
2469 sv_2mortal(*dst); /* free them eventualy */
2476 *MARK = AvARRAY(ary)[offset+length-1];
2478 if (!SvIMMORTAL(*MARK))
2480 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
2481 SvREFCNT_dec(*dst++); /* free them now */
2484 AvFILL(ary) += diff;
2486 /* pull up or down? */
2488 if (offset < after) { /* easier to pull up */
2489 if (offset) { /* esp. if nothing to pull */
2490 src = &AvARRAY(ary)[offset-1];
2491 dst = src - diff; /* diff is negative */
2492 for (i = offset; i > 0; i--) /* can't trust Copy */
2496 SvPVX(ary) = (char*)(AvARRAY(ary) - diff); /* diff is negative */
2500 if (after) { /* anything to pull down? */
2501 src = AvARRAY(ary) + offset + length;
2502 dst = src + diff; /* diff is negative */
2503 Move(src, dst, after, SV*);
2505 dst = &AvARRAY(ary)[AvFILL(ary)+1];
2506 /* avoid later double free */
2510 dst[--i] = &sv_undef;
2513 for (src = tmparyval, dst = AvARRAY(ary) + offset;
2515 *dst = NEWSV(46, 0);
2516 sv_setsv(*dst++, *src++);
2518 Safefree(tmparyval);
2521 else { /* no, expanding (or same) */
2523 New(452, tmparyval, length, SV*); /* so remember deletion */
2524 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
2527 if (diff > 0) { /* expanding */
2529 /* push up or down? */
2531 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
2535 Move(src, dst, offset, SV*);
2537 SvPVX(ary) = (char*)(AvARRAY(ary) - diff);/* diff is positive */
2539 AvFILL(ary) += diff;
2542 if (AvFILL(ary) + diff >= AvMAX(ary)) /* oh, well */
2543 av_extend(ary, AvFILL(ary) + diff);
2544 AvFILL(ary) += diff;
2547 dst = AvARRAY(ary) + AvFILL(ary);
2549 for (i = after; i; i--) {
2556 for (src = MARK, dst = AvARRAY(ary) + offset; newlen; newlen--) {
2557 *dst = NEWSV(46, 0);
2558 sv_setsv(*dst++, *src++);
2560 MARK = ORIGMARK + 1;
2561 if (GIMME == G_ARRAY) { /* copy return vals to stack */
2563 Copy(tmparyval, MARK, length, SV*);
2565 EXTEND_MORTAL(length);
2566 for (i = length, dst = MARK; i; i--) {
2567 if (!SvIMMORTAL(*dst))
2568 sv_2mortal(*dst); /* free them eventualy */
2572 Safefree(tmparyval);
2576 else if (length--) {
2577 *MARK = tmparyval[length];
2579 if (!SvIMMORTAL(*MARK))
2581 while (length-- > 0)
2582 SvREFCNT_dec(tmparyval[length]);
2584 Safefree(tmparyval);
2595 dSP; dMARK; dORIGMARK; dTARGET;
2596 register AV *ary = (AV*)*++MARK;
2597 register SV *sv = &sv_undef;
2599 for (++MARK; MARK <= SP; MARK++) {
2602 sv_setsv(sv, *MARK);
2606 PUSHi( AvFILL(ary) + 1 );
2614 SV *sv = av_pop(av);
2615 if (!SvIMMORTAL(sv) && AvREAL(av))
2616 (void)sv_2mortal(sv);
2625 SV *sv = av_shift(av);
2629 if (!SvIMMORTAL(sv) && AvREAL(av))
2630 (void)sv_2mortal(sv);
2637 dSP; dMARK; dORIGMARK; dTARGET;
2638 register AV *ary = (AV*)*++MARK;
2642 av_unshift(ary, SP - MARK);
2645 sv_setsv(sv, *++MARK);
2646 (void)av_store(ary, i++, sv);
2650 PUSHi( AvFILL(ary) + 1 );
2660 if (GIMME == G_ARRAY) {
2671 register char *down;
2677 do_join(TARG, &sv_no, MARK, SP);
2679 sv_setsv(TARG, (SP > MARK) ? *SP : GvSV(defgv));
2680 up = SvPV_force(TARG, len);
2682 down = SvPVX(TARG) + len - 1;
2688 (void)SvPOK_only(TARG);
2702 char *s = SvPV(sv, len);
2706 if (!strnEQ(s, "0000", 4)) { /* need to grow sv */
2707 SV *new = newSVpv("0000000000", 10);
2710 SvREFCNT_dec(sv); /* free old sv */
2715 while (!*t) /* trailing '\0'? */
2718 i = ((*t - '0') << 7) + m;
2719 *(t--) = '0' + (i % 10);
2725 /* Explosives and implosives. */
2732 I32 gimme = GIMME_V;
2736 register char *pat = SvPV(left, llen);
2737 register char *s = SvPV(right, rlen);
2738 char *strend = s + rlen;
2740 register char *patend = pat + llen;
2745 /* These must not be in registers: */
2756 unsigned Quad_t auquad;
2762 register U32 culong;
2764 static char* bitcount = 0;
2767 if (gimme != G_ARRAY) { /* arrange to do first one only */
2769 for (patend = pat; !isALPHA(*patend) || *patend == 'x'; patend++) ;
2770 if (strchr("aAbBhHP", *patend) || *pat == '%') {
2772 while (isDIGIT(*patend) || *patend == '*')
2778 while (pat < patend) {
2780 datumtype = *pat++ & 0xFF;
2781 if (isSPACE(datumtype))
2785 else if (*pat == '*') {
2786 len = strend - strbeg; /* long enough */
2789 else if (isDIGIT(*pat)) {
2791 while (isDIGIT(*pat))
2792 len = (len * 10) + (*pat++ - '0');
2795 len = (datumtype != '@');
2798 croak("Invalid type in unpack: '%c'", (int)datumtype);
2799 case ',': /* grandfather in commas but with a warning */
2800 if (commas++ == 0 && dowarn)
2801 warn("Invalid type in unpack: '%c'", (int)datumtype);
2804 if (len == 1 && pat[-1] != '1')
2813 if (len > strend - strbeg)
2814 DIE("@ outside of string");
2818 if (len > s - strbeg)
2819 DIE("X outside of string");
2823 if (len > strend - s)
2824 DIE("x outside of string");
2829 if (len > strend - s)
2832 goto uchar_checksum;
2833 sv = NEWSV(35, len);
2834 sv_setpvn(sv, s, len);
2836 if (datumtype == 'A') {
2837 aptr = s; /* borrow register */
2838 s = SvPVX(sv) + len - 1;
2839 while (s >= SvPVX(sv) && (!*s || isSPACE(*s)))
2842 SvCUR_set(sv, s - SvPVX(sv));
2843 s = aptr; /* unborrow register */
2845 XPUSHs(sv_2mortal(sv));
2849 if (pat[-1] == '*' || len > (strend - s) * 8)
2850 len = (strend - s) * 8;
2853 Newz(601, bitcount, 256, char);
2854 for (bits = 1; bits < 256; bits++) {
2855 if (bits & 1) bitcount[bits]++;
2856 if (bits & 2) bitcount[bits]++;
2857 if (bits & 4) bitcount[bits]++;
2858 if (bits & 8) bitcount[bits]++;
2859 if (bits & 16) bitcount[bits]++;
2860 if (bits & 32) bitcount[bits]++;
2861 if (bits & 64) bitcount[bits]++;
2862 if (bits & 128) bitcount[bits]++;
2866 culong += bitcount[*(unsigned char*)s++];
2871 if (datumtype == 'b') {
2873 if (bits & 1) culong++;
2879 if (bits & 128) culong++;
2886 sv = NEWSV(35, len + 1);
2889 aptr = pat; /* borrow register */
2891 if (datumtype == 'b') {
2893 for (len = 0; len < aint; len++) {
2894 if (len & 7) /*SUPPRESS 595*/
2898 *pat++ = '0' + (bits & 1);
2903 for (len = 0; len < aint; len++) {
2908 *pat++ = '0' + ((bits & 128) != 0);
2912 pat = aptr; /* unborrow register */
2913 XPUSHs(sv_2mortal(sv));
2917 if (pat[-1] == '*' || len > (strend - s) * 2)
2918 len = (strend - s) * 2;
2919 sv = NEWSV(35, len + 1);
2922 aptr = pat; /* borrow register */
2924 if (datumtype == 'h') {
2926 for (len = 0; len < aint; len++) {
2931 *pat++ = hexdigit[bits & 15];
2936 for (len = 0; len < aint; len++) {
2941 *pat++ = hexdigit[(bits >> 4) & 15];
2945 pat = aptr; /* unborrow register */
2946 XPUSHs(sv_2mortal(sv));
2949 if (len > strend - s)
2954 if (aint >= 128) /* fake up signed chars */
2964 if (aint >= 128) /* fake up signed chars */
2967 sv_setiv(sv, (IV)aint);
2968 PUSHs(sv_2mortal(sv));
2973 if (len > strend - s)
2988 sv_setiv(sv, (IV)auint);
2989 PUSHs(sv_2mortal(sv));
2994 along = (strend - s) / SIZE16;
3011 sv_setiv(sv, (IV)ashort);
3012 PUSHs(sv_2mortal(sv));
3019 along = (strend - s) / SIZE16;
3024 COPY16(s, &aushort);
3027 if (datumtype == 'n')
3028 aushort = ntohs(aushort);
3031 if (datumtype == 'v')
3032 aushort = vtohs(aushort);
3041 COPY16(s, &aushort);
3045 if (datumtype == 'n')
3046 aushort = ntohs(aushort);
3049 if (datumtype == 'v')
3050 aushort = vtohs(aushort);
3052 sv_setiv(sv, (IV)aushort);
3053 PUSHs(sv_2mortal(sv));
3058 along = (strend - s) / sizeof(int);
3063 Copy(s, &aint, 1, int);
3066 cdouble += (double)aint;
3075 Copy(s, &aint, 1, int);
3078 sv_setiv(sv, (IV)aint);
3079 PUSHs(sv_2mortal(sv));
3084 along = (strend - s) / sizeof(unsigned int);
3089 Copy(s, &auint, 1, unsigned int);
3090 s += sizeof(unsigned int);
3092 cdouble += (double)auint;
3101 Copy(s, &auint, 1, unsigned int);
3102 s += sizeof(unsigned int);
3104 sv_setuv(sv, (UV)auint);
3105 PUSHs(sv_2mortal(sv));
3110 along = (strend - s) / SIZE32;
3118 cdouble += (double)along;
3130 sv_setiv(sv, (IV)along);
3131 PUSHs(sv_2mortal(sv));
3138 along = (strend - s) / SIZE32;
3146 if (datumtype == 'N')
3147 aulong = ntohl(aulong);
3150 if (datumtype == 'V')
3151 aulong = vtohl(aulong);
3154 cdouble += (double)aulong;
3166 if (datumtype == 'N')
3167 aulong = ntohl(aulong);
3170 if (datumtype == 'V')
3171 aulong = vtohl(aulong);
3174 sv_setuv(sv, (UV)aulong);
3175 PUSHs(sv_2mortal(sv));
3180 along = (strend - s) / sizeof(char*);
3186 if (sizeof(char*) > strend - s)
3189 Copy(s, &aptr, 1, char*);
3195 PUSHs(sv_2mortal(sv));
3205 while ((len > 0) && (s < strend)) {
3206 auv = (auv << 7) | (*s & 0x7f);
3207 if (!(*s++ & 0x80)) {
3211 PUSHs(sv_2mortal(sv));
3215 else if (++bytes >= sizeof(UV)) { /* promote to string */
3218 sv = newSVpvf("%.*Vu", (int)TYPE_DIGITS(UV), auv);
3219 while (s < strend) {
3220 sv = mul128(sv, *s & 0x7f);
3221 if (!(*s++ & 0x80)) {
3230 PUSHs(sv_2mortal(sv));
3235 if ((s >= strend) && bytes)
3236 croak("Unterminated compressed integer");
3241 if (sizeof(char*) > strend - s)
3244 Copy(s, &aptr, 1, char*);
3249 sv_setpvn(sv, aptr, len);
3250 PUSHs(sv_2mortal(sv));
3257 if (s + sizeof(Quad_t) > strend)
3260 Copy(s, &aquad, 1, Quad_t);
3261 s += sizeof(Quad_t);
3264 if (aquad >= IV_MIN && aquad <= IV_MAX)
3265 sv_setiv(sv, (IV)aquad);
3267 sv_setnv(sv, (double)aquad);
3268 PUSHs(sv_2mortal(sv));
3275 if (s + sizeof(unsigned Quad_t) > strend)
3278 Copy(s, &auquad, 1, unsigned Quad_t);
3279 s += sizeof(unsigned Quad_t);
3282 if (aquad <= UV_MAX)
3283 sv_setuv(sv, (UV)auquad);
3285 sv_setnv(sv, (double)auquad);
3286 PUSHs(sv_2mortal(sv));
3290 /* float and double added gnb@melba.bby.oz.au 22/11/89 */
3293 along = (strend - s) / sizeof(float);
3298 Copy(s, &afloat, 1, float);
3307 Copy(s, &afloat, 1, float);
3310 sv_setnv(sv, (double)afloat);
3311 PUSHs(sv_2mortal(sv));
3317 along = (strend - s) / sizeof(double);
3322 Copy(s, &adouble, 1, double);
3323 s += sizeof(double);
3331 Copy(s, &adouble, 1, double);
3332 s += sizeof(double);
3334 sv_setnv(sv, (double)adouble);
3335 PUSHs(sv_2mortal(sv));
3340 along = (strend - s) * 3 / 4;
3341 sv = NEWSV(42, along);
3344 while (s < strend && *s > ' ' && *s < 'a') {
3349 len = (*s++ - ' ') & 077;
3351 if (s < strend && *s >= ' ')
3352 a = (*s++ - ' ') & 077;
3355 if (s < strend && *s >= ' ')
3356 b = (*s++ - ' ') & 077;
3359 if (s < strend && *s >= ' ')
3360 c = (*s++ - ' ') & 077;
3363 if (s < strend && *s >= ' ')
3364 d = (*s++ - ' ') & 077;
3367 hunk[0] = a << 2 | b >> 4;
3368 hunk[1] = b << 4 | c >> 2;
3369 hunk[2] = c << 6 | d;
3370 sv_catpvn(sv, hunk, len > 3 ? 3 : len);
3375 else if (s[1] == '\n') /* possible checksum byte */
3378 XPUSHs(sv_2mortal(sv));
3383 if (strchr("fFdD", datumtype) ||
3384 (checksum > 32 && strchr("iIlLN", datumtype)) ) {
3388 while (checksum >= 16) {
3392 while (checksum >= 4) {
3398 along = (1 << checksum) - 1;
3399 while (cdouble < 0.0)
3401 cdouble = modf(cdouble / adouble, &trouble) * adouble;
3402 sv_setnv(sv, cdouble);
3405 if (checksum < 32) {
3406 aulong = (1 << checksum) - 1;
3409 sv_setuv(sv, (UV)culong);
3411 XPUSHs(sv_2mortal(sv));
3415 if (sp == oldsp && gimme == G_SCALAR)
3421 doencodes(sv, s, len)
3429 sv_catpvn(sv, hunk, 1);
3432 hunk[0] = ' ' + (077 & (*s >> 2));
3433 hunk[1] = ' ' + (077 & ((*s << 4) & 060 | (s[1] >> 4) & 017));
3434 hunk[2] = ' ' + (077 & ((s[1] << 2) & 074 | (s[2] >> 6) & 03));
3435 hunk[3] = ' ' + (077 & (s[2] & 077));
3436 sv_catpvn(sv, hunk, 4);
3440 for (s = SvPVX(sv); *s; s++) {
3444 sv_catpvn(sv, "\n", 1);
3452 SV *result = newSVpv("", l);
3453 char *result_c = SvPV(result, na); /* convenience */
3454 char *out = result_c;
3464 SvREFCNT_dec(result);
3487 SvREFCNT_dec(result);
3493 SvCUR_set(result, out - result_c);
3499 SV *pnum; /* must be '\0' terminated */
3503 char *s = SvPV(pnum, len);
3512 i = m * 10 + (*t - '0');
3514 r = (i >> 7); /* r < 10 */
3521 SvCUR_set(pnum, (STRLEN) (t - s));
3528 dSP; dMARK; dORIGMARK; dTARGET;
3529 register SV *cat = TARG;
3532 register char *pat = SvPVx(*++MARK, fromlen);
3533 register char *patend = pat + fromlen;
3538 static char null10[] = {0,0,0,0,0,0,0,0,0,0};
3539 static char *space10 = " ";
3541 /* These must not be in registers: */
3550 unsigned Quad_t auquad;
3559 sv_setpvn(cat, "", 0);
3560 while (pat < patend) {
3561 #define NEXTFROM (items-- > 0 ? *MARK++ : &sv_no)
3562 datumtype = *pat++ & 0xFF;
3563 if (isSPACE(datumtype))
3566 len = strchr("@Xxu", datumtype) ? 0 : items;
3569 else if (isDIGIT(*pat)) {
3571 while (isDIGIT(*pat))
3572 len = (len * 10) + (*pat++ - '0');
3578 croak("Invalid type in pack: '%c'", (int)datumtype);
3579 case ',': /* grandfather in commas but with a warning */
3580 if (commas++ == 0 && dowarn)
3581 warn("Invalid type in pack: '%c'", (int)datumtype);
3584 DIE("%% may only be used in unpack");
3595 if (SvCUR(cat) < len)
3596 DIE("X outside of string");
3603 sv_catpvn(cat, null10, 10);
3606 sv_catpvn(cat, null10, len);
3611 aptr = SvPV(fromstr, fromlen);
3615 sv_catpvn(cat, aptr, len);
3617 sv_catpvn(cat, aptr, fromlen);
3619 if (datumtype == 'A') {
3621 sv_catpvn(cat, space10, 10);
3624 sv_catpvn(cat, space10, len);
3628 sv_catpvn(cat, null10, 10);
3631 sv_catpvn(cat, null10, len);
3638 char *savepat = pat;
3643 aptr = SvPV(fromstr, fromlen);
3648 SvCUR(cat) += (len+7)/8;
3649 SvGROW(cat, SvCUR(cat) + 1);
3650 aptr = SvPVX(cat) + aint;
3655 if (datumtype == 'B') {
3656 for (len = 0; len++ < aint;) {
3657 items |= *pat++ & 1;
3661 *aptr++ = items & 0xff;
3667 for (len = 0; len++ < aint;) {
3673 *aptr++ = items & 0xff;
3679 if (datumtype == 'B')
3680 items <<= 7 - (aint & 7);
3682 items >>= 7 - (aint & 7);
3683 *aptr++ = items & 0xff;
3685 pat = SvPVX(cat) + SvCUR(cat);
3696 char *savepat = pat;
3701 aptr = SvPV(fromstr, fromlen);
3706 SvCUR(cat) += (len+1)/2;
3707 SvGROW(cat, SvCUR(cat) + 1);
3708 aptr = SvPVX(cat) + aint;
3713 if (datumtype == 'H') {
3714 for (len = 0; len++ < aint;) {
3716 items |= ((*pat++ & 15) + 9) & 15;
3718 items |= *pat++ & 15;
3722 *aptr++ = items & 0xff;
3728 for (len = 0; len++ < aint;) {
3730 items |= (((*pat++ & 15) + 9) & 15) << 4;
3732 items |= (*pat++ & 15) << 4;
3736 *aptr++ = items & 0xff;
3742 *aptr++ = items & 0xff;
3743 pat = SvPVX(cat) + SvCUR(cat);
3755 aint = SvIV(fromstr);
3757 sv_catpvn(cat, &achar, sizeof(char));
3760 /* Float and double added by gnb@melba.bby.oz.au 22/11/89 */
3765 afloat = (float)SvNV(fromstr);
3766 sv_catpvn(cat, (char *)&afloat, sizeof (float));
3773 adouble = (double)SvNV(fromstr);
3774 sv_catpvn(cat, (char *)&adouble, sizeof (double));
3780 ashort = (I16)SvIV(fromstr);
3782 ashort = htons(ashort);
3784 CAT16(cat, &ashort);
3790 ashort = (I16)SvIV(fromstr);
3792 ashort = htovs(ashort);
3794 CAT16(cat, &ashort);
3801 ashort = (I16)SvIV(fromstr);
3802 CAT16(cat, &ashort);
3808 auint = SvUV(fromstr);
3809 sv_catpvn(cat, (char*)&auint, sizeof(unsigned int));
3815 adouble = floor(SvNV(fromstr));
3818 croak("Cannot compress negative numbers");
3824 #ifdef CXUX_BROKEN_CONSTANT_CONVERT
3825 adouble <= UV_MAX_cxux
3832 char buf[1 + sizeof(UV)];
3833 char *in = buf + sizeof(buf);
3834 UV auv = U_V(adouble);;
3837 *--in = (auv & 0x7f) | 0x80;
3840 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
3841 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
3843 else if (SvPOKp(fromstr)) { /* decimal string arithmetics */
3844 char *from, *result, *in;
3849 /* Copy string and check for compliance */
3850 from = SvPV(fromstr, len);
3851 if ((norm = is_an_int(from, len)) == NULL)
3852 croak("can compress only unsigned integer");
3854 New('w', result, len, char);
3858 *--in = div128(norm, &done) | 0x80;
3859 result[len - 1] &= 0x7F; /* clear continue bit */
3860 sv_catpvn(cat, in, (result + len) - in);
3862 SvREFCNT_dec(norm); /* free norm */
3864 else if (SvNOKp(fromstr)) {
3865 char buf[sizeof(double) * 2]; /* 8/7 <= 2 */
3866 char *in = buf + sizeof(buf);
3869 double next = floor(adouble / 128);
3870 *--in = (unsigned char)(adouble - (next * 128)) | 0x80;
3871 if (--in < buf) /* this cannot happen ;-) */
3872 croak ("Cannot compress integer");
3874 } while (adouble > 0);
3875 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
3876 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
3879 croak("Cannot compress non integer");
3885 aint = SvIV(fromstr);
3886 sv_catpvn(cat, (char*)&aint, sizeof(int));
3892 aulong = SvUV(fromstr);
3894 aulong = htonl(aulong);
3896 CAT32(cat, &aulong);
3902 aulong = SvUV(fromstr);
3904 aulong = htovl(aulong);
3906 CAT32(cat, &aulong);
3912 aulong = SvUV(fromstr);
3913 CAT32(cat, &aulong);
3919 along = SvIV(fromstr);
3927 auquad = (unsigned Quad_t)SvIV(fromstr);
3928 sv_catpvn(cat, (char*)&auquad, sizeof(unsigned Quad_t));
3934 aquad = (Quad_t)SvIV(fromstr);
3935 sv_catpvn(cat, (char*)&aquad, sizeof(Quad_t));
3938 #endif /* HAS_QUAD */
3940 len = 1; /* assume SV is correct length */
3945 if (fromstr == &sv_undef)
3948 /* XXX better yet, could spirit away the string to
3949 * a safe spot and hang on to it until the result
3950 * of pack() (and all copies of the result) are
3953 if (dowarn && (SvTEMP(fromstr) || SvPADTMP(fromstr)))
3954 warn("Attempt to pack pointer to temporary value");
3955 if (SvPOK(fromstr) || SvNIOK(fromstr))
3956 aptr = SvPV(fromstr,na);
3958 aptr = SvPV_force(fromstr,na);
3960 sv_catpvn(cat, (char*)&aptr, sizeof(char*));
3965 aptr = SvPV(fromstr, fromlen);
3966 SvGROW(cat, fromlen * 4 / 3);
3971 while (fromlen > 0) {
3978 doencodes(cat, aptr, todo);
3996 register I32 limit = POPi; /* note, negative is forever */
3999 register char *s = SvPV(sv, len);
4000 char *strend = s + len;
4002 register REGEXP *rx;
4006 I32 maxiters = (strend - s) + 10;
4009 I32 origlimit = limit;
4012 AV *oldstack = curstack;
4013 I32 gimme = GIMME_V;
4014 I32 oldsave = savestack_ix;
4017 Copy(&LvTARGOFF(POPs), &pm, 1, PMOP*);
4022 DIE("panic: do_split");
4023 rx = pm->op_pmregexp;
4025 TAINT_IF((pm->op_pmflags & PMf_LOCALE) &&
4026 (pm->op_pmflags & (PMf_WHITE | PMf_SKIPWHITE)));
4028 if (pm->op_pmreplroot)
4029 ary = GvAVn((GV*)pm->op_pmreplroot);
4030 else if (gimme != G_ARRAY)
4034 if (ary && (gimme != G_ARRAY || (pm->op_pmflags & PMf_ONCE))) {
4038 for (i = AvFILL(ary); i >= 0; i--)
4039 AvARRAY(ary)[i] = &sv_undef; /* don't free mere refs */
4043 /* temporarily switch stacks */
4044 SWITCHSTACK(curstack, ary);
4046 base = SP - stack_base;
4048 if (pm->op_pmflags & PMf_SKIPWHITE) {
4049 if (pm->op_pmflags & PMf_LOCALE) {
4050 while (isSPACE_LC(*s))
4058 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
4060 multiline = pm->op_pmflags & PMf_MULTILINE;
4064 limit = maxiters + 2;
4065 if (pm->op_pmflags & PMf_WHITE) {
4068 while (m < strend &&
4069 !((pm->op_pmflags & PMf_LOCALE)
4070 ? isSPACE_LC(*m) : isSPACE(*m)))
4075 dstr = NEWSV(30, m-s);
4076 sv_setpvn(dstr, s, m-s);
4082 while (s < strend &&
4083 ((pm->op_pmflags & PMf_LOCALE)
4084 ? isSPACE_LC(*s) : isSPACE(*s)))
4088 else if (strEQ("^", rx->precomp)) {
4091 for (m = s; m < strend && *m != '\n'; m++) ;
4095 dstr = NEWSV(30, m-s);
4096 sv_setpvn(dstr, s, m-s);
4103 else if (pm->op_pmshort && !rx->nparens) {
4104 i = SvCUR(pm->op_pmshort);
4106 i = *SvPVX(pm->op_pmshort);
4109 for (m = s; m < strend && *m != i; m++) ;
4112 dstr = NEWSV(30, m-s);
4113 sv_setpvn(dstr, s, m-s);
4122 while (s < strend && --limit &&
4123 (m=fbm_instr((unsigned char*)s, (unsigned char*)strend,
4127 dstr = NEWSV(31, m-s);
4128 sv_setpvn(dstr, s, m-s);
4137 maxiters += (strend - s) * rx->nparens;
4138 while (s < strend && --limit &&
4139 pregexec(rx, s, strend, orig, 1, Nullsv, TRUE))
4141 TAINT_IF(rx->exec_tainted);
4143 && rx->subbase != orig) {
4148 strend = s + (strend - m);
4151 dstr = NEWSV(32, m-s);
4152 sv_setpvn(dstr, s, m-s);
4157 for (i = 1; i <= rx->nparens; i++) {
4161 dstr = NEWSV(33, m-s);
4162 sv_setpvn(dstr, s, m-s);
4165 dstr = NEWSV(33, 0);
4174 LEAVE_SCOPE(oldsave);
4175 iters = (SP - stack_base) - base;
4176 if (iters > maxiters)
4179 /* keep field after final delim? */
4180 if (s < strend || (iters && origlimit)) {
4181 dstr = NEWSV(34, strend-s);
4182 sv_setpvn(dstr, s, strend-s);
4188 else if (!origlimit) {
4189 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0))
4193 SWITCHSTACK(ary, oldstack);
4194 if (SvSMAGICAL(ary)) {
4199 if (gimme == G_ARRAY) {
4201 Copy(AvARRAY(ary), SP + 1, iters, SV*);
4207 if (gimme == G_ARRAY)
4210 if (iters || !pm->op_pmreplroot) {