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.
32 #include "regcharclass.h"
34 /* XXX I can't imagine anyone who doesn't have this actually _needs_
35 it, since pid_t is an integral type.
38 #ifdef NEED_GETPID_PROTO
39 extern Pid_t getpid (void);
43 * Some BSDs and Cygwin default to POSIX math instead of IEEE.
44 * This switches them over to IEEE.
46 #if defined(LIBM_LIB_VERSION)
47 _LIB_VERSION_TYPE _LIB_VERSION = _IEEE_;
50 /* variations on pp_null */
56 if (GIMME_V == G_SCALAR)
67 assert(SvTYPE(TARG) == SVt_PVAV);
68 if (PL_op->op_private & OPpLVAL_INTRO)
69 if (!(PL_op->op_private & OPpPAD_STATE))
70 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
72 if (PL_op->op_flags & OPf_REF) {
75 } else if (PL_op->op_private & OPpMAYBE_LVSUB) {
76 const I32 flags = is_lvalue_sub();
77 if (flags && !(flags & OPpENTERSUB_INARGS)) {
78 if (GIMME == G_SCALAR)
79 /* diag_listed_as: Can't return %s to lvalue scalar context */
80 Perl_croak(aTHX_ "Can't return array to lvalue scalar context");
86 if (gimme == G_ARRAY) {
87 const I32 maxarg = AvFILL(MUTABLE_AV(TARG)) + 1;
89 if (SvMAGICAL(TARG)) {
91 for (i=0; i < (U32)maxarg; i++) {
92 SV * const * const svp = av_fetch(MUTABLE_AV(TARG), i, FALSE);
93 SP[i+1] = (svp) ? *svp : &PL_sv_undef;
97 Copy(AvARRAY((const AV *)TARG), SP+1, maxarg, SV*);
101 else if (gimme == G_SCALAR) {
102 SV* const sv = sv_newmortal();
103 const I32 maxarg = AvFILL(MUTABLE_AV(TARG)) + 1;
104 sv_setiv(sv, maxarg);
115 assert(SvTYPE(TARG) == SVt_PVHV);
117 if (PL_op->op_private & OPpLVAL_INTRO)
118 if (!(PL_op->op_private & OPpPAD_STATE))
119 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
120 if (PL_op->op_flags & OPf_REF)
122 else if (PL_op->op_private & OPpMAYBE_LVSUB) {
123 const I32 flags = is_lvalue_sub();
124 if (flags && !(flags & OPpENTERSUB_INARGS)) {
125 if (GIMME == G_SCALAR)
126 /* diag_listed_as: Can't return %s to lvalue scalar context */
127 Perl_croak(aTHX_ "Can't return hash to lvalue scalar context");
132 if (gimme == G_ARRAY) {
133 RETURNOP(Perl_do_kv(aTHX));
135 else if ((PL_op->op_private & OPpTRUEBOOL
136 || ( PL_op->op_private & OPpMAYBE_TRUEBOOL
137 && block_gimme() == G_VOID ))
138 && (!SvRMAGICAL(TARG) || !mg_find(TARG, PERL_MAGIC_tied)))
139 SETs(HvUSEDKEYS(TARG) ? &PL_sv_yes : sv_2mortal(newSViv(0)));
140 else if (gimme == G_SCALAR) {
141 SV* const sv = Perl_hv_scalar(aTHX_ MUTABLE_HV(TARG));
150 assert(SvTYPE(TARG) == SVt_PVCV);
157 static const char S_no_symref_sv[] =
158 "Can't use string (\"%" SVf32 "\"%s) as %s ref while \"strict refs\" in use";
160 /* In some cases this function inspects PL_op. If this function is called
161 for new op types, more bool parameters may need to be added in place of
164 When noinit is true, the absence of a gv will cause a retval of undef.
165 This is unrelated to the cv-to-gv assignment case.
169 S_rv2gv(pTHX_ SV *sv, const bool vivify_sv, const bool strict,
173 if (!isGV(sv) || SvFAKE(sv)) SvGETMAGIC(sv);
176 sv = amagic_deref_call(sv, to_gv_amg);
180 if (SvTYPE(sv) == SVt_PVIO) {
181 GV * const gv = MUTABLE_GV(sv_newmortal());
182 gv_init(gv, 0, "__ANONIO__", 10, 0);
183 GvIOp(gv) = MUTABLE_IO(sv);
184 SvREFCNT_inc_void_NN(sv);
187 else if (!isGV_with_GP(sv))
188 return (SV *)Perl_die(aTHX_ "Not a GLOB reference");
191 if (!isGV_with_GP(sv)) {
193 /* If this is a 'my' scalar and flag is set then vivify
196 if (vivify_sv && sv != &PL_sv_undef) {
199 Perl_croak_no_modify(aTHX);
200 if (cUNOP->op_targ) {
201 SV * const namesv = PAD_SV(cUNOP->op_targ);
202 gv = MUTABLE_GV(newSV(0));
203 gv_init_sv(gv, CopSTASH(PL_curcop), namesv, 0);
206 const char * const name = CopSTASHPV(PL_curcop);
207 gv = newGVgen_flags(name,
208 HvNAMEUTF8(CopSTASH(PL_curcop)) ? SVf_UTF8 : 0 );
210 prepare_SV_for_RV(sv);
211 SvRV_set(sv, MUTABLE_SV(gv));
216 if (PL_op->op_flags & OPf_REF || strict)
217 return (SV *)Perl_die(aTHX_ PL_no_usym, "a symbol");
218 if (ckWARN(WARN_UNINITIALIZED))
224 if (!(sv = MUTABLE_SV(gv_fetchsv_nomg(
225 sv, GV_ADDMG, SVt_PVGV
235 (SvPOKp(sv) && SvCUR(sv)>32 ? "..." : ""),
238 if ((PL_op->op_private & (OPpLVAL_INTRO|OPpDONT_INIT_GV))
239 == OPpDONT_INIT_GV) {
240 /* We are the target of a coderef assignment. Return
241 the scalar unchanged, and let pp_sasssign deal with
245 sv = MUTABLE_SV(gv_fetchsv_nomg(sv, GV_ADD, SVt_PVGV));
247 /* FAKE globs in the symbol table cause weird bugs (#77810) */
251 if (SvFAKE(sv) && !(PL_op->op_private & OPpALLOW_FAKE)) {
252 SV *newsv = sv_newmortal();
253 sv_setsv_flags(newsv, sv, 0);
265 sv, PL_op->op_private & OPpDEREF,
266 PL_op->op_private & HINT_STRICT_REFS,
267 ((PL_op->op_flags & OPf_SPECIAL) && !(PL_op->op_flags & OPf_MOD))
268 || PL_op->op_type == OP_READLINE
270 if (PL_op->op_private & OPpLVAL_INTRO)
271 save_gp(MUTABLE_GV(sv), !(PL_op->op_flags & OPf_SPECIAL));
276 /* Helper function for pp_rv2sv and pp_rv2av */
278 Perl_softref2xv(pTHX_ SV *const sv, const char *const what,
279 const svtype type, SV ***spp)
284 PERL_ARGS_ASSERT_SOFTREF2XV;
286 if (PL_op->op_private & HINT_STRICT_REFS) {
288 Perl_die(aTHX_ S_no_symref_sv, sv,
289 (SvPOKp(sv) && SvCUR(sv)>32 ? "..." : ""), what);
291 Perl_die(aTHX_ PL_no_usym, what);
295 PL_op->op_flags & OPf_REF
297 Perl_die(aTHX_ PL_no_usym, what);
298 if (ckWARN(WARN_UNINITIALIZED))
300 if (type != SVt_PV && GIMME_V == G_ARRAY) {
304 **spp = &PL_sv_undef;
307 if ((PL_op->op_flags & OPf_SPECIAL) &&
308 !(PL_op->op_flags & OPf_MOD))
310 if (!(gv = gv_fetchsv_nomg(sv, GV_ADDMG, type)))
312 **spp = &PL_sv_undef;
317 gv = gv_fetchsv_nomg(sv, GV_ADD, type);
330 sv = amagic_deref_call(sv, to_sv_amg);
334 switch (SvTYPE(sv)) {
340 DIE(aTHX_ "Not a SCALAR reference");
347 if (!isGV_with_GP(gv)) {
348 gv = Perl_softref2xv(aTHX_ sv, "a SCALAR", SVt_PV, &sp);
354 if (PL_op->op_flags & OPf_MOD) {
355 if (PL_op->op_private & OPpLVAL_INTRO) {
356 if (cUNOP->op_first->op_type == OP_NULL)
357 sv = save_scalar(MUTABLE_GV(TOPs));
359 sv = save_scalar(gv);
361 Perl_croak(aTHX_ "%s", PL_no_localize_ref);
363 else if (PL_op->op_private & OPpDEREF)
364 sv = vivify_ref(sv, PL_op->op_private & OPpDEREF);
373 AV * const av = MUTABLE_AV(TOPs);
374 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
376 SV ** const sv = Perl_av_arylen_p(aTHX_ MUTABLE_AV(av));
378 *sv = newSV_type(SVt_PVMG);
379 sv_magic(*sv, MUTABLE_SV(av), PERL_MAGIC_arylen, NULL, 0);
383 SETs(sv_2mortal(newSViv(AvFILL(MUTABLE_AV(av)))));
392 if (PL_op->op_flags & OPf_MOD || LVRET) {
393 SV * const ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
394 sv_magic(ret, NULL, PERL_MAGIC_pos, NULL, 0);
396 LvTARG(ret) = SvREFCNT_inc_simple(sv);
397 PUSHs(ret); /* no SvSETMAGIC */
401 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
402 const MAGIC * const mg = mg_find(sv, PERL_MAGIC_regex_global);
403 if (mg && mg->mg_len >= 0) {
421 const I32 flags = (PL_op->op_flags & OPf_SPECIAL)
423 : ((PL_op->op_private & (OPpLVAL_INTRO|OPpMAY_RETURN_CONSTANT)) == OPpMAY_RETURN_CONSTANT)
426 /* We usually try to add a non-existent subroutine in case of AUTOLOAD. */
427 /* (But not in defined().) */
429 CV *cv = sv_2cv(TOPs, &stash_unused, &gv, flags);
431 else if ((flags == (GV_ADD|GV_NOEXPAND)) && gv && SvROK(gv)) {
435 cv = MUTABLE_CV(&PL_sv_undef);
436 SETs(MUTABLE_SV(cv));
446 SV *ret = &PL_sv_undef;
448 if (SvGMAGICAL(TOPs)) SETs(sv_mortalcopy(TOPs));
449 if (SvPOK(TOPs) && SvCUR(TOPs) >= 7) {
450 const char * s = SvPVX_const(TOPs);
451 if (strnEQ(s, "CORE::", 6)) {
452 const int code = keyword(s + 6, SvCUR(TOPs) - 6, 1);
453 if (!code || code == -KEY_CORE)
454 DIE(aTHX_ "Can't find an opnumber for \"%"SVf"\"",
455 SVfARG(newSVpvn_flags(
456 s+6, SvCUR(TOPs)-6, SvFLAGS(TOPs) & SVf_UTF8
459 SV * const sv = core_prototype(NULL, s + 6, code, NULL);
465 cv = sv_2cv(TOPs, &stash, &gv, 0);
467 ret = newSVpvn_flags(
468 CvPROTO(cv), CvPROTOLEN(cv), SVs_TEMP | SvUTF8(cv)
478 CV *cv = MUTABLE_CV(PAD_SV(PL_op->op_targ));
480 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
482 PUSHs(MUTABLE_SV(cv));
496 if (GIMME != G_ARRAY) {
500 *MARK = &PL_sv_undef;
501 *MARK = refto(*MARK);
505 EXTEND_MORTAL(SP - MARK);
507 *MARK = refto(*MARK);
512 S_refto(pTHX_ SV *sv)
517 PERL_ARGS_ASSERT_REFTO;
519 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
522 if (!(sv = LvTARG(sv)))
525 SvREFCNT_inc_void_NN(sv);
527 else if (SvTYPE(sv) == SVt_PVAV) {
528 if (!AvREAL((const AV *)sv) && AvREIFY((const AV *)sv))
529 av_reify(MUTABLE_AV(sv));
531 SvREFCNT_inc_void_NN(sv);
533 else if (SvPADTMP(sv) && !IS_PADGV(sv))
537 SvREFCNT_inc_void_NN(sv);
540 sv_upgrade(rv, SVt_IV);
549 SV * const sv = POPs;
554 if (!sv || !SvROK(sv))
557 (void)sv_ref(TARG,SvRV(sv),TRUE);
569 stash = CopSTASH(PL_curcop);
571 SV * const ssv = POPs;
575 if (!ssv) goto curstash;
576 if (!SvGMAGICAL(ssv) && !SvAMAGIC(ssv) && SvROK(ssv))
577 Perl_croak(aTHX_ "Attempt to bless into a reference");
578 ptr = SvPV_const(ssv,len);
580 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
581 "Explicit blessing to '' (assuming package main)");
582 stash = gv_stashpvn(ptr, len, GV_ADD|SvUTF8(ssv));
585 (void)sv_bless(TOPs, stash);
595 const char * const elem = SvPV_const(sv, len);
596 GV * const gv = MUTABLE_GV(POPs);
601 /* elem will always be NUL terminated. */
602 const char * const second_letter = elem + 1;
605 if (len == 5 && strEQ(second_letter, "RRAY"))
606 tmpRef = MUTABLE_SV(GvAV(gv));
609 if (len == 4 && strEQ(second_letter, "ODE"))
610 tmpRef = MUTABLE_SV(GvCVu(gv));
613 if (len == 10 && strEQ(second_letter, "ILEHANDLE")) {
614 /* finally deprecated in 5.8.0 */
615 deprecate("*glob{FILEHANDLE}");
616 tmpRef = MUTABLE_SV(GvIOp(gv));
619 if (len == 6 && strEQ(second_letter, "ORMAT"))
620 tmpRef = MUTABLE_SV(GvFORM(gv));
623 if (len == 4 && strEQ(second_letter, "LOB"))
624 tmpRef = MUTABLE_SV(gv);
627 if (len == 4 && strEQ(second_letter, "ASH"))
628 tmpRef = MUTABLE_SV(GvHV(gv));
631 if (*second_letter == 'O' && !elem[2] && len == 2)
632 tmpRef = MUTABLE_SV(GvIOp(gv));
635 if (len == 4 && strEQ(second_letter, "AME"))
636 sv = newSVhek(GvNAME_HEK(gv));
639 if (len == 7 && strEQ(second_letter, "ACKAGE")) {
640 const HV * const stash = GvSTASH(gv);
641 const HEK * const hek = stash ? HvNAME_HEK(stash) : NULL;
642 sv = hek ? newSVhek(hek) : newSVpvs("__ANON__");
646 if (len == 6 && strEQ(second_letter, "CALAR"))
661 /* Pattern matching */
669 if (len == 0 || len > I32_MAX || !SvPOK(sv) || SvUTF8(sv) || SvVALID(sv)) {
670 /* Historically, study was skipped in these cases. */
674 /* Make study a no-op. It's no longer useful and its existence
675 complicates matters elsewhere. */
684 if (PL_op->op_flags & OPf_STACKED)
686 else if (PL_op->op_private & OPpTARGET_MY)
692 if(PL_op->op_type == OP_TRANSR) {
694 const char * const pv = SvPV(sv,len);
695 SV * const newsv = newSVpvn_flags(pv, len, SVs_TEMP|SvUTF8(sv));
700 TARG = sv_newmortal();
706 /* Lvalue operators. */
709 S_do_chomp(pTHX_ SV *retval, SV *sv, bool chomping)
715 PERL_ARGS_ASSERT_DO_CHOMP;
717 if (chomping && (RsSNARF(PL_rs) || RsRECORD(PL_rs)))
719 if (SvTYPE(sv) == SVt_PVAV) {
721 AV *const av = MUTABLE_AV(sv);
722 const I32 max = AvFILL(av);
724 for (i = 0; i <= max; i++) {
725 sv = MUTABLE_SV(av_fetch(av, i, FALSE));
726 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
727 do_chomp(retval, sv, chomping);
731 else if (SvTYPE(sv) == SVt_PVHV) {
732 HV* const hv = MUTABLE_HV(sv);
734 (void)hv_iterinit(hv);
735 while ((entry = hv_iternext(hv)))
736 do_chomp(retval, hv_iterval(hv,entry), chomping);
739 else if (SvREADONLY(sv)) {
741 /* SV is copy-on-write */
742 sv_force_normal_flags(sv, 0);
745 Perl_croak_no_modify(aTHX);
750 /* XXX, here sv is utf8-ized as a side-effect!
751 If encoding.pm is used properly, almost string-generating
752 operations, including literal strings, chr(), input data, etc.
753 should have been utf8-ized already, right?
755 sv_recode_to_utf8(sv, PL_encoding);
761 char *temp_buffer = NULL;
770 while (len && s[-1] == '\n') {
777 STRLEN rslen, rs_charlen;
778 const char *rsptr = SvPV_const(PL_rs, rslen);
780 rs_charlen = SvUTF8(PL_rs)
784 if (SvUTF8(PL_rs) != SvUTF8(sv)) {
785 /* Assumption is that rs is shorter than the scalar. */
787 /* RS is utf8, scalar is 8 bit. */
789 temp_buffer = (char*)bytes_from_utf8((U8*)rsptr,
792 /* Cannot downgrade, therefore cannot possibly match
794 assert (temp_buffer == rsptr);
800 else if (PL_encoding) {
801 /* RS is 8 bit, encoding.pm is used.
802 * Do not recode PL_rs as a side-effect. */
803 svrecode = newSVpvn(rsptr, rslen);
804 sv_recode_to_utf8(svrecode, PL_encoding);
805 rsptr = SvPV_const(svrecode, rslen);
806 rs_charlen = sv_len_utf8(svrecode);
809 /* RS is 8 bit, scalar is utf8. */
810 temp_buffer = (char*)bytes_to_utf8((U8*)rsptr, &rslen);
824 if (memNE(s, rsptr, rslen))
826 SvIVX(retval) += rs_charlen;
829 s = SvPV_force_nomg_nolen(sv);
837 SvREFCNT_dec(svrecode);
839 Safefree(temp_buffer);
841 if (len && !SvPOK(sv))
842 s = SvPV_force_nomg(sv, len);
845 char * const send = s + len;
846 char * const start = s;
848 while (s > start && UTF8_IS_CONTINUATION(*s))
850 if (is_utf8_string((U8*)s, send - s)) {
851 sv_setpvn(retval, s, send - s);
853 SvCUR_set(sv, s - start);
859 sv_setpvs(retval, "");
863 sv_setpvn(retval, s, 1);
870 sv_setpvs(retval, "");
878 const bool chomping = PL_op->op_type == OP_SCHOMP;
882 do_chomp(TARG, TOPs, chomping);
889 dVAR; dSP; dMARK; dTARGET; dORIGMARK;
890 const bool chomping = PL_op->op_type == OP_CHOMP;
895 do_chomp(TARG, *++MARK, chomping);
906 if (!PL_op->op_private) {
915 SV_CHECK_THINKFIRST_COW_DROP(sv);
917 switch (SvTYPE(sv)) {
921 av_undef(MUTABLE_AV(sv));
924 hv_undef(MUTABLE_HV(sv));
927 if (cv_const_sv((const CV *)sv))
928 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
929 "Constant subroutine %"SVf" undefined",
930 SVfARG(CvANON((const CV *)sv)
931 ? newSVpvs_flags("(anonymous)", SVs_TEMP)
932 : sv_2mortal(newSVhek(GvENAME_HEK(CvGV((const CV *)sv))))));
936 /* let user-undef'd sub keep its identity */
937 GV* const gv = CvGV((const CV *)sv);
938 cv_undef(MUTABLE_CV(sv));
939 CvGV_set(MUTABLE_CV(sv), gv);
944 SvSetMagicSV(sv, &PL_sv_undef);
947 else if (isGV_with_GP(sv)) {
951 /* undef *Pkg::meth_name ... */
953 = GvCVu((const GV *)sv) && (stash = GvSTASH((const GV *)sv))
954 && HvENAME_get(stash);
956 if((stash = GvHV((const GV *)sv))) {
957 if(HvENAME_get(stash))
958 SvREFCNT_inc_simple_void_NN(sv_2mortal((SV *)stash));
962 gp_free(MUTABLE_GV(sv));
964 GvGP_set(sv, gp_ref(gp));
966 GvLINE(sv) = CopLINE(PL_curcop);
967 GvEGV(sv) = MUTABLE_GV(sv);
971 mro_package_moved(NULL, stash, (const GV *)sv, 0);
973 /* undef *Foo::ISA */
974 if( strEQ(GvNAME((const GV *)sv), "ISA")
975 && (stash = GvSTASH((const GV *)sv))
976 && (method_changed || HvENAME(stash)) )
977 mro_isa_changed_in(stash);
978 else if(method_changed)
979 mro_method_changed_in(
980 GvSTASH((const GV *)sv)
987 if (SvTYPE(sv) >= SVt_PV && SvPVX_const(sv) && SvLEN(sv)) {
1003 PL_op->op_type == OP_POSTINC || PL_op->op_type == OP_I_POSTINC;
1004 if (SvTYPE(TOPs) >= SVt_PVAV || (isGV_with_GP(TOPs) && !SvFAKE(TOPs)))
1005 Perl_croak_no_modify(aTHX);
1007 TARG = sv_newmortal();
1008 sv_setsv(TARG, TOPs);
1009 if (!SvREADONLY(TOPs) && !SvGMAGICAL(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
1010 && SvIVX(TOPs) != (inc ? IV_MAX : IV_MIN))
1012 SvIV_set(TOPs, SvIVX(TOPs) + (inc ? 1 : -1));
1013 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
1017 else sv_dec_nomg(TOPs);
1019 /* special case for undef: see thread at 2003-03/msg00536.html in archive */
1020 if (inc && !SvOK(TARG))
1026 /* Ordinary operators. */
1030 dVAR; dSP; dATARGET; SV *svl, *svr;
1031 #ifdef PERL_PRESERVE_IVUV
1034 tryAMAGICbin_MG(pow_amg, AMGf_assign|AMGf_numeric);
1037 #ifdef PERL_PRESERVE_IVUV
1038 /* For integer to integer power, we do the calculation by hand wherever
1039 we're sure it is safe; otherwise we call pow() and try to convert to
1040 integer afterwards. */
1041 if (SvIV_please_nomg(svr) && SvIV_please_nomg(svl)) {
1049 const IV iv = SvIVX(svr);
1053 goto float_it; /* Can't do negative powers this way. */
1057 baseuok = SvUOK(svl);
1059 baseuv = SvUVX(svl);
1061 const IV iv = SvIVX(svl);
1064 baseuok = TRUE; /* effectively it's a UV now */
1066 baseuv = -iv; /* abs, baseuok == false records sign */
1069 /* now we have integer ** positive integer. */
1072 /* foo & (foo - 1) is zero only for a power of 2. */
1073 if (!(baseuv & (baseuv - 1))) {
1074 /* We are raising power-of-2 to a positive integer.
1075 The logic here will work for any base (even non-integer
1076 bases) but it can be less accurate than
1077 pow (base,power) or exp (power * log (base)) when the
1078 intermediate values start to spill out of the mantissa.
1079 With powers of 2 we know this can't happen.
1080 And powers of 2 are the favourite thing for perl
1081 programmers to notice ** not doing what they mean. */
1083 NV base = baseuok ? baseuv : -(NV)baseuv;
1088 while (power >>= 1) {
1096 SvIV_please_nomg(svr);
1099 unsigned int highbit = 8 * sizeof(UV);
1100 unsigned int diff = 8 * sizeof(UV);
1101 while (diff >>= 1) {
1103 if (baseuv >> highbit) {
1107 /* we now have baseuv < 2 ** highbit */
1108 if (power * highbit <= 8 * sizeof(UV)) {
1109 /* result will definitely fit in UV, so use UV math
1110 on same algorithm as above */
1113 const bool odd_power = cBOOL(power & 1);
1117 while (power >>= 1) {
1124 if (baseuok || !odd_power)
1125 /* answer is positive */
1127 else if (result <= (UV)IV_MAX)
1128 /* answer negative, fits in IV */
1129 SETi( -(IV)result );
1130 else if (result == (UV)IV_MIN)
1131 /* 2's complement assumption: special case IV_MIN */
1134 /* answer negative, doesn't fit */
1135 SETn( -(NV)result );
1143 NV right = SvNV_nomg(svr);
1144 NV left = SvNV_nomg(svl);
1147 #if defined(USE_LONG_DOUBLE) && defined(HAS_AIX_POWL_NEG_BASE_BUG)
1149 We are building perl with long double support and are on an AIX OS
1150 afflicted with a powl() function that wrongly returns NaNQ for any
1151 negative base. This was reported to IBM as PMR #23047-379 on
1152 03/06/2006. The problem exists in at least the following versions
1153 of AIX and the libm fileset, and no doubt others as well:
1155 AIX 4.3.3-ML10 bos.adt.libm 4.3.3.50
1156 AIX 5.1.0-ML04 bos.adt.libm 5.1.0.29
1157 AIX 5.2.0 bos.adt.libm 5.2.0.85
1159 So, until IBM fixes powl(), we provide the following workaround to
1160 handle the problem ourselves. Our logic is as follows: for
1161 negative bases (left), we use fmod(right, 2) to check if the
1162 exponent is an odd or even integer:
1164 - if odd, powl(left, right) == -powl(-left, right)
1165 - if even, powl(left, right) == powl(-left, right)
1167 If the exponent is not an integer, the result is rightly NaNQ, so
1168 we just return that (as NV_NAN).
1172 NV mod2 = Perl_fmod( right, 2.0 );
1173 if (mod2 == 1.0 || mod2 == -1.0) { /* odd integer */
1174 SETn( -Perl_pow( -left, right) );
1175 } else if (mod2 == 0.0) { /* even integer */
1176 SETn( Perl_pow( -left, right) );
1177 } else { /* fractional power */
1181 SETn( Perl_pow( left, right) );
1184 SETn( Perl_pow( left, right) );
1185 #endif /* HAS_AIX_POWL_NEG_BASE_BUG */
1187 #ifdef PERL_PRESERVE_IVUV
1189 SvIV_please_nomg(svr);
1197 dVAR; dSP; dATARGET; SV *svl, *svr;
1198 tryAMAGICbin_MG(mult_amg, AMGf_assign|AMGf_numeric);
1201 #ifdef PERL_PRESERVE_IVUV
1202 if (SvIV_please_nomg(svr)) {
1203 /* Unless the left argument is integer in range we are going to have to
1204 use NV maths. Hence only attempt to coerce the right argument if
1205 we know the left is integer. */
1206 /* Left operand is defined, so is it IV? */
1207 if (SvIV_please_nomg(svl)) {
1208 bool auvok = SvUOK(svl);
1209 bool buvok = SvUOK(svr);
1210 const UV topmask = (~ (UV)0) << (4 * sizeof (UV));
1211 const UV botmask = ~((~ (UV)0) << (4 * sizeof (UV)));
1220 const IV aiv = SvIVX(svl);
1223 auvok = TRUE; /* effectively it's a UV now */
1225 alow = -aiv; /* abs, auvok == false records sign */
1231 const IV biv = SvIVX(svr);
1234 buvok = TRUE; /* effectively it's a UV now */
1236 blow = -biv; /* abs, buvok == false records sign */
1240 /* If this does sign extension on unsigned it's time for plan B */
1241 ahigh = alow >> (4 * sizeof (UV));
1243 bhigh = blow >> (4 * sizeof (UV));
1245 if (ahigh && bhigh) {
1247 /* eg 32 bit is at least 0x10000 * 0x10000 == 0x100000000
1248 which is overflow. Drop to NVs below. */
1249 } else if (!ahigh && !bhigh) {
1250 /* eg 32 bit is at most 0xFFFF * 0xFFFF == 0xFFFE0001
1251 so the unsigned multiply cannot overflow. */
1252 const UV product = alow * blow;
1253 if (auvok == buvok) {
1254 /* -ve * -ve or +ve * +ve gives a +ve result. */
1258 } else if (product <= (UV)IV_MIN) {
1259 /* 2s complement assumption that (UV)-IV_MIN is correct. */
1260 /* -ve result, which could overflow an IV */
1262 SETi( -(IV)product );
1264 } /* else drop to NVs below. */
1266 /* One operand is large, 1 small */
1269 /* swap the operands */
1271 bhigh = blow; /* bhigh now the temp var for the swap */
1275 /* now, ((ahigh * blow) << half_UV_len) + (alow * blow)
1276 multiplies can't overflow. shift can, add can, -ve can. */
1277 product_middle = ahigh * blow;
1278 if (!(product_middle & topmask)) {
1279 /* OK, (ahigh * blow) won't lose bits when we shift it. */
1281 product_middle <<= (4 * sizeof (UV));
1282 product_low = alow * blow;
1284 /* as for pp_add, UV + something mustn't get smaller.
1285 IIRC ANSI mandates this wrapping *behaviour* for
1286 unsigned whatever the actual representation*/
1287 product_low += product_middle;
1288 if (product_low >= product_middle) {
1289 /* didn't overflow */
1290 if (auvok == buvok) {
1291 /* -ve * -ve or +ve * +ve gives a +ve result. */
1293 SETu( product_low );
1295 } else if (product_low <= (UV)IV_MIN) {
1296 /* 2s complement assumption again */
1297 /* -ve result, which could overflow an IV */
1299 SETi( -(IV)product_low );
1301 } /* else drop to NVs below. */
1303 } /* product_middle too large */
1304 } /* ahigh && bhigh */
1309 NV right = SvNV_nomg(svr);
1310 NV left = SvNV_nomg(svl);
1312 SETn( left * right );
1319 dVAR; dSP; dATARGET; SV *svl, *svr;
1320 tryAMAGICbin_MG(div_amg, AMGf_assign|AMGf_numeric);
1323 /* Only try to do UV divide first
1324 if ((SLOPPYDIVIDE is true) or
1325 (PERL_PRESERVE_IVUV is true and one or both SV is a UV too large
1327 The assumption is that it is better to use floating point divide
1328 whenever possible, only doing integer divide first if we can't be sure.
1329 If NV_PRESERVES_UV is true then we know at compile time that no UV
1330 can be too large to preserve, so don't need to compile the code to
1331 test the size of UVs. */
1334 # define PERL_TRY_UV_DIVIDE
1335 /* ensure that 20./5. == 4. */
1337 # ifdef PERL_PRESERVE_IVUV
1338 # ifndef NV_PRESERVES_UV
1339 # define PERL_TRY_UV_DIVIDE
1344 #ifdef PERL_TRY_UV_DIVIDE
1345 if (SvIV_please_nomg(svr) && SvIV_please_nomg(svl)) {
1346 bool left_non_neg = SvUOK(svl);
1347 bool right_non_neg = SvUOK(svr);
1351 if (right_non_neg) {
1355 const IV biv = SvIVX(svr);
1358 right_non_neg = TRUE; /* effectively it's a UV now */
1364 /* historically undef()/0 gives a "Use of uninitialized value"
1365 warning before dieing, hence this test goes here.
1366 If it were immediately before the second SvIV_please, then
1367 DIE() would be invoked before left was even inspected, so
1368 no inspection would give no warning. */
1370 DIE(aTHX_ "Illegal division by zero");
1376 const IV aiv = SvIVX(svl);
1379 left_non_neg = TRUE; /* effectively it's a UV now */
1388 /* For sloppy divide we always attempt integer division. */
1390 /* Otherwise we only attempt it if either or both operands
1391 would not be preserved by an NV. If both fit in NVs
1392 we fall through to the NV divide code below. However,
1393 as left >= right to ensure integer result here, we know that
1394 we can skip the test on the right operand - right big
1395 enough not to be preserved can't get here unless left is
1398 && (left > ((UV)1 << NV_PRESERVES_UV_BITS))
1401 /* Integer division can't overflow, but it can be imprecise. */
1402 const UV result = left / right;
1403 if (result * right == left) {
1404 SP--; /* result is valid */
1405 if (left_non_neg == right_non_neg) {
1406 /* signs identical, result is positive. */
1410 /* 2s complement assumption */
1411 if (result <= (UV)IV_MIN)
1412 SETi( -(IV)result );
1414 /* It's exact but too negative for IV. */
1415 SETn( -(NV)result );
1418 } /* tried integer divide but it was not an integer result */
1419 } /* else (PERL_ABS(result) < 1.0) or (both UVs in range for NV) */
1420 } /* one operand wasn't SvIOK */
1421 #endif /* PERL_TRY_UV_DIVIDE */
1423 NV right = SvNV_nomg(svr);
1424 NV left = SvNV_nomg(svl);
1425 (void)POPs;(void)POPs;
1426 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
1427 if (! Perl_isnan(right) && right == 0.0)
1431 DIE(aTHX_ "Illegal division by zero");
1432 PUSHn( left / right );
1439 dVAR; dSP; dATARGET;
1440 tryAMAGICbin_MG(modulo_amg, AMGf_assign|AMGf_numeric);
1444 bool left_neg = FALSE;
1445 bool right_neg = FALSE;
1446 bool use_double = FALSE;
1447 bool dright_valid = FALSE;
1450 SV * const svr = TOPs;
1451 SV * const svl = TOPm1s;
1452 if (SvIV_please_nomg(svr)) {
1453 right_neg = !SvUOK(svr);
1457 const IV biv = SvIVX(svr);
1460 right_neg = FALSE; /* effectively it's a UV now */
1467 dright = SvNV_nomg(svr);
1468 right_neg = dright < 0;
1471 if (dright < UV_MAX_P1) {
1472 right = U_V(dright);
1473 dright_valid = TRUE; /* In case we need to use double below. */
1479 /* At this point use_double is only true if right is out of range for
1480 a UV. In range NV has been rounded down to nearest UV and
1481 use_double false. */
1482 if (!use_double && SvIV_please_nomg(svl)) {
1483 left_neg = !SvUOK(svl);
1487 const IV aiv = SvIVX(svl);
1490 left_neg = FALSE; /* effectively it's a UV now */
1497 dleft = SvNV_nomg(svl);
1498 left_neg = dleft < 0;
1502 /* This should be exactly the 5.6 behaviour - if left and right are
1503 both in range for UV then use U_V() rather than floor. */
1505 if (dleft < UV_MAX_P1) {
1506 /* right was in range, so is dleft, so use UVs not double.
1510 /* left is out of range for UV, right was in range, so promote
1511 right (back) to double. */
1513 /* The +0.5 is used in 5.6 even though it is not strictly
1514 consistent with the implicit +0 floor in the U_V()
1515 inside the #if 1. */
1516 dleft = Perl_floor(dleft + 0.5);
1519 dright = Perl_floor(dright + 0.5);
1530 DIE(aTHX_ "Illegal modulus zero");
1532 dans = Perl_fmod(dleft, dright);
1533 if ((left_neg != right_neg) && dans)
1534 dans = dright - dans;
1537 sv_setnv(TARG, dans);
1543 DIE(aTHX_ "Illegal modulus zero");
1546 if ((left_neg != right_neg) && ans)
1549 /* XXX may warn: unary minus operator applied to unsigned type */
1550 /* could change -foo to be (~foo)+1 instead */
1551 if (ans <= ~((UV)IV_MAX)+1)
1552 sv_setiv(TARG, ~ans+1);
1554 sv_setnv(TARG, -(NV)ans);
1557 sv_setuv(TARG, ans);
1566 dVAR; dSP; dATARGET;
1570 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1571 /* TODO: think of some way of doing list-repeat overloading ??? */
1576 tryAMAGICbin_MG(repeat_amg, AMGf_assign);
1582 const UV uv = SvUV_nomg(sv);
1584 count = IV_MAX; /* The best we can do? */
1588 const IV iv = SvIV_nomg(sv);
1595 else if (SvNOKp(sv)) {
1596 const NV nv = SvNV_nomg(sv);
1603 count = SvIV_nomg(sv);
1605 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1607 static const char oom_list_extend[] = "Out of memory during list extend";
1608 const I32 items = SP - MARK;
1609 const I32 max = items * count;
1611 MEM_WRAP_CHECK_1(max, SV*, oom_list_extend);
1612 /* Did the max computation overflow? */
1613 if (items > 0 && max > 0 && (max < items || max < count))
1614 Perl_croak(aTHX_ oom_list_extend);
1619 /* This code was intended to fix 20010809.028:
1622 for (($x =~ /./g) x 2) {
1623 print chop; # "abcdabcd" expected as output.
1626 * but that change (#11635) broke this code:
1628 $x = [("foo")x2]; # only one "foo" ended up in the anonlist.
1630 * I can't think of a better fix that doesn't introduce
1631 * an efficiency hit by copying the SVs. The stack isn't
1632 * refcounted, and mortalisation obviously doesn't
1633 * Do The Right Thing when the stack has more than
1634 * one pointer to the same mortal value.
1638 *SP = sv_2mortal(newSVsv(*SP));
1648 repeatcpy((char*)(MARK + items), (char*)MARK,
1649 items * sizeof(const SV *), count - 1);
1652 else if (count <= 0)
1655 else { /* Note: mark already snarfed by pp_list */
1656 SV * const tmpstr = POPs;
1659 static const char oom_string_extend[] =
1660 "Out of memory during string extend";
1663 sv_setsv_nomg(TARG, tmpstr);
1664 SvPV_force_nomg(TARG, len);
1665 isutf = DO_UTF8(TARG);
1670 const STRLEN max = (UV)count * len;
1671 if (len > MEM_SIZE_MAX / count)
1672 Perl_croak(aTHX_ oom_string_extend);
1673 MEM_WRAP_CHECK_1(max, char, oom_string_extend);
1674 SvGROW(TARG, max + 1);
1675 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
1676 SvCUR_set(TARG, SvCUR(TARG) * count);
1678 *SvEND(TARG) = '\0';
1681 (void)SvPOK_only_UTF8(TARG);
1683 (void)SvPOK_only(TARG);
1685 if (PL_op->op_private & OPpREPEAT_DOLIST) {
1686 /* The parser saw this as a list repeat, and there
1687 are probably several items on the stack. But we're
1688 in scalar context, and there's no pp_list to save us
1689 now. So drop the rest of the items -- robin@kitsite.com
1701 dVAR; dSP; dATARGET; bool useleft; SV *svl, *svr;
1702 tryAMAGICbin_MG(subtr_amg, AMGf_assign|AMGf_numeric);
1705 useleft = USE_LEFT(svl);
1706 #ifdef PERL_PRESERVE_IVUV
1707 /* See comments in pp_add (in pp_hot.c) about Overflow, and how
1708 "bad things" happen if you rely on signed integers wrapping. */
1709 if (SvIV_please_nomg(svr)) {
1710 /* Unless the left argument is integer in range we are going to have to
1711 use NV maths. Hence only attempt to coerce the right argument if
1712 we know the left is integer. */
1719 a_valid = auvok = 1;
1720 /* left operand is undef, treat as zero. */
1722 /* Left operand is defined, so is it IV? */
1723 if (SvIV_please_nomg(svl)) {
1724 if ((auvok = SvUOK(svl)))
1727 const IV aiv = SvIVX(svl);
1730 auvok = 1; /* Now acting as a sign flag. */
1731 } else { /* 2s complement assumption for IV_MIN */
1739 bool result_good = 0;
1742 bool buvok = SvUOK(svr);
1747 const IV biv = SvIVX(svr);
1754 /* ?uvok if value is >= 0. basically, flagged as UV if it's +ve,
1755 else "IV" now, independent of how it came in.
1756 if a, b represents positive, A, B negative, a maps to -A etc
1761 all UV maths. negate result if A negative.
1762 subtract if signs same, add if signs differ. */
1764 if (auvok ^ buvok) {
1773 /* Must get smaller */
1778 if (result <= buv) {
1779 /* result really should be -(auv-buv). as its negation
1780 of true value, need to swap our result flag */
1792 if (result <= (UV)IV_MIN)
1793 SETi( -(IV)result );
1795 /* result valid, but out of range for IV. */
1796 SETn( -(NV)result );
1800 } /* Overflow, drop through to NVs. */
1805 NV value = SvNV_nomg(svr);
1809 /* left operand is undef, treat as zero - value */
1813 SETn( SvNV_nomg(svl) - value );
1820 dVAR; dSP; dATARGET; SV *svl, *svr;
1821 tryAMAGICbin_MG(lshift_amg, AMGf_assign|AMGf_numeric);
1825 const IV shift = SvIV_nomg(svr);
1826 if (PL_op->op_private & HINT_INTEGER) {
1827 const IV i = SvIV_nomg(svl);
1831 const UV u = SvUV_nomg(svl);
1840 dVAR; dSP; dATARGET; SV *svl, *svr;
1841 tryAMAGICbin_MG(rshift_amg, AMGf_assign|AMGf_numeric);
1845 const IV shift = SvIV_nomg(svr);
1846 if (PL_op->op_private & HINT_INTEGER) {
1847 const IV i = SvIV_nomg(svl);
1851 const UV u = SvUV_nomg(svl);
1863 tryAMAGICbin_MG(lt_amg, AMGf_set|AMGf_numeric);
1867 (SvIOK_notUV(left) && SvIOK_notUV(right))
1868 ? (SvIVX(left) < SvIVX(right))
1869 : (do_ncmp(left, right) == -1)
1879 tryAMAGICbin_MG(gt_amg, AMGf_set|AMGf_numeric);
1883 (SvIOK_notUV(left) && SvIOK_notUV(right))
1884 ? (SvIVX(left) > SvIVX(right))
1885 : (do_ncmp(left, right) == 1)
1895 tryAMAGICbin_MG(le_amg, AMGf_set|AMGf_numeric);
1899 (SvIOK_notUV(left) && SvIOK_notUV(right))
1900 ? (SvIVX(left) <= SvIVX(right))
1901 : (do_ncmp(left, right) <= 0)
1911 tryAMAGICbin_MG(ge_amg, AMGf_set|AMGf_numeric);
1915 (SvIOK_notUV(left) && SvIOK_notUV(right))
1916 ? (SvIVX(left) >= SvIVX(right))
1917 : ( (do_ncmp(left, right) & 2) == 0)
1927 tryAMAGICbin_MG(ne_amg, AMGf_set|AMGf_numeric);
1931 (SvIOK_notUV(left) && SvIOK_notUV(right))
1932 ? (SvIVX(left) != SvIVX(right))
1933 : (do_ncmp(left, right) != 0)
1938 /* compare left and right SVs. Returns:
1942 * 2: left or right was a NaN
1945 Perl_do_ncmp(pTHX_ SV* const left, SV * const right)
1949 PERL_ARGS_ASSERT_DO_NCMP;
1950 #ifdef PERL_PRESERVE_IVUV
1951 /* Fortunately it seems NaN isn't IOK */
1952 if (SvIV_please_nomg(right) && SvIV_please_nomg(left)) {
1954 const IV leftiv = SvIVX(left);
1955 if (!SvUOK(right)) {
1956 /* ## IV <=> IV ## */
1957 const IV rightiv = SvIVX(right);
1958 return (leftiv > rightiv) - (leftiv < rightiv);
1960 /* ## IV <=> UV ## */
1962 /* As (b) is a UV, it's >=0, so it must be < */
1965 const UV rightuv = SvUVX(right);
1966 return ((UV)leftiv > rightuv) - ((UV)leftiv < rightuv);
1971 /* ## UV <=> UV ## */
1972 const UV leftuv = SvUVX(left);
1973 const UV rightuv = SvUVX(right);
1974 return (leftuv > rightuv) - (leftuv < rightuv);
1976 /* ## UV <=> IV ## */
1978 const IV rightiv = SvIVX(right);
1980 /* As (a) is a UV, it's >=0, so it cannot be < */
1983 const UV leftuv = SvUVX(left);
1984 return (leftuv > (UV)rightiv) - (leftuv < (UV)rightiv);
1987 assert(0); /* NOTREACHED */
1991 NV const rnv = SvNV_nomg(right);
1992 NV const lnv = SvNV_nomg(left);
1994 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
1995 if (Perl_isnan(lnv) || Perl_isnan(rnv)) {
1998 return (lnv > rnv) - (lnv < rnv);
2017 tryAMAGICbin_MG(ncmp_amg, AMGf_numeric);
2020 value = do_ncmp(left, right);
2035 int amg_type = sle_amg;
2039 switch (PL_op->op_type) {
2058 tryAMAGICbin_MG(amg_type, AMGf_set);
2061 const int cmp = (IN_LOCALE_RUNTIME
2062 ? sv_cmp_locale_flags(left, right, 0)
2063 : sv_cmp_flags(left, right, 0));
2064 SETs(boolSV(cmp * multiplier < rhs));
2072 tryAMAGICbin_MG(seq_amg, AMGf_set);
2075 SETs(boolSV(sv_eq_flags(left, right, 0)));
2083 tryAMAGICbin_MG(sne_amg, AMGf_set);
2086 SETs(boolSV(!sv_eq_flags(left, right, 0)));
2094 tryAMAGICbin_MG(scmp_amg, 0);
2097 const int cmp = (IN_LOCALE_RUNTIME
2098 ? sv_cmp_locale_flags(left, right, 0)
2099 : sv_cmp_flags(left, right, 0));
2107 dVAR; dSP; dATARGET;
2108 tryAMAGICbin_MG(band_amg, AMGf_assign);
2111 if (SvNIOKp(left) || SvNIOKp(right)) {
2112 const bool left_ro_nonnum = !SvNIOKp(left) && SvREADONLY(left);
2113 const bool right_ro_nonnum = !SvNIOKp(right) && SvREADONLY(right);
2114 if (PL_op->op_private & HINT_INTEGER) {
2115 const IV i = SvIV_nomg(left) & SvIV_nomg(right);
2119 const UV u = SvUV_nomg(left) & SvUV_nomg(right);
2122 if (left_ro_nonnum && left != TARG) SvNIOK_off(left);
2123 if (right_ro_nonnum) SvNIOK_off(right);
2126 do_vop(PL_op->op_type, TARG, left, right);
2135 dVAR; dSP; dATARGET;
2136 const int op_type = PL_op->op_type;
2138 tryAMAGICbin_MG((op_type == OP_BIT_OR ? bor_amg : bxor_amg), AMGf_assign);
2141 if (SvNIOKp(left) || SvNIOKp(right)) {
2142 const bool left_ro_nonnum = !SvNIOKp(left) && SvREADONLY(left);
2143 const bool right_ro_nonnum = !SvNIOKp(right) && SvREADONLY(right);
2144 if (PL_op->op_private & HINT_INTEGER) {
2145 const IV l = (USE_LEFT(left) ? SvIV_nomg(left) : 0);
2146 const IV r = SvIV_nomg(right);
2147 const IV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2151 const UV l = (USE_LEFT(left) ? SvUV_nomg(left) : 0);
2152 const UV r = SvUV_nomg(right);
2153 const UV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2156 if (left_ro_nonnum && left != TARG) SvNIOK_off(left);
2157 if (right_ro_nonnum) SvNIOK_off(right);
2160 do_vop(op_type, TARG, left, right);
2167 PERL_STATIC_INLINE bool
2168 S_negate_string(pTHX)
2173 SV * const sv = TOPs;
2174 if (!SvPOKp(sv) || SvNIOK(sv) || (!SvPOK(sv) && SvNIOKp(sv)))
2176 s = SvPV_nomg_const(sv, len);
2177 if (isIDFIRST(*s)) {
2178 sv_setpvs(TARG, "-");
2181 else if (*s == '+' || (*s == '-' && !looks_like_number(sv))) {
2182 sv_setsv_nomg(TARG, sv);
2183 *SvPV_force_nomg(TARG, len) = *s == '-' ? '+' : '-';
2193 tryAMAGICun_MG(neg_amg, AMGf_numeric);
2194 if (S_negate_string(aTHX)) return NORMAL;
2196 SV * const sv = TOPs;
2199 /* It's publicly an integer */
2202 if (SvIVX(sv) == IV_MIN) {
2203 /* 2s complement assumption. */
2204 SETi(SvIVX(sv)); /* special case: -((UV)IV_MAX+1) == IV_MIN */
2207 else if (SvUVX(sv) <= IV_MAX) {
2212 else if (SvIVX(sv) != IV_MIN) {
2216 #ifdef PERL_PRESERVE_IVUV
2223 if (SvNIOKp(sv) && (SvNIOK(sv) || !SvPOK(sv)))
2224 SETn(-SvNV_nomg(sv));
2225 else if (SvPOKp(sv) && SvIV_please_nomg(sv))
2226 goto oops_its_an_int;
2228 SETn(-SvNV_nomg(sv));
2236 tryAMAGICun_MG(not_amg, AMGf_set);
2237 *PL_stack_sp = boolSV(!SvTRUE_nomg(*PL_stack_sp));
2244 tryAMAGICun_MG(compl_amg, AMGf_numeric);
2248 if (PL_op->op_private & HINT_INTEGER) {
2249 const IV i = ~SvIV_nomg(sv);
2253 const UV u = ~SvUV_nomg(sv);
2262 (void)SvPV_nomg_const(sv,len); /* force check for uninit var */
2263 sv_setsv_nomg(TARG, sv);
2264 tmps = (U8*)SvPV_force_nomg(TARG, len);
2267 /* Calculate exact length, let's not estimate. */
2272 U8 * const send = tmps + len;
2273 U8 * const origtmps = tmps;
2274 const UV utf8flags = UTF8_ALLOW_ANYUV;
2276 while (tmps < send) {
2277 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2279 targlen += UNISKIP(~c);
2285 /* Now rewind strings and write them. */
2292 Newx(result, targlen + 1, U8);
2294 while (tmps < send) {
2295 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2297 p = uvchr_to_utf8_flags(p, ~c, UNICODE_ALLOW_ANY);
2300 sv_usepvn_flags(TARG, (char*)result, targlen,
2301 SV_HAS_TRAILING_NUL);
2308 Newx(result, nchar + 1, U8);
2310 while (tmps < send) {
2311 const U8 c = (U8)utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2316 sv_usepvn_flags(TARG, (char*)result, nchar, SV_HAS_TRAILING_NUL);
2325 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
2328 for ( ; anum >= (I32)sizeof(long); anum -= (I32)sizeof(long), tmpl++)
2333 for ( ; anum > 0; anum--, tmps++)
2341 /* integer versions of some of the above */
2345 dVAR; dSP; dATARGET;
2346 tryAMAGICbin_MG(mult_amg, AMGf_assign);
2349 SETi( left * right );
2357 dVAR; dSP; dATARGET;
2358 tryAMAGICbin_MG(div_amg, AMGf_assign);
2361 IV value = SvIV_nomg(right);
2363 DIE(aTHX_ "Illegal division by zero");
2364 num = SvIV_nomg(left);
2366 /* avoid FPE_INTOVF on some platforms when num is IV_MIN */
2370 value = num / value;
2376 #if defined(__GLIBC__) && IVSIZE == 8 && !defined(PERL_DEBUG_READONLY_OPS)
2383 /* This is the vanilla old i_modulo. */
2384 dVAR; dSP; dATARGET;
2385 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2389 DIE(aTHX_ "Illegal modulus zero");
2390 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2394 SETi( left % right );
2399 #if defined(__GLIBC__) && IVSIZE == 8 && !defined(PERL_DEBUG_READONLY_OPS)
2404 /* This is the i_modulo with the workaround for the _moddi3 bug
2405 * in (at least) glibc 2.2.5 (the PERL_ABS() the workaround).
2406 * See below for pp_i_modulo. */
2407 dVAR; dSP; dATARGET;
2408 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2412 DIE(aTHX_ "Illegal modulus zero");
2413 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2417 SETi( left % PERL_ABS(right) );
2424 dVAR; dSP; dATARGET;
2425 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2429 DIE(aTHX_ "Illegal modulus zero");
2430 /* The assumption is to use hereafter the old vanilla version... */
2432 PL_ppaddr[OP_I_MODULO] =
2434 /* .. but if we have glibc, we might have a buggy _moddi3
2435 * (at least glicb 2.2.5 is known to have this bug), in other
2436 * words our integer modulus with negative quad as the second
2437 * argument might be broken. Test for this and re-patch the
2438 * opcode dispatch table if that is the case, remembering to
2439 * also apply the workaround so that this first round works
2440 * right, too. See [perl #9402] for more information. */
2444 /* Cannot do this check with inlined IV constants since
2445 * that seems to work correctly even with the buggy glibc. */
2447 /* Yikes, we have the bug.
2448 * Patch in the workaround version. */
2450 PL_ppaddr[OP_I_MODULO] =
2451 &Perl_pp_i_modulo_1;
2452 /* Make certain we work right this time, too. */
2453 right = PERL_ABS(right);
2456 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2460 SETi( left % right );
2468 dVAR; dSP; dATARGET;
2469 tryAMAGICbin_MG(add_amg, AMGf_assign);
2471 dPOPTOPiirl_ul_nomg;
2472 SETi( left + right );
2479 dVAR; dSP; dATARGET;
2480 tryAMAGICbin_MG(subtr_amg, AMGf_assign);
2482 dPOPTOPiirl_ul_nomg;
2483 SETi( left - right );
2491 tryAMAGICbin_MG(lt_amg, AMGf_set);
2494 SETs(boolSV(left < right));
2502 tryAMAGICbin_MG(gt_amg, AMGf_set);
2505 SETs(boolSV(left > right));
2513 tryAMAGICbin_MG(le_amg, AMGf_set);
2516 SETs(boolSV(left <= right));
2524 tryAMAGICbin_MG(ge_amg, AMGf_set);
2527 SETs(boolSV(left >= right));
2535 tryAMAGICbin_MG(eq_amg, AMGf_set);
2538 SETs(boolSV(left == right));
2546 tryAMAGICbin_MG(ne_amg, AMGf_set);
2549 SETs(boolSV(left != right));
2557 tryAMAGICbin_MG(ncmp_amg, 0);
2564 else if (left < right)
2576 tryAMAGICun_MG(neg_amg, 0);
2577 if (S_negate_string(aTHX)) return NORMAL;
2579 SV * const sv = TOPs;
2580 IV const i = SvIV_nomg(sv);
2586 /* High falutin' math. */
2591 tryAMAGICbin_MG(atan2_amg, 0);
2594 SETn(Perl_atan2(left, right));
2602 int amg_type = sin_amg;
2603 const char *neg_report = NULL;
2604 NV (*func)(NV) = Perl_sin;
2605 const int op_type = PL_op->op_type;
2622 amg_type = sqrt_amg;
2624 neg_report = "sqrt";
2629 tryAMAGICun_MG(amg_type, 0);
2631 SV * const arg = POPs;
2632 const NV value = SvNV_nomg(arg);
2634 if (op_type == OP_LOG ? (value <= 0.0) : (value < 0.0)) {
2635 SET_NUMERIC_STANDARD();
2636 /* diag_listed_as: Can't take log of %g */
2637 DIE(aTHX_ "Can't take %s of %"NVgf, neg_report, value);
2640 XPUSHn(func(value));
2645 /* Support Configure command-line overrides for rand() functions.
2646 After 5.005, perhaps we should replace this by Configure support
2647 for drand48(), random(), or rand(). For 5.005, though, maintain
2648 compatibility by calling rand() but allow the user to override it.
2649 See INSTALL for details. --Andy Dougherty 15 July 1998
2651 /* Now it's after 5.005, and Configure supports drand48() and random(),
2652 in addition to rand(). So the overrides should not be needed any more.
2653 --Jarkko Hietaniemi 27 September 1998
2656 #ifndef HAS_DRAND48_PROTO
2657 extern double drand48 (void);
2667 value = 1.0; (void)POPs;
2673 if (!PL_srand_called) {
2674 (void)seedDrand01((Rand_seed_t)seed());
2675 PL_srand_called = TRUE;
2687 if (MAXARG >= 1 && (TOPs || POPs)) {
2694 pv = SvPV(top, len);
2695 flags = grok_number(pv, len, &anum);
2697 if (!(flags & IS_NUMBER_IN_UV)) {
2698 Perl_ck_warner_d(aTHX_ packWARN(WARN_OVERFLOW),
2699 "Integer overflow in srand");
2707 (void)seedDrand01((Rand_seed_t)anum);
2708 PL_srand_called = TRUE;
2712 /* Historically srand always returned true. We can avoid breaking
2714 sv_setpvs(TARG, "0 but true");
2723 tryAMAGICun_MG(int_amg, AMGf_numeric);
2725 SV * const sv = TOPs;
2726 const IV iv = SvIV_nomg(sv);
2727 /* XXX it's arguable that compiler casting to IV might be subtly
2728 different from modf (for numbers inside (IV_MIN,UV_MAX)) in which
2729 else preferring IV has introduced a subtle behaviour change bug. OTOH
2730 relying on floating point to be accurate is a bug. */
2735 else if (SvIOK(sv)) {
2737 SETu(SvUV_nomg(sv));
2742 const NV value = SvNV_nomg(sv);
2744 if (value < (NV)UV_MAX + 0.5) {
2747 SETn(Perl_floor(value));
2751 if (value > (NV)IV_MIN - 0.5) {
2754 SETn(Perl_ceil(value));
2765 tryAMAGICun_MG(abs_amg, AMGf_numeric);
2767 SV * const sv = TOPs;
2768 /* This will cache the NV value if string isn't actually integer */
2769 const IV iv = SvIV_nomg(sv);
2774 else if (SvIOK(sv)) {
2775 /* IVX is precise */
2777 SETu(SvUV_nomg(sv)); /* force it to be numeric only */
2785 /* 2s complement assumption. Also, not really needed as
2786 IV_MIN and -IV_MIN should both be %100...00 and NV-able */
2792 const NV value = SvNV_nomg(sv);
2806 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES;
2810 SV* const sv = POPs;
2812 tmps = (SvPV_const(sv, len));
2814 /* If Unicode, try to downgrade
2815 * If not possible, croak. */
2816 SV* const tsv = sv_2mortal(newSVsv(sv));
2819 sv_utf8_downgrade(tsv, FALSE);
2820 tmps = SvPV_const(tsv, len);
2822 if (PL_op->op_type == OP_HEX)
2825 while (*tmps && len && isSPACE(*tmps))
2829 if (*tmps == 'x' || *tmps == 'X') {
2831 result_uv = grok_hex (tmps, &len, &flags, &result_nv);
2833 else if (*tmps == 'b' || *tmps == 'B')
2834 result_uv = grok_bin (tmps, &len, &flags, &result_nv);
2836 result_uv = grok_oct (tmps, &len, &flags, &result_nv);
2838 if (flags & PERL_SCAN_GREATER_THAN_UV_MAX) {
2852 SV * const sv = TOPs;
2854 if (SvGAMAGIC(sv)) {
2855 /* For an overloaded or magic scalar, we can't know in advance if
2856 it's going to be UTF-8 or not. Also, we can't call sv_len_utf8 as
2857 it likes to cache the length. Maybe that should be a documented
2862 = sv_2pv_flags(sv, &len,
2863 SV_UNDEF_RETURNS_NULL|SV_CONST_RETURN|SV_GMAGIC);
2866 if (!SvPADTMP(TARG)) {
2867 sv_setsv(TARG, &PL_sv_undef);
2872 else if (DO_UTF8(sv)) {
2873 SETi(utf8_length((U8*)p, (U8*)p + len));
2877 } else if (SvOK(sv)) {
2878 /* Neither magic nor overloaded. */
2880 SETi(sv_len_utf8(sv));
2884 if (!SvPADTMP(TARG)) {
2885 sv_setsv_nomg(TARG, &PL_sv_undef);
2893 /* Returns false if substring is completely outside original string.
2894 No length is indicated by len_iv = 0 and len_is_uv = 0. len_is_uv must
2895 always be true for an explicit 0.
2898 Perl_translate_substr_offsets(pTHX_ STRLEN curlen, IV pos1_iv,
2899 bool pos1_is_uv, IV len_iv,
2900 bool len_is_uv, STRLEN *posp,
2906 PERL_ARGS_ASSERT_TRANSLATE_SUBSTR_OFFSETS;
2908 if (!pos1_is_uv && pos1_iv < 0 && curlen) {
2909 pos1_is_uv = curlen-1 > ~(UV)pos1_iv;
2912 if ((pos1_is_uv || pos1_iv > 0) && (UV)pos1_iv > curlen)
2915 if (len_iv || len_is_uv) {
2916 if (!len_is_uv && len_iv < 0) {
2917 pos2_iv = curlen + len_iv;
2919 pos2_is_uv = curlen-1 > ~(UV)len_iv;
2922 } else { /* len_iv >= 0 */
2923 if (!pos1_is_uv && pos1_iv < 0) {
2924 pos2_iv = pos1_iv + len_iv;
2925 pos2_is_uv = (UV)len_iv > (UV)IV_MAX;
2927 if ((UV)len_iv > curlen-(UV)pos1_iv)
2930 pos2_iv = pos1_iv+len_iv;
2940 if (!pos2_is_uv && pos2_iv < 0) {
2941 if (!pos1_is_uv && pos1_iv < 0)
2945 else if (!pos1_is_uv && pos1_iv < 0)
2948 if ((UV)pos2_iv < (UV)pos1_iv)
2950 if ((UV)pos2_iv > curlen)
2953 /* pos1_iv and pos2_iv both in 0..curlen, so the cast is safe */
2954 *posp = (STRLEN)( (UV)pos1_iv );
2955 *lenp = (STRLEN)( (UV)pos2_iv - (UV)pos1_iv );
2972 I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
2973 const bool rvalue = (GIMME_V != G_VOID);
2976 const char *repl = NULL;
2978 int num_args = PL_op->op_private & 7;
2979 bool repl_need_utf8_upgrade = FALSE;
2980 bool repl_is_utf8 = FALSE;
2984 if(!(repl_sv = POPs)) num_args--;
2986 if ((len_sv = POPs)) {
2987 len_iv = SvIV(len_sv);
2988 len_is_uv = len_iv ? SvIOK_UV(len_sv) : 1;
2993 pos1_iv = SvIV(pos_sv);
2994 pos1_is_uv = SvIOK_UV(pos_sv);
2996 if (PL_op->op_private & OPpSUBSTR_REPL_FIRST) {
3002 repl = SvPV_const(repl_sv, repl_len);
3003 repl_is_utf8 = DO_UTF8(repl_sv) && repl_len;
3006 sv_utf8_upgrade(sv);
3008 else if (DO_UTF8(sv))
3009 repl_need_utf8_upgrade = TRUE;
3013 ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
3014 sv_magic(ret, NULL, PERL_MAGIC_substr, NULL, 0);
3016 LvTARG(ret) = SvREFCNT_inc_simple(sv);
3018 pos1_is_uv || pos1_iv >= 0
3019 ? (STRLEN)(UV)pos1_iv
3020 : (LvFLAGS(ret) |= 1, (STRLEN)(UV)-pos1_iv);
3022 len_is_uv || len_iv > 0
3023 ? (STRLEN)(UV)len_iv
3024 : (LvFLAGS(ret) |= 2, (STRLEN)(UV)-len_iv);
3027 PUSHs(ret); /* avoid SvSETMAGIC here */
3030 tmps = SvPV_const(sv, curlen);
3032 utf8_curlen = sv_len_utf8_nomg(sv);
3033 if (utf8_curlen == curlen)
3036 curlen = utf8_curlen;
3042 STRLEN pos, len, byte_len, byte_pos;
3044 if (!translate_substr_offsets(
3045 curlen, pos1_iv, pos1_is_uv, len_iv, len_is_uv, &pos, &len
3049 byte_pos = utf8_curlen
3050 ? sv_pos_u2b_flags(sv, pos, &byte_len, SV_CONST_RETURN) : pos;
3055 SvTAINTED_off(TARG); /* decontaminate */
3056 SvUTF8_off(TARG); /* decontaminate */
3057 sv_setpvn(TARG, tmps, byte_len);
3058 #ifdef USE_LOCALE_COLLATE
3059 sv_unmagic(TARG, PERL_MAGIC_collxfrm);
3066 SV* repl_sv_copy = NULL;
3068 if (repl_need_utf8_upgrade) {
3069 repl_sv_copy = newSVsv(repl_sv);
3070 sv_utf8_upgrade(repl_sv_copy);
3071 repl = SvPV_const(repl_sv_copy, repl_len);
3072 repl_is_utf8 = DO_UTF8(repl_sv_copy) && repl_len;
3075 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR),
3076 "Attempt to use reference as lvalue in substr"
3080 sv_insert_flags(sv, byte_pos, byte_len, repl, repl_len, 0);
3083 SvREFCNT_dec(repl_sv_copy);
3095 Perl_croak(aTHX_ "substr outside of string");
3096 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR), "substr outside of string");
3103 const IV size = POPi;
3104 const IV offset = POPi;
3105 SV * const src = POPs;
3106 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
3109 if (lvalue) { /* it's an lvalue! */
3110 ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
3111 sv_magic(ret, NULL, PERL_MAGIC_vec, NULL, 0);
3113 LvTARG(ret) = SvREFCNT_inc_simple(src);
3114 LvTARGOFF(ret) = offset;
3115 LvTARGLEN(ret) = size;
3119 SvTAINTED_off(TARG); /* decontaminate */
3123 sv_setuv(ret, do_vecget(src, offset, size));
3139 const char *little_p;
3142 const bool is_index = PL_op->op_type == OP_INDEX;
3143 const bool threeargs = MAXARG >= 3 && (TOPs || ((void)POPs,0));
3149 big_p = SvPV_const(big, biglen);
3150 little_p = SvPV_const(little, llen);
3152 big_utf8 = DO_UTF8(big);
3153 little_utf8 = DO_UTF8(little);
3154 if (big_utf8 ^ little_utf8) {
3155 /* One needs to be upgraded. */
3156 if (little_utf8 && !PL_encoding) {
3157 /* Well, maybe instead we might be able to downgrade the small
3159 char * const pv = (char*)bytes_from_utf8((U8 *)little_p, &llen,
3162 /* If the large string is ISO-8859-1, and it's not possible to
3163 convert the small string to ISO-8859-1, then there is no
3164 way that it could be found anywhere by index. */
3169 /* At this point, pv is a malloc()ed string. So donate it to temp
3170 to ensure it will get free()d */
3171 little = temp = newSV(0);
3172 sv_usepvn(temp, pv, llen);
3173 little_p = SvPVX(little);
3176 ? newSVpvn(big_p, biglen) : newSVpvn(little_p, llen);
3179 sv_recode_to_utf8(temp, PL_encoding);
3181 sv_utf8_upgrade(temp);
3186 big_p = SvPV_const(big, biglen);
3189 little_p = SvPV_const(little, llen);
3193 if (SvGAMAGIC(big)) {
3194 /* Life just becomes a lot easier if I use a temporary here.
3195 Otherwise I need to avoid calls to sv_pos_u2b(), which (dangerously)
3196 will trigger magic and overloading again, as will fbm_instr()
3198 big = newSVpvn_flags(big_p, biglen,
3199 SVs_TEMP | (big_utf8 ? SVf_UTF8 : 0));
3202 if (SvGAMAGIC(little) || (is_index && !SvOK(little))) {
3203 /* index && SvOK() is a hack. fbm_instr() calls SvPV_const, which will
3204 warn on undef, and we've already triggered a warning with the
3205 SvPV_const some lines above. We can't remove that, as we need to
3206 call some SvPV to trigger overloading early and find out if the
3208 This is all getting to messy. The API isn't quite clean enough,
3209 because data access has side effects.
3211 little = newSVpvn_flags(little_p, llen,
3212 SVs_TEMP | (little_utf8 ? SVf_UTF8 : 0));
3213 little_p = SvPVX(little);
3217 offset = is_index ? 0 : biglen;
3219 if (big_utf8 && offset > 0)
3220 sv_pos_u2b(big, &offset, 0);
3226 else if (offset > (I32)biglen)
3228 if (!(little_p = is_index
3229 ? fbm_instr((unsigned char*)big_p + offset,
3230 (unsigned char*)big_p + biglen, little, 0)
3231 : rninstr(big_p, big_p + offset,
3232 little_p, little_p + llen)))
3235 retval = little_p - big_p;
3236 if (retval > 0 && big_utf8)
3237 sv_pos_b2u(big, &retval);
3247 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
3248 SvTAINTED_off(TARG);
3249 do_sprintf(TARG, SP-MARK, MARK+1);
3250 TAINT_IF(SvTAINTED(TARG));
3262 const U8 *s = (U8*)SvPV_const(argsv, len);
3264 if (PL_encoding && SvPOK(argsv) && !DO_UTF8(argsv)) {
3265 SV * const tmpsv = sv_2mortal(newSVsv(argsv));
3266 s = (U8*)sv_recode_to_utf8(tmpsv, PL_encoding);
3270 XPUSHu(DO_UTF8(argsv) ?
3271 utf8n_to_uvchr(s, UTF8_MAXBYTES, 0, UTF8_ALLOW_ANYUV) :
3285 if (!IN_BYTES /* under bytes, chr(-1) eq chr(0xff), etc. */
3286 && ((SvIOKp(top) && !SvIsUV(top) && SvIV_nomg(top) < 0)
3288 ((SvNOKp(top) || (SvOK(top) && !SvIsUV(top)))
3289 && SvNV_nomg(top) < 0.0))) {
3290 if (ckWARN(WARN_UTF8)) {
3291 if (SvGMAGICAL(top)) {
3292 SV *top2 = sv_newmortal();
3293 sv_setsv_nomg(top2, top);
3296 Perl_warner(aTHX_ packWARN(WARN_UTF8),
3297 "Invalid negative number (%"SVf") in chr", top);
3299 value = UNICODE_REPLACEMENT;
3301 value = SvUV_nomg(top);
3304 SvUPGRADE(TARG,SVt_PV);
3306 if (value > 255 && !IN_BYTES) {
3307 SvGROW(TARG, (STRLEN)UNISKIP(value)+1);
3308 tmps = (char*)uvchr_to_utf8_flags((U8*)SvPVX(TARG), value, 0);
3309 SvCUR_set(TARG, tmps - SvPVX_const(TARG));
3311 (void)SvPOK_only(TARG);
3320 *tmps++ = (char)value;
3322 (void)SvPOK_only(TARG);
3324 if (PL_encoding && !IN_BYTES) {
3325 sv_recode_to_utf8(TARG, PL_encoding);
3327 if (SvCUR(TARG) == 0
3328 || ! is_utf8_string((U8*)tmps, SvCUR(TARG))
3329 || UTF8_IS_REPLACEMENT((U8*) tmps, (U8*) tmps + SvCUR(TARG)))
3334 *tmps++ = (char)value;
3350 const char *tmps = SvPV_const(left, len);
3352 if (DO_UTF8(left)) {
3353 /* If Unicode, try to downgrade.
3354 * If not possible, croak.
3355 * Yes, we made this up. */
3356 SV* const tsv = sv_2mortal(newSVsv(left));
3359 sv_utf8_downgrade(tsv, FALSE);
3360 tmps = SvPV_const(tsv, len);
3362 # ifdef USE_ITHREADS
3364 if (!PL_reentrant_buffer->_crypt_struct_buffer) {
3365 /* This should be threadsafe because in ithreads there is only
3366 * one thread per interpreter. If this would not be true,
3367 * we would need a mutex to protect this malloc. */
3368 PL_reentrant_buffer->_crypt_struct_buffer =
3369 (struct crypt_data *)safemalloc(sizeof(struct crypt_data));
3370 #if defined(__GLIBC__) || defined(__EMX__)
3371 if (PL_reentrant_buffer->_crypt_struct_buffer) {
3372 PL_reentrant_buffer->_crypt_struct_buffer->initialized = 0;
3373 /* work around glibc-2.2.5 bug */
3374 PL_reentrant_buffer->_crypt_struct_buffer->current_saltbits = 0;
3378 # endif /* HAS_CRYPT_R */
3379 # endif /* USE_ITHREADS */
3381 sv_setpv(TARG, fcrypt(tmps, SvPV_nolen_const(right)));
3383 sv_setpv(TARG, PerlProc_crypt(tmps, SvPV_nolen_const(right)));
3389 "The crypt() function is unimplemented due to excessive paranoia.");
3393 /* Generally UTF-8 and UTF-EBCDIC are indistinguishable at this level. So
3394 * most comments below say UTF-8, when in fact they mean UTF-EBCDIC as well */
3396 /* Generates code to store a unicode codepoint c that is known to occupy
3397 * exactly two UTF-8 and UTF-EBCDIC bytes; it is stored into p and p+1,
3398 * and p is advanced to point to the next available byte after the two bytes */
3399 #define CAT_UNI_TO_UTF8_TWO_BYTE(p, c) \
3401 *(p)++ = UTF8_TWO_BYTE_HI(c); \
3402 *((p)++) = UTF8_TWO_BYTE_LO(c); \
3407 /* Actually is both lcfirst() and ucfirst(). Only the first character
3408 * changes. This means that possibly we can change in-place, ie., just
3409 * take the source and change that one character and store it back, but not
3410 * if read-only etc, or if the length changes */
3415 STRLEN slen; /* slen is the byte length of the whole SV. */
3418 bool inplace; /* ? Convert first char only, in-place */
3419 bool doing_utf8 = FALSE; /* ? using utf8 */
3420 bool convert_source_to_utf8 = FALSE; /* ? need to convert */
3421 const int op_type = PL_op->op_type;
3424 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
3425 STRLEN ulen; /* ulen is the byte length of the original Unicode character
3426 * stored as UTF-8 at s. */
3427 STRLEN tculen; /* tculen is the byte length of the freshly titlecased (or
3428 * lowercased) character stored in tmpbuf. May be either
3429 * UTF-8 or not, but in either case is the number of bytes */
3430 bool tainted = FALSE;
3434 s = (const U8*)SvPV_nomg_const(source, slen);
3436 if (ckWARN(WARN_UNINITIALIZED))
3437 report_uninit(source);
3442 /* We may be able to get away with changing only the first character, in
3443 * place, but not if read-only, etc. Later we may discover more reasons to
3444 * not convert in-place. */
3445 inplace = SvPADTMP(source) && !SvREADONLY(source) && SvTEMP(source);
3447 /* First calculate what the changed first character should be. This affects
3448 * whether we can just swap it out, leaving the rest of the string unchanged,
3449 * or even if have to convert the dest to UTF-8 when the source isn't */
3451 if (! slen) { /* If empty */
3452 need = 1; /* still need a trailing NUL */
3455 else if (DO_UTF8(source)) { /* Is the source utf8? */
3458 if (op_type == OP_UCFIRST) {
3459 _to_utf8_title_flags(s, tmpbuf, &tculen,
3460 cBOOL(IN_LOCALE_RUNTIME), &tainted);
3463 _to_utf8_lower_flags(s, tmpbuf, &tculen,
3464 cBOOL(IN_LOCALE_RUNTIME), &tainted);
3467 /* we can't do in-place if the length changes. */
3468 if (ulen != tculen) inplace = FALSE;
3469 need = slen + 1 - ulen + tculen;
3471 else { /* Non-zero length, non-UTF-8, Need to consider locale and if
3472 * latin1 is treated as caseless. Note that a locale takes
3474 ulen = 1; /* Original character is 1 byte */
3475 tculen = 1; /* Most characters will require one byte, but this will
3476 * need to be overridden for the tricky ones */
3479 if (op_type == OP_LCFIRST) {
3481 /* lower case the first letter: no trickiness for any character */
3482 *tmpbuf = (IN_LOCALE_RUNTIME) ? toLOWER_LC(*s) :
3483 ((IN_UNI_8_BIT) ? toLOWER_LATIN1(*s) : toLOWER(*s));
3486 else if (IN_LOCALE_RUNTIME) {
3487 *tmpbuf = toUPPER_LC(*s); /* This would be a bug if any locales
3488 * have upper and title case different
3491 else if (! IN_UNI_8_BIT) {
3492 *tmpbuf = toUPPER(*s); /* Returns caseless for non-ascii, or
3493 * on EBCDIC machines whatever the
3494 * native function does */
3496 else { /* is ucfirst non-UTF-8, not in locale, and cased latin1 */
3497 UV title_ord = _to_upper_title_latin1(*s, tmpbuf, &tculen, 's');
3499 assert(tculen == 2);
3501 /* If the result is an upper Latin1-range character, it can
3502 * still be represented in one byte, which is its ordinal */
3503 if (UTF8_IS_DOWNGRADEABLE_START(*tmpbuf)) {
3504 *tmpbuf = (U8) title_ord;
3508 /* Otherwise it became more than one ASCII character (in
3509 * the case of LATIN_SMALL_LETTER_SHARP_S) or changed to
3510 * beyond Latin1, so the number of bytes changed, so can't
3511 * replace just the first character in place. */
3514 /* If the result won't fit in a byte, the entire result will
3515 * have to be in UTF-8. Assume worst case sizing in
3516 * conversion. (all latin1 characters occupy at most two bytes
3518 if (title_ord > 255) {
3520 convert_source_to_utf8 = TRUE;
3521 need = slen * 2 + 1;
3523 /* The (converted) UTF-8 and UTF-EBCDIC lengths of all
3524 * (both) characters whose title case is above 255 is
3528 else { /* LATIN_SMALL_LETTER_SHARP_S expands by 1 byte */
3529 need = slen + 1 + 1;
3533 } /* End of use Unicode (Latin1) semantics */
3534 } /* End of changing the case of the first character */
3536 /* Here, have the first character's changed case stored in tmpbuf. Ready to
3537 * generate the result */
3540 /* We can convert in place. This means we change just the first
3541 * character without disturbing the rest; no need to grow */
3543 s = d = (U8*)SvPV_force_nomg(source, slen);
3549 /* Here, we can't convert in place; we earlier calculated how much
3550 * space we will need, so grow to accommodate that */
3551 SvUPGRADE(dest, SVt_PV);
3552 d = (U8*)SvGROW(dest, need);
3553 (void)SvPOK_only(dest);
3560 if (! convert_source_to_utf8) {
3562 /* Here both source and dest are in UTF-8, but have to create
3563 * the entire output. We initialize the result to be the
3564 * title/lower cased first character, and then append the rest
3566 sv_setpvn(dest, (char*)tmpbuf, tculen);
3568 sv_catpvn(dest, (char*)(s + ulen), slen - ulen);
3572 const U8 *const send = s + slen;
3574 /* Here the dest needs to be in UTF-8, but the source isn't,
3575 * except we earlier UTF-8'd the first character of the source
3576 * into tmpbuf. First put that into dest, and then append the
3577 * rest of the source, converting it to UTF-8 as we go. */
3579 /* Assert tculen is 2 here because the only two characters that
3580 * get to this part of the code have 2-byte UTF-8 equivalents */
3582 *d++ = *(tmpbuf + 1);
3583 s++; /* We have just processed the 1st char */
3585 for (; s < send; s++) {
3586 d = uvchr_to_utf8(d, *s);
3589 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3593 else { /* in-place UTF-8. Just overwrite the first character */
3594 Copy(tmpbuf, d, tculen, U8);
3595 SvCUR_set(dest, need - 1);
3603 else { /* Neither source nor dest are in or need to be UTF-8 */
3605 if (IN_LOCALE_RUNTIME) {
3609 if (inplace) { /* in-place, only need to change the 1st char */
3612 else { /* Not in-place */
3614 /* Copy the case-changed character(s) from tmpbuf */
3615 Copy(tmpbuf, d, tculen, U8);
3616 d += tculen - 1; /* Code below expects d to point to final
3617 * character stored */
3620 else { /* empty source */
3621 /* See bug #39028: Don't taint if empty */
3625 /* In a "use bytes" we don't treat the source as UTF-8, but, still want
3626 * the destination to retain that flag */
3630 if (!inplace) { /* Finish the rest of the string, unchanged */
3631 /* This will copy the trailing NUL */
3632 Copy(s + 1, d + 1, slen, U8);
3633 SvCUR_set(dest, need - 1);
3636 if (dest != source && SvTAINTED(source))
3642 /* There's so much setup/teardown code common between uc and lc, I wonder if
3643 it would be worth merging the two, and just having a switch outside each
3644 of the three tight loops. There is less and less commonality though */
3658 if (SvPADTMP(source) && !SvREADONLY(source) && !SvAMAGIC(source)
3659 && SvTEMP(source) && !DO_UTF8(source)
3660 && (IN_LOCALE_RUNTIME || ! IN_UNI_8_BIT)) {
3662 /* We can convert in place. The reason we can't if in UNI_8_BIT is to
3663 * make the loop tight, so we overwrite the source with the dest before
3664 * looking at it, and we need to look at the original source
3665 * afterwards. There would also need to be code added to handle
3666 * switching to not in-place in midstream if we run into characters
3667 * that change the length.
3670 s = d = (U8*)SvPV_force_nomg(source, len);
3677 /* The old implementation would copy source into TARG at this point.
3678 This had the side effect that if source was undef, TARG was now
3679 an undefined SV with PADTMP set, and they don't warn inside
3680 sv_2pv_flags(). However, we're now getting the PV direct from
3681 source, which doesn't have PADTMP set, so it would warn. Hence the
3685 s = (const U8*)SvPV_nomg_const(source, len);
3687 if (ckWARN(WARN_UNINITIALIZED))
3688 report_uninit(source);
3694 SvUPGRADE(dest, SVt_PV);
3695 d = (U8*)SvGROW(dest, min);
3696 (void)SvPOK_only(dest);
3701 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
3702 to check DO_UTF8 again here. */
3704 if (DO_UTF8(source)) {
3705 const U8 *const send = s + len;
3706 U8 tmpbuf[UTF8_MAXBYTES+1];
3707 bool tainted = FALSE;
3709 /* All occurrences of these are to be moved to follow any other marks.
3710 * This is context-dependent. We may not be passed enough context to
3711 * move the iota subscript beyond all of them, but we do the best we can
3712 * with what we're given. The result is always better than if we
3713 * hadn't done this. And, the problem would only arise if we are
3714 * passed a character without all its combining marks, which would be
3715 * the caller's mistake. The information this is based on comes from a
3716 * comment in Unicode SpecialCasing.txt, (and the Standard's text
3717 * itself) and so can't be checked properly to see if it ever gets
3718 * revised. But the likelihood of it changing is remote */
3719 bool in_iota_subscript = FALSE;
3725 if (in_iota_subscript && ! is_utf8_mark(s)) {
3727 /* A non-mark. Time to output the iota subscript */
3728 #define GREEK_CAPITAL_LETTER_IOTA 0x0399
3729 #define COMBINING_GREEK_YPOGEGRAMMENI 0x0345
3731 CAT_UNI_TO_UTF8_TWO_BYTE(d, GREEK_CAPITAL_LETTER_IOTA);
3732 in_iota_subscript = FALSE;
3735 /* Then handle the current character. Get the changed case value
3736 * and copy it to the output buffer */
3739 uv = _to_utf8_upper_flags(s, tmpbuf, &ulen,
3740 cBOOL(IN_LOCALE_RUNTIME), &tainted);
3741 if (uv == GREEK_CAPITAL_LETTER_IOTA
3742 && utf8_to_uvchr_buf(s, send, 0) == COMBINING_GREEK_YPOGEGRAMMENI)
3744 in_iota_subscript = TRUE;
3747 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
3748 /* If the eventually required minimum size outgrows the
3749 * available space, we need to grow. */
3750 const UV o = d - (U8*)SvPVX_const(dest);
3752 /* If someone uppercases one million U+03B0s we SvGROW()
3753 * one million times. Or we could try guessing how much to
3754 * allocate without allocating too much. Such is life.
3755 * See corresponding comment in lc code for another option
3758 d = (U8*)SvPVX(dest) + o;
3760 Copy(tmpbuf, d, ulen, U8);
3765 if (in_iota_subscript) {
3766 CAT_UNI_TO_UTF8_TWO_BYTE(d, GREEK_CAPITAL_LETTER_IOTA);
3771 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3777 else { /* Not UTF-8 */
3779 const U8 *const send = s + len;
3781 /* Use locale casing if in locale; regular style if not treating
3782 * latin1 as having case; otherwise the latin1 casing. Do the
3783 * whole thing in a tight loop, for speed, */
3784 if (IN_LOCALE_RUNTIME) {
3787 for (; s < send; d++, s++)
3788 *d = toUPPER_LC(*s);
3790 else if (! IN_UNI_8_BIT) {
3791 for (; s < send; d++, s++) {
3796 for (; s < send; d++, s++) {
3797 *d = toUPPER_LATIN1_MOD(*s);
3798 if (LIKELY(*d != LATIN_SMALL_LETTER_Y_WITH_DIAERESIS)) continue;
3800 /* The mainstream case is the tight loop above. To avoid
3801 * extra tests in that, all three characters that require
3802 * special handling are mapped by the MOD to the one tested
3804 * Use the source to distinguish between the three cases */
3806 if (*s == LATIN_SMALL_LETTER_SHARP_S) {
3808 /* uc() of this requires 2 characters, but they are
3809 * ASCII. If not enough room, grow the string */
3810 if (SvLEN(dest) < ++min) {
3811 const UV o = d - (U8*)SvPVX_const(dest);
3813 d = (U8*)SvPVX(dest) + o;
3815 *d++ = 'S'; *d = 'S'; /* upper case is 'SS' */
3816 continue; /* Back to the tight loop; still in ASCII */
3819 /* The other two special handling characters have their
3820 * upper cases outside the latin1 range, hence need to be
3821 * in UTF-8, so the whole result needs to be in UTF-8. So,
3822 * here we are somewhere in the middle of processing a
3823 * non-UTF-8 string, and realize that we will have to convert
3824 * the whole thing to UTF-8. What to do? There are
3825 * several possibilities. The simplest to code is to
3826 * convert what we have so far, set a flag, and continue on
3827 * in the loop. The flag would be tested each time through
3828 * the loop, and if set, the next character would be
3829 * converted to UTF-8 and stored. But, I (khw) didn't want
3830 * to slow down the mainstream case at all for this fairly
3831 * rare case, so I didn't want to add a test that didn't
3832 * absolutely have to be there in the loop, besides the
3833 * possibility that it would get too complicated for
3834 * optimizers to deal with. Another possibility is to just
3835 * give up, convert the source to UTF-8, and restart the
3836 * function that way. Another possibility is to convert
3837 * both what has already been processed and what is yet to
3838 * come separately to UTF-8, then jump into the loop that
3839 * handles UTF-8. But the most efficient time-wise of the
3840 * ones I could think of is what follows, and turned out to
3841 * not require much extra code. */
3843 /* Convert what we have so far into UTF-8, telling the
3844 * function that we know it should be converted, and to
3845 * allow extra space for what we haven't processed yet.
3846 * Assume the worst case space requirements for converting
3847 * what we haven't processed so far: that it will require
3848 * two bytes for each remaining source character, plus the
3849 * NUL at the end. This may cause the string pointer to
3850 * move, so re-find it. */
3852 len = d - (U8*)SvPVX_const(dest);
3853 SvCUR_set(dest, len);
3854 len = sv_utf8_upgrade_flags_grow(dest,
3855 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
3857 d = (U8*)SvPVX(dest) + len;
3859 /* Now process the remainder of the source, converting to
3860 * upper and UTF-8. If a resulting byte is invariant in
3861 * UTF-8, output it as-is, otherwise convert to UTF-8 and
3862 * append it to the output. */
3863 for (; s < send; s++) {
3864 (void) _to_upper_title_latin1(*s, d, &len, 'S');
3868 /* Here have processed the whole source; no need to continue
3869 * with the outer loop. Each character has been converted
3870 * to upper case and converted to UTF-8 */
3873 } /* End of processing all latin1-style chars */
3874 } /* End of processing all chars */
3875 } /* End of source is not empty */
3877 if (source != dest) {
3878 *d = '\0'; /* Here d points to 1 after last char, add NUL */
3879 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3881 } /* End of isn't utf8 */
3882 if (dest != source && SvTAINTED(source))
3901 if (SvPADTMP(source) && !SvREADONLY(source) && !SvAMAGIC(source)
3902 && SvTEMP(source) && !DO_UTF8(source)) {
3904 /* We can convert in place, as lowercasing anything in the latin1 range
3905 * (or else DO_UTF8 would have been on) doesn't lengthen it */
3907 s = d = (U8*)SvPV_force_nomg(source, len);
3914 /* The old implementation would copy source into TARG at this point.
3915 This had the side effect that if source was undef, TARG was now
3916 an undefined SV with PADTMP set, and they don't warn inside
3917 sv_2pv_flags(). However, we're now getting the PV direct from
3918 source, which doesn't have PADTMP set, so it would warn. Hence the
3922 s = (const U8*)SvPV_nomg_const(source, len);
3924 if (ckWARN(WARN_UNINITIALIZED))
3925 report_uninit(source);
3931 SvUPGRADE(dest, SVt_PV);
3932 d = (U8*)SvGROW(dest, min);
3933 (void)SvPOK_only(dest);
3938 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
3939 to check DO_UTF8 again here. */
3941 if (DO_UTF8(source)) {
3942 const U8 *const send = s + len;
3943 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
3944 bool tainted = FALSE;
3947 const STRLEN u = UTF8SKIP(s);
3950 _to_utf8_lower_flags(s, tmpbuf, &ulen,
3951 cBOOL(IN_LOCALE_RUNTIME), &tainted);
3953 /* Here is where we would do context-sensitive actions. See the
3954 * commit message for this comment for why there isn't any */
3956 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
3958 /* If the eventually required minimum size outgrows the
3959 * available space, we need to grow. */
3960 const UV o = d - (U8*)SvPVX_const(dest);
3962 /* If someone lowercases one million U+0130s we SvGROW() one
3963 * million times. Or we could try guessing how much to
3964 * allocate without allocating too much. Such is life.
3965 * Another option would be to grow an extra byte or two more
3966 * each time we need to grow, which would cut down the million
3967 * to 500K, with little waste */
3969 d = (U8*)SvPVX(dest) + o;
3972 /* Copy the newly lowercased letter to the output buffer we're
3974 Copy(tmpbuf, d, ulen, U8);
3977 } /* End of looping through the source string */
3980 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3985 } else { /* Not utf8 */
3987 const U8 *const send = s + len;
3989 /* Use locale casing if in locale; regular style if not treating
3990 * latin1 as having case; otherwise the latin1 casing. Do the
3991 * whole thing in a tight loop, for speed, */
3992 if (IN_LOCALE_RUNTIME) {
3995 for (; s < send; d++, s++)
3996 *d = toLOWER_LC(*s);
3998 else if (! IN_UNI_8_BIT) {
3999 for (; s < send; d++, s++) {
4004 for (; s < send; d++, s++) {
4005 *d = toLOWER_LATIN1(*s);
4009 if (source != dest) {
4011 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4014 if (dest != source && SvTAINTED(source))
4023 SV * const sv = TOPs;
4025 const char *s = SvPV_const(sv,len);
4027 SvUTF8_off(TARG); /* decontaminate */
4030 SvUPGRADE(TARG, SVt_PV);
4031 SvGROW(TARG, (len * 2) + 1);
4035 STRLEN ulen = UTF8SKIP(s);
4036 bool to_quote = FALSE;
4038 if (UTF8_IS_INVARIANT(*s)) {
4039 if (_isQUOTEMETA(*s)) {
4043 else if (UTF8_IS_DOWNGRADEABLE_START(*s)) {
4045 /* In locale, we quote all non-ASCII Latin1 chars.
4046 * Otherwise use the quoting rules */
4047 if (IN_LOCALE_RUNTIME
4048 || _isQUOTEMETA(TWO_BYTE_UTF8_TO_UNI(*s, *(s + 1))))
4053 else if (is_QUOTEMETA_high(s)) {
4068 else if (IN_UNI_8_BIT) {
4070 if (_isQUOTEMETA(*s))
4076 /* For non UNI_8_BIT (and hence in locale) just quote all \W
4077 * including everything above ASCII */
4079 if (!isWORDCHAR_A(*s))
4085 SvCUR_set(TARG, d - SvPVX_const(TARG));
4086 (void)SvPOK_only_UTF8(TARG);
4089 sv_setpvn(TARG, s, len);
4106 U8 tmpbuf[UTF8_MAXBYTES * UTF8_MAX_FOLD_CHAR_EXPAND + 1];
4107 const bool full_folding = TRUE;
4108 const U8 flags = ( full_folding ? FOLD_FLAGS_FULL : 0 )
4109 | ( IN_LOCALE_RUNTIME ? FOLD_FLAGS_LOCALE : 0 );
4111 /* This is a facsimile of pp_lc, but with a thousand bugs thanks to me.
4112 * You are welcome(?) -Hugmeir
4120 s = (const U8*)SvPV_nomg_const(source, len);
4122 if (ckWARN(WARN_UNINITIALIZED))
4123 report_uninit(source);
4130 SvUPGRADE(dest, SVt_PV);
4131 d = (U8*)SvGROW(dest, min);
4132 (void)SvPOK_only(dest);
4137 if (DO_UTF8(source)) { /* UTF-8 flagged string. */
4138 bool tainted = FALSE;
4140 const STRLEN u = UTF8SKIP(s);
4143 _to_utf8_fold_flags(s, tmpbuf, &ulen, flags, &tainted);
4145 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
4146 const UV o = d - (U8*)SvPVX_const(dest);
4148 d = (U8*)SvPVX(dest) + o;
4151 Copy(tmpbuf, d, ulen, U8);
4160 } /* Unflagged string */
4162 /* For locale, bytes, and nothing, the behavior is supposed to be the
4165 if ( IN_LOCALE_RUNTIME ) { /* Under locale */
4168 for (; s < send; d++, s++)
4169 *d = toLOWER_LC(*s);
4171 else if ( !IN_UNI_8_BIT ) { /* Under nothing, or bytes */
4172 for (; s < send; d++, s++)
4176 /* For ASCII and the Latin-1 range, there's only two troublesome folds,
4177 * \x{DF} (\N{LATIN SMALL LETTER SHARP S}), which under full casefolding
4178 * becomes 'ss', and \x{B5} (\N{MICRO SIGN}), which under any fold becomes
4179 * \x{3BC} (\N{GREEK SMALL LETTER MU}) -- For the rest, the casefold is
4182 for (; s < send; d++, s++) {
4183 if (*s == MICRO_SIGN) {
4184 /* \N{MICRO SIGN}'s casefold is \N{GREEK SMALL LETTER MU}, which
4185 * is outside of the latin-1 range. There's a couple of ways to
4186 * deal with this -- khw discusses them in pp_lc/uc, so go there :)
4187 * What we do here is upgrade what we had already casefolded,
4188 * then enter an inner loop that appends the rest of the characters
4191 len = d - (U8*)SvPVX_const(dest);
4192 SvCUR_set(dest, len);
4193 len = sv_utf8_upgrade_flags_grow(dest,
4194 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
4195 /* The max expansion for latin1
4196 * chars is 1 byte becomes 2 */
4198 d = (U8*)SvPVX(dest) + len;
4200 CAT_UNI_TO_UTF8_TWO_BYTE(d, GREEK_SMALL_LETTER_MU);
4202 for (; s < send; s++) {
4204 UV fc = _to_uni_fold_flags(*s, tmpbuf, &ulen, flags);
4205 if UNI_IS_INVARIANT(fc) {
4206 if ( full_folding && *s == LATIN_SMALL_LETTER_SHARP_S) {
4214 Copy(tmpbuf, d, ulen, U8);
4220 else if (full_folding && *s == LATIN_SMALL_LETTER_SHARP_S) {
4221 /* Under full casefolding, LATIN SMALL LETTER SHARP S becomes "ss",
4222 * which may require growing the SV.
4224 if (SvLEN(dest) < ++min) {
4225 const UV o = d - (U8*)SvPVX_const(dest);
4227 d = (U8*)SvPVX(dest) + o;
4232 else { /* If it's not one of those two, the fold is their lower case */
4233 *d = toLOWER_LATIN1(*s);
4239 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4241 if (SvTAINTED(source))
4251 dVAR; dSP; dMARK; dORIGMARK;
4252 AV *const av = MUTABLE_AV(POPs);
4253 const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
4255 if (SvTYPE(av) == SVt_PVAV) {
4256 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
4257 bool can_preserve = FALSE;
4263 can_preserve = SvCANEXISTDELETE(av);
4266 if (lval && localizing) {
4269 for (svp = MARK + 1; svp <= SP; svp++) {
4270 const I32 elem = SvIV(*svp);
4274 if (max > AvMAX(av))
4278 while (++MARK <= SP) {
4280 I32 elem = SvIV(*MARK);
4281 bool preeminent = TRUE;
4283 if (localizing && can_preserve) {
4284 /* If we can determine whether the element exist,
4285 * Try to preserve the existenceness of a tied array
4286 * element by using EXISTS and DELETE if possible.
4287 * Fallback to FETCH and STORE otherwise. */
4288 preeminent = av_exists(av, elem);
4291 svp = av_fetch(av, elem, lval);
4293 if (!svp || *svp == &PL_sv_undef)
4294 DIE(aTHX_ PL_no_aelem, elem);
4297 save_aelem(av, elem, svp);
4299 SAVEADELETE(av, elem);
4302 *MARK = svp ? *svp : &PL_sv_undef;
4305 if (GIMME != G_ARRAY) {
4307 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
4313 /* Smart dereferencing for keys, values and each */
4325 (SvTYPE(sv) != SVt_PVHV && SvTYPE(sv) != SVt_PVAV)
4330 "Type of argument to %s must be unblessed hashref or arrayref",
4331 PL_op_desc[PL_op->op_type] );
4334 if (PL_op->op_flags & OPf_SPECIAL && SvTYPE(sv) == SVt_PVAV)
4336 "Can't modify %s in %s",
4337 PL_op_desc[PL_op->op_type], PL_op_desc[PL_op->op_next->op_type]
4340 /* Delegate to correct function for op type */
4342 if (PL_op->op_type == OP_RKEYS || PL_op->op_type == OP_RVALUES) {
4343 return (SvTYPE(sv) == SVt_PVHV) ? Perl_do_kv(aTHX) : Perl_pp_akeys(aTHX);
4346 return (SvTYPE(sv) == SVt_PVHV) ? Perl_pp_each(aTHX) : Perl_pp_aeach(aTHX);
4354 AV *array = MUTABLE_AV(POPs);
4355 const I32 gimme = GIMME_V;
4356 IV *iterp = Perl_av_iter_p(aTHX_ array);
4357 const IV current = (*iterp)++;
4359 if (current > av_len(array)) {
4361 if (gimme == G_SCALAR)
4369 if (gimme == G_ARRAY) {
4370 SV **const element = av_fetch(array, current, 0);
4371 PUSHs(element ? *element : &PL_sv_undef);
4380 AV *array = MUTABLE_AV(POPs);
4381 const I32 gimme = GIMME_V;
4383 *Perl_av_iter_p(aTHX_ array) = 0;
4385 if (gimme == G_SCALAR) {
4387 PUSHi(av_len(array) + 1);
4389 else if (gimme == G_ARRAY) {
4390 IV n = Perl_av_len(aTHX_ array);
4395 if (PL_op->op_type == OP_AKEYS || PL_op->op_type == OP_RKEYS) {
4396 for (i = 0; i <= n; i++) {
4401 for (i = 0; i <= n; i++) {
4402 SV *const *const elem = Perl_av_fetch(aTHX_ array, i, 0);
4403 PUSHs(elem ? *elem : &PL_sv_undef);
4410 /* Associative arrays. */
4416 HV * hash = MUTABLE_HV(POPs);
4418 const I32 gimme = GIMME_V;
4421 /* might clobber stack_sp */
4422 entry = hv_iternext(hash);
4427 SV* const sv = hv_iterkeysv(entry);
4428 PUSHs(sv); /* won't clobber stack_sp */
4429 if (gimme == G_ARRAY) {
4432 /* might clobber stack_sp */
4433 val = hv_iterval(hash, entry);
4438 else if (gimme == G_SCALAR)
4445 S_do_delete_local(pTHX)
4449 const I32 gimme = GIMME_V;
4452 const bool sliced = !!(PL_op->op_private & OPpSLICE);
4453 SV *unsliced_keysv = sliced ? NULL : POPs;
4454 SV * const osv = POPs;
4455 SV **mark = sliced ? PL_stack_base + POPMARK : &unsliced_keysv-1;
4457 const bool tied = SvRMAGICAL(osv)
4458 && mg_find((const SV *)osv, PERL_MAGIC_tied);
4459 const bool can_preserve = SvCANEXISTDELETE(osv);
4460 const U32 type = SvTYPE(osv);
4461 SV ** const end = sliced ? SP : &unsliced_keysv;
4463 if (type == SVt_PVHV) { /* hash element */
4464 HV * const hv = MUTABLE_HV(osv);
4465 while (++MARK <= end) {
4466 SV * const keysv = *MARK;
4468 bool preeminent = TRUE;
4470 preeminent = hv_exists_ent(hv, keysv, 0);
4472 HE *he = hv_fetch_ent(hv, keysv, 1, 0);
4479 sv = hv_delete_ent(hv, keysv, 0, 0);
4480 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4483 if (!sv) DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
4484 save_helem_flags(hv, keysv, &sv, SAVEf_KEEPOLDELEM);
4486 *MARK = sv_mortalcopy(sv);
4492 SAVEHDELETE(hv, keysv);
4493 *MARK = &PL_sv_undef;
4497 else if (type == SVt_PVAV) { /* array element */
4498 if (PL_op->op_flags & OPf_SPECIAL) {
4499 AV * const av = MUTABLE_AV(osv);
4500 while (++MARK <= end) {
4501 I32 idx = SvIV(*MARK);
4503 bool preeminent = TRUE;
4505 preeminent = av_exists(av, idx);
4507 SV **svp = av_fetch(av, idx, 1);
4514 sv = av_delete(av, idx, 0);
4515 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4518 save_aelem_flags(av, idx, &sv, SAVEf_KEEPOLDELEM);
4520 *MARK = sv_mortalcopy(sv);
4526 SAVEADELETE(av, idx);
4527 *MARK = &PL_sv_undef;
4532 DIE(aTHX_ "panic: avhv_delete no longer supported");
4535 DIE(aTHX_ "Not a HASH reference");
4537 if (gimme == G_VOID)
4539 else if (gimme == G_SCALAR) {
4544 *++MARK = &PL_sv_undef;
4548 else if (gimme != G_VOID)
4549 PUSHs(unsliced_keysv);
4561 if (PL_op->op_private & OPpLVAL_INTRO)
4562 return do_delete_local();
4565 discard = (gimme == G_VOID) ? G_DISCARD : 0;
4567 if (PL_op->op_private & OPpSLICE) {
4569 HV * const hv = MUTABLE_HV(POPs);
4570 const U32 hvtype = SvTYPE(hv);
4571 if (hvtype == SVt_PVHV) { /* hash element */
4572 while (++MARK <= SP) {
4573 SV * const sv = hv_delete_ent(hv, *MARK, discard, 0);
4574 *MARK = sv ? sv : &PL_sv_undef;
4577 else if (hvtype == SVt_PVAV) { /* array element */
4578 if (PL_op->op_flags & OPf_SPECIAL) {
4579 while (++MARK <= SP) {
4580 SV * const sv = av_delete(MUTABLE_AV(hv), SvIV(*MARK), discard);
4581 *MARK = sv ? sv : &PL_sv_undef;
4586 DIE(aTHX_ "Not a HASH reference");
4589 else if (gimme == G_SCALAR) {
4594 *++MARK = &PL_sv_undef;
4600 HV * const hv = MUTABLE_HV(POPs);
4602 if (SvTYPE(hv) == SVt_PVHV)
4603 sv = hv_delete_ent(hv, keysv, discard, 0);
4604 else if (SvTYPE(hv) == SVt_PVAV) {
4605 if (PL_op->op_flags & OPf_SPECIAL)
4606 sv = av_delete(MUTABLE_AV(hv), SvIV(keysv), discard);
4608 DIE(aTHX_ "panic: avhv_delete no longer supported");
4611 DIE(aTHX_ "Not a HASH reference");