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