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