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