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);
443 /* Pattern matching */
448 register unsigned char *s;
451 register I32 *sfirst;
455 if (sv == lastscream) {
461 SvSCREAM_off(lastscream);
462 SvREFCNT_dec(lastscream);
464 lastscream = SvREFCNT_inc(sv);
467 s = (unsigned char*)(SvPV(sv, len));
471 if (pos > maxscream) {
473 maxscream = pos + 80;
474 New(301, screamfirst, 256, I32);
475 New(302, screamnext, maxscream, I32);
478 maxscream = pos + pos / 4;
479 Renew(screamnext, maxscream, I32);
483 sfirst = screamfirst;
486 if (!sfirst || !snext)
487 DIE("do_study: out of memory");
489 for (ch = 256; ch; --ch)
496 snext[pos] = sfirst[ch] - pos;
503 sv_magic(sv, Nullsv, 'g', Nullch, 0); /* piggyback on m//g magic */
512 if (op->op_flags & OPf_STACKED)
518 TARG = sv_newmortal();
519 PUSHi(do_trans(sv, op));
523 /* Lvalue operators. */
545 SETi(do_chomp(TOPs));
552 register I32 count = 0;
555 count += do_chomp(POPs);
566 if (!sv || !SvANY(sv))
568 switch (SvTYPE(sv)) {
570 if (AvMAX(sv) >= 0 || SvRMAGICAL(sv))
574 if (HvARRAY(sv) || SvRMAGICAL(sv))
578 if (CvROOT(sv) || CvXSUB(sv))
595 if (!op->op_private) {
604 if (SvTHINKFIRST(sv)) {
611 switch (SvTYPE(sv)) {
621 if (cv_const_sv((CV*)sv))
622 warn("Constant subroutine %s undefined",
623 CvANON((CV*)sv) ? "(anonymous)" : GvENAME(CvGV((CV*)sv)));
626 { GV* gv = (GV*)SvREFCNT_inc(CvGV((CV*)sv));
628 CvGV((CV*)sv) = gv; } /* let user-undef'd sub keep its identity */
632 sv_setsv(sv, &sv_undef);
635 if (SvTYPE(sv) >= SVt_PV && SvPVX(sv) && SvLEN(sv)) {
638 SvPV_set(sv, Nullch);
651 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
653 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
654 SvIVX(TOPs) != IV_MIN)
657 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
668 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
670 sv_setsv(TARG, TOPs);
671 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
672 SvIVX(TOPs) != IV_MAX)
675 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
689 if(SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
691 sv_setsv(TARG, TOPs);
692 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
693 SvIVX(TOPs) != IV_MIN)
696 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
705 /* Ordinary operators. */
709 dSP; dATARGET; tryAMAGICbin(pow,opASSIGN);
712 SETn( pow( left, right) );
719 dSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
722 SETn( left * right );
729 dSP; dATARGET; tryAMAGICbin(div,opASSIGN);
734 DIE("Illegal division by zero");
736 /* insure that 20./5. == 4. */
739 if ((double)I_V(left) == left &&
740 (double)I_V(right) == right &&
741 (k = I_V(left)/I_V(right))*I_V(right) == I_V(left)) {
744 value = left / right;
748 value = left / right;
757 dSP; dATARGET; tryAMAGICbin(mod,opASSIGN);
765 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)) {
767 right = (right_neg = (i < 0)) ? -i : i;
771 right = U_V((right_neg = (n < 0)) ? -n : n);
774 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)) {
776 left = (left_neg = (i < 0)) ? -i : i;
780 left = U_V((left_neg = (n < 0)) ? -n : n);
784 DIE("Illegal modulus zero");
787 if ((left_neg != right_neg) && ans)
790 /* XXX may warn: unary minus operator applied to unsigned type */
791 /* could change -foo to be (~foo)+1 instead */
792 if (ans <= -(UV)IV_MAX)
793 sv_setiv(TARG, (IV) -ans);
795 sv_setnv(TARG, -(double)ans);
806 dSP; dATARGET; tryAMAGICbin(repeat,opASSIGN);
808 register I32 count = POPi;
809 if (GIMME == G_ARRAY && op->op_private & OPpREPEAT_DOLIST) {
811 I32 items = SP - MARK;
823 repeatcpy((char*)(MARK + items), (char*)MARK,
824 items * sizeof(SV*), count - 1);
830 else { /* Note: mark already snarfed by pp_list */
835 if (TARG == tmpstr && SvTHINKFIRST(tmpstr)) {
836 if (SvREADONLY(tmpstr) && curcop != &compiling)
837 DIE("Can't x= to readonly value");
841 SvSetSV(TARG, tmpstr);
842 SvPV_force(TARG, len);
847 SvGROW(TARG, (count * len) + 1);
848 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
849 SvCUR(TARG) *= count;
853 (void)SvPOK_only(TARG);
862 dSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
865 SETn( left - right );
872 dSP; dATARGET; tryAMAGICbin(lshift,opASSIGN);
875 if (op->op_private & HINT_INTEGER) {
891 dSP; dATARGET; tryAMAGICbin(rshift,opASSIGN);
894 if (op->op_private & HINT_INTEGER) {
910 dSP; tryAMAGICbinSET(lt,0);
913 SETs(boolSV(TOPn < value));
920 dSP; tryAMAGICbinSET(gt,0);
923 SETs(boolSV(TOPn > value));
930 dSP; tryAMAGICbinSET(le,0);
933 SETs(boolSV(TOPn <= value));
940 dSP; tryAMAGICbinSET(ge,0);
943 SETs(boolSV(TOPn >= value));
950 dSP; tryAMAGICbinSET(ne,0);
953 SETs(boolSV(TOPn != value));
960 dSP; dTARGET; tryAMAGICbin(ncmp,0);
967 else if (left < right)
969 else if (left > right)
982 dSP; tryAMAGICbinSET(slt,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(sgt,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(sle,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(sge,0);
1024 int cmp = ((op->op_private & OPpLOCALE)
1025 ? sv_cmp_locale(left, right)
1026 : sv_cmp(left, right));
1027 SETs(boolSV(cmp >= 0));
1034 dSP; tryAMAGICbinSET(seq,0);
1037 SETs(boolSV(sv_eq(left, right)));
1044 dSP; tryAMAGICbinSET(sne,0);
1047 SETs(boolSV(!sv_eq(left, right)));
1054 dSP; dTARGET; tryAMAGICbin(scmp,0);
1057 int cmp = ((op->op_private & OPpLOCALE)
1058 ? sv_cmp_locale(left, right)
1059 : sv_cmp(left, right));
1067 dSP; dATARGET; tryAMAGICbin(band,opASSIGN);
1070 if (SvNIOKp(left) || SvNIOKp(right)) {
1071 if (op->op_private & HINT_INTEGER) {
1072 IBW value = SvIV(left) & SvIV(right);
1076 UBW value = SvUV(left) & SvUV(right);
1081 do_vop(op->op_type, TARG, left, right);
1090 dSP; dATARGET; tryAMAGICbin(bxor,opASSIGN);
1093 if (SvNIOKp(left) || SvNIOKp(right)) {
1094 if (op->op_private & HINT_INTEGER) {
1095 IBW value = (USE_LEFT(left) ? SvIV(left) : 0) ^ SvIV(right);
1099 UBW value = (USE_LEFT(left) ? SvUV(left) : 0) ^ SvUV(right);
1104 do_vop(op->op_type, TARG, left, right);
1113 dSP; dATARGET; tryAMAGICbin(bor,opASSIGN);
1116 if (SvNIOKp(left) || SvNIOKp(right)) {
1117 if (op->op_private & HINT_INTEGER) {
1118 IBW value = (USE_LEFT(left) ? SvIV(left) : 0) | SvIV(right);
1122 UBW value = (USE_LEFT(left) ? SvUV(left) : 0) | SvUV(right);
1127 do_vop(op->op_type, TARG, left, right);
1136 dSP; dTARGET; tryAMAGICun(neg);
1141 if (SvIOKp(sv) && !SvNOKp(sv) && !SvPOKp(sv) && SvIVX(sv) != IV_MIN)
1143 else if (SvNIOKp(sv))
1145 else if (SvPOKp(sv)) {
1147 char *s = SvPV(sv, len);
1148 if (isIDFIRST(*s)) {
1149 sv_setpvn(TARG, "-", 1);
1152 else if (*s == '+' || *s == '-') {
1154 *SvPV_force(TARG, len) = *s == '-' ? '+' : '-';
1157 sv_setnv(TARG, -SvNV(sv));
1169 dSP; tryAMAGICunSET(not);
1170 #endif /* OVERLOAD */
1171 *stack_sp = boolSV(!SvTRUE(*stack_sp));
1177 dSP; dTARGET; tryAMAGICun(compl);
1181 if (op->op_private & HINT_INTEGER) {
1182 IBW value = ~SvIV(sv);
1186 UBW value = ~SvUV(sv);
1191 register char *tmps;
1192 register long *tmpl;
1197 tmps = SvPV_force(TARG, len);
1200 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
1203 for ( ; anum >= sizeof(long); anum -= sizeof(long), tmpl++)
1207 for ( ; anum > 0; anum--, tmps++)
1216 /* integer versions of some of the above */
1220 dSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
1223 SETi( left * right );
1230 dSP; dATARGET; tryAMAGICbin(div,opASSIGN);
1234 DIE("Illegal division by zero");
1235 value = POPi / value;
1243 dSP; dATARGET; tryAMAGICbin(mod,opASSIGN);
1247 DIE("Illegal modulus zero");
1248 SETi( left % right );
1255 dSP; dATARGET; tryAMAGICbin(add,opASSIGN);
1258 SETi( left + right );
1265 dSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
1268 SETi( left - right );
1275 dSP; tryAMAGICbinSET(lt,0);
1278 SETs(boolSV(left < right));
1285 dSP; tryAMAGICbinSET(gt,0);
1288 SETs(boolSV(left > right));
1295 dSP; tryAMAGICbinSET(le,0);
1298 SETs(boolSV(left <= right));
1305 dSP; tryAMAGICbinSET(ge,0);
1308 SETs(boolSV(left >= right));
1315 dSP; tryAMAGICbinSET(eq,0);
1318 SETs(boolSV(left == right));
1325 dSP; tryAMAGICbinSET(ne,0);
1328 SETs(boolSV(left != right));
1335 dSP; dTARGET; tryAMAGICbin(ncmp,0);
1342 else if (left < right)
1353 dSP; dTARGET; tryAMAGICun(neg);
1358 /* High falutin' math. */
1362 dSP; dTARGET; tryAMAGICbin(atan2,0);
1365 SETn(atan2(left, right));
1372 dSP; dTARGET; tryAMAGICun(sin);
1384 dSP; dTARGET; tryAMAGICun(cos);
1404 if (!srand_called) {
1405 (void)srand((unsigned)seed());
1406 srand_called = TRUE;
1409 value = rand() * value / 2147483648.0;
1412 value = rand() * value / 65536.0;
1415 value = rand() * value / 32768.0;
1417 value = rand() * value / (double)(((unsigned long)1) << RANDBITS);
1433 (void)srand((unsigned)anum);
1434 srand_called = TRUE;
1443 * This is really just a quick hack which grabs various garbage
1444 * values. It really should be a real hash algorithm which
1445 * spreads the effect of every input bit onto every output bit,
1446 * if someone who knows about such tings would bother to write it.
1447 * Might be a good idea to add that function to CORE as well.
1448 * No numbers below come from careful analysis or anyting here,
1449 * except they are primes and SEED_C1 > 1E6 to get a full-width
1450 * value from (tv_sec * SEED_C1 + tv_usec). The multipliers should
1451 * probably be bigger too.
1454 # define SEED_C1 1000003
1455 #define SEED_C4 73819
1457 # define SEED_C1 25747
1458 #define SEED_C4 20639
1462 #define SEED_C5 26107
1466 # include <starlet.h>
1467 /* when[] = (low 32 bits, high 32 bits) of time since epoch
1468 * in 100-ns units, typically incremented ever 10 ms. */
1469 unsigned int when[2];
1470 _ckvmssts(sys$gettim(when));
1471 u = (U32)SEED_C1 * when[0] + (U32)SEED_C2 * when[1];
1473 # ifdef HAS_GETTIMEOFDAY
1474 struct timeval when;
1475 gettimeofday(&when,(struct timezone *) 0);
1476 u = (U32)SEED_C1 * when.tv_sec + (U32)SEED_C2 * when.tv_usec;
1480 u = (U32)SEED_C1 * when;
1483 u += SEED_C3 * (U32)getpid();
1484 u += SEED_C4 * (U32)(UV)stack_sp;
1485 #ifndef PLAN9 /* XXX Plan9 assembler chokes on this; fix needed */
1486 u += SEED_C5 * (U32)(UV)&when;
1493 dSP; dTARGET; tryAMAGICun(exp);
1505 dSP; dTARGET; tryAMAGICun(log);
1510 SET_NUMERIC_STANDARD();
1511 DIE("Can't take log of %g", value);
1521 dSP; dTARGET; tryAMAGICun(sqrt);
1526 SET_NUMERIC_STANDARD();
1527 DIE("Can't take sqrt of %g", value);
1529 value = sqrt(value);
1539 double value = TOPn;
1542 if (SvIOKp(TOPs) && !SvNOKp(TOPs) && !SvPOKp(TOPs)) {
1548 (void)modf(value, &value);
1550 (void)modf(-value, &value);
1565 dSP; dTARGET; tryAMAGICun(abs);
1567 double value = TOPn;
1570 if (SvIOKp(TOPs) && !SvNOKp(TOPs) && !SvPOKp(TOPs) &&
1571 (iv = SvIVX(TOPs)) != IV_MIN) {
1592 XPUSHu(scan_hex(tmps, 99, &argtype));
1604 while (*tmps && isSPACE(*tmps))
1609 value = scan_hex(++tmps, 99, &argtype);
1611 value = scan_oct(tmps, 99, &argtype);
1621 SETi( sv_len(TOPs) );
1633 I32 lvalue = op->op_flags & OPf_MOD;
1635 I32 arybase = curcop->cop_arybase;
1639 pos = POPi - arybase;
1641 tmps = SvPV(sv, curlen);
1643 pos += curlen + arybase;
1644 if (pos < 0 || pos > curlen) {
1645 if (dowarn || lvalue)
1646 warn("substr outside of string");
1653 len += curlen - pos;
1658 rem = curlen - pos; /* rem=how many bytes left*/
1661 sv_setpvn(TARG, tmps, rem);
1662 if (lvalue) { /* it's an lvalue! */
1663 if (!SvGMAGICAL(sv)) {
1667 warn("Attempt to use reference as lvalue in substr");
1669 if (SvOK(sv)) /* is it defined ? */
1670 (void)SvPOK_only(sv);
1672 sv_setpvn(sv,"",0); /* avoid lexical reincarnation */
1675 if (SvTYPE(TARG) < SVt_PVLV) {
1676 sv_upgrade(TARG, SVt_PVLV);
1677 sv_magic(TARG, Nullsv, 'x', Nullch, 0);
1682 LvTARGOFF(TARG) = pos;
1683 LvTARGLEN(TARG) = rem;
1686 PUSHs(TARG); /* avoid SvSETMAGIC here */
1693 register I32 size = POPi;
1694 register I32 offset = POPi;
1695 register SV *src = POPs;
1696 I32 lvalue = op->op_flags & OPf_MOD;
1698 unsigned char *s = (unsigned char*)SvPV(src, srclen);
1699 unsigned long retnum;
1702 offset *= size; /* turn into bit offset */
1703 len = (offset + size + 7) / 8;
1704 if (offset < 0 || size < 1)
1707 if (lvalue) { /* it's an lvalue! */
1708 if (SvTYPE(TARG) < SVt_PVLV) {
1709 sv_upgrade(TARG, SVt_PVLV);
1710 sv_magic(TARG, Nullsv, 'v', Nullch, 0);
1715 LvTARGOFF(TARG) = offset;
1716 LvTARGLEN(TARG) = size;
1724 if (offset >= srclen)
1727 retnum = (unsigned long) s[offset] << 8;
1729 else if (size == 32) {
1730 if (offset >= srclen)
1732 else if (offset + 1 >= srclen)
1733 retnum = (unsigned long) s[offset] << 24;
1734 else if (offset + 2 >= srclen)
1735 retnum = ((unsigned long) s[offset] << 24) +
1736 ((unsigned long) s[offset + 1] << 16);
1738 retnum = ((unsigned long) s[offset] << 24) +
1739 ((unsigned long) s[offset + 1] << 16) +
1740 (s[offset + 2] << 8);
1745 retnum = (s[offset >> 3] >> (offset & 7)) & ((1 << size) - 1);
1750 else if (size == 16)
1751 retnum = ((unsigned long) s[offset] << 8) + s[offset+1];
1752 else if (size == 32)
1753 retnum = ((unsigned long) s[offset] << 24) +
1754 ((unsigned long) s[offset + 1] << 16) +
1755 (s[offset + 2] << 8) + s[offset+3];
1759 sv_setiv(TARG, (IV)retnum);
1774 I32 arybase = curcop->cop_arybase;
1779 offset = POPi - arybase;
1782 tmps = SvPV(big, biglen);
1785 else if (offset > biglen)
1787 if (!(tmps2 = fbm_instr((unsigned char*)tmps + offset,
1788 (unsigned char*)tmps + biglen, little)))
1789 retval = -1 + arybase;
1791 retval = tmps2 - tmps + arybase;
1808 I32 arybase = curcop->cop_arybase;
1814 tmps2 = SvPV(little, llen);
1815 tmps = SvPV(big, blen);
1819 offset = SvIV(offstr) - arybase + llen;
1822 else if (offset > blen)
1824 if (!(tmps2 = rninstr(tmps, tmps + offset,
1825 tmps2, tmps2 + llen)))
1826 retval = -1 + arybase;
1828 retval = tmps2 - tmps + arybase;
1835 dSP; dMARK; dORIGMARK; dTARGET;
1836 #ifdef USE_LOCALE_NUMERIC
1837 if (op->op_private & OPpLOCALE)
1838 SET_NUMERIC_LOCAL();
1840 SET_NUMERIC_STANDARD();
1842 do_sprintf(TARG, SP-MARK, MARK+1);
1843 TAINT_IF(SvTAINTED(TARG));
1857 value = (I32) (*tmps & 255);
1862 value = (I32) (anum & 255);
1873 (void)SvUPGRADE(TARG,SVt_PV);
1879 (void)SvPOK_only(TARG);
1886 dSP; dTARGET; dPOPTOPssrl;
1888 char *tmps = SvPV(left, na);
1890 sv_setpv(TARG, fcrypt(tmps, SvPV(right, na)));
1892 sv_setpv(TARG, crypt(tmps, SvPV(right, na)));
1896 "The crypt() function is unimplemented due to excessive paranoia.");
1908 if (!SvPADTMP(sv)) {
1914 s = SvPV_force(sv, na);
1916 if (op->op_private & OPpLOCALE) {
1919 *s = toUPPER_LC(*s);
1934 if (!SvPADTMP(sv)) {
1940 s = SvPV_force(sv, na);
1942 if (op->op_private & OPpLOCALE) {
1945 *s = toLOWER_LC(*s);
1962 if (!SvPADTMP(sv)) {
1969 s = SvPV_force(sv, len);
1971 register char *send = s + len;
1973 if (op->op_private & OPpLOCALE) {
1976 for (; s < send; s++)
1977 *s = toUPPER_LC(*s);
1980 for (; s < send; s++)
1994 if (!SvPADTMP(sv)) {
2001 s = SvPV_force(sv, len);
2003 register char *send = s + len;
2005 if (op->op_private & OPpLOCALE) {
2008 for (; s < send; s++)
2009 *s = toLOWER_LC(*s);
2012 for (; s < send; s++)
2024 register char *s = SvPV(sv,len);
2028 (void)SvUPGRADE(TARG, SVt_PV);
2029 SvGROW(TARG, (len * 2) + 1);
2037 SvCUR_set(TARG, d - SvPVX(TARG));
2038 (void)SvPOK_only(TARG);
2041 sv_setpvn(TARG, s, len);
2050 dSP; dMARK; dORIGMARK;
2052 register AV* av = (AV*)POPs;
2053 register I32 lval = op->op_flags & OPf_MOD;
2054 I32 arybase = curcop->cop_arybase;
2057 if (SvTYPE(av) == SVt_PVAV) {
2058 if (lval && op->op_private & OPpLVAL_INTRO) {
2060 for (svp = mark + 1; svp <= sp; svp++) {
2065 if (max > AvMAX(av))
2068 while (++MARK <= SP) {
2069 elem = SvIVx(*MARK);
2073 svp = av_fetch(av, elem, lval);
2075 if (!svp || *svp == &sv_undef)
2076 DIE(no_aelem, elem);
2077 if (op->op_private & OPpLVAL_INTRO)
2080 *MARK = svp ? *svp : &sv_undef;
2083 if (GIMME != G_ARRAY) {
2091 /* Associative arrays. */
2096 HV *hash = (HV*)POPs;
2098 I32 gimme = GIMME_V;
2101 entry = hv_iternext(hash); /* might clobber stack_sp */
2106 PUSHs(hv_iterkeysv(entry)); /* won't clobber stack_sp */
2107 if (gimme == G_ARRAY) {
2109 sv_setsv(TARG, hv_iterval(hash, entry)); /* might hit stack_sp */
2114 else if (gimme == G_SCALAR)
2133 I32 gimme = GIMME_V;
2134 I32 discard = (gimme == G_VOID) ? G_DISCARD : 0;
2138 if (op->op_private & OPpSLICE) {
2141 if (SvTYPE(hv) != SVt_PVHV)
2142 DIE("Not a HASH reference");
2143 while (++MARK <= SP) {
2144 sv = hv_delete_ent(hv, *MARK, discard, 0);
2145 *MARK = sv ? sv : &sv_undef;
2149 else if (gimme == G_SCALAR) {
2158 if (SvTYPE(hv) != SVt_PVHV)
2159 DIE("Not a HASH reference");
2160 sv = hv_delete_ent(hv, keysv, discard, 0);
2175 if (SvTYPE(hv) != SVt_PVHV) {
2176 DIE("Not a HASH reference");
2178 if (hv_exists_ent(hv, tmpsv, 0))
2185 dSP; dMARK; dORIGMARK;
2187 register HV *hv = (HV*)POPs;
2188 register I32 lval = op->op_flags & OPf_MOD;
2190 if (SvTYPE(hv) == SVt_PVHV) {
2191 while (++MARK <= SP) {
2194 he = hv_fetch_ent(hv, keysv, lval, 0);
2196 if (!he || HeVAL(he) == &sv_undef)
2197 DIE(no_helem, SvPV(keysv, na));
2198 if (op->op_private & OPpLVAL_INTRO)
2199 save_svref(&HeVAL(he));
2201 *MARK = he ? HeVAL(he) : &sv_undef;
2204 if (GIMME != G_ARRAY) {
2212 /* List operators. */
2217 if (GIMME != G_ARRAY) {
2219 *MARK = *SP; /* unwanted list, return last item */
2230 SV **lastrelem = stack_sp;
2231 SV **lastlelem = stack_base + POPMARK;
2232 SV **firstlelem = stack_base + POPMARK + 1;
2233 register SV **firstrelem = lastlelem + 1;
2234 I32 arybase = curcop->cop_arybase;
2235 I32 lval = op->op_flags & OPf_MOD;
2236 I32 is_something_there = lval;
2238 register I32 max = lastrelem - lastlelem;
2239 register SV **lelem;
2242 if (GIMME != G_ARRAY) {
2243 ix = SvIVx(*lastlelem);
2248 if (ix < 0 || ix >= max)
2249 *firstlelem = &sv_undef;
2251 *firstlelem = firstrelem[ix];
2257 SP = firstlelem - 1;
2261 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
2267 else if (!(*lelem = firstrelem[ix]))
2272 if (ix >= max || !(*lelem = firstrelem[ix]))
2275 if (!is_something_there && (SvOK(*lelem) || SvGMAGICAL(*lelem)))
2276 is_something_there = TRUE;
2278 if (is_something_there)
2281 SP = firstlelem - 1;
2287 dSP; dMARK; dORIGMARK;
2288 I32 items = SP - MARK;
2289 SV *av = sv_2mortal((SV*)av_make(items, MARK+1));
2290 SP = ORIGMARK; /* av_make() might realloc stack_sp */
2297 dSP; dMARK; dORIGMARK;
2298 HV* hv = (HV*)sv_2mortal((SV*)newHV());
2302 SV *val = NEWSV(46, 0);
2304 sv_setsv(val, *++MARK);
2306 warn("Odd number of elements in hash list");
2307 (void)hv_store_ent(hv,key,val,0);
2316 dSP; dMARK; dORIGMARK;
2317 register AV *ary = (AV*)*++MARK;
2321 register I32 offset;
2322 register I32 length;
2331 offset = SvIVx(*MARK);
2333 offset += AvFILL(ary) + 1;
2335 offset -= curcop->cop_arybase;
2337 length = SvIVx(*MARK++);
2342 length = AvMAX(ary) + 1; /* close enough to infinity */
2346 length = AvMAX(ary) + 1;
2354 if (offset > AvFILL(ary) + 1)
2355 offset = AvFILL(ary) + 1;
2356 after = AvFILL(ary) + 1 - (offset + length);
2357 if (after < 0) { /* not that much array */
2358 length += after; /* offset+length now in array */
2364 /* At this point, MARK .. SP-1 is our new LIST */
2367 diff = newlen - length;
2369 if (diff < 0) { /* shrinking the area */
2371 New(451, tmparyval, newlen, SV*); /* so remember insertion */
2372 Copy(MARK, tmparyval, newlen, SV*);
2375 MARK = ORIGMARK + 1;
2376 if (GIMME == G_ARRAY) { /* copy return vals to stack */
2377 MEXTEND(MARK, length);
2378 Copy(AvARRAY(ary)+offset, MARK, length, SV*);
2380 EXTEND_MORTAL(length);
2381 for (i = length, dst = MARK; i; i--) {
2382 if (!SvIMMORTAL(*dst))
2383 sv_2mortal(*dst); /* free them eventualy */
2390 *MARK = AvARRAY(ary)[offset+length-1];
2392 if (!SvIMMORTAL(*MARK))
2394 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
2395 SvREFCNT_dec(*dst++); /* free them now */
2398 AvFILL(ary) += diff;
2400 /* pull up or down? */
2402 if (offset < after) { /* easier to pull up */
2403 if (offset) { /* esp. if nothing to pull */
2404 src = &AvARRAY(ary)[offset-1];
2405 dst = src - diff; /* diff is negative */
2406 for (i = offset; i > 0; i--) /* can't trust Copy */
2410 SvPVX(ary) = (char*)(AvARRAY(ary) - diff); /* diff is negative */
2414 if (after) { /* anything to pull down? */
2415 src = AvARRAY(ary) + offset + length;
2416 dst = src + diff; /* diff is negative */
2417 Move(src, dst, after, SV*);
2419 dst = &AvARRAY(ary)[AvFILL(ary)+1];
2420 /* avoid later double free */
2424 dst[--i] = &sv_undef;
2427 for (src = tmparyval, dst = AvARRAY(ary) + offset;
2429 *dst = NEWSV(46, 0);
2430 sv_setsv(*dst++, *src++);
2432 Safefree(tmparyval);
2435 else { /* no, expanding (or same) */
2437 New(452, tmparyval, length, SV*); /* so remember deletion */
2438 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
2441 if (diff > 0) { /* expanding */
2443 /* push up or down? */
2445 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
2449 Move(src, dst, offset, SV*);
2451 SvPVX(ary) = (char*)(AvARRAY(ary) - diff);/* diff is positive */
2453 AvFILL(ary) += diff;
2456 if (AvFILL(ary) + diff >= AvMAX(ary)) /* oh, well */
2457 av_extend(ary, AvFILL(ary) + diff);
2458 AvFILL(ary) += diff;
2461 dst = AvARRAY(ary) + AvFILL(ary);
2463 for (i = after; i; i--) {
2470 for (src = MARK, dst = AvARRAY(ary) + offset; newlen; newlen--) {
2471 *dst = NEWSV(46, 0);
2472 sv_setsv(*dst++, *src++);
2474 MARK = ORIGMARK + 1;
2475 if (GIMME == G_ARRAY) { /* copy return vals to stack */
2477 Copy(tmparyval, MARK, length, SV*);
2479 EXTEND_MORTAL(length);
2480 for (i = length, dst = MARK; i; i--) {
2481 if (!SvIMMORTAL(*dst))
2482 sv_2mortal(*dst); /* free them eventualy */
2486 Safefree(tmparyval);
2490 else if (length--) {
2491 *MARK = tmparyval[length];
2493 if (!SvIMMORTAL(*MARK))
2495 while (length-- > 0)
2496 SvREFCNT_dec(tmparyval[length]);
2498 Safefree(tmparyval);
2509 dSP; dMARK; dORIGMARK; dTARGET;
2510 register AV *ary = (AV*)*++MARK;
2511 register SV *sv = &sv_undef;
2513 for (++MARK; MARK <= SP; MARK++) {
2516 sv_setsv(sv, *MARK);
2520 PUSHi( AvFILL(ary) + 1 );
2528 SV *sv = av_pop(av);
2529 if (!SvIMMORTAL(sv) && AvREAL(av))
2530 (void)sv_2mortal(sv);
2539 SV *sv = av_shift(av);
2543 if (!SvIMMORTAL(sv) && AvREAL(av))
2544 (void)sv_2mortal(sv);
2551 dSP; dMARK; dORIGMARK; dTARGET;
2552 register AV *ary = (AV*)*++MARK;
2556 av_unshift(ary, SP - MARK);
2559 sv_setsv(sv, *++MARK);
2560 (void)av_store(ary, i++, sv);
2564 PUSHi( AvFILL(ary) + 1 );
2574 if (GIMME == G_ARRAY) {
2585 register char *down;
2591 do_join(TARG, &sv_no, MARK, SP);
2593 sv_setsv(TARG, (SP > MARK) ? *SP : GvSV(defgv));
2594 up = SvPV_force(TARG, len);
2596 down = SvPVX(TARG) + len - 1;
2602 (void)SvPOK_only(TARG);
2616 char *s = SvPV(sv, len);
2620 if (!strnEQ(s, "0000", 4)) { /* need to grow sv */
2621 SV *new = newSVpv("0000000000", 10);
2624 SvREFCNT_dec(sv); /* free old sv */
2629 while (!*t) /* trailing '\0'? */
2632 i = ((*t - '0') << 7) + m;
2633 *(t--) = '0' + (i % 10);
2639 /* Explosives and implosives. */
2646 I32 gimme = GIMME_V;
2650 register char *pat = SvPV(left, llen);
2651 register char *s = SvPV(right, rlen);
2652 char *strend = s + rlen;
2654 register char *patend = pat + llen;
2659 /* These must not be in registers: */
2670 unsigned Quad_t auquad;
2676 register U32 culong;
2678 static char* bitcount = 0;
2680 if (gimme != G_ARRAY) { /* arrange to do first one only */
2682 for (patend = pat; !isALPHA(*patend) || *patend == 'x'; patend++) ;
2683 if (strchr("aAbBhHP", *patend) || *pat == '%') {
2685 while (isDIGIT(*patend) || *patend == '*')
2691 while (pat < patend) {
2693 datumtype = *pat++ & 0xFF;
2694 if (isSPACE(datumtype))
2698 else if (*pat == '*') {
2699 len = strend - strbeg; /* long enough */
2702 else if (isDIGIT(*pat)) {
2704 while (isDIGIT(*pat))
2705 len = (len * 10) + (*pat++ - '0');
2708 len = (datumtype != '@');
2711 croak("Invalid type in unpack: '%c'", (int)datumtype);
2713 if (len == 1 && pat[-1] != '1')
2722 if (len > strend - strbeg)
2723 DIE("@ outside of string");
2727 if (len > s - strbeg)
2728 DIE("X outside of string");
2732 if (len > strend - s)
2733 DIE("x outside of string");
2738 if (len > strend - s)
2741 goto uchar_checksum;
2742 sv = NEWSV(35, len);
2743 sv_setpvn(sv, s, len);
2745 if (datumtype == 'A') {
2746 aptr = s; /* borrow register */
2747 s = SvPVX(sv) + len - 1;
2748 while (s >= SvPVX(sv) && (!*s || isSPACE(*s)))
2751 SvCUR_set(sv, s - SvPVX(sv));
2752 s = aptr; /* unborrow register */
2754 XPUSHs(sv_2mortal(sv));
2758 if (pat[-1] == '*' || len > (strend - s) * 8)
2759 len = (strend - s) * 8;
2762 Newz(601, bitcount, 256, char);
2763 for (bits = 1; bits < 256; bits++) {
2764 if (bits & 1) bitcount[bits]++;
2765 if (bits & 2) bitcount[bits]++;
2766 if (bits & 4) bitcount[bits]++;
2767 if (bits & 8) bitcount[bits]++;
2768 if (bits & 16) bitcount[bits]++;
2769 if (bits & 32) bitcount[bits]++;
2770 if (bits & 64) bitcount[bits]++;
2771 if (bits & 128) bitcount[bits]++;
2775 culong += bitcount[*(unsigned char*)s++];
2780 if (datumtype == 'b') {
2782 if (bits & 1) culong++;
2788 if (bits & 128) culong++;
2795 sv = NEWSV(35, len + 1);
2798 aptr = pat; /* borrow register */
2800 if (datumtype == 'b') {
2802 for (len = 0; len < aint; len++) {
2803 if (len & 7) /*SUPPRESS 595*/
2807 *pat++ = '0' + (bits & 1);
2812 for (len = 0; len < aint; len++) {
2817 *pat++ = '0' + ((bits & 128) != 0);
2821 pat = aptr; /* unborrow register */
2822 XPUSHs(sv_2mortal(sv));
2826 if (pat[-1] == '*' || len > (strend - s) * 2)
2827 len = (strend - s) * 2;
2828 sv = NEWSV(35, len + 1);
2831 aptr = pat; /* borrow register */
2833 if (datumtype == 'h') {
2835 for (len = 0; len < aint; len++) {
2840 *pat++ = hexdigit[bits & 15];
2845 for (len = 0; len < aint; len++) {
2850 *pat++ = hexdigit[(bits >> 4) & 15];
2854 pat = aptr; /* unborrow register */
2855 XPUSHs(sv_2mortal(sv));
2858 if (len > strend - s)
2863 if (aint >= 128) /* fake up signed chars */
2873 if (aint >= 128) /* fake up signed chars */
2876 sv_setiv(sv, (IV)aint);
2877 PUSHs(sv_2mortal(sv));
2882 if (len > strend - s)
2897 sv_setiv(sv, (IV)auint);
2898 PUSHs(sv_2mortal(sv));
2903 along = (strend - s) / SIZE16;
2920 sv_setiv(sv, (IV)ashort);
2921 PUSHs(sv_2mortal(sv));
2928 along = (strend - s) / SIZE16;
2933 COPY16(s, &aushort);
2936 if (datumtype == 'n')
2937 aushort = ntohs(aushort);
2940 if (datumtype == 'v')
2941 aushort = vtohs(aushort);
2950 COPY16(s, &aushort);
2954 if (datumtype == 'n')
2955 aushort = ntohs(aushort);
2958 if (datumtype == 'v')
2959 aushort = vtohs(aushort);
2961 sv_setiv(sv, (IV)aushort);
2962 PUSHs(sv_2mortal(sv));
2967 along = (strend - s) / sizeof(int);
2972 Copy(s, &aint, 1, int);
2975 cdouble += (double)aint;
2984 Copy(s, &aint, 1, int);
2987 sv_setiv(sv, (IV)aint);
2988 PUSHs(sv_2mortal(sv));
2993 along = (strend - s) / sizeof(unsigned int);
2998 Copy(s, &auint, 1, unsigned int);
2999 s += sizeof(unsigned int);
3001 cdouble += (double)auint;
3010 Copy(s, &auint, 1, unsigned int);
3011 s += sizeof(unsigned int);
3013 sv_setuv(sv, (UV)auint);
3014 PUSHs(sv_2mortal(sv));
3019 along = (strend - s) / SIZE32;
3027 cdouble += (double)along;
3039 sv_setiv(sv, (IV)along);
3040 PUSHs(sv_2mortal(sv));
3047 along = (strend - s) / SIZE32;
3055 if (datumtype == 'N')
3056 aulong = ntohl(aulong);
3059 if (datumtype == 'V')
3060 aulong = vtohl(aulong);
3063 cdouble += (double)aulong;
3075 if (datumtype == 'N')
3076 aulong = ntohl(aulong);
3079 if (datumtype == 'V')
3080 aulong = vtohl(aulong);
3083 sv_setuv(sv, (UV)aulong);
3084 PUSHs(sv_2mortal(sv));
3089 along = (strend - s) / sizeof(char*);
3095 if (sizeof(char*) > strend - s)
3098 Copy(s, &aptr, 1, char*);
3104 PUSHs(sv_2mortal(sv));
3114 while ((len > 0) && (s < strend)) {
3115 auv = (auv << 7) | (*s & 0x7f);
3116 if (!(*s++ & 0x80)) {
3120 PUSHs(sv_2mortal(sv));
3124 else if (++bytes >= sizeof(UV)) { /* promote to string */
3127 sv = newSVpvf("%.*Vu", (int)TYPE_DIGITS(UV), auv);
3128 while (s < strend) {
3129 sv = mul128(sv, *s & 0x7f);
3130 if (!(*s++ & 0x80)) {
3139 PUSHs(sv_2mortal(sv));
3144 if ((s >= strend) && bytes)
3145 croak("Unterminated compressed integer");
3150 if (sizeof(char*) > strend - s)
3153 Copy(s, &aptr, 1, char*);
3158 sv_setpvn(sv, aptr, len);
3159 PUSHs(sv_2mortal(sv));
3166 if (s + sizeof(Quad_t) > strend)
3169 Copy(s, &aquad, 1, Quad_t);
3170 s += sizeof(Quad_t);
3173 if (aquad >= IV_MIN && aquad <= IV_MAX)
3174 sv_setiv(sv, (IV)aquad);
3176 sv_setnv(sv, (double)aquad);
3177 PUSHs(sv_2mortal(sv));
3184 if (s + sizeof(unsigned Quad_t) > strend)
3187 Copy(s, &auquad, 1, unsigned Quad_t);
3188 s += sizeof(unsigned Quad_t);
3191 if (aquad <= UV_MAX)
3192 sv_setuv(sv, (UV)auquad);
3194 sv_setnv(sv, (double)auquad);
3195 PUSHs(sv_2mortal(sv));
3199 /* float and double added gnb@melba.bby.oz.au 22/11/89 */
3202 along = (strend - s) / sizeof(float);
3207 Copy(s, &afloat, 1, float);
3216 Copy(s, &afloat, 1, float);
3219 sv_setnv(sv, (double)afloat);
3220 PUSHs(sv_2mortal(sv));
3226 along = (strend - s) / sizeof(double);
3231 Copy(s, &adouble, 1, double);
3232 s += sizeof(double);
3240 Copy(s, &adouble, 1, double);
3241 s += sizeof(double);
3243 sv_setnv(sv, (double)adouble);
3244 PUSHs(sv_2mortal(sv));
3249 along = (strend - s) * 3 / 4;
3250 sv = NEWSV(42, along);
3253 while (s < strend && *s > ' ' && *s < 'a') {
3258 len = (*s++ - ' ') & 077;
3260 if (s < strend && *s >= ' ')
3261 a = (*s++ - ' ') & 077;
3264 if (s < strend && *s >= ' ')
3265 b = (*s++ - ' ') & 077;
3268 if (s < strend && *s >= ' ')
3269 c = (*s++ - ' ') & 077;
3272 if (s < strend && *s >= ' ')
3273 d = (*s++ - ' ') & 077;
3276 hunk[0] = a << 2 | b >> 4;
3277 hunk[1] = b << 4 | c >> 2;
3278 hunk[2] = c << 6 | d;
3279 sv_catpvn(sv, hunk, len > 3 ? 3 : len);
3284 else if (s[1] == '\n') /* possible checksum byte */
3287 XPUSHs(sv_2mortal(sv));
3292 if (strchr("fFdD", datumtype) ||
3293 (checksum > 32 && strchr("iIlLN", datumtype)) ) {
3297 while (checksum >= 16) {
3301 while (checksum >= 4) {
3307 along = (1 << checksum) - 1;
3308 while (cdouble < 0.0)
3310 cdouble = modf(cdouble / adouble, &trouble) * adouble;
3311 sv_setnv(sv, cdouble);
3314 if (checksum < 32) {
3315 aulong = (1 << checksum) - 1;
3318 sv_setuv(sv, (UV)culong);
3320 XPUSHs(sv_2mortal(sv));
3324 if (sp == oldsp && gimme == G_SCALAR)
3330 doencodes(sv, s, len)
3338 sv_catpvn(sv, hunk, 1);
3341 hunk[0] = ' ' + (077 & (*s >> 2));
3342 hunk[1] = ' ' + (077 & ((*s << 4) & 060 | (s[1] >> 4) & 017));
3343 hunk[2] = ' ' + (077 & ((s[1] << 2) & 074 | (s[2] >> 6) & 03));
3344 hunk[3] = ' ' + (077 & (s[2] & 077));
3345 sv_catpvn(sv, hunk, 4);
3349 for (s = SvPVX(sv); *s; s++) {
3353 sv_catpvn(sv, "\n", 1);
3361 SV *result = newSVpv("", l);
3362 char *result_c = SvPV(result, na); /* convenience */
3363 char *out = result_c;
3373 SvREFCNT_dec(result);
3396 SvREFCNT_dec(result);
3402 SvCUR_set(result, out - result_c);
3408 SV *pnum; /* must be '\0' terminated */
3412 char *s = SvPV(pnum, len);
3421 i = m * 10 + (*t - '0');
3423 r = (i >> 7); /* r < 10 */
3430 SvCUR_set(pnum, (STRLEN) (t - s));
3437 dSP; dMARK; dORIGMARK; dTARGET;
3438 register SV *cat = TARG;
3441 register char *pat = SvPVx(*++MARK, fromlen);
3442 register char *patend = pat + fromlen;
3447 static char null10[] = {0,0,0,0,0,0,0,0,0,0};
3448 static char *space10 = " ";
3450 /* These must not be in registers: */
3459 unsigned Quad_t auquad;
3467 sv_setpvn(cat, "", 0);
3468 while (pat < patend) {
3469 #define NEXTFROM (items-- > 0 ? *MARK++ : &sv_no)
3470 datumtype = *pat++ & 0xFF;
3471 if (isSPACE(datumtype))
3474 len = strchr("@Xxu", datumtype) ? 0 : items;
3477 else if (isDIGIT(*pat)) {
3479 while (isDIGIT(*pat))
3480 len = (len * 10) + (*pat++ - '0');
3486 croak("Invalid type in pack: '%c'", (int)datumtype);
3488 DIE("%% may only be used in unpack");
3499 if (SvCUR(cat) < len)
3500 DIE("X outside of string");
3507 sv_catpvn(cat, null10, 10);
3510 sv_catpvn(cat, null10, len);
3515 aptr = SvPV(fromstr, fromlen);
3519 sv_catpvn(cat, aptr, len);
3521 sv_catpvn(cat, aptr, fromlen);
3523 if (datumtype == 'A') {
3525 sv_catpvn(cat, space10, 10);
3528 sv_catpvn(cat, space10, len);
3532 sv_catpvn(cat, null10, 10);
3535 sv_catpvn(cat, null10, len);
3542 char *savepat = pat;
3547 aptr = SvPV(fromstr, fromlen);
3552 SvCUR(cat) += (len+7)/8;
3553 SvGROW(cat, SvCUR(cat) + 1);
3554 aptr = SvPVX(cat) + aint;
3559 if (datumtype == 'B') {
3560 for (len = 0; len++ < aint;) {
3561 items |= *pat++ & 1;
3565 *aptr++ = items & 0xff;
3571 for (len = 0; len++ < aint;) {
3577 *aptr++ = items & 0xff;
3583 if (datumtype == 'B')
3584 items <<= 7 - (aint & 7);
3586 items >>= 7 - (aint & 7);
3587 *aptr++ = items & 0xff;
3589 pat = SvPVX(cat) + SvCUR(cat);
3600 char *savepat = pat;
3605 aptr = SvPV(fromstr, fromlen);
3610 SvCUR(cat) += (len+1)/2;
3611 SvGROW(cat, SvCUR(cat) + 1);
3612 aptr = SvPVX(cat) + aint;
3617 if (datumtype == 'H') {
3618 for (len = 0; len++ < aint;) {
3620 items |= ((*pat++ & 15) + 9) & 15;
3622 items |= *pat++ & 15;
3626 *aptr++ = items & 0xff;
3632 for (len = 0; len++ < aint;) {
3634 items |= (((*pat++ & 15) + 9) & 15) << 4;
3636 items |= (*pat++ & 15) << 4;
3640 *aptr++ = items & 0xff;
3646 *aptr++ = items & 0xff;
3647 pat = SvPVX(cat) + SvCUR(cat);
3659 aint = SvIV(fromstr);
3661 sv_catpvn(cat, &achar, sizeof(char));
3664 /* Float and double added by gnb@melba.bby.oz.au 22/11/89 */
3669 afloat = (float)SvNV(fromstr);
3670 sv_catpvn(cat, (char *)&afloat, sizeof (float));
3677 adouble = (double)SvNV(fromstr);
3678 sv_catpvn(cat, (char *)&adouble, sizeof (double));
3684 ashort = (I16)SvIV(fromstr);
3686 ashort = htons(ashort);
3688 CAT16(cat, &ashort);
3694 ashort = (I16)SvIV(fromstr);
3696 ashort = htovs(ashort);
3698 CAT16(cat, &ashort);
3705 ashort = (I16)SvIV(fromstr);
3706 CAT16(cat, &ashort);
3712 auint = SvUV(fromstr);
3713 sv_catpvn(cat, (char*)&auint, sizeof(unsigned int));
3719 adouble = floor(SvNV(fromstr));
3722 croak("Cannot compress negative numbers");
3728 #ifdef CXUX_BROKEN_CONSTANT_CONVERT
3729 adouble <= UV_MAX_cxux
3736 char buf[1 + sizeof(UV)];
3737 char *in = buf + sizeof(buf);
3738 UV auv = U_V(adouble);;
3741 *--in = (auv & 0x7f) | 0x80;
3744 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
3745 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
3747 else if (SvPOKp(fromstr)) { /* decimal string arithmetics */
3748 char *from, *result, *in;
3753 /* Copy string and check for compliance */
3754 from = SvPV(fromstr, len);
3755 if ((norm = is_an_int(from, len)) == NULL)
3756 croak("can compress only unsigned integer");
3758 New('w', result, len, char);
3762 *--in = div128(norm, &done) | 0x80;
3763 result[len - 1] &= 0x7F; /* clear continue bit */
3764 sv_catpvn(cat, in, (result + len) - in);
3766 SvREFCNT_dec(norm); /* free norm */
3768 else if (SvNOKp(fromstr)) {
3769 char buf[sizeof(double) * 2]; /* 8/7 <= 2 */
3770 char *in = buf + sizeof(buf);
3773 double next = floor(adouble / 128);
3774 *--in = (unsigned char)(adouble - (next * 128)) | 0x80;
3775 if (--in < buf) /* this cannot happen ;-) */
3776 croak ("Cannot compress integer");
3778 } while (adouble > 0);
3779 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
3780 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
3783 croak("Cannot compress non integer");
3789 aint = SvIV(fromstr);
3790 sv_catpvn(cat, (char*)&aint, sizeof(int));
3796 aulong = SvUV(fromstr);
3798 aulong = htonl(aulong);
3800 CAT32(cat, &aulong);
3806 aulong = SvUV(fromstr);
3808 aulong = htovl(aulong);
3810 CAT32(cat, &aulong);
3816 aulong = SvUV(fromstr);
3817 CAT32(cat, &aulong);
3823 along = SvIV(fromstr);
3831 auquad = (unsigned Quad_t)SvIV(fromstr);
3832 sv_catpvn(cat, (char*)&auquad, sizeof(unsigned Quad_t));
3838 aquad = (Quad_t)SvIV(fromstr);
3839 sv_catpvn(cat, (char*)&aquad, sizeof(Quad_t));
3842 #endif /* HAS_QUAD */
3844 len = 1; /* assume SV is correct length */
3849 if (fromstr == &sv_undef)
3852 if (SvREADONLY(fromstr) && curcop != &compiling) {
3853 fromstr = sv_mortalcopy(fromstr);
3855 aptr = SvPV_force(fromstr, na);
3857 sv_catpvn(cat, (char*)&aptr, sizeof(char*));
3862 aptr = SvPV(fromstr, fromlen);
3863 SvGROW(cat, fromlen * 4 / 3);
3868 while (fromlen > 0) {
3875 doencodes(cat, aptr, todo);
3893 register I32 limit = POPi; /* note, negative is forever */
3896 register char *s = SvPV(sv, len);
3897 char *strend = s + len;
3899 register REGEXP *rx;
3903 I32 maxiters = (strend - s) + 10;
3906 I32 origlimit = limit;
3909 AV *oldstack = curstack;
3910 I32 gimme = GIMME_V;
3911 I32 oldsave = savestack_ix;
3914 Copy(&LvTARGOFF(POPs), &pm, 1, PMOP*);
3919 DIE("panic: do_split");
3920 rx = pm->op_pmregexp;
3922 TAINT_IF((pm->op_pmflags & PMf_LOCALE) &&
3923 (pm->op_pmflags & (PMf_WHITE | PMf_SKIPWHITE)));
3925 if (pm->op_pmreplroot)
3926 ary = GvAVn((GV*)pm->op_pmreplroot);
3927 else if (gimme != G_ARRAY)
3931 if (ary && (gimme != G_ARRAY || (pm->op_pmflags & PMf_ONCE))) {
3935 for (i = AvFILL(ary); i >= 0; i--)
3936 AvARRAY(ary)[i] = &sv_undef; /* don't free mere refs */
3940 /* temporarily switch stacks */
3941 SWITCHSTACK(curstack, ary);
3943 base = SP - stack_base;
3945 if (pm->op_pmflags & PMf_SKIPWHITE) {
3946 if (pm->op_pmflags & PMf_LOCALE) {
3947 while (isSPACE_LC(*s))
3955 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
3957 multiline = pm->op_pmflags & PMf_MULTILINE;
3961 limit = maxiters + 2;
3962 if (pm->op_pmflags & PMf_WHITE) {
3965 while (m < strend &&
3966 !((pm->op_pmflags & PMf_LOCALE)
3967 ? isSPACE_LC(*m) : isSPACE(*m)))
3972 dstr = NEWSV(30, m-s);
3973 sv_setpvn(dstr, s, m-s);
3979 while (s < strend &&
3980 ((pm->op_pmflags & PMf_LOCALE)
3981 ? isSPACE_LC(*s) : isSPACE(*s)))
3985 else if (strEQ("^", rx->precomp)) {
3988 for (m = s; m < strend && *m != '\n'; m++) ;
3992 dstr = NEWSV(30, m-s);
3993 sv_setpvn(dstr, s, m-s);
4000 else if (pm->op_pmshort && !rx->nparens) {
4001 i = SvCUR(pm->op_pmshort);
4003 i = *SvPVX(pm->op_pmshort);
4006 for (m = s; m < strend && *m != i; m++) ;
4009 dstr = NEWSV(30, m-s);
4010 sv_setpvn(dstr, s, m-s);
4019 while (s < strend && --limit &&
4020 (m=fbm_instr((unsigned char*)s, (unsigned char*)strend,
4024 dstr = NEWSV(31, m-s);
4025 sv_setpvn(dstr, s, m-s);
4034 maxiters += (strend - s) * rx->nparens;
4035 while (s < strend && --limit &&
4036 pregexec(rx, s, strend, orig, 1, Nullsv, TRUE))
4038 TAINT_IF(rx->exec_tainted);
4040 && rx->subbase != orig) {
4045 strend = s + (strend - m);
4048 dstr = NEWSV(32, m-s);
4049 sv_setpvn(dstr, s, m-s);
4054 for (i = 1; i <= rx->nparens; i++) {
4058 dstr = NEWSV(33, m-s);
4059 sv_setpvn(dstr, s, m-s);
4062 dstr = NEWSV(33, 0);
4071 LEAVE_SCOPE(oldsave);
4072 iters = (SP - stack_base) - base;
4073 if (iters > maxiters)
4076 /* keep field after final delim? */
4077 if (s < strend || (iters && origlimit)) {
4078 dstr = NEWSV(34, strend-s);
4079 sv_setpvn(dstr, s, strend-s);
4085 else if (!origlimit) {
4086 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0))
4090 SWITCHSTACK(ary, oldstack);
4091 if (gimme == G_ARRAY) {
4093 Copy(AvARRAY(ary), SP + 1, iters, SV*);
4099 if (gimme == G_ARRAY)
4102 if (iters || !pm->op_pmreplroot) {