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 | { | |
97aff369 | 57 | dVAR; |
39644a26 | 58 | dSP; |
54310121 | 59 | if (GIMME_V == G_SCALAR) |
3280af22 | 60 | XPUSHs(&PL_sv_undef); |
93a17b20 LW |
61 | RETURN; |
62 | } | |
63 | ||
79072805 LW |
64 | /* Pushy stuff. */ |
65 | ||
93a17b20 LW |
66 | PP(pp_padav) |
67 | { | |
97aff369 | 68 | dVAR; dSP; dTARGET; |
13017935 | 69 | I32 gimme; |
e190e9b4 | 70 | assert(SvTYPE(TARG) == SVt_PVAV); |
533c011a | 71 | if (PL_op->op_private & OPpLVAL_INTRO) |
a5911867 RGS |
72 | if (!(PL_op->op_private & OPpPAD_STATE)) |
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 */ |
502c6561 | 91 | const I32 maxarg = AvFILL(MUTABLE_AV(TARG)) + 1; |
85e6fe83 | 92 | EXTEND(SP, maxarg); |
93965878 NIS |
93 | if (SvMAGICAL(TARG)) { |
94 | U32 i; | |
eb160463 | 95 | for (i=0; i < (U32)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 { | |
502c6561 | 101 | Copy(AvARRAY((const AV *)TARG), SP+1, maxarg, SV*); |
93965878 | 102 | } |
85e6fe83 LW |
103 | SP += maxarg; |
104 | } | |
13017935 | 105 | else if (gimme == G_SCALAR) { |
1b6737cc | 106 | SV* const sv = sv_newmortal(); |
502c6561 | 107 | const I32 maxarg = AvFILL(MUTABLE_AV(TARG)) + 1; |
85e6fe83 LW |
108 | sv_setiv(sv, maxarg); |
109 | PUSHs(sv); | |
110 | } | |
111 | RETURN; | |
93a17b20 LW |
112 | } |
113 | ||
114 | PP(pp_padhv) | |
115 | { | |
97aff369 | 116 | dVAR; dSP; dTARGET; |
54310121 | 117 | I32 gimme; |
118 | ||
e190e9b4 | 119 | assert(SvTYPE(TARG) == SVt_PVHV); |
93a17b20 | 120 | XPUSHs(TARG); |
533c011a | 121 | if (PL_op->op_private & OPpLVAL_INTRO) |
a5911867 RGS |
122 | if (!(PL_op->op_private & OPpPAD_STATE)) |
123 | SAVECLEARSV(PAD_SVl(PL_op->op_targ)); | |
533c011a | 124 | if (PL_op->op_flags & OPf_REF) |
93a17b20 | 125 | RETURN; |
40c94d11 FC |
126 | else if (PL_op->op_private & OPpMAYBE_LVSUB) { |
127 | const I32 flags = is_lvalue_sub(); | |
128 | if (flags && !(flags & OPpENTERSUB_INARGS)) { | |
78f9721b | 129 | if (GIMME == G_SCALAR) |
a84828f3 | 130 | /* diag_listed_as: Can't return %s to lvalue scalar context */ |
78f9721b SM |
131 | Perl_croak(aTHX_ "Can't return hash to lvalue scalar context"); |
132 | RETURN; | |
40c94d11 | 133 | } |
78f9721b | 134 | } |
54310121 | 135 | gimme = GIMME_V; |
136 | if (gimme == G_ARRAY) { | |
981b7185 | 137 | RETURNOP(Perl_do_kv(aTHX)); |
85e6fe83 | 138 | } |
c8fe3bdf | 139 | else if ((PL_op->op_private & OPpTRUEBOOL |
adc42c31 | 140 | || ( PL_op->op_private & OPpMAYBE_TRUEBOOL |
c8fe3bdf FC |
141 | && block_gimme() == G_VOID )) |
142 | && (!SvRMAGICAL(TARG) || !mg_find(TARG, PERL_MAGIC_tied))) | |
143 | SETs(HvUSEDKEYS(TARG) ? &PL_sv_yes : sv_2mortal(newSViv(0))); | |
54310121 | 144 | else if (gimme == G_SCALAR) { |
85fbaab2 | 145 | SV* const sv = Perl_hv_scalar(aTHX_ MUTABLE_HV(TARG)); |
85e6fe83 | 146 | SETs(sv); |
85e6fe83 | 147 | } |
54310121 | 148 | RETURN; |
93a17b20 LW |
149 | } |
150 | ||
ac217057 FC |
151 | PP(pp_padcv) |
152 | { | |
97b03d64 FC |
153 | dVAR; dSP; dTARGET; |
154 | assert(SvTYPE(TARG) == SVt_PVCV); | |
155 | XPUSHs(TARG); | |
156 | RETURN; | |
ac217057 FC |
157 | } |
158 | ||
ecf9c8b7 FC |
159 | PP(pp_introcv) |
160 | { | |
6d5c2147 FC |
161 | dVAR; dTARGET; |
162 | SvPADSTALE_off(TARG); | |
163 | return NORMAL; | |
ecf9c8b7 FC |
164 | } |
165 | ||
13f89586 FC |
166 | PP(pp_clonecv) |
167 | { | |
6d5c2147 | 168 | dVAR; dTARGET; |
81df9f6f | 169 | MAGIC * const mg = |
62698e04 FC |
170 | mg_find(PadlistNAMESARRAY(CvPADLIST(find_runcv(NULL)))[ARGTARG], |
171 | PERL_MAGIC_proto); | |
6d5c2147 FC |
172 | assert(SvTYPE(TARG) == SVt_PVCV); |
173 | assert(mg); | |
174 | assert(mg->mg_obj); | |
175 | if (CvISXSUB(mg->mg_obj)) { /* constant */ | |
176 | /* XXX Should we clone it here? */ | |
6d5c2147 FC |
177 | /* If this changes to use SAVECLEARSV, we can move the SAVECLEARSV |
178 | to introcv and remove the SvPADSTALE_off. */ | |
179 | SAVEPADSVANDMORTALIZE(ARGTARG); | |
4ded55f3 | 180 | PAD_SVl(ARGTARG) = SvREFCNT_inc_simple_NN(mg->mg_obj); |
6d5c2147 FC |
181 | } |
182 | else { | |
183 | if (CvROOT(mg->mg_obj)) { | |
184 | assert(CvCLONE(mg->mg_obj)); | |
185 | assert(!CvCLONED(mg->mg_obj)); | |
186 | } | |
187 | cv_clone_into((CV *)mg->mg_obj,(CV *)TARG); | |
188 | SAVECLEARSV(PAD_SVl(ARGTARG)); | |
189 | } | |
190 | return NORMAL; | |
13f89586 FC |
191 | } |
192 | ||
79072805 LW |
193 | /* Translations. */ |
194 | ||
4bdf8368 | 195 | static const char S_no_symref_sv[] = |
def89bff NC |
196 | "Can't use string (\"%" SVf32 "\"%s) as %s ref while \"strict refs\" in use"; |
197 | ||
6f7909da FC |
198 | /* In some cases this function inspects PL_op. If this function is called |
199 | for new op types, more bool parameters may need to be added in place of | |
200 | the checks. | |
201 | ||
202 | When noinit is true, the absence of a gv will cause a retval of undef. | |
203 | This is unrelated to the cv-to-gv assignment case. | |
6f7909da FC |
204 | */ |
205 | ||
206 | static SV * | |
207 | S_rv2gv(pTHX_ SV *sv, const bool vivify_sv, const bool strict, | |
208 | const bool noinit) | |
209 | { | |
14f0f125 | 210 | dVAR; |
f64c9ac5 | 211 | if (!isGV(sv) || SvFAKE(sv)) SvGETMAGIC(sv); |
ed6116ce | 212 | if (SvROK(sv)) { |
93d7320b DM |
213 | if (SvAMAGIC(sv)) { |
214 | sv = amagic_deref_call(sv, to_gv_amg); | |
93d7320b | 215 | } |
e4a1664f | 216 | wasref: |
ed6116ce | 217 | sv = SvRV(sv); |
b1dadf13 | 218 | if (SvTYPE(sv) == SVt_PVIO) { |
159b6efe | 219 | GV * const gv = MUTABLE_GV(sv_newmortal()); |
885f468a | 220 | gv_init(gv, 0, "__ANONIO__", 10, 0); |
a45c7426 | 221 | GvIOp(gv) = MUTABLE_IO(sv); |
b37c2d43 | 222 | SvREFCNT_inc_void_NN(sv); |
ad64d0ec | 223 | sv = MUTABLE_SV(gv); |
ef54e1a4 | 224 | } |
6e592b3a | 225 | else if (!isGV_with_GP(sv)) |
6f7909da | 226 | return (SV *)Perl_die(aTHX_ "Not a GLOB reference"); |
79072805 LW |
227 | } |
228 | else { | |
6e592b3a | 229 | if (!isGV_with_GP(sv)) { |
f132ae69 | 230 | if (!SvOK(sv)) { |
b13b2135 | 231 | /* If this is a 'my' scalar and flag is set then vivify |
853846ea | 232 | * NI-S 1999/05/07 |
b13b2135 | 233 | */ |
f132ae69 | 234 | if (vivify_sv && sv != &PL_sv_undef) { |
2c8ac474 | 235 | GV *gv; |
ce74145d | 236 | if (SvREADONLY(sv)) |
cb077ed2 | 237 | Perl_croak_no_modify(); |
2c8ac474 | 238 | if (cUNOP->op_targ) { |
0bd48802 | 239 | SV * const namesv = PAD_SV(cUNOP->op_targ); |
94e7eb6f FC |
240 | HV *stash = CopSTASH(PL_curcop); |
241 | if (SvTYPE(stash) != SVt_PVHV) stash = NULL; | |
159b6efe | 242 | gv = MUTABLE_GV(newSV(0)); |
94e7eb6f | 243 | gv_init_sv(gv, stash, namesv, 0); |
2c8ac474 GS |
244 | } |
245 | else { | |
0bd48802 | 246 | const char * const name = CopSTASHPV(PL_curcop); |
6b10071b | 247 | gv = newGVgen_flags(name, |
d14578b8 | 248 | HvNAMEUTF8(CopSTASH(PL_curcop)) ? SVf_UTF8 : 0 ); |
1d8d4d2a | 249 | } |
43230e26 | 250 | prepare_SV_for_RV(sv); |
ad64d0ec | 251 | SvRV_set(sv, MUTABLE_SV(gv)); |
853846ea | 252 | SvROK_on(sv); |
1d8d4d2a | 253 | SvSETMAGIC(sv); |
853846ea | 254 | goto wasref; |
2c8ac474 | 255 | } |
6f7909da FC |
256 | if (PL_op->op_flags & OPf_REF || strict) |
257 | return (SV *)Perl_die(aTHX_ PL_no_usym, "a symbol"); | |
599cee73 | 258 | if (ckWARN(WARN_UNINITIALIZED)) |
29489e7c | 259 | report_uninit(sv); |
6f7909da | 260 | return &PL_sv_undef; |
a0d0e21e | 261 | } |
6f7909da | 262 | if (noinit) |
35cd451c | 263 | { |
77cb3b01 FC |
264 | if (!(sv = MUTABLE_SV(gv_fetchsv_nomg( |
265 | sv, GV_ADDMG, SVt_PVGV | |
23496c6e | 266 | )))) |
6f7909da | 267 | return &PL_sv_undef; |
35cd451c GS |
268 | } |
269 | else { | |
6f7909da FC |
270 | if (strict) |
271 | return | |
272 | (SV *)Perl_die(aTHX_ | |
273 | S_no_symref_sv, | |
274 | sv, | |
bf3d870f | 275 | (SvPOKp(sv) && SvCUR(sv)>32 ? "..." : ""), |
6f7909da FC |
276 | "a symbol" |
277 | ); | |
e26df76a NC |
278 | if ((PL_op->op_private & (OPpLVAL_INTRO|OPpDONT_INIT_GV)) |
279 | == OPpDONT_INIT_GV) { | |
280 | /* We are the target of a coderef assignment. Return | |
281 | the scalar unchanged, and let pp_sasssign deal with | |
282 | things. */ | |
6f7909da | 283 | return sv; |
e26df76a | 284 | } |
77cb3b01 | 285 | sv = MUTABLE_SV(gv_fetchsv_nomg(sv, GV_ADD, SVt_PVGV)); |
35cd451c | 286 | } |
2acc3314 | 287 | /* FAKE globs in the symbol table cause weird bugs (#77810) */ |
96293f45 | 288 | SvFAKE_off(sv); |
93a17b20 | 289 | } |
79072805 | 290 | } |
8dc99089 | 291 | if (SvFAKE(sv) && !(PL_op->op_private & OPpALLOW_FAKE)) { |
2acc3314 | 292 | SV *newsv = sv_newmortal(); |
5cf4b255 | 293 | sv_setsv_flags(newsv, sv, 0); |
2acc3314 | 294 | SvFAKE_off(newsv); |
d8906c05 | 295 | sv = newsv; |
2acc3314 | 296 | } |
6f7909da FC |
297 | return sv; |
298 | } | |
299 | ||
300 | PP(pp_rv2gv) | |
301 | { | |
302 | dVAR; dSP; dTOPss; | |
303 | ||
304 | sv = S_rv2gv(aTHX_ | |
305 | sv, PL_op->op_private & OPpDEREF, | |
306 | PL_op->op_private & HINT_STRICT_REFS, | |
307 | ((PL_op->op_flags & OPf_SPECIAL) && !(PL_op->op_flags & OPf_MOD)) | |
308 | || PL_op->op_type == OP_READLINE | |
309 | ); | |
d8906c05 FC |
310 | if (PL_op->op_private & OPpLVAL_INTRO) |
311 | save_gp(MUTABLE_GV(sv), !(PL_op->op_flags & OPf_SPECIAL)); | |
312 | SETs(sv); | |
79072805 LW |
313 | RETURN; |
314 | } | |
315 | ||
dc3c76f8 NC |
316 | /* Helper function for pp_rv2sv and pp_rv2av */ |
317 | GV * | |
fe9845cc RB |
318 | Perl_softref2xv(pTHX_ SV *const sv, const char *const what, |
319 | const svtype type, SV ***spp) | |
dc3c76f8 NC |
320 | { |
321 | dVAR; | |
322 | GV *gv; | |
323 | ||
7918f24d NC |
324 | PERL_ARGS_ASSERT_SOFTREF2XV; |
325 | ||
dc3c76f8 NC |
326 | if (PL_op->op_private & HINT_STRICT_REFS) { |
327 | if (SvOK(sv)) | |
bf3d870f FC |
328 | Perl_die(aTHX_ S_no_symref_sv, sv, |
329 | (SvPOKp(sv) && SvCUR(sv)>32 ? "..." : ""), what); | |
dc3c76f8 NC |
330 | else |
331 | Perl_die(aTHX_ PL_no_usym, what); | |
332 | } | |
333 | if (!SvOK(sv)) { | |
fd1d9b5c | 334 | if ( |
c8fe3bdf | 335 | PL_op->op_flags & OPf_REF |
fd1d9b5c | 336 | ) |
dc3c76f8 NC |
337 | Perl_die(aTHX_ PL_no_usym, what); |
338 | if (ckWARN(WARN_UNINITIALIZED)) | |
339 | report_uninit(sv); | |
340 | if (type != SVt_PV && GIMME_V == G_ARRAY) { | |
341 | (*spp)--; | |
342 | return NULL; | |
343 | } | |
344 | **spp = &PL_sv_undef; | |
345 | return NULL; | |
346 | } | |
347 | if ((PL_op->op_flags & OPf_SPECIAL) && | |
348 | !(PL_op->op_flags & OPf_MOD)) | |
349 | { | |
77cb3b01 | 350 | if (!(gv = gv_fetchsv_nomg(sv, GV_ADDMG, type))) |
dc3c76f8 NC |
351 | { |
352 | **spp = &PL_sv_undef; | |
353 | return NULL; | |
354 | } | |
355 | } | |
356 | else { | |
77cb3b01 | 357 | gv = gv_fetchsv_nomg(sv, GV_ADD, type); |
dc3c76f8 NC |
358 | } |
359 | return gv; | |
360 | } | |
361 | ||
79072805 LW |
362 | PP(pp_rv2sv) |
363 | { | |
97aff369 | 364 | dVAR; dSP; dTOPss; |
c445ea15 | 365 | GV *gv = NULL; |
79072805 | 366 | |
9026059d | 367 | SvGETMAGIC(sv); |
ed6116ce | 368 | if (SvROK(sv)) { |
93d7320b DM |
369 | if (SvAMAGIC(sv)) { |
370 | sv = amagic_deref_call(sv, to_sv_amg); | |
93d7320b | 371 | } |
f5284f61 | 372 | |
ed6116ce | 373 | sv = SvRV(sv); |
79072805 LW |
374 | switch (SvTYPE(sv)) { |
375 | case SVt_PVAV: | |
376 | case SVt_PVHV: | |
377 | case SVt_PVCV: | |
cbae9b9f YST |
378 | case SVt_PVFM: |
379 | case SVt_PVIO: | |
cea2e8a9 | 380 | DIE(aTHX_ "Not a SCALAR reference"); |
42d0e0b7 | 381 | default: NOOP; |
79072805 LW |
382 | } |
383 | } | |
384 | else { | |
159b6efe | 385 | gv = MUTABLE_GV(sv); |
748a9306 | 386 | |
6e592b3a | 387 | if (!isGV_with_GP(gv)) { |
dc3c76f8 NC |
388 | gv = Perl_softref2xv(aTHX_ sv, "a SCALAR", SVt_PV, &sp); |
389 | if (!gv) | |
390 | RETURN; | |
463ee0b2 | 391 | } |
29c711a3 | 392 | sv = GvSVn(gv); |
a0d0e21e | 393 | } |
533c011a | 394 | if (PL_op->op_flags & OPf_MOD) { |
82d03984 RGS |
395 | if (PL_op->op_private & OPpLVAL_INTRO) { |
396 | if (cUNOP->op_first->op_type == OP_NULL) | |
159b6efe | 397 | sv = save_scalar(MUTABLE_GV(TOPs)); |
82d03984 RGS |
398 | else if (gv) |
399 | sv = save_scalar(gv); | |
400 | else | |
f1f66076 | 401 | Perl_croak(aTHX_ "%s", PL_no_localize_ref); |
82d03984 | 402 | } |
533c011a | 403 | else if (PL_op->op_private & OPpDEREF) |
9026059d | 404 | sv = vivify_ref(sv, PL_op->op_private & OPpDEREF); |
79072805 | 405 | } |
a0d0e21e | 406 | SETs(sv); |
79072805 LW |
407 | RETURN; |
408 | } | |
409 | ||
410 | PP(pp_av2arylen) | |
411 | { | |
97aff369 | 412 | dVAR; dSP; |
502c6561 | 413 | AV * const av = MUTABLE_AV(TOPs); |
02d85cc3 EB |
414 | const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET; |
415 | if (lvalue) { | |
416 | SV ** const sv = Perl_av_arylen_p(aTHX_ MUTABLE_AV(av)); | |
417 | if (!*sv) { | |
418 | *sv = newSV_type(SVt_PVMG); | |
419 | sv_magic(*sv, MUTABLE_SV(av), PERL_MAGIC_arylen, NULL, 0); | |
420 | } | |
421 | SETs(*sv); | |
422 | } else { | |
e1dccc0d | 423 | SETs(sv_2mortal(newSViv(AvFILL(MUTABLE_AV(av))))); |
79072805 | 424 | } |
79072805 LW |
425 | RETURN; |
426 | } | |
427 | ||
a0d0e21e LW |
428 | PP(pp_pos) |
429 | { | |
2154eca7 | 430 | dVAR; dSP; dPOPss; |
8ec5e241 | 431 | |
78f9721b | 432 | if (PL_op->op_flags & OPf_MOD || LVRET) { |
d14578b8 | 433 | SV * const ret = sv_2mortal(newSV_type(SVt_PVLV));/* Not TARG RT#67838 */ |
16eb5365 FC |
434 | sv_magic(ret, NULL, PERL_MAGIC_pos, NULL, 0); |
435 | LvTYPE(ret) = '.'; | |
436 | LvTARG(ret) = SvREFCNT_inc_simple(sv); | |
2154eca7 | 437 | PUSHs(ret); /* no SvSETMAGIC */ |
a0d0e21e LW |
438 | RETURN; |
439 | } | |
440 | else { | |
a0d0e21e | 441 | if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) { |
1b6737cc | 442 | const MAGIC * const mg = mg_find(sv, PERL_MAGIC_regex_global); |
565764a8 | 443 | if (mg && mg->mg_len >= 0) { |
2154eca7 | 444 | dTARGET; |
a0ed51b3 | 445 | I32 i = mg->mg_len; |
7e2040f0 | 446 | if (DO_UTF8(sv)) |
a0ed51b3 | 447 | sv_pos_b2u(sv, &i); |
e1dccc0d | 448 | PUSHi(i); |
a0d0e21e LW |
449 | RETURN; |
450 | } | |
451 | } | |
452 | RETPUSHUNDEF; | |
453 | } | |
454 | } | |
455 | ||
79072805 LW |
456 | PP(pp_rv2cv) |
457 | { | |
97aff369 | 458 | dVAR; dSP; |
79072805 | 459 | GV *gv; |
1eced8f8 | 460 | HV *stash_unused; |
c445ea15 | 461 | const I32 flags = (PL_op->op_flags & OPf_SPECIAL) |
9da346da | 462 | ? GV_ADDMG |
d14578b8 KW |
463 | : ((PL_op->op_private & (OPpLVAL_INTRO|OPpMAY_RETURN_CONSTANT)) |
464 | == OPpMAY_RETURN_CONSTANT) | |
c445ea15 AL |
465 | ? GV_ADD|GV_NOEXPAND |
466 | : GV_ADD; | |
4633a7c4 LW |
467 | /* We usually try to add a non-existent subroutine in case of AUTOLOAD. */ |
468 | /* (But not in defined().) */ | |
e26df76a | 469 | |
1eced8f8 | 470 | CV *cv = sv_2cv(TOPs, &stash_unused, &gv, flags); |
5a20ba3d | 471 | if (cv) NOOP; |
e26df76a | 472 | else if ((flags == (GV_ADD|GV_NOEXPAND)) && gv && SvROK(gv)) { |
ea726b52 | 473 | cv = MUTABLE_CV(gv); |
e26df76a | 474 | } |
07055b4c | 475 | else |
ea726b52 | 476 | cv = MUTABLE_CV(&PL_sv_undef); |
ad64d0ec | 477 | SETs(MUTABLE_SV(cv)); |
79072805 LW |
478 | RETURN; |
479 | } | |
480 | ||
c07a80fd | 481 | PP(pp_prototype) |
482 | { | |
97aff369 | 483 | dVAR; dSP; |
c07a80fd | 484 | CV *cv; |
485 | HV *stash; | |
486 | GV *gv; | |
fabdb6c0 | 487 | SV *ret = &PL_sv_undef; |
c07a80fd | 488 | |
6954f42f | 489 | if (SvGMAGICAL(TOPs)) SETs(sv_mortalcopy(TOPs)); |
b6c543e3 | 490 | if (SvPOK(TOPs) && SvCUR(TOPs) >= 7) { |
e3f73d4e | 491 | const char * s = SvPVX_const(TOPs); |
b6c543e3 | 492 | if (strnEQ(s, "CORE::", 6)) { |
be1b855b | 493 | const int code = keyword(s + 6, SvCUR(TOPs) - 6, 1); |
b66130dd | 494 | if (!code || code == -KEY_CORE) |
1b08e051 FC |
495 | DIE(aTHX_ "Can't find an opnumber for \"%"SVf"\"", |
496 | SVfARG(newSVpvn_flags( | |
c9af70d5 FC |
497 | s+6, SvCUR(TOPs)-6, |
498 | (SvFLAGS(TOPs) & SVf_UTF8)|SVs_TEMP | |
1b08e051 | 499 | ))); |
4e338c21 | 500 | { |
b66130dd FC |
501 | SV * const sv = core_prototype(NULL, s + 6, code, NULL); |
502 | if (sv) ret = sv; | |
503 | } | |
b8c38f0a | 504 | goto set; |
b6c543e3 IZ |
505 | } |
506 | } | |
f2c0649b | 507 | cv = sv_2cv(TOPs, &stash, &gv, 0); |
5f05dabc | 508 | if (cv && SvPOK(cv)) |
8fa6a409 FC |
509 | ret = newSVpvn_flags( |
510 | CvPROTO(cv), CvPROTOLEN(cv), SVs_TEMP | SvUTF8(cv) | |
511 | ); | |
b6c543e3 | 512 | set: |
c07a80fd | 513 | SETs(ret); |
514 | RETURN; | |
515 | } | |
516 | ||
a0d0e21e LW |
517 | PP(pp_anoncode) |
518 | { | |
97aff369 | 519 | dVAR; dSP; |
ea726b52 | 520 | CV *cv = MUTABLE_CV(PAD_SV(PL_op->op_targ)); |
a5f75d66 | 521 | if (CvCLONE(cv)) |
ad64d0ec | 522 | cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv)))); |
5f05dabc | 523 | EXTEND(SP,1); |
ad64d0ec | 524 | PUSHs(MUTABLE_SV(cv)); |
a0d0e21e LW |
525 | RETURN; |
526 | } | |
527 | ||
528 | PP(pp_srefgen) | |
79072805 | 529 | { |
97aff369 | 530 | dVAR; dSP; |
71be2cbc | 531 | *SP = refto(*SP); |
79072805 | 532 | RETURN; |
8ec5e241 | 533 | } |
a0d0e21e LW |
534 | |
535 | PP(pp_refgen) | |
536 | { | |
97aff369 | 537 | dVAR; dSP; dMARK; |
a0d0e21e | 538 | if (GIMME != G_ARRAY) { |
5f0b1d4e GS |
539 | if (++MARK <= SP) |
540 | *MARK = *SP; | |
541 | else | |
3280af22 | 542 | *MARK = &PL_sv_undef; |
5f0b1d4e GS |
543 | *MARK = refto(*MARK); |
544 | SP = MARK; | |
545 | RETURN; | |
a0d0e21e | 546 | } |
bbce6d69 | 547 | EXTEND_MORTAL(SP - MARK); |
71be2cbc | 548 | while (++MARK <= SP) |
549 | *MARK = refto(*MARK); | |
a0d0e21e | 550 | RETURN; |
79072805 LW |
551 | } |
552 | ||
76e3520e | 553 | STATIC SV* |
cea2e8a9 | 554 | S_refto(pTHX_ SV *sv) |
71be2cbc | 555 | { |
97aff369 | 556 | dVAR; |
71be2cbc | 557 | SV* rv; |
558 | ||
7918f24d NC |
559 | PERL_ARGS_ASSERT_REFTO; |
560 | ||
71be2cbc | 561 | if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') { |
562 | if (LvTARGLEN(sv)) | |
68dc0745 | 563 | vivify_defelem(sv); |
564 | if (!(sv = LvTARG(sv))) | |
3280af22 | 565 | sv = &PL_sv_undef; |
0dd88869 | 566 | else |
b37c2d43 | 567 | SvREFCNT_inc_void_NN(sv); |
71be2cbc | 568 | } |
d8b46c1b | 569 | else if (SvTYPE(sv) == SVt_PVAV) { |
502c6561 NC |
570 | if (!AvREAL((const AV *)sv) && AvREIFY((const AV *)sv)) |
571 | av_reify(MUTABLE_AV(sv)); | |
d8b46c1b | 572 | SvTEMP_off(sv); |
b37c2d43 | 573 | SvREFCNT_inc_void_NN(sv); |
d8b46c1b | 574 | } |
f2933f5f DM |
575 | else if (SvPADTMP(sv) && !IS_PADGV(sv)) |
576 | sv = newSVsv(sv); | |
71be2cbc | 577 | else { |
578 | SvTEMP_off(sv); | |
b37c2d43 | 579 | SvREFCNT_inc_void_NN(sv); |
71be2cbc | 580 | } |
581 | rv = sv_newmortal(); | |
4df7f6af | 582 | sv_upgrade(rv, SVt_IV); |
b162af07 | 583 | SvRV_set(rv, sv); |
71be2cbc | 584 | SvROK_on(rv); |
585 | return rv; | |
586 | } | |
587 | ||
79072805 LW |
588 | PP(pp_ref) |
589 | { | |
97aff369 | 590 | dVAR; dSP; dTARGET; |
1b6737cc | 591 | SV * const sv = POPs; |
f12c7020 | 592 | |
5b295bef RD |
593 | if (sv) |
594 | SvGETMAGIC(sv); | |
f12c7020 | 595 | |
a0d0e21e | 596 | if (!sv || !SvROK(sv)) |
4633a7c4 | 597 | RETPUSHNO; |
79072805 | 598 | |
a15456de BF |
599 | (void)sv_ref(TARG,SvRV(sv),TRUE); |
600 | PUSHTARG; | |
79072805 LW |
601 | RETURN; |
602 | } | |
603 | ||
604 | PP(pp_bless) | |
605 | { | |
97aff369 | 606 | dVAR; dSP; |
463ee0b2 | 607 | HV *stash; |
79072805 | 608 | |
463ee0b2 | 609 | if (MAXARG == 1) |
dcdfe746 | 610 | { |
c2f922f1 | 611 | curstash: |
11faa288 | 612 | stash = CopSTASH(PL_curcop); |
dcdfe746 FC |
613 | if (SvTYPE(stash) != SVt_PVHV) |
614 | Perl_croak(aTHX_ "Attempt to bless into a freed package"); | |
615 | } | |
7b8d334a | 616 | else { |
1b6737cc | 617 | SV * const ssv = POPs; |
7b8d334a | 618 | STRLEN len; |
e1ec3a88 | 619 | const char *ptr; |
81689caa | 620 | |
c2f922f1 FC |
621 | if (!ssv) goto curstash; |
622 | if (!SvGMAGICAL(ssv) && !SvAMAGIC(ssv) && SvROK(ssv)) | |
81689caa | 623 | Perl_croak(aTHX_ "Attempt to bless into a reference"); |
5c144d81 | 624 | ptr = SvPV_const(ssv,len); |
a2a5de95 NC |
625 | if (len == 0) |
626 | Perl_ck_warner(aTHX_ packWARN(WARN_MISC), | |
627 | "Explicit blessing to '' (assuming package main)"); | |
e69c50fe | 628 | stash = gv_stashpvn(ptr, len, GV_ADD|SvUTF8(ssv)); |
7b8d334a | 629 | } |
a0d0e21e | 630 | |
5d3fdfeb | 631 | (void)sv_bless(TOPs, stash); |
79072805 LW |
632 | RETURN; |
633 | } | |
634 | ||
fb73857a | 635 | PP(pp_gelem) |
636 | { | |
97aff369 | 637 | dVAR; dSP; |
b13b2135 | 638 | |
1b6737cc | 639 | SV *sv = POPs; |
a180b31a BF |
640 | STRLEN len; |
641 | const char * const elem = SvPV_const(sv, len); | |
159b6efe | 642 | GV * const gv = MUTABLE_GV(POPs); |
c445ea15 | 643 | SV * tmpRef = NULL; |
1b6737cc | 644 | |
c445ea15 | 645 | sv = NULL; |
c4ba80c3 NC |
646 | if (elem) { |
647 | /* elem will always be NUL terminated. */ | |
1b6737cc | 648 | const char * const second_letter = elem + 1; |
c4ba80c3 NC |
649 | switch (*elem) { |
650 | case 'A': | |
a180b31a | 651 | if (len == 5 && strEQ(second_letter, "RRAY")) |
e14698d8 | 652 | { |
ad64d0ec | 653 | tmpRef = MUTABLE_SV(GvAV(gv)); |
e14698d8 FC |
654 | if (tmpRef && !AvREAL((const AV *)tmpRef) |
655 | && AvREIFY((const AV *)tmpRef)) | |
656 | av_reify(MUTABLE_AV(tmpRef)); | |
657 | } | |
c4ba80c3 NC |
658 | break; |
659 | case 'C': | |
a180b31a | 660 | if (len == 4 && strEQ(second_letter, "ODE")) |
ad64d0ec | 661 | tmpRef = MUTABLE_SV(GvCVu(gv)); |
c4ba80c3 NC |
662 | break; |
663 | case 'F': | |
a180b31a | 664 | if (len == 10 && strEQ(second_letter, "ILEHANDLE")) { |
c4ba80c3 NC |
665 | /* finally deprecated in 5.8.0 */ |
666 | deprecate("*glob{FILEHANDLE}"); | |
ad64d0ec | 667 | tmpRef = MUTABLE_SV(GvIOp(gv)); |
c4ba80c3 NC |
668 | } |
669 | else | |
a180b31a | 670 | if (len == 6 && strEQ(second_letter, "ORMAT")) |
ad64d0ec | 671 | tmpRef = MUTABLE_SV(GvFORM(gv)); |
c4ba80c3 NC |
672 | break; |
673 | case 'G': | |
a180b31a | 674 | if (len == 4 && strEQ(second_letter, "LOB")) |
ad64d0ec | 675 | tmpRef = MUTABLE_SV(gv); |
c4ba80c3 NC |
676 | break; |
677 | case 'H': | |
a180b31a | 678 | if (len == 4 && strEQ(second_letter, "ASH")) |
ad64d0ec | 679 | tmpRef = MUTABLE_SV(GvHV(gv)); |
c4ba80c3 NC |
680 | break; |
681 | case 'I': | |
a180b31a | 682 | if (*second_letter == 'O' && !elem[2] && len == 2) |
ad64d0ec | 683 | tmpRef = MUTABLE_SV(GvIOp(gv)); |
c4ba80c3 NC |
684 | break; |
685 | case 'N': | |
a180b31a | 686 | if (len == 4 && strEQ(second_letter, "AME")) |
a663657d | 687 | sv = newSVhek(GvNAME_HEK(gv)); |
c4ba80c3 NC |
688 | break; |
689 | case 'P': | |
a180b31a | 690 | if (len == 7 && strEQ(second_letter, "ACKAGE")) { |
7fa3a4ab NC |
691 | const HV * const stash = GvSTASH(gv); |
692 | const HEK * const hek = stash ? HvNAME_HEK(stash) : NULL; | |
396482e1 | 693 | sv = hek ? newSVhek(hek) : newSVpvs("__ANON__"); |
c4ba80c3 NC |
694 | } |
695 | break; | |
696 | case 'S': | |
a180b31a | 697 | if (len == 6 && strEQ(second_letter, "CALAR")) |
f9d52e31 | 698 | tmpRef = GvSVn(gv); |
c4ba80c3 | 699 | break; |
39b99f21 | 700 | } |
fb73857a | 701 | } |
76e3520e GS |
702 | if (tmpRef) |
703 | sv = newRV(tmpRef); | |
fb73857a | 704 | if (sv) |
705 | sv_2mortal(sv); | |
706 | else | |
3280af22 | 707 | sv = &PL_sv_undef; |
fb73857a | 708 | XPUSHs(sv); |
709 | RETURN; | |
710 | } | |
711 | ||
a0d0e21e | 712 | /* Pattern matching */ |
79072805 | 713 | |
a0d0e21e | 714 | PP(pp_study) |
79072805 | 715 | { |
97aff369 | 716 | dVAR; dSP; dPOPss; |
a0d0e21e LW |
717 | STRLEN len; |
718 | ||
1fa930f2 | 719 | (void)SvPV(sv, len); |
bc9a5256 | 720 | if (len == 0 || len > I32_MAX || !SvPOK(sv) || SvUTF8(sv) || SvVALID(sv)) { |
32f0ea87 | 721 | /* Historically, study was skipped in these cases. */ |
a4f4e906 NC |
722 | RETPUSHNO; |
723 | } | |
724 | ||
a58a85fa | 725 | /* Make study a no-op. It's no longer useful and its existence |
32f0ea87 | 726 | complicates matters elsewhere. */ |
1e422769 | 727 | RETPUSHYES; |
79072805 LW |
728 | } |
729 | ||
a0d0e21e | 730 | PP(pp_trans) |
79072805 | 731 | { |
97aff369 | 732 | dVAR; dSP; dTARG; |
a0d0e21e LW |
733 | SV *sv; |
734 | ||
533c011a | 735 | if (PL_op->op_flags & OPf_STACKED) |
a0d0e21e | 736 | sv = POPs; |
59f00321 RGS |
737 | else if (PL_op->op_private & OPpTARGET_MY) |
738 | sv = GETTARGET; | |
79072805 | 739 | else { |
54b9620d | 740 | sv = DEFSV; |
a0d0e21e | 741 | EXTEND(SP,1); |
79072805 | 742 | } |
bb16bae8 | 743 | if(PL_op->op_type == OP_TRANSR) { |
290797f7 FC |
744 | STRLEN len; |
745 | const char * const pv = SvPV(sv,len); | |
746 | SV * const newsv = newSVpvn_flags(pv, len, SVs_TEMP|SvUTF8(sv)); | |
bb16bae8 | 747 | do_trans(newsv); |
290797f7 | 748 | PUSHs(newsv); |
bb16bae8 | 749 | } |
5bbe7184 FC |
750 | else { |
751 | TARG = sv_newmortal(); | |
752 | PUSHi(do_trans(sv)); | |
753 | } | |
a0d0e21e | 754 | RETURN; |
79072805 LW |
755 | } |
756 | ||
a0d0e21e | 757 | /* Lvalue operators. */ |
79072805 | 758 | |
81745e4e NC |
759 | static void |
760 | S_do_chomp(pTHX_ SV *retval, SV *sv, bool chomping) | |
761 | { | |
762 | dVAR; | |
763 | STRLEN len; | |
764 | char *s; | |
765 | ||
766 | PERL_ARGS_ASSERT_DO_CHOMP; | |
767 | ||
768 | if (chomping && (RsSNARF(PL_rs) || RsRECORD(PL_rs))) | |
769 | return; | |
770 | if (SvTYPE(sv) == SVt_PVAV) { | |
771 | I32 i; | |
772 | AV *const av = MUTABLE_AV(sv); | |
773 | const I32 max = AvFILL(av); | |
774 | ||
775 | for (i = 0; i <= max; i++) { | |
776 | sv = MUTABLE_SV(av_fetch(av, i, FALSE)); | |
777 | if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef)) | |
778 | do_chomp(retval, sv, chomping); | |
779 | } | |
780 | return; | |
781 | } | |
782 | else if (SvTYPE(sv) == SVt_PVHV) { | |
783 | HV* const hv = MUTABLE_HV(sv); | |
784 | HE* entry; | |
785 | (void)hv_iterinit(hv); | |
786 | while ((entry = hv_iternext(hv))) | |
787 | do_chomp(retval, hv_iterval(hv,entry), chomping); | |
788 | return; | |
789 | } | |
790 | else if (SvREADONLY(sv)) { | |
cb077ed2 | 791 | Perl_croak_no_modify(); |
81745e4e | 792 | } |
e3918bb7 FC |
793 | else if (SvIsCOW(sv)) { |
794 | sv_force_normal_flags(sv, 0); | |
795 | } | |
81745e4e NC |
796 | |
797 | if (PL_encoding) { | |
798 | if (!SvUTF8(sv)) { | |
799 | /* XXX, here sv is utf8-ized as a side-effect! | |
800 | If encoding.pm is used properly, almost string-generating | |
801 | operations, including literal strings, chr(), input data, etc. | |
802 | should have been utf8-ized already, right? | |
803 | */ | |
804 | sv_recode_to_utf8(sv, PL_encoding); | |
805 | } | |
806 | } | |
807 | ||
808 | s = SvPV(sv, len); | |
809 | if (chomping) { | |
810 | char *temp_buffer = NULL; | |
811 | SV *svrecode = NULL; | |
812 | ||
813 | if (s && len) { | |
814 | s += --len; | |
815 | if (RsPARA(PL_rs)) { | |
816 | if (*s != '\n') | |
817 | goto nope; | |
818 | ++SvIVX(retval); | |
819 | while (len && s[-1] == '\n') { | |
820 | --len; | |
821 | --s; | |
822 | ++SvIVX(retval); | |
823 | } | |
824 | } | |
825 | else { | |
826 | STRLEN rslen, rs_charlen; | |
827 | const char *rsptr = SvPV_const(PL_rs, rslen); | |
828 | ||
829 | rs_charlen = SvUTF8(PL_rs) | |
830 | ? sv_len_utf8(PL_rs) | |
831 | : rslen; | |
832 | ||
833 | if (SvUTF8(PL_rs) != SvUTF8(sv)) { | |
834 | /* Assumption is that rs is shorter than the scalar. */ | |
835 | if (SvUTF8(PL_rs)) { | |
836 | /* RS is utf8, scalar is 8 bit. */ | |
837 | bool is_utf8 = TRUE; | |
838 | temp_buffer = (char*)bytes_from_utf8((U8*)rsptr, | |
839 | &rslen, &is_utf8); | |
840 | if (is_utf8) { | |
841 | /* Cannot downgrade, therefore cannot possibly match | |
842 | */ | |
843 | assert (temp_buffer == rsptr); | |
844 | temp_buffer = NULL; | |
845 | goto nope; | |
846 | } | |
847 | rsptr = temp_buffer; | |
848 | } | |
849 | else if (PL_encoding) { | |
850 | /* RS is 8 bit, encoding.pm is used. | |
851 | * Do not recode PL_rs as a side-effect. */ | |
852 | svrecode = newSVpvn(rsptr, rslen); | |
853 | sv_recode_to_utf8(svrecode, PL_encoding); | |
854 | rsptr = SvPV_const(svrecode, rslen); | |
855 | rs_charlen = sv_len_utf8(svrecode); | |
856 | } | |
857 | else { | |
858 | /* RS is 8 bit, scalar is utf8. */ | |
859 | temp_buffer = (char*)bytes_to_utf8((U8*)rsptr, &rslen); | |
860 | rsptr = temp_buffer; | |
861 | } | |
862 | } | |
863 | if (rslen == 1) { | |
864 | if (*s != *rsptr) | |
865 | goto nope; | |
866 | ++SvIVX(retval); | |
867 | } | |
868 | else { | |
869 | if (len < rslen - 1) | |
870 | goto nope; | |
871 | len -= rslen - 1; | |
872 | s -= rslen - 1; | |
873 | if (memNE(s, rsptr, rslen)) | |
874 | goto nope; | |
875 | SvIVX(retval) += rs_charlen; | |
876 | } | |
877 | } | |
fbac7ddf | 878 | s = SvPV_force_nomg_nolen(sv); |
81745e4e NC |
879 | SvCUR_set(sv, len); |
880 | *SvEND(sv) = '\0'; | |
881 | SvNIOK_off(sv); | |
882 | SvSETMAGIC(sv); | |
883 | } | |
884 | nope: | |
885 | ||
886 | SvREFCNT_dec(svrecode); | |
887 | ||
888 | Safefree(temp_buffer); | |
889 | } else { | |
890 | if (len && !SvPOK(sv)) | |
891 | s = SvPV_force_nomg(sv, len); | |
892 | if (DO_UTF8(sv)) { | |
893 | if (s && len) { | |
894 | char * const send = s + len; | |
895 | char * const start = s; | |
896 | s = send - 1; | |
897 | while (s > start && UTF8_IS_CONTINUATION(*s)) | |
898 | s--; | |
899 | if (is_utf8_string((U8*)s, send - s)) { | |
900 | sv_setpvn(retval, s, send - s); | |
901 | *s = '\0'; | |
902 | SvCUR_set(sv, s - start); | |
903 | SvNIOK_off(sv); | |
904 | SvUTF8_on(retval); | |
905 | } | |
906 | } | |
907 | else | |
908 | sv_setpvs(retval, ""); | |
909 | } | |
910 | else if (s && len) { | |
911 | s += --len; | |
912 | sv_setpvn(retval, s, 1); | |
913 | *s = '\0'; | |
914 | SvCUR_set(sv, len); | |
915 | SvUTF8_off(sv); | |
916 | SvNIOK_off(sv); | |
917 | } | |
918 | else | |
919 | sv_setpvs(retval, ""); | |
920 | SvSETMAGIC(sv); | |
921 | } | |
922 | } | |
923 | ||
a0d0e21e LW |
924 | PP(pp_schop) |
925 | { | |
97aff369 | 926 | dVAR; dSP; dTARGET; |
fa54efae NC |
927 | const bool chomping = PL_op->op_type == OP_SCHOMP; |
928 | ||
929 | if (chomping) | |
930 | sv_setiv(TARG, 0); | |
931 | do_chomp(TARG, TOPs, chomping); | |
a0d0e21e LW |
932 | SETTARG; |
933 | RETURN; | |
79072805 LW |
934 | } |
935 | ||
a0d0e21e | 936 | PP(pp_chop) |
79072805 | 937 | { |
97aff369 | 938 | dVAR; dSP; dMARK; dTARGET; dORIGMARK; |
fa54efae | 939 | const bool chomping = PL_op->op_type == OP_CHOMP; |
8ec5e241 | 940 | |
fa54efae NC |
941 | if (chomping) |
942 | sv_setiv(TARG, 0); | |
20cf1f79 | 943 | while (MARK < SP) |
fa54efae | 944 | do_chomp(TARG, *++MARK, chomping); |
20cf1f79 NC |
945 | SP = ORIGMARK; |
946 | XPUSHTARG; | |
a0d0e21e | 947 | RETURN; |
79072805 LW |
948 | } |
949 | ||
a0d0e21e LW |
950 | PP(pp_undef) |
951 | { | |
97aff369 | 952 | dVAR; dSP; |
a0d0e21e LW |
953 | SV *sv; |
954 | ||
533c011a | 955 | if (!PL_op->op_private) { |
774d564b | 956 | EXTEND(SP, 1); |
a0d0e21e | 957 | RETPUSHUNDEF; |
774d564b | 958 | } |
79072805 | 959 | |
a0d0e21e LW |
960 | sv = POPs; |
961 | if (!sv) | |
962 | RETPUSHUNDEF; | |
85e6fe83 | 963 | |
765f542d | 964 | SV_CHECK_THINKFIRST_COW_DROP(sv); |
85e6fe83 | 965 | |
a0d0e21e LW |
966 | switch (SvTYPE(sv)) { |
967 | case SVt_NULL: | |
968 | break; | |
969 | case SVt_PVAV: | |
60edcf09 | 970 | av_undef(MUTABLE_AV(sv)); |
a0d0e21e LW |
971 | break; |
972 | case SVt_PVHV: | |
60edcf09 | 973 | hv_undef(MUTABLE_HV(sv)); |
a0d0e21e LW |
974 | break; |
975 | case SVt_PVCV: | |
a2a5de95 | 976 | if (cv_const_sv((const CV *)sv)) |
714cd18f BF |
977 | Perl_ck_warner(aTHX_ packWARN(WARN_MISC), |
978 | "Constant subroutine %"SVf" undefined", | |
979 | SVfARG(CvANON((const CV *)sv) | |
980 | ? newSVpvs_flags("(anonymous)", SVs_TEMP) | |
981 | : sv_2mortal(newSVhek(GvENAME_HEK(CvGV((const CV *)sv)))))); | |
5f66b61c | 982 | /* FALLTHROUGH */ |
9607fc9c | 983 | case SVt_PVFM: |
6fc92669 GS |
984 | { |
985 | /* let user-undef'd sub keep its identity */ | |
ea726b52 | 986 | GV* const gv = CvGV((const CV *)sv); |
b290562e FC |
987 | HEK * const hek = CvNAME_HEK((CV *)sv); |
988 | if (hek) share_hek_hek(hek); | |
ea726b52 | 989 | cv_undef(MUTABLE_CV(sv)); |
b290562e FC |
990 | if (gv) CvGV_set(MUTABLE_CV(sv), gv); |
991 | else if (hek) { | |
992 | SvANY((CV *)sv)->xcv_gv_u.xcv_hek = hek; | |
993 | CvNAMED_on(sv); | |
994 | } | |
6fc92669 | 995 | } |
a0d0e21e | 996 | break; |
8e07c86e | 997 | case SVt_PVGV: |
bc1df6c2 FC |
998 | assert(isGV_with_GP(sv)); |
999 | assert(!SvFAKE(sv)); | |
1000 | { | |
20408e3c | 1001 | GP *gp; |
dd69841b BB |
1002 | HV *stash; |
1003 | ||
dd69841b | 1004 | /* undef *Pkg::meth_name ... */ |
e530fb81 FC |
1005 | bool method_changed |
1006 | = GvCVu((const GV *)sv) && (stash = GvSTASH((const GV *)sv)) | |
1007 | && HvENAME_get(stash); | |
1008 | /* undef *Foo:: */ | |
1009 | if((stash = GvHV((const GV *)sv))) { | |
1010 | if(HvENAME_get(stash)) | |
1011 | SvREFCNT_inc_simple_void_NN(sv_2mortal((SV *)stash)); | |
1012 | else stash = NULL; | |
1013 | } | |
dd69841b | 1014 | |
159b6efe | 1015 | gp_free(MUTABLE_GV(sv)); |
a02a5408 | 1016 | Newxz(gp, 1, GP); |
c43ae56f | 1017 | GvGP_set(sv, gp_ref(gp)); |
561b68a9 | 1018 | GvSV(sv) = newSV(0); |
57843af0 | 1019 | GvLINE(sv) = CopLINE(PL_curcop); |
159b6efe | 1020 | GvEGV(sv) = MUTABLE_GV(sv); |
20408e3c | 1021 | GvMULTI_on(sv); |
e530fb81 FC |
1022 | |
1023 | if(stash) | |
afdbe55d | 1024 | mro_package_moved(NULL, stash, (const GV *)sv, 0); |
e530fb81 FC |
1025 | stash = NULL; |
1026 | /* undef *Foo::ISA */ | |
1027 | if( strEQ(GvNAME((const GV *)sv), "ISA") | |
1028 | && (stash = GvSTASH((const GV *)sv)) | |
1029 | && (method_changed || HvENAME(stash)) ) | |
1030 | mro_isa_changed_in(stash); | |
1031 | else if(method_changed) | |
1032 | mro_method_changed_in( | |
da9043f5 | 1033 | GvSTASH((const GV *)sv) |
e530fb81 FC |
1034 | ); |
1035 | ||
6e592b3a | 1036 | break; |
20408e3c | 1037 | } |
a0d0e21e | 1038 | default: |
b15aece3 | 1039 | if (SvTYPE(sv) >= SVt_PV && SvPVX_const(sv) && SvLEN(sv)) { |
8bd4d4c5 | 1040 | SvPV_free(sv); |
c445ea15 | 1041 | SvPV_set(sv, NULL); |
4633a7c4 | 1042 | SvLEN_set(sv, 0); |
a0d0e21e | 1043 | } |
0c34ef67 | 1044 | SvOK_off(sv); |
4633a7c4 | 1045 | SvSETMAGIC(sv); |
79072805 | 1046 | } |
a0d0e21e LW |
1047 | |
1048 | RETPUSHUNDEF; | |
79072805 LW |
1049 | } |
1050 | ||
a0d0e21e LW |
1051 | PP(pp_postinc) |
1052 | { | |
97aff369 | 1053 | dVAR; dSP; dTARGET; |
c22c99bc FC |
1054 | const bool inc = |
1055 | PL_op->op_type == OP_POSTINC || PL_op->op_type == OP_I_POSTINC; | |
60092ce4 | 1056 | if (SvTYPE(TOPs) >= SVt_PVAV || (isGV_with_GP(TOPs) && !SvFAKE(TOPs))) |
cb077ed2 | 1057 | Perl_croak_no_modify(); |
7dcb9b98 DM |
1058 | if (SvROK(TOPs)) |
1059 | TARG = sv_newmortal(); | |
a0d0e21e | 1060 | sv_setsv(TARG, TOPs); |
4bac9ae4 | 1061 | if (!SvREADONLY(TOPs) && !SvGMAGICAL(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) |
c22c99bc | 1062 | && SvIVX(TOPs) != (inc ? IV_MAX : IV_MIN)) |
55497cff | 1063 | { |
c22c99bc | 1064 | SvIV_set(TOPs, SvIVX(TOPs) + (inc ? 1 : -1)); |
55497cff | 1065 | SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK); |
748a9306 | 1066 | } |
c22c99bc | 1067 | else if (inc) |
6f1401dc | 1068 | sv_inc_nomg(TOPs); |
c22c99bc | 1069 | else sv_dec_nomg(TOPs); |
a0d0e21e | 1070 | SvSETMAGIC(TOPs); |
1e54a23f | 1071 | /* special case for undef: see thread at 2003-03/msg00536.html in archive */ |
c22c99bc | 1072 | if (inc && !SvOK(TARG)) |
a0d0e21e LW |
1073 | sv_setiv(TARG, 0); |
1074 | SETs(TARG); | |
1075 | return NORMAL; | |
1076 | } | |
79072805 | 1077 | |
a0d0e21e LW |
1078 | /* Ordinary operators. */ |
1079 | ||
1080 | PP(pp_pow) | |
1081 | { | |
800401ee | 1082 | dVAR; dSP; dATARGET; SV *svl, *svr; |
58d76dfd | 1083 | #ifdef PERL_PRESERVE_IVUV |
52a96ae6 HS |
1084 | bool is_int = 0; |
1085 | #endif | |
6f1401dc DM |
1086 | tryAMAGICbin_MG(pow_amg, AMGf_assign|AMGf_numeric); |
1087 | svr = TOPs; | |
1088 | svl = TOPm1s; | |
52a96ae6 HS |
1089 | #ifdef PERL_PRESERVE_IVUV |
1090 | /* For integer to integer power, we do the calculation by hand wherever | |
1091 | we're sure it is safe; otherwise we call pow() and try to convert to | |
1092 | integer afterwards. */ | |
01f91bf2 | 1093 | if (SvIV_please_nomg(svr) && SvIV_please_nomg(svl)) { |
900658e3 PF |
1094 | UV power; |
1095 | bool baseuok; | |
1096 | UV baseuv; | |
1097 | ||
800401ee JH |
1098 | if (SvUOK(svr)) { |
1099 | power = SvUVX(svr); | |
900658e3 | 1100 | } else { |
800401ee | 1101 | const IV iv = SvIVX(svr); |
900658e3 PF |
1102 | if (iv >= 0) { |
1103 | power = iv; | |
1104 | } else { | |
1105 | goto float_it; /* Can't do negative powers this way. */ | |
1106 | } | |
1107 | } | |
1108 | ||
800401ee | 1109 | baseuok = SvUOK(svl); |
900658e3 | 1110 | if (baseuok) { |
800401ee | 1111 | baseuv = SvUVX(svl); |
900658e3 | 1112 | } else { |
800401ee | 1113 | const IV iv = SvIVX(svl); |
900658e3 PF |
1114 | if (iv >= 0) { |
1115 | baseuv = iv; | |
1116 | baseuok = TRUE; /* effectively it's a UV now */ | |
1117 | } else { | |
1118 | baseuv = -iv; /* abs, baseuok == false records sign */ | |
1119 | } | |
1120 | } | |
52a96ae6 HS |
1121 | /* now we have integer ** positive integer. */ |
1122 | is_int = 1; | |
1123 | ||
1124 | /* foo & (foo - 1) is zero only for a power of 2. */ | |
58d76dfd | 1125 | if (!(baseuv & (baseuv - 1))) { |
52a96ae6 | 1126 | /* We are raising power-of-2 to a positive integer. |
58d76dfd JH |
1127 | The logic here will work for any base (even non-integer |
1128 | bases) but it can be less accurate than | |
1129 | pow (base,power) or exp (power * log (base)) when the | |
1130 | intermediate values start to spill out of the mantissa. | |
1131 | With powers of 2 we know this can't happen. | |
1132 | And powers of 2 are the favourite thing for perl | |
1133 | programmers to notice ** not doing what they mean. */ | |
1134 | NV result = 1.0; | |
1135 | NV base = baseuok ? baseuv : -(NV)baseuv; | |
900658e3 PF |
1136 | |
1137 | if (power & 1) { | |
1138 | result *= base; | |
1139 | } | |
1140 | while (power >>= 1) { | |
1141 | base *= base; | |
1142 | if (power & 1) { | |
1143 | result *= base; | |
1144 | } | |
1145 | } | |
58d76dfd JH |
1146 | SP--; |
1147 | SETn( result ); | |
6f1401dc | 1148 | SvIV_please_nomg(svr); |
58d76dfd | 1149 | RETURN; |
52a96ae6 | 1150 | } else { |
eb578fdb KW |
1151 | unsigned int highbit = 8 * sizeof(UV); |
1152 | unsigned int diff = 8 * sizeof(UV); | |
900658e3 PF |
1153 | while (diff >>= 1) { |
1154 | highbit -= diff; | |
1155 | if (baseuv >> highbit) { | |
1156 | highbit += diff; | |
1157 | } | |
52a96ae6 HS |
1158 | } |
1159 | /* we now have baseuv < 2 ** highbit */ | |
1160 | if (power * highbit <= 8 * sizeof(UV)) { | |
1161 | /* result will definitely fit in UV, so use UV math | |
1162 | on same algorithm as above */ | |
eb578fdb KW |
1163 | UV result = 1; |
1164 | UV base = baseuv; | |
f2338a2e | 1165 | const bool odd_power = cBOOL(power & 1); |
900658e3 PF |
1166 | if (odd_power) { |
1167 | result *= base; | |
1168 | } | |
1169 | while (power >>= 1) { | |
1170 | base *= base; | |
1171 | if (power & 1) { | |
52a96ae6 | 1172 | result *= base; |
52a96ae6 HS |
1173 | } |
1174 | } | |
1175 | SP--; | |
0615a994 | 1176 | if (baseuok || !odd_power) |
52a96ae6 HS |
1177 | /* answer is positive */ |
1178 | SETu( result ); | |
1179 | else if (result <= (UV)IV_MAX) | |
1180 | /* answer negative, fits in IV */ | |
1181 | SETi( -(IV)result ); | |
1182 | else if (result == (UV)IV_MIN) | |
1183 | /* 2's complement assumption: special case IV_MIN */ | |
1184 | SETi( IV_MIN ); | |
1185 | else | |
1186 | /* answer negative, doesn't fit */ | |
1187 | SETn( -(NV)result ); | |
1188 | RETURN; | |
1189 | } | |
1190 | } | |
58d76dfd | 1191 | } |
52a96ae6 | 1192 | float_it: |
58d76dfd | 1193 | #endif |
a0d0e21e | 1194 | { |
6f1401dc DM |
1195 | NV right = SvNV_nomg(svr); |
1196 | NV left = SvNV_nomg(svl); | |
4efa5a16 | 1197 | (void)POPs; |
3aaeb624 JA |
1198 | |
1199 | #if defined(USE_LONG_DOUBLE) && defined(HAS_AIX_POWL_NEG_BASE_BUG) | |
1200 | /* | |
1201 | We are building perl with long double support and are on an AIX OS | |
1202 | afflicted with a powl() function that wrongly returns NaNQ for any | |
1203 | negative base. This was reported to IBM as PMR #23047-379 on | |
1204 | 03/06/2006. The problem exists in at least the following versions | |
1205 | of AIX and the libm fileset, and no doubt others as well: | |
1206 | ||
1207 | AIX 4.3.3-ML10 bos.adt.libm 4.3.3.50 | |
1208 | AIX 5.1.0-ML04 bos.adt.libm 5.1.0.29 | |
1209 | AIX 5.2.0 bos.adt.libm 5.2.0.85 | |
1210 | ||
1211 | So, until IBM fixes powl(), we provide the following workaround to | |
1212 | handle the problem ourselves. Our logic is as follows: for | |
1213 | negative bases (left), we use fmod(right, 2) to check if the | |
1214 | exponent is an odd or even integer: | |
1215 | ||
1216 | - if odd, powl(left, right) == -powl(-left, right) | |
1217 | - if even, powl(left, right) == powl(-left, right) | |
1218 | ||
1219 | If the exponent is not an integer, the result is rightly NaNQ, so | |
1220 | we just return that (as NV_NAN). | |
1221 | */ | |
1222 | ||
1223 | if (left < 0.0) { | |
1224 | NV mod2 = Perl_fmod( right, 2.0 ); | |
1225 | if (mod2 == 1.0 || mod2 == -1.0) { /* odd integer */ | |
1226 | SETn( -Perl_pow( -left, right) ); | |
1227 | } else if (mod2 == 0.0) { /* even integer */ | |
1228 | SETn( Perl_pow( -left, right) ); | |
1229 | } else { /* fractional power */ | |
1230 | SETn( NV_NAN ); | |
1231 | } | |
1232 | } else { | |
1233 | SETn( Perl_pow( left, right) ); | |
1234 | } | |
1235 | #else | |
52a96ae6 | 1236 | SETn( Perl_pow( left, right) ); |
3aaeb624 JA |
1237 | #endif /* HAS_AIX_POWL_NEG_BASE_BUG */ |
1238 | ||
52a96ae6 HS |
1239 | #ifdef PERL_PRESERVE_IVUV |
1240 | if (is_int) | |
6f1401dc | 1241 | SvIV_please_nomg(svr); |
52a96ae6 HS |
1242 | #endif |
1243 | RETURN; | |
93a17b20 | 1244 | } |
a0d0e21e LW |
1245 | } |
1246 | ||
1247 | PP(pp_multiply) | |
1248 | { | |
800401ee | 1249 | dVAR; dSP; dATARGET; SV *svl, *svr; |
6f1401dc DM |
1250 | tryAMAGICbin_MG(mult_amg, AMGf_assign|AMGf_numeric); |
1251 | svr = TOPs; | |
1252 | svl = TOPm1s; | |
28e5dec8 | 1253 | #ifdef PERL_PRESERVE_IVUV |
01f91bf2 | 1254 | if (SvIV_please_nomg(svr)) { |
28e5dec8 JH |
1255 | /* Unless the left argument is integer in range we are going to have to |
1256 | use NV maths. Hence only attempt to coerce the right argument if | |
1257 | we know the left is integer. */ | |
1258 | /* Left operand is defined, so is it IV? */ | |
01f91bf2 | 1259 | if (SvIV_please_nomg(svl)) { |
800401ee JH |
1260 | bool auvok = SvUOK(svl); |
1261 | bool buvok = SvUOK(svr); | |
28e5dec8 JH |
1262 | const UV topmask = (~ (UV)0) << (4 * sizeof (UV)); |
1263 | const UV botmask = ~((~ (UV)0) << (4 * sizeof (UV))); | |
1264 | UV alow; | |
1265 | UV ahigh; | |
1266 | UV blow; | |
1267 | UV bhigh; | |
1268 | ||
1269 | if (auvok) { | |
800401ee | 1270 | alow = SvUVX(svl); |
28e5dec8 | 1271 | } else { |
800401ee | 1272 | const IV aiv = SvIVX(svl); |
28e5dec8 JH |
1273 | if (aiv >= 0) { |
1274 | alow = aiv; | |
1275 | auvok = TRUE; /* effectively it's a UV now */ | |
1276 | } else { | |
1277 | alow = -aiv; /* abs, auvok == false records sign */ | |
1278 | } | |
1279 | } | |
1280 | if (buvok) { | |
800401ee | 1281 | blow = SvUVX(svr); |
28e5dec8 | 1282 | } else { |
800401ee | 1283 | const IV biv = SvIVX(svr); |
28e5dec8 JH |
1284 | if (biv >= 0) { |
1285 | blow = biv; | |
1286 | buvok = TRUE; /* effectively it's a UV now */ | |
1287 | } else { | |
1288 | blow = -biv; /* abs, buvok == false records sign */ | |
1289 | } | |
1290 | } | |
1291 | ||
1292 | /* If this does sign extension on unsigned it's time for plan B */ | |
1293 | ahigh = alow >> (4 * sizeof (UV)); | |
1294 | alow &= botmask; | |
1295 | bhigh = blow >> (4 * sizeof (UV)); | |
1296 | blow &= botmask; | |
1297 | if (ahigh && bhigh) { | |
6f207bd3 | 1298 | NOOP; |
28e5dec8 JH |
1299 | /* eg 32 bit is at least 0x10000 * 0x10000 == 0x100000000 |
1300 | which is overflow. Drop to NVs below. */ | |
1301 | } else if (!ahigh && !bhigh) { | |
1302 | /* eg 32 bit is at most 0xFFFF * 0xFFFF == 0xFFFE0001 | |
1303 | so the unsigned multiply cannot overflow. */ | |
c445ea15 | 1304 | const UV product = alow * blow; |
28e5dec8 JH |
1305 | if (auvok == buvok) { |
1306 | /* -ve * -ve or +ve * +ve gives a +ve result. */ | |
1307 | SP--; | |
1308 | SETu( product ); | |
1309 | RETURN; | |
1310 | } else if (product <= (UV)IV_MIN) { | |
1311 | /* 2s complement assumption that (UV)-IV_MIN is correct. */ | |
1312 | /* -ve result, which could overflow an IV */ | |
1313 | SP--; | |
25716404 | 1314 | SETi( -(IV)product ); |
28e5dec8 JH |
1315 | RETURN; |
1316 | } /* else drop to NVs below. */ | |
1317 | } else { | |
1318 | /* One operand is large, 1 small */ | |
1319 | UV product_middle; | |
1320 | if (bhigh) { | |
1321 | /* swap the operands */ | |
1322 | ahigh = bhigh; | |
1323 | bhigh = blow; /* bhigh now the temp var for the swap */ | |
1324 | blow = alow; | |
1325 | alow = bhigh; | |
1326 | } | |
1327 | /* now, ((ahigh * blow) << half_UV_len) + (alow * blow) | |
1328 | multiplies can't overflow. shift can, add can, -ve can. */ | |
1329 | product_middle = ahigh * blow; | |
1330 | if (!(product_middle & topmask)) { | |
1331 | /* OK, (ahigh * blow) won't lose bits when we shift it. */ | |
1332 | UV product_low; | |
1333 | product_middle <<= (4 * sizeof (UV)); | |
1334 | product_low = alow * blow; | |
1335 | ||
1336 | /* as for pp_add, UV + something mustn't get smaller. | |
1337 | IIRC ANSI mandates this wrapping *behaviour* for | |
1338 | unsigned whatever the actual representation*/ | |
1339 | product_low += product_middle; | |
1340 | if (product_low >= product_middle) { | |
1341 | /* didn't overflow */ | |
1342 | if (auvok == buvok) { | |
1343 | /* -ve * -ve or +ve * +ve gives a +ve result. */ | |
1344 | SP--; | |
1345 | SETu( product_low ); | |
1346 | RETURN; | |
1347 | } else if (product_low <= (UV)IV_MIN) { | |
1348 | /* 2s complement assumption again */ | |
1349 | /* -ve result, which could overflow an IV */ | |
1350 | SP--; | |
25716404 | 1351 | SETi( -(IV)product_low ); |
28e5dec8 JH |
1352 | RETURN; |
1353 | } /* else drop to NVs below. */ | |
1354 | } | |
1355 | } /* product_middle too large */ | |
1356 | } /* ahigh && bhigh */ | |
800401ee JH |
1357 | } /* SvIOK(svl) */ |
1358 | } /* SvIOK(svr) */ | |
28e5dec8 | 1359 | #endif |
a0d0e21e | 1360 | { |
6f1401dc DM |
1361 | NV right = SvNV_nomg(svr); |
1362 | NV left = SvNV_nomg(svl); | |
4efa5a16 | 1363 | (void)POPs; |
a0d0e21e LW |
1364 | SETn( left * right ); |
1365 | RETURN; | |
79072805 | 1366 | } |
a0d0e21e LW |
1367 | } |
1368 | ||
1369 | PP(pp_divide) | |
1370 | { | |
800401ee | 1371 | dVAR; dSP; dATARGET; SV *svl, *svr; |
6f1401dc DM |
1372 | tryAMAGICbin_MG(div_amg, AMGf_assign|AMGf_numeric); |
1373 | svr = TOPs; | |
1374 | svl = TOPm1s; | |
5479d192 | 1375 | /* Only try to do UV divide first |
68795e93 | 1376 | if ((SLOPPYDIVIDE is true) or |
5479d192 NC |
1377 | (PERL_PRESERVE_IVUV is true and one or both SV is a UV too large |
1378 | to preserve)) | |
1379 | The assumption is that it is better to use floating point divide | |
1380 | whenever possible, only doing integer divide first if we can't be sure. | |
1381 | If NV_PRESERVES_UV is true then we know at compile time that no UV | |
1382 | can be too large to preserve, so don't need to compile the code to | |
1383 | test the size of UVs. */ | |
1384 | ||
a0d0e21e | 1385 | #ifdef SLOPPYDIVIDE |
5479d192 NC |
1386 | # define PERL_TRY_UV_DIVIDE |
1387 | /* ensure that 20./5. == 4. */ | |
a0d0e21e | 1388 | #else |
5479d192 NC |
1389 | # ifdef PERL_PRESERVE_IVUV |
1390 | # ifndef NV_PRESERVES_UV | |
1391 | # define PERL_TRY_UV_DIVIDE | |
1392 | # endif | |
1393 | # endif | |
a0d0e21e | 1394 | #endif |
5479d192 NC |
1395 | |
1396 | #ifdef PERL_TRY_UV_DIVIDE | |
01f91bf2 | 1397 | if (SvIV_please_nomg(svr) && SvIV_please_nomg(svl)) { |
800401ee JH |
1398 | bool left_non_neg = SvUOK(svl); |
1399 | bool right_non_neg = SvUOK(svr); | |
5479d192 NC |
1400 | UV left; |
1401 | UV right; | |
1402 | ||
1403 | if (right_non_neg) { | |
800401ee | 1404 | right = SvUVX(svr); |
5479d192 NC |
1405 | } |
1406 | else { | |
800401ee | 1407 | const IV biv = SvIVX(svr); |
5479d192 NC |
1408 | if (biv >= 0) { |
1409 | right = biv; | |
1410 | right_non_neg = TRUE; /* effectively it's a UV now */ | |
1411 | } | |
1412 | else { | |
1413 | right = -biv; | |
1414 | } | |
1415 | } | |
1416 | /* historically undef()/0 gives a "Use of uninitialized value" | |
1417 | warning before dieing, hence this test goes here. | |
1418 | If it were immediately before the second SvIV_please, then | |
1419 | DIE() would be invoked before left was even inspected, so | |
486ec47a | 1420 | no inspection would give no warning. */ |
5479d192 NC |
1421 | if (right == 0) |
1422 | DIE(aTHX_ "Illegal division by zero"); | |
1423 | ||
1424 | if (left_non_neg) { | |
800401ee | 1425 | left = SvUVX(svl); |
5479d192 NC |
1426 | } |
1427 | else { | |
800401ee | 1428 | const IV aiv = SvIVX(svl); |
5479d192 NC |
1429 | if (aiv >= 0) { |
1430 | left = aiv; | |
1431 | left_non_neg = TRUE; /* effectively it's a UV now */ | |
1432 | } | |
1433 | else { | |
1434 | left = -aiv; | |
1435 | } | |
1436 | } | |
1437 | ||
1438 | if (left >= right | |
1439 | #ifdef SLOPPYDIVIDE | |
1440 | /* For sloppy divide we always attempt integer division. */ | |
1441 | #else | |
1442 | /* Otherwise we only attempt it if either or both operands | |
1443 | would not be preserved by an NV. If both fit in NVs | |
0c2ee62a NC |
1444 | we fall through to the NV divide code below. However, |
1445 | as left >= right to ensure integer result here, we know that | |
1446 | we can skip the test on the right operand - right big | |
1447 | enough not to be preserved can't get here unless left is | |
1448 | also too big. */ | |
1449 | ||
1450 | && (left > ((UV)1 << NV_PRESERVES_UV_BITS)) | |
5479d192 NC |
1451 | #endif |
1452 | ) { | |
1453 | /* Integer division can't overflow, but it can be imprecise. */ | |
1b6737cc | 1454 | const UV result = left / right; |
5479d192 NC |
1455 | if (result * right == left) { |
1456 | SP--; /* result is valid */ | |
1457 | if (left_non_neg == right_non_neg) { | |
1458 | /* signs identical, result is positive. */ | |
1459 | SETu( result ); | |
1460 | RETURN; | |
1461 | } | |
1462 | /* 2s complement assumption */ | |
1463 | if (result <= (UV)IV_MIN) | |
91f3b821 | 1464 | SETi( -(IV)result ); |
5479d192 NC |
1465 | else { |
1466 | /* It's exact but too negative for IV. */ | |
1467 | SETn( -(NV)result ); | |
1468 | } | |
1469 | RETURN; | |
1470 | } /* tried integer divide but it was not an integer result */ | |
32fdb065 | 1471 | } /* else (PERL_ABS(result) < 1.0) or (both UVs in range for NV) */ |
01f91bf2 | 1472 | } /* one operand wasn't SvIOK */ |
5479d192 NC |
1473 | #endif /* PERL_TRY_UV_DIVIDE */ |
1474 | { | |
6f1401dc DM |
1475 | NV right = SvNV_nomg(svr); |
1476 | NV left = SvNV_nomg(svl); | |
4efa5a16 | 1477 | (void)POPs;(void)POPs; |
ebc6a117 PD |
1478 | #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan) |
1479 | if (! Perl_isnan(right) && right == 0.0) | |
1480 | #else | |
5479d192 | 1481 | if (right == 0.0) |
ebc6a117 | 1482 | #endif |
5479d192 NC |
1483 | DIE(aTHX_ "Illegal division by zero"); |
1484 | PUSHn( left / right ); | |
1485 | RETURN; | |
79072805 | 1486 | } |
a0d0e21e LW |
1487 | } |
1488 | ||
1489 | PP(pp_modulo) | |
1490 | { | |
6f1401dc DM |
1491 | dVAR; dSP; dATARGET; |
1492 | tryAMAGICbin_MG(modulo_amg, AMGf_assign|AMGf_numeric); | |
a0d0e21e | 1493 | { |
9c5ffd7c JH |
1494 | UV left = 0; |
1495 | UV right = 0; | |
dc656993 JH |
1496 | bool left_neg = FALSE; |
1497 | bool right_neg = FALSE; | |
e2c88acc NC |
1498 | bool use_double = FALSE; |
1499 | bool dright_valid = FALSE; | |
9c5ffd7c JH |
1500 | NV dright = 0.0; |
1501 | NV dleft = 0.0; | |
6f1401dc DM |
1502 | SV * const svr = TOPs; |
1503 | SV * const svl = TOPm1s; | |
01f91bf2 | 1504 | if (SvIV_please_nomg(svr)) { |
800401ee | 1505 | right_neg = !SvUOK(svr); |
e2c88acc | 1506 | if (!right_neg) { |
800401ee | 1507 | right = SvUVX(svr); |
e2c88acc | 1508 | } else { |
800401ee | 1509 | const IV biv = SvIVX(svr); |
e2c88acc NC |
1510 | if (biv >= 0) { |
1511 | right = biv; | |
1512 | right_neg = FALSE; /* effectively it's a UV now */ | |
1513 | } else { | |
1514 | right = -biv; | |
1515 | } | |
1516 | } | |
1517 | } | |
1518 | else { | |
6f1401dc | 1519 | dright = SvNV_nomg(svr); |
787eafbd IZ |
1520 | right_neg = dright < 0; |
1521 | if (right_neg) | |
1522 | dright = -dright; | |
e2c88acc NC |
1523 | if (dright < UV_MAX_P1) { |
1524 | right = U_V(dright); | |
1525 | dright_valid = TRUE; /* In case we need to use double below. */ | |
1526 | } else { | |
1527 | use_double = TRUE; | |
1528 | } | |
787eafbd | 1529 | } |
a0d0e21e | 1530 | |
e2c88acc NC |
1531 | /* At this point use_double is only true if right is out of range for |
1532 | a UV. In range NV has been rounded down to nearest UV and | |
1533 | use_double false. */ | |
01f91bf2 | 1534 | if (!use_double && SvIV_please_nomg(svl)) { |
800401ee | 1535 | left_neg = !SvUOK(svl); |
e2c88acc | 1536 | if (!left_neg) { |
800401ee | 1537 | left = SvUVX(svl); |
e2c88acc | 1538 | } else { |
800401ee | 1539 | const IV aiv = SvIVX(svl); |
e2c88acc NC |
1540 | if (aiv >= 0) { |
1541 | left = aiv; | |
1542 | left_neg = FALSE; /* effectively it's a UV now */ | |
1543 | } else { | |
1544 | left = -aiv; | |
1545 | } | |
1546 | } | |
e2c88acc | 1547 | } |
787eafbd | 1548 | else { |
6f1401dc | 1549 | dleft = SvNV_nomg(svl); |
787eafbd IZ |
1550 | left_neg = dleft < 0; |
1551 | if (left_neg) | |
1552 | dleft = -dleft; | |
68dc0745 | 1553 | |
e2c88acc NC |
1554 | /* This should be exactly the 5.6 behaviour - if left and right are |
1555 | both in range for UV then use U_V() rather than floor. */ | |
1556 | if (!use_double) { | |
1557 | if (dleft < UV_MAX_P1) { | |
1558 | /* right was in range, so is dleft, so use UVs not double. | |
1559 | */ | |
1560 | left = U_V(dleft); | |
1561 | } | |
1562 | /* left is out of range for UV, right was in range, so promote | |
1563 | right (back) to double. */ | |
1564 | else { | |
1565 | /* The +0.5 is used in 5.6 even though it is not strictly | |
1566 | consistent with the implicit +0 floor in the U_V() | |
1567 | inside the #if 1. */ | |
1568 | dleft = Perl_floor(dleft + 0.5); | |
1569 | use_double = TRUE; | |
1570 | if (dright_valid) | |
1571 | dright = Perl_floor(dright + 0.5); | |
1572 | else | |
1573 | dright = right; | |
1574 | } | |
1575 | } | |
1576 | } | |
6f1401dc | 1577 | sp -= 2; |
787eafbd | 1578 | if (use_double) { |
65202027 | 1579 | NV dans; |
787eafbd | 1580 | |
787eafbd | 1581 | if (!dright) |
cea2e8a9 | 1582 | DIE(aTHX_ "Illegal modulus zero"); |
787eafbd | 1583 | |
65202027 | 1584 | dans = Perl_fmod(dleft, dright); |
787eafbd IZ |
1585 | if ((left_neg != right_neg) && dans) |
1586 | dans = dright - dans; | |
1587 | if (right_neg) | |
1588 | dans = -dans; | |
1589 | sv_setnv(TARG, dans); | |
1590 | } | |
1591 | else { | |
1592 | UV ans; | |
1593 | ||
787eafbd | 1594 | if (!right) |
cea2e8a9 | 1595 | DIE(aTHX_ "Illegal modulus zero"); |
787eafbd IZ |
1596 | |
1597 | ans = left % right; | |
1598 | if ((left_neg != right_neg) && ans) | |
1599 | ans = right - ans; | |
1600 | if (right_neg) { | |
1601 | /* XXX may warn: unary minus operator applied to unsigned type */ | |
1602 | /* could change -foo to be (~foo)+1 instead */ | |
1603 | if (ans <= ~((UV)IV_MAX)+1) | |
1604 | sv_setiv(TARG, ~ans+1); | |
1605 | else | |
65202027 | 1606 | sv_setnv(TARG, -(NV)ans); |
787eafbd IZ |
1607 | } |
1608 | else | |
1609 | sv_setuv(TARG, ans); | |
1610 | } | |
1611 | PUSHTARG; | |
1612 | RETURN; | |
79072805 | 1613 | } |
a0d0e21e | 1614 | } |
79072805 | 1615 | |
a0d0e21e LW |
1616 | PP(pp_repeat) |
1617 | { | |
6f1401dc | 1618 | dVAR; dSP; dATARGET; |
eb578fdb | 1619 | IV count; |
6f1401dc DM |
1620 | SV *sv; |
1621 | ||
1622 | if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) { | |
1623 | /* TODO: think of some way of doing list-repeat overloading ??? */ | |
1624 | sv = POPs; | |
1625 | SvGETMAGIC(sv); | |
1626 | } | |
1627 | else { | |
1628 | tryAMAGICbin_MG(repeat_amg, AMGf_assign); | |
1629 | sv = POPs; | |
1630 | } | |
1631 | ||
2b573ace JH |
1632 | if (SvIOKp(sv)) { |
1633 | if (SvUOK(sv)) { | |
6f1401dc | 1634 | const UV uv = SvUV_nomg(sv); |
2b573ace JH |
1635 | if (uv > IV_MAX) |
1636 | count = IV_MAX; /* The best we can do? */ | |
1637 | else | |
1638 | count = uv; | |
1639 | } else { | |
6f1401dc | 1640 | const IV iv = SvIV_nomg(sv); |
2b573ace JH |
1641 | if (iv < 0) |
1642 | count = 0; | |
1643 | else | |
1644 | count = iv; | |
1645 | } | |
1646 | } | |
1647 | else if (SvNOKp(sv)) { | |
6f1401dc | 1648 | const NV nv = SvNV_nomg(sv); |
2b573ace JH |
1649 | if (nv < 0.0) |
1650 | count = 0; | |
1651 | else | |
1652 | count = (IV)nv; | |
1653 | } | |
1654 | else | |
6f1401dc DM |
1655 | count = SvIV_nomg(sv); |
1656 | ||
533c011a | 1657 | if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) { |
a0d0e21e | 1658 | dMARK; |
a1894d81 | 1659 | static const char* const oom_list_extend = "Out of memory during list extend"; |
0bd48802 AL |
1660 | const I32 items = SP - MARK; |
1661 | const I32 max = items * count; | |
79072805 | 1662 | |
2b573ace JH |
1663 | MEM_WRAP_CHECK_1(max, SV*, oom_list_extend); |
1664 | /* Did the max computation overflow? */ | |
27d5b266 | 1665 | if (items > 0 && max > 0 && (max < items || max < count)) |
0157ef98 | 1666 | Perl_croak(aTHX_ "%s", oom_list_extend); |
a0d0e21e LW |
1667 | MEXTEND(MARK, max); |
1668 | if (count > 1) { | |
1669 | while (SP > MARK) { | |
976c8a39 JH |
1670 | #if 0 |
1671 | /* This code was intended to fix 20010809.028: | |
1672 | ||
1673 | $x = 'abcd'; | |
1674 | for (($x =~ /./g) x 2) { | |
1675 | print chop; # "abcdabcd" expected as output. | |
1676 | } | |
1677 | ||
1678 | * but that change (#11635) broke this code: | |
1679 | ||
1680 | $x = [("foo")x2]; # only one "foo" ended up in the anonlist. | |
1681 | ||
1682 | * I can't think of a better fix that doesn't introduce | |
1683 | * an efficiency hit by copying the SVs. The stack isn't | |
1684 | * refcounted, and mortalisation obviously doesn't | |
1685 | * Do The Right Thing when the stack has more than | |
1686 | * one pointer to the same mortal value. | |
1687 | * .robin. | |
1688 | */ | |
e30acc16 RH |
1689 | if (*SP) { |
1690 | *SP = sv_2mortal(newSVsv(*SP)); | |
1691 | SvREADONLY_on(*SP); | |
1692 | } | |
976c8a39 JH |
1693 | #else |
1694 | if (*SP) | |
1695 | SvTEMP_off((*SP)); | |
1696 | #endif | |
a0d0e21e | 1697 | SP--; |
79072805 | 1698 | } |
a0d0e21e LW |
1699 | MARK++; |
1700 | repeatcpy((char*)(MARK + items), (char*)MARK, | |
ad64d0ec | 1701 | items * sizeof(const SV *), count - 1); |
a0d0e21e | 1702 | SP += max; |
79072805 | 1703 | } |
a0d0e21e LW |
1704 | else if (count <= 0) |
1705 | SP -= items; | |
79072805 | 1706 | } |
a0d0e21e | 1707 | else { /* Note: mark already snarfed by pp_list */ |
0bd48802 | 1708 | SV * const tmpstr = POPs; |
a0d0e21e | 1709 | STRLEN len; |
9b877dbb | 1710 | bool isutf; |
a1894d81 | 1711 | static const char* const oom_string_extend = |
2b573ace | 1712 | "Out of memory during string extend"; |
a0d0e21e | 1713 | |
6f1401dc DM |
1714 | if (TARG != tmpstr) |
1715 | sv_setsv_nomg(TARG, tmpstr); | |
1716 | SvPV_force_nomg(TARG, len); | |
9b877dbb | 1717 | isutf = DO_UTF8(TARG); |
8ebc5c01 | 1718 | if (count != 1) { |
1719 | if (count < 1) | |
1720 | SvCUR_set(TARG, 0); | |
1721 | else { | |
c445ea15 | 1722 | const STRLEN max = (UV)count * len; |
19a94d75 | 1723 | if (len > MEM_SIZE_MAX / count) |
0157ef98 | 1724 | Perl_croak(aTHX_ "%s", oom_string_extend); |
2b573ace | 1725 | MEM_WRAP_CHECK_1(max, char, oom_string_extend); |
8569b9dc | 1726 | SvGROW(TARG, max + 1); |
a0d0e21e | 1727 | repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1); |
b162af07 | 1728 | SvCUR_set(TARG, SvCUR(TARG) * count); |
7a4c00b4 | 1729 | } |
a0d0e21e | 1730 | *SvEND(TARG) = '\0'; |
a0d0e21e | 1731 | } |
dfcb284a GS |
1732 | if (isutf) |
1733 | (void)SvPOK_only_UTF8(TARG); | |
1734 | else | |
1735 | (void)SvPOK_only(TARG); | |
b80b6069 RH |
1736 | |
1737 | if (PL_op->op_private & OPpREPEAT_DOLIST) { | |
1738 | /* The parser saw this as a list repeat, and there | |
1739 | are probably several items on the stack. But we're | |
1740 | in scalar context, and there's no pp_list to save us | |
1741 | now. So drop the rest of the items -- robin@kitsite.com | |
1742 | */ | |
1743 | dMARK; | |
1744 | SP = MARK; | |
1745 | } | |
a0d0e21e | 1746 | PUSHTARG; |
79072805 | 1747 | } |
a0d0e21e LW |
1748 | RETURN; |
1749 | } | |
79072805 | 1750 | |
a0d0e21e LW |
1751 | PP(pp_subtract) |
1752 | { | |
800401ee | 1753 | dVAR; dSP; dATARGET; bool useleft; SV *svl, *svr; |
6f1401dc DM |
1754 | tryAMAGICbin_MG(subtr_amg, AMGf_assign|AMGf_numeric); |
1755 | svr = TOPs; | |
1756 | svl = TOPm1s; | |
800401ee | 1757 | useleft = USE_LEFT(svl); |
28e5dec8 | 1758 | #ifdef PERL_PRESERVE_IVUV |
7dca457a NC |
1759 | /* See comments in pp_add (in pp_hot.c) about Overflow, and how |
1760 | "bad things" happen if you rely on signed integers wrapping. */ | |
01f91bf2 | 1761 | if (SvIV_please_nomg(svr)) { |
28e5dec8 JH |
1762 | /* Unless the left argument is integer in range we are going to have to |
1763 | use NV maths. Hence only attempt to coerce the right argument if | |
1764 | we know the left is integer. */ | |
eb578fdb | 1765 | UV auv = 0; |
9c5ffd7c | 1766 | bool auvok = FALSE; |
7dca457a NC |
1767 | bool a_valid = 0; |
1768 | ||
28e5dec8 | 1769 | if (!useleft) { |
7dca457a NC |
1770 | auv = 0; |
1771 | a_valid = auvok = 1; | |
1772 | /* left operand is undef, treat as zero. */ | |
28e5dec8 JH |
1773 | } else { |
1774 | /* Left operand is defined, so is it IV? */ | |
01f91bf2 | 1775 | if (SvIV_please_nomg(svl)) { |
800401ee JH |
1776 | if ((auvok = SvUOK(svl))) |
1777 | auv = SvUVX(svl); | |
7dca457a | 1778 | else { |
eb578fdb | 1779 | const IV aiv = SvIVX(svl); |
7dca457a NC |
1780 | if (aiv >= 0) { |
1781 | auv = aiv; | |
1782 | auvok = 1; /* Now acting as a sign flag. */ | |
1783 | } else { /* 2s complement assumption for IV_MIN */ | |
1784 | auv = (UV)-aiv; | |
28e5dec8 | 1785 | } |
7dca457a NC |
1786 | } |
1787 | a_valid = 1; | |
1788 | } | |
1789 | } | |
1790 | if (a_valid) { | |
1791 | bool result_good = 0; | |
1792 | UV result; | |
eb578fdb | 1793 | UV buv; |
800401ee | 1794 | bool buvok = SvUOK(svr); |
9041c2e3 | 1795 | |
7dca457a | 1796 | if (buvok) |
800401ee | 1797 | buv = SvUVX(svr); |
7dca457a | 1798 | else { |
eb578fdb | 1799 | const IV biv = SvIVX(svr); |
7dca457a NC |
1800 | if (biv >= 0) { |
1801 | buv = biv; | |
1802 | buvok = 1; | |
1803 | } else | |
1804 | buv = (UV)-biv; | |
1805 | } | |
1806 | /* ?uvok if value is >= 0. basically, flagged as UV if it's +ve, | |
602f51c4 | 1807 | else "IV" now, independent of how it came in. |
7dca457a NC |
1808 | if a, b represents positive, A, B negative, a maps to -A etc |
1809 | a - b => (a - b) | |
1810 | A - b => -(a + b) | |
1811 | a - B => (a + b) | |
1812 | A - B => -(a - b) | |
1813 | all UV maths. negate result if A negative. | |
1814 | subtract if signs same, add if signs differ. */ | |
1815 | ||
1816 | if (auvok ^ buvok) { | |
1817 | /* Signs differ. */ | |
1818 | result = auv + buv; | |
1819 | if (result >= auv) | |
1820 | result_good = 1; | |
1821 | } else { | |
1822 | /* Signs same */ | |
1823 | if (auv >= buv) { | |
1824 | result = auv - buv; | |
1825 | /* Must get smaller */ | |
1826 | if (result <= auv) | |
1827 | result_good = 1; | |
1828 | } else { | |
1829 | result = buv - auv; | |
1830 | if (result <= buv) { | |
1831 | /* result really should be -(auv-buv). as its negation | |
1832 | of true value, need to swap our result flag */ | |
1833 | auvok = !auvok; | |
1834 | result_good = 1; | |
28e5dec8 | 1835 | } |
28e5dec8 JH |
1836 | } |
1837 | } | |
7dca457a NC |
1838 | if (result_good) { |
1839 | SP--; | |
1840 | if (auvok) | |
1841 | SETu( result ); | |
1842 | else { | |
1843 | /* Negate result */ | |
1844 | if (result <= (UV)IV_MIN) | |
1845 | SETi( -(IV)result ); | |
1846 | else { | |
1847 | /* result valid, but out of range for IV. */ | |
1848 | SETn( -(NV)result ); | |
1849 | } | |
1850 | } | |
1851 | RETURN; | |
1852 | } /* Overflow, drop through to NVs. */ | |
28e5dec8 JH |
1853 | } |
1854 | } | |
1855 | #endif | |
a0d0e21e | 1856 | { |
6f1401dc | 1857 | NV value = SvNV_nomg(svr); |
4efa5a16 RD |
1858 | (void)POPs; |
1859 | ||
28e5dec8 JH |
1860 | if (!useleft) { |
1861 | /* left operand is undef, treat as zero - value */ | |
1862 | SETn(-value); | |
1863 | RETURN; | |
1864 | } | |
6f1401dc | 1865 | SETn( SvNV_nomg(svl) - value ); |
28e5dec8 | 1866 | RETURN; |
79072805 | 1867 | } |
a0d0e21e | 1868 | } |
79072805 | 1869 | |
a0d0e21e LW |
1870 | PP(pp_left_shift) |
1871 | { | |
6f1401dc | 1872 | dVAR; dSP; dATARGET; SV *svl, *svr; |
a42d0242 | 1873 | tryAMAGICbin_MG(lshift_amg, AMGf_assign|AMGf_numeric); |
6f1401dc DM |
1874 | svr = POPs; |
1875 | svl = TOPs; | |
a0d0e21e | 1876 | { |
6f1401dc | 1877 | const IV shift = SvIV_nomg(svr); |
d0ba1bd2 | 1878 | if (PL_op->op_private & HINT_INTEGER) { |
6f1401dc | 1879 | const IV i = SvIV_nomg(svl); |
972b05a9 | 1880 | SETi(i << shift); |
d0ba1bd2 JH |
1881 | } |
1882 | else { | |
6f1401dc | 1883 | const UV u = SvUV_nomg(svl); |
972b05a9 | 1884 | SETu(u << shift); |
d0ba1bd2 | 1885 | } |
55497cff | 1886 | RETURN; |
79072805 | 1887 | } |
a0d0e21e | 1888 | } |
79072805 | 1889 | |
a0d0e21e LW |
1890 | PP(pp_right_shift) |
1891 | { | |
6f1401dc | 1892 | dVAR; dSP; dATARGET; SV *svl, *svr; |
a42d0242 | 1893 | tryAMAGICbin_MG(rshift_amg, AMGf_assign|AMGf_numeric); |
6f1401dc DM |
1894 | svr = POPs; |
1895 | svl = TOPs; | |
a0d0e21e | 1896 | { |
6f1401dc | 1897 | const IV shift = SvIV_nomg(svr); |
d0ba1bd2 | 1898 | if (PL_op->op_private & HINT_INTEGER) { |
6f1401dc | 1899 | const IV i = SvIV_nomg(svl); |
972b05a9 | 1900 | SETi(i >> shift); |
d0ba1bd2 JH |
1901 | } |
1902 | else { | |
6f1401dc | 1903 | const UV u = SvUV_nomg(svl); |
972b05a9 | 1904 | SETu(u >> shift); |
d0ba1bd2 | 1905 | } |
a0d0e21e | 1906 | RETURN; |
93a17b20 | 1907 | } |
79072805 LW |
1908 | } |
1909 | ||
a0d0e21e | 1910 | PP(pp_lt) |
79072805 | 1911 | { |
6f1401dc | 1912 | dVAR; dSP; |
33efebe6 DM |
1913 | SV *left, *right; |
1914 | ||
a42d0242 | 1915 | tryAMAGICbin_MG(lt_amg, AMGf_set|AMGf_numeric); |
33efebe6 DM |
1916 | right = POPs; |
1917 | left = TOPs; | |
1918 | SETs(boolSV( | |
1919 | (SvIOK_notUV(left) && SvIOK_notUV(right)) | |
1920 | ? (SvIVX(left) < SvIVX(right)) | |
1921 | : (do_ncmp(left, right) == -1) | |
1922 | )); | |
1923 | RETURN; | |
a0d0e21e | 1924 | } |
79072805 | 1925 | |
a0d0e21e LW |
1926 | PP(pp_gt) |
1927 | { | |
6f1401dc | 1928 | dVAR; dSP; |
33efebe6 | 1929 | SV *left, *right; |
1b6737cc | 1930 | |
33efebe6 DM |
1931 | tryAMAGICbin_MG(gt_amg, AMGf_set|AMGf_numeric); |
1932 | right = POPs; | |
1933 | left = TOPs; | |
1934 | SETs(boolSV( | |
1935 | (SvIOK_notUV(left) && SvIOK_notUV(right)) | |
1936 | ? (SvIVX(left) > SvIVX(right)) | |
1937 | : (do_ncmp(left, right) == 1) | |
1938 | )); | |
1939 | RETURN; | |
a0d0e21e LW |
1940 | } |
1941 | ||
1942 | PP(pp_le) | |
1943 | { | |
6f1401dc | 1944 | dVAR; dSP; |
33efebe6 | 1945 | SV *left, *right; |
1b6737cc | 1946 | |
33efebe6 DM |
1947 | tryAMAGICbin_MG(le_amg, AMGf_set|AMGf_numeric); |
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) <= 0) | |
1954 | )); | |
1955 | RETURN; | |
a0d0e21e LW |
1956 | } |
1957 | ||
1958 | PP(pp_ge) | |
1959 | { | |
6f1401dc | 1960 | dVAR; dSP; |
33efebe6 DM |
1961 | SV *left, *right; |
1962 | ||
1963 | tryAMAGICbin_MG(ge_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) & 2) == 0) | |
1970 | )); | |
1971 | RETURN; | |
1972 | } | |
1b6737cc | 1973 | |
33efebe6 DM |
1974 | PP(pp_ne) |
1975 | { | |
1976 | dVAR; dSP; | |
1977 | SV *left, *right; | |
1978 | ||
1979 | tryAMAGICbin_MG(ne_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; | |
1988 | } | |
1b6737cc | 1989 | |
33efebe6 DM |
1990 | /* compare left and right SVs. Returns: |
1991 | * -1: < | |
1992 | * 0: == | |
1993 | * 1: > | |
1994 | * 2: left or right was a NaN | |
1995 | */ | |
1996 | I32 | |
1997 | Perl_do_ncmp(pTHX_ SV* const left, SV * const right) | |
1998 | { | |
1999 | dVAR; | |
1b6737cc | 2000 | |
33efebe6 DM |
2001 | PERL_ARGS_ASSERT_DO_NCMP; |
2002 | #ifdef PERL_PRESERVE_IVUV | |
33efebe6 | 2003 | /* Fortunately it seems NaN isn't IOK */ |
01f91bf2 | 2004 | if (SvIV_please_nomg(right) && SvIV_please_nomg(left)) { |
33efebe6 DM |
2005 | if (!SvUOK(left)) { |
2006 | const IV leftiv = SvIVX(left); | |
2007 | if (!SvUOK(right)) { | |
2008 | /* ## IV <=> IV ## */ | |
2009 | const IV rightiv = SvIVX(right); | |
2010 | return (leftiv > rightiv) - (leftiv < rightiv); | |
28e5dec8 | 2011 | } |
33efebe6 DM |
2012 | /* ## IV <=> UV ## */ |
2013 | if (leftiv < 0) | |
2014 | /* As (b) is a UV, it's >=0, so it must be < */ | |
2015 | return -1; | |
2016 | { | |
2017 | const UV rightuv = SvUVX(right); | |
2018 | return ((UV)leftiv > rightuv) - ((UV)leftiv < rightuv); | |
28e5dec8 | 2019 | } |
28e5dec8 | 2020 | } |
79072805 | 2021 | |
33efebe6 DM |
2022 | if (SvUOK(right)) { |
2023 | /* ## UV <=> UV ## */ | |
2024 | const UV leftuv = SvUVX(left); | |
2025 | const UV rightuv = SvUVX(right); | |
2026 | return (leftuv > rightuv) - (leftuv < rightuv); | |
28e5dec8 | 2027 | } |
33efebe6 DM |
2028 | /* ## UV <=> IV ## */ |
2029 | { | |
2030 | const IV rightiv = SvIVX(right); | |
2031 | if (rightiv < 0) | |
2032 | /* As (a) is a UV, it's >=0, so it cannot be < */ | |
2033 | return 1; | |
2034 | { | |
2035 | const UV leftuv = SvUVX(left); | |
2036 | return (leftuv > (UV)rightiv) - (leftuv < (UV)rightiv); | |
28e5dec8 | 2037 | } |
28e5dec8 | 2038 | } |
118e2215 | 2039 | assert(0); /* NOTREACHED */ |
28e5dec8 JH |
2040 | } |
2041 | #endif | |
a0d0e21e | 2042 | { |
33efebe6 DM |
2043 | NV const rnv = SvNV_nomg(right); |
2044 | NV const lnv = SvNV_nomg(left); | |
2045 | ||
cab190d4 | 2046 | #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan) |
33efebe6 DM |
2047 | if (Perl_isnan(lnv) || Perl_isnan(rnv)) { |
2048 | return 2; | |
2049 | } | |
2050 | return (lnv > rnv) - (lnv < rnv); | |
cab190d4 | 2051 | #else |
33efebe6 DM |
2052 | if (lnv < rnv) |
2053 | return -1; | |
2054 | if (lnv > rnv) | |
2055 | return 1; | |
2056 | if (lnv == rnv) | |
2057 | return 0; | |
2058 | return 2; | |
cab190d4 | 2059 | #endif |
a0d0e21e | 2060 | } |
79072805 LW |
2061 | } |
2062 | ||
33efebe6 | 2063 | |
a0d0e21e | 2064 | PP(pp_ncmp) |
79072805 | 2065 | { |
33efebe6 DM |
2066 | dVAR; dSP; |
2067 | SV *left, *right; | |
2068 | I32 value; | |
a42d0242 | 2069 | tryAMAGICbin_MG(ncmp_amg, AMGf_numeric); |
33efebe6 DM |
2070 | right = POPs; |
2071 | left = TOPs; | |
2072 | value = do_ncmp(left, right); | |
2073 | if (value == 2) { | |
3280af22 | 2074 | SETs(&PL_sv_undef); |
79072805 | 2075 | } |
33efebe6 DM |
2076 | else { |
2077 | dTARGET; | |
2078 | SETi(value); | |
2079 | } | |
2080 | RETURN; | |
a0d0e21e | 2081 | } |
79072805 | 2082 | |
afd9910b | 2083 | PP(pp_sle) |
a0d0e21e | 2084 | { |
97aff369 | 2085 | dVAR; dSP; |
79072805 | 2086 | |
afd9910b NC |
2087 | int amg_type = sle_amg; |
2088 | int multiplier = 1; | |
2089 | int rhs = 1; | |
79072805 | 2090 | |
afd9910b NC |
2091 | switch (PL_op->op_type) { |
2092 | case OP_SLT: | |
2093 | amg_type = slt_amg; | |
2094 | /* cmp < 0 */ | |
2095 | rhs = 0; | |
2096 | break; | |
2097 | case OP_SGT: | |
2098 | amg_type = sgt_amg; | |
2099 | /* cmp > 0 */ | |
2100 | multiplier = -1; | |
2101 | rhs = 0; | |
2102 | break; | |
2103 | case OP_SGE: | |
2104 | amg_type = sge_amg; | |
2105 | /* cmp >= 0 */ | |
2106 | multiplier = -1; | |
2107 | break; | |
79072805 | 2108 | } |
79072805 | 2109 | |
6f1401dc | 2110 | tryAMAGICbin_MG(amg_type, AMGf_set); |
a0d0e21e LW |
2111 | { |
2112 | dPOPTOPssrl; | |
1b6737cc | 2113 | const int cmp = (IN_LOCALE_RUNTIME |
078504b2 FC |
2114 | ? sv_cmp_locale_flags(left, right, 0) |
2115 | : sv_cmp_flags(left, right, 0)); | |
afd9910b | 2116 | SETs(boolSV(cmp * multiplier < rhs)); |
a0d0e21e LW |
2117 | RETURN; |
2118 | } | |
2119 | } | |
79072805 | 2120 | |
36477c24 | 2121 | PP(pp_seq) |
2122 | { | |
6f1401dc DM |
2123 | dVAR; dSP; |
2124 | tryAMAGICbin_MG(seq_amg, AMGf_set); | |
36477c24 | 2125 | { |
2126 | dPOPTOPssrl; | |
078504b2 | 2127 | SETs(boolSV(sv_eq_flags(left, right, 0))); |
a0d0e21e LW |
2128 | RETURN; |
2129 | } | |
2130 | } | |
79072805 | 2131 | |
a0d0e21e | 2132 | PP(pp_sne) |
79072805 | 2133 | { |
6f1401dc DM |
2134 | dVAR; dSP; |
2135 | tryAMAGICbin_MG(sne_amg, AMGf_set); | |
a0d0e21e LW |
2136 | { |
2137 | dPOPTOPssrl; | |
078504b2 | 2138 | SETs(boolSV(!sv_eq_flags(left, right, 0))); |
a0d0e21e | 2139 | RETURN; |
463ee0b2 | 2140 | } |
79072805 LW |
2141 | } |
2142 | ||
a0d0e21e | 2143 | PP(pp_scmp) |
79072805 | 2144 | { |
6f1401dc DM |
2145 | dVAR; dSP; dTARGET; |
2146 | tryAMAGICbin_MG(scmp_amg, 0); | |
a0d0e21e LW |
2147 | { |
2148 | dPOPTOPssrl; | |
1b6737cc | 2149 | const int cmp = (IN_LOCALE_RUNTIME |
078504b2 FC |
2150 | ? sv_cmp_locale_flags(left, right, 0) |
2151 | : sv_cmp_flags(left, right, 0)); | |
bbce6d69 | 2152 | SETi( cmp ); |
a0d0e21e LW |
2153 | RETURN; |
2154 | } | |
2155 | } | |
79072805 | 2156 | |
55497cff | 2157 | PP(pp_bit_and) |
2158 | { | |
6f1401dc DM |
2159 | dVAR; dSP; dATARGET; |
2160 | tryAMAGICbin_MG(band_amg, AMGf_assign); | |
a0d0e21e LW |
2161 | { |
2162 | dPOPTOPssrl; | |
4633a7c4 | 2163 | if (SvNIOKp(left) || SvNIOKp(right)) { |
b20c4ee1 FC |
2164 | const bool left_ro_nonnum = !SvNIOKp(left) && SvREADONLY(left); |
2165 | const bool right_ro_nonnum = !SvNIOKp(right) && SvREADONLY(right); | |
d0ba1bd2 | 2166 | if (PL_op->op_private & HINT_INTEGER) { |
1b6737cc | 2167 | const IV i = SvIV_nomg(left) & SvIV_nomg(right); |
972b05a9 | 2168 | SETi(i); |
d0ba1bd2 JH |
2169 | } |
2170 | else { | |
1b6737cc | 2171 | const UV u = SvUV_nomg(left) & SvUV_nomg(right); |
972b05a9 | 2172 | SETu(u); |
d0ba1bd2 | 2173 | } |
5ee80e13 | 2174 | if (left_ro_nonnum && left != TARG) SvNIOK_off(left); |
b20c4ee1 | 2175 | if (right_ro_nonnum) SvNIOK_off(right); |
a0d0e21e LW |
2176 | } |
2177 | else { | |
533c011a | 2178 | do_vop(PL_op->op_type, TARG, left, right); |
a0d0e21e LW |
2179 | SETTARG; |
2180 | } | |
2181 | RETURN; | |
2182 | } | |
2183 | } | |
79072805 | 2184 | |
a0d0e21e LW |
2185 | PP(pp_bit_or) |
2186 | { | |
3658c1f1 NC |
2187 | dVAR; dSP; dATARGET; |
2188 | const int op_type = PL_op->op_type; | |
2189 | ||
6f1401dc | 2190 | tryAMAGICbin_MG((op_type == OP_BIT_OR ? bor_amg : bxor_amg), AMGf_assign); |
a0d0e21e LW |
2191 | { |
2192 | dPOPTOPssrl; | |
4633a7c4 | 2193 | if (SvNIOKp(left) || SvNIOKp(right)) { |
b20c4ee1 FC |
2194 | const bool left_ro_nonnum = !SvNIOKp(left) && SvREADONLY(left); |
2195 | const bool right_ro_nonnum = !SvNIOKp(right) && SvREADONLY(right); | |
d0ba1bd2 | 2196 | if (PL_op->op_private & HINT_INTEGER) { |
3658c1f1 NC |
2197 | const IV l = (USE_LEFT(left) ? SvIV_nomg(left) : 0); |
2198 | const IV r = SvIV_nomg(right); | |
2199 | const IV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r); | |
2200 | SETi(result); | |
d0ba1bd2 JH |
2201 | } |
2202 | else { | |
3658c1f1 NC |
2203 | const UV l = (USE_LEFT(left) ? SvUV_nomg(left) : 0); |
2204 | const UV r = SvUV_nomg(right); | |
2205 | const UV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r); | |
2206 | SETu(result); | |
d0ba1bd2 | 2207 | } |
5ee80e13 | 2208 | if (left_ro_nonnum && left != TARG) SvNIOK_off(left); |
b20c4ee1 | 2209 | if (right_ro_nonnum) SvNIOK_off(right); |
a0d0e21e LW |
2210 | } |
2211 | else { | |
3658c1f1 | 2212 | do_vop(op_type, TARG, left, right); |
a0d0e21e LW |
2213 | SETTARG; |
2214 | } | |
2215 | RETURN; | |
79072805 | 2216 | } |
a0d0e21e | 2217 | } |
79072805 | 2218 | |
1c2b3fd6 FC |
2219 | PERL_STATIC_INLINE bool |
2220 | S_negate_string(pTHX) | |
2221 | { | |
2222 | dTARGET; dSP; | |
2223 | STRLEN len; | |
2224 | const char *s; | |
2225 | SV * const sv = TOPs; | |
2226 | if (!SvPOKp(sv) || SvNIOK(sv) || (!SvPOK(sv) && SvNIOKp(sv))) | |
2227 | return FALSE; | |
2228 | s = SvPV_nomg_const(sv, len); | |
2229 | if (isIDFIRST(*s)) { | |
2230 | sv_setpvs(TARG, "-"); | |
2231 | sv_catsv(TARG, sv); | |
2232 | } | |
2233 | else if (*s == '+' || (*s == '-' && !looks_like_number(sv))) { | |
2234 | sv_setsv_nomg(TARG, sv); | |
2235 | *SvPV_force_nomg(TARG, len) = *s == '-' ? '+' : '-'; | |
2236 | } | |
2237 | else return FALSE; | |
2238 | SETTARG; PUTBACK; | |
2239 | return TRUE; | |
2240 | } | |
2241 | ||
a0d0e21e LW |
2242 | PP(pp_negate) |
2243 | { | |
6f1401dc DM |
2244 | dVAR; dSP; dTARGET; |
2245 | tryAMAGICun_MG(neg_amg, AMGf_numeric); | |
1c2b3fd6 | 2246 | if (S_negate_string(aTHX)) return NORMAL; |
a0d0e21e | 2247 | { |
6f1401dc | 2248 | SV * const sv = TOPs; |
a5b92898 | 2249 | |
d96ab1b5 | 2250 | if (SvIOK(sv)) { |
7dbe3150 | 2251 | /* It's publicly an integer */ |
28e5dec8 | 2252 | oops_its_an_int: |
9b0e499b GS |
2253 | if (SvIsUV(sv)) { |
2254 | if (SvIVX(sv) == IV_MIN) { | |
28e5dec8 | 2255 | /* 2s complement assumption. */ |
d14578b8 KW |
2256 | SETi(SvIVX(sv)); /* special case: -((UV)IV_MAX+1) == |
2257 | IV_MIN */ | |
9b0e499b GS |
2258 | RETURN; |
2259 | } | |
2260 | else if (SvUVX(sv) <= IV_MAX) { | |
beccb14c | 2261 | SETi(-SvIVX(sv)); |
9b0e499b GS |
2262 | RETURN; |
2263 | } | |
2264 | } | |
2265 | else if (SvIVX(sv) != IV_MIN) { | |
2266 | SETi(-SvIVX(sv)); | |
2267 | RETURN; | |
2268 | } | |
28e5dec8 JH |
2269 | #ifdef PERL_PRESERVE_IVUV |
2270 | else { | |
2271 | SETu((UV)IV_MIN); | |
2272 | RETURN; | |
2273 | } | |
2274 | #endif | |
9b0e499b | 2275 | } |
8a5decd8 | 2276 | if (SvNIOKp(sv) && (SvNIOK(sv) || !SvPOK(sv))) |
6f1401dc | 2277 | SETn(-SvNV_nomg(sv)); |
1c2b3fd6 | 2278 | else if (SvPOKp(sv) && SvIV_please_nomg(sv)) |
8eb28a70 | 2279 | goto oops_its_an_int; |
4633a7c4 | 2280 | else |
6f1401dc | 2281 | SETn(-SvNV_nomg(sv)); |
79072805 | 2282 | } |
a0d0e21e | 2283 | RETURN; |
79072805 LW |
2284 | } |
2285 | ||
a0d0e21e | 2286 | PP(pp_not) |
79072805 | 2287 | { |
6f1401dc DM |
2288 | dVAR; dSP; |
2289 | tryAMAGICun_MG(not_amg, AMGf_set); | |
06c841cf | 2290 | *PL_stack_sp = boolSV(!SvTRUE_nomg(*PL_stack_sp)); |
a0d0e21e | 2291 | return NORMAL; |
79072805 LW |
2292 | } |
2293 | ||
a0d0e21e | 2294 | PP(pp_complement) |
79072805 | 2295 | { |
6f1401dc | 2296 | dVAR; dSP; dTARGET; |
a42d0242 | 2297 | tryAMAGICun_MG(compl_amg, AMGf_numeric); |
a0d0e21e LW |
2298 | { |
2299 | dTOPss; | |
4633a7c4 | 2300 | if (SvNIOKp(sv)) { |
d0ba1bd2 | 2301 | if (PL_op->op_private & HINT_INTEGER) { |
1b6737cc | 2302 | const IV i = ~SvIV_nomg(sv); |
972b05a9 | 2303 | SETi(i); |
d0ba1bd2 JH |
2304 | } |
2305 | else { | |
1b6737cc | 2306 | const UV u = ~SvUV_nomg(sv); |
972b05a9 | 2307 | SETu(u); |
d0ba1bd2 | 2308 | } |
a0d0e21e LW |
2309 | } |
2310 | else { | |
eb578fdb KW |
2311 | U8 *tmps; |
2312 | I32 anum; | |
a0d0e21e LW |
2313 | STRLEN len; |
2314 | ||
10516c54 | 2315 | (void)SvPV_nomg_const(sv,len); /* force check for uninit var */ |
891f9566 | 2316 | sv_setsv_nomg(TARG, sv); |
6f1401dc | 2317 | tmps = (U8*)SvPV_force_nomg(TARG, len); |
a0d0e21e | 2318 | anum = len; |
1d68d6cd | 2319 | if (SvUTF8(TARG)) { |
a1ca4561 | 2320 | /* Calculate exact length, let's not estimate. */ |
1d68d6cd | 2321 | STRLEN targlen = 0; |
ba210ebe | 2322 | STRLEN l; |
a1ca4561 YST |
2323 | UV nchar = 0; |
2324 | UV nwide = 0; | |
01f6e806 | 2325 | U8 * const send = tmps + len; |
74d49cd0 TS |
2326 | U8 * const origtmps = tmps; |
2327 | const UV utf8flags = UTF8_ALLOW_ANYUV; | |
1d68d6cd | 2328 | |
1d68d6cd | 2329 | while (tmps < send) { |
74d49cd0 TS |
2330 | const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags); |
2331 | tmps += l; | |
5bbb0b5a | 2332 | targlen += UNISKIP(~c); |
a1ca4561 YST |
2333 | nchar++; |
2334 | if (c > 0xff) | |
2335 | nwide++; | |
1d68d6cd SC |
2336 | } |
2337 | ||
2338 | /* Now rewind strings and write them. */ | |
74d49cd0 | 2339 | tmps = origtmps; |
a1ca4561 YST |
2340 | |
2341 | if (nwide) { | |
01f6e806 AL |
2342 | U8 *result; |
2343 | U8 *p; | |
2344 | ||
74d49cd0 | 2345 | Newx(result, targlen + 1, U8); |
01f6e806 | 2346 | p = result; |
a1ca4561 | 2347 | while (tmps < send) { |
74d49cd0 TS |
2348 | const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags); |
2349 | tmps += l; | |
01f6e806 | 2350 | p = uvchr_to_utf8_flags(p, ~c, UNICODE_ALLOW_ANY); |
a1ca4561 | 2351 | } |
01f6e806 | 2352 | *p = '\0'; |
c1c21316 NC |
2353 | sv_usepvn_flags(TARG, (char*)result, targlen, |
2354 | SV_HAS_TRAILING_NUL); | |
a1ca4561 YST |
2355 | SvUTF8_on(TARG); |
2356 | } | |
2357 | else { | |
01f6e806 AL |
2358 | U8 *result; |
2359 | U8 *p; | |
2360 | ||
74d49cd0 | 2361 | Newx(result, nchar + 1, U8); |
01f6e806 | 2362 | p = result; |
a1ca4561 | 2363 | while (tmps < send) { |
74d49cd0 TS |
2364 | const U8 c = (U8)utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags); |
2365 | tmps += l; | |
01f6e806 | 2366 | *p++ = ~c; |
a1ca4561 | 2367 | } |
01f6e806 | 2368 | *p = '\0'; |
c1c21316 | 2369 | sv_usepvn_flags(TARG, (char*)result, nchar, SV_HAS_TRAILING_NUL); |
d0a21e00 | 2370 | SvUTF8_off(TARG); |
1d68d6cd | 2371 | } |
ec93b65f | 2372 | SETTARG; |
1d68d6cd SC |
2373 | RETURN; |
2374 | } | |
a0d0e21e | 2375 | #ifdef LIBERAL |
51723571 | 2376 | { |
eb578fdb | 2377 | long *tmpl; |
51723571 JH |
2378 | for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++) |
2379 | *tmps = ~*tmps; | |
2380 | tmpl = (long*)tmps; | |
bb7a0f54 | 2381 | for ( ; anum >= (I32)sizeof(long); anum -= (I32)sizeof(long), tmpl++) |
51723571 JH |
2382 | *tmpl = ~*tmpl; |
2383 | tmps = (U8*)tmpl; | |
2384 | } | |
a0d0e21e LW |
2385 | #endif |
2386 | for ( ; anum > 0; anum--, tmps++) | |
2387 | *tmps = ~*tmps; | |
ec93b65f | 2388 | SETTARG; |
a0d0e21e LW |
2389 | } |
2390 | RETURN; | |
2391 | } | |
79072805 LW |
2392 | } |
2393 | ||
a0d0e21e LW |
2394 | /* integer versions of some of the above */ |
2395 | ||
a0d0e21e | 2396 | PP(pp_i_multiply) |
79072805 | 2397 | { |
6f1401dc DM |
2398 | dVAR; dSP; dATARGET; |
2399 | tryAMAGICbin_MG(mult_amg, AMGf_assign); | |
a0d0e21e | 2400 | { |
6f1401dc | 2401 | dPOPTOPiirl_nomg; |
a0d0e21e LW |
2402 | SETi( left * right ); |
2403 | RETURN; | |
2404 | } | |
79072805 LW |
2405 | } |
2406 | ||
a0d0e21e | 2407 | PP(pp_i_divide) |
79072805 | 2408 | { |
85935d8e | 2409 | IV num; |
6f1401dc DM |
2410 | dVAR; dSP; dATARGET; |
2411 | tryAMAGICbin_MG(div_amg, AMGf_assign); | |
a0d0e21e | 2412 | { |
6f1401dc | 2413 | dPOPTOPssrl; |
85935d8e | 2414 | IV value = SvIV_nomg(right); |
a0d0e21e | 2415 | if (value == 0) |
ece1bcef | 2416 | DIE(aTHX_ "Illegal division by zero"); |
85935d8e | 2417 | num = SvIV_nomg(left); |
a0cec769 YST |
2418 | |
2419 | /* avoid FPE_INTOVF on some platforms when num is IV_MIN */ | |
2420 | if (value == -1) | |
2421 | value = - num; | |
2422 | else | |
2423 | value = num / value; | |
6f1401dc | 2424 | SETi(value); |
a0d0e21e LW |
2425 | RETURN; |
2426 | } | |
79072805 LW |
2427 | } |
2428 | ||
a5bd31f4 | 2429 | #if defined(__GLIBC__) && IVSIZE == 8 && !defined(PERL_DEBUG_READONLY_OPS) |
224ec323 JH |
2430 | STATIC |
2431 | PP(pp_i_modulo_0) | |
befad5d1 NC |
2432 | #else |
2433 | PP(pp_i_modulo) | |
2434 | #endif | |
224ec323 JH |
2435 | { |
2436 | /* This is the vanilla old i_modulo. */ | |
6f1401dc DM |
2437 | dVAR; dSP; dATARGET; |
2438 | tryAMAGICbin_MG(modulo_amg, AMGf_assign); | |
224ec323 | 2439 | { |
6f1401dc | 2440 | dPOPTOPiirl_nomg; |
224ec323 JH |
2441 | if (!right) |
2442 | DIE(aTHX_ "Illegal modulus zero"); | |
a0cec769 YST |
2443 | /* avoid FPE_INTOVF on some platforms when left is IV_MIN */ |
2444 | if (right == -1) | |
2445 | SETi( 0 ); | |
2446 | else | |
2447 | SETi( left % right ); | |
224ec323 JH |
2448 | RETURN; |
2449 | } | |
2450 | } | |
2451 | ||
a5bd31f4 | 2452 | #if defined(__GLIBC__) && IVSIZE == 8 && !defined(PERL_DEBUG_READONLY_OPS) |
224ec323 JH |
2453 | STATIC |
2454 | PP(pp_i_modulo_1) | |
befad5d1 | 2455 | |
224ec323 | 2456 | { |
224ec323 | 2457 | /* This is the i_modulo with the workaround for the _moddi3 bug |
fce2b89e | 2458 | * in (at least) glibc 2.2.5 (the PERL_ABS() the workaround). |
224ec323 | 2459 | * See below for pp_i_modulo. */ |
6f1401dc DM |
2460 | dVAR; dSP; dATARGET; |
2461 | tryAMAGICbin_MG(modulo_amg, AMGf_assign); | |
224ec323 | 2462 | { |
6f1401dc | 2463 | dPOPTOPiirl_nomg; |
224ec323 JH |
2464 | if (!right) |
2465 | DIE(aTHX_ "Illegal modulus zero"); | |
a0cec769 YST |
2466 | /* avoid FPE_INTOVF on some platforms when left is IV_MIN */ |
2467 | if (right == -1) | |
2468 | SETi( 0 ); | |
2469 | else | |
2470 | SETi( left % PERL_ABS(right) ); | |
224ec323 JH |
2471 | RETURN; |
2472 | } | |
224ec323 JH |
2473 | } |
2474 | ||
a0d0e21e | 2475 | PP(pp_i_modulo) |
79072805 | 2476 | { |
6f1401dc DM |
2477 | dVAR; dSP; dATARGET; |
2478 | tryAMAGICbin_MG(modulo_amg, AMGf_assign); | |
224ec323 | 2479 | { |
6f1401dc | 2480 | dPOPTOPiirl_nomg; |
224ec323 JH |
2481 | if (!right) |
2482 | DIE(aTHX_ "Illegal modulus zero"); | |
2483 | /* The assumption is to use hereafter the old vanilla version... */ | |
2484 | PL_op->op_ppaddr = | |
2485 | PL_ppaddr[OP_I_MODULO] = | |
1c127fab | 2486 | Perl_pp_i_modulo_0; |
224ec323 JH |
2487 | /* .. but if we have glibc, we might have a buggy _moddi3 |
2488 | * (at least glicb 2.2.5 is known to have this bug), in other | |
2489 | * words our integer modulus with negative quad as the second | |
2490 | * argument might be broken. Test for this and re-patch the | |
2491 | * opcode dispatch table if that is the case, remembering to | |
2492 | * also apply the workaround so that this first round works | |
2493 | * right, too. See [perl #9402] for more information. */ | |
224ec323 JH |
2494 | { |
2495 | IV l = 3; | |
2496 | IV r = -10; | |
2497 | /* Cannot do this check with inlined IV constants since | |
2498 | * that seems to work correctly even with the buggy glibc. */ | |
2499 | if (l % r == -3) { | |
2500 | /* Yikes, we have the bug. | |
2501 | * Patch in the workaround version. */ | |
2502 | PL_op->op_ppaddr = | |
2503 | PL_ppaddr[OP_I_MODULO] = | |
2504 | &Perl_pp_i_modulo_1; | |
2505 | /* Make certain we work right this time, too. */ | |
32fdb065 | 2506 | right = PERL_ABS(right); |
224ec323 JH |
2507 | } |
2508 | } | |
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 % right ); | |
224ec323 JH |
2514 | RETURN; |
2515 | } | |
79072805 | 2516 | } |
befad5d1 | 2517 | #endif |
79072805 | 2518 | |
a0d0e21e | 2519 | PP(pp_i_add) |
79072805 | 2520 | { |
6f1401dc DM |
2521 | dVAR; dSP; dATARGET; |
2522 | tryAMAGICbin_MG(add_amg, AMGf_assign); | |
a0d0e21e | 2523 | { |
6f1401dc | 2524 | dPOPTOPiirl_ul_nomg; |
a0d0e21e LW |
2525 | SETi( left + right ); |
2526 | RETURN; | |
79072805 | 2527 | } |
79072805 LW |
2528 | } |
2529 | ||
a0d0e21e | 2530 | PP(pp_i_subtract) |
79072805 | 2531 | { |
6f1401dc DM |
2532 | dVAR; dSP; dATARGET; |
2533 | tryAMAGICbin_MG(subtr_amg, AMGf_assign); | |
a0d0e21e | 2534 | { |
6f1401dc | 2535 | dPOPTOPiirl_ul_nomg; |
a0d0e21e LW |
2536 | SETi( left - right ); |
2537 | RETURN; | |
79072805 | 2538 | } |
79072805 LW |
2539 | } |
2540 | ||
a0d0e21e | 2541 | PP(pp_i_lt) |
79072805 | 2542 | { |
6f1401dc DM |
2543 | dVAR; dSP; |
2544 | tryAMAGICbin_MG(lt_amg, AMGf_set); | |
a0d0e21e | 2545 | { |
96b6b87f | 2546 | dPOPTOPiirl_nomg; |
54310121 | 2547 | SETs(boolSV(left < right)); |
a0d0e21e LW |
2548 | RETURN; |
2549 | } | |
79072805 LW |
2550 | } |
2551 | ||
a0d0e21e | 2552 | PP(pp_i_gt) |
79072805 | 2553 | { |
6f1401dc DM |
2554 | dVAR; dSP; |
2555 | tryAMAGICbin_MG(gt_amg, AMGf_set); | |
a0d0e21e | 2556 | { |
96b6b87f | 2557 | dPOPTOPiirl_nomg; |
54310121 | 2558 | SETs(boolSV(left > right)); |
a0d0e21e LW |
2559 | RETURN; |
2560 | } | |
79072805 LW |
2561 | } |
2562 | ||
a0d0e21e | 2563 | PP(pp_i_le) |
79072805 | 2564 | { |
6f1401dc DM |
2565 | dVAR; dSP; |
2566 | tryAMAGICbin_MG(le_amg, AMGf_set); | |
a0d0e21e | 2567 | { |
96b6b87f | 2568 | dPOPTOPiirl_nomg; |
54310121 | 2569 | SETs(boolSV(left <= right)); |
a0d0e21e | 2570 | RETURN; |
85e6fe83 | 2571 | } |
79072805 LW |
2572 | } |
2573 | ||
a0d0e21e | 2574 | PP(pp_i_ge) |
79072805 | 2575 | { |
6f1401dc DM |
2576 | dVAR; dSP; |
2577 | tryAMAGICbin_MG(ge_amg, AMGf_set); | |
a0d0e21e | 2578 | { |
96b6b87f | 2579 | dPOPTOPiirl_nomg; |
54310121 | 2580 | SETs(boolSV(left >= right)); |
a0d0e21e LW |
2581 | RETURN; |
2582 | } | |
79072805 LW |
2583 | } |
2584 | ||
a0d0e21e | 2585 | PP(pp_i_eq) |
79072805 | 2586 | { |
6f1401dc DM |
2587 | dVAR; dSP; |
2588 | tryAMAGICbin_MG(eq_amg, AMGf_set); | |
a0d0e21e | 2589 | { |
96b6b87f | 2590 | dPOPTOPiirl_nomg; |
54310121 | 2591 | SETs(boolSV(left == right)); |
a0d0e21e LW |
2592 | RETURN; |
2593 | } | |
79072805 LW |
2594 | } |
2595 | ||
a0d0e21e | 2596 | PP(pp_i_ne) |
79072805 | 2597 | { |
6f1401dc DM |
2598 | dVAR; dSP; |
2599 | tryAMAGICbin_MG(ne_amg, AMGf_set); | |
a0d0e21e | 2600 | { |
96b6b87f | 2601 | dPOPTOPiirl_nomg; |
54310121 | 2602 | SETs(boolSV(left != right)); |
a0d0e21e LW |
2603 | RETURN; |
2604 | } | |
79072805 LW |
2605 | } |
2606 | ||
a0d0e21e | 2607 | PP(pp_i_ncmp) |
79072805 | 2608 | { |
6f1401dc DM |
2609 | dVAR; dSP; dTARGET; |
2610 | tryAMAGICbin_MG(ncmp_amg, 0); | |
a0d0e21e | 2611 | { |
96b6b87f | 2612 | dPOPTOPiirl_nomg; |
a0d0e21e | 2613 | I32 value; |
79072805 | 2614 | |
a0d0e21e | 2615 | if (left > right) |
79072805 | 2616 | value = 1; |
a0d0e21e | 2617 | else if (left < right) |
79072805 | 2618 | value = -1; |
a0d0e21e | 2619 | else |
79072805 | 2620 | value = 0; |
a0d0e21e LW |
2621 | SETi(value); |
2622 | RETURN; | |
79072805 | 2623 | } |
85e6fe83 LW |
2624 | } |
2625 | ||
2626 | PP(pp_i_negate) | |
2627 | { | |
6f1401dc DM |
2628 | dVAR; dSP; dTARGET; |
2629 | tryAMAGICun_MG(neg_amg, 0); | |
1c2b3fd6 | 2630 | if (S_negate_string(aTHX)) return NORMAL; |
6f1401dc DM |
2631 | { |
2632 | SV * const sv = TOPs; | |
2633 | IV const i = SvIV_nomg(sv); | |
2634 | SETi(-i); | |
2635 | RETURN; | |
2636 | } | |
85e6fe83 LW |
2637 | } |
2638 | ||
79072805 LW |
2639 | /* High falutin' math. */ |
2640 | ||
2641 | PP(pp_atan2) | |
2642 | { | |
6f1401dc DM |
2643 | dVAR; dSP; dTARGET; |
2644 | tryAMAGICbin_MG(atan2_amg, 0); | |
a0d0e21e | 2645 | { |
096c060c | 2646 | dPOPTOPnnrl_nomg; |
a1021d57 | 2647 | SETn(Perl_atan2(left, right)); |
a0d0e21e LW |
2648 | RETURN; |
2649 | } | |
79072805 LW |
2650 | } |
2651 | ||
2652 | PP(pp_sin) | |
2653 | { | |
71302fe3 NC |
2654 | dVAR; dSP; dTARGET; |
2655 | int amg_type = sin_amg; | |
2656 | const char *neg_report = NULL; | |
bc81784a | 2657 | NV (*func)(NV) = Perl_sin; |
71302fe3 NC |
2658 | const int op_type = PL_op->op_type; |
2659 | ||
2660 | switch (op_type) { | |
2661 | case OP_COS: | |
2662 | amg_type = cos_amg; | |
bc81784a | 2663 | func = Perl_cos; |
71302fe3 NC |
2664 | break; |
2665 | case OP_EXP: | |
2666 | amg_type = exp_amg; | |
bc81784a | 2667 | func = Perl_exp; |
71302fe3 NC |
2668 | break; |
2669 | case OP_LOG: | |
2670 | amg_type = log_amg; | |
bc81784a | 2671 | func = Perl_log; |
71302fe3 NC |
2672 | neg_report = "log"; |
2673 | break; | |
2674 | case OP_SQRT: | |
2675 | amg_type = sqrt_amg; | |
bc81784a | 2676 | func = Perl_sqrt; |
71302fe3 NC |
2677 | neg_report = "sqrt"; |
2678 | break; | |
a0d0e21e | 2679 | } |
79072805 | 2680 | |
6f1401dc DM |
2681 | |
2682 | tryAMAGICun_MG(amg_type, 0); | |
a0d0e21e | 2683 | { |
6f1401dc DM |
2684 | SV * const arg = POPs; |
2685 | const NV value = SvNV_nomg(arg); | |
71302fe3 NC |
2686 | if (neg_report) { |
2687 | if (op_type == OP_LOG ? (value <= 0.0) : (value < 0.0)) { | |
2688 | SET_NUMERIC_STANDARD(); | |
dcbac5bb | 2689 | /* diag_listed_as: Can't take log of %g */ |
71302fe3 NC |
2690 | DIE(aTHX_ "Can't take %s of %"NVgf, neg_report, value); |
2691 | } | |
2692 | } | |
2693 | XPUSHn(func(value)); | |
a0d0e21e LW |
2694 | RETURN; |
2695 | } | |
79072805 LW |
2696 | } |
2697 | ||
56cb0a1c AD |
2698 | /* Support Configure command-line overrides for rand() functions. |
2699 | After 5.005, perhaps we should replace this by Configure support | |
2700 | for drand48(), random(), or rand(). For 5.005, though, maintain | |
2701 | compatibility by calling rand() but allow the user to override it. | |
2702 | See INSTALL for details. --Andy Dougherty 15 July 1998 | |
2703 | */ | |
85ab1d1d JH |
2704 | /* Now it's after 5.005, and Configure supports drand48() and random(), |
2705 | in addition to rand(). So the overrides should not be needed any more. | |
2706 | --Jarkko Hietaniemi 27 September 1998 | |
2707 | */ | |
2708 | ||
2709 | #ifndef HAS_DRAND48_PROTO | |
20ce7b12 | 2710 | extern double drand48 (void); |
56cb0a1c AD |
2711 | #endif |
2712 | ||
79072805 LW |
2713 | PP(pp_rand) |
2714 | { | |
fdf4dddd | 2715 | dVAR; |
80252599 | 2716 | if (!PL_srand_called) { |
85ab1d1d | 2717 | (void)seedDrand01((Rand_seed_t)seed()); |
80252599 | 2718 | PL_srand_called = TRUE; |
93dc8474 | 2719 | } |
fdf4dddd DD |
2720 | { |
2721 | dSP; | |
2722 | NV value; | |
2723 | EXTEND(SP, 1); | |
2724 | ||
2725 | if (MAXARG < 1) | |
2726 | value = 1.0; | |
2727 | else { | |
2728 | SV * const sv = POPs; | |
2729 | if(!sv) | |
2730 | value = 1.0; | |
2731 | else | |
2732 | value = SvNV(sv); | |
2733 | } | |
2734 | /* 1 of 2 things can be carried through SvNV, SP or TARG, SP was carried */ | |
2735 | if (value == 0.0) | |
2736 | value = 1.0; | |
2737 | { | |
2738 | dTARGET; | |
2739 | PUSHs(TARG); | |
2740 | PUTBACK; | |
2741 | value *= Drand01(); | |
2742 | sv_setnv_mg(TARG, value); | |
2743 | } | |
2744 | } | |
2745 | return NORMAL; | |
79072805 LW |
2746 | } |
2747 | ||
2748 | PP(pp_srand) | |
2749 | { | |
83832992 | 2750 | dVAR; dSP; dTARGET; |
f914a682 JL |
2751 | UV anum; |
2752 | ||
0a5f3363 | 2753 | if (MAXARG >= 1 && (TOPs || POPs)) { |
f914a682 JL |
2754 | SV *top; |
2755 | char *pv; | |
2756 | STRLEN len; | |
2757 | int flags; | |
2758 | ||
2759 | top = POPs; | |
2760 | pv = SvPV(top, len); | |
2761 | flags = grok_number(pv, len, &anum); | |
2762 | ||
2763 | if (!(flags & IS_NUMBER_IN_UV)) { | |
2764 | Perl_ck_warner_d(aTHX_ packWARN(WARN_OVERFLOW), | |
2765 | "Integer overflow in srand"); | |
2766 | anum = UV_MAX; | |
2767 | } | |
2768 | } | |
2769 | else { | |
2770 | anum = seed(); | |
2771 | } | |
2772 | ||
85ab1d1d | 2773 | (void)seedDrand01((Rand_seed_t)anum); |
80252599 | 2774 | PL_srand_called = TRUE; |
da1010ec NC |
2775 | if (anum) |
2776 | XPUSHu(anum); | |
2777 | else { | |
2778 | /* Historically srand always returned true. We can avoid breaking | |
2779 | that like this: */ | |
2780 | sv_setpvs(TARG, "0 but true"); | |
2781 | XPUSHTARG; | |
2782 | } | |
83832992 | 2783 | RETURN; |
79072805 LW |
2784 | } |
2785 | ||
79072805 LW |
2786 | PP(pp_int) |
2787 | { | |
6f1401dc DM |
2788 | dVAR; dSP; dTARGET; |
2789 | tryAMAGICun_MG(int_amg, AMGf_numeric); | |
774d564b | 2790 | { |
6f1401dc DM |
2791 | SV * const sv = TOPs; |
2792 | const IV iv = SvIV_nomg(sv); | |
28e5dec8 JH |
2793 | /* XXX it's arguable that compiler casting to IV might be subtly |
2794 | different from modf (for numbers inside (IV_MIN,UV_MAX)) in which | |
2795 | else preferring IV has introduced a subtle behaviour change bug. OTOH | |
2796 | relying on floating point to be accurate is a bug. */ | |
2797 | ||
c781a409 | 2798 | if (!SvOK(sv)) { |
922c4365 | 2799 | SETu(0); |
c781a409 RD |
2800 | } |
2801 | else if (SvIOK(sv)) { | |
2802 | if (SvIsUV(sv)) | |
6f1401dc | 2803 | SETu(SvUV_nomg(sv)); |
c781a409 | 2804 | else |
28e5dec8 | 2805 | SETi(iv); |
c781a409 | 2806 | } |
c781a409 | 2807 | else { |
6f1401dc | 2808 | const NV value = SvNV_nomg(sv); |
1048ea30 | 2809 | if (value >= 0.0) { |
28e5dec8 JH |
2810 | if (value < (NV)UV_MAX + 0.5) { |
2811 | SETu(U_V(value)); | |
2812 | } else { | |
059a1014 | 2813 | SETn(Perl_floor(value)); |
28e5dec8 | 2814 | } |
1048ea30 | 2815 | } |
28e5dec8 JH |
2816 | else { |
2817 | if (value > (NV)IV_MIN - 0.5) { | |
2818 | SETi(I_V(value)); | |
2819 | } else { | |
1bbae031 | 2820 | SETn(Perl_ceil(value)); |
28e5dec8 JH |
2821 | } |
2822 | } | |
774d564b | 2823 | } |
79072805 | 2824 | } |
79072805 LW |
2825 | RETURN; |
2826 | } | |
2827 | ||
463ee0b2 LW |
2828 | PP(pp_abs) |
2829 | { | |
6f1401dc DM |
2830 | dVAR; dSP; dTARGET; |
2831 | tryAMAGICun_MG(abs_amg, AMGf_numeric); | |
a0d0e21e | 2832 | { |
6f1401dc | 2833 | SV * const sv = TOPs; |
28e5dec8 | 2834 | /* This will cache the NV value if string isn't actually integer */ |
6f1401dc | 2835 | const IV iv = SvIV_nomg(sv); |
a227d84d | 2836 | |
800401ee | 2837 | if (!SvOK(sv)) { |
922c4365 | 2838 | SETu(0); |
800401ee JH |
2839 | } |
2840 | else if (SvIOK(sv)) { | |
28e5dec8 | 2841 | /* IVX is precise */ |
800401ee | 2842 | if (SvIsUV(sv)) { |
6f1401dc | 2843 | SETu(SvUV_nomg(sv)); /* force it to be numeric only */ |
28e5dec8 JH |
2844 | } else { |
2845 | if (iv >= 0) { | |
2846 | SETi(iv); | |
2847 | } else { | |
2848 | if (iv != IV_MIN) { | |
2849 | SETi(-iv); | |
2850 | } else { | |
2851 | /* 2s complement assumption. Also, not really needed as | |
2852 | IV_MIN and -IV_MIN should both be %100...00 and NV-able */ | |
2853 | SETu(IV_MIN); | |
2854 | } | |
a227d84d | 2855 | } |
28e5dec8 JH |
2856 | } |
2857 | } else{ | |
6f1401dc | 2858 | const NV value = SvNV_nomg(sv); |
774d564b | 2859 | if (value < 0.0) |
1b6737cc | 2860 | SETn(-value); |
a4474c9e DD |
2861 | else |
2862 | SETn(value); | |
774d564b | 2863 | } |
a0d0e21e | 2864 | } |
774d564b | 2865 | RETURN; |
463ee0b2 LW |
2866 | } |
2867 | ||
79072805 LW |
2868 | PP(pp_oct) |
2869 | { | |
97aff369 | 2870 | dVAR; dSP; dTARGET; |
5c144d81 | 2871 | const char *tmps; |
53305cf1 | 2872 | I32 flags = PERL_SCAN_ALLOW_UNDERSCORES; |
6f894ead | 2873 | STRLEN len; |
53305cf1 NC |
2874 | NV result_nv; |
2875 | UV result_uv; | |
1b6737cc | 2876 | SV* const sv = POPs; |
79072805 | 2877 | |
349d4f2f | 2878 | tmps = (SvPV_const(sv, len)); |
2bc69dc4 NIS |
2879 | if (DO_UTF8(sv)) { |
2880 | /* If Unicode, try to downgrade | |
2881 | * If not possible, croak. */ | |
1b6737cc | 2882 | SV* const tsv = sv_2mortal(newSVsv(sv)); |
2bc69dc4 NIS |
2883 | |
2884 | SvUTF8_on(tsv); | |
2885 | sv_utf8_downgrade(tsv, FALSE); | |
349d4f2f | 2886 | tmps = SvPV_const(tsv, len); |
2bc69dc4 | 2887 | } |
daa2adfd NC |
2888 | if (PL_op->op_type == OP_HEX) |
2889 | goto hex; | |
2890 | ||
6f894ead | 2891 | while (*tmps && len && isSPACE(*tmps)) |
53305cf1 | 2892 | tmps++, len--; |
9e24b6e2 | 2893 | if (*tmps == '0') |
53305cf1 | 2894 | tmps++, len--; |
a674e8db | 2895 | if (*tmps == 'x' || *tmps == 'X') { |
daa2adfd | 2896 | hex: |
53305cf1 | 2897 | result_uv = grok_hex (tmps, &len, &flags, &result_nv); |
daa2adfd | 2898 | } |
a674e8db | 2899 | else if (*tmps == 'b' || *tmps == 'B') |
53305cf1 | 2900 | result_uv = grok_bin (tmps, &len, &flags, &result_nv); |
464e2e8a | 2901 | else |
53305cf1 NC |
2902 | result_uv = grok_oct (tmps, &len, &flags, &result_nv); |
2903 | ||
2904 | if (flags & PERL_SCAN_GREATER_THAN_UV_MAX) { | |
2905 | XPUSHn(result_nv); | |
2906 | } | |
2907 | else { | |
2908 | XPUSHu(result_uv); | |
2909 | } | |
79072805 LW |
2910 | RETURN; |
2911 | } | |
2912 | ||
2913 | /* String stuff. */ | |
2914 | ||
2915 | PP(pp_length) | |
2916 | { | |
97aff369 | 2917 | dVAR; dSP; dTARGET; |
0bd48802 | 2918 | SV * const sv = TOPs; |
a0ed51b3 | 2919 | |
0f43fd57 FC |
2920 | SvGETMAGIC(sv); |
2921 | if (SvOK(sv)) { | |
193059ca | 2922 | if (!IN_BYTES) |
0f43fd57 | 2923 | SETi(sv_len_utf8_nomg(sv)); |
9f621bb0 | 2924 | else |
0f43fd57 FC |
2925 | { |
2926 | STRLEN len; | |
2927 | (void)SvPV_nomg_const(sv,len); | |
2928 | SETi(len); | |
2929 | } | |
656266fc | 2930 | } else { |
9407f9c1 DL |
2931 | if (!SvPADTMP(TARG)) { |
2932 | sv_setsv_nomg(TARG, &PL_sv_undef); | |
2933 | SETTARG; | |
2934 | } | |
2935 | SETs(&PL_sv_undef); | |
92331800 | 2936 | } |
79072805 LW |
2937 | RETURN; |
2938 | } | |
2939 | ||
83f78d1a FC |
2940 | /* Returns false if substring is completely outside original string. |
2941 | No length is indicated by len_iv = 0 and len_is_uv = 0. len_is_uv must | |
2942 | always be true for an explicit 0. | |
2943 | */ | |
2944 | bool | |
2945 | Perl_translate_substr_offsets(pTHX_ STRLEN curlen, IV pos1_iv, | |
2946 | bool pos1_is_uv, IV len_iv, | |
2947 | bool len_is_uv, STRLEN *posp, | |
2948 | STRLEN *lenp) | |
2949 | { | |
2950 | IV pos2_iv; | |
2951 | int pos2_is_uv; | |
2952 | ||
2953 | PERL_ARGS_ASSERT_TRANSLATE_SUBSTR_OFFSETS; | |
2954 | ||
2955 | if (!pos1_is_uv && pos1_iv < 0 && curlen) { | |
2956 | pos1_is_uv = curlen-1 > ~(UV)pos1_iv; | |
2957 | pos1_iv += curlen; | |
2958 | } | |
2959 | if ((pos1_is_uv || pos1_iv > 0) && (UV)pos1_iv > curlen) | |
2960 | return FALSE; | |
2961 | ||
2962 | if (len_iv || len_is_uv) { | |
2963 | if (!len_is_uv && len_iv < 0) { | |
2964 | pos2_iv = curlen + len_iv; | |
2965 | if (curlen) | |
2966 | pos2_is_uv = curlen-1 > ~(UV)len_iv; | |
2967 | else | |
2968 | pos2_is_uv = 0; | |
2969 | } else { /* len_iv >= 0 */ | |
2970 | if (!pos1_is_uv && pos1_iv < 0) { | |
2971 | pos2_iv = pos1_iv + len_iv; | |
2972 | pos2_is_uv = (UV)len_iv > (UV)IV_MAX; | |
2973 | } else { | |
2974 | if ((UV)len_iv > curlen-(UV)pos1_iv) | |
2975 | pos2_iv = curlen; | |
2976 | else | |
2977 | pos2_iv = pos1_iv+len_iv; | |
2978 | pos2_is_uv = 1; | |
2979 | } | |
2980 | } | |
2981 | } | |
2982 | else { | |
2983 | pos2_iv = curlen; | |
2984 | pos2_is_uv = 1; | |
2985 | } | |
2986 | ||
2987 | if (!pos2_is_uv && pos2_iv < 0) { | |
2988 | if (!pos1_is_uv && pos1_iv < 0) | |
2989 | return FALSE; | |
2990 | pos2_iv = 0; | |
2991 | } | |
2992 | else if (!pos1_is_uv && pos1_iv < 0) | |
2993 | pos1_iv = 0; | |
2994 | ||
2995 | if ((UV)pos2_iv < (UV)pos1_iv) | |
2996 | pos2_iv = pos1_iv; | |
2997 | if ((UV)pos2_iv > curlen) | |
2998 | pos2_iv = curlen; | |
2999 | ||
3000 | /* pos1_iv and pos2_iv both in 0..curlen, so the cast is safe */ | |
3001 | *posp = (STRLEN)( (UV)pos1_iv ); | |
3002 | *lenp = (STRLEN)( (UV)pos2_iv - (UV)pos1_iv ); | |
3003 | ||
3004 | return TRUE; | |
3005 | } | |
3006 | ||
79072805 LW |
3007 | PP(pp_substr) |
3008 | { | |
97aff369 | 3009 | dVAR; dSP; dTARGET; |
79072805 | 3010 | SV *sv; |
463ee0b2 | 3011 | STRLEN curlen; |
9402d6ed | 3012 | STRLEN utf8_curlen; |
777f7c56 EB |
3013 | SV * pos_sv; |
3014 | IV pos1_iv; | |
3015 | int pos1_is_uv; | |
777f7c56 EB |
3016 | SV * len_sv; |
3017 | IV len_iv = 0; | |
83f78d1a | 3018 | int len_is_uv = 0; |
24fcb59f | 3019 | I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET; |
bbddc9e0 | 3020 | const bool rvalue = (GIMME_V != G_VOID); |
e1ec3a88 | 3021 | const char *tmps; |
9402d6ed | 3022 | SV *repl_sv = NULL; |
cbbf8932 | 3023 | const char *repl = NULL; |
7b8d334a | 3024 | STRLEN repl_len; |
7bc95ae1 | 3025 | int num_args = PL_op->op_private & 7; |
13e30c65 | 3026 | bool repl_need_utf8_upgrade = FALSE; |
79072805 | 3027 | |
78f9721b SM |
3028 | if (num_args > 2) { |
3029 | if (num_args > 3) { | |
24fcb59f | 3030 | if(!(repl_sv = POPs)) num_args--; |
7bc95ae1 FC |
3031 | } |
3032 | if ((len_sv = POPs)) { | |
3033 | len_iv = SvIV(len_sv); | |
83f78d1a | 3034 | len_is_uv = len_iv ? SvIOK_UV(len_sv) : 1; |
7b8d334a | 3035 | } |
7bc95ae1 | 3036 | else num_args--; |
5d82c453 | 3037 | } |
777f7c56 EB |
3038 | pos_sv = POPs; |
3039 | pos1_iv = SvIV(pos_sv); | |
3040 | pos1_is_uv = SvIOK_UV(pos_sv); | |
79072805 | 3041 | sv = POPs; |
24fcb59f FC |
3042 | if (PL_op->op_private & OPpSUBSTR_REPL_FIRST) { |
3043 | assert(!repl_sv); | |
3044 | repl_sv = POPs; | |
3045 | } | |
849ca7ee | 3046 | PUTBACK; |
6582db62 | 3047 | if (lvalue && !repl_sv) { |
83f78d1a FC |
3048 | SV * ret; |
3049 | ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */ | |
3050 | sv_magic(ret, NULL, PERL_MAGIC_substr, NULL, 0); | |
3051 | LvTYPE(ret) = 'x'; | |
3052 | LvTARG(ret) = SvREFCNT_inc_simple(sv); | |
3053 | LvTARGOFF(ret) = | |
3054 | pos1_is_uv || pos1_iv >= 0 | |
3055 | ? (STRLEN)(UV)pos1_iv | |
3056 | : (LvFLAGS(ret) |= 1, (STRLEN)(UV)-pos1_iv); | |
3057 | LvTARGLEN(ret) = | |
3058 | len_is_uv || len_iv > 0 | |
3059 | ? (STRLEN)(UV)len_iv | |
3060 | : (LvFLAGS(ret) |= 2, (STRLEN)(UV)-len_iv); | |
3061 | ||
3062 | SPAGAIN; | |
3063 | PUSHs(ret); /* avoid SvSETMAGIC here */ | |
3064 | RETURN; | |
a74fb2cd | 3065 | } |
6582db62 FC |
3066 | if (repl_sv) { |
3067 | repl = SvPV_const(repl_sv, repl_len); | |
3068 | SvGETMAGIC(sv); | |
3069 | if (SvROK(sv)) | |
3070 | Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR), | |
3071 | "Attempt to use reference as lvalue in substr" | |
3072 | ); | |
3073 | tmps = SvPV_force_nomg(sv, curlen); | |
3074 | if (DO_UTF8(repl_sv) && repl_len) { | |
3075 | if (!DO_UTF8(sv)) { | |
01680ee9 | 3076 | sv_utf8_upgrade_nomg(sv); |
6582db62 FC |
3077 | curlen = SvCUR(sv); |
3078 | } | |
3079 | } | |
3080 | else if (DO_UTF8(sv)) | |
3081 | repl_need_utf8_upgrade = TRUE; | |
3082 | } | |
3083 | else tmps = SvPV_const(sv, curlen); | |
7e2040f0 | 3084 | if (DO_UTF8(sv)) { |
0d788f38 | 3085 | utf8_curlen = sv_or_pv_len_utf8(sv, tmps, curlen); |
9402d6ed JH |
3086 | if (utf8_curlen == curlen) |
3087 | utf8_curlen = 0; | |
a0ed51b3 | 3088 | else |
9402d6ed | 3089 | curlen = utf8_curlen; |
a0ed51b3 | 3090 | } |
d1c2b58a | 3091 | else |
9402d6ed | 3092 | utf8_curlen = 0; |
a0ed51b3 | 3093 | |
83f78d1a FC |
3094 | { |
3095 | STRLEN pos, len, byte_len, byte_pos; | |
777f7c56 | 3096 | |
83f78d1a FC |
3097 | if (!translate_substr_offsets( |
3098 | curlen, pos1_iv, pos1_is_uv, len_iv, len_is_uv, &pos, &len | |
3099 | )) goto bound_fail; | |
777f7c56 | 3100 | |
83f78d1a FC |
3101 | byte_len = len; |
3102 | byte_pos = utf8_curlen | |
0d788f38 | 3103 | ? sv_or_pv_pos_u2b(sv, tmps, pos, &byte_len) : pos; |
d931b1be | 3104 | |
2154eca7 | 3105 | tmps += byte_pos; |
bbddc9e0 CS |
3106 | |
3107 | if (rvalue) { | |
3108 | SvTAINTED_off(TARG); /* decontaminate */ | |
3109 | SvUTF8_off(TARG); /* decontaminate */ | |
3110 | sv_setpvn(TARG, tmps, byte_len); | |
12aa1545 | 3111 | #ifdef USE_LOCALE_COLLATE |
bbddc9e0 | 3112 | sv_unmagic(TARG, PERL_MAGIC_collxfrm); |
12aa1545 | 3113 | #endif |
bbddc9e0 CS |
3114 | if (utf8_curlen) |
3115 | SvUTF8_on(TARG); | |
3116 | } | |
2154eca7 | 3117 | |
f7928d6c | 3118 | if (repl) { |
13e30c65 JH |
3119 | SV* repl_sv_copy = NULL; |
3120 | ||
3121 | if (repl_need_utf8_upgrade) { | |
3122 | repl_sv_copy = newSVsv(repl_sv); | |
3123 | sv_utf8_upgrade(repl_sv_copy); | |
349d4f2f | 3124 | repl = SvPV_const(repl_sv_copy, repl_len); |
13e30c65 | 3125 | } |
502d9230 VP |
3126 | if (!SvOK(sv)) |
3127 | sv_setpvs(sv, ""); | |
777f7c56 | 3128 | sv_insert_flags(sv, byte_pos, byte_len, repl, repl_len, 0); |
ef8d46e8 | 3129 | SvREFCNT_dec(repl_sv_copy); |
f7928d6c | 3130 | } |
79072805 | 3131 | } |
849ca7ee | 3132 | SPAGAIN; |
bbddc9e0 CS |
3133 | if (rvalue) { |
3134 | SvSETMAGIC(TARG); | |
3135 | PUSHs(TARG); | |
3136 | } | |
79072805 | 3137 | RETURN; |
777f7c56 | 3138 | |
1c900557 | 3139 | bound_fail: |
83f78d1a | 3140 | if (repl) |
777f7c56 EB |
3141 | Perl_croak(aTHX_ "substr outside of string"); |
3142 | Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR), "substr outside of string"); | |
3143 | RETPUSHUNDEF; | |
79072805 LW |
3144 | } |
3145 | ||
3146 | PP(pp_vec) | |
3147 | { | |
2154eca7 | 3148 | dVAR; dSP; |
eb578fdb KW |
3149 | const IV size = POPi; |
3150 | const IV offset = POPi; | |
3151 | SV * const src = POPs; | |
1b6737cc | 3152 | const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET; |
2154eca7 | 3153 | SV * ret; |
a0d0e21e | 3154 | |
81e118e0 | 3155 | if (lvalue) { /* it's an lvalue! */ |
2154eca7 EB |
3156 | ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */ |
3157 | sv_magic(ret, NULL, PERL_MAGIC_vec, NULL, 0); | |
3158 | LvTYPE(ret) = 'v'; | |
3159 | LvTARG(ret) = SvREFCNT_inc_simple(src); | |
3160 | LvTARGOFF(ret) = offset; | |
3161 | LvTARGLEN(ret) = size; | |
3162 | } | |
3163 | else { | |
3164 | dTARGET; | |
3165 | SvTAINTED_off(TARG); /* decontaminate */ | |
3166 | ret = TARG; | |
79072805 LW |
3167 | } |
3168 | ||
2154eca7 EB |
3169 | sv_setuv(ret, do_vecget(src, offset, size)); |
3170 | PUSHs(ret); | |
79072805 LW |
3171 | RETURN; |
3172 | } | |
3173 | ||
3174 | PP(pp_index) | |
3175 | { | |
97aff369 | 3176 | dVAR; dSP; dTARGET; |
79072805 LW |
3177 | SV *big; |
3178 | SV *little; | |
c445ea15 | 3179 | SV *temp = NULL; |
ad66a58c | 3180 | STRLEN biglen; |
2723d216 | 3181 | STRLEN llen = 0; |
79072805 LW |
3182 | I32 offset; |
3183 | I32 retval; | |
73ee8be2 NC |
3184 | const char *big_p; |
3185 | const char *little_p; | |
2f040f7f NC |
3186 | bool big_utf8; |
3187 | bool little_utf8; | |
2723d216 | 3188 | const bool is_index = PL_op->op_type == OP_INDEX; |
d3e26383 | 3189 | const bool threeargs = MAXARG >= 3 && (TOPs || ((void)POPs,0)); |
79072805 | 3190 | |
e1dccc0d Z |
3191 | if (threeargs) |
3192 | offset = POPi; | |
79072805 LW |
3193 | little = POPs; |
3194 | big = POPs; | |
73ee8be2 NC |
3195 | big_p = SvPV_const(big, biglen); |
3196 | little_p = SvPV_const(little, llen); | |
3197 | ||
e609e586 NC |
3198 | big_utf8 = DO_UTF8(big); |
3199 | little_utf8 = DO_UTF8(little); | |
3200 | if (big_utf8 ^ little_utf8) { | |
3201 | /* One needs to be upgraded. */ | |
2f040f7f NC |
3202 | if (little_utf8 && !PL_encoding) { |
3203 | /* Well, maybe instead we might be able to downgrade the small | |
3204 | string? */ | |
1eced8f8 | 3205 | char * const pv = (char*)bytes_from_utf8((U8 *)little_p, &llen, |
2f040f7f NC |
3206 | &little_utf8); |
3207 | if (little_utf8) { | |
3208 | /* If the large string is ISO-8859-1, and it's not possible to | |
3209 | convert the small string to ISO-8859-1, then there is no | |
3210 | way that it could be found anywhere by index. */ | |
3211 | retval = -1; | |
3212 | goto fail; | |
3213 | } | |
e609e586 | 3214 | |
2f040f7f NC |
3215 | /* At this point, pv is a malloc()ed string. So donate it to temp |
3216 | to ensure it will get free()d */ | |
3217 | little = temp = newSV(0); | |
73ee8be2 NC |
3218 | sv_usepvn(temp, pv, llen); |
3219 | little_p = SvPVX(little); | |
e609e586 | 3220 | } else { |
73ee8be2 NC |
3221 | temp = little_utf8 |
3222 | ? newSVpvn(big_p, biglen) : newSVpvn(little_p, llen); | |
2f040f7f NC |
3223 | |
3224 | if (PL_encoding) { | |
3225 | sv_recode_to_utf8(temp, PL_encoding); | |
3226 | } else { | |
3227 | sv_utf8_upgrade(temp); | |
3228 | } | |
3229 | if (little_utf8) { | |
3230 | big = temp; | |
3231 | big_utf8 = TRUE; | |
73ee8be2 | 3232 | big_p = SvPV_const(big, biglen); |
2f040f7f NC |
3233 | } else { |
3234 | little = temp; | |
73ee8be2 | 3235 | little_p = SvPV_const(little, llen); |
2f040f7f | 3236 | } |
e609e586 NC |
3237 | } |
3238 | } | |
73ee8be2 NC |
3239 | if (SvGAMAGIC(big)) { |
3240 | /* Life just becomes a lot easier if I use a temporary here. | |
3241 | Otherwise I need to avoid calls to sv_pos_u2b(), which (dangerously) | |
3242 | will trigger magic and overloading again, as will fbm_instr() | |
3243 | */ | |
59cd0e26 NC |
3244 | big = newSVpvn_flags(big_p, biglen, |
3245 | SVs_TEMP | (big_utf8 ? SVf_UTF8 : 0)); | |
73ee8be2 NC |
3246 | big_p = SvPVX(big); |
3247 | } | |
e4e44778 | 3248 | if (SvGAMAGIC(little) || (is_index && !SvOK(little))) { |
73ee8be2 NC |
3249 | /* index && SvOK() is a hack. fbm_instr() calls SvPV_const, which will |
3250 | warn on undef, and we've already triggered a warning with the | |
3251 | SvPV_const some lines above. We can't remove that, as we need to | |
3252 | call some SvPV to trigger overloading early and find out if the | |
3253 | string is UTF-8. | |
3254 | This is all getting to messy. The API isn't quite clean enough, | |
3255 | because data access has side effects. | |
3256 | */ | |
59cd0e26 NC |
3257 | little = newSVpvn_flags(little_p, llen, |
3258 | SVs_TEMP | (little_utf8 ? SVf_UTF8 : 0)); | |
73ee8be2 NC |
3259 | little_p = SvPVX(little); |
3260 | } | |
e609e586 | 3261 | |
d3e26383 | 3262 | if (!threeargs) |
2723d216 | 3263 | offset = is_index ? 0 : biglen; |
a0ed51b3 | 3264 | else { |
ad66a58c | 3265 | if (big_utf8 && offset > 0) |
a0ed51b3 | 3266 | sv_pos_u2b(big, &offset, 0); |
73ee8be2 NC |
3267 | if (!is_index) |
3268 | offset += llen; | |
a0ed51b3 | 3269 | } |
79072805 LW |
3270 | if (offset < 0) |
3271 | offset = 0; | |
ad66a58c NC |
3272 | else if (offset > (I32)biglen) |
3273 | offset = biglen; | |
73ee8be2 NC |
3274 | if (!(little_p = is_index |
3275 | ? fbm_instr((unsigned char*)big_p + offset, | |
3276 | (unsigned char*)big_p + biglen, little, 0) | |
3277 | : rninstr(big_p, big_p + offset, | |
3278 | little_p, little_p + llen))) | |
a0ed51b3 | 3279 | retval = -1; |
ad66a58c | 3280 | else { |
73ee8be2 | 3281 | retval = little_p - big_p; |
ad66a58c NC |
3282 | if (retval > 0 && big_utf8) |
3283 | sv_pos_b2u(big, &retval); | |
3284 | } | |
ef8d46e8 | 3285 | SvREFCNT_dec(temp); |
2723d216 | 3286 | fail: |
e1dccc0d | 3287 | PUSHi(retval); |
79072805 LW |
3288 | RETURN; |
3289 | } | |
3290 | ||
3291 | PP(pp_sprintf) | |
3292 | { | |
97aff369 | 3293 | dVAR; dSP; dMARK; dORIGMARK; dTARGET; |
3e6bd4bf | 3294 | SvTAINTED_off(TARG); |
79072805 | 3295 | do_sprintf(TARG, SP-MARK, MARK+1); |
bbce6d69 | 3296 | TAINT_IF(SvTAINTED(TARG)); |
79072805 LW |
3297 | SP = ORIGMARK; |
3298 | PUSHTARG; | |
3299 | RETURN; | |
3300 | } | |
3301 | ||
79072805 LW |
3302 | PP(pp_ord) |
3303 | { | |
97aff369 | 3304 | dVAR; dSP; dTARGET; |
1eced8f8 | 3305 | |
7df053ec | 3306 | SV *argsv = POPs; |
ba210ebe | 3307 | STRLEN len; |
349d4f2f | 3308 | const U8 *s = (U8*)SvPV_const(argsv, len); |
121910a4 | 3309 | |
799ef3cb | 3310 | if (PL_encoding && SvPOK(argsv) && !DO_UTF8(argsv)) { |
1eced8f8 | 3311 | SV * const tmpsv = sv_2mortal(newSVsv(argsv)); |
799ef3cb | 3312 | s = (U8*)sv_recode_to_utf8(tmpsv, PL_encoding); |
121910a4 JH |
3313 | argsv = tmpsv; |
3314 | } | |
79072805 | 3315 | |
872c91ae | 3316 | XPUSHu(DO_UTF8(argsv) ? |
89ebb4a3 | 3317 | utf8n_to_uvchr(s, UTF8_MAXBYTES, 0, UTF8_ALLOW_ANYUV) : |
5fc32dea | 3318 | (UV)(*s & 0xff)); |
68795e93 | 3319 | |
79072805 LW |
3320 | RETURN; |
3321 | } | |
3322 | ||
463ee0b2 LW |
3323 | PP(pp_chr) |
3324 | { | |
97aff369 | 3325 | dVAR; dSP; dTARGET; |
463ee0b2 | 3326 | char *tmps; |
8a064bd6 | 3327 | UV value; |
71739502 | 3328 | SV *top = POPs; |
8a064bd6 | 3329 | |
71739502 FC |
3330 | SvGETMAGIC(top); |
3331 | if (!IN_BYTES /* under bytes, chr(-1) eq chr(0xff), etc. */ | |
3332 | && ((SvIOKp(top) && !SvIsUV(top) && SvIV_nomg(top) < 0) | |
8a064bd6 | 3333 | || |
71739502 FC |
3334 | ((SvNOKp(top) || (SvOK(top) && !SvIsUV(top))) |
3335 | && SvNV_nomg(top) < 0.0))) { | |
b3fe8680 FC |
3336 | if (ckWARN(WARN_UTF8)) { |
3337 | if (SvGMAGICAL(top)) { | |
3338 | SV *top2 = sv_newmortal(); | |
3339 | sv_setsv_nomg(top2, top); | |
3340 | top = top2; | |
3341 | } | |
3342 | Perl_warner(aTHX_ packWARN(WARN_UTF8), | |
3343 | "Invalid negative number (%"SVf") in chr", top); | |
3344 | } | |
8a064bd6 | 3345 | value = UNICODE_REPLACEMENT; |
8a064bd6 | 3346 | } else { |
71739502 | 3347 | value = SvUV_nomg(top); |
8a064bd6 | 3348 | } |
463ee0b2 | 3349 | |
862a34c6 | 3350 | SvUPGRADE(TARG,SVt_PV); |
a0ed51b3 | 3351 | |
0064a8a9 | 3352 | if (value > 255 && !IN_BYTES) { |
eb160463 | 3353 | SvGROW(TARG, (STRLEN)UNISKIP(value)+1); |
62961d2e | 3354 | tmps = (char*)uvchr_to_utf8_flags((U8*)SvPVX(TARG), value, 0); |
349d4f2f | 3355 | SvCUR_set(TARG, tmps - SvPVX_const(TARG)); |
a0ed51b3 LW |
3356 | *tmps = '\0'; |
3357 | (void)SvPOK_only(TARG); | |
aa6ffa16 | 3358 | SvUTF8_on(TARG); |
a0ed51b3 LW |
3359 | XPUSHs(TARG); |
3360 | RETURN; | |
3361 | } | |
3362 | ||
748a9306 | 3363 | SvGROW(TARG,2); |
463ee0b2 LW |
3364 | SvCUR_set(TARG, 1); |
3365 | tmps = SvPVX(TARG); | |
eb160463 | 3366 | *tmps++ = (char)value; |
748a9306 | 3367 | *tmps = '\0'; |
a0d0e21e | 3368 | (void)SvPOK_only(TARG); |
4c5ed6e2 | 3369 | |
88632417 | 3370 | if (PL_encoding && !IN_BYTES) { |
799ef3cb | 3371 | sv_recode_to_utf8(TARG, PL_encoding); |
88632417 | 3372 | tmps = SvPVX(TARG); |
28936164 KW |
3373 | if (SvCUR(TARG) == 0 |
3374 | || ! is_utf8_string((U8*)tmps, SvCUR(TARG)) | |
3375 | || UTF8_IS_REPLACEMENT((U8*) tmps, (U8*) tmps + SvCUR(TARG))) | |
3376 | { | |
4c5ed6e2 | 3377 | SvGROW(TARG, 2); |
d5a15ac2 | 3378 | tmps = SvPVX(TARG); |
4c5ed6e2 TS |
3379 | SvCUR_set(TARG, 1); |
3380 | *tmps++ = (char)value; | |
88632417 | 3381 | *tmps = '\0'; |
4c5ed6e2 | 3382 | SvUTF8_off(TARG); |
88632417 JH |
3383 | } |
3384 | } | |
4c5ed6e2 | 3385 | |
463ee0b2 LW |
3386 | XPUSHs(TARG); |
3387 | RETURN; | |
3388 | } | |
3389 | ||
79072805 LW |
3390 | PP(pp_crypt) |
3391 | { | |
79072805 | 3392 | #ifdef HAS_CRYPT |
97aff369 | 3393 | dVAR; dSP; dTARGET; |
5f74f29c | 3394 | dPOPTOPssrl; |
85c16d83 | 3395 | STRLEN len; |
10516c54 | 3396 | const char *tmps = SvPV_const(left, len); |
2bc69dc4 | 3397 | |
85c16d83 | 3398 | if (DO_UTF8(left)) { |
2bc69dc4 | 3399 | /* If Unicode, try to downgrade. |
f2791508 JH |
3400 | * If not possible, croak. |
3401 | * Yes, we made this up. */ | |
1b6737cc | 3402 | SV* const tsv = sv_2mortal(newSVsv(left)); |
2bc69dc4 | 3403 | |
f2791508 | 3404 | SvUTF8_on(tsv); |
2bc69dc4 | 3405 | sv_utf8_downgrade(tsv, FALSE); |
349d4f2f | 3406 | tmps = SvPV_const(tsv, len); |
85c16d83 | 3407 | } |
05404ffe JH |
3408 | # ifdef USE_ITHREADS |
3409 | # ifdef HAS_CRYPT_R | |
3410 | if (!PL_reentrant_buffer->_crypt_struct_buffer) { | |
3411 | /* This should be threadsafe because in ithreads there is only | |
3412 | * one thread per interpreter. If this would not be true, | |
3413 | * we would need a mutex to protect this malloc. */ | |
3414 | PL_reentrant_buffer->_crypt_struct_buffer = | |
3415 | (struct crypt_data *)safemalloc(sizeof(struct crypt_data)); | |
3416 | #if defined(__GLIBC__) || defined(__EMX__) | |
3417 | if (PL_reentrant_buffer->_crypt_struct_buffer) { | |
3418 | PL_reentrant_buffer->_crypt_struct_buffer->initialized = 0; | |
3419 | /* work around glibc-2.2.5 bug */ | |
3420 | PL_reentrant_buffer->_crypt_struct_buffer->current_saltbits = 0; | |
3421 | } | |
05404ffe | 3422 | #endif |
6ab58e4d | 3423 | } |
05404ffe JH |
3424 | # endif /* HAS_CRYPT_R */ |
3425 | # endif /* USE_ITHREADS */ | |
5f74f29c | 3426 | # ifdef FCRYPT |
83003860 | 3427 | sv_setpv(TARG, fcrypt(tmps, SvPV_nolen_const(right))); |
5f74f29c | 3428 | # else |
83003860 | 3429 | sv_setpv(TARG, PerlProc_crypt(tmps, SvPV_nolen_const(right))); |
5f74f29c | 3430 | # endif |
ec93b65f | 3431 | SETTARG; |
4808266b | 3432 | RETURN; |
79072805 | 3433 | #else |
b13b2135 | 3434 | DIE(aTHX_ |
79072805 LW |
3435 | "The crypt() function is unimplemented due to excessive paranoia."); |
3436 | #endif | |
79072805 LW |
3437 | } |
3438 | ||
00f254e2 KW |
3439 | /* Generally UTF-8 and UTF-EBCDIC are indistinguishable at this level. So |
3440 | * most comments below say UTF-8, when in fact they mean UTF-EBCDIC as well */ | |
3441 | ||
79072805 LW |
3442 | PP(pp_ucfirst) |
3443 | { | |
00f254e2 KW |
3444 | /* Actually is both lcfirst() and ucfirst(). Only the first character |
3445 | * changes. This means that possibly we can change in-place, ie., just | |
3446 | * take the source and change that one character and store it back, but not | |
3447 | * if read-only etc, or if the length changes */ | |
3448 | ||
97aff369 | 3449 | dVAR; |
39644a26 | 3450 | dSP; |
d54190f6 | 3451 | SV *source = TOPs; |
00f254e2 | 3452 | STRLEN slen; /* slen is the byte length of the whole SV. */ |
d54190f6 NC |
3453 | STRLEN need; |
3454 | SV *dest; | |
00f254e2 KW |
3455 | bool inplace; /* ? Convert first char only, in-place */ |
3456 | bool doing_utf8 = FALSE; /* ? using utf8 */ | |
3457 | bool convert_source_to_utf8 = FALSE; /* ? need to convert */ | |
12e9c124 | 3458 | const int op_type = PL_op->op_type; |
d54190f6 NC |
3459 | const U8 *s; |
3460 | U8 *d; | |
3461 | U8 tmpbuf[UTF8_MAXBYTES_CASE+1]; | |
00f254e2 KW |
3462 | STRLEN ulen; /* ulen is the byte length of the original Unicode character |
3463 | * stored as UTF-8 at s. */ | |
3464 | STRLEN tculen; /* tculen is the byte length of the freshly titlecased (or | |
3465 | * lowercased) character stored in tmpbuf. May be either | |
3466 | * UTF-8 or not, but in either case is the number of bytes */ | |
094a2f8c | 3467 | bool tainted = FALSE; |
d54190f6 NC |
3468 | |
3469 | SvGETMAGIC(source); | |
3470 | if (SvOK(source)) { | |
3471 | s = (const U8*)SvPV_nomg_const(source, slen); | |
3472 | } else { | |
0a0ffbce RGS |
3473 | if (ckWARN(WARN_UNINITIALIZED)) |
3474 | report_uninit(source); | |
1eced8f8 | 3475 | s = (const U8*)""; |
d54190f6 NC |
3476 | slen = 0; |
3477 | } | |
a0ed51b3 | 3478 | |
00f254e2 KW |
3479 | /* We may be able to get away with changing only the first character, in |
3480 | * place, but not if read-only, etc. Later we may discover more reasons to | |
3481 | * not convert in-place. */ | |
3482 | inplace = SvPADTMP(source) && !SvREADONLY(source) && SvTEMP(source); | |
3483 | ||
3484 | /* First calculate what the changed first character should be. This affects | |
3485 | * whether we can just swap it out, leaving the rest of the string unchanged, | |
3486 | * or even if have to convert the dest to UTF-8 when the source isn't */ | |
3487 | ||
3488 | if (! slen) { /* If empty */ | |
3489 | need = 1; /* still need a trailing NUL */ | |
b7576bcb | 3490 | ulen = 0; |
00f254e2 KW |
3491 | } |
3492 | else if (DO_UTF8(source)) { /* Is the source utf8? */ | |
d54190f6 | 3493 | doing_utf8 = TRUE; |
17e95c9d | 3494 | ulen = UTF8SKIP(s); |
094a2f8c KW |
3495 | if (op_type == OP_UCFIRST) { |
3496 | _to_utf8_title_flags(s, tmpbuf, &tculen, | |
3497 | cBOOL(IN_LOCALE_RUNTIME), &tainted); | |
3498 | } | |
3499 | else { | |
3500 | _to_utf8_lower_flags(s, tmpbuf, &tculen, | |
3501 | cBOOL(IN_LOCALE_RUNTIME), &tainted); | |
3502 | } | |
00f254e2 | 3503 | |
17e95c9d KW |
3504 | /* we can't do in-place if the length changes. */ |
3505 | if (ulen != tculen) inplace = FALSE; | |
3506 | need = slen + 1 - ulen + tculen; | |
d54190f6 | 3507 | } |
00f254e2 KW |
3508 | else { /* Non-zero length, non-UTF-8, Need to consider locale and if |
3509 | * latin1 is treated as caseless. Note that a locale takes | |
3510 | * precedence */ | |
167d19f2 | 3511 | ulen = 1; /* Original character is 1 byte */ |
00f254e2 KW |
3512 | tculen = 1; /* Most characters will require one byte, but this will |
3513 | * need to be overridden for the tricky ones */ | |
3514 | need = slen + 1; | |
3515 | ||
3516 | if (op_type == OP_LCFIRST) { | |
d54190f6 | 3517 | |
00f254e2 KW |
3518 | /* lower case the first letter: no trickiness for any character */ |
3519 | *tmpbuf = (IN_LOCALE_RUNTIME) ? toLOWER_LC(*s) : | |
3520 | ((IN_UNI_8_BIT) ? toLOWER_LATIN1(*s) : toLOWER(*s)); | |
3521 | } | |
3522 | /* is ucfirst() */ | |
3523 | else if (IN_LOCALE_RUNTIME) { | |
3524 | *tmpbuf = toUPPER_LC(*s); /* This would be a bug if any locales | |
3525 | * have upper and title case different | |
3526 | */ | |
3527 | } | |
3528 | else if (! IN_UNI_8_BIT) { | |
3529 | *tmpbuf = toUPPER(*s); /* Returns caseless for non-ascii, or | |
3530 | * on EBCDIC machines whatever the | |
3531 | * native function does */ | |
3532 | } | |
3533 | else { /* is ucfirst non-UTF-8, not in locale, and cased latin1 */ | |
167d19f2 KW |
3534 | UV title_ord = _to_upper_title_latin1(*s, tmpbuf, &tculen, 's'); |
3535 | if (tculen > 1) { | |
3536 | assert(tculen == 2); | |
3537 | ||
3538 | /* If the result is an upper Latin1-range character, it can | |
3539 | * still be represented in one byte, which is its ordinal */ | |
3540 | if (UTF8_IS_DOWNGRADEABLE_START(*tmpbuf)) { | |
3541 | *tmpbuf = (U8) title_ord; | |
3542 | tculen = 1; | |
00f254e2 KW |
3543 | } |
3544 | else { | |
167d19f2 KW |
3545 | /* Otherwise it became more than one ASCII character (in |
3546 | * the case of LATIN_SMALL_LETTER_SHARP_S) or changed to | |
3547 | * beyond Latin1, so the number of bytes changed, so can't | |
3548 | * replace just the first character in place. */ | |
3549 | inplace = FALSE; | |
3550 | ||
d14578b8 KW |
3551 | /* If the result won't fit in a byte, the entire result |
3552 | * will have to be in UTF-8. Assume worst case sizing in | |
3553 | * conversion. (all latin1 characters occupy at most two | |
3554 | * bytes in utf8) */ | |
167d19f2 KW |
3555 | if (title_ord > 255) { |
3556 | doing_utf8 = TRUE; | |
3557 | convert_source_to_utf8 = TRUE; | |
3558 | need = slen * 2 + 1; | |
3559 | ||
3560 | /* The (converted) UTF-8 and UTF-EBCDIC lengths of all | |
3561 | * (both) characters whose title case is above 255 is | |
3562 | * 2. */ | |
3563 | ulen = 2; | |
3564 | } | |
3565 | else { /* LATIN_SMALL_LETTER_SHARP_S expands by 1 byte */ | |
3566 | need = slen + 1 + 1; | |
3567 | } | |
00f254e2 | 3568 | } |
167d19f2 | 3569 | } |
00f254e2 KW |
3570 | } /* End of use Unicode (Latin1) semantics */ |
3571 | } /* End of changing the case of the first character */ | |
3572 | ||
3573 | /* Here, have the first character's changed case stored in tmpbuf. Ready to | |
3574 | * generate the result */ | |
3575 | if (inplace) { | |
3576 | ||
3577 | /* We can convert in place. This means we change just the first | |
3578 | * character without disturbing the rest; no need to grow */ | |
d54190f6 NC |
3579 | dest = source; |
3580 | s = d = (U8*)SvPV_force_nomg(source, slen); | |
3581 | } else { | |
3582 | dTARGET; | |
3583 | ||
3584 | dest = TARG; | |
3585 | ||
00f254e2 KW |
3586 | /* Here, we can't convert in place; we earlier calculated how much |
3587 | * space we will need, so grow to accommodate that */ | |
d54190f6 | 3588 | SvUPGRADE(dest, SVt_PV); |
3b416f41 | 3589 | d = (U8*)SvGROW(dest, need); |
d54190f6 NC |
3590 | (void)SvPOK_only(dest); |
3591 | ||
3592 | SETs(dest); | |
d54190f6 | 3593 | } |
44bc797b | 3594 | |
d54190f6 | 3595 | if (doing_utf8) { |
00f254e2 KW |
3596 | if (! inplace) { |
3597 | if (! convert_source_to_utf8) { | |
3598 | ||
3599 | /* Here both source and dest are in UTF-8, but have to create | |
3600 | * the entire output. We initialize the result to be the | |
3601 | * title/lower cased first character, and then append the rest | |
3602 | * of the string. */ | |
3603 | sv_setpvn(dest, (char*)tmpbuf, tculen); | |
3604 | if (slen > ulen) { | |
3605 | sv_catpvn(dest, (char*)(s + ulen), slen - ulen); | |
3606 | } | |
3607 | } | |
3608 | else { | |
3609 | const U8 *const send = s + slen; | |
3610 | ||
3611 | /* Here the dest needs to be in UTF-8, but the source isn't, | |
3612 | * except we earlier UTF-8'd the first character of the source | |
3613 | * into tmpbuf. First put that into dest, and then append the | |
3614 | * rest of the source, converting it to UTF-8 as we go. */ | |
3615 | ||
3616 | /* Assert tculen is 2 here because the only two characters that | |
3617 | * get to this part of the code have 2-byte UTF-8 equivalents */ | |
3618 | *d++ = *tmpbuf; | |
3619 | *d++ = *(tmpbuf + 1); | |
3620 | s++; /* We have just processed the 1st char */ | |
3621 | ||
3622 | for (; s < send; s++) { | |
3623 | d = uvchr_to_utf8(d, *s); | |
3624 | } | |
3625 | *d = '\0'; | |
3626 | SvCUR_set(dest, d - (U8*)SvPVX_const(dest)); | |
3627 | } | |
d54190f6 | 3628 | SvUTF8_on(dest); |
a0ed51b3 | 3629 | } |
00f254e2 | 3630 | else { /* in-place UTF-8. Just overwrite the first character */ |
d54190f6 NC |
3631 | Copy(tmpbuf, d, tculen, U8); |
3632 | SvCUR_set(dest, need - 1); | |
a0ed51b3 | 3633 | } |
094a2f8c KW |
3634 | |
3635 | if (tainted) { | |
3636 | TAINT; | |
3637 | SvTAINTED_on(dest); | |
3638 | } | |
a0ed51b3 | 3639 | } |
00f254e2 KW |
3640 | else { /* Neither source nor dest are in or need to be UTF-8 */ |
3641 | if (slen) { | |
2de3dbcc | 3642 | if (IN_LOCALE_RUNTIME) { |
31351b04 | 3643 |