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 (PL_op->op_private & OPpLVAL_INTRO)
72 if (!(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 I32 maxarg = AvFILL(MUTABLE_AV(TARG)) + 1;
93 if (SvMAGICAL(TARG)) {
95 for (i=0; i < (U32)maxarg; i++) {
96 SV * const * const svp = av_fetch(MUTABLE_AV(TARG), i, FALSE);
97 SP[i+1] = (svp) ? *svp : &PL_sv_undef;
101 Copy(AvARRAY((const AV *)TARG), SP+1, maxarg, SV*);
105 else if (gimme == G_SCALAR) {
106 SV* const sv = sv_newmortal();
107 const I32 maxarg = AvFILL(MUTABLE_AV(TARG)) + 1;
108 sv_setiv(sv, maxarg);
119 assert(SvTYPE(TARG) == SVt_PVHV);
121 if (PL_op->op_private & OPpLVAL_INTRO)
122 if (!(PL_op->op_private & OPpPAD_STATE))
123 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
124 if (PL_op->op_flags & OPf_REF)
126 else if (PL_op->op_private & OPpMAYBE_LVSUB) {
127 const I32 flags = is_lvalue_sub();
128 if (flags && !(flags & OPpENTERSUB_INARGS)) {
129 if (GIMME == G_SCALAR)
130 /* diag_listed_as: Can't return %s to lvalue scalar context */
131 Perl_croak(aTHX_ "Can't return hash to lvalue scalar context");
136 if (gimme == G_ARRAY) {
137 RETURNOP(Perl_do_kv(aTHX));
139 else if ((PL_op->op_private & OPpTRUEBOOL
140 || ( PL_op->op_private & OPpMAYBE_TRUEBOOL
141 && block_gimme() == G_VOID ))
142 && (!SvRMAGICAL(TARG) || !mg_find(TARG, PERL_MAGIC_tied)))
143 SETs(HvUSEDKEYS(TARG) ? &PL_sv_yes : sv_2mortal(newSViv(0)));
144 else if (gimme == G_SCALAR) {
145 SV* const sv = Perl_hv_scalar(aTHX_ MUTABLE_HV(TARG));
154 assert(SvTYPE(TARG) == SVt_PVCV);
162 SvPADSTALE_off(TARG);
170 mg_find(PadlistNAMESARRAY(CvPADLIST(find_runcv(NULL)))[ARGTARG],
172 assert(SvTYPE(TARG) == SVt_PVCV);
175 if (CvISXSUB(mg->mg_obj)) { /* constant */
176 /* XXX Should we clone it here? */
177 /* If this changes to use SAVECLEARSV, we can move the SAVECLEARSV
178 to introcv and remove the SvPADSTALE_off. */
179 SAVEPADSVANDMORTALIZE(ARGTARG);
180 PAD_SVl(ARGTARG) = SvREFCNT_inc_simple_NN(mg->mg_obj);
183 if (CvROOT(mg->mg_obj)) {
184 assert(CvCLONE(mg->mg_obj));
185 assert(!CvCLONED(mg->mg_obj));
187 cv_clone_into((CV *)mg->mg_obj,(CV *)TARG);
188 SAVECLEARSV(PAD_SVl(ARGTARG));
195 static const char S_no_symref_sv[] =
196 "Can't use string (\"%" SVf32 "\"%s) as %s ref while \"strict refs\" in use";
198 /* In some cases this function inspects PL_op. If this function is called
199 for new op types, more bool parameters may need to be added in place of
202 When noinit is true, the absence of a gv will cause a retval of undef.
203 This is unrelated to the cv-to-gv assignment case.
207 S_rv2gv(pTHX_ SV *sv, const bool vivify_sv, const bool strict,
211 if (!isGV(sv) || SvFAKE(sv)) SvGETMAGIC(sv);
214 sv = amagic_deref_call(sv, to_gv_amg);
218 if (SvTYPE(sv) == SVt_PVIO) {
219 GV * const gv = MUTABLE_GV(sv_newmortal());
220 gv_init(gv, 0, "__ANONIO__", 10, 0);
221 GvIOp(gv) = MUTABLE_IO(sv);
222 SvREFCNT_inc_void_NN(sv);
225 else if (!isGV_with_GP(sv))
226 return (SV *)Perl_die(aTHX_ "Not a GLOB reference");
229 if (!isGV_with_GP(sv)) {
231 /* If this is a 'my' scalar and flag is set then vivify
234 if (vivify_sv && sv != &PL_sv_undef) {
237 Perl_croak_no_modify();
238 if (cUNOP->op_targ) {
239 SV * const namesv = PAD_SV(cUNOP->op_targ);
240 HV *stash = CopSTASH(PL_curcop);
241 if (SvTYPE(stash) != SVt_PVHV) stash = NULL;
242 gv = MUTABLE_GV(newSV(0));
243 gv_init_sv(gv, stash, namesv, 0);
246 const char * const name = CopSTASHPV(PL_curcop);
247 gv = newGVgen_flags(name,
248 HvNAMEUTF8(CopSTASH(PL_curcop)) ? SVf_UTF8 : 0 );
250 prepare_SV_for_RV(sv);
251 SvRV_set(sv, MUTABLE_SV(gv));
256 if (PL_op->op_flags & OPf_REF || strict)
257 return (SV *)Perl_die(aTHX_ PL_no_usym, "a symbol");
258 if (ckWARN(WARN_UNINITIALIZED))
264 if (!(sv = MUTABLE_SV(gv_fetchsv_nomg(
265 sv, GV_ADDMG, SVt_PVGV
275 (SvPOKp(sv) && SvCUR(sv)>32 ? "..." : ""),
278 if ((PL_op->op_private & (OPpLVAL_INTRO|OPpDONT_INIT_GV))
279 == OPpDONT_INIT_GV) {
280 /* We are the target of a coderef assignment. Return
281 the scalar unchanged, and let pp_sasssign deal with
285 sv = MUTABLE_SV(gv_fetchsv_nomg(sv, GV_ADD, SVt_PVGV));
287 /* FAKE globs in the symbol table cause weird bugs (#77810) */
291 if (SvFAKE(sv) && !(PL_op->op_private & OPpALLOW_FAKE)) {
292 SV *newsv = sv_newmortal();
293 sv_setsv_flags(newsv, sv, 0);
305 sv, PL_op->op_private & OPpDEREF,
306 PL_op->op_private & HINT_STRICT_REFS,
307 ((PL_op->op_flags & OPf_SPECIAL) && !(PL_op->op_flags & OPf_MOD))
308 || PL_op->op_type == OP_READLINE
310 if (PL_op->op_private & OPpLVAL_INTRO)
311 save_gp(MUTABLE_GV(sv), !(PL_op->op_flags & OPf_SPECIAL));
316 /* Helper function for pp_rv2sv and pp_rv2av */
318 Perl_softref2xv(pTHX_ SV *const sv, const char *const what,
319 const svtype type, SV ***spp)
324 PERL_ARGS_ASSERT_SOFTREF2XV;
326 if (PL_op->op_private & HINT_STRICT_REFS) {
328 Perl_die(aTHX_ S_no_symref_sv, sv,
329 (SvPOKp(sv) && SvCUR(sv)>32 ? "..." : ""), what);
331 Perl_die(aTHX_ PL_no_usym, what);
335 PL_op->op_flags & OPf_REF
337 Perl_die(aTHX_ PL_no_usym, what);
338 if (ckWARN(WARN_UNINITIALIZED))
340 if (type != SVt_PV && GIMME_V == G_ARRAY) {
344 **spp = &PL_sv_undef;
347 if ((PL_op->op_flags & OPf_SPECIAL) &&
348 !(PL_op->op_flags & OPf_MOD))
350 if (!(gv = gv_fetchsv_nomg(sv, GV_ADDMG, type)))
352 **spp = &PL_sv_undef;
357 gv = gv_fetchsv_nomg(sv, GV_ADD, type);
370 sv = amagic_deref_call(sv, to_sv_amg);
374 switch (SvTYPE(sv)) {
380 DIE(aTHX_ "Not a SCALAR reference");
387 if (!isGV_with_GP(gv)) {
388 gv = Perl_softref2xv(aTHX_ sv, "a SCALAR", SVt_PV, &sp);
394 if (PL_op->op_flags & OPf_MOD) {
395 if (PL_op->op_private & OPpLVAL_INTRO) {
396 if (cUNOP->op_first->op_type == OP_NULL)
397 sv = save_scalar(MUTABLE_GV(TOPs));
399 sv = save_scalar(gv);
401 Perl_croak(aTHX_ "%s", PL_no_localize_ref);
403 else if (PL_op->op_private & OPpDEREF)
404 sv = vivify_ref(sv, PL_op->op_private & OPpDEREF);
413 AV * const av = MUTABLE_AV(TOPs);
414 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
416 SV ** const sv = Perl_av_arylen_p(aTHX_ MUTABLE_AV(av));
418 *sv = newSV_type(SVt_PVMG);
419 sv_magic(*sv, MUTABLE_SV(av), PERL_MAGIC_arylen, NULL, 0);
423 SETs(sv_2mortal(newSViv(AvFILL(MUTABLE_AV(av)))));
432 if (PL_op->op_flags & OPf_MOD || LVRET) {
433 SV * const ret = sv_2mortal(newSV_type(SVt_PVLV));/* Not TARG RT#67838 */
434 sv_magic(ret, NULL, PERL_MAGIC_pos, NULL, 0);
436 LvTARG(ret) = SvREFCNT_inc_simple(sv);
437 PUSHs(ret); /* no SvSETMAGIC */
441 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
442 const MAGIC * const mg = mg_find(sv, PERL_MAGIC_regex_global);
443 if (mg && mg->mg_len >= 0) {
461 const I32 flags = (PL_op->op_flags & OPf_SPECIAL)
463 : ((PL_op->op_private & (OPpLVAL_INTRO|OPpMAY_RETURN_CONSTANT))
464 == OPpMAY_RETURN_CONSTANT)
467 /* We usually try to add a non-existent subroutine in case of AUTOLOAD. */
468 /* (But not in defined().) */
470 CV *cv = sv_2cv(TOPs, &stash_unused, &gv, flags);
472 else if ((flags == (GV_ADD|GV_NOEXPAND)) && gv && SvROK(gv)) {
476 cv = MUTABLE_CV(&PL_sv_undef);
477 SETs(MUTABLE_SV(cv));
487 SV *ret = &PL_sv_undef;
489 if (SvGMAGICAL(TOPs)) SETs(sv_mortalcopy(TOPs));
490 if (SvPOK(TOPs) && SvCUR(TOPs) >= 7) {
491 const char * s = SvPVX_const(TOPs);
492 if (strnEQ(s, "CORE::", 6)) {
493 const int code = keyword(s + 6, SvCUR(TOPs) - 6, 1);
494 if (!code || code == -KEY_CORE)
495 DIE(aTHX_ "Can't find an opnumber for \"%"SVf"\"",
496 SVfARG(newSVpvn_flags(
498 (SvFLAGS(TOPs) & SVf_UTF8)|SVs_TEMP
501 SV * const sv = core_prototype(NULL, s + 6, code, NULL);
507 cv = sv_2cv(TOPs, &stash, &gv, 0);
509 ret = newSVpvn_flags(
510 CvPROTO(cv), CvPROTOLEN(cv), SVs_TEMP | SvUTF8(cv)
520 CV *cv = MUTABLE_CV(PAD_SV(PL_op->op_targ));
522 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
524 PUSHs(MUTABLE_SV(cv));
538 if (GIMME != G_ARRAY) {
542 *MARK = &PL_sv_undef;
543 *MARK = refto(*MARK);
547 EXTEND_MORTAL(SP - MARK);
549 *MARK = refto(*MARK);
554 S_refto(pTHX_ SV *sv)
559 PERL_ARGS_ASSERT_REFTO;
561 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
564 if (!(sv = LvTARG(sv)))
567 SvREFCNT_inc_void_NN(sv);
569 else if (SvTYPE(sv) == SVt_PVAV) {
570 if (!AvREAL((const AV *)sv) && AvREIFY((const AV *)sv))
571 av_reify(MUTABLE_AV(sv));
573 SvREFCNT_inc_void_NN(sv);
575 else if (SvPADTMP(sv) && !IS_PADGV(sv))
579 SvREFCNT_inc_void_NN(sv);
582 sv_upgrade(rv, SVt_IV);
591 SV * const sv = POPs;
596 if (!sv || !SvROK(sv))
599 (void)sv_ref(TARG,SvRV(sv),TRUE);
611 stash = CopSTASH(PL_curcop);
613 SV * const ssv = POPs;
617 if (!ssv) goto curstash;
618 if (!SvGMAGICAL(ssv) && !SvAMAGIC(ssv) && SvROK(ssv))
619 Perl_croak(aTHX_ "Attempt to bless into a reference");
620 ptr = SvPV_const(ssv,len);
622 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
623 "Explicit blessing to '' (assuming package main)");
624 stash = gv_stashpvn(ptr, len, GV_ADD|SvUTF8(ssv));
627 (void)sv_bless(TOPs, stash);
637 const char * const elem = SvPV_const(sv, len);
638 GV * const gv = MUTABLE_GV(POPs);
643 /* elem will always be NUL terminated. */
644 const char * const second_letter = elem + 1;
647 if (len == 5 && strEQ(second_letter, "RRAY"))
649 tmpRef = MUTABLE_SV(GvAV(gv));
650 if (tmpRef && !AvREAL((const AV *)tmpRef)
651 && AvREIFY((const AV *)tmpRef))
652 av_reify(MUTABLE_AV(tmpRef));
656 if (len == 4 && strEQ(second_letter, "ODE"))
657 tmpRef = MUTABLE_SV(GvCVu(gv));
660 if (len == 10 && strEQ(second_letter, "ILEHANDLE")) {
661 /* finally deprecated in 5.8.0 */
662 deprecate("*glob{FILEHANDLE}");
663 tmpRef = MUTABLE_SV(GvIOp(gv));
666 if (len == 6 && strEQ(second_letter, "ORMAT"))
667 tmpRef = MUTABLE_SV(GvFORM(gv));
670 if (len == 4 && strEQ(second_letter, "LOB"))
671 tmpRef = MUTABLE_SV(gv);
674 if (len == 4 && strEQ(second_letter, "ASH"))
675 tmpRef = MUTABLE_SV(GvHV(gv));
678 if (*second_letter == 'O' && !elem[2] && len == 2)
679 tmpRef = MUTABLE_SV(GvIOp(gv));
682 if (len == 4 && strEQ(second_letter, "AME"))
683 sv = newSVhek(GvNAME_HEK(gv));
686 if (len == 7 && strEQ(second_letter, "ACKAGE")) {
687 const HV * const stash = GvSTASH(gv);
688 const HEK * const hek = stash ? HvNAME_HEK(stash) : NULL;
689 sv = hek ? newSVhek(hek) : newSVpvs("__ANON__");
693 if (len == 6 && strEQ(second_letter, "CALAR"))
708 /* Pattern matching */
716 if (len == 0 || len > I32_MAX || !SvPOK(sv) || SvUTF8(sv) || SvVALID(sv)) {
717 /* Historically, study was skipped in these cases. */
721 /* Make study a no-op. It's no longer useful and its existence
722 complicates matters elsewhere. */
731 if (PL_op->op_flags & OPf_STACKED)
733 else if (PL_op->op_private & OPpTARGET_MY)
739 if(PL_op->op_type == OP_TRANSR) {
741 const char * const pv = SvPV(sv,len);
742 SV * const newsv = newSVpvn_flags(pv, len, SVs_TEMP|SvUTF8(sv));
747 TARG = sv_newmortal();
753 /* Lvalue operators. */
756 S_do_chomp(pTHX_ SV *retval, SV *sv, bool chomping)
762 PERL_ARGS_ASSERT_DO_CHOMP;
764 if (chomping && (RsSNARF(PL_rs) || RsRECORD(PL_rs)))
766 if (SvTYPE(sv) == SVt_PVAV) {
768 AV *const av = MUTABLE_AV(sv);
769 const I32 max = AvFILL(av);
771 for (i = 0; i <= max; i++) {
772 sv = MUTABLE_SV(av_fetch(av, i, FALSE));
773 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
774 do_chomp(retval, sv, chomping);
778 else if (SvTYPE(sv) == SVt_PVHV) {
779 HV* const hv = MUTABLE_HV(sv);
781 (void)hv_iterinit(hv);
782 while ((entry = hv_iternext(hv)))
783 do_chomp(retval, hv_iterval(hv,entry), chomping);
786 else if (SvREADONLY(sv)) {
787 Perl_croak_no_modify();
789 else if (SvIsCOW(sv)) {
790 sv_force_normal_flags(sv, 0);
795 /* XXX, here sv is utf8-ized as a side-effect!
796 If encoding.pm is used properly, almost string-generating
797 operations, including literal strings, chr(), input data, etc.
798 should have been utf8-ized already, right?
800 sv_recode_to_utf8(sv, PL_encoding);
806 char *temp_buffer = NULL;
815 while (len && s[-1] == '\n') {
822 STRLEN rslen, rs_charlen;
823 const char *rsptr = SvPV_const(PL_rs, rslen);
825 rs_charlen = SvUTF8(PL_rs)
829 if (SvUTF8(PL_rs) != SvUTF8(sv)) {
830 /* Assumption is that rs is shorter than the scalar. */
832 /* RS is utf8, scalar is 8 bit. */
834 temp_buffer = (char*)bytes_from_utf8((U8*)rsptr,
837 /* Cannot downgrade, therefore cannot possibly match
839 assert (temp_buffer == rsptr);
845 else if (PL_encoding) {
846 /* RS is 8 bit, encoding.pm is used.
847 * Do not recode PL_rs as a side-effect. */
848 svrecode = newSVpvn(rsptr, rslen);
849 sv_recode_to_utf8(svrecode, PL_encoding);
850 rsptr = SvPV_const(svrecode, rslen);
851 rs_charlen = sv_len_utf8(svrecode);
854 /* RS is 8 bit, scalar is utf8. */
855 temp_buffer = (char*)bytes_to_utf8((U8*)rsptr, &rslen);
869 if (memNE(s, rsptr, rslen))
871 SvIVX(retval) += rs_charlen;
874 s = SvPV_force_nomg_nolen(sv);
882 SvREFCNT_dec(svrecode);
884 Safefree(temp_buffer);
886 if (len && !SvPOK(sv))
887 s = SvPV_force_nomg(sv, len);
890 char * const send = s + len;
891 char * const start = s;
893 while (s > start && UTF8_IS_CONTINUATION(*s))
895 if (is_utf8_string((U8*)s, send - s)) {
896 sv_setpvn(retval, s, send - s);
898 SvCUR_set(sv, s - start);
904 sv_setpvs(retval, "");
908 sv_setpvn(retval, s, 1);
915 sv_setpvs(retval, "");
923 const bool chomping = PL_op->op_type == OP_SCHOMP;
927 do_chomp(TARG, TOPs, chomping);
934 dVAR; dSP; dMARK; dTARGET; dORIGMARK;
935 const bool chomping = PL_op->op_type == OP_CHOMP;
940 do_chomp(TARG, *++MARK, chomping);
951 if (!PL_op->op_private) {
960 SV_CHECK_THINKFIRST_COW_DROP(sv);
962 switch (SvTYPE(sv)) {
966 av_undef(MUTABLE_AV(sv));
969 hv_undef(MUTABLE_HV(sv));
972 if (cv_const_sv((const CV *)sv))
973 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
974 "Constant subroutine %"SVf" undefined",
975 SVfARG(CvANON((const CV *)sv)
976 ? newSVpvs_flags("(anonymous)", SVs_TEMP)
977 : sv_2mortal(newSVhek(GvENAME_HEK(CvGV((const CV *)sv))))));
981 /* let user-undef'd sub keep its identity */
982 GV* const gv = CvGV((const CV *)sv);
983 HEK * const hek = CvNAME_HEK((CV *)sv);
984 if (hek) share_hek_hek(hek);
985 cv_undef(MUTABLE_CV(sv));
986 if (gv) CvGV_set(MUTABLE_CV(sv), gv);
988 SvANY((CV *)sv)->xcv_gv_u.xcv_hek = hek;
994 assert(isGV_with_GP(sv));
1000 /* undef *Pkg::meth_name ... */
1002 = GvCVu((const GV *)sv) && (stash = GvSTASH((const GV *)sv))
1003 && HvENAME_get(stash);
1005 if((stash = GvHV((const GV *)sv))) {
1006 if(HvENAME_get(stash))
1007 SvREFCNT_inc_simple_void_NN(sv_2mortal((SV *)stash));
1011 gp_free(MUTABLE_GV(sv));
1013 GvGP_set(sv, gp_ref(gp));
1014 GvSV(sv) = newSV(0);
1015 GvLINE(sv) = CopLINE(PL_curcop);
1016 GvEGV(sv) = MUTABLE_GV(sv);
1020 mro_package_moved(NULL, stash, (const GV *)sv, 0);
1022 /* undef *Foo::ISA */
1023 if( strEQ(GvNAME((const GV *)sv), "ISA")
1024 && (stash = GvSTASH((const GV *)sv))
1025 && (method_changed || HvENAME(stash)) )
1026 mro_isa_changed_in(stash);
1027 else if(method_changed)
1028 mro_method_changed_in(
1029 GvSTASH((const GV *)sv)
1035 if (SvTYPE(sv) >= SVt_PV && SvPVX_const(sv) && SvLEN(sv)) {
1051 PL_op->op_type == OP_POSTINC || PL_op->op_type == OP_I_POSTINC;
1052 if (SvTYPE(TOPs) >= SVt_PVAV || (isGV_with_GP(TOPs) && !SvFAKE(TOPs)))
1053 Perl_croak_no_modify();
1055 TARG = sv_newmortal();
1056 sv_setsv(TARG, TOPs);
1057 if (!SvREADONLY(TOPs) && !SvGMAGICAL(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
1058 && SvIVX(TOPs) != (inc ? IV_MAX : IV_MIN))
1060 SvIV_set(TOPs, SvIVX(TOPs) + (inc ? 1 : -1));
1061 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
1065 else sv_dec_nomg(TOPs);
1067 /* special case for undef: see thread at 2003-03/msg00536.html in archive */
1068 if (inc && !SvOK(TARG))
1074 /* Ordinary operators. */
1078 dVAR; dSP; dATARGET; SV *svl, *svr;
1079 #ifdef PERL_PRESERVE_IVUV
1082 tryAMAGICbin_MG(pow_amg, AMGf_assign|AMGf_numeric);
1085 #ifdef PERL_PRESERVE_IVUV
1086 /* For integer to integer power, we do the calculation by hand wherever
1087 we're sure it is safe; otherwise we call pow() and try to convert to
1088 integer afterwards. */
1089 if (SvIV_please_nomg(svr) && SvIV_please_nomg(svl)) {
1097 const IV iv = SvIVX(svr);
1101 goto float_it; /* Can't do negative powers this way. */
1105 baseuok = SvUOK(svl);
1107 baseuv = SvUVX(svl);
1109 const IV iv = SvIVX(svl);
1112 baseuok = TRUE; /* effectively it's a UV now */
1114 baseuv = -iv; /* abs, baseuok == false records sign */
1117 /* now we have integer ** positive integer. */
1120 /* foo & (foo - 1) is zero only for a power of 2. */
1121 if (!(baseuv & (baseuv - 1))) {
1122 /* We are raising power-of-2 to a positive integer.
1123 The logic here will work for any base (even non-integer
1124 bases) but it can be less accurate than
1125 pow (base,power) or exp (power * log (base)) when the
1126 intermediate values start to spill out of the mantissa.
1127 With powers of 2 we know this can't happen.
1128 And powers of 2 are the favourite thing for perl
1129 programmers to notice ** not doing what they mean. */
1131 NV base = baseuok ? baseuv : -(NV)baseuv;
1136 while (power >>= 1) {
1144 SvIV_please_nomg(svr);
1147 unsigned int highbit = 8 * sizeof(UV);
1148 unsigned int diff = 8 * sizeof(UV);
1149 while (diff >>= 1) {
1151 if (baseuv >> highbit) {
1155 /* we now have baseuv < 2 ** highbit */
1156 if (power * highbit <= 8 * sizeof(UV)) {
1157 /* result will definitely fit in UV, so use UV math
1158 on same algorithm as above */
1161 const bool odd_power = cBOOL(power & 1);
1165 while (power >>= 1) {
1172 if (baseuok || !odd_power)
1173 /* answer is positive */
1175 else if (result <= (UV)IV_MAX)
1176 /* answer negative, fits in IV */
1177 SETi( -(IV)result );
1178 else if (result == (UV)IV_MIN)
1179 /* 2's complement assumption: special case IV_MIN */
1182 /* answer negative, doesn't fit */
1183 SETn( -(NV)result );
1191 NV right = SvNV_nomg(svr);
1192 NV left = SvNV_nomg(svl);
1195 #if defined(USE_LONG_DOUBLE) && defined(HAS_AIX_POWL_NEG_BASE_BUG)
1197 We are building perl with long double support and are on an AIX OS
1198 afflicted with a powl() function that wrongly returns NaNQ for any
1199 negative base. This was reported to IBM as PMR #23047-379 on
1200 03/06/2006. The problem exists in at least the following versions
1201 of AIX and the libm fileset, and no doubt others as well:
1203 AIX 4.3.3-ML10 bos.adt.libm 4.3.3.50
1204 AIX 5.1.0-ML04 bos.adt.libm 5.1.0.29
1205 AIX 5.2.0 bos.adt.libm 5.2.0.85
1207 So, until IBM fixes powl(), we provide the following workaround to
1208 handle the problem ourselves. Our logic is as follows: for
1209 negative bases (left), we use fmod(right, 2) to check if the
1210 exponent is an odd or even integer:
1212 - if odd, powl(left, right) == -powl(-left, right)
1213 - if even, powl(left, right) == powl(-left, right)
1215 If the exponent is not an integer, the result is rightly NaNQ, so
1216 we just return that (as NV_NAN).
1220 NV mod2 = Perl_fmod( right, 2.0 );
1221 if (mod2 == 1.0 || mod2 == -1.0) { /* odd integer */
1222 SETn( -Perl_pow( -left, right) );
1223 } else if (mod2 == 0.0) { /* even integer */
1224 SETn( Perl_pow( -left, right) );
1225 } else { /* fractional power */
1229 SETn( Perl_pow( left, right) );
1232 SETn( Perl_pow( left, right) );
1233 #endif /* HAS_AIX_POWL_NEG_BASE_BUG */
1235 #ifdef PERL_PRESERVE_IVUV
1237 SvIV_please_nomg(svr);
1245 dVAR; dSP; dATARGET; SV *svl, *svr;
1246 tryAMAGICbin_MG(mult_amg, AMGf_assign|AMGf_numeric);
1249 #ifdef PERL_PRESERVE_IVUV
1250 if (SvIV_please_nomg(svr)) {
1251 /* Unless the left argument is integer in range we are going to have to
1252 use NV maths. Hence only attempt to coerce the right argument if
1253 we know the left is integer. */
1254 /* Left operand is defined, so is it IV? */
1255 if (SvIV_please_nomg(svl)) {
1256 bool auvok = SvUOK(svl);
1257 bool buvok = SvUOK(svr);
1258 const UV topmask = (~ (UV)0) << (4 * sizeof (UV));
1259 const UV botmask = ~((~ (UV)0) << (4 * sizeof (UV)));
1268 const IV aiv = SvIVX(svl);
1271 auvok = TRUE; /* effectively it's a UV now */
1273 alow = -aiv; /* abs, auvok == false records sign */
1279 const IV biv = SvIVX(svr);
1282 buvok = TRUE; /* effectively it's a UV now */
1284 blow = -biv; /* abs, buvok == false records sign */
1288 /* If this does sign extension on unsigned it's time for plan B */
1289 ahigh = alow >> (4 * sizeof (UV));
1291 bhigh = blow >> (4 * sizeof (UV));
1293 if (ahigh && bhigh) {
1295 /* eg 32 bit is at least 0x10000 * 0x10000 == 0x100000000
1296 which is overflow. Drop to NVs below. */
1297 } else if (!ahigh && !bhigh) {
1298 /* eg 32 bit is at most 0xFFFF * 0xFFFF == 0xFFFE0001
1299 so the unsigned multiply cannot overflow. */
1300 const UV product = alow * blow;
1301 if (auvok == buvok) {
1302 /* -ve * -ve or +ve * +ve gives a +ve result. */
1306 } else if (product <= (UV)IV_MIN) {
1307 /* 2s complement assumption that (UV)-IV_MIN is correct. */
1308 /* -ve result, which could overflow an IV */
1310 SETi( -(IV)product );
1312 } /* else drop to NVs below. */
1314 /* One operand is large, 1 small */
1317 /* swap the operands */
1319 bhigh = blow; /* bhigh now the temp var for the swap */
1323 /* now, ((ahigh * blow) << half_UV_len) + (alow * blow)
1324 multiplies can't overflow. shift can, add can, -ve can. */
1325 product_middle = ahigh * blow;
1326 if (!(product_middle & topmask)) {
1327 /* OK, (ahigh * blow) won't lose bits when we shift it. */
1329 product_middle <<= (4 * sizeof (UV));
1330 product_low = alow * blow;
1332 /* as for pp_add, UV + something mustn't get smaller.
1333 IIRC ANSI mandates this wrapping *behaviour* for
1334 unsigned whatever the actual representation*/
1335 product_low += product_middle;
1336 if (product_low >= product_middle) {
1337 /* didn't overflow */
1338 if (auvok == buvok) {
1339 /* -ve * -ve or +ve * +ve gives a +ve result. */
1341 SETu( product_low );
1343 } else if (product_low <= (UV)IV_MIN) {
1344 /* 2s complement assumption again */
1345 /* -ve result, which could overflow an IV */
1347 SETi( -(IV)product_low );
1349 } /* else drop to NVs below. */
1351 } /* product_middle too large */
1352 } /* ahigh && bhigh */
1357 NV right = SvNV_nomg(svr);
1358 NV left = SvNV_nomg(svl);
1360 SETn( left * right );
1367 dVAR; dSP; dATARGET; SV *svl, *svr;
1368 tryAMAGICbin_MG(div_amg, AMGf_assign|AMGf_numeric);
1371 /* Only try to do UV divide first
1372 if ((SLOPPYDIVIDE is true) or
1373 (PERL_PRESERVE_IVUV is true and one or both SV is a UV too large
1375 The assumption is that it is better to use floating point divide
1376 whenever possible, only doing integer divide first if we can't be sure.
1377 If NV_PRESERVES_UV is true then we know at compile time that no UV
1378 can be too large to preserve, so don't need to compile the code to
1379 test the size of UVs. */
1382 # define PERL_TRY_UV_DIVIDE
1383 /* ensure that 20./5. == 4. */
1385 # ifdef PERL_PRESERVE_IVUV
1386 # ifndef NV_PRESERVES_UV
1387 # define PERL_TRY_UV_DIVIDE
1392 #ifdef PERL_TRY_UV_DIVIDE
1393 if (SvIV_please_nomg(svr) && SvIV_please_nomg(svl)) {
1394 bool left_non_neg = SvUOK(svl);
1395 bool right_non_neg = SvUOK(svr);
1399 if (right_non_neg) {
1403 const IV biv = SvIVX(svr);
1406 right_non_neg = TRUE; /* effectively it's a UV now */
1412 /* historically undef()/0 gives a "Use of uninitialized value"
1413 warning before dieing, hence this test goes here.
1414 If it were immediately before the second SvIV_please, then
1415 DIE() would be invoked before left was even inspected, so
1416 no inspection would give no warning. */
1418 DIE(aTHX_ "Illegal division by zero");
1424 const IV aiv = SvIVX(svl);
1427 left_non_neg = TRUE; /* effectively it's a UV now */
1436 /* For sloppy divide we always attempt integer division. */
1438 /* Otherwise we only attempt it if either or both operands
1439 would not be preserved by an NV. If both fit in NVs
1440 we fall through to the NV divide code below. However,
1441 as left >= right to ensure integer result here, we know that
1442 we can skip the test on the right operand - right big
1443 enough not to be preserved can't get here unless left is
1446 && (left > ((UV)1 << NV_PRESERVES_UV_BITS))
1449 /* Integer division can't overflow, but it can be imprecise. */
1450 const UV result = left / right;
1451 if (result * right == left) {
1452 SP--; /* result is valid */
1453 if (left_non_neg == right_non_neg) {
1454 /* signs identical, result is positive. */
1458 /* 2s complement assumption */
1459 if (result <= (UV)IV_MIN)
1460 SETi( -(IV)result );
1462 /* It's exact but too negative for IV. */
1463 SETn( -(NV)result );
1466 } /* tried integer divide but it was not an integer result */
1467 } /* else (PERL_ABS(result) < 1.0) or (both UVs in range for NV) */
1468 } /* one operand wasn't SvIOK */
1469 #endif /* PERL_TRY_UV_DIVIDE */
1471 NV right = SvNV_nomg(svr);
1472 NV left = SvNV_nomg(svl);
1473 (void)POPs;(void)POPs;
1474 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
1475 if (! Perl_isnan(right) && right == 0.0)
1479 DIE(aTHX_ "Illegal division by zero");
1480 PUSHn( left / right );
1487 dVAR; dSP; dATARGET;
1488 tryAMAGICbin_MG(modulo_amg, AMGf_assign|AMGf_numeric);
1492 bool left_neg = FALSE;
1493 bool right_neg = FALSE;
1494 bool use_double = FALSE;
1495 bool dright_valid = FALSE;
1498 SV * const svr = TOPs;
1499 SV * const svl = TOPm1s;
1500 if (SvIV_please_nomg(svr)) {
1501 right_neg = !SvUOK(svr);
1505 const IV biv = SvIVX(svr);
1508 right_neg = FALSE; /* effectively it's a UV now */
1515 dright = SvNV_nomg(svr);
1516 right_neg = dright < 0;
1519 if (dright < UV_MAX_P1) {
1520 right = U_V(dright);
1521 dright_valid = TRUE; /* In case we need to use double below. */
1527 /* At this point use_double is only true if right is out of range for
1528 a UV. In range NV has been rounded down to nearest UV and
1529 use_double false. */
1530 if (!use_double && SvIV_please_nomg(svl)) {
1531 left_neg = !SvUOK(svl);
1535 const IV aiv = SvIVX(svl);
1538 left_neg = FALSE; /* effectively it's a UV now */
1545 dleft = SvNV_nomg(svl);
1546 left_neg = dleft < 0;
1550 /* This should be exactly the 5.6 behaviour - if left and right are
1551 both in range for UV then use U_V() rather than floor. */
1553 if (dleft < UV_MAX_P1) {
1554 /* right was in range, so is dleft, so use UVs not double.
1558 /* left is out of range for UV, right was in range, so promote
1559 right (back) to double. */
1561 /* The +0.5 is used in 5.6 even though it is not strictly
1562 consistent with the implicit +0 floor in the U_V()
1563 inside the #if 1. */
1564 dleft = Perl_floor(dleft + 0.5);
1567 dright = Perl_floor(dright + 0.5);
1578 DIE(aTHX_ "Illegal modulus zero");
1580 dans = Perl_fmod(dleft, dright);
1581 if ((left_neg != right_neg) && dans)
1582 dans = dright - dans;
1585 sv_setnv(TARG, dans);
1591 DIE(aTHX_ "Illegal modulus zero");
1594 if ((left_neg != right_neg) && ans)
1597 /* XXX may warn: unary minus operator applied to unsigned type */
1598 /* could change -foo to be (~foo)+1 instead */
1599 if (ans <= ~((UV)IV_MAX)+1)
1600 sv_setiv(TARG, ~ans+1);
1602 sv_setnv(TARG, -(NV)ans);
1605 sv_setuv(TARG, ans);
1614 dVAR; dSP; dATARGET;
1618 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1619 /* TODO: think of some way of doing list-repeat overloading ??? */
1624 tryAMAGICbin_MG(repeat_amg, AMGf_assign);
1630 const UV uv = SvUV_nomg(sv);
1632 count = IV_MAX; /* The best we can do? */
1636 const IV iv = SvIV_nomg(sv);
1643 else if (SvNOKp(sv)) {
1644 const NV nv = SvNV_nomg(sv);
1651 count = SvIV_nomg(sv);
1653 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1655 static const char* const oom_list_extend = "Out of memory during list extend";
1656 const I32 items = SP - MARK;
1657 const I32 max = items * count;
1659 MEM_WRAP_CHECK_1(max, SV*, oom_list_extend);
1660 /* Did the max computation overflow? */
1661 if (items > 0 && max > 0 && (max < items || max < count))
1662 Perl_croak(aTHX_ "%s", oom_list_extend);
1667 /* This code was intended to fix 20010809.028:
1670 for (($x =~ /./g) x 2) {
1671 print chop; # "abcdabcd" expected as output.
1674 * but that change (#11635) broke this code:
1676 $x = [("foo")x2]; # only one "foo" ended up in the anonlist.
1678 * I can't think of a better fix that doesn't introduce
1679 * an efficiency hit by copying the SVs. The stack isn't
1680 * refcounted, and mortalisation obviously doesn't
1681 * Do The Right Thing when the stack has more than
1682 * one pointer to the same mortal value.
1686 *SP = sv_2mortal(newSVsv(*SP));
1696 repeatcpy((char*)(MARK + items), (char*)MARK,
1697 items * sizeof(const SV *), count - 1);
1700 else if (count <= 0)
1703 else { /* Note: mark already snarfed by pp_list */
1704 SV * const tmpstr = POPs;
1707 static const char* const oom_string_extend =
1708 "Out of memory during string extend";
1711 sv_setsv_nomg(TARG, tmpstr);
1712 SvPV_force_nomg(TARG, len);
1713 isutf = DO_UTF8(TARG);
1718 const STRLEN max = (UV)count * len;
1719 if (len > MEM_SIZE_MAX / count)
1720 Perl_croak(aTHX_ "%s", oom_string_extend);
1721 MEM_WRAP_CHECK_1(max, char, oom_string_extend);
1722 SvGROW(TARG, max + 1);
1723 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
1724 SvCUR_set(TARG, SvCUR(TARG) * count);
1726 *SvEND(TARG) = '\0';
1729 (void)SvPOK_only_UTF8(TARG);
1731 (void)SvPOK_only(TARG);
1733 if (PL_op->op_private & OPpREPEAT_DOLIST) {
1734 /* The parser saw this as a list repeat, and there
1735 are probably several items on the stack. But we're
1736 in scalar context, and there's no pp_list to save us
1737 now. So drop the rest of the items -- robin@kitsite.com
1749 dVAR; dSP; dATARGET; bool useleft; SV *svl, *svr;
1750 tryAMAGICbin_MG(subtr_amg, AMGf_assign|AMGf_numeric);
1753 useleft = USE_LEFT(svl);
1754 #ifdef PERL_PRESERVE_IVUV
1755 /* See comments in pp_add (in pp_hot.c) about Overflow, and how
1756 "bad things" happen if you rely on signed integers wrapping. */
1757 if (SvIV_please_nomg(svr)) {
1758 /* Unless the left argument is integer in range we are going to have to
1759 use NV maths. Hence only attempt to coerce the right argument if
1760 we know the left is integer. */
1767 a_valid = auvok = 1;
1768 /* left operand is undef, treat as zero. */
1770 /* Left operand is defined, so is it IV? */
1771 if (SvIV_please_nomg(svl)) {
1772 if ((auvok = SvUOK(svl)))
1775 const IV aiv = SvIVX(svl);
1778 auvok = 1; /* Now acting as a sign flag. */
1779 } else { /* 2s complement assumption for IV_MIN */
1787 bool result_good = 0;
1790 bool buvok = SvUOK(svr);
1795 const IV biv = SvIVX(svr);
1802 /* ?uvok if value is >= 0. basically, flagged as UV if it's +ve,
1803 else "IV" now, independent of how it came in.
1804 if a, b represents positive, A, B negative, a maps to -A etc
1809 all UV maths. negate result if A negative.
1810 subtract if signs same, add if signs differ. */
1812 if (auvok ^ buvok) {
1821 /* Must get smaller */
1826 if (result <= buv) {
1827 /* result really should be -(auv-buv). as its negation
1828 of true value, need to swap our result flag */
1840 if (result <= (UV)IV_MIN)
1841 SETi( -(IV)result );
1843 /* result valid, but out of range for IV. */
1844 SETn( -(NV)result );
1848 } /* Overflow, drop through to NVs. */
1853 NV value = SvNV_nomg(svr);
1857 /* left operand is undef, treat as zero - value */
1861 SETn( SvNV_nomg(svl) - value );
1868 dVAR; dSP; dATARGET; SV *svl, *svr;
1869 tryAMAGICbin_MG(lshift_amg, AMGf_assign|AMGf_numeric);
1873 const IV shift = SvIV_nomg(svr);
1874 if (PL_op->op_private & HINT_INTEGER) {
1875 const IV i = SvIV_nomg(svl);
1879 const UV u = SvUV_nomg(svl);
1888 dVAR; dSP; dATARGET; SV *svl, *svr;
1889 tryAMAGICbin_MG(rshift_amg, AMGf_assign|AMGf_numeric);
1893 const IV shift = SvIV_nomg(svr);
1894 if (PL_op->op_private & HINT_INTEGER) {
1895 const IV i = SvIV_nomg(svl);
1899 const UV u = SvUV_nomg(svl);
1911 tryAMAGICbin_MG(lt_amg, AMGf_set|AMGf_numeric);
1915 (SvIOK_notUV(left) && SvIOK_notUV(right))
1916 ? (SvIVX(left) < SvIVX(right))
1917 : (do_ncmp(left, right) == -1)
1927 tryAMAGICbin_MG(gt_amg, AMGf_set|AMGf_numeric);
1931 (SvIOK_notUV(left) && SvIOK_notUV(right))
1932 ? (SvIVX(left) > SvIVX(right))
1933 : (do_ncmp(left, right) == 1)
1943 tryAMAGICbin_MG(le_amg, AMGf_set|AMGf_numeric);
1947 (SvIOK_notUV(left) && SvIOK_notUV(right))
1948 ? (SvIVX(left) <= SvIVX(right))
1949 : (do_ncmp(left, right) <= 0)
1959 tryAMAGICbin_MG(ge_amg, AMGf_set|AMGf_numeric);
1963 (SvIOK_notUV(left) && SvIOK_notUV(right))
1964 ? (SvIVX(left) >= SvIVX(right))
1965 : ( (do_ncmp(left, right) & 2) == 0)
1975 tryAMAGICbin_MG(ne_amg, AMGf_set|AMGf_numeric);
1979 (SvIOK_notUV(left) && SvIOK_notUV(right))
1980 ? (SvIVX(left) != SvIVX(right))
1981 : (do_ncmp(left, right) != 0)
1986 /* compare left and right SVs. Returns:
1990 * 2: left or right was a NaN
1993 Perl_do_ncmp(pTHX_ SV* const left, SV * const right)
1997 PERL_ARGS_ASSERT_DO_NCMP;
1998 #ifdef PERL_PRESERVE_IVUV
1999 /* Fortunately it seems NaN isn't IOK */
2000 if (SvIV_please_nomg(right) && SvIV_please_nomg(left)) {
2002 const IV leftiv = SvIVX(left);
2003 if (!SvUOK(right)) {
2004 /* ## IV <=> IV ## */
2005 const IV rightiv = SvIVX(right);
2006 return (leftiv > rightiv) - (leftiv < rightiv);
2008 /* ## IV <=> UV ## */
2010 /* As (b) is a UV, it's >=0, so it must be < */
2013 const UV rightuv = SvUVX(right);
2014 return ((UV)leftiv > rightuv) - ((UV)leftiv < rightuv);
2019 /* ## UV <=> UV ## */
2020 const UV leftuv = SvUVX(left);
2021 const UV rightuv = SvUVX(right);
2022 return (leftuv > rightuv) - (leftuv < rightuv);
2024 /* ## UV <=> IV ## */
2026 const IV rightiv = SvIVX(right);
2028 /* As (a) is a UV, it's >=0, so it cannot be < */
2031 const UV leftuv = SvUVX(left);
2032 return (leftuv > (UV)rightiv) - (leftuv < (UV)rightiv);
2035 assert(0); /* NOTREACHED */
2039 NV const rnv = SvNV_nomg(right);
2040 NV const lnv = SvNV_nomg(left);
2042 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
2043 if (Perl_isnan(lnv) || Perl_isnan(rnv)) {
2046 return (lnv > rnv) - (lnv < rnv);
2065 tryAMAGICbin_MG(ncmp_amg, AMGf_numeric);
2068 value = do_ncmp(left, right);
2083 int amg_type = sle_amg;
2087 switch (PL_op->op_type) {
2106 tryAMAGICbin_MG(amg_type, AMGf_set);
2109 const int cmp = (IN_LOCALE_RUNTIME
2110 ? sv_cmp_locale_flags(left, right, 0)
2111 : sv_cmp_flags(left, right, 0));
2112 SETs(boolSV(cmp * multiplier < rhs));
2120 tryAMAGICbin_MG(seq_amg, AMGf_set);
2123 SETs(boolSV(sv_eq_flags(left, right, 0)));
2131 tryAMAGICbin_MG(sne_amg, AMGf_set);
2134 SETs(boolSV(!sv_eq_flags(left, right, 0)));
2142 tryAMAGICbin_MG(scmp_amg, 0);
2145 const int cmp = (IN_LOCALE_RUNTIME
2146 ? sv_cmp_locale_flags(left, right, 0)
2147 : sv_cmp_flags(left, right, 0));
2155 dVAR; dSP; dATARGET;
2156 tryAMAGICbin_MG(band_amg, AMGf_assign);
2159 if (SvNIOKp(left) || SvNIOKp(right)) {
2160 const bool left_ro_nonnum = !SvNIOKp(left) && SvREADONLY(left);
2161 const bool right_ro_nonnum = !SvNIOKp(right) && SvREADONLY(right);
2162 if (PL_op->op_private & HINT_INTEGER) {
2163 const IV i = SvIV_nomg(left) & SvIV_nomg(right);
2167 const UV u = SvUV_nomg(left) & SvUV_nomg(right);
2170 if (left_ro_nonnum && left != TARG) SvNIOK_off(left);
2171 if (right_ro_nonnum) SvNIOK_off(right);
2174 do_vop(PL_op->op_type, TARG, left, right);
2183 dVAR; dSP; dATARGET;
2184 const int op_type = PL_op->op_type;
2186 tryAMAGICbin_MG((op_type == OP_BIT_OR ? bor_amg : bxor_amg), AMGf_assign);
2189 if (SvNIOKp(left) || SvNIOKp(right)) {
2190 const bool left_ro_nonnum = !SvNIOKp(left) && SvREADONLY(left);
2191 const bool right_ro_nonnum = !SvNIOKp(right) && SvREADONLY(right);
2192 if (PL_op->op_private & HINT_INTEGER) {
2193 const IV l = (USE_LEFT(left) ? SvIV_nomg(left) : 0);
2194 const IV r = SvIV_nomg(right);
2195 const IV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2199 const UV l = (USE_LEFT(left) ? SvUV_nomg(left) : 0);
2200 const UV r = SvUV_nomg(right);
2201 const UV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2204 if (left_ro_nonnum && left != TARG) SvNIOK_off(left);
2205 if (right_ro_nonnum) SvNIOK_off(right);
2208 do_vop(op_type, TARG, left, right);
2215 PERL_STATIC_INLINE bool
2216 S_negate_string(pTHX)
2221 SV * const sv = TOPs;
2222 if (!SvPOKp(sv) || SvNIOK(sv) || (!SvPOK(sv) && SvNIOKp(sv)))
2224 s = SvPV_nomg_const(sv, len);
2225 if (isIDFIRST(*s)) {
2226 sv_setpvs(TARG, "-");
2229 else if (*s == '+' || (*s == '-' && !looks_like_number(sv))) {
2230 sv_setsv_nomg(TARG, sv);
2231 *SvPV_force_nomg(TARG, len) = *s == '-' ? '+' : '-';
2241 tryAMAGICun_MG(neg_amg, AMGf_numeric);
2242 if (S_negate_string(aTHX)) return NORMAL;
2244 SV * const sv = TOPs;
2247 /* It's publicly an integer */
2250 if (SvIVX(sv) == IV_MIN) {
2251 /* 2s complement assumption. */
2252 SETi(SvIVX(sv)); /* special case: -((UV)IV_MAX+1) ==
2256 else if (SvUVX(sv) <= IV_MAX) {
2261 else if (SvIVX(sv) != IV_MIN) {
2265 #ifdef PERL_PRESERVE_IVUV
2272 if (SvNIOKp(sv) && (SvNIOK(sv) || !SvPOK(sv)))
2273 SETn(-SvNV_nomg(sv));
2274 else if (SvPOKp(sv) && SvIV_please_nomg(sv))
2275 goto oops_its_an_int;
2277 SETn(-SvNV_nomg(sv));
2285 tryAMAGICun_MG(not_amg, AMGf_set);
2286 *PL_stack_sp = boolSV(!SvTRUE_nomg(*PL_stack_sp));
2293 tryAMAGICun_MG(compl_amg, AMGf_numeric);
2297 if (PL_op->op_private & HINT_INTEGER) {
2298 const IV i = ~SvIV_nomg(sv);
2302 const UV u = ~SvUV_nomg(sv);
2311 (void)SvPV_nomg_const(sv,len); /* force check for uninit var */
2312 sv_setsv_nomg(TARG, sv);
2313 tmps = (U8*)SvPV_force_nomg(TARG, len);
2316 /* Calculate exact length, let's not estimate. */
2321 U8 * const send = tmps + len;
2322 U8 * const origtmps = tmps;
2323 const UV utf8flags = UTF8_ALLOW_ANYUV;
2325 while (tmps < send) {
2326 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2328 targlen += UNISKIP(~c);
2334 /* Now rewind strings and write them. */
2341 Newx(result, targlen + 1, U8);
2343 while (tmps < send) {
2344 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2346 p = uvchr_to_utf8_flags(p, ~c, UNICODE_ALLOW_ANY);
2349 sv_usepvn_flags(TARG, (char*)result, targlen,
2350 SV_HAS_TRAILING_NUL);
2357 Newx(result, nchar + 1, U8);
2359 while (tmps < send) {
2360 const U8 c = (U8)utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2365 sv_usepvn_flags(TARG, (char*)result, nchar, SV_HAS_TRAILING_NUL);
2374 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
2377 for ( ; anum >= (I32)sizeof(long); anum -= (I32)sizeof(long), tmpl++)
2382 for ( ; anum > 0; anum--, tmps++)
2390 /* integer versions of some of the above */
2394 dVAR; dSP; dATARGET;
2395 tryAMAGICbin_MG(mult_amg, AMGf_assign);
2398 SETi( left * right );
2406 dVAR; dSP; dATARGET;
2407 tryAMAGICbin_MG(div_amg, AMGf_assign);
2410 IV value = SvIV_nomg(right);
2412 DIE(aTHX_ "Illegal division by zero");
2413 num = SvIV_nomg(left);
2415 /* avoid FPE_INTOVF on some platforms when num is IV_MIN */
2419 value = num / value;
2425 #if defined(__GLIBC__) && IVSIZE == 8 && !defined(PERL_DEBUG_READONLY_OPS)
2432 /* This is the vanilla old i_modulo. */
2433 dVAR; dSP; dATARGET;
2434 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2438 DIE(aTHX_ "Illegal modulus zero");
2439 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2443 SETi( left % right );
2448 #if defined(__GLIBC__) && IVSIZE == 8 && !defined(PERL_DEBUG_READONLY_OPS)
2453 /* This is the i_modulo with the workaround for the _moddi3 bug
2454 * in (at least) glibc 2.2.5 (the PERL_ABS() the workaround).
2455 * See below for pp_i_modulo. */
2456 dVAR; dSP; dATARGET;
2457 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2461 DIE(aTHX_ "Illegal modulus zero");
2462 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2466 SETi( left % PERL_ABS(right) );
2473 dVAR; dSP; dATARGET;
2474 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2478 DIE(aTHX_ "Illegal modulus zero");
2479 /* The assumption is to use hereafter the old vanilla version... */
2481 PL_ppaddr[OP_I_MODULO] =
2483 /* .. but if we have glibc, we might have a buggy _moddi3
2484 * (at least glicb 2.2.5 is known to have this bug), in other
2485 * words our integer modulus with negative quad as the second
2486 * argument might be broken. Test for this and re-patch the
2487 * opcode dispatch table if that is the case, remembering to
2488 * also apply the workaround so that this first round works
2489 * right, too. See [perl #9402] for more information. */
2493 /* Cannot do this check with inlined IV constants since
2494 * that seems to work correctly even with the buggy glibc. */
2496 /* Yikes, we have the bug.
2497 * Patch in the workaround version. */
2499 PL_ppaddr[OP_I_MODULO] =
2500 &Perl_pp_i_modulo_1;
2501 /* Make certain we work right this time, too. */
2502 right = PERL_ABS(right);
2505 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2509 SETi( left % right );
2517 dVAR; dSP; dATARGET;
2518 tryAMAGICbin_MG(add_amg, AMGf_assign);
2520 dPOPTOPiirl_ul_nomg;
2521 SETi( left + right );
2528 dVAR; dSP; dATARGET;
2529 tryAMAGICbin_MG(subtr_amg, AMGf_assign);
2531 dPOPTOPiirl_ul_nomg;
2532 SETi( left - right );
2540 tryAMAGICbin_MG(lt_amg, AMGf_set);
2543 SETs(boolSV(left < right));
2551 tryAMAGICbin_MG(gt_amg, AMGf_set);
2554 SETs(boolSV(left > right));
2562 tryAMAGICbin_MG(le_amg, AMGf_set);
2565 SETs(boolSV(left <= right));
2573 tryAMAGICbin_MG(ge_amg, AMGf_set);
2576 SETs(boolSV(left >= right));
2584 tryAMAGICbin_MG(eq_amg, AMGf_set);
2587 SETs(boolSV(left == right));
2595 tryAMAGICbin_MG(ne_amg, AMGf_set);
2598 SETs(boolSV(left != right));
2606 tryAMAGICbin_MG(ncmp_amg, 0);
2613 else if (left < right)
2625 tryAMAGICun_MG(neg_amg, 0);
2626 if (S_negate_string(aTHX)) return NORMAL;
2628 SV * const sv = TOPs;
2629 IV const i = SvIV_nomg(sv);
2635 /* High falutin' math. */
2640 tryAMAGICbin_MG(atan2_amg, 0);
2643 SETn(Perl_atan2(left, right));
2651 int amg_type = sin_amg;
2652 const char *neg_report = NULL;
2653 NV (*func)(NV) = Perl_sin;
2654 const int op_type = PL_op->op_type;
2671 amg_type = sqrt_amg;
2673 neg_report = "sqrt";
2678 tryAMAGICun_MG(amg_type, 0);
2680 SV * const arg = POPs;
2681 const NV value = SvNV_nomg(arg);
2683 if (op_type == OP_LOG ? (value <= 0.0) : (value < 0.0)) {
2684 SET_NUMERIC_STANDARD();
2685 /* diag_listed_as: Can't take log of %g */
2686 DIE(aTHX_ "Can't take %s of %"NVgf, neg_report, value);
2689 XPUSHn(func(value));
2694 /* Support Configure command-line overrides for rand() functions.
2695 After 5.005, perhaps we should replace this by Configure support
2696 for drand48(), random(), or rand(). For 5.005, though, maintain
2697 compatibility by calling rand() but allow the user to override it.
2698 See INSTALL for details. --Andy Dougherty 15 July 1998
2700 /* Now it's after 5.005, and Configure supports drand48() and random(),
2701 in addition to rand(). So the overrides should not be needed any more.
2702 --Jarkko Hietaniemi 27 September 1998
2705 #ifndef HAS_DRAND48_PROTO
2706 extern double drand48 (void);
2712 if (!PL_srand_called) {
2713 (void)seedDrand01((Rand_seed_t)seed());
2714 PL_srand_called = TRUE;
2724 SV * const sv = POPs;
2730 /* 1 of 2 things can be carried through SvNV, SP or TARG, SP was carried */
2738 sv_setnv_mg(TARG, value);
2749 if (MAXARG >= 1 && (TOPs || POPs)) {
2756 pv = SvPV(top, len);
2757 flags = grok_number(pv, len, &anum);
2759 if (!(flags & IS_NUMBER_IN_UV)) {
2760 Perl_ck_warner_d(aTHX_ packWARN(WARN_OVERFLOW),
2761 "Integer overflow in srand");
2769 (void)seedDrand01((Rand_seed_t)anum);
2770 PL_srand_called = TRUE;
2774 /* Historically srand always returned true. We can avoid breaking
2776 sv_setpvs(TARG, "0 but true");
2785 tryAMAGICun_MG(int_amg, AMGf_numeric);
2787 SV * const sv = TOPs;
2788 const IV iv = SvIV_nomg(sv);
2789 /* XXX it's arguable that compiler casting to IV might be subtly
2790 different from modf (for numbers inside (IV_MIN,UV_MAX)) in which
2791 else preferring IV has introduced a subtle behaviour change bug. OTOH
2792 relying on floating point to be accurate is a bug. */
2797 else if (SvIOK(sv)) {
2799 SETu(SvUV_nomg(sv));
2804 const NV value = SvNV_nomg(sv);
2806 if (value < (NV)UV_MAX + 0.5) {
2809 SETn(Perl_floor(value));
2813 if (value > (NV)IV_MIN - 0.5) {
2816 SETn(Perl_ceil(value));
2827 tryAMAGICun_MG(abs_amg, AMGf_numeric);
2829 SV * const sv = TOPs;
2830 /* This will cache the NV value if string isn't actually integer */
2831 const IV iv = SvIV_nomg(sv);
2836 else if (SvIOK(sv)) {
2837 /* IVX is precise */
2839 SETu(SvUV_nomg(sv)); /* force it to be numeric only */
2847 /* 2s complement assumption. Also, not really needed as
2848 IV_MIN and -IV_MIN should both be %100...00 and NV-able */
2854 const NV value = SvNV_nomg(sv);
2868 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES;
2872 SV* const sv = POPs;
2874 tmps = (SvPV_const(sv, len));
2876 /* If Unicode, try to downgrade
2877 * If not possible, croak. */
2878 SV* const tsv = sv_2mortal(newSVsv(sv));
2881 sv_utf8_downgrade(tsv, FALSE);
2882 tmps = SvPV_const(tsv, len);
2884 if (PL_op->op_type == OP_HEX)
2887 while (*tmps && len && isSPACE(*tmps))
2891 if (*tmps == 'x' || *tmps == 'X') {
2893 result_uv = grok_hex (tmps, &len, &flags, &result_nv);
2895 else if (*tmps == 'b' || *tmps == 'B')
2896 result_uv = grok_bin (tmps, &len, &flags, &result_nv);
2898 result_uv = grok_oct (tmps, &len, &flags, &result_nv);
2900 if (flags & PERL_SCAN_GREATER_THAN_UV_MAX) {
2914 SV * const sv = TOPs;
2919 SETi(sv_len_utf8_nomg(sv));
2923 (void)SvPV_nomg_const(sv,len);
2927 if (!SvPADTMP(TARG)) {
2928 sv_setsv_nomg(TARG, &PL_sv_undef);
2936 /* Returns false if substring is completely outside original string.
2937 No length is indicated by len_iv = 0 and len_is_uv = 0. len_is_uv must
2938 always be true for an explicit 0.
2941 Perl_translate_substr_offsets(pTHX_ STRLEN curlen, IV pos1_iv,
2942 bool pos1_is_uv, IV len_iv,
2943 bool len_is_uv, STRLEN *posp,
2949 PERL_ARGS_ASSERT_TRANSLATE_SUBSTR_OFFSETS;
2951 if (!pos1_is_uv && pos1_iv < 0 && curlen) {
2952 pos1_is_uv = curlen-1 > ~(UV)pos1_iv;
2955 if ((pos1_is_uv || pos1_iv > 0) && (UV)pos1_iv > curlen)
2958 if (len_iv || len_is_uv) {
2959 if (!len_is_uv && len_iv < 0) {
2960 pos2_iv = curlen + len_iv;
2962 pos2_is_uv = curlen-1 > ~(UV)len_iv;
2965 } else { /* len_iv >= 0 */
2966 if (!pos1_is_uv && pos1_iv < 0) {
2967 pos2_iv = pos1_iv + len_iv;
2968 pos2_is_uv = (UV)len_iv > (UV)IV_MAX;
2970 if ((UV)len_iv > curlen-(UV)pos1_iv)
2973 pos2_iv = pos1_iv+len_iv;
2983 if (!pos2_is_uv && pos2_iv < 0) {
2984 if (!pos1_is_uv && pos1_iv < 0)
2988 else if (!pos1_is_uv && pos1_iv < 0)
2991 if ((UV)pos2_iv < (UV)pos1_iv)
2993 if ((UV)pos2_iv > curlen)
2996 /* pos1_iv and pos2_iv both in 0..curlen, so the cast is safe */
2997 *posp = (STRLEN)( (UV)pos1_iv );
2998 *lenp = (STRLEN)( (UV)pos2_iv - (UV)pos1_iv );
3015 I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
3016 const bool rvalue = (GIMME_V != G_VOID);
3019 const char *repl = NULL;
3021 int num_args = PL_op->op_private & 7;
3022 bool repl_need_utf8_upgrade = FALSE;
3026 if(!(repl_sv = POPs)) num_args--;
3028 if ((len_sv = POPs)) {
3029 len_iv = SvIV(len_sv);
3030 len_is_uv = len_iv ? SvIOK_UV(len_sv) : 1;
3035 pos1_iv = SvIV(pos_sv);
3036 pos1_is_uv = SvIOK_UV(pos_sv);
3038 if (PL_op->op_private & OPpSUBSTR_REPL_FIRST) {
3043 if (lvalue && !repl_sv) {
3045 ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
3046 sv_magic(ret, NULL, PERL_MAGIC_substr, NULL, 0);
3048 LvTARG(ret) = SvREFCNT_inc_simple(sv);
3050 pos1_is_uv || pos1_iv >= 0
3051 ? (STRLEN)(UV)pos1_iv
3052 : (LvFLAGS(ret) |= 1, (STRLEN)(UV)-pos1_iv);
3054 len_is_uv || len_iv > 0
3055 ? (STRLEN)(UV)len_iv
3056 : (LvFLAGS(ret) |= 2, (STRLEN)(UV)-len_iv);
3059 PUSHs(ret); /* avoid SvSETMAGIC here */
3063 repl = SvPV_const(repl_sv, repl_len);
3066 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR),
3067 "Attempt to use reference as lvalue in substr"
3069 tmps = SvPV_force_nomg(sv, curlen);
3070 if (DO_UTF8(repl_sv) && repl_len) {
3072 sv_utf8_upgrade_nomg(sv);
3076 else if (DO_UTF8(sv))
3077 repl_need_utf8_upgrade = TRUE;
3079 else tmps = SvPV_const(sv, curlen);
3081 utf8_curlen = sv_or_pv_len_utf8(sv, tmps, curlen);
3082 if (utf8_curlen == curlen)
3085 curlen = utf8_curlen;
3091 STRLEN pos, len, byte_len, byte_pos;
3093 if (!translate_substr_offsets(
3094 curlen, pos1_iv, pos1_is_uv, len_iv, len_is_uv, &pos, &len
3098 byte_pos = utf8_curlen
3099 ? sv_or_pv_pos_u2b(sv, tmps, pos, &byte_len) : pos;
3104 SvTAINTED_off(TARG); /* decontaminate */
3105 SvUTF8_off(TARG); /* decontaminate */
3106 sv_setpvn(TARG, tmps, byte_len);
3107 #ifdef USE_LOCALE_COLLATE
3108 sv_unmagic(TARG, PERL_MAGIC_collxfrm);
3115 SV* repl_sv_copy = NULL;
3117 if (repl_need_utf8_upgrade) {
3118 repl_sv_copy = newSVsv(repl_sv);
3119 sv_utf8_upgrade(repl_sv_copy);
3120 repl = SvPV_const(repl_sv_copy, repl_len);
3124 sv_insert_flags(sv, byte_pos, byte_len, repl, repl_len, 0);
3125 SvREFCNT_dec(repl_sv_copy);
3137 Perl_croak(aTHX_ "substr outside of string");
3138 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR), "substr outside of string");
3145 const IV size = POPi;
3146 const IV offset = POPi;
3147 SV * const src = POPs;
3148 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
3151 if (lvalue) { /* it's an lvalue! */
3152 ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
3153 sv_magic(ret, NULL, PERL_MAGIC_vec, NULL, 0);
3155 LvTARG(ret) = SvREFCNT_inc_simple(src);
3156 LvTARGOFF(ret) = offset;
3157 LvTARGLEN(ret) = size;
3161 SvTAINTED_off(TARG); /* decontaminate */
3165 sv_setuv(ret, do_vecget(src, offset, size));
3181 const char *little_p;
3184 const bool is_index = PL_op->op_type == OP_INDEX;
3185 const bool threeargs = MAXARG >= 3 && (TOPs || ((void)POPs,0));
3191 big_p = SvPV_const(big, biglen);
3192 little_p = SvPV_const(little, llen);
3194 big_utf8 = DO_UTF8(big);
3195 little_utf8 = DO_UTF8(little);
3196 if (big_utf8 ^ little_utf8) {
3197 /* One needs to be upgraded. */
3198 if (little_utf8 && !PL_encoding) {
3199 /* Well, maybe instead we might be able to downgrade the small
3201 char * const pv = (char*)bytes_from_utf8((U8 *)little_p, &llen,
3204 /* If the large string is ISO-8859-1, and it's not possible to
3205 convert the small string to ISO-8859-1, then there is no
3206 way that it could be found anywhere by index. */
3211 /* At this point, pv is a malloc()ed string. So donate it to temp
3212 to ensure it will get free()d */
3213 little = temp = newSV(0);
3214 sv_usepvn(temp, pv, llen);
3215 little_p = SvPVX(little);
3218 ? newSVpvn(big_p, biglen) : newSVpvn(little_p, llen);
3221 sv_recode_to_utf8(temp, PL_encoding);
3223 sv_utf8_upgrade(temp);
3228 big_p = SvPV_const(big, biglen);
3231 little_p = SvPV_const(little, llen);
3235 if (SvGAMAGIC(big)) {
3236 /* Life just becomes a lot easier if I use a temporary here.
3237 Otherwise I need to avoid calls to sv_pos_u2b(), which (dangerously)
3238 will trigger magic and overloading again, as will fbm_instr()
3240 big = newSVpvn_flags(big_p, biglen,
3241 SVs_TEMP | (big_utf8 ? SVf_UTF8 : 0));
3244 if (SvGAMAGIC(little) || (is_index && !SvOK(little))) {
3245 /* index && SvOK() is a hack. fbm_instr() calls SvPV_const, which will
3246 warn on undef, and we've already triggered a warning with the
3247 SvPV_const some lines above. We can't remove that, as we need to
3248 call some SvPV to trigger overloading early and find out if the
3250 This is all getting to messy. The API isn't quite clean enough,
3251 because data access has side effects.
3253 little = newSVpvn_flags(little_p, llen,
3254 SVs_TEMP | (little_utf8 ? SVf_UTF8 : 0));
3255 little_p = SvPVX(little);
3259 offset = is_index ? 0 : biglen;
3261 if (big_utf8 && offset > 0)
3262 sv_pos_u2b(big, &offset, 0);
3268 else if (offset > (I32)biglen)
3270 if (!(little_p = is_index
3271 ? fbm_instr((unsigned char*)big_p + offset,
3272 (unsigned char*)big_p + biglen, little, 0)
3273 : rninstr(big_p, big_p + offset,
3274 little_p, little_p + llen)))
3277 retval = little_p - big_p;
3278 if (retval > 0 && big_utf8)
3279 sv_pos_b2u(big, &retval);
3289 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
3290 SvTAINTED_off(TARG);
3291 do_sprintf(TARG, SP-MARK, MARK+1);
3292 TAINT_IF(SvTAINTED(TARG));
3304 const U8 *s = (U8*)SvPV_const(argsv, len);
3306 if (PL_encoding && SvPOK(argsv) && !DO_UTF8(argsv)) {
3307 SV * const tmpsv = sv_2mortal(newSVsv(argsv));
3308 s = (U8*)sv_recode_to_utf8(tmpsv, PL_encoding);
3312 XPUSHu(DO_UTF8(argsv) ?
3313 utf8n_to_uvchr(s, UTF8_MAXBYTES, 0, UTF8_ALLOW_ANYUV) :
3327 if (!IN_BYTES /* under bytes, chr(-1) eq chr(0xff), etc. */
3328 && ((SvIOKp(top) && !SvIsUV(top) && SvIV_nomg(top) < 0)
3330 ((SvNOKp(top) || (SvOK(top) && !SvIsUV(top)))
3331 && SvNV_nomg(top) < 0.0))) {
3332 if (ckWARN(WARN_UTF8)) {
3333 if (SvGMAGICAL(top)) {
3334 SV *top2 = sv_newmortal();
3335 sv_setsv_nomg(top2, top);
3338 Perl_warner(aTHX_ packWARN(WARN_UTF8),
3339 "Invalid negative number (%"SVf") in chr", top);
3341 value = UNICODE_REPLACEMENT;
3343 value = SvUV_nomg(top);
3346 SvUPGRADE(TARG,SVt_PV);
3348 if (value > 255 && !IN_BYTES) {
3349 SvGROW(TARG, (STRLEN)UNISKIP(value)+1);
3350 tmps = (char*)uvchr_to_utf8_flags((U8*)SvPVX(TARG), value, 0);
3351 SvCUR_set(TARG, tmps - SvPVX_const(TARG));
3353 (void)SvPOK_only(TARG);
3362 *tmps++ = (char)value;
3364 (void)SvPOK_only(TARG);
3366 if (PL_encoding && !IN_BYTES) {
3367 sv_recode_to_utf8(TARG, PL_encoding);
3369 if (SvCUR(TARG) == 0
3370 || ! is_utf8_string((U8*)tmps, SvCUR(TARG))
3371 || UTF8_IS_REPLACEMENT((U8*) tmps, (U8*) tmps + SvCUR(TARG)))
3376 *tmps++ = (char)value;
3392 const char *tmps = SvPV_const(left, len);
3394 if (DO_UTF8(left)) {
3395 /* If Unicode, try to downgrade.
3396 * If not possible, croak.
3397 * Yes, we made this up. */
3398 SV* const tsv = sv_2mortal(newSVsv(left));
3401 sv_utf8_downgrade(tsv, FALSE);
3402 tmps = SvPV_const(tsv, len);
3404 # ifdef USE_ITHREADS
3406 if (!PL_reentrant_buffer->_crypt_struct_buffer) {
3407 /* This should be threadsafe because in ithreads there is only
3408 * one thread per interpreter. If this would not be true,
3409 * we would need a mutex to protect this malloc. */
3410 PL_reentrant_buffer->_crypt_struct_buffer =
3411 (struct crypt_data *)safemalloc(sizeof(struct crypt_data));
3412 #if defined(__GLIBC__) || defined(__EMX__)
3413 if (PL_reentrant_buffer->_crypt_struct_buffer) {
3414 PL_reentrant_buffer->_crypt_struct_buffer->initialized = 0;
3415 /* work around glibc-2.2.5 bug */
3416 PL_reentrant_buffer->_crypt_struct_buffer->current_saltbits = 0;
3420 # endif /* HAS_CRYPT_R */
3421 # endif /* USE_ITHREADS */
3423 sv_setpv(TARG, fcrypt(tmps, SvPV_nolen_const(right)));
3425 sv_setpv(TARG, PerlProc_crypt(tmps, SvPV_nolen_const(right)));
3431 "The crypt() function is unimplemented due to excessive paranoia.");
3435 /* Generally UTF-8 and UTF-EBCDIC are indistinguishable at this level. So
3436 * most comments below say UTF-8, when in fact they mean UTF-EBCDIC as well */
3440 /* Actually is both lcfirst() and ucfirst(). Only the first character
3441 * changes. This means that possibly we can change in-place, ie., just
3442 * take the source and change that one character and store it back, but not
3443 * if read-only etc, or if the length changes */
3448 STRLEN slen; /* slen is the byte length of the whole SV. */
3451 bool inplace; /* ? Convert first char only, in-place */
3452 bool doing_utf8 = FALSE; /* ? using utf8 */
3453 bool convert_source_to_utf8 = FALSE; /* ? need to convert */
3454 const int op_type = PL_op->op_type;
3457 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
3458 STRLEN ulen; /* ulen is the byte length of the original Unicode character
3459 * stored as UTF-8 at s. */
3460 STRLEN tculen; /* tculen is the byte length of the freshly titlecased (or
3461 * lowercased) character stored in tmpbuf. May be either
3462 * UTF-8 or not, but in either case is the number of bytes */
3463 bool tainted = FALSE;
3467 s = (const U8*)SvPV_nomg_const(source, slen);
3469 if (ckWARN(WARN_UNINITIALIZED))
3470 report_uninit(source);
3475 /* We may be able to get away with changing only the first character, in
3476 * place, but not if read-only, etc. Later we may discover more reasons to
3477 * not convert in-place. */
3478 inplace = SvPADTMP(source) && !SvREADONLY(source) && SvTEMP(source);
3480 /* First calculate what the changed first character should be. This affects
3481 * whether we can just swap it out, leaving the rest of the string unchanged,
3482 * or even if have to convert the dest to UTF-8 when the source isn't */
3484 if (! slen) { /* If empty */
3485 need = 1; /* still need a trailing NUL */
3488 else if (DO_UTF8(source)) { /* Is the source utf8? */
3491 if (op_type == OP_UCFIRST) {
3492 _to_utf8_title_flags(s, tmpbuf, &tculen,
3493 cBOOL(IN_LOCALE_RUNTIME), &tainted);
3496 _to_utf8_lower_flags(s, tmpbuf, &tculen,
3497 cBOOL(IN_LOCALE_RUNTIME), &tainted);
3500 /* we can't do in-place if the length changes. */
3501 if (ulen != tculen) inplace = FALSE;
3502 need = slen + 1 - ulen + tculen;
3504 else { /* Non-zero length, non-UTF-8, Need to consider locale and if
3505 * latin1 is treated as caseless. Note that a locale takes
3507 ulen = 1; /* Original character is 1 byte */
3508 tculen = 1; /* Most characters will require one byte, but this will
3509 * need to be overridden for the tricky ones */
3512 if (op_type == OP_LCFIRST) {
3514 /* lower case the first letter: no trickiness for any character */
3515 *tmpbuf = (IN_LOCALE_RUNTIME) ? toLOWER_LC(*s) :
3516 ((IN_UNI_8_BIT) ? toLOWER_LATIN1(*s) : toLOWER(*s));
3519 else if (IN_LOCALE_RUNTIME) {
3520 *tmpbuf = toUPPER_LC(*s); /* This would be a bug if any locales
3521 * have upper and title case different
3524 else if (! IN_UNI_8_BIT) {
3525 *tmpbuf = toUPPER(*s); /* Returns caseless for non-ascii, or
3526 * on EBCDIC machines whatever the
3527 * native function does */
3529 else { /* is ucfirst non-UTF-8, not in locale, and cased latin1 */
3530 UV title_ord = _to_upper_title_latin1(*s, tmpbuf, &tculen, 's');
3532 assert(tculen == 2);
3534 /* If the result is an upper Latin1-range character, it can
3535 * still be represented in one byte, which is its ordinal */
3536 if (UTF8_IS_DOWNGRADEABLE_START(*tmpbuf)) {
3537 *tmpbuf = (U8) title_ord;
3541 /* Otherwise it became more than one ASCII character (in
3542 * the case of LATIN_SMALL_LETTER_SHARP_S) or changed to
3543 * beyond Latin1, so the number of bytes changed, so can't
3544 * replace just the first character in place. */
3547 /* If the result won't fit in a byte, the entire result
3548 * will have to be in UTF-8. Assume worst case sizing in
3549 * conversion. (all latin1 characters occupy at most two
3551 if (title_ord > 255) {
3553 convert_source_to_utf8 = TRUE;
3554 need = slen * 2 + 1;
3556 /* The (converted) UTF-8 and UTF-EBCDIC lengths of all
3557 * (both) characters whose title case is above 255 is
3561 else { /* LATIN_SMALL_LETTER_SHARP_S expands by 1 byte */
3562 need = slen + 1 + 1;
3566 } /* End of use Unicode (Latin1) semantics */
3567 } /* End of changing the case of the first character */
3569 /* Here, have the first character's changed case stored in tmpbuf. Ready to
3570 * generate the result */
3573 /* We can convert in place. This means we change just the first
3574 * character without disturbing the rest; no need to grow */
3576 s = d = (U8*)SvPV_force_nomg(source, slen);
3582 /* Here, we can't convert in place; we earlier calculated how much
3583 * space we will need, so grow to accommodate that */
3584 SvUPGRADE(dest, SVt_PV);
3585 d = (U8*)SvGROW(dest, need);
3586 (void)SvPOK_only(dest);
3593 if (! convert_source_to_utf8) {
3595 /* Here both source and dest are in UTF-8, but have to create
3596 * the entire output. We initialize the result to be the
3597 * title/lower cased first character, and then append the rest
3599 sv_setpvn(dest, (char*)tmpbuf, tculen);
3601 sv_catpvn(dest, (char*)(s + ulen), slen - ulen);
3605 const U8 *const send = s + slen;
3607 /* Here the dest needs to be in UTF-8, but the source isn't,
3608 * except we earlier UTF-8'd the first character of the source
3609 * into tmpbuf. First put that into dest, and then append the
3610 * rest of the source, converting it to UTF-8 as we go. */
3612 /* Assert tculen is 2 here because the only two characters that
3613 * get to this part of the code have 2-byte UTF-8 equivalents */
3615 *d++ = *(tmpbuf + 1);
3616 s++; /* We have just processed the 1st char */
3618 for (; s < send; s++) {
3619 d = uvchr_to_utf8(d, *s);
3622 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3626 else { /* in-place UTF-8. Just overwrite the first character */
3627 Copy(tmpbuf, d, tculen, U8);
3628 SvCUR_set(dest, need - 1);
3636 else { /* Neither source nor dest are in or need to be UTF-8 */
3638 if (IN_LOCALE_RUNTIME) {
3642 if (inplace) { /* in-place, only need to change the 1st char */
3645 else { /* Not in-place */
3647 /* Copy the case-changed character(s) from tmpbuf */
3648 Copy(tmpbuf, d, tculen, U8);
3649 d += tculen - 1; /* Code below expects d to point to final
3650 * character stored */
3653 else { /* empty source */
3654 /* See bug #39028: Don't taint if empty */
3658 /* In a "use bytes" we don't treat the source as UTF-8, but, still want
3659 * the destination to retain that flag */
3663 if (!inplace) { /* Finish the rest of the string, unchanged */
3664 /* This will copy the trailing NUL */
3665 Copy(s + 1, d + 1, slen, U8);
3666 SvCUR_set(dest, need - 1);
3669 if (dest != source && SvTAINTED(source))
3675 /* There's so much setup/teardown code common between uc and lc, I wonder if
3676 it would be worth merging the two, and just having a switch outside each
3677 of the three tight loops. There is less and less commonality though */
3691 if (SvPADTMP(source) && !SvREADONLY(source) && !SvAMAGIC(source)
3692 && SvTEMP(source) && !DO_UTF8(source)
3693 && (IN_LOCALE_RUNTIME || ! IN_UNI_8_BIT)) {
3695 /* We can convert in place. The reason we can't if in UNI_8_BIT is to
3696 * make the loop tight, so we overwrite the source with the dest before
3697 * looking at it, and we need to look at the original source
3698 * afterwards. There would also need to be code added to handle
3699 * switching to not in-place in midstream if we run into characters
3700 * that change the length.
3703 s = d = (U8*)SvPV_force_nomg(source, len);
3710 /* The old implementation would copy source into TARG at this point.
3711 This had the side effect that if source was undef, TARG was now
3712 an undefined SV with PADTMP set, and they don't warn inside
3713 sv_2pv_flags(). However, we're now getting the PV direct from
3714 source, which doesn't have PADTMP set, so it would warn. Hence the
3718 s = (const U8*)SvPV_nomg_const(source, len);
3720 if (ckWARN(WARN_UNINITIALIZED))
3721 report_uninit(source);
3727 SvUPGRADE(dest, SVt_PV);
3728 d = (U8*)SvGROW(dest, min);
3729 (void)SvPOK_only(dest);
3734 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
3735 to check DO_UTF8 again here. */
3737 if (DO_UTF8(source)) {
3738 const U8 *const send = s + len;
3739 U8 tmpbuf[UTF8_MAXBYTES+1];
3740 bool tainted = FALSE;
3742 /* All occurrences of these are to be moved to follow any other marks.
3743 * This is context-dependent. We may not be passed enough context to
3744 * move the iota subscript beyond all of them, but we do the best we can
3745 * with what we're given. The result is always better than if we
3746 * hadn't done this. And, the problem would only arise if we are
3747 * passed a character without all its combining marks, which would be
3748 * the caller's mistake. The information this is based on comes from a
3749 * comment in Unicode SpecialCasing.txt, (and the Standard's text
3750 * itself) and so can't be checked properly to see if it ever gets
3751 * revised. But the likelihood of it changing is remote */
3752 bool in_iota_subscript = FALSE;
3758 if (in_iota_subscript && ! _is_utf8_mark(s)) {
3760 /* A non-mark. Time to output the iota subscript */
3761 Copy(GREEK_CAPITAL_LETTER_IOTA_UTF8, d, capital_iota_len, U8);
3762 d += capital_iota_len;
3763 in_iota_subscript = FALSE;
3766 /* Then handle the current character. Get the changed case value
3767 * and copy it to the output buffer */
3770 uv = _to_utf8_upper_flags(s, tmpbuf, &ulen,
3771 cBOOL(IN_LOCALE_RUNTIME), &tainted);
3772 #define GREEK_CAPITAL_LETTER_IOTA 0x0399
3773 #define COMBINING_GREEK_YPOGEGRAMMENI 0x0345
3774 if (uv == GREEK_CAPITAL_LETTER_IOTA
3775 && utf8_to_uvchr_buf(s, send, 0) == COMBINING_GREEK_YPOGEGRAMMENI)
3777 in_iota_subscript = TRUE;
3780 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
3781 /* If the eventually required minimum size outgrows the
3782 * available space, we need to grow. */
3783 const UV o = d - (U8*)SvPVX_const(dest);
3785 /* If someone uppercases one million U+03B0s we SvGROW()
3786 * one million times. Or we could try guessing how much to
3787 * allocate without allocating too much. Such is life.
3788 * See corresponding comment in lc code for another option
3791 d = (U8*)SvPVX(dest) + o;
3793 Copy(tmpbuf, d, ulen, U8);
3798 if (in_iota_subscript) {
3799 Copy(GREEK_CAPITAL_LETTER_IOTA_UTF8, d, capital_iota_len, U8);
3800 d += capital_iota_len;
3805 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3811 else { /* Not UTF-8 */
3813 const U8 *const send = s + len;
3815 /* Use locale casing if in locale; regular style if not treating
3816 * latin1 as having case; otherwise the latin1 casing. Do the
3817 * whole thing in a tight loop, for speed, */
3818 if (IN_LOCALE_RUNTIME) {
3821 for (; s < send; d++, s++)
3822 *d = toUPPER_LC(*s);
3824 else if (! IN_UNI_8_BIT) {
3825 for (; s < send; d++, s++) {
3830 for (; s < send; d++, s++) {
3831 *d = toUPPER_LATIN1_MOD(*s);
3832 if (LIKELY(*d != LATIN_SMALL_LETTER_Y_WITH_DIAERESIS)) {
3836 /* The mainstream case is the tight loop above. To avoid
3837 * extra tests in that, all three characters that require
3838 * special handling are mapped by the MOD to the one tested
3840 * Use the source to distinguish between the three cases */
3842 if (*s == LATIN_SMALL_LETTER_SHARP_S) {
3844 /* uc() of this requires 2 characters, but they are
3845 * ASCII. If not enough room, grow the string */
3846 if (SvLEN(dest) < ++min) {
3847 const UV o = d - (U8*)SvPVX_const(dest);
3849 d = (U8*)SvPVX(dest) + o;
3851 *d++ = 'S'; *d = 'S'; /* upper case is 'SS' */
3852 continue; /* Back to the tight loop; still in ASCII */
3855 /* The other two special handling characters have their
3856 * upper cases outside the latin1 range, hence need to be
3857 * in UTF-8, so the whole result needs to be in UTF-8. So,
3858 * here we are somewhere in the middle of processing a
3859 * non-UTF-8 string, and realize that we will have to convert
3860 * the whole thing to UTF-8. What to do? There are
3861 * several possibilities. The simplest to code is to
3862 * convert what we have so far, set a flag, and continue on
3863 * in the loop. The flag would be tested each time through
3864 * the loop, and if set, the next character would be
3865 * converted to UTF-8 and stored. But, I (khw) didn't want
3866 * to slow down the mainstream case at all for this fairly
3867 * rare case, so I didn't want to add a test that didn't
3868 * absolutely have to be there in the loop, besides the
3869 * possibility that it would get too complicated for
3870 * optimizers to deal with. Another possibility is to just
3871 * give up, convert the source to UTF-8, and restart the
3872 * function that way. Another possibility is to convert
3873 * both what has already been processed and what is yet to
3874 * come separately to UTF-8, then jump into the loop that
3875 * handles UTF-8. But the most efficient time-wise of the
3876 * ones I could think of is what follows, and turned out to
3877 * not require much extra code. */
3879 /* Convert what we have so far into UTF-8, telling the
3880 * function that we know it should be converted, and to
3881 * allow extra space for what we haven't processed yet.
3882 * Assume the worst case space requirements for converting
3883 * what we haven't processed so far: that it will require
3884 * two bytes for each remaining source character, plus the
3885 * NUL at the end. This may cause the string pointer to
3886 * move, so re-find it. */
3888 len = d - (U8*)SvPVX_const(dest);
3889 SvCUR_set(dest, len);
3890 len = sv_utf8_upgrade_flags_grow(dest,
3891 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
3893 d = (U8*)SvPVX(dest) + len;
3895 /* Now process the remainder of the source, converting to
3896 * upper and UTF-8. If a resulting byte is invariant in
3897 * UTF-8, output it as-is, otherwise convert to UTF-8 and
3898 * append it to the output. */
3899 for (; s < send; s++) {
3900 (void) _to_upper_title_latin1(*s, d, &len, 'S');
3904 /* Here have processed the whole source; no need to continue
3905 * with the outer loop. Each character has been converted
3906 * to upper case and converted to UTF-8 */
3909 } /* End of processing all latin1-style chars */
3910 } /* End of processing all chars */
3911 } /* End of source is not empty */
3913 if (source != dest) {
3914 *d = '\0'; /* Here d points to 1 after last char, add NUL */
3915 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3917 } /* End of isn't utf8 */
3918 if (dest != source && SvTAINTED(source))
3937 if (SvPADTMP(source) && !SvREADONLY(source) && !SvAMAGIC(source)
3938 && SvTEMP(source) && !DO_UTF8(source)) {
3940 /* We can convert in place, as lowercasing anything in the latin1 range
3941 * (or else DO_UTF8 would have been on) doesn't lengthen it */
3943 s = d = (U8*)SvPV_force_nomg(source, len);
3950 /* The old implementation would copy source into TARG at this point.
3951 This had the side effect that if source was undef, TARG was now
3952 an undefined SV with PADTMP set, and they don't warn inside
3953 sv_2pv_flags(). However, we're now getting the PV direct from
3954 source, which doesn't have PADTMP set, so it would warn. Hence the
3958 s = (const U8*)SvPV_nomg_const(source, len);
3960 if (ckWARN(WARN_UNINITIALIZED))
3961 report_uninit(source);
3967 SvUPGRADE(dest, SVt_PV);
3968 d = (U8*)SvGROW(dest, min);
3969 (void)SvPOK_only(dest);
3974 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
3975 to check DO_UTF8 again here. */
3977 if (DO_UTF8(source)) {
3978 const U8 *const send = s + len;
3979 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
3980 bool tainted = FALSE;
3983 const STRLEN u = UTF8SKIP(s);
3986 _to_utf8_lower_flags(s, tmpbuf, &ulen,
3987 cBOOL(IN_LOCALE_RUNTIME), &tainted);
3989 /* Here is where we would do context-sensitive actions. See the
3990 * commit message for this comment for why there isn't any */
3992 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
3994 /* If the eventually required minimum size outgrows the
3995 * available space, we need to grow. */
3996 const UV o = d - (U8*)SvPVX_const(dest);
3998 /* If someone lowercases one million U+0130s we SvGROW() one
3999 * million times. Or we could try guessing how much to
4000 * allocate without allocating too much. Such is life.
4001 * Another option would be to grow an extra byte or two more
4002 * each time we need to grow, which would cut down the million
4003 * to 500K, with little waste */
4005 d = (U8*)SvPVX(dest) + o;
4008 /* Copy the newly lowercased letter to the output buffer we're
4010 Copy(tmpbuf, d, ulen, U8);
4013 } /* End of looping through the source string */
4016 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4021 } else { /* Not utf8 */
4023 const U8 *const send = s + len;
4025 /* Use locale casing if in locale; regular style if not treating
4026 * latin1 as having case; otherwise the latin1 casing. Do the
4027 * whole thing in a tight loop, for speed, */
4028 if (IN_LOCALE_RUNTIME) {
4031 for (; s < send; d++, s++)
4032 *d = toLOWER_LC(*s);
4034 else if (! IN_UNI_8_BIT) {
4035 for (; s < send; d++, s++) {
4040 for (; s < send; d++, s++) {
4041 *d = toLOWER_LATIN1(*s);
4045 if (source != dest) {
4047 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4050 if (dest != source && SvTAINTED(source))
4059 SV * const sv = TOPs;
4061 const char *s = SvPV_const(sv,len);
4063 SvUTF8_off(TARG); /* decontaminate */
4066 SvUPGRADE(TARG, SVt_PV);
4067 SvGROW(TARG, (len * 2) + 1);
4071 STRLEN ulen = UTF8SKIP(s);
4072 bool to_quote = FALSE;
4074 if (UTF8_IS_INVARIANT(*s)) {
4075 if (_isQUOTEMETA(*s)) {
4079 else if (UTF8_IS_DOWNGRADEABLE_START(*s)) {
4081 /* In locale, we quote all non-ASCII Latin1 chars.
4082 * Otherwise use the quoting rules */
4083 if (IN_LOCALE_RUNTIME
4084 || _isQUOTEMETA(TWO_BYTE_UTF8_TO_UNI(*s, *(s + 1))))
4089 else if (is_QUOTEMETA_high(s)) {
4104 else if (IN_UNI_8_BIT) {
4106 if (_isQUOTEMETA(*s))
4112 /* For non UNI_8_BIT (and hence in locale) just quote all \W
4113 * including everything above ASCII */
4115 if (!isWORDCHAR_A(*s))
4121 SvCUR_set(TARG, d - SvPVX_const(TARG));
4122 (void)SvPOK_only_UTF8(TARG);
4125 sv_setpvn(TARG, s, len);
4142 U8 tmpbuf[UTF8_MAXBYTES * UTF8_MAX_FOLD_CHAR_EXPAND + 1];
4143 const bool full_folding = TRUE;
4144 const U8 flags = ( full_folding ? FOLD_FLAGS_FULL : 0 )
4145 | ( IN_LOCALE_RUNTIME ? FOLD_FLAGS_LOCALE : 0 );
4147 /* This is a facsimile of pp_lc, but with a thousand bugs thanks to me.
4148 * You are welcome(?) -Hugmeir
4156 s = (const U8*)SvPV_nomg_const(source, len);
4158 if (ckWARN(WARN_UNINITIALIZED))
4159 report_uninit(source);
4166 SvUPGRADE(dest, SVt_PV);
4167 d = (U8*)SvGROW(dest, min);
4168 (void)SvPOK_only(dest);
4173 if (DO_UTF8(source)) { /* UTF-8 flagged string. */
4174 bool tainted = FALSE;
4176 const STRLEN u = UTF8SKIP(s);
4179 _to_utf8_fold_flags(s, tmpbuf, &ulen, flags, &tainted);
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);
4196 } /* Unflagged string */
4198 if ( IN_LOCALE_RUNTIME ) { /* Under locale */
4201 for (; s < send; d++, s++)
4204 else if ( !IN_UNI_8_BIT ) { /* Under nothing, or bytes */
4205 for (; s < send; d++, s++)
4209 /* For ASCII and the Latin-1 range, there's only two troublesome
4210 * folds, \x{DF} (\N{LATIN SMALL LETTER SHARP S}), which under full
4211 * casefolding becomes 'ss'; and \x{B5} (\N{MICRO SIGN}), which
4212 * under any fold becomes \x{3BC} (\N{GREEK SMALL LETTER MU}) --
4213 * For the rest, the casefold is their lowercase. */
4214 for (; s < send; d++, s++) {
4215 if (*s == MICRO_SIGN) {
4216 /* \N{MICRO SIGN}'s casefold is \N{GREEK SMALL LETTER MU},
4217 * which is outside of the latin-1 range. There's a couple
4218 * of ways to deal with this -- khw discusses them in
4219 * pp_lc/uc, so go there :) What we do here is upgrade what
4220 * we had already casefolded, then enter an inner loop that
4221 * appends the rest of the characters as UTF-8. */
4222 len = d - (U8*)SvPVX_const(dest);
4223 SvCUR_set(dest, len);
4224 len = sv_utf8_upgrade_flags_grow(dest,
4225 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
4226 /* The max expansion for latin1
4227 * chars is 1 byte becomes 2 */
4229 d = (U8*)SvPVX(dest) + len;
4231 Copy(GREEK_SMALL_LETTER_MU_UTF8, d, small_mu_len, U8);
4234 for (; s < send; s++) {
4236 UV fc = _to_uni_fold_flags(*s, tmpbuf, &ulen, flags);
4237 if UNI_IS_INVARIANT(fc) {
4239 && *s == LATIN_SMALL_LETTER_SHARP_S)
4248 Copy(tmpbuf, d, ulen, U8);
4254 else if (full_folding && *s == LATIN_SMALL_LETTER_SHARP_S) {
4255 /* Under full casefolding, LATIN SMALL LETTER SHARP S
4256 * becomes "ss", which may require growing the SV. */
4257 if (SvLEN(dest) < ++min) {
4258 const UV o = d - (U8*)SvPVX_const(dest);
4260 d = (U8*)SvPVX(dest) + o;
4265 else { /* If it's not one of those two, the fold is their lower
4267 *d = toLOWER_LATIN1(*s);
4273 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4275 if (SvTAINTED(source))
4285 dVAR; dSP; dMARK; dORIGMARK;
4286 AV *const av = MUTABLE_AV(POPs);
4287 const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
4289 if (SvTYPE(av) == SVt_PVAV) {
4290 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
4291 bool can_preserve = FALSE;
4297 can_preserve = SvCANEXISTDELETE(av);
4300 if (lval && localizing) {
4303 for (svp = MARK + 1; svp <= SP; svp++) {
4304 const I32 elem = SvIV(*svp);
4308 if (max > AvMAX(av))
4312 while (++MARK <= SP) {
4314 I32 elem = SvIV(*MARK);
4315 bool preeminent = TRUE;
4317 if (localizing && can_preserve) {
4318 /* If we can determine whether the element exist,
4319 * Try to preserve the existenceness of a tied array
4320 * element by using EXISTS and DELETE if possible.
4321 * Fallback to FETCH and STORE otherwise. */
4322 preeminent = av_exists(av, elem);
4325 svp = av_fetch(av, elem, lval);
4327 if (!svp || *svp == &PL_sv_undef)
4328 DIE(aTHX_ PL_no_aelem, elem);
4331 save_aelem(av, elem, svp);
4333 SAVEADELETE(av, elem);
4336 *MARK = svp ? *svp : &PL_sv_undef;
4339 if (GIMME != G_ARRAY) {
4341 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
4347 /* Smart dereferencing for keys, values and each */
4359 (SvTYPE(sv) != SVt_PVHV && SvTYPE(sv) != SVt_PVAV)
4364 "Type of argument to %s must be unblessed hashref or arrayref",
4365 PL_op_desc[PL_op->op_type] );
4368 if (PL_op->op_flags & OPf_SPECIAL && SvTYPE(sv) == SVt_PVAV)
4370 "Can't modify %s in %s",
4371 PL_op_desc[PL_op->op_type], PL_op_desc[PL_op->op_next->op_type]
4374 /* Delegate to correct function for op type */
4376 if (PL_op->op_type == OP_RKEYS || PL_op->op_type == OP_RVALUES) {
4377 return (SvTYPE(sv) == SVt_PVHV) ? Perl_do_kv(aTHX) : Perl_pp_akeys(aTHX);
4380 return (SvTYPE(sv) == SVt_PVHV)
4381 ? Perl_pp_each(aTHX)
4382 : Perl_pp_aeach(aTHX);
4390 AV *array = MUTABLE_AV(POPs);
4391 const I32 gimme = GIMME_V;
4392 IV *iterp = Perl_av_iter_p(aTHX_ array);
4393 const IV current = (*iterp)++;
4395 if (current > av_len(array)) {
4397 if (gimme == G_SCALAR)
4405 if (gimme == G_ARRAY) {
4406 SV **const element = av_fetch(array, current, 0);
4407 PUSHs(element ? *element : &PL_sv_undef);
4416 AV *array = MUTABLE_AV(POPs);
4417 const I32 gimme = GIMME_V;
4419 *Perl_av_iter_p(aTHX_ array) = 0;
4421 if (gimme == G_SCALAR) {
4423 PUSHi(av_len(array) + 1);
4425 else if (gimme == G_ARRAY) {
4426 IV n = Perl_av_len(aTHX_ array);
4431 if (PL_op->op_type == OP_AKEYS || PL_op->op_type == OP_RKEYS) {
4432 for (i = 0; i <= n; i++) {
4437 for (i = 0; i <= n; i++) {
4438 SV *const *const elem = Perl_av_fetch(aTHX_ array, i, 0);
4439 PUSHs(elem ? *elem : &PL_sv_undef);
4446 /* Associative arrays. */
4452 HV * hash = MUTABLE_HV(POPs);
4454 const I32 gimme = GIMME_V;
4457 /* might clobber stack_sp */
4458 entry = hv_iternext(hash);
4463 SV* const sv = hv_iterkeysv(entry);
4464 PUSHs(sv); /* won't clobber stack_sp */
4465 if (gimme == G_ARRAY) {
4468 /* might clobber stack_sp */
4469 val = hv_iterval(hash, entry);
4474 else if (gimme == G_SCALAR)
4481 S_do_delete_local(pTHX)
4485 const I32 gimme = GIMME_V;
4488 const bool sliced = !!(PL_op->op_private & OPpSLICE);
4489 SV *unsliced_keysv = sliced ? NULL : POPs;
4490 SV * const osv = POPs;
4491 SV **mark = sliced ? PL_stack_base + POPMARK : &unsliced_keysv-1;
4493 const bool tied = SvRMAGICAL(osv)
4494 && mg_find((const SV *)osv, PERL_MAGIC_tied);
4495 const bool can_preserve = SvCANEXISTDELETE(osv);
4496 const U32 type = SvTYPE(osv);
4497 SV ** const end = sliced ? SP : &unsliced_keysv;
4499 if (type == SVt_PVHV) { /* hash element */
4500 HV * const hv = MUTABLE_HV(osv);
4501 while (++MARK <= end) {
4502 SV * const keysv = *MARK;
4504 bool preeminent = TRUE;
4506 preeminent = hv_exists_ent(hv, keysv, 0);
4508 HE *he = hv_fetch_ent(hv, keysv, 1, 0);
4515 sv = hv_delete_ent(hv, keysv, 0, 0);
4517 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4520 if (!sv) DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
4521 save_helem_flags(hv, keysv, &sv, SAVEf_KEEPOLDELEM);
4523 *MARK = sv_mortalcopy(sv);
4529 SAVEHDELETE(hv, keysv);
4530 *MARK = &PL_sv_undef;
4534 else if (type == SVt_PVAV) { /* array element */
4535 if (PL_op->op_flags & OPf_SPECIAL) {
4536 AV * const av = MUTABLE_AV(osv);
4537 while (++MARK <= end) {
4538 I32 idx = SvIV(*MARK);
4540 bool preeminent = TRUE;
4542 preeminent = av_exists(av, idx);
4544 SV **svp = av_fetch(av, idx, 1);
4551 sv = av_delete(av, idx, 0);
4553 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4556 save_aelem_flags(av, idx, &sv, SAVEf_KEEPOLDELEM);
4558 *MARK = sv_mortalcopy(sv);
4564 SAVEADELETE(av, idx);
4565 *MARK = &PL_sv_undef;
4570 DIE(aTHX_ "panic: avhv_delete no longer supported");
4573 DIE(aTHX_ "Not a HASH reference");
4575 if (gimme == G_VOID)
4577 else if (gimme == G_SCALAR) {
4582 *++MARK = &PL_sv_undef;
4586 else if (gimme != G_VOID)
4587 PUSHs(unsliced_keysv);
4599 if (PL_op->op_private & OPpLVAL_INTRO)
4600 return do_delete_local();
4603 discard = (gimme == G_VOID) ? G_DISCARD : 0;
4605 if (PL_op->op_private & OPpSLICE) {
4607 HV * const hv = MUTABLE_HV(POPs);
4608 const U32 hvtype = SvTYPE(hv);
4609 if (hvtype == SVt_PVHV) { /* hash element */
4610 while (++MARK <= SP) {
4611 SV * const sv = hv_delete_ent(hv, *MARK, discard, 0);
4612 *MARK = sv ? sv : &PL_sv_undef;
4615 else if (hvtype == SVt_PVAV) { /* array element */
4616 if (PL_op->op_flags & OPf_SPECIAL) {
4617 while (++MARK <= SP) {
4618 SV * const sv = av_delete(MUTABLE_AV(hv), SvIV(*MARK), discard);
4619 *MARK = sv ? sv : &PL_sv_undef;
4624 DIE(aTHX_ "Not a HASH reference");
4627 else if (gimme == G_SCALAR) {
4632 *++MARK = &PL_sv_undef;