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 Perl_croak(aTHX_ "Can't return array to lvalue scalar context");
84 if (gimme == G_ARRAY) {
85 const I32 maxarg = AvFILL(MUTABLE_AV(TARG)) + 1;
87 if (SvMAGICAL(TARG)) {
89 for (i=0; i < (U32)maxarg; i++) {
90 SV * const * const svp = av_fetch(MUTABLE_AV(TARG), i, FALSE);
91 SP[i+1] = (svp) ? *svp : &PL_sv_undef;
95 Copy(AvARRAY((const AV *)TARG), SP+1, maxarg, SV*);
99 else if (gimme == G_SCALAR) {
100 SV* const sv = sv_newmortal();
101 const I32 maxarg = AvFILL(MUTABLE_AV(TARG)) + 1;
102 sv_setiv(sv, maxarg);
113 assert(SvTYPE(TARG) == SVt_PVHV);
115 if (PL_op->op_private & OPpLVAL_INTRO)
116 if (!(PL_op->op_private & OPpPAD_STATE))
117 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
118 if (PL_op->op_flags & OPf_REF)
120 else if (PL_op->op_private & OPpMAYBE_LVSUB) {
121 const I32 flags = is_lvalue_sub();
122 if (flags && !(flags & OPpENTERSUB_INARGS)) {
123 if (GIMME == G_SCALAR)
124 Perl_croak(aTHX_ "Can't return hash to lvalue scalar context");
129 if (gimme == G_ARRAY) {
130 RETURNOP(Perl_do_kv(aTHX));
132 else if (gimme == G_SCALAR) {
133 SV* const sv = Perl_hv_scalar(aTHX_ MUTABLE_HV(TARG));
141 static const char S_no_symref_sv[] =
142 "Can't use string (\"%" SVf32 "\"%s) as %s ref while \"strict refs\" in use";
148 if (!isGV(sv) || SvFAKE(sv)) SvGETMAGIC(sv);
152 sv = amagic_deref_call(sv, to_gv_amg);
156 if (SvTYPE(sv) == SVt_PVIO) {
157 GV * const gv = MUTABLE_GV(sv_newmortal());
158 gv_init(gv, 0, "", 0, 0);
159 GvIOp(gv) = MUTABLE_IO(sv);
160 SvREFCNT_inc_void_NN(sv);
163 else if (!isGV_with_GP(sv))
164 DIE(aTHX_ "Not a GLOB reference");
167 if (!isGV_with_GP(sv)) {
168 if (!SvOK(sv) && sv != &PL_sv_undef) {
169 /* If this is a 'my' scalar and flag is set then vivify
173 Perl_croak_no_modify(aTHX);
174 if (PL_op->op_private & OPpDEREF) {
176 if (cUNOP->op_targ) {
178 SV * const namesv = PAD_SV(cUNOP->op_targ);
179 const char * const name = SvPV(namesv, len);
180 gv = MUTABLE_GV(newSV(0));
181 gv_init(gv, CopSTASH(PL_curcop), name, len, 0);
184 const char * const name = CopSTASHPV(PL_curcop);
187 prepare_SV_for_RV(sv);
188 SvRV_set(sv, MUTABLE_SV(gv));
193 if (PL_op->op_flags & OPf_REF ||
194 PL_op->op_private & HINT_STRICT_REFS)
195 DIE(aTHX_ PL_no_usym, "a symbol");
196 if (ckWARN(WARN_UNINITIALIZED))
200 if ((PL_op->op_flags & OPf_SPECIAL) &&
201 !(PL_op->op_flags & OPf_MOD))
203 SV * const temp = MUTABLE_SV(gv_fetchsv(sv, 0, SVt_PVGV));
205 && (!is_gv_magical_sv(sv,0)
206 || !(sv = MUTABLE_SV(gv_fetchsv(sv, GV_ADD,
213 if (PL_op->op_private & HINT_STRICT_REFS)
214 DIE(aTHX_ S_no_symref_sv, sv, (SvPOK(sv) && SvCUR(sv)>32 ? "..." : ""), "a symbol");
215 if ((PL_op->op_private & (OPpLVAL_INTRO|OPpDONT_INIT_GV))
216 == OPpDONT_INIT_GV) {
217 /* We are the target of a coderef assignment. Return
218 the scalar unchanged, and let pp_sasssign deal with
222 sv = MUTABLE_SV(gv_fetchsv(sv, GV_ADD, SVt_PVGV));
224 /* FAKE globs in the symbol table cause weird bugs (#77810) */
225 if (sv) SvFAKE_off(sv);
228 if (sv && SvFAKE(sv)) {
229 SV *newsv = sv_newmortal();
230 sv_setsv_flags(newsv, sv, 0);
234 if (PL_op->op_private & OPpLVAL_INTRO)
235 save_gp(MUTABLE_GV(sv), !(PL_op->op_flags & OPf_SPECIAL));
240 /* Helper function for pp_rv2sv and pp_rv2av */
242 Perl_softref2xv(pTHX_ SV *const sv, const char *const what,
243 const svtype type, SV ***spp)
248 PERL_ARGS_ASSERT_SOFTREF2XV;
250 if (PL_op->op_private & HINT_STRICT_REFS) {
252 Perl_die(aTHX_ S_no_symref_sv, sv, (SvPOK(sv) && SvCUR(sv)>32 ? "..." : ""), what);
254 Perl_die(aTHX_ PL_no_usym, what);
258 PL_op->op_flags & OPf_REF &&
259 PL_op->op_next->op_type != OP_BOOLKEYS
261 Perl_die(aTHX_ PL_no_usym, what);
262 if (ckWARN(WARN_UNINITIALIZED))
264 if (type != SVt_PV && GIMME_V == G_ARRAY) {
268 **spp = &PL_sv_undef;
271 if ((PL_op->op_flags & OPf_SPECIAL) &&
272 !(PL_op->op_flags & OPf_MOD))
274 gv = gv_fetchsv(sv, 0, type);
276 && (!is_gv_magical_sv(sv,0)
277 || !(gv = gv_fetchsv(sv, GV_ADD, type))))
279 **spp = &PL_sv_undef;
284 gv = gv_fetchsv(sv, GV_ADD, type);
294 if (!(PL_op->op_private & OPpDEREFed))
298 sv = amagic_deref_call(sv, to_sv_amg);
303 switch (SvTYPE(sv)) {
309 DIE(aTHX_ "Not a SCALAR reference");
316 if (!isGV_with_GP(gv)) {
317 gv = Perl_softref2xv(aTHX_ sv, "a SCALAR", SVt_PV, &sp);
323 if (PL_op->op_flags & OPf_MOD) {
324 if (PL_op->op_private & OPpLVAL_INTRO) {
325 if (cUNOP->op_first->op_type == OP_NULL)
326 sv = save_scalar(MUTABLE_GV(TOPs));
328 sv = save_scalar(gv);
330 Perl_croak(aTHX_ "%s", PL_no_localize_ref);
332 else if (PL_op->op_private & OPpDEREF)
333 vivify_ref(sv, PL_op->op_private & OPpDEREF);
342 AV * const av = MUTABLE_AV(TOPs);
343 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
345 SV ** const sv = Perl_av_arylen_p(aTHX_ MUTABLE_AV(av));
347 *sv = newSV_type(SVt_PVMG);
348 sv_magic(*sv, MUTABLE_SV(av), PERL_MAGIC_arylen, NULL, 0);
352 SETs(sv_2mortal(newSViv(
353 AvFILL(MUTABLE_AV(av)) + CopARYBASE_get(PL_curcop)
363 if (PL_op->op_flags & OPf_MOD || LVRET) {
364 SV * const ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
365 sv_magic(ret, NULL, PERL_MAGIC_pos, NULL, 0);
367 LvTARG(ret) = SvREFCNT_inc_simple(sv);
368 PUSHs(ret); /* no SvSETMAGIC */
372 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
373 const MAGIC * const mg = mg_find(sv, PERL_MAGIC_regex_global);
374 if (mg && mg->mg_len >= 0) {
379 PUSHi(i + CopARYBASE_get(PL_curcop));
392 const I32 flags = (PL_op->op_flags & OPf_SPECIAL)
394 : ((PL_op->op_private & (OPpLVAL_INTRO|OPpMAY_RETURN_CONSTANT)) == OPpMAY_RETURN_CONSTANT)
397 /* We usually try to add a non-existent subroutine in case of AUTOLOAD. */
398 /* (But not in defined().) */
400 CV *cv = sv_2cv(TOPs, &stash_unused, &gv, flags);
403 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
404 if ((PL_op->op_private & OPpLVAL_INTRO)) {
405 if (gv && GvCV(gv) == cv && (gv = gv_autoload4(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv), FALSE)))
408 DIE(aTHX_ "Can't modify non-lvalue subroutine call");
411 else if ((flags == (GV_ADD|GV_NOEXPAND)) && gv && SvROK(gv)) {
415 cv = MUTABLE_CV(&PL_sv_undef);
416 SETs(MUTABLE_SV(cv));
426 SV *ret = &PL_sv_undef;
428 if (SvPOK(TOPs) && SvCUR(TOPs) >= 7) {
429 const char * s = SvPVX_const(TOPs);
430 if (strnEQ(s, "CORE::", 6)) {
431 const int code = keyword(s + 6, SvCUR(TOPs) - 6, 1);
432 if (code < 0) { /* Overridable. */
433 #define MAX_ARGS_OP ((sizeof(I32) - 1) * 2)
434 int i = 0, n = 0, seen_question = 0, defgv = 0;
436 char str[ MAX_ARGS_OP * 2 + 2 ]; /* One ';', one '\0' */
438 if (code == -KEY_chop || code == -KEY_chomp
439 || code == -KEY_exec || code == -KEY_system)
441 if (code == -KEY_mkdir) {
442 ret = newSVpvs_flags("_;$", SVs_TEMP);
445 if (code == -KEY_keys || code == -KEY_values || code == -KEY_each) {
446 ret = newSVpvs_flags("+", SVs_TEMP);
449 if (code == -KEY_push || code == -KEY_unshift) {
450 ret = newSVpvs_flags("+@", SVs_TEMP);
453 if (code == -KEY_pop || code == -KEY_shift) {
454 ret = newSVpvs_flags(";+", SVs_TEMP);
457 if (code == -KEY_splice) {
458 ret = newSVpvs_flags("+;$$@", SVs_TEMP);
461 if (code == -KEY_tied || code == -KEY_untie) {
462 ret = newSVpvs_flags("\\[$@%*]", SVs_TEMP);
465 if (code == -KEY_tie) {
466 ret = newSVpvs_flags("\\[$@%*]$@", SVs_TEMP);
469 if (code == -KEY_readpipe) {
470 s = "CORE::backtick";
472 while (i < MAXO) { /* The slow way. */
473 if (strEQ(s + 6, PL_op_name[i])
474 || strEQ(s + 6, PL_op_desc[i]))
480 goto nonesuch; /* Should not happen... */
482 defgv = PL_opargs[i] & OA_DEFGV;
483 oa = PL_opargs[i] >> OASHIFT;
485 if (oa & OA_OPTIONAL && !seen_question && !defgv) {
489 if ((oa & (OA_OPTIONAL - 1)) >= OA_AVREF
490 && (oa & (OA_OPTIONAL - 1)) <= OA_SCALARREF
491 /* But globs are already references (kinda) */
492 && (oa & (OA_OPTIONAL - 1)) != OA_FILEREF
496 str[n++] = ("?$@@%&*$")[oa & (OA_OPTIONAL - 1)];
499 if (defgv && str[n - 1] == '$')
502 ret = newSVpvn_flags(str, n - 1, SVs_TEMP);
504 else if (code) /* Non-Overridable */
506 else { /* None such */
508 DIE(aTHX_ "Can't find an opnumber for \"%s\"", s+6);
512 cv = sv_2cv(TOPs, &stash, &gv, 0);
514 ret = newSVpvn_flags(SvPVX_const(cv), SvCUR(cv), SVs_TEMP);
523 CV *cv = MUTABLE_CV(PAD_SV(PL_op->op_targ));
525 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
527 PUSHs(MUTABLE_SV(cv));
541 if (GIMME != G_ARRAY) {
545 *MARK = &PL_sv_undef;
546 *MARK = refto(*MARK);
550 EXTEND_MORTAL(SP - MARK);
552 *MARK = refto(*MARK);
557 S_refto(pTHX_ SV *sv)
562 PERL_ARGS_ASSERT_REFTO;
564 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
567 if (!(sv = LvTARG(sv)))
570 SvREFCNT_inc_void_NN(sv);
572 else if (SvTYPE(sv) == SVt_PVAV) {
573 if (!AvREAL((const AV *)sv) && AvREIFY((const AV *)sv))
574 av_reify(MUTABLE_AV(sv));
576 SvREFCNT_inc_void_NN(sv);
578 else if (SvPADTMP(sv) && !IS_PADGV(sv))
582 SvREFCNT_inc_void_NN(sv);
585 sv_upgrade(rv, SVt_IV);
595 SV * const sv = POPs;
600 if (!sv || !SvROK(sv))
603 pv = sv_reftype(SvRV(sv),TRUE);
604 PUSHp(pv, strlen(pv));
614 stash = CopSTASH(PL_curcop);
616 SV * const ssv = POPs;
620 if (ssv && !SvGMAGICAL(ssv) && !SvAMAGIC(ssv) && SvROK(ssv))
621 Perl_croak(aTHX_ "Attempt to bless into a reference");
622 ptr = SvPV_const(ssv,len);
624 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
625 "Explicit blessing to '' (assuming package main)");
626 stash = gv_stashpvn(ptr, len, GV_ADD);
629 (void)sv_bless(TOPs, stash);
638 const char * const elem = SvPV_nolen_const(sv);
639 GV * const gv = MUTABLE_GV(POPs);
644 /* elem will always be NUL terminated. */
645 const char * const second_letter = elem + 1;
648 if (strEQ(second_letter, "RRAY"))
649 tmpRef = MUTABLE_SV(GvAV(gv));
652 if (strEQ(second_letter, "ODE"))
653 tmpRef = MUTABLE_SV(GvCVu(gv));
656 if (strEQ(second_letter, "ILEHANDLE")) {
657 /* finally deprecated in 5.8.0 */
658 deprecate("*glob{FILEHANDLE}");
659 tmpRef = MUTABLE_SV(GvIOp(gv));
662 if (strEQ(second_letter, "ORMAT"))
663 tmpRef = MUTABLE_SV(GvFORM(gv));
666 if (strEQ(second_letter, "LOB"))
667 tmpRef = MUTABLE_SV(gv);
670 if (strEQ(second_letter, "ASH"))
671 tmpRef = MUTABLE_SV(GvHV(gv));
674 if (*second_letter == 'O' && !elem[2])
675 tmpRef = MUTABLE_SV(GvIOp(gv));
678 if (strEQ(second_letter, "AME"))
679 sv = newSVhek(GvNAME_HEK(gv));
682 if (strEQ(second_letter, "ACKAGE")) {
683 const HV * const stash = GvSTASH(gv);
684 const HEK * const hek = stash ? HvNAME_HEK(stash) : NULL;
685 sv = hek ? newSVhek(hek) : newSVpvs("__ANON__");
689 if (strEQ(second_letter, "CALAR"))
704 /* Pattern matching */
709 register unsigned char *s;
713 MAGIC *mg = SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_study) : NULL;
715 if (mg && SvSCREAM(sv))
718 s = (unsigned char*)(SvPV(sv, len));
719 if (len == 0 || len > I32_MAX || !SvPOK(sv) || SvUTF8(sv) || SvVALID(sv)) {
720 /* No point in studying a zero length string, and not safe to study
721 anything that doesn't appear to be a simple scalar (and hence might
722 change between now and when the regexp engine runs without our set
723 magic ever running) such as a reference to an object with overloaded
724 stringification. Also refuse to study an FBM scalar, as this gives
725 more flexibility in SV flag usage. No real-world code would ever
726 end up studying an FBM scalar, so this isn't a real pessimisation.
731 Newx(sfirst, 256 + len, U32);
734 DIE(aTHX_ "do_study: out of memory");
738 mg = sv_magicext(sv, NULL, PERL_MAGIC_study, &PL_vtbl_regexp, NULL, 0);
739 mg->mg_ptr = (char *) sfirst;
740 mg->mg_len = (256 + len) * sizeof(U32);
742 snext = sfirst + 256;
743 memset(sfirst, ~0, 256 * sizeof(U32));
746 const U8 ch = s[len];
747 snext[len] = sfirst[ch];
759 if (PL_op->op_flags & OPf_STACKED)
761 else if (PL_op->op_private & OPpTARGET_MY)
767 TARG = sv_newmortal();
768 if(PL_op->op_type == OP_TRANSR) {
769 SV * const newsv = newSVsv(sv);
773 else PUSHi(do_trans(sv));
777 /* Lvalue operators. */
780 S_do_chomp(pTHX_ SV *retval, SV *sv, bool chomping)
786 PERL_ARGS_ASSERT_DO_CHOMP;
788 if (chomping && (RsSNARF(PL_rs) || RsRECORD(PL_rs)))
790 if (SvTYPE(sv) == SVt_PVAV) {
792 AV *const av = MUTABLE_AV(sv);
793 const I32 max = AvFILL(av);
795 for (i = 0; i <= max; i++) {
796 sv = MUTABLE_SV(av_fetch(av, i, FALSE));
797 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
798 do_chomp(retval, sv, chomping);
802 else if (SvTYPE(sv) == SVt_PVHV) {
803 HV* const hv = MUTABLE_HV(sv);
805 (void)hv_iterinit(hv);
806 while ((entry = hv_iternext(hv)))
807 do_chomp(retval, hv_iterval(hv,entry), chomping);
810 else if (SvREADONLY(sv)) {
812 /* SV is copy-on-write */
813 sv_force_normal_flags(sv, 0);
816 Perl_croak_no_modify(aTHX);
821 /* XXX, here sv is utf8-ized as a side-effect!
822 If encoding.pm is used properly, almost string-generating
823 operations, including literal strings, chr(), input data, etc.
824 should have been utf8-ized already, right?
826 sv_recode_to_utf8(sv, PL_encoding);
832 char *temp_buffer = NULL;
841 while (len && s[-1] == '\n') {
848 STRLEN rslen, rs_charlen;
849 const char *rsptr = SvPV_const(PL_rs, rslen);
851 rs_charlen = SvUTF8(PL_rs)
855 if (SvUTF8(PL_rs) != SvUTF8(sv)) {
856 /* Assumption is that rs is shorter than the scalar. */
858 /* RS is utf8, scalar is 8 bit. */
860 temp_buffer = (char*)bytes_from_utf8((U8*)rsptr,
863 /* Cannot downgrade, therefore cannot possibly match
865 assert (temp_buffer == rsptr);
871 else if (PL_encoding) {
872 /* RS is 8 bit, encoding.pm is used.
873 * Do not recode PL_rs as a side-effect. */
874 svrecode = newSVpvn(rsptr, rslen);
875 sv_recode_to_utf8(svrecode, PL_encoding);
876 rsptr = SvPV_const(svrecode, rslen);
877 rs_charlen = sv_len_utf8(svrecode);
880 /* RS is 8 bit, scalar is utf8. */
881 temp_buffer = (char*)bytes_to_utf8((U8*)rsptr, &rslen);
895 if (memNE(s, rsptr, rslen))
897 SvIVX(retval) += rs_charlen;
900 s = SvPV_force_nolen(sv);
908 SvREFCNT_dec(svrecode);
910 Safefree(temp_buffer);
912 if (len && !SvPOK(sv))
913 s = SvPV_force_nomg(sv, len);
916 char * const send = s + len;
917 char * const start = s;
919 while (s > start && UTF8_IS_CONTINUATION(*s))
921 if (is_utf8_string((U8*)s, send - s)) {
922 sv_setpvn(retval, s, send - s);
924 SvCUR_set(sv, s - start);
930 sv_setpvs(retval, "");
934 sv_setpvn(retval, s, 1);
941 sv_setpvs(retval, "");
949 const bool chomping = PL_op->op_type == OP_SCHOMP;
953 do_chomp(TARG, TOPs, chomping);
960 dVAR; dSP; dMARK; dTARGET; dORIGMARK;
961 const bool chomping = PL_op->op_type == OP_CHOMP;
966 do_chomp(TARG, *++MARK, chomping);
977 if (!PL_op->op_private) {
986 SV_CHECK_THINKFIRST_COW_DROP(sv);
988 switch (SvTYPE(sv)) {
992 av_undef(MUTABLE_AV(sv));
995 hv_undef(MUTABLE_HV(sv));
998 if (cv_const_sv((const CV *)sv))
999 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Constant subroutine %s undefined",
1000 CvANON((const CV *)sv) ? "(anonymous)"
1001 : GvENAME(CvGV((const CV *)sv)));
1005 /* let user-undef'd sub keep its identity */
1006 GV* const gv = CvGV((const CV *)sv);
1007 cv_undef(MUTABLE_CV(sv));
1008 CvGV_set(MUTABLE_CV(sv), gv);
1013 SvSetMagicSV(sv, &PL_sv_undef);
1016 else if (isGV_with_GP(sv)) {
1020 /* undef *Pkg::meth_name ... */
1022 = GvCVu((const GV *)sv) && (stash = GvSTASH((const GV *)sv))
1023 && HvENAME_get(stash);
1025 if((stash = GvHV((const GV *)sv))) {
1026 if(HvENAME_get(stash))
1027 SvREFCNT_inc_simple_void_NN(sv_2mortal((SV *)stash));
1031 gp_free(MUTABLE_GV(sv));
1033 GvGP_set(sv, gp_ref(gp));
1034 GvSV(sv) = newSV(0);
1035 GvLINE(sv) = CopLINE(PL_curcop);
1036 GvEGV(sv) = MUTABLE_GV(sv);
1040 mro_package_moved(NULL, stash, (const GV *)sv, 0);
1042 /* undef *Foo::ISA */
1043 if( strEQ(GvNAME((const GV *)sv), "ISA")
1044 && (stash = GvSTASH((const GV *)sv))
1045 && (method_changed || HvENAME(stash)) )
1046 mro_isa_changed_in(stash);
1047 else if(method_changed)
1048 mro_method_changed_in(
1049 GvSTASH((const GV *)sv)
1056 if (SvTYPE(sv) >= SVt_PV && SvPVX_const(sv) && SvLEN(sv)) {
1071 if (SvTYPE(TOPs) >= SVt_PVAV || isGV_with_GP(TOPs))
1072 Perl_croak_no_modify(aTHX);
1073 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
1074 && SvIVX(TOPs) != IV_MIN)
1076 SvIV_set(TOPs, SvIVX(TOPs) - 1);
1077 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
1088 if (SvTYPE(TOPs) >= SVt_PVAV || isGV_with_GP(TOPs))
1089 Perl_croak_no_modify(aTHX);
1091 TARG = sv_newmortal();
1092 sv_setsv(TARG, TOPs);
1093 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
1094 && SvIVX(TOPs) != IV_MAX)
1096 SvIV_set(TOPs, SvIVX(TOPs) + 1);
1097 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
1102 /* special case for undef: see thread at 2003-03/msg00536.html in archive */
1112 if (SvTYPE(TOPs) >= SVt_PVAV || isGV_with_GP(TOPs))
1113 Perl_croak_no_modify(aTHX);
1115 TARG = sv_newmortal();
1116 sv_setsv(TARG, TOPs);
1117 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
1118 && SvIVX(TOPs) != IV_MIN)
1120 SvIV_set(TOPs, SvIVX(TOPs) - 1);
1121 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
1130 /* Ordinary operators. */
1134 dVAR; dSP; dATARGET; SV *svl, *svr;
1135 #ifdef PERL_PRESERVE_IVUV
1138 tryAMAGICbin_MG(pow_amg, AMGf_assign|AMGf_numeric);
1141 #ifdef PERL_PRESERVE_IVUV
1142 /* For integer to integer power, we do the calculation by hand wherever
1143 we're sure it is safe; otherwise we call pow() and try to convert to
1144 integer afterwards. */
1146 SvIV_please_nomg(svr);
1148 SvIV_please_nomg(svl);
1157 const IV iv = SvIVX(svr);
1161 goto float_it; /* Can't do negative powers this way. */
1165 baseuok = SvUOK(svl);
1167 baseuv = SvUVX(svl);
1169 const IV iv = SvIVX(svl);
1172 baseuok = TRUE; /* effectively it's a UV now */
1174 baseuv = -iv; /* abs, baseuok == false records sign */
1177 /* now we have integer ** positive integer. */
1180 /* foo & (foo - 1) is zero only for a power of 2. */
1181 if (!(baseuv & (baseuv - 1))) {
1182 /* We are raising power-of-2 to a positive integer.
1183 The logic here will work for any base (even non-integer
1184 bases) but it can be less accurate than
1185 pow (base,power) or exp (power * log (base)) when the
1186 intermediate values start to spill out of the mantissa.
1187 With powers of 2 we know this can't happen.
1188 And powers of 2 are the favourite thing for perl
1189 programmers to notice ** not doing what they mean. */
1191 NV base = baseuok ? baseuv : -(NV)baseuv;
1196 while (power >>= 1) {
1204 SvIV_please_nomg(svr);
1207 register unsigned int highbit = 8 * sizeof(UV);
1208 register unsigned int diff = 8 * sizeof(UV);
1209 while (diff >>= 1) {
1211 if (baseuv >> highbit) {
1215 /* we now have baseuv < 2 ** highbit */
1216 if (power * highbit <= 8 * sizeof(UV)) {
1217 /* result will definitely fit in UV, so use UV math
1218 on same algorithm as above */
1219 register UV result = 1;
1220 register UV base = baseuv;
1221 const bool odd_power = cBOOL(power & 1);
1225 while (power >>= 1) {
1232 if (baseuok || !odd_power)
1233 /* answer is positive */
1235 else if (result <= (UV)IV_MAX)
1236 /* answer negative, fits in IV */
1237 SETi( -(IV)result );
1238 else if (result == (UV)IV_MIN)
1239 /* 2's complement assumption: special case IV_MIN */
1242 /* answer negative, doesn't fit */
1243 SETn( -(NV)result );
1253 NV right = SvNV_nomg(svr);
1254 NV left = SvNV_nomg(svl);
1257 #if defined(USE_LONG_DOUBLE) && defined(HAS_AIX_POWL_NEG_BASE_BUG)
1259 We are building perl with long double support and are on an AIX OS
1260 afflicted with a powl() function that wrongly returns NaNQ for any
1261 negative base. This was reported to IBM as PMR #23047-379 on
1262 03/06/2006. The problem exists in at least the following versions
1263 of AIX and the libm fileset, and no doubt others as well:
1265 AIX 4.3.3-ML10 bos.adt.libm 4.3.3.50
1266 AIX 5.1.0-ML04 bos.adt.libm 5.1.0.29
1267 AIX 5.2.0 bos.adt.libm 5.2.0.85
1269 So, until IBM fixes powl(), we provide the following workaround to
1270 handle the problem ourselves. Our logic is as follows: for
1271 negative bases (left), we use fmod(right, 2) to check if the
1272 exponent is an odd or even integer:
1274 - if odd, powl(left, right) == -powl(-left, right)
1275 - if even, powl(left, right) == powl(-left, right)
1277 If the exponent is not an integer, the result is rightly NaNQ, so
1278 we just return that (as NV_NAN).
1282 NV mod2 = Perl_fmod( right, 2.0 );
1283 if (mod2 == 1.0 || mod2 == -1.0) { /* odd integer */
1284 SETn( -Perl_pow( -left, right) );
1285 } else if (mod2 == 0.0) { /* even integer */
1286 SETn( Perl_pow( -left, right) );
1287 } else { /* fractional power */
1291 SETn( Perl_pow( left, right) );
1294 SETn( Perl_pow( left, right) );
1295 #endif /* HAS_AIX_POWL_NEG_BASE_BUG */
1297 #ifdef PERL_PRESERVE_IVUV
1299 SvIV_please_nomg(svr);
1307 dVAR; dSP; dATARGET; SV *svl, *svr;
1308 tryAMAGICbin_MG(mult_amg, AMGf_assign|AMGf_numeric);
1311 #ifdef PERL_PRESERVE_IVUV
1312 SvIV_please_nomg(svr);
1314 /* Unless the left argument is integer in range we are going to have to
1315 use NV maths. Hence only attempt to coerce the right argument if
1316 we know the left is integer. */
1317 /* Left operand is defined, so is it IV? */
1318 SvIV_please_nomg(svl);
1320 bool auvok = SvUOK(svl);
1321 bool buvok = SvUOK(svr);
1322 const UV topmask = (~ (UV)0) << (4 * sizeof (UV));
1323 const UV botmask = ~((~ (UV)0) << (4 * sizeof (UV)));
1332 const IV aiv = SvIVX(svl);
1335 auvok = TRUE; /* effectively it's a UV now */
1337 alow = -aiv; /* abs, auvok == false records sign */
1343 const IV biv = SvIVX(svr);
1346 buvok = TRUE; /* effectively it's a UV now */
1348 blow = -biv; /* abs, buvok == false records sign */
1352 /* If this does sign extension on unsigned it's time for plan B */
1353 ahigh = alow >> (4 * sizeof (UV));
1355 bhigh = blow >> (4 * sizeof (UV));
1357 if (ahigh && bhigh) {
1359 /* eg 32 bit is at least 0x10000 * 0x10000 == 0x100000000
1360 which is overflow. Drop to NVs below. */
1361 } else if (!ahigh && !bhigh) {
1362 /* eg 32 bit is at most 0xFFFF * 0xFFFF == 0xFFFE0001
1363 so the unsigned multiply cannot overflow. */
1364 const UV product = alow * blow;
1365 if (auvok == buvok) {
1366 /* -ve * -ve or +ve * +ve gives a +ve result. */
1370 } else if (product <= (UV)IV_MIN) {
1371 /* 2s complement assumption that (UV)-IV_MIN is correct. */
1372 /* -ve result, which could overflow an IV */
1374 SETi( -(IV)product );
1376 } /* else drop to NVs below. */
1378 /* One operand is large, 1 small */
1381 /* swap the operands */
1383 bhigh = blow; /* bhigh now the temp var for the swap */
1387 /* now, ((ahigh * blow) << half_UV_len) + (alow * blow)
1388 multiplies can't overflow. shift can, add can, -ve can. */
1389 product_middle = ahigh * blow;
1390 if (!(product_middle & topmask)) {
1391 /* OK, (ahigh * blow) won't lose bits when we shift it. */
1393 product_middle <<= (4 * sizeof (UV));
1394 product_low = alow * blow;
1396 /* as for pp_add, UV + something mustn't get smaller.
1397 IIRC ANSI mandates this wrapping *behaviour* for
1398 unsigned whatever the actual representation*/
1399 product_low += product_middle;
1400 if (product_low >= product_middle) {
1401 /* didn't overflow */
1402 if (auvok == buvok) {
1403 /* -ve * -ve or +ve * +ve gives a +ve result. */
1405 SETu( product_low );
1407 } else if (product_low <= (UV)IV_MIN) {
1408 /* 2s complement assumption again */
1409 /* -ve result, which could overflow an IV */
1411 SETi( -(IV)product_low );
1413 } /* else drop to NVs below. */
1415 } /* product_middle too large */
1416 } /* ahigh && bhigh */
1421 NV right = SvNV_nomg(svr);
1422 NV left = SvNV_nomg(svl);
1424 SETn( left * right );
1431 dVAR; dSP; dATARGET; SV *svl, *svr;
1432 tryAMAGICbin_MG(div_amg, AMGf_assign|AMGf_numeric);
1435 /* Only try to do UV divide first
1436 if ((SLOPPYDIVIDE is true) or
1437 (PERL_PRESERVE_IVUV is true and one or both SV is a UV too large
1439 The assumption is that it is better to use floating point divide
1440 whenever possible, only doing integer divide first if we can't be sure.
1441 If NV_PRESERVES_UV is true then we know at compile time that no UV
1442 can be too large to preserve, so don't need to compile the code to
1443 test the size of UVs. */
1446 # define PERL_TRY_UV_DIVIDE
1447 /* ensure that 20./5. == 4. */
1449 # ifdef PERL_PRESERVE_IVUV
1450 # ifndef NV_PRESERVES_UV
1451 # define PERL_TRY_UV_DIVIDE
1456 #ifdef PERL_TRY_UV_DIVIDE
1457 SvIV_please_nomg(svr);
1459 SvIV_please_nomg(svl);
1461 bool left_non_neg = SvUOK(svl);
1462 bool right_non_neg = SvUOK(svr);
1466 if (right_non_neg) {
1470 const IV biv = SvIVX(svr);
1473 right_non_neg = TRUE; /* effectively it's a UV now */
1479 /* historically undef()/0 gives a "Use of uninitialized value"
1480 warning before dieing, hence this test goes here.
1481 If it were immediately before the second SvIV_please, then
1482 DIE() would be invoked before left was even inspected, so
1483 no inspection would give no warning. */
1485 DIE(aTHX_ "Illegal division by zero");
1491 const IV aiv = SvIVX(svl);
1494 left_non_neg = TRUE; /* effectively it's a UV now */
1503 /* For sloppy divide we always attempt integer division. */
1505 /* Otherwise we only attempt it if either or both operands
1506 would not be preserved by an NV. If both fit in NVs
1507 we fall through to the NV divide code below. However,
1508 as left >= right to ensure integer result here, we know that
1509 we can skip the test on the right operand - right big
1510 enough not to be preserved can't get here unless left is
1513 && (left > ((UV)1 << NV_PRESERVES_UV_BITS))
1516 /* Integer division can't overflow, but it can be imprecise. */
1517 const UV result = left / right;
1518 if (result * right == left) {
1519 SP--; /* result is valid */
1520 if (left_non_neg == right_non_neg) {
1521 /* signs identical, result is positive. */
1525 /* 2s complement assumption */
1526 if (result <= (UV)IV_MIN)
1527 SETi( -(IV)result );
1529 /* It's exact but too negative for IV. */
1530 SETn( -(NV)result );
1533 } /* tried integer divide but it was not an integer result */
1534 } /* else (PERL_ABS(result) < 1.0) or (both UVs in range for NV) */
1535 } /* left wasn't SvIOK */
1536 } /* right wasn't SvIOK */
1537 #endif /* PERL_TRY_UV_DIVIDE */
1539 NV right = SvNV_nomg(svr);
1540 NV left = SvNV_nomg(svl);
1541 (void)POPs;(void)POPs;
1542 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
1543 if (! Perl_isnan(right) && right == 0.0)
1547 DIE(aTHX_ "Illegal division by zero");
1548 PUSHn( left / right );
1555 dVAR; dSP; dATARGET;
1556 tryAMAGICbin_MG(modulo_amg, AMGf_assign|AMGf_numeric);
1560 bool left_neg = FALSE;
1561 bool right_neg = FALSE;
1562 bool use_double = FALSE;
1563 bool dright_valid = FALSE;
1566 SV * const svr = TOPs;
1567 SV * const svl = TOPm1s;
1568 SvIV_please_nomg(svr);
1570 right_neg = !SvUOK(svr);
1574 const IV biv = SvIVX(svr);
1577 right_neg = FALSE; /* effectively it's a UV now */
1584 dright = SvNV_nomg(svr);
1585 right_neg = dright < 0;
1588 if (dright < UV_MAX_P1) {
1589 right = U_V(dright);
1590 dright_valid = TRUE; /* In case we need to use double below. */
1596 /* At this point use_double is only true if right is out of range for
1597 a UV. In range NV has been rounded down to nearest UV and
1598 use_double false. */
1599 SvIV_please_nomg(svl);
1600 if (!use_double && SvIOK(svl)) {
1602 left_neg = !SvUOK(svl);
1606 const IV aiv = SvIVX(svl);
1609 left_neg = FALSE; /* effectively it's a UV now */
1617 dleft = SvNV_nomg(svl);
1618 left_neg = dleft < 0;
1622 /* This should be exactly the 5.6 behaviour - if left and right are
1623 both in range for UV then use U_V() rather than floor. */
1625 if (dleft < UV_MAX_P1) {
1626 /* right was in range, so is dleft, so use UVs not double.
1630 /* left is out of range for UV, right was in range, so promote
1631 right (back) to double. */
1633 /* The +0.5 is used in 5.6 even though it is not strictly
1634 consistent with the implicit +0 floor in the U_V()
1635 inside the #if 1. */
1636 dleft = Perl_floor(dleft + 0.5);
1639 dright = Perl_floor(dright + 0.5);
1650 DIE(aTHX_ "Illegal modulus zero");
1652 dans = Perl_fmod(dleft, dright);
1653 if ((left_neg != right_neg) && dans)
1654 dans = dright - dans;
1657 sv_setnv(TARG, dans);
1663 DIE(aTHX_ "Illegal modulus zero");
1666 if ((left_neg != right_neg) && ans)
1669 /* XXX may warn: unary minus operator applied to unsigned type */
1670 /* could change -foo to be (~foo)+1 instead */
1671 if (ans <= ~((UV)IV_MAX)+1)
1672 sv_setiv(TARG, ~ans+1);
1674 sv_setnv(TARG, -(NV)ans);
1677 sv_setuv(TARG, ans);
1686 dVAR; dSP; dATARGET;
1690 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1691 /* TODO: think of some way of doing list-repeat overloading ??? */
1696 tryAMAGICbin_MG(repeat_amg, AMGf_assign);
1702 const UV uv = SvUV_nomg(sv);
1704 count = IV_MAX; /* The best we can do? */
1708 const IV iv = SvIV_nomg(sv);
1715 else if (SvNOKp(sv)) {
1716 const NV nv = SvNV_nomg(sv);
1723 count = SvIV_nomg(sv);
1725 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1727 static const char oom_list_extend[] = "Out of memory during list extend";
1728 const I32 items = SP - MARK;
1729 const I32 max = items * count;
1731 MEM_WRAP_CHECK_1(max, SV*, oom_list_extend);
1732 /* Did the max computation overflow? */
1733 if (items > 0 && max > 0 && (max < items || max < count))
1734 Perl_croak(aTHX_ oom_list_extend);
1739 /* This code was intended to fix 20010809.028:
1742 for (($x =~ /./g) x 2) {
1743 print chop; # "abcdabcd" expected as output.
1746 * but that change (#11635) broke this code:
1748 $x = [("foo")x2]; # only one "foo" ended up in the anonlist.
1750 * I can't think of a better fix that doesn't introduce
1751 * an efficiency hit by copying the SVs. The stack isn't
1752 * refcounted, and mortalisation obviously doesn't
1753 * Do The Right Thing when the stack has more than
1754 * one pointer to the same mortal value.
1758 *SP = sv_2mortal(newSVsv(*SP));
1768 repeatcpy((char*)(MARK + items), (char*)MARK,
1769 items * sizeof(const SV *), count - 1);
1772 else if (count <= 0)
1775 else { /* Note: mark already snarfed by pp_list */
1776 SV * const tmpstr = POPs;
1779 static const char oom_string_extend[] =
1780 "Out of memory during string extend";
1783 sv_setsv_nomg(TARG, tmpstr);
1784 SvPV_force_nomg(TARG, len);
1785 isutf = DO_UTF8(TARG);
1790 const STRLEN max = (UV)count * len;
1791 if (len > MEM_SIZE_MAX / count)
1792 Perl_croak(aTHX_ oom_string_extend);
1793 MEM_WRAP_CHECK_1(max, char, oom_string_extend);
1794 SvGROW(TARG, max + 1);
1795 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
1796 SvCUR_set(TARG, SvCUR(TARG) * count);
1798 *SvEND(TARG) = '\0';
1801 (void)SvPOK_only_UTF8(TARG);
1803 (void)SvPOK_only(TARG);
1805 if (PL_op->op_private & OPpREPEAT_DOLIST) {
1806 /* The parser saw this as a list repeat, and there
1807 are probably several items on the stack. But we're
1808 in scalar context, and there's no pp_list to save us
1809 now. So drop the rest of the items -- robin@kitsite.com
1821 dVAR; dSP; dATARGET; bool useleft; SV *svl, *svr;
1822 tryAMAGICbin_MG(subtr_amg, AMGf_assign|AMGf_numeric);
1825 useleft = USE_LEFT(svl);
1826 #ifdef PERL_PRESERVE_IVUV
1827 /* See comments in pp_add (in pp_hot.c) about Overflow, and how
1828 "bad things" happen if you rely on signed integers wrapping. */
1829 SvIV_please_nomg(svr);
1831 /* Unless the left argument is integer in range we are going to have to
1832 use NV maths. Hence only attempt to coerce the right argument if
1833 we know the left is integer. */
1834 register UV auv = 0;
1840 a_valid = auvok = 1;
1841 /* left operand is undef, treat as zero. */
1843 /* Left operand is defined, so is it IV? */
1844 SvIV_please_nomg(svl);
1846 if ((auvok = SvUOK(svl)))
1849 register const IV aiv = SvIVX(svl);
1852 auvok = 1; /* Now acting as a sign flag. */
1853 } else { /* 2s complement assumption for IV_MIN */
1861 bool result_good = 0;
1864 bool buvok = SvUOK(svr);
1869 register const IV biv = SvIVX(svr);
1876 /* ?uvok if value is >= 0. basically, flagged as UV if it's +ve,
1877 else "IV" now, independent of how it came in.
1878 if a, b represents positive, A, B negative, a maps to -A etc
1883 all UV maths. negate result if A negative.
1884 subtract if signs same, add if signs differ. */
1886 if (auvok ^ buvok) {
1895 /* Must get smaller */
1900 if (result <= buv) {
1901 /* result really should be -(auv-buv). as its negation
1902 of true value, need to swap our result flag */
1914 if (result <= (UV)IV_MIN)
1915 SETi( -(IV)result );
1917 /* result valid, but out of range for IV. */
1918 SETn( -(NV)result );
1922 } /* Overflow, drop through to NVs. */
1927 NV value = SvNV_nomg(svr);
1931 /* left operand is undef, treat as zero - value */
1935 SETn( SvNV_nomg(svl) - value );
1942 dVAR; dSP; dATARGET; SV *svl, *svr;
1943 tryAMAGICbin_MG(lshift_amg, AMGf_assign|AMGf_numeric);
1947 const IV shift = SvIV_nomg(svr);
1948 if (PL_op->op_private & HINT_INTEGER) {
1949 const IV i = SvIV_nomg(svl);
1953 const UV u = SvUV_nomg(svl);
1962 dVAR; dSP; dATARGET; SV *svl, *svr;
1963 tryAMAGICbin_MG(rshift_amg, AMGf_assign|AMGf_numeric);
1967 const IV shift = SvIV_nomg(svr);
1968 if (PL_op->op_private & HINT_INTEGER) {
1969 const IV i = SvIV_nomg(svl);
1973 const UV u = SvUV_nomg(svl);
1985 tryAMAGICbin_MG(lt_amg, AMGf_set|AMGf_numeric);
1989 (SvIOK_notUV(left) && SvIOK_notUV(right))
1990 ? (SvIVX(left) < SvIVX(right))
1991 : (do_ncmp(left, right) == -1)
2001 tryAMAGICbin_MG(gt_amg, AMGf_set|AMGf_numeric);
2005 (SvIOK_notUV(left) && SvIOK_notUV(right))
2006 ? (SvIVX(left) > SvIVX(right))
2007 : (do_ncmp(left, right) == 1)
2017 tryAMAGICbin_MG(le_amg, AMGf_set|AMGf_numeric);
2021 (SvIOK_notUV(left) && SvIOK_notUV(right))
2022 ? (SvIVX(left) <= SvIVX(right))
2023 : (do_ncmp(left, right) <= 0)
2033 tryAMAGICbin_MG(ge_amg, AMGf_set|AMGf_numeric);
2037 (SvIOK_notUV(left) && SvIOK_notUV(right))
2038 ? (SvIVX(left) >= SvIVX(right))
2039 : ( (do_ncmp(left, right) & 2) == 0)
2049 tryAMAGICbin_MG(ne_amg, AMGf_set|AMGf_numeric);
2053 (SvIOK_notUV(left) && SvIOK_notUV(right))
2054 ? (SvIVX(left) != SvIVX(right))
2055 : (do_ncmp(left, right) != 0)
2060 /* compare left and right SVs. Returns:
2064 * 2: left or right was a NaN
2067 Perl_do_ncmp(pTHX_ SV* const left, SV * const right)
2071 PERL_ARGS_ASSERT_DO_NCMP;
2072 #ifdef PERL_PRESERVE_IVUV
2073 SvIV_please_nomg(right);
2074 /* Fortunately it seems NaN isn't IOK */
2076 SvIV_please_nomg(left);
2079 const IV leftiv = SvIVX(left);
2080 if (!SvUOK(right)) {
2081 /* ## IV <=> IV ## */
2082 const IV rightiv = SvIVX(right);
2083 return (leftiv > rightiv) - (leftiv < rightiv);
2085 /* ## IV <=> UV ## */
2087 /* As (b) is a UV, it's >=0, so it must be < */
2090 const UV rightuv = SvUVX(right);
2091 return ((UV)leftiv > rightuv) - ((UV)leftiv < rightuv);
2096 /* ## UV <=> UV ## */
2097 const UV leftuv = SvUVX(left);
2098 const UV rightuv = SvUVX(right);
2099 return (leftuv > rightuv) - (leftuv < rightuv);
2101 /* ## UV <=> IV ## */
2103 const IV rightiv = SvIVX(right);
2105 /* As (a) is a UV, it's >=0, so it cannot be < */
2108 const UV leftuv = SvUVX(left);
2109 return (leftuv > (UV)rightiv) - (leftuv < (UV)rightiv);
2117 NV const rnv = SvNV_nomg(right);
2118 NV const lnv = SvNV_nomg(left);
2120 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
2121 if (Perl_isnan(lnv) || Perl_isnan(rnv)) {
2124 return (lnv > rnv) - (lnv < rnv);
2143 tryAMAGICbin_MG(ncmp_amg, AMGf_numeric);
2146 value = do_ncmp(left, right);
2161 int amg_type = sle_amg;
2165 switch (PL_op->op_type) {
2184 tryAMAGICbin_MG(amg_type, AMGf_set);
2187 const int cmp = (IN_LOCALE_RUNTIME
2188 ? sv_cmp_locale_flags(left, right, 0)
2189 : sv_cmp_flags(left, right, 0));
2190 SETs(boolSV(cmp * multiplier < rhs));
2198 tryAMAGICbin_MG(seq_amg, AMGf_set);
2201 SETs(boolSV(sv_eq_flags(left, right, 0)));
2209 tryAMAGICbin_MG(sne_amg, AMGf_set);
2212 SETs(boolSV(!sv_eq_flags(left, right, 0)));
2220 tryAMAGICbin_MG(scmp_amg, 0);
2223 const int cmp = (IN_LOCALE_RUNTIME
2224 ? sv_cmp_locale_flags(left, right, 0)
2225 : sv_cmp_flags(left, right, 0));
2233 dVAR; dSP; dATARGET;
2234 tryAMAGICbin_MG(band_amg, AMGf_assign);
2237 if (SvNIOKp(left) || SvNIOKp(right)) {
2238 const bool left_ro_nonnum = !SvNIOKp(left) && SvREADONLY(left);
2239 const bool right_ro_nonnum = !SvNIOKp(right) && SvREADONLY(right);
2240 if (PL_op->op_private & HINT_INTEGER) {
2241 const IV i = SvIV_nomg(left) & SvIV_nomg(right);
2245 const UV u = SvUV_nomg(left) & SvUV_nomg(right);
2248 if (left_ro_nonnum) SvNIOK_off(left);
2249 if (right_ro_nonnum) SvNIOK_off(right);
2252 do_vop(PL_op->op_type, TARG, left, right);
2261 dVAR; dSP; dATARGET;
2262 const int op_type = PL_op->op_type;
2264 tryAMAGICbin_MG((op_type == OP_BIT_OR ? bor_amg : bxor_amg), AMGf_assign);
2267 if (SvNIOKp(left) || SvNIOKp(right)) {
2268 const bool left_ro_nonnum = !SvNIOKp(left) && SvREADONLY(left);
2269 const bool right_ro_nonnum = !SvNIOKp(right) && SvREADONLY(right);
2270 if (PL_op->op_private & HINT_INTEGER) {
2271 const IV l = (USE_LEFT(left) ? SvIV_nomg(left) : 0);
2272 const IV r = SvIV_nomg(right);
2273 const IV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2277 const UV l = (USE_LEFT(left) ? SvUV_nomg(left) : 0);
2278 const UV r = SvUV_nomg(right);
2279 const UV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2282 if (left_ro_nonnum) SvNIOK_off(left);
2283 if (right_ro_nonnum) SvNIOK_off(right);
2286 do_vop(op_type, TARG, left, right);
2296 tryAMAGICun_MG(neg_amg, AMGf_numeric);
2298 SV * const sv = TOPs;
2299 const int flags = SvFLAGS(sv);
2301 if( !SvNIOK( sv ) && looks_like_number( sv ) ){
2305 if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
2306 /* It's publicly an integer, or privately an integer-not-float */
2309 if (SvIVX(sv) == IV_MIN) {
2310 /* 2s complement assumption. */
2311 SETi(SvIVX(sv)); /* special case: -((UV)IV_MAX+1) == IV_MIN */
2314 else if (SvUVX(sv) <= IV_MAX) {
2319 else if (SvIVX(sv) != IV_MIN) {
2323 #ifdef PERL_PRESERVE_IVUV
2331 SETn(-SvNV_nomg(sv));
2332 else if (SvPOKp(sv)) {
2334 const char * const s = SvPV_nomg_const(sv, len);
2335 if (isIDFIRST(*s)) {
2336 sv_setpvs(TARG, "-");
2339 else if (*s == '+' || *s == '-') {
2340 sv_setsv_nomg(TARG, sv);
2341 *SvPV_force_nomg(TARG, len) = *s == '-' ? '+' : '-';
2343 else if (DO_UTF8(sv)) {
2344 SvIV_please_nomg(sv);
2346 goto oops_its_an_int;
2348 sv_setnv(TARG, -SvNV_nomg(sv));
2350 sv_setpvs(TARG, "-");
2355 SvIV_please_nomg(sv);
2357 goto oops_its_an_int;
2358 sv_setnv(TARG, -SvNV_nomg(sv));
2363 SETn(-SvNV_nomg(sv));
2371 tryAMAGICun_MG(not_amg, AMGf_set);
2372 *PL_stack_sp = boolSV(!SvTRUE_nomg(*PL_stack_sp));
2379 tryAMAGICun_MG(compl_amg, AMGf_numeric);
2383 if (PL_op->op_private & HINT_INTEGER) {
2384 const IV i = ~SvIV_nomg(sv);
2388 const UV u = ~SvUV_nomg(sv);
2397 (void)SvPV_nomg_const(sv,len); /* force check for uninit var */
2398 sv_setsv_nomg(TARG, sv);
2399 tmps = (U8*)SvPV_force_nomg(TARG, len);
2402 /* Calculate exact length, let's not estimate. */
2407 U8 * const send = tmps + len;
2408 U8 * const origtmps = tmps;
2409 const UV utf8flags = UTF8_ALLOW_ANYUV;
2411 while (tmps < send) {
2412 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2414 targlen += UNISKIP(~c);
2420 /* Now rewind strings and write them. */
2427 Newx(result, targlen + 1, U8);
2429 while (tmps < send) {
2430 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2432 p = uvchr_to_utf8_flags(p, ~c, UNICODE_ALLOW_ANY);
2435 sv_usepvn_flags(TARG, (char*)result, targlen,
2436 SV_HAS_TRAILING_NUL);
2443 Newx(result, nchar + 1, U8);
2445 while (tmps < send) {
2446 const U8 c = (U8)utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2451 sv_usepvn_flags(TARG, (char*)result, nchar, SV_HAS_TRAILING_NUL);
2459 register long *tmpl;
2460 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
2463 for ( ; anum >= (I32)sizeof(long); anum -= (I32)sizeof(long), tmpl++)
2468 for ( ; anum > 0; anum--, tmps++)
2476 /* integer versions of some of the above */
2480 dVAR; dSP; dATARGET;
2481 tryAMAGICbin_MG(mult_amg, AMGf_assign);
2484 SETi( left * right );
2492 dVAR; dSP; dATARGET;
2493 tryAMAGICbin_MG(div_amg, AMGf_assign);
2496 IV value = SvIV_nomg(right);
2498 DIE(aTHX_ "Illegal division by zero");
2499 num = SvIV_nomg(left);
2501 /* avoid FPE_INTOVF on some platforms when num is IV_MIN */
2505 value = num / value;
2511 #if defined(__GLIBC__) && IVSIZE == 8
2518 /* This is the vanilla old i_modulo. */
2519 dVAR; dSP; dATARGET;
2520 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2524 DIE(aTHX_ "Illegal modulus zero");
2525 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2529 SETi( left % right );
2534 #if defined(__GLIBC__) && IVSIZE == 8
2539 /* This is the i_modulo with the workaround for the _moddi3 bug
2540 * in (at least) glibc 2.2.5 (the PERL_ABS() the workaround).
2541 * See below for pp_i_modulo. */
2542 dVAR; dSP; dATARGET;
2543 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2547 DIE(aTHX_ "Illegal modulus zero");
2548 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2552 SETi( left % PERL_ABS(right) );
2559 dVAR; dSP; dATARGET;
2560 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2564 DIE(aTHX_ "Illegal modulus zero");
2565 /* The assumption is to use hereafter the old vanilla version... */
2567 PL_ppaddr[OP_I_MODULO] =
2569 /* .. but if we have glibc, we might have a buggy _moddi3
2570 * (at least glicb 2.2.5 is known to have this bug), in other
2571 * words our integer modulus with negative quad as the second
2572 * argument might be broken. Test for this and re-patch the
2573 * opcode dispatch table if that is the case, remembering to
2574 * also apply the workaround so that this first round works
2575 * right, too. See [perl #9402] for more information. */
2579 /* Cannot do this check with inlined IV constants since
2580 * that seems to work correctly even with the buggy glibc. */
2582 /* Yikes, we have the bug.
2583 * Patch in the workaround version. */
2585 PL_ppaddr[OP_I_MODULO] =
2586 &Perl_pp_i_modulo_1;
2587 /* Make certain we work right this time, too. */
2588 right = PERL_ABS(right);
2591 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2595 SETi( left % right );
2603 dVAR; dSP; dATARGET;
2604 tryAMAGICbin_MG(add_amg, AMGf_assign);
2606 dPOPTOPiirl_ul_nomg;
2607 SETi( left + right );
2614 dVAR; dSP; dATARGET;
2615 tryAMAGICbin_MG(subtr_amg, AMGf_assign);
2617 dPOPTOPiirl_ul_nomg;
2618 SETi( left - right );
2626 tryAMAGICbin_MG(lt_amg, AMGf_set);
2629 SETs(boolSV(left < right));
2637 tryAMAGICbin_MG(gt_amg, AMGf_set);
2640 SETs(boolSV(left > right));
2648 tryAMAGICbin_MG(le_amg, AMGf_set);
2651 SETs(boolSV(left <= right));
2659 tryAMAGICbin_MG(ge_amg, AMGf_set);
2662 SETs(boolSV(left >= right));
2670 tryAMAGICbin_MG(eq_amg, AMGf_set);
2673 SETs(boolSV(left == right));
2681 tryAMAGICbin_MG(ne_amg, AMGf_set);
2684 SETs(boolSV(left != right));
2692 tryAMAGICbin_MG(ncmp_amg, 0);
2699 else if (left < right)
2711 tryAMAGICun_MG(neg_amg, 0);
2713 SV * const sv = TOPs;
2714 IV const i = SvIV_nomg(sv);
2720 /* High falutin' math. */
2725 tryAMAGICbin_MG(atan2_amg, 0);
2728 SETn(Perl_atan2(left, right));
2736 int amg_type = sin_amg;
2737 const char *neg_report = NULL;
2738 NV (*func)(NV) = Perl_sin;
2739 const int op_type = PL_op->op_type;
2756 amg_type = sqrt_amg;
2758 neg_report = "sqrt";
2763 tryAMAGICun_MG(amg_type, 0);
2765 SV * const arg = POPs;
2766 const NV value = SvNV_nomg(arg);
2768 if (op_type == OP_LOG ? (value <= 0.0) : (value < 0.0)) {
2769 SET_NUMERIC_STANDARD();
2770 DIE(aTHX_ "Can't take %s of %"NVgf, neg_report, value);
2773 XPUSHn(func(value));
2778 /* Support Configure command-line overrides for rand() functions.
2779 After 5.005, perhaps we should replace this by Configure support
2780 for drand48(), random(), or rand(). For 5.005, though, maintain
2781 compatibility by calling rand() but allow the user to override it.
2782 See INSTALL for details. --Andy Dougherty 15 July 1998
2784 /* Now it's after 5.005, and Configure supports drand48() and random(),
2785 in addition to rand(). So the overrides should not be needed any more.
2786 --Jarkko Hietaniemi 27 September 1998
2789 #ifndef HAS_DRAND48_PROTO
2790 extern double drand48 (void);
2803 if (!PL_srand_called) {
2804 (void)seedDrand01((Rand_seed_t)seed());
2805 PL_srand_called = TRUE;
2815 const UV anum = (MAXARG < 1) ? seed() : POPu;
2816 (void)seedDrand01((Rand_seed_t)anum);
2817 PL_srand_called = TRUE;
2821 /* Historically srand always returned true. We can avoid breaking
2823 sv_setpvs(TARG, "0 but true");
2832 tryAMAGICun_MG(int_amg, AMGf_numeric);
2834 SV * const sv = TOPs;
2835 const IV iv = SvIV_nomg(sv);
2836 /* XXX it's arguable that compiler casting to IV might be subtly
2837 different from modf (for numbers inside (IV_MIN,UV_MAX)) in which
2838 else preferring IV has introduced a subtle behaviour change bug. OTOH
2839 relying on floating point to be accurate is a bug. */
2844 else if (SvIOK(sv)) {
2846 SETu(SvUV_nomg(sv));
2851 const NV value = SvNV_nomg(sv);
2853 if (value < (NV)UV_MAX + 0.5) {
2856 SETn(Perl_floor(value));
2860 if (value > (NV)IV_MIN - 0.5) {
2863 SETn(Perl_ceil(value));
2874 tryAMAGICun_MG(abs_amg, AMGf_numeric);
2876 SV * const sv = TOPs;
2877 /* This will cache the NV value if string isn't actually integer */
2878 const IV iv = SvIV_nomg(sv);
2883 else if (SvIOK(sv)) {
2884 /* IVX is precise */
2886 SETu(SvUV_nomg(sv)); /* force it to be numeric only */
2894 /* 2s complement assumption. Also, not really needed as
2895 IV_MIN and -IV_MIN should both be %100...00 and NV-able */
2901 const NV value = SvNV_nomg(sv);
2915 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES;
2919 SV* const sv = POPs;
2921 tmps = (SvPV_const(sv, len));
2923 /* If Unicode, try to downgrade
2924 * If not possible, croak. */
2925 SV* const tsv = sv_2mortal(newSVsv(sv));
2928 sv_utf8_downgrade(tsv, FALSE);
2929 tmps = SvPV_const(tsv, len);
2931 if (PL_op->op_type == OP_HEX)
2934 while (*tmps && len && isSPACE(*tmps))
2938 if (*tmps == 'x' || *tmps == 'X') {
2940 result_uv = grok_hex (tmps, &len, &flags, &result_nv);
2942 else if (*tmps == 'b' || *tmps == 'B')
2943 result_uv = grok_bin (tmps, &len, &flags, &result_nv);
2945 result_uv = grok_oct (tmps, &len, &flags, &result_nv);
2947 if (flags & PERL_SCAN_GREATER_THAN_UV_MAX) {
2961 SV * const sv = TOPs;
2963 if (SvGAMAGIC(sv)) {
2964 /* For an overloaded or magic scalar, we can't know in advance if
2965 it's going to be UTF-8 or not. Also, we can't call sv_len_utf8 as
2966 it likes to cache the length. Maybe that should be a documented
2971 = sv_2pv_flags(sv, &len,
2972 SV_UNDEF_RETURNS_NULL|SV_CONST_RETURN|SV_GMAGIC);
2975 if (!SvPADTMP(TARG)) {
2976 sv_setsv(TARG, &PL_sv_undef);
2981 else if (DO_UTF8(sv)) {
2982 SETi(utf8_length((U8*)p, (U8*)p + len));
2986 } else if (SvOK(sv)) {
2987 /* Neither magic nor overloaded. */
2989 SETi(sv_len_utf8(sv));
2993 if (!SvPADTMP(TARG)) {
2994 sv_setsv_nomg(TARG, &PL_sv_undef);
3016 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
3018 const IV arybase = CopARYBASE_get(PL_curcop);
3020 const char *repl = NULL;
3022 const int num_args = PL_op->op_private & 7;
3023 bool repl_need_utf8_upgrade = FALSE;
3024 bool repl_is_utf8 = FALSE;
3029 repl = SvPV_const(repl_sv, repl_len);
3030 repl_is_utf8 = DO_UTF8(repl_sv) && SvCUR(repl_sv);
3033 len_iv = SvIV(len_sv);
3034 len_is_uv = SvIOK_UV(len_sv);
3037 pos1_iv = SvIV(pos_sv);
3038 pos1_is_uv = SvIOK_UV(pos_sv);
3044 sv_utf8_upgrade(sv);
3046 else if (DO_UTF8(sv))
3047 repl_need_utf8_upgrade = TRUE;
3049 tmps = SvPV_const(sv, curlen);
3051 utf8_curlen = sv_len_utf8(sv);
3052 if (utf8_curlen == curlen)
3055 curlen = utf8_curlen;
3060 if ( (pos1_is_uv && arybase < 0) || (pos1_iv >= arybase) ) { /* pos >= $[ */
3061 UV pos1_uv = pos1_iv-arybase;
3062 /* Overflow can occur when $[ < 0 */
3063 if (arybase < 0 && pos1_uv < (UV)pos1_iv)
3068 else if (pos1_is_uv ? (UV)pos1_iv > 0 : pos1_iv > 0) {
3069 goto bound_fail; /* $[=3; substr($_,2,...) */
3071 else { /* pos < $[ */
3072 if (pos1_iv == 0) { /* $[=1; substr($_,0,...) */
3077 pos1_is_uv = curlen-1 > ~(UV)pos1_iv;
3082 if (pos1_is_uv || pos1_iv > 0) {
3083 if ((UV)pos1_iv > curlen)
3088 if (!len_is_uv && len_iv < 0) {
3089 pos2_iv = curlen + len_iv;
3091 pos2_is_uv = curlen-1 > ~(UV)len_iv;
3094 } else { /* len_iv >= 0 */
3095 if (!pos1_is_uv && pos1_iv < 0) {
3096 pos2_iv = pos1_iv + len_iv;
3097 pos2_is_uv = (UV)len_iv > (UV)IV_MAX;
3099 if ((UV)len_iv > curlen-(UV)pos1_iv)
3102 pos2_iv = pos1_iv+len_iv;
3112 if (!pos2_is_uv && pos2_iv < 0) {
3113 if (!pos1_is_uv && pos1_iv < 0)
3117 else if (!pos1_is_uv && pos1_iv < 0)
3120 if ((UV)pos2_iv < (UV)pos1_iv)
3122 if ((UV)pos2_iv > curlen)
3126 /* pos1_iv and pos2_iv both in 0..curlen, so the cast is safe */
3127 const STRLEN pos = (STRLEN)( (UV)pos1_iv );
3128 const STRLEN len = (STRLEN)( (UV)pos2_iv - (UV)pos1_iv );
3129 STRLEN byte_len = len;
3130 STRLEN byte_pos = utf8_curlen
3131 ? sv_pos_u2b_flags(sv, pos, &byte_len, SV_CONST_RETURN) : pos;
3133 if (lvalue && !repl) {
3136 if (!SvGMAGICAL(sv)) {
3138 SvPV_force_nolen(sv);
3139 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR),
3140 "Attempt to use reference as lvalue in substr");
3142 if (isGV_with_GP(sv))
3143 SvPV_force_nolen(sv);
3144 else if (SvOK(sv)) /* is it defined ? */
3145 (void)SvPOK_only_UTF8(sv);
3147 sv_setpvs(sv, ""); /* avoid lexical reincarnation */
3150 ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
3151 sv_magic(ret, NULL, PERL_MAGIC_substr, NULL, 0);
3153 LvTARG(ret) = SvREFCNT_inc_simple(sv);
3154 LvTARGOFF(ret) = pos;
3155 LvTARGLEN(ret) = len;
3158 PUSHs(ret); /* avoid SvSETMAGIC here */
3162 SvTAINTED_off(TARG); /* decontaminate */
3163 SvUTF8_off(TARG); /* decontaminate */
3166 sv_setpvn(TARG, tmps, byte_len);
3167 #ifdef USE_LOCALE_COLLATE
3168 sv_unmagic(TARG, PERL_MAGIC_collxfrm);
3174 SV* repl_sv_copy = NULL;
3176 if (repl_need_utf8_upgrade) {
3177 repl_sv_copy = newSVsv(repl_sv);
3178 sv_utf8_upgrade(repl_sv_copy);
3179 repl = SvPV_const(repl_sv_copy, repl_len);
3180 repl_is_utf8 = DO_UTF8(repl_sv_copy) && SvCUR(sv);
3184 sv_insert_flags(sv, byte_pos, byte_len, repl, repl_len, 0);
3187 SvREFCNT_dec(repl_sv_copy);
3197 Perl_croak(aTHX_ "substr outside of string");
3198 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR), "substr outside of string");
3205 register const IV size = POPi;
3206 register const IV offset = POPi;
3207 register SV * const src = POPs;
3208 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
3211 if (lvalue) { /* it's an lvalue! */
3212 ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
3213 sv_magic(ret, NULL, PERL_MAGIC_vec, NULL, 0);
3215 LvTARG(ret) = SvREFCNT_inc_simple(src);
3216 LvTARGOFF(ret) = offset;
3217 LvTARGLEN(ret) = size;
3221 SvTAINTED_off(TARG); /* decontaminate */
3225 sv_setuv(ret, do_vecget(src, offset, size));
3241 const char *little_p;
3242 const I32 arybase = CopARYBASE_get(PL_curcop);
3245 const bool is_index = PL_op->op_type == OP_INDEX;
3248 /* arybase is in characters, like offset, so combine prior to the
3249 UTF-8 to bytes calculation. */
3250 offset = POPi - arybase;
3254 big_p = SvPV_const(big, biglen);
3255 little_p = SvPV_const(little, llen);
3257 big_utf8 = DO_UTF8(big);
3258 little_utf8 = DO_UTF8(little);
3259 if (big_utf8 ^ little_utf8) {
3260 /* One needs to be upgraded. */
3261 if (little_utf8 && !PL_encoding) {
3262 /* Well, maybe instead we might be able to downgrade the small
3264 char * const pv = (char*)bytes_from_utf8((U8 *)little_p, &llen,
3267 /* If the large string is ISO-8859-1, and it's not possible to
3268 convert the small string to ISO-8859-1, then there is no
3269 way that it could be found anywhere by index. */
3274 /* At this point, pv is a malloc()ed string. So donate it to temp
3275 to ensure it will get free()d */
3276 little = temp = newSV(0);
3277 sv_usepvn(temp, pv, llen);
3278 little_p = SvPVX(little);
3281 ? newSVpvn(big_p, biglen) : newSVpvn(little_p, llen);
3284 sv_recode_to_utf8(temp, PL_encoding);
3286 sv_utf8_upgrade(temp);
3291 big_p = SvPV_const(big, biglen);
3294 little_p = SvPV_const(little, llen);
3298 if (SvGAMAGIC(big)) {
3299 /* Life just becomes a lot easier if I use a temporary here.
3300 Otherwise I need to avoid calls to sv_pos_u2b(), which (dangerously)
3301 will trigger magic and overloading again, as will fbm_instr()
3303 big = newSVpvn_flags(big_p, biglen,
3304 SVs_TEMP | (big_utf8 ? SVf_UTF8 : 0));
3307 if (SvGAMAGIC(little) || (is_index && !SvOK(little))) {
3308 /* index && SvOK() is a hack. fbm_instr() calls SvPV_const, which will
3309 warn on undef, and we've already triggered a warning with the
3310 SvPV_const some lines above. We can't remove that, as we need to
3311 call some SvPV to trigger overloading early and find out if the
3313 This is all getting to messy. The API isn't quite clean enough,
3314 because data access has side effects.
3316 little = newSVpvn_flags(little_p, llen,
3317 SVs_TEMP | (little_utf8 ? SVf_UTF8 : 0));
3318 little_p = SvPVX(little);
3322 offset = is_index ? 0 : biglen;
3324 if (big_utf8 && offset > 0)
3325 sv_pos_u2b(big, &offset, 0);
3331 else if (offset > (I32)biglen)
3333 if (!(little_p = is_index
3334 ? fbm_instr((unsigned char*)big_p + offset,
3335 (unsigned char*)big_p + biglen, little, 0)
3336 : rninstr(big_p, big_p + offset,
3337 little_p, little_p + llen)))
3340 retval = little_p - big_p;
3341 if (retval > 0 && big_utf8)
3342 sv_pos_b2u(big, &retval);
3346 PUSHi(retval + arybase);
3352 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
3353 SvTAINTED_off(TARG);
3354 do_sprintf(TARG, SP-MARK, MARK+1);
3355 TAINT_IF(SvTAINTED(TARG));
3367 const U8 *s = (U8*)SvPV_const(argsv, len);
3369 if (PL_encoding && SvPOK(argsv) && !DO_UTF8(argsv)) {
3370 SV * const tmpsv = sv_2mortal(newSVsv(argsv));
3371 s = (U8*)sv_recode_to_utf8(tmpsv, PL_encoding);
3375 XPUSHu(DO_UTF8(argsv) ?
3376 utf8n_to_uvchr(s, UTF8_MAXBYTES, 0, UTF8_ALLOW_ANYUV) :
3388 if (((SvIOK_notUV(TOPs) && SvIV(TOPs) < 0)
3390 (SvNOK(TOPs) && SvNV(TOPs) < 0.0))) {
3392 value = POPu; /* chr(-1) eq chr(0xff), etc. */
3394 (void) POPs; /* Ignore the argument value. */
3395 value = UNICODE_REPLACEMENT;
3401 SvUPGRADE(TARG,SVt_PV);
3403 if (value > 255 && !IN_BYTES) {
3404 SvGROW(TARG, (STRLEN)UNISKIP(value)+1);
3405 tmps = (char*)uvchr_to_utf8_flags((U8*)SvPVX(TARG), value, 0);
3406 SvCUR_set(TARG, tmps - SvPVX_const(TARG));
3408 (void)SvPOK_only(TARG);
3417 *tmps++ = (char)value;
3419 (void)SvPOK_only(TARG);
3421 if (PL_encoding && !IN_BYTES) {
3422 sv_recode_to_utf8(TARG, PL_encoding);
3424 if (SvCUR(TARG) == 0 || !is_utf8_string((U8*)tmps, SvCUR(TARG)) ||
3425 UNICODE_IS_REPLACEMENT(utf8_to_uvchr((U8*)tmps, NULL))) {
3429 *tmps++ = (char)value;
3445 const char *tmps = SvPV_const(left, len);
3447 if (DO_UTF8(left)) {
3448 /* If Unicode, try to downgrade.
3449 * If not possible, croak.
3450 * Yes, we made this up. */
3451 SV* const tsv = sv_2mortal(newSVsv(left));
3454 sv_utf8_downgrade(tsv, FALSE);
3455 tmps = SvPV_const(tsv, len);
3457 # ifdef USE_ITHREADS
3459 if (!PL_reentrant_buffer->_crypt_struct_buffer) {
3460 /* This should be threadsafe because in ithreads there is only
3461 * one thread per interpreter. If this would not be true,
3462 * we would need a mutex to protect this malloc. */
3463 PL_reentrant_buffer->_crypt_struct_buffer =
3464 (struct crypt_data *)safemalloc(sizeof(struct crypt_data));
3465 #if defined(__GLIBC__) || defined(__EMX__)
3466 if (PL_reentrant_buffer->_crypt_struct_buffer) {
3467 PL_reentrant_buffer->_crypt_struct_buffer->initialized = 0;
3468 /* work around glibc-2.2.5 bug */
3469 PL_reentrant_buffer->_crypt_struct_buffer->current_saltbits = 0;
3473 # endif /* HAS_CRYPT_R */
3474 # endif /* USE_ITHREADS */
3476 sv_setpv(TARG, fcrypt(tmps, SvPV_nolen_const(right)));
3478 sv_setpv(TARG, PerlProc_crypt(tmps, SvPV_nolen_const(right)));
3484 "The crypt() function is unimplemented due to excessive paranoia.");
3488 /* Generally UTF-8 and UTF-EBCDIC are indistinguishable at this level. So
3489 * most comments below say UTF-8, when in fact they mean UTF-EBCDIC as well */
3491 /* Below are several macros that generate code */
3492 /* Generates code to store a unicode codepoint c that is known to occupy
3493 * exactly two UTF-8 and UTF-EBCDIC bytes; it is stored into p and p+1. */
3494 #define STORE_UNI_TO_UTF8_TWO_BYTE(p, c) \
3496 *(p) = UTF8_TWO_BYTE_HI(c); \
3497 *((p)+1) = UTF8_TWO_BYTE_LO(c); \
3500 /* Like STORE_UNI_TO_UTF8_TWO_BYTE, but advances p to point to the next
3501 * available byte after the two bytes */
3502 #define CAT_UNI_TO_UTF8_TWO_BYTE(p, c) \
3504 *(p)++ = UTF8_TWO_BYTE_HI(c); \
3505 *((p)++) = UTF8_TWO_BYTE_LO(c); \
3508 /* Generates code to store the upper case of latin1 character l which is known
3509 * to have its upper case be non-latin1 into the two bytes p and p+1. There
3510 * are only two characters that fit this description, and this macro knows
3511 * about them, and that the upper case values fit into two UTF-8 or UTF-EBCDIC
3513 #define STORE_NON_LATIN1_UC(p, l) \
3515 if ((l) == LATIN_SMALL_LETTER_Y_WITH_DIAERESIS) { \
3516 STORE_UNI_TO_UTF8_TWO_BYTE((p), LATIN_CAPITAL_LETTER_Y_WITH_DIAERESIS); \
3517 } else { /* Must be the following letter */ \
3518 STORE_UNI_TO_UTF8_TWO_BYTE((p), GREEK_CAPITAL_LETTER_MU); \
3522 /* Like STORE_NON_LATIN1_UC, but advances p to point to the next available byte
3523 * after the character stored */
3524 #define CAT_NON_LATIN1_UC(p, l) \
3526 if ((l) == LATIN_SMALL_LETTER_Y_WITH_DIAERESIS) { \
3527 CAT_UNI_TO_UTF8_TWO_BYTE((p), LATIN_CAPITAL_LETTER_Y_WITH_DIAERESIS); \
3529 CAT_UNI_TO_UTF8_TWO_BYTE((p), GREEK_CAPITAL_LETTER_MU); \
3533 /* Generates code to add the two UTF-8 bytes (probably u) that are the upper
3534 * case of l into p and p+1. u must be the result of toUPPER_LATIN1_MOD(l),
3535 * and must require two bytes to store it. Advances p to point to the next
3536 * available position */
3537 #define CAT_TWO_BYTE_UNI_UPPER_MOD(p, l, u) \
3539 if ((u) != LATIN_SMALL_LETTER_Y_WITH_DIAERESIS) { \
3540 CAT_UNI_TO_UTF8_TWO_BYTE((p), (u)); /* not special, just save it */ \
3541 } else if (l == LATIN_SMALL_LETTER_SHARP_S) { \
3542 *(p)++ = 'S'; *(p)++ = 'S'; /* upper case is 'SS' */ \
3543 } else {/* else is one of the other two special cases */ \
3544 CAT_NON_LATIN1_UC((p), (l)); \
3550 /* Actually is both lcfirst() and ucfirst(). Only the first character
3551 * changes. This means that possibly we can change in-place, ie., just
3552 * take the source and change that one character and store it back, but not
3553 * if read-only etc, or if the length changes */
3558 STRLEN slen; /* slen is the byte length of the whole SV. */
3561 bool inplace; /* ? Convert first char only, in-place */
3562 bool doing_utf8 = FALSE; /* ? using utf8 */
3563 bool convert_source_to_utf8 = FALSE; /* ? need to convert */
3564 const int op_type = PL_op->op_type;
3567 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
3568 STRLEN ulen; /* ulen is the byte length of the original Unicode character
3569 * stored as UTF-8 at s. */
3570 STRLEN tculen; /* tculen is the byte length of the freshly titlecased (or
3571 * lowercased) character stored in tmpbuf. May be either
3572 * UTF-8 or not, but in either case is the number of bytes */
3576 s = (const U8*)SvPV_nomg_const(source, slen);
3578 if (ckWARN(WARN_UNINITIALIZED))
3579 report_uninit(source);
3584 /* We may be able to get away with changing only the first character, in
3585 * place, but not if read-only, etc. Later we may discover more reasons to
3586 * not convert in-place. */
3587 inplace = SvPADTMP(source) && !SvREADONLY(source) && SvTEMP(source);
3589 /* First calculate what the changed first character should be. This affects
3590 * whether we can just swap it out, leaving the rest of the string unchanged,
3591 * or even if have to convert the dest to UTF-8 when the source isn't */
3593 if (! slen) { /* If empty */
3594 need = 1; /* still need a trailing NUL */
3596 else if (DO_UTF8(source)) { /* Is the source utf8? */
3599 /* TODO: This is #ifdefd out because it has hard-coded the standard mappings,
3600 * and doesn't allow for the user to specify their own. When code is added to
3601 * detect if there is a user-defined mapping in force here, and if so to use
3602 * that, then the code below can be compiled. The detection would be a good
3603 * thing anyway, as currently the user-defined mappings only work on utf8
3604 * strings, and thus depend on the chosen internal storage method, which is a
3606 #ifdef GO_AHEAD_AND_BREAK_USER_DEFINED_CASE_MAPPINGS
3607 if (UTF8_IS_INVARIANT(*s)) {
3609 /* An invariant source character is either ASCII or, in EBCDIC, an
3610 * ASCII equivalent or a caseless C1 control. In both these cases,
3611 * the lower and upper cases of any character are also invariants
3612 * (and title case is the same as upper case). So it is safe to
3613 * use the simple case change macros which avoid the overhead of
3614 * the general functions. Note that if perl were to be extended to
3615 * do locale handling in UTF-8 strings, this wouldn't be true in,
3616 * for example, Lithuanian or Turkic. */
3617 *tmpbuf = (op_type == OP_LCFIRST) ? toLOWER(*s) : toUPPER(*s);
3621 else if (UTF8_IS_DOWNGRADEABLE_START(*s)) {
3624 /* Similarly, if the source character isn't invariant but is in the
3625 * latin1 range (or EBCDIC equivalent thereof), we have the case
3626 * changes compiled into perl, and can avoid the overhead of the
3627 * general functions. In this range, the characters are stored as
3628 * two UTF-8 bytes, and it so happens that any changed-case version
3629 * is also two bytes (in both ASCIIish and EBCDIC machines). */
3633 /* Convert the two source bytes to a single Unicode code point
3634 * value, change case and save for below */
3635 chr = TWO_BYTE_UTF8_TO_UNI(*s, *(s+1));
3636 if (op_type == OP_LCFIRST) { /* lower casing is easy */
3637 U8 lower = toLOWER_LATIN1(chr);
3638 STORE_UNI_TO_UTF8_TWO_BYTE(tmpbuf, lower);
3640 else { /* ucfirst */
3641 U8 upper = toUPPER_LATIN1_MOD(chr);
3643 /* Most of the latin1 range characters are well-behaved. Their
3644 * title and upper cases are the same, and are also in the
3645 * latin1 range. The macro above returns their upper (hence
3646 * title) case, and all that need be done is to save the result
3647 * for below. However, several characters are problematic, and
3648 * have to be handled specially. The MOD in the macro name
3649 * above means that these tricky characters all get mapped to
3650 * the single character LATIN_SMALL_LETTER_Y_WITH_DIAERESIS.
3651 * This mapping saves some tests for the majority of the
3654 if (upper != LATIN_SMALL_LETTER_Y_WITH_DIAERESIS) {
3656 /* Not tricky. Just save it. */
3657 STORE_UNI_TO_UTF8_TWO_BYTE(tmpbuf, upper);
3659 else if (chr == LATIN_SMALL_LETTER_SHARP_S) {
3661 /* This one is tricky because it is two characters long,
3662 * though the UTF-8 is still two bytes, so the stored
3663 * length doesn't change */
3664 *tmpbuf = 'S'; /* The UTF-8 is 'Ss' */
3665 *(tmpbuf + 1) = 's';
3669 /* The other two have their title and upper cases the same,
3670 * but are tricky because the changed-case characters
3671 * aren't in the latin1 range. They, however, do fit into
3672 * two UTF-8 bytes */
3673 STORE_NON_LATIN1_UC(tmpbuf, chr);
3678 #endif /* end of dont want to break user-defined casing */
3680 /* Here, can't short-cut the general case */
3682 utf8_to_uvchr(s, &ulen);
3683 if (op_type == OP_UCFIRST) toTITLE_utf8(s, tmpbuf, &tculen);
3684 else toLOWER_utf8(s, tmpbuf, &tculen);
3686 /* we can't do in-place if the length changes. */
3687 if (ulen != tculen) inplace = FALSE;
3688 need = slen + 1 - ulen + tculen;
3689 #ifdef GO_AHEAD_AND_BREAK_USER_DEFINED_CASE_MAPPINGS
3693 else { /* Non-zero length, non-UTF-8, Need to consider locale and if
3694 * latin1 is treated as caseless. Note that a locale takes
3696 tculen = 1; /* Most characters will require one byte, but this will
3697 * need to be overridden for the tricky ones */
3700 if (op_type == OP_LCFIRST) {
3702 /* lower case the first letter: no trickiness for any character */
3703 *tmpbuf = (IN_LOCALE_RUNTIME) ? toLOWER_LC(*s) :
3704 ((IN_UNI_8_BIT) ? toLOWER_LATIN1(*s) : toLOWER(*s));
3707 else if (IN_LOCALE_RUNTIME) {
3708 *tmpbuf = toUPPER_LC(*s); /* This would be a bug if any locales
3709 * have upper and title case different
3712 else if (! IN_UNI_8_BIT) {
3713 *tmpbuf = toUPPER(*s); /* Returns caseless for non-ascii, or
3714 * on EBCDIC machines whatever the
3715 * native function does */
3717 else { /* is ucfirst non-UTF-8, not in locale, and cased latin1 */
3718 *tmpbuf = toUPPER_LATIN1_MOD(*s);
3720 /* tmpbuf now has the correct title case for all latin1 characters
3721 * except for the several ones that have tricky handling. All
3722 * of these are mapped by the MOD to the letter below. */
3723 if (*tmpbuf == LATIN_SMALL_LETTER_Y_WITH_DIAERESIS) {
3725 /* The length is going to change, with all three of these, so
3726 * can't replace just the first character */
3729 /* We use the original to distinguish between these tricky
3731 if (*s == LATIN_SMALL_LETTER_SHARP_S) {
3732 /* Two character title case 'Ss', but can remain non-UTF-8 */
3735 *(tmpbuf + 1) = 's'; /* Assert: length(tmpbuf) >= 2 */
3740 /* The other two tricky ones have their title case outside
3741 * latin1. It is the same as their upper case. */
3743 STORE_NON_LATIN1_UC(tmpbuf, *s);
3745 /* The UTF-8 and UTF-EBCDIC lengths of both these characters
3746 * and their upper cases is 2. */
3749 /* The entire result will have to be in UTF-8. Assume worst
3750 * case sizing in conversion. (all latin1 characters occupy
3751 * at most two bytes in utf8) */
3752 convert_source_to_utf8 = TRUE;
3753 need = slen * 2 + 1;
3755 } /* End of is one of the three special chars */
3756 } /* End of use Unicode (Latin1) semantics */
3757 } /* End of changing the case of the first character */
3759 /* Here, have the first character's changed case stored in tmpbuf. Ready to
3760 * generate the result */
3763 /* We can convert in place. This means we change just the first
3764 * character without disturbing the rest; no need to grow */
3766 s = d = (U8*)SvPV_force_nomg(source, slen);
3772 /* Here, we can't convert in place; we earlier calculated how much
3773 * space we will need, so grow to accommodate that */
3774 SvUPGRADE(dest, SVt_PV);
3775 d = (U8*)SvGROW(dest, need);
3776 (void)SvPOK_only(dest);
3783 if (! convert_source_to_utf8) {
3785 /* Here both source and dest are in UTF-8, but have to create
3786 * the entire output. We initialize the result to be the
3787 * title/lower cased first character, and then append the rest
3789 sv_setpvn(dest, (char*)tmpbuf, tculen);
3791 sv_catpvn(dest, (char*)(s + ulen), slen - ulen);
3795 const U8 *const send = s + slen;
3797 /* Here the dest needs to be in UTF-8, but the source isn't,
3798 * except we earlier UTF-8'd the first character of the source
3799 * into tmpbuf. First put that into dest, and then append the
3800 * rest of the source, converting it to UTF-8 as we go. */
3802 /* Assert tculen is 2 here because the only two characters that
3803 * get to this part of the code have 2-byte UTF-8 equivalents */
3805 *d++ = *(tmpbuf + 1);
3806 s++; /* We have just processed the 1st char */
3808 for (; s < send; s++) {
3809 d = uvchr_to_utf8(d, *s);
3812 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3816 else { /* in-place UTF-8. Just overwrite the first character */
3817 Copy(tmpbuf, d, tculen, U8);
3818 SvCUR_set(dest, need - 1);
3821 else { /* Neither source nor dest are in or need to be UTF-8 */
3823 if (IN_LOCALE_RUNTIME) {
3827 if (inplace) { /* in-place, only need to change the 1st char */
3830 else { /* Not in-place */
3832 /* Copy the case-changed character(s) from tmpbuf */
3833 Copy(tmpbuf, d, tculen, U8);
3834 d += tculen - 1; /* Code below expects d to point to final
3835 * character stored */
3838 else { /* empty source */
3839 /* See bug #39028: Don't taint if empty */
3843 /* In a "use bytes" we don't treat the source as UTF-8, but, still want
3844 * the destination to retain that flag */
3848 if (!inplace) { /* Finish the rest of the string, unchanged */
3849 /* This will copy the trailing NUL */
3850 Copy(s + 1, d + 1, slen, U8);
3851 SvCUR_set(dest, need - 1);
3854 if (dest != source && SvTAINTED(source))
3860 /* There's so much setup/teardown code common between uc and lc, I wonder if
3861 it would be worth merging the two, and just having a switch outside each
3862 of the three tight loops. There is less and less commonality though */
3876 if (SvPADTMP(source) && !SvREADONLY(source) && !SvAMAGIC(source)
3877 && SvTEMP(source) && !DO_UTF8(source)
3878 && (IN_LOCALE_RUNTIME || ! IN_UNI_8_BIT)) {
3880 /* We can convert in place. The reason we can't if in UNI_8_BIT is to
3881 * make the loop tight, so we overwrite the source with the dest before
3882 * looking at it, and we need to look at the original source
3883 * afterwards. There would also need to be code added to handle
3884 * switching to not in-place in midstream if we run into characters
3885 * that change the length.
3888 s = d = (U8*)SvPV_force_nomg(source, len);
3895 /* The old implementation would copy source into TARG at this point.
3896 This had the side effect that if source was undef, TARG was now
3897 an undefined SV with PADTMP set, and they don't warn inside
3898 sv_2pv_flags(). However, we're now getting the PV direct from
3899 source, which doesn't have PADTMP set, so it would warn. Hence the
3903 s = (const U8*)SvPV_nomg_const(source, len);
3905 if (ckWARN(WARN_UNINITIALIZED))
3906 report_uninit(source);
3912 SvUPGRADE(dest, SVt_PV);
3913 d = (U8*)SvGROW(dest, min);
3914 (void)SvPOK_only(dest);
3919 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
3920 to check DO_UTF8 again here. */
3922 if (DO_UTF8(source)) {
3923 const U8 *const send = s + len;
3924 U8 tmpbuf[UTF8_MAXBYTES+1];
3926 /* All occurrences of these are to be moved to follow any other marks.
3927 * This is context-dependent. We may not be passed enough context to
3928 * move the iota subscript beyond all of them, but we do the best we can
3929 * with what we're given. The result is always better than if we
3930 * hadn't done this. And, the problem would only arise if we are
3931 * passed a character without all its combining marks, which would be
3932 * the caller's mistake. The information this is based on comes from a
3933 * comment in Unicode SpecialCasing.txt, (and the Standard's text
3934 * itself) and so can't be checked properly to see if it ever gets
3935 * revised. But the likelihood of it changing is remote */
3936 bool in_iota_subscript = FALSE;
3939 if (in_iota_subscript && ! is_utf8_mark(s)) {
3940 /* A non-mark. Time to output the iota subscript */
3941 #define GREEK_CAPITAL_LETTER_IOTA 0x0399
3942 #define COMBINING_GREEK_YPOGEGRAMMENI 0x0345
3944 CAT_UNI_TO_UTF8_TWO_BYTE(d, GREEK_CAPITAL_LETTER_IOTA);
3945 in_iota_subscript = FALSE;
3949 /* See comments at the first instance in this file of this ifdef */
3950 #ifdef GO_AHEAD_AND_BREAK_USER_DEFINED_CASE_MAPPINGS
3952 /* If the UTF-8 character is invariant, then it is in the range
3953 * known by the standard macro; result is only one byte long */
3954 if (UTF8_IS_INVARIANT(*s)) {
3958 else if (UTF8_IS_DOWNGRADEABLE_START(*s)) {
3960 /* Likewise, if it fits in a byte, its case change is in our
3962 U8 orig = TWO_BYTE_UTF8_TO_UNI(*s, *s++);
3963 U8 upper = toUPPER_LATIN1_MOD(orig);
3964 CAT_TWO_BYTE_UNI_UPPER_MOD(d, orig, upper);
3972 /* Otherwise, need the general UTF-8 case. Get the changed
3973 * case value and copy it to the output buffer */
3975 const STRLEN u = UTF8SKIP(s);
3978 const UV uv = toUPPER_utf8(s, tmpbuf, &ulen);
3979 if (uv == GREEK_CAPITAL_LETTER_IOTA
3980 && utf8_to_uvchr(s, 0) == COMBINING_GREEK_YPOGEGRAMMENI)
3982 in_iota_subscript = TRUE;
3985 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
3986 /* If the eventually required minimum size outgrows
3987 * the available space, we need to grow. */
3988 const UV o = d - (U8*)SvPVX_const(dest);
3990 /* If someone uppercases one million U+03B0s we
3991 * SvGROW() one million times. Or we could try
3992 * guessing how much to allocate without allocating too
3993 * much. Such is life. See corresponding comment in
3994 * lc code for another option */
3996 d = (U8*)SvPVX(dest) + o;
3998 Copy(tmpbuf, d, ulen, U8);
4004 if (in_iota_subscript) {
4005 CAT_UNI_TO_UTF8_TWO_BYTE(d, GREEK_CAPITAL_LETTER_IOTA);
4009 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4011 else { /* Not UTF-8 */
4013 const U8 *const send = s + len;
4015 /* Use locale casing if in locale; regular style if not treating
4016 * latin1 as having case; otherwise the latin1 casing. Do the
4017 * whole thing in a tight loop, for speed, */
4018 if (IN_LOCALE_RUNTIME) {
4021 for (; s < send; d++, s++)
4022 *d = toUPPER_LC(*s);
4024 else if (! IN_UNI_8_BIT) {
4025 for (; s < send; d++, s++) {
4030 for (; s < send; d++, s++) {
4031 *d = toUPPER_LATIN1_MOD(*s);
4032 if (*d != LATIN_SMALL_LETTER_Y_WITH_DIAERESIS) continue;
4034 /* The mainstream case is the tight loop above. To avoid
4035 * extra tests in that, all three characters that require
4036 * special handling are mapped by the MOD to the one tested
4038 * Use the source to distinguish between the three cases */
4040 if (*s == LATIN_SMALL_LETTER_SHARP_S) {
4042 /* uc() of this requires 2 characters, but they are
4043 * ASCII. If not enough room, grow the string */
4044 if (SvLEN(dest) < ++min) {
4045 const UV o = d - (U8*)SvPVX_const(dest);
4047 d = (U8*)SvPVX(dest) + o;
4049 *d++ = 'S'; *d = 'S'; /* upper case is 'SS' */
4050 continue; /* Back to the tight loop; still in ASCII */
4053 /* The other two special handling characters have their
4054 * upper cases outside the latin1 range, hence need to be
4055 * in UTF-8, so the whole result needs to be in UTF-8. So,
4056 * here we are somewhere in the middle of processing a
4057 * non-UTF-8 string, and realize that we will have to convert
4058 * the whole thing to UTF-8. What to do? There are
4059 * several possibilities. The simplest to code is to
4060 * convert what we have so far, set a flag, and continue on
4061 * in the loop. The flag would be tested each time through
4062 * the loop, and if set, the next character would be
4063 * converted to UTF-8 and stored. But, I (khw) didn't want
4064 * to slow down the mainstream case at all for this fairly
4065 * rare case, so I didn't want to add a test that didn't
4066 * absolutely have to be there in the loop, besides the
4067 * possibility that it would get too complicated for
4068 * optimizers to deal with. Another possibility is to just
4069 * give up, convert the source to UTF-8, and restart the
4070 * function that way. Another possibility is to convert
4071 * both what has already been processed and what is yet to
4072 * come separately to UTF-8, then jump into the loop that
4073 * handles UTF-8. But the most efficient time-wise of the
4074 * ones I could think of is what follows, and turned out to
4075 * not require much extra code. */
4077 /* Convert what we have so far into UTF-8, telling the
4078 * function that we know it should be converted, and to
4079 * allow extra space for what we haven't processed yet.
4080 * Assume the worst case space requirements for converting
4081 * what we haven't processed so far: that it will require
4082 * two bytes for each remaining source character, plus the
4083 * NUL at the end. This may cause the string pointer to
4084 * move, so re-find it. */
4086 len = d - (U8*)SvPVX_const(dest);
4087 SvCUR_set(dest, len);
4088 len = sv_utf8_upgrade_flags_grow(dest,
4089 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
4091 d = (U8*)SvPVX(dest) + len;
4093 /* And append the current character's upper case in UTF-8 */
4094 CAT_NON_LATIN1_UC(d, *s);
4096 /* Now process the remainder of the source, converting to
4097 * upper and UTF-8. If a resulting byte is invariant in
4098 * UTF-8, output it as-is, otherwise convert to UTF-8 and
4099 * append it to the output. */
4102 for (; s < send; s++) {
4103 U8 upper = toUPPER_LATIN1_MOD(*s);
4104 if UTF8_IS_INVARIANT(upper) {
4108 CAT_TWO_BYTE_UNI_UPPER_MOD(d, *s, upper);
4112 /* Here have processed the whole source; no need to continue
4113 * with the outer loop. Each character has been converted
4114 * to upper case and converted to UTF-8 */
4117 } /* End of processing all latin1-style chars */
4118 } /* End of processing all chars */
4119 } /* End of source is not empty */
4121 if (source != dest) {
4122 *d = '\0'; /* Here d points to 1 after last char, add NUL */
4123 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4125 } /* End of isn't utf8 */
4126 if (dest != source && SvTAINTED(source))
4145 if (SvPADTMP(source) && !SvREADONLY(source) && !SvAMAGIC(source)
4146 && SvTEMP(source) && !DO_UTF8(source)) {
4148 /* We can convert in place, as lowercasing anything in the latin1 range
4149 * (or else DO_UTF8 would have been on) doesn't lengthen it */
4151 s = d = (U8*)SvPV_force_nomg(source, len);
4158 /* The old implementation would copy source into TARG at this point.
4159 This had the side effect that if source was undef, TARG was now
4160 an undefined SV with PADTMP set, and they don't warn inside
4161 sv_2pv_flags(). However, we're now getting the PV direct from
4162 source, which doesn't have PADTMP set, so it would warn. Hence the
4166 s = (const U8*)SvPV_nomg_const(source, len);
4168 if (ckWARN(WARN_UNINITIALIZED))
4169 report_uninit(source);
4175 SvUPGRADE(dest, SVt_PV);
4176 d = (U8*)SvGROW(dest, min);
4177 (void)SvPOK_only(dest);
4182 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
4183 to check DO_UTF8 again here. */
4185 if (DO_UTF8(source)) {
4186 const U8 *const send = s + len;
4187 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
4190 /* See comments at the first instance in this file of this ifdef */
4191 #ifdef GO_AHEAD_AND_BREAK_USER_DEFINED_CASE_MAPPINGS
4192 if (UTF8_IS_INVARIANT(*s)) {
4194 /* Invariant characters use the standard mappings compiled in.
4199 else if (UTF8_IS_DOWNGRADEABLE_START(*s)) {
4201 /* As do the ones in the Latin1 range */
4202 U8 lower = toLOWER_LATIN1(TWO_BYTE_UTF8_TO_UNI(*s, *s++));
4203 CAT_UNI_TO_UTF8_TWO_BYTE(d, lower);
4208 /* Here, is utf8 not in Latin-1 range, have to go out and get
4209 * the mappings from the tables. */
4211 const STRLEN u = UTF8SKIP(s);
4214 #ifndef CONTEXT_DEPENDENT_CASING
4215 toLOWER_utf8(s, tmpbuf, &ulen);
4217 /* This is ifdefd out because it needs more work and thought. It isn't clear
4218 * that we should do it.
4219 * A minor objection is that this is based on a hard-coded rule from the
4220 * Unicode standard, and may change, but this is not very likely at all.
4221 * mktables should check and warn if it does.
4222 * More importantly, if the sigma occurs at the end of the string, we don't
4223 * have enough context to know whether it is part of a larger string or going
4224 * to be or not. It may be that we are passed a subset of the context, via
4225 * a \U...\E, for example, and we could conceivably know the larger context if
4226 * code were changed to pass that in. But, if the string passed in is an
4227 * intermediate result, and the user concatenates two strings together
4228 * after we have made a final sigma, that would be wrong. If the final sigma
4229 * occurs in the middle of the string we are working on, then we know that it
4230 * should be a final sigma, but otherwise we can't be sure. */
4232 const UV uv = toLOWER_utf8(s, tmpbuf, &ulen);
4234 /* If the lower case is a small sigma, it may be that we need
4235 * to change it to a final sigma. This happens at the end of
4236 * a word that contains more than just this character, and only
4237 * when we started with a capital sigma. */
4238 if (uv == UNICODE_GREEK_SMALL_LETTER_SIGMA &&
4239 s > send - len && /* Makes sure not the first letter */
4240 utf8_to_uvchr(s, 0) == UNICODE_GREEK_CAPITAL_LETTER_SIGMA
4243 /* We use the algorithm in:
4244 * http://www.unicode.org/versions/Unicode5.0.0/ch03.pdf (C
4245 * is a CAPITAL SIGMA): If C is preceded by a sequence
4246 * consisting of a cased letter and a case-ignorable
4247 * sequence, and C is not followed by a sequence consisting
4248 * of a case ignorable sequence and then a cased letter,
4249 * then when lowercasing C, C becomes a final sigma */
4251 /* To determine if this is the end of a word, need to peek
4252 * ahead. Look at the next character */
4253 const U8 *peek = s + u;
4255 /* Skip any case ignorable characters */
4256 while (peek < send && is_utf8_case_ignorable(peek)) {
4257 peek += UTF8SKIP(peek);
4260 /* If we reached the end of the string without finding any
4261 * non-case ignorable characters, or if the next such one
4262 * is not-cased, then we have met the conditions for it
4263 * being a final sigma with regards to peek ahead, and so
4264 * must do peek behind for the remaining conditions. (We
4265 * know there is stuff behind to look at since we tested
4266 * above that this isn't the first letter) */
4267 if (peek >= send || ! is_utf8_cased(peek)) {
4268 peek = utf8_hop(s, -1);
4270 /* Here are at the beginning of the first character
4271 * before the original upper case sigma. Keep backing
4272 * up, skipping any case ignorable characters */
4273 while (is_utf8_case_ignorable(peek)) {
4274 peek = utf8_hop(peek, -1);
4277 /* Here peek points to the first byte of the closest
4278 * non-case-ignorable character before the capital
4279 * sigma. If it is cased, then by the Unicode
4280 * algorithm, we should use a small final sigma instead
4281 * of what we have */
4282 if (is_utf8_cased(peek)) {
4283 STORE_UNI_TO_UTF8_TWO_BYTE(tmpbuf,
4284 UNICODE_GREEK_SMALL_LETTER_FINAL_SIGMA);
4288 else { /* Not a context sensitive mapping */
4289 #endif /* End of commented out context sensitive */
4290 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
4292 /* If the eventually required minimum size outgrows
4293 * the available space, we need to grow. */
4294 const UV o = d - (U8*)SvPVX_const(dest);
4296 /* If someone lowercases one million U+0130s we
4297 * SvGROW() one million times. Or we could try
4298 * guessing how much to allocate without allocating too
4299 * much. Such is life. Another option would be to
4300 * grow an extra byte or two more each time we need to
4301 * grow, which would cut down the million to 500K, with
4304 d = (U8*)SvPVX(dest) + o;
4306 #ifdef CONTEXT_DEPENDENT_CASING
4309 /* Copy the newly lowercased letter to the output buffer we're
4311 Copy(tmpbuf, d, ulen, U8);
4314 #ifdef GO_AHEAD_AND_BREAK_USER_DEFINED_CASE_MAPPINGS
4317 } /* End of looping through the source string */
4320 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4321 } else { /* Not utf8 */
4323 const U8 *const send = s + len;
4325 /* Use locale casing if in locale; regular style if not treating
4326 * latin1 as having case; otherwise the latin1 casing. Do the
4327 * whole thing in a tight loop, for speed, */
4328 if (IN_LOCALE_RUNTIME) {
4331 for (; s < send; d++, s++)
4332 *d = toLOWER_LC(*s);
4334 else if (! IN_UNI_8_BIT) {
4335 for (; s < send; d++, s++) {
4340 for (; s < send; d++, s++) {
4341 *d = toLOWER_LATIN1(*s);
4345 if (source != dest) {
4347 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4350 if (dest != source && SvTAINTED(source))
4359 SV * const sv = TOPs;
4361 register const char *s = SvPV_const(sv,len);
4363 SvUTF8_off(TARG); /* decontaminate */
4366 SvUPGRADE(TARG, SVt_PV);
4367 SvGROW(TARG, (len * 2) + 1);
4371 if (UTF8_IS_CONTINUED(*s)) {
4372 STRLEN ulen = UTF8SKIP(s);
4396 SvCUR_set(TARG, d - SvPVX_const(TARG));
4397 (void)SvPOK_only_UTF8(TARG);
4400 sv_setpvn(TARG, s, len);
4409 dVAR; dSP; dMARK; dORIGMARK;
4410 register AV *const av = MUTABLE_AV(POPs);
4411 register const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
4413 if (SvTYPE(av) == SVt_PVAV) {
4414 const I32 arybase = CopARYBASE_get(PL_curcop);
4415 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
4416 bool can_preserve = FALSE;
4422 can_preserve = SvCANEXISTDELETE(av);
4425 if (lval && localizing) {
4428 for (svp = MARK + 1; svp <= SP; svp++) {
4429 const I32 elem = SvIV(*svp);
4433 if (max > AvMAX(av))
4437 while (++MARK <= SP) {
4439 I32 elem = SvIV(*MARK);
4440 bool preeminent = TRUE;
4444 if (localizing && can_preserve) {
4445 /* If we can determine whether the element exist,
4446 * Try to preserve the existenceness of a tied array
4447 * element by using EXISTS and DELETE if possible.
4448 * Fallback to FETCH and STORE otherwise. */
4449 preeminent = av_exists(av, elem);
4452 svp = av_fetch(av, elem, lval);
4454 if (!svp || *svp == &PL_sv_undef)
4455 DIE(aTHX_ PL_no_aelem, elem);
4458 save_aelem(av, elem, svp);
4460 SAVEADELETE(av, elem);
4463 *MARK = svp ? *svp : &PL_sv_undef;
4466 if (GIMME != G_ARRAY) {
4468 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
4474 /* Smart dereferencing for keys, values and each */
4486 (SvTYPE(sv) != SVt_PVHV && SvTYPE(sv) != SVt_PVAV)
4491 "Type of argument to %s must be unblessed hashref or arrayref",
4492 PL_op_desc[PL_op->op_type] );
4495 if (PL_op->op_flags & OPf_SPECIAL && SvTYPE(sv) == SVt_PVAV)
4497 "Can't modify %s in %s",
4498 PL_op_desc[PL_op->op_type], PL_op_desc[PL_op->op_next->op_type]
4501 /* Delegate to correct function for op type */
4503 if (PL_op->op_type == OP_RKEYS || PL_op->op_type == OP_RVALUES) {
4504 return (SvTYPE(sv) == SVt_PVHV) ? Perl_do_kv(aTHX) : Perl_pp_akeys(aTHX);
4507 return (SvTYPE(sv) == SVt_PVHV) ? Perl_pp_each(aTHX) : Perl_pp_aeach(aTHX);
4515 AV *array = MUTABLE_AV(POPs);
4516 const I32 gimme = GIMME_V;
4517 IV *iterp = Perl_av_iter_p(aTHX_ array);
4518 const IV current = (*iterp)++;
4520 if (current > av_len(array)) {
4522 if (gimme == G_SCALAR)
4529 mPUSHi(CopARYBASE_get(PL_curcop) + current);
4530 if (gimme == G_ARRAY) {
4531 SV **const element = av_fetch(array, current, 0);
4532 PUSHs(element ? *element : &PL_sv_undef);
4541 AV *array = MUTABLE_AV(POPs);
4542 const I32 gimme = GIMME_V;
4544 *Perl_av_iter_p(aTHX_ array) = 0;
4546 if (gimme == G_SCALAR) {
4548 PUSHi(av_len(array) + 1);
4550 else if (gimme == G_ARRAY) {
4551 IV n = Perl_av_len(aTHX_ array);
4552 IV i = CopARYBASE_get(PL_curcop);
4556 if (PL_op->op_type == OP_AKEYS || PL_op->op_type == OP_RKEYS) {
4558 for (; i <= n; i++) {
4563 for (i = 0; i <= n; i++) {
4564 SV *const *const elem = Perl_av_fetch(aTHX_ array, i, 0);
4565 PUSHs(elem ? *elem : &PL_sv_undef);
4572 /* Associative arrays. */
4578 HV * hash = MUTABLE_HV(POPs);
4580 const I32 gimme = GIMME_V;
4583 /* might clobber stack_sp */
4584 entry = hv_iternext(hash);
4589 SV* const sv = hv_iterkeysv(entry);
4590 PUSHs(sv); /* won't clobber stack_sp */
4591 if (gimme == G_ARRAY) {
4594 /* might clobber stack_sp */
4595 val = hv_iterval(hash, entry);
4600 else if (gimme == G_SCALAR)
4607 S_do_delete_local(pTHX)
4611 const I32 gimme = GIMME_V;
4615 if (PL_op->op_private & OPpSLICE) {
4617 SV * const osv = POPs;
4618 const bool tied = SvRMAGICAL(osv)
4619 && mg_find((const SV *)osv, PERL_MAGIC_tied);
4620 const bool can_preserve = SvCANEXISTDELETE(osv)
4621 || mg_find((const SV *)osv, PERL_MAGIC_env);
4622 const U32 type = SvTYPE(osv);
4623 if (type == SVt_PVHV) { /* hash element */
4624 HV * const hv = MUTABLE_HV(osv);
4625 while (++MARK <= SP) {
4626 SV * const keysv = *MARK;
4628 bool preeminent = TRUE;
4630 preeminent = hv_exists_ent(hv, keysv, 0);
4632 HE *he = hv_fetch_ent(hv, keysv, 1, 0);
4639 sv = hv_delete_ent(hv, keysv, 0, 0);
4640 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4643 save_helem_flags(hv, keysv, &sv, SAVEf_KEEPOLDELEM);
4645 *MARK = sv_mortalcopy(sv);
4651 SAVEHDELETE(hv, keysv);
4652 *MARK = &PL_sv_undef;
4656 else if (type == SVt_PVAV) { /* array element */
4657 if (PL_op->op_flags & OPf_SPECIAL) {
4658 AV * const av = MUTABLE_AV(osv);
4659 while (++MARK <= SP) {
4660 I32 idx = SvIV(*MARK);
4662 bool preeminent = TRUE;
4664 preeminent = av_exists(av, idx);
4666 SV **svp = av_fetch(av, idx, 1);
4673 sv = av_delete(av, idx, 0);
4674 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4677 save_aelem_flags(av, idx, &sv, SAVEf_KEEPOLDELEM);
4679 *MARK = sv_mortalcopy(sv);
4685 SAVEADELETE(av, idx);
4686 *MARK = &PL_sv_undef;
4692 DIE(aTHX_ "Not a HASH reference");
4693 if (gimme == G_VOID)
4695 else if (gimme == G_SCALAR) {
4700 *++MARK = &PL_sv_undef;
4705 SV * const keysv = POPs;
4706 SV * const osv = POPs;
4707 const bool tied = SvRMAGICAL(osv)
4708 && mg_find((const SV *)osv, PERL_MAGIC_tied);
4709 const bool can_preserve = SvCANEXISTDELETE(osv)
4710 || mg_find((const SV *)osv, PERL_MAGIC_env);
4711 const U32 type = SvTYPE(osv);
4713 if (type == SVt_PVHV) {
4714 HV * const hv = MUTABLE_HV(osv);
4715 bool preeminent = TRUE;
4717 preeminent = hv_exists_ent(hv, keysv, 0);
4719 HE *he = hv_fetch_ent(hv, keysv, 1, 0);
4726 sv = hv_delete_ent(hv, keysv, 0, 0);
4727 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4730 save_helem_flags(hv, keysv, &sv, SAVEf_KEEPOLDELEM);
4732 SV *nsv = sv_mortalcopy(sv);
4738 SAVEHDELETE(hv, keysv);
4740 else if (type == SVt_PVAV) {
4741 if (PL_op->op_flags & OPf_SPECIAL) {
4742 AV * const av = MUTABLE_AV(osv);
4743 I32 idx = SvIV(keysv);
4744 bool preeminent = TRUE;
4746 preeminent = av_exists(av, idx);
4748 SV **svp = av_fetch(av, idx, 1);
4755 sv = av_delete(av, idx, 0);
4756 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4759 save_aelem_flags(av, idx, &sv, SAVEf_KEEPOLDELEM);
4761 SV *nsv = sv_mortalcopy(sv);
4767 SAVEADELETE(av, idx);
4770 DIE(aTHX_ "panic: avhv_delete no longer supported");
4773 DIE(aTHX_ "Not a HASH reference");
4776 if (gimme != G_VOID)
4790 if (PL_op->op_private & OPpLVAL_INTRO)
4791 return do_delete_local();
4794 discard = (gimme == G_VOID) ? G_DISCARD : 0;
4796 if (PL_op->op_private & OPpSLICE) {
4798 HV * const hv = MUTABLE_HV(POPs);
4799 const U32 hvtype = SvTYPE(hv);
4800 if (hvtype == SVt_PVHV) { /* hash element */
4801 while (++MARK <= SP) {
4802 SV * const sv = hv_delete_ent(hv, *MARK, discard, 0);
4803 *MARK = sv ? sv : &PL_sv_undef;
4806 else if (hvtype == SVt_PVAV) { /* array element */
4807 if (PL_op->op_flags & OPf_SPECIAL) {
4808 while (++MARK <= SP) {
4809 SV * const sv = av_delete(MUTABLE_AV(hv), SvIV(*MARK), discard);
4810 *MARK = sv ? sv : &PL_sv_undef;
4815 DIE(aTHX_ "Not a HASH reference");
4818 else if (gimme == G_SCALAR) {
4823 *++MARK = &PL_sv_undef;
4829 HV * const hv = MUTABLE_HV(POPs);
4831 if (SvTYPE(hv) == SVt_PVHV)
4832 sv = hv_delete_ent(hv, keysv, discard, 0);
4833 else if (SvTYPE(hv) == SVt_PVAV) {
4834 if (PL_op->op_flags & OPf_SPECIAL)
4835 sv = av_delete(MUTABLE_AV(hv), SvIV(keysv), discard);
4837 DIE(aTHX_ "panic: avhv_delete no longer supported");
4840 DIE(aTHX_ "Not a HASH reference");
4856 if (PL_op->op_private & OPpEXISTS_SUB) {
4858 SV * const sv = POPs;
4859 CV * const cv = sv_2cv(sv, &hv, &gv, 0);
4862 if (gv && isGV(gv) && GvCV(gv) && !GvCVGEN(gv))
4867 hv = MUTABLE_HV(POPs);
4868 if (SvTYPE(hv) == SVt_PVHV) {
4869 if (hv_exists_ent(hv, tmpsv, 0))
4872 else if (SvTYPE(hv) == SVt_PVAV) {
4873 if (PL_op->op_flags & OPf_SPECIAL) { /* array element */
4874 if (av_exists(MUTABLE_AV(hv), SvIV(tmpsv)))
4879 DIE(aTHX_ "Not a HASH reference");
4886 dVAR; dSP; dMARK; dORIGMARK;
4887 register HV * const hv = MUTABLE_HV(POPs);
4888 register const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
4889 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
4890 bool can_preserve = FALSE;
4896 if (SvCANEXISTDELETE(hv) || mg_find((const SV *)hv, PERL_MAGIC_env))
4897 can_preserve = TRUE;
4900 while (++MARK <= SP) {
4901 SV * const keysv = *MARK;
4904 bool preeminent = TRUE;
4906 if (localizing && can_preserve) {
4907 /* If we can determine whether the element exist,
4908 * try to preserve the existenceness of a tied hash
4909 * element by using EXISTS and DELETE if possible.
4910 * Fallback to FETCH and STORE otherwise. */
4911 preeminent = hv_exists_ent(hv, keysv, 0);
4914 he = hv_fetch_ent(hv, keysv, lval, 0);
4915 svp = he ? &HeVAL(he) : NULL;
4918 if (!svp || *svp == &PL_sv_undef) {
4919 DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
4922 if (HvNAME_get(hv) && isGV(*svp))
4923 save_gp(MUTABLE_GV(*svp), !(PL_op->op_flags & OPf_SPECIAL));
4924 else if (preeminent)
4925 save_helem_flags(hv, keysv, svp,
4926 (PL_op->op_flags & OPf_SPECIAL) ? 0 : SAVEf_SETMAGIC);
4928 SAVEHDELETE(hv, keysv);
4931 *MARK = svp ? *svp : &PL_sv_undef;
4933 if (GIMME != G_ARRAY) {
4935 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
4941 /* List operators. */
4946 if (GIMME != G_ARRAY) {
4948 *MARK = *SP; /* unwanted list, return last item */
4950 *MARK = &PL_sv_undef;
4960 SV ** const lastrelem = PL_stack_sp;
4961 SV ** const lastlelem = PL_stack_base + POPMARK;
4962 SV ** const firstlelem = PL_stack_base + POPMARK + 1;
4963 register SV ** const firstrelem = lastlelem + 1;
4964 const I32 arybase = CopARYBASE_get(PL_curcop);
4965 I32 is_something_there = FALSE;
4967 register const I32 max = lastrelem - lastlelem;
4968 register SV **lelem;
4970 if (GIMME != G_ARRAY) {
4971 I32 ix = SvIV(*lastlelem);
4976 if (ix < 0 || ix >= max)
4977 *firstlelem = &PL_sv_undef;
4979 *firstlelem = firstrelem[ix];
4985 SP = firstlelem - 1;
4989 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
4990 I32 ix = SvIV(*lelem);
4995 if (ix < 0 || ix >= max)
4996 *lelem = &PL_sv_undef;
4998 is_something_there = TRUE;
4999 if (!(*lelem = firstrelem[ix]))
5000 *lelem = &PL_sv_undef;
5003 if (is_something_there)
5006 SP = firstlelem - 1;
5012 dVAR; dSP; dMARK; dORIGMARK;
5013 const I32 items = SP - MARK;
5014 SV * const av = MUTABLE_SV(av_make(items, MARK+1));
5015 SP = ORIGMARK; /* av_make() might realloc stack_sp */
5016 mXPUSHs((PL_op->op_flags & OPf_SPECIAL)
5017 ? newRV_noinc(av) : av);
5023 dVAR; dSP; dMARK; dORIGMARK;
5024 HV* const hv = newHV();
5027 SV * const key = *++MARK;
5028 SV * const val = newSV(0);
5030 sv_setsv(val, *++MARK);
5032 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Odd number of elements in anonymous hash");
5033 (void)hv_store_ent(hv,key,val,0);
5036 mXPUSHs((PL_op->op_flags & OPf_SPECIAL)
5037 ? newRV_noinc(MUTABLE_SV(hv)) : MUTABLE_SV(hv));
5042 S_deref_plain_array(pTHX_ AV *ary)
5044 if (SvTYPE(ary) == SVt_PVAV) return ary;
5045 SvGETMAGIC((SV *)ary);
5046 if (!SvROK(ary) || SvTYPE(SvRV(ary)) != SVt_PVAV)
5047 Perl_die(aTHX_ "Not an ARRAY reference");
5048 else if (SvOBJECT(SvRV(ary)))
5049 Perl_die(aTHX_ "Not an unblessed ARRAY reference");
5050 return (AV *)SvRV(ary);
5053 #if defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
5054 # define DEREF_PLAIN_ARRAY(ary) \
5057 SvTYPE(aRrRay) == SVt_PVAV \
5059 : S_deref_plain_array(aTHX_ aRrRay); \
5062 # define DEREF_PLAIN_ARRAY(ary) \
5064 PL_Sv = (SV *)(ary), \
5065 SvTYPE(PL_Sv) == SVt_PVAV \
5067 : S_deref_plain_array(aTHX_ (AV *)PL_Sv) \
5073 dVAR; dSP; dMARK; dORIGMARK;
5074 int num_args = (SP - MARK);
5075 register AV *ary = DEREF_PLAIN_ARRAY(MUTABLE_AV(*++MARK));
5079 register I32 offset;
5080 register I32 length;
5084 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
5087 return Perl_tied_method(aTHX_ "SPLICE", mark - 1, MUTABLE_SV(ary), mg,
5088 GIMME_V | TIED_METHOD_ARGUMENTS_ON_STACK,
5095 offset = i = SvIV(*MARK);
5097 offset += AvFILLp(ary) + 1;
5099 offset -= CopARYBASE_get(PL_curcop);
5101 DIE(aTHX_ PL_no_aelem, i);
5103 length = SvIVx(*MARK++);
5105 length += AvFILLp(ary) - offset + 1;
5111 length = AvMAX(ary) + 1; /* close enough to infinity */
5115 length = AvMAX(ary) + 1;
5117 if (offset > AvFILLp(ary) + 1) {
5119 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "splice() offset past end of array" );
5120 offset = AvFILLp(ary) + 1;
5122 after = AvFILLp(ary) + 1 - (offset + length);
5123 if (after < 0) { /* not that much array */
5124 length += after; /* offset+length now in array */
5130 /* At this point, MARK .. SP-1 is our new LIST */
5133 diff = newlen - length;
5134 if (newlen && !AvREAL(ary) && AvREIFY(ary))
5137 /* make new elements SVs now: avoid problems if they're from the array */
5138 for (dst = MARK, i = newlen; i; i--) {
5139 SV * const h = *dst;
5140 *dst++ = newSVsv(h);
5143 if (diff < 0) { /* shrinking the area */
5144 SV **tmparyval = NULL;
5146 Newx(tmparyval, newlen, SV*); /* so remember insertion */
5147 Copy(MARK, tmparyval, newlen, SV*);
5150 MARK = ORIGMARK + 1;
5151 if (GIMME == G_ARRAY) { /* copy return vals to stack */
5152 MEXTEND(MARK, length);
5153 Copy(AvARRAY(ary)+offset, MARK, length, SV*);
5155 EXTEND_MORTAL(length);
5156 for (i = length, dst = MARK; i; i--) {
5157 sv_2mortal(*dst); /* free them eventually */
5164 *MARK = AvARRAY(ary)[offset+length-1];
5167 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
5168 SvREFCNT_dec(*dst++); /* free them now */
5171 AvFILLp(ary) += diff;
5173 /* pull up or down? */
5175 if (offset < after) { /* easier to pull up */
5176 if (offset) { /* esp. if nothing to pull */
5177 src = &AvARRAY(ary)[offset-1];
5178 dst = src - diff; /* diff is negative */
5179 for (i = offset; i > 0; i--) /* can't trust Copy */
5183 AvARRAY(ary) = AvARRAY(ary) - diff; /* diff is negative */
5187 if (after) { /* anything to pull down? */
5188 src = AvARRAY(ary) + offset + length;
5189 dst = src + diff; /* diff is negative */
5190 Move(src, dst, after, SV*);
5192 dst = &AvARRAY(ary)[AvFILLp(ary)+1];
5193 /* avoid later double free */
5197 dst[--i] = &PL_sv_undef;
5200 Copy( tmparyval, AvARRAY(ary) + offset, newlen, SV* );
5201 Safefree(tmparyval);
5204 else { /* no, expanding (or same) */
5205 SV** tmparyval = NULL;
5207 Newx(tmparyval, length, SV*); /* so remember deletion */
5208 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
5211 if (diff > 0) { /* expanding */
5212 /* push up or down? */
5213 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
5217 Move(src, dst, offset, SV*);
5219 AvARRAY(ary) = AvARRAY(ary) - diff;/* diff is positive */
5221 AvFILLp(ary) += diff;
5224 if (AvFILLp(ary) + diff >= AvMAX(ary)) /* oh, well */
5225 av_extend(ary, AvFILLp(ary) + diff);
5226 AvFILLp(ary) += diff;
5229 dst = AvARRAY(ary) + AvFILLp(ary);
5231 for (i = after; i; i--) {
5239 Copy( MARK, AvARRAY(ary) + offset, newlen, SV* );
5242 MARK = ORIGMARK + 1;
5243 if (GIMME == G_ARRAY) { /* copy return vals to stack */
5245 Copy(tmparyval, MARK, length, SV*);
5247 EXTEND_MORTAL(length);
5248 for (i = length, dst = MARK; i; i--) {
5249 sv_2mortal(*dst); /* free them eventually */
5256 else if (length--) {
5257 *MARK = tmparyval[length];
5260 while (length-- > 0)
5261 SvREFCNT_dec(tmparyval[length]);
5265 *MARK = &PL_sv_undef;
5266 Safefree(tmparyval);
5270 mg_set(MUTABLE_SV(ary));
5278 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
5279 register AV * const ary = DEREF_PLAIN_ARRAY(MUTABLE_AV(*++MARK));
5280 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
5283 *MARK-- = SvTIED_obj(MUTABLE_SV(ary), mg);
5286 ENTER_with_name("call_PUSH");
5287 call_method("PUSH",G_SCALAR|G_DISCARD);
5288 LEAVE_with_name("call_PUSH");
5292 PL_delaymagic = DM_DELAY;
5293 for (++MARK; MARK <= SP; MARK++) {
5294 SV * const sv = newSV(0);
5296 sv_setsv(sv, *MARK);
5297 av_store(ary, AvFILLp(ary)+1, sv);
5299 if (PL_delaymagic & DM_ARRAY_ISA)
5300 mg_set(MUTABLE_SV(ary));
5305 if (OP_GIMME(PL_op, 0) != G_VOID) {
5306 PUSHi( AvFILL(ary) + 1 );
5315 AV * const av = PL_op->op_flags & OPf_SPECIAL
5316 ? MUTABLE_AV(GvAV(PL_defgv)) : DEREF_PLAIN_ARRAY(MUTABLE_AV(POPs));
5317 SV * const sv = PL_op->op_type == OP_SHIFT ? av_shift(av) : av_pop(av);
5321 (void)sv_2mortal(sv);
5328 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
5329 register AV *ary = DEREF_PLAIN_ARRAY(MUTABLE_AV(*++MARK));
5330 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
5333 *MARK-- = SvTIED_obj(MUTABLE_SV(ary), mg);
5336 ENTER_with_name("call_UNSHIFT");
5337 call_method("UNSHIFT",G_SCALAR|G_DISCARD);
5338 LEAVE_with_name("call_UNSHIFT");
5343 av_unshift(ary, SP - MARK);
5345 SV * const sv = newSVsv(*++MARK);
5346 (void)av_store(ary, i++, sv);
5350 if (OP_GIMME(PL_op, 0) != G_VOID) {
5351 PUSHi( AvFILL(ary) + 1 );
5360 if (GIMME == G_ARRAY) {
5361 if (PL_op->op_private & OPpREVERSE_INPLACE) {
5365 assert( MARK+1 == SP && *SP && SvTYPE(*SP) == SVt_PVAV);
5366 (void)POPMARK; /* remove mark associated with ex-OP_AASSIGN */
5367 av = MUTABLE_AV((*SP));
5368 /* In-place reversing only happens in void context for the array
5369 * assignment. We don't need to push anything on the stack. */
5372 if (SvMAGICAL(av)) {
5374 register SV *tmp = sv_newmortal();
5375 /* For SvCANEXISTDELETE */
5378 bool can_preserve = SvCANEXISTDELETE(av);
5380 for (i = 0, j = av_len(av); i < j; ++i, --j) {
5381 register SV *begin, *end;
5384 if (!av_exists(av, i)) {
5385 if (av_exists(av, j)) {
5386 register SV *sv = av_delete(av, j, 0);
5387 begin = *av_fetch(av, i, TRUE);
5388 sv_setsv_mg(begin, sv);
5392 else if (!av_exists(av, j)) {
5393 register SV *sv = av_delete(av, i, 0);
5394 end = *av_fetch(av, j, TRUE);
5395 sv_setsv_mg(end, sv);
5400 begin = *av_fetch(av, i, TRUE);
5401 end = *av_fetch(av, j, TRUE);
5402 sv_setsv(tmp, begin);
5403 sv_setsv_mg(begin, end);
5404 sv_setsv_mg(end, tmp);
5408 SV **begin = AvARRAY(av);
5411 SV **end = begin + AvFILLp(av);
5413 while (begin < end) {
5414 register SV * const tmp = *begin;
5425 register SV * const tmp = *MARK;
5429 /* safe as long as stack cannot get extended in the above */
5435 register char *down;
5440 SvUTF8_off(TARG); /* decontaminate */
5442 do_join(TARG, &PL_sv_no, MARK, SP);
5444 sv_setsv(TARG, SP > MARK ? *SP : find_rundefsv());
5445 if (! SvOK(TARG) && ckWARN(WARN_UNINITIALIZED))
5446 report_uninit(TARG);
5449 up = SvPV_force(TARG, len);
5451 if (DO_UTF8(TARG)) { /* first reverse each character */
5452 U8* s = (U8*)SvPVX(TARG);
5453 const U8* send = (U8*)(s + len);
5455 if (UTF8_IS_INVARIANT(*s)) {
5460 if (!utf8_to_uvchr(s, 0))
5464 down = (char*)(s - 1);
5465 /* reverse this character */
5469 *down-- = (char)tmp;
5475 down = SvPVX(TARG) + len - 1;
5479 *down-- = (char)tmp;
5481 (void)SvPOK_only_UTF8(TARG);
5493 register IV limit = POPi; /* note, negative is forever */
5494 SV * const sv = POPs;
5496 register const char *s = SvPV_const(sv, len);
5497 const bool do_utf8 = DO_UTF8(sv);
5498 const char *strend = s + len;
5500 register REGEXP *rx;
5502 register const char *m;
5504 const STRLEN slen = do_utf8 ? utf8_length((U8*)s, (U8*)strend) : (STRLEN)(strend - s);
5505 I32 maxiters = slen + 10;
5506 I32 trailing_empty = 0;
5508 const I32 origlimit = limit;
5511 const I32 gimme = GIMME_V;
5513 const I32 oldsave = PL_savestack_ix;
5514 U32 make_mortal = SVs_TEMP;
5519 Copy(&LvTARGOFF(POPs), &pm, 1, PMOP*);
5524 DIE(aTHX_ "panic: pp_split");
5527 TAINT_IF(get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET &&
5528 (RX_EXTFLAGS(rx) & (RXf_WHITE | RXf_SKIPWHITE)));
5530 RX_MATCH_UTF8_set(rx, do_utf8);
5533 if (pm->op_pmreplrootu.op_pmtargetoff) {
5534 ary = GvAVn(MUTABLE_GV(PAD_SVl(pm->op_pmreplrootu.op_pmtargetoff)));
5537 if (pm->op_pmreplrootu.op_pmtargetgv) {
5538 ary = GvAVn(pm->op_pmreplrootu.op_pmtargetgv);
5543 if (ary && (gimme != G_ARRAY || (pm->op_pmflags & PMf_ONCE))) {
5549 if ((mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied))) {
5551 XPUSHs(SvTIED_obj(MUTABLE_SV(ary), mg));
5558 for (i = AvFILLp(ary); i >= 0; i--)
5559 AvARRAY(ary)[i] = &PL_sv_undef; /* don't free mere refs */
5561 /* temporarily switch stacks */
5562 SAVESWITCHSTACK(PL_curstack, ary);
5566 base = SP - PL_stack_base;
5568 if (RX_EXTFLAGS(rx) & RXf_SKIPWHITE) {
5570 while (*s == ' ' || is_utf8_space((U8*)s))
5573 else if (get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET) {
5574 while (isSPACE_LC(*s))
5582 if (RX_EXTFLAGS(rx) & RXf_PMf_MULTILINE) {
5586 gimme_scalar = gimme == G_SCALAR && !ary;
5589 limit = maxiters + 2;
5590 if (RX_EXTFLAGS(rx) & RXf_WHITE) {
5593 /* this one uses 'm' and is a negative test */
5595 while (m < strend && !( *m == ' ' || is_utf8_space((U8*)m) )) {
5596 const int t = UTF8SKIP(m);
5597 /* is_utf8_space returns FALSE for malform utf8 */
5604 else if (get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET) {
5605 while (m < strend && !isSPACE_LC(*m))
5608 while (m < strend && !isSPACE(*m))
5621 dstr = newSVpvn_flags(s, m-s,
5622 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5626 /* skip the whitespace found last */
5628 s = m + UTF8SKIP(m);
5632 /* this one uses 's' and is a positive test */
5634 while (s < strend && ( *s == ' ' || is_utf8_space((U8*)s) ))
5637 else if (get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET) {
5638 while (s < strend && isSPACE_LC(*s))
5641 while (s < strend && isSPACE(*s))
5646 else if (RX_EXTFLAGS(rx) & RXf_START_ONLY) {
5648 for (m = s; m < strend && *m != '\n'; m++)
5661 dstr = newSVpvn_flags(s, m-s,
5662 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5668 else if (RX_EXTFLAGS(rx) & RXf_NULL && !(s >= strend)) {
5670 Pre-extend the stack, either the number of bytes or
5671 characters in the string or a limited amount, triggered by:
5673 my ($x, $y) = split //, $str;
5677 if (!gimme_scalar) {
5678 const U32 items = limit - 1;
5687 /* keep track of how many bytes we skip over */
5697 dstr = newSVpvn_flags(m, s-m, SVf_UTF8 | make_mortal);
5710 dstr = newSVpvn(s, 1);
5726 else if (do_utf8 == (RX_UTF8(rx) != 0) &&
5727 (RX_EXTFLAGS(rx) & RXf_USE_INTUIT) && !RX_NPARENS(rx)
5728 && (RX_EXTFLAGS(rx) & RXf_CHECK_ALL)
5729 && !(RX_EXTFLAGS(rx) & RXf_ANCH)) {
5730 const int tail = (RX_EXTFLAGS(rx) & RXf_INTUIT_TAIL);
5731 SV * const csv = CALLREG_INTUIT_STRING(rx);
5733 len = RX_MINLENRET(rx);
5734 if (len == 1 && !RX_UTF8(rx) && !tail) {
5735 const char c = *SvPV_nolen_const(csv);
5737 for (m = s; m < strend && *m != c; m++)
5748 dstr = newSVpvn_flags(s, m-s,
5749 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5752 /* The rx->minlen is in characters but we want to step
5753 * s ahead by bytes. */
5755 s = (char*)utf8_hop((U8*)m, len);
5757 s = m + len; /* Fake \n at the end */
5761 while (s < strend && --limit &&
5762 (m = fbm_instr((unsigned char*)s, (unsigned char*)strend,
5763 csv, multiline ? FBMrf_MULTILINE : 0)) )
5772 dstr = newSVpvn_flags(s, m-s,
5773 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5776 /* The rx->minlen is in characters but we want to step
5777 * s ahead by bytes. */
5779 s = (char*)utf8_hop((U8*)m, len);
5781 s = m + len; /* Fake \n at the end */
5786 maxiters += slen * RX_NPARENS(rx);
5787 while (s < strend && --limit)
5791 rex_return = CALLREGEXEC(rx, (char*)s, (char*)strend, (char*)orig, 1 ,
5792 sv, NULL, SvSCREAM(sv) ? REXEC_SCREAM : 0);
5794 if (rex_return == 0)
5796 TAINT_IF(RX_MATCH_TAINTED(rx));
5797 if (RX_MATCH_COPIED(rx) && RX_SUBBEG(rx) != orig) {
5800 orig = RX_SUBBEG(rx);
5802 strend = s + (strend - m);
5804 m = RX_OFFS(rx)[0].start + orig;
5813 dstr = newSVpvn_flags(s, m-s,
5814 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5817 if (RX_NPARENS(rx)) {
5819 for (i = 1; i <= (I32)RX_NPARENS(rx); i++) {
5820 s = RX_OFFS(rx)[i].start + orig;
5821 m = RX_OFFS(rx)[i].end + orig;
5823 /* japhy (07/27/01) -- the (m && s) test doesn't catch
5824 parens that didn't match -- they should be set to
5825 undef, not the empty string */
5833 if (m >= orig && s >= orig) {
5834 dstr = newSVpvn_flags(s, m-s,
5835 (do_utf8 ? SVf_UTF8 : 0)
5839 dstr = &PL_sv_undef; /* undef, not "" */
5845 s = RX_OFFS(rx)[0].end + orig;
5849 if (!gimme_scalar) {
5850 iters = (SP - PL_stack_base) - base;
5852 if (iters > maxiters)
5853 DIE(aTHX_ "Split loop");
5855 /* keep field after final delim? */
5856 if (s < strend || (iters && origlimit)) {
5857 if (!gimme_scalar) {
5858 const STRLEN l = strend - s;
5859 dstr = newSVpvn_flags(s, l, (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5864 else if (!origlimit) {
5866 iters -= trailing_empty;
5868 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0)) {
5869 if (TOPs && !make_mortal)
5871 *SP-- = &PL_sv_undef;
5878 LEAVE_SCOPE(oldsave); /* may undo an earlier SWITCHSTACK */
5882 if (SvSMAGICAL(ary)) {
5884 mg_set(MUTABLE_SV(ary));
5887 if (gimme == G_ARRAY) {
5889 Copy(AvARRAY(ary), SP + 1, iters, SV*);
5896 ENTER_with_name("call_PUSH");
5897 call_method("PUSH",G_SCALAR|G_DISCARD);
5898 LEAVE_with_name("call_PUSH");
5900 if (gimme == G_ARRAY) {
5902 /* EXTEND should not be needed - we just popped them */
5904 for (i=0; i < iters; i++) {
5905 SV **svp = av_fetch(ary, i, FALSE);
5906 PUSHs((svp) ? *svp : &PL_sv_undef);
5913 if (gimme == G_ARRAY)
5925 SV *const sv = PAD_SVl(PL_op->op_targ);
5927 if (SvPADSTALE(sv)) {
5930 RETURNOP(cLOGOP->op_other);
5932 RETURNOP(cLOGOP->op_next);
5941 assert(SvTYPE(retsv) != SVt_PVCV);
5943 if (SvTYPE(retsv) == SVt_PVAV || SvTYPE(retsv) == SVt_PVHV) {
5944 retsv = refto(retsv);
5951 PP(unimplemented_op)
5954 const Optype op_type = PL_op->op_type;
5955 /* Using OP_NAME() isn't going to be helpful here. Firstly, it doesn't cope
5956 with out of range op numbers - it only "special" cases op_custom.
5957 Secondly, as the three ops we "panic" on are padmy, mapstart and custom,
5958 if we get here for a custom op then that means that the custom op didn't
5959 have an implementation. Given that OP_NAME() looks up the custom op
5960 by its pp_addr, likely it will return NULL, unless someone (unhelpfully)
5961 registers &PL_unimplemented_op as the address of their custom op.
5962 NULL doesn't generate a useful error message. "custom" does. */
5963 const char *const name = op_type >= OP_max
5964 ? "[out of range]" : PL_op_name[PL_op->op_type];
5965 if(OP_IS_SOCKET(op_type))
5966 DIE(aTHX_ PL_no_sock_func, name);
5967 DIE(aTHX_ "panic: unimplemented op %s (#%d) called", name, op_type);
5974 HV * const hv = (HV*)POPs;
5976 if (SvTYPE(hv) != SVt_PVHV) { XPUSHs(&PL_sv_no); RETURN; }
5978 if (SvRMAGICAL(hv)) {
5979 MAGIC * const mg = mg_find((SV*)hv, PERL_MAGIC_tied);
5981 XPUSHs(magic_scalarpack(hv, mg));
5986 XPUSHs(boolSV(HvUSEDKEYS(hv) != 0));
5992 * c-indentation-style: bsd
5994 * indent-tabs-mode: t
5997 * ex: set ts=8 sts=4 sw=4 noet: