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 (gimme == G_SCALAR) {
135 SV* const sv = Perl_hv_scalar(aTHX_ MUTABLE_HV(TARG));
143 static const char S_no_symref_sv[] =
144 "Can't use string (\"%" SVf32 "\"%s) as %s ref while \"strict refs\" in use";
146 /* In some cases this function inspects PL_op. If this function is called
147 for new op types, more bool parameters may need to be added in place of
150 When noinit is true, the absence of a gv will cause a retval of undef.
151 This is unrelated to the cv-to-gv assignment case.
155 S_rv2gv(pTHX_ SV *sv, const bool vivify_sv, const bool strict,
159 if (!isGV(sv) || SvFAKE(sv)) SvGETMAGIC(sv);
162 sv = amagic_deref_call(sv, to_gv_amg);
166 if (SvTYPE(sv) == SVt_PVIO) {
167 GV * const gv = MUTABLE_GV(sv_newmortal());
168 gv_init(gv, 0, "__ANONIO__", 10, 0);
169 GvIOp(gv) = MUTABLE_IO(sv);
170 SvREFCNT_inc_void_NN(sv);
173 else if (!isGV_with_GP(sv))
174 return (SV *)Perl_die(aTHX_ "Not a GLOB reference");
177 if (!isGV_with_GP(sv)) {
179 /* If this is a 'my' scalar and flag is set then vivify
182 if (vivify_sv && sv != &PL_sv_undef) {
185 Perl_croak_no_modify(aTHX);
186 if (cUNOP->op_targ) {
187 SV * const namesv = PAD_SV(cUNOP->op_targ);
188 gv = MUTABLE_GV(newSV(0));
189 gv_init_sv(gv, CopSTASH(PL_curcop), namesv, 0);
192 const char * const name = CopSTASHPV(PL_curcop);
193 gv = newGVgen_flags(name,
194 HvNAMEUTF8(CopSTASH(PL_curcop)) ? SVf_UTF8 : 0 );
196 prepare_SV_for_RV(sv);
197 SvRV_set(sv, MUTABLE_SV(gv));
202 if (PL_op->op_flags & OPf_REF || strict)
203 return (SV *)Perl_die(aTHX_ PL_no_usym, "a symbol");
204 if (ckWARN(WARN_UNINITIALIZED))
210 if (!(sv = MUTABLE_SV(gv_fetchsv_nomg(
211 sv, GV_ADDMG, SVt_PVGV
221 (SvPOKp(sv) && SvCUR(sv)>32 ? "..." : ""),
224 if ((PL_op->op_private & (OPpLVAL_INTRO|OPpDONT_INIT_GV))
225 == OPpDONT_INIT_GV) {
226 /* We are the target of a coderef assignment. Return
227 the scalar unchanged, and let pp_sasssign deal with
231 sv = MUTABLE_SV(gv_fetchsv_nomg(sv, GV_ADD, SVt_PVGV));
233 /* FAKE globs in the symbol table cause weird bugs (#77810) */
237 if (SvFAKE(sv) && !(PL_op->op_private & OPpALLOW_FAKE)) {
238 SV *newsv = sv_newmortal();
239 sv_setsv_flags(newsv, sv, 0);
251 sv, PL_op->op_private & OPpDEREF,
252 PL_op->op_private & HINT_STRICT_REFS,
253 ((PL_op->op_flags & OPf_SPECIAL) && !(PL_op->op_flags & OPf_MOD))
254 || PL_op->op_type == OP_READLINE
256 if (PL_op->op_private & OPpLVAL_INTRO)
257 save_gp(MUTABLE_GV(sv), !(PL_op->op_flags & OPf_SPECIAL));
262 /* Helper function for pp_rv2sv and pp_rv2av */
264 Perl_softref2xv(pTHX_ SV *const sv, const char *const what,
265 const svtype type, SV ***spp)
270 PERL_ARGS_ASSERT_SOFTREF2XV;
272 if (PL_op->op_private & HINT_STRICT_REFS) {
274 Perl_die(aTHX_ S_no_symref_sv, sv,
275 (SvPOKp(sv) && SvCUR(sv)>32 ? "..." : ""), what);
277 Perl_die(aTHX_ PL_no_usym, what);
281 PL_op->op_flags & OPf_REF &&
282 PL_op->op_next->op_type != OP_BOOLKEYS
284 Perl_die(aTHX_ PL_no_usym, what);
285 if (ckWARN(WARN_UNINITIALIZED))
287 if (type != SVt_PV && GIMME_V == G_ARRAY) {
291 **spp = &PL_sv_undef;
294 if ((PL_op->op_flags & OPf_SPECIAL) &&
295 !(PL_op->op_flags & OPf_MOD))
297 if (!(gv = gv_fetchsv_nomg(sv, GV_ADDMG, type)))
299 **spp = &PL_sv_undef;
304 gv = gv_fetchsv_nomg(sv, GV_ADD, type);
317 sv = amagic_deref_call(sv, to_sv_amg);
321 switch (SvTYPE(sv)) {
327 DIE(aTHX_ "Not a SCALAR reference");
334 if (!isGV_with_GP(gv)) {
335 gv = Perl_softref2xv(aTHX_ sv, "a SCALAR", SVt_PV, &sp);
341 if (PL_op->op_flags & OPf_MOD) {
342 if (PL_op->op_private & OPpLVAL_INTRO) {
343 if (cUNOP->op_first->op_type == OP_NULL)
344 sv = save_scalar(MUTABLE_GV(TOPs));
346 sv = save_scalar(gv);
348 Perl_croak(aTHX_ "%s", PL_no_localize_ref);
350 else if (PL_op->op_private & OPpDEREF)
351 sv = vivify_ref(sv, PL_op->op_private & OPpDEREF);
360 AV * const av = MUTABLE_AV(TOPs);
361 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
363 SV ** const sv = Perl_av_arylen_p(aTHX_ MUTABLE_AV(av));
365 *sv = newSV_type(SVt_PVMG);
366 sv_magic(*sv, MUTABLE_SV(av), PERL_MAGIC_arylen, NULL, 0);
370 SETs(sv_2mortal(newSViv(AvFILL(MUTABLE_AV(av)))));
379 if (PL_op->op_flags & OPf_MOD || LVRET) {
380 SV * const ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
381 sv_magic(ret, NULL, PERL_MAGIC_pos, NULL, 0);
383 LvTARG(ret) = SvREFCNT_inc_simple(sv);
384 PUSHs(ret); /* no SvSETMAGIC */
388 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
389 const MAGIC * const mg = mg_find(sv, PERL_MAGIC_regex_global);
390 if (mg && mg->mg_len >= 0) {
408 const I32 flags = (PL_op->op_flags & OPf_SPECIAL)
410 : ((PL_op->op_private & (OPpLVAL_INTRO|OPpMAY_RETURN_CONSTANT)) == OPpMAY_RETURN_CONSTANT)
413 /* We usually try to add a non-existent subroutine in case of AUTOLOAD. */
414 /* (But not in defined().) */
416 CV *cv = sv_2cv(TOPs, &stash_unused, &gv, flags);
418 else if ((flags == (GV_ADD|GV_NOEXPAND)) && gv && SvROK(gv)) {
422 cv = MUTABLE_CV(&PL_sv_undef);
423 SETs(MUTABLE_SV(cv));
433 SV *ret = &PL_sv_undef;
435 if (SvGMAGICAL(TOPs)) SETs(sv_mortalcopy(TOPs));
436 if (SvPOK(TOPs) && SvCUR(TOPs) >= 7) {
437 const char * s = SvPVX_const(TOPs);
438 if (strnEQ(s, "CORE::", 6)) {
439 const int code = keyword(s + 6, SvCUR(TOPs) - 6, 1);
440 if (!code || code == -KEY_CORE)
441 DIE(aTHX_ "Can't find an opnumber for \"%"SVf"\"",
442 SVfARG(newSVpvn_flags(
443 s+6, SvCUR(TOPs)-6, SvFLAGS(TOPs) & SVf_UTF8
446 SV * const sv = core_prototype(NULL, s + 6, code, NULL);
452 cv = sv_2cv(TOPs, &stash, &gv, 0);
454 ret = newSVpvn_flags(
455 CvPROTO(cv), CvPROTOLEN(cv), SVs_TEMP | SvUTF8(cv)
465 CV *cv = MUTABLE_CV(PAD_SV(PL_op->op_targ));
467 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
469 PUSHs(MUTABLE_SV(cv));
483 if (GIMME != G_ARRAY) {
487 *MARK = &PL_sv_undef;
488 *MARK = refto(*MARK);
492 EXTEND_MORTAL(SP - MARK);
494 *MARK = refto(*MARK);
499 S_refto(pTHX_ SV *sv)
504 PERL_ARGS_ASSERT_REFTO;
506 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
509 if (!(sv = LvTARG(sv)))
512 SvREFCNT_inc_void_NN(sv);
514 else if (SvTYPE(sv) == SVt_PVAV) {
515 if (!AvREAL((const AV *)sv) && AvREIFY((const AV *)sv))
516 av_reify(MUTABLE_AV(sv));
518 SvREFCNT_inc_void_NN(sv);
520 else if (SvPADTMP(sv) && !IS_PADGV(sv))
524 SvREFCNT_inc_void_NN(sv);
527 sv_upgrade(rv, SVt_IV);
536 SV * const sv = POPs;
541 if (!sv || !SvROK(sv))
544 (void)sv_ref(TARG,SvRV(sv),TRUE);
556 stash = CopSTASH(PL_curcop);
558 SV * const ssv = POPs;
562 if (!ssv) goto curstash;
563 if (!SvGMAGICAL(ssv) && !SvAMAGIC(ssv) && SvROK(ssv))
564 Perl_croak(aTHX_ "Attempt to bless into a reference");
565 ptr = SvPV_const(ssv,len);
567 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
568 "Explicit blessing to '' (assuming package main)");
569 stash = gv_stashpvn(ptr, len, GV_ADD|SvUTF8(ssv));
572 (void)sv_bless(TOPs, stash);
582 const char * const elem = SvPV_const(sv, len);
583 GV * const gv = MUTABLE_GV(POPs);
588 /* elem will always be NUL terminated. */
589 const char * const second_letter = elem + 1;
592 if (len == 5 && strEQ(second_letter, "RRAY"))
593 tmpRef = MUTABLE_SV(GvAV(gv));
596 if (len == 4 && strEQ(second_letter, "ODE"))
597 tmpRef = MUTABLE_SV(GvCVu(gv));
600 if (len == 10 && strEQ(second_letter, "ILEHANDLE")) {
601 /* finally deprecated in 5.8.0 */
602 deprecate("*glob{FILEHANDLE}");
603 tmpRef = MUTABLE_SV(GvIOp(gv));
606 if (len == 6 && strEQ(second_letter, "ORMAT"))
607 tmpRef = MUTABLE_SV(GvFORM(gv));
610 if (len == 4 && strEQ(second_letter, "LOB"))
611 tmpRef = MUTABLE_SV(gv);
614 if (len == 4 && strEQ(second_letter, "ASH"))
615 tmpRef = MUTABLE_SV(GvHV(gv));
618 if (*second_letter == 'O' && !elem[2] && len == 2)
619 tmpRef = MUTABLE_SV(GvIOp(gv));
622 if (len == 4 && strEQ(second_letter, "AME"))
623 sv = newSVhek(GvNAME_HEK(gv));
626 if (len == 7 && strEQ(second_letter, "ACKAGE")) {
627 const HV * const stash = GvSTASH(gv);
628 const HEK * const hek = stash ? HvNAME_HEK(stash) : NULL;
629 sv = hek ? newSVhek(hek) : newSVpvs("__ANON__");
633 if (len == 6 && strEQ(second_letter, "CALAR"))
648 /* Pattern matching */
656 if (len == 0 || len > I32_MAX || !SvPOK(sv) || SvUTF8(sv) || SvVALID(sv)) {
657 /* Historically, study was skipped in these cases. */
661 /* Make study a no-op. It's no longer useful and its existence
662 complicates matters elsewhere. */
671 if (PL_op->op_flags & OPf_STACKED)
673 else if (PL_op->op_private & OPpTARGET_MY)
679 TARG = sv_newmortal();
680 if(PL_op->op_type == OP_TRANSR) {
682 const char * const pv = SvPV(sv,len);
683 SV * const newsv = newSVpvn_flags(pv, len, SVs_TEMP|SvUTF8(sv));
687 else PUSHi(do_trans(sv));
691 /* Lvalue operators. */
694 S_do_chomp(pTHX_ SV *retval, SV *sv, bool chomping)
700 PERL_ARGS_ASSERT_DO_CHOMP;
702 if (chomping && (RsSNARF(PL_rs) || RsRECORD(PL_rs)))
704 if (SvTYPE(sv) == SVt_PVAV) {
706 AV *const av = MUTABLE_AV(sv);
707 const I32 max = AvFILL(av);
709 for (i = 0; i <= max; i++) {
710 sv = MUTABLE_SV(av_fetch(av, i, FALSE));
711 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
712 do_chomp(retval, sv, chomping);
716 else if (SvTYPE(sv) == SVt_PVHV) {
717 HV* const hv = MUTABLE_HV(sv);
719 (void)hv_iterinit(hv);
720 while ((entry = hv_iternext(hv)))
721 do_chomp(retval, hv_iterval(hv,entry), chomping);
724 else if (SvREADONLY(sv)) {
726 /* SV is copy-on-write */
727 sv_force_normal_flags(sv, 0);
730 Perl_croak_no_modify(aTHX);
735 /* XXX, here sv is utf8-ized as a side-effect!
736 If encoding.pm is used properly, almost string-generating
737 operations, including literal strings, chr(), input data, etc.
738 should have been utf8-ized already, right?
740 sv_recode_to_utf8(sv, PL_encoding);
746 char *temp_buffer = NULL;
755 while (len && s[-1] == '\n') {
762 STRLEN rslen, rs_charlen;
763 const char *rsptr = SvPV_const(PL_rs, rslen);
765 rs_charlen = SvUTF8(PL_rs)
769 if (SvUTF8(PL_rs) != SvUTF8(sv)) {
770 /* Assumption is that rs is shorter than the scalar. */
772 /* RS is utf8, scalar is 8 bit. */
774 temp_buffer = (char*)bytes_from_utf8((U8*)rsptr,
777 /* Cannot downgrade, therefore cannot possibly match
779 assert (temp_buffer == rsptr);
785 else if (PL_encoding) {
786 /* RS is 8 bit, encoding.pm is used.
787 * Do not recode PL_rs as a side-effect. */
788 svrecode = newSVpvn(rsptr, rslen);
789 sv_recode_to_utf8(svrecode, PL_encoding);
790 rsptr = SvPV_const(svrecode, rslen);
791 rs_charlen = sv_len_utf8(svrecode);
794 /* RS is 8 bit, scalar is utf8. */
795 temp_buffer = (char*)bytes_to_utf8((U8*)rsptr, &rslen);
809 if (memNE(s, rsptr, rslen))
811 SvIVX(retval) += rs_charlen;
814 s = SvPV_force_nomg_nolen(sv);
822 SvREFCNT_dec(svrecode);
824 Safefree(temp_buffer);
826 if (len && !SvPOK(sv))
827 s = SvPV_force_nomg(sv, len);
830 char * const send = s + len;
831 char * const start = s;
833 while (s > start && UTF8_IS_CONTINUATION(*s))
835 if (is_utf8_string((U8*)s, send - s)) {
836 sv_setpvn(retval, s, send - s);
838 SvCUR_set(sv, s - start);
844 sv_setpvs(retval, "");
848 sv_setpvn(retval, s, 1);
855 sv_setpvs(retval, "");
863 const bool chomping = PL_op->op_type == OP_SCHOMP;
867 do_chomp(TARG, TOPs, chomping);
874 dVAR; dSP; dMARK; dTARGET; dORIGMARK;
875 const bool chomping = PL_op->op_type == OP_CHOMP;
880 do_chomp(TARG, *++MARK, chomping);
891 if (!PL_op->op_private) {
900 SV_CHECK_THINKFIRST_COW_DROP(sv);
902 switch (SvTYPE(sv)) {
906 av_undef(MUTABLE_AV(sv));
909 hv_undef(MUTABLE_HV(sv));
912 if (cv_const_sv((const CV *)sv))
913 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
914 "Constant subroutine %"SVf" undefined",
915 SVfARG(CvANON((const CV *)sv)
916 ? newSVpvs_flags("(anonymous)", SVs_TEMP)
917 : sv_2mortal(newSVhek(GvENAME_HEK(CvGV((const CV *)sv))))));
921 /* let user-undef'd sub keep its identity */
922 GV* const gv = CvGV((const CV *)sv);
923 cv_undef(MUTABLE_CV(sv));
924 CvGV_set(MUTABLE_CV(sv), gv);
929 SvSetMagicSV(sv, &PL_sv_undef);
932 else if (isGV_with_GP(sv)) {
936 /* undef *Pkg::meth_name ... */
938 = GvCVu((const GV *)sv) && (stash = GvSTASH((const GV *)sv))
939 && HvENAME_get(stash);
941 if((stash = GvHV((const GV *)sv))) {
942 if(HvENAME_get(stash))
943 SvREFCNT_inc_simple_void_NN(sv_2mortal((SV *)stash));
947 gp_free(MUTABLE_GV(sv));
949 GvGP_set(sv, gp_ref(gp));
951 GvLINE(sv) = CopLINE(PL_curcop);
952 GvEGV(sv) = MUTABLE_GV(sv);
956 mro_package_moved(NULL, stash, (const GV *)sv, 0);
958 /* undef *Foo::ISA */
959 if( strEQ(GvNAME((const GV *)sv), "ISA")
960 && (stash = GvSTASH((const GV *)sv))
961 && (method_changed || HvENAME(stash)) )
962 mro_isa_changed_in(stash);
963 else if(method_changed)
964 mro_method_changed_in(
965 GvSTASH((const GV *)sv)
972 if (SvTYPE(sv) >= SVt_PV && SvPVX_const(sv) && SvLEN(sv)) {
988 PL_op->op_type == OP_POSTINC || PL_op->op_type == OP_I_POSTINC;
989 if (SvTYPE(TOPs) >= SVt_PVAV || (isGV_with_GP(TOPs) && !SvFAKE(TOPs)))
990 Perl_croak_no_modify(aTHX);
992 TARG = sv_newmortal();
993 sv_setsv(TARG, TOPs);
994 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
995 && SvIVX(TOPs) != (inc ? IV_MAX : IV_MIN))
997 SvIV_set(TOPs, SvIVX(TOPs) + (inc ? 1 : -1));
998 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
1002 else sv_dec_nomg(TOPs);
1004 /* special case for undef: see thread at 2003-03/msg00536.html in archive */
1005 if (inc && !SvOK(TARG))
1011 /* Ordinary operators. */
1015 dVAR; dSP; dATARGET; SV *svl, *svr;
1016 #ifdef PERL_PRESERVE_IVUV
1019 tryAMAGICbin_MG(pow_amg, AMGf_assign|AMGf_numeric);
1022 #ifdef PERL_PRESERVE_IVUV
1023 /* For integer to integer power, we do the calculation by hand wherever
1024 we're sure it is safe; otherwise we call pow() and try to convert to
1025 integer afterwards. */
1026 if (SvIV_please_nomg(svr) && SvIV_please_nomg(svl)) {
1034 const IV iv = SvIVX(svr);
1038 goto float_it; /* Can't do negative powers this way. */
1042 baseuok = SvUOK(svl);
1044 baseuv = SvUVX(svl);
1046 const IV iv = SvIVX(svl);
1049 baseuok = TRUE; /* effectively it's a UV now */
1051 baseuv = -iv; /* abs, baseuok == false records sign */
1054 /* now we have integer ** positive integer. */
1057 /* foo & (foo - 1) is zero only for a power of 2. */
1058 if (!(baseuv & (baseuv - 1))) {
1059 /* We are raising power-of-2 to a positive integer.
1060 The logic here will work for any base (even non-integer
1061 bases) but it can be less accurate than
1062 pow (base,power) or exp (power * log (base)) when the
1063 intermediate values start to spill out of the mantissa.
1064 With powers of 2 we know this can't happen.
1065 And powers of 2 are the favourite thing for perl
1066 programmers to notice ** not doing what they mean. */
1068 NV base = baseuok ? baseuv : -(NV)baseuv;
1073 while (power >>= 1) {
1081 SvIV_please_nomg(svr);
1084 register unsigned int highbit = 8 * sizeof(UV);
1085 register unsigned int diff = 8 * sizeof(UV);
1086 while (diff >>= 1) {
1088 if (baseuv >> highbit) {
1092 /* we now have baseuv < 2 ** highbit */
1093 if (power * highbit <= 8 * sizeof(UV)) {
1094 /* result will definitely fit in UV, so use UV math
1095 on same algorithm as above */
1096 register UV result = 1;
1097 register UV base = baseuv;
1098 const bool odd_power = cBOOL(power & 1);
1102 while (power >>= 1) {
1109 if (baseuok || !odd_power)
1110 /* answer is positive */
1112 else if (result <= (UV)IV_MAX)
1113 /* answer negative, fits in IV */
1114 SETi( -(IV)result );
1115 else if (result == (UV)IV_MIN)
1116 /* 2's complement assumption: special case IV_MIN */
1119 /* answer negative, doesn't fit */
1120 SETn( -(NV)result );
1128 NV right = SvNV_nomg(svr);
1129 NV left = SvNV_nomg(svl);
1132 #if defined(USE_LONG_DOUBLE) && defined(HAS_AIX_POWL_NEG_BASE_BUG)
1134 We are building perl with long double support and are on an AIX OS
1135 afflicted with a powl() function that wrongly returns NaNQ for any
1136 negative base. This was reported to IBM as PMR #23047-379 on
1137 03/06/2006. The problem exists in at least the following versions
1138 of AIX and the libm fileset, and no doubt others as well:
1140 AIX 4.3.3-ML10 bos.adt.libm 4.3.3.50
1141 AIX 5.1.0-ML04 bos.adt.libm 5.1.0.29
1142 AIX 5.2.0 bos.adt.libm 5.2.0.85
1144 So, until IBM fixes powl(), we provide the following workaround to
1145 handle the problem ourselves. Our logic is as follows: for
1146 negative bases (left), we use fmod(right, 2) to check if the
1147 exponent is an odd or even integer:
1149 - if odd, powl(left, right) == -powl(-left, right)
1150 - if even, powl(left, right) == powl(-left, right)
1152 If the exponent is not an integer, the result is rightly NaNQ, so
1153 we just return that (as NV_NAN).
1157 NV mod2 = Perl_fmod( right, 2.0 );
1158 if (mod2 == 1.0 || mod2 == -1.0) { /* odd integer */
1159 SETn( -Perl_pow( -left, right) );
1160 } else if (mod2 == 0.0) { /* even integer */
1161 SETn( Perl_pow( -left, right) );
1162 } else { /* fractional power */
1166 SETn( Perl_pow( left, right) );
1169 SETn( Perl_pow( left, right) );
1170 #endif /* HAS_AIX_POWL_NEG_BASE_BUG */
1172 #ifdef PERL_PRESERVE_IVUV
1174 SvIV_please_nomg(svr);
1182 dVAR; dSP; dATARGET; SV *svl, *svr;
1183 tryAMAGICbin_MG(mult_amg, AMGf_assign|AMGf_numeric);
1186 #ifdef PERL_PRESERVE_IVUV
1187 if (SvIV_please_nomg(svr)) {
1188 /* Unless the left argument is integer in range we are going to have to
1189 use NV maths. Hence only attempt to coerce the right argument if
1190 we know the left is integer. */
1191 /* Left operand is defined, so is it IV? */
1192 if (SvIV_please_nomg(svl)) {
1193 bool auvok = SvUOK(svl);
1194 bool buvok = SvUOK(svr);
1195 const UV topmask = (~ (UV)0) << (4 * sizeof (UV));
1196 const UV botmask = ~((~ (UV)0) << (4 * sizeof (UV)));
1205 const IV aiv = SvIVX(svl);
1208 auvok = TRUE; /* effectively it's a UV now */
1210 alow = -aiv; /* abs, auvok == false records sign */
1216 const IV biv = SvIVX(svr);
1219 buvok = TRUE; /* effectively it's a UV now */
1221 blow = -biv; /* abs, buvok == false records sign */
1225 /* If this does sign extension on unsigned it's time for plan B */
1226 ahigh = alow >> (4 * sizeof (UV));
1228 bhigh = blow >> (4 * sizeof (UV));
1230 if (ahigh && bhigh) {
1232 /* eg 32 bit is at least 0x10000 * 0x10000 == 0x100000000
1233 which is overflow. Drop to NVs below. */
1234 } else if (!ahigh && !bhigh) {
1235 /* eg 32 bit is at most 0xFFFF * 0xFFFF == 0xFFFE0001
1236 so the unsigned multiply cannot overflow. */
1237 const UV product = alow * blow;
1238 if (auvok == buvok) {
1239 /* -ve * -ve or +ve * +ve gives a +ve result. */
1243 } else if (product <= (UV)IV_MIN) {
1244 /* 2s complement assumption that (UV)-IV_MIN is correct. */
1245 /* -ve result, which could overflow an IV */
1247 SETi( -(IV)product );
1249 } /* else drop to NVs below. */
1251 /* One operand is large, 1 small */
1254 /* swap the operands */
1256 bhigh = blow; /* bhigh now the temp var for the swap */
1260 /* now, ((ahigh * blow) << half_UV_len) + (alow * blow)
1261 multiplies can't overflow. shift can, add can, -ve can. */
1262 product_middle = ahigh * blow;
1263 if (!(product_middle & topmask)) {
1264 /* OK, (ahigh * blow) won't lose bits when we shift it. */
1266 product_middle <<= (4 * sizeof (UV));
1267 product_low = alow * blow;
1269 /* as for pp_add, UV + something mustn't get smaller.
1270 IIRC ANSI mandates this wrapping *behaviour* for
1271 unsigned whatever the actual representation*/
1272 product_low += product_middle;
1273 if (product_low >= product_middle) {
1274 /* didn't overflow */
1275 if (auvok == buvok) {
1276 /* -ve * -ve or +ve * +ve gives a +ve result. */
1278 SETu( product_low );
1280 } else if (product_low <= (UV)IV_MIN) {
1281 /* 2s complement assumption again */
1282 /* -ve result, which could overflow an IV */
1284 SETi( -(IV)product_low );
1286 } /* else drop to NVs below. */
1288 } /* product_middle too large */
1289 } /* ahigh && bhigh */
1294 NV right = SvNV_nomg(svr);
1295 NV left = SvNV_nomg(svl);
1297 SETn( left * right );
1304 dVAR; dSP; dATARGET; SV *svl, *svr;
1305 tryAMAGICbin_MG(div_amg, AMGf_assign|AMGf_numeric);
1308 /* Only try to do UV divide first
1309 if ((SLOPPYDIVIDE is true) or
1310 (PERL_PRESERVE_IVUV is true and one or both SV is a UV too large
1312 The assumption is that it is better to use floating point divide
1313 whenever possible, only doing integer divide first if we can't be sure.
1314 If NV_PRESERVES_UV is true then we know at compile time that no UV
1315 can be too large to preserve, so don't need to compile the code to
1316 test the size of UVs. */
1319 # define PERL_TRY_UV_DIVIDE
1320 /* ensure that 20./5. == 4. */
1322 # ifdef PERL_PRESERVE_IVUV
1323 # ifndef NV_PRESERVES_UV
1324 # define PERL_TRY_UV_DIVIDE
1329 #ifdef PERL_TRY_UV_DIVIDE
1330 if (SvIV_please_nomg(svr) && SvIV_please_nomg(svl)) {
1331 bool left_non_neg = SvUOK(svl);
1332 bool right_non_neg = SvUOK(svr);
1336 if (right_non_neg) {
1340 const IV biv = SvIVX(svr);
1343 right_non_neg = TRUE; /* effectively it's a UV now */
1349 /* historically undef()/0 gives a "Use of uninitialized value"
1350 warning before dieing, hence this test goes here.
1351 If it were immediately before the second SvIV_please, then
1352 DIE() would be invoked before left was even inspected, so
1353 no inspection would give no warning. */
1355 DIE(aTHX_ "Illegal division by zero");
1361 const IV aiv = SvIVX(svl);
1364 left_non_neg = TRUE; /* effectively it's a UV now */
1373 /* For sloppy divide we always attempt integer division. */
1375 /* Otherwise we only attempt it if either or both operands
1376 would not be preserved by an NV. If both fit in NVs
1377 we fall through to the NV divide code below. However,
1378 as left >= right to ensure integer result here, we know that
1379 we can skip the test on the right operand - right big
1380 enough not to be preserved can't get here unless left is
1383 && (left > ((UV)1 << NV_PRESERVES_UV_BITS))
1386 /* Integer division can't overflow, but it can be imprecise. */
1387 const UV result = left / right;
1388 if (result * right == left) {
1389 SP--; /* result is valid */
1390 if (left_non_neg == right_non_neg) {
1391 /* signs identical, result is positive. */
1395 /* 2s complement assumption */
1396 if (result <= (UV)IV_MIN)
1397 SETi( -(IV)result );
1399 /* It's exact but too negative for IV. */
1400 SETn( -(NV)result );
1403 } /* tried integer divide but it was not an integer result */
1404 } /* else (PERL_ABS(result) < 1.0) or (both UVs in range for NV) */
1405 } /* one operand wasn't SvIOK */
1406 #endif /* PERL_TRY_UV_DIVIDE */
1408 NV right = SvNV_nomg(svr);
1409 NV left = SvNV_nomg(svl);
1410 (void)POPs;(void)POPs;
1411 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
1412 if (! Perl_isnan(right) && right == 0.0)
1416 DIE(aTHX_ "Illegal division by zero");
1417 PUSHn( left / right );
1424 dVAR; dSP; dATARGET;
1425 tryAMAGICbin_MG(modulo_amg, AMGf_assign|AMGf_numeric);
1429 bool left_neg = FALSE;
1430 bool right_neg = FALSE;
1431 bool use_double = FALSE;
1432 bool dright_valid = FALSE;
1435 SV * const svr = TOPs;
1436 SV * const svl = TOPm1s;
1437 if (SvIV_please_nomg(svr)) {
1438 right_neg = !SvUOK(svr);
1442 const IV biv = SvIVX(svr);
1445 right_neg = FALSE; /* effectively it's a UV now */
1452 dright = SvNV_nomg(svr);
1453 right_neg = dright < 0;
1456 if (dright < UV_MAX_P1) {
1457 right = U_V(dright);
1458 dright_valid = TRUE; /* In case we need to use double below. */
1464 /* At this point use_double is only true if right is out of range for
1465 a UV. In range NV has been rounded down to nearest UV and
1466 use_double false. */
1467 if (!use_double && SvIV_please_nomg(svl)) {
1468 left_neg = !SvUOK(svl);
1472 const IV aiv = SvIVX(svl);
1475 left_neg = FALSE; /* effectively it's a UV now */
1482 dleft = SvNV_nomg(svl);
1483 left_neg = dleft < 0;
1487 /* This should be exactly the 5.6 behaviour - if left and right are
1488 both in range for UV then use U_V() rather than floor. */
1490 if (dleft < UV_MAX_P1) {
1491 /* right was in range, so is dleft, so use UVs not double.
1495 /* left is out of range for UV, right was in range, so promote
1496 right (back) to double. */
1498 /* The +0.5 is used in 5.6 even though it is not strictly
1499 consistent with the implicit +0 floor in the U_V()
1500 inside the #if 1. */
1501 dleft = Perl_floor(dleft + 0.5);
1504 dright = Perl_floor(dright + 0.5);
1515 DIE(aTHX_ "Illegal modulus zero");
1517 dans = Perl_fmod(dleft, dright);
1518 if ((left_neg != right_neg) && dans)
1519 dans = dright - dans;
1522 sv_setnv(TARG, dans);
1528 DIE(aTHX_ "Illegal modulus zero");
1531 if ((left_neg != right_neg) && ans)
1534 /* XXX may warn: unary minus operator applied to unsigned type */
1535 /* could change -foo to be (~foo)+1 instead */
1536 if (ans <= ~((UV)IV_MAX)+1)
1537 sv_setiv(TARG, ~ans+1);
1539 sv_setnv(TARG, -(NV)ans);
1542 sv_setuv(TARG, ans);
1551 dVAR; dSP; dATARGET;
1555 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1556 /* TODO: think of some way of doing list-repeat overloading ??? */
1561 tryAMAGICbin_MG(repeat_amg, AMGf_assign);
1567 const UV uv = SvUV_nomg(sv);
1569 count = IV_MAX; /* The best we can do? */
1573 const IV iv = SvIV_nomg(sv);
1580 else if (SvNOKp(sv)) {
1581 const NV nv = SvNV_nomg(sv);
1588 count = SvIV_nomg(sv);
1590 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1592 static const char oom_list_extend[] = "Out of memory during list extend";
1593 const I32 items = SP - MARK;
1594 const I32 max = items * count;
1596 MEM_WRAP_CHECK_1(max, SV*, oom_list_extend);
1597 /* Did the max computation overflow? */
1598 if (items > 0 && max > 0 && (max < items || max < count))
1599 Perl_croak(aTHX_ oom_list_extend);
1604 /* This code was intended to fix 20010809.028:
1607 for (($x =~ /./g) x 2) {
1608 print chop; # "abcdabcd" expected as output.
1611 * but that change (#11635) broke this code:
1613 $x = [("foo")x2]; # only one "foo" ended up in the anonlist.
1615 * I can't think of a better fix that doesn't introduce
1616 * an efficiency hit by copying the SVs. The stack isn't
1617 * refcounted, and mortalisation obviously doesn't
1618 * Do The Right Thing when the stack has more than
1619 * one pointer to the same mortal value.
1623 *SP = sv_2mortal(newSVsv(*SP));
1633 repeatcpy((char*)(MARK + items), (char*)MARK,
1634 items * sizeof(const SV *), count - 1);
1637 else if (count <= 0)
1640 else { /* Note: mark already snarfed by pp_list */
1641 SV * const tmpstr = POPs;
1644 static const char oom_string_extend[] =
1645 "Out of memory during string extend";
1648 sv_setsv_nomg(TARG, tmpstr);
1649 SvPV_force_nomg(TARG, len);
1650 isutf = DO_UTF8(TARG);
1655 const STRLEN max = (UV)count * len;
1656 if (len > MEM_SIZE_MAX / count)
1657 Perl_croak(aTHX_ oom_string_extend);
1658 MEM_WRAP_CHECK_1(max, char, oom_string_extend);
1659 SvGROW(TARG, max + 1);
1660 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
1661 SvCUR_set(TARG, SvCUR(TARG) * count);
1663 *SvEND(TARG) = '\0';
1666 (void)SvPOK_only_UTF8(TARG);
1668 (void)SvPOK_only(TARG);
1670 if (PL_op->op_private & OPpREPEAT_DOLIST) {
1671 /* The parser saw this as a list repeat, and there
1672 are probably several items on the stack. But we're
1673 in scalar context, and there's no pp_list to save us
1674 now. So drop the rest of the items -- robin@kitsite.com
1686 dVAR; dSP; dATARGET; bool useleft; SV *svl, *svr;
1687 tryAMAGICbin_MG(subtr_amg, AMGf_assign|AMGf_numeric);
1690 useleft = USE_LEFT(svl);
1691 #ifdef PERL_PRESERVE_IVUV
1692 /* See comments in pp_add (in pp_hot.c) about Overflow, and how
1693 "bad things" happen if you rely on signed integers wrapping. */
1694 if (SvIV_please_nomg(svr)) {
1695 /* Unless the left argument is integer in range we are going to have to
1696 use NV maths. Hence only attempt to coerce the right argument if
1697 we know the left is integer. */
1698 register UV auv = 0;
1704 a_valid = auvok = 1;
1705 /* left operand is undef, treat as zero. */
1707 /* Left operand is defined, so is it IV? */
1708 if (SvIV_please_nomg(svl)) {
1709 if ((auvok = SvUOK(svl)))
1712 register const IV aiv = SvIVX(svl);
1715 auvok = 1; /* Now acting as a sign flag. */
1716 } else { /* 2s complement assumption for IV_MIN */
1724 bool result_good = 0;
1727 bool buvok = SvUOK(svr);
1732 register const IV biv = SvIVX(svr);
1739 /* ?uvok if value is >= 0. basically, flagged as UV if it's +ve,
1740 else "IV" now, independent of how it came in.
1741 if a, b represents positive, A, B negative, a maps to -A etc
1746 all UV maths. negate result if A negative.
1747 subtract if signs same, add if signs differ. */
1749 if (auvok ^ buvok) {
1758 /* Must get smaller */
1763 if (result <= buv) {
1764 /* result really should be -(auv-buv). as its negation
1765 of true value, need to swap our result flag */
1777 if (result <= (UV)IV_MIN)
1778 SETi( -(IV)result );
1780 /* result valid, but out of range for IV. */
1781 SETn( -(NV)result );
1785 } /* Overflow, drop through to NVs. */
1790 NV value = SvNV_nomg(svr);
1794 /* left operand is undef, treat as zero - value */
1798 SETn( SvNV_nomg(svl) - value );
1805 dVAR; dSP; dATARGET; SV *svl, *svr;
1806 tryAMAGICbin_MG(lshift_amg, AMGf_assign|AMGf_numeric);
1810 const IV shift = SvIV_nomg(svr);
1811 if (PL_op->op_private & HINT_INTEGER) {
1812 const IV i = SvIV_nomg(svl);
1816 const UV u = SvUV_nomg(svl);
1825 dVAR; dSP; dATARGET; SV *svl, *svr;
1826 tryAMAGICbin_MG(rshift_amg, AMGf_assign|AMGf_numeric);
1830 const IV shift = SvIV_nomg(svr);
1831 if (PL_op->op_private & HINT_INTEGER) {
1832 const IV i = SvIV_nomg(svl);
1836 const UV u = SvUV_nomg(svl);
1848 tryAMAGICbin_MG(lt_amg, AMGf_set|AMGf_numeric);
1852 (SvIOK_notUV(left) && SvIOK_notUV(right))
1853 ? (SvIVX(left) < SvIVX(right))
1854 : (do_ncmp(left, right) == -1)
1864 tryAMAGICbin_MG(gt_amg, AMGf_set|AMGf_numeric);
1868 (SvIOK_notUV(left) && SvIOK_notUV(right))
1869 ? (SvIVX(left) > SvIVX(right))
1870 : (do_ncmp(left, right) == 1)
1880 tryAMAGICbin_MG(le_amg, AMGf_set|AMGf_numeric);
1884 (SvIOK_notUV(left) && SvIOK_notUV(right))
1885 ? (SvIVX(left) <= SvIVX(right))
1886 : (do_ncmp(left, right) <= 0)
1896 tryAMAGICbin_MG(ge_amg, AMGf_set|AMGf_numeric);
1900 (SvIOK_notUV(left) && SvIOK_notUV(right))
1901 ? (SvIVX(left) >= SvIVX(right))
1902 : ( (do_ncmp(left, right) & 2) == 0)
1912 tryAMAGICbin_MG(ne_amg, AMGf_set|AMGf_numeric);
1916 (SvIOK_notUV(left) && SvIOK_notUV(right))
1917 ? (SvIVX(left) != SvIVX(right))
1918 : (do_ncmp(left, right) != 0)
1923 /* compare left and right SVs. Returns:
1927 * 2: left or right was a NaN
1930 Perl_do_ncmp(pTHX_ SV* const left, SV * const right)
1934 PERL_ARGS_ASSERT_DO_NCMP;
1935 #ifdef PERL_PRESERVE_IVUV
1936 /* Fortunately it seems NaN isn't IOK */
1937 if (SvIV_please_nomg(right) && SvIV_please_nomg(left)) {
1939 const IV leftiv = SvIVX(left);
1940 if (!SvUOK(right)) {
1941 /* ## IV <=> IV ## */
1942 const IV rightiv = SvIVX(right);
1943 return (leftiv > rightiv) - (leftiv < rightiv);
1945 /* ## IV <=> UV ## */
1947 /* As (b) is a UV, it's >=0, so it must be < */
1950 const UV rightuv = SvUVX(right);
1951 return ((UV)leftiv > rightuv) - ((UV)leftiv < rightuv);
1956 /* ## UV <=> UV ## */
1957 const UV leftuv = SvUVX(left);
1958 const UV rightuv = SvUVX(right);
1959 return (leftuv > rightuv) - (leftuv < rightuv);
1961 /* ## UV <=> IV ## */
1963 const IV rightiv = SvIVX(right);
1965 /* As (a) is a UV, it's >=0, so it cannot be < */
1968 const UV leftuv = SvUVX(left);
1969 return (leftuv > (UV)rightiv) - (leftuv < (UV)rightiv);
1972 assert(0); /* NOTREACHED */
1976 NV const rnv = SvNV_nomg(right);
1977 NV const lnv = SvNV_nomg(left);
1979 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
1980 if (Perl_isnan(lnv) || Perl_isnan(rnv)) {
1983 return (lnv > rnv) - (lnv < rnv);
2002 tryAMAGICbin_MG(ncmp_amg, AMGf_numeric);
2005 value = do_ncmp(left, right);
2020 int amg_type = sle_amg;
2024 switch (PL_op->op_type) {
2043 tryAMAGICbin_MG(amg_type, AMGf_set);
2046 const int cmp = (IN_LOCALE_RUNTIME
2047 ? sv_cmp_locale_flags(left, right, 0)
2048 : sv_cmp_flags(left, right, 0));
2049 SETs(boolSV(cmp * multiplier < rhs));
2057 tryAMAGICbin_MG(seq_amg, AMGf_set);
2060 SETs(boolSV(sv_eq_flags(left, right, 0)));
2068 tryAMAGICbin_MG(sne_amg, AMGf_set);
2071 SETs(boolSV(!sv_eq_flags(left, right, 0)));
2079 tryAMAGICbin_MG(scmp_amg, 0);
2082 const int cmp = (IN_LOCALE_RUNTIME
2083 ? sv_cmp_locale_flags(left, right, 0)
2084 : sv_cmp_flags(left, right, 0));
2092 dVAR; dSP; dATARGET;
2093 tryAMAGICbin_MG(band_amg, AMGf_assign);
2096 if (SvNIOKp(left) || SvNIOKp(right)) {
2097 const bool left_ro_nonnum = !SvNIOKp(left) && SvREADONLY(left);
2098 const bool right_ro_nonnum = !SvNIOKp(right) && SvREADONLY(right);
2099 if (PL_op->op_private & HINT_INTEGER) {
2100 const IV i = SvIV_nomg(left) & SvIV_nomg(right);
2104 const UV u = SvUV_nomg(left) & SvUV_nomg(right);
2107 if (left_ro_nonnum && left != TARG) SvNIOK_off(left);
2108 if (right_ro_nonnum) SvNIOK_off(right);
2111 do_vop(PL_op->op_type, TARG, left, right);
2120 dVAR; dSP; dATARGET;
2121 const int op_type = PL_op->op_type;
2123 tryAMAGICbin_MG((op_type == OP_BIT_OR ? bor_amg : bxor_amg), AMGf_assign);
2126 if (SvNIOKp(left) || SvNIOKp(right)) {
2127 const bool left_ro_nonnum = !SvNIOKp(left) && SvREADONLY(left);
2128 const bool right_ro_nonnum = !SvNIOKp(right) && SvREADONLY(right);
2129 if (PL_op->op_private & HINT_INTEGER) {
2130 const IV l = (USE_LEFT(left) ? SvIV_nomg(left) : 0);
2131 const IV r = SvIV_nomg(right);
2132 const IV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2136 const UV l = (USE_LEFT(left) ? SvUV_nomg(left) : 0);
2137 const UV r = SvUV_nomg(right);
2138 const UV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2141 if (left_ro_nonnum && left != TARG) SvNIOK_off(left);
2142 if (right_ro_nonnum) SvNIOK_off(right);
2145 do_vop(op_type, TARG, left, right);
2152 PERL_STATIC_INLINE bool
2153 S_negate_string(pTHX)
2158 SV * const sv = TOPs;
2159 if (!SvPOKp(sv) || SvNIOK(sv) || (!SvPOK(sv) && SvNIOKp(sv)))
2161 s = SvPV_nomg_const(sv, len);
2162 if (isIDFIRST(*s)) {
2163 sv_setpvs(TARG, "-");
2166 else if (*s == '+' || (*s == '-' && !looks_like_number(sv))) {
2167 sv_setsv_nomg(TARG, sv);
2168 *SvPV_force_nomg(TARG, len) = *s == '-' ? '+' : '-';
2178 tryAMAGICun_MG(neg_amg, AMGf_numeric);
2179 if (S_negate_string(aTHX)) return NORMAL;
2181 SV * const sv = TOPs;
2183 if (SvIOK(sv) || (SvGMAGICAL(sv) && SvIOKp(sv))) {
2184 /* It's publicly an integer */
2187 if (SvIVX(sv) == IV_MIN) {
2188 /* 2s complement assumption. */
2189 SETi(SvIVX(sv)); /* special case: -((UV)IV_MAX+1) == IV_MIN */
2192 else if (SvUVX(sv) <= IV_MAX) {
2197 else if (SvIVX(sv) != IV_MIN) {
2201 #ifdef PERL_PRESERVE_IVUV
2208 if (SvNIOKp(sv) && (SvNIOK(sv) || !SvPOK(sv)))
2209 SETn(-SvNV_nomg(sv));
2210 else if (SvPOKp(sv) && SvIV_please_nomg(sv))
2211 goto oops_its_an_int;
2213 SETn(-SvNV_nomg(sv));
2221 tryAMAGICun_MG(not_amg, AMGf_set);
2222 *PL_stack_sp = boolSV(!SvTRUE_nomg(*PL_stack_sp));
2229 tryAMAGICun_MG(compl_amg, AMGf_numeric);
2233 if (PL_op->op_private & HINT_INTEGER) {
2234 const IV i = ~SvIV_nomg(sv);
2238 const UV u = ~SvUV_nomg(sv);
2247 (void)SvPV_nomg_const(sv,len); /* force check for uninit var */
2248 sv_setsv_nomg(TARG, sv);
2249 tmps = (U8*)SvPV_force_nomg(TARG, len);
2252 /* Calculate exact length, let's not estimate. */
2257 U8 * const send = tmps + len;
2258 U8 * const origtmps = tmps;
2259 const UV utf8flags = UTF8_ALLOW_ANYUV;
2261 while (tmps < send) {
2262 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2264 targlen += UNISKIP(~c);
2270 /* Now rewind strings and write them. */
2277 Newx(result, targlen + 1, U8);
2279 while (tmps < send) {
2280 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2282 p = uvchr_to_utf8_flags(p, ~c, UNICODE_ALLOW_ANY);
2285 sv_usepvn_flags(TARG, (char*)result, targlen,
2286 SV_HAS_TRAILING_NUL);
2293 Newx(result, nchar + 1, U8);
2295 while (tmps < send) {
2296 const U8 c = (U8)utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2301 sv_usepvn_flags(TARG, (char*)result, nchar, SV_HAS_TRAILING_NUL);
2309 register long *tmpl;
2310 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
2313 for ( ; anum >= (I32)sizeof(long); anum -= (I32)sizeof(long), tmpl++)
2318 for ( ; anum > 0; anum--, tmps++)
2326 /* integer versions of some of the above */
2330 dVAR; dSP; dATARGET;
2331 tryAMAGICbin_MG(mult_amg, AMGf_assign);
2334 SETi( left * right );
2342 dVAR; dSP; dATARGET;
2343 tryAMAGICbin_MG(div_amg, AMGf_assign);
2346 IV value = SvIV_nomg(right);
2348 DIE(aTHX_ "Illegal division by zero");
2349 num = SvIV_nomg(left);
2351 /* avoid FPE_INTOVF on some platforms when num is IV_MIN */
2355 value = num / value;
2361 #if defined(__GLIBC__) && IVSIZE == 8
2368 /* This is the vanilla old i_modulo. */
2369 dVAR; dSP; dATARGET;
2370 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2374 DIE(aTHX_ "Illegal modulus zero");
2375 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2379 SETi( left % right );
2384 #if defined(__GLIBC__) && IVSIZE == 8
2389 /* This is the i_modulo with the workaround for the _moddi3 bug
2390 * in (at least) glibc 2.2.5 (the PERL_ABS() the workaround).
2391 * See below for pp_i_modulo. */
2392 dVAR; dSP; dATARGET;
2393 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2397 DIE(aTHX_ "Illegal modulus zero");
2398 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2402 SETi( left % PERL_ABS(right) );
2409 dVAR; dSP; dATARGET;
2410 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2414 DIE(aTHX_ "Illegal modulus zero");
2415 /* The assumption is to use hereafter the old vanilla version... */
2417 PL_ppaddr[OP_I_MODULO] =
2419 /* .. but if we have glibc, we might have a buggy _moddi3
2420 * (at least glicb 2.2.5 is known to have this bug), in other
2421 * words our integer modulus with negative quad as the second
2422 * argument might be broken. Test for this and re-patch the
2423 * opcode dispatch table if that is the case, remembering to
2424 * also apply the workaround so that this first round works
2425 * right, too. See [perl #9402] for more information. */
2429 /* Cannot do this check with inlined IV constants since
2430 * that seems to work correctly even with the buggy glibc. */
2432 /* Yikes, we have the bug.
2433 * Patch in the workaround version. */
2435 PL_ppaddr[OP_I_MODULO] =
2436 &Perl_pp_i_modulo_1;
2437 /* Make certain we work right this time, too. */
2438 right = PERL_ABS(right);
2441 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2445 SETi( left % right );
2453 dVAR; dSP; dATARGET;
2454 tryAMAGICbin_MG(add_amg, AMGf_assign);
2456 dPOPTOPiirl_ul_nomg;
2457 SETi( left + right );
2464 dVAR; dSP; dATARGET;
2465 tryAMAGICbin_MG(subtr_amg, AMGf_assign);
2467 dPOPTOPiirl_ul_nomg;
2468 SETi( left - right );
2476 tryAMAGICbin_MG(lt_amg, AMGf_set);
2479 SETs(boolSV(left < right));
2487 tryAMAGICbin_MG(gt_amg, AMGf_set);
2490 SETs(boolSV(left > right));
2498 tryAMAGICbin_MG(le_amg, AMGf_set);
2501 SETs(boolSV(left <= right));
2509 tryAMAGICbin_MG(ge_amg, AMGf_set);
2512 SETs(boolSV(left >= right));
2520 tryAMAGICbin_MG(eq_amg, AMGf_set);
2523 SETs(boolSV(left == right));
2531 tryAMAGICbin_MG(ne_amg, AMGf_set);
2534 SETs(boolSV(left != right));
2542 tryAMAGICbin_MG(ncmp_amg, 0);
2549 else if (left < right)
2561 tryAMAGICun_MG(neg_amg, 0);
2562 if (S_negate_string(aTHX)) return NORMAL;
2564 SV * const sv = TOPs;
2565 IV const i = SvIV_nomg(sv);
2571 /* High falutin' math. */
2576 tryAMAGICbin_MG(atan2_amg, 0);
2579 SETn(Perl_atan2(left, right));
2587 int amg_type = sin_amg;
2588 const char *neg_report = NULL;
2589 NV (*func)(NV) = Perl_sin;
2590 const int op_type = PL_op->op_type;
2607 amg_type = sqrt_amg;
2609 neg_report = "sqrt";
2614 tryAMAGICun_MG(amg_type, 0);
2616 SV * const arg = POPs;
2617 const NV value = SvNV_nomg(arg);
2619 if (op_type == OP_LOG ? (value <= 0.0) : (value < 0.0)) {
2620 SET_NUMERIC_STANDARD();
2621 /* diag_listed_as: Can't take log of %g */
2622 DIE(aTHX_ "Can't take %s of %"NVgf, neg_report, value);
2625 XPUSHn(func(value));
2630 /* Support Configure command-line overrides for rand() functions.
2631 After 5.005, perhaps we should replace this by Configure support
2632 for drand48(), random(), or rand(). For 5.005, though, maintain
2633 compatibility by calling rand() but allow the user to override it.
2634 See INSTALL for details. --Andy Dougherty 15 July 1998
2636 /* Now it's after 5.005, and Configure supports drand48() and random(),
2637 in addition to rand(). So the overrides should not be needed any more.
2638 --Jarkko Hietaniemi 27 September 1998
2641 #ifndef HAS_DRAND48_PROTO
2642 extern double drand48 (void);
2652 value = 1.0; (void)POPs;
2658 if (!PL_srand_called) {
2659 (void)seedDrand01((Rand_seed_t)seed());
2660 PL_srand_called = TRUE;
2672 if (MAXARG >= 1 && (TOPs || POPs)) {
2679 pv = SvPV(top, len);
2680 flags = grok_number(pv, len, &anum);
2682 if (!(flags & IS_NUMBER_IN_UV)) {
2683 Perl_ck_warner_d(aTHX_ packWARN(WARN_OVERFLOW),
2684 "Integer overflow in srand");
2692 (void)seedDrand01((Rand_seed_t)anum);
2693 PL_srand_called = TRUE;
2697 /* Historically srand always returned true. We can avoid breaking
2699 sv_setpvs(TARG, "0 but true");
2708 tryAMAGICun_MG(int_amg, AMGf_numeric);
2710 SV * const sv = TOPs;
2711 const IV iv = SvIV_nomg(sv);
2712 /* XXX it's arguable that compiler casting to IV might be subtly
2713 different from modf (for numbers inside (IV_MIN,UV_MAX)) in which
2714 else preferring IV has introduced a subtle behaviour change bug. OTOH
2715 relying on floating point to be accurate is a bug. */
2720 else if (SvIOK(sv)) {
2722 SETu(SvUV_nomg(sv));
2727 const NV value = SvNV_nomg(sv);
2729 if (value < (NV)UV_MAX + 0.5) {
2732 SETn(Perl_floor(value));
2736 if (value > (NV)IV_MIN - 0.5) {
2739 SETn(Perl_ceil(value));
2750 tryAMAGICun_MG(abs_amg, AMGf_numeric);
2752 SV * const sv = TOPs;
2753 /* This will cache the NV value if string isn't actually integer */
2754 const IV iv = SvIV_nomg(sv);
2759 else if (SvIOK(sv)) {
2760 /* IVX is precise */
2762 SETu(SvUV_nomg(sv)); /* force it to be numeric only */
2770 /* 2s complement assumption. Also, not really needed as
2771 IV_MIN and -IV_MIN should both be %100...00 and NV-able */
2777 const NV value = SvNV_nomg(sv);
2791 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES;
2795 SV* const sv = POPs;
2797 tmps = (SvPV_const(sv, len));
2799 /* If Unicode, try to downgrade
2800 * If not possible, croak. */
2801 SV* const tsv = sv_2mortal(newSVsv(sv));
2804 sv_utf8_downgrade(tsv, FALSE);
2805 tmps = SvPV_const(tsv, len);
2807 if (PL_op->op_type == OP_HEX)
2810 while (*tmps && len && isSPACE(*tmps))
2814 if (*tmps == 'x' || *tmps == 'X') {
2816 result_uv = grok_hex (tmps, &len, &flags, &result_nv);
2818 else if (*tmps == 'b' || *tmps == 'B')
2819 result_uv = grok_bin (tmps, &len, &flags, &result_nv);
2821 result_uv = grok_oct (tmps, &len, &flags, &result_nv);
2823 if (flags & PERL_SCAN_GREATER_THAN_UV_MAX) {
2837 SV * const sv = TOPs;
2839 if (SvGAMAGIC(sv)) {
2840 /* For an overloaded or magic scalar, we can't know in advance if
2841 it's going to be UTF-8 or not. Also, we can't call sv_len_utf8 as
2842 it likes to cache the length. Maybe that should be a documented
2847 = sv_2pv_flags(sv, &len,
2848 SV_UNDEF_RETURNS_NULL|SV_CONST_RETURN|SV_GMAGIC);
2851 if (!SvPADTMP(TARG)) {
2852 sv_setsv(TARG, &PL_sv_undef);
2857 else if (DO_UTF8(sv)) {
2858 SETi(utf8_length((U8*)p, (U8*)p + len));
2862 } else if (SvOK(sv)) {
2863 /* Neither magic nor overloaded. */
2865 SETi(sv_len_utf8(sv));
2869 if (!SvPADTMP(TARG)) {
2870 sv_setsv_nomg(TARG, &PL_sv_undef);
2878 /* Returns false if substring is completely outside original string.
2879 No length is indicated by len_iv = 0 and len_is_uv = 0. len_is_uv must
2880 always be true for an explicit 0.
2883 Perl_translate_substr_offsets(pTHX_ STRLEN curlen, IV pos1_iv,
2884 bool pos1_is_uv, IV len_iv,
2885 bool len_is_uv, STRLEN *posp,
2891 PERL_ARGS_ASSERT_TRANSLATE_SUBSTR_OFFSETS;
2893 if (!pos1_is_uv && pos1_iv < 0 && curlen) {
2894 pos1_is_uv = curlen-1 > ~(UV)pos1_iv;
2897 if ((pos1_is_uv || pos1_iv > 0) && (UV)pos1_iv > curlen)
2900 if (len_iv || len_is_uv) {
2901 if (!len_is_uv && len_iv < 0) {
2902 pos2_iv = curlen + len_iv;
2904 pos2_is_uv = curlen-1 > ~(UV)len_iv;
2907 } else { /* len_iv >= 0 */
2908 if (!pos1_is_uv && pos1_iv < 0) {
2909 pos2_iv = pos1_iv + len_iv;
2910 pos2_is_uv = (UV)len_iv > (UV)IV_MAX;
2912 if ((UV)len_iv > curlen-(UV)pos1_iv)
2915 pos2_iv = pos1_iv+len_iv;
2925 if (!pos2_is_uv && pos2_iv < 0) {
2926 if (!pos1_is_uv && pos1_iv < 0)
2930 else if (!pos1_is_uv && pos1_iv < 0)
2933 if ((UV)pos2_iv < (UV)pos1_iv)
2935 if ((UV)pos2_iv > curlen)
2938 /* pos1_iv and pos2_iv both in 0..curlen, so the cast is safe */
2939 *posp = (STRLEN)( (UV)pos1_iv );
2940 *lenp = (STRLEN)( (UV)pos2_iv - (UV)pos1_iv );
2957 I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
2958 const bool rvalue = (GIMME_V != G_VOID);
2961 const char *repl = NULL;
2963 int num_args = PL_op->op_private & 7;
2964 bool repl_need_utf8_upgrade = FALSE;
2965 bool repl_is_utf8 = FALSE;
2969 if(!(repl_sv = POPs)) num_args--;
2971 if ((len_sv = POPs)) {
2972 len_iv = SvIV(len_sv);
2973 len_is_uv = len_iv ? SvIOK_UV(len_sv) : 1;
2978 pos1_iv = SvIV(pos_sv);
2979 pos1_is_uv = SvIOK_UV(pos_sv);
2981 if (PL_op->op_private & OPpSUBSTR_REPL_FIRST) {
2987 repl = SvPV_const(repl_sv, repl_len);
2988 repl_is_utf8 = DO_UTF8(repl_sv) && repl_len;
2991 sv_utf8_upgrade(sv);
2993 else if (DO_UTF8(sv))
2994 repl_need_utf8_upgrade = TRUE;
2998 ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
2999 sv_magic(ret, NULL, PERL_MAGIC_substr, NULL, 0);
3001 LvTARG(ret) = SvREFCNT_inc_simple(sv);
3003 pos1_is_uv || pos1_iv >= 0
3004 ? (STRLEN)(UV)pos1_iv
3005 : (LvFLAGS(ret) |= 1, (STRLEN)(UV)-pos1_iv);
3007 len_is_uv || len_iv > 0
3008 ? (STRLEN)(UV)len_iv
3009 : (LvFLAGS(ret) |= 2, (STRLEN)(UV)-len_iv);
3012 PUSHs(ret); /* avoid SvSETMAGIC here */
3015 tmps = SvPV_const(sv, curlen);
3017 utf8_curlen = sv_len_utf8(sv);
3018 if (utf8_curlen == curlen)
3021 curlen = utf8_curlen;
3027 STRLEN pos, len, byte_len, byte_pos;
3029 if (!translate_substr_offsets(
3030 curlen, pos1_iv, pos1_is_uv, len_iv, len_is_uv, &pos, &len
3034 byte_pos = utf8_curlen
3035 ? sv_pos_u2b_flags(sv, pos, &byte_len, SV_CONST_RETURN) : pos;
3040 SvTAINTED_off(TARG); /* decontaminate */
3041 SvUTF8_off(TARG); /* decontaminate */
3042 sv_setpvn(TARG, tmps, byte_len);
3043 #ifdef USE_LOCALE_COLLATE
3044 sv_unmagic(TARG, PERL_MAGIC_collxfrm);
3051 SV* repl_sv_copy = NULL;
3053 if (repl_need_utf8_upgrade) {
3054 repl_sv_copy = newSVsv(repl_sv);
3055 sv_utf8_upgrade(repl_sv_copy);
3056 repl = SvPV_const(repl_sv_copy, repl_len);
3057 repl_is_utf8 = DO_UTF8(repl_sv_copy) && repl_len;
3060 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR),
3061 "Attempt to use reference as lvalue in substr"
3065 sv_insert_flags(sv, byte_pos, byte_len, repl, repl_len, 0);
3068 SvREFCNT_dec(repl_sv_copy);
3080 Perl_croak(aTHX_ "substr outside of string");
3081 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR), "substr outside of string");
3088 register const IV size = POPi;
3089 register const IV offset = POPi;
3090 register SV * const src = POPs;
3091 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
3094 if (lvalue) { /* it's an lvalue! */
3095 ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
3096 sv_magic(ret, NULL, PERL_MAGIC_vec, NULL, 0);
3098 LvTARG(ret) = SvREFCNT_inc_simple(src);
3099 LvTARGOFF(ret) = offset;
3100 LvTARGLEN(ret) = size;
3104 SvTAINTED_off(TARG); /* decontaminate */
3108 sv_setuv(ret, do_vecget(src, offset, size));
3124 const char *little_p;
3127 const bool is_index = PL_op->op_type == OP_INDEX;
3128 const bool threeargs = MAXARG >= 3 && (TOPs || ((void)POPs,0));
3134 big_p = SvPV_const(big, biglen);
3135 little_p = SvPV_const(little, llen);
3137 big_utf8 = DO_UTF8(big);
3138 little_utf8 = DO_UTF8(little);
3139 if (big_utf8 ^ little_utf8) {
3140 /* One needs to be upgraded. */
3141 if (little_utf8 && !PL_encoding) {
3142 /* Well, maybe instead we might be able to downgrade the small
3144 char * const pv = (char*)bytes_from_utf8((U8 *)little_p, &llen,
3147 /* If the large string is ISO-8859-1, and it's not possible to
3148 convert the small string to ISO-8859-1, then there is no
3149 way that it could be found anywhere by index. */
3154 /* At this point, pv is a malloc()ed string. So donate it to temp
3155 to ensure it will get free()d */
3156 little = temp = newSV(0);
3157 sv_usepvn(temp, pv, llen);
3158 little_p = SvPVX(little);
3161 ? newSVpvn(big_p, biglen) : newSVpvn(little_p, llen);
3164 sv_recode_to_utf8(temp, PL_encoding);
3166 sv_utf8_upgrade(temp);
3171 big_p = SvPV_const(big, biglen);
3174 little_p = SvPV_const(little, llen);
3178 if (SvGAMAGIC(big)) {
3179 /* Life just becomes a lot easier if I use a temporary here.
3180 Otherwise I need to avoid calls to sv_pos_u2b(), which (dangerously)
3181 will trigger magic and overloading again, as will fbm_instr()
3183 big = newSVpvn_flags(big_p, biglen,
3184 SVs_TEMP | (big_utf8 ? SVf_UTF8 : 0));
3187 if (SvGAMAGIC(little) || (is_index && !SvOK(little))) {
3188 /* index && SvOK() is a hack. fbm_instr() calls SvPV_const, which will
3189 warn on undef, and we've already triggered a warning with the
3190 SvPV_const some lines above. We can't remove that, as we need to
3191 call some SvPV to trigger overloading early and find out if the
3193 This is all getting to messy. The API isn't quite clean enough,
3194 because data access has side effects.
3196 little = newSVpvn_flags(little_p, llen,
3197 SVs_TEMP | (little_utf8 ? SVf_UTF8 : 0));
3198 little_p = SvPVX(little);
3202 offset = is_index ? 0 : biglen;
3204 if (big_utf8 && offset > 0)
3205 sv_pos_u2b(big, &offset, 0);
3211 else if (offset > (I32)biglen)
3213 if (!(little_p = is_index
3214 ? fbm_instr((unsigned char*)big_p + offset,
3215 (unsigned char*)big_p + biglen, little, 0)
3216 : rninstr(big_p, big_p + offset,
3217 little_p, little_p + llen)))
3220 retval = little_p - big_p;
3221 if (retval > 0 && big_utf8)
3222 sv_pos_b2u(big, &retval);
3232 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
3233 SvTAINTED_off(TARG);
3234 do_sprintf(TARG, SP-MARK, MARK+1);
3235 TAINT_IF(SvTAINTED(TARG));
3247 const U8 *s = (U8*)SvPV_const(argsv, len);
3249 if (PL_encoding && SvPOK(argsv) && !DO_UTF8(argsv)) {
3250 SV * const tmpsv = sv_2mortal(newSVsv(argsv));
3251 s = (U8*)sv_recode_to_utf8(tmpsv, PL_encoding);
3255 XPUSHu(DO_UTF8(argsv) ?
3256 utf8n_to_uvchr(s, UTF8_MAXBYTES, 0, UTF8_ALLOW_ANYUV) :
3270 if (!IN_BYTES /* under bytes, chr(-1) eq chr(0xff), etc. */
3271 && ((SvIOKp(top) && !SvIsUV(top) && SvIV_nomg(top) < 0)
3273 ((SvNOKp(top) || (SvOK(top) && !SvIsUV(top)))
3274 && SvNV_nomg(top) < 0.0))) {
3275 if (ckWARN(WARN_UTF8)) {
3276 if (SvGMAGICAL(top)) {
3277 SV *top2 = sv_newmortal();
3278 sv_setsv_nomg(top2, top);
3281 Perl_warner(aTHX_ packWARN(WARN_UTF8),
3282 "Invalid negative number (%"SVf") in chr", top);
3284 value = UNICODE_REPLACEMENT;
3286 value = SvUV_nomg(top);
3289 SvUPGRADE(TARG,SVt_PV);
3291 if (value > 255 && !IN_BYTES) {
3292 SvGROW(TARG, (STRLEN)UNISKIP(value)+1);
3293 tmps = (char*)uvchr_to_utf8_flags((U8*)SvPVX(TARG), value, 0);
3294 SvCUR_set(TARG, tmps - SvPVX_const(TARG));
3296 (void)SvPOK_only(TARG);
3305 *tmps++ = (char)value;
3307 (void)SvPOK_only(TARG);
3309 if (PL_encoding && !IN_BYTES) {
3310 sv_recode_to_utf8(TARG, PL_encoding);
3312 if (SvCUR(TARG) == 0
3313 || ! is_utf8_string((U8*)tmps, SvCUR(TARG))
3314 || UTF8_IS_REPLACEMENT((U8*) tmps, (U8*) tmps + SvCUR(TARG)))
3319 *tmps++ = (char)value;
3335 const char *tmps = SvPV_const(left, len);
3337 if (DO_UTF8(left)) {
3338 /* If Unicode, try to downgrade.
3339 * If not possible, croak.
3340 * Yes, we made this up. */
3341 SV* const tsv = sv_2mortal(newSVsv(left));
3344 sv_utf8_downgrade(tsv, FALSE);
3345 tmps = SvPV_const(tsv, len);
3347 # ifdef USE_ITHREADS
3349 if (!PL_reentrant_buffer->_crypt_struct_buffer) {
3350 /* This should be threadsafe because in ithreads there is only
3351 * one thread per interpreter. If this would not be true,
3352 * we would need a mutex to protect this malloc. */
3353 PL_reentrant_buffer->_crypt_struct_buffer =
3354 (struct crypt_data *)safemalloc(sizeof(struct crypt_data));
3355 #if defined(__GLIBC__) || defined(__EMX__)
3356 if (PL_reentrant_buffer->_crypt_struct_buffer) {
3357 PL_reentrant_buffer->_crypt_struct_buffer->initialized = 0;
3358 /* work around glibc-2.2.5 bug */
3359 PL_reentrant_buffer->_crypt_struct_buffer->current_saltbits = 0;
3363 # endif /* HAS_CRYPT_R */
3364 # endif /* USE_ITHREADS */
3366 sv_setpv(TARG, fcrypt(tmps, SvPV_nolen_const(right)));
3368 sv_setpv(TARG, PerlProc_crypt(tmps, SvPV_nolen_const(right)));
3374 "The crypt() function is unimplemented due to excessive paranoia.");
3378 /* Generally UTF-8 and UTF-EBCDIC are indistinguishable at this level. So
3379 * most comments below say UTF-8, when in fact they mean UTF-EBCDIC as well */
3381 /* Generates code to store a unicode codepoint c that is known to occupy
3382 * exactly two UTF-8 and UTF-EBCDIC bytes; it is stored into p and p+1,
3383 * and p is advanced to point to the next available byte after the two bytes */
3384 #define CAT_UNI_TO_UTF8_TWO_BYTE(p, c) \
3386 *(p)++ = UTF8_TWO_BYTE_HI(c); \
3387 *((p)++) = UTF8_TWO_BYTE_LO(c); \
3392 /* Actually is both lcfirst() and ucfirst(). Only the first character
3393 * changes. This means that possibly we can change in-place, ie., just
3394 * take the source and change that one character and store it back, but not
3395 * if read-only etc, or if the length changes */
3400 STRLEN slen; /* slen is the byte length of the whole SV. */
3403 bool inplace; /* ? Convert first char only, in-place */
3404 bool doing_utf8 = FALSE; /* ? using utf8 */
3405 bool convert_source_to_utf8 = FALSE; /* ? need to convert */
3406 const int op_type = PL_op->op_type;
3409 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
3410 STRLEN ulen; /* ulen is the byte length of the original Unicode character
3411 * stored as UTF-8 at s. */
3412 STRLEN tculen; /* tculen is the byte length of the freshly titlecased (or
3413 * lowercased) character stored in tmpbuf. May be either
3414 * UTF-8 or not, but in either case is the number of bytes */
3415 bool tainted = FALSE;
3419 s = (const U8*)SvPV_nomg_const(source, slen);
3421 if (ckWARN(WARN_UNINITIALIZED))
3422 report_uninit(source);
3427 /* We may be able to get away with changing only the first character, in
3428 * place, but not if read-only, etc. Later we may discover more reasons to
3429 * not convert in-place. */
3430 inplace = SvPADTMP(source) && !SvREADONLY(source) && SvTEMP(source);
3432 /* First calculate what the changed first character should be. This affects
3433 * whether we can just swap it out, leaving the rest of the string unchanged,
3434 * or even if have to convert the dest to UTF-8 when the source isn't */
3436 if (! slen) { /* If empty */
3437 need = 1; /* still need a trailing NUL */
3440 else if (DO_UTF8(source)) { /* Is the source utf8? */
3443 if (op_type == OP_UCFIRST) {
3444 _to_utf8_title_flags(s, tmpbuf, &tculen,
3445 cBOOL(IN_LOCALE_RUNTIME), &tainted);
3448 _to_utf8_lower_flags(s, tmpbuf, &tculen,
3449 cBOOL(IN_LOCALE_RUNTIME), &tainted);
3452 /* we can't do in-place if the length changes. */
3453 if (ulen != tculen) inplace = FALSE;
3454 need = slen + 1 - ulen + tculen;
3456 else { /* Non-zero length, non-UTF-8, Need to consider locale and if
3457 * latin1 is treated as caseless. Note that a locale takes
3459 ulen = 1; /* Original character is 1 byte */
3460 tculen = 1; /* Most characters will require one byte, but this will
3461 * need to be overridden for the tricky ones */
3464 if (op_type == OP_LCFIRST) {
3466 /* lower case the first letter: no trickiness for any character */
3467 *tmpbuf = (IN_LOCALE_RUNTIME) ? toLOWER_LC(*s) :
3468 ((IN_UNI_8_BIT) ? toLOWER_LATIN1(*s) : toLOWER(*s));
3471 else if (IN_LOCALE_RUNTIME) {
3472 *tmpbuf = toUPPER_LC(*s); /* This would be a bug if any locales
3473 * have upper and title case different
3476 else if (! IN_UNI_8_BIT) {
3477 *tmpbuf = toUPPER(*s); /* Returns caseless for non-ascii, or
3478 * on EBCDIC machines whatever the
3479 * native function does */
3481 else { /* is ucfirst non-UTF-8, not in locale, and cased latin1 */
3482 UV title_ord = _to_upper_title_latin1(*s, tmpbuf, &tculen, 's');
3484 assert(tculen == 2);
3486 /* If the result is an upper Latin1-range character, it can
3487 * still be represented in one byte, which is its ordinal */
3488 if (UTF8_IS_DOWNGRADEABLE_START(*tmpbuf)) {
3489 *tmpbuf = (U8) title_ord;
3493 /* Otherwise it became more than one ASCII character (in
3494 * the case of LATIN_SMALL_LETTER_SHARP_S) or changed to
3495 * beyond Latin1, so the number of bytes changed, so can't
3496 * replace just the first character in place. */
3499 /* If the result won't fit in a byte, the entire result will
3500 * have to be in UTF-8. Assume worst case sizing in
3501 * conversion. (all latin1 characters occupy at most two bytes
3503 if (title_ord > 255) {
3505 convert_source_to_utf8 = TRUE;
3506 need = slen * 2 + 1;
3508 /* The (converted) UTF-8 and UTF-EBCDIC lengths of all
3509 * (both) characters whose title case is above 255 is
3513 else { /* LATIN_SMALL_LETTER_SHARP_S expands by 1 byte */
3514 need = slen + 1 + 1;
3518 } /* End of use Unicode (Latin1) semantics */
3519 } /* End of changing the case of the first character */
3521 /* Here, have the first character's changed case stored in tmpbuf. Ready to
3522 * generate the result */
3525 /* We can convert in place. This means we change just the first
3526 * character without disturbing the rest; no need to grow */
3528 s = d = (U8*)SvPV_force_nomg(source, slen);
3534 /* Here, we can't convert in place; we earlier calculated how much
3535 * space we will need, so grow to accommodate that */
3536 SvUPGRADE(dest, SVt_PV);
3537 d = (U8*)SvGROW(dest, need);
3538 (void)SvPOK_only(dest);
3545 if (! convert_source_to_utf8) {
3547 /* Here both source and dest are in UTF-8, but have to create
3548 * the entire output. We initialize the result to be the
3549 * title/lower cased first character, and then append the rest
3551 sv_setpvn(dest, (char*)tmpbuf, tculen);
3553 sv_catpvn(dest, (char*)(s + ulen), slen - ulen);
3557 const U8 *const send = s + slen;
3559 /* Here the dest needs to be in UTF-8, but the source isn't,
3560 * except we earlier UTF-8'd the first character of the source
3561 * into tmpbuf. First put that into dest, and then append the
3562 * rest of the source, converting it to UTF-8 as we go. */
3564 /* Assert tculen is 2 here because the only two characters that
3565 * get to this part of the code have 2-byte UTF-8 equivalents */
3567 *d++ = *(tmpbuf + 1);
3568 s++; /* We have just processed the 1st char */
3570 for (; s < send; s++) {
3571 d = uvchr_to_utf8(d, *s);
3574 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3578 else { /* in-place UTF-8. Just overwrite the first character */
3579 Copy(tmpbuf, d, tculen, U8);
3580 SvCUR_set(dest, need - 1);
3588 else { /* Neither source nor dest are in or need to be UTF-8 */
3590 if (IN_LOCALE_RUNTIME) {
3594 if (inplace) { /* in-place, only need to change the 1st char */
3597 else { /* Not in-place */
3599 /* Copy the case-changed character(s) from tmpbuf */
3600 Copy(tmpbuf, d, tculen, U8);
3601 d += tculen - 1; /* Code below expects d to point to final
3602 * character stored */
3605 else { /* empty source */
3606 /* See bug #39028: Don't taint if empty */
3610 /* In a "use bytes" we don't treat the source as UTF-8, but, still want
3611 * the destination to retain that flag */
3615 if (!inplace) { /* Finish the rest of the string, unchanged */
3616 /* This will copy the trailing NUL */
3617 Copy(s + 1, d + 1, slen, U8);
3618 SvCUR_set(dest, need - 1);
3621 if (dest != source && SvTAINTED(source))
3627 /* There's so much setup/teardown code common between uc and lc, I wonder if
3628 it would be worth merging the two, and just having a switch outside each
3629 of the three tight loops. There is less and less commonality though */
3643 if (SvPADTMP(source) && !SvREADONLY(source) && !SvAMAGIC(source)
3644 && SvTEMP(source) && !DO_UTF8(source)
3645 && (IN_LOCALE_RUNTIME || ! IN_UNI_8_BIT)) {
3647 /* We can convert in place. The reason we can't if in UNI_8_BIT is to
3648 * make the loop tight, so we overwrite the source with the dest before
3649 * looking at it, and we need to look at the original source
3650 * afterwards. There would also need to be code added to handle
3651 * switching to not in-place in midstream if we run into characters
3652 * that change the length.
3655 s = d = (U8*)SvPV_force_nomg(source, len);
3662 /* The old implementation would copy source into TARG at this point.
3663 This had the side effect that if source was undef, TARG was now
3664 an undefined SV with PADTMP set, and they don't warn inside
3665 sv_2pv_flags(). However, we're now getting the PV direct from
3666 source, which doesn't have PADTMP set, so it would warn. Hence the
3670 s = (const U8*)SvPV_nomg_const(source, len);
3672 if (ckWARN(WARN_UNINITIALIZED))
3673 report_uninit(source);
3679 SvUPGRADE(dest, SVt_PV);
3680 d = (U8*)SvGROW(dest, min);
3681 (void)SvPOK_only(dest);
3686 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
3687 to check DO_UTF8 again here. */
3689 if (DO_UTF8(source)) {
3690 const U8 *const send = s + len;
3691 U8 tmpbuf[UTF8_MAXBYTES+1];
3692 bool tainted = FALSE;
3694 /* All occurrences of these are to be moved to follow any other marks.
3695 * This is context-dependent. We may not be passed enough context to
3696 * move the iota subscript beyond all of them, but we do the best we can
3697 * with what we're given. The result is always better than if we
3698 * hadn't done this. And, the problem would only arise if we are
3699 * passed a character without all its combining marks, which would be
3700 * the caller's mistake. The information this is based on comes from a
3701 * comment in Unicode SpecialCasing.txt, (and the Standard's text
3702 * itself) and so can't be checked properly to see if it ever gets
3703 * revised. But the likelihood of it changing is remote */
3704 bool in_iota_subscript = FALSE;
3710 if (in_iota_subscript && ! is_utf8_mark(s)) {
3712 /* A non-mark. Time to output the iota subscript */
3713 #define GREEK_CAPITAL_LETTER_IOTA 0x0399
3714 #define COMBINING_GREEK_YPOGEGRAMMENI 0x0345
3716 CAT_UNI_TO_UTF8_TWO_BYTE(d, GREEK_CAPITAL_LETTER_IOTA);
3717 in_iota_subscript = FALSE;
3720 /* Then handle the current character. Get the changed case value
3721 * and copy it to the output buffer */
3724 uv = _to_utf8_upper_flags(s, tmpbuf, &ulen,
3725 cBOOL(IN_LOCALE_RUNTIME), &tainted);
3726 if (uv == GREEK_CAPITAL_LETTER_IOTA
3727 && utf8_to_uvchr_buf(s, send, 0) == COMBINING_GREEK_YPOGEGRAMMENI)
3729 in_iota_subscript = TRUE;
3732 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
3733 /* If the eventually required minimum size outgrows the
3734 * available space, we need to grow. */
3735 const UV o = d - (U8*)SvPVX_const(dest);
3737 /* If someone uppercases one million U+03B0s we SvGROW()
3738 * one million times. Or we could try guessing how much to
3739 * allocate without allocating too much. Such is life.
3740 * See corresponding comment in lc code for another option
3743 d = (U8*)SvPVX(dest) + o;
3745 Copy(tmpbuf, d, ulen, U8);
3750 if (in_iota_subscript) {
3751 CAT_UNI_TO_UTF8_TWO_BYTE(d, GREEK_CAPITAL_LETTER_IOTA);
3756 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3762 else { /* Not UTF-8 */
3764 const U8 *const send = s + len;
3766 /* Use locale casing if in locale; regular style if not treating
3767 * latin1 as having case; otherwise the latin1 casing. Do the
3768 * whole thing in a tight loop, for speed, */
3769 if (IN_LOCALE_RUNTIME) {
3772 for (; s < send; d++, s++)
3773 *d = toUPPER_LC(*s);
3775 else if (! IN_UNI_8_BIT) {
3776 for (; s < send; d++, s++) {
3781 for (; s < send; d++, s++) {
3782 *d = toUPPER_LATIN1_MOD(*s);
3783 if (LIKELY(*d != LATIN_SMALL_LETTER_Y_WITH_DIAERESIS)) continue;
3785 /* The mainstream case is the tight loop above. To avoid
3786 * extra tests in that, all three characters that require
3787 * special handling are mapped by the MOD to the one tested
3789 * Use the source to distinguish between the three cases */
3791 if (*s == LATIN_SMALL_LETTER_SHARP_S) {
3793 /* uc() of this requires 2 characters, but they are
3794 * ASCII. If not enough room, grow the string */
3795 if (SvLEN(dest) < ++min) {
3796 const UV o = d - (U8*)SvPVX_const(dest);
3798 d = (U8*)SvPVX(dest) + o;
3800 *d++ = 'S'; *d = 'S'; /* upper case is 'SS' */
3801 continue; /* Back to the tight loop; still in ASCII */
3804 /* The other two special handling characters have their
3805 * upper cases outside the latin1 range, hence need to be
3806 * in UTF-8, so the whole result needs to be in UTF-8. So,
3807 * here we are somewhere in the middle of processing a
3808 * non-UTF-8 string, and realize that we will have to convert
3809 * the whole thing to UTF-8. What to do? There are
3810 * several possibilities. The simplest to code is to
3811 * convert what we have so far, set a flag, and continue on
3812 * in the loop. The flag would be tested each time through
3813 * the loop, and if set, the next character would be
3814 * converted to UTF-8 and stored. But, I (khw) didn't want
3815 * to slow down the mainstream case at all for this fairly
3816 * rare case, so I didn't want to add a test that didn't
3817 * absolutely have to be there in the loop, besides the
3818 * possibility that it would get too complicated for
3819 * optimizers to deal with. Another possibility is to just
3820 * give up, convert the source to UTF-8, and restart the
3821 * function that way. Another possibility is to convert
3822 * both what has already been processed and what is yet to
3823 * come separately to UTF-8, then jump into the loop that
3824 * handles UTF-8. But the most efficient time-wise of the
3825 * ones I could think of is what follows, and turned out to
3826 * not require much extra code. */
3828 /* Convert what we have so far into UTF-8, telling the
3829 * function that we know it should be converted, and to
3830 * allow extra space for what we haven't processed yet.
3831 * Assume the worst case space requirements for converting
3832 * what we haven't processed so far: that it will require
3833 * two bytes for each remaining source character, plus the
3834 * NUL at the end. This may cause the string pointer to
3835 * move, so re-find it. */
3837 len = d - (U8*)SvPVX_const(dest);
3838 SvCUR_set(dest, len);
3839 len = sv_utf8_upgrade_flags_grow(dest,
3840 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
3842 d = (U8*)SvPVX(dest) + len;
3844 /* Now process the remainder of the source, converting to
3845 * upper and UTF-8. If a resulting byte is invariant in
3846 * UTF-8, output it as-is, otherwise convert to UTF-8 and
3847 * append it to the output. */
3848 for (; s < send; s++) {
3849 (void) _to_upper_title_latin1(*s, d, &len, 'S');
3853 /* Here have processed the whole source; no need to continue
3854 * with the outer loop. Each character has been converted
3855 * to upper case and converted to UTF-8 */
3858 } /* End of processing all latin1-style chars */
3859 } /* End of processing all chars */
3860 } /* End of source is not empty */
3862 if (source != dest) {
3863 *d = '\0'; /* Here d points to 1 after last char, add NUL */
3864 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3866 } /* End of isn't utf8 */
3867 if (dest != source && SvTAINTED(source))
3886 if (SvPADTMP(source) && !SvREADONLY(source) && !SvAMAGIC(source)
3887 && SvTEMP(source) && !DO_UTF8(source)) {
3889 /* We can convert in place, as lowercasing anything in the latin1 range
3890 * (or else DO_UTF8 would have been on) doesn't lengthen it */
3892 s = d = (U8*)SvPV_force_nomg(source, len);
3899 /* The old implementation would copy source into TARG at this point.
3900 This had the side effect that if source was undef, TARG was now
3901 an undefined SV with PADTMP set, and they don't warn inside
3902 sv_2pv_flags(). However, we're now getting the PV direct from
3903 source, which doesn't have PADTMP set, so it would warn. Hence the
3907 s = (const U8*)SvPV_nomg_const(source, len);
3909 if (ckWARN(WARN_UNINITIALIZED))
3910 report_uninit(source);
3916 SvUPGRADE(dest, SVt_PV);
3917 d = (U8*)SvGROW(dest, min);
3918 (void)SvPOK_only(dest);
3923 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
3924 to check DO_UTF8 again here. */
3926 if (DO_UTF8(source)) {
3927 const U8 *const send = s + len;
3928 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
3929 bool tainted = FALSE;
3932 const STRLEN u = UTF8SKIP(s);
3935 _to_utf8_lower_flags(s, tmpbuf, &ulen,
3936 cBOOL(IN_LOCALE_RUNTIME), &tainted);
3938 /* Here is where we would do context-sensitive actions. See the
3939 * commit message for this comment for why there isn't any */
3941 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
3943 /* If the eventually required minimum size outgrows the
3944 * available space, we need to grow. */
3945 const UV o = d - (U8*)SvPVX_const(dest);
3947 /* If someone lowercases one million U+0130s we SvGROW() one
3948 * million times. Or we could try guessing how much to
3949 * allocate without allocating too much. Such is life.
3950 * Another option would be to grow an extra byte or two more
3951 * each time we need to grow, which would cut down the million
3952 * to 500K, with little waste */
3954 d = (U8*)SvPVX(dest) + o;
3957 /* Copy the newly lowercased letter to the output buffer we're
3959 Copy(tmpbuf, d, ulen, U8);
3962 } /* End of looping through the source string */
3965 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3970 } else { /* Not utf8 */
3972 const U8 *const send = s + len;
3974 /* Use locale casing if in locale; regular style if not treating
3975 * latin1 as having case; otherwise the latin1 casing. Do the
3976 * whole thing in a tight loop, for speed, */
3977 if (IN_LOCALE_RUNTIME) {
3980 for (; s < send; d++, s++)
3981 *d = toLOWER_LC(*s);
3983 else if (! IN_UNI_8_BIT) {
3984 for (; s < send; d++, s++) {
3989 for (; s < send; d++, s++) {
3990 *d = toLOWER_LATIN1(*s);
3994 if (source != dest) {
3996 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3999 if (dest != source && SvTAINTED(source))
4008 SV * const sv = TOPs;
4010 register const char *s = SvPV_const(sv,len);
4012 SvUTF8_off(TARG); /* decontaminate */
4015 SvUPGRADE(TARG, SVt_PV);
4016 SvGROW(TARG, (len * 2) + 1);
4020 STRLEN ulen = UTF8SKIP(s);
4021 bool to_quote = FALSE;
4023 if (UTF8_IS_INVARIANT(*s)) {
4024 if (_isQUOTEMETA(*s)) {
4028 else if (UTF8_IS_DOWNGRADEABLE_START(*s)) {
4030 /* In locale, we quote all non-ASCII Latin1 chars.
4031 * Otherwise use the quoting rules */
4032 if (IN_LOCALE_RUNTIME
4033 || _isQUOTEMETA(TWO_BYTE_UTF8_TO_UNI(*s, *(s + 1))))
4038 else if (_is_utf8_quotemeta((U8 *) s)) {
4053 else if (IN_UNI_8_BIT) {
4055 if (_isQUOTEMETA(*s))
4061 /* For non UNI_8_BIT (and hence in locale) just quote all \W
4062 * including everything above ASCII */
4064 if (!isWORDCHAR_A(*s))
4070 SvCUR_set(TARG, d - SvPVX_const(TARG));
4071 (void)SvPOK_only_UTF8(TARG);
4074 sv_setpvn(TARG, s, len);
4091 U8 tmpbuf[UTF8_MAXBYTES * UTF8_MAX_FOLD_CHAR_EXPAND + 1];
4092 const bool full_folding = TRUE;
4093 const U8 flags = ( full_folding ? FOLD_FLAGS_FULL : 0 )
4094 | ( IN_LOCALE_RUNTIME ? FOLD_FLAGS_LOCALE : 0 );
4096 /* This is a facsimile of pp_lc, but with a thousand bugs thanks to me.
4097 * You are welcome(?) -Hugmeir
4105 s = (const U8*)SvPV_nomg_const(source, len);
4107 if (ckWARN(WARN_UNINITIALIZED))
4108 report_uninit(source);
4115 SvUPGRADE(dest, SVt_PV);
4116 d = (U8*)SvGROW(dest, min);
4117 (void)SvPOK_only(dest);
4122 if (DO_UTF8(source)) { /* UTF-8 flagged string. */
4123 bool tainted = FALSE;
4125 const STRLEN u = UTF8SKIP(s);
4128 _to_utf8_fold_flags(s, tmpbuf, &ulen, flags, &tainted);
4130 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
4131 const UV o = d - (U8*)SvPVX_const(dest);
4133 d = (U8*)SvPVX(dest) + o;
4136 Copy(tmpbuf, d, ulen, U8);
4145 } /* Unflagged string */
4147 /* For locale, bytes, and nothing, the behavior is supposed to be the
4150 if ( IN_LOCALE_RUNTIME ) { /* Under locale */
4153 for (; s < send; d++, s++)
4154 *d = toLOWER_LC(*s);
4156 else if ( !IN_UNI_8_BIT ) { /* Under nothing, or bytes */
4157 for (; s < send; d++, s++)
4161 /* For ASCII and the Latin-1 range, there's only two troublesome folds,
4162 * \x{DF} (\N{LATIN SMALL LETTER SHARP S}), which under full casefolding
4163 * becomes 'ss', and \x{B5} (\N{MICRO SIGN}), which under any fold becomes
4164 * \x{3BC} (\N{GREEK SMALL LETTER MU}) -- For the rest, the casefold is
4167 for (; s < send; d++, s++) {
4168 if (*s == MICRO_SIGN) {
4169 /* \N{MICRO SIGN}'s casefold is \N{GREEK SMALL LETTER MU}, which
4170 * is outside of the latin-1 range. There's a couple of ways to
4171 * deal with this -- khw discusses them in pp_lc/uc, so go there :)
4172 * What we do here is upgrade what we had already casefolded,
4173 * then enter an inner loop that appends the rest of the characters
4176 len = d - (U8*)SvPVX_const(dest);
4177 SvCUR_set(dest, len);
4178 len = sv_utf8_upgrade_flags_grow(dest,
4179 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
4180 /* The max expansion for latin1
4181 * chars is 1 byte becomes 2 */
4183 d = (U8*)SvPVX(dest) + len;
4185 CAT_UNI_TO_UTF8_TWO_BYTE(d, GREEK_SMALL_LETTER_MU);
4187 for (; s < send; s++) {
4189 UV fc = _to_uni_fold_flags(*s, tmpbuf, &ulen, flags);
4190 if UNI_IS_INVARIANT(fc) {
4191 if ( full_folding && *s == LATIN_SMALL_LETTER_SHARP_S) {
4199 Copy(tmpbuf, d, ulen, U8);
4205 else if (full_folding && *s == LATIN_SMALL_LETTER_SHARP_S) {
4206 /* Under full casefolding, LATIN SMALL LETTER SHARP S becomes "ss",
4207 * which may require growing the SV.
4209 if (SvLEN(dest) < ++min) {
4210 const UV o = d - (U8*)SvPVX_const(dest);
4212 d = (U8*)SvPVX(dest) + o;
4217 else { /* If it's not one of those two, the fold is their lower case */
4218 *d = toLOWER_LATIN1(*s);
4224 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4226 if (SvTAINTED(source))
4236 dVAR; dSP; dMARK; dORIGMARK;
4237 register AV *const av = MUTABLE_AV(POPs);
4238 register const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
4240 if (SvTYPE(av) == SVt_PVAV) {
4241 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
4242 bool can_preserve = FALSE;
4248 can_preserve = SvCANEXISTDELETE(av);
4251 if (lval && localizing) {
4254 for (svp = MARK + 1; svp <= SP; svp++) {
4255 const I32 elem = SvIV(*svp);
4259 if (max > AvMAX(av))
4263 while (++MARK <= SP) {
4265 I32 elem = SvIV(*MARK);
4266 bool preeminent = TRUE;
4268 if (localizing && can_preserve) {
4269 /* If we can determine whether the element exist,
4270 * Try to preserve the existenceness of a tied array
4271 * element by using EXISTS and DELETE if possible.
4272 * Fallback to FETCH and STORE otherwise. */
4273 preeminent = av_exists(av, elem);
4276 svp = av_fetch(av, elem, lval);
4278 if (!svp || *svp == &PL_sv_undef)
4279 DIE(aTHX_ PL_no_aelem, elem);
4282 save_aelem(av, elem, svp);
4284 SAVEADELETE(av, elem);
4287 *MARK = svp ? *svp : &PL_sv_undef;
4290 if (GIMME != G_ARRAY) {
4292 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
4298 /* Smart dereferencing for keys, values and each */
4310 (SvTYPE(sv) != SVt_PVHV && SvTYPE(sv) != SVt_PVAV)
4315 "Type of argument to %s must be unblessed hashref or arrayref",
4316 PL_op_desc[PL_op->op_type] );
4319 if (PL_op->op_flags & OPf_SPECIAL && SvTYPE(sv) == SVt_PVAV)
4321 "Can't modify %s in %s",
4322 PL_op_desc[PL_op->op_type], PL_op_desc[PL_op->op_next->op_type]
4325 /* Delegate to correct function for op type */
4327 if (PL_op->op_type == OP_RKEYS || PL_op->op_type == OP_RVALUES) {
4328 return (SvTYPE(sv) == SVt_PVHV) ? Perl_do_kv(aTHX) : Perl_pp_akeys(aTHX);
4331 return (SvTYPE(sv) == SVt_PVHV) ? Perl_pp_each(aTHX) : Perl_pp_aeach(aTHX);
4339 AV *array = MUTABLE_AV(POPs);
4340 const I32 gimme = GIMME_V;
4341 IV *iterp = Perl_av_iter_p(aTHX_ array);
4342 const IV current = (*iterp)++;
4344 if (current > av_len(array)) {
4346 if (gimme == G_SCALAR)
4354 if (gimme == G_ARRAY) {
4355 SV **const element = av_fetch(array, current, 0);
4356 PUSHs(element ? *element : &PL_sv_undef);
4365 AV *array = MUTABLE_AV(POPs);
4366 const I32 gimme = GIMME_V;
4368 *Perl_av_iter_p(aTHX_ array) = 0;
4370 if (gimme == G_SCALAR) {
4372 PUSHi(av_len(array) + 1);
4374 else if (gimme == G_ARRAY) {
4375 IV n = Perl_av_len(aTHX_ array);
4380 if (PL_op->op_type == OP_AKEYS || PL_op->op_type == OP_RKEYS) {
4381 for (i = 0; i <= n; i++) {
4386 for (i = 0; i <= n; i++) {
4387 SV *const *const elem = Perl_av_fetch(aTHX_ array, i, 0);
4388 PUSHs(elem ? *elem : &PL_sv_undef);
4395 /* Associative arrays. */
4401 HV * hash = MUTABLE_HV(POPs);
4403 const I32 gimme = GIMME_V;
4406 /* might clobber stack_sp */
4407 entry = hv_iternext(hash);
4412 SV* const sv = hv_iterkeysv(entry);
4413 PUSHs(sv); /* won't clobber stack_sp */
4414 if (gimme == G_ARRAY) {
4417 /* might clobber stack_sp */
4418 val = hv_iterval(hash, entry);
4423 else if (gimme == G_SCALAR)
4430 S_do_delete_local(pTHX)
4434 const I32 gimme = GIMME_V;
4437 const bool sliced = !!(PL_op->op_private & OPpSLICE);
4438 SV *unsliced_keysv = sliced ? NULL : POPs;
4439 SV * const osv = POPs;
4440 register SV **mark =
4441 sliced ? PL_stack_base + POPMARK : &unsliced_keysv-1;
4443 const bool tied = SvRMAGICAL(osv)
4444 && mg_find((const SV *)osv, PERL_MAGIC_tied);
4445 const bool can_preserve = SvCANEXISTDELETE(osv);
4446 const U32 type = SvTYPE(osv);
4447 SV ** const end = sliced ? SP : &unsliced_keysv;
4449 if (type == SVt_PVHV) { /* hash element */
4450 HV * const hv = MUTABLE_HV(osv);
4451 while (++MARK <= end) {
4452 SV * const keysv = *MARK;
4454 bool preeminent = TRUE;
4456 preeminent = hv_exists_ent(hv, keysv, 0);
4458 HE *he = hv_fetch_ent(hv, keysv, 1, 0);
4465 sv = hv_delete_ent(hv, keysv, 0, 0);
4466 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4469 if (!sv) DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
4470 save_helem_flags(hv, keysv, &sv, SAVEf_KEEPOLDELEM);
4472 *MARK = sv_mortalcopy(sv);
4478 SAVEHDELETE(hv, keysv);
4479 *MARK = &PL_sv_undef;
4483 else if (type == SVt_PVAV) { /* array element */
4484 if (PL_op->op_flags & OPf_SPECIAL) {
4485 AV * const av = MUTABLE_AV(osv);
4486 while (++MARK <= end) {
4487 I32 idx = SvIV(*MARK);
4489 bool preeminent = TRUE;
4491 preeminent = av_exists(av, idx);
4493 SV **svp = av_fetch(av, idx, 1);
4500 sv = av_delete(av, idx, 0);
4501 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4504 save_aelem_flags(av, idx, &sv, SAVEf_KEEPOLDELEM);
4506 *MARK = sv_mortalcopy(sv);
4512 SAVEADELETE(av, idx);
4513 *MARK = &PL_sv_undef;
4518 DIE(aTHX_ "panic: avhv_delete no longer supported");
4521 DIE(aTHX_ "Not a HASH reference");
4523 if (gimme == G_VOID)
4525 else if (gimme == G_SCALAR) {
4530 *++MARK = &PL_sv_undef;
4534 else if (gimme != G_VOID)
4535 PUSHs(unsliced_keysv);
4547 if (PL_op->op_private & OPpLVAL_INTRO)
4548 return do_delete_local();
4551 discard = (gimme == G_VOID) ? G_DISCARD : 0;
4553 if (PL_op->op_private & OPpSLICE) {
4555 HV * const hv = MUTABLE_HV(POPs);
4556 const U32 hvtype = SvTYPE(hv);
4557 if (hvtype == SVt_PVHV) { /* hash element */
4558 while (++MARK <= SP) {
4559 SV * const sv = hv_delete_ent(hv, *MARK, discard, 0);
4560 *MARK = sv ? sv : &PL_sv_undef;
4563 else if (hvtype == SVt_PVAV) { /* array element */
4564 if (PL_op->op_flags & OPf_SPECIAL) {
4565 while (++MARK <= SP) {
4566 SV * const sv = av_delete(MUTABLE_AV(hv), SvIV(*MARK), discard);
4567 *MARK = sv ? sv : &PL_sv_undef;
4572 DIE(aTHX_ "Not a HASH reference");
4575 else if (gimme == G_SCALAR) {
4580 *++MARK = &PL_sv_undef;
4586 HV * const hv = MUTABLE_HV(POPs);
4588 if (SvTYPE(hv) == SVt_PVHV)
4589 sv = hv_delete_ent(hv, keysv, discard, 0);
4590 else if (SvTYPE(hv) == SVt_PVAV) {
4591 if (PL_op->op_flags & OPf_SPECIAL)
4592 sv = av_delete(MUTABLE_AV(hv), SvIV(keysv), discard);
4594 DIE(aTHX_ "panic: avhv_delete no longer supported");
4597 DIE(aTHX_ "Not a HASH reference");
4613 if (PL_op->op_private & OPpEXISTS_SUB) {
4615 SV * const sv = POPs;
4616 CV * const cv = sv_2cv(sv, &hv, &gv, 0);
4619 if (gv && isGV(gv) && GvCV(gv) && !GvCVGEN(gv))
4624 hv = MUTABLE_HV(POPs);
4625 if (SvTYPE(hv) == SVt_PVHV) {
4626 if (hv_exists_ent(hv, tmpsv, 0))
4629 else if (SvTYPE(hv) == SVt_PVAV) {
4630 if (PL_op->op_flags & OPf_SPECIAL) { /* array element */
4631 if (av_exists(MUTABLE_AV(hv), SvIV(tmpsv)))
4636 DIE(aTHX_ "Not a HASH reference");
4643 dVAR; dSP; dMARK; dORIGMARK;
4644 register HV * const hv = MUTABLE_HV(POPs);
4645 register const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
4646 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
4647 bool can_preserve = FALSE;
4653 if (SvCANEXISTDELETE(hv))
4654 can_preserve = TRUE;
4657 while (++MARK <= SP) {
4658 SV * const keysv = *MARK;
4661 bool preeminent = TRUE;
4663 if (localizing && can_preserve) {
4664 /* If we can determine whether the element exist,
4665 * try to preserve the existenceness of a tied hash
4666 * element by using EXISTS and DELETE if possible.
4667 * Fallback to FETCH and STORE otherwise. */
4668 preeminent = hv_exists_ent(hv, keysv, 0);
4671 he = hv_fetch_ent(hv, keysv, lval, 0);
4672 svp = he ? &HeVAL(he) : NULL;
4675 if (!svp || !*svp || *svp == &PL_sv_undef) {
4676 DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
4679 if (HvNAME_get(hv) && isGV(*svp))
4680 save_gp(MUTABLE_GV(*svp), !(PL_op->op_flags & OPf_SPECIAL));
4681 else if (preeminent)
4682 save_helem_flags(hv, keysv, svp,
4683 (PL_op->op_flags & OPf_SPECIAL) ? 0 : SAVEf_SETMAGIC);
4685 SAVEHDELETE(hv, keysv);
4688 *MARK = svp && *svp ? *svp : &PL_sv_undef;
4690 if (GIMME != G_ARRAY) {
4692 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
4698 /* List operators. */
4703 if (GIMME != G_ARRAY) {
4705 *MARK = *SP; /* unwanted list, return last item */
4707 *MARK = &PL_sv_undef;
4717 SV ** const lastrelem = PL_stack_sp;
4718 SV ** const lastlelem = PL_stack_base + POPMARK;
4719 SV ** const firstlelem = PL_stack_base + POPMARK + 1;
4720 register SV ** const firstrelem = lastlelem + 1;
4721 I32 is_something_there = FALSE;
4723 register const I32 max = lastrelem - lastlelem;
4724 register SV **lelem;
4726 if (GIMME != G_ARRAY) {
4727 I32 ix = SvIV(*lastlelem);
4730 if (ix < 0 || ix >= max)
4731 *firstlelem = &PL_sv_undef;
4733 *firstlelem = firstrelem[ix];
4739 SP = firstlelem - 1;
4743 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
4744 I32 ix = SvIV(*lelem);
4747 if (ix < 0 || ix >= max)
4748 *lelem = &PL_sv_undef;
4750 is_something_there = TRUE;
4751 if (!(*lelem = firstrelem[ix]))
4752 *lelem = &PL_sv_undef;
4755 if (is_something_there)
4758 SP = firstlelem - 1;
4764 dVAR; dSP; dMARK; dORIGMARK;
4765 const I32 items = SP - MARK;
4766 SV * const av = MUTABLE_SV(av_make(items, MARK+1));
4767 SP = ORIGMARK; /* av_make() might realloc stack_sp */
4768 mXPUSHs((PL_op->op_flags & OPf_SPECIAL)
4769 ? newRV_noinc(av) : av);
4775 dVAR; dSP; dMARK; dORIGMARK;
4776 HV* const hv = newHV();
4779 SV * const key = *++MARK;
4780 SV * const val = newSV(0);
4782 sv_setsv(val, *++MARK);
4784 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Odd number of elements in anonymous hash");
4785 (void)hv_store_ent(hv,key,val,0);
4788 mXPUSHs((PL_op->op_flags & OPf_SPECIAL)
4789 ? newRV_noinc(MUTABLE_SV(hv)) : MUTABLE_SV(hv));
4794 S_deref_plain_array(pTHX_ AV *ary)
4796 if (SvTYPE(ary) == SVt_PVAV) return ary;
4797 SvGETMAGIC((SV *)ary);
4798 if (!SvROK(ary) || SvTYPE(SvRV(ary)) != SVt_PVAV)
4799 Perl_die(aTHX_ "Not an ARRAY reference");
4800 else if (SvOBJECT(SvRV(ary)))
4801 Perl_die(aTHX_ "Not an unblessed ARRAY reference");
4802 return (AV *)SvRV(ary);
4805 #if defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
4806 # define DEREF_PLAIN_ARRAY(ary) \
4809 SvTYPE(aRrRay) == SVt_PVAV \
4811 : S_deref_plain_array(aTHX_ aRrRay); \
4814 # define DEREF_PLAIN_ARRAY(ary) \
4816 PL_Sv = (SV *)(ary), \
4817 SvTYPE(PL_Sv) == SVt_PVAV \
4819 : S_deref_plain_array(aTHX_ (AV *)PL_Sv) \
4825 dVAR; dSP; dMARK; dORIGMARK;
4826 int num_args = (SP - MARK);
4827 register AV *ary = DEREF_PLAIN_ARRAY(MUTABLE_AV(*++MARK));
4831 register I32 offset;
4832 register I32 length;
4836 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
4839 return Perl_tied_method(aTHX_ "SPLICE", mark - 1, MUTABLE_SV(ary), mg,
4840 GIMME_V | TIED_METHOD_ARGUMENTS_ON_STACK,
4847 offset = i = SvIV(*MARK);
4849 offset += AvFILLp(ary) + 1;
4851 DIE(aTHX_ PL_no_aelem, i);
4853 length = SvIVx(*MARK++);
4855 length += AvFILLp(ary) - offset + 1;
4861 length = AvMAX(ary) + 1; /* close enough to infinity */
4865 length = AvMAX(ary) + 1;
4867 if (offset > AvFILLp(ary) + 1) {
4869 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "splice() offset past end of array" );
4870 offset = AvFILLp(ary) + 1;
4872 after = AvFILLp(ary) + 1 - (offset + length);
4873 if (after < 0) { /* not that much array */
4874 length += after; /* offset+length now in array */
4880 /* At this point, MARK .. SP-1 is our new LIST */
4883 diff = newlen - length;
4884 if (newlen && !AvREAL(ary) && AvREIFY(ary))
4887 /* make new elements SVs now: avoid problems if they're from the array */
4888 for (dst = MARK, i = newlen; i; i--) {
4889 SV * const h = *dst;
4890 *dst++ = newSVsv(h);
4893 if (diff < 0) { /* shrinking the area */
4894 SV **tmparyval = NULL;
4896 Newx(tmparyval, newlen, SV*); /* so remember insertion */
4897 Copy(MARK, tmparyval, newlen, SV*);
4900 MARK = ORIGMARK + 1;
4901 if (GIMME == G_ARRAY) { /* copy return vals to stack */
4902 MEXTEND(MARK, length);
4903 Copy(AvARRAY(ary)+offset, MARK, length, SV*);
4905 EXTEND_MORTAL(length);
4906 for (i = length, dst = MARK; i; i--) {
4907 sv_2mortal(*dst); /* free them eventually */
4914 *MARK = AvARRAY(ary)[offset+length-1];
4917 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
4918 SvREFCNT_dec(*dst++); /* free them now */
4921 AvFILLp(ary) += diff;
4923 /* pull up or down? */
4925 if (offset < after) { /* easier to pull up */
4926 if (offset) { /* esp. if nothing to pull */
4927 src = &AvARRAY(ary)[offset-1];
4928 dst = src - diff; /* diff is negative */
4929 for (i = offset; i > 0; i--) /* can't trust Copy */
4933 AvARRAY(ary) = AvARRAY(ary) - diff; /* diff is negative */
4937 if (after) { /* anything to pull down? */
4938 src = AvARRAY(ary) + offset + length;
4939 dst = src + diff; /* diff is negative */
4940 Move(src, dst, after, SV*);
4942 dst = &AvARRAY(ary)[AvFILLp(ary)+1];
4943 /* avoid later double free */
4947 dst[--i] = &PL_sv_undef;
4950 Copy( tmparyval, AvARRAY(ary) + offset, newlen, SV* );
4951 Safefree(tmparyval);
4954 else { /* no, expanding (or same) */
4955 SV** tmparyval = NULL;
4957 Newx(tmparyval, length, SV*); /* so remember deletion */
4958 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
4961 if (diff > 0) { /* expanding */
4962 /* push up or down? */
4963 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
4967 Move(src, dst, offset, SV*);
4969 AvARRAY(ary) = AvARRAY(ary) - diff;/* diff is positive */
4971 AvFILLp(ary) += diff;
4974 if (AvFILLp(ary) + diff >= AvMAX(ary)) /* oh, well */
4975 av_extend(ary, AvFILLp(ary) + diff);
4976 AvFILLp(ary) += diff;
4979 dst = AvARRAY(ary) + AvFILLp(ary);
4981 for (i = after; i; i--) {
4989 Copy( MARK, AvARRAY(ary) + offset, newlen, SV* );
4992 MARK = ORIGMARK + 1;
4993 if (GIMME == G_ARRAY) { /* copy return vals to stack */
4995 Copy(tmparyval, MARK, length, SV*);
4997 EXTEND_MORTAL(length);
4998 for (i = length, dst = MARK; i; i--) {
4999 sv_2mortal(*dst); /* free them eventually */
5006 else if (length--) {
5007 *MARK = tmparyval[length];
5010 while (length-- > 0)
5011 SvREFCNT_dec(tmparyval[length]);
5015 *MARK = &PL_sv_undef;
5016 Safefree(tmparyval);
5020 mg_set(MUTABLE_SV(ary));
5028 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
5029 register AV * const ary = DEREF_PLAIN_ARRAY(MUTABLE_AV(*++MARK));
5030 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
5033 *MARK-- = SvTIED_obj(MUTABLE_SV(ary), mg);
5036 ENTER_with_name("call_PUSH");
5037 call_method("PUSH",G_SCALAR|G_DISCARD);
5038 LEAVE_with_name("call_PUSH");
5042 PL_delaymagic = DM_DELAY;
5043 for (++MARK; MARK <= SP; MARK++) {
5044 SV * const sv = newSV(0);
5046 sv_setsv(sv, *MARK);
5047 av_store(ary, AvFILLp(ary)+1, sv);
5049 if (PL_delaymagic & DM_ARRAY_ISA)
5050 mg_set(MUTABLE_SV(ary));
5055 if (OP_GIMME(PL_op, 0) != G_VOID) {
5056 PUSHi( AvFILL(ary) + 1 );
5065 AV * const av = PL_op->op_flags & OPf_SPECIAL
5066 ? MUTABLE_AV(GvAV(PL_defgv)) : DEREF_PLAIN_ARRAY(MUTABLE_AV(POPs));
5067 SV * const sv = PL_op->op_type == OP_SHIFT ? av_shift(av) : av_pop(av);
5071 (void)sv_2mortal(sv);
5078 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
5079 register AV *ary = DEREF_PLAIN_ARRAY(MUTABLE_AV(*++MARK));
5080 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
5083 *MARK-- = SvTIED_obj(MUTABLE_SV(ary), mg);
5086 ENTER_with_name("call_UNSHIFT");
5087 call_method("UNSHIFT",G_SCALAR|G_DISCARD);
5088 LEAVE_with_name("call_UNSHIFT");
5093 av_unshift(ary, SP - MARK);
5095 SV * const sv = newSVsv(*++MARK);
5096 (void)av_store(ary, i++, sv);
5100 if (OP_GIMME(PL_op, 0) != G_VOID) {
5101 PUSHi( AvFILL(ary) + 1 );
5110 if (GIMME == G_ARRAY) {
5111 if (PL_op->op_private & OPpREVERSE_INPLACE) {
5115 assert( MARK+1 == SP && *SP && SvTYPE(*SP) == SVt_PVAV);
5116 (void)POPMARK; /* remove mark associated with ex-OP_AASSIGN */
5117 av = MUTABLE_AV((*SP));
5118 /* In-place reversing only happens in void context for the array
5119 * assignment. We don't need to push anything on the stack. */
5122 if (SvMAGICAL(av)) {
5124 register SV *tmp = sv_newmortal();
5125 /* For SvCANEXISTDELETE */
5128 bool can_preserve = SvCANEXISTDELETE(av);
5130 for (i = 0, j = av_len(av); i < j; ++i, --j) {
5131 register SV *begin, *end;
5134 if (!av_exists(av, i)) {
5135 if (av_exists(av, j)) {
5136 register SV *sv = av_delete(av, j, 0);
5137 begin = *av_fetch(av, i, TRUE);
5138 sv_setsv_mg(begin, sv);
5142 else if (!av_exists(av, j)) {
5143 register SV *sv = av_delete(av, i, 0);
5144 end = *av_fetch(av, j, TRUE);
5145 sv_setsv_mg(end, sv);
5150 begin = *av_fetch(av, i, TRUE);
5151 end = *av_fetch(av, j, TRUE);
5152 sv_setsv(tmp, begin);
5153 sv_setsv_mg(begin, end);
5154 sv_setsv_mg(end, tmp);
5158 SV **begin = AvARRAY(av);
5161 SV **end = begin + AvFILLp(av);
5163 while (begin < end) {
5164 register SV * const tmp = *begin;
5175 register SV * const tmp = *MARK;
5179 /* safe as long as stack cannot get extended in the above */
5185 register char *down;
5190 SvUTF8_off(TARG); /* decontaminate */
5192 do_join(TARG, &PL_sv_no, MARK, SP);
5194 sv_setsv(TARG, SP > MARK ? *SP : find_rundefsv());
5195 if (! SvOK(TARG) && ckWARN(WARN_UNINITIALIZED))
5196 report_uninit(TARG);
5199 up = SvPV_force(TARG, len);
5201 if (DO_UTF8(TARG)) { /* first reverse each character */
5202 U8* s = (U8*)SvPVX(TARG);
5203 const U8* send = (U8*)(s + len);
5205 if (UTF8_IS_INVARIANT(*s)) {
5210 if (!utf8_to_uvchr_buf(s, send, 0))
5214 down = (char*)(s - 1);
5215 /* reverse this character */
5219 *down-- = (char)tmp;
5225 down = SvPVX(TARG) + len - 1;
5229 *down-- = (char)tmp;
5231 (void)SvPOK_only_UTF8(TARG);
5243 register IV limit = POPi; /* note, negative is forever */
5244 SV * const sv = POPs;
5246 register const char *s = SvPV_const(sv, len);
5247 const bool do_utf8 = DO_UTF8(sv);
5248 const char *strend = s + len;
5250 register REGEXP *rx;
5252 register const char *m;
5254 const STRLEN slen = do_utf8 ? utf8_length((U8*)s, (U8*)strend) : (STRLEN)(strend - s);
5255 I32 maxiters = slen + 10;
5256 I32 trailing_empty = 0;
5258 const I32 origlimit = limit;
5261 const I32 gimme = GIMME_V;
5263 const I32 oldsave = PL_savestack_ix;
5264 U32 make_mortal = SVs_TEMP;
5269 Copy(&LvTARGOFF(POPs), &pm, 1, PMOP*);
5274 DIE(aTHX_ "panic: pp_split, pm=%p, s=%p", pm, s);
5277 TAINT_IF(get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET &&
5278 (RX_EXTFLAGS(rx) & (RXf_WHITE | RXf_SKIPWHITE)));
5280 RX_MATCH_UTF8_set(rx, do_utf8);
5283 if (pm->op_pmreplrootu.op_pmtargetoff) {
5284 ary = GvAVn(MUTABLE_GV(PAD_SVl(pm->op_pmreplrootu.op_pmtargetoff)));
5287 if (pm->op_pmreplrootu.op_pmtargetgv) {
5288 ary = GvAVn(pm->op_pmreplrootu.op_pmtargetgv);
5293 if (ary && (gimme != G_ARRAY || (pm->op_pmflags & PMf_ONCE))) {
5299 if ((mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied))) {
5301 XPUSHs(SvTIED_obj(MUTABLE_SV(ary), mg));
5308 for (i = AvFILLp(ary); i >= 0; i--)
5309 AvARRAY(ary)[i] = &PL_sv_undef; /* don't free mere refs */
5311 /* temporarily switch stacks */
5312 SAVESWITCHSTACK(PL_curstack, ary);
5316 base = SP - PL_stack_base;
5318 if (RX_EXTFLAGS(rx) & RXf_SKIPWHITE) {
5320 while (*s == ' ' || is_utf8_space((U8*)s))
5323 else if (get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET) {
5324 while (isSPACE_LC(*s))
5332 if (RX_EXTFLAGS(rx) & RXf_PMf_MULTILINE) {
5336 gimme_scalar = gimme == G_SCALAR && !ary;
5339 limit = maxiters + 2;
5340 if (RX_EXTFLAGS(rx) & RXf_WHITE) {
5343 /* this one uses 'm' and is a negative test */
5345 while (m < strend && !( *m == ' ' || is_utf8_space((U8*)m) )) {
5346 const int t = UTF8SKIP(m);
5347 /* is_utf8_space returns FALSE for malform utf8 */
5354 else if (get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET) {
5355 while (m < strend && !isSPACE_LC(*m))
5358 while (m < strend && !isSPACE(*m))
5371 dstr = newSVpvn_flags(s, m-s,
5372 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5376 /* skip the whitespace found last */
5378 s = m + UTF8SKIP(m);
5382 /* this one uses 's' and is a positive test */
5384 while (s < strend && ( *s == ' ' || is_utf8_space((U8*)s) ))
5387 else if (get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET) {
5388 while (s < strend && isSPACE_LC(*s))
5391 while (s < strend && isSPACE(*s))
5396 else if (RX_EXTFLAGS(rx) & RXf_START_ONLY) {
5398 for (m = s; m < strend && *m != '\n'; m++)
5411 dstr = newSVpvn_flags(s, m-s,
5412 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5418 else if (RX_EXTFLAGS(rx) & RXf_NULL && !(s >= strend)) {
5420 Pre-extend the stack, either the number of bytes or
5421 characters in the string or a limited amount, triggered by:
5423 my ($x, $y) = split //, $str;
5427 if (!gimme_scalar) {
5428 const U32 items = limit - 1;
5437 /* keep track of how many bytes we skip over */
5447 dstr = newSVpvn_flags(m, s-m, SVf_UTF8 | make_mortal);
5460 dstr = newSVpvn(s, 1);
5476 else if (do_utf8 == (RX_UTF8(rx) != 0) &&
5477 (RX_EXTFLAGS(rx) & RXf_USE_INTUIT) && !RX_NPARENS(rx)
5478 && (RX_EXTFLAGS(rx) & RXf_CHECK_ALL)
5479 && !(RX_EXTFLAGS(rx) & RXf_ANCH)) {
5480 const int tail = (RX_EXTFLAGS(rx) & RXf_INTUIT_TAIL);
5481 SV * const csv = CALLREG_INTUIT_STRING(rx);
5483 len = RX_MINLENRET(rx);
5484 if (len == 1 && !RX_UTF8(rx) && !tail) {
5485 const char c = *SvPV_nolen_const(csv);
5487 for (m = s; m < strend && *m != c; m++)
5498 dstr = newSVpvn_flags(s, m-s,
5499 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5502 /* The rx->minlen is in characters but we want to step
5503 * s ahead by bytes. */
5505 s = (char*)utf8_hop((U8*)m, len);
5507 s = m + len; /* Fake \n at the end */
5511 while (s < strend && --limit &&
5512 (m = fbm_instr((unsigned char*)s, (unsigned char*)strend,
5513 csv, multiline ? FBMrf_MULTILINE : 0)) )
5522 dstr = newSVpvn_flags(s, m-s,
5523 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5526 /* The rx->minlen is in characters but we want to step
5527 * s ahead by bytes. */
5529 s = (char*)utf8_hop((U8*)m, len);
5531 s = m + len; /* Fake \n at the end */
5536 maxiters += slen * RX_NPARENS(rx);
5537 while (s < strend && --limit)
5541 rex_return = CALLREGEXEC(rx, (char*)s, (char*)strend, (char*)orig, 1 ,
5544 if (rex_return == 0)
5546 TAINT_IF(RX_MATCH_TAINTED(rx));
5547 if (RX_MATCH_COPIED(rx) && RX_SUBBEG(rx) != orig) {
5550 orig = RX_SUBBEG(rx);
5552 strend = s + (strend - m);
5554 m = RX_OFFS(rx)[0].start + orig;
5563 dstr = newSVpvn_flags(s, m-s,
5564 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5567 if (RX_NPARENS(rx)) {
5569 for (i = 1; i <= (I32)RX_NPARENS(rx); i++) {
5570 s = RX_OFFS(rx)[i].start + orig;
5571 m = RX_OFFS(rx)[i].end + orig;
5573 /* japhy (07/27/01) -- the (m && s) test doesn't catch
5574 parens that didn't match -- they should be set to
5575 undef, not the empty string */
5583 if (m >= orig && s >= orig) {
5584 dstr = newSVpvn_flags(s, m-s,
5585 (do_utf8 ? SVf_UTF8 : 0)
5589 dstr = &PL_sv_undef; /* undef, not "" */
5595 s = RX_OFFS(rx)[0].end + orig;
5599 if (!gimme_scalar) {
5600 iters = (SP - PL_stack_base) - base;
5602 if (iters > maxiters)
5603 DIE(aTHX_ "Split loop");
5605 /* keep field after final delim? */
5606 if (s < strend || (iters && origlimit)) {
5607 if (!gimme_scalar) {
5608 const STRLEN l = strend - s;
5609 dstr = newSVpvn_flags(s, l, (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5614 else if (!origlimit) {
5616 iters -= trailing_empty;
5618 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0)) {
5619 if (TOPs && !make_mortal)
5621 *SP-- = &PL_sv_undef;
5628 LEAVE_SCOPE(oldsave); /* may undo an earlier SWITCHSTACK */
5632 if (SvSMAGICAL(ary)) {
5634 mg_set(MUTABLE_SV(ary));
5637 if (gimme == G_ARRAY) {
5639 Copy(AvARRAY(ary), SP + 1, iters, SV*);
5646 ENTER_with_name("call_PUSH");
5647 call_method("PUSH",G_SCALAR|G_DISCARD);
5648 LEAVE_with_name("call_PUSH");
5650 if (gimme == G_ARRAY) {
5652 /* EXTEND should not be needed - we just popped them */
5654 for (i=0; i < iters; i++) {
5655 SV **svp = av_fetch(ary, i, FALSE);
5656 PUSHs((svp) ? *svp : &PL_sv_undef);
5663 if (gimme == G_ARRAY)
5675 SV *const sv = PAD_SVl(PL_op->op_targ);
5677 if (SvPADSTALE(sv)) {
5680 RETURNOP(cLOGOP->op_other);
5682 RETURNOP(cLOGOP->op_next);
5692 if (SvTYPE(retsv) == SVt_PVAV || SvTYPE(retsv) == SVt_PVHV
5693 || SvTYPE(retsv) == SVt_PVCV) {
5694 retsv = refto(retsv);
5701 PP(unimplemented_op)
5704 const Optype op_type = PL_op->op_type;
5705 /* Using OP_NAME() isn't going to be helpful here. Firstly, it doesn't cope
5706 with out of range op numbers - it only "special" cases op_custom.
5707 Secondly, as the three ops we "panic" on are padmy, mapstart and custom,
5708 if we get here for a custom op then that means that the custom op didn't
5709 have an implementation. Given that OP_NAME() looks up the custom op
5710 by its pp_addr, likely it will return NULL, unless someone (unhelpfully)
5711 registers &PL_unimplemented_op as the address of their custom op.
5712 NULL doesn't generate a useful error message. "custom" does. */
5713 const char *const name = op_type >= OP_max
5714 ? "[out of range]" : PL_op_name[PL_op->op_type];
5715 if(OP_IS_SOCKET(op_type))
5716 DIE(aTHX_ PL_no_sock_func, name);
5717 DIE(aTHX_ "panic: unimplemented op %s (#%d) called", name, op_type);
5724 HV * const hv = (HV*)POPs;
5726 if (SvTYPE(hv) != SVt_PVHV) { XPUSHs(&PL_sv_no); RETURN; }
5728 if (SvRMAGICAL(hv)) {
5729 MAGIC * const mg = mg_find((SV*)hv, PERL_MAGIC_tied);
5731 XPUSHs(magic_scalarpack(hv, mg));
5736 XPUSHs(boolSV(HvUSEDKEYS(hv) != 0));
5740 /* For sorting out arguments passed to a &CORE:: subroutine */
5744 int opnum = SvIOK(cSVOP_sv) ? (int)SvUV(cSVOP_sv) : 0;
5745 int defgv = PL_opargs[opnum] & OA_DEFGV ||opnum==OP_GLOB, whicharg = 0;
5746 AV * const at_ = GvAV(PL_defgv);
5747 SV **svp = at_ ? AvARRAY(at_) : NULL;
5748 I32 minargs = 0, maxargs = 0, numargs = at_ ? AvFILLp(at_)+1 : 0;
5749 I32 oa = opnum ? PL_opargs[opnum] >> OASHIFT : 0;
5750 bool seen_question = 0;
5751 const char *err = NULL;
5752 const bool pushmark = PL_op->op_private & OPpCOREARGS_PUSHMARK;
5754 /* Count how many args there are first, to get some idea how far to
5755 extend the stack. */
5757 if ((oa & 7) == OA_LIST) { maxargs = I32_MAX; break; }
5759 if (oa & OA_OPTIONAL) seen_question = 1;
5760 if (!seen_question) minargs++;
5764 if(numargs < minargs) err = "Not enough";
5765 else if(numargs > maxargs) err = "Too many";
5767 /* diag_listed_as: Too many arguments for %s */
5769 "%s arguments for %s", err,
5770 opnum ? PL_op_desc[opnum] : SvPV_nolen_const(cSVOP_sv)
5773 /* Reset the stack pointer. Without this, we end up returning our own
5774 arguments in list context, in addition to the values we are supposed
5775 to return. nextstate usually does this on sub entry, but we need
5776 to run the next op with the caller's hints, so we cannot have a
5778 SP = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;
5780 if(!maxargs) RETURN;
5782 /* We do this here, rather than with a separate pushmark op, as it has
5783 to come in between two things this function does (stack reset and
5784 arg pushing). This seems the easiest way to do it. */
5787 (void)Perl_pp_pushmark(aTHX);
5790 EXTEND(SP, maxargs == I32_MAX ? numargs : maxargs);
5791 PUTBACK; /* The code below can die in various places. */
5793 oa = PL_opargs[opnum] >> OASHIFT;
5794 for (; oa&&(numargs||!pushmark); (void)(numargs&&(++svp,--numargs))) {
5799 if (!numargs && defgv && whicharg == minargs + 1) {
5800 PUSHs(find_rundefsv2(
5801 find_runcv_where(FIND_RUNCV_level_eq, (void *)1, NULL),
5802 cxstack[cxstack_ix].blk_oldcop->cop_seq
5805 else PUSHs(numargs ? svp && *svp ? *svp : &PL_sv_undef : NULL);
5809 PUSHs(svp && *svp ? *svp : &PL_sv_undef);
5814 if (!svp || !*svp || !SvROK(*svp)
5815 || SvTYPE(SvRV(*svp)) != SVt_PVHV)
5817 /* diag_listed_as: Type of arg %d to &CORE::%s must be %s*/
5818 "Type of arg %d to &CORE::%s must be hash reference",
5819 whicharg, OP_DESC(PL_op->op_next)
5824 if (!numargs) PUSHs(NULL);
5825 else if(svp && *svp && SvROK(*svp) && isGV_with_GP(SvRV(*svp)))
5826 /* no magic here, as the prototype will have added an extra
5827 refgen and we just want what was there before that */
5830 const bool constr = PL_op->op_private & whicharg;
5832 svp && *svp ? *svp : &PL_sv_undef,
5833 constr, CopHINTS_get(PL_curcop) & HINT_STRICT_REFS,
5839 if (!numargs) goto try_defsv;
5841 const bool wantscalar =
5842 PL_op->op_private & OPpCOREARGS_SCALARMOD;
5843 if (!svp || !*svp || !SvROK(*svp)
5844 /* We have to permit globrefs even for the \$ proto, as
5845 *foo is indistinguishable from ${\*foo}, and the proto-
5846 type permits the latter. */
5847 || SvTYPE(SvRV(*svp)) > (
5848 wantscalar ? SVt_PVLV
5849 : opnum == OP_LOCK || opnum == OP_UNDEF
5855 /* diag_listed_as: Type of arg %d to &CORE::%s must be %s*/
5856 "Type of arg %d to &CORE::%s must be %s",
5857 whicharg, PL_op_name[opnum],
5859 ? "scalar reference"
5860 : opnum == OP_LOCK || opnum == OP_UNDEF
5861 ? "reference to one of [$@%&*]"
5862 : "reference to one of [$@%*]"
5865 if (opnum == OP_UNDEF && SvRV(*svp) == (SV *)PL_defgv
5866 && cxstack[cxstack_ix].cx_type & CXp_HASARGS) {
5867 /* Undo @_ localisation, so that sub exit does not undo
5868 part of our undeffing. */
5869 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
5871 cx->cx_type &= ~ CXp_HASARGS;
5872 assert(!AvREAL(cx->blk_sub.argarray));
5877 DIE(aTHX_ "panic: unknown OA_*: %x", (unsigned)(oa&7));
5889 if (PL_op->op_private & OPpOFFBYONE) {
5890 cv = find_runcv_where(FIND_RUNCV_level_eq, (void *)1, NULL);
5892 else cv = find_runcv(NULL);
5893 XPUSHs(CvEVAL(cv) ? &PL_sv_undef : sv_2mortal(newRV((SV *)cv)));
5900 * c-indentation-style: bsd
5902 * indent-tabs-mode: nil
5905 * ex: set ts=8 sts=4 sw=4 et: