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.
33 /* XXX I can't imagine anyone who doesn't have this actually _needs_
34 it, since pid_t is an integral type.
37 #ifdef NEED_GETPID_PROTO
38 extern Pid_t getpid (void);
42 * Some BSDs and Cygwin default to POSIX math instead of IEEE.
43 * This switches them over to IEEE.
45 #if defined(LIBM_LIB_VERSION)
46 _LIB_VERSION_TYPE _LIB_VERSION = _IEEE_;
49 /* variations on pp_null */
55 if (GIMME_V == G_SCALAR)
66 assert(SvTYPE(TARG) == SVt_PVAV);
67 if (PL_op->op_private & OPpLVAL_INTRO)
68 if (!(PL_op->op_private & OPpPAD_STATE))
69 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
71 if (PL_op->op_flags & OPf_REF) {
74 } else if (PL_op->op_private & OPpMAYBE_LVSUB) {
75 const I32 flags = is_lvalue_sub();
76 if (flags && !(flags & OPpENTERSUB_INARGS)) {
77 if (GIMME == G_SCALAR)
78 /* diag_listed_as: Can't return %s to lvalue scalar context */
79 Perl_croak(aTHX_ "Can't return array to lvalue scalar context");
85 if (gimme == G_ARRAY) {
86 const I32 maxarg = AvFILL(MUTABLE_AV(TARG)) + 1;
88 if (SvMAGICAL(TARG)) {
90 for (i=0; i < (U32)maxarg; i++) {
91 SV * const * const svp = av_fetch(MUTABLE_AV(TARG), i, FALSE);
92 SP[i+1] = (svp) ? *svp : &PL_sv_undef;
96 Copy(AvARRAY((const AV *)TARG), SP+1, maxarg, SV*);
100 else if (gimme == G_SCALAR) {
101 SV* const sv = sv_newmortal();
102 const I32 maxarg = AvFILL(MUTABLE_AV(TARG)) + 1;
103 sv_setiv(sv, maxarg);
114 assert(SvTYPE(TARG) == SVt_PVHV);
116 if (PL_op->op_private & OPpLVAL_INTRO)
117 if (!(PL_op->op_private & OPpPAD_STATE))
118 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
119 if (PL_op->op_flags & OPf_REF)
121 else if (PL_op->op_private & OPpMAYBE_LVSUB) {
122 const I32 flags = is_lvalue_sub();
123 if (flags && !(flags & OPpENTERSUB_INARGS)) {
124 if (GIMME == G_SCALAR)
125 /* diag_listed_as: Can't return %s to lvalue scalar context */
126 Perl_croak(aTHX_ "Can't return hash to lvalue scalar context");
131 if (gimme == G_ARRAY) {
132 RETURNOP(Perl_do_kv(aTHX));
134 else if (PL_op->op_private & OpMAYBE_TRUEBOOL
135 && block_gimme() == G_VOID)
136 SETs(boolSV(HvUSEDKEYS(TARG)));
137 else if (gimme == G_SCALAR) {
138 SV* const sv = Perl_hv_scalar(aTHX_ MUTABLE_HV(TARG));
146 static const char S_no_symref_sv[] =
147 "Can't use string (\"%" SVf32 "\"%s) as %s ref while \"strict refs\" in use";
149 /* In some cases this function inspects PL_op. If this function is called
150 for new op types, more bool parameters may need to be added in place of
153 When noinit is true, the absence of a gv will cause a retval of undef.
154 This is unrelated to the cv-to-gv assignment case.
158 S_rv2gv(pTHX_ SV *sv, const bool vivify_sv, const bool strict,
162 if (!isGV(sv) || SvFAKE(sv)) SvGETMAGIC(sv);
165 sv = amagic_deref_call(sv, to_gv_amg);
169 if (SvTYPE(sv) == SVt_PVIO) {
170 GV * const gv = MUTABLE_GV(sv_newmortal());
171 gv_init(gv, 0, "__ANONIO__", 10, 0);
172 GvIOp(gv) = MUTABLE_IO(sv);
173 SvREFCNT_inc_void_NN(sv);
176 else if (!isGV_with_GP(sv))
177 return (SV *)Perl_die(aTHX_ "Not a GLOB reference");
180 if (!isGV_with_GP(sv)) {
182 /* If this is a 'my' scalar and flag is set then vivify
185 if (vivify_sv && sv != &PL_sv_undef) {
188 Perl_croak_no_modify(aTHX);
189 if (cUNOP->op_targ) {
190 SV * const namesv = PAD_SV(cUNOP->op_targ);
191 gv = MUTABLE_GV(newSV(0));
192 gv_init_sv(gv, CopSTASH(PL_curcop), namesv, 0);
195 const char * const name = CopSTASHPV(PL_curcop);
196 gv = newGVgen_flags(name,
197 HvNAMEUTF8(CopSTASH(PL_curcop)) ? SVf_UTF8 : 0 );
199 prepare_SV_for_RV(sv);
200 SvRV_set(sv, MUTABLE_SV(gv));
205 if (PL_op->op_flags & OPf_REF || strict)
206 return (SV *)Perl_die(aTHX_ PL_no_usym, "a symbol");
207 if (ckWARN(WARN_UNINITIALIZED))
213 if (!(sv = MUTABLE_SV(gv_fetchsv_nomg(
214 sv, GV_ADDMG, SVt_PVGV
224 (SvPOKp(sv) && SvCUR(sv)>32 ? "..." : ""),
227 if ((PL_op->op_private & (OPpLVAL_INTRO|OPpDONT_INIT_GV))
228 == OPpDONT_INIT_GV) {
229 /* We are the target of a coderef assignment. Return
230 the scalar unchanged, and let pp_sasssign deal with
234 sv = MUTABLE_SV(gv_fetchsv_nomg(sv, GV_ADD, SVt_PVGV));
236 /* FAKE globs in the symbol table cause weird bugs (#77810) */
240 if (SvFAKE(sv) && !(PL_op->op_private & OPpALLOW_FAKE)) {
241 SV *newsv = sv_newmortal();
242 sv_setsv_flags(newsv, sv, 0);
254 sv, PL_op->op_private & OPpDEREF,
255 PL_op->op_private & HINT_STRICT_REFS,
256 ((PL_op->op_flags & OPf_SPECIAL) && !(PL_op->op_flags & OPf_MOD))
257 || PL_op->op_type == OP_READLINE
259 if (PL_op->op_private & OPpLVAL_INTRO)
260 save_gp(MUTABLE_GV(sv), !(PL_op->op_flags & OPf_SPECIAL));
265 /* Helper function for pp_rv2sv and pp_rv2av */
267 Perl_softref2xv(pTHX_ SV *const sv, const char *const what,
268 const svtype type, SV ***spp)
273 PERL_ARGS_ASSERT_SOFTREF2XV;
275 if (PL_op->op_private & HINT_STRICT_REFS) {
277 Perl_die(aTHX_ S_no_symref_sv, sv,
278 (SvPOKp(sv) && SvCUR(sv)>32 ? "..." : ""), what);
280 Perl_die(aTHX_ PL_no_usym, what);
284 PL_op->op_flags & OPf_REF &&
285 PL_op->op_next->op_type != OP_BOOLKEYS
287 Perl_die(aTHX_ PL_no_usym, what);
288 if (ckWARN(WARN_UNINITIALIZED))
290 if (type != SVt_PV && GIMME_V == G_ARRAY) {
294 **spp = &PL_sv_undef;
297 if ((PL_op->op_flags & OPf_SPECIAL) &&
298 !(PL_op->op_flags & OPf_MOD))
300 if (!(gv = gv_fetchsv_nomg(sv, GV_ADDMG, type)))
302 **spp = &PL_sv_undef;
307 gv = gv_fetchsv_nomg(sv, GV_ADD, type);
320 sv = amagic_deref_call(sv, to_sv_amg);
324 switch (SvTYPE(sv)) {
330 DIE(aTHX_ "Not a SCALAR reference");
337 if (!isGV_with_GP(gv)) {
338 gv = Perl_softref2xv(aTHX_ sv, "a SCALAR", SVt_PV, &sp);
344 if (PL_op->op_flags & OPf_MOD) {
345 if (PL_op->op_private & OPpLVAL_INTRO) {
346 if (cUNOP->op_first->op_type == OP_NULL)
347 sv = save_scalar(MUTABLE_GV(TOPs));
349 sv = save_scalar(gv);
351 Perl_croak(aTHX_ "%s", PL_no_localize_ref);
353 else if (PL_op->op_private & OPpDEREF)
354 sv = vivify_ref(sv, PL_op->op_private & OPpDEREF);
363 AV * const av = MUTABLE_AV(TOPs);
364 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
366 SV ** const sv = Perl_av_arylen_p(aTHX_ MUTABLE_AV(av));
368 *sv = newSV_type(SVt_PVMG);
369 sv_magic(*sv, MUTABLE_SV(av), PERL_MAGIC_arylen, NULL, 0);
373 SETs(sv_2mortal(newSViv(AvFILL(MUTABLE_AV(av)))));
382 if (PL_op->op_flags & OPf_MOD || LVRET) {
383 SV * const ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
384 sv_magic(ret, NULL, PERL_MAGIC_pos, NULL, 0);
386 LvTARG(ret) = SvREFCNT_inc_simple(sv);
387 PUSHs(ret); /* no SvSETMAGIC */
391 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
392 const MAGIC * const mg = mg_find(sv, PERL_MAGIC_regex_global);
393 if (mg && mg->mg_len >= 0) {
411 const I32 flags = (PL_op->op_flags & OPf_SPECIAL)
413 : ((PL_op->op_private & (OPpLVAL_INTRO|OPpMAY_RETURN_CONSTANT)) == OPpMAY_RETURN_CONSTANT)
416 /* We usually try to add a non-existent subroutine in case of AUTOLOAD. */
417 /* (But not in defined().) */
419 CV *cv = sv_2cv(TOPs, &stash_unused, &gv, flags);
421 else if ((flags == (GV_ADD|GV_NOEXPAND)) && gv && SvROK(gv)) {
425 cv = MUTABLE_CV(&PL_sv_undef);
426 SETs(MUTABLE_SV(cv));
436 SV *ret = &PL_sv_undef;
438 if (SvGMAGICAL(TOPs)) SETs(sv_mortalcopy(TOPs));
439 if (SvPOK(TOPs) && SvCUR(TOPs) >= 7) {
440 const char * s = SvPVX_const(TOPs);
441 if (strnEQ(s, "CORE::", 6)) {
442 const int code = keyword(s + 6, SvCUR(TOPs) - 6, 1);
443 if (!code || code == -KEY_CORE)
444 DIE(aTHX_ "Can't find an opnumber for \"%"SVf"\"",
445 SVfARG(newSVpvn_flags(
446 s+6, SvCUR(TOPs)-6, SvFLAGS(TOPs) & SVf_UTF8
449 SV * const sv = core_prototype(NULL, s + 6, code, NULL);
455 cv = sv_2cv(TOPs, &stash, &gv, 0);
457 ret = newSVpvn_flags(
458 CvPROTO(cv), CvPROTOLEN(cv), SVs_TEMP | SvUTF8(cv)
468 CV *cv = MUTABLE_CV(PAD_SV(PL_op->op_targ));
470 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
472 PUSHs(MUTABLE_SV(cv));
486 if (GIMME != G_ARRAY) {
490 *MARK = &PL_sv_undef;
491 *MARK = refto(*MARK);
495 EXTEND_MORTAL(SP - MARK);
497 *MARK = refto(*MARK);
502 S_refto(pTHX_ SV *sv)
507 PERL_ARGS_ASSERT_REFTO;
509 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
512 if (!(sv = LvTARG(sv)))
515 SvREFCNT_inc_void_NN(sv);
517 else if (SvTYPE(sv) == SVt_PVAV) {
518 if (!AvREAL((const AV *)sv) && AvREIFY((const AV *)sv))
519 av_reify(MUTABLE_AV(sv));
521 SvREFCNT_inc_void_NN(sv);
523 else if (SvPADTMP(sv) && !IS_PADGV(sv))
527 SvREFCNT_inc_void_NN(sv);
530 sv_upgrade(rv, SVt_IV);
539 SV * const sv = POPs;
544 if (!sv || !SvROK(sv))
547 (void)sv_ref(TARG,SvRV(sv),TRUE);
559 stash = CopSTASH(PL_curcop);
561 SV * const ssv = POPs;
565 if (!ssv) goto curstash;
566 if (!SvGMAGICAL(ssv) && !SvAMAGIC(ssv) && SvROK(ssv))
567 Perl_croak(aTHX_ "Attempt to bless into a reference");
568 ptr = SvPV_const(ssv,len);
570 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
571 "Explicit blessing to '' (assuming package main)");
572 stash = gv_stashpvn(ptr, len, GV_ADD|SvUTF8(ssv));
575 (void)sv_bless(TOPs, stash);
585 const char * const elem = SvPV_const(sv, len);
586 GV * const gv = MUTABLE_GV(POPs);
591 /* elem will always be NUL terminated. */
592 const char * const second_letter = elem + 1;
595 if (len == 5 && strEQ(second_letter, "RRAY"))
596 tmpRef = MUTABLE_SV(GvAV(gv));
599 if (len == 4 && strEQ(second_letter, "ODE"))
600 tmpRef = MUTABLE_SV(GvCVu(gv));
603 if (len == 10 && strEQ(second_letter, "ILEHANDLE")) {
604 /* finally deprecated in 5.8.0 */
605 deprecate("*glob{FILEHANDLE}");
606 tmpRef = MUTABLE_SV(GvIOp(gv));
609 if (len == 6 && strEQ(second_letter, "ORMAT"))
610 tmpRef = MUTABLE_SV(GvFORM(gv));
613 if (len == 4 && strEQ(second_letter, "LOB"))
614 tmpRef = MUTABLE_SV(gv);
617 if (len == 4 && strEQ(second_letter, "ASH"))
618 tmpRef = MUTABLE_SV(GvHV(gv));
621 if (*second_letter == 'O' && !elem[2] && len == 2)
622 tmpRef = MUTABLE_SV(GvIOp(gv));
625 if (len == 4 && strEQ(second_letter, "AME"))
626 sv = newSVhek(GvNAME_HEK(gv));
629 if (len == 7 && strEQ(second_letter, "ACKAGE")) {
630 const HV * const stash = GvSTASH(gv);
631 const HEK * const hek = stash ? HvNAME_HEK(stash) : NULL;
632 sv = hek ? newSVhek(hek) : newSVpvs("__ANON__");
636 if (len == 6 && strEQ(second_letter, "CALAR"))
651 /* Pattern matching */
659 if (len == 0 || len > I32_MAX || !SvPOK(sv) || SvUTF8(sv) || SvVALID(sv)) {
660 /* Historically, study was skipped in these cases. */
664 /* Make study a no-op. It's no longer useful and its existence
665 complicates matters elsewhere. */
674 if (PL_op->op_flags & OPf_STACKED)
676 else if (PL_op->op_private & OPpTARGET_MY)
682 if(PL_op->op_type == OP_TRANSR) {
684 const char * const pv = SvPV(sv,len);
685 SV * const newsv = newSVpvn_flags(pv, len, SVs_TEMP|SvUTF8(sv));
690 TARG = sv_newmortal();
696 /* Lvalue operators. */
699 S_do_chomp(pTHX_ SV *retval, SV *sv, bool chomping)
705 PERL_ARGS_ASSERT_DO_CHOMP;
707 if (chomping && (RsSNARF(PL_rs) || RsRECORD(PL_rs)))
709 if (SvTYPE(sv) == SVt_PVAV) {
711 AV *const av = MUTABLE_AV(sv);
712 const I32 max = AvFILL(av);
714 for (i = 0; i <= max; i++) {
715 sv = MUTABLE_SV(av_fetch(av, i, FALSE));
716 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
717 do_chomp(retval, sv, chomping);
721 else if (SvTYPE(sv) == SVt_PVHV) {
722 HV* const hv = MUTABLE_HV(sv);
724 (void)hv_iterinit(hv);
725 while ((entry = hv_iternext(hv)))
726 do_chomp(retval, hv_iterval(hv,entry), chomping);
729 else if (SvREADONLY(sv)) {
731 /* SV is copy-on-write */
732 sv_force_normal_flags(sv, 0);
735 Perl_croak_no_modify(aTHX);
740 /* XXX, here sv is utf8-ized as a side-effect!
741 If encoding.pm is used properly, almost string-generating
742 operations, including literal strings, chr(), input data, etc.
743 should have been utf8-ized already, right?
745 sv_recode_to_utf8(sv, PL_encoding);
751 char *temp_buffer = NULL;
760 while (len && s[-1] == '\n') {
767 STRLEN rslen, rs_charlen;
768 const char *rsptr = SvPV_const(PL_rs, rslen);
770 rs_charlen = SvUTF8(PL_rs)
774 if (SvUTF8(PL_rs) != SvUTF8(sv)) {
775 /* Assumption is that rs is shorter than the scalar. */
777 /* RS is utf8, scalar is 8 bit. */
779 temp_buffer = (char*)bytes_from_utf8((U8*)rsptr,
782 /* Cannot downgrade, therefore cannot possibly match
784 assert (temp_buffer == rsptr);
790 else if (PL_encoding) {
791 /* RS is 8 bit, encoding.pm is used.
792 * Do not recode PL_rs as a side-effect. */
793 svrecode = newSVpvn(rsptr, rslen);
794 sv_recode_to_utf8(svrecode, PL_encoding);
795 rsptr = SvPV_const(svrecode, rslen);
796 rs_charlen = sv_len_utf8(svrecode);
799 /* RS is 8 bit, scalar is utf8. */
800 temp_buffer = (char*)bytes_to_utf8((U8*)rsptr, &rslen);
814 if (memNE(s, rsptr, rslen))
816 SvIVX(retval) += rs_charlen;
819 s = SvPV_force_nomg_nolen(sv);
827 SvREFCNT_dec(svrecode);
829 Safefree(temp_buffer);
831 if (len && !SvPOK(sv))
832 s = SvPV_force_nomg(sv, len);
835 char * const send = s + len;
836 char * const start = s;
838 while (s > start && UTF8_IS_CONTINUATION(*s))
840 if (is_utf8_string((U8*)s, send - s)) {
841 sv_setpvn(retval, s, send - s);
843 SvCUR_set(sv, s - start);
849 sv_setpvs(retval, "");
853 sv_setpvn(retval, s, 1);
860 sv_setpvs(retval, "");
868 const bool chomping = PL_op->op_type == OP_SCHOMP;
872 do_chomp(TARG, TOPs, chomping);
879 dVAR; dSP; dMARK; dTARGET; dORIGMARK;
880 const bool chomping = PL_op->op_type == OP_CHOMP;
885 do_chomp(TARG, *++MARK, chomping);
896 if (!PL_op->op_private) {
905 SV_CHECK_THINKFIRST_COW_DROP(sv);
907 switch (SvTYPE(sv)) {
911 av_undef(MUTABLE_AV(sv));
914 hv_undef(MUTABLE_HV(sv));
917 if (cv_const_sv((const CV *)sv))
918 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
919 "Constant subroutine %"SVf" undefined",
920 SVfARG(CvANON((const CV *)sv)
921 ? newSVpvs_flags("(anonymous)", SVs_TEMP)
922 : sv_2mortal(newSVhek(GvENAME_HEK(CvGV((const CV *)sv))))));
926 /* let user-undef'd sub keep its identity */
927 GV* const gv = CvGV((const CV *)sv);
928 cv_undef(MUTABLE_CV(sv));
929 CvGV_set(MUTABLE_CV(sv), gv);
934 SvSetMagicSV(sv, &PL_sv_undef);
937 else if (isGV_with_GP(sv)) {
941 /* undef *Pkg::meth_name ... */
943 = GvCVu((const GV *)sv) && (stash = GvSTASH((const GV *)sv))
944 && HvENAME_get(stash);
946 if((stash = GvHV((const GV *)sv))) {
947 if(HvENAME_get(stash))
948 SvREFCNT_inc_simple_void_NN(sv_2mortal((SV *)stash));
952 gp_free(MUTABLE_GV(sv));
954 GvGP_set(sv, gp_ref(gp));
956 GvLINE(sv) = CopLINE(PL_curcop);
957 GvEGV(sv) = MUTABLE_GV(sv);
961 mro_package_moved(NULL, stash, (const GV *)sv, 0);
963 /* undef *Foo::ISA */
964 if( strEQ(GvNAME((const GV *)sv), "ISA")
965 && (stash = GvSTASH((const GV *)sv))
966 && (method_changed || HvENAME(stash)) )
967 mro_isa_changed_in(stash);
968 else if(method_changed)
969 mro_method_changed_in(
970 GvSTASH((const GV *)sv)
977 if (SvTYPE(sv) >= SVt_PV && SvPVX_const(sv) && SvLEN(sv)) {
993 PL_op->op_type == OP_POSTINC || PL_op->op_type == OP_I_POSTINC;
994 if (SvTYPE(TOPs) >= SVt_PVAV || (isGV_with_GP(TOPs) && !SvFAKE(TOPs)))
995 Perl_croak_no_modify(aTHX);
997 TARG = sv_newmortal();
998 sv_setsv(TARG, TOPs);
999 if (!SvREADONLY(TOPs) && !SvGMAGICAL(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
1000 && SvIVX(TOPs) != (inc ? IV_MAX : IV_MIN))
1002 SvIV_set(TOPs, SvIVX(TOPs) + (inc ? 1 : -1));
1003 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
1007 else sv_dec_nomg(TOPs);
1009 /* special case for undef: see thread at 2003-03/msg00536.html in archive */
1010 if (inc && !SvOK(TARG))
1016 /* Ordinary operators. */
1020 dVAR; dSP; dATARGET; SV *svl, *svr;
1021 #ifdef PERL_PRESERVE_IVUV
1024 tryAMAGICbin_MG(pow_amg, AMGf_assign|AMGf_numeric);
1027 #ifdef PERL_PRESERVE_IVUV
1028 /* For integer to integer power, we do the calculation by hand wherever
1029 we're sure it is safe; otherwise we call pow() and try to convert to
1030 integer afterwards. */
1031 if (SvIV_please_nomg(svr) && SvIV_please_nomg(svl)) {
1039 const IV iv = SvIVX(svr);
1043 goto float_it; /* Can't do negative powers this way. */
1047 baseuok = SvUOK(svl);
1049 baseuv = SvUVX(svl);
1051 const IV iv = SvIVX(svl);
1054 baseuok = TRUE; /* effectively it's a UV now */
1056 baseuv = -iv; /* abs, baseuok == false records sign */
1059 /* now we have integer ** positive integer. */
1062 /* foo & (foo - 1) is zero only for a power of 2. */
1063 if (!(baseuv & (baseuv - 1))) {
1064 /* We are raising power-of-2 to a positive integer.
1065 The logic here will work for any base (even non-integer
1066 bases) but it can be less accurate than
1067 pow (base,power) or exp (power * log (base)) when the
1068 intermediate values start to spill out of the mantissa.
1069 With powers of 2 we know this can't happen.
1070 And powers of 2 are the favourite thing for perl
1071 programmers to notice ** not doing what they mean. */
1073 NV base = baseuok ? baseuv : -(NV)baseuv;
1078 while (power >>= 1) {
1086 SvIV_please_nomg(svr);
1089 unsigned int highbit = 8 * sizeof(UV);
1090 unsigned int diff = 8 * sizeof(UV);
1091 while (diff >>= 1) {
1093 if (baseuv >> highbit) {
1097 /* we now have baseuv < 2 ** highbit */
1098 if (power * highbit <= 8 * sizeof(UV)) {
1099 /* result will definitely fit in UV, so use UV math
1100 on same algorithm as above */
1103 const bool odd_power = cBOOL(power & 1);
1107 while (power >>= 1) {
1114 if (baseuok || !odd_power)
1115 /* answer is positive */
1117 else if (result <= (UV)IV_MAX)
1118 /* answer negative, fits in IV */
1119 SETi( -(IV)result );
1120 else if (result == (UV)IV_MIN)
1121 /* 2's complement assumption: special case IV_MIN */
1124 /* answer negative, doesn't fit */
1125 SETn( -(NV)result );
1133 NV right = SvNV_nomg(svr);
1134 NV left = SvNV_nomg(svl);
1137 #if defined(USE_LONG_DOUBLE) && defined(HAS_AIX_POWL_NEG_BASE_BUG)
1139 We are building perl with long double support and are on an AIX OS
1140 afflicted with a powl() function that wrongly returns NaNQ for any
1141 negative base. This was reported to IBM as PMR #23047-379 on
1142 03/06/2006. The problem exists in at least the following versions
1143 of AIX and the libm fileset, and no doubt others as well:
1145 AIX 4.3.3-ML10 bos.adt.libm 4.3.3.50
1146 AIX 5.1.0-ML04 bos.adt.libm 5.1.0.29
1147 AIX 5.2.0 bos.adt.libm 5.2.0.85
1149 So, until IBM fixes powl(), we provide the following workaround to
1150 handle the problem ourselves. Our logic is as follows: for
1151 negative bases (left), we use fmod(right, 2) to check if the
1152 exponent is an odd or even integer:
1154 - if odd, powl(left, right) == -powl(-left, right)
1155 - if even, powl(left, right) == powl(-left, right)
1157 If the exponent is not an integer, the result is rightly NaNQ, so
1158 we just return that (as NV_NAN).
1162 NV mod2 = Perl_fmod( right, 2.0 );
1163 if (mod2 == 1.0 || mod2 == -1.0) { /* odd integer */
1164 SETn( -Perl_pow( -left, right) );
1165 } else if (mod2 == 0.0) { /* even integer */
1166 SETn( Perl_pow( -left, right) );
1167 } else { /* fractional power */
1171 SETn( Perl_pow( left, right) );
1174 SETn( Perl_pow( left, right) );
1175 #endif /* HAS_AIX_POWL_NEG_BASE_BUG */
1177 #ifdef PERL_PRESERVE_IVUV
1179 SvIV_please_nomg(svr);
1187 dVAR; dSP; dATARGET; SV *svl, *svr;
1188 tryAMAGICbin_MG(mult_amg, AMGf_assign|AMGf_numeric);
1191 #ifdef PERL_PRESERVE_IVUV
1192 if (SvIV_please_nomg(svr)) {
1193 /* Unless the left argument is integer in range we are going to have to
1194 use NV maths. Hence only attempt to coerce the right argument if
1195 we know the left is integer. */
1196 /* Left operand is defined, so is it IV? */
1197 if (SvIV_please_nomg(svl)) {
1198 bool auvok = SvUOK(svl);
1199 bool buvok = SvUOK(svr);
1200 const UV topmask = (~ (UV)0) << (4 * sizeof (UV));
1201 const UV botmask = ~((~ (UV)0) << (4 * sizeof (UV)));
1210 const IV aiv = SvIVX(svl);
1213 auvok = TRUE; /* effectively it's a UV now */
1215 alow = -aiv; /* abs, auvok == false records sign */
1221 const IV biv = SvIVX(svr);
1224 buvok = TRUE; /* effectively it's a UV now */
1226 blow = -biv; /* abs, buvok == false records sign */
1230 /* If this does sign extension on unsigned it's time for plan B */
1231 ahigh = alow >> (4 * sizeof (UV));
1233 bhigh = blow >> (4 * sizeof (UV));
1235 if (ahigh && bhigh) {
1237 /* eg 32 bit is at least 0x10000 * 0x10000 == 0x100000000
1238 which is overflow. Drop to NVs below. */
1239 } else if (!ahigh && !bhigh) {
1240 /* eg 32 bit is at most 0xFFFF * 0xFFFF == 0xFFFE0001
1241 so the unsigned multiply cannot overflow. */
1242 const UV product = alow * blow;
1243 if (auvok == buvok) {
1244 /* -ve * -ve or +ve * +ve gives a +ve result. */
1248 } else if (product <= (UV)IV_MIN) {
1249 /* 2s complement assumption that (UV)-IV_MIN is correct. */
1250 /* -ve result, which could overflow an IV */
1252 SETi( -(IV)product );
1254 } /* else drop to NVs below. */
1256 /* One operand is large, 1 small */
1259 /* swap the operands */
1261 bhigh = blow; /* bhigh now the temp var for the swap */
1265 /* now, ((ahigh * blow) << half_UV_len) + (alow * blow)
1266 multiplies can't overflow. shift can, add can, -ve can. */
1267 product_middle = ahigh * blow;
1268 if (!(product_middle & topmask)) {
1269 /* OK, (ahigh * blow) won't lose bits when we shift it. */
1271 product_middle <<= (4 * sizeof (UV));
1272 product_low = alow * blow;
1274 /* as for pp_add, UV + something mustn't get smaller.
1275 IIRC ANSI mandates this wrapping *behaviour* for
1276 unsigned whatever the actual representation*/
1277 product_low += product_middle;
1278 if (product_low >= product_middle) {
1279 /* didn't overflow */
1280 if (auvok == buvok) {
1281 /* -ve * -ve or +ve * +ve gives a +ve result. */
1283 SETu( product_low );
1285 } else if (product_low <= (UV)IV_MIN) {
1286 /* 2s complement assumption again */
1287 /* -ve result, which could overflow an IV */
1289 SETi( -(IV)product_low );
1291 } /* else drop to NVs below. */
1293 } /* product_middle too large */
1294 } /* ahigh && bhigh */
1299 NV right = SvNV_nomg(svr);
1300 NV left = SvNV_nomg(svl);
1302 SETn( left * right );
1309 dVAR; dSP; dATARGET; SV *svl, *svr;
1310 tryAMAGICbin_MG(div_amg, AMGf_assign|AMGf_numeric);
1313 /* Only try to do UV divide first
1314 if ((SLOPPYDIVIDE is true) or
1315 (PERL_PRESERVE_IVUV is true and one or both SV is a UV too large
1317 The assumption is that it is better to use floating point divide
1318 whenever possible, only doing integer divide first if we can't be sure.
1319 If NV_PRESERVES_UV is true then we know at compile time that no UV
1320 can be too large to preserve, so don't need to compile the code to
1321 test the size of UVs. */
1324 # define PERL_TRY_UV_DIVIDE
1325 /* ensure that 20./5. == 4. */
1327 # ifdef PERL_PRESERVE_IVUV
1328 # ifndef NV_PRESERVES_UV
1329 # define PERL_TRY_UV_DIVIDE
1334 #ifdef PERL_TRY_UV_DIVIDE
1335 if (SvIV_please_nomg(svr) && SvIV_please_nomg(svl)) {
1336 bool left_non_neg = SvUOK(svl);
1337 bool right_non_neg = SvUOK(svr);
1341 if (right_non_neg) {
1345 const IV biv = SvIVX(svr);
1348 right_non_neg = TRUE; /* effectively it's a UV now */
1354 /* historically undef()/0 gives a "Use of uninitialized value"
1355 warning before dieing, hence this test goes here.
1356 If it were immediately before the second SvIV_please, then
1357 DIE() would be invoked before left was even inspected, so
1358 no inspection would give no warning. */
1360 DIE(aTHX_ "Illegal division by zero");
1366 const IV aiv = SvIVX(svl);
1369 left_non_neg = TRUE; /* effectively it's a UV now */
1378 /* For sloppy divide we always attempt integer division. */
1380 /* Otherwise we only attempt it if either or both operands
1381 would not be preserved by an NV. If both fit in NVs
1382 we fall through to the NV divide code below. However,
1383 as left >= right to ensure integer result here, we know that
1384 we can skip the test on the right operand - right big
1385 enough not to be preserved can't get here unless left is
1388 && (left > ((UV)1 << NV_PRESERVES_UV_BITS))
1391 /* Integer division can't overflow, but it can be imprecise. */
1392 const UV result = left / right;
1393 if (result * right == left) {
1394 SP--; /* result is valid */
1395 if (left_non_neg == right_non_neg) {
1396 /* signs identical, result is positive. */
1400 /* 2s complement assumption */
1401 if (result <= (UV)IV_MIN)
1402 SETi( -(IV)result );
1404 /* It's exact but too negative for IV. */
1405 SETn( -(NV)result );
1408 } /* tried integer divide but it was not an integer result */
1409 } /* else (PERL_ABS(result) < 1.0) or (both UVs in range for NV) */
1410 } /* one operand wasn't SvIOK */
1411 #endif /* PERL_TRY_UV_DIVIDE */
1413 NV right = SvNV_nomg(svr);
1414 NV left = SvNV_nomg(svl);
1415 (void)POPs;(void)POPs;
1416 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
1417 if (! Perl_isnan(right) && right == 0.0)
1421 DIE(aTHX_ "Illegal division by zero");
1422 PUSHn( left / right );
1429 dVAR; dSP; dATARGET;
1430 tryAMAGICbin_MG(modulo_amg, AMGf_assign|AMGf_numeric);
1434 bool left_neg = FALSE;
1435 bool right_neg = FALSE;
1436 bool use_double = FALSE;
1437 bool dright_valid = FALSE;
1440 SV * const svr = TOPs;
1441 SV * const svl = TOPm1s;
1442 if (SvIV_please_nomg(svr)) {
1443 right_neg = !SvUOK(svr);
1447 const IV biv = SvIVX(svr);
1450 right_neg = FALSE; /* effectively it's a UV now */
1457 dright = SvNV_nomg(svr);
1458 right_neg = dright < 0;
1461 if (dright < UV_MAX_P1) {
1462 right = U_V(dright);
1463 dright_valid = TRUE; /* In case we need to use double below. */
1469 /* At this point use_double is only true if right is out of range for
1470 a UV. In range NV has been rounded down to nearest UV and
1471 use_double false. */
1472 if (!use_double && SvIV_please_nomg(svl)) {
1473 left_neg = !SvUOK(svl);
1477 const IV aiv = SvIVX(svl);
1480 left_neg = FALSE; /* effectively it's a UV now */
1487 dleft = SvNV_nomg(svl);
1488 left_neg = dleft < 0;
1492 /* This should be exactly the 5.6 behaviour - if left and right are
1493 both in range for UV then use U_V() rather than floor. */
1495 if (dleft < UV_MAX_P1) {
1496 /* right was in range, so is dleft, so use UVs not double.
1500 /* left is out of range for UV, right was in range, so promote
1501 right (back) to double. */
1503 /* The +0.5 is used in 5.6 even though it is not strictly
1504 consistent with the implicit +0 floor in the U_V()
1505 inside the #if 1. */
1506 dleft = Perl_floor(dleft + 0.5);
1509 dright = Perl_floor(dright + 0.5);
1520 DIE(aTHX_ "Illegal modulus zero");
1522 dans = Perl_fmod(dleft, dright);
1523 if ((left_neg != right_neg) && dans)
1524 dans = dright - dans;
1527 sv_setnv(TARG, dans);
1533 DIE(aTHX_ "Illegal modulus zero");
1536 if ((left_neg != right_neg) && ans)
1539 /* XXX may warn: unary minus operator applied to unsigned type */
1540 /* could change -foo to be (~foo)+1 instead */
1541 if (ans <= ~((UV)IV_MAX)+1)
1542 sv_setiv(TARG, ~ans+1);
1544 sv_setnv(TARG, -(NV)ans);
1547 sv_setuv(TARG, ans);
1556 dVAR; dSP; dATARGET;
1560 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1561 /* TODO: think of some way of doing list-repeat overloading ??? */
1566 tryAMAGICbin_MG(repeat_amg, AMGf_assign);
1572 const UV uv = SvUV_nomg(sv);
1574 count = IV_MAX; /* The best we can do? */
1578 const IV iv = SvIV_nomg(sv);
1585 else if (SvNOKp(sv)) {
1586 const NV nv = SvNV_nomg(sv);
1593 count = SvIV_nomg(sv);
1595 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1597 static const char oom_list_extend[] = "Out of memory during list extend";
1598 const I32 items = SP - MARK;
1599 const I32 max = items * count;
1601 MEM_WRAP_CHECK_1(max, SV*, oom_list_extend);
1602 /* Did the max computation overflow? */
1603 if (items > 0 && max > 0 && (max < items || max < count))
1604 Perl_croak(aTHX_ oom_list_extend);
1609 /* This code was intended to fix 20010809.028:
1612 for (($x =~ /./g) x 2) {
1613 print chop; # "abcdabcd" expected as output.
1616 * but that change (#11635) broke this code:
1618 $x = [("foo")x2]; # only one "foo" ended up in the anonlist.
1620 * I can't think of a better fix that doesn't introduce
1621 * an efficiency hit by copying the SVs. The stack isn't
1622 * refcounted, and mortalisation obviously doesn't
1623 * Do The Right Thing when the stack has more than
1624 * one pointer to the same mortal value.
1628 *SP = sv_2mortal(newSVsv(*SP));
1638 repeatcpy((char*)(MARK + items), (char*)MARK,
1639 items * sizeof(const SV *), count - 1);
1642 else if (count <= 0)
1645 else { /* Note: mark already snarfed by pp_list */
1646 SV * const tmpstr = POPs;
1649 static const char oom_string_extend[] =
1650 "Out of memory during string extend";
1653 sv_setsv_nomg(TARG, tmpstr);
1654 SvPV_force_nomg(TARG, len);
1655 isutf = DO_UTF8(TARG);
1660 const STRLEN max = (UV)count * len;
1661 if (len > MEM_SIZE_MAX / count)
1662 Perl_croak(aTHX_ oom_string_extend);
1663 MEM_WRAP_CHECK_1(max, char, oom_string_extend);
1664 SvGROW(TARG, max + 1);
1665 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
1666 SvCUR_set(TARG, SvCUR(TARG) * count);
1668 *SvEND(TARG) = '\0';
1671 (void)SvPOK_only_UTF8(TARG);
1673 (void)SvPOK_only(TARG);
1675 if (PL_op->op_private & OPpREPEAT_DOLIST) {
1676 /* The parser saw this as a list repeat, and there
1677 are probably several items on the stack. But we're
1678 in scalar context, and there's no pp_list to save us
1679 now. So drop the rest of the items -- robin@kitsite.com
1691 dVAR; dSP; dATARGET; bool useleft; SV *svl, *svr;
1692 tryAMAGICbin_MG(subtr_amg, AMGf_assign|AMGf_numeric);
1695 useleft = USE_LEFT(svl);
1696 #ifdef PERL_PRESERVE_IVUV
1697 /* See comments in pp_add (in pp_hot.c) about Overflow, and how
1698 "bad things" happen if you rely on signed integers wrapping. */
1699 if (SvIV_please_nomg(svr)) {
1700 /* Unless the left argument is integer in range we are going to have to
1701 use NV maths. Hence only attempt to coerce the right argument if
1702 we know the left is integer. */
1709 a_valid = auvok = 1;
1710 /* left operand is undef, treat as zero. */
1712 /* Left operand is defined, so is it IV? */
1713 if (SvIV_please_nomg(svl)) {
1714 if ((auvok = SvUOK(svl)))
1717 const IV aiv = SvIVX(svl);
1720 auvok = 1; /* Now acting as a sign flag. */
1721 } else { /* 2s complement assumption for IV_MIN */
1729 bool result_good = 0;
1732 bool buvok = SvUOK(svr);
1737 const IV biv = SvIVX(svr);
1744 /* ?uvok if value is >= 0. basically, flagged as UV if it's +ve,
1745 else "IV" now, independent of how it came in.
1746 if a, b represents positive, A, B negative, a maps to -A etc
1751 all UV maths. negate result if A negative.
1752 subtract if signs same, add if signs differ. */
1754 if (auvok ^ buvok) {
1763 /* Must get smaller */
1768 if (result <= buv) {
1769 /* result really should be -(auv-buv). as its negation
1770 of true value, need to swap our result flag */
1782 if (result <= (UV)IV_MIN)
1783 SETi( -(IV)result );
1785 /* result valid, but out of range for IV. */
1786 SETn( -(NV)result );
1790 } /* Overflow, drop through to NVs. */
1795 NV value = SvNV_nomg(svr);
1799 /* left operand is undef, treat as zero - value */
1803 SETn( SvNV_nomg(svl) - value );
1810 dVAR; dSP; dATARGET; SV *svl, *svr;
1811 tryAMAGICbin_MG(lshift_amg, AMGf_assign|AMGf_numeric);
1815 const IV shift = SvIV_nomg(svr);
1816 if (PL_op->op_private & HINT_INTEGER) {
1817 const IV i = SvIV_nomg(svl);
1821 const UV u = SvUV_nomg(svl);
1830 dVAR; dSP; dATARGET; SV *svl, *svr;
1831 tryAMAGICbin_MG(rshift_amg, AMGf_assign|AMGf_numeric);
1835 const IV shift = SvIV_nomg(svr);
1836 if (PL_op->op_private & HINT_INTEGER) {
1837 const IV i = SvIV_nomg(svl);
1841 const UV u = SvUV_nomg(svl);
1853 tryAMAGICbin_MG(lt_amg, AMGf_set|AMGf_numeric);
1857 (SvIOK_notUV(left) && SvIOK_notUV(right))
1858 ? (SvIVX(left) < SvIVX(right))
1859 : (do_ncmp(left, right) == -1)
1869 tryAMAGICbin_MG(gt_amg, AMGf_set|AMGf_numeric);
1873 (SvIOK_notUV(left) && SvIOK_notUV(right))
1874 ? (SvIVX(left) > SvIVX(right))
1875 : (do_ncmp(left, right) == 1)
1885 tryAMAGICbin_MG(le_amg, AMGf_set|AMGf_numeric);
1889 (SvIOK_notUV(left) && SvIOK_notUV(right))
1890 ? (SvIVX(left) <= SvIVX(right))
1891 : (do_ncmp(left, right) <= 0)
1901 tryAMAGICbin_MG(ge_amg, AMGf_set|AMGf_numeric);
1905 (SvIOK_notUV(left) && SvIOK_notUV(right))
1906 ? (SvIVX(left) >= SvIVX(right))
1907 : ( (do_ncmp(left, right) & 2) == 0)
1917 tryAMAGICbin_MG(ne_amg, AMGf_set|AMGf_numeric);
1921 (SvIOK_notUV(left) && SvIOK_notUV(right))
1922 ? (SvIVX(left) != SvIVX(right))
1923 : (do_ncmp(left, right) != 0)
1928 /* compare left and right SVs. Returns:
1932 * 2: left or right was a NaN
1935 Perl_do_ncmp(pTHX_ SV* const left, SV * const right)
1939 PERL_ARGS_ASSERT_DO_NCMP;
1940 #ifdef PERL_PRESERVE_IVUV
1941 /* Fortunately it seems NaN isn't IOK */
1942 if (SvIV_please_nomg(right) && SvIV_please_nomg(left)) {
1944 const IV leftiv = SvIVX(left);
1945 if (!SvUOK(right)) {
1946 /* ## IV <=> IV ## */
1947 const IV rightiv = SvIVX(right);
1948 return (leftiv > rightiv) - (leftiv < rightiv);
1950 /* ## IV <=> UV ## */
1952 /* As (b) is a UV, it's >=0, so it must be < */
1955 const UV rightuv = SvUVX(right);
1956 return ((UV)leftiv > rightuv) - ((UV)leftiv < rightuv);
1961 /* ## UV <=> UV ## */
1962 const UV leftuv = SvUVX(left);
1963 const UV rightuv = SvUVX(right);
1964 return (leftuv > rightuv) - (leftuv < rightuv);
1966 /* ## UV <=> IV ## */
1968 const IV rightiv = SvIVX(right);
1970 /* As (a) is a UV, it's >=0, so it cannot be < */
1973 const UV leftuv = SvUVX(left);
1974 return (leftuv > (UV)rightiv) - (leftuv < (UV)rightiv);
1977 assert(0); /* NOTREACHED */
1981 NV const rnv = SvNV_nomg(right);
1982 NV const lnv = SvNV_nomg(left);
1984 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
1985 if (Perl_isnan(lnv) || Perl_isnan(rnv)) {
1988 return (lnv > rnv) - (lnv < rnv);
2007 tryAMAGICbin_MG(ncmp_amg, AMGf_numeric);
2010 value = do_ncmp(left, right);
2025 int amg_type = sle_amg;
2029 switch (PL_op->op_type) {
2048 tryAMAGICbin_MG(amg_type, AMGf_set);
2051 const int cmp = (IN_LOCALE_RUNTIME
2052 ? sv_cmp_locale_flags(left, right, 0)
2053 : sv_cmp_flags(left, right, 0));
2054 SETs(boolSV(cmp * multiplier < rhs));
2062 tryAMAGICbin_MG(seq_amg, AMGf_set);
2065 SETs(boolSV(sv_eq_flags(left, right, 0)));
2073 tryAMAGICbin_MG(sne_amg, AMGf_set);
2076 SETs(boolSV(!sv_eq_flags(left, right, 0)));
2084 tryAMAGICbin_MG(scmp_amg, 0);
2087 const int cmp = (IN_LOCALE_RUNTIME
2088 ? sv_cmp_locale_flags(left, right, 0)
2089 : sv_cmp_flags(left, right, 0));
2097 dVAR; dSP; dATARGET;
2098 tryAMAGICbin_MG(band_amg, AMGf_assign);
2101 if (SvNIOKp(left) || SvNIOKp(right)) {
2102 const bool left_ro_nonnum = !SvNIOKp(left) && SvREADONLY(left);
2103 const bool right_ro_nonnum = !SvNIOKp(right) && SvREADONLY(right);
2104 if (PL_op->op_private & HINT_INTEGER) {
2105 const IV i = SvIV_nomg(left) & SvIV_nomg(right);
2109 const UV u = SvUV_nomg(left) & SvUV_nomg(right);
2112 if (left_ro_nonnum && left != TARG) SvNIOK_off(left);
2113 if (right_ro_nonnum) SvNIOK_off(right);
2116 do_vop(PL_op->op_type, TARG, left, right);
2125 dVAR; dSP; dATARGET;
2126 const int op_type = PL_op->op_type;
2128 tryAMAGICbin_MG((op_type == OP_BIT_OR ? bor_amg : bxor_amg), AMGf_assign);
2131 if (SvNIOKp(left) || SvNIOKp(right)) {
2132 const bool left_ro_nonnum = !SvNIOKp(left) && SvREADONLY(left);
2133 const bool right_ro_nonnum = !SvNIOKp(right) && SvREADONLY(right);
2134 if (PL_op->op_private & HINT_INTEGER) {
2135 const IV l = (USE_LEFT(left) ? SvIV_nomg(left) : 0);
2136 const IV r = SvIV_nomg(right);
2137 const IV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2141 const UV l = (USE_LEFT(left) ? SvUV_nomg(left) : 0);
2142 const UV r = SvUV_nomg(right);
2143 const UV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2146 if (left_ro_nonnum && left != TARG) SvNIOK_off(left);
2147 if (right_ro_nonnum) SvNIOK_off(right);
2150 do_vop(op_type, TARG, left, right);
2157 PERL_STATIC_INLINE bool
2158 S_negate_string(pTHX)
2163 SV * const sv = TOPs;
2164 if (!SvPOKp(sv) || SvNIOK(sv) || (!SvPOK(sv) && SvNIOKp(sv)))
2166 s = SvPV_nomg_const(sv, len);
2167 if (isIDFIRST(*s)) {
2168 sv_setpvs(TARG, "-");
2171 else if (*s == '+' || (*s == '-' && !looks_like_number(sv))) {
2172 sv_setsv_nomg(TARG, sv);
2173 *SvPV_force_nomg(TARG, len) = *s == '-' ? '+' : '-';
2183 tryAMAGICun_MG(neg_amg, AMGf_numeric);
2184 if (S_negate_string(aTHX)) return NORMAL;
2186 SV * const sv = TOPs;
2189 /* It's publicly an integer */
2192 if (SvIVX(sv) == IV_MIN) {
2193 /* 2s complement assumption. */
2194 SETi(SvIVX(sv)); /* special case: -((UV)IV_MAX+1) == IV_MIN */
2197 else if (SvUVX(sv) <= IV_MAX) {
2202 else if (SvIVX(sv) != IV_MIN) {
2206 #ifdef PERL_PRESERVE_IVUV
2213 if (SvNIOKp(sv) && (SvNIOK(sv) || !SvPOK(sv)))
2214 SETn(-SvNV_nomg(sv));
2215 else if (SvPOKp(sv) && SvIV_please_nomg(sv))
2216 goto oops_its_an_int;
2218 SETn(-SvNV_nomg(sv));
2226 tryAMAGICun_MG(not_amg, AMGf_set);
2227 *PL_stack_sp = boolSV(!SvTRUE_nomg(*PL_stack_sp));
2234 tryAMAGICun_MG(compl_amg, AMGf_numeric);
2238 if (PL_op->op_private & HINT_INTEGER) {
2239 const IV i = ~SvIV_nomg(sv);
2243 const UV u = ~SvUV_nomg(sv);
2252 (void)SvPV_nomg_const(sv,len); /* force check for uninit var */
2253 sv_setsv_nomg(TARG, sv);
2254 tmps = (U8*)SvPV_force_nomg(TARG, len);
2257 /* Calculate exact length, let's not estimate. */
2262 U8 * const send = tmps + len;
2263 U8 * const origtmps = tmps;
2264 const UV utf8flags = UTF8_ALLOW_ANYUV;
2266 while (tmps < send) {
2267 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2269 targlen += UNISKIP(~c);
2275 /* Now rewind strings and write them. */
2282 Newx(result, targlen + 1, U8);
2284 while (tmps < send) {
2285 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2287 p = uvchr_to_utf8_flags(p, ~c, UNICODE_ALLOW_ANY);
2290 sv_usepvn_flags(TARG, (char*)result, targlen,
2291 SV_HAS_TRAILING_NUL);
2298 Newx(result, nchar + 1, U8);
2300 while (tmps < send) {
2301 const U8 c = (U8)utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2306 sv_usepvn_flags(TARG, (char*)result, nchar, SV_HAS_TRAILING_NUL);
2315 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
2318 for ( ; anum >= (I32)sizeof(long); anum -= (I32)sizeof(long), tmpl++)
2323 for ( ; anum > 0; anum--, tmps++)
2331 /* integer versions of some of the above */
2335 dVAR; dSP; dATARGET;
2336 tryAMAGICbin_MG(mult_amg, AMGf_assign);
2339 SETi( left * right );
2347 dVAR; dSP; dATARGET;
2348 tryAMAGICbin_MG(div_amg, AMGf_assign);
2351 IV value = SvIV_nomg(right);
2353 DIE(aTHX_ "Illegal division by zero");
2354 num = SvIV_nomg(left);
2356 /* avoid FPE_INTOVF on some platforms when num is IV_MIN */
2360 value = num / value;
2366 #if defined(__GLIBC__) && IVSIZE == 8
2373 /* This is the vanilla old i_modulo. */
2374 dVAR; dSP; dATARGET;
2375 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2379 DIE(aTHX_ "Illegal modulus zero");
2380 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2384 SETi( left % right );
2389 #if defined(__GLIBC__) && IVSIZE == 8
2394 /* This is the i_modulo with the workaround for the _moddi3 bug
2395 * in (at least) glibc 2.2.5 (the PERL_ABS() the workaround).
2396 * See below for pp_i_modulo. */
2397 dVAR; dSP; dATARGET;
2398 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2402 DIE(aTHX_ "Illegal modulus zero");
2403 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2407 SETi( left % PERL_ABS(right) );
2414 dVAR; dSP; dATARGET;
2415 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2419 DIE(aTHX_ "Illegal modulus zero");
2420 /* The assumption is to use hereafter the old vanilla version... */
2422 PL_ppaddr[OP_I_MODULO] =
2424 /* .. but if we have glibc, we might have a buggy _moddi3
2425 * (at least glicb 2.2.5 is known to have this bug), in other
2426 * words our integer modulus with negative quad as the second
2427 * argument might be broken. Test for this and re-patch the
2428 * opcode dispatch table if that is the case, remembering to
2429 * also apply the workaround so that this first round works
2430 * right, too. See [perl #9402] for more information. */
2434 /* Cannot do this check with inlined IV constants since
2435 * that seems to work correctly even with the buggy glibc. */
2437 /* Yikes, we have the bug.
2438 * Patch in the workaround version. */
2440 PL_ppaddr[OP_I_MODULO] =
2441 &Perl_pp_i_modulo_1;
2442 /* Make certain we work right this time, too. */
2443 right = PERL_ABS(right);
2446 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2450 SETi( left % right );
2458 dVAR; dSP; dATARGET;
2459 tryAMAGICbin_MG(add_amg, AMGf_assign);
2461 dPOPTOPiirl_ul_nomg;
2462 SETi( left + right );
2469 dVAR; dSP; dATARGET;
2470 tryAMAGICbin_MG(subtr_amg, AMGf_assign);
2472 dPOPTOPiirl_ul_nomg;
2473 SETi( left - right );
2481 tryAMAGICbin_MG(lt_amg, AMGf_set);
2484 SETs(boolSV(left < right));
2492 tryAMAGICbin_MG(gt_amg, AMGf_set);
2495 SETs(boolSV(left > right));
2503 tryAMAGICbin_MG(le_amg, AMGf_set);
2506 SETs(boolSV(left <= right));
2514 tryAMAGICbin_MG(ge_amg, AMGf_set);
2517 SETs(boolSV(left >= right));
2525 tryAMAGICbin_MG(eq_amg, AMGf_set);
2528 SETs(boolSV(left == right));
2536 tryAMAGICbin_MG(ne_amg, AMGf_set);
2539 SETs(boolSV(left != right));
2547 tryAMAGICbin_MG(ncmp_amg, 0);
2554 else if (left < right)
2566 tryAMAGICun_MG(neg_amg, 0);
2567 if (S_negate_string(aTHX)) return NORMAL;
2569 SV * const sv = TOPs;
2570 IV const i = SvIV_nomg(sv);
2576 /* High falutin' math. */
2581 tryAMAGICbin_MG(atan2_amg, 0);
2584 SETn(Perl_atan2(left, right));
2592 int amg_type = sin_amg;
2593 const char *neg_report = NULL;
2594 NV (*func)(NV) = Perl_sin;
2595 const int op_type = PL_op->op_type;
2612 amg_type = sqrt_amg;
2614 neg_report = "sqrt";
2619 tryAMAGICun_MG(amg_type, 0);
2621 SV * const arg = POPs;
2622 const NV value = SvNV_nomg(arg);
2624 if (op_type == OP_LOG ? (value <= 0.0) : (value < 0.0)) {
2625 SET_NUMERIC_STANDARD();
2626 /* diag_listed_as: Can't take log of %g */
2627 DIE(aTHX_ "Can't take %s of %"NVgf, neg_report, value);
2630 XPUSHn(func(value));
2635 /* Support Configure command-line overrides for rand() functions.
2636 After 5.005, perhaps we should replace this by Configure support
2637 for drand48(), random(), or rand(). For 5.005, though, maintain
2638 compatibility by calling rand() but allow the user to override it.
2639 See INSTALL for details. --Andy Dougherty 15 July 1998
2641 /* Now it's after 5.005, and Configure supports drand48() and random(),
2642 in addition to rand(). So the overrides should not be needed any more.
2643 --Jarkko Hietaniemi 27 September 1998
2646 #ifndef HAS_DRAND48_PROTO
2647 extern double drand48 (void);
2657 value = 1.0; (void)POPs;
2663 if (!PL_srand_called) {
2664 (void)seedDrand01((Rand_seed_t)seed());
2665 PL_srand_called = TRUE;
2677 if (MAXARG >= 1 && (TOPs || POPs)) {
2684 pv = SvPV(top, len);
2685 flags = grok_number(pv, len, &anum);
2687 if (!(flags & IS_NUMBER_IN_UV)) {
2688 Perl_ck_warner_d(aTHX_ packWARN(WARN_OVERFLOW),
2689 "Integer overflow in srand");
2697 (void)seedDrand01((Rand_seed_t)anum);
2698 PL_srand_called = TRUE;
2702 /* Historically srand always returned true. We can avoid breaking
2704 sv_setpvs(TARG, "0 but true");
2713 tryAMAGICun_MG(int_amg, AMGf_numeric);
2715 SV * const sv = TOPs;
2716 const IV iv = SvIV_nomg(sv);
2717 /* XXX it's arguable that compiler casting to IV might be subtly
2718 different from modf (for numbers inside (IV_MIN,UV_MAX)) in which
2719 else preferring IV has introduced a subtle behaviour change bug. OTOH
2720 relying on floating point to be accurate is a bug. */
2725 else if (SvIOK(sv)) {
2727 SETu(SvUV_nomg(sv));
2732 const NV value = SvNV_nomg(sv);
2734 if (value < (NV)UV_MAX + 0.5) {
2737 SETn(Perl_floor(value));
2741 if (value > (NV)IV_MIN - 0.5) {
2744 SETn(Perl_ceil(value));
2755 tryAMAGICun_MG(abs_amg, AMGf_numeric);
2757 SV * const sv = TOPs;
2758 /* This will cache the NV value if string isn't actually integer */
2759 const IV iv = SvIV_nomg(sv);
2764 else if (SvIOK(sv)) {
2765 /* IVX is precise */
2767 SETu(SvUV_nomg(sv)); /* force it to be numeric only */
2775 /* 2s complement assumption. Also, not really needed as
2776 IV_MIN and -IV_MIN should both be %100...00 and NV-able */
2782 const NV value = SvNV_nomg(sv);
2796 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES;
2800 SV* const sv = POPs;
2802 tmps = (SvPV_const(sv, len));
2804 /* If Unicode, try to downgrade
2805 * If not possible, croak. */
2806 SV* const tsv = sv_2mortal(newSVsv(sv));
2809 sv_utf8_downgrade(tsv, FALSE);
2810 tmps = SvPV_const(tsv, len);
2812 if (PL_op->op_type == OP_HEX)
2815 while (*tmps && len && isSPACE(*tmps))
2819 if (*tmps == 'x' || *tmps == 'X') {
2821 result_uv = grok_hex (tmps, &len, &flags, &result_nv);
2823 else if (*tmps == 'b' || *tmps == 'B')
2824 result_uv = grok_bin (tmps, &len, &flags, &result_nv);
2826 result_uv = grok_oct (tmps, &len, &flags, &result_nv);
2828 if (flags & PERL_SCAN_GREATER_THAN_UV_MAX) {
2842 SV * const sv = TOPs;
2844 if (SvGAMAGIC(sv)) {
2845 /* For an overloaded or magic scalar, we can't know in advance if
2846 it's going to be UTF-8 or not. Also, we can't call sv_len_utf8 as
2847 it likes to cache the length. Maybe that should be a documented
2852 = sv_2pv_flags(sv, &len,
2853 SV_UNDEF_RETURNS_NULL|SV_CONST_RETURN|SV_GMAGIC);
2856 if (!SvPADTMP(TARG)) {
2857 sv_setsv(TARG, &PL_sv_undef);
2862 else if (DO_UTF8(sv)) {
2863 SETi(utf8_length((U8*)p, (U8*)p + len));
2867 } else if (SvOK(sv)) {
2868 /* Neither magic nor overloaded. */
2870 SETi(sv_len_utf8(sv));
2874 if (!SvPADTMP(TARG)) {
2875 sv_setsv_nomg(TARG, &PL_sv_undef);
2883 /* Returns false if substring is completely outside original string.
2884 No length is indicated by len_iv = 0 and len_is_uv = 0. len_is_uv must
2885 always be true for an explicit 0.
2888 Perl_translate_substr_offsets(pTHX_ STRLEN curlen, IV pos1_iv,
2889 bool pos1_is_uv, IV len_iv,
2890 bool len_is_uv, STRLEN *posp,
2896 PERL_ARGS_ASSERT_TRANSLATE_SUBSTR_OFFSETS;
2898 if (!pos1_is_uv && pos1_iv < 0 && curlen) {
2899 pos1_is_uv = curlen-1 > ~(UV)pos1_iv;
2902 if ((pos1_is_uv || pos1_iv > 0) && (UV)pos1_iv > curlen)
2905 if (len_iv || len_is_uv) {
2906 if (!len_is_uv && len_iv < 0) {
2907 pos2_iv = curlen + len_iv;
2909 pos2_is_uv = curlen-1 > ~(UV)len_iv;
2912 } else { /* len_iv >= 0 */
2913 if (!pos1_is_uv && pos1_iv < 0) {
2914 pos2_iv = pos1_iv + len_iv;
2915 pos2_is_uv = (UV)len_iv > (UV)IV_MAX;
2917 if ((UV)len_iv > curlen-(UV)pos1_iv)
2920 pos2_iv = pos1_iv+len_iv;
2930 if (!pos2_is_uv && pos2_iv < 0) {
2931 if (!pos1_is_uv && pos1_iv < 0)
2935 else if (!pos1_is_uv && pos1_iv < 0)
2938 if ((UV)pos2_iv < (UV)pos1_iv)
2940 if ((UV)pos2_iv > curlen)
2943 /* pos1_iv and pos2_iv both in 0..curlen, so the cast is safe */
2944 *posp = (STRLEN)( (UV)pos1_iv );
2945 *lenp = (STRLEN)( (UV)pos2_iv - (UV)pos1_iv );
2962 I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
2963 const bool rvalue = (GIMME_V != G_VOID);
2966 const char *repl = NULL;
2968 int num_args = PL_op->op_private & 7;
2969 bool repl_need_utf8_upgrade = FALSE;
2970 bool repl_is_utf8 = FALSE;
2974 if(!(repl_sv = POPs)) num_args--;
2976 if ((len_sv = POPs)) {
2977 len_iv = SvIV(len_sv);
2978 len_is_uv = len_iv ? SvIOK_UV(len_sv) : 1;
2983 pos1_iv = SvIV(pos_sv);
2984 pos1_is_uv = SvIOK_UV(pos_sv);
2986 if (PL_op->op_private & OPpSUBSTR_REPL_FIRST) {
2992 repl = SvPV_const(repl_sv, repl_len);
2993 repl_is_utf8 = DO_UTF8(repl_sv) && repl_len;
2996 sv_utf8_upgrade(sv);
2998 else if (DO_UTF8(sv))
2999 repl_need_utf8_upgrade = TRUE;
3003 ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
3004 sv_magic(ret, NULL, PERL_MAGIC_substr, NULL, 0);
3006 LvTARG(ret) = SvREFCNT_inc_simple(sv);
3008 pos1_is_uv || pos1_iv >= 0
3009 ? (STRLEN)(UV)pos1_iv
3010 : (LvFLAGS(ret) |= 1, (STRLEN)(UV)-pos1_iv);
3012 len_is_uv || len_iv > 0
3013 ? (STRLEN)(UV)len_iv
3014 : (LvFLAGS(ret) |= 2, (STRLEN)(UV)-len_iv);
3017 PUSHs(ret); /* avoid SvSETMAGIC here */
3020 tmps = SvPV_const(sv, curlen);
3022 utf8_curlen = sv_len_utf8(sv);
3023 if (utf8_curlen == curlen)
3026 curlen = utf8_curlen;
3032 STRLEN pos, len, byte_len, byte_pos;
3034 if (!translate_substr_offsets(
3035 curlen, pos1_iv, pos1_is_uv, len_iv, len_is_uv, &pos, &len
3039 byte_pos = utf8_curlen
3040 ? sv_pos_u2b_flags(sv, pos, &byte_len, SV_CONST_RETURN) : pos;
3045 SvTAINTED_off(TARG); /* decontaminate */
3046 SvUTF8_off(TARG); /* decontaminate */
3047 sv_setpvn(TARG, tmps, byte_len);
3048 #ifdef USE_LOCALE_COLLATE
3049 sv_unmagic(TARG, PERL_MAGIC_collxfrm);
3056 SV* repl_sv_copy = NULL;
3058 if (repl_need_utf8_upgrade) {
3059 repl_sv_copy = newSVsv(repl_sv);
3060 sv_utf8_upgrade(repl_sv_copy);
3061 repl = SvPV_const(repl_sv_copy, repl_len);
3062 repl_is_utf8 = DO_UTF8(repl_sv_copy) && repl_len;
3065 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR),
3066 "Attempt to use reference as lvalue in substr"
3070 sv_insert_flags(sv, byte_pos, byte_len, repl, repl_len, 0);
3073 SvREFCNT_dec(repl_sv_copy);
3085 Perl_croak(aTHX_ "substr outside of string");
3086 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR), "substr outside of string");
3093 const IV size = POPi;
3094 const IV offset = POPi;
3095 SV * const src = POPs;
3096 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
3099 if (lvalue) { /* it's an lvalue! */
3100 ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
3101 sv_magic(ret, NULL, PERL_MAGIC_vec, NULL, 0);
3103 LvTARG(ret) = SvREFCNT_inc_simple(src);
3104 LvTARGOFF(ret) = offset;
3105 LvTARGLEN(ret) = size;
3109 SvTAINTED_off(TARG); /* decontaminate */
3113 sv_setuv(ret, do_vecget(src, offset, size));
3129 const char *little_p;
3132 const bool is_index = PL_op->op_type == OP_INDEX;
3133 const bool threeargs = MAXARG >= 3 && (TOPs || ((void)POPs,0));
3139 big_p = SvPV_const(big, biglen);
3140 little_p = SvPV_const(little, llen);
3142 big_utf8 = DO_UTF8(big);
3143 little_utf8 = DO_UTF8(little);
3144 if (big_utf8 ^ little_utf8) {
3145 /* One needs to be upgraded. */
3146 if (little_utf8 && !PL_encoding) {
3147 /* Well, maybe instead we might be able to downgrade the small
3149 char * const pv = (char*)bytes_from_utf8((U8 *)little_p, &llen,
3152 /* If the large string is ISO-8859-1, and it's not possible to
3153 convert the small string to ISO-8859-1, then there is no
3154 way that it could be found anywhere by index. */
3159 /* At this point, pv is a malloc()ed string. So donate it to temp
3160 to ensure it will get free()d */
3161 little = temp = newSV(0);
3162 sv_usepvn(temp, pv, llen);
3163 little_p = SvPVX(little);
3166 ? newSVpvn(big_p, biglen) : newSVpvn(little_p, llen);
3169 sv_recode_to_utf8(temp, PL_encoding);
3171 sv_utf8_upgrade(temp);
3176 big_p = SvPV_const(big, biglen);
3179 little_p = SvPV_const(little, llen);
3183 if (SvGAMAGIC(big)) {
3184 /* Life just becomes a lot easier if I use a temporary here.
3185 Otherwise I need to avoid calls to sv_pos_u2b(), which (dangerously)
3186 will trigger magic and overloading again, as will fbm_instr()
3188 big = newSVpvn_flags(big_p, biglen,
3189 SVs_TEMP | (big_utf8 ? SVf_UTF8 : 0));
3192 if (SvGAMAGIC(little) || (is_index && !SvOK(little))) {
3193 /* index && SvOK() is a hack. fbm_instr() calls SvPV_const, which will
3194 warn on undef, and we've already triggered a warning with the
3195 SvPV_const some lines above. We can't remove that, as we need to
3196 call some SvPV to trigger overloading early and find out if the
3198 This is all getting to messy. The API isn't quite clean enough,
3199 because data access has side effects.
3201 little = newSVpvn_flags(little_p, llen,
3202 SVs_TEMP | (little_utf8 ? SVf_UTF8 : 0));
3203 little_p = SvPVX(little);
3207 offset = is_index ? 0 : biglen;
3209 if (big_utf8 && offset > 0)
3210 sv_pos_u2b(big, &offset, 0);
3216 else if (offset > (I32)biglen)
3218 if (!(little_p = is_index
3219 ? fbm_instr((unsigned char*)big_p + offset,
3220 (unsigned char*)big_p + biglen, little, 0)
3221 : rninstr(big_p, big_p + offset,
3222 little_p, little_p + llen)))
3225 retval = little_p - big_p;
3226 if (retval > 0 && big_utf8)
3227 sv_pos_b2u(big, &retval);
3237 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
3238 SvTAINTED_off(TARG);
3239 do_sprintf(TARG, SP-MARK, MARK+1);
3240 TAINT_IF(SvTAINTED(TARG));
3252 const U8 *s = (U8*)SvPV_const(argsv, len);
3254 if (PL_encoding && SvPOK(argsv) && !DO_UTF8(argsv)) {
3255 SV * const tmpsv = sv_2mortal(newSVsv(argsv));
3256 s = (U8*)sv_recode_to_utf8(tmpsv, PL_encoding);
3260 XPUSHu(DO_UTF8(argsv) ?
3261 utf8n_to_uvchr(s, UTF8_MAXBYTES, 0, UTF8_ALLOW_ANYUV) :
3275 if (!IN_BYTES /* under bytes, chr(-1) eq chr(0xff), etc. */
3276 && ((SvIOKp(top) && !SvIsUV(top) && SvIV_nomg(top) < 0)
3278 ((SvNOKp(top) || (SvOK(top) && !SvIsUV(top)))
3279 && SvNV_nomg(top) < 0.0))) {
3280 if (ckWARN(WARN_UTF8)) {
3281 if (SvGMAGICAL(top)) {
3282 SV *top2 = sv_newmortal();
3283 sv_setsv_nomg(top2, top);
3286 Perl_warner(aTHX_ packWARN(WARN_UTF8),
3287 "Invalid negative number (%"SVf") in chr", top);
3289 value = UNICODE_REPLACEMENT;
3291 value = SvUV_nomg(top);
3294 SvUPGRADE(TARG,SVt_PV);
3296 if (value > 255 && !IN_BYTES) {
3297 SvGROW(TARG, (STRLEN)UNISKIP(value)+1);
3298 tmps = (char*)uvchr_to_utf8_flags((U8*)SvPVX(TARG), value, 0);
3299 SvCUR_set(TARG, tmps - SvPVX_const(TARG));
3301 (void)SvPOK_only(TARG);
3310 *tmps++ = (char)value;
3312 (void)SvPOK_only(TARG);
3314 if (PL_encoding && !IN_BYTES) {
3315 sv_recode_to_utf8(TARG, PL_encoding);
3317 if (SvCUR(TARG) == 0
3318 || ! is_utf8_string((U8*)tmps, SvCUR(TARG))
3319 || UTF8_IS_REPLACEMENT((U8*) tmps, (U8*) tmps + SvCUR(TARG)))
3324 *tmps++ = (char)value;
3340 const char *tmps = SvPV_const(left, len);
3342 if (DO_UTF8(left)) {
3343 /* If Unicode, try to downgrade.
3344 * If not possible, croak.
3345 * Yes, we made this up. */
3346 SV* const tsv = sv_2mortal(newSVsv(left));
3349 sv_utf8_downgrade(tsv, FALSE);
3350 tmps = SvPV_const(tsv, len);
3352 # ifdef USE_ITHREADS
3354 if (!PL_reentrant_buffer->_crypt_struct_buffer) {
3355 /* This should be threadsafe because in ithreads there is only
3356 * one thread per interpreter. If this would not be true,
3357 * we would need a mutex to protect this malloc. */
3358 PL_reentrant_buffer->_crypt_struct_buffer =
3359 (struct crypt_data *)safemalloc(sizeof(struct crypt_data));
3360 #if defined(__GLIBC__) || defined(__EMX__)
3361 if (PL_reentrant_buffer->_crypt_struct_buffer) {
3362 PL_reentrant_buffer->_crypt_struct_buffer->initialized = 0;
3363 /* work around glibc-2.2.5 bug */
3364 PL_reentrant_buffer->_crypt_struct_buffer->current_saltbits = 0;
3368 # endif /* HAS_CRYPT_R */
3369 # endif /* USE_ITHREADS */
3371 sv_setpv(TARG, fcrypt(tmps, SvPV_nolen_const(right)));
3373 sv_setpv(TARG, PerlProc_crypt(tmps, SvPV_nolen_const(right)));
3379 "The crypt() function is unimplemented due to excessive paranoia.");
3383 /* Generally UTF-8 and UTF-EBCDIC are indistinguishable at this level. So
3384 * most comments below say UTF-8, when in fact they mean UTF-EBCDIC as well */
3386 /* Generates code to store a unicode codepoint c that is known to occupy
3387 * exactly two UTF-8 and UTF-EBCDIC bytes; it is stored into p and p+1,
3388 * and p is advanced to point to the next available byte after the two bytes */
3389 #define CAT_UNI_TO_UTF8_TWO_BYTE(p, c) \
3391 *(p)++ = UTF8_TWO_BYTE_HI(c); \
3392 *((p)++) = UTF8_TWO_BYTE_LO(c); \
3397 /* Actually is both lcfirst() and ucfirst(). Only the first character
3398 * changes. This means that possibly we can change in-place, ie., just
3399 * take the source and change that one character and store it back, but not
3400 * if read-only etc, or if the length changes */
3405 STRLEN slen; /* slen is the byte length of the whole SV. */
3408 bool inplace; /* ? Convert first char only, in-place */
3409 bool doing_utf8 = FALSE; /* ? using utf8 */
3410 bool convert_source_to_utf8 = FALSE; /* ? need to convert */
3411 const int op_type = PL_op->op_type;
3414 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
3415 STRLEN ulen; /* ulen is the byte length of the original Unicode character
3416 * stored as UTF-8 at s. */
3417 STRLEN tculen; /* tculen is the byte length of the freshly titlecased (or
3418 * lowercased) character stored in tmpbuf. May be either
3419 * UTF-8 or not, but in either case is the number of bytes */
3420 bool tainted = FALSE;
3424 s = (const U8*)SvPV_nomg_const(source, slen);
3426 if (ckWARN(WARN_UNINITIALIZED))
3427 report_uninit(source);
3432 /* We may be able to get away with changing only the first character, in
3433 * place, but not if read-only, etc. Later we may discover more reasons to
3434 * not convert in-place. */
3435 inplace = SvPADTMP(source) && !SvREADONLY(source) && SvTEMP(source);
3437 /* First calculate what the changed first character should be. This affects
3438 * whether we can just swap it out, leaving the rest of the string unchanged,
3439 * or even if have to convert the dest to UTF-8 when the source isn't */
3441 if (! slen) { /* If empty */
3442 need = 1; /* still need a trailing NUL */
3445 else if (DO_UTF8(source)) { /* Is the source utf8? */
3448 if (op_type == OP_UCFIRST) {
3449 _to_utf8_title_flags(s, tmpbuf, &tculen,
3450 cBOOL(IN_LOCALE_RUNTIME), &tainted);
3453 _to_utf8_lower_flags(s, tmpbuf, &tculen,
3454 cBOOL(IN_LOCALE_RUNTIME), &tainted);
3457 /* we can't do in-place if the length changes. */
3458 if (ulen != tculen) inplace = FALSE;
3459 need = slen + 1 - ulen + tculen;
3461 else { /* Non-zero length, non-UTF-8, Need to consider locale and if
3462 * latin1 is treated as caseless. Note that a locale takes
3464 ulen = 1; /* Original character is 1 byte */
3465 tculen = 1; /* Most characters will require one byte, but this will
3466 * need to be overridden for the tricky ones */
3469 if (op_type == OP_LCFIRST) {
3471 /* lower case the first letter: no trickiness for any character */
3472 *tmpbuf = (IN_LOCALE_RUNTIME) ? toLOWER_LC(*s) :
3473 ((IN_UNI_8_BIT) ? toLOWER_LATIN1(*s) : toLOWER(*s));
3476 else if (IN_LOCALE_RUNTIME) {
3477 *tmpbuf = toUPPER_LC(*s); /* This would be a bug if any locales
3478 * have upper and title case different
3481 else if (! IN_UNI_8_BIT) {
3482 *tmpbuf = toUPPER(*s); /* Returns caseless for non-ascii, or
3483 * on EBCDIC machines whatever the
3484 * native function does */
3486 else { /* is ucfirst non-UTF-8, not in locale, and cased latin1 */
3487 UV title_ord = _to_upper_title_latin1(*s, tmpbuf, &tculen, 's');
3489 assert(tculen == 2);
3491 /* If the result is an upper Latin1-range character, it can
3492 * still be represented in one byte, which is its ordinal */
3493 if (UTF8_IS_DOWNGRADEABLE_START(*tmpbuf)) {
3494 *tmpbuf = (U8) title_ord;
3498 /* Otherwise it became more than one ASCII character (in
3499 * the case of LATIN_SMALL_LETTER_SHARP_S) or changed to
3500 * beyond Latin1, so the number of bytes changed, so can't
3501 * replace just the first character in place. */
3504 /* If the result won't fit in a byte, the entire result will
3505 * have to be in UTF-8. Assume worst case sizing in
3506 * conversion. (all latin1 characters occupy at most two bytes
3508 if (title_ord > 255) {
3510 convert_source_to_utf8 = TRUE;
3511 need = slen * 2 + 1;
3513 /* The (converted) UTF-8 and UTF-EBCDIC lengths of all
3514 * (both) characters whose title case is above 255 is
3518 else { /* LATIN_SMALL_LETTER_SHARP_S expands by 1 byte */
3519 need = slen + 1 + 1;
3523 } /* End of use Unicode (Latin1) semantics */
3524 } /* End of changing the case of the first character */
3526 /* Here, have the first character's changed case stored in tmpbuf. Ready to
3527 * generate the result */
3530 /* We can convert in place. This means we change just the first
3531 * character without disturbing the rest; no need to grow */
3533 s = d = (U8*)SvPV_force_nomg(source, slen);
3539 /* Here, we can't convert in place; we earlier calculated how much
3540 * space we will need, so grow to accommodate that */
3541 SvUPGRADE(dest, SVt_PV);
3542 d = (U8*)SvGROW(dest, need);
3543 (void)SvPOK_only(dest);
3550 if (! convert_source_to_utf8) {
3552 /* Here both source and dest are in UTF-8, but have to create
3553 * the entire output. We initialize the result to be the
3554 * title/lower cased first character, and then append the rest
3556 sv_setpvn(dest, (char*)tmpbuf, tculen);
3558 sv_catpvn(dest, (char*)(s + ulen), slen - ulen);
3562 const U8 *const send = s + slen;
3564 /* Here the dest needs to be in UTF-8, but the source isn't,
3565 * except we earlier UTF-8'd the first character of the source
3566 * into tmpbuf. First put that into dest, and then append the
3567 * rest of the source, converting it to UTF-8 as we go. */
3569 /* Assert tculen is 2 here because the only two characters that
3570 * get to this part of the code have 2-byte UTF-8 equivalents */
3572 *d++ = *(tmpbuf + 1);
3573 s++; /* We have just processed the 1st char */
3575 for (; s < send; s++) {
3576 d = uvchr_to_utf8(d, *s);
3579 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3583 else { /* in-place UTF-8. Just overwrite the first character */
3584 Copy(tmpbuf, d, tculen, U8);
3585 SvCUR_set(dest, need - 1);
3593 else { /* Neither source nor dest are in or need to be UTF-8 */
3595 if (IN_LOCALE_RUNTIME) {
3599 if (inplace) { /* in-place, only need to change the 1st char */
3602 else { /* Not in-place */
3604 /* Copy the case-changed character(s) from tmpbuf */
3605 Copy(tmpbuf, d, tculen, U8);
3606 d += tculen - 1; /* Code below expects d to point to final
3607 * character stored */
3610 else { /* empty source */
3611 /* See bug #39028: Don't taint if empty */
3615 /* In a "use bytes" we don't treat the source as UTF-8, but, still want
3616 * the destination to retain that flag */
3620 if (!inplace) { /* Finish the rest of the string, unchanged */
3621 /* This will copy the trailing NUL */
3622 Copy(s + 1, d + 1, slen, U8);
3623 SvCUR_set(dest, need - 1);
3626 if (dest != source && SvTAINTED(source))
3632 /* There's so much setup/teardown code common between uc and lc, I wonder if
3633 it would be worth merging the two, and just having a switch outside each
3634 of the three tight loops. There is less and less commonality though */
3648 if (SvPADTMP(source) && !SvREADONLY(source) && !SvAMAGIC(source)
3649 && SvTEMP(source) && !DO_UTF8(source)
3650 && (IN_LOCALE_RUNTIME || ! IN_UNI_8_BIT)) {
3652 /* We can convert in place. The reason we can't if in UNI_8_BIT is to
3653 * make the loop tight, so we overwrite the source with the dest before
3654 * looking at it, and we need to look at the original source
3655 * afterwards. There would also need to be code added to handle
3656 * switching to not in-place in midstream if we run into characters
3657 * that change the length.
3660 s = d = (U8*)SvPV_force_nomg(source, len);
3667 /* The old implementation would copy source into TARG at this point.
3668 This had the side effect that if source was undef, TARG was now
3669 an undefined SV with PADTMP set, and they don't warn inside
3670 sv_2pv_flags(). However, we're now getting the PV direct from
3671 source, which doesn't have PADTMP set, so it would warn. Hence the
3675 s = (const U8*)SvPV_nomg_const(source, len);
3677 if (ckWARN(WARN_UNINITIALIZED))
3678 report_uninit(source);
3684 SvUPGRADE(dest, SVt_PV);
3685 d = (U8*)SvGROW(dest, min);
3686 (void)SvPOK_only(dest);
3691 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
3692 to check DO_UTF8 again here. */
3694 if (DO_UTF8(source)) {
3695 const U8 *const send = s + len;
3696 U8 tmpbuf[UTF8_MAXBYTES+1];
3697 bool tainted = FALSE;
3699 /* All occurrences of these are to be moved to follow any other marks.
3700 * This is context-dependent. We may not be passed enough context to
3701 * move the iota subscript beyond all of them, but we do the best we can
3702 * with what we're given. The result is always better than if we
3703 * hadn't done this. And, the problem would only arise if we are
3704 * passed a character without all its combining marks, which would be
3705 * the caller's mistake. The information this is based on comes from a
3706 * comment in Unicode SpecialCasing.txt, (and the Standard's text
3707 * itself) and so can't be checked properly to see if it ever gets
3708 * revised. But the likelihood of it changing is remote */
3709 bool in_iota_subscript = FALSE;
3715 if (in_iota_subscript && ! is_utf8_mark(s)) {
3717 /* A non-mark. Time to output the iota subscript */
3718 #define GREEK_CAPITAL_LETTER_IOTA 0x0399
3719 #define COMBINING_GREEK_YPOGEGRAMMENI 0x0345
3721 CAT_UNI_TO_UTF8_TWO_BYTE(d, GREEK_CAPITAL_LETTER_IOTA);
3722 in_iota_subscript = FALSE;
3725 /* Then handle the current character. Get the changed case value
3726 * and copy it to the output buffer */
3729 uv = _to_utf8_upper_flags(s, tmpbuf, &ulen,
3730 cBOOL(IN_LOCALE_RUNTIME), &tainted);
3731 if (uv == GREEK_CAPITAL_LETTER_IOTA
3732 && utf8_to_uvchr_buf(s, send, 0) == COMBINING_GREEK_YPOGEGRAMMENI)
3734 in_iota_subscript = TRUE;
3737 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
3738 /* If the eventually required minimum size outgrows the
3739 * available space, we need to grow. */
3740 const UV o = d - (U8*)SvPVX_const(dest);
3742 /* If someone uppercases one million U+03B0s we SvGROW()
3743 * one million times. Or we could try guessing how much to
3744 * allocate without allocating too much. Such is life.
3745 * See corresponding comment in lc code for another option
3748 d = (U8*)SvPVX(dest) + o;
3750 Copy(tmpbuf, d, ulen, U8);
3755 if (in_iota_subscript) {
3756 CAT_UNI_TO_UTF8_TWO_BYTE(d, GREEK_CAPITAL_LETTER_IOTA);
3761 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3767 else { /* Not UTF-8 */
3769 const U8 *const send = s + len;
3771 /* Use locale casing if in locale; regular style if not treating
3772 * latin1 as having case; otherwise the latin1 casing. Do the
3773 * whole thing in a tight loop, for speed, */
3774 if (IN_LOCALE_RUNTIME) {
3777 for (; s < send; d++, s++)
3778 *d = toUPPER_LC(*s);
3780 else if (! IN_UNI_8_BIT) {
3781 for (; s < send; d++, s++) {
3786 for (; s < send; d++, s++) {
3787 *d = toUPPER_LATIN1_MOD(*s);
3788 if (LIKELY(*d != LATIN_SMALL_LETTER_Y_WITH_DIAERESIS)) continue;
3790 /* The mainstream case is the tight loop above. To avoid
3791 * extra tests in that, all three characters that require
3792 * special handling are mapped by the MOD to the one tested
3794 * Use the source to distinguish between the three cases */
3796 if (*s == LATIN_SMALL_LETTER_SHARP_S) {
3798 /* uc() of this requires 2 characters, but they are
3799 * ASCII. If not enough room, grow the string */
3800 if (SvLEN(dest) < ++min) {
3801 const UV o = d - (U8*)SvPVX_const(dest);
3803 d = (U8*)SvPVX(dest) + o;
3805 *d++ = 'S'; *d = 'S'; /* upper case is 'SS' */
3806 continue; /* Back to the tight loop; still in ASCII */
3809 /* The other two special handling characters have their
3810 * upper cases outside the latin1 range, hence need to be
3811 * in UTF-8, so the whole result needs to be in UTF-8. So,
3812 * here we are somewhere in the middle of processing a
3813 * non-UTF-8 string, and realize that we will have to convert
3814 * the whole thing to UTF-8. What to do? There are
3815 * several possibilities. The simplest to code is to
3816 * convert what we have so far, set a flag, and continue on
3817 * in the loop. The flag would be tested each time through
3818 * the loop, and if set, the next character would be
3819 * converted to UTF-8 and stored. But, I (khw) didn't want
3820 * to slow down the mainstream case at all for this fairly
3821 * rare case, so I didn't want to add a test that didn't
3822 * absolutely have to be there in the loop, besides the
3823 * possibility that it would get too complicated for
3824 * optimizers to deal with. Another possibility is to just
3825 * give up, convert the source to UTF-8, and restart the
3826 * function that way. Another possibility is to convert
3827 * both what has already been processed and what is yet to
3828 * come separately to UTF-8, then jump into the loop that
3829 * handles UTF-8. But the most efficient time-wise of the
3830 * ones I could think of is what follows, and turned out to
3831 * not require much extra code. */
3833 /* Convert what we have so far into UTF-8, telling the
3834 * function that we know it should be converted, and to
3835 * allow extra space for what we haven't processed yet.
3836 * Assume the worst case space requirements for converting
3837 * what we haven't processed so far: that it will require
3838 * two bytes for each remaining source character, plus the
3839 * NUL at the end. This may cause the string pointer to
3840 * move, so re-find it. */
3842 len = d - (U8*)SvPVX_const(dest);
3843 SvCUR_set(dest, len);
3844 len = sv_utf8_upgrade_flags_grow(dest,
3845 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
3847 d = (U8*)SvPVX(dest) + len;
3849 /* Now process the remainder of the source, converting to
3850 * upper and UTF-8. If a resulting byte is invariant in
3851 * UTF-8, output it as-is, otherwise convert to UTF-8 and
3852 * append it to the output. */
3853 for (; s < send; s++) {
3854 (void) _to_upper_title_latin1(*s, d, &len, 'S');
3858 /* Here have processed the whole source; no need to continue
3859 * with the outer loop. Each character has been converted
3860 * to upper case and converted to UTF-8 */
3863 } /* End of processing all latin1-style chars */
3864 } /* End of processing all chars */
3865 } /* End of source is not empty */
3867 if (source != dest) {
3868 *d = '\0'; /* Here d points to 1 after last char, add NUL */
3869 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3871 } /* End of isn't utf8 */
3872 if (dest != source && SvTAINTED(source))
3891 if (SvPADTMP(source) && !SvREADONLY(source) && !SvAMAGIC(source)
3892 && SvTEMP(source) && !DO_UTF8(source)) {
3894 /* We can convert in place, as lowercasing anything in the latin1 range
3895 * (or else DO_UTF8 would have been on) doesn't lengthen it */
3897 s = d = (U8*)SvPV_force_nomg(source, len);
3904 /* The old implementation would copy source into TARG at this point.
3905 This had the side effect that if source was undef, TARG was now
3906 an undefined SV with PADTMP set, and they don't warn inside
3907 sv_2pv_flags(). However, we're now getting the PV direct from
3908 source, which doesn't have PADTMP set, so it would warn. Hence the
3912 s = (const U8*)SvPV_nomg_const(source, len);
3914 if (ckWARN(WARN_UNINITIALIZED))
3915 report_uninit(source);
3921 SvUPGRADE(dest, SVt_PV);
3922 d = (U8*)SvGROW(dest, min);
3923 (void)SvPOK_only(dest);
3928 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
3929 to check DO_UTF8 again here. */
3931 if (DO_UTF8(source)) {
3932 const U8 *const send = s + len;
3933 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
3934 bool tainted = FALSE;
3937 const STRLEN u = UTF8SKIP(s);
3940 _to_utf8_lower_flags(s, tmpbuf, &ulen,
3941 cBOOL(IN_LOCALE_RUNTIME), &tainted);
3943 /* Here is where we would do context-sensitive actions. See the
3944 * commit message for this comment for why there isn't any */
3946 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
3948 /* If the eventually required minimum size outgrows the
3949 * available space, we need to grow. */
3950 const UV o = d - (U8*)SvPVX_const(dest);
3952 /* If someone lowercases one million U+0130s we SvGROW() one
3953 * million times. Or we could try guessing how much to
3954 * allocate without allocating too much. Such is life.
3955 * Another option would be to grow an extra byte or two more
3956 * each time we need to grow, which would cut down the million
3957 * to 500K, with little waste */
3959 d = (U8*)SvPVX(dest) + o;
3962 /* Copy the newly lowercased letter to the output buffer we're
3964 Copy(tmpbuf, d, ulen, U8);
3967 } /* End of looping through the source string */
3970 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3975 } else { /* Not utf8 */
3977 const U8 *const send = s + len;
3979 /* Use locale casing if in locale; regular style if not treating
3980 * latin1 as having case; otherwise the latin1 casing. Do the
3981 * whole thing in a tight loop, for speed, */
3982 if (IN_LOCALE_RUNTIME) {
3985 for (; s < send; d++, s++)
3986 *d = toLOWER_LC(*s);
3988 else if (! IN_UNI_8_BIT) {
3989 for (; s < send; d++, s++) {
3994 for (; s < send; d++, s++) {
3995 *d = toLOWER_LATIN1(*s);
3999 if (source != dest) {
4001 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4004 if (dest != source && SvTAINTED(source))
4013 SV * const sv = TOPs;
4015 const char *s = SvPV_const(sv,len);
4017 SvUTF8_off(TARG); /* decontaminate */
4020 SvUPGRADE(TARG, SVt_PV);
4021 SvGROW(TARG, (len * 2) + 1);
4025 STRLEN ulen = UTF8SKIP(s);
4026 bool to_quote = FALSE;
4028 if (UTF8_IS_INVARIANT(*s)) {
4029 if (_isQUOTEMETA(*s)) {
4033 else if (UTF8_IS_DOWNGRADEABLE_START(*s)) {
4035 /* In locale, we quote all non-ASCII Latin1 chars.
4036 * Otherwise use the quoting rules */
4037 if (IN_LOCALE_RUNTIME
4038 || _isQUOTEMETA(TWO_BYTE_UTF8_TO_UNI(*s, *(s + 1))))
4043 else if (_is_utf8_quotemeta((U8 *) s)) {
4058 else if (IN_UNI_8_BIT) {
4060 if (_isQUOTEMETA(*s))
4066 /* For non UNI_8_BIT (and hence in locale) just quote all \W
4067 * including everything above ASCII */
4069 if (!isWORDCHAR_A(*s))
4075 SvCUR_set(TARG, d - SvPVX_const(TARG));
4076 (void)SvPOK_only_UTF8(TARG);
4079 sv_setpvn(TARG, s, len);
4096 U8 tmpbuf[UTF8_MAXBYTES * UTF8_MAX_FOLD_CHAR_EXPAND + 1];
4097 const bool full_folding = TRUE;
4098 const U8 flags = ( full_folding ? FOLD_FLAGS_FULL : 0 )
4099 | ( IN_LOCALE_RUNTIME ? FOLD_FLAGS_LOCALE : 0 );
4101 /* This is a facsimile of pp_lc, but with a thousand bugs thanks to me.
4102 * You are welcome(?) -Hugmeir
4110 s = (const U8*)SvPV_nomg_const(source, len);
4112 if (ckWARN(WARN_UNINITIALIZED))
4113 report_uninit(source);
4120 SvUPGRADE(dest, SVt_PV);
4121 d = (U8*)SvGROW(dest, min);
4122 (void)SvPOK_only(dest);
4127 if (DO_UTF8(source)) { /* UTF-8 flagged string. */
4128 bool tainted = FALSE;
4130 const STRLEN u = UTF8SKIP(s);
4133 _to_utf8_fold_flags(s, tmpbuf, &ulen, flags, &tainted);
4135 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
4136 const UV o = d - (U8*)SvPVX_const(dest);
4138 d = (U8*)SvPVX(dest) + o;
4141 Copy(tmpbuf, d, ulen, U8);
4150 } /* Unflagged string */
4152 /* For locale, bytes, and nothing, the behavior is supposed to be the
4155 if ( IN_LOCALE_RUNTIME ) { /* Under locale */
4158 for (; s < send; d++, s++)
4159 *d = toLOWER_LC(*s);
4161 else if ( !IN_UNI_8_BIT ) { /* Under nothing, or bytes */
4162 for (; s < send; d++, s++)
4166 /* For ASCII and the Latin-1 range, there's only two troublesome folds,
4167 * \x{DF} (\N{LATIN SMALL LETTER SHARP S}), which under full casefolding
4168 * becomes 'ss', and \x{B5} (\N{MICRO SIGN}), which under any fold becomes
4169 * \x{3BC} (\N{GREEK SMALL LETTER MU}) -- For the rest, the casefold is
4172 for (; s < send; d++, s++) {
4173 if (*s == MICRO_SIGN) {
4174 /* \N{MICRO SIGN}'s casefold is \N{GREEK SMALL LETTER MU}, which
4175 * is outside of the latin-1 range. There's a couple of ways to
4176 * deal with this -- khw discusses them in pp_lc/uc, so go there :)
4177 * What we do here is upgrade what we had already casefolded,
4178 * then enter an inner loop that appends the rest of the characters
4181 len = d - (U8*)SvPVX_const(dest);
4182 SvCUR_set(dest, len);
4183 len = sv_utf8_upgrade_flags_grow(dest,
4184 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
4185 /* The max expansion for latin1
4186 * chars is 1 byte becomes 2 */
4188 d = (U8*)SvPVX(dest) + len;
4190 CAT_UNI_TO_UTF8_TWO_BYTE(d, GREEK_SMALL_LETTER_MU);
4192 for (; s < send; s++) {
4194 UV fc = _to_uni_fold_flags(*s, tmpbuf, &ulen, flags);
4195 if UNI_IS_INVARIANT(fc) {
4196 if ( full_folding && *s == LATIN_SMALL_LETTER_SHARP_S) {
4204 Copy(tmpbuf, d, ulen, U8);
4210 else if (full_folding && *s == LATIN_SMALL_LETTER_SHARP_S) {
4211 /* Under full casefolding, LATIN SMALL LETTER SHARP S becomes "ss",
4212 * which may require growing the SV.
4214 if (SvLEN(dest) < ++min) {
4215 const UV o = d - (U8*)SvPVX_const(dest);
4217 d = (U8*)SvPVX(dest) + o;
4222 else { /* If it's not one of those two, the fold is their lower case */
4223 *d = toLOWER_LATIN1(*s);
4229 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4231 if (SvTAINTED(source))
4241 dVAR; dSP; dMARK; dORIGMARK;
4242 AV *const av = MUTABLE_AV(POPs);
4243 const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
4245 if (SvTYPE(av) == SVt_PVAV) {
4246 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
4247 bool can_preserve = FALSE;
4253 can_preserve = SvCANEXISTDELETE(av);
4256 if (lval && localizing) {
4259 for (svp = MARK + 1; svp <= SP; svp++) {
4260 const I32 elem = SvIV(*svp);
4264 if (max > AvMAX(av))
4268 while (++MARK <= SP) {
4270 I32 elem = SvIV(*MARK);
4271 bool preeminent = TRUE;
4273 if (localizing && can_preserve) {
4274 /* If we can determine whether the element exist,
4275 * Try to preserve the existenceness of a tied array
4276 * element by using EXISTS and DELETE if possible.
4277 * Fallback to FETCH and STORE otherwise. */
4278 preeminent = av_exists(av, elem);
4281 svp = av_fetch(av, elem, lval);
4283 if (!svp || *svp == &PL_sv_undef)
4284 DIE(aTHX_ PL_no_aelem, elem);
4287 save_aelem(av, elem, svp);
4289 SAVEADELETE(av, elem);
4292 *MARK = svp ? *svp : &PL_sv_undef;
4295 if (GIMME != G_ARRAY) {
4297 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
4303 /* Smart dereferencing for keys, values and each */
4315 (SvTYPE(sv) != SVt_PVHV && SvTYPE(sv) != SVt_PVAV)
4320 "Type of argument to %s must be unblessed hashref or arrayref",
4321 PL_op_desc[PL_op->op_type] );
4324 if (PL_op->op_flags & OPf_SPECIAL && SvTYPE(sv) == SVt_PVAV)
4326 "Can't modify %s in %s",
4327 PL_op_desc[PL_op->op_type], PL_op_desc[PL_op->op_next->op_type]
4330 /* Delegate to correct function for op type */
4332 if (PL_op->op_type == OP_RKEYS || PL_op->op_type == OP_RVALUES) {
4333 return (SvTYPE(sv) == SVt_PVHV) ? Perl_do_kv(aTHX) : Perl_pp_akeys(aTHX);
4336 return (SvTYPE(sv) == SVt_PVHV) ? Perl_pp_each(aTHX) : Perl_pp_aeach(aTHX);
4344 AV *array = MUTABLE_AV(POPs);
4345 const I32 gimme = GIMME_V;
4346 IV *iterp = Perl_av_iter_p(aTHX_ array);
4347 const IV current = (*iterp)++;
4349 if (current > av_len(array)) {
4351 if (gimme == G_SCALAR)
4359 if (gimme == G_ARRAY) {
4360 SV **const element = av_fetch(array, current, 0);
4361 PUSHs(element ? *element : &PL_sv_undef);
4370 AV *array = MUTABLE_AV(POPs);
4371 const I32 gimme = GIMME_V;
4373 *Perl_av_iter_p(aTHX_ array) = 0;
4375 if (gimme == G_SCALAR) {
4377 PUSHi(av_len(array) + 1);
4379 else if (gimme == G_ARRAY) {
4380 IV n = Perl_av_len(aTHX_ array);
4385 if (PL_op->op_type == OP_AKEYS || PL_op->op_type == OP_RKEYS) {
4386 for (i = 0; i <= n; i++) {
4391 for (i = 0; i <= n; i++) {
4392 SV *const *const elem = Perl_av_fetch(aTHX_ array, i, 0);
4393 PUSHs(elem ? *elem : &PL_sv_undef);
4400 /* Associative arrays. */
4406 HV * hash = MUTABLE_HV(POPs);
4408 const I32 gimme = GIMME_V;
4411 /* might clobber stack_sp */
4412 entry = hv_iternext(hash);
4417 SV* const sv = hv_iterkeysv(entry);
4418 PUSHs(sv); /* won't clobber stack_sp */
4419 if (gimme == G_ARRAY) {
4422 /* might clobber stack_sp */
4423 val = hv_iterval(hash, entry);
4428 else if (gimme == G_SCALAR)
4435 S_do_delete_local(pTHX)
4439 const I32 gimme = GIMME_V;
4442 const bool sliced = !!(PL_op->op_private & OPpSLICE);
4443 SV *unsliced_keysv = sliced ? NULL : POPs;
4444 SV * const osv = POPs;
4445 SV **mark = sliced ? PL_stack_base + POPMARK : &unsliced_keysv-1;
4447 const bool tied = SvRMAGICAL(osv)
4448 && mg_find((const SV *)osv, PERL_MAGIC_tied);
4449 const bool can_preserve = SvCANEXISTDELETE(osv);
4450 const U32 type = SvTYPE(osv);
4451 SV ** const end = sliced ? SP : &unsliced_keysv;
4453 if (type == SVt_PVHV) { /* hash element */
4454 HV * const hv = MUTABLE_HV(osv);
4455 while (++MARK <= end) {
4456 SV * const keysv = *MARK;
4458 bool preeminent = TRUE;
4460 preeminent = hv_exists_ent(hv, keysv, 0);
4462 HE *he = hv_fetch_ent(hv, keysv, 1, 0);
4469 sv = hv_delete_ent(hv, keysv, 0, 0);
4470 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4473 if (!sv) DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
4474 save_helem_flags(hv, keysv, &sv, SAVEf_KEEPOLDELEM);
4476 *MARK = sv_mortalcopy(sv);
4482 SAVEHDELETE(hv, keysv);
4483 *MARK = &PL_sv_undef;
4487 else if (type == SVt_PVAV) { /* array element */
4488 if (PL_op->op_flags & OPf_SPECIAL) {
4489 AV * const av = MUTABLE_AV(osv);
4490 while (++MARK <= end) {
4491 I32 idx = SvIV(*MARK);
4493 bool preeminent = TRUE;
4495 preeminent = av_exists(av, idx);
4497 SV **svp = av_fetch(av, idx, 1);
4504 sv = av_delete(av, idx, 0);
4505 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4508 save_aelem_flags(av, idx, &sv, SAVEf_KEEPOLDELEM);
4510 *MARK = sv_mortalcopy(sv);
4516 SAVEADELETE(av, idx);
4517 *MARK = &PL_sv_undef;
4522 DIE(aTHX_ "panic: avhv_delete no longer supported");
4525 DIE(aTHX_ "Not a HASH reference");
4527 if (gimme == G_VOID)
4529 else if (gimme == G_SCALAR) {
4534 *++MARK = &PL_sv_undef;
4538 else if (gimme != G_VOID)
4539 PUSHs(unsliced_keysv);
4551 if (PL_op->op_private & OPpLVAL_INTRO)
4552 return do_delete_local();
4555 discard = (gimme == G_VOID) ? G_DISCARD : 0;
4557 if (PL_op->op_private & OPpSLICE) {
4559 HV * const hv = MUTABLE_HV(POPs);
4560 const U32 hvtype = SvTYPE(hv);
4561 if (hvtype == SVt_PVHV) { /* hash element */
4562 while (++MARK <= SP) {
4563 SV * const sv = hv_delete_ent(hv, *MARK, discard, 0);
4564 *MARK = sv ? sv : &PL_sv_undef;
4567 else if (hvtype == SVt_PVAV) { /* array element */
4568 if (PL_op->op_flags & OPf_SPECIAL) {
4569 while (++MARK <= SP) {
4570 SV * const sv = av_delete(MUTABLE_AV(hv), SvIV(*MARK), discard);
4571 *MARK = sv ? sv : &PL_sv_undef;
4576 DIE(aTHX_ "Not a HASH reference");
4579 else if (gimme == G_SCALAR) {
4584 *++MARK = &PL_sv_undef;
4590 HV * const hv = MUTABLE_HV(POPs);
4592 if (SvTYPE(hv) == SVt_PVHV)
4593 sv = hv_delete_ent(hv, keysv, discard, 0);
4594 else if (SvTYPE(hv) == SVt_PVAV) {
4595 if (PL_op->op_flags & OPf_SPECIAL)
4596 sv = av_delete(MUTABLE_AV(hv), SvIV(keysv), discard);
4598 DIE(aTHX_ "panic: avhv_delete no longer supported");
4601 DIE(aTHX_ "Not a HASH reference");
4617 if (PL_op->op_private & OPpEXISTS_SUB) {
4619 SV * const sv = POPs;
4620 CV * const cv = sv_2cv(sv, &hv, &gv, 0);
4623 if (gv && isGV(gv) && GvCV(gv) && !GvCVGEN(gv))
4628 hv = MUTABLE_HV(POPs);
4629 if (SvTYPE(hv) == SVt_PVHV) {
4630 if (hv_exists_ent(hv, tmpsv, 0))
4633 else if (SvTYPE(hv) == SVt_PVAV) {
4634 if (PL_op->op_flags & OPf_SPECIAL) { /* array element */
4635 if (av_exists(MUTABLE_AV(hv), SvIV(tmpsv)))
4640 DIE(aTHX_ "Not a HASH reference");
4647 dVAR; dSP; dMARK; dORIGMARK;
4648 HV * const hv = MUTABLE_HV(POPs);
4649 const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
4650 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
4651 bool can_preserve = FALSE;
4657 if (SvCANEXISTDELETE(hv))
4658 can_preserve = TRUE;
4661 while (++MARK <= SP) {
4662 SV * const keysv = *MARK;
4665 bool preeminent = TRUE;
4667 if (localizing && can_preserve) {
4668 /* If we can determine whether the element exist,
4669 * try to preserve the existenceness of a tied hash
4670 * element by using EXISTS and DELETE if possible.
4671 * Fallback to FETCH and STORE otherwise. */
4672 preeminent = hv_exists_ent(hv, keysv, 0);
4675 he = hv_fetch_ent(hv, keysv, lval, 0);
4676 svp = he ? &HeVAL(he) : NULL;
4679 if (!svp || !*svp || *svp == &PL_sv_undef) {
4680 DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
4683 if (HvNAME_get(hv) && isGV(*svp))
4684 save_gp(MUTABLE_GV(*svp), !(PL_op->op_flags & OPf_SPECIAL));
4685 else if (preeminent)
4686 save_helem_flags(hv, keysv, svp,
4687 (PL_op->op_flags & OPf_SPECIAL) ? 0 : SAVEf_SETMAGIC);
4689 SAVEHDELETE(hv, keysv);
4692 *MARK = svp && *svp ? *svp : &PL_sv_undef;
4694 if (GIMME != G_ARRAY) {
4696 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
4702 /* List operators. */
4707 if (GIMME != G_ARRAY) {
4709 *MARK = *SP; /* unwanted list, return last item */
4711 *MARK = &PL_sv_undef;
4721 SV ** const lastrelem = PL_stack_sp;
4722 SV ** const lastlelem = PL_stack_base + POPMARK;
4723 SV ** const firstlelem = PL_stack_base + POPMARK + 1;
4724 SV ** const firstrelem = lastlelem + 1;
4725 I32 is_something_there = FALSE;
4727 const I32 max = lastrelem - lastlelem;
4730 if (GIMME != G_ARRAY) {
4731 I32 ix = SvIV(*lastlelem);
4734 if (ix < 0 || ix >= max)
4735 *firstlelem = &PL_sv_undef;
4737 *firstlelem = firstrelem[ix];
4743 SP = firstlelem - 1;
4747 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
4748 I32 ix = SvIV(*lelem);
4751 if (ix < 0 || ix >= max)
4752 *lelem = &PL_sv_undef;
4754 is_something_there = TRUE;
4755 if (!(*lelem = firstrelem[ix]))
4756 *lelem = &PL_sv_undef;
4759 if (is_something_there)
4762 SP = firstlelem - 1;
4768 dVAR; dSP; dMARK; dORIGMARK;
4769 const I32 items = SP - MARK;
4770 SV * const av = MUTABLE_SV(av_make(items, MARK+1));
4771 SP = ORIGMARK; /* av_make() might realloc stack_sp */
4772 mXPUSHs((PL_op->op_flags & OPf_SPECIAL)
4773 ? newRV_noinc(av) : av);
4779 dVAR; dSP; dMARK; dORIGMARK;
4780 HV* const hv = newHV();
4783 SV * const key = *++MARK;
4784 SV * const val = newSV(0);
4786 sv_setsv(val, *++MARK);
4788 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Odd number of elements in anonymous hash");
4789 (void)hv_store_ent(hv,key,val,0);
4792 mXPUSHs((PL_op->op_flags & OPf_SPECIAL)
4793 ? newRV_noinc(MUTABLE_SV(hv)) : MUTABLE_SV(hv));
4798 S_deref_plain_array(pTHX_ AV *ary)
4800 if (SvTYPE(ary) == SVt_PVAV) return ary;
4801 SvGETMAGIC((SV *)ary);
4802 if (!SvROK(ary) || SvTYPE(SvRV(ary)) != SVt_PVAV)
4803 Perl_die(aTHX_ "Not an ARRAY reference");
4804 else if (SvOBJECT(SvRV(ary)))
4805 Perl_die(aTHX_ "Not an unblessed ARRAY reference");
4806 return (AV *)SvRV(ary);
4809 #if defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
4810 # define DEREF_PLAIN_ARRAY(ary) \
4813 SvTYPE(aRrRay) == SVt_PVAV \
4815 : S_deref_plain_array(aTHX_ aRrRay); \
4818 # define DEREF_PLAIN_ARRAY(ary) \
4820 PL_Sv = (SV *)(ary), \
4821 SvTYPE(PL_Sv) == SVt_PVAV \
4823 : S_deref_plain_array(aTHX_ (AV *)PL_Sv) \
4829 dVAR; dSP; dMARK; dORIGMARK;
4830 int num_args = (SP - MARK);
4831 AV *ary = DEREF_PLAIN_ARRAY(MUTABLE_AV(*++MARK));
4840 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
4843 return Perl_tied_method(aTHX_ "SPLICE", mark - 1, MUTABLE_SV(ary), mg,
4844 GIMME_V | TIED_METHOD_ARGUMENTS_ON_STACK,
4851 offset = i = SvIV(*MARK);
4853 offset += AvFILLp(ary) + 1;
4855 DIE(aTHX_ PL_no_aelem, i);
4857 length = SvIVx(*MARK++);
4859 length += AvFILLp(ary) - offset + 1;
4865 length = AvMAX(ary) + 1; /* close enough to infinity */
4869 length = AvMAX(ary) + 1;
4871 if (offset > AvFILLp(ary) + 1) {
4873 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "splice() offset past end of array" );
4874 offset = AvFILLp(ary) + 1;
4876 after = AvFILLp(ary) + 1 - (offset + length);
4877 if (after < 0) { /* not that much array */
4878 length += after; /* offset+length now in array */
4884 /* At this point, MARK .. SP-1 is our new LIST */
4887 diff = newlen - length;
4888 if (newlen && !AvREAL(ary) && AvREIFY(ary))
4891 /* make new elements SVs now: avoid problems if they're from the array */
4892 for (dst = MARK, i = newlen; i; i--) {
4893 SV * const h = *dst;
4894 *dst++ = newSVsv(h);
4897 if (diff < 0) { /* shrinking the area */
4898 SV **tmparyval = NULL;
4900 Newx(tmparyval, newlen, SV*); /* so remember insertion */
4901 Copy(MARK, tmparyval, newlen, SV*);
4904 MARK = ORIGMARK + 1;
4905 if (GIMME == G_ARRAY) { /* copy return vals to stack */
4906 MEXTEND(MARK, length);
4907 Copy(AvARRAY(ary)+offset, MARK, length, SV*);
4909 EXTEND_MORTAL(length);
4910 for (i = length, dst = MARK; i; i--) {
4911 sv_2mortal(*dst); /* free them eventually */
4918 *MARK = AvARRAY(ary)[offset+length-1];
4921 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
4922 SvREFCNT_dec(*dst++); /* free them now */
4925 AvFILLp(ary) += diff;
4927 /* pull up or down? */
4929 if (offset < after) { /* easier to pull up */
4930 if (offset) { /* esp. if nothing to pull */
4931 src = &AvARRAY(ary)[offset-1];
4932 dst = src - diff; /* diff is negative */
4933 for (i = offset; i > 0; i--) /* can't trust Copy */
4937 AvARRAY(ary) = AvARRAY(ary) - diff; /* diff is negative */
4941 if (after) { /* anything to pull down? */
4942 src = AvARRAY(ary) + offset + length;
4943 dst = src + diff; /* diff is negative */
4944 Move(src, dst, after, SV*);
4946 dst = &AvARRAY(ary)[AvFILLp(ary)+1];
4947 /* avoid later double free */
4951 dst[--i] = &PL_sv_undef;
4954 Copy( tmparyval, AvARRAY(ary) + offset, newlen, SV* );
4955 Safefree(tmparyval);
4958 else { /* no, expanding (or same) */
4959 SV** tmparyval = NULL;
4961 Newx(tmparyval, length, SV*); /* so remember deletion */
4962 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
4965 if (diff > 0) { /* expanding */
4966 /* push up or down? */
4967 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
4971 Move(src, dst, offset, SV*);
4973 AvARRAY(ary) = AvARRAY(ary) - diff;/* diff is positive */
4975 AvFILLp(ary) += diff;
4978 if (AvFILLp(ary) + diff >= AvMAX(ary)) /* oh, well */
4979 av_extend(ary, AvFILLp(ary) + diff);
4980 AvFILLp(ary) += diff;
4983 dst = AvARRAY(ary) + AvFILLp(ary);
4985 for (i = after; i; i--) {
4993 Copy( MARK, AvARRAY(ary) + offset, newlen, SV* );
4996 MARK = ORIGMARK + 1;
4997 if (GIMME == G_ARRAY) { /* copy return vals to stack */
4999 Copy(tmparyval, MARK, length, SV*);
5001 EXTEND_MORTAL(length);
5002 for (i = length, dst = MARK; i; i--) {
5003 sv_2mortal(*dst); /* free them eventually */
5010 else if (length--) {
5011 *MARK = tmparyval[length];
5014 while (length-- > 0)
5015 SvREFCNT_dec(tmparyval[length]);
5019 *MARK = &PL_sv_undef;
5020 Safefree(tmparyval);
5024 mg_set(MUTABLE_SV(ary));
5032 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
5033 AV * const ary = DEREF_PLAIN_ARRAY(MUTABLE_AV(*++MARK));
5034 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
5037 *MARK-- = SvTIED_obj(MUTABLE_SV(ary), mg);
5040 ENTER_with_name("call_PUSH");
5041 call_method("PUSH",G_SCALAR|G_DISCARD);
5042 LEAVE_with_name("call_PUSH");
5046 PL_delaymagic = DM_DELAY;
5047 for (++MARK; MARK <= SP; MARK++) {
5048 SV * const sv = newSV(0);
5050 sv_setsv(sv, *MARK);
5051 av_store(ary, AvFILLp(ary)+1, sv);
5053 if (PL_delaymagic & DM_ARRAY_ISA)
5054 mg_set(MUTABLE_SV(ary));
5059 if (OP_GIMME(PL_op, 0) != G_VOID) {
5060 PUSHi( AvFILL(ary) + 1 );
5069 AV * const av = PL_op->op_flags & OPf_SPECIAL
5070 ? MUTABLE_AV(GvAV(PL_defgv)) : DEREF_PLAIN_ARRAY(MUTABLE_AV(POPs));
5071 SV * const sv = PL_op->op_type == OP_SHIFT ? av_shift(av) : av_pop(av);
5075 (void)sv_2mortal(sv);
5082 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
5083 AV *ary = DEREF_PLAIN_ARRAY(MUTABLE_AV(*++MARK));
5084 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
5087 *MARK-- = SvTIED_obj(MUTABLE_SV(ary), mg);
5090 ENTER_with_name("call_UNSHIFT");
5091 call_method("UNSHIFT",G_SCALAR|G_DISCARD);
5092 LEAVE_with_name("call_UNSHIFT");
5097 av_unshift(ary, SP - MARK);
5099 SV * const sv = newSVsv(*++MARK);
5100 (void)av_store(ary, i++, sv);
5104 if (OP_GIMME(PL_op, 0) != G_VOID) {
5105 PUSHi( AvFILL(ary) + 1 );
5114 if (GIMME == G_ARRAY) {
5115 if (PL_op->op_private & OPpREVERSE_INPLACE) {
5119 assert( MARK+1 == SP && *SP && SvTYPE(*SP) == SVt_PVAV);
5120 (void)POPMARK; /* remove mark associated with ex-OP_AASSIGN */
5121 av = MUTABLE_AV((*SP));
5122 /* In-place reversing only happens in void context for the array
5123 * assignment. We don't need to push anything on the stack. */
5126 if (SvMAGICAL(av)) {
5128 SV *tmp = sv_newmortal();
5129 /* For SvCANEXISTDELETE */
5132 bool can_preserve = SvCANEXISTDELETE(av);
5134 for (i = 0, j = av_len(av); i < j; ++i, --j) {
5138 if (!av_exists(av, i)) {
5139 if (av_exists(av, j)) {
5140 SV *sv = av_delete(av, j, 0);
5141 begin = *av_fetch(av, i, TRUE);
5142 sv_setsv_mg(begin, sv);
5146 else if (!av_exists(av, j)) {
5147 SV *sv = av_delete(av, i, 0);
5148 end = *av_fetch(av, j, TRUE);
5149 sv_setsv_mg(end, sv);
5154 begin = *av_fetch(av, i, TRUE);
5155 end = *av_fetch(av, j, TRUE);
5156 sv_setsv(tmp, begin);
5157 sv_setsv_mg(begin, end);
5158 sv_setsv_mg(end, tmp);
5162 SV **begin = AvARRAY(av);
5165 SV **end = begin + AvFILLp(av);
5167 while (begin < end) {
5168 SV * const tmp = *begin;
5179 SV * const tmp = *MARK;
5183 /* safe as long as stack cannot get extended in the above */
5194 SvUTF8_off(TARG); /* decontaminate */
5196 do_join(TARG, &PL_sv_no, MARK, SP);
5198 sv_setsv(TARG, SP > MARK ? *SP : find_rundefsv());
5199 if (! SvOK(TARG) && ckWARN(WARN_UNINITIALIZED))
5200 report_uninit(TARG);
5203 up = SvPV_force(TARG, len);
5205 if (DO_UTF8(TARG)) { /* first reverse each character */
5206 U8* s = (U8*)SvPVX(TARG);
5207 const U8* send = (U8*)(s + len);
5209 if (UTF8_IS_INVARIANT(*s)) {
5214 if (!utf8_to_uvchr_buf(s, send, 0))
5218 down = (char*)(s - 1);
5219 /* reverse this character */
5223 *down-- = (char)tmp;
5229 down = SvPVX(TARG) + len - 1;
5233 *down-- = (char)tmp;
5235 (void)SvPOK_only_UTF8(TARG);
5247 IV limit = POPi; /* note, negative is forever */
5248 SV * const sv = POPs;
5250 const char *s = SvPV_const(sv, len);
5251 const bool do_utf8 = DO_UTF8(sv);
5252 const char *strend = s + len;
5258 const STRLEN slen = do_utf8 ? utf8_length((U8*)s, (U8*)strend) : (STRLEN)(strend - s);
5259 I32 maxiters = slen + 10;
5260 I32 trailing_empty = 0;
5262 const I32 origlimit = limit;
5265 const I32 gimme = GIMME_V;
5267 const I32 oldsave = PL_savestack_ix;
5268 U32 make_mortal = SVs_TEMP;
5273 Copy(&LvTARGOFF(POPs), &pm, 1, PMOP*);
5278 DIE(aTHX_ "panic: pp_split, pm=%p, s=%p", pm, s);
5281 TAINT_IF(get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET &&
5282 (RX_EXTFLAGS(rx) & (RXf_WHITE | RXf_SKIPWHITE)));
5284 RX_MATCH_UTF8_set(rx, do_utf8);
5287 if (pm->op_pmreplrootu.op_pmtargetoff) {
5288 ary = GvAVn(MUTABLE_GV(PAD_SVl(pm->op_pmreplrootu.op_pmtargetoff)));
5291 if (pm->op_pmreplrootu.op_pmtargetgv) {
5292 ary = GvAVn(pm->op_pmreplrootu.op_pmtargetgv);
5297 if (ary && (gimme != G_ARRAY || (pm->op_pmflags & PMf_ONCE))) {
5303 if ((mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied))) {
5305 XPUSHs(SvTIED_obj(MUTABLE_SV(ary), mg));
5312 for (i = AvFILLp(ary); i >= 0; i--)
5313 AvARRAY(ary)[i] = &PL_sv_undef; /* don't free mere refs */
5315 /* temporarily switch stacks */
5316 SAVESWITCHSTACK(PL_curstack, ary);
5320 base = SP - PL_stack_base;
5322 if (RX_EXTFLAGS(rx) & RXf_SKIPWHITE) {
5324 while (*s == ' ' || is_utf8_space((U8*)s))
5327 else if (get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET) {
5328 while (isSPACE_LC(*s))
5336 if (RX_EXTFLAGS(rx) & RXf_PMf_MULTILINE) {
5340 gimme_scalar = gimme == G_SCALAR && !ary;
5343 limit = maxiters + 2;
5344 if (RX_EXTFLAGS(rx) & RXf_WHITE) {
5347 /* this one uses 'm' and is a negative test */
5349 while (m < strend && !( *m == ' ' || is_utf8_space((U8*)m) )) {
5350 const int t = UTF8SKIP(m);
5351 /* is_utf8_space returns FALSE for malform utf8 */
5358 else if (get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET) {
5359 while (m < strend && !isSPACE_LC(*m))
5362 while (m < strend && !isSPACE(*m))
5375 dstr = newSVpvn_flags(s, m-s,
5376 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5380 /* skip the whitespace found last */
5382 s = m + UTF8SKIP(m);
5386 /* this one uses 's' and is a positive test */
5388 while (s < strend && ( *s == ' ' || is_utf8_space((U8*)s) ))
5391 else if (get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET) {
5392 while (s < strend && isSPACE_LC(*s))
5395 while (s < strend && isSPACE(*s))
5400 else if (RX_EXTFLAGS(rx) & RXf_START_ONLY) {
5402 for (m = s; m < strend && *m != '\n'; m++)
5415 dstr = newSVpvn_flags(s, m-s,
5416 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5422 else if (RX_EXTFLAGS(rx) & RXf_NULL && !(s >= strend)) {
5424 Pre-extend the stack, either the number of bytes or
5425 characters in the string or a limited amount, triggered by:
5427 my ($x, $y) = split //, $str;
5431 if (!gimme_scalar) {
5432 const U32 items = limit - 1;
5441 /* keep track of how many bytes we skip over */
5451 dstr = newSVpvn_flags(m, s-m, SVf_UTF8 | make_mortal);
5464 dstr = newSVpvn(s, 1);
5480 else if (do_utf8 == (RX_UTF8(rx) != 0) &&
5481 (RX_EXTFLAGS(rx) & RXf_USE_INTUIT) && !RX_NPARENS(rx)
5482 && (RX_EXTFLAGS(rx) & RXf_CHECK_ALL)
5483 && !(RX_EXTFLAGS(rx) & RXf_ANCH)) {
5484 const int tail = (RX_EXTFLAGS(rx) & RXf_INTUIT_TAIL);
5485 SV * const csv = CALLREG_INTUIT_STRING(rx);
5487 len = RX_MINLENRET(rx);
5488 if (len == 1 && !RX_UTF8(rx) && !tail) {
5489 const char c = *SvPV_nolen_const(csv);
5491 for (m = s; m < strend && *m != c; m++)
5502 dstr = newSVpvn_flags(s, m-s,
5503 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5506 /* The rx->minlen is in characters but we want to step
5507 * s ahead by bytes. */
5509 s = (char*)utf8_hop((U8*)m, len);
5511 s = m + len; /* Fake \n at the end */
5515 while (s < strend && --limit &&
5516 (m = fbm_instr((unsigned char*)s, (unsigned char*)strend,
5517 csv, multiline ? FBMrf_MULTILINE : 0)) )
5526 dstr = newSVpvn_flags(s, m-s,
5527 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5530 /* The rx->minlen is in characters but we want to step
5531 * s ahead by bytes. */
5533 s = (char*)utf8_hop((U8*)m, len);
5535 s = m + len; /* Fake \n at the end */
5540 maxiters += slen * RX_NPARENS(rx);
5541 while (s < strend && --limit)
5545 rex_return = CALLREGEXEC(rx, (char*)s, (char*)strend, (char*)orig, 1 ,
5548 if (rex_return == 0)
5550 TAINT_IF(RX_MATCH_TAINTED(rx));
5551 if (RX_MATCH_COPIED(rx) && RX_SUBBEG(rx) != orig) {
5554 orig = RX_SUBBEG(rx);
5556 strend = s + (strend - m);
5558 m = RX_OFFS(rx)[0].start + orig;
5567 dstr = newSVpvn_flags(s, m-s,
5568 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5571 if (RX_NPARENS(rx)) {
5573 for (i = 1; i <= (I32)RX_NPARENS(rx); i++) {
5574 s = RX_OFFS(rx)[i].start + orig;
5575 m = RX_OFFS(rx)[i].end + orig;
5577 /* japhy (07/27/01) -- the (m && s) test doesn't catch
5578 parens that didn't match -- they should be set to
5579 undef, not the empty string */
5587 if (m >= orig && s >= orig) {
5588 dstr = newSVpvn_flags(s, m-s,
5589 (do_utf8 ? SVf_UTF8 : 0)
5593 dstr = &PL_sv_undef; /* undef, not "" */
5599 s = RX_OFFS(rx)[0].end + orig;
5603 if (!gimme_scalar) {
5604 iters = (SP - PL_stack_base) - base;
5606 if (iters > maxiters)
5607 DIE(aTHX_ "Split loop");
5609 /* keep field after final delim? */
5610 if (s < strend || (iters && origlimit)) {
5611 if (!gimme_scalar) {
5612 const STRLEN l = strend - s;
5613 dstr = newSVpvn_flags(s, l, (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5618 else if (!origlimit) {
5620 iters -= trailing_empty;
5622 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0)) {
5623 if (TOPs && !make_mortal)
5625 *SP-- = &PL_sv_undef;
5632 LEAVE_SCOPE(oldsave); /* may undo an earlier SWITCHSTACK */
5636 if (SvSMAGICAL(ary)) {
5638 mg_set(MUTABLE_SV(ary));
5641 if (gimme == G_ARRAY) {
5643 Copy(AvARRAY(ary), SP + 1, iters, SV*);
5650 ENTER_with_name("call_PUSH");
5651 call_method("PUSH",G_SCALAR|G_DISCARD);
5652 LEAVE_with_name("call_PUSH");
5654 if (gimme == G_ARRAY) {
5656 /* EXTEND should not be needed - we just popped them */
5658 for (i=0; i < iters; i++) {
5659 SV **svp = av_fetch(ary, i, FALSE);
5660 PUSHs((svp) ? *svp : &PL_sv_undef);
5667 if (gimme == G_ARRAY)
5679 SV *const sv = PAD_SVl(PL_op->op_targ);
5681 if (SvPADSTALE(sv)) {
5684 RETURNOP(cLOGOP->op_other);
5686 RETURNOP(cLOGOP->op_next);
5696 if (SvTYPE(retsv) == SVt_PVAV || SvTYPE(retsv) == SVt_PVHV
5697 || SvTYPE(retsv) == SVt_PVCV) {
5698 retsv = refto(retsv);
5705 PP(unimplemented_op)
5708 const Optype op_type = PL_op->op_type;
5709 /* Using OP_NAME() isn't going to be helpful here. Firstly, it doesn't cope
5710 with out of range op numbers - it only "special" cases op_custom.
5711 Secondly, as the three ops we "panic" on are padmy, mapstart and custom,
5712 if we get here for a custom op then that means that the custom op didn't
5713 have an implementation. Given that OP_NAME() looks up the custom op
5714 by its pp_addr, likely it will return NULL, unless someone (unhelpfully)
5715 registers &PL_unimplemented_op as the address of their custom op.
5716 NULL doesn't generate a useful error message. "custom" does. */
5717 const char *const name = op_type >= OP_max
5718 ? "[out of range]" : PL_op_name[PL_op->op_type];
5719 if(OP_IS_SOCKET(op_type))
5720 DIE(aTHX_ PL_no_sock_func, name);
5721 DIE(aTHX_ "panic: unimplemented op %s (#%d) called", name, op_type);
5729 HV * const hv = (HV*)TOPs;
5731 if (SvTYPE(hv) != SVt_PVHV) RETSETNO;
5733 if (SvRMAGICAL(hv)) {
5734 MAGIC * const mg = mg_find((SV*)hv, PERL_MAGIC_tied);
5736 SETs(magic_scalarpack(hv, mg));
5741 if (HvUSEDKEYS(hv) != 0) RETSETYES;
5742 else SETi(0); /* for $ret = %hash && foo() */
5746 /* For sorting out arguments passed to a &CORE:: subroutine */
5750 int opnum = SvIOK(cSVOP_sv) ? (int)SvUV(cSVOP_sv) : 0;
5751 int defgv = PL_opargs[opnum] & OA_DEFGV ||opnum==OP_GLOB, whicharg = 0;
5752 AV * const at_ = GvAV(PL_defgv);
5753 SV **svp = at_ ? AvARRAY(at_) : NULL;
5754 I32 minargs = 0, maxargs = 0, numargs = at_ ? AvFILLp(at_)+1 : 0;
5755 I32 oa = opnum ? PL_opargs[opnum] >> OASHIFT : 0;
5756 bool seen_question = 0;
5757 const char *err = NULL;
5758 const bool pushmark = PL_op->op_private & OPpCOREARGS_PUSHMARK;
5760 /* Count how many args there are first, to get some idea how far to
5761 extend the stack. */
5763 if ((oa & 7) == OA_LIST) { maxargs = I32_MAX; break; }
5765 if (oa & OA_OPTIONAL) seen_question = 1;
5766 if (!seen_question) minargs++;
5770 if(numargs < minargs) err = "Not enough";
5771 else if(numargs > maxargs) err = "Too many";
5773 /* diag_listed_as: Too many arguments for %s */
5775 "%s arguments for %s", err,
5776 opnum ? PL_op_desc[opnum] : SvPV_nolen_const(cSVOP_sv)
5779 /* Reset the stack pointer. Without this, we end up returning our own
5780 arguments in list context, in addition to the values we are supposed
5781 to return. nextstate usually does this on sub entry, but we need
5782 to run the next op with the caller's hints, so we cannot have a
5784 SP = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;
5786 if(!maxargs) RETURN;
5788 /* We do this here, rather than with a separate pushmark op, as it has
5789 to come in between two things this function does (stack reset and
5790 arg pushing). This seems the easiest way to do it. */
5793 (void)Perl_pp_pushmark(aTHX);
5796 EXTEND(SP, maxargs == I32_MAX ? numargs : maxargs);
5797 PUTBACK; /* The code below can die in various places. */
5799 oa = PL_opargs[opnum] >> OASHIFT;
5800 for (; oa&&(numargs||!pushmark); (void)(numargs&&(++svp,--numargs))) {
5805 if (!numargs && defgv && whicharg == minargs + 1) {
5806 PUSHs(find_rundefsv2(
5807 find_runcv_where(FIND_RUNCV_level_eq, 1, NULL),
5808 cxstack[cxstack_ix].blk_oldcop->cop_seq
5811 else PUSHs(numargs ? svp && *svp ? *svp : &PL_sv_undef : NULL);
5815 PUSHs(svp && *svp ? *svp : &PL_sv_undef);
5820 if (!svp || !*svp || !SvROK(*svp)
5821 || SvTYPE(SvRV(*svp)) != SVt_PVHV)
5823 /* diag_listed_as: Type of arg %d to &CORE::%s must be %s*/
5824 "Type of arg %d to &CORE::%s must be hash reference",
5825 whicharg, OP_DESC(PL_op->op_next)
5830 if (!numargs) PUSHs(NULL);
5831 else if(svp && *svp && SvROK(*svp) && isGV_with_GP(SvRV(*svp)))
5832 /* no magic here, as the prototype will have added an extra
5833 refgen and we just want what was there before that */
5836 const bool constr = PL_op->op_private & whicharg;
5838 svp && *svp ? *svp : &PL_sv_undef,
5839 constr, CopHINTS_get(PL_curcop) & HINT_STRICT_REFS,
5845 if (!numargs) goto try_defsv;
5847 const bool wantscalar =
5848 PL_op->op_private & OPpCOREARGS_SCALARMOD;
5849 if (!svp || !*svp || !SvROK(*svp)
5850 /* We have to permit globrefs even for the \$ proto, as
5851 *foo is indistinguishable from ${\*foo}, and the proto-
5852 type permits the latter. */
5853 || SvTYPE(SvRV(*svp)) > (
5854 wantscalar ? SVt_PVLV
5855 : opnum == OP_LOCK || opnum == OP_UNDEF
5861 /* diag_listed_as: Type of arg %d to &CORE::%s must be %s*/
5862 "Type of arg %d to &CORE::%s must be %s",
5863 whicharg, PL_op_name[opnum],
5865 ? "scalar reference"
5866 : opnum == OP_LOCK || opnum == OP_UNDEF
5867 ? "reference to one of [$@%&*]"
5868 : "reference to one of [$@%*]"
5871 if (opnum == OP_UNDEF && SvRV(*svp) == (SV *)PL_defgv
5872 && cxstack[cxstack_ix].cx_type & CXp_HASARGS) {
5873 /* Undo @_ localisation, so that sub exit does not undo
5874 part of our undeffing. */
5875 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
5877 cx->cx_type &= ~ CXp_HASARGS;
5878 assert(!AvREAL(cx->blk_sub.argarray));
5883 DIE(aTHX_ "panic: unknown OA_*: %x", (unsigned)(oa&7));
5895 if (PL_op->op_private & OPpOFFBYONE) {
5896 cv = find_runcv_where(FIND_RUNCV_level_eq, 1, NULL);
5898 else cv = find_runcv(NULL);
5899 XPUSHs(CvEVAL(cv) ? &PL_sv_undef : sv_2mortal(newRV((SV *)cv)));
5906 * c-indentation-style: bsd
5908 * indent-tabs-mode: nil
5911 * ex: set ts=8 sts=4 sw=4 et: