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