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