This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
embed.fnc: Add E flag to regcurly entry
[perl5.git] / pp.c
CommitLineData
a0d0e21e 1/* pp.c
79072805 2 *
1129b882
NC
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
79072805 5 *
a0d0e21e
LW
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.
79072805 8 *
a0d0e21e
LW
9 */
10
11/*
4ac71550
TC
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
15 *
16 * [p.225 of _The Lord of the Rings_, II/i: "Many Meetings"]
a0d0e21e 17 */
79072805 18
166f8a29
DM
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.
24 */
25
79072805 26#include "EXTERN.h"
864dbfa3 27#define PERL_IN_PP_C
79072805 28#include "perl.h"
77bc9082 29#include "keywords.h"
79072805 30
a4af207c
JH
31#include "reentr.h"
32
dfe9444c
AD
33/* XXX I can't imagine anyone who doesn't have this actually _needs_
34 it, since pid_t is an integral type.
35 --AD 2/20/1998
36*/
37#ifdef NEED_GETPID_PROTO
38extern Pid_t getpid (void);
8ac85365
NIS
39#endif
40
0630166f
SP
41/*
42 * Some BSDs and Cygwin default to POSIX math instead of IEEE.
43 * This switches them over to IEEE.
44 */
45#if defined(LIBM_LIB_VERSION)
46 _LIB_VERSION_TYPE _LIB_VERSION = _IEEE_;
47#endif
48
13017935
SM
49/* variations on pp_null */
50
93a17b20
LW
51PP(pp_stub)
52{
97aff369 53 dVAR;
39644a26 54 dSP;
54310121 55 if (GIMME_V == G_SCALAR)
3280af22 56 XPUSHs(&PL_sv_undef);
93a17b20
LW
57 RETURN;
58}
59
79072805
LW
60/* Pushy stuff. */
61
93a17b20
LW
62PP(pp_padav)
63{
97aff369 64 dVAR; dSP; dTARGET;
13017935 65 I32 gimme;
e190e9b4 66 assert(SvTYPE(TARG) == SVt_PVAV);
533c011a 67 if (PL_op->op_private & OPpLVAL_INTRO)
a5911867
RGS
68 if (!(PL_op->op_private & OPpPAD_STATE))
69 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
85e6fe83 70 EXTEND(SP, 1);
533c011a 71 if (PL_op->op_flags & OPf_REF) {
85e6fe83 72 PUSHs(TARG);
93a17b20 73 RETURN;
78f9721b
SM
74 } else if (LVRET) {
75 if (GIMME == G_SCALAR)
76 Perl_croak(aTHX_ "Can't return array to lvalue scalar context");
77 PUSHs(TARG);
78 RETURN;
85e6fe83 79 }
13017935
SM
80 gimme = GIMME_V;
81 if (gimme == G_ARRAY) {
502c6561 82 const I32 maxarg = AvFILL(MUTABLE_AV(TARG)) + 1;
85e6fe83 83 EXTEND(SP, maxarg);
93965878
NIS
84 if (SvMAGICAL(TARG)) {
85 U32 i;
eb160463 86 for (i=0; i < (U32)maxarg; i++) {
502c6561 87 SV * const * const svp = av_fetch(MUTABLE_AV(TARG), i, FALSE);
3280af22 88 SP[i+1] = (svp) ? *svp : &PL_sv_undef;
93965878
NIS
89 }
90 }
91 else {
502c6561 92 Copy(AvARRAY((const AV *)TARG), SP+1, maxarg, SV*);
93965878 93 }
85e6fe83
LW
94 SP += maxarg;
95 }
13017935 96 else if (gimme == G_SCALAR) {
1b6737cc 97 SV* const sv = sv_newmortal();
502c6561 98 const I32 maxarg = AvFILL(MUTABLE_AV(TARG)) + 1;
85e6fe83
LW
99 sv_setiv(sv, maxarg);
100 PUSHs(sv);
101 }
102 RETURN;
93a17b20
LW
103}
104
105PP(pp_padhv)
106{
97aff369 107 dVAR; dSP; dTARGET;
54310121 108 I32 gimme;
109
e190e9b4 110 assert(SvTYPE(TARG) == SVt_PVHV);
93a17b20 111 XPUSHs(TARG);
533c011a 112 if (PL_op->op_private & OPpLVAL_INTRO)
a5911867
RGS
113 if (!(PL_op->op_private & OPpPAD_STATE))
114 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
533c011a 115 if (PL_op->op_flags & OPf_REF)
93a17b20 116 RETURN;
78f9721b
SM
117 else if (LVRET) {
118 if (GIMME == G_SCALAR)
119 Perl_croak(aTHX_ "Can't return hash to lvalue scalar context");
120 RETURN;
121 }
54310121 122 gimme = GIMME_V;
123 if (gimme == G_ARRAY) {
981b7185 124 RETURNOP(Perl_do_kv(aTHX));
85e6fe83 125 }
54310121 126 else if (gimme == G_SCALAR) {
85fbaab2 127 SV* const sv = Perl_hv_scalar(aTHX_ MUTABLE_HV(TARG));
85e6fe83 128 SETs(sv);
85e6fe83 129 }
54310121 130 RETURN;
93a17b20
LW
131}
132
79072805
LW
133/* Translations. */
134
4bdf8368 135static const char S_no_symref_sv[] =
def89bff
NC
136 "Can't use string (\"%" SVf32 "\"%s) as %s ref while \"strict refs\" in use";
137
79072805
LW
138PP(pp_rv2gv)
139{
97aff369 140 dVAR; dSP; dTOPss;
8ec5e241 141
f64c9ac5 142 if (!isGV(sv) || SvFAKE(sv)) SvGETMAGIC(sv);
ed6116ce 143 if (SvROK(sv)) {
a0d0e21e 144 wasref:
93d7320b
DM
145 if (SvAMAGIC(sv)) {
146 sv = amagic_deref_call(sv, to_gv_amg);
147 SPAGAIN;
148 }
ed6116ce 149 sv = SvRV(sv);
b1dadf13 150 if (SvTYPE(sv) == SVt_PVIO) {
159b6efe 151 GV * const gv = MUTABLE_GV(sv_newmortal());
b1dadf13 152 gv_init(gv, 0, "", 0, 0);
a45c7426 153 GvIOp(gv) = MUTABLE_IO(sv);
b37c2d43 154 SvREFCNT_inc_void_NN(sv);
ad64d0ec 155 sv = MUTABLE_SV(gv);
ef54e1a4 156 }
6e592b3a 157 else if (!isGV_with_GP(sv))
cea2e8a9 158 DIE(aTHX_ "Not a GLOB reference");
79072805
LW
159 }
160 else {
6e592b3a 161 if (!isGV_with_GP(sv)) {
afd1915d 162 if (!SvOK(sv) && sv != &PL_sv_undef) {
b13b2135 163 /* If this is a 'my' scalar and flag is set then vivify
853846ea 164 * NI-S 1999/05/07
b13b2135 165 */
ac53db4c 166 if (SvREADONLY(sv))
6ad8f254 167 Perl_croak_no_modify(aTHX);
1d8d4d2a 168 if (PL_op->op_private & OPpDEREF) {
2c8ac474
GS
169 GV *gv;
170 if (cUNOP->op_targ) {
171 STRLEN len;
0bd48802
AL
172 SV * const namesv = PAD_SV(cUNOP->op_targ);
173 const char * const name = SvPV(namesv, len);
159b6efe 174 gv = MUTABLE_GV(newSV(0));
2c8ac474
GS
175 gv_init(gv, CopSTASH(PL_curcop), name, len, 0);
176 }
177 else {
0bd48802 178 const char * const name = CopSTASHPV(PL_curcop);
2c8ac474 179 gv = newGVgen(name);
1d8d4d2a 180 }
43230e26 181 prepare_SV_for_RV(sv);
ad64d0ec 182 SvRV_set(sv, MUTABLE_SV(gv));
853846ea 183 SvROK_on(sv);
1d8d4d2a 184 SvSETMAGIC(sv);
853846ea 185 goto wasref;
2c8ac474 186 }
533c011a
NIS
187 if (PL_op->op_flags & OPf_REF ||
188 PL_op->op_private & HINT_STRICT_REFS)
cea2e8a9 189 DIE(aTHX_ PL_no_usym, "a symbol");
599cee73 190 if (ckWARN(WARN_UNINITIALIZED))
29489e7c 191 report_uninit(sv);
a0d0e21e
LW
192 RETSETUNDEF;
193 }
35cd451c
GS
194 if ((PL_op->op_flags & OPf_SPECIAL) &&
195 !(PL_op->op_flags & OPf_MOD))
196 {
ad64d0ec 197 SV * const temp = MUTABLE_SV(gv_fetchsv(sv, 0, SVt_PVGV));
7a5fd60d
NC
198 if (!temp
199 && (!is_gv_magical_sv(sv,0)
ad64d0ec
NC
200 || !(sv = MUTABLE_SV(gv_fetchsv(sv, GV_ADD,
201 SVt_PVGV))))) {
35cd451c 202 RETSETUNDEF;
c9d5ac95 203 }
7a5fd60d 204 sv = temp;
35cd451c
GS
205 }
206 else {
207 if (PL_op->op_private & HINT_STRICT_REFS)
10b53e54 208 DIE(aTHX_ S_no_symref_sv, sv, (SvPOK(sv) && SvCUR(sv)>32 ? "..." : ""), "a symbol");
e26df76a
NC
209 if ((PL_op->op_private & (OPpLVAL_INTRO|OPpDONT_INIT_GV))
210 == OPpDONT_INIT_GV) {
211 /* We are the target of a coderef assignment. Return
212 the scalar unchanged, and let pp_sasssign deal with
213 things. */
214 RETURN;
215 }
ad64d0ec 216 sv = MUTABLE_SV(gv_fetchsv(sv, GV_ADD, SVt_PVGV));
35cd451c 217 }
2acc3314
FC
218 /* FAKE globs in the symbol table cause weird bugs (#77810) */
219 if (sv) SvFAKE_off(sv);
93a17b20 220 }
79072805 221 }
2acc3314
FC
222 if (sv && SvFAKE(sv)) {
223 SV *newsv = sv_newmortal();
5cf4b255 224 sv_setsv_flags(newsv, sv, 0);
2acc3314 225 SvFAKE_off(newsv);
d8906c05 226 sv = newsv;
2acc3314 227 }
d8906c05
FC
228 if (PL_op->op_private & OPpLVAL_INTRO)
229 save_gp(MUTABLE_GV(sv), !(PL_op->op_flags & OPf_SPECIAL));
230 SETs(sv);
79072805
LW
231 RETURN;
232}
233
dc3c76f8
NC
234/* Helper function for pp_rv2sv and pp_rv2av */
235GV *
fe9845cc
RB
236Perl_softref2xv(pTHX_ SV *const sv, const char *const what,
237 const svtype type, SV ***spp)
dc3c76f8
NC
238{
239 dVAR;
240 GV *gv;
241
7918f24d
NC
242 PERL_ARGS_ASSERT_SOFTREF2XV;
243
dc3c76f8
NC
244 if (PL_op->op_private & HINT_STRICT_REFS) {
245 if (SvOK(sv))
10b53e54 246 Perl_die(aTHX_ S_no_symref_sv, sv, (SvPOK(sv) && SvCUR(sv)>32 ? "..." : ""), what);
dc3c76f8
NC
247 else
248 Perl_die(aTHX_ PL_no_usym, what);
249 }
250 if (!SvOK(sv)) {
fd1d9b5c
FC
251 if (
252 PL_op->op_flags & OPf_REF &&
253 PL_op->op_next->op_type != OP_BOOLKEYS
254 )
dc3c76f8
NC
255 Perl_die(aTHX_ PL_no_usym, what);
256 if (ckWARN(WARN_UNINITIALIZED))
257 report_uninit(sv);
258 if (type != SVt_PV && GIMME_V == G_ARRAY) {
259 (*spp)--;
260 return NULL;
261 }
262 **spp = &PL_sv_undef;
263 return NULL;
264 }
265 if ((PL_op->op_flags & OPf_SPECIAL) &&
266 !(PL_op->op_flags & OPf_MOD))
267 {
81e3fc25 268 gv = gv_fetchsv(sv, 0, type);
dc3c76f8
NC
269 if (!gv
270 && (!is_gv_magical_sv(sv,0)
81e3fc25 271 || !(gv = gv_fetchsv(sv, GV_ADD, type))))
dc3c76f8
NC
272 {
273 **spp = &PL_sv_undef;
274 return NULL;
275 }
276 }
277 else {
81e3fc25 278 gv = gv_fetchsv(sv, GV_ADD, type);
dc3c76f8
NC
279 }
280 return gv;
281}
282
79072805
LW
283PP(pp_rv2sv)
284{
97aff369 285 dVAR; dSP; dTOPss;
c445ea15 286 GV *gv = NULL;
79072805 287
0824d667
DM
288 if (!(PL_op->op_private & OPpDEREFed))
289 SvGETMAGIC(sv);
ed6116ce 290 if (SvROK(sv)) {
93d7320b
DM
291 if (SvAMAGIC(sv)) {
292 sv = amagic_deref_call(sv, to_sv_amg);
293 SPAGAIN;
294 }
f5284f61 295
ed6116ce 296 sv = SvRV(sv);
79072805
LW
297 switch (SvTYPE(sv)) {
298 case SVt_PVAV:
299 case SVt_PVHV:
300 case SVt_PVCV:
cbae9b9f
YST
301 case SVt_PVFM:
302 case SVt_PVIO:
cea2e8a9 303 DIE(aTHX_ "Not a SCALAR reference");
42d0e0b7 304 default: NOOP;
79072805
LW
305 }
306 }
307 else {
159b6efe 308 gv = MUTABLE_GV(sv);
748a9306 309
6e592b3a 310 if (!isGV_with_GP(gv)) {
dc3c76f8
NC
311 gv = Perl_softref2xv(aTHX_ sv, "a SCALAR", SVt_PV, &sp);
312 if (!gv)
313 RETURN;
463ee0b2 314 }
29c711a3 315 sv = GvSVn(gv);
a0d0e21e 316 }
533c011a 317 if (PL_op->op_flags & OPf_MOD) {
82d03984
RGS
318 if (PL_op->op_private & OPpLVAL_INTRO) {
319 if (cUNOP->op_first->op_type == OP_NULL)
159b6efe 320 sv = save_scalar(MUTABLE_GV(TOPs));
82d03984
RGS
321 else if (gv)
322 sv = save_scalar(gv);
323 else
f1f66076 324 Perl_croak(aTHX_ "%s", PL_no_localize_ref);
82d03984 325 }
533c011a
NIS
326 else if (PL_op->op_private & OPpDEREF)
327 vivify_ref(sv, PL_op->op_private & OPpDEREF);
79072805 328 }
a0d0e21e 329 SETs(sv);
79072805
LW
330 RETURN;
331}
332
333PP(pp_av2arylen)
334{
97aff369 335 dVAR; dSP;
502c6561 336 AV * const av = MUTABLE_AV(TOPs);
02d85cc3
EB
337 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
338 if (lvalue) {
339 SV ** const sv = Perl_av_arylen_p(aTHX_ MUTABLE_AV(av));
340 if (!*sv) {
341 *sv = newSV_type(SVt_PVMG);
342 sv_magic(*sv, MUTABLE_SV(av), PERL_MAGIC_arylen, NULL, 0);
343 }
344 SETs(*sv);
345 } else {
346 SETs(sv_2mortal(newSViv(
347 AvFILL(MUTABLE_AV(av)) + CopARYBASE_get(PL_curcop)
348 )));
79072805 349 }
79072805
LW
350 RETURN;
351}
352
a0d0e21e
LW
353PP(pp_pos)
354{
2154eca7 355 dVAR; dSP; dPOPss;
8ec5e241 356
78f9721b 357 if (PL_op->op_flags & OPf_MOD || LVRET) {
2154eca7
EB
358 SV * const ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
359 sv_magic(ret, NULL, PERL_MAGIC_pos, NULL, 0);
360 LvTYPE(ret) = '.';
361 LvTARG(ret) = SvREFCNT_inc_simple(sv);
362 PUSHs(ret); /* no SvSETMAGIC */
a0d0e21e
LW
363 RETURN;
364 }
365 else {
a0d0e21e 366 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
1b6737cc 367 const MAGIC * const mg = mg_find(sv, PERL_MAGIC_regex_global);
565764a8 368 if (mg && mg->mg_len >= 0) {
2154eca7 369 dTARGET;
a0ed51b3 370 I32 i = mg->mg_len;
7e2040f0 371 if (DO_UTF8(sv))
a0ed51b3 372 sv_pos_b2u(sv, &i);
fc15ae8f 373 PUSHi(i + CopARYBASE_get(PL_curcop));
a0d0e21e
LW
374 RETURN;
375 }
376 }
377 RETPUSHUNDEF;
378 }
379}
380
79072805
LW
381PP(pp_rv2cv)
382{
97aff369 383 dVAR; dSP;
79072805 384 GV *gv;
1eced8f8 385 HV *stash_unused;
c445ea15
AL
386 const I32 flags = (PL_op->op_flags & OPf_SPECIAL)
387 ? 0
388 : ((PL_op->op_private & (OPpLVAL_INTRO|OPpMAY_RETURN_CONSTANT)) == OPpMAY_RETURN_CONSTANT)
389 ? GV_ADD|GV_NOEXPAND
390 : GV_ADD;
4633a7c4
LW
391 /* We usually try to add a non-existent subroutine in case of AUTOLOAD. */
392 /* (But not in defined().) */
e26df76a 393
1eced8f8 394 CV *cv = sv_2cv(TOPs, &stash_unused, &gv, flags);
07055b4c
CS
395 if (cv) {
396 if (CvCLONE(cv))
ad64d0ec 397 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
d32f2495
SC
398 if ((PL_op->op_private & OPpLVAL_INTRO)) {
399 if (gv && GvCV(gv) == cv && (gv = gv_autoload4(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv), FALSE)))
400 cv = GvCV(gv);
401 if (!CvLVALUE(cv))
402 DIE(aTHX_ "Can't modify non-lvalue subroutine call");
403 }
07055b4c 404 }
e26df76a 405 else if ((flags == (GV_ADD|GV_NOEXPAND)) && gv && SvROK(gv)) {
ea726b52 406 cv = MUTABLE_CV(gv);
e26df76a 407 }
07055b4c 408 else
ea726b52 409 cv = MUTABLE_CV(&PL_sv_undef);
ad64d0ec 410 SETs(MUTABLE_SV(cv));
79072805
LW
411 RETURN;
412}
413
c07a80fd 414PP(pp_prototype)
415{
97aff369 416 dVAR; dSP;
c07a80fd 417 CV *cv;
418 HV *stash;
419 GV *gv;
fabdb6c0 420 SV *ret = &PL_sv_undef;
c07a80fd 421
b6c543e3 422 if (SvPOK(TOPs) && SvCUR(TOPs) >= 7) {
e3f73d4e 423 const char * s = SvPVX_const(TOPs);
b6c543e3 424 if (strnEQ(s, "CORE::", 6)) {
5458a98a 425 const int code = keyword(s + 6, SvCUR(TOPs) - 6, 1);
b6c543e3
IZ
426 if (code < 0) { /* Overridable. */
427#define MAX_ARGS_OP ((sizeof(I32) - 1) * 2)
59b085e1 428 int i = 0, n = 0, seen_question = 0, defgv = 0;
b6c543e3
IZ
429 I32 oa;
430 char str[ MAX_ARGS_OP * 2 + 2 ]; /* One ';', one '\0' */
431
bdf1bb36 432 if (code == -KEY_chop || code == -KEY_chomp
f23102e2 433 || code == -KEY_exec || code == -KEY_system)
77bc9082 434 goto set;
d116c547 435 if (code == -KEY_mkdir) {
84bafc02 436 ret = newSVpvs_flags("_;$", SVs_TEMP);
d116c547
RGS
437 goto set;
438 }
7c8178a1 439 if (code == -KEY_keys || code == -KEY_values || code == -KEY_each) {
cba5a3b0
DG
440 ret = newSVpvs_flags("+", SVs_TEMP);
441 goto set;
442 }
443 if (code == -KEY_push || code == -KEY_unshift) {
444 ret = newSVpvs_flags("+@", SVs_TEMP);
445 goto set;
446 }
447 if (code == -KEY_pop || code == -KEY_shift) {
448 ret = newSVpvs_flags(";+", SVs_TEMP);
449 goto set;
450 }
451 if (code == -KEY_splice) {
452 ret = newSVpvs_flags("+;$$@", SVs_TEMP);
1db4d195
FC
453 goto set;
454 }
455 if (code == -KEY_tied || code == -KEY_untie) {
456 ret = newSVpvs_flags("\\[$@%*]", SVs_TEMP);
457 goto set;
458 }
459 if (code == -KEY_tie) {
460 ret = newSVpvs_flags("\\[$@%*]$@", SVs_TEMP);
7c8178a1
RGS
461 goto set;
462 }
e3f73d4e
RGS
463 if (code == -KEY_readpipe) {
464 s = "CORE::backtick";
465 }
b6c543e3 466 while (i < MAXO) { /* The slow way. */
22c35a8c
GS
467 if (strEQ(s + 6, PL_op_name[i])
468 || strEQ(s + 6, PL_op_desc[i]))
469 {
b6c543e3 470 goto found;
22c35a8c 471 }
b6c543e3
IZ
472 i++;
473 }
474 goto nonesuch; /* Should not happen... */
475 found:
59b085e1 476 defgv = PL_opargs[i] & OA_DEFGV;
22c35a8c 477 oa = PL_opargs[i] >> OASHIFT;
b6c543e3 478 while (oa) {
59b085e1 479 if (oa & OA_OPTIONAL && !seen_question && !defgv) {
b6c543e3
IZ
480 seen_question = 1;
481 str[n++] = ';';
ef54e1a4 482 }
b13b2135 483 if ((oa & (OA_OPTIONAL - 1)) >= OA_AVREF
6e97e420
SC
484 && (oa & (OA_OPTIONAL - 1)) <= OA_SCALARREF
485 /* But globs are already references (kinda) */
486 && (oa & (OA_OPTIONAL - 1)) != OA_FILEREF
487 ) {
b6c543e3
IZ
488 str[n++] = '\\';
489 }
b6c543e3
IZ
490 str[n++] = ("?$@@%&*$")[oa & (OA_OPTIONAL - 1)];
491 oa = oa >> 4;
492 }
59b085e1
RGS
493 if (defgv && str[n - 1] == '$')
494 str[n - 1] = '_';
b6c543e3 495 str[n++] = '\0';
59cd0e26 496 ret = newSVpvn_flags(str, n - 1, SVs_TEMP);
ef54e1a4
JH
497 }
498 else if (code) /* Non-Overridable */
b6c543e3
IZ
499 goto set;
500 else { /* None such */
501 nonesuch:
d470f89e 502 DIE(aTHX_ "Can't find an opnumber for \"%s\"", s+6);
b6c543e3
IZ
503 }
504 }
505 }
f2c0649b 506 cv = sv_2cv(TOPs, &stash, &gv, 0);
5f05dabc 507 if (cv && SvPOK(cv))
59cd0e26 508 ret = newSVpvn_flags(SvPVX_const(cv), SvCUR(cv), SVs_TEMP);
b6c543e3 509 set:
c07a80fd 510 SETs(ret);
511 RETURN;
512}
513
a0d0e21e
LW
514PP(pp_anoncode)
515{
97aff369 516 dVAR; dSP;
ea726b52 517 CV *cv = MUTABLE_CV(PAD_SV(PL_op->op_targ));
a5f75d66 518 if (CvCLONE(cv))
ad64d0ec 519 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
5f05dabc 520 EXTEND(SP,1);
ad64d0ec 521 PUSHs(MUTABLE_SV(cv));
a0d0e21e
LW
522 RETURN;
523}
524
525PP(pp_srefgen)
79072805 526{
97aff369 527 dVAR; dSP;
71be2cbc 528 *SP = refto(*SP);
79072805 529 RETURN;
8ec5e241 530}
a0d0e21e
LW
531
532PP(pp_refgen)
533{
97aff369 534 dVAR; dSP; dMARK;
a0d0e21e 535 if (GIMME != G_ARRAY) {
5f0b1d4e
GS
536 if (++MARK <= SP)
537 *MARK = *SP;
538 else
3280af22 539 *MARK = &PL_sv_undef;
5f0b1d4e
GS
540 *MARK = refto(*MARK);
541 SP = MARK;
542 RETURN;
a0d0e21e 543 }
bbce6d69 544 EXTEND_MORTAL(SP - MARK);
71be2cbc 545 while (++MARK <= SP)
546 *MARK = refto(*MARK);
a0d0e21e 547 RETURN;
79072805
LW
548}
549
76e3520e 550STATIC SV*
cea2e8a9 551S_refto(pTHX_ SV *sv)
71be2cbc 552{
97aff369 553 dVAR;
71be2cbc 554 SV* rv;
555
7918f24d
NC
556 PERL_ARGS_ASSERT_REFTO;
557
71be2cbc 558 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
559 if (LvTARGLEN(sv))
68dc0745 560 vivify_defelem(sv);
561 if (!(sv = LvTARG(sv)))
3280af22 562 sv = &PL_sv_undef;
0dd88869 563 else
b37c2d43 564 SvREFCNT_inc_void_NN(sv);
71be2cbc 565 }
d8b46c1b 566 else if (SvTYPE(sv) == SVt_PVAV) {
502c6561
NC
567 if (!AvREAL((const AV *)sv) && AvREIFY((const AV *)sv))
568 av_reify(MUTABLE_AV(sv));
d8b46c1b 569 SvTEMP_off(sv);
b37c2d43 570 SvREFCNT_inc_void_NN(sv);
d8b46c1b 571 }
f2933f5f
DM
572 else if (SvPADTMP(sv) && !IS_PADGV(sv))
573 sv = newSVsv(sv);
71be2cbc 574 else {
575 SvTEMP_off(sv);
b37c2d43 576 SvREFCNT_inc_void_NN(sv);
71be2cbc 577 }
578 rv = sv_newmortal();
4df7f6af 579 sv_upgrade(rv, SVt_IV);
b162af07 580 SvRV_set(rv, sv);
71be2cbc 581 SvROK_on(rv);
582 return rv;
583}
584
79072805
LW
585PP(pp_ref)
586{
97aff369 587 dVAR; dSP; dTARGET;
e1ec3a88 588 const char *pv;
1b6737cc 589 SV * const sv = POPs;
f12c7020 590
5b295bef
RD
591 if (sv)
592 SvGETMAGIC(sv);
f12c7020 593
a0d0e21e 594 if (!sv || !SvROK(sv))
4633a7c4 595 RETPUSHNO;
79072805 596
cba0b539
FR
597 pv = sv_reftype(SvRV(sv),TRUE);
598 PUSHp(pv, strlen(pv));
79072805
LW
599 RETURN;
600}
601
602PP(pp_bless)
603{
97aff369 604 dVAR; dSP;
463ee0b2 605 HV *stash;
79072805 606
463ee0b2 607 if (MAXARG == 1)
11faa288 608 stash = CopSTASH(PL_curcop);
7b8d334a 609 else {
1b6737cc 610 SV * const ssv = POPs;
7b8d334a 611 STRLEN len;
e1ec3a88 612 const char *ptr;
81689caa 613
016a42f3 614 if (ssv && !SvGMAGICAL(ssv) && !SvAMAGIC(ssv) && SvROK(ssv))
81689caa 615 Perl_croak(aTHX_ "Attempt to bless into a reference");
5c144d81 616 ptr = SvPV_const(ssv,len);
a2a5de95
NC
617 if (len == 0)
618 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
619 "Explicit blessing to '' (assuming package main)");
da51bb9b 620 stash = gv_stashpvn(ptr, len, GV_ADD);
7b8d334a 621 }
a0d0e21e 622
5d3fdfeb 623 (void)sv_bless(TOPs, stash);
79072805
LW
624 RETURN;
625}
626
fb73857a 627PP(pp_gelem)
628{
97aff369 629 dVAR; dSP;
b13b2135 630
1b6737cc
AL
631 SV *sv = POPs;
632 const char * const elem = SvPV_nolen_const(sv);
159b6efe 633 GV * const gv = MUTABLE_GV(POPs);
c445ea15 634 SV * tmpRef = NULL;
1b6737cc 635
c445ea15 636 sv = NULL;
c4ba80c3
NC
637 if (elem) {
638 /* elem will always be NUL terminated. */
1b6737cc 639 const char * const second_letter = elem + 1;
c4ba80c3
NC
640 switch (*elem) {
641 case 'A':
1b6737cc 642 if (strEQ(second_letter, "RRAY"))
ad64d0ec 643 tmpRef = MUTABLE_SV(GvAV(gv));
c4ba80c3
NC
644 break;
645 case 'C':
1b6737cc 646 if (strEQ(second_letter, "ODE"))
ad64d0ec 647 tmpRef = MUTABLE_SV(GvCVu(gv));
c4ba80c3
NC
648 break;
649 case 'F':
1b6737cc 650 if (strEQ(second_letter, "ILEHANDLE")) {
c4ba80c3
NC
651 /* finally deprecated in 5.8.0 */
652 deprecate("*glob{FILEHANDLE}");
ad64d0ec 653 tmpRef = MUTABLE_SV(GvIOp(gv));
c4ba80c3
NC
654 }
655 else
1b6737cc 656 if (strEQ(second_letter, "ORMAT"))
ad64d0ec 657 tmpRef = MUTABLE_SV(GvFORM(gv));
c4ba80c3
NC
658 break;
659 case 'G':
1b6737cc 660 if (strEQ(second_letter, "LOB"))
ad64d0ec 661 tmpRef = MUTABLE_SV(gv);
c4ba80c3
NC
662 break;
663 case 'H':
1b6737cc 664 if (strEQ(second_letter, "ASH"))
ad64d0ec 665 tmpRef = MUTABLE_SV(GvHV(gv));
c4ba80c3
NC
666 break;
667 case 'I':
1b6737cc 668 if (*second_letter == 'O' && !elem[2])
ad64d0ec 669 tmpRef = MUTABLE_SV(GvIOp(gv));
c4ba80c3
NC
670 break;
671 case 'N':
1b6737cc 672 if (strEQ(second_letter, "AME"))
a663657d 673 sv = newSVhek(GvNAME_HEK(gv));
c4ba80c3
NC
674 break;
675 case 'P':
1b6737cc 676 if (strEQ(second_letter, "ACKAGE")) {
7fa3a4ab
NC
677 const HV * const stash = GvSTASH(gv);
678 const HEK * const hek = stash ? HvNAME_HEK(stash) : NULL;
396482e1 679 sv = hek ? newSVhek(hek) : newSVpvs("__ANON__");
c4ba80c3
NC
680 }
681 break;
682 case 'S':
1b6737cc 683 if (strEQ(second_letter, "CALAR"))
f9d52e31 684 tmpRef = GvSVn(gv);
c4ba80c3 685 break;
39b99f21 686 }
fb73857a 687 }
76e3520e
GS
688 if (tmpRef)
689 sv = newRV(tmpRef);
fb73857a 690 if (sv)
691 sv_2mortal(sv);
692 else
3280af22 693 sv = &PL_sv_undef;
fb73857a 694 XPUSHs(sv);
695 RETURN;
696}
697
a0d0e21e 698/* Pattern matching */
79072805 699
a0d0e21e 700PP(pp_study)
79072805 701{
97aff369 702 dVAR; dSP; dPOPss;
a0d0e21e
LW
703 register unsigned char *s;
704 register I32 pos;
705 register I32 ch;
706 register I32 *sfirst;
707 register I32 *snext;
a0d0e21e
LW
708 STRLEN len;
709
3280af22 710 if (sv == PL_lastscream) {
1e422769 711 if (SvSCREAM(sv))
712 RETPUSHYES;
713 }
a4f4e906
NC
714 s = (unsigned char*)(SvPV(sv, len));
715 pos = len;
c9b9f909 716 if (pos <= 0 || !SvPOK(sv) || SvUTF8(sv)) {
a4f4e906
NC
717 /* No point in studying a zero length string, and not safe to study
718 anything that doesn't appear to be a simple scalar (and hence might
719 change between now and when the regexp engine runs without our set
bd473224 720 magic ever running) such as a reference to an object with overloaded
a4f4e906
NC
721 stringification. */
722 RETPUSHNO;
723 }
724
725 if (PL_lastscream) {
726 SvSCREAM_off(PL_lastscream);
727 SvREFCNT_dec(PL_lastscream);
c07a80fd 728 }
b37c2d43 729 PL_lastscream = SvREFCNT_inc_simple(sv);
1e422769 730
731 s = (unsigned char*)(SvPV(sv, len));
732 pos = len;
733 if (pos <= 0)
734 RETPUSHNO;
3280af22
NIS
735 if (pos > PL_maxscream) {
736 if (PL_maxscream < 0) {
737 PL_maxscream = pos + 80;
a02a5408
JC
738 Newx(PL_screamfirst, 256, I32);
739 Newx(PL_screamnext, PL_maxscream, I32);
79072805
LW
740 }
741 else {
3280af22
NIS
742 PL_maxscream = pos + pos / 4;
743 Renew(PL_screamnext, PL_maxscream, I32);
79072805 744 }
79072805 745 }
a0d0e21e 746
3280af22
NIS
747 sfirst = PL_screamfirst;
748 snext = PL_screamnext;
a0d0e21e
LW
749
750 if (!sfirst || !snext)
cea2e8a9 751 DIE(aTHX_ "do_study: out of memory");
a0d0e21e
LW
752
753 for (ch = 256; ch; --ch)
754 *sfirst++ = -1;
755 sfirst -= 256;
756
757 while (--pos >= 0) {
1b6737cc 758 register const I32 ch = s[pos];
a0d0e21e
LW
759 if (sfirst[ch] >= 0)
760 snext[pos] = sfirst[ch] - pos;
761 else
762 snext[pos] = -pos;
763 sfirst[ch] = pos;
79072805
LW
764 }
765
c07a80fd 766 SvSCREAM_on(sv);
14befaf4 767 /* piggyback on m//g magic */
c445ea15 768 sv_magic(sv, NULL, PERL_MAGIC_regex_global, NULL, 0);
1e422769 769 RETPUSHYES;
79072805
LW
770}
771
a0d0e21e 772PP(pp_trans)
79072805 773{
97aff369 774 dVAR; dSP; dTARG;
a0d0e21e
LW
775 SV *sv;
776
533c011a 777 if (PL_op->op_flags & OPf_STACKED)
a0d0e21e 778 sv = POPs;
59f00321
RGS
779 else if (PL_op->op_private & OPpTARGET_MY)
780 sv = GETTARGET;
79072805 781 else {
54b9620d 782 sv = DEFSV;
a0d0e21e 783 EXTEND(SP,1);
79072805 784 }
adbc6bb1 785 TARG = sv_newmortal();
bb16bae8
FC
786 if(PL_op->op_type == OP_TRANSR) {
787 SV * const newsv = newSVsv(sv);
788 do_trans(newsv);
789 mPUSHs(newsv);
790 }
791 else PUSHi(do_trans(sv));
a0d0e21e 792 RETURN;
79072805
LW
793}
794
a0d0e21e 795/* Lvalue operators. */
79072805 796
81745e4e
NC
797static void
798S_do_chomp(pTHX_ SV *retval, SV *sv, bool chomping)
799{
800 dVAR;
801 STRLEN len;
802 char *s;
803
804 PERL_ARGS_ASSERT_DO_CHOMP;
805
806 if (chomping && (RsSNARF(PL_rs) || RsRECORD(PL_rs)))
807 return;
808 if (SvTYPE(sv) == SVt_PVAV) {
809 I32 i;
810 AV *const av = MUTABLE_AV(sv);
811 const I32 max = AvFILL(av);
812
813 for (i = 0; i <= max; i++) {
814 sv = MUTABLE_SV(av_fetch(av, i, FALSE));
815 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
816 do_chomp(retval, sv, chomping);
817 }
818 return;
819 }
820 else if (SvTYPE(sv) == SVt_PVHV) {
821 HV* const hv = MUTABLE_HV(sv);
822 HE* entry;
823 (void)hv_iterinit(hv);
824 while ((entry = hv_iternext(hv)))
825 do_chomp(retval, hv_iterval(hv,entry), chomping);
826 return;
827 }
828 else if (SvREADONLY(sv)) {
829 if (SvFAKE(sv)) {
830 /* SV is copy-on-write */
831 sv_force_normal_flags(sv, 0);
832 }
833 if (SvREADONLY(sv))
834 Perl_croak_no_modify(aTHX);
835 }
836
837 if (PL_encoding) {
838 if (!SvUTF8(sv)) {
839 /* XXX, here sv is utf8-ized as a side-effect!
840 If encoding.pm is used properly, almost string-generating
841 operations, including literal strings, chr(), input data, etc.
842 should have been utf8-ized already, right?
843 */
844 sv_recode_to_utf8(sv, PL_encoding);
845 }
846 }
847
848 s = SvPV(sv, len);
849 if (chomping) {
850 char *temp_buffer = NULL;
851 SV *svrecode = NULL;
852
853 if (s && len) {
854 s += --len;
855 if (RsPARA(PL_rs)) {
856 if (*s != '\n')
857 goto nope;
858 ++SvIVX(retval);
859 while (len && s[-1] == '\n') {
860 --len;
861 --s;
862 ++SvIVX(retval);
863 }
864 }
865 else {
866 STRLEN rslen, rs_charlen;
867 const char *rsptr = SvPV_const(PL_rs, rslen);
868
869 rs_charlen = SvUTF8(PL_rs)
870 ? sv_len_utf8(PL_rs)
871 : rslen;
872
873 if (SvUTF8(PL_rs) != SvUTF8(sv)) {
874 /* Assumption is that rs is shorter than the scalar. */
875 if (SvUTF8(PL_rs)) {
876 /* RS is utf8, scalar is 8 bit. */
877 bool is_utf8 = TRUE;
878 temp_buffer = (char*)bytes_from_utf8((U8*)rsptr,
879 &rslen, &is_utf8);
880 if (is_utf8) {
881 /* Cannot downgrade, therefore cannot possibly match
882 */
883 assert (temp_buffer == rsptr);
884 temp_buffer = NULL;
885 goto nope;
886 }
887 rsptr = temp_buffer;
888 }
889 else if (PL_encoding) {
890 /* RS is 8 bit, encoding.pm is used.
891 * Do not recode PL_rs as a side-effect. */
892 svrecode = newSVpvn(rsptr, rslen);
893 sv_recode_to_utf8(svrecode, PL_encoding);
894 rsptr = SvPV_const(svrecode, rslen);
895 rs_charlen = sv_len_utf8(svrecode);
896 }
897 else {
898 /* RS is 8 bit, scalar is utf8. */
899 temp_buffer = (char*)bytes_to_utf8((U8*)rsptr, &rslen);
900 rsptr = temp_buffer;
901 }
902 }
903 if (rslen == 1) {
904 if (*s != *rsptr)
905 goto nope;
906 ++SvIVX(retval);
907 }
908 else {
909 if (len < rslen - 1)
910 goto nope;
911 len -= rslen - 1;
912 s -= rslen - 1;
913 if (memNE(s, rsptr, rslen))
914 goto nope;
915 SvIVX(retval) += rs_charlen;
916 }
917 }
918 s = SvPV_force_nolen(sv);
919 SvCUR_set(sv, len);
920 *SvEND(sv) = '\0';
921 SvNIOK_off(sv);
922 SvSETMAGIC(sv);
923 }
924 nope:
925
926 SvREFCNT_dec(svrecode);
927
928 Safefree(temp_buffer);
929 } else {
930 if (len && !SvPOK(sv))
931 s = SvPV_force_nomg(sv, len);
932 if (DO_UTF8(sv)) {
933 if (s && len) {
934 char * const send = s + len;
935 char * const start = s;
936 s = send - 1;
937 while (s > start && UTF8_IS_CONTINUATION(*s))
938 s--;
939 if (is_utf8_string((U8*)s, send - s)) {
940 sv_setpvn(retval, s, send - s);
941 *s = '\0';
942 SvCUR_set(sv, s - start);
943 SvNIOK_off(sv);
944 SvUTF8_on(retval);
945 }
946 }
947 else
948 sv_setpvs(retval, "");
949 }
950 else if (s && len) {
951 s += --len;
952 sv_setpvn(retval, s, 1);
953 *s = '\0';
954 SvCUR_set(sv, len);
955 SvUTF8_off(sv);
956 SvNIOK_off(sv);
957 }
958 else
959 sv_setpvs(retval, "");
960 SvSETMAGIC(sv);
961 }
962}
963
a0d0e21e
LW
964PP(pp_schop)
965{
97aff369 966 dVAR; dSP; dTARGET;
fa54efae
NC
967 const bool chomping = PL_op->op_type == OP_SCHOMP;
968
969 if (chomping)
970 sv_setiv(TARG, 0);
971 do_chomp(TARG, TOPs, chomping);
a0d0e21e
LW
972 SETTARG;
973 RETURN;
79072805
LW
974}
975
a0d0e21e 976PP(pp_chop)
79072805 977{
97aff369 978 dVAR; dSP; dMARK; dTARGET; dORIGMARK;
fa54efae 979 const bool chomping = PL_op->op_type == OP_CHOMP;
8ec5e241 980
fa54efae
NC
981 if (chomping)
982 sv_setiv(TARG, 0);
20cf1f79 983 while (MARK < SP)
fa54efae 984 do_chomp(TARG, *++MARK, chomping);
20cf1f79
NC
985 SP = ORIGMARK;
986 XPUSHTARG;
a0d0e21e 987 RETURN;
79072805
LW
988}
989
a0d0e21e
LW
990PP(pp_undef)
991{
97aff369 992 dVAR; dSP;
a0d0e21e
LW
993 SV *sv;
994
533c011a 995 if (!PL_op->op_private) {
774d564b 996 EXTEND(SP, 1);
a0d0e21e 997 RETPUSHUNDEF;
774d564b 998 }
79072805 999
a0d0e21e
LW
1000 sv = POPs;
1001 if (!sv)
1002 RETPUSHUNDEF;
85e6fe83 1003
765f542d 1004 SV_CHECK_THINKFIRST_COW_DROP(sv);
85e6fe83 1005
a0d0e21e
LW
1006 switch (SvTYPE(sv)) {
1007 case SVt_NULL:
1008 break;
1009 case SVt_PVAV:
502c6561 1010 av_undef(MUTABLE_AV(sv));
a0d0e21e
LW
1011 break;
1012 case SVt_PVHV:
85fbaab2 1013 hv_undef(MUTABLE_HV(sv));
a0d0e21e
LW
1014 break;
1015 case SVt_PVCV:
a2a5de95
NC
1016 if (cv_const_sv((const CV *)sv))
1017 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Constant subroutine %s undefined",
1018 CvANON((const CV *)sv) ? "(anonymous)"
1019 : GvENAME(CvGV((const CV *)sv)));
5f66b61c 1020 /* FALLTHROUGH */
9607fc9c 1021 case SVt_PVFM:
6fc92669
GS
1022 {
1023 /* let user-undef'd sub keep its identity */
ea726b52
NC
1024 GV* const gv = CvGV((const CV *)sv);
1025 cv_undef(MUTABLE_CV(sv));
b3f91e91 1026 CvGV_set(MUTABLE_CV(sv), gv);
6fc92669 1027 }
a0d0e21e 1028 break;
8e07c86e 1029 case SVt_PVGV:
6e592b3a 1030 if (SvFAKE(sv)) {
3280af22 1031 SvSetMagicSV(sv, &PL_sv_undef);
6e592b3a
BM
1032 break;
1033 }
1034 else if (isGV_with_GP(sv)) {
20408e3c 1035 GP *gp;
dd69841b
BB
1036 HV *stash;
1037
dd69841b 1038 /* undef *Pkg::meth_name ... */
e530fb81
FC
1039 bool method_changed
1040 = GvCVu((const GV *)sv) && (stash = GvSTASH((const GV *)sv))
1041 && HvENAME_get(stash);
1042 /* undef *Foo:: */
1043 if((stash = GvHV((const GV *)sv))) {
1044 if(HvENAME_get(stash))
1045 SvREFCNT_inc_simple_void_NN(sv_2mortal((SV *)stash));
1046 else stash = NULL;
1047 }
dd69841b 1048
159b6efe 1049 gp_free(MUTABLE_GV(sv));
a02a5408 1050 Newxz(gp, 1, GP);
c43ae56f 1051 GvGP_set(sv, gp_ref(gp));
561b68a9 1052 GvSV(sv) = newSV(0);
57843af0 1053 GvLINE(sv) = CopLINE(PL_curcop);
159b6efe 1054 GvEGV(sv) = MUTABLE_GV(sv);
20408e3c 1055 GvMULTI_on(sv);
e530fb81
FC
1056
1057 if(stash)
afdbe55d 1058 mro_package_moved(NULL, stash, (const GV *)sv, 0);
e530fb81
FC
1059 stash = NULL;
1060 /* undef *Foo::ISA */
1061 if( strEQ(GvNAME((const GV *)sv), "ISA")
1062 && (stash = GvSTASH((const GV *)sv))
1063 && (method_changed || HvENAME(stash)) )
1064 mro_isa_changed_in(stash);
1065 else if(method_changed)
1066 mro_method_changed_in(
da9043f5 1067 GvSTASH((const GV *)sv)
e530fb81
FC
1068 );
1069
6e592b3a 1070 break;
20408e3c 1071 }
6e592b3a 1072 /* FALL THROUGH */
a0d0e21e 1073 default:
b15aece3 1074 if (SvTYPE(sv) >= SVt_PV && SvPVX_const(sv) && SvLEN(sv)) {
8bd4d4c5 1075 SvPV_free(sv);
c445ea15 1076 SvPV_set(sv, NULL);
4633a7c4 1077 SvLEN_set(sv, 0);
a0d0e21e 1078 }
0c34ef67 1079 SvOK_off(sv);
4633a7c4 1080 SvSETMAGIC(sv);
79072805 1081 }
a0d0e21e
LW
1082
1083 RETPUSHUNDEF;
79072805
LW
1084}
1085
a0d0e21e 1086PP(pp_predec)
79072805 1087{
97aff369 1088 dVAR; dSP;
6e592b3a 1089 if (SvTYPE(TOPs) >= SVt_PVAV || isGV_with_GP(TOPs))
6ad8f254 1090 Perl_croak_no_modify(aTHX);
3510b4a1
NC
1091 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
1092 && SvIVX(TOPs) != IV_MIN)
55497cff 1093 {
45977657 1094 SvIV_set(TOPs, SvIVX(TOPs) - 1);
55497cff 1095 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
748a9306
LW
1096 }
1097 else
1098 sv_dec(TOPs);
a0d0e21e
LW
1099 SvSETMAGIC(TOPs);
1100 return NORMAL;
1101}
79072805 1102
a0d0e21e
LW
1103PP(pp_postinc)
1104{
97aff369 1105 dVAR; dSP; dTARGET;
6e592b3a 1106 if (SvTYPE(TOPs) >= SVt_PVAV || isGV_with_GP(TOPs))
6ad8f254 1107 Perl_croak_no_modify(aTHX);
7dcb9b98
DM
1108 if (SvROK(TOPs))
1109 TARG = sv_newmortal();
a0d0e21e 1110 sv_setsv(TARG, TOPs);
3510b4a1
NC
1111 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
1112 && SvIVX(TOPs) != IV_MAX)
55497cff 1113 {
45977657 1114 SvIV_set(TOPs, SvIVX(TOPs) + 1);
55497cff 1115 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
748a9306
LW
1116 }
1117 else
6f1401dc 1118 sv_inc_nomg(TOPs);
a0d0e21e 1119 SvSETMAGIC(TOPs);
1e54a23f 1120 /* special case for undef: see thread at 2003-03/msg00536.html in archive */
a0d0e21e
LW
1121 if (!SvOK(TARG))
1122 sv_setiv(TARG, 0);
1123 SETs(TARG);
1124 return NORMAL;
1125}
79072805 1126
a0d0e21e
LW
1127PP(pp_postdec)
1128{
97aff369 1129 dVAR; dSP; dTARGET;
6e592b3a 1130 if (SvTYPE(TOPs) >= SVt_PVAV || isGV_with_GP(TOPs))
6ad8f254 1131 Perl_croak_no_modify(aTHX);
7dcb9b98
DM
1132 if (SvROK(TOPs))
1133 TARG = sv_newmortal();
a0d0e21e 1134 sv_setsv(TARG, TOPs);
3510b4a1
NC
1135 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
1136 && SvIVX(TOPs) != IV_MIN)
55497cff 1137 {
45977657 1138 SvIV_set(TOPs, SvIVX(TOPs) - 1);
55497cff 1139 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
748a9306
LW
1140 }
1141 else
6f1401dc 1142 sv_dec_nomg(TOPs);
a0d0e21e
LW
1143 SvSETMAGIC(TOPs);
1144 SETs(TARG);
1145 return NORMAL;
1146}
79072805 1147
a0d0e21e
LW
1148/* Ordinary operators. */
1149
1150PP(pp_pow)
1151{
800401ee 1152 dVAR; dSP; dATARGET; SV *svl, *svr;
58d76dfd 1153#ifdef PERL_PRESERVE_IVUV
52a96ae6
HS
1154 bool is_int = 0;
1155#endif
6f1401dc
DM
1156 tryAMAGICbin_MG(pow_amg, AMGf_assign|AMGf_numeric);
1157 svr = TOPs;
1158 svl = TOPm1s;
52a96ae6
HS
1159#ifdef PERL_PRESERVE_IVUV
1160 /* For integer to integer power, we do the calculation by hand wherever
1161 we're sure it is safe; otherwise we call pow() and try to convert to
1162 integer afterwards. */
58d76dfd 1163 {
6f1401dc 1164 SvIV_please_nomg(svr);
800401ee 1165 if (SvIOK(svr)) {
6f1401dc 1166 SvIV_please_nomg(svl);
800401ee 1167 if (SvIOK(svl)) {
900658e3
PF
1168 UV power;
1169 bool baseuok;
1170 UV baseuv;
1171
800401ee
JH
1172 if (SvUOK(svr)) {
1173 power = SvUVX(svr);
900658e3 1174 } else {
800401ee 1175 const IV iv = SvIVX(svr);
900658e3
PF
1176 if (iv >= 0) {
1177 power = iv;
1178 } else {
1179 goto float_it; /* Can't do negative powers this way. */
1180 }
1181 }
1182
800401ee 1183 baseuok = SvUOK(svl);
900658e3 1184 if (baseuok) {
800401ee 1185 baseuv = SvUVX(svl);
900658e3 1186 } else {
800401ee 1187 const IV iv = SvIVX(svl);
900658e3
PF
1188 if (iv >= 0) {
1189 baseuv = iv;
1190 baseuok = TRUE; /* effectively it's a UV now */
1191 } else {
1192 baseuv = -iv; /* abs, baseuok == false records sign */
1193 }
1194 }
52a96ae6
HS
1195 /* now we have integer ** positive integer. */
1196 is_int = 1;
1197
1198 /* foo & (foo - 1) is zero only for a power of 2. */
58d76dfd 1199 if (!(baseuv & (baseuv - 1))) {
52a96ae6 1200 /* We are raising power-of-2 to a positive integer.
58d76dfd
JH
1201 The logic here will work for any base (even non-integer
1202 bases) but it can be less accurate than
1203 pow (base,power) or exp (power * log (base)) when the
1204 intermediate values start to spill out of the mantissa.
1205 With powers of 2 we know this can't happen.
1206 And powers of 2 are the favourite thing for perl
1207 programmers to notice ** not doing what they mean. */
1208 NV result = 1.0;
1209 NV base = baseuok ? baseuv : -(NV)baseuv;
900658e3
PF
1210
1211 if (power & 1) {
1212 result *= base;
1213 }
1214 while (power >>= 1) {
1215 base *= base;
1216 if (power & 1) {
1217 result *= base;
1218 }
1219 }
58d76dfd
JH
1220 SP--;
1221 SETn( result );
6f1401dc 1222 SvIV_please_nomg(svr);
58d76dfd 1223 RETURN;
52a96ae6
HS
1224 } else {
1225 register unsigned int highbit = 8 * sizeof(UV);
900658e3
PF
1226 register unsigned int diff = 8 * sizeof(UV);
1227 while (diff >>= 1) {
1228 highbit -= diff;
1229 if (baseuv >> highbit) {
1230 highbit += diff;
1231 }
52a96ae6
HS
1232 }
1233 /* we now have baseuv < 2 ** highbit */
1234 if (power * highbit <= 8 * sizeof(UV)) {
1235 /* result will definitely fit in UV, so use UV math
1236 on same algorithm as above */
1237 register UV result = 1;
1238 register UV base = baseuv;
f2338a2e 1239 const bool odd_power = cBOOL(power & 1);
900658e3
PF
1240 if (odd_power) {
1241 result *= base;
1242 }
1243 while (power >>= 1) {
1244 base *= base;
1245 if (power & 1) {
52a96ae6 1246 result *= base;
52a96ae6
HS
1247 }
1248 }
1249 SP--;
0615a994 1250 if (baseuok || !odd_power)
52a96ae6
HS
1251 /* answer is positive */
1252 SETu( result );
1253 else if (result <= (UV)IV_MAX)
1254 /* answer negative, fits in IV */
1255 SETi( -(IV)result );
1256 else if (result == (UV)IV_MIN)
1257 /* 2's complement assumption: special case IV_MIN */
1258 SETi( IV_MIN );
1259 else
1260 /* answer negative, doesn't fit */
1261 SETn( -(NV)result );
1262 RETURN;
1263 }
1264 }
1265 }
1266 }
58d76dfd 1267 }
52a96ae6 1268 float_it:
58d76dfd 1269#endif
a0d0e21e 1270 {
6f1401dc
DM
1271 NV right = SvNV_nomg(svr);
1272 NV left = SvNV_nomg(svl);
4efa5a16 1273 (void)POPs;
3aaeb624
JA
1274
1275#if defined(USE_LONG_DOUBLE) && defined(HAS_AIX_POWL_NEG_BASE_BUG)
1276 /*
1277 We are building perl with long double support and are on an AIX OS
1278 afflicted with a powl() function that wrongly returns NaNQ for any
1279 negative base. This was reported to IBM as PMR #23047-379 on
1280 03/06/2006. The problem exists in at least the following versions
1281 of AIX and the libm fileset, and no doubt others as well:
1282
1283 AIX 4.3.3-ML10 bos.adt.libm 4.3.3.50
1284 AIX 5.1.0-ML04 bos.adt.libm 5.1.0.29
1285 AIX 5.2.0 bos.adt.libm 5.2.0.85
1286
1287 So, until IBM fixes powl(), we provide the following workaround to
1288 handle the problem ourselves. Our logic is as follows: for
1289 negative bases (left), we use fmod(right, 2) to check if the
1290 exponent is an odd or even integer:
1291
1292 - if odd, powl(left, right) == -powl(-left, right)
1293 - if even, powl(left, right) == powl(-left, right)
1294
1295 If the exponent is not an integer, the result is rightly NaNQ, so
1296 we just return that (as NV_NAN).
1297 */
1298
1299 if (left < 0.0) {
1300 NV mod2 = Perl_fmod( right, 2.0 );
1301 if (mod2 == 1.0 || mod2 == -1.0) { /* odd integer */
1302 SETn( -Perl_pow( -left, right) );
1303 } else if (mod2 == 0.0) { /* even integer */
1304 SETn( Perl_pow( -left, right) );
1305 } else { /* fractional power */
1306 SETn( NV_NAN );
1307 }
1308 } else {
1309 SETn( Perl_pow( left, right) );
1310 }
1311#else
52a96ae6 1312 SETn( Perl_pow( left, right) );
3aaeb624
JA
1313#endif /* HAS_AIX_POWL_NEG_BASE_BUG */
1314
52a96ae6
HS
1315#ifdef PERL_PRESERVE_IVUV
1316 if (is_int)
6f1401dc 1317 SvIV_please_nomg(svr);
52a96ae6
HS
1318#endif
1319 RETURN;
93a17b20 1320 }
a0d0e21e
LW
1321}
1322
1323PP(pp_multiply)
1324{
800401ee 1325 dVAR; dSP; dATARGET; SV *svl, *svr;
6f1401dc
DM
1326 tryAMAGICbin_MG(mult_amg, AMGf_assign|AMGf_numeric);
1327 svr = TOPs;
1328 svl = TOPm1s;
28e5dec8 1329#ifdef PERL_PRESERVE_IVUV
6f1401dc 1330 SvIV_please_nomg(svr);
800401ee 1331 if (SvIOK(svr)) {
28e5dec8
JH
1332 /* Unless the left argument is integer in range we are going to have to
1333 use NV maths. Hence only attempt to coerce the right argument if
1334 we know the left is integer. */
1335 /* Left operand is defined, so is it IV? */
6f1401dc 1336 SvIV_please_nomg(svl);
800401ee
JH
1337 if (SvIOK(svl)) {
1338 bool auvok = SvUOK(svl);
1339 bool buvok = SvUOK(svr);
28e5dec8
JH
1340 const UV topmask = (~ (UV)0) << (4 * sizeof (UV));
1341 const UV botmask = ~((~ (UV)0) << (4 * sizeof (UV)));
1342 UV alow;
1343 UV ahigh;
1344 UV blow;
1345 UV bhigh;
1346
1347 if (auvok) {
800401ee 1348 alow = SvUVX(svl);
28e5dec8 1349 } else {
800401ee 1350 const IV aiv = SvIVX(svl);
28e5dec8
JH
1351 if (aiv >= 0) {
1352 alow = aiv;
1353 auvok = TRUE; /* effectively it's a UV now */
1354 } else {
1355 alow = -aiv; /* abs, auvok == false records sign */
1356 }
1357 }
1358 if (buvok) {
800401ee 1359 blow = SvUVX(svr);
28e5dec8 1360 } else {
800401ee 1361 const IV biv = SvIVX(svr);
28e5dec8
JH
1362 if (biv >= 0) {
1363 blow = biv;
1364 buvok = TRUE; /* effectively it's a UV now */
1365 } else {
1366 blow = -biv; /* abs, buvok == false records sign */
1367 }
1368 }
1369
1370 /* If this does sign extension on unsigned it's time for plan B */
1371 ahigh = alow >> (4 * sizeof (UV));
1372 alow &= botmask;
1373 bhigh = blow >> (4 * sizeof (UV));
1374 blow &= botmask;
1375 if (ahigh && bhigh) {
6f207bd3 1376 NOOP;
28e5dec8
JH
1377 /* eg 32 bit is at least 0x10000 * 0x10000 == 0x100000000
1378 which is overflow. Drop to NVs below. */
1379 } else if (!ahigh && !bhigh) {
1380 /* eg 32 bit is at most 0xFFFF * 0xFFFF == 0xFFFE0001
1381 so the unsigned multiply cannot overflow. */
c445ea15 1382 const UV product = alow * blow;
28e5dec8
JH
1383 if (auvok == buvok) {
1384 /* -ve * -ve or +ve * +ve gives a +ve result. */
1385 SP--;
1386 SETu( product );
1387 RETURN;
1388 } else if (product <= (UV)IV_MIN) {
1389 /* 2s complement assumption that (UV)-IV_MIN is correct. */
1390 /* -ve result, which could overflow an IV */
1391 SP--;
25716404 1392 SETi( -(IV)product );
28e5dec8
JH
1393 RETURN;
1394 } /* else drop to NVs below. */
1395 } else {
1396 /* One operand is large, 1 small */
1397 UV product_middle;
1398 if (bhigh) {
1399 /* swap the operands */
1400 ahigh = bhigh;
1401 bhigh = blow; /* bhigh now the temp var for the swap */
1402 blow = alow;
1403 alow = bhigh;
1404 }
1405 /* now, ((ahigh * blow) << half_UV_len) + (alow * blow)
1406 multiplies can't overflow. shift can, add can, -ve can. */
1407 product_middle = ahigh * blow;
1408 if (!(product_middle & topmask)) {
1409 /* OK, (ahigh * blow) won't lose bits when we shift it. */
1410 UV product_low;
1411 product_middle <<= (4 * sizeof (UV));
1412 product_low = alow * blow;
1413
1414 /* as for pp_add, UV + something mustn't get smaller.
1415 IIRC ANSI mandates this wrapping *behaviour* for
1416 unsigned whatever the actual representation*/
1417 product_low += product_middle;
1418 if (product_low >= product_middle) {
1419 /* didn't overflow */
1420 if (auvok == buvok) {
1421 /* -ve * -ve or +ve * +ve gives a +ve result. */
1422 SP--;
1423 SETu( product_low );
1424 RETURN;
1425 } else if (product_low <= (UV)IV_MIN) {
1426 /* 2s complement assumption again */
1427 /* -ve result, which could overflow an IV */
1428 SP--;
25716404 1429 SETi( -(IV)product_low );
28e5dec8
JH
1430 RETURN;
1431 } /* else drop to NVs below. */
1432 }
1433 } /* product_middle too large */
1434 } /* ahigh && bhigh */
800401ee
JH
1435 } /* SvIOK(svl) */
1436 } /* SvIOK(svr) */
28e5dec8 1437#endif
a0d0e21e 1438 {
6f1401dc
DM
1439 NV right = SvNV_nomg(svr);
1440 NV left = SvNV_nomg(svl);
4efa5a16 1441 (void)POPs;
a0d0e21e
LW
1442 SETn( left * right );
1443 RETURN;
79072805 1444 }
a0d0e21e
LW
1445}
1446
1447PP(pp_divide)
1448{
800401ee 1449 dVAR; dSP; dATARGET; SV *svl, *svr;
6f1401dc
DM
1450 tryAMAGICbin_MG(div_amg, AMGf_assign|AMGf_numeric);
1451 svr = TOPs;
1452 svl = TOPm1s;
5479d192 1453 /* Only try to do UV divide first
68795e93 1454 if ((SLOPPYDIVIDE is true) or
5479d192
NC
1455 (PERL_PRESERVE_IVUV is true and one or both SV is a UV too large
1456 to preserve))
1457 The assumption is that it is better to use floating point divide
1458 whenever possible, only doing integer divide first if we can't be sure.
1459 If NV_PRESERVES_UV is true then we know at compile time that no UV
1460 can be too large to preserve, so don't need to compile the code to
1461 test the size of UVs. */
1462
a0d0e21e 1463#ifdef SLOPPYDIVIDE
5479d192
NC
1464# define PERL_TRY_UV_DIVIDE
1465 /* ensure that 20./5. == 4. */
a0d0e21e 1466#else
5479d192
NC
1467# ifdef PERL_PRESERVE_IVUV
1468# ifndef NV_PRESERVES_UV
1469# define PERL_TRY_UV_DIVIDE
1470# endif
1471# endif
a0d0e21e 1472#endif
5479d192
NC
1473
1474#ifdef PERL_TRY_UV_DIVIDE
6f1401dc 1475 SvIV_please_nomg(svr);
800401ee 1476 if (SvIOK(svr)) {
6f1401dc 1477 SvIV_please_nomg(svl);
800401ee
JH
1478 if (SvIOK(svl)) {
1479 bool left_non_neg = SvUOK(svl);
1480 bool right_non_neg = SvUOK(svr);
5479d192
NC
1481 UV left;
1482 UV right;
1483
1484 if (right_non_neg) {
800401ee 1485 right = SvUVX(svr);
5479d192
NC
1486 }
1487 else {
800401ee 1488 const IV biv = SvIVX(svr);
5479d192
NC
1489 if (biv >= 0) {
1490 right = biv;
1491 right_non_neg = TRUE; /* effectively it's a UV now */
1492 }
1493 else {
1494 right = -biv;
1495 }
1496 }
1497 /* historically undef()/0 gives a "Use of uninitialized value"
1498 warning before dieing, hence this test goes here.
1499 If it were immediately before the second SvIV_please, then
1500 DIE() would be invoked before left was even inspected, so
486ec47a 1501 no inspection would give no warning. */
5479d192
NC
1502 if (right == 0)
1503 DIE(aTHX_ "Illegal division by zero");
1504
1505 if (left_non_neg) {
800401ee 1506 left = SvUVX(svl);
5479d192
NC
1507 }
1508 else {
800401ee 1509 const IV aiv = SvIVX(svl);
5479d192
NC
1510 if (aiv >= 0) {
1511 left = aiv;
1512 left_non_neg = TRUE; /* effectively it's a UV now */
1513 }
1514 else {
1515 left = -aiv;
1516 }
1517 }
1518
1519 if (left >= right
1520#ifdef SLOPPYDIVIDE
1521 /* For sloppy divide we always attempt integer division. */
1522#else
1523 /* Otherwise we only attempt it if either or both operands
1524 would not be preserved by an NV. If both fit in NVs
0c2ee62a
NC
1525 we fall through to the NV divide code below. However,
1526 as left >= right to ensure integer result here, we know that
1527 we can skip the test on the right operand - right big
1528 enough not to be preserved can't get here unless left is
1529 also too big. */
1530
1531 && (left > ((UV)1 << NV_PRESERVES_UV_BITS))
5479d192
NC
1532#endif
1533 ) {
1534 /* Integer division can't overflow, but it can be imprecise. */
1b6737cc 1535 const UV result = left / right;
5479d192
NC
1536 if (result * right == left) {
1537 SP--; /* result is valid */
1538 if (left_non_neg == right_non_neg) {
1539 /* signs identical, result is positive. */
1540 SETu( result );
1541 RETURN;
1542 }
1543 /* 2s complement assumption */
1544 if (result <= (UV)IV_MIN)
91f3b821 1545 SETi( -(IV)result );
5479d192
NC
1546 else {
1547 /* It's exact but too negative for IV. */
1548 SETn( -(NV)result );
1549 }
1550 RETURN;
1551 } /* tried integer divide but it was not an integer result */
32fdb065 1552 } /* else (PERL_ABS(result) < 1.0) or (both UVs in range for NV) */
5479d192
NC
1553 } /* left wasn't SvIOK */
1554 } /* right wasn't SvIOK */
1555#endif /* PERL_TRY_UV_DIVIDE */
1556 {
6f1401dc
DM
1557 NV right = SvNV_nomg(svr);
1558 NV left = SvNV_nomg(svl);
4efa5a16 1559 (void)POPs;(void)POPs;
ebc6a117
PD
1560#if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
1561 if (! Perl_isnan(right) && right == 0.0)
1562#else
5479d192 1563 if (right == 0.0)
ebc6a117 1564#endif
5479d192
NC
1565 DIE(aTHX_ "Illegal division by zero");
1566 PUSHn( left / right );
1567 RETURN;
79072805 1568 }
a0d0e21e
LW
1569}
1570
1571PP(pp_modulo)
1572{
6f1401dc
DM
1573 dVAR; dSP; dATARGET;
1574 tryAMAGICbin_MG(modulo_amg, AMGf_assign|AMGf_numeric);
a0d0e21e 1575 {
9c5ffd7c
JH
1576 UV left = 0;
1577 UV right = 0;
dc656993
JH
1578 bool left_neg = FALSE;
1579 bool right_neg = FALSE;
e2c88acc
NC
1580 bool use_double = FALSE;
1581 bool dright_valid = FALSE;
9c5ffd7c
JH
1582 NV dright = 0.0;
1583 NV dleft = 0.0;
6f1401dc
DM
1584 SV * const svr = TOPs;
1585 SV * const svl = TOPm1s;
1586 SvIV_please_nomg(svr);
800401ee
JH
1587 if (SvIOK(svr)) {
1588 right_neg = !SvUOK(svr);
e2c88acc 1589 if (!right_neg) {
800401ee 1590 right = SvUVX(svr);
e2c88acc 1591 } else {
800401ee 1592 const IV biv = SvIVX(svr);
e2c88acc
NC
1593 if (biv >= 0) {
1594 right = biv;
1595 right_neg = FALSE; /* effectively it's a UV now */
1596 } else {
1597 right = -biv;
1598 }
1599 }
1600 }
1601 else {
6f1401dc 1602 dright = SvNV_nomg(svr);
787eafbd
IZ
1603 right_neg = dright < 0;
1604 if (right_neg)
1605 dright = -dright;
e2c88acc
NC
1606 if (dright < UV_MAX_P1) {
1607 right = U_V(dright);
1608 dright_valid = TRUE; /* In case we need to use double below. */
1609 } else {
1610 use_double = TRUE;
1611 }
787eafbd 1612 }
a0d0e21e 1613
e2c88acc
NC
1614 /* At this point use_double is only true if right is out of range for
1615 a UV. In range NV has been rounded down to nearest UV and
1616 use_double false. */
6f1401dc 1617 SvIV_please_nomg(svl);
800401ee
JH
1618 if (!use_double && SvIOK(svl)) {
1619 if (SvIOK(svl)) {
1620 left_neg = !SvUOK(svl);
e2c88acc 1621 if (!left_neg) {
800401ee 1622 left = SvUVX(svl);
e2c88acc 1623 } else {
800401ee 1624 const IV aiv = SvIVX(svl);
e2c88acc
NC
1625 if (aiv >= 0) {
1626 left = aiv;
1627 left_neg = FALSE; /* effectively it's a UV now */
1628 } else {
1629 left = -aiv;
1630 }
1631 }
1632 }
1633 }
787eafbd 1634 else {
6f1401dc 1635 dleft = SvNV_nomg(svl);
787eafbd
IZ
1636 left_neg = dleft < 0;
1637 if (left_neg)
1638 dleft = -dleft;
68dc0745 1639
e2c88acc
NC
1640 /* This should be exactly the 5.6 behaviour - if left and right are
1641 both in range for UV then use U_V() rather than floor. */
1642 if (!use_double) {
1643 if (dleft < UV_MAX_P1) {
1644 /* right was in range, so is dleft, so use UVs not double.
1645 */
1646 left = U_V(dleft);
1647 }
1648 /* left is out of range for UV, right was in range, so promote
1649 right (back) to double. */
1650 else {
1651 /* The +0.5 is used in 5.6 even though it is not strictly
1652 consistent with the implicit +0 floor in the U_V()
1653 inside the #if 1. */
1654 dleft = Perl_floor(dleft + 0.5);
1655 use_double = TRUE;
1656 if (dright_valid)
1657 dright = Perl_floor(dright + 0.5);
1658 else
1659 dright = right;
1660 }
1661 }
1662 }
6f1401dc 1663 sp -= 2;
787eafbd 1664 if (use_double) {
65202027 1665 NV dans;
787eafbd 1666
787eafbd 1667 if (!dright)
cea2e8a9 1668 DIE(aTHX_ "Illegal modulus zero");
787eafbd 1669
65202027 1670 dans = Perl_fmod(dleft, dright);
787eafbd
IZ
1671 if ((left_neg != right_neg) && dans)
1672 dans = dright - dans;
1673 if (right_neg)
1674 dans = -dans;
1675 sv_setnv(TARG, dans);
1676 }
1677 else {
1678 UV ans;
1679
787eafbd 1680 if (!right)
cea2e8a9 1681 DIE(aTHX_ "Illegal modulus zero");
787eafbd
IZ
1682
1683 ans = left % right;
1684 if ((left_neg != right_neg) && ans)
1685 ans = right - ans;
1686 if (right_neg) {
1687 /* XXX may warn: unary minus operator applied to unsigned type */
1688 /* could change -foo to be (~foo)+1 instead */
1689 if (ans <= ~((UV)IV_MAX)+1)
1690 sv_setiv(TARG, ~ans+1);
1691 else
65202027 1692 sv_setnv(TARG, -(NV)ans);
787eafbd
IZ
1693 }
1694 else
1695 sv_setuv(TARG, ans);
1696 }
1697 PUSHTARG;
1698 RETURN;
79072805 1699 }
a0d0e21e 1700}
79072805 1701
a0d0e21e
LW
1702PP(pp_repeat)
1703{
6f1401dc 1704 dVAR; dSP; dATARGET;
2b573ace 1705 register IV count;
6f1401dc
DM
1706 SV *sv;
1707
1708 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1709 /* TODO: think of some way of doing list-repeat overloading ??? */
1710 sv = POPs;
1711 SvGETMAGIC(sv);
1712 }
1713 else {
1714 tryAMAGICbin_MG(repeat_amg, AMGf_assign);
1715 sv = POPs;
1716 }
1717
2b573ace
JH
1718 if (SvIOKp(sv)) {
1719 if (SvUOK(sv)) {
6f1401dc 1720 const UV uv = SvUV_nomg(sv);
2b573ace
JH
1721 if (uv > IV_MAX)
1722 count = IV_MAX; /* The best we can do? */
1723 else
1724 count = uv;
1725 } else {
6f1401dc 1726 const IV iv = SvIV_nomg(sv);
2b573ace
JH
1727 if (iv < 0)
1728 count = 0;
1729 else
1730 count = iv;
1731 }
1732 }
1733 else if (SvNOKp(sv)) {
6f1401dc 1734 const NV nv = SvNV_nomg(sv);
2b573ace
JH
1735 if (nv < 0.0)
1736 count = 0;
1737 else
1738 count = (IV)nv;
1739 }
1740 else
6f1401dc
DM
1741 count = SvIV_nomg(sv);
1742
533c011a 1743 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
a0d0e21e 1744 dMARK;
0bd48802
AL
1745 static const char oom_list_extend[] = "Out of memory during list extend";
1746 const I32 items = SP - MARK;
1747 const I32 max = items * count;
79072805 1748
2b573ace
JH
1749 MEM_WRAP_CHECK_1(max, SV*, oom_list_extend);
1750 /* Did the max computation overflow? */
27d5b266 1751 if (items > 0 && max > 0 && (max < items || max < count))
2b573ace 1752 Perl_croak(aTHX_ oom_list_extend);
a0d0e21e
LW
1753 MEXTEND(MARK, max);
1754 if (count > 1) {
1755 while (SP > MARK) {
976c8a39
JH
1756#if 0
1757 /* This code was intended to fix 20010809.028:
1758
1759 $x = 'abcd';
1760 for (($x =~ /./g) x 2) {
1761 print chop; # "abcdabcd" expected as output.
1762 }
1763
1764 * but that change (#11635) broke this code:
1765
1766 $x = [("foo")x2]; # only one "foo" ended up in the anonlist.
1767
1768 * I can't think of a better fix that doesn't introduce
1769 * an efficiency hit by copying the SVs. The stack isn't
1770 * refcounted, and mortalisation obviously doesn't
1771 * Do The Right Thing when the stack has more than
1772 * one pointer to the same mortal value.
1773 * .robin.
1774 */
e30acc16
RH
1775 if (*SP) {
1776 *SP = sv_2mortal(newSVsv(*SP));
1777 SvREADONLY_on(*SP);
1778 }
976c8a39
JH
1779#else
1780 if (*SP)
1781 SvTEMP_off((*SP));
1782#endif
a0d0e21e 1783 SP--;
79072805 1784 }
a0d0e21e
LW
1785 MARK++;
1786 repeatcpy((char*)(MARK + items), (char*)MARK,
ad64d0ec 1787 items * sizeof(const SV *), count - 1);
a0d0e21e 1788 SP += max;
79072805 1789 }
a0d0e21e
LW
1790 else if (count <= 0)
1791 SP -= items;
79072805 1792 }
a0d0e21e 1793 else { /* Note: mark already snarfed by pp_list */
0bd48802 1794 SV * const tmpstr = POPs;
a0d0e21e 1795 STRLEN len;
9b877dbb 1796 bool isutf;
2b573ace
JH
1797 static const char oom_string_extend[] =
1798 "Out of memory during string extend";
a0d0e21e 1799
6f1401dc
DM
1800 if (TARG != tmpstr)
1801 sv_setsv_nomg(TARG, tmpstr);
1802 SvPV_force_nomg(TARG, len);
9b877dbb 1803 isutf = DO_UTF8(TARG);
8ebc5c01 1804 if (count != 1) {
1805 if (count < 1)
1806 SvCUR_set(TARG, 0);
1807 else {
c445ea15 1808 const STRLEN max = (UV)count * len;
19a94d75 1809 if (len > MEM_SIZE_MAX / count)
2b573ace
JH
1810 Perl_croak(aTHX_ oom_string_extend);
1811 MEM_WRAP_CHECK_1(max, char, oom_string_extend);
8569b9dc 1812 SvGROW(TARG, max + 1);
a0d0e21e 1813 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
b162af07 1814 SvCUR_set(TARG, SvCUR(TARG) * count);
7a4c00b4 1815 }
a0d0e21e 1816 *SvEND(TARG) = '\0';
a0d0e21e 1817 }
dfcb284a
GS
1818 if (isutf)
1819 (void)SvPOK_only_UTF8(TARG);
1820 else
1821 (void)SvPOK_only(TARG);
b80b6069
RH
1822
1823 if (PL_op->op_private & OPpREPEAT_DOLIST) {
1824 /* The parser saw this as a list repeat, and there
1825 are probably several items on the stack. But we're
1826 in scalar context, and there's no pp_list to save us
1827 now. So drop the rest of the items -- robin@kitsite.com
1828 */
1829 dMARK;
1830 SP = MARK;
1831 }
a0d0e21e 1832 PUSHTARG;
79072805 1833 }
a0d0e21e
LW
1834 RETURN;
1835}
79072805 1836
a0d0e21e
LW
1837PP(pp_subtract)
1838{
800401ee 1839 dVAR; dSP; dATARGET; bool useleft; SV *svl, *svr;
6f1401dc
DM
1840 tryAMAGICbin_MG(subtr_amg, AMGf_assign|AMGf_numeric);
1841 svr = TOPs;
1842 svl = TOPm1s;
800401ee 1843 useleft = USE_LEFT(svl);
28e5dec8 1844#ifdef PERL_PRESERVE_IVUV
7dca457a
NC
1845 /* See comments in pp_add (in pp_hot.c) about Overflow, and how
1846 "bad things" happen if you rely on signed integers wrapping. */
6f1401dc 1847 SvIV_please_nomg(svr);
800401ee 1848 if (SvIOK(svr)) {
28e5dec8
JH
1849 /* Unless the left argument is integer in range we are going to have to
1850 use NV maths. Hence only attempt to coerce the right argument if
1851 we know the left is integer. */
9c5ffd7c
JH
1852 register UV auv = 0;
1853 bool auvok = FALSE;
7dca457a
NC
1854 bool a_valid = 0;
1855
28e5dec8 1856 if (!useleft) {
7dca457a
NC
1857 auv = 0;
1858 a_valid = auvok = 1;
1859 /* left operand is undef, treat as zero. */
28e5dec8
JH
1860 } else {
1861 /* Left operand is defined, so is it IV? */
6f1401dc 1862 SvIV_please_nomg(svl);
800401ee
JH
1863 if (SvIOK(svl)) {
1864 if ((auvok = SvUOK(svl)))
1865 auv = SvUVX(svl);
7dca457a 1866 else {
800401ee 1867 register const IV aiv = SvIVX(svl);
7dca457a
NC
1868 if (aiv >= 0) {
1869 auv = aiv;
1870 auvok = 1; /* Now acting as a sign flag. */
1871 } else { /* 2s complement assumption for IV_MIN */
1872 auv = (UV)-aiv;
28e5dec8 1873 }
7dca457a
NC
1874 }
1875 a_valid = 1;
1876 }
1877 }
1878 if (a_valid) {
1879 bool result_good = 0;
1880 UV result;
1881 register UV buv;
800401ee 1882 bool buvok = SvUOK(svr);
9041c2e3 1883
7dca457a 1884 if (buvok)
800401ee 1885 buv = SvUVX(svr);
7dca457a 1886 else {
800401ee 1887 register const IV biv = SvIVX(svr);
7dca457a
NC
1888 if (biv >= 0) {
1889 buv = biv;
1890 buvok = 1;
1891 } else
1892 buv = (UV)-biv;
1893 }
1894 /* ?uvok if value is >= 0. basically, flagged as UV if it's +ve,
602f51c4 1895 else "IV" now, independent of how it came in.
7dca457a
NC
1896 if a, b represents positive, A, B negative, a maps to -A etc
1897 a - b => (a - b)
1898 A - b => -(a + b)
1899 a - B => (a + b)
1900 A - B => -(a - b)
1901 all UV maths. negate result if A negative.
1902 subtract if signs same, add if signs differ. */
1903
1904 if (auvok ^ buvok) {
1905 /* Signs differ. */
1906 result = auv + buv;
1907 if (result >= auv)
1908 result_good = 1;
1909 } else {
1910 /* Signs same */
1911 if (auv >= buv) {
1912 result = auv - buv;
1913 /* Must get smaller */
1914 if (result <= auv)
1915 result_good = 1;
1916 } else {
1917 result = buv - auv;
1918 if (result <= buv) {
1919 /* result really should be -(auv-buv). as its negation
1920 of true value, need to swap our result flag */
1921 auvok = !auvok;
1922 result_good = 1;
28e5dec8 1923 }
28e5dec8
JH
1924 }
1925 }
7dca457a
NC
1926 if (result_good) {
1927 SP--;
1928 if (auvok)
1929 SETu( result );
1930 else {
1931 /* Negate result */
1932 if (result <= (UV)IV_MIN)
1933 SETi( -(IV)result );
1934 else {
1935 /* result valid, but out of range for IV. */
1936 SETn( -(NV)result );
1937 }
1938 }
1939 RETURN;
1940 } /* Overflow, drop through to NVs. */
28e5dec8
JH
1941 }
1942 }
1943#endif
a0d0e21e 1944 {
6f1401dc 1945 NV value = SvNV_nomg(svr);
4efa5a16
RD
1946 (void)POPs;
1947
28e5dec8
JH
1948 if (!useleft) {
1949 /* left operand is undef, treat as zero - value */
1950 SETn(-value);
1951 RETURN;
1952 }
6f1401dc 1953 SETn( SvNV_nomg(svl) - value );
28e5dec8 1954 RETURN;
79072805 1955 }
a0d0e21e 1956}
79072805 1957
a0d0e21e
LW
1958PP(pp_left_shift)
1959{
6f1401dc 1960 dVAR; dSP; dATARGET; SV *svl, *svr;
a42d0242 1961 tryAMAGICbin_MG(lshift_amg, AMGf_assign|AMGf_numeric);
6f1401dc
DM
1962 svr = POPs;
1963 svl = TOPs;
a0d0e21e 1964 {
6f1401dc 1965 const IV shift = SvIV_nomg(svr);
d0ba1bd2 1966 if (PL_op->op_private & HINT_INTEGER) {
6f1401dc 1967 const IV i = SvIV_nomg(svl);
972b05a9 1968 SETi(i << shift);
d0ba1bd2
JH
1969 }
1970 else {
6f1401dc 1971 const UV u = SvUV_nomg(svl);
972b05a9 1972 SETu(u << shift);
d0ba1bd2 1973 }
55497cff 1974 RETURN;
79072805 1975 }
a0d0e21e 1976}
79072805 1977
a0d0e21e
LW
1978PP(pp_right_shift)
1979{
6f1401dc 1980 dVAR; dSP; dATARGET; SV *svl, *svr;
a42d0242 1981 tryAMAGICbin_MG(rshift_amg, AMGf_assign|AMGf_numeric);
6f1401dc
DM
1982 svr = POPs;
1983 svl = TOPs;
a0d0e21e 1984 {
6f1401dc 1985 const IV shift = SvIV_nomg(svr);
d0ba1bd2 1986 if (PL_op->op_private & HINT_INTEGER) {
6f1401dc 1987 const IV i = SvIV_nomg(svl);
972b05a9 1988 SETi(i >> shift);
d0ba1bd2
JH
1989 }
1990 else {
6f1401dc 1991 const UV u = SvUV_nomg(svl);
972b05a9 1992 SETu(u >> shift);
d0ba1bd2 1993 }
a0d0e21e 1994 RETURN;
93a17b20 1995 }
79072805
LW
1996}
1997
a0d0e21e 1998PP(pp_lt)
79072805 1999{
6f1401dc 2000 dVAR; dSP;
a42d0242 2001 tryAMAGICbin_MG(lt_amg, AMGf_set|AMGf_numeric);
28e5dec8 2002#ifdef PERL_PRESERVE_IVUV
6f1401dc 2003 SvIV_please_nomg(TOPs);
28e5dec8 2004 if (SvIOK(TOPs)) {
6f1401dc 2005 SvIV_please_nomg(TOPm1s);
28e5dec8
JH
2006 if (SvIOK(TOPm1s)) {
2007 bool auvok = SvUOK(TOPm1s);
2008 bool buvok = SvUOK(TOPs);
a227d84d 2009
28e5dec8 2010 if (!auvok && !buvok) { /* ## IV < IV ## */
1b6737cc
AL
2011 const IV aiv = SvIVX(TOPm1s);
2012 const IV biv = SvIVX(TOPs);
28e5dec8
JH
2013
2014 SP--;
2015 SETs(boolSV(aiv < biv));
2016 RETURN;
2017 }
2018 if (auvok && buvok) { /* ## UV < UV ## */
1b6737cc
AL
2019 const UV auv = SvUVX(TOPm1s);
2020 const UV buv = SvUVX(TOPs);
28e5dec8
JH
2021
2022 SP--;
2023 SETs(boolSV(auv < buv));
2024 RETURN;
2025 }
2026 if (auvok) { /* ## UV < IV ## */
2027 UV auv;
1b6737cc 2028 const IV biv = SvIVX(TOPs);
28e5dec8
JH
2029 SP--;
2030 if (biv < 0) {
2031 /* As (a) is a UV, it's >=0, so it cannot be < */
2032 SETs(&PL_sv_no);
2033 RETURN;
2034 }
2035 auv = SvUVX(TOPs);
28e5dec8
JH
2036 SETs(boolSV(auv < (UV)biv));
2037 RETURN;
2038 }
2039 { /* ## IV < UV ## */
1b6737cc 2040 const IV aiv = SvIVX(TOPm1s);
28e5dec8
JH
2041 UV buv;
2042
28e5dec8
JH
2043 if (aiv < 0) {
2044 /* As (b) is a UV, it's >=0, so it must be < */
2045 SP--;
2046 SETs(&PL_sv_yes);
2047 RETURN;
2048 }
2049 buv = SvUVX(TOPs);
2050 SP--;
28e5dec8
JH
2051 SETs(boolSV((UV)aiv < buv));
2052 RETURN;
2053 }
2054 }
2055 }
2056#endif
30de85b6 2057#ifndef NV_PRESERVES_UV
50fb3111
NC
2058#ifdef PERL_PRESERVE_IVUV
2059 else
2060#endif
ed3b9b3c 2061 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
0bdaccee
NC
2062 SP--;
2063 SETs(boolSV(SvRV(TOPs) < SvRV(TOPp1s)));
2064 RETURN;
2065 }
30de85b6 2066#endif
a0d0e21e 2067 {
cab190d4 2068#if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
6f1401dc 2069 dPOPTOPnnrl_nomg;
cab190d4
JD
2070 if (Perl_isnan(left) || Perl_isnan(right))
2071 RETSETNO;
2072 SETs(boolSV(left < right));
2073#else
6f1401dc
DM
2074 dPOPnv_nomg;
2075 SETs(boolSV(SvNV_nomg(TOPs) < value));
cab190d4 2076#endif
a0d0e21e 2077 RETURN;
79072805 2078 }
a0d0e21e 2079}
79072805 2080
a0d0e21e
LW
2081PP(pp_gt)
2082{
6f1401dc 2083 dVAR; dSP;
a42d0242 2084 tryAMAGICbin_MG(gt_amg, AMGf_set|AMGf_numeric);
28e5dec8 2085#ifdef PERL_PRESERVE_IVUV
6f1401dc 2086 SvIV_please_nomg(TOPs);
28e5dec8 2087 if (SvIOK(TOPs)) {
6f1401dc 2088 SvIV_please_nomg(TOPm1s);
28e5dec8
JH
2089 if (SvIOK(TOPm1s)) {
2090 bool auvok = SvUOK(TOPm1s);
2091 bool buvok = SvUOK(TOPs);
a227d84d 2092
28e5dec8 2093 if (!auvok && !buvok) { /* ## IV > IV ## */
1b6737cc
AL
2094 const IV aiv = SvIVX(TOPm1s);
2095 const IV biv = SvIVX(TOPs);
2096
28e5dec8
JH
2097 SP--;
2098 SETs(boolSV(aiv > biv));
2099 RETURN;
2100 }
2101 if (auvok && buvok) { /* ## UV > UV ## */
1b6737cc
AL
2102 const UV auv = SvUVX(TOPm1s);
2103 const UV buv = SvUVX(TOPs);
28e5dec8
JH
2104
2105 SP--;
2106 SETs(boolSV(auv > buv));
2107 RETURN;
2108 }
2109 if (auvok) { /* ## UV > IV ## */
2110 UV auv;
1b6737cc
AL
2111 const IV biv = SvIVX(TOPs);
2112
28e5dec8
JH
2113 SP--;
2114 if (biv < 0) {
2115 /* As (a) is a UV, it's >=0, so it must be > */
2116 SETs(&PL_sv_yes);
2117 RETURN;
2118 }
2119 auv = SvUVX(TOPs);
28e5dec8
JH
2120 SETs(boolSV(auv > (UV)biv));
2121 RETURN;
2122 }
2123 { /* ## IV > UV ## */
1b6737cc 2124 const IV aiv = SvIVX(TOPm1s);
28e5dec8
JH
2125 UV buv;
2126
28e5dec8
JH
2127 if (aiv < 0) {
2128 /* As (b) is a UV, it's >=0, so it cannot be > */
2129 SP--;
2130 SETs(&PL_sv_no);
2131 RETURN;
2132 }
2133 buv = SvUVX(TOPs);
2134 SP--;
28e5dec8
JH
2135 SETs(boolSV((UV)aiv > buv));
2136 RETURN;
2137 }
2138 }
2139 }
2140#endif
30de85b6 2141#ifndef NV_PRESERVES_UV
50fb3111
NC
2142#ifdef PERL_PRESERVE_IVUV
2143 else
2144#endif
ed3b9b3c 2145 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
30de85b6
NC
2146 SP--;
2147 SETs(boolSV(SvRV(TOPs) > SvRV(TOPp1s)));
2148 RETURN;
2149 }
2150#endif
a0d0e21e 2151 {
cab190d4 2152#if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
6f1401dc 2153 dPOPTOPnnrl_nomg;
cab190d4
JD
2154 if (Perl_isnan(left) || Perl_isnan(right))
2155 RETSETNO;
2156 SETs(boolSV(left > right));
2157#else
6f1401dc
DM
2158 dPOPnv_nomg;
2159 SETs(boolSV(SvNV_nomg(TOPs) > value));
cab190d4 2160#endif
a0d0e21e 2161 RETURN;
79072805 2162 }
a0d0e21e
LW
2163}
2164
2165PP(pp_le)
2166{
6f1401dc 2167 dVAR; dSP;
a42d0242 2168 tryAMAGICbin_MG(le_amg, AMGf_set|AMGf_numeric);
28e5dec8 2169#ifdef PERL_PRESERVE_IVUV
6f1401dc 2170 SvIV_please_nomg(TOPs);
28e5dec8 2171 if (SvIOK(TOPs)) {
6f1401dc 2172 SvIV_please_nomg(TOPm1s);
28e5dec8
JH
2173 if (SvIOK(TOPm1s)) {
2174 bool auvok = SvUOK(TOPm1s);
2175 bool buvok = SvUOK(TOPs);
a227d84d 2176
28e5dec8 2177 if (!auvok && !buvok) { /* ## IV <= IV ## */
1b6737cc
AL
2178 const IV aiv = SvIVX(TOPm1s);
2179 const IV biv = SvIVX(TOPs);
28e5dec8
JH
2180
2181 SP--;
2182 SETs(boolSV(aiv <= biv));
2183 RETURN;
2184 }
2185 if (auvok && buvok) { /* ## UV <= UV ## */
2186 UV auv = SvUVX(TOPm1s);
2187 UV buv = SvUVX(TOPs);
2188
2189 SP--;
2190 SETs(boolSV(auv <= buv));
2191 RETURN;
2192 }
2193 if (auvok) { /* ## UV <= IV ## */
2194 UV auv;
1b6737cc
AL
2195 const IV biv = SvIVX(TOPs);
2196
28e5dec8
JH
2197 SP--;
2198 if (biv < 0) {
2199 /* As (a) is a UV, it's >=0, so a cannot be <= */
2200 SETs(&PL_sv_no);
2201 RETURN;
2202 }
2203 auv = SvUVX(TOPs);
28e5dec8
JH
2204 SETs(boolSV(auv <= (UV)biv));
2205 RETURN;
2206 }
2207 { /* ## IV <= UV ## */
1b6737cc 2208 const IV aiv = SvIVX(TOPm1s);
28e5dec8 2209 UV buv;
1b6737cc 2210
28e5dec8
JH
2211 if (aiv < 0) {
2212 /* As (b) is a UV, it's >=0, so a must be <= */
2213 SP--;
2214 SETs(&PL_sv_yes);
2215 RETURN;
2216 }
2217 buv = SvUVX(TOPs);
2218 SP--;
28e5dec8
JH
2219 SETs(boolSV((UV)aiv <= buv));
2220 RETURN;
2221 }
2222 }
2223 }
2224#endif
30de85b6 2225#ifndef NV_PRESERVES_UV
50fb3111
NC
2226#ifdef PERL_PRESERVE_IVUV
2227 else
2228#endif
ed3b9b3c 2229 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
30de85b6
NC
2230 SP--;
2231 SETs(boolSV(SvRV(TOPs) <= SvRV(TOPp1s)));
2232 RETURN;
2233 }
2234#endif
a0d0e21e 2235 {
cab190d4 2236#if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
6f1401dc 2237 dPOPTOPnnrl_nomg;
cab190d4
JD
2238 if (Perl_isnan(left) || Perl_isnan(right))
2239 RETSETNO;
2240 SETs(boolSV(left <= right));
2241#else
6f1401dc
DM
2242 dPOPnv_nomg;
2243 SETs(boolSV(SvNV_nomg(TOPs) <= value));
cab190d4 2244#endif
a0d0e21e 2245 RETURN;
79072805 2246 }
a0d0e21e
LW
2247}
2248
2249PP(pp_ge)
2250{
6f1401dc 2251 dVAR; dSP;
a42d0242 2252 tryAMAGICbin_MG(ge_amg,AMGf_set|AMGf_numeric);
28e5dec8 2253#ifdef PERL_PRESERVE_IVUV
6f1401dc 2254 SvIV_please_nomg(TOPs);
28e5dec8 2255 if (SvIOK(TOPs)) {
6f1401dc 2256 SvIV_please_nomg(TOPm1s);
28e5dec8
JH
2257 if (SvIOK(TOPm1s)) {
2258 bool auvok = SvUOK(TOPm1s);
2259 bool buvok = SvUOK(TOPs);
a227d84d 2260
28e5dec8 2261 if (!auvok && !buvok) { /* ## IV >= IV ## */
1b6737cc
AL
2262 const IV aiv = SvIVX(TOPm1s);
2263 const IV biv = SvIVX(TOPs);
2264
28e5dec8
JH
2265 SP--;
2266 SETs(boolSV(aiv >= biv));
2267 RETURN;
2268 }
2269 if (auvok && buvok) { /* ## UV >= UV ## */
1b6737cc
AL
2270 const UV auv = SvUVX(TOPm1s);
2271 const UV buv = SvUVX(TOPs);
2272
28e5dec8
JH
2273 SP--;
2274 SETs(boolSV(auv >= buv));
2275 RETURN;
2276 }
2277 if (auvok) { /* ## UV >= IV ## */
2278 UV auv;
1b6737cc
AL
2279 const IV biv = SvIVX(TOPs);
2280
28e5dec8
JH
2281 SP--;
2282 if (biv < 0) {
2283 /* As (a) is a UV, it's >=0, so it must be >= */
2284 SETs(&PL_sv_yes);
2285 RETURN;
2286 }
2287 auv = SvUVX(TOPs);
28e5dec8
JH
2288 SETs(boolSV(auv >= (UV)biv));
2289 RETURN;
2290 }
2291 { /* ## IV >= UV ## */
1b6737cc 2292 const IV aiv = SvIVX(TOPm1s);
28e5dec8 2293 UV buv;
1b6737cc 2294
28e5dec8
JH
2295 if (aiv < 0) {
2296 /* As (b) is a UV, it's >=0, so a cannot be >= */
2297 SP--;
2298 SETs(&PL_sv_no);
2299 RETURN;
2300 }
2301 buv = SvUVX(TOPs);
2302 SP--;
28e5dec8
JH
2303 SETs(boolSV((UV)aiv >= buv));
2304 RETURN;
2305 }
2306 }
2307 }
2308#endif
30de85b6 2309#ifndef NV_PRESERVES_UV
50fb3111
NC
2310#ifdef PERL_PRESERVE_IVUV
2311 else
2312#endif
ed3b9b3c 2313 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
30de85b6
NC
2314 SP--;
2315 SETs(boolSV(SvRV(TOPs) >= SvRV(TOPp1s)));
2316 RETURN;
2317 }
2318#endif
a0d0e21e 2319 {
cab190d4 2320#if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
6f1401dc 2321 dPOPTOPnnrl_nomg;
cab190d4
JD
2322 if (Perl_isnan(left) || Perl_isnan(right))
2323 RETSETNO;
2324 SETs(boolSV(left >= right));
2325#else
6f1401dc
DM
2326 dPOPnv_nomg;
2327 SETs(boolSV(SvNV_nomg(TOPs) >= value));
cab190d4 2328#endif
a0d0e21e 2329 RETURN;
79072805 2330 }
a0d0e21e 2331}
79072805 2332
a0d0e21e
LW
2333PP(pp_ne)
2334{
6f1401dc 2335 dVAR; dSP;
a42d0242 2336 tryAMAGICbin_MG(ne_amg,AMGf_set|AMGf_numeric);
3bb2c415 2337#ifndef NV_PRESERVES_UV
ed3b9b3c 2338 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
e61d22ef
NC
2339 SP--;
2340 SETs(boolSV(SvRV(TOPs) != SvRV(TOPp1s)));
3bb2c415
JH
2341 RETURN;
2342 }
2343#endif
28e5dec8 2344#ifdef PERL_PRESERVE_IVUV
6f1401dc 2345 SvIV_please_nomg(TOPs);
28e5dec8 2346 if (SvIOK(TOPs)) {
6f1401dc 2347 SvIV_please_nomg(TOPm1s);
28e5dec8 2348 if (SvIOK(TOPm1s)) {
0bd48802
AL
2349 const bool auvok = SvUOK(TOPm1s);
2350 const bool buvok = SvUOK(TOPs);
a227d84d 2351
30de85b6
NC
2352 if (auvok == buvok) { /* ## IV == IV or UV == UV ## */
2353 /* Casting IV to UV before comparison isn't going to matter
2354 on 2s complement. On 1s complement or sign&magnitude
2355 (if we have any of them) it could make negative zero
2356 differ from normal zero. As I understand it. (Need to
2357 check - is negative zero implementation defined behaviour
2358 anyway?). NWC */
1b6737cc
AL
2359 const UV buv = SvUVX(POPs);
2360 const UV auv = SvUVX(TOPs);
2361
28e5dec8
JH
2362 SETs(boolSV(auv != buv));
2363 RETURN;
2364 }
2365 { /* ## Mixed IV,UV ## */
2366 IV iv;
2367 UV uv;
2368
2369 /* != is commutative so swap if needed (save code) */
2370 if (auvok) {
2371 /* swap. top of stack (b) is the iv */
2372 iv = SvIVX(TOPs);
2373 SP--;
2374 if (iv < 0) {
2375 /* As (a) is a UV, it's >0, so it cannot be == */
2376 SETs(&PL_sv_yes);
2377 RETURN;
2378 }
2379 uv = SvUVX(TOPs);
2380 } else {
2381 iv = SvIVX(TOPm1s);
2382 SP--;
2383 if (iv < 0) {
2384 /* As (b) is a UV, it's >0, so it cannot be == */
2385 SETs(&PL_sv_yes);
2386 RETURN;
2387 }
2388 uv = SvUVX(*(SP+1)); /* Do I want TOPp1s() ? */
2389 }
28e5dec8
JH
2390 SETs(boolSV((UV)iv != uv));
2391 RETURN;
2392 }
2393 }
2394 }
2395#endif
a0d0e21e 2396 {
cab190d4 2397#if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
6f1401dc 2398 dPOPTOPnnrl_nomg;
cab190d4
JD
2399 if (Perl_isnan(left) || Perl_isnan(right))
2400 RETSETYES;
2401 SETs(boolSV(left != right));
2402#else
6f1401dc
DM
2403 dPOPnv_nomg;
2404 SETs(boolSV(SvNV_nomg(TOPs) != value));
cab190d4 2405#endif
a0d0e21e
LW
2406 RETURN;
2407 }
79072805
LW
2408}
2409
a0d0e21e 2410PP(pp_ncmp)
79072805 2411{
6f1401dc 2412 dVAR; dSP; dTARGET;
a42d0242 2413 tryAMAGICbin_MG(ncmp_amg, AMGf_numeric);
d8c7644e 2414#ifndef NV_PRESERVES_UV
ed3b9b3c 2415 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
0bd48802
AL
2416 const UV right = PTR2UV(SvRV(POPs));
2417 const UV left = PTR2UV(SvRV(TOPs));
e61d22ef 2418 SETi((left > right) - (left < right));
d8c7644e
JH
2419 RETURN;
2420 }
2421#endif
28e5dec8
JH
2422#ifdef PERL_PRESERVE_IVUV
2423 /* Fortunately it seems NaN isn't IOK */
6f1401dc 2424 SvIV_please_nomg(TOPs);
28e5dec8 2425 if (SvIOK(TOPs)) {
6f1401dc 2426 SvIV_please_nomg(TOPm1s);
28e5dec8 2427 if (SvIOK(TOPm1s)) {
1b6737cc
AL
2428 const bool leftuvok = SvUOK(TOPm1s);
2429 const bool rightuvok = SvUOK(TOPs);
28e5dec8
JH
2430 I32 value;
2431 if (!leftuvok && !rightuvok) { /* ## IV <=> IV ## */
1b6737cc
AL
2432 const IV leftiv = SvIVX(TOPm1s);
2433 const IV rightiv = SvIVX(TOPs);
28e5dec8
JH
2434
2435 if (leftiv > rightiv)
2436 value = 1;
2437 else if (leftiv < rightiv)
2438 value = -1;
2439 else
2440 value = 0;
2441 } else if (leftuvok && rightuvok) { /* ## UV <=> UV ## */
1b6737cc
AL
2442 const UV leftuv = SvUVX(TOPm1s);
2443 const UV rightuv = SvUVX(TOPs);
28e5dec8
JH
2444
2445 if (leftuv > rightuv)
2446 value = 1;
2447 else if (leftuv < rightuv)
2448 value = -1;
2449 else
2450 value = 0;
2451 } else if (leftuvok) { /* ## UV <=> IV ## */
1b6737cc 2452 const IV rightiv = SvIVX(TOPs);
28e5dec8
JH
2453 if (rightiv < 0) {
2454 /* As (a) is a UV, it's >=0, so it cannot be < */
2455 value = 1;
2456 } else {
1b6737cc 2457 const UV leftuv = SvUVX(TOPm1s);
83bac5dd 2458 if (leftuv > (UV)rightiv) {
28e5dec8
JH
2459 value = 1;
2460 } else if (leftuv < (UV)rightiv) {
2461 value = -1;
2462 } else {
2463 value = 0;
2464 }
2465 }
2466 } else { /* ## IV <=> UV ## */
1b6737cc 2467 const IV leftiv = SvIVX(TOPm1s);
28e5dec8
JH
2468 if (leftiv < 0) {
2469 /* As (b) is a UV, it's >=0, so it must be < */
2470 value = -1;
2471 } else {
1b6737cc 2472 const UV rightuv = SvUVX(TOPs);
83bac5dd 2473 if ((UV)leftiv > rightuv) {
28e5dec8 2474 value = 1;
83bac5dd 2475 } else if ((UV)leftiv < rightuv) {
28e5dec8
JH
2476 value = -1;
2477 } else {
2478 value = 0;
2479 }
2480 }
2481 }
2482 SP--;
2483 SETi(value);
2484 RETURN;
2485 }
2486 }
2487#endif
a0d0e21e 2488 {
6f1401dc 2489 dPOPTOPnnrl_nomg;
a0d0e21e 2490 I32 value;
79072805 2491
a3540c92 2492#ifdef Perl_isnan
1ad04cfd
JH
2493 if (Perl_isnan(left) || Perl_isnan(right)) {
2494 SETs(&PL_sv_undef);
2495 RETURN;
2496 }
2497 value = (left > right) - (left < right);
2498#else
ff0cee69 2499 if (left == right)
a0d0e21e 2500 value = 0;
a0d0e21e
LW
2501 else if (left < right)
2502 value = -1;
44a8e56a 2503 else if (left > right)
2504 value = 1;
2505 else {
3280af22 2506 SETs(&PL_sv_undef);
44a8e56a 2507 RETURN;
2508 }
1ad04cfd 2509#endif
a0d0e21e
LW
2510 SETi(value);
2511 RETURN;
79072805 2512 }
a0d0e21e 2513}
79072805 2514
afd9910b 2515PP(pp_sle)
a0d0e21e 2516{
97aff369 2517 dVAR; dSP;
79072805 2518
afd9910b
NC
2519 int amg_type = sle_amg;
2520 int multiplier = 1;
2521 int rhs = 1;
79072805 2522
afd9910b
NC
2523 switch (PL_op->op_type) {
2524 case OP_SLT:
2525 amg_type = slt_amg;
2526 /* cmp < 0 */
2527 rhs = 0;
2528 break;
2529 case OP_SGT:
2530 amg_type = sgt_amg;
2531 /* cmp > 0 */
2532 multiplier = -1;
2533 rhs = 0;
2534 break;
2535 case OP_SGE:
2536 amg_type = sge_amg;
2537 /* cmp >= 0 */
2538 multiplier = -1;
2539 break;
79072805 2540 }
79072805 2541
6f1401dc 2542 tryAMAGICbin_MG(amg_type, AMGf_set);
a0d0e21e
LW
2543 {
2544 dPOPTOPssrl;
1b6737cc 2545 const int cmp = (IN_LOCALE_RUNTIME
078504b2
FC
2546 ? sv_cmp_locale_flags(left, right, 0)
2547 : sv_cmp_flags(left, right, 0));
afd9910b 2548 SETs(boolSV(cmp * multiplier < rhs));
a0d0e21e
LW
2549 RETURN;
2550 }
2551}
79072805 2552
36477c24 2553PP(pp_seq)
2554{
6f1401dc
DM
2555 dVAR; dSP;
2556 tryAMAGICbin_MG(seq_amg, AMGf_set);
36477c24 2557 {
2558 dPOPTOPssrl;
078504b2 2559 SETs(boolSV(sv_eq_flags(left, right, 0)));
a0d0e21e
LW
2560 RETURN;
2561 }
2562}
79072805 2563
a0d0e21e 2564PP(pp_sne)
79072805 2565{
6f1401dc
DM
2566 dVAR; dSP;
2567 tryAMAGICbin_MG(sne_amg, AMGf_set);
a0d0e21e
LW
2568 {
2569 dPOPTOPssrl;
078504b2 2570 SETs(boolSV(!sv_eq_flags(left, right, 0)));
a0d0e21e 2571 RETURN;
463ee0b2 2572 }
79072805
LW
2573}
2574
a0d0e21e 2575PP(pp_scmp)
79072805 2576{
6f1401dc
DM
2577 dVAR; dSP; dTARGET;
2578 tryAMAGICbin_MG(scmp_amg, 0);
a0d0e21e
LW
2579 {
2580 dPOPTOPssrl;
1b6737cc 2581 const int cmp = (IN_LOCALE_RUNTIME
078504b2
FC
2582 ? sv_cmp_locale_flags(left, right, 0)
2583 : sv_cmp_flags(left, right, 0));
bbce6d69 2584 SETi( cmp );
a0d0e21e
LW
2585 RETURN;
2586 }
2587}
79072805 2588
55497cff 2589PP(pp_bit_and)
2590{
6f1401dc
DM
2591 dVAR; dSP; dATARGET;
2592 tryAMAGICbin_MG(band_amg, AMGf_assign);
a0d0e21e
LW
2593 {
2594 dPOPTOPssrl;
4633a7c4 2595 if (SvNIOKp(left) || SvNIOKp(right)) {
b20c4ee1
FC
2596 const bool left_ro_nonnum = !SvNIOKp(left) && SvREADONLY(left);
2597 const bool right_ro_nonnum = !SvNIOKp(right) && SvREADONLY(right);
d0ba1bd2 2598 if (PL_op->op_private & HINT_INTEGER) {
1b6737cc 2599 const IV i = SvIV_nomg(left) & SvIV_nomg(right);
972b05a9 2600 SETi(i);
d0ba1bd2
JH
2601 }
2602 else {
1b6737cc 2603 const UV u = SvUV_nomg(left) & SvUV_nomg(right);
972b05a9 2604 SETu(u);
d0ba1bd2 2605 }
b20c4ee1
FC
2606 if (left_ro_nonnum) SvNIOK_off(left);
2607 if (right_ro_nonnum) SvNIOK_off(right);
a0d0e21e
LW
2608 }
2609 else {
533c011a 2610 do_vop(PL_op->op_type, TARG, left, right);
a0d0e21e
LW
2611 SETTARG;
2612 }
2613 RETURN;
2614 }
2615}
79072805 2616
a0d0e21e
LW
2617PP(pp_bit_or)
2618{
3658c1f1
NC
2619 dVAR; dSP; dATARGET;
2620 const int op_type = PL_op->op_type;
2621
6f1401dc 2622 tryAMAGICbin_MG((op_type == OP_BIT_OR ? bor_amg : bxor_amg), AMGf_assign);
a0d0e21e
LW
2623 {
2624 dPOPTOPssrl;
4633a7c4 2625 if (SvNIOKp(left) || SvNIOKp(right)) {
b20c4ee1
FC
2626 const bool left_ro_nonnum = !SvNIOKp(left) && SvREADONLY(left);
2627 const bool right_ro_nonnum = !SvNIOKp(right) && SvREADONLY(right);
d0ba1bd2 2628 if (PL_op->op_private & HINT_INTEGER) {
3658c1f1
NC
2629 const IV l = (USE_LEFT(left) ? SvIV_nomg(left) : 0);
2630 const IV r = SvIV_nomg(right);
2631 const IV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2632 SETi(result);
d0ba1bd2
JH
2633 }
2634 else {
3658c1f1
NC
2635 const UV l = (USE_LEFT(left) ? SvUV_nomg(left) : 0);
2636 const UV r = SvUV_nomg(right);
2637 const UV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2638 SETu(result);
d0ba1bd2 2639 }
b20c4ee1
FC
2640 if (left_ro_nonnum) SvNIOK_off(left);
2641 if (right_ro_nonnum) SvNIOK_off(right);
a0d0e21e
LW
2642 }
2643 else {
3658c1f1 2644 do_vop(op_type, TARG, left, right);
a0d0e21e
LW
2645 SETTARG;
2646 }
2647 RETURN;
79072805 2648 }
a0d0e21e 2649}
79072805 2650
a0d0e21e
LW
2651PP(pp_negate)
2652{
6f1401dc
DM
2653 dVAR; dSP; dTARGET;
2654 tryAMAGICun_MG(neg_amg, AMGf_numeric);
a0d0e21e 2655 {
6f1401dc 2656 SV * const sv = TOPs;
1b6737cc 2657 const int flags = SvFLAGS(sv);
a5b92898 2658
886a4465 2659 if( !SvNIOK( sv ) && looks_like_number( sv ) ){
a5b92898
RB
2660 SvIV_please( sv );
2661 }
2662
28e5dec8
JH
2663 if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
2664 /* It's publicly an integer, or privately an integer-not-float */
2665 oops_its_an_int:
9b0e499b
GS
2666 if (SvIsUV(sv)) {
2667 if (SvIVX(sv) == IV_MIN) {
28e5dec8 2668 /* 2s complement assumption. */
9b0e499b
GS
2669 SETi(SvIVX(sv)); /* special case: -((UV)IV_MAX+1) == IV_MIN */
2670 RETURN;
2671 }
2672 else if (SvUVX(sv) <= IV_MAX) {
beccb14c 2673 SETi(-SvIVX(sv));
9b0e499b
GS
2674 RETURN;
2675 }
2676 }
2677 else if (SvIVX(sv) != IV_MIN) {
2678 SETi(-SvIVX(sv));
2679 RETURN;
2680 }
28e5dec8
JH
2681#ifdef PERL_PRESERVE_IVUV
2682 else {
2683 SETu((UV)IV_MIN);
2684 RETURN;
2685 }
2686#endif
9b0e499b
GS
2687 }
2688 if (SvNIOKp(sv))
6f1401dc 2689 SETn(-SvNV_nomg(sv));
4633a7c4 2690 else if (SvPOKp(sv)) {
a0d0e21e 2691 STRLEN len;
6f1401dc 2692 const char * const s = SvPV_nomg_const(sv, len);
bbce6d69 2693 if (isIDFIRST(*s)) {
76f68e9b 2694 sv_setpvs(TARG, "-");
a0d0e21e 2695 sv_catsv(TARG, sv);
79072805 2696 }
a0d0e21e 2697 else if (*s == '+' || *s == '-') {
6f1401dc
DM
2698 sv_setsv_nomg(TARG, sv);
2699 *SvPV_force_nomg(TARG, len) = *s == '-' ? '+' : '-';
79072805 2700 }
8eb28a70 2701 else if (DO_UTF8(sv)) {
6f1401dc 2702 SvIV_please_nomg(sv);
8eb28a70
JH
2703 if (SvIOK(sv))
2704 goto oops_its_an_int;
2705 if (SvNOK(sv))
6f1401dc 2706 sv_setnv(TARG, -SvNV_nomg(sv));
8eb28a70 2707 else {
76f68e9b 2708 sv_setpvs(TARG, "-");
8eb28a70
JH
2709 sv_catsv(TARG, sv);
2710 }
834a4ddd 2711 }
28e5dec8 2712 else {
6f1401dc 2713 SvIV_please_nomg(sv);
8eb28a70
JH
2714 if (SvIOK(sv))
2715 goto oops_its_an_int;
6f1401dc 2716 sv_setnv(TARG, -SvNV_nomg(sv));
28e5dec8 2717 }
a0d0e21e 2718 SETTARG;
79072805 2719 }
4633a7c4 2720 else
6f1401dc 2721 SETn(-SvNV_nomg(sv));
79072805 2722 }
a0d0e21e 2723 RETURN;
79072805
LW
2724}
2725
a0d0e21e 2726PP(pp_not)
79072805 2727{
6f1401dc
DM
2728 dVAR; dSP;
2729 tryAMAGICun_MG(not_amg, AMGf_set);
06c841cf 2730 *PL_stack_sp = boolSV(!SvTRUE_nomg(*PL_stack_sp));
a0d0e21e 2731 return NORMAL;
79072805
LW
2732}
2733
a0d0e21e 2734PP(pp_complement)
79072805 2735{
6f1401dc 2736 dVAR; dSP; dTARGET;
a42d0242 2737 tryAMAGICun_MG(compl_amg, AMGf_numeric);
a0d0e21e
LW
2738 {
2739 dTOPss;
4633a7c4 2740 if (SvNIOKp(sv)) {
d0ba1bd2 2741 if (PL_op->op_private & HINT_INTEGER) {
1b6737cc 2742 const IV i = ~SvIV_nomg(sv);
972b05a9 2743 SETi(i);
d0ba1bd2
JH
2744 }
2745 else {
1b6737cc 2746 const UV u = ~SvUV_nomg(sv);
972b05a9 2747 SETu(u);
d0ba1bd2 2748 }
a0d0e21e
LW
2749 }
2750 else {
51723571 2751 register U8 *tmps;
55497cff 2752 register I32 anum;
a0d0e21e
LW
2753 STRLEN len;
2754
10516c54 2755 (void)SvPV_nomg_const(sv,len); /* force check for uninit var */
891f9566 2756 sv_setsv_nomg(TARG, sv);
6f1401dc 2757 tmps = (U8*)SvPV_force_nomg(TARG, len);
a0d0e21e 2758 anum = len;
1d68d6cd 2759 if (SvUTF8(TARG)) {
a1ca4561 2760 /* Calculate exact length, let's not estimate. */
1d68d6cd 2761 STRLEN targlen = 0;
ba210ebe 2762 STRLEN l;
a1ca4561
YST
2763 UV nchar = 0;
2764 UV nwide = 0;
01f6e806 2765 U8 * const send = tmps + len;
74d49cd0
TS
2766 U8 * const origtmps = tmps;
2767 const UV utf8flags = UTF8_ALLOW_ANYUV;
1d68d6cd 2768
1d68d6cd 2769 while (tmps < send) {
74d49cd0
TS
2770 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2771 tmps += l;
5bbb0b5a 2772 targlen += UNISKIP(~c);
a1ca4561
YST
2773 nchar++;
2774 if (c > 0xff)
2775 nwide++;
1d68d6cd
SC
2776 }
2777
2778 /* Now rewind strings and write them. */
74d49cd0 2779 tmps = origtmps;
a1ca4561
YST
2780
2781 if (nwide) {
01f6e806
AL
2782 U8 *result;
2783 U8 *p;
2784
74d49cd0 2785 Newx(result, targlen + 1, U8);
01f6e806 2786 p = result;
a1ca4561 2787 while (tmps < send) {
74d49cd0
TS
2788 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2789 tmps += l;
01f6e806 2790 p = uvchr_to_utf8_flags(p, ~c, UNICODE_ALLOW_ANY);
a1ca4561 2791 }
01f6e806 2792 *p = '\0';
c1c21316
NC
2793 sv_usepvn_flags(TARG, (char*)result, targlen,
2794 SV_HAS_TRAILING_NUL);
a1ca4561
YST
2795 SvUTF8_on(TARG);
2796 }
2797 else {
01f6e806
AL
2798 U8 *result;
2799 U8 *p;
2800
74d49cd0 2801 Newx(result, nchar + 1, U8);
01f6e806 2802 p = result;
a1ca4561 2803 while (tmps < send) {
74d49cd0
TS
2804 const U8 c = (U8)utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2805 tmps += l;
01f6e806 2806 *p++ = ~c;
a1ca4561 2807 }
01f6e806 2808 *p = '\0';
c1c21316 2809 sv_usepvn_flags(TARG, (char*)result, nchar, SV_HAS_TRAILING_NUL);
d0a21e00 2810 SvUTF8_off(TARG);
1d68d6cd 2811 }
ec93b65f 2812 SETTARG;
1d68d6cd
SC
2813 RETURN;
2814 }
a0d0e21e 2815#ifdef LIBERAL
51723571
JH
2816 {
2817 register long *tmpl;
2818 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
2819 *tmps = ~*tmps;
2820 tmpl = (long*)tmps;
bb7a0f54 2821 for ( ; anum >= (I32)sizeof(long); anum -= (I32)sizeof(long), tmpl++)
51723571
JH
2822 *tmpl = ~*tmpl;
2823 tmps = (U8*)tmpl;
2824 }
a0d0e21e
LW
2825#endif
2826 for ( ; anum > 0; anum--, tmps++)
2827 *tmps = ~*tmps;
ec93b65f 2828 SETTARG;
a0d0e21e
LW
2829 }
2830 RETURN;
2831 }
79072805
LW
2832}
2833
a0d0e21e
LW
2834/* integer versions of some of the above */
2835
a0d0e21e 2836PP(pp_i_multiply)
79072805 2837{
6f1401dc
DM
2838 dVAR; dSP; dATARGET;
2839 tryAMAGICbin_MG(mult_amg, AMGf_assign);
a0d0e21e 2840 {
6f1401dc 2841 dPOPTOPiirl_nomg;
a0d0e21e
LW
2842 SETi( left * right );
2843 RETURN;
2844 }
79072805
LW
2845}
2846
a0d0e21e 2847PP(pp_i_divide)
79072805 2848{
ece1bcef 2849 IV num;
6f1401dc
DM
2850 dVAR; dSP; dATARGET;
2851 tryAMAGICbin_MG(div_amg, AMGf_assign);
a0d0e21e 2852 {
6f1401dc
DM
2853 dPOPTOPssrl;
2854 IV value = SvIV_nomg(right);
a0d0e21e 2855 if (value == 0)
ece1bcef 2856 DIE(aTHX_ "Illegal division by zero");
6f1401dc 2857 num = SvIV_nomg(left);
a0cec769
YST
2858
2859 /* avoid FPE_INTOVF on some platforms when num is IV_MIN */
2860 if (value == -1)
2861 value = - num;
2862 else
2863 value = num / value;
6f1401dc 2864 SETi(value);
a0d0e21e
LW
2865 RETURN;
2866 }
79072805
LW
2867}
2868
befad5d1 2869#if defined(__GLIBC__) && IVSIZE == 8
224ec323
JH
2870STATIC
2871PP(pp_i_modulo_0)
befad5d1
NC
2872#else
2873PP(pp_i_modulo)
2874#endif
224ec323
JH
2875{
2876 /* This is the vanilla old i_modulo. */
6f1401dc
DM
2877 dVAR; dSP; dATARGET;
2878 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
224ec323 2879 {
6f1401dc 2880 dPOPTOPiirl_nomg;
224ec323
JH
2881 if (!right)
2882 DIE(aTHX_ "Illegal modulus zero");
a0cec769
YST
2883 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2884 if (right == -1)
2885 SETi( 0 );
2886 else
2887 SETi( left % right );
224ec323
JH
2888 RETURN;
2889 }
2890}
2891
11010fa3 2892#if defined(__GLIBC__) && IVSIZE == 8
224ec323
JH
2893STATIC
2894PP(pp_i_modulo_1)
befad5d1 2895
224ec323 2896{
224ec323 2897 /* This is the i_modulo with the workaround for the _moddi3 bug
fce2b89e 2898 * in (at least) glibc 2.2.5 (the PERL_ABS() the workaround).
224ec323 2899 * See below for pp_i_modulo. */
6f1401dc
DM
2900 dVAR; dSP; dATARGET;
2901 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
224ec323 2902 {
6f1401dc 2903 dPOPTOPiirl_nomg;
224ec323
JH
2904 if (!right)
2905 DIE(aTHX_ "Illegal modulus zero");
a0cec769
YST
2906 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2907 if (right == -1)
2908 SETi( 0 );
2909 else
2910 SETi( left % PERL_ABS(right) );
224ec323
JH
2911 RETURN;
2912 }
224ec323
JH
2913}
2914
a0d0e21e 2915PP(pp_i_modulo)
79072805 2916{
6f1401dc
DM
2917 dVAR; dSP; dATARGET;
2918 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
224ec323 2919 {
6f1401dc 2920 dPOPTOPiirl_nomg;
224ec323
JH
2921 if (!right)
2922 DIE(aTHX_ "Illegal modulus zero");
2923 /* The assumption is to use hereafter the old vanilla version... */
2924 PL_op->op_ppaddr =
2925 PL_ppaddr[OP_I_MODULO] =
1c127fab 2926 Perl_pp_i_modulo_0;
224ec323
JH
2927 /* .. but if we have glibc, we might have a buggy _moddi3
2928 * (at least glicb 2.2.5 is known to have this bug), in other
2929 * words our integer modulus with negative quad as the second
2930 * argument might be broken. Test for this and re-patch the
2931 * opcode dispatch table if that is the case, remembering to
2932 * also apply the workaround so that this first round works
2933 * right, too. See [perl #9402] for more information. */
224ec323
JH
2934 {
2935 IV l = 3;
2936 IV r = -10;
2937 /* Cannot do this check with inlined IV constants since
2938 * that seems to work correctly even with the buggy glibc. */
2939 if (l % r == -3) {
2940 /* Yikes, we have the bug.
2941 * Patch in the workaround version. */
2942 PL_op->op_ppaddr =
2943 PL_ppaddr[OP_I_MODULO] =
2944 &Perl_pp_i_modulo_1;
2945 /* Make certain we work right this time, too. */
32fdb065 2946 right = PERL_ABS(right);
224ec323
JH
2947 }
2948 }
a0cec769
YST
2949 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2950 if (right == -1)
2951 SETi( 0 );
2952 else
2953 SETi( left % right );
224ec323
JH
2954 RETURN;
2955 }
79072805 2956}
befad5d1 2957#endif
79072805 2958
a0d0e21e 2959PP(pp_i_add)
79072805 2960{
6f1401dc
DM
2961 dVAR; dSP; dATARGET;
2962 tryAMAGICbin_MG(add_amg, AMGf_assign);
a0d0e21e 2963 {
6f1401dc 2964 dPOPTOPiirl_ul_nomg;
a0d0e21e
LW
2965 SETi( left + right );
2966 RETURN;
79072805 2967 }
79072805
LW
2968}
2969
a0d0e21e 2970PP(pp_i_subtract)
79072805 2971{
6f1401dc
DM
2972 dVAR; dSP; dATARGET;
2973 tryAMAGICbin_MG(subtr_amg, AMGf_assign);
a0d0e21e 2974 {
6f1401dc 2975 dPOPTOPiirl_ul_nomg;
a0d0e21e
LW
2976 SETi( left - right );
2977 RETURN;
79072805 2978 }
79072805
LW
2979}
2980
a0d0e21e 2981PP(pp_i_lt)
79072805 2982{
6f1401dc
DM
2983 dVAR; dSP;
2984 tryAMAGICbin_MG(lt_amg, AMGf_set);
a0d0e21e 2985 {
6f1401dc 2986 dPOPTOPiirl_nomg;
54310121 2987 SETs(boolSV(left < right));
a0d0e21e
LW
2988 RETURN;
2989 }
79072805
LW
2990}
2991
a0d0e21e 2992PP(pp_i_gt)
79072805 2993{
6f1401dc
DM
2994 dVAR; dSP;
2995 tryAMAGICbin_MG(gt_amg, AMGf_set);
a0d0e21e 2996 {
6f1401dc 2997 dPOPTOPiirl_nomg;
54310121 2998 SETs(boolSV(left > right));
a0d0e21e
LW
2999 RETURN;
3000 }
79072805
LW
3001}
3002
a0d0e21e 3003PP(pp_i_le)
79072805 3004{
6f1401dc
DM
3005 dVAR; dSP;
3006 tryAMAGICbin_MG(le_amg, AMGf_set);
a0d0e21e 3007 {
6f1401dc 3008 dPOPTOPiirl_nomg;
54310121 3009 SETs(boolSV(left <= right));
a0d0e21e 3010 RETURN;
85e6fe83 3011 }
79072805
LW
3012}
3013
a0d0e21e 3014PP(pp_i_ge)
79072805 3015{
6f1401dc
DM
3016 dVAR; dSP;
3017 tryAMAGICbin_MG(ge_amg, AMGf_set);
a0d0e21e 3018 {
6f1401dc 3019 dPOPTOPiirl_nomg;
54310121 3020 SETs(boolSV(left >= right));
a0d0e21e
LW
3021 RETURN;
3022 }
79072805
LW
3023}
3024
a0d0e21e 3025PP(pp_i_eq)
79072805 3026{
6f1401dc
DM
3027 dVAR; dSP;
3028 tryAMAGICbin_MG(eq_amg, AMGf_set);
a0d0e21e 3029 {
6f1401dc 3030 dPOPTOPiirl_nomg;
54310121 3031 SETs(boolSV(left == right));
a0d0e21e
LW
3032 RETURN;
3033 }
79072805
LW
3034}
3035
a0d0e21e 3036PP(pp_i_ne)
79072805 3037{
6f1401dc
DM
3038 dVAR; dSP;
3039 tryAMAGICbin_MG(ne_amg, AMGf_set);
a0d0e21e 3040 {
6f1401dc 3041 dPOPTOPiirl_nomg;
54310121 3042 SETs(boolSV(left != right));
a0d0e21e
LW
3043 RETURN;
3044 }
79072805
LW
3045}
3046
a0d0e21e 3047PP(pp_i_ncmp)
79072805 3048{
6f1401dc
DM
3049 dVAR; dSP; dTARGET;
3050 tryAMAGICbin_MG(ncmp_amg, 0);
a0d0e21e 3051 {
6f1401dc 3052 dPOPTOPiirl_nomg;
a0d0e21e 3053 I32 value;
79072805 3054
a0d0e21e 3055 if (left > right)
79072805 3056 value = 1;
a0d0e21e 3057 else if (left < right)
79072805 3058 value = -1;
a0d0e21e 3059 else
79072805 3060 value = 0;
a0d0e21e
LW
3061 SETi(value);
3062 RETURN;
79072805 3063 }
85e6fe83
LW
3064}
3065
3066PP(pp_i_negate)
3067{
6f1401dc
DM
3068 dVAR; dSP; dTARGET;
3069 tryAMAGICun_MG(neg_amg, 0);
3070 {
3071 SV * const sv = TOPs;
3072 IV const i = SvIV_nomg(sv);
3073 SETi(-i);
3074 RETURN;
3075 }
85e6fe83
LW
3076}
3077
79072805
LW
3078/* High falutin' math. */
3079
3080PP(pp_atan2)
3081{
6f1401dc
DM
3082 dVAR; dSP; dTARGET;
3083 tryAMAGICbin_MG(atan2_amg, 0);
a0d0e21e 3084 {
6f1401dc 3085 dPOPTOPnnrl_nomg;
a1021d57 3086 SETn(Perl_atan2(left, right));
a0d0e21e
LW
3087 RETURN;
3088 }
79072805
LW
3089}
3090
3091PP(pp_sin)
3092{
71302fe3
NC
3093 dVAR; dSP; dTARGET;
3094 int amg_type = sin_amg;
3095 const char *neg_report = NULL;
bc81784a 3096 NV (*func)(NV) = Perl_sin;
71302fe3
NC
3097 const int op_type = PL_op->op_type;
3098
3099 switch (op_type) {
3100 case OP_COS:
3101 amg_type = cos_amg;
bc81784a 3102 func = Perl_cos;
71302fe3
NC
3103 break;
3104 case OP_EXP:
3105 amg_type = exp_amg;
bc81784a 3106 func = Perl_exp;
71302fe3
NC
3107 break;
3108 case OP_LOG:
3109 amg_type = log_amg;
bc81784a 3110 func = Perl_log;
71302fe3
NC
3111 neg_report = "log";
3112 break;
3113 case OP_SQRT:
3114 amg_type = sqrt_amg;
bc81784a 3115 func = Perl_sqrt;
71302fe3
NC
3116 neg_report = "sqrt";
3117 break;
a0d0e21e 3118 }
79072805 3119
6f1401dc
DM
3120
3121 tryAMAGICun_MG(amg_type, 0);
a0d0e21e 3122 {
6f1401dc
DM
3123 SV * const arg = POPs;
3124 const NV value = SvNV_nomg(arg);
71302fe3
NC
3125 if (neg_report) {
3126 if (op_type == OP_LOG ? (value <= 0.0) : (value < 0.0)) {
3127 SET_NUMERIC_STANDARD();
3128 DIE(aTHX_ "Can't take %s of %"NVgf, neg_report, value);
3129 }
3130 }
3131 XPUSHn(func(value));
a0d0e21e
LW
3132 RETURN;
3133 }
79072805
LW
3134}
3135
56cb0a1c
AD
3136/* Support Configure command-line overrides for rand() functions.
3137 After 5.005, perhaps we should replace this by Configure support
3138 for drand48(), random(), or rand(). For 5.005, though, maintain
3139 compatibility by calling rand() but allow the user to override it.
3140 See INSTALL for details. --Andy Dougherty 15 July 1998
3141*/
85ab1d1d
JH
3142/* Now it's after 5.005, and Configure supports drand48() and random(),
3143 in addition to rand(). So the overrides should not be needed any more.
3144 --Jarkko Hietaniemi 27 September 1998
3145 */
3146
3147#ifndef HAS_DRAND48_PROTO
20ce7b12 3148extern double drand48 (void);
56cb0a1c
AD
3149#endif
3150
79072805
LW
3151PP(pp_rand)
3152{
97aff369 3153 dVAR; dSP; dTARGET;
65202027 3154 NV value;
79072805
LW
3155 if (MAXARG < 1)
3156 value = 1.0;
3157 else
3158 value = POPn;
3159 if (value == 0.0)
3160 value = 1.0;
80252599 3161 if (!PL_srand_called) {
85ab1d1d 3162 (void)seedDrand01((Rand_seed_t)seed());
80252599 3163 PL_srand_called = TRUE;
93dc8474 3164 }
85ab1d1d 3165 value *= Drand01();
79072805
LW
3166 XPUSHn(value);
3167 RETURN;
3168}
3169
3170PP(pp_srand)
3171{
83832992 3172 dVAR; dSP; dTARGET;
0bd48802 3173 const UV anum = (MAXARG < 1) ? seed() : POPu;
85ab1d1d 3174 (void)seedDrand01((Rand_seed_t)anum);
80252599 3175 PL_srand_called = TRUE;
da1010ec
NC
3176 if (anum)
3177 XPUSHu(anum);
3178 else {
3179 /* Historically srand always returned true. We can avoid breaking
3180 that like this: */
3181 sv_setpvs(TARG, "0 but true");
3182 XPUSHTARG;
3183 }
83832992 3184 RETURN;
79072805
LW
3185}
3186
79072805
LW
3187PP(pp_int)
3188{
6f1401dc
DM
3189 dVAR; dSP; dTARGET;
3190 tryAMAGICun_MG(int_amg, AMGf_numeric);
774d564b 3191 {
6f1401dc
DM
3192 SV * const sv = TOPs;
3193 const IV iv = SvIV_nomg(sv);
28e5dec8
JH
3194 /* XXX it's arguable that compiler casting to IV might be subtly
3195 different from modf (for numbers inside (IV_MIN,UV_MAX)) in which
3196 else preferring IV has introduced a subtle behaviour change bug. OTOH
3197 relying on floating point to be accurate is a bug. */
3198
c781a409 3199 if (!SvOK(sv)) {
922c4365 3200 SETu(0);
c781a409
RD
3201 }
3202 else if (SvIOK(sv)) {
3203 if (SvIsUV(sv))
6f1401dc 3204 SETu(SvUV_nomg(sv));
c781a409 3205 else
28e5dec8 3206 SETi(iv);
c781a409 3207 }
c781a409 3208 else {
6f1401dc 3209 const NV value = SvNV_nomg(sv);
1048ea30 3210 if (value >= 0.0) {
28e5dec8
JH
3211 if (value < (NV)UV_MAX + 0.5) {
3212 SETu(U_V(value));
3213 } else {
059a1014 3214 SETn(Perl_floor(value));
28e5dec8 3215 }
1048ea30 3216 }
28e5dec8
JH
3217 else {
3218 if (value > (NV)IV_MIN - 0.5) {
3219 SETi(I_V(value));
3220 } else {
1bbae031 3221 SETn(Perl_ceil(value));
28e5dec8
JH
3222 }
3223 }
774d564b 3224 }
79072805 3225 }
79072805
LW
3226 RETURN;
3227}
3228
463ee0b2
LW
3229PP(pp_abs)
3230{
6f1401dc
DM
3231 dVAR; dSP; dTARGET;
3232 tryAMAGICun_MG(abs_amg, AMGf_numeric);
a0d0e21e 3233 {
6f1401dc 3234 SV * const sv = TOPs;
28e5dec8 3235 /* This will cache the NV value if string isn't actually integer */
6f1401dc 3236 const IV iv = SvIV_nomg(sv);
a227d84d 3237
800401ee 3238 if (!SvOK(sv)) {
922c4365 3239 SETu(0);
800401ee
JH
3240 }
3241 else if (SvIOK(sv)) {
28e5dec8 3242 /* IVX is precise */
800401ee 3243 if (SvIsUV(sv)) {
6f1401dc 3244 SETu(SvUV_nomg(sv)); /* force it to be numeric only */
28e5dec8
JH
3245 } else {
3246 if (iv >= 0) {
3247 SETi(iv);
3248 } else {
3249 if (iv != IV_MIN) {
3250 SETi(-iv);
3251 } else {
3252 /* 2s complement assumption. Also, not really needed as
3253 IV_MIN and -IV_MIN should both be %100...00 and NV-able */
3254 SETu(IV_MIN);
3255 }
a227d84d 3256 }
28e5dec8
JH
3257 }
3258 } else{
6f1401dc 3259 const NV value = SvNV_nomg(sv);
774d564b 3260 if (value < 0.0)
1b6737cc 3261 SETn(-value);
a4474c9e
DD
3262 else
3263 SETn(value);
774d564b 3264 }
a0d0e21e 3265 }
774d564b 3266 RETURN;
463ee0b2
LW
3267}
3268
79072805
LW
3269PP(pp_oct)
3270{
97aff369 3271 dVAR; dSP; dTARGET;
5c144d81 3272 const char *tmps;
53305cf1 3273 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES;
6f894ead 3274 STRLEN len;
53305cf1
NC
3275 NV result_nv;
3276 UV result_uv;
1b6737cc 3277 SV* const sv = POPs;
79072805 3278
349d4f2f 3279 tmps = (SvPV_const(sv, len));
2bc69dc4
NIS
3280 if (DO_UTF8(sv)) {
3281 /* If Unicode, try to downgrade
3282 * If not possible, croak. */
1b6737cc 3283 SV* const tsv = sv_2mortal(newSVsv(sv));
2bc69dc4
NIS
3284
3285 SvUTF8_on(tsv);
3286 sv_utf8_downgrade(tsv, FALSE);
349d4f2f 3287 tmps = SvPV_const(tsv, len);
2bc69dc4 3288 }
daa2adfd
NC
3289 if (PL_op->op_type == OP_HEX)
3290 goto hex;
3291
6f894ead 3292 while (*tmps && len && isSPACE(*tmps))
53305cf1 3293 tmps++, len--;
9e24b6e2 3294 if (*tmps == '0')
53305cf1 3295 tmps++, len--;
a674e8db 3296 if (*tmps == 'x' || *tmps == 'X') {
daa2adfd 3297 hex:
53305cf1 3298 result_uv = grok_hex (tmps, &len, &flags, &result_nv);
daa2adfd 3299 }
a674e8db 3300 else if (*tmps == 'b' || *tmps == 'B')
53305cf1 3301 result_uv = grok_bin (tmps, &len, &flags, &result_nv);
464e2e8a 3302 else
53305cf1
NC
3303 result_uv = grok_oct (tmps, &len, &flags, &result_nv);
3304
3305 if (flags & PERL_SCAN_GREATER_THAN_UV_MAX) {
3306 XPUSHn(result_nv);
3307 }
3308 else {
3309 XPUSHu(result_uv);
3310 }
79072805
LW
3311 RETURN;
3312}
3313
3314/* String stuff. */
3315
3316PP(pp_length)
3317{
97aff369 3318 dVAR; dSP; dTARGET;
0bd48802 3319 SV * const sv = TOPs;
a0ed51b3 3320
656266fc 3321 if (SvGAMAGIC(sv)) {
9f621bb0
NC
3322 /* For an overloaded or magic scalar, we can't know in advance if
3323 it's going to be UTF-8 or not. Also, we can't call sv_len_utf8 as
3324 it likes to cache the length. Maybe that should be a documented
3325 feature of it.
92331800
NC
3326 */
3327 STRLEN len;
9f621bb0
NC
3328 const char *const p
3329 = sv_2pv_flags(sv, &len,
3330 SV_UNDEF_RETURNS_NULL|SV_CONST_RETURN|SV_GMAGIC);
92331800 3331
d88e091f
BM
3332 if (!p) {
3333 sv_setsv(TARG, &PL_sv_undef);
3334 SETTARG;
3335 }
9f621bb0 3336 else if (DO_UTF8(sv)) {
899be101 3337 SETi(utf8_length((U8*)p, (U8*)p + len));
92331800
NC
3338 }
3339 else
3340 SETi(len);
656266fc 3341 } else if (SvOK(sv)) {
9f621bb0
NC
3342 /* Neither magic nor overloaded. */
3343 if (DO_UTF8(sv))
3344 SETi(sv_len_utf8(sv));
3345 else
3346 SETi(sv_len(sv));
656266fc 3347 } else {
d88e091f
BM
3348 sv_setsv_nomg(TARG, &PL_sv_undef);
3349 SETTARG;
92331800 3350 }
79072805
LW
3351 RETURN;
3352}
3353
3354PP(pp_substr)
3355{
97aff369 3356 dVAR; dSP; dTARGET;
79072805 3357 SV *sv;
463ee0b2 3358 STRLEN curlen;
9402d6ed 3359 STRLEN utf8_curlen;
777f7c56
EB
3360 SV * pos_sv;
3361 IV pos1_iv;
3362 int pos1_is_uv;
3363 IV pos2_iv;
3364 int pos2_is_uv;
3365 SV * len_sv;
3366 IV len_iv = 0;
3367 int len_is_uv = 1;
050e6362 3368 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
e1ec3a88 3369 const char *tmps;
777f7c56 3370 const IV arybase = CopARYBASE_get(PL_curcop);
9402d6ed 3371 SV *repl_sv = NULL;
cbbf8932 3372 const char *repl = NULL;
7b8d334a 3373 STRLEN repl_len;
050e6362 3374 const int num_args = PL_op->op_private & 7;
13e30c65 3375 bool repl_need_utf8_upgrade = FALSE;
9402d6ed 3376 bool repl_is_utf8 = FALSE;
79072805 3377
78f9721b
SM
3378 if (num_args > 2) {
3379 if (num_args > 3) {
9402d6ed 3380 repl_sv = POPs;
83003860 3381 repl = SvPV_const(repl_sv, repl_len);
9402d6ed 3382 repl_is_utf8 = DO_UTF8(repl_sv) && SvCUR(repl_sv);
7b8d334a 3383 }
777f7c56
EB
3384 len_sv = POPs;
3385 len_iv = SvIV(len_sv);
3386 len_is_uv = SvIOK_UV(len_sv);
5d82c453 3387 }
777f7c56
EB
3388 pos_sv = POPs;
3389 pos1_iv = SvIV(pos_sv);
3390 pos1_is_uv = SvIOK_UV(pos_sv);
79072805 3391 sv = POPs;
849ca7ee 3392 PUTBACK;
9402d6ed
JH
3393 if (repl_sv) {
3394 if (repl_is_utf8) {
3395 if (!DO_UTF8(sv))
3396 sv_utf8_upgrade(sv);
3397 }
13e30c65
JH
3398 else if (DO_UTF8(sv))
3399 repl_need_utf8_upgrade = TRUE;
9402d6ed 3400 }
5c144d81 3401 tmps = SvPV_const(sv, curlen);
7e2040f0 3402 if (DO_UTF8(sv)) {
9402d6ed
JH
3403 utf8_curlen = sv_len_utf8(sv);
3404 if (utf8_curlen == curlen)
3405 utf8_curlen = 0;
a0ed51b3 3406 else
9402d6ed 3407 curlen = utf8_curlen;
a0ed51b3 3408 }
d1c2b58a 3409 else
9402d6ed 3410 utf8_curlen = 0;
a0ed51b3 3411
777f7c56
EB
3412 if ( (pos1_is_uv && arybase < 0) || (pos1_iv >= arybase) ) { /* pos >= $[ */
3413 UV pos1_uv = pos1_iv-arybase;
3414 /* Overflow can occur when $[ < 0 */
3415 if (arybase < 0 && pos1_uv < (UV)pos1_iv)
1c900557 3416 goto bound_fail;
777f7c56
EB
3417 pos1_iv = pos1_uv;
3418 pos1_is_uv = 1;
3419 }
3420 else if (pos1_is_uv ? (UV)pos1_iv > 0 : pos1_iv > 0) {
1c900557 3421 goto bound_fail; /* $[=3; substr($_,2,...) */
777f7c56
EB
3422 }
3423 else { /* pos < $[ */
3424 if (pos1_iv == 0) { /* $[=1; substr($_,0,...) */
3425 pos1_iv = curlen;
3426 pos1_is_uv = 1;
3427 } else {
3428 if (curlen) {
3429 pos1_is_uv = curlen-1 > ~(UV)pos1_iv;
3430 pos1_iv += curlen;
3431 }
5d82c453 3432 }
68dc0745 3433 }
777f7c56
EB
3434 if (pos1_is_uv || pos1_iv > 0) {
3435 if ((UV)pos1_iv > curlen)
1c900557 3436 goto bound_fail;
777f7c56
EB
3437 }
3438
3439 if (num_args > 2) {
3440 if (!len_is_uv && len_iv < 0) {
3441 pos2_iv = curlen + len_iv;
3442 if (curlen)
3443 pos2_is_uv = curlen-1 > ~(UV)len_iv;
3444 else
3445 pos2_is_uv = 0;
3446 } else { /* len_iv >= 0 */
3447 if (!pos1_is_uv && pos1_iv < 0) {
3448 pos2_iv = pos1_iv + len_iv;
3449 pos2_is_uv = (UV)len_iv > (UV)IV_MAX;
3450 } else {
3451 if ((UV)len_iv > curlen-(UV)pos1_iv)
3452 pos2_iv = curlen;
3453 else
3454 pos2_iv = pos1_iv+len_iv;
3455 pos2_is_uv = 1;
3456 }
5d82c453 3457 }
2304df62 3458 }
79072805 3459 else {
777f7c56
EB
3460 pos2_iv = curlen;
3461 pos2_is_uv = 1;
3462 }
3463
3464 if (!pos2_is_uv && pos2_iv < 0) {
3465 if (!pos1_is_uv && pos1_iv < 0)
1c900557 3466 goto bound_fail;
777f7c56
EB
3467 pos2_iv = 0;
3468 }
3469 else if (!pos1_is_uv && pos1_iv < 0)
3470 pos1_iv = 0;
3471
3472 if ((UV)pos2_iv < (UV)pos1_iv)
3473 pos2_iv = pos1_iv;
3474 if ((UV)pos2_iv > curlen)
3475 pos2_iv = curlen;
3476
3477 {
3478 /* pos1_iv and pos2_iv both in 0..curlen, so the cast is safe */
3479 const STRLEN pos = (STRLEN)( (UV)pos1_iv );
3480 const STRLEN len = (STRLEN)( (UV)pos2_iv - (UV)pos1_iv );
777f7c56 3481 STRLEN byte_len = len;
d931b1be
NC
3482 STRLEN byte_pos = utf8_curlen
3483 ? sv_pos_u2b_flags(sv, pos, &byte_len, SV_CONST_RETURN) : pos;
3484
2154eca7
EB
3485 if (lvalue && !repl) {
3486 SV * ret;
3487
3488 if (!SvGMAGICAL(sv)) {
3489 if (SvROK(sv)) {
3490 SvPV_force_nolen(sv);
3491 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR),
3492 "Attempt to use reference as lvalue in substr");
3493 }
3494 if (isGV_with_GP(sv))
3495 SvPV_force_nolen(sv);
3496 else if (SvOK(sv)) /* is it defined ? */
3497 (void)SvPOK_only_UTF8(sv);
3498 else
3499 sv_setpvs(sv, ""); /* avoid lexical reincarnation */
781e7547 3500 }
2154eca7
EB
3501
3502 ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
3503 sv_magic(ret, NULL, PERL_MAGIC_substr, NULL, 0);
3504 LvTYPE(ret) = 'x';
3505 LvTARG(ret) = SvREFCNT_inc_simple(sv);
3506 LvTARGOFF(ret) = pos;
3507 LvTARGLEN(ret) = len;
3508
3509 SPAGAIN;
3510 PUSHs(ret); /* avoid SvSETMAGIC here */
3511 RETURN;
781e7547
DM
3512 }
3513
2154eca7
EB
3514 SvTAINTED_off(TARG); /* decontaminate */
3515 SvUTF8_off(TARG); /* decontaminate */
3516
3517 tmps += byte_pos;
777f7c56 3518 sv_setpvn(TARG, tmps, byte_len);
12aa1545 3519#ifdef USE_LOCALE_COLLATE
14befaf4 3520 sv_unmagic(TARG, PERL_MAGIC_collxfrm);
12aa1545 3521#endif
9402d6ed 3522 if (utf8_curlen)
7f66633b 3523 SvUTF8_on(TARG);
2154eca7 3524
f7928d6c 3525 if (repl) {
13e30c65
JH
3526 SV* repl_sv_copy = NULL;
3527
3528 if (repl_need_utf8_upgrade) {
3529 repl_sv_copy = newSVsv(repl_sv);
3530 sv_utf8_upgrade(repl_sv_copy);
349d4f2f 3531 repl = SvPV_const(repl_sv_copy, repl_len);
13e30c65
JH
3532 repl_is_utf8 = DO_UTF8(repl_sv_copy) && SvCUR(sv);
3533 }
502d9230
VP
3534 if (!SvOK(sv))
3535 sv_setpvs(sv, "");
777f7c56 3536 sv_insert_flags(sv, byte_pos, byte_len, repl, repl_len, 0);
9402d6ed 3537 if (repl_is_utf8)
f7928d6c 3538 SvUTF8_on(sv);
ef8d46e8 3539 SvREFCNT_dec(repl_sv_copy);
f7928d6c 3540 }
79072805 3541 }
849ca7ee 3542 SPAGAIN;
79072805
LW
3543 PUSHs(TARG); /* avoid SvSETMAGIC here */
3544 RETURN;
777f7c56 3545
1c900557 3546bound_fail:
777f7c56
EB
3547 if (lvalue || repl)
3548 Perl_croak(aTHX_ "substr outside of string");
3549 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR), "substr outside of string");
3550 RETPUSHUNDEF;
79072805
LW
3551}
3552
3553PP(pp_vec)
3554{
2154eca7 3555 dVAR; dSP;
1b6737cc
AL
3556 register const IV size = POPi;
3557 register const IV offset = POPi;
3558 register SV * const src = POPs;
3559 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
2154eca7 3560 SV * ret;
a0d0e21e 3561
81e118e0 3562 if (lvalue) { /* it's an lvalue! */
2154eca7
EB
3563 ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
3564 sv_magic(ret, NULL, PERL_MAGIC_vec, NULL, 0);
3565 LvTYPE(ret) = 'v';
3566 LvTARG(ret) = SvREFCNT_inc_simple(src);
3567 LvTARGOFF(ret) = offset;
3568 LvTARGLEN(ret) = size;
3569 }
3570 else {
3571 dTARGET;
3572 SvTAINTED_off(TARG); /* decontaminate */
3573 ret = TARG;
79072805
LW
3574 }
3575
2154eca7
EB
3576 sv_setuv(ret, do_vecget(src, offset, size));
3577 PUSHs(ret);
79072805
LW
3578 RETURN;
3579}
3580
3581PP(pp_index)
3582{
97aff369 3583 dVAR; dSP; dTARGET;
79072805
LW
3584 SV *big;
3585 SV *little;
c445ea15 3586 SV *temp = NULL;
ad66a58c 3587 STRLEN biglen;
2723d216 3588 STRLEN llen = 0;
79072805
LW
3589 I32 offset;
3590 I32 retval;
73ee8be2
NC
3591 const char *big_p;
3592 const char *little_p;
fc15ae8f 3593 const I32 arybase = CopARYBASE_get(PL_curcop);
2f040f7f
NC
3594 bool big_utf8;
3595 bool little_utf8;
2723d216 3596 const bool is_index = PL_op->op_type == OP_INDEX;
79072805 3597
2723d216
NC
3598 if (MAXARG >= 3) {
3599 /* arybase is in characters, like offset, so combine prior to the
3600 UTF-8 to bytes calculation. */
79072805 3601 offset = POPi - arybase;
2723d216 3602 }
79072805
LW
3603 little = POPs;
3604 big = POPs;
73ee8be2
NC
3605 big_p = SvPV_const(big, biglen);
3606 little_p = SvPV_const(little, llen);
3607
e609e586
NC
3608 big_utf8 = DO_UTF8(big);
3609 little_utf8 = DO_UTF8(little);
3610 if (big_utf8 ^ little_utf8) {
3611 /* One needs to be upgraded. */
2f040f7f
NC
3612 if (little_utf8 && !PL_encoding) {
3613 /* Well, maybe instead we might be able to downgrade the small
3614 string? */
1eced8f8 3615 char * const pv = (char*)bytes_from_utf8((U8 *)little_p, &llen,
2f040f7f
NC
3616 &little_utf8);
3617 if (little_utf8) {
3618 /* If the large string is ISO-8859-1, and it's not possible to
3619 convert the small string to ISO-8859-1, then there is no
3620 way that it could be found anywhere by index. */
3621 retval = -1;
3622 goto fail;
3623 }
e609e586 3624
2f040f7f
NC
3625 /* At this point, pv is a malloc()ed string. So donate it to temp
3626 to ensure it will get free()d */
3627 little = temp = newSV(0);
73ee8be2
NC
3628 sv_usepvn(temp, pv, llen);
3629 little_p = SvPVX(little);
e609e586 3630 } else {
73ee8be2
NC
3631 temp = little_utf8
3632 ? newSVpvn(big_p, biglen) : newSVpvn(little_p, llen);
2f040f7f
NC
3633
3634 if (PL_encoding) {
3635 sv_recode_to_utf8(temp, PL_encoding);
3636 } else {
3637 sv_utf8_upgrade(temp);
3638 }
3639 if (little_utf8) {
3640 big = temp;
3641 big_utf8 = TRUE;
73ee8be2 3642 big_p = SvPV_const(big, biglen);
2f040f7f
NC
3643 } else {
3644 little = temp;
73ee8be2 3645 little_p = SvPV_const(little, llen);
2f040f7f 3646 }
e609e586
NC
3647 }
3648 }
73ee8be2
NC
3649 if (SvGAMAGIC(big)) {
3650 /* Life just becomes a lot easier if I use a temporary here.
3651 Otherwise I need to avoid calls to sv_pos_u2b(), which (dangerously)
3652 will trigger magic and overloading again, as will fbm_instr()
3653 */
59cd0e26
NC
3654 big = newSVpvn_flags(big_p, biglen,
3655 SVs_TEMP | (big_utf8 ? SVf_UTF8 : 0));
73ee8be2
NC
3656 big_p = SvPVX(big);
3657 }
e4e44778 3658 if (SvGAMAGIC(little) || (is_index && !SvOK(little))) {
73ee8be2
NC
3659 /* index && SvOK() is a hack. fbm_instr() calls SvPV_const, which will
3660 warn on undef, and we've already triggered a warning with the
3661 SvPV_const some lines above. We can't remove that, as we need to
3662 call some SvPV to trigger overloading early and find out if the
3663 string is UTF-8.
3664 This is all getting to messy. The API isn't quite clean enough,
3665 because data access has side effects.
3666 */
59cd0e26
NC
3667 little = newSVpvn_flags(little_p, llen,
3668 SVs_TEMP | (little_utf8 ? SVf_UTF8 : 0));
73ee8be2
NC
3669 little_p = SvPVX(little);
3670 }
e609e586 3671
79072805 3672 if (MAXARG < 3)
2723d216 3673 offset = is_index ? 0 : biglen;
a0ed51b3 3674 else {
ad66a58c 3675 if (big_utf8 && offset > 0)
a0ed51b3 3676 sv_pos_u2b(big, &offset, 0);
73ee8be2
NC
3677 if (!is_index)
3678 offset += llen;
a0ed51b3 3679 }
79072805
LW
3680 if (offset < 0)
3681 offset = 0;
ad66a58c
NC
3682 else if (offset > (I32)biglen)
3683 offset = biglen;
73ee8be2
NC
3684 if (!(little_p = is_index
3685 ? fbm_instr((unsigned char*)big_p + offset,
3686 (unsigned char*)big_p + biglen, little, 0)
3687 : rninstr(big_p, big_p + offset,
3688 little_p, little_p + llen)))
a0ed51b3 3689 retval = -1;
ad66a58c 3690 else {
73ee8be2 3691 retval = little_p - big_p;
ad66a58c
NC
3692 if (retval > 0 && big_utf8)
3693 sv_pos_b2u(big, &retval);
3694 }
ef8d46e8 3695 SvREFCNT_dec(temp);
2723d216 3696 fail:
a0ed51b3 3697 PUSHi(retval + arybase);
79072805
LW
3698 RETURN;
3699}
3700
3701PP(pp_sprintf)
3702{
97aff369 3703 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
20ee07fb
RGS
3704 if (SvTAINTED(MARK[1]))
3705 TAINT_PROPER("sprintf");
3e6bd4bf 3706 SvTAINTED_off(TARG);
79072805 3707 do_sprintf(TARG, SP-MARK, MARK+1);
bbce6d69 3708 TAINT_IF(SvTAINTED(TARG));
79072805
LW
3709 SP = ORIGMARK;
3710 PUSHTARG;
3711 RETURN;
3712}
3713
79072805
LW
3714PP(pp_ord)
3715{
97aff369 3716 dVAR; dSP; dTARGET;
1eced8f8 3717
7df053ec 3718 SV *argsv = POPs;
ba210ebe 3719 STRLEN len;
349d4f2f 3720 const U8 *s = (U8*)SvPV_const(argsv, len);
121910a4 3721
799ef3cb 3722 if (PL_encoding && SvPOK(argsv) && !DO_UTF8(argsv)) {
1eced8f8 3723 SV * const tmpsv = sv_2mortal(newSVsv(argsv));
799ef3cb 3724 s = (U8*)sv_recode_to_utf8(tmpsv, PL_encoding);
121910a4
JH
3725 argsv = tmpsv;
3726 }
79072805 3727
872c91ae 3728 XPUSHu(DO_UTF8(argsv) ?
89ebb4a3 3729 utf8n_to_uvchr(s, UTF8_MAXBYTES, 0, UTF8_ALLOW_ANYUV) :
5fc32dea 3730 (UV)(*s & 0xff));
68795e93 3731
79072805
LW
3732 RETURN;
3733}
3734
463ee0b2
LW
3735PP(pp_chr)
3736{
97aff369 3737 dVAR; dSP; dTARGET;
463ee0b2 3738 char *tmps;
8a064bd6
JH
3739 UV value;
3740
3741 if (((SvIOK_notUV(TOPs) && SvIV(TOPs) < 0)
3742 ||
3743 (SvNOK(TOPs) && SvNV(TOPs) < 0.0))) {
3744 if (IN_BYTES) {
3745 value = POPu; /* chr(-1) eq chr(0xff), etc. */
3746 } else {
3747 (void) POPs; /* Ignore the argument value. */
3748 value = UNICODE_REPLACEMENT;
3749 }
3750 } else {
3751 value = POPu;
3752 }
463ee0b2 3753
862a34c6 3754 SvUPGRADE(TARG,SVt_PV);
a0ed51b3 3755
0064a8a9 3756 if (value > 255 && !IN_BYTES) {
eb160463 3757 SvGROW(TARG, (STRLEN)UNISKIP(value)+1);
62961d2e 3758 tmps = (char*)uvchr_to_utf8_flags((U8*)SvPVX(TARG), value, 0);
349d4f2f 3759 SvCUR_set(TARG, tmps - SvPVX_const(TARG));
a0ed51b3
LW
3760 *tmps = '\0';
3761 (void)SvPOK_only(TARG);
aa6ffa16 3762 SvUTF8_on(TARG);
a0ed51b3
LW
3763 XPUSHs(TARG);
3764 RETURN;
3765 }
3766
748a9306 3767 SvGROW(TARG,2);
463ee0b2
LW
3768 SvCUR_set(TARG, 1);
3769 tmps = SvPVX(TARG);
eb160463 3770 *tmps++ = (char)value;
748a9306 3771 *tmps = '\0';
a0d0e21e 3772 (void)SvPOK_only(TARG);
4c5ed6e2 3773
88632417 3774 if (PL_encoding && !IN_BYTES) {
799ef3cb 3775 sv_recode_to_utf8(TARG, PL_encoding);
88632417
JH
3776 tmps = SvPVX(TARG);
3777 if (SvCUR(TARG) == 0 || !is_utf8_string((U8*)tmps, SvCUR(TARG)) ||
4c5ed6e2
TS
3778 UNICODE_IS_REPLACEMENT(utf8_to_uvchr((U8*)tmps, NULL))) {
3779 SvGROW(TARG, 2);
d5a15ac2 3780 tmps = SvPVX(TARG);
4c5ed6e2
TS
3781 SvCUR_set(TARG, 1);
3782 *tmps++ = (char)value;
88632417 3783 *tmps = '\0';
4c5ed6e2 3784 SvUTF8_off(TARG);
88632417
JH
3785 }
3786 }
4c5ed6e2 3787
463ee0b2
LW
3788 XPUSHs(TARG);
3789 RETURN;
3790}
3791
79072805
LW
3792PP(pp_crypt)
3793{
79072805 3794#ifdef HAS_CRYPT
97aff369 3795 dVAR; dSP; dTARGET;
5f74f29c 3796 dPOPTOPssrl;
85c16d83 3797 STRLEN len;
10516c54 3798 const char *tmps = SvPV_const(left, len);
2bc69dc4 3799
85c16d83 3800 if (DO_UTF8(left)) {
2bc69dc4 3801 /* If Unicode, try to downgrade.
f2791508
JH
3802 * If not possible, croak.
3803 * Yes, we made this up. */
1b6737cc 3804 SV* const tsv = sv_2mortal(newSVsv(left));
2bc69dc4 3805
f2791508 3806 SvUTF8_on(tsv);
2bc69dc4 3807 sv_utf8_downgrade(tsv, FALSE);
349d4f2f 3808 tmps = SvPV_const(tsv, len);
85c16d83 3809 }
05404ffe
JH
3810# ifdef USE_ITHREADS
3811# ifdef HAS_CRYPT_R
3812 if (!PL_reentrant_buffer->_crypt_struct_buffer) {
3813 /* This should be threadsafe because in ithreads there is only
3814 * one thread per interpreter. If this would not be true,
3815 * we would need a mutex to protect this malloc. */
3816 PL_reentrant_buffer->_crypt_struct_buffer =
3817 (struct crypt_data *)safemalloc(sizeof(struct crypt_data));
3818#if defined(__GLIBC__) || defined(__EMX__)
3819 if (PL_reentrant_buffer->_crypt_struct_buffer) {
3820 PL_reentrant_buffer->_crypt_struct_buffer->initialized = 0;
3821 /* work around glibc-2.2.5 bug */
3822 PL_reentrant_buffer->_crypt_struct_buffer->current_saltbits = 0;
3823 }
05404ffe 3824#endif
6ab58e4d 3825 }
05404ffe
JH
3826# endif /* HAS_CRYPT_R */
3827# endif /* USE_ITHREADS */
5f74f29c 3828# ifdef FCRYPT
83003860 3829 sv_setpv(TARG, fcrypt(tmps, SvPV_nolen_const(right)));
5f74f29c 3830# else
83003860 3831 sv_setpv(TARG, PerlProc_crypt(tmps, SvPV_nolen_const(right)));
5f74f29c 3832# endif
ec93b65f 3833 SETTARG;
4808266b 3834 RETURN;
79072805 3835#else
b13b2135 3836 DIE(aTHX_
79072805
LW
3837 "The crypt() function is unimplemented due to excessive paranoia.");
3838#endif
79072805
LW
3839}
3840
00f254e2
KW
3841/* Generally UTF-8 and UTF-EBCDIC are indistinguishable at this level. So
3842 * most comments below say UTF-8, when in fact they mean UTF-EBCDIC as well */
3843
3844/* Both the characters below can be stored in two UTF-8 bytes. In UTF-8 the max
3845 * character that 2 bytes can hold is U+07FF, and in UTF-EBCDIC it is U+03FF.
3846 * See http://www.unicode.org/unicode/reports/tr16 */
3847#define LATIN_CAPITAL_LETTER_Y_WITH_DIAERESIS 0x0178 /* Also is title case */
3848#define GREEK_CAPITAL_LETTER_MU 0x039C /* Upper and title case of MICRON */
3849
3850/* Below are several macros that generate code */
3851/* Generates code to store a unicode codepoint c that is known to occupy
3852 * exactly two UTF-8 and UTF-EBCDIC bytes; it is stored into p and p+1. */
3853#define STORE_UNI_TO_UTF8_TWO_BYTE(p, c) \
3854 STMT_START { \
3855 *(p) = UTF8_TWO_BYTE_HI(c); \
3856 *((p)+1) = UTF8_TWO_BYTE_LO(c); \
3857 } STMT_END
3858
3859/* Like STORE_UNI_TO_UTF8_TWO_BYTE, but advances p to point to the next
3860 * available byte after the two bytes */
3861#define CAT_UNI_TO_UTF8_TWO_BYTE(p, c) \
3862 STMT_START { \
3863 *(p)++ = UTF8_TWO_BYTE_HI(c); \
3864 *((p)++) = UTF8_TWO_BYTE_LO(c); \
3865 } STMT_END
3866
3867/* Generates code to store the upper case of latin1 character l which is known
3868 * to have its upper case be non-latin1 into the two bytes p and p+1. There
3869 * are only two characters that fit this description, and this macro knows
3870 * about them, and that the upper case values fit into two UTF-8 or UTF-EBCDIC
3871 * bytes */
3872#define STORE_NON_LATIN1_UC(p, l) \
3873STMT_START { \
3874 if ((l) == LATIN_SMALL_LETTER_Y_WITH_DIAERESIS) { \
3875 STORE_UNI_TO_UTF8_TWO_BYTE((p), LATIN_CAPITAL_LETTER_Y_WITH_DIAERESIS); \
3876 } else { /* Must be the following letter */ \
3877 STORE_UNI_TO_UTF8_TWO_BYTE((p), GREEK_CAPITAL_LETTER_MU); \
3878 } \
3879} STMT_END
3880
3881/* Like STORE_NON_LATIN1_UC, but advances p to point to the next available byte
3882 * after the character stored */
3883#define CAT_NON_LATIN1_UC(p, l) \
3884STMT_START { \
3885 if ((l) == LATIN_SMALL_LETTER_Y_WITH_DIAERESIS) { \
3886 CAT_UNI_TO_UTF8_TWO_BYTE((p), LATIN_CAPITAL_LETTER_Y_WITH_DIAERESIS); \
3887 } else { \
3888 CAT_UNI_TO_UTF8_TWO_BYTE((p), GREEK_CAPITAL_LETTER_MU); \
3889 } \
3890} STMT_END
3891
3892/* Generates code to add the two UTF-8 bytes (probably u) that are the upper
3893 * case of l into p and p+1. u must be the result of toUPPER_LATIN1_MOD(l),
3894 * and must require two bytes to store it. Advances p to point to the next
3895 * available position */
3896#define CAT_TWO_BYTE_UNI_UPPER_MOD(p, l, u) \
3897STMT_START { \
3898 if ((u) != LATIN_SMALL_LETTER_Y_WITH_DIAERESIS) { \
3899 CAT_UNI_TO_UTF8_TWO_BYTE((p), (u)); /* not special, just save it */ \
3900 } else if (l == LATIN_SMALL_LETTER_SHARP_S) { \
3901 *(p)++ = 'S'; *(p)++ = 'S'; /* upper case is 'SS' */ \
3902 } else {/* else is one of the other two special cases */ \
3903 CAT_NON_LATIN1_UC((p), (l)); \
3904 } \
3905} STMT_END
3906
79072805
LW
3907PP(pp_ucfirst)
3908{
00f254e2
KW
3909 /* Actually is both lcfirst() and ucfirst(). Only the first character
3910 * changes. This means that possibly we can change in-place, ie., just
3911 * take the source and change that one character and store it back, but not
3912 * if read-only etc, or if the length changes */
3913
97aff369 3914 dVAR;
39644a26 3915 dSP;
d54190f6 3916 SV *source = TOPs;
00f254e2 3917 STRLEN slen; /* slen is the byte length of the whole SV. */
d54190f6
NC
3918 STRLEN need;
3919 SV *dest;
00f254e2
KW
3920 bool inplace; /* ? Convert first char only, in-place */
3921 bool doing_utf8 = FALSE; /* ? using utf8 */
3922 bool convert_source_to_utf8 = FALSE; /* ? need to convert */
12e9c124 3923 const int op_type = PL_op->op_type;
d54190f6
NC
3924 const U8 *s;
3925 U8 *d;
3926 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
00f254e2
KW
3927 STRLEN ulen; /* ulen is the byte length of the original Unicode character
3928 * stored as UTF-8 at s. */
3929 STRLEN tculen; /* tculen is the byte length of the freshly titlecased (or
3930 * lowercased) character stored in tmpbuf. May be either
3931 * UTF-8 or not, but in either case is the number of bytes */
d54190f6
NC
3932
3933 SvGETMAGIC(source);
3934 if (SvOK(source)) {
3935 s = (const U8*)SvPV_nomg_const(source, slen);
3936 } else {
0a0ffbce
RGS
3937 if (ckWARN(WARN_UNINITIALIZED))
3938 report_uninit(source);
1eced8f8 3939 s = (const U8*)"";
d54190f6
NC
3940 slen = 0;
3941 }
a0ed51b3 3942
00f254e2
KW
3943 /* We may be able to get away with changing only the first character, in
3944 * place, but not if read-only, etc. Later we may discover more reasons to
3945 * not convert in-place. */
3946 inplace = SvPADTMP(source) && !SvREADONLY(source) && SvTEMP(source);
3947
3948 /* First calculate what the changed first character should be. This affects
3949 * whether we can just swap it out, leaving the rest of the string unchanged,
3950 * or even if have to convert the dest to UTF-8 when the source isn't */
3951
3952 if (! slen) { /* If empty */
3953 need = 1; /* still need a trailing NUL */
3954 }
3955 else if (DO_UTF8(source)) { /* Is the source utf8? */
d54190f6 3956 doing_utf8 = TRUE;
00f254e2
KW
3957
3958/* TODO: This is #ifdefd out because it has hard-coded the standard mappings,
3959 * and doesn't allow for the user to specify their own. When code is added to
3960 * detect if there is a user-defined mapping in force here, and if so to use
3961 * that, then the code below can be compiled. The detection would be a good
3962 * thing anyway, as currently the user-defined mappings only work on utf8
3963 * strings, and thus depend on the chosen internal storage method, which is a
3964 * bad thing */
3965#ifdef GO_AHEAD_AND_BREAK_USER_DEFINED_CASE_MAPPINGS
3966 if (UTF8_IS_INVARIANT(*s)) {
3967
3968 /* An invariant source character is either ASCII or, in EBCDIC, an
3969 * ASCII equivalent or a caseless C1 control. In both these cases,
3970 * the lower and upper cases of any character are also invariants
3971 * (and title case is the same as upper case). So it is safe to
3972 * use the simple case change macros which avoid the overhead of
3973 * the general functions. Note that if perl were to be extended to
3974 * do locale handling in UTF-8 strings, this wouldn't be true in,
3975 * for example, Lithuanian or Turkic. */
3976 *tmpbuf = (op_type == OP_LCFIRST) ? toLOWER(*s) : toUPPER(*s);
3977 tculen = ulen = 1;
3978 need = slen + 1;
12e9c124 3979 }
00f254e2
KW
3980 else if (UTF8_IS_DOWNGRADEABLE_START(*s)) {
3981 U8 chr;
3982
3983 /* Similarly, if the source character isn't invariant but is in the
3984 * latin1 range (or EBCDIC equivalent thereof), we have the case
3985 * changes compiled into perl, and can avoid the overhead of the
3986 * general functions. In this range, the characters are stored as
3987 * two UTF-8 bytes, and it so happens that any changed-case version
3988 * is also two bytes (in both ASCIIish and EBCDIC machines). */
3989 tculen = ulen = 2;
3990 need = slen + 1;
3991
3992 /* Convert the two source bytes to a single Unicode code point
3993 * value, change case and save for below */
356979f4 3994 chr = TWO_BYTE_UTF8_TO_UNI(*s, *(s+1));
00f254e2
KW
3995 if (op_type == OP_LCFIRST) { /* lower casing is easy */
3996 U8 lower = toLOWER_LATIN1(chr);
3997 STORE_UNI_TO_UTF8_TWO_BYTE(tmpbuf, lower);
3998 }
3999 else { /* ucfirst */
4000 U8 upper = toUPPER_LATIN1_MOD(chr);
4001
4002 /* Most of the latin1 range characters are well-behaved. Their
4003 * title and upper cases are the same, and are also in the
4004 * latin1 range. The macro above returns their upper (hence
4005 * title) case, and all that need be done is to save the result
4006 * for below. However, several characters are problematic, and
4007 * have to be handled specially. The MOD in the macro name
4008 * above means that these tricky characters all get mapped to
4009 * the single character LATIN_SMALL_LETTER_Y_WITH_DIAERESIS.
4010 * This mapping saves some tests for the majority of the
4011 * characters */
4012
4013 if (upper != LATIN_SMALL_LETTER_Y_WITH_DIAERESIS) {
4014
4015 /* Not tricky. Just save it. */
4016 STORE_UNI_TO_UTF8_TWO_BYTE(tmpbuf, upper);
4017 }
4018 else if (chr == LATIN_SMALL_LETTER_SHARP_S) {
4019
4020 /* This one is tricky because it is two characters long,
4021 * though the UTF-8 is still two bytes, so the stored
4022 * length doesn't change */
4023 *tmpbuf = 'S'; /* The UTF-8 is 'Ss' */
4024 *(tmpbuf + 1) = 's';
4025 }
4026 else {
4027
4028 /* The other two have their title and upper cases the same,
4029 * but are tricky because the changed-case characters
4030 * aren't in the latin1 range. They, however, do fit into
4031 * two UTF-8 bytes */
4032 STORE_NON_LATIN1_UC(tmpbuf, chr);
4033 }
4034 }
4035 }
4036 else {
4037#endif /* end of dont want to break user-defined casing */
4038
4039 /* Here, can't short-cut the general case */
4040
4041 utf8_to_uvchr(s, &ulen);
4042 if (op_type == OP_UCFIRST) toTITLE_utf8(s, tmpbuf, &tculen);
4043 else toLOWER_utf8(s, tmpbuf, &tculen);
4044
4045 /* we can't do in-place if the length changes. */
4046 if (ulen != tculen) inplace = FALSE;
4047 need = slen + 1 - ulen + tculen;
4048#ifdef GO_AHEAD_AND_BREAK_USER_DEFINED_CASE_MAPPINGS
4049 }
4050#endif
d54190f6 4051 }
00f254e2
KW
4052 else { /* Non-zero length, non-UTF-8, Need to consider locale and if
4053 * latin1 is treated as caseless. Note that a locale takes
4054 * precedence */
4055 tculen = 1; /* Most characters will require one byte, but this will
4056 * need to be overridden for the tricky ones */
4057 need = slen + 1;
4058
4059 if (op_type == OP_LCFIRST) {
d54190f6 4060
00f254e2
KW
4061 /* lower case the first letter: no trickiness for any character */
4062 *tmpbuf = (IN_LOCALE_RUNTIME) ? toLOWER_LC(*s) :
4063 ((IN_UNI_8_BIT) ? toLOWER_LATIN1(*s) : toLOWER(*s));
4064 }
4065 /* is ucfirst() */
4066 else if (IN_LOCALE_RUNTIME) {
4067 *tmpbuf = toUPPER_LC(*s); /* This would be a bug if any locales
4068 * have upper and title case different
4069 */
4070 }
4071 else if (! IN_UNI_8_BIT) {
4072 *tmpbuf = toUPPER(*s); /* Returns caseless for non-ascii, or
4073 * on EBCDIC machines whatever the
4074 * native function does */
4075 }
4076 else { /* is ucfirst non-UTF-8, not in locale, and cased latin1 */
4077 *tmpbuf = toUPPER_LATIN1_MOD(*s);
4078
4079 /* tmpbuf now has the correct title case for all latin1 characters
4080 * except for the several ones that have tricky handling. All
4081 * of these are mapped by the MOD to the letter below. */
4082 if (*tmpbuf == LATIN_SMALL_LETTER_Y_WITH_DIAERESIS) {
4083
4084 /* The length is going to change, with all three of these, so
4085 * can't replace just the first character */
4086 inplace = FALSE;
4087
4088 /* We use the original to distinguish between these tricky
4089 * cases */
4090 if (*s == LATIN_SMALL_LETTER_SHARP_S) {
4091 /* Two character title case 'Ss', but can remain non-UTF-8 */
4092 need = slen + 2;
4093 *tmpbuf = 'S';
4094 *(tmpbuf + 1) = 's'; /* Assert: length(tmpbuf) >= 2 */
4095 tculen = 2;
4096 }
4097 else {
d54190f6 4098
00f254e2
KW
4099 /* The other two tricky ones have their title case outside
4100 * latin1. It is the same as their upper case. */
4101 doing_utf8 = TRUE;
4102 STORE_NON_LATIN1_UC(tmpbuf, *s);
4103
4104 /* The UTF-8 and UTF-EBCDIC lengths of both these characters
4105 * and their upper cases is 2. */
4106 tculen = ulen = 2;
4107
4108 /* The entire result will have to be in UTF-8. Assume worst
4109 * case sizing in conversion. (all latin1 characters occupy
4110 * at most two bytes in utf8) */
4111 convert_source_to_utf8 = TRUE;
4112 need = slen * 2 + 1;
4113 }
4114 } /* End of is one of the three special chars */
4115 } /* End of use Unicode (Latin1) semantics */
4116 } /* End of changing the case of the first character */
4117
4118 /* Here, have the first character's changed case stored in tmpbuf. Ready to
4119 * generate the result */
4120 if (inplace) {
4121
4122 /* We can convert in place. This means we change just the first
4123 * character without disturbing the rest; no need to grow */
d54190f6
NC
4124 dest = source;
4125 s = d = (U8*)SvPV_force_nomg(source, slen);
4126 } else {
4127 dTARGET;
4128
4129 dest = TARG;
4130
00f254e2
KW
4131 /* Here, we can't convert in place; we earlier calculated how much
4132 * space we will need, so grow to accommodate that */
d54190f6 4133 SvUPGRADE(dest, SVt_PV);
3b416f41 4134 d = (U8*)SvGROW(dest, need);
d54190f6
NC
4135 (void)SvPOK_only(dest);
4136
4137 SETs(dest);
d54190f6 4138 }
44bc797b 4139
d54190f6 4140 if (doing_utf8) {
00f254e2
KW
4141 if (! inplace) {
4142 if (! convert_source_to_utf8) {
4143
4144 /* Here both source and dest are in UTF-8, but have to create
4145 * the entire output. We initialize the result to be the
4146 * title/lower cased first character, and then append the rest
4147 * of the string. */
4148 sv_setpvn(dest, (char*)tmpbuf, tculen);
4149 if (slen > ulen) {
4150 sv_catpvn(dest, (char*)(s + ulen), slen - ulen);
4151 }
4152 }
4153 else {
4154 const U8 *const send = s + slen;
4155
4156 /* Here the dest needs to be in UTF-8, but the source isn't,
4157 * except we earlier UTF-8'd the first character of the source
4158 * into tmpbuf. First put that into dest, and then append the
4159 * rest of the source, converting it to UTF-8 as we go. */
4160
4161 /* Assert tculen is 2 here because the only two characters that
4162 * get to this part of the code have 2-byte UTF-8 equivalents */
4163 *d++ = *tmpbuf;
4164 *d++ = *(tmpbuf + 1);
4165 s++; /* We have just processed the 1st char */
4166
4167 for (; s < send; s++) {
4168 d = uvchr_to_utf8(d, *s);
4169 }
4170 *d = '\0';
4171 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4172 }
d54190f6 4173 SvUTF8_on(dest);
a0ed51b3 4174 }
00f254e2 4175 else { /* in-place UTF-8. Just overwrite the first character */
d54190f6
NC
4176 Copy(tmpbuf, d, tculen, U8);
4177 SvCUR_set(dest, need - 1);
a0ed51b3 4178 }
a0ed51b3 4179 }
00f254e2
KW
4180 else { /* Neither source nor dest are in or need to be UTF-8 */
4181 if (slen) {
2de3dbcc 4182 if (IN_LOCALE_RUNTIME) {
31351b04 4183 TAINT;
d54190f6 4184 SvTAINTED_on(dest);
31351b04 4185 }
00f254e2
KW
4186 if (inplace) { /* in-place, only need to change the 1st char */
4187 *d = *tmpbuf;
4188 }
4189 else { /* Not in-place */
4190
4191 /* Copy the case-changed character(s) from tmpbuf */
4192 Copy(tmpbuf, d, tculen, U8);
4193 d += tculen - 1; /* Code below expects d to point to final
4194 * character stored */
4195 }
4196 }
4197 else { /* empty source */
4198 /* See bug #39028: Don't taint if empty */
d54190f6
NC
4199 *d = *s;
4200 }
4201
00f254e2
KW
4202 /* In a "use bytes" we don't treat the source as UTF-8, but, still want
4203 * the destination to retain that flag */
d54190f6
NC
4204 if (SvUTF8(source))
4205 SvUTF8_on(dest);
4206
00f254e2 4207 if (!inplace) { /* Finish the rest of the string, unchanged */
d54190f6
NC
4208 /* This will copy the trailing NUL */
4209 Copy(s + 1, d + 1, slen, U8);
4210 SvCUR_set(dest, need - 1);
bbce6d69 4211 }
bbce6d69 4212 }
d54190f6 4213 SvSETMAGIC(dest);
79072805
LW
4214 RETURN;
4215}
4216
67306194
NC
4217/* There's so much setup/teardown code common between uc and lc, I wonder if
4218 it would be worth merging the two, and just having a switch outside each
00f254e2 4219 of the three tight loops. There is less and less commonality though */
79072805
LW
4220PP(pp_uc)
4221{
97aff369 4222 dVAR;
39644a26 4223 dSP;
67306194 4224 SV *source = TOPs;
463ee0b2 4225 STRLEN len;
67306194
NC
4226 STRLEN min;
4227 SV *dest;
4228 const U8 *s;
4229 U8 *d;
79072805 4230
67306194
NC
4231 SvGETMAGIC(source);
4232
4233 if (SvPADTMP(source) && !SvREADONLY(source) && !SvAMAGIC(source)
00f254e2
KW
4234 && SvTEMP(source) && !DO_UTF8(source)
4235 && (IN_LOCALE_RUNTIME || ! IN_UNI_8_BIT)) {
4236
4237 /* We can convert in place. The reason we can't if in UNI_8_BIT is to
4238 * make the loop tight, so we overwrite the source with the dest before
4239 * looking at it, and we need to look at the original source
4240 * afterwards. There would also need to be code added to handle
4241 * switching to not in-place in midstream if we run into characters
4242 * that change the length.
4243 */
67306194
NC
4244 dest = source;
4245 s = d = (U8*)SvPV_force_nomg(source, len);
4246 min = len + 1;
4247 } else {
a0ed51b3 4248 dTARGET;
a0ed51b3 4249
67306194 4250 dest = TARG;
128c9517 4251
67306194
NC
4252 /* The old implementation would copy source into TARG at this point.
4253 This had the side effect that if source was undef, TARG was now
4254 an undefined SV with PADTMP set, and they don't warn inside
4255 sv_2pv_flags(). However, we're now getting the PV direct from
4256 source, which doesn't have PADTMP set, so it would warn. Hence the
4257 little games. */
4258
4259 if (SvOK(source)) {
4260 s = (const U8*)SvPV_nomg_const(source, len);
4261 } else {
0a0ffbce
RGS
4262 if (ckWARN(WARN_UNINITIALIZED))
4263 report_uninit(source);
1eced8f8 4264 s = (const U8*)"";
67306194 4265 len = 0;
a0ed51b3 4266 }
67306194
NC
4267 min = len + 1;
4268
4269 SvUPGRADE(dest, SVt_PV);
3b416f41 4270 d = (U8*)SvGROW(dest, min);
67306194
NC
4271 (void)SvPOK_only(dest);
4272
4273 SETs(dest);
a0ed51b3 4274 }
31351b04 4275
67306194
NC
4276 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
4277 to check DO_UTF8 again here. */
4278
4279 if (DO_UTF8(source)) {
4280 const U8 *const send = s + len;
4281 U8 tmpbuf[UTF8_MAXBYTES+1];
4282
4c8a458a
KW
4283 /* All occurrences of these are to be moved to follow any other marks.
4284 * This is context-dependent. We may not be passed enough context to
4285 * move the iota subscript beyond all of them, but we do the best we can
4286 * with what we're given. The result is always better than if we
4287 * hadn't done this. And, the problem would only arise if we are
4288 * passed a character without all its combining marks, which would be
4289 * the caller's mistake. The information this is based on comes from a
4290 * comment in Unicode SpecialCasing.txt, (and the Standard's text
4291 * itself) and so can't be checked properly to see if it ever gets
4292 * revised. But the likelihood of it changing is remote */
00f254e2 4293 bool in_iota_subscript = FALSE;
00f254e2 4294
67306194 4295 while (s < send) {
00f254e2
KW
4296 if (in_iota_subscript && ! is_utf8_mark(s)) {
4297 /* A non-mark. Time to output the iota subscript */
4298#define GREEK_CAPITAL_LETTER_IOTA 0x0399
4299#define COMBINING_GREEK_YPOGEGRAMMENI 0x0345
4300
4301 CAT_UNI_TO_UTF8_TWO_BYTE(d, GREEK_CAPITAL_LETTER_IOTA);
4302 in_iota_subscript = FALSE;
4303 }
00f254e2
KW
4304
4305
4306/* See comments at the first instance in this file of this ifdef */
4307#ifdef GO_AHEAD_AND_BREAK_USER_DEFINED_CASE_MAPPINGS
67306194 4308
00f254e2
KW
4309 /* If the UTF-8 character is invariant, then it is in the range
4310 * known by the standard macro; result is only one byte long */
4311 if (UTF8_IS_INVARIANT(*s)) {
4312 *d++ = toUPPER(*s);
4313 s++;
4314 }
4315 else if (UTF8_IS_DOWNGRADEABLE_START(*s)) {
4316
4317 /* Likewise, if it fits in a byte, its case change is in our
4318 * table */
81367fea 4319 U8 orig = TWO_BYTE_UTF8_TO_UNI(*s, *s++);
00f254e2
KW
4320 U8 upper = toUPPER_LATIN1_MOD(orig);
4321 CAT_TWO_BYTE_UNI_UPPER_MOD(d, orig, upper);
81367fea 4322 s++;
00f254e2
KW
4323 }
4324 else {
4325#else
4326 {
4327#endif
4328
4329 /* Otherwise, need the general UTF-8 case. Get the changed
4330 * case value and copy it to the output buffer */
4331
4332 const STRLEN u = UTF8SKIP(s);
4333 STRLEN ulen;
67306194 4334
00f254e2 4335 const UV uv = toUPPER_utf8(s, tmpbuf, &ulen);
4c8a458a
KW
4336 if (uv == GREEK_CAPITAL_LETTER_IOTA
4337 && utf8_to_uvchr(s, 0) == COMBINING_GREEK_YPOGEGRAMMENI)
4338 {
00f254e2
KW
4339 in_iota_subscript = TRUE;
4340 }
4341 else {
00f254e2
KW
4342 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
4343 /* If the eventually required minimum size outgrows
4344 * the available space, we need to grow. */
4345 const UV o = d - (U8*)SvPVX_const(dest);
4346
4347 /* If someone uppercases one million U+03B0s we
4348 * SvGROW() one million times. Or we could try
4349 * guessing how much to allocate without allocating too
4c8a458a
KW
4350 * much. Such is life. See corresponding comment in
4351 * lc code for another option */
00f254e2
KW
4352 SvGROW(dest, min);
4353 d = (U8*)SvPVX(dest) + o;
4354 }
4355 Copy(tmpbuf, d, ulen, U8);
4356 d += ulen;
00f254e2 4357 }
00f254e2 4358 s += u;
67306194 4359 }
67306194 4360 }
4c8a458a
KW
4361 if (in_iota_subscript) {
4362 CAT_UNI_TO_UTF8_TWO_BYTE(d, GREEK_CAPITAL_LETTER_IOTA);
4363 }
67306194
NC
4364 SvUTF8_on(dest);
4365 *d = '\0';
4366 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4c8a458a
KW
4367 }
4368 else { /* Not UTF-8 */
67306194
NC
4369 if (len) {
4370 const U8 *const send = s + len;
00f254e2
KW
4371
4372 /* Use locale casing if in locale; regular style if not treating
4373 * latin1 as having case; otherwise the latin1 casing. Do the
4374 * whole thing in a tight loop, for speed, */
2de3dbcc 4375 if (IN_LOCALE_RUNTIME) {
31351b04 4376 TAINT;
67306194
NC
4377 SvTAINTED_on(dest);
4378 for (; s < send; d++, s++)
4379 *d = toUPPER_LC(*s);
31351b04 4380 }
00f254e2
KW
4381 else if (! IN_UNI_8_BIT) {
4382 for (; s < send; d++, s++) {
67306194 4383 *d = toUPPER(*s);
00f254e2 4384 }
31351b04 4385 }
00f254e2
KW
4386 else {
4387 for (; s < send; d++, s++) {
4388 *d = toUPPER_LATIN1_MOD(*s);
4389 if (*d != LATIN_SMALL_LETTER_Y_WITH_DIAERESIS) continue;
4390
4391 /* The mainstream case is the tight loop above. To avoid
4392 * extra tests in that, all three characters that require
4393 * special handling are mapped by the MOD to the one tested
4394 * just above.
4395 * Use the source to distinguish between the three cases */
4396
4397 if (*s == LATIN_SMALL_LETTER_SHARP_S) {
4398
4399 /* uc() of this requires 2 characters, but they are
4400 * ASCII. If not enough room, grow the string */
4401 if (SvLEN(dest) < ++min) {
4402 const UV o = d - (U8*)SvPVX_const(dest);
4403 SvGROW(dest, min);
4404 d = (U8*)SvPVX(dest) + o;
4405 }
4406 *d++ = 'S'; *d = 'S'; /* upper case is 'SS' */
4407 continue; /* Back to the tight loop; still in ASCII */
4408 }
4409
4410 /* The other two special handling characters have their
4411 * upper cases outside the latin1 range, hence need to be
4412 * in UTF-8, so the whole result needs to be in UTF-8. So,
4413 * here we are somewhere in the middle of processing a
4414 * non-UTF-8 string, and realize that we will have to convert
4415 * the whole thing to UTF-8. What to do? There are
4416 * several possibilities. The simplest to code is to
4417 * convert what we have so far, set a flag, and continue on
4418 * in the loop. The flag would be tested each time through
4419 * the loop, and if set, the next character would be
4420 * converted to UTF-8 and stored. But, I (khw) didn't want
4421 * to slow down the mainstream case at all for this fairly
4422 * rare case, so I didn't want to add a test that didn't
4423 * absolutely have to be there in the loop, besides the
4424 * possibility that it would get too complicated for
4425 * optimizers to deal with. Another possibility is to just
4426 * give up, convert the source to UTF-8, and restart the
4427 * function that way. Another possibility is to convert
4428 * both what has already been processed and what is yet to
4429 * come separately to UTF-8, then jump into the loop that
4430 * handles UTF-8. But the most efficient time-wise of the
4431 * ones I could think of is what follows, and turned out to
4432 * not require much extra code. */
4433
4434 /* Convert what we have so far into UTF-8, telling the
4435 * function that we know it should be converted, and to
4436 * allow extra space for what we haven't processed yet.
4437 * Assume the worst case space requirements for converting
4438 * what we haven't processed so far: that it will require
4439 * two bytes for each remaining source character, plus the
4440 * NUL at the end. This may cause the string pointer to
4441 * move, so re-find it. */
4442
4443 len = d - (U8*)SvPVX_const(dest);
4444 SvCUR_set(dest, len);
4445 len = sv_utf8_upgrade_flags_grow(dest,
4446 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
4447 (send -s) * 2 + 1);
4448 d = (U8*)SvPVX(dest) + len;
4449
4450 /* And append the current character's upper case in UTF-8 */
4451 CAT_NON_LATIN1_UC(d, *s);
4452
4453 /* Now process the remainder of the source, converting to
4454 * upper and UTF-8. If a resulting byte is invariant in
4455 * UTF-8, output it as-is, otherwise convert to UTF-8 and
4456 * append it to the output. */
4457
4458 s++;
4459 for (; s < send; s++) {
4460 U8 upper = toUPPER_LATIN1_MOD(*s);
4461 if UTF8_IS_INVARIANT(upper) {
4462 *d++ = upper;
4463 }
4464 else {
4465 CAT_TWO_BYTE_UNI_UPPER_MOD(d, *s, upper);
4466 }
4467 }
4468
4469 /* Here have processed the whole source; no need to continue
4470 * with the outer loop. Each character has been converted
4471 * to upper case and converted to UTF-8 */
4472
4473 break;
4474 } /* End of processing all latin1-style chars */
4475 } /* End of processing all chars */
4476 } /* End of source is not empty */
4477
67306194 4478 if (source != dest) {
00f254e2 4479 *d = '\0'; /* Here d points to 1 after last char, add NUL */
67306194
NC
4480 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4481 }
00f254e2 4482 } /* End of isn't utf8 */
67306194 4483 SvSETMAGIC(dest);
79072805
LW
4484 RETURN;
4485}
4486
4487PP(pp_lc)
4488{
97aff369 4489 dVAR;
39644a26 4490 dSP;
ec9af7d4 4491 SV *source = TOPs;
463ee0b2 4492 STRLEN len;
ec9af7d4
NC
4493 STRLEN min;
4494 SV *dest;
4495 const U8 *s;
4496 U8 *d;
79072805 4497
ec9af7d4
NC
4498 SvGETMAGIC(source);
4499
4500 if (SvPADTMP(source) && !SvREADONLY(source) && !SvAMAGIC(source)
17fa0776 4501 && SvTEMP(source) && !DO_UTF8(source)) {
ec9af7d4 4502
00f254e2
KW
4503 /* We can convert in place, as lowercasing anything in the latin1 range
4504 * (or else DO_UTF8 would have been on) doesn't lengthen it */
ec9af7d4
NC
4505 dest = source;
4506 s = d = (U8*)SvPV_force_nomg(source, len);
4507 min = len + 1;
4508 } else {
a0ed51b3 4509 dTARGET;
a0ed51b3 4510
ec9af7d4
NC
4511 dest = TARG;
4512
4513 /* The old implementation would copy source into TARG at this point.
4514 This had the side effect that if source was undef, TARG was now
4515 an undefined SV with PADTMP set, and they don't warn inside
4516 sv_2pv_flags(). However, we're now getting the PV direct from
4517 source, which doesn't have PADTMP set, so it would warn. Hence the
4518 little games. */
4519
4520 if (SvOK(source)) {
4521 s = (const U8*)SvPV_nomg_const(source, len);
4522 } else {
0a0ffbce
RGS
4523 if (ckWARN(WARN_UNINITIALIZED))
4524 report_uninit(source);
1eced8f8 4525 s = (const U8*)"";
ec9af7d4 4526 len = 0;
a0ed51b3 4527 }
ec9af7d4 4528 min = len + 1;
128c9517 4529
ec9af7d4 4530 SvUPGRADE(dest, SVt_PV);
3b416f41 4531 d = (U8*)SvGROW(dest, min);
ec9af7d4
NC
4532 (void)SvPOK_only(dest);
4533
4534 SETs(dest);
4535 }
4536
4537 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
4538 to check DO_UTF8 again here. */
4539
4540 if (DO_UTF8(source)) {
4541 const U8 *const send = s + len;
4542 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
4543
4544 while (s < send) {
00f254e2
KW
4545/* See comments at the first instance in this file of this ifdef */
4546#ifdef GO_AHEAD_AND_BREAK_USER_DEFINED_CASE_MAPPINGS
4547 if (UTF8_IS_INVARIANT(*s)) {
89ebb4a3 4548
00f254e2 4549 /* Invariant characters use the standard mappings compiled in.
ec9af7d4 4550 */
00f254e2
KW
4551 *d++ = toLOWER(*s);
4552 s++;
ec9af7d4 4553 }
00f254e2 4554 else if (UTF8_IS_DOWNGRADEABLE_START(*s)) {
89ebb4a3 4555
00f254e2 4556 /* As do the ones in the Latin1 range */
81367fea 4557 U8 lower = toLOWER_LATIN1(TWO_BYTE_UTF8_TO_UNI(*s, *s++));
00f254e2 4558 CAT_UNI_TO_UTF8_TWO_BYTE(d, lower);
81367fea 4559 s++;
a0ed51b3 4560 }
00f254e2
KW
4561 else {
4562#endif
4563 /* Here, is utf8 not in Latin-1 range, have to go out and get
4564 * the mappings from the tables. */
4565
4566 const STRLEN u = UTF8SKIP(s);
4567 STRLEN ulen;
4568
00f254e2
KW
4569#ifndef CONTEXT_DEPENDENT_CASING
4570 toLOWER_utf8(s, tmpbuf, &ulen);
4571#else
4c8a458a
KW
4572/* This is ifdefd out because it needs more work and thought. It isn't clear
4573 * that we should do it.
4574 * A minor objection is that this is based on a hard-coded rule from the
4575 * Unicode standard, and may change, but this is not very likely at all.
4576 * mktables should check and warn if it does.
4577 * More importantly, if the sigma occurs at the end of the string, we don't
4578 * have enough context to know whether it is part of a larger string or going
4579 * to be or not. It may be that we are passed a subset of the context, via
4580 * a \U...\E, for example, and we could conceivably know the larger context if
4581 * code were changed to pass that in. But, if the string passed in is an
4582 * intermediate result, and the user concatenates two strings together
4583 * after we have made a final sigma, that would be wrong. If the final sigma
4584 * occurs in the middle of the string we are working on, then we know that it
4585 * should be a final sigma, but otherwise we can't be sure. */
00f254e2
KW
4586
4587 const UV uv = toLOWER_utf8(s, tmpbuf, &ulen);
4588
4589 /* If the lower case is a small sigma, it may be that we need
4590 * to change it to a final sigma. This happens at the end of
4591 * a word that contains more than just this character, and only
4592 * when we started with a capital sigma. */
4593 if (uv == UNICODE_GREEK_SMALL_LETTER_SIGMA &&
4594 s > send - len && /* Makes sure not the first letter */
4595 utf8_to_uvchr(s, 0) == UNICODE_GREEK_CAPITAL_LETTER_SIGMA
4596 ) {
4597
4598 /* We use the algorithm in:
4599 * http://www.unicode.org/versions/Unicode5.0.0/ch03.pdf (C
4600 * is a CAPITAL SIGMA): If C is preceded by a sequence
4601 * consisting of a cased letter and a case-ignorable
4602 * sequence, and C is not followed by a sequence consisting
4603 * of a case ignorable sequence and then a cased letter,
4604 * then when lowercasing C, C becomes a final sigma */
4605
4606 /* To determine if this is the end of a word, need to peek
4607 * ahead. Look at the next character */
4608 const U8 *peek = s + u;
4609
4610 /* Skip any case ignorable characters */
4611 while (peek < send && is_utf8_case_ignorable(peek)) {
4612 peek += UTF8SKIP(peek);
4613 }
4614
4615 /* If we reached the end of the string without finding any
4616 * non-case ignorable characters, or if the next such one
4617 * is not-cased, then we have met the conditions for it
4618 * being a final sigma with regards to peek ahead, and so
4619 * must do peek behind for the remaining conditions. (We
4620 * know there is stuff behind to look at since we tested
4621 * above that this isn't the first letter) */
4622 if (peek >= send || ! is_utf8_cased(peek)) {
4623 peek = utf8_hop(s, -1);
4624
4625 /* Here are at the beginning of the first character
4626 * before the original upper case sigma. Keep backing
4627 * up, skipping any case ignorable characters */
4628 while (is_utf8_case_ignorable(peek)) {
4629 peek = utf8_hop(peek, -1);
4630 }
4631
4632 /* Here peek points to the first byte of the closest
4633 * non-case-ignorable character before the capital
4634 * sigma. If it is cased, then by the Unicode
4635 * algorithm, we should use a small final sigma instead
4636 * of what we have */
4637 if (is_utf8_cased(peek)) {
4638 STORE_UNI_TO_UTF8_TWO_BYTE(tmpbuf,
4639 UNICODE_GREEK_SMALL_LETTER_FINAL_SIGMA);
4640 }
4641 }
4642 }
4643 else { /* Not a context sensitive mapping */
4644#endif /* End of commented out context sensitive */
4645 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
4646
4647 /* If the eventually required minimum size outgrows
4648 * the available space, we need to grow. */
4649 const UV o = d - (U8*)SvPVX_const(dest);
4650
4651 /* If someone lowercases one million U+0130s we
4652 * SvGROW() one million times. Or we could try
4653 * guessing how much to allocate without allocating too
4654 * much. Such is life. Another option would be to
4655 * grow an extra byte or two more each time we need to
4656 * grow, which would cut down the million to 500K, with
4657 * little waste */
4658 SvGROW(dest, min);
4659 d = (U8*)SvPVX(dest) + o;
4660 }
4661#ifdef CONTEXT_DEPENDENT_CASING
4662 }
4663#endif
4664 /* Copy the newly lowercased letter to the output buffer we're
4665 * building */
4666 Copy(tmpbuf, d, ulen, U8);
4667 d += ulen;
4668 s += u;
4669#ifdef GO_AHEAD_AND_BREAK_USER_DEFINED_CASE_MAPPINGS
4670 }
4671#endif
4672 } /* End of looping through the source string */
ec9af7d4
NC
4673 SvUTF8_on(dest);
4674 *d = '\0';
4675 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
00f254e2 4676 } else { /* Not utf8 */
31351b04 4677 if (len) {
ec9af7d4 4678 const U8 *const send = s + len;
00f254e2
KW
4679
4680 /* Use locale casing if in locale; regular style if not treating
4681 * latin1 as having case; otherwise the latin1 casing. Do the
4682 * whole thing in a tight loop, for speed, */
2de3dbcc 4683 if (IN_LOCALE_RUNTIME) {
31351b04 4684 TAINT;
ec9af7d4
NC
4685 SvTAINTED_on(dest);
4686 for (; s < send; d++, s++)
4687 *d = toLOWER_LC(*s);
31351b04 4688 }
00f254e2
KW
4689 else if (! IN_UNI_8_BIT) {
4690 for (; s < send; d++, s++) {
ec9af7d4 4691 *d = toLOWER(*s);
00f254e2
KW
4692 }
4693 }
4694 else {
4695 for (; s < send; d++, s++) {
4696 *d = toLOWER_LATIN1(*s);
4697 }
31351b04 4698 }
bbce6d69 4699 }
ec9af7d4
NC
4700 if (source != dest) {
4701 *d = '\0';
4702 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4703 }
79072805 4704 }
ec9af7d4 4705 SvSETMAGIC(dest);
79072805
LW
4706 RETURN;
4707}
4708
a0d0e21e 4709PP(pp_quotemeta)
79072805 4710{
97aff369 4711 dVAR; dSP; dTARGET;
1b6737cc 4712 SV * const sv = TOPs;
a0d0e21e 4713 STRLEN len;
0d46e09a 4714 register const char *s = SvPV_const(sv,len);
79072805 4715
7e2040f0 4716 SvUTF8_off(TARG); /* decontaminate */
a0d0e21e 4717 if (len) {
1b6737cc 4718 register char *d;
862a34c6 4719 SvUPGRADE(TARG, SVt_PV);
c07a80fd 4720 SvGROW(TARG, (len * 2) + 1);
a0d0e21e 4721 d = SvPVX(TARG);
7e2040f0 4722 if (DO_UTF8(sv)) {
0dd2cdef 4723 while (len) {
fd400ab9 4724 if (UTF8_IS_CONTINUED(*s)) {
0dd2cdef
LW
4725 STRLEN ulen = UTF8SKIP(s);
4726 if (ulen > len)
4727 ulen = len;
4728 len -= ulen;
4729 while (ulen--)
4730 *d++ = *s++;
4731 }
4732 else {
4733 if (!isALNUM(*s))
4734 *d++ = '\\';
4735 *d++ = *s++;
4736 len--;
4737 }
4738 }
7e2040f0 4739 SvUTF8_on(TARG);
0dd2cdef
LW
4740 }
4741 else {
4742 while (len--) {
4743 if (!isALNUM(*s))
4744 *d++ = '\\';
4745 *d++ = *s++;
4746 }
79072805 4747 }
a0d0e21e 4748 *d = '\0';
349d4f2f 4749 SvCUR_set(TARG, d - SvPVX_const(TARG));
3aa33fe5 4750 (void)SvPOK_only_UTF8(TARG);
79072805 4751 }
a0d0e21e
LW
4752 else
4753 sv_setpvn(TARG, s, len);
ec93b65f 4754 SETTARG;
79072805
LW
4755 RETURN;
4756}
4757
a0d0e21e 4758/* Arrays. */
79072805 4759
a0d0e21e 4760PP(pp_aslice)
79072805 4761{
97aff369 4762 dVAR; dSP; dMARK; dORIGMARK;
502c6561 4763 register AV *const av = MUTABLE_AV(POPs);
1b6737cc 4764 register const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
79072805 4765
a0d0e21e 4766 if (SvTYPE(av) == SVt_PVAV) {
fc15ae8f 4767 const I32 arybase = CopARYBASE_get(PL_curcop);
4ad10a0b
VP
4768 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
4769 bool can_preserve = FALSE;
4770
4771 if (localizing) {
4772 MAGIC *mg;
4773 HV *stash;
4774
4775 can_preserve = SvCANEXISTDELETE(av);
4776 }
4777
4778 if (lval && localizing) {
1b6737cc 4779 register SV **svp;
748a9306 4780 I32 max = -1;
924508f0 4781 for (svp = MARK + 1; svp <= SP; svp++) {
4ea561bc 4782 const I32 elem = SvIV(*svp);
748a9306
LW
4783 if (elem > max)
4784 max = elem;
4785 }
4786 if (max > AvMAX(av))
4787 av_extend(av, max);
4788 }
4ad10a0b 4789
a0d0e21e 4790 while (++MARK <= SP) {
1b6737cc 4791 register SV **svp;
4ea561bc 4792 I32 elem = SvIV(*MARK);
4ad10a0b 4793 bool preeminent = TRUE;
a0d0e21e 4794
748a9306
LW
4795 if (elem > 0)
4796 elem -= arybase;
4ad10a0b
VP
4797 if (localizing && can_preserve) {
4798 /* If we can determine whether the element exist,
4799 * Try to preserve the existenceness of a tied array
4800 * element by using EXISTS and DELETE if possible.
4801 * Fallback to FETCH and STORE otherwise. */
4802 preeminent = av_exists(av, elem);
4803 }
4804
a0d0e21e
LW
4805 svp = av_fetch(av, elem, lval);
4806 if (lval) {
3280af22 4807 if (!svp || *svp == &PL_sv_undef)
cea2e8a9 4808 DIE(aTHX_ PL_no_aelem, elem);
4ad10a0b
VP
4809 if (localizing) {
4810 if (preeminent)
4811 save_aelem(av, elem, svp);
4812 else
4813 SAVEADELETE(av, elem);
4814 }
79072805 4815 }
3280af22 4816 *MARK = svp ? *svp : &PL_sv_undef;
79072805
LW
4817 }
4818 }
748a9306 4819 if (GIMME != G_ARRAY) {
a0d0e21e 4820 MARK = ORIGMARK;
04ab2c87 4821 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
a0d0e21e
LW
4822 SP = MARK;
4823 }
79072805
LW
4824 RETURN;
4825}
4826
cba5a3b0
DG
4827/* Smart dereferencing for keys, values and each */
4828PP(pp_rkeys)
4829{
4830 dVAR;
4831 dSP;
4832 dPOPss;
4833
4834 if (!SvOK(sv))
4835 RETURN;
4836
4837 if (SvROK(sv)) {
4838 SvGETMAGIC(sv);
4839 if (SvAMAGIC(sv)) {
4840 /* N.B.: AMG macros return sv if no overloading is found */
31d632c3
DM
4841 SV *maybe_hv = AMG_CALLunary(sv, to_hv_amg);
4842 SV *maybe_av = AMG_CALLunary(sv, to_av_amg);
cba5a3b0
DG
4843 if ( maybe_hv != sv && maybe_av != sv ) {
4844 Perl_ck_warner(aTHX_ packWARN(WARN_AMBIGUOUS), "%s",
4845 Perl_form(aTHX_ "Ambiguous overloaded argument to %s resolved as %%{}",
4846 PL_op_desc[PL_op->op_type]
4847 )
4848 );
4849 sv = maybe_hv;
4850 }
4851 else if ( maybe_av != sv ) {
4852 if ( SvTYPE(SvRV(sv)) == SVt_PVHV ) {
4853 /* @{} overload, but underlying reftype is HV */
4854 Perl_ck_warner(aTHX_ packWARN(WARN_AMBIGUOUS), "%s",
4855 Perl_form(aTHX_ "Ambiguous overloaded argument to %s resolved as @{}",
4856 PL_op_desc[PL_op->op_type]
4857 )
4858 );
4859 }
4860 sv = maybe_av;
4861 }
4862 else if ( maybe_hv != sv ) {
4863 if ( SvTYPE(SvRV(sv)) == SVt_PVAV ) {
4864 /* %{} overload, but underlying reftype is AV */
4865 Perl_ck_warner(aTHX_ packWARN(WARN_AMBIGUOUS), "%s",
4866 Perl_form(aTHX_ "Ambiguous overloaded argument to %s resolved as %%{}",
4867 PL_op_desc[PL_op->op_type]
4868 )
4869 );
4870 }
4871 sv = maybe_hv;
4872 }
4873 }
4874 sv = SvRV(sv);
4875 }
4876
4877 if ( SvTYPE(sv) != SVt_PVHV && SvTYPE(sv) != SVt_PVAV ) {
4c540399
RGS
4878 DIE(aTHX_ "Type of argument to %s must be hashref or arrayref",
4879 PL_op_desc[PL_op->op_type] );
cba5a3b0
DG
4880 }
4881
4882 /* Delegate to correct function for op type */
4883 PUSHs(sv);
4884 if (PL_op->op_type == OP_RKEYS || PL_op->op_type == OP_RVALUES) {
4885 return (SvTYPE(sv) == SVt_PVHV) ? Perl_do_kv(aTHX) : Perl_pp_akeys(aTHX);
4886 }
4887 else {
4888 return (SvTYPE(sv) == SVt_PVHV) ? Perl_pp_each(aTHX) : Perl_pp_aeach(aTHX);
4889 }
4890}
4891
878d132a
NC
4892PP(pp_aeach)
4893{
4894 dVAR;
4895 dSP;
502c6561 4896 AV *array = MUTABLE_AV(POPs);
878d132a 4897 const I32 gimme = GIMME_V;
453d94a9 4898 IV *iterp = Perl_av_iter_p(aTHX_ array);
878d132a
NC
4899 const IV current = (*iterp)++;
4900
4901 if (current > av_len(array)) {
4902 *iterp = 0;
4903 if (gimme == G_SCALAR)
4904 RETPUSHUNDEF;
4905 else
4906 RETURN;
4907 }
4908
4909 EXTEND(SP, 2);
4910 mPUSHi(CopARYBASE_get(PL_curcop) + current);
4911 if (gimme == G_ARRAY) {
4912 SV **const element = av_fetch(array, current, 0);
4913 PUSHs(element ? *element : &PL_sv_undef);
4914 }
4915 RETURN;
4916}
4917
4918PP(pp_akeys)
4919{
4920 dVAR;
4921 dSP;
502c6561 4922 AV *array = MUTABLE_AV(POPs);
878d132a
NC
4923 const I32 gimme = GIMME_V;
4924
4925 *Perl_av_iter_p(aTHX_ array) = 0;
4926
4927 if (gimme == G_SCALAR) {
4928 dTARGET;
4929 PUSHi(av_len(array) + 1);
4930 }
4931 else if (gimme == G_ARRAY) {
4932 IV n = Perl_av_len(aTHX_ array);
4933 IV i = CopARYBASE_get(PL_curcop);
4934
4935 EXTEND(SP, n + 1);
4936
cba5a3b0 4937 if (PL_op->op_type == OP_AKEYS || PL_op->op_type == OP_RKEYS) {
878d132a
NC
4938 n += i;
4939 for (; i <= n; i++) {
4940 mPUSHi(i);
4941 }
4942 }
4943 else {
4944 for (i = 0; i <= n; i++) {
4945 SV *const *const elem = Perl_av_fetch(aTHX_ array, i, 0);
4946 PUSHs(elem ? *elem : &PL_sv_undef);
4947 }
4948 }
4949 }
4950 RETURN;
4951}
4952
79072805
LW
4953/* Associative arrays. */
4954
4955PP(pp_each)
4956{
97aff369 4957 dVAR;
39644a26 4958 dSP;
85fbaab2 4959 HV * hash = MUTABLE_HV(POPs);
c07a80fd 4960 HE *entry;
f54cb97a 4961 const I32 gimme = GIMME_V;
8ec5e241 4962
c07a80fd 4963 PUTBACK;
c750a3ec 4964 /* might clobber stack_sp */
6d822dc4 4965 entry = hv_iternext(hash);
c07a80fd 4966 SPAGAIN;
79072805 4967
79072805
LW
4968 EXTEND(SP, 2);
4969 if (entry) {
1b6737cc 4970 SV* const sv = hv_iterkeysv(entry);
574c8022 4971 PUSHs(sv); /* won't clobber stack_sp */
54310121 4972 if (gimme == G_ARRAY) {
59af0135 4973 SV *val;
c07a80fd 4974 PUTBACK;
c750a3ec 4975 /* might clobber stack_sp */
6d822dc4 4976 val = hv_iterval(hash, entry);
c07a80fd 4977 SPAGAIN;
59af0135 4978 PUSHs(val);
79072805 4979 }
79072805 4980 }
54310121 4981 else if (gimme == G_SCALAR)
79072805
LW
4982 RETPUSHUNDEF;
4983
4984 RETURN;
4985}
4986
7332a6c4
VP
4987STATIC OP *
4988S_do_delete_local(pTHX)
79072805 4989{
97aff369 4990 dVAR;
39644a26 4991 dSP;
f54cb97a 4992 const I32 gimme = GIMME_V;
7332a6c4
VP
4993 const MAGIC *mg;
4994 HV *stash;
4995
4996 if (PL_op->op_private & OPpSLICE) {
4997 dMARK; dORIGMARK;
4998 SV * const osv = POPs;
4999 const bool tied = SvRMAGICAL(osv)
5000 && mg_find((const SV *)osv, PERL_MAGIC_tied);
5001 const bool can_preserve = SvCANEXISTDELETE(osv)
5002 || mg_find((const SV *)osv, PERL_MAGIC_env);
5003 const U32 type = SvTYPE(osv);
5004 if (type == SVt_PVHV) { /* hash element */
5005 HV * const hv = MUTABLE_HV(osv);
5006 while (++MARK <= SP) {
5007 SV * const keysv = *MARK;
5008 SV *sv = NULL;
5009 bool preeminent = TRUE;
5010 if (can_preserve)
5011 preeminent = hv_exists_ent(hv, keysv, 0);
5012 if (tied) {
5013 HE *he = hv_fetch_ent(hv, keysv, 1, 0);
5014 if (he)
5015 sv = HeVAL(he);
5016 else
5017 preeminent = FALSE;
5018 }
5019 else {
5020 sv = hv_delete_ent(hv, keysv, 0, 0);
5021 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
5022 }
5023 if (preeminent) {
5024 save_helem_flags(hv, keysv, &sv, SAVEf_KEEPOLDELEM);
5025 if (tied) {
5026 *MARK = sv_mortalcopy(sv);
5027 mg_clear(sv);
5028 } else
5029 *MARK = sv;
5030 }
5031 else {
5032 SAVEHDELETE(hv, keysv);
5033 *MARK = &PL_sv_undef;
5034 }
5035 }
5036 }
5037 else if (type == SVt_PVAV) { /* array element */
5038 if (PL_op->op_flags & OPf_SPECIAL) {
5039 AV * const av = MUTABLE_AV(osv);
5040 while (++MARK <= SP) {
5041 I32 idx = SvIV(*MARK);
5042 SV *sv = NULL;
5043 bool preeminent = TRUE;
5044 if (can_preserve)
5045 preeminent = av_exists(av, idx);
5046 if (tied) {
5047 SV **svp = av_fetch(av, idx, 1);
5048 if (svp)
5049 sv = *svp;
5050 else
5051 preeminent = FALSE;
5052 }
5053 else {
5054 sv = av_delete(av, idx, 0);
5055 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
5056 }
5057 if (preeminent) {
5058 save_aelem_flags(av, idx, &sv, SAVEf_KEEPOLDELEM);
5059 if (tied) {
5060 *MARK = sv_mortalcopy(sv);
5061 mg_clear(sv);
5062 } else
5063 *MARK = sv;
5064 }
5065 else {
5066 SAVEADELETE(av, idx);
5067 *MARK = &PL_sv_undef;
5068 }
5069 }
5070 }
5071 }
5072 else
5073 DIE(aTHX_ "Not a HASH reference");
5074 if (gimme == G_VOID)
5075 SP = ORIGMARK;
5076 else if (gimme == G_SCALAR) {
5077 MARK = ORIGMARK;
5078 if (SP > MARK)
5079 *++MARK = *SP;
5080 else
5081 *++MARK = &PL_sv_undef;
5082 SP = MARK;
5083 }
5084 }
5085 else {
5086 SV * const keysv = POPs;
5087 SV * const osv = POPs;
5088 const bool tied = SvRMAGICAL(osv)
5089 && mg_find((const SV *)osv, PERL_MAGIC_tied);
5090 const bool can_preserve = SvCANEXISTDELETE(osv)
5091 || mg_find((const SV *)osv, PERL_MAGIC_env);
5092 const U32 type = SvTYPE(osv);
5093 SV *sv = NULL;
5094 if (type == SVt_PVHV) {
5095 HV * const hv = MUTABLE_HV(osv);
5096 bool preeminent = TRUE;
5097 if (can_preserve)
5098 preeminent = hv_exists_ent(hv, keysv, 0);
5099 if (tied) {
5100 HE *he = hv_fetch_ent(hv, keysv, 1, 0);
5101 if (he)
5102 sv = HeVAL(he);
5103 else
5104 preeminent = FALSE;
5105 }
5106 else {
5107 sv = hv_delete_ent(hv, keysv, 0, 0);
5108 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
5109 }
5110 if (preeminent) {
5111 save_helem_flags(hv, keysv, &sv, SAVEf_KEEPOLDELEM);
5112 if (tied) {
5113 SV *nsv = sv_mortalcopy(sv);
5114 mg_clear(sv);
5115 sv = nsv;
5116 }
5117 }
5118 else
5119 SAVEHDELETE(hv, keysv);
5120 }
5121 else if (type == SVt_PVAV) {
5122 if (PL_op->op_flags & OPf_SPECIAL) {
5123 AV * const av = MUTABLE_AV(osv);
5124 I32 idx = SvIV(keysv);
5125 bool preeminent = TRUE;
5126 if (can_preserve)
5127 preeminent = av_exists(av, idx);
5128 if (tied) {
5129 SV **svp = av_fetch(av, idx, 1);
5130 if (svp)
5131 sv = *svp;
5132 else
5133 preeminent = FALSE;
5134 }
5135 else {
5136 sv = av_delete(av, idx, 0);
5137 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
5138 }
5139 if (preeminent) {
5140 save_aelem_flags(av, idx, &sv, SAVEf_KEEPOLDELEM);
5141 if (tied) {
5142 SV *nsv = sv_mortalcopy(sv);
5143 mg_clear(sv);
5144 sv = nsv;
5145 }
5146 }
5147 else
5148 SAVEADELETE(av, idx);
5149 }
5150 else
5151 DIE(aTHX_ "panic: avhv_delete no longer supported");
5152 }
5153 else
5154 DIE(aTHX_ "Not a HASH reference");
5155 if (!sv)
5156 sv = &PL_sv_undef;
5157 if (gimme != G_VOID)
5158 PUSHs(sv);
5159 }
5160
5161 RETURN;
5162}
5163
5164PP(pp_delete)
5165{
5166 dVAR;
5167 dSP;
5168 I32 gimme;
5169 I32 discard;
5170
5171 if (PL_op->op_private & OPpLVAL_INTRO)
5172 return do_delete_local();
5173
5174 gimme = GIMME_V;
5175 discard = (gimme == G_VOID) ? G_DISCARD : 0;
5f05dabc 5176
533c011a 5177 if (PL_op->op_private & OPpSLICE) {
5f05dabc 5178 dMARK; dORIGMARK;
85fbaab2 5179 HV * const hv = MUTABLE_HV(POPs);
1b6737cc 5180 const U32 hvtype = SvTYPE(hv);
01020589
GS
5181 if (hvtype == SVt_PVHV) { /* hash element */
5182 while (++MARK <= SP) {
1b6737cc 5183 SV * const sv = hv_delete_ent(hv, *MARK, discard, 0);
01020589
GS
5184 *MARK = sv ? sv : &PL_sv_undef;
5185 }
5f05dabc 5186 }
6d822dc4
MS
5187 else if (hvtype == SVt_PVAV) { /* array element */
5188 if (PL_op->op_flags & OPf_SPECIAL) {
5189 while (++MARK <= SP) {
502c6561 5190 SV * const sv = av_delete(MUTABLE_AV(hv), SvIV(*MARK), discard);
6d822dc4
MS
5191 *MARK = sv ? sv : &PL_sv_undef;
5192 }
5193 }
01020589
GS
5194 }
5195 else
5196 DIE(aTHX_ "Not a HASH reference");
54310121 5197 if (discard)
5198 SP = ORIGMARK;
5199 else if (gimme == G_SCALAR) {
5f05dabc 5200 MARK = ORIGMARK;
9111c9c0
DM
5201 if (SP > MARK)
5202 *++MARK = *SP;
5203 else
5204 *++MARK = &PL_sv_undef;
5f05dabc 5205 SP = MARK;
5206 }
5207 }
5208 else {
5209 SV *keysv = POPs;
85fbaab2 5210 HV * const hv = MUTABLE_HV(POPs);
295d248e 5211 SV *sv = NULL;
97fcbf96
MB
5212 if (SvTYPE(hv) == SVt_PVHV)
5213 sv = hv_delete_ent(hv, keysv, discard, 0);
01020589
GS
5214 else if (SvTYPE(hv) == SVt_PVAV) {
5215 if (PL_op->op_flags & OPf_SPECIAL)
502c6561 5216 sv = av_delete(MUTABLE_AV(hv), SvIV(keysv), discard);
af288a60
HS
5217 else
5218 DIE(aTHX_ "panic: avhv_delete no longer supported");
01020589 5219 }
97fcbf96 5220 else
cea2e8a9 5221 DIE(aTHX_ "Not a HASH reference");
5f05dabc 5222 if (!sv)
3280af22 5223 sv = &PL_sv_undef;
54310121 5224 if (!discard)
5225 PUSHs(sv);
79072805 5226 }
79072805
LW
5227 RETURN;
5228}
5229
a0d0e21e 5230PP(pp_exists)
79072805 5231{
97aff369 5232 dVAR;
39644a26 5233 dSP;
afebc493
GS
5234 SV *tmpsv;
5235 HV *hv;
5236
5237 if (PL_op->op_private & OPpEXISTS_SUB) {
5238 GV *gv;
0bd48802 5239 SV * const sv = POPs;
f2c0649b 5240 CV * const cv = sv_2cv(sv, &hv, &gv, 0);
afebc493
GS
5241 if (cv)
5242 RETPUSHYES;
5243 if (gv && isGV(gv) && GvCV(gv) && !GvCVGEN(gv))
5244 RETPUSHYES;
5245 RETPUSHNO;
5246 }
5247 tmpsv = POPs;
85fbaab2 5248 hv = MUTABLE_HV(POPs);
c750a3ec 5249 if (SvTYPE(hv) == SVt_PVHV) {
ae77835f 5250 if (hv_exists_ent(hv, tmpsv, 0))
c750a3ec 5251 RETPUSHYES;
ef54e1a4
JH
5252 }
5253 else if (SvTYPE(hv) == SVt_PVAV) {
01020589 5254 if (PL_op->op_flags & OPf_SPECIAL) { /* array element */
502c6561 5255 if (av_exists(MUTABLE_AV(hv), SvIV(tmpsv)))
01020589
GS
5256 RETPUSHYES;
5257 }
ef54e1a4
JH
5258 }
5259 else {
cea2e8a9 5260 DIE(aTHX_ "Not a HASH reference");
a0d0e21e 5261 }
a0d0e21e
LW
5262 RETPUSHNO;
5263}
79072805 5264
a0d0e21e
LW
5265PP(pp_hslice)
5266{
97aff369 5267 dVAR; dSP; dMARK; dORIGMARK;
85fbaab2 5268 register HV * const hv = MUTABLE_HV(POPs);
1b6737cc
AL
5269 register const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
5270 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
d30e492c 5271 bool can_preserve = FALSE;
79072805 5272
eb85dfd3
DM
5273 if (localizing) {
5274 MAGIC *mg;
5275 HV *stash;
5276
d30e492c
VP
5277 if (SvCANEXISTDELETE(hv) || mg_find((const SV *)hv, PERL_MAGIC_env))
5278 can_preserve = TRUE;
eb85dfd3
DM
5279 }
5280
6d822dc4 5281 while (++MARK <= SP) {
1b6737cc 5282 SV * const keysv = *MARK;
6d822dc4
MS
5283 SV **svp;
5284 HE *he;
d30e492c
VP
5285 bool preeminent = TRUE;
5286
5287 if (localizing && can_preserve) {
5288 /* If we can determine whether the element exist,
5289 * try to preserve the existenceness of a tied hash
5290 * element by using EXISTS and DELETE if possible.
5291 * Fallback to FETCH and STORE otherwise. */
5292 preeminent = hv_exists_ent(hv, keysv, 0);
6d822dc4 5293 }
eb85dfd3 5294
6d822dc4 5295 he = hv_fetch_ent(hv, keysv, lval, 0);
fe5bfecd 5296 svp = he ? &HeVAL(he) : NULL;
eb85dfd3 5297
6d822dc4
MS
5298 if (lval) {
5299 if (!svp || *svp == &PL_sv_undef) {
be2597df 5300 DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
6d822dc4
MS
5301 }
5302 if (localizing) {
7a2e501a 5303 if (HvNAME_get(hv) && isGV(*svp))
159b6efe 5304 save_gp(MUTABLE_GV(*svp), !(PL_op->op_flags & OPf_SPECIAL));
47cfc530
VP
5305 else if (preeminent)
5306 save_helem_flags(hv, keysv, svp,
5307 (PL_op->op_flags & OPf_SPECIAL) ? 0 : SAVEf_SETMAGIC);
5308 else
5309 SAVEHDELETE(hv, keysv);
6d822dc4
MS
5310 }
5311 }
5312 *MARK = svp ? *svp : &PL_sv_undef;
79072805 5313 }
a0d0e21e
LW
5314 if (GIMME != G_ARRAY) {
5315 MARK = ORIGMARK;
04ab2c87 5316 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
a0d0e21e 5317 SP = MARK;
79072805 5318 }
a0d0e21e
LW
5319 RETURN;
5320}
5321
5322/* List operators. */
5323
5324PP(pp_list)
5325{
97aff369 5326 dVAR; dSP; dMARK;
a0d0e21e
LW
5327 if (GIMME != G_ARRAY) {
5328 if (++MARK <= SP)
5329 *MARK = *SP; /* unwanted list, return last item */
8990e307 5330 else
3280af22 5331 *MARK = &PL_sv_undef;
a0d0e21e 5332 SP = MARK;
79072805 5333 }
a0d0e21e 5334 RETURN;
79072805
LW
5335}
5336
a0d0e21e 5337PP(pp_lslice)
79072805 5338{
97aff369 5339 dVAR;
39644a26 5340 dSP;
1b6737cc
AL
5341 SV ** const lastrelem = PL_stack_sp;
5342 SV ** const lastlelem = PL_stack_base + POPMARK;
5343 SV ** const firstlelem = PL_stack_base + POPMARK + 1;
5344 register SV ** const firstrelem = lastlelem + 1;
fc15ae8f 5345 const I32 arybase = CopARYBASE_get(PL_curcop);
42e73ed0 5346 I32 is_something_there = FALSE;
1b6737cc
AL
5347
5348 register const I32 max = lastrelem - lastlelem;
a0d0e21e 5349 register SV **lelem;
a0d0e21e
LW
5350
5351 if (GIMME != G_ARRAY) {
4ea561bc 5352 I32 ix = SvIV(*lastlelem);
748a9306
LW
5353 if (ix < 0)
5354 ix += max;
5355 else
5356 ix -= arybase;
a0d0e21e 5357 if (ix < 0 || ix >= max)
3280af22 5358 *firstlelem = &PL_sv_undef;
a0d0e21e
LW
5359 else
5360 *firstlelem = firstrelem[ix];
5361 SP = firstlelem;
5362 RETURN;
5363 }
5364
5365 if (max == 0) {
5366 SP = firstlelem - 1;
5367 RETURN;
5368 }
5369
5370 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
4ea561bc 5371 I32 ix = SvIV(*lelem);
c73bf8e3 5372 if (ix < 0)
a0d0e21e 5373 ix += max;
b13b2135 5374 else
748a9306 5375 ix -= arybase;
c73bf8e3
HS
5376 if (ix < 0 || ix >= max)
5377 *lelem = &PL_sv_undef;
5378 else {
5379 is_something_there = TRUE;
5380 if (!(*lelem = firstrelem[ix]))
3280af22 5381 *lelem = &PL_sv_undef;
748a9306 5382 }
79072805 5383 }
4633a7c4
LW
5384 if (is_something_there)
5385 SP = lastlelem;
5386 else
5387 SP = firstlelem - 1;
79072805
LW
5388 RETURN;
5389}
5390
a0d0e21e
LW
5391PP(pp_anonlist)
5392{
97aff369 5393 dVAR; dSP; dMARK; dORIGMARK;
1b6737cc 5394 const I32 items = SP - MARK;
ad64d0ec 5395 SV * const av = MUTABLE_SV(av_make(items, MARK+1));
44a8e56a 5396 SP = ORIGMARK; /* av_make() might realloc stack_sp */
6e449a3a
MHM
5397 mXPUSHs((PL_op->op_flags & OPf_SPECIAL)
5398 ? newRV_noinc(av) : av);
a0d0e21e
LW
5399 RETURN;
5400}
5401
5402PP(pp_anonhash)
79072805 5403{
97aff369 5404 dVAR; dSP; dMARK; dORIGMARK;
78c72037 5405 HV* const hv = newHV();
a0d0e21e
LW
5406
5407 while (MARK < SP) {
1b6737cc 5408 SV * const key = *++MARK;
561b68a9 5409 SV * const val = newSV(0);
a0d0e21e
LW
5410 if (MARK < SP)
5411 sv_setsv(val, *++MARK);
a2a5de95
NC
5412 else
5413 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Odd number of elements in anonymous hash");
f12c7020 5414 (void)hv_store_ent(hv,key,val,0);
79072805 5415 }
a0d0e21e 5416 SP = ORIGMARK;
6e449a3a 5417 mXPUSHs((PL_op->op_flags & OPf_SPECIAL)
ad64d0ec 5418 ? newRV_noinc(MUTABLE_SV(hv)) : MUTABLE_SV(hv));
79072805
LW
5419 RETURN;
5420}
5421
a0d0e21e 5422PP(pp_splice)
79072805 5423{
27da23d5 5424 dVAR; dSP; dMARK; dORIGMARK;
502c6561 5425 register AV *ary = MUTABLE_AV(*++MARK);
a0d0e21e
LW
5426 register SV **src;
5427 register SV **dst;
5428 register I32 i;
5429 register I32 offset;
5430 register I32 length;
5431 I32 newlen;
5432 I32 after;
5433 I32 diff;
ad64d0ec 5434 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
93965878 5435
1b6737cc 5436 if (mg) {
af71faff
NC
5437 return Perl_tied_method(aTHX_ "SPLICE", mark - 1, MUTABLE_SV(ary), mg,
5438 GIMME_V | TIED_METHOD_ARGUMENTS_ON_STACK,
5439 sp - mark);
93965878 5440 }
79072805 5441
a0d0e21e 5442 SP++;
79072805 5443
a0d0e21e 5444 if (++MARK < SP) {
4ea561bc 5445 offset = i = SvIV(*MARK);
a0d0e21e 5446 if (offset < 0)
93965878 5447 offset += AvFILLp(ary) + 1;
a0d0e21e 5448 else
fc15ae8f 5449 offset -= CopARYBASE_get(PL_curcop);
84902520 5450 if (offset < 0)
cea2e8a9 5451 DIE(aTHX_ PL_no_aelem, i);
a0d0e21e
LW
5452 if (++MARK < SP) {
5453 length = SvIVx(*MARK++);
48cdf507
GA
5454 if (length < 0) {
5455 length += AvFILLp(ary) - offset + 1;
5456 if (length < 0)
5457 length = 0;
5458 }
79072805
LW
5459 }
5460 else
a0d0e21e 5461 length = AvMAX(ary) + 1; /* close enough to infinity */
79072805 5462 }
a0d0e21e
LW
5463 else {
5464 offset = 0;
5465 length = AvMAX(ary) + 1;
5466 }
8cbc2e3b 5467 if (offset > AvFILLp(ary) + 1) {
a2a5de95 5468 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "splice() offset past end of array" );
93965878 5469 offset = AvFILLp(ary) + 1;
8cbc2e3b 5470 }
93965878 5471 after = AvFILLp(ary) + 1 - (offset + length);
a0d0e21e
LW
5472 if (after < 0) { /* not that much array */
5473 length += after; /* offset+length now in array */
5474 after = 0;
5475 if (!AvALLOC(ary))
5476 av_extend(ary, 0);
5477 }
5478
5479 /* At this point, MARK .. SP-1 is our new LIST */
5480
5481 newlen = SP - MARK;
5482 diff = newlen - length;
13d7cbc1
GS
5483 if (newlen && !AvREAL(ary) && AvREIFY(ary))
5484 av_reify(ary);
a0d0e21e 5485
50528de0
WL
5486 /* make new elements SVs now: avoid problems if they're from the array */
5487 for (dst = MARK, i = newlen; i; i--) {
1b6737cc 5488 SV * const h = *dst;
f2b990bf 5489 *dst++ = newSVsv(h);
50528de0
WL
5490 }
5491
a0d0e21e 5492 if (diff < 0) { /* shrinking the area */
95b63a38 5493 SV **tmparyval = NULL;
a0d0e21e 5494 if (newlen) {
a02a5408 5495 Newx(tmparyval, newlen, SV*); /* so remember insertion */
a0d0e21e 5496 Copy(MARK, tmparyval, newlen, SV*);
79072805 5497 }
a0d0e21e
LW
5498
5499 MARK = ORIGMARK + 1;
5500 if (GIMME == G_ARRAY) { /* copy return vals to stack */
5501 MEXTEND(MARK, length);
5502 Copy(AvARRAY(ary)+offset, MARK, length, SV*);
5503 if (AvREAL(ary)) {
bbce6d69 5504 EXTEND_MORTAL(length);
36477c24 5505 for (i = length, dst = MARK; i; i--) {
486ec47a 5506 sv_2mortal(*dst); /* free them eventually */
36477c24 5507 dst++;
5508 }
a0d0e21e
LW
5509 }
5510 MARK += length - 1;
79072805 5511 }
a0d0e21e
LW
5512 else {
5513 *MARK = AvARRAY(ary)[offset+length-1];
5514 if (AvREAL(ary)) {
d689ffdd 5515 sv_2mortal(*MARK);
a0d0e21e
LW
5516 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
5517 SvREFCNT_dec(*dst++); /* free them now */
79072805 5518 }
a0d0e21e 5519 }
93965878 5520 AvFILLp(ary) += diff;
a0d0e21e
LW
5521
5522 /* pull up or down? */
5523
5524 if (offset < after) { /* easier to pull up */
5525 if (offset) { /* esp. if nothing to pull */
5526 src = &AvARRAY(ary)[offset-1];
5527 dst = src - diff; /* diff is negative */
5528 for (i = offset; i > 0; i--) /* can't trust Copy */
5529 *dst-- = *src--;
79072805 5530 }
a0d0e21e 5531 dst = AvARRAY(ary);
9c6bc640 5532 AvARRAY(ary) = AvARRAY(ary) - diff; /* diff is negative */
a0d0e21e
LW
5533 AvMAX(ary) += diff;
5534 }
5535 else {
5536 if (after) { /* anything to pull down? */
5537 src = AvARRAY(ary) + offset + length;
5538 dst = src + diff; /* diff is negative */
5539 Move(src, dst, after, SV*);
79072805 5540 }
93965878 5541 dst = &AvARRAY(ary)[AvFILLp(ary)+1];
a0d0e21e
LW
5542 /* avoid later double free */
5543 }
5544 i = -diff;
5545 while (i)
3280af22 5546 dst[--i] = &PL_sv_undef;
a0d0e21e
LW
5547
5548 if (newlen) {
50528de0 5549 Copy( tmparyval, AvARRAY(ary) + offset, newlen, SV* );
a0d0e21e
LW
5550 Safefree(tmparyval);
5551 }
5552 }
5553 else { /* no, expanding (or same) */
d3961450 5554 SV** tmparyval = NULL;
a0d0e21e 5555 if (length) {
a02a5408 5556 Newx(tmparyval, length, SV*); /* so remember deletion */
a0d0e21e
LW
5557 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
5558 }
5559
5560 if (diff > 0) { /* expanding */
a0d0e21e 5561 /* push up or down? */
a0d0e21e
LW
5562 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
5563 if (offset) {
5564 src = AvARRAY(ary);
5565 dst = src - diff;
5566 Move(src, dst, offset, SV*);
79072805 5567 }
9c6bc640 5568 AvARRAY(ary) = AvARRAY(ary) - diff;/* diff is positive */
a0d0e21e 5569 AvMAX(ary) += diff;
93965878 5570 AvFILLp(ary) += diff;
79072805
LW
5571 }
5572 else {
93965878
NIS
5573 if (AvFILLp(ary) + diff >= AvMAX(ary)) /* oh, well */
5574 av_extend(ary, AvFILLp(ary) + diff);
5575 AvFILLp(ary) += diff;
a0d0e21e
LW
5576
5577 if (after) {
93965878 5578 dst = AvARRAY(ary) + AvFILLp(ary);
a0d0e21e
LW
5579 src = dst - diff;
5580 for (i = after; i; i--) {
5581 *dst-- = *src--;
5582 }
79072805
LW
5583 }
5584 }
a0d0e21e
LW
5585 }
5586
50528de0
WL
5587 if (newlen) {
5588 Copy( MARK, AvARRAY(ary) + offset, newlen, SV* );
a0d0e21e 5589 }
50528de0 5590
a0d0e21e
LW
5591 MARK = ORIGMARK + 1;
5592 if (GIMME == G_ARRAY) { /* copy return vals to stack */
5593 if (length) {
5594 Copy(tmparyval, MARK, length, SV*);
5595 if (AvREAL(ary)) {
bbce6d69 5596 EXTEND_MORTAL(length);
36477c24 5597 for (i = length, dst = MARK; i; i--) {
486ec47a 5598 sv_2mortal(*dst); /* free them eventually */
36477c24 5599 dst++;
5600 }
79072805
LW
5601 }
5602 }
a0d0e21e
LW
5603 MARK += length - 1;
5604 }
5605 else if (length--) {
5606 *MARK = tmparyval[length];
5607 if (AvREAL(ary)) {
d689ffdd 5608 sv_2mortal(*MARK);
a0d0e21e
LW
5609 while (length-- > 0)
5610 SvREFCNT_dec(tmparyval[length]);
79072805 5611 }
79072805 5612 }
a0d0e21e 5613 else
3280af22 5614 *MARK = &PL_sv_undef;
d3961450 5615 Safefree(tmparyval);
79072805 5616 }
474af990
FR
5617
5618 if (SvMAGICAL(ary))
5619 mg_set(MUTABLE_SV(ary));
5620
a0d0e21e 5621 SP = MARK;
79072805
LW
5622 RETURN;
5623}
5624
a0d0e21e 5625PP(pp_push)
79072805 5626{
27da23d5 5627 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
502c6561 5628 register AV * const ary = MUTABLE_AV(*++MARK);
ad64d0ec 5629 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
79072805 5630
1b6737cc 5631 if (mg) {
ad64d0ec 5632 *MARK-- = SvTIED_obj(MUTABLE_SV(ary), mg);
93965878
NIS
5633 PUSHMARK(MARK);
5634 PUTBACK;
d343c3ef 5635 ENTER_with_name("call_PUSH");
864dbfa3 5636 call_method("PUSH",G_SCALAR|G_DISCARD);
d343c3ef 5637 LEAVE_with_name("call_PUSH");
93965878 5638 SPAGAIN;
93965878 5639 }
a60c0954 5640 else {
89c14e2e 5641 PL_delaymagic = DM_DELAY;
a60c0954 5642 for (++MARK; MARK <= SP; MARK++) {
561b68a9 5643 SV * const sv = newSV(0);
a60c0954
NIS
5644 if (*MARK)
5645 sv_setsv(sv, *MARK);
0a75904b 5646 av_store(ary, AvFILLp(ary)+1, sv);
a60c0954 5647 }
354b0578 5648 if (PL_delaymagic & DM_ARRAY_ISA)
ad64d0ec 5649 mg_set(MUTABLE_SV(ary));
89c14e2e
BB
5650
5651 PL_delaymagic = 0;
6eeabd23
VP
5652 }
5653 SP = ORIGMARK;
5654 if (OP_GIMME(PL_op, 0) != G_VOID) {
5655 PUSHi( AvFILL(ary) + 1 );
79072805 5656 }
79072805
LW
5657 RETURN;
5658}
5659
a0d0e21e 5660PP(pp_shift)
79072805 5661{
97aff369 5662 dVAR;
39644a26 5663 dSP;
538f5756
RZ
5664 AV * const av = PL_op->op_flags & OPf_SPECIAL
5665 ? MUTABLE_AV(GvAV(PL_defgv)) : MUTABLE_AV(POPs);
789b4bc9 5666 SV * const sv = PL_op->op_type == OP_SHIFT ? av_shift(av) : av_pop(av);
79072805 5667 EXTEND(SP, 1);
c2b4a044 5668 assert (sv);
d689ffdd 5669 if (AvREAL(av))
a0d0e21e
LW
5670 (void)sv_2mortal(sv);
5671 PUSHs(sv);
79072805 5672 RETURN;
79072805
LW
5673}
5674
a0d0e21e 5675PP(pp_unshift)
79072805 5676{
27da23d5 5677 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
502c6561 5678 register AV *ary = MUTABLE_AV(*++MARK);
ad64d0ec 5679 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
93965878 5680
1b6737cc 5681 if (mg) {
ad64d0ec 5682 *MARK-- = SvTIED_obj(MUTABLE_SV(ary), mg);
7fd66d9d 5683 PUSHMARK(MARK);
93965878 5684 PUTBACK;
d343c3ef 5685 ENTER_with_name("call_UNSHIFT");
864dbfa3 5686 call_method("UNSHIFT",G_SCALAR|G_DISCARD);
d343c3ef 5687 LEAVE_with_name("call_UNSHIFT");
93965878 5688 SPAGAIN;
93965878 5689 }
a60c0954 5690 else {
1b6737cc 5691 register I32 i = 0;
a60c0954
NIS
5692 av_unshift(ary, SP - MARK);
5693 while (MARK < SP) {
1b6737cc 5694 SV * const sv = newSVsv(*++MARK);
a60c0954
NIS
5695 (void)av_store(ary, i++, sv);
5696 }
79072805 5697 }
a0d0e21e 5698 SP = ORIGMARK;
6eeabd23 5699 if (OP_GIMME(PL_op, 0) != G_VOID) {
5658d0a9
LR
5700 PUSHi( AvFILL(ary) + 1 );
5701 }
79072805 5702 RETURN;
79072805
LW
5703}
5704
a0d0e21e 5705PP(pp_reverse)
79072805 5706{
97aff369 5707 dVAR; dSP; dMARK;
79072805 5708
a0d0e21e 5709 if (GIMME == G_ARRAY) {
484c818f
VP
5710 if (PL_op->op_private & OPpREVERSE_INPLACE) {
5711 AV *av;
5712
5713 /* See pp_sort() */
5714 assert( MARK+1 == SP && *SP && SvTYPE(*SP) == SVt_PVAV);
5715 (void)POPMARK; /* remove mark associated with ex-OP_AASSIGN */
5716 av = MUTABLE_AV((*SP));
5717 /* In-place reversing only happens in void context for the array
5718 * assignment. We don't need to push anything on the stack. */
5719 SP = MARK;
5720
5721 if (SvMAGICAL(av)) {
5722 I32 i, j;
5723 register SV *tmp = sv_newmortal();
5724 /* For SvCANEXISTDELETE */
5725 HV *stash;
5726 const MAGIC *mg;
5727 bool can_preserve = SvCANEXISTDELETE(av);
5728
5729 for (i = 0, j = av_len(av); i < j; ++i, --j) {
5730 register SV *begin, *end;
5731
5732 if (can_preserve) {
5733 if (!av_exists(av, i)) {
5734 if (av_exists(av, j)) {
5735 register SV *sv = av_delete(av, j, 0);
5736 begin = *av_fetch(av, i, TRUE);
5737 sv_setsv_mg(begin, sv);
5738 }
5739 continue;
5740 }
5741 else if (!av_exists(av, j)) {
5742 register SV *sv = av_delete(av, i, 0);
5743 end = *av_fetch(av, j, TRUE);
5744 sv_setsv_mg(end, sv);
5745 continue;
5746 }
5747 }
5748
5749 begin = *av_fetch(av, i, TRUE);
5750 end = *av_fetch(av, j, TRUE);
5751 sv_setsv(tmp, begin);
5752 sv_setsv_mg(begin, end);
5753 sv_setsv_mg(end, tmp);
5754 }
5755 }
5756 else {
5757 SV **begin = AvARRAY(av);
484c818f 5758
95a26d8e
VP
5759 if (begin) {
5760 SV **end = begin + AvFILLp(av);
5761
5762 while (begin < end) {
5763 register SV * const tmp = *begin;
5764 *begin++ = *end;
5765 *end-- = tmp;
5766 }
484c818f
VP
5767 }
5768 }
5769 }
5770 else {
5771 SV **oldsp = SP;
5772 MARK++;
5773 while (MARK < SP) {
5774 register SV * const tmp = *MARK;
5775 *MARK++ = *SP;
5776 *SP-- = tmp;
5777 }
5778 /* safe as long as stack cannot get extended in the above */
5779 SP = oldsp;
a0d0e21e 5780 }
79072805
LW
5781 }
5782 else {
a0d0e21e
LW
5783 register char *up;
5784 register char *down;
5785 register I32 tmp;
5786 dTARGET;
5787 STRLEN len;
79072805 5788
7e2040f0 5789 SvUTF8_off(TARG); /* decontaminate */
a0d0e21e 5790 if (SP - MARK > 1)
3280af22 5791 do_join(TARG, &PL_sv_no, MARK, SP);
1e21d011 5792 else {
789bd863 5793 sv_setsv(TARG, SP > MARK ? *SP : find_rundefsv());
1e21d011
B
5794 if (! SvOK(TARG) && ckWARN(WARN_UNINITIALIZED))
5795 report_uninit(TARG);
5796 }
5797
a0d0e21e
LW
5798 up = SvPV_force(TARG, len);
5799 if (len > 1) {
7e2040f0 5800 if (DO_UTF8(TARG)) { /* first reverse each character */
dfe13c55 5801 U8* s = (U8*)SvPVX(TARG);
349d4f2f 5802 const U8* send = (U8*)(s + len);
a0ed51b3 5803 while (s < send) {
d742c382 5804 if (UTF8_IS_INVARIANT(*s)) {
a0ed51b3
LW
5805 s++;
5806 continue;
5807 }
5808 else {
9041c2e3 5809 if (!utf8_to_uvchr(s, 0))
a0dbb045 5810 break;
dfe13c55 5811 up = (char*)s;
a0ed51b3 5812 s += UTF8SKIP(s);
dfe13c55 5813 down = (char*)(s - 1);
a0dbb045 5814 /* reverse this character */
a0ed51b3
LW
5815 while (down > up) {
5816 tmp = *up;
5817 *up++ = *down;
eb160463 5818 *down-- = (char)tmp;
a0ed51b3
LW
5819 }
5820 }
5821 }
5822 up = SvPVX(TARG);
5823 }
a0d0e21e
LW
5824 down = SvPVX(TARG) + len - 1;
5825 while (down > up) {
5826 tmp = *up;
5827 *up++ = *down;
eb160463 5828 *down-- = (char)tmp;
a0d0e21e 5829 }
3aa33fe5 5830 (void)SvPOK_only_UTF8(TARG);
79072805 5831 }
a0d0e21e
LW
5832 SP = MARK + 1;
5833 SETTARG;
79072805 5834 }
a0d0e21e 5835 RETURN;
79072805
LW
5836}
5837
a0d0e21e 5838PP(pp_split)
79072805 5839{
27da23d5 5840 dVAR; dSP; dTARG;
a0d0e21e 5841 AV *ary;
467f0320 5842 register IV limit = POPi; /* note, negative is forever */
1b6737cc 5843 SV * const sv = POPs;
a0d0e21e 5844 STRLEN len;
727b7506 5845 register const char *s = SvPV_const(sv, len);
1b6737cc 5846 const bool do_utf8 = DO_UTF8(sv);
727b7506 5847 const char *strend = s + len;
44a8e56a 5848 register PMOP *pm;
d9f97599 5849 register REGEXP *rx;
a0d0e21e 5850 register SV *dstr;
727b7506 5851 register const char *m;
a0d0e21e 5852 I32 iters = 0;
bb7a0f54 5853 const STRLEN slen = do_utf8 ? utf8_length((U8*)s, (U8*)strend) : (STRLEN)(strend - s);
792b2c16 5854 I32 maxiters = slen + 10;
c1a7495a 5855 I32 trailing_empty = 0;
727b7506 5856 const char *orig;
1b6737cc 5857 const I32 origlimit = limit;
a0d0e21e
LW
5858 I32 realarray = 0;
5859 I32 base;
f54cb97a 5860 const I32 gimme = GIMME_V;
941446f6 5861 bool gimme_scalar;
f54cb97a 5862 const I32 oldsave = PL_savestack_ix;
437d3b4e 5863 U32 make_mortal = SVs_TEMP;
7fba1cd6 5864 bool multiline = 0;
b37c2d43 5865 MAGIC *mg = NULL;
79072805 5866
44a8e56a 5867#ifdef DEBUGGING
5868 Copy(&LvTARGOFF(POPs), &pm, 1, PMOP*);
5869#else
5870 pm = (PMOP*)POPs;
5871#endif
a0d0e21e 5872 if (!pm || !s)
2269b42e 5873 DIE(aTHX_ "panic: pp_split");
aaa362c4 5874 rx = PM_GETRE(pm);
bbce6d69 5875
a62b1201 5876 TAINT_IF(get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET &&
07bc277f 5877 (RX_EXTFLAGS(rx) & (RXf_WHITE | RXf_SKIPWHITE)));
bbce6d69 5878
a30b2f1f 5879 RX_MATCH_UTF8_set(rx, do_utf8);
d9f424b2 5880
971a9dd3 5881#ifdef USE_ITHREADS
20e98b0f 5882 if (pm->op_pmreplrootu.op_pmtargetoff) {
159b6efe 5883 ary = GvAVn(MUTABLE_GV(PAD_SVl(pm->op_pmreplrootu.op_pmtargetoff)));
20e98b0f 5884 }
971a9dd3 5885#else
20e98b0f
NC
5886 if (pm->op_pmreplrootu.op_pmtargetgv) {
5887 ary = GvAVn(pm->op_pmreplrootu.op_pmtargetgv);
971a9dd3 5888 }
20e98b0f 5889#endif
79072805 5890 else
7d49f689 5891 ary = NULL;
a0d0e21e
LW
5892 if (ary && (gimme != G_ARRAY || (pm->op_pmflags & PMf_ONCE))) {
5893 realarray = 1;
8ec5e241 5894 PUTBACK;
a0d0e21e
LW
5895 av_extend(ary,0);
5896 av_clear(ary);
8ec5e241 5897 SPAGAIN;
ad64d0ec 5898 if ((mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied))) {
8ec5e241 5899 PUSHMARK(SP);
ad64d0ec 5900 XPUSHs(SvTIED_obj(MUTABLE_SV(ary), mg));
8ec5e241
NIS
5901 }
5902 else {
1c0b011c 5903 if (!AvREAL(ary)) {
1b6737cc 5904 I32 i;
1c0b011c 5905 AvREAL_on(ary);
abff13bb 5906 AvREIFY_off(ary);
1c0b011c 5907 for (i = AvFILLp(ary); i >= 0; i--)
3280af22 5908 AvARRAY(ary)[i] = &PL_sv_undef; /* don't free mere refs */
1c0b011c
NIS
5909 }
5910 /* temporarily switch stacks */
8b7059b1 5911 SAVESWITCHSTACK(PL_curstack, ary);
8ec5e241 5912 make_mortal = 0;
1c0b011c 5913 }
79072805 5914 }
3280af22 5915 base = SP - PL_stack_base;
a0d0e21e 5916 orig = s;
07bc277f 5917 if (RX_EXTFLAGS(rx) & RXf_SKIPWHITE) {
613f191e
TS
5918 if (do_utf8) {
5919 while (*s == ' ' || is_utf8_space((U8*)s))
5920 s += UTF8SKIP(s);
5921 }
a62b1201 5922 else if (get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET) {
bbce6d69 5923 while (isSPACE_LC(*s))
5924 s++;
5925 }
5926 else {
5927 while (isSPACE(*s))
5928 s++;
5929 }
a0d0e21e 5930 }
73134a2e 5931 if (RX_EXTFLAGS(rx) & RXf_PMf_MULTILINE) {
7fba1cd6 5932 multiline = 1;
c07a80fd 5933 }
5934
941446f6
FC
5935 gimme_scalar = gimme == G_SCALAR && !ary;
5936
a0d0e21e
LW
5937 if (!limit)
5938 limit = maxiters + 2;
07bc277f 5939 if (RX_EXTFLAGS(rx) & RXf_WHITE) {
a0d0e21e 5940 while (--limit) {
bbce6d69 5941 m = s;
8727f688
YO
5942 /* this one uses 'm' and is a negative test */
5943 if (do_utf8) {
613f191e
TS
5944 while (m < strend && !( *m == ' ' || is_utf8_space((U8*)m) )) {
5945 const int t = UTF8SKIP(m);
5946 /* is_utf8_space returns FALSE for malform utf8 */
5947 if (strend - m < t)
5948 m = strend;
5949 else
5950 m += t;
5951 }
a62b1201
KW
5952 }
5953 else if (get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET) {
8727f688
YO
5954 while (m < strend && !isSPACE_LC(*m))
5955 ++m;
5956 } else {
5957 while (m < strend && !isSPACE(*m))
5958 ++m;
5959 }
a0d0e21e
LW
5960 if (m >= strend)
5961 break;
bbce6d69 5962
c1a7495a
BB
5963 if (gimme_scalar) {
5964 iters++;
5965 if (m-s == 0)
5966 trailing_empty++;
5967 else
5968 trailing_empty = 0;
5969 } else {
5970 dstr = newSVpvn_flags(s, m-s,
5971 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5972 XPUSHs(dstr);
5973 }
bbce6d69 5974
613f191e
TS
5975 /* skip the whitespace found last */
5976 if (do_utf8)
5977 s = m + UTF8SKIP(m);
5978 else
5979 s = m + 1;
5980
8727f688
YO
5981 /* this one uses 's' and is a positive test */
5982 if (do_utf8) {
613f191e 5983 while (s < strend && ( *s == ' ' || is_utf8_space((U8*)s) ))
8727f688 5984 s += UTF8SKIP(s);
a62b1201
KW
5985 }
5986 else if (get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET) {
8727f688
YO
5987 while (s < strend && isSPACE_LC(*s))
5988 ++s;
5989 } else {
5990 while (s < strend && isSPACE(*s))
5991 ++s;
5992 }
79072805
LW
5993 }
5994 }
07bc277f 5995 else if (RX_EXTFLAGS(rx) & RXf_START_ONLY) {
a0d0e21e 5996 while (--limit) {
a6e20a40
AL
5997 for (m = s; m < strend && *m != '\n'; m++)
5998 ;
a0d0e21e
LW
5999 m++;
6000 if (m >= strend)
6001 break;
c1a7495a
BB
6002
6003 if (gimme_scalar) {
6004 iters++;
6005 if (m-s == 0)
6006 trailing_empty++;
6007 else
6008 trailing_empty = 0;
6009 } else {
6010 dstr = newSVpvn_flags(s, m-s,
6011 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
6012 XPUSHs(dstr);
6013 }
a0d0e21e
LW
6014 s = m;
6015 }
6016 }
07bc277f 6017 else if (RX_EXTFLAGS(rx) & RXf_NULL && !(s >= strend)) {
640f820d
AB
6018 /*
6019 Pre-extend the stack, either the number of bytes or
6020 characters in the string or a limited amount, triggered by:
6021
6022 my ($x, $y) = split //, $str;
6023 or
6024 split //, $str, $i;
6025 */
c1a7495a
BB
6026 if (!gimme_scalar) {
6027 const U32 items = limit - 1;
6028 if (items < slen)
6029 EXTEND(SP, items);
6030 else
6031 EXTEND(SP, slen);
6032 }
640f820d 6033
e9515b0f
AB
6034 if (do_utf8) {
6035 while (--limit) {
6036 /* keep track of how many bytes we skip over */
6037 m = s;
640f820d 6038 s += UTF8SKIP(s);
c1a7495a
BB
6039 if (gimme_scalar) {
6040 iters++;
6041 if (s-m == 0)
6042 trailing_empty++;
6043 else
6044 trailing_empty = 0;
6045 } else {
6046 dstr = newSVpvn_flags(m, s-m, SVf_UTF8 | make_mortal);
640f820d 6047
c1a7495a
BB
6048 PUSHs(dstr);
6049 }
640f820d 6050
e9515b0f
AB
6051 if (s >= strend)
6052 break;
6053 }
6054 } else {
6055 while (--limit) {
c1a7495a
BB
6056 if (gimme_scalar) {
6057 iters++;
6058 } else {
6059 dstr = newSVpvn(s, 1);
e9515b0f 6060
e9515b0f 6061
c1a7495a
BB
6062 if (make_mortal)
6063 sv_2mortal(dstr);
640f820d 6064
c1a7495a
BB
6065 PUSHs(dstr);
6066 }
6067
6068 s++;
e9515b0f
AB
6069
6070 if (s >= strend)
6071 break;
6072 }
640f820d
AB
6073 }
6074 }
3c8556c3 6075 else if (do_utf8 == (RX_UTF8(rx) != 0) &&
07bc277f
NC
6076 (RX_EXTFLAGS(rx) & RXf_USE_INTUIT) && !RX_NPARENS(rx)
6077 && (RX_EXTFLAGS(rx) & RXf_CHECK_ALL)
6078 && !(RX_EXTFLAGS(rx) & RXf_ANCH)) {
6079 const int tail = (RX_EXTFLAGS(rx) & RXf_INTUIT_TAIL);
f9f4320a 6080 SV * const csv = CALLREG_INTUIT_STRING(rx);
cf93c79d 6081
07bc277f 6082 len = RX_MINLENRET(rx);
3c8556c3 6083 if (len == 1 && !RX_UTF8(rx) && !tail) {
1b6737cc 6084 const char c = *SvPV_nolen_const(csv);
a0d0e21e 6085 while (--limit) {
a6e20a40
AL
6086 for (m = s; m < strend && *m != c; m++)
6087 ;
a0d0e21e
LW
6088 if (m >= strend)
6089 break;
c1a7495a
BB
6090 if (gimme_scalar) {
6091 iters++;
6092 if (m-s == 0)
6093 trailing_empty++;
6094 else
6095 trailing_empty = 0;
6096 } else {
6097 dstr = newSVpvn_flags(s, m-s,
6098 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
6099 XPUSHs(dstr);
6100 }
93f04dac
JH
6101 /* The rx->minlen is in characters but we want to step
6102 * s ahead by bytes. */
1aa99e6b
IH
6103 if (do_utf8)
6104 s = (char*)utf8_hop((U8*)m, len);
6105 else
6106 s = m + len; /* Fake \n at the end */
a0d0e21e
LW
6107 }
6108 }
6109 else {
a0d0e21e 6110 while (s < strend && --limit &&
f722798b 6111 (m = fbm_instr((unsigned char*)s, (unsigned char*)strend,
7fba1cd6 6112 csv, multiline ? FBMrf_MULTILINE : 0)) )
a0d0e21e 6113 {
c1a7495a
BB
6114 if (gimme_scalar) {
6115 iters++;
6116 if (m-s == 0)
6117 trailing_empty++;
6118 else
6119 trailing_empty = 0;
6120 } else {
6121 dstr = newSVpvn_flags(s, m-s,
6122 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
6123 XPUSHs(dstr);
6124 }
93f04dac
JH
6125 /* The rx->minlen is in characters but we want to step
6126 * s ahead by bytes. */
1aa99e6b
IH
6127 if (do_utf8)
6128 s = (char*)utf8_hop((U8*)m, len);
6129 else
6130 s = m + len; /* Fake \n at the end */
a0d0e21e 6131 }
463ee0b2 6132 }
463ee0b2 6133 }
a0d0e21e 6134 else {
07bc277f 6135 maxiters += slen * RX_NPARENS(rx);
080c2dec 6136 while (s < strend && --limit)
bbce6d69 6137 {
1b6737cc 6138 I32 rex_return;
080c2dec 6139 PUTBACK;
f9f4320a 6140 rex_return = CALLREGEXEC(rx, (char*)s, (char*)strend, (char*)orig, 1 ,
727b7506 6141 sv, NULL, 0);
080c2dec 6142 SPAGAIN;
1b6737cc 6143 if (rex_return == 0)
080c2dec 6144 break;
d9f97599 6145 TAINT_IF(RX_MATCH_TAINTED(rx));
07bc277f 6146 if (RX_MATCH_COPIED(rx) && RX_SUBBEG(rx) != orig) {
a0d0e21e
LW
6147 m = s;
6148 s = orig;
07bc277f 6149 orig = RX_SUBBEG(rx);
a0d0e21e
LW
6150 s = orig + (m - s);
6151 strend = s + (strend - m);
6152 }
07bc277f 6153 m = RX_OFFS(rx)[0].start + orig;
c1a7495a
BB
6154
6155 if (gimme_scalar) {
6156 iters++;
6157 if (m-s == 0)
6158 trailing_empty++;
6159 else
6160 trailing_empty = 0;
6161 } else {
6162 dstr = newSVpvn_flags(s, m-s,
6163 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
6164 XPUSHs(dstr);
6165 }
07bc277f 6166 if (RX_NPARENS(rx)) {
1b6737cc 6167 I32 i;
07bc277f
NC
6168 for (i = 1; i <= (I32)RX_NPARENS(rx); i++) {
6169 s = RX_OFFS(rx)[i].start + orig;
6170 m = RX_OFFS(rx)[i].end + orig;
6de67870
JP
6171
6172 /* japhy (07/27/01) -- the (m && s) test doesn't catch
6173 parens that didn't match -- they should be set to
6174 undef, not the empty string */
c1a7495a
BB
6175 if (gimme_scalar) {
6176 iters++;
6177 if (m-s == 0)
6178 trailing_empty++;
6179 else
6180 trailing_empty = 0;
6181 } else {
6182 if (m >= orig && s >= orig) {
6183 dstr = newSVpvn_flags(s, m-s,
6184 (do_utf8 ? SVf_UTF8 : 0)
6185 | make_mortal);
6186 }
6187 else
6188 dstr = &PL_sv_undef; /* undef, not "" */
6189 XPUSHs(dstr);
748a9306 6190 }
c1a7495a 6191
a0d0e21e
LW
6192 }
6193 }
07bc277f 6194 s = RX_OFFS(rx)[0].end + orig;
a0d0e21e 6195 }
79072805 6196 }
8ec5e241 6197
c1a7495a
BB
6198 if (!gimme_scalar) {
6199 iters = (SP - PL_stack_base) - base;
6200 }
a0d0e21e 6201 if (iters > maxiters)
cea2e8a9 6202 DIE(aTHX_ "Split loop");
8ec5e241 6203
a0d0e21e
LW
6204 /* keep field after final delim? */
6205 if (s < strend || (iters && origlimit)) {
c1a7495a
BB
6206 if (!gimme_scalar) {
6207 const STRLEN l = strend - s;
6208 dstr = newSVpvn_flags(s, l, (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
6209 XPUSHs(dstr);
6210 }
a0d0e21e 6211 iters++;
79072805 6212 }
a0d0e21e 6213 else if (!origlimit) {
c1a7495a
BB
6214 if (gimme_scalar) {
6215 iters -= trailing_empty;
6216 } else {
6217 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0)) {
6218 if (TOPs && !make_mortal)
6219 sv_2mortal(TOPs);
6220 *SP-- = &PL_sv_undef;
6221 iters--;
6222 }
89900bd3 6223 }
a0d0e21e 6224 }
8ec5e241 6225
8b7059b1
DM
6226 PUTBACK;
6227 LEAVE_SCOPE(oldsave); /* may undo an earlier SWITCHSTACK */
6228 SPAGAIN;
a0d0e21e 6229 if (realarray) {
8ec5e241 6230 if (!mg) {
1c0b011c
NIS
6231 if (SvSMAGICAL(ary)) {
6232 PUTBACK;
ad64d0ec 6233 mg_set(MUTABLE_SV(ary));
1c0b011c
NIS
6234 SPAGAIN;
6235 }
6236 if (gimme == G_ARRAY) {
6237 EXTEND(SP, iters);
6238 Copy(AvARRAY(ary), SP + 1, iters, SV*);
6239 SP += iters;
6240 RETURN;
6241 }
8ec5e241 6242 }
1c0b011c 6243 else {
fb73857a 6244 PUTBACK;
d343c3ef 6245 ENTER_with_name("call_PUSH");
864dbfa3 6246 call_method("PUSH",G_SCALAR|G_DISCARD);
d343c3ef 6247 LEAVE_with_name("call_PUSH");
fb73857a 6248 SPAGAIN;
8ec5e241 6249 if (gimme == G_ARRAY) {
1b6737cc 6250 I32 i;
8ec5e241
NIS
6251 /* EXTEND should not be needed - we just popped them */
6252 EXTEND(SP, iters);
6253 for (i=0; i < iters; i++) {
6254 SV **svp = av_fetch(ary, i, FALSE);
3280af22 6255 PUSHs((svp) ? *svp : &PL_sv_undef);
8ec5e241 6256 }
1c0b011c
NIS
6257 RETURN;
6258 }
a0d0e21e
LW
6259 }
6260 }
6261 else {
6262 if (gimme == G_ARRAY)
6263 RETURN;
6264 }
7f18b612
YST
6265
6266 GETTARGET;
6267 PUSHi(iters);
6268 RETURN;
79072805 6269}
85e6fe83 6270
c5917253
NC
6271PP(pp_once)
6272{
6273 dSP;
6274 SV *const sv = PAD_SVl(PL_op->op_targ);
6275
6276 if (SvPADSTALE(sv)) {
6277 /* First time. */
6278 SvPADSTALE_off(sv);
6279 RETURNOP(cLOGOP->op_other);
6280 }
6281 RETURNOP(cLOGOP->op_next);
6282}
6283
c0329465
MB
6284PP(pp_lock)
6285{
97aff369 6286 dVAR;
39644a26 6287 dSP;
c0329465 6288 dTOPss;
e55aaa0e 6289 SV *retsv = sv;
076a2a80 6290 assert(SvTYPE(retsv) != SVt_PVCV);
68795e93 6291 SvLOCK(sv);
076a2a80 6292 if (SvTYPE(retsv) == SVt_PVAV || SvTYPE(retsv) == SVt_PVHV) {
e55aaa0e
MB
6293 retsv = refto(retsv);
6294 }
6295 SETs(retsv);
c0329465
MB
6296 RETURN;
6297}
a863c7d1 6298
65bca31a
NC
6299
6300PP(unimplemented_op)
6301{
97aff369 6302 dVAR;
361ed549
NC
6303 const Optype op_type = PL_op->op_type;
6304 /* Using OP_NAME() isn't going to be helpful here. Firstly, it doesn't cope
6305 with out of range op numbers - it only "special" cases op_custom.
6306 Secondly, as the three ops we "panic" on are padmy, mapstart and custom,
6307 if we get here for a custom op then that means that the custom op didn't
6308 have an implementation. Given that OP_NAME() looks up the custom op
6309 by its pp_addr, likely it will return NULL, unless someone (unhelpfully)
6310 registers &PL_unimplemented_op as the address of their custom op.
6311 NULL doesn't generate a useful error message. "custom" does. */
6312 const char *const name = op_type >= OP_max
6313 ? "[out of range]" : PL_op_name[PL_op->op_type];
7627e6d0
NC
6314 if(OP_IS_SOCKET(op_type))
6315 DIE(aTHX_ PL_no_sock_func, name);
361ed549 6316 DIE(aTHX_ "panic: unimplemented op %s (#%d) called", name, op_type);
65bca31a
NC
6317}
6318
867fa1e2
YO
6319PP(pp_boolkeys)
6320{
6321 dVAR;
6322 dSP;
6323 HV * const hv = (HV*)POPs;
6324
fd1d9b5c
FC
6325 if (SvTYPE(hv) != SVt_PVHV) { XPUSHs(&PL_sv_no); RETURN; }
6326
867fa1e2
YO
6327 if (SvRMAGICAL(hv)) {
6328 MAGIC * const mg = mg_find((SV*)hv, PERL_MAGIC_tied);
6329 if (mg) {
6330 XPUSHs(magic_scalarpack(hv, mg));
6331 RETURN;
6332 }
6333 }
6334
6335 XPUSHs(boolSV(HvKEYS(hv) != 0));
6336 RETURN;
6337}
6338
e609e586
NC
6339/*
6340 * Local variables:
6341 * c-indentation-style: bsd
6342 * c-basic-offset: 4
6343 * indent-tabs-mode: t
6344 * End:
6345 *
37442d52
RGS
6346 * ex: set ts=8 sts=4 sw=4 noet:
6347 */