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