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