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