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