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;
983 assert(isGV_with_GP(sv));
989 /* undef *Pkg::meth_name ... */
991 = GvCVu((const GV *)sv) && (stash = GvSTASH((const GV *)sv))
992 && HvENAME_get(stash);
994 if((stash = GvHV((const GV *)sv))) {
995 if(HvENAME_get(stash))
996 SvREFCNT_inc_simple_void_NN(sv_2mortal((SV *)stash));
1000 gp_free(MUTABLE_GV(sv));
1002 GvGP_set(sv, gp_ref(gp));
1003 GvSV(sv) = newSV(0);
1004 GvLINE(sv) = CopLINE(PL_curcop);
1005 GvEGV(sv) = MUTABLE_GV(sv);
1009 mro_package_moved(NULL, stash, (const GV *)sv, 0);
1011 /* undef *Foo::ISA */
1012 if( strEQ(GvNAME((const GV *)sv), "ISA")
1013 && (stash = GvSTASH((const GV *)sv))
1014 && (method_changed || HvENAME(stash)) )
1015 mro_isa_changed_in(stash);
1016 else if(method_changed)
1017 mro_method_changed_in(
1018 GvSTASH((const GV *)sv)
1024 if (SvTYPE(sv) >= SVt_PV && SvPVX_const(sv) && SvLEN(sv)) {
1040 PL_op->op_type == OP_POSTINC || PL_op->op_type == OP_I_POSTINC;
1041 if (SvTYPE(TOPs) >= SVt_PVAV || (isGV_with_GP(TOPs) && !SvFAKE(TOPs)))
1042 Perl_croak_no_modify(aTHX);
1044 TARG = sv_newmortal();
1045 sv_setsv(TARG, TOPs);
1046 if (!SvREADONLY(TOPs) && !SvGMAGICAL(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
1047 && SvIVX(TOPs) != (inc ? IV_MAX : IV_MIN))
1049 SvIV_set(TOPs, SvIVX(TOPs) + (inc ? 1 : -1));
1050 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
1054 else sv_dec_nomg(TOPs);
1056 /* special case for undef: see thread at 2003-03/msg00536.html in archive */
1057 if (inc && !SvOK(TARG))
1063 /* Ordinary operators. */
1067 dVAR; dSP; dATARGET; SV *svl, *svr;
1068 #ifdef PERL_PRESERVE_IVUV
1071 tryAMAGICbin_MG(pow_amg, AMGf_assign|AMGf_numeric);
1074 #ifdef PERL_PRESERVE_IVUV
1075 /* For integer to integer power, we do the calculation by hand wherever
1076 we're sure it is safe; otherwise we call pow() and try to convert to
1077 integer afterwards. */
1078 if (SvIV_please_nomg(svr) && SvIV_please_nomg(svl)) {
1086 const IV iv = SvIVX(svr);
1090 goto float_it; /* Can't do negative powers this way. */
1094 baseuok = SvUOK(svl);
1096 baseuv = SvUVX(svl);
1098 const IV iv = SvIVX(svl);
1101 baseuok = TRUE; /* effectively it's a UV now */
1103 baseuv = -iv; /* abs, baseuok == false records sign */
1106 /* now we have integer ** positive integer. */
1109 /* foo & (foo - 1) is zero only for a power of 2. */
1110 if (!(baseuv & (baseuv - 1))) {
1111 /* We are raising power-of-2 to a positive integer.
1112 The logic here will work for any base (even non-integer
1113 bases) but it can be less accurate than
1114 pow (base,power) or exp (power * log (base)) when the
1115 intermediate values start to spill out of the mantissa.
1116 With powers of 2 we know this can't happen.
1117 And powers of 2 are the favourite thing for perl
1118 programmers to notice ** not doing what they mean. */
1120 NV base = baseuok ? baseuv : -(NV)baseuv;
1125 while (power >>= 1) {
1133 SvIV_please_nomg(svr);
1136 unsigned int highbit = 8 * sizeof(UV);
1137 unsigned int diff = 8 * sizeof(UV);
1138 while (diff >>= 1) {
1140 if (baseuv >> highbit) {
1144 /* we now have baseuv < 2 ** highbit */
1145 if (power * highbit <= 8 * sizeof(UV)) {
1146 /* result will definitely fit in UV, so use UV math
1147 on same algorithm as above */
1150 const bool odd_power = cBOOL(power & 1);
1154 while (power >>= 1) {
1161 if (baseuok || !odd_power)
1162 /* answer is positive */
1164 else if (result <= (UV)IV_MAX)
1165 /* answer negative, fits in IV */
1166 SETi( -(IV)result );
1167 else if (result == (UV)IV_MIN)
1168 /* 2's complement assumption: special case IV_MIN */
1171 /* answer negative, doesn't fit */
1172 SETn( -(NV)result );
1180 NV right = SvNV_nomg(svr);
1181 NV left = SvNV_nomg(svl);
1184 #if defined(USE_LONG_DOUBLE) && defined(HAS_AIX_POWL_NEG_BASE_BUG)
1186 We are building perl with long double support and are on an AIX OS
1187 afflicted with a powl() function that wrongly returns NaNQ for any
1188 negative base. This was reported to IBM as PMR #23047-379 on
1189 03/06/2006. The problem exists in at least the following versions
1190 of AIX and the libm fileset, and no doubt others as well:
1192 AIX 4.3.3-ML10 bos.adt.libm 4.3.3.50
1193 AIX 5.1.0-ML04 bos.adt.libm 5.1.0.29
1194 AIX 5.2.0 bos.adt.libm 5.2.0.85
1196 So, until IBM fixes powl(), we provide the following workaround to
1197 handle the problem ourselves. Our logic is as follows: for
1198 negative bases (left), we use fmod(right, 2) to check if the
1199 exponent is an odd or even integer:
1201 - if odd, powl(left, right) == -powl(-left, right)
1202 - if even, powl(left, right) == powl(-left, right)
1204 If the exponent is not an integer, the result is rightly NaNQ, so
1205 we just return that (as NV_NAN).
1209 NV mod2 = Perl_fmod( right, 2.0 );
1210 if (mod2 == 1.0 || mod2 == -1.0) { /* odd integer */
1211 SETn( -Perl_pow( -left, right) );
1212 } else if (mod2 == 0.0) { /* even integer */
1213 SETn( Perl_pow( -left, right) );
1214 } else { /* fractional power */
1218 SETn( Perl_pow( left, right) );
1221 SETn( Perl_pow( left, right) );
1222 #endif /* HAS_AIX_POWL_NEG_BASE_BUG */
1224 #ifdef PERL_PRESERVE_IVUV
1226 SvIV_please_nomg(svr);
1234 dVAR; dSP; dATARGET; SV *svl, *svr;
1235 tryAMAGICbin_MG(mult_amg, AMGf_assign|AMGf_numeric);
1238 #ifdef PERL_PRESERVE_IVUV
1239 if (SvIV_please_nomg(svr)) {
1240 /* Unless the left argument is integer in range we are going to have to
1241 use NV maths. Hence only attempt to coerce the right argument if
1242 we know the left is integer. */
1243 /* Left operand is defined, so is it IV? */
1244 if (SvIV_please_nomg(svl)) {
1245 bool auvok = SvUOK(svl);
1246 bool buvok = SvUOK(svr);
1247 const UV topmask = (~ (UV)0) << (4 * sizeof (UV));
1248 const UV botmask = ~((~ (UV)0) << (4 * sizeof (UV)));
1257 const IV aiv = SvIVX(svl);
1260 auvok = TRUE; /* effectively it's a UV now */
1262 alow = -aiv; /* abs, auvok == false records sign */
1268 const IV biv = SvIVX(svr);
1271 buvok = TRUE; /* effectively it's a UV now */
1273 blow = -biv; /* abs, buvok == false records sign */
1277 /* If this does sign extension on unsigned it's time for plan B */
1278 ahigh = alow >> (4 * sizeof (UV));
1280 bhigh = blow >> (4 * sizeof (UV));
1282 if (ahigh && bhigh) {
1284 /* eg 32 bit is at least 0x10000 * 0x10000 == 0x100000000
1285 which is overflow. Drop to NVs below. */
1286 } else if (!ahigh && !bhigh) {
1287 /* eg 32 bit is at most 0xFFFF * 0xFFFF == 0xFFFE0001
1288 so the unsigned multiply cannot overflow. */
1289 const UV product = alow * blow;
1290 if (auvok == buvok) {
1291 /* -ve * -ve or +ve * +ve gives a +ve result. */
1295 } else if (product <= (UV)IV_MIN) {
1296 /* 2s complement assumption that (UV)-IV_MIN is correct. */
1297 /* -ve result, which could overflow an IV */
1299 SETi( -(IV)product );
1301 } /* else drop to NVs below. */
1303 /* One operand is large, 1 small */
1306 /* swap the operands */
1308 bhigh = blow; /* bhigh now the temp var for the swap */
1312 /* now, ((ahigh * blow) << half_UV_len) + (alow * blow)
1313 multiplies can't overflow. shift can, add can, -ve can. */
1314 product_middle = ahigh * blow;
1315 if (!(product_middle & topmask)) {
1316 /* OK, (ahigh * blow) won't lose bits when we shift it. */
1318 product_middle <<= (4 * sizeof (UV));
1319 product_low = alow * blow;
1321 /* as for pp_add, UV + something mustn't get smaller.
1322 IIRC ANSI mandates this wrapping *behaviour* for
1323 unsigned whatever the actual representation*/
1324 product_low += product_middle;
1325 if (product_low >= product_middle) {
1326 /* didn't overflow */
1327 if (auvok == buvok) {
1328 /* -ve * -ve or +ve * +ve gives a +ve result. */
1330 SETu( product_low );
1332 } else if (product_low <= (UV)IV_MIN) {
1333 /* 2s complement assumption again */
1334 /* -ve result, which could overflow an IV */
1336 SETi( -(IV)product_low );
1338 } /* else drop to NVs below. */
1340 } /* product_middle too large */
1341 } /* ahigh && bhigh */
1346 NV right = SvNV_nomg(svr);
1347 NV left = SvNV_nomg(svl);
1349 SETn( left * right );
1356 dVAR; dSP; dATARGET; SV *svl, *svr;
1357 tryAMAGICbin_MG(div_amg, AMGf_assign|AMGf_numeric);
1360 /* Only try to do UV divide first
1361 if ((SLOPPYDIVIDE is true) or
1362 (PERL_PRESERVE_IVUV is true and one or both SV is a UV too large
1364 The assumption is that it is better to use floating point divide
1365 whenever possible, only doing integer divide first if we can't be sure.
1366 If NV_PRESERVES_UV is true then we know at compile time that no UV
1367 can be too large to preserve, so don't need to compile the code to
1368 test the size of UVs. */
1371 # define PERL_TRY_UV_DIVIDE
1372 /* ensure that 20./5. == 4. */
1374 # ifdef PERL_PRESERVE_IVUV
1375 # ifndef NV_PRESERVES_UV
1376 # define PERL_TRY_UV_DIVIDE
1381 #ifdef PERL_TRY_UV_DIVIDE
1382 if (SvIV_please_nomg(svr) && SvIV_please_nomg(svl)) {
1383 bool left_non_neg = SvUOK(svl);
1384 bool right_non_neg = SvUOK(svr);
1388 if (right_non_neg) {
1392 const IV biv = SvIVX(svr);
1395 right_non_neg = TRUE; /* effectively it's a UV now */
1401 /* historically undef()/0 gives a "Use of uninitialized value"
1402 warning before dieing, hence this test goes here.
1403 If it were immediately before the second SvIV_please, then
1404 DIE() would be invoked before left was even inspected, so
1405 no inspection would give no warning. */
1407 DIE(aTHX_ "Illegal division by zero");
1413 const IV aiv = SvIVX(svl);
1416 left_non_neg = TRUE; /* effectively it's a UV now */
1425 /* For sloppy divide we always attempt integer division. */
1427 /* Otherwise we only attempt it if either or both operands
1428 would not be preserved by an NV. If both fit in NVs
1429 we fall through to the NV divide code below. However,
1430 as left >= right to ensure integer result here, we know that
1431 we can skip the test on the right operand - right big
1432 enough not to be preserved can't get here unless left is
1435 && (left > ((UV)1 << NV_PRESERVES_UV_BITS))
1438 /* Integer division can't overflow, but it can be imprecise. */
1439 const UV result = left / right;
1440 if (result * right == left) {
1441 SP--; /* result is valid */
1442 if (left_non_neg == right_non_neg) {
1443 /* signs identical, result is positive. */
1447 /* 2s complement assumption */
1448 if (result <= (UV)IV_MIN)
1449 SETi( -(IV)result );
1451 /* It's exact but too negative for IV. */
1452 SETn( -(NV)result );
1455 } /* tried integer divide but it was not an integer result */
1456 } /* else (PERL_ABS(result) < 1.0) or (both UVs in range for NV) */
1457 } /* one operand wasn't SvIOK */
1458 #endif /* PERL_TRY_UV_DIVIDE */
1460 NV right = SvNV_nomg(svr);
1461 NV left = SvNV_nomg(svl);
1462 (void)POPs;(void)POPs;
1463 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
1464 if (! Perl_isnan(right) && right == 0.0)
1468 DIE(aTHX_ "Illegal division by zero");
1469 PUSHn( left / right );
1476 dVAR; dSP; dATARGET;
1477 tryAMAGICbin_MG(modulo_amg, AMGf_assign|AMGf_numeric);
1481 bool left_neg = FALSE;
1482 bool right_neg = FALSE;
1483 bool use_double = FALSE;
1484 bool dright_valid = FALSE;
1487 SV * const svr = TOPs;
1488 SV * const svl = TOPm1s;
1489 if (SvIV_please_nomg(svr)) {
1490 right_neg = !SvUOK(svr);
1494 const IV biv = SvIVX(svr);
1497 right_neg = FALSE; /* effectively it's a UV now */
1504 dright = SvNV_nomg(svr);
1505 right_neg = dright < 0;
1508 if (dright < UV_MAX_P1) {
1509 right = U_V(dright);
1510 dright_valid = TRUE; /* In case we need to use double below. */
1516 /* At this point use_double is only true if right is out of range for
1517 a UV. In range NV has been rounded down to nearest UV and
1518 use_double false. */
1519 if (!use_double && SvIV_please_nomg(svl)) {
1520 left_neg = !SvUOK(svl);
1524 const IV aiv = SvIVX(svl);
1527 left_neg = FALSE; /* effectively it's a UV now */
1534 dleft = SvNV_nomg(svl);
1535 left_neg = dleft < 0;
1539 /* This should be exactly the 5.6 behaviour - if left and right are
1540 both in range for UV then use U_V() rather than floor. */
1542 if (dleft < UV_MAX_P1) {
1543 /* right was in range, so is dleft, so use UVs not double.
1547 /* left is out of range for UV, right was in range, so promote
1548 right (back) to double. */
1550 /* The +0.5 is used in 5.6 even though it is not strictly
1551 consistent with the implicit +0 floor in the U_V()
1552 inside the #if 1. */
1553 dleft = Perl_floor(dleft + 0.5);
1556 dright = Perl_floor(dright + 0.5);
1567 DIE(aTHX_ "Illegal modulus zero");
1569 dans = Perl_fmod(dleft, dright);
1570 if ((left_neg != right_neg) && dans)
1571 dans = dright - dans;
1574 sv_setnv(TARG, dans);
1580 DIE(aTHX_ "Illegal modulus zero");
1583 if ((left_neg != right_neg) && ans)
1586 /* XXX may warn: unary minus operator applied to unsigned type */
1587 /* could change -foo to be (~foo)+1 instead */
1588 if (ans <= ~((UV)IV_MAX)+1)
1589 sv_setiv(TARG, ~ans+1);
1591 sv_setnv(TARG, -(NV)ans);
1594 sv_setuv(TARG, ans);
1603 dVAR; dSP; dATARGET;
1607 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1608 /* TODO: think of some way of doing list-repeat overloading ??? */
1613 tryAMAGICbin_MG(repeat_amg, AMGf_assign);
1619 const UV uv = SvUV_nomg(sv);
1621 count = IV_MAX; /* The best we can do? */
1625 const IV iv = SvIV_nomg(sv);
1632 else if (SvNOKp(sv)) {
1633 const NV nv = SvNV_nomg(sv);
1640 count = SvIV_nomg(sv);
1642 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1644 static const char oom_list_extend[] = "Out of memory during list extend";
1645 const I32 items = SP - MARK;
1646 const I32 max = items * count;
1648 MEM_WRAP_CHECK_1(max, SV*, oom_list_extend);
1649 /* Did the max computation overflow? */
1650 if (items > 0 && max > 0 && (max < items || max < count))
1651 Perl_croak(aTHX_ oom_list_extend);
1656 /* This code was intended to fix 20010809.028:
1659 for (($x =~ /./g) x 2) {
1660 print chop; # "abcdabcd" expected as output.
1663 * but that change (#11635) broke this code:
1665 $x = [("foo")x2]; # only one "foo" ended up in the anonlist.
1667 * I can't think of a better fix that doesn't introduce
1668 * an efficiency hit by copying the SVs. The stack isn't
1669 * refcounted, and mortalisation obviously doesn't
1670 * Do The Right Thing when the stack has more than
1671 * one pointer to the same mortal value.
1675 *SP = sv_2mortal(newSVsv(*SP));
1685 repeatcpy((char*)(MARK + items), (char*)MARK,
1686 items * sizeof(const SV *), count - 1);
1689 else if (count <= 0)
1692 else { /* Note: mark already snarfed by pp_list */
1693 SV * const tmpstr = POPs;
1696 static const char oom_string_extend[] =
1697 "Out of memory during string extend";
1700 sv_setsv_nomg(TARG, tmpstr);
1701 SvPV_force_nomg(TARG, len);
1702 isutf = DO_UTF8(TARG);
1707 const STRLEN max = (UV)count * len;
1708 if (len > MEM_SIZE_MAX / count)
1709 Perl_croak(aTHX_ oom_string_extend);
1710 MEM_WRAP_CHECK_1(max, char, oom_string_extend);
1711 SvGROW(TARG, max + 1);
1712 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
1713 SvCUR_set(TARG, SvCUR(TARG) * count);
1715 *SvEND(TARG) = '\0';
1718 (void)SvPOK_only_UTF8(TARG);
1720 (void)SvPOK_only(TARG);
1722 if (PL_op->op_private & OPpREPEAT_DOLIST) {
1723 /* The parser saw this as a list repeat, and there
1724 are probably several items on the stack. But we're
1725 in scalar context, and there's no pp_list to save us
1726 now. So drop the rest of the items -- robin@kitsite.com
1738 dVAR; dSP; dATARGET; bool useleft; SV *svl, *svr;
1739 tryAMAGICbin_MG(subtr_amg, AMGf_assign|AMGf_numeric);
1742 useleft = USE_LEFT(svl);
1743 #ifdef PERL_PRESERVE_IVUV
1744 /* See comments in pp_add (in pp_hot.c) about Overflow, and how
1745 "bad things" happen if you rely on signed integers wrapping. */
1746 if (SvIV_please_nomg(svr)) {
1747 /* Unless the left argument is integer in range we are going to have to
1748 use NV maths. Hence only attempt to coerce the right argument if
1749 we know the left is integer. */
1756 a_valid = auvok = 1;
1757 /* left operand is undef, treat as zero. */
1759 /* Left operand is defined, so is it IV? */
1760 if (SvIV_please_nomg(svl)) {
1761 if ((auvok = SvUOK(svl)))
1764 const IV aiv = SvIVX(svl);
1767 auvok = 1; /* Now acting as a sign flag. */
1768 } else { /* 2s complement assumption for IV_MIN */
1776 bool result_good = 0;
1779 bool buvok = SvUOK(svr);
1784 const IV biv = SvIVX(svr);
1791 /* ?uvok if value is >= 0. basically, flagged as UV if it's +ve,
1792 else "IV" now, independent of how it came in.
1793 if a, b represents positive, A, B negative, a maps to -A etc
1798 all UV maths. negate result if A negative.
1799 subtract if signs same, add if signs differ. */
1801 if (auvok ^ buvok) {
1810 /* Must get smaller */
1815 if (result <= buv) {
1816 /* result really should be -(auv-buv). as its negation
1817 of true value, need to swap our result flag */
1829 if (result <= (UV)IV_MIN)
1830 SETi( -(IV)result );
1832 /* result valid, but out of range for IV. */
1833 SETn( -(NV)result );
1837 } /* Overflow, drop through to NVs. */
1842 NV value = SvNV_nomg(svr);
1846 /* left operand is undef, treat as zero - value */
1850 SETn( SvNV_nomg(svl) - value );
1857 dVAR; dSP; dATARGET; SV *svl, *svr;
1858 tryAMAGICbin_MG(lshift_amg, AMGf_assign|AMGf_numeric);
1862 const IV shift = SvIV_nomg(svr);
1863 if (PL_op->op_private & HINT_INTEGER) {
1864 const IV i = SvIV_nomg(svl);
1868 const UV u = SvUV_nomg(svl);
1877 dVAR; dSP; dATARGET; SV *svl, *svr;
1878 tryAMAGICbin_MG(rshift_amg, AMGf_assign|AMGf_numeric);
1882 const IV shift = SvIV_nomg(svr);
1883 if (PL_op->op_private & HINT_INTEGER) {
1884 const IV i = SvIV_nomg(svl);
1888 const UV u = SvUV_nomg(svl);
1900 tryAMAGICbin_MG(lt_amg, AMGf_set|AMGf_numeric);
1904 (SvIOK_notUV(left) && SvIOK_notUV(right))
1905 ? (SvIVX(left) < SvIVX(right))
1906 : (do_ncmp(left, right) == -1)
1916 tryAMAGICbin_MG(gt_amg, AMGf_set|AMGf_numeric);
1920 (SvIOK_notUV(left) && SvIOK_notUV(right))
1921 ? (SvIVX(left) > SvIVX(right))
1922 : (do_ncmp(left, right) == 1)
1932 tryAMAGICbin_MG(le_amg, AMGf_set|AMGf_numeric);
1936 (SvIOK_notUV(left) && SvIOK_notUV(right))
1937 ? (SvIVX(left) <= SvIVX(right))
1938 : (do_ncmp(left, right) <= 0)
1948 tryAMAGICbin_MG(ge_amg, AMGf_set|AMGf_numeric);
1952 (SvIOK_notUV(left) && SvIOK_notUV(right))
1953 ? (SvIVX(left) >= SvIVX(right))
1954 : ( (do_ncmp(left, right) & 2) == 0)
1964 tryAMAGICbin_MG(ne_amg, AMGf_set|AMGf_numeric);
1968 (SvIOK_notUV(left) && SvIOK_notUV(right))
1969 ? (SvIVX(left) != SvIVX(right))
1970 : (do_ncmp(left, right) != 0)
1975 /* compare left and right SVs. Returns:
1979 * 2: left or right was a NaN
1982 Perl_do_ncmp(pTHX_ SV* const left, SV * const right)
1986 PERL_ARGS_ASSERT_DO_NCMP;
1987 #ifdef PERL_PRESERVE_IVUV
1988 /* Fortunately it seems NaN isn't IOK */
1989 if (SvIV_please_nomg(right) && SvIV_please_nomg(left)) {
1991 const IV leftiv = SvIVX(left);
1992 if (!SvUOK(right)) {
1993 /* ## IV <=> IV ## */
1994 const IV rightiv = SvIVX(right);
1995 return (leftiv > rightiv) - (leftiv < rightiv);
1997 /* ## IV <=> UV ## */
1999 /* As (b) is a UV, it's >=0, so it must be < */
2002 const UV rightuv = SvUVX(right);
2003 return ((UV)leftiv > rightuv) - ((UV)leftiv < rightuv);
2008 /* ## UV <=> UV ## */
2009 const UV leftuv = SvUVX(left);
2010 const UV rightuv = SvUVX(right);
2011 return (leftuv > rightuv) - (leftuv < rightuv);
2013 /* ## UV <=> IV ## */
2015 const IV rightiv = SvIVX(right);
2017 /* As (a) is a UV, it's >=0, so it cannot be < */
2020 const UV leftuv = SvUVX(left);
2021 return (leftuv > (UV)rightiv) - (leftuv < (UV)rightiv);
2024 assert(0); /* NOTREACHED */
2028 NV const rnv = SvNV_nomg(right);
2029 NV const lnv = SvNV_nomg(left);
2031 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
2032 if (Perl_isnan(lnv) || Perl_isnan(rnv)) {
2035 return (lnv > rnv) - (lnv < rnv);
2054 tryAMAGICbin_MG(ncmp_amg, AMGf_numeric);
2057 value = do_ncmp(left, right);
2072 int amg_type = sle_amg;
2076 switch (PL_op->op_type) {
2095 tryAMAGICbin_MG(amg_type, AMGf_set);
2098 const int cmp = (IN_LOCALE_RUNTIME
2099 ? sv_cmp_locale_flags(left, right, 0)
2100 : sv_cmp_flags(left, right, 0));
2101 SETs(boolSV(cmp * multiplier < rhs));
2109 tryAMAGICbin_MG(seq_amg, AMGf_set);
2112 SETs(boolSV(sv_eq_flags(left, right, 0)));
2120 tryAMAGICbin_MG(sne_amg, AMGf_set);
2123 SETs(boolSV(!sv_eq_flags(left, right, 0)));
2131 tryAMAGICbin_MG(scmp_amg, 0);
2134 const int cmp = (IN_LOCALE_RUNTIME
2135 ? sv_cmp_locale_flags(left, right, 0)
2136 : sv_cmp_flags(left, right, 0));
2144 dVAR; dSP; dATARGET;
2145 tryAMAGICbin_MG(band_amg, AMGf_assign);
2148 if (SvNIOKp(left) || SvNIOKp(right)) {
2149 const bool left_ro_nonnum = !SvNIOKp(left) && SvREADONLY(left);
2150 const bool right_ro_nonnum = !SvNIOKp(right) && SvREADONLY(right);
2151 if (PL_op->op_private & HINT_INTEGER) {
2152 const IV i = SvIV_nomg(left) & SvIV_nomg(right);
2156 const UV u = SvUV_nomg(left) & SvUV_nomg(right);
2159 if (left_ro_nonnum && left != TARG) SvNIOK_off(left);
2160 if (right_ro_nonnum) SvNIOK_off(right);
2163 do_vop(PL_op->op_type, TARG, left, right);
2172 dVAR; dSP; dATARGET;
2173 const int op_type = PL_op->op_type;
2175 tryAMAGICbin_MG((op_type == OP_BIT_OR ? bor_amg : bxor_amg), AMGf_assign);
2178 if (SvNIOKp(left) || SvNIOKp(right)) {
2179 const bool left_ro_nonnum = !SvNIOKp(left) && SvREADONLY(left);
2180 const bool right_ro_nonnum = !SvNIOKp(right) && SvREADONLY(right);
2181 if (PL_op->op_private & HINT_INTEGER) {
2182 const IV l = (USE_LEFT(left) ? SvIV_nomg(left) : 0);
2183 const IV r = SvIV_nomg(right);
2184 const IV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2188 const UV l = (USE_LEFT(left) ? SvUV_nomg(left) : 0);
2189 const UV r = SvUV_nomg(right);
2190 const UV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2193 if (left_ro_nonnum && left != TARG) SvNIOK_off(left);
2194 if (right_ro_nonnum) SvNIOK_off(right);
2197 do_vop(op_type, TARG, left, right);
2204 PERL_STATIC_INLINE bool
2205 S_negate_string(pTHX)
2210 SV * const sv = TOPs;
2211 if (!SvPOKp(sv) || SvNIOK(sv) || (!SvPOK(sv) && SvNIOKp(sv)))
2213 s = SvPV_nomg_const(sv, len);
2214 if (isIDFIRST(*s)) {
2215 sv_setpvs(TARG, "-");
2218 else if (*s == '+' || (*s == '-' && !looks_like_number(sv))) {
2219 sv_setsv_nomg(TARG, sv);
2220 *SvPV_force_nomg(TARG, len) = *s == '-' ? '+' : '-';
2230 tryAMAGICun_MG(neg_amg, AMGf_numeric);
2231 if (S_negate_string(aTHX)) return NORMAL;
2233 SV * const sv = TOPs;
2236 /* It's publicly an integer */
2239 if (SvIVX(sv) == IV_MIN) {
2240 /* 2s complement assumption. */
2241 SETi(SvIVX(sv)); /* special case: -((UV)IV_MAX+1) == IV_MIN */
2244 else if (SvUVX(sv) <= IV_MAX) {
2249 else if (SvIVX(sv) != IV_MIN) {
2253 #ifdef PERL_PRESERVE_IVUV
2260 if (SvNIOKp(sv) && (SvNIOK(sv) || !SvPOK(sv)))
2261 SETn(-SvNV_nomg(sv));
2262 else if (SvPOKp(sv) && SvIV_please_nomg(sv))
2263 goto oops_its_an_int;
2265 SETn(-SvNV_nomg(sv));
2273 tryAMAGICun_MG(not_amg, AMGf_set);
2274 *PL_stack_sp = boolSV(!SvTRUE_nomg(*PL_stack_sp));
2281 tryAMAGICun_MG(compl_amg, AMGf_numeric);
2285 if (PL_op->op_private & HINT_INTEGER) {
2286 const IV i = ~SvIV_nomg(sv);
2290 const UV u = ~SvUV_nomg(sv);
2299 (void)SvPV_nomg_const(sv,len); /* force check for uninit var */
2300 sv_setsv_nomg(TARG, sv);
2301 tmps = (U8*)SvPV_force_nomg(TARG, len);
2304 /* Calculate exact length, let's not estimate. */
2309 U8 * const send = tmps + len;
2310 U8 * const origtmps = tmps;
2311 const UV utf8flags = UTF8_ALLOW_ANYUV;
2313 while (tmps < send) {
2314 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2316 targlen += UNISKIP(~c);
2322 /* Now rewind strings and write them. */
2329 Newx(result, targlen + 1, U8);
2331 while (tmps < send) {
2332 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2334 p = uvchr_to_utf8_flags(p, ~c, UNICODE_ALLOW_ANY);
2337 sv_usepvn_flags(TARG, (char*)result, targlen,
2338 SV_HAS_TRAILING_NUL);
2345 Newx(result, nchar + 1, U8);
2347 while (tmps < send) {
2348 const U8 c = (U8)utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2353 sv_usepvn_flags(TARG, (char*)result, nchar, SV_HAS_TRAILING_NUL);
2362 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
2365 for ( ; anum >= (I32)sizeof(long); anum -= (I32)sizeof(long), tmpl++)
2370 for ( ; anum > 0; anum--, tmps++)
2378 /* integer versions of some of the above */
2382 dVAR; dSP; dATARGET;
2383 tryAMAGICbin_MG(mult_amg, AMGf_assign);
2386 SETi( left * right );
2394 dVAR; dSP; dATARGET;
2395 tryAMAGICbin_MG(div_amg, AMGf_assign);
2398 IV value = SvIV_nomg(right);
2400 DIE(aTHX_ "Illegal division by zero");
2401 num = SvIV_nomg(left);
2403 /* avoid FPE_INTOVF on some platforms when num is IV_MIN */
2407 value = num / value;
2413 #if defined(__GLIBC__) && IVSIZE == 8 && !defined(PERL_DEBUG_READONLY_OPS)
2420 /* This is the vanilla old i_modulo. */
2421 dVAR; dSP; dATARGET;
2422 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2426 DIE(aTHX_ "Illegal modulus zero");
2427 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2431 SETi( left % right );
2436 #if defined(__GLIBC__) && IVSIZE == 8 && !defined(PERL_DEBUG_READONLY_OPS)
2441 /* This is the i_modulo with the workaround for the _moddi3 bug
2442 * in (at least) glibc 2.2.5 (the PERL_ABS() the workaround).
2443 * See below for pp_i_modulo. */
2444 dVAR; dSP; dATARGET;
2445 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2449 DIE(aTHX_ "Illegal modulus zero");
2450 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2454 SETi( left % PERL_ABS(right) );
2461 dVAR; dSP; dATARGET;
2462 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2466 DIE(aTHX_ "Illegal modulus zero");
2467 /* The assumption is to use hereafter the old vanilla version... */
2469 PL_ppaddr[OP_I_MODULO] =
2471 /* .. but if we have glibc, we might have a buggy _moddi3
2472 * (at least glicb 2.2.5 is known to have this bug), in other
2473 * words our integer modulus with negative quad as the second
2474 * argument might be broken. Test for this and re-patch the
2475 * opcode dispatch table if that is the case, remembering to
2476 * also apply the workaround so that this first round works
2477 * right, too. See [perl #9402] for more information. */
2481 /* Cannot do this check with inlined IV constants since
2482 * that seems to work correctly even with the buggy glibc. */
2484 /* Yikes, we have the bug.
2485 * Patch in the workaround version. */
2487 PL_ppaddr[OP_I_MODULO] =
2488 &Perl_pp_i_modulo_1;
2489 /* Make certain we work right this time, too. */
2490 right = PERL_ABS(right);
2493 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2497 SETi( left % right );
2505 dVAR; dSP; dATARGET;
2506 tryAMAGICbin_MG(add_amg, AMGf_assign);
2508 dPOPTOPiirl_ul_nomg;
2509 SETi( left + right );
2516 dVAR; dSP; dATARGET;
2517 tryAMAGICbin_MG(subtr_amg, AMGf_assign);
2519 dPOPTOPiirl_ul_nomg;
2520 SETi( left - right );
2528 tryAMAGICbin_MG(lt_amg, AMGf_set);
2531 SETs(boolSV(left < right));
2539 tryAMAGICbin_MG(gt_amg, AMGf_set);
2542 SETs(boolSV(left > right));
2550 tryAMAGICbin_MG(le_amg, AMGf_set);
2553 SETs(boolSV(left <= right));
2561 tryAMAGICbin_MG(ge_amg, AMGf_set);
2564 SETs(boolSV(left >= right));
2572 tryAMAGICbin_MG(eq_amg, AMGf_set);
2575 SETs(boolSV(left == right));
2583 tryAMAGICbin_MG(ne_amg, AMGf_set);
2586 SETs(boolSV(left != right));
2594 tryAMAGICbin_MG(ncmp_amg, 0);
2601 else if (left < right)
2613 tryAMAGICun_MG(neg_amg, 0);
2614 if (S_negate_string(aTHX)) return NORMAL;
2616 SV * const sv = TOPs;
2617 IV const i = SvIV_nomg(sv);
2623 /* High falutin' math. */
2628 tryAMAGICbin_MG(atan2_amg, 0);
2631 SETn(Perl_atan2(left, right));
2639 int amg_type = sin_amg;
2640 const char *neg_report = NULL;
2641 NV (*func)(NV) = Perl_sin;
2642 const int op_type = PL_op->op_type;
2659 amg_type = sqrt_amg;
2661 neg_report = "sqrt";
2666 tryAMAGICun_MG(amg_type, 0);
2668 SV * const arg = POPs;
2669 const NV value = SvNV_nomg(arg);
2671 if (op_type == OP_LOG ? (value <= 0.0) : (value < 0.0)) {
2672 SET_NUMERIC_STANDARD();
2673 /* diag_listed_as: Can't take log of %g */
2674 DIE(aTHX_ "Can't take %s of %"NVgf, neg_report, value);
2677 XPUSHn(func(value));
2682 /* Support Configure command-line overrides for rand() functions.
2683 After 5.005, perhaps we should replace this by Configure support
2684 for drand48(), random(), or rand(). For 5.005, though, maintain
2685 compatibility by calling rand() but allow the user to override it.
2686 See INSTALL for details. --Andy Dougherty 15 July 1998
2688 /* Now it's after 5.005, and Configure supports drand48() and random(),
2689 in addition to rand(). So the overrides should not be needed any more.
2690 --Jarkko Hietaniemi 27 September 1998
2693 #ifndef HAS_DRAND48_PROTO
2694 extern double drand48 (void);
2704 value = 1.0; (void)POPs;
2710 if (!PL_srand_called) {
2711 (void)seedDrand01((Rand_seed_t)seed());
2712 PL_srand_called = TRUE;
2724 if (MAXARG >= 1 && (TOPs || POPs)) {
2731 pv = SvPV(top, len);
2732 flags = grok_number(pv, len, &anum);
2734 if (!(flags & IS_NUMBER_IN_UV)) {
2735 Perl_ck_warner_d(aTHX_ packWARN(WARN_OVERFLOW),
2736 "Integer overflow in srand");
2744 (void)seedDrand01((Rand_seed_t)anum);
2745 PL_srand_called = TRUE;
2749 /* Historically srand always returned true. We can avoid breaking
2751 sv_setpvs(TARG, "0 but true");
2760 tryAMAGICun_MG(int_amg, AMGf_numeric);
2762 SV * const sv = TOPs;
2763 const IV iv = SvIV_nomg(sv);
2764 /* XXX it's arguable that compiler casting to IV might be subtly
2765 different from modf (for numbers inside (IV_MIN,UV_MAX)) in which
2766 else preferring IV has introduced a subtle behaviour change bug. OTOH
2767 relying on floating point to be accurate is a bug. */
2772 else if (SvIOK(sv)) {
2774 SETu(SvUV_nomg(sv));
2779 const NV value = SvNV_nomg(sv);
2781 if (value < (NV)UV_MAX + 0.5) {
2784 SETn(Perl_floor(value));
2788 if (value > (NV)IV_MIN - 0.5) {
2791 SETn(Perl_ceil(value));
2802 tryAMAGICun_MG(abs_amg, AMGf_numeric);
2804 SV * const sv = TOPs;
2805 /* This will cache the NV value if string isn't actually integer */
2806 const IV iv = SvIV_nomg(sv);
2811 else if (SvIOK(sv)) {
2812 /* IVX is precise */
2814 SETu(SvUV_nomg(sv)); /* force it to be numeric only */
2822 /* 2s complement assumption. Also, not really needed as
2823 IV_MIN and -IV_MIN should both be %100...00 and NV-able */
2829 const NV value = SvNV_nomg(sv);
2843 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES;
2847 SV* const sv = POPs;
2849 tmps = (SvPV_const(sv, len));
2851 /* If Unicode, try to downgrade
2852 * If not possible, croak. */
2853 SV* const tsv = sv_2mortal(newSVsv(sv));
2856 sv_utf8_downgrade(tsv, FALSE);
2857 tmps = SvPV_const(tsv, len);
2859 if (PL_op->op_type == OP_HEX)
2862 while (*tmps && len && isSPACE(*tmps))
2866 if (*tmps == 'x' || *tmps == 'X') {
2868 result_uv = grok_hex (tmps, &len, &flags, &result_nv);
2870 else if (*tmps == 'b' || *tmps == 'B')
2871 result_uv = grok_bin (tmps, &len, &flags, &result_nv);
2873 result_uv = grok_oct (tmps, &len, &flags, &result_nv);
2875 if (flags & PERL_SCAN_GREATER_THAN_UV_MAX) {
2889 SV * const sv = TOPs;
2894 SETi(sv_len_utf8_nomg(sv));
2898 (void)SvPV_nomg_const(sv,len);
2902 if (!SvPADTMP(TARG)) {
2903 sv_setsv_nomg(TARG, &PL_sv_undef);
2911 /* Returns false if substring is completely outside original string.
2912 No length is indicated by len_iv = 0 and len_is_uv = 0. len_is_uv must
2913 always be true for an explicit 0.
2916 Perl_translate_substr_offsets(pTHX_ STRLEN curlen, IV pos1_iv,
2917 bool pos1_is_uv, IV len_iv,
2918 bool len_is_uv, STRLEN *posp,
2924 PERL_ARGS_ASSERT_TRANSLATE_SUBSTR_OFFSETS;
2926 if (!pos1_is_uv && pos1_iv < 0 && curlen) {
2927 pos1_is_uv = curlen-1 > ~(UV)pos1_iv;
2930 if ((pos1_is_uv || pos1_iv > 0) && (UV)pos1_iv > curlen)
2933 if (len_iv || len_is_uv) {
2934 if (!len_is_uv && len_iv < 0) {
2935 pos2_iv = curlen + len_iv;
2937 pos2_is_uv = curlen-1 > ~(UV)len_iv;
2940 } else { /* len_iv >= 0 */
2941 if (!pos1_is_uv && pos1_iv < 0) {
2942 pos2_iv = pos1_iv + len_iv;
2943 pos2_is_uv = (UV)len_iv > (UV)IV_MAX;
2945 if ((UV)len_iv > curlen-(UV)pos1_iv)
2948 pos2_iv = pos1_iv+len_iv;
2958 if (!pos2_is_uv && pos2_iv < 0) {
2959 if (!pos1_is_uv && pos1_iv < 0)
2963 else if (!pos1_is_uv && pos1_iv < 0)
2966 if ((UV)pos2_iv < (UV)pos1_iv)
2968 if ((UV)pos2_iv > curlen)
2971 /* pos1_iv and pos2_iv both in 0..curlen, so the cast is safe */
2972 *posp = (STRLEN)( (UV)pos1_iv );
2973 *lenp = (STRLEN)( (UV)pos2_iv - (UV)pos1_iv );
2990 I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
2991 const bool rvalue = (GIMME_V != G_VOID);
2994 const char *repl = NULL;
2996 int num_args = PL_op->op_private & 7;
2997 bool repl_need_utf8_upgrade = FALSE;
3001 if(!(repl_sv = POPs)) num_args--;
3003 if ((len_sv = POPs)) {
3004 len_iv = SvIV(len_sv);
3005 len_is_uv = len_iv ? SvIOK_UV(len_sv) : 1;
3010 pos1_iv = SvIV(pos_sv);
3011 pos1_is_uv = SvIOK_UV(pos_sv);
3013 if (PL_op->op_private & OPpSUBSTR_REPL_FIRST) {
3018 if (lvalue && !repl_sv) {
3020 ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
3021 sv_magic(ret, NULL, PERL_MAGIC_substr, NULL, 0);
3023 LvTARG(ret) = SvREFCNT_inc_simple(sv);
3025 pos1_is_uv || pos1_iv >= 0
3026 ? (STRLEN)(UV)pos1_iv
3027 : (LvFLAGS(ret) |= 1, (STRLEN)(UV)-pos1_iv);
3029 len_is_uv || len_iv > 0
3030 ? (STRLEN)(UV)len_iv
3031 : (LvFLAGS(ret) |= 2, (STRLEN)(UV)-len_iv);
3034 PUSHs(ret); /* avoid SvSETMAGIC here */
3038 repl = SvPV_const(repl_sv, repl_len);
3041 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR),
3042 "Attempt to use reference as lvalue in substr"
3044 tmps = SvPV_force_nomg(sv, curlen);
3045 if (DO_UTF8(repl_sv) && repl_len) {
3047 sv_utf8_upgrade_nomg(sv);
3051 else if (DO_UTF8(sv))
3052 repl_need_utf8_upgrade = TRUE;
3054 else tmps = SvPV_const(sv, curlen);
3056 utf8_curlen = sv_or_pv_len_utf8(sv, tmps, curlen);
3057 if (utf8_curlen == curlen)
3060 curlen = utf8_curlen;
3066 STRLEN pos, len, byte_len, byte_pos;
3068 if (!translate_substr_offsets(
3069 curlen, pos1_iv, pos1_is_uv, len_iv, len_is_uv, &pos, &len
3073 byte_pos = utf8_curlen
3074 ? sv_or_pv_pos_u2b(sv, tmps, pos, &byte_len) : pos;
3079 SvTAINTED_off(TARG); /* decontaminate */
3080 SvUTF8_off(TARG); /* decontaminate */
3081 sv_setpvn(TARG, tmps, byte_len);
3082 #ifdef USE_LOCALE_COLLATE
3083 sv_unmagic(TARG, PERL_MAGIC_collxfrm);
3090 SV* repl_sv_copy = NULL;
3092 if (repl_need_utf8_upgrade) {
3093 repl_sv_copy = newSVsv(repl_sv);
3094 sv_utf8_upgrade(repl_sv_copy);
3095 repl = SvPV_const(repl_sv_copy, repl_len);
3099 sv_insert_flags(sv, byte_pos, byte_len, repl, repl_len, 0);
3100 SvREFCNT_dec(repl_sv_copy);
3112 Perl_croak(aTHX_ "substr outside of string");
3113 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR), "substr outside of string");
3120 const IV size = POPi;
3121 const IV offset = POPi;
3122 SV * const src = POPs;
3123 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
3126 if (lvalue) { /* it's an lvalue! */
3127 ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
3128 sv_magic(ret, NULL, PERL_MAGIC_vec, NULL, 0);
3130 LvTARG(ret) = SvREFCNT_inc_simple(src);
3131 LvTARGOFF(ret) = offset;
3132 LvTARGLEN(ret) = size;
3136 SvTAINTED_off(TARG); /* decontaminate */
3140 sv_setuv(ret, do_vecget(src, offset, size));
3156 const char *little_p;
3159 const bool is_index = PL_op->op_type == OP_INDEX;
3160 const bool threeargs = MAXARG >= 3 && (TOPs || ((void)POPs,0));
3166 big_p = SvPV_const(big, biglen);
3167 little_p = SvPV_const(little, llen);
3169 big_utf8 = DO_UTF8(big);
3170 little_utf8 = DO_UTF8(little);
3171 if (big_utf8 ^ little_utf8) {
3172 /* One needs to be upgraded. */
3173 if (little_utf8 && !PL_encoding) {
3174 /* Well, maybe instead we might be able to downgrade the small
3176 char * const pv = (char*)bytes_from_utf8((U8 *)little_p, &llen,
3179 /* If the large string is ISO-8859-1, and it's not possible to
3180 convert the small string to ISO-8859-1, then there is no
3181 way that it could be found anywhere by index. */
3186 /* At this point, pv is a malloc()ed string. So donate it to temp
3187 to ensure it will get free()d */
3188 little = temp = newSV(0);
3189 sv_usepvn(temp, pv, llen);
3190 little_p = SvPVX(little);
3193 ? newSVpvn(big_p, biglen) : newSVpvn(little_p, llen);
3196 sv_recode_to_utf8(temp, PL_encoding);
3198 sv_utf8_upgrade(temp);
3203 big_p = SvPV_const(big, biglen);
3206 little_p = SvPV_const(little, llen);
3210 if (SvGAMAGIC(big)) {
3211 /* Life just becomes a lot easier if I use a temporary here.
3212 Otherwise I need to avoid calls to sv_pos_u2b(), which (dangerously)
3213 will trigger magic and overloading again, as will fbm_instr()
3215 big = newSVpvn_flags(big_p, biglen,
3216 SVs_TEMP | (big_utf8 ? SVf_UTF8 : 0));
3219 if (SvGAMAGIC(little) || (is_index && !SvOK(little))) {
3220 /* index && SvOK() is a hack. fbm_instr() calls SvPV_const, which will
3221 warn on undef, and we've already triggered a warning with the
3222 SvPV_const some lines above. We can't remove that, as we need to
3223 call some SvPV to trigger overloading early and find out if the
3225 This is all getting to messy. The API isn't quite clean enough,
3226 because data access has side effects.
3228 little = newSVpvn_flags(little_p, llen,
3229 SVs_TEMP | (little_utf8 ? SVf_UTF8 : 0));
3230 little_p = SvPVX(little);
3234 offset = is_index ? 0 : biglen;
3236 if (big_utf8 && offset > 0)
3237 sv_pos_u2b(big, &offset, 0);
3243 else if (offset > (I32)biglen)
3245 if (!(little_p = is_index
3246 ? fbm_instr((unsigned char*)big_p + offset,
3247 (unsigned char*)big_p + biglen, little, 0)
3248 : rninstr(big_p, big_p + offset,
3249 little_p, little_p + llen)))
3252 retval = little_p - big_p;
3253 if (retval > 0 && big_utf8)
3254 sv_pos_b2u(big, &retval);
3264 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
3265 SvTAINTED_off(TARG);
3266 do_sprintf(TARG, SP-MARK, MARK+1);
3267 TAINT_IF(SvTAINTED(TARG));
3279 const U8 *s = (U8*)SvPV_const(argsv, len);
3281 if (PL_encoding && SvPOK(argsv) && !DO_UTF8(argsv)) {
3282 SV * const tmpsv = sv_2mortal(newSVsv(argsv));
3283 s = (U8*)sv_recode_to_utf8(tmpsv, PL_encoding);
3287 XPUSHu(DO_UTF8(argsv) ?
3288 utf8n_to_uvchr(s, UTF8_MAXBYTES, 0, UTF8_ALLOW_ANYUV) :
3302 if (!IN_BYTES /* under bytes, chr(-1) eq chr(0xff), etc. */
3303 && ((SvIOKp(top) && !SvIsUV(top) && SvIV_nomg(top) < 0)
3305 ((SvNOKp(top) || (SvOK(top) && !SvIsUV(top)))
3306 && SvNV_nomg(top) < 0.0))) {
3307 if (ckWARN(WARN_UTF8)) {
3308 if (SvGMAGICAL(top)) {
3309 SV *top2 = sv_newmortal();
3310 sv_setsv_nomg(top2, top);
3313 Perl_warner(aTHX_ packWARN(WARN_UTF8),
3314 "Invalid negative number (%"SVf") in chr", top);
3316 value = UNICODE_REPLACEMENT;
3318 value = SvUV_nomg(top);
3321 SvUPGRADE(TARG,SVt_PV);
3323 if (value > 255 && !IN_BYTES) {
3324 SvGROW(TARG, (STRLEN)UNISKIP(value)+1);
3325 tmps = (char*)uvchr_to_utf8_flags((U8*)SvPVX(TARG), value, 0);
3326 SvCUR_set(TARG, tmps - SvPVX_const(TARG));
3328 (void)SvPOK_only(TARG);
3337 *tmps++ = (char)value;
3339 (void)SvPOK_only(TARG);
3341 if (PL_encoding && !IN_BYTES) {
3342 sv_recode_to_utf8(TARG, PL_encoding);
3344 if (SvCUR(TARG) == 0
3345 || ! is_utf8_string((U8*)tmps, SvCUR(TARG))
3346 || UTF8_IS_REPLACEMENT((U8*) tmps, (U8*) tmps + SvCUR(TARG)))
3351 *tmps++ = (char)value;
3367 const char *tmps = SvPV_const(left, len);
3369 if (DO_UTF8(left)) {
3370 /* If Unicode, try to downgrade.
3371 * If not possible, croak.
3372 * Yes, we made this up. */
3373 SV* const tsv = sv_2mortal(newSVsv(left));
3376 sv_utf8_downgrade(tsv, FALSE);
3377 tmps = SvPV_const(tsv, len);
3379 # ifdef USE_ITHREADS
3381 if (!PL_reentrant_buffer->_crypt_struct_buffer) {
3382 /* This should be threadsafe because in ithreads there is only
3383 * one thread per interpreter. If this would not be true,
3384 * we would need a mutex to protect this malloc. */
3385 PL_reentrant_buffer->_crypt_struct_buffer =
3386 (struct crypt_data *)safemalloc(sizeof(struct crypt_data));
3387 #if defined(__GLIBC__) || defined(__EMX__)
3388 if (PL_reentrant_buffer->_crypt_struct_buffer) {
3389 PL_reentrant_buffer->_crypt_struct_buffer->initialized = 0;
3390 /* work around glibc-2.2.5 bug */
3391 PL_reentrant_buffer->_crypt_struct_buffer->current_saltbits = 0;
3395 # endif /* HAS_CRYPT_R */
3396 # endif /* USE_ITHREADS */
3398 sv_setpv(TARG, fcrypt(tmps, SvPV_nolen_const(right)));
3400 sv_setpv(TARG, PerlProc_crypt(tmps, SvPV_nolen_const(right)));
3406 "The crypt() function is unimplemented due to excessive paranoia.");
3410 /* Generally UTF-8 and UTF-EBCDIC are indistinguishable at this level. So
3411 * most comments below say UTF-8, when in fact they mean UTF-EBCDIC as well */
3413 /* Generates code to store a unicode codepoint c that is known to occupy
3414 * exactly two UTF-8 and UTF-EBCDIC bytes; it is stored into p and p+1,
3415 * and p is advanced to point to the next available byte after the two bytes */
3416 #define CAT_UNI_TO_UTF8_TWO_BYTE(p, c) \
3418 *(p)++ = UTF8_TWO_BYTE_HI(c); \
3419 *((p)++) = UTF8_TWO_BYTE_LO(c); \
3424 /* Actually is both lcfirst() and ucfirst(). Only the first character
3425 * changes. This means that possibly we can change in-place, ie., just
3426 * take the source and change that one character and store it back, but not
3427 * if read-only etc, or if the length changes */
3432 STRLEN slen; /* slen is the byte length of the whole SV. */
3435 bool inplace; /* ? Convert first char only, in-place */
3436 bool doing_utf8 = FALSE; /* ? using utf8 */
3437 bool convert_source_to_utf8 = FALSE; /* ? need to convert */
3438 const int op_type = PL_op->op_type;
3441 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
3442 STRLEN ulen; /* ulen is the byte length of the original Unicode character
3443 * stored as UTF-8 at s. */
3444 STRLEN tculen; /* tculen is the byte length of the freshly titlecased (or
3445 * lowercased) character stored in tmpbuf. May be either
3446 * UTF-8 or not, but in either case is the number of bytes */
3447 bool tainted = FALSE;
3451 s = (const U8*)SvPV_nomg_const(source, slen);
3453 if (ckWARN(WARN_UNINITIALIZED))
3454 report_uninit(source);
3459 /* We may be able to get away with changing only the first character, in
3460 * place, but not if read-only, etc. Later we may discover more reasons to
3461 * not convert in-place. */
3462 inplace = SvPADTMP(source) && !SvREADONLY(source) && SvTEMP(source);
3464 /* First calculate what the changed first character should be. This affects
3465 * whether we can just swap it out, leaving the rest of the string unchanged,
3466 * or even if have to convert the dest to UTF-8 when the source isn't */
3468 if (! slen) { /* If empty */
3469 need = 1; /* still need a trailing NUL */
3472 else if (DO_UTF8(source)) { /* Is the source utf8? */
3475 if (op_type == OP_UCFIRST) {
3476 _to_utf8_title_flags(s, tmpbuf, &tculen,
3477 cBOOL(IN_LOCALE_RUNTIME), &tainted);
3480 _to_utf8_lower_flags(s, tmpbuf, &tculen,
3481 cBOOL(IN_LOCALE_RUNTIME), &tainted);
3484 /* we can't do in-place if the length changes. */
3485 if (ulen != tculen) inplace = FALSE;
3486 need = slen + 1 - ulen + tculen;
3488 else { /* Non-zero length, non-UTF-8, Need to consider locale and if
3489 * latin1 is treated as caseless. Note that a locale takes
3491 ulen = 1; /* Original character is 1 byte */
3492 tculen = 1; /* Most characters will require one byte, but this will
3493 * need to be overridden for the tricky ones */
3496 if (op_type == OP_LCFIRST) {
3498 /* lower case the first letter: no trickiness for any character */
3499 *tmpbuf = (IN_LOCALE_RUNTIME) ? toLOWER_LC(*s) :
3500 ((IN_UNI_8_BIT) ? toLOWER_LATIN1(*s) : toLOWER(*s));
3503 else if (IN_LOCALE_RUNTIME) {
3504 *tmpbuf = toUPPER_LC(*s); /* This would be a bug if any locales
3505 * have upper and title case different
3508 else if (! IN_UNI_8_BIT) {
3509 *tmpbuf = toUPPER(*s); /* Returns caseless for non-ascii, or
3510 * on EBCDIC machines whatever the
3511 * native function does */
3513 else { /* is ucfirst non-UTF-8, not in locale, and cased latin1 */
3514 UV title_ord = _to_upper_title_latin1(*s, tmpbuf, &tculen, 's');
3516 assert(tculen == 2);
3518 /* If the result is an upper Latin1-range character, it can
3519 * still be represented in one byte, which is its ordinal */
3520 if (UTF8_IS_DOWNGRADEABLE_START(*tmpbuf)) {
3521 *tmpbuf = (U8) title_ord;
3525 /* Otherwise it became more than one ASCII character (in
3526 * the case of LATIN_SMALL_LETTER_SHARP_S) or changed to
3527 * beyond Latin1, so the number of bytes changed, so can't
3528 * replace just the first character in place. */
3531 /* If the result won't fit in a byte, the entire result will
3532 * have to be in UTF-8. Assume worst case sizing in
3533 * conversion. (all latin1 characters occupy at most two bytes
3535 if (title_ord > 255) {
3537 convert_source_to_utf8 = TRUE;
3538 need = slen * 2 + 1;
3540 /* The (converted) UTF-8 and UTF-EBCDIC lengths of all
3541 * (both) characters whose title case is above 255 is
3545 else { /* LATIN_SMALL_LETTER_SHARP_S expands by 1 byte */
3546 need = slen + 1 + 1;
3550 } /* End of use Unicode (Latin1) semantics */
3551 } /* End of changing the case of the first character */
3553 /* Here, have the first character's changed case stored in tmpbuf. Ready to
3554 * generate the result */
3557 /* We can convert in place. This means we change just the first
3558 * character without disturbing the rest; no need to grow */
3560 s = d = (U8*)SvPV_force_nomg(source, slen);
3566 /* Here, we can't convert in place; we earlier calculated how much
3567 * space we will need, so grow to accommodate that */
3568 SvUPGRADE(dest, SVt_PV);
3569 d = (U8*)SvGROW(dest, need);
3570 (void)SvPOK_only(dest);
3577 if (! convert_source_to_utf8) {
3579 /* Here both source and dest are in UTF-8, but have to create
3580 * the entire output. We initialize the result to be the
3581 * title/lower cased first character, and then append the rest
3583 sv_setpvn(dest, (char*)tmpbuf, tculen);
3585 sv_catpvn(dest, (char*)(s + ulen), slen - ulen);
3589 const U8 *const send = s + slen;
3591 /* Here the dest needs to be in UTF-8, but the source isn't,
3592 * except we earlier UTF-8'd the first character of the source
3593 * into tmpbuf. First put that into dest, and then append the
3594 * rest of the source, converting it to UTF-8 as we go. */
3596 /* Assert tculen is 2 here because the only two characters that
3597 * get to this part of the code have 2-byte UTF-8 equivalents */
3599 *d++ = *(tmpbuf + 1);
3600 s++; /* We have just processed the 1st char */
3602 for (; s < send; s++) {
3603 d = uvchr_to_utf8(d, *s);
3606 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3610 else { /* in-place UTF-8. Just overwrite the first character */
3611 Copy(tmpbuf, d, tculen, U8);
3612 SvCUR_set(dest, need - 1);
3620 else { /* Neither source nor dest are in or need to be UTF-8 */
3622 if (IN_LOCALE_RUNTIME) {
3626 if (inplace) { /* in-place, only need to change the 1st char */
3629 else { /* Not in-place */
3631 /* Copy the case-changed character(s) from tmpbuf */
3632 Copy(tmpbuf, d, tculen, U8);
3633 d += tculen - 1; /* Code below expects d to point to final
3634 * character stored */
3637 else { /* empty source */
3638 /* See bug #39028: Don't taint if empty */
3642 /* In a "use bytes" we don't treat the source as UTF-8, but, still want
3643 * the destination to retain that flag */
3647 if (!inplace) { /* Finish the rest of the string, unchanged */
3648 /* This will copy the trailing NUL */
3649 Copy(s + 1, d + 1, slen, U8);
3650 SvCUR_set(dest, need - 1);
3653 if (dest != source && SvTAINTED(source))
3659 /* There's so much setup/teardown code common between uc and lc, I wonder if
3660 it would be worth merging the two, and just having a switch outside each
3661 of the three tight loops. There is less and less commonality though */
3675 if (SvPADTMP(source) && !SvREADONLY(source) && !SvAMAGIC(source)
3676 && SvTEMP(source) && !DO_UTF8(source)
3677 && (IN_LOCALE_RUNTIME || ! IN_UNI_8_BIT)) {
3679 /* We can convert in place. The reason we can't if in UNI_8_BIT is to
3680 * make the loop tight, so we overwrite the source with the dest before
3681 * looking at it, and we need to look at the original source
3682 * afterwards. There would also need to be code added to handle
3683 * switching to not in-place in midstream if we run into characters
3684 * that change the length.
3687 s = d = (U8*)SvPV_force_nomg(source, len);
3694 /* The old implementation would copy source into TARG at this point.
3695 This had the side effect that if source was undef, TARG was now
3696 an undefined SV with PADTMP set, and they don't warn inside
3697 sv_2pv_flags(). However, we're now getting the PV direct from
3698 source, which doesn't have PADTMP set, so it would warn. Hence the
3702 s = (const U8*)SvPV_nomg_const(source, len);
3704 if (ckWARN(WARN_UNINITIALIZED))
3705 report_uninit(source);
3711 SvUPGRADE(dest, SVt_PV);
3712 d = (U8*)SvGROW(dest, min);
3713 (void)SvPOK_only(dest);
3718 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
3719 to check DO_UTF8 again here. */
3721 if (DO_UTF8(source)) {
3722 const U8 *const send = s + len;
3723 U8 tmpbuf[UTF8_MAXBYTES+1];
3724 bool tainted = FALSE;
3726 /* All occurrences of these are to be moved to follow any other marks.
3727 * This is context-dependent. We may not be passed enough context to
3728 * move the iota subscript beyond all of them, but we do the best we can
3729 * with what we're given. The result is always better than if we
3730 * hadn't done this. And, the problem would only arise if we are
3731 * passed a character without all its combining marks, which would be
3732 * the caller's mistake. The information this is based on comes from a
3733 * comment in Unicode SpecialCasing.txt, (and the Standard's text
3734 * itself) and so can't be checked properly to see if it ever gets
3735 * revised. But the likelihood of it changing is remote */
3736 bool in_iota_subscript = FALSE;
3742 if (in_iota_subscript && ! is_utf8_mark(s)) {
3744 /* A non-mark. Time to output the iota subscript */
3745 #define GREEK_CAPITAL_LETTER_IOTA 0x0399
3746 #define COMBINING_GREEK_YPOGEGRAMMENI 0x0345
3748 CAT_UNI_TO_UTF8_TWO_BYTE(d, GREEK_CAPITAL_LETTER_IOTA);
3749 in_iota_subscript = FALSE;
3752 /* Then handle the current character. Get the changed case value
3753 * and copy it to the output buffer */
3756 uv = _to_utf8_upper_flags(s, tmpbuf, &ulen,
3757 cBOOL(IN_LOCALE_RUNTIME), &tainted);
3758 if (uv == GREEK_CAPITAL_LETTER_IOTA
3759 && utf8_to_uvchr_buf(s, send, 0) == COMBINING_GREEK_YPOGEGRAMMENI)
3761 in_iota_subscript = TRUE;
3764 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
3765 /* If the eventually required minimum size outgrows the
3766 * available space, we need to grow. */
3767 const UV o = d - (U8*)SvPVX_const(dest);
3769 /* If someone uppercases one million U+03B0s we SvGROW()
3770 * one million times. Or we could try guessing how much to
3771 * allocate without allocating too much. Such is life.
3772 * See corresponding comment in lc code for another option
3775 d = (U8*)SvPVX(dest) + o;
3777 Copy(tmpbuf, d, ulen, U8);
3782 if (in_iota_subscript) {
3783 CAT_UNI_TO_UTF8_TWO_BYTE(d, GREEK_CAPITAL_LETTER_IOTA);
3788 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3794 else { /* Not UTF-8 */
3796 const U8 *const send = s + len;
3798 /* Use locale casing if in locale; regular style if not treating
3799 * latin1 as having case; otherwise the latin1 casing. Do the
3800 * whole thing in a tight loop, for speed, */
3801 if (IN_LOCALE_RUNTIME) {
3804 for (; s < send; d++, s++)
3805 *d = toUPPER_LC(*s);
3807 else if (! IN_UNI_8_BIT) {
3808 for (; s < send; d++, s++) {
3813 for (; s < send; d++, s++) {
3814 *d = toUPPER_LATIN1_MOD(*s);
3815 if (LIKELY(*d != LATIN_SMALL_LETTER_Y_WITH_DIAERESIS)) continue;
3817 /* The mainstream case is the tight loop above. To avoid
3818 * extra tests in that, all three characters that require
3819 * special handling are mapped by the MOD to the one tested
3821 * Use the source to distinguish between the three cases */
3823 if (*s == LATIN_SMALL_LETTER_SHARP_S) {
3825 /* uc() of this requires 2 characters, but they are
3826 * ASCII. If not enough room, grow the string */
3827 if (SvLEN(dest) < ++min) {
3828 const UV o = d - (U8*)SvPVX_const(dest);
3830 d = (U8*)SvPVX(dest) + o;
3832 *d++ = 'S'; *d = 'S'; /* upper case is 'SS' */
3833 continue; /* Back to the tight loop; still in ASCII */
3836 /* The other two special handling characters have their
3837 * upper cases outside the latin1 range, hence need to be
3838 * in UTF-8, so the whole result needs to be in UTF-8. So,
3839 * here we are somewhere in the middle of processing a
3840 * non-UTF-8 string, and realize that we will have to convert
3841 * the whole thing to UTF-8. What to do? There are
3842 * several possibilities. The simplest to code is to
3843 * convert what we have so far, set a flag, and continue on
3844 * in the loop. The flag would be tested each time through
3845 * the loop, and if set, the next character would be
3846 * converted to UTF-8 and stored. But, I (khw) didn't want
3847 * to slow down the mainstream case at all for this fairly
3848 * rare case, so I didn't want to add a test that didn't
3849 * absolutely have to be there in the loop, besides the
3850 * possibility that it would get too complicated for
3851 * optimizers to deal with. Another possibility is to just
3852 * give up, convert the source to UTF-8, and restart the
3853 * function that way. Another possibility is to convert
3854 * both what has already been processed and what is yet to
3855 * come separately to UTF-8, then jump into the loop that
3856 * handles UTF-8. But the most efficient time-wise of the
3857 * ones I could think of is what follows, and turned out to
3858 * not require much extra code. */
3860 /* Convert what we have so far into UTF-8, telling the
3861 * function that we know it should be converted, and to
3862 * allow extra space for what we haven't processed yet.
3863 * Assume the worst case space requirements for converting
3864 * what we haven't processed so far: that it will require
3865 * two bytes for each remaining source character, plus the
3866 * NUL at the end. This may cause the string pointer to
3867 * move, so re-find it. */
3869 len = d - (U8*)SvPVX_const(dest);
3870 SvCUR_set(dest, len);
3871 len = sv_utf8_upgrade_flags_grow(dest,
3872 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
3874 d = (U8*)SvPVX(dest) + len;
3876 /* Now process the remainder of the source, converting to
3877 * upper and UTF-8. If a resulting byte is invariant in
3878 * UTF-8, output it as-is, otherwise convert to UTF-8 and
3879 * append it to the output. */
3880 for (; s < send; s++) {
3881 (void) _to_upper_title_latin1(*s, d, &len, 'S');
3885 /* Here have processed the whole source; no need to continue
3886 * with the outer loop. Each character has been converted
3887 * to upper case and converted to UTF-8 */
3890 } /* End of processing all latin1-style chars */
3891 } /* End of processing all chars */
3892 } /* End of source is not empty */
3894 if (source != dest) {
3895 *d = '\0'; /* Here d points to 1 after last char, add NUL */
3896 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3898 } /* End of isn't utf8 */
3899 if (dest != source && SvTAINTED(source))
3918 if (SvPADTMP(source) && !SvREADONLY(source) && !SvAMAGIC(source)
3919 && SvTEMP(source) && !DO_UTF8(source)) {
3921 /* We can convert in place, as lowercasing anything in the latin1 range
3922 * (or else DO_UTF8 would have been on) doesn't lengthen it */
3924 s = d = (U8*)SvPV_force_nomg(source, len);
3931 /* The old implementation would copy source into TARG at this point.
3932 This had the side effect that if source was undef, TARG was now
3933 an undefined SV with PADTMP set, and they don't warn inside
3934 sv_2pv_flags(). However, we're now getting the PV direct from
3935 source, which doesn't have PADTMP set, so it would warn. Hence the
3939 s = (const U8*)SvPV_nomg_const(source, len);
3941 if (ckWARN(WARN_UNINITIALIZED))
3942 report_uninit(source);
3948 SvUPGRADE(dest, SVt_PV);
3949 d = (U8*)SvGROW(dest, min);
3950 (void)SvPOK_only(dest);
3955 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
3956 to check DO_UTF8 again here. */
3958 if (DO_UTF8(source)) {
3959 const U8 *const send = s + len;
3960 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
3961 bool tainted = FALSE;
3964 const STRLEN u = UTF8SKIP(s);
3967 _to_utf8_lower_flags(s, tmpbuf, &ulen,
3968 cBOOL(IN_LOCALE_RUNTIME), &tainted);
3970 /* Here is where we would do context-sensitive actions. See the
3971 * commit message for this comment for why there isn't any */
3973 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
3975 /* If the eventually required minimum size outgrows the
3976 * available space, we need to grow. */
3977 const UV o = d - (U8*)SvPVX_const(dest);
3979 /* If someone lowercases one million U+0130s we SvGROW() one
3980 * million times. Or we could try guessing how much to
3981 * allocate without allocating too much. Such is life.
3982 * Another option would be to grow an extra byte or two more
3983 * each time we need to grow, which would cut down the million
3984 * to 500K, with little waste */
3986 d = (U8*)SvPVX(dest) + o;
3989 /* Copy the newly lowercased letter to the output buffer we're
3991 Copy(tmpbuf, d, ulen, U8);
3994 } /* End of looping through the source string */
3997 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4002 } else { /* Not utf8 */
4004 const U8 *const send = s + len;
4006 /* Use locale casing if in locale; regular style if not treating
4007 * latin1 as having case; otherwise the latin1 casing. Do the
4008 * whole thing in a tight loop, for speed, */
4009 if (IN_LOCALE_RUNTIME) {
4012 for (; s < send; d++, s++)
4013 *d = toLOWER_LC(*s);
4015 else if (! IN_UNI_8_BIT) {
4016 for (; s < send; d++, s++) {
4021 for (; s < send; d++, s++) {
4022 *d = toLOWER_LATIN1(*s);
4026 if (source != dest) {
4028 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4031 if (dest != source && SvTAINTED(source))
4040 SV * const sv = TOPs;
4042 const char *s = SvPV_const(sv,len);
4044 SvUTF8_off(TARG); /* decontaminate */
4047 SvUPGRADE(TARG, SVt_PV);
4048 SvGROW(TARG, (len * 2) + 1);
4052 STRLEN ulen = UTF8SKIP(s);
4053 bool to_quote = FALSE;
4055 if (UTF8_IS_INVARIANT(*s)) {
4056 if (_isQUOTEMETA(*s)) {
4060 else if (UTF8_IS_DOWNGRADEABLE_START(*s)) {
4062 /* In locale, we quote all non-ASCII Latin1 chars.
4063 * Otherwise use the quoting rules */
4064 if (IN_LOCALE_RUNTIME
4065 || _isQUOTEMETA(TWO_BYTE_UTF8_TO_UNI(*s, *(s + 1))))
4070 else if (is_QUOTEMETA_high(s)) {
4085 else if (IN_UNI_8_BIT) {
4087 if (_isQUOTEMETA(*s))
4093 /* For non UNI_8_BIT (and hence in locale) just quote all \W
4094 * including everything above ASCII */
4096 if (!isWORDCHAR_A(*s))
4102 SvCUR_set(TARG, d - SvPVX_const(TARG));
4103 (void)SvPOK_only_UTF8(TARG);
4106 sv_setpvn(TARG, s, len);
4123 U8 tmpbuf[UTF8_MAXBYTES * UTF8_MAX_FOLD_CHAR_EXPAND + 1];
4124 const bool full_folding = TRUE;
4125 const U8 flags = ( full_folding ? FOLD_FLAGS_FULL : 0 )
4126 | ( IN_LOCALE_RUNTIME ? FOLD_FLAGS_LOCALE : 0 );
4128 /* This is a facsimile of pp_lc, but with a thousand bugs thanks to me.
4129 * You are welcome(?) -Hugmeir
4137 s = (const U8*)SvPV_nomg_const(source, len);
4139 if (ckWARN(WARN_UNINITIALIZED))
4140 report_uninit(source);
4147 SvUPGRADE(dest, SVt_PV);
4148 d = (U8*)SvGROW(dest, min);
4149 (void)SvPOK_only(dest);
4154 if (DO_UTF8(source)) { /* UTF-8 flagged string. */
4155 bool tainted = FALSE;
4157 const STRLEN u = UTF8SKIP(s);
4160 _to_utf8_fold_flags(s, tmpbuf, &ulen, flags, &tainted);
4162 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
4163 const UV o = d - (U8*)SvPVX_const(dest);
4165 d = (U8*)SvPVX(dest) + o;
4168 Copy(tmpbuf, d, ulen, U8);
4177 } /* Unflagged string */
4179 /* For locale, bytes, and nothing, the behavior is supposed to be the
4182 if ( IN_LOCALE_RUNTIME ) { /* Under locale */
4185 for (; s < send; d++, s++)
4186 *d = toLOWER_LC(*s);
4188 else if ( !IN_UNI_8_BIT ) { /* Under nothing, or bytes */
4189 for (; s < send; d++, s++)
4193 /* For ASCII and the Latin-1 range, there's only two troublesome folds,
4194 * \x{DF} (\N{LATIN SMALL LETTER SHARP S}), which under full casefolding
4195 * becomes 'ss', and \x{B5} (\N{MICRO SIGN}), which under any fold becomes
4196 * \x{3BC} (\N{GREEK SMALL LETTER MU}) -- For the rest, the casefold is
4199 for (; s < send; d++, s++) {
4200 if (*s == MICRO_SIGN) {
4201 /* \N{MICRO SIGN}'s casefold is \N{GREEK SMALL LETTER MU}, which
4202 * is outside of the latin-1 range. There's a couple of ways to
4203 * deal with this -- khw discusses them in pp_lc/uc, so go there :)
4204 * What we do here is upgrade what we had already casefolded,
4205 * then enter an inner loop that appends the rest of the characters
4208 len = d - (U8*)SvPVX_const(dest);
4209 SvCUR_set(dest, len);
4210 len = sv_utf8_upgrade_flags_grow(dest,
4211 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
4212 /* The max expansion for latin1
4213 * chars is 1 byte becomes 2 */
4215 d = (U8*)SvPVX(dest) + len;
4217 CAT_UNI_TO_UTF8_TWO_BYTE(d, GREEK_SMALL_LETTER_MU);
4219 for (; s < send; s++) {
4221 UV fc = _to_uni_fold_flags(*s, tmpbuf, &ulen, flags);
4222 if UNI_IS_INVARIANT(fc) {
4223 if ( full_folding && *s == LATIN_SMALL_LETTER_SHARP_S) {
4231 Copy(tmpbuf, d, ulen, U8);
4237 else if (full_folding && *s == LATIN_SMALL_LETTER_SHARP_S) {
4238 /* Under full casefolding, LATIN SMALL LETTER SHARP S becomes "ss",
4239 * which may require growing the SV.
4241 if (SvLEN(dest) < ++min) {
4242 const UV o = d - (U8*)SvPVX_const(dest);
4244 d = (U8*)SvPVX(dest) + o;
4249 else { /* If it's not one of those two, the fold is their lower case */
4250 *d = toLOWER_LATIN1(*s);
4256 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4258 if (SvTAINTED(source))
4268 dVAR; dSP; dMARK; dORIGMARK;
4269 AV *const av = MUTABLE_AV(POPs);
4270 const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
4272 if (SvTYPE(av) == SVt_PVAV) {
4273 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
4274 bool can_preserve = FALSE;
4280 can_preserve = SvCANEXISTDELETE(av);
4283 if (lval && localizing) {
4286 for (svp = MARK + 1; svp <= SP; svp++) {
4287 const I32 elem = SvIV(*svp);
4291 if (max > AvMAX(av))
4295 while (++MARK <= SP) {
4297 I32 elem = SvIV(*MARK);
4298 bool preeminent = TRUE;
4300 if (localizing && can_preserve) {
4301 /* If we can determine whether the element exist,
4302 * Try to preserve the existenceness of a tied array
4303 * element by using EXISTS and DELETE if possible.
4304 * Fallback to FETCH and STORE otherwise. */
4305 preeminent = av_exists(av, elem);
4308 svp = av_fetch(av, elem, lval);
4310 if (!svp || *svp == &PL_sv_undef)
4311 DIE(aTHX_ PL_no_aelem, elem);
4314 save_aelem(av, elem, svp);
4316 SAVEADELETE(av, elem);
4319 *MARK = svp ? *svp : &PL_sv_undef;
4322 if (GIMME != G_ARRAY) {
4324 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
4330 /* Smart dereferencing for keys, values and each */
4342 (SvTYPE(sv) != SVt_PVHV && SvTYPE(sv) != SVt_PVAV)
4347 "Type of argument to %s must be unblessed hashref or arrayref",
4348 PL_op_desc[PL_op->op_type] );
4351 if (PL_op->op_flags & OPf_SPECIAL && SvTYPE(sv) == SVt_PVAV)
4353 "Can't modify %s in %s",
4354 PL_op_desc[PL_op->op_type], PL_op_desc[PL_op->op_next->op_type]
4357 /* Delegate to correct function for op type */
4359 if (PL_op->op_type == OP_RKEYS || PL_op->op_type == OP_RVALUES) {
4360 return (SvTYPE(sv) == SVt_PVHV) ? Perl_do_kv(aTHX) : Perl_pp_akeys(aTHX);
4363 return (SvTYPE(sv) == SVt_PVHV) ? Perl_pp_each(aTHX) : Perl_pp_aeach(aTHX);
4371 AV *array = MUTABLE_AV(POPs);
4372 const I32 gimme = GIMME_V;
4373 IV *iterp = Perl_av_iter_p(aTHX_ array);
4374 const IV current = (*iterp)++;
4376 if (current > av_len(array)) {
4378 if (gimme == G_SCALAR)
4386 if (gimme == G_ARRAY) {
4387 SV **const element = av_fetch(array, current, 0);
4388 PUSHs(element ? *element : &PL_sv_undef);
4397 AV *array = MUTABLE_AV(POPs);
4398 const I32 gimme = GIMME_V;
4400 *Perl_av_iter_p(aTHX_ array) = 0;
4402 if (gimme == G_SCALAR) {
4404 PUSHi(av_len(array) + 1);
4406 else if (gimme == G_ARRAY) {
4407 IV n = Perl_av_len(aTHX_ array);
4412 if (PL_op->op_type == OP_AKEYS || PL_op->op_type == OP_RKEYS) {
4413 for (i = 0; i <= n; i++) {
4418 for (i = 0; i <= n; i++) {
4419 SV *const *const elem = Perl_av_fetch(aTHX_ array, i, 0);
4420 PUSHs(elem ? *elem : &PL_sv_undef);
4427 /* Associative arrays. */
4433 HV * hash = MUTABLE_HV(POPs);
4435 const I32 gimme = GIMME_V;
4438 /* might clobber stack_sp */
4439 entry = hv_iternext(hash);
4444 SV* const sv = hv_iterkeysv(entry);
4445 PUSHs(sv); /* won't clobber stack_sp */
4446 if (gimme == G_ARRAY) {
4449 /* might clobber stack_sp */
4450 val = hv_iterval(hash, entry);
4455 else if (gimme == G_SCALAR)
4462 S_do_delete_local(pTHX)
4466 const I32 gimme = GIMME_V;
4469 const bool sliced = !!(PL_op->op_private & OPpSLICE);
4470 SV *unsliced_keysv = sliced ? NULL : POPs;
4471 SV * const osv = POPs;
4472 SV **mark = sliced ? PL_stack_base + POPMARK : &unsliced_keysv-1;
4474 const bool tied = SvRMAGICAL(osv)
4475 && mg_find((const SV *)osv, PERL_MAGIC_tied);
4476 const bool can_preserve = SvCANEXISTDELETE(osv);
4477 const U32 type = SvTYPE(osv);
4478 SV ** const end = sliced ? SP : &unsliced_keysv;
4480 if (type == SVt_PVHV) { /* hash element */
4481 HV * const hv = MUTABLE_HV(osv);
4482 while (++MARK <= end) {
4483 SV * const keysv = *MARK;
4485 bool preeminent = TRUE;
4487 preeminent = hv_exists_ent(hv, keysv, 0);
4489 HE *he = hv_fetch_ent(hv, keysv, 1, 0);
4496 sv = hv_delete_ent(hv, keysv, 0, 0);
4497 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4500 if (!sv) DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
4501 save_helem_flags(hv, keysv, &sv, SAVEf_KEEPOLDELEM);
4503 *MARK = sv_mortalcopy(sv);
4509 SAVEHDELETE(hv, keysv);
4510 *MARK = &PL_sv_undef;
4514 else if (type == SVt_PVAV) { /* array element */
4515 if (PL_op->op_flags & OPf_SPECIAL) {
4516 AV * const av = MUTABLE_AV(osv);
4517 while (++MARK <= end) {
4518 I32 idx = SvIV(*MARK);
4520 bool preeminent = TRUE;
4522 preeminent = av_exists(av, idx);
4524 SV **svp = av_fetch(av, idx, 1);
4531 sv = av_delete(av, idx, 0);
4532 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4535 save_aelem_flags(av, idx, &sv, SAVEf_KEEPOLDELEM);
4537 *MARK = sv_mortalcopy(sv);
4543 SAVEADELETE(av, idx);
4544 *MARK = &PL_sv_undef;
4549 DIE(aTHX_ "panic: avhv_delete no longer supported");
4552 DIE(aTHX_ "Not a HASH reference");
4554 if (gimme == G_VOID)
4556 else if (gimme == G_SCALAR) {
4561 *++MARK = &PL_sv_undef;
4565 else if (gimme != G_VOID)
4566 PUSHs(unsliced_keysv);
4578 if (PL_op->op_private & OPpLVAL_INTRO)
4579 return do_delete_local();
4582 discard = (gimme == G_VOID) ? G_DISCARD : 0;
4584 if (PL_op->op_private & OPpSLICE) {
4586 HV * const hv = MUTABLE_HV(POPs);
4587 const U32 hvtype = SvTYPE(hv);
4588 if (hvtype == SVt_PVHV) { /* hash element */
4589 while (++MARK <= SP) {
4590 SV * const sv = hv_delete_ent(hv, *MARK, discard, 0);
4591 *MARK = sv ? sv : &PL_sv_undef;
4594 else if (hvtype == SVt_PVAV) { /* array element */
4595 if (PL_op->op_flags & OPf_SPECIAL) {
4596 while (++MARK <= SP) {
4597 SV * const sv = av_delete(MUTABLE_AV(hv), SvIV(*MARK), discard);
4598 *MARK = sv ? sv : &PL_sv_undef;
4603 DIE(aTHX_ "Not a HASH reference");
4606 else if (gimme == G_SCALAR) {
4611 *++MARK = &PL_sv_undef;
4617 HV * const hv = MUTABLE_HV(POPs);
4619 if (SvTYPE(hv) == SVt_PVHV)
4620 sv = hv_delete_ent(hv, keysv, discard, 0);
4621 else if (SvTYPE(hv) == SVt_PVAV) {
4622 if (PL_op->op_flags & OPf_SPECIAL)
4623 sv = av_delete(MUTABLE_AV(hv), SvIV(keysv), discard);
4625 DIE(aTHX_ "panic: avhv_delete no longer supported");
4628 DIE(aTHX_ "Not a HASH reference");
4644 if (PL_op->op_private & OPpEXISTS_SUB) {
4646 SV * const sv = POPs;
4647 CV * const cv = sv_2cv(sv, &hv, &gv, 0);
4650 if (gv && isGV(gv) && GvCV(gv) && !GvCVGEN(gv))
4655 hv = MUTABLE_HV(POPs);
4656 if (SvTYPE(hv) == SVt_PVHV) {
4657 if (hv_exists_ent(hv, tmpsv, 0))
4660 else if (SvTYPE(hv) == SVt_PVAV) {
4661 if (PL_op->op_flags & OPf_SPECIAL) { /* array element */
4662 if (av_exists(MUTABLE_AV(hv), SvIV(tmpsv)))
4667 DIE(aTHX_ "Not a HASH reference");
4674 dVAR; dSP; dMARK; dORIGMARK;
4675 HV * const hv = MUTABLE_HV(POPs);
4676 const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
4677 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
4678 bool can_preserve = FALSE;
4684 if (SvCANEXISTDELETE(hv))
4685 can_preserve = TRUE;
4688 while (++MARK <= SP) {
4689 SV * const keysv = *MARK;
4692 bool preeminent = TRUE;
4694 if (localizing && can_preserve) {
4695 /* If we can determine whether the element exist,
4696 * try to preserve the existenceness of a tied hash
4697 * element by using EXISTS and DELETE if possible.
4698 * Fallback to FETCH and STORE otherwise. */
4699 preeminent = hv_exists_ent(hv, keysv, 0);
4702 he = hv_fetch_ent(hv, keysv, lval, 0);
4703 svp = he ? &HeVAL(he) : NULL;
4706 if (!svp || !*svp || *svp == &PL_sv_undef) {
4707 DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
4710 if (HvNAME_get(hv) && isGV(*svp))
4711 save_gp(MUTABLE_GV(*svp), !(PL_op->op_flags & OPf_SPECIAL));
4712 else if (preeminent)
4713 save_helem_flags(hv, keysv, svp,
4714 (PL_op->op_flags & OPf_SPECIAL) ? 0 : SAVEf_SETMAGIC);
4716 SAVEHDELETE(hv, keysv);
4719 *MARK = svp && *svp ? *svp : &PL_sv_undef;
4721 if (GIMME != G_ARRAY) {
4723 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
4729 /* List operators. */
4734 if (GIMME != G_ARRAY) {
4736 *MARK = *SP; /* unwanted list, return last item */
4738 *MARK = &PL_sv_undef;
4748 SV ** const lastrelem = PL_stack_sp;
4749 SV ** const lastlelem = PL_stack_base + POPMARK;
4750 SV ** const firstlelem = PL_stack_base + POPMARK + 1;
4751 SV ** const firstrelem = lastlelem + 1;
4752 I32 is_something_there = FALSE;
4754 const I32 max = lastrelem - lastlelem;
4757 if (GIMME != G_ARRAY) {
4758 I32 ix = SvIV(*lastlelem);
4761 if (ix < 0 || ix >= max)
4762 *firstlelem = &PL_sv_undef;
4764 *firstlelem = firstrelem[ix];
4770 SP = firstlelem - 1;
4774 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
4775 I32 ix = SvIV(*lelem);
4778 if (ix < 0 || ix >= max)
4779 *lelem = &PL_sv_undef;
4781 is_something_there = TRUE;
4782 if (!(*lelem = firstrelem[ix]))
4783 *lelem = &PL_sv_undef;
4786 if (is_something_there)
4789 SP = firstlelem - 1;
4795 dVAR; dSP; dMARK; dORIGMARK;
4796 const I32 items = SP - MARK;
4797 SV * const av = MUTABLE_SV(av_make(items, MARK+1));
4798 SP = ORIGMARK; /* av_make() might realloc stack_sp */
4799 mXPUSHs((PL_op->op_flags & OPf_SPECIAL)
4800 ? newRV_noinc(av) : av);
4806 dVAR; dSP; dMARK; dORIGMARK;
4807 HV* const hv = (HV *)sv_2mortal((SV *)newHV());
4811 (MARK++, SvGMAGICAL(*MARK) ? sv_mortalcopy(*MARK) : *MARK);
4818 sv_setsv(val, *MARK);
4822 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Odd number of elements in anonymous hash");
4825 (void)hv_store_ent(hv,key,val,0);
4828 if (PL_op->op_flags & OPf_SPECIAL)
4829 mXPUSHs(newRV_inc(MUTABLE_SV(hv)));
4830 else XPUSHs(MUTABLE_SV(hv));
4835 S_deref_plain_array(pTHX_ AV *ary)
4837 if (SvTYPE(ary) == SVt_PVAV) return ary;
4838 SvGETMAGIC((SV *)ary);
4839 if (!SvROK(ary) || SvTYPE(SvRV(ary)) != SVt_PVAV)
4840 Perl_die(aTHX_ "Not an ARRAY reference");
4841 else if (SvOBJECT(SvRV(ary)))
4842 Perl_die(aTHX_ "Not an unblessed ARRAY reference");
4843 return (AV *)SvRV(ary);
4846 #if defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
4847 # define DEREF_PLAIN_ARRAY(ary) \
4850 SvTYPE(aRrRay) == SVt_PVAV \
4852 : S_deref_plain_array(aTHX_ aRrRay); \
4855 # define DEREF_PLAIN_ARRAY(ary) \
4857 PL_Sv = (SV *)(ary), \
4858 SvTYPE(PL_Sv) == SVt_PVAV \
4860 : S_deref_plain_array(aTHX_ (AV *)PL_Sv) \
4866 dVAR; dSP; dMARK; dORIGMARK;
4867 int num_args = (SP - MARK);
4868 AV *ary = DEREF_PLAIN_ARRAY(MUTABLE_AV(*++MARK));
4877 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
4880 return Perl_tied_method(aTHX_ "SPLICE", mark - 1, MUTABLE_SV(ary), mg,
4881 GIMME_V | TIED_METHOD_ARGUMENTS_ON_STACK,
4888 offset = i = SvIV(*MARK);
4890 offset += AvFILLp(ary) + 1;
4892 DIE(aTHX_ PL_no_aelem, i);
4894 length = SvIVx(*MARK++);
4896 length += AvFILLp(ary) - offset + 1;
4902 length = AvMAX(ary) + 1; /* close enough to infinity */
4906 length = AvMAX(ary) + 1;
4908 if (offset > AvFILLp(ary) + 1) {
4910 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "splice() offset past end of array" );
4911 offset = AvFILLp(ary) + 1;
4913 after = AvFILLp(ary) + 1 - (offset + length);
4914 if (after < 0) { /* not that much array */
4915 length += after; /* offset+length now in array */
4921 /* At this point, MARK .. SP-1 is our new LIST */
4924 diff = newlen - length;
4925 if (newlen && !AvREAL(ary) && AvREIFY(ary))
4928 /* make new elements SVs now: avoid problems if they're from the array */
4929 for (dst = MARK, i = newlen; i; i--) {
4930 SV * const h = *dst;
4931 *dst++ = newSVsv(h);
4934 if (diff < 0) { /* shrinking the area */
4935 SV **tmparyval = NULL;
4937 Newx(tmparyval, newlen, SV*); /* so remember insertion */
4938 Copy(MARK, tmparyval, newlen, SV*);
4941 MARK = ORIGMARK + 1;
4942 if (GIMME == G_ARRAY) { /* copy return vals to stack */
4943 MEXTEND(MARK, length);
4944 Copy(AvARRAY(ary)+offset, MARK, length, SV*);
4946 EXTEND_MORTAL(length);
4947 for (i = length, dst = MARK; i; i--) {
4948 sv_2mortal(*dst); /* free them eventually */
4955 *MARK = AvARRAY(ary)[offset+length-1];
4958 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
4959 SvREFCNT_dec(*dst++); /* free them now */
4962 AvFILLp(ary) += diff;
4964 /* pull up or down? */
4966 if (offset < after) { /* easier to pull up */
4967 if (offset) { /* esp. if nothing to pull */
4968 src = &AvARRAY(ary)[offset-1];
4969 dst = src - diff; /* diff is negative */
4970 for (i = offset; i > 0; i--) /* can't trust Copy */
4974 AvARRAY(ary) = AvARRAY(ary) - diff; /* diff is negative */
4978 if (after) { /* anything to pull down? */
4979 src = AvARRAY(ary) + offset + length;
4980 dst = src + diff; /* diff is negative */
4981 Move(src, dst, after, SV*);
4983 dst = &AvARRAY(ary)[AvFILLp(ary)+1];
4984 /* avoid later double free */
4988 dst[--i] = &PL_sv_undef;
4991 Copy( tmparyval, AvARRAY(ary) + offset, newlen, SV* );
4992 Safefree(tmparyval);
4995 else { /* no, expanding (or same) */
4996 SV** tmparyval = NULL;
4998 Newx(tmparyval, length, SV*); /* so remember deletion */
4999 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
5002 if (diff > 0) { /* expanding */
5003 /* push up or down? */
5004 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
5008 Move(src, dst, offset, SV*);
5010 AvARRAY(ary) = AvARRAY(ary) - diff;/* diff is positive */
5012 AvFILLp(ary) += diff;
5015 if (AvFILLp(ary) + diff >= AvMAX(ary)) /* oh, well */
5016 av_extend(ary, AvFILLp(ary) + diff);
5017 AvFILLp(ary) += diff;
5020 dst = AvARRAY(ary) + AvFILLp(ary);
5022 for (i = after; i; i--) {
5030 Copy( MARK, AvARRAY(ary) + offset, newlen, SV* );
5033 MARK = ORIGMARK + 1;
5034 if (GIMME == G_ARRAY) { /* copy return vals to stack */
5036 Copy(tmparyval, MARK, length, SV*);
5038 EXTEND_MORTAL(length);
5039 for (i = length, dst = MARK; i; i--) {
5040 sv_2mortal(*dst); /* free them eventually */
5047 else if (length--) {
5048 *MARK = tmparyval[length];
5051 while (length-- > 0)
5052 SvREFCNT_dec(tmparyval[length]);
5056 *MARK = &PL_sv_undef;
5057 Safefree(tmparyval);
5061 mg_set(MUTABLE_SV(ary));
5069 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
5070 AV * const ary = DEREF_PLAIN_ARRAY(MUTABLE_AV(*++MARK));
5071 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
5074 *MARK-- = SvTIED_obj(MUTABLE_SV(ary), mg);
5077 ENTER_with_name("call_PUSH");
5078 call_method("PUSH",G_SCALAR|G_DISCARD);
5079 LEAVE_with_name("call_PUSH");
5083 PL_delaymagic = DM_DELAY;
5084 for (++MARK; MARK <= SP; MARK++) {
5086 if (*MARK) SvGETMAGIC(*MARK);
5089 sv_setsv_nomg(sv, *MARK);
5090 av_store(ary, AvFILLp(ary)+1, sv);
5092 if (PL_delaymagic & DM_ARRAY_ISA)
5093 mg_set(MUTABLE_SV(ary));
5098 if (OP_GIMME(PL_op, 0) != G_VOID) {
5099 PUSHi( AvFILL(ary) + 1 );
5108 AV * const av = PL_op->op_flags & OPf_SPECIAL
5109 ? MUTABLE_AV(GvAV(PL_defgv)) : DEREF_PLAIN_ARRAY(MUTABLE_AV(POPs));
5110 SV * const sv = PL_op->op_type == OP_SHIFT ? av_shift(av) : av_pop(av);
5114 (void)sv_2mortal(sv);
5121 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
5122 AV *ary = DEREF_PLAIN_ARRAY(MUTABLE_AV(*++MARK));
5123 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
5126 *MARK-- = SvTIED_obj(MUTABLE_SV(ary), mg);
5129 ENTER_with_name("call_UNSHIFT");
5130 call_method("UNSHIFT",G_SCALAR|G_DISCARD);
5131 LEAVE_with_name("call_UNSHIFT");
5136 av_unshift(ary, SP - MARK);
5138 SV * const sv = newSVsv(*++MARK);
5139 (void)av_store(ary, i++, sv);
5143 if (OP_GIMME(PL_op, 0) != G_VOID) {
5144 PUSHi( AvFILL(ary) + 1 );
5153 if (GIMME == G_ARRAY) {
5154 if (PL_op->op_private & OPpREVERSE_INPLACE) {
5158 assert( MARK+1 == SP && *SP && SvTYPE(*SP) == SVt_PVAV);
5159 (void)POPMARK; /* remove mark associated with ex-OP_AASSIGN */
5160 av = MUTABLE_AV((*SP));
5161 /* In-place reversing only happens in void context for the array
5162 * assignment. We don't need to push anything on the stack. */
5165 if (SvMAGICAL(av)) {
5167 SV *tmp = sv_newmortal();
5168 /* For SvCANEXISTDELETE */
5171 bool can_preserve = SvCANEXISTDELETE(av);
5173 for (i = 0, j = av_len(av); i < j; ++i, --j) {
5177 if (!av_exists(av, i)) {
5178 if (av_exists(av, j)) {
5179 SV *sv = av_delete(av, j, 0);
5180 begin = *av_fetch(av, i, TRUE);
5181 sv_setsv_mg(begin, sv);
5185 else if (!av_exists(av, j)) {
5186 SV *sv = av_delete(av, i, 0);
5187 end = *av_fetch(av, j, TRUE);
5188 sv_setsv_mg(end, sv);
5193 begin = *av_fetch(av, i, TRUE);
5194 end = *av_fetch(av, j, TRUE);
5195 sv_setsv(tmp, begin);
5196 sv_setsv_mg(begin, end);
5197 sv_setsv_mg(end, tmp);
5201 SV **begin = AvARRAY(av);
5204 SV **end = begin + AvFILLp(av);
5206 while (begin < end) {
5207 SV * const tmp = *begin;
5218 SV * const tmp = *MARK;
5222 /* safe as long as stack cannot get extended in the above */
5233 SvUTF8_off(TARG); /* decontaminate */
5235 do_join(TARG, &PL_sv_no, MARK, SP);
5237 sv_setsv(TARG, SP > MARK ? *SP : find_rundefsv());
5238 if (! SvOK(TARG) && ckWARN(WARN_UNINITIALIZED))
5239 report_uninit(TARG);
5242 up = SvPV_force(TARG, len);
5244 if (DO_UTF8(TARG)) { /* first reverse each character */
5245 U8* s = (U8*)SvPVX(TARG);
5246 const U8* send = (U8*)(s + len);
5248 if (UTF8_IS_INVARIANT(*s)) {
5253 if (!utf8_to_uvchr_buf(s, send, 0))
5257 down = (char*)(s - 1);
5258 /* reverse this character */
5262 *down-- = (char)tmp;
5268 down = SvPVX(TARG) + len - 1;
5272 *down-- = (char)tmp;
5274 (void)SvPOK_only_UTF8(TARG);
5286 IV limit = POPi; /* note, negative is forever */
5287 SV * const sv = POPs;
5289 const char *s = SvPV_const(sv, len);
5290 const bool do_utf8 = DO_UTF8(sv);
5291 const bool skipwhite = PL_op->op_flags & OPf_SPECIAL;
5292 const char *strend = s + len;
5298 const STRLEN slen = do_utf8 ? utf8_length((U8*)s, (U8*)strend) : (STRLEN)(strend - s);
5299 I32 maxiters = slen + 10;
5300 I32 trailing_empty = 0;
5302 const I32 origlimit = limit;
5305 const I32 gimme = GIMME_V;
5307 const I32 oldsave = PL_savestack_ix;
5308 U32 make_mortal = SVs_TEMP;
5313 Copy(&LvTARGOFF(POPs), &pm, 1, PMOP*);
5318 DIE(aTHX_ "panic: pp_split, pm=%p, s=%p", pm, s);
5321 TAINT_IF(get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET &&
5322 (RX_EXTFLAGS(rx) & RXf_WHITE || skipwhite));
5324 RX_MATCH_UTF8_set(rx, do_utf8);
5327 if (pm->op_pmreplrootu.op_pmtargetoff) {
5328 ary = GvAVn(MUTABLE_GV(PAD_SVl(pm->op_pmreplrootu.op_pmtargetoff)));
5331 if (pm->op_pmreplrootu.op_pmtargetgv) {
5332 ary = GvAVn(pm->op_pmreplrootu.op_pmtargetgv);
5337 if (ary && (gimme != G_ARRAY || (pm->op_pmflags & PMf_ONCE))) {
5343 if ((mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied))) {
5345 XPUSHs(SvTIED_obj(MUTABLE_SV(ary), mg));
5352 for (i = AvFILLp(ary); i >= 0; i--)
5353 AvARRAY(ary)[i] = &PL_sv_undef; /* don't free mere refs */
5355 /* temporarily switch stacks */
5356 SAVESWITCHSTACK(PL_curstack, ary);
5360 base = SP - PL_stack_base;
5364 while (*s == ' ' || is_utf8_space((U8*)s))
5367 else if (get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET) {
5368 while (isSPACE_LC(*s))
5376 if (RX_EXTFLAGS(rx) & RXf_PMf_MULTILINE) {
5380 gimme_scalar = gimme == G_SCALAR && !ary;
5383 limit = maxiters + 2;
5384 if (RX_EXTFLAGS(rx) & RXf_WHITE || skipwhite) {
5387 /* this one uses 'm' and is a negative test */
5389 while (m < strend && !( *m == ' ' || is_utf8_space((U8*)m) )) {
5390 const int t = UTF8SKIP(m);
5391 /* is_utf8_space returns FALSE for malform utf8 */
5398 else if (get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET) {
5399 while (m < strend && !isSPACE_LC(*m))
5402 while (m < strend && !isSPACE(*m))
5415 dstr = newSVpvn_flags(s, m-s,
5416 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5420 /* skip the whitespace found last */
5422 s = m + UTF8SKIP(m);
5426 /* this one uses 's' and is a positive test */
5428 while (s < strend && ( *s == ' ' || is_utf8_space((U8*)s) ))
5431 else if (get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET) {
5432 while (s < strend && isSPACE_LC(*s))
5435 while (s < strend && isSPACE(*s))
5440 else if (RX_EXTFLAGS(rx) & RXf_START_ONLY) {
5442 for (m = s; m < strend && *m != '\n'; m++)
5455 dstr = newSVpvn_flags(s, m-s,
5456 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5462 else if (RX_EXTFLAGS(rx) & RXf_NULL && !(s >= strend)) {
5464 Pre-extend the stack, either the number of bytes or
5465 characters in the string or a limited amount, triggered by:
5467 my ($x, $y) = split //, $str;
5471 if (!gimme_scalar) {
5472 const U32 items = limit - 1;
5481 /* keep track of how many bytes we skip over */
5491 dstr = newSVpvn_flags(m, s-m, SVf_UTF8 | make_mortal);
5504 dstr = newSVpvn(s, 1);
5520 else if (do_utf8 == (RX_UTF8(rx) != 0) &&
5521 (RX_EXTFLAGS(rx) & RXf_USE_INTUIT) && !RX_NPARENS(rx)
5522 && (RX_EXTFLAGS(rx) & RXf_CHECK_ALL)
5523 && !(RX_EXTFLAGS(rx) & RXf_ANCH)) {
5524 const int tail = (RX_EXTFLAGS(rx) & RXf_INTUIT_TAIL);
5525 SV * const csv = CALLREG_INTUIT_STRING(rx);
5527 len = RX_MINLENRET(rx);
5528 if (len == 1 && !RX_UTF8(rx) && !tail) {
5529 const char c = *SvPV_nolen_const(csv);
5531 for (m = s; m < strend && *m != c; m++)
5542 dstr = newSVpvn_flags(s, m-s,
5543 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5546 /* The rx->minlen is in characters but we want to step
5547 * s ahead by bytes. */
5549 s = (char*)utf8_hop((U8*)m, len);
5551 s = m + len; /* Fake \n at the end */
5555 while (s < strend && --limit &&
5556 (m = fbm_instr((unsigned char*)s, (unsigned char*)strend,
5557 csv, multiline ? FBMrf_MULTILINE : 0)) )
5566 dstr = newSVpvn_flags(s, m-s,
5567 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5570 /* The rx->minlen is in characters but we want to step
5571 * s ahead by bytes. */
5573 s = (char*)utf8_hop((U8*)m, len);
5575 s = m + len; /* Fake \n at the end */
5580 maxiters += slen * RX_NPARENS(rx);
5581 while (s < strend && --limit)
5585 rex_return = CALLREGEXEC(rx, (char*)s, (char*)strend, (char*)orig, 1 ,
5588 if (rex_return == 0)
5590 TAINT_IF(RX_MATCH_TAINTED(rx));
5591 /* we never pass the REXEC_COPY_STR flag, so it should
5592 * never get copied */
5593 assert(!RX_MATCH_COPIED(rx));
5594 m = RX_OFFS(rx)[0].start + orig;
5603 dstr = newSVpvn_flags(s, m-s,
5604 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5607 if (RX_NPARENS(rx)) {
5609 for (i = 1; i <= (I32)RX_NPARENS(rx); i++) {
5610 s = RX_OFFS(rx)[i].start + orig;
5611 m = RX_OFFS(rx)[i].end + orig;
5613 /* japhy (07/27/01) -- the (m && s) test doesn't catch
5614 parens that didn't match -- they should be set to
5615 undef, not the empty string */
5623 if (m >= orig && s >= orig) {
5624 dstr = newSVpvn_flags(s, m-s,
5625 (do_utf8 ? SVf_UTF8 : 0)
5629 dstr = &PL_sv_undef; /* undef, not "" */
5635 s = RX_OFFS(rx)[0].end + orig;
5639 if (!gimme_scalar) {
5640 iters = (SP - PL_stack_base) - base;
5642 if (iters > maxiters)
5643 DIE(aTHX_ "Split loop");
5645 /* keep field after final delim? */
5646 if (s < strend || (iters && origlimit)) {
5647 if (!gimme_scalar) {
5648 const STRLEN l = strend - s;
5649 dstr = newSVpvn_flags(s, l, (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5654 else if (!origlimit) {
5656 iters -= trailing_empty;
5658 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0)) {
5659 if (TOPs && !make_mortal)
5661 *SP-- = &PL_sv_undef;
5668 LEAVE_SCOPE(oldsave); /* may undo an earlier SWITCHSTACK */
5672 if (SvSMAGICAL(ary)) {
5674 mg_set(MUTABLE_SV(ary));
5677 if (gimme == G_ARRAY) {
5679 Copy(AvARRAY(ary), SP + 1, iters, SV*);
5686 ENTER_with_name("call_PUSH");
5687 call_method("PUSH",G_SCALAR|G_DISCARD);
5688 LEAVE_with_name("call_PUSH");
5690 if (gimme == G_ARRAY) {
5692 /* EXTEND should not be needed - we just popped them */
5694 for (i=0; i < iters; i++) {
5695 SV **svp = av_fetch(ary, i, FALSE);
5696 PUSHs((svp) ? *svp : &PL_sv_undef);
5703 if (gimme == G_ARRAY)
5715 SV *const sv = PAD_SVl(PL_op->op_targ);
5717 if (SvPADSTALE(sv)) {
5720 RETURNOP(cLOGOP->op_other);
5722 RETURNOP(cLOGOP->op_next);
5732 if (SvTYPE(retsv) == SVt_PVAV || SvTYPE(retsv) == SVt_PVHV
5733 || SvTYPE(retsv) == SVt_PVCV) {
5734 retsv = refto(retsv);
5741 PP(unimplemented_op)
5744 const Optype op_type = PL_op->op_type;
5745 /* Using OP_NAME() isn't going to be helpful here. Firstly, it doesn't cope
5746 with out of range op numbers - it only "special" cases op_custom.
5747 Secondly, as the three ops we "panic" on are padmy, mapstart and custom,
5748 if we get here for a custom op then that means that the custom op didn't
5749 have an implementation. Given that OP_NAME() looks up the custom op
5750 by its pp_addr, likely it will return NULL, unless someone (unhelpfully)
5751 registers &PL_unimplemented_op as the address of their custom op.
5752 NULL doesn't generate a useful error message. "custom" does. */
5753 const char *const name = op_type >= OP_max
5754 ? "[out of range]" : PL_op_name[PL_op->op_type];
5755 if(OP_IS_SOCKET(op_type))
5756 DIE(aTHX_ PL_no_sock_func, name);
5757 DIE(aTHX_ "panic: unimplemented op %s (#%d) called", name, op_type);
5760 /* For sorting out arguments passed to a &CORE:: subroutine */
5764 int opnum = SvIOK(cSVOP_sv) ? (int)SvUV(cSVOP_sv) : 0;
5765 int defgv = PL_opargs[opnum] & OA_DEFGV ||opnum==OP_GLOB, whicharg = 0;
5766 AV * const at_ = GvAV(PL_defgv);
5767 SV **svp = at_ ? AvARRAY(at_) : NULL;
5768 I32 minargs = 0, maxargs = 0, numargs = at_ ? AvFILLp(at_)+1 : 0;
5769 I32 oa = opnum ? PL_opargs[opnum] >> OASHIFT : 0;
5770 bool seen_question = 0;
5771 const char *err = NULL;
5772 const bool pushmark = PL_op->op_private & OPpCOREARGS_PUSHMARK;
5774 /* Count how many args there are first, to get some idea how far to
5775 extend the stack. */
5777 if ((oa & 7) == OA_LIST) { maxargs = I32_MAX; break; }
5779 if (oa & OA_OPTIONAL) seen_question = 1;
5780 if (!seen_question) minargs++;
5784 if(numargs < minargs) err = "Not enough";
5785 else if(numargs > maxargs) err = "Too many";
5787 /* diag_listed_as: Too many arguments for %s */
5789 "%s arguments for %s", err,
5790 opnum ? PL_op_desc[opnum] : SvPV_nolen_const(cSVOP_sv)
5793 /* Reset the stack pointer. Without this, we end up returning our own
5794 arguments in list context, in addition to the values we are supposed
5795 to return. nextstate usually does this on sub entry, but we need
5796 to run the next op with the caller's hints, so we cannot have a
5798 SP = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;
5800 if(!maxargs) RETURN;
5802 /* We do this here, rather than with a separate pushmark op, as it has
5803 to come in between two things this function does (stack reset and
5804 arg pushing). This seems the easiest way to do it. */
5807 (void)Perl_pp_pushmark(aTHX);
5810 EXTEND(SP, maxargs == I32_MAX ? numargs : maxargs);
5811 PUTBACK; /* The code below can die in various places. */
5813 oa = PL_opargs[opnum] >> OASHIFT;
5814 for (; oa&&(numargs||!pushmark); (void)(numargs&&(++svp,--numargs))) {
5819 if (!numargs && defgv && whicharg == minargs + 1) {
5820 PUSHs(find_rundefsv2(
5821 find_runcv_where(FIND_RUNCV_level_eq, 1, NULL),
5822 cxstack[cxstack_ix].blk_oldcop->cop_seq
5825 else PUSHs(numargs ? svp && *svp ? *svp : &PL_sv_undef : NULL);
5829 PUSHs(svp && *svp ? *svp : &PL_sv_undef);
5834 if (!svp || !*svp || !SvROK(*svp)
5835 || SvTYPE(SvRV(*svp)) != SVt_PVHV)
5837 /* diag_listed_as: Type of arg %d to &CORE::%s must be %s*/
5838 "Type of arg %d to &CORE::%s must be hash reference",
5839 whicharg, OP_DESC(PL_op->op_next)
5844 if (!numargs) PUSHs(NULL);
5845 else if(svp && *svp && SvROK(*svp) && isGV_with_GP(SvRV(*svp)))
5846 /* no magic here, as the prototype will have added an extra
5847 refgen and we just want what was there before that */
5850 const bool constr = PL_op->op_private & whicharg;
5852 svp && *svp ? *svp : &PL_sv_undef,
5853 constr, CopHINTS_get(PL_curcop) & HINT_STRICT_REFS,
5859 if (!numargs) goto try_defsv;
5861 const bool wantscalar =
5862 PL_op->op_private & OPpCOREARGS_SCALARMOD;
5863 if (!svp || !*svp || !SvROK(*svp)
5864 /* We have to permit globrefs even for the \$ proto, as
5865 *foo is indistinguishable from ${\*foo}, and the proto-
5866 type permits the latter. */
5867 || SvTYPE(SvRV(*svp)) > (
5868 wantscalar ? SVt_PVLV
5869 : opnum == OP_LOCK || opnum == OP_UNDEF
5875 /* diag_listed_as: Type of arg %d to &CORE::%s must be %s*/
5876 "Type of arg %d to &CORE::%s must be %s",
5877 whicharg, PL_op_name[opnum],
5879 ? "scalar reference"
5880 : opnum == OP_LOCK || opnum == OP_UNDEF
5881 ? "reference to one of [$@%&*]"
5882 : "reference to one of [$@%*]"
5885 if (opnum == OP_UNDEF && SvRV(*svp) == (SV *)PL_defgv
5886 && cxstack[cxstack_ix].cx_type & CXp_HASARGS) {
5887 /* Undo @_ localisation, so that sub exit does not undo
5888 part of our undeffing. */
5889 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
5891 cx->cx_type &= ~ CXp_HASARGS;
5892 assert(!AvREAL(cx->blk_sub.argarray));
5897 DIE(aTHX_ "panic: unknown OA_*: %x", (unsigned)(oa&7));
5909 if (PL_op->op_private & OPpOFFBYONE) {
5910 cv = find_runcv_where(FIND_RUNCV_level_eq, 1, NULL);
5912 else cv = find_runcv(NULL);
5913 XPUSHs(CvEVAL(cv) ? &PL_sv_undef : sv_2mortal(newRV((SV *)cv)));
5920 * c-indentation-style: bsd
5922 * indent-tabs-mode: nil
5925 * ex: set ts=8 sts=4 sw=4 et: