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)) {
575 assert(!IS_PADGV(sv));
580 SvREFCNT_inc_void_NN(sv);
583 sv_upgrade(rv, SVt_IV);
592 SV * const sv = POPs;
598 (void)sv_ref(TARG,SvRV(sv),TRUE);
611 stash = CopSTASH(PL_curcop);
612 if (SvTYPE(stash) != SVt_PVHV)
613 Perl_croak(aTHX_ "Attempt to bless into a freed package");
616 SV * const ssv = POPs;
620 if (!ssv) goto curstash;
623 if (!SvAMAGIC(ssv)) {
625 Perl_croak(aTHX_ "Attempt to bless into a reference");
627 /* SvAMAGIC is on here, but it only means potentially overloaded,
628 so after stringification: */
629 ptr = SvPV_nomg_const(ssv,len);
630 /* We need to check the flag again: */
631 if (!SvAMAGIC(ssv)) goto frog;
633 else ptr = SvPV_nomg_const(ssv,len);
635 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
636 "Explicit blessing to '' (assuming package main)");
637 stash = gv_stashpvn(ptr, len, GV_ADD|SvUTF8(ssv));
640 (void)sv_bless(TOPs, stash);
650 const char * const elem = SvPV_const(sv, len);
651 GV * const gv = MUTABLE_GV(POPs);
656 /* elem will always be NUL terminated. */
657 const char * const second_letter = elem + 1;
660 if (len == 5 && strEQ(second_letter, "RRAY"))
662 tmpRef = MUTABLE_SV(GvAV(gv));
663 if (tmpRef && !AvREAL((const AV *)tmpRef)
664 && AvREIFY((const AV *)tmpRef))
665 av_reify(MUTABLE_AV(tmpRef));
669 if (len == 4 && strEQ(second_letter, "ODE"))
670 tmpRef = MUTABLE_SV(GvCVu(gv));
673 if (len == 10 && strEQ(second_letter, "ILEHANDLE")) {
674 /* finally deprecated in 5.8.0 */
675 deprecate("*glob{FILEHANDLE}");
676 tmpRef = MUTABLE_SV(GvIOp(gv));
679 if (len == 6 && strEQ(second_letter, "ORMAT"))
680 tmpRef = MUTABLE_SV(GvFORM(gv));
683 if (len == 4 && strEQ(second_letter, "LOB"))
684 tmpRef = MUTABLE_SV(gv);
687 if (len == 4 && strEQ(second_letter, "ASH"))
688 tmpRef = MUTABLE_SV(GvHV(gv));
691 if (*second_letter == 'O' && !elem[2] && len == 2)
692 tmpRef = MUTABLE_SV(GvIOp(gv));
695 if (len == 4 && strEQ(second_letter, "AME"))
696 sv = newSVhek(GvNAME_HEK(gv));
699 if (len == 7 && strEQ(second_letter, "ACKAGE")) {
700 const HV * const stash = GvSTASH(gv);
701 const HEK * const hek = stash ? HvNAME_HEK(stash) : NULL;
702 sv = hek ? newSVhek(hek) : newSVpvs("__ANON__");
706 if (len == 6 && strEQ(second_letter, "CALAR"))
721 /* Pattern matching */
729 if (len == 0 || len > I32_MAX || !SvPOK(sv) || SvUTF8(sv) || SvVALID(sv)) {
730 /* Historically, study was skipped in these cases. */
734 /* Make study a no-op. It's no longer useful and its existence
735 complicates matters elsewhere. */
744 if (PL_op->op_flags & OPf_STACKED)
746 else if (PL_op->op_private & OPpTARGET_MY)
752 if(PL_op->op_type == OP_TRANSR) {
754 const char * const pv = SvPV(sv,len);
755 SV * const newsv = newSVpvn_flags(pv, len, SVs_TEMP|SvUTF8(sv));
760 TARG = sv_newmortal();
766 /* Lvalue operators. */
769 S_do_chomp(pTHX_ SV *retval, SV *sv, bool chomping)
775 PERL_ARGS_ASSERT_DO_CHOMP;
777 if (chomping && (RsSNARF(PL_rs) || RsRECORD(PL_rs)))
779 if (SvTYPE(sv) == SVt_PVAV) {
781 AV *const av = MUTABLE_AV(sv);
782 const I32 max = AvFILL(av);
784 for (i = 0; i <= max; i++) {
785 sv = MUTABLE_SV(av_fetch(av, i, FALSE));
786 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
787 do_chomp(retval, sv, chomping);
791 else if (SvTYPE(sv) == SVt_PVHV) {
792 HV* const hv = MUTABLE_HV(sv);
794 (void)hv_iterinit(hv);
795 while ((entry = hv_iternext(hv)))
796 do_chomp(retval, hv_iterval(hv,entry), chomping);
799 else if (SvREADONLY(sv)) {
800 Perl_croak_no_modify();
802 else if (SvIsCOW(sv)) {
803 sv_force_normal_flags(sv, 0);
808 /* XXX, here sv is utf8-ized as a side-effect!
809 If encoding.pm is used properly, almost string-generating
810 operations, including literal strings, chr(), input data, etc.
811 should have been utf8-ized already, right?
813 sv_recode_to_utf8(sv, PL_encoding);
819 char *temp_buffer = NULL;
828 while (len && s[-1] == '\n') {
835 STRLEN rslen, rs_charlen;
836 const char *rsptr = SvPV_const(PL_rs, rslen);
838 rs_charlen = SvUTF8(PL_rs)
842 if (SvUTF8(PL_rs) != SvUTF8(sv)) {
843 /* Assumption is that rs is shorter than the scalar. */
845 /* RS is utf8, scalar is 8 bit. */
847 temp_buffer = (char*)bytes_from_utf8((U8*)rsptr,
850 /* Cannot downgrade, therefore cannot possibly match
852 assert (temp_buffer == rsptr);
858 else if (PL_encoding) {
859 /* RS is 8 bit, encoding.pm is used.
860 * Do not recode PL_rs as a side-effect. */
861 svrecode = newSVpvn(rsptr, rslen);
862 sv_recode_to_utf8(svrecode, PL_encoding);
863 rsptr = SvPV_const(svrecode, rslen);
864 rs_charlen = sv_len_utf8(svrecode);
867 /* RS is 8 bit, scalar is utf8. */
868 temp_buffer = (char*)bytes_to_utf8((U8*)rsptr, &rslen);
882 if (memNE(s, rsptr, rslen))
884 SvIVX(retval) += rs_charlen;
887 s = SvPV_force_nomg_nolen(sv);
895 SvREFCNT_dec(svrecode);
897 Safefree(temp_buffer);
899 if (len && !SvPOK(sv))
900 s = SvPV_force_nomg(sv, len);
903 char * const send = s + len;
904 char * const start = s;
906 while (s > start && UTF8_IS_CONTINUATION(*s))
908 if (is_utf8_string((U8*)s, send - s)) {
909 sv_setpvn(retval, s, send - s);
911 SvCUR_set(sv, s - start);
917 sv_setpvs(retval, "");
921 sv_setpvn(retval, s, 1);
928 sv_setpvs(retval, "");
936 const bool chomping = PL_op->op_type == OP_SCHOMP;
940 do_chomp(TARG, TOPs, chomping);
947 dVAR; dSP; dMARK; dTARGET; dORIGMARK;
948 const bool chomping = PL_op->op_type == OP_CHOMP;
953 do_chomp(TARG, *++MARK, chomping);
964 if (!PL_op->op_private) {
973 SV_CHECK_THINKFIRST_COW_DROP(sv);
975 switch (SvTYPE(sv)) {
979 av_undef(MUTABLE_AV(sv));
982 hv_undef(MUTABLE_HV(sv));
985 if (cv_const_sv((const CV *)sv))
986 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
987 "Constant subroutine %"SVf" undefined",
988 SVfARG(CvANON((const CV *)sv)
989 ? newSVpvs_flags("(anonymous)", SVs_TEMP)
990 : sv_2mortal(newSVhek(
992 ? CvNAME_HEK((CV *)sv)
993 : GvENAME_HEK(CvGV((const CV *)sv))
999 /* let user-undef'd sub keep its identity */
1000 GV* const gv = CvGV((const CV *)sv);
1001 HEK * const hek = CvNAME_HEK((CV *)sv);
1002 if (hek) share_hek_hek(hek);
1003 cv_undef(MUTABLE_CV(sv));
1004 if (gv) CvGV_set(MUTABLE_CV(sv), gv);
1006 SvANY((CV *)sv)->xcv_gv_u.xcv_hek = hek;
1012 assert(isGV_with_GP(sv));
1013 assert(!SvFAKE(sv));
1018 /* undef *Pkg::meth_name ... */
1020 = GvCVu((const GV *)sv) && (stash = GvSTASH((const GV *)sv))
1021 && HvENAME_get(stash);
1023 if((stash = GvHV((const GV *)sv))) {
1024 if(HvENAME_get(stash))
1025 SvREFCNT_inc_simple_void_NN(sv_2mortal((SV *)stash));
1029 SvREFCNT_inc_simple_void_NN(sv_2mortal(sv));
1030 gp_free(MUTABLE_GV(sv));
1032 GvGP_set(sv, gp_ref(gp));
1033 #ifndef PERL_DONT_CREATE_GVSV
1034 GvSV(sv) = newSV(0);
1036 GvLINE(sv) = CopLINE(PL_curcop);
1037 GvEGV(sv) = MUTABLE_GV(sv);
1041 mro_package_moved(NULL, stash, (const GV *)sv, 0);
1043 /* undef *Foo::ISA */
1044 if( strEQ(GvNAME((const GV *)sv), "ISA")
1045 && (stash = GvSTASH((const GV *)sv))
1046 && (method_changed || HvENAME(stash)) )
1047 mro_isa_changed_in(stash);
1048 else if(method_changed)
1049 mro_method_changed_in(
1050 GvSTASH((const GV *)sv)
1056 if (SvTYPE(sv) >= SVt_PV && SvPVX_const(sv) && SvLEN(sv)) {
1072 PL_op->op_type == OP_POSTINC || PL_op->op_type == OP_I_POSTINC;
1073 if (SvTYPE(TOPs) >= SVt_PVAV || (isGV_with_GP(TOPs) && !SvFAKE(TOPs)))
1074 Perl_croak_no_modify();
1076 TARG = sv_newmortal();
1077 sv_setsv(TARG, TOPs);
1078 if (!SvREADONLY(TOPs) && !SvGMAGICAL(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
1079 && SvIVX(TOPs) != (inc ? IV_MAX : IV_MIN))
1081 SvIV_set(TOPs, SvIVX(TOPs) + (inc ? 1 : -1));
1082 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
1086 else sv_dec_nomg(TOPs);
1088 /* special case for undef: see thread at 2003-03/msg00536.html in archive */
1089 if (inc && !SvOK(TARG))
1095 /* Ordinary operators. */
1099 dVAR; dSP; dATARGET; SV *svl, *svr;
1100 #ifdef PERL_PRESERVE_IVUV
1103 tryAMAGICbin_MG(pow_amg, AMGf_assign|AMGf_numeric);
1106 #ifdef PERL_PRESERVE_IVUV
1107 /* For integer to integer power, we do the calculation by hand wherever
1108 we're sure it is safe; otherwise we call pow() and try to convert to
1109 integer afterwards. */
1110 if (SvIV_please_nomg(svr) && SvIV_please_nomg(svl)) {
1118 const IV iv = SvIVX(svr);
1122 goto float_it; /* Can't do negative powers this way. */
1126 baseuok = SvUOK(svl);
1128 baseuv = SvUVX(svl);
1130 const IV iv = SvIVX(svl);
1133 baseuok = TRUE; /* effectively it's a UV now */
1135 baseuv = -iv; /* abs, baseuok == false records sign */
1138 /* now we have integer ** positive integer. */
1141 /* foo & (foo - 1) is zero only for a power of 2. */
1142 if (!(baseuv & (baseuv - 1))) {
1143 /* We are raising power-of-2 to a positive integer.
1144 The logic here will work for any base (even non-integer
1145 bases) but it can be less accurate than
1146 pow (base,power) or exp (power * log (base)) when the
1147 intermediate values start to spill out of the mantissa.
1148 With powers of 2 we know this can't happen.
1149 And powers of 2 are the favourite thing for perl
1150 programmers to notice ** not doing what they mean. */
1152 NV base = baseuok ? baseuv : -(NV)baseuv;
1157 while (power >>= 1) {
1165 SvIV_please_nomg(svr);
1168 unsigned int highbit = 8 * sizeof(UV);
1169 unsigned int diff = 8 * sizeof(UV);
1170 while (diff >>= 1) {
1172 if (baseuv >> highbit) {
1176 /* we now have baseuv < 2 ** highbit */
1177 if (power * highbit <= 8 * sizeof(UV)) {
1178 /* result will definitely fit in UV, so use UV math
1179 on same algorithm as above */
1182 const bool odd_power = cBOOL(power & 1);
1186 while (power >>= 1) {
1193 if (baseuok || !odd_power)
1194 /* answer is positive */
1196 else if (result <= (UV)IV_MAX)
1197 /* answer negative, fits in IV */
1198 SETi( -(IV)result );
1199 else if (result == (UV)IV_MIN)
1200 /* 2's complement assumption: special case IV_MIN */
1203 /* answer negative, doesn't fit */
1204 SETn( -(NV)result );
1212 NV right = SvNV_nomg(svr);
1213 NV left = SvNV_nomg(svl);
1216 #if defined(USE_LONG_DOUBLE) && defined(HAS_AIX_POWL_NEG_BASE_BUG)
1218 We are building perl with long double support and are on an AIX OS
1219 afflicted with a powl() function that wrongly returns NaNQ for any
1220 negative base. This was reported to IBM as PMR #23047-379 on
1221 03/06/2006. The problem exists in at least the following versions
1222 of AIX and the libm fileset, and no doubt others as well:
1224 AIX 4.3.3-ML10 bos.adt.libm 4.3.3.50
1225 AIX 5.1.0-ML04 bos.adt.libm 5.1.0.29
1226 AIX 5.2.0 bos.adt.libm 5.2.0.85
1228 So, until IBM fixes powl(), we provide the following workaround to
1229 handle the problem ourselves. Our logic is as follows: for
1230 negative bases (left), we use fmod(right, 2) to check if the
1231 exponent is an odd or even integer:
1233 - if odd, powl(left, right) == -powl(-left, right)
1234 - if even, powl(left, right) == powl(-left, right)
1236 If the exponent is not an integer, the result is rightly NaNQ, so
1237 we just return that (as NV_NAN).
1241 NV mod2 = Perl_fmod( right, 2.0 );
1242 if (mod2 == 1.0 || mod2 == -1.0) { /* odd integer */
1243 SETn( -Perl_pow( -left, right) );
1244 } else if (mod2 == 0.0) { /* even integer */
1245 SETn( Perl_pow( -left, right) );
1246 } else { /* fractional power */
1250 SETn( Perl_pow( left, right) );
1253 SETn( Perl_pow( left, right) );
1254 #endif /* HAS_AIX_POWL_NEG_BASE_BUG */
1256 #ifdef PERL_PRESERVE_IVUV
1258 SvIV_please_nomg(svr);
1266 dVAR; dSP; dATARGET; SV *svl, *svr;
1267 tryAMAGICbin_MG(mult_amg, AMGf_assign|AMGf_numeric);
1270 #ifdef PERL_PRESERVE_IVUV
1271 if (SvIV_please_nomg(svr)) {
1272 /* Unless the left argument is integer in range we are going to have to
1273 use NV maths. Hence only attempt to coerce the right argument if
1274 we know the left is integer. */
1275 /* Left operand is defined, so is it IV? */
1276 if (SvIV_please_nomg(svl)) {
1277 bool auvok = SvUOK(svl);
1278 bool buvok = SvUOK(svr);
1279 const UV topmask = (~ (UV)0) << (4 * sizeof (UV));
1280 const UV botmask = ~((~ (UV)0) << (4 * sizeof (UV)));
1289 const IV aiv = SvIVX(svl);
1292 auvok = TRUE; /* effectively it's a UV now */
1294 alow = -aiv; /* abs, auvok == false records sign */
1300 const IV biv = SvIVX(svr);
1303 buvok = TRUE; /* effectively it's a UV now */
1305 blow = -biv; /* abs, buvok == false records sign */
1309 /* If this does sign extension on unsigned it's time for plan B */
1310 ahigh = alow >> (4 * sizeof (UV));
1312 bhigh = blow >> (4 * sizeof (UV));
1314 if (ahigh && bhigh) {
1316 /* eg 32 bit is at least 0x10000 * 0x10000 == 0x100000000
1317 which is overflow. Drop to NVs below. */
1318 } else if (!ahigh && !bhigh) {
1319 /* eg 32 bit is at most 0xFFFF * 0xFFFF == 0xFFFE0001
1320 so the unsigned multiply cannot overflow. */
1321 const UV product = alow * blow;
1322 if (auvok == buvok) {
1323 /* -ve * -ve or +ve * +ve gives a +ve result. */
1327 } else if (product <= (UV)IV_MIN) {
1328 /* 2s complement assumption that (UV)-IV_MIN is correct. */
1329 /* -ve result, which could overflow an IV */
1331 SETi( -(IV)product );
1333 } /* else drop to NVs below. */
1335 /* One operand is large, 1 small */
1338 /* swap the operands */
1340 bhigh = blow; /* bhigh now the temp var for the swap */
1344 /* now, ((ahigh * blow) << half_UV_len) + (alow * blow)
1345 multiplies can't overflow. shift can, add can, -ve can. */
1346 product_middle = ahigh * blow;
1347 if (!(product_middle & topmask)) {
1348 /* OK, (ahigh * blow) won't lose bits when we shift it. */
1350 product_middle <<= (4 * sizeof (UV));
1351 product_low = alow * blow;
1353 /* as for pp_add, UV + something mustn't get smaller.
1354 IIRC ANSI mandates this wrapping *behaviour* for
1355 unsigned whatever the actual representation*/
1356 product_low += product_middle;
1357 if (product_low >= product_middle) {
1358 /* didn't overflow */
1359 if (auvok == buvok) {
1360 /* -ve * -ve or +ve * +ve gives a +ve result. */
1362 SETu( product_low );
1364 } else if (product_low <= (UV)IV_MIN) {
1365 /* 2s complement assumption again */
1366 /* -ve result, which could overflow an IV */
1368 SETi( -(IV)product_low );
1370 } /* else drop to NVs below. */
1372 } /* product_middle too large */
1373 } /* ahigh && bhigh */
1378 NV right = SvNV_nomg(svr);
1379 NV left = SvNV_nomg(svl);
1381 SETn( left * right );
1388 dVAR; dSP; dATARGET; SV *svl, *svr;
1389 tryAMAGICbin_MG(div_amg, AMGf_assign|AMGf_numeric);
1392 /* Only try to do UV divide first
1393 if ((SLOPPYDIVIDE is true) or
1394 (PERL_PRESERVE_IVUV is true and one or both SV is a UV too large
1396 The assumption is that it is better to use floating point divide
1397 whenever possible, only doing integer divide first if we can't be sure.
1398 If NV_PRESERVES_UV is true then we know at compile time that no UV
1399 can be too large to preserve, so don't need to compile the code to
1400 test the size of UVs. */
1403 # define PERL_TRY_UV_DIVIDE
1404 /* ensure that 20./5. == 4. */
1406 # ifdef PERL_PRESERVE_IVUV
1407 # ifndef NV_PRESERVES_UV
1408 # define PERL_TRY_UV_DIVIDE
1413 #ifdef PERL_TRY_UV_DIVIDE
1414 if (SvIV_please_nomg(svr) && SvIV_please_nomg(svl)) {
1415 bool left_non_neg = SvUOK(svl);
1416 bool right_non_neg = SvUOK(svr);
1420 if (right_non_neg) {
1424 const IV biv = SvIVX(svr);
1427 right_non_neg = TRUE; /* effectively it's a UV now */
1433 /* historically undef()/0 gives a "Use of uninitialized value"
1434 warning before dieing, hence this test goes here.
1435 If it were immediately before the second SvIV_please, then
1436 DIE() would be invoked before left was even inspected, so
1437 no inspection would give no warning. */
1439 DIE(aTHX_ "Illegal division by zero");
1445 const IV aiv = SvIVX(svl);
1448 left_non_neg = TRUE; /* effectively it's a UV now */
1457 /* For sloppy divide we always attempt integer division. */
1459 /* Otherwise we only attempt it if either or both operands
1460 would not be preserved by an NV. If both fit in NVs
1461 we fall through to the NV divide code below. However,
1462 as left >= right to ensure integer result here, we know that
1463 we can skip the test on the right operand - right big
1464 enough not to be preserved can't get here unless left is
1467 && (left > ((UV)1 << NV_PRESERVES_UV_BITS))
1470 /* Integer division can't overflow, but it can be imprecise. */
1471 const UV result = left / right;
1472 if (result * right == left) {
1473 SP--; /* result is valid */
1474 if (left_non_neg == right_non_neg) {
1475 /* signs identical, result is positive. */
1479 /* 2s complement assumption */
1480 if (result <= (UV)IV_MIN)
1481 SETi( -(IV)result );
1483 /* It's exact but too negative for IV. */
1484 SETn( -(NV)result );
1487 } /* tried integer divide but it was not an integer result */
1488 } /* else (PERL_ABS(result) < 1.0) or (both UVs in range for NV) */
1489 } /* one operand wasn't SvIOK */
1490 #endif /* PERL_TRY_UV_DIVIDE */
1492 NV right = SvNV_nomg(svr);
1493 NV left = SvNV_nomg(svl);
1494 (void)POPs;(void)POPs;
1495 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
1496 if (! Perl_isnan(right) && right == 0.0)
1500 DIE(aTHX_ "Illegal division by zero");
1501 PUSHn( left / right );
1508 dVAR; dSP; dATARGET;
1509 tryAMAGICbin_MG(modulo_amg, AMGf_assign|AMGf_numeric);
1513 bool left_neg = FALSE;
1514 bool right_neg = FALSE;
1515 bool use_double = FALSE;
1516 bool dright_valid = FALSE;
1519 SV * const svr = TOPs;
1520 SV * const svl = TOPm1s;
1521 if (SvIV_please_nomg(svr)) {
1522 right_neg = !SvUOK(svr);
1526 const IV biv = SvIVX(svr);
1529 right_neg = FALSE; /* effectively it's a UV now */
1536 dright = SvNV_nomg(svr);
1537 right_neg = dright < 0;
1540 if (dright < UV_MAX_P1) {
1541 right = U_V(dright);
1542 dright_valid = TRUE; /* In case we need to use double below. */
1548 /* At this point use_double is only true if right is out of range for
1549 a UV. In range NV has been rounded down to nearest UV and
1550 use_double false. */
1551 if (!use_double && SvIV_please_nomg(svl)) {
1552 left_neg = !SvUOK(svl);
1556 const IV aiv = SvIVX(svl);
1559 left_neg = FALSE; /* effectively it's a UV now */
1566 dleft = SvNV_nomg(svl);
1567 left_neg = dleft < 0;
1571 /* This should be exactly the 5.6 behaviour - if left and right are
1572 both in range for UV then use U_V() rather than floor. */
1574 if (dleft < UV_MAX_P1) {
1575 /* right was in range, so is dleft, so use UVs not double.
1579 /* left is out of range for UV, right was in range, so promote
1580 right (back) to double. */
1582 /* The +0.5 is used in 5.6 even though it is not strictly
1583 consistent with the implicit +0 floor in the U_V()
1584 inside the #if 1. */
1585 dleft = Perl_floor(dleft + 0.5);
1588 dright = Perl_floor(dright + 0.5);
1599 DIE(aTHX_ "Illegal modulus zero");
1601 dans = Perl_fmod(dleft, dright);
1602 if ((left_neg != right_neg) && dans)
1603 dans = dright - dans;
1606 sv_setnv(TARG, dans);
1612 DIE(aTHX_ "Illegal modulus zero");
1615 if ((left_neg != right_neg) && ans)
1618 /* XXX may warn: unary minus operator applied to unsigned type */
1619 /* could change -foo to be (~foo)+1 instead */
1620 if (ans <= ~((UV)IV_MAX)+1)
1621 sv_setiv(TARG, ~ans+1);
1623 sv_setnv(TARG, -(NV)ans);
1626 sv_setuv(TARG, ans);
1635 dVAR; dSP; dATARGET;
1639 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1640 /* TODO: think of some way of doing list-repeat overloading ??? */
1645 tryAMAGICbin_MG(repeat_amg, AMGf_assign);
1651 const UV uv = SvUV_nomg(sv);
1653 count = IV_MAX; /* The best we can do? */
1657 const IV iv = SvIV_nomg(sv);
1664 else if (SvNOKp(sv)) {
1665 const NV nv = SvNV_nomg(sv);
1672 count = SvIV_nomg(sv);
1674 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1676 static const char* const oom_list_extend = "Out of memory during list extend";
1677 const I32 items = SP - MARK;
1678 const I32 max = items * count;
1679 const U8 mod = PL_op->op_flags & OPf_MOD;
1681 MEM_WRAP_CHECK_1(max, SV*, oom_list_extend);
1682 /* Did the max computation overflow? */
1683 if (items > 0 && max > 0 && (max < items || max < count))
1684 Perl_croak(aTHX_ "%s", oom_list_extend);
1689 /* This code was intended to fix 20010809.028:
1692 for (($x =~ /./g) x 2) {
1693 print chop; # "abcdabcd" expected as output.
1696 * but that change (#11635) broke this code:
1698 $x = [("foo")x2]; # only one "foo" ended up in the anonlist.
1700 * I can't think of a better fix that doesn't introduce
1701 * an efficiency hit by copying the SVs. The stack isn't
1702 * refcounted, and mortalisation obviously doesn't
1703 * Do The Right Thing when the stack has more than
1704 * one pointer to the same mortal value.
1708 *SP = sv_2mortal(newSVsv(*SP));
1713 if (mod && SvPADTMP(*SP)) {
1714 assert(!IS_PADGV(*SP));
1715 *SP = sv_mortalcopy(*SP);
1723 repeatcpy((char*)(MARK + items), (char*)MARK,
1724 items * sizeof(const SV *), count - 1);
1727 else if (count <= 0)
1730 else { /* Note: mark already snarfed by pp_list */
1731 SV * const tmpstr = POPs;
1734 static const char* const oom_string_extend =
1735 "Out of memory during string extend";
1738 sv_setsv_nomg(TARG, tmpstr);
1739 SvPV_force_nomg(TARG, len);
1740 isutf = DO_UTF8(TARG);
1745 const STRLEN max = (UV)count * len;
1746 if (len > MEM_SIZE_MAX / count)
1747 Perl_croak(aTHX_ "%s", oom_string_extend);
1748 MEM_WRAP_CHECK_1(max, char, oom_string_extend);
1749 SvGROW(TARG, max + 1);
1750 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
1751 SvCUR_set(TARG, SvCUR(TARG) * count);
1753 *SvEND(TARG) = '\0';
1756 (void)SvPOK_only_UTF8(TARG);
1758 (void)SvPOK_only(TARG);
1760 if (PL_op->op_private & OPpREPEAT_DOLIST) {
1761 /* The parser saw this as a list repeat, and there
1762 are probably several items on the stack. But we're
1763 in scalar context, and there's no pp_list to save us
1764 now. So drop the rest of the items -- robin@kitsite.com
1776 dVAR; dSP; dATARGET; bool useleft; SV *svl, *svr;
1777 tryAMAGICbin_MG(subtr_amg, AMGf_assign|AMGf_numeric);
1780 useleft = USE_LEFT(svl);
1781 #ifdef PERL_PRESERVE_IVUV
1782 /* See comments in pp_add (in pp_hot.c) about Overflow, and how
1783 "bad things" happen if you rely on signed integers wrapping. */
1784 if (SvIV_please_nomg(svr)) {
1785 /* Unless the left argument is integer in range we are going to have to
1786 use NV maths. Hence only attempt to coerce the right argument if
1787 we know the left is integer. */
1794 a_valid = auvok = 1;
1795 /* left operand is undef, treat as zero. */
1797 /* Left operand is defined, so is it IV? */
1798 if (SvIV_please_nomg(svl)) {
1799 if ((auvok = SvUOK(svl)))
1802 const IV aiv = SvIVX(svl);
1805 auvok = 1; /* Now acting as a sign flag. */
1806 } else { /* 2s complement assumption for IV_MIN */
1814 bool result_good = 0;
1817 bool buvok = SvUOK(svr);
1822 const IV biv = SvIVX(svr);
1829 /* ?uvok if value is >= 0. basically, flagged as UV if it's +ve,
1830 else "IV" now, independent of how it came in.
1831 if a, b represents positive, A, B negative, a maps to -A etc
1836 all UV maths. negate result if A negative.
1837 subtract if signs same, add if signs differ. */
1839 if (auvok ^ buvok) {
1848 /* Must get smaller */
1853 if (result <= buv) {
1854 /* result really should be -(auv-buv). as its negation
1855 of true value, need to swap our result flag */
1867 if (result <= (UV)IV_MIN)
1868 SETi( -(IV)result );
1870 /* result valid, but out of range for IV. */
1871 SETn( -(NV)result );
1875 } /* Overflow, drop through to NVs. */
1880 NV value = SvNV_nomg(svr);
1884 /* left operand is undef, treat as zero - value */
1888 SETn( SvNV_nomg(svl) - value );
1895 dVAR; dSP; dATARGET; SV *svl, *svr;
1896 tryAMAGICbin_MG(lshift_amg, AMGf_assign|AMGf_numeric);
1900 const IV shift = SvIV_nomg(svr);
1901 if (PL_op->op_private & HINT_INTEGER) {
1902 const IV i = SvIV_nomg(svl);
1906 const UV u = SvUV_nomg(svl);
1915 dVAR; dSP; dATARGET; SV *svl, *svr;
1916 tryAMAGICbin_MG(rshift_amg, AMGf_assign|AMGf_numeric);
1920 const IV shift = SvIV_nomg(svr);
1921 if (PL_op->op_private & HINT_INTEGER) {
1922 const IV i = SvIV_nomg(svl);
1926 const UV u = SvUV_nomg(svl);
1938 tryAMAGICbin_MG(lt_amg, AMGf_set|AMGf_numeric);
1942 (SvIOK_notUV(left) && SvIOK_notUV(right))
1943 ? (SvIVX(left) < SvIVX(right))
1944 : (do_ncmp(left, right) == -1)
1954 tryAMAGICbin_MG(gt_amg, AMGf_set|AMGf_numeric);
1958 (SvIOK_notUV(left) && SvIOK_notUV(right))
1959 ? (SvIVX(left) > SvIVX(right))
1960 : (do_ncmp(left, right) == 1)
1970 tryAMAGICbin_MG(le_amg, AMGf_set|AMGf_numeric);
1974 (SvIOK_notUV(left) && SvIOK_notUV(right))
1975 ? (SvIVX(left) <= SvIVX(right))
1976 : (do_ncmp(left, right) <= 0)
1986 tryAMAGICbin_MG(ge_amg, AMGf_set|AMGf_numeric);
1990 (SvIOK_notUV(left) && SvIOK_notUV(right))
1991 ? (SvIVX(left) >= SvIVX(right))
1992 : ( (do_ncmp(left, right) & 2) == 0)
2002 tryAMAGICbin_MG(ne_amg, AMGf_set|AMGf_numeric);
2006 (SvIOK_notUV(left) && SvIOK_notUV(right))
2007 ? (SvIVX(left) != SvIVX(right))
2008 : (do_ncmp(left, right) != 0)
2013 /* compare left and right SVs. Returns:
2017 * 2: left or right was a NaN
2020 Perl_do_ncmp(pTHX_ SV* const left, SV * const right)
2024 PERL_ARGS_ASSERT_DO_NCMP;
2025 #ifdef PERL_PRESERVE_IVUV
2026 /* Fortunately it seems NaN isn't IOK */
2027 if (SvIV_please_nomg(right) && SvIV_please_nomg(left)) {
2029 const IV leftiv = SvIVX(left);
2030 if (!SvUOK(right)) {
2031 /* ## IV <=> IV ## */
2032 const IV rightiv = SvIVX(right);
2033 return (leftiv > rightiv) - (leftiv < rightiv);
2035 /* ## IV <=> UV ## */
2037 /* As (b) is a UV, it's >=0, so it must be < */
2040 const UV rightuv = SvUVX(right);
2041 return ((UV)leftiv > rightuv) - ((UV)leftiv < rightuv);
2046 /* ## UV <=> UV ## */
2047 const UV leftuv = SvUVX(left);
2048 const UV rightuv = SvUVX(right);
2049 return (leftuv > rightuv) - (leftuv < rightuv);
2051 /* ## UV <=> IV ## */
2053 const IV rightiv = SvIVX(right);
2055 /* As (a) is a UV, it's >=0, so it cannot be < */
2058 const UV leftuv = SvUVX(left);
2059 return (leftuv > (UV)rightiv) - (leftuv < (UV)rightiv);
2062 assert(0); /* NOTREACHED */
2066 NV const rnv = SvNV_nomg(right);
2067 NV const lnv = SvNV_nomg(left);
2069 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
2070 if (Perl_isnan(lnv) || Perl_isnan(rnv)) {
2073 return (lnv > rnv) - (lnv < rnv);
2092 tryAMAGICbin_MG(ncmp_amg, AMGf_numeric);
2095 value = do_ncmp(left, right);
2110 int amg_type = sle_amg;
2114 switch (PL_op->op_type) {
2133 tryAMAGICbin_MG(amg_type, AMGf_set);
2136 const int cmp = (IN_LOCALE_RUNTIME
2137 ? sv_cmp_locale_flags(left, right, 0)
2138 : sv_cmp_flags(left, right, 0));
2139 SETs(boolSV(cmp * multiplier < rhs));
2147 tryAMAGICbin_MG(seq_amg, AMGf_set);
2150 SETs(boolSV(sv_eq_flags(left, right, 0)));
2158 tryAMAGICbin_MG(sne_amg, AMGf_set);
2161 SETs(boolSV(!sv_eq_flags(left, right, 0)));
2169 tryAMAGICbin_MG(scmp_amg, 0);
2172 const int cmp = (IN_LOCALE_RUNTIME
2173 ? sv_cmp_locale_flags(left, right, 0)
2174 : sv_cmp_flags(left, right, 0));
2182 dVAR; dSP; dATARGET;
2183 tryAMAGICbin_MG(band_amg, AMGf_assign);
2186 if (SvNIOKp(left) || SvNIOKp(right)) {
2187 const bool left_ro_nonnum = !SvNIOKp(left) && SvREADONLY(left);
2188 const bool right_ro_nonnum = !SvNIOKp(right) && SvREADONLY(right);
2189 if (PL_op->op_private & HINT_INTEGER) {
2190 const IV i = SvIV_nomg(left) & SvIV_nomg(right);
2194 const UV u = SvUV_nomg(left) & SvUV_nomg(right);
2197 if (left_ro_nonnum && left != TARG) SvNIOK_off(left);
2198 if (right_ro_nonnum) SvNIOK_off(right);
2201 do_vop(PL_op->op_type, TARG, left, right);
2210 dVAR; dSP; dATARGET;
2211 const int op_type = PL_op->op_type;
2213 tryAMAGICbin_MG((op_type == OP_BIT_OR ? bor_amg : bxor_amg), AMGf_assign);
2216 if (SvNIOKp(left) || SvNIOKp(right)) {
2217 const bool left_ro_nonnum = !SvNIOKp(left) && SvREADONLY(left);
2218 const bool right_ro_nonnum = !SvNIOKp(right) && SvREADONLY(right);
2219 if (PL_op->op_private & HINT_INTEGER) {
2220 const IV l = (USE_LEFT(left) ? SvIV_nomg(left) : 0);
2221 const IV r = SvIV_nomg(right);
2222 const IV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2226 const UV l = (USE_LEFT(left) ? SvUV_nomg(left) : 0);
2227 const UV r = SvUV_nomg(right);
2228 const UV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2231 if (left_ro_nonnum && left != TARG) SvNIOK_off(left);
2232 if (right_ro_nonnum) SvNIOK_off(right);
2235 do_vop(op_type, TARG, left, right);
2242 PERL_STATIC_INLINE bool
2243 S_negate_string(pTHX)
2248 SV * const sv = TOPs;
2249 if (!SvPOKp(sv) || SvNIOK(sv) || (!SvPOK(sv) && SvNIOKp(sv)))
2251 s = SvPV_nomg_const(sv, len);
2252 if (isIDFIRST(*s)) {
2253 sv_setpvs(TARG, "-");
2256 else if (*s == '+' || (*s == '-' && !looks_like_number(sv))) {
2257 sv_setsv_nomg(TARG, sv);
2258 *SvPV_force_nomg(TARG, len) = *s == '-' ? '+' : '-';
2268 tryAMAGICun_MG(neg_amg, AMGf_numeric);
2269 if (S_negate_string(aTHX)) return NORMAL;
2271 SV * const sv = TOPs;
2274 /* It's publicly an integer */
2277 if (SvIVX(sv) == IV_MIN) {
2278 /* 2s complement assumption. */
2279 SETi(SvIVX(sv)); /* special case: -((UV)IV_MAX+1) ==
2283 else if (SvUVX(sv) <= IV_MAX) {
2288 else if (SvIVX(sv) != IV_MIN) {
2292 #ifdef PERL_PRESERVE_IVUV
2299 if (SvNIOKp(sv) && (SvNIOK(sv) || !SvPOK(sv)))
2300 SETn(-SvNV_nomg(sv));
2301 else if (SvPOKp(sv) && SvIV_please_nomg(sv))
2302 goto oops_its_an_int;
2304 SETn(-SvNV_nomg(sv));
2312 tryAMAGICun_MG(not_amg, AMGf_set);
2313 *PL_stack_sp = boolSV(!SvTRUE_nomg(*PL_stack_sp));
2320 tryAMAGICun_MG(compl_amg, AMGf_numeric);
2324 if (PL_op->op_private & HINT_INTEGER) {
2325 const IV i = ~SvIV_nomg(sv);
2329 const UV u = ~SvUV_nomg(sv);
2338 sv_copypv_nomg(TARG, sv);
2339 tmps = (U8*)SvPV_nomg(TARG, len);
2342 /* Calculate exact length, let's not estimate. */
2347 U8 * const send = tmps + len;
2348 U8 * const origtmps = tmps;
2349 const UV utf8flags = UTF8_ALLOW_ANYUV;
2351 while (tmps < send) {
2352 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2354 targlen += UNISKIP(~c);
2360 /* Now rewind strings and write them. */
2367 Newx(result, targlen + 1, U8);
2369 while (tmps < send) {
2370 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2372 p = uvchr_to_utf8_flags(p, ~c, UNICODE_ALLOW_ANY);
2375 sv_usepvn_flags(TARG, (char*)result, targlen,
2376 SV_HAS_TRAILING_NUL);
2383 Newx(result, nchar + 1, U8);
2385 while (tmps < send) {
2386 const U8 c = (U8)utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2391 sv_usepvn_flags(TARG, (char*)result, nchar, SV_HAS_TRAILING_NUL);
2400 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
2403 for ( ; anum >= (I32)sizeof(long); anum -= (I32)sizeof(long), tmpl++)
2408 for ( ; anum > 0; anum--, tmps++)
2416 /* integer versions of some of the above */
2420 dVAR; dSP; dATARGET;
2421 tryAMAGICbin_MG(mult_amg, AMGf_assign);
2424 SETi( left * right );
2432 dVAR; dSP; dATARGET;
2433 tryAMAGICbin_MG(div_amg, AMGf_assign);
2436 IV value = SvIV_nomg(right);
2438 DIE(aTHX_ "Illegal division by zero");
2439 num = SvIV_nomg(left);
2441 /* avoid FPE_INTOVF on some platforms when num is IV_MIN */
2445 value = num / value;
2451 #if defined(__GLIBC__) && IVSIZE == 8 && !defined(PERL_DEBUG_READONLY_OPS)
2458 /* This is the vanilla old i_modulo. */
2459 dVAR; dSP; dATARGET;
2460 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2464 DIE(aTHX_ "Illegal modulus zero");
2465 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2469 SETi( left % right );
2474 #if defined(__GLIBC__) && IVSIZE == 8 && !defined(PERL_DEBUG_READONLY_OPS)
2479 /* This is the i_modulo with the workaround for the _moddi3 bug
2480 * in (at least) glibc 2.2.5 (the PERL_ABS() the workaround).
2481 * See below for pp_i_modulo. */
2482 dVAR; dSP; dATARGET;
2483 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2487 DIE(aTHX_ "Illegal modulus zero");
2488 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2492 SETi( left % PERL_ABS(right) );
2499 dVAR; dSP; dATARGET;
2500 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2504 DIE(aTHX_ "Illegal modulus zero");
2505 /* The assumption is to use hereafter the old vanilla version... */
2507 PL_ppaddr[OP_I_MODULO] =
2509 /* .. but if we have glibc, we might have a buggy _moddi3
2510 * (at least glicb 2.2.5 is known to have this bug), in other
2511 * words our integer modulus with negative quad as the second
2512 * argument might be broken. Test for this and re-patch the
2513 * opcode dispatch table if that is the case, remembering to
2514 * also apply the workaround so that this first round works
2515 * right, too. See [perl #9402] for more information. */
2519 /* Cannot do this check with inlined IV constants since
2520 * that seems to work correctly even with the buggy glibc. */
2522 /* Yikes, we have the bug.
2523 * Patch in the workaround version. */
2525 PL_ppaddr[OP_I_MODULO] =
2526 &Perl_pp_i_modulo_1;
2527 /* Make certain we work right this time, too. */
2528 right = PERL_ABS(right);
2531 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2535 SETi( left % right );
2543 dVAR; dSP; dATARGET;
2544 tryAMAGICbin_MG(add_amg, AMGf_assign);
2546 dPOPTOPiirl_ul_nomg;
2547 SETi( left + right );
2554 dVAR; dSP; dATARGET;
2555 tryAMAGICbin_MG(subtr_amg, AMGf_assign);
2557 dPOPTOPiirl_ul_nomg;
2558 SETi( left - right );
2566 tryAMAGICbin_MG(lt_amg, AMGf_set);
2569 SETs(boolSV(left < right));
2577 tryAMAGICbin_MG(gt_amg, AMGf_set);
2580 SETs(boolSV(left > right));
2588 tryAMAGICbin_MG(le_amg, AMGf_set);
2591 SETs(boolSV(left <= right));
2599 tryAMAGICbin_MG(ge_amg, AMGf_set);
2602 SETs(boolSV(left >= right));
2610 tryAMAGICbin_MG(eq_amg, AMGf_set);
2613 SETs(boolSV(left == right));
2621 tryAMAGICbin_MG(ne_amg, AMGf_set);
2624 SETs(boolSV(left != right));
2632 tryAMAGICbin_MG(ncmp_amg, 0);
2639 else if (left < right)
2651 tryAMAGICun_MG(neg_amg, 0);
2652 if (S_negate_string(aTHX)) return NORMAL;
2654 SV * const sv = TOPs;
2655 IV const i = SvIV_nomg(sv);
2661 /* High falutin' math. */
2666 tryAMAGICbin_MG(atan2_amg, 0);
2669 SETn(Perl_atan2(left, right));
2677 int amg_type = sin_amg;
2678 const char *neg_report = NULL;
2679 NV (*func)(NV) = Perl_sin;
2680 const int op_type = PL_op->op_type;
2697 amg_type = sqrt_amg;
2699 neg_report = "sqrt";
2704 tryAMAGICun_MG(amg_type, 0);
2706 SV * const arg = POPs;
2707 const NV value = SvNV_nomg(arg);
2709 if (op_type == OP_LOG ? (value <= 0.0) : (value < 0.0)) {
2710 SET_NUMERIC_STANDARD();
2711 /* diag_listed_as: Can't take log of %g */
2712 DIE(aTHX_ "Can't take %s of %"NVgf, neg_report, value);
2715 XPUSHn(func(value));
2720 /* Support Configure command-line overrides for rand() functions.
2721 After 5.005, perhaps we should replace this by Configure support
2722 for drand48(), random(), or rand(). For 5.005, though, maintain
2723 compatibility by calling rand() but allow the user to override it.
2724 See INSTALL for details. --Andy Dougherty 15 July 1998
2726 /* Now it's after 5.005, and Configure supports drand48() and random(),
2727 in addition to rand(). So the overrides should not be needed any more.
2728 --Jarkko Hietaniemi 27 September 1998
2734 if (!PL_srand_called) {
2735 (void)seedDrand01((Rand_seed_t)seed());
2736 PL_srand_called = TRUE;
2746 SV * const sv = POPs;
2752 /* 1 of 2 things can be carried through SvNV, SP or TARG, SP was carried */
2760 sv_setnv_mg(TARG, value);
2771 if (MAXARG >= 1 && (TOPs || POPs)) {
2778 pv = SvPV(top, len);
2779 flags = grok_number(pv, len, &anum);
2781 if (!(flags & IS_NUMBER_IN_UV)) {
2782 Perl_ck_warner_d(aTHX_ packWARN(WARN_OVERFLOW),
2783 "Integer overflow in srand");
2791 (void)seedDrand01((Rand_seed_t)anum);
2792 PL_srand_called = TRUE;
2796 /* Historically srand always returned true. We can avoid breaking
2798 sv_setpvs(TARG, "0 but true");
2807 tryAMAGICun_MG(int_amg, AMGf_numeric);
2809 SV * const sv = TOPs;
2810 const IV iv = SvIV_nomg(sv);
2811 /* XXX it's arguable that compiler casting to IV might be subtly
2812 different from modf (for numbers inside (IV_MIN,UV_MAX)) in which
2813 else preferring IV has introduced a subtle behaviour change bug. OTOH
2814 relying on floating point to be accurate is a bug. */
2819 else if (SvIOK(sv)) {
2821 SETu(SvUV_nomg(sv));
2826 const NV value = SvNV_nomg(sv);
2828 if (value < (NV)UV_MAX + 0.5) {
2831 SETn(Perl_floor(value));
2835 if (value > (NV)IV_MIN - 0.5) {
2838 SETn(Perl_ceil(value));
2849 tryAMAGICun_MG(abs_amg, AMGf_numeric);
2851 SV * const sv = TOPs;
2852 /* This will cache the NV value if string isn't actually integer */
2853 const IV iv = SvIV_nomg(sv);
2858 else if (SvIOK(sv)) {
2859 /* IVX is precise */
2861 SETu(SvUV_nomg(sv)); /* force it to be numeric only */
2869 /* 2s complement assumption. Also, not really needed as
2870 IV_MIN and -IV_MIN should both be %100...00 and NV-able */
2876 const NV value = SvNV_nomg(sv);
2890 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES;
2894 SV* const sv = POPs;
2896 tmps = (SvPV_const(sv, len));
2898 /* If Unicode, try to downgrade
2899 * If not possible, croak. */
2900 SV* const tsv = sv_2mortal(newSVsv(sv));
2903 sv_utf8_downgrade(tsv, FALSE);
2904 tmps = SvPV_const(tsv, len);
2906 if (PL_op->op_type == OP_HEX)
2909 while (*tmps && len && isSPACE(*tmps))
2913 if (*tmps == 'x' || *tmps == 'X') {
2915 result_uv = grok_hex (tmps, &len, &flags, &result_nv);
2917 else if (*tmps == 'b' || *tmps == 'B')
2918 result_uv = grok_bin (tmps, &len, &flags, &result_nv);
2920 result_uv = grok_oct (tmps, &len, &flags, &result_nv);
2922 if (flags & PERL_SCAN_GREATER_THAN_UV_MAX) {
2936 SV * const sv = TOPs;
2941 SETi(sv_len_utf8_nomg(sv));
2945 (void)SvPV_nomg_const(sv,len);
2949 if (!SvPADTMP(TARG)) {
2950 sv_setsv_nomg(TARG, &PL_sv_undef);
2958 /* Returns false if substring is completely outside original string.
2959 No length is indicated by len_iv = 0 and len_is_uv = 0. len_is_uv must
2960 always be true for an explicit 0.
2963 Perl_translate_substr_offsets(pTHX_ STRLEN curlen, IV pos1_iv,
2964 bool pos1_is_uv, IV len_iv,
2965 bool len_is_uv, STRLEN *posp,
2971 PERL_ARGS_ASSERT_TRANSLATE_SUBSTR_OFFSETS;
2973 if (!pos1_is_uv && pos1_iv < 0 && curlen) {
2974 pos1_is_uv = curlen-1 > ~(UV)pos1_iv;
2977 if ((pos1_is_uv || pos1_iv > 0) && (UV)pos1_iv > curlen)
2980 if (len_iv || len_is_uv) {
2981 if (!len_is_uv && len_iv < 0) {
2982 pos2_iv = curlen + len_iv;
2984 pos2_is_uv = curlen-1 > ~(UV)len_iv;
2987 } else { /* len_iv >= 0 */
2988 if (!pos1_is_uv && pos1_iv < 0) {
2989 pos2_iv = pos1_iv + len_iv;
2990 pos2_is_uv = (UV)len_iv > (UV)IV_MAX;
2992 if ((UV)len_iv > curlen-(UV)pos1_iv)
2995 pos2_iv = pos1_iv+len_iv;
3005 if (!pos2_is_uv && pos2_iv < 0) {
3006 if (!pos1_is_uv && pos1_iv < 0)
3010 else if (!pos1_is_uv && pos1_iv < 0)
3013 if ((UV)pos2_iv < (UV)pos1_iv)
3015 if ((UV)pos2_iv > curlen)
3018 /* pos1_iv and pos2_iv both in 0..curlen, so the cast is safe */
3019 *posp = (STRLEN)( (UV)pos1_iv );
3020 *lenp = (STRLEN)( (UV)pos2_iv - (UV)pos1_iv );
3037 I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
3038 const bool rvalue = (GIMME_V != G_VOID);
3041 const char *repl = NULL;
3043 int num_args = PL_op->op_private & 7;
3044 bool repl_need_utf8_upgrade = FALSE;
3048 if(!(repl_sv = POPs)) num_args--;
3050 if ((len_sv = POPs)) {
3051 len_iv = SvIV(len_sv);
3052 len_is_uv = len_iv ? SvIOK_UV(len_sv) : 1;
3057 pos1_iv = SvIV(pos_sv);
3058 pos1_is_uv = SvIOK_UV(pos_sv);
3060 if (PL_op->op_private & OPpSUBSTR_REPL_FIRST) {
3065 if (lvalue && !repl_sv) {
3067 ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
3068 sv_magic(ret, NULL, PERL_MAGIC_substr, NULL, 0);
3070 LvTARG(ret) = SvREFCNT_inc_simple(sv);
3072 pos1_is_uv || pos1_iv >= 0
3073 ? (STRLEN)(UV)pos1_iv
3074 : (LvFLAGS(ret) |= 1, (STRLEN)(UV)-pos1_iv);
3076 len_is_uv || len_iv > 0
3077 ? (STRLEN)(UV)len_iv
3078 : (LvFLAGS(ret) |= 2, (STRLEN)(UV)-len_iv);
3081 PUSHs(ret); /* avoid SvSETMAGIC here */
3085 repl = SvPV_const(repl_sv, repl_len);
3088 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR),
3089 "Attempt to use reference as lvalue in substr"
3091 tmps = SvPV_force_nomg(sv, curlen);
3092 if (DO_UTF8(repl_sv) && repl_len) {
3094 sv_utf8_upgrade_nomg(sv);
3098 else if (DO_UTF8(sv))
3099 repl_need_utf8_upgrade = TRUE;
3101 else tmps = SvPV_const(sv, curlen);
3103 utf8_curlen = sv_or_pv_len_utf8(sv, tmps, curlen);
3104 if (utf8_curlen == curlen)
3107 curlen = utf8_curlen;
3113 STRLEN pos, len, byte_len, byte_pos;
3115 if (!translate_substr_offsets(
3116 curlen, pos1_iv, pos1_is_uv, len_iv, len_is_uv, &pos, &len
3120 byte_pos = utf8_curlen
3121 ? sv_or_pv_pos_u2b(sv, tmps, pos, &byte_len) : pos;
3126 SvTAINTED_off(TARG); /* decontaminate */
3127 SvUTF8_off(TARG); /* decontaminate */
3128 sv_setpvn(TARG, tmps, byte_len);
3129 #ifdef USE_LOCALE_COLLATE
3130 sv_unmagic(TARG, PERL_MAGIC_collxfrm);
3137 SV* repl_sv_copy = NULL;
3139 if (repl_need_utf8_upgrade) {
3140 repl_sv_copy = newSVsv(repl_sv);
3141 sv_utf8_upgrade(repl_sv_copy);
3142 repl = SvPV_const(repl_sv_copy, repl_len);
3146 sv_insert_flags(sv, byte_pos, byte_len, repl, repl_len, 0);
3147 SvREFCNT_dec(repl_sv_copy);
3159 Perl_croak(aTHX_ "substr outside of string");
3160 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR), "substr outside of string");
3167 const IV size = POPi;
3168 const IV offset = POPi;
3169 SV * const src = POPs;
3170 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
3173 if (lvalue) { /* it's an lvalue! */
3174 ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
3175 sv_magic(ret, NULL, PERL_MAGIC_vec, NULL, 0);
3177 LvTARG(ret) = SvREFCNT_inc_simple(src);
3178 LvTARGOFF(ret) = offset;
3179 LvTARGLEN(ret) = size;
3183 SvTAINTED_off(TARG); /* decontaminate */
3187 sv_setuv(ret, do_vecget(src, offset, size));
3203 const char *little_p;
3206 const bool is_index = PL_op->op_type == OP_INDEX;
3207 const bool threeargs = MAXARG >= 3 && (TOPs || ((void)POPs,0));
3213 big_p = SvPV_const(big, biglen);
3214 little_p = SvPV_const(little, llen);
3216 big_utf8 = DO_UTF8(big);
3217 little_utf8 = DO_UTF8(little);
3218 if (big_utf8 ^ little_utf8) {
3219 /* One needs to be upgraded. */
3220 if (little_utf8 && !PL_encoding) {
3221 /* Well, maybe instead we might be able to downgrade the small
3223 char * const pv = (char*)bytes_from_utf8((U8 *)little_p, &llen,
3226 /* If the large string is ISO-8859-1, and it's not possible to
3227 convert the small string to ISO-8859-1, then there is no
3228 way that it could be found anywhere by index. */
3233 /* At this point, pv is a malloc()ed string. So donate it to temp
3234 to ensure it will get free()d */
3235 little = temp = newSV(0);
3236 sv_usepvn(temp, pv, llen);
3237 little_p = SvPVX(little);
3240 ? newSVpvn(big_p, biglen) : newSVpvn(little_p, llen);
3243 sv_recode_to_utf8(temp, PL_encoding);
3245 sv_utf8_upgrade(temp);
3250 big_p = SvPV_const(big, biglen);
3253 little_p = SvPV_const(little, llen);
3257 if (SvGAMAGIC(big)) {
3258 /* Life just becomes a lot easier if I use a temporary here.
3259 Otherwise I need to avoid calls to sv_pos_u2b(), which (dangerously)
3260 will trigger magic and overloading again, as will fbm_instr()
3262 big = newSVpvn_flags(big_p, biglen,
3263 SVs_TEMP | (big_utf8 ? SVf_UTF8 : 0));
3266 if (SvGAMAGIC(little) || (is_index && !SvOK(little))) {
3267 /* index && SvOK() is a hack. fbm_instr() calls SvPV_const, which will
3268 warn on undef, and we've already triggered a warning with the
3269 SvPV_const some lines above. We can't remove that, as we need to
3270 call some SvPV to trigger overloading early and find out if the
3272 This is all getting to messy. The API isn't quite clean enough,
3273 because data access has side effects.
3275 little = newSVpvn_flags(little_p, llen,
3276 SVs_TEMP | (little_utf8 ? SVf_UTF8 : 0));
3277 little_p = SvPVX(little);
3281 offset = is_index ? 0 : biglen;
3283 if (big_utf8 && offset > 0)
3284 offset = sv_pos_u2b_flags(big, offset, 0, SV_CONST_RETURN);
3290 else if (offset > (SSize_t)biglen)
3292 if (!(little_p = is_index
3293 ? fbm_instr((unsigned char*)big_p + offset,
3294 (unsigned char*)big_p + biglen, little, 0)
3295 : rninstr(big_p, big_p + offset,
3296 little_p, little_p + llen)))
3299 retval = little_p - big_p;
3300 if (retval > 0 && big_utf8)
3301 retval = sv_pos_b2u_flags(big, retval, SV_CONST_RETURN);
3311 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
3312 SvTAINTED_off(TARG);
3313 do_sprintf(TARG, SP-MARK, MARK+1);
3314 TAINT_IF(SvTAINTED(TARG));
3326 const U8 *s = (U8*)SvPV_const(argsv, len);
3328 if (PL_encoding && SvPOK(argsv) && !DO_UTF8(argsv)) {
3329 SV * const tmpsv = sv_2mortal(newSVsv(argsv));
3330 s = (U8*)sv_recode_to_utf8(tmpsv, PL_encoding);
3331 len = UTF8SKIP(s); /* Should be well-formed; so this is its length */
3335 XPUSHu(DO_UTF8(argsv)
3336 ? utf8n_to_uvchr(s, len, 0, UTF8_ALLOW_ANYUV)
3350 if (!IN_BYTES /* under bytes, chr(-1) eq chr(0xff), etc. */
3351 && ((SvIOKp(top) && !SvIsUV(top) && SvIV_nomg(top) < 0)
3353 ((SvNOKp(top) || (SvOK(top) && !SvIsUV(top)))
3354 && SvNV_nomg(top) < 0.0))) {
3355 if (ckWARN(WARN_UTF8)) {
3356 if (SvGMAGICAL(top)) {
3357 SV *top2 = sv_newmortal();
3358 sv_setsv_nomg(top2, top);
3361 Perl_warner(aTHX_ packWARN(WARN_UTF8),
3362 "Invalid negative number (%"SVf") in chr", top);
3364 value = UNICODE_REPLACEMENT;
3366 value = SvUV_nomg(top);
3369 SvUPGRADE(TARG,SVt_PV);
3371 if (value > 255 && !IN_BYTES) {
3372 SvGROW(TARG, (STRLEN)UNISKIP(value)+1);
3373 tmps = (char*)uvchr_to_utf8_flags((U8*)SvPVX(TARG), value, 0);
3374 SvCUR_set(TARG, tmps - SvPVX_const(TARG));
3376 (void)SvPOK_only(TARG);
3385 *tmps++ = (char)value;
3387 (void)SvPOK_only(TARG);
3389 if (PL_encoding && !IN_BYTES) {
3390 sv_recode_to_utf8(TARG, PL_encoding);
3392 if (SvCUR(TARG) == 0
3393 || ! is_utf8_string((U8*)tmps, SvCUR(TARG))
3394 || UTF8_IS_REPLACEMENT((U8*) tmps, (U8*) tmps + SvCUR(TARG)))
3399 *tmps++ = (char)value;
3415 const char *tmps = SvPV_const(left, len);
3417 if (DO_UTF8(left)) {
3418 /* If Unicode, try to downgrade.
3419 * If not possible, croak.
3420 * Yes, we made this up. */
3421 SV* const tsv = sv_2mortal(newSVsv(left));
3424 sv_utf8_downgrade(tsv, FALSE);
3425 tmps = SvPV_const(tsv, len);
3427 # ifdef USE_ITHREADS
3429 if (!PL_reentrant_buffer->_crypt_struct_buffer) {
3430 /* This should be threadsafe because in ithreads there is only
3431 * one thread per interpreter. If this would not be true,
3432 * we would need a mutex to protect this malloc. */
3433 PL_reentrant_buffer->_crypt_struct_buffer =
3434 (struct crypt_data *)safemalloc(sizeof(struct crypt_data));
3435 #if defined(__GLIBC__) || defined(__EMX__)
3436 if (PL_reentrant_buffer->_crypt_struct_buffer) {
3437 PL_reentrant_buffer->_crypt_struct_buffer->initialized = 0;
3438 /* work around glibc-2.2.5 bug */
3439 PL_reentrant_buffer->_crypt_struct_buffer->current_saltbits = 0;
3443 # endif /* HAS_CRYPT_R */
3444 # endif /* USE_ITHREADS */
3446 sv_setpv(TARG, fcrypt(tmps, SvPV_nolen_const(right)));
3448 sv_setpv(TARG, PerlProc_crypt(tmps, SvPV_nolen_const(right)));
3454 "The crypt() function is unimplemented due to excessive paranoia.");
3458 /* Generally UTF-8 and UTF-EBCDIC are indistinguishable at this level. So
3459 * most comments below say UTF-8, when in fact they mean UTF-EBCDIC as well */
3463 /* Actually is both lcfirst() and ucfirst(). Only the first character
3464 * changes. This means that possibly we can change in-place, ie., just
3465 * take the source and change that one character and store it back, but not
3466 * if read-only etc, or if the length changes */
3471 STRLEN slen; /* slen is the byte length of the whole SV. */
3474 bool inplace; /* ? Convert first char only, in-place */
3475 bool doing_utf8 = FALSE; /* ? using utf8 */
3476 bool convert_source_to_utf8 = FALSE; /* ? need to convert */
3477 const int op_type = PL_op->op_type;
3480 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
3481 STRLEN ulen; /* ulen is the byte length of the original Unicode character
3482 * stored as UTF-8 at s. */
3483 STRLEN tculen; /* tculen is the byte length of the freshly titlecased (or
3484 * lowercased) character stored in tmpbuf. May be either
3485 * UTF-8 or not, but in either case is the number of bytes */
3487 s = (const U8*)SvPV_const(source, slen);
3489 /* We may be able to get away with changing only the first character, in
3490 * place, but not if read-only, etc. Later we may discover more reasons to
3491 * not convert in-place. */
3492 inplace = !SvREADONLY(source)
3493 && ( SvPADTMP(source)
3494 || ( SvTEMP(source) && !SvSMAGICAL(source)
3495 && SvREFCNT(source) == 1));
3497 /* First calculate what the changed first character should be. This affects
3498 * whether we can just swap it out, leaving the rest of the string unchanged,
3499 * or even if have to convert the dest to UTF-8 when the source isn't */
3501 if (! slen) { /* If empty */
3502 need = 1; /* still need a trailing NUL */
3505 else if (DO_UTF8(source)) { /* Is the source utf8? */
3508 if (op_type == OP_UCFIRST) {
3509 _to_utf8_title_flags(s, tmpbuf, &tculen, IN_LOCALE_RUNTIME);
3512 _to_utf8_lower_flags(s, tmpbuf, &tculen, IN_LOCALE_RUNTIME);
3515 /* we can't do in-place if the length changes. */
3516 if (ulen != tculen) inplace = FALSE;
3517 need = slen + 1 - ulen + tculen;
3519 else { /* Non-zero length, non-UTF-8, Need to consider locale and if
3520 * latin1 is treated as caseless. Note that a locale takes
3522 ulen = 1; /* Original character is 1 byte */
3523 tculen = 1; /* Most characters will require one byte, but this will
3524 * need to be overridden for the tricky ones */
3527 if (op_type == OP_LCFIRST) {
3529 /* lower case the first letter: no trickiness for any character */
3530 *tmpbuf = (IN_LOCALE_RUNTIME) ? toLOWER_LC(*s) :
3531 ((IN_UNI_8_BIT) ? toLOWER_LATIN1(*s) : toLOWER(*s));
3534 else if (IN_LOCALE_RUNTIME) {
3535 if (IN_UTF8_CTYPE_LOCALE) {
3539 *tmpbuf = (U8) toUPPER_LC(*s); /* This would be a bug if any
3540 locales have upper and title case
3543 else if (! IN_UNI_8_BIT) {
3544 *tmpbuf = toUPPER(*s); /* Returns caseless for non-ascii, or
3545 * on EBCDIC machines whatever the
3546 * native function does */
3549 /* Here, is ucfirst non-UTF-8, not in locale (unless that locale is
3550 * UTF-8, which we treat as not in locale), and cased latin1 */
3555 title_ord = _to_upper_title_latin1(*s, tmpbuf, &tculen, 's');
3557 assert(tculen == 2);
3559 /* If the result is an upper Latin1-range character, it can
3560 * still be represented in one byte, which is its ordinal */
3561 if (UTF8_IS_DOWNGRADEABLE_START(*tmpbuf)) {
3562 *tmpbuf = (U8) title_ord;
3566 /* Otherwise it became more than one ASCII character (in
3567 * the case of LATIN_SMALL_LETTER_SHARP_S) or changed to
3568 * beyond Latin1, so the number of bytes changed, so can't
3569 * replace just the first character in place. */
3572 /* If the result won't fit in a byte, the entire result
3573 * will have to be in UTF-8. Assume worst case sizing in
3574 * conversion. (all latin1 characters occupy at most two
3576 if (title_ord > 255) {
3578 convert_source_to_utf8 = TRUE;
3579 need = slen * 2 + 1;
3581 /* The (converted) UTF-8 and UTF-EBCDIC lengths of all
3582 * (both) characters whose title case is above 255 is
3586 else { /* LATIN_SMALL_LETTER_SHARP_S expands by 1 byte */
3587 need = slen + 1 + 1;
3591 } /* End of use Unicode (Latin1) semantics */
3592 } /* End of changing the case of the first character */
3594 /* Here, have the first character's changed case stored in tmpbuf. Ready to
3595 * generate the result */
3598 /* We can convert in place. This means we change just the first
3599 * character without disturbing the rest; no need to grow */
3601 s = d = (U8*)SvPV_force_nomg(source, slen);
3607 /* Here, we can't convert in place; we earlier calculated how much
3608 * space we will need, so grow to accommodate that */
3609 SvUPGRADE(dest, SVt_PV);
3610 d = (U8*)SvGROW(dest, need);
3611 (void)SvPOK_only(dest);
3618 if (! convert_source_to_utf8) {
3620 /* Here both source and dest are in UTF-8, but have to create
3621 * the entire output. We initialize the result to be the
3622 * title/lower cased first character, and then append the rest
3624 sv_setpvn(dest, (char*)tmpbuf, tculen);
3626 sv_catpvn(dest, (char*)(s + ulen), slen - ulen);
3630 const U8 *const send = s + slen;
3632 /* Here the dest needs to be in UTF-8, but the source isn't,
3633 * except we earlier UTF-8'd the first character of the source
3634 * into tmpbuf. First put that into dest, and then append the
3635 * rest of the source, converting it to UTF-8 as we go. */
3637 /* Assert tculen is 2 here because the only two characters that
3638 * get to this part of the code have 2-byte UTF-8 equivalents */
3640 *d++ = *(tmpbuf + 1);
3641 s++; /* We have just processed the 1st char */
3643 for (; s < send; s++) {
3644 d = uvchr_to_utf8(d, *s);
3647 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3651 else { /* in-place UTF-8. Just overwrite the first character */
3652 Copy(tmpbuf, d, tculen, U8);
3653 SvCUR_set(dest, need - 1);
3657 else { /* Neither source nor dest are in or need to be UTF-8 */
3659 if (inplace) { /* in-place, only need to change the 1st char */
3662 else { /* Not in-place */
3664 /* Copy the case-changed character(s) from tmpbuf */
3665 Copy(tmpbuf, d, tculen, U8);
3666 d += tculen - 1; /* Code below expects d to point to final
3667 * character stored */
3670 else { /* empty source */
3671 /* See bug #39028: Don't taint if empty */
3675 /* In a "use bytes" we don't treat the source as UTF-8, but, still want
3676 * the destination to retain that flag */
3677 if (SvUTF8(source) && ! IN_BYTES)
3680 if (!inplace) { /* Finish the rest of the string, unchanged */
3681 /* This will copy the trailing NUL */
3682 Copy(s + 1, d + 1, slen, U8);
3683 SvCUR_set(dest, need - 1);
3686 if (IN_LOCALE_RUNTIME) {
3690 if (dest != source && SvTAINTED(source))
3696 /* There's so much setup/teardown code common between uc and lc, I wonder if
3697 it would be worth merging the two, and just having a switch outside each
3698 of the three tight loops. There is less and less commonality though */
3712 if ((SvPADTMP(source)
3714 (SvTEMP(source) && !SvSMAGICAL(source) && SvREFCNT(source) == 1))
3715 && !SvREADONLY(source) && SvPOK(source)
3717 && ((IN_LOCALE_RUNTIME)
3718 ? ! IN_UTF8_CTYPE_LOCALE
3722 /* We can convert in place. The reason we can't if in UNI_8_BIT is to
3723 * make the loop tight, so we overwrite the source with the dest before
3724 * looking at it, and we need to look at the original source
3725 * afterwards. There would also need to be code added to handle
3726 * switching to not in-place in midstream if we run into characters
3727 * that change the length. Since being in locale overrides UNI_8_BIT,
3728 * that latter becomes irrelevant in the above test; instead for
3729 * locale, the size can't normally change, except if the locale is a
3732 s = d = (U8*)SvPV_force_nomg(source, len);
3739 s = (const U8*)SvPV_nomg_const(source, len);
3742 SvUPGRADE(dest, SVt_PV);
3743 d = (U8*)SvGROW(dest, min);
3744 (void)SvPOK_only(dest);
3749 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
3750 to check DO_UTF8 again here. */
3752 if (DO_UTF8(source)) {
3753 const U8 *const send = s + len;
3754 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
3756 /* All occurrences of these are to be moved to follow any other marks.
3757 * This is context-dependent. We may not be passed enough context to
3758 * move the iota subscript beyond all of them, but we do the best we can
3759 * with what we're given. The result is always better than if we
3760 * hadn't done this. And, the problem would only arise if we are
3761 * passed a character without all its combining marks, which would be
3762 * the caller's mistake. The information this is based on comes from a
3763 * comment in Unicode SpecialCasing.txt, (and the Standard's text
3764 * itself) and so can't be checked properly to see if it ever gets
3765 * revised. But the likelihood of it changing is remote */
3766 bool in_iota_subscript = FALSE;
3772 if (in_iota_subscript && ! _is_utf8_mark(s)) {
3774 /* A non-mark. Time to output the iota subscript */
3775 Copy(GREEK_CAPITAL_LETTER_IOTA_UTF8, d, capital_iota_len, U8);
3776 d += capital_iota_len;
3777 in_iota_subscript = FALSE;
3780 /* Then handle the current character. Get the changed case value
3781 * and copy it to the output buffer */
3784 uv = _to_utf8_upper_flags(s, tmpbuf, &ulen, IN_LOCALE_RUNTIME);
3785 #define GREEK_CAPITAL_LETTER_IOTA 0x0399
3786 #define COMBINING_GREEK_YPOGEGRAMMENI 0x0345
3787 if (uv == GREEK_CAPITAL_LETTER_IOTA
3788 && utf8_to_uvchr_buf(s, send, 0) == COMBINING_GREEK_YPOGEGRAMMENI)
3790 in_iota_subscript = TRUE;
3793 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
3794 /* If the eventually required minimum size outgrows the
3795 * available space, we need to grow. */
3796 const UV o = d - (U8*)SvPVX_const(dest);
3798 /* If someone uppercases one million U+03B0s we SvGROW()
3799 * one million times. Or we could try guessing how much to
3800 * allocate without allocating too much. Such is life.
3801 * See corresponding comment in lc code for another option
3804 d = (U8*)SvPVX(dest) + o;
3806 Copy(tmpbuf, d, ulen, U8);
3811 if (in_iota_subscript) {
3812 Copy(GREEK_CAPITAL_LETTER_IOTA_UTF8, d, capital_iota_len, U8);
3813 d += capital_iota_len;
3818 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3820 else { /* Not UTF-8 */
3822 const U8 *const send = s + len;
3824 /* Use locale casing if in locale; regular style if not treating
3825 * latin1 as having case; otherwise the latin1 casing. Do the
3826 * whole thing in a tight loop, for speed, */
3827 if (IN_LOCALE_RUNTIME) {
3828 if (IN_UTF8_CTYPE_LOCALE) {
3831 for (; s < send; d++, s++)
3832 *d = (U8) toUPPER_LC(*s);
3834 else if (! IN_UNI_8_BIT) {
3835 for (; s < send; d++, s++) {
3841 for (; s < send; d++, s++) {
3842 *d = toUPPER_LATIN1_MOD(*s);
3843 if (LIKELY(*d != LATIN_SMALL_LETTER_Y_WITH_DIAERESIS)) {
3847 /* The mainstream case is the tight loop above. To avoid
3848 * extra tests in that, all three characters that require
3849 * special handling are mapped by the MOD to the one tested
3851 * Use the source to distinguish between the three cases */
3853 if (*s == LATIN_SMALL_LETTER_SHARP_S) {
3855 /* uc() of this requires 2 characters, but they are
3856 * ASCII. If not enough room, grow the string */
3857 if (SvLEN(dest) < ++min) {
3858 const UV o = d - (U8*)SvPVX_const(dest);
3860 d = (U8*)SvPVX(dest) + o;
3862 *d++ = 'S'; *d = 'S'; /* upper case is 'SS' */
3863 continue; /* Back to the tight loop; still in ASCII */
3866 /* The other two special handling characters have their
3867 * upper cases outside the latin1 range, hence need to be
3868 * in UTF-8, so the whole result needs to be in UTF-8. So,
3869 * here we are somewhere in the middle of processing a
3870 * non-UTF-8 string, and realize that we will have to convert
3871 * the whole thing to UTF-8. What to do? There are
3872 * several possibilities. The simplest to code is to
3873 * convert what we have so far, set a flag, and continue on
3874 * in the loop. The flag would be tested each time through
3875 * the loop, and if set, the next character would be
3876 * converted to UTF-8 and stored. But, I (khw) didn't want
3877 * to slow down the mainstream case at all for this fairly
3878 * rare case, so I didn't want to add a test that didn't
3879 * absolutely have to be there in the loop, besides the
3880 * possibility that it would get too complicated for
3881 * optimizers to deal with. Another possibility is to just
3882 * give up, convert the source to UTF-8, and restart the
3883 * function that way. Another possibility is to convert
3884 * both what has already been processed and what is yet to
3885 * come separately to UTF-8, then jump into the loop that
3886 * handles UTF-8. But the most efficient time-wise of the
3887 * ones I could think of is what follows, and turned out to
3888 * not require much extra code. */
3890 /* Convert what we have so far into UTF-8, telling the
3891 * function that we know it should be converted, and to
3892 * allow extra space for what we haven't processed yet.
3893 * Assume the worst case space requirements for converting
3894 * what we haven't processed so far: that it will require
3895 * two bytes for each remaining source character, plus the
3896 * NUL at the end. This may cause the string pointer to
3897 * move, so re-find it. */
3899 len = d - (U8*)SvPVX_const(dest);
3900 SvCUR_set(dest, len);
3901 len = sv_utf8_upgrade_flags_grow(dest,
3902 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
3904 d = (U8*)SvPVX(dest) + len;
3906 /* Now process the remainder of the source, converting to
3907 * upper and UTF-8. If a resulting byte is invariant in
3908 * UTF-8, output it as-is, otherwise convert to UTF-8 and
3909 * append it to the output. */
3910 for (; s < send; s++) {
3911 (void) _to_upper_title_latin1(*s, d, &len, 'S');
3915 /* Here have processed the whole source; no need to continue
3916 * with the outer loop. Each character has been converted
3917 * to upper case and converted to UTF-8 */
3920 } /* End of processing all latin1-style chars */
3921 } /* End of processing all chars */
3922 } /* End of source is not empty */
3924 if (source != dest) {
3925 *d = '\0'; /* Here d points to 1 after last char, add NUL */
3926 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3928 } /* End of isn't utf8 */
3929 if (IN_LOCALE_RUNTIME) {
3933 if (dest != source && SvTAINTED(source))
3952 if ( ( SvPADTMP(source)
3953 || ( SvTEMP(source) && !SvSMAGICAL(source)
3954 && SvREFCNT(source) == 1 )
3956 && !SvREADONLY(source) && SvPOK(source)
3957 && !DO_UTF8(source)) {
3959 /* We can convert in place, as lowercasing anything in the latin1 range
3960 * (or else DO_UTF8 would have been on) doesn't lengthen it */
3962 s = d = (U8*)SvPV_force_nomg(source, len);
3969 s = (const U8*)SvPV_nomg_const(source, len);
3972 SvUPGRADE(dest, SVt_PV);
3973 d = (U8*)SvGROW(dest, min);
3974 (void)SvPOK_only(dest);
3979 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
3980 to check DO_UTF8 again here. */
3982 if (DO_UTF8(source)) {
3983 const U8 *const send = s + len;
3984 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
3987 const STRLEN u = UTF8SKIP(s);
3990 _to_utf8_lower_flags(s, tmpbuf, &ulen, IN_LOCALE_RUNTIME);
3992 /* Here is where we would do context-sensitive actions. See the
3993 * commit message for 86510fb15 for why there isn't any */
3995 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
3997 /* If the eventually required minimum size outgrows the
3998 * available space, we need to grow. */
3999 const UV o = d - (U8*)SvPVX_const(dest);
4001 /* If someone lowercases one million U+0130s we SvGROW() one
4002 * million times. Or we could try guessing how much to
4003 * allocate without allocating too much. Such is life.
4004 * Another option would be to grow an extra byte or two more
4005 * each time we need to grow, which would cut down the million
4006 * to 500K, with little waste */
4008 d = (U8*)SvPVX(dest) + o;
4011 /* Copy the newly lowercased letter to the output buffer we're
4013 Copy(tmpbuf, d, ulen, U8);
4016 } /* End of looping through the source string */
4019 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4020 } else { /* Not utf8 */
4022 const U8 *const send = s + len;
4024 /* Use locale casing if in locale; regular style if not treating
4025 * latin1 as having case; otherwise the latin1 casing. Do the
4026 * whole thing in a tight loop, for speed, */
4027 if (IN_LOCALE_RUNTIME) {
4028 for (; s < send; d++, s++)
4029 *d = toLOWER_LC(*s);
4031 else if (! IN_UNI_8_BIT) {
4032 for (; s < send; d++, s++) {
4037 for (; s < send; d++, s++) {
4038 *d = toLOWER_LATIN1(*s);
4042 if (source != dest) {
4044 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4047 if (IN_LOCALE_RUNTIME) {
4051 if (dest != source && SvTAINTED(source))
4060 SV * const sv = TOPs;
4062 const char *s = SvPV_const(sv,len);
4064 SvUTF8_off(TARG); /* decontaminate */
4067 SvUPGRADE(TARG, SVt_PV);
4068 SvGROW(TARG, (len * 2) + 1);
4072 STRLEN ulen = UTF8SKIP(s);
4073 bool to_quote = FALSE;
4075 if (UTF8_IS_INVARIANT(*s)) {
4076 if (_isQUOTEMETA(*s)) {
4080 else if (UTF8_IS_DOWNGRADEABLE_START(*s)) {
4082 /* In locale, we quote all non-ASCII Latin1 chars.
4083 * Otherwise use the quoting rules */
4084 if (IN_LOCALE_RUNTIME
4085 || _isQUOTEMETA(TWO_BYTE_UTF8_TO_NATIVE(*s, *(s + 1))))
4090 else if (is_QUOTEMETA_high(s)) {
4105 else if (IN_UNI_8_BIT) {
4107 if (_isQUOTEMETA(*s))
4113 /* For non UNI_8_BIT (and hence in locale) just quote all \W
4114 * including everything above ASCII */
4116 if (!isWORDCHAR_A(*s))
4122 SvCUR_set(TARG, d - SvPVX_const(TARG));
4123 (void)SvPOK_only_UTF8(TARG);
4126 sv_setpvn(TARG, s, len);
4143 U8 tmpbuf[UTF8_MAXBYTES_CASE + 1];
4144 const bool full_folding = TRUE;
4145 const U8 flags = ( full_folding ? FOLD_FLAGS_FULL : 0 )
4146 | ( IN_LOCALE_RUNTIME ? FOLD_FLAGS_LOCALE : 0 );
4148 /* This is a facsimile of pp_lc, but with a thousand bugs thanks to me.
4149 * You are welcome(?) -Hugmeir
4157 s = (const U8*)SvPV_nomg_const(source, len);
4159 if (ckWARN(WARN_UNINITIALIZED))
4160 report_uninit(source);
4167 SvUPGRADE(dest, SVt_PV);
4168 d = (U8*)SvGROW(dest, min);
4169 (void)SvPOK_only(dest);
4174 if (DO_UTF8(source)) { /* UTF-8 flagged string. */
4176 const STRLEN u = UTF8SKIP(s);
4179 _to_utf8_fold_flags(s, tmpbuf, &ulen, flags);
4181 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
4182 const UV o = d - (U8*)SvPVX_const(dest);
4184 d = (U8*)SvPVX(dest) + o;
4187 Copy(tmpbuf, d, ulen, U8);
4192 } /* Unflagged string */
4194 if ( IN_LOCALE_RUNTIME ) { /* Under locale */
4195 if (IN_UTF8_CTYPE_LOCALE) {
4196 goto do_uni_folding;
4198 for (; s < send; d++, s++)
4199 *d = (U8) toFOLD_LC(*s);
4201 else if ( !IN_UNI_8_BIT ) { /* Under nothing, or bytes */
4202 for (; s < send; d++, s++)
4207 /* For ASCII and the Latin-1 range, there's only two troublesome
4208 * folds, \x{DF} (\N{LATIN SMALL LETTER SHARP S}), which under full
4209 * casefolding becomes 'ss'; and \x{B5} (\N{MICRO SIGN}), which
4210 * under any fold becomes \x{3BC} (\N{GREEK SMALL LETTER MU}) --
4211 * For the rest, the casefold is their lowercase. */
4212 for (; s < send; d++, s++) {
4213 if (*s == MICRO_SIGN) {
4214 /* \N{MICRO SIGN}'s casefold is \N{GREEK SMALL LETTER MU},
4215 * which is outside of the latin-1 range. There's a couple
4216 * of ways to deal with this -- khw discusses them in
4217 * pp_lc/uc, so go there :) What we do here is upgrade what
4218 * we had already casefolded, then enter an inner loop that
4219 * appends the rest of the characters as UTF-8. */
4220 len = d - (U8*)SvPVX_const(dest);
4221 SvCUR_set(dest, len);
4222 len = sv_utf8_upgrade_flags_grow(dest,
4223 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
4224 /* The max expansion for latin1
4225 * chars is 1 byte becomes 2 */
4227 d = (U8*)SvPVX(dest) + len;
4229 Copy(GREEK_SMALL_LETTER_MU_UTF8, d, small_mu_len, U8);
4232 for (; s < send; s++) {
4234 UV fc = _to_uni_fold_flags(*s, tmpbuf, &ulen, flags);
4235 if UVCHR_IS_INVARIANT(fc) {
4237 && *s == LATIN_SMALL_LETTER_SHARP_S)
4246 Copy(tmpbuf, d, ulen, U8);
4252 else if (full_folding && *s == LATIN_SMALL_LETTER_SHARP_S) {
4253 /* Under full casefolding, LATIN SMALL LETTER SHARP S
4254 * becomes "ss", which may require growing the SV. */
4255 if (SvLEN(dest) < ++min) {
4256 const UV o = d - (U8*)SvPVX_const(dest);
4258 d = (U8*)SvPVX(dest) + o;
4263 else { /* If it's not one of those two, the fold is their lower
4265 *d = toLOWER_LATIN1(*s);
4271 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4273 if (IN_LOCALE_RUNTIME) {
4277 if (SvTAINTED(source))
4287 dVAR; dSP; dMARK; dORIGMARK;
4288 AV *const av = MUTABLE_AV(POPs);
4289 const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
4291 if (SvTYPE(av) == SVt_PVAV) {
4292 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
4293 bool can_preserve = FALSE;
4299 can_preserve = SvCANEXISTDELETE(av);
4302 if (lval && localizing) {
4305 for (svp = MARK + 1; svp <= SP; svp++) {
4306 const SSize_t elem = SvIV(*svp);
4310 if (max > AvMAX(av))
4314 while (++MARK <= SP) {
4316 SSize_t elem = SvIV(*MARK);
4317 bool preeminent = TRUE;
4319 if (localizing && can_preserve) {
4320 /* If we can determine whether the element exist,
4321 * Try to preserve the existenceness of a tied array
4322 * element by using EXISTS and DELETE if possible.
4323 * Fallback to FETCH and STORE otherwise. */
4324 preeminent = av_exists(av, elem);
4327 svp = av_fetch(av, elem, lval);
4330 DIE(aTHX_ PL_no_aelem, elem);
4333 save_aelem(av, elem, svp);
4335 SAVEADELETE(av, elem);
4338 *MARK = svp ? *svp : &PL_sv_undef;
4341 if (GIMME != G_ARRAY) {
4343 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
4352 AV *const av = MUTABLE_AV(POPs);
4353 I32 lval = (PL_op->op_flags & OPf_MOD);
4354 SSize_t items = SP - MARK;
4356 if (PL_op->op_private & OPpMAYBE_LVSUB) {
4357 const I32 flags = is_lvalue_sub();
4359 if (!(flags & OPpENTERSUB_INARGS))
4360 /* diag_listed_as: Can't modify %s in %s */
4361 Perl_croak(aTHX_ "Can't modify index/value array slice in list assignment");
4368 *(MARK+items*2-1) = *(MARK+items);
4374 while (++MARK <= SP) {
4377 svp = av_fetch(av, SvIV(*MARK), lval);
4379 if (!svp || !*svp || *svp == &PL_sv_undef) {
4380 DIE(aTHX_ PL_no_aelem, SvIV(*MARK));
4382 *MARK = sv_mortalcopy(*MARK);
4384 *++MARK = svp ? *svp : &PL_sv_undef;
4386 if (GIMME != G_ARRAY) {
4387 MARK = SP - items*2;
4388 *++MARK = items > 0 ? *SP : &PL_sv_undef;
4394 /* Smart dereferencing for keys, values and each */
4406 (SvTYPE(sv) != SVt_PVHV && SvTYPE(sv) != SVt_PVAV)
4411 "Type of argument to %s must be unblessed hashref or arrayref",
4412 PL_op_desc[PL_op->op_type] );
4415 if (PL_op->op_flags & OPf_SPECIAL && SvTYPE(sv) == SVt_PVAV)
4417 "Can't modify %s in %s",
4418 PL_op_desc[PL_op->op_type], PL_op_desc[PL_op->op_next->op_type]
4421 /* Delegate to correct function for op type */
4423 if (PL_op->op_type == OP_RKEYS || PL_op->op_type == OP_RVALUES) {
4424 return (SvTYPE(sv) == SVt_PVHV) ? Perl_do_kv(aTHX) : Perl_pp_akeys(aTHX);
4427 return (SvTYPE(sv) == SVt_PVHV)
4428 ? Perl_pp_each(aTHX)
4429 : Perl_pp_aeach(aTHX);
4437 AV *array = MUTABLE_AV(POPs);
4438 const I32 gimme = GIMME_V;
4439 IV *iterp = Perl_av_iter_p(aTHX_ array);
4440 const IV current = (*iterp)++;
4442 if (current > av_tindex(array)) {
4444 if (gimme == G_SCALAR)
4452 if (gimme == G_ARRAY) {
4453 SV **const element = av_fetch(array, current, 0);
4454 PUSHs(element ? *element : &PL_sv_undef);
4463 AV *array = MUTABLE_AV(POPs);
4464 const I32 gimme = GIMME_V;
4466 *Perl_av_iter_p(aTHX_ array) = 0;
4468 if (gimme == G_SCALAR) {
4470 PUSHi(av_tindex(array) + 1);
4472 else if (gimme == G_ARRAY) {
4473 IV n = Perl_av_len(aTHX_ array);
4478 if (PL_op->op_type == OP_AKEYS || PL_op->op_type == OP_RKEYS) {
4479 for (i = 0; i <= n; i++) {
4484 for (i = 0; i <= n; i++) {
4485 SV *const *const elem = Perl_av_fetch(aTHX_ array, i, 0);
4486 PUSHs(elem ? *elem : &PL_sv_undef);
4493 /* Associative arrays. */
4499 HV * hash = MUTABLE_HV(POPs);
4501 const I32 gimme = GIMME_V;
4504 /* might clobber stack_sp */
4505 entry = hv_iternext(hash);
4510 SV* const sv = hv_iterkeysv(entry);
4511 PUSHs(sv); /* won't clobber stack_sp */
4512 if (gimme == G_ARRAY) {
4515 /* might clobber stack_sp */
4516 val = hv_iterval(hash, entry);
4521 else if (gimme == G_SCALAR)
4528 S_do_delete_local(pTHX)
4532 const I32 gimme = GIMME_V;
4535 const bool sliced = !!(PL_op->op_private & OPpSLICE);
4536 SV *unsliced_keysv = sliced ? NULL : POPs;
4537 SV * const osv = POPs;
4538 SV **mark = sliced ? PL_stack_base + POPMARK : &unsliced_keysv-1;
4540 const bool tied = SvRMAGICAL(osv)
4541 && mg_find((const SV *)osv, PERL_MAGIC_tied);
4542 const bool can_preserve = SvCANEXISTDELETE(osv);
4543 const U32 type = SvTYPE(osv);
4544 SV ** const end = sliced ? SP : &unsliced_keysv;
4546 if (type == SVt_PVHV) { /* hash element */
4547 HV * const hv = MUTABLE_HV(osv);
4548 while (++MARK <= end) {
4549 SV * const keysv = *MARK;
4551 bool preeminent = TRUE;
4553 preeminent = hv_exists_ent(hv, keysv, 0);
4555 HE *he = hv_fetch_ent(hv, keysv, 1, 0);
4562 sv = hv_delete_ent(hv, keysv, 0, 0);
4564 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4567 if (!sv) DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
4568 save_helem_flags(hv, keysv, &sv, SAVEf_KEEPOLDELEM);
4570 *MARK = sv_mortalcopy(sv);
4576 SAVEHDELETE(hv, keysv);
4577 *MARK = &PL_sv_undef;
4581 else if (type == SVt_PVAV) { /* array element */
4582 if (PL_op->op_flags & OPf_SPECIAL) {
4583 AV * const av = MUTABLE_AV(osv);
4584 while (++MARK <= end) {
4585 SSize_t idx = SvIV(*MARK);
4587 bool preeminent = TRUE;
4589 preeminent = av_exists(av, idx);
4591 SV **svp = av_fetch(av, idx, 1);
4598 sv = av_delete(av, idx, 0);
4600 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4603 save_aelem_flags(av, idx, &sv, SAVEf_KEEPOLDELEM);
4605 *MARK = sv_mortalcopy(sv);
4611 SAVEADELETE(av, idx);
4612 *MARK = &PL_sv_undef;
4617 DIE(aTHX_ "panic: avhv_delete no longer supported");
4620 DIE(aTHX_ "Not a HASH reference");