3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4 * 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
12 * 'It's a big house this, and very peculiar. Always a bit more
13 * to discover, and no knowing what you'll find round a corner.
14 * And Elves, sir!' --Samwise Gamgee
16 * [p.225 of _The Lord of the Rings_, II/i: "Many Meetings"]
19 /* This file contains general pp ("push/pop") functions that execute the
20 * opcodes that make up a perl program. A typical pp function expects to
21 * find its arguments on the stack, and usually pushes its results onto
22 * the stack, hence the 'pp' terminology. Each OP structure contains
23 * a pointer to the relevant pp_foo() function.
32 #include "regcharclass.h"
34 /* XXX I can't imagine anyone who doesn't have this actually _needs_
35 it, since pid_t is an integral type.
38 #ifdef NEED_GETPID_PROTO
39 extern Pid_t getpid (void);
43 * Some BSDs and Cygwin default to POSIX math instead of IEEE.
44 * This switches them over to IEEE.
46 #if defined(LIBM_LIB_VERSION)
47 _LIB_VERSION_TYPE _LIB_VERSION = _IEEE_;
50 /* variations on pp_null */
56 if (GIMME_V == G_SCALAR)
67 assert(SvTYPE(TARG) == SVt_PVAV);
68 if (PL_op->op_private & OPpLVAL_INTRO)
69 if (!(PL_op->op_private & OPpPAD_STATE))
70 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
72 if (PL_op->op_flags & OPf_REF) {
75 } else if (PL_op->op_private & OPpMAYBE_LVSUB) {
76 const I32 flags = is_lvalue_sub();
77 if (flags && !(flags & OPpENTERSUB_INARGS)) {
78 if (GIMME == G_SCALAR)
79 /* diag_listed_as: Can't return %s to lvalue scalar context */
80 Perl_croak(aTHX_ "Can't return array to lvalue scalar context");
86 if (gimme == G_ARRAY) {
87 const I32 maxarg = AvFILL(MUTABLE_AV(TARG)) + 1;
89 if (SvMAGICAL(TARG)) {
91 for (i=0; i < (U32)maxarg; i++) {
92 SV * const * const svp = av_fetch(MUTABLE_AV(TARG), i, FALSE);
93 SP[i+1] = (svp) ? *svp : &PL_sv_undef;
97 Copy(AvARRAY((const AV *)TARG), SP+1, maxarg, SV*);
101 else if (gimme == G_SCALAR) {
102 SV* const sv = sv_newmortal();
103 const I32 maxarg = AvFILL(MUTABLE_AV(TARG)) + 1;
104 sv_setiv(sv, maxarg);
115 assert(SvTYPE(TARG) == SVt_PVHV);
117 if (PL_op->op_private & OPpLVAL_INTRO)
118 if (!(PL_op->op_private & OPpPAD_STATE))
119 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
120 if (PL_op->op_flags & OPf_REF)
122 else if (PL_op->op_private & OPpMAYBE_LVSUB) {
123 const I32 flags = is_lvalue_sub();
124 if (flags && !(flags & OPpENTERSUB_INARGS)) {
125 if (GIMME == G_SCALAR)
126 /* diag_listed_as: Can't return %s to lvalue scalar context */
127 Perl_croak(aTHX_ "Can't return hash to lvalue scalar context");
132 if (gimme == G_ARRAY) {
133 RETURNOP(Perl_do_kv(aTHX));
135 else if ((PL_op->op_private & OPpTRUEBOOL
136 || ( PL_op->op_private & OPpMAYBE_TRUEBOOL
137 && block_gimme() == G_VOID ))
138 && (!SvRMAGICAL(TARG) || !mg_find(TARG, PERL_MAGIC_tied)))
139 SETs(HvUSEDKEYS(TARG) ? &PL_sv_yes : sv_2mortal(newSViv(0)));
140 else if (gimme == G_SCALAR) {
141 SV* const sv = Perl_hv_scalar(aTHX_ MUTABLE_HV(TARG));
150 assert(SvTYPE(TARG) == SVt_PVCV);
158 SvPADSTALE_off(TARG);
166 mg_find(PadlistNAMESARRAY(CvPADLIST(find_runcv(NULL)))[ARGTARG],
168 assert(SvTYPE(TARG) == SVt_PVCV);
171 if (CvISXSUB(mg->mg_obj)) { /* constant */
172 /* XXX Should we clone it here? */
173 /* If this changes to use SAVECLEARSV, we can move the SAVECLEARSV
174 to introcv and remove the SvPADSTALE_off. */
175 SAVEPADSVANDMORTALIZE(ARGTARG);
176 PAD_SVl(ARGTARG) = mg->mg_obj;
179 if (CvROOT(mg->mg_obj)) {
180 assert(CvCLONE(mg->mg_obj));
181 assert(!CvCLONED(mg->mg_obj));
183 cv_clone_into((CV *)mg->mg_obj,(CV *)TARG);
184 SAVECLEARSV(PAD_SVl(ARGTARG));
191 static const char S_no_symref_sv[] =
192 "Can't use string (\"%" SVf32 "\"%s) as %s ref while \"strict refs\" in use";
194 /* In some cases this function inspects PL_op. If this function is called
195 for new op types, more bool parameters may need to be added in place of
198 When noinit is true, the absence of a gv will cause a retval of undef.
199 This is unrelated to the cv-to-gv assignment case.
203 S_rv2gv(pTHX_ SV *sv, const bool vivify_sv, const bool strict,
207 if (!isGV(sv) || SvFAKE(sv)) SvGETMAGIC(sv);
210 sv = amagic_deref_call(sv, to_gv_amg);
214 if (SvTYPE(sv) == SVt_PVIO) {
215 GV * const gv = MUTABLE_GV(sv_newmortal());
216 gv_init(gv, 0, "__ANONIO__", 10, 0);
217 GvIOp(gv) = MUTABLE_IO(sv);
218 SvREFCNT_inc_void_NN(sv);
221 else if (!isGV_with_GP(sv))
222 return (SV *)Perl_die(aTHX_ "Not a GLOB reference");
225 if (!isGV_with_GP(sv)) {
227 /* If this is a 'my' scalar and flag is set then vivify
230 if (vivify_sv && sv != &PL_sv_undef) {
233 Perl_croak_no_modify(aTHX);
234 if (cUNOP->op_targ) {
235 SV * const namesv = PAD_SV(cUNOP->op_targ);
236 gv = MUTABLE_GV(newSV(0));
237 gv_init_sv(gv, CopSTASH(PL_curcop), namesv, 0);
240 const char * const name = CopSTASHPV(PL_curcop);
241 gv = newGVgen_flags(name,
242 HvNAMEUTF8(CopSTASH(PL_curcop)) ? SVf_UTF8 : 0 );
244 prepare_SV_for_RV(sv);
245 SvRV_set(sv, MUTABLE_SV(gv));
250 if (PL_op->op_flags & OPf_REF || strict)
251 return (SV *)Perl_die(aTHX_ PL_no_usym, "a symbol");
252 if (ckWARN(WARN_UNINITIALIZED))
258 if (!(sv = MUTABLE_SV(gv_fetchsv_nomg(
259 sv, GV_ADDMG, SVt_PVGV
269 (SvPOKp(sv) && SvCUR(sv)>32 ? "..." : ""),
272 if ((PL_op->op_private & (OPpLVAL_INTRO|OPpDONT_INIT_GV))
273 == OPpDONT_INIT_GV) {
274 /* We are the target of a coderef assignment. Return
275 the scalar unchanged, and let pp_sasssign deal with
279 sv = MUTABLE_SV(gv_fetchsv_nomg(sv, GV_ADD, SVt_PVGV));
281 /* FAKE globs in the symbol table cause weird bugs (#77810) */
285 if (SvFAKE(sv) && !(PL_op->op_private & OPpALLOW_FAKE)) {
286 SV *newsv = sv_newmortal();
287 sv_setsv_flags(newsv, sv, 0);
299 sv, PL_op->op_private & OPpDEREF,
300 PL_op->op_private & HINT_STRICT_REFS,
301 ((PL_op->op_flags & OPf_SPECIAL) && !(PL_op->op_flags & OPf_MOD))
302 || PL_op->op_type == OP_READLINE
304 if (PL_op->op_private & OPpLVAL_INTRO)
305 save_gp(MUTABLE_GV(sv), !(PL_op->op_flags & OPf_SPECIAL));
310 /* Helper function for pp_rv2sv and pp_rv2av */
312 Perl_softref2xv(pTHX_ SV *const sv, const char *const what,
313 const svtype type, SV ***spp)
318 PERL_ARGS_ASSERT_SOFTREF2XV;
320 if (PL_op->op_private & HINT_STRICT_REFS) {
322 Perl_die(aTHX_ S_no_symref_sv, sv,
323 (SvPOKp(sv) && SvCUR(sv)>32 ? "..." : ""), what);
325 Perl_die(aTHX_ PL_no_usym, what);
329 PL_op->op_flags & OPf_REF
331 Perl_die(aTHX_ PL_no_usym, what);
332 if (ckWARN(WARN_UNINITIALIZED))
334 if (type != SVt_PV && GIMME_V == G_ARRAY) {
338 **spp = &PL_sv_undef;
341 if ((PL_op->op_flags & OPf_SPECIAL) &&
342 !(PL_op->op_flags & OPf_MOD))
344 if (!(gv = gv_fetchsv_nomg(sv, GV_ADDMG, type)))
346 **spp = &PL_sv_undef;
351 gv = gv_fetchsv_nomg(sv, GV_ADD, type);
364 sv = amagic_deref_call(sv, to_sv_amg);
368 switch (SvTYPE(sv)) {
374 DIE(aTHX_ "Not a SCALAR reference");
381 if (!isGV_with_GP(gv)) {
382 gv = Perl_softref2xv(aTHX_ sv, "a SCALAR", SVt_PV, &sp);
388 if (PL_op->op_flags & OPf_MOD) {
389 if (PL_op->op_private & OPpLVAL_INTRO) {
390 if (cUNOP->op_first->op_type == OP_NULL)
391 sv = save_scalar(MUTABLE_GV(TOPs));
393 sv = save_scalar(gv);
395 Perl_croak(aTHX_ "%s", PL_no_localize_ref);
397 else if (PL_op->op_private & OPpDEREF)
398 sv = vivify_ref(sv, PL_op->op_private & OPpDEREF);
407 AV * const av = MUTABLE_AV(TOPs);
408 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
410 SV ** const sv = Perl_av_arylen_p(aTHX_ MUTABLE_AV(av));
412 *sv = newSV_type(SVt_PVMG);
413 sv_magic(*sv, MUTABLE_SV(av), PERL_MAGIC_arylen, NULL, 0);
417 SETs(sv_2mortal(newSViv(AvFILL(MUTABLE_AV(av)))));
426 if (PL_op->op_flags & OPf_MOD || LVRET) {
427 SV * const ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
428 sv_magic(ret, NULL, PERL_MAGIC_pos, NULL, 0);
430 LvTARG(ret) = SvREFCNT_inc_simple(sv);
431 PUSHs(ret); /* no SvSETMAGIC */
435 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
436 const MAGIC * const mg = mg_find(sv, PERL_MAGIC_regex_global);
437 if (mg && mg->mg_len >= 0) {
455 const I32 flags = (PL_op->op_flags & OPf_SPECIAL)
457 : ((PL_op->op_private & (OPpLVAL_INTRO|OPpMAY_RETURN_CONSTANT)) == OPpMAY_RETURN_CONSTANT)
460 /* We usually try to add a non-existent subroutine in case of AUTOLOAD. */
461 /* (But not in defined().) */
463 CV *cv = sv_2cv(TOPs, &stash_unused, &gv, flags);
465 else if ((flags == (GV_ADD|GV_NOEXPAND)) && gv && SvROK(gv)) {
469 cv = MUTABLE_CV(&PL_sv_undef);
470 SETs(MUTABLE_SV(cv));
480 SV *ret = &PL_sv_undef;
482 if (SvGMAGICAL(TOPs)) SETs(sv_mortalcopy(TOPs));
483 if (SvPOK(TOPs) && SvCUR(TOPs) >= 7) {
484 const char * s = SvPVX_const(TOPs);
485 if (strnEQ(s, "CORE::", 6)) {
486 const int code = keyword(s + 6, SvCUR(TOPs) - 6, 1);
487 if (!code || code == -KEY_CORE)
488 DIE(aTHX_ "Can't find an opnumber for \"%"SVf"\"",
489 SVfARG(newSVpvn_flags(
490 s+6, SvCUR(TOPs)-6, SvFLAGS(TOPs) & SVf_UTF8
493 SV * const sv = core_prototype(NULL, s + 6, code, NULL);
499 cv = sv_2cv(TOPs, &stash, &gv, 0);
501 ret = newSVpvn_flags(
502 CvPROTO(cv), CvPROTOLEN(cv), SVs_TEMP | SvUTF8(cv)
512 CV *cv = MUTABLE_CV(PAD_SV(PL_op->op_targ));
514 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
516 PUSHs(MUTABLE_SV(cv));
530 if (GIMME != G_ARRAY) {
534 *MARK = &PL_sv_undef;
535 *MARK = refto(*MARK);
539 EXTEND_MORTAL(SP - MARK);
541 *MARK = refto(*MARK);
546 S_refto(pTHX_ SV *sv)
551 PERL_ARGS_ASSERT_REFTO;
553 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
556 if (!(sv = LvTARG(sv)))
559 SvREFCNT_inc_void_NN(sv);
561 else if (SvTYPE(sv) == SVt_PVAV) {
562 if (!AvREAL((const AV *)sv) && AvREIFY((const AV *)sv))
563 av_reify(MUTABLE_AV(sv));
565 SvREFCNT_inc_void_NN(sv);
567 else if (SvPADTMP(sv) && !IS_PADGV(sv))
571 SvREFCNT_inc_void_NN(sv);
574 sv_upgrade(rv, SVt_IV);
583 SV * const sv = POPs;
588 if (!sv || !SvROK(sv))
591 (void)sv_ref(TARG,SvRV(sv),TRUE);
603 stash = CopSTASH(PL_curcop);
605 SV * const ssv = POPs;
609 if (!ssv) goto curstash;
610 if (!SvGMAGICAL(ssv) && !SvAMAGIC(ssv) && SvROK(ssv))
611 Perl_croak(aTHX_ "Attempt to bless into a reference");
612 ptr = SvPV_const(ssv,len);
614 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
615 "Explicit blessing to '' (assuming package main)");
616 stash = gv_stashpvn(ptr, len, GV_ADD|SvUTF8(ssv));
619 (void)sv_bless(TOPs, stash);
629 const char * const elem = SvPV_const(sv, len);
630 GV * const gv = MUTABLE_GV(POPs);
635 /* elem will always be NUL terminated. */
636 const char * const second_letter = elem + 1;
639 if (len == 5 && strEQ(second_letter, "RRAY"))
640 tmpRef = MUTABLE_SV(GvAV(gv));
643 if (len == 4 && strEQ(second_letter, "ODE"))
644 tmpRef = MUTABLE_SV(GvCVu(gv));
647 if (len == 10 && strEQ(second_letter, "ILEHANDLE")) {
648 /* finally deprecated in 5.8.0 */
649 deprecate("*glob{FILEHANDLE}");
650 tmpRef = MUTABLE_SV(GvIOp(gv));
653 if (len == 6 && strEQ(second_letter, "ORMAT"))
654 tmpRef = MUTABLE_SV(GvFORM(gv));
657 if (len == 4 && strEQ(second_letter, "LOB"))
658 tmpRef = MUTABLE_SV(gv);
661 if (len == 4 && strEQ(second_letter, "ASH"))
662 tmpRef = MUTABLE_SV(GvHV(gv));
665 if (*second_letter == 'O' && !elem[2] && len == 2)
666 tmpRef = MUTABLE_SV(GvIOp(gv));
669 if (len == 4 && strEQ(second_letter, "AME"))
670 sv = newSVhek(GvNAME_HEK(gv));
673 if (len == 7 && strEQ(second_letter, "ACKAGE")) {
674 const HV * const stash = GvSTASH(gv);
675 const HEK * const hek = stash ? HvNAME_HEK(stash) : NULL;
676 sv = hek ? newSVhek(hek) : newSVpvs("__ANON__");
680 if (len == 6 && strEQ(second_letter, "CALAR"))
695 /* Pattern matching */
703 if (len == 0 || len > I32_MAX || !SvPOK(sv) || SvUTF8(sv) || SvVALID(sv)) {
704 /* Historically, study was skipped in these cases. */
708 /* Make study a no-op. It's no longer useful and its existence
709 complicates matters elsewhere. */
718 if (PL_op->op_flags & OPf_STACKED)
720 else if (PL_op->op_private & OPpTARGET_MY)
726 if(PL_op->op_type == OP_TRANSR) {
728 const char * const pv = SvPV(sv,len);
729 SV * const newsv = newSVpvn_flags(pv, len, SVs_TEMP|SvUTF8(sv));
734 TARG = sv_newmortal();
740 /* Lvalue operators. */
743 S_do_chomp(pTHX_ SV *retval, SV *sv, bool chomping)
749 PERL_ARGS_ASSERT_DO_CHOMP;
751 if (chomping && (RsSNARF(PL_rs) || RsRECORD(PL_rs)))
753 if (SvTYPE(sv) == SVt_PVAV) {
755 AV *const av = MUTABLE_AV(sv);
756 const I32 max = AvFILL(av);
758 for (i = 0; i <= max; i++) {
759 sv = MUTABLE_SV(av_fetch(av, i, FALSE));
760 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
761 do_chomp(retval, sv, chomping);
765 else if (SvTYPE(sv) == SVt_PVHV) {
766 HV* const hv = MUTABLE_HV(sv);
768 (void)hv_iterinit(hv);
769 while ((entry = hv_iternext(hv)))
770 do_chomp(retval, hv_iterval(hv,entry), chomping);
773 else if (SvREADONLY(sv)) {
775 /* SV is copy-on-write */
776 sv_force_normal_flags(sv, 0);
779 Perl_croak_no_modify(aTHX);
784 /* XXX, here sv is utf8-ized as a side-effect!
785 If encoding.pm is used properly, almost string-generating
786 operations, including literal strings, chr(), input data, etc.
787 should have been utf8-ized already, right?
789 sv_recode_to_utf8(sv, PL_encoding);
795 char *temp_buffer = NULL;
804 while (len && s[-1] == '\n') {
811 STRLEN rslen, rs_charlen;
812 const char *rsptr = SvPV_const(PL_rs, rslen);
814 rs_charlen = SvUTF8(PL_rs)
818 if (SvUTF8(PL_rs) != SvUTF8(sv)) {
819 /* Assumption is that rs is shorter than the scalar. */
821 /* RS is utf8, scalar is 8 bit. */
823 temp_buffer = (char*)bytes_from_utf8((U8*)rsptr,
826 /* Cannot downgrade, therefore cannot possibly match
828 assert (temp_buffer == rsptr);
834 else if (PL_encoding) {
835 /* RS is 8 bit, encoding.pm is used.
836 * Do not recode PL_rs as a side-effect. */
837 svrecode = newSVpvn(rsptr, rslen);
838 sv_recode_to_utf8(svrecode, PL_encoding);
839 rsptr = SvPV_const(svrecode, rslen);
840 rs_charlen = sv_len_utf8(svrecode);
843 /* RS is 8 bit, scalar is utf8. */
844 temp_buffer = (char*)bytes_to_utf8((U8*)rsptr, &rslen);
858 if (memNE(s, rsptr, rslen))
860 SvIVX(retval) += rs_charlen;
863 s = SvPV_force_nomg_nolen(sv);
871 SvREFCNT_dec(svrecode);
873 Safefree(temp_buffer);
875 if (len && !SvPOK(sv))
876 s = SvPV_force_nomg(sv, len);
879 char * const send = s + len;
880 char * const start = s;
882 while (s > start && UTF8_IS_CONTINUATION(*s))
884 if (is_utf8_string((U8*)s, send - s)) {
885 sv_setpvn(retval, s, send - s);
887 SvCUR_set(sv, s - start);
893 sv_setpvs(retval, "");
897 sv_setpvn(retval, s, 1);
904 sv_setpvs(retval, "");
912 const bool chomping = PL_op->op_type == OP_SCHOMP;
916 do_chomp(TARG, TOPs, chomping);
923 dVAR; dSP; dMARK; dTARGET; dORIGMARK;
924 const bool chomping = PL_op->op_type == OP_CHOMP;
929 do_chomp(TARG, *++MARK, chomping);
940 if (!PL_op->op_private) {
949 SV_CHECK_THINKFIRST_COW_DROP(sv);
951 switch (SvTYPE(sv)) {
955 av_undef(MUTABLE_AV(sv));
958 hv_undef(MUTABLE_HV(sv));
961 if (cv_const_sv((const CV *)sv))
962 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
963 "Constant subroutine %"SVf" undefined",
964 SVfARG(CvANON((const CV *)sv)
965 ? newSVpvs_flags("(anonymous)", SVs_TEMP)
966 : sv_2mortal(newSVhek(GvENAME_HEK(CvGV((const CV *)sv))))));
970 /* let user-undef'd sub keep its identity */
971 GV* const gv = CvGV((const CV *)sv);
972 HEK * const hek = CvNAME_HEK((CV *)sv);
973 if (hek) share_hek_hek(hek);
974 cv_undef(MUTABLE_CV(sv));
975 if (gv) CvGV_set(MUTABLE_CV(sv), gv);
977 SvANY((CV *)sv)->xcv_gv_u.xcv_hek = hek;
984 SvSetMagicSV(sv, &PL_sv_undef);
987 else if (isGV_with_GP(sv)) {
991 /* undef *Pkg::meth_name ... */
993 = GvCVu((const GV *)sv) && (stash = GvSTASH((const GV *)sv))
994 && HvENAME_get(stash);
996 if((stash = GvHV((const GV *)sv))) {
997 if(HvENAME_get(stash))
998 SvREFCNT_inc_simple_void_NN(sv_2mortal((SV *)stash));
1002 gp_free(MUTABLE_GV(sv));
1004 GvGP_set(sv, gp_ref(gp));
1005 GvSV(sv) = newSV(0);
1006 GvLINE(sv) = CopLINE(PL_curcop);
1007 GvEGV(sv) = MUTABLE_GV(sv);
1011 mro_package_moved(NULL, stash, (const GV *)sv, 0);
1013 /* undef *Foo::ISA */
1014 if( strEQ(GvNAME((const GV *)sv), "ISA")
1015 && (stash = GvSTASH((const GV *)sv))
1016 && (method_changed || HvENAME(stash)) )
1017 mro_isa_changed_in(stash);
1018 else if(method_changed)
1019 mro_method_changed_in(
1020 GvSTASH((const GV *)sv)
1027 if (SvTYPE(sv) >= SVt_PV && SvPVX_const(sv) && SvLEN(sv)) {
1043 PL_op->op_type == OP_POSTINC || PL_op->op_type == OP_I_POSTINC;
1044 if (SvTYPE(TOPs) >= SVt_PVAV || (isGV_with_GP(TOPs) && !SvFAKE(TOPs)))
1045 Perl_croak_no_modify(aTHX);
1047 TARG = sv_newmortal();
1048 sv_setsv(TARG, TOPs);
1049 if (!SvREADONLY(TOPs) && !SvGMAGICAL(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
1050 && SvIVX(TOPs) != (inc ? IV_MAX : IV_MIN))
1052 SvIV_set(TOPs, SvIVX(TOPs) + (inc ? 1 : -1));
1053 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
1057 else sv_dec_nomg(TOPs);
1059 /* special case for undef: see thread at 2003-03/msg00536.html in archive */
1060 if (inc && !SvOK(TARG))
1066 /* Ordinary operators. */
1070 dVAR; dSP; dATARGET; SV *svl, *svr;
1071 #ifdef PERL_PRESERVE_IVUV
1074 tryAMAGICbin_MG(pow_amg, AMGf_assign|AMGf_numeric);
1077 #ifdef PERL_PRESERVE_IVUV
1078 /* For integer to integer power, we do the calculation by hand wherever
1079 we're sure it is safe; otherwise we call pow() and try to convert to
1080 integer afterwards. */
1081 if (SvIV_please_nomg(svr) && SvIV_please_nomg(svl)) {
1089 const IV iv = SvIVX(svr);
1093 goto float_it; /* Can't do negative powers this way. */
1097 baseuok = SvUOK(svl);
1099 baseuv = SvUVX(svl);
1101 const IV iv = SvIVX(svl);
1104 baseuok = TRUE; /* effectively it's a UV now */
1106 baseuv = -iv; /* abs, baseuok == false records sign */
1109 /* now we have integer ** positive integer. */
1112 /* foo & (foo - 1) is zero only for a power of 2. */
1113 if (!(baseuv & (baseuv - 1))) {
1114 /* We are raising power-of-2 to a positive integer.
1115 The logic here will work for any base (even non-integer
1116 bases) but it can be less accurate than
1117 pow (base,power) or exp (power * log (base)) when the
1118 intermediate values start to spill out of the mantissa.
1119 With powers of 2 we know this can't happen.
1120 And powers of 2 are the favourite thing for perl
1121 programmers to notice ** not doing what they mean. */
1123 NV base = baseuok ? baseuv : -(NV)baseuv;
1128 while (power >>= 1) {
1136 SvIV_please_nomg(svr);
1139 unsigned int highbit = 8 * sizeof(UV);
1140 unsigned int diff = 8 * sizeof(UV);
1141 while (diff >>= 1) {
1143 if (baseuv >> highbit) {
1147 /* we now have baseuv < 2 ** highbit */
1148 if (power * highbit <= 8 * sizeof(UV)) {
1149 /* result will definitely fit in UV, so use UV math
1150 on same algorithm as above */
1153 const bool odd_power = cBOOL(power & 1);
1157 while (power >>= 1) {
1164 if (baseuok || !odd_power)
1165 /* answer is positive */
1167 else if (result <= (UV)IV_MAX)
1168 /* answer negative, fits in IV */
1169 SETi( -(IV)result );
1170 else if (result == (UV)IV_MIN)
1171 /* 2's complement assumption: special case IV_MIN */
1174 /* answer negative, doesn't fit */
1175 SETn( -(NV)result );
1183 NV right = SvNV_nomg(svr);
1184 NV left = SvNV_nomg(svl);
1187 #if defined(USE_LONG_DOUBLE) && defined(HAS_AIX_POWL_NEG_BASE_BUG)
1189 We are building perl with long double support and are on an AIX OS
1190 afflicted with a powl() function that wrongly returns NaNQ for any
1191 negative base. This was reported to IBM as PMR #23047-379 on
1192 03/06/2006. The problem exists in at least the following versions
1193 of AIX and the libm fileset, and no doubt others as well:
1195 AIX 4.3.3-ML10 bos.adt.libm 4.3.3.50
1196 AIX 5.1.0-ML04 bos.adt.libm 5.1.0.29
1197 AIX 5.2.0 bos.adt.libm 5.2.0.85
1199 So, until IBM fixes powl(), we provide the following workaround to
1200 handle the problem ourselves. Our logic is as follows: for
1201 negative bases (left), we use fmod(right, 2) to check if the
1202 exponent is an odd or even integer:
1204 - if odd, powl(left, right) == -powl(-left, right)
1205 - if even, powl(left, right) == powl(-left, right)
1207 If the exponent is not an integer, the result is rightly NaNQ, so
1208 we just return that (as NV_NAN).
1212 NV mod2 = Perl_fmod( right, 2.0 );
1213 if (mod2 == 1.0 || mod2 == -1.0) { /* odd integer */
1214 SETn( -Perl_pow( -left, right) );
1215 } else if (mod2 == 0.0) { /* even integer */
1216 SETn( Perl_pow( -left, right) );
1217 } else { /* fractional power */
1221 SETn( Perl_pow( left, right) );
1224 SETn( Perl_pow( left, right) );
1225 #endif /* HAS_AIX_POWL_NEG_BASE_BUG */
1227 #ifdef PERL_PRESERVE_IVUV
1229 SvIV_please_nomg(svr);
1237 dVAR; dSP; dATARGET; SV *svl, *svr;
1238 tryAMAGICbin_MG(mult_amg, AMGf_assign|AMGf_numeric);
1241 #ifdef PERL_PRESERVE_IVUV
1242 if (SvIV_please_nomg(svr)) {
1243 /* Unless the left argument is integer in range we are going to have to
1244 use NV maths. Hence only attempt to coerce the right argument if
1245 we know the left is integer. */
1246 /* Left operand is defined, so is it IV? */
1247 if (SvIV_please_nomg(svl)) {
1248 bool auvok = SvUOK(svl);
1249 bool buvok = SvUOK(svr);
1250 const UV topmask = (~ (UV)0) << (4 * sizeof (UV));
1251 const UV botmask = ~((~ (UV)0) << (4 * sizeof (UV)));
1260 const IV aiv = SvIVX(svl);
1263 auvok = TRUE; /* effectively it's a UV now */
1265 alow = -aiv; /* abs, auvok == false records sign */
1271 const IV biv = SvIVX(svr);
1274 buvok = TRUE; /* effectively it's a UV now */
1276 blow = -biv; /* abs, buvok == false records sign */
1280 /* If this does sign extension on unsigned it's time for plan B */
1281 ahigh = alow >> (4 * sizeof (UV));
1283 bhigh = blow >> (4 * sizeof (UV));
1285 if (ahigh && bhigh) {
1287 /* eg 32 bit is at least 0x10000 * 0x10000 == 0x100000000
1288 which is overflow. Drop to NVs below. */
1289 } else if (!ahigh && !bhigh) {
1290 /* eg 32 bit is at most 0xFFFF * 0xFFFF == 0xFFFE0001
1291 so the unsigned multiply cannot overflow. */
1292 const UV product = alow * blow;
1293 if (auvok == buvok) {
1294 /* -ve * -ve or +ve * +ve gives a +ve result. */
1298 } else if (product <= (UV)IV_MIN) {
1299 /* 2s complement assumption that (UV)-IV_MIN is correct. */
1300 /* -ve result, which could overflow an IV */
1302 SETi( -(IV)product );
1304 } /* else drop to NVs below. */
1306 /* One operand is large, 1 small */
1309 /* swap the operands */
1311 bhigh = blow; /* bhigh now the temp var for the swap */
1315 /* now, ((ahigh * blow) << half_UV_len) + (alow * blow)
1316 multiplies can't overflow. shift can, add can, -ve can. */
1317 product_middle = ahigh * blow;
1318 if (!(product_middle & topmask)) {
1319 /* OK, (ahigh * blow) won't lose bits when we shift it. */
1321 product_middle <<= (4 * sizeof (UV));
1322 product_low = alow * blow;
1324 /* as for pp_add, UV + something mustn't get smaller.
1325 IIRC ANSI mandates this wrapping *behaviour* for
1326 unsigned whatever the actual representation*/
1327 product_low += product_middle;
1328 if (product_low >= product_middle) {
1329 /* didn't overflow */
1330 if (auvok == buvok) {
1331 /* -ve * -ve or +ve * +ve gives a +ve result. */
1333 SETu( product_low );
1335 } else if (product_low <= (UV)IV_MIN) {
1336 /* 2s complement assumption again */
1337 /* -ve result, which could overflow an IV */
1339 SETi( -(IV)product_low );
1341 } /* else drop to NVs below. */
1343 } /* product_middle too large */
1344 } /* ahigh && bhigh */
1349 NV right = SvNV_nomg(svr);
1350 NV left = SvNV_nomg(svl);
1352 SETn( left * right );
1359 dVAR; dSP; dATARGET; SV *svl, *svr;
1360 tryAMAGICbin_MG(div_amg, AMGf_assign|AMGf_numeric);
1363 /* Only try to do UV divide first
1364 if ((SLOPPYDIVIDE is true) or
1365 (PERL_PRESERVE_IVUV is true and one or both SV is a UV too large
1367 The assumption is that it is better to use floating point divide
1368 whenever possible, only doing integer divide first if we can't be sure.
1369 If NV_PRESERVES_UV is true then we know at compile time that no UV
1370 can be too large to preserve, so don't need to compile the code to
1371 test the size of UVs. */
1374 # define PERL_TRY_UV_DIVIDE
1375 /* ensure that 20./5. == 4. */
1377 # ifdef PERL_PRESERVE_IVUV
1378 # ifndef NV_PRESERVES_UV
1379 # define PERL_TRY_UV_DIVIDE
1384 #ifdef PERL_TRY_UV_DIVIDE
1385 if (SvIV_please_nomg(svr) && SvIV_please_nomg(svl)) {
1386 bool left_non_neg = SvUOK(svl);
1387 bool right_non_neg = SvUOK(svr);
1391 if (right_non_neg) {
1395 const IV biv = SvIVX(svr);
1398 right_non_neg = TRUE; /* effectively it's a UV now */
1404 /* historically undef()/0 gives a "Use of uninitialized value"
1405 warning before dieing, hence this test goes here.
1406 If it were immediately before the second SvIV_please, then
1407 DIE() would be invoked before left was even inspected, so
1408 no inspection would give no warning. */
1410 DIE(aTHX_ "Illegal division by zero");
1416 const IV aiv = SvIVX(svl);
1419 left_non_neg = TRUE; /* effectively it's a UV now */
1428 /* For sloppy divide we always attempt integer division. */
1430 /* Otherwise we only attempt it if either or both operands
1431 would not be preserved by an NV. If both fit in NVs
1432 we fall through to the NV divide code below. However,
1433 as left >= right to ensure integer result here, we know that
1434 we can skip the test on the right operand - right big
1435 enough not to be preserved can't get here unless left is
1438 && (left > ((UV)1 << NV_PRESERVES_UV_BITS))
1441 /* Integer division can't overflow, but it can be imprecise. */
1442 const UV result = left / right;
1443 if (result * right == left) {
1444 SP--; /* result is valid */
1445 if (left_non_neg == right_non_neg) {
1446 /* signs identical, result is positive. */
1450 /* 2s complement assumption */
1451 if (result <= (UV)IV_MIN)
1452 SETi( -(IV)result );
1454 /* It's exact but too negative for IV. */
1455 SETn( -(NV)result );
1458 } /* tried integer divide but it was not an integer result */
1459 } /* else (PERL_ABS(result) < 1.0) or (both UVs in range for NV) */
1460 } /* one operand wasn't SvIOK */
1461 #endif /* PERL_TRY_UV_DIVIDE */
1463 NV right = SvNV_nomg(svr);
1464 NV left = SvNV_nomg(svl);
1465 (void)POPs;(void)POPs;
1466 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
1467 if (! Perl_isnan(right) && right == 0.0)
1471 DIE(aTHX_ "Illegal division by zero");
1472 PUSHn( left / right );
1479 dVAR; dSP; dATARGET;
1480 tryAMAGICbin_MG(modulo_amg, AMGf_assign|AMGf_numeric);
1484 bool left_neg = FALSE;
1485 bool right_neg = FALSE;
1486 bool use_double = FALSE;
1487 bool dright_valid = FALSE;
1490 SV * const svr = TOPs;
1491 SV * const svl = TOPm1s;
1492 if (SvIV_please_nomg(svr)) {
1493 right_neg = !SvUOK(svr);
1497 const IV biv = SvIVX(svr);
1500 right_neg = FALSE; /* effectively it's a UV now */
1507 dright = SvNV_nomg(svr);
1508 right_neg = dright < 0;
1511 if (dright < UV_MAX_P1) {
1512 right = U_V(dright);
1513 dright_valid = TRUE; /* In case we need to use double below. */
1519 /* At this point use_double is only true if right is out of range for
1520 a UV. In range NV has been rounded down to nearest UV and
1521 use_double false. */
1522 if (!use_double && SvIV_please_nomg(svl)) {
1523 left_neg = !SvUOK(svl);
1527 const IV aiv = SvIVX(svl);
1530 left_neg = FALSE; /* effectively it's a UV now */
1537 dleft = SvNV_nomg(svl);
1538 left_neg = dleft < 0;
1542 /* This should be exactly the 5.6 behaviour - if left and right are
1543 both in range for UV then use U_V() rather than floor. */
1545 if (dleft < UV_MAX_P1) {
1546 /* right was in range, so is dleft, so use UVs not double.
1550 /* left is out of range for UV, right was in range, so promote
1551 right (back) to double. */
1553 /* The +0.5 is used in 5.6 even though it is not strictly
1554 consistent with the implicit +0 floor in the U_V()
1555 inside the #if 1. */
1556 dleft = Perl_floor(dleft + 0.5);
1559 dright = Perl_floor(dright + 0.5);
1570 DIE(aTHX_ "Illegal modulus zero");
1572 dans = Perl_fmod(dleft, dright);
1573 if ((left_neg != right_neg) && dans)
1574 dans = dright - dans;
1577 sv_setnv(TARG, dans);
1583 DIE(aTHX_ "Illegal modulus zero");
1586 if ((left_neg != right_neg) && ans)
1589 /* XXX may warn: unary minus operator applied to unsigned type */
1590 /* could change -foo to be (~foo)+1 instead */
1591 if (ans <= ~((UV)IV_MAX)+1)
1592 sv_setiv(TARG, ~ans+1);
1594 sv_setnv(TARG, -(NV)ans);
1597 sv_setuv(TARG, ans);
1606 dVAR; dSP; dATARGET;
1610 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1611 /* TODO: think of some way of doing list-repeat overloading ??? */
1616 tryAMAGICbin_MG(repeat_amg, AMGf_assign);
1622 const UV uv = SvUV_nomg(sv);
1624 count = IV_MAX; /* The best we can do? */
1628 const IV iv = SvIV_nomg(sv);
1635 else if (SvNOKp(sv)) {
1636 const NV nv = SvNV_nomg(sv);
1643 count = SvIV_nomg(sv);
1645 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1647 static const char oom_list_extend[] = "Out of memory during list extend";
1648 const I32 items = SP - MARK;
1649 const I32 max = items * count;
1651 MEM_WRAP_CHECK_1(max, SV*, oom_list_extend);
1652 /* Did the max computation overflow? */
1653 if (items > 0 && max > 0 && (max < items || max < count))
1654 Perl_croak(aTHX_ oom_list_extend);
1659 /* This code was intended to fix 20010809.028:
1662 for (($x =~ /./g) x 2) {
1663 print chop; # "abcdabcd" expected as output.
1666 * but that change (#11635) broke this code:
1668 $x = [("foo")x2]; # only one "foo" ended up in the anonlist.
1670 * I can't think of a better fix that doesn't introduce
1671 * an efficiency hit by copying the SVs. The stack isn't
1672 * refcounted, and mortalisation obviously doesn't
1673 * Do The Right Thing when the stack has more than
1674 * one pointer to the same mortal value.
1678 *SP = sv_2mortal(newSVsv(*SP));
1688 repeatcpy((char*)(MARK + items), (char*)MARK,
1689 items * sizeof(const SV *), count - 1);
1692 else if (count <= 0)
1695 else { /* Note: mark already snarfed by pp_list */
1696 SV * const tmpstr = POPs;
1699 static const char oom_string_extend[] =
1700 "Out of memory during string extend";
1703 sv_setsv_nomg(TARG, tmpstr);
1704 SvPV_force_nomg(TARG, len);
1705 isutf = DO_UTF8(TARG);
1710 const STRLEN max = (UV)count * len;
1711 if (len > MEM_SIZE_MAX / count)
1712 Perl_croak(aTHX_ oom_string_extend);
1713 MEM_WRAP_CHECK_1(max, char, oom_string_extend);
1714 SvGROW(TARG, max + 1);
1715 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
1716 SvCUR_set(TARG, SvCUR(TARG) * count);
1718 *SvEND(TARG) = '\0';
1721 (void)SvPOK_only_UTF8(TARG);
1723 (void)SvPOK_only(TARG);
1725 if (PL_op->op_private & OPpREPEAT_DOLIST) {
1726 /* The parser saw this as a list repeat, and there
1727 are probably several items on the stack. But we're
1728 in scalar context, and there's no pp_list to save us
1729 now. So drop the rest of the items -- robin@kitsite.com
1741 dVAR; dSP; dATARGET; bool useleft; SV *svl, *svr;
1742 tryAMAGICbin_MG(subtr_amg, AMGf_assign|AMGf_numeric);
1745 useleft = USE_LEFT(svl);
1746 #ifdef PERL_PRESERVE_IVUV
1747 /* See comments in pp_add (in pp_hot.c) about Overflow, and how
1748 "bad things" happen if you rely on signed integers wrapping. */
1749 if (SvIV_please_nomg(svr)) {
1750 /* Unless the left argument is integer in range we are going to have to
1751 use NV maths. Hence only attempt to coerce the right argument if
1752 we know the left is integer. */
1759 a_valid = auvok = 1;
1760 /* left operand is undef, treat as zero. */
1762 /* Left operand is defined, so is it IV? */
1763 if (SvIV_please_nomg(svl)) {
1764 if ((auvok = SvUOK(svl)))
1767 const IV aiv = SvIVX(svl);
1770 auvok = 1; /* Now acting as a sign flag. */
1771 } else { /* 2s complement assumption for IV_MIN */
1779 bool result_good = 0;
1782 bool buvok = SvUOK(svr);
1787 const IV biv = SvIVX(svr);
1794 /* ?uvok if value is >= 0. basically, flagged as UV if it's +ve,
1795 else "IV" now, independent of how it came in.
1796 if a, b represents positive, A, B negative, a maps to -A etc
1801 all UV maths. negate result if A negative.
1802 subtract if signs same, add if signs differ. */
1804 if (auvok ^ buvok) {
1813 /* Must get smaller */
1818 if (result <= buv) {
1819 /* result really should be -(auv-buv). as its negation
1820 of true value, need to swap our result flag */
1832 if (result <= (UV)IV_MIN)
1833 SETi( -(IV)result );
1835 /* result valid, but out of range for IV. */
1836 SETn( -(NV)result );
1840 } /* Overflow, drop through to NVs. */
1845 NV value = SvNV_nomg(svr);
1849 /* left operand is undef, treat as zero - value */
1853 SETn( SvNV_nomg(svl) - value );
1860 dVAR; dSP; dATARGET; SV *svl, *svr;
1861 tryAMAGICbin_MG(lshift_amg, AMGf_assign|AMGf_numeric);
1865 const IV shift = SvIV_nomg(svr);
1866 if (PL_op->op_private & HINT_INTEGER) {
1867 const IV i = SvIV_nomg(svl);
1871 const UV u = SvUV_nomg(svl);
1880 dVAR; dSP; dATARGET; SV *svl, *svr;
1881 tryAMAGICbin_MG(rshift_amg, AMGf_assign|AMGf_numeric);
1885 const IV shift = SvIV_nomg(svr);
1886 if (PL_op->op_private & HINT_INTEGER) {
1887 const IV i = SvIV_nomg(svl);
1891 const UV u = SvUV_nomg(svl);
1903 tryAMAGICbin_MG(lt_amg, AMGf_set|AMGf_numeric);
1907 (SvIOK_notUV(left) && SvIOK_notUV(right))
1908 ? (SvIVX(left) < SvIVX(right))
1909 : (do_ncmp(left, right) == -1)
1919 tryAMAGICbin_MG(gt_amg, AMGf_set|AMGf_numeric);
1923 (SvIOK_notUV(left) && SvIOK_notUV(right))
1924 ? (SvIVX(left) > SvIVX(right))
1925 : (do_ncmp(left, right) == 1)
1935 tryAMAGICbin_MG(le_amg, AMGf_set|AMGf_numeric);
1939 (SvIOK_notUV(left) && SvIOK_notUV(right))
1940 ? (SvIVX(left) <= SvIVX(right))
1941 : (do_ncmp(left, right) <= 0)
1951 tryAMAGICbin_MG(ge_amg, AMGf_set|AMGf_numeric);
1955 (SvIOK_notUV(left) && SvIOK_notUV(right))
1956 ? (SvIVX(left) >= SvIVX(right))
1957 : ( (do_ncmp(left, right) & 2) == 0)
1967 tryAMAGICbin_MG(ne_amg, AMGf_set|AMGf_numeric);
1971 (SvIOK_notUV(left) && SvIOK_notUV(right))
1972 ? (SvIVX(left) != SvIVX(right))
1973 : (do_ncmp(left, right) != 0)
1978 /* compare left and right SVs. Returns:
1982 * 2: left or right was a NaN
1985 Perl_do_ncmp(pTHX_ SV* const left, SV * const right)
1989 PERL_ARGS_ASSERT_DO_NCMP;
1990 #ifdef PERL_PRESERVE_IVUV
1991 /* Fortunately it seems NaN isn't IOK */
1992 if (SvIV_please_nomg(right) && SvIV_please_nomg(left)) {
1994 const IV leftiv = SvIVX(left);
1995 if (!SvUOK(right)) {
1996 /* ## IV <=> IV ## */
1997 const IV rightiv = SvIVX(right);
1998 return (leftiv > rightiv) - (leftiv < rightiv);
2000 /* ## IV <=> UV ## */
2002 /* As (b) is a UV, it's >=0, so it must be < */
2005 const UV rightuv = SvUVX(right);
2006 return ((UV)leftiv > rightuv) - ((UV)leftiv < rightuv);
2011 /* ## UV <=> UV ## */
2012 const UV leftuv = SvUVX(left);
2013 const UV rightuv = SvUVX(right);
2014 return (leftuv > rightuv) - (leftuv < rightuv);
2016 /* ## UV <=> IV ## */
2018 const IV rightiv = SvIVX(right);
2020 /* As (a) is a UV, it's >=0, so it cannot be < */
2023 const UV leftuv = SvUVX(left);
2024 return (leftuv > (UV)rightiv) - (leftuv < (UV)rightiv);
2027 assert(0); /* NOTREACHED */
2031 NV const rnv = SvNV_nomg(right);
2032 NV const lnv = SvNV_nomg(left);
2034 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
2035 if (Perl_isnan(lnv) || Perl_isnan(rnv)) {
2038 return (lnv > rnv) - (lnv < rnv);
2057 tryAMAGICbin_MG(ncmp_amg, AMGf_numeric);
2060 value = do_ncmp(left, right);
2075 int amg_type = sle_amg;
2079 switch (PL_op->op_type) {
2098 tryAMAGICbin_MG(amg_type, AMGf_set);
2101 const int cmp = (IN_LOCALE_RUNTIME
2102 ? sv_cmp_locale_flags(left, right, 0)
2103 : sv_cmp_flags(left, right, 0));
2104 SETs(boolSV(cmp * multiplier < rhs));
2112 tryAMAGICbin_MG(seq_amg, AMGf_set);
2115 SETs(boolSV(sv_eq_flags(left, right, 0)));
2123 tryAMAGICbin_MG(sne_amg, AMGf_set);
2126 SETs(boolSV(!sv_eq_flags(left, right, 0)));
2134 tryAMAGICbin_MG(scmp_amg, 0);
2137 const int cmp = (IN_LOCALE_RUNTIME
2138 ? sv_cmp_locale_flags(left, right, 0)
2139 : sv_cmp_flags(left, right, 0));
2147 dVAR; dSP; dATARGET;
2148 tryAMAGICbin_MG(band_amg, AMGf_assign);
2151 if (SvNIOKp(left) || SvNIOKp(right)) {
2152 const bool left_ro_nonnum = !SvNIOKp(left) && SvREADONLY(left);
2153 const bool right_ro_nonnum = !SvNIOKp(right) && SvREADONLY(right);
2154 if (PL_op->op_private & HINT_INTEGER) {
2155 const IV i = SvIV_nomg(left) & SvIV_nomg(right);
2159 const UV u = SvUV_nomg(left) & SvUV_nomg(right);
2162 if (left_ro_nonnum && left != TARG) SvNIOK_off(left);
2163 if (right_ro_nonnum) SvNIOK_off(right);
2166 do_vop(PL_op->op_type, TARG, left, right);
2175 dVAR; dSP; dATARGET;
2176 const int op_type = PL_op->op_type;
2178 tryAMAGICbin_MG((op_type == OP_BIT_OR ? bor_amg : bxor_amg), AMGf_assign);
2181 if (SvNIOKp(left) || SvNIOKp(right)) {
2182 const bool left_ro_nonnum = !SvNIOKp(left) && SvREADONLY(left);
2183 const bool right_ro_nonnum = !SvNIOKp(right) && SvREADONLY(right);
2184 if (PL_op->op_private & HINT_INTEGER) {
2185 const IV l = (USE_LEFT(left) ? SvIV_nomg(left) : 0);
2186 const IV r = SvIV_nomg(right);
2187 const IV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2191 const UV l = (USE_LEFT(left) ? SvUV_nomg(left) : 0);
2192 const UV r = SvUV_nomg(right);
2193 const UV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2196 if (left_ro_nonnum && left != TARG) SvNIOK_off(left);
2197 if (right_ro_nonnum) SvNIOK_off(right);
2200 do_vop(op_type, TARG, left, right);
2207 PERL_STATIC_INLINE bool
2208 S_negate_string(pTHX)
2213 SV * const sv = TOPs;
2214 if (!SvPOKp(sv) || SvNIOK(sv) || (!SvPOK(sv) && SvNIOKp(sv)))
2216 s = SvPV_nomg_const(sv, len);
2217 if (isIDFIRST(*s)) {
2218 sv_setpvs(TARG, "-");
2221 else if (*s == '+' || (*s == '-' && !looks_like_number(sv))) {
2222 sv_setsv_nomg(TARG, sv);
2223 *SvPV_force_nomg(TARG, len) = *s == '-' ? '+' : '-';
2233 tryAMAGICun_MG(neg_amg, AMGf_numeric);
2234 if (S_negate_string(aTHX)) return NORMAL;
2236 SV * const sv = TOPs;
2239 /* It's publicly an integer */
2242 if (SvIVX(sv) == IV_MIN) {
2243 /* 2s complement assumption. */
2244 SETi(SvIVX(sv)); /* special case: -((UV)IV_MAX+1) == IV_MIN */
2247 else if (SvUVX(sv) <= IV_MAX) {
2252 else if (SvIVX(sv) != IV_MIN) {
2256 #ifdef PERL_PRESERVE_IVUV
2263 if (SvNIOKp(sv) && (SvNIOK(sv) || !SvPOK(sv)))
2264 SETn(-SvNV_nomg(sv));
2265 else if (SvPOKp(sv) && SvIV_please_nomg(sv))
2266 goto oops_its_an_int;
2268 SETn(-SvNV_nomg(sv));
2276 tryAMAGICun_MG(not_amg, AMGf_set);
2277 *PL_stack_sp = boolSV(!SvTRUE_nomg(*PL_stack_sp));
2284 tryAMAGICun_MG(compl_amg, AMGf_numeric);
2288 if (PL_op->op_private & HINT_INTEGER) {
2289 const IV i = ~SvIV_nomg(sv);
2293 const UV u = ~SvUV_nomg(sv);
2302 (void)SvPV_nomg_const(sv,len); /* force check for uninit var */
2303 sv_setsv_nomg(TARG, sv);
2304 tmps = (U8*)SvPV_force_nomg(TARG, len);
2307 /* Calculate exact length, let's not estimate. */
2312 U8 * const send = tmps + len;
2313 U8 * const origtmps = tmps;
2314 const UV utf8flags = UTF8_ALLOW_ANYUV;
2316 while (tmps < send) {
2317 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2319 targlen += UNISKIP(~c);
2325 /* Now rewind strings and write them. */
2332 Newx(result, targlen + 1, U8);
2334 while (tmps < send) {
2335 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2337 p = uvchr_to_utf8_flags(p, ~c, UNICODE_ALLOW_ANY);
2340 sv_usepvn_flags(TARG, (char*)result, targlen,
2341 SV_HAS_TRAILING_NUL);
2348 Newx(result, nchar + 1, U8);
2350 while (tmps < send) {
2351 const U8 c = (U8)utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2356 sv_usepvn_flags(TARG, (char*)result, nchar, SV_HAS_TRAILING_NUL);
2365 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
2368 for ( ; anum >= (I32)sizeof(long); anum -= (I32)sizeof(long), tmpl++)
2373 for ( ; anum > 0; anum--, tmps++)
2381 /* integer versions of some of the above */
2385 dVAR; dSP; dATARGET;
2386 tryAMAGICbin_MG(mult_amg, AMGf_assign);
2389 SETi( left * right );
2397 dVAR; dSP; dATARGET;
2398 tryAMAGICbin_MG(div_amg, AMGf_assign);
2401 IV value = SvIV_nomg(right);
2403 DIE(aTHX_ "Illegal division by zero");
2404 num = SvIV_nomg(left);
2406 /* avoid FPE_INTOVF on some platforms when num is IV_MIN */
2410 value = num / value;
2416 #if defined(__GLIBC__) && IVSIZE == 8 && !defined(PERL_DEBUG_READONLY_OPS)
2423 /* This is the vanilla old i_modulo. */
2424 dVAR; dSP; dATARGET;
2425 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2429 DIE(aTHX_ "Illegal modulus zero");
2430 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2434 SETi( left % right );
2439 #if defined(__GLIBC__) && IVSIZE == 8 && !defined(PERL_DEBUG_READONLY_OPS)
2444 /* This is the i_modulo with the workaround for the _moddi3 bug
2445 * in (at least) glibc 2.2.5 (the PERL_ABS() the workaround).
2446 * See below for pp_i_modulo. */
2447 dVAR; dSP; dATARGET;
2448 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2452 DIE(aTHX_ "Illegal modulus zero");
2453 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2457 SETi( left % PERL_ABS(right) );
2464 dVAR; dSP; dATARGET;
2465 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2469 DIE(aTHX_ "Illegal modulus zero");
2470 /* The assumption is to use hereafter the old vanilla version... */
2472 PL_ppaddr[OP_I_MODULO] =
2474 /* .. but if we have glibc, we might have a buggy _moddi3
2475 * (at least glicb 2.2.5 is known to have this bug), in other
2476 * words our integer modulus with negative quad as the second
2477 * argument might be broken. Test for this and re-patch the
2478 * opcode dispatch table if that is the case, remembering to
2479 * also apply the workaround so that this first round works
2480 * right, too. See [perl #9402] for more information. */
2484 /* Cannot do this check with inlined IV constants since
2485 * that seems to work correctly even with the buggy glibc. */
2487 /* Yikes, we have the bug.
2488 * Patch in the workaround version. */
2490 PL_ppaddr[OP_I_MODULO] =
2491 &Perl_pp_i_modulo_1;
2492 /* Make certain we work right this time, too. */
2493 right = PERL_ABS(right);
2496 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2500 SETi( left % right );
2508 dVAR; dSP; dATARGET;
2509 tryAMAGICbin_MG(add_amg, AMGf_assign);
2511 dPOPTOPiirl_ul_nomg;
2512 SETi( left + right );
2519 dVAR; dSP; dATARGET;
2520 tryAMAGICbin_MG(subtr_amg, AMGf_assign);
2522 dPOPTOPiirl_ul_nomg;
2523 SETi( left - right );
2531 tryAMAGICbin_MG(lt_amg, AMGf_set);
2534 SETs(boolSV(left < right));
2542 tryAMAGICbin_MG(gt_amg, AMGf_set);
2545 SETs(boolSV(left > right));
2553 tryAMAGICbin_MG(le_amg, AMGf_set);
2556 SETs(boolSV(left <= right));
2564 tryAMAGICbin_MG(ge_amg, AMGf_set);
2567 SETs(boolSV(left >= right));
2575 tryAMAGICbin_MG(eq_amg, AMGf_set);
2578 SETs(boolSV(left == right));
2586 tryAMAGICbin_MG(ne_amg, AMGf_set);
2589 SETs(boolSV(left != right));
2597 tryAMAGICbin_MG(ncmp_amg, 0);
2604 else if (left < right)
2616 tryAMAGICun_MG(neg_amg, 0);
2617 if (S_negate_string(aTHX)) return NORMAL;
2619 SV * const sv = TOPs;
2620 IV const i = SvIV_nomg(sv);
2626 /* High falutin' math. */
2631 tryAMAGICbin_MG(atan2_amg, 0);
2634 SETn(Perl_atan2(left, right));
2642 int amg_type = sin_amg;
2643 const char *neg_report = NULL;
2644 NV (*func)(NV) = Perl_sin;
2645 const int op_type = PL_op->op_type;
2662 amg_type = sqrt_amg;
2664 neg_report = "sqrt";
2669 tryAMAGICun_MG(amg_type, 0);
2671 SV * const arg = POPs;
2672 const NV value = SvNV_nomg(arg);
2674 if (op_type == OP_LOG ? (value <= 0.0) : (value < 0.0)) {
2675 SET_NUMERIC_STANDARD();
2676 /* diag_listed_as: Can't take log of %g */
2677 DIE(aTHX_ "Can't take %s of %"NVgf, neg_report, value);
2680 XPUSHn(func(value));
2685 /* Support Configure command-line overrides for rand() functions.
2686 After 5.005, perhaps we should replace this by Configure support
2687 for drand48(), random(), or rand(). For 5.005, though, maintain
2688 compatibility by calling rand() but allow the user to override it.
2689 See INSTALL for details. --Andy Dougherty 15 July 1998
2691 /* Now it's after 5.005, and Configure supports drand48() and random(),
2692 in addition to rand(). So the overrides should not be needed any more.
2693 --Jarkko Hietaniemi 27 September 1998
2696 #ifndef HAS_DRAND48_PROTO
2697 extern double drand48 (void);
2707 value = 1.0; (void)POPs;
2713 if (!PL_srand_called) {
2714 (void)seedDrand01((Rand_seed_t)seed());
2715 PL_srand_called = TRUE;
2727 if (MAXARG >= 1 && (TOPs || POPs)) {
2734 pv = SvPV(top, len);
2735 flags = grok_number(pv, len, &anum);
2737 if (!(flags & IS_NUMBER_IN_UV)) {
2738 Perl_ck_warner_d(aTHX_ packWARN(WARN_OVERFLOW),
2739 "Integer overflow in srand");
2747 (void)seedDrand01((Rand_seed_t)anum);
2748 PL_srand_called = TRUE;
2752 /* Historically srand always returned true. We can avoid breaking
2754 sv_setpvs(TARG, "0 but true");
2763 tryAMAGICun_MG(int_amg, AMGf_numeric);
2765 SV * const sv = TOPs;
2766 const IV iv = SvIV_nomg(sv);
2767 /* XXX it's arguable that compiler casting to IV might be subtly
2768 different from modf (for numbers inside (IV_MIN,UV_MAX)) in which
2769 else preferring IV has introduced a subtle behaviour change bug. OTOH
2770 relying on floating point to be accurate is a bug. */
2775 else if (SvIOK(sv)) {
2777 SETu(SvUV_nomg(sv));
2782 const NV value = SvNV_nomg(sv);
2784 if (value < (NV)UV_MAX + 0.5) {
2787 SETn(Perl_floor(value));
2791 if (value > (NV)IV_MIN - 0.5) {
2794 SETn(Perl_ceil(value));
2805 tryAMAGICun_MG(abs_amg, AMGf_numeric);
2807 SV * const sv = TOPs;
2808 /* This will cache the NV value if string isn't actually integer */
2809 const IV iv = SvIV_nomg(sv);
2814 else if (SvIOK(sv)) {
2815 /* IVX is precise */
2817 SETu(SvUV_nomg(sv)); /* force it to be numeric only */
2825 /* 2s complement assumption. Also, not really needed as
2826 IV_MIN and -IV_MIN should both be %100...00 and NV-able */
2832 const NV value = SvNV_nomg(sv);
2846 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES;
2850 SV* const sv = POPs;
2852 tmps = (SvPV_const(sv, len));
2854 /* If Unicode, try to downgrade
2855 * If not possible, croak. */
2856 SV* const tsv = sv_2mortal(newSVsv(sv));
2859 sv_utf8_downgrade(tsv, FALSE);
2860 tmps = SvPV_const(tsv, len);
2862 if (PL_op->op_type == OP_HEX)
2865 while (*tmps && len && isSPACE(*tmps))
2869 if (*tmps == 'x' || *tmps == 'X') {
2871 result_uv = grok_hex (tmps, &len, &flags, &result_nv);
2873 else if (*tmps == 'b' || *tmps == 'B')
2874 result_uv = grok_bin (tmps, &len, &flags, &result_nv);
2876 result_uv = grok_oct (tmps, &len, &flags, &result_nv);
2878 if (flags & PERL_SCAN_GREATER_THAN_UV_MAX) {
2892 SV * const sv = TOPs;
2894 if (SvGAMAGIC(sv)) {
2895 /* For an overloaded or magic scalar, we can't know in advance if
2896 it's going to be UTF-8 or not. Also, we can't call sv_len_utf8 as
2897 it likes to cache the length. Maybe that should be a documented
2902 = sv_2pv_flags(sv, &len,
2903 SV_UNDEF_RETURNS_NULL|SV_CONST_RETURN|SV_GMAGIC);
2906 if (!SvPADTMP(TARG)) {
2907 sv_setsv(TARG, &PL_sv_undef);
2912 else if (DO_UTF8(sv)) {
2913 SETi(utf8_length((U8*)p, (U8*)p + len));
2917 } else if (SvOK(sv)) {
2918 /* Neither magic nor overloaded. */
2920 SETi(sv_len_utf8(sv));
2924 if (!SvPADTMP(TARG)) {
2925 sv_setsv_nomg(TARG, &PL_sv_undef);
2933 /* Returns false if substring is completely outside original string.
2934 No length is indicated by len_iv = 0 and len_is_uv = 0. len_is_uv must
2935 always be true for an explicit 0.
2938 Perl_translate_substr_offsets(pTHX_ STRLEN curlen, IV pos1_iv,
2939 bool pos1_is_uv, IV len_iv,
2940 bool len_is_uv, STRLEN *posp,
2946 PERL_ARGS_ASSERT_TRANSLATE_SUBSTR_OFFSETS;
2948 if (!pos1_is_uv && pos1_iv < 0 && curlen) {
2949 pos1_is_uv = curlen-1 > ~(UV)pos1_iv;
2952 if ((pos1_is_uv || pos1_iv > 0) && (UV)pos1_iv > curlen)
2955 if (len_iv || len_is_uv) {
2956 if (!len_is_uv && len_iv < 0) {
2957 pos2_iv = curlen + len_iv;
2959 pos2_is_uv = curlen-1 > ~(UV)len_iv;
2962 } else { /* len_iv >= 0 */
2963 if (!pos1_is_uv && pos1_iv < 0) {
2964 pos2_iv = pos1_iv + len_iv;
2965 pos2_is_uv = (UV)len_iv > (UV)IV_MAX;
2967 if ((UV)len_iv > curlen-(UV)pos1_iv)
2970 pos2_iv = pos1_iv+len_iv;
2980 if (!pos2_is_uv && pos2_iv < 0) {
2981 if (!pos1_is_uv && pos1_iv < 0)
2985 else if (!pos1_is_uv && pos1_iv < 0)
2988 if ((UV)pos2_iv < (UV)pos1_iv)
2990 if ((UV)pos2_iv > curlen)
2993 /* pos1_iv and pos2_iv both in 0..curlen, so the cast is safe */
2994 *posp = (STRLEN)( (UV)pos1_iv );
2995 *lenp = (STRLEN)( (UV)pos2_iv - (UV)pos1_iv );
3012 I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
3013 const bool rvalue = (GIMME_V != G_VOID);
3016 const char *repl = NULL;
3018 int num_args = PL_op->op_private & 7;
3019 bool repl_need_utf8_upgrade = FALSE;
3020 bool repl_is_utf8 = FALSE;
3024 if(!(repl_sv = POPs)) num_args--;
3026 if ((len_sv = POPs)) {
3027 len_iv = SvIV(len_sv);
3028 len_is_uv = len_iv ? SvIOK_UV(len_sv) : 1;
3033 pos1_iv = SvIV(pos_sv);
3034 pos1_is_uv = SvIOK_UV(pos_sv);
3036 if (PL_op->op_private & OPpSUBSTR_REPL_FIRST) {
3042 repl = SvPV_const(repl_sv, repl_len);
3043 repl_is_utf8 = DO_UTF8(repl_sv) && repl_len;
3046 sv_utf8_upgrade(sv);
3048 else if (DO_UTF8(sv))
3049 repl_need_utf8_upgrade = TRUE;
3053 ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
3054 sv_magic(ret, NULL, PERL_MAGIC_substr, NULL, 0);
3056 LvTARG(ret) = SvREFCNT_inc_simple(sv);
3058 pos1_is_uv || pos1_iv >= 0
3059 ? (STRLEN)(UV)pos1_iv
3060 : (LvFLAGS(ret) |= 1, (STRLEN)(UV)-pos1_iv);
3062 len_is_uv || len_iv > 0
3063 ? (STRLEN)(UV)len_iv
3064 : (LvFLAGS(ret) |= 2, (STRLEN)(UV)-len_iv);
3067 PUSHs(ret); /* avoid SvSETMAGIC here */
3070 tmps = SvPV_const(sv, curlen);
3072 utf8_curlen = sv_len_utf8_nomg(sv);
3073 if (utf8_curlen == curlen)
3076 curlen = utf8_curlen;
3082 STRLEN pos, len, byte_len, byte_pos;
3084 if (!translate_substr_offsets(
3085 curlen, pos1_iv, pos1_is_uv, len_iv, len_is_uv, &pos, &len
3089 byte_pos = utf8_curlen
3090 ? sv_pos_u2b_flags(sv, pos, &byte_len, SV_CONST_RETURN) : pos;
3095 SvTAINTED_off(TARG); /* decontaminate */
3096 SvUTF8_off(TARG); /* decontaminate */
3097 sv_setpvn(TARG, tmps, byte_len);
3098 #ifdef USE_LOCALE_COLLATE
3099 sv_unmagic(TARG, PERL_MAGIC_collxfrm);
3106 SV* repl_sv_copy = NULL;
3108 if (repl_need_utf8_upgrade) {
3109 repl_sv_copy = newSVsv(repl_sv);
3110 sv_utf8_upgrade(repl_sv_copy);
3111 repl = SvPV_const(repl_sv_copy, repl_len);
3112 repl_is_utf8 = DO_UTF8(repl_sv_copy) && repl_len;
3115 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR),
3116 "Attempt to use reference as lvalue in substr"
3120 sv_insert_flags(sv, byte_pos, byte_len, repl, repl_len, 0);
3123 SvREFCNT_dec(repl_sv_copy);
3135 Perl_croak(aTHX_ "substr outside of string");
3136 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR), "substr outside of string");
3143 const IV size = POPi;
3144 const IV offset = POPi;
3145 SV * const src = POPs;
3146 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
3149 if (lvalue) { /* it's an lvalue! */
3150 ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
3151 sv_magic(ret, NULL, PERL_MAGIC_vec, NULL, 0);
3153 LvTARG(ret) = SvREFCNT_inc_simple(src);
3154 LvTARGOFF(ret) = offset;
3155 LvTARGLEN(ret) = size;
3159 SvTAINTED_off(TARG); /* decontaminate */
3163 sv_setuv(ret, do_vecget(src, offset, size));
3179 const char *little_p;
3182 const bool is_index = PL_op->op_type == OP_INDEX;
3183 const bool threeargs = MAXARG >= 3 && (TOPs || ((void)POPs,0));
3189 big_p = SvPV_const(big, biglen);
3190 little_p = SvPV_const(little, llen);
3192 big_utf8 = DO_UTF8(big);
3193 little_utf8 = DO_UTF8(little);
3194 if (big_utf8 ^ little_utf8) {
3195 /* One needs to be upgraded. */
3196 if (little_utf8 && !PL_encoding) {
3197 /* Well, maybe instead we might be able to downgrade the small
3199 char * const pv = (char*)bytes_from_utf8((U8 *)little_p, &llen,
3202 /* If the large string is ISO-8859-1, and it's not possible to
3203 convert the small string to ISO-8859-1, then there is no
3204 way that it could be found anywhere by index. */
3209 /* At this point, pv is a malloc()ed string. So donate it to temp
3210 to ensure it will get free()d */
3211 little = temp = newSV(0);
3212 sv_usepvn(temp, pv, llen);
3213 little_p = SvPVX(little);
3216 ? newSVpvn(big_p, biglen) : newSVpvn(little_p, llen);
3219 sv_recode_to_utf8(temp, PL_encoding);
3221 sv_utf8_upgrade(temp);
3226 big_p = SvPV_const(big, biglen);
3229 little_p = SvPV_const(little, llen);
3233 if (SvGAMAGIC(big)) {
3234 /* Life just becomes a lot easier if I use a temporary here.
3235 Otherwise I need to avoid calls to sv_pos_u2b(), which (dangerously)
3236 will trigger magic and overloading again, as will fbm_instr()
3238 big = newSVpvn_flags(big_p, biglen,
3239 SVs_TEMP | (big_utf8 ? SVf_UTF8 : 0));
3242 if (SvGAMAGIC(little) || (is_index && !SvOK(little))) {
3243 /* index && SvOK() is a hack. fbm_instr() calls SvPV_const, which will
3244 warn on undef, and we've already triggered a warning with the
3245 SvPV_const some lines above. We can't remove that, as we need to
3246 call some SvPV to trigger overloading early and find out if the
3248 This is all getting to messy. The API isn't quite clean enough,
3249 because data access has side effects.
3251 little = newSVpvn_flags(little_p, llen,
3252 SVs_TEMP | (little_utf8 ? SVf_UTF8 : 0));
3253 little_p = SvPVX(little);
3257 offset = is_index ? 0 : biglen;
3259 if (big_utf8 && offset > 0)
3260 sv_pos_u2b(big, &offset, 0);
3266 else if (offset > (I32)biglen)
3268 if (!(little_p = is_index
3269 ? fbm_instr((unsigned char*)big_p + offset,
3270 (unsigned char*)big_p + biglen, little, 0)
3271 : rninstr(big_p, big_p + offset,
3272 little_p, little_p + llen)))
3275 retval = little_p - big_p;
3276 if (retval > 0 && big_utf8)
3277 sv_pos_b2u(big, &retval);
3287 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
3288 SvTAINTED_off(TARG);
3289 do_sprintf(TARG, SP-MARK, MARK+1);
3290 TAINT_IF(SvTAINTED(TARG));
3302 const U8 *s = (U8*)SvPV_const(argsv, len);
3304 if (PL_encoding && SvPOK(argsv) && !DO_UTF8(argsv)) {
3305 SV * const tmpsv = sv_2mortal(newSVsv(argsv));
3306 s = (U8*)sv_recode_to_utf8(tmpsv, PL_encoding);
3310 XPUSHu(DO_UTF8(argsv) ?
3311 utf8n_to_uvchr(s, UTF8_MAXBYTES, 0, UTF8_ALLOW_ANYUV) :
3325 if (!IN_BYTES /* under bytes, chr(-1) eq chr(0xff), etc. */
3326 && ((SvIOKp(top) && !SvIsUV(top) && SvIV_nomg(top) < 0)
3328 ((SvNOKp(top) || (SvOK(top) && !SvIsUV(top)))
3329 && SvNV_nomg(top) < 0.0))) {
3330 if (ckWARN(WARN_UTF8)) {
3331 if (SvGMAGICAL(top)) {
3332 SV *top2 = sv_newmortal();
3333 sv_setsv_nomg(top2, top);
3336 Perl_warner(aTHX_ packWARN(WARN_UTF8),
3337 "Invalid negative number (%"SVf") in chr", top);
3339 value = UNICODE_REPLACEMENT;
3341 value = SvUV_nomg(top);
3344 SvUPGRADE(TARG,SVt_PV);
3346 if (value > 255 && !IN_BYTES) {
3347 SvGROW(TARG, (STRLEN)UNISKIP(value)+1);
3348 tmps = (char*)uvchr_to_utf8_flags((U8*)SvPVX(TARG), value, 0);
3349 SvCUR_set(TARG, tmps - SvPVX_const(TARG));
3351 (void)SvPOK_only(TARG);
3360 *tmps++ = (char)value;
3362 (void)SvPOK_only(TARG);
3364 if (PL_encoding && !IN_BYTES) {
3365 sv_recode_to_utf8(TARG, PL_encoding);
3367 if (SvCUR(TARG) == 0
3368 || ! is_utf8_string((U8*)tmps, SvCUR(TARG))
3369 || UTF8_IS_REPLACEMENT((U8*) tmps, (U8*) tmps + SvCUR(TARG)))
3374 *tmps++ = (char)value;
3390 const char *tmps = SvPV_const(left, len);
3392 if (DO_UTF8(left)) {
3393 /* If Unicode, try to downgrade.
3394 * If not possible, croak.
3395 * Yes, we made this up. */
3396 SV* const tsv = sv_2mortal(newSVsv(left));
3399 sv_utf8_downgrade(tsv, FALSE);
3400 tmps = SvPV_const(tsv, len);
3402 # ifdef USE_ITHREADS
3404 if (!PL_reentrant_buffer->_crypt_struct_buffer) {
3405 /* This should be threadsafe because in ithreads there is only
3406 * one thread per interpreter. If this would not be true,
3407 * we would need a mutex to protect this malloc. */
3408 PL_reentrant_buffer->_crypt_struct_buffer =
3409 (struct crypt_data *)safemalloc(sizeof(struct crypt_data));
3410 #if defined(__GLIBC__) || defined(__EMX__)
3411 if (PL_reentrant_buffer->_crypt_struct_buffer) {
3412 PL_reentrant_buffer->_crypt_struct_buffer->initialized = 0;
3413 /* work around glibc-2.2.5 bug */
3414 PL_reentrant_buffer->_crypt_struct_buffer->current_saltbits = 0;
3418 # endif /* HAS_CRYPT_R */
3419 # endif /* USE_ITHREADS */
3421 sv_setpv(TARG, fcrypt(tmps, SvPV_nolen_const(right)));
3423 sv_setpv(TARG, PerlProc_crypt(tmps, SvPV_nolen_const(right)));
3429 "The crypt() function is unimplemented due to excessive paranoia.");
3433 /* Generally UTF-8 and UTF-EBCDIC are indistinguishable at this level. So
3434 * most comments below say UTF-8, when in fact they mean UTF-EBCDIC as well */
3436 /* Generates code to store a unicode codepoint c that is known to occupy
3437 * exactly two UTF-8 and UTF-EBCDIC bytes; it is stored into p and p+1,
3438 * and p is advanced to point to the next available byte after the two bytes */
3439 #define CAT_UNI_TO_UTF8_TWO_BYTE(p, c) \
3441 *(p)++ = UTF8_TWO_BYTE_HI(c); \
3442 *((p)++) = UTF8_TWO_BYTE_LO(c); \
3447 /* Actually is both lcfirst() and ucfirst(). Only the first character
3448 * changes. This means that possibly we can change in-place, ie., just
3449 * take the source and change that one character and store it back, but not
3450 * if read-only etc, or if the length changes */
3455 STRLEN slen; /* slen is the byte length of the whole SV. */
3458 bool inplace; /* ? Convert first char only, in-place */
3459 bool doing_utf8 = FALSE; /* ? using utf8 */
3460 bool convert_source_to_utf8 = FALSE; /* ? need to convert */
3461 const int op_type = PL_op->op_type;
3464 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
3465 STRLEN ulen; /* ulen is the byte length of the original Unicode character
3466 * stored as UTF-8 at s. */
3467 STRLEN tculen; /* tculen is the byte length of the freshly titlecased (or
3468 * lowercased) character stored in tmpbuf. May be either
3469 * UTF-8 or not, but in either case is the number of bytes */
3470 bool tainted = FALSE;
3474 s = (const U8*)SvPV_nomg_const(source, slen);
3476 if (ckWARN(WARN_UNINITIALIZED))
3477 report_uninit(source);
3482 /* We may be able to get away with changing only the first character, in
3483 * place, but not if read-only, etc. Later we may discover more reasons to
3484 * not convert in-place. */
3485 inplace = SvPADTMP(source) && !SvREADONLY(source) && SvTEMP(source);
3487 /* First calculate what the changed first character should be. This affects
3488 * whether we can just swap it out, leaving the rest of the string unchanged,
3489 * or even if have to convert the dest to UTF-8 when the source isn't */
3491 if (! slen) { /* If empty */
3492 need = 1; /* still need a trailing NUL */
3495 else if (DO_UTF8(source)) { /* Is the source utf8? */
3498 if (op_type == OP_UCFIRST) {
3499 _to_utf8_title_flags(s, tmpbuf, &tculen,
3500 cBOOL(IN_LOCALE_RUNTIME), &tainted);
3503 _to_utf8_lower_flags(s, tmpbuf, &tculen,
3504 cBOOL(IN_LOCALE_RUNTIME), &tainted);
3507 /* we can't do in-place if the length changes. */
3508 if (ulen != tculen) inplace = FALSE;
3509 need = slen + 1 - ulen + tculen;
3511 else { /* Non-zero length, non-UTF-8, Need to consider locale and if
3512 * latin1 is treated as caseless. Note that a locale takes
3514 ulen = 1; /* Original character is 1 byte */
3515 tculen = 1; /* Most characters will require one byte, but this will
3516 * need to be overridden for the tricky ones */
3519 if (op_type == OP_LCFIRST) {
3521 /* lower case the first letter: no trickiness for any character */
3522 *tmpbuf = (IN_LOCALE_RUNTIME) ? toLOWER_LC(*s) :
3523 ((IN_UNI_8_BIT) ? toLOWER_LATIN1(*s) : toLOWER(*s));
3526 else if (IN_LOCALE_RUNTIME) {
3527 *tmpbuf = toUPPER_LC(*s); /* This would be a bug if any locales
3528 * have upper and title case different
3531 else if (! IN_UNI_8_BIT) {
3532 *tmpbuf = toUPPER(*s); /* Returns caseless for non-ascii, or
3533 * on EBCDIC machines whatever the
3534 * native function does */
3536 else { /* is ucfirst non-UTF-8, not in locale, and cased latin1 */
3537 UV title_ord = _to_upper_title_latin1(*s, tmpbuf, &tculen, 's');
3539 assert(tculen == 2);
3541 /* If the result is an upper Latin1-range character, it can
3542 * still be represented in one byte, which is its ordinal */
3543 if (UTF8_IS_DOWNGRADEABLE_START(*tmpbuf)) {
3544 *tmpbuf = (U8) title_ord;
3548 /* Otherwise it became more than one ASCII character (in
3549 * the case of LATIN_SMALL_LETTER_SHARP_S) or changed to
3550 * beyond Latin1, so the number of bytes changed, so can't
3551 * replace just the first character in place. */
3554 /* If the result won't fit in a byte, the entire result will
3555 * have to be in UTF-8. Assume worst case sizing in
3556 * conversion. (all latin1 characters occupy at most two bytes
3558 if (title_ord > 255) {
3560 convert_source_to_utf8 = TRUE;
3561 need = slen * 2 + 1;
3563 /* The (converted) UTF-8 and UTF-EBCDIC lengths of all
3564 * (both) characters whose title case is above 255 is
3568 else { /* LATIN_SMALL_LETTER_SHARP_S expands by 1 byte */
3569 need = slen + 1 + 1;
3573 } /* End of use Unicode (Latin1) semantics */
3574 } /* End of changing the case of the first character */
3576 /* Here, have the first character's changed case stored in tmpbuf. Ready to
3577 * generate the result */
3580 /* We can convert in place. This means we change just the first
3581 * character without disturbing the rest; no need to grow */
3583 s = d = (U8*)SvPV_force_nomg(source, slen);
3589 /* Here, we can't convert in place; we earlier calculated how much
3590 * space we will need, so grow to accommodate that */
3591 SvUPGRADE(dest, SVt_PV);
3592 d = (U8*)SvGROW(dest, need);
3593 (void)SvPOK_only(dest);
3600 if (! convert_source_to_utf8) {
3602 /* Here both source and dest are in UTF-8, but have to create
3603 * the entire output. We initialize the result to be the
3604 * title/lower cased first character, and then append the rest
3606 sv_setpvn(dest, (char*)tmpbuf, tculen);
3608 sv_catpvn(dest, (char*)(s + ulen), slen - ulen);
3612 const U8 *const send = s + slen;
3614 /* Here the dest needs to be in UTF-8, but the source isn't,
3615 * except we earlier UTF-8'd the first character of the source
3616 * into tmpbuf. First put that into dest, and then append the
3617 * rest of the source, converting it to UTF-8 as we go. */
3619 /* Assert tculen is 2 here because the only two characters that
3620 * get to this part of the code have 2-byte UTF-8 equivalents */
3622 *d++ = *(tmpbuf + 1);
3623 s++; /* We have just processed the 1st char */
3625 for (; s < send; s++) {
3626 d = uvchr_to_utf8(d, *s);
3629 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3633 else { /* in-place UTF-8. Just overwrite the first character */
3634 Copy(tmpbuf, d, tculen, U8);
3635 SvCUR_set(dest, need - 1);
3643 else { /* Neither source nor dest are in or need to be UTF-8 */
3645 if (IN_LOCALE_RUNTIME) {
3649 if (inplace) { /* in-place, only need to change the 1st char */
3652 else { /* Not in-place */
3654 /* Copy the case-changed character(s) from tmpbuf */
3655 Copy(tmpbuf, d, tculen, U8);
3656 d += tculen - 1; /* Code below expects d to point to final
3657 * character stored */
3660 else { /* empty source */
3661 /* See bug #39028: Don't taint if empty */
3665 /* In a "use bytes" we don't treat the source as UTF-8, but, still want
3666 * the destination to retain that flag */
3670 if (!inplace) { /* Finish the rest of the string, unchanged */
3671 /* This will copy the trailing NUL */
3672 Copy(s + 1, d + 1, slen, U8);
3673 SvCUR_set(dest, need - 1);
3676 if (dest != source && SvTAINTED(source))
3682 /* There's so much setup/teardown code common between uc and lc, I wonder if
3683 it would be worth merging the two, and just having a switch outside each
3684 of the three tight loops. There is less and less commonality though */
3698 if (SvPADTMP(source) && !SvREADONLY(source) && !SvAMAGIC(source)
3699 && SvTEMP(source) && !DO_UTF8(source)
3700 && (IN_LOCALE_RUNTIME || ! IN_UNI_8_BIT)) {
3702 /* We can convert in place. The reason we can't if in UNI_8_BIT is to
3703 * make the loop tight, so we overwrite the source with the dest before
3704 * looking at it, and we need to look at the original source
3705 * afterwards. There would also need to be code added to handle
3706 * switching to not in-place in midstream if we run into characters
3707 * that change the length.
3710 s = d = (U8*)SvPV_force_nomg(source, len);
3717 /* The old implementation would copy source into TARG at this point.
3718 This had the side effect that if source was undef, TARG was now
3719 an undefined SV with PADTMP set, and they don't warn inside
3720 sv_2pv_flags(). However, we're now getting the PV direct from
3721 source, which doesn't have PADTMP set, so it would warn. Hence the
3725 s = (const U8*)SvPV_nomg_const(source, len);
3727 if (ckWARN(WARN_UNINITIALIZED))
3728 report_uninit(source);
3734 SvUPGRADE(dest, SVt_PV);
3735 d = (U8*)SvGROW(dest, min);
3736 (void)SvPOK_only(dest);
3741 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
3742 to check DO_UTF8 again here. */
3744 if (DO_UTF8(source)) {
3745 const U8 *const send = s + len;
3746 U8 tmpbuf[UTF8_MAXBYTES+1];
3747 bool tainted = FALSE;
3749 /* All occurrences of these are to be moved to follow any other marks.
3750 * This is context-dependent. We may not be passed enough context to
3751 * move the iota subscript beyond all of them, but we do the best we can
3752 * with what we're given. The result is always better than if we
3753 * hadn't done this. And, the problem would only arise if we are
3754 * passed a character without all its combining marks, which would be
3755 * the caller's mistake. The information this is based on comes from a
3756 * comment in Unicode SpecialCasing.txt, (and the Standard's text
3757 * itself) and so can't be checked properly to see if it ever gets
3758 * revised. But the likelihood of it changing is remote */
3759 bool in_iota_subscript = FALSE;
3765 if (in_iota_subscript && ! is_utf8_mark(s)) {
3767 /* A non-mark. Time to output the iota subscript */
3768 #define GREEK_CAPITAL_LETTER_IOTA 0x0399
3769 #define COMBINING_GREEK_YPOGEGRAMMENI 0x0345
3771 CAT_UNI_TO_UTF8_TWO_BYTE(d, GREEK_CAPITAL_LETTER_IOTA);
3772 in_iota_subscript = FALSE;
3775 /* Then handle the current character. Get the changed case value
3776 * and copy it to the output buffer */
3779 uv = _to_utf8_upper_flags(s, tmpbuf, &ulen,
3780 cBOOL(IN_LOCALE_RUNTIME), &tainted);
3781 if (uv == GREEK_CAPITAL_LETTER_IOTA
3782 && utf8_to_uvchr_buf(s, send, 0) == COMBINING_GREEK_YPOGEGRAMMENI)
3784 in_iota_subscript = TRUE;
3787 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
3788 /* If the eventually required minimum size outgrows the
3789 * available space, we need to grow. */
3790 const UV o = d - (U8*)SvPVX_const(dest);
3792 /* If someone uppercases one million U+03B0s we SvGROW()
3793 * one million times. Or we could try guessing how much to
3794 * allocate without allocating too much. Such is life.
3795 * See corresponding comment in lc code for another option
3798 d = (U8*)SvPVX(dest) + o;
3800 Copy(tmpbuf, d, ulen, U8);
3805 if (in_iota_subscript) {
3806 CAT_UNI_TO_UTF8_TWO_BYTE(d, GREEK_CAPITAL_LETTER_IOTA);
3811 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3817 else { /* Not UTF-8 */
3819 const U8 *const send = s + len;
3821 /* Use locale casing if in locale; regular style if not treating
3822 * latin1 as having case; otherwise the latin1 casing. Do the
3823 * whole thing in a tight loop, for speed, */
3824 if (IN_LOCALE_RUNTIME) {
3827 for (; s < send; d++, s++)
3828 *d = toUPPER_LC(*s);
3830 else if (! IN_UNI_8_BIT) {
3831 for (; s < send; d++, s++) {
3836 for (; s < send; d++, s++) {
3837 *d = toUPPER_LATIN1_MOD(*s);
3838 if (LIKELY(*d != LATIN_SMALL_LETTER_Y_WITH_DIAERESIS)) continue;
3840 /* The mainstream case is the tight loop above. To avoid
3841 * extra tests in that, all three characters that require
3842 * special handling are mapped by the MOD to the one tested
3844 * Use the source to distinguish between the three cases */
3846 if (*s == LATIN_SMALL_LETTER_SHARP_S) {
3848 /* uc() of this requires 2 characters, but they are
3849 * ASCII. If not enough room, grow the string */
3850 if (SvLEN(dest) < ++min) {
3851 const UV o = d - (U8*)SvPVX_const(dest);
3853 d = (U8*)SvPVX(dest) + o;
3855 *d++ = 'S'; *d = 'S'; /* upper case is 'SS' */
3856 continue; /* Back to the tight loop; still in ASCII */
3859 /* The other two special handling characters have their
3860 * upper cases outside the latin1 range, hence need to be
3861 * in UTF-8, so the whole result needs to be in UTF-8. So,
3862 * here we are somewhere in the middle of processing a
3863 * non-UTF-8 string, and realize that we will have to convert
3864 * the whole thing to UTF-8. What to do? There are
3865 * several possibilities. The simplest to code is to
3866 * convert what we have so far, set a flag, and continue on
3867 * in the loop. The flag would be tested each time through
3868 * the loop, and if set, the next character would be
3869 * converted to UTF-8 and stored. But, I (khw) didn't want
3870 * to slow down the mainstream case at all for this fairly
3871 * rare case, so I didn't want to add a test that didn't
3872 * absolutely have to be there in the loop, besides the
3873 * possibility that it would get too complicated for
3874 * optimizers to deal with. Another possibility is to just
3875 * give up, convert the source to UTF-8, and restart the
3876 * function that way. Another possibility is to convert
3877 * both what has already been processed and what is yet to
3878 * come separately to UTF-8, then jump into the loop that
3879 * handles UTF-8. But the most efficient time-wise of the
3880 * ones I could think of is what follows, and turned out to
3881 * not require much extra code. */
3883 /* Convert what we have so far into UTF-8, telling the
3884 * function that we know it should be converted, and to
3885 * allow extra space for what we haven't processed yet.
3886 * Assume the worst case space requirements for converting
3887 * what we haven't processed so far: that it will require
3888 * two bytes for each remaining source character, plus the
3889 * NUL at the end. This may cause the string pointer to
3890 * move, so re-find it. */
3892 len = d - (U8*)SvPVX_const(dest);
3893 SvCUR_set(dest, len);
3894 len = sv_utf8_upgrade_flags_grow(dest,
3895 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
3897 d = (U8*)SvPVX(dest) + len;
3899 /* Now process the remainder of the source, converting to
3900 * upper and UTF-8. If a resulting byte is invariant in
3901 * UTF-8, output it as-is, otherwise convert to UTF-8 and
3902 * append it to the output. */
3903 for (; s < send; s++) {
3904 (void) _to_upper_title_latin1(*s, d, &len, 'S');
3908 /* Here have processed the whole source; no need to continue
3909 * with the outer loop. Each character has been converted
3910 * to upper case and converted to UTF-8 */
3913 } /* End of processing all latin1-style chars */
3914 } /* End of processing all chars */
3915 } /* End of source is not empty */
3917 if (source != dest) {
3918 *d = '\0'; /* Here d points to 1 after last char, add NUL */
3919 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3921 } /* End of isn't utf8 */
3922 if (dest != source && SvTAINTED(source))
3941 if (SvPADTMP(source) && !SvREADONLY(source) && !SvAMAGIC(source)
3942 && SvTEMP(source) && !DO_UTF8(source)) {
3944 /* We can convert in place, as lowercasing anything in the latin1 range
3945 * (or else DO_UTF8 would have been on) doesn't lengthen it */
3947 s = d = (U8*)SvPV_force_nomg(source, len);
3954 /* The old implementation would copy source into TARG at this point.
3955 This had the side effect that if source was undef, TARG was now
3956 an undefined SV with PADTMP set, and they don't warn inside
3957 sv_2pv_flags(). However, we're now getting the PV direct from
3958 source, which doesn't have PADTMP set, so it would warn. Hence the
3962 s = (const U8*)SvPV_nomg_const(source, len);
3964 if (ckWARN(WARN_UNINITIALIZED))
3965 report_uninit(source);
3971 SvUPGRADE(dest, SVt_PV);
3972 d = (U8*)SvGROW(dest, min);
3973 (void)SvPOK_only(dest);
3978 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
3979 to check DO_UTF8 again here. */
3981 if (DO_UTF8(source)) {
3982 const U8 *const send = s + len;
3983 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
3984 bool tainted = FALSE;
3987 const STRLEN u = UTF8SKIP(s);
3990 _to_utf8_lower_flags(s, tmpbuf, &ulen,
3991 cBOOL(IN_LOCALE_RUNTIME), &tainted);
3993 /* Here is where we would do context-sensitive actions. See the
3994 * commit message for this comment for why there isn't any */
3996 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
3998 /* If the eventually required minimum size outgrows the
3999 * available space, we need to grow. */
4000 const UV o = d - (U8*)SvPVX_const(dest);
4002 /* If someone lowercases one million U+0130s we SvGROW() one
4003 * million times. Or we could try guessing how much to
4004 * allocate without allocating too much. Such is life.
4005 * Another option would be to grow an extra byte or two more
4006 * each time we need to grow, which would cut down the million
4007 * to 500K, with little waste */
4009 d = (U8*)SvPVX(dest) + o;
4012 /* Copy the newly lowercased letter to the output buffer we're
4014 Copy(tmpbuf, d, ulen, U8);
4017 } /* End of looping through the source string */
4020 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4025 } else { /* Not utf8 */
4027 const U8 *const send = s + len;
4029 /* Use locale casing if in locale; regular style if not treating
4030 * latin1 as having case; otherwise the latin1 casing. Do the
4031 * whole thing in a tight loop, for speed, */
4032 if (IN_LOCALE_RUNTIME) {
4035 for (; s < send; d++, s++)
4036 *d = toLOWER_LC(*s);
4038 else if (! IN_UNI_8_BIT) {
4039 for (; s < send; d++, s++) {
4044 for (; s < send; d++, s++) {
4045 *d = toLOWER_LATIN1(*s);
4049 if (source != dest) {
4051 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4054 if (dest != source && SvTAINTED(source))
4063 SV * const sv = TOPs;
4065 const char *s = SvPV_const(sv,len);
4067 SvUTF8_off(TARG); /* decontaminate */
4070 SvUPGRADE(TARG, SVt_PV);
4071 SvGROW(TARG, (len * 2) + 1);
4075 STRLEN ulen = UTF8SKIP(s);
4076 bool to_quote = FALSE;
4078 if (UTF8_IS_INVARIANT(*s)) {
4079 if (_isQUOTEMETA(*s)) {
4083 else if (UTF8_IS_DOWNGRADEABLE_START(*s)) {
4085 /* In locale, we quote all non-ASCII Latin1 chars.
4086 * Otherwise use the quoting rules */
4087 if (IN_LOCALE_RUNTIME
4088 || _isQUOTEMETA(TWO_BYTE_UTF8_TO_UNI(*s, *(s + 1))))
4093 else if (is_QUOTEMETA_high(s)) {
4108 else if (IN_UNI_8_BIT) {
4110 if (_isQUOTEMETA(*s))
4116 /* For non UNI_8_BIT (and hence in locale) just quote all \W
4117 * including everything above ASCII */
4119 if (!isWORDCHAR_A(*s))
4125 SvCUR_set(TARG, d - SvPVX_const(TARG));
4126 (void)SvPOK_only_UTF8(TARG);
4129 sv_setpvn(TARG, s, len);
4146 U8 tmpbuf[UTF8_MAXBYTES * UTF8_MAX_FOLD_CHAR_EXPAND + 1];
4147 const bool full_folding = TRUE;
4148 const U8 flags = ( full_folding ? FOLD_FLAGS_FULL : 0 )
4149 | ( IN_LOCALE_RUNTIME ? FOLD_FLAGS_LOCALE : 0 );
4151 /* This is a facsimile of pp_lc, but with a thousand bugs thanks to me.
4152 * You are welcome(?) -Hugmeir
4160 s = (const U8*)SvPV_nomg_const(source, len);
4162 if (ckWARN(WARN_UNINITIALIZED))
4163 report_uninit(source);
4170 SvUPGRADE(dest, SVt_PV);
4171 d = (U8*)SvGROW(dest, min);
4172 (void)SvPOK_only(dest);
4177 if (DO_UTF8(source)) { /* UTF-8 flagged string. */
4178 bool tainted = FALSE;
4180 const STRLEN u = UTF8SKIP(s);
4183 _to_utf8_fold_flags(s, tmpbuf, &ulen, flags, &tainted);
4185 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
4186 const UV o = d - (U8*)SvPVX_const(dest);
4188 d = (U8*)SvPVX(dest) + o;
4191 Copy(tmpbuf, d, ulen, U8);
4200 } /* Unflagged string */
4202 /* For locale, bytes, and nothing, the behavior is supposed to be the
4205 if ( IN_LOCALE_RUNTIME ) { /* Under locale */
4208 for (; s < send; d++, s++)
4209 *d = toLOWER_LC(*s);
4211 else if ( !IN_UNI_8_BIT ) { /* Under nothing, or bytes */
4212 for (; s < send; d++, s++)
4216 /* For ASCII and the Latin-1 range, there's only two troublesome folds,
4217 * \x{DF} (\N{LATIN SMALL LETTER SHARP S}), which under full casefolding
4218 * becomes 'ss', and \x{B5} (\N{MICRO SIGN}), which under any fold becomes
4219 * \x{3BC} (\N{GREEK SMALL LETTER MU}) -- For the rest, the casefold is
4222 for (; s < send; d++, s++) {
4223 if (*s == MICRO_SIGN) {
4224 /* \N{MICRO SIGN}'s casefold is \N{GREEK SMALL LETTER MU}, which
4225 * is outside of the latin-1 range. There's a couple of ways to
4226 * deal with this -- khw discusses them in pp_lc/uc, so go there :)
4227 * What we do here is upgrade what we had already casefolded,
4228 * then enter an inner loop that appends the rest of the characters
4231 len = d - (U8*)SvPVX_const(dest);
4232 SvCUR_set(dest, len);
4233 len = sv_utf8_upgrade_flags_grow(dest,
4234 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
4235 /* The max expansion for latin1
4236 * chars is 1 byte becomes 2 */
4238 d = (U8*)SvPVX(dest) + len;
4240 CAT_UNI_TO_UTF8_TWO_BYTE(d, GREEK_SMALL_LETTER_MU);
4242 for (; s < send; s++) {
4244 UV fc = _to_uni_fold_flags(*s, tmpbuf, &ulen, flags);
4245 if UNI_IS_INVARIANT(fc) {
4246 if ( full_folding && *s == LATIN_SMALL_LETTER_SHARP_S) {
4254 Copy(tmpbuf, d, ulen, U8);
4260 else if (full_folding && *s == LATIN_SMALL_LETTER_SHARP_S) {
4261 /* Under full casefolding, LATIN SMALL LETTER SHARP S becomes "ss",
4262 * which may require growing the SV.
4264 if (SvLEN(dest) < ++min) {
4265 const UV o = d - (U8*)SvPVX_const(dest);
4267 d = (U8*)SvPVX(dest) + o;
4272 else { /* If it's not one of those two, the fold is their lower case */
4273 *d = toLOWER_LATIN1(*s);
4279 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4281 if (SvTAINTED(source))
4291 dVAR; dSP; dMARK; dORIGMARK;
4292 AV *const av = MUTABLE_AV(POPs);
4293 const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
4295 if (SvTYPE(av) == SVt_PVAV) {
4296 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
4297 bool can_preserve = FALSE;
4303 can_preserve = SvCANEXISTDELETE(av);
4306 if (lval && localizing) {
4309 for (svp = MARK + 1; svp <= SP; svp++) {
4310 const I32 elem = SvIV(*svp);
4314 if (max > AvMAX(av))
4318 while (++MARK <= SP) {
4320 I32 elem = SvIV(*MARK);
4321 bool preeminent = TRUE;
4323 if (localizing && can_preserve) {
4324 /* If we can determine whether the element exist,
4325 * Try to preserve the existenceness of a tied array
4326 * element by using EXISTS and DELETE if possible.
4327 * Fallback to FETCH and STORE otherwise. */
4328 preeminent = av_exists(av, elem);
4331 svp = av_fetch(av, elem, lval);
4333 if (!svp || *svp == &PL_sv_undef)
4334 DIE(aTHX_ PL_no_aelem, elem);
4337 save_aelem(av, elem, svp);
4339 SAVEADELETE(av, elem);
4342 *MARK = svp ? *svp : &PL_sv_undef;
4345 if (GIMME != G_ARRAY) {
4347 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
4353 /* Smart dereferencing for keys, values and each */
4365 (SvTYPE(sv) != SVt_PVHV && SvTYPE(sv) != SVt_PVAV)
4370 "Type of argument to %s must be unblessed hashref or arrayref",
4371 PL_op_desc[PL_op->op_type] );
4374 if (PL_op->op_flags & OPf_SPECIAL && SvTYPE(sv) == SVt_PVAV)
4376 "Can't modify %s in %s",
4377 PL_op_desc[PL_op->op_type], PL_op_desc[PL_op->op_next->op_type]
4380 /* Delegate to correct function for op type */
4382 if (PL_op->op_type == OP_RKEYS || PL_op->op_type == OP_RVALUES) {
4383 return (SvTYPE(sv) == SVt_PVHV) ? Perl_do_kv(aTHX) : Perl_pp_akeys(aTHX);
4386 return (SvTYPE(sv) == SVt_PVHV) ? Perl_pp_each(aTHX) : Perl_pp_aeach(aTHX);
4394 AV *array = MUTABLE_AV(POPs);
4395 const I32 gimme = GIMME_V;
4396 IV *iterp = Perl_av_iter_p(aTHX_ array);
4397 const IV current = (*iterp)++;
4399 if (current > av_len(array)) {
4401 if (gimme == G_SCALAR)
4409 if (gimme == G_ARRAY) {
4410 SV **const element = av_fetch(array, current, 0);
4411 PUSHs(element ? *element : &PL_sv_undef);
4420 AV *array = MUTABLE_AV(POPs);
4421 const I32 gimme = GIMME_V;
4423 *Perl_av_iter_p(aTHX_ array) = 0;
4425 if (gimme == G_SCALAR) {
4427 PUSHi(av_len(array) + 1);
4429 else if (gimme == G_ARRAY) {
4430 IV n = Perl_av_len(aTHX_ array);
4435 if (PL_op->op_type == OP_AKEYS || PL_op->op_type == OP_RKEYS) {
4436 for (i = 0; i <= n; i++) {
4441 for (i = 0; i <= n; i++) {
4442 SV *const *const elem = Perl_av_fetch(aTHX_ array, i, 0);
4443 PUSHs(elem ? *elem : &PL_sv_undef);
4450 /* Associative arrays. */
4456 HV * hash = MUTABLE_HV(POPs);
4458 const I32 gimme = GIMME_V;
4461 /* might clobber stack_sp */
4462 entry = hv_iternext(hash);
4467 SV* const sv = hv_iterkeysv(entry);
4468 PUSHs(sv); /* won't clobber stack_sp */
4469 if (gimme == G_ARRAY) {
4472 /* might clobber stack_sp */
4473 val = hv_iterval(hash, entry);
4478 else if (gimme == G_SCALAR)
4485 S_do_delete_local(pTHX)
4489 const I32 gimme = GIMME_V;
4492 const bool sliced = !!(PL_op->op_private & OPpSLICE);
4493 SV *unsliced_keysv = sliced ? NULL : POPs;
4494 SV * const osv = POPs;
4495 SV **mark = sliced ? PL_stack_base + POPMARK : &unsliced_keysv-1;
4497 const bool tied = SvRMAGICAL(osv)
4498 && mg_find((const SV *)osv, PERL_MAGIC_tied);
4499 const bool can_preserve = SvCANEXISTDELETE(osv);
4500 const U32 type = SvTYPE(osv);
4501 SV ** const end = sliced ? SP : &unsliced_keysv;
4503 if (type == SVt_PVHV) { /* hash element */
4504 HV * const hv = MUTABLE_HV(osv);
4505 while (++MARK <= end) {
4506 SV * const keysv = *MARK;
4508 bool preeminent = TRUE;
4510 preeminent = hv_exists_ent(hv, keysv, 0);
4512 HE *he = hv_fetch_ent(hv, keysv, 1, 0);
4519 sv = hv_delete_ent(hv, keysv, 0, 0);
4520 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4523 if (!sv) DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
4524 save_helem_flags(hv, keysv, &sv, SAVEf_KEEPOLDELEM);
4526 *MARK = sv_mortalcopy(sv);
4532 SAVEHDELETE(hv, keysv);
4533 *MARK = &PL_sv_undef;
4537 else if (type == SVt_PVAV) { /* array element */
4538 if (PL_op->op_flags & OPf_SPECIAL) {
4539 AV * const av = MUTABLE_AV(osv);
4540 while (++MARK <= end) {
4541 I32 idx = SvIV(*MARK);
4543 bool preeminent = TRUE;
4545 preeminent = av_exists(av, idx);
4547 SV **svp = av_fetch(av, idx, 1);
4554 sv = av_delete(av, idx, 0);
4555 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4558 save_aelem_flags(av, idx, &sv, SAVEf_KEEPOLDELEM);
4560 *MARK = sv_mortalcopy(sv);
4566 SAVEADELETE(av, idx);
4567 *MARK = &PL_sv_undef;
4572 DIE(aTHX_ "panic: avhv_delete no longer supported");
4575 DIE(aTHX_ "Not a HASH reference");
4577 if (gimme == G_VOID)
4579 else if (gimme == G_SCALAR) {
4584 *++MARK = &PL_sv_undef;
4588 else if (gimme != G_VOID)
4589 PUSHs(unsliced_keysv);
4601 if (PL_op->op_private & OPpLVAL_INTRO)
4602 return do_delete_local();
4605 discard = (gimme == G_VOID) ? G_DISCARD : 0;
4607 if (PL_op->op_private & OPpSLICE) {
4609 HV * const hv = MUTABLE_HV(POPs);
4610 const U32 hvtype = SvTYPE(hv);
4611 if (hvtype == SVt_PVHV) { /* hash element */
4612 while (++MARK <= SP) {
4613 SV * const sv = hv_delete_ent(hv, *MARK, discard, 0);
4614 *MARK = sv ? sv : &PL_sv_undef;
4617 else if (hvtype == SVt_PVAV) { /* array element */
4618 if (PL_op->op_flags & OPf_SPECIAL) {
4619 while (++MARK <= SP) {
4620 SV * const sv = av_delete(MUTABLE_AV(hv), SvIV(*MARK), discard);
4621 *MARK = sv ? sv : &PL_sv_undef;
4626 DIE(aTHX_ "Not a HASH reference");
4629 else if (gimme == G_SCALAR) {
4634 *++MARK = &PL_sv_undef;