This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perldoc under OS/2
[perl5.git] / pp.c
CommitLineData
a0d0e21e 1/* pp.c
79072805 2 *
9607fc9c 3 * Copyright (c) 1991-1997, Larry Wall
79072805 4 *
a0d0e21e
LW
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.
79072805 7 *
a0d0e21e
LW
8 */
9
10/*
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
13 */
79072805
LW
14
15#include "EXTERN.h"
16#include "perl.h"
17
36477c24 18/*
ef2d312d
TH
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.
24 */
25#ifdef CXUX_BROKEN_CONSTANT_CONVERT
26static double UV_MAX_cxux = ((double)UV_MAX);
27#endif
28
29/*
36477c24
PP
30 * Types used in bitwise operations.
31 *
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).)
36 *
96e4d5b1 37 * It just so happens that "int" is the right size almost everywhere.
36477c24
PP
38 */
39typedef int IBW;
40typedef unsigned UBW;
41
96e4d5b1
PP
42/*
43 * Mask used after bitwise operations.
44 *
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.
48 */
49#if BYTEORDER > 0xFFFF && defined(_CRAY) && !defined(_CRAYMPP)
46fc3d4c
PP
50# define BW_BITS 32
51# define BW_MASK ((1 << BW_BITS) - 1)
52# define BW_SIGN (1 << (BW_BITS - 1))
96e4d5b1
PP
53# define BWi(i) (((i) & BW_SIGN) ? ((i) | ~BW_MASK) : ((i) & BW_MASK))
54# define BWu(u) ((u) & BW_MASK)
55#else
56# define BWi(i) (i)
57# define BWu(u) (u)
58#endif
59
60/*
61 * Offset for integer pack/unpack.
62 *
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.
65 */
66
67/*
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
72 * the preprocessor.)
73 */
74#define SIZE16 2
75#define SIZE32 4
76
77#if BYTEORDER > 0xFFFF && defined(_CRAY) && !defined(_CRAYMPP)
78# if BYTEORDER == 0x12345678
79# define OFF16(p) (char*)(p)
80# define OFF32(p) (char*)(p)
81# else
82# if BYTEORDER == 0x87654321
83# define OFF16(p) ((char*)(p) + (sizeof(U16) - SIZE16))
84# define OFF32(p) ((char*)(p) + (sizeof(U32) - SIZE32))
85# else
86 }}}} bad cray byte order
87# endif
88# endif
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)
93#else
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)
98#endif
99
71be2cbc 100static void doencodes _((SV* sv, char* s, I32 len));
93dc8474
CS
101static SV* refto _((SV* sv));
102static U32 seed _((void));
103
104static bool srand_called = FALSE;
79072805 105
a0d0e21e 106/* variations on pp_null */
79072805 107
93a17b20
LW
108PP(pp_stub)
109{
110 dSP;
54310121 111 if (GIMME_V == G_SCALAR)
93a17b20 112 XPUSHs(&sv_undef);
93a17b20
LW
113 RETURN;
114}
115
79072805
LW
116PP(pp_scalar)
117{
118 return NORMAL;
119}
120
121/* Pushy stuff. */
122
93a17b20
LW
123PP(pp_padav)
124{
125 dSP; dTARGET;
a0d0e21e 126 if (op->op_private & OPpLVAL_INTRO)
8990e307 127 SAVECLEARSV(curpad[op->op_targ]);
85e6fe83 128 EXTEND(SP, 1);
a0d0e21e 129 if (op->op_flags & OPf_REF) {
85e6fe83 130 PUSHs(TARG);
93a17b20 131 RETURN;
85e6fe83
LW
132 }
133 if (GIMME == G_ARRAY) {
134 I32 maxarg = AvFILL((AV*)TARG) + 1;
135 EXTEND(SP, maxarg);
136 Copy(AvARRAY((AV*)TARG), SP+1, maxarg, SV*);
137 SP += maxarg;
138 }
139 else {
140 SV* sv = sv_newmortal();
141 I32 maxarg = AvFILL((AV*)TARG) + 1;
142 sv_setiv(sv, maxarg);
143 PUSHs(sv);
144 }
145 RETURN;
93a17b20
LW
146}
147
148PP(pp_padhv)
149{
150 dSP; dTARGET;
54310121
PP
151 I32 gimme;
152
93a17b20 153 XPUSHs(TARG);
a0d0e21e 154 if (op->op_private & OPpLVAL_INTRO)
8990e307 155 SAVECLEARSV(curpad[op->op_targ]);
a0d0e21e 156 if (op->op_flags & OPf_REF)
93a17b20 157 RETURN;
54310121
PP
158 gimme = GIMME_V;
159 if (gimme == G_ARRAY) {
a0d0e21e 160 RETURNOP(do_kv(ARGS));
85e6fe83 161 }
54310121 162 else if (gimme == G_SCALAR) {
85e6fe83 163 SV* sv = sv_newmortal();
46fc3d4c
PP
164 if (HvFILL((HV*)TARG))
165 sv_setpvf(sv, "%ld/%ld",
166 (long)HvFILL((HV*)TARG), (long)HvMAX((HV*)TARG) + 1);
85e6fe83
LW
167 else
168 sv_setiv(sv, 0);
169 SETs(sv);
85e6fe83 170 }
54310121 171 RETURN;
93a17b20
LW
172}
173
ed6116ce
LW
174PP(pp_padany)
175{
176 DIE("NOT IMPL LINE %d",__LINE__);
177}
178
79072805
LW
179/* Translations. */
180
181PP(pp_rv2gv)
182{
183 dSP; dTOPss;
a0d0e21e 184
ed6116ce 185 if (SvROK(sv)) {
a0d0e21e 186 wasref:
ed6116ce 187 sv = SvRV(sv);
b1dadf13
PP
188 if (SvTYPE(sv) == SVt_PVIO) {
189 GV *gv = (GV*) sv_newmortal();
190 gv_init(gv, 0, "", 0, 0);
191 GvIOp(gv) = (IO *)sv;
3e3baf6d 192 (void)SvREFCNT_inc(sv);
b1dadf13
PP
193 sv = (SV*) gv;
194 } else if (SvTYPE(sv) != SVt_PVGV)
a0d0e21e 195 DIE("Not a GLOB reference");
79072805
LW
196 }
197 else {
93a17b20 198 if (SvTYPE(sv) != SVt_PVGV) {
748a9306
LW
199 char *sym;
200
a0d0e21e
LW
201 if (SvGMAGICAL(sv)) {
202 mg_get(sv);
203 if (SvROK(sv))
204 goto wasref;
205 }
206 if (!SvOK(sv)) {
207 if (op->op_flags & OPf_REF ||
208 op->op_private & HINT_STRICT_REFS)
209 DIE(no_usym, "a symbol");
d83e6520
CS
210 if (dowarn)
211 warn(warn_uninit);
a0d0e21e
LW
212 RETSETUNDEF;
213 }
748a9306 214 sym = SvPV(sv, na);
85e6fe83 215 if (op->op_private & HINT_STRICT_REFS)
748a9306
LW
216 DIE(no_symref, sym, "a symbol");
217 sv = (SV*)gv_fetchpv(sym, TRUE, SVt_PVGV);
93a17b20 218 }
79072805 219 }
5f05dabc
PP
220 if (op->op_private & OPpLVAL_INTRO)
221 save_gp((GV*)sv, !(op->op_flags & OPf_SPECIAL));
79072805
LW
222 SETs(sv);
223 RETURN;
224}
225
79072805
LW
226PP(pp_rv2sv)
227{
228 dSP; dTOPss;
229
ed6116ce 230 if (SvROK(sv)) {
a0d0e21e 231 wasref:
ed6116ce 232 sv = SvRV(sv);
79072805
LW
233 switch (SvTYPE(sv)) {
234 case SVt_PVAV:
235 case SVt_PVHV:
236 case SVt_PVCV:
a0d0e21e 237 DIE("Not a SCALAR reference");
79072805
LW
238 }
239 }
240 else {
f12c7020 241 GV *gv = (GV*)sv;
748a9306
LW
242 char *sym;
243
463ee0b2 244 if (SvTYPE(gv) != SVt_PVGV) {
a0d0e21e
LW
245 if (SvGMAGICAL(sv)) {
246 mg_get(sv);
247 if (SvROK(sv))
248 goto wasref;
249 }
250 if (!SvOK(sv)) {
251 if (op->op_flags & OPf_REF ||
252 op->op_private & HINT_STRICT_REFS)
253 DIE(no_usym, "a SCALAR");
d83e6520
CS
254 if (dowarn)
255 warn(warn_uninit);
a0d0e21e
LW
256 RETSETUNDEF;
257 }
748a9306 258 sym = SvPV(sv, na);
85e6fe83 259 if (op->op_private & HINT_STRICT_REFS)
748a9306 260 DIE(no_symref, sym, "a SCALAR");
f12c7020 261 gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PV);
463ee0b2
LW
262 }
263 sv = GvSV(gv);
a0d0e21e
LW
264 }
265 if (op->op_flags & OPf_MOD) {
266 if (op->op_private & OPpLVAL_INTRO)
267 sv = save_scalar((GV*)TOPs);
5f05dabc 268 else if (op->op_private & OPpDEREF)
68dc0745 269 vivify_ref(sv, op->op_private & OPpDEREF);
79072805 270 }
a0d0e21e 271 SETs(sv);
79072805
LW
272 RETURN;
273}
274
275PP(pp_av2arylen)
276{
277 dSP;
278 AV *av = (AV*)TOPs;
279 SV *sv = AvARYLEN(av);
280 if (!sv) {
281 AvARYLEN(av) = sv = NEWSV(0,0);
282 sv_upgrade(sv, SVt_IV);
283 sv_magic(sv, (SV*)av, '#', Nullch, 0);
284 }
285 SETs(sv);
286 RETURN;
287}
288
a0d0e21e
LW
289PP(pp_pos)
290{
291 dSP; dTARGET; dPOPss;
292
293 if (op->op_flags & OPf_MOD) {
5f05dabc
PP
294 if (SvTYPE(TARG) < SVt_PVLV) {
295 sv_upgrade(TARG, SVt_PVLV);
296 sv_magic(TARG, Nullsv, '.', Nullch, 0);
297 }
298
299 LvTYPE(TARG) = '.';
a0d0e21e
LW
300 LvTARG(TARG) = sv;
301 PUSHs(TARG); /* no SvSETMAGIC */
302 RETURN;
303 }
304 else {
305 MAGIC* mg;
306
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);
311 RETURN;
312 }
313 }
314 RETPUSHUNDEF;
315 }
316}
317
79072805
LW
318PP(pp_rv2cv)
319{
320 dSP;
79072805
LW
321 GV *gv;
322 HV *stash;
8990e307 323
4633a7c4
LW
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));
07055b4c
CS
327 if (cv) {
328 if (CvCLONE(cv))
329 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
330 }
331 else
4633a7c4 332 cv = (CV*)&sv_undef;
79072805
LW
333 SETs((SV*)cv);
334 RETURN;
335}
336
c07a80fd
PP
337PP(pp_prototype)
338{
339 dSP;
340 CV *cv;
341 HV *stash;
342 GV *gv;
343 SV *ret;
344
345 ret = &sv_undef;
346 cv = sv_2cv(TOPs, &stash, &gv, FALSE);
5f05dabc
PP
347 if (cv && SvPOK(cv))
348 ret = sv_2mortal(newSVpv(SvPVX(cv), SvCUR(cv)));
c07a80fd
PP
349 SETs(ret);
350 RETURN;
351}
352
a0d0e21e
LW
353PP(pp_anoncode)
354{
355 dSP;
5f05dabc 356 CV* cv = (CV*)curpad[op->op_targ];
a5f75d66 357 if (CvCLONE(cv))
b355b4e0 358 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
5f05dabc 359 EXTEND(SP,1);
748a9306 360 PUSHs((SV*)cv);
a0d0e21e
LW
361 RETURN;
362}
363
364PP(pp_srefgen)
79072805 365{
71be2cbc
PP
366 dSP;
367 *SP = refto(*SP);
79072805 368 RETURN;
a0d0e21e
LW
369}
370
371PP(pp_refgen)
372{
373 dSP; dMARK;
a0d0e21e
LW
374 if (GIMME != G_ARRAY) {
375 MARK[1] = *SP;
376 SP = MARK + 1;
377 }
bbce6d69 378 EXTEND_MORTAL(SP - MARK);
71be2cbc
PP
379 while (++MARK <= SP)
380 *MARK = refto(*MARK);
a0d0e21e 381 RETURN;
79072805
LW
382}
383
71be2cbc
PP
384static SV*
385refto(sv)
386SV* sv;
387{
388 SV* rv;
389
390 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
391 if (LvTARGLEN(sv))
68dc0745
PP
392 vivify_defelem(sv);
393 if (!(sv = LvTARG(sv)))
394 sv = &sv_undef;
71be2cbc
PP
395 }
396 else if (SvPADTMP(sv))
397 sv = newSVsv(sv);
398 else {
399 SvTEMP_off(sv);
400 (void)SvREFCNT_inc(sv);
401 }
402 rv = sv_newmortal();
403 sv_upgrade(rv, SVt_RV);
404 SvRV(rv) = sv;
405 SvROK_on(rv);
406 return rv;
407}
408
79072805
LW
409PP(pp_ref)
410{
463ee0b2
LW
411 dSP; dTARGET;
412 SV *sv;
79072805
LW
413 char *pv;
414
a0d0e21e 415 sv = POPs;
f12c7020
PP
416
417 if (sv && SvGMAGICAL(sv))
418 mg_get(sv);
419
a0d0e21e 420 if (!sv || !SvROK(sv))
4633a7c4 421 RETPUSHNO;
79072805 422
ed6116ce 423 sv = SvRV(sv);
a0d0e21e 424 pv = sv_reftype(sv,TRUE);
463ee0b2 425 PUSHp(pv, strlen(pv));
79072805
LW
426 RETURN;
427}
428
429PP(pp_bless)
430{
463ee0b2 431 dSP;
463ee0b2 432 HV *stash;
79072805 433
463ee0b2
LW
434 if (MAXARG == 1)
435 stash = curcop->cop_stash;
436 else
a0d0e21e
LW
437 stash = gv_stashsv(POPs, TRUE);
438
439 (void)sv_bless(TOPs, stash);
79072805
LW
440 RETURN;
441}
442
a0d0e21e 443/* Pattern matching */
79072805 444
a0d0e21e 445PP(pp_study)
79072805 446{
c07a80fd 447 dSP; dPOPss;
a0d0e21e
LW
448 register unsigned char *s;
449 register I32 pos;
450 register I32 ch;
451 register I32 *sfirst;
452 register I32 *snext;
a0d0e21e
LW
453 STRLEN len;
454
1e422769
PP
455 if (sv == lastscream) {
456 if (SvSCREAM(sv))
457 RETPUSHYES;
458 }
c07a80fd
PP
459 else {
460 if (lastscream) {
461 SvSCREAM_off(lastscream);
462 SvREFCNT_dec(lastscream);
463 }
464 lastscream = SvREFCNT_inc(sv);
465 }
1e422769
PP
466
467 s = (unsigned char*)(SvPV(sv, len));
468 pos = len;
469 if (pos <= 0)
470 RETPUSHNO;
a0d0e21e
LW
471 if (pos > maxscream) {
472 if (maxscream < 0) {
473 maxscream = pos + 80;
474 New(301, screamfirst, 256, I32);
475 New(302, screamnext, maxscream, I32);
79072805
LW
476 }
477 else {
a0d0e21e
LW
478 maxscream = pos + pos / 4;
479 Renew(screamnext, maxscream, I32);
79072805 480 }
79072805 481 }
a0d0e21e
LW
482
483 sfirst = screamfirst;
484 snext = screamnext;
485
486 if (!sfirst || !snext)
487 DIE("do_study: out of memory");
488
489 for (ch = 256; ch; --ch)
490 *sfirst++ = -1;
491 sfirst -= 256;
492
493 while (--pos >= 0) {
494 ch = s[pos];
495 if (sfirst[ch] >= 0)
496 snext[pos] = sfirst[ch] - pos;
497 else
498 snext[pos] = -pos;
499 sfirst[ch] = pos;
79072805
LW
500 }
501
c07a80fd 502 SvSCREAM_on(sv);
464e2e8a 503 sv_magic(sv, Nullsv, 'g', Nullch, 0); /* piggyback on m//g magic */
1e422769 504 RETPUSHYES;
79072805
LW
505}
506
a0d0e21e 507PP(pp_trans)
79072805 508{
a0d0e21e
LW
509 dSP; dTARG;
510 SV *sv;
511
512 if (op->op_flags & OPf_STACKED)
513 sv = POPs;
79072805 514 else {
a0d0e21e
LW
515 sv = GvSV(defgv);
516 EXTEND(SP,1);
79072805 517 }
adbc6bb1 518 TARG = sv_newmortal();
a0d0e21e
LW
519 PUSHi(do_trans(sv, op));
520 RETURN;
79072805
LW
521}
522
a0d0e21e 523/* Lvalue operators. */
79072805 524
a0d0e21e
LW
525PP(pp_schop)
526{
527 dSP; dTARGET;
528 do_chop(TARG, TOPs);
529 SETTARG;
530 RETURN;
79072805
LW
531}
532
a0d0e21e 533PP(pp_chop)
79072805 534{
a0d0e21e
LW
535 dSP; dMARK; dTARGET;
536 while (SP > MARK)
537 do_chop(TARG, POPs);
538 PUSHTARG;
539 RETURN;
79072805
LW
540}
541
a0d0e21e 542PP(pp_schomp)
79072805 543{
a0d0e21e
LW
544 dSP; dTARGET;
545 SETi(do_chomp(TOPs));
546 RETURN;
79072805
LW
547}
548
a0d0e21e 549PP(pp_chomp)
79072805 550{
a0d0e21e
LW
551 dSP; dMARK; dTARGET;
552 register I32 count = 0;
553
554 while (SP > MARK)
555 count += do_chomp(POPs);
556 PUSHi(count);
557 RETURN;
79072805
LW
558}
559
a0d0e21e 560PP(pp_defined)
463ee0b2 561{
a0d0e21e
LW
562 dSP;
563 register SV* sv;
564
565 sv = POPs;
566 if (!sv || !SvANY(sv))
567 RETPUSHNO;
568 switch (SvTYPE(sv)) {
569 case SVt_PVAV:
8e07c86e 570 if (AvMAX(sv) >= 0 || SvRMAGICAL(sv))
a0d0e21e
LW
571 RETPUSHYES;
572 break;
573 case SVt_PVHV:
8e07c86e 574 if (HvARRAY(sv) || SvRMAGICAL(sv))
a0d0e21e
LW
575 RETPUSHYES;
576 break;
577 case SVt_PVCV:
578 if (CvROOT(sv) || CvXSUB(sv))
579 RETPUSHYES;
580 break;
581 default:
582 if (SvGMAGICAL(sv))
583 mg_get(sv);
584 if (SvOK(sv))
585 RETPUSHYES;
586 }
587 RETPUSHNO;
463ee0b2
LW
588}
589
a0d0e21e
LW
590PP(pp_undef)
591{
79072805 592 dSP;
a0d0e21e
LW
593 SV *sv;
594
774d564b
PP
595 if (!op->op_private) {
596 EXTEND(SP, 1);
a0d0e21e 597 RETPUSHUNDEF;
774d564b 598 }
79072805 599
a0d0e21e
LW
600 sv = POPs;
601 if (!sv)
602 RETPUSHUNDEF;
85e6fe83 603
a0d0e21e
LW
604 if (SvTHINKFIRST(sv)) {
605 if (SvREADONLY(sv))
606 RETPUSHUNDEF;
607 if (SvROK(sv))
608 sv_unref(sv);
85e6fe83
LW
609 }
610
a0d0e21e
LW
611 switch (SvTYPE(sv)) {
612 case SVt_NULL:
613 break;
614 case SVt_PVAV:
615 av_undef((AV*)sv);
616 break;
617 case SVt_PVHV:
618 hv_undef((HV*)sv);
619 break;
620 case SVt_PVCV:
54310121 621 if (cv_const_sv((CV*)sv))
9607fc9c 622 warn("Constant subroutine %s undefined",
54310121 623 CvANON((CV*)sv) ? "(anonymous)" : GvENAME(CvGV((CV*)sv)));
9607fc9c
PP
624 /* FALL THROUGH */
625 case SVt_PVFM:
09280a33
CS
626 { GV* gv = (GV*)SvREFCNT_inc(CvGV((CV*)sv));
627 cv_undef((CV*)sv);
628 CvGV((CV*)sv) = gv; } /* let user-undef'd sub keep its identity */
a0d0e21e 629 break;
8e07c86e 630 case SVt_PVGV:
44a8e56a
PP
631 if (SvFAKE(sv))
632 sv_setsv(sv, &sv_undef);
633 break;
a0d0e21e 634 default:
1e422769 635 if (SvTYPE(sv) >= SVt_PV && SvPVX(sv) && SvLEN(sv)) {
4633a7c4
LW
636 (void)SvOOK_off(sv);
637 Safefree(SvPVX(sv));
638 SvPV_set(sv, Nullch);
639 SvLEN_set(sv, 0);
a0d0e21e 640 }
4633a7c4
LW
641 (void)SvOK_off(sv);
642 SvSETMAGIC(sv);
79072805 643 }
a0d0e21e
LW
644
645 RETPUSHUNDEF;
79072805
LW
646}
647
a0d0e21e 648PP(pp_predec)
79072805 649{
a0d0e21e 650 dSP;
68dc0745 651 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
44a8e56a 652 croak(no_modify);
55497cff
PP
653 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
654 SvIVX(TOPs) != IV_MIN)
655 {
656 --SvIVX(TOPs);
657 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
748a9306
LW
658 }
659 else
660 sv_dec(TOPs);
a0d0e21e
LW
661 SvSETMAGIC(TOPs);
662 return NORMAL;
663}
79072805 664
a0d0e21e
LW
665PP(pp_postinc)
666{
667 dSP; dTARGET;
68dc0745 668 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
44a8e56a 669 croak(no_modify);
a0d0e21e 670 sv_setsv(TARG, TOPs);
55497cff
PP
671 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
672 SvIVX(TOPs) != IV_MAX)
673 {
674 ++SvIVX(TOPs);
675 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
748a9306
LW
676 }
677 else
678 sv_inc(TOPs);
a0d0e21e
LW
679 SvSETMAGIC(TOPs);
680 if (!SvOK(TARG))
681 sv_setiv(TARG, 0);
682 SETs(TARG);
683 return NORMAL;
684}
79072805 685
a0d0e21e
LW
686PP(pp_postdec)
687{
688 dSP; dTARGET;
68dc0745 689 if(SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
44a8e56a 690 croak(no_modify);
a0d0e21e 691 sv_setsv(TARG, TOPs);
55497cff
PP
692 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
693 SvIVX(TOPs) != IV_MIN)
694 {
695 --SvIVX(TOPs);
696 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
748a9306
LW
697 }
698 else
699 sv_dec(TOPs);
a0d0e21e
LW
700 SvSETMAGIC(TOPs);
701 SETs(TARG);
702 return NORMAL;
703}
79072805 704
a0d0e21e
LW
705/* Ordinary operators. */
706
707PP(pp_pow)
708{
709 dSP; dATARGET; tryAMAGICbin(pow,opASSIGN);
710 {
77676ba1 711 dPOPTOPnnrl;
a0d0e21e
LW
712 SETn( pow( left, right) );
713 RETURN;
93a17b20 714 }
a0d0e21e
LW
715}
716
717PP(pp_multiply)
718{
719 dSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
720 {
77676ba1 721 dPOPTOPnnrl;
a0d0e21e
LW
722 SETn( left * right );
723 RETURN;
79072805 724 }
a0d0e21e
LW
725}
726
727PP(pp_divide)
728{
729 dSP; dATARGET; tryAMAGICbin(div,opASSIGN);
730 {
77676ba1 731 dPOPPOPnnrl;
7a4c00b4
PP
732 double value;
733 if (right == 0.0)
a0d0e21e
LW
734 DIE("Illegal division by zero");
735#ifdef SLOPPYDIVIDE
736 /* insure that 20./5. == 4. */
737 {
7a4c00b4
PP
738 IV k;
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)) {
a0d0e21e
LW
742 value = k;
743 } else {
7a4c00b4 744 value = left / right;
79072805 745 }
a0d0e21e
LW
746 }
747#else
7a4c00b4 748 value = left / right;
a0d0e21e
LW
749#endif
750 PUSHn( value );
751 RETURN;
79072805 752 }
a0d0e21e
LW
753}
754
755PP(pp_modulo)
756{
757 dSP; dATARGET; tryAMAGICbin(mod,opASSIGN);
758 {
68dc0745
PP
759 UV left;
760 UV right;
beb18505
CS
761 bool left_neg;
762 bool right_neg;
68dc0745 763 UV ans;
a0d0e21e 764
68dc0745
PP
765 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)) {
766 IV i = SvIVX(POPs);
beb18505 767 right = (right_neg = (i < 0)) ? -i : i;
68dc0745
PP
768 }
769 else {
770 double n = POPn;
beb18505 771 right = U_V((right_neg = (n < 0)) ? -n : n);
68dc0745 772 }
36477c24
PP
773
774 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)) {
68dc0745 775 IV i = SvIVX(POPs);
beb18505 776 left = (left_neg = (i < 0)) ? -i : i;
36477c24 777 }
a0d0e21e 778 else {
68dc0745 779 double n = POPn;
beb18505 780 left = U_V((left_neg = (n < 0)) ? -n : n);
a0d0e21e 781 }
68dc0745
PP
782
783 if (!right)
784 DIE("Illegal modulus zero");
785
786 ans = left % right;
beb18505 787 if ((left_neg != right_neg) && ans)
68dc0745 788 ans = right - ans;
beb18505 789 if (right_neg) {
3e3baf6d
TB
790 /* XXX may warn: unary minus operator applied to unsigned type */
791 /* could change -foo to be (~foo)+1 instead */
beb18505
CS
792 if (ans <= -(UV)IV_MAX)
793 sv_setiv(TARG, (IV) -ans);
794 else
795 sv_setnv(TARG, -(double)ans);
796 }
797 else
798 sv_setuv(TARG, ans);
799 PUSHTARG;
a0d0e21e 800 RETURN;
79072805 801 }
a0d0e21e 802}
79072805 803
a0d0e21e
LW
804PP(pp_repeat)
805{
748a9306
LW
806 dSP; dATARGET; tryAMAGICbin(repeat,opASSIGN);
807 {
a0d0e21e
LW
808 register I32 count = POPi;
809 if (GIMME == G_ARRAY && op->op_private & OPpREPEAT_DOLIST) {
810 dMARK;
811 I32 items = SP - MARK;
812 I32 max;
79072805 813
a0d0e21e
LW
814 max = items * count;
815 MEXTEND(MARK, max);
816 if (count > 1) {
817 while (SP > MARK) {
818 if (*SP)
819 SvTEMP_off((*SP));
820 SP--;
79072805 821 }
a0d0e21e
LW
822 MARK++;
823 repeatcpy((char*)(MARK + items), (char*)MARK,
824 items * sizeof(SV*), count - 1);
825 SP += max;
79072805 826 }
a0d0e21e
LW
827 else if (count <= 0)
828 SP -= items;
79072805 829 }
a0d0e21e
LW
830 else { /* Note: mark already snarfed by pp_list */
831 SV *tmpstr;
832 STRLEN len;
833
834 tmpstr = POPs;
835 if (TARG == tmpstr && SvTHINKFIRST(tmpstr)) {
836 if (SvREADONLY(tmpstr) && curcop != &compiling)
837 DIE("Can't x= to readonly value");
838 if (SvROK(tmpstr))
839 sv_unref(tmpstr);
93a17b20 840 }
8ebc5c01
PP
841 SvSetSV(TARG, tmpstr);
842 SvPV_force(TARG, len);
843 if (count != 1) {
844 if (count < 1)
845 SvCUR_set(TARG, 0);
846 else {
847 SvGROW(TARG, (count * len) + 1);
848 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
849 SvCUR(TARG) *= count;
7a4c00b4 850 }
8ebc5c01 851 *SvEND(TARG) = '\0';
a0d0e21e 852 }
8ebc5c01 853 (void)SvPOK_only(TARG);
a0d0e21e 854 PUSHTARG;
79072805 855 }
a0d0e21e 856 RETURN;
748a9306 857 }
a0d0e21e 858}
79072805 859
a0d0e21e
LW
860PP(pp_subtract)
861{
862 dSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
863 {
7a4c00b4 864 dPOPTOPnnrl_ul;
a0d0e21e
LW
865 SETn( left - right );
866 RETURN;
79072805 867 }
a0d0e21e 868}
79072805 869
a0d0e21e
LW
870PP(pp_left_shift)
871{
872 dSP; dATARGET; tryAMAGICbin(lshift,opASSIGN);
873 {
36477c24 874 IBW shift = POPi;
ff68c719 875 if (op->op_private & HINT_INTEGER) {
36477c24 876 IBW i = TOPi;
46fc3d4c 877 i = BWi(i) << shift;
96e4d5b1 878 SETi(BWi(i));
ff68c719
PP
879 }
880 else {
36477c24 881 UBW u = TOPu;
96e4d5b1
PP
882 u <<= shift;
883 SETu(BWu(u));
ff68c719 884 }
55497cff 885 RETURN;
79072805 886 }
a0d0e21e 887}
79072805 888
a0d0e21e
LW
889PP(pp_right_shift)
890{
891 dSP; dATARGET; tryAMAGICbin(rshift,opASSIGN);
892 {
36477c24 893 IBW shift = POPi;
ff68c719 894 if (op->op_private & HINT_INTEGER) {
36477c24 895 IBW i = TOPi;
46fc3d4c 896 i = BWi(i) >> shift;
96e4d5b1 897 SETi(BWi(i));
ff68c719
PP
898 }
899 else {
36477c24 900 UBW u = TOPu;
96e4d5b1
PP
901 u >>= shift;
902 SETu(BWu(u));
ff68c719 903 }
a0d0e21e 904 RETURN;
93a17b20 905 }
79072805
LW
906}
907
a0d0e21e 908PP(pp_lt)
79072805 909{
a0d0e21e
LW
910 dSP; tryAMAGICbinSET(lt,0);
911 {
912 dPOPnv;
54310121 913 SETs(boolSV(TOPn < value));
a0d0e21e 914 RETURN;
79072805 915 }
a0d0e21e 916}
79072805 917
a0d0e21e
LW
918PP(pp_gt)
919{
920 dSP; tryAMAGICbinSET(gt,0);
921 {
922 dPOPnv;
54310121 923 SETs(boolSV(TOPn > value));
a0d0e21e 924 RETURN;
79072805 925 }
a0d0e21e
LW
926}
927
928PP(pp_le)
929{
930 dSP; tryAMAGICbinSET(le,0);
931 {
932 dPOPnv;
54310121 933 SETs(boolSV(TOPn <= value));
a0d0e21e 934 RETURN;
79072805 935 }
a0d0e21e
LW
936}
937
938PP(pp_ge)
939{
940 dSP; tryAMAGICbinSET(ge,0);
941 {
942 dPOPnv;
54310121 943 SETs(boolSV(TOPn >= value));
a0d0e21e 944 RETURN;
79072805 945 }
a0d0e21e 946}
79072805 947
a0d0e21e
LW
948PP(pp_ne)
949{
950 dSP; tryAMAGICbinSET(ne,0);
951 {
952 dPOPnv;
54310121 953 SETs(boolSV(TOPn != value));
a0d0e21e
LW
954 RETURN;
955 }
79072805
LW
956}
957
a0d0e21e 958PP(pp_ncmp)
79072805 959{
a0d0e21e
LW
960 dSP; dTARGET; tryAMAGICbin(ncmp,0);
961 {
962 dPOPTOPnnrl;
963 I32 value;
79072805 964
ff0cee69 965 if (left == right)
a0d0e21e 966 value = 0;
ff0cee69
PP
967 else if (left < right)
968 value = -1;
44a8e56a
PP
969 else if (left > right)
970 value = 1;
971 else {
972 SETs(&sv_undef);
973 RETURN;
974 }
a0d0e21e
LW
975 SETi(value);
976 RETURN;
79072805 977 }
a0d0e21e 978}
79072805 979
a0d0e21e
LW
980PP(pp_slt)
981{
982 dSP; tryAMAGICbinSET(slt,0);
983 {
984 dPOPTOPssrl;
bbce6d69
PP
985 int cmp = ((op->op_private & OPpLOCALE)
986 ? sv_cmp_locale(left, right)
987 : sv_cmp(left, right));
54310121 988 SETs(boolSV(cmp < 0));
a0d0e21e
LW
989 RETURN;
990 }
79072805
LW
991}
992
a0d0e21e 993PP(pp_sgt)
79072805 994{
a0d0e21e
LW
995 dSP; tryAMAGICbinSET(sgt,0);
996 {
997 dPOPTOPssrl;
bbce6d69
PP
998 int cmp = ((op->op_private & OPpLOCALE)
999 ? sv_cmp_locale(left, right)
1000 : sv_cmp(left, right));
54310121 1001 SETs(boolSV(cmp > 0));
a0d0e21e
LW
1002 RETURN;
1003 }
1004}
79072805 1005
a0d0e21e
LW
1006PP(pp_sle)
1007{
1008 dSP; tryAMAGICbinSET(sle,0);
1009 {
1010 dPOPTOPssrl;
bbce6d69
PP
1011 int cmp = ((op->op_private & OPpLOCALE)
1012 ? sv_cmp_locale(left, right)
1013 : sv_cmp(left, right));
54310121 1014 SETs(boolSV(cmp <= 0));
a0d0e21e 1015 RETURN;
79072805 1016 }
79072805
LW
1017}
1018
a0d0e21e
LW
1019PP(pp_sge)
1020{
1021 dSP; tryAMAGICbinSET(sge,0);
1022 {
1023 dPOPTOPssrl;
bbce6d69
PP
1024 int cmp = ((op->op_private & OPpLOCALE)
1025 ? sv_cmp_locale(left, right)
1026 : sv_cmp(left, right));
54310121 1027 SETs(boolSV(cmp >= 0));
a0d0e21e
LW
1028 RETURN;
1029 }
1030}
79072805 1031
36477c24
PP
1032PP(pp_seq)
1033{
1034 dSP; tryAMAGICbinSET(seq,0);
1035 {
1036 dPOPTOPssrl;
54310121 1037 SETs(boolSV(sv_eq(left, right)));
36477c24
PP
1038 RETURN;
1039 }
1040}
1041
a0d0e21e 1042PP(pp_sne)
79072805 1043{
a0d0e21e
LW
1044 dSP; tryAMAGICbinSET(sne,0);
1045 {
1046 dPOPTOPssrl;
54310121 1047 SETs(boolSV(!sv_eq(left, right)));
a0d0e21e 1048 RETURN;
463ee0b2 1049 }
79072805
LW
1050}
1051
a0d0e21e 1052PP(pp_scmp)
79072805 1053{
a0d0e21e
LW
1054 dSP; dTARGET; tryAMAGICbin(scmp,0);
1055 {
1056 dPOPTOPssrl;
bbce6d69
PP
1057 int cmp = ((op->op_private & OPpLOCALE)
1058 ? sv_cmp_locale(left, right)
1059 : sv_cmp(left, right));
1060 SETi( cmp );
a0d0e21e
LW
1061 RETURN;
1062 }
1063}
79072805 1064
55497cff
PP
1065PP(pp_bit_and)
1066{
a0d0e21e
LW
1067 dSP; dATARGET; tryAMAGICbin(band,opASSIGN);
1068 {
1069 dPOPTOPssrl;
4633a7c4 1070 if (SvNIOKp(left) || SvNIOKp(right)) {
36477c24
PP
1071 if (op->op_private & HINT_INTEGER) {
1072 IBW value = SvIV(left) & SvIV(right);
96e4d5b1 1073 SETi(BWi(value));
36477c24
PP
1074 }
1075 else {
1076 UBW value = SvUV(left) & SvUV(right);
96e4d5b1 1077 SETu(BWu(value));
36477c24 1078 }
a0d0e21e
LW
1079 }
1080 else {
1081 do_vop(op->op_type, TARG, left, right);
1082 SETTARG;
1083 }
1084 RETURN;
1085 }
1086}
79072805 1087
a0d0e21e
LW
1088PP(pp_bit_xor)
1089{
1090 dSP; dATARGET; tryAMAGICbin(bxor,opASSIGN);
1091 {
1092 dPOPTOPssrl;
4633a7c4 1093 if (SvNIOKp(left) || SvNIOKp(right)) {
36477c24 1094 if (op->op_private & HINT_INTEGER) {
1fbd88dc 1095 IBW value = (USE_LEFT(left) ? SvIV(left) : 0) ^ SvIV(right);
96e4d5b1 1096 SETi(BWi(value));
36477c24
PP
1097 }
1098 else {
1fbd88dc 1099 UBW value = (USE_LEFT(left) ? SvUV(left) : 0) ^ SvUV(right);
96e4d5b1 1100 SETu(BWu(value));
36477c24 1101 }
a0d0e21e
LW
1102 }
1103 else {
1104 do_vop(op->op_type, TARG, left, right);
1105 SETTARG;
1106 }
1107 RETURN;
1108 }
1109}
79072805 1110
a0d0e21e
LW
1111PP(pp_bit_or)
1112{
1113 dSP; dATARGET; tryAMAGICbin(bor,opASSIGN);
1114 {
1115 dPOPTOPssrl;
4633a7c4 1116 if (SvNIOKp(left) || SvNIOKp(right)) {
36477c24 1117 if (op->op_private & HINT_INTEGER) {
8ebc5c01 1118 IBW value = (USE_LEFT(left) ? SvIV(left) : 0) | SvIV(right);
96e4d5b1 1119 SETi(BWi(value));
36477c24
PP
1120 }
1121 else {
8ebc5c01 1122 UBW value = (USE_LEFT(left) ? SvUV(left) : 0) | SvUV(right);
96e4d5b1 1123 SETu(BWu(value));
36477c24 1124 }
a0d0e21e
LW
1125 }
1126 else {
1127 do_vop(op->op_type, TARG, left, right);
1128 SETTARG;
1129 }
1130 RETURN;
79072805 1131 }
a0d0e21e 1132}
79072805 1133
a0d0e21e
LW
1134PP(pp_negate)
1135{
1136 dSP; dTARGET; tryAMAGICun(neg);
1137 {
1138 dTOPss;
4633a7c4
LW
1139 if (SvGMAGICAL(sv))
1140 mg_get(sv);
55497cff
PP
1141 if (SvIOKp(sv) && !SvNOKp(sv) && !SvPOKp(sv) && SvIVX(sv) != IV_MIN)
1142 SETi(-SvIVX(sv));
1143 else if (SvNIOKp(sv))
a0d0e21e 1144 SETn(-SvNV(sv));
4633a7c4 1145 else if (SvPOKp(sv)) {
a0d0e21e
LW
1146 STRLEN len;
1147 char *s = SvPV(sv, len);
bbce6d69 1148 if (isIDFIRST(*s)) {
a0d0e21e
LW
1149 sv_setpvn(TARG, "-", 1);
1150 sv_catsv(TARG, sv);
79072805 1151 }
a0d0e21e
LW
1152 else if (*s == '+' || *s == '-') {
1153 sv_setsv(TARG, sv);
1154 *SvPV_force(TARG, len) = *s == '-' ? '+' : '-';
79072805
LW
1155 }
1156 else
a0d0e21e
LW
1157 sv_setnv(TARG, -SvNV(sv));
1158 SETTARG;
79072805 1159 }
4633a7c4
LW
1160 else
1161 SETn(-SvNV(sv));
79072805 1162 }
a0d0e21e 1163 RETURN;
79072805
LW
1164}
1165
a0d0e21e 1166PP(pp_not)
79072805 1167{
a0d0e21e
LW
1168#ifdef OVERLOAD
1169 dSP; tryAMAGICunSET(not);
1170#endif /* OVERLOAD */
54310121 1171 *stack_sp = boolSV(!SvTRUE(*stack_sp));
a0d0e21e 1172 return NORMAL;
79072805
LW
1173}
1174
a0d0e21e 1175PP(pp_complement)
79072805 1176{
a0d0e21e
LW
1177 dSP; dTARGET; tryAMAGICun(compl);
1178 {
1179 dTOPss;
4633a7c4 1180 if (SvNIOKp(sv)) {
36477c24
PP
1181 if (op->op_private & HINT_INTEGER) {
1182 IBW value = ~SvIV(sv);
96e4d5b1 1183 SETi(BWi(value));
36477c24
PP
1184 }
1185 else {
1186 UBW value = ~SvUV(sv);
96e4d5b1 1187 SETu(BWu(value));
36477c24 1188 }
a0d0e21e
LW
1189 }
1190 else {
1191 register char *tmps;
1192 register long *tmpl;
55497cff 1193 register I32 anum;
a0d0e21e
LW
1194 STRLEN len;
1195
1196 SvSetSV(TARG, sv);
1197 tmps = SvPV_force(TARG, len);
1198 anum = len;
1199#ifdef LIBERAL
1200 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
1201 *tmps = ~*tmps;
1202 tmpl = (long*)tmps;
1203 for ( ; anum >= sizeof(long); anum -= sizeof(long), tmpl++)
1204 *tmpl = ~*tmpl;
1205 tmps = (char*)tmpl;
1206#endif
1207 for ( ; anum > 0; anum--, tmps++)
1208 *tmps = ~*tmps;
1209
1210 SETs(TARG);
1211 }
1212 RETURN;
1213 }
79072805
LW
1214}
1215
a0d0e21e
LW
1216/* integer versions of some of the above */
1217
a0d0e21e 1218PP(pp_i_multiply)
79072805 1219{
a0d0e21e
LW
1220 dSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
1221 {
1222 dPOPTOPiirl;
1223 SETi( left * right );
1224 RETURN;
1225 }
79072805
LW
1226}
1227
a0d0e21e 1228PP(pp_i_divide)
79072805 1229{
a0d0e21e
LW
1230 dSP; dATARGET; tryAMAGICbin(div,opASSIGN);
1231 {
1232 dPOPiv;
1233 if (value == 0)
1234 DIE("Illegal division by zero");
1235 value = POPi / value;
1236 PUSHi( value );
1237 RETURN;
1238 }
79072805
LW
1239}
1240
a0d0e21e 1241PP(pp_i_modulo)
79072805 1242{
a0d0e21e 1243 dSP; dATARGET; tryAMAGICbin(mod,opASSIGN);
79072805 1244 {
a0d0e21e 1245 dPOPTOPiirl;
aa306039
CS
1246 if (!right)
1247 DIE("Illegal modulus zero");
a0d0e21e
LW
1248 SETi( left % right );
1249 RETURN;
79072805 1250 }
79072805
LW
1251}
1252
a0d0e21e 1253PP(pp_i_add)
79072805 1254{
a0d0e21e
LW
1255 dSP; dATARGET; tryAMAGICbin(add,opASSIGN);
1256 {
1257 dPOPTOPiirl;
1258 SETi( left + right );
1259 RETURN;
79072805 1260 }
79072805
LW
1261}
1262
a0d0e21e 1263PP(pp_i_subtract)
79072805 1264{
a0d0e21e
LW
1265 dSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
1266 {
1267 dPOPTOPiirl;
1268 SETi( left - right );
1269 RETURN;
79072805 1270 }
79072805
LW
1271}
1272
a0d0e21e 1273PP(pp_i_lt)
79072805 1274{
a0d0e21e
LW
1275 dSP; tryAMAGICbinSET(lt,0);
1276 {
1277 dPOPTOPiirl;
54310121 1278 SETs(boolSV(left < right));
a0d0e21e
LW
1279 RETURN;
1280 }
79072805
LW
1281}
1282
a0d0e21e 1283PP(pp_i_gt)
79072805 1284{
a0d0e21e
LW
1285 dSP; tryAMAGICbinSET(gt,0);
1286 {
1287 dPOPTOPiirl;
54310121 1288 SETs(boolSV(left > right));
a0d0e21e
LW
1289 RETURN;
1290 }
79072805
LW
1291}
1292
a0d0e21e 1293PP(pp_i_le)
79072805 1294{
a0d0e21e
LW
1295 dSP; tryAMAGICbinSET(le,0);
1296 {
1297 dPOPTOPiirl;
54310121 1298 SETs(boolSV(left <= right));
a0d0e21e 1299 RETURN;
85e6fe83 1300 }
79072805
LW
1301}
1302
a0d0e21e 1303PP(pp_i_ge)
79072805 1304{
a0d0e21e
LW
1305 dSP; tryAMAGICbinSET(ge,0);
1306 {
1307 dPOPTOPiirl;
54310121 1308 SETs(boolSV(left >= right));
a0d0e21e
LW
1309 RETURN;
1310 }
79072805
LW
1311}
1312
a0d0e21e 1313PP(pp_i_eq)
79072805 1314{
a0d0e21e
LW
1315 dSP; tryAMAGICbinSET(eq,0);
1316 {
1317 dPOPTOPiirl;
54310121 1318 SETs(boolSV(left == right));
a0d0e21e
LW
1319 RETURN;
1320 }
79072805
LW
1321}
1322
a0d0e21e 1323PP(pp_i_ne)
79072805 1324{
a0d0e21e
LW
1325 dSP; tryAMAGICbinSET(ne,0);
1326 {
1327 dPOPTOPiirl;
54310121 1328 SETs(boolSV(left != right));
a0d0e21e
LW
1329 RETURN;
1330 }
79072805
LW
1331}
1332
a0d0e21e 1333PP(pp_i_ncmp)
79072805 1334{
a0d0e21e
LW
1335 dSP; dTARGET; tryAMAGICbin(ncmp,0);
1336 {
1337 dPOPTOPiirl;
1338 I32 value;
79072805 1339
a0d0e21e 1340 if (left > right)
79072805 1341 value = 1;
a0d0e21e 1342 else if (left < right)
79072805 1343 value = -1;
a0d0e21e 1344 else
79072805 1345 value = 0;
a0d0e21e
LW
1346 SETi(value);
1347 RETURN;
79072805 1348 }
85e6fe83
LW
1349}
1350
1351PP(pp_i_negate)
1352{
a0d0e21e 1353 dSP; dTARGET; tryAMAGICun(neg);
85e6fe83
LW
1354 SETi(-TOPi);
1355 RETURN;
1356}
1357
79072805
LW
1358/* High falutin' math. */
1359
1360PP(pp_atan2)
1361{
a0d0e21e
LW
1362 dSP; dTARGET; tryAMAGICbin(atan2,0);
1363 {
1364 dPOPTOPnnrl;
1365 SETn(atan2(left, right));
1366 RETURN;
1367 }
79072805
LW
1368}
1369
1370PP(pp_sin)
1371{
a0d0e21e
LW
1372 dSP; dTARGET; tryAMAGICun(sin);
1373 {
1374 double value;
1375 value = POPn;
1376 value = sin(value);
1377 XPUSHn(value);
1378 RETURN;
1379 }
79072805
LW
1380}
1381
1382PP(pp_cos)
1383{
a0d0e21e
LW
1384 dSP; dTARGET; tryAMAGICun(cos);
1385 {
1386 double value;
1387 value = POPn;
1388 value = cos(value);
1389 XPUSHn(value);
1390 RETURN;
1391 }
79072805
LW
1392}
1393
1394PP(pp_rand)
1395{
1396 dSP; dTARGET;
1397 double value;
1398 if (MAXARG < 1)
1399 value = 1.0;
1400 else
1401 value = POPn;
1402 if (value == 0.0)
1403 value = 1.0;
93dc8474
CS
1404 if (!srand_called) {
1405 (void)srand((unsigned)seed());
1406 srand_called = TRUE;
1407 }
79072805
LW
1408#if RANDBITS == 31
1409 value = rand() * value / 2147483648.0;
1410#else
1411#if RANDBITS == 16
1412 value = rand() * value / 65536.0;
1413#else
1414#if RANDBITS == 15
1415 value = rand() * value / 32768.0;
1416#else
1417 value = rand() * value / (double)(((unsigned long)1) << RANDBITS);
1418#endif
1419#endif
1420#endif
1421 XPUSHn(value);
1422 RETURN;
1423}
1424
1425PP(pp_srand)
1426{
1427 dSP;
93dc8474
CS
1428 UV anum;
1429 if (MAXARG < 1)
1430 anum = seed();
1431 else
1432 anum = POPu;
1433 (void)srand((unsigned)anum);
1434 srand_called = TRUE;
1435 EXTEND(SP, 1);
1436 RETPUSHYES;
1437}
79072805 1438
93dc8474
CS
1439static U32
1440seed()
1441{
54310121
PP
1442 /*
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.
1452 */
1453#if RANDBITS > 16
1454# define SEED_C1 1000003
1455#define SEED_C4 73819
1456#else
1457# define SEED_C1 25747
1458#define SEED_C4 20639
1459#endif
1460#define SEED_C2 3
1461#define SEED_C3 269
1462#define SEED_C5 26107
1463
93dc8474 1464 U32 u;
f12c7020
PP
1465#ifdef VMS
1466# include <starlet.h>
43c92808
HF
1467 /* when[] = (low 32 bits, high 32 bits) of time since epoch
1468 * in 100-ns units, typically incremented ever 10 ms. */
93dc8474
CS
1469 unsigned int when[2];
1470 _ckvmssts(sys$gettim(when));
54310121 1471 u = (U32)SEED_C1 * when[0] + (U32)SEED_C2 * when[1];
f12c7020 1472#else
5f05dabc 1473# ifdef HAS_GETTIMEOFDAY
93dc8474
CS
1474 struct timeval when;
1475 gettimeofday(&when,(struct timezone *) 0);
54310121 1476 u = (U32)SEED_C1 * when.tv_sec + (U32)SEED_C2 * when.tv_usec;
f12c7020 1477# else
93dc8474
CS
1478 Time_t when;
1479 (void)time(&when);
54310121 1480 u = (U32)SEED_C1 * when;
f12c7020
PP
1481# endif
1482#endif
54310121
PP
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;
f12c7020 1487#endif
93dc8474 1488 return u;
79072805
LW
1489}
1490
1491PP(pp_exp)
1492{
a0d0e21e
LW
1493 dSP; dTARGET; tryAMAGICun(exp);
1494 {
1495 double value;
1496 value = POPn;
1497 value = exp(value);
1498 XPUSHn(value);
1499 RETURN;
1500 }
79072805
LW
1501}
1502
1503PP(pp_log)
1504{
a0d0e21e
LW
1505 dSP; dTARGET; tryAMAGICun(log);
1506 {
1507 double value;
1508 value = POPn;
bbce6d69 1509 if (value <= 0.0) {
36477c24 1510 SET_NUMERIC_STANDARD();
2304df62 1511 DIE("Can't take log of %g", value);
bbce6d69 1512 }
a0d0e21e
LW
1513 value = log(value);
1514 XPUSHn(value);
1515 RETURN;
1516 }
79072805
LW
1517}
1518
1519PP(pp_sqrt)
1520{
a0d0e21e
LW
1521 dSP; dTARGET; tryAMAGICun(sqrt);
1522 {
1523 double value;
1524 value = POPn;
bbce6d69 1525 if (value < 0.0) {
36477c24 1526 SET_NUMERIC_STANDARD();
2304df62 1527 DIE("Can't take sqrt of %g", value);
bbce6d69 1528 }
a0d0e21e
LW
1529 value = sqrt(value);
1530 XPUSHn(value);
1531 RETURN;
1532 }
79072805
LW
1533}
1534
1535PP(pp_int)
1536{
1537 dSP; dTARGET;
774d564b
PP
1538 {
1539 double value = TOPn;
1540 IV iv;
1541
1542 if (SvIOKp(TOPs) && !SvNOKp(TOPs) && !SvPOKp(TOPs)) {
1543 iv = SvIVX(TOPs);
1544 SETi(iv);
1545 }
1546 else {
1547 if (value >= 0.0)
1548 (void)modf(value, &value);
1549 else {
1550 (void)modf(-value, &value);
1551 value = -value;
1552 }
1553 iv = I_V(value);
1554 if (iv == value)
1555 SETi(iv);
1556 else
1557 SETn(value);
1558 }
79072805 1559 }
79072805
LW
1560 RETURN;
1561}
1562
463ee0b2
LW
1563PP(pp_abs)
1564{
a0d0e21e
LW
1565 dSP; dTARGET; tryAMAGICun(abs);
1566 {
774d564b
PP
1567 double value = TOPn;
1568 IV iv;
1569
1570 if (SvIOKp(TOPs) && !SvNOKp(TOPs) && !SvPOKp(TOPs) &&
1571 (iv = SvIVX(TOPs)) != IV_MIN) {
1572 if (iv < 0)
1573 iv = -iv;
1574 SETi(iv);
1575 }
1576 else {
1577 if (value < 0.0)
1578 value = -value;
1579 SETn(value);
1580 }
a0d0e21e 1581 }
774d564b 1582 RETURN;
463ee0b2
LW
1583}
1584
79072805
LW
1585PP(pp_hex)
1586{
1587 dSP; dTARGET;
1588 char *tmps;
1589 I32 argtype;
1590
a0d0e21e 1591 tmps = POPp;
55497cff 1592 XPUSHu(scan_hex(tmps, 99, &argtype));
79072805
LW
1593 RETURN;
1594}
1595
1596PP(pp_oct)
1597{
1598 dSP; dTARGET;
55497cff 1599 UV value;
79072805
LW
1600 I32 argtype;
1601 char *tmps;
1602
a0d0e21e 1603 tmps = POPp;
464e2e8a
PP
1604 while (*tmps && isSPACE(*tmps))
1605 tmps++;
1606 if (*tmps == '0')
79072805
LW
1607 tmps++;
1608 if (*tmps == 'x')
464e2e8a
PP
1609 value = scan_hex(++tmps, 99, &argtype);
1610 else
1611 value = scan_oct(tmps, 99, &argtype);
55497cff 1612 XPUSHu(value);
79072805
LW
1613 RETURN;
1614}
1615
1616/* String stuff. */
1617
1618PP(pp_length)
1619{
1620 dSP; dTARGET;
a0d0e21e 1621 SETi( sv_len(TOPs) );
79072805
LW
1622 RETURN;
1623}
1624
1625PP(pp_substr)
1626{
1627 dSP; dTARGET;
1628 SV *sv;
1629 I32 len;
463ee0b2 1630 STRLEN curlen;
79072805
LW
1631 I32 pos;
1632 I32 rem;
a0d0e21e 1633 I32 lvalue = op->op_flags & OPf_MOD;
79072805 1634 char *tmps;
a0d0e21e 1635 I32 arybase = curcop->cop_arybase;
79072805
LW
1636
1637 if (MAXARG > 2)
1638 len = POPi;
1639 pos = POPi - arybase;
1640 sv = POPs;
a0d0e21e 1641 tmps = SvPV(sv, curlen);
d9fdd1af 1642 if (pos < 0)
79072805 1643 pos += curlen + arybase;
2304df62 1644 if (pos < 0 || pos > curlen) {
a0d0e21e 1645 if (dowarn || lvalue)
2304df62
AD
1646 warn("substr outside of string");
1647 RETPUSHUNDEF;
1648 }
79072805
LW
1649 else {
1650 if (MAXARG < 3)
1651 len = curlen;
a0d0e21e 1652 else if (len < 0) {
748a9306 1653 len += curlen - pos;
a0d0e21e
LW
1654 if (len < 0)
1655 len = 0;
1656 }
79072805
LW
1657 tmps += pos;
1658 rem = curlen - pos; /* rem=how many bytes left*/
1659 if (rem > len)
1660 rem = len;
1661 sv_setpvn(TARG, tmps, rem);
1662 if (lvalue) { /* it's an lvalue! */
dedeecda
PP
1663 if (!SvGMAGICAL(sv)) {
1664 if (SvROK(sv)) {
1665 SvPV_force(sv,na);
1666 if (dowarn)
1667 warn("Attempt to use reference as lvalue in substr");
1668 }
1669 if (SvOK(sv)) /* is it defined ? */
1670 (void)SvPOK_only(sv);
1671 else
1672 sv_setpvn(sv,"",0); /* avoid lexical reincarnation */
1673 }
5f05dabc 1674
a0d0e21e
LW
1675 if (SvTYPE(TARG) < SVt_PVLV) {
1676 sv_upgrade(TARG, SVt_PVLV);
1677 sv_magic(TARG, Nullsv, 'x', Nullch, 0);
ed6116ce 1678 }
a0d0e21e 1679
5f05dabc 1680 LvTYPE(TARG) = 'x';
79072805 1681 LvTARG(TARG) = sv;
a0d0e21e 1682 LvTARGOFF(TARG) = pos;
79072805
LW
1683 LvTARGLEN(TARG) = rem;
1684 }
1685 }
1686 PUSHs(TARG); /* avoid SvSETMAGIC here */
1687 RETURN;
1688}
1689
1690PP(pp_vec)
1691{
1692 dSP; dTARGET;
1693 register I32 size = POPi;
1694 register I32 offset = POPi;
1695 register SV *src = POPs;
a0d0e21e 1696 I32 lvalue = op->op_flags & OPf_MOD;
463ee0b2
LW
1697 STRLEN srclen;
1698 unsigned char *s = (unsigned char*)SvPV(src, srclen);
79072805
LW
1699 unsigned long retnum;
1700 I32 len;
1701
1702 offset *= size; /* turn into bit offset */
1703 len = (offset + size + 7) / 8;
1704 if (offset < 0 || size < 1)
1705 retnum = 0;
79072805 1706 else {
a0d0e21e
LW
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);
1711 }
1712
1713 LvTYPE(TARG) = 'v';
1714 LvTARG(TARG) = src;
1715 LvTARGOFF(TARG) = offset;
1716 LvTARGLEN(TARG) = size;
1717 }
93a17b20 1718 if (len > srclen) {
a0d0e21e
LW
1719 if (size <= 8)
1720 retnum = 0;
1721 else {
1722 offset >>= 3;
748a9306
LW
1723 if (size == 16) {
1724 if (offset >= srclen)
1725 retnum = 0;
a0d0e21e 1726 else
748a9306
LW
1727 retnum = (unsigned long) s[offset] << 8;
1728 }
1729 else if (size == 32) {
1730 if (offset >= srclen)
1731 retnum = 0;
1732 else if (offset + 1 >= srclen)
a0d0e21e 1733 retnum = (unsigned long) s[offset] << 24;
748a9306
LW
1734 else if (offset + 2 >= srclen)
1735 retnum = ((unsigned long) s[offset] << 24) +
1736 ((unsigned long) s[offset + 1] << 16);
1737 else
1738 retnum = ((unsigned long) s[offset] << 24) +
1739 ((unsigned long) s[offset + 1] << 16) +
1740 (s[offset + 2] << 8);
a0d0e21e
LW
1741 }
1742 }
79072805 1743 }
a0d0e21e 1744 else if (size < 8)
79072805
LW
1745 retnum = (s[offset >> 3] >> (offset & 7)) & ((1 << size) - 1);
1746 else {
1747 offset >>= 3;
1748 if (size == 8)
1749 retnum = s[offset];
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];
1756 }
79072805
LW
1757 }
1758
1e422769 1759 sv_setiv(TARG, (IV)retnum);
79072805
LW
1760 PUSHs(TARG);
1761 RETURN;
1762}
1763
1764PP(pp_index)
1765{
1766 dSP; dTARGET;
1767 SV *big;
1768 SV *little;
1769 I32 offset;
1770 I32 retval;
1771 char *tmps;
1772 char *tmps2;
463ee0b2 1773 STRLEN biglen;
a0d0e21e 1774 I32 arybase = curcop->cop_arybase;
79072805
LW
1775
1776 if (MAXARG < 3)
1777 offset = 0;
1778 else
1779 offset = POPi - arybase;
1780 little = POPs;
1781 big = POPs;
463ee0b2 1782 tmps = SvPV(big, biglen);
79072805
LW
1783 if (offset < 0)
1784 offset = 0;
93a17b20
LW
1785 else if (offset > biglen)
1786 offset = biglen;
79072805 1787 if (!(tmps2 = fbm_instr((unsigned char*)tmps + offset,
93a17b20 1788 (unsigned char*)tmps + biglen, little)))
79072805
LW
1789 retval = -1 + arybase;
1790 else
1791 retval = tmps2 - tmps + arybase;
1792 PUSHi(retval);
1793 RETURN;
1794}
1795
1796PP(pp_rindex)
1797{
1798 dSP; dTARGET;
1799 SV *big;
1800 SV *little;
463ee0b2
LW
1801 STRLEN blen;
1802 STRLEN llen;
79072805
LW
1803 SV *offstr;
1804 I32 offset;
1805 I32 retval;
1806 char *tmps;
1807 char *tmps2;
a0d0e21e 1808 I32 arybase = curcop->cop_arybase;
79072805 1809
a0d0e21e 1810 if (MAXARG >= 3)
79072805
LW
1811 offstr = POPs;
1812 little = POPs;
1813 big = POPs;
463ee0b2
LW
1814 tmps2 = SvPV(little, llen);
1815 tmps = SvPV(big, blen);
79072805 1816 if (MAXARG < 3)
463ee0b2 1817 offset = blen;
79072805 1818 else
463ee0b2 1819 offset = SvIV(offstr) - arybase + llen;
79072805
LW
1820 if (offset < 0)
1821 offset = 0;
463ee0b2
LW
1822 else if (offset > blen)
1823 offset = blen;
79072805 1824 if (!(tmps2 = rninstr(tmps, tmps + offset,
463ee0b2 1825 tmps2, tmps2 + llen)))
79072805
LW
1826 retval = -1 + arybase;
1827 else
1828 retval = tmps2 - tmps + arybase;
1829 PUSHi(retval);
1830 RETURN;
1831}
1832
1833PP(pp_sprintf)
1834{
1835 dSP; dMARK; dORIGMARK; dTARGET;
36477c24 1836#ifdef USE_LOCALE_NUMERIC
bbce6d69 1837 if (op->op_private & OPpLOCALE)
36477c24 1838 SET_NUMERIC_LOCAL();
bbce6d69 1839 else
36477c24
PP
1840 SET_NUMERIC_STANDARD();
1841#endif
79072805 1842 do_sprintf(TARG, SP-MARK, MARK+1);
bbce6d69 1843 TAINT_IF(SvTAINTED(TARG));
79072805
LW
1844 SP = ORIGMARK;
1845 PUSHTARG;
1846 RETURN;
1847}
1848
79072805
LW
1849PP(pp_ord)
1850{
1851 dSP; dTARGET;
1852 I32 value;
1853 char *tmps;
79072805 1854
79072805 1855#ifndef I286
a0d0e21e 1856 tmps = POPp;
79072805
LW
1857 value = (I32) (*tmps & 255);
1858#else
a0d0e21e
LW
1859 I32 anum;
1860 tmps = POPp;
79072805
LW
1861 anum = (I32) *tmps;
1862 value = (I32) (anum & 255);
1863#endif
1864 XPUSHi(value);
1865 RETURN;
1866}
1867
463ee0b2
LW
1868PP(pp_chr)
1869{
1870 dSP; dTARGET;
1871 char *tmps;
1872
748a9306
LW
1873 (void)SvUPGRADE(TARG,SVt_PV);
1874 SvGROW(TARG,2);
463ee0b2
LW
1875 SvCUR_set(TARG, 1);
1876 tmps = SvPVX(TARG);
748a9306
LW
1877 *tmps++ = POPi;
1878 *tmps = '\0';
a0d0e21e 1879 (void)SvPOK_only(TARG);
463ee0b2
LW
1880 XPUSHs(TARG);
1881 RETURN;
1882}
1883
79072805
LW
1884PP(pp_crypt)
1885{
1886 dSP; dTARGET; dPOPTOPssrl;
1887#ifdef HAS_CRYPT
a0d0e21e 1888 char *tmps = SvPV(left, na);
79072805 1889#ifdef FCRYPT
a0d0e21e 1890 sv_setpv(TARG, fcrypt(tmps, SvPV(right, na)));
79072805 1891#else
a0d0e21e 1892 sv_setpv(TARG, crypt(tmps, SvPV(right, na)));
79072805
LW
1893#endif
1894#else
1895 DIE(
1896 "The crypt() function is unimplemented due to excessive paranoia.");
1897#endif
1898 SETs(TARG);
1899 RETURN;
1900}
1901
1902PP(pp_ucfirst)
1903{
1904 dSP;
1905 SV *sv = TOPs;
1906 register char *s;
1907
ed6116ce 1908 if (!SvPADTMP(sv)) {
79072805
LW
1909 dTARGET;
1910 sv_setsv(TARG, sv);
1911 sv = TARG;
1912 SETs(sv);
1913 }
a0d0e21e 1914 s = SvPV_force(sv, na);
bbce6d69
PP
1915 if (*s) {
1916 if (op->op_private & OPpLOCALE) {
1917 TAINT;
1918 SvTAINTED_on(sv);
1919 *s = toUPPER_LC(*s);
1920 }
1921 else
1922 *s = toUPPER(*s);
1923 }
79072805
LW
1924
1925 RETURN;
1926}
1927
1928PP(pp_lcfirst)
1929{
1930 dSP;
1931 SV *sv = TOPs;
1932 register char *s;
1933
ed6116ce 1934 if (!SvPADTMP(sv)) {
79072805
LW
1935 dTARGET;
1936 sv_setsv(TARG, sv);
1937 sv = TARG;
1938 SETs(sv);
1939 }
a0d0e21e 1940 s = SvPV_force(sv, na);
bbce6d69
PP
1941 if (*s) {
1942 if (op->op_private & OPpLOCALE) {
1943 TAINT;
1944 SvTAINTED_on(sv);
1945 *s = toLOWER_LC(*s);
1946 }
1947 else
1948 *s = toLOWER(*s);
1949 }
79072805
LW
1950
1951 SETs(sv);
1952 RETURN;
1953}
1954
1955PP(pp_uc)
1956{
1957 dSP;
1958 SV *sv = TOPs;
1959 register char *s;
463ee0b2 1960 STRLEN len;
79072805 1961
ed6116ce 1962 if (!SvPADTMP(sv)) {
79072805
LW
1963 dTARGET;
1964 sv_setsv(TARG, sv);
1965 sv = TARG;
1966 SETs(sv);
1967 }
bbce6d69 1968
a0d0e21e 1969 s = SvPV_force(sv, len);
bbce6d69
PP
1970 if (len) {
1971 register char *send = s + len;
1972
1973 if (op->op_private & OPpLOCALE) {
1974 TAINT;
1975 SvTAINTED_on(sv);
1976 for (; s < send; s++)
1977 *s = toUPPER_LC(*s);
1978 }
1979 else {
1980 for (; s < send; s++)
1981 *s = toUPPER(*s);
1982 }
79072805
LW
1983 }
1984 RETURN;
1985}
1986
1987PP(pp_lc)
1988{
1989 dSP;
1990 SV *sv = TOPs;
1991 register char *s;
463ee0b2 1992 STRLEN len;
79072805 1993
ed6116ce 1994 if (!SvPADTMP(sv)) {
79072805
LW
1995 dTARGET;
1996 sv_setsv(TARG, sv);
1997 sv = TARG;
1998 SETs(sv);
1999 }
bbce6d69 2000
a0d0e21e 2001 s = SvPV_force(sv, len);
bbce6d69
PP
2002 if (len) {
2003 register char *send = s + len;
2004
2005 if (op->op_private & OPpLOCALE) {
2006 TAINT;
2007 SvTAINTED_on(sv);
2008 for (; s < send; s++)
2009 *s = toLOWER_LC(*s);
2010 }
2011 else {
2012 for (; s < send; s++)
2013 *s = toLOWER(*s);
2014 }
79072805
LW
2015 }
2016 RETURN;
2017}
2018
a0d0e21e 2019PP(pp_quotemeta)
79072805 2020{
a0d0e21e
LW
2021 dSP; dTARGET;
2022 SV *sv = TOPs;
2023 STRLEN len;
2024 register char *s = SvPV(sv,len);
2025 register char *d;
79072805 2026
a0d0e21e
LW
2027 if (len) {
2028 (void)SvUPGRADE(TARG, SVt_PV);
c07a80fd 2029 SvGROW(TARG, (len * 2) + 1);
a0d0e21e
LW
2030 d = SvPVX(TARG);
2031 while (len--) {
2032 if (!isALNUM(*s))
2033 *d++ = '\\';
2034 *d++ = *s++;
79072805 2035 }
a0d0e21e
LW
2036 *d = '\0';
2037 SvCUR_set(TARG, d - SvPVX(TARG));
2038 (void)SvPOK_only(TARG);
79072805 2039 }
a0d0e21e
LW
2040 else
2041 sv_setpvn(TARG, s, len);
2042 SETs(TARG);
79072805
LW
2043 RETURN;
2044}
2045
a0d0e21e 2046/* Arrays. */
79072805 2047
a0d0e21e 2048PP(pp_aslice)
79072805 2049{
a0d0e21e
LW
2050 dSP; dMARK; dORIGMARK;
2051 register SV** svp;
2052 register AV* av = (AV*)POPs;
2053 register I32 lval = op->op_flags & OPf_MOD;
748a9306
LW
2054 I32 arybase = curcop->cop_arybase;
2055 I32 elem;
79072805 2056
a0d0e21e 2057 if (SvTYPE(av) == SVt_PVAV) {
748a9306
LW
2058 if (lval && op->op_private & OPpLVAL_INTRO) {
2059 I32 max = -1;
2060 for (svp = mark + 1; svp <= sp; svp++) {
2061 elem = SvIVx(*svp);
2062 if (elem > max)
2063 max = elem;
2064 }
2065 if (max > AvMAX(av))
2066 av_extend(av, max);
2067 }
a0d0e21e 2068 while (++MARK <= SP) {
748a9306 2069 elem = SvIVx(*MARK);
a0d0e21e 2070
748a9306
LW
2071 if (elem > 0)
2072 elem -= arybase;
a0d0e21e
LW
2073 svp = av_fetch(av, elem, lval);
2074 if (lval) {
2075 if (!svp || *svp == &sv_undef)
2076 DIE(no_aelem, elem);
2077 if (op->op_private & OPpLVAL_INTRO)
2078 save_svref(svp);
79072805 2079 }
a0d0e21e 2080 *MARK = svp ? *svp : &sv_undef;
79072805
LW
2081 }
2082 }
748a9306 2083 if (GIMME != G_ARRAY) {
a0d0e21e
LW
2084 MARK = ORIGMARK;
2085 *++MARK = *SP;
2086 SP = MARK;
2087 }
79072805
LW
2088 RETURN;
2089}
2090
2091/* Associative arrays. */
2092
2093PP(pp_each)
2094{
2095 dSP; dTARGET;
2096 HV *hash = (HV*)POPs;
c07a80fd 2097 HE *entry;
54310121 2098 I32 gimme = GIMME_V;
c07a80fd
PP
2099
2100 PUTBACK;
54310121 2101 entry = hv_iternext(hash); /* might clobber stack_sp */
c07a80fd 2102 SPAGAIN;
79072805 2103
79072805
LW
2104 EXTEND(SP, 2);
2105 if (entry) {
54310121
PP
2106 PUSHs(hv_iterkeysv(entry)); /* won't clobber stack_sp */
2107 if (gimme == G_ARRAY) {
c07a80fd 2108 PUTBACK;
54310121 2109 sv_setsv(TARG, hv_iterval(hash, entry)); /* might hit stack_sp */
c07a80fd 2110 SPAGAIN;
8990e307 2111 PUSHs(TARG);
79072805 2112 }
79072805 2113 }
54310121 2114 else if (gimme == G_SCALAR)
79072805
LW
2115 RETPUSHUNDEF;
2116
2117 RETURN;
2118}
2119
2120PP(pp_values)
2121{
2122 return do_kv(ARGS);
2123}
2124
2125PP(pp_keys)
2126{
2127 return do_kv(ARGS);
2128}
2129
2130PP(pp_delete)
2131{
2132 dSP;
54310121
PP
2133 I32 gimme = GIMME_V;
2134 I32 discard = (gimme == G_VOID) ? G_DISCARD : 0;
79072805 2135 SV *sv;
5f05dabc
PP
2136 HV *hv;
2137
2138 if (op->op_private & OPpSLICE) {
2139 dMARK; dORIGMARK;
2140 hv = (HV*)POPs;
2141 if (SvTYPE(hv) != SVt_PVHV)
2142 DIE("Not a HASH reference");
2143 while (++MARK <= SP) {
54310121 2144 sv = hv_delete_ent(hv, *MARK, discard, 0);
5f05dabc
PP
2145 *MARK = sv ? sv : &sv_undef;
2146 }
54310121
PP
2147 if (discard)
2148 SP = ORIGMARK;
2149 else if (gimme == G_SCALAR) {
5f05dabc
PP
2150 MARK = ORIGMARK;
2151 *++MARK = *SP;
2152 SP = MARK;
2153 }
2154 }
2155 else {
2156 SV *keysv = POPs;
2157 hv = (HV*)POPs;
2158 if (SvTYPE(hv) != SVt_PVHV)
2159 DIE("Not a HASH reference");
54310121 2160 sv = hv_delete_ent(hv, keysv, discard, 0);
5f05dabc
PP
2161 if (!sv)
2162 sv = &sv_undef;
54310121
PP
2163 if (!discard)
2164 PUSHs(sv);
79072805 2165 }
79072805
LW
2166 RETURN;
2167}
2168
a0d0e21e 2169PP(pp_exists)
79072805 2170{
a0d0e21e
LW
2171 dSP;
2172 SV *tmpsv = POPs;
2173 HV *hv = (HV*)POPs;
a0d0e21e
LW
2174 STRLEN len;
2175 if (SvTYPE(hv) != SVt_PVHV) {
2176 DIE("Not a HASH reference");
2177 }
f12c7020 2178 if (hv_exists_ent(hv, tmpsv, 0))
a0d0e21e
LW
2179 RETPUSHYES;
2180 RETPUSHNO;
2181}
79072805 2182
a0d0e21e
LW
2183PP(pp_hslice)
2184{
2185 dSP; dMARK; dORIGMARK;
f12c7020 2186 register HE *he;
a0d0e21e
LW
2187 register HV *hv = (HV*)POPs;
2188 register I32 lval = op->op_flags & OPf_MOD;
79072805 2189
a0d0e21e
LW
2190 if (SvTYPE(hv) == SVt_PVHV) {
2191 while (++MARK <= SP) {
f12c7020 2192 SV *keysv = *MARK;
79072805 2193
f12c7020 2194 he = hv_fetch_ent(hv, keysv, lval, 0);
a0d0e21e 2195 if (lval) {
f12c7020
PP
2196 if (!he || HeVAL(he) == &sv_undef)
2197 DIE(no_helem, SvPV(keysv, na));
a0d0e21e 2198 if (op->op_private & OPpLVAL_INTRO)
f12c7020 2199 save_svref(&HeVAL(he));
93a17b20 2200 }
f12c7020 2201 *MARK = he ? HeVAL(he) : &sv_undef;
79072805
LW
2202 }
2203 }
a0d0e21e
LW
2204 if (GIMME != G_ARRAY) {
2205 MARK = ORIGMARK;
2206 *++MARK = *SP;
2207 SP = MARK;
79072805 2208 }
a0d0e21e
LW
2209 RETURN;
2210}
2211
2212/* List operators. */
2213
2214PP(pp_list)
2215{
2216 dSP; dMARK;
2217 if (GIMME != G_ARRAY) {
2218 if (++MARK <= SP)
2219 *MARK = *SP; /* unwanted list, return last item */
8990e307 2220 else
a0d0e21e
LW
2221 *MARK = &sv_undef;
2222 SP = MARK;
79072805 2223 }
a0d0e21e 2224 RETURN;
79072805
LW
2225}
2226
a0d0e21e 2227PP(pp_lslice)
79072805
LW
2228{
2229 dSP;
a0d0e21e
LW
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;
4633a7c4
LW
2235 I32 lval = op->op_flags & OPf_MOD;
2236 I32 is_something_there = lval;
79072805 2237
a0d0e21e
LW
2238 register I32 max = lastrelem - lastlelem;
2239 register SV **lelem;
2240 register I32 ix;
2241
2242 if (GIMME != G_ARRAY) {
748a9306
LW
2243 ix = SvIVx(*lastlelem);
2244 if (ix < 0)
2245 ix += max;
2246 else
2247 ix -= arybase;
a0d0e21e
LW
2248 if (ix < 0 || ix >= max)
2249 *firstlelem = &sv_undef;
2250 else
2251 *firstlelem = firstrelem[ix];
2252 SP = firstlelem;
2253 RETURN;
2254 }
2255
2256 if (max == 0) {
2257 SP = firstlelem - 1;
2258 RETURN;
2259 }
2260
2261 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
748a9306 2262 ix = SvIVx(*lelem);
a0d0e21e
LW
2263 if (ix < 0) {
2264 ix += max;
2265 if (ix < 0)
2266 *lelem = &sv_undef;
2267 else if (!(*lelem = firstrelem[ix]))
2268 *lelem = &sv_undef;
79072805 2269 }
748a9306
LW
2270 else {
2271 ix -= arybase;
2272 if (ix >= max || !(*lelem = firstrelem[ix]))
2273 *lelem = &sv_undef;
2274 }
ff0cee69 2275 if (!is_something_there && (SvOK(*lelem) || SvGMAGICAL(*lelem)))
4633a7c4 2276 is_something_there = TRUE;
79072805 2277 }
4633a7c4
LW
2278 if (is_something_there)
2279 SP = lastlelem;
2280 else
2281 SP = firstlelem - 1;
79072805
LW
2282 RETURN;
2283}
2284
a0d0e21e
LW
2285PP(pp_anonlist)
2286{
44a8e56a 2287 dSP; dMARK; dORIGMARK;
a0d0e21e 2288 I32 items = SP - MARK;
44a8e56a
PP
2289 SV *av = sv_2mortal((SV*)av_make(items, MARK+1));
2290 SP = ORIGMARK; /* av_make() might realloc stack_sp */
2291 XPUSHs(av);
a0d0e21e
LW
2292 RETURN;
2293}
2294
2295PP(pp_anonhash)
79072805
LW
2296{
2297 dSP; dMARK; dORIGMARK;
a0d0e21e
LW
2298 HV* hv = (HV*)sv_2mortal((SV*)newHV());
2299
2300 while (MARK < SP) {
2301 SV* key = *++MARK;
a0d0e21e
LW
2302 SV *val = NEWSV(46, 0);
2303 if (MARK < SP)
2304 sv_setsv(val, *++MARK);
2305 else
2306 warn("Odd number of elements in hash list");
f12c7020 2307 (void)hv_store_ent(hv,key,val,0);
79072805 2308 }
a0d0e21e
LW
2309 SP = ORIGMARK;
2310 XPUSHs((SV*)hv);
79072805
LW
2311 RETURN;
2312}
2313
a0d0e21e 2314PP(pp_splice)
79072805 2315{
a0d0e21e
LW
2316 dSP; dMARK; dORIGMARK;
2317 register AV *ary = (AV*)*++MARK;
2318 register SV **src;
2319 register SV **dst;
2320 register I32 i;
2321 register I32 offset;
2322 register I32 length;
2323 I32 newlen;
2324 I32 after;
2325 I32 diff;
2326 SV **tmparyval = 0;
79072805 2327
a0d0e21e 2328 SP++;
79072805 2329
a0d0e21e
LW
2330 if (++MARK < SP) {
2331 offset = SvIVx(*MARK);
2332 if (offset < 0)
2333 offset += AvFILL(ary) + 1;
2334 else
2335 offset -= curcop->cop_arybase;
2336 if (++MARK < SP) {
2337 length = SvIVx(*MARK++);
2338 if (length < 0)
2339 length = 0;
79072805
LW
2340 }
2341 else
a0d0e21e 2342 length = AvMAX(ary) + 1; /* close enough to infinity */
79072805 2343 }
a0d0e21e
LW
2344 else {
2345 offset = 0;
2346 length = AvMAX(ary) + 1;
2347 }
2348 if (offset < 0) {
2349 length += offset;
2350 offset = 0;
2351 if (length < 0)
2352 length = 0;
2353 }
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 */
2359 after = 0;
2360 if (!AvALLOC(ary))
2361 av_extend(ary, 0);
2362 }
2363
2364 /* At this point, MARK .. SP-1 is our new LIST */
2365
2366 newlen = SP - MARK;
2367 diff = newlen - length;
2368
2369 if (diff < 0) { /* shrinking the area */
2370 if (newlen) {
2371 New(451, tmparyval, newlen, SV*); /* so remember insertion */
2372 Copy(MARK, tmparyval, newlen, SV*);
79072805 2373 }
a0d0e21e
LW
2374
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*);
2379 if (AvREAL(ary)) {
bbce6d69 2380 EXTEND_MORTAL(length);
36477c24
PP
2381 for (i = length, dst = MARK; i; i--) {
2382 if (!SvIMMORTAL(*dst))
2383 sv_2mortal(*dst); /* free them eventualy */
2384 dst++;
2385 }
a0d0e21e
LW
2386 }
2387 MARK += length - 1;
79072805 2388 }
a0d0e21e
LW
2389 else {
2390 *MARK = AvARRAY(ary)[offset+length-1];
2391 if (AvREAL(ary)) {
36477c24
PP
2392 if (!SvIMMORTAL(*MARK))
2393 sv_2mortal(*MARK);
a0d0e21e
LW
2394 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
2395 SvREFCNT_dec(*dst++); /* free them now */
79072805 2396 }
a0d0e21e
LW
2397 }
2398 AvFILL(ary) += diff;
2399
2400 /* pull up or down? */
2401
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 */
2407 *dst-- = *src--;
79072805 2408 }
a0d0e21e
LW
2409 dst = AvARRAY(ary);
2410 SvPVX(ary) = (char*)(AvARRAY(ary) - diff); /* diff is negative */
2411 AvMAX(ary) += diff;
2412 }
2413 else {
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*);
79072805 2418 }
a0d0e21e
LW
2419 dst = &AvARRAY(ary)[AvFILL(ary)+1];
2420 /* avoid later double free */
2421 }
2422 i = -diff;
2423 while (i)
2424 dst[--i] = &sv_undef;
2425
2426 if (newlen) {
2427 for (src = tmparyval, dst = AvARRAY(ary) + offset;
2428 newlen; newlen--) {
2429 *dst = NEWSV(46, 0);
2430 sv_setsv(*dst++, *src++);
79072805 2431 }
a0d0e21e
LW
2432 Safefree(tmparyval);
2433 }
2434 }
2435 else { /* no, expanding (or same) */
2436 if (length) {
2437 New(452, tmparyval, length, SV*); /* so remember deletion */
2438 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
2439 }
2440
2441 if (diff > 0) { /* expanding */
2442
2443 /* push up or down? */
2444
2445 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
2446 if (offset) {
2447 src = AvARRAY(ary);
2448 dst = src - diff;
2449 Move(src, dst, offset, SV*);
79072805 2450 }
a0d0e21e
LW
2451 SvPVX(ary) = (char*)(AvARRAY(ary) - diff);/* diff is positive */
2452 AvMAX(ary) += diff;
2453 AvFILL(ary) += diff;
79072805
LW
2454 }
2455 else {
a0d0e21e
LW
2456 if (AvFILL(ary) + diff >= AvMAX(ary)) /* oh, well */
2457 av_extend(ary, AvFILL(ary) + diff);
2458 AvFILL(ary) += diff;
2459
2460 if (after) {
2461 dst = AvARRAY(ary) + AvFILL(ary);
2462 src = dst - diff;
2463 for (i = after; i; i--) {
2464 *dst-- = *src--;
2465 }
79072805
LW
2466 }
2467 }
a0d0e21e
LW
2468 }
2469
2470 for (src = MARK, dst = AvARRAY(ary) + offset; newlen; newlen--) {
2471 *dst = NEWSV(46, 0);
2472 sv_setsv(*dst++, *src++);
2473 }
2474 MARK = ORIGMARK + 1;
2475 if (GIMME == G_ARRAY) { /* copy return vals to stack */
2476 if (length) {
2477 Copy(tmparyval, MARK, length, SV*);
2478 if (AvREAL(ary)) {
bbce6d69 2479 EXTEND_MORTAL(length);
36477c24
PP
2480 for (i = length, dst = MARK; i; i--) {
2481 if (!SvIMMORTAL(*dst))
2482 sv_2mortal(*dst); /* free them eventualy */
2483 dst++;
2484 }
79072805 2485 }
a0d0e21e 2486 Safefree(tmparyval);
79072805 2487 }
a0d0e21e
LW
2488 MARK += length - 1;
2489 }
2490 else if (length--) {
2491 *MARK = tmparyval[length];
2492 if (AvREAL(ary)) {
36477c24
PP
2493 if (!SvIMMORTAL(*MARK))
2494 sv_2mortal(*MARK);
a0d0e21e
LW
2495 while (length-- > 0)
2496 SvREFCNT_dec(tmparyval[length]);
79072805 2497 }
a0d0e21e 2498 Safefree(tmparyval);
79072805 2499 }
a0d0e21e
LW
2500 else
2501 *MARK = &sv_undef;
79072805 2502 }
a0d0e21e 2503 SP = MARK;
79072805
LW
2504 RETURN;
2505}
2506
a0d0e21e 2507PP(pp_push)
79072805
LW
2508{
2509 dSP; dMARK; dORIGMARK; dTARGET;
a0d0e21e
LW
2510 register AV *ary = (AV*)*++MARK;
2511 register SV *sv = &sv_undef;
79072805 2512
a0d0e21e
LW
2513 for (++MARK; MARK <= SP; MARK++) {
2514 sv = NEWSV(51, 0);
2515 if (*MARK)
2516 sv_setsv(sv, *MARK);
2517 av_push(ary, sv);
79072805
LW
2518 }
2519 SP = ORIGMARK;
a0d0e21e 2520 PUSHi( AvFILL(ary) + 1 );
79072805
LW
2521 RETURN;
2522}
2523
a0d0e21e 2524PP(pp_pop)
79072805
LW
2525{
2526 dSP;
a0d0e21e
LW
2527 AV *av = (AV*)POPs;
2528 SV *sv = av_pop(av);
36477c24 2529 if (!SvIMMORTAL(sv) && AvREAL(av))
a0d0e21e
LW
2530 (void)sv_2mortal(sv);
2531 PUSHs(sv);
79072805 2532 RETURN;
79072805
LW
2533}
2534
a0d0e21e 2535PP(pp_shift)
79072805
LW
2536{
2537 dSP;
a0d0e21e
LW
2538 AV *av = (AV*)POPs;
2539 SV *sv = av_shift(av);
79072805 2540 EXTEND(SP, 1);
a0d0e21e 2541 if (!sv)
79072805 2542 RETPUSHUNDEF;
36477c24 2543 if (!SvIMMORTAL(sv) && AvREAL(av))
a0d0e21e
LW
2544 (void)sv_2mortal(sv);
2545 PUSHs(sv);
79072805 2546 RETURN;
79072805
LW
2547}
2548
a0d0e21e 2549PP(pp_unshift)
79072805 2550{
a0d0e21e
LW
2551 dSP; dMARK; dORIGMARK; dTARGET;
2552 register AV *ary = (AV*)*++MARK;
2553 register SV *sv;
2554 register I32 i = 0;
79072805 2555
a0d0e21e
LW
2556 av_unshift(ary, SP - MARK);
2557 while (MARK < SP) {
2558 sv = NEWSV(27, 0);
2559 sv_setsv(sv, *++MARK);
2560 (void)av_store(ary, i++, sv);
79072805 2561 }
79072805 2562
a0d0e21e
LW
2563 SP = ORIGMARK;
2564 PUSHi( AvFILL(ary) + 1 );
79072805 2565 RETURN;
79072805
LW
2566}
2567
a0d0e21e 2568PP(pp_reverse)
79072805 2569{
a0d0e21e
LW
2570 dSP; dMARK;
2571 register SV *tmp;
2572 SV **oldsp = SP;
79072805 2573
a0d0e21e
LW
2574 if (GIMME == G_ARRAY) {
2575 MARK++;
2576 while (MARK < SP) {
2577 tmp = *MARK;
2578 *MARK++ = *SP;
2579 *SP-- = tmp;
2580 }
2581 SP = oldsp;
79072805
LW
2582 }
2583 else {
a0d0e21e
LW
2584 register char *up;
2585 register char *down;
2586 register I32 tmp;
2587 dTARGET;
2588 STRLEN len;
79072805 2589
a0d0e21e
LW
2590 if (SP - MARK > 1)
2591 do_join(TARG, &sv_no, MARK, SP);
2592 else
54310121 2593 sv_setsv(TARG, (SP > MARK) ? *SP : GvSV(defgv));
a0d0e21e
LW
2594 up = SvPV_force(TARG, len);
2595 if (len > 1) {
2596 down = SvPVX(TARG) + len - 1;
2597 while (down > up) {
2598 tmp = *up;
2599 *up++ = *down;
2600 *down-- = tmp;
2601 }
2602 (void)SvPOK_only(TARG);
79072805 2603 }
a0d0e21e
LW
2604 SP = MARK + 1;
2605 SETTARG;
79072805 2606 }
a0d0e21e 2607 RETURN;
79072805
LW
2608}
2609
55497cff
PP
2610static SV *
2611mul128(sv, m)
2612 SV *sv;
2613 U8 m;
2614{
2615 STRLEN len;
2616 char *s = SvPV(sv, len);
2617 char *t;
2618 U32 i = 0;
2619
2620 if (!strnEQ(s, "0000", 4)) { /* need to grow sv */
2621 SV *new = newSVpv("0000000000", 10);
2622
2623 sv_catsv(new, sv);
2624 SvREFCNT_dec(sv); /* free old sv */
2625 sv = new;
2626 s = SvPV(sv, len);
2627 }
2628 t = s + len - 1;
2629 while (!*t) /* trailing '\0'? */
2630 t--;
2631 while (t > s) {
2632 i = ((*t - '0') << 7) + m;
2633 *(t--) = '0' + (i % 10);
2634 m = i / 10;
2635 }
2636 return (sv);
2637}
2638
a0d0e21e
LW
2639/* Explosives and implosives. */
2640
2641PP(pp_unpack)
79072805
LW
2642{
2643 dSP;
a0d0e21e 2644 dPOPPOPssrl;
36477c24 2645 SV **oldsp = sp;
54310121 2646 I32 gimme = GIMME_V;
ed6116ce 2647 SV *sv;
a0d0e21e
LW
2648 STRLEN llen;
2649 STRLEN rlen;
2650 register char *pat = SvPV(left, llen);
2651 register char *s = SvPV(right, rlen);
2652 char *strend = s + rlen;
2653 char *strbeg = s;
2654 register char *patend = pat + llen;
2655 I32 datumtype;
2656 register I32 len;
2657 register I32 bits;
79072805 2658
a0d0e21e
LW
2659 /* These must not be in registers: */
2660 I16 ashort;
2661 int aint;
2662 I32 along;
ecfc5424
AD
2663#ifdef HAS_QUAD
2664 Quad_t aquad;
a0d0e21e
LW
2665#endif
2666 U16 aushort;
2667 unsigned int auint;
2668 U32 aulong;
ecfc5424
AD
2669#ifdef HAS_QUAD
2670 unsigned Quad_t auquad;
a0d0e21e
LW
2671#endif
2672 char *aptr;
2673 float afloat;
2674 double adouble;
2675 I32 checksum = 0;
2676 register U32 culong;
2677 double cdouble;
2678 static char* bitcount = 0;
79072805 2679
54310121 2680 if (gimme != G_ARRAY) { /* arrange to do first one only */
a0d0e21e
LW
2681 /*SUPPRESS 530*/
2682 for (patend = pat; !isALPHA(*patend) || *patend == 'x'; patend++) ;
748a9306 2683 if (strchr("aAbBhHP", *patend) || *pat == '%') {
a0d0e21e
LW
2684 patend++;
2685 while (isDIGIT(*patend) || *patend == '*')
2686 patend++;
2687 }
2688 else
2689 patend++;
79072805 2690 }
a0d0e21e
LW
2691 while (pat < patend) {
2692 reparse:
bbdab043
CS
2693 datumtype = *pat++ & 0xFF;
2694 if (isSPACE(datumtype))
2695 continue;
a0d0e21e
LW
2696 if (pat >= patend)
2697 len = 1;
2698 else if (*pat == '*') {
2699 len = strend - strbeg; /* long enough */
2700 pat++;
2701 }
2702 else if (isDIGIT(*pat)) {
2703 len = *pat++ - '0';
2704 while (isDIGIT(*pat))
2705 len = (len * 10) + (*pat++ - '0');
2706 }
2707 else
2708 len = (datumtype != '@');
2709 switch(datumtype) {
2710 default:
bbdab043 2711 croak("Invalid type in unpack: '%c'", (int)datumtype);
a0d0e21e
LW
2712 case '%':
2713 if (len == 1 && pat[-1] != '1')
2714 len = 16;
2715 checksum = len;
2716 culong = 0;
2717 cdouble = 0;
2718 if (pat < patend)
2719 goto reparse;
2720 break;
2721 case '@':
2722 if (len > strend - strbeg)
2723 DIE("@ outside of string");
2724 s = strbeg + len;
2725 break;
2726 case 'X':
2727 if (len > s - strbeg)
2728 DIE("X outside of string");
2729 s -= len;
2730 break;
2731 case 'x':
2732 if (len > strend - s)
2733 DIE("x outside of string");
2734 s += len;
2735 break;
2736 case 'A':
2737 case 'a':
2738 if (len > strend - s)
2739 len = strend - s;
2740 if (checksum)
2741 goto uchar_checksum;
2742 sv = NEWSV(35, len);
2743 sv_setpvn(sv, s, len);
2744 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)))
2749 s--;
2750 *++s = '\0';
2751 SvCUR_set(sv, s - SvPVX(sv));
2752 s = aptr; /* unborrow register */
2753 }
2754 XPUSHs(sv_2mortal(sv));
2755 break;
2756 case 'B':
2757 case 'b':
2758 if (pat[-1] == '*' || len > (strend - s) * 8)
2759 len = (strend - s) * 8;
2760 if (checksum) {
2761 if (!bitcount) {
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]++;
2772 }
2773 }
2774 while (len >= 8) {
2775 culong += bitcount[*(unsigned char*)s++];
2776 len -= 8;
2777 }
2778 if (len) {
2779 bits = *s;
2780 if (datumtype == 'b') {
2781 while (len-- > 0) {
2782 if (bits & 1) culong++;
2783 bits >>= 1;
2784 }
2785 }
2786 else {
2787 while (len-- > 0) {
2788 if (bits & 128) culong++;
2789 bits <<= 1;
2790 }
2791 }
2792 }
79072805
LW
2793 break;
2794 }
a0d0e21e
LW
2795 sv = NEWSV(35, len + 1);
2796 SvCUR_set(sv, len);
2797 SvPOK_on(sv);
2798 aptr = pat; /* borrow register */
2799 pat = SvPVX(sv);
2800 if (datumtype == 'b') {
2801 aint = len;
2802 for (len = 0; len < aint; len++) {
2803 if (len & 7) /*SUPPRESS 595*/
2804 bits >>= 1;
2805 else
2806 bits = *s++;
2807 *pat++ = '0' + (bits & 1);
2808 }
2809 }
2810 else {
2811 aint = len;
2812 for (len = 0; len < aint; len++) {
2813 if (len & 7)
2814 bits <<= 1;
2815 else
2816 bits = *s++;
2817 *pat++ = '0' + ((bits & 128) != 0);
2818 }
2819 }
2820 *pat = '\0';
2821 pat = aptr; /* unborrow register */
2822 XPUSHs(sv_2mortal(sv));
2823 break;
2824 case 'H':
2825 case 'h':
2826 if (pat[-1] == '*' || len > (strend - s) * 2)
2827 len = (strend - s) * 2;
2828 sv = NEWSV(35, len + 1);
2829 SvCUR_set(sv, len);
2830 SvPOK_on(sv);
2831 aptr = pat; /* borrow register */
2832 pat = SvPVX(sv);
2833 if (datumtype == 'h') {
2834 aint = len;
2835 for (len = 0; len < aint; len++) {
2836 if (len & 1)
2837 bits >>= 4;
2838 else
2839 bits = *s++;
2840 *pat++ = hexdigit[bits & 15];
2841 }
2842 }
2843 else {
2844 aint = len;
2845 for (len = 0; len < aint; len++) {
2846 if (len & 1)
2847 bits <<= 4;
2848 else
2849 bits = *s++;
2850 *pat++ = hexdigit[(bits >> 4) & 15];
2851 }
2852 }
2853 *pat = '\0';
2854 pat = aptr; /* unborrow register */
2855 XPUSHs(sv_2mortal(sv));
2856 break;
2857 case 'c':
2858 if (len > strend - s)
2859 len = strend - s;
2860 if (checksum) {
2861 while (len-- > 0) {
2862 aint = *s++;
2863 if (aint >= 128) /* fake up signed chars */
2864 aint -= 256;
2865 culong += aint;
2866 }
2867 }
2868 else {
2869 EXTEND(SP, len);
bbce6d69 2870 EXTEND_MORTAL(len);
a0d0e21e
LW
2871 while (len-- > 0) {
2872 aint = *s++;
2873 if (aint >= 128) /* fake up signed chars */
2874 aint -= 256;
2875 sv = NEWSV(36, 0);
1e422769 2876 sv_setiv(sv, (IV)aint);
a0d0e21e
LW
2877 PUSHs(sv_2mortal(sv));
2878 }
2879 }
2880 break;
2881 case 'C':
2882 if (len > strend - s)
2883 len = strend - s;
2884 if (checksum) {
2885 uchar_checksum:
2886 while (len-- > 0) {
2887 auint = *s++ & 255;
2888 culong += auint;
2889 }
2890 }
2891 else {
2892 EXTEND(SP, len);
bbce6d69 2893 EXTEND_MORTAL(len);
a0d0e21e
LW
2894 while (len-- > 0) {
2895 auint = *s++ & 255;
2896 sv = NEWSV(37, 0);
1e422769 2897 sv_setiv(sv, (IV)auint);
a0d0e21e
LW
2898 PUSHs(sv_2mortal(sv));
2899 }
2900 }
2901 break;
2902 case 's':
96e4d5b1 2903 along = (strend - s) / SIZE16;
a0d0e21e
LW
2904 if (len > along)
2905 len = along;
2906 if (checksum) {
2907 while (len-- > 0) {
96e4d5b1
PP
2908 COPY16(s, &ashort);
2909 s += SIZE16;
a0d0e21e
LW
2910 culong += ashort;
2911 }
2912 }
2913 else {
2914 EXTEND(SP, len);
bbce6d69 2915 EXTEND_MORTAL(len);
a0d0e21e 2916 while (len-- > 0) {
96e4d5b1
PP
2917 COPY16(s, &ashort);
2918 s += SIZE16;
a0d0e21e 2919 sv = NEWSV(38, 0);
1e422769 2920 sv_setiv(sv, (IV)ashort);
a0d0e21e
LW
2921 PUSHs(sv_2mortal(sv));
2922 }
2923 }
2924 break;
2925 case 'v':
2926 case 'n':
2927 case 'S':
96e4d5b1 2928 along = (strend - s) / SIZE16;
a0d0e21e
LW
2929 if (len > along)
2930 len = along;
2931 if (checksum) {
2932 while (len-- > 0) {
96e4d5b1
PP
2933 COPY16(s, &aushort);
2934 s += SIZE16;
a0d0e21e
LW
2935#ifdef HAS_NTOHS
2936 if (datumtype == 'n')
2937 aushort = ntohs(aushort);
79072805 2938#endif
a0d0e21e
LW
2939#ifdef HAS_VTOHS
2940 if (datumtype == 'v')
2941 aushort = vtohs(aushort);
79072805 2942#endif
a0d0e21e
LW
2943 culong += aushort;
2944 }
2945 }
2946 else {
2947 EXTEND(SP, len);
bbce6d69 2948 EXTEND_MORTAL(len);
a0d0e21e 2949 while (len-- > 0) {
96e4d5b1
PP
2950 COPY16(s, &aushort);
2951 s += SIZE16;
a0d0e21e
LW
2952 sv = NEWSV(39, 0);
2953#ifdef HAS_NTOHS
2954 if (datumtype == 'n')
2955 aushort = ntohs(aushort);
79072805 2956#endif
a0d0e21e
LW
2957#ifdef HAS_VTOHS
2958 if (datumtype == 'v')
2959 aushort = vtohs(aushort);
79072805 2960#endif
1e422769 2961 sv_setiv(sv, (IV)aushort);
a0d0e21e
LW
2962 PUSHs(sv_2mortal(sv));
2963 }
2964 }
2965 break;
2966 case 'i':
2967 along = (strend - s) / sizeof(int);
2968 if (len > along)
2969 len = along;
2970 if (checksum) {
2971 while (len-- > 0) {
2972 Copy(s, &aint, 1, int);
2973 s += sizeof(int);
2974 if (checksum > 32)
2975 cdouble += (double)aint;
2976 else
2977 culong += aint;
2978 }
2979 }
2980 else {
2981 EXTEND(SP, len);
bbce6d69 2982 EXTEND_MORTAL(len);
a0d0e21e
LW
2983 while (len-- > 0) {
2984 Copy(s, &aint, 1, int);
2985 s += sizeof(int);
2986 sv = NEWSV(40, 0);
1e422769 2987 sv_setiv(sv, (IV)aint);
a0d0e21e
LW
2988 PUSHs(sv_2mortal(sv));
2989 }
2990 }
2991 break;
2992 case 'I':
2993 along = (strend - s) / sizeof(unsigned int);
2994 if (len > along)
2995 len = along;
2996 if (checksum) {
2997 while (len-- > 0) {
2998 Copy(s, &auint, 1, unsigned int);
2999 s += sizeof(unsigned int);
3000 if (checksum > 32)
3001 cdouble += (double)auint;
3002 else
3003 culong += auint;
3004 }
3005 }
3006 else {
3007 EXTEND(SP, len);
bbce6d69 3008 EXTEND_MORTAL(len);
a0d0e21e
LW
3009 while (len-- > 0) {
3010 Copy(s, &auint, 1, unsigned int);
3011 s += sizeof(unsigned int);
3012 sv = NEWSV(41, 0);
1e422769 3013 sv_setuv(sv, (UV)auint);
a0d0e21e
LW
3014 PUSHs(sv_2mortal(sv));
3015 }
3016 }
3017 break;
3018 case 'l':
96e4d5b1 3019 along = (strend - s) / SIZE32;
a0d0e21e
LW
3020 if (len > along)
3021 len = along;
3022 if (checksum) {
3023 while (len-- > 0) {
96e4d5b1
PP
3024 COPY32(s, &along);
3025 s += SIZE32;
a0d0e21e
LW
3026 if (checksum > 32)
3027 cdouble += (double)along;
3028 else
3029 culong += along;
3030 }
3031 }
3032 else {
3033 EXTEND(SP, len);
bbce6d69 3034 EXTEND_MORTAL(len);
a0d0e21e 3035 while (len-- > 0) {
96e4d5b1
PP
3036 COPY32(s, &along);
3037 s += SIZE32;
a0d0e21e 3038 sv = NEWSV(42, 0);
1e422769 3039 sv_setiv(sv, (IV)along);
a0d0e21e
LW
3040 PUSHs(sv_2mortal(sv));
3041 }
79072805 3042 }
a0d0e21e
LW
3043 break;
3044 case 'V':
3045 case 'N':
3046 case 'L':
96e4d5b1 3047 along = (strend - s) / SIZE32;
a0d0e21e
LW
3048 if (len > along)
3049 len = along;
3050 if (checksum) {
3051 while (len-- > 0) {
96e4d5b1
PP
3052 COPY32(s, &aulong);
3053 s += SIZE32;
a0d0e21e
LW
3054#ifdef HAS_NTOHL
3055 if (datumtype == 'N')
3056 aulong = ntohl(aulong);
79072805 3057#endif
a0d0e21e
LW
3058#ifdef HAS_VTOHL
3059 if (datumtype == 'V')
3060 aulong = vtohl(aulong);
79072805 3061#endif
a0d0e21e
LW
3062 if (checksum > 32)
3063 cdouble += (double)aulong;
3064 else
3065 culong += aulong;
3066 }
3067 }
3068 else {
3069 EXTEND(SP, len);
bbce6d69 3070 EXTEND_MORTAL(len);
a0d0e21e 3071 while (len-- > 0) {
96e4d5b1
PP
3072 COPY32(s, &aulong);
3073 s += SIZE32;
a0d0e21e
LW
3074#ifdef HAS_NTOHL
3075 if (datumtype == 'N')
3076 aulong = ntohl(aulong);
79072805 3077#endif
a0d0e21e
LW
3078#ifdef HAS_VTOHL
3079 if (datumtype == 'V')
3080 aulong = vtohl(aulong);
79072805 3081#endif
1e422769
PP
3082 sv = NEWSV(43, 0);
3083 sv_setuv(sv, (UV)aulong);
a0d0e21e
LW
3084 PUSHs(sv_2mortal(sv));
3085 }
3086 }
3087 break;
3088 case 'p':
3089 along = (strend - s) / sizeof(char*);
3090 if (len > along)
3091 len = along;
3092 EXTEND(SP, len);
bbce6d69 3093 EXTEND_MORTAL(len);
a0d0e21e
LW
3094 while (len-- > 0) {
3095 if (sizeof(char*) > strend - s)
3096 break;
3097 else {
3098 Copy(s, &aptr, 1, char*);
3099 s += sizeof(char*);
3100 }
3101 sv = NEWSV(44, 0);
3102 if (aptr)
3103 sv_setpv(sv, aptr);
3104 PUSHs(sv_2mortal(sv));
3105 }
3106 break;
def98dd4 3107 case 'w':
def98dd4 3108 EXTEND(SP, len);
bbce6d69
PP
3109 EXTEND_MORTAL(len);
3110 {
3111 UV auv = 0;
3112 U32 bytes = 0;
3113
3114 while ((len > 0) && (s < strend)) {
3115 auv = (auv << 7) | (*s & 0x7f);
3116 if (!(*s++ & 0x80)) {
3117 bytes = 0;
3118 sv = NEWSV(40, 0);
3119 sv_setuv(sv, auv);
3120 PUSHs(sv_2mortal(sv));
3121 len--;
3122 auv = 0;
3123 }
3124 else if (++bytes >= sizeof(UV)) { /* promote to string */
bbce6d69
PP
3125 char *t;
3126
fc36a67e 3127 sv = newSVpvf("%.*Vu", (int)TYPE_DIGITS(UV), auv);
bbce6d69
PP
3128 while (s < strend) {
3129 sv = mul128(sv, *s & 0x7f);
3130 if (!(*s++ & 0x80)) {
3131 bytes = 0;
3132 break;
3133 }
3134 }
3135 t = SvPV(sv, na);
3136 while (*t == '0')
3137 t++;
3138 sv_chop(sv, t);
3139 PUSHs(sv_2mortal(sv));
3140 len--;
3141 auv = 0;
3142 }
3143 }
3144 if ((s >= strend) && bytes)
3145 croak("Unterminated compressed integer");
3146 }
def98dd4 3147 break;
a0d0e21e
LW
3148 case 'P':
3149 EXTEND(SP, 1);
3150 if (sizeof(char*) > strend - s)
3151 break;
3152 else {
3153 Copy(s, &aptr, 1, char*);
3154 s += sizeof(char*);
3155 }
3156 sv = NEWSV(44, 0);
3157 if (aptr)
3158 sv_setpvn(sv, aptr, len);
3159 PUSHs(sv_2mortal(sv));
3160 break;
ecfc5424 3161#ifdef HAS_QUAD
a0d0e21e
LW
3162 case 'q':
3163 EXTEND(SP, len);
bbce6d69 3164 EXTEND_MORTAL(len);
a0d0e21e 3165 while (len-- > 0) {
ecfc5424 3166 if (s + sizeof(Quad_t) > strend)
a0d0e21e
LW
3167 aquad = 0;
3168 else {
ecfc5424
AD
3169 Copy(s, &aquad, 1, Quad_t);
3170 s += sizeof(Quad_t);
a0d0e21e
LW
3171 }
3172 sv = NEWSV(42, 0);
96e4d5b1
PP
3173 if (aquad >= IV_MIN && aquad <= IV_MAX)
3174 sv_setiv(sv, (IV)aquad);
3175 else
3176 sv_setnv(sv, (double)aquad);
a0d0e21e
LW
3177 PUSHs(sv_2mortal(sv));
3178 }
3179 break;
3180 case 'Q':
3181 EXTEND(SP, len);
bbce6d69 3182 EXTEND_MORTAL(len);
a0d0e21e 3183 while (len-- > 0) {
ecfc5424 3184 if (s + sizeof(unsigned Quad_t) > strend)
a0d0e21e
LW
3185 auquad = 0;
3186 else {
ecfc5424
AD
3187 Copy(s, &auquad, 1, unsigned Quad_t);
3188 s += sizeof(unsigned Quad_t);
a0d0e21e
LW
3189 }
3190 sv = NEWSV(43, 0);
96e4d5b1
PP
3191 if (aquad <= UV_MAX)
3192 sv_setuv(sv, (UV)auquad);
3193 else
3194 sv_setnv(sv, (double)auquad);
a0d0e21e
LW
3195 PUSHs(sv_2mortal(sv));
3196 }
3197 break;
79072805 3198#endif
a0d0e21e
LW
3199 /* float and double added gnb@melba.bby.oz.au 22/11/89 */
3200 case 'f':
3201 case 'F':
3202 along = (strend - s) / sizeof(float);
3203 if (len > along)
3204 len = along;
3205 if (checksum) {
3206 while (len-- > 0) {
3207 Copy(s, &afloat, 1, float);
3208 s += sizeof(float);
3209 cdouble += afloat;
3210 }
3211 }
3212 else {
3213 EXTEND(SP, len);
bbce6d69 3214 EXTEND_MORTAL(len);
a0d0e21e
LW
3215 while (len-- > 0) {
3216 Copy(s, &afloat, 1, float);
3217 s += sizeof(float);
3218 sv = NEWSV(47, 0);
3219 sv_setnv(sv, (double)afloat);
3220 PUSHs(sv_2mortal(sv));
3221 }
3222 }
3223 break;
3224 case 'd':
3225 case 'D':
3226 along = (strend - s) / sizeof(double);
3227 if (len > along)
3228 len = along;
3229 if (checksum) {
3230 while (len-- > 0) {
3231 Copy(s, &adouble, 1, double);
3232 s += sizeof(double);
3233 cdouble += adouble;
3234 }
3235 }
3236 else {
3237 EXTEND(SP, len);
bbce6d69 3238 EXTEND_MORTAL(len);
a0d0e21e
LW
3239 while (len-- > 0) {
3240 Copy(s, &adouble, 1, double);
3241 s += sizeof(double);
3242 sv = NEWSV(48, 0);
3243 sv_setnv(sv, (double)adouble);
3244 PUSHs(sv_2mortal(sv));
3245 }
3246 }
3247 break;
3248 case 'u':
3249 along = (strend - s) * 3 / 4;
3250 sv = NEWSV(42, along);
f12c7020
PP
3251 if (along)
3252 SvPOK_on(sv);
a0d0e21e
LW
3253 while (s < strend && *s > ' ' && *s < 'a') {
3254 I32 a, b, c, d;
3255 char hunk[4];
79072805 3256
a0d0e21e
LW
3257 hunk[3] = '\0';
3258 len = (*s++ - ' ') & 077;
3259 while (len > 0) {
3260 if (s < strend && *s >= ' ')
3261 a = (*s++ - ' ') & 077;
3262 else
3263 a = 0;
3264 if (s < strend && *s >= ' ')
3265 b = (*s++ - ' ') & 077;
3266 else
3267 b = 0;
3268 if (s < strend && *s >= ' ')
3269 c = (*s++ - ' ') & 077;
3270 else
3271 c = 0;
3272 if (s < strend && *s >= ' ')
3273 d = (*s++ - ' ') & 077;
3274 else
3275 d = 0;
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);
3280 len -= 3;
3281 }
3282 if (*s == '\n')
3283 s++;
3284 else if (s[1] == '\n') /* possible checksum byte */
3285 s += 2;
79072805 3286 }
a0d0e21e
LW
3287 XPUSHs(sv_2mortal(sv));
3288 break;
79072805 3289 }
a0d0e21e
LW
3290 if (checksum) {
3291 sv = NEWSV(42, 0);
3292 if (strchr("fFdD", datumtype) ||
3293 (checksum > 32 && strchr("iIlLN", datumtype)) ) {
3294 double trouble;
79072805 3295
a0d0e21e
LW
3296 adouble = 1.0;
3297 while (checksum >= 16) {
3298 checksum -= 16;
3299 adouble *= 65536.0;
3300 }
3301 while (checksum >= 4) {
3302 checksum -= 4;
3303 adouble *= 16.0;
3304 }
3305 while (checksum--)
3306 adouble *= 2.0;
3307 along = (1 << checksum) - 1;
3308 while (cdouble < 0.0)
3309 cdouble += adouble;
3310 cdouble = modf(cdouble / adouble, &trouble) * adouble;
3311 sv_setnv(sv, cdouble);
3312 }
3313 else {
3314 if (checksum < 32) {
96e4d5b1
PP
3315 aulong = (1 << checksum) - 1;
3316 culong &= aulong;
a0d0e21e 3317 }
96e4d5b1 3318 sv_setuv(sv, (UV)culong);
a0d0e21e
LW
3319 }
3320 XPUSHs(sv_2mortal(sv));
3321 checksum = 0;
79072805 3322 }
79072805 3323 }
54310121 3324 if (sp == oldsp && gimme == G_SCALAR)
36477c24 3325 PUSHs(&sv_undef);
79072805 3326 RETURN;
79072805
LW
3327}
3328
a0d0e21e
LW
3329static void
3330doencodes(sv, s, len)
3331register SV *sv;
3332register char *s;
3333register I32 len;
79072805 3334{
a0d0e21e 3335 char hunk[5];
79072805 3336
a0d0e21e
LW
3337 *hunk = len + ' ';
3338 sv_catpvn(sv, hunk, 1);
3339 hunk[4] = '\0';
3340 while (len > 0) {
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);
3346 s += 3;
3347 len -= 3;
3348 }
3349 for (s = SvPVX(sv); *s; s++) {
3350 if (*s == ' ')
3351 *s = '`';
3352 }
3353 sv_catpvn(sv, "\n", 1);
79072805
LW
3354}
3355
55497cff
PP
3356static SV *
3357is_an_int(s, l)
3358 char *s;
3359 STRLEN l;
3360{
3361 SV *result = newSVpv("", l);
3362 char *result_c = SvPV(result, na); /* convenience */
3363 char *out = result_c;
3364 bool skip = 1;
3365 bool ignore = 0;
3366
3367 while (*s) {
3368 switch (*s) {
3369 case ' ':
3370 break;
3371 case '+':
3372 if (!skip) {
3373 SvREFCNT_dec(result);
3374 return (NULL);
3375 }
3376 break;
3377 case '0':
3378 case '1':
3379 case '2':
3380 case '3':
3381 case '4':
3382 case '5':
3383 case '6':
3384 case '7':
3385 case '8':
3386 case '9':
3387 skip = 0;
3388 if (!ignore) {
3389 *(out++) = *s;
3390 }
3391 break;
3392 case '.':
3393 ignore = 1;
3394 break;
3395 default:
3396 SvREFCNT_dec(result);
3397 return (NULL);
3398 }
3399 s++;
3400 }
3401 *(out++) = '\0';
3402 SvCUR_set(result, out - result_c);
3403 return (result);
3404}
3405
3406static int
3407div128(pnum, done)
3408 SV *pnum; /* must be '\0' terminated */
3409 bool *done;
3410{
3411 STRLEN len;
3412 char *s = SvPV(pnum, len);
3413 int m = 0;
3414 int r = 0;
3415 char *t = s;
3416
3417 *done = 1;
3418 while (*t) {
3419 int i;
3420
3421 i = m * 10 + (*t - '0');
3422 m = i & 0x7F;
3423 r = (i >> 7); /* r < 10 */
3424 if (r) {
3425 *done = 0;
3426 }
3427 *(t++) = '0' + r;
3428 }
3429 *(t++) = '\0';
3430 SvCUR_set(pnum, (STRLEN) (t - s));
3431 return (m);
3432}
3433
3434
a0d0e21e 3435PP(pp_pack)
79072805 3436{
a0d0e21e
LW
3437 dSP; dMARK; dORIGMARK; dTARGET;
3438 register SV *cat = TARG;
3439 register I32 items;
3440 STRLEN fromlen;
3441 register char *pat = SvPVx(*++MARK, fromlen);
3442 register char *patend = pat + fromlen;
3443 register I32 len;
3444 I32 datumtype;
3445 SV *fromstr;
3446 /*SUPPRESS 442*/
3447 static char null10[] = {0,0,0,0,0,0,0,0,0,0};
3448 static char *space10 = " ";
79072805 3449
a0d0e21e
LW
3450 /* These must not be in registers: */
3451 char achar;
3452 I16 ashort;
3453 int aint;
3454 unsigned int auint;
3455 I32 along;
3456 U32 aulong;
ecfc5424
AD
3457#ifdef HAS_QUAD
3458 Quad_t aquad;
3459 unsigned Quad_t auquad;
79072805 3460#endif
a0d0e21e
LW
3461 char *aptr;
3462 float afloat;
3463 double adouble;
79072805 3464
a0d0e21e
LW
3465 items = SP - MARK;
3466 MARK++;
3467 sv_setpvn(cat, "", 0);
3468 while (pat < patend) {
3469#define NEXTFROM (items-- > 0 ? *MARK++ : &sv_no)
bbdab043
CS
3470 datumtype = *pat++ & 0xFF;
3471 if (isSPACE(datumtype))
3472 continue;
a0d0e21e
LW
3473 if (*pat == '*') {
3474 len = strchr("@Xxu", datumtype) ? 0 : items;
3475 pat++;
3476 }
3477 else if (isDIGIT(*pat)) {
3478 len = *pat++ - '0';
3479 while (isDIGIT(*pat))
3480 len = (len * 10) + (*pat++ - '0');
3481 }
3482 else
3483 len = 1;
3484 switch(datumtype) {
3485 default:
bbdab043 3486 croak("Invalid type in pack: '%c'", (int)datumtype);
a0d0e21e
LW
3487 case '%':
3488 DIE("%% may only be used in unpack");
3489 case '@':
3490 len -= SvCUR(cat);
3491 if (len > 0)
3492 goto grow;
3493 len = -len;
3494 if (len > 0)
3495 goto shrink;
3496 break;
3497 case 'X':
3498 shrink:
3499 if (SvCUR(cat) < len)
3500 DIE("X outside of string");
3501 SvCUR(cat) -= len;
3502 *SvEND(cat) = '\0';
3503 break;
3504 case 'x':
3505 grow:
3506 while (len >= 10) {
3507 sv_catpvn(cat, null10, 10);
3508 len -= 10;
3509 }
3510 sv_catpvn(cat, null10, len);
3511 break;
3512 case 'A':
3513 case 'a':
3514 fromstr = NEXTFROM;
3515 aptr = SvPV(fromstr, fromlen);
3516 if (pat[-1] == '*')
3517 len = fromlen;
3518 if (fromlen > len)
3519 sv_catpvn(cat, aptr, len);
3520 else {
3521 sv_catpvn(cat, aptr, fromlen);
3522 len -= fromlen;
3523 if (datumtype == 'A') {
3524 while (len >= 10) {
3525 sv_catpvn(cat, space10, 10);
3526 len -= 10;
3527 }
3528 sv_catpvn(cat, space10, len);
3529 }
3530 else {
3531 while (len >= 10) {
3532 sv_catpvn(cat, null10, 10);
3533 len -= 10;
3534 }
3535 sv_catpvn(cat, null10, len);
3536 }
3537 }
3538 break;
3539 case 'B':
3540 case 'b':
3541 {
3542 char *savepat = pat;
3543 I32 saveitems;
79072805 3544
a0d0e21e
LW
3545 fromstr = NEXTFROM;
3546 saveitems = items;
3547 aptr = SvPV(fromstr, fromlen);
3548 if (pat[-1] == '*')
3549 len = fromlen;
3550 pat = aptr;
3551 aint = SvCUR(cat);
3552 SvCUR(cat) += (len+7)/8;
3553 SvGROW(cat, SvCUR(cat) + 1);
3554 aptr = SvPVX(cat) + aint;
3555 if (len > fromlen)
3556 len = fromlen;
3557 aint = len;
3558 items = 0;
3559 if (datumtype == 'B') {
3560 for (len = 0; len++ < aint;) {
3561 items |= *pat++ & 1;
3562 if (len & 7)
3563 items <<= 1;
3564 else {
3565 *aptr++ = items & 0xff;
3566 items = 0;
3567 }
3568 }
3569 }
3570 else {
3571 for (len = 0; len++ < aint;) {
3572 if (*pat++ & 1)
3573 items |= 128;
3574 if (len & 7)
3575 items >>= 1;
3576 else {
3577 *aptr++ = items & 0xff;
3578 items = 0;
3579 }
3580 }
3581 }
3582 if (aint & 7) {
3583 if (datumtype == 'B')
3584 items <<= 7 - (aint & 7);
3585 else
3586 items >>= 7 - (aint & 7);
3587 *aptr++ = items & 0xff;
3588 }
3589 pat = SvPVX(cat) + SvCUR(cat);
3590 while (aptr <= pat)
3591 *aptr++ = '\0';
79072805 3592
a0d0e21e
LW
3593 pat = savepat;
3594 items = saveitems;
3595 }
3596 break;
3597 case 'H':
3598 case 'h':
3599 {
3600 char *savepat = pat;
3601 I32 saveitems;
79072805 3602
a0d0e21e
LW
3603 fromstr = NEXTFROM;
3604 saveitems = items;
3605 aptr = SvPV(fromstr, fromlen);
3606 if (pat[-1] == '*')
3607 len = fromlen;
3608 pat = aptr;
3609 aint = SvCUR(cat);
3610 SvCUR(cat) += (len+1)/2;
3611 SvGROW(cat, SvCUR(cat) + 1);
3612 aptr = SvPVX(cat) + aint;
3613 if (len > fromlen)
3614 len = fromlen;
3615 aint = len;
3616 items = 0;
3617 if (datumtype == 'H') {
3618 for (len = 0; len++ < aint;) {
3619 if (isALPHA(*pat))
3620 items |= ((*pat++ & 15) + 9) & 15;
3621 else
3622 items |= *pat++ & 15;
3623 if (len & 1)
3624 items <<= 4;
3625 else {
3626 *aptr++ = items & 0xff;
3627 items = 0;
3628 }
3629 }
3630 }
3631 else {
3632 for (len = 0; len++ < aint;) {
3633 if (isALPHA(*pat))
3634 items |= (((*pat++ & 15) + 9) & 15) << 4;
3635 else
3636 items |= (*pat++ & 15) << 4;
3637 if (len & 1)
3638 items >>= 4;
3639 else {
3640 *aptr++ = items & 0xff;
3641 items = 0;
3642 }
3643 }
3644 }
3645 if (aint & 1)
3646 *aptr++ = items & 0xff;
3647 pat = SvPVX(cat) + SvCUR(cat);
3648 while (aptr <= pat)
3649 *aptr++ = '\0';
79072805 3650
a0d0e21e
LW
3651 pat = savepat;
3652 items = saveitems;
3653 }
3654 break;
3655 case 'C':
3656 case 'c':
3657 while (len-- > 0) {
3658 fromstr = NEXTFROM;
3659 aint = SvIV(fromstr);
3660 achar = aint;
3661 sv_catpvn(cat, &achar, sizeof(char));
3662 }
3663 break;
3664 /* Float and double added by gnb@melba.bby.oz.au 22/11/89 */
3665 case 'f':
3666 case 'F':
3667 while (len-- > 0) {
3668 fromstr = NEXTFROM;
3669 afloat = (float)SvNV(fromstr);
3670 sv_catpvn(cat, (char *)&afloat, sizeof (float));
3671 }
3672 break;
3673 case 'd':
3674 case 'D':
3675 while (len-- > 0) {
3676 fromstr = NEXTFROM;
3677 adouble = (double)SvNV(fromstr);
3678 sv_catpvn(cat, (char *)&adouble, sizeof (double));
3679 }
3680 break;
3681 case 'n':
3682 while (len-- > 0) {
3683 fromstr = NEXTFROM;
3684 ashort = (I16)SvIV(fromstr);
3685#ifdef HAS_HTONS
3686 ashort = htons(ashort);
79072805 3687#endif
96e4d5b1 3688 CAT16(cat, &ashort);
a0d0e21e
LW
3689 }
3690 break;
3691 case 'v':
3692 while (len-- > 0) {
3693 fromstr = NEXTFROM;
3694 ashort = (I16)SvIV(fromstr);
3695#ifdef HAS_HTOVS
3696 ashort = htovs(ashort);
79072805 3697#endif
96e4d5b1 3698 CAT16(cat, &ashort);
a0d0e21e
LW
3699 }
3700 break;
3701 case 'S':
3702 case 's':
3703 while (len-- > 0) {
3704 fromstr = NEXTFROM;
3705 ashort = (I16)SvIV(fromstr);
96e4d5b1 3706 CAT16(cat, &ashort);
a0d0e21e
LW
3707 }
3708 break;
3709 case 'I':
3710 while (len-- > 0) {
3711 fromstr = NEXTFROM;
96e4d5b1 3712 auint = SvUV(fromstr);
a0d0e21e
LW
3713 sv_catpvn(cat, (char*)&auint, sizeof(unsigned int));
3714 }
3715 break;
def98dd4
UP
3716 case 'w':
3717 while (len-- > 0) {
bbce6d69
PP
3718 fromstr = NEXTFROM;
3719 adouble = floor(SvNV(fromstr));
3720
3721 if (adouble < 0)
3722 croak("Cannot compress negative numbers");
3723
46fc3d4c
PP
3724 if (
3725#ifdef BW_BITS
3726 adouble <= BW_MASK
3727#else
ef2d312d
TH
3728#ifdef CXUX_BROKEN_CONSTANT_CONVERT
3729 adouble <= UV_MAX_cxux
3730#else
46fc3d4c
PP
3731 adouble <= UV_MAX
3732#endif
ef2d312d 3733#endif
46fc3d4c
PP
3734 )
3735 {
bbce6d69
PP
3736 char buf[1 + sizeof(UV)];
3737 char *in = buf + sizeof(buf);
3738 UV auv = U_V(adouble);;
3739
3740 do {
3741 *--in = (auv & 0x7f) | 0x80;
3742 auv >>= 7;
3743 } while (auv);
3744 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
3745 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
3746 }
3747 else if (SvPOKp(fromstr)) { /* decimal string arithmetics */
3748 char *from, *result, *in;
3749 SV *norm;
3750 STRLEN len;
3751 bool done;
55497cff 3752
bbce6d69
PP
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");
3757
3758 New('w', result, len, char);
3759 in = result + len;
3760 done = FALSE;
3761 while (!done)
3762 *--in = div128(norm, &done) | 0x80;
3763 result[len - 1] &= 0x7F; /* clear continue bit */
3764 sv_catpvn(cat, in, (result + len) - in);
5f05dabc 3765 Safefree(result);
bbce6d69 3766 SvREFCNT_dec(norm); /* free norm */
def98dd4 3767 }
bbce6d69
PP
3768 else if (SvNOKp(fromstr)) {
3769 char buf[sizeof(double) * 2]; /* 8/7 <= 2 */
3770 char *in = buf + sizeof(buf);
3771
3772 do {
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");
3777 adouble = next;
3778 } while (adouble > 0);
3779 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
3780 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
3781 }
3782 else
3783 croak("Cannot compress non integer");
3784 }
def98dd4 3785 break;
a0d0e21e
LW
3786 case 'i':
3787 while (len-- > 0) {
3788 fromstr = NEXTFROM;
3789 aint = SvIV(fromstr);
3790 sv_catpvn(cat, (char*)&aint, sizeof(int));
3791 }
3792 break;
3793 case 'N':
3794 while (len-- > 0) {
3795 fromstr = NEXTFROM;
96e4d5b1 3796 aulong = SvUV(fromstr);
a0d0e21e
LW
3797#ifdef HAS_HTONL
3798 aulong = htonl(aulong);
79072805 3799#endif
96e4d5b1 3800 CAT32(cat, &aulong);
a0d0e21e
LW
3801 }
3802 break;
3803 case 'V':
3804 while (len-- > 0) {
3805 fromstr = NEXTFROM;
96e4d5b1 3806 aulong = SvUV(fromstr);
a0d0e21e
LW
3807#ifdef HAS_HTOVL
3808 aulong = htovl(aulong);
79072805 3809#endif
96e4d5b1 3810 CAT32(cat, &aulong);
a0d0e21e
LW
3811 }
3812 break;
3813 case 'L':
3814 while (len-- > 0) {
3815 fromstr = NEXTFROM;
96e4d5b1
PP
3816 aulong = SvUV(fromstr);
3817 CAT32(cat, &aulong);
a0d0e21e
LW
3818 }
3819 break;
3820 case 'l':
3821 while (len-- > 0) {
3822 fromstr = NEXTFROM;
3823 along = SvIV(fromstr);
96e4d5b1 3824 CAT32(cat, &along);
a0d0e21e
LW
3825 }
3826 break;
ecfc5424 3827#ifdef HAS_QUAD
a0d0e21e
LW
3828 case 'Q':
3829 while (len-- > 0) {
3830 fromstr = NEXTFROM;
ecfc5424
AD
3831 auquad = (unsigned Quad_t)SvIV(fromstr);
3832 sv_catpvn(cat, (char*)&auquad, sizeof(unsigned Quad_t));
a0d0e21e
LW
3833 }
3834 break;
3835 case 'q':
3836 while (len-- > 0) {
3837 fromstr = NEXTFROM;
ecfc5424
AD
3838 aquad = (Quad_t)SvIV(fromstr);
3839 sv_catpvn(cat, (char*)&aquad, sizeof(Quad_t));
a0d0e21e
LW
3840 }
3841 break;
ecfc5424 3842#endif /* HAS_QUAD */
a0d0e21e
LW
3843 case 'P':
3844 len = 1; /* assume SV is correct length */
3845 /* FALL THROUGH */
3846 case 'p':
3847 while (len-- > 0) {
3848 fromstr = NEXTFROM;
72dbcb4b
JT
3849 if (fromstr == &sv_undef)
3850 aptr = NULL;
3851 else {
3852 if (SvREADONLY(fromstr) && curcop != &compiling) {
3853 fromstr = sv_mortalcopy(fromstr);
3854 }
3855 aptr = SvPV_force(fromstr, na);
3856 }
a0d0e21e
LW
3857 sv_catpvn(cat, (char*)&aptr, sizeof(char*));
3858 }
3859 break;
3860 case 'u':
3861 fromstr = NEXTFROM;
3862 aptr = SvPV(fromstr, fromlen);
3863 SvGROW(cat, fromlen * 4 / 3);
3864 if (len <= 1)
3865 len = 45;
3866 else
3867 len = len / 3 * 3;
3868 while (fromlen > 0) {
3869 I32 todo;
79072805 3870
a0d0e21e
LW
3871 if (fromlen > len)
3872 todo = len;
3873 else
3874 todo = fromlen;
3875 doencodes(cat, aptr, todo);
3876 fromlen -= todo;
3877 aptr += todo;
3878 }
3879 break;
3880 }
3881 }
3882 SvSETMAGIC(cat);
3883 SP = ORIGMARK;
3884 PUSHs(cat);
3885 RETURN;
79072805 3886}
a0d0e21e 3887#undef NEXTFROM
79072805 3888
a0d0e21e 3889PP(pp_split)
79072805 3890{
a0d0e21e
LW
3891 dSP; dTARG;
3892 AV *ary;
3893 register I32 limit = POPi; /* note, negative is forever */
3894 SV *sv = POPs;
3895 STRLEN len;
3896 register char *s = SvPV(sv, len);
3897 char *strend = s + len;
44a8e56a
PP
3898 register PMOP *pm;
3899 register REGEXP *rx;
a0d0e21e
LW
3900 register SV *dstr;
3901 register char *m;
3902 I32 iters = 0;
3903 I32 maxiters = (strend - s) + 10;
3904 I32 i;
3905 char *orig;
3906 I32 origlimit = limit;
3907 I32 realarray = 0;
3908 I32 base;
f12c7020 3909 AV *oldstack = curstack;
54310121 3910 I32 gimme = GIMME_V;
c07a80fd 3911 I32 oldsave = savestack_ix;
79072805 3912
44a8e56a
PP
3913#ifdef DEBUGGING
3914 Copy(&LvTARGOFF(POPs), &pm, 1, PMOP*);
3915#else
3916 pm = (PMOP*)POPs;
3917#endif
a0d0e21e
LW
3918 if (!pm || !s)
3919 DIE("panic: do_split");
44a8e56a 3920 rx = pm->op_pmregexp;
bbce6d69
PP
3921
3922 TAINT_IF((pm->op_pmflags & PMf_LOCALE) &&
3923 (pm->op_pmflags & (PMf_WHITE | PMf_SKIPWHITE)));
3924
a0d0e21e
LW
3925 if (pm->op_pmreplroot)
3926 ary = GvAVn((GV*)pm->op_pmreplroot);
3927 else if (gimme != G_ARRAY)
3928 ary = GvAVn(defgv);
79072805 3929 else
a0d0e21e
LW
3930 ary = Nullav;
3931 if (ary && (gimme != G_ARRAY || (pm->op_pmflags & PMf_ONCE))) {
3932 realarray = 1;
3933 if (!AvREAL(ary)) {
3934 AvREAL_on(ary);
3935 for (i = AvFILL(ary); i >= 0; i--)
3936 AvARRAY(ary)[i] = &sv_undef; /* don't free mere refs */
79072805 3937 }
a0d0e21e
LW
3938 av_extend(ary,0);
3939 av_clear(ary);
3940 /* temporarily switch stacks */
f12c7020 3941 SWITCHSTACK(curstack, ary);
79072805 3942 }
a0d0e21e
LW
3943 base = SP - stack_base;
3944 orig = s;
3945 if (pm->op_pmflags & PMf_SKIPWHITE) {
bbce6d69
PP
3946 if (pm->op_pmflags & PMf_LOCALE) {
3947 while (isSPACE_LC(*s))
3948 s++;
3949 }
3950 else {
3951 while (isSPACE(*s))
3952 s++;
3953 }
a0d0e21e 3954 }
c07a80fd
PP
3955 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
3956 SAVEINT(multiline);
3957 multiline = pm->op_pmflags & PMf_MULTILINE;
3958 }
3959
a0d0e21e
LW
3960 if (!limit)
3961 limit = maxiters + 2;
3962 if (pm->op_pmflags & PMf_WHITE) {
3963 while (--limit) {
bbce6d69
PP
3964 m = s;
3965 while (m < strend &&
3966 !((pm->op_pmflags & PMf_LOCALE)
3967 ? isSPACE_LC(*m) : isSPACE(*m)))
3968 ++m;
a0d0e21e
LW
3969 if (m >= strend)
3970 break;
bbce6d69 3971
a0d0e21e
LW
3972 dstr = NEWSV(30, m-s);
3973 sv_setpvn(dstr, s, m-s);
3974 if (!realarray)
3975 sv_2mortal(dstr);
3976 XPUSHs(dstr);
bbce6d69
PP
3977
3978 s = m + 1;
3979 while (s < strend &&
3980 ((pm->op_pmflags & PMf_LOCALE)
3981 ? isSPACE_LC(*s) : isSPACE(*s)))
3982 ++s;
79072805
LW
3983 }
3984 }
a0d0e21e
LW
3985 else if (strEQ("^", rx->precomp)) {
3986 while (--limit) {
3987 /*SUPPRESS 530*/
3988 for (m = s; m < strend && *m != '\n'; m++) ;
3989 m++;
3990 if (m >= strend)
3991 break;
3992 dstr = NEWSV(30, m-s);
3993 sv_setpvn(dstr, s, m-s);
3994 if (!realarray)
3995 sv_2mortal(dstr);
3996 XPUSHs(dstr);
3997 s = m;
3998 }
3999 }
44a8e56a 4000 else if (pm->op_pmshort && !rx->nparens) {
a0d0e21e
LW
4001 i = SvCUR(pm->op_pmshort);
4002 if (i == 1) {
a0d0e21e 4003 i = *SvPVX(pm->op_pmshort);
a0d0e21e 4004 while (--limit) {
bbce6d69
PP
4005 /*SUPPRESS 530*/
4006 for (m = s; m < strend && *m != i; m++) ;
a0d0e21e
LW
4007 if (m >= strend)
4008 break;
4009 dstr = NEWSV(30, m-s);
4010 sv_setpvn(dstr, s, m-s);
4011 if (!realarray)
4012 sv_2mortal(dstr);
4013 XPUSHs(dstr);
4014 s = m + 1;
4015 }
4016 }
4017 else {
4018#ifndef lint
4019 while (s < strend && --limit &&
4020 (m=fbm_instr((unsigned char*)s, (unsigned char*)strend,
4021 pm->op_pmshort)) )
79072805 4022#endif
a0d0e21e
LW
4023 {
4024 dstr = NEWSV(31, m-s);
4025 sv_setpvn(dstr, s, m-s);
4026 if (!realarray)
4027 sv_2mortal(dstr);
4028 XPUSHs(dstr);
4029 s = m + i;
4030 }
463ee0b2 4031 }
463ee0b2 4032 }
a0d0e21e
LW
4033 else {
4034 maxiters += (strend - s) * rx->nparens;
4035 while (s < strend && --limit &&
bbce6d69
PP
4036 pregexec(rx, s, strend, orig, 1, Nullsv, TRUE))
4037 {
4038 TAINT_IF(rx->exec_tainted);
a0d0e21e
LW
4039 if (rx->subbase
4040 && rx->subbase != orig) {
4041 m = s;
4042 s = orig;
4043 orig = rx->subbase;
4044 s = orig + (m - s);
4045 strend = s + (strend - m);
4046 }
4047 m = rx->startp[0];
4048 dstr = NEWSV(32, m-s);
4049 sv_setpvn(dstr, s, m-s);
4050 if (!realarray)
4051 sv_2mortal(dstr);