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 if (PL_op->op_private & OPpLVAL_INTRO)
67 if (!(PL_op->op_private & OPpPAD_STATE))
68 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
70 if (PL_op->op_flags & OPf_REF) {
74 if (GIMME == G_SCALAR)
75 Perl_croak(aTHX_ "Can't return array to lvalue scalar context");
80 if (gimme == G_ARRAY) {
81 const I32 maxarg = AvFILL(MUTABLE_AV(TARG)) + 1;
83 if (SvMAGICAL(TARG)) {
85 for (i=0; i < (U32)maxarg; i++) {
86 SV * const * const svp = av_fetch(MUTABLE_AV(TARG), i, FALSE);
87 SP[i+1] = (svp) ? *svp : &PL_sv_undef;
91 Copy(AvARRAY((const AV *)TARG), SP+1, maxarg, SV*);
95 else if (gimme == G_SCALAR) {
96 SV* const sv = sv_newmortal();
97 const I32 maxarg = AvFILL(MUTABLE_AV(TARG)) + 1;
110 if (PL_op->op_private & OPpLVAL_INTRO)
111 if (!(PL_op->op_private & OPpPAD_STATE))
112 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
113 if (PL_op->op_flags & OPf_REF)
116 if (GIMME == G_SCALAR)
117 Perl_croak(aTHX_ "Can't return hash to lvalue scalar context");
121 if (gimme == G_ARRAY) {
124 else if (gimme == G_SCALAR) {
125 SV* const sv = Perl_hv_scalar(aTHX_ MUTABLE_HV(TARG));
133 const char S_no_symref_sv[] =
134 "Can't use string (\"%" SVf32 "\"%s) as %s ref while \"strict refs\" in use";
142 tryAMAGICunDEREF(to_gv);
145 if (SvTYPE(sv) == SVt_PVIO) {
146 GV * const gv = MUTABLE_GV(sv_newmortal());
147 gv_init(gv, 0, "", 0, 0);
148 GvIOp(gv) = MUTABLE_IO(sv);
149 SvREFCNT_inc_void_NN(sv);
152 else if (!isGV_with_GP(sv))
153 DIE(aTHX_ "Not a GLOB reference");
156 if (!isGV_with_GP(sv)) {
157 if (SvGMAGICAL(sv)) {
162 if (!SvOK(sv) && sv != &PL_sv_undef) {
163 /* If this is a 'my' scalar and flag is set then vivify
167 Perl_croak(aTHX_ "%s", PL_no_modify);
168 if (PL_op->op_private & OPpDEREF) {
170 if (cUNOP->op_targ) {
172 SV * const namesv = PAD_SV(cUNOP->op_targ);
173 const char * const name = SvPV(namesv, len);
174 gv = MUTABLE_GV(newSV(0));
175 gv_init(gv, CopSTASH(PL_curcop), name, len, 0);
178 const char * const name = CopSTASHPV(PL_curcop);
181 prepare_SV_for_RV(sv);
182 SvRV_set(sv, MUTABLE_SV(gv));
187 if (PL_op->op_flags & OPf_REF ||
188 PL_op->op_private & HINT_STRICT_REFS)
189 DIE(aTHX_ PL_no_usym, "a symbol");
190 if (ckWARN(WARN_UNINITIALIZED))
194 if ((PL_op->op_flags & OPf_SPECIAL) &&
195 !(PL_op->op_flags & OPf_MOD))
197 SV * const temp = MUTABLE_SV(gv_fetchsv(sv, 0, SVt_PVGV));
199 && (!is_gv_magical_sv(sv,0)
200 || !(sv = MUTABLE_SV(gv_fetchsv(sv, GV_ADD,
207 if (PL_op->op_private & HINT_STRICT_REFS)
208 DIE(aTHX_ S_no_symref_sv, sv, (SvCUR(sv)>32 ? "..." : ""), "a symbol");
209 if ((PL_op->op_private & (OPpLVAL_INTRO|OPpDONT_INIT_GV))
210 == OPpDONT_INIT_GV) {
211 /* We are the target of a coderef assignment. Return
212 the scalar unchanged, and let pp_sasssign deal with
216 sv = MUTABLE_SV(gv_fetchsv(sv, GV_ADD, SVt_PVGV));
220 if (PL_op->op_private & OPpLVAL_INTRO)
221 save_gp(MUTABLE_GV(sv), !(PL_op->op_flags & OPf_SPECIAL));
226 /* Helper function for pp_rv2sv and pp_rv2av */
228 Perl_softref2xv(pTHX_ SV *const sv, const char *const what,
229 const svtype type, SV ***spp)
234 PERL_ARGS_ASSERT_SOFTREF2XV;
236 if (PL_op->op_private & HINT_STRICT_REFS) {
238 Perl_die(aTHX_ S_no_symref_sv, sv, (SvCUR(sv)>32 ? "..." : ""), what);
240 Perl_die(aTHX_ PL_no_usym, what);
243 if (PL_op->op_flags & OPf_REF)
244 Perl_die(aTHX_ PL_no_usym, what);
245 if (ckWARN(WARN_UNINITIALIZED))
247 if (type != SVt_PV && GIMME_V == G_ARRAY) {
251 **spp = &PL_sv_undef;
254 if ((PL_op->op_flags & OPf_SPECIAL) &&
255 !(PL_op->op_flags & OPf_MOD))
257 gv = gv_fetchsv(sv, 0, type);
259 && (!is_gv_magical_sv(sv,0)
260 || !(gv = gv_fetchsv(sv, GV_ADD, type))))
262 **spp = &PL_sv_undef;
267 gv = gv_fetchsv(sv, GV_ADD, type);
279 tryAMAGICunDEREF(to_sv);
282 switch (SvTYPE(sv)) {
288 DIE(aTHX_ "Not a SCALAR reference");
295 if (!isGV_with_GP(gv)) {
296 if (SvGMAGICAL(sv)) {
301 gv = Perl_softref2xv(aTHX_ sv, "a SCALAR", SVt_PV, &sp);
307 if (PL_op->op_flags & OPf_MOD) {
308 if (PL_op->op_private & OPpLVAL_INTRO) {
309 if (cUNOP->op_first->op_type == OP_NULL)
310 sv = save_scalar(MUTABLE_GV(TOPs));
312 sv = save_scalar(gv);
314 Perl_croak(aTHX_ "%s", PL_no_localize_ref);
316 else if (PL_op->op_private & OPpDEREF)
317 vivify_ref(sv, PL_op->op_private & OPpDEREF);
326 AV * const av = MUTABLE_AV(TOPs);
327 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
329 SV ** const sv = Perl_av_arylen_p(aTHX_ MUTABLE_AV(av));
331 *sv = newSV_type(SVt_PVMG);
332 sv_magic(*sv, MUTABLE_SV(av), PERL_MAGIC_arylen, NULL, 0);
336 SETs(sv_2mortal(newSViv(
337 AvFILL(MUTABLE_AV(av)) + CopARYBASE_get(PL_curcop)
345 dVAR; dSP; dTARGET; dPOPss;
347 if (PL_op->op_flags & OPf_MOD || LVRET) {
348 if (SvTYPE(TARG) < SVt_PVLV) {
349 sv_upgrade(TARG, SVt_PVLV);
350 sv_magic(TARG, NULL, PERL_MAGIC_pos, NULL, 0);
354 if (LvTARG(TARG) != sv) {
355 SvREFCNT_dec(LvTARG(TARG));
356 LvTARG(TARG) = SvREFCNT_inc_simple(sv);
358 PUSHs(TARG); /* no SvSETMAGIC */
362 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
363 const MAGIC * const mg = mg_find(sv, PERL_MAGIC_regex_global);
364 if (mg && mg->mg_len >= 0) {
368 PUSHi(i + CopARYBASE_get(PL_curcop));
381 const I32 flags = (PL_op->op_flags & OPf_SPECIAL)
383 : ((PL_op->op_private & (OPpLVAL_INTRO|OPpMAY_RETURN_CONSTANT)) == OPpMAY_RETURN_CONSTANT)
386 /* We usually try to add a non-existent subroutine in case of AUTOLOAD. */
387 /* (But not in defined().) */
389 CV *cv = sv_2cv(TOPs, &stash_unused, &gv, flags);
392 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
393 if ((PL_op->op_private & OPpLVAL_INTRO)) {
394 if (gv && GvCV(gv) == cv && (gv = gv_autoload4(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv), FALSE)))
397 DIE(aTHX_ "Can't modify non-lvalue subroutine call");
400 else if ((flags == (GV_ADD|GV_NOEXPAND)) && gv && SvROK(gv)) {
404 cv = MUTABLE_CV(&PL_sv_undef);
405 SETs(MUTABLE_SV(cv));
415 SV *ret = &PL_sv_undef;
417 if (SvPOK(TOPs) && SvCUR(TOPs) >= 7) {
418 const char * s = SvPVX_const(TOPs);
419 if (strnEQ(s, "CORE::", 6)) {
420 const int code = keyword(s + 6, SvCUR(TOPs) - 6, 1);
421 if (code < 0) { /* Overridable. */
422 #define MAX_ARGS_OP ((sizeof(I32) - 1) * 2)
423 int i = 0, n = 0, seen_question = 0, defgv = 0;
425 char str[ MAX_ARGS_OP * 2 + 2 ]; /* One ';', one '\0' */
427 if (code == -KEY_chop || code == -KEY_chomp
428 || code == -KEY_exec || code == -KEY_system)
430 if (code == -KEY_mkdir) {
431 ret = newSVpvs_flags("_;$", SVs_TEMP);
434 if (code == -KEY_keys || code == -KEY_values || code == -KEY_each) {
435 ret = newSVpvs_flags("\\[@%]", SVs_TEMP);
438 if (code == -KEY_readpipe) {
439 s = "CORE::backtick";
441 while (i < MAXO) { /* The slow way. */
442 if (strEQ(s + 6, PL_op_name[i])
443 || strEQ(s + 6, PL_op_desc[i]))
449 goto nonesuch; /* Should not happen... */
451 defgv = PL_opargs[i] & OA_DEFGV;
452 oa = PL_opargs[i] >> OASHIFT;
454 if (oa & OA_OPTIONAL && !seen_question && !defgv) {
458 if ((oa & (OA_OPTIONAL - 1)) >= OA_AVREF
459 && (oa & (OA_OPTIONAL - 1)) <= OA_SCALARREF
460 /* But globs are already references (kinda) */
461 && (oa & (OA_OPTIONAL - 1)) != OA_FILEREF
465 str[n++] = ("?$@@%&*$")[oa & (OA_OPTIONAL - 1)];
468 if (defgv && str[n - 1] == '$')
471 ret = newSVpvn_flags(str, n - 1, SVs_TEMP);
473 else if (code) /* Non-Overridable */
475 else { /* None such */
477 DIE(aTHX_ "Can't find an opnumber for \"%s\"", s+6);
481 cv = sv_2cv(TOPs, &stash, &gv, 0);
483 ret = newSVpvn_flags(SvPVX_const(cv), SvCUR(cv), SVs_TEMP);
492 CV *cv = MUTABLE_CV(PAD_SV(PL_op->op_targ));
494 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
496 PUSHs(MUTABLE_SV(cv));
510 if (GIMME != G_ARRAY) {
514 *MARK = &PL_sv_undef;
515 *MARK = refto(*MARK);
519 EXTEND_MORTAL(SP - MARK);
521 *MARK = refto(*MARK);
526 S_refto(pTHX_ SV *sv)
531 PERL_ARGS_ASSERT_REFTO;
533 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
536 if (!(sv = LvTARG(sv)))
539 SvREFCNT_inc_void_NN(sv);
541 else if (SvTYPE(sv) == SVt_PVAV) {
542 if (!AvREAL((const AV *)sv) && AvREIFY((const AV *)sv))
543 av_reify(MUTABLE_AV(sv));
545 SvREFCNT_inc_void_NN(sv);
547 else if (SvPADTMP(sv) && !IS_PADGV(sv))
551 SvREFCNT_inc_void_NN(sv);
554 sv_upgrade(rv, SVt_IV);
564 SV * const sv = POPs;
569 if (!sv || !SvROK(sv))
572 pv = sv_reftype(SvRV(sv),TRUE);
573 PUSHp(pv, strlen(pv));
583 stash = CopSTASH(PL_curcop);
585 SV * const ssv = POPs;
589 if (ssv && !SvGMAGICAL(ssv) && !SvAMAGIC(ssv) && SvROK(ssv))
590 Perl_croak(aTHX_ "Attempt to bless into a reference");
591 ptr = SvPV_const(ssv,len);
593 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
594 "Explicit blessing to '' (assuming package main)");
595 stash = gv_stashpvn(ptr, len, GV_ADD);
598 (void)sv_bless(TOPs, stash);
607 const char * const elem = SvPV_nolen_const(sv);
608 GV * const gv = MUTABLE_GV(POPs);
613 /* elem will always be NUL terminated. */
614 const char * const second_letter = elem + 1;
617 if (strEQ(second_letter, "RRAY"))
618 tmpRef = MUTABLE_SV(GvAV(gv));
621 if (strEQ(second_letter, "ODE"))
622 tmpRef = MUTABLE_SV(GvCVu(gv));
625 if (strEQ(second_letter, "ILEHANDLE")) {
626 /* finally deprecated in 5.8.0 */
627 deprecate("*glob{FILEHANDLE}");
628 tmpRef = MUTABLE_SV(GvIOp(gv));
631 if (strEQ(second_letter, "ORMAT"))
632 tmpRef = MUTABLE_SV(GvFORM(gv));
635 if (strEQ(second_letter, "LOB"))
636 tmpRef = MUTABLE_SV(gv);
639 if (strEQ(second_letter, "ASH"))
640 tmpRef = MUTABLE_SV(GvHV(gv));
643 if (*second_letter == 'O' && !elem[2])
644 tmpRef = MUTABLE_SV(GvIOp(gv));
647 if (strEQ(second_letter, "AME"))
648 sv = newSVhek(GvNAME_HEK(gv));
651 if (strEQ(second_letter, "ACKAGE")) {
652 const HV * const stash = GvSTASH(gv);
653 const HEK * const hek = stash ? HvNAME_HEK(stash) : NULL;
654 sv = hek ? newSVhek(hek) : newSVpvs("__ANON__");
658 if (strEQ(second_letter, "CALAR"))
673 /* Pattern matching */
678 register unsigned char *s;
681 register I32 *sfirst;
685 if (sv == PL_lastscream) {
689 s = (unsigned char*)(SvPV(sv, len));
691 if (pos <= 0 || !SvPOK(sv) || SvUTF8(sv)) {
692 /* No point in studying a zero length string, and not safe to study
693 anything that doesn't appear to be a simple scalar (and hence might
694 change between now and when the regexp engine runs without our set
695 magic ever running) such as a reference to an object with overloaded
701 SvSCREAM_off(PL_lastscream);
702 SvREFCNT_dec(PL_lastscream);
704 PL_lastscream = SvREFCNT_inc_simple(sv);
706 s = (unsigned char*)(SvPV(sv, len));
710 if (pos > PL_maxscream) {
711 if (PL_maxscream < 0) {
712 PL_maxscream = pos + 80;
713 Newx(PL_screamfirst, 256, I32);
714 Newx(PL_screamnext, PL_maxscream, I32);
717 PL_maxscream = pos + pos / 4;
718 Renew(PL_screamnext, PL_maxscream, I32);
722 sfirst = PL_screamfirst;
723 snext = PL_screamnext;
725 if (!sfirst || !snext)
726 DIE(aTHX_ "do_study: out of memory");
728 for (ch = 256; ch; --ch)
733 register const I32 ch = s[pos];
735 snext[pos] = sfirst[ch] - pos;
742 /* piggyback on m//g magic */
743 sv_magic(sv, NULL, PERL_MAGIC_regex_global, NULL, 0);
752 if (PL_op->op_flags & OPf_STACKED)
754 else if (PL_op->op_private & OPpTARGET_MY)
760 TARG = sv_newmortal();
765 /* Lvalue operators. */
777 dVAR; dSP; dMARK; dTARGET; dORIGMARK;
779 do_chop(TARG, *++MARK);
788 SETi(do_chomp(TOPs));
794 dVAR; dSP; dMARK; dTARGET;
795 register I32 count = 0;
798 count += do_chomp(POPs);
808 if (!PL_op->op_private) {
817 SV_CHECK_THINKFIRST_COW_DROP(sv);
819 switch (SvTYPE(sv)) {
823 av_undef(MUTABLE_AV(sv));
826 hv_undef(MUTABLE_HV(sv));
829 if (cv_const_sv((const CV *)sv))
830 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Constant subroutine %s undefined",
831 CvANON((const CV *)sv) ? "(anonymous)"
832 : GvENAME(CvGV((const CV *)sv)));
836 /* let user-undef'd sub keep its identity */
837 GV* const gv = CvGV((const CV *)sv);
838 cv_undef(MUTABLE_CV(sv));
839 CvGV((const CV *)sv) = gv;
844 SvSetMagicSV(sv, &PL_sv_undef);
847 else if (isGV_with_GP(sv)) {
852 if((stash = GvHV((const GV *)sv)) && HvNAME_get(stash))
853 mro_isa_changed_in(stash);
854 /* undef *Pkg::meth_name ... */
855 else if(GvCVu((const GV *)sv) && (stash = GvSTASH((const GV *)sv))
856 && HvNAME_get(stash))
857 mro_method_changed_in(stash);
859 gp_free(MUTABLE_GV(sv));
861 GvGP(sv) = gp_ref(gp);
863 GvLINE(sv) = CopLINE(PL_curcop);
864 GvEGV(sv) = MUTABLE_GV(sv);
870 if (SvTYPE(sv) >= SVt_PV && SvPVX_const(sv) && SvLEN(sv)) {
885 if (SvTYPE(TOPs) >= SVt_PVAV || isGV_with_GP(TOPs))
886 DIE(aTHX_ "%s", PL_no_modify);
887 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
888 && SvIVX(TOPs) != IV_MIN)
890 SvIV_set(TOPs, SvIVX(TOPs) - 1);
891 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
902 if (SvTYPE(TOPs) >= SVt_PVAV || isGV_with_GP(TOPs))
903 DIE(aTHX_ "%s", PL_no_modify);
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 DIE(aTHX_ "%s", PL_no_modify);
926 sv_setsv(TARG, TOPs);
927 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
928 && SvIVX(TOPs) != IV_MIN)
930 SvIV_set(TOPs, SvIVX(TOPs) - 1);
931 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
940 /* Ordinary operators. */
944 dVAR; dSP; dATARGET; SV *svl, *svr;
945 #ifdef PERL_PRESERVE_IVUV
948 tryAMAGICbin(pow,opASSIGN);
949 svl = sv_2num(TOPm1s);
951 #ifdef PERL_PRESERVE_IVUV
952 /* For integer to integer power, we do the calculation by hand wherever
953 we're sure it is safe; otherwise we call pow() and try to convert to
954 integer afterwards. */
967 const IV iv = SvIVX(svr);
971 goto float_it; /* Can't do negative powers this way. */
975 baseuok = SvUOK(svl);
979 const IV iv = SvIVX(svl);
982 baseuok = TRUE; /* effectively it's a UV now */
984 baseuv = -iv; /* abs, baseuok == false records sign */
987 /* now we have integer ** positive integer. */
990 /* foo & (foo - 1) is zero only for a power of 2. */
991 if (!(baseuv & (baseuv - 1))) {
992 /* We are raising power-of-2 to a positive integer.
993 The logic here will work for any base (even non-integer
994 bases) but it can be less accurate than
995 pow (base,power) or exp (power * log (base)) when the
996 intermediate values start to spill out of the mantissa.
997 With powers of 2 we know this can't happen.
998 And powers of 2 are the favourite thing for perl
999 programmers to notice ** not doing what they mean. */
1001 NV base = baseuok ? baseuv : -(NV)baseuv;
1006 while (power >>= 1) {
1017 register unsigned int highbit = 8 * sizeof(UV);
1018 register unsigned int diff = 8 * sizeof(UV);
1019 while (diff >>= 1) {
1021 if (baseuv >> highbit) {
1025 /* we now have baseuv < 2 ** highbit */
1026 if (power * highbit <= 8 * sizeof(UV)) {
1027 /* result will definitely fit in UV, so use UV math
1028 on same algorithm as above */
1029 register UV result = 1;
1030 register UV base = baseuv;
1031 const bool odd_power = (bool)(power & 1);
1035 while (power >>= 1) {
1042 if (baseuok || !odd_power)
1043 /* answer is positive */
1045 else if (result <= (UV)IV_MAX)
1046 /* answer negative, fits in IV */
1047 SETi( -(IV)result );
1048 else if (result == (UV)IV_MIN)
1049 /* 2's complement assumption: special case IV_MIN */
1052 /* answer negative, doesn't fit */
1053 SETn( -(NV)result );
1063 NV right = SvNV(svr);
1064 NV left = SvNV(svl);
1067 #if defined(USE_LONG_DOUBLE) && defined(HAS_AIX_POWL_NEG_BASE_BUG)
1069 We are building perl with long double support and are on an AIX OS
1070 afflicted with a powl() function that wrongly returns NaNQ for any
1071 negative base. This was reported to IBM as PMR #23047-379 on
1072 03/06/2006. The problem exists in at least the following versions
1073 of AIX and the libm fileset, and no doubt others as well:
1075 AIX 4.3.3-ML10 bos.adt.libm 4.3.3.50
1076 AIX 5.1.0-ML04 bos.adt.libm 5.1.0.29
1077 AIX 5.2.0 bos.adt.libm 5.2.0.85
1079 So, until IBM fixes powl(), we provide the following workaround to
1080 handle the problem ourselves. Our logic is as follows: for
1081 negative bases (left), we use fmod(right, 2) to check if the
1082 exponent is an odd or even integer:
1084 - if odd, powl(left, right) == -powl(-left, right)
1085 - if even, powl(left, right) == powl(-left, right)
1087 If the exponent is not an integer, the result is rightly NaNQ, so
1088 we just return that (as NV_NAN).
1092 NV mod2 = Perl_fmod( right, 2.0 );
1093 if (mod2 == 1.0 || mod2 == -1.0) { /* odd integer */
1094 SETn( -Perl_pow( -left, right) );
1095 } else if (mod2 == 0.0) { /* even integer */
1096 SETn( Perl_pow( -left, right) );
1097 } else { /* fractional power */
1101 SETn( Perl_pow( left, right) );
1104 SETn( Perl_pow( left, right) );
1105 #endif /* HAS_AIX_POWL_NEG_BASE_BUG */
1107 #ifdef PERL_PRESERVE_IVUV
1117 dVAR; dSP; dATARGET; SV *svl, *svr;
1118 tryAMAGICbin(mult,opASSIGN);
1119 svl = sv_2num(TOPm1s);
1120 svr = sv_2num(TOPs);
1121 #ifdef PERL_PRESERVE_IVUV
1124 /* Unless the left argument is integer in range we are going to have to
1125 use NV maths. Hence only attempt to coerce the right argument if
1126 we know the left is integer. */
1127 /* Left operand is defined, so is it IV? */
1130 bool auvok = SvUOK(svl);
1131 bool buvok = SvUOK(svr);
1132 const UV topmask = (~ (UV)0) << (4 * sizeof (UV));
1133 const UV botmask = ~((~ (UV)0) << (4 * sizeof (UV)));
1142 const IV aiv = SvIVX(svl);
1145 auvok = TRUE; /* effectively it's a UV now */
1147 alow = -aiv; /* abs, auvok == false records sign */
1153 const IV biv = SvIVX(svr);
1156 buvok = TRUE; /* effectively it's a UV now */
1158 blow = -biv; /* abs, buvok == false records sign */
1162 /* If this does sign extension on unsigned it's time for plan B */
1163 ahigh = alow >> (4 * sizeof (UV));
1165 bhigh = blow >> (4 * sizeof (UV));
1167 if (ahigh && bhigh) {
1169 /* eg 32 bit is at least 0x10000 * 0x10000 == 0x100000000
1170 which is overflow. Drop to NVs below. */
1171 } else if (!ahigh && !bhigh) {
1172 /* eg 32 bit is at most 0xFFFF * 0xFFFF == 0xFFFE0001
1173 so the unsigned multiply cannot overflow. */
1174 const UV product = alow * blow;
1175 if (auvok == buvok) {
1176 /* -ve * -ve or +ve * +ve gives a +ve result. */
1180 } else if (product <= (UV)IV_MIN) {
1181 /* 2s complement assumption that (UV)-IV_MIN is correct. */
1182 /* -ve result, which could overflow an IV */
1184 SETi( -(IV)product );
1186 } /* else drop to NVs below. */
1188 /* One operand is large, 1 small */
1191 /* swap the operands */
1193 bhigh = blow; /* bhigh now the temp var for the swap */
1197 /* now, ((ahigh * blow) << half_UV_len) + (alow * blow)
1198 multiplies can't overflow. shift can, add can, -ve can. */
1199 product_middle = ahigh * blow;
1200 if (!(product_middle & topmask)) {
1201 /* OK, (ahigh * blow) won't lose bits when we shift it. */
1203 product_middle <<= (4 * sizeof (UV));
1204 product_low = alow * blow;
1206 /* as for pp_add, UV + something mustn't get smaller.
1207 IIRC ANSI mandates this wrapping *behaviour* for
1208 unsigned whatever the actual representation*/
1209 product_low += product_middle;
1210 if (product_low >= product_middle) {
1211 /* didn't overflow */
1212 if (auvok == buvok) {
1213 /* -ve * -ve or +ve * +ve gives a +ve result. */
1215 SETu( product_low );
1217 } else if (product_low <= (UV)IV_MIN) {
1218 /* 2s complement assumption again */
1219 /* -ve result, which could overflow an IV */
1221 SETi( -(IV)product_low );
1223 } /* else drop to NVs below. */
1225 } /* product_middle too large */
1226 } /* ahigh && bhigh */
1231 NV right = SvNV(svr);
1232 NV left = SvNV(svl);
1234 SETn( left * right );
1241 dVAR; dSP; dATARGET; SV *svl, *svr;
1242 tryAMAGICbin(div,opASSIGN);
1243 svl = sv_2num(TOPm1s);
1244 svr = sv_2num(TOPs);
1245 /* Only try to do UV divide first
1246 if ((SLOPPYDIVIDE is true) or
1247 (PERL_PRESERVE_IVUV is true and one or both SV is a UV too large
1249 The assumption is that it is better to use floating point divide
1250 whenever possible, only doing integer divide first if we can't be sure.
1251 If NV_PRESERVES_UV is true then we know at compile time that no UV
1252 can be too large to preserve, so don't need to compile the code to
1253 test the size of UVs. */
1256 # define PERL_TRY_UV_DIVIDE
1257 /* ensure that 20./5. == 4. */
1259 # ifdef PERL_PRESERVE_IVUV
1260 # ifndef NV_PRESERVES_UV
1261 # define PERL_TRY_UV_DIVIDE
1266 #ifdef PERL_TRY_UV_DIVIDE
1271 bool left_non_neg = SvUOK(svl);
1272 bool right_non_neg = SvUOK(svr);
1276 if (right_non_neg) {
1280 const IV biv = SvIVX(svr);
1283 right_non_neg = TRUE; /* effectively it's a UV now */
1289 /* historically undef()/0 gives a "Use of uninitialized value"
1290 warning before dieing, hence this test goes here.
1291 If it were immediately before the second SvIV_please, then
1292 DIE() would be invoked before left was even inspected, so
1293 no inpsection would give no warning. */
1295 DIE(aTHX_ "Illegal division by zero");
1301 const IV aiv = SvIVX(svl);
1304 left_non_neg = TRUE; /* effectively it's a UV now */
1313 /* For sloppy divide we always attempt integer division. */
1315 /* Otherwise we only attempt it if either or both operands
1316 would not be preserved by an NV. If both fit in NVs
1317 we fall through to the NV divide code below. However,
1318 as left >= right to ensure integer result here, we know that
1319 we can skip the test on the right operand - right big
1320 enough not to be preserved can't get here unless left is
1323 && (left > ((UV)1 << NV_PRESERVES_UV_BITS))
1326 /* Integer division can't overflow, but it can be imprecise. */
1327 const UV result = left / right;
1328 if (result * right == left) {
1329 SP--; /* result is valid */
1330 if (left_non_neg == right_non_neg) {
1331 /* signs identical, result is positive. */
1335 /* 2s complement assumption */
1336 if (result <= (UV)IV_MIN)
1337 SETi( -(IV)result );
1339 /* It's exact but too negative for IV. */
1340 SETn( -(NV)result );
1343 } /* tried integer divide but it was not an integer result */
1344 } /* else (PERL_ABS(result) < 1.0) or (both UVs in range for NV) */
1345 } /* left wasn't SvIOK */
1346 } /* right wasn't SvIOK */
1347 #endif /* PERL_TRY_UV_DIVIDE */
1349 NV right = SvNV(svr);
1350 NV left = SvNV(svl);
1351 (void)POPs;(void)POPs;
1352 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
1353 if (! Perl_isnan(right) && right == 0.0)
1357 DIE(aTHX_ "Illegal division by zero");
1358 PUSHn( left / right );
1365 dVAR; dSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
1369 bool left_neg = FALSE;
1370 bool right_neg = FALSE;
1371 bool use_double = FALSE;
1372 bool dright_valid = FALSE;
1376 SV * const svr = sv_2num(TOPs);
1379 right_neg = !SvUOK(svr);
1383 const IV biv = SvIVX(svr);
1386 right_neg = FALSE; /* effectively it's a UV now */
1394 right_neg = dright < 0;
1397 if (dright < UV_MAX_P1) {
1398 right = U_V(dright);
1399 dright_valid = TRUE; /* In case we need to use double below. */
1406 /* At this point use_double is only true if right is out of range for
1407 a UV. In range NV has been rounded down to nearest UV and
1408 use_double false. */
1409 svl = sv_2num(TOPs);
1411 if (!use_double && SvIOK(svl)) {
1413 left_neg = !SvUOK(svl);
1417 const IV aiv = SvIVX(svl);
1420 left_neg = FALSE; /* effectively it's a UV now */
1429 left_neg = dleft < 0;
1433 /* This should be exactly the 5.6 behaviour - if left and right are
1434 both in range for UV then use U_V() rather than floor. */
1436 if (dleft < UV_MAX_P1) {
1437 /* right was in range, so is dleft, so use UVs not double.
1441 /* left is out of range for UV, right was in range, so promote
1442 right (back) to double. */
1444 /* The +0.5 is used in 5.6 even though it is not strictly
1445 consistent with the implicit +0 floor in the U_V()
1446 inside the #if 1. */
1447 dleft = Perl_floor(dleft + 0.5);
1450 dright = Perl_floor(dright + 0.5);
1461 DIE(aTHX_ "Illegal modulus zero");
1463 dans = Perl_fmod(dleft, dright);
1464 if ((left_neg != right_neg) && dans)
1465 dans = dright - dans;
1468 sv_setnv(TARG, dans);
1474 DIE(aTHX_ "Illegal modulus zero");
1477 if ((left_neg != right_neg) && ans)
1480 /* XXX may warn: unary minus operator applied to unsigned type */
1481 /* could change -foo to be (~foo)+1 instead */
1482 if (ans <= ~((UV)IV_MAX)+1)
1483 sv_setiv(TARG, ~ans+1);
1485 sv_setnv(TARG, -(NV)ans);
1488 sv_setuv(TARG, ans);
1497 dVAR; dSP; dATARGET; tryAMAGICbin(repeat,opASSIGN);
1504 const UV uv = SvUV(sv);
1506 count = IV_MAX; /* The best we can do? */
1510 const IV iv = SvIV(sv);
1517 else if (SvNOKp(sv)) {
1518 const NV nv = SvNV(sv);
1526 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1528 static const char oom_list_extend[] = "Out of memory during list extend";
1529 const I32 items = SP - MARK;
1530 const I32 max = items * count;
1532 MEM_WRAP_CHECK_1(max, SV*, oom_list_extend);
1533 /* Did the max computation overflow? */
1534 if (items > 0 && max > 0 && (max < items || max < count))
1535 Perl_croak(aTHX_ oom_list_extend);
1540 /* This code was intended to fix 20010809.028:
1543 for (($x =~ /./g) x 2) {
1544 print chop; # "abcdabcd" expected as output.
1547 * but that change (#11635) broke this code:
1549 $x = [("foo")x2]; # only one "foo" ended up in the anonlist.
1551 * I can't think of a better fix that doesn't introduce
1552 * an efficiency hit by copying the SVs. The stack isn't
1553 * refcounted, and mortalisation obviously doesn't
1554 * Do The Right Thing when the stack has more than
1555 * one pointer to the same mortal value.
1559 *SP = sv_2mortal(newSVsv(*SP));
1569 repeatcpy((char*)(MARK + items), (char*)MARK,
1570 items * sizeof(const SV *), count - 1);
1573 else if (count <= 0)
1576 else { /* Note: mark already snarfed by pp_list */
1577 SV * const tmpstr = POPs;
1580 static const char oom_string_extend[] =
1581 "Out of memory during string extend";
1583 SvSetSV(TARG, tmpstr);
1584 SvPV_force(TARG, len);
1585 isutf = DO_UTF8(TARG);
1590 const STRLEN max = (UV)count * len;
1591 if (len > MEM_SIZE_MAX / count)
1592 Perl_croak(aTHX_ oom_string_extend);
1593 MEM_WRAP_CHECK_1(max, char, oom_string_extend);
1594 SvGROW(TARG, max + 1);
1595 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
1596 SvCUR_set(TARG, SvCUR(TARG) * count);
1598 *SvEND(TARG) = '\0';
1601 (void)SvPOK_only_UTF8(TARG);
1603 (void)SvPOK_only(TARG);
1605 if (PL_op->op_private & OPpREPEAT_DOLIST) {
1606 /* The parser saw this as a list repeat, and there
1607 are probably several items on the stack. But we're
1608 in scalar context, and there's no pp_list to save us
1609 now. So drop the rest of the items -- robin@kitsite.com
1622 dVAR; dSP; dATARGET; bool useleft; SV *svl, *svr;
1623 tryAMAGICbin(subtr,opASSIGN);
1624 svl = sv_2num(TOPm1s);
1625 svr = sv_2num(TOPs);
1626 useleft = USE_LEFT(svl);
1627 #ifdef PERL_PRESERVE_IVUV
1628 /* See comments in pp_add (in pp_hot.c) about Overflow, and how
1629 "bad things" happen if you rely on signed integers wrapping. */
1632 /* Unless the left argument is integer in range we are going to have to
1633 use NV maths. Hence only attempt to coerce the right argument if
1634 we know the left is integer. */
1635 register UV auv = 0;
1641 a_valid = auvok = 1;
1642 /* left operand is undef, treat as zero. */
1644 /* Left operand is defined, so is it IV? */
1647 if ((auvok = SvUOK(svl)))
1650 register const IV aiv = SvIVX(svl);
1653 auvok = 1; /* Now acting as a sign flag. */
1654 } else { /* 2s complement assumption for IV_MIN */
1662 bool result_good = 0;
1665 bool buvok = SvUOK(svr);
1670 register const IV biv = SvIVX(svr);
1677 /* ?uvok if value is >= 0. basically, flagged as UV if it's +ve,
1678 else "IV" now, independent of how it came in.
1679 if a, b represents positive, A, B negative, a maps to -A etc
1684 all UV maths. negate result if A negative.
1685 subtract if signs same, add if signs differ. */
1687 if (auvok ^ buvok) {
1696 /* Must get smaller */
1701 if (result <= buv) {
1702 /* result really should be -(auv-buv). as its negation
1703 of true value, need to swap our result flag */
1715 if (result <= (UV)IV_MIN)
1716 SETi( -(IV)result );
1718 /* result valid, but out of range for IV. */
1719 SETn( -(NV)result );
1723 } /* Overflow, drop through to NVs. */
1728 NV value = SvNV(svr);
1732 /* left operand is undef, treat as zero - value */
1736 SETn( SvNV(svl) - value );
1743 dVAR; dSP; dATARGET; tryAMAGICbin(lshift,opASSIGN);
1745 const IV shift = POPi;
1746 if (PL_op->op_private & HINT_INTEGER) {
1760 dVAR; dSP; dATARGET; tryAMAGICbin(rshift,opASSIGN);
1762 const IV shift = POPi;
1763 if (PL_op->op_private & HINT_INTEGER) {
1777 dVAR; dSP; tryAMAGICbinSET(lt,0);
1778 #ifdef PERL_PRESERVE_IVUV
1781 SvIV_please(TOPm1s);
1782 if (SvIOK(TOPm1s)) {
1783 bool auvok = SvUOK(TOPm1s);
1784 bool buvok = SvUOK(TOPs);
1786 if (!auvok && !buvok) { /* ## IV < IV ## */
1787 const IV aiv = SvIVX(TOPm1s);
1788 const IV biv = SvIVX(TOPs);
1791 SETs(boolSV(aiv < biv));
1794 if (auvok && buvok) { /* ## UV < UV ## */
1795 const UV auv = SvUVX(TOPm1s);
1796 const UV buv = SvUVX(TOPs);
1799 SETs(boolSV(auv < buv));
1802 if (auvok) { /* ## UV < IV ## */
1804 const IV biv = SvIVX(TOPs);
1807 /* As (a) is a UV, it's >=0, so it cannot be < */
1812 SETs(boolSV(auv < (UV)biv));
1815 { /* ## IV < UV ## */
1816 const IV aiv = SvIVX(TOPm1s);
1820 /* As (b) is a UV, it's >=0, so it must be < */
1827 SETs(boolSV((UV)aiv < buv));
1833 #ifndef NV_PRESERVES_UV
1834 #ifdef PERL_PRESERVE_IVUV
1837 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
1839 SETs(boolSV(SvRV(TOPs) < SvRV(TOPp1s)));
1844 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
1846 if (Perl_isnan(left) || Perl_isnan(right))
1848 SETs(boolSV(left < right));
1851 SETs(boolSV(TOPn < value));
1859 dVAR; dSP; tryAMAGICbinSET(gt,0);
1860 #ifdef PERL_PRESERVE_IVUV
1863 SvIV_please(TOPm1s);
1864 if (SvIOK(TOPm1s)) {
1865 bool auvok = SvUOK(TOPm1s);
1866 bool buvok = SvUOK(TOPs);
1868 if (!auvok && !buvok) { /* ## IV > IV ## */
1869 const IV aiv = SvIVX(TOPm1s);
1870 const IV biv = SvIVX(TOPs);
1873 SETs(boolSV(aiv > biv));
1876 if (auvok && buvok) { /* ## UV > UV ## */
1877 const UV auv = SvUVX(TOPm1s);
1878 const UV buv = SvUVX(TOPs);
1881 SETs(boolSV(auv > buv));
1884 if (auvok) { /* ## UV > IV ## */
1886 const IV biv = SvIVX(TOPs);
1890 /* As (a) is a UV, it's >=0, so it must be > */
1895 SETs(boolSV(auv > (UV)biv));
1898 { /* ## IV > UV ## */
1899 const IV aiv = SvIVX(TOPm1s);
1903 /* As (b) is a UV, it's >=0, so it cannot be > */
1910 SETs(boolSV((UV)aiv > buv));
1916 #ifndef NV_PRESERVES_UV
1917 #ifdef PERL_PRESERVE_IVUV
1920 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
1922 SETs(boolSV(SvRV(TOPs) > SvRV(TOPp1s)));
1927 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
1929 if (Perl_isnan(left) || Perl_isnan(right))
1931 SETs(boolSV(left > right));
1934 SETs(boolSV(TOPn > value));
1942 dVAR; dSP; tryAMAGICbinSET(le,0);
1943 #ifdef PERL_PRESERVE_IVUV
1946 SvIV_please(TOPm1s);
1947 if (SvIOK(TOPm1s)) {
1948 bool auvok = SvUOK(TOPm1s);
1949 bool buvok = SvUOK(TOPs);
1951 if (!auvok && !buvok) { /* ## IV <= IV ## */
1952 const IV aiv = SvIVX(TOPm1s);
1953 const IV biv = SvIVX(TOPs);
1956 SETs(boolSV(aiv <= biv));
1959 if (auvok && buvok) { /* ## UV <= UV ## */
1960 UV auv = SvUVX(TOPm1s);
1961 UV buv = SvUVX(TOPs);
1964 SETs(boolSV(auv <= buv));
1967 if (auvok) { /* ## UV <= IV ## */
1969 const IV biv = SvIVX(TOPs);
1973 /* As (a) is a UV, it's >=0, so a cannot be <= */
1978 SETs(boolSV(auv <= (UV)biv));
1981 { /* ## IV <= UV ## */
1982 const IV aiv = SvIVX(TOPm1s);
1986 /* As (b) is a UV, it's >=0, so a must be <= */
1993 SETs(boolSV((UV)aiv <= buv));
1999 #ifndef NV_PRESERVES_UV
2000 #ifdef PERL_PRESERVE_IVUV
2003 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
2005 SETs(boolSV(SvRV(TOPs) <= SvRV(TOPp1s)));
2010 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
2012 if (Perl_isnan(left) || Perl_isnan(right))
2014 SETs(boolSV(left <= right));
2017 SETs(boolSV(TOPn <= value));
2025 dVAR; dSP; tryAMAGICbinSET(ge,0);
2026 #ifdef PERL_PRESERVE_IVUV
2029 SvIV_please(TOPm1s);
2030 if (SvIOK(TOPm1s)) {
2031 bool auvok = SvUOK(TOPm1s);
2032 bool buvok = SvUOK(TOPs);
2034 if (!auvok && !buvok) { /* ## IV >= IV ## */
2035 const IV aiv = SvIVX(TOPm1s);
2036 const IV biv = SvIVX(TOPs);
2039 SETs(boolSV(aiv >= biv));
2042 if (auvok && buvok) { /* ## UV >= UV ## */
2043 const UV auv = SvUVX(TOPm1s);
2044 const UV buv = SvUVX(TOPs);
2047 SETs(boolSV(auv >= buv));
2050 if (auvok) { /* ## UV >= IV ## */
2052 const IV biv = SvIVX(TOPs);
2056 /* As (a) is a UV, it's >=0, so it must be >= */
2061 SETs(boolSV(auv >= (UV)biv));
2064 { /* ## IV >= UV ## */
2065 const IV aiv = SvIVX(TOPm1s);
2069 /* As (b) is a UV, it's >=0, so a cannot be >= */
2076 SETs(boolSV((UV)aiv >= buv));
2082 #ifndef NV_PRESERVES_UV
2083 #ifdef PERL_PRESERVE_IVUV
2086 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
2088 SETs(boolSV(SvRV(TOPs) >= SvRV(TOPp1s)));
2093 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
2095 if (Perl_isnan(left) || Perl_isnan(right))
2097 SETs(boolSV(left >= right));
2100 SETs(boolSV(TOPn >= value));
2108 dVAR; dSP; tryAMAGICbinSET(ne,0);
2109 #ifndef NV_PRESERVES_UV
2110 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
2112 SETs(boolSV(SvRV(TOPs) != SvRV(TOPp1s)));
2116 #ifdef PERL_PRESERVE_IVUV
2119 SvIV_please(TOPm1s);
2120 if (SvIOK(TOPm1s)) {
2121 const bool auvok = SvUOK(TOPm1s);
2122 const bool buvok = SvUOK(TOPs);
2124 if (auvok == buvok) { /* ## IV == IV or UV == UV ## */
2125 /* Casting IV to UV before comparison isn't going to matter
2126 on 2s complement. On 1s complement or sign&magnitude
2127 (if we have any of them) it could make negative zero
2128 differ from normal zero. As I understand it. (Need to
2129 check - is negative zero implementation defined behaviour
2131 const UV buv = SvUVX(POPs);
2132 const UV auv = SvUVX(TOPs);
2134 SETs(boolSV(auv != buv));
2137 { /* ## Mixed IV,UV ## */
2141 /* != is commutative so swap if needed (save code) */
2143 /* swap. top of stack (b) is the iv */
2147 /* As (a) is a UV, it's >0, so it cannot be == */
2156 /* As (b) is a UV, it's >0, so it cannot be == */
2160 uv = SvUVX(*(SP+1)); /* Do I want TOPp1s() ? */
2162 SETs(boolSV((UV)iv != uv));
2169 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
2171 if (Perl_isnan(left) || Perl_isnan(right))
2173 SETs(boolSV(left != right));
2176 SETs(boolSV(TOPn != value));
2184 dVAR; dSP; dTARGET; tryAMAGICbin(ncmp,0);
2185 #ifndef NV_PRESERVES_UV
2186 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
2187 const UV right = PTR2UV(SvRV(POPs));
2188 const UV left = PTR2UV(SvRV(TOPs));
2189 SETi((left > right) - (left < right));
2193 #ifdef PERL_PRESERVE_IVUV
2194 /* Fortunately it seems NaN isn't IOK */
2197 SvIV_please(TOPm1s);
2198 if (SvIOK(TOPm1s)) {
2199 const bool leftuvok = SvUOK(TOPm1s);
2200 const bool rightuvok = SvUOK(TOPs);
2202 if (!leftuvok && !rightuvok) { /* ## IV <=> IV ## */
2203 const IV leftiv = SvIVX(TOPm1s);
2204 const IV rightiv = SvIVX(TOPs);
2206 if (leftiv > rightiv)
2208 else if (leftiv < rightiv)
2212 } else if (leftuvok && rightuvok) { /* ## UV <=> UV ## */
2213 const UV leftuv = SvUVX(TOPm1s);
2214 const UV rightuv = SvUVX(TOPs);
2216 if (leftuv > rightuv)
2218 else if (leftuv < rightuv)
2222 } else if (leftuvok) { /* ## UV <=> IV ## */
2223 const IV rightiv = SvIVX(TOPs);
2225 /* As (a) is a UV, it's >=0, so it cannot be < */
2228 const UV leftuv = SvUVX(TOPm1s);
2229 if (leftuv > (UV)rightiv) {
2231 } else if (leftuv < (UV)rightiv) {
2237 } else { /* ## IV <=> UV ## */
2238 const IV leftiv = SvIVX(TOPm1s);
2240 /* As (b) is a UV, it's >=0, so it must be < */
2243 const UV rightuv = SvUVX(TOPs);
2244 if ((UV)leftiv > rightuv) {
2246 } else if ((UV)leftiv < rightuv) {
2264 if (Perl_isnan(left) || Perl_isnan(right)) {
2268 value = (left > right) - (left < right);
2272 else if (left < right)
2274 else if (left > right)
2290 int amg_type = sle_amg;
2294 switch (PL_op->op_type) {
2313 tryAMAGICbinSET_var(amg_type,0);
2316 const int cmp = (IN_LOCALE_RUNTIME
2317 ? sv_cmp_locale(left, right)
2318 : sv_cmp(left, right));
2319 SETs(boolSV(cmp * multiplier < rhs));
2326 dVAR; dSP; tryAMAGICbinSET(seq,0);
2329 SETs(boolSV(sv_eq(left, right)));
2336 dVAR; dSP; tryAMAGICbinSET(sne,0);
2339 SETs(boolSV(!sv_eq(left, right)));
2346 dVAR; dSP; dTARGET; tryAMAGICbin(scmp,0);
2349 const int cmp = (IN_LOCALE_RUNTIME
2350 ? sv_cmp_locale(left, right)
2351 : sv_cmp(left, right));
2359 dVAR; dSP; dATARGET; tryAMAGICbin(band,opASSIGN);
2364 if (SvNIOKp(left) || SvNIOKp(right)) {
2365 if (PL_op->op_private & HINT_INTEGER) {
2366 const IV i = SvIV_nomg(left) & SvIV_nomg(right);
2370 const UV u = SvUV_nomg(left) & SvUV_nomg(right);
2375 do_vop(PL_op->op_type, TARG, left, right);
2384 dVAR; dSP; dATARGET;
2385 const int op_type = PL_op->op_type;
2387 tryAMAGICbin_var((op_type == OP_BIT_OR ? bor_amg : bxor_amg), opASSIGN);
2392 if (SvNIOKp(left) || SvNIOKp(right)) {
2393 if (PL_op->op_private & HINT_INTEGER) {
2394 const IV l = (USE_LEFT(left) ? SvIV_nomg(left) : 0);
2395 const IV r = SvIV_nomg(right);
2396 const IV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2400 const UV l = (USE_LEFT(left) ? SvUV_nomg(left) : 0);
2401 const UV r = SvUV_nomg(right);
2402 const UV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2407 do_vop(op_type, TARG, left, right);
2416 dVAR; dSP; dTARGET; tryAMAGICun(neg);
2418 SV * const sv = sv_2num(TOPs);
2419 const int flags = SvFLAGS(sv);
2421 if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
2422 /* It's publicly an integer, or privately an integer-not-float */
2425 if (SvIVX(sv) == IV_MIN) {
2426 /* 2s complement assumption. */
2427 SETi(SvIVX(sv)); /* special case: -((UV)IV_MAX+1) == IV_MIN */
2430 else if (SvUVX(sv) <= IV_MAX) {
2435 else if (SvIVX(sv) != IV_MIN) {
2439 #ifdef PERL_PRESERVE_IVUV
2448 else if (SvPOKp(sv)) {
2450 const char * const s = SvPV_const(sv, len);
2451 if (isIDFIRST(*s)) {
2452 sv_setpvs(TARG, "-");
2455 else if (*s == '+' || *s == '-') {
2457 *SvPV_force(TARG, len) = *s == '-' ? '+' : '-';
2459 else if (DO_UTF8(sv)) {
2462 goto oops_its_an_int;
2464 sv_setnv(TARG, -SvNV(sv));
2466 sv_setpvs(TARG, "-");
2473 goto oops_its_an_int;
2474 sv_setnv(TARG, -SvNV(sv));
2486 dVAR; dSP; tryAMAGICunSET(not);
2487 *PL_stack_sp = boolSV(!SvTRUE(*PL_stack_sp));
2493 dVAR; dSP; dTARGET; tryAMAGICun(compl);
2498 if (PL_op->op_private & HINT_INTEGER) {
2499 const IV i = ~SvIV_nomg(sv);
2503 const UV u = ~SvUV_nomg(sv);
2512 (void)SvPV_nomg_const(sv,len); /* force check for uninit var */
2513 sv_setsv_nomg(TARG, sv);
2514 tmps = (U8*)SvPV_force(TARG, len);
2517 /* Calculate exact length, let's not estimate. */
2522 U8 * const send = tmps + len;
2523 U8 * const origtmps = tmps;
2524 const UV utf8flags = UTF8_ALLOW_ANYUV;
2526 while (tmps < send) {
2527 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2529 targlen += UNISKIP(~c);
2535 /* Now rewind strings and write them. */
2542 Newx(result, targlen + 1, U8);
2544 while (tmps < send) {
2545 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2547 p = uvchr_to_utf8_flags(p, ~c, UNICODE_ALLOW_ANY);
2550 sv_usepvn_flags(TARG, (char*)result, targlen,
2551 SV_HAS_TRAILING_NUL);
2558 Newx(result, nchar + 1, U8);
2560 while (tmps < send) {
2561 const U8 c = (U8)utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2566 sv_usepvn_flags(TARG, (char*)result, nchar, SV_HAS_TRAILING_NUL);
2574 register long *tmpl;
2575 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
2578 for ( ; anum >= (I32)sizeof(long); anum -= (I32)sizeof(long), tmpl++)
2583 for ( ; anum > 0; anum--, tmps++)
2591 /* integer versions of some of the above */
2595 dVAR; dSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
2598 SETi( left * right );
2606 dVAR; dSP; dATARGET; tryAMAGICbin(div,opASSIGN);
2610 DIE(aTHX_ "Illegal division by zero");
2613 /* avoid FPE_INTOVF on some platforms when num is IV_MIN */
2617 value = num / value;
2623 #if defined(__GLIBC__) && IVSIZE == 8
2630 /* This is the vanilla old i_modulo. */
2631 dVAR; dSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
2635 DIE(aTHX_ "Illegal modulus zero");
2636 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2640 SETi( left % right );
2645 #if defined(__GLIBC__) && IVSIZE == 8
2650 /* This is the i_modulo with the workaround for the _moddi3 bug
2651 * in (at least) glibc 2.2.5 (the PERL_ABS() the workaround).
2652 * See below for pp_i_modulo. */
2653 dVAR; dSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
2657 DIE(aTHX_ "Illegal modulus zero");
2658 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2662 SETi( left % PERL_ABS(right) );
2669 dVAR; dSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
2673 DIE(aTHX_ "Illegal modulus zero");
2674 /* The assumption is to use hereafter the old vanilla version... */
2676 PL_ppaddr[OP_I_MODULO] =
2678 /* .. but if we have glibc, we might have a buggy _moddi3
2679 * (at least glicb 2.2.5 is known to have this bug), in other
2680 * words our integer modulus with negative quad as the second
2681 * argument might be broken. Test for this and re-patch the
2682 * opcode dispatch table if that is the case, remembering to
2683 * also apply the workaround so that this first round works
2684 * right, too. See [perl #9402] for more information. */
2688 /* Cannot do this check with inlined IV constants since
2689 * that seems to work correctly even with the buggy glibc. */
2691 /* Yikes, we have the bug.
2692 * Patch in the workaround version. */
2694 PL_ppaddr[OP_I_MODULO] =
2695 &Perl_pp_i_modulo_1;
2696 /* Make certain we work right this time, too. */
2697 right = PERL_ABS(right);
2700 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2704 SETi( left % right );
2712 dVAR; dSP; dATARGET; tryAMAGICbin(add,opASSIGN);
2715 SETi( left + right );
2722 dVAR; dSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
2725 SETi( left - right );
2732 dVAR; dSP; tryAMAGICbinSET(lt,0);
2735 SETs(boolSV(left < right));
2742 dVAR; dSP; tryAMAGICbinSET(gt,0);
2745 SETs(boolSV(left > right));
2752 dVAR; dSP; tryAMAGICbinSET(le,0);
2755 SETs(boolSV(left <= right));
2762 dVAR; dSP; tryAMAGICbinSET(ge,0);
2765 SETs(boolSV(left >= right));
2772 dVAR; dSP; tryAMAGICbinSET(eq,0);
2775 SETs(boolSV(left == right));
2782 dVAR; dSP; tryAMAGICbinSET(ne,0);
2785 SETs(boolSV(left != right));
2792 dVAR; dSP; dTARGET; tryAMAGICbin(ncmp,0);
2799 else if (left < right)
2810 dVAR; dSP; dTARGET; tryAMAGICun(neg);
2815 /* High falutin' math. */
2819 dVAR; dSP; dTARGET; tryAMAGICbin(atan2,0);
2822 SETn(Perl_atan2(left, right));
2830 int amg_type = sin_amg;
2831 const char *neg_report = NULL;
2832 NV (*func)(NV) = Perl_sin;
2833 const int op_type = PL_op->op_type;
2850 amg_type = sqrt_amg;
2852 neg_report = "sqrt";
2856 tryAMAGICun_var(amg_type);
2858 const NV value = POPn;
2860 if (op_type == OP_LOG ? (value <= 0.0) : (value < 0.0)) {
2861 SET_NUMERIC_STANDARD();
2862 DIE(aTHX_ "Can't take %s of %"NVgf, neg_report, value);
2865 XPUSHn(func(value));
2870 /* Support Configure command-line overrides for rand() functions.
2871 After 5.005, perhaps we should replace this by Configure support
2872 for drand48(), random(), or rand(). For 5.005, though, maintain
2873 compatibility by calling rand() but allow the user to override it.
2874 See INSTALL for details. --Andy Dougherty 15 July 1998
2876 /* Now it's after 5.005, and Configure supports drand48() and random(),
2877 in addition to rand(). So the overrides should not be needed any more.
2878 --Jarkko Hietaniemi 27 September 1998
2881 #ifndef HAS_DRAND48_PROTO
2882 extern double drand48 (void);
2895 if (!PL_srand_called) {
2896 (void)seedDrand01((Rand_seed_t)seed());
2897 PL_srand_called = TRUE;
2907 const UV anum = (MAXARG < 1) ? seed() : POPu;
2908 (void)seedDrand01((Rand_seed_t)anum);
2909 PL_srand_called = TRUE;
2916 dVAR; dSP; dTARGET; tryAMAGICun(int);
2918 SV * const sv = sv_2num(TOPs);
2919 const IV iv = SvIV(sv);
2920 /* XXX it's arguable that compiler casting to IV might be subtly
2921 different from modf (for numbers inside (IV_MIN,UV_MAX)) in which
2922 else preferring IV has introduced a subtle behaviour change bug. OTOH
2923 relying on floating point to be accurate is a bug. */
2928 else if (SvIOK(sv)) {
2935 const NV value = SvNV(sv);
2937 if (value < (NV)UV_MAX + 0.5) {
2940 SETn(Perl_floor(value));
2944 if (value > (NV)IV_MIN - 0.5) {
2947 SETn(Perl_ceil(value));
2957 dVAR; dSP; dTARGET; tryAMAGICun(abs);
2959 SV * const sv = sv_2num(TOPs);
2960 /* This will cache the NV value if string isn't actually integer */
2961 const IV iv = SvIV(sv);
2966 else if (SvIOK(sv)) {
2967 /* IVX is precise */
2969 SETu(SvUV(sv)); /* force it to be numeric only */
2977 /* 2s complement assumption. Also, not really needed as
2978 IV_MIN and -IV_MIN should both be %100...00 and NV-able */
2984 const NV value = SvNV(sv);
2998 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES;
3002 SV* const sv = POPs;
3004 tmps = (SvPV_const(sv, len));
3006 /* If Unicode, try to downgrade
3007 * If not possible, croak. */
3008 SV* const tsv = sv_2mortal(newSVsv(sv));
3011 sv_utf8_downgrade(tsv, FALSE);
3012 tmps = SvPV_const(tsv, len);
3014 if (PL_op->op_type == OP_HEX)
3017 while (*tmps && len && isSPACE(*tmps))
3023 result_uv = grok_hex (tmps, &len, &flags, &result_nv);
3025 else if (*tmps == 'b')
3026 result_uv = grok_bin (tmps, &len, &flags, &result_nv);
3028 result_uv = grok_oct (tmps, &len, &flags, &result_nv);
3030 if (flags & PERL_SCAN_GREATER_THAN_UV_MAX) {
3044 SV * const sv = TOPs;
3046 if (SvGAMAGIC(sv)) {
3047 /* For an overloaded or magic scalar, we can't know in advance if
3048 it's going to be UTF-8 or not. Also, we can't call sv_len_utf8 as
3049 it likes to cache the length. Maybe that should be a documented
3054 = sv_2pv_flags(sv, &len,
3055 SV_UNDEF_RETURNS_NULL|SV_CONST_RETURN|SV_GMAGIC);
3059 else if (DO_UTF8(sv)) {
3060 SETi(utf8_length((U8*)p, (U8*)p + len));
3064 } else if (SvOK(sv)) {
3065 /* Neither magic nor overloaded. */
3067 SETi(sv_len_utf8(sv));
3086 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
3088 const I32 arybase = CopARYBASE_get(PL_curcop);
3090 const char *repl = NULL;
3092 const int num_args = PL_op->op_private & 7;
3093 bool repl_need_utf8_upgrade = FALSE;
3094 bool repl_is_utf8 = FALSE;
3096 SvTAINTED_off(TARG); /* decontaminate */
3097 SvUTF8_off(TARG); /* decontaminate */
3101 repl = SvPV_const(repl_sv, repl_len);
3102 repl_is_utf8 = DO_UTF8(repl_sv) && SvCUR(repl_sv);
3112 sv_utf8_upgrade(sv);
3114 else if (DO_UTF8(sv))
3115 repl_need_utf8_upgrade = TRUE;
3117 tmps = SvPV_const(sv, curlen);
3119 utf8_curlen = sv_len_utf8(sv);
3120 if (utf8_curlen == curlen)
3123 curlen = utf8_curlen;
3128 if (pos >= arybase) {
3146 else if (len >= 0) {
3148 if (rem > (I32)curlen)
3163 Perl_croak(aTHX_ "substr outside of string");
3164 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR), "substr outside of string");
3168 const I32 upos = pos;
3169 const I32 urem = rem;
3171 sv_pos_u2b(sv, &pos, &rem);
3173 /* we either return a PV or an LV. If the TARG hasn't been used
3174 * before, or is of that type, reuse it; otherwise use a mortal
3175 * instead. Note that LVs can have an extended lifetime, so also
3176 * dont reuse if refcount > 1 (bug #20933) */
3177 if (SvTYPE(TARG) > SVt_NULL) {
3178 if ( (SvTYPE(TARG) == SVt_PVLV)
3179 ? (!lvalue || SvREFCNT(TARG) > 1)
3182 TARG = sv_newmortal();
3186 sv_setpvn(TARG, tmps, rem);
3187 #ifdef USE_LOCALE_COLLATE
3188 sv_unmagic(TARG, PERL_MAGIC_collxfrm);
3193 SV* repl_sv_copy = NULL;
3195 if (repl_need_utf8_upgrade) {
3196 repl_sv_copy = newSVsv(repl_sv);
3197 sv_utf8_upgrade(repl_sv_copy);
3198 repl = SvPV_const(repl_sv_copy, repl_len);
3199 repl_is_utf8 = DO_UTF8(repl_sv_copy) && SvCUR(sv);
3203 sv_insert_flags(sv, pos, rem, repl, repl_len, 0);
3206 SvREFCNT_dec(repl_sv_copy);
3208 else if (lvalue) { /* it's an lvalue! */
3209 if (!SvGMAGICAL(sv)) {
3211 SvPV_force_nolen(sv);
3212 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR),
3213 "Attempt to use reference as lvalue in substr");
3215 if (isGV_with_GP(sv))
3216 SvPV_force_nolen(sv);
3217 else if (SvOK(sv)) /* is it defined ? */
3218 (void)SvPOK_only_UTF8(sv);
3220 sv_setpvs(sv, ""); /* avoid lexical reincarnation */
3223 if (SvTYPE(TARG) < SVt_PVLV) {
3224 sv_upgrade(TARG, SVt_PVLV);
3225 sv_magic(TARG, NULL, PERL_MAGIC_substr, NULL, 0);
3229 if (LvTARG(TARG) != sv) {
3230 SvREFCNT_dec(LvTARG(TARG));
3231 LvTARG(TARG) = SvREFCNT_inc_simple(sv);
3233 LvTARGOFF(TARG) = upos;
3234 LvTARGLEN(TARG) = urem;
3238 PUSHs(TARG); /* avoid SvSETMAGIC here */
3245 register const IV size = POPi;
3246 register const IV offset = POPi;
3247 register SV * const src = POPs;
3248 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
3250 SvTAINTED_off(TARG); /* decontaminate */
3251 if (lvalue) { /* it's an lvalue! */
3252 if (SvREFCNT(TARG) > 1) /* don't share the TARG (#20933) */
3253 TARG = sv_newmortal();
3254 if (SvTYPE(TARG) < SVt_PVLV) {
3255 sv_upgrade(TARG, SVt_PVLV);
3256 sv_magic(TARG, NULL, PERL_MAGIC_vec, NULL, 0);
3259 if (LvTARG(TARG) != src) {
3260 SvREFCNT_dec(LvTARG(TARG));
3261 LvTARG(TARG) = SvREFCNT_inc_simple(src);
3263 LvTARGOFF(TARG) = offset;
3264 LvTARGLEN(TARG) = size;
3267 sv_setuv(TARG, do_vecget(src, offset, size));
3283 const char *little_p;
3284 const I32 arybase = CopARYBASE_get(PL_curcop);
3287 const bool is_index = PL_op->op_type == OP_INDEX;
3290 /* arybase is in characters, like offset, so combine prior to the
3291 UTF-8 to bytes calculation. */
3292 offset = POPi - arybase;
3296 big_p = SvPV_const(big, biglen);
3297 little_p = SvPV_const(little, llen);
3299 big_utf8 = DO_UTF8(big);
3300 little_utf8 = DO_UTF8(little);
3301 if (big_utf8 ^ little_utf8) {
3302 /* One needs to be upgraded. */
3303 if (little_utf8 && !PL_encoding) {
3304 /* Well, maybe instead we might be able to downgrade the small
3306 char * const pv = (char*)bytes_from_utf8((U8 *)little_p, &llen,
3309 /* If the large string is ISO-8859-1, and it's not possible to
3310 convert the small string to ISO-8859-1, then there is no
3311 way that it could be found anywhere by index. */
3316 /* At this point, pv is a malloc()ed string. So donate it to temp
3317 to ensure it will get free()d */
3318 little = temp = newSV(0);
3319 sv_usepvn(temp, pv, llen);
3320 little_p = SvPVX(little);
3323 ? newSVpvn(big_p, biglen) : newSVpvn(little_p, llen);
3326 sv_recode_to_utf8(temp, PL_encoding);
3328 sv_utf8_upgrade(temp);
3333 big_p = SvPV_const(big, biglen);
3336 little_p = SvPV_const(little, llen);
3340 if (SvGAMAGIC(big)) {
3341 /* Life just becomes a lot easier if I use a temporary here.
3342 Otherwise I need to avoid calls to sv_pos_u2b(), which (dangerously)
3343 will trigger magic and overloading again, as will fbm_instr()
3345 big = newSVpvn_flags(big_p, biglen,
3346 SVs_TEMP | (big_utf8 ? SVf_UTF8 : 0));
3349 if (SvGAMAGIC(little) || (is_index && !SvOK(little))) {
3350 /* index && SvOK() is a hack. fbm_instr() calls SvPV_const, which will
3351 warn on undef, and we've already triggered a warning with the
3352 SvPV_const some lines above. We can't remove that, as we need to
3353 call some SvPV to trigger overloading early and find out if the
3355 This is all getting to messy. The API isn't quite clean enough,
3356 because data access has side effects.
3358 little = newSVpvn_flags(little_p, llen,
3359 SVs_TEMP | (little_utf8 ? SVf_UTF8 : 0));
3360 little_p = SvPVX(little);
3364 offset = is_index ? 0 : biglen;
3366 if (big_utf8 && offset > 0)
3367 sv_pos_u2b(big, &offset, 0);
3373 else if (offset > (I32)biglen)
3375 if (!(little_p = is_index
3376 ? fbm_instr((unsigned char*)big_p + offset,
3377 (unsigned char*)big_p + biglen, little, 0)
3378 : rninstr(big_p, big_p + offset,
3379 little_p, little_p + llen)))
3382 retval = little_p - big_p;
3383 if (retval > 0 && big_utf8)
3384 sv_pos_b2u(big, &retval);
3388 PUSHi(retval + arybase);
3394 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
3395 if (SvTAINTED(MARK[1]))
3396 TAINT_PROPER("sprintf");
3397 do_sprintf(TARG, SP-MARK, MARK+1);
3398 TAINT_IF(SvTAINTED(TARG));
3410 const U8 *s = (U8*)SvPV_const(argsv, len);
3412 if (PL_encoding && SvPOK(argsv) && !DO_UTF8(argsv)) {
3413 SV * const tmpsv = sv_2mortal(newSVsv(argsv));
3414 s = (U8*)sv_recode_to_utf8(tmpsv, PL_encoding);
3418 XPUSHu(DO_UTF8(argsv) ?
3419 utf8n_to_uvchr(s, UTF8_MAXBYTES, 0, UTF8_ALLOW_ANYUV) :
3431 if (((SvIOK_notUV(TOPs) && SvIV(TOPs) < 0)
3433 (SvNOK(TOPs) && SvNV(TOPs) < 0.0))) {
3435 value = POPu; /* chr(-1) eq chr(0xff), etc. */
3437 (void) POPs; /* Ignore the argument value. */
3438 value = UNICODE_REPLACEMENT;
3444 SvUPGRADE(TARG,SVt_PV);
3446 if (value > 255 && !IN_BYTES) {
3447 SvGROW(TARG, (STRLEN)UNISKIP(value)+1);
3448 tmps = (char*)uvchr_to_utf8_flags((U8*)SvPVX(TARG), value, 0);
3449 SvCUR_set(TARG, tmps - SvPVX_const(TARG));
3451 (void)SvPOK_only(TARG);
3460 *tmps++ = (char)value;
3462 (void)SvPOK_only(TARG);
3464 if (PL_encoding && !IN_BYTES) {
3465 sv_recode_to_utf8(TARG, PL_encoding);
3467 if (SvCUR(TARG) == 0 || !is_utf8_string((U8*)tmps, SvCUR(TARG)) ||
3468 UNICODE_IS_REPLACEMENT(utf8_to_uvchr((U8*)tmps, NULL))) {
3472 *tmps++ = (char)value;
3488 const char *tmps = SvPV_const(left, len);
3490 if (DO_UTF8(left)) {
3491 /* If Unicode, try to downgrade.
3492 * If not possible, croak.
3493 * Yes, we made this up. */
3494 SV* const tsv = sv_2mortal(newSVsv(left));
3497 sv_utf8_downgrade(tsv, FALSE);
3498 tmps = SvPV_const(tsv, len);
3500 # ifdef USE_ITHREADS
3502 if (!PL_reentrant_buffer->_crypt_struct_buffer) {
3503 /* This should be threadsafe because in ithreads there is only
3504 * one thread per interpreter. If this would not be true,
3505 * we would need a mutex to protect this malloc. */
3506 PL_reentrant_buffer->_crypt_struct_buffer =
3507 (struct crypt_data *)safemalloc(sizeof(struct crypt_data));
3508 #if defined(__GLIBC__) || defined(__EMX__)
3509 if (PL_reentrant_buffer->_crypt_struct_buffer) {
3510 PL_reentrant_buffer->_crypt_struct_buffer->initialized = 0;
3511 /* work around glibc-2.2.5 bug */
3512 PL_reentrant_buffer->_crypt_struct_buffer->current_saltbits = 0;
3516 # endif /* HAS_CRYPT_R */
3517 # endif /* USE_ITHREADS */
3519 sv_setpv(TARG, fcrypt(tmps, SvPV_nolen_const(right)));
3521 sv_setpv(TARG, PerlProc_crypt(tmps, SvPV_nolen_const(right)));
3527 "The crypt() function is unimplemented due to excessive paranoia.");
3531 /* Generally UTF-8 and UTF-EBCDIC are indistinguishable at this level. So
3532 * most comments below say UTF-8, when in fact they mean UTF-EBCDIC as well */
3534 /* Both the characters below can be stored in two UTF-8 bytes. In UTF-8 the max
3535 * character that 2 bytes can hold is U+07FF, and in UTF-EBCDIC it is U+03FF.
3536 * See http://www.unicode.org/unicode/reports/tr16 */
3537 #define LATIN_CAPITAL_LETTER_Y_WITH_DIAERESIS 0x0178 /* Also is title case */
3538 #define GREEK_CAPITAL_LETTER_MU 0x039C /* Upper and title case of MICRON */
3540 /* Below are several macros that generate code */
3541 /* Generates code to store a unicode codepoint c that is known to occupy
3542 * exactly two UTF-8 and UTF-EBCDIC bytes; it is stored into p and p+1. */
3543 #define STORE_UNI_TO_UTF8_TWO_BYTE(p, c) \
3545 *(p) = UTF8_TWO_BYTE_HI(c); \
3546 *((p)+1) = UTF8_TWO_BYTE_LO(c); \
3549 /* Like STORE_UNI_TO_UTF8_TWO_BYTE, but advances p to point to the next
3550 * available byte after the two bytes */
3551 #define CAT_UNI_TO_UTF8_TWO_BYTE(p, c) \
3553 *(p)++ = UTF8_TWO_BYTE_HI(c); \
3554 *((p)++) = UTF8_TWO_BYTE_LO(c); \
3557 /* Generates code to store the upper case of latin1 character l which is known
3558 * to have its upper case be non-latin1 into the two bytes p and p+1. There
3559 * are only two characters that fit this description, and this macro knows
3560 * about them, and that the upper case values fit into two UTF-8 or UTF-EBCDIC
3562 #define STORE_NON_LATIN1_UC(p, l) \
3564 if ((l) == LATIN_SMALL_LETTER_Y_WITH_DIAERESIS) { \
3565 STORE_UNI_TO_UTF8_TWO_BYTE((p), LATIN_CAPITAL_LETTER_Y_WITH_DIAERESIS); \
3566 } else { /* Must be the following letter */ \
3567 STORE_UNI_TO_UTF8_TWO_BYTE((p), GREEK_CAPITAL_LETTER_MU); \
3571 /* Like STORE_NON_LATIN1_UC, but advances p to point to the next available byte
3572 * after the character stored */
3573 #define CAT_NON_LATIN1_UC(p, l) \
3575 if ((l) == LATIN_SMALL_LETTER_Y_WITH_DIAERESIS) { \
3576 CAT_UNI_TO_UTF8_TWO_BYTE((p), LATIN_CAPITAL_LETTER_Y_WITH_DIAERESIS); \
3578 CAT_UNI_TO_UTF8_TWO_BYTE((p), GREEK_CAPITAL_LETTER_MU); \
3582 /* Generates code to add the two UTF-8 bytes (probably u) that are the upper
3583 * case of l into p and p+1. u must be the result of toUPPER_LATIN1_MOD(l),
3584 * and must require two bytes to store it. Advances p to point to the next
3585 * available position */
3586 #define CAT_TWO_BYTE_UNI_UPPER_MOD(p, l, u) \
3588 if ((u) != LATIN_SMALL_LETTER_Y_WITH_DIAERESIS) { \
3589 CAT_UNI_TO_UTF8_TWO_BYTE((p), (u)); /* not special, just save it */ \
3590 } else if (l == LATIN_SMALL_LETTER_SHARP_S) { \
3591 *(p)++ = 'S'; *(p)++ = 'S'; /* upper case is 'SS' */ \
3592 } else {/* else is one of the other two special cases */ \
3593 CAT_NON_LATIN1_UC((p), (l)); \
3599 /* Actually is both lcfirst() and ucfirst(). Only the first character
3600 * changes. This means that possibly we can change in-place, ie., just
3601 * take the source and change that one character and store it back, but not
3602 * if read-only etc, or if the length changes */
3607 STRLEN slen; /* slen is the byte length of the whole SV. */
3610 bool inplace; /* ? Convert first char only, in-place */
3611 bool doing_utf8 = FALSE; /* ? using utf8 */
3612 bool convert_source_to_utf8 = FALSE; /* ? need to convert */
3613 const int op_type = PL_op->op_type;
3616 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
3617 STRLEN ulen; /* ulen is the byte length of the original Unicode character
3618 * stored as UTF-8 at s. */
3619 STRLEN tculen; /* tculen is the byte length of the freshly titlecased (or
3620 * lowercased) character stored in tmpbuf. May be either
3621 * UTF-8 or not, but in either case is the number of bytes */
3625 s = (const U8*)SvPV_nomg_const(source, slen);
3627 if (ckWARN(WARN_UNINITIALIZED))
3628 report_uninit(source);
3633 /* We may be able to get away with changing only the first character, in
3634 * place, but not if read-only, etc. Later we may discover more reasons to
3635 * not convert in-place. */
3636 inplace = SvPADTMP(source) && !SvREADONLY(source) && SvTEMP(source);
3638 /* First calculate what the changed first character should be. This affects
3639 * whether we can just swap it out, leaving the rest of the string unchanged,
3640 * or even if have to convert the dest to UTF-8 when the source isn't */
3642 if (! slen) { /* If empty */
3643 need = 1; /* still need a trailing NUL */
3645 else if (DO_UTF8(source)) { /* Is the source utf8? */
3648 /* TODO: This is #ifdefd out because it has hard-coded the standard mappings,
3649 * and doesn't allow for the user to specify their own. When code is added to
3650 * detect if there is a user-defined mapping in force here, and if so to use
3651 * that, then the code below can be compiled. The detection would be a good
3652 * thing anyway, as currently the user-defined mappings only work on utf8
3653 * strings, and thus depend on the chosen internal storage method, which is a
3655 #ifdef GO_AHEAD_AND_BREAK_USER_DEFINED_CASE_MAPPINGS
3656 if (UTF8_IS_INVARIANT(*s)) {
3658 /* An invariant source character is either ASCII or, in EBCDIC, an
3659 * ASCII equivalent or a caseless C1 control. In both these cases,
3660 * the lower and upper cases of any character are also invariants
3661 * (and title case is the same as upper case). So it is safe to
3662 * use the simple case change macros which avoid the overhead of
3663 * the general functions. Note that if perl were to be extended to
3664 * do locale handling in UTF-8 strings, this wouldn't be true in,
3665 * for example, Lithuanian or Turkic. */
3666 *tmpbuf = (op_type == OP_LCFIRST) ? toLOWER(*s) : toUPPER(*s);
3670 else if (UTF8_IS_DOWNGRADEABLE_START(*s)) {
3673 /* Similarly, if the source character isn't invariant but is in the
3674 * latin1 range (or EBCDIC equivalent thereof), we have the case
3675 * changes compiled into perl, and can avoid the overhead of the
3676 * general functions. In this range, the characters are stored as
3677 * two UTF-8 bytes, and it so happens that any changed-case version
3678 * is also two bytes (in both ASCIIish and EBCDIC machines). */
3682 /* Convert the two source bytes to a single Unicode code point
3683 * value, change case and save for below */
3684 chr = UTF8_ACCUMULATE(*s, *(s+1));
3685 if (op_type == OP_LCFIRST) { /* lower casing is easy */
3686 U8 lower = toLOWER_LATIN1(chr);
3687 STORE_UNI_TO_UTF8_TWO_BYTE(tmpbuf, lower);
3689 else { /* ucfirst */
3690 U8 upper = toUPPER_LATIN1_MOD(chr);
3692 /* Most of the latin1 range characters are well-behaved. Their
3693 * title and upper cases are the same, and are also in the
3694 * latin1 range. The macro above returns their upper (hence
3695 * title) case, and all that need be done is to save the result
3696 * for below. However, several characters are problematic, and
3697 * have to be handled specially. The MOD in the macro name
3698 * above means that these tricky characters all get mapped to
3699 * the single character LATIN_SMALL_LETTER_Y_WITH_DIAERESIS.
3700 * This mapping saves some tests for the majority of the
3703 if (upper != LATIN_SMALL_LETTER_Y_WITH_DIAERESIS) {
3705 /* Not tricky. Just save it. */
3706 STORE_UNI_TO_UTF8_TWO_BYTE(tmpbuf, upper);
3708 else if (chr == LATIN_SMALL_LETTER_SHARP_S) {
3710 /* This one is tricky because it is two characters long,
3711 * though the UTF-8 is still two bytes, so the stored
3712 * length doesn't change */
3713 *tmpbuf = 'S'; /* The UTF-8 is 'Ss' */
3714 *(tmpbuf + 1) = 's';
3718 /* The other two have their title and upper cases the same,
3719 * but are tricky because the changed-case characters
3720 * aren't in the latin1 range. They, however, do fit into
3721 * two UTF-8 bytes */
3722 STORE_NON_LATIN1_UC(tmpbuf, chr);
3727 #endif /* end of dont want to break user-defined casing */
3729 /* Here, can't short-cut the general case */
3731 utf8_to_uvchr(s, &ulen);
3732 if (op_type == OP_UCFIRST) toTITLE_utf8(s, tmpbuf, &tculen);
3733 else toLOWER_utf8(s, tmpbuf, &tculen);
3735 /* we can't do in-place if the length changes. */
3736 if (ulen != tculen) inplace = FALSE;
3737 need = slen + 1 - ulen + tculen;
3738 #ifdef GO_AHEAD_AND_BREAK_USER_DEFINED_CASE_MAPPINGS
3742 else { /* Non-zero length, non-UTF-8, Need to consider locale and if
3743 * latin1 is treated as caseless. Note that a locale takes
3745 tculen = 1; /* Most characters will require one byte, but this will
3746 * need to be overridden for the tricky ones */
3749 if (op_type == OP_LCFIRST) {
3751 /* lower case the first letter: no trickiness for any character */
3752 *tmpbuf = (IN_LOCALE_RUNTIME) ? toLOWER_LC(*s) :
3753 ((IN_UNI_8_BIT) ? toLOWER_LATIN1(*s) : toLOWER(*s));
3756 else if (IN_LOCALE_RUNTIME) {
3757 *tmpbuf = toUPPER_LC(*s); /* This would be a bug if any locales
3758 * have upper and title case different
3761 else if (! IN_UNI_8_BIT) {
3762 *tmpbuf = toUPPER(*s); /* Returns caseless for non-ascii, or
3763 * on EBCDIC machines whatever the
3764 * native function does */
3766 else { /* is ucfirst non-UTF-8, not in locale, and cased latin1 */
3767 *tmpbuf = toUPPER_LATIN1_MOD(*s);
3769 /* tmpbuf now has the correct title case for all latin1 characters
3770 * except for the several ones that have tricky handling. All
3771 * of these are mapped by the MOD to the letter below. */
3772 if (*tmpbuf == LATIN_SMALL_LETTER_Y_WITH_DIAERESIS) {
3774 /* The length is going to change, with all three of these, so
3775 * can't replace just the first character */
3778 /* We use the original to distinguish between these tricky
3780 if (*s == LATIN_SMALL_LETTER_SHARP_S) {
3781 /* Two character title case 'Ss', but can remain non-UTF-8 */
3784 *(tmpbuf + 1) = 's'; /* Assert: length(tmpbuf) >= 2 */
3789 /* The other two tricky ones have their title case outside
3790 * latin1. It is the same as their upper case. */
3792 STORE_NON_LATIN1_UC(tmpbuf, *s);
3794 /* The UTF-8 and UTF-EBCDIC lengths of both these characters
3795 * and their upper cases is 2. */
3798 /* The entire result will have to be in UTF-8. Assume worst
3799 * case sizing in conversion. (all latin1 characters occupy
3800 * at most two bytes in utf8) */
3801 convert_source_to_utf8 = TRUE;
3802 need = slen * 2 + 1;
3804 } /* End of is one of the three special chars */
3805 } /* End of use Unicode (Latin1) semantics */
3806 } /* End of changing the case of the first character */
3808 /* Here, have the first character's changed case stored in tmpbuf. Ready to
3809 * generate the result */
3812 /* We can convert in place. This means we change just the first
3813 * character without disturbing the rest; no need to grow */
3815 s = d = (U8*)SvPV_force_nomg(source, slen);
3821 /* Here, we can't convert in place; we earlier calculated how much
3822 * space we will need, so grow to accommodate that */
3823 SvUPGRADE(dest, SVt_PV);
3824 d = (U8*)SvGROW(dest, need);
3825 (void)SvPOK_only(dest);
3832 if (! convert_source_to_utf8) {
3834 /* Here both source and dest are in UTF-8, but have to create
3835 * the entire output. We initialize the result to be the
3836 * title/lower cased first character, and then append the rest
3838 sv_setpvn(dest, (char*)tmpbuf, tculen);
3840 sv_catpvn(dest, (char*)(s + ulen), slen - ulen);
3844 const U8 *const send = s + slen;
3846 /* Here the dest needs to be in UTF-8, but the source isn't,
3847 * except we earlier UTF-8'd the first character of the source
3848 * into tmpbuf. First put that into dest, and then append the
3849 * rest of the source, converting it to UTF-8 as we go. */
3851 /* Assert tculen is 2 here because the only two characters that
3852 * get to this part of the code have 2-byte UTF-8 equivalents */
3854 *d++ = *(tmpbuf + 1);
3855 s++; /* We have just processed the 1st char */
3857 for (; s < send; s++) {
3858 d = uvchr_to_utf8(d, *s);
3861 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3865 else { /* in-place UTF-8. Just overwrite the first character */
3866 Copy(tmpbuf, d, tculen, U8);
3867 SvCUR_set(dest, need - 1);
3870 else { /* Neither source nor dest are in or need to be UTF-8 */
3872 if (IN_LOCALE_RUNTIME) {
3876 if (inplace) { /* in-place, only need to change the 1st char */
3879 else { /* Not in-place */
3881 /* Copy the case-changed character(s) from tmpbuf */
3882 Copy(tmpbuf, d, tculen, U8);
3883 d += tculen - 1; /* Code below expects d to point to final
3884 * character stored */
3887 else { /* empty source */
3888 /* See bug #39028: Don't taint if empty */
3892 /* In a "use bytes" we don't treat the source as UTF-8, but, still want
3893 * the destination to retain that flag */
3897 if (!inplace) { /* Finish the rest of the string, unchanged */
3898 /* This will copy the trailing NUL */
3899 Copy(s + 1, d + 1, slen, U8);
3900 SvCUR_set(dest, need - 1);
3907 /* There's so much setup/teardown code common between uc and lc, I wonder if
3908 it would be worth merging the two, and just having a switch outside each
3909 of the three tight loops. There is less and less commonality though */
3923 if (SvPADTMP(source) && !SvREADONLY(source) && !SvAMAGIC(source)
3924 && SvTEMP(source) && !DO_UTF8(source)
3925 && (IN_LOCALE_RUNTIME || ! IN_UNI_8_BIT)) {
3927 /* We can convert in place. The reason we can't if in UNI_8_BIT is to
3928 * make the loop tight, so we overwrite the source with the dest before
3929 * looking at it, and we need to look at the original source
3930 * afterwards. There would also need to be code added to handle
3931 * switching to not in-place in midstream if we run into characters
3932 * that change the length.
3935 s = d = (U8*)SvPV_force_nomg(source, len);
3942 /* The old implementation would copy source into TARG at this point.
3943 This had the side effect that if source was undef, TARG was now
3944 an undefined SV with PADTMP set, and they don't warn inside
3945 sv_2pv_flags(). However, we're now getting the PV direct from
3946 source, which doesn't have PADTMP set, so it would warn. Hence the
3950 s = (const U8*)SvPV_nomg_const(source, len);
3952 if (ckWARN(WARN_UNINITIALIZED))
3953 report_uninit(source);
3959 SvUPGRADE(dest, SVt_PV);
3960 d = (U8*)SvGROW(dest, min);
3961 (void)SvPOK_only(dest);
3966 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
3967 to check DO_UTF8 again here. */
3969 if (DO_UTF8(source)) {
3970 const U8 *const send = s + len;
3971 U8 tmpbuf[UTF8_MAXBYTES+1];
3973 /* This is ifdefd out because it needs more work and thought. It isn't clear
3974 * that we should do it. These are hard-coded rules from the Unicode standard,
3975 * and may change. 5.2 gives new guidance on the iota subscript, for example,
3976 * which has not been checked against this; and secondly it may be that we are
3977 * passed a subset of the context, via a \U...\E, for example, and its not
3978 * clear what the best approach is to that */
3979 #ifdef CONTEXT_DEPENDENT_CASING
3980 bool in_iota_subscript = FALSE;
3984 #ifdef CONTEXT_DEPENDENT_CASING
3985 if (in_iota_subscript && ! is_utf8_mark(s)) {
3986 /* A non-mark. Time to output the iota subscript */
3987 #define GREEK_CAPITAL_LETTER_IOTA 0x0399
3988 #define COMBINING_GREEK_YPOGEGRAMMENI 0x0345
3990 CAT_UNI_TO_UTF8_TWO_BYTE(d, GREEK_CAPITAL_LETTER_IOTA);
3991 in_iota_subscript = FALSE;
3996 /* See comments at the first instance in this file of this ifdef */
3997 #ifdef GO_AHEAD_AND_BREAK_USER_DEFINED_CASE_MAPPINGS
3999 /* If the UTF-8 character is invariant, then it is in the range
4000 * known by the standard macro; result is only one byte long */
4001 if (UTF8_IS_INVARIANT(*s)) {
4005 else if (UTF8_IS_DOWNGRADEABLE_START(*s)) {
4007 /* Likewise, if it fits in a byte, its case change is in our
4009 U8 orig = UTF8_ACCUMULATE(*s, *(s+1));
4010 U8 upper = toUPPER_LATIN1_MOD(orig);
4011 CAT_TWO_BYTE_UNI_UPPER_MOD(d, orig, upper);
4019 /* Otherwise, need the general UTF-8 case. Get the changed
4020 * case value and copy it to the output buffer */
4022 const STRLEN u = UTF8SKIP(s);
4025 #ifndef CONTEXT_DEPENDENT_CASING
4026 toUPPER_utf8(s, tmpbuf, &ulen);
4028 const UV uv = toUPPER_utf8(s, tmpbuf, &ulen);
4029 if (uv == GREEK_CAPITAL_LETTER_IOTA && utf8_to_uvchr(s, 0) == COMBINING_GREEK_YPOGEGRAMMENI) {
4030 in_iota_subscript = TRUE;
4034 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
4035 /* If the eventually required minimum size outgrows
4036 * the available space, we need to grow. */
4037 const UV o = d - (U8*)SvPVX_const(dest);
4039 /* If someone uppercases one million U+03B0s we
4040 * SvGROW() one million times. Or we could try
4041 * guessing how much to allocate without allocating too
4042 * much. Such is life. See corresponding comment in lc code
4043 * for another option */
4045 d = (U8*)SvPVX(dest) + o;
4047 Copy(tmpbuf, d, ulen, U8);
4049 #ifdef CONTEXT_DEPENDENT_CASING
4055 #ifdef CONTEXT_DEPENDENT_CASING
4056 if (in_iota_subscript) CAT_UNI_TO_UTF8_TWO_BYTE(d, GREEK_CAPITAL_LETTER_IOTA);
4060 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4061 } else { /* Not UTF-8 */
4063 const U8 *const send = s + len;
4065 /* Use locale casing if in locale; regular style if not treating
4066 * latin1 as having case; otherwise the latin1 casing. Do the
4067 * whole thing in a tight loop, for speed, */
4068 if (IN_LOCALE_RUNTIME) {
4071 for (; s < send; d++, s++)
4072 *d = toUPPER_LC(*s);
4074 else if (! IN_UNI_8_BIT) {
4075 for (; s < send; d++, s++) {
4080 for (; s < send; d++, s++) {
4081 *d = toUPPER_LATIN1_MOD(*s);
4082 if (*d != LATIN_SMALL_LETTER_Y_WITH_DIAERESIS) continue;
4084 /* The mainstream case is the tight loop above. To avoid
4085 * extra tests in that, all three characters that require
4086 * special handling are mapped by the MOD to the one tested
4088 * Use the source to distinguish between the three cases */
4090 if (*s == LATIN_SMALL_LETTER_SHARP_S) {
4092 /* uc() of this requires 2 characters, but they are
4093 * ASCII. If not enough room, grow the string */
4094 if (SvLEN(dest) < ++min) {
4095 const UV o = d - (U8*)SvPVX_const(dest);
4097 d = (U8*)SvPVX(dest) + o;
4099 *d++ = 'S'; *d = 'S'; /* upper case is 'SS' */
4100 continue; /* Back to the tight loop; still in ASCII */
4103 /* The other two special handling characters have their
4104 * upper cases outside the latin1 range, hence need to be
4105 * in UTF-8, so the whole result needs to be in UTF-8. So,
4106 * here we are somewhere in the middle of processing a
4107 * non-UTF-8 string, and realize that we will have to convert
4108 * the whole thing to UTF-8. What to do? There are
4109 * several possibilities. The simplest to code is to
4110 * convert what we have so far, set a flag, and continue on
4111 * in the loop. The flag would be tested each time through
4112 * the loop, and if set, the next character would be
4113 * converted to UTF-8 and stored. But, I (khw) didn't want
4114 * to slow down the mainstream case at all for this fairly
4115 * rare case, so I didn't want to add a test that didn't
4116 * absolutely have to be there in the loop, besides the
4117 * possibility that it would get too complicated for
4118 * optimizers to deal with. Another possibility is to just
4119 * give up, convert the source to UTF-8, and restart the
4120 * function that way. Another possibility is to convert
4121 * both what has already been processed and what is yet to
4122 * come separately to UTF-8, then jump into the loop that
4123 * handles UTF-8. But the most efficient time-wise of the
4124 * ones I could think of is what follows, and turned out to
4125 * not require much extra code. */
4127 /* Convert what we have so far into UTF-8, telling the
4128 * function that we know it should be converted, and to
4129 * allow extra space for what we haven't processed yet.
4130 * Assume the worst case space requirements for converting
4131 * what we haven't processed so far: that it will require
4132 * two bytes for each remaining source character, plus the
4133 * NUL at the end. This may cause the string pointer to
4134 * move, so re-find it. */
4136 len = d - (U8*)SvPVX_const(dest);
4137 SvCUR_set(dest, len);
4138 len = sv_utf8_upgrade_flags_grow(dest,
4139 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
4141 d = (U8*)SvPVX(dest) + len;
4143 /* And append the current character's upper case in UTF-8 */
4144 CAT_NON_LATIN1_UC(d, *s);
4146 /* Now process the remainder of the source, converting to
4147 * upper and UTF-8. If a resulting byte is invariant in
4148 * UTF-8, output it as-is, otherwise convert to UTF-8 and
4149 * append it to the output. */
4152 for (; s < send; s++) {
4153 U8 upper = toUPPER_LATIN1_MOD(*s);
4154 if UTF8_IS_INVARIANT(upper) {
4158 CAT_TWO_BYTE_UNI_UPPER_MOD(d, *s, upper);
4162 /* Here have processed the whole source; no need to continue
4163 * with the outer loop. Each character has been converted
4164 * to upper case and converted to UTF-8 */
4167 } /* End of processing all latin1-style chars */
4168 } /* End of processing all chars */
4169 } /* End of source is not empty */
4171 if (source != dest) {
4172 *d = '\0'; /* Here d points to 1 after last char, add NUL */
4173 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4175 } /* End of isn't utf8 */
4193 if (SvPADTMP(source) && !SvREADONLY(source) && !SvAMAGIC(source)
4194 && SvTEMP(source) && !DO_UTF8(source)) {
4196 /* We can convert in place, as lowercasing anything in the latin1 range
4197 * (or else DO_UTF8 would have been on) doesn't lengthen it */
4199 s = d = (U8*)SvPV_force_nomg(source, len);
4206 /* The old implementation would copy source into TARG at this point.
4207 This had the side effect that if source was undef, TARG was now
4208 an undefined SV with PADTMP set, and they don't warn inside
4209 sv_2pv_flags(). However, we're now getting the PV direct from
4210 source, which doesn't have PADTMP set, so it would warn. Hence the
4214 s = (const U8*)SvPV_nomg_const(source, len);
4216 if (ckWARN(WARN_UNINITIALIZED))
4217 report_uninit(source);
4223 SvUPGRADE(dest, SVt_PV);
4224 d = (U8*)SvGROW(dest, min);
4225 (void)SvPOK_only(dest);
4230 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
4231 to check DO_UTF8 again here. */
4233 if (DO_UTF8(source)) {
4234 const U8 *const send = s + len;
4235 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
4238 /* See comments at the first instance in this file of this ifdef */
4239 #ifdef GO_AHEAD_AND_BREAK_USER_DEFINED_CASE_MAPPINGS
4240 if (UTF8_IS_INVARIANT(*s)) {
4242 /* Invariant characters use the standard mappings compiled in.
4247 else if (UTF8_IS_DOWNGRADEABLE_START(*s)) {
4249 /* As do the ones in the Latin1 range */
4250 U8 lower = toLOWER_LATIN1(UTF8_ACCUMULATE(*s, *(s+1)));
4251 CAT_UNI_TO_UTF8_TWO_BYTE(d, lower);
4256 /* Here, is utf8 not in Latin-1 range, have to go out and get
4257 * the mappings from the tables. */
4259 const STRLEN u = UTF8SKIP(s);
4262 /* See comments at the first instance in this file of this ifdef */
4263 #ifndef CONTEXT_DEPENDENT_CASING
4264 toLOWER_utf8(s, tmpbuf, &ulen);
4266 /* Here is context dependent casing, not compiled in currently;
4267 * needs more thought and work */
4269 const UV uv = toLOWER_utf8(s, tmpbuf, &ulen);
4271 /* If the lower case is a small sigma, it may be that we need
4272 * to change it to a final sigma. This happens at the end of
4273 * a word that contains more than just this character, and only
4274 * when we started with a capital sigma. */
4275 if (uv == UNICODE_GREEK_SMALL_LETTER_SIGMA &&
4276 s > send - len && /* Makes sure not the first letter */
4277 utf8_to_uvchr(s, 0) == UNICODE_GREEK_CAPITAL_LETTER_SIGMA
4280 /* We use the algorithm in:
4281 * http://www.unicode.org/versions/Unicode5.0.0/ch03.pdf (C
4282 * is a CAPITAL SIGMA): If C is preceded by a sequence
4283 * consisting of a cased letter and a case-ignorable
4284 * sequence, and C is not followed by a sequence consisting
4285 * of a case ignorable sequence and then a cased letter,
4286 * then when lowercasing C, C becomes a final sigma */
4288 /* To determine if this is the end of a word, need to peek
4289 * ahead. Look at the next character */
4290 const U8 *peek = s + u;
4292 /* Skip any case ignorable characters */
4293 while (peek < send && is_utf8_case_ignorable(peek)) {
4294 peek += UTF8SKIP(peek);
4297 /* If we reached the end of the string without finding any
4298 * non-case ignorable characters, or if the next such one
4299 * is not-cased, then we have met the conditions for it
4300 * being a final sigma with regards to peek ahead, and so
4301 * must do peek behind for the remaining conditions. (We
4302 * know there is stuff behind to look at since we tested
4303 * above that this isn't the first letter) */
4304 if (peek >= send || ! is_utf8_cased(peek)) {
4305 peek = utf8_hop(s, -1);
4307 /* Here are at the beginning of the first character
4308 * before the original upper case sigma. Keep backing
4309 * up, skipping any case ignorable characters */
4310 while (is_utf8_case_ignorable(peek)) {
4311 peek = utf8_hop(peek, -1);
4314 /* Here peek points to the first byte of the closest
4315 * non-case-ignorable character before the capital
4316 * sigma. If it is cased, then by the Unicode
4317 * algorithm, we should use a small final sigma instead
4318 * of what we have */
4319 if (is_utf8_cased(peek)) {
4320 STORE_UNI_TO_UTF8_TWO_BYTE(tmpbuf,
4321 UNICODE_GREEK_SMALL_LETTER_FINAL_SIGMA);
4325 else { /* Not a context sensitive mapping */
4326 #endif /* End of commented out context sensitive */
4327 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
4329 /* If the eventually required minimum size outgrows
4330 * the available space, we need to grow. */
4331 const UV o = d - (U8*)SvPVX_const(dest);
4333 /* If someone lowercases one million U+0130s we
4334 * SvGROW() one million times. Or we could try
4335 * guessing how much to allocate without allocating too
4336 * much. Such is life. Another option would be to
4337 * grow an extra byte or two more each time we need to
4338 * grow, which would cut down the million to 500K, with
4341 d = (U8*)SvPVX(dest) + o;
4343 #ifdef CONTEXT_DEPENDENT_CASING
4346 /* Copy the newly lowercased letter to the output buffer we're
4348 Copy(tmpbuf, d, ulen, U8);
4351 #ifdef GO_AHEAD_AND_BREAK_USER_DEFINED_CASE_MAPPINGS
4354 } /* End of looping through the source string */
4357 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4358 } else { /* Not utf8 */
4360 const U8 *const send = s + len;
4362 /* Use locale casing if in locale; regular style if not treating
4363 * latin1 as having case; otherwise the latin1 casing. Do the
4364 * whole thing in a tight loop, for speed, */
4365 if (IN_LOCALE_RUNTIME) {
4368 for (; s < send; d++, s++)
4369 *d = toLOWER_LC(*s);
4371 else if (! IN_UNI_8_BIT) {
4372 for (; s < send; d++, s++) {
4377 for (; s < send; d++, s++) {
4378 *d = toLOWER_LATIN1(*s);
4382 if (source != dest) {
4384 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4394 SV * const sv = TOPs;
4396 register const char *s = SvPV_const(sv,len);
4398 SvUTF8_off(TARG); /* decontaminate */
4401 SvUPGRADE(TARG, SVt_PV);
4402 SvGROW(TARG, (len * 2) + 1);
4406 if (UTF8_IS_CONTINUED(*s)) {
4407 STRLEN ulen = UTF8SKIP(s);
4431 SvCUR_set(TARG, d - SvPVX_const(TARG));
4432 (void)SvPOK_only_UTF8(TARG);
4435 sv_setpvn(TARG, s, len);
4444 dVAR; dSP; dMARK; dORIGMARK;
4445 register AV *const av = MUTABLE_AV(POPs);
4446 register const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
4448 if (SvTYPE(av) == SVt_PVAV) {
4449 const I32 arybase = CopARYBASE_get(PL_curcop);
4450 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
4451 bool can_preserve = FALSE;
4457 can_preserve = SvCANEXISTDELETE(av);
4460 if (lval && localizing) {
4463 for (svp = MARK + 1; svp <= SP; svp++) {
4464 const I32 elem = SvIV(*svp);
4468 if (max > AvMAX(av))
4472 while (++MARK <= SP) {
4474 I32 elem = SvIV(*MARK);
4475 bool preeminent = TRUE;
4479 if (localizing && can_preserve) {
4480 /* If we can determine whether the element exist,
4481 * Try to preserve the existenceness of a tied array
4482 * element by using EXISTS and DELETE if possible.
4483 * Fallback to FETCH and STORE otherwise. */
4484 preeminent = av_exists(av, elem);
4487 svp = av_fetch(av, elem, lval);
4489 if (!svp || *svp == &PL_sv_undef)
4490 DIE(aTHX_ PL_no_aelem, elem);
4493 save_aelem(av, elem, svp);
4495 SAVEADELETE(av, elem);
4498 *MARK = svp ? *svp : &PL_sv_undef;
4501 if (GIMME != G_ARRAY) {
4503 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
4513 AV *array = MUTABLE_AV(POPs);
4514 const I32 gimme = GIMME_V;
4515 IV *iterp = Perl_av_iter_p(aTHX_ array);
4516 const IV current = (*iterp)++;
4518 if (current > av_len(array)) {
4520 if (gimme == G_SCALAR)
4527 mPUSHi(CopARYBASE_get(PL_curcop) + current);
4528 if (gimme == G_ARRAY) {
4529 SV **const element = av_fetch(array, current, 0);
4530 PUSHs(element ? *element : &PL_sv_undef);
4539 AV *array = MUTABLE_AV(POPs);
4540 const I32 gimme = GIMME_V;
4542 *Perl_av_iter_p(aTHX_ array) = 0;
4544 if (gimme == G_SCALAR) {
4546 PUSHi(av_len(array) + 1);
4548 else if (gimme == G_ARRAY) {
4549 IV n = Perl_av_len(aTHX_ array);
4550 IV i = CopARYBASE_get(PL_curcop);
4554 if (PL_op->op_type == OP_AKEYS) {
4556 for (; i <= n; i++) {
4561 for (i = 0; i <= n; i++) {
4562 SV *const *const elem = Perl_av_fetch(aTHX_ array, i, 0);
4563 PUSHs(elem ? *elem : &PL_sv_undef);
4570 /* Associative arrays. */
4576 HV * hash = MUTABLE_HV(POPs);
4578 const I32 gimme = GIMME_V;
4581 /* might clobber stack_sp */
4582 entry = hv_iternext(hash);
4587 SV* const sv = hv_iterkeysv(entry);
4588 PUSHs(sv); /* won't clobber stack_sp */
4589 if (gimme == G_ARRAY) {
4592 /* might clobber stack_sp */
4593 val = hv_iterval(hash, entry);
4598 else if (gimme == G_SCALAR)
4605 S_do_delete_local(pTHX)
4609 const I32 gimme = GIMME_V;
4613 if (PL_op->op_private & OPpSLICE) {
4615 SV * const osv = POPs;
4616 const bool tied = SvRMAGICAL(osv)
4617 && mg_find((const SV *)osv, PERL_MAGIC_tied);
4618 const bool can_preserve = SvCANEXISTDELETE(osv)
4619 || mg_find((const SV *)osv, PERL_MAGIC_env);
4620 const U32 type = SvTYPE(osv);
4621 if (type == SVt_PVHV) { /* hash element */
4622 HV * const hv = MUTABLE_HV(osv);
4623 while (++MARK <= SP) {
4624 SV * const keysv = *MARK;
4626 bool preeminent = TRUE;
4628 preeminent = hv_exists_ent(hv, keysv, 0);
4630 HE *he = hv_fetch_ent(hv, keysv, 1, 0);
4637 sv = hv_delete_ent(hv, keysv, 0, 0);
4638 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4641 save_helem_flags(hv, keysv, &sv, SAVEf_KEEPOLDELEM);
4643 *MARK = sv_mortalcopy(sv);
4649 SAVEHDELETE(hv, keysv);
4650 *MARK = &PL_sv_undef;
4654 else if (type == SVt_PVAV) { /* array element */
4655 if (PL_op->op_flags & OPf_SPECIAL) {
4656 AV * const av = MUTABLE_AV(osv);
4657 while (++MARK <= SP) {
4658 I32 idx = SvIV(*MARK);
4660 bool preeminent = TRUE;
4662 preeminent = av_exists(av, idx);
4664 SV **svp = av_fetch(av, idx, 1);
4671 sv = av_delete(av, idx, 0);
4672 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4675 save_aelem_flags(av, idx, &sv, SAVEf_KEEPOLDELEM);
4677 *MARK = sv_mortalcopy(sv);
4683 SAVEADELETE(av, idx);
4684 *MARK = &PL_sv_undef;
4690 DIE(aTHX_ "Not a HASH reference");
4691 if (gimme == G_VOID)
4693 else if (gimme == G_SCALAR) {
4698 *++MARK = &PL_sv_undef;
4703 SV * const keysv = POPs;
4704 SV * const osv = POPs;
4705 const bool tied = SvRMAGICAL(osv)
4706 && mg_find((const SV *)osv, PERL_MAGIC_tied);
4707 const bool can_preserve = SvCANEXISTDELETE(osv)
4708 || mg_find((const SV *)osv, PERL_MAGIC_env);
4709 const U32 type = SvTYPE(osv);
4711 if (type == SVt_PVHV) {
4712 HV * const hv = MUTABLE_HV(osv);
4713 bool preeminent = TRUE;
4715 preeminent = hv_exists_ent(hv, keysv, 0);
4717 HE *he = hv_fetch_ent(hv, keysv, 1, 0);
4724 sv = hv_delete_ent(hv, keysv, 0, 0);
4725 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4728 save_helem_flags(hv, keysv, &sv, SAVEf_KEEPOLDELEM);
4730 SV *nsv = sv_mortalcopy(sv);
4736 SAVEHDELETE(hv, keysv);
4738 else if (type == SVt_PVAV) {
4739 if (PL_op->op_flags & OPf_SPECIAL) {
4740 AV * const av = MUTABLE_AV(osv);
4741 I32 idx = SvIV(keysv);
4742 bool preeminent = TRUE;
4744 preeminent = av_exists(av, idx);
4746 SV **svp = av_fetch(av, idx, 1);
4753 sv = av_delete(av, idx, 0);
4754 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4757 save_aelem_flags(av, idx, &sv, SAVEf_KEEPOLDELEM);
4759 SV *nsv = sv_mortalcopy(sv);
4765 SAVEADELETE(av, idx);
4768 DIE(aTHX_ "panic: avhv_delete no longer supported");
4771 DIE(aTHX_ "Not a HASH reference");
4774 if (gimme != G_VOID)
4788 if (PL_op->op_private & OPpLVAL_INTRO)
4789 return do_delete_local();
4792 discard = (gimme == G_VOID) ? G_DISCARD : 0;
4794 if (PL_op->op_private & OPpSLICE) {
4796 HV * const hv = MUTABLE_HV(POPs);
4797 const U32 hvtype = SvTYPE(hv);
4798 if (hvtype == SVt_PVHV) { /* hash element */
4799 while (++MARK <= SP) {
4800 SV * const sv = hv_delete_ent(hv, *MARK, discard, 0);
4801 *MARK = sv ? sv : &PL_sv_undef;
4804 else if (hvtype == SVt_PVAV) { /* array element */
4805 if (PL_op->op_flags & OPf_SPECIAL) {
4806 while (++MARK <= SP) {
4807 SV * const sv = av_delete(MUTABLE_AV(hv), SvIV(*MARK), discard);
4808 *MARK = sv ? sv : &PL_sv_undef;
4813 DIE(aTHX_ "Not a HASH reference");
4816 else if (gimme == G_SCALAR) {
4821 *++MARK = &PL_sv_undef;
4827 HV * const hv = MUTABLE_HV(POPs);
4829 if (SvTYPE(hv) == SVt_PVHV)
4830 sv = hv_delete_ent(hv, keysv, discard, 0);
4831 else if (SvTYPE(hv) == SVt_PVAV) {
4832 if (PL_op->op_flags & OPf_SPECIAL)
4833 sv = av_delete(MUTABLE_AV(hv), SvIV(keysv), discard);
4835 DIE(aTHX_ "panic: avhv_delete no longer supported");
4838 DIE(aTHX_ "Not a HASH reference");
4854 if (PL_op->op_private & OPpEXISTS_SUB) {
4856 SV * const sv = POPs;
4857 CV * const cv = sv_2cv(sv, &hv, &gv, 0);
4860 if (gv && isGV(gv) && GvCV(gv) && !GvCVGEN(gv))
4865 hv = MUTABLE_HV(POPs);
4866 if (SvTYPE(hv) == SVt_PVHV) {
4867 if (hv_exists_ent(hv, tmpsv, 0))
4870 else if (SvTYPE(hv) == SVt_PVAV) {
4871 if (PL_op->op_flags & OPf_SPECIAL) { /* array element */
4872 if (av_exists(MUTABLE_AV(hv), SvIV(tmpsv)))
4877 DIE(aTHX_ "Not a HASH reference");
4884 dVAR; dSP; dMARK; dORIGMARK;
4885 register HV * const hv = MUTABLE_HV(POPs);
4886 register const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
4887 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
4888 bool can_preserve = FALSE;
4894 if (SvCANEXISTDELETE(hv) || mg_find((const SV *)hv, PERL_MAGIC_env))
4895 can_preserve = TRUE;
4898 while (++MARK <= SP) {
4899 SV * const keysv = *MARK;
4902 bool preeminent = TRUE;
4904 if (localizing && can_preserve) {
4905 /* If we can determine whether the element exist,
4906 * try to preserve the existenceness of a tied hash
4907 * element by using EXISTS and DELETE if possible.
4908 * Fallback to FETCH and STORE otherwise. */
4909 preeminent = hv_exists_ent(hv, keysv, 0);
4912 he = hv_fetch_ent(hv, keysv, lval, 0);
4913 svp = he ? &HeVAL(he) : NULL;
4916 if (!svp || *svp == &PL_sv_undef) {
4917 DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
4920 if (HvNAME_get(hv) && isGV(*svp))
4921 save_gp(MUTABLE_GV(*svp), !(PL_op->op_flags & OPf_SPECIAL));
4922 else if (preeminent)
4923 save_helem_flags(hv, keysv, svp,
4924 (PL_op->op_flags & OPf_SPECIAL) ? 0 : SAVEf_SETMAGIC);
4926 SAVEHDELETE(hv, keysv);
4929 *MARK = svp ? *svp : &PL_sv_undef;
4931 if (GIMME != G_ARRAY) {
4933 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
4939 /* List operators. */
4944 if (GIMME != G_ARRAY) {
4946 *MARK = *SP; /* unwanted list, return last item */
4948 *MARK = &PL_sv_undef;
4958 SV ** const lastrelem = PL_stack_sp;
4959 SV ** const lastlelem = PL_stack_base + POPMARK;
4960 SV ** const firstlelem = PL_stack_base + POPMARK + 1;
4961 register SV ** const firstrelem = lastlelem + 1;
4962 const I32 arybase = CopARYBASE_get(PL_curcop);
4963 I32 is_something_there = FALSE;
4965 register const I32 max = lastrelem - lastlelem;
4966 register SV **lelem;
4968 if (GIMME != G_ARRAY) {
4969 I32 ix = SvIV(*lastlelem);
4974 if (ix < 0 || ix >= max)
4975 *firstlelem = &PL_sv_undef;
4977 *firstlelem = firstrelem[ix];
4983 SP = firstlelem - 1;
4987 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
4988 I32 ix = SvIV(*lelem);
4993 if (ix < 0 || ix >= max)
4994 *lelem = &PL_sv_undef;
4996 is_something_there = TRUE;
4997 if (!(*lelem = firstrelem[ix]))
4998 *lelem = &PL_sv_undef;
5001 if (is_something_there)
5004 SP = firstlelem - 1;
5010 dVAR; dSP; dMARK; dORIGMARK;
5011 const I32 items = SP - MARK;
5012 SV * const av = MUTABLE_SV(av_make(items, MARK+1));
5013 SP = ORIGMARK; /* av_make() might realloc stack_sp */
5014 mXPUSHs((PL_op->op_flags & OPf_SPECIAL)
5015 ? newRV_noinc(av) : av);
5021 dVAR; dSP; dMARK; dORIGMARK;
5022 HV* const hv = newHV();
5025 SV * const key = *++MARK;
5026 SV * const val = newSV(0);
5028 sv_setsv(val, *++MARK);
5030 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Odd number of elements in anonymous hash");
5031 (void)hv_store_ent(hv,key,val,0);
5034 mXPUSHs((PL_op->op_flags & OPf_SPECIAL)
5035 ? newRV_noinc(MUTABLE_SV(hv)) : MUTABLE_SV(hv));
5041 dVAR; dSP; dMARK; dORIGMARK;
5042 register AV *ary = MUTABLE_AV(*++MARK);
5046 register I32 offset;
5047 register I32 length;
5051 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
5054 *MARK-- = SvTIED_obj(MUTABLE_SV(ary), mg);
5057 ENTER_with_name("call_SPLICE");
5058 call_method("SPLICE",GIMME_V);
5059 LEAVE_with_name("call_SPLICE");
5067 offset = i = SvIV(*MARK);
5069 offset += AvFILLp(ary) + 1;
5071 offset -= CopARYBASE_get(PL_curcop);
5073 DIE(aTHX_ PL_no_aelem, i);
5075 length = SvIVx(*MARK++);
5077 length += AvFILLp(ary) - offset + 1;
5083 length = AvMAX(ary) + 1; /* close enough to infinity */
5087 length = AvMAX(ary) + 1;
5089 if (offset > AvFILLp(ary) + 1) {
5090 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "splice() offset past end of array" );
5091 offset = AvFILLp(ary) + 1;
5093 after = AvFILLp(ary) + 1 - (offset + length);
5094 if (after < 0) { /* not that much array */
5095 length += after; /* offset+length now in array */
5101 /* At this point, MARK .. SP-1 is our new LIST */
5104 diff = newlen - length;
5105 if (newlen && !AvREAL(ary) && AvREIFY(ary))
5108 /* make new elements SVs now: avoid problems if they're from the array */
5109 for (dst = MARK, i = newlen; i; i--) {
5110 SV * const h = *dst;
5111 *dst++ = newSVsv(h);
5114 if (diff < 0) { /* shrinking the area */
5115 SV **tmparyval = NULL;
5117 Newx(tmparyval, newlen, SV*); /* so remember insertion */
5118 Copy(MARK, tmparyval, newlen, SV*);
5121 MARK = ORIGMARK + 1;
5122 if (GIMME == G_ARRAY) { /* copy return vals to stack */
5123 MEXTEND(MARK, length);
5124 Copy(AvARRAY(ary)+offset, MARK, length, SV*);
5126 EXTEND_MORTAL(length);
5127 for (i = length, dst = MARK; i; i--) {
5128 sv_2mortal(*dst); /* free them eventualy */
5135 *MARK = AvARRAY(ary)[offset+length-1];
5138 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
5139 SvREFCNT_dec(*dst++); /* free them now */
5142 AvFILLp(ary) += diff;
5144 /* pull up or down? */
5146 if (offset < after) { /* easier to pull up */
5147 if (offset) { /* esp. if nothing to pull */
5148 src = &AvARRAY(ary)[offset-1];
5149 dst = src - diff; /* diff is negative */
5150 for (i = offset; i > 0; i--) /* can't trust Copy */
5154 AvARRAY(ary) = AvARRAY(ary) - diff; /* diff is negative */
5158 if (after) { /* anything to pull down? */
5159 src = AvARRAY(ary) + offset + length;
5160 dst = src + diff; /* diff is negative */
5161 Move(src, dst, after, SV*);
5163 dst = &AvARRAY(ary)[AvFILLp(ary)+1];
5164 /* avoid later double free */
5168 dst[--i] = &PL_sv_undef;
5171 Copy( tmparyval, AvARRAY(ary) + offset, newlen, SV* );
5172 Safefree(tmparyval);
5175 else { /* no, expanding (or same) */
5176 SV** tmparyval = NULL;
5178 Newx(tmparyval, length, SV*); /* so remember deletion */
5179 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
5182 if (diff > 0) { /* expanding */
5183 /* push up or down? */
5184 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
5188 Move(src, dst, offset, SV*);
5190 AvARRAY(ary) = AvARRAY(ary) - diff;/* diff is positive */
5192 AvFILLp(ary) += diff;
5195 if (AvFILLp(ary) + diff >= AvMAX(ary)) /* oh, well */
5196 av_extend(ary, AvFILLp(ary) + diff);
5197 AvFILLp(ary) += diff;
5200 dst = AvARRAY(ary) + AvFILLp(ary);
5202 for (i = after; i; i--) {
5210 Copy( MARK, AvARRAY(ary) + offset, newlen, SV* );
5213 MARK = ORIGMARK + 1;
5214 if (GIMME == G_ARRAY) { /* copy return vals to stack */
5216 Copy(tmparyval, MARK, length, SV*);
5218 EXTEND_MORTAL(length);
5219 for (i = length, dst = MARK; i; i--) {
5220 sv_2mortal(*dst); /* free them eventualy */
5227 else if (length--) {
5228 *MARK = tmparyval[length];
5231 while (length-- > 0)
5232 SvREFCNT_dec(tmparyval[length]);
5236 *MARK = &PL_sv_undef;
5237 Safefree(tmparyval);
5245 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
5246 register AV * const ary = MUTABLE_AV(*++MARK);
5247 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
5250 *MARK-- = SvTIED_obj(MUTABLE_SV(ary), mg);
5253 ENTER_with_name("call_PUSH");
5254 call_method("PUSH",G_SCALAR|G_DISCARD);
5255 LEAVE_with_name("call_PUSH");
5259 PL_delaymagic = DM_DELAY;
5260 for (++MARK; MARK <= SP; MARK++) {
5261 SV * const sv = newSV(0);
5263 sv_setsv(sv, *MARK);
5264 av_store(ary, AvFILLp(ary)+1, sv);
5266 if (PL_delaymagic & DM_ARRAY)
5267 mg_set(MUTABLE_SV(ary));
5272 if (OP_GIMME(PL_op, 0) != G_VOID) {
5273 PUSHi( AvFILL(ary) + 1 );
5282 AV * const av = MUTABLE_AV(POPs);
5283 SV * const sv = PL_op->op_type == OP_SHIFT ? av_shift(av) : av_pop(av);
5287 (void)sv_2mortal(sv);
5294 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
5295 register AV *ary = MUTABLE_AV(*++MARK);
5296 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
5299 *MARK-- = SvTIED_obj(MUTABLE_SV(ary), mg);
5302 ENTER_with_name("call_UNSHIFT");
5303 call_method("UNSHIFT",G_SCALAR|G_DISCARD);
5304 LEAVE_with_name("call_UNSHIFT");
5309 av_unshift(ary, SP - MARK);
5311 SV * const sv = newSVsv(*++MARK);
5312 (void)av_store(ary, i++, sv);
5316 if (OP_GIMME(PL_op, 0) != G_VOID) {
5317 PUSHi( AvFILL(ary) + 1 );
5326 if (GIMME == G_ARRAY) {
5327 if (PL_op->op_private & OPpREVERSE_INPLACE) {
5331 assert( MARK+1 == SP && *SP && SvTYPE(*SP) == SVt_PVAV);
5332 (void)POPMARK; /* remove mark associated with ex-OP_AASSIGN */
5333 av = MUTABLE_AV((*SP));
5334 /* In-place reversing only happens in void context for the array
5335 * assignment. We don't need to push anything on the stack. */
5338 if (SvMAGICAL(av)) {
5340 register SV *tmp = sv_newmortal();
5341 /* For SvCANEXISTDELETE */
5344 bool can_preserve = SvCANEXISTDELETE(av);
5346 for (i = 0, j = av_len(av); i < j; ++i, --j) {
5347 register SV *begin, *end;
5350 if (!av_exists(av, i)) {
5351 if (av_exists(av, j)) {
5352 register SV *sv = av_delete(av, j, 0);
5353 begin = *av_fetch(av, i, TRUE);
5354 sv_setsv_mg(begin, sv);
5358 else if (!av_exists(av, j)) {
5359 register SV *sv = av_delete(av, i, 0);
5360 end = *av_fetch(av, j, TRUE);
5361 sv_setsv_mg(end, sv);
5366 begin = *av_fetch(av, i, TRUE);
5367 end = *av_fetch(av, j, TRUE);
5368 sv_setsv(tmp, begin);
5369 sv_setsv_mg(begin, end);
5370 sv_setsv_mg(end, tmp);
5374 SV **begin = AvARRAY(av);
5375 SV **end = begin + AvFILLp(av);
5377 while (begin < end) {
5378 register SV * const tmp = *begin;
5388 register SV * const tmp = *MARK;
5392 /* safe as long as stack cannot get extended in the above */
5398 register char *down;
5402 PADOFFSET padoff_du;
5404 SvUTF8_off(TARG); /* decontaminate */
5406 do_join(TARG, &PL_sv_no, MARK, SP);
5408 sv_setsv(TARG, (SP > MARK)
5410 : (padoff_du = find_rundefsvoffset(),
5411 (padoff_du == NOT_IN_PAD
5412 || PAD_COMPNAME_FLAGS_isOUR(padoff_du))
5413 ? DEFSV : PAD_SVl(padoff_du)));
5415 if (! SvOK(TARG) && ckWARN(WARN_UNINITIALIZED))
5416 report_uninit(TARG);
5419 up = SvPV_force(TARG, len);
5421 if (DO_UTF8(TARG)) { /* first reverse each character */
5422 U8* s = (U8*)SvPVX(TARG);
5423 const U8* send = (U8*)(s + len);
5425 if (UTF8_IS_INVARIANT(*s)) {
5430 if (!utf8_to_uvchr(s, 0))
5434 down = (char*)(s - 1);
5435 /* reverse this character */
5439 *down-- = (char)tmp;
5445 down = SvPVX(TARG) + len - 1;
5449 *down-- = (char)tmp;
5451 (void)SvPOK_only_UTF8(TARG);
5463 register IV limit = POPi; /* note, negative is forever */
5464 SV * const sv = POPs;
5466 register const char *s = SvPV_const(sv, len);
5467 const bool do_utf8 = DO_UTF8(sv);
5468 const char *strend = s + len;
5470 register REGEXP *rx;
5472 register const char *m;
5474 const STRLEN slen = do_utf8 ? utf8_length((U8*)s, (U8*)strend) : (STRLEN)(strend - s);
5475 I32 maxiters = slen + 10;
5476 I32 trailing_empty = 0;
5478 const I32 origlimit = limit;
5481 const I32 gimme = GIMME_V;
5483 const I32 oldsave = PL_savestack_ix;
5484 U32 make_mortal = SVs_TEMP;
5489 Copy(&LvTARGOFF(POPs), &pm, 1, PMOP*);
5494 DIE(aTHX_ "panic: pp_split");
5497 TAINT_IF((RX_EXTFLAGS(rx) & RXf_PMf_LOCALE) &&
5498 (RX_EXTFLAGS(rx) & (RXf_WHITE | RXf_SKIPWHITE)));
5500 RX_MATCH_UTF8_set(rx, do_utf8);
5503 if (pm->op_pmreplrootu.op_pmtargetoff) {
5504 ary = GvAVn(MUTABLE_GV(PAD_SVl(pm->op_pmreplrootu.op_pmtargetoff)));
5507 if (pm->op_pmreplrootu.op_pmtargetgv) {
5508 ary = GvAVn(pm->op_pmreplrootu.op_pmtargetgv);
5513 if (ary && (gimme != G_ARRAY || (pm->op_pmflags & PMf_ONCE))) {
5519 if ((mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied))) {
5521 XPUSHs(SvTIED_obj(MUTABLE_SV(ary), mg));
5528 for (i = AvFILLp(ary); i >= 0; i--)
5529 AvARRAY(ary)[i] = &PL_sv_undef; /* don't free mere refs */
5531 /* temporarily switch stacks */
5532 SAVESWITCHSTACK(PL_curstack, ary);
5536 base = SP - PL_stack_base;
5538 if (RX_EXTFLAGS(rx) & RXf_SKIPWHITE) {
5540 while (*s == ' ' || is_utf8_space((U8*)s))
5543 else if (RX_EXTFLAGS(rx) & RXf_PMf_LOCALE) {
5544 while (isSPACE_LC(*s))
5552 if (RX_EXTFLAGS(rx) & PMf_MULTILINE) {
5556 gimme_scalar = gimme == G_SCALAR && !ary;
5559 limit = maxiters + 2;
5560 if (RX_EXTFLAGS(rx) & RXf_WHITE) {
5563 /* this one uses 'm' and is a negative test */
5565 while (m < strend && !( *m == ' ' || is_utf8_space((U8*)m) )) {
5566 const int t = UTF8SKIP(m);
5567 /* is_utf8_space returns FALSE for malform utf8 */
5573 } else if (RX_EXTFLAGS(rx) & RXf_PMf_LOCALE) {
5574 while (m < strend && !isSPACE_LC(*m))
5577 while (m < strend && !isSPACE(*m))
5590 dstr = newSVpvn_flags(s, m-s,
5591 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5595 /* skip the whitespace found last */
5597 s = m + UTF8SKIP(m);
5601 /* this one uses 's' and is a positive test */
5603 while (s < strend && ( *s == ' ' || is_utf8_space((U8*)s) ))
5605 } else if (RX_EXTFLAGS(rx) & RXf_PMf_LOCALE) {
5606 while (s < strend && isSPACE_LC(*s))
5609 while (s < strend && isSPACE(*s))
5614 else if (RX_EXTFLAGS(rx) & RXf_START_ONLY) {
5616 for (m = s; m < strend && *m != '\n'; m++)
5629 dstr = newSVpvn_flags(s, m-s,
5630 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5636 else if (RX_EXTFLAGS(rx) & RXf_NULL && !(s >= strend)) {
5638 Pre-extend the stack, either the number of bytes or
5639 characters in the string or a limited amount, triggered by:
5641 my ($x, $y) = split //, $str;
5645 if (!gimme_scalar) {
5646 const U32 items = limit - 1;
5655 /* keep track of how many bytes we skip over */
5665 dstr = newSVpvn_flags(m, s-m, SVf_UTF8 | make_mortal);
5678 dstr = newSVpvn(s, 1);
5694 else if (do_utf8 == (RX_UTF8(rx) != 0) &&
5695 (RX_EXTFLAGS(rx) & RXf_USE_INTUIT) && !RX_NPARENS(rx)
5696 && (RX_EXTFLAGS(rx) & RXf_CHECK_ALL)
5697 && !(RX_EXTFLAGS(rx) & RXf_ANCH)) {
5698 const int tail = (RX_EXTFLAGS(rx) & RXf_INTUIT_TAIL);
5699 SV * const csv = CALLREG_INTUIT_STRING(rx);
5701 len = RX_MINLENRET(rx);
5702 if (len == 1 && !RX_UTF8(rx) && !tail) {
5703 const char c = *SvPV_nolen_const(csv);
5705 for (m = s; m < strend && *m != c; m++)
5716 dstr = newSVpvn_flags(s, m-s,
5717 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5720 /* The rx->minlen is in characters but we want to step
5721 * s ahead by bytes. */
5723 s = (char*)utf8_hop((U8*)m, len);
5725 s = m + len; /* Fake \n at the end */
5729 while (s < strend && --limit &&
5730 (m = fbm_instr((unsigned char*)s, (unsigned char*)strend,
5731 csv, multiline ? FBMrf_MULTILINE : 0)) )
5740 dstr = newSVpvn_flags(s, m-s,
5741 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5744 /* The rx->minlen is in characters but we want to step
5745 * s ahead by bytes. */
5747 s = (char*)utf8_hop((U8*)m, len);
5749 s = m + len; /* Fake \n at the end */
5754 maxiters += slen * RX_NPARENS(rx);
5755 while (s < strend && --limit)
5759 rex_return = CALLREGEXEC(rx, (char*)s, (char*)strend, (char*)orig, 1 ,
5762 if (rex_return == 0)
5764 TAINT_IF(RX_MATCH_TAINTED(rx));
5765 if (RX_MATCH_COPIED(rx) && RX_SUBBEG(rx) != orig) {
5768 orig = RX_SUBBEG(rx);
5770 strend = s + (strend - m);
5772 m = RX_OFFS(rx)[0].start + orig;
5781 dstr = newSVpvn_flags(s, m-s,
5782 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5785 if (RX_NPARENS(rx)) {
5787 for (i = 1; i <= (I32)RX_NPARENS(rx); i++) {
5788 s = RX_OFFS(rx)[i].start + orig;
5789 m = RX_OFFS(rx)[i].end + orig;
5791 /* japhy (07/27/01) -- the (m && s) test doesn't catch
5792 parens that didn't match -- they should be set to
5793 undef, not the empty string */
5801 if (m >= orig && s >= orig) {
5802 dstr = newSVpvn_flags(s, m-s,
5803 (do_utf8 ? SVf_UTF8 : 0)
5807 dstr = &PL_sv_undef; /* undef, not "" */
5813 s = RX_OFFS(rx)[0].end + orig;
5817 if (!gimme_scalar) {
5818 iters = (SP - PL_stack_base) - base;
5820 if (iters > maxiters)
5821 DIE(aTHX_ "Split loop");
5823 /* keep field after final delim? */
5824 if (s < strend || (iters && origlimit)) {
5825 if (!gimme_scalar) {
5826 const STRLEN l = strend - s;
5827 dstr = newSVpvn_flags(s, l, (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5832 else if (!origlimit) {
5834 iters -= trailing_empty;
5836 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0)) {
5837 if (TOPs && !make_mortal)
5839 *SP-- = &PL_sv_undef;
5846 LEAVE_SCOPE(oldsave); /* may undo an earlier SWITCHSTACK */
5850 if (SvSMAGICAL(ary)) {
5852 mg_set(MUTABLE_SV(ary));
5855 if (gimme == G_ARRAY) {
5857 Copy(AvARRAY(ary), SP + 1, iters, SV*);
5864 ENTER_with_name("call_PUSH");
5865 call_method("PUSH",G_SCALAR|G_DISCARD);
5866 LEAVE_with_name("call_PUSH");
5868 if (gimme == G_ARRAY) {
5870 /* EXTEND should not be needed - we just popped them */
5872 for (i=0; i < iters; i++) {
5873 SV **svp = av_fetch(ary, i, FALSE);
5874 PUSHs((svp) ? *svp : &PL_sv_undef);
5881 if (gimme == G_ARRAY)
5893 SV *const sv = PAD_SVl(PL_op->op_targ);
5895 if (SvPADSTALE(sv)) {
5898 RETURNOP(cLOGOP->op_other);
5900 RETURNOP(cLOGOP->op_next);
5909 assert(SvTYPE(retsv) != SVt_PVCV);
5911 if (SvTYPE(retsv) == SVt_PVAV || SvTYPE(retsv) == SVt_PVHV) {
5912 retsv = refto(retsv);
5919 PP(unimplemented_op)
5922 DIE(aTHX_ "panic: unimplemented op %s (#%d) called", OP_NAME(PL_op),
5931 HV * const hv = (HV*)POPs;
5933 if (SvRMAGICAL(hv)) {
5934 MAGIC * const mg = mg_find((SV*)hv, PERL_MAGIC_tied);
5936 XPUSHs(magic_scalarpack(hv, mg));
5941 XPUSHs(boolSV(HvKEYS(hv) != 0));
5947 * c-indentation-style: bsd
5949 * indent-tabs-mode: t
5952 * ex: set ts=8 sts=4 sw=4 noet: