This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
minor improvements to git-deltatool
[perl5.git] / pp.c
... / ...
CommitLineData
1/* pp.c
2 *
3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4 * 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others
5 *
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
8 *
9 */
10
11/*
12 * 'It's a big house this, and very peculiar. Always a bit more
13 * to discover, and no knowing what you'll find round a corner.
14 * And Elves, sir!' --Samwise Gamgee
15 *
16 * [p.225 of _The Lord of the Rings_, II/i: "Many Meetings"]
17 */
18
19/* This file contains general pp ("push/pop") functions that execute the
20 * opcodes that make up a perl program. A typical pp function expects to
21 * find its arguments on the stack, and usually pushes its results onto
22 * the stack, hence the 'pp' terminology. Each OP structure contains
23 * a pointer to the relevant pp_foo() function.
24 */
25
26#include "EXTERN.h"
27#define PERL_IN_PP_C
28#include "perl.h"
29#include "keywords.h"
30
31#include "reentr.h"
32
33/* XXX I can't imagine anyone who doesn't have this actually _needs_
34 it, since pid_t is an integral type.
35 --AD 2/20/1998
36*/
37#ifdef NEED_GETPID_PROTO
38extern Pid_t getpid (void);
39#endif
40
41/*
42 * Some BSDs and Cygwin default to POSIX math instead of IEEE.
43 * This switches them over to IEEE.
44 */
45#if defined(LIBM_LIB_VERSION)
46 _LIB_VERSION_TYPE _LIB_VERSION = _IEEE_;
47#endif
48
49/* variations on pp_null */
50
51PP(pp_stub)
52{
53 dVAR;
54 dSP;
55 if (GIMME_V == G_SCALAR)
56 XPUSHs(&PL_sv_undef);
57 RETURN;
58}
59
60/* Pushy stuff. */
61
62PP(pp_padav)
63{
64 dVAR; dSP; dTARGET;
65 I32 gimme;
66 assert(SvTYPE(TARG) == SVt_PVAV);
67 if (PL_op->op_private & OPpLVAL_INTRO)
68 if (!(PL_op->op_private & OPpPAD_STATE))
69 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
70 EXTEND(SP, 1);
71 if (PL_op->op_flags & OPf_REF) {
72 PUSHs(TARG);
73 RETURN;
74 } else if (LVRET) {
75 if (GIMME == G_SCALAR)
76 Perl_croak(aTHX_ "Can't return array to lvalue scalar context");
77 PUSHs(TARG);
78 RETURN;
79 }
80 gimme = GIMME_V;
81 if (gimme == G_ARRAY) {
82 const I32 maxarg = AvFILL(MUTABLE_AV(TARG)) + 1;
83 EXTEND(SP, maxarg);
84 if (SvMAGICAL(TARG)) {
85 U32 i;
86 for (i=0; i < (U32)maxarg; i++) {
87 SV * const * const svp = av_fetch(MUTABLE_AV(TARG), i, FALSE);
88 SP[i+1] = (svp) ? *svp : &PL_sv_undef;
89 }
90 }
91 else {
92 Copy(AvARRAY((const AV *)TARG), SP+1, maxarg, SV*);
93 }
94 SP += maxarg;
95 }
96 else if (gimme == G_SCALAR) {
97 SV* const sv = sv_newmortal();
98 const I32 maxarg = AvFILL(MUTABLE_AV(TARG)) + 1;
99 sv_setiv(sv, maxarg);
100 PUSHs(sv);
101 }
102 RETURN;
103}
104
105PP(pp_padhv)
106{
107 dVAR; dSP; dTARGET;
108 I32 gimme;
109
110 assert(SvTYPE(TARG) == SVt_PVHV);
111 XPUSHs(TARG);
112 if (PL_op->op_private & OPpLVAL_INTRO)
113 if (!(PL_op->op_private & OPpPAD_STATE))
114 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
115 if (PL_op->op_flags & OPf_REF)
116 RETURN;
117 else if (LVRET) {
118 if (GIMME == G_SCALAR)
119 Perl_croak(aTHX_ "Can't return hash to lvalue scalar context");
120 RETURN;
121 }
122 gimme = GIMME_V;
123 if (gimme == G_ARRAY) {
124 RETURNOP(do_kv());
125 }
126 else if (gimme == G_SCALAR) {
127 SV* const sv = Perl_hv_scalar(aTHX_ MUTABLE_HV(TARG));
128 SETs(sv);
129 }
130 RETURN;
131}
132
133/* Translations. */
134
135static const char S_no_symref_sv[] =
136 "Can't use string (\"%" SVf32 "\"%s) as %s ref while \"strict refs\" in use";
137
138PP(pp_rv2gv)
139{
140 dVAR; dSP; dTOPss;
141
142 SvGETMAGIC(sv);
143 if (SvROK(sv)) {
144 wasref:
145 tryAMAGICunDEREF(to_gv);
146
147 sv = SvRV(sv);
148 if (SvTYPE(sv) == SVt_PVIO) {
149 GV * const gv = MUTABLE_GV(sv_newmortal());
150 gv_init(gv, 0, "", 0, 0);
151 GvIOp(gv) = MUTABLE_IO(sv);
152 SvREFCNT_inc_void_NN(sv);
153 sv = MUTABLE_SV(gv);
154 }
155 else if (!isGV_with_GP(sv))
156 DIE(aTHX_ "Not a GLOB reference");
157 }
158 else {
159 if (!isGV_with_GP(sv)) {
160 if (!SvOK(sv) && sv != &PL_sv_undef) {
161 /* If this is a 'my' scalar and flag is set then vivify
162 * NI-S 1999/05/07
163 */
164 if (SvREADONLY(sv))
165 Perl_croak_no_modify(aTHX);
166 if (PL_op->op_private & OPpDEREF) {
167 GV *gv;
168 if (cUNOP->op_targ) {
169 STRLEN len;
170 SV * const namesv = PAD_SV(cUNOP->op_targ);
171 const char * const name = SvPV(namesv, len);
172 gv = MUTABLE_GV(newSV(0));
173 gv_init(gv, CopSTASH(PL_curcop), name, len, 0);
174 }
175 else {
176 const char * const name = CopSTASHPV(PL_curcop);
177 gv = newGVgen(name);
178 }
179 prepare_SV_for_RV(sv);
180 SvRV_set(sv, MUTABLE_SV(gv));
181 SvROK_on(sv);
182 SvSETMAGIC(sv);
183 goto wasref;
184 }
185 if (PL_op->op_flags & OPf_REF ||
186 PL_op->op_private & HINT_STRICT_REFS)
187 DIE(aTHX_ PL_no_usym, "a symbol");
188 if (ckWARN(WARN_UNINITIALIZED))
189 report_uninit(sv);
190 RETSETUNDEF;
191 }
192 if ((PL_op->op_flags & OPf_SPECIAL) &&
193 !(PL_op->op_flags & OPf_MOD))
194 {
195 SV * const temp = MUTABLE_SV(gv_fetchsv(sv, 0, SVt_PVGV));
196 if (!temp
197 && (!is_gv_magical_sv(sv,0)
198 || !(sv = MUTABLE_SV(gv_fetchsv(sv, GV_ADD,
199 SVt_PVGV))))) {
200 RETSETUNDEF;
201 }
202 sv = temp;
203 }
204 else {
205 if (PL_op->op_private & HINT_STRICT_REFS)
206 DIE(aTHX_ S_no_symref_sv, sv, (SvPOK(sv) && SvCUR(sv)>32 ? "..." : ""), "a symbol");
207 if ((PL_op->op_private & (OPpLVAL_INTRO|OPpDONT_INIT_GV))
208 == OPpDONT_INIT_GV) {
209 /* We are the target of a coderef assignment. Return
210 the scalar unchanged, and let pp_sasssign deal with
211 things. */
212 RETURN;
213 }
214 sv = MUTABLE_SV(gv_fetchsv(sv, GV_ADD, SVt_PVGV));
215 }
216 }
217 }
218 if (PL_op->op_private & OPpLVAL_INTRO)
219 save_gp(MUTABLE_GV(sv), !(PL_op->op_flags & OPf_SPECIAL));
220 SETs(sv);
221 RETURN;
222}
223
224/* Helper function for pp_rv2sv and pp_rv2av */
225GV *
226Perl_softref2xv(pTHX_ SV *const sv, const char *const what,
227 const svtype type, SV ***spp)
228{
229 dVAR;
230 GV *gv;
231
232 PERL_ARGS_ASSERT_SOFTREF2XV;
233
234 if (PL_op->op_private & HINT_STRICT_REFS) {
235 if (SvOK(sv))
236 Perl_die(aTHX_ S_no_symref_sv, sv, (SvPOK(sv) && SvCUR(sv)>32 ? "..." : ""), what);
237 else
238 Perl_die(aTHX_ PL_no_usym, what);
239 }
240 if (!SvOK(sv)) {
241 if (PL_op->op_flags & OPf_REF)
242 Perl_die(aTHX_ PL_no_usym, what);
243 if (ckWARN(WARN_UNINITIALIZED))
244 report_uninit(sv);
245 if (type != SVt_PV && GIMME_V == G_ARRAY) {
246 (*spp)--;
247 return NULL;
248 }
249 **spp = &PL_sv_undef;
250 return NULL;
251 }
252 if ((PL_op->op_flags & OPf_SPECIAL) &&
253 !(PL_op->op_flags & OPf_MOD))
254 {
255 gv = gv_fetchsv(sv, 0, type);
256 if (!gv
257 && (!is_gv_magical_sv(sv,0)
258 || !(gv = gv_fetchsv(sv, GV_ADD, type))))
259 {
260 **spp = &PL_sv_undef;
261 return NULL;
262 }
263 }
264 else {
265 gv = gv_fetchsv(sv, GV_ADD, type);
266 }
267 return gv;
268}
269
270PP(pp_rv2sv)
271{
272 dVAR; dSP; dTOPss;
273 GV *gv = NULL;
274
275 if (!(PL_op->op_private & OPpDEREFed))
276 SvGETMAGIC(sv);
277 if (SvROK(sv)) {
278 tryAMAGICunDEREF(to_sv);
279
280 sv = SvRV(sv);
281 switch (SvTYPE(sv)) {
282 case SVt_PVAV:
283 case SVt_PVHV:
284 case SVt_PVCV:
285 case SVt_PVFM:
286 case SVt_PVIO:
287 DIE(aTHX_ "Not a SCALAR reference");
288 default: NOOP;
289 }
290 }
291 else {
292 gv = MUTABLE_GV(sv);
293
294 if (!isGV_with_GP(gv)) {
295 gv = Perl_softref2xv(aTHX_ sv, "a SCALAR", SVt_PV, &sp);
296 if (!gv)
297 RETURN;
298 }
299 sv = GvSVn(gv);
300 }
301 if (PL_op->op_flags & OPf_MOD) {
302 if (PL_op->op_private & OPpLVAL_INTRO) {
303 if (cUNOP->op_first->op_type == OP_NULL)
304 sv = save_scalar(MUTABLE_GV(TOPs));
305 else if (gv)
306 sv = save_scalar(gv);
307 else
308 Perl_croak(aTHX_ "%s", PL_no_localize_ref);
309 }
310 else if (PL_op->op_private & OPpDEREF)
311 vivify_ref(sv, PL_op->op_private & OPpDEREF);
312 }
313 SETs(sv);
314 RETURN;
315}
316
317PP(pp_av2arylen)
318{
319 dVAR; dSP;
320 AV * const av = MUTABLE_AV(TOPs);
321 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
322 if (lvalue) {
323 SV ** const sv = Perl_av_arylen_p(aTHX_ MUTABLE_AV(av));
324 if (!*sv) {
325 *sv = newSV_type(SVt_PVMG);
326 sv_magic(*sv, MUTABLE_SV(av), PERL_MAGIC_arylen, NULL, 0);
327 }
328 SETs(*sv);
329 } else {
330 SETs(sv_2mortal(newSViv(
331 AvFILL(MUTABLE_AV(av)) + CopARYBASE_get(PL_curcop)
332 )));
333 }
334 RETURN;
335}
336
337PP(pp_pos)
338{
339 dVAR; dSP; dTARGET; dPOPss;
340
341 if (PL_op->op_flags & OPf_MOD || LVRET) {
342 if (SvTYPE(TARG) < SVt_PVLV) {
343 sv_upgrade(TARG, SVt_PVLV);
344 sv_magic(TARG, NULL, PERL_MAGIC_pos, NULL, 0);
345 }
346
347 LvTYPE(TARG) = '.';
348 if (LvTARG(TARG) != sv) {
349 SvREFCNT_dec(LvTARG(TARG));
350 LvTARG(TARG) = SvREFCNT_inc_simple(sv);
351 }
352 PUSHs(TARG); /* no SvSETMAGIC */
353 RETURN;
354 }
355 else {
356 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
357 const MAGIC * const mg = mg_find(sv, PERL_MAGIC_regex_global);
358 if (mg && mg->mg_len >= 0) {
359 I32 i = mg->mg_len;
360 if (DO_UTF8(sv))
361 sv_pos_b2u(sv, &i);
362 PUSHi(i + CopARYBASE_get(PL_curcop));
363 RETURN;
364 }
365 }
366 RETPUSHUNDEF;
367 }
368}
369
370PP(pp_rv2cv)
371{
372 dVAR; dSP;
373 GV *gv;
374 HV *stash_unused;
375 const I32 flags = (PL_op->op_flags & OPf_SPECIAL)
376 ? 0
377 : ((PL_op->op_private & (OPpLVAL_INTRO|OPpMAY_RETURN_CONSTANT)) == OPpMAY_RETURN_CONSTANT)
378 ? GV_ADD|GV_NOEXPAND
379 : GV_ADD;
380 /* We usually try to add a non-existent subroutine in case of AUTOLOAD. */
381 /* (But not in defined().) */
382
383 CV *cv = sv_2cv(TOPs, &stash_unused, &gv, flags);
384 if (cv) {
385 if (CvCLONE(cv))
386 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
387 if ((PL_op->op_private & OPpLVAL_INTRO)) {
388 if (gv && GvCV(gv) == cv && (gv = gv_autoload4(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv), FALSE)))
389 cv = GvCV(gv);
390 if (!CvLVALUE(cv))
391 DIE(aTHX_ "Can't modify non-lvalue subroutine call");
392 }
393 }
394 else if ((flags == (GV_ADD|GV_NOEXPAND)) && gv && SvROK(gv)) {
395 cv = MUTABLE_CV(gv);
396 }
397 else
398 cv = MUTABLE_CV(&PL_sv_undef);
399 SETs(MUTABLE_SV(cv));
400 RETURN;
401}
402
403PP(pp_prototype)
404{
405 dVAR; dSP;
406 CV *cv;
407 HV *stash;
408 GV *gv;
409 SV *ret = &PL_sv_undef;
410
411 if (SvPOK(TOPs) && SvCUR(TOPs) >= 7) {
412 const char * s = SvPVX_const(TOPs);
413 if (strnEQ(s, "CORE::", 6)) {
414 const int code = keyword(s + 6, SvCUR(TOPs) - 6, 1);
415 if (code < 0) { /* Overridable. */
416#define MAX_ARGS_OP ((sizeof(I32) - 1) * 2)
417 int i = 0, n = 0, seen_question = 0, defgv = 0;
418 I32 oa;
419 char str[ MAX_ARGS_OP * 2 + 2 ]; /* One ';', one '\0' */
420
421 if (code == -KEY_chop || code == -KEY_chomp
422 || code == -KEY_exec || code == -KEY_system)
423 goto set;
424 if (code == -KEY_mkdir) {
425 ret = newSVpvs_flags("_;$", SVs_TEMP);
426 goto set;
427 }
428 if (code == -KEY_keys || code == -KEY_values || code == -KEY_each) {
429 ret = newSVpvs_flags("\\[@%]", SVs_TEMP);
430 goto set;
431 }
432 if (code == -KEY_tied || code == -KEY_untie) {
433 ret = newSVpvs_flags("\\[$@%*]", SVs_TEMP);
434 goto set;
435 }
436 if (code == -KEY_tie) {
437 ret = newSVpvs_flags("\\[$@%*]$@", SVs_TEMP);
438 goto set;
439 }
440 if (code == -KEY_readpipe) {
441 s = "CORE::backtick";
442 }
443 while (i < MAXO) { /* The slow way. */
444 if (strEQ(s + 6, PL_op_name[i])
445 || strEQ(s + 6, PL_op_desc[i]))
446 {
447 goto found;
448 }
449 i++;
450 }
451 goto nonesuch; /* Should not happen... */
452 found:
453 defgv = PL_opargs[i] & OA_DEFGV;
454 oa = PL_opargs[i] >> OASHIFT;
455 while (oa) {
456 if (oa & OA_OPTIONAL && !seen_question && !defgv) {
457 seen_question = 1;
458 str[n++] = ';';
459 }
460 if ((oa & (OA_OPTIONAL - 1)) >= OA_AVREF
461 && (oa & (OA_OPTIONAL - 1)) <= OA_SCALARREF
462 /* But globs are already references (kinda) */
463 && (oa & (OA_OPTIONAL - 1)) != OA_FILEREF
464 ) {
465 str[n++] = '\\';
466 }
467 str[n++] = ("?$@@%&*$")[oa & (OA_OPTIONAL - 1)];
468 oa = oa >> 4;
469 }
470 if (defgv && str[n - 1] == '$')
471 str[n - 1] = '_';
472 str[n++] = '\0';
473 ret = newSVpvn_flags(str, n - 1, SVs_TEMP);
474 }
475 else if (code) /* Non-Overridable */
476 goto set;
477 else { /* None such */
478 nonesuch:
479 DIE(aTHX_ "Can't find an opnumber for \"%s\"", s+6);
480 }
481 }
482 }
483 cv = sv_2cv(TOPs, &stash, &gv, 0);
484 if (cv && SvPOK(cv))
485 ret = newSVpvn_flags(SvPVX_const(cv), SvCUR(cv), SVs_TEMP);
486 set:
487 SETs(ret);
488 RETURN;
489}
490
491PP(pp_anoncode)
492{
493 dVAR; dSP;
494 CV *cv = MUTABLE_CV(PAD_SV(PL_op->op_targ));
495 if (CvCLONE(cv))
496 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
497 EXTEND(SP,1);
498 PUSHs(MUTABLE_SV(cv));
499 RETURN;
500}
501
502PP(pp_srefgen)
503{
504 dVAR; dSP;
505 *SP = refto(*SP);
506 RETURN;
507}
508
509PP(pp_refgen)
510{
511 dVAR; dSP; dMARK;
512 if (GIMME != G_ARRAY) {
513 if (++MARK <= SP)
514 *MARK = *SP;
515 else
516 *MARK = &PL_sv_undef;
517 *MARK = refto(*MARK);
518 SP = MARK;
519 RETURN;
520 }
521 EXTEND_MORTAL(SP - MARK);
522 while (++MARK <= SP)
523 *MARK = refto(*MARK);
524 RETURN;
525}
526
527STATIC SV*
528S_refto(pTHX_ SV *sv)
529{
530 dVAR;
531 SV* rv;
532
533 PERL_ARGS_ASSERT_REFTO;
534
535 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
536 if (LvTARGLEN(sv))
537 vivify_defelem(sv);
538 if (!(sv = LvTARG(sv)))
539 sv = &PL_sv_undef;
540 else
541 SvREFCNT_inc_void_NN(sv);
542 }
543 else if (SvTYPE(sv) == SVt_PVAV) {
544 if (!AvREAL((const AV *)sv) && AvREIFY((const AV *)sv))
545 av_reify(MUTABLE_AV(sv));
546 SvTEMP_off(sv);
547 SvREFCNT_inc_void_NN(sv);
548 }
549 else if (SvPADTMP(sv) && !IS_PADGV(sv))
550 sv = newSVsv(sv);
551 else {
552 SvTEMP_off(sv);
553 SvREFCNT_inc_void_NN(sv);
554 }
555 rv = sv_newmortal();
556 sv_upgrade(rv, SVt_IV);
557 SvRV_set(rv, sv);
558 SvROK_on(rv);
559 return rv;
560}
561
562PP(pp_ref)
563{
564 dVAR; dSP; dTARGET;
565 const char *pv;
566 SV * const sv = POPs;
567
568 if (sv)
569 SvGETMAGIC(sv);
570
571 if (!sv || !SvROK(sv))
572 RETPUSHNO;
573
574 pv = sv_reftype(SvRV(sv),TRUE);
575 PUSHp(pv, strlen(pv));
576 RETURN;
577}
578
579PP(pp_bless)
580{
581 dVAR; dSP;
582 HV *stash;
583
584 if (MAXARG == 1)
585 stash = CopSTASH(PL_curcop);
586 else {
587 SV * const ssv = POPs;
588 STRLEN len;
589 const char *ptr;
590
591 if (ssv && !SvGMAGICAL(ssv) && !SvAMAGIC(ssv) && SvROK(ssv))
592 Perl_croak(aTHX_ "Attempt to bless into a reference");
593 ptr = SvPV_const(ssv,len);
594 if (len == 0)
595 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
596 "Explicit blessing to '' (assuming package main)");
597 stash = gv_stashpvn(ptr, len, GV_ADD);
598 }
599
600 (void)sv_bless(TOPs, stash);
601 RETURN;
602}
603
604PP(pp_gelem)
605{
606 dVAR; dSP;
607
608 SV *sv = POPs;
609 const char * const elem = SvPV_nolen_const(sv);
610 GV * const gv = MUTABLE_GV(POPs);
611 SV * tmpRef = NULL;
612
613 sv = NULL;
614 if (elem) {
615 /* elem will always be NUL terminated. */
616 const char * const second_letter = elem + 1;
617 switch (*elem) {
618 case 'A':
619 if (strEQ(second_letter, "RRAY"))
620 tmpRef = MUTABLE_SV(GvAV(gv));
621 break;
622 case 'C':
623 if (strEQ(second_letter, "ODE"))
624 tmpRef = MUTABLE_SV(GvCVu(gv));
625 break;
626 case 'F':
627 if (strEQ(second_letter, "ILEHANDLE")) {
628 /* finally deprecated in 5.8.0 */
629 deprecate("*glob{FILEHANDLE}");
630 tmpRef = MUTABLE_SV(GvIOp(gv));
631 }
632 else
633 if (strEQ(second_letter, "ORMAT"))
634 tmpRef = MUTABLE_SV(GvFORM(gv));
635 break;
636 case 'G':
637 if (strEQ(second_letter, "LOB"))
638 tmpRef = MUTABLE_SV(gv);
639 break;
640 case 'H':
641 if (strEQ(second_letter, "ASH"))
642 tmpRef = MUTABLE_SV(GvHV(gv));
643 break;
644 case 'I':
645 if (*second_letter == 'O' && !elem[2])
646 tmpRef = MUTABLE_SV(GvIOp(gv));
647 break;
648 case 'N':
649 if (strEQ(second_letter, "AME"))
650 sv = newSVhek(GvNAME_HEK(gv));
651 break;
652 case 'P':
653 if (strEQ(second_letter, "ACKAGE")) {
654 const HV * const stash = GvSTASH(gv);
655 const HEK * const hek = stash ? HvNAME_HEK(stash) : NULL;
656 sv = hek ? newSVhek(hek) : newSVpvs("__ANON__");
657 }
658 break;
659 case 'S':
660 if (strEQ(second_letter, "CALAR"))
661 tmpRef = GvSVn(gv);
662 break;
663 }
664 }
665 if (tmpRef)
666 sv = newRV(tmpRef);
667 if (sv)
668 sv_2mortal(sv);
669 else
670 sv = &PL_sv_undef;
671 XPUSHs(sv);
672 RETURN;
673}
674
675/* Pattern matching */
676
677PP(pp_study)
678{
679 dVAR; dSP; dPOPss;
680 register unsigned char *s;
681 register I32 pos;
682 register I32 ch;
683 register I32 *sfirst;
684 register I32 *snext;
685 STRLEN len;
686
687 if (sv == PL_lastscream) {
688 if (SvSCREAM(sv))
689 RETPUSHYES;
690 }
691 s = (unsigned char*)(SvPV(sv, len));
692 pos = len;
693 if (pos <= 0 || !SvPOK(sv) || SvUTF8(sv)) {
694 /* No point in studying a zero length string, and not safe to study
695 anything that doesn't appear to be a simple scalar (and hence might
696 change between now and when the regexp engine runs without our set
697 magic ever running) such as a reference to an object with overloaded
698 stringification. */
699 RETPUSHNO;
700 }
701
702 if (PL_lastscream) {
703 SvSCREAM_off(PL_lastscream);
704 SvREFCNT_dec(PL_lastscream);
705 }
706 PL_lastscream = SvREFCNT_inc_simple(sv);
707
708 s = (unsigned char*)(SvPV(sv, len));
709 pos = len;
710 if (pos <= 0)
711 RETPUSHNO;
712 if (pos > PL_maxscream) {
713 if (PL_maxscream < 0) {
714 PL_maxscream = pos + 80;
715 Newx(PL_screamfirst, 256, I32);
716 Newx(PL_screamnext, PL_maxscream, I32);
717 }
718 else {
719 PL_maxscream = pos + pos / 4;
720 Renew(PL_screamnext, PL_maxscream, I32);
721 }
722 }
723
724 sfirst = PL_screamfirst;
725 snext = PL_screamnext;
726
727 if (!sfirst || !snext)
728 DIE(aTHX_ "do_study: out of memory");
729
730 for (ch = 256; ch; --ch)
731 *sfirst++ = -1;
732 sfirst -= 256;
733
734 while (--pos >= 0) {
735 register const I32 ch = s[pos];
736 if (sfirst[ch] >= 0)
737 snext[pos] = sfirst[ch] - pos;
738 else
739 snext[pos] = -pos;
740 sfirst[ch] = pos;
741 }
742
743 SvSCREAM_on(sv);
744 /* piggyback on m//g magic */
745 sv_magic(sv, NULL, PERL_MAGIC_regex_global, NULL, 0);
746 RETPUSHYES;
747}
748
749PP(pp_trans)
750{
751 dVAR; dSP; dTARG;
752 SV *sv;
753
754 if (PL_op->op_flags & OPf_STACKED)
755 sv = POPs;
756 else if (PL_op->op_private & OPpTARGET_MY)
757 sv = GETTARGET;
758 else {
759 sv = DEFSV;
760 EXTEND(SP,1);
761 }
762 TARG = sv_newmortal();
763 PUSHi(do_trans(sv));
764 RETURN;
765}
766
767/* Lvalue operators. */
768
769PP(pp_schop)
770{
771 dVAR; dSP; dTARGET;
772 do_chop(TARG, TOPs);
773 SETTARG;
774 RETURN;
775}
776
777PP(pp_chop)
778{
779 dVAR; dSP; dMARK; dTARGET; dORIGMARK;
780 while (MARK < SP)
781 do_chop(TARG, *++MARK);
782 SP = ORIGMARK;
783 XPUSHTARG;
784 RETURN;
785}
786
787PP(pp_schomp)
788{
789 dVAR; dSP; dTARGET;
790 SETi(do_chomp(TOPs));
791 RETURN;
792}
793
794PP(pp_chomp)
795{
796 dVAR; dSP; dMARK; dTARGET;
797 register I32 count = 0;
798
799 while (SP > MARK)
800 count += do_chomp(POPs);
801 XPUSHi(count);
802 RETURN;
803}
804
805PP(pp_undef)
806{
807 dVAR; dSP;
808 SV *sv;
809
810 if (!PL_op->op_private) {
811 EXTEND(SP, 1);
812 RETPUSHUNDEF;
813 }
814
815 sv = POPs;
816 if (!sv)
817 RETPUSHUNDEF;
818
819 SV_CHECK_THINKFIRST_COW_DROP(sv);
820
821 switch (SvTYPE(sv)) {
822 case SVt_NULL:
823 break;
824 case SVt_PVAV:
825 av_undef(MUTABLE_AV(sv));
826 break;
827 case SVt_PVHV:
828 hv_undef(MUTABLE_HV(sv));
829 break;
830 case SVt_PVCV:
831 if (cv_const_sv((const CV *)sv))
832 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Constant subroutine %s undefined",
833 CvANON((const CV *)sv) ? "(anonymous)"
834 : GvENAME(CvGV((const CV *)sv)));
835 /* FALLTHROUGH */
836 case SVt_PVFM:
837 {
838 /* let user-undef'd sub keep its identity */
839 GV* const gv = CvGV((const CV *)sv);
840 cv_undef(MUTABLE_CV(sv));
841 CvGV((const CV *)sv) = gv;
842 }
843 break;
844 case SVt_PVGV:
845 if (SvFAKE(sv)) {
846 SvSetMagicSV(sv, &PL_sv_undef);
847 break;
848 }
849 else if (isGV_with_GP(sv)) {
850 GP *gp;
851 HV *stash;
852
853 /* undef *Foo:: */
854 if((stash = GvHV((const GV *)sv)) && HvNAME_get(stash))
855 mro_isa_changed_in(stash);
856 /* undef *Pkg::meth_name ... */
857 else if(GvCVu((const GV *)sv) && (stash = GvSTASH((const GV *)sv))
858 && HvNAME_get(stash))
859 mro_method_changed_in(stash);
860
861 gp_free(MUTABLE_GV(sv));
862 Newxz(gp, 1, GP);
863 GvGP(sv) = gp_ref(gp);
864 GvSV(sv) = newSV(0);
865 GvLINE(sv) = CopLINE(PL_curcop);
866 GvEGV(sv) = MUTABLE_GV(sv);
867 GvMULTI_on(sv);
868 break;
869 }
870 /* FALL THROUGH */
871 default:
872 if (SvTYPE(sv) >= SVt_PV && SvPVX_const(sv) && SvLEN(sv)) {
873 SvPV_free(sv);
874 SvPV_set(sv, NULL);
875 SvLEN_set(sv, 0);
876 }
877 SvOK_off(sv);
878 SvSETMAGIC(sv);
879 }
880
881 RETPUSHUNDEF;
882}
883
884PP(pp_predec)
885{
886 dVAR; dSP;
887 if (SvTYPE(TOPs) >= SVt_PVAV || isGV_with_GP(TOPs))
888 Perl_croak_no_modify(aTHX);
889 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
890 && SvIVX(TOPs) != IV_MIN)
891 {
892 SvIV_set(TOPs, SvIVX(TOPs) - 1);
893 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
894 }
895 else
896 sv_dec(TOPs);
897 SvSETMAGIC(TOPs);
898 return NORMAL;
899}
900
901PP(pp_postinc)
902{
903 dVAR; dSP; dTARGET;
904 if (SvTYPE(TOPs) >= SVt_PVAV || isGV_with_GP(TOPs))
905 Perl_croak_no_modify(aTHX);
906 sv_setsv(TARG, TOPs);
907 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
908 && SvIVX(TOPs) != IV_MAX)
909 {
910 SvIV_set(TOPs, SvIVX(TOPs) + 1);
911 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
912 }
913 else
914 sv_inc_nomg(TOPs);
915 SvSETMAGIC(TOPs);
916 /* special case for undef: see thread at 2003-03/msg00536.html in archive */
917 if (!SvOK(TARG))
918 sv_setiv(TARG, 0);
919 SETs(TARG);
920 return NORMAL;
921}
922
923PP(pp_postdec)
924{
925 dVAR; dSP; dTARGET;
926 if (SvTYPE(TOPs) >= SVt_PVAV || isGV_with_GP(TOPs))
927 Perl_croak_no_modify(aTHX);
928 sv_setsv(TARG, TOPs);
929 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
930 && SvIVX(TOPs) != IV_MIN)
931 {
932 SvIV_set(TOPs, SvIVX(TOPs) - 1);
933 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
934 }
935 else
936 sv_dec_nomg(TOPs);
937 SvSETMAGIC(TOPs);
938 SETs(TARG);
939 return NORMAL;
940}
941
942/* Ordinary operators. */
943
944PP(pp_pow)
945{
946 dVAR; dSP; dATARGET; SV *svl, *svr;
947#ifdef PERL_PRESERVE_IVUV
948 bool is_int = 0;
949#endif
950 tryAMAGICbin_MG(pow_amg, AMGf_assign|AMGf_numeric);
951 svr = TOPs;
952 svl = TOPm1s;
953#ifdef PERL_PRESERVE_IVUV
954 /* For integer to integer power, we do the calculation by hand wherever
955 we're sure it is safe; otherwise we call pow() and try to convert to
956 integer afterwards. */
957 {
958 SvIV_please_nomg(svr);
959 if (SvIOK(svr)) {
960 SvIV_please_nomg(svl);
961 if (SvIOK(svl)) {
962 UV power;
963 bool baseuok;
964 UV baseuv;
965
966 if (SvUOK(svr)) {
967 power = SvUVX(svr);
968 } else {
969 const IV iv = SvIVX(svr);
970 if (iv >= 0) {
971 power = iv;
972 } else {
973 goto float_it; /* Can't do negative powers this way. */
974 }
975 }
976
977 baseuok = SvUOK(svl);
978 if (baseuok) {
979 baseuv = SvUVX(svl);
980 } else {
981 const IV iv = SvIVX(svl);
982 if (iv >= 0) {
983 baseuv = iv;
984 baseuok = TRUE; /* effectively it's a UV now */
985 } else {
986 baseuv = -iv; /* abs, baseuok == false records sign */
987 }
988 }
989 /* now we have integer ** positive integer. */
990 is_int = 1;
991
992 /* foo & (foo - 1) is zero only for a power of 2. */
993 if (!(baseuv & (baseuv - 1))) {
994 /* We are raising power-of-2 to a positive integer.
995 The logic here will work for any base (even non-integer
996 bases) but it can be less accurate than
997 pow (base,power) or exp (power * log (base)) when the
998 intermediate values start to spill out of the mantissa.
999 With powers of 2 we know this can't happen.
1000 And powers of 2 are the favourite thing for perl
1001 programmers to notice ** not doing what they mean. */
1002 NV result = 1.0;
1003 NV base = baseuok ? baseuv : -(NV)baseuv;
1004
1005 if (power & 1) {
1006 result *= base;
1007 }
1008 while (power >>= 1) {
1009 base *= base;
1010 if (power & 1) {
1011 result *= base;
1012 }
1013 }
1014 SP--;
1015 SETn( result );
1016 SvIV_please_nomg(svr);
1017 RETURN;
1018 } else {
1019 register unsigned int highbit = 8 * sizeof(UV);
1020 register unsigned int diff = 8 * sizeof(UV);
1021 while (diff >>= 1) {
1022 highbit -= diff;
1023 if (baseuv >> highbit) {
1024 highbit += diff;
1025 }
1026 }
1027 /* we now have baseuv < 2 ** highbit */
1028 if (power * highbit <= 8 * sizeof(UV)) {
1029 /* result will definitely fit in UV, so use UV math
1030 on same algorithm as above */
1031 register UV result = 1;
1032 register UV base = baseuv;
1033 const bool odd_power = cBOOL(power & 1);
1034 if (odd_power) {
1035 result *= base;
1036 }
1037 while (power >>= 1) {
1038 base *= base;
1039 if (power & 1) {
1040 result *= base;
1041 }
1042 }
1043 SP--;
1044 if (baseuok || !odd_power)
1045 /* answer is positive */
1046 SETu( result );
1047 else if (result <= (UV)IV_MAX)
1048 /* answer negative, fits in IV */
1049 SETi( -(IV)result );
1050 else if (result == (UV)IV_MIN)
1051 /* 2's complement assumption: special case IV_MIN */
1052 SETi( IV_MIN );
1053 else
1054 /* answer negative, doesn't fit */
1055 SETn( -(NV)result );
1056 RETURN;
1057 }
1058 }
1059 }
1060 }
1061 }
1062 float_it:
1063#endif
1064 {
1065 NV right = SvNV_nomg(svr);
1066 NV left = SvNV_nomg(svl);
1067 (void)POPs;
1068
1069#if defined(USE_LONG_DOUBLE) && defined(HAS_AIX_POWL_NEG_BASE_BUG)
1070 /*
1071 We are building perl with long double support and are on an AIX OS
1072 afflicted with a powl() function that wrongly returns NaNQ for any
1073 negative base. This was reported to IBM as PMR #23047-379 on
1074 03/06/2006. The problem exists in at least the following versions
1075 of AIX and the libm fileset, and no doubt others as well:
1076
1077 AIX 4.3.3-ML10 bos.adt.libm 4.3.3.50
1078 AIX 5.1.0-ML04 bos.adt.libm 5.1.0.29
1079 AIX 5.2.0 bos.adt.libm 5.2.0.85
1080
1081 So, until IBM fixes powl(), we provide the following workaround to
1082 handle the problem ourselves. Our logic is as follows: for
1083 negative bases (left), we use fmod(right, 2) to check if the
1084 exponent is an odd or even integer:
1085
1086 - if odd, powl(left, right) == -powl(-left, right)
1087 - if even, powl(left, right) == powl(-left, right)
1088
1089 If the exponent is not an integer, the result is rightly NaNQ, so
1090 we just return that (as NV_NAN).
1091 */
1092
1093 if (left < 0.0) {
1094 NV mod2 = Perl_fmod( right, 2.0 );
1095 if (mod2 == 1.0 || mod2 == -1.0) { /* odd integer */
1096 SETn( -Perl_pow( -left, right) );
1097 } else if (mod2 == 0.0) { /* even integer */
1098 SETn( Perl_pow( -left, right) );
1099 } else { /* fractional power */
1100 SETn( NV_NAN );
1101 }
1102 } else {
1103 SETn( Perl_pow( left, right) );
1104 }
1105#else
1106 SETn( Perl_pow( left, right) );
1107#endif /* HAS_AIX_POWL_NEG_BASE_BUG */
1108
1109#ifdef PERL_PRESERVE_IVUV
1110 if (is_int)
1111 SvIV_please_nomg(svr);
1112#endif
1113 RETURN;
1114 }
1115}
1116
1117PP(pp_multiply)
1118{
1119 dVAR; dSP; dATARGET; SV *svl, *svr;
1120 tryAMAGICbin_MG(mult_amg, AMGf_assign|AMGf_numeric);
1121 svr = TOPs;
1122 svl = TOPm1s;
1123#ifdef PERL_PRESERVE_IVUV
1124 SvIV_please_nomg(svr);
1125 if (SvIOK(svr)) {
1126 /* Unless the left argument is integer in range we are going to have to
1127 use NV maths. Hence only attempt to coerce the right argument if
1128 we know the left is integer. */
1129 /* Left operand is defined, so is it IV? */
1130 SvIV_please_nomg(svl);
1131 if (SvIOK(svl)) {
1132 bool auvok = SvUOK(svl);
1133 bool buvok = SvUOK(svr);
1134 const UV topmask = (~ (UV)0) << (4 * sizeof (UV));
1135 const UV botmask = ~((~ (UV)0) << (4 * sizeof (UV)));
1136 UV alow;
1137 UV ahigh;
1138 UV blow;
1139 UV bhigh;
1140
1141 if (auvok) {
1142 alow = SvUVX(svl);
1143 } else {
1144 const IV aiv = SvIVX(svl);
1145 if (aiv >= 0) {
1146 alow = aiv;
1147 auvok = TRUE; /* effectively it's a UV now */
1148 } else {
1149 alow = -aiv; /* abs, auvok == false records sign */
1150 }
1151 }
1152 if (buvok) {
1153 blow = SvUVX(svr);
1154 } else {
1155 const IV biv = SvIVX(svr);
1156 if (biv >= 0) {
1157 blow = biv;
1158 buvok = TRUE; /* effectively it's a UV now */
1159 } else {
1160 blow = -biv; /* abs, buvok == false records sign */
1161 }
1162 }
1163
1164 /* If this does sign extension on unsigned it's time for plan B */
1165 ahigh = alow >> (4 * sizeof (UV));
1166 alow &= botmask;
1167 bhigh = blow >> (4 * sizeof (UV));
1168 blow &= botmask;
1169 if (ahigh && bhigh) {
1170 NOOP;
1171 /* eg 32 bit is at least 0x10000 * 0x10000 == 0x100000000
1172 which is overflow. Drop to NVs below. */
1173 } else if (!ahigh && !bhigh) {
1174 /* eg 32 bit is at most 0xFFFF * 0xFFFF == 0xFFFE0001
1175 so the unsigned multiply cannot overflow. */
1176 const UV product = alow * blow;
1177 if (auvok == buvok) {
1178 /* -ve * -ve or +ve * +ve gives a +ve result. */
1179 SP--;
1180 SETu( product );
1181 RETURN;
1182 } else if (product <= (UV)IV_MIN) {
1183 /* 2s complement assumption that (UV)-IV_MIN is correct. */
1184 /* -ve result, which could overflow an IV */
1185 SP--;
1186 SETi( -(IV)product );
1187 RETURN;
1188 } /* else drop to NVs below. */
1189 } else {
1190 /* One operand is large, 1 small */
1191 UV product_middle;
1192 if (bhigh) {
1193 /* swap the operands */
1194 ahigh = bhigh;
1195 bhigh = blow; /* bhigh now the temp var for the swap */
1196 blow = alow;
1197 alow = bhigh;
1198 }
1199 /* now, ((ahigh * blow) << half_UV_len) + (alow * blow)
1200 multiplies can't overflow. shift can, add can, -ve can. */
1201 product_middle = ahigh * blow;
1202 if (!(product_middle & topmask)) {
1203 /* OK, (ahigh * blow) won't lose bits when we shift it. */
1204 UV product_low;
1205 product_middle <<= (4 * sizeof (UV));
1206 product_low = alow * blow;
1207
1208 /* as for pp_add, UV + something mustn't get smaller.
1209 IIRC ANSI mandates this wrapping *behaviour* for
1210 unsigned whatever the actual representation*/
1211 product_low += product_middle;
1212 if (product_low >= product_middle) {
1213 /* didn't overflow */
1214 if (auvok == buvok) {
1215 /* -ve * -ve or +ve * +ve gives a +ve result. */
1216 SP--;
1217 SETu( product_low );
1218 RETURN;
1219 } else if (product_low <= (UV)IV_MIN) {
1220 /* 2s complement assumption again */
1221 /* -ve result, which could overflow an IV */
1222 SP--;
1223 SETi( -(IV)product_low );
1224 RETURN;
1225 } /* else drop to NVs below. */
1226 }
1227 } /* product_middle too large */
1228 } /* ahigh && bhigh */
1229 } /* SvIOK(svl) */
1230 } /* SvIOK(svr) */
1231#endif
1232 {
1233 NV right = SvNV_nomg(svr);
1234 NV left = SvNV_nomg(svl);
1235 (void)POPs;
1236 SETn( left * right );
1237 RETURN;
1238 }
1239}
1240
1241PP(pp_divide)
1242{
1243 dVAR; dSP; dATARGET; SV *svl, *svr;
1244 tryAMAGICbin_MG(div_amg, AMGf_assign|AMGf_numeric);
1245 svr = TOPs;
1246 svl = TOPm1s;
1247 /* Only try to do UV divide first
1248 if ((SLOPPYDIVIDE is true) or
1249 (PERL_PRESERVE_IVUV is true and one or both SV is a UV too large
1250 to preserve))
1251 The assumption is that it is better to use floating point divide
1252 whenever possible, only doing integer divide first if we can't be sure.
1253 If NV_PRESERVES_UV is true then we know at compile time that no UV
1254 can be too large to preserve, so don't need to compile the code to
1255 test the size of UVs. */
1256
1257#ifdef SLOPPYDIVIDE
1258# define PERL_TRY_UV_DIVIDE
1259 /* ensure that 20./5. == 4. */
1260#else
1261# ifdef PERL_PRESERVE_IVUV
1262# ifndef NV_PRESERVES_UV
1263# define PERL_TRY_UV_DIVIDE
1264# endif
1265# endif
1266#endif
1267
1268#ifdef PERL_TRY_UV_DIVIDE
1269 SvIV_please_nomg(svr);
1270 if (SvIOK(svr)) {
1271 SvIV_please_nomg(svl);
1272 if (SvIOK(svl)) {
1273 bool left_non_neg = SvUOK(svl);
1274 bool right_non_neg = SvUOK(svr);
1275 UV left;
1276 UV right;
1277
1278 if (right_non_neg) {
1279 right = SvUVX(svr);
1280 }
1281 else {
1282 const IV biv = SvIVX(svr);
1283 if (biv >= 0) {
1284 right = biv;
1285 right_non_neg = TRUE; /* effectively it's a UV now */
1286 }
1287 else {
1288 right = -biv;
1289 }
1290 }
1291 /* historically undef()/0 gives a "Use of uninitialized value"
1292 warning before dieing, hence this test goes here.
1293 If it were immediately before the second SvIV_please, then
1294 DIE() would be invoked before left was even inspected, so
1295 no inpsection would give no warning. */
1296 if (right == 0)
1297 DIE(aTHX_ "Illegal division by zero");
1298
1299 if (left_non_neg) {
1300 left = SvUVX(svl);
1301 }
1302 else {
1303 const IV aiv = SvIVX(svl);
1304 if (aiv >= 0) {
1305 left = aiv;
1306 left_non_neg = TRUE; /* effectively it's a UV now */
1307 }
1308 else {
1309 left = -aiv;
1310 }
1311 }
1312
1313 if (left >= right
1314#ifdef SLOPPYDIVIDE
1315 /* For sloppy divide we always attempt integer division. */
1316#else
1317 /* Otherwise we only attempt it if either or both operands
1318 would not be preserved by an NV. If both fit in NVs
1319 we fall through to the NV divide code below. However,
1320 as left >= right to ensure integer result here, we know that
1321 we can skip the test on the right operand - right big
1322 enough not to be preserved can't get here unless left is
1323 also too big. */
1324
1325 && (left > ((UV)1 << NV_PRESERVES_UV_BITS))
1326#endif
1327 ) {
1328 /* Integer division can't overflow, but it can be imprecise. */
1329 const UV result = left / right;
1330 if (result * right == left) {
1331 SP--; /* result is valid */
1332 if (left_non_neg == right_non_neg) {
1333 /* signs identical, result is positive. */
1334 SETu( result );
1335 RETURN;
1336 }
1337 /* 2s complement assumption */
1338 if (result <= (UV)IV_MIN)
1339 SETi( -(IV)result );
1340 else {
1341 /* It's exact but too negative for IV. */
1342 SETn( -(NV)result );
1343 }
1344 RETURN;
1345 } /* tried integer divide but it was not an integer result */
1346 } /* else (PERL_ABS(result) < 1.0) or (both UVs in range for NV) */
1347 } /* left wasn't SvIOK */
1348 } /* right wasn't SvIOK */
1349#endif /* PERL_TRY_UV_DIVIDE */
1350 {
1351 NV right = SvNV_nomg(svr);
1352 NV left = SvNV_nomg(svl);
1353 (void)POPs;(void)POPs;
1354#if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
1355 if (! Perl_isnan(right) && right == 0.0)
1356#else
1357 if (right == 0.0)
1358#endif
1359 DIE(aTHX_ "Illegal division by zero");
1360 PUSHn( left / right );
1361 RETURN;
1362 }
1363}
1364
1365PP(pp_modulo)
1366{
1367 dVAR; dSP; dATARGET;
1368 tryAMAGICbin_MG(modulo_amg, AMGf_assign|AMGf_numeric);
1369 {
1370 UV left = 0;
1371 UV right = 0;
1372 bool left_neg = FALSE;
1373 bool right_neg = FALSE;
1374 bool use_double = FALSE;
1375 bool dright_valid = FALSE;
1376 NV dright = 0.0;
1377 NV dleft = 0.0;
1378 SV * const svr = TOPs;
1379 SV * const svl = TOPm1s;
1380 SvIV_please_nomg(svr);
1381 if (SvIOK(svr)) {
1382 right_neg = !SvUOK(svr);
1383 if (!right_neg) {
1384 right = SvUVX(svr);
1385 } else {
1386 const IV biv = SvIVX(svr);
1387 if (biv >= 0) {
1388 right = biv;
1389 right_neg = FALSE; /* effectively it's a UV now */
1390 } else {
1391 right = -biv;
1392 }
1393 }
1394 }
1395 else {
1396 dright = SvNV_nomg(svr);
1397 right_neg = dright < 0;
1398 if (right_neg)
1399 dright = -dright;
1400 if (dright < UV_MAX_P1) {
1401 right = U_V(dright);
1402 dright_valid = TRUE; /* In case we need to use double below. */
1403 } else {
1404 use_double = TRUE;
1405 }
1406 }
1407
1408 /* At this point use_double is only true if right is out of range for
1409 a UV. In range NV has been rounded down to nearest UV and
1410 use_double false. */
1411 SvIV_please_nomg(svl);
1412 if (!use_double && SvIOK(svl)) {
1413 if (SvIOK(svl)) {
1414 left_neg = !SvUOK(svl);
1415 if (!left_neg) {
1416 left = SvUVX(svl);
1417 } else {
1418 const IV aiv = SvIVX(svl);
1419 if (aiv >= 0) {
1420 left = aiv;
1421 left_neg = FALSE; /* effectively it's a UV now */
1422 } else {
1423 left = -aiv;
1424 }
1425 }
1426 }
1427 }
1428 else {
1429 dleft = SvNV_nomg(svl);
1430 left_neg = dleft < 0;
1431 if (left_neg)
1432 dleft = -dleft;
1433
1434 /* This should be exactly the 5.6 behaviour - if left and right are
1435 both in range for UV then use U_V() rather than floor. */
1436 if (!use_double) {
1437 if (dleft < UV_MAX_P1) {
1438 /* right was in range, so is dleft, so use UVs not double.
1439 */
1440 left = U_V(dleft);
1441 }
1442 /* left is out of range for UV, right was in range, so promote
1443 right (back) to double. */
1444 else {
1445 /* The +0.5 is used in 5.6 even though it is not strictly
1446 consistent with the implicit +0 floor in the U_V()
1447 inside the #if 1. */
1448 dleft = Perl_floor(dleft + 0.5);
1449 use_double = TRUE;
1450 if (dright_valid)
1451 dright = Perl_floor(dright + 0.5);
1452 else
1453 dright = right;
1454 }
1455 }
1456 }
1457 sp -= 2;
1458 if (use_double) {
1459 NV dans;
1460
1461 if (!dright)
1462 DIE(aTHX_ "Illegal modulus zero");
1463
1464 dans = Perl_fmod(dleft, dright);
1465 if ((left_neg != right_neg) && dans)
1466 dans = dright - dans;
1467 if (right_neg)
1468 dans = -dans;
1469 sv_setnv(TARG, dans);
1470 }
1471 else {
1472 UV ans;
1473
1474 if (!right)
1475 DIE(aTHX_ "Illegal modulus zero");
1476
1477 ans = left % right;
1478 if ((left_neg != right_neg) && ans)
1479 ans = right - ans;
1480 if (right_neg) {
1481 /* XXX may warn: unary minus operator applied to unsigned type */
1482 /* could change -foo to be (~foo)+1 instead */
1483 if (ans <= ~((UV)IV_MAX)+1)
1484 sv_setiv(TARG, ~ans+1);
1485 else
1486 sv_setnv(TARG, -(NV)ans);
1487 }
1488 else
1489 sv_setuv(TARG, ans);
1490 }
1491 PUSHTARG;
1492 RETURN;
1493 }
1494}
1495
1496PP(pp_repeat)
1497{
1498 dVAR; dSP; dATARGET;
1499 register IV count;
1500 SV *sv;
1501
1502 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1503 /* TODO: think of some way of doing list-repeat overloading ??? */
1504 sv = POPs;
1505 SvGETMAGIC(sv);
1506 }
1507 else {
1508 tryAMAGICbin_MG(repeat_amg, AMGf_assign);
1509 sv = POPs;
1510 }
1511
1512 if (SvIOKp(sv)) {
1513 if (SvUOK(sv)) {
1514 const UV uv = SvUV_nomg(sv);
1515 if (uv > IV_MAX)
1516 count = IV_MAX; /* The best we can do? */
1517 else
1518 count = uv;
1519 } else {
1520 const IV iv = SvIV_nomg(sv);
1521 if (iv < 0)
1522 count = 0;
1523 else
1524 count = iv;
1525 }
1526 }
1527 else if (SvNOKp(sv)) {
1528 const NV nv = SvNV_nomg(sv);
1529 if (nv < 0.0)
1530 count = 0;
1531 else
1532 count = (IV)nv;
1533 }
1534 else
1535 count = SvIV_nomg(sv);
1536
1537 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1538 dMARK;
1539 static const char oom_list_extend[] = "Out of memory during list extend";
1540 const I32 items = SP - MARK;
1541 const I32 max = items * count;
1542
1543 MEM_WRAP_CHECK_1(max, SV*, oom_list_extend);
1544 /* Did the max computation overflow? */
1545 if (items > 0 && max > 0 && (max < items || max < count))
1546 Perl_croak(aTHX_ oom_list_extend);
1547 MEXTEND(MARK, max);
1548 if (count > 1) {
1549 while (SP > MARK) {
1550#if 0
1551 /* This code was intended to fix 20010809.028:
1552
1553 $x = 'abcd';
1554 for (($x =~ /./g) x 2) {
1555 print chop; # "abcdabcd" expected as output.
1556 }
1557
1558 * but that change (#11635) broke this code:
1559
1560 $x = [("foo")x2]; # only one "foo" ended up in the anonlist.
1561
1562 * I can't think of a better fix that doesn't introduce
1563 * an efficiency hit by copying the SVs. The stack isn't
1564 * refcounted, and mortalisation obviously doesn't
1565 * Do The Right Thing when the stack has more than
1566 * one pointer to the same mortal value.
1567 * .robin.
1568 */
1569 if (*SP) {
1570 *SP = sv_2mortal(newSVsv(*SP));
1571 SvREADONLY_on(*SP);
1572 }
1573#else
1574 if (*SP)
1575 SvTEMP_off((*SP));
1576#endif
1577 SP--;
1578 }
1579 MARK++;
1580 repeatcpy((char*)(MARK + items), (char*)MARK,
1581 items * sizeof(const SV *), count - 1);
1582 SP += max;
1583 }
1584 else if (count <= 0)
1585 SP -= items;
1586 }
1587 else { /* Note: mark already snarfed by pp_list */
1588 SV * const tmpstr = POPs;
1589 STRLEN len;
1590 bool isutf;
1591 static const char oom_string_extend[] =
1592 "Out of memory during string extend";
1593
1594 if (TARG != tmpstr)
1595 sv_setsv_nomg(TARG, tmpstr);
1596 SvPV_force_nomg(TARG, len);
1597 isutf = DO_UTF8(TARG);
1598 if (count != 1) {
1599 if (count < 1)
1600 SvCUR_set(TARG, 0);
1601 else {
1602 const STRLEN max = (UV)count * len;
1603 if (len > MEM_SIZE_MAX / count)
1604 Perl_croak(aTHX_ oom_string_extend);
1605 MEM_WRAP_CHECK_1(max, char, oom_string_extend);
1606 SvGROW(TARG, max + 1);
1607 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
1608 SvCUR_set(TARG, SvCUR(TARG) * count);
1609 }
1610 *SvEND(TARG) = '\0';
1611 }
1612 if (isutf)
1613 (void)SvPOK_only_UTF8(TARG);
1614 else
1615 (void)SvPOK_only(TARG);
1616
1617 if (PL_op->op_private & OPpREPEAT_DOLIST) {
1618 /* The parser saw this as a list repeat, and there
1619 are probably several items on the stack. But we're
1620 in scalar context, and there's no pp_list to save us
1621 now. So drop the rest of the items -- robin@kitsite.com
1622 */
1623 dMARK;
1624 SP = MARK;
1625 }
1626 PUSHTARG;
1627 }
1628 RETURN;
1629}
1630
1631PP(pp_subtract)
1632{
1633 dVAR; dSP; dATARGET; bool useleft; SV *svl, *svr;
1634 tryAMAGICbin_MG(subtr_amg, AMGf_assign|AMGf_numeric);
1635 svr = TOPs;
1636 svl = TOPm1s;
1637 useleft = USE_LEFT(svl);
1638#ifdef PERL_PRESERVE_IVUV
1639 /* See comments in pp_add (in pp_hot.c) about Overflow, and how
1640 "bad things" happen if you rely on signed integers wrapping. */
1641 SvIV_please_nomg(svr);
1642 if (SvIOK(svr)) {
1643 /* Unless the left argument is integer in range we are going to have to
1644 use NV maths. Hence only attempt to coerce the right argument if
1645 we know the left is integer. */
1646 register UV auv = 0;
1647 bool auvok = FALSE;
1648 bool a_valid = 0;
1649
1650 if (!useleft) {
1651 auv = 0;
1652 a_valid = auvok = 1;
1653 /* left operand is undef, treat as zero. */
1654 } else {
1655 /* Left operand is defined, so is it IV? */
1656 SvIV_please_nomg(svl);
1657 if (SvIOK(svl)) {
1658 if ((auvok = SvUOK(svl)))
1659 auv = SvUVX(svl);
1660 else {
1661 register const IV aiv = SvIVX(svl);
1662 if (aiv >= 0) {
1663 auv = aiv;
1664 auvok = 1; /* Now acting as a sign flag. */
1665 } else { /* 2s complement assumption for IV_MIN */
1666 auv = (UV)-aiv;
1667 }
1668 }
1669 a_valid = 1;
1670 }
1671 }
1672 if (a_valid) {
1673 bool result_good = 0;
1674 UV result;
1675 register UV buv;
1676 bool buvok = SvUOK(svr);
1677
1678 if (buvok)
1679 buv = SvUVX(svr);
1680 else {
1681 register const IV biv = SvIVX(svr);
1682 if (biv >= 0) {
1683 buv = biv;
1684 buvok = 1;
1685 } else
1686 buv = (UV)-biv;
1687 }
1688 /* ?uvok if value is >= 0. basically, flagged as UV if it's +ve,
1689 else "IV" now, independent of how it came in.
1690 if a, b represents positive, A, B negative, a maps to -A etc
1691 a - b => (a - b)
1692 A - b => -(a + b)
1693 a - B => (a + b)
1694 A - B => -(a - b)
1695 all UV maths. negate result if A negative.
1696 subtract if signs same, add if signs differ. */
1697
1698 if (auvok ^ buvok) {
1699 /* Signs differ. */
1700 result = auv + buv;
1701 if (result >= auv)
1702 result_good = 1;
1703 } else {
1704 /* Signs same */
1705 if (auv >= buv) {
1706 result = auv - buv;
1707 /* Must get smaller */
1708 if (result <= auv)
1709 result_good = 1;
1710 } else {
1711 result = buv - auv;
1712 if (result <= buv) {
1713 /* result really should be -(auv-buv). as its negation
1714 of true value, need to swap our result flag */
1715 auvok = !auvok;
1716 result_good = 1;
1717 }
1718 }
1719 }
1720 if (result_good) {
1721 SP--;
1722 if (auvok)
1723 SETu( result );
1724 else {
1725 /* Negate result */
1726 if (result <= (UV)IV_MIN)
1727 SETi( -(IV)result );
1728 else {
1729 /* result valid, but out of range for IV. */
1730 SETn( -(NV)result );
1731 }
1732 }
1733 RETURN;
1734 } /* Overflow, drop through to NVs. */
1735 }
1736 }
1737#endif
1738 {
1739 NV value = SvNV_nomg(svr);
1740 (void)POPs;
1741
1742 if (!useleft) {
1743 /* left operand is undef, treat as zero - value */
1744 SETn(-value);
1745 RETURN;
1746 }
1747 SETn( SvNV_nomg(svl) - value );
1748 RETURN;
1749 }
1750}
1751
1752PP(pp_left_shift)
1753{
1754 dVAR; dSP; dATARGET; SV *svl, *svr;
1755 tryAMAGICbin_MG(lshift_amg, AMGf_assign);
1756 svr = POPs;
1757 svl = TOPs;
1758 {
1759 const IV shift = SvIV_nomg(svr);
1760 if (PL_op->op_private & HINT_INTEGER) {
1761 const IV i = SvIV_nomg(svl);
1762 SETi(i << shift);
1763 }
1764 else {
1765 const UV u = SvUV_nomg(svl);
1766 SETu(u << shift);
1767 }
1768 RETURN;
1769 }
1770}
1771
1772PP(pp_right_shift)
1773{
1774 dVAR; dSP; dATARGET; SV *svl, *svr;
1775 tryAMAGICbin_MG(rshift_amg, AMGf_assign);
1776 svr = POPs;
1777 svl = TOPs;
1778 {
1779 const IV shift = SvIV_nomg(svr);
1780 if (PL_op->op_private & HINT_INTEGER) {
1781 const IV i = SvIV_nomg(svl);
1782 SETi(i >> shift);
1783 }
1784 else {
1785 const UV u = SvUV_nomg(svl);
1786 SETu(u >> shift);
1787 }
1788 RETURN;
1789 }
1790}
1791
1792PP(pp_lt)
1793{
1794 dVAR; dSP;
1795 tryAMAGICbin_MG(lt_amg, AMGf_set);
1796#ifdef PERL_PRESERVE_IVUV
1797 SvIV_please_nomg(TOPs);
1798 if (SvIOK(TOPs)) {
1799 SvIV_please_nomg(TOPm1s);
1800 if (SvIOK(TOPm1s)) {
1801 bool auvok = SvUOK(TOPm1s);
1802 bool buvok = SvUOK(TOPs);
1803
1804 if (!auvok && !buvok) { /* ## IV < IV ## */
1805 const IV aiv = SvIVX(TOPm1s);
1806 const IV biv = SvIVX(TOPs);
1807
1808 SP--;
1809 SETs(boolSV(aiv < biv));
1810 RETURN;
1811 }
1812 if (auvok && buvok) { /* ## UV < UV ## */
1813 const UV auv = SvUVX(TOPm1s);
1814 const UV buv = SvUVX(TOPs);
1815
1816 SP--;
1817 SETs(boolSV(auv < buv));
1818 RETURN;
1819 }
1820 if (auvok) { /* ## UV < IV ## */
1821 UV auv;
1822 const IV biv = SvIVX(TOPs);
1823 SP--;
1824 if (biv < 0) {
1825 /* As (a) is a UV, it's >=0, so it cannot be < */
1826 SETs(&PL_sv_no);
1827 RETURN;
1828 }
1829 auv = SvUVX(TOPs);
1830 SETs(boolSV(auv < (UV)biv));
1831 RETURN;
1832 }
1833 { /* ## IV < UV ## */
1834 const IV aiv = SvIVX(TOPm1s);
1835 UV buv;
1836
1837 if (aiv < 0) {
1838 /* As (b) is a UV, it's >=0, so it must be < */
1839 SP--;
1840 SETs(&PL_sv_yes);
1841 RETURN;
1842 }
1843 buv = SvUVX(TOPs);
1844 SP--;
1845 SETs(boolSV((UV)aiv < buv));
1846 RETURN;
1847 }
1848 }
1849 }
1850#endif
1851#ifndef NV_PRESERVES_UV
1852#ifdef PERL_PRESERVE_IVUV
1853 else
1854#endif
1855 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
1856 SP--;
1857 SETs(boolSV(SvRV(TOPs) < SvRV(TOPp1s)));
1858 RETURN;
1859 }
1860#endif
1861 {
1862#if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
1863 dPOPTOPnnrl_nomg;
1864 if (Perl_isnan(left) || Perl_isnan(right))
1865 RETSETNO;
1866 SETs(boolSV(left < right));
1867#else
1868 dPOPnv_nomg;
1869 SETs(boolSV(SvNV_nomg(TOPs) < value));
1870#endif
1871 RETURN;
1872 }
1873}
1874
1875PP(pp_gt)
1876{
1877 dVAR; dSP;
1878 tryAMAGICbin_MG(gt_amg, AMGf_set);
1879#ifdef PERL_PRESERVE_IVUV
1880 SvIV_please_nomg(TOPs);
1881 if (SvIOK(TOPs)) {
1882 SvIV_please_nomg(TOPm1s);
1883 if (SvIOK(TOPm1s)) {
1884 bool auvok = SvUOK(TOPm1s);
1885 bool buvok = SvUOK(TOPs);
1886
1887 if (!auvok && !buvok) { /* ## IV > IV ## */
1888 const IV aiv = SvIVX(TOPm1s);
1889 const IV biv = SvIVX(TOPs);
1890
1891 SP--;
1892 SETs(boolSV(aiv > biv));
1893 RETURN;
1894 }
1895 if (auvok && buvok) { /* ## UV > UV ## */
1896 const UV auv = SvUVX(TOPm1s);
1897 const UV buv = SvUVX(TOPs);
1898
1899 SP--;
1900 SETs(boolSV(auv > buv));
1901 RETURN;
1902 }
1903 if (auvok) { /* ## UV > IV ## */
1904 UV auv;
1905 const IV biv = SvIVX(TOPs);
1906
1907 SP--;
1908 if (biv < 0) {
1909 /* As (a) is a UV, it's >=0, so it must be > */
1910 SETs(&PL_sv_yes);
1911 RETURN;
1912 }
1913 auv = SvUVX(TOPs);
1914 SETs(boolSV(auv > (UV)biv));
1915 RETURN;
1916 }
1917 { /* ## IV > UV ## */
1918 const IV aiv = SvIVX(TOPm1s);
1919 UV buv;
1920
1921 if (aiv < 0) {
1922 /* As (b) is a UV, it's >=0, so it cannot be > */
1923 SP--;
1924 SETs(&PL_sv_no);
1925 RETURN;
1926 }
1927 buv = SvUVX(TOPs);
1928 SP--;
1929 SETs(boolSV((UV)aiv > buv));
1930 RETURN;
1931 }
1932 }
1933 }
1934#endif
1935#ifndef NV_PRESERVES_UV
1936#ifdef PERL_PRESERVE_IVUV
1937 else
1938#endif
1939 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
1940 SP--;
1941 SETs(boolSV(SvRV(TOPs) > SvRV(TOPp1s)));
1942 RETURN;
1943 }
1944#endif
1945 {
1946#if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
1947 dPOPTOPnnrl_nomg;
1948 if (Perl_isnan(left) || Perl_isnan(right))
1949 RETSETNO;
1950 SETs(boolSV(left > right));
1951#else
1952 dPOPnv_nomg;
1953 SETs(boolSV(SvNV_nomg(TOPs) > value));
1954#endif
1955 RETURN;
1956 }
1957}
1958
1959PP(pp_le)
1960{
1961 dVAR; dSP;
1962 tryAMAGICbin_MG(le_amg, AMGf_set);
1963#ifdef PERL_PRESERVE_IVUV
1964 SvIV_please_nomg(TOPs);
1965 if (SvIOK(TOPs)) {
1966 SvIV_please_nomg(TOPm1s);
1967 if (SvIOK(TOPm1s)) {
1968 bool auvok = SvUOK(TOPm1s);
1969 bool buvok = SvUOK(TOPs);
1970
1971 if (!auvok && !buvok) { /* ## IV <= IV ## */
1972 const IV aiv = SvIVX(TOPm1s);
1973 const IV biv = SvIVX(TOPs);
1974
1975 SP--;
1976 SETs(boolSV(aiv <= biv));
1977 RETURN;
1978 }
1979 if (auvok && buvok) { /* ## UV <= UV ## */
1980 UV auv = SvUVX(TOPm1s);
1981 UV buv = SvUVX(TOPs);
1982
1983 SP--;
1984 SETs(boolSV(auv <= buv));
1985 RETURN;
1986 }
1987 if (auvok) { /* ## UV <= IV ## */
1988 UV auv;
1989 const IV biv = SvIVX(TOPs);
1990
1991 SP--;
1992 if (biv < 0) {
1993 /* As (a) is a UV, it's >=0, so a cannot be <= */
1994 SETs(&PL_sv_no);
1995 RETURN;
1996 }
1997 auv = SvUVX(TOPs);
1998 SETs(boolSV(auv <= (UV)biv));
1999 RETURN;
2000 }
2001 { /* ## IV <= UV ## */
2002 const IV aiv = SvIVX(TOPm1s);
2003 UV buv;
2004
2005 if (aiv < 0) {
2006 /* As (b) is a UV, it's >=0, so a must be <= */
2007 SP--;
2008 SETs(&PL_sv_yes);
2009 RETURN;
2010 }
2011 buv = SvUVX(TOPs);
2012 SP--;
2013 SETs(boolSV((UV)aiv <= buv));
2014 RETURN;
2015 }
2016 }
2017 }
2018#endif
2019#ifndef NV_PRESERVES_UV
2020#ifdef PERL_PRESERVE_IVUV
2021 else
2022#endif
2023 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
2024 SP--;
2025 SETs(boolSV(SvRV(TOPs) <= SvRV(TOPp1s)));
2026 RETURN;
2027 }
2028#endif
2029 {
2030#if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
2031 dPOPTOPnnrl_nomg;
2032 if (Perl_isnan(left) || Perl_isnan(right))
2033 RETSETNO;
2034 SETs(boolSV(left <= right));
2035#else
2036 dPOPnv_nomg;
2037 SETs(boolSV(SvNV_nomg(TOPs) <= value));
2038#endif
2039 RETURN;
2040 }
2041}
2042
2043PP(pp_ge)
2044{
2045 dVAR; dSP;
2046 tryAMAGICbin_MG(ge_amg,AMGf_set);
2047#ifdef PERL_PRESERVE_IVUV
2048 SvIV_please_nomg(TOPs);
2049 if (SvIOK(TOPs)) {
2050 SvIV_please_nomg(TOPm1s);
2051 if (SvIOK(TOPm1s)) {
2052 bool auvok = SvUOK(TOPm1s);
2053 bool buvok = SvUOK(TOPs);
2054
2055 if (!auvok && !buvok) { /* ## IV >= IV ## */
2056 const IV aiv = SvIVX(TOPm1s);
2057 const IV biv = SvIVX(TOPs);
2058
2059 SP--;
2060 SETs(boolSV(aiv >= biv));
2061 RETURN;
2062 }
2063 if (auvok && buvok) { /* ## UV >= UV ## */
2064 const UV auv = SvUVX(TOPm1s);
2065 const UV buv = SvUVX(TOPs);
2066
2067 SP--;
2068 SETs(boolSV(auv >= buv));
2069 RETURN;
2070 }
2071 if (auvok) { /* ## UV >= IV ## */
2072 UV auv;
2073 const IV biv = SvIVX(TOPs);
2074
2075 SP--;
2076 if (biv < 0) {
2077 /* As (a) is a UV, it's >=0, so it must be >= */
2078 SETs(&PL_sv_yes);
2079 RETURN;
2080 }
2081 auv = SvUVX(TOPs);
2082 SETs(boolSV(auv >= (UV)biv));
2083 RETURN;
2084 }
2085 { /* ## IV >= UV ## */
2086 const IV aiv = SvIVX(TOPm1s);
2087 UV buv;
2088
2089 if (aiv < 0) {
2090 /* As (b) is a UV, it's >=0, so a cannot be >= */
2091 SP--;
2092 SETs(&PL_sv_no);
2093 RETURN;
2094 }
2095 buv = SvUVX(TOPs);
2096 SP--;
2097 SETs(boolSV((UV)aiv >= buv));
2098 RETURN;
2099 }
2100 }
2101 }
2102#endif
2103#ifndef NV_PRESERVES_UV
2104#ifdef PERL_PRESERVE_IVUV
2105 else
2106#endif
2107 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
2108 SP--;
2109 SETs(boolSV(SvRV(TOPs) >= SvRV(TOPp1s)));
2110 RETURN;
2111 }
2112#endif
2113 {
2114#if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
2115 dPOPTOPnnrl_nomg;
2116 if (Perl_isnan(left) || Perl_isnan(right))
2117 RETSETNO;
2118 SETs(boolSV(left >= right));
2119#else
2120 dPOPnv_nomg;
2121 SETs(boolSV(SvNV_nomg(TOPs) >= value));
2122#endif
2123 RETURN;
2124 }
2125}
2126
2127PP(pp_ne)
2128{
2129 dVAR; dSP;
2130 tryAMAGICbin_MG(ne_amg,AMGf_set);
2131#ifndef NV_PRESERVES_UV
2132 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
2133 SP--;
2134 SETs(boolSV(SvRV(TOPs) != SvRV(TOPp1s)));
2135 RETURN;
2136 }
2137#endif
2138#ifdef PERL_PRESERVE_IVUV
2139 SvIV_please_nomg(TOPs);
2140 if (SvIOK(TOPs)) {
2141 SvIV_please_nomg(TOPm1s);
2142 if (SvIOK(TOPm1s)) {
2143 const bool auvok = SvUOK(TOPm1s);
2144 const bool buvok = SvUOK(TOPs);
2145
2146 if (auvok == buvok) { /* ## IV == IV or UV == UV ## */
2147 /* Casting IV to UV before comparison isn't going to matter
2148 on 2s complement. On 1s complement or sign&magnitude
2149 (if we have any of them) it could make negative zero
2150 differ from normal zero. As I understand it. (Need to
2151 check - is negative zero implementation defined behaviour
2152 anyway?). NWC */
2153 const UV buv = SvUVX(POPs);
2154 const UV auv = SvUVX(TOPs);
2155
2156 SETs(boolSV(auv != buv));
2157 RETURN;
2158 }
2159 { /* ## Mixed IV,UV ## */
2160 IV iv;
2161 UV uv;
2162
2163 /* != is commutative so swap if needed (save code) */
2164 if (auvok) {
2165 /* swap. top of stack (b) is the iv */
2166 iv = SvIVX(TOPs);
2167 SP--;
2168 if (iv < 0) {
2169 /* As (a) is a UV, it's >0, so it cannot be == */
2170 SETs(&PL_sv_yes);
2171 RETURN;
2172 }
2173 uv = SvUVX(TOPs);
2174 } else {
2175 iv = SvIVX(TOPm1s);
2176 SP--;
2177 if (iv < 0) {
2178 /* As (b) is a UV, it's >0, so it cannot be == */
2179 SETs(&PL_sv_yes);
2180 RETURN;
2181 }
2182 uv = SvUVX(*(SP+1)); /* Do I want TOPp1s() ? */
2183 }
2184 SETs(boolSV((UV)iv != uv));
2185 RETURN;
2186 }
2187 }
2188 }
2189#endif
2190 {
2191#if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
2192 dPOPTOPnnrl_nomg;
2193 if (Perl_isnan(left) || Perl_isnan(right))
2194 RETSETYES;
2195 SETs(boolSV(left != right));
2196#else
2197 dPOPnv_nomg;
2198 SETs(boolSV(SvNV_nomg(TOPs) != value));
2199#endif
2200 RETURN;
2201 }
2202}
2203
2204PP(pp_ncmp)
2205{
2206 dVAR; dSP; dTARGET;
2207 tryAMAGICbin_MG(ncmp_amg, 0);
2208#ifndef NV_PRESERVES_UV
2209 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
2210 const UV right = PTR2UV(SvRV(POPs));
2211 const UV left = PTR2UV(SvRV(TOPs));
2212 SETi((left > right) - (left < right));
2213 RETURN;
2214 }
2215#endif
2216#ifdef PERL_PRESERVE_IVUV
2217 /* Fortunately it seems NaN isn't IOK */
2218 SvIV_please_nomg(TOPs);
2219 if (SvIOK(TOPs)) {
2220 SvIV_please_nomg(TOPm1s);
2221 if (SvIOK(TOPm1s)) {
2222 const bool leftuvok = SvUOK(TOPm1s);
2223 const bool rightuvok = SvUOK(TOPs);
2224 I32 value;
2225 if (!leftuvok && !rightuvok) { /* ## IV <=> IV ## */
2226 const IV leftiv = SvIVX(TOPm1s);
2227 const IV rightiv = SvIVX(TOPs);
2228
2229 if (leftiv > rightiv)
2230 value = 1;
2231 else if (leftiv < rightiv)
2232 value = -1;
2233 else
2234 value = 0;
2235 } else if (leftuvok && rightuvok) { /* ## UV <=> UV ## */
2236 const UV leftuv = SvUVX(TOPm1s);
2237 const UV rightuv = SvUVX(TOPs);
2238
2239 if (leftuv > rightuv)
2240 value = 1;
2241 else if (leftuv < rightuv)
2242 value = -1;
2243 else
2244 value = 0;
2245 } else if (leftuvok) { /* ## UV <=> IV ## */
2246 const IV rightiv = SvIVX(TOPs);
2247 if (rightiv < 0) {
2248 /* As (a) is a UV, it's >=0, so it cannot be < */
2249 value = 1;
2250 } else {
2251 const UV leftuv = SvUVX(TOPm1s);
2252 if (leftuv > (UV)rightiv) {
2253 value = 1;
2254 } else if (leftuv < (UV)rightiv) {
2255 value = -1;
2256 } else {
2257 value = 0;
2258 }
2259 }
2260 } else { /* ## IV <=> UV ## */
2261 const IV leftiv = SvIVX(TOPm1s);
2262 if (leftiv < 0) {
2263 /* As (b) is a UV, it's >=0, so it must be < */
2264 value = -1;
2265 } else {
2266 const UV rightuv = SvUVX(TOPs);
2267 if ((UV)leftiv > rightuv) {
2268 value = 1;
2269 } else if ((UV)leftiv < rightuv) {
2270 value = -1;
2271 } else {
2272 value = 0;
2273 }
2274 }
2275 }
2276 SP--;
2277 SETi(value);
2278 RETURN;
2279 }
2280 }
2281#endif
2282 {
2283 dPOPTOPnnrl_nomg;
2284 I32 value;
2285
2286#ifdef Perl_isnan
2287 if (Perl_isnan(left) || Perl_isnan(right)) {
2288 SETs(&PL_sv_undef);
2289 RETURN;
2290 }
2291 value = (left > right) - (left < right);
2292#else
2293 if (left == right)
2294 value = 0;
2295 else if (left < right)
2296 value = -1;
2297 else if (left > right)
2298 value = 1;
2299 else {
2300 SETs(&PL_sv_undef);
2301 RETURN;
2302 }
2303#endif
2304 SETi(value);
2305 RETURN;
2306 }
2307}
2308
2309PP(pp_sle)
2310{
2311 dVAR; dSP;
2312
2313 int amg_type = sle_amg;
2314 int multiplier = 1;
2315 int rhs = 1;
2316
2317 switch (PL_op->op_type) {
2318 case OP_SLT:
2319 amg_type = slt_amg;
2320 /* cmp < 0 */
2321 rhs = 0;
2322 break;
2323 case OP_SGT:
2324 amg_type = sgt_amg;
2325 /* cmp > 0 */
2326 multiplier = -1;
2327 rhs = 0;
2328 break;
2329 case OP_SGE:
2330 amg_type = sge_amg;
2331 /* cmp >= 0 */
2332 multiplier = -1;
2333 break;
2334 }
2335
2336 tryAMAGICbin_MG(amg_type, AMGf_set);
2337 {
2338 dPOPTOPssrl;
2339 const int cmp = (IN_LOCALE_RUNTIME
2340 ? sv_cmp_locale(left, right)
2341 : sv_cmp(left, right));
2342 SETs(boolSV(cmp * multiplier < rhs));
2343 RETURN;
2344 }
2345}
2346
2347PP(pp_seq)
2348{
2349 dVAR; dSP;
2350 tryAMAGICbin_MG(seq_amg, AMGf_set);
2351 {
2352 dPOPTOPssrl;
2353 SETs(boolSV(sv_eq(left, right)));
2354 RETURN;
2355 }
2356}
2357
2358PP(pp_sne)
2359{
2360 dVAR; dSP;
2361 tryAMAGICbin_MG(sne_amg, AMGf_set);
2362 {
2363 dPOPTOPssrl;
2364 SETs(boolSV(!sv_eq(left, right)));
2365 RETURN;
2366 }
2367}
2368
2369PP(pp_scmp)
2370{
2371 dVAR; dSP; dTARGET;
2372 tryAMAGICbin_MG(scmp_amg, 0);
2373 {
2374 dPOPTOPssrl;
2375 const int cmp = (IN_LOCALE_RUNTIME
2376 ? sv_cmp_locale(left, right)
2377 : sv_cmp(left, right));
2378 SETi( cmp );
2379 RETURN;
2380 }
2381}
2382
2383PP(pp_bit_and)
2384{
2385 dVAR; dSP; dATARGET;
2386 tryAMAGICbin_MG(band_amg, AMGf_assign);
2387 {
2388 dPOPTOPssrl;
2389 if (SvNIOKp(left) || SvNIOKp(right)) {
2390 if (PL_op->op_private & HINT_INTEGER) {
2391 const IV i = SvIV_nomg(left) & SvIV_nomg(right);
2392 SETi(i);
2393 }
2394 else {
2395 const UV u = SvUV_nomg(left) & SvUV_nomg(right);
2396 SETu(u);
2397 }
2398 }
2399 else {
2400 do_vop(PL_op->op_type, TARG, left, right);
2401 SETTARG;
2402 }
2403 RETURN;
2404 }
2405}
2406
2407PP(pp_bit_or)
2408{
2409 dVAR; dSP; dATARGET;
2410 const int op_type = PL_op->op_type;
2411
2412 tryAMAGICbin_MG((op_type == OP_BIT_OR ? bor_amg : bxor_amg), AMGf_assign);
2413 {
2414 dPOPTOPssrl;
2415 if (SvNIOKp(left) || SvNIOKp(right)) {
2416 if (PL_op->op_private & HINT_INTEGER) {
2417 const IV l = (USE_LEFT(left) ? SvIV_nomg(left) : 0);
2418 const IV r = SvIV_nomg(right);
2419 const IV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2420 SETi(result);
2421 }
2422 else {
2423 const UV l = (USE_LEFT(left) ? SvUV_nomg(left) : 0);
2424 const UV r = SvUV_nomg(right);
2425 const UV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2426 SETu(result);
2427 }
2428 }
2429 else {
2430 do_vop(op_type, TARG, left, right);
2431 SETTARG;
2432 }
2433 RETURN;
2434 }
2435}
2436
2437PP(pp_negate)
2438{
2439 dVAR; dSP; dTARGET;
2440 tryAMAGICun_MG(neg_amg, AMGf_numeric);
2441 {
2442 SV * const sv = TOPs;
2443 const int flags = SvFLAGS(sv);
2444 if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
2445 /* It's publicly an integer, or privately an integer-not-float */
2446 oops_its_an_int:
2447 if (SvIsUV(sv)) {
2448 if (SvIVX(sv) == IV_MIN) {
2449 /* 2s complement assumption. */
2450 SETi(SvIVX(sv)); /* special case: -((UV)IV_MAX+1) == IV_MIN */
2451 RETURN;
2452 }
2453 else if (SvUVX(sv) <= IV_MAX) {
2454 SETi(-SvIVX(sv));
2455 RETURN;
2456 }
2457 }
2458 else if (SvIVX(sv) != IV_MIN) {
2459 SETi(-SvIVX(sv));
2460 RETURN;
2461 }
2462#ifdef PERL_PRESERVE_IVUV
2463 else {
2464 SETu((UV)IV_MIN);
2465 RETURN;
2466 }
2467#endif
2468 }
2469 if (SvNIOKp(sv))
2470 SETn(-SvNV_nomg(sv));
2471 else if (SvPOKp(sv)) {
2472 STRLEN len;
2473 const char * const s = SvPV_nomg_const(sv, len);
2474 if (isIDFIRST(*s)) {
2475 sv_setpvs(TARG, "-");
2476 sv_catsv(TARG, sv);
2477 }
2478 else if (*s == '+' || *s == '-') {
2479 sv_setsv_nomg(TARG, sv);
2480 *SvPV_force_nomg(TARG, len) = *s == '-' ? '+' : '-';
2481 }
2482 else if (DO_UTF8(sv)) {
2483 SvIV_please_nomg(sv);
2484 if (SvIOK(sv))
2485 goto oops_its_an_int;
2486 if (SvNOK(sv))
2487 sv_setnv(TARG, -SvNV_nomg(sv));
2488 else {
2489 sv_setpvs(TARG, "-");
2490 sv_catsv(TARG, sv);
2491 }
2492 }
2493 else {
2494 SvIV_please_nomg(sv);
2495 if (SvIOK(sv))
2496 goto oops_its_an_int;
2497 sv_setnv(TARG, -SvNV_nomg(sv));
2498 }
2499 SETTARG;
2500 }
2501 else
2502 SETn(-SvNV_nomg(sv));
2503 }
2504 RETURN;
2505}
2506
2507PP(pp_not)
2508{
2509 dVAR; dSP;
2510 tryAMAGICun_MG(not_amg, AMGf_set);
2511 *PL_stack_sp = boolSV(!SvTRUE(*PL_stack_sp));
2512 return NORMAL;
2513}
2514
2515PP(pp_complement)
2516{
2517 dVAR; dSP; dTARGET;
2518 tryAMAGICun_MG(compl_amg, 0);
2519 {
2520 dTOPss;
2521 if (SvNIOKp(sv)) {
2522 if (PL_op->op_private & HINT_INTEGER) {
2523 const IV i = ~SvIV_nomg(sv);
2524 SETi(i);
2525 }
2526 else {
2527 const UV u = ~SvUV_nomg(sv);
2528 SETu(u);
2529 }
2530 }
2531 else {
2532 register U8 *tmps;
2533 register I32 anum;
2534 STRLEN len;
2535
2536 (void)SvPV_nomg_const(sv,len); /* force check for uninit var */
2537 sv_setsv_nomg(TARG, sv);
2538 tmps = (U8*)SvPV_force_nomg(TARG, len);
2539 anum = len;
2540 if (SvUTF8(TARG)) {
2541 /* Calculate exact length, let's not estimate. */
2542 STRLEN targlen = 0;
2543 STRLEN l;
2544 UV nchar = 0;
2545 UV nwide = 0;
2546 U8 * const send = tmps + len;
2547 U8 * const origtmps = tmps;
2548 const UV utf8flags = UTF8_ALLOW_ANYUV;
2549
2550 while (tmps < send) {
2551 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2552 tmps += l;
2553 targlen += UNISKIP(~c);
2554 nchar++;
2555 if (c > 0xff)
2556 nwide++;
2557 }
2558
2559 /* Now rewind strings and write them. */
2560 tmps = origtmps;
2561
2562 if (nwide) {
2563 U8 *result;
2564 U8 *p;
2565
2566 Newx(result, targlen + 1, U8);
2567 p = result;
2568 while (tmps < send) {
2569 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2570 tmps += l;
2571 p = uvchr_to_utf8_flags(p, ~c, UNICODE_ALLOW_ANY);
2572 }
2573 *p = '\0';
2574 sv_usepvn_flags(TARG, (char*)result, targlen,
2575 SV_HAS_TRAILING_NUL);
2576 SvUTF8_on(TARG);
2577 }
2578 else {
2579 U8 *result;
2580 U8 *p;
2581
2582 Newx(result, nchar + 1, U8);
2583 p = result;
2584 while (tmps < send) {
2585 const U8 c = (U8)utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2586 tmps += l;
2587 *p++ = ~c;
2588 }
2589 *p = '\0';
2590 sv_usepvn_flags(TARG, (char*)result, nchar, SV_HAS_TRAILING_NUL);
2591 SvUTF8_off(TARG);
2592 }
2593 SETTARG;
2594 RETURN;
2595 }
2596#ifdef LIBERAL
2597 {
2598 register long *tmpl;
2599 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
2600 *tmps = ~*tmps;
2601 tmpl = (long*)tmps;
2602 for ( ; anum >= (I32)sizeof(long); anum -= (I32)sizeof(long), tmpl++)
2603 *tmpl = ~*tmpl;
2604 tmps = (U8*)tmpl;
2605 }
2606#endif
2607 for ( ; anum > 0; anum--, tmps++)
2608 *tmps = ~*tmps;
2609 SETTARG;
2610 }
2611 RETURN;
2612 }
2613}
2614
2615/* integer versions of some of the above */
2616
2617PP(pp_i_multiply)
2618{
2619 dVAR; dSP; dATARGET;
2620 tryAMAGICbin_MG(mult_amg, AMGf_assign);
2621 {
2622 dPOPTOPiirl_nomg;
2623 SETi( left * right );
2624 RETURN;
2625 }
2626}
2627
2628PP(pp_i_divide)
2629{
2630 IV num;
2631 dVAR; dSP; dATARGET;
2632 tryAMAGICbin_MG(div_amg, AMGf_assign);
2633 {
2634 dPOPTOPssrl;
2635 IV value = SvIV_nomg(right);
2636 if (value == 0)
2637 DIE(aTHX_ "Illegal division by zero");
2638 num = SvIV_nomg(left);
2639
2640 /* avoid FPE_INTOVF on some platforms when num is IV_MIN */
2641 if (value == -1)
2642 value = - num;
2643 else
2644 value = num / value;
2645 SETi(value);
2646 RETURN;
2647 }
2648}
2649
2650#if defined(__GLIBC__) && IVSIZE == 8
2651STATIC
2652PP(pp_i_modulo_0)
2653#else
2654PP(pp_i_modulo)
2655#endif
2656{
2657 /* This is the vanilla old i_modulo. */
2658 dVAR; dSP; dATARGET;
2659 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2660 {
2661 dPOPTOPiirl_nomg;
2662 if (!right)
2663 DIE(aTHX_ "Illegal modulus zero");
2664 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2665 if (right == -1)
2666 SETi( 0 );
2667 else
2668 SETi( left % right );
2669 RETURN;
2670 }
2671}
2672
2673#if defined(__GLIBC__) && IVSIZE == 8
2674STATIC
2675PP(pp_i_modulo_1)
2676
2677{
2678 /* This is the i_modulo with the workaround for the _moddi3 bug
2679 * in (at least) glibc 2.2.5 (the PERL_ABS() the workaround).
2680 * See below for pp_i_modulo. */
2681 dVAR; dSP; dATARGET;
2682 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2683 {
2684 dPOPTOPiirl_nomg;
2685 if (!right)
2686 DIE(aTHX_ "Illegal modulus zero");
2687 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2688 if (right == -1)
2689 SETi( 0 );
2690 else
2691 SETi( left % PERL_ABS(right) );
2692 RETURN;
2693 }
2694}
2695
2696PP(pp_i_modulo)
2697{
2698 dVAR; dSP; dATARGET;
2699 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2700 {
2701 dPOPTOPiirl_nomg;
2702 if (!right)
2703 DIE(aTHX_ "Illegal modulus zero");
2704 /* The assumption is to use hereafter the old vanilla version... */
2705 PL_op->op_ppaddr =
2706 PL_ppaddr[OP_I_MODULO] =
2707 Perl_pp_i_modulo_0;
2708 /* .. but if we have glibc, we might have a buggy _moddi3
2709 * (at least glicb 2.2.5 is known to have this bug), in other
2710 * words our integer modulus with negative quad as the second
2711 * argument might be broken. Test for this and re-patch the
2712 * opcode dispatch table if that is the case, remembering to
2713 * also apply the workaround so that this first round works
2714 * right, too. See [perl #9402] for more information. */
2715 {
2716 IV l = 3;
2717 IV r = -10;
2718 /* Cannot do this check with inlined IV constants since
2719 * that seems to work correctly even with the buggy glibc. */
2720 if (l % r == -3) {
2721 /* Yikes, we have the bug.
2722 * Patch in the workaround version. */
2723 PL_op->op_ppaddr =
2724 PL_ppaddr[OP_I_MODULO] =
2725 &Perl_pp_i_modulo_1;
2726 /* Make certain we work right this time, too. */
2727 right = PERL_ABS(right);
2728 }
2729 }
2730 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2731 if (right == -1)
2732 SETi( 0 );
2733 else
2734 SETi( left % right );
2735 RETURN;
2736 }
2737}
2738#endif
2739
2740PP(pp_i_add)
2741{
2742 dVAR; dSP; dATARGET;
2743 tryAMAGICbin_MG(add_amg, AMGf_assign);
2744 {
2745 dPOPTOPiirl_ul_nomg;
2746 SETi( left + right );
2747 RETURN;
2748 }
2749}
2750
2751PP(pp_i_subtract)
2752{
2753 dVAR; dSP; dATARGET;
2754 tryAMAGICbin_MG(subtr_amg, AMGf_assign);
2755 {
2756 dPOPTOPiirl_ul_nomg;
2757 SETi( left - right );
2758 RETURN;
2759 }
2760}
2761
2762PP(pp_i_lt)
2763{
2764 dVAR; dSP;
2765 tryAMAGICbin_MG(lt_amg, AMGf_set);
2766 {
2767 dPOPTOPiirl_nomg;
2768 SETs(boolSV(left < right));
2769 RETURN;
2770 }
2771}
2772
2773PP(pp_i_gt)
2774{
2775 dVAR; dSP;
2776 tryAMAGICbin_MG(gt_amg, AMGf_set);
2777 {
2778 dPOPTOPiirl_nomg;
2779 SETs(boolSV(left > right));
2780 RETURN;
2781 }
2782}
2783
2784PP(pp_i_le)
2785{
2786 dVAR; dSP;
2787 tryAMAGICbin_MG(le_amg, AMGf_set);
2788 {
2789 dPOPTOPiirl_nomg;
2790 SETs(boolSV(left <= right));
2791 RETURN;
2792 }
2793}
2794
2795PP(pp_i_ge)
2796{
2797 dVAR; dSP;
2798 tryAMAGICbin_MG(ge_amg, AMGf_set);
2799 {
2800 dPOPTOPiirl_nomg;
2801 SETs(boolSV(left >= right));
2802 RETURN;
2803 }
2804}
2805
2806PP(pp_i_eq)
2807{
2808 dVAR; dSP;
2809 tryAMAGICbin_MG(eq_amg, AMGf_set);
2810 {
2811 dPOPTOPiirl_nomg;
2812 SETs(boolSV(left == right));
2813 RETURN;
2814 }
2815}
2816
2817PP(pp_i_ne)
2818{
2819 dVAR; dSP;
2820 tryAMAGICbin_MG(ne_amg, AMGf_set);
2821 {
2822 dPOPTOPiirl_nomg;
2823 SETs(boolSV(left != right));
2824 RETURN;
2825 }
2826}
2827
2828PP(pp_i_ncmp)
2829{
2830 dVAR; dSP; dTARGET;
2831 tryAMAGICbin_MG(ncmp_amg, 0);
2832 {
2833 dPOPTOPiirl_nomg;
2834 I32 value;
2835
2836 if (left > right)
2837 value = 1;
2838 else if (left < right)
2839 value = -1;
2840 else
2841 value = 0;
2842 SETi(value);
2843 RETURN;
2844 }
2845}
2846
2847PP(pp_i_negate)
2848{
2849 dVAR; dSP; dTARGET;
2850 tryAMAGICun_MG(neg_amg, 0);
2851 {
2852 SV * const sv = TOPs;
2853 IV const i = SvIV_nomg(sv);
2854 SETi(-i);
2855 RETURN;
2856 }
2857}
2858
2859/* High falutin' math. */
2860
2861PP(pp_atan2)
2862{
2863 dVAR; dSP; dTARGET;
2864 tryAMAGICbin_MG(atan2_amg, 0);
2865 {
2866 dPOPTOPnnrl_nomg;
2867 SETn(Perl_atan2(left, right));
2868 RETURN;
2869 }
2870}
2871
2872PP(pp_sin)
2873{
2874 dVAR; dSP; dTARGET;
2875 int amg_type = sin_amg;
2876 const char *neg_report = NULL;
2877 NV (*func)(NV) = Perl_sin;
2878 const int op_type = PL_op->op_type;
2879
2880 switch (op_type) {
2881 case OP_COS:
2882 amg_type = cos_amg;
2883 func = Perl_cos;
2884 break;
2885 case OP_EXP:
2886 amg_type = exp_amg;
2887 func = Perl_exp;
2888 break;
2889 case OP_LOG:
2890 amg_type = log_amg;
2891 func = Perl_log;
2892 neg_report = "log";
2893 break;
2894 case OP_SQRT:
2895 amg_type = sqrt_amg;
2896 func = Perl_sqrt;
2897 neg_report = "sqrt";
2898 break;
2899 }
2900
2901
2902 tryAMAGICun_MG(amg_type, 0);
2903 {
2904 SV * const arg = POPs;
2905 const NV value = SvNV_nomg(arg);
2906 if (neg_report) {
2907 if (op_type == OP_LOG ? (value <= 0.0) : (value < 0.0)) {
2908 SET_NUMERIC_STANDARD();
2909 DIE(aTHX_ "Can't take %s of %"NVgf, neg_report, value);
2910 }
2911 }
2912 XPUSHn(func(value));
2913 RETURN;
2914 }
2915}
2916
2917/* Support Configure command-line overrides for rand() functions.
2918 After 5.005, perhaps we should replace this by Configure support
2919 for drand48(), random(), or rand(). For 5.005, though, maintain
2920 compatibility by calling rand() but allow the user to override it.
2921 See INSTALL for details. --Andy Dougherty 15 July 1998
2922*/
2923/* Now it's after 5.005, and Configure supports drand48() and random(),
2924 in addition to rand(). So the overrides should not be needed any more.
2925 --Jarkko Hietaniemi 27 September 1998
2926 */
2927
2928#ifndef HAS_DRAND48_PROTO
2929extern double drand48 (void);
2930#endif
2931
2932PP(pp_rand)
2933{
2934 dVAR; dSP; dTARGET;
2935 NV value;
2936 if (MAXARG < 1)
2937 value = 1.0;
2938 else
2939 value = POPn;
2940 if (value == 0.0)
2941 value = 1.0;
2942 if (!PL_srand_called) {
2943 (void)seedDrand01((Rand_seed_t)seed());
2944 PL_srand_called = TRUE;
2945 }
2946 value *= Drand01();
2947 XPUSHn(value);
2948 RETURN;
2949}
2950
2951PP(pp_srand)
2952{
2953 dVAR; dSP;
2954 const UV anum = (MAXARG < 1) ? seed() : POPu;
2955 (void)seedDrand01((Rand_seed_t)anum);
2956 PL_srand_called = TRUE;
2957 EXTEND(SP, 1);
2958 RETPUSHYES;
2959}
2960
2961PP(pp_int)
2962{
2963 dVAR; dSP; dTARGET;
2964 tryAMAGICun_MG(int_amg, AMGf_numeric);
2965 {
2966 SV * const sv = TOPs;
2967 const IV iv = SvIV_nomg(sv);
2968 /* XXX it's arguable that compiler casting to IV might be subtly
2969 different from modf (for numbers inside (IV_MIN,UV_MAX)) in which
2970 else preferring IV has introduced a subtle behaviour change bug. OTOH
2971 relying on floating point to be accurate is a bug. */
2972
2973 if (!SvOK(sv)) {
2974 SETu(0);
2975 }
2976 else if (SvIOK(sv)) {
2977 if (SvIsUV(sv))
2978 SETu(SvUV_nomg(sv));
2979 else
2980 SETi(iv);
2981 }
2982 else {
2983 const NV value = SvNV_nomg(sv);
2984 if (value >= 0.0) {
2985 if (value < (NV)UV_MAX + 0.5) {
2986 SETu(U_V(value));
2987 } else {
2988 SETn(Perl_floor(value));
2989 }
2990 }
2991 else {
2992 if (value > (NV)IV_MIN - 0.5) {
2993 SETi(I_V(value));
2994 } else {
2995 SETn(Perl_ceil(value));
2996 }
2997 }
2998 }
2999 }
3000 RETURN;
3001}
3002
3003PP(pp_abs)
3004{
3005 dVAR; dSP; dTARGET;
3006 tryAMAGICun_MG(abs_amg, AMGf_numeric);
3007 {
3008 SV * const sv = TOPs;
3009 /* This will cache the NV value if string isn't actually integer */
3010 const IV iv = SvIV_nomg(sv);
3011
3012 if (!SvOK(sv)) {
3013 SETu(0);
3014 }
3015 else if (SvIOK(sv)) {
3016 /* IVX is precise */
3017 if (SvIsUV(sv)) {
3018 SETu(SvUV_nomg(sv)); /* force it to be numeric only */
3019 } else {
3020 if (iv >= 0) {
3021 SETi(iv);
3022 } else {
3023 if (iv != IV_MIN) {
3024 SETi(-iv);
3025 } else {
3026 /* 2s complement assumption. Also, not really needed as
3027 IV_MIN and -IV_MIN should both be %100...00 and NV-able */
3028 SETu(IV_MIN);
3029 }
3030 }
3031 }
3032 } else{
3033 const NV value = SvNV_nomg(sv);
3034 if (value < 0.0)
3035 SETn(-value);
3036 else
3037 SETn(value);
3038 }
3039 }
3040 RETURN;
3041}
3042
3043PP(pp_oct)
3044{
3045 dVAR; dSP; dTARGET;
3046 const char *tmps;
3047 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES;
3048 STRLEN len;
3049 NV result_nv;
3050 UV result_uv;
3051 SV* const sv = POPs;
3052
3053 tmps = (SvPV_const(sv, len));
3054 if (DO_UTF8(sv)) {
3055 /* If Unicode, try to downgrade
3056 * If not possible, croak. */
3057 SV* const tsv = sv_2mortal(newSVsv(sv));
3058
3059 SvUTF8_on(tsv);
3060 sv_utf8_downgrade(tsv, FALSE);
3061 tmps = SvPV_const(tsv, len);
3062 }
3063 if (PL_op->op_type == OP_HEX)
3064 goto hex;
3065
3066 while (*tmps && len && isSPACE(*tmps))
3067 tmps++, len--;
3068 if (*tmps == '0')
3069 tmps++, len--;
3070 if (*tmps == 'x') {
3071 hex:
3072 result_uv = grok_hex (tmps, &len, &flags, &result_nv);
3073 }
3074 else if (*tmps == 'b')
3075 result_uv = grok_bin (tmps, &len, &flags, &result_nv);
3076 else
3077 result_uv = grok_oct (tmps, &len, &flags, &result_nv);
3078
3079 if (flags & PERL_SCAN_GREATER_THAN_UV_MAX) {
3080 XPUSHn(result_nv);
3081 }
3082 else {
3083 XPUSHu(result_uv);
3084 }
3085 RETURN;
3086}
3087
3088/* String stuff. */
3089
3090PP(pp_length)
3091{
3092 dVAR; dSP; dTARGET;
3093 SV * const sv = TOPs;
3094
3095 if (SvGAMAGIC(sv)) {
3096 /* For an overloaded or magic scalar, we can't know in advance if
3097 it's going to be UTF-8 or not. Also, we can't call sv_len_utf8 as
3098 it likes to cache the length. Maybe that should be a documented
3099 feature of it.
3100 */
3101 STRLEN len;
3102 const char *const p
3103 = sv_2pv_flags(sv, &len,
3104 SV_UNDEF_RETURNS_NULL|SV_CONST_RETURN|SV_GMAGIC);
3105
3106 if (!p)
3107 SETs(&PL_sv_undef);
3108 else if (DO_UTF8(sv)) {
3109 SETi(utf8_length((U8*)p, (U8*)p + len));
3110 }
3111 else
3112 SETi(len);
3113 } else if (SvOK(sv)) {
3114 /* Neither magic nor overloaded. */
3115 if (DO_UTF8(sv))
3116 SETi(sv_len_utf8(sv));
3117 else
3118 SETi(sv_len(sv));
3119 } else {
3120 SETs(&PL_sv_undef);
3121 }
3122 RETURN;
3123}
3124
3125PP(pp_substr)
3126{
3127 dVAR; dSP; dTARGET;
3128 SV *sv;
3129 STRLEN curlen;
3130 STRLEN utf8_curlen;
3131 SV * pos_sv;
3132 IV pos1_iv;
3133 int pos1_is_uv;
3134 IV pos2_iv;
3135 int pos2_is_uv;
3136 SV * len_sv;
3137 IV len_iv = 0;
3138 int len_is_uv = 1;
3139 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
3140 const char *tmps;
3141 const IV arybase = CopARYBASE_get(PL_curcop);
3142 SV *repl_sv = NULL;
3143 const char *repl = NULL;
3144 STRLEN repl_len;
3145 const int num_args = PL_op->op_private & 7;
3146 bool repl_need_utf8_upgrade = FALSE;
3147 bool repl_is_utf8 = FALSE;
3148
3149 SvTAINTED_off(TARG); /* decontaminate */
3150 SvUTF8_off(TARG); /* decontaminate */
3151 if (num_args > 2) {
3152 if (num_args > 3) {
3153 repl_sv = POPs;
3154 repl = SvPV_const(repl_sv, repl_len);
3155 repl_is_utf8 = DO_UTF8(repl_sv) && SvCUR(repl_sv);
3156 }
3157 len_sv = POPs;
3158 len_iv = SvIV(len_sv);
3159 len_is_uv = SvIOK_UV(len_sv);
3160 }
3161 pos_sv = POPs;
3162 pos1_iv = SvIV(pos_sv);
3163 pos1_is_uv = SvIOK_UV(pos_sv);
3164 sv = POPs;
3165 PUTBACK;
3166 if (repl_sv) {
3167 if (repl_is_utf8) {
3168 if (!DO_UTF8(sv))
3169 sv_utf8_upgrade(sv);
3170 }
3171 else if (DO_UTF8(sv))
3172 repl_need_utf8_upgrade = TRUE;
3173 }
3174 tmps = SvPV_const(sv, curlen);
3175 if (DO_UTF8(sv)) {
3176 utf8_curlen = sv_len_utf8(sv);
3177 if (utf8_curlen == curlen)
3178 utf8_curlen = 0;
3179 else
3180 curlen = utf8_curlen;
3181 }
3182 else
3183 utf8_curlen = 0;
3184
3185 if ( (pos1_is_uv && arybase < 0) || (pos1_iv >= arybase) ) { /* pos >= $[ */
3186 UV pos1_uv = pos1_iv-arybase;
3187 /* Overflow can occur when $[ < 0 */
3188 if (arybase < 0 && pos1_uv < (UV)pos1_iv)
3189 goto bound_fail;
3190 pos1_iv = pos1_uv;
3191 pos1_is_uv = 1;
3192 }
3193 else if (pos1_is_uv ? (UV)pos1_iv > 0 : pos1_iv > 0) {
3194 goto bound_fail; /* $[=3; substr($_,2,...) */
3195 }
3196 else { /* pos < $[ */
3197 if (pos1_iv == 0) { /* $[=1; substr($_,0,...) */
3198 pos1_iv = curlen;
3199 pos1_is_uv = 1;
3200 } else {
3201 if (curlen) {
3202 pos1_is_uv = curlen-1 > ~(UV)pos1_iv;
3203 pos1_iv += curlen;
3204 }
3205 }
3206 }
3207 if (pos1_is_uv || pos1_iv > 0) {
3208 if ((UV)pos1_iv > curlen)
3209 goto bound_fail;
3210 }
3211
3212 if (num_args > 2) {
3213 if (!len_is_uv && len_iv < 0) {
3214 pos2_iv = curlen + len_iv;
3215 if (curlen)
3216 pos2_is_uv = curlen-1 > ~(UV)len_iv;
3217 else
3218 pos2_is_uv = 0;
3219 } else { /* len_iv >= 0 */
3220 if (!pos1_is_uv && pos1_iv < 0) {
3221 pos2_iv = pos1_iv + len_iv;
3222 pos2_is_uv = (UV)len_iv > (UV)IV_MAX;
3223 } else {
3224 if ((UV)len_iv > curlen-(UV)pos1_iv)
3225 pos2_iv = curlen;
3226 else
3227 pos2_iv = pos1_iv+len_iv;
3228 pos2_is_uv = 1;
3229 }
3230 }
3231 }
3232 else {
3233 pos2_iv = curlen;
3234 pos2_is_uv = 1;
3235 }
3236
3237 if (!pos2_is_uv && pos2_iv < 0) {
3238 if (!pos1_is_uv && pos1_iv < 0)
3239 goto bound_fail;
3240 pos2_iv = 0;
3241 }
3242 else if (!pos1_is_uv && pos1_iv < 0)
3243 pos1_iv = 0;
3244
3245 if ((UV)pos2_iv < (UV)pos1_iv)
3246 pos2_iv = pos1_iv;
3247 if ((UV)pos2_iv > curlen)
3248 pos2_iv = curlen;
3249
3250 {
3251 /* pos1_iv and pos2_iv both in 0..curlen, so the cast is safe */
3252 const STRLEN pos = (STRLEN)( (UV)pos1_iv );
3253 const STRLEN len = (STRLEN)( (UV)pos2_iv - (UV)pos1_iv );
3254 STRLEN byte_len = len;
3255 STRLEN byte_pos = utf8_curlen
3256 ? sv_pos_u2b_flags(sv, pos, &byte_len, SV_CONST_RETURN) : pos;
3257
3258 tmps += byte_pos;
3259 /* we either return a PV or an LV. If the TARG hasn't been used
3260 * before, or is of that type, reuse it; otherwise use a mortal
3261 * instead. Note that LVs can have an extended lifetime, so also
3262 * dont reuse if refcount > 1 (bug #20933) */
3263 if (SvTYPE(TARG) > SVt_NULL) {
3264 if ( (SvTYPE(TARG) == SVt_PVLV)
3265 ? (!lvalue || SvREFCNT(TARG) > 1)
3266 : lvalue)
3267 {
3268 TARG = sv_newmortal();
3269 }
3270 }
3271
3272 sv_setpvn(TARG, tmps, byte_len);
3273#ifdef USE_LOCALE_COLLATE
3274 sv_unmagic(TARG, PERL_MAGIC_collxfrm);
3275#endif
3276 if (utf8_curlen)
3277 SvUTF8_on(TARG);
3278 if (repl) {
3279 SV* repl_sv_copy = NULL;
3280
3281 if (repl_need_utf8_upgrade) {
3282 repl_sv_copy = newSVsv(repl_sv);
3283 sv_utf8_upgrade(repl_sv_copy);
3284 repl = SvPV_const(repl_sv_copy, repl_len);
3285 repl_is_utf8 = DO_UTF8(repl_sv_copy) && SvCUR(sv);
3286 }
3287 if (!SvOK(sv))
3288 sv_setpvs(sv, "");
3289 sv_insert_flags(sv, byte_pos, byte_len, repl, repl_len, 0);
3290 if (repl_is_utf8)
3291 SvUTF8_on(sv);
3292 SvREFCNT_dec(repl_sv_copy);
3293 }
3294 else if (lvalue) { /* it's an lvalue! */
3295 if (!SvGMAGICAL(sv)) {
3296 if (SvROK(sv)) {
3297 SvPV_force_nolen(sv);
3298 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR),
3299 "Attempt to use reference as lvalue in substr");
3300 }
3301 if (isGV_with_GP(sv))
3302 SvPV_force_nolen(sv);
3303 else if (SvOK(sv)) /* is it defined ? */
3304 (void)SvPOK_only_UTF8(sv);
3305 else
3306 sv_setpvs(sv, ""); /* avoid lexical reincarnation */
3307 }
3308
3309 if (SvTYPE(TARG) < SVt_PVLV) {
3310 sv_upgrade(TARG, SVt_PVLV);
3311 sv_magic(TARG, NULL, PERL_MAGIC_substr, NULL, 0);
3312 }
3313
3314 LvTYPE(TARG) = 'x';
3315 if (LvTARG(TARG) != sv) {
3316 SvREFCNT_dec(LvTARG(TARG));
3317 LvTARG(TARG) = SvREFCNT_inc_simple(sv);
3318 }
3319 LvTARGOFF(TARG) = pos;
3320 LvTARGLEN(TARG) = len;
3321 }
3322 }
3323 SPAGAIN;
3324 PUSHs(TARG); /* avoid SvSETMAGIC here */
3325 RETURN;
3326
3327bound_fail:
3328 if (lvalue || repl)
3329 Perl_croak(aTHX_ "substr outside of string");
3330 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR), "substr outside of string");
3331 RETPUSHUNDEF;
3332}
3333
3334PP(pp_vec)
3335{
3336 dVAR; dSP; dTARGET;
3337 register const IV size = POPi;
3338 register const IV offset = POPi;
3339 register SV * const src = POPs;
3340 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
3341
3342 SvTAINTED_off(TARG); /* decontaminate */
3343 if (lvalue) { /* it's an lvalue! */
3344 if (SvREFCNT(TARG) > 1) /* don't share the TARG (#20933) */
3345 TARG = sv_newmortal();
3346 if (SvTYPE(TARG) < SVt_PVLV) {
3347 sv_upgrade(TARG, SVt_PVLV);
3348 sv_magic(TARG, NULL, PERL_MAGIC_vec, NULL, 0);
3349 }
3350 LvTYPE(TARG) = 'v';
3351 if (LvTARG(TARG) != src) {
3352 SvREFCNT_dec(LvTARG(TARG));
3353 LvTARG(TARG) = SvREFCNT_inc_simple(src);
3354 }
3355 LvTARGOFF(TARG) = offset;
3356 LvTARGLEN(TARG) = size;
3357 }
3358
3359 sv_setuv(TARG, do_vecget(src, offset, size));
3360 PUSHs(TARG);
3361 RETURN;
3362}
3363
3364PP(pp_index)
3365{
3366 dVAR; dSP; dTARGET;
3367 SV *big;
3368 SV *little;
3369 SV *temp = NULL;
3370 STRLEN biglen;
3371 STRLEN llen = 0;
3372 I32 offset;
3373 I32 retval;
3374 const char *big_p;
3375 const char *little_p;
3376 const I32 arybase = CopARYBASE_get(PL_curcop);
3377 bool big_utf8;
3378 bool little_utf8;
3379 const bool is_index = PL_op->op_type == OP_INDEX;
3380
3381 if (MAXARG >= 3) {
3382 /* arybase is in characters, like offset, so combine prior to the
3383 UTF-8 to bytes calculation. */
3384 offset = POPi - arybase;
3385 }
3386 little = POPs;
3387 big = POPs;
3388 big_p = SvPV_const(big, biglen);
3389 little_p = SvPV_const(little, llen);
3390
3391 big_utf8 = DO_UTF8(big);
3392 little_utf8 = DO_UTF8(little);
3393 if (big_utf8 ^ little_utf8) {
3394 /* One needs to be upgraded. */
3395 if (little_utf8 && !PL_encoding) {
3396 /* Well, maybe instead we might be able to downgrade the small
3397 string? */
3398 char * const pv = (char*)bytes_from_utf8((U8 *)little_p, &llen,
3399 &little_utf8);
3400 if (little_utf8) {
3401 /* If the large string is ISO-8859-1, and it's not possible to
3402 convert the small string to ISO-8859-1, then there is no
3403 way that it could be found anywhere by index. */
3404 retval = -1;
3405 goto fail;
3406 }
3407
3408 /* At this point, pv is a malloc()ed string. So donate it to temp
3409 to ensure it will get free()d */
3410 little = temp = newSV(0);
3411 sv_usepvn(temp, pv, llen);
3412 little_p = SvPVX(little);
3413 } else {
3414 temp = little_utf8
3415 ? newSVpvn(big_p, biglen) : newSVpvn(little_p, llen);
3416
3417 if (PL_encoding) {
3418 sv_recode_to_utf8(temp, PL_encoding);
3419 } else {
3420 sv_utf8_upgrade(temp);
3421 }
3422 if (little_utf8) {
3423 big = temp;
3424 big_utf8 = TRUE;
3425 big_p = SvPV_const(big, biglen);
3426 } else {
3427 little = temp;
3428 little_p = SvPV_const(little, llen);
3429 }
3430 }
3431 }
3432 if (SvGAMAGIC(big)) {
3433 /* Life just becomes a lot easier if I use a temporary here.
3434 Otherwise I need to avoid calls to sv_pos_u2b(), which (dangerously)
3435 will trigger magic and overloading again, as will fbm_instr()
3436 */
3437 big = newSVpvn_flags(big_p, biglen,
3438 SVs_TEMP | (big_utf8 ? SVf_UTF8 : 0));
3439 big_p = SvPVX(big);
3440 }
3441 if (SvGAMAGIC(little) || (is_index && !SvOK(little))) {
3442 /* index && SvOK() is a hack. fbm_instr() calls SvPV_const, which will
3443 warn on undef, and we've already triggered a warning with the
3444 SvPV_const some lines above. We can't remove that, as we need to
3445 call some SvPV to trigger overloading early and find out if the
3446 string is UTF-8.
3447 This is all getting to messy. The API isn't quite clean enough,
3448 because data access has side effects.
3449 */
3450 little = newSVpvn_flags(little_p, llen,
3451 SVs_TEMP | (little_utf8 ? SVf_UTF8 : 0));
3452 little_p = SvPVX(little);
3453 }
3454
3455 if (MAXARG < 3)
3456 offset = is_index ? 0 : biglen;
3457 else {
3458 if (big_utf8 && offset > 0)
3459 sv_pos_u2b(big, &offset, 0);
3460 if (!is_index)
3461 offset += llen;
3462 }
3463 if (offset < 0)
3464 offset = 0;
3465 else if (offset > (I32)biglen)
3466 offset = biglen;
3467 if (!(little_p = is_index
3468 ? fbm_instr((unsigned char*)big_p + offset,
3469 (unsigned char*)big_p + biglen, little, 0)
3470 : rninstr(big_p, big_p + offset,
3471 little_p, little_p + llen)))
3472 retval = -1;
3473 else {
3474 retval = little_p - big_p;
3475 if (retval > 0 && big_utf8)
3476 sv_pos_b2u(big, &retval);
3477 }
3478 SvREFCNT_dec(temp);
3479 fail:
3480 PUSHi(retval + arybase);
3481 RETURN;
3482}
3483
3484PP(pp_sprintf)
3485{
3486 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
3487 if (SvTAINTED(MARK[1]))
3488 TAINT_PROPER("sprintf");
3489 SvTAINTED_off(TARG);
3490 do_sprintf(TARG, SP-MARK, MARK+1);
3491 TAINT_IF(SvTAINTED(TARG));
3492 SP = ORIGMARK;
3493 PUSHTARG;
3494 RETURN;
3495}
3496
3497PP(pp_ord)
3498{
3499 dVAR; dSP; dTARGET;
3500
3501 SV *argsv = POPs;
3502 STRLEN len;
3503 const U8 *s = (U8*)SvPV_const(argsv, len);
3504
3505 if (PL_encoding && SvPOK(argsv) && !DO_UTF8(argsv)) {
3506 SV * const tmpsv = sv_2mortal(newSVsv(argsv));
3507 s = (U8*)sv_recode_to_utf8(tmpsv, PL_encoding);
3508 argsv = tmpsv;
3509 }
3510
3511 XPUSHu(DO_UTF8(argsv) ?
3512 utf8n_to_uvchr(s, UTF8_MAXBYTES, 0, UTF8_ALLOW_ANYUV) :
3513 (UV)(*s & 0xff));
3514
3515 RETURN;
3516}
3517
3518PP(pp_chr)
3519{
3520 dVAR; dSP; dTARGET;
3521 char *tmps;
3522 UV value;
3523
3524 if (((SvIOK_notUV(TOPs) && SvIV(TOPs) < 0)
3525 ||
3526 (SvNOK(TOPs) && SvNV(TOPs) < 0.0))) {
3527 if (IN_BYTES) {
3528 value = POPu; /* chr(-1) eq chr(0xff), etc. */
3529 } else {
3530 (void) POPs; /* Ignore the argument value. */
3531 value = UNICODE_REPLACEMENT;
3532 }
3533 } else {
3534 value = POPu;
3535 }
3536
3537 SvUPGRADE(TARG,SVt_PV);
3538
3539 if (value > 255 && !IN_BYTES) {
3540 SvGROW(TARG, (STRLEN)UNISKIP(value)+1);
3541 tmps = (char*)uvchr_to_utf8_flags((U8*)SvPVX(TARG), value, 0);
3542 SvCUR_set(TARG, tmps - SvPVX_const(TARG));
3543 *tmps = '\0';
3544 (void)SvPOK_only(TARG);
3545 SvUTF8_on(TARG);
3546 XPUSHs(TARG);
3547 RETURN;
3548 }
3549
3550 SvGROW(TARG,2);
3551 SvCUR_set(TARG, 1);
3552 tmps = SvPVX(TARG);
3553 *tmps++ = (char)value;
3554 *tmps = '\0';
3555 (void)SvPOK_only(TARG);
3556
3557 if (PL_encoding && !IN_BYTES) {
3558 sv_recode_to_utf8(TARG, PL_encoding);
3559 tmps = SvPVX(TARG);
3560 if (SvCUR(TARG) == 0 || !is_utf8_string((U8*)tmps, SvCUR(TARG)) ||
3561 UNICODE_IS_REPLACEMENT(utf8_to_uvchr((U8*)tmps, NULL))) {
3562 SvGROW(TARG, 2);
3563 tmps = SvPVX(TARG);
3564 SvCUR_set(TARG, 1);
3565 *tmps++ = (char)value;
3566 *tmps = '\0';
3567 SvUTF8_off(TARG);
3568 }
3569 }
3570
3571 XPUSHs(TARG);
3572 RETURN;
3573}
3574
3575PP(pp_crypt)
3576{
3577#ifdef HAS_CRYPT
3578 dVAR; dSP; dTARGET;
3579 dPOPTOPssrl;
3580 STRLEN len;
3581 const char *tmps = SvPV_const(left, len);
3582
3583 if (DO_UTF8(left)) {
3584 /* If Unicode, try to downgrade.
3585 * If not possible, croak.
3586 * Yes, we made this up. */
3587 SV* const tsv = sv_2mortal(newSVsv(left));
3588
3589 SvUTF8_on(tsv);
3590 sv_utf8_downgrade(tsv, FALSE);
3591 tmps = SvPV_const(tsv, len);
3592 }
3593# ifdef USE_ITHREADS
3594# ifdef HAS_CRYPT_R
3595 if (!PL_reentrant_buffer->_crypt_struct_buffer) {
3596 /* This should be threadsafe because in ithreads there is only
3597 * one thread per interpreter. If this would not be true,
3598 * we would need a mutex to protect this malloc. */
3599 PL_reentrant_buffer->_crypt_struct_buffer =
3600 (struct crypt_data *)safemalloc(sizeof(struct crypt_data));
3601#if defined(__GLIBC__) || defined(__EMX__)
3602 if (PL_reentrant_buffer->_crypt_struct_buffer) {
3603 PL_reentrant_buffer->_crypt_struct_buffer->initialized = 0;
3604 /* work around glibc-2.2.5 bug */
3605 PL_reentrant_buffer->_crypt_struct_buffer->current_saltbits = 0;
3606 }
3607#endif
3608 }
3609# endif /* HAS_CRYPT_R */
3610# endif /* USE_ITHREADS */
3611# ifdef FCRYPT
3612 sv_setpv(TARG, fcrypt(tmps, SvPV_nolen_const(right)));
3613# else
3614 sv_setpv(TARG, PerlProc_crypt(tmps, SvPV_nolen_const(right)));
3615# endif
3616 SETTARG;
3617 RETURN;
3618#else
3619 DIE(aTHX_
3620 "The crypt() function is unimplemented due to excessive paranoia.");
3621#endif
3622}
3623
3624/* Generally UTF-8 and UTF-EBCDIC are indistinguishable at this level. So
3625 * most comments below say UTF-8, when in fact they mean UTF-EBCDIC as well */
3626
3627/* Both the characters below can be stored in two UTF-8 bytes. In UTF-8 the max
3628 * character that 2 bytes can hold is U+07FF, and in UTF-EBCDIC it is U+03FF.
3629 * See http://www.unicode.org/unicode/reports/tr16 */
3630#define LATIN_CAPITAL_LETTER_Y_WITH_DIAERESIS 0x0178 /* Also is title case */
3631#define GREEK_CAPITAL_LETTER_MU 0x039C /* Upper and title case of MICRON */
3632
3633/* Below are several macros that generate code */
3634/* Generates code to store a unicode codepoint c that is known to occupy
3635 * exactly two UTF-8 and UTF-EBCDIC bytes; it is stored into p and p+1. */
3636#define STORE_UNI_TO_UTF8_TWO_BYTE(p, c) \
3637 STMT_START { \
3638 *(p) = UTF8_TWO_BYTE_HI(c); \
3639 *((p)+1) = UTF8_TWO_BYTE_LO(c); \
3640 } STMT_END
3641
3642/* Like STORE_UNI_TO_UTF8_TWO_BYTE, but advances p to point to the next
3643 * available byte after the two bytes */
3644#define CAT_UNI_TO_UTF8_TWO_BYTE(p, c) \
3645 STMT_START { \
3646 *(p)++ = UTF8_TWO_BYTE_HI(c); \
3647 *((p)++) = UTF8_TWO_BYTE_LO(c); \
3648 } STMT_END
3649
3650/* Generates code to store the upper case of latin1 character l which is known
3651 * to have its upper case be non-latin1 into the two bytes p and p+1. There
3652 * are only two characters that fit this description, and this macro knows
3653 * about them, and that the upper case values fit into two UTF-8 or UTF-EBCDIC
3654 * bytes */
3655#define STORE_NON_LATIN1_UC(p, l) \
3656STMT_START { \
3657 if ((l) == LATIN_SMALL_LETTER_Y_WITH_DIAERESIS) { \
3658 STORE_UNI_TO_UTF8_TWO_BYTE((p), LATIN_CAPITAL_LETTER_Y_WITH_DIAERESIS); \
3659 } else { /* Must be the following letter */ \
3660 STORE_UNI_TO_UTF8_TWO_BYTE((p), GREEK_CAPITAL_LETTER_MU); \
3661 } \
3662} STMT_END
3663
3664/* Like STORE_NON_LATIN1_UC, but advances p to point to the next available byte
3665 * after the character stored */
3666#define CAT_NON_LATIN1_UC(p, l) \
3667STMT_START { \
3668 if ((l) == LATIN_SMALL_LETTER_Y_WITH_DIAERESIS) { \
3669 CAT_UNI_TO_UTF8_TWO_BYTE((p), LATIN_CAPITAL_LETTER_Y_WITH_DIAERESIS); \
3670 } else { \
3671 CAT_UNI_TO_UTF8_TWO_BYTE((p), GREEK_CAPITAL_LETTER_MU); \
3672 } \
3673} STMT_END
3674
3675/* Generates code to add the two UTF-8 bytes (probably u) that are the upper
3676 * case of l into p and p+1. u must be the result of toUPPER_LATIN1_MOD(l),
3677 * and must require two bytes to store it. Advances p to point to the next
3678 * available position */
3679#define CAT_TWO_BYTE_UNI_UPPER_MOD(p, l, u) \
3680STMT_START { \
3681 if ((u) != LATIN_SMALL_LETTER_Y_WITH_DIAERESIS) { \
3682 CAT_UNI_TO_UTF8_TWO_BYTE((p), (u)); /* not special, just save it */ \
3683 } else if (l == LATIN_SMALL_LETTER_SHARP_S) { \
3684 *(p)++ = 'S'; *(p)++ = 'S'; /* upper case is 'SS' */ \
3685 } else {/* else is one of the other two special cases */ \
3686 CAT_NON_LATIN1_UC((p), (l)); \
3687 } \
3688} STMT_END
3689
3690PP(pp_ucfirst)
3691{
3692 /* Actually is both lcfirst() and ucfirst(). Only the first character
3693 * changes. This means that possibly we can change in-place, ie., just
3694 * take the source and change that one character and store it back, but not
3695 * if read-only etc, or if the length changes */
3696
3697 dVAR;
3698 dSP;
3699 SV *source = TOPs;
3700 STRLEN slen; /* slen is the byte length of the whole SV. */
3701 STRLEN need;
3702 SV *dest;
3703 bool inplace; /* ? Convert first char only, in-place */
3704 bool doing_utf8 = FALSE; /* ? using utf8 */
3705 bool convert_source_to_utf8 = FALSE; /* ? need to convert */
3706 const int op_type = PL_op->op_type;
3707 const U8 *s;
3708 U8 *d;
3709 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
3710 STRLEN ulen; /* ulen is the byte length of the original Unicode character
3711 * stored as UTF-8 at s. */
3712 STRLEN tculen; /* tculen is the byte length of the freshly titlecased (or
3713 * lowercased) character stored in tmpbuf. May be either
3714 * UTF-8 or not, but in either case is the number of bytes */
3715
3716 SvGETMAGIC(source);
3717 if (SvOK(source)) {
3718 s = (const U8*)SvPV_nomg_const(source, slen);
3719 } else {
3720 if (ckWARN(WARN_UNINITIALIZED))
3721 report_uninit(source);
3722 s = (const U8*)"";
3723 slen = 0;
3724 }
3725
3726 /* We may be able to get away with changing only the first character, in
3727 * place, but not if read-only, etc. Later we may discover more reasons to
3728 * not convert in-place. */
3729 inplace = SvPADTMP(source) && !SvREADONLY(source) && SvTEMP(source);
3730
3731 /* First calculate what the changed first character should be. This affects
3732 * whether we can just swap it out, leaving the rest of the string unchanged,
3733 * or even if have to convert the dest to UTF-8 when the source isn't */
3734
3735 if (! slen) { /* If empty */
3736 need = 1; /* still need a trailing NUL */
3737 }
3738 else if (DO_UTF8(source)) { /* Is the source utf8? */
3739 doing_utf8 = TRUE;
3740
3741/* TODO: This is #ifdefd out because it has hard-coded the standard mappings,
3742 * and doesn't allow for the user to specify their own. When code is added to
3743 * detect if there is a user-defined mapping in force here, and if so to use
3744 * that, then the code below can be compiled. The detection would be a good
3745 * thing anyway, as currently the user-defined mappings only work on utf8
3746 * strings, and thus depend on the chosen internal storage method, which is a
3747 * bad thing */
3748#ifdef GO_AHEAD_AND_BREAK_USER_DEFINED_CASE_MAPPINGS
3749 if (UTF8_IS_INVARIANT(*s)) {
3750
3751 /* An invariant source character is either ASCII or, in EBCDIC, an
3752 * ASCII equivalent or a caseless C1 control. In both these cases,
3753 * the lower and upper cases of any character are also invariants
3754 * (and title case is the same as upper case). So it is safe to
3755 * use the simple case change macros which avoid the overhead of
3756 * the general functions. Note that if perl were to be extended to
3757 * do locale handling in UTF-8 strings, this wouldn't be true in,
3758 * for example, Lithuanian or Turkic. */
3759 *tmpbuf = (op_type == OP_LCFIRST) ? toLOWER(*s) : toUPPER(*s);
3760 tculen = ulen = 1;
3761 need = slen + 1;
3762 }
3763 else if (UTF8_IS_DOWNGRADEABLE_START(*s)) {
3764 U8 chr;
3765
3766 /* Similarly, if the source character isn't invariant but is in the
3767 * latin1 range (or EBCDIC equivalent thereof), we have the case
3768 * changes compiled into perl, and can avoid the overhead of the
3769 * general functions. In this range, the characters are stored as
3770 * two UTF-8 bytes, and it so happens that any changed-case version
3771 * is also two bytes (in both ASCIIish and EBCDIC machines). */
3772 tculen = ulen = 2;
3773 need = slen + 1;
3774
3775 /* Convert the two source bytes to a single Unicode code point
3776 * value, change case and save for below */
3777 chr = UTF8_ACCUMULATE(*s, *(s+1));
3778 if (op_type == OP_LCFIRST) { /* lower casing is easy */
3779 U8 lower = toLOWER_LATIN1(chr);
3780 STORE_UNI_TO_UTF8_TWO_BYTE(tmpbuf, lower);
3781 }
3782 else { /* ucfirst */
3783 U8 upper = toUPPER_LATIN1_MOD(chr);
3784
3785 /* Most of the latin1 range characters are well-behaved. Their
3786 * title and upper cases are the same, and are also in the
3787 * latin1 range. The macro above returns their upper (hence
3788 * title) case, and all that need be done is to save the result
3789 * for below. However, several characters are problematic, and
3790 * have to be handled specially. The MOD in the macro name
3791 * above means that these tricky characters all get mapped to
3792 * the single character LATIN_SMALL_LETTER_Y_WITH_DIAERESIS.
3793 * This mapping saves some tests for the majority of the
3794 * characters */
3795
3796 if (upper != LATIN_SMALL_LETTER_Y_WITH_DIAERESIS) {
3797
3798 /* Not tricky. Just save it. */
3799 STORE_UNI_TO_UTF8_TWO_BYTE(tmpbuf, upper);
3800 }
3801 else if (chr == LATIN_SMALL_LETTER_SHARP_S) {
3802
3803 /* This one is tricky because it is two characters long,
3804 * though the UTF-8 is still two bytes, so the stored
3805 * length doesn't change */
3806 *tmpbuf = 'S'; /* The UTF-8 is 'Ss' */
3807 *(tmpbuf + 1) = 's';
3808 }
3809 else {
3810
3811 /* The other two have their title and upper cases the same,
3812 * but are tricky because the changed-case characters
3813 * aren't in the latin1 range. They, however, do fit into
3814 * two UTF-8 bytes */
3815 STORE_NON_LATIN1_UC(tmpbuf, chr);
3816 }
3817 }
3818 }
3819 else {
3820#endif /* end of dont want to break user-defined casing */
3821
3822 /* Here, can't short-cut the general case */
3823
3824 utf8_to_uvchr(s, &ulen);
3825 if (op_type == OP_UCFIRST) toTITLE_utf8(s, tmpbuf, &tculen);
3826 else toLOWER_utf8(s, tmpbuf, &tculen);
3827
3828 /* we can't do in-place if the length changes. */
3829 if (ulen != tculen) inplace = FALSE;
3830 need = slen + 1 - ulen + tculen;
3831#ifdef GO_AHEAD_AND_BREAK_USER_DEFINED_CASE_MAPPINGS
3832 }
3833#endif
3834 }
3835 else { /* Non-zero length, non-UTF-8, Need to consider locale and if
3836 * latin1 is treated as caseless. Note that a locale takes
3837 * precedence */
3838 tculen = 1; /* Most characters will require one byte, but this will
3839 * need to be overridden for the tricky ones */
3840 need = slen + 1;
3841
3842 if (op_type == OP_LCFIRST) {
3843
3844 /* lower case the first letter: no trickiness for any character */
3845 *tmpbuf = (IN_LOCALE_RUNTIME) ? toLOWER_LC(*s) :
3846 ((IN_UNI_8_BIT) ? toLOWER_LATIN1(*s) : toLOWER(*s));
3847 }
3848 /* is ucfirst() */
3849 else if (IN_LOCALE_RUNTIME) {
3850 *tmpbuf = toUPPER_LC(*s); /* This would be a bug if any locales
3851 * have upper and title case different
3852 */
3853 }
3854 else if (! IN_UNI_8_BIT) {
3855 *tmpbuf = toUPPER(*s); /* Returns caseless for non-ascii, or
3856 * on EBCDIC machines whatever the
3857 * native function does */
3858 }
3859 else { /* is ucfirst non-UTF-8, not in locale, and cased latin1 */
3860 *tmpbuf = toUPPER_LATIN1_MOD(*s);
3861
3862 /* tmpbuf now has the correct title case for all latin1 characters
3863 * except for the several ones that have tricky handling. All
3864 * of these are mapped by the MOD to the letter below. */
3865 if (*tmpbuf == LATIN_SMALL_LETTER_Y_WITH_DIAERESIS) {
3866
3867 /* The length is going to change, with all three of these, so
3868 * can't replace just the first character */
3869 inplace = FALSE;
3870
3871 /* We use the original to distinguish between these tricky
3872 * cases */
3873 if (*s == LATIN_SMALL_LETTER_SHARP_S) {
3874 /* Two character title case 'Ss', but can remain non-UTF-8 */
3875 need = slen + 2;
3876 *tmpbuf = 'S';
3877 *(tmpbuf + 1) = 's'; /* Assert: length(tmpbuf) >= 2 */
3878 tculen = 2;
3879 }
3880 else {
3881
3882 /* The other two tricky ones have their title case outside
3883 * latin1. It is the same as their upper case. */
3884 doing_utf8 = TRUE;
3885 STORE_NON_LATIN1_UC(tmpbuf, *s);
3886
3887 /* The UTF-8 and UTF-EBCDIC lengths of both these characters
3888 * and their upper cases is 2. */
3889 tculen = ulen = 2;
3890
3891 /* The entire result will have to be in UTF-8. Assume worst
3892 * case sizing in conversion. (all latin1 characters occupy
3893 * at most two bytes in utf8) */
3894 convert_source_to_utf8 = TRUE;
3895 need = slen * 2 + 1;
3896 }
3897 } /* End of is one of the three special chars */
3898 } /* End of use Unicode (Latin1) semantics */
3899 } /* End of changing the case of the first character */
3900
3901 /* Here, have the first character's changed case stored in tmpbuf. Ready to
3902 * generate the result */
3903 if (inplace) {
3904
3905 /* We can convert in place. This means we change just the first
3906 * character without disturbing the rest; no need to grow */
3907 dest = source;
3908 s = d = (U8*)SvPV_force_nomg(source, slen);
3909 } else {
3910 dTARGET;
3911
3912 dest = TARG;
3913
3914 /* Here, we can't convert in place; we earlier calculated how much
3915 * space we will need, so grow to accommodate that */
3916 SvUPGRADE(dest, SVt_PV);
3917 d = (U8*)SvGROW(dest, need);
3918 (void)SvPOK_only(dest);
3919
3920 SETs(dest);
3921 }
3922
3923 if (doing_utf8) {
3924 if (! inplace) {
3925 if (! convert_source_to_utf8) {
3926
3927 /* Here both source and dest are in UTF-8, but have to create
3928 * the entire output. We initialize the result to be the
3929 * title/lower cased first character, and then append the rest
3930 * of the string. */
3931 sv_setpvn(dest, (char*)tmpbuf, tculen);
3932 if (slen > ulen) {
3933 sv_catpvn(dest, (char*)(s + ulen), slen - ulen);
3934 }
3935 }
3936 else {
3937 const U8 *const send = s + slen;
3938
3939 /* Here the dest needs to be in UTF-8, but the source isn't,
3940 * except we earlier UTF-8'd the first character of the source
3941 * into tmpbuf. First put that into dest, and then append the
3942 * rest of the source, converting it to UTF-8 as we go. */
3943
3944 /* Assert tculen is 2 here because the only two characters that
3945 * get to this part of the code have 2-byte UTF-8 equivalents */
3946 *d++ = *tmpbuf;
3947 *d++ = *(tmpbuf + 1);
3948 s++; /* We have just processed the 1st char */
3949
3950 for (; s < send; s++) {
3951 d = uvchr_to_utf8(d, *s);
3952 }
3953 *d = '\0';
3954 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3955 }
3956 SvUTF8_on(dest);
3957 }
3958 else { /* in-place UTF-8. Just overwrite the first character */
3959 Copy(tmpbuf, d, tculen, U8);
3960 SvCUR_set(dest, need - 1);
3961 }
3962 }
3963 else { /* Neither source nor dest are in or need to be UTF-8 */
3964 if (slen) {
3965 if (IN_LOCALE_RUNTIME) {
3966 TAINT;
3967 SvTAINTED_on(dest);
3968 }
3969 if (inplace) { /* in-place, only need to change the 1st char */
3970 *d = *tmpbuf;
3971 }
3972 else { /* Not in-place */
3973
3974 /* Copy the case-changed character(s) from tmpbuf */
3975 Copy(tmpbuf, d, tculen, U8);
3976 d += tculen - 1; /* Code below expects d to point to final
3977 * character stored */
3978 }
3979 }
3980 else { /* empty source */
3981 /* See bug #39028: Don't taint if empty */
3982 *d = *s;
3983 }
3984
3985 /* In a "use bytes" we don't treat the source as UTF-8, but, still want
3986 * the destination to retain that flag */
3987 if (SvUTF8(source))
3988 SvUTF8_on(dest);
3989
3990 if (!inplace) { /* Finish the rest of the string, unchanged */
3991 /* This will copy the trailing NUL */
3992 Copy(s + 1, d + 1, slen, U8);
3993 SvCUR_set(dest, need - 1);
3994 }
3995 }
3996 SvSETMAGIC(dest);
3997 RETURN;
3998}
3999
4000/* There's so much setup/teardown code common between uc and lc, I wonder if
4001 it would be worth merging the two, and just having a switch outside each
4002 of the three tight loops. There is less and less commonality though */
4003PP(pp_uc)
4004{
4005 dVAR;
4006 dSP;
4007 SV *source = TOPs;
4008 STRLEN len;
4009 STRLEN min;
4010 SV *dest;
4011 const U8 *s;
4012 U8 *d;
4013
4014 SvGETMAGIC(source);
4015
4016 if (SvPADTMP(source) && !SvREADONLY(source) && !SvAMAGIC(source)
4017 && SvTEMP(source) && !DO_UTF8(source)
4018 && (IN_LOCALE_RUNTIME || ! IN_UNI_8_BIT)) {
4019
4020 /* We can convert in place. The reason we can't if in UNI_8_BIT is to
4021 * make the loop tight, so we overwrite the source with the dest before
4022 * looking at it, and we need to look at the original source
4023 * afterwards. There would also need to be code added to handle
4024 * switching to not in-place in midstream if we run into characters
4025 * that change the length.
4026 */
4027 dest = source;
4028 s = d = (U8*)SvPV_force_nomg(source, len);
4029 min = len + 1;
4030 } else {
4031 dTARGET;
4032
4033 dest = TARG;
4034
4035 /* The old implementation would copy source into TARG at this point.
4036 This had the side effect that if source was undef, TARG was now
4037 an undefined SV with PADTMP set, and they don't warn inside
4038 sv_2pv_flags(). However, we're now getting the PV direct from
4039 source, which doesn't have PADTMP set, so it would warn. Hence the
4040 little games. */
4041
4042 if (SvOK(source)) {
4043 s = (const U8*)SvPV_nomg_const(source, len);
4044 } else {
4045 if (ckWARN(WARN_UNINITIALIZED))
4046 report_uninit(source);
4047 s = (const U8*)"";
4048 len = 0;
4049 }
4050 min = len + 1;
4051
4052 SvUPGRADE(dest, SVt_PV);
4053 d = (U8*)SvGROW(dest, min);
4054 (void)SvPOK_only(dest);
4055
4056 SETs(dest);
4057 }
4058
4059 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
4060 to check DO_UTF8 again here. */
4061
4062 if (DO_UTF8(source)) {
4063 const U8 *const send = s + len;
4064 U8 tmpbuf[UTF8_MAXBYTES+1];
4065
4066 /* All occurrences of these are to be moved to follow any other marks.
4067 * This is context-dependent. We may not be passed enough context to
4068 * move the iota subscript beyond all of them, but we do the best we can
4069 * with what we're given. The result is always better than if we
4070 * hadn't done this. And, the problem would only arise if we are
4071 * passed a character without all its combining marks, which would be
4072 * the caller's mistake. The information this is based on comes from a
4073 * comment in Unicode SpecialCasing.txt, (and the Standard's text
4074 * itself) and so can't be checked properly to see if it ever gets
4075 * revised. But the likelihood of it changing is remote */
4076 bool in_iota_subscript = FALSE;
4077
4078 while (s < send) {
4079 if (in_iota_subscript && ! is_utf8_mark(s)) {
4080 /* A non-mark. Time to output the iota subscript */
4081#define GREEK_CAPITAL_LETTER_IOTA 0x0399
4082#define COMBINING_GREEK_YPOGEGRAMMENI 0x0345
4083
4084 CAT_UNI_TO_UTF8_TWO_BYTE(d, GREEK_CAPITAL_LETTER_IOTA);
4085 in_iota_subscript = FALSE;
4086 }
4087
4088
4089/* See comments at the first instance in this file of this ifdef */
4090#ifdef GO_AHEAD_AND_BREAK_USER_DEFINED_CASE_MAPPINGS
4091
4092 /* If the UTF-8 character is invariant, then it is in the range
4093 * known by the standard macro; result is only one byte long */
4094 if (UTF8_IS_INVARIANT(*s)) {
4095 *d++ = toUPPER(*s);
4096 s++;
4097 }
4098 else if (UTF8_IS_DOWNGRADEABLE_START(*s)) {
4099
4100 /* Likewise, if it fits in a byte, its case change is in our
4101 * table */
4102 U8 orig = UTF8_ACCUMULATE(*s, *(s+1));
4103 U8 upper = toUPPER_LATIN1_MOD(orig);
4104 CAT_TWO_BYTE_UNI_UPPER_MOD(d, orig, upper);
4105 s += 2;
4106 }
4107 else {
4108#else
4109 {
4110#endif
4111
4112 /* Otherwise, need the general UTF-8 case. Get the changed
4113 * case value and copy it to the output buffer */
4114
4115 const STRLEN u = UTF8SKIP(s);
4116 STRLEN ulen;
4117
4118 const UV uv = toUPPER_utf8(s, tmpbuf, &ulen);
4119 if (uv == GREEK_CAPITAL_LETTER_IOTA
4120 && utf8_to_uvchr(s, 0) == COMBINING_GREEK_YPOGEGRAMMENI)
4121 {
4122 in_iota_subscript = TRUE;
4123 }
4124 else {
4125 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
4126 /* If the eventually required minimum size outgrows
4127 * the available space, we need to grow. */
4128 const UV o = d - (U8*)SvPVX_const(dest);
4129
4130 /* If someone uppercases one million U+03B0s we
4131 * SvGROW() one million times. Or we could try
4132 * guessing how much to allocate without allocating too
4133 * much. Such is life. See corresponding comment in
4134 * lc code for another option */
4135 SvGROW(dest, min);
4136 d = (U8*)SvPVX(dest) + o;
4137 }
4138 Copy(tmpbuf, d, ulen, U8);
4139 d += ulen;
4140 }
4141 s += u;
4142 }
4143 }
4144 if (in_iota_subscript) {
4145 CAT_UNI_TO_UTF8_TWO_BYTE(d, GREEK_CAPITAL_LETTER_IOTA);
4146 }
4147 SvUTF8_on(dest);
4148 *d = '\0';
4149 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4150 }
4151 else { /* Not UTF-8 */
4152 if (len) {
4153 const U8 *const send = s + len;
4154
4155 /* Use locale casing if in locale; regular style if not treating
4156 * latin1 as having case; otherwise the latin1 casing. Do the
4157 * whole thing in a tight loop, for speed, */
4158 if (IN_LOCALE_RUNTIME) {
4159 TAINT;
4160 SvTAINTED_on(dest);
4161 for (; s < send; d++, s++)
4162 *d = toUPPER_LC(*s);
4163 }
4164 else if (! IN_UNI_8_BIT) {
4165 for (; s < send; d++, s++) {
4166 *d = toUPPER(*s);
4167 }
4168 }
4169 else {
4170 for (; s < send; d++, s++) {
4171 *d = toUPPER_LATIN1_MOD(*s);
4172 if (*d != LATIN_SMALL_LETTER_Y_WITH_DIAERESIS) continue;
4173
4174 /* The mainstream case is the tight loop above. To avoid
4175 * extra tests in that, all three characters that require
4176 * special handling are mapped by the MOD to the one tested
4177 * just above.
4178 * Use the source to distinguish between the three cases */
4179
4180 if (*s == LATIN_SMALL_LETTER_SHARP_S) {
4181
4182 /* uc() of this requires 2 characters, but they are
4183 * ASCII. If not enough room, grow the string */
4184 if (SvLEN(dest) < ++min) {
4185 const UV o = d - (U8*)SvPVX_const(dest);
4186 SvGROW(dest, min);
4187 d = (U8*)SvPVX(dest) + o;
4188 }
4189 *d++ = 'S'; *d = 'S'; /* upper case is 'SS' */
4190 continue; /* Back to the tight loop; still in ASCII */
4191 }
4192
4193 /* The other two special handling characters have their
4194 * upper cases outside the latin1 range, hence need to be
4195 * in UTF-8, so the whole result needs to be in UTF-8. So,
4196 * here we are somewhere in the middle of processing a
4197 * non-UTF-8 string, and realize that we will have to convert
4198 * the whole thing to UTF-8. What to do? There are
4199 * several possibilities. The simplest to code is to
4200 * convert what we have so far, set a flag, and continue on
4201 * in the loop. The flag would be tested each time through
4202 * the loop, and if set, the next character would be
4203 * converted to UTF-8 and stored. But, I (khw) didn't want
4204 * to slow down the mainstream case at all for this fairly
4205 * rare case, so I didn't want to add a test that didn't
4206 * absolutely have to be there in the loop, besides the
4207 * possibility that it would get too complicated for
4208 * optimizers to deal with. Another possibility is to just
4209 * give up, convert the source to UTF-8, and restart the
4210 * function that way. Another possibility is to convert
4211 * both what has already been processed and what is yet to
4212 * come separately to UTF-8, then jump into the loop that
4213 * handles UTF-8. But the most efficient time-wise of the
4214 * ones I could think of is what follows, and turned out to
4215 * not require much extra code. */
4216
4217 /* Convert what we have so far into UTF-8, telling the
4218 * function that we know it should be converted, and to
4219 * allow extra space for what we haven't processed yet.
4220 * Assume the worst case space requirements for converting
4221 * what we haven't processed so far: that it will require
4222 * two bytes for each remaining source character, plus the
4223 * NUL at the end. This may cause the string pointer to
4224 * move, so re-find it. */
4225
4226 len = d - (U8*)SvPVX_const(dest);
4227 SvCUR_set(dest, len);
4228 len = sv_utf8_upgrade_flags_grow(dest,
4229 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
4230 (send -s) * 2 + 1);
4231 d = (U8*)SvPVX(dest) + len;
4232
4233 /* And append the current character's upper case in UTF-8 */
4234 CAT_NON_LATIN1_UC(d, *s);
4235
4236 /* Now process the remainder of the source, converting to
4237 * upper and UTF-8. If a resulting byte is invariant in
4238 * UTF-8, output it as-is, otherwise convert to UTF-8 and
4239 * append it to the output. */
4240
4241 s++;
4242 for (; s < send; s++) {
4243 U8 upper = toUPPER_LATIN1_MOD(*s);
4244 if UTF8_IS_INVARIANT(upper) {
4245 *d++ = upper;
4246 }
4247 else {
4248 CAT_TWO_BYTE_UNI_UPPER_MOD(d, *s, upper);
4249 }
4250 }
4251
4252 /* Here have processed the whole source; no need to continue
4253 * with the outer loop. Each character has been converted
4254 * to upper case and converted to UTF-8 */
4255
4256 break;
4257 } /* End of processing all latin1-style chars */
4258 } /* End of processing all chars */
4259 } /* End of source is not empty */
4260
4261 if (source != dest) {
4262 *d = '\0'; /* Here d points to 1 after last char, add NUL */
4263 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4264 }
4265 } /* End of isn't utf8 */
4266 SvSETMAGIC(dest);
4267 RETURN;
4268}
4269
4270PP(pp_lc)
4271{
4272 dVAR;
4273 dSP;
4274 SV *source = TOPs;
4275 STRLEN len;
4276 STRLEN min;
4277 SV *dest;
4278 const U8 *s;
4279 U8 *d;
4280
4281 SvGETMAGIC(source);
4282
4283 if (SvPADTMP(source) && !SvREADONLY(source) && !SvAMAGIC(source)
4284 && SvTEMP(source) && !DO_UTF8(source)) {
4285
4286 /* We can convert in place, as lowercasing anything in the latin1 range
4287 * (or else DO_UTF8 would have been on) doesn't lengthen it */
4288 dest = source;
4289 s = d = (U8*)SvPV_force_nomg(source, len);
4290 min = len + 1;
4291 } else {
4292 dTARGET;
4293
4294 dest = TARG;
4295
4296 /* The old implementation would copy source into TARG at this point.
4297 This had the side effect that if source was undef, TARG was now
4298 an undefined SV with PADTMP set, and they don't warn inside
4299 sv_2pv_flags(). However, we're now getting the PV direct from
4300 source, which doesn't have PADTMP set, so it would warn. Hence the
4301 little games. */
4302
4303 if (SvOK(source)) {
4304 s = (const U8*)SvPV_nomg_const(source, len);
4305 } else {
4306 if (ckWARN(WARN_UNINITIALIZED))
4307 report_uninit(source);
4308 s = (const U8*)"";
4309 len = 0;
4310 }
4311 min = len + 1;
4312
4313 SvUPGRADE(dest, SVt_PV);
4314 d = (U8*)SvGROW(dest, min);
4315 (void)SvPOK_only(dest);
4316
4317 SETs(dest);
4318 }
4319
4320 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
4321 to check DO_UTF8 again here. */
4322
4323 if (DO_UTF8(source)) {
4324 const U8 *const send = s + len;
4325 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
4326
4327 while (s < send) {
4328/* See comments at the first instance in this file of this ifdef */
4329#ifdef GO_AHEAD_AND_BREAK_USER_DEFINED_CASE_MAPPINGS
4330 if (UTF8_IS_INVARIANT(*s)) {
4331
4332 /* Invariant characters use the standard mappings compiled in.
4333 */
4334 *d++ = toLOWER(*s);
4335 s++;
4336 }
4337 else if (UTF8_IS_DOWNGRADEABLE_START(*s)) {
4338
4339 /* As do the ones in the Latin1 range */
4340 U8 lower = toLOWER_LATIN1(UTF8_ACCUMULATE(*s, *(s+1)));
4341 CAT_UNI_TO_UTF8_TWO_BYTE(d, lower);
4342 s += 2;
4343 }
4344 else {
4345#endif
4346 /* Here, is utf8 not in Latin-1 range, have to go out and get
4347 * the mappings from the tables. */
4348
4349 const STRLEN u = UTF8SKIP(s);
4350 STRLEN ulen;
4351
4352#ifndef CONTEXT_DEPENDENT_CASING
4353 toLOWER_utf8(s, tmpbuf, &ulen);
4354#else
4355/* This is ifdefd out because it needs more work and thought. It isn't clear
4356 * that we should do it.
4357 * A minor objection is that this is based on a hard-coded rule from the
4358 * Unicode standard, and may change, but this is not very likely at all.
4359 * mktables should check and warn if it does.
4360 * More importantly, if the sigma occurs at the end of the string, we don't
4361 * have enough context to know whether it is part of a larger string or going
4362 * to be or not. It may be that we are passed a subset of the context, via
4363 * a \U...\E, for example, and we could conceivably know the larger context if
4364 * code were changed to pass that in. But, if the string passed in is an
4365 * intermediate result, and the user concatenates two strings together
4366 * after we have made a final sigma, that would be wrong. If the final sigma
4367 * occurs in the middle of the string we are working on, then we know that it
4368 * should be a final sigma, but otherwise we can't be sure. */
4369
4370 const UV uv = toLOWER_utf8(s, tmpbuf, &ulen);
4371
4372 /* If the lower case is a small sigma, it may be that we need
4373 * to change it to a final sigma. This happens at the end of
4374 * a word that contains more than just this character, and only
4375 * when we started with a capital sigma. */
4376 if (uv == UNICODE_GREEK_SMALL_LETTER_SIGMA &&
4377 s > send - len && /* Makes sure not the first letter */
4378 utf8_to_uvchr(s, 0) == UNICODE_GREEK_CAPITAL_LETTER_SIGMA
4379 ) {
4380
4381 /* We use the algorithm in:
4382 * http://www.unicode.org/versions/Unicode5.0.0/ch03.pdf (C
4383 * is a CAPITAL SIGMA): If C is preceded by a sequence
4384 * consisting of a cased letter and a case-ignorable
4385 * sequence, and C is not followed by a sequence consisting
4386 * of a case ignorable sequence and then a cased letter,
4387 * then when lowercasing C, C becomes a final sigma */
4388
4389 /* To determine if this is the end of a word, need to peek
4390 * ahead. Look at the next character */
4391 const U8 *peek = s + u;
4392
4393 /* Skip any case ignorable characters */
4394 while (peek < send && is_utf8_case_ignorable(peek)) {
4395 peek += UTF8SKIP(peek);
4396 }
4397
4398 /* If we reached the end of the string without finding any
4399 * non-case ignorable characters, or if the next such one
4400 * is not-cased, then we have met the conditions for it
4401 * being a final sigma with regards to peek ahead, and so
4402 * must do peek behind for the remaining conditions. (We
4403 * know there is stuff behind to look at since we tested
4404 * above that this isn't the first letter) */
4405 if (peek >= send || ! is_utf8_cased(peek)) {
4406 peek = utf8_hop(s, -1);
4407
4408 /* Here are at the beginning of the first character
4409 * before the original upper case sigma. Keep backing
4410 * up, skipping any case ignorable characters */
4411 while (is_utf8_case_ignorable(peek)) {
4412 peek = utf8_hop(peek, -1);
4413 }
4414
4415 /* Here peek points to the first byte of the closest
4416 * non-case-ignorable character before the capital
4417 * sigma. If it is cased, then by the Unicode
4418 * algorithm, we should use a small final sigma instead
4419 * of what we have */
4420 if (is_utf8_cased(peek)) {
4421 STORE_UNI_TO_UTF8_TWO_BYTE(tmpbuf,
4422 UNICODE_GREEK_SMALL_LETTER_FINAL_SIGMA);
4423 }
4424 }
4425 }
4426 else { /* Not a context sensitive mapping */
4427#endif /* End of commented out context sensitive */
4428 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
4429
4430 /* If the eventually required minimum size outgrows
4431 * the available space, we need to grow. */
4432 const UV o = d - (U8*)SvPVX_const(dest);
4433
4434 /* If someone lowercases one million U+0130s we
4435 * SvGROW() one million times. Or we could try
4436 * guessing how much to allocate without allocating too
4437 * much. Such is life. Another option would be to
4438 * grow an extra byte or two more each time we need to
4439 * grow, which would cut down the million to 500K, with
4440 * little waste */
4441 SvGROW(dest, min);
4442 d = (U8*)SvPVX(dest) + o;
4443 }
4444#ifdef CONTEXT_DEPENDENT_CASING
4445 }
4446#endif
4447 /* Copy the newly lowercased letter to the output buffer we're
4448 * building */
4449 Copy(tmpbuf, d, ulen, U8);
4450 d += ulen;
4451 s += u;
4452#ifdef GO_AHEAD_AND_BREAK_USER_DEFINED_CASE_MAPPINGS
4453 }
4454#endif
4455 } /* End of looping through the source string */
4456 SvUTF8_on(dest);
4457 *d = '\0';
4458 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4459 } else { /* Not utf8 */
4460 if (len) {
4461 const U8 *const send = s + len;
4462
4463 /* Use locale casing if in locale; regular style if not treating
4464 * latin1 as having case; otherwise the latin1 casing. Do the
4465 * whole thing in a tight loop, for speed, */
4466 if (IN_LOCALE_RUNTIME) {
4467 TAINT;
4468 SvTAINTED_on(dest);
4469 for (; s < send; d++, s++)
4470 *d = toLOWER_LC(*s);
4471 }
4472 else if (! IN_UNI_8_BIT) {
4473 for (; s < send; d++, s++) {
4474 *d = toLOWER(*s);
4475 }
4476 }
4477 else {
4478 for (; s < send; d++, s++) {
4479 *d = toLOWER_LATIN1(*s);
4480 }
4481 }
4482 }
4483 if (source != dest) {
4484 *d = '\0';
4485 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4486 }
4487 }
4488 SvSETMAGIC(dest);
4489 RETURN;
4490}
4491
4492PP(pp_quotemeta)
4493{
4494 dVAR; dSP; dTARGET;
4495 SV * const sv = TOPs;
4496 STRLEN len;
4497 register const char *s = SvPV_const(sv,len);
4498
4499 SvUTF8_off(TARG); /* decontaminate */
4500 if (len) {
4501 register char *d;
4502 SvUPGRADE(TARG, SVt_PV);
4503 SvGROW(TARG, (len * 2) + 1);
4504 d = SvPVX(TARG);
4505 if (DO_UTF8(sv)) {
4506 while (len) {
4507 if (UTF8_IS_CONTINUED(*s)) {
4508 STRLEN ulen = UTF8SKIP(s);
4509 if (ulen > len)
4510 ulen = len;
4511 len -= ulen;
4512 while (ulen--)
4513 *d++ = *s++;
4514 }
4515 else {
4516 if (!isALNUM(*s))
4517 *d++ = '\\';
4518 *d++ = *s++;
4519 len--;
4520 }
4521 }
4522 SvUTF8_on(TARG);
4523 }
4524 else {
4525 while (len--) {
4526 if (!isALNUM(*s))
4527 *d++ = '\\';
4528 *d++ = *s++;
4529 }
4530 }
4531 *d = '\0';
4532 SvCUR_set(TARG, d - SvPVX_const(TARG));
4533 (void)SvPOK_only_UTF8(TARG);
4534 }
4535 else
4536 sv_setpvn(TARG, s, len);
4537 SETTARG;
4538 RETURN;
4539}
4540
4541/* Arrays. */
4542
4543PP(pp_aslice)
4544{
4545 dVAR; dSP; dMARK; dORIGMARK;
4546 register AV *const av = MUTABLE_AV(POPs);
4547 register const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
4548
4549 if (SvTYPE(av) == SVt_PVAV) {
4550 const I32 arybase = CopARYBASE_get(PL_curcop);
4551 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
4552 bool can_preserve = FALSE;
4553
4554 if (localizing) {
4555 MAGIC *mg;
4556 HV *stash;
4557
4558 can_preserve = SvCANEXISTDELETE(av);
4559 }
4560
4561 if (lval && localizing) {
4562 register SV **svp;
4563 I32 max = -1;
4564 for (svp = MARK + 1; svp <= SP; svp++) {
4565 const I32 elem = SvIV(*svp);
4566 if (elem > max)
4567 max = elem;
4568 }
4569 if (max > AvMAX(av))
4570 av_extend(av, max);
4571 }
4572
4573 while (++MARK <= SP) {
4574 register SV **svp;
4575 I32 elem = SvIV(*MARK);
4576 bool preeminent = TRUE;
4577
4578 if (elem > 0)
4579 elem -= arybase;
4580 if (localizing && can_preserve) {
4581 /* If we can determine whether the element exist,
4582 * Try to preserve the existenceness of a tied array
4583 * element by using EXISTS and DELETE if possible.
4584 * Fallback to FETCH and STORE otherwise. */
4585 preeminent = av_exists(av, elem);
4586 }
4587
4588 svp = av_fetch(av, elem, lval);
4589 if (lval) {
4590 if (!svp || *svp == &PL_sv_undef)
4591 DIE(aTHX_ PL_no_aelem, elem);
4592 if (localizing) {
4593 if (preeminent)
4594 save_aelem(av, elem, svp);
4595 else
4596 SAVEADELETE(av, elem);
4597 }
4598 }
4599 *MARK = svp ? *svp : &PL_sv_undef;
4600 }
4601 }
4602 if (GIMME != G_ARRAY) {
4603 MARK = ORIGMARK;
4604 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
4605 SP = MARK;
4606 }
4607 RETURN;
4608}
4609
4610PP(pp_aeach)
4611{
4612 dVAR;
4613 dSP;
4614 AV *array = MUTABLE_AV(POPs);
4615 const I32 gimme = GIMME_V;
4616 IV *iterp = Perl_av_iter_p(aTHX_ array);
4617 const IV current = (*iterp)++;
4618
4619 if (current > av_len(array)) {
4620 *iterp = 0;
4621 if (gimme == G_SCALAR)
4622 RETPUSHUNDEF;
4623 else
4624 RETURN;
4625 }
4626
4627 EXTEND(SP, 2);
4628 mPUSHi(CopARYBASE_get(PL_curcop) + current);
4629 if (gimme == G_ARRAY) {
4630 SV **const element = av_fetch(array, current, 0);
4631 PUSHs(element ? *element : &PL_sv_undef);
4632 }
4633 RETURN;
4634}
4635
4636PP(pp_akeys)
4637{
4638 dVAR;
4639 dSP;
4640 AV *array = MUTABLE_AV(POPs);
4641 const I32 gimme = GIMME_V;
4642
4643 *Perl_av_iter_p(aTHX_ array) = 0;
4644
4645 if (gimme == G_SCALAR) {
4646 dTARGET;
4647 PUSHi(av_len(array) + 1);
4648 }
4649 else if (gimme == G_ARRAY) {
4650 IV n = Perl_av_len(aTHX_ array);
4651 IV i = CopARYBASE_get(PL_curcop);
4652
4653 EXTEND(SP, n + 1);
4654
4655 if (PL_op->op_type == OP_AKEYS) {
4656 n += i;
4657 for (; i <= n; i++) {
4658 mPUSHi(i);
4659 }
4660 }
4661 else {
4662 for (i = 0; i <= n; i++) {
4663 SV *const *const elem = Perl_av_fetch(aTHX_ array, i, 0);
4664 PUSHs(elem ? *elem : &PL_sv_undef);
4665 }
4666 }
4667 }
4668 RETURN;
4669}
4670
4671/* Associative arrays. */
4672
4673PP(pp_each)
4674{
4675 dVAR;
4676 dSP;
4677 HV * hash = MUTABLE_HV(POPs);
4678 HE *entry;
4679 const I32 gimme = GIMME_V;
4680
4681 PUTBACK;
4682 /* might clobber stack_sp */
4683 entry = hv_iternext(hash);
4684 SPAGAIN;
4685
4686 EXTEND(SP, 2);
4687 if (entry) {
4688 SV* const sv = hv_iterkeysv(entry);
4689 PUSHs(sv); /* won't clobber stack_sp */
4690 if (gimme == G_ARRAY) {
4691 SV *val;
4692 PUTBACK;
4693 /* might clobber stack_sp */
4694 val = hv_iterval(hash, entry);
4695 SPAGAIN;
4696 PUSHs(val);
4697 }
4698 }
4699 else if (gimme == G_SCALAR)
4700 RETPUSHUNDEF;
4701
4702 RETURN;
4703}
4704
4705STATIC OP *
4706S_do_delete_local(pTHX)
4707{
4708 dVAR;
4709 dSP;
4710 const I32 gimme = GIMME_V;
4711 const MAGIC *mg;
4712 HV *stash;
4713
4714 if (PL_op->op_private & OPpSLICE) {
4715 dMARK; dORIGMARK;
4716 SV * const osv = POPs;
4717 const bool tied = SvRMAGICAL(osv)
4718 && mg_find((const SV *)osv, PERL_MAGIC_tied);
4719 const bool can_preserve = SvCANEXISTDELETE(osv)
4720 || mg_find((const SV *)osv, PERL_MAGIC_env);
4721 const U32 type = SvTYPE(osv);
4722 if (type == SVt_PVHV) { /* hash element */
4723 HV * const hv = MUTABLE_HV(osv);
4724 while (++MARK <= SP) {
4725 SV * const keysv = *MARK;
4726 SV *sv = NULL;
4727 bool preeminent = TRUE;
4728 if (can_preserve)
4729 preeminent = hv_exists_ent(hv, keysv, 0);
4730 if (tied) {
4731 HE *he = hv_fetch_ent(hv, keysv, 1, 0);
4732 if (he)
4733 sv = HeVAL(he);
4734 else
4735 preeminent = FALSE;
4736 }
4737 else {
4738 sv = hv_delete_ent(hv, keysv, 0, 0);
4739 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4740 }
4741 if (preeminent) {
4742 save_helem_flags(hv, keysv, &sv, SAVEf_KEEPOLDELEM);
4743 if (tied) {
4744 *MARK = sv_mortalcopy(sv);
4745 mg_clear(sv);
4746 } else
4747 *MARK = sv;
4748 }
4749 else {
4750 SAVEHDELETE(hv, keysv);
4751 *MARK = &PL_sv_undef;
4752 }
4753 }
4754 }
4755 else if (type == SVt_PVAV) { /* array element */
4756 if (PL_op->op_flags & OPf_SPECIAL) {
4757 AV * const av = MUTABLE_AV(osv);
4758 while (++MARK <= SP) {
4759 I32 idx = SvIV(*MARK);
4760 SV *sv = NULL;
4761 bool preeminent = TRUE;
4762 if (can_preserve)
4763 preeminent = av_exists(av, idx);
4764 if (tied) {
4765 SV **svp = av_fetch(av, idx, 1);
4766 if (svp)
4767 sv = *svp;
4768 else
4769 preeminent = FALSE;
4770 }
4771 else {
4772 sv = av_delete(av, idx, 0);
4773 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4774 }
4775 if (preeminent) {
4776 save_aelem_flags(av, idx, &sv, SAVEf_KEEPOLDELEM);
4777 if (tied) {
4778 *MARK = sv_mortalcopy(sv);
4779 mg_clear(sv);
4780 } else
4781 *MARK = sv;
4782 }
4783 else {
4784 SAVEADELETE(av, idx);
4785 *MARK = &PL_sv_undef;
4786 }
4787 }
4788 }
4789 }
4790 else
4791 DIE(aTHX_ "Not a HASH reference");
4792 if (gimme == G_VOID)
4793 SP = ORIGMARK;
4794 else if (gimme == G_SCALAR) {
4795 MARK = ORIGMARK;
4796 if (SP > MARK)
4797 *++MARK = *SP;
4798 else
4799 *++MARK = &PL_sv_undef;
4800 SP = MARK;
4801 }
4802 }
4803 else {
4804 SV * const keysv = POPs;
4805 SV * const osv = POPs;
4806 const bool tied = SvRMAGICAL(osv)
4807 && mg_find((const SV *)osv, PERL_MAGIC_tied);
4808 const bool can_preserve = SvCANEXISTDELETE(osv)
4809 || mg_find((const SV *)osv, PERL_MAGIC_env);
4810 const U32 type = SvTYPE(osv);
4811 SV *sv = NULL;
4812 if (type == SVt_PVHV) {
4813 HV * const hv = MUTABLE_HV(osv);
4814 bool preeminent = TRUE;
4815 if (can_preserve)
4816 preeminent = hv_exists_ent(hv, keysv, 0);
4817 if (tied) {
4818 HE *he = hv_fetch_ent(hv, keysv, 1, 0);
4819 if (he)
4820 sv = HeVAL(he);
4821 else
4822 preeminent = FALSE;
4823 }
4824 else {
4825 sv = hv_delete_ent(hv, keysv, 0, 0);
4826 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4827 }
4828 if (preeminent) {
4829 save_helem_flags(hv, keysv, &sv, SAVEf_KEEPOLDELEM);
4830 if (tied) {
4831 SV *nsv = sv_mortalcopy(sv);
4832 mg_clear(sv);
4833 sv = nsv;
4834 }
4835 }
4836 else
4837 SAVEHDELETE(hv, keysv);
4838 }
4839 else if (type == SVt_PVAV) {
4840 if (PL_op->op_flags & OPf_SPECIAL) {
4841 AV * const av = MUTABLE_AV(osv);
4842 I32 idx = SvIV(keysv);
4843 bool preeminent = TRUE;
4844 if (can_preserve)
4845 preeminent = av_exists(av, idx);
4846 if (tied) {
4847 SV **svp = av_fetch(av, idx, 1);
4848 if (svp)
4849 sv = *svp;
4850 else
4851 preeminent = FALSE;
4852 }
4853 else {
4854 sv = av_delete(av, idx, 0);
4855 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4856 }
4857 if (preeminent) {
4858 save_aelem_flags(av, idx, &sv, SAVEf_KEEPOLDELEM);
4859 if (tied) {
4860 SV *nsv = sv_mortalcopy(sv);
4861 mg_clear(sv);
4862 sv = nsv;
4863 }
4864 }
4865 else
4866 SAVEADELETE(av, idx);
4867 }
4868 else
4869 DIE(aTHX_ "panic: avhv_delete no longer supported");
4870 }
4871 else
4872 DIE(aTHX_ "Not a HASH reference");
4873 if (!sv)
4874 sv = &PL_sv_undef;
4875 if (gimme != G_VOID)
4876 PUSHs(sv);
4877 }
4878
4879 RETURN;
4880}
4881
4882PP(pp_delete)
4883{
4884 dVAR;
4885 dSP;
4886 I32 gimme;
4887 I32 discard;
4888
4889 if (PL_op->op_private & OPpLVAL_INTRO)
4890 return do_delete_local();
4891
4892 gimme = GIMME_V;
4893 discard = (gimme == G_VOID) ? G_DISCARD : 0;
4894
4895 if (PL_op->op_private & OPpSLICE) {
4896 dMARK; dORIGMARK;
4897 HV * const hv = MUTABLE_HV(POPs);
4898 const U32 hvtype = SvTYPE(hv);
4899 if (hvtype == SVt_PVHV) { /* hash element */
4900 while (++MARK <= SP) {
4901 SV * const sv = hv_delete_ent(hv, *MARK, discard, 0);
4902 *MARK = sv ? sv : &PL_sv_undef;
4903 }
4904 }
4905 else if (hvtype == SVt_PVAV) { /* array element */
4906 if (PL_op->op_flags & OPf_SPECIAL) {
4907 while (++MARK <= SP) {
4908 SV * const sv = av_delete(MUTABLE_AV(hv), SvIV(*MARK), discard);
4909 *MARK = sv ? sv : &PL_sv_undef;
4910 }
4911 }
4912 }
4913 else
4914 DIE(aTHX_ "Not a HASH reference");
4915 if (discard)
4916 SP = ORIGMARK;
4917 else if (gimme == G_SCALAR) {
4918 MARK = ORIGMARK;
4919 if (SP > MARK)
4920 *++MARK = *SP;
4921 else
4922 *++MARK = &PL_sv_undef;
4923 SP = MARK;
4924 }
4925 }
4926 else {
4927 SV *keysv = POPs;
4928 HV * const hv = MUTABLE_HV(POPs);
4929 SV *sv = NULL;
4930 if (SvTYPE(hv) == SVt_PVHV)
4931 sv = hv_delete_ent(hv, keysv, discard, 0);
4932 else if (SvTYPE(hv) == SVt_PVAV) {
4933 if (PL_op->op_flags & OPf_SPECIAL)
4934 sv = av_delete(MUTABLE_AV(hv), SvIV(keysv), discard);
4935 else
4936 DIE(aTHX_ "panic: avhv_delete no longer supported");
4937 }
4938 else
4939 DIE(aTHX_ "Not a HASH reference");
4940 if (!sv)
4941 sv = &PL_sv_undef;
4942 if (!discard)
4943 PUSHs(sv);
4944 }
4945 RETURN;
4946}
4947
4948PP(pp_exists)
4949{
4950 dVAR;
4951 dSP;
4952 SV *tmpsv;
4953 HV *hv;
4954
4955 if (PL_op->op_private & OPpEXISTS_SUB) {
4956 GV *gv;
4957 SV * const sv = POPs;
4958 CV * const cv = sv_2cv(sv, &hv, &gv, 0);
4959 if (cv)
4960 RETPUSHYES;
4961 if (gv && isGV(gv) && GvCV(gv) && !GvCVGEN(gv))
4962 RETPUSHYES;
4963 RETPUSHNO;
4964 }
4965 tmpsv = POPs;
4966 hv = MUTABLE_HV(POPs);
4967 if (SvTYPE(hv) == SVt_PVHV) {
4968 if (hv_exists_ent(hv, tmpsv, 0))
4969 RETPUSHYES;
4970 }
4971 else if (SvTYPE(hv) == SVt_PVAV) {
4972 if (PL_op->op_flags & OPf_SPECIAL) { /* array element */
4973 if (av_exists(MUTABLE_AV(hv), SvIV(tmpsv)))
4974 RETPUSHYES;
4975 }
4976 }
4977 else {
4978 DIE(aTHX_ "Not a HASH reference");
4979 }
4980 RETPUSHNO;
4981}
4982
4983PP(pp_hslice)
4984{
4985 dVAR; dSP; dMARK; dORIGMARK;
4986 register HV * const hv = MUTABLE_HV(POPs);
4987 register const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
4988 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
4989 bool can_preserve = FALSE;
4990
4991 if (localizing) {
4992 MAGIC *mg;
4993 HV *stash;
4994
4995 if (SvCANEXISTDELETE(hv) || mg_find((const SV *)hv, PERL_MAGIC_env))
4996 can_preserve = TRUE;
4997 }
4998
4999 while (++MARK <= SP) {
5000 SV * const keysv = *MARK;
5001 SV **svp;
5002 HE *he;
5003 bool preeminent = TRUE;
5004
5005 if (localizing && can_preserve) {
5006 /* If we can determine whether the element exist,
5007 * try to preserve the existenceness of a tied hash
5008 * element by using EXISTS and DELETE if possible.
5009 * Fallback to FETCH and STORE otherwise. */
5010 preeminent = hv_exists_ent(hv, keysv, 0);
5011 }
5012
5013 he = hv_fetch_ent(hv, keysv, lval, 0);
5014 svp = he ? &HeVAL(he) : NULL;
5015
5016 if (lval) {
5017 if (!svp || *svp == &PL_sv_undef) {
5018 DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
5019 }
5020 if (localizing) {
5021 if (HvNAME_get(hv) && isGV(*svp))
5022 save_gp(MUTABLE_GV(*svp), !(PL_op->op_flags & OPf_SPECIAL));
5023 else if (preeminent)
5024 save_helem_flags(hv, keysv, svp,
5025 (PL_op->op_flags & OPf_SPECIAL) ? 0 : SAVEf_SETMAGIC);
5026 else
5027 SAVEHDELETE(hv, keysv);
5028 }
5029 }
5030 *MARK = svp ? *svp : &PL_sv_undef;
5031 }
5032 if (GIMME != G_ARRAY) {
5033 MARK = ORIGMARK;
5034 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
5035 SP = MARK;
5036 }
5037 RETURN;
5038}
5039
5040/* List operators. */
5041
5042PP(pp_list)
5043{
5044 dVAR; dSP; dMARK;
5045 if (GIMME != G_ARRAY) {
5046 if (++MARK <= SP)
5047 *MARK = *SP; /* unwanted list, return last item */
5048 else
5049 *MARK = &PL_sv_undef;
5050 SP = MARK;
5051 }
5052 RETURN;
5053}
5054
5055PP(pp_lslice)
5056{
5057 dVAR;
5058 dSP;
5059 SV ** const lastrelem = PL_stack_sp;
5060 SV ** const lastlelem = PL_stack_base + POPMARK;
5061 SV ** const firstlelem = PL_stack_base + POPMARK + 1;
5062 register SV ** const firstrelem = lastlelem + 1;
5063 const I32 arybase = CopARYBASE_get(PL_curcop);
5064 I32 is_something_there = FALSE;
5065
5066 register const I32 max = lastrelem - lastlelem;
5067 register SV **lelem;
5068
5069 if (GIMME != G_ARRAY) {
5070 I32 ix = SvIV(*lastlelem);
5071 if (ix < 0)
5072 ix += max;
5073 else
5074 ix -= arybase;
5075 if (ix < 0 || ix >= max)
5076 *firstlelem = &PL_sv_undef;
5077 else
5078 *firstlelem = firstrelem[ix];
5079 SP = firstlelem;
5080 RETURN;
5081 }
5082
5083 if (max == 0) {
5084 SP = firstlelem - 1;
5085 RETURN;
5086 }
5087
5088 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
5089 I32 ix = SvIV(*lelem);
5090 if (ix < 0)
5091 ix += max;
5092 else
5093 ix -= arybase;
5094 if (ix < 0 || ix >= max)
5095 *lelem = &PL_sv_undef;
5096 else {
5097 is_something_there = TRUE;
5098 if (!(*lelem = firstrelem[ix]))
5099 *lelem = &PL_sv_undef;
5100 }
5101 }
5102 if (is_something_there)
5103 SP = lastlelem;
5104 else
5105 SP = firstlelem - 1;
5106 RETURN;
5107}
5108
5109PP(pp_anonlist)
5110{
5111 dVAR; dSP; dMARK; dORIGMARK;
5112 const I32 items = SP - MARK;
5113 SV * const av = MUTABLE_SV(av_make(items, MARK+1));
5114 SP = ORIGMARK; /* av_make() might realloc stack_sp */
5115 mXPUSHs((PL_op->op_flags & OPf_SPECIAL)
5116 ? newRV_noinc(av) : av);
5117 RETURN;
5118}
5119
5120PP(pp_anonhash)
5121{
5122 dVAR; dSP; dMARK; dORIGMARK;
5123 HV* const hv = newHV();
5124
5125 while (MARK < SP) {
5126 SV * const key = *++MARK;
5127 SV * const val = newSV(0);
5128 if (MARK < SP)
5129 sv_setsv(val, *++MARK);
5130 else
5131 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Odd number of elements in anonymous hash");
5132 (void)hv_store_ent(hv,key,val,0);
5133 }
5134 SP = ORIGMARK;
5135 mXPUSHs((PL_op->op_flags & OPf_SPECIAL)
5136 ? newRV_noinc(MUTABLE_SV(hv)) : MUTABLE_SV(hv));
5137 RETURN;
5138}
5139
5140PP(pp_splice)
5141{
5142 dVAR; dSP; dMARK; dORIGMARK;
5143 register AV *ary = MUTABLE_AV(*++MARK);
5144 register SV **src;
5145 register SV **dst;
5146 register I32 i;
5147 register I32 offset;
5148 register I32 length;
5149 I32 newlen;
5150 I32 after;
5151 I32 diff;
5152 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
5153
5154 if (mg) {
5155 *MARK-- = SvTIED_obj(MUTABLE_SV(ary), mg);
5156 PUSHMARK(MARK);
5157 PUTBACK;
5158 ENTER_with_name("call_SPLICE");
5159 call_method("SPLICE",GIMME_V);
5160 LEAVE_with_name("call_SPLICE");
5161 SPAGAIN;
5162 RETURN;
5163 }
5164
5165 SP++;
5166
5167 if (++MARK < SP) {
5168 offset = i = SvIV(*MARK);
5169 if (offset < 0)
5170 offset += AvFILLp(ary) + 1;
5171 else
5172 offset -= CopARYBASE_get(PL_curcop);
5173 if (offset < 0)
5174 DIE(aTHX_ PL_no_aelem, i);
5175 if (++MARK < SP) {
5176 length = SvIVx(*MARK++);
5177 if (length < 0) {
5178 length += AvFILLp(ary) - offset + 1;
5179 if (length < 0)
5180 length = 0;
5181 }
5182 }
5183 else
5184 length = AvMAX(ary) + 1; /* close enough to infinity */
5185 }
5186 else {
5187 offset = 0;
5188 length = AvMAX(ary) + 1;
5189 }
5190 if (offset > AvFILLp(ary) + 1) {
5191 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "splice() offset past end of array" );
5192 offset = AvFILLp(ary) + 1;
5193 }
5194 after = AvFILLp(ary) + 1 - (offset + length);
5195 if (after < 0) { /* not that much array */
5196 length += after; /* offset+length now in array */
5197 after = 0;
5198 if (!AvALLOC(ary))
5199 av_extend(ary, 0);
5200 }
5201
5202 /* At this point, MARK .. SP-1 is our new LIST */
5203
5204 newlen = SP - MARK;
5205 diff = newlen - length;
5206 if (newlen && !AvREAL(ary) && AvREIFY(ary))
5207 av_reify(ary);
5208
5209 /* make new elements SVs now: avoid problems if they're from the array */
5210 for (dst = MARK, i = newlen; i; i--) {
5211 SV * const h = *dst;
5212 *dst++ = newSVsv(h);
5213 }
5214
5215 if (diff < 0) { /* shrinking the area */
5216 SV **tmparyval = NULL;
5217 if (newlen) {
5218 Newx(tmparyval, newlen, SV*); /* so remember insertion */
5219 Copy(MARK, tmparyval, newlen, SV*);
5220 }
5221
5222 MARK = ORIGMARK + 1;
5223 if (GIMME == G_ARRAY) { /* copy return vals to stack */
5224 MEXTEND(MARK, length);
5225 Copy(AvARRAY(ary)+offset, MARK, length, SV*);
5226 if (AvREAL(ary)) {
5227 EXTEND_MORTAL(length);
5228 for (i = length, dst = MARK; i; i--) {
5229 sv_2mortal(*dst); /* free them eventualy */
5230 dst++;
5231 }
5232 }
5233 MARK += length - 1;
5234 }
5235 else {
5236 *MARK = AvARRAY(ary)[offset+length-1];
5237 if (AvREAL(ary)) {
5238 sv_2mortal(*MARK);
5239 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
5240 SvREFCNT_dec(*dst++); /* free them now */
5241 }
5242 }
5243 AvFILLp(ary) += diff;
5244
5245 /* pull up or down? */
5246
5247 if (offset < after) { /* easier to pull up */
5248 if (offset) { /* esp. if nothing to pull */
5249 src = &AvARRAY(ary)[offset-1];
5250 dst = src - diff; /* diff is negative */
5251 for (i = offset; i > 0; i--) /* can't trust Copy */
5252 *dst-- = *src--;
5253 }
5254 dst = AvARRAY(ary);
5255 AvARRAY(ary) = AvARRAY(ary) - diff; /* diff is negative */
5256 AvMAX(ary) += diff;
5257 }
5258 else {
5259 if (after) { /* anything to pull down? */
5260 src = AvARRAY(ary) + offset + length;
5261 dst = src + diff; /* diff is negative */
5262 Move(src, dst, after, SV*);
5263 }
5264 dst = &AvARRAY(ary)[AvFILLp(ary)+1];
5265 /* avoid later double free */
5266 }
5267 i = -diff;
5268 while (i)
5269 dst[--i] = &PL_sv_undef;
5270
5271 if (newlen) {
5272 Copy( tmparyval, AvARRAY(ary) + offset, newlen, SV* );
5273 Safefree(tmparyval);
5274 }
5275 }
5276 else { /* no, expanding (or same) */
5277 SV** tmparyval = NULL;
5278 if (length) {
5279 Newx(tmparyval, length, SV*); /* so remember deletion */
5280 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
5281 }
5282
5283 if (diff > 0) { /* expanding */
5284 /* push up or down? */
5285 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
5286 if (offset) {
5287 src = AvARRAY(ary);
5288 dst = src - diff;
5289 Move(src, dst, offset, SV*);
5290 }
5291 AvARRAY(ary) = AvARRAY(ary) - diff;/* diff is positive */
5292 AvMAX(ary) += diff;
5293 AvFILLp(ary) += diff;
5294 }
5295 else {
5296 if (AvFILLp(ary) + diff >= AvMAX(ary)) /* oh, well */
5297 av_extend(ary, AvFILLp(ary) + diff);
5298 AvFILLp(ary) += diff;
5299
5300 if (after) {
5301 dst = AvARRAY(ary) + AvFILLp(ary);
5302 src = dst - diff;
5303 for (i = after; i; i--) {
5304 *dst-- = *src--;
5305 }
5306 }
5307 }
5308 }
5309
5310 if (newlen) {
5311 Copy( MARK, AvARRAY(ary) + offset, newlen, SV* );
5312 }
5313
5314 MARK = ORIGMARK + 1;
5315 if (GIMME == G_ARRAY) { /* copy return vals to stack */
5316 if (length) {
5317 Copy(tmparyval, MARK, length, SV*);
5318 if (AvREAL(ary)) {
5319 EXTEND_MORTAL(length);
5320 for (i = length, dst = MARK; i; i--) {
5321 sv_2mortal(*dst); /* free them eventualy */
5322 dst++;
5323 }
5324 }
5325 }
5326 MARK += length - 1;
5327 }
5328 else if (length--) {
5329 *MARK = tmparyval[length];
5330 if (AvREAL(ary)) {
5331 sv_2mortal(*MARK);
5332 while (length-- > 0)
5333 SvREFCNT_dec(tmparyval[length]);
5334 }
5335 }
5336 else
5337 *MARK = &PL_sv_undef;
5338 Safefree(tmparyval);
5339 }
5340 SP = MARK;
5341 RETURN;
5342}
5343
5344PP(pp_push)
5345{
5346 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
5347 register AV * const ary = MUTABLE_AV(*++MARK);
5348 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
5349
5350 if (mg) {
5351 *MARK-- = SvTIED_obj(MUTABLE_SV(ary), mg);
5352 PUSHMARK(MARK);
5353 PUTBACK;
5354 ENTER_with_name("call_PUSH");
5355 call_method("PUSH",G_SCALAR|G_DISCARD);
5356 LEAVE_with_name("call_PUSH");
5357 SPAGAIN;
5358 }
5359 else {
5360 PL_delaymagic = DM_DELAY;
5361 for (++MARK; MARK <= SP; MARK++) {
5362 SV * const sv = newSV(0);
5363 if (*MARK)
5364 sv_setsv(sv, *MARK);
5365 av_store(ary, AvFILLp(ary)+1, sv);
5366 }
5367 if (PL_delaymagic & DM_ARRAY_ISA)
5368 mg_set(MUTABLE_SV(ary));
5369
5370 PL_delaymagic = 0;
5371 }
5372 SP = ORIGMARK;
5373 if (OP_GIMME(PL_op, 0) != G_VOID) {
5374 PUSHi( AvFILL(ary) + 1 );
5375 }
5376 RETURN;
5377}
5378
5379PP(pp_shift)
5380{
5381 dVAR;
5382 dSP;
5383 AV * const av = PL_op->op_flags & OPf_SPECIAL
5384 ? MUTABLE_AV(GvAV(PL_defgv)) : MUTABLE_AV(POPs);
5385 SV * const sv = PL_op->op_type == OP_SHIFT ? av_shift(av) : av_pop(av);
5386 EXTEND(SP, 1);
5387 assert (sv);
5388 if (AvREAL(av))
5389 (void)sv_2mortal(sv);
5390 PUSHs(sv);
5391 RETURN;
5392}
5393
5394PP(pp_unshift)
5395{
5396 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
5397 register AV *ary = MUTABLE_AV(*++MARK);
5398 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
5399
5400 if (mg) {
5401 *MARK-- = SvTIED_obj(MUTABLE_SV(ary), mg);
5402 PUSHMARK(MARK);
5403 PUTBACK;
5404 ENTER_with_name("call_UNSHIFT");
5405 call_method("UNSHIFT",G_SCALAR|G_DISCARD);
5406 LEAVE_with_name("call_UNSHIFT");
5407 SPAGAIN;
5408 }
5409 else {
5410 register I32 i = 0;
5411 av_unshift(ary, SP - MARK);
5412 while (MARK < SP) {
5413 SV * const sv = newSVsv(*++MARK);
5414 (void)av_store(ary, i++, sv);
5415 }
5416 }
5417 SP = ORIGMARK;
5418 if (OP_GIMME(PL_op, 0) != G_VOID) {
5419 PUSHi( AvFILL(ary) + 1 );
5420 }
5421 RETURN;
5422}
5423
5424PP(pp_reverse)
5425{
5426 dVAR; dSP; dMARK;
5427
5428 if (GIMME == G_ARRAY) {
5429 if (PL_op->op_private & OPpREVERSE_INPLACE) {
5430 AV *av;
5431
5432 /* See pp_sort() */
5433 assert( MARK+1 == SP && *SP && SvTYPE(*SP) == SVt_PVAV);
5434 (void)POPMARK; /* remove mark associated with ex-OP_AASSIGN */
5435 av = MUTABLE_AV((*SP));
5436 /* In-place reversing only happens in void context for the array
5437 * assignment. We don't need to push anything on the stack. */
5438 SP = MARK;
5439
5440 if (SvMAGICAL(av)) {
5441 I32 i, j;
5442 register SV *tmp = sv_newmortal();
5443 /* For SvCANEXISTDELETE */
5444 HV *stash;
5445 const MAGIC *mg;
5446 bool can_preserve = SvCANEXISTDELETE(av);
5447
5448 for (i = 0, j = av_len(av); i < j; ++i, --j) {
5449 register SV *begin, *end;
5450
5451 if (can_preserve) {
5452 if (!av_exists(av, i)) {
5453 if (av_exists(av, j)) {
5454 register SV *sv = av_delete(av, j, 0);
5455 begin = *av_fetch(av, i, TRUE);
5456 sv_setsv_mg(begin, sv);
5457 }
5458 continue;
5459 }
5460 else if (!av_exists(av, j)) {
5461 register SV *sv = av_delete(av, i, 0);
5462 end = *av_fetch(av, j, TRUE);
5463 sv_setsv_mg(end, sv);
5464 continue;
5465 }
5466 }
5467
5468 begin = *av_fetch(av, i, TRUE);
5469 end = *av_fetch(av, j, TRUE);
5470 sv_setsv(tmp, begin);
5471 sv_setsv_mg(begin, end);
5472 sv_setsv_mg(end, tmp);
5473 }
5474 }
5475 else {
5476 SV **begin = AvARRAY(av);
5477
5478 if (begin) {
5479 SV **end = begin + AvFILLp(av);
5480
5481 while (begin < end) {
5482 register SV * const tmp = *begin;
5483 *begin++ = *end;
5484 *end-- = tmp;
5485 }
5486 }
5487 }
5488 }
5489 else {
5490 SV **oldsp = SP;
5491 MARK++;
5492 while (MARK < SP) {
5493 register SV * const tmp = *MARK;
5494 *MARK++ = *SP;
5495 *SP-- = tmp;
5496 }
5497 /* safe as long as stack cannot get extended in the above */
5498 SP = oldsp;
5499 }
5500 }
5501 else {
5502 register char *up;
5503 register char *down;
5504 register I32 tmp;
5505 dTARGET;
5506 STRLEN len;
5507
5508 SvUTF8_off(TARG); /* decontaminate */
5509 if (SP - MARK > 1)
5510 do_join(TARG, &PL_sv_no, MARK, SP);
5511 else {
5512 sv_setsv(TARG, SP > MARK ? *SP : find_rundefsv());
5513 if (! SvOK(TARG) && ckWARN(WARN_UNINITIALIZED))
5514 report_uninit(TARG);
5515 }
5516
5517 up = SvPV_force(TARG, len);
5518 if (len > 1) {
5519 if (DO_UTF8(TARG)) { /* first reverse each character */
5520 U8* s = (U8*)SvPVX(TARG);
5521 const U8* send = (U8*)(s + len);
5522 while (s < send) {
5523 if (UTF8_IS_INVARIANT(*s)) {
5524 s++;
5525 continue;
5526 }
5527 else {
5528 if (!utf8_to_uvchr(s, 0))
5529 break;
5530 up = (char*)s;
5531 s += UTF8SKIP(s);
5532 down = (char*)(s - 1);
5533 /* reverse this character */
5534 while (down > up) {
5535 tmp = *up;
5536 *up++ = *down;
5537 *down-- = (char)tmp;
5538 }
5539 }
5540 }
5541 up = SvPVX(TARG);
5542 }
5543 down = SvPVX(TARG) + len - 1;
5544 while (down > up) {
5545 tmp = *up;
5546 *up++ = *down;
5547 *down-- = (char)tmp;
5548 }
5549 (void)SvPOK_only_UTF8(TARG);
5550 }
5551 SP = MARK + 1;
5552 SETTARG;
5553 }
5554 RETURN;
5555}
5556
5557PP(pp_split)
5558{
5559 dVAR; dSP; dTARG;
5560 AV *ary;
5561 register IV limit = POPi; /* note, negative is forever */
5562 SV * const sv = POPs;
5563 STRLEN len;
5564 register const char *s = SvPV_const(sv, len);
5565 const bool do_utf8 = DO_UTF8(sv);
5566 const char *strend = s + len;
5567 register PMOP *pm;
5568 register REGEXP *rx;
5569 register SV *dstr;
5570 register const char *m;
5571 I32 iters = 0;
5572 const STRLEN slen = do_utf8 ? utf8_length((U8*)s, (U8*)strend) : (STRLEN)(strend - s);
5573 I32 maxiters = slen + 10;
5574 I32 trailing_empty = 0;
5575 const char *orig;
5576 const I32 origlimit = limit;
5577 I32 realarray = 0;
5578 I32 base;
5579 const I32 gimme = GIMME_V;
5580 bool gimme_scalar;
5581 const I32 oldsave = PL_savestack_ix;
5582 U32 make_mortal = SVs_TEMP;
5583 bool multiline = 0;
5584 MAGIC *mg = NULL;
5585
5586#ifdef DEBUGGING
5587 Copy(&LvTARGOFF(POPs), &pm, 1, PMOP*);
5588#else
5589 pm = (PMOP*)POPs;
5590#endif
5591 if (!pm || !s)
5592 DIE(aTHX_ "panic: pp_split");
5593 rx = PM_GETRE(pm);
5594
5595 TAINT_IF((RX_EXTFLAGS(rx) & RXf_PMf_LOCALE) &&
5596 (RX_EXTFLAGS(rx) & (RXf_WHITE | RXf_SKIPWHITE)));
5597
5598 RX_MATCH_UTF8_set(rx, do_utf8);
5599
5600#ifdef USE_ITHREADS
5601 if (pm->op_pmreplrootu.op_pmtargetoff) {
5602 ary = GvAVn(MUTABLE_GV(PAD_SVl(pm->op_pmreplrootu.op_pmtargetoff)));
5603 }
5604#else
5605 if (pm->op_pmreplrootu.op_pmtargetgv) {
5606 ary = GvAVn(pm->op_pmreplrootu.op_pmtargetgv);
5607 }
5608#endif
5609 else
5610 ary = NULL;
5611 if (ary && (gimme != G_ARRAY || (pm->op_pmflags & PMf_ONCE))) {
5612 realarray = 1;
5613 PUTBACK;
5614 av_extend(ary,0);
5615 av_clear(ary);
5616 SPAGAIN;
5617 if ((mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied))) {
5618 PUSHMARK(SP);
5619 XPUSHs(SvTIED_obj(MUTABLE_SV(ary), mg));
5620 }
5621 else {
5622 if (!AvREAL(ary)) {
5623 I32 i;
5624 AvREAL_on(ary);
5625 AvREIFY_off(ary);
5626 for (i = AvFILLp(ary); i >= 0; i--)
5627 AvARRAY(ary)[i] = &PL_sv_undef; /* don't free mere refs */
5628 }
5629 /* temporarily switch stacks */
5630 SAVESWITCHSTACK(PL_curstack, ary);
5631 make_mortal = 0;
5632 }
5633 }
5634 base = SP - PL_stack_base;
5635 orig = s;
5636 if (RX_EXTFLAGS(rx) & RXf_SKIPWHITE) {
5637 if (do_utf8) {
5638 while (*s == ' ' || is_utf8_space((U8*)s))
5639 s += UTF8SKIP(s);
5640 }
5641 else if (RX_EXTFLAGS(rx) & RXf_PMf_LOCALE) {
5642 while (isSPACE_LC(*s))
5643 s++;
5644 }
5645 else {
5646 while (isSPACE(*s))
5647 s++;
5648 }
5649 }
5650 if (RX_EXTFLAGS(rx) & PMf_MULTILINE) {
5651 multiline = 1;
5652 }
5653
5654 gimme_scalar = gimme == G_SCALAR && !ary;
5655
5656 if (!limit)
5657 limit = maxiters + 2;
5658 if (RX_EXTFLAGS(rx) & RXf_WHITE) {
5659 while (--limit) {
5660 m = s;
5661 /* this one uses 'm' and is a negative test */
5662 if (do_utf8) {
5663 while (m < strend && !( *m == ' ' || is_utf8_space((U8*)m) )) {
5664 const int t = UTF8SKIP(m);
5665 /* is_utf8_space returns FALSE for malform utf8 */
5666 if (strend - m < t)
5667 m = strend;
5668 else
5669 m += t;
5670 }
5671 } else if (RX_EXTFLAGS(rx) & RXf_PMf_LOCALE) {
5672 while (m < strend && !isSPACE_LC(*m))
5673 ++m;
5674 } else {
5675 while (m < strend && !isSPACE(*m))
5676 ++m;
5677 }
5678 if (m >= strend)
5679 break;
5680
5681 if (gimme_scalar) {
5682 iters++;
5683 if (m-s == 0)
5684 trailing_empty++;
5685 else
5686 trailing_empty = 0;
5687 } else {
5688 dstr = newSVpvn_flags(s, m-s,
5689 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5690 XPUSHs(dstr);
5691 }
5692
5693 /* skip the whitespace found last */
5694 if (do_utf8)
5695 s = m + UTF8SKIP(m);
5696 else
5697 s = m + 1;
5698
5699 /* this one uses 's' and is a positive test */
5700 if (do_utf8) {
5701 while (s < strend && ( *s == ' ' || is_utf8_space((U8*)s) ))
5702 s += UTF8SKIP(s);
5703 } else if (RX_EXTFLAGS(rx) & RXf_PMf_LOCALE) {
5704 while (s < strend && isSPACE_LC(*s))
5705 ++s;
5706 } else {
5707 while (s < strend && isSPACE(*s))
5708 ++s;
5709 }
5710 }
5711 }
5712 else if (RX_EXTFLAGS(rx) & RXf_START_ONLY) {
5713 while (--limit) {
5714 for (m = s; m < strend && *m != '\n'; m++)
5715 ;
5716 m++;
5717 if (m >= strend)
5718 break;
5719
5720 if (gimme_scalar) {
5721 iters++;
5722 if (m-s == 0)
5723 trailing_empty++;
5724 else
5725 trailing_empty = 0;
5726 } else {
5727 dstr = newSVpvn_flags(s, m-s,
5728 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5729 XPUSHs(dstr);
5730 }
5731 s = m;
5732 }
5733 }
5734 else if (RX_EXTFLAGS(rx) & RXf_NULL && !(s >= strend)) {
5735 /*
5736 Pre-extend the stack, either the number of bytes or
5737 characters in the string or a limited amount, triggered by:
5738
5739 my ($x, $y) = split //, $str;
5740 or
5741 split //, $str, $i;
5742 */
5743 if (!gimme_scalar) {
5744 const U32 items = limit - 1;
5745 if (items < slen)
5746 EXTEND(SP, items);
5747 else
5748 EXTEND(SP, slen);
5749 }
5750
5751 if (do_utf8) {
5752 while (--limit) {
5753 /* keep track of how many bytes we skip over */
5754 m = s;
5755 s += UTF8SKIP(s);
5756 if (gimme_scalar) {
5757 iters++;
5758 if (s-m == 0)
5759 trailing_empty++;
5760 else
5761 trailing_empty = 0;
5762 } else {
5763 dstr = newSVpvn_flags(m, s-m, SVf_UTF8 | make_mortal);
5764
5765 PUSHs(dstr);
5766 }
5767
5768 if (s >= strend)
5769 break;
5770 }
5771 } else {
5772 while (--limit) {
5773 if (gimme_scalar) {
5774 iters++;
5775 } else {
5776 dstr = newSVpvn(s, 1);
5777
5778
5779 if (make_mortal)
5780 sv_2mortal(dstr);
5781
5782 PUSHs(dstr);
5783 }
5784
5785 s++;
5786
5787 if (s >= strend)
5788 break;
5789 }
5790 }
5791 }
5792 else if (do_utf8 == (RX_UTF8(rx) != 0) &&
5793 (RX_EXTFLAGS(rx) & RXf_USE_INTUIT) && !RX_NPARENS(rx)
5794 && (RX_EXTFLAGS(rx) & RXf_CHECK_ALL)
5795 && !(RX_EXTFLAGS(rx) & RXf_ANCH)) {
5796 const int tail = (RX_EXTFLAGS(rx) & RXf_INTUIT_TAIL);
5797 SV * const csv = CALLREG_INTUIT_STRING(rx);
5798
5799 len = RX_MINLENRET(rx);
5800 if (len == 1 && !RX_UTF8(rx) && !tail) {
5801 const char c = *SvPV_nolen_const(csv);
5802 while (--limit) {
5803 for (m = s; m < strend && *m != c; m++)
5804 ;
5805 if (m >= strend)
5806 break;
5807 if (gimme_scalar) {
5808 iters++;
5809 if (m-s == 0)
5810 trailing_empty++;
5811 else
5812 trailing_empty = 0;
5813 } else {
5814 dstr = newSVpvn_flags(s, m-s,
5815 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5816 XPUSHs(dstr);
5817 }
5818 /* The rx->minlen is in characters but we want to step
5819 * s ahead by bytes. */
5820 if (do_utf8)
5821 s = (char*)utf8_hop((U8*)m, len);
5822 else
5823 s = m + len; /* Fake \n at the end */
5824 }
5825 }
5826 else {
5827 while (s < strend && --limit &&
5828 (m = fbm_instr((unsigned char*)s, (unsigned char*)strend,
5829 csv, multiline ? FBMrf_MULTILINE : 0)) )
5830 {
5831 if (gimme_scalar) {
5832 iters++;
5833 if (m-s == 0)
5834 trailing_empty++;
5835 else
5836 trailing_empty = 0;
5837 } else {
5838 dstr = newSVpvn_flags(s, m-s,
5839 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5840 XPUSHs(dstr);
5841 }
5842 /* The rx->minlen is in characters but we want to step
5843 * s ahead by bytes. */
5844 if (do_utf8)
5845 s = (char*)utf8_hop((U8*)m, len);
5846 else
5847 s = m + len; /* Fake \n at the end */
5848 }
5849 }
5850 }
5851 else {
5852 maxiters += slen * RX_NPARENS(rx);
5853 while (s < strend && --limit)
5854 {
5855 I32 rex_return;
5856 PUTBACK;
5857 rex_return = CALLREGEXEC(rx, (char*)s, (char*)strend, (char*)orig, 1 ,
5858 sv, NULL, 0);
5859 SPAGAIN;
5860 if (rex_return == 0)
5861 break;
5862 TAINT_IF(RX_MATCH_TAINTED(rx));
5863 if (RX_MATCH_COPIED(rx) && RX_SUBBEG(rx) != orig) {
5864 m = s;
5865 s = orig;
5866 orig = RX_SUBBEG(rx);
5867 s = orig + (m - s);
5868 strend = s + (strend - m);
5869 }
5870 m = RX_OFFS(rx)[0].start + orig;
5871
5872 if (gimme_scalar) {
5873 iters++;
5874 if (m-s == 0)
5875 trailing_empty++;
5876 else
5877 trailing_empty = 0;
5878 } else {
5879 dstr = newSVpvn_flags(s, m-s,
5880 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5881 XPUSHs(dstr);
5882 }
5883 if (RX_NPARENS(rx)) {
5884 I32 i;
5885 for (i = 1; i <= (I32)RX_NPARENS(rx); i++) {
5886 s = RX_OFFS(rx)[i].start + orig;
5887 m = RX_OFFS(rx)[i].end + orig;
5888
5889 /* japhy (07/27/01) -- the (m && s) test doesn't catch
5890 parens that didn't match -- they should be set to
5891 undef, not the empty string */
5892 if (gimme_scalar) {
5893 iters++;
5894 if (m-s == 0)
5895 trailing_empty++;
5896 else
5897 trailing_empty = 0;
5898 } else {
5899 if (m >= orig && s >= orig) {
5900 dstr = newSVpvn_flags(s, m-s,
5901 (do_utf8 ? SVf_UTF8 : 0)
5902 | make_mortal);
5903 }
5904 else
5905 dstr = &PL_sv_undef; /* undef, not "" */
5906 XPUSHs(dstr);
5907 }
5908
5909 }
5910 }
5911 s = RX_OFFS(rx)[0].end + orig;
5912 }
5913 }
5914
5915 if (!gimme_scalar) {
5916 iters = (SP - PL_stack_base) - base;
5917 }
5918 if (iters > maxiters)
5919 DIE(aTHX_ "Split loop");
5920
5921 /* keep field after final delim? */
5922 if (s < strend || (iters && origlimit)) {
5923 if (!gimme_scalar) {
5924 const STRLEN l = strend - s;
5925 dstr = newSVpvn_flags(s, l, (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5926 XPUSHs(dstr);
5927 }
5928 iters++;
5929 }
5930 else if (!origlimit) {
5931 if (gimme_scalar) {
5932 iters -= trailing_empty;
5933 } else {
5934 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0)) {
5935 if (TOPs && !make_mortal)
5936 sv_2mortal(TOPs);
5937 *SP-- = &PL_sv_undef;
5938 iters--;
5939 }
5940 }
5941 }
5942
5943 PUTBACK;
5944 LEAVE_SCOPE(oldsave); /* may undo an earlier SWITCHSTACK */
5945 SPAGAIN;
5946 if (realarray) {
5947 if (!mg) {
5948 if (SvSMAGICAL(ary)) {
5949 PUTBACK;
5950 mg_set(MUTABLE_SV(ary));
5951 SPAGAIN;
5952 }
5953 if (gimme == G_ARRAY) {
5954 EXTEND(SP, iters);
5955 Copy(AvARRAY(ary), SP + 1, iters, SV*);
5956 SP += iters;
5957 RETURN;
5958 }
5959 }
5960 else {
5961 PUTBACK;
5962 ENTER_with_name("call_PUSH");
5963 call_method("PUSH",G_SCALAR|G_DISCARD);
5964 LEAVE_with_name("call_PUSH");
5965 SPAGAIN;
5966 if (gimme == G_ARRAY) {
5967 I32 i;
5968 /* EXTEND should not be needed - we just popped them */
5969 EXTEND(SP, iters);
5970 for (i=0; i < iters; i++) {
5971 SV **svp = av_fetch(ary, i, FALSE);
5972 PUSHs((svp) ? *svp : &PL_sv_undef);
5973 }
5974 RETURN;
5975 }
5976 }
5977 }
5978 else {
5979 if (gimme == G_ARRAY)
5980 RETURN;
5981 }
5982
5983 GETTARGET;
5984 PUSHi(iters);
5985 RETURN;
5986}
5987
5988PP(pp_once)
5989{
5990 dSP;
5991 SV *const sv = PAD_SVl(PL_op->op_targ);
5992
5993 if (SvPADSTALE(sv)) {
5994 /* First time. */
5995 SvPADSTALE_off(sv);
5996 RETURNOP(cLOGOP->op_other);
5997 }
5998 RETURNOP(cLOGOP->op_next);
5999}
6000
6001PP(pp_lock)
6002{
6003 dVAR;
6004 dSP;
6005 dTOPss;
6006 SV *retsv = sv;
6007 assert(SvTYPE(retsv) != SVt_PVCV);
6008 SvLOCK(sv);
6009 if (SvTYPE(retsv) == SVt_PVAV || SvTYPE(retsv) == SVt_PVHV) {
6010 retsv = refto(retsv);
6011 }
6012 SETs(retsv);
6013 RETURN;
6014}
6015
6016
6017PP(unimplemented_op)
6018{
6019 dVAR;
6020 DIE(aTHX_ "panic: unimplemented op %s (#%d) called", OP_NAME(PL_op),
6021 PL_op->op_type);
6022}
6023
6024PP(pp_boolkeys)
6025{
6026 dVAR;
6027 dSP;
6028 HV * const hv = (HV*)POPs;
6029
6030 if (SvRMAGICAL(hv)) {
6031 MAGIC * const mg = mg_find((SV*)hv, PERL_MAGIC_tied);
6032 if (mg) {
6033 XPUSHs(magic_scalarpack(hv, mg));
6034 RETURN;
6035 }
6036 }
6037
6038 XPUSHs(boolSV(HvKEYS(hv) != 0));
6039 RETURN;
6040}
6041
6042/*
6043 * Local variables:
6044 * c-indentation-style: bsd
6045 * c-basic-offset: 4
6046 * indent-tabs-mode: t
6047 * End:
6048 *
6049 * ex: set ts=8 sts=4 sw=4 noet:
6050 */