3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4 * 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
12 * 'It's a big house this, and very peculiar. Always a bit more
13 * to discover, and no knowing what you'll find round a corner.
14 * And Elves, sir!' --Samwise Gamgee
16 * [p.225 of _The Lord of the Rings_, II/i: "Many Meetings"]
19 /* This file contains general pp ("push/pop") functions that execute the
20 * opcodes that make up a perl program. A typical pp function expects to
21 * find its arguments on the stack, and usually pushes its results onto
22 * the stack, hence the 'pp' terminology. Each OP structure contains
23 * a pointer to the relevant pp_foo() function.
33 /* XXX I can't imagine anyone who doesn't have this actually _needs_
34 it, since pid_t is an integral type.
37 #ifdef NEED_GETPID_PROTO
38 extern Pid_t getpid (void);
42 * Some BSDs and Cygwin default to POSIX math instead of IEEE.
43 * This switches them over to IEEE.
45 #if defined(LIBM_LIB_VERSION)
46 _LIB_VERSION_TYPE _LIB_VERSION = _IEEE_;
49 /* variations on pp_null */
55 if (GIMME_V == G_SCALAR)
66 assert(SvTYPE(TARG) == SVt_PVAV);
67 if (PL_op->op_private & OPpLVAL_INTRO)
68 if (!(PL_op->op_private & OPpPAD_STATE))
69 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
71 if (PL_op->op_flags & OPf_REF) {
74 } else if (PL_op->op_private & OPpMAYBE_LVSUB) {
75 const I32 flags = is_lvalue_sub();
76 if (flags && !(flags & OPpENTERSUB_INARGS)) {
77 if (GIMME == G_SCALAR)
78 /* diag_listed_as: Can't return %s to lvalue scalar context */
79 Perl_croak(aTHX_ "Can't return array to lvalue scalar context");
85 if (gimme == G_ARRAY) {
86 const I32 maxarg = AvFILL(MUTABLE_AV(TARG)) + 1;
88 if (SvMAGICAL(TARG)) {
90 for (i=0; i < (U32)maxarg; i++) {
91 SV * const * const svp = av_fetch(MUTABLE_AV(TARG), i, FALSE);
92 SP[i+1] = (svp) ? *svp : &PL_sv_undef;
96 Copy(AvARRAY((const AV *)TARG), SP+1, maxarg, SV*);
100 else if (gimme == G_SCALAR) {
101 SV* const sv = sv_newmortal();
102 const I32 maxarg = AvFILL(MUTABLE_AV(TARG)) + 1;
103 sv_setiv(sv, maxarg);
114 assert(SvTYPE(TARG) == SVt_PVHV);
116 if (PL_op->op_private & OPpLVAL_INTRO)
117 if (!(PL_op->op_private & OPpPAD_STATE))
118 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
119 if (PL_op->op_flags & OPf_REF)
121 else if (PL_op->op_private & OPpMAYBE_LVSUB) {
122 const I32 flags = is_lvalue_sub();
123 if (flags && !(flags & OPpENTERSUB_INARGS)) {
124 if (GIMME == G_SCALAR)
125 /* diag_listed_as: Can't return %s to lvalue scalar context */
126 Perl_croak(aTHX_ "Can't return hash to lvalue scalar context");
131 if (gimme == G_ARRAY) {
132 RETURNOP(Perl_do_kv(aTHX));
134 else if (gimme == G_SCALAR) {
135 SV* const sv = Perl_hv_scalar(aTHX_ MUTABLE_HV(TARG));
143 static const char S_no_symref_sv[] =
144 "Can't use string (\"%" SVf32 "\"%s) as %s ref while \"strict refs\" in use";
146 /* In some cases this function inspects PL_op. If this function is called
147 for new op types, more bool parameters may need to be added in place of
150 When noinit is true, the absence of a gv will cause a retval of undef.
151 This is unrelated to the cv-to-gv assignment case.
155 S_rv2gv(pTHX_ SV *sv, const bool vivify_sv, const bool strict,
159 if (!isGV(sv) || SvFAKE(sv)) SvGETMAGIC(sv);
162 sv = amagic_deref_call(sv, to_gv_amg);
166 if (SvTYPE(sv) == SVt_PVIO) {
167 GV * const gv = MUTABLE_GV(sv_newmortal());
168 gv_init(gv, 0, "__ANONIO__", 10, 0);
169 GvIOp(gv) = MUTABLE_IO(sv);
170 SvREFCNT_inc_void_NN(sv);
173 else if (!isGV_with_GP(sv))
174 return (SV *)Perl_die(aTHX_ "Not a GLOB reference");
177 if (!isGV_with_GP(sv)) {
179 /* If this is a 'my' scalar and flag is set then vivify
182 if (vivify_sv && sv != &PL_sv_undef) {
185 Perl_croak_no_modify(aTHX);
186 if (cUNOP->op_targ) {
187 SV * const namesv = PAD_SV(cUNOP->op_targ);
188 gv = MUTABLE_GV(newSV(0));
189 gv_init_sv(gv, CopSTASH(PL_curcop), namesv, 0);
192 const char * const name = CopSTASHPV(PL_curcop);
193 gv = newGVgen_flags(name,
194 HvNAMEUTF8(CopSTASH(PL_curcop)) ? SVf_UTF8 : 0 );
196 prepare_SV_for_RV(sv);
197 SvRV_set(sv, MUTABLE_SV(gv));
202 if (PL_op->op_flags & OPf_REF || strict)
203 return (SV *)Perl_die(aTHX_ PL_no_usym, "a symbol");
204 if (ckWARN(WARN_UNINITIALIZED))
210 if (!(sv = MUTABLE_SV(gv_fetchsv_nomg(
211 sv, GV_ADDMG, SVt_PVGV
221 (SvPOK(sv) && SvCUR(sv)>32 ? "..." : ""),
224 if ((PL_op->op_private & (OPpLVAL_INTRO|OPpDONT_INIT_GV))
225 == OPpDONT_INIT_GV) {
226 /* We are the target of a coderef assignment. Return
227 the scalar unchanged, and let pp_sasssign deal with
231 sv = MUTABLE_SV(gv_fetchsv_nomg(sv, GV_ADD, SVt_PVGV));
233 /* FAKE globs in the symbol table cause weird bugs (#77810) */
237 if (SvFAKE(sv) && !(PL_op->op_private & OPpALLOW_FAKE)) {
238 SV *newsv = sv_newmortal();
239 sv_setsv_flags(newsv, sv, 0);
251 sv, PL_op->op_private & OPpDEREF,
252 PL_op->op_private & HINT_STRICT_REFS,
253 ((PL_op->op_flags & OPf_SPECIAL) && !(PL_op->op_flags & OPf_MOD))
254 || PL_op->op_type == OP_READLINE
256 if (PL_op->op_private & OPpLVAL_INTRO)
257 save_gp(MUTABLE_GV(sv), !(PL_op->op_flags & OPf_SPECIAL));
262 /* Helper function for pp_rv2sv and pp_rv2av */
264 Perl_softref2xv(pTHX_ SV *const sv, const char *const what,
265 const svtype type, SV ***spp)
270 PERL_ARGS_ASSERT_SOFTREF2XV;
272 if (PL_op->op_private & HINT_STRICT_REFS) {
274 Perl_die(aTHX_ S_no_symref_sv, sv, (SvPOK(sv) && SvCUR(sv)>32 ? "..." : ""), what);
276 Perl_die(aTHX_ PL_no_usym, what);
280 PL_op->op_flags & OPf_REF &&
281 PL_op->op_next->op_type != OP_BOOLKEYS
283 Perl_die(aTHX_ PL_no_usym, what);
284 if (ckWARN(WARN_UNINITIALIZED))
286 if (type != SVt_PV && GIMME_V == G_ARRAY) {
290 **spp = &PL_sv_undef;
293 if ((PL_op->op_flags & OPf_SPECIAL) &&
294 !(PL_op->op_flags & OPf_MOD))
296 if (!(gv = gv_fetchsv_nomg(sv, GV_ADDMG, type)))
298 **spp = &PL_sv_undef;
303 gv = gv_fetchsv_nomg(sv, GV_ADD, type);
316 sv = amagic_deref_call(sv, to_sv_amg);
320 switch (SvTYPE(sv)) {
326 DIE(aTHX_ "Not a SCALAR reference");
333 if (!isGV_with_GP(gv)) {
334 gv = Perl_softref2xv(aTHX_ sv, "a SCALAR", SVt_PV, &sp);
340 if (PL_op->op_flags & OPf_MOD) {
341 if (PL_op->op_private & OPpLVAL_INTRO) {
342 if (cUNOP->op_first->op_type == OP_NULL)
343 sv = save_scalar(MUTABLE_GV(TOPs));
345 sv = save_scalar(gv);
347 Perl_croak(aTHX_ "%s", PL_no_localize_ref);
349 else if (PL_op->op_private & OPpDEREF)
350 sv = vivify_ref(sv, PL_op->op_private & OPpDEREF);
359 AV * const av = MUTABLE_AV(TOPs);
360 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
362 SV ** const sv = Perl_av_arylen_p(aTHX_ MUTABLE_AV(av));
364 *sv = newSV_type(SVt_PVMG);
365 sv_magic(*sv, MUTABLE_SV(av), PERL_MAGIC_arylen, NULL, 0);
369 SETs(sv_2mortal(newSViv(AvFILL(MUTABLE_AV(av)))));
378 if (PL_op->op_flags & OPf_MOD || LVRET) {
379 SV * const ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
380 sv_magic(ret, NULL, PERL_MAGIC_pos, NULL, 0);
382 LvTARG(ret) = SvREFCNT_inc_simple(sv);
383 PUSHs(ret); /* no SvSETMAGIC */
387 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
388 const MAGIC * const mg = mg_find(sv, PERL_MAGIC_regex_global);
389 if (mg && mg->mg_len >= 0) {
407 const I32 flags = (PL_op->op_flags & OPf_SPECIAL)
409 : ((PL_op->op_private & (OPpLVAL_INTRO|OPpMAY_RETURN_CONSTANT)) == OPpMAY_RETURN_CONSTANT)
412 /* We usually try to add a non-existent subroutine in case of AUTOLOAD. */
413 /* (But not in defined().) */
415 CV *cv = sv_2cv(TOPs, &stash_unused, &gv, flags);
417 else if ((flags == (GV_ADD|GV_NOEXPAND)) && gv && SvROK(gv)) {
421 cv = MUTABLE_CV(&PL_sv_undef);
422 SETs(MUTABLE_SV(cv));
432 SV *ret = &PL_sv_undef;
434 if (SvPOK(TOPs) && SvCUR(TOPs) >= 7) {
435 const char * s = SvPVX_const(TOPs);
436 if (strnEQ(s, "CORE::", 6)) {
437 const int code = keyword(s + 6, SvCUR(TOPs) - 6, 1);
438 if (!code || code == -KEY_CORE)
439 DIE(aTHX_ "Can't find an opnumber for \"%"SVf"\"",
440 SVfARG(newSVpvn_flags(
441 s+6, SvCUR(TOPs)-6, SvFLAGS(TOPs) & SVf_UTF8
444 SV * const sv = core_prototype(NULL, s + 6, code, NULL);
450 cv = sv_2cv(TOPs, &stash, &gv, 0);
452 ret = newSVpvn_flags(
453 CvPROTO(cv), CvPROTOLEN(cv), SVs_TEMP | SvUTF8(cv)
463 CV *cv = MUTABLE_CV(PAD_SV(PL_op->op_targ));
465 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
467 PUSHs(MUTABLE_SV(cv));
481 if (GIMME != G_ARRAY) {
485 *MARK = &PL_sv_undef;
486 *MARK = refto(*MARK);
490 EXTEND_MORTAL(SP - MARK);
492 *MARK = refto(*MARK);
497 S_refto(pTHX_ SV *sv)
502 PERL_ARGS_ASSERT_REFTO;
504 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
507 if (!(sv = LvTARG(sv)))
510 SvREFCNT_inc_void_NN(sv);
512 else if (SvTYPE(sv) == SVt_PVAV) {
513 if (!AvREAL((const AV *)sv) && AvREIFY((const AV *)sv))
514 av_reify(MUTABLE_AV(sv));
516 SvREFCNT_inc_void_NN(sv);
518 else if (SvPADTMP(sv) && !IS_PADGV(sv))
522 SvREFCNT_inc_void_NN(sv);
525 sv_upgrade(rv, SVt_IV);
534 SV * const sv = POPs;
539 if (!sv || !SvROK(sv))
542 (void)sv_ref(TARG,SvRV(sv),TRUE);
554 stash = CopSTASH(PL_curcop);
556 SV * const ssv = POPs;
560 if (!ssv) goto curstash;
561 if (!SvGMAGICAL(ssv) && !SvAMAGIC(ssv) && SvROK(ssv))
562 Perl_croak(aTHX_ "Attempt to bless into a reference");
563 ptr = SvPV_const(ssv,len);
565 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
566 "Explicit blessing to '' (assuming package main)");
567 stash = gv_stashpvn(ptr, len, GV_ADD|SvUTF8(ssv));
570 (void)sv_bless(TOPs, stash);
580 const char * const elem = SvPV_const(sv, len);
581 GV * const gv = MUTABLE_GV(POPs);
586 /* elem will always be NUL terminated. */
587 const char * const second_letter = elem + 1;
590 if (len == 5 && strEQ(second_letter, "RRAY"))
591 tmpRef = MUTABLE_SV(GvAV(gv));
594 if (len == 4 && strEQ(second_letter, "ODE"))
595 tmpRef = MUTABLE_SV(GvCVu(gv));
598 if (len == 10 && strEQ(second_letter, "ILEHANDLE")) {
599 /* finally deprecated in 5.8.0 */
600 deprecate("*glob{FILEHANDLE}");
601 tmpRef = MUTABLE_SV(GvIOp(gv));
604 if (len == 6 && strEQ(second_letter, "ORMAT"))
605 tmpRef = MUTABLE_SV(GvFORM(gv));
608 if (len == 4 && strEQ(second_letter, "LOB"))
609 tmpRef = MUTABLE_SV(gv);
612 if (len == 4 && strEQ(second_letter, "ASH"))
613 tmpRef = MUTABLE_SV(GvHV(gv));
616 if (*second_letter == 'O' && !elem[2] && len == 2)
617 tmpRef = MUTABLE_SV(GvIOp(gv));
620 if (len == 4 && strEQ(second_letter, "AME"))
621 sv = newSVhek(GvNAME_HEK(gv));
624 if (len == 7 && strEQ(second_letter, "ACKAGE")) {
625 const HV * const stash = GvSTASH(gv);
626 const HEK * const hek = stash ? HvNAME_HEK(stash) : NULL;
627 sv = hek ? newSVhek(hek) : newSVpvs("__ANON__");
631 if (len == 6 && strEQ(second_letter, "CALAR"))
646 /* Pattern matching */
651 register unsigned char *s;
654 MAGIC *mg = SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_study) : NULL;
658 if (mg && SvSCREAM(sv))
661 s = (unsigned char*)(SvPV(sv, len));
662 if (len == 0 || len > I32_MAX || !SvPOK(sv) || SvUTF8(sv) || SvVALID(sv)) {
663 /* No point in studying a zero length string, and not safe to study
664 anything that doesn't appear to be a simple scalar (and hence might
665 change between now and when the regexp engine runs without our set
666 magic ever running) such as a reference to an object with overloaded
667 stringification. Also refuse to study an FBM scalar, as this gives
668 more flexibility in SV flag usage. No real-world code would ever
669 end up studying an FBM scalar, so this isn't a real pessimisation.
670 Endemic use of I32 in Perl_screaminstr makes it hard to safely push
671 the study length limit from I32_MAX to U32_MAX - 1.
676 /* Make study a no-op. It's no longer useful and its existence
677 complicates matters elsewhere. This is a low-impact band-aid.
678 The relevant code will be neatly removed in a future release. */
683 } else if (len < 0xFFFF) {
688 size = (256 + len) * quanta;
689 sfirst_raw = (char *)safemalloc(size);
692 DIE(aTHX_ "do_study: out of memory");
696 mg = sv_magicext(sv, NULL, PERL_MAGIC_study, &PL_vtbl_regexp, NULL, 0);
697 mg->mg_ptr = sfirst_raw;
699 mg->mg_private = quanta;
701 memset(sfirst_raw, ~0, 256 * quanta);
703 /* The assumption here is that most studied strings are fairly short, hence
704 the pain of the extra code is worth it, given the memory savings.
705 80 character string, 336 bytes as U8, down from 1344 as U32
706 800 character string, 2112 bytes as U16, down from 4224 as U32
710 U8 *const sfirst = (U8 *)sfirst_raw;
711 U8 *const snext = sfirst + 256;
713 const U8 ch = s[len];
714 snext[len] = sfirst[ch];
717 } else if (quanta == 2) {
718 U16 *const sfirst = (U16 *)sfirst_raw;
719 U16 *const snext = sfirst + 256;
721 const U8 ch = s[len];
722 snext[len] = sfirst[ch];
726 U32 *const sfirst = (U32 *)sfirst_raw;
727 U32 *const snext = sfirst + 256;
729 const U8 ch = s[len];
730 snext[len] = sfirst[ch];
743 if (PL_op->op_flags & OPf_STACKED)
745 else if (PL_op->op_private & OPpTARGET_MY)
751 TARG = sv_newmortal();
752 if(PL_op->op_type == OP_TRANSR) {
754 const char * const pv = SvPV(sv,len);
755 SV * const newsv = newSVpvn_flags(pv, len, SVs_TEMP|SvUTF8(sv));
759 else PUSHi(do_trans(sv));
763 /* Lvalue operators. */
766 S_do_chomp(pTHX_ SV *retval, SV *sv, bool chomping)
772 PERL_ARGS_ASSERT_DO_CHOMP;
774 if (chomping && (RsSNARF(PL_rs) || RsRECORD(PL_rs)))
776 if (SvTYPE(sv) == SVt_PVAV) {
778 AV *const av = MUTABLE_AV(sv);
779 const I32 max = AvFILL(av);
781 for (i = 0; i <= max; i++) {
782 sv = MUTABLE_SV(av_fetch(av, i, FALSE));
783 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
784 do_chomp(retval, sv, chomping);
788 else if (SvTYPE(sv) == SVt_PVHV) {
789 HV* const hv = MUTABLE_HV(sv);
791 (void)hv_iterinit(hv);
792 while ((entry = hv_iternext(hv)))
793 do_chomp(retval, hv_iterval(hv,entry), chomping);
796 else if (SvREADONLY(sv)) {
798 /* SV is copy-on-write */
799 sv_force_normal_flags(sv, 0);
802 Perl_croak_no_modify(aTHX);
807 /* XXX, here sv is utf8-ized as a side-effect!
808 If encoding.pm is used properly, almost string-generating
809 operations, including literal strings, chr(), input data, etc.
810 should have been utf8-ized already, right?
812 sv_recode_to_utf8(sv, PL_encoding);
818 char *temp_buffer = NULL;
827 while (len && s[-1] == '\n') {
834 STRLEN rslen, rs_charlen;
835 const char *rsptr = SvPV_const(PL_rs, rslen);
837 rs_charlen = SvUTF8(PL_rs)
841 if (SvUTF8(PL_rs) != SvUTF8(sv)) {
842 /* Assumption is that rs is shorter than the scalar. */
844 /* RS is utf8, scalar is 8 bit. */
846 temp_buffer = (char*)bytes_from_utf8((U8*)rsptr,
849 /* Cannot downgrade, therefore cannot possibly match
851 assert (temp_buffer == rsptr);
857 else if (PL_encoding) {
858 /* RS is 8 bit, encoding.pm is used.
859 * Do not recode PL_rs as a side-effect. */
860 svrecode = newSVpvn(rsptr, rslen);
861 sv_recode_to_utf8(svrecode, PL_encoding);
862 rsptr = SvPV_const(svrecode, rslen);
863 rs_charlen = sv_len_utf8(svrecode);
866 /* RS is 8 bit, scalar is utf8. */
867 temp_buffer = (char*)bytes_to_utf8((U8*)rsptr, &rslen);
881 if (memNE(s, rsptr, rslen))
883 SvIVX(retval) += rs_charlen;
886 s = SvPV_force_nomg_nolen(sv);
894 SvREFCNT_dec(svrecode);
896 Safefree(temp_buffer);
898 if (len && !SvPOK(sv))
899 s = SvPV_force_nomg(sv, len);
902 char * const send = s + len;
903 char * const start = s;
905 while (s > start && UTF8_IS_CONTINUATION(*s))
907 if (is_utf8_string((U8*)s, send - s)) {
908 sv_setpvn(retval, s, send - s);
910 SvCUR_set(sv, s - start);
916 sv_setpvs(retval, "");
920 sv_setpvn(retval, s, 1);
927 sv_setpvs(retval, "");
935 const bool chomping = PL_op->op_type == OP_SCHOMP;
939 do_chomp(TARG, TOPs, chomping);
946 dVAR; dSP; dMARK; dTARGET; dORIGMARK;
947 const bool chomping = PL_op->op_type == OP_CHOMP;
952 do_chomp(TARG, *++MARK, chomping);
963 if (!PL_op->op_private) {
972 SV_CHECK_THINKFIRST_COW_DROP(sv);
974 switch (SvTYPE(sv)) {
978 av_undef(MUTABLE_AV(sv));
981 hv_undef(MUTABLE_HV(sv));
984 if (cv_const_sv((const CV *)sv))
985 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
986 "Constant subroutine %"SVf" undefined",
987 SVfARG(CvANON((const CV *)sv)
988 ? newSVpvs_flags("(anonymous)", SVs_TEMP)
989 : sv_2mortal(newSVhek(GvENAME_HEK(CvGV((const CV *)sv))))));
993 /* let user-undef'd sub keep its identity */
994 GV* const gv = CvGV((const CV *)sv);
995 cv_undef(MUTABLE_CV(sv));
996 CvGV_set(MUTABLE_CV(sv), gv);
1001 SvSetMagicSV(sv, &PL_sv_undef);
1004 else if (isGV_with_GP(sv)) {
1008 /* undef *Pkg::meth_name ... */
1010 = GvCVu((const GV *)sv) && (stash = GvSTASH((const GV *)sv))
1011 && HvENAME_get(stash);
1013 if((stash = GvHV((const GV *)sv))) {
1014 if(HvENAME_get(stash))
1015 SvREFCNT_inc_simple_void_NN(sv_2mortal((SV *)stash));
1019 gp_free(MUTABLE_GV(sv));
1021 GvGP_set(sv, gp_ref(gp));
1022 GvSV(sv) = newSV(0);
1023 GvLINE(sv) = CopLINE(PL_curcop);
1024 GvEGV(sv) = MUTABLE_GV(sv);
1028 mro_package_moved(NULL, stash, (const GV *)sv, 0);
1030 /* undef *Foo::ISA */
1031 if( strEQ(GvNAME((const GV *)sv), "ISA")
1032 && (stash = GvSTASH((const GV *)sv))
1033 && (method_changed || HvENAME(stash)) )
1034 mro_isa_changed_in(stash);
1035 else if(method_changed)
1036 mro_method_changed_in(
1037 GvSTASH((const GV *)sv)
1044 if (SvTYPE(sv) >= SVt_PV && SvPVX_const(sv) && SvLEN(sv)) {
1060 PL_op->op_type == OP_POSTINC || PL_op->op_type == OP_I_POSTINC;
1061 if (SvTYPE(TOPs) >= SVt_PVAV || (isGV_with_GP(TOPs) && !SvFAKE(TOPs)))
1062 Perl_croak_no_modify(aTHX);
1064 TARG = sv_newmortal();
1065 sv_setsv(TARG, TOPs);
1066 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
1067 && SvIVX(TOPs) != (inc ? IV_MAX : IV_MIN))
1069 SvIV_set(TOPs, SvIVX(TOPs) + (inc ? 1 : -1));
1070 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
1074 else sv_dec_nomg(TOPs);
1076 /* special case for undef: see thread at 2003-03/msg00536.html in archive */
1077 if (inc && !SvOK(TARG))
1083 /* Ordinary operators. */
1087 dVAR; dSP; dATARGET; SV *svl, *svr;
1088 #ifdef PERL_PRESERVE_IVUV
1091 tryAMAGICbin_MG(pow_amg, AMGf_assign|AMGf_numeric);
1094 #ifdef PERL_PRESERVE_IVUV
1095 /* For integer to integer power, we do the calculation by hand wherever
1096 we're sure it is safe; otherwise we call pow() and try to convert to
1097 integer afterwards. */
1099 SvIV_please_nomg(svr);
1101 SvIV_please_nomg(svl);
1110 const IV iv = SvIVX(svr);
1114 goto float_it; /* Can't do negative powers this way. */
1118 baseuok = SvUOK(svl);
1120 baseuv = SvUVX(svl);
1122 const IV iv = SvIVX(svl);
1125 baseuok = TRUE; /* effectively it's a UV now */
1127 baseuv = -iv; /* abs, baseuok == false records sign */
1130 /* now we have integer ** positive integer. */
1133 /* foo & (foo - 1) is zero only for a power of 2. */
1134 if (!(baseuv & (baseuv - 1))) {
1135 /* We are raising power-of-2 to a positive integer.
1136 The logic here will work for any base (even non-integer
1137 bases) but it can be less accurate than
1138 pow (base,power) or exp (power * log (base)) when the
1139 intermediate values start to spill out of the mantissa.
1140 With powers of 2 we know this can't happen.
1141 And powers of 2 are the favourite thing for perl
1142 programmers to notice ** not doing what they mean. */
1144 NV base = baseuok ? baseuv : -(NV)baseuv;
1149 while (power >>= 1) {
1157 SvIV_please_nomg(svr);
1160 register unsigned int highbit = 8 * sizeof(UV);
1161 register unsigned int diff = 8 * sizeof(UV);
1162 while (diff >>= 1) {
1164 if (baseuv >> highbit) {
1168 /* we now have baseuv < 2 ** highbit */
1169 if (power * highbit <= 8 * sizeof(UV)) {
1170 /* result will definitely fit in UV, so use UV math
1171 on same algorithm as above */
1172 register UV result = 1;
1173 register UV base = baseuv;
1174 const bool odd_power = cBOOL(power & 1);
1178 while (power >>= 1) {
1185 if (baseuok || !odd_power)
1186 /* answer is positive */
1188 else if (result <= (UV)IV_MAX)
1189 /* answer negative, fits in IV */
1190 SETi( -(IV)result );
1191 else if (result == (UV)IV_MIN)
1192 /* 2's complement assumption: special case IV_MIN */
1195 /* answer negative, doesn't fit */
1196 SETn( -(NV)result );
1206 NV right = SvNV_nomg(svr);
1207 NV left = SvNV_nomg(svl);
1210 #if defined(USE_LONG_DOUBLE) && defined(HAS_AIX_POWL_NEG_BASE_BUG)
1212 We are building perl with long double support and are on an AIX OS
1213 afflicted with a powl() function that wrongly returns NaNQ for any
1214 negative base. This was reported to IBM as PMR #23047-379 on
1215 03/06/2006. The problem exists in at least the following versions
1216 of AIX and the libm fileset, and no doubt others as well:
1218 AIX 4.3.3-ML10 bos.adt.libm 4.3.3.50
1219 AIX 5.1.0-ML04 bos.adt.libm 5.1.0.29
1220 AIX 5.2.0 bos.adt.libm 5.2.0.85
1222 So, until IBM fixes powl(), we provide the following workaround to
1223 handle the problem ourselves. Our logic is as follows: for
1224 negative bases (left), we use fmod(right, 2) to check if the
1225 exponent is an odd or even integer:
1227 - if odd, powl(left, right) == -powl(-left, right)
1228 - if even, powl(left, right) == powl(-left, right)
1230 If the exponent is not an integer, the result is rightly NaNQ, so
1231 we just return that (as NV_NAN).
1235 NV mod2 = Perl_fmod( right, 2.0 );
1236 if (mod2 == 1.0 || mod2 == -1.0) { /* odd integer */
1237 SETn( -Perl_pow( -left, right) );
1238 } else if (mod2 == 0.0) { /* even integer */
1239 SETn( Perl_pow( -left, right) );
1240 } else { /* fractional power */
1244 SETn( Perl_pow( left, right) );
1247 SETn( Perl_pow( left, right) );
1248 #endif /* HAS_AIX_POWL_NEG_BASE_BUG */
1250 #ifdef PERL_PRESERVE_IVUV
1252 SvIV_please_nomg(svr);
1260 dVAR; dSP; dATARGET; SV *svl, *svr;
1261 tryAMAGICbin_MG(mult_amg, AMGf_assign|AMGf_numeric);
1264 #ifdef PERL_PRESERVE_IVUV
1265 SvIV_please_nomg(svr);
1267 /* Unless the left argument is integer in range we are going to have to
1268 use NV maths. Hence only attempt to coerce the right argument if
1269 we know the left is integer. */
1270 /* Left operand is defined, so is it IV? */
1271 SvIV_please_nomg(svl);
1273 bool auvok = SvUOK(svl);
1274 bool buvok = SvUOK(svr);
1275 const UV topmask = (~ (UV)0) << (4 * sizeof (UV));
1276 const UV botmask = ~((~ (UV)0) << (4 * sizeof (UV)));
1285 const IV aiv = SvIVX(svl);
1288 auvok = TRUE; /* effectively it's a UV now */
1290 alow = -aiv; /* abs, auvok == false records sign */
1296 const IV biv = SvIVX(svr);
1299 buvok = TRUE; /* effectively it's a UV now */
1301 blow = -biv; /* abs, buvok == false records sign */
1305 /* If this does sign extension on unsigned it's time for plan B */
1306 ahigh = alow >> (4 * sizeof (UV));
1308 bhigh = blow >> (4 * sizeof (UV));
1310 if (ahigh && bhigh) {
1312 /* eg 32 bit is at least 0x10000 * 0x10000 == 0x100000000
1313 which is overflow. Drop to NVs below. */
1314 } else if (!ahigh && !bhigh) {
1315 /* eg 32 bit is at most 0xFFFF * 0xFFFF == 0xFFFE0001
1316 so the unsigned multiply cannot overflow. */
1317 const UV product = alow * blow;
1318 if (auvok == buvok) {
1319 /* -ve * -ve or +ve * +ve gives a +ve result. */
1323 } else if (product <= (UV)IV_MIN) {
1324 /* 2s complement assumption that (UV)-IV_MIN is correct. */
1325 /* -ve result, which could overflow an IV */
1327 SETi( -(IV)product );
1329 } /* else drop to NVs below. */
1331 /* One operand is large, 1 small */
1334 /* swap the operands */
1336 bhigh = blow; /* bhigh now the temp var for the swap */
1340 /* now, ((ahigh * blow) << half_UV_len) + (alow * blow)
1341 multiplies can't overflow. shift can, add can, -ve can. */
1342 product_middle = ahigh * blow;
1343 if (!(product_middle & topmask)) {
1344 /* OK, (ahigh * blow) won't lose bits when we shift it. */
1346 product_middle <<= (4 * sizeof (UV));
1347 product_low = alow * blow;
1349 /* as for pp_add, UV + something mustn't get smaller.
1350 IIRC ANSI mandates this wrapping *behaviour* for
1351 unsigned whatever the actual representation*/
1352 product_low += product_middle;
1353 if (product_low >= product_middle) {
1354 /* didn't overflow */
1355 if (auvok == buvok) {
1356 /* -ve * -ve or +ve * +ve gives a +ve result. */
1358 SETu( product_low );
1360 } else if (product_low <= (UV)IV_MIN) {
1361 /* 2s complement assumption again */
1362 /* -ve result, which could overflow an IV */
1364 SETi( -(IV)product_low );
1366 } /* else drop to NVs below. */
1368 } /* product_middle too large */
1369 } /* ahigh && bhigh */
1374 NV right = SvNV_nomg(svr);
1375 NV left = SvNV_nomg(svl);
1377 SETn( left * right );
1384 dVAR; dSP; dATARGET; SV *svl, *svr;
1385 tryAMAGICbin_MG(div_amg, AMGf_assign|AMGf_numeric);
1388 /* Only try to do UV divide first
1389 if ((SLOPPYDIVIDE is true) or
1390 (PERL_PRESERVE_IVUV is true and one or both SV is a UV too large
1392 The assumption is that it is better to use floating point divide
1393 whenever possible, only doing integer divide first if we can't be sure.
1394 If NV_PRESERVES_UV is true then we know at compile time that no UV
1395 can be too large to preserve, so don't need to compile the code to
1396 test the size of UVs. */
1399 # define PERL_TRY_UV_DIVIDE
1400 /* ensure that 20./5. == 4. */
1402 # ifdef PERL_PRESERVE_IVUV
1403 # ifndef NV_PRESERVES_UV
1404 # define PERL_TRY_UV_DIVIDE
1409 #ifdef PERL_TRY_UV_DIVIDE
1410 SvIV_please_nomg(svr);
1412 SvIV_please_nomg(svl);
1414 bool left_non_neg = SvUOK(svl);
1415 bool right_non_neg = SvUOK(svr);
1419 if (right_non_neg) {
1423 const IV biv = SvIVX(svr);
1426 right_non_neg = TRUE; /* effectively it's a UV now */
1432 /* historically undef()/0 gives a "Use of uninitialized value"
1433 warning before dieing, hence this test goes here.
1434 If it were immediately before the second SvIV_please, then
1435 DIE() would be invoked before left was even inspected, so
1436 no inspection would give no warning. */
1438 DIE(aTHX_ "Illegal division by zero");
1444 const IV aiv = SvIVX(svl);
1447 left_non_neg = TRUE; /* effectively it's a UV now */
1456 /* For sloppy divide we always attempt integer division. */
1458 /* Otherwise we only attempt it if either or both operands
1459 would not be preserved by an NV. If both fit in NVs
1460 we fall through to the NV divide code below. However,
1461 as left >= right to ensure integer result here, we know that
1462 we can skip the test on the right operand - right big
1463 enough not to be preserved can't get here unless left is
1466 && (left > ((UV)1 << NV_PRESERVES_UV_BITS))
1469 /* Integer division can't overflow, but it can be imprecise. */
1470 const UV result = left / right;
1471 if (result * right == left) {
1472 SP--; /* result is valid */
1473 if (left_non_neg == right_non_neg) {
1474 /* signs identical, result is positive. */
1478 /* 2s complement assumption */
1479 if (result <= (UV)IV_MIN)
1480 SETi( -(IV)result );
1482 /* It's exact but too negative for IV. */
1483 SETn( -(NV)result );
1486 } /* tried integer divide but it was not an integer result */
1487 } /* else (PERL_ABS(result) < 1.0) or (both UVs in range for NV) */
1488 } /* left wasn't SvIOK */
1489 } /* right wasn't SvIOK */
1490 #endif /* PERL_TRY_UV_DIVIDE */
1492 NV right = SvNV_nomg(svr);
1493 NV left = SvNV_nomg(svl);
1494 (void)POPs;(void)POPs;
1495 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
1496 if (! Perl_isnan(right) && right == 0.0)
1500 DIE(aTHX_ "Illegal division by zero");
1501 PUSHn( left / right );
1508 dVAR; dSP; dATARGET;
1509 tryAMAGICbin_MG(modulo_amg, AMGf_assign|AMGf_numeric);
1513 bool left_neg = FALSE;
1514 bool right_neg = FALSE;
1515 bool use_double = FALSE;
1516 bool dright_valid = FALSE;
1519 SV * const svr = TOPs;
1520 SV * const svl = TOPm1s;
1521 SvIV_please_nomg(svr);
1523 right_neg = !SvUOK(svr);
1527 const IV biv = SvIVX(svr);
1530 right_neg = FALSE; /* effectively it's a UV now */
1537 dright = SvNV_nomg(svr);
1538 right_neg = dright < 0;
1541 if (dright < UV_MAX_P1) {
1542 right = U_V(dright);
1543 dright_valid = TRUE; /* In case we need to use double below. */
1549 /* At this point use_double is only true if right is out of range for
1550 a UV. In range NV has been rounded down to nearest UV and
1551 use_double false. */
1552 SvIV_please_nomg(svl);
1553 if (!use_double && SvIOK(svl)) {
1555 left_neg = !SvUOK(svl);
1559 const IV aiv = SvIVX(svl);
1562 left_neg = FALSE; /* effectively it's a UV now */
1570 dleft = SvNV_nomg(svl);
1571 left_neg = dleft < 0;
1575 /* This should be exactly the 5.6 behaviour - if left and right are
1576 both in range for UV then use U_V() rather than floor. */
1578 if (dleft < UV_MAX_P1) {
1579 /* right was in range, so is dleft, so use UVs not double.
1583 /* left is out of range for UV, right was in range, so promote
1584 right (back) to double. */
1586 /* The +0.5 is used in 5.6 even though it is not strictly
1587 consistent with the implicit +0 floor in the U_V()
1588 inside the #if 1. */
1589 dleft = Perl_floor(dleft + 0.5);
1592 dright = Perl_floor(dright + 0.5);
1603 DIE(aTHX_ "Illegal modulus zero");
1605 dans = Perl_fmod(dleft, dright);
1606 if ((left_neg != right_neg) && dans)
1607 dans = dright - dans;
1610 sv_setnv(TARG, dans);
1616 DIE(aTHX_ "Illegal modulus zero");
1619 if ((left_neg != right_neg) && ans)
1622 /* XXX may warn: unary minus operator applied to unsigned type */
1623 /* could change -foo to be (~foo)+1 instead */
1624 if (ans <= ~((UV)IV_MAX)+1)
1625 sv_setiv(TARG, ~ans+1);
1627 sv_setnv(TARG, -(NV)ans);
1630 sv_setuv(TARG, ans);
1639 dVAR; dSP; dATARGET;
1643 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1644 /* TODO: think of some way of doing list-repeat overloading ??? */
1649 tryAMAGICbin_MG(repeat_amg, AMGf_assign);
1655 const UV uv = SvUV_nomg(sv);
1657 count = IV_MAX; /* The best we can do? */
1661 const IV iv = SvIV_nomg(sv);
1668 else if (SvNOKp(sv)) {
1669 const NV nv = SvNV_nomg(sv);
1676 count = SvIV_nomg(sv);
1678 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1680 static const char oom_list_extend[] = "Out of memory during list extend";
1681 const I32 items = SP - MARK;
1682 const I32 max = items * count;
1684 MEM_WRAP_CHECK_1(max, SV*, oom_list_extend);
1685 /* Did the max computation overflow? */
1686 if (items > 0 && max > 0 && (max < items || max < count))
1687 Perl_croak(aTHX_ oom_list_extend);
1692 /* This code was intended to fix 20010809.028:
1695 for (($x =~ /./g) x 2) {
1696 print chop; # "abcdabcd" expected as output.
1699 * but that change (#11635) broke this code:
1701 $x = [("foo")x2]; # only one "foo" ended up in the anonlist.
1703 * I can't think of a better fix that doesn't introduce
1704 * an efficiency hit by copying the SVs. The stack isn't
1705 * refcounted, and mortalisation obviously doesn't
1706 * Do The Right Thing when the stack has more than
1707 * one pointer to the same mortal value.
1711 *SP = sv_2mortal(newSVsv(*SP));
1721 repeatcpy((char*)(MARK + items), (char*)MARK,
1722 items * sizeof(const SV *), count - 1);
1725 else if (count <= 0)
1728 else { /* Note: mark already snarfed by pp_list */
1729 SV * const tmpstr = POPs;
1732 static const char oom_string_extend[] =
1733 "Out of memory during string extend";
1736 sv_setsv_nomg(TARG, tmpstr);
1737 SvPV_force_nomg(TARG, len);
1738 isutf = DO_UTF8(TARG);
1743 const STRLEN max = (UV)count * len;
1744 if (len > MEM_SIZE_MAX / count)
1745 Perl_croak(aTHX_ oom_string_extend);
1746 MEM_WRAP_CHECK_1(max, char, oom_string_extend);
1747 SvGROW(TARG, max + 1);
1748 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
1749 SvCUR_set(TARG, SvCUR(TARG) * count);
1751 *SvEND(TARG) = '\0';
1754 (void)SvPOK_only_UTF8(TARG);
1756 (void)SvPOK_only(TARG);
1758 if (PL_op->op_private & OPpREPEAT_DOLIST) {
1759 /* The parser saw this as a list repeat, and there
1760 are probably several items on the stack. But we're
1761 in scalar context, and there's no pp_list to save us
1762 now. So drop the rest of the items -- robin@kitsite.com
1774 dVAR; dSP; dATARGET; bool useleft; SV *svl, *svr;
1775 tryAMAGICbin_MG(subtr_amg, AMGf_assign|AMGf_numeric);
1778 useleft = USE_LEFT(svl);
1779 #ifdef PERL_PRESERVE_IVUV
1780 /* See comments in pp_add (in pp_hot.c) about Overflow, and how
1781 "bad things" happen if you rely on signed integers wrapping. */
1782 SvIV_please_nomg(svr);
1784 /* Unless the left argument is integer in range we are going to have to
1785 use NV maths. Hence only attempt to coerce the right argument if
1786 we know the left is integer. */
1787 register UV auv = 0;
1793 a_valid = auvok = 1;
1794 /* left operand is undef, treat as zero. */
1796 /* Left operand is defined, so is it IV? */
1797 SvIV_please_nomg(svl);
1799 if ((auvok = SvUOK(svl)))
1802 register const IV aiv = SvIVX(svl);
1805 auvok = 1; /* Now acting as a sign flag. */
1806 } else { /* 2s complement assumption for IV_MIN */
1814 bool result_good = 0;
1817 bool buvok = SvUOK(svr);
1822 register const IV biv = SvIVX(svr);
1829 /* ?uvok if value is >= 0. basically, flagged as UV if it's +ve,
1830 else "IV" now, independent of how it came in.
1831 if a, b represents positive, A, B negative, a maps to -A etc
1836 all UV maths. negate result if A negative.
1837 subtract if signs same, add if signs differ. */
1839 if (auvok ^ buvok) {
1848 /* Must get smaller */
1853 if (result <= buv) {
1854 /* result really should be -(auv-buv). as its negation
1855 of true value, need to swap our result flag */
1867 if (result <= (UV)IV_MIN)
1868 SETi( -(IV)result );
1870 /* result valid, but out of range for IV. */
1871 SETn( -(NV)result );
1875 } /* Overflow, drop through to NVs. */
1880 NV value = SvNV_nomg(svr);
1884 /* left operand is undef, treat as zero - value */
1888 SETn( SvNV_nomg(svl) - value );
1895 dVAR; dSP; dATARGET; SV *svl, *svr;
1896 tryAMAGICbin_MG(lshift_amg, AMGf_assign|AMGf_numeric);
1900 const IV shift = SvIV_nomg(svr);
1901 if (PL_op->op_private & HINT_INTEGER) {
1902 const IV i = SvIV_nomg(svl);
1906 const UV u = SvUV_nomg(svl);
1915 dVAR; dSP; dATARGET; SV *svl, *svr;
1916 tryAMAGICbin_MG(rshift_amg, AMGf_assign|AMGf_numeric);
1920 const IV shift = SvIV_nomg(svr);
1921 if (PL_op->op_private & HINT_INTEGER) {
1922 const IV i = SvIV_nomg(svl);
1926 const UV u = SvUV_nomg(svl);
1938 tryAMAGICbin_MG(lt_amg, AMGf_set|AMGf_numeric);
1942 (SvIOK_notUV(left) && SvIOK_notUV(right))
1943 ? (SvIVX(left) < SvIVX(right))
1944 : (do_ncmp(left, right) == -1)
1954 tryAMAGICbin_MG(gt_amg, AMGf_set|AMGf_numeric);
1958 (SvIOK_notUV(left) && SvIOK_notUV(right))
1959 ? (SvIVX(left) > SvIVX(right))
1960 : (do_ncmp(left, right) == 1)
1970 tryAMAGICbin_MG(le_amg, AMGf_set|AMGf_numeric);
1974 (SvIOK_notUV(left) && SvIOK_notUV(right))
1975 ? (SvIVX(left) <= SvIVX(right))
1976 : (do_ncmp(left, right) <= 0)
1986 tryAMAGICbin_MG(ge_amg, AMGf_set|AMGf_numeric);
1990 (SvIOK_notUV(left) && SvIOK_notUV(right))
1991 ? (SvIVX(left) >= SvIVX(right))
1992 : ( (do_ncmp(left, right) & 2) == 0)
2002 tryAMAGICbin_MG(ne_amg, AMGf_set|AMGf_numeric);
2006 (SvIOK_notUV(left) && SvIOK_notUV(right))
2007 ? (SvIVX(left) != SvIVX(right))
2008 : (do_ncmp(left, right) != 0)
2013 /* compare left and right SVs. Returns:
2017 * 2: left or right was a NaN
2020 Perl_do_ncmp(pTHX_ SV* const left, SV * const right)
2024 PERL_ARGS_ASSERT_DO_NCMP;
2025 #ifdef PERL_PRESERVE_IVUV
2026 SvIV_please_nomg(right);
2027 /* Fortunately it seems NaN isn't IOK */
2029 SvIV_please_nomg(left);
2032 const IV leftiv = SvIVX(left);
2033 if (!SvUOK(right)) {
2034 /* ## IV <=> IV ## */
2035 const IV rightiv = SvIVX(right);
2036 return (leftiv > rightiv) - (leftiv < rightiv);
2038 /* ## IV <=> UV ## */
2040 /* As (b) is a UV, it's >=0, so it must be < */
2043 const UV rightuv = SvUVX(right);
2044 return ((UV)leftiv > rightuv) - ((UV)leftiv < rightuv);
2049 /* ## UV <=> UV ## */
2050 const UV leftuv = SvUVX(left);
2051 const UV rightuv = SvUVX(right);
2052 return (leftuv > rightuv) - (leftuv < rightuv);
2054 /* ## UV <=> IV ## */
2056 const IV rightiv = SvIVX(right);
2058 /* As (a) is a UV, it's >=0, so it cannot be < */
2061 const UV leftuv = SvUVX(left);
2062 return (leftuv > (UV)rightiv) - (leftuv < (UV)rightiv);
2070 NV const rnv = SvNV_nomg(right);
2071 NV const lnv = SvNV_nomg(left);
2073 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
2074 if (Perl_isnan(lnv) || Perl_isnan(rnv)) {
2077 return (lnv > rnv) - (lnv < rnv);
2096 tryAMAGICbin_MG(ncmp_amg, AMGf_numeric);
2099 value = do_ncmp(left, right);
2114 int amg_type = sle_amg;
2118 switch (PL_op->op_type) {
2137 tryAMAGICbin_MG(amg_type, AMGf_set);
2140 const int cmp = (IN_LOCALE_RUNTIME
2141 ? sv_cmp_locale_flags(left, right, 0)
2142 : sv_cmp_flags(left, right, 0));
2143 SETs(boolSV(cmp * multiplier < rhs));
2151 tryAMAGICbin_MG(seq_amg, AMGf_set);
2154 SETs(boolSV(sv_eq_flags(left, right, 0)));
2162 tryAMAGICbin_MG(sne_amg, AMGf_set);
2165 SETs(boolSV(!sv_eq_flags(left, right, 0)));
2173 tryAMAGICbin_MG(scmp_amg, 0);
2176 const int cmp = (IN_LOCALE_RUNTIME
2177 ? sv_cmp_locale_flags(left, right, 0)
2178 : sv_cmp_flags(left, right, 0));
2186 dVAR; dSP; dATARGET;
2187 tryAMAGICbin_MG(band_amg, AMGf_assign);
2190 if (SvNIOKp(left) || SvNIOKp(right)) {
2191 const bool left_ro_nonnum = !SvNIOKp(left) && SvREADONLY(left);
2192 const bool right_ro_nonnum = !SvNIOKp(right) && SvREADONLY(right);
2193 if (PL_op->op_private & HINT_INTEGER) {
2194 const IV i = SvIV_nomg(left) & SvIV_nomg(right);
2198 const UV u = SvUV_nomg(left) & SvUV_nomg(right);
2201 if (left_ro_nonnum && left != TARG) SvNIOK_off(left);
2202 if (right_ro_nonnum) SvNIOK_off(right);
2205 do_vop(PL_op->op_type, TARG, left, right);
2214 dVAR; dSP; dATARGET;
2215 const int op_type = PL_op->op_type;
2217 tryAMAGICbin_MG((op_type == OP_BIT_OR ? bor_amg : bxor_amg), AMGf_assign);
2220 if (SvNIOKp(left) || SvNIOKp(right)) {
2221 const bool left_ro_nonnum = !SvNIOKp(left) && SvREADONLY(left);
2222 const bool right_ro_nonnum = !SvNIOKp(right) && SvREADONLY(right);
2223 if (PL_op->op_private & HINT_INTEGER) {
2224 const IV l = (USE_LEFT(left) ? SvIV_nomg(left) : 0);
2225 const IV r = SvIV_nomg(right);
2226 const IV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2230 const UV l = (USE_LEFT(left) ? SvUV_nomg(left) : 0);
2231 const UV r = SvUV_nomg(right);
2232 const UV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2235 if (left_ro_nonnum && left != TARG) SvNIOK_off(left);
2236 if (right_ro_nonnum) SvNIOK_off(right);
2239 do_vop(op_type, TARG, left, right);
2249 tryAMAGICun_MG(neg_amg, AMGf_numeric);
2251 SV * const sv = TOPs;
2252 const int flags = SvFLAGS(sv);
2254 if( !SvNIOK( sv ) && looks_like_number( sv ) ){
2258 if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
2259 /* It's publicly an integer, or privately an integer-not-float */
2262 if (SvIVX(sv) == IV_MIN) {
2263 /* 2s complement assumption. */
2264 SETi(SvIVX(sv)); /* special case: -((UV)IV_MAX+1) == IV_MIN */
2267 else if (SvUVX(sv) <= IV_MAX) {
2272 else if (SvIVX(sv) != IV_MIN) {
2276 #ifdef PERL_PRESERVE_IVUV
2284 SETn(-SvNV_nomg(sv));
2285 else if (SvPOKp(sv)) {
2287 const char * const s = SvPV_nomg_const(sv, len);
2288 if (isIDFIRST(*s)) {
2289 sv_setpvs(TARG, "-");
2292 else if (*s == '+' || *s == '-') {
2293 sv_setsv_nomg(TARG, sv);
2294 *SvPV_force_nomg(TARG, len) = *s == '-' ? '+' : '-';
2296 else if (DO_UTF8(sv)) {
2297 SvIV_please_nomg(sv);
2299 goto oops_its_an_int;
2301 sv_setnv(TARG, -SvNV_nomg(sv));
2303 sv_setpvs(TARG, "-");
2308 SvIV_please_nomg(sv);
2310 goto oops_its_an_int;
2311 sv_setnv(TARG, -SvNV_nomg(sv));
2316 SETn(-SvNV_nomg(sv));
2324 tryAMAGICun_MG(not_amg, AMGf_set);
2325 *PL_stack_sp = boolSV(!SvTRUE_nomg(*PL_stack_sp));
2332 tryAMAGICun_MG(compl_amg, AMGf_numeric);
2336 if (PL_op->op_private & HINT_INTEGER) {
2337 const IV i = ~SvIV_nomg(sv);
2341 const UV u = ~SvUV_nomg(sv);
2350 (void)SvPV_nomg_const(sv,len); /* force check for uninit var */
2351 sv_setsv_nomg(TARG, sv);
2352 tmps = (U8*)SvPV_force_nomg(TARG, len);
2355 /* Calculate exact length, let's not estimate. */
2360 U8 * const send = tmps + len;
2361 U8 * const origtmps = tmps;
2362 const UV utf8flags = UTF8_ALLOW_ANYUV;
2364 while (tmps < send) {
2365 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2367 targlen += UNISKIP(~c);
2373 /* Now rewind strings and write them. */
2380 Newx(result, targlen + 1, U8);
2382 while (tmps < send) {
2383 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2385 p = uvchr_to_utf8_flags(p, ~c, UNICODE_ALLOW_ANY);
2388 sv_usepvn_flags(TARG, (char*)result, targlen,
2389 SV_HAS_TRAILING_NUL);
2396 Newx(result, nchar + 1, U8);
2398 while (tmps < send) {
2399 const U8 c = (U8)utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2404 sv_usepvn_flags(TARG, (char*)result, nchar, SV_HAS_TRAILING_NUL);
2412 register long *tmpl;
2413 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
2416 for ( ; anum >= (I32)sizeof(long); anum -= (I32)sizeof(long), tmpl++)
2421 for ( ; anum > 0; anum--, tmps++)
2429 /* integer versions of some of the above */
2433 dVAR; dSP; dATARGET;
2434 tryAMAGICbin_MG(mult_amg, AMGf_assign);
2437 SETi( left * right );
2445 dVAR; dSP; dATARGET;
2446 tryAMAGICbin_MG(div_amg, AMGf_assign);
2449 IV value = SvIV_nomg(right);
2451 DIE(aTHX_ "Illegal division by zero");
2452 num = SvIV_nomg(left);
2454 /* avoid FPE_INTOVF on some platforms when num is IV_MIN */
2458 value = num / value;
2464 #if defined(__GLIBC__) && IVSIZE == 8
2471 /* This is the vanilla old i_modulo. */
2472 dVAR; dSP; dATARGET;
2473 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2477 DIE(aTHX_ "Illegal modulus zero");
2478 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2482 SETi( left % right );
2487 #if defined(__GLIBC__) && IVSIZE == 8
2492 /* This is the i_modulo with the workaround for the _moddi3 bug
2493 * in (at least) glibc 2.2.5 (the PERL_ABS() the workaround).
2494 * See below for pp_i_modulo. */
2495 dVAR; dSP; dATARGET;
2496 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2500 DIE(aTHX_ "Illegal modulus zero");
2501 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2505 SETi( left % PERL_ABS(right) );
2512 dVAR; dSP; dATARGET;
2513 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2517 DIE(aTHX_ "Illegal modulus zero");
2518 /* The assumption is to use hereafter the old vanilla version... */
2520 PL_ppaddr[OP_I_MODULO] =
2522 /* .. but if we have glibc, we might have a buggy _moddi3
2523 * (at least glicb 2.2.5 is known to have this bug), in other
2524 * words our integer modulus with negative quad as the second
2525 * argument might be broken. Test for this and re-patch the
2526 * opcode dispatch table if that is the case, remembering to
2527 * also apply the workaround so that this first round works
2528 * right, too. See [perl #9402] for more information. */
2532 /* Cannot do this check with inlined IV constants since
2533 * that seems to work correctly even with the buggy glibc. */
2535 /* Yikes, we have the bug.
2536 * Patch in the workaround version. */
2538 PL_ppaddr[OP_I_MODULO] =
2539 &Perl_pp_i_modulo_1;
2540 /* Make certain we work right this time, too. */
2541 right = PERL_ABS(right);
2544 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2548 SETi( left % right );
2556 dVAR; dSP; dATARGET;
2557 tryAMAGICbin_MG(add_amg, AMGf_assign);
2559 dPOPTOPiirl_ul_nomg;
2560 SETi( left + right );
2567 dVAR; dSP; dATARGET;
2568 tryAMAGICbin_MG(subtr_amg, AMGf_assign);
2570 dPOPTOPiirl_ul_nomg;
2571 SETi( left - right );
2579 tryAMAGICbin_MG(lt_amg, AMGf_set);
2582 SETs(boolSV(left < right));
2590 tryAMAGICbin_MG(gt_amg, AMGf_set);
2593 SETs(boolSV(left > right));
2601 tryAMAGICbin_MG(le_amg, AMGf_set);
2604 SETs(boolSV(left <= right));
2612 tryAMAGICbin_MG(ge_amg, AMGf_set);
2615 SETs(boolSV(left >= right));
2623 tryAMAGICbin_MG(eq_amg, AMGf_set);
2626 SETs(boolSV(left == right));
2634 tryAMAGICbin_MG(ne_amg, AMGf_set);
2637 SETs(boolSV(left != right));
2645 tryAMAGICbin_MG(ncmp_amg, 0);
2652 else if (left < right)
2664 tryAMAGICun_MG(neg_amg, 0);
2666 SV * const sv = TOPs;
2667 IV const i = SvIV_nomg(sv);
2673 /* High falutin' math. */
2678 tryAMAGICbin_MG(atan2_amg, 0);
2681 SETn(Perl_atan2(left, right));
2689 int amg_type = sin_amg;
2690 const char *neg_report = NULL;
2691 NV (*func)(NV) = Perl_sin;
2692 const int op_type = PL_op->op_type;
2709 amg_type = sqrt_amg;
2711 neg_report = "sqrt";
2716 tryAMAGICun_MG(amg_type, 0);
2718 SV * const arg = POPs;
2719 const NV value = SvNV_nomg(arg);
2721 if (op_type == OP_LOG ? (value <= 0.0) : (value < 0.0)) {
2722 SET_NUMERIC_STANDARD();
2723 /* diag_listed_as: Can't take log of %g */
2724 DIE(aTHX_ "Can't take %s of %"NVgf, neg_report, value);
2727 XPUSHn(func(value));
2732 /* Support Configure command-line overrides for rand() functions.
2733 After 5.005, perhaps we should replace this by Configure support
2734 for drand48(), random(), or rand(). For 5.005, though, maintain
2735 compatibility by calling rand() but allow the user to override it.
2736 See INSTALL for details. --Andy Dougherty 15 July 1998
2738 /* Now it's after 5.005, and Configure supports drand48() and random(),
2739 in addition to rand(). So the overrides should not be needed any more.
2740 --Jarkko Hietaniemi 27 September 1998
2743 #ifndef HAS_DRAND48_PROTO
2744 extern double drand48 (void);
2754 value = 1.0; (void)POPs;
2760 if (!PL_srand_called) {
2761 (void)seedDrand01((Rand_seed_t)seed());
2762 PL_srand_called = TRUE;
2772 const UV anum = (MAXARG < 1 || (!TOPs && !POPs)) ? seed() : POPu;
2773 (void)seedDrand01((Rand_seed_t)anum);
2774 PL_srand_called = TRUE;
2778 /* Historically srand always returned true. We can avoid breaking
2780 sv_setpvs(TARG, "0 but true");
2789 tryAMAGICun_MG(int_amg, AMGf_numeric);
2791 SV * const sv = TOPs;
2792 const IV iv = SvIV_nomg(sv);
2793 /* XXX it's arguable that compiler casting to IV might be subtly
2794 different from modf (for numbers inside (IV_MIN,UV_MAX)) in which
2795 else preferring IV has introduced a subtle behaviour change bug. OTOH
2796 relying on floating point to be accurate is a bug. */
2801 else if (SvIOK(sv)) {
2803 SETu(SvUV_nomg(sv));
2808 const NV value = SvNV_nomg(sv);
2810 if (value < (NV)UV_MAX + 0.5) {
2813 SETn(Perl_floor(value));
2817 if (value > (NV)IV_MIN - 0.5) {
2820 SETn(Perl_ceil(value));
2831 tryAMAGICun_MG(abs_amg, AMGf_numeric);
2833 SV * const sv = TOPs;
2834 /* This will cache the NV value if string isn't actually integer */
2835 const IV iv = SvIV_nomg(sv);
2840 else if (SvIOK(sv)) {
2841 /* IVX is precise */
2843 SETu(SvUV_nomg(sv)); /* force it to be numeric only */
2851 /* 2s complement assumption. Also, not really needed as
2852 IV_MIN and -IV_MIN should both be %100...00 and NV-able */
2858 const NV value = SvNV_nomg(sv);
2872 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES;
2876 SV* const sv = POPs;
2878 tmps = (SvPV_const(sv, len));
2880 /* If Unicode, try to downgrade
2881 * If not possible, croak. */
2882 SV* const tsv = sv_2mortal(newSVsv(sv));
2885 sv_utf8_downgrade(tsv, FALSE);
2886 tmps = SvPV_const(tsv, len);
2888 if (PL_op->op_type == OP_HEX)
2891 while (*tmps && len && isSPACE(*tmps))
2895 if (*tmps == 'x' || *tmps == 'X') {
2897 result_uv = grok_hex (tmps, &len, &flags, &result_nv);
2899 else if (*tmps == 'b' || *tmps == 'B')
2900 result_uv = grok_bin (tmps, &len, &flags, &result_nv);
2902 result_uv = grok_oct (tmps, &len, &flags, &result_nv);
2904 if (flags & PERL_SCAN_GREATER_THAN_UV_MAX) {
2918 SV * const sv = TOPs;
2920 if (SvGAMAGIC(sv)) {
2921 /* For an overloaded or magic scalar, we can't know in advance if
2922 it's going to be UTF-8 or not. Also, we can't call sv_len_utf8 as
2923 it likes to cache the length. Maybe that should be a documented
2928 = sv_2pv_flags(sv, &len,
2929 SV_UNDEF_RETURNS_NULL|SV_CONST_RETURN|SV_GMAGIC);
2932 if (!SvPADTMP(TARG)) {
2933 sv_setsv(TARG, &PL_sv_undef);
2938 else if (DO_UTF8(sv)) {
2939 SETi(utf8_length((U8*)p, (U8*)p + len));
2943 } else if (SvOK(sv)) {
2944 /* Neither magic nor overloaded. */
2946 SETi(sv_len_utf8(sv));
2950 if (!SvPADTMP(TARG)) {
2951 sv_setsv_nomg(TARG, &PL_sv_undef);
2959 /* Returns false if substring is completely outside original string.
2960 No length is indicated by len_iv = 0 and len_is_uv = 0. len_is_uv must
2961 always be true for an explicit 0.
2964 Perl_translate_substr_offsets(pTHX_ STRLEN curlen, IV pos1_iv,
2965 bool pos1_is_uv, IV len_iv,
2966 bool len_is_uv, STRLEN *posp,
2972 PERL_ARGS_ASSERT_TRANSLATE_SUBSTR_OFFSETS;
2974 if (!pos1_is_uv && pos1_iv < 0 && curlen) {
2975 pos1_is_uv = curlen-1 > ~(UV)pos1_iv;
2978 if ((pos1_is_uv || pos1_iv > 0) && (UV)pos1_iv > curlen)
2981 if (len_iv || len_is_uv) {
2982 if (!len_is_uv && len_iv < 0) {
2983 pos2_iv = curlen + len_iv;
2985 pos2_is_uv = curlen-1 > ~(UV)len_iv;
2988 } else { /* len_iv >= 0 */
2989 if (!pos1_is_uv && pos1_iv < 0) {
2990 pos2_iv = pos1_iv + len_iv;
2991 pos2_is_uv = (UV)len_iv > (UV)IV_MAX;
2993 if ((UV)len_iv > curlen-(UV)pos1_iv)
2996 pos2_iv = pos1_iv+len_iv;
3006 if (!pos2_is_uv && pos2_iv < 0) {
3007 if (!pos1_is_uv && pos1_iv < 0)
3011 else if (!pos1_is_uv && pos1_iv < 0)
3014 if ((UV)pos2_iv < (UV)pos1_iv)
3016 if ((UV)pos2_iv > curlen)
3019 /* pos1_iv and pos2_iv both in 0..curlen, so the cast is safe */
3020 *posp = (STRLEN)( (UV)pos1_iv );
3021 *lenp = (STRLEN)( (UV)pos2_iv - (UV)pos1_iv );
3038 I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
3039 const bool rvalue = (GIMME_V != G_VOID);
3042 const char *repl = NULL;
3044 int num_args = PL_op->op_private & 7;
3045 bool repl_need_utf8_upgrade = FALSE;
3046 bool repl_is_utf8 = FALSE;
3050 if(!(repl_sv = POPs)) num_args--;
3052 if ((len_sv = POPs)) {
3053 len_iv = SvIV(len_sv);
3054 len_is_uv = len_iv ? SvIOK_UV(len_sv) : 1;
3059 pos1_iv = SvIV(pos_sv);
3060 pos1_is_uv = SvIOK_UV(pos_sv);
3062 if (PL_op->op_private & OPpSUBSTR_REPL_FIRST) {
3068 repl = SvPV_const(repl_sv, repl_len);
3069 repl_is_utf8 = DO_UTF8(repl_sv) && repl_len;
3072 sv_utf8_upgrade(sv);
3074 else if (DO_UTF8(sv))
3075 repl_need_utf8_upgrade = TRUE;
3079 ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
3080 sv_magic(ret, NULL, PERL_MAGIC_substr, NULL, 0);
3082 LvTARG(ret) = SvREFCNT_inc_simple(sv);
3084 pos1_is_uv || pos1_iv >= 0
3085 ? (STRLEN)(UV)pos1_iv
3086 : (LvFLAGS(ret) |= 1, (STRLEN)(UV)-pos1_iv);
3088 len_is_uv || len_iv > 0
3089 ? (STRLEN)(UV)len_iv
3090 : (LvFLAGS(ret) |= 2, (STRLEN)(UV)-len_iv);
3093 PUSHs(ret); /* avoid SvSETMAGIC here */
3096 tmps = SvPV_const(sv, curlen);
3098 utf8_curlen = sv_len_utf8(sv);
3099 if (utf8_curlen == curlen)
3102 curlen = utf8_curlen;
3108 STRLEN pos, len, byte_len, byte_pos;
3110 if (!translate_substr_offsets(
3111 curlen, pos1_iv, pos1_is_uv, len_iv, len_is_uv, &pos, &len
3115 byte_pos = utf8_curlen
3116 ? sv_pos_u2b_flags(sv, pos, &byte_len, SV_CONST_RETURN) : pos;
3121 SvTAINTED_off(TARG); /* decontaminate */
3122 SvUTF8_off(TARG); /* decontaminate */
3123 sv_setpvn(TARG, tmps, byte_len);
3124 #ifdef USE_LOCALE_COLLATE
3125 sv_unmagic(TARG, PERL_MAGIC_collxfrm);
3132 SV* repl_sv_copy = NULL;
3134 if (repl_need_utf8_upgrade) {
3135 repl_sv_copy = newSVsv(repl_sv);
3136 sv_utf8_upgrade(repl_sv_copy);
3137 repl = SvPV_const(repl_sv_copy, repl_len);
3138 repl_is_utf8 = DO_UTF8(repl_sv_copy) && repl_len;
3141 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR),
3142 "Attempt to use reference as lvalue in substr"
3146 sv_insert_flags(sv, byte_pos, byte_len, repl, repl_len, 0);
3149 SvREFCNT_dec(repl_sv_copy);
3161 Perl_croak(aTHX_ "substr outside of string");
3162 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR), "substr outside of string");
3169 register const IV size = POPi;
3170 register const IV offset = POPi;
3171 register SV * const src = POPs;
3172 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
3175 if (lvalue) { /* it's an lvalue! */
3176 ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
3177 sv_magic(ret, NULL, PERL_MAGIC_vec, NULL, 0);
3179 LvTARG(ret) = SvREFCNT_inc_simple(src);
3180 LvTARGOFF(ret) = offset;
3181 LvTARGLEN(ret) = size;
3185 SvTAINTED_off(TARG); /* decontaminate */
3189 sv_setuv(ret, do_vecget(src, offset, size));
3205 const char *little_p;
3208 const bool is_index = PL_op->op_type == OP_INDEX;
3209 const bool threeargs = MAXARG >= 3 && (TOPs || ((void)POPs,0));
3215 big_p = SvPV_const(big, biglen);
3216 little_p = SvPV_const(little, llen);
3218 big_utf8 = DO_UTF8(big);
3219 little_utf8 = DO_UTF8(little);
3220 if (big_utf8 ^ little_utf8) {
3221 /* One needs to be upgraded. */
3222 if (little_utf8 && !PL_encoding) {
3223 /* Well, maybe instead we might be able to downgrade the small
3225 char * const pv = (char*)bytes_from_utf8((U8 *)little_p, &llen,
3228 /* If the large string is ISO-8859-1, and it's not possible to
3229 convert the small string to ISO-8859-1, then there is no
3230 way that it could be found anywhere by index. */
3235 /* At this point, pv is a malloc()ed string. So donate it to temp
3236 to ensure it will get free()d */
3237 little = temp = newSV(0);
3238 sv_usepvn(temp, pv, llen);
3239 little_p = SvPVX(little);
3242 ? newSVpvn(big_p, biglen) : newSVpvn(little_p, llen);
3245 sv_recode_to_utf8(temp, PL_encoding);
3247 sv_utf8_upgrade(temp);
3252 big_p = SvPV_const(big, biglen);
3255 little_p = SvPV_const(little, llen);
3259 if (SvGAMAGIC(big)) {
3260 /* Life just becomes a lot easier if I use a temporary here.
3261 Otherwise I need to avoid calls to sv_pos_u2b(), which (dangerously)
3262 will trigger magic and overloading again, as will fbm_instr()
3264 big = newSVpvn_flags(big_p, biglen,
3265 SVs_TEMP | (big_utf8 ? SVf_UTF8 : 0));
3268 if (SvGAMAGIC(little) || (is_index && !SvOK(little))) {
3269 /* index && SvOK() is a hack. fbm_instr() calls SvPV_const, which will
3270 warn on undef, and we've already triggered a warning with the
3271 SvPV_const some lines above. We can't remove that, as we need to
3272 call some SvPV to trigger overloading early and find out if the
3274 This is all getting to messy. The API isn't quite clean enough,
3275 because data access has side effects.
3277 little = newSVpvn_flags(little_p, llen,
3278 SVs_TEMP | (little_utf8 ? SVf_UTF8 : 0));
3279 little_p = SvPVX(little);
3283 offset = is_index ? 0 : biglen;
3285 if (big_utf8 && offset > 0)
3286 sv_pos_u2b(big, &offset, 0);
3292 else if (offset > (I32)biglen)
3294 if (!(little_p = is_index
3295 ? fbm_instr((unsigned char*)big_p + offset,
3296 (unsigned char*)big_p + biglen, little, 0)
3297 : rninstr(big_p, big_p + offset,
3298 little_p, little_p + llen)))
3301 retval = little_p - big_p;
3302 if (retval > 0 && big_utf8)
3303 sv_pos_b2u(big, &retval);
3313 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
3314 SvTAINTED_off(TARG);
3315 do_sprintf(TARG, SP-MARK, MARK+1);
3316 TAINT_IF(SvTAINTED(TARG));
3328 const U8 *s = (U8*)SvPV_const(argsv, len);
3330 if (PL_encoding && SvPOK(argsv) && !DO_UTF8(argsv)) {
3331 SV * const tmpsv = sv_2mortal(newSVsv(argsv));
3332 s = (U8*)sv_recode_to_utf8(tmpsv, PL_encoding);
3336 XPUSHu(DO_UTF8(argsv) ?
3337 utf8n_to_uvchr(s, UTF8_MAXBYTES, 0, UTF8_ALLOW_ANYUV) :
3349 if (((SvIOK_notUV(TOPs) && SvIV(TOPs) < 0)
3351 (SvNOK(TOPs) && SvNV(TOPs) < 0.0))) {
3353 value = POPu; /* chr(-1) eq chr(0xff), etc. */
3355 (void) POPs; /* Ignore the argument value. */
3356 value = UNICODE_REPLACEMENT;
3362 SvUPGRADE(TARG,SVt_PV);
3364 if (value > 255 && !IN_BYTES) {
3365 SvGROW(TARG, (STRLEN)UNISKIP(value)+1);
3366 tmps = (char*)uvchr_to_utf8_flags((U8*)SvPVX(TARG), value, 0);
3367 SvCUR_set(TARG, tmps - SvPVX_const(TARG));
3369 (void)SvPOK_only(TARG);
3378 *tmps++ = (char)value;
3380 (void)SvPOK_only(TARG);
3382 if (PL_encoding && !IN_BYTES) {
3383 sv_recode_to_utf8(TARG, PL_encoding);
3385 if (SvCUR(TARG) == 0
3386 || ! is_utf8_string((U8*)tmps, SvCUR(TARG))
3387 || UTF8_IS_REPLACEMENT((U8*) tmps, (U8*) tmps + SvCUR(TARG)))
3392 *tmps++ = (char)value;
3408 const char *tmps = SvPV_const(left, len);
3410 if (DO_UTF8(left)) {
3411 /* If Unicode, try to downgrade.
3412 * If not possible, croak.
3413 * Yes, we made this up. */
3414 SV* const tsv = sv_2mortal(newSVsv(left));
3417 sv_utf8_downgrade(tsv, FALSE);
3418 tmps = SvPV_const(tsv, len);
3420 # ifdef USE_ITHREADS
3422 if (!PL_reentrant_buffer->_crypt_struct_buffer) {
3423 /* This should be threadsafe because in ithreads there is only
3424 * one thread per interpreter. If this would not be true,
3425 * we would need a mutex to protect this malloc. */
3426 PL_reentrant_buffer->_crypt_struct_buffer =
3427 (struct crypt_data *)safemalloc(sizeof(struct crypt_data));
3428 #if defined(__GLIBC__) || defined(__EMX__)
3429 if (PL_reentrant_buffer->_crypt_struct_buffer) {
3430 PL_reentrant_buffer->_crypt_struct_buffer->initialized = 0;
3431 /* work around glibc-2.2.5 bug */
3432 PL_reentrant_buffer->_crypt_struct_buffer->current_saltbits = 0;
3436 # endif /* HAS_CRYPT_R */
3437 # endif /* USE_ITHREADS */
3439 sv_setpv(TARG, fcrypt(tmps, SvPV_nolen_const(right)));
3441 sv_setpv(TARG, PerlProc_crypt(tmps, SvPV_nolen_const(right)));
3447 "The crypt() function is unimplemented due to excessive paranoia.");
3451 /* Generally UTF-8 and UTF-EBCDIC are indistinguishable at this level. So
3452 * most comments below say UTF-8, when in fact they mean UTF-EBCDIC as well */
3454 /* Generates code to store a unicode codepoint c that is known to occupy
3455 * exactly two UTF-8 and UTF-EBCDIC bytes; it is stored into p and p+1,
3456 * and p is advanced to point to the next available byte after the two bytes */
3457 #define CAT_UNI_TO_UTF8_TWO_BYTE(p, c) \
3459 *(p)++ = UTF8_TWO_BYTE_HI(c); \
3460 *((p)++) = UTF8_TWO_BYTE_LO(c); \
3465 /* Actually is both lcfirst() and ucfirst(). Only the first character
3466 * changes. This means that possibly we can change in-place, ie., just
3467 * take the source and change that one character and store it back, but not
3468 * if read-only etc, or if the length changes */
3473 STRLEN slen; /* slen is the byte length of the whole SV. */
3476 bool inplace; /* ? Convert first char only, in-place */
3477 bool doing_utf8 = FALSE; /* ? using utf8 */
3478 bool convert_source_to_utf8 = FALSE; /* ? need to convert */
3479 const int op_type = PL_op->op_type;
3482 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
3483 STRLEN ulen; /* ulen is the byte length of the original Unicode character
3484 * stored as UTF-8 at s. */
3485 STRLEN tculen; /* tculen is the byte length of the freshly titlecased (or
3486 * lowercased) character stored in tmpbuf. May be either
3487 * UTF-8 or not, but in either case is the number of bytes */
3488 bool tainted = FALSE;
3492 s = (const U8*)SvPV_nomg_const(source, slen);
3494 if (ckWARN(WARN_UNINITIALIZED))
3495 report_uninit(source);
3500 /* We may be able to get away with changing only the first character, in
3501 * place, but not if read-only, etc. Later we may discover more reasons to
3502 * not convert in-place. */
3503 inplace = SvPADTMP(source) && !SvREADONLY(source) && SvTEMP(source);
3505 /* First calculate what the changed first character should be. This affects
3506 * whether we can just swap it out, leaving the rest of the string unchanged,
3507 * or even if have to convert the dest to UTF-8 when the source isn't */
3509 if (! slen) { /* If empty */
3510 need = 1; /* still need a trailing NUL */
3513 else if (DO_UTF8(source)) { /* Is the source utf8? */
3516 if (op_type == OP_UCFIRST) {
3517 _to_utf8_title_flags(s, tmpbuf, &tculen,
3518 cBOOL(IN_LOCALE_RUNTIME), &tainted);
3521 _to_utf8_lower_flags(s, tmpbuf, &tculen,
3522 cBOOL(IN_LOCALE_RUNTIME), &tainted);
3525 /* we can't do in-place if the length changes. */
3526 if (ulen != tculen) inplace = FALSE;
3527 need = slen + 1 - ulen + tculen;
3529 else { /* Non-zero length, non-UTF-8, Need to consider locale and if
3530 * latin1 is treated as caseless. Note that a locale takes
3532 ulen = 1; /* Original character is 1 byte */
3533 tculen = 1; /* Most characters will require one byte, but this will
3534 * need to be overridden for the tricky ones */
3537 if (op_type == OP_LCFIRST) {
3539 /* lower case the first letter: no trickiness for any character */
3540 *tmpbuf = (IN_LOCALE_RUNTIME) ? toLOWER_LC(*s) :
3541 ((IN_UNI_8_BIT) ? toLOWER_LATIN1(*s) : toLOWER(*s));
3544 else if (IN_LOCALE_RUNTIME) {
3545 *tmpbuf = toUPPER_LC(*s); /* This would be a bug if any locales
3546 * have upper and title case different
3549 else if (! IN_UNI_8_BIT) {
3550 *tmpbuf = toUPPER(*s); /* Returns caseless for non-ascii, or
3551 * on EBCDIC machines whatever the
3552 * native function does */
3554 else { /* is ucfirst non-UTF-8, not in locale, and cased latin1 */
3555 UV title_ord = _to_upper_title_latin1(*s, tmpbuf, &tculen, 's');
3557 assert(tculen == 2);
3559 /* If the result is an upper Latin1-range character, it can
3560 * still be represented in one byte, which is its ordinal */
3561 if (UTF8_IS_DOWNGRADEABLE_START(*tmpbuf)) {
3562 *tmpbuf = (U8) title_ord;
3566 /* Otherwise it became more than one ASCII character (in
3567 * the case of LATIN_SMALL_LETTER_SHARP_S) or changed to
3568 * beyond Latin1, so the number of bytes changed, so can't
3569 * replace just the first character in place. */
3572 /* If the result won't fit in a byte, the entire result will
3573 * have to be in UTF-8. Assume worst case sizing in
3574 * conversion. (all latin1 characters occupy at most two bytes
3576 if (title_ord > 255) {
3578 convert_source_to_utf8 = TRUE;
3579 need = slen * 2 + 1;
3581 /* The (converted) UTF-8 and UTF-EBCDIC lengths of all
3582 * (both) characters whose title case is above 255 is
3586 else { /* LATIN_SMALL_LETTER_SHARP_S expands by 1 byte */
3587 need = slen + 1 + 1;
3591 } /* End of use Unicode (Latin1) semantics */
3592 } /* End of changing the case of the first character */
3594 /* Here, have the first character's changed case stored in tmpbuf. Ready to
3595 * generate the result */
3598 /* We can convert in place. This means we change just the first
3599 * character without disturbing the rest; no need to grow */
3601 s = d = (U8*)SvPV_force_nomg(source, slen);
3607 /* Here, we can't convert in place; we earlier calculated how much
3608 * space we will need, so grow to accommodate that */
3609 SvUPGRADE(dest, SVt_PV);
3610 d = (U8*)SvGROW(dest, need);
3611 (void)SvPOK_only(dest);
3618 if (! convert_source_to_utf8) {
3620 /* Here both source and dest are in UTF-8, but have to create
3621 * the entire output. We initialize the result to be the
3622 * title/lower cased first character, and then append the rest
3624 sv_setpvn(dest, (char*)tmpbuf, tculen);
3626 sv_catpvn(dest, (char*)(s + ulen), slen - ulen);
3630 const U8 *const send = s + slen;
3632 /* Here the dest needs to be in UTF-8, but the source isn't,
3633 * except we earlier UTF-8'd the first character of the source
3634 * into tmpbuf. First put that into dest, and then append the
3635 * rest of the source, converting it to UTF-8 as we go. */
3637 /* Assert tculen is 2 here because the only two characters that
3638 * get to this part of the code have 2-byte UTF-8 equivalents */
3640 *d++ = *(tmpbuf + 1);
3641 s++; /* We have just processed the 1st char */
3643 for (; s < send; s++) {
3644 d = uvchr_to_utf8(d, *s);
3647 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3651 else { /* in-place UTF-8. Just overwrite the first character */
3652 Copy(tmpbuf, d, tculen, U8);
3653 SvCUR_set(dest, need - 1);
3661 else { /* Neither source nor dest are in or need to be UTF-8 */
3663 if (IN_LOCALE_RUNTIME) {
3667 if (inplace) { /* in-place, only need to change the 1st char */
3670 else { /* Not in-place */
3672 /* Copy the case-changed character(s) from tmpbuf */
3673 Copy(tmpbuf, d, tculen, U8);
3674 d += tculen - 1; /* Code below expects d to point to final
3675 * character stored */
3678 else { /* empty source */
3679 /* See bug #39028: Don't taint if empty */
3683 /* In a "use bytes" we don't treat the source as UTF-8, but, still want
3684 * the destination to retain that flag */
3688 if (!inplace) { /* Finish the rest of the string, unchanged */
3689 /* This will copy the trailing NUL */
3690 Copy(s + 1, d + 1, slen, U8);
3691 SvCUR_set(dest, need - 1);
3694 if (dest != source && SvTAINTED(source))
3700 /* There's so much setup/teardown code common between uc and lc, I wonder if
3701 it would be worth merging the two, and just having a switch outside each
3702 of the three tight loops. There is less and less commonality though */
3716 if (SvPADTMP(source) && !SvREADONLY(source) && !SvAMAGIC(source)
3717 && SvTEMP(source) && !DO_UTF8(source)
3718 && (IN_LOCALE_RUNTIME || ! IN_UNI_8_BIT)) {
3720 /* We can convert in place. The reason we can't if in UNI_8_BIT is to
3721 * make the loop tight, so we overwrite the source with the dest before
3722 * looking at it, and we need to look at the original source
3723 * afterwards. There would also need to be code added to handle
3724 * switching to not in-place in midstream if we run into characters
3725 * that change the length.
3728 s = d = (U8*)SvPV_force_nomg(source, len);
3735 /* The old implementation would copy source into TARG at this point.
3736 This had the side effect that if source was undef, TARG was now
3737 an undefined SV with PADTMP set, and they don't warn inside
3738 sv_2pv_flags(). However, we're now getting the PV direct from
3739 source, which doesn't have PADTMP set, so it would warn. Hence the
3743 s = (const U8*)SvPV_nomg_const(source, len);
3745 if (ckWARN(WARN_UNINITIALIZED))
3746 report_uninit(source);
3752 SvUPGRADE(dest, SVt_PV);
3753 d = (U8*)SvGROW(dest, min);
3754 (void)SvPOK_only(dest);
3759 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
3760 to check DO_UTF8 again here. */
3762 if (DO_UTF8(source)) {
3763 const U8 *const send = s + len;
3764 U8 tmpbuf[UTF8_MAXBYTES+1];
3765 bool tainted = FALSE;
3767 /* All occurrences of these are to be moved to follow any other marks.
3768 * This is context-dependent. We may not be passed enough context to
3769 * move the iota subscript beyond all of them, but we do the best we can
3770 * with what we're given. The result is always better than if we
3771 * hadn't done this. And, the problem would only arise if we are
3772 * passed a character without all its combining marks, which would be
3773 * the caller's mistake. The information this is based on comes from a
3774 * comment in Unicode SpecialCasing.txt, (and the Standard's text
3775 * itself) and so can't be checked properly to see if it ever gets
3776 * revised. But the likelihood of it changing is remote */
3777 bool in_iota_subscript = FALSE;
3783 if (in_iota_subscript && ! is_utf8_mark(s)) {
3785 /* A non-mark. Time to output the iota subscript */
3786 #define GREEK_CAPITAL_LETTER_IOTA 0x0399
3787 #define COMBINING_GREEK_YPOGEGRAMMENI 0x0345
3789 CAT_UNI_TO_UTF8_TWO_BYTE(d, GREEK_CAPITAL_LETTER_IOTA);
3790 in_iota_subscript = FALSE;
3793 /* Then handle the current character. Get the changed case value
3794 * and copy it to the output buffer */
3797 uv = _to_utf8_upper_flags(s, tmpbuf, &ulen,
3798 cBOOL(IN_LOCALE_RUNTIME), &tainted);
3799 if (uv == GREEK_CAPITAL_LETTER_IOTA
3800 && utf8_to_uvchr_buf(s, send, 0) == COMBINING_GREEK_YPOGEGRAMMENI)
3802 in_iota_subscript = TRUE;
3805 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
3806 /* If the eventually required minimum size outgrows the
3807 * available space, we need to grow. */
3808 const UV o = d - (U8*)SvPVX_const(dest);
3810 /* If someone uppercases one million U+03B0s we SvGROW()
3811 * one million times. Or we could try guessing how much to
3812 * allocate without allocating too much. Such is life.
3813 * See corresponding comment in lc code for another option
3816 d = (U8*)SvPVX(dest) + o;
3818 Copy(tmpbuf, d, ulen, U8);
3823 if (in_iota_subscript) {
3824 CAT_UNI_TO_UTF8_TWO_BYTE(d, GREEK_CAPITAL_LETTER_IOTA);
3829 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3835 else { /* Not UTF-8 */
3837 const U8 *const send = s + len;
3839 /* Use locale casing if in locale; regular style if not treating
3840 * latin1 as having case; otherwise the latin1 casing. Do the
3841 * whole thing in a tight loop, for speed, */
3842 if (IN_LOCALE_RUNTIME) {
3845 for (; s < send; d++, s++)
3846 *d = toUPPER_LC(*s);
3848 else if (! IN_UNI_8_BIT) {
3849 for (; s < send; d++, s++) {
3854 for (; s < send; d++, s++) {
3855 *d = toUPPER_LATIN1_MOD(*s);
3856 if (LIKELY(*d != LATIN_SMALL_LETTER_Y_WITH_DIAERESIS)) continue;
3858 /* The mainstream case is the tight loop above. To avoid
3859 * extra tests in that, all three characters that require
3860 * special handling are mapped by the MOD to the one tested
3862 * Use the source to distinguish between the three cases */
3864 if (*s == LATIN_SMALL_LETTER_SHARP_S) {
3866 /* uc() of this requires 2 characters, but they are
3867 * ASCII. If not enough room, grow the string */
3868 if (SvLEN(dest) < ++min) {
3869 const UV o = d - (U8*)SvPVX_const(dest);
3871 d = (U8*)SvPVX(dest) + o;
3873 *d++ = 'S'; *d = 'S'; /* upper case is 'SS' */
3874 continue; /* Back to the tight loop; still in ASCII */
3877 /* The other two special handling characters have their
3878 * upper cases outside the latin1 range, hence need to be
3879 * in UTF-8, so the whole result needs to be in UTF-8. So,
3880 * here we are somewhere in the middle of processing a
3881 * non-UTF-8 string, and realize that we will have to convert
3882 * the whole thing to UTF-8. What to do? There are
3883 * several possibilities. The simplest to code is to
3884 * convert what we have so far, set a flag, and continue on
3885 * in the loop. The flag would be tested each time through
3886 * the loop, and if set, the next character would be
3887 * converted to UTF-8 and stored. But, I (khw) didn't want
3888 * to slow down the mainstream case at all for this fairly
3889 * rare case, so I didn't want to add a test that didn't
3890 * absolutely have to be there in the loop, besides the
3891 * possibility that it would get too complicated for
3892 * optimizers to deal with. Another possibility is to just
3893 * give up, convert the source to UTF-8, and restart the
3894 * function that way. Another possibility is to convert
3895 * both what has already been processed and what is yet to
3896 * come separately to UTF-8, then jump into the loop that
3897 * handles UTF-8. But the most efficient time-wise of the
3898 * ones I could think of is what follows, and turned out to
3899 * not require much extra code. */
3901 /* Convert what we have so far into UTF-8, telling the
3902 * function that we know it should be converted, and to
3903 * allow extra space for what we haven't processed yet.
3904 * Assume the worst case space requirements for converting
3905 * what we haven't processed so far: that it will require
3906 * two bytes for each remaining source character, plus the
3907 * NUL at the end. This may cause the string pointer to
3908 * move, so re-find it. */
3910 len = d - (U8*)SvPVX_const(dest);
3911 SvCUR_set(dest, len);
3912 len = sv_utf8_upgrade_flags_grow(dest,
3913 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
3915 d = (U8*)SvPVX(dest) + len;
3917 /* Now process the remainder of the source, converting to
3918 * upper and UTF-8. If a resulting byte is invariant in
3919 * UTF-8, output it as-is, otherwise convert to UTF-8 and
3920 * append it to the output. */
3921 for (; s < send; s++) {
3922 (void) _to_upper_title_latin1(*s, d, &len, 'S');
3926 /* Here have processed the whole source; no need to continue
3927 * with the outer loop. Each character has been converted
3928 * to upper case and converted to UTF-8 */
3931 } /* End of processing all latin1-style chars */
3932 } /* End of processing all chars */
3933 } /* End of source is not empty */
3935 if (source != dest) {
3936 *d = '\0'; /* Here d points to 1 after last char, add NUL */
3937 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3939 } /* End of isn't utf8 */
3940 if (dest != source && SvTAINTED(source))
3959 if (SvPADTMP(source) && !SvREADONLY(source) && !SvAMAGIC(source)
3960 && SvTEMP(source) && !DO_UTF8(source)) {
3962 /* We can convert in place, as lowercasing anything in the latin1 range
3963 * (or else DO_UTF8 would have been on) doesn't lengthen it */
3965 s = d = (U8*)SvPV_force_nomg(source, len);
3972 /* The old implementation would copy source into TARG at this point.
3973 This had the side effect that if source was undef, TARG was now
3974 an undefined SV with PADTMP set, and they don't warn inside
3975 sv_2pv_flags(). However, we're now getting the PV direct from
3976 source, which doesn't have PADTMP set, so it would warn. Hence the
3980 s = (const U8*)SvPV_nomg_const(source, len);
3982 if (ckWARN(WARN_UNINITIALIZED))
3983 report_uninit(source);
3989 SvUPGRADE(dest, SVt_PV);
3990 d = (U8*)SvGROW(dest, min);
3991 (void)SvPOK_only(dest);
3996 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
3997 to check DO_UTF8 again here. */
3999 if (DO_UTF8(source)) {
4000 const U8 *const send = s + len;
4001 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
4002 bool tainted = FALSE;
4005 const STRLEN u = UTF8SKIP(s);
4008 _to_utf8_lower_flags(s, tmpbuf, &ulen,
4009 cBOOL(IN_LOCALE_RUNTIME), &tainted);
4011 /* Here is where we would do context-sensitive actions. See the
4012 * commit message for this comment for why there isn't any */
4014 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
4016 /* If the eventually required minimum size outgrows the
4017 * available space, we need to grow. */
4018 const UV o = d - (U8*)SvPVX_const(dest);
4020 /* If someone lowercases one million U+0130s we SvGROW() one
4021 * million times. Or we could try guessing how much to
4022 * allocate without allocating too much. Such is life.
4023 * Another option would be to grow an extra byte or two more
4024 * each time we need to grow, which would cut down the million
4025 * to 500K, with little waste */
4027 d = (U8*)SvPVX(dest) + o;
4030 /* Copy the newly lowercased letter to the output buffer we're
4032 Copy(tmpbuf, d, ulen, U8);
4035 } /* End of looping through the source string */
4038 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4043 } else { /* Not utf8 */
4045 const U8 *const send = s + len;
4047 /* Use locale casing if in locale; regular style if not treating
4048 * latin1 as having case; otherwise the latin1 casing. Do the
4049 * whole thing in a tight loop, for speed, */
4050 if (IN_LOCALE_RUNTIME) {
4053 for (; s < send; d++, s++)
4054 *d = toLOWER_LC(*s);
4056 else if (! IN_UNI_8_BIT) {
4057 for (; s < send; d++, s++) {
4062 for (; s < send; d++, s++) {
4063 *d = toLOWER_LATIN1(*s);
4067 if (source != dest) {
4069 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4072 if (dest != source && SvTAINTED(source))
4081 SV * const sv = TOPs;
4083 register const char *s = SvPV_const(sv,len);
4085 SvUTF8_off(TARG); /* decontaminate */
4088 SvUPGRADE(TARG, SVt_PV);
4089 SvGROW(TARG, (len * 2) + 1);
4093 STRLEN ulen = UTF8SKIP(s);
4094 bool to_quote = FALSE;
4096 if (UTF8_IS_INVARIANT(*s)) {
4097 if (_isQUOTEMETA(*s)) {
4101 else if (UTF8_IS_DOWNGRADEABLE_START(*s)) {
4103 /* In locale, we quote all non-ASCII Latin1 chars.
4104 * Otherwise use the quoting rules */
4105 if (IN_LOCALE_RUNTIME
4106 || _isQUOTEMETA(TWO_BYTE_UTF8_TO_UNI(*s, *(s + 1))))
4111 else if (_is_utf8_quotemeta((U8 *) s)) {
4126 else if (IN_UNI_8_BIT) {
4128 if (_isQUOTEMETA(*s))
4134 /* For non UNI_8_BIT (and hence in locale) just quote all \W
4135 * including everything above ASCII */
4137 if (!isWORDCHAR_A(*s))
4143 SvCUR_set(TARG, d - SvPVX_const(TARG));
4144 (void)SvPOK_only_UTF8(TARG);
4147 sv_setpvn(TARG, s, len);
4164 U8 tmpbuf[UTF8_MAXBYTES * UTF8_MAX_FOLD_CHAR_EXPAND + 1];
4165 const bool full_folding = TRUE;
4166 const U8 flags = ( full_folding ? FOLD_FLAGS_FULL : 0 )
4167 | ( IN_LOCALE_RUNTIME ? FOLD_FLAGS_LOCALE : 0 );
4169 /* This is a facsimile of pp_lc, but with a thousand bugs thanks to me.
4170 * You are welcome(?) -Hugmeir
4178 s = (const U8*)SvPV_nomg_const(source, len);
4180 if (ckWARN(WARN_UNINITIALIZED))
4181 report_uninit(source);
4188 SvUPGRADE(dest, SVt_PV);
4189 d = (U8*)SvGROW(dest, min);
4190 (void)SvPOK_only(dest);
4195 if (DO_UTF8(source)) { /* UTF-8 flagged string. */
4196 bool tainted = FALSE;
4198 const STRLEN u = UTF8SKIP(s);
4201 _to_utf8_fold_flags(s, tmpbuf, &ulen, flags, &tainted);
4203 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
4204 const UV o = d - (U8*)SvPVX_const(dest);
4206 d = (U8*)SvPVX(dest) + o;
4209 Copy(tmpbuf, d, ulen, U8);
4218 } /* Unflagged string */
4220 /* For locale, bytes, and nothing, the behavior is supposed to be the
4223 if ( IN_LOCALE_RUNTIME ) { /* Under locale */
4226 for (; s < send; d++, s++)
4227 *d = toLOWER_LC(*s);
4229 else if ( !IN_UNI_8_BIT ) { /* Under nothing, or bytes */
4230 for (; s < send; d++, s++)
4234 /* For ASCII and the Latin-1 range, there's only two troublesome folds,
4235 * \x{DF} (\N{LATIN SMALL LETTER SHARP S}), which under full casefolding
4236 * becomes 'ss', and \x{B5} (\N{MICRO SIGN}), which under any fold becomes
4237 * \x{3BC} (\N{GREEK SMALL LETTER MU}) -- For the rest, the casefold is
4240 for (; s < send; d++, s++) {
4241 if (*s == MICRO_SIGN) {
4242 /* \N{MICRO SIGN}'s casefold is \N{GREEK SMALL LETTER MU}, which
4243 * is outside of the latin-1 range. There's a couple of ways to
4244 * deal with this -- khw discusses them in pp_lc/uc, so go there :)
4245 * What we do here is upgrade what we had already casefolded,
4246 * then enter an inner loop that appends the rest of the characters
4249 len = d - (U8*)SvPVX_const(dest);
4250 SvCUR_set(dest, len);
4251 len = sv_utf8_upgrade_flags_grow(dest,
4252 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
4253 /* The max expansion for latin1
4254 * chars is 1 byte becomes 2 */
4256 d = (U8*)SvPVX(dest) + len;
4258 CAT_UNI_TO_UTF8_TWO_BYTE(d, GREEK_SMALL_LETTER_MU);
4260 for (; s < send; s++) {
4262 UV fc = _to_uni_fold_flags(*s, tmpbuf, &ulen, flags);
4263 if UNI_IS_INVARIANT(fc) {
4264 if ( full_folding && *s == LATIN_SMALL_LETTER_SHARP_S) {
4272 Copy(tmpbuf, d, ulen, U8);
4278 else if (full_folding && *s == LATIN_SMALL_LETTER_SHARP_S) {
4279 /* Under full casefolding, LATIN SMALL LETTER SHARP S becomes "ss",
4280 * which may require growing the SV.
4282 if (SvLEN(dest) < ++min) {
4283 const UV o = d - (U8*)SvPVX_const(dest);
4285 d = (U8*)SvPVX(dest) + o;
4290 else { /* If it's not one of those two, the fold is their lower case */
4291 *d = toLOWER_LATIN1(*s);
4297 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4299 if (SvTAINTED(source))
4309 dVAR; dSP; dMARK; dORIGMARK;
4310 register AV *const av = MUTABLE_AV(POPs);
4311 register const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
4313 if (SvTYPE(av) == SVt_PVAV) {
4314 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
4315 bool can_preserve = FALSE;
4321 can_preserve = SvCANEXISTDELETE(av);
4324 if (lval && localizing) {
4327 for (svp = MARK + 1; svp <= SP; svp++) {
4328 const I32 elem = SvIV(*svp);
4332 if (max > AvMAX(av))
4336 while (++MARK <= SP) {
4338 I32 elem = SvIV(*MARK);
4339 bool preeminent = TRUE;
4341 if (localizing && can_preserve) {
4342 /* If we can determine whether the element exist,
4343 * Try to preserve the existenceness of a tied array
4344 * element by using EXISTS and DELETE if possible.
4345 * Fallback to FETCH and STORE otherwise. */
4346 preeminent = av_exists(av, elem);
4349 svp = av_fetch(av, elem, lval);
4351 if (!svp || *svp == &PL_sv_undef)
4352 DIE(aTHX_ PL_no_aelem, elem);
4355 save_aelem(av, elem, svp);
4357 SAVEADELETE(av, elem);
4360 *MARK = svp ? *svp : &PL_sv_undef;
4363 if (GIMME != G_ARRAY) {
4365 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
4371 /* Smart dereferencing for keys, values and each */
4383 (SvTYPE(sv) != SVt_PVHV && SvTYPE(sv) != SVt_PVAV)
4388 "Type of argument to %s must be unblessed hashref or arrayref",
4389 PL_op_desc[PL_op->op_type] );
4392 if (PL_op->op_flags & OPf_SPECIAL && SvTYPE(sv) == SVt_PVAV)
4394 "Can't modify %s in %s",
4395 PL_op_desc[PL_op->op_type], PL_op_desc[PL_op->op_next->op_type]
4398 /* Delegate to correct function for op type */
4400 if (PL_op->op_type == OP_RKEYS || PL_op->op_type == OP_RVALUES) {
4401 return (SvTYPE(sv) == SVt_PVHV) ? Perl_do_kv(aTHX) : Perl_pp_akeys(aTHX);
4404 return (SvTYPE(sv) == SVt_PVHV) ? Perl_pp_each(aTHX) : Perl_pp_aeach(aTHX);
4412 AV *array = MUTABLE_AV(POPs);
4413 const I32 gimme = GIMME_V;
4414 IV *iterp = Perl_av_iter_p(aTHX_ array);
4415 const IV current = (*iterp)++;
4417 if (current > av_len(array)) {
4419 if (gimme == G_SCALAR)
4427 if (gimme == G_ARRAY) {
4428 SV **const element = av_fetch(array, current, 0);
4429 PUSHs(element ? *element : &PL_sv_undef);
4438 AV *array = MUTABLE_AV(POPs);
4439 const I32 gimme = GIMME_V;
4441 *Perl_av_iter_p(aTHX_ array) = 0;
4443 if (gimme == G_SCALAR) {
4445 PUSHi(av_len(array) + 1);
4447 else if (gimme == G_ARRAY) {
4448 IV n = Perl_av_len(aTHX_ array);
4453 if (PL_op->op_type == OP_AKEYS || PL_op->op_type == OP_RKEYS) {
4454 for (i = 0; i <= n; i++) {
4459 for (i = 0; i <= n; i++) {
4460 SV *const *const elem = Perl_av_fetch(aTHX_ array, i, 0);
4461 PUSHs(elem ? *elem : &PL_sv_undef);
4468 /* Associative arrays. */
4474 HV * hash = MUTABLE_HV(POPs);
4476 const I32 gimme = GIMME_V;
4479 /* might clobber stack_sp */
4480 entry = hv_iternext(hash);
4485 SV* const sv = hv_iterkeysv(entry);
4486 PUSHs(sv); /* won't clobber stack_sp */
4487 if (gimme == G_ARRAY) {
4490 /* might clobber stack_sp */
4491 val = hv_iterval(hash, entry);
4496 else if (gimme == G_SCALAR)
4503 S_do_delete_local(pTHX)
4507 const I32 gimme = GIMME_V;
4511 if (PL_op->op_private & OPpSLICE) {
4513 SV * const osv = POPs;
4514 const bool tied = SvRMAGICAL(osv)
4515 && mg_find((const SV *)osv, PERL_MAGIC_tied);
4516 const bool can_preserve = SvCANEXISTDELETE(osv)
4517 || mg_find((const SV *)osv, PERL_MAGIC_env);
4518 const U32 type = SvTYPE(osv);
4519 if (type == SVt_PVHV) { /* hash element */
4520 HV * const hv = MUTABLE_HV(osv);
4521 while (++MARK <= SP) {
4522 SV * const keysv = *MARK;
4524 bool preeminent = TRUE;
4526 preeminent = hv_exists_ent(hv, keysv, 0);
4528 HE *he = hv_fetch_ent(hv, keysv, 1, 0);
4535 sv = hv_delete_ent(hv, keysv, 0, 0);
4536 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4539 save_helem_flags(hv, keysv, &sv, SAVEf_KEEPOLDELEM);
4541 *MARK = sv_mortalcopy(sv);
4547 SAVEHDELETE(hv, keysv);
4548 *MARK = &PL_sv_undef;
4552 else if (type == SVt_PVAV) { /* array element */
4553 if (PL_op->op_flags & OPf_SPECIAL) {
4554 AV * const av = MUTABLE_AV(osv);
4555 while (++MARK <= SP) {
4556 I32 idx = SvIV(*MARK);
4558 bool preeminent = TRUE;
4560 preeminent = av_exists(av, idx);
4562 SV **svp = av_fetch(av, idx, 1);
4569 sv = av_delete(av, idx, 0);
4570 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4573 save_aelem_flags(av, idx, &sv, SAVEf_KEEPOLDELEM);
4575 *MARK = sv_mortalcopy(sv);
4581 SAVEADELETE(av, idx);
4582 *MARK = &PL_sv_undef;
4588 DIE(aTHX_ "Not a HASH reference");
4589 if (gimme == G_VOID)
4591 else if (gimme == G_SCALAR) {
4596 *++MARK = &PL_sv_undef;
4601 SV * const keysv = POPs;
4602 SV * const osv = POPs;
4603 const bool tied = SvRMAGICAL(osv)
4604 && mg_find((const SV *)osv, PERL_MAGIC_tied);
4605 const bool can_preserve = SvCANEXISTDELETE(osv)