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 static const STRLEN small_mu_len = sizeof(GREEK_SMALL_LETTER_MU_UTF8) - 1;
51 static const STRLEN capital_iota_len = sizeof(GREEK_CAPITAL_LETTER_IOTA_UTF8) - 1;
53 /* variations on pp_null */
59 if (GIMME_V == G_SCALAR)
70 assert(SvTYPE(TARG) == SVt_PVAV);
71 if (UNLIKELY( PL_op->op_private & OPpLVAL_INTRO ))
72 if (LIKELY( !(PL_op->op_private & OPpPAD_STATE) ))
73 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
75 if (PL_op->op_flags & OPf_REF) {
78 } else if (PL_op->op_private & OPpMAYBE_LVSUB) {
79 const I32 flags = is_lvalue_sub();
80 if (flags && !(flags & OPpENTERSUB_INARGS)) {
81 if (GIMME == G_SCALAR)
82 /* diag_listed_as: Can't return %s to lvalue scalar context */
83 Perl_croak(aTHX_ "Can't return array to lvalue scalar context");
89 if (gimme == G_ARRAY) {
90 /* XXX see also S_pushav in pp_hot.c */
91 const Size_t maxarg = AvFILL(MUTABLE_AV(TARG)) + 1;
93 if (SvMAGICAL(TARG)) {
95 for (i=0; i < maxarg; i++) {
96 SV * const * const svp = av_fetch(MUTABLE_AV(TARG), i, FALSE);
97 SP[i+1] = (svp) ? *svp : &PL_sv_undef;
102 for (i=0; i < (PADOFFSET)maxarg; i++) {
103 SV * const sv = AvARRAY((const AV *)TARG)[i];
104 SP[i+1] = sv ? sv : &PL_sv_undef;
109 else if (gimme == G_SCALAR) {
110 SV* const sv = sv_newmortal();
111 const SSize_t maxarg = AvFILL(MUTABLE_AV(TARG)) + 1;
112 sv_setiv(sv, maxarg);
123 assert(SvTYPE(TARG) == SVt_PVHV);
125 if (UNLIKELY( PL_op->op_private & OPpLVAL_INTRO ))
126 if (LIKELY( !(PL_op->op_private & OPpPAD_STATE) ))
127 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
128 if (PL_op->op_flags & OPf_REF)
130 else if (PL_op->op_private & OPpMAYBE_LVSUB) {
131 const I32 flags = is_lvalue_sub();
132 if (flags && !(flags & OPpENTERSUB_INARGS)) {
133 if (GIMME == G_SCALAR)
134 /* diag_listed_as: Can't return %s to lvalue scalar context */
135 Perl_croak(aTHX_ "Can't return hash to lvalue scalar context");
140 if (gimme == G_ARRAY) {
141 RETURNOP(Perl_do_kv(aTHX));
143 else if ((PL_op->op_private & OPpTRUEBOOL
144 || ( PL_op->op_private & OPpMAYBE_TRUEBOOL
145 && block_gimme() == G_VOID ))
146 && (!SvRMAGICAL(TARG) || !mg_find(TARG, PERL_MAGIC_tied)))
147 SETs(HvUSEDKEYS(TARG) ? &PL_sv_yes : sv_2mortal(newSViv(0)));
148 else if (gimme == G_SCALAR) {
149 SV* const sv = Perl_hv_scalar(aTHX_ MUTABLE_HV(TARG));
158 assert(SvTYPE(TARG) == SVt_PVCV);
166 SvPADSTALE_off(TARG);
174 mg_find(PadlistNAMESARRAY(CvPADLIST(find_runcv(NULL)))[ARGTARG],
176 assert(SvTYPE(TARG) == SVt_PVCV);
179 if (CvISXSUB(mg->mg_obj)) { /* constant */
180 /* XXX Should we clone it here? */
181 /* If this changes to use SAVECLEARSV, we can move the SAVECLEARSV
182 to introcv and remove the SvPADSTALE_off. */
183 SAVEPADSVANDMORTALIZE(ARGTARG);
184 PAD_SVl(ARGTARG) = SvREFCNT_inc_simple_NN(mg->mg_obj);
187 if (CvROOT(mg->mg_obj)) {
188 assert(CvCLONE(mg->mg_obj));
189 assert(!CvCLONED(mg->mg_obj));
191 cv_clone_into((CV *)mg->mg_obj,(CV *)TARG);
192 SAVECLEARSV(PAD_SVl(ARGTARG));
199 static const char S_no_symref_sv[] =
200 "Can't use string (\"%" SVf32 "\"%s) as %s ref while \"strict refs\" in use";
202 /* In some cases this function inspects PL_op. If this function is called
203 for new op types, more bool parameters may need to be added in place of
206 When noinit is true, the absence of a gv will cause a retval of undef.
207 This is unrelated to the cv-to-gv assignment case.
211 S_rv2gv(pTHX_ SV *sv, const bool vivify_sv, const bool strict,
215 if (!isGV(sv) || SvFAKE(sv)) SvGETMAGIC(sv);
218 sv = amagic_deref_call(sv, to_gv_amg);
222 if (SvTYPE(sv) == SVt_PVIO) {
223 GV * const gv = MUTABLE_GV(sv_newmortal());
224 gv_init(gv, 0, "__ANONIO__", 10, 0);
225 GvIOp(gv) = MUTABLE_IO(sv);
226 SvREFCNT_inc_void_NN(sv);
229 else if (!isGV_with_GP(sv))
230 return (SV *)Perl_die(aTHX_ "Not a GLOB reference");
233 if (!isGV_with_GP(sv)) {
235 /* If this is a 'my' scalar and flag is set then vivify
238 if (vivify_sv && sv != &PL_sv_undef) {
241 Perl_croak_no_modify();
242 if (cUNOP->op_targ) {
243 SV * const namesv = PAD_SV(cUNOP->op_targ);
244 HV *stash = CopSTASH(PL_curcop);
245 if (SvTYPE(stash) != SVt_PVHV) stash = NULL;
246 gv = MUTABLE_GV(newSV(0));
247 gv_init_sv(gv, stash, namesv, 0);
250 const char * const name = CopSTASHPV(PL_curcop);
251 gv = newGVgen_flags(name,
252 HvNAMEUTF8(CopSTASH(PL_curcop)) ? SVf_UTF8 : 0 );
254 prepare_SV_for_RV(sv);
255 SvRV_set(sv, MUTABLE_SV(gv));
260 if (PL_op->op_flags & OPf_REF || strict)
261 return (SV *)Perl_die(aTHX_ PL_no_usym, "a symbol");
262 if (ckWARN(WARN_UNINITIALIZED))
268 if (!(sv = MUTABLE_SV(gv_fetchsv_nomg(
269 sv, GV_ADDMG, SVt_PVGV
279 (SvPOKp(sv) && SvCUR(sv)>32 ? "..." : ""),
282 if ((PL_op->op_private & (OPpLVAL_INTRO|OPpDONT_INIT_GV))
283 == OPpDONT_INIT_GV) {
284 /* We are the target of a coderef assignment. Return
285 the scalar unchanged, and let pp_sasssign deal with
289 sv = MUTABLE_SV(gv_fetchsv_nomg(sv, GV_ADD, SVt_PVGV));
291 /* FAKE globs in the symbol table cause weird bugs (#77810) */
295 if (SvFAKE(sv) && !(PL_op->op_private & OPpALLOW_FAKE)) {
296 SV *newsv = sv_newmortal();
297 sv_setsv_flags(newsv, sv, 0);
309 sv, PL_op->op_private & OPpDEREF,
310 PL_op->op_private & HINT_STRICT_REFS,
311 ((PL_op->op_flags & OPf_SPECIAL) && !(PL_op->op_flags & OPf_MOD))
312 || PL_op->op_type == OP_READLINE
314 if (PL_op->op_private & OPpLVAL_INTRO)
315 save_gp(MUTABLE_GV(sv), !(PL_op->op_flags & OPf_SPECIAL));
320 /* Helper function for pp_rv2sv and pp_rv2av */
322 Perl_softref2xv(pTHX_ SV *const sv, const char *const what,
323 const svtype type, SV ***spp)
328 PERL_ARGS_ASSERT_SOFTREF2XV;
330 if (PL_op->op_private & HINT_STRICT_REFS) {
332 Perl_die(aTHX_ S_no_symref_sv, sv,
333 (SvPOKp(sv) && SvCUR(sv)>32 ? "..." : ""), what);
335 Perl_die(aTHX_ PL_no_usym, what);
339 PL_op->op_flags & OPf_REF
341 Perl_die(aTHX_ PL_no_usym, what);
342 if (ckWARN(WARN_UNINITIALIZED))
344 if (type != SVt_PV && GIMME_V == G_ARRAY) {
348 **spp = &PL_sv_undef;
351 if ((PL_op->op_flags & OPf_SPECIAL) &&
352 !(PL_op->op_flags & OPf_MOD))
354 if (!(gv = gv_fetchsv_nomg(sv, GV_ADDMG, type)))
356 **spp = &PL_sv_undef;
361 gv = gv_fetchsv_nomg(sv, GV_ADD, type);
374 sv = amagic_deref_call(sv, to_sv_amg);
378 switch (SvTYPE(sv)) {
384 DIE(aTHX_ "Not a SCALAR reference");
391 if (!isGV_with_GP(gv)) {
392 gv = Perl_softref2xv(aTHX_ sv, "a SCALAR", SVt_PV, &sp);
398 if (PL_op->op_flags & OPf_MOD) {
399 if (PL_op->op_private & OPpLVAL_INTRO) {
400 if (cUNOP->op_first->op_type == OP_NULL)
401 sv = save_scalar(MUTABLE_GV(TOPs));
403 sv = save_scalar(gv);
405 Perl_croak(aTHX_ "%s", PL_no_localize_ref);
407 else if (PL_op->op_private & OPpDEREF)
408 sv = vivify_ref(sv, PL_op->op_private & OPpDEREF);
417 AV * const av = MUTABLE_AV(TOPs);
418 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
420 SV ** const sv = Perl_av_arylen_p(aTHX_ MUTABLE_AV(av));
422 *sv = newSV_type(SVt_PVMG);
423 sv_magic(*sv, MUTABLE_SV(av), PERL_MAGIC_arylen, NULL, 0);
427 SETs(sv_2mortal(newSViv(AvFILL(MUTABLE_AV(av)))));
436 if (PL_op->op_flags & OPf_MOD || LVRET) {
437 SV * const ret = sv_2mortal(newSV_type(SVt_PVLV));/* Not TARG RT#67838 */
438 sv_magic(ret, NULL, PERL_MAGIC_pos, NULL, 0);
440 LvTARG(ret) = SvREFCNT_inc_simple(sv);
441 PUSHs(ret); /* no SvSETMAGIC */
445 const MAGIC * const mg = mg_find_mglob(sv);
446 if (mg && mg->mg_len != -1) {
448 STRLEN i = mg->mg_len;
449 if (mg->mg_flags & MGf_BYTES && DO_UTF8(sv))
450 i = sv_pos_b2u_flags(sv, i, SV_GMAGIC|SV_CONST_RETURN);
463 const I32 flags = (PL_op->op_flags & OPf_SPECIAL)
465 : ((PL_op->op_private & (OPpLVAL_INTRO|OPpMAY_RETURN_CONSTANT))
466 == OPpMAY_RETURN_CONSTANT)
469 /* We usually try to add a non-existent subroutine in case of AUTOLOAD. */
470 /* (But not in defined().) */
472 CV *cv = sv_2cv(TOPs, &stash_unused, &gv, flags);
474 else if ((flags == (GV_ADD|GV_NOEXPAND)) && gv && SvROK(gv)) {
478 cv = MUTABLE_CV(&PL_sv_undef);
479 SETs(MUTABLE_SV(cv));
489 SV *ret = &PL_sv_undef;
491 if (SvGMAGICAL(TOPs)) SETs(sv_mortalcopy(TOPs));
492 if (SvPOK(TOPs) && SvCUR(TOPs) >= 7) {
493 const char * s = SvPVX_const(TOPs);
494 if (strnEQ(s, "CORE::", 6)) {
495 const int code = keyword(s + 6, SvCUR(TOPs) - 6, 1);
497 DIE(aTHX_ "Can't find an opnumber for \"%"UTF8f"\"",
498 UTF8fARG(SvFLAGS(TOPs) & SVf_UTF8, SvCUR(TOPs)-6, s+6));
500 SV * const sv = core_prototype(NULL, s + 6, code, NULL);
506 cv = sv_2cv(TOPs, &stash, &gv, 0);
508 ret = newSVpvn_flags(
509 CvPROTO(cv), CvPROTOLEN(cv), SVs_TEMP | SvUTF8(cv)
519 CV *cv = MUTABLE_CV(PAD_SV(PL_op->op_targ));
521 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
523 PUSHs(MUTABLE_SV(cv));
537 if (GIMME != G_ARRAY) {
541 *MARK = &PL_sv_undef;
542 *MARK = refto(*MARK);
546 EXTEND_MORTAL(SP - MARK);
548 *MARK = refto(*MARK);
553 S_refto(pTHX_ SV *sv)
558 PERL_ARGS_ASSERT_REFTO;
560 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
563 if (!(sv = LvTARG(sv)))
566 SvREFCNT_inc_void_NN(sv);
568 else if (SvTYPE(sv) == SVt_PVAV) {
569 if (!AvREAL((const AV *)sv) && AvREIFY((const AV *)sv))
570 av_reify(MUTABLE_AV(sv));
572 SvREFCNT_inc_void_NN(sv);
574 else if (SvPADTMP(sv) && !IS_PADGV(sv))
578 SvREFCNT_inc_void_NN(sv);
581 sv_upgrade(rv, SVt_IV);
590 SV * const sv = POPs;
596 (void)sv_ref(TARG,SvRV(sv),TRUE);
609 stash = CopSTASH(PL_curcop);
610 if (SvTYPE(stash) != SVt_PVHV)
611 Perl_croak(aTHX_ "Attempt to bless into a freed package");
614 SV * const ssv = POPs;
618 if (!ssv) goto curstash;
621 if (!SvAMAGIC(ssv)) {
623 Perl_croak(aTHX_ "Attempt to bless into a reference");
625 /* SvAMAGIC is on here, but it only means potentially overloaded,
626 so after stringification: */
627 ptr = SvPV_nomg_const(ssv,len);
628 /* We need to check the flag again: */
629 if (!SvAMAGIC(ssv)) goto frog;
631 else ptr = SvPV_nomg_const(ssv,len);
633 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
634 "Explicit blessing to '' (assuming package main)");
635 stash = gv_stashpvn(ptr, len, GV_ADD|SvUTF8(ssv));
638 (void)sv_bless(TOPs, stash);
648 const char * const elem = SvPV_const(sv, len);
649 GV * const gv = MUTABLE_GV(POPs);
654 /* elem will always be NUL terminated. */
655 const char * const second_letter = elem + 1;
658 if (len == 5 && strEQ(second_letter, "RRAY"))
660 tmpRef = MUTABLE_SV(GvAV(gv));
661 if (tmpRef && !AvREAL((const AV *)tmpRef)
662 && AvREIFY((const AV *)tmpRef))
663 av_reify(MUTABLE_AV(tmpRef));
667 if (len == 4 && strEQ(second_letter, "ODE"))
668 tmpRef = MUTABLE_SV(GvCVu(gv));
671 if (len == 10 && strEQ(second_letter, "ILEHANDLE")) {
672 /* finally deprecated in 5.8.0 */
673 deprecate("*glob{FILEHANDLE}");
674 tmpRef = MUTABLE_SV(GvIOp(gv));
677 if (len == 6 && strEQ(second_letter, "ORMAT"))
678 tmpRef = MUTABLE_SV(GvFORM(gv));
681 if (len == 4 && strEQ(second_letter, "LOB"))
682 tmpRef = MUTABLE_SV(gv);
685 if (len == 4 && strEQ(second_letter, "ASH"))
686 tmpRef = MUTABLE_SV(GvHV(gv));
689 if (*second_letter == 'O' && !elem[2] && len == 2)
690 tmpRef = MUTABLE_SV(GvIOp(gv));
693 if (len == 4 && strEQ(second_letter, "AME"))
694 sv = newSVhek(GvNAME_HEK(gv));
697 if (len == 7 && strEQ(second_letter, "ACKAGE")) {
698 const HV * const stash = GvSTASH(gv);
699 const HEK * const hek = stash ? HvNAME_HEK(stash) : NULL;
700 sv = hek ? newSVhek(hek) : newSVpvs("__ANON__");
704 if (len == 6 && strEQ(second_letter, "CALAR"))
719 /* Pattern matching */
727 if (len == 0 || len > I32_MAX || !SvPOK(sv) || SvUTF8(sv) || SvVALID(sv)) {
728 /* Historically, study was skipped in these cases. */
732 /* Make study a no-op. It's no longer useful and its existence
733 complicates matters elsewhere. */
742 if (PL_op->op_flags & OPf_STACKED)
744 else if (PL_op->op_private & OPpTARGET_MY)
750 if(PL_op->op_type == OP_TRANSR) {
752 const char * const pv = SvPV(sv,len);
753 SV * const newsv = newSVpvn_flags(pv, len, SVs_TEMP|SvUTF8(sv));
758 TARG = sv_newmortal();
764 /* Lvalue operators. */
767 S_do_chomp(pTHX_ SV *retval, SV *sv, bool chomping)
773 PERL_ARGS_ASSERT_DO_CHOMP;
775 if (chomping && (RsSNARF(PL_rs) || RsRECORD(PL_rs)))
777 if (SvTYPE(sv) == SVt_PVAV) {
779 AV *const av = MUTABLE_AV(sv);
780 const I32 max = AvFILL(av);
782 for (i = 0; i <= max; i++) {
783 sv = MUTABLE_SV(av_fetch(av, i, FALSE));
784 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
785 do_chomp(retval, sv, chomping);
789 else if (SvTYPE(sv) == SVt_PVHV) {
790 HV* const hv = MUTABLE_HV(sv);
792 (void)hv_iterinit(hv);
793 while ((entry = hv_iternext(hv)))
794 do_chomp(retval, hv_iterval(hv,entry), chomping);
797 else if (SvREADONLY(sv)) {
798 Perl_croak_no_modify();
800 else if (SvIsCOW(sv)) {
801 sv_force_normal_flags(sv, 0);
806 /* XXX, here sv is utf8-ized as a side-effect!
807 If encoding.pm is used properly, almost string-generating
808 operations, including literal strings, chr(), input data, etc.
809 should have been utf8-ized already, right?
811 sv_recode_to_utf8(sv, PL_encoding);
817 char *temp_buffer = NULL;
826 while (len && s[-1] == '\n') {
833 STRLEN rslen, rs_charlen;
834 const char *rsptr = SvPV_const(PL_rs, rslen);
836 rs_charlen = SvUTF8(PL_rs)
840 if (SvUTF8(PL_rs) != SvUTF8(sv)) {
841 /* Assumption is that rs is shorter than the scalar. */
843 /* RS is utf8, scalar is 8 bit. */
845 temp_buffer = (char*)bytes_from_utf8((U8*)rsptr,
848 /* Cannot downgrade, therefore cannot possibly match
850 assert (temp_buffer == rsptr);
856 else if (PL_encoding) {
857 /* RS is 8 bit, encoding.pm is used.
858 * Do not recode PL_rs as a side-effect. */
859 svrecode = newSVpvn(rsptr, rslen);
860 sv_recode_to_utf8(svrecode, PL_encoding);
861 rsptr = SvPV_const(svrecode, rslen);
862 rs_charlen = sv_len_utf8(svrecode);
865 /* RS is 8 bit, scalar is utf8. */
866 temp_buffer = (char*)bytes_to_utf8((U8*)rsptr, &rslen);
880 if (memNE(s, rsptr, rslen))
882 SvIVX(retval) += rs_charlen;
885 s = SvPV_force_nomg_nolen(sv);
893 SvREFCNT_dec(svrecode);
895 Safefree(temp_buffer);
897 if (len && !SvPOK(sv))
898 s = SvPV_force_nomg(sv, len);
901 char * const send = s + len;
902 char * const start = s;
904 while (s > start && UTF8_IS_CONTINUATION(*s))
906 if (is_utf8_string((U8*)s, send - s)) {
907 sv_setpvn(retval, s, send - s);
909 SvCUR_set(sv, s - start);
915 sv_setpvs(retval, "");
919 sv_setpvn(retval, s, 1);
926 sv_setpvs(retval, "");
934 const bool chomping = PL_op->op_type == OP_SCHOMP;
938 do_chomp(TARG, TOPs, chomping);
945 dVAR; dSP; dMARK; dTARGET; dORIGMARK;
946 const bool chomping = PL_op->op_type == OP_CHOMP;
951 do_chomp(TARG, *++MARK, chomping);
962 if (!PL_op->op_private) {
971 SV_CHECK_THINKFIRST_COW_DROP(sv);
973 switch (SvTYPE(sv)) {
977 av_undef(MUTABLE_AV(sv));
980 hv_undef(MUTABLE_HV(sv));
983 if (cv_const_sv((const CV *)sv))
984 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
985 "Constant subroutine %"SVf" undefined",
986 SVfARG(CvANON((const CV *)sv)
987 ? newSVpvs_flags("(anonymous)", SVs_TEMP)
988 : sv_2mortal(newSVhek(
990 ? CvNAME_HEK((CV *)sv)
991 : GvENAME_HEK(CvGV((const CV *)sv))
997 /* let user-undef'd sub keep its identity */
998 GV* const gv = CvGV((const CV *)sv);
999 HEK * const hek = CvNAME_HEK((CV *)sv);
1000 if (hek) share_hek_hek(hek);
1001 cv_undef(MUTABLE_CV(sv));
1002 if (gv) CvGV_set(MUTABLE_CV(sv), gv);
1004 SvANY((CV *)sv)->xcv_gv_u.xcv_hek = hek;
1010 assert(isGV_with_GP(sv));
1011 assert(!SvFAKE(sv));
1016 /* undef *Pkg::meth_name ... */
1018 = GvCVu((const GV *)sv) && (stash = GvSTASH((const GV *)sv))
1019 && HvENAME_get(stash);
1021 if((stash = GvHV((const GV *)sv))) {
1022 if(HvENAME_get(stash))
1023 SvREFCNT_inc_simple_void_NN(sv_2mortal((SV *)stash));
1027 gp_free(MUTABLE_GV(sv));
1029 GvGP_set(sv, gp_ref(gp));
1030 #ifndef PERL_DONT_CREATE_GVSV
1031 GvSV(sv) = newSV(0);
1033 GvLINE(sv) = CopLINE(PL_curcop);
1034 GvEGV(sv) = MUTABLE_GV(sv);
1038 mro_package_moved(NULL, stash, (const GV *)sv, 0);
1040 /* undef *Foo::ISA */
1041 if( strEQ(GvNAME((const GV *)sv), "ISA")
1042 && (stash = GvSTASH((const GV *)sv))
1043 && (method_changed || HvENAME(stash)) )
1044 mro_isa_changed_in(stash);
1045 else if(method_changed)
1046 mro_method_changed_in(
1047 GvSTASH((const GV *)sv)
1053 if (SvTYPE(sv) >= SVt_PV && SvPVX_const(sv) && SvLEN(sv)) {
1069 PL_op->op_type == OP_POSTINC || PL_op->op_type == OP_I_POSTINC;
1070 if (SvTYPE(TOPs) >= SVt_PVAV || (isGV_with_GP(TOPs) && !SvFAKE(TOPs)))
1071 Perl_croak_no_modify();
1073 TARG = sv_newmortal();
1074 sv_setsv(TARG, TOPs);
1075 if (!SvREADONLY(TOPs) && !SvGMAGICAL(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
1076 && SvIVX(TOPs) != (inc ? IV_MAX : IV_MIN))
1078 SvIV_set(TOPs, SvIVX(TOPs) + (inc ? 1 : -1));
1079 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
1083 else sv_dec_nomg(TOPs);
1085 /* special case for undef: see thread at 2003-03/msg00536.html in archive */
1086 if (inc && !SvOK(TARG))
1092 /* Ordinary operators. */
1096 dVAR; dSP; dATARGET; SV *svl, *svr;
1097 #ifdef PERL_PRESERVE_IVUV
1100 tryAMAGICbin_MG(pow_amg, AMGf_assign|AMGf_numeric);
1103 #ifdef PERL_PRESERVE_IVUV
1104 /* For integer to integer power, we do the calculation by hand wherever
1105 we're sure it is safe; otherwise we call pow() and try to convert to
1106 integer afterwards. */
1107 if (SvIV_please_nomg(svr) && SvIV_please_nomg(svl)) {
1115 const IV iv = SvIVX(svr);
1119 goto float_it; /* Can't do negative powers this way. */
1123 baseuok = SvUOK(svl);
1125 baseuv = SvUVX(svl);
1127 const IV iv = SvIVX(svl);
1130 baseuok = TRUE; /* effectively it's a UV now */
1132 baseuv = -iv; /* abs, baseuok == false records sign */
1135 /* now we have integer ** positive integer. */
1138 /* foo & (foo - 1) is zero only for a power of 2. */
1139 if (!(baseuv & (baseuv - 1))) {
1140 /* We are raising power-of-2 to a positive integer.
1141 The logic here will work for any base (even non-integer
1142 bases) but it can be less accurate than
1143 pow (base,power) or exp (power * log (base)) when the
1144 intermediate values start to spill out of the mantissa.
1145 With powers of 2 we know this can't happen.
1146 And powers of 2 are the favourite thing for perl
1147 programmers to notice ** not doing what they mean. */
1149 NV base = baseuok ? baseuv : -(NV)baseuv;
1154 while (power >>= 1) {
1162 SvIV_please_nomg(svr);
1165 unsigned int highbit = 8 * sizeof(UV);
1166 unsigned int diff = 8 * sizeof(UV);
1167 while (diff >>= 1) {
1169 if (baseuv >> highbit) {
1173 /* we now have baseuv < 2 ** highbit */
1174 if (power * highbit <= 8 * sizeof(UV)) {
1175 /* result will definitely fit in UV, so use UV math
1176 on same algorithm as above */
1179 const bool odd_power = cBOOL(power & 1);
1183 while (power >>= 1) {
1190 if (baseuok || !odd_power)
1191 /* answer is positive */
1193 else if (result <= (UV)IV_MAX)
1194 /* answer negative, fits in IV */
1195 SETi( -(IV)result );
1196 else if (result == (UV)IV_MIN)
1197 /* 2's complement assumption: special case IV_MIN */
1200 /* answer negative, doesn't fit */
1201 SETn( -(NV)result );
1209 NV right = SvNV_nomg(svr);
1210 NV left = SvNV_nomg(svl);
1213 #if defined(USE_LONG_DOUBLE) && defined(HAS_AIX_POWL_NEG_BASE_BUG)
1215 We are building perl with long double support and are on an AIX OS
1216 afflicted with a powl() function that wrongly returns NaNQ for any
1217 negative base. This was reported to IBM as PMR #23047-379 on
1218 03/06/2006. The problem exists in at least the following versions
1219 of AIX and the libm fileset, and no doubt others as well:
1221 AIX 4.3.3-ML10 bos.adt.libm 4.3.3.50
1222 AIX 5.1.0-ML04 bos.adt.libm 5.1.0.29
1223 AIX 5.2.0 bos.adt.libm 5.2.0.85
1225 So, until IBM fixes powl(), we provide the following workaround to
1226 handle the problem ourselves. Our logic is as follows: for
1227 negative bases (left), we use fmod(right, 2) to check if the
1228 exponent is an odd or even integer:
1230 - if odd, powl(left, right) == -powl(-left, right)
1231 - if even, powl(left, right) == powl(-left, right)
1233 If the exponent is not an integer, the result is rightly NaNQ, so
1234 we just return that (as NV_NAN).
1238 NV mod2 = Perl_fmod( right, 2.0 );
1239 if (mod2 == 1.0 || mod2 == -1.0) { /* odd integer */
1240 SETn( -Perl_pow( -left, right) );
1241 } else if (mod2 == 0.0) { /* even integer */
1242 SETn( Perl_pow( -left, right) );
1243 } else { /* fractional power */
1247 SETn( Perl_pow( left, right) );
1250 SETn( Perl_pow( left, right) );
1251 #endif /* HAS_AIX_POWL_NEG_BASE_BUG */
1253 #ifdef PERL_PRESERVE_IVUV
1255 SvIV_please_nomg(svr);
1263 dVAR; dSP; dATARGET; SV *svl, *svr;
1264 tryAMAGICbin_MG(mult_amg, AMGf_assign|AMGf_numeric);
1267 #ifdef PERL_PRESERVE_IVUV
1268 if (SvIV_please_nomg(svr)) {
1269 /* Unless the left argument is integer in range we are going to have to
1270 use NV maths. Hence only attempt to coerce the right argument if
1271 we know the left is integer. */
1272 /* Left operand is defined, so is it IV? */
1273 if (SvIV_please_nomg(svl)) {
1274 bool auvok = SvUOK(svl);
1275 bool buvok = SvUOK(svr);
1276 const UV topmask = (~ (UV)0) << (4 * sizeof (UV));
1277 const UV botmask = ~((~ (UV)0) << (4 * sizeof (UV)));
1286 const IV aiv = SvIVX(svl);
1289 auvok = TRUE; /* effectively it's a UV now */
1291 alow = -aiv; /* abs, auvok == false records sign */
1297 const IV biv = SvIVX(svr);
1300 buvok = TRUE; /* effectively it's a UV now */
1302 blow = -biv; /* abs, buvok == false records sign */
1306 /* If this does sign extension on unsigned it's time for plan B */
1307 ahigh = alow >> (4 * sizeof (UV));
1309 bhigh = blow >> (4 * sizeof (UV));
1311 if (ahigh && bhigh) {
1313 /* eg 32 bit is at least 0x10000 * 0x10000 == 0x100000000
1314 which is overflow. Drop to NVs below. */
1315 } else if (!ahigh && !bhigh) {
1316 /* eg 32 bit is at most 0xFFFF * 0xFFFF == 0xFFFE0001
1317 so the unsigned multiply cannot overflow. */
1318 const UV product = alow * blow;
1319 if (auvok == buvok) {
1320 /* -ve * -ve or +ve * +ve gives a +ve result. */
1324 } else if (product <= (UV)IV_MIN) {
1325 /* 2s complement assumption that (UV)-IV_MIN is correct. */
1326 /* -ve result, which could overflow an IV */
1328 SETi( -(IV)product );
1330 } /* else drop to NVs below. */
1332 /* One operand is large, 1 small */
1335 /* swap the operands */
1337 bhigh = blow; /* bhigh now the temp var for the swap */
1341 /* now, ((ahigh * blow) << half_UV_len) + (alow * blow)
1342 multiplies can't overflow. shift can, add can, -ve can. */
1343 product_middle = ahigh * blow;
1344 if (!(product_middle & topmask)) {
1345 /* OK, (ahigh * blow) won't lose bits when we shift it. */
1347 product_middle <<= (4 * sizeof (UV));
1348 product_low = alow * blow;
1350 /* as for pp_add, UV + something mustn't get smaller.
1351 IIRC ANSI mandates this wrapping *behaviour* for
1352 unsigned whatever the actual representation*/
1353 product_low += product_middle;
1354 if (product_low >= product_middle) {
1355 /* didn't overflow */
1356 if (auvok == buvok) {
1357 /* -ve * -ve or +ve * +ve gives a +ve result. */
1359 SETu( product_low );
1361 } else if (product_low <= (UV)IV_MIN) {
1362 /* 2s complement assumption again */
1363 /* -ve result, which could overflow an IV */
1365 SETi( -(IV)product_low );
1367 } /* else drop to NVs below. */
1369 } /* product_middle too large */
1370 } /* ahigh && bhigh */
1375 NV right = SvNV_nomg(svr);
1376 NV left = SvNV_nomg(svl);
1378 SETn( left * right );
1385 dVAR; dSP; dATARGET; SV *svl, *svr;
1386 tryAMAGICbin_MG(div_amg, AMGf_assign|AMGf_numeric);
1389 /* Only try to do UV divide first
1390 if ((SLOPPYDIVIDE is true) or
1391 (PERL_PRESERVE_IVUV is true and one or both SV is a UV too large
1393 The assumption is that it is better to use floating point divide
1394 whenever possible, only doing integer divide first if we can't be sure.
1395 If NV_PRESERVES_UV is true then we know at compile time that no UV
1396 can be too large to preserve, so don't need to compile the code to
1397 test the size of UVs. */
1400 # define PERL_TRY_UV_DIVIDE
1401 /* ensure that 20./5. == 4. */
1403 # ifdef PERL_PRESERVE_IVUV
1404 # ifndef NV_PRESERVES_UV
1405 # define PERL_TRY_UV_DIVIDE
1410 #ifdef PERL_TRY_UV_DIVIDE
1411 if (SvIV_please_nomg(svr) && SvIV_please_nomg(svl)) {
1412 bool left_non_neg = SvUOK(svl);
1413 bool right_non_neg = SvUOK(svr);
1417 if (right_non_neg) {
1421 const IV biv = SvIVX(svr);
1424 right_non_neg = TRUE; /* effectively it's a UV now */
1430 /* historically undef()/0 gives a "Use of uninitialized value"
1431 warning before dieing, hence this test goes here.
1432 If it were immediately before the second SvIV_please, then
1433 DIE() would be invoked before left was even inspected, so
1434 no inspection would give no warning. */
1436 DIE(aTHX_ "Illegal division by zero");
1442 const IV aiv = SvIVX(svl);
1445 left_non_neg = TRUE; /* effectively it's a UV now */
1454 /* For sloppy divide we always attempt integer division. */
1456 /* Otherwise we only attempt it if either or both operands
1457 would not be preserved by an NV. If both fit in NVs
1458 we fall through to the NV divide code below. However,
1459 as left >= right to ensure integer result here, we know that
1460 we can skip the test on the right operand - right big
1461 enough not to be preserved can't get here unless left is
1464 && (left > ((UV)1 << NV_PRESERVES_UV_BITS))
1467 /* Integer division can't overflow, but it can be imprecise. */
1468 const UV result = left / right;
1469 if (result * right == left) {
1470 SP--; /* result is valid */
1471 if (left_non_neg == right_non_neg) {
1472 /* signs identical, result is positive. */
1476 /* 2s complement assumption */
1477 if (result <= (UV)IV_MIN)
1478 SETi( -(IV)result );
1480 /* It's exact but too negative for IV. */
1481 SETn( -(NV)result );
1484 } /* tried integer divide but it was not an integer result */
1485 } /* else (PERL_ABS(result) < 1.0) or (both UVs in range for NV) */
1486 } /* one operand wasn't SvIOK */
1487 #endif /* PERL_TRY_UV_DIVIDE */
1489 NV right = SvNV_nomg(svr);
1490 NV left = SvNV_nomg(svl);
1491 (void)POPs;(void)POPs;
1492 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
1493 if (! Perl_isnan(right) && right == 0.0)
1497 DIE(aTHX_ "Illegal division by zero");
1498 PUSHn( left / right );
1505 dVAR; dSP; dATARGET;
1506 tryAMAGICbin_MG(modulo_amg, AMGf_assign|AMGf_numeric);
1510 bool left_neg = FALSE;
1511 bool right_neg = FALSE;
1512 bool use_double = FALSE;
1513 bool dright_valid = FALSE;
1516 SV * const svr = TOPs;
1517 SV * const svl = TOPm1s;
1518 if (SvIV_please_nomg(svr)) {
1519 right_neg = !SvUOK(svr);
1523 const IV biv = SvIVX(svr);
1526 right_neg = FALSE; /* effectively it's a UV now */
1533 dright = SvNV_nomg(svr);
1534 right_neg = dright < 0;
1537 if (dright < UV_MAX_P1) {
1538 right = U_V(dright);
1539 dright_valid = TRUE; /* In case we need to use double below. */
1545 /* At this point use_double is only true if right is out of range for
1546 a UV. In range NV has been rounded down to nearest UV and
1547 use_double false. */
1548 if (!use_double && SvIV_please_nomg(svl)) {
1549 left_neg = !SvUOK(svl);
1553 const IV aiv = SvIVX(svl);
1556 left_neg = FALSE; /* effectively it's a UV now */
1563 dleft = SvNV_nomg(svl);
1564 left_neg = dleft < 0;
1568 /* This should be exactly the 5.6 behaviour - if left and right are
1569 both in range for UV then use U_V() rather than floor. */
1571 if (dleft < UV_MAX_P1) {
1572 /* right was in range, so is dleft, so use UVs not double.
1576 /* left is out of range for UV, right was in range, so promote
1577 right (back) to double. */
1579 /* The +0.5 is used in 5.6 even though it is not strictly
1580 consistent with the implicit +0 floor in the U_V()
1581 inside the #if 1. */
1582 dleft = Perl_floor(dleft + 0.5);
1585 dright = Perl_floor(dright + 0.5);
1596 DIE(aTHX_ "Illegal modulus zero");
1598 dans = Perl_fmod(dleft, dright);
1599 if ((left_neg != right_neg) && dans)
1600 dans = dright - dans;
1603 sv_setnv(TARG, dans);
1609 DIE(aTHX_ "Illegal modulus zero");
1612 if ((left_neg != right_neg) && ans)
1615 /* XXX may warn: unary minus operator applied to unsigned type */
1616 /* could change -foo to be (~foo)+1 instead */
1617 if (ans <= ~((UV)IV_MAX)+1)
1618 sv_setiv(TARG, ~ans+1);
1620 sv_setnv(TARG, -(NV)ans);
1623 sv_setuv(TARG, ans);
1632 dVAR; dSP; dATARGET;
1636 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1637 /* TODO: think of some way of doing list-repeat overloading ??? */
1642 tryAMAGICbin_MG(repeat_amg, AMGf_assign);
1648 const UV uv = SvUV_nomg(sv);
1650 count = IV_MAX; /* The best we can do? */
1654 const IV iv = SvIV_nomg(sv);
1661 else if (SvNOKp(sv)) {
1662 const NV nv = SvNV_nomg(sv);
1669 count = SvIV_nomg(sv);
1671 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1673 static const char* const oom_list_extend = "Out of memory during list extend";
1674 const I32 items = SP - MARK;
1675 const I32 max = items * count;
1676 const U8 mod = PL_op->op_flags & OPf_MOD;
1678 MEM_WRAP_CHECK_1(max, SV*, oom_list_extend);
1679 /* Did the max computation overflow? */
1680 if (items > 0 && max > 0 && (max < items || max < count))
1681 Perl_croak(aTHX_ "%s", oom_list_extend);
1686 /* This code was intended to fix 20010809.028:
1689 for (($x =~ /./g) x 2) {
1690 print chop; # "abcdabcd" expected as output.
1693 * but that change (#11635) broke this code:
1695 $x = [("foo")x2]; # only one "foo" ended up in the anonlist.
1697 * I can't think of a better fix that doesn't introduce
1698 * an efficiency hit by copying the SVs. The stack isn't
1699 * refcounted, and mortalisation obviously doesn't
1700 * Do The Right Thing when the stack has more than
1701 * one pointer to the same mortal value.
1705 *SP = sv_2mortal(newSVsv(*SP));
1711 if (mod && SvPADTMP(*SP) && !IS_PADGV(*SP))
1712 *SP = sv_mortalcopy(*SP);
1719 repeatcpy((char*)(MARK + items), (char*)MARK,
1720 items * sizeof(const SV *), count - 1);
1723 else if (count <= 0)
1726 else { /* Note: mark already snarfed by pp_list */
1727 SV * const tmpstr = POPs;
1730 static const char* const oom_string_extend =
1731 "Out of memory during string extend";
1734 sv_setsv_nomg(TARG, tmpstr);
1735 SvPV_force_nomg(TARG, len);
1736 isutf = DO_UTF8(TARG);
1741 const STRLEN max = (UV)count * len;
1742 if (len > MEM_SIZE_MAX / count)
1743 Perl_croak(aTHX_ "%s", oom_string_extend);
1744 MEM_WRAP_CHECK_1(max, char, oom_string_extend);
1745 SvGROW(TARG, max + 1);
1746 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
1747 SvCUR_set(TARG, SvCUR(TARG) * count);
1749 *SvEND(TARG) = '\0';
1752 (void)SvPOK_only_UTF8(TARG);
1754 (void)SvPOK_only(TARG);
1756 if (PL_op->op_private & OPpREPEAT_DOLIST) {
1757 /* The parser saw this as a list repeat, and there
1758 are probably several items on the stack. But we're
1759 in scalar context, and there's no pp_list to save us
1760 now. So drop the rest of the items -- robin@kitsite.com
1772 dVAR; dSP; dATARGET; bool useleft; SV *svl, *svr;
1773 tryAMAGICbin_MG(subtr_amg, AMGf_assign|AMGf_numeric);
1776 useleft = USE_LEFT(svl);
1777 #ifdef PERL_PRESERVE_IVUV
1778 /* See comments in pp_add (in pp_hot.c) about Overflow, and how
1779 "bad things" happen if you rely on signed integers wrapping. */
1780 if (SvIV_please_nomg(svr)) {
1781 /* Unless the left argument is integer in range we are going to have to
1782 use NV maths. Hence only attempt to coerce the right argument if
1783 we know the left is integer. */
1790 a_valid = auvok = 1;
1791 /* left operand is undef, treat as zero. */
1793 /* Left operand is defined, so is it IV? */
1794 if (SvIV_please_nomg(svl)) {
1795 if ((auvok = SvUOK(svl)))
1798 const IV aiv = SvIVX(svl);
1801 auvok = 1; /* Now acting as a sign flag. */
1802 } else { /* 2s complement assumption for IV_MIN */
1810 bool result_good = 0;
1813 bool buvok = SvUOK(svr);
1818 const IV biv = SvIVX(svr);
1825 /* ?uvok if value is >= 0. basically, flagged as UV if it's +ve,
1826 else "IV" now, independent of how it came in.
1827 if a, b represents positive, A, B negative, a maps to -A etc
1832 all UV maths. negate result if A negative.
1833 subtract if signs same, add if signs differ. */
1835 if (auvok ^ buvok) {
1844 /* Must get smaller */
1849 if (result <= buv) {
1850 /* result really should be -(auv-buv). as its negation
1851 of true value, need to swap our result flag */
1863 if (result <= (UV)IV_MIN)
1864 SETi( -(IV)result );
1866 /* result valid, but out of range for IV. */
1867 SETn( -(NV)result );
1871 } /* Overflow, drop through to NVs. */
1876 NV value = SvNV_nomg(svr);
1880 /* left operand is undef, treat as zero - value */
1884 SETn( SvNV_nomg(svl) - value );
1891 dVAR; dSP; dATARGET; SV *svl, *svr;
1892 tryAMAGICbin_MG(lshift_amg, AMGf_assign|AMGf_numeric);
1896 const IV shift = SvIV_nomg(svr);
1897 if (PL_op->op_private & HINT_INTEGER) {
1898 const IV i = SvIV_nomg(svl);
1902 const UV u = SvUV_nomg(svl);
1911 dVAR; dSP; dATARGET; SV *svl, *svr;
1912 tryAMAGICbin_MG(rshift_amg, AMGf_assign|AMGf_numeric);
1916 const IV shift = SvIV_nomg(svr);
1917 if (PL_op->op_private & HINT_INTEGER) {
1918 const IV i = SvIV_nomg(svl);
1922 const UV u = SvUV_nomg(svl);
1934 tryAMAGICbin_MG(lt_amg, AMGf_set|AMGf_numeric);
1938 (SvIOK_notUV(left) && SvIOK_notUV(right))
1939 ? (SvIVX(left) < SvIVX(right))
1940 : (do_ncmp(left, right) == -1)
1950 tryAMAGICbin_MG(gt_amg, AMGf_set|AMGf_numeric);
1954 (SvIOK_notUV(left) && SvIOK_notUV(right))
1955 ? (SvIVX(left) > SvIVX(right))
1956 : (do_ncmp(left, right) == 1)
1966 tryAMAGICbin_MG(le_amg, AMGf_set|AMGf_numeric);
1970 (SvIOK_notUV(left) && SvIOK_notUV(right))
1971 ? (SvIVX(left) <= SvIVX(right))
1972 : (do_ncmp(left, right) <= 0)
1982 tryAMAGICbin_MG(ge_amg, AMGf_set|AMGf_numeric);
1986 (SvIOK_notUV(left) && SvIOK_notUV(right))
1987 ? (SvIVX(left) >= SvIVX(right))
1988 : ( (do_ncmp(left, right) & 2) == 0)
1998 tryAMAGICbin_MG(ne_amg, AMGf_set|AMGf_numeric);
2002 (SvIOK_notUV(left) && SvIOK_notUV(right))
2003 ? (SvIVX(left) != SvIVX(right))
2004 : (do_ncmp(left, right) != 0)
2009 /* compare left and right SVs. Returns:
2013 * 2: left or right was a NaN
2016 Perl_do_ncmp(pTHX_ SV* const left, SV * const right)
2020 PERL_ARGS_ASSERT_DO_NCMP;
2021 #ifdef PERL_PRESERVE_IVUV
2022 /* Fortunately it seems NaN isn't IOK */
2023 if (SvIV_please_nomg(right) && SvIV_please_nomg(left)) {
2025 const IV leftiv = SvIVX(left);
2026 if (!SvUOK(right)) {
2027 /* ## IV <=> IV ## */
2028 const IV rightiv = SvIVX(right);
2029 return (leftiv > rightiv) - (leftiv < rightiv);
2031 /* ## IV <=> UV ## */
2033 /* As (b) is a UV, it's >=0, so it must be < */
2036 const UV rightuv = SvUVX(right);
2037 return ((UV)leftiv > rightuv) - ((UV)leftiv < rightuv);
2042 /* ## UV <=> UV ## */
2043 const UV leftuv = SvUVX(left);
2044 const UV rightuv = SvUVX(right);
2045 return (leftuv > rightuv) - (leftuv < rightuv);
2047 /* ## UV <=> IV ## */
2049 const IV rightiv = SvIVX(right);
2051 /* As (a) is a UV, it's >=0, so it cannot be < */
2054 const UV leftuv = SvUVX(left);
2055 return (leftuv > (UV)rightiv) - (leftuv < (UV)rightiv);
2058 assert(0); /* NOTREACHED */
2062 NV const rnv = SvNV_nomg(right);
2063 NV const lnv = SvNV_nomg(left);
2065 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
2066 if (Perl_isnan(lnv) || Perl_isnan(rnv)) {
2069 return (lnv > rnv) - (lnv < rnv);
2088 tryAMAGICbin_MG(ncmp_amg, AMGf_numeric);
2091 value = do_ncmp(left, right);
2106 int amg_type = sle_amg;
2110 switch (PL_op->op_type) {
2129 tryAMAGICbin_MG(amg_type, AMGf_set);
2132 const int cmp = (IN_LOCALE_RUNTIME
2133 ? sv_cmp_locale_flags(left, right, 0)
2134 : sv_cmp_flags(left, right, 0));
2135 SETs(boolSV(cmp * multiplier < rhs));
2143 tryAMAGICbin_MG(seq_amg, AMGf_set);
2146 SETs(boolSV(sv_eq_flags(left, right, 0)));
2154 tryAMAGICbin_MG(sne_amg, AMGf_set);
2157 SETs(boolSV(!sv_eq_flags(left, right, 0)));
2165 tryAMAGICbin_MG(scmp_amg, 0);
2168 const int cmp = (IN_LOCALE_RUNTIME
2169 ? sv_cmp_locale_flags(left, right, 0)
2170 : sv_cmp_flags(left, right, 0));
2178 dVAR; dSP; dATARGET;
2179 tryAMAGICbin_MG(band_amg, AMGf_assign);
2182 if (SvNIOKp(left) || SvNIOKp(right)) {
2183 const bool left_ro_nonnum = !SvNIOKp(left) && SvREADONLY(left);
2184 const bool right_ro_nonnum = !SvNIOKp(right) && SvREADONLY(right);
2185 if (PL_op->op_private & HINT_INTEGER) {
2186 const IV i = SvIV_nomg(left) & SvIV_nomg(right);
2190 const UV u = SvUV_nomg(left) & SvUV_nomg(right);
2193 if (left_ro_nonnum && left != TARG) SvNIOK_off(left);
2194 if (right_ro_nonnum) SvNIOK_off(right);
2197 do_vop(PL_op->op_type, TARG, left, right);
2206 dVAR; dSP; dATARGET;
2207 const int op_type = PL_op->op_type;
2209 tryAMAGICbin_MG((op_type == OP_BIT_OR ? bor_amg : bxor_amg), AMGf_assign);
2212 if (SvNIOKp(left) || SvNIOKp(right)) {
2213 const bool left_ro_nonnum = !SvNIOKp(left) && SvREADONLY(left);
2214 const bool right_ro_nonnum = !SvNIOKp(right) && SvREADONLY(right);
2215 if (PL_op->op_private & HINT_INTEGER) {
2216 const IV l = (USE_LEFT(left) ? SvIV_nomg(left) : 0);
2217 const IV r = SvIV_nomg(right);
2218 const IV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2222 const UV l = (USE_LEFT(left) ? SvUV_nomg(left) : 0);
2223 const UV r = SvUV_nomg(right);
2224 const UV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2227 if (left_ro_nonnum && left != TARG) SvNIOK_off(left);
2228 if (right_ro_nonnum) SvNIOK_off(right);
2231 do_vop(op_type, TARG, left, right);
2238 PERL_STATIC_INLINE bool
2239 S_negate_string(pTHX)
2244 SV * const sv = TOPs;
2245 if (!SvPOKp(sv) || SvNIOK(sv) || (!SvPOK(sv) && SvNIOKp(sv)))
2247 s = SvPV_nomg_const(sv, len);
2248 if (isIDFIRST(*s)) {
2249 sv_setpvs(TARG, "-");
2252 else if (*s == '+' || (*s == '-' && !looks_like_number(sv))) {
2253 sv_setsv_nomg(TARG, sv);
2254 *SvPV_force_nomg(TARG, len) = *s == '-' ? '+' : '-';
2264 tryAMAGICun_MG(neg_amg, AMGf_numeric);
2265 if (S_negate_string(aTHX)) return NORMAL;
2267 SV * const sv = TOPs;
2270 /* It's publicly an integer */
2273 if (SvIVX(sv) == IV_MIN) {
2274 /* 2s complement assumption. */
2275 SETi(SvIVX(sv)); /* special case: -((UV)IV_MAX+1) ==
2279 else if (SvUVX(sv) <= IV_MAX) {
2284 else if (SvIVX(sv) != IV_MIN) {
2288 #ifdef PERL_PRESERVE_IVUV
2295 if (SvNIOKp(sv) && (SvNIOK(sv) || !SvPOK(sv)))
2296 SETn(-SvNV_nomg(sv));
2297 else if (SvPOKp(sv) && SvIV_please_nomg(sv))
2298 goto oops_its_an_int;
2300 SETn(-SvNV_nomg(sv));
2308 tryAMAGICun_MG(not_amg, AMGf_set);
2309 *PL_stack_sp = boolSV(!SvTRUE_nomg(*PL_stack_sp));
2316 tryAMAGICun_MG(compl_amg, AMGf_numeric);
2320 if (PL_op->op_private & HINT_INTEGER) {
2321 const IV i = ~SvIV_nomg(sv);
2325 const UV u = ~SvUV_nomg(sv);
2334 sv_copypv_nomg(TARG, sv);
2335 tmps = (U8*)SvPV_nomg(TARG, len);
2338 /* Calculate exact length, let's not estimate. */
2343 U8 * const send = tmps + len;
2344 U8 * const origtmps = tmps;
2345 const UV utf8flags = UTF8_ALLOW_ANYUV;
2347 while (tmps < send) {
2348 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2350 targlen += UNISKIP(~c);
2356 /* Now rewind strings and write them. */
2363 Newx(result, targlen + 1, U8);
2365 while (tmps < send) {
2366 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2368 p = uvchr_to_utf8_flags(p, ~c, UNICODE_ALLOW_ANY);
2371 sv_usepvn_flags(TARG, (char*)result, targlen,
2372 SV_HAS_TRAILING_NUL);
2379 Newx(result, nchar + 1, U8);
2381 while (tmps < send) {
2382 const U8 c = (U8)utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2387 sv_usepvn_flags(TARG, (char*)result, nchar, SV_HAS_TRAILING_NUL);
2396 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
2399 for ( ; anum >= (I32)sizeof(long); anum -= (I32)sizeof(long), tmpl++)
2404 for ( ; anum > 0; anum--, tmps++)
2412 /* integer versions of some of the above */
2416 dVAR; dSP; dATARGET;
2417 tryAMAGICbin_MG(mult_amg, AMGf_assign);
2420 SETi( left * right );
2428 dVAR; dSP; dATARGET;
2429 tryAMAGICbin_MG(div_amg, AMGf_assign);
2432 IV value = SvIV_nomg(right);
2434 DIE(aTHX_ "Illegal division by zero");
2435 num = SvIV_nomg(left);
2437 /* avoid FPE_INTOVF on some platforms when num is IV_MIN */
2441 value = num / value;
2447 #if defined(__GLIBC__) && IVSIZE == 8 && !defined(PERL_DEBUG_READONLY_OPS)
2454 /* This is the vanilla old i_modulo. */
2455 dVAR; dSP; dATARGET;
2456 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2460 DIE(aTHX_ "Illegal modulus zero");
2461 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2465 SETi( left % right );
2470 #if defined(__GLIBC__) && IVSIZE == 8 && !defined(PERL_DEBUG_READONLY_OPS)
2475 /* This is the i_modulo with the workaround for the _moddi3 bug
2476 * in (at least) glibc 2.2.5 (the PERL_ABS() the workaround).
2477 * See below for pp_i_modulo. */
2478 dVAR; dSP; dATARGET;
2479 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2483 DIE(aTHX_ "Illegal modulus zero");
2484 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2488 SETi( left % PERL_ABS(right) );
2495 dVAR; dSP; dATARGET;
2496 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2500 DIE(aTHX_ "Illegal modulus zero");
2501 /* The assumption is to use hereafter the old vanilla version... */
2503 PL_ppaddr[OP_I_MODULO] =
2505 /* .. but if we have glibc, we might have a buggy _moddi3
2506 * (at least glicb 2.2.5 is known to have this bug), in other
2507 * words our integer modulus with negative quad as the second
2508 * argument might be broken. Test for this and re-patch the
2509 * opcode dispatch table if that is the case, remembering to
2510 * also apply the workaround so that this first round works
2511 * right, too. See [perl #9402] for more information. */
2515 /* Cannot do this check with inlined IV constants since
2516 * that seems to work correctly even with the buggy glibc. */
2518 /* Yikes, we have the bug.
2519 * Patch in the workaround version. */
2521 PL_ppaddr[OP_I_MODULO] =
2522 &Perl_pp_i_modulo_1;
2523 /* Make certain we work right this time, too. */
2524 right = PERL_ABS(right);
2527 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2531 SETi( left % right );
2539 dVAR; dSP; dATARGET;
2540 tryAMAGICbin_MG(add_amg, AMGf_assign);
2542 dPOPTOPiirl_ul_nomg;
2543 SETi( left + right );
2550 dVAR; dSP; dATARGET;
2551 tryAMAGICbin_MG(subtr_amg, AMGf_assign);
2553 dPOPTOPiirl_ul_nomg;
2554 SETi( left - right );
2562 tryAMAGICbin_MG(lt_amg, AMGf_set);
2565 SETs(boolSV(left < right));
2573 tryAMAGICbin_MG(gt_amg, AMGf_set);
2576 SETs(boolSV(left > right));
2584 tryAMAGICbin_MG(le_amg, AMGf_set);
2587 SETs(boolSV(left <= right));
2595 tryAMAGICbin_MG(ge_amg, AMGf_set);
2598 SETs(boolSV(left >= right));
2606 tryAMAGICbin_MG(eq_amg, AMGf_set);
2609 SETs(boolSV(left == right));
2617 tryAMAGICbin_MG(ne_amg, AMGf_set);
2620 SETs(boolSV(left != right));
2628 tryAMAGICbin_MG(ncmp_amg, 0);
2635 else if (left < right)
2647 tryAMAGICun_MG(neg_amg, 0);
2648 if (S_negate_string(aTHX)) return NORMAL;
2650 SV * const sv = TOPs;
2651 IV const i = SvIV_nomg(sv);
2657 /* High falutin' math. */
2662 tryAMAGICbin_MG(atan2_amg, 0);
2665 SETn(Perl_atan2(left, right));
2673 int amg_type = sin_amg;
2674 const char *neg_report = NULL;
2675 NV (*func)(NV) = Perl_sin;
2676 const int op_type = PL_op->op_type;
2693 amg_type = sqrt_amg;
2695 neg_report = "sqrt";
2700 tryAMAGICun_MG(amg_type, 0);
2702 SV * const arg = POPs;
2703 const NV value = SvNV_nomg(arg);
2705 if (op_type == OP_LOG ? (value <= 0.0) : (value < 0.0)) {
2706 SET_NUMERIC_STANDARD();
2707 /* diag_listed_as: Can't take log of %g */
2708 DIE(aTHX_ "Can't take %s of %"NVgf, neg_report, value);
2711 XPUSHn(func(value));
2716 /* Support Configure command-line overrides for rand() functions.
2717 After 5.005, perhaps we should replace this by Configure support
2718 for drand48(), random(), or rand(). For 5.005, though, maintain
2719 compatibility by calling rand() but allow the user to override it.
2720 See INSTALL for details. --Andy Dougherty 15 July 1998
2722 /* Now it's after 5.005, and Configure supports drand48() and random(),
2723 in addition to rand(). So the overrides should not be needed any more.
2724 --Jarkko Hietaniemi 27 September 1998
2730 if (!PL_srand_called) {
2731 (void)seedDrand01((Rand_seed_t)seed());
2732 PL_srand_called = TRUE;
2742 SV * const sv = POPs;
2748 /* 1 of 2 things can be carried through SvNV, SP or TARG, SP was carried */
2756 sv_setnv_mg(TARG, value);
2767 if (MAXARG >= 1 && (TOPs || POPs)) {
2774 pv = SvPV(top, len);
2775 flags = grok_number(pv, len, &anum);
2777 if (!(flags & IS_NUMBER_IN_UV)) {
2778 Perl_ck_warner_d(aTHX_ packWARN(WARN_OVERFLOW),
2779 "Integer overflow in srand");
2787 (void)seedDrand01((Rand_seed_t)anum);
2788 PL_srand_called = TRUE;
2792 /* Historically srand always returned true. We can avoid breaking
2794 sv_setpvs(TARG, "0 but true");
2803 tryAMAGICun_MG(int_amg, AMGf_numeric);
2805 SV * const sv = TOPs;
2806 const IV iv = SvIV_nomg(sv);
2807 /* XXX it's arguable that compiler casting to IV might be subtly
2808 different from modf (for numbers inside (IV_MIN,UV_MAX)) in which
2809 else preferring IV has introduced a subtle behaviour change bug. OTOH
2810 relying on floating point to be accurate is a bug. */
2815 else if (SvIOK(sv)) {
2817 SETu(SvUV_nomg(sv));
2822 const NV value = SvNV_nomg(sv);
2824 if (value < (NV)UV_MAX + 0.5) {
2827 SETn(Perl_floor(value));
2831 if (value > (NV)IV_MIN - 0.5) {
2834 SETn(Perl_ceil(value));
2845 tryAMAGICun_MG(abs_amg, AMGf_numeric);
2847 SV * const sv = TOPs;
2848 /* This will cache the NV value if string isn't actually integer */
2849 const IV iv = SvIV_nomg(sv);
2854 else if (SvIOK(sv)) {
2855 /* IVX is precise */
2857 SETu(SvUV_nomg(sv)); /* force it to be numeric only */
2865 /* 2s complement assumption. Also, not really needed as
2866 IV_MIN and -IV_MIN should both be %100...00 and NV-able */
2872 const NV value = SvNV_nomg(sv);
2886 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES;
2890 SV* const sv = POPs;
2892 tmps = (SvPV_const(sv, len));
2894 /* If Unicode, try to downgrade
2895 * If not possible, croak. */
2896 SV* const tsv = sv_2mortal(newSVsv(sv));
2899 sv_utf8_downgrade(tsv, FALSE);
2900 tmps = SvPV_const(tsv, len);
2902 if (PL_op->op_type == OP_HEX)
2905 while (*tmps && len && isSPACE(*tmps))
2909 if (*tmps == 'x' || *tmps == 'X') {
2911 result_uv = grok_hex (tmps, &len, &flags, &result_nv);
2913 else if (*tmps == 'b' || *tmps == 'B')
2914 result_uv = grok_bin (tmps, &len, &flags, &result_nv);
2916 result_uv = grok_oct (tmps, &len, &flags, &result_nv);
2918 if (flags & PERL_SCAN_GREATER_THAN_UV_MAX) {
2932 SV * const sv = TOPs;
2937 SETi(sv_len_utf8_nomg(sv));
2941 (void)SvPV_nomg_const(sv,len);
2945 if (!SvPADTMP(TARG)) {
2946 sv_setsv_nomg(TARG, &PL_sv_undef);
2954 /* Returns false if substring is completely outside original string.
2955 No length is indicated by len_iv = 0 and len_is_uv = 0. len_is_uv must
2956 always be true for an explicit 0.
2959 Perl_translate_substr_offsets(pTHX_ STRLEN curlen, IV pos1_iv,
2960 bool pos1_is_uv, IV len_iv,
2961 bool len_is_uv, STRLEN *posp,
2967 PERL_ARGS_ASSERT_TRANSLATE_SUBSTR_OFFSETS;
2969 if (!pos1_is_uv && pos1_iv < 0 && curlen) {
2970 pos1_is_uv = curlen-1 > ~(UV)pos1_iv;
2973 if ((pos1_is_uv || pos1_iv > 0) && (UV)pos1_iv > curlen)
2976 if (len_iv || len_is_uv) {
2977 if (!len_is_uv && len_iv < 0) {
2978 pos2_iv = curlen + len_iv;
2980 pos2_is_uv = curlen-1 > ~(UV)len_iv;
2983 } else { /* len_iv >= 0 */
2984 if (!pos1_is_uv && pos1_iv < 0) {
2985 pos2_iv = pos1_iv + len_iv;
2986 pos2_is_uv = (UV)len_iv > (UV)IV_MAX;
2988 if ((UV)len_iv > curlen-(UV)pos1_iv)
2991 pos2_iv = pos1_iv+len_iv;
3001 if (!pos2_is_uv && pos2_iv < 0) {
3002 if (!pos1_is_uv && pos1_iv < 0)
3006 else if (!pos1_is_uv && pos1_iv < 0)
3009 if ((UV)pos2_iv < (UV)pos1_iv)
3011 if ((UV)pos2_iv > curlen)
3014 /* pos1_iv and pos2_iv both in 0..curlen, so the cast is safe */
3015 *posp = (STRLEN)( (UV)pos1_iv );
3016 *lenp = (STRLEN)( (UV)pos2_iv - (UV)pos1_iv );
3033 I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
3034 const bool rvalue = (GIMME_V != G_VOID);
3037 const char *repl = NULL;
3039 int num_args = PL_op->op_private & 7;
3040 bool repl_need_utf8_upgrade = FALSE;
3044 if(!(repl_sv = POPs)) num_args--;
3046 if ((len_sv = POPs)) {
3047 len_iv = SvIV(len_sv);
3048 len_is_uv = len_iv ? SvIOK_UV(len_sv) : 1;
3053 pos1_iv = SvIV(pos_sv);
3054 pos1_is_uv = SvIOK_UV(pos_sv);
3056 if (PL_op->op_private & OPpSUBSTR_REPL_FIRST) {
3061 if (lvalue && !repl_sv) {
3063 ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
3064 sv_magic(ret, NULL, PERL_MAGIC_substr, NULL, 0);
3066 LvTARG(ret) = SvREFCNT_inc_simple(sv);
3068 pos1_is_uv || pos1_iv >= 0
3069 ? (STRLEN)(UV)pos1_iv
3070 : (LvFLAGS(ret) |= 1, (STRLEN)(UV)-pos1_iv);
3072 len_is_uv || len_iv > 0
3073 ? (STRLEN)(UV)len_iv
3074 : (LvFLAGS(ret) |= 2, (STRLEN)(UV)-len_iv);
3077 PUSHs(ret); /* avoid SvSETMAGIC here */
3081 repl = SvPV_const(repl_sv, repl_len);
3084 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR),
3085 "Attempt to use reference as lvalue in substr"
3087 tmps = SvPV_force_nomg(sv, curlen);
3088 if (DO_UTF8(repl_sv) && repl_len) {
3090 sv_utf8_upgrade_nomg(sv);
3094 else if (DO_UTF8(sv))
3095 repl_need_utf8_upgrade = TRUE;
3097 else tmps = SvPV_const(sv, curlen);
3099 utf8_curlen = sv_or_pv_len_utf8(sv, tmps, curlen);
3100 if (utf8_curlen == curlen)
3103 curlen = utf8_curlen;
3109 STRLEN pos, len, byte_len, byte_pos;
3111 if (!translate_substr_offsets(
3112 curlen, pos1_iv, pos1_is_uv, len_iv, len_is_uv, &pos, &len
3116 byte_pos = utf8_curlen
3117 ? sv_or_pv_pos_u2b(sv, tmps, pos, &byte_len) : pos;
3122 SvTAINTED_off(TARG); /* decontaminate */
3123 SvUTF8_off(TARG); /* decontaminate */
3124 sv_setpvn(TARG, tmps, byte_len);
3125 #ifdef USE_LOCALE_COLLATE
3126 sv_unmagic(TARG, PERL_MAGIC_collxfrm);
3133 SV* repl_sv_copy = NULL;
3135 if (repl_need_utf8_upgrade) {
3136 repl_sv_copy = newSVsv(repl_sv);
3137 sv_utf8_upgrade(repl_sv_copy);
3138 repl = SvPV_const(repl_sv_copy, repl_len);
3142 sv_insert_flags(sv, byte_pos, byte_len, repl, repl_len, 0);
3143 SvREFCNT_dec(repl_sv_copy);
3155 Perl_croak(aTHX_ "substr outside of string");
3156 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR), "substr outside of string");
3163 const IV size = POPi;
3164 const IV offset = POPi;
3165 SV * const src = POPs;
3166 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
3169 if (lvalue) { /* it's an lvalue! */
3170 ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
3171 sv_magic(ret, NULL, PERL_MAGIC_vec, NULL, 0);
3173 LvTARG(ret) = SvREFCNT_inc_simple(src);
3174 LvTARGOFF(ret) = offset;
3175 LvTARGLEN(ret) = size;
3179 SvTAINTED_off(TARG); /* decontaminate */
3183 sv_setuv(ret, do_vecget(src, offset, size));
3199 const char *little_p;
3202 const bool is_index = PL_op->op_type == OP_INDEX;
3203 const bool threeargs = MAXARG >= 3 && (TOPs || ((void)POPs,0));
3209 big_p = SvPV_const(big, biglen);
3210 little_p = SvPV_const(little, llen);
3212 big_utf8 = DO_UTF8(big);
3213 little_utf8 = DO_UTF8(little);
3214 if (big_utf8 ^ little_utf8) {
3215 /* One needs to be upgraded. */
3216 if (little_utf8 && !PL_encoding) {
3217 /* Well, maybe instead we might be able to downgrade the small
3219 char * const pv = (char*)bytes_from_utf8((U8 *)little_p, &llen,
3222 /* If the large string is ISO-8859-1, and it's not possible to
3223 convert the small string to ISO-8859-1, then there is no
3224 way that it could be found anywhere by index. */
3229 /* At this point, pv is a malloc()ed string. So donate it to temp
3230 to ensure it will get free()d */
3231 little = temp = newSV(0);
3232 sv_usepvn(temp, pv, llen);
3233 little_p = SvPVX(little);
3236 ? newSVpvn(big_p, biglen) : newSVpvn(little_p, llen);
3239 sv_recode_to_utf8(temp, PL_encoding);
3241 sv_utf8_upgrade(temp);
3246 big_p = SvPV_const(big, biglen);
3249 little_p = SvPV_const(little, llen);
3253 if (SvGAMAGIC(big)) {
3254 /* Life just becomes a lot easier if I use a temporary here.
3255 Otherwise I need to avoid calls to sv_pos_u2b(), which (dangerously)
3256 will trigger magic and overloading again, as will fbm_instr()
3258 big = newSVpvn_flags(big_p, biglen,
3259 SVs_TEMP | (big_utf8 ? SVf_UTF8 : 0));
3262 if (SvGAMAGIC(little) || (is_index && !SvOK(little))) {
3263 /* index && SvOK() is a hack. fbm_instr() calls SvPV_const, which will
3264 warn on undef, and we've already triggered a warning with the
3265 SvPV_const some lines above. We can't remove that, as we need to
3266 call some SvPV to trigger overloading early and find out if the
3268 This is all getting to messy. The API isn't quite clean enough,
3269 because data access has side effects.
3271 little = newSVpvn_flags(little_p, llen,
3272 SVs_TEMP | (little_utf8 ? SVf_UTF8 : 0));
3273 little_p = SvPVX(little);
3277 offset = is_index ? 0 : biglen;
3279 if (big_utf8 && offset > 0)
3280 sv_pos_u2b(big, &offset, 0);
3286 else if (offset > (I32)biglen)
3288 if (!(little_p = is_index
3289 ? fbm_instr((unsigned char*)big_p + offset,
3290 (unsigned char*)big_p + biglen, little, 0)
3291 : rninstr(big_p, big_p + offset,
3292 little_p, little_p + llen)))
3295 retval = little_p - big_p;
3296 if (retval > 0 && big_utf8)
3297 sv_pos_b2u(big, &retval);
3307 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
3308 SvTAINTED_off(TARG);
3309 do_sprintf(TARG, SP-MARK, MARK+1);
3310 TAINT_IF(SvTAINTED(TARG));
3322 const U8 *s = (U8*)SvPV_const(argsv, len);
3324 if (PL_encoding && SvPOK(argsv) && !DO_UTF8(argsv)) {
3325 SV * const tmpsv = sv_2mortal(newSVsv(argsv));
3326 s = (U8*)sv_recode_to_utf8(tmpsv, PL_encoding);
3330 XPUSHu(DO_UTF8(argsv)
3331 ? utf8n_to_uvchr(s, UTF8_MAXBYTES, 0, UTF8_ALLOW_ANYUV)
3345 if (!IN_BYTES /* under bytes, chr(-1) eq chr(0xff), etc. */
3346 && ((SvIOKp(top) && !SvIsUV(top) && SvIV_nomg(top) < 0)
3348 ((SvNOKp(top) || (SvOK(top) && !SvIsUV(top)))
3349 && SvNV_nomg(top) < 0.0))) {
3350 if (ckWARN(WARN_UTF8)) {
3351 if (SvGMAGICAL(top)) {
3352 SV *top2 = sv_newmortal();
3353 sv_setsv_nomg(top2, top);
3356 Perl_warner(aTHX_ packWARN(WARN_UTF8),
3357 "Invalid negative number (%"SVf") in chr", top);
3359 value = UNICODE_REPLACEMENT;
3361 value = SvUV_nomg(top);
3364 SvUPGRADE(TARG,SVt_PV);
3366 if (value > 255 && !IN_BYTES) {
3367 SvGROW(TARG, (STRLEN)UNISKIP(value)+1);
3368 tmps = (char*)uvchr_to_utf8_flags((U8*)SvPVX(TARG), value, 0);
3369 SvCUR_set(TARG, tmps - SvPVX_const(TARG));
3371 (void)SvPOK_only(TARG);
3380 *tmps++ = (char)value;
3382 (void)SvPOK_only(TARG);
3384 if (PL_encoding && !IN_BYTES) {
3385 sv_recode_to_utf8(TARG, PL_encoding);
3387 if (SvCUR(TARG) == 0
3388 || ! is_utf8_string((U8*)tmps, SvCUR(TARG))
3389 || UTF8_IS_REPLACEMENT((U8*) tmps, (U8*) tmps + SvCUR(TARG)))
3394 *tmps++ = (char)value;
3410 const char *tmps = SvPV_const(left, len);
3412 if (DO_UTF8(left)) {
3413 /* If Unicode, try to downgrade.
3414 * If not possible, croak.
3415 * Yes, we made this up. */
3416 SV* const tsv = sv_2mortal(newSVsv(left));
3419 sv_utf8_downgrade(tsv, FALSE);
3420 tmps = SvPV_const(tsv, len);
3422 # ifdef USE_ITHREADS
3424 if (!PL_reentrant_buffer->_crypt_struct_buffer) {
3425 /* This should be threadsafe because in ithreads there is only
3426 * one thread per interpreter. If this would not be true,
3427 * we would need a mutex to protect this malloc. */
3428 PL_reentrant_buffer->_crypt_struct_buffer =
3429 (struct crypt_data *)safemalloc(sizeof(struct crypt_data));
3430 #if defined(__GLIBC__) || defined(__EMX__)
3431 if (PL_reentrant_buffer->_crypt_struct_buffer) {
3432 PL_reentrant_buffer->_crypt_struct_buffer->initialized = 0;
3433 /* work around glibc-2.2.5 bug */
3434 PL_reentrant_buffer->_crypt_struct_buffer->current_saltbits = 0;
3438 # endif /* HAS_CRYPT_R */
3439 # endif /* USE_ITHREADS */
3441 sv_setpv(TARG, fcrypt(tmps, SvPV_nolen_const(right)));
3443 sv_setpv(TARG, PerlProc_crypt(tmps, SvPV_nolen_const(right)));
3449 "The crypt() function is unimplemented due to excessive paranoia.");
3453 /* Generally UTF-8 and UTF-EBCDIC are indistinguishable at this level. So
3454 * most comments below say UTF-8, when in fact they mean UTF-EBCDIC as well */
3458 /* Actually is both lcfirst() and ucfirst(). Only the first character
3459 * changes. This means that possibly we can change in-place, ie., just
3460 * take the source and change that one character and store it back, but not
3461 * if read-only etc, or if the length changes */
3466 STRLEN slen; /* slen is the byte length of the whole SV. */
3469 bool inplace; /* ? Convert first char only, in-place */
3470 bool doing_utf8 = FALSE; /* ? using utf8 */
3471 bool convert_source_to_utf8 = FALSE; /* ? need to convert */
3472 const int op_type = PL_op->op_type;
3475 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
3476 STRLEN ulen; /* ulen is the byte length of the original Unicode character
3477 * stored as UTF-8 at s. */
3478 STRLEN tculen; /* tculen is the byte length of the freshly titlecased (or
3479 * lowercased) character stored in tmpbuf. May be either
3480 * UTF-8 or not, but in either case is the number of bytes */
3481 bool tainted = FALSE;
3485 s = (const U8*)SvPV_nomg_const(source, slen);
3487 if (ckWARN(WARN_UNINITIALIZED))
3488 report_uninit(source);
3493 /* We may be able to get away with changing only the first character, in
3494 * place, but not if read-only, etc. Later we may discover more reasons to
3495 * not convert in-place. */
3496 inplace = SvPADTMP(source) && !SvREADONLY(source) && SvTEMP(source);
3498 /* First calculate what the changed first character should be. This affects
3499 * whether we can just swap it out, leaving the rest of the string unchanged,
3500 * or even if have to convert the dest to UTF-8 when the source isn't */
3502 if (! slen) { /* If empty */
3503 need = 1; /* still need a trailing NUL */
3506 else if (DO_UTF8(source)) { /* Is the source utf8? */
3509 if (op_type == OP_UCFIRST) {
3510 _to_utf8_title_flags(s, tmpbuf, &tculen,
3511 cBOOL(IN_LOCALE_RUNTIME), &tainted);
3514 _to_utf8_lower_flags(s, tmpbuf, &tculen,
3515 cBOOL(IN_LOCALE_RUNTIME), &tainted);
3518 /* we can't do in-place if the length changes. */
3519 if (ulen != tculen) inplace = FALSE;
3520 need = slen + 1 - ulen + tculen;
3522 else { /* Non-zero length, non-UTF-8, Need to consider locale and if
3523 * latin1 is treated as caseless. Note that a locale takes
3525 ulen = 1; /* Original character is 1 byte */
3526 tculen = 1; /* Most characters will require one byte, but this will
3527 * need to be overridden for the tricky ones */
3530 if (op_type == OP_LCFIRST) {
3532 /* lower case the first letter: no trickiness for any character */
3533 *tmpbuf = (IN_LOCALE_RUNTIME) ? toLOWER_LC(*s) :
3534 ((IN_UNI_8_BIT) ? toLOWER_LATIN1(*s) : toLOWER(*s));
3537 else if (IN_LOCALE_RUNTIME) {
3538 *tmpbuf = toUPPER_LC(*s); /* This would be a bug if any locales
3539 * have upper and title case different
3542 else if (! IN_UNI_8_BIT) {
3543 *tmpbuf = toUPPER(*s); /* Returns caseless for non-ascii, or
3544 * on EBCDIC machines whatever the
3545 * native function does */
3547 else { /* is ucfirst non-UTF-8, not in locale, and cased latin1 */
3548 UV title_ord = _to_upper_title_latin1(*s, tmpbuf, &tculen, 's');
3550 assert(tculen == 2);
3552 /* If the result is an upper Latin1-range character, it can
3553 * still be represented in one byte, which is its ordinal */
3554 if (UTF8_IS_DOWNGRADEABLE_START(*tmpbuf)) {
3555 *tmpbuf = (U8) title_ord;
3559 /* Otherwise it became more than one ASCII character (in
3560 * the case of LATIN_SMALL_LETTER_SHARP_S) or changed to
3561 * beyond Latin1, so the number of bytes changed, so can't
3562 * replace just the first character in place. */
3565 /* If the result won't fit in a byte, the entire result
3566 * will have to be in UTF-8. Assume worst case sizing in
3567 * conversion. (all latin1 characters occupy at most two
3569 if (title_ord > 255) {
3571 convert_source_to_utf8 = TRUE;
3572 need = slen * 2 + 1;
3574 /* The (converted) UTF-8 and UTF-EBCDIC lengths of all
3575 * (both) characters whose title case is above 255 is
3579 else { /* LATIN_SMALL_LETTER_SHARP_S expands by 1 byte */
3580 need = slen + 1 + 1;
3584 } /* End of use Unicode (Latin1) semantics */
3585 } /* End of changing the case of the first character */
3587 /* Here, have the first character's changed case stored in tmpbuf. Ready to
3588 * generate the result */
3591 /* We can convert in place. This means we change just the first
3592 * character without disturbing the rest; no need to grow */
3594 s = d = (U8*)SvPV_force_nomg(source, slen);
3600 /* Here, we can't convert in place; we earlier calculated how much
3601 * space we will need, so grow to accommodate that */
3602 SvUPGRADE(dest, SVt_PV);
3603 d = (U8*)SvGROW(dest, need);
3604 (void)SvPOK_only(dest);
3611 if (! convert_source_to_utf8) {
3613 /* Here both source and dest are in UTF-8, but have to create
3614 * the entire output. We initialize the result to be the
3615 * title/lower cased first character, and then append the rest
3617 sv_setpvn(dest, (char*)tmpbuf, tculen);
3619 sv_catpvn(dest, (char*)(s + ulen), slen - ulen);
3623 const U8 *const send = s + slen;
3625 /* Here the dest needs to be in UTF-8, but the source isn't,
3626 * except we earlier UTF-8'd the first character of the source
3627 * into tmpbuf. First put that into dest, and then append the
3628 * rest of the source, converting it to UTF-8 as we go. */
3630 /* Assert tculen is 2 here because the only two characters that
3631 * get to this part of the code have 2-byte UTF-8 equivalents */
3633 *d++ = *(tmpbuf + 1);
3634 s++; /* We have just processed the 1st char */
3636 for (; s < send; s++) {
3637 d = uvchr_to_utf8(d, *s);
3640 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3644 else { /* in-place UTF-8. Just overwrite the first character */
3645 Copy(tmpbuf, d, tculen, U8);
3646 SvCUR_set(dest, need - 1);
3654 else { /* Neither source nor dest are in or need to be UTF-8 */
3656 if (IN_LOCALE_RUNTIME) {
3660 if (inplace) { /* in-place, only need to change the 1st char */
3663 else { /* Not in-place */
3665 /* Copy the case-changed character(s) from tmpbuf */
3666 Copy(tmpbuf, d, tculen, U8);
3667 d += tculen - 1; /* Code below expects d to point to final
3668 * character stored */
3671 else { /* empty source */
3672 /* See bug #39028: Don't taint if empty */
3676 /* In a "use bytes" we don't treat the source as UTF-8, but, still want
3677 * the destination to retain that flag */
3678 if (SvUTF8(source) && ! IN_BYTES)
3681 if (!inplace) { /* Finish the rest of the string, unchanged */
3682 /* This will copy the trailing NUL */
3683 Copy(s + 1, d + 1, slen, U8);
3684 SvCUR_set(dest, need - 1);
3687 if (dest != source && SvTAINTED(source))
3693 /* There's so much setup/teardown code common between uc and lc, I wonder if
3694 it would be worth merging the two, and just having a switch outside each
3695 of the three tight loops. There is less and less commonality though */
3709 if (SvPADTMP(source) && !SvREADONLY(source) && !SvAMAGIC(source)
3710 && SvTEMP(source) && !DO_UTF8(source)
3711 && (IN_LOCALE_RUNTIME || ! IN_UNI_8_BIT)) {
3713 /* We can convert in place. The reason we can't if in UNI_8_BIT is to
3714 * make the loop tight, so we overwrite the source with the dest before
3715 * looking at it, and we need to look at the original source
3716 * afterwards. There would also need to be code added to handle
3717 * switching to not in-place in midstream if we run into characters
3718 * that change the length.
3721 s = d = (U8*)SvPV_force_nomg(source, len);
3728 /* The old implementation would copy source into TARG at this point.
3729 This had the side effect that if source was undef, TARG was now
3730 an undefined SV with PADTMP set, and they don't warn inside
3731 sv_2pv_flags(). However, we're now getting the PV direct from
3732 source, which doesn't have PADTMP set, so it would warn. Hence the
3736 s = (const U8*)SvPV_nomg_const(source, len);
3738 if (ckWARN(WARN_UNINITIALIZED))
3739 report_uninit(source);
3745 SvUPGRADE(dest, SVt_PV);
3746 d = (U8*)SvGROW(dest, min);
3747 (void)SvPOK_only(dest);
3752 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
3753 to check DO_UTF8 again here. */
3755 if (DO_UTF8(source)) {
3756 const U8 *const send = s + len;
3757 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
3758 bool tainted = FALSE;
3760 /* All occurrences of these are to be moved to follow any other marks.
3761 * This is context-dependent. We may not be passed enough context to
3762 * move the iota subscript beyond all of them, but we do the best we can
3763 * with what we're given. The result is always better than if we
3764 * hadn't done this. And, the problem would only arise if we are
3765 * passed a character without all its combining marks, which would be
3766 * the caller's mistake. The information this is based on comes from a
3767 * comment in Unicode SpecialCasing.txt, (and the Standard's text
3768 * itself) and so can't be checked properly to see if it ever gets
3769 * revised. But the likelihood of it changing is remote */
3770 bool in_iota_subscript = FALSE;
3776 if (in_iota_subscript && ! _is_utf8_mark(s)) {
3778 /* A non-mark. Time to output the iota subscript */
3779 Copy(GREEK_CAPITAL_LETTER_IOTA_UTF8, d, capital_iota_len, U8);
3780 d += capital_iota_len;
3781 in_iota_subscript = FALSE;
3784 /* Then handle the current character. Get the changed case value
3785 * and copy it to the output buffer */
3788 uv = _to_utf8_upper_flags(s, tmpbuf, &ulen,
3789 cBOOL(IN_LOCALE_RUNTIME), &tainted);
3790 #define GREEK_CAPITAL_LETTER_IOTA 0x0399
3791 #define COMBINING_GREEK_YPOGEGRAMMENI 0x0345
3792 if (uv == GREEK_CAPITAL_LETTER_IOTA
3793 && utf8_to_uvchr_buf(s, send, 0) == COMBINING_GREEK_YPOGEGRAMMENI)
3795 in_iota_subscript = TRUE;
3798 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
3799 /* If the eventually required minimum size outgrows the
3800 * available space, we need to grow. */
3801 const UV o = d - (U8*)SvPVX_const(dest);
3803 /* If someone uppercases one million U+03B0s we SvGROW()
3804 * one million times. Or we could try guessing how much to
3805 * allocate without allocating too much. Such is life.
3806 * See corresponding comment in lc code for another option
3809 d = (U8*)SvPVX(dest) + o;
3811 Copy(tmpbuf, d, ulen, U8);
3816 if (in_iota_subscript) {
3817 Copy(GREEK_CAPITAL_LETTER_IOTA_UTF8, d, capital_iota_len, U8);
3818 d += capital_iota_len;
3823 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3829 else { /* Not UTF-8 */
3831 const U8 *const send = s + len;
3833 /* Use locale casing if in locale; regular style if not treating
3834 * latin1 as having case; otherwise the latin1 casing. Do the
3835 * whole thing in a tight loop, for speed, */
3836 if (IN_LOCALE_RUNTIME) {
3839 for (; s < send; d++, s++)
3840 *d = toUPPER_LC(*s);
3842 else if (! IN_UNI_8_BIT) {
3843 for (; s < send; d++, s++) {
3848 for (; s < send; d++, s++) {
3849 *d = toUPPER_LATIN1_MOD(*s);
3850 if (LIKELY(*d != LATIN_SMALL_LETTER_Y_WITH_DIAERESIS)) {
3854 /* The mainstream case is the tight loop above. To avoid
3855 * extra tests in that, all three characters that require
3856 * special handling are mapped by the MOD to the one tested
3858 * Use the source to distinguish between the three cases */
3860 if (*s == LATIN_SMALL_LETTER_SHARP_S) {
3862 /* uc() of this requires 2 characters, but they are
3863 * ASCII. If not enough room, grow the string */
3864 if (SvLEN(dest) < ++min) {
3865 const UV o = d - (U8*)SvPVX_const(dest);
3867 d = (U8*)SvPVX(dest) + o;
3869 *d++ = 'S'; *d = 'S'; /* upper case is 'SS' */
3870 continue; /* Back to the tight loop; still in ASCII */
3873 /* The other two special handling characters have their
3874 * upper cases outside the latin1 range, hence need to be
3875 * in UTF-8, so the whole result needs to be in UTF-8. So,
3876 * here we are somewhere in the middle of processing a
3877 * non-UTF-8 string, and realize that we will have to convert
3878 * the whole thing to UTF-8. What to do? There are
3879 * several possibilities. The simplest to code is to
3880 * convert what we have so far, set a flag, and continue on
3881 * in the loop. The flag would be tested each time through
3882 * the loop, and if set, the next character would be
3883 * converted to UTF-8 and stored. But, I (khw) didn't want
3884 * to slow down the mainstream case at all for this fairly
3885 * rare case, so I didn't want to add a test that didn't
3886 * absolutely have to be there in the loop, besides the
3887 * possibility that it would get too complicated for
3888 * optimizers to deal with. Another possibility is to just
3889 * give up, convert the source to UTF-8, and restart the
3890 * function that way. Another possibility is to convert
3891 * both what has already been processed and what is yet to
3892 * come separately to UTF-8, then jump into the loop that
3893 * handles UTF-8. But the most efficient time-wise of the
3894 * ones I could think of is what follows, and turned out to
3895 * not require much extra code. */
3897 /* Convert what we have so far into UTF-8, telling the
3898 * function that we know it should be converted, and to
3899 * allow extra space for what we haven't processed yet.
3900 * Assume the worst case space requirements for converting
3901 * what we haven't processed so far: that it will require
3902 * two bytes for each remaining source character, plus the
3903 * NUL at the end. This may cause the string pointer to
3904 * move, so re-find it. */
3906 len = d - (U8*)SvPVX_const(dest);
3907 SvCUR_set(dest, len);
3908 len = sv_utf8_upgrade_flags_grow(dest,
3909 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
3911 d = (U8*)SvPVX(dest) + len;
3913 /* Now process the remainder of the source, converting to
3914 * upper and UTF-8. If a resulting byte is invariant in
3915 * UTF-8, output it as-is, otherwise convert to UTF-8 and
3916 * append it to the output. */
3917 for (; s < send; s++) {
3918 (void) _to_upper_title_latin1(*s, d, &len, 'S');
3922 /* Here have processed the whole source; no need to continue
3923 * with the outer loop. Each character has been converted
3924 * to upper case and converted to UTF-8 */
3927 } /* End of processing all latin1-style chars */
3928 } /* End of processing all chars */
3929 } /* End of source is not empty */
3931 if (source != dest) {
3932 *d = '\0'; /* Here d points to 1 after last char, add NUL */
3933 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3935 } /* End of isn't utf8 */
3936 if (dest != source && SvTAINTED(source))
3955 if (SvPADTMP(source) && !SvREADONLY(source) && !SvAMAGIC(source)
3956 && SvTEMP(source) && !DO_UTF8(source)) {
3958 /* We can convert in place, as lowercasing anything in the latin1 range
3959 * (or else DO_UTF8 would have been on) doesn't lengthen it */
3961 s = d = (U8*)SvPV_force_nomg(source, len);
3968 /* The old implementation would copy source into TARG at this point.
3969 This had the side effect that if source was undef, TARG was now
3970 an undefined SV with PADTMP set, and they don't warn inside
3971 sv_2pv_flags(). However, we're now getting the PV direct from
3972 source, which doesn't have PADTMP set, so it would warn. Hence the
3976 s = (const U8*)SvPV_nomg_const(source, len);
3978 if (ckWARN(WARN_UNINITIALIZED))
3979 report_uninit(source);
3985 SvUPGRADE(dest, SVt_PV);
3986 d = (U8*)SvGROW(dest, min);
3987 (void)SvPOK_only(dest);
3992 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
3993 to check DO_UTF8 again here. */
3995 if (DO_UTF8(source)) {
3996 const U8 *const send = s + len;
3997 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
3998 bool tainted = FALSE;
4001 const STRLEN u = UTF8SKIP(s);
4004 _to_utf8_lower_flags(s, tmpbuf, &ulen,
4005 cBOOL(IN_LOCALE_RUNTIME), &tainted);
4007 /* Here is where we would do context-sensitive actions. See the
4008 * commit message for this comment for why there isn't any */
4010 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
4012 /* If the eventually required minimum size outgrows the
4013 * available space, we need to grow. */
4014 const UV o = d - (U8*)SvPVX_const(dest);
4016 /* If someone lowercases one million U+0130s we SvGROW() one
4017 * million times. Or we could try guessing how much to
4018 * allocate without allocating too much. Such is life.
4019 * Another option would be to grow an extra byte or two more
4020 * each time we need to grow, which would cut down the million
4021 * to 500K, with little waste */
4023 d = (U8*)SvPVX(dest) + o;
4026 /* Copy the newly lowercased letter to the output buffer we're
4028 Copy(tmpbuf, d, ulen, U8);
4031 } /* End of looping through the source string */
4034 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4039 } else { /* Not utf8 */
4041 const U8 *const send = s + len;
4043 /* Use locale casing if in locale; regular style if not treating
4044 * latin1 as having case; otherwise the latin1 casing. Do the
4045 * whole thing in a tight loop, for speed, */
4046 if (IN_LOCALE_RUNTIME) {
4049 for (; s < send; d++, s++)
4050 *d = toLOWER_LC(*s);
4052 else if (! IN_UNI_8_BIT) {
4053 for (; s < send; d++, s++) {
4058 for (; s < send; d++, s++) {
4059 *d = toLOWER_LATIN1(*s);
4063 if (source != dest) {
4065 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4068 if (dest != source && SvTAINTED(source))
4077 SV * const sv = TOPs;
4079 const char *s = SvPV_const(sv,len);
4081 SvUTF8_off(TARG); /* decontaminate */
4084 SvUPGRADE(TARG, SVt_PV);
4085 SvGROW(TARG, (len * 2) + 1);
4089 STRLEN ulen = UTF8SKIP(s);
4090 bool to_quote = FALSE;
4092 if (UTF8_IS_INVARIANT(*s)) {
4093 if (_isQUOTEMETA(*s)) {
4097 else if (UTF8_IS_DOWNGRADEABLE_START(*s)) {
4099 /* In locale, we quote all non-ASCII Latin1 chars.
4100 * Otherwise use the quoting rules */
4101 if (IN_LOCALE_RUNTIME
4102 || _isQUOTEMETA(TWO_BYTE_UTF8_TO_NATIVE(*s, *(s + 1))))
4107 else if (is_QUOTEMETA_high(s)) {
4122 else if (IN_UNI_8_BIT) {
4124 if (_isQUOTEMETA(*s))
4130 /* For non UNI_8_BIT (and hence in locale) just quote all \W
4131 * including everything above ASCII */
4133 if (!isWORDCHAR_A(*s))
4139 SvCUR_set(TARG, d - SvPVX_const(TARG));
4140 (void)SvPOK_only_UTF8(TARG);
4143 sv_setpvn(TARG, s, len);
4160 U8 tmpbuf[UTF8_MAXBYTES_CASE + 1];
4161 const bool full_folding = TRUE;
4162 const U8 flags = ( full_folding ? FOLD_FLAGS_FULL : 0 )
4163 | ( IN_LOCALE_RUNTIME ? FOLD_FLAGS_LOCALE : 0 );
4165 /* This is a facsimile of pp_lc, but with a thousand bugs thanks to me.
4166 * You are welcome(?) -Hugmeir
4174 s = (const U8*)SvPV_nomg_const(source, len);
4176 if (ckWARN(WARN_UNINITIALIZED))
4177 report_uninit(source);
4184 SvUPGRADE(dest, SVt_PV);
4185 d = (U8*)SvGROW(dest, min);
4186 (void)SvPOK_only(dest);
4191 if (DO_UTF8(source)) { /* UTF-8 flagged string. */
4192 bool tainted = FALSE;
4194 const STRLEN u = UTF8SKIP(s);
4197 _to_utf8_fold_flags(s, tmpbuf, &ulen, flags, &tainted);
4199 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
4200 const UV o = d - (U8*)SvPVX_const(dest);
4202 d = (U8*)SvPVX(dest) + o;
4205 Copy(tmpbuf, d, ulen, U8);
4214 } /* Unflagged string */
4216 if ( IN_LOCALE_RUNTIME ) { /* Under locale */
4219 for (; s < send; d++, s++)
4222 else if ( !IN_UNI_8_BIT ) { /* Under nothing, or bytes */
4223 for (; s < send; d++, s++)
4227 /* For ASCII and the Latin-1 range, there's only two troublesome
4228 * folds, \x{DF} (\N{LATIN SMALL LETTER SHARP S}), which under full
4229 * casefolding becomes 'ss'; and \x{B5} (\N{MICRO SIGN}), which
4230 * under any fold becomes \x{3BC} (\N{GREEK SMALL LETTER MU}) --
4231 * For the rest, the casefold is their lowercase. */
4232 for (; s < send; d++, s++) {
4233 if (*s == MICRO_SIGN) {
4234 /* \N{MICRO SIGN}'s casefold is \N{GREEK SMALL LETTER MU},
4235 * which is outside of the latin-1 range. There's a couple
4236 * of ways to deal with this -- khw discusses them in
4237 * pp_lc/uc, so go there :) What we do here is upgrade what
4238 * we had already casefolded, then enter an inner loop that
4239 * appends the rest of the characters as UTF-8. */
4240 len = d - (U8*)SvPVX_const(dest);
4241 SvCUR_set(dest, len);
4242 len = sv_utf8_upgrade_flags_grow(dest,
4243 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
4244 /* The max expansion for latin1
4245 * chars is 1 byte becomes 2 */
4247 d = (U8*)SvPVX(dest) + len;
4249 Copy(GREEK_SMALL_LETTER_MU_UTF8, d, small_mu_len, U8);
4252 for (; s < send; s++) {
4254 UV fc = _to_uni_fold_flags(*s, tmpbuf, &ulen, flags);
4255 if UVCHR_IS_INVARIANT(fc) {
4257 && *s == LATIN_SMALL_LETTER_SHARP_S)
4266 Copy(tmpbuf, d, ulen, U8);
4272 else if (full_folding && *s == LATIN_SMALL_LETTER_SHARP_S) {
4273 /* Under full casefolding, LATIN SMALL LETTER SHARP S
4274 * becomes "ss", which may require growing the SV. */
4275 if (SvLEN(dest) < ++min) {
4276 const UV o = d - (U8*)SvPVX_const(dest);
4278 d = (U8*)SvPVX(dest) + o;
4283 else { /* If it's not one of those two, the fold is their lower
4285 *d = toLOWER_LATIN1(*s);
4291 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4293 if (SvTAINTED(source))
4303 dVAR; dSP; dMARK; dORIGMARK;
4304 AV *const av = MUTABLE_AV(POPs);
4305 const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
4307 if (SvTYPE(av) == SVt_PVAV) {
4308 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
4309 bool can_preserve = FALSE;
4315 can_preserve = SvCANEXISTDELETE(av);
4318 if (lval && localizing) {
4321 for (svp = MARK + 1; svp <= SP; svp++) {
4322 const SSize_t elem = SvIV(*svp);
4326 if (max > AvMAX(av))
4330 while (++MARK <= SP) {
4332 SSize_t elem = SvIV(*MARK);
4333 bool preeminent = TRUE;
4335 if (localizing && can_preserve) {
4336 /* If we can determine whether the element exist,
4337 * Try to preserve the existenceness of a tied array
4338 * element by using EXISTS and DELETE if possible.
4339 * Fallback to FETCH and STORE otherwise. */
4340 preeminent = av_exists(av, elem);
4343 svp = av_fetch(av, elem, lval);
4346 DIE(aTHX_ PL_no_aelem, elem);
4349 save_aelem(av, elem, svp);
4351 SAVEADELETE(av, elem);
4354 *MARK = svp ? *svp : &PL_sv_undef;
4357 if (GIMME != G_ARRAY) {
4359 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
4368 AV *const av = MUTABLE_AV(POPs);
4369 I32 lval = (PL_op->op_flags & OPf_MOD);
4370 SSize_t items = SP - MARK;
4372 if (PL_op->op_private & OPpMAYBE_LVSUB) {
4373 const I32 flags = is_lvalue_sub();
4375 if (!(flags & OPpENTERSUB_INARGS))
4376 /* diag_listed_as: Can't modify %s in %s */
4377 Perl_croak(aTHX_ "Can't modify index/value array slice in list assignment");
4384 *(MARK+items*2-1) = *(MARK+items);
4390 while (++MARK <= SP) {
4393 svp = av_fetch(av, SvIV(*MARK), lval);
4395 if (!svp || !*svp || *svp == &PL_sv_undef) {
4396 DIE(aTHX_ PL_no_aelem, SvIV(*MARK));
4398 *MARK = sv_mortalcopy(*MARK);
4400 *++MARK = svp ? *svp : &PL_sv_undef;
4402 if (GIMME != G_ARRAY) {
4403 MARK = SP - items*2;
4404 *++MARK = items > 0 ? *SP : &PL_sv_undef;
4410 /* Smart dereferencing for keys, values and each */
4422 (SvTYPE(sv) != SVt_PVHV && SvTYPE(sv) != SVt_PVAV)
4427 "Type of argument to %s must be unblessed hashref or arrayref",
4428 PL_op_desc[PL_op->op_type] );
4431 if (PL_op->op_flags & OPf_SPECIAL && SvTYPE(sv) == SVt_PVAV)
4433 "Can't modify %s in %s",
4434 PL_op_desc[PL_op->op_type], PL_op_desc[PL_op->op_next->op_type]
4437 /* Delegate to correct function for op type */
4439 if (PL_op->op_type == OP_RKEYS || PL_op->op_type == OP_RVALUES) {
4440 return (SvTYPE(sv) == SVt_PVHV) ? Perl_do_kv(aTHX) : Perl_pp_akeys(aTHX);
4443 return (SvTYPE(sv) == SVt_PVHV)
4444 ? Perl_pp_each(aTHX)
4445 : Perl_pp_aeach(aTHX);
4453 AV *array = MUTABLE_AV(POPs);
4454 const I32 gimme = GIMME_V;
4455 IV *iterp = Perl_av_iter_p(aTHX_ array);
4456 const IV current = (*iterp)++;
4458 if (current > av_len(array)) {
4460 if (gimme == G_SCALAR)
4468 if (gimme == G_ARRAY) {
4469 SV **const element = av_fetch(array, current, 0);
4470 PUSHs(element ? *element : &PL_sv_undef);
4479 AV *array = MUTABLE_AV(POPs);
4480 const I32 gimme = GIMME_V;
4482 *Perl_av_iter_p(aTHX_ array) = 0;
4484 if (gimme == G_SCALAR) {
4486 PUSHi(av_len(array) + 1);
4488 else if (gimme == G_ARRAY) {
4489 IV n = Perl_av_len(aTHX_ array);
4494 if (PL_op->op_type == OP_AKEYS || PL_op->op_type == OP_RKEYS) {
4495 for (i = 0; i <= n; i++) {
4500 for (i = 0; i <= n; i++) {
4501 SV *const *const elem = Perl_av_fetch(aTHX_ array, i, 0);
4502 PUSHs(elem ? *elem : &PL_sv_undef);
4509 /* Associative arrays. */
4515 HV * hash = MUTABLE_HV(POPs);
4517 const I32 gimme = GIMME_V;
4520 /* might clobber stack_sp */
4521 entry = hv_iternext(hash);
4526 SV* const sv = hv_iterkeysv(entry);
4527 PUSHs(sv); /* won't clobber stack_sp */
4528 if (gimme == G_ARRAY) {
4531 /* might clobber stack_sp */
4532 val = hv_iterval(hash, entry);
4537 else if (gimme == G_SCALAR)
4544 S_do_delete_local(pTHX)
4548 const I32 gimme = GIMME_V;
4551 const bool sliced = !!(PL_op->op_private & OPpSLICE);
4552 SV *unsliced_keysv = sliced ? NULL : POPs;
4553 SV * const osv = POPs;
4554 SV **mark = sliced ? PL_stack_base + POPMARK : &unsliced_keysv-1;
4556 const bool tied = SvRMAGICAL(osv)
4557 && mg_find((const SV *)osv, PERL_MAGIC_tied);
4558 const bool can_preserve = SvCANEXISTDELETE(osv);
4559 const U32 type = SvTYPE(osv);
4560 SV ** const end = sliced ? SP : &unsliced_keysv;
4562 if (type == SVt_PVHV) { /* hash element */
4563 HV * const hv = MUTABLE_HV(osv);
4564 while (++MARK <= end) {
4565 SV * const keysv = *MARK;
4567 bool preeminent = TRUE;
4569 preeminent = hv_exists_ent(hv, keysv, 0);
4571 HE *he = hv_fetch_ent(hv, keysv, 1, 0);
4578 sv = hv_delete_ent(hv, keysv, 0, 0);
4580 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4583 if (!sv) DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
4584 save_helem_flags(hv, keysv, &sv, SAVEf_KEEPOLDELEM);
4586 *MARK = sv_mortalcopy(sv);
4592 SAVEHDELETE(hv, keysv);
4593 *MARK = &PL_sv_undef;
4597 else if (type == SVt_PVAV) { /* array element */
4598 if (PL_op->op_flags & OPf_SPECIAL) {
4599 AV * const av = MUTABLE_AV(osv);
4600 while (++MARK <= end) {
4601 SSize_t idx = SvIV(*MARK);
4603 bool preeminent = TRUE;
4605 preeminent = av_exists(av, idx);
4607 SV **svp = av_fetch(av, idx, 1);
4614 sv = av_delete(av, idx, 0);
4616 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4619 save_aelem_flags(av, idx, &sv, SAVEf_KEEPOLDELEM);
4621 *MARK = sv_mortalcopy(sv);
4627 SAVEADELETE(av, idx);
4628 *MARK = &PL_sv_undef;