3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, 2004, 2005, 2006, 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 to discover,
13 * and no knowing what you'll find around a corner. And Elves, sir!" --Samwise
16 /* This file contains general pp ("push/pop") functions that execute the
17 * opcodes that make up a perl program. A typical pp function expects to
18 * find its arguments on the stack, and usually pushes its results onto
19 * the stack, hence the 'pp' terminology. Each OP structure contains
20 * a pointer to the relevant pp_foo() function.
30 /* XXX I can't imagine anyone who doesn't have this actually _needs_
31 it, since pid_t is an integral type.
34 #ifdef NEED_GETPID_PROTO
35 extern Pid_t getpid (void);
39 * Some BSDs and Cygwin default to POSIX math instead of IEEE.
40 * This switches them over to IEEE.
42 #if defined(LIBM_LIB_VERSION)
43 _LIB_VERSION_TYPE _LIB_VERSION = _IEEE_;
46 /* variations on pp_null */
52 if (GIMME_V == G_SCALAR)
63 if (PL_op->op_private & OPpLVAL_INTRO)
64 if (!(PL_op->op_private & OPpPAD_STATE))
65 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
67 if (PL_op->op_flags & OPf_REF) {
71 if (GIMME == G_SCALAR)
72 Perl_croak(aTHX_ "Can't return array to lvalue scalar context");
77 if (gimme == G_ARRAY) {
78 const I32 maxarg = AvFILL((AV*)TARG) + 1;
80 if (SvMAGICAL(TARG)) {
82 for (i=0; i < (U32)maxarg; i++) {
83 SV * const * const svp = av_fetch((AV*)TARG, i, FALSE);
84 SP[i+1] = (svp) ? *svp : &PL_sv_undef;
88 Copy(AvARRAY((AV*)TARG), SP+1, maxarg, SV*);
92 else if (gimme == G_SCALAR) {
93 SV* const sv = sv_newmortal();
94 const I32 maxarg = AvFILL((AV*)TARG) + 1;
107 if (PL_op->op_private & OPpLVAL_INTRO)
108 if (!(PL_op->op_private & OPpPAD_STATE))
109 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
110 if (PL_op->op_flags & OPf_REF)
113 if (GIMME == G_SCALAR)
114 Perl_croak(aTHX_ "Can't return hash to lvalue scalar context");
118 if (gimme == G_ARRAY) {
121 else if (gimme == G_SCALAR) {
122 SV* const sv = Perl_hv_scalar(aTHX_ (HV*)TARG);
136 tryAMAGICunDEREF(to_gv);
139 if (SvTYPE(sv) == SVt_PVIO) {
140 GV * const gv = (GV*) sv_newmortal();
141 gv_init(gv, 0, "", 0, 0);
142 GvIOp(gv) = (IO *)sv;
143 SvREFCNT_inc_void_NN(sv);
146 else if (SvTYPE(sv) != SVt_PVGV)
147 DIE(aTHX_ "Not a GLOB reference");
150 if (SvTYPE(sv) != SVt_PVGV) {
151 if (SvGMAGICAL(sv)) {
156 if (!SvOK(sv) && sv != &PL_sv_undef) {
157 /* If this is a 'my' scalar and flag is set then vivify
161 Perl_croak(aTHX_ PL_no_modify);
162 if (PL_op->op_private & OPpDEREF) {
164 if (cUNOP->op_targ) {
166 SV * const namesv = PAD_SV(cUNOP->op_targ);
167 const char * const name = SvPV(namesv, len);
169 gv_init(gv, CopSTASH(PL_curcop), name, len, 0);
172 const char * const name = CopSTASHPV(PL_curcop);
175 if (SvTYPE(sv) < SVt_RV)
176 sv_upgrade(sv, SVt_RV);
177 else if (SvPVX_const(sv)) {
182 SvRV_set(sv, (SV*)gv);
187 if (PL_op->op_flags & OPf_REF ||
188 PL_op->op_private & HINT_STRICT_REFS)
189 DIE(aTHX_ PL_no_usym, "a symbol");
190 if (ckWARN(WARN_UNINITIALIZED))
194 if ((PL_op->op_flags & OPf_SPECIAL) &&
195 !(PL_op->op_flags & OPf_MOD))
197 SV * const temp = (SV*)gv_fetchsv(sv, 0, SVt_PVGV);
199 && (!is_gv_magical_sv(sv,0)
200 || !(sv = (SV*)gv_fetchsv(sv, GV_ADD, SVt_PVGV)))) {
206 if (PL_op->op_private & HINT_STRICT_REFS)
207 DIE(aTHX_ PL_no_symref_sv, sv, "a symbol");
208 if ((PL_op->op_private & (OPpLVAL_INTRO|OPpDONT_INIT_GV))
209 == OPpDONT_INIT_GV) {
210 /* We are the target of a coderef assignment. Return
211 the scalar unchanged, and let pp_sasssign deal with
215 sv = (SV*)gv_fetchsv(sv, GV_ADD, SVt_PVGV);
219 if (PL_op->op_private & OPpLVAL_INTRO)
220 save_gp((GV*)sv, !(PL_op->op_flags & OPf_SPECIAL));
232 tryAMAGICunDEREF(to_sv);
235 switch (SvTYPE(sv)) {
241 DIE(aTHX_ "Not a SCALAR reference");
248 if (SvTYPE(gv) != SVt_PVGV) {
249 if (SvGMAGICAL(sv)) {
254 if (PL_op->op_private & HINT_STRICT_REFS) {
256 DIE(aTHX_ PL_no_symref_sv, sv, "a SCALAR");
258 DIE(aTHX_ PL_no_usym, "a SCALAR");
261 if (PL_op->op_flags & OPf_REF)
262 DIE(aTHX_ PL_no_usym, "a SCALAR");
263 if (ckWARN(WARN_UNINITIALIZED))
267 if ((PL_op->op_flags & OPf_SPECIAL) &&
268 !(PL_op->op_flags & OPf_MOD))
270 gv = (GV*)gv_fetchsv(sv, 0, SVt_PV);
272 && (!is_gv_magical_sv(sv, 0)
273 || !(gv = (GV*)gv_fetchsv(sv, GV_ADD, SVt_PV))))
279 gv = (GV*)gv_fetchsv(sv, GV_ADD, SVt_PV);
284 if (PL_op->op_flags & OPf_MOD) {
285 if (PL_op->op_private & OPpLVAL_INTRO) {
286 if (cUNOP->op_first->op_type == OP_NULL)
287 sv = save_scalar((GV*)TOPs);
289 sv = save_scalar(gv);
291 Perl_croak(aTHX_ PL_no_localize_ref);
293 else if (PL_op->op_private & OPpDEREF)
294 vivify_ref(sv, PL_op->op_private & OPpDEREF);
303 AV * const av = (AV*)TOPs;
304 SV ** const sv = Perl_av_arylen_p(aTHX_ (AV*)av);
307 sv_upgrade(*sv, SVt_PVMG);
308 sv_magic(*sv, (SV*)av, PERL_MAGIC_arylen, NULL, 0);
316 dVAR; dSP; dTARGET; dPOPss;
318 if (PL_op->op_flags & OPf_MOD || LVRET) {
319 if (SvTYPE(TARG) < SVt_PVLV) {
320 sv_upgrade(TARG, SVt_PVLV);
321 sv_magic(TARG, NULL, PERL_MAGIC_pos, NULL, 0);
325 if (LvTARG(TARG) != sv) {
327 SvREFCNT_dec(LvTARG(TARG));
328 LvTARG(TARG) = SvREFCNT_inc_simple(sv);
330 PUSHs(TARG); /* no SvSETMAGIC */
334 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
335 const MAGIC * const mg = mg_find(sv, PERL_MAGIC_regex_global);
336 if (mg && mg->mg_len >= 0) {
340 PUSHi(i + CopARYBASE_get(PL_curcop));
353 const I32 flags = (PL_op->op_flags & OPf_SPECIAL)
355 : ((PL_op->op_private & (OPpLVAL_INTRO|OPpMAY_RETURN_CONSTANT)) == OPpMAY_RETURN_CONSTANT)
358 /* We usually try to add a non-existent subroutine in case of AUTOLOAD. */
359 /* (But not in defined().) */
361 CV *cv = sv_2cv(TOPs, &stash_unused, &gv, flags);
364 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
365 if ((PL_op->op_private & OPpLVAL_INTRO)) {
366 if (gv && GvCV(gv) == cv && (gv = gv_autoload4(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv), FALSE)))
369 DIE(aTHX_ "Can't modify non-lvalue subroutine call");
372 else if ((flags == (GV_ADD|GV_NOEXPAND)) && gv && SvROK(gv)) {
376 cv = (CV*)&PL_sv_undef;
387 SV *ret = &PL_sv_undef;
389 if (SvPOK(TOPs) && SvCUR(TOPs) >= 7) {
390 const char * const s = SvPVX_const(TOPs);
391 if (strnEQ(s, "CORE::", 6)) {
392 const int code = keyword(s + 6, SvCUR(TOPs) - 6);
393 if (code < 0) { /* Overridable. */
394 #define MAX_ARGS_OP ((sizeof(I32) - 1) * 2)
395 int i = 0, n = 0, seen_question = 0;
397 char str[ MAX_ARGS_OP * 2 + 2 ]; /* One ';', one '\0' */
399 if (code == -KEY_chop || code == -KEY_chomp
400 || code == -KEY_exec || code == -KEY_system)
402 while (i < MAXO) { /* The slow way. */
403 if (strEQ(s + 6, PL_op_name[i])
404 || strEQ(s + 6, PL_op_desc[i]))
410 goto nonesuch; /* Should not happen... */
412 oa = PL_opargs[i] >> OASHIFT;
414 if (oa & OA_OPTIONAL && !seen_question) {
418 if ((oa & (OA_OPTIONAL - 1)) >= OA_AVREF
419 && (oa & (OA_OPTIONAL - 1)) <= OA_SCALARREF
420 /* But globs are already references (kinda) */
421 && (oa & (OA_OPTIONAL - 1)) != OA_FILEREF
425 str[n++] = ("?$@@%&*$")[oa & (OA_OPTIONAL - 1)];
429 ret = sv_2mortal(newSVpvn(str, n - 1));
431 else if (code) /* Non-Overridable */
433 else { /* None such */
435 DIE(aTHX_ "Can't find an opnumber for \"%s\"", s+6);
439 cv = sv_2cv(TOPs, &stash, &gv, 0);
441 ret = sv_2mortal(newSVpvn(SvPVX_const(cv), SvCUR(cv)));
450 CV* cv = (CV*)PAD_SV(PL_op->op_targ);
452 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
468 if (GIMME != G_ARRAY) {
472 *MARK = &PL_sv_undef;
473 *MARK = refto(*MARK);
477 EXTEND_MORTAL(SP - MARK);
479 *MARK = refto(*MARK);
484 S_refto(pTHX_ SV *sv)
489 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
492 if (!(sv = LvTARG(sv)))
495 SvREFCNT_inc_void_NN(sv);
497 else if (SvTYPE(sv) == SVt_PVAV) {
498 if (!AvREAL((AV*)sv) && AvREIFY((AV*)sv))
501 SvREFCNT_inc_void_NN(sv);
503 else if (SvPADTMP(sv) && !IS_PADGV(sv))
507 SvREFCNT_inc_void_NN(sv);
510 sv_upgrade(rv, SVt_RV);
520 SV * const sv = POPs;
525 if (!sv || !SvROK(sv))
528 pv = sv_reftype(SvRV(sv),TRUE);
529 PUSHp(pv, strlen(pv));
539 stash = CopSTASH(PL_curcop);
541 SV * const ssv = POPs;
545 if (ssv && !SvGMAGICAL(ssv) && !SvAMAGIC(ssv) && SvROK(ssv))
546 Perl_croak(aTHX_ "Attempt to bless into a reference");
547 ptr = SvPV_const(ssv,len);
548 if (len == 0 && ckWARN(WARN_MISC))
549 Perl_warner(aTHX_ packWARN(WARN_MISC),
550 "Explicit blessing to '' (assuming package main)");
551 stash = gv_stashpvn(ptr, len, TRUE);
554 (void)sv_bless(TOPs, stash);
563 const char * const elem = SvPV_nolen_const(sv);
564 GV * const gv = (GV*)POPs;
569 /* elem will always be NUL terminated. */
570 const char * const second_letter = elem + 1;
573 if (strEQ(second_letter, "RRAY"))
574 tmpRef = (SV*)GvAV(gv);
577 if (strEQ(second_letter, "ODE"))
578 tmpRef = (SV*)GvCVu(gv);
581 if (strEQ(second_letter, "ILEHANDLE")) {
582 /* finally deprecated in 5.8.0 */
583 deprecate("*glob{FILEHANDLE}");
584 tmpRef = (SV*)GvIOp(gv);
587 if (strEQ(second_letter, "ORMAT"))
588 tmpRef = (SV*)GvFORM(gv);
591 if (strEQ(second_letter, "LOB"))
595 if (strEQ(second_letter, "ASH"))
596 tmpRef = (SV*)GvHV(gv);
599 if (*second_letter == 'O' && !elem[2])
600 tmpRef = (SV*)GvIOp(gv);
603 if (strEQ(second_letter, "AME"))
604 sv = newSVpvn(GvNAME(gv), GvNAMELEN(gv));
607 if (strEQ(second_letter, "ACKAGE")) {
608 const HV * const stash = GvSTASH(gv);
609 const HEK * const hek = stash ? HvNAME_HEK(stash) : NULL;
610 sv = hek ? newSVhek(hek) : newSVpvs("__ANON__");
614 if (strEQ(second_letter, "CALAR"))
629 /* Pattern matching */
634 register unsigned char *s;
637 register I32 *sfirst;
641 if (sv == PL_lastscream) {
645 s = (unsigned char*)(SvPV(sv, len));
647 if (pos <= 0 || !SvPOK(sv) || SvUTF8(sv)) {
648 /* No point in studying a zero length string, and not safe to study
649 anything that doesn't appear to be a simple scalar (and hence might
650 change between now and when the regexp engine runs without our set
651 magic ever running) such as a reference to an object with overloaded
657 SvSCREAM_off(PL_lastscream);
658 SvREFCNT_dec(PL_lastscream);
660 PL_lastscream = SvREFCNT_inc_simple(sv);
662 s = (unsigned char*)(SvPV(sv, len));
666 if (pos > PL_maxscream) {
667 if (PL_maxscream < 0) {
668 PL_maxscream = pos + 80;
669 Newx(PL_screamfirst, 256, I32);
670 Newx(PL_screamnext, PL_maxscream, I32);
673 PL_maxscream = pos + pos / 4;
674 Renew(PL_screamnext, PL_maxscream, I32);
678 sfirst = PL_screamfirst;
679 snext = PL_screamnext;
681 if (!sfirst || !snext)
682 DIE(aTHX_ "do_study: out of memory");
684 for (ch = 256; ch; --ch)
689 register const I32 ch = s[pos];
691 snext[pos] = sfirst[ch] - pos;
698 /* piggyback on m//g magic */
699 sv_magic(sv, NULL, PERL_MAGIC_regex_global, NULL, 0);
708 if (PL_op->op_flags & OPf_STACKED)
710 else if (PL_op->op_private & OPpTARGET_MY)
716 TARG = sv_newmortal();
721 /* Lvalue operators. */
733 dVAR; dSP; dMARK; dTARGET; dORIGMARK;
735 do_chop(TARG, *++MARK);
744 SETi(do_chomp(TOPs));
750 dVAR; dSP; dMARK; dTARGET;
751 register I32 count = 0;
754 count += do_chomp(POPs);
764 if (!PL_op->op_private) {
773 SV_CHECK_THINKFIRST_COW_DROP(sv);
775 switch (SvTYPE(sv)) {
785 if (cv_const_sv((CV*)sv) && ckWARN(WARN_MISC))
786 Perl_warner(aTHX_ packWARN(WARN_MISC), "Constant subroutine %s undefined",
787 CvANON((CV*)sv) ? "(anonymous)" : GvENAME(CvGV((CV*)sv)));
791 /* let user-undef'd sub keep its identity */
792 GV* const gv = CvGV((CV*)sv);
799 SvSetMagicSV(sv, &PL_sv_undef);
804 GvGP(sv) = gp_ref(gp);
806 GvLINE(sv) = CopLINE(PL_curcop);
812 if (SvTYPE(sv) >= SVt_PV && SvPVX_const(sv) && SvLEN(sv)) {
827 if (SvTYPE(TOPs) >= SVt_PVGV && SvTYPE(TOPs) != SVt_PVLV)
828 DIE(aTHX_ PL_no_modify);
829 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
830 && SvIVX(TOPs) != IV_MIN)
832 SvIV_set(TOPs, SvIVX(TOPs) - 1);
833 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
844 if (SvTYPE(TOPs) >= SVt_PVGV && SvTYPE(TOPs) != SVt_PVLV)
845 DIE(aTHX_ PL_no_modify);
846 sv_setsv(TARG, TOPs);
847 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
848 && SvIVX(TOPs) != IV_MAX)
850 SvIV_set(TOPs, SvIVX(TOPs) + 1);
851 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
856 /* special case for undef: see thread at 2003-03/msg00536.html in archive */
866 if (SvTYPE(TOPs) >= SVt_PVGV && SvTYPE(TOPs) != SVt_PVLV)
867 DIE(aTHX_ PL_no_modify);
868 sv_setsv(TARG, TOPs);
869 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
870 && SvIVX(TOPs) != IV_MIN)
872 SvIV_set(TOPs, SvIVX(TOPs) - 1);
873 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
882 /* Ordinary operators. */
887 #ifdef PERL_PRESERVE_IVUV
890 tryAMAGICbin(pow,opASSIGN);
891 #ifdef PERL_PRESERVE_IVUV
892 /* For integer to integer power, we do the calculation by hand wherever
893 we're sure it is safe; otherwise we call pow() and try to convert to
894 integer afterwards. */
907 const IV iv = SvIVX(TOPs);
911 goto float_it; /* Can't do negative powers this way. */
915 baseuok = SvUOK(TOPm1s);
917 baseuv = SvUVX(TOPm1s);
919 const IV iv = SvIVX(TOPm1s);
922 baseuok = TRUE; /* effectively it's a UV now */
924 baseuv = -iv; /* abs, baseuok == false records sign */
927 /* now we have integer ** positive integer. */
930 /* foo & (foo - 1) is zero only for a power of 2. */
931 if (!(baseuv & (baseuv - 1))) {
932 /* We are raising power-of-2 to a positive integer.
933 The logic here will work for any base (even non-integer
934 bases) but it can be less accurate than
935 pow (base,power) or exp (power * log (base)) when the
936 intermediate values start to spill out of the mantissa.
937 With powers of 2 we know this can't happen.
938 And powers of 2 are the favourite thing for perl
939 programmers to notice ** not doing what they mean. */
941 NV base = baseuok ? baseuv : -(NV)baseuv;
946 while (power >>= 1) {
957 register unsigned int highbit = 8 * sizeof(UV);
958 register unsigned int diff = 8 * sizeof(UV);
961 if (baseuv >> highbit) {
965 /* we now have baseuv < 2 ** highbit */
966 if (power * highbit <= 8 * sizeof(UV)) {
967 /* result will definitely fit in UV, so use UV math
968 on same algorithm as above */
969 register UV result = 1;
970 register UV base = baseuv;
971 const bool odd_power = (bool)(power & 1);
975 while (power >>= 1) {
982 if (baseuok || !odd_power)
983 /* answer is positive */
985 else if (result <= (UV)IV_MAX)
986 /* answer negative, fits in IV */
988 else if (result == (UV)IV_MIN)
989 /* 2's complement assumption: special case IV_MIN */
992 /* answer negative, doesn't fit */
1005 #if defined(USE_LONG_DOUBLE) && defined(HAS_AIX_POWL_NEG_BASE_BUG)
1007 We are building perl with long double support and are on an AIX OS
1008 afflicted with a powl() function that wrongly returns NaNQ for any
1009 negative base. This was reported to IBM as PMR #23047-379 on
1010 03/06/2006. The problem exists in at least the following versions
1011 of AIX and the libm fileset, and no doubt others as well:
1013 AIX 4.3.3-ML10 bos.adt.libm 4.3.3.50
1014 AIX 5.1.0-ML04 bos.adt.libm 5.1.0.29
1015 AIX 5.2.0 bos.adt.libm 5.2.0.85
1017 So, until IBM fixes powl(), we provide the following workaround to
1018 handle the problem ourselves. Our logic is as follows: for
1019 negative bases (left), we use fmod(right, 2) to check if the
1020 exponent is an odd or even integer:
1022 - if odd, powl(left, right) == -powl(-left, right)
1023 - if even, powl(left, right) == powl(-left, right)
1025 If the exponent is not an integer, the result is rightly NaNQ, so
1026 we just return that (as NV_NAN).
1030 NV mod2 = Perl_fmod( right, 2.0 );
1031 if (mod2 == 1.0 || mod2 == -1.0) { /* odd integer */
1032 SETn( -Perl_pow( -left, right) );
1033 } else if (mod2 == 0.0) { /* even integer */
1034 SETn( Perl_pow( -left, right) );
1035 } else { /* fractional power */
1039 SETn( Perl_pow( left, right) );
1042 SETn( Perl_pow( left, right) );
1043 #endif /* HAS_AIX_POWL_NEG_BASE_BUG */
1045 #ifdef PERL_PRESERVE_IVUV
1055 dVAR; dSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
1056 #ifdef PERL_PRESERVE_IVUV
1059 /* Unless the left argument is integer in range we are going to have to
1060 use NV maths. Hence only attempt to coerce the right argument if
1061 we know the left is integer. */
1062 /* Left operand is defined, so is it IV? */
1063 SvIV_please(TOPm1s);
1064 if (SvIOK(TOPm1s)) {
1065 bool auvok = SvUOK(TOPm1s);
1066 bool buvok = SvUOK(TOPs);
1067 const UV topmask = (~ (UV)0) << (4 * sizeof (UV));
1068 const UV botmask = ~((~ (UV)0) << (4 * sizeof (UV)));
1075 alow = SvUVX(TOPm1s);
1077 const IV aiv = SvIVX(TOPm1s);
1080 auvok = TRUE; /* effectively it's a UV now */
1082 alow = -aiv; /* abs, auvok == false records sign */
1088 const IV biv = SvIVX(TOPs);
1091 buvok = TRUE; /* effectively it's a UV now */
1093 blow = -biv; /* abs, buvok == false records sign */
1097 /* If this does sign extension on unsigned it's time for plan B */
1098 ahigh = alow >> (4 * sizeof (UV));
1100 bhigh = blow >> (4 * sizeof (UV));
1102 if (ahigh && bhigh) {
1104 /* eg 32 bit is at least 0x10000 * 0x10000 == 0x100000000
1105 which is overflow. Drop to NVs below. */
1106 } else if (!ahigh && !bhigh) {
1107 /* eg 32 bit is at most 0xFFFF * 0xFFFF == 0xFFFE0001
1108 so the unsigned multiply cannot overflow. */
1109 const UV product = alow * blow;
1110 if (auvok == buvok) {
1111 /* -ve * -ve or +ve * +ve gives a +ve result. */
1115 } else if (product <= (UV)IV_MIN) {
1116 /* 2s complement assumption that (UV)-IV_MIN is correct. */
1117 /* -ve result, which could overflow an IV */
1119 SETi( -(IV)product );
1121 } /* else drop to NVs below. */
1123 /* One operand is large, 1 small */
1126 /* swap the operands */
1128 bhigh = blow; /* bhigh now the temp var for the swap */
1132 /* now, ((ahigh * blow) << half_UV_len) + (alow * blow)
1133 multiplies can't overflow. shift can, add can, -ve can. */
1134 product_middle = ahigh * blow;
1135 if (!(product_middle & topmask)) {
1136 /* OK, (ahigh * blow) won't lose bits when we shift it. */
1138 product_middle <<= (4 * sizeof (UV));
1139 product_low = alow * blow;
1141 /* as for pp_add, UV + something mustn't get smaller.
1142 IIRC ANSI mandates this wrapping *behaviour* for
1143 unsigned whatever the actual representation*/
1144 product_low += product_middle;
1145 if (product_low >= product_middle) {
1146 /* didn't overflow */
1147 if (auvok == buvok) {
1148 /* -ve * -ve or +ve * +ve gives a +ve result. */
1150 SETu( product_low );
1152 } else if (product_low <= (UV)IV_MIN) {
1153 /* 2s complement assumption again */
1154 /* -ve result, which could overflow an IV */
1156 SETi( -(IV)product_low );
1158 } /* else drop to NVs below. */
1160 } /* product_middle too large */
1161 } /* ahigh && bhigh */
1162 } /* SvIOK(TOPm1s) */
1167 SETn( left * right );
1174 dVAR; dSP; dATARGET; tryAMAGICbin(div,opASSIGN);
1175 /* Only try to do UV divide first
1176 if ((SLOPPYDIVIDE is true) or
1177 (PERL_PRESERVE_IVUV is true and one or both SV is a UV too large
1179 The assumption is that it is better to use floating point divide
1180 whenever possible, only doing integer divide first if we can't be sure.
1181 If NV_PRESERVES_UV is true then we know at compile time that no UV
1182 can be too large to preserve, so don't need to compile the code to
1183 test the size of UVs. */
1186 # define PERL_TRY_UV_DIVIDE
1187 /* ensure that 20./5. == 4. */
1189 # ifdef PERL_PRESERVE_IVUV
1190 # ifndef NV_PRESERVES_UV
1191 # define PERL_TRY_UV_DIVIDE
1196 #ifdef PERL_TRY_UV_DIVIDE
1199 SvIV_please(TOPm1s);
1200 if (SvIOK(TOPm1s)) {
1201 bool left_non_neg = SvUOK(TOPm1s);
1202 bool right_non_neg = SvUOK(TOPs);
1206 if (right_non_neg) {
1207 right = SvUVX(TOPs);
1210 const IV biv = SvIVX(TOPs);
1213 right_non_neg = TRUE; /* effectively it's a UV now */
1219 /* historically undef()/0 gives a "Use of uninitialized value"
1220 warning before dieing, hence this test goes here.
1221 If it were immediately before the second SvIV_please, then
1222 DIE() would be invoked before left was even inspected, so
1223 no inpsection would give no warning. */
1225 DIE(aTHX_ "Illegal division by zero");
1228 left = SvUVX(TOPm1s);
1231 const IV aiv = SvIVX(TOPm1s);
1234 left_non_neg = TRUE; /* effectively it's a UV now */
1243 /* For sloppy divide we always attempt integer division. */
1245 /* Otherwise we only attempt it if either or both operands
1246 would not be preserved by an NV. If both fit in NVs
1247 we fall through to the NV divide code below. However,
1248 as left >= right to ensure integer result here, we know that
1249 we can skip the test on the right operand - right big
1250 enough not to be preserved can't get here unless left is
1253 && (left > ((UV)1 << NV_PRESERVES_UV_BITS))
1256 /* Integer division can't overflow, but it can be imprecise. */
1257 const UV result = left / right;
1258 if (result * right == left) {
1259 SP--; /* result is valid */
1260 if (left_non_neg == right_non_neg) {
1261 /* signs identical, result is positive. */
1265 /* 2s complement assumption */
1266 if (result <= (UV)IV_MIN)
1267 SETi( -(IV)result );
1269 /* It's exact but too negative for IV. */
1270 SETn( -(NV)result );
1273 } /* tried integer divide but it was not an integer result */
1274 } /* else (PERL_ABS(result) < 1.0) or (both UVs in range for NV) */
1275 } /* left wasn't SvIOK */
1276 } /* right wasn't SvIOK */
1277 #endif /* PERL_TRY_UV_DIVIDE */
1281 DIE(aTHX_ "Illegal division by zero");
1282 PUSHn( left / right );
1289 dVAR; dSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
1293 bool left_neg = FALSE;
1294 bool right_neg = FALSE;
1295 bool use_double = FALSE;
1296 bool dright_valid = FALSE;
1302 right_neg = !SvUOK(TOPs);
1304 right = SvUVX(POPs);
1306 const IV biv = SvIVX(POPs);
1309 right_neg = FALSE; /* effectively it's a UV now */
1317 right_neg = dright < 0;
1320 if (dright < UV_MAX_P1) {
1321 right = U_V(dright);
1322 dright_valid = TRUE; /* In case we need to use double below. */
1328 /* At this point use_double is only true if right is out of range for
1329 a UV. In range NV has been rounded down to nearest UV and
1330 use_double false. */
1332 if (!use_double && SvIOK(TOPs)) {
1334 left_neg = !SvUOK(TOPs);
1338 const IV aiv = SvIVX(POPs);
1341 left_neg = FALSE; /* effectively it's a UV now */
1350 left_neg = dleft < 0;
1354 /* This should be exactly the 5.6 behaviour - if left and right are
1355 both in range for UV then use U_V() rather than floor. */
1357 if (dleft < UV_MAX_P1) {
1358 /* right was in range, so is dleft, so use UVs not double.
1362 /* left is out of range for UV, right was in range, so promote
1363 right (back) to double. */
1365 /* The +0.5 is used in 5.6 even though it is not strictly
1366 consistent with the implicit +0 floor in the U_V()
1367 inside the #if 1. */
1368 dleft = Perl_floor(dleft + 0.5);
1371 dright = Perl_floor(dright + 0.5);
1381 DIE(aTHX_ "Illegal modulus zero");
1383 dans = Perl_fmod(dleft, dright);
1384 if ((left_neg != right_neg) && dans)
1385 dans = dright - dans;
1388 sv_setnv(TARG, dans);
1394 DIE(aTHX_ "Illegal modulus zero");
1397 if ((left_neg != right_neg) && ans)
1400 /* XXX may warn: unary minus operator applied to unsigned type */
1401 /* could change -foo to be (~foo)+1 instead */
1402 if (ans <= ~((UV)IV_MAX)+1)
1403 sv_setiv(TARG, ~ans+1);
1405 sv_setnv(TARG, -(NV)ans);
1408 sv_setuv(TARG, ans);
1417 dVAR; dSP; dATARGET; tryAMAGICbin(repeat,opASSIGN);
1424 const UV uv = SvUV(sv);
1426 count = IV_MAX; /* The best we can do? */
1430 const IV iv = SvIV(sv);
1437 else if (SvNOKp(sv)) {
1438 const NV nv = SvNV(sv);
1446 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1448 static const char oom_list_extend[] = "Out of memory during list extend";
1449 const I32 items = SP - MARK;
1450 const I32 max = items * count;
1452 MEM_WRAP_CHECK_1(max, SV*, oom_list_extend);
1453 /* Did the max computation overflow? */
1454 if (items > 0 && max > 0 && (max < items || max < count))
1455 Perl_croak(aTHX_ oom_list_extend);
1460 /* This code was intended to fix 20010809.028:
1463 for (($x =~ /./g) x 2) {
1464 print chop; # "abcdabcd" expected as output.
1467 * but that change (#11635) broke this code:
1469 $x = [("foo")x2]; # only one "foo" ended up in the anonlist.
1471 * I can't think of a better fix that doesn't introduce
1472 * an efficiency hit by copying the SVs. The stack isn't
1473 * refcounted, and mortalisation obviously doesn't
1474 * Do The Right Thing when the stack has more than
1475 * one pointer to the same mortal value.
1479 *SP = sv_2mortal(newSVsv(*SP));
1489 repeatcpy((char*)(MARK + items), (char*)MARK,
1490 items * sizeof(SV*), count - 1);
1493 else if (count <= 0)
1496 else { /* Note: mark already snarfed by pp_list */
1497 SV * const tmpstr = POPs;
1500 static const char oom_string_extend[] =
1501 "Out of memory during string extend";
1503 SvSetSV(TARG, tmpstr);
1504 SvPV_force(TARG, len);
1505 isutf = DO_UTF8(TARG);
1510 const STRLEN max = (UV)count * len;
1511 if (len > ((MEM_SIZE)~0)/count)
1512 Perl_croak(aTHX_ oom_string_extend);
1513 MEM_WRAP_CHECK_1(max, char, oom_string_extend);
1514 SvGROW(TARG, max + 1);
1515 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
1516 SvCUR_set(TARG, SvCUR(TARG) * count);
1518 *SvEND(TARG) = '\0';
1521 (void)SvPOK_only_UTF8(TARG);
1523 (void)SvPOK_only(TARG);
1525 if (PL_op->op_private & OPpREPEAT_DOLIST) {
1526 /* The parser saw this as a list repeat, and there
1527 are probably several items on the stack. But we're
1528 in scalar context, and there's no pp_list to save us
1529 now. So drop the rest of the items -- robin@kitsite.com
1542 dVAR; dSP; dATARGET; bool useleft; tryAMAGICbin(subtr,opASSIGN);
1543 useleft = USE_LEFT(TOPm1s);
1544 #ifdef PERL_PRESERVE_IVUV
1545 /* See comments in pp_add (in pp_hot.c) about Overflow, and how
1546 "bad things" happen if you rely on signed integers wrapping. */
1549 /* Unless the left argument is integer in range we are going to have to
1550 use NV maths. Hence only attempt to coerce the right argument if
1551 we know the left is integer. */
1552 register UV auv = 0;
1558 a_valid = auvok = 1;
1559 /* left operand is undef, treat as zero. */
1561 /* Left operand is defined, so is it IV? */
1562 SvIV_please(TOPm1s);
1563 if (SvIOK(TOPm1s)) {
1564 if ((auvok = SvUOK(TOPm1s)))
1565 auv = SvUVX(TOPm1s);
1567 register const IV aiv = SvIVX(TOPm1s);
1570 auvok = 1; /* Now acting as a sign flag. */
1571 } else { /* 2s complement assumption for IV_MIN */
1579 bool result_good = 0;
1582 bool buvok = SvUOK(TOPs);
1587 register const IV biv = SvIVX(TOPs);
1594 /* ?uvok if value is >= 0. basically, flagged as UV if it's +ve,
1595 else "IV" now, independent of how it came in.
1596 if a, b represents positive, A, B negative, a maps to -A etc
1601 all UV maths. negate result if A negative.
1602 subtract if signs same, add if signs differ. */
1604 if (auvok ^ buvok) {
1613 /* Must get smaller */
1618 if (result <= buv) {
1619 /* result really should be -(auv-buv). as its negation
1620 of true value, need to swap our result flag */
1632 if (result <= (UV)IV_MIN)
1633 SETi( -(IV)result );
1635 /* result valid, but out of range for IV. */
1636 SETn( -(NV)result );
1640 } /* Overflow, drop through to NVs. */
1644 useleft = USE_LEFT(TOPm1s);
1648 /* left operand is undef, treat as zero - value */
1652 SETn( TOPn - value );
1659 dVAR; dSP; dATARGET; tryAMAGICbin(lshift,opASSIGN);
1661 const IV shift = POPi;
1662 if (PL_op->op_private & HINT_INTEGER) {
1676 dVAR; dSP; dATARGET; tryAMAGICbin(rshift,opASSIGN);
1678 const IV shift = POPi;
1679 if (PL_op->op_private & HINT_INTEGER) {
1693 dVAR; dSP; tryAMAGICbinSET(lt,0);
1694 #ifdef PERL_PRESERVE_IVUV
1697 SvIV_please(TOPm1s);
1698 if (SvIOK(TOPm1s)) {
1699 bool auvok = SvUOK(TOPm1s);
1700 bool buvok = SvUOK(TOPs);
1702 if (!auvok && !buvok) { /* ## IV < IV ## */
1703 const IV aiv = SvIVX(TOPm1s);
1704 const IV biv = SvIVX(TOPs);
1707 SETs(boolSV(aiv < biv));
1710 if (auvok && buvok) { /* ## UV < UV ## */
1711 const UV auv = SvUVX(TOPm1s);
1712 const UV buv = SvUVX(TOPs);
1715 SETs(boolSV(auv < buv));
1718 if (auvok) { /* ## UV < IV ## */
1720 const IV biv = SvIVX(TOPs);
1723 /* As (a) is a UV, it's >=0, so it cannot be < */
1728 SETs(boolSV(auv < (UV)biv));
1731 { /* ## IV < UV ## */
1732 const IV aiv = SvIVX(TOPm1s);
1736 /* As (b) is a UV, it's >=0, so it must be < */
1743 SETs(boolSV((UV)aiv < buv));
1749 #ifndef NV_PRESERVES_UV
1750 #ifdef PERL_PRESERVE_IVUV
1753 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
1755 SETs(boolSV(SvRV(TOPs) < SvRV(TOPp1s)));
1760 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
1762 if (Perl_isnan(left) || Perl_isnan(right))
1764 SETs(boolSV(left < right));
1767 SETs(boolSV(TOPn < value));
1775 dVAR; dSP; tryAMAGICbinSET(gt,0);
1776 #ifdef PERL_PRESERVE_IVUV
1779 SvIV_please(TOPm1s);
1780 if (SvIOK(TOPm1s)) {
1781 bool auvok = SvUOK(TOPm1s);
1782 bool buvok = SvUOK(TOPs);
1784 if (!auvok && !buvok) { /* ## IV > IV ## */
1785 const IV aiv = SvIVX(TOPm1s);
1786 const IV biv = SvIVX(TOPs);
1789 SETs(boolSV(aiv > biv));
1792 if (auvok && buvok) { /* ## UV > UV ## */
1793 const UV auv = SvUVX(TOPm1s);
1794 const UV buv = SvUVX(TOPs);
1797 SETs(boolSV(auv > buv));
1800 if (auvok) { /* ## UV > IV ## */
1802 const IV biv = SvIVX(TOPs);
1806 /* As (a) is a UV, it's >=0, so it must be > */
1811 SETs(boolSV(auv > (UV)biv));
1814 { /* ## IV > UV ## */
1815 const IV aiv = SvIVX(TOPm1s);
1819 /* As (b) is a UV, it's >=0, so it cannot be > */
1826 SETs(boolSV((UV)aiv > buv));
1832 #ifndef NV_PRESERVES_UV
1833 #ifdef PERL_PRESERVE_IVUV
1836 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
1838 SETs(boolSV(SvRV(TOPs) > SvRV(TOPp1s)));
1843 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
1845 if (Perl_isnan(left) || Perl_isnan(right))
1847 SETs(boolSV(left > right));
1850 SETs(boolSV(TOPn > value));
1858 dVAR; dSP; tryAMAGICbinSET(le,0);
1859 #ifdef PERL_PRESERVE_IVUV
1862 SvIV_please(TOPm1s);
1863 if (SvIOK(TOPm1s)) {
1864 bool auvok = SvUOK(TOPm1s);
1865 bool buvok = SvUOK(TOPs);
1867 if (!auvok && !buvok) { /* ## IV <= IV ## */
1868 const IV aiv = SvIVX(TOPm1s);
1869 const IV biv = SvIVX(TOPs);
1872 SETs(boolSV(aiv <= biv));
1875 if (auvok && buvok) { /* ## UV <= UV ## */
1876 UV auv = SvUVX(TOPm1s);
1877 UV buv = SvUVX(TOPs);
1880 SETs(boolSV(auv <= buv));
1883 if (auvok) { /* ## UV <= IV ## */
1885 const IV biv = SvIVX(TOPs);
1889 /* As (a) is a UV, it's >=0, so a cannot be <= */
1894 SETs(boolSV(auv <= (UV)biv));
1897 { /* ## IV <= UV ## */
1898 const IV aiv = SvIVX(TOPm1s);
1902 /* As (b) is a UV, it's >=0, so a must be <= */
1909 SETs(boolSV((UV)aiv <= buv));
1915 #ifndef NV_PRESERVES_UV
1916 #ifdef PERL_PRESERVE_IVUV
1919 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
1921 SETs(boolSV(SvRV(TOPs) <= SvRV(TOPp1s)));
1926 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
1928 if (Perl_isnan(left) || Perl_isnan(right))
1930 SETs(boolSV(left <= right));
1933 SETs(boolSV(TOPn <= value));
1941 dVAR; dSP; tryAMAGICbinSET(ge,0);
1942 #ifdef PERL_PRESERVE_IVUV
1945 SvIV_please(TOPm1s);
1946 if (SvIOK(TOPm1s)) {
1947 bool auvok = SvUOK(TOPm1s);
1948 bool buvok = SvUOK(TOPs);
1950 if (!auvok && !buvok) { /* ## IV >= IV ## */
1951 const IV aiv = SvIVX(TOPm1s);
1952 const IV biv = SvIVX(TOPs);
1955 SETs(boolSV(aiv >= biv));
1958 if (auvok && buvok) { /* ## UV >= UV ## */
1959 const UV auv = SvUVX(TOPm1s);
1960 const UV buv = SvUVX(TOPs);
1963 SETs(boolSV(auv >= buv));
1966 if (auvok) { /* ## UV >= IV ## */
1968 const IV biv = SvIVX(TOPs);
1972 /* As (a) is a UV, it's >=0, so it must be >= */
1977 SETs(boolSV(auv >= (UV)biv));
1980 { /* ## IV >= UV ## */
1981 const IV aiv = SvIVX(TOPm1s);
1985 /* As (b) is a UV, it's >=0, so a cannot be >= */
1992 SETs(boolSV((UV)aiv >= buv));
1998 #ifndef NV_PRESERVES_UV
1999 #ifdef PERL_PRESERVE_IVUV
2002 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
2004 SETs(boolSV(SvRV(TOPs) >= SvRV(TOPp1s)));
2009 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
2011 if (Perl_isnan(left) || Perl_isnan(right))
2013 SETs(boolSV(left >= right));
2016 SETs(boolSV(TOPn >= value));
2024 dVAR; dSP; tryAMAGICbinSET(ne,0);
2025 #ifndef NV_PRESERVES_UV
2026 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
2028 SETs(boolSV(SvRV(TOPs) != SvRV(TOPp1s)));
2032 #ifdef PERL_PRESERVE_IVUV
2035 SvIV_please(TOPm1s);
2036 if (SvIOK(TOPm1s)) {
2037 const bool auvok = SvUOK(TOPm1s);
2038 const bool buvok = SvUOK(TOPs);
2040 if (auvok == buvok) { /* ## IV == IV or UV == UV ## */
2041 /* Casting IV to UV before comparison isn't going to matter
2042 on 2s complement. On 1s complement or sign&magnitude
2043 (if we have any of them) it could make negative zero
2044 differ from normal zero. As I understand it. (Need to
2045 check - is negative zero implementation defined behaviour
2047 const UV buv = SvUVX(POPs);
2048 const UV auv = SvUVX(TOPs);
2050 SETs(boolSV(auv != buv));
2053 { /* ## Mixed IV,UV ## */
2057 /* != is commutative so swap if needed (save code) */
2059 /* swap. top of stack (b) is the iv */
2063 /* As (a) is a UV, it's >0, so it cannot be == */
2072 /* As (b) is a UV, it's >0, so it cannot be == */
2076 uv = SvUVX(*(SP+1)); /* Do I want TOPp1s() ? */
2078 SETs(boolSV((UV)iv != uv));
2085 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
2087 if (Perl_isnan(left) || Perl_isnan(right))
2089 SETs(boolSV(left != right));
2092 SETs(boolSV(TOPn != value));
2100 dVAR; dSP; dTARGET; tryAMAGICbin(ncmp,0);
2101 #ifndef NV_PRESERVES_UV
2102 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
2103 const UV right = PTR2UV(SvRV(POPs));
2104 const UV left = PTR2UV(SvRV(TOPs));
2105 SETi((left > right) - (left < right));
2109 #ifdef PERL_PRESERVE_IVUV
2110 /* Fortunately it seems NaN isn't IOK */
2113 SvIV_please(TOPm1s);
2114 if (SvIOK(TOPm1s)) {
2115 const bool leftuvok = SvUOK(TOPm1s);
2116 const bool rightuvok = SvUOK(TOPs);
2118 if (!leftuvok && !rightuvok) { /* ## IV <=> IV ## */
2119 const IV leftiv = SvIVX(TOPm1s);
2120 const IV rightiv = SvIVX(TOPs);
2122 if (leftiv > rightiv)
2124 else if (leftiv < rightiv)
2128 } else if (leftuvok && rightuvok) { /* ## UV <=> UV ## */
2129 const UV leftuv = SvUVX(TOPm1s);
2130 const UV rightuv = SvUVX(TOPs);
2132 if (leftuv > rightuv)
2134 else if (leftuv < rightuv)
2138 } else if (leftuvok) { /* ## UV <=> IV ## */
2139 const IV rightiv = SvIVX(TOPs);
2141 /* As (a) is a UV, it's >=0, so it cannot be < */
2144 const UV leftuv = SvUVX(TOPm1s);
2145 if (leftuv > (UV)rightiv) {
2147 } else if (leftuv < (UV)rightiv) {
2153 } else { /* ## IV <=> UV ## */
2154 const IV leftiv = SvIVX(TOPm1s);
2156 /* As (b) is a UV, it's >=0, so it must be < */
2159 const UV rightuv = SvUVX(TOPs);
2160 if ((UV)leftiv > rightuv) {
2162 } else if ((UV)leftiv < rightuv) {
2180 if (Perl_isnan(left) || Perl_isnan(right)) {
2184 value = (left > right) - (left < right);
2188 else if (left < right)
2190 else if (left > right)
2206 int amg_type = sle_amg;
2210 switch (PL_op->op_type) {
2229 tryAMAGICbinSET_var(amg_type,0);
2232 const int cmp = (IN_LOCALE_RUNTIME
2233 ? sv_cmp_locale(left, right)
2234 : sv_cmp(left, right));
2235 SETs(boolSV(cmp * multiplier < rhs));
2242 dVAR; dSP; tryAMAGICbinSET(seq,0);
2245 SETs(boolSV(sv_eq(left, right)));
2252 dVAR; dSP; tryAMAGICbinSET(sne,0);
2255 SETs(boolSV(!sv_eq(left, right)));
2262 dVAR; dSP; dTARGET; tryAMAGICbin(scmp,0);
2265 const int cmp = (IN_LOCALE_RUNTIME
2266 ? sv_cmp_locale(left, right)
2267 : sv_cmp(left, right));
2275 dVAR; dSP; dATARGET; tryAMAGICbin(band,opASSIGN);
2280 if (SvNIOKp(left) || SvNIOKp(right)) {
2281 if (PL_op->op_private & HINT_INTEGER) {
2282 const IV i = SvIV_nomg(left) & SvIV_nomg(right);
2286 const UV u = SvUV_nomg(left) & SvUV_nomg(right);
2291 do_vop(PL_op->op_type, TARG, left, right);
2300 dVAR; dSP; dATARGET;
2301 const int op_type = PL_op->op_type;
2303 tryAMAGICbin_var((op_type == OP_BIT_OR ? bor_amg : bxor_amg), opASSIGN);
2308 if (SvNIOKp(left) || SvNIOKp(right)) {
2309 if (PL_op->op_private & HINT_INTEGER) {
2310 const IV l = (USE_LEFT(left) ? SvIV_nomg(left) : 0);
2311 const IV r = SvIV_nomg(right);
2312 const IV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2316 const UV l = (USE_LEFT(left) ? SvUV_nomg(left) : 0);
2317 const UV r = SvUV_nomg(right);
2318 const UV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2323 do_vop(op_type, TARG, left, right);
2332 dVAR; dSP; dTARGET; tryAMAGICun(neg);
2335 const int flags = SvFLAGS(sv);
2337 if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
2338 /* It's publicly an integer, or privately an integer-not-float */
2341 if (SvIVX(sv) == IV_MIN) {
2342 /* 2s complement assumption. */
2343 SETi(SvIVX(sv)); /* special case: -((UV)IV_MAX+1) == IV_MIN */
2346 else if (SvUVX(sv) <= IV_MAX) {
2351 else if (SvIVX(sv) != IV_MIN) {
2355 #ifdef PERL_PRESERVE_IVUV
2364 else if (SvPOKp(sv)) {
2366 const char * const s = SvPV_const(sv, len);
2367 if (isIDFIRST(*s)) {
2368 sv_setpvn(TARG, "-", 1);
2371 else if (*s == '+' || *s == '-') {
2373 *SvPV_force(TARG, len) = *s == '-' ? '+' : '-';
2375 else if (DO_UTF8(sv)) {
2378 goto oops_its_an_int;
2380 sv_setnv(TARG, -SvNV(sv));
2382 sv_setpvn(TARG, "-", 1);
2389 goto oops_its_an_int;
2390 sv_setnv(TARG, -SvNV(sv));
2402 dVAR; dSP; tryAMAGICunSET(not);
2403 *PL_stack_sp = boolSV(!SvTRUE(*PL_stack_sp));
2409 dVAR; dSP; dTARGET; tryAMAGICun(compl);
2414 if (PL_op->op_private & HINT_INTEGER) {
2415 const IV i = ~SvIV_nomg(sv);
2419 const UV u = ~SvUV_nomg(sv);
2428 (void)SvPV_nomg_const(sv,len); /* force check for uninit var */
2429 sv_setsv_nomg(TARG, sv);
2430 tmps = (U8*)SvPV_force(TARG, len);
2433 /* Calculate exact length, let's not estimate. */
2438 U8 * const send = tmps + len;
2439 U8 * const origtmps = tmps;
2440 const UV utf8flags = UTF8_ALLOW_ANYUV;
2442 while (tmps < send) {
2443 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2445 targlen += UNISKIP(~c);
2451 /* Now rewind strings and write them. */
2458 Newx(result, targlen + 1, U8);
2460 while (tmps < send) {
2461 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2463 p = uvchr_to_utf8_flags(p, ~c, UNICODE_ALLOW_ANY);
2466 sv_usepvn_flags(TARG, (char*)result, targlen,
2467 SV_HAS_TRAILING_NUL);
2474 Newx(result, nchar + 1, U8);
2476 while (tmps < send) {
2477 const U8 c = (U8)utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2482 sv_usepvn_flags(TARG, (char*)result, nchar, SV_HAS_TRAILING_NUL);
2490 register long *tmpl;
2491 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
2494 for ( ; anum >= (I32)sizeof(long); anum -= (I32)sizeof(long), tmpl++)
2499 for ( ; anum > 0; anum--, tmps++)
2508 /* integer versions of some of the above */
2512 dVAR; dSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
2515 SETi( left * right );
2523 dVAR; dSP; dATARGET; tryAMAGICbin(div,opASSIGN);
2527 DIE(aTHX_ "Illegal division by zero");
2530 /* avoid FPE_INTOVF on some platforms when num is IV_MIN */
2534 value = num / value;
2543 /* This is the vanilla old i_modulo. */
2544 dVAR; dSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
2548 DIE(aTHX_ "Illegal modulus zero");
2549 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2553 SETi( left % right );
2558 #if defined(__GLIBC__) && IVSIZE == 8
2562 /* This is the i_modulo with the workaround for the _moddi3 bug
2563 * in (at least) glibc 2.2.5 (the PERL_ABS() the workaround).
2564 * See below for pp_i_modulo. */
2565 dVAR; dVAR; dSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
2569 DIE(aTHX_ "Illegal modulus zero");
2570 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2574 SETi( left % PERL_ABS(right) );
2582 dVAR; dSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
2586 DIE(aTHX_ "Illegal modulus zero");
2587 /* The assumption is to use hereafter the old vanilla version... */
2589 PL_ppaddr[OP_I_MODULO] =
2591 /* .. but if we have glibc, we might have a buggy _moddi3
2592 * (at least glicb 2.2.5 is known to have this bug), in other
2593 * words our integer modulus with negative quad as the second
2594 * argument might be broken. Test for this and re-patch the
2595 * opcode dispatch table if that is the case, remembering to
2596 * also apply the workaround so that this first round works
2597 * right, too. See [perl #9402] for more information. */
2598 #if defined(__GLIBC__) && IVSIZE == 8
2602 /* Cannot do this check with inlined IV constants since
2603 * that seems to work correctly even with the buggy glibc. */
2605 /* Yikes, we have the bug.
2606 * Patch in the workaround version. */
2608 PL_ppaddr[OP_I_MODULO] =
2609 &Perl_pp_i_modulo_1;
2610 /* Make certain we work right this time, too. */
2611 right = PERL_ABS(right);
2615 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2619 SETi( left % right );
2626 dVAR; dSP; dATARGET; tryAMAGICbin(add,opASSIGN);
2629 SETi( left + right );
2636 dVAR; dSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
2639 SETi( left - right );
2646 dVAR; dSP; tryAMAGICbinSET(lt,0);
2649 SETs(boolSV(left < right));
2656 dVAR; dSP; tryAMAGICbinSET(gt,0);
2659 SETs(boolSV(left > right));
2666 dVAR; dSP; tryAMAGICbinSET(le,0);
2669 SETs(boolSV(left <= right));
2676 dVAR; dSP; tryAMAGICbinSET(ge,0);
2679 SETs(boolSV(left >= right));
2686 dVAR; dSP; tryAMAGICbinSET(eq,0);
2689 SETs(boolSV(left == right));
2696 dVAR; dSP; tryAMAGICbinSET(ne,0);
2699 SETs(boolSV(left != right));
2706 dVAR; dSP; dTARGET; tryAMAGICbin(ncmp,0);
2713 else if (left < right)
2724 dVAR; dSP; dTARGET; tryAMAGICun(neg);
2729 /* High falutin' math. */
2733 dVAR; dSP; dTARGET; tryAMAGICbin(atan2,0);
2736 SETn(Perl_atan2(left, right));
2744 int amg_type = sin_amg;
2745 const char *neg_report = NULL;
2746 NV (*func)(NV) = Perl_sin;
2747 const int op_type = PL_op->op_type;
2764 amg_type = sqrt_amg;
2766 neg_report = "sqrt";
2770 tryAMAGICun_var(amg_type);
2772 const NV value = POPn;
2774 if (op_type == OP_LOG ? (value <= 0.0) : (value < 0.0)) {
2775 SET_NUMERIC_STANDARD();
2776 DIE(aTHX_ "Can't take %s of %"NVgf, neg_report, value);
2779 XPUSHn(func(value));
2784 /* Support Configure command-line overrides for rand() functions.
2785 After 5.005, perhaps we should replace this by Configure support
2786 for drand48(), random(), or rand(). For 5.005, though, maintain
2787 compatibility by calling rand() but allow the user to override it.
2788 See INSTALL for details. --Andy Dougherty 15 July 1998
2790 /* Now it's after 5.005, and Configure supports drand48() and random(),
2791 in addition to rand(). So the overrides should not be needed any more.
2792 --Jarkko Hietaniemi 27 September 1998
2795 #ifndef HAS_DRAND48_PROTO
2796 extern double drand48 (void);
2809 if (!PL_srand_called) {
2810 (void)seedDrand01((Rand_seed_t)seed());
2811 PL_srand_called = TRUE;
2821 const UV anum = (MAXARG < 1) ? seed() : POPu;
2822 (void)seedDrand01((Rand_seed_t)anum);
2823 PL_srand_called = TRUE;
2830 dVAR; dSP; dTARGET; tryAMAGICun(int);
2832 const IV iv = TOPi; /* attempt to convert to IV if possible. */
2833 /* XXX it's arguable that compiler casting to IV might be subtly
2834 different from modf (for numbers inside (IV_MIN,UV_MAX)) in which
2835 else preferring IV has introduced a subtle behaviour change bug. OTOH
2836 relying on floating point to be accurate is a bug. */
2840 else if (SvIOK(TOPs)) {
2847 const NV value = TOPn;
2849 if (value < (NV)UV_MAX + 0.5) {
2852 SETn(Perl_floor(value));
2856 if (value > (NV)IV_MIN - 0.5) {
2859 SETn(Perl_ceil(value));
2869 dVAR; dSP; dTARGET; tryAMAGICun(abs);
2871 /* This will cache the NV value if string isn't actually integer */
2876 else if (SvIOK(TOPs)) {
2877 /* IVX is precise */
2879 SETu(TOPu); /* force it to be numeric only */
2887 /* 2s complement assumption. Also, not really needed as
2888 IV_MIN and -IV_MIN should both be %100...00 and NV-able */
2894 const NV value = TOPn;
2908 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES;
2912 SV* const sv = POPs;
2914 tmps = (SvPV_const(sv, len));
2916 /* If Unicode, try to downgrade
2917 * If not possible, croak. */
2918 SV* const tsv = sv_2mortal(newSVsv(sv));
2921 sv_utf8_downgrade(tsv, FALSE);
2922 tmps = SvPV_const(tsv, len);
2924 if (PL_op->op_type == OP_HEX)
2927 while (*tmps && len && isSPACE(*tmps))
2933 result_uv = grok_hex (tmps, &len, &flags, &result_nv);
2935 else if (*tmps == 'b')
2936 result_uv = grok_bin (tmps, &len, &flags, &result_nv);
2938 result_uv = grok_oct (tmps, &len, &flags, &result_nv);
2940 if (flags & PERL_SCAN_GREATER_THAN_UV_MAX) {
2954 SV * const sv = TOPs;
2957 /* For an overloaded scalar, we can't know in advance if it's going to
2958 be UTF-8 or not. Also, we can't call sv_len_utf8 as it likes to
2959 cache the length. Maybe that should be a documented feature of it.
2962 const char *const p = SvPV_const(sv, len);
2965 SETi(utf8_length((U8*)p, (U8*)p + len));
2971 else if (DO_UTF8(sv))
2972 SETi(sv_len_utf8(sv));
2988 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
2990 const I32 arybase = CopARYBASE_get(PL_curcop);
2992 const char *repl = NULL;
2994 const int num_args = PL_op->op_private & 7;
2995 bool repl_need_utf8_upgrade = FALSE;
2996 bool repl_is_utf8 = FALSE;
2998 SvTAINTED_off(TARG); /* decontaminate */
2999 SvUTF8_off(TARG); /* decontaminate */
3003 repl = SvPV_const(repl_sv, repl_len);
3004 repl_is_utf8 = DO_UTF8(repl_sv) && SvCUR(repl_sv);
3014 sv_utf8_upgrade(sv);
3016 else if (DO_UTF8(sv))
3017 repl_need_utf8_upgrade = TRUE;
3019 tmps = SvPV_const(sv, curlen);
3021 utf8_curlen = sv_len_utf8(sv);
3022 if (utf8_curlen == curlen)
3025 curlen = utf8_curlen;
3030 if (pos >= arybase) {
3048 else if (len >= 0) {
3050 if (rem > (I32)curlen)
3065 Perl_croak(aTHX_ "substr outside of string");
3066 if (ckWARN(WARN_SUBSTR))
3067 Perl_warner(aTHX_ packWARN(WARN_SUBSTR), "substr outside of string");
3071 const I32 upos = pos;
3072 const I32 urem = rem;
3074 sv_pos_u2b(sv, &pos, &rem);
3076 /* we either return a PV or an LV. If the TARG hasn't been used
3077 * before, or is of that type, reuse it; otherwise use a mortal
3078 * instead. Note that LVs can have an extended lifetime, so also
3079 * dont reuse if refcount > 1 (bug #20933) */
3080 if (SvTYPE(TARG) > SVt_NULL) {
3081 if ( (SvTYPE(TARG) == SVt_PVLV)
3082 ? (!lvalue || SvREFCNT(TARG) > 1)
3085 TARG = sv_newmortal();
3089 sv_setpvn(TARG, tmps, rem);
3090 #ifdef USE_LOCALE_COLLATE
3091 sv_unmagic(TARG, PERL_MAGIC_collxfrm);
3096 SV* repl_sv_copy = NULL;
3098 if (repl_need_utf8_upgrade) {
3099 repl_sv_copy = newSVsv(repl_sv);
3100 sv_utf8_upgrade(repl_sv_copy);
3101 repl = SvPV_const(repl_sv_copy, repl_len);
3102 repl_is_utf8 = DO_UTF8(repl_sv_copy) && SvCUR(sv);
3104 sv_insert(sv, pos, rem, repl, repl_len);
3108 SvREFCNT_dec(repl_sv_copy);
3110 else if (lvalue) { /* it's an lvalue! */
3111 if (!SvGMAGICAL(sv)) {
3113 SvPV_force_nolen(sv);
3114 if (ckWARN(WARN_SUBSTR))
3115 Perl_warner(aTHX_ packWARN(WARN_SUBSTR),
3116 "Attempt to use reference as lvalue in substr");
3118 if (isGV_with_GP(sv))
3119 SvPV_force_nolen(sv);
3120 else if (SvOK(sv)) /* is it defined ? */
3121 (void)SvPOK_only_UTF8(sv);
3123 sv_setpvn(sv,"",0); /* avoid lexical reincarnation */
3126 if (SvTYPE(TARG) < SVt_PVLV) {
3127 sv_upgrade(TARG, SVt_PVLV);
3128 sv_magic(TARG, NULL, PERL_MAGIC_substr, NULL, 0);
3132 if (LvTARG(TARG) != sv) {
3134 SvREFCNT_dec(LvTARG(TARG));
3135 LvTARG(TARG) = SvREFCNT_inc_simple(sv);
3137 LvTARGOFF(TARG) = upos;
3138 LvTARGLEN(TARG) = urem;
3142 PUSHs(TARG); /* avoid SvSETMAGIC here */
3149 register const IV size = POPi;
3150 register const IV offset = POPi;
3151 register SV * const src = POPs;
3152 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
3154 SvTAINTED_off(TARG); /* decontaminate */
3155 if (lvalue) { /* it's an lvalue! */
3156 if (SvREFCNT(TARG) > 1) /* don't share the TARG (#20933) */
3157 TARG = sv_newmortal();
3158 if (SvTYPE(TARG) < SVt_PVLV) {
3159 sv_upgrade(TARG, SVt_PVLV);
3160 sv_magic(TARG, NULL, PERL_MAGIC_vec, NULL, 0);
3163 if (LvTARG(TARG) != src) {
3165 SvREFCNT_dec(LvTARG(TARG));
3166 LvTARG(TARG) = SvREFCNT_inc_simple(src);
3168 LvTARGOFF(TARG) = offset;
3169 LvTARGLEN(TARG) = size;
3172 sv_setuv(TARG, do_vecget(src, offset, size));
3188 const char *little_p;
3189 const I32 arybase = CopARYBASE_get(PL_curcop);
3192 const bool is_index = PL_op->op_type == OP_INDEX;
3195 /* arybase is in characters, like offset, so combine prior to the
3196 UTF-8 to bytes calculation. */
3197 offset = POPi - arybase;
3201 big_p = SvPV_const(big, biglen);
3202 little_p = SvPV_const(little, llen);
3204 big_utf8 = DO_UTF8(big);
3205 little_utf8 = DO_UTF8(little);
3206 if (big_utf8 ^ little_utf8) {
3207 /* One needs to be upgraded. */
3208 if (little_utf8 && !PL_encoding) {
3209 /* Well, maybe instead we might be able to downgrade the small
3211 char * const pv = (char*)bytes_from_utf8((U8 *)little_p, &llen,
3214 /* If the large string is ISO-8859-1, and it's not possible to
3215 convert the small string to ISO-8859-1, then there is no
3216 way that it could be found anywhere by index. */
3221 /* At this point, pv is a malloc()ed string. So donate it to temp
3222 to ensure it will get free()d */
3223 little = temp = newSV(0);
3224 sv_usepvn(temp, pv, llen);
3225 little_p = SvPVX(little);
3228 ? newSVpvn(big_p, biglen) : newSVpvn(little_p, llen);
3231 sv_recode_to_utf8(temp, PL_encoding);
3233 sv_utf8_upgrade(temp);
3238 big_p = SvPV_const(big, biglen);
3241 little_p = SvPV_const(little, llen);
3245 if (SvGAMAGIC(big)) {
3246 /* Life just becomes a lot easier if I use a temporary here.
3247 Otherwise I need to avoid calls to sv_pos_u2b(), which (dangerously)
3248 will trigger magic and overloading again, as will fbm_instr()
3250 big = sv_2mortal(newSVpvn(big_p, biglen));
3255 if (SvGAMAGIC(little) || (is_index && !SvOK(little))) {
3256 /* index && SvOK() is a hack. fbm_instr() calls SvPV_const, which will
3257 warn on undef, and we've already triggered a warning with the
3258 SvPV_const some lines above. We can't remove that, as we need to
3259 call some SvPV to trigger overloading early and find out if the
3261 This is all getting to messy. The API isn't quite clean enough,
3262 because data access has side effects.
3264 little = sv_2mortal(newSVpvn(little_p, llen));
3267 little_p = SvPVX(little);
3271 offset = is_index ? 0 : biglen;
3273 if (big_utf8 && offset > 0)
3274 sv_pos_u2b(big, &offset, 0);
3280 else if (offset > (I32)biglen)
3282 if (!(little_p = is_index
3283 ? fbm_instr((unsigned char*)big_p + offset,
3284 (unsigned char*)big_p + biglen, little, 0)
3285 : rninstr(big_p, big_p + offset,
3286 little_p, little_p + llen)))
3289 retval = little_p - big_p;
3290 if (retval > 0 && big_utf8)
3291 sv_pos_b2u(big, &retval);
3296 PUSHi(retval + arybase);
3302 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
3303 do_sprintf(TARG, SP-MARK, MARK+1);
3304 TAINT_IF(SvTAINTED(TARG));
3316 const U8 *s = (U8*)SvPV_const(argsv, len);
3318 if (PL_encoding && SvPOK(argsv) && !DO_UTF8(argsv)) {
3319 SV * const tmpsv = sv_2mortal(newSVsv(argsv));
3320 s = (U8*)sv_recode_to_utf8(tmpsv, PL_encoding);
3324 XPUSHu(DO_UTF8(argsv) ?
3325 utf8n_to_uvchr(s, UTF8_MAXBYTES, 0, UTF8_ALLOW_ANYUV) :
3337 if (((SvIOK_notUV(TOPs) && SvIV(TOPs) < 0)
3339 (SvNOK(TOPs) && SvNV(TOPs) < 0.0))) {
3341 value = POPu; /* chr(-1) eq chr(0xff), etc. */
3343 (void) POPs; /* Ignore the argument value. */
3344 value = UNICODE_REPLACEMENT;
3350 SvUPGRADE(TARG,SVt_PV);
3352 if (value > 255 && !IN_BYTES) {
3353 SvGROW(TARG, (STRLEN)UNISKIP(value)+1);
3354 tmps = (char*)uvchr_to_utf8_flags((U8*)SvPVX(TARG), value, 0);
3355 SvCUR_set(TARG, tmps - SvPVX_const(TARG));
3357 (void)SvPOK_only(TARG);
3366 *tmps++ = (char)value;
3368 (void)SvPOK_only(TARG);
3370 if (PL_encoding && !IN_BYTES) {
3371 sv_recode_to_utf8(TARG, PL_encoding);
3373 if (SvCUR(TARG) == 0 || !is_utf8_string((U8*)tmps, SvCUR(TARG)) ||
3374 UNICODE_IS_REPLACEMENT(utf8_to_uvchr((U8*)tmps, NULL))) {
3378 *tmps++ = (char)value;
3394 const char *tmps = SvPV_const(left, len);
3396 if (DO_UTF8(left)) {
3397 /* If Unicode, try to downgrade.
3398 * If not possible, croak.
3399 * Yes, we made this up. */
3400 SV* const tsv = sv_2mortal(newSVsv(left));
3403 sv_utf8_downgrade(tsv, FALSE);
3404 tmps = SvPV_const(tsv, len);
3406 # ifdef USE_ITHREADS
3408 if (!PL_reentrant_buffer->_crypt_struct_buffer) {
3409 /* This should be threadsafe because in ithreads there is only
3410 * one thread per interpreter. If this would not be true,
3411 * we would need a mutex to protect this malloc. */
3412 PL_reentrant_buffer->_crypt_struct_buffer =
3413 (struct crypt_data *)safemalloc(sizeof(struct crypt_data));
3414 #if defined(__GLIBC__) || defined(__EMX__)
3415 if (PL_reentrant_buffer->_crypt_struct_buffer) {
3416 PL_reentrant_buffer->_crypt_struct_buffer->initialized = 0;
3417 /* work around glibc-2.2.5 bug */
3418 PL_reentrant_buffer->_crypt_struct_buffer->current_saltbits = 0;
3422 # endif /* HAS_CRYPT_R */
3423 # endif /* USE_ITHREADS */
3425 sv_setpv(TARG, fcrypt(tmps, SvPV_nolen_const(right)));
3427 sv_setpv(TARG, PerlProc_crypt(tmps, SvPV_nolen_const(right)));
3433 "The crypt() function is unimplemented due to excessive paranoia.");
3445 bool inplace = TRUE;
3447 const int op_type = PL_op->op_type;
3450 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
3456 s = (const U8*)SvPV_nomg_const(source, slen);
3462 if (slen && DO_UTF8(source) && UTF8_IS_START(*s)) {
3464 utf8_to_uvchr(s, &ulen);
3465 if (op_type == OP_UCFIRST) {
3466 toTITLE_utf8(s, tmpbuf, &tculen);
3468 toLOWER_utf8(s, tmpbuf, &tculen);
3470 /* If the two differ, we definately cannot do inplace. */
3471 inplace = (ulen == tculen);
3472 need = slen + 1 - ulen + tculen;
3478 if (SvPADTMP(source) && !SvREADONLY(source) && inplace) {
3479 /* We can convert in place. */
3482 s = d = (U8*)SvPV_force_nomg(source, slen);
3488 SvUPGRADE(dest, SVt_PV);
3489 d = (U8*)SvGROW(dest, need);
3490 (void)SvPOK_only(dest);
3499 /* slen is the byte length of the whole SV.
3500 * ulen is the byte length of the original Unicode character
3501 * stored as UTF-8 at s.
3502 * tculen is the byte length of the freshly titlecased (or
3503 * lowercased) Unicode character stored as UTF-8 at tmpbuf.
3504 * We first set the result to be the titlecased (/lowercased)
3505 * character, and then append the rest of the SV data. */
3506 sv_setpvn(dest, (char*)tmpbuf, tculen);
3508 sv_catpvn(dest, (char*)(s + ulen), slen - ulen);
3512 Copy(tmpbuf, d, tculen, U8);
3513 SvCUR_set(dest, need - 1);
3518 if (IN_LOCALE_RUNTIME) {
3521 *d = (op_type == OP_UCFIRST)
3522 ? toUPPER_LC(*s) : toLOWER_LC(*s);
3525 *d = (op_type == OP_UCFIRST) ? toUPPER(*s) : toLOWER(*s);
3527 /* See bug #39028 */
3535 /* This will copy the trailing NUL */
3536 Copy(s + 1, d + 1, slen, U8);
3537 SvCUR_set(dest, need - 1);
3544 /* There's so much setup/teardown code common between uc and lc, I wonder if
3545 it would be worth merging the two, and just having a switch outside each
3546 of the three tight loops. */
3560 if (SvPADTMP(source) && !SvREADONLY(source) && !SvAMAGIC(source)
3561 && !DO_UTF8(source)) {
3562 /* We can convert in place. */
3565 s = d = (U8*)SvPV_force_nomg(source, len);
3572 /* The old implementation would copy source into TARG at this point.
3573 This had the side effect that if source was undef, TARG was now
3574 an undefined SV with PADTMP set, and they don't warn inside
3575 sv_2pv_flags(). However, we're now getting the PV direct from
3576 source, which doesn't have PADTMP set, so it would warn. Hence the
3580 s = (const U8*)SvPV_nomg_const(source, len);
3587 SvUPGRADE(dest, SVt_PV);
3588 d = (U8*)SvGROW(dest, min);
3589 (void)SvPOK_only(dest);
3594 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
3595 to check DO_UTF8 again here. */
3597 if (DO_UTF8(source)) {
3598 const U8 *const send = s + len;
3599 U8 tmpbuf[UTF8_MAXBYTES+1];
3602 const STRLEN u = UTF8SKIP(s);
3605 toUPPER_utf8(s, tmpbuf, &ulen);
3606 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
3607 /* If the eventually required minimum size outgrows
3608 * the available space, we need to grow. */
3609 const UV o = d - (U8*)SvPVX_const(dest);
3611 /* If someone uppercases one million U+03B0s we SvGROW() one
3612 * million times. Or we could try guessing how much to
3613 allocate without allocating too much. Such is life. */
3615 d = (U8*)SvPVX(dest) + o;
3617 Copy(tmpbuf, d, ulen, U8);
3623 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3626 const U8 *const send = s + len;
3627 if (IN_LOCALE_RUNTIME) {
3630 for (; s < send; d++, s++)
3631 *d = toUPPER_LC(*s);
3634 for (; s < send; d++, s++)
3638 if (source != dest) {
3640 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3660 if (SvPADTMP(source) && !SvREADONLY(source) && !SvAMAGIC(source)
3661 && !DO_UTF8(source)) {
3662 /* We can convert in place. */
3665 s = d = (U8*)SvPV_force_nomg(source, len);
3672 /* The old implementation would copy source into TARG at this point.
3673 This had the side effect that if source was undef, TARG was now
3674 an undefined SV with PADTMP set, and they don't warn inside
3675 sv_2pv_flags(). However, we're now getting the PV direct from
3676 source, which doesn't have PADTMP set, so it would warn. Hence the
3680 s = (const U8*)SvPV_nomg_const(source, len);
3687 SvUPGRADE(dest, SVt_PV);
3688 d = (U8*)SvGROW(dest, min);
3689 (void)SvPOK_only(dest);
3694 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
3695 to check DO_UTF8 again here. */
3697 if (DO_UTF8(source)) {
3698 const U8 *const send = s + len;
3699 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
3702 const STRLEN u = UTF8SKIP(s);
3704 const UV uv = toLOWER_utf8(s, tmpbuf, &ulen);
3706 #define GREEK_CAPITAL_LETTER_SIGMA 0x03A3 /* Unicode U+03A3 */
3707 if (uv == GREEK_CAPITAL_LETTER_SIGMA) {
3710 * Now if the sigma is NOT followed by
3711 * /$ignorable_sequence$cased_letter/;
3712 * and it IS preceded by /$cased_letter$ignorable_sequence/;
3713 * where $ignorable_sequence is [\x{2010}\x{AD}\p{Mn}]*
3714 * and $cased_letter is [\p{Ll}\p{Lo}\p{Lt}]
3715 * then it should be mapped to 0x03C2,
3716 * (GREEK SMALL LETTER FINAL SIGMA),
3717 * instead of staying 0x03A3.
3718 * "should be": in other words, this is not implemented yet.
3719 * See lib/unicore/SpecialCasing.txt.
3722 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
3723 /* If the eventually required minimum size outgrows
3724 * the available space, we need to grow. */
3725 const UV o = d - (U8*)SvPVX_const(dest);
3727 /* If someone lowercases one million U+0130s we SvGROW() one
3728 * million times. Or we could try guessing how much to
3729 allocate without allocating too much. Such is life. */
3731 d = (U8*)SvPVX(dest) + o;
3733 Copy(tmpbuf, d, ulen, U8);
3739 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3742 const U8 *const send = s + len;
3743 if (IN_LOCALE_RUNTIME) {
3746 for (; s < send; d++, s++)
3747 *d = toLOWER_LC(*s);
3750 for (; s < send; d++, s++)
3754 if (source != dest) {
3756 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3766 SV * const sv = TOPs;
3768 register const char *s = SvPV_const(sv,len);
3770 SvUTF8_off(TARG); /* decontaminate */
3773 SvUPGRADE(TARG, SVt_PV);
3774 SvGROW(TARG, (len * 2) + 1);
3778 if (UTF8_IS_CONTINUED(*s)) {
3779 STRLEN ulen = UTF8SKIP(s);
3803 SvCUR_set(TARG, d - SvPVX_const(TARG));
3804 (void)SvPOK_only_UTF8(TARG);
3807 sv_setpvn(TARG, s, len);
3809 if (SvSMAGICAL(TARG))
3818 dVAR; dSP; dMARK; dORIGMARK;
3819 register AV* const av = (AV*)POPs;
3820 register const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
3822 if (SvTYPE(av) == SVt_PVAV) {
3823 const I32 arybase = CopARYBASE_get(PL_curcop);
3824 if (lval && PL_op->op_private & OPpLVAL_INTRO) {
3827 for (svp = MARK + 1; svp <= SP; svp++) {
3828 const I32 elem = SvIVx(*svp);
3832 if (max > AvMAX(av))
3835 while (++MARK <= SP) {
3837 I32 elem = SvIVx(*MARK);
3841 svp = av_fetch(av, elem, lval);
3843 if (!svp || *svp == &PL_sv_undef)
3844 DIE(aTHX_ PL_no_aelem, elem);
3845 if (PL_op->op_private & OPpLVAL_INTRO)
3846 save_aelem(av, elem, svp);
3848 *MARK = svp ? *svp : &PL_sv_undef;
3851 if (GIMME != G_ARRAY) {
3853 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
3859 /* Associative arrays. */
3865 HV * const hash = (HV*)POPs;
3867 const I32 gimme = GIMME_V;
3870 /* might clobber stack_sp */
3871 entry = hv_iternext(hash);
3876 SV* const sv = hv_iterkeysv(entry);
3877 PUSHs(sv); /* won't clobber stack_sp */
3878 if (gimme == G_ARRAY) {
3881 /* might clobber stack_sp */
3882 val = hv_iterval(hash, entry);
3887 else if (gimme == G_SCALAR)
3897 const I32 gimme = GIMME_V;
3898 const I32 discard = (gimme == G_VOID) ? G_DISCARD : 0;
3900 if (PL_op->op_private & OPpSLICE) {
3902 HV * const hv = (HV*)POPs;
3903 const U32 hvtype = SvTYPE(hv);
3904 if (hvtype == SVt_PVHV) { /* hash element */
3905 while (++MARK <= SP) {
3906 SV * const sv = hv_delete_ent(hv, *MARK, discard, 0);
3907 *MARK = sv ? sv : &PL_sv_undef;
3910 else if (hvtype == SVt_PVAV) { /* array element */
3911 if (PL_op->op_flags & OPf_SPECIAL) {
3912 while (++MARK <= SP) {
3913 SV * const sv = av_delete((AV*)hv, SvIV(*MARK), discard);
3914 *MARK = sv ? sv : &PL_sv_undef;
3919 DIE(aTHX_ "Not a HASH reference");
3922 else if (gimme == G_SCALAR) {
3927 *++MARK = &PL_sv_undef;
3933 HV * const hv = (HV*)POPs;
3935 if (SvTYPE(hv) == SVt_PVHV)
3936 sv = hv_delete_ent(hv, keysv, discard, 0);
3937 else if (SvTYPE(hv) == SVt_PVAV) {
3938 if (PL_op->op_flags & OPf_SPECIAL)
3939 sv = av_delete((AV*)hv, SvIV(keysv), discard);
3941 DIE(aTHX_ "panic: avhv_delete no longer supported");
3944 DIE(aTHX_ "Not a HASH reference");
3960 if (PL_op->op_private & OPpEXISTS_SUB) {
3962 SV * const sv = POPs;
3963 CV * const cv = sv_2cv(sv, &hv, &gv, 0);
3966 if (gv && isGV(gv) && GvCV(gv) && !GvCVGEN(gv))
3972 if (SvTYPE(hv) == SVt_PVHV) {
3973 if (hv_exists_ent(hv, tmpsv, 0))
3976 else if (SvTYPE(hv) == SVt_PVAV) {
3977 if (PL_op->op_flags & OPf_SPECIAL) { /* array element */
3978 if (av_exists((AV*)hv, SvIV(tmpsv)))
3983 DIE(aTHX_ "Not a HASH reference");
3990 dVAR; dSP; dMARK; dORIGMARK;
3991 register HV * const hv = (HV*)POPs;
3992 register const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
3993 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
3994 bool other_magic = FALSE;
4000 other_magic = mg_find((SV*)hv, PERL_MAGIC_env) ||
4001 ((mg = mg_find((SV*)hv, PERL_MAGIC_tied))
4002 /* Try to preserve the existenceness of a tied hash
4003 * element by using EXISTS and DELETE if possible.
4004 * Fallback to FETCH and STORE otherwise */
4005 && (stash = SvSTASH(SvRV(SvTIED_obj((SV*)hv, mg))))
4006 && gv_fetchmethod_autoload(stash, "EXISTS", TRUE)
4007 && gv_fetchmethod_autoload(stash, "DELETE", TRUE));
4010 while (++MARK <= SP) {
4011 SV * const keysv = *MARK;
4014 bool preeminent = FALSE;
4017 preeminent = SvRMAGICAL(hv) && !other_magic ? 1 :
4018 hv_exists_ent(hv, keysv, 0);
4021 he = hv_fetch_ent(hv, keysv, lval, 0);
4022 svp = he ? &HeVAL(he) : 0;
4025 if (!svp || *svp == &PL_sv_undef) {
4026 DIE(aTHX_ PL_no_helem_sv, keysv);
4029 if (HvNAME_get(hv) && isGV(*svp))
4030 save_gp((GV*)*svp, !(PL_op->op_flags & OPf_SPECIAL));
4033 save_helem(hv, keysv, svp);
4036 const char * const key = SvPV_const(keysv, keylen);
4037 SAVEDELETE(hv, savepvn(key,keylen),
4038 SvUTF8(keysv) ? -(I32)keylen : (I32)keylen);
4043 *MARK = svp ? *svp : &PL_sv_undef;
4045 if (GIMME != G_ARRAY) {
4047 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
4053 /* List operators. */
4058 if (GIMME != G_ARRAY) {
4060 *MARK = *SP; /* unwanted list, return last item */
4062 *MARK = &PL_sv_undef;
4072 SV ** const lastrelem = PL_stack_sp;
4073 SV ** const lastlelem = PL_stack_base + POPMARK;
4074 SV ** const firstlelem = PL_stack_base + POPMARK + 1;
4075 register SV ** const firstrelem = lastlelem + 1;
4076 const I32 arybase = CopARYBASE_get(PL_curcop);
4077 I32 is_something_there = FALSE;
4079 register const I32 max = lastrelem - lastlelem;
4080 register SV **lelem;
4082 if (GIMME != G_ARRAY) {
4083 I32 ix = SvIVx(*lastlelem);
4088 if (ix < 0 || ix >= max)
4089 *firstlelem = &PL_sv_undef;
4091 *firstlelem = firstrelem[ix];
4097 SP = firstlelem - 1;
4101 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
4102 I32 ix = SvIVx(*lelem);
4107 if (ix < 0 || ix >= max)
4108 *lelem = &PL_sv_undef;
4110 is_something_there = TRUE;
4111 if (!(*lelem = firstrelem[ix]))
4112 *lelem = &PL_sv_undef;
4115 if (is_something_there)
4118 SP = firstlelem - 1;
4124 dVAR; dSP; dMARK; dORIGMARK;
4125 const I32 items = SP - MARK;
4126 SV * const av = (SV *) av_make(items, MARK+1);
4127 SP = ORIGMARK; /* av_make() might realloc stack_sp */
4128 XPUSHs(sv_2mortal((PL_op->op_flags & OPf_SPECIAL)
4129 ? newRV_noinc(av) : av));
4135 dVAR; dSP; dMARK; dORIGMARK;
4136 HV* const hv = newHV();
4139 SV * const key = *++MARK;
4140 SV * const val = newSV(0);
4142 sv_setsv(val, *++MARK);
4143 else if (ckWARN(WARN_MISC))
4144 Perl_warner(aTHX_ packWARN(WARN_MISC), "Odd number of elements in anonymous hash");
4145 (void)hv_store_ent(hv,key,val,0);
4148 XPUSHs(sv_2mortal((PL_op->op_flags & OPf_SPECIAL)
4149 ? newRV_noinc((SV*) hv) : (SV*)hv));
4155 dVAR; dSP; dMARK; dORIGMARK;
4156 register AV *ary = (AV*)*++MARK;
4160 register I32 offset;
4161 register I32 length;
4165 const MAGIC * const mg = SvTIED_mg((SV*)ary, PERL_MAGIC_tied);
4168 *MARK-- = SvTIED_obj((SV*)ary, mg);
4172 call_method("SPLICE",GIMME_V);
4181 offset = i = SvIVx(*MARK);
4183 offset += AvFILLp(ary) + 1;
4185 offset -= CopARYBASE_get(PL_curcop);
4187 DIE(aTHX_ PL_no_aelem, i);
4189 length = SvIVx(*MARK++);
4191 length += AvFILLp(ary) - offset + 1;
4197 length = AvMAX(ary) + 1; /* close enough to infinity */
4201 length = AvMAX(ary) + 1;
4203 if (offset > AvFILLp(ary) + 1) {
4204 if (ckWARN(WARN_MISC))
4205 Perl_warner(aTHX_ packWARN(WARN_MISC), "splice() offset past end of array" );
4206 offset = AvFILLp(ary) + 1;
4208 after = AvFILLp(ary) + 1 - (offset + length);
4209 if (after < 0) { /* not that much array */
4210 length += after; /* offset+length now in array */
4216 /* At this point, MARK .. SP-1 is our new LIST */
4219 diff = newlen - length;
4220 if (newlen && !AvREAL(ary) && AvREIFY(ary))
4223 /* make new elements SVs now: avoid problems if they're from the array */
4224 for (dst = MARK, i = newlen; i; i--) {
4225 SV * const h = *dst;
4226 *dst++ = newSVsv(h);
4229 if (diff < 0) { /* shrinking the area */
4230 SV **tmparyval = NULL;
4232 Newx(tmparyval, newlen, SV*); /* so remember insertion */
4233 Copy(MARK, tmparyval, newlen, SV*);
4236 MARK = ORIGMARK + 1;
4237 if (GIMME == G_ARRAY) { /* copy return vals to stack */
4238 MEXTEND(MARK, length);
4239 Copy(AvARRAY(ary)+offset, MARK, length, SV*);
4241 EXTEND_MORTAL(length);
4242 for (i = length, dst = MARK; i; i--) {
4243 sv_2mortal(*dst); /* free them eventualy */
4250 *MARK = AvARRAY(ary)[offset+length-1];
4253 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
4254 SvREFCNT_dec(*dst++); /* free them now */
4257 AvFILLp(ary) += diff;
4259 /* pull up or down? */
4261 if (offset < after) { /* easier to pull up */
4262 if (offset) { /* esp. if nothing to pull */
4263 src = &AvARRAY(ary)[offset-1];
4264 dst = src - diff; /* diff is negative */
4265 for (i = offset; i > 0; i--) /* can't trust Copy */
4269 SvPV_set(ary, (char*)(AvARRAY(ary) - diff)); /* diff is negative */
4273 if (after) { /* anything to pull down? */
4274 src = AvARRAY(ary) + offset + length;
4275 dst = src + diff; /* diff is negative */
4276 Move(src, dst, after, SV*);
4278 dst = &AvARRAY(ary)[AvFILLp(ary)+1];
4279 /* avoid later double free */
4283 dst[--i] = &PL_sv_undef;
4286 Copy( tmparyval, AvARRAY(ary) + offset, newlen, SV* );
4287 Safefree(tmparyval);
4290 else { /* no, expanding (or same) */
4291 SV** tmparyval = NULL;
4293 Newx(tmparyval, length, SV*); /* so remember deletion */
4294 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
4297 if (diff > 0) { /* expanding */
4298 /* push up or down? */
4299 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
4303 Move(src, dst, offset, SV*);
4305 SvPV_set(ary, (char*)(AvARRAY(ary) - diff));/* diff is positive */
4307 AvFILLp(ary) += diff;
4310 if (AvFILLp(ary) + diff >= AvMAX(ary)) /* oh, well */
4311 av_extend(ary, AvFILLp(ary) + diff);
4312 AvFILLp(ary) += diff;
4315 dst = AvARRAY(ary) + AvFILLp(ary);
4317 for (i = after; i; i--) {
4325 Copy( MARK, AvARRAY(ary) + offset, newlen, SV* );
4328 MARK = ORIGMARK + 1;
4329 if (GIMME == G_ARRAY) { /* copy return vals to stack */
4331 Copy(tmparyval, MARK, length, SV*);
4333 EXTEND_MORTAL(length);
4334 for (i = length, dst = MARK; i; i--) {
4335 sv_2mortal(*dst); /* free them eventualy */
4342 else if (length--) {
4343 *MARK = tmparyval[length];
4346 while (length-- > 0)
4347 SvREFCNT_dec(tmparyval[length]);
4351 *MARK = &PL_sv_undef;
4352 Safefree(tmparyval);
4360 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
4361 register AV * const ary = (AV*)*++MARK;
4362 const MAGIC * const mg = SvTIED_mg((SV*)ary, PERL_MAGIC_tied);
4365 *MARK-- = SvTIED_obj((SV*)ary, mg);
4369 call_method("PUSH",G_SCALAR|G_DISCARD);
4373 PUSHi( AvFILL(ary) + 1 );
4376 for (++MARK; MARK <= SP; MARK++) {
4377 SV * const sv = newSV(0);
4379 sv_setsv(sv, *MARK);
4380 av_store(ary, AvFILLp(ary)+1, sv);
4383 PUSHi( AvFILLp(ary) + 1 );
4392 AV * const av = (AV*)POPs;
4393 SV * const sv = PL_op->op_type == OP_SHIFT ? av_shift(av) : av_pop(av);
4397 (void)sv_2mortal(sv);
4404 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
4405 register AV *ary = (AV*)*++MARK;
4406 const MAGIC * const mg = SvTIED_mg((SV*)ary, PERL_MAGIC_tied);
4409 *MARK-- = SvTIED_obj((SV*)ary, mg);
4413 call_method("UNSHIFT",G_SCALAR|G_DISCARD);
4419 av_unshift(ary, SP - MARK);
4421 SV * const sv = newSVsv(*++MARK);
4422 (void)av_store(ary, i++, sv);
4426 PUSHi( AvFILL(ary) + 1 );
4433 SV ** const oldsp = SP;
4435 if (GIMME == G_ARRAY) {
4438 register SV * const tmp = *MARK;
4442 /* safe as long as stack cannot get extended in the above */
4447 register char *down;
4451 PADOFFSET padoff_du;
4453 SvUTF8_off(TARG); /* decontaminate */
4455 do_join(TARG, &PL_sv_no, MARK, SP);
4457 sv_setsv(TARG, (SP > MARK)
4459 : (padoff_du = find_rundefsvoffset(),
4460 (padoff_du == NOT_IN_PAD
4461 || PAD_COMPNAME_FLAGS_isOUR(padoff_du))
4462 ? DEFSV : PAD_SVl(padoff_du)));
4463 up = SvPV_force(TARG, len);
4465 if (DO_UTF8(TARG)) { /* first reverse each character */
4466 U8* s = (U8*)SvPVX(TARG);
4467 const U8* send = (U8*)(s + len);
4469 if (UTF8_IS_INVARIANT(*s)) {
4474 if (!utf8_to_uvchr(s, 0))
4478 down = (char*)(s - 1);
4479 /* reverse this character */
4483 *down-- = (char)tmp;
4489 down = SvPVX(TARG) + len - 1;
4493 *down-- = (char)tmp;
4495 (void)SvPOK_only_UTF8(TARG);
4507 register IV limit = POPi; /* note, negative is forever */
4508 SV * const sv = POPs;
4510 register const char *s = SvPV_const(sv, len);
4511 const bool do_utf8 = DO_UTF8(sv);
4512 const char *strend = s + len;
4514 register REGEXP *rx;
4516 register const char *m;
4518 const STRLEN slen = do_utf8 ? utf8_length((U8*)s, (U8*)strend) : (STRLEN)(strend - s);
4519 I32 maxiters = slen + 10;
4521 const I32 origlimit = limit;
4524 const I32 gimme = GIMME_V;
4525 const I32 oldsave = PL_savestack_ix;
4526 I32 make_mortal = 1;
4531 Copy(&LvTARGOFF(POPs), &pm, 1, PMOP*);
4536 DIE(aTHX_ "panic: pp_split");
4539 TAINT_IF((pm->op_pmflags & PMf_LOCALE) &&
4540 (pm->op_pmflags & (PMf_WHITE | PMf_SKIPWHITE)));
4542 RX_MATCH_UTF8_set(rx, do_utf8);
4544 if (pm->op_pmreplroot) {
4546 ary = GvAVn((GV*)PAD_SVl(INT2PTR(PADOFFSET, pm->op_pmreplroot)));
4548 ary = GvAVn((GV*)pm->op_pmreplroot);
4551 else if (gimme != G_ARRAY)
4552 ary = GvAVn(PL_defgv);
4555 if (ary && (gimme != G_ARRAY || (pm->op_pmflags & PMf_ONCE))) {
4561 if ((mg = SvTIED_mg((SV*)ary, PERL_MAGIC_tied))) {
4563 XPUSHs(SvTIED_obj((SV*)ary, mg));
4570 for (i = AvFILLp(ary); i >= 0; i--)
4571 AvARRAY(ary)[i] = &PL_sv_undef; /* don't free mere refs */
4573 /* temporarily switch stacks */
4574 SAVESWITCHSTACK(PL_curstack, ary);
4578 base = SP - PL_stack_base;
4580 if (pm->op_pmflags & PMf_SKIPWHITE) {
4581 if (pm->op_pmflags & PMf_LOCALE) {
4582 while (isSPACE_LC(*s))
4590 if (pm->op_pmflags & PMf_MULTILINE) {
4595 limit = maxiters + 2;
4596 if (pm->op_pmflags & PMf_WHITE) {
4599 while (m < strend &&
4600 !((pm->op_pmflags & PMf_LOCALE)
4601 ? isSPACE_LC(*m) : isSPACE(*m)))
4606 dstr = newSVpvn(s, m-s);
4610 (void)SvUTF8_on(dstr);
4614 while (s < strend &&
4615 ((pm->op_pmflags & PMf_LOCALE)
4616 ? isSPACE_LC(*s) : isSPACE(*s)))
4620 else if (rx->precomp[0] == '^' && rx->precomp[1] == '\0') {
4622 for (m = s; m < strend && *m != '\n'; m++)
4627 dstr = newSVpvn(s, m-s);
4631 (void)SvUTF8_on(dstr);
4636 else if (do_utf8 == ((rx->reganch & ROPT_UTF8) != 0) &&
4637 (rx->reganch & RE_USE_INTUIT) && !rx->nparens
4638 && (rx->reganch & ROPT_CHECK_ALL)
4639 && !(rx->reganch & ROPT_ANCH)) {
4640 const int tail = (rx->reganch & RE_INTUIT_TAIL);
4641 SV * const csv = CALLREG_INTUIT_STRING(aTHX_ rx);
4644 if (len == 1 && !(rx->reganch & ROPT_UTF8) && !tail) {
4645 const char c = *SvPV_nolen_const(csv);
4647 for (m = s; m < strend && *m != c; m++)
4651 dstr = newSVpvn(s, m-s);
4655 (void)SvUTF8_on(dstr);
4657 /* The rx->minlen is in characters but we want to step
4658 * s ahead by bytes. */
4660 s = (char*)utf8_hop((U8*)m, len);
4662 s = m + len; /* Fake \n at the end */
4666 while (s < strend && --limit &&
4667 (m = fbm_instr((unsigned char*)s, (unsigned char*)strend,
4668 csv, multiline ? FBMrf_MULTILINE : 0)) )
4670 dstr = newSVpvn(s, m-s);
4674 (void)SvUTF8_on(dstr);
4676 /* The rx->minlen is in characters but we want to step
4677 * s ahead by bytes. */
4679 s = (char*)utf8_hop((U8*)m, len);
4681 s = m + len; /* Fake \n at the end */
4686 maxiters += slen * rx->nparens;
4687 while (s < strend && --limit)
4691 rex_return = CALLREGEXEC(aTHX_ rx, (char*)s, (char*)strend, (char*)orig, 1 ,
4694 if (rex_return == 0)
4696 TAINT_IF(RX_MATCH_TAINTED(rx));
4697 if (RX_MATCH_COPIED(rx) && rx->subbeg != orig) {
4702 strend = s + (strend - m);
4704 m = rx->startp[0] + orig;
4705 dstr = newSVpvn(s, m-s);
4709 (void)SvUTF8_on(dstr);
4713 for (i = 1; i <= (I32)rx->nparens; i++) {
4714 s = rx->startp[i] + orig;
4715 m = rx->endp[i] + orig;
4717 /* japhy (07/27/01) -- the (m && s) test doesn't catch
4718 parens that didn't match -- they should be set to
4719 undef, not the empty string */
4720 if (m >= orig && s >= orig) {
4721 dstr = newSVpvn(s, m-s);
4724 dstr = &PL_sv_undef; /* undef, not "" */
4728 (void)SvUTF8_on(dstr);
4732 s = rx->endp[0] + orig;
4736 iters = (SP - PL_stack_base) - base;
4737 if (iters > maxiters)
4738 DIE(aTHX_ "Split loop");
4740 /* keep field after final delim? */
4741 if (s < strend || (iters && origlimit)) {
4742 const STRLEN l = strend - s;
4743 dstr = newSVpvn(s, l);
4747 (void)SvUTF8_on(dstr);
4751 else if (!origlimit) {
4752 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0)) {
4753 if (TOPs && !make_mortal)
4756 *SP-- = &PL_sv_undef;
4761 LEAVE_SCOPE(oldsave); /* may undo an earlier SWITCHSTACK */
4765 if (SvSMAGICAL(ary)) {
4770 if (gimme == G_ARRAY) {
4772 Copy(AvARRAY(ary), SP + 1, iters, SV*);
4780 call_method("PUSH",G_SCALAR|G_DISCARD);
4783 if (gimme == G_ARRAY) {
4785 /* EXTEND should not be needed - we just popped them */
4787 for (i=0; i < iters; i++) {
4788 SV **svp = av_fetch(ary, i, FALSE);
4789 PUSHs((svp) ? *svp : &PL_sv_undef);
4796 if (gimme == G_ARRAY)
4812 if (SvTYPE(retsv) == SVt_PVAV || SvTYPE(retsv) == SVt_PVHV
4813 || SvTYPE(retsv) == SVt_PVCV) {
4814 retsv = refto(retsv);
4821 PP(unimplemented_op)
4824 DIE(aTHX_ "panic: unimplemented op %s (#%d) called", OP_NAME(PL_op),
4830 * c-indentation-style: bsd
4832 * indent-tabs-mode: t
4835 * ex: set ts=8 sts=4 sw=4 noet: