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