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
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.
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
16 * [p.225 of _The Lord of the Rings_, II/i: "Many Meetings"]
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.
33 /* XXX I can't imagine anyone who doesn't have this actually _needs_
34 it, since pid_t is an integral type.
37 #ifdef NEED_GETPID_PROTO
38 extern Pid_t getpid (void);
42 * Some BSDs and Cygwin default to POSIX math instead of IEEE.
43 * This switches them over to IEEE.
45 #if defined(LIBM_LIB_VERSION)
46 _LIB_VERSION_TYPE _LIB_VERSION = _IEEE_;
49 /* variations on pp_null */
55 if (GIMME_V == G_SCALAR)
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));
71 if (PL_op->op_flags & OPf_REF) {
75 if (GIMME == G_SCALAR)
76 Perl_croak(aTHX_ "Can't return array to lvalue scalar context");
81 if (gimme == G_ARRAY) {
82 const I32 maxarg = AvFILL(MUTABLE_AV(TARG)) + 1;
84 if (SvMAGICAL(TARG)) {
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;
92 Copy(AvARRAY((const AV *)TARG), SP+1, maxarg, SV*);
96 else if (gimme == G_SCALAR) {
97 SV* const sv = sv_newmortal();
98 const I32 maxarg = AvFILL(MUTABLE_AV(TARG)) + 1;
110 assert(SvTYPE(TARG) == SVt_PVHV);
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)
118 if (GIMME == G_SCALAR)
119 Perl_croak(aTHX_ "Can't return hash to lvalue scalar context");
123 if (gimme == G_ARRAY) {
126 else if (gimme == G_SCALAR) {
127 SV* const sv = Perl_hv_scalar(aTHX_ MUTABLE_HV(TARG));
135 static const char S_no_symref_sv[] =
136 "Can't use string (\"%" SVf32 "\"%s) as %s ref while \"strict refs\" in use";
145 tryAMAGICunDEREF(to_gv);
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);
155 else if (!isGV_with_GP(sv))
156 DIE(aTHX_ "Not a GLOB reference");
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
165 Perl_croak_no_modify(aTHX);
166 if (PL_op->op_private & OPpDEREF) {
168 if (cUNOP->op_targ) {
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);
176 const char * const name = CopSTASHPV(PL_curcop);
179 prepare_SV_for_RV(sv);
180 SvRV_set(sv, MUTABLE_SV(gv));
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))
192 if ((PL_op->op_flags & OPf_SPECIAL) &&
193 !(PL_op->op_flags & OPf_MOD))
195 SV * const temp = MUTABLE_SV(gv_fetchsv(sv, 0, SVt_PVGV));
197 && (!is_gv_magical_sv(sv,0)
198 || !(sv = MUTABLE_SV(gv_fetchsv(sv, GV_ADD,
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
214 sv = MUTABLE_SV(gv_fetchsv(sv, GV_ADD, SVt_PVGV));
218 if (PL_op->op_private & OPpLVAL_INTRO)
219 save_gp(MUTABLE_GV(sv), !(PL_op->op_flags & OPf_SPECIAL));
224 /* Helper function for pp_rv2sv and pp_rv2av */
226 Perl_softref2xv(pTHX_ SV *const sv, const char *const what,
227 const svtype type, SV ***spp)
232 PERL_ARGS_ASSERT_SOFTREF2XV;
234 if (PL_op->op_private & HINT_STRICT_REFS) {
236 Perl_die(aTHX_ S_no_symref_sv, sv, (SvPOK(sv) && SvCUR(sv)>32 ? "..." : ""), what);
238 Perl_die(aTHX_ PL_no_usym, what);
241 if (PL_op->op_flags & OPf_REF)
242 Perl_die(aTHX_ PL_no_usym, what);
243 if (ckWARN(WARN_UNINITIALIZED))
245 if (type != SVt_PV && GIMME_V == G_ARRAY) {
249 **spp = &PL_sv_undef;
252 if ((PL_op->op_flags & OPf_SPECIAL) &&
253 !(PL_op->op_flags & OPf_MOD))
255 gv = gv_fetchsv(sv, 0, type);
257 && (!is_gv_magical_sv(sv,0)
258 || !(gv = gv_fetchsv(sv, GV_ADD, type))))
260 **spp = &PL_sv_undef;
265 gv = gv_fetchsv(sv, GV_ADD, type);
275 if (!(PL_op->op_private & OPpDEREFed))
278 tryAMAGICunDEREF(to_sv);
281 switch (SvTYPE(sv)) {
287 DIE(aTHX_ "Not a SCALAR reference");
294 if (!isGV_with_GP(gv)) {
295 gv = Perl_softref2xv(aTHX_ sv, "a SCALAR", SVt_PV, &sp);
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));
306 sv = save_scalar(gv);
308 Perl_croak(aTHX_ "%s", PL_no_localize_ref);
310 else if (PL_op->op_private & OPpDEREF)
311 vivify_ref(sv, PL_op->op_private & OPpDEREF);
320 AV * const av = MUTABLE_AV(TOPs);
321 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
323 SV ** const sv = Perl_av_arylen_p(aTHX_ MUTABLE_AV(av));
325 *sv = newSV_type(SVt_PVMG);
326 sv_magic(*sv, MUTABLE_SV(av), PERL_MAGIC_arylen, NULL, 0);
330 SETs(sv_2mortal(newSViv(
331 AvFILL(MUTABLE_AV(av)) + CopARYBASE_get(PL_curcop)
341 if (PL_op->op_flags & OPf_MOD || LVRET) {
342 SV * const ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
343 sv_magic(ret, NULL, PERL_MAGIC_pos, NULL, 0);
345 LvTARG(ret) = SvREFCNT_inc_simple(sv);
346 PUSHs(ret); /* no SvSETMAGIC */
350 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
351 const MAGIC * const mg = mg_find(sv, PERL_MAGIC_regex_global);
352 if (mg && mg->mg_len >= 0) {
357 PUSHi(i + CopARYBASE_get(PL_curcop));
370 const I32 flags = (PL_op->op_flags & OPf_SPECIAL)
372 : ((PL_op->op_private & (OPpLVAL_INTRO|OPpMAY_RETURN_CONSTANT)) == OPpMAY_RETURN_CONSTANT)
375 /* We usually try to add a non-existent subroutine in case of AUTOLOAD. */
376 /* (But not in defined().) */
378 CV *cv = sv_2cv(TOPs, &stash_unused, &gv, flags);
381 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
382 if ((PL_op->op_private & OPpLVAL_INTRO)) {
383 if (gv && GvCV(gv) == cv && (gv = gv_autoload4(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv), FALSE)))
386 DIE(aTHX_ "Can't modify non-lvalue subroutine call");
389 else if ((flags == (GV_ADD|GV_NOEXPAND)) && gv && SvROK(gv)) {
393 cv = MUTABLE_CV(&PL_sv_undef);
394 SETs(MUTABLE_SV(cv));
404 SV *ret = &PL_sv_undef;
406 if (SvPOK(TOPs) && SvCUR(TOPs) >= 7) {
407 const char * s = SvPVX_const(TOPs);
408 if (strnEQ(s, "CORE::", 6)) {
409 const int code = keyword(s + 6, SvCUR(TOPs) - 6, 1);
410 if (code < 0) { /* Overridable. */
411 #define MAX_ARGS_OP ((sizeof(I32) - 1) * 2)
412 int i = 0, n = 0, seen_question = 0, defgv = 0;
414 char str[ MAX_ARGS_OP * 2 + 2 ]; /* One ';', one '\0' */
416 if (code == -KEY_chop || code == -KEY_chomp
417 || code == -KEY_exec || code == -KEY_system)
419 if (code == -KEY_mkdir) {
420 ret = newSVpvs_flags("_;$", SVs_TEMP);
423 if (code == -KEY_keys || code == -KEY_values || code == -KEY_each) {
424 ret = newSVpvs_flags("\\[@%]", SVs_TEMP);
427 if (code == -KEY_tied || code == -KEY_untie) {
428 ret = newSVpvs_flags("\\[$@%*]", SVs_TEMP);
431 if (code == -KEY_tie) {
432 ret = newSVpvs_flags("\\[$@%*]$@", SVs_TEMP);
435 if (code == -KEY_readpipe) {
436 s = "CORE::backtick";
438 while (i < MAXO) { /* The slow way. */
439 if (strEQ(s + 6, PL_op_name[i])
440 || strEQ(s + 6, PL_op_desc[i]))
446 goto nonesuch; /* Should not happen... */
448 defgv = PL_opargs[i] & OA_DEFGV;
449 oa = PL_opargs[i] >> OASHIFT;
451 if (oa & OA_OPTIONAL && !seen_question && !defgv) {
455 if ((oa & (OA_OPTIONAL - 1)) >= OA_AVREF
456 && (oa & (OA_OPTIONAL - 1)) <= OA_SCALARREF
457 /* But globs are already references (kinda) */
458 && (oa & (OA_OPTIONAL - 1)) != OA_FILEREF
462 str[n++] = ("?$@@%&*$")[oa & (OA_OPTIONAL - 1)];
465 if (defgv && str[n - 1] == '$')
468 ret = newSVpvn_flags(str, n - 1, SVs_TEMP);
470 else if (code) /* Non-Overridable */
472 else { /* None such */
474 DIE(aTHX_ "Can't find an opnumber for \"%s\"", s+6);
478 cv = sv_2cv(TOPs, &stash, &gv, 0);
480 ret = newSVpvn_flags(SvPVX_const(cv), SvCUR(cv), SVs_TEMP);
489 CV *cv = MUTABLE_CV(PAD_SV(PL_op->op_targ));
491 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
493 PUSHs(MUTABLE_SV(cv));
507 if (GIMME != G_ARRAY) {
511 *MARK = &PL_sv_undef;
512 *MARK = refto(*MARK);
516 EXTEND_MORTAL(SP - MARK);
518 *MARK = refto(*MARK);
523 S_refto(pTHX_ SV *sv)
528 PERL_ARGS_ASSERT_REFTO;
530 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
533 if (!(sv = LvTARG(sv)))
536 SvREFCNT_inc_void_NN(sv);
538 else if (SvTYPE(sv) == SVt_PVAV) {
539 if (!AvREAL((const AV *)sv) && AvREIFY((const AV *)sv))
540 av_reify(MUTABLE_AV(sv));
542 SvREFCNT_inc_void_NN(sv);
544 else if (SvPADTMP(sv) && !IS_PADGV(sv))
548 SvREFCNT_inc_void_NN(sv);
551 sv_upgrade(rv, SVt_IV);
561 SV * const sv = POPs;
567 if (!sv || !SvROK(sv))
570 pv = sv_reftype_len(SvRV(sv),TRUE,&len);
581 stash = CopSTASH(PL_curcop);
583 SV * const ssv = POPs;
587 if (ssv && !SvGMAGICAL(ssv) && !SvAMAGIC(ssv) && SvROK(ssv))
588 Perl_croak(aTHX_ "Attempt to bless into a reference");
589 ptr = SvPV_const(ssv,len);
591 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
592 "Explicit blessing to '' (assuming package main)");
593 stash = gv_stashpvn(ptr, len, GV_ADD);
596 (void)sv_bless(TOPs, stash);
605 const char * const elem = SvPV_nolen_const(sv);
606 GV * const gv = MUTABLE_GV(POPs);
611 /* elem will always be NUL terminated. */
612 const char * const second_letter = elem + 1;
615 if (strEQ(second_letter, "RRAY"))
616 tmpRef = MUTABLE_SV(GvAV(gv));
619 if (strEQ(second_letter, "ODE"))
620 tmpRef = MUTABLE_SV(GvCVu(gv));
623 if (strEQ(second_letter, "ILEHANDLE")) {
624 /* finally deprecated in 5.8.0 */
625 deprecate("*glob{FILEHANDLE}");
626 tmpRef = MUTABLE_SV(GvIOp(gv));
629 if (strEQ(second_letter, "ORMAT"))
630 tmpRef = MUTABLE_SV(GvFORM(gv));
633 if (strEQ(second_letter, "LOB"))
634 tmpRef = MUTABLE_SV(gv);
637 if (strEQ(second_letter, "ASH"))
638 tmpRef = MUTABLE_SV(GvHV(gv));
641 if (*second_letter == 'O' && !elem[2])
642 tmpRef = MUTABLE_SV(GvIOp(gv));
645 if (strEQ(second_letter, "AME"))
646 sv = newSVhek(GvNAME_HEK(gv));
649 if (strEQ(second_letter, "ACKAGE")) {
650 const HV * const stash = GvSTASH(gv);
651 const HEK * const hek = stash ? HvNAME_HEK(stash) : NULL;
652 sv = hek ? newSVhek(hek) : newSVpvs("__ANON__");
656 if (strEQ(second_letter, "CALAR"))
671 /* Pattern matching */
676 register unsigned char *s;
679 register I32 *sfirst;
683 if (sv == PL_lastscream) {
687 s = (unsigned char*)(SvPV(sv, len));
689 if (pos <= 0 || !SvPOK(sv) || SvUTF8(sv)) {
690 /* No point in studying a zero length string, and not safe to study
691 anything that doesn't appear to be a simple scalar (and hence might
692 change between now and when the regexp engine runs without our set
693 magic ever running) such as a reference to an object with overloaded
699 SvSCREAM_off(PL_lastscream);
700 SvREFCNT_dec(PL_lastscream);
702 PL_lastscream = SvREFCNT_inc_simple(sv);
704 s = (unsigned char*)(SvPV(sv, len));
708 if (pos > PL_maxscream) {
709 if (PL_maxscream < 0) {
710 PL_maxscream = pos + 80;
711 Newx(PL_screamfirst, 256, I32);
712 Newx(PL_screamnext, PL_maxscream, I32);
715 PL_maxscream = pos + pos / 4;
716 Renew(PL_screamnext, PL_maxscream, I32);
720 sfirst = PL_screamfirst;
721 snext = PL_screamnext;
723 if (!sfirst || !snext)
724 DIE(aTHX_ "do_study: out of memory");
726 for (ch = 256; ch; --ch)
731 register const I32 ch = s[pos];
733 snext[pos] = sfirst[ch] - pos;
740 /* piggyback on m//g magic */
741 sv_magic(sv, NULL, PERL_MAGIC_regex_global, NULL, 0);
750 if (PL_op->op_flags & OPf_STACKED)
752 else if (PL_op->op_private & OPpTARGET_MY)
758 TARG = sv_newmortal();
763 /* Lvalue operators. */
775 dVAR; dSP; dMARK; dTARGET; dORIGMARK;
777 do_chop(TARG, *++MARK);
786 SETi(do_chomp(TOPs));
792 dVAR; dSP; dMARK; dTARGET;
793 register I32 count = 0;
796 count += do_chomp(POPs);
806 if (!PL_op->op_private) {
815 SV_CHECK_THINKFIRST_COW_DROP(sv);
817 switch (SvTYPE(sv)) {
821 av_undef(MUTABLE_AV(sv));
824 hv_undef(MUTABLE_HV(sv));
827 if (cv_const_sv((const CV *)sv))
828 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Constant subroutine %s undefined",
829 CvANON((const CV *)sv) ? "(anonymous)"
830 : GvENAME(CvGV((const CV *)sv)));
834 /* let user-undef'd sub keep its identity */
835 GV* const gv = CvGV((const CV *)sv);
836 cv_undef(MUTABLE_CV(sv));
837 CvGV_set(MUTABLE_CV(sv), gv);
842 SvSetMagicSV(sv, &PL_sv_undef);
845 else if (isGV_with_GP(sv)) {
850 if((stash = GvHV((const GV *)sv)) && HvNAME_get(stash))
851 mro_isa_changed_in(stash);
852 /* undef *Pkg::meth_name ... */
853 else if(GvCVu((const GV *)sv) && (stash = GvSTASH((const GV *)sv))
854 && HvNAME_get(stash))
855 mro_method_changed_in(stash);
857 gp_free(MUTABLE_GV(sv));
859 GvGP(sv) = gp_ref(gp);
861 GvLINE(sv) = CopLINE(PL_curcop);
862 GvEGV(sv) = MUTABLE_GV(sv);
868 if (SvTYPE(sv) >= SVt_PV && SvPVX_const(sv) && SvLEN(sv)) {
883 if (SvTYPE(TOPs) >= SVt_PVAV || isGV_with_GP(TOPs))
884 Perl_croak_no_modify(aTHX);
885 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
886 && SvIVX(TOPs) != IV_MIN)
888 SvIV_set(TOPs, SvIVX(TOPs) - 1);
889 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
900 if (SvTYPE(TOPs) >= SVt_PVAV || isGV_with_GP(TOPs))
901 Perl_croak_no_modify(aTHX);
903 TARG = sv_newmortal();
904 sv_setsv(TARG, TOPs);
905 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
906 && SvIVX(TOPs) != IV_MAX)
908 SvIV_set(TOPs, SvIVX(TOPs) + 1);
909 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
914 /* special case for undef: see thread at 2003-03/msg00536.html in archive */
924 if (SvTYPE(TOPs) >= SVt_PVAV || isGV_with_GP(TOPs))
925 Perl_croak_no_modify(aTHX);
927 TARG = sv_newmortal();
928 sv_setsv(TARG, TOPs);
929 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
930 && SvIVX(TOPs) != IV_MIN)
932 SvIV_set(TOPs, SvIVX(TOPs) - 1);
933 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
942 /* Ordinary operators. */
946 dVAR; dSP; dATARGET; SV *svl, *svr;
947 #ifdef PERL_PRESERVE_IVUV
950 tryAMAGICbin_MG(pow_amg, AMGf_assign|AMGf_numeric);
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. */
958 SvIV_please_nomg(svr);
960 SvIV_please_nomg(svl);
969 const IV iv = SvIVX(svr);
973 goto float_it; /* Can't do negative powers this way. */
977 baseuok = SvUOK(svl);
981 const IV iv = SvIVX(svl);
984 baseuok = TRUE; /* effectively it's a UV now */
986 baseuv = -iv; /* abs, baseuok == false records sign */
989 /* now we have integer ** positive integer. */
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. */
1003 NV base = baseuok ? baseuv : -(NV)baseuv;
1008 while (power >>= 1) {
1016 SvIV_please_nomg(svr);
1019 register unsigned int highbit = 8 * sizeof(UV);
1020 register unsigned int diff = 8 * sizeof(UV);
1021 while (diff >>= 1) {
1023 if (baseuv >> highbit) {
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);
1037 while (power >>= 1) {
1044 if (baseuok || !odd_power)
1045 /* answer is positive */
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 */
1054 /* answer negative, doesn't fit */
1055 SETn( -(NV)result );
1065 NV right = SvNV_nomg(svr);
1066 NV left = SvNV_nomg(svl);
1069 #if defined(USE_LONG_DOUBLE) && defined(HAS_AIX_POWL_NEG_BASE_BUG)
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:
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
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:
1086 - if odd, powl(left, right) == -powl(-left, right)
1087 - if even, powl(left, right) == powl(-left, right)
1089 If the exponent is not an integer, the result is rightly NaNQ, so
1090 we just return that (as NV_NAN).
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 */
1103 SETn( Perl_pow( left, right) );
1106 SETn( Perl_pow( left, right) );
1107 #endif /* HAS_AIX_POWL_NEG_BASE_BUG */
1109 #ifdef PERL_PRESERVE_IVUV
1111 SvIV_please_nomg(svr);
1119 dVAR; dSP; dATARGET; SV *svl, *svr;
1120 tryAMAGICbin_MG(mult_amg, AMGf_assign|AMGf_numeric);
1123 #ifdef PERL_PRESERVE_IVUV
1124 SvIV_please_nomg(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);
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)));
1144 const IV aiv = SvIVX(svl);
1147 auvok = TRUE; /* effectively it's a UV now */
1149 alow = -aiv; /* abs, auvok == false records sign */
1155 const IV biv = SvIVX(svr);
1158 buvok = TRUE; /* effectively it's a UV now */
1160 blow = -biv; /* abs, buvok == false records sign */
1164 /* If this does sign extension on unsigned it's time for plan B */
1165 ahigh = alow >> (4 * sizeof (UV));
1167 bhigh = blow >> (4 * sizeof (UV));
1169 if (ahigh && bhigh) {
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. */
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 */
1186 SETi( -(IV)product );
1188 } /* else drop to NVs below. */
1190 /* One operand is large, 1 small */
1193 /* swap the operands */
1195 bhigh = blow; /* bhigh now the temp var for the swap */
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. */
1205 product_middle <<= (4 * sizeof (UV));
1206 product_low = alow * blow;
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. */
1217 SETu( product_low );
1219 } else if (product_low <= (UV)IV_MIN) {
1220 /* 2s complement assumption again */
1221 /* -ve result, which could overflow an IV */
1223 SETi( -(IV)product_low );
1225 } /* else drop to NVs below. */
1227 } /* product_middle too large */
1228 } /* ahigh && bhigh */
1233 NV right = SvNV_nomg(svr);
1234 NV left = SvNV_nomg(svl);
1236 SETn( left * right );
1243 dVAR; dSP; dATARGET; SV *svl, *svr;
1244 tryAMAGICbin_MG(div_amg, AMGf_assign|AMGf_numeric);
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
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. */
1258 # define PERL_TRY_UV_DIVIDE
1259 /* ensure that 20./5. == 4. */
1261 # ifdef PERL_PRESERVE_IVUV
1262 # ifndef NV_PRESERVES_UV
1263 # define PERL_TRY_UV_DIVIDE
1268 #ifdef PERL_TRY_UV_DIVIDE
1269 SvIV_please_nomg(svr);
1271 SvIV_please_nomg(svl);
1273 bool left_non_neg = SvUOK(svl);
1274 bool right_non_neg = SvUOK(svr);
1278 if (right_non_neg) {
1282 const IV biv = SvIVX(svr);
1285 right_non_neg = TRUE; /* effectively it's a UV now */
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. */
1297 DIE(aTHX_ "Illegal division by zero");
1303 const IV aiv = SvIVX(svl);
1306 left_non_neg = TRUE; /* effectively it's a UV now */
1315 /* For sloppy divide we always attempt integer division. */
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
1325 && (left > ((UV)1 << NV_PRESERVES_UV_BITS))
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. */
1337 /* 2s complement assumption */
1338 if (result <= (UV)IV_MIN)
1339 SETi( -(IV)result );
1341 /* It's exact but too negative for IV. */
1342 SETn( -(NV)result );
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 */
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)
1359 DIE(aTHX_ "Illegal division by zero");
1360 PUSHn( left / right );
1367 dVAR; dSP; dATARGET;
1368 tryAMAGICbin_MG(modulo_amg, AMGf_assign|AMGf_numeric);
1372 bool left_neg = FALSE;
1373 bool right_neg = FALSE;
1374 bool use_double = FALSE;
1375 bool dright_valid = FALSE;
1378 SV * const svr = TOPs;
1379 SV * const svl = TOPm1s;
1380 SvIV_please_nomg(svr);
1382 right_neg = !SvUOK(svr);
1386 const IV biv = SvIVX(svr);
1389 right_neg = FALSE; /* effectively it's a UV now */
1396 dright = SvNV_nomg(svr);
1397 right_neg = dright < 0;
1400 if (dright < UV_MAX_P1) {
1401 right = U_V(dright);
1402 dright_valid = TRUE; /* In case we need to use double below. */
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)) {
1414 left_neg = !SvUOK(svl);
1418 const IV aiv = SvIVX(svl);
1421 left_neg = FALSE; /* effectively it's a UV now */
1429 dleft = SvNV_nomg(svl);
1430 left_neg = dleft < 0;
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. */
1437 if (dleft < UV_MAX_P1) {
1438 /* right was in range, so is dleft, so use UVs not double.
1442 /* left is out of range for UV, right was in range, so promote
1443 right (back) to double. */
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);
1451 dright = Perl_floor(dright + 0.5);
1462 DIE(aTHX_ "Illegal modulus zero");
1464 dans = Perl_fmod(dleft, dright);
1465 if ((left_neg != right_neg) && dans)
1466 dans = dright - dans;
1469 sv_setnv(TARG, dans);
1475 DIE(aTHX_ "Illegal modulus zero");
1478 if ((left_neg != right_neg) && ans)
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);
1486 sv_setnv(TARG, -(NV)ans);
1489 sv_setuv(TARG, ans);
1498 dVAR; dSP; dATARGET;
1502 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1503 /* TODO: think of some way of doing list-repeat overloading ??? */
1508 tryAMAGICbin_MG(repeat_amg, AMGf_assign);
1514 const UV uv = SvUV_nomg(sv);
1516 count = IV_MAX; /* The best we can do? */
1520 const IV iv = SvIV_nomg(sv);
1527 else if (SvNOKp(sv)) {
1528 const NV nv = SvNV_nomg(sv);
1535 count = SvIV_nomg(sv);
1537 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
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;
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);
1551 /* This code was intended to fix 20010809.028:
1554 for (($x =~ /./g) x 2) {
1555 print chop; # "abcdabcd" expected as output.
1558 * but that change (#11635) broke this code:
1560 $x = [("foo")x2]; # only one "foo" ended up in the anonlist.
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.
1570 *SP = sv_2mortal(newSVsv(*SP));
1580 repeatcpy((char*)(MARK + items), (char*)MARK,
1581 items * sizeof(const SV *), count - 1);
1584 else if (count <= 0)
1587 else { /* Note: mark already snarfed by pp_list */
1588 SV * const tmpstr = POPs;
1591 static const char oom_string_extend[] =
1592 "Out of memory during string extend";
1595 sv_setsv_nomg(TARG, tmpstr);
1596 SvPV_force_nomg(TARG, len);
1597 isutf = DO_UTF8(TARG);
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);
1610 *SvEND(TARG) = '\0';
1613 (void)SvPOK_only_UTF8(TARG);
1615 (void)SvPOK_only(TARG);
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
1633 dVAR; dSP; dATARGET; bool useleft; SV *svl, *svr;
1634 tryAMAGICbin_MG(subtr_amg, AMGf_assign|AMGf_numeric);
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);
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;
1652 a_valid = auvok = 1;
1653 /* left operand is undef, treat as zero. */
1655 /* Left operand is defined, so is it IV? */
1656 SvIV_please_nomg(svl);
1658 if ((auvok = SvUOK(svl)))
1661 register const IV aiv = SvIVX(svl);
1664 auvok = 1; /* Now acting as a sign flag. */
1665 } else { /* 2s complement assumption for IV_MIN */
1673 bool result_good = 0;
1676 bool buvok = SvUOK(svr);
1681 register const IV biv = SvIVX(svr);
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
1695 all UV maths. negate result if A negative.
1696 subtract if signs same, add if signs differ. */
1698 if (auvok ^ buvok) {
1707 /* Must get smaller */
1712 if (result <= buv) {
1713 /* result really should be -(auv-buv). as its negation
1714 of true value, need to swap our result flag */
1726 if (result <= (UV)IV_MIN)
1727 SETi( -(IV)result );
1729 /* result valid, but out of range for IV. */
1730 SETn( -(NV)result );
1734 } /* Overflow, drop through to NVs. */
1739 NV value = SvNV_nomg(svr);
1743 /* left operand is undef, treat as zero - value */
1747 SETn( SvNV_nomg(svl) - value );
1754 dVAR; dSP; dATARGET; SV *svl, *svr;
1755 tryAMAGICbin_MG(lshift_amg, AMGf_assign);
1759 const IV shift = SvIV_nomg(svr);
1760 if (PL_op->op_private & HINT_INTEGER) {
1761 const IV i = SvIV_nomg(svl);
1765 const UV u = SvUV_nomg(svl);
1774 dVAR; dSP; dATARGET; SV *svl, *svr;
1775 tryAMAGICbin_MG(rshift_amg, AMGf_assign);
1779 const IV shift = SvIV_nomg(svr);
1780 if (PL_op->op_private & HINT_INTEGER) {
1781 const IV i = SvIV_nomg(svl);
1785 const UV u = SvUV_nomg(svl);
1795 tryAMAGICbin_MG(lt_amg, AMGf_set);
1796 #ifdef PERL_PRESERVE_IVUV
1797 SvIV_please_nomg(TOPs);
1799 SvIV_please_nomg(TOPm1s);
1800 if (SvIOK(TOPm1s)) {
1801 bool auvok = SvUOK(TOPm1s);
1802 bool buvok = SvUOK(TOPs);
1804 if (!auvok && !buvok) { /* ## IV < IV ## */
1805 const IV aiv = SvIVX(TOPm1s);
1806 const IV biv = SvIVX(TOPs);
1809 SETs(boolSV(aiv < biv));
1812 if (auvok && buvok) { /* ## UV < UV ## */
1813 const UV auv = SvUVX(TOPm1s);
1814 const UV buv = SvUVX(TOPs);
1817 SETs(boolSV(auv < buv));
1820 if (auvok) { /* ## UV < IV ## */
1822 const IV biv = SvIVX(TOPs);
1825 /* As (a) is a UV, it's >=0, so it cannot be < */
1830 SETs(boolSV(auv < (UV)biv));
1833 { /* ## IV < UV ## */
1834 const IV aiv = SvIVX(TOPm1s);
1838 /* As (b) is a UV, it's >=0, so it must be < */
1845 SETs(boolSV((UV)aiv < buv));
1851 #ifndef NV_PRESERVES_UV
1852 #ifdef PERL_PRESERVE_IVUV
1855 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
1857 SETs(boolSV(SvRV(TOPs) < SvRV(TOPp1s)));
1862 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
1864 if (Perl_isnan(left) || Perl_isnan(right))
1866 SETs(boolSV(left < right));
1869 SETs(boolSV(SvNV_nomg(TOPs) < value));
1878 tryAMAGICbin_MG(gt_amg, AMGf_set);
1879 #ifdef PERL_PRESERVE_IVUV
1880 SvIV_please_nomg(TOPs);
1882 SvIV_please_nomg(TOPm1s);
1883 if (SvIOK(TOPm1s)) {
1884 bool auvok = SvUOK(TOPm1s);
1885 bool buvok = SvUOK(TOPs);
1887 if (!auvok && !buvok) { /* ## IV > IV ## */
1888 const IV aiv = SvIVX(TOPm1s);
1889 const IV biv = SvIVX(TOPs);
1892 SETs(boolSV(aiv > biv));
1895 if (auvok && buvok) { /* ## UV > UV ## */
1896 const UV auv = SvUVX(TOPm1s);
1897 const UV buv = SvUVX(TOPs);
1900 SETs(boolSV(auv > buv));
1903 if (auvok) { /* ## UV > IV ## */
1905 const IV biv = SvIVX(TOPs);
1909 /* As (a) is a UV, it's >=0, so it must be > */
1914 SETs(boolSV(auv > (UV)biv));
1917 { /* ## IV > UV ## */
1918 const IV aiv = SvIVX(TOPm1s);
1922 /* As (b) is a UV, it's >=0, so it cannot be > */
1929 SETs(boolSV((UV)aiv > buv));
1935 #ifndef NV_PRESERVES_UV
1936 #ifdef PERL_PRESERVE_IVUV
1939 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
1941 SETs(boolSV(SvRV(TOPs) > SvRV(TOPp1s)));
1946 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
1948 if (Perl_isnan(left) || Perl_isnan(right))
1950 SETs(boolSV(left > right));
1953 SETs(boolSV(SvNV_nomg(TOPs) > value));
1962 tryAMAGICbin_MG(le_amg, AMGf_set);
1963 #ifdef PERL_PRESERVE_IVUV
1964 SvIV_please_nomg(TOPs);
1966 SvIV_please_nomg(TOPm1s);
1967 if (SvIOK(TOPm1s)) {
1968 bool auvok = SvUOK(TOPm1s);
1969 bool buvok = SvUOK(TOPs);
1971 if (!auvok && !buvok) { /* ## IV <= IV ## */
1972 const IV aiv = SvIVX(TOPm1s);
1973 const IV biv = SvIVX(TOPs);
1976 SETs(boolSV(aiv <= biv));
1979 if (auvok && buvok) { /* ## UV <= UV ## */
1980 UV auv = SvUVX(TOPm1s);
1981 UV buv = SvUVX(TOPs);
1984 SETs(boolSV(auv <= buv));
1987 if (auvok) { /* ## UV <= IV ## */
1989 const IV biv = SvIVX(TOPs);
1993 /* As (a) is a UV, it's >=0, so a cannot be <= */
1998 SETs(boolSV(auv <= (UV)biv));
2001 { /* ## IV <= UV ## */
2002 const IV aiv = SvIVX(TOPm1s);
2006 /* As (b) is a UV, it's >=0, so a must be <= */
2013 SETs(boolSV((UV)aiv <= buv));
2019 #ifndef NV_PRESERVES_UV
2020 #ifdef PERL_PRESERVE_IVUV
2023 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
2025 SETs(boolSV(SvRV(TOPs) <= SvRV(TOPp1s)));
2030 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
2032 if (Perl_isnan(left) || Perl_isnan(right))
2034 SETs(boolSV(left <= right));
2037 SETs(boolSV(SvNV_nomg(TOPs) <= value));
2046 tryAMAGICbin_MG(ge_amg,AMGf_set);
2047 #ifdef PERL_PRESERVE_IVUV
2048 SvIV_please_nomg(TOPs);
2050 SvIV_please_nomg(TOPm1s);
2051 if (SvIOK(TOPm1s)) {
2052 bool auvok = SvUOK(TOPm1s);
2053 bool buvok = SvUOK(TOPs);
2055 if (!auvok && !buvok) { /* ## IV >= IV ## */
2056 const IV aiv = SvIVX(TOPm1s);
2057 const IV biv = SvIVX(TOPs);
2060 SETs(boolSV(aiv >= biv));
2063 if (auvok && buvok) { /* ## UV >= UV ## */
2064 const UV auv = SvUVX(TOPm1s);
2065 const UV buv = SvUVX(TOPs);
2068 SETs(boolSV(auv >= buv));
2071 if (auvok) { /* ## UV >= IV ## */
2073 const IV biv = SvIVX(TOPs);
2077 /* As (a) is a UV, it's >=0, so it must be >= */
2082 SETs(boolSV(auv >= (UV)biv));
2085 { /* ## IV >= UV ## */
2086 const IV aiv = SvIVX(TOPm1s);
2090 /* As (b) is a UV, it's >=0, so a cannot be >= */
2097 SETs(boolSV((UV)aiv >= buv));
2103 #ifndef NV_PRESERVES_UV
2104 #ifdef PERL_PRESERVE_IVUV
2107 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
2109 SETs(boolSV(SvRV(TOPs) >= SvRV(TOPp1s)));
2114 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
2116 if (Perl_isnan(left) || Perl_isnan(right))
2118 SETs(boolSV(left >= right));
2121 SETs(boolSV(SvNV_nomg(TOPs) >= value));
2130 tryAMAGICbin_MG(ne_amg,AMGf_set);
2131 #ifndef NV_PRESERVES_UV
2132 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
2134 SETs(boolSV(SvRV(TOPs) != SvRV(TOPp1s)));
2138 #ifdef PERL_PRESERVE_IVUV
2139 SvIV_please_nomg(TOPs);
2141 SvIV_please_nomg(TOPm1s);
2142 if (SvIOK(TOPm1s)) {
2143 const bool auvok = SvUOK(TOPm1s);
2144 const bool buvok = SvUOK(TOPs);
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
2153 const UV buv = SvUVX(POPs);
2154 const UV auv = SvUVX(TOPs);
2156 SETs(boolSV(auv != buv));
2159 { /* ## Mixed IV,UV ## */
2163 /* != is commutative so swap if needed (save code) */
2165 /* swap. top of stack (b) is the iv */
2169 /* As (a) is a UV, it's >0, so it cannot be == */
2178 /* As (b) is a UV, it's >0, so it cannot be == */
2182 uv = SvUVX(*(SP+1)); /* Do I want TOPp1s() ? */
2184 SETs(boolSV((UV)iv != uv));
2191 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
2193 if (Perl_isnan(left) || Perl_isnan(right))
2195 SETs(boolSV(left != right));
2198 SETs(boolSV(SvNV_nomg(TOPs) != value));
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));
2216 #ifdef PERL_PRESERVE_IVUV
2217 /* Fortunately it seems NaN isn't IOK */
2218 SvIV_please_nomg(TOPs);
2220 SvIV_please_nomg(TOPm1s);
2221 if (SvIOK(TOPm1s)) {
2222 const bool leftuvok = SvUOK(TOPm1s);
2223 const bool rightuvok = SvUOK(TOPs);
2225 if (!leftuvok && !rightuvok) { /* ## IV <=> IV ## */
2226 const IV leftiv = SvIVX(TOPm1s);
2227 const IV rightiv = SvIVX(TOPs);
2229 if (leftiv > rightiv)
2231 else if (leftiv < rightiv)
2235 } else if (leftuvok && rightuvok) { /* ## UV <=> UV ## */
2236 const UV leftuv = SvUVX(TOPm1s);
2237 const UV rightuv = SvUVX(TOPs);
2239 if (leftuv > rightuv)
2241 else if (leftuv < rightuv)
2245 } else if (leftuvok) { /* ## UV <=> IV ## */
2246 const IV rightiv = SvIVX(TOPs);
2248 /* As (a) is a UV, it's >=0, so it cannot be < */
2251 const UV leftuv = SvUVX(TOPm1s);
2252 if (leftuv > (UV)rightiv) {
2254 } else if (leftuv < (UV)rightiv) {
2260 } else { /* ## IV <=> UV ## */
2261 const IV leftiv = SvIVX(TOPm1s);
2263 /* As (b) is a UV, it's >=0, so it must be < */
2266 const UV rightuv = SvUVX(TOPs);
2267 if ((UV)leftiv > rightuv) {
2269 } else if ((UV)leftiv < rightuv) {
2287 if (Perl_isnan(left) || Perl_isnan(right)) {
2291 value = (left > right) - (left < right);
2295 else if (left < right)
2297 else if (left > right)
2313 int amg_type = sle_amg;
2317 switch (PL_op->op_type) {
2336 tryAMAGICbin_MG(amg_type, AMGf_set);
2339 const int cmp = (IN_LOCALE_RUNTIME
2340 ? sv_cmp_locale(left, right)
2341 : sv_cmp(left, right));
2342 SETs(boolSV(cmp * multiplier < rhs));
2350 tryAMAGICbin_MG(seq_amg, AMGf_set);
2353 SETs(boolSV(sv_eq(left, right)));
2361 tryAMAGICbin_MG(sne_amg, AMGf_set);
2364 SETs(boolSV(!sv_eq(left, right)));
2372 tryAMAGICbin_MG(scmp_amg, 0);
2375 const int cmp = (IN_LOCALE_RUNTIME
2376 ? sv_cmp_locale(left, right)
2377 : sv_cmp(left, right));
2385 dVAR; dSP; dATARGET;
2386 tryAMAGICbin_MG(band_amg, AMGf_assign);
2389 if (SvNIOKp(left) || SvNIOKp(right)) {
2390 if (PL_op->op_private & HINT_INTEGER) {
2391 const IV i = SvIV_nomg(left) & SvIV_nomg(right);
2395 const UV u = SvUV_nomg(left) & SvUV_nomg(right);
2400 do_vop(PL_op->op_type, TARG, left, right);
2409 dVAR; dSP; dATARGET;
2410 const int op_type = PL_op->op_type;
2412 tryAMAGICbin_MG((op_type == OP_BIT_OR ? bor_amg : bxor_amg), AMGf_assign);
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);
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);
2430 do_vop(op_type, TARG, left, right);
2440 tryAMAGICun_MG(neg_amg, AMGf_numeric);
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 */
2448 if (SvIVX(sv) == IV_MIN) {
2449 /* 2s complement assumption. */
2450 SETi(SvIVX(sv)); /* special case: -((UV)IV_MAX+1) == IV_MIN */
2453 else if (SvUVX(sv) <= IV_MAX) {
2458 else if (SvIVX(sv) != IV_MIN) {
2462 #ifdef PERL_PRESERVE_IVUV
2470 SETn(-SvNV_nomg(sv));
2471 else if (SvPOKp(sv)) {
2473 const char * const s = SvPV_nomg_const(sv, len);
2474 if (isIDFIRST(*s)) {
2475 sv_setpvs(TARG, "-");
2478 else if (*s == '+' || *s == '-') {
2479 sv_setsv_nomg(TARG, sv);
2480 *SvPV_force_nomg(TARG, len) = *s == '-' ? '+' : '-';
2482 else if (DO_UTF8(sv)) {
2483 SvIV_please_nomg(sv);
2485 goto oops_its_an_int;
2487 sv_setnv(TARG, -SvNV_nomg(sv));
2489 sv_setpvs(TARG, "-");
2494 SvIV_please_nomg(sv);
2496 goto oops_its_an_int;
2497 sv_setnv(TARG, -SvNV_nomg(sv));
2502 SETn(-SvNV_nomg(sv));
2510 tryAMAGICun_MG(not_amg, AMGf_set);
2511 *PL_stack_sp = boolSV(!SvTRUE(*PL_stack_sp));
2518 tryAMAGICun_MG(compl_amg, 0);
2522 if (PL_op->op_private & HINT_INTEGER) {
2523 const IV i = ~SvIV_nomg(sv);
2527 const UV u = ~SvUV_nomg(sv);
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);
2541 /* Calculate exact length, let's not estimate. */
2546 U8 * const send = tmps + len;
2547 U8 * const origtmps = tmps;
2548 const UV utf8flags = UTF8_ALLOW_ANYUV;
2550 while (tmps < send) {
2551 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2553 targlen += UNISKIP(~c);
2559 /* Now rewind strings and write them. */
2566 Newx(result, targlen + 1, U8);
2568 while (tmps < send) {
2569 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2571 p = uvchr_to_utf8_flags(p, ~c, UNICODE_ALLOW_ANY);
2574 sv_usepvn_flags(TARG, (char*)result, targlen,
2575 SV_HAS_TRAILING_NUL);
2582 Newx(result, nchar + 1, U8);
2584 while (tmps < send) {
2585 const U8 c = (U8)utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2590 sv_usepvn_flags(TARG, (char*)result, nchar, SV_HAS_TRAILING_NUL);
2598 register long *tmpl;
2599 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
2602 for ( ; anum >= (I32)sizeof(long); anum -= (I32)sizeof(long), tmpl++)
2607 for ( ; anum > 0; anum--, tmps++)
2615 /* integer versions of some of the above */
2619 dVAR; dSP; dATARGET;
2620 tryAMAGICbin_MG(mult_amg, AMGf_assign);
2623 SETi( left * right );
2631 dVAR; dSP; dATARGET;
2632 tryAMAGICbin_MG(div_amg, AMGf_assign);
2635 IV value = SvIV_nomg(right);
2637 DIE(aTHX_ "Illegal division by zero");
2638 num = SvIV_nomg(left);
2640 /* avoid FPE_INTOVF on some platforms when num is IV_MIN */
2644 value = num / value;
2650 #if defined(__GLIBC__) && IVSIZE == 8
2657 /* This is the vanilla old i_modulo. */
2658 dVAR; dSP; dATARGET;
2659 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2663 DIE(aTHX_ "Illegal modulus zero");
2664 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2668 SETi( left % right );
2673 #if defined(__GLIBC__) && IVSIZE == 8
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);
2686 DIE(aTHX_ "Illegal modulus zero");
2687 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2691 SETi( left % PERL_ABS(right) );
2698 dVAR; dSP; dATARGET;
2699 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2703 DIE(aTHX_ "Illegal modulus zero");
2704 /* The assumption is to use hereafter the old vanilla version... */
2706 PL_ppaddr[OP_I_MODULO] =
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. */
2718 /* Cannot do this check with inlined IV constants since
2719 * that seems to work correctly even with the buggy glibc. */
2721 /* Yikes, we have the bug.
2722 * Patch in the workaround version. */
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);
2730 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2734 SETi( left % right );
2742 dVAR; dSP; dATARGET;
2743 tryAMAGICbin_MG(add_amg, AMGf_assign);
2745 dPOPTOPiirl_ul_nomg;
2746 SETi( left + right );
2753 dVAR; dSP; dATARGET;
2754 tryAMAGICbin_MG(subtr_amg, AMGf_assign);
2756 dPOPTOPiirl_ul_nomg;
2757 SETi( left - right );
2765 tryAMAGICbin_MG(lt_amg, AMGf_set);
2768 SETs(boolSV(left < right));
2776 tryAMAGICbin_MG(gt_amg, AMGf_set);
2779 SETs(boolSV(left > right));
2787 tryAMAGICbin_MG(le_amg, AMGf_set);
2790 SETs(boolSV(left <= right));
2798 tryAMAGICbin_MG(ge_amg, AMGf_set);
2801 SETs(boolSV(left >= right));
2809 tryAMAGICbin_MG(eq_amg, AMGf_set);
2812 SETs(boolSV(left == right));
2820 tryAMAGICbin_MG(ne_amg, AMGf_set);
2823 SETs(boolSV(left != right));
2831 tryAMAGICbin_MG(ncmp_amg, 0);
2838 else if (left < right)
2850 tryAMAGICun_MG(neg_amg, 0);
2852 SV * const sv = TOPs;
2853 IV const i = SvIV_nomg(sv);
2859 /* High falutin' math. */
2864 tryAMAGICbin_MG(atan2_amg, 0);
2867 SETn(Perl_atan2(left, right));
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;
2895 amg_type = sqrt_amg;
2897 neg_report = "sqrt";
2902 tryAMAGICun_MG(amg_type, 0);
2904 SV * const arg = POPs;
2905 const NV value = SvNV_nomg(arg);
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);
2912 XPUSHn(func(value));
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
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
2928 #ifndef HAS_DRAND48_PROTO
2929 extern double drand48 (void);
2942 if (!PL_srand_called) {
2943 (void)seedDrand01((Rand_seed_t)seed());
2944 PL_srand_called = TRUE;
2954 const UV anum = (MAXARG < 1) ? seed() : POPu;
2955 (void)seedDrand01((Rand_seed_t)anum);
2956 PL_srand_called = TRUE;
2960 /* Historically srand always returned true. We can avoid breaking
2962 sv_setpvs(TARG, "0 but true");
2971 tryAMAGICun_MG(int_amg, AMGf_numeric);
2973 SV * const sv = TOPs;
2974 const IV iv = SvIV_nomg(sv);
2975 /* XXX it's arguable that compiler casting to IV might be subtly
2976 different from modf (for numbers inside (IV_MIN,UV_MAX)) in which
2977 else preferring IV has introduced a subtle behaviour change bug. OTOH
2978 relying on floating point to be accurate is a bug. */
2983 else if (SvIOK(sv)) {
2985 SETu(SvUV_nomg(sv));
2990 const NV value = SvNV_nomg(sv);
2992 if (value < (NV)UV_MAX + 0.5) {
2995 SETn(Perl_floor(value));
2999 if (value > (NV)IV_MIN - 0.5) {
3002 SETn(Perl_ceil(value));
3013 tryAMAGICun_MG(abs_amg, AMGf_numeric);
3015 SV * const sv = TOPs;
3016 /* This will cache the NV value if string isn't actually integer */
3017 const IV iv = SvIV_nomg(sv);
3022 else if (SvIOK(sv)) {
3023 /* IVX is precise */
3025 SETu(SvUV_nomg(sv)); /* force it to be numeric only */
3033 /* 2s complement assumption. Also, not really needed as
3034 IV_MIN and -IV_MIN should both be %100...00 and NV-able */
3040 const NV value = SvNV_nomg(sv);
3054 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES;
3058 SV* const sv = POPs;
3060 tmps = (SvPV_const(sv, len));
3062 /* If Unicode, try to downgrade
3063 * If not possible, croak. */
3064 SV* const tsv = sv_2mortal(newSVsv(sv));
3067 sv_utf8_downgrade(tsv, FALSE);
3068 tmps = SvPV_const(tsv, len);
3070 if (PL_op->op_type == OP_HEX)
3073 while (*tmps && len && isSPACE(*tmps))
3077 if (*tmps == 'x' || *tmps == 'X') {
3079 result_uv = grok_hex (tmps, &len, &flags, &result_nv);
3081 else if (*tmps == 'b' || *tmps == 'B')
3082 result_uv = grok_bin (tmps, &len, &flags, &result_nv);
3084 result_uv = grok_oct (tmps, &len, &flags, &result_nv);
3086 if (flags & PERL_SCAN_GREATER_THAN_UV_MAX) {
3100 SV * const sv = TOPs;
3102 if (SvGAMAGIC(sv)) {
3103 /* For an overloaded or magic scalar, we can't know in advance if
3104 it's going to be UTF-8 or not. Also, we can't call sv_len_utf8 as
3105 it likes to cache the length. Maybe that should be a documented
3110 = sv_2pv_flags(sv, &len,
3111 SV_UNDEF_RETURNS_NULL|SV_CONST_RETURN|SV_GMAGIC);
3114 sv_setsv(TARG, &PL_sv_undef);
3117 else if (DO_UTF8(sv)) {
3118 SETi(utf8_length((U8*)p, (U8*)p + len));
3122 } else if (SvOK(sv)) {
3123 /* Neither magic nor overloaded. */
3125 SETi(sv_len_utf8(sv));
3129 sv_setsv_nomg(TARG, &PL_sv_undef);
3149 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
3151 const IV arybase = CopARYBASE_get(PL_curcop);
3153 const char *repl = NULL;
3155 const int num_args = PL_op->op_private & 7;
3156 bool repl_need_utf8_upgrade = FALSE;
3157 bool repl_is_utf8 = FALSE;
3162 repl = SvPV_const(repl_sv, repl_len);
3163 repl_is_utf8 = DO_UTF8(repl_sv) && SvCUR(repl_sv);
3166 len_iv = SvIV(len_sv);
3167 len_is_uv = SvIOK_UV(len_sv);
3170 pos1_iv = SvIV(pos_sv);
3171 pos1_is_uv = SvIOK_UV(pos_sv);
3177 sv_utf8_upgrade(sv);
3179 else if (DO_UTF8(sv))
3180 repl_need_utf8_upgrade = TRUE;
3182 tmps = SvPV_const(sv, curlen);
3184 utf8_curlen = sv_len_utf8(sv);
3185 if (utf8_curlen == curlen)
3188 curlen = utf8_curlen;
3193 if ( (pos1_is_uv && arybase < 0) || (pos1_iv >= arybase) ) { /* pos >= $[ */
3194 UV pos1_uv = pos1_iv-arybase;
3195 /* Overflow can occur when $[ < 0 */
3196 if (arybase < 0 && pos1_uv < (UV)pos1_iv)
3201 else if (pos1_is_uv ? (UV)pos1_iv > 0 : pos1_iv > 0) {
3202 goto bound_fail; /* $[=3; substr($_,2,...) */
3204 else { /* pos < $[ */
3205 if (pos1_iv == 0) { /* $[=1; substr($_,0,...) */
3210 pos1_is_uv = curlen-1 > ~(UV)pos1_iv;
3215 if (pos1_is_uv || pos1_iv > 0) {
3216 if ((UV)pos1_iv > curlen)
3221 if (!len_is_uv && len_iv < 0) {
3222 pos2_iv = curlen + len_iv;
3224 pos2_is_uv = curlen-1 > ~(UV)len_iv;
3227 } else { /* len_iv >= 0 */
3228 if (!pos1_is_uv && pos1_iv < 0) {
3229 pos2_iv = pos1_iv + len_iv;
3230 pos2_is_uv = (UV)len_iv > (UV)IV_MAX;
3232 if ((UV)len_iv > curlen-(UV)pos1_iv)
3235 pos2_iv = pos1_iv+len_iv;
3245 if (!pos2_is_uv && pos2_iv < 0) {
3246 if (!pos1_is_uv && pos1_iv < 0)
3250 else if (!pos1_is_uv && pos1_iv < 0)
3253 if ((UV)pos2_iv < (UV)pos1_iv)
3255 if ((UV)pos2_iv > curlen)
3259 /* pos1_iv and pos2_iv both in 0..curlen, so the cast is safe */
3260 const STRLEN pos = (STRLEN)( (UV)pos1_iv );
3261 const STRLEN len = (STRLEN)( (UV)pos2_iv - (UV)pos1_iv );
3262 STRLEN byte_len = len;
3263 STRLEN byte_pos = utf8_curlen
3264 ? sv_pos_u2b_flags(sv, pos, &byte_len, SV_CONST_RETURN) : pos;
3266 if (lvalue && !repl) {
3269 if (!SvGMAGICAL(sv)) {
3271 SvPV_force_nolen(sv);
3272 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR),
3273 "Attempt to use reference as lvalue in substr");
3275 if (isGV_with_GP(sv))
3276 SvPV_force_nolen(sv);
3277 else if (SvOK(sv)) /* is it defined ? */
3278 (void)SvPOK_only_UTF8(sv);
3280 sv_setpvs(sv, ""); /* avoid lexical reincarnation */
3283 ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
3284 sv_magic(ret, NULL, PERL_MAGIC_substr, NULL, 0);
3286 LvTARG(ret) = SvREFCNT_inc_simple(sv);
3287 LvTARGOFF(ret) = pos;
3288 LvTARGLEN(ret) = len;
3291 PUSHs(ret); /* avoid SvSETMAGIC here */
3295 SvTAINTED_off(TARG); /* decontaminate */
3296 SvUTF8_off(TARG); /* decontaminate */
3299 sv_setpvn(TARG, tmps, byte_len);
3300 #ifdef USE_LOCALE_COLLATE
3301 sv_unmagic(TARG, PERL_MAGIC_collxfrm);
3307 SV* repl_sv_copy = NULL;
3309 if (repl_need_utf8_upgrade) {
3310 repl_sv_copy = newSVsv(repl_sv);
3311 sv_utf8_upgrade(repl_sv_copy);
3312 repl = SvPV_const(repl_sv_copy, repl_len);
3313 repl_is_utf8 = DO_UTF8(repl_sv_copy) && SvCUR(sv);
3317 sv_insert_flags(sv, byte_pos, byte_len, repl, repl_len, 0);
3320 SvREFCNT_dec(repl_sv_copy);
3324 PUSHs(TARG); /* avoid SvSETMAGIC here */
3329 Perl_croak(aTHX_ "substr outside of string");
3330 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR), "substr outside of string");
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;
3343 if (lvalue) { /* it's an lvalue! */
3344 ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
3345 sv_magic(ret, NULL, PERL_MAGIC_vec, NULL, 0);
3347 LvTARG(ret) = SvREFCNT_inc_simple(src);
3348 LvTARGOFF(ret) = offset;
3349 LvTARGLEN(ret) = size;
3353 SvTAINTED_off(TARG); /* decontaminate */
3357 sv_setuv(ret, do_vecget(src, offset, size));
3373 const char *little_p;
3374 const I32 arybase = CopARYBASE_get(PL_curcop);
3377 const bool is_index = PL_op->op_type == OP_INDEX;
3380 /* arybase is in characters, like offset, so combine prior to the
3381 UTF-8 to bytes calculation. */
3382 offset = POPi - arybase;
3386 big_p = SvPV_const(big, biglen);
3387 little_p = SvPV_const(little, llen);
3389 big_utf8 = DO_UTF8(big);
3390 little_utf8 = DO_UTF8(little);
3391 if (big_utf8 ^ little_utf8) {
3392 /* One needs to be upgraded. */
3393 if (little_utf8 && !PL_encoding) {
3394 /* Well, maybe instead we might be able to downgrade the small
3396 char * const pv = (char*)bytes_from_utf8((U8 *)little_p, &llen,
3399 /* If the large string is ISO-8859-1, and it's not possible to
3400 convert the small string to ISO-8859-1, then there is no
3401 way that it could be found anywhere by index. */
3406 /* At this point, pv is a malloc()ed string. So donate it to temp
3407 to ensure it will get free()d */
3408 little = temp = newSV(0);
3409 sv_usepvn(temp, pv, llen);
3410 little_p = SvPVX(little);
3413 ? newSVpvn(big_p, biglen) : newSVpvn(little_p, llen);
3416 sv_recode_to_utf8(temp, PL_encoding);
3418 sv_utf8_upgrade(temp);
3423 big_p = SvPV_const(big, biglen);
3426 little_p = SvPV_const(little, llen);
3430 if (SvGAMAGIC(big)) {
3431 /* Life just becomes a lot easier if I use a temporary here.
3432 Otherwise I need to avoid calls to sv_pos_u2b(), which (dangerously)
3433 will trigger magic and overloading again, as will fbm_instr()
3435 big = newSVpvn_flags(big_p, biglen,
3436 SVs_TEMP | (big_utf8 ? SVf_UTF8 : 0));
3439 if (SvGAMAGIC(little) || (is_index && !SvOK(little))) {
3440 /* index && SvOK() is a hack. fbm_instr() calls SvPV_const, which will
3441 warn on undef, and we've already triggered a warning with the
3442 SvPV_const some lines above. We can't remove that, as we need to
3443 call some SvPV to trigger overloading early and find out if the
3445 This is all getting to messy. The API isn't quite clean enough,
3446 because data access has side effects.
3448 little = newSVpvn_flags(little_p, llen,
3449 SVs_TEMP | (little_utf8 ? SVf_UTF8 : 0));
3450 little_p = SvPVX(little);
3454 offset = is_index ? 0 : biglen;
3456 if (big_utf8 && offset > 0)
3457 sv_pos_u2b(big, &offset, 0);
3463 else if (offset > (I32)biglen)
3465 if (!(little_p = is_index
3466 ? fbm_instr((unsigned char*)big_p + offset,
3467 (unsigned char*)big_p + biglen, little, 0)
3468 : rninstr(big_p, big_p + offset,
3469 little_p, little_p + llen)))
3472 retval = little_p - big_p;
3473 if (retval > 0 && big_utf8)
3474 sv_pos_b2u(big, &retval);
3478 PUSHi(retval + arybase);
3484 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
3485 if (SvTAINTED(MARK[1]))
3486 TAINT_PROPER("sprintf");
3487 SvTAINTED_off(TARG);
3488 do_sprintf(TARG, SP-MARK, MARK+1);
3489 TAINT_IF(SvTAINTED(TARG));
3501 const U8 *s = (U8*)SvPV_const(argsv, len);
3503 if (PL_encoding && SvPOK(argsv) && !DO_UTF8(argsv)) {
3504 SV * const tmpsv = sv_2mortal(newSVsv(argsv));
3505 s = (U8*)sv_recode_to_utf8(tmpsv, PL_encoding);
3509 XPUSHu(DO_UTF8(argsv) ?
3510 utf8n_to_uvchr(s, UTF8_MAXBYTES, 0, UTF8_ALLOW_ANYUV) :
3522 if (((SvIOK_notUV(TOPs) && SvIV(TOPs) < 0)
3524 (SvNOK(TOPs) && SvNV(TOPs) < 0.0))) {
3526 value = POPu; /* chr(-1) eq chr(0xff), etc. */
3528 (void) POPs; /* Ignore the argument value. */
3529 value = UNICODE_REPLACEMENT;
3535 SvUPGRADE(TARG,SVt_PV);
3537 if (value > 255 && !IN_BYTES) {
3538 SvGROW(TARG, (STRLEN)UNISKIP(value)+1);
3539 tmps = (char*)uvchr_to_utf8_flags((U8*)SvPVX(TARG), value, 0);
3540 SvCUR_set(TARG, tmps - SvPVX_const(TARG));
3542 (void)SvPOK_only(TARG);
3551 *tmps++ = (char)value;
3553 (void)SvPOK_only(TARG);
3555 if (PL_encoding && !IN_BYTES) {
3556 sv_recode_to_utf8(TARG, PL_encoding);
3558 if (SvCUR(TARG) == 0 || !is_utf8_string((U8*)tmps, SvCUR(TARG)) ||
3559 UNICODE_IS_REPLACEMENT(utf8_to_uvchr((U8*)tmps, NULL))) {
3563 *tmps++ = (char)value;
3579 const char *tmps = SvPV_const(left, len);
3581 if (DO_UTF8(left)) {
3582 /* If Unicode, try to downgrade.
3583 * If not possible, croak.
3584 * Yes, we made this up. */
3585 SV* const tsv = sv_2mortal(newSVsv(left));
3588 sv_utf8_downgrade(tsv, FALSE);
3589 tmps = SvPV_const(tsv, len);
3591 # ifdef USE_ITHREADS
3593 if (!PL_reentrant_buffer->_crypt_struct_buffer) {
3594 /* This should be threadsafe because in ithreads there is only
3595 * one thread per interpreter. If this would not be true,
3596 * we would need a mutex to protect this malloc. */
3597 PL_reentrant_buffer->_crypt_struct_buffer =
3598 (struct crypt_data *)safemalloc(sizeof(struct crypt_data));
3599 #if defined(__GLIBC__) || defined(__EMX__)
3600 if (PL_reentrant_buffer->_crypt_struct_buffer) {
3601 PL_reentrant_buffer->_crypt_struct_buffer->initialized = 0;
3602 /* work around glibc-2.2.5 bug */
3603 PL_reentrant_buffer->_crypt_struct_buffer->current_saltbits = 0;
3607 # endif /* HAS_CRYPT_R */
3608 # endif /* USE_ITHREADS */
3610 sv_setpv(TARG, fcrypt(tmps, SvPV_nolen_const(right)));
3612 sv_setpv(TARG, PerlProc_crypt(tmps, SvPV_nolen_const(right)));
3618 "The crypt() function is unimplemented due to excessive paranoia.");
3622 /* Generally UTF-8 and UTF-EBCDIC are indistinguishable at this level. So
3623 * most comments below say UTF-8, when in fact they mean UTF-EBCDIC as well */
3625 /* Both the characters below can be stored in two UTF-8 bytes. In UTF-8 the max
3626 * character that 2 bytes can hold is U+07FF, and in UTF-EBCDIC it is U+03FF.
3627 * See http://www.unicode.org/unicode/reports/tr16 */
3628 #define LATIN_CAPITAL_LETTER_Y_WITH_DIAERESIS 0x0178 /* Also is title case */
3629 #define GREEK_CAPITAL_LETTER_MU 0x039C /* Upper and title case of MICRON */
3631 /* Below are several macros that generate code */
3632 /* Generates code to store a unicode codepoint c that is known to occupy
3633 * exactly two UTF-8 and UTF-EBCDIC bytes; it is stored into p and p+1. */
3634 #define STORE_UNI_TO_UTF8_TWO_BYTE(p, c) \
3636 *(p) = UTF8_TWO_BYTE_HI(c); \
3637 *((p)+1) = UTF8_TWO_BYTE_LO(c); \
3640 /* Like STORE_UNI_TO_UTF8_TWO_BYTE, but advances p to point to the next
3641 * available byte after the two bytes */
3642 #define CAT_UNI_TO_UTF8_TWO_BYTE(p, c) \
3644 *(p)++ = UTF8_TWO_BYTE_HI(c); \
3645 *((p)++) = UTF8_TWO_BYTE_LO(c); \
3648 /* Generates code to store the upper case of latin1 character l which is known
3649 * to have its upper case be non-latin1 into the two bytes p and p+1. There
3650 * are only two characters that fit this description, and this macro knows
3651 * about them, and that the upper case values fit into two UTF-8 or UTF-EBCDIC
3653 #define STORE_NON_LATIN1_UC(p, l) \
3655 if ((l) == LATIN_SMALL_LETTER_Y_WITH_DIAERESIS) { \
3656 STORE_UNI_TO_UTF8_TWO_BYTE((p), LATIN_CAPITAL_LETTER_Y_WITH_DIAERESIS); \
3657 } else { /* Must be the following letter */ \
3658 STORE_UNI_TO_UTF8_TWO_BYTE((p), GREEK_CAPITAL_LETTER_MU); \
3662 /* Like STORE_NON_LATIN1_UC, but advances p to point to the next available byte
3663 * after the character stored */
3664 #define CAT_NON_LATIN1_UC(p, l) \
3666 if ((l) == LATIN_SMALL_LETTER_Y_WITH_DIAERESIS) { \
3667 CAT_UNI_TO_UTF8_TWO_BYTE((p), LATIN_CAPITAL_LETTER_Y_WITH_DIAERESIS); \
3669 CAT_UNI_TO_UTF8_TWO_BYTE((p), GREEK_CAPITAL_LETTER_MU); \
3673 /* Generates code to add the two UTF-8 bytes (probably u) that are the upper
3674 * case of l into p and p+1. u must be the result of toUPPER_LATIN1_MOD(l),
3675 * and must require two bytes to store it. Advances p to point to the next
3676 * available position */
3677 #define CAT_TWO_BYTE_UNI_UPPER_MOD(p, l, u) \
3679 if ((u) != LATIN_SMALL_LETTER_Y_WITH_DIAERESIS) { \
3680 CAT_UNI_TO_UTF8_TWO_BYTE((p), (u)); /* not special, just save it */ \
3681 } else if (l == LATIN_SMALL_LETTER_SHARP_S) { \
3682 *(p)++ = 'S'; *(p)++ = 'S'; /* upper case is 'SS' */ \
3683 } else {/* else is one of the other two special cases */ \
3684 CAT_NON_LATIN1_UC((p), (l)); \
3690 /* Actually is both lcfirst() and ucfirst(). Only the first character
3691 * changes. This means that possibly we can change in-place, ie., just
3692 * take the source and change that one character and store it back, but not
3693 * if read-only etc, or if the length changes */
3698 STRLEN slen; /* slen is the byte length of the whole SV. */
3701 bool inplace; /* ? Convert first char only, in-place */
3702 bool doing_utf8 = FALSE; /* ? using utf8 */
3703 bool convert_source_to_utf8 = FALSE; /* ? need to convert */
3704 const int op_type = PL_op->op_type;
3707 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
3708 STRLEN ulen; /* ulen is the byte length of the original Unicode character
3709 * stored as UTF-8 at s. */
3710 STRLEN tculen; /* tculen is the byte length of the freshly titlecased (or
3711 * lowercased) character stored in tmpbuf. May be either
3712 * UTF-8 or not, but in either case is the number of bytes */
3716 s = (const U8*)SvPV_nomg_const(source, slen);
3718 if (ckWARN(WARN_UNINITIALIZED))
3719 report_uninit(source);
3724 /* We may be able to get away with changing only the first character, in
3725 * place, but not if read-only, etc. Later we may discover more reasons to
3726 * not convert in-place. */
3727 inplace = SvPADTMP(source) && !SvREADONLY(source) && SvTEMP(source);
3729 /* First calculate what the changed first character should be. This affects
3730 * whether we can just swap it out, leaving the rest of the string unchanged,
3731 * or even if have to convert the dest to UTF-8 when the source isn't */
3733 if (! slen) { /* If empty */
3734 need = 1; /* still need a trailing NUL */
3736 else if (DO_UTF8(source)) { /* Is the source utf8? */
3739 /* TODO: This is #ifdefd out because it has hard-coded the standard mappings,
3740 * and doesn't allow for the user to specify their own. When code is added to
3741 * detect if there is a user-defined mapping in force here, and if so to use
3742 * that, then the code below can be compiled. The detection would be a good
3743 * thing anyway, as currently the user-defined mappings only work on utf8
3744 * strings, and thus depend on the chosen internal storage method, which is a
3746 #ifdef GO_AHEAD_AND_BREAK_USER_DEFINED_CASE_MAPPINGS
3747 if (UTF8_IS_INVARIANT(*s)) {
3749 /* An invariant source character is either ASCII or, in EBCDIC, an
3750 * ASCII equivalent or a caseless C1 control. In both these cases,
3751 * the lower and upper cases of any character are also invariants
3752 * (and title case is the same as upper case). So it is safe to
3753 * use the simple case change macros which avoid the overhead of
3754 * the general functions. Note that if perl were to be extended to
3755 * do locale handling in UTF-8 strings, this wouldn't be true in,
3756 * for example, Lithuanian or Turkic. */
3757 *tmpbuf = (op_type == OP_LCFIRST) ? toLOWER(*s) : toUPPER(*s);
3761 else if (UTF8_IS_DOWNGRADEABLE_START(*s)) {
3764 /* Similarly, if the source character isn't invariant but is in the
3765 * latin1 range (or EBCDIC equivalent thereof), we have the case
3766 * changes compiled into perl, and can avoid the overhead of the
3767 * general functions. In this range, the characters are stored as
3768 * two UTF-8 bytes, and it so happens that any changed-case version
3769 * is also two bytes (in both ASCIIish and EBCDIC machines). */
3773 /* Convert the two source bytes to a single Unicode code point
3774 * value, change case and save for below */
3775 chr = UTF8_ACCUMULATE(*s, *(s+1));
3776 if (op_type == OP_LCFIRST) { /* lower casing is easy */
3777 U8 lower = toLOWER_LATIN1(chr);
3778 STORE_UNI_TO_UTF8_TWO_BYTE(tmpbuf, lower);
3780 else { /* ucfirst */
3781 U8 upper = toUPPER_LATIN1_MOD(chr);
3783 /* Most of the latin1 range characters are well-behaved. Their
3784 * title and upper cases are the same, and are also in the
3785 * latin1 range. The macro above returns their upper (hence
3786 * title) case, and all that need be done is to save the result
3787 * for below. However, several characters are problematic, and
3788 * have to be handled specially. The MOD in the macro name
3789 * above means that these tricky characters all get mapped to
3790 * the single character LATIN_SMALL_LETTER_Y_WITH_DIAERESIS.
3791 * This mapping saves some tests for the majority of the
3794 if (upper != LATIN_SMALL_LETTER_Y_WITH_DIAERESIS) {
3796 /* Not tricky. Just save it. */
3797 STORE_UNI_TO_UTF8_TWO_BYTE(tmpbuf, upper);
3799 else if (chr == LATIN_SMALL_LETTER_SHARP_S) {
3801 /* This one is tricky because it is two characters long,
3802 * though the UTF-8 is still two bytes, so the stored
3803 * length doesn't change */
3804 *tmpbuf = 'S'; /* The UTF-8 is 'Ss' */
3805 *(tmpbuf + 1) = 's';
3809 /* The other two have their title and upper cases the same,
3810 * but are tricky because the changed-case characters
3811 * aren't in the latin1 range. They, however, do fit into
3812 * two UTF-8 bytes */
3813 STORE_NON_LATIN1_UC(tmpbuf, chr);
3818 #endif /* end of dont want to break user-defined casing */
3820 /* Here, can't short-cut the general case */
3822 utf8_to_uvchr(s, &ulen);
3823 if (op_type == OP_UCFIRST) toTITLE_utf8(s, tmpbuf, &tculen);
3824 else toLOWER_utf8(s, tmpbuf, &tculen);
3826 /* we can't do in-place if the length changes. */
3827 if (ulen != tculen) inplace = FALSE;
3828 need = slen + 1 - ulen + tculen;
3829 #ifdef GO_AHEAD_AND_BREAK_USER_DEFINED_CASE_MAPPINGS
3833 else { /* Non-zero length, non-UTF-8, Need to consider locale and if
3834 * latin1 is treated as caseless. Note that a locale takes
3836 tculen = 1; /* Most characters will require one byte, but this will
3837 * need to be overridden for the tricky ones */
3840 if (op_type == OP_LCFIRST) {
3842 /* lower case the first letter: no trickiness for any character */
3843 *tmpbuf = (IN_LOCALE_RUNTIME) ? toLOWER_LC(*s) :
3844 ((IN_UNI_8_BIT) ? toLOWER_LATIN1(*s) : toLOWER(*s));
3847 else if (IN_LOCALE_RUNTIME) {
3848 *tmpbuf = toUPPER_LC(*s); /* This would be a bug if any locales
3849 * have upper and title case different
3852 else if (! IN_UNI_8_BIT) {
3853 *tmpbuf = toUPPER(*s); /* Returns caseless for non-ascii, or
3854 * on EBCDIC machines whatever the
3855 * native function does */
3857 else { /* is ucfirst non-UTF-8, not in locale, and cased latin1 */
3858 *tmpbuf = toUPPER_LATIN1_MOD(*s);
3860 /* tmpbuf now has the correct title case for all latin1 characters
3861 * except for the several ones that have tricky handling. All
3862 * of these are mapped by the MOD to the letter below. */
3863 if (*tmpbuf == LATIN_SMALL_LETTER_Y_WITH_DIAERESIS) {
3865 /* The length is going to change, with all three of these, so
3866 * can't replace just the first character */
3869 /* We use the original to distinguish between these tricky
3871 if (*s == LATIN_SMALL_LETTER_SHARP_S) {
3872 /* Two character title case 'Ss', but can remain non-UTF-8 */
3875 *(tmpbuf + 1) = 's'; /* Assert: length(tmpbuf) >= 2 */
3880 /* The other two tricky ones have their title case outside
3881 * latin1. It is the same as their upper case. */
3883 STORE_NON_LATIN1_UC(tmpbuf, *s);
3885 /* The UTF-8 and UTF-EBCDIC lengths of both these characters
3886 * and their upper cases is 2. */
3889 /* The entire result will have to be in UTF-8. Assume worst
3890 * case sizing in conversion. (all latin1 characters occupy
3891 * at most two bytes in utf8) */
3892 convert_source_to_utf8 = TRUE;
3893 need = slen * 2 + 1;
3895 } /* End of is one of the three special chars */
3896 } /* End of use Unicode (Latin1) semantics */
3897 } /* End of changing the case of the first character */
3899 /* Here, have the first character's changed case stored in tmpbuf. Ready to
3900 * generate the result */
3903 /* We can convert in place. This means we change just the first
3904 * character without disturbing the rest; no need to grow */
3906 s = d = (U8*)SvPV_force_nomg(source, slen);
3912 /* Here, we can't convert in place; we earlier calculated how much
3913 * space we will need, so grow to accommodate that */
3914 SvUPGRADE(dest, SVt_PV);
3915 d = (U8*)SvGROW(dest, need);
3916 (void)SvPOK_only(dest);
3923 if (! convert_source_to_utf8) {
3925 /* Here both source and dest are in UTF-8, but have to create
3926 * the entire output. We initialize the result to be the
3927 * title/lower cased first character, and then append the rest
3929 sv_setpvn(dest, (char*)tmpbuf, tculen);
3931 sv_catpvn(dest, (char*)(s + ulen), slen - ulen);
3935 const U8 *const send = s + slen;
3937 /* Here the dest needs to be in UTF-8, but the source isn't,
3938 * except we earlier UTF-8'd the first character of the source
3939 * into tmpbuf. First put that into dest, and then append the
3940 * rest of the source, converting it to UTF-8 as we go. */
3942 /* Assert tculen is 2 here because the only two characters that
3943 * get to this part of the code have 2-byte UTF-8 equivalents */
3945 *d++ = *(tmpbuf + 1);
3946 s++; /* We have just processed the 1st char */
3948 for (; s < send; s++) {
3949 d = uvchr_to_utf8(d, *s);
3952 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3956 else { /* in-place UTF-8. Just overwrite the first character */
3957 Copy(tmpbuf, d, tculen, U8);
3958 SvCUR_set(dest, need - 1);
3961 else { /* Neither source nor dest are in or need to be UTF-8 */
3963 if (IN_LOCALE_RUNTIME) {
3967 if (inplace) { /* in-place, only need to change the 1st char */
3970 else { /* Not in-place */
3972 /* Copy the case-changed character(s) from tmpbuf */
3973 Copy(tmpbuf, d, tculen, U8);
3974 d += tculen - 1; /* Code below expects d to point to final
3975 * character stored */
3978 else { /* empty source */
3979 /* See bug #39028: Don't taint if empty */
3983 /* In a "use bytes" we don't treat the source as UTF-8, but, still want
3984 * the destination to retain that flag */
3988 if (!inplace) { /* Finish the rest of the string, unchanged */
3989 /* This will copy the trailing NUL */
3990 Copy(s + 1, d + 1, slen, U8);
3991 SvCUR_set(dest, need - 1);
3998 /* There's so much setup/teardown code common between uc and lc, I wonder if
3999 it would be worth merging the two, and just having a switch outside each
4000 of the three tight loops. There is less and less commonality though */
4014 if (SvPADTMP(source) && !SvREADONLY(source) && !SvAMAGIC(source)
4015 && SvTEMP(source) && !DO_UTF8(source)
4016 && (IN_LOCALE_RUNTIME || ! IN_UNI_8_BIT)) {
4018 /* We can convert in place. The reason we can't if in UNI_8_BIT is to
4019 * make the loop tight, so we overwrite the source with the dest before
4020 * looking at it, and we need to look at the original source
4021 * afterwards. There would also need to be code added to handle
4022 * switching to not in-place in midstream if we run into characters
4023 * that change the length.
4026 s = d = (U8*)SvPV_force_nomg(source, len);
4033 /* The old implementation would copy source into TARG at this point.
4034 This had the side effect that if source was undef, TARG was now
4035 an undefined SV with PADTMP set, and they don't warn inside
4036 sv_2pv_flags(). However, we're now getting the PV direct from
4037 source, which doesn't have PADTMP set, so it would warn. Hence the
4041 s = (const U8*)SvPV_nomg_const(source, len);
4043 if (ckWARN(WARN_UNINITIALIZED))
4044 report_uninit(source);
4050 SvUPGRADE(dest, SVt_PV);
4051 d = (U8*)SvGROW(dest, min);
4052 (void)SvPOK_only(dest);
4057 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
4058 to check DO_UTF8 again here. */
4060 if (DO_UTF8(source)) {
4061 const U8 *const send = s + len;
4062 U8 tmpbuf[UTF8_MAXBYTES+1];
4064 /* All occurrences of these are to be moved to follow any other marks.
4065 * This is context-dependent. We may not be passed enough context to
4066 * move the iota subscript beyond all of them, but we do the best we can
4067 * with what we're given. The result is always better than if we
4068 * hadn't done this. And, the problem would only arise if we are
4069 * passed a character without all its combining marks, which would be
4070 * the caller's mistake. The information this is based on comes from a
4071 * comment in Unicode SpecialCasing.txt, (and the Standard's text
4072 * itself) and so can't be checked properly to see if it ever gets
4073 * revised. But the likelihood of it changing is remote */
4074 bool in_iota_subscript = FALSE;
4077 if (in_iota_subscript && ! is_utf8_mark(s)) {
4078 /* A non-mark. Time to output the iota subscript */
4079 #define GREEK_CAPITAL_LETTER_IOTA 0x0399
4080 #define COMBINING_GREEK_YPOGEGRAMMENI 0x0345
4082 CAT_UNI_TO_UTF8_TWO_BYTE(d, GREEK_CAPITAL_LETTER_IOTA);
4083 in_iota_subscript = FALSE;
4087 /* See comments at the first instance in this file of this ifdef */
4088 #ifdef GO_AHEAD_AND_BREAK_USER_DEFINED_CASE_MAPPINGS
4090 /* If the UTF-8 character is invariant, then it is in the range
4091 * known by the standard macro; result is only one byte long */
4092 if (UTF8_IS_INVARIANT(*s)) {
4096 else if (UTF8_IS_DOWNGRADEABLE_START(*s)) {
4098 /* Likewise, if it fits in a byte, its case change is in our
4100 U8 orig = UTF8_ACCUMULATE(*s, *(s+1));
4101 U8 upper = toUPPER_LATIN1_MOD(orig);
4102 CAT_TWO_BYTE_UNI_UPPER_MOD(d, orig, upper);
4110 /* Otherwise, need the general UTF-8 case. Get the changed
4111 * case value and copy it to the output buffer */
4113 const STRLEN u = UTF8SKIP(s);
4116 const UV uv = toUPPER_utf8(s, tmpbuf, &ulen);
4117 if (uv == GREEK_CAPITAL_LETTER_IOTA
4118 && utf8_to_uvchr(s, 0) == COMBINING_GREEK_YPOGEGRAMMENI)
4120 in_iota_subscript = TRUE;
4123 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
4124 /* If the eventually required minimum size outgrows
4125 * the available space, we need to grow. */
4126 const UV o = d - (U8*)SvPVX_const(dest);
4128 /* If someone uppercases one million U+03B0s we
4129 * SvGROW() one million times. Or we could try
4130 * guessing how much to allocate without allocating too
4131 * much. Such is life. See corresponding comment in
4132 * lc code for another option */
4134 d = (U8*)SvPVX(dest) + o;
4136 Copy(tmpbuf, d, ulen, U8);
4142 if (in_iota_subscript) {
4143 CAT_UNI_TO_UTF8_TWO_BYTE(d, GREEK_CAPITAL_LETTER_IOTA);
4147 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4149 else { /* Not UTF-8 */
4151 const U8 *const send = s + len;
4153 /* Use locale casing if in locale; regular style if not treating
4154 * latin1 as having case; otherwise the latin1 casing. Do the
4155 * whole thing in a tight loop, for speed, */
4156 if (IN_LOCALE_RUNTIME) {
4159 for (; s < send; d++, s++)
4160 *d = toUPPER_LC(*s);
4162 else if (! IN_UNI_8_BIT) {
4163 for (; s < send; d++, s++) {
4168 for (; s < send; d++, s++) {
4169 *d = toUPPER_LATIN1_MOD(*s);
4170 if (*d != LATIN_SMALL_LETTER_Y_WITH_DIAERESIS) continue;
4172 /* The mainstream case is the tight loop above. To avoid
4173 * extra tests in that, all three characters that require
4174 * special handling are mapped by the MOD to the one tested
4176 * Use the source to distinguish between the three cases */
4178 if (*s == LATIN_SMALL_LETTER_SHARP_S) {
4180 /* uc() of this requires 2 characters, but they are
4181 * ASCII. If not enough room, grow the string */
4182 if (SvLEN(dest) < ++min) {
4183 const UV o = d - (U8*)SvPVX_const(dest);
4185 d = (U8*)SvPVX(dest) + o;
4187 *d++ = 'S'; *d = 'S'; /* upper case is 'SS' */
4188 continue; /* Back to the tight loop; still in ASCII */
4191 /* The other two special handling characters have their
4192 * upper cases outside the latin1 range, hence need to be
4193 * in UTF-8, so the whole result needs to be in UTF-8. So,
4194 * here we are somewhere in the middle of processing a
4195 * non-UTF-8 string, and realize that we will have to convert
4196 * the whole thing to UTF-8. What to do? There are
4197 * several possibilities. The simplest to code is to
4198 * convert what we have so far, set a flag, and continue on
4199 * in the loop. The flag would be tested each time through
4200 * the loop, and if set, the next character would be
4201 * converted to UTF-8 and stored. But, I (khw) didn't want
4202 * to slow down the mainstream case at all for this fairly
4203 * rare case, so I didn't want to add a test that didn't
4204 * absolutely have to be there in the loop, besides the
4205 * possibility that it would get too complicated for
4206 * optimizers to deal with. Another possibility is to just
4207 * give up, convert the source to UTF-8, and restart the
4208 * function that way. Another possibility is to convert
4209 * both what has already been processed and what is yet to
4210 * come separately to UTF-8, then jump into the loop that
4211 * handles UTF-8. But the most efficient time-wise of the
4212 * ones I could think of is what follows, and turned out to
4213 * not require much extra code. */
4215 /* Convert what we have so far into UTF-8, telling the
4216 * function that we know it should be converted, and to
4217 * allow extra space for what we haven't processed yet.
4218 * Assume the worst case space requirements for converting
4219 * what we haven't processed so far: that it will require
4220 * two bytes for each remaining source character, plus the
4221 * NUL at the end. This may cause the string pointer to
4222 * move, so re-find it. */
4224 len = d - (U8*)SvPVX_const(dest);
4225 SvCUR_set(dest, len);
4226 len = sv_utf8_upgrade_flags_grow(dest,
4227 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
4229 d = (U8*)SvPVX(dest) + len;
4231 /* And append the current character's upper case in UTF-8 */
4232 CAT_NON_LATIN1_UC(d, *s);
4234 /* Now process the remainder of the source, converting to
4235 * upper and UTF-8. If a resulting byte is invariant in
4236 * UTF-8, output it as-is, otherwise convert to UTF-8 and
4237 * append it to the output. */
4240 for (; s < send; s++) {
4241 U8 upper = toUPPER_LATIN1_MOD(*s);
4242 if UTF8_IS_INVARIANT(upper) {
4246 CAT_TWO_BYTE_UNI_UPPER_MOD(d, *s, upper);
4250 /* Here have processed the whole source; no need to continue
4251 * with the outer loop. Each character has been converted
4252 * to upper case and converted to UTF-8 */
4255 } /* End of processing all latin1-style chars */
4256 } /* End of processing all chars */
4257 } /* End of source is not empty */
4259 if (source != dest) {
4260 *d = '\0'; /* Here d points to 1 after last char, add NUL */
4261 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4263 } /* End of isn't utf8 */
4281 if (SvPADTMP(source) && !SvREADONLY(source) && !SvAMAGIC(source)
4282 && SvTEMP(source) && !DO_UTF8(source)) {
4284 /* We can convert in place, as lowercasing anything in the latin1 range
4285 * (or else DO_UTF8 would have been on) doesn't lengthen it */
4287 s = d = (U8*)SvPV_force_nomg(source, len);
4294 /* The old implementation would copy source into TARG at this point.
4295 This had the side effect that if source was undef, TARG was now
4296 an undefined SV with PADTMP set, and they don't warn inside
4297 sv_2pv_flags(). However, we're now getting the PV direct from
4298 source, which doesn't have PADTMP set, so it would warn. Hence the
4302 s = (const U8*)SvPV_nomg_const(source, len);
4304 if (ckWARN(WARN_UNINITIALIZED))
4305 report_uninit(source);
4311 SvUPGRADE(dest, SVt_PV);
4312 d = (U8*)SvGROW(dest, min);
4313 (void)SvPOK_only(dest);
4318 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
4319 to check DO_UTF8 again here. */
4321 if (DO_UTF8(source)) {
4322 const U8 *const send = s + len;
4323 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
4326 /* See comments at the first instance in this file of this ifdef */
4327 #ifdef GO_AHEAD_AND_BREAK_USER_DEFINED_CASE_MAPPINGS
4328 if (UTF8_IS_INVARIANT(*s)) {
4330 /* Invariant characters use the standard mappings compiled in.
4335 else if (UTF8_IS_DOWNGRADEABLE_START(*s)) {
4337 /* As do the ones in the Latin1 range */
4338 U8 lower = toLOWER_LATIN1(UTF8_ACCUMULATE(*s, *(s+1)));
4339 CAT_UNI_TO_UTF8_TWO_BYTE(d, lower);
4344 /* Here, is utf8 not in Latin-1 range, have to go out and get
4345 * the mappings from the tables. */
4347 const STRLEN u = UTF8SKIP(s);
4350 #ifndef CONTEXT_DEPENDENT_CASING
4351 toLOWER_utf8(s, tmpbuf, &ulen);
4353 /* This is ifdefd out because it needs more work and thought. It isn't clear
4354 * that we should do it.
4355 * A minor objection is that this is based on a hard-coded rule from the
4356 * Unicode standard, and may change, but this is not very likely at all.
4357 * mktables should check and warn if it does.
4358 * More importantly, if the sigma occurs at the end of the string, we don't
4359 * have enough context to know whether it is part of a larger string or going
4360 * to be or not. It may be that we are passed a subset of the context, via
4361 * a \U...\E, for example, and we could conceivably know the larger context if
4362 * code were changed to pass that in. But, if the string passed in is an
4363 * intermediate result, and the user concatenates two strings together
4364 * after we have made a final sigma, that would be wrong. If the final sigma
4365 * occurs in the middle of the string we are working on, then we know that it
4366 * should be a final sigma, but otherwise we can't be sure. */
4368 const UV uv = toLOWER_utf8(s, tmpbuf, &ulen);
4370 /* If the lower case is a small sigma, it may be that we need
4371 * to change it to a final sigma. This happens at the end of
4372 * a word that contains more than just this character, and only
4373 * when we started with a capital sigma. */
4374 if (uv == UNICODE_GREEK_SMALL_LETTER_SIGMA &&
4375 s > send - len && /* Makes sure not the first letter */
4376 utf8_to_uvchr(s, 0) == UNICODE_GREEK_CAPITAL_LETTER_SIGMA
4379 /* We use the algorithm in:
4380 * http://www.unicode.org/versions/Unicode5.0.0/ch03.pdf (C
4381 * is a CAPITAL SIGMA): If C is preceded by a sequence
4382 * consisting of a cased letter and a case-ignorable
4383 * sequence, and C is not followed by a sequence consisting
4384 * of a case ignorable sequence and then a cased letter,
4385 * then when lowercasing C, C becomes a final sigma */
4387 /* To determine if this is the end of a word, need to peek
4388 * ahead. Look at the next character */
4389 const U8 *peek = s + u;
4391 /* Skip any case ignorable characters */
4392 while (peek < send && is_utf8_case_ignorable(peek)) {
4393 peek += UTF8SKIP(peek);
4396 /* If we reached the end of the string without finding any
4397 * non-case ignorable characters, or if the next such one
4398 * is not-cased, then we have met the conditions for it
4399 * being a final sigma with regards to peek ahead, and so
4400 * must do peek behind for the remaining conditions. (We
4401 * know there is stuff behind to look at since we tested
4402 * above that this isn't the first letter) */
4403 if (peek >= send || ! is_utf8_cased(peek)) {
4404 peek = utf8_hop(s, -1);
4406 /* Here are at the beginning of the first character
4407 * before the original upper case sigma. Keep backing
4408 * up, skipping any case ignorable characters */
4409 while (is_utf8_case_ignorable(peek)) {
4410 peek = utf8_hop(peek, -1);
4413 /* Here peek points to the first byte of the closest
4414 * non-case-ignorable character before the capital
4415 * sigma. If it is cased, then by the Unicode
4416 * algorithm, we should use a small final sigma instead
4417 * of what we have */
4418 if (is_utf8_cased(peek)) {
4419 STORE_UNI_TO_UTF8_TWO_BYTE(tmpbuf,
4420 UNICODE_GREEK_SMALL_LETTER_FINAL_SIGMA);
4424 else { /* Not a context sensitive mapping */
4425 #endif /* End of commented out context sensitive */
4426 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
4428 /* If the eventually required minimum size outgrows
4429 * the available space, we need to grow. */
4430 const UV o = d - (U8*)SvPVX_const(dest);
4432 /* If someone lowercases one million U+0130s we
4433 * SvGROW() one million times. Or we could try
4434 * guessing how much to allocate without allocating too
4435 * much. Such is life. Another option would be to
4436 * grow an extra byte or two more each time we need to
4437 * grow, which would cut down the million to 500K, with
4440 d = (U8*)SvPVX(dest) + o;
4442 #ifdef CONTEXT_DEPENDENT_CASING
4445 /* Copy the newly lowercased letter to the output buffer we're
4447 Copy(tmpbuf, d, ulen, U8);
4450 #ifdef GO_AHEAD_AND_BREAK_USER_DEFINED_CASE_MAPPINGS
4453 } /* End of looping through the source string */
4456 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4457 } else { /* Not utf8 */
4459 const U8 *const send = s + len;
4461 /* Use locale casing if in locale; regular style if not treating
4462 * latin1 as having case; otherwise the latin1 casing. Do the
4463 * whole thing in a tight loop, for speed, */
4464 if (IN_LOCALE_RUNTIME) {
4467 for (; s < send; d++, s++)
4468 *d = toLOWER_LC(*s);
4470 else if (! IN_UNI_8_BIT) {
4471 for (; s < send; d++, s++) {
4476 for (; s < send; d++, s++) {
4477 *d = toLOWER_LATIN1(*s);
4481 if (source != dest) {
4483 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4493 SV * const sv = TOPs;
4495 register const char *s = SvPV_const(sv,len);
4497 SvUTF8_off(TARG); /* decontaminate */
4500 SvUPGRADE(TARG, SVt_PV);
4501 SvGROW(TARG, (len * 2) + 1);
4505 if (UTF8_IS_CONTINUED(*s)) {
4506 STRLEN ulen = UTF8SKIP(s);
4530 SvCUR_set(TARG, d - SvPVX_const(TARG));
4531 (void)SvPOK_only_UTF8(TARG);
4534 sv_setpvn(TARG, s, len);
4543 dVAR; dSP; dMARK; dORIGMARK;
4544 register AV *const av = MUTABLE_AV(POPs);
4545 register const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
4547 if (SvTYPE(av) == SVt_PVAV) {
4548 const I32 arybase = CopARYBASE_get(PL_curcop);
4549 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
4550 bool can_preserve = FALSE;
4556 can_preserve = SvCANEXISTDELETE(av);
4559 if (lval && localizing) {
4562 for (svp = MARK + 1; svp <= SP; svp++) {
4563 const I32 elem = SvIV(*svp);
4567 if (max > AvMAX(av))
4571 while (++MARK <= SP) {
4573 I32 elem = SvIV(*MARK);
4574 bool preeminent = TRUE;
4578 if (localizing && can_preserve) {
4579 /* If we can determine whether the element exist,
4580 * Try to preserve the existenceness of a tied array
4581 * element by using EXISTS and DELETE if possible.
4582 * Fallback to FETCH and STORE otherwise. */
4583 preeminent = av_exists(av, elem);
4586 svp = av_fetch(av, elem, lval);
4588 if (!svp || *svp == &PL_sv_undef)
4589 DIE(aTHX_ PL_no_aelem, elem);
4592 save_aelem(av, elem, svp);
4594 SAVEADELETE(av, elem);
4597 *MARK = svp ? *svp : &PL_sv_undef;
4600 if (GIMME != G_ARRAY) {
4602 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
4612 AV *array = MUTABLE_AV(POPs);
4613 const I32 gimme = GIMME_V;
4614 IV *iterp = Perl_av_iter_p(aTHX_ array);
4615 const IV current = (*iterp)++;
4617 if (current > av_len(array)) {
4619 if (gimme == G_SCALAR)
4626 mPUSHi(CopARYBASE_get(PL_curcop) + current);
4627 if (gimme == G_ARRAY) {
4628 SV **const element = av_fetch(array, current, 0);
4629 PUSHs(element ? *element : &PL_sv_undef);
4638 AV *array = MUTABLE_AV(POPs);
4639 const I32 gimme = GIMME_V;
4641 *Perl_av_iter_p(aTHX_ array) = 0;
4643 if (gimme == G_SCALAR) {
4645 PUSHi(av_len(array) + 1);
4647 else if (gimme == G_ARRAY) {
4648 IV n = Perl_av_len(aTHX_ array);
4649 IV i = CopARYBASE_get(PL_curcop);
4653 if (PL_op->op_type == OP_AKEYS) {
4655 for (; i <= n; i++) {
4660 for (i = 0; i <= n; i++) {
4661 SV *const *const elem = Perl_av_fetch(aTHX_ array, i, 0);
4662 PUSHs(elem ? *elem : &PL_sv_undef);
4669 /* Associative arrays. */
4675 HV * hash = MUTABLE_HV(POPs);
4677 const I32 gimme = GIMME_V;
4680 /* might clobber stack_sp */
4681 entry = hv_iternext(hash);
4686 SV* const sv = hv_iterkeysv(entry);
4687 PUSHs(sv); /* won't clobber stack_sp */
4688 if (gimme == G_ARRAY) {
4691 /* might clobber stack_sp */
4692 val = hv_iterval(hash, entry);
4697 else if (gimme == G_SCALAR)
4704 S_do_delete_local(pTHX)
4708 const I32 gimme = GIMME_V;
4712 if (PL_op->op_private & OPpSLICE) {
4714 SV * const osv = POPs;
4715 const bool tied = SvRMAGICAL(osv)
4716 && mg_find((const SV *)osv, PERL_MAGIC_tied);
4717 const bool can_preserve = SvCANEXISTDELETE(osv)
4718 || mg_find((const SV *)osv, PERL_MAGIC_env);
4719 const U32 type = SvTYPE(osv);
4720 if (type == SVt_PVHV) { /* hash element */
4721 HV * const hv = MUTABLE_HV(osv);
4722 while (++MARK <= SP) {
4723 SV * const keysv = *MARK;
4725 bool preeminent = TRUE;
4727 preeminent = hv_exists_ent(hv, keysv, 0);
4729 HE *he = hv_fetch_ent(hv, keysv, 1, 0);
4736 sv = hv_delete_ent(hv, keysv, 0, 0);
4737 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4740 save_helem_flags(hv, keysv, &sv, SAVEf_KEEPOLDELEM);
4742 *MARK = sv_mortalcopy(sv);
4748 SAVEHDELETE(hv, keysv);
4749 *MARK = &PL_sv_undef;
4753 else if (type == SVt_PVAV) { /* array element */
4754 if (PL_op->op_flags & OPf_SPECIAL) {
4755 AV * const av = MUTABLE_AV(osv);
4756 while (++MARK <= SP) {
4757 I32 idx = SvIV(*MARK);
4759 bool preeminent = TRUE;
4761 preeminent = av_exists(av, idx);
4763 SV **svp = av_fetch(av, idx, 1);
4770 sv = av_delete(av, idx, 0);
4771 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4774 save_aelem_flags(av, idx, &sv, SAVEf_KEEPOLDELEM);
4776 *MARK = sv_mortalcopy(sv);
4782 SAVEADELETE(av, idx);
4783 *MARK = &PL_sv_undef;
4789 DIE(aTHX_ "Not a HASH reference");
4790 if (gimme == G_VOID)
4792 else if (gimme == G_SCALAR) {
4797 *++MARK = &PL_sv_undef;
4802 SV * const keysv = POPs;
4803 SV * const osv = POPs;
4804 const bool tied = SvRMAGICAL(osv)
4805 && mg_find((const SV *)osv, PERL_MAGIC_tied);
4806 const bool can_preserve = SvCANEXISTDELETE(osv)
4807 || mg_find((const SV *)osv, PERL_MAGIC_env);
4808 const U32 type = SvTYPE(osv);
4810 if (type == SVt_PVHV) {
4811 HV * const hv = MUTABLE_HV(osv);
4812 bool preeminent = TRUE;
4814 preeminent = hv_exists_ent(hv, keysv, 0);
4816 HE *he = hv_fetch_ent(hv, keysv, 1, 0);
4823 sv = hv_delete_ent(hv, keysv, 0, 0);
4824 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4827 save_helem_flags(hv, keysv, &sv, SAVEf_KEEPOLDELEM);
4829 SV *nsv = sv_mortalcopy(sv);
4835 SAVEHDELETE(hv, keysv);
4837 else if (type == SVt_PVAV) {
4838 if (PL_op->op_flags & OPf_SPECIAL) {
4839 AV * const av = MUTABLE_AV(osv);
4840 I32 idx = SvIV(keysv);
4841 bool preeminent = TRUE;
4843 preeminent = av_exists(av, idx);
4845 SV **svp = av_fetch(av, idx, 1);
4852 sv = av_delete(av, idx, 0);
4853 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4856 save_aelem_flags(av, idx, &sv, SAVEf_KEEPOLDELEM);
4858 SV *nsv = sv_mortalcopy(sv);
4864 SAVEADELETE(av, idx);
4867 DIE(aTHX_ "panic: avhv_delete no longer supported");
4870 DIE(aTHX_ "Not a HASH reference");
4873 if (gimme != G_VOID)
4887 if (PL_op->op_private & OPpLVAL_INTRO)
4888 return do_delete_local();
4891 discard = (gimme == G_VOID) ? G_DISCARD : 0;
4893 if (PL_op->op_private & OPpSLICE) {
4895 HV * const hv = MUTABLE_HV(POPs);
4896 const U32 hvtype = SvTYPE(hv);
4897 if (hvtype == SVt_PVHV) { /* hash element */
4898 while (++MARK <= SP) {
4899 SV * const sv = hv_delete_ent(hv, *MARK, discard, 0);
4900 *MARK = sv ? sv : &PL_sv_undef;
4903 else if (hvtype == SVt_PVAV) { /* array element */
4904 if (PL_op->op_flags & OPf_SPECIAL) {
4905 while (++MARK <= SP) {
4906 SV * const sv = av_delete(MUTABLE_AV(hv), SvIV(*MARK), discard);
4907 *MARK = sv ? sv : &PL_sv_undef;
4912 DIE(aTHX_ "Not a HASH reference");
4915 else if (gimme == G_SCALAR) {
4920 *++MARK = &PL_sv_undef;
4926 HV * const hv = MUTABLE_HV(POPs);
4928 if (SvTYPE(hv) == SVt_PVHV)
4929 sv = hv_delete_ent(hv, keysv, discard, 0);
4930 else if (SvTYPE(hv) == SVt_PVAV) {
4931 if (PL_op->op_flags & OPf_SPECIAL)
4932 sv = av_delete(MUTABLE_AV(hv), SvIV(keysv), discard);
4934 DIE(aTHX_ "panic: avhv_delete no longer supported");
4937 DIE(aTHX_ "Not a HASH reference");
4953 if (PL_op->op_private & OPpEXISTS_SUB) {
4955 SV * const sv = POPs;
4956 CV * const cv = sv_2cv(sv, &hv, &gv, 0);
4959 if (gv && isGV(gv) && GvCV(gv) && !GvCVGEN(gv))
4964 hv = MUTABLE_HV(POPs);
4965 if (SvTYPE(hv) == SVt_PVHV) {
4966 if (hv_exists_ent(hv, tmpsv, 0))
4969 else if (SvTYPE(hv) == SVt_PVAV) {
4970 if (PL_op->op_flags & OPf_SPECIAL) { /* array element */
4971 if (av_exists(MUTABLE_AV(hv), SvIV(tmpsv)))
4976 DIE(aTHX_ "Not a HASH reference");
4983 dVAR; dSP; dMARK; dORIGMARK;
4984 register HV * const hv = MUTABLE_HV(POPs);
4985 register const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
4986 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
4987 bool can_preserve = FALSE;
4993 if (SvCANEXISTDELETE(hv) || mg_find((const SV *)hv, PERL_MAGIC_env))
4994 can_preserve = TRUE;
4997 while (++MARK <= SP) {
4998 SV * const keysv = *MARK;
5001 bool preeminent = TRUE;
5003 if (localizing && can_preserve) {
5004 /* If we can determine whether the element exist,
5005 * try to preserve the existenceness of a tied hash
5006 * element by using EXISTS and DELETE if possible.
5007 * Fallback to FETCH and STORE otherwise. */
5008 preeminent = hv_exists_ent(hv, keysv, 0);
5011 he = hv_fetch_ent(hv, keysv, lval, 0);
5012 svp = he ? &HeVAL(he) : NULL;
5015 if (!svp || *svp == &PL_sv_undef) {
5016 DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
5019 if (HvNAME_get(hv) && isGV(*svp))
5020 save_gp(MUTABLE_GV(*svp), !(PL_op->op_flags & OPf_SPECIAL));
5021 else if (preeminent)
5022 save_helem_flags(hv, keysv, svp,
5023 (PL_op->op_flags & OPf_SPECIAL) ? 0 : SAVEf_SETMAGIC);
5025 SAVEHDELETE(hv, keysv);
5028 *MARK = svp ? *svp : &PL_sv_undef;
5030 if (GIMME != G_ARRAY) {
5032 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
5038 /* List operators. */
5043 if (GIMME != G_ARRAY) {
5045 *MARK = *SP; /* unwanted list, return last item */
5047 *MARK = &PL_sv_undef;
5057 SV ** const lastrelem = PL_stack_sp;
5058 SV ** const lastlelem = PL_stack_base + POPMARK;
5059 SV ** const firstlelem = PL_stack_base + POPMARK + 1;
5060 register SV ** const firstrelem = lastlelem + 1;
5061 const I32 arybase = CopARYBASE_get(PL_curcop);
5062 I32 is_something_there = FALSE;
5064 register const I32 max = lastrelem - lastlelem;
5065 register SV **lelem;
5067 if (GIMME != G_ARRAY) {
5068 I32 ix = SvIV(*lastlelem);
5073 if (ix < 0 || ix >= max)
5074 *firstlelem = &PL_sv_undef;
5076 *firstlelem = firstrelem[ix];
5082 SP = firstlelem - 1;
5086 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
5087 I32 ix = SvIV(*lelem);
5092 if (ix < 0 || ix >= max)
5093 *lelem = &PL_sv_undef;
5095 is_something_there = TRUE;
5096 if (!(*lelem = firstrelem[ix]))
5097 *lelem = &PL_sv_undef;
5100 if (is_something_there)
5103 SP = firstlelem - 1;
5109 dVAR; dSP; dMARK; dORIGMARK;
5110 const I32 items = SP - MARK;
5111 SV * const av = MUTABLE_SV(av_make(items, MARK+1));
5112 SP = ORIGMARK; /* av_make() might realloc stack_sp */
5113 mXPUSHs((PL_op->op_flags & OPf_SPECIAL)
5114 ? newRV_noinc(av) : av);
5120 dVAR; dSP; dMARK; dORIGMARK;
5121 HV* const hv = newHV();
5124 SV * const key = *++MARK;
5125 SV * const val = newSV(0);
5127 sv_setsv(val, *++MARK);
5129 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Odd number of elements in anonymous hash");
5130 (void)hv_store_ent(hv,key,val,0);
5133 mXPUSHs((PL_op->op_flags & OPf_SPECIAL)
5134 ? newRV_noinc(MUTABLE_SV(hv)) : MUTABLE_SV(hv));
5140 dVAR; dSP; dMARK; dORIGMARK;
5141 register AV *ary = MUTABLE_AV(*++MARK);
5145 register I32 offset;
5146 register I32 length;
5150 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
5153 *MARK-- = SvTIED_obj(MUTABLE_SV(ary), mg);
5156 ENTER_with_name("call_SPLICE");
5157 call_method("SPLICE",GIMME_V);
5158 LEAVE_with_name("call_SPLICE");
5166 offset = i = SvIV(*MARK);
5168 offset += AvFILLp(ary) + 1;
5170 offset -= CopARYBASE_get(PL_curcop);
5172 DIE(aTHX_ PL_no_aelem, i);
5174 length = SvIVx(*MARK++);
5176 length += AvFILLp(ary) - offset + 1;
5182 length = AvMAX(ary) + 1; /* close enough to infinity */
5186 length = AvMAX(ary) + 1;
5188 if (offset > AvFILLp(ary) + 1) {
5189 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "splice() offset past end of array" );
5190 offset = AvFILLp(ary) + 1;
5192 after = AvFILLp(ary) + 1 - (offset + length);
5193 if (after < 0) { /* not that much array */
5194 length += after; /* offset+length now in array */
5200 /* At this point, MARK .. SP-1 is our new LIST */
5203 diff = newlen - length;
5204 if (newlen && !AvREAL(ary) && AvREIFY(ary))
5207 /* make new elements SVs now: avoid problems if they're from the array */
5208 for (dst = MARK, i = newlen; i; i--) {
5209 SV * const h = *dst;
5210 *dst++ = newSVsv(h);
5213 if (diff < 0) { /* shrinking the area */
5214 SV **tmparyval = NULL;
5216 Newx(tmparyval, newlen, SV*); /* so remember insertion */
5217 Copy(MARK, tmparyval, newlen, SV*);
5220 MARK = ORIGMARK + 1;
5221 if (GIMME == G_ARRAY) { /* copy return vals to stack */
5222 MEXTEND(MARK, length);
5223 Copy(AvARRAY(ary)+offset, MARK, length, SV*);
5225 EXTEND_MORTAL(length);
5226 for (i = length, dst = MARK; i; i--) {
5227 sv_2mortal(*dst); /* free them eventualy */
5234 *MARK = AvARRAY(ary)[offset+length-1];
5237 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
5238 SvREFCNT_dec(*dst++); /* free them now */
5241 AvFILLp(ary) += diff;
5243 /* pull up or down? */
5245 if (offset < after) { /* easier to pull up */
5246 if (offset) { /* esp. if nothing to pull */
5247 src = &AvARRAY(ary)[offset-1];
5248 dst = src - diff; /* diff is negative */
5249 for (i = offset; i > 0; i--) /* can't trust Copy */
5253 AvARRAY(ary) = AvARRAY(ary) - diff; /* diff is negative */
5257 if (after) { /* anything to pull down? */
5258 src = AvARRAY(ary) + offset + length;
5259 dst = src + diff; /* diff is negative */
5260 Move(src, dst, after, SV*);
5262 dst = &AvARRAY(ary)[AvFILLp(ary)+1];
5263 /* avoid later double free */
5267 dst[--i] = &PL_sv_undef;
5270 Copy( tmparyval, AvARRAY(ary) + offset, newlen, SV* );
5271 Safefree(tmparyval);
5274 else { /* no, expanding (or same) */
5275 SV** tmparyval = NULL;
5277 Newx(tmparyval, length, SV*); /* so remember deletion */
5278 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
5281 if (diff > 0) { /* expanding */
5282 /* push up or down? */
5283 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
5287 Move(src, dst, offset, SV*);
5289 AvARRAY(ary) = AvARRAY(ary) - diff;/* diff is positive */
5291 AvFILLp(ary) += diff;
5294 if (AvFILLp(ary) + diff >= AvMAX(ary)) /* oh, well */
5295 av_extend(ary, AvFILLp(ary) + diff);
5296 AvFILLp(ary) += diff;
5299 dst = AvARRAY(ary) + AvFILLp(ary);
5301 for (i = after; i; i--) {
5309 Copy( MARK, AvARRAY(ary) + offset, newlen, SV* );
5312 MARK = ORIGMARK + 1;
5313 if (GIMME == G_ARRAY) { /* copy return vals to stack */
5315 Copy(tmparyval, MARK, length, SV*);
5317 EXTEND_MORTAL(length);
5318 for (i = length, dst = MARK; i; i--) {
5319 sv_2mortal(*dst); /* free them eventualy */
5326 else if (length--) {
5327 *MARK = tmparyval[length];
5330 while (length-- > 0)
5331 SvREFCNT_dec(tmparyval[length]);
5335 *MARK = &PL_sv_undef;
5336 Safefree(tmparyval);
5344 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
5345 register AV * const ary = MUTABLE_AV(*++MARK);
5346 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
5349 *MARK-- = SvTIED_obj(MUTABLE_SV(ary), mg);
5352 ENTER_with_name("call_PUSH");
5353 call_method("PUSH",G_SCALAR|G_DISCARD);
5354 LEAVE_with_name("call_PUSH");
5358 PL_delaymagic = DM_DELAY;
5359 for (++MARK; MARK <= SP; MARK++) {
5360 SV * const sv = newSV(0);
5362 sv_setsv(sv, *MARK);
5363 av_store(ary, AvFILLp(ary)+1, sv);
5365 if (PL_delaymagic & DM_ARRAY_ISA)
5366 mg_set(MUTABLE_SV(ary));
5371 if (OP_GIMME(PL_op, 0) != G_VOID) {
5372 PUSHi( AvFILL(ary) + 1 );
5381 AV * const av = PL_op->op_flags & OPf_SPECIAL
5382 ? MUTABLE_AV(GvAV(PL_defgv)) : MUTABLE_AV(POPs);
5383 SV * const sv = PL_op->op_type == OP_SHIFT ? av_shift(av) : av_pop(av);
5387 (void)sv_2mortal(sv);
5394 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
5395 register AV *ary = MUTABLE_AV(*++MARK);
5396 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
5399 *MARK-- = SvTIED_obj(MUTABLE_SV(ary), mg);
5402 ENTER_with_name("call_UNSHIFT");
5403 call_method("UNSHIFT",G_SCALAR|G_DISCARD);
5404 LEAVE_with_name("call_UNSHIFT");
5409 av_unshift(ary, SP - MARK);
5411 SV * const sv = newSVsv(*++MARK);
5412 (void)av_store(ary, i++, sv);
5416 if (OP_GIMME(PL_op, 0) != G_VOID) {
5417 PUSHi( AvFILL(ary) + 1 );
5426 if (GIMME == G_ARRAY) {
5427 if (PL_op->op_private & OPpREVERSE_INPLACE) {
5431 assert( MARK+1 == SP && *SP && SvTYPE(*SP) == SVt_PVAV);
5432 (void)POPMARK; /* remove mark associated with ex-OP_AASSIGN */
5433 av = MUTABLE_AV((*SP));
5434 /* In-place reversing only happens in void context for the array
5435 * assignment. We don't need to push anything on the stack. */
5438 if (SvMAGICAL(av)) {
5440 register SV *tmp = sv_newmortal();
5441 /* For SvCANEXISTDELETE */
5444 bool can_preserve = SvCANEXISTDELETE(av);
5446 for (i = 0, j = av_len(av); i < j; ++i, --j) {
5447 register SV *begin, *end;
5450 if (!av_exists(av, i)) {
5451 if (av_exists(av, j)) {
5452 register SV *sv = av_delete(av, j, 0);
5453 begin = *av_fetch(av, i, TRUE);
5454 sv_setsv_mg(begin, sv);
5458 else if (!av_exists(av, j)) {
5459 register SV *sv = av_delete(av, i, 0);
5460 end = *av_fetch(av, j, TRUE);
5461 sv_setsv_mg(end, sv);
5466 begin = *av_fetch(av, i, TRUE);
5467 end = *av_fetch(av, j, TRUE);
5468 sv_setsv(tmp, begin);
5469 sv_setsv_mg(begin, end);
5470 sv_setsv_mg(end, tmp);
5474 SV **begin = AvARRAY(av);
5477 SV **end = begin + AvFILLp(av);
5479 while (begin < end) {
5480 register SV * const tmp = *begin;
5491 register SV * const tmp = *MARK;
5495 /* safe as long as stack cannot get extended in the above */
5501 register char *down;
5506 SvUTF8_off(TARG); /* decontaminate */
5508 do_join(TARG, &PL_sv_no, MARK, SP);
5510 sv_setsv(TARG, SP > MARK ? *SP : find_rundefsv());
5511 if (! SvOK(TARG) && ckWARN(WARN_UNINITIALIZED))
5512 report_uninit(TARG);
5515 up = SvPV_force(TARG, len);
5517 if (DO_UTF8(TARG)) { /* first reverse each character */
5518 U8* s = (U8*)SvPVX(TARG);
5519 const U8* send = (U8*)(s + len);
5521 if (UTF8_IS_INVARIANT(*s)) {
5526 if (!utf8_to_uvchr(s, 0))
5530 down = (char*)(s - 1);
5531 /* reverse this character */
5535 *down-- = (char)tmp;
5541 down = SvPVX(TARG) + len - 1;
5545 *down-- = (char)tmp;
5547 (void)SvPOK_only_UTF8(TARG);
5559 register IV limit = POPi; /* note, negative is forever */
5560 SV * const sv = POPs;
5562 register const char *s = SvPV_const(sv, len);
5563 const bool do_utf8 = DO_UTF8(sv);
5564 const char *strend = s + len;
5566 register REGEXP *rx;
5568 register const char *m;
5570 const STRLEN slen = do_utf8 ? utf8_length((U8*)s, (U8*)strend) : (STRLEN)(strend - s);
5571 I32 maxiters = slen + 10;
5572 I32 trailing_empty = 0;
5574 const I32 origlimit = limit;
5577 const I32 gimme = GIMME_V;
5579 const I32 oldsave = PL_savestack_ix;
5580 U32 make_mortal = SVs_TEMP;
5585 Copy(&LvTARGOFF(POPs), &pm, 1, PMOP*);
5590 DIE(aTHX_ "panic: pp_split");
5593 TAINT_IF((RX_EXTFLAGS(rx) & RXf_PMf_LOCALE) &&
5594 (RX_EXTFLAGS(rx) & (RXf_WHITE | RXf_SKIPWHITE)));
5596 RX_MATCH_UTF8_set(rx, do_utf8);
5599 if (pm->op_pmreplrootu.op_pmtargetoff) {
5600 ary = GvAVn(MUTABLE_GV(PAD_SVl(pm->op_pmreplrootu.op_pmtargetoff)));
5603 if (pm->op_pmreplrootu.op_pmtargetgv) {
5604 ary = GvAVn(pm->op_pmreplrootu.op_pmtargetgv);
5609 if (ary && (gimme != G_ARRAY || (pm->op_pmflags & PMf_ONCE))) {
5615 if ((mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied))) {
5617 XPUSHs(SvTIED_obj(MUTABLE_SV(ary), mg));
5624 for (i = AvFILLp(ary); i >= 0; i--)
5625 AvARRAY(ary)[i] = &PL_sv_undef; /* don't free mere refs */
5627 /* temporarily switch stacks */
5628 SAVESWITCHSTACK(PL_curstack, ary);
5632 base = SP - PL_stack_base;
5634 if (RX_EXTFLAGS(rx) & RXf_SKIPWHITE) {
5636 while (*s == ' ' || is_utf8_space((U8*)s))
5639 else if (RX_EXTFLAGS(rx) & RXf_PMf_LOCALE) {
5640 while (isSPACE_LC(*s))
5648 if (RX_EXTFLAGS(rx) & PMf_MULTILINE) {
5652 gimme_scalar = gimme == G_SCALAR && !ary;
5655 limit = maxiters + 2;
5656 if (RX_EXTFLAGS(rx) & RXf_WHITE) {
5659 /* this one uses 'm' and is a negative test */
5661 while (m < strend && !( *m == ' ' || is_utf8_space((U8*)m) )) {
5662 const int t = UTF8SKIP(m);
5663 /* is_utf8_space returns FALSE for malform utf8 */
5669 } else if (RX_EXTFLAGS(rx) & RXf_PMf_LOCALE) {
5670 while (m < strend && !isSPACE_LC(*m))
5673 while (m < strend && !isSPACE(*m))
5686 dstr = newSVpvn_flags(s, m-s,
5687 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5691 /* skip the whitespace found last */
5693 s = m + UTF8SKIP(m);
5697 /* this one uses 's' and is a positive test */
5699 while (s < strend && ( *s == ' ' || is_utf8_space((U8*)s) ))
5701 } else if (RX_EXTFLAGS(rx) & RXf_PMf_LOCALE) {
5702 while (s < strend && isSPACE_LC(*s))
5705 while (s < strend && isSPACE(*s))
5710 else if (RX_EXTFLAGS(rx) & RXf_START_ONLY) {
5712 for (m = s; m < strend && *m != '\n'; m++)
5725 dstr = newSVpvn_flags(s, m-s,
5726 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5732 else if (RX_EXTFLAGS(rx) & RXf_NULL && !(s >= strend)) {
5734 Pre-extend the stack, either the number of bytes or
5735 characters in the string or a limited amount, triggered by:
5737 my ($x, $y) = split //, $str;
5741 if (!gimme_scalar) {
5742 const U32 items = limit - 1;
5751 /* keep track of how many bytes we skip over */
5761 dstr = newSVpvn_flags(m, s-m, SVf_UTF8 | make_mortal);
5774 dstr = newSVpvn(s, 1);
5790 else if (do_utf8 == (RX_UTF8(rx) != 0) &&
5791 (RX_EXTFLAGS(rx) & RXf_USE_INTUIT) && !RX_NPARENS(rx)
5792 && (RX_EXTFLAGS(rx) & RXf_CHECK_ALL)
5793 && !(RX_EXTFLAGS(rx) & RXf_ANCH)) {
5794 const int tail = (RX_EXTFLAGS(rx) & RXf_INTUIT_TAIL);
5795 SV * const csv = CALLREG_INTUIT_STRING(rx);
5797 len = RX_MINLENRET(rx);
5798 if (len == 1 && !RX_UTF8(rx) && !tail) {
5799 const char c = *SvPV_nolen_const(csv);
5801 for (m = s; m < strend && *m != c; m++)
5812 dstr = newSVpvn_flags(s, m-s,
5813 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5816 /* The rx->minlen is in characters but we want to step
5817 * s ahead by bytes. */
5819 s = (char*)utf8_hop((U8*)m, len);
5821 s = m + len; /* Fake \n at the end */
5825 while (s < strend && --limit &&
5826 (m = fbm_instr((unsigned char*)s, (unsigned char*)strend,
5827 csv, multiline ? FBMrf_MULTILINE : 0)) )
5836 dstr = newSVpvn_flags(s, m-s,
5837 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5840 /* The rx->minlen is in characters but we want to step
5841 * s ahead by bytes. */
5843 s = (char*)utf8_hop((U8*)m, len);
5845 s = m + len; /* Fake \n at the end */
5850 maxiters += slen * RX_NPARENS(rx);
5851 while (s < strend && --limit)
5855 rex_return = CALLREGEXEC(rx, (char*)s, (char*)strend, (char*)orig, 1 ,
5858 if (rex_return == 0)
5860 TAINT_IF(RX_MATCH_TAINTED(rx));
5861 if (RX_MATCH_COPIED(rx) && RX_SUBBEG(rx) != orig) {
5864 orig = RX_SUBBEG(rx);
5866 strend = s + (strend - m);
5868 m = RX_OFFS(rx)[0].start + orig;
5877 dstr = newSVpvn_flags(s, m-s,
5878 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5881 if (RX_NPARENS(rx)) {
5883 for (i = 1; i <= (I32)RX_NPARENS(rx); i++) {
5884 s = RX_OFFS(rx)[i].start + orig;
5885 m = RX_OFFS(rx)[i].end + orig;
5887 /* japhy (07/27/01) -- the (m && s) test doesn't catch
5888 parens that didn't match -- they should be set to
5889 undef, not the empty string */
5897 if (m >= orig && s >= orig) {
5898 dstr = newSVpvn_flags(s, m-s,
5899 (do_utf8 ? SVf_UTF8 : 0)
5903 dstr = &PL_sv_undef; /* undef, not "" */
5909 s = RX_OFFS(rx)[0].end + orig;
5913 if (!gimme_scalar) {
5914 iters = (SP - PL_stack_base) - base;
5916 if (iters > maxiters)
5917 DIE(aTHX_ "Split loop");
5919 /* keep field after final delim? */
5920 if (s < strend || (iters && origlimit)) {
5921 if (!gimme_scalar) {
5922 const STRLEN l = strend - s;
5923 dstr = newSVpvn_flags(s, l, (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5928 else if (!origlimit) {
5930 iters -= trailing_empty;
5932 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0)) {
5933 if (TOPs && !make_mortal)
5935 *SP-- = &PL_sv_undef;
5942 LEAVE_SCOPE(oldsave); /* may undo an earlier SWITCHSTACK */
5946 if (SvSMAGICAL(ary)) {
5948 mg_set(MUTABLE_SV(ary));
5951 if (gimme == G_ARRAY) {
5953 Copy(AvARRAY(ary), SP + 1, iters, SV*);
5960 ENTER_with_name("call_PUSH");
5961 call_method("PUSH",G_SCALAR|G_DISCARD);
5962 LEAVE_with_name("call_PUSH");
5964 if (gimme == G_ARRAY) {
5966 /* EXTEND should not be needed - we just popped them */
5968 for (i=0; i < iters; i++) {
5969 SV **svp = av_fetch(ary, i, FALSE);
5970 PUSHs((svp) ? *svp : &PL_sv_undef);
5977 if (gimme == G_ARRAY)
5989 SV *const sv = PAD_SVl(PL_op->op_targ);
5991 if (SvPADSTALE(sv)) {
5994 RETURNOP(cLOGOP->op_other);
5996 RETURNOP(cLOGOP->op_next);
6005 assert(SvTYPE(retsv) != SVt_PVCV);
6007 if (SvTYPE(retsv) == SVt_PVAV || SvTYPE(retsv) == SVt_PVHV) {
6008 retsv = refto(retsv);
6015 PP(unimplemented_op)
6018 DIE(aTHX_ "panic: unimplemented op %s (#%d) called", OP_NAME(PL_op),
6026 HV * const hv = (HV*)POPs;
6028 if (SvRMAGICAL(hv)) {
6029 MAGIC * const mg = mg_find((SV*)hv, PERL_MAGIC_tied);
6031 XPUSHs(magic_scalarpack(hv, mg));
6036 XPUSHs(boolSV(HvKEYS(hv) != 0));
6042 * c-indentation-style: bsd
6044 * indent-tabs-mode: t
6047 * ex: set ts=8 sts=4 sw=4 noet: