Commit | Line | Data |
---|---|---|
a0d0e21e | 1 | /* pp.c |
79072805 | 2 | * |
4bb101f2 | 3 | * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, |
770526c1 | 4 | * 2000, 2001, 2002, 2003, 2004, 2005, 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 | /* | |
12 | * "It's a big house this, and very peculiar. Always a bit more to discover, | |
13 | * and no knowing what you'll find around a corner. And Elves, sir!" --Samwise | |
14 | */ | |
79072805 | 15 | |
166f8a29 DM |
16 | /* This file contains general pp ("push/pop") functions that execute the |
17 | * opcodes that make up a perl program. A typical pp function expects to | |
18 | * find its arguments on the stack, and usually pushes its results onto | |
19 | * the stack, hence the 'pp' terminology. Each OP structure contains | |
20 | * a pointer to the relevant pp_foo() function. | |
21 | */ | |
22 | ||
79072805 | 23 | #include "EXTERN.h" |
864dbfa3 | 24 | #define PERL_IN_PP_C |
79072805 | 25 | #include "perl.h" |
77bc9082 | 26 | #include "keywords.h" |
79072805 | 27 | |
a4af207c JH |
28 | #include "reentr.h" |
29 | ||
dfe9444c AD |
30 | /* XXX I can't imagine anyone who doesn't have this actually _needs_ |
31 | it, since pid_t is an integral type. | |
32 | --AD 2/20/1998 | |
33 | */ | |
34 | #ifdef NEED_GETPID_PROTO | |
35 | extern Pid_t getpid (void); | |
8ac85365 NIS |
36 | #endif |
37 | ||
0630166f SP |
38 | /* |
39 | * Some BSDs and Cygwin default to POSIX math instead of IEEE. | |
40 | * This switches them over to IEEE. | |
41 | */ | |
42 | #if defined(LIBM_LIB_VERSION) | |
43 | _LIB_VERSION_TYPE _LIB_VERSION = _IEEE_; | |
44 | #endif | |
45 | ||
13017935 SM |
46 | /* variations on pp_null */ |
47 | ||
93a17b20 LW |
48 | PP(pp_stub) |
49 | { | |
39644a26 | 50 | dSP; |
54310121 | 51 | if (GIMME_V == G_SCALAR) |
3280af22 | 52 | XPUSHs(&PL_sv_undef); |
93a17b20 LW |
53 | RETURN; |
54 | } | |
55 | ||
79072805 LW |
56 | /* Pushy stuff. */ |
57 | ||
93a17b20 LW |
58 | PP(pp_padav) |
59 | { | |
39644a26 | 60 | dSP; dTARGET; |
13017935 | 61 | I32 gimme; |
533c011a | 62 | if (PL_op->op_private & OPpLVAL_INTRO) |
dd2155a4 | 63 | SAVECLEARSV(PAD_SVl(PL_op->op_targ)); |
85e6fe83 | 64 | EXTEND(SP, 1); |
533c011a | 65 | if (PL_op->op_flags & OPf_REF) { |
85e6fe83 | 66 | PUSHs(TARG); |
93a17b20 | 67 | RETURN; |
78f9721b SM |
68 | } else if (LVRET) { |
69 | if (GIMME == G_SCALAR) | |
70 | Perl_croak(aTHX_ "Can't return array to lvalue scalar context"); | |
71 | PUSHs(TARG); | |
72 | RETURN; | |
85e6fe83 | 73 | } |
13017935 SM |
74 | gimme = GIMME_V; |
75 | if (gimme == G_ARRAY) { | |
f54cb97a | 76 | const I32 maxarg = AvFILL((AV*)TARG) + 1; |
85e6fe83 | 77 | EXTEND(SP, maxarg); |
93965878 NIS |
78 | if (SvMAGICAL(TARG)) { |
79 | U32 i; | |
eb160463 | 80 | for (i=0; i < (U32)maxarg; i++) { |
0bd48802 | 81 | SV * const * const svp = av_fetch((AV*)TARG, i, FALSE); |
3280af22 | 82 | SP[i+1] = (svp) ? *svp : &PL_sv_undef; |
93965878 NIS |
83 | } |
84 | } | |
85 | else { | |
86 | Copy(AvARRAY((AV*)TARG), SP+1, maxarg, SV*); | |
87 | } | |
85e6fe83 LW |
88 | SP += maxarg; |
89 | } | |
13017935 | 90 | else if (gimme == G_SCALAR) { |
1b6737cc | 91 | SV* const sv = sv_newmortal(); |
f54cb97a | 92 | const I32 maxarg = AvFILL((AV*)TARG) + 1; |
85e6fe83 LW |
93 | sv_setiv(sv, maxarg); |
94 | PUSHs(sv); | |
95 | } | |
96 | RETURN; | |
93a17b20 LW |
97 | } |
98 | ||
99 | PP(pp_padhv) | |
100 | { | |
39644a26 | 101 | dSP; dTARGET; |
54310121 | 102 | I32 gimme; |
103 | ||
93a17b20 | 104 | XPUSHs(TARG); |
533c011a | 105 | if (PL_op->op_private & OPpLVAL_INTRO) |
dd2155a4 | 106 | SAVECLEARSV(PAD_SVl(PL_op->op_targ)); |
533c011a | 107 | if (PL_op->op_flags & OPf_REF) |
93a17b20 | 108 | RETURN; |
78f9721b SM |
109 | else if (LVRET) { |
110 | if (GIMME == G_SCALAR) | |
111 | Perl_croak(aTHX_ "Can't return hash to lvalue scalar context"); | |
112 | RETURN; | |
113 | } | |
54310121 | 114 | gimme = GIMME_V; |
115 | if (gimme == G_ARRAY) { | |
cea2e8a9 | 116 | RETURNOP(do_kv()); |
85e6fe83 | 117 | } |
54310121 | 118 | else if (gimme == G_SCALAR) { |
1b6737cc | 119 | SV* const sv = Perl_hv_scalar(aTHX_ (HV*)TARG); |
85e6fe83 | 120 | SETs(sv); |
85e6fe83 | 121 | } |
54310121 | 122 | RETURN; |
93a17b20 LW |
123 | } |
124 | ||
79072805 LW |
125 | /* Translations. */ |
126 | ||
127 | PP(pp_rv2gv) | |
128 | { | |
39644a26 | 129 | dSP; dTOPss; |
8ec5e241 | 130 | |
ed6116ce | 131 | if (SvROK(sv)) { |
a0d0e21e | 132 | wasref: |
f5284f61 IZ |
133 | tryAMAGICunDEREF(to_gv); |
134 | ||
ed6116ce | 135 | sv = SvRV(sv); |
b1dadf13 | 136 | if (SvTYPE(sv) == SVt_PVIO) { |
1b6737cc | 137 | GV * const gv = (GV*) sv_newmortal(); |
b1dadf13 | 138 | gv_init(gv, 0, "", 0, 0); |
139 | GvIOp(gv) = (IO *)sv; | |
3e3baf6d | 140 | (void)SvREFCNT_inc(sv); |
b1dadf13 | 141 | sv = (SV*) gv; |
ef54e1a4 JH |
142 | } |
143 | else if (SvTYPE(sv) != SVt_PVGV) | |
cea2e8a9 | 144 | DIE(aTHX_ "Not a GLOB reference"); |
79072805 LW |
145 | } |
146 | else { | |
93a17b20 | 147 | if (SvTYPE(sv) != SVt_PVGV) { |
a0d0e21e LW |
148 | if (SvGMAGICAL(sv)) { |
149 | mg_get(sv); | |
150 | if (SvROK(sv)) | |
151 | goto wasref; | |
152 | } | |
afd1915d | 153 | if (!SvOK(sv) && sv != &PL_sv_undef) { |
b13b2135 | 154 | /* If this is a 'my' scalar and flag is set then vivify |
853846ea | 155 | * NI-S 1999/05/07 |
b13b2135 | 156 | */ |
ac53db4c DM |
157 | if (SvREADONLY(sv)) |
158 | Perl_croak(aTHX_ PL_no_modify); | |
1d8d4d2a | 159 | if (PL_op->op_private & OPpDEREF) { |
2c8ac474 GS |
160 | GV *gv; |
161 | if (cUNOP->op_targ) { | |
162 | STRLEN len; | |
0bd48802 AL |
163 | SV * const namesv = PAD_SV(cUNOP->op_targ); |
164 | const char * const name = SvPV(namesv, len); | |
2d6d9f7a | 165 | gv = (GV*)NEWSV(0,0); |
2c8ac474 GS |
166 | gv_init(gv, CopSTASH(PL_curcop), name, len, 0); |
167 | } | |
168 | else { | |
0bd48802 | 169 | const char * const name = CopSTASHPV(PL_curcop); |
2c8ac474 | 170 | gv = newGVgen(name); |
1d8d4d2a | 171 | } |
b13b2135 NIS |
172 | if (SvTYPE(sv) < SVt_RV) |
173 | sv_upgrade(sv, SVt_RV); | |
b15aece3 | 174 | if (SvPVX_const(sv)) { |
8bd4d4c5 | 175 | SvPV_free(sv); |
b162af07 SP |
176 | SvLEN_set(sv, 0); |
177 | SvCUR_set(sv, 0); | |
8f3c2c0c | 178 | } |
b162af07 | 179 | SvRV_set(sv, (SV*)gv); |
853846ea | 180 | SvROK_on(sv); |
1d8d4d2a | 181 | SvSETMAGIC(sv); |
853846ea | 182 | goto wasref; |
2c8ac474 | 183 | } |
533c011a NIS |
184 | if (PL_op->op_flags & OPf_REF || |
185 | PL_op->op_private & HINT_STRICT_REFS) | |
cea2e8a9 | 186 | DIE(aTHX_ PL_no_usym, "a symbol"); |
599cee73 | 187 | if (ckWARN(WARN_UNINITIALIZED)) |
29489e7c | 188 | report_uninit(sv); |
a0d0e21e LW |
189 | RETSETUNDEF; |
190 | } | |
35cd451c GS |
191 | if ((PL_op->op_flags & OPf_SPECIAL) && |
192 | !(PL_op->op_flags & OPf_MOD)) | |
193 | { | |
f776e3cd | 194 | SV * const temp = (SV*)gv_fetchsv(sv, 0, SVt_PVGV); |
7a5fd60d NC |
195 | if (!temp |
196 | && (!is_gv_magical_sv(sv,0) | |
f776e3cd | 197 | || !(sv = (SV*)gv_fetchsv(sv, GV_ADD, SVt_PVGV)))) { |
35cd451c | 198 | RETSETUNDEF; |
c9d5ac95 | 199 | } |
7a5fd60d | 200 | sv = temp; |
35cd451c GS |
201 | } |
202 | else { | |
203 | if (PL_op->op_private & HINT_STRICT_REFS) | |
7a5fd60d | 204 | DIE(aTHX_ PL_no_symref_sv, sv, "a symbol"); |
e26df76a NC |
205 | if ((PL_op->op_private & (OPpLVAL_INTRO|OPpDONT_INIT_GV)) |
206 | == OPpDONT_INIT_GV) { | |
207 | /* We are the target of a coderef assignment. Return | |
208 | the scalar unchanged, and let pp_sasssign deal with | |
209 | things. */ | |
210 | RETURN; | |
211 | } | |
f776e3cd | 212 | sv = (SV*)gv_fetchsv(sv, GV_ADD, SVt_PVGV); |
35cd451c | 213 | } |
93a17b20 | 214 | } |
79072805 | 215 | } |
533c011a NIS |
216 | if (PL_op->op_private & OPpLVAL_INTRO) |
217 | save_gp((GV*)sv, !(PL_op->op_flags & OPf_SPECIAL)); | |
79072805 LW |
218 | SETs(sv); |
219 | RETURN; | |
220 | } | |
221 | ||
79072805 LW |
222 | PP(pp_rv2sv) |
223 | { | |
82d03984 | 224 | GV *gv = Nullgv; |
39644a26 | 225 | dSP; dTOPss; |
79072805 | 226 | |
ed6116ce | 227 | if (SvROK(sv)) { |
a0d0e21e | 228 | wasref: |
f5284f61 IZ |
229 | tryAMAGICunDEREF(to_sv); |
230 | ||
ed6116ce | 231 | sv = SvRV(sv); |
79072805 LW |
232 | switch (SvTYPE(sv)) { |
233 | case SVt_PVAV: | |
234 | case SVt_PVHV: | |
235 | case SVt_PVCV: | |
cea2e8a9 | 236 | DIE(aTHX_ "Not a SCALAR reference"); |
79072805 LW |
237 | } |
238 | } | |
239 | else { | |
82d03984 | 240 | gv = (GV*)sv; |
748a9306 | 241 | |
463ee0b2 | 242 | if (SvTYPE(gv) != SVt_PVGV) { |
a0d0e21e LW |
243 | if (SvGMAGICAL(sv)) { |
244 | mg_get(sv); | |
245 | if (SvROK(sv)) | |
246 | goto wasref; | |
247 | } | |
2e6a7e23 RGS |
248 | if (PL_op->op_private & HINT_STRICT_REFS) { |
249 | if (SvOK(sv)) | |
250 | DIE(aTHX_ PL_no_symref_sv, sv, "a SCALAR"); | |
251 | else | |
252 | DIE(aTHX_ PL_no_usym, "a SCALAR"); | |
253 | } | |
a0d0e21e | 254 | if (!SvOK(sv)) { |
2e6a7e23 | 255 | if (PL_op->op_flags & OPf_REF) |
cea2e8a9 | 256 | DIE(aTHX_ PL_no_usym, "a SCALAR"); |
599cee73 | 257 | if (ckWARN(WARN_UNINITIALIZED)) |
29489e7c | 258 | report_uninit(sv); |
a0d0e21e LW |
259 | RETSETUNDEF; |
260 | } | |
35cd451c GS |
261 | if ((PL_op->op_flags & OPf_SPECIAL) && |
262 | !(PL_op->op_flags & OPf_MOD)) | |
263 | { | |
f776e3cd | 264 | gv = (GV*)gv_fetchsv(sv, 0, SVt_PV); |
c9d5ac95 | 265 | if (!gv |
7a5fd60d | 266 | && (!is_gv_magical_sv(sv, 0) |
f776e3cd | 267 | || !(gv = (GV*)gv_fetchsv(sv, GV_ADD, SVt_PV)))) |
c9d5ac95 | 268 | { |
35cd451c | 269 | RETSETUNDEF; |
c9d5ac95 | 270 | } |
35cd451c GS |
271 | } |
272 | else { | |
f776e3cd | 273 | gv = (GV*)gv_fetchsv(sv, GV_ADD, SVt_PV); |
35cd451c | 274 | } |
463ee0b2 | 275 | } |
29c711a3 | 276 | sv = GvSVn(gv); |
a0d0e21e | 277 | } |
533c011a | 278 | if (PL_op->op_flags & OPf_MOD) { |
82d03984 RGS |
279 | if (PL_op->op_private & OPpLVAL_INTRO) { |
280 | if (cUNOP->op_first->op_type == OP_NULL) | |
281 | sv = save_scalar((GV*)TOPs); | |
282 | else if (gv) | |
283 | sv = save_scalar(gv); | |
284 | else | |
285 | Perl_croak(aTHX_ PL_no_localize_ref); | |
286 | } | |
533c011a NIS |
287 | else if (PL_op->op_private & OPpDEREF) |
288 | vivify_ref(sv, PL_op->op_private & OPpDEREF); | |
79072805 | 289 | } |
a0d0e21e | 290 | SETs(sv); |
79072805 LW |
291 | RETURN; |
292 | } | |
293 | ||
294 | PP(pp_av2arylen) | |
295 | { | |
39644a26 | 296 | dSP; |
1b6737cc AL |
297 | AV * const av = (AV*)TOPs; |
298 | SV ** const sv = Perl_av_arylen_p(aTHX_ (AV*)av); | |
a3874608 NC |
299 | if (!*sv) { |
300 | *sv = NEWSV(0,0); | |
301 | sv_upgrade(*sv, SVt_PVMG); | |
302 | sv_magic(*sv, (SV*)av, PERL_MAGIC_arylen, Nullch, 0); | |
79072805 | 303 | } |
a3874608 | 304 | SETs(*sv); |
79072805 LW |
305 | RETURN; |
306 | } | |
307 | ||
a0d0e21e LW |
308 | PP(pp_pos) |
309 | { | |
39644a26 | 310 | dSP; dTARGET; dPOPss; |
8ec5e241 | 311 | |
78f9721b | 312 | if (PL_op->op_flags & OPf_MOD || LVRET) { |
5f05dabc | 313 | if (SvTYPE(TARG) < SVt_PVLV) { |
314 | sv_upgrade(TARG, SVt_PVLV); | |
14befaf4 | 315 | sv_magic(TARG, Nullsv, PERL_MAGIC_pos, Nullch, 0); |
5f05dabc | 316 | } |
317 | ||
318 | LvTYPE(TARG) = '.'; | |
6ff81951 GS |
319 | if (LvTARG(TARG) != sv) { |
320 | if (LvTARG(TARG)) | |
321 | SvREFCNT_dec(LvTARG(TARG)); | |
322 | LvTARG(TARG) = SvREFCNT_inc(sv); | |
323 | } | |
a0d0e21e LW |
324 | PUSHs(TARG); /* no SvSETMAGIC */ |
325 | RETURN; | |
326 | } | |
327 | else { | |
a0d0e21e | 328 | if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) { |
1b6737cc | 329 | const MAGIC * const mg = mg_find(sv, PERL_MAGIC_regex_global); |
565764a8 | 330 | if (mg && mg->mg_len >= 0) { |
a0ed51b3 | 331 | I32 i = mg->mg_len; |
7e2040f0 | 332 | if (DO_UTF8(sv)) |
a0ed51b3 LW |
333 | sv_pos_b2u(sv, &i); |
334 | PUSHi(i + PL_curcop->cop_arybase); | |
a0d0e21e LW |
335 | RETURN; |
336 | } | |
337 | } | |
338 | RETPUSHUNDEF; | |
339 | } | |
340 | } | |
341 | ||
79072805 LW |
342 | PP(pp_rv2cv) |
343 | { | |
39644a26 | 344 | dSP; |
79072805 LW |
345 | GV *gv; |
346 | HV *stash; | |
e26df76a NC |
347 | I32 flags = (PL_op->op_flags & OPf_SPECIAL) ? 0 |
348 | : ((PL_op->op_private & (OPpLVAL_INTRO|OPpMAY_RETURN_CONSTANT)) | |
349 | == OPpMAY_RETURN_CONSTANT) ? GV_ADD|GV_NOEXPAND : GV_ADD; | |
4633a7c4 LW |
350 | /* We usually try to add a non-existent subroutine in case of AUTOLOAD. */ |
351 | /* (But not in defined().) */ | |
e26df76a NC |
352 | |
353 | CV *cv = sv_2cv(TOPs, &stash, &gv, flags); | |
07055b4c CS |
354 | if (cv) { |
355 | if (CvCLONE(cv)) | |
356 | cv = (CV*)sv_2mortal((SV*)cv_clone(cv)); | |
d32f2495 SC |
357 | if ((PL_op->op_private & OPpLVAL_INTRO)) { |
358 | if (gv && GvCV(gv) == cv && (gv = gv_autoload4(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv), FALSE))) | |
359 | cv = GvCV(gv); | |
360 | if (!CvLVALUE(cv)) | |
361 | DIE(aTHX_ "Can't modify non-lvalue subroutine call"); | |
362 | } | |
07055b4c | 363 | } |
e26df76a NC |
364 | else if ((flags == (GV_ADD|GV_NOEXPAND)) && gv && SvROK(gv)) { |
365 | cv = (CV*)gv; | |
366 | } | |
07055b4c | 367 | else |
3280af22 | 368 | cv = (CV*)&PL_sv_undef; |
79072805 LW |
369 | SETs((SV*)cv); |
370 | RETURN; | |
371 | } | |
372 | ||
c07a80fd | 373 | PP(pp_prototype) |
374 | { | |
39644a26 | 375 | dSP; |
c07a80fd | 376 | CV *cv; |
377 | HV *stash; | |
378 | GV *gv; | |
379 | SV *ret; | |
380 | ||
3280af22 | 381 | ret = &PL_sv_undef; |
b6c543e3 | 382 | if (SvPOK(TOPs) && SvCUR(TOPs) >= 7) { |
0bd48802 | 383 | const char * const s = SvPVX_const(TOPs); |
b6c543e3 | 384 | if (strnEQ(s, "CORE::", 6)) { |
f54cb97a | 385 | const int code = keyword(s + 6, SvCUR(TOPs) - 6); |
b6c543e3 IZ |
386 | if (code < 0) { /* Overridable. */ |
387 | #define MAX_ARGS_OP ((sizeof(I32) - 1) * 2) | |
388 | int i = 0, n = 0, seen_question = 0; | |
389 | I32 oa; | |
390 | char str[ MAX_ARGS_OP * 2 + 2 ]; /* One ';', one '\0' */ | |
391 | ||
bdf1bb36 RGS |
392 | if (code == -KEY_chop || code == -KEY_chomp |
393 | || code == -KEY_exec || code == -KEY_system) | |
77bc9082 | 394 | goto set; |
b6c543e3 | 395 | while (i < MAXO) { /* The slow way. */ |
22c35a8c GS |
396 | if (strEQ(s + 6, PL_op_name[i]) |
397 | || strEQ(s + 6, PL_op_desc[i])) | |
398 | { | |
b6c543e3 | 399 | goto found; |
22c35a8c | 400 | } |
b6c543e3 IZ |
401 | i++; |
402 | } | |
403 | goto nonesuch; /* Should not happen... */ | |
404 | found: | |
22c35a8c | 405 | oa = PL_opargs[i] >> OASHIFT; |
b6c543e3 | 406 | while (oa) { |
3012a639 | 407 | if (oa & OA_OPTIONAL && !seen_question) { |
b6c543e3 IZ |
408 | seen_question = 1; |
409 | str[n++] = ';'; | |
ef54e1a4 | 410 | } |
b13b2135 | 411 | if ((oa & (OA_OPTIONAL - 1)) >= OA_AVREF |
6e97e420 SC |
412 | && (oa & (OA_OPTIONAL - 1)) <= OA_SCALARREF |
413 | /* But globs are already references (kinda) */ | |
414 | && (oa & (OA_OPTIONAL - 1)) != OA_FILEREF | |
415 | ) { | |
b6c543e3 IZ |
416 | str[n++] = '\\'; |
417 | } | |
b6c543e3 IZ |
418 | str[n++] = ("?$@@%&*$")[oa & (OA_OPTIONAL - 1)]; |
419 | oa = oa >> 4; | |
420 | } | |
421 | str[n++] = '\0'; | |
79cb57f6 | 422 | ret = sv_2mortal(newSVpvn(str, n - 1)); |
ef54e1a4 JH |
423 | } |
424 | else if (code) /* Non-Overridable */ | |
b6c543e3 IZ |
425 | goto set; |
426 | else { /* None such */ | |
427 | nonesuch: | |
d470f89e | 428 | DIE(aTHX_ "Can't find an opnumber for \"%s\"", s+6); |
b6c543e3 IZ |
429 | } |
430 | } | |
431 | } | |
f2c0649b | 432 | cv = sv_2cv(TOPs, &stash, &gv, 0); |
5f05dabc | 433 | if (cv && SvPOK(cv)) |
b15aece3 | 434 | ret = sv_2mortal(newSVpvn(SvPVX_const(cv), SvCUR(cv))); |
b6c543e3 | 435 | set: |
c07a80fd | 436 | SETs(ret); |
437 | RETURN; | |
438 | } | |
439 | ||
a0d0e21e LW |
440 | PP(pp_anoncode) |
441 | { | |
39644a26 | 442 | dSP; |
dd2155a4 | 443 | CV* cv = (CV*)PAD_SV(PL_op->op_targ); |
a5f75d66 | 444 | if (CvCLONE(cv)) |
b355b4e0 | 445 | cv = (CV*)sv_2mortal((SV*)cv_clone(cv)); |
5f05dabc | 446 | EXTEND(SP,1); |
748a9306 | 447 | PUSHs((SV*)cv); |
a0d0e21e LW |
448 | RETURN; |
449 | } | |
450 | ||
451 | PP(pp_srefgen) | |
79072805 | 452 | { |
39644a26 | 453 | dSP; |
71be2cbc | 454 | *SP = refto(*SP); |
79072805 | 455 | RETURN; |
8ec5e241 | 456 | } |
a0d0e21e LW |
457 | |
458 | PP(pp_refgen) | |
459 | { | |
39644a26 | 460 | dSP; dMARK; |
a0d0e21e | 461 | if (GIMME != G_ARRAY) { |
5f0b1d4e GS |
462 | if (++MARK <= SP) |
463 | *MARK = *SP; | |
464 | else | |
3280af22 | 465 | *MARK = &PL_sv_undef; |
5f0b1d4e GS |
466 | *MARK = refto(*MARK); |
467 | SP = MARK; | |
468 | RETURN; | |
a0d0e21e | 469 | } |
bbce6d69 | 470 | EXTEND_MORTAL(SP - MARK); |
71be2cbc | 471 | while (++MARK <= SP) |
472 | *MARK = refto(*MARK); | |
a0d0e21e | 473 | RETURN; |
79072805 LW |
474 | } |
475 | ||
76e3520e | 476 | STATIC SV* |
cea2e8a9 | 477 | S_refto(pTHX_ SV *sv) |
71be2cbc | 478 | { |
479 | SV* rv; | |
480 | ||
481 | if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') { | |
482 | if (LvTARGLEN(sv)) | |
68dc0745 | 483 | vivify_defelem(sv); |
484 | if (!(sv = LvTARG(sv))) | |
3280af22 | 485 | sv = &PL_sv_undef; |
0dd88869 | 486 | else |
a6c40364 | 487 | (void)SvREFCNT_inc(sv); |
71be2cbc | 488 | } |
d8b46c1b GS |
489 | else if (SvTYPE(sv) == SVt_PVAV) { |
490 | if (!AvREAL((AV*)sv) && AvREIFY((AV*)sv)) | |
491 | av_reify((AV*)sv); | |
492 | SvTEMP_off(sv); | |
493 | (void)SvREFCNT_inc(sv); | |
494 | } | |
f2933f5f DM |
495 | else if (SvPADTMP(sv) && !IS_PADGV(sv)) |
496 | sv = newSVsv(sv); | |
71be2cbc | 497 | else { |
498 | SvTEMP_off(sv); | |
499 | (void)SvREFCNT_inc(sv); | |
500 | } | |
501 | rv = sv_newmortal(); | |
502 | sv_upgrade(rv, SVt_RV); | |
b162af07 | 503 | SvRV_set(rv, sv); |
71be2cbc | 504 | SvROK_on(rv); |
505 | return rv; | |
506 | } | |
507 | ||
79072805 LW |
508 | PP(pp_ref) |
509 | { | |
39644a26 | 510 | dSP; dTARGET; |
e1ec3a88 | 511 | const char *pv; |
1b6737cc | 512 | SV * const sv = POPs; |
f12c7020 | 513 | |
5b295bef RD |
514 | if (sv) |
515 | SvGETMAGIC(sv); | |
f12c7020 | 516 | |
a0d0e21e | 517 | if (!sv || !SvROK(sv)) |
4633a7c4 | 518 | RETPUSHNO; |
79072805 | 519 | |
1b6737cc | 520 | pv = sv_reftype(SvRV(sv),TRUE); |
463ee0b2 | 521 | PUSHp(pv, strlen(pv)); |
79072805 LW |
522 | RETURN; |
523 | } | |
524 | ||
525 | PP(pp_bless) | |
526 | { | |
39644a26 | 527 | dSP; |
463ee0b2 | 528 | HV *stash; |
79072805 | 529 | |
463ee0b2 | 530 | if (MAXARG == 1) |
11faa288 | 531 | stash = CopSTASH(PL_curcop); |
7b8d334a | 532 | else { |
1b6737cc | 533 | SV * const ssv = POPs; |
7b8d334a | 534 | STRLEN len; |
e1ec3a88 | 535 | const char *ptr; |
81689caa | 536 | |
016a42f3 | 537 | if (ssv && !SvGMAGICAL(ssv) && !SvAMAGIC(ssv) && SvROK(ssv)) |
81689caa | 538 | Perl_croak(aTHX_ "Attempt to bless into a reference"); |
5c144d81 | 539 | ptr = SvPV_const(ssv,len); |
041457d9 | 540 | if (len == 0 && ckWARN(WARN_MISC)) |
9014280d | 541 | Perl_warner(aTHX_ packWARN(WARN_MISC), |
599cee73 | 542 | "Explicit blessing to '' (assuming package main)"); |
7b8d334a GS |
543 | stash = gv_stashpvn(ptr, len, TRUE); |
544 | } | |
a0d0e21e | 545 | |
5d3fdfeb | 546 | (void)sv_bless(TOPs, stash); |
79072805 LW |
547 | RETURN; |
548 | } | |
549 | ||
fb73857a | 550 | PP(pp_gelem) |
551 | { | |
39644a26 | 552 | dSP; |
b13b2135 | 553 | |
1b6737cc AL |
554 | SV *sv = POPs; |
555 | const char * const elem = SvPV_nolen_const(sv); | |
556 | GV * const gv = (GV*)POPs; | |
557 | SV * tmpRef = Nullsv; | |
558 | ||
fb73857a | 559 | sv = Nullsv; |
c4ba80c3 NC |
560 | if (elem) { |
561 | /* elem will always be NUL terminated. */ | |
1b6737cc | 562 | const char * const second_letter = elem + 1; |
c4ba80c3 NC |
563 | switch (*elem) { |
564 | case 'A': | |
1b6737cc | 565 | if (strEQ(second_letter, "RRAY")) |
c4ba80c3 NC |
566 | tmpRef = (SV*)GvAV(gv); |
567 | break; | |
568 | case 'C': | |
1b6737cc | 569 | if (strEQ(second_letter, "ODE")) |
c4ba80c3 NC |
570 | tmpRef = (SV*)GvCVu(gv); |
571 | break; | |
572 | case 'F': | |
1b6737cc | 573 | if (strEQ(second_letter, "ILEHANDLE")) { |
c4ba80c3 NC |
574 | /* finally deprecated in 5.8.0 */ |
575 | deprecate("*glob{FILEHANDLE}"); | |
576 | tmpRef = (SV*)GvIOp(gv); | |
577 | } | |
578 | else | |
1b6737cc | 579 | if (strEQ(second_letter, "ORMAT")) |
c4ba80c3 NC |
580 | tmpRef = (SV*)GvFORM(gv); |
581 | break; | |
582 | case 'G': | |
1b6737cc | 583 | if (strEQ(second_letter, "LOB")) |
c4ba80c3 NC |
584 | tmpRef = (SV*)gv; |
585 | break; | |
586 | case 'H': | |
1b6737cc | 587 | if (strEQ(second_letter, "ASH")) |
c4ba80c3 NC |
588 | tmpRef = (SV*)GvHV(gv); |
589 | break; | |
590 | case 'I': | |
1b6737cc | 591 | if (*second_letter == 'O' && !elem[2]) |
c4ba80c3 NC |
592 | tmpRef = (SV*)GvIOp(gv); |
593 | break; | |
594 | case 'N': | |
1b6737cc | 595 | if (strEQ(second_letter, "AME")) |
c4ba80c3 NC |
596 | sv = newSVpvn(GvNAME(gv), GvNAMELEN(gv)); |
597 | break; | |
598 | case 'P': | |
1b6737cc | 599 | if (strEQ(second_letter, "ACKAGE")) { |
5aaec2b4 NC |
600 | const HEK *hek = HvNAME_HEK(GvSTASH(gv)); |
601 | sv = hek ? newSVhek(hek) : newSVpvn("__ANON__", 8); | |
c4ba80c3 NC |
602 | } |
603 | break; | |
604 | case 'S': | |
1b6737cc | 605 | if (strEQ(second_letter, "CALAR")) |
c4ba80c3 NC |
606 | tmpRef = GvSV(gv); |
607 | break; | |
39b99f21 | 608 | } |
fb73857a | 609 | } |
76e3520e GS |
610 | if (tmpRef) |
611 | sv = newRV(tmpRef); | |
fb73857a | 612 | if (sv) |
613 | sv_2mortal(sv); | |
614 | else | |
3280af22 | 615 | sv = &PL_sv_undef; |
fb73857a | 616 | XPUSHs(sv); |
617 | RETURN; | |
618 | } | |
619 | ||
a0d0e21e | 620 | /* Pattern matching */ |
79072805 | 621 | |
a0d0e21e | 622 | PP(pp_study) |
79072805 | 623 | { |
39644a26 | 624 | dSP; dPOPss; |
a0d0e21e LW |
625 | register unsigned char *s; |
626 | register I32 pos; | |
627 | register I32 ch; | |
628 | register I32 *sfirst; | |
629 | register I32 *snext; | |
a0d0e21e LW |
630 | STRLEN len; |
631 | ||
3280af22 | 632 | if (sv == PL_lastscream) { |
1e422769 | 633 | if (SvSCREAM(sv)) |
634 | RETPUSHYES; | |
635 | } | |
c07a80fd | 636 | else { |
3280af22 NIS |
637 | if (PL_lastscream) { |
638 | SvSCREAM_off(PL_lastscream); | |
639 | SvREFCNT_dec(PL_lastscream); | |
c07a80fd | 640 | } |
3280af22 | 641 | PL_lastscream = SvREFCNT_inc(sv); |
c07a80fd | 642 | } |
1e422769 | 643 | |
644 | s = (unsigned char*)(SvPV(sv, len)); | |
645 | pos = len; | |
646 | if (pos <= 0) | |
647 | RETPUSHNO; | |
3280af22 NIS |
648 | if (pos > PL_maxscream) { |
649 | if (PL_maxscream < 0) { | |
650 | PL_maxscream = pos + 80; | |
a02a5408 JC |
651 | Newx(PL_screamfirst, 256, I32); |
652 | Newx(PL_screamnext, PL_maxscream, I32); | |
79072805 LW |
653 | } |
654 | else { | |
3280af22 NIS |
655 | PL_maxscream = pos + pos / 4; |
656 | Renew(PL_screamnext, PL_maxscream, I32); | |
79072805 | 657 | } |
79072805 | 658 | } |
a0d0e21e | 659 | |
3280af22 NIS |
660 | sfirst = PL_screamfirst; |
661 | snext = PL_screamnext; | |
a0d0e21e LW |
662 | |
663 | if (!sfirst || !snext) | |
cea2e8a9 | 664 | DIE(aTHX_ "do_study: out of memory"); |
a0d0e21e LW |
665 | |
666 | for (ch = 256; ch; --ch) | |
667 | *sfirst++ = -1; | |
668 | sfirst -= 256; | |
669 | ||
670 | while (--pos >= 0) { | |
1b6737cc | 671 | register const I32 ch = s[pos]; |
a0d0e21e LW |
672 | if (sfirst[ch] >= 0) |
673 | snext[pos] = sfirst[ch] - pos; | |
674 | else | |
675 | snext[pos] = -pos; | |
676 | sfirst[ch] = pos; | |
79072805 LW |
677 | } |
678 | ||
c07a80fd | 679 | SvSCREAM_on(sv); |
14befaf4 DM |
680 | /* piggyback on m//g magic */ |
681 | sv_magic(sv, Nullsv, PERL_MAGIC_regex_global, Nullch, 0); | |
1e422769 | 682 | RETPUSHYES; |
79072805 LW |
683 | } |
684 | ||
a0d0e21e | 685 | PP(pp_trans) |
79072805 | 686 | { |
39644a26 | 687 | dSP; dTARG; |
a0d0e21e LW |
688 | SV *sv; |
689 | ||
533c011a | 690 | if (PL_op->op_flags & OPf_STACKED) |
a0d0e21e | 691 | sv = POPs; |
59f00321 RGS |
692 | else if (PL_op->op_private & OPpTARGET_MY) |
693 | sv = GETTARGET; | |
79072805 | 694 | else { |
54b9620d | 695 | sv = DEFSV; |
a0d0e21e | 696 | EXTEND(SP,1); |
79072805 | 697 | } |
adbc6bb1 | 698 | TARG = sv_newmortal(); |
4757a243 | 699 | PUSHi(do_trans(sv)); |
a0d0e21e | 700 | RETURN; |
79072805 LW |
701 | } |
702 | ||
a0d0e21e | 703 | /* Lvalue operators. */ |
79072805 | 704 | |
a0d0e21e LW |
705 | PP(pp_schop) |
706 | { | |
39644a26 | 707 | dSP; dTARGET; |
a0d0e21e LW |
708 | do_chop(TARG, TOPs); |
709 | SETTARG; | |
710 | RETURN; | |
79072805 LW |
711 | } |
712 | ||
a0d0e21e | 713 | PP(pp_chop) |
79072805 | 714 | { |
2ec6af5f RG |
715 | dSP; dMARK; dTARGET; dORIGMARK; |
716 | while (MARK < SP) | |
717 | do_chop(TARG, *++MARK); | |
718 | SP = ORIGMARK; | |
b59aed67 | 719 | XPUSHTARG; |
a0d0e21e | 720 | RETURN; |
79072805 LW |
721 | } |
722 | ||
a0d0e21e | 723 | PP(pp_schomp) |
79072805 | 724 | { |
39644a26 | 725 | dSP; dTARGET; |
a0d0e21e LW |
726 | SETi(do_chomp(TOPs)); |
727 | RETURN; | |
79072805 LW |
728 | } |
729 | ||
a0d0e21e | 730 | PP(pp_chomp) |
79072805 | 731 | { |
39644a26 | 732 | dSP; dMARK; dTARGET; |
a0d0e21e | 733 | register I32 count = 0; |
8ec5e241 | 734 | |
a0d0e21e LW |
735 | while (SP > MARK) |
736 | count += do_chomp(POPs); | |
b59aed67 | 737 | XPUSHi(count); |
a0d0e21e | 738 | RETURN; |
79072805 LW |
739 | } |
740 | ||
a0d0e21e LW |
741 | PP(pp_undef) |
742 | { | |
39644a26 | 743 | dSP; |
a0d0e21e LW |
744 | SV *sv; |
745 | ||
533c011a | 746 | if (!PL_op->op_private) { |
774d564b | 747 | EXTEND(SP, 1); |
a0d0e21e | 748 | RETPUSHUNDEF; |
774d564b | 749 | } |
79072805 | 750 | |
a0d0e21e LW |
751 | sv = POPs; |
752 | if (!sv) | |
753 | RETPUSHUNDEF; | |
85e6fe83 | 754 | |
765f542d | 755 | SV_CHECK_THINKFIRST_COW_DROP(sv); |
85e6fe83 | 756 | |
a0d0e21e LW |
757 | switch (SvTYPE(sv)) { |
758 | case SVt_NULL: | |
759 | break; | |
760 | case SVt_PVAV: | |
761 | av_undef((AV*)sv); | |
762 | break; | |
763 | case SVt_PVHV: | |
764 | hv_undef((HV*)sv); | |
765 | break; | |
766 | case SVt_PVCV: | |
041457d9 | 767 | if (cv_const_sv((CV*)sv) && ckWARN(WARN_MISC)) |
9014280d | 768 | Perl_warner(aTHX_ packWARN(WARN_MISC), "Constant subroutine %s undefined", |
54310121 | 769 | CvANON((CV*)sv) ? "(anonymous)" : GvENAME(CvGV((CV*)sv))); |
9607fc9c | 770 | /* FALL THROUGH */ |
771 | case SVt_PVFM: | |
6fc92669 GS |
772 | { |
773 | /* let user-undef'd sub keep its identity */ | |
0bd48802 | 774 | GV* const gv = CvGV((CV*)sv); |
6fc92669 GS |
775 | cv_undef((CV*)sv); |
776 | CvGV((CV*)sv) = gv; | |
777 | } | |
a0d0e21e | 778 | break; |
8e07c86e | 779 | case SVt_PVGV: |
44a8e56a | 780 | if (SvFAKE(sv)) |
3280af22 | 781 | SvSetMagicSV(sv, &PL_sv_undef); |
20408e3c GS |
782 | else { |
783 | GP *gp; | |
784 | gp_free((GV*)sv); | |
a02a5408 | 785 | Newxz(gp, 1, GP); |
20408e3c GS |
786 | GvGP(sv) = gp_ref(gp); |
787 | GvSV(sv) = NEWSV(72,0); | |
57843af0 | 788 | GvLINE(sv) = CopLINE(PL_curcop); |
20408e3c GS |
789 | GvEGV(sv) = (GV*)sv; |
790 | GvMULTI_on(sv); | |
791 | } | |
44a8e56a | 792 | break; |
a0d0e21e | 793 | default: |
b15aece3 | 794 | if (SvTYPE(sv) >= SVt_PV && SvPVX_const(sv) && SvLEN(sv)) { |
8bd4d4c5 | 795 | SvPV_free(sv); |
4633a7c4 LW |
796 | SvPV_set(sv, Nullch); |
797 | SvLEN_set(sv, 0); | |
a0d0e21e | 798 | } |
0c34ef67 | 799 | SvOK_off(sv); |
4633a7c4 | 800 | SvSETMAGIC(sv); |
79072805 | 801 | } |
a0d0e21e LW |
802 | |
803 | RETPUSHUNDEF; | |
79072805 LW |
804 | } |
805 | ||
a0d0e21e | 806 | PP(pp_predec) |
79072805 | 807 | { |
39644a26 | 808 | dSP; |
f39684df | 809 | if (SvTYPE(TOPs) >= SVt_PVGV && SvTYPE(TOPs) != SVt_PVLV) |
d470f89e | 810 | DIE(aTHX_ PL_no_modify); |
3510b4a1 NC |
811 | if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) |
812 | && SvIVX(TOPs) != IV_MIN) | |
55497cff | 813 | { |
45977657 | 814 | SvIV_set(TOPs, SvIVX(TOPs) - 1); |
55497cff | 815 | SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK); |
748a9306 LW |
816 | } |
817 | else | |
818 | sv_dec(TOPs); | |
a0d0e21e LW |
819 | SvSETMAGIC(TOPs); |
820 | return NORMAL; | |
821 | } | |
79072805 | 822 | |
a0d0e21e LW |
823 | PP(pp_postinc) |
824 | { | |
39644a26 | 825 | dSP; dTARGET; |
f39684df | 826 | if (SvTYPE(TOPs) >= SVt_PVGV && SvTYPE(TOPs) != SVt_PVLV) |
d470f89e | 827 | DIE(aTHX_ PL_no_modify); |
a0d0e21e | 828 | sv_setsv(TARG, TOPs); |
3510b4a1 NC |
829 | if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) |
830 | && SvIVX(TOPs) != IV_MAX) | |
55497cff | 831 | { |
45977657 | 832 | SvIV_set(TOPs, SvIVX(TOPs) + 1); |
55497cff | 833 | SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK); |
748a9306 LW |
834 | } |
835 | else | |
836 | sv_inc(TOPs); | |
a0d0e21e | 837 | SvSETMAGIC(TOPs); |
1e54a23f | 838 | /* special case for undef: see thread at 2003-03/msg00536.html in archive */ |
a0d0e21e LW |
839 | if (!SvOK(TARG)) |
840 | sv_setiv(TARG, 0); | |
841 | SETs(TARG); | |
842 | return NORMAL; | |
843 | } | |
79072805 | 844 | |
a0d0e21e LW |
845 | PP(pp_postdec) |
846 | { | |
39644a26 | 847 | dSP; dTARGET; |
f39684df | 848 | if (SvTYPE(TOPs) >= SVt_PVGV && SvTYPE(TOPs) != SVt_PVLV) |
d470f89e | 849 | DIE(aTHX_ PL_no_modify); |
a0d0e21e | 850 | sv_setsv(TARG, TOPs); |
3510b4a1 NC |
851 | if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) |
852 | && SvIVX(TOPs) != IV_MIN) | |
55497cff | 853 | { |
45977657 | 854 | SvIV_set(TOPs, SvIVX(TOPs) - 1); |
55497cff | 855 | SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK); |
748a9306 LW |
856 | } |
857 | else | |
858 | sv_dec(TOPs); | |
a0d0e21e LW |
859 | SvSETMAGIC(TOPs); |
860 | SETs(TARG); | |
861 | return NORMAL; | |
862 | } | |
79072805 | 863 | |
a0d0e21e LW |
864 | /* Ordinary operators. */ |
865 | ||
866 | PP(pp_pow) | |
867 | { | |
52a96ae6 | 868 | dSP; dATARGET; |
58d76dfd | 869 | #ifdef PERL_PRESERVE_IVUV |
52a96ae6 HS |
870 | bool is_int = 0; |
871 | #endif | |
872 | tryAMAGICbin(pow,opASSIGN); | |
873 | #ifdef PERL_PRESERVE_IVUV | |
874 | /* For integer to integer power, we do the calculation by hand wherever | |
875 | we're sure it is safe; otherwise we call pow() and try to convert to | |
876 | integer afterwards. */ | |
58d76dfd | 877 | { |
900658e3 PF |
878 | SvIV_please(TOPs); |
879 | if (SvIOK(TOPs)) { | |
880 | SvIV_please(TOPm1s); | |
881 | if (SvIOK(TOPm1s)) { | |
882 | UV power; | |
883 | bool baseuok; | |
884 | UV baseuv; | |
885 | ||
886 | if (SvUOK(TOPs)) { | |
887 | power = SvUVX(TOPs); | |
888 | } else { | |
889 | const IV iv = SvIVX(TOPs); | |
890 | if (iv >= 0) { | |
891 | power = iv; | |
892 | } else { | |
893 | goto float_it; /* Can't do negative powers this way. */ | |
894 | } | |
895 | } | |
896 | ||
897 | baseuok = SvUOK(TOPm1s); | |
898 | if (baseuok) { | |
899 | baseuv = SvUVX(TOPm1s); | |
900 | } else { | |
901 | const IV iv = SvIVX(TOPm1s); | |
902 | if (iv >= 0) { | |
903 | baseuv = iv; | |
904 | baseuok = TRUE; /* effectively it's a UV now */ | |
905 | } else { | |
906 | baseuv = -iv; /* abs, baseuok == false records sign */ | |
907 | } | |
908 | } | |
52a96ae6 HS |
909 | /* now we have integer ** positive integer. */ |
910 | is_int = 1; | |
911 | ||
912 | /* foo & (foo - 1) is zero only for a power of 2. */ | |
58d76dfd | 913 | if (!(baseuv & (baseuv - 1))) { |
52a96ae6 | 914 | /* We are raising power-of-2 to a positive integer. |
58d76dfd JH |
915 | The logic here will work for any base (even non-integer |
916 | bases) but it can be less accurate than | |
917 | pow (base,power) or exp (power * log (base)) when the | |
918 | intermediate values start to spill out of the mantissa. | |
919 | With powers of 2 we know this can't happen. | |
920 | And powers of 2 are the favourite thing for perl | |
921 | programmers to notice ** not doing what they mean. */ | |
922 | NV result = 1.0; | |
923 | NV base = baseuok ? baseuv : -(NV)baseuv; | |
900658e3 PF |
924 | |
925 | if (power & 1) { | |
926 | result *= base; | |
927 | } | |
928 | while (power >>= 1) { | |
929 | base *= base; | |
930 | if (power & 1) { | |
931 | result *= base; | |
932 | } | |
933 | } | |
58d76dfd JH |
934 | SP--; |
935 | SETn( result ); | |
52a96ae6 | 936 | SvIV_please(TOPs); |
58d76dfd | 937 | RETURN; |
52a96ae6 HS |
938 | } else { |
939 | register unsigned int highbit = 8 * sizeof(UV); | |
900658e3 PF |
940 | register unsigned int diff = 8 * sizeof(UV); |
941 | while (diff >>= 1) { | |
942 | highbit -= diff; | |
943 | if (baseuv >> highbit) { | |
944 | highbit += diff; | |
945 | } | |
52a96ae6 HS |
946 | } |
947 | /* we now have baseuv < 2 ** highbit */ | |
948 | if (power * highbit <= 8 * sizeof(UV)) { | |
949 | /* result will definitely fit in UV, so use UV math | |
950 | on same algorithm as above */ | |
951 | register UV result = 1; | |
952 | register UV base = baseuv; | |
900658e3 PF |
953 | const bool odd_power = (bool)(power & 1); |
954 | if (odd_power) { | |
955 | result *= base; | |
956 | } | |
957 | while (power >>= 1) { | |
958 | base *= base; | |
959 | if (power & 1) { | |
52a96ae6 | 960 | result *= base; |
52a96ae6 HS |
961 | } |
962 | } | |
963 | SP--; | |
0615a994 | 964 | if (baseuok || !odd_power) |
52a96ae6 HS |
965 | /* answer is positive */ |
966 | SETu( result ); | |
967 | else if (result <= (UV)IV_MAX) | |
968 | /* answer negative, fits in IV */ | |
969 | SETi( -(IV)result ); | |
970 | else if (result == (UV)IV_MIN) | |
971 | /* 2's complement assumption: special case IV_MIN */ | |
972 | SETi( IV_MIN ); | |
973 | else | |
974 | /* answer negative, doesn't fit */ | |
975 | SETn( -(NV)result ); | |
976 | RETURN; | |
977 | } | |
978 | } | |
979 | } | |
980 | } | |
58d76dfd | 981 | } |
52a96ae6 | 982 | float_it: |
58d76dfd | 983 | #endif |
a0d0e21e | 984 | { |
52a96ae6 HS |
985 | dPOPTOPnnrl; |
986 | SETn( Perl_pow( left, right) ); | |
987 | #ifdef PERL_PRESERVE_IVUV | |
988 | if (is_int) | |
989 | SvIV_please(TOPs); | |
990 | #endif | |
991 | RETURN; | |
93a17b20 | 992 | } |
a0d0e21e LW |
993 | } |
994 | ||
995 | PP(pp_multiply) | |
996 | { | |
39644a26 | 997 | dSP; dATARGET; tryAMAGICbin(mult,opASSIGN); |
28e5dec8 JH |
998 | #ifdef PERL_PRESERVE_IVUV |
999 | SvIV_please(TOPs); | |
1000 | if (SvIOK(TOPs)) { | |
1001 | /* Unless the left argument is integer in range we are going to have to | |
1002 | use NV maths. Hence only attempt to coerce the right argument if | |
1003 | we know the left is integer. */ | |
1004 | /* Left operand is defined, so is it IV? */ | |
1005 | SvIV_please(TOPm1s); | |
1006 | if (SvIOK(TOPm1s)) { | |
1007 | bool auvok = SvUOK(TOPm1s); | |
1008 | bool buvok = SvUOK(TOPs); | |
1009 | const UV topmask = (~ (UV)0) << (4 * sizeof (UV)); | |
1010 | const UV botmask = ~((~ (UV)0) << (4 * sizeof (UV))); | |
1011 | UV alow; | |
1012 | UV ahigh; | |
1013 | UV blow; | |
1014 | UV bhigh; | |
1015 | ||
1016 | if (auvok) { | |
1017 | alow = SvUVX(TOPm1s); | |
1018 | } else { | |
1b6737cc | 1019 | const IV aiv = SvIVX(TOPm1s); |
28e5dec8 JH |
1020 | if (aiv >= 0) { |
1021 | alow = aiv; | |
1022 | auvok = TRUE; /* effectively it's a UV now */ | |
1023 | } else { | |
1024 | alow = -aiv; /* abs, auvok == false records sign */ | |
1025 | } | |
1026 | } | |
1027 | if (buvok) { | |
1028 | blow = SvUVX(TOPs); | |
1029 | } else { | |
1b6737cc | 1030 | const IV biv = SvIVX(TOPs); |
28e5dec8 JH |
1031 | if (biv >= 0) { |
1032 | blow = biv; | |
1033 | buvok = TRUE; /* effectively it's a UV now */ | |
1034 | } else { | |
1035 | blow = -biv; /* abs, buvok == false records sign */ | |
1036 | } | |
1037 | } | |
1038 | ||
1039 | /* If this does sign extension on unsigned it's time for plan B */ | |
1040 | ahigh = alow >> (4 * sizeof (UV)); | |
1041 | alow &= botmask; | |
1042 | bhigh = blow >> (4 * sizeof (UV)); | |
1043 | blow &= botmask; | |
1044 | if (ahigh && bhigh) { | |
1045 | /* eg 32 bit is at least 0x10000 * 0x10000 == 0x100000000 | |
1046 | which is overflow. Drop to NVs below. */ | |
1047 | } else if (!ahigh && !bhigh) { | |
1048 | /* eg 32 bit is at most 0xFFFF * 0xFFFF == 0xFFFE0001 | |
1049 | so the unsigned multiply cannot overflow. */ | |
1050 | UV product = alow * blow; | |
1051 | if (auvok == buvok) { | |
1052 | /* -ve * -ve or +ve * +ve gives a +ve result. */ | |
1053 | SP--; | |
1054 | SETu( product ); | |
1055 | RETURN; | |
1056 | } else if (product <= (UV)IV_MIN) { | |
1057 | /* 2s complement assumption that (UV)-IV_MIN is correct. */ | |
1058 | /* -ve result, which could overflow an IV */ | |
1059 | SP--; | |
25716404 | 1060 | SETi( -(IV)product ); |
28e5dec8 JH |
1061 | RETURN; |
1062 | } /* else drop to NVs below. */ | |
1063 | } else { | |
1064 | /* One operand is large, 1 small */ | |
1065 | UV product_middle; | |
1066 | if (bhigh) { | |
1067 | /* swap the operands */ | |
1068 | ahigh = bhigh; | |
1069 | bhigh = blow; /* bhigh now the temp var for the swap */ | |
1070 | blow = alow; | |
1071 | alow = bhigh; | |
1072 | } | |
1073 | /* now, ((ahigh * blow) << half_UV_len) + (alow * blow) | |
1074 | multiplies can't overflow. shift can, add can, -ve can. */ | |
1075 | product_middle = ahigh * blow; | |
1076 | if (!(product_middle & topmask)) { | |
1077 | /* OK, (ahigh * blow) won't lose bits when we shift it. */ | |
1078 | UV product_low; | |
1079 | product_middle <<= (4 * sizeof (UV)); | |
1080 | product_low = alow * blow; | |
1081 | ||
1082 | /* as for pp_add, UV + something mustn't get smaller. | |
1083 | IIRC ANSI mandates this wrapping *behaviour* for | |
1084 | unsigned whatever the actual representation*/ | |
1085 | product_low += product_middle; | |
1086 | if (product_low >= product_middle) { | |
1087 | /* didn't overflow */ | |
1088 | if (auvok == buvok) { | |
1089 | /* -ve * -ve or +ve * +ve gives a +ve result. */ | |
1090 | SP--; | |
1091 | SETu( product_low ); | |
1092 | RETURN; | |
1093 | } else if (product_low <= (UV)IV_MIN) { | |
1094 | /* 2s complement assumption again */ | |
1095 | /* -ve result, which could overflow an IV */ | |
1096 | SP--; | |
25716404 | 1097 | SETi( -(IV)product_low ); |
28e5dec8 JH |
1098 | RETURN; |
1099 | } /* else drop to NVs below. */ | |
1100 | } | |
1101 | } /* product_middle too large */ | |
1102 | } /* ahigh && bhigh */ | |
1103 | } /* SvIOK(TOPm1s) */ | |
1104 | } /* SvIOK(TOPs) */ | |
1105 | #endif | |
a0d0e21e LW |
1106 | { |
1107 | dPOPTOPnnrl; | |
1108 | SETn( left * right ); | |
1109 | RETURN; | |
79072805 | 1110 | } |
a0d0e21e LW |
1111 | } |
1112 | ||
1113 | PP(pp_divide) | |
1114 | { | |
39644a26 | 1115 | dSP; dATARGET; tryAMAGICbin(div,opASSIGN); |
5479d192 | 1116 | /* Only try to do UV divide first |
68795e93 | 1117 | if ((SLOPPYDIVIDE is true) or |
5479d192 NC |
1118 | (PERL_PRESERVE_IVUV is true and one or both SV is a UV too large |
1119 | to preserve)) | |
1120 | The assumption is that it is better to use floating point divide | |
1121 | whenever possible, only doing integer divide first if we can't be sure. | |
1122 | If NV_PRESERVES_UV is true then we know at compile time that no UV | |
1123 | can be too large to preserve, so don't need to compile the code to | |
1124 | test the size of UVs. */ | |
1125 | ||
a0d0e21e | 1126 | #ifdef SLOPPYDIVIDE |
5479d192 NC |
1127 | # define PERL_TRY_UV_DIVIDE |
1128 | /* ensure that 20./5. == 4. */ | |
a0d0e21e | 1129 | #else |
5479d192 NC |
1130 | # ifdef PERL_PRESERVE_IVUV |
1131 | # ifndef NV_PRESERVES_UV | |
1132 | # define PERL_TRY_UV_DIVIDE | |
1133 | # endif | |
1134 | # endif | |
a0d0e21e | 1135 | #endif |
5479d192 NC |
1136 | |
1137 | #ifdef PERL_TRY_UV_DIVIDE | |
1138 | SvIV_please(TOPs); | |
1139 | if (SvIOK(TOPs)) { | |
1140 | SvIV_please(TOPm1s); | |
1141 | if (SvIOK(TOPm1s)) { | |
1142 | bool left_non_neg = SvUOK(TOPm1s); | |
1143 | bool right_non_neg = SvUOK(TOPs); | |
1144 | UV left; | |
1145 | UV right; | |
1146 | ||
1147 | if (right_non_neg) { | |
1148 | right = SvUVX(TOPs); | |
1149 | } | |
1150 | else { | |
1b6737cc | 1151 | const IV biv = SvIVX(TOPs); |
5479d192 NC |
1152 | if (biv >= 0) { |
1153 | right = biv; | |
1154 | right_non_neg = TRUE; /* effectively it's a UV now */ | |
1155 | } | |
1156 | else { | |
1157 | right = -biv; | |
1158 | } | |
1159 | } | |
1160 | /* historically undef()/0 gives a "Use of uninitialized value" | |
1161 | warning before dieing, hence this test goes here. | |
1162 | If it were immediately before the second SvIV_please, then | |
1163 | DIE() would be invoked before left was even inspected, so | |
1164 | no inpsection would give no warning. */ | |
1165 | if (right == 0) | |
1166 | DIE(aTHX_ "Illegal division by zero"); | |
1167 | ||
1168 | if (left_non_neg) { | |
1169 | left = SvUVX(TOPm1s); | |
1170 | } | |
1171 | else { | |
1b6737cc | 1172 | const IV aiv = SvIVX(TOPm1s); |
5479d192 NC |
1173 | if (aiv >= 0) { |
1174 | left = aiv; | |
1175 | left_non_neg = TRUE; /* effectively it's a UV now */ | |
1176 | } | |
1177 | else { | |
1178 | left = -aiv; | |
1179 | } | |
1180 | } | |
1181 | ||
1182 | if (left >= right | |
1183 | #ifdef SLOPPYDIVIDE | |
1184 | /* For sloppy divide we always attempt integer division. */ | |
1185 | #else | |
1186 | /* Otherwise we only attempt it if either or both operands | |
1187 | would not be preserved by an NV. If both fit in NVs | |
0c2ee62a NC |
1188 | we fall through to the NV divide code below. However, |
1189 | as left >= right to ensure integer result here, we know that | |
1190 | we can skip the test on the right operand - right big | |
1191 | enough not to be preserved can't get here unless left is | |
1192 | also too big. */ | |
1193 | ||
1194 | && (left > ((UV)1 << NV_PRESERVES_UV_BITS)) | |
5479d192 NC |
1195 | #endif |
1196 | ) { | |
1197 | /* Integer division can't overflow, but it can be imprecise. */ | |
1b6737cc | 1198 | const UV result = left / right; |
5479d192 NC |
1199 | if (result * right == left) { |
1200 | SP--; /* result is valid */ | |
1201 | if (left_non_neg == right_non_neg) { | |
1202 | /* signs identical, result is positive. */ | |
1203 | SETu( result ); | |
1204 | RETURN; | |
1205 | } | |
1206 | /* 2s complement assumption */ | |
1207 | if (result <= (UV)IV_MIN) | |
91f3b821 | 1208 | SETi( -(IV)result ); |
5479d192 NC |
1209 | else { |
1210 | /* It's exact but too negative for IV. */ | |
1211 | SETn( -(NV)result ); | |
1212 | } | |
1213 | RETURN; | |
1214 | } /* tried integer divide but it was not an integer result */ | |
32fdb065 | 1215 | } /* else (PERL_ABS(result) < 1.0) or (both UVs in range for NV) */ |
5479d192 NC |
1216 | } /* left wasn't SvIOK */ |
1217 | } /* right wasn't SvIOK */ | |
1218 | #endif /* PERL_TRY_UV_DIVIDE */ | |
1219 | { | |
1220 | dPOPPOPnnrl; | |
1221 | if (right == 0.0) | |
1222 | DIE(aTHX_ "Illegal division by zero"); | |
1223 | PUSHn( left / right ); | |
1224 | RETURN; | |
79072805 | 1225 | } |
a0d0e21e LW |
1226 | } |
1227 | ||
1228 | PP(pp_modulo) | |
1229 | { | |
39644a26 | 1230 | dSP; dATARGET; tryAMAGICbin(modulo,opASSIGN); |
a0d0e21e | 1231 | { |
9c5ffd7c JH |
1232 | UV left = 0; |
1233 | UV right = 0; | |
dc656993 JH |
1234 | bool left_neg = FALSE; |
1235 | bool right_neg = FALSE; | |
e2c88acc NC |
1236 | bool use_double = FALSE; |
1237 | bool dright_valid = FALSE; | |
9c5ffd7c JH |
1238 | NV dright = 0.0; |
1239 | NV dleft = 0.0; | |
787eafbd | 1240 | |
e2c88acc NC |
1241 | SvIV_please(TOPs); |
1242 | if (SvIOK(TOPs)) { | |
1243 | right_neg = !SvUOK(TOPs); | |
1244 | if (!right_neg) { | |
1245 | right = SvUVX(POPs); | |
1246 | } else { | |
1b6737cc | 1247 | const IV biv = SvIVX(POPs); |
e2c88acc NC |
1248 | if (biv >= 0) { |
1249 | right = biv; | |
1250 | right_neg = FALSE; /* effectively it's a UV now */ | |
1251 | } else { | |
1252 | right = -biv; | |
1253 | } | |
1254 | } | |
1255 | } | |
1256 | else { | |
787eafbd | 1257 | dright = POPn; |
787eafbd IZ |
1258 | right_neg = dright < 0; |
1259 | if (right_neg) | |
1260 | dright = -dright; | |
e2c88acc NC |
1261 | if (dright < UV_MAX_P1) { |
1262 | right = U_V(dright); | |
1263 | dright_valid = TRUE; /* In case we need to use double below. */ | |
1264 | } else { | |
1265 | use_double = TRUE; | |
1266 | } | |
787eafbd | 1267 | } |
a0d0e21e | 1268 | |
e2c88acc NC |
1269 | /* At this point use_double is only true if right is out of range for |
1270 | a UV. In range NV has been rounded down to nearest UV and | |
1271 | use_double false. */ | |
1272 | SvIV_please(TOPs); | |
1273 | if (!use_double && SvIOK(TOPs)) { | |
1274 | if (SvIOK(TOPs)) { | |
1275 | left_neg = !SvUOK(TOPs); | |
1276 | if (!left_neg) { | |
1277 | left = SvUVX(POPs); | |
1278 | } else { | |
0bd48802 | 1279 | const IV aiv = SvIVX(POPs); |
e2c88acc NC |
1280 | if (aiv >= 0) { |
1281 | left = aiv; | |
1282 | left_neg = FALSE; /* effectively it's a UV now */ | |
1283 | } else { | |
1284 | left = -aiv; | |
1285 | } | |
1286 | } | |
1287 | } | |
1288 | } | |
787eafbd IZ |
1289 | else { |
1290 | dleft = POPn; | |
787eafbd IZ |
1291 | left_neg = dleft < 0; |
1292 | if (left_neg) | |
1293 | dleft = -dleft; | |
68dc0745 | 1294 | |
e2c88acc NC |
1295 | /* This should be exactly the 5.6 behaviour - if left and right are |
1296 | both in range for UV then use U_V() rather than floor. */ | |
1297 | if (!use_double) { | |
1298 | if (dleft < UV_MAX_P1) { | |
1299 | /* right was in range, so is dleft, so use UVs not double. | |
1300 | */ | |
1301 | left = U_V(dleft); | |
1302 | } | |
1303 | /* left is out of range for UV, right was in range, so promote | |
1304 | right (back) to double. */ | |
1305 | else { | |
1306 | /* The +0.5 is used in 5.6 even though it is not strictly | |
1307 | consistent with the implicit +0 floor in the U_V() | |
1308 | inside the #if 1. */ | |
1309 | dleft = Perl_floor(dleft + 0.5); | |
1310 | use_double = TRUE; | |
1311 | if (dright_valid) | |
1312 | dright = Perl_floor(dright + 0.5); | |
1313 | else | |
1314 | dright = right; | |
1315 | } | |
1316 | } | |
1317 | } | |
787eafbd | 1318 | if (use_double) { |
65202027 | 1319 | NV dans; |
787eafbd | 1320 | |
787eafbd | 1321 | if (!dright) |
cea2e8a9 | 1322 | DIE(aTHX_ "Illegal modulus zero"); |
787eafbd | 1323 | |
65202027 | 1324 | dans = Perl_fmod(dleft, dright); |
787eafbd IZ |
1325 | if ((left_neg != right_neg) && dans) |
1326 | dans = dright - dans; | |
1327 | if (right_neg) | |
1328 | dans = -dans; | |
1329 | sv_setnv(TARG, dans); | |
1330 | } | |
1331 | else { | |
1332 | UV ans; | |
1333 | ||
787eafbd | 1334 | if (!right) |
cea2e8a9 | 1335 | DIE(aTHX_ "Illegal modulus zero"); |
787eafbd IZ |
1336 | |
1337 | ans = left % right; | |
1338 | if ((left_neg != right_neg) && ans) | |
1339 | ans = right - ans; | |
1340 | if (right_neg) { | |
1341 | /* XXX may warn: unary minus operator applied to unsigned type */ | |
1342 | /* could change -foo to be (~foo)+1 instead */ | |
1343 | if (ans <= ~((UV)IV_MAX)+1) | |
1344 | sv_setiv(TARG, ~ans+1); | |
1345 | else | |
65202027 | 1346 | sv_setnv(TARG, -(NV)ans); |
787eafbd IZ |
1347 | } |
1348 | else | |
1349 | sv_setuv(TARG, ans); | |
1350 | } | |
1351 | PUSHTARG; | |
1352 | RETURN; | |
79072805 | 1353 | } |
a0d0e21e | 1354 | } |
79072805 | 1355 | |
a0d0e21e LW |
1356 | PP(pp_repeat) |
1357 | { | |
39644a26 | 1358 | dSP; dATARGET; tryAMAGICbin(repeat,opASSIGN); |
748a9306 | 1359 | { |
2b573ace JH |
1360 | register IV count; |
1361 | dPOPss; | |
5b295bef | 1362 | SvGETMAGIC(sv); |
2b573ace JH |
1363 | if (SvIOKp(sv)) { |
1364 | if (SvUOK(sv)) { | |
1b6737cc | 1365 | const UV uv = SvUV(sv); |
2b573ace JH |
1366 | if (uv > IV_MAX) |
1367 | count = IV_MAX; /* The best we can do? */ | |
1368 | else | |
1369 | count = uv; | |
1370 | } else { | |
0bd48802 | 1371 | const IV iv = SvIV(sv); |
2b573ace JH |
1372 | if (iv < 0) |
1373 | count = 0; | |
1374 | else | |
1375 | count = iv; | |
1376 | } | |
1377 | } | |
1378 | else if (SvNOKp(sv)) { | |
1b6737cc | 1379 | const NV nv = SvNV(sv); |
2b573ace JH |
1380 | if (nv < 0.0) |
1381 | count = 0; | |
1382 | else | |
1383 | count = (IV)nv; | |
1384 | } | |
1385 | else | |
1386 | count = SvIVx(sv); | |
533c011a | 1387 | if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) { |
a0d0e21e | 1388 | dMARK; |
0bd48802 AL |
1389 | static const char oom_list_extend[] = "Out of memory during list extend"; |
1390 | const I32 items = SP - MARK; | |
1391 | const I32 max = items * count; | |
79072805 | 1392 | |
2b573ace JH |
1393 | MEM_WRAP_CHECK_1(max, SV*, oom_list_extend); |
1394 | /* Did the max computation overflow? */ | |
27d5b266 | 1395 | if (items > 0 && max > 0 && (max < items || max < count)) |
2b573ace | 1396 | Perl_croak(aTHX_ oom_list_extend); |
a0d0e21e LW |
1397 | MEXTEND(MARK, max); |
1398 | if (count > 1) { | |
1399 | while (SP > MARK) { | |
976c8a39 JH |
1400 | #if 0 |
1401 | /* This code was intended to fix 20010809.028: | |
1402 | ||
1403 | $x = 'abcd'; | |
1404 | for (($x =~ /./g) x 2) { | |
1405 | print chop; # "abcdabcd" expected as output. | |
1406 | } | |
1407 | ||
1408 | * but that change (#11635) broke this code: | |
1409 | ||
1410 | $x = [("foo")x2]; # only one "foo" ended up in the anonlist. | |
1411 | ||
1412 | * I can't think of a better fix that doesn't introduce | |
1413 | * an efficiency hit by copying the SVs. The stack isn't | |
1414 | * refcounted, and mortalisation obviously doesn't | |
1415 | * Do The Right Thing when the stack has more than | |
1416 | * one pointer to the same mortal value. | |
1417 | * .robin. | |
1418 | */ | |
e30acc16 RH |
1419 | if (*SP) { |
1420 | *SP = sv_2mortal(newSVsv(*SP)); | |
1421 | SvREADONLY_on(*SP); | |
1422 | } | |
976c8a39 JH |
1423 | #else |
1424 | if (*SP) | |
1425 | SvTEMP_off((*SP)); | |
1426 | #endif | |
a0d0e21e | 1427 | SP--; |
79072805 | 1428 | } |
a0d0e21e LW |
1429 | MARK++; |
1430 | repeatcpy((char*)(MARK + items), (char*)MARK, | |
1431 | items * sizeof(SV*), count - 1); | |
1432 | SP += max; | |
79072805 | 1433 | } |
a0d0e21e LW |
1434 | else if (count <= 0) |
1435 | SP -= items; | |
79072805 | 1436 | } |
a0d0e21e | 1437 | else { /* Note: mark already snarfed by pp_list */ |
0bd48802 | 1438 | SV * const tmpstr = POPs; |
a0d0e21e | 1439 | STRLEN len; |
9b877dbb | 1440 | bool isutf; |
2b573ace JH |
1441 | static const char oom_string_extend[] = |
1442 | "Out of memory during string extend"; | |
a0d0e21e | 1443 | |
a0d0e21e LW |
1444 | SvSetSV(TARG, tmpstr); |
1445 | SvPV_force(TARG, len); | |
9b877dbb | 1446 | isutf = DO_UTF8(TARG); |
8ebc5c01 | 1447 | if (count != 1) { |
1448 | if (count < 1) | |
1449 | SvCUR_set(TARG, 0); | |
1450 | else { | |
991350d8 | 1451 | STRLEN max = (UV)count * len; |
2b573ace JH |
1452 | if (len > ((MEM_SIZE)~0)/count) |
1453 | Perl_croak(aTHX_ oom_string_extend); | |
1454 | MEM_WRAP_CHECK_1(max, char, oom_string_extend); | |
8569b9dc | 1455 | SvGROW(TARG, max + 1); |
a0d0e21e | 1456 | repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1); |
b162af07 | 1457 | SvCUR_set(TARG, SvCUR(TARG) * count); |
7a4c00b4 | 1458 | } |
a0d0e21e | 1459 | *SvEND(TARG) = '\0'; |
a0d0e21e | 1460 | } |
dfcb284a GS |
1461 | if (isutf) |
1462 | (void)SvPOK_only_UTF8(TARG); | |
1463 | else | |
1464 | (void)SvPOK_only(TARG); | |
b80b6069 RH |
1465 | |
1466 | if (PL_op->op_private & OPpREPEAT_DOLIST) { | |
1467 | /* The parser saw this as a list repeat, and there | |
1468 | are probably several items on the stack. But we're | |
1469 | in scalar context, and there's no pp_list to save us | |
1470 | now. So drop the rest of the items -- robin@kitsite.com | |
1471 | */ | |
1472 | dMARK; | |
1473 | SP = MARK; | |
1474 | } | |
a0d0e21e | 1475 | PUSHTARG; |
79072805 | 1476 | } |
a0d0e21e | 1477 | RETURN; |
748a9306 | 1478 | } |
a0d0e21e | 1479 | } |
79072805 | 1480 | |
a0d0e21e LW |
1481 | PP(pp_subtract) |
1482 | { | |
39644a26 | 1483 | dSP; dATARGET; bool useleft; tryAMAGICbin(subtr,opASSIGN); |
28e5dec8 JH |
1484 | useleft = USE_LEFT(TOPm1s); |
1485 | #ifdef PERL_PRESERVE_IVUV | |
7dca457a NC |
1486 | /* See comments in pp_add (in pp_hot.c) about Overflow, and how |
1487 | "bad things" happen if you rely on signed integers wrapping. */ | |
28e5dec8 JH |
1488 | SvIV_please(TOPs); |
1489 | if (SvIOK(TOPs)) { | |
1490 | /* Unless the left argument is integer in range we are going to have to | |
1491 | use NV maths. Hence only attempt to coerce the right argument if | |
1492 | we know the left is integer. */ | |
9c5ffd7c JH |
1493 | register UV auv = 0; |
1494 | bool auvok = FALSE; | |
7dca457a NC |
1495 | bool a_valid = 0; |
1496 | ||
28e5dec8 | 1497 | if (!useleft) { |
7dca457a NC |
1498 | auv = 0; |
1499 | a_valid = auvok = 1; | |
1500 | /* left operand is undef, treat as zero. */ | |
28e5dec8 JH |
1501 | } else { |
1502 | /* Left operand is defined, so is it IV? */ | |
1503 | SvIV_please(TOPm1s); | |
1504 | if (SvIOK(TOPm1s)) { | |
7dca457a NC |
1505 | if ((auvok = SvUOK(TOPm1s))) |
1506 | auv = SvUVX(TOPm1s); | |
1507 | else { | |
1b6737cc | 1508 | register const IV aiv = SvIVX(TOPm1s); |
7dca457a NC |
1509 | if (aiv >= 0) { |
1510 | auv = aiv; | |
1511 | auvok = 1; /* Now acting as a sign flag. */ | |
1512 | } else { /* 2s complement assumption for IV_MIN */ | |
1513 | auv = (UV)-aiv; | |
28e5dec8 | 1514 | } |
7dca457a NC |
1515 | } |
1516 | a_valid = 1; | |
1517 | } | |
1518 | } | |
1519 | if (a_valid) { | |
1520 | bool result_good = 0; | |
1521 | UV result; | |
1522 | register UV buv; | |
1523 | bool buvok = SvUOK(TOPs); | |
9041c2e3 | 1524 | |
7dca457a NC |
1525 | if (buvok) |
1526 | buv = SvUVX(TOPs); | |
1527 | else { | |
1b6737cc | 1528 | register const IV biv = SvIVX(TOPs); |
7dca457a NC |
1529 | if (biv >= 0) { |
1530 | buv = biv; | |
1531 | buvok = 1; | |
1532 | } else | |
1533 | buv = (UV)-biv; | |
1534 | } | |
1535 | /* ?uvok if value is >= 0. basically, flagged as UV if it's +ve, | |
602f51c4 | 1536 | else "IV" now, independent of how it came in. |
7dca457a NC |
1537 | if a, b represents positive, A, B negative, a maps to -A etc |
1538 | a - b => (a - b) | |
1539 | A - b => -(a + b) | |
1540 | a - B => (a + b) | |
1541 | A - B => -(a - b) | |
1542 | all UV maths. negate result if A negative. | |
1543 | subtract if signs same, add if signs differ. */ | |
1544 | ||
1545 | if (auvok ^ buvok) { | |
1546 | /* Signs differ. */ | |
1547 | result = auv + buv; | |
1548 | if (result >= auv) | |
1549 | result_good = 1; | |
1550 | } else { | |
1551 | /* Signs same */ | |
1552 | if (auv >= buv) { | |
1553 | result = auv - buv; | |
1554 | /* Must get smaller */ | |
1555 | if (result <= auv) | |
1556 | result_good = 1; | |
1557 | } else { | |
1558 | result = buv - auv; | |
1559 | if (result <= buv) { | |
1560 | /* result really should be -(auv-buv). as its negation | |
1561 | of true value, need to swap our result flag */ | |
1562 | auvok = !auvok; | |
1563 | result_good = 1; | |
28e5dec8 | 1564 | } |
28e5dec8 JH |
1565 | } |
1566 | } | |
7dca457a NC |
1567 | if (result_good) { |
1568 | SP--; | |
1569 | if (auvok) | |
1570 | SETu( result ); | |
1571 | else { | |
1572 | /* Negate result */ | |
1573 | if (result <= (UV)IV_MIN) | |
1574 | SETi( -(IV)result ); | |
1575 | else { | |
1576 | /* result valid, but out of range for IV. */ | |
1577 | SETn( -(NV)result ); | |
1578 | } | |
1579 | } | |
1580 | RETURN; | |
1581 | } /* Overflow, drop through to NVs. */ | |
28e5dec8 JH |
1582 | } |
1583 | } | |
1584 | #endif | |
7dca457a | 1585 | useleft = USE_LEFT(TOPm1s); |
a0d0e21e | 1586 | { |
28e5dec8 JH |
1587 | dPOPnv; |
1588 | if (!useleft) { | |
1589 | /* left operand is undef, treat as zero - value */ | |
1590 | SETn(-value); | |
1591 | RETURN; | |
1592 | } | |
1593 | SETn( TOPn - value ); | |
1594 | RETURN; | |
79072805 | 1595 | } |
a0d0e21e | 1596 | } |
79072805 | 1597 | |
a0d0e21e LW |
1598 | PP(pp_left_shift) |
1599 | { | |
39644a26 | 1600 | dSP; dATARGET; tryAMAGICbin(lshift,opASSIGN); |
a0d0e21e | 1601 | { |
1b6737cc | 1602 | const IV shift = POPi; |
d0ba1bd2 | 1603 | if (PL_op->op_private & HINT_INTEGER) { |
972b05a9 JH |
1604 | IV i = TOPi; |
1605 | SETi(i << shift); | |
d0ba1bd2 JH |
1606 | } |
1607 | else { | |
972b05a9 JH |
1608 | UV u = TOPu; |
1609 | SETu(u << shift); | |
d0ba1bd2 | 1610 | } |
55497cff | 1611 | RETURN; |
79072805 | 1612 | } |
a0d0e21e | 1613 | } |
79072805 | 1614 | |
a0d0e21e LW |
1615 | PP(pp_right_shift) |
1616 | { | |
39644a26 | 1617 | dSP; dATARGET; tryAMAGICbin(rshift,opASSIGN); |
a0d0e21e | 1618 | { |
1b6737cc | 1619 | const IV shift = POPi; |
d0ba1bd2 | 1620 | if (PL_op->op_private & HINT_INTEGER) { |
0bd48802 | 1621 | const IV i = TOPi; |
972b05a9 | 1622 | SETi(i >> shift); |
d0ba1bd2 JH |
1623 | } |
1624 | else { | |
0bd48802 | 1625 | const UV u = TOPu; |
972b05a9 | 1626 | SETu(u >> shift); |
d0ba1bd2 | 1627 | } |
a0d0e21e | 1628 | RETURN; |
93a17b20 | 1629 | } |
79072805 LW |
1630 | } |
1631 | ||
a0d0e21e | 1632 | PP(pp_lt) |
79072805 | 1633 | { |
39644a26 | 1634 | dSP; tryAMAGICbinSET(lt,0); |
28e5dec8 JH |
1635 | #ifdef PERL_PRESERVE_IVUV |
1636 | SvIV_please(TOPs); | |
1637 | if (SvIOK(TOPs)) { | |
1638 | SvIV_please(TOPm1s); | |
1639 | if (SvIOK(TOPm1s)) { | |
1640 | bool auvok = SvUOK(TOPm1s); | |
1641 | bool buvok = SvUOK(TOPs); | |
a227d84d | 1642 | |
28e5dec8 | 1643 | if (!auvok && !buvok) { /* ## IV < IV ## */ |
1b6737cc AL |
1644 | const IV aiv = SvIVX(TOPm1s); |
1645 | const IV biv = SvIVX(TOPs); | |
28e5dec8 JH |
1646 | |
1647 | SP--; | |
1648 | SETs(boolSV(aiv < biv)); | |
1649 | RETURN; | |
1650 | } | |
1651 | if (auvok && buvok) { /* ## UV < UV ## */ | |
1b6737cc AL |
1652 | const UV auv = SvUVX(TOPm1s); |
1653 | const UV buv = SvUVX(TOPs); | |
28e5dec8 JH |
1654 | |
1655 | SP--; | |
1656 | SETs(boolSV(auv < buv)); | |
1657 | RETURN; | |
1658 | } | |
1659 | if (auvok) { /* ## UV < IV ## */ | |
1660 | UV auv; | |
1b6737cc | 1661 | const IV biv = SvIVX(TOPs); |
28e5dec8 JH |
1662 | SP--; |
1663 | if (biv < 0) { | |
1664 | /* As (a) is a UV, it's >=0, so it cannot be < */ | |
1665 | SETs(&PL_sv_no); | |
1666 | RETURN; | |
1667 | } | |
1668 | auv = SvUVX(TOPs); | |
28e5dec8 JH |
1669 | SETs(boolSV(auv < (UV)biv)); |
1670 | RETURN; | |
1671 | } | |
1672 | { /* ## IV < UV ## */ | |
1b6737cc | 1673 | const IV aiv = SvIVX(TOPm1s); |
28e5dec8 JH |
1674 | UV buv; |
1675 | ||
28e5dec8 JH |
1676 | if (aiv < 0) { |
1677 | /* As (b) is a UV, it's >=0, so it must be < */ | |
1678 | SP--; | |
1679 | SETs(&PL_sv_yes); | |
1680 | RETURN; | |
1681 | } | |
1682 | buv = SvUVX(TOPs); | |
1683 | SP--; | |
28e5dec8 JH |
1684 | SETs(boolSV((UV)aiv < buv)); |
1685 | RETURN; | |
1686 | } | |
1687 | } | |
1688 | } | |
1689 | #endif | |
30de85b6 | 1690 | #ifndef NV_PRESERVES_UV |
50fb3111 NC |
1691 | #ifdef PERL_PRESERVE_IVUV |
1692 | else | |
1693 | #endif | |
0bdaccee NC |
1694 | if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) { |
1695 | SP--; | |
1696 | SETs(boolSV(SvRV(TOPs) < SvRV(TOPp1s))); | |
1697 | RETURN; | |
1698 | } | |
30de85b6 | 1699 | #endif |
a0d0e21e LW |
1700 | { |
1701 | dPOPnv; | |
54310121 | 1702 | SETs(boolSV(TOPn < value)); |
a0d0e21e | 1703 | RETURN; |
79072805 | 1704 | } |
a0d0e21e | 1705 | } |
79072805 | 1706 | |
a0d0e21e LW |
1707 | PP(pp_gt) |
1708 | { | |
39644a26 | 1709 | dSP; tryAMAGICbinSET(gt,0); |
28e5dec8 JH |
1710 | #ifdef PERL_PRESERVE_IVUV |
1711 | SvIV_please(TOPs); | |
1712 | if (SvIOK(TOPs)) { | |
1713 | SvIV_please(TOPm1s); | |
1714 | if (SvIOK(TOPm1s)) { | |
1715 | bool auvok = SvUOK(TOPm1s); | |
1716 | bool buvok = SvUOK(TOPs); | |
a227d84d | 1717 | |
28e5dec8 | 1718 | if (!auvok && !buvok) { /* ## IV > IV ## */ |
1b6737cc AL |
1719 | const IV aiv = SvIVX(TOPm1s); |
1720 | const IV biv = SvIVX(TOPs); | |
1721 | ||
28e5dec8 JH |
1722 | SP--; |
1723 | SETs(boolSV(aiv > biv)); | |
1724 | RETURN; | |
1725 | } | |
1726 | if (auvok && buvok) { /* ## UV > UV ## */ | |
1b6737cc AL |
1727 | const UV auv = SvUVX(TOPm1s); |
1728 | const UV buv = SvUVX(TOPs); | |
28e5dec8 JH |
1729 | |
1730 | SP--; | |
1731 | SETs(boolSV(auv > buv)); | |
1732 | RETURN; | |
1733 | } | |
1734 | if (auvok) { /* ## UV > IV ## */ | |
1735 | UV auv; | |
1b6737cc AL |
1736 | const IV biv = SvIVX(TOPs); |
1737 | ||
28e5dec8 JH |
1738 | SP--; |
1739 | if (biv < 0) { | |
1740 | /* As (a) is a UV, it's >=0, so it must be > */ | |
1741 | SETs(&PL_sv_yes); | |
1742 | RETURN; | |
1743 | } | |
1744 | auv = SvUVX(TOPs); | |
28e5dec8 JH |
1745 | SETs(boolSV(auv > (UV)biv)); |
1746 | RETURN; | |
1747 | } | |
1748 | { /* ## IV > UV ## */ | |
1b6737cc | 1749 | const IV aiv = SvIVX(TOPm1s); |
28e5dec8 JH |
1750 | UV buv; |
1751 | ||
28e5dec8 JH |
1752 | if (aiv < 0) { |
1753 | /* As (b) is a UV, it's >=0, so it cannot be > */ | |
1754 | SP--; | |
1755 | SETs(&PL_sv_no); | |
1756 | RETURN; | |
1757 | } | |
1758 | buv = SvUVX(TOPs); | |
1759 | SP--; | |
28e5dec8 JH |
1760 | SETs(boolSV((UV)aiv > buv)); |
1761 | RETURN; | |
1762 | } | |
1763 | } | |
1764 | } | |
1765 | #endif | |
30de85b6 | 1766 | #ifndef NV_PRESERVES_UV |
50fb3111 NC |
1767 | #ifdef PERL_PRESERVE_IVUV |
1768 | else | |
1769 | #endif | |
0bdaccee | 1770 | if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) { |
30de85b6 NC |
1771 | SP--; |
1772 | SETs(boolSV(SvRV(TOPs) > SvRV(TOPp1s))); | |
1773 | RETURN; | |
1774 | } | |
1775 | #endif | |
a0d0e21e LW |
1776 | { |
1777 | dPOPnv; | |
54310121 | 1778 | SETs(boolSV(TOPn > value)); |
a0d0e21e | 1779 | RETURN; |
79072805 | 1780 | } |
a0d0e21e LW |
1781 | } |
1782 | ||
1783 | PP(pp_le) | |
1784 | { | |
39644a26 | 1785 | dSP; tryAMAGICbinSET(le,0); |
28e5dec8 JH |
1786 | #ifdef PERL_PRESERVE_IVUV |
1787 | SvIV_please(TOPs); | |
1788 | if (SvIOK(TOPs)) { | |
1789 | SvIV_please(TOPm1s); | |
1790 | if (SvIOK(TOPm1s)) { | |
1791 | bool auvok = SvUOK(TOPm1s); | |
1792 | bool buvok = SvUOK(TOPs); | |
a227d84d | 1793 | |
28e5dec8 | 1794 | if (!auvok && !buvok) { /* ## IV <= IV ## */ |
1b6737cc AL |
1795 | const IV aiv = SvIVX(TOPm1s); |
1796 | const IV biv = SvIVX(TOPs); | |
28e5dec8 JH |
1797 | |
1798 | SP--; | |
1799 | SETs(boolSV(aiv <= biv)); | |
1800 | RETURN; | |
1801 | } | |
1802 | if (auvok && buvok) { /* ## UV <= UV ## */ | |
1803 | UV auv = SvUVX(TOPm1s); | |
1804 | UV buv = SvUVX(TOPs); | |
1805 | ||
1806 | SP--; | |
1807 | SETs(boolSV(auv <= buv)); | |
1808 | RETURN; | |
1809 | } | |
1810 | if (auvok) { /* ## UV <= IV ## */ | |
1811 | UV auv; | |
1b6737cc AL |
1812 | const IV biv = SvIVX(TOPs); |
1813 | ||
28e5dec8 JH |
1814 | SP--; |
1815 | if (biv < 0) { | |
1816 | /* As (a) is a UV, it's >=0, so a cannot be <= */ | |
1817 | SETs(&PL_sv_no); | |
1818 | RETURN; | |
1819 | } | |
1820 | auv = SvUVX(TOPs); | |
28e5dec8 JH |
1821 | SETs(boolSV(auv <= (UV)biv)); |
1822 | RETURN; | |
1823 | } | |
1824 | { /* ## IV <= UV ## */ | |
1b6737cc | 1825 | const IV aiv = SvIVX(TOPm1s); |
28e5dec8 | 1826 | UV buv; |
1b6737cc | 1827 | |
28e5dec8 JH |
1828 | if (aiv < 0) { |
1829 | /* As (b) is a UV, it's >=0, so a must be <= */ | |
1830 | SP--; | |
1831 | SETs(&PL_sv_yes); | |
1832 | RETURN; | |
1833 | } | |
1834 | buv = SvUVX(TOPs); | |
1835 | SP--; | |
28e5dec8 JH |
1836 | SETs(boolSV((UV)aiv <= buv)); |
1837 | RETURN; | |
1838 | } | |
1839 | } | |
1840 | } | |
1841 | #endif | |
30de85b6 | 1842 | #ifndef NV_PRESERVES_UV |
50fb3111 NC |
1843 | #ifdef PERL_PRESERVE_IVUV |
1844 | else | |
1845 | #endif | |
0bdaccee | 1846 | if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) { |
30de85b6 NC |
1847 | SP--; |
1848 | SETs(boolSV(SvRV(TOPs) <= SvRV(TOPp1s))); | |
1849 | RETURN; | |
1850 | } | |
1851 | #endif | |
a0d0e21e LW |
1852 | { |
1853 | dPOPnv; | |
54310121 | 1854 | SETs(boolSV(TOPn <= value)); |
a0d0e21e | 1855 | RETURN; |
79072805 | 1856 | } |
a0d0e21e LW |
1857 | } |
1858 | ||
1859 | PP(pp_ge) | |
1860 | { | |
39644a26 | 1861 | dSP; tryAMAGICbinSET(ge,0); |
28e5dec8 JH |
1862 | #ifdef PERL_PRESERVE_IVUV |
1863 | SvIV_please(TOPs); | |
1864 | if (SvIOK(TOPs)) { | |
1865 | SvIV_please(TOPm1s); | |
1866 | if (SvIOK(TOPm1s)) { | |
1867 | bool auvok = SvUOK(TOPm1s); | |
1868 | bool buvok = SvUOK(TOPs); | |
a227d84d | 1869 | |
28e5dec8 | 1870 | if (!auvok && !buvok) { /* ## IV >= IV ## */ |
1b6737cc AL |
1871 | const IV aiv = SvIVX(TOPm1s); |
1872 | const IV biv = SvIVX(TOPs); | |
1873 | ||
28e5dec8 JH |
1874 | SP--; |
1875 | SETs(boolSV(aiv >= biv)); | |
1876 | RETURN; | |
1877 | } | |
1878 | if (auvok && buvok) { /* ## UV >= UV ## */ | |
1b6737cc AL |
1879 | const UV auv = SvUVX(TOPm1s); |
1880 | const UV buv = SvUVX(TOPs); | |
1881 | ||
28e5dec8 JH |
1882 | SP--; |
1883 | SETs(boolSV(auv >= buv)); | |
1884 | RETURN; | |
1885 | } | |
1886 | if (auvok) { /* ## UV >= IV ## */ | |
1887 | UV auv; | |
1b6737cc AL |
1888 | const IV biv = SvIVX(TOPs); |
1889 | ||
28e5dec8 JH |
1890 | SP--; |
1891 | if (biv < 0) { | |
1892 | /* As (a) is a UV, it's >=0, so it must be >= */ | |
1893 | SETs(&PL_sv_yes); | |
1894 | RETURN; | |
1895 | } | |
1896 | auv = SvUVX(TOPs); | |
28e5dec8 JH |
1897 | SETs(boolSV(auv >= (UV)biv)); |
1898 | RETURN; | |
1899 | } | |
1900 | { /* ## IV >= UV ## */ | |
1b6737cc | 1901 | const IV aiv = SvIVX(TOPm1s); |
28e5dec8 | 1902 | UV buv; |
1b6737cc | 1903 | |
28e5dec8 JH |
1904 | if (aiv < 0) { |
1905 | /* As (b) is a UV, it's >=0, so a cannot be >= */ | |
1906 | SP--; | |
1907 | SETs(&PL_sv_no); | |
1908 | RETURN; | |
1909 | } | |
1910 | buv = SvUVX(TOPs); | |
1911 | SP--; | |
28e5dec8 JH |
1912 | SETs(boolSV((UV)aiv >= buv)); |
1913 | RETURN; | |
1914 | } | |
1915 | } | |
1916 | } | |
1917 | #endif | |
30de85b6 | 1918 | #ifndef NV_PRESERVES_UV |
50fb3111 NC |
1919 | #ifdef PERL_PRESERVE_IVUV |
1920 | else | |
1921 | #endif | |
0bdaccee | 1922 | if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) { |
30de85b6 NC |
1923 | SP--; |
1924 | SETs(boolSV(SvRV(TOPs) >= SvRV(TOPp1s))); | |
1925 | RETURN; | |
1926 | } | |
1927 | #endif | |
a0d0e21e LW |
1928 | { |
1929 | dPOPnv; | |
54310121 | 1930 | SETs(boolSV(TOPn >= value)); |
a0d0e21e | 1931 | RETURN; |
79072805 | 1932 | } |
a0d0e21e | 1933 | } |
79072805 | 1934 | |
a0d0e21e LW |
1935 | PP(pp_ne) |
1936 | { | |
16303949 | 1937 | dSP; tryAMAGICbinSET(ne,0); |
3bb2c415 | 1938 | #ifndef NV_PRESERVES_UV |
0bdaccee | 1939 | if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) { |
e61d22ef NC |
1940 | SP--; |
1941 | SETs(boolSV(SvRV(TOPs) != SvRV(TOPp1s))); | |
3bb2c415 JH |
1942 | RETURN; |
1943 | } | |
1944 | #endif | |
28e5dec8 JH |
1945 | #ifdef PERL_PRESERVE_IVUV |
1946 | SvIV_please(TOPs); | |
1947 | if (SvIOK(TOPs)) { | |
1948 | SvIV_please(TOPm1s); | |
1949 | if (SvIOK(TOPm1s)) { | |
0bd48802 AL |
1950 | const bool auvok = SvUOK(TOPm1s); |
1951 | const bool buvok = SvUOK(TOPs); | |
a227d84d | 1952 | |
30de85b6 NC |
1953 | if (auvok == buvok) { /* ## IV == IV or UV == UV ## */ |
1954 | /* Casting IV to UV before comparison isn't going to matter | |
1955 | on 2s complement. On 1s complement or sign&magnitude | |
1956 | (if we have any of them) it could make negative zero | |
1957 | differ from normal zero. As I understand it. (Need to | |
1958 | check - is negative zero implementation defined behaviour | |
1959 | anyway?). NWC */ | |
1b6737cc AL |
1960 | const UV buv = SvUVX(POPs); |
1961 | const UV auv = SvUVX(TOPs); | |
1962 | ||
28e5dec8 JH |
1963 | SETs(boolSV(auv != buv)); |
1964 | RETURN; | |
1965 | } | |
1966 | { /* ## Mixed IV,UV ## */ | |
1967 | IV iv; | |
1968 | UV uv; | |
1969 | ||
1970 | /* != is commutative so swap if needed (save code) */ | |
1971 | if (auvok) { | |
1972 | /* swap. top of stack (b) is the iv */ | |
1973 | iv = SvIVX(TOPs); | |
1974 | SP--; | |
1975 | if (iv < 0) { | |
1976 | /* As (a) is a UV, it's >0, so it cannot be == */ | |
1977 | SETs(&PL_sv_yes); | |
1978 | RETURN; | |
1979 | } | |
1980 | uv = SvUVX(TOPs); | |
1981 | } else { | |
1982 | iv = SvIVX(TOPm1s); | |
1983 | SP--; | |
1984 | if (iv < 0) { | |
1985 | /* As (b) is a UV, it's >0, so it cannot be == */ | |
1986 | SETs(&PL_sv_yes); | |
1987 | RETURN; | |
1988 | } | |
1989 | uv = SvUVX(*(SP+1)); /* Do I want TOPp1s() ? */ | |
1990 | } | |
28e5dec8 JH |
1991 | SETs(boolSV((UV)iv != uv)); |
1992 | RETURN; | |
1993 | } | |
1994 | } | |
1995 | } | |
1996 | #endif | |
a0d0e21e LW |
1997 | { |
1998 | dPOPnv; | |
54310121 | 1999 | SETs(boolSV(TOPn != value)); |
a0d0e21e LW |
2000 | RETURN; |
2001 | } | |
79072805 LW |
2002 | } |
2003 | ||
a0d0e21e | 2004 | PP(pp_ncmp) |
79072805 | 2005 | { |
39644a26 | 2006 | dSP; dTARGET; tryAMAGICbin(ncmp,0); |
d8c7644e | 2007 | #ifndef NV_PRESERVES_UV |
0bdaccee | 2008 | if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) { |
0bd48802 AL |
2009 | const UV right = PTR2UV(SvRV(POPs)); |
2010 | const UV left = PTR2UV(SvRV(TOPs)); | |
e61d22ef | 2011 | SETi((left > right) - (left < right)); |
d8c7644e JH |
2012 | RETURN; |
2013 | } | |
2014 | #endif | |
28e5dec8 JH |
2015 | #ifdef PERL_PRESERVE_IVUV |
2016 | /* Fortunately it seems NaN isn't IOK */ | |
2017 | SvIV_please(TOPs); | |
2018 | if (SvIOK(TOPs)) { | |
2019 | SvIV_please(TOPm1s); | |
2020 | if (SvIOK(TOPm1s)) { | |
1b6737cc AL |
2021 | const bool leftuvok = SvUOK(TOPm1s); |
2022 | const bool rightuvok = SvUOK(TOPs); | |
28e5dec8 JH |
2023 | I32 value; |
2024 | if (!leftuvok && !rightuvok) { /* ## IV <=> IV ## */ | |
1b6737cc AL |
2025 | const IV leftiv = SvIVX(TOPm1s); |
2026 | const IV rightiv = SvIVX(TOPs); | |
28e5dec8 JH |
2027 | |
2028 | if (leftiv > rightiv) | |
2029 | value = 1; | |
2030 | else if (leftiv < rightiv) | |
2031 | value = -1; | |
2032 | else | |
2033 | value = 0; | |
2034 | } else if (leftuvok && rightuvok) { /* ## UV <=> UV ## */ | |
1b6737cc AL |
2035 | const UV leftuv = SvUVX(TOPm1s); |
2036 | const UV rightuv = SvUVX(TOPs); | |
28e5dec8 JH |
2037 | |
2038 | if (leftuv > rightuv) | |
2039 | value = 1; | |
2040 | else if (leftuv < rightuv) | |
2041 | value = -1; | |
2042 | else | |
2043 | value = 0; | |
2044 | } else if (leftuvok) { /* ## UV <=> IV ## */ | |
1b6737cc | 2045 | const IV rightiv = SvIVX(TOPs); |
28e5dec8 JH |
2046 | if (rightiv < 0) { |
2047 | /* As (a) is a UV, it's >=0, so it cannot be < */ | |
2048 | value = 1; | |
2049 | } else { | |
1b6737cc | 2050 | const UV leftuv = SvUVX(TOPm1s); |
83bac5dd | 2051 | if (leftuv > (UV)rightiv) { |
28e5dec8 JH |
2052 | value = 1; |
2053 | } else if (leftuv < (UV)rightiv) { | |
2054 | value = -1; | |
2055 | } else { | |
2056 | value = 0; | |
2057 | } | |
2058 | } | |
2059 | } else { /* ## IV <=> UV ## */ | |
1b6737cc | 2060 | const IV leftiv = SvIVX(TOPm1s); |
28e5dec8 JH |
2061 | if (leftiv < 0) { |
2062 | /* As (b) is a UV, it's >=0, so it must be < */ | |
2063 | value = -1; | |
2064 | } else { | |
1b6737cc | 2065 | const UV rightuv = SvUVX(TOPs); |
83bac5dd | 2066 | if ((UV)leftiv > rightuv) { |
28e5dec8 | 2067 | value = 1; |
83bac5dd | 2068 | } else if ((UV)leftiv < rightuv) { |
28e5dec8 JH |
2069 | value = -1; |
2070 | } else { | |
2071 | value = 0; | |
2072 | } | |
2073 | } | |
2074 | } | |
2075 | SP--; | |
2076 | SETi(value); | |
2077 | RETURN; | |
2078 | } | |
2079 | } | |
2080 | #endif | |
a0d0e21e LW |
2081 | { |
2082 | dPOPTOPnnrl; | |
2083 | I32 value; | |
79072805 | 2084 | |
a3540c92 | 2085 | #ifdef Perl_isnan |
1ad04cfd JH |
2086 | if (Perl_isnan(left) || Perl_isnan(right)) { |
2087 | SETs(&PL_sv_undef); | |
2088 | RETURN; | |
2089 | } | |
2090 | value = (left > right) - (left < right); | |
2091 | #else | |
ff0cee69 | 2092 | if (left == right) |
a0d0e21e | 2093 | value = 0; |
a0d0e21e LW |
2094 | else if (left < right) |
2095 | value = -1; | |
44a8e56a | 2096 | else if (left > right) |
2097 | value = 1; | |
2098 | else { | |
3280af22 | 2099 | SETs(&PL_sv_undef); |
44a8e56a | 2100 | RETURN; |
2101 | } | |
1ad04cfd | 2102 | #endif |
a0d0e21e LW |
2103 | SETi(value); |
2104 | RETURN; | |
79072805 | 2105 | } |
a0d0e21e | 2106 | } |
79072805 | 2107 | |
afd9910b | 2108 | PP(pp_sle) |
a0d0e21e | 2109 | { |
afd9910b | 2110 | dSP; |
79072805 | 2111 | |
afd9910b NC |
2112 | int amg_type = sle_amg; |
2113 | int multiplier = 1; | |
2114 | int rhs = 1; | |
79072805 | 2115 | |
afd9910b NC |
2116 | switch (PL_op->op_type) { |
2117 | case OP_SLT: | |
2118 | amg_type = slt_amg; | |
2119 | /* cmp < 0 */ | |
2120 | rhs = 0; | |
2121 | break; | |
2122 | case OP_SGT: | |
2123 | amg_type = sgt_amg; | |
2124 | /* cmp > 0 */ | |
2125 | multiplier = -1; | |
2126 | rhs = 0; | |
2127 | break; | |
2128 | case OP_SGE: | |
2129 | amg_type = sge_amg; | |
2130 | /* cmp >= 0 */ | |
2131 | multiplier = -1; | |
2132 | break; | |
79072805 | 2133 | } |
79072805 | 2134 | |
afd9910b | 2135 | tryAMAGICbinSET_var(amg_type,0); |
a0d0e21e LW |
2136 | { |
2137 | dPOPTOPssrl; | |
1b6737cc | 2138 | const int cmp = (IN_LOCALE_RUNTIME |
bbce6d69 | 2139 | ? sv_cmp_locale(left, right) |
2140 | : sv_cmp(left, right)); | |
afd9910b | 2141 | SETs(boolSV(cmp * multiplier < rhs)); |
a0d0e21e LW |
2142 | RETURN; |
2143 | } | |
2144 | } | |
79072805 | 2145 | |
36477c24 | 2146 | PP(pp_seq) |
2147 | { | |
39644a26 | 2148 | dSP; tryAMAGICbinSET(seq,0); |
36477c24 | 2149 | { |
2150 | dPOPTOPssrl; | |
54310121 | 2151 | SETs(boolSV(sv_eq(left, right))); |
a0d0e21e LW |
2152 | RETURN; |
2153 | } | |
2154 | } | |
79072805 | 2155 | |
a0d0e21e | 2156 | PP(pp_sne) |
79072805 | 2157 | { |
39644a26 | 2158 | dSP; tryAMAGICbinSET(sne,0); |
a0d0e21e LW |
2159 | { |
2160 | dPOPTOPssrl; | |
54310121 | 2161 | SETs(boolSV(!sv_eq(left, right))); |
a0d0e21e | 2162 | RETURN; |
463ee0b2 | 2163 | } |
79072805 LW |
2164 | } |
2165 | ||
a0d0e21e | 2166 | PP(pp_scmp) |
79072805 | 2167 | { |
39644a26 | 2168 | dSP; dTARGET; tryAMAGICbin(scmp,0); |
a0d0e21e LW |
2169 | { |
2170 | dPOPTOPssrl; | |
1b6737cc | 2171 | const int cmp = (IN_LOCALE_RUNTIME |
bbce6d69 | 2172 | ? sv_cmp_locale(left, right) |
2173 | : sv_cmp(left, right)); | |
2174 | SETi( cmp ); | |
a0d0e21e LW |
2175 | RETURN; |
2176 | } | |
2177 | } | |
79072805 | 2178 | |
55497cff | 2179 | PP(pp_bit_and) |
2180 | { | |
39644a26 | 2181 | dSP; dATARGET; tryAMAGICbin(band,opASSIGN); |
a0d0e21e LW |
2182 | { |
2183 | dPOPTOPssrl; | |
5b295bef RD |
2184 | SvGETMAGIC(left); |
2185 | SvGETMAGIC(right); | |
4633a7c4 | 2186 | if (SvNIOKp(left) || SvNIOKp(right)) { |
d0ba1bd2 | 2187 | if (PL_op->op_private & HINT_INTEGER) { |
1b6737cc | 2188 | const IV i = SvIV_nomg(left) & SvIV_nomg(right); |
972b05a9 | 2189 | SETi(i); |
d0ba1bd2 JH |
2190 | } |
2191 | else { | |
1b6737cc | 2192 | const UV u = SvUV_nomg(left) & SvUV_nomg(right); |
972b05a9 | 2193 | SETu(u); |
d0ba1bd2 | 2194 | } |
a0d0e21e LW |
2195 | } |
2196 | else { | |
533c011a | 2197 | do_vop(PL_op->op_type, TARG, left, right); |
a0d0e21e LW |
2198 | SETTARG; |
2199 | } | |
2200 | RETURN; | |
2201 | } | |
2202 | } | |
79072805 | 2203 | |
a0d0e21e LW |
2204 | PP(pp_bit_xor) |
2205 | { | |
39644a26 | 2206 | dSP; dATARGET; tryAMAGICbin(bxor,opASSIGN); |
a0d0e21e LW |
2207 | { |
2208 | dPOPTOPssrl; | |
5b295bef RD |
2209 | SvGETMAGIC(left); |
2210 | SvGETMAGIC(right); | |
4633a7c4 | 2211 | if (SvNIOKp(left) || SvNIOKp(right)) { |
d0ba1bd2 | 2212 | if (PL_op->op_private & HINT_INTEGER) { |
1b6737cc | 2213 | const IV i = (USE_LEFT(left) ? SvIV_nomg(left) : 0) ^ SvIV_nomg(right); |
972b05a9 | 2214 | SETi(i); |
d0ba1bd2 JH |
2215 | } |
2216 | else { | |
1b6737cc | 2217 | const UV u = (USE_LEFT(left) ? SvUV_nomg(left) : 0) ^ SvUV_nomg(right); |
972b05a9 | 2218 | SETu(u); |
d0ba1bd2 | 2219 | } |
a0d0e21e LW |
2220 | } |
2221 | else { | |
533c011a | 2222 | do_vop(PL_op->op_type, TARG, left, right); |
a0d0e21e LW |
2223 | SETTARG; |
2224 | } | |
2225 | RETURN; | |
2226 | } | |
2227 | } | |
79072805 | 2228 | |
a0d0e21e LW |
2229 | PP(pp_bit_or) |
2230 | { | |
39644a26 | 2231 | dSP; dATARGET; tryAMAGICbin(bor,opASSIGN); |
a0d0e21e LW |
2232 | { |
2233 | dPOPTOPssrl; | |
5b295bef RD |
2234 | SvGETMAGIC(left); |
2235 | SvGETMAGIC(right); | |
4633a7c4 | 2236 | if (SvNIOKp(left) || SvNIOKp(right)) { |
d0ba1bd2 | 2237 | if (PL_op->op_private & HINT_INTEGER) { |
1b6737cc | 2238 | const IV i = (USE_LEFT(left) ? SvIV_nomg(left) : 0) | SvIV_nomg(right); |
972b05a9 | 2239 | SETi(i); |
d0ba1bd2 JH |
2240 | } |
2241 | else { | |
1b6737cc | 2242 | const UV u = (USE_LEFT(left) ? SvUV_nomg(left) : 0) | SvUV_nomg(right); |
972b05a9 | 2243 | SETu(u); |
d0ba1bd2 | 2244 | } |
a0d0e21e LW |
2245 | } |
2246 | else { | |
533c011a | 2247 | do_vop(PL_op->op_type, TARG, left, right); |
a0d0e21e LW |
2248 | SETTARG; |
2249 | } | |
2250 | RETURN; | |
79072805 | 2251 | } |
a0d0e21e | 2252 | } |
79072805 | 2253 | |
a0d0e21e LW |
2254 | PP(pp_negate) |
2255 | { | |
39644a26 | 2256 | dSP; dTARGET; tryAMAGICun(neg); |
a0d0e21e LW |
2257 | { |
2258 | dTOPss; | |
1b6737cc | 2259 | const int flags = SvFLAGS(sv); |
5b295bef | 2260 | SvGETMAGIC(sv); |
28e5dec8 JH |
2261 | if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) { |
2262 | /* It's publicly an integer, or privately an integer-not-float */ | |
2263 | oops_its_an_int: | |
9b0e499b GS |
2264 | if (SvIsUV(sv)) { |
2265 | if (SvIVX(sv) == IV_MIN) { | |
28e5dec8 | 2266 | /* 2s complement assumption. */ |
9b0e499b GS |
2267 | SETi(SvIVX(sv)); /* special case: -((UV)IV_MAX+1) == IV_MIN */ |
2268 | RETURN; | |
2269 | } | |
2270 | else if (SvUVX(sv) <= IV_MAX) { | |
beccb14c | 2271 | SETi(-SvIVX(sv)); |
9b0e499b GS |
2272 | RETURN; |
2273 | } | |
2274 | } | |
2275 | else if (SvIVX(sv) != IV_MIN) { | |
2276 | SETi(-SvIVX(sv)); | |
2277 | RETURN; | |
2278 | } | |
28e5dec8 JH |
2279 | #ifdef PERL_PRESERVE_IVUV |
2280 | else { | |
2281 | SETu((UV)IV_MIN); | |
2282 | RETURN; | |
2283 | } | |
2284 | #endif | |
9b0e499b GS |
2285 | } |
2286 | if (SvNIOKp(sv)) | |
a0d0e21e | 2287 | SETn(-SvNV(sv)); |
4633a7c4 | 2288 | else if (SvPOKp(sv)) { |
a0d0e21e | 2289 | STRLEN len; |
6f46942a | 2290 | const char *s = SvPV_const(sv, len); |
bbce6d69 | 2291 | if (isIDFIRST(*s)) { |
a0d0e21e LW |
2292 | sv_setpvn(TARG, "-", 1); |
2293 | sv_catsv(TARG, sv); | |
79072805 | 2294 | } |
a0d0e21e LW |
2295 | else if (*s == '+' || *s == '-') { |
2296 | sv_setsv(TARG, sv); | |
2297 | *SvPV_force(TARG, len) = *s == '-' ? '+' : '-'; | |
79072805 | 2298 | } |
8eb28a70 JH |
2299 | else if (DO_UTF8(sv)) { |
2300 | SvIV_please(sv); | |
2301 | if (SvIOK(sv)) | |
2302 | goto oops_its_an_int; | |
2303 | if (SvNOK(sv)) | |
2304 | sv_setnv(TARG, -SvNV(sv)); | |
2305 | else { | |
2306 | sv_setpvn(TARG, "-", 1); | |
2307 | sv_catsv(TARG, sv); | |
2308 | } | |
834a4ddd | 2309 | } |
28e5dec8 | 2310 | else { |
8eb28a70 JH |
2311 | SvIV_please(sv); |
2312 | if (SvIOK(sv)) | |
2313 | goto oops_its_an_int; | |
2314 | sv_setnv(TARG, -SvNV(sv)); | |
28e5dec8 | 2315 | } |
a0d0e21e | 2316 | SETTARG; |
79072805 | 2317 | } |
4633a7c4 LW |
2318 | else |
2319 | SETn(-SvNV(sv)); | |
79072805 | 2320 | } |
a0d0e21e | 2321 | RETURN; |
79072805 LW |
2322 | } |
2323 | ||
a0d0e21e | 2324 | PP(pp_not) |
79072805 | 2325 | { |
39644a26 | 2326 | dSP; tryAMAGICunSET(not); |
3280af22 | 2327 | *PL_stack_sp = boolSV(!SvTRUE(*PL_stack_sp)); |
a0d0e21e | 2328 | return NORMAL; |
79072805 LW |
2329 | } |
2330 | ||
a0d0e21e | 2331 | PP(pp_complement) |
79072805 | 2332 | { |
39644a26 | 2333 | dSP; dTARGET; tryAMAGICun(compl); |
a0d0e21e LW |
2334 | { |
2335 | dTOPss; | |
5b295bef | 2336 | SvGETMAGIC(sv); |
4633a7c4 | 2337 | if (SvNIOKp(sv)) { |
d0ba1bd2 | 2338 | if (PL_op->op_private & HINT_INTEGER) { |
1b6737cc | 2339 | const IV i = ~SvIV_nomg(sv); |
972b05a9 | 2340 | SETi(i); |
d0ba1bd2 JH |
2341 | } |
2342 | else { | |
1b6737cc | 2343 | const UV u = ~SvUV_nomg(sv); |
972b05a9 | 2344 | SETu(u); |
d0ba1bd2 | 2345 | } |
a0d0e21e LW |
2346 | } |
2347 | else { | |
51723571 | 2348 | register U8 *tmps; |
55497cff | 2349 | register I32 anum; |
a0d0e21e LW |
2350 | STRLEN len; |
2351 | ||
10516c54 | 2352 | (void)SvPV_nomg_const(sv,len); /* force check for uninit var */ |
891f9566 | 2353 | sv_setsv_nomg(TARG, sv); |
51723571 | 2354 | tmps = (U8*)SvPV_force(TARG, len); |
a0d0e21e | 2355 | anum = len; |
1d68d6cd | 2356 | if (SvUTF8(TARG)) { |
a1ca4561 | 2357 | /* Calculate exact length, let's not estimate. */ |
1d68d6cd SC |
2358 | STRLEN targlen = 0; |
2359 | U8 *result; | |
51723571 | 2360 | U8 *send; |
ba210ebe | 2361 | STRLEN l; |
a1ca4561 YST |
2362 | UV nchar = 0; |
2363 | UV nwide = 0; | |
1d68d6cd SC |
2364 | |
2365 | send = tmps + len; | |
2366 | while (tmps < send) { | |
1b6737cc | 2367 | const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, UTF8_ALLOW_ANYUV); |
1d68d6cd | 2368 | tmps += UTF8SKIP(tmps); |
5bbb0b5a | 2369 | targlen += UNISKIP(~c); |
a1ca4561 YST |
2370 | nchar++; |
2371 | if (c > 0xff) | |
2372 | nwide++; | |
1d68d6cd SC |
2373 | } |
2374 | ||
2375 | /* Now rewind strings and write them. */ | |
2376 | tmps -= len; | |
a1ca4561 YST |
2377 | |
2378 | if (nwide) { | |
a02a5408 | 2379 | Newxz(result, targlen + 1, U8); |
a1ca4561 | 2380 | while (tmps < send) { |
1b6737cc | 2381 | const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, UTF8_ALLOW_ANYUV); |
a1ca4561 | 2382 | tmps += UTF8SKIP(tmps); |
b851fbc1 | 2383 | result = uvchr_to_utf8_flags(result, ~c, UNICODE_ALLOW_ANY); |
a1ca4561 YST |
2384 | } |
2385 | *result = '\0'; | |
2386 | result -= targlen; | |
2387 | sv_setpvn(TARG, (char*)result, targlen); | |
2388 | SvUTF8_on(TARG); | |
2389 | } | |
2390 | else { | |
a02a5408 | 2391 | Newxz(result, nchar + 1, U8); |
a1ca4561 | 2392 | while (tmps < send) { |
1b6737cc | 2393 | const U8 c = (U8)utf8n_to_uvchr(tmps, 0, &l, UTF8_ALLOW_ANY); |
a1ca4561 YST |
2394 | tmps += UTF8SKIP(tmps); |
2395 | *result++ = ~c; | |
2396 | } | |
2397 | *result = '\0'; | |
2398 | result -= nchar; | |
2399 | sv_setpvn(TARG, (char*)result, nchar); | |
d0a21e00 | 2400 | SvUTF8_off(TARG); |
1d68d6cd | 2401 | } |
1d68d6cd SC |
2402 | Safefree(result); |
2403 | SETs(TARG); | |
2404 | RETURN; | |
2405 | } | |
a0d0e21e | 2406 | #ifdef LIBERAL |
51723571 JH |
2407 | { |
2408 | register long *tmpl; | |
2409 | for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++) | |
2410 | *tmps = ~*tmps; | |
2411 | tmpl = (long*)tmps; | |
2412 | for ( ; anum >= sizeof(long); anum -= sizeof(long), tmpl++) | |
2413 | *tmpl = ~*tmpl; | |
2414 | tmps = (U8*)tmpl; | |
2415 | } | |
a0d0e21e LW |
2416 | #endif |
2417 | for ( ; anum > 0; anum--, tmps++) | |
2418 | *tmps = ~*tmps; | |
2419 | ||
2420 | SETs(TARG); | |
2421 | } | |
2422 | RETURN; | |
2423 | } | |
79072805 LW |
2424 | } |
2425 | ||
a0d0e21e LW |
2426 | /* integer versions of some of the above */ |
2427 | ||
a0d0e21e | 2428 | PP(pp_i_multiply) |
79072805 | 2429 | { |
39644a26 | 2430 | dSP; dATARGET; tryAMAGICbin(mult,opASSIGN); |
a0d0e21e LW |
2431 | { |
2432 | dPOPTOPiirl; | |
2433 | SETi( left * right ); | |
2434 | RETURN; | |
2435 | } | |
79072805 LW |
2436 | } |
2437 | ||
a0d0e21e | 2438 | PP(pp_i_divide) |
79072805 | 2439 | { |
39644a26 | 2440 | dSP; dATARGET; tryAMAGICbin(div,opASSIGN); |
a0d0e21e LW |
2441 | { |
2442 | dPOPiv; | |
2443 | if (value == 0) | |
cea2e8a9 | 2444 | DIE(aTHX_ "Illegal division by zero"); |
a0d0e21e LW |
2445 | value = POPi / value; |
2446 | PUSHi( value ); | |
2447 | RETURN; | |
2448 | } | |
79072805 LW |
2449 | } |
2450 | ||
224ec323 JH |
2451 | STATIC |
2452 | PP(pp_i_modulo_0) | |
2453 | { | |
2454 | /* This is the vanilla old i_modulo. */ | |
27da23d5 | 2455 | dVAR; dSP; dATARGET; tryAMAGICbin(modulo,opASSIGN); |
224ec323 JH |
2456 | { |
2457 | dPOPTOPiirl; | |
2458 | if (!right) | |
2459 | DIE(aTHX_ "Illegal modulus zero"); | |
2460 | SETi( left % right ); | |
2461 | RETURN; | |
2462 | } | |
2463 | } | |
2464 | ||
11010fa3 | 2465 | #if defined(__GLIBC__) && IVSIZE == 8 |
224ec323 JH |
2466 | STATIC |
2467 | PP(pp_i_modulo_1) | |
2468 | { | |
224ec323 | 2469 | /* This is the i_modulo with the workaround for the _moddi3 bug |
fce2b89e | 2470 | * in (at least) glibc 2.2.5 (the PERL_ABS() the workaround). |
224ec323 | 2471 | * See below for pp_i_modulo. */ |
27da23d5 | 2472 | dVAR; dSP; dATARGET; tryAMAGICbin(modulo,opASSIGN); |
224ec323 JH |
2473 | { |
2474 | dPOPTOPiirl; | |
2475 | if (!right) | |
2476 | DIE(aTHX_ "Illegal modulus zero"); | |
32fdb065 | 2477 | SETi( left % PERL_ABS(right) ); |
224ec323 JH |
2478 | RETURN; |
2479 | } | |
224ec323 | 2480 | } |
fce2b89e | 2481 | #endif |
224ec323 | 2482 | |
a0d0e21e | 2483 | PP(pp_i_modulo) |
79072805 | 2484 | { |
27da23d5 | 2485 | dVAR; dSP; dATARGET; tryAMAGICbin(modulo,opASSIGN); |
224ec323 JH |
2486 | { |
2487 | dPOPTOPiirl; | |
2488 | if (!right) | |
2489 | DIE(aTHX_ "Illegal modulus zero"); | |
2490 | /* The assumption is to use hereafter the old vanilla version... */ | |
2491 | PL_op->op_ppaddr = | |
2492 | PL_ppaddr[OP_I_MODULO] = | |
1c127fab | 2493 | Perl_pp_i_modulo_0; |
224ec323 JH |
2494 | /* .. but if we have glibc, we might have a buggy _moddi3 |
2495 | * (at least glicb 2.2.5 is known to have this bug), in other | |
2496 | * words our integer modulus with negative quad as the second | |
2497 | * argument might be broken. Test for this and re-patch the | |
2498 | * opcode dispatch table if that is the case, remembering to | |
2499 | * also apply the workaround so that this first round works | |
2500 | * right, too. See [perl #9402] for more information. */ | |
2501 | #if defined(__GLIBC__) && IVSIZE == 8 | |
2502 | { | |
2503 | IV l = 3; | |
2504 | IV r = -10; | |
2505 | /* Cannot do this check with inlined IV constants since | |
2506 | * that seems to work correctly even with the buggy glibc. */ | |
2507 | if (l % r == -3) { | |
2508 | /* Yikes, we have the bug. | |
2509 | * Patch in the workaround version. */ | |
2510 | PL_op->op_ppaddr = | |
2511 | PL_ppaddr[OP_I_MODULO] = | |
2512 | &Perl_pp_i_modulo_1; | |
2513 | /* Make certain we work right this time, too. */ | |
32fdb065 | 2514 | right = PERL_ABS(right); |
224ec323 JH |
2515 | } |
2516 | } | |
2517 | #endif | |
2518 | SETi( left % right ); | |
2519 | RETURN; | |
2520 | } | |
79072805 LW |
2521 | } |
2522 | ||
a0d0e21e | 2523 | PP(pp_i_add) |
79072805 | 2524 | { |
39644a26 | 2525 | dSP; dATARGET; tryAMAGICbin(add,opASSIGN); |
a0d0e21e | 2526 | { |
5e66d4f1 | 2527 | dPOPTOPiirl_ul; |
a0d0e21e LW |
2528 | SETi( left + right ); |
2529 | RETURN; | |
79072805 | 2530 | } |
79072805 LW |
2531 | } |
2532 | ||
a0d0e21e | 2533 | PP(pp_i_subtract) |
79072805 | 2534 | { |
39644a26 | 2535 | dSP; dATARGET; tryAMAGICbin(subtr,opASSIGN); |
a0d0e21e | 2536 | { |
5e66d4f1 | 2537 | dPOPTOPiirl_ul; |
a0d0e21e LW |
2538 | SETi( left - right ); |
2539 | RETURN; | |
79072805 | 2540 | } |
79072805 LW |
2541 | } |
2542 | ||
a0d0e21e | 2543 | PP(pp_i_lt) |
79072805 | 2544 | { |
39644a26 | 2545 | dSP; tryAMAGICbinSET(lt,0); |
a0d0e21e LW |
2546 | { |
2547 | dPOPTOPiirl; | |
54310121 | 2548 | SETs(boolSV(left < right)); |
a0d0e21e LW |
2549 | RETURN; |
2550 | } | |
79072805 LW |
2551 | } |
2552 | ||
a0d0e21e | 2553 | PP(pp_i_gt) |
79072805 | 2554 | { |
39644a26 | 2555 | dSP; tryAMAGICbinSET(gt,0); |
a0d0e21e LW |
2556 | { |
2557 | dPOPTOPiirl; | |
54310121 | 2558 | SETs(boolSV(left > right)); |
a0d0e21e LW |
2559 | RETURN; |
2560 | } | |
79072805 LW |
2561 | } |
2562 | ||
a0d0e21e | 2563 | PP(pp_i_le) |
79072805 | 2564 | { |
39644a26 | 2565 | dSP; tryAMAGICbinSET(le,0); |
a0d0e21e LW |
2566 | { |
2567 | dPOPTOPiirl; | |
54310121 | 2568 | SETs(boolSV(left <= right)); |
a0d0e21e | 2569 | RETURN; |
85e6fe83 | 2570 | } |
79072805 LW |
2571 | } |
2572 | ||
a0d0e21e | 2573 | PP(pp_i_ge) |
79072805 | 2574 | { |
39644a26 | 2575 | dSP; tryAMAGICbinSET(ge,0); |
a0d0e21e LW |
2576 | { |
2577 | dPOPTOPiirl; | |
54310121 | 2578 | SETs(boolSV(left >= right)); |
a0d0e21e LW |
2579 | RETURN; |
2580 | } | |
79072805 LW |
2581 | } |
2582 | ||
a0d0e21e | 2583 | PP(pp_i_eq) |
79072805 | 2584 | { |
39644a26 | 2585 | dSP; tryAMAGICbinSET(eq,0); |
a0d0e21e LW |
2586 | { |
2587 | dPOPTOPiirl; | |
54310121 | 2588 | SETs(boolSV(left == right)); |
a0d0e21e LW |
2589 | RETURN; |
2590 | } | |
79072805 LW |
2591 | } |
2592 | ||
a0d0e21e | 2593 | PP(pp_i_ne) |
79072805 | 2594 | { |
39644a26 | 2595 | dSP; tryAMAGICbinSET(ne,0); |
a0d0e21e LW |
2596 | { |
2597 | dPOPTOPiirl; | |
54310121 | 2598 | SETs(boolSV(left != right)); |
a0d0e21e LW |
2599 | RETURN; |
2600 | } | |
79072805 LW |
2601 | } |
2602 | ||
a0d0e21e | 2603 | PP(pp_i_ncmp) |
79072805 | 2604 | { |
39644a26 | 2605 | dSP; dTARGET; tryAMAGICbin(ncmp,0); |
a0d0e21e LW |
2606 | { |
2607 | dPOPTOPiirl; | |
2608 | I32 value; | |
79072805 | 2609 | |
a0d0e21e | 2610 | if (left > right) |
79072805 | 2611 | value = 1; |
a0d0e21e | 2612 | else if (left < right) |
79072805 | 2613 | value = -1; |
a0d0e21e | 2614 | else |
79072805 | 2615 | value = 0; |
a0d0e21e LW |
2616 | SETi(value); |
2617 | RETURN; | |
79072805 | 2618 | } |
85e6fe83 LW |
2619 | } |
2620 | ||
2621 | PP(pp_i_negate) | |
2622 | { | |
39644a26 | 2623 | dSP; dTARGET; tryAMAGICun(neg); |
85e6fe83 LW |
2624 | SETi(-TOPi); |
2625 | RETURN; | |
2626 | } | |
2627 | ||
79072805 LW |
2628 | /* High falutin' math. */ |
2629 | ||
2630 | PP(pp_atan2) | |
2631 | { | |
39644a26 | 2632 | dSP; dTARGET; tryAMAGICbin(atan2,0); |
a0d0e21e LW |
2633 | { |
2634 | dPOPTOPnnrl; | |
65202027 | 2635 | SETn(Perl_atan2(left, right)); |
a0d0e21e LW |
2636 | RETURN; |
2637 | } | |
79072805 LW |
2638 | } |
2639 | ||
2640 | PP(pp_sin) | |
2641 | { | |
39644a26 | 2642 | dSP; dTARGET; tryAMAGICun(sin); |
a0d0e21e | 2643 | { |
1b6737cc AL |
2644 | const NV value = POPn; |
2645 | XPUSHn(Perl_sin(value)); | |
a0d0e21e LW |
2646 | RETURN; |
2647 | } | |
79072805 LW |
2648 | } |
2649 | ||
2650 | PP(pp_cos) | |
2651 | { | |
39644a26 | 2652 | dSP; dTARGET; tryAMAGICun(cos); |
a0d0e21e | 2653 | { |
1b6737cc AL |
2654 | const NV value = POPn; |
2655 | XPUSHn(Perl_cos(value)); | |
a0d0e21e LW |
2656 | RETURN; |
2657 | } | |
79072805 LW |
2658 | } |
2659 | ||
56cb0a1c AD |
2660 | /* Support Configure command-line overrides for rand() functions. |
2661 | After 5.005, perhaps we should replace this by Configure support | |
2662 | for drand48(), random(), or rand(). For 5.005, though, maintain | |
2663 | compatibility by calling rand() but allow the user to override it. | |
2664 | See INSTALL for details. --Andy Dougherty 15 July 1998 | |
2665 | */ | |
85ab1d1d JH |
2666 | /* Now it's after 5.005, and Configure supports drand48() and random(), |
2667 | in addition to rand(). So the overrides should not be needed any more. | |
2668 | --Jarkko Hietaniemi 27 September 1998 | |
2669 | */ | |
2670 | ||
2671 | #ifndef HAS_DRAND48_PROTO | |
20ce7b12 | 2672 | extern double drand48 (void); |
56cb0a1c AD |
2673 | #endif |
2674 | ||
79072805 LW |
2675 | PP(pp_rand) |
2676 | { | |
39644a26 | 2677 | dSP; dTARGET; |
65202027 | 2678 | NV value; |
79072805 LW |
2679 | if (MAXARG < 1) |
2680 | value = 1.0; | |
2681 | else | |
2682 | value = POPn; | |
2683 | if (value == 0.0) | |
2684 | value = 1.0; | |
80252599 | 2685 | if (!PL_srand_called) { |
85ab1d1d | 2686 | (void)seedDrand01((Rand_seed_t)seed()); |
80252599 | 2687 | PL_srand_called = TRUE; |
93dc8474 | 2688 | } |
85ab1d1d | 2689 | value *= Drand01(); |
79072805 LW |
2690 | XPUSHn(value); |
2691 | RETURN; | |
2692 | } | |
2693 | ||
2694 | PP(pp_srand) | |
2695 | { | |
39644a26 | 2696 | dSP; |
0bd48802 | 2697 | const UV anum = (MAXARG < 1) ? seed() : POPu; |
85ab1d1d | 2698 | (void)seedDrand01((Rand_seed_t)anum); |
80252599 | 2699 | PL_srand_called = TRUE; |
79072805 LW |
2700 | EXTEND(SP, 1); |
2701 | RETPUSHYES; | |
2702 | } | |
2703 | ||
2704 | PP(pp_exp) | |
2705 | { | |
39644a26 | 2706 | dSP; dTARGET; tryAMAGICun(exp); |
a0d0e21e | 2707 | { |
65202027 | 2708 | NV value; |
a0d0e21e | 2709 | value = POPn; |
65202027 | 2710 | value = Perl_exp(value); |
a0d0e21e LW |
2711 | XPUSHn(value); |
2712 | RETURN; | |
2713 | } | |
79072805 LW |
2714 | } |
2715 | ||
2716 | PP(pp_log) | |
2717 | { | |
39644a26 | 2718 | dSP; dTARGET; tryAMAGICun(log); |
a0d0e21e | 2719 | { |
1b6737cc | 2720 | const NV value = POPn; |
bbce6d69 | 2721 | if (value <= 0.0) { |
f93f4e46 | 2722 | SET_NUMERIC_STANDARD(); |
1779d84d | 2723 | DIE(aTHX_ "Can't take log of %"NVgf, value); |
bbce6d69 | 2724 | } |
1b6737cc | 2725 | XPUSHn(Perl_log(value)); |
a0d0e21e LW |
2726 | RETURN; |
2727 | } | |
79072805 LW |
2728 | } |
2729 | ||
2730 | PP(pp_sqrt) | |
2731 | { | |
39644a26 | 2732 | dSP; dTARGET; tryAMAGICun(sqrt); |
a0d0e21e | 2733 | { |
1b6737cc | 2734 | const NV value = POPn; |
bbce6d69 | 2735 | if (value < 0.0) { |
f93f4e46 | 2736 | SET_NUMERIC_STANDARD(); |
1779d84d | 2737 | DIE(aTHX_ "Can't take sqrt of %"NVgf, value); |
bbce6d69 | 2738 | } |
1b6737cc | 2739 | XPUSHn(Perl_sqrt(value)); |
a0d0e21e LW |
2740 | RETURN; |
2741 | } | |
79072805 LW |
2742 | } |
2743 | ||
2744 | PP(pp_int) | |
2745 | { | |
39644a26 | 2746 | dSP; dTARGET; tryAMAGICun(int); |
774d564b | 2747 | { |
1b6737cc | 2748 | const IV iv = TOPi; /* attempt to convert to IV if possible. */ |
28e5dec8 JH |
2749 | /* XXX it's arguable that compiler casting to IV might be subtly |
2750 | different from modf (for numbers inside (IV_MIN,UV_MAX)) in which | |
2751 | else preferring IV has introduced a subtle behaviour change bug. OTOH | |
2752 | relying on floating point to be accurate is a bug. */ | |
2753 | ||
922c4365 MHM |
2754 | if (!SvOK(TOPs)) |
2755 | SETu(0); | |
2756 | else if (SvIOK(TOPs)) { | |
28e5dec8 | 2757 | if (SvIsUV(TOPs)) { |
1b6737cc | 2758 | const UV uv = TOPu; |
28e5dec8 JH |
2759 | SETu(uv); |
2760 | } else | |
2761 | SETi(iv); | |
2762 | } else { | |
1b6737cc | 2763 | const NV value = TOPn; |
1048ea30 | 2764 | if (value >= 0.0) { |
28e5dec8 JH |
2765 | if (value < (NV)UV_MAX + 0.5) { |
2766 | SETu(U_V(value)); | |
2767 | } else { | |
059a1014 | 2768 | SETn(Perl_floor(value)); |
28e5dec8 | 2769 | } |
1048ea30 | 2770 | } |
28e5dec8 JH |
2771 | else { |
2772 | if (value > (NV)IV_MIN - 0.5) { | |
2773 | SETi(I_V(value)); | |
2774 | } else { | |
1bbae031 | 2775 | SETn(Perl_ceil(value)); |
28e5dec8 JH |
2776 | } |
2777 | } | |
774d564b | 2778 | } |
79072805 | 2779 | } |
79072805 LW |
2780 | RETURN; |
2781 | } | |
2782 | ||
463ee0b2 LW |
2783 | PP(pp_abs) |
2784 | { | |
39644a26 | 2785 | dSP; dTARGET; tryAMAGICun(abs); |
a0d0e21e | 2786 | { |
28e5dec8 | 2787 | /* This will cache the NV value if string isn't actually integer */ |
1b6737cc | 2788 | const IV iv = TOPi; |
a227d84d | 2789 | |
922c4365 MHM |
2790 | if (!SvOK(TOPs)) |
2791 | SETu(0); | |
2792 | else if (SvIOK(TOPs)) { | |
28e5dec8 JH |
2793 | /* IVX is precise */ |
2794 | if (SvIsUV(TOPs)) { | |
2795 | SETu(TOPu); /* force it to be numeric only */ | |
2796 | } else { | |
2797 | if (iv >= 0) { | |
2798 | SETi(iv); | |
2799 | } else { | |
2800 | if (iv != IV_MIN) { | |
2801 | SETi(-iv); | |
2802 | } else { | |
2803 | /* 2s complement assumption. Also, not really needed as | |
2804 | IV_MIN and -IV_MIN should both be %100...00 and NV-able */ | |
2805 | SETu(IV_MIN); | |
2806 | } | |
a227d84d | 2807 | } |
28e5dec8 JH |
2808 | } |
2809 | } else{ | |
1b6737cc | 2810 | const NV value = TOPn; |
774d564b | 2811 | if (value < 0.0) |
1b6737cc | 2812 | SETn(-value); |
a4474c9e DD |
2813 | else |
2814 | SETn(value); | |
774d564b | 2815 | } |
a0d0e21e | 2816 | } |
774d564b | 2817 | RETURN; |
463ee0b2 LW |
2818 | } |
2819 | ||
53305cf1 | 2820 | |
79072805 LW |
2821 | PP(pp_hex) |
2822 | { | |
39644a26 | 2823 | dSP; dTARGET; |
5c144d81 | 2824 | const char *tmps; |
53305cf1 | 2825 | I32 flags = PERL_SCAN_ALLOW_UNDERSCORES; |
6f894ead | 2826 | STRLEN len; |
53305cf1 NC |
2827 | NV result_nv; |
2828 | UV result_uv; | |
1b6737cc | 2829 | SV* const sv = POPs; |
79072805 | 2830 | |
349d4f2f | 2831 | tmps = (SvPV_const(sv, len)); |
2bc69dc4 NIS |
2832 | if (DO_UTF8(sv)) { |
2833 | /* If Unicode, try to downgrade | |
2834 | * If not possible, croak. */ | |
1b6737cc | 2835 | SV* const tsv = sv_2mortal(newSVsv(sv)); |
2bc69dc4 NIS |
2836 | |
2837 | SvUTF8_on(tsv); | |
2838 | sv_utf8_downgrade(tsv, FALSE); | |
349d4f2f | 2839 | tmps = SvPV_const(tsv, len); |
2bc69dc4 | 2840 | } |
53305cf1 NC |
2841 | result_uv = grok_hex (tmps, &len, &flags, &result_nv); |
2842 | if (flags & PERL_SCAN_GREATER_THAN_UV_MAX) { | |
2843 | XPUSHn(result_nv); | |
2844 | } | |
2845 | else { | |
2846 | XPUSHu(result_uv); | |
2847 | } | |
79072805 LW |
2848 | RETURN; |
2849 | } | |
2850 | ||
2851 | PP(pp_oct) | |
2852 | { | |
39644a26 | 2853 | dSP; dTARGET; |
5c144d81 | 2854 | const char *tmps; |
53305cf1 | 2855 | I32 flags = PERL_SCAN_ALLOW_UNDERSCORES; |
6f894ead | 2856 | STRLEN len; |
53305cf1 NC |
2857 | NV result_nv; |
2858 | UV result_uv; | |
1b6737cc | 2859 | SV* const sv = POPs; |
79072805 | 2860 | |
349d4f2f | 2861 | tmps = (SvPV_const(sv, len)); |
2bc69dc4 NIS |
2862 | if (DO_UTF8(sv)) { |
2863 | /* If Unicode, try to downgrade | |
2864 | * If not possible, croak. */ | |
1b6737cc | 2865 | SV* const tsv = sv_2mortal(newSVsv(sv)); |
2bc69dc4 NIS |
2866 | |
2867 | SvUTF8_on(tsv); | |
2868 | sv_utf8_downgrade(tsv, FALSE); | |
349d4f2f | 2869 | tmps = SvPV_const(tsv, len); |
2bc69dc4 | 2870 | } |
6f894ead | 2871 | while (*tmps && len && isSPACE(*tmps)) |
53305cf1 | 2872 | tmps++, len--; |
9e24b6e2 | 2873 | if (*tmps == '0') |
53305cf1 | 2874 | tmps++, len--; |
9e24b6e2 | 2875 | if (*tmps == 'x') |
53305cf1 | 2876 | result_uv = grok_hex (tmps, &len, &flags, &result_nv); |
9e24b6e2 | 2877 | else if (*tmps == 'b') |
53305cf1 | 2878 | result_uv = grok_bin (tmps, &len, &flags, &result_nv); |
464e2e8a | 2879 | else |
53305cf1 NC |
2880 | result_uv = grok_oct (tmps, &len, &flags, &result_nv); |
2881 | ||
2882 | if (flags & PERL_SCAN_GREATER_THAN_UV_MAX) { | |
2883 | XPUSHn(result_nv); | |
2884 | } | |
2885 | else { | |
2886 | XPUSHu(result_uv); | |
2887 | } | |
79072805 LW |
2888 | RETURN; |
2889 | } | |
2890 | ||
2891 | /* String stuff. */ | |
2892 | ||
2893 | PP(pp_length) | |
2894 | { | |
39644a26 | 2895 | dSP; dTARGET; |
0bd48802 | 2896 | SV * const sv = TOPs; |
a0ed51b3 | 2897 | |
7e2040f0 GS |
2898 | if (DO_UTF8(sv)) |
2899 | SETi(sv_len_utf8(sv)); | |
2900 | else | |
2901 | SETi(sv_len(sv)); | |
79072805 LW |
2902 | RETURN; |
2903 | } | |
2904 | ||
2905 | PP(pp_substr) | |
2906 | { | |
39644a26 | 2907 | dSP; dTARGET; |
79072805 | 2908 | SV *sv; |
9c5ffd7c | 2909 | I32 len = 0; |
463ee0b2 | 2910 | STRLEN curlen; |
9402d6ed | 2911 | STRLEN utf8_curlen; |
79072805 LW |
2912 | I32 pos; |
2913 | I32 rem; | |
84902520 | 2914 | I32 fail; |
e1ec3a88 AL |
2915 | const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET; |
2916 | const char *tmps; | |
2917 | const I32 arybase = PL_curcop->cop_arybase; | |
9402d6ed | 2918 | SV *repl_sv = NULL; |
e1ec3a88 | 2919 | const char *repl = 0; |
7b8d334a | 2920 | STRLEN repl_len; |
1b6737cc | 2921 | const int num_args = PL_op->op_private & 7; |
13e30c65 | 2922 | bool repl_need_utf8_upgrade = FALSE; |
9402d6ed | 2923 | bool repl_is_utf8 = FALSE; |
79072805 | 2924 | |
20408e3c | 2925 | SvTAINTED_off(TARG); /* decontaminate */ |
7e2040f0 | 2926 | SvUTF8_off(TARG); /* decontaminate */ |
78f9721b SM |
2927 | if (num_args > 2) { |
2928 | if (num_args > 3) { | |
9402d6ed | 2929 | repl_sv = POPs; |
83003860 | 2930 | repl = SvPV_const(repl_sv, repl_len); |
9402d6ed | 2931 | repl_is_utf8 = DO_UTF8(repl_sv) && SvCUR(repl_sv); |
7b8d334a | 2932 | } |
79072805 | 2933 | len = POPi; |
5d82c453 | 2934 | } |
84902520 | 2935 | pos = POPi; |
79072805 | 2936 | sv = POPs; |
849ca7ee | 2937 | PUTBACK; |
9402d6ed JH |
2938 | if (repl_sv) { |
2939 | if (repl_is_utf8) { | |
2940 | if (!DO_UTF8(sv)) | |
2941 | sv_utf8_upgrade(sv); | |
2942 | } | |
13e30c65 JH |
2943 | else if (DO_UTF8(sv)) |
2944 | repl_need_utf8_upgrade = TRUE; | |
9402d6ed | 2945 | } |
5c144d81 | 2946 | tmps = SvPV_const(sv, curlen); |
7e2040f0 | 2947 | if (DO_UTF8(sv)) { |
9402d6ed JH |
2948 | utf8_curlen = sv_len_utf8(sv); |
2949 | if (utf8_curlen == curlen) | |
2950 | utf8_curlen = 0; | |
a0ed51b3 | 2951 | else |
9402d6ed | 2952 | curlen = utf8_curlen; |
a0ed51b3 | 2953 | } |
d1c2b58a | 2954 | else |
9402d6ed | 2955 | utf8_curlen = 0; |
a0ed51b3 | 2956 | |
84902520 TB |
2957 | if (pos >= arybase) { |
2958 | pos -= arybase; | |
2959 | rem = curlen-pos; | |
2960 | fail = rem; | |
78f9721b | 2961 | if (num_args > 2) { |
5d82c453 GA |
2962 | if (len < 0) { |
2963 | rem += len; | |
2964 | if (rem < 0) | |
2965 | rem = 0; | |
2966 | } | |
2967 | else if (rem > len) | |
2968 | rem = len; | |
2969 | } | |
68dc0745 | 2970 | } |
84902520 | 2971 | else { |
5d82c453 | 2972 | pos += curlen; |
78f9721b | 2973 | if (num_args < 3) |
5d82c453 GA |
2974 | rem = curlen; |
2975 | else if (len >= 0) { | |
2976 | rem = pos+len; | |
2977 | if (rem > (I32)curlen) | |
2978 | rem = curlen; | |
2979 | } | |
2980 | else { | |
2981 | rem = curlen+len; | |
2982 | if (rem < pos) | |
2983 | rem = pos; | |
2984 | } | |
2985 | if (pos < 0) | |
2986 | pos = 0; | |
2987 | fail = rem; | |
2988 | rem -= pos; | |
84902520 TB |
2989 | } |
2990 | if (fail < 0) { | |
e476b1b5 GS |
2991 | if (lvalue || repl) |
2992 | Perl_croak(aTHX_ "substr outside of string"); | |
2993 | if (ckWARN(WARN_SUBSTR)) | |
9014280d | 2994 | Perl_warner(aTHX_ packWARN(WARN_SUBSTR), "substr outside of string"); |
2304df62 AD |
2995 | RETPUSHUNDEF; |
2996 | } | |
79072805 | 2997 | else { |
1b6737cc AL |
2998 | const I32 upos = pos; |
2999 | const I32 urem = rem; | |
9402d6ed | 3000 | if (utf8_curlen) |
a0ed51b3 | 3001 | sv_pos_u2b(sv, &pos, &rem); |
79072805 | 3002 | tmps += pos; |
781e7547 DM |
3003 | /* we either return a PV or an LV. If the TARG hasn't been used |
3004 | * before, or is of that type, reuse it; otherwise use a mortal | |
3005 | * instead. Note that LVs can have an extended lifetime, so also | |
3006 | * dont reuse if refcount > 1 (bug #20933) */ | |
3007 | if (SvTYPE(TARG) > SVt_NULL) { | |
3008 | if ( (SvTYPE(TARG) == SVt_PVLV) | |
3009 | ? (!lvalue || SvREFCNT(TARG) > 1) | |
3010 | : lvalue) | |
3011 | { | |
3012 | TARG = sv_newmortal(); | |
3013 | } | |
3014 | } | |
3015 | ||
79072805 | 3016 | sv_setpvn(TARG, tmps, rem); |
12aa1545 | 3017 | #ifdef USE_LOCALE_COLLATE |
14befaf4 | 3018 | sv_unmagic(TARG, PERL_MAGIC_collxfrm); |
12aa1545 | 3019 | #endif |
9402d6ed | 3020 | if (utf8_curlen) |
7f66633b | 3021 | SvUTF8_on(TARG); |
f7928d6c | 3022 | if (repl) { |
13e30c65 JH |
3023 | SV* repl_sv_copy = NULL; |
3024 | ||
3025 | if (repl_need_utf8_upgrade) { | |
3026 | repl_sv_copy = newSVsv(repl_sv); | |
3027 | sv_utf8_upgrade(repl_sv_copy); | |
349d4f2f | 3028 | repl = SvPV_const(repl_sv_copy, repl_len); |
13e30c65 JH |
3029 | repl_is_utf8 = DO_UTF8(repl_sv_copy) && SvCUR(sv); |
3030 | } | |
c8faf1c5 | 3031 | sv_insert(sv, pos, rem, repl, repl_len); |
9402d6ed | 3032 | if (repl_is_utf8) |
f7928d6c | 3033 | SvUTF8_on(sv); |
9402d6ed JH |
3034 | if (repl_sv_copy) |
3035 | SvREFCNT_dec(repl_sv_copy); | |
f7928d6c | 3036 | } |
c8faf1c5 | 3037 | else if (lvalue) { /* it's an lvalue! */ |
dedeecda | 3038 | if (!SvGMAGICAL(sv)) { |
3039 | if (SvROK(sv)) { | |
13c5b33c | 3040 | SvPV_force_nolen(sv); |
599cee73 | 3041 | if (ckWARN(WARN_SUBSTR)) |
9014280d | 3042 | Perl_warner(aTHX_ packWARN(WARN_SUBSTR), |
599cee73 | 3043 | "Attempt to use reference as lvalue in substr"); |
dedeecda | 3044 | } |
3045 | if (SvOK(sv)) /* is it defined ? */ | |
7f66633b | 3046 | (void)SvPOK_only_UTF8(sv); |
dedeecda | 3047 | else |
3048 | sv_setpvn(sv,"",0); /* avoid lexical reincarnation */ | |
3049 | } | |
5f05dabc | 3050 | |
a0d0e21e LW |
3051 | if (SvTYPE(TARG) < SVt_PVLV) { |
3052 | sv_upgrade(TARG, SVt_PVLV); | |
14befaf4 | 3053 | sv_magic(TARG, Nullsv, PERL_MAGIC_substr, Nullch, 0); |
ed6116ce | 3054 | } |
6214ab63 | 3055 | else |
0c34ef67 | 3056 | SvOK_off(TARG); |
a0d0e21e | 3057 | |
5f05dabc | 3058 | LvTYPE(TARG) = 'x'; |
6ff81951 GS |
3059 | if (LvTARG(TARG) != sv) { |
3060 | if (LvTARG(TARG)) | |
3061 | SvREFCNT_dec(LvTARG(TARG)); | |
3062 | LvTARG(TARG) = SvREFCNT_inc(sv); | |
3063 | } | |
9aa983d2 JH |
3064 | LvTARGOFF(TARG) = upos; |
3065 | LvTARGLEN(TARG) = urem; | |
79072805 LW |
3066 | } |
3067 | } | |
849ca7ee | 3068 | SPAGAIN; |
79072805 LW |
3069 | PUSHs(TARG); /* avoid SvSETMAGIC here */ |
3070 | RETURN; | |
3071 | } | |
3072 | ||
3073 | PP(pp_vec) | |
3074 | { | |
39644a26 | 3075 | dSP; dTARGET; |
1b6737cc AL |
3076 | register const IV size = POPi; |
3077 | register const IV offset = POPi; | |
3078 | register SV * const src = POPs; | |
3079 | const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET; | |
a0d0e21e | 3080 | |
81e118e0 JH |
3081 | SvTAINTED_off(TARG); /* decontaminate */ |
3082 | if (lvalue) { /* it's an lvalue! */ | |
24aef97f HS |
3083 | if (SvREFCNT(TARG) > 1) /* don't share the TARG (#20933) */ |
3084 | TARG = sv_newmortal(); | |
81e118e0 JH |
3085 | if (SvTYPE(TARG) < SVt_PVLV) { |
3086 | sv_upgrade(TARG, SVt_PVLV); | |
14befaf4 | 3087 | sv_magic(TARG, Nullsv, PERL_MAGIC_vec, Nullch, 0); |
79072805 | 3088 | } |
81e118e0 JH |
3089 | LvTYPE(TARG) = 'v'; |
3090 | if (LvTARG(TARG) != src) { | |
3091 | if (LvTARG(TARG)) | |
3092 | SvREFCNT_dec(LvTARG(TARG)); | |
3093 | LvTARG(TARG) = SvREFCNT_inc(src); | |
79072805 | 3094 | } |
81e118e0 JH |
3095 | LvTARGOFF(TARG) = offset; |
3096 | LvTARGLEN(TARG) = size; | |
79072805 LW |
3097 | } |
3098 | ||
81e118e0 | 3099 | sv_setuv(TARG, do_vecget(src, offset, size)); |
79072805 LW |
3100 | PUSHs(TARG); |
3101 | RETURN; | |
3102 | } | |
3103 | ||
3104 | PP(pp_index) | |
3105 | { | |
39644a26 | 3106 | dSP; dTARGET; |
79072805 LW |
3107 | SV *big; |
3108 | SV *little; | |
e609e586 | 3109 | SV *temp = Nullsv; |
79072805 LW |
3110 | I32 offset; |
3111 | I32 retval; | |
10516c54 NC |
3112 | const char *tmps; |
3113 | const char *tmps2; | |
463ee0b2 | 3114 | STRLEN biglen; |
1b6737cc | 3115 | const I32 arybase = PL_curcop->cop_arybase; |
e609e586 NC |
3116 | int big_utf8; |
3117 | int little_utf8; | |
79072805 LW |
3118 | |
3119 | if (MAXARG < 3) | |
3120 | offset = 0; | |
3121 | else | |
3122 | offset = POPi - arybase; | |
3123 | little = POPs; | |
3124 | big = POPs; | |
e609e586 NC |
3125 | big_utf8 = DO_UTF8(big); |
3126 | little_utf8 = DO_UTF8(little); | |
3127 | if (big_utf8 ^ little_utf8) { | |
3128 | /* One needs to be upgraded. */ | |
1b6737cc | 3129 | SV * const bytes = little_utf8 ? big : little; |
e609e586 | 3130 | STRLEN len; |
1b6737cc | 3131 | const char * const p = SvPV_const(bytes, len); |
e609e586 NC |
3132 | |
3133 | temp = newSVpvn(p, len); | |
3134 | ||
3135 | if (PL_encoding) { | |
3136 | sv_recode_to_utf8(temp, PL_encoding); | |
3137 | } else { | |
3138 | sv_utf8_upgrade(temp); | |
3139 | } | |
3140 | if (little_utf8) { | |
3141 | big = temp; | |
3142 | big_utf8 = TRUE; | |
3143 | } else { | |
3144 | little = temp; | |
3145 | } | |
3146 | } | |
3147 | if (big_utf8 && offset > 0) | |
a0ed51b3 | 3148 | sv_pos_u2b(big, &offset, 0); |
10516c54 | 3149 | tmps = SvPV_const(big, biglen); |
79072805 LW |
3150 | if (offset < 0) |
3151 | offset = 0; | |
eb160463 | 3152 | else if (offset > (I32)biglen) |
93a17b20 | 3153 | offset = biglen; |
79072805 | 3154 | if (!(tmps2 = fbm_instr((unsigned char*)tmps + offset, |
411d5715 | 3155 | (unsigned char*)tmps + biglen, little, 0))) |
a0ed51b3 | 3156 | retval = -1; |
79072805 | 3157 | else |
a0ed51b3 | 3158 | retval = tmps2 - tmps; |
e609e586 | 3159 | if (retval > 0 && big_utf8) |
a0ed51b3 | 3160 | sv_pos_b2u(big, &retval); |
e609e586 NC |
3161 | if (temp) |
3162 | SvREFCNT_dec(temp); | |
a0ed51b3 | 3163 | PUSHi(retval + arybase); |
79072805 LW |
3164 | RETURN; |
3165 | } | |
3166 | ||
3167 | PP(pp_rindex) | |
3168 | { | |
39644a26 | 3169 | dSP; dTARGET; |
79072805 LW |
3170 | SV *big; |
3171 | SV *little; | |
e609e586 | 3172 | SV *temp = Nullsv; |
463ee0b2 LW |
3173 | STRLEN blen; |
3174 | STRLEN llen; | |
79072805 LW |
3175 | I32 offset; |
3176 | I32 retval; | |
10516c54 NC |
3177 | const char *tmps; |
3178 | const char *tmps2; | |
1b6737cc | 3179 | const I32 arybase = PL_curcop->cop_arybase; |
e609e586 NC |
3180 | int big_utf8; |
3181 | int little_utf8; | |
79072805 | 3182 | |
a0d0e21e | 3183 | if (MAXARG >= 3) |
a0ed51b3 | 3184 | offset = POPi; |
79072805 LW |
3185 | little = POPs; |
3186 | big = POPs; | |
e609e586 NC |
3187 | big_utf8 = DO_UTF8(big); |
3188 | little_utf8 = DO_UTF8(little); | |
3189 | if (big_utf8 ^ little_utf8) { | |
3190 | /* One needs to be upgraded. */ | |
1b6737cc | 3191 | SV * const bytes = little_utf8 ? big : little; |
e609e586 | 3192 | STRLEN len; |
83003860 | 3193 | const char *p = SvPV_const(bytes, len); |
e609e586 NC |
3194 | |
3195 | temp = newSVpvn(p, len); | |
3196 | ||
3197 | if (PL_encoding) { | |
3198 | sv_recode_to_utf8(temp, PL_encoding); | |
3199 | } else { | |
3200 | sv_utf8_upgrade(temp); | |
3201 | } | |
3202 | if (little_utf8) { | |
3203 | big = temp; | |
3204 | big_utf8 = TRUE; | |
3205 | } else { | |
3206 | little = temp; | |
3207 | } | |
3208 | } | |
10516c54 NC |
3209 | tmps2 = SvPV_const(little, llen); |
3210 | tmps = SvPV_const(big, blen); | |
e609e586 | 3211 | |
79072805 | 3212 | if (MAXARG < 3) |
463ee0b2 | 3213 | offset = blen; |
a0ed51b3 | 3214 | else { |
e609e586 | 3215 | if (offset > 0 && big_utf8) |
a0ed51b3 LW |
3216 | sv_pos_u2b(big, &offset, 0); |
3217 | offset = offset - arybase + llen; | |
3218 | } | |
79072805 LW |
3219 | if (offset < 0) |
3220 | offset = 0; | |
eb160463 | 3221 | else if (offset > (I32)blen) |
463ee0b2 | 3222 | offset = blen; |
79072805 | 3223 | if (!(tmps2 = rninstr(tmps, tmps + offset, |
463ee0b2 | 3224 | tmps2, tmps2 + llen))) |
a0ed51b3 | 3225 | retval = -1; |
79072805 | 3226 | else |
a0ed51b3 | 3227 | retval = tmps2 - tmps; |
e609e586 | 3228 | if (retval > 0 && big_utf8) |
a0ed51b3 | 3229 | sv_pos_b2u(big, &retval); |
e609e586 NC |
3230 | if (temp) |
3231 | SvREFCNT_dec(temp); | |
a0ed51b3 | 3232 | PUSHi(retval + arybase); |
79072805 LW |
3233 | RETURN; |
3234 | } | |
3235 | ||
3236 | PP(pp_sprintf) | |
3237 | { | |
39644a26 | 3238 | dSP; dMARK; dORIGMARK; dTARGET; |
79072805 | 3239 | do_sprintf(TARG, SP-MARK, MARK+1); |
bbce6d69 | 3240 | TAINT_IF(SvTAINTED(TARG)); |
79072805 LW |
3241 | SP = ORIGMARK; |
3242 | PUSHTARG; | |
3243 | RETURN; | |
3244 | } | |
3245 | ||
79072805 LW |
3246 | PP(pp_ord) |
3247 | { | |
39644a26 | 3248 | dSP; dTARGET; |
7df053ec | 3249 | SV *argsv = POPs; |
ba210ebe | 3250 | STRLEN len; |
349d4f2f | 3251 | const U8 *s = (U8*)SvPV_const(argsv, len); |
121910a4 JH |
3252 | SV *tmpsv; |
3253 | ||
799ef3cb | 3254 | if (PL_encoding && SvPOK(argsv) && !DO_UTF8(argsv)) { |
121910a4 | 3255 | tmpsv = sv_2mortal(newSVsv(argsv)); |
799ef3cb | 3256 | s = (U8*)sv_recode_to_utf8(tmpsv, PL_encoding); |
121910a4 JH |
3257 | argsv = tmpsv; |
3258 | } | |
79072805 | 3259 | |
872c91ae | 3260 | XPUSHu(DO_UTF8(argsv) ? |
89ebb4a3 | 3261 | utf8n_to_uvchr(s, UTF8_MAXBYTES, 0, UTF8_ALLOW_ANYUV) : |
872c91ae | 3262 | (*s & 0xff)); |
68795e93 | 3263 | |
79072805 LW |
3264 | RETURN; |
3265 | } | |
3266 | ||
463ee0b2 LW |
3267 | PP(pp_chr) |
3268 | { | |
39644a26 | 3269 | dSP; dTARGET; |
463ee0b2 | 3270 | char *tmps; |
8a064bd6 JH |
3271 | UV value; |
3272 | ||
3273 | if (((SvIOK_notUV(TOPs) && SvIV(TOPs) < 0) | |
3274 | || | |
3275 | (SvNOK(TOPs) && SvNV(TOPs) < 0.0))) { | |
3276 | if (IN_BYTES) { | |
3277 | value = POPu; /* chr(-1) eq chr(0xff), etc. */ | |
3278 | } else { | |
3279 | (void) POPs; /* Ignore the argument value. */ | |
3280 | value = UNICODE_REPLACEMENT; | |
3281 | } | |
3282 | } else { | |
3283 | value = POPu; | |
3284 | } | |
463ee0b2 | 3285 | |
862a34c6 | 3286 | SvUPGRADE(TARG,SVt_PV); |
a0ed51b3 | 3287 | |
0064a8a9 | 3288 | if (value > 255 && !IN_BYTES) { |
eb160463 | 3289 | SvGROW(TARG, (STRLEN)UNISKIP(value)+1); |
62961d2e | 3290 | tmps = (char*)uvchr_to_utf8_flags((U8*)SvPVX(TARG), value, 0); |
349d4f2f | 3291 | SvCUR_set(TARG, tmps - SvPVX_const(TARG)); |
a0ed51b3 LW |
3292 | *tmps = '\0'; |
3293 | (void)SvPOK_only(TARG); | |
aa6ffa16 | 3294 | SvUTF8_on(TARG); |
a0ed51b3 LW |
3295 | XPUSHs(TARG); |
3296 | RETURN; | |
3297 | } | |
3298 | ||
748a9306 | 3299 | SvGROW(TARG,2); |
463ee0b2 LW |
3300 | SvCUR_set(TARG, 1); |
3301 | tmps = SvPVX(TARG); | |
eb160463 | 3302 | *tmps++ = (char)value; |
748a9306 | 3303 | *tmps = '\0'; |
a0d0e21e | 3304 | (void)SvPOK_only(TARG); |
88632417 | 3305 | if (PL_encoding && !IN_BYTES) { |
799ef3cb | 3306 | sv_recode_to_utf8(TARG, PL_encoding); |
88632417 JH |
3307 | tmps = SvPVX(TARG); |
3308 | if (SvCUR(TARG) == 0 || !is_utf8_string((U8*)tmps, SvCUR(TARG)) || | |
3309 | memEQ(tmps, "\xef\xbf\xbd\0", 4)) { | |
d5a15ac2 JH |
3310 | SvGROW(TARG, 3); |
3311 | tmps = SvPVX(TARG); | |
88632417 JH |
3312 | SvCUR_set(TARG, 2); |
3313 | *tmps++ = (U8)UTF8_EIGHT_BIT_HI(value); | |
3314 | *tmps++ = (U8)UTF8_EIGHT_BIT_LO(value); | |
3315 | *tmps = '\0'; | |
3316 | SvUTF8_on(TARG); | |
3317 | } | |
3318 | } | |
463ee0b2 LW |
3319 | XPUSHs(TARG); |
3320 | RETURN; | |
3321 | } | |
3322 | ||
79072805 LW |
3323 | PP(pp_crypt) |
3324 | { | |
79072805 | 3325 | #ifdef HAS_CRYPT |
27da23d5 | 3326 | dSP; dTARGET; |
5f74f29c | 3327 | dPOPTOPssrl; |
85c16d83 | 3328 | STRLEN len; |
10516c54 | 3329 | const char *tmps = SvPV_const(left, len); |
2bc69dc4 | 3330 | |
85c16d83 | 3331 | if (DO_UTF8(left)) { |
2bc69dc4 | 3332 | /* If Unicode, try to downgrade. |
f2791508 JH |
3333 | * If not possible, croak. |
3334 | * Yes, we made this up. */ | |
1b6737cc | 3335 | SV* const tsv = sv_2mortal(newSVsv(left)); |
2bc69dc4 | 3336 | |
f2791508 | 3337 | SvUTF8_on(tsv); |
2bc69dc4 | 3338 | sv_utf8_downgrade(tsv, FALSE); |
349d4f2f | 3339 | tmps = SvPV_const(tsv, len); |
85c16d83 | 3340 | } |
05404ffe JH |
3341 | # ifdef USE_ITHREADS |
3342 | # ifdef HAS_CRYPT_R | |
3343 | if (!PL_reentrant_buffer->_crypt_struct_buffer) { | |
3344 | /* This should be threadsafe because in ithreads there is only | |
3345 | * one thread per interpreter. If this would not be true, | |
3346 | * we would need a mutex to protect this malloc. */ | |
3347 | PL_reentrant_buffer->_crypt_struct_buffer = | |
3348 | (struct crypt_data *)safemalloc(sizeof(struct crypt_data)); | |
3349 | #if defined(__GLIBC__) || defined(__EMX__) | |
3350 | if (PL_reentrant_buffer->_crypt_struct_buffer) { | |
3351 | PL_reentrant_buffer->_crypt_struct_buffer->initialized = 0; | |
3352 | /* work around glibc-2.2.5 bug */ | |
3353 | PL_reentrant_buffer->_crypt_struct_buffer->current_saltbits = 0; | |
3354 | } | |
05404ffe | 3355 | #endif |
6ab58e4d | 3356 | } |
05404ffe JH |
3357 | # endif /* HAS_CRYPT_R */ |
3358 | # endif /* USE_ITHREADS */ | |
5f74f29c | 3359 | # ifdef FCRYPT |
83003860 | 3360 | sv_setpv(TARG, fcrypt(tmps, SvPV_nolen_const(right))); |
5f74f29c | 3361 | # else |
83003860 | 3362 | sv_setpv(TARG, PerlProc_crypt(tmps, SvPV_nolen_const(right))); |
5f74f29c | 3363 | # endif |
4808266b JH |
3364 | SETs(TARG); |
3365 | RETURN; | |
79072805 | 3366 | #else |
b13b2135 | 3367 | DIE(aTHX_ |
79072805 LW |
3368 | "The crypt() function is unimplemented due to excessive paranoia."); |
3369 | #endif | |
79072805 LW |
3370 | } |
3371 | ||
3372 | PP(pp_ucfirst) | |
3373 | { | |
39644a26 | 3374 | dSP; |
79072805 | 3375 | SV *sv = TOPs; |
83003860 | 3376 | const U8 *s; |
a0ed51b3 | 3377 | STRLEN slen; |
12e9c124 | 3378 | const int op_type = PL_op->op_type; |
a0ed51b3 | 3379 | |
d104a74c | 3380 | SvGETMAGIC(sv); |
3a2263fe | 3381 | if (DO_UTF8(sv) && |
83003860 | 3382 | (s = (const U8*)SvPV_nomg_const(sv, slen)) && slen && |
3a2263fe | 3383 | UTF8_IS_START(*s)) { |
89ebb4a3 | 3384 | U8 tmpbuf[UTF8_MAXBYTES_CASE+1]; |
44bc797b JH |
3385 | STRLEN ulen; |
3386 | STRLEN tculen; | |
a0ed51b3 | 3387 | |
44bc797b | 3388 | utf8_to_uvchr(s, &ulen); |
12e9c124 NC |
3389 | if (op_type == OP_UCFIRST) { |
3390 | toTITLE_utf8(s, tmpbuf, &tculen); | |
3391 | } else { | |
3392 | toLOWER_utf8(s, tmpbuf, &tculen); | |
3393 | } | |
44bc797b | 3394 | |
6f9b16a7 | 3395 | if (!SvPADTMP(sv) || SvREADONLY(sv) || ulen != tculen) { |
a0ed51b3 | 3396 | dTARGET; |
3a2263fe RGS |
3397 | /* slen is the byte length of the whole SV. |
3398 | * ulen is the byte length of the original Unicode character | |
3399 | * stored as UTF-8 at s. | |
12e9c124 NC |
3400 | * tculen is the byte length of the freshly titlecased (or |
3401 | * lowercased) Unicode character stored as UTF-8 at tmpbuf. | |
3402 | * We first set the result to be the titlecased (/lowercased) | |
3403 | * character, and then append the rest of the SV data. */ | |
44bc797b | 3404 | sv_setpvn(TARG, (char*)tmpbuf, tculen); |
3a2263fe RGS |
3405 | if (slen > ulen) |
3406 | sv_catpvn(TARG, (char*)(s + ulen), slen - ulen); | |
7e2040f0 | 3407 | SvUTF8_on(TARG); |
a0ed51b3 LW |
3408 | SETs(TARG); |
3409 | } | |
3410 | else { | |
d104a74c | 3411 | s = (U8*)SvPV_force_nomg(sv, slen); |
44bc797b | 3412 | Copy(tmpbuf, s, tculen, U8); |
a0ed51b3 | 3413 | } |
a0ed51b3 | 3414 | } |
626727d5 | 3415 | else { |
83003860 | 3416 | U8 *s1; |
014822e4 | 3417 | if (!SvPADTMP(sv) || SvREADONLY(sv)) { |
31351b04 | 3418 | dTARGET; |
7e2040f0 | 3419 | SvUTF8_off(TARG); /* decontaminate */ |
d104a74c | 3420 | sv_setsv_nomg(TARG, sv); |
31351b04 JS |
3421 | sv = TARG; |
3422 | SETs(sv); | |
3423 | } | |
83003860 NC |
3424 | s1 = (U8*)SvPV_force_nomg(sv, slen); |
3425 | if (*s1) { | |
2de3dbcc | 3426 | if (IN_LOCALE_RUNTIME) { |
31351b04 JS |
3427 | TAINT; |
3428 | SvTAINTED_on(sv); | |
12e9c124 NC |
3429 | *s1 = (op_type == OP_UCFIRST) |
3430 | ? toUPPER_LC(*s1) : toLOWER_LC(*s1); | |
31351b04 JS |
3431 | } |
3432 | else | |
12e9c124 | 3433 | *s1 = (op_type == OP_UCFIRST) ? toUPPER(*s1) : toLOWER(*s1); |
bbce6d69 | 3434 | } |
bbce6d69 | 3435 | } |
d104a74c | 3436 | SvSETMAGIC(sv); |
79072805 LW |
3437 | RETURN; |
3438 | } | |
3439 | ||
3440 | PP(pp_uc) | |
3441 | { | |
39644a26 | 3442 | dSP; |
79072805 | 3443 | SV *sv = TOPs; |
463ee0b2 | 3444 | STRLEN len; |
79072805 | 3445 | |
d104a74c | 3446 | SvGETMAGIC(sv); |
7e2040f0 | 3447 | if (DO_UTF8(sv)) { |
a0ed51b3 | 3448 | dTARGET; |
ba210ebe | 3449 | STRLEN ulen; |
a0ed51b3 | 3450 | register U8 *d; |
10516c54 NC |
3451 | const U8 *s; |
3452 | const U8 *send; | |
89ebb4a3 | 3453 | U8 tmpbuf[UTF8_MAXBYTES+1]; |
a0ed51b3 | 3454 | |
10516c54 | 3455 | s = (const U8*)SvPV_nomg_const(sv,len); |
a5a20234 | 3456 | if (!len) { |
7e2040f0 | 3457 | SvUTF8_off(TARG); /* decontaminate */ |
a5a20234 LW |
3458 | sv_setpvn(TARG, "", 0); |
3459 | SETs(TARG); | |
a0ed51b3 LW |
3460 | } |
3461 | else { | |
128c9517 JH |
3462 | STRLEN min = len + 1; |
3463 | ||
862a34c6 | 3464 | SvUPGRADE(TARG, SVt_PV); |
128c9517 | 3465 | SvGROW(TARG, min); |
31351b04 JS |
3466 | (void)SvPOK_only(TARG); |
3467 | d = (U8*)SvPVX(TARG); | |
3468 | send = s + len; | |
a2a2844f | 3469 | while (s < send) { |
89ebb4a3 JH |
3470 | STRLEN u = UTF8SKIP(s); |
3471 | ||
6fdb5f96 | 3472 | toUPPER_utf8(s, tmpbuf, &ulen); |
128c9517 JH |
3473 | if (ulen > u && (SvLEN(TARG) < (min += ulen - u))) { |
3474 | /* If the eventually required minimum size outgrows | |
3475 | * the available space, we need to grow. */ | |
0bd48802 | 3476 | const UV o = d - (U8*)SvPVX_const(TARG); |
89ebb4a3 JH |
3477 | |
3478 | /* If someone uppercases one million U+03B0s we | |
3479 | * SvGROW() one million times. Or we could try | |
32c480af JH |
3480 | * guessing how much to allocate without allocating |
3481 | * too much. Such is life. */ | |
128c9517 | 3482 | SvGROW(TARG, min); |
89ebb4a3 JH |
3483 | d = (U8*)SvPVX(TARG) + o; |
3484 | } | |
a2a2844f JH |
3485 | Copy(tmpbuf, d, ulen, U8); |
3486 | d += ulen; | |
89ebb4a3 | 3487 | s += u; |
a0ed51b3 | 3488 | } |
31351b04 | 3489 | *d = '\0'; |
7e2040f0 | 3490 | SvUTF8_on(TARG); |
349d4f2f | 3491 | SvCUR_set(TARG, d - (U8*)SvPVX_const(TARG)); |
31351b04 | 3492 | SETs(TARG); |
a0ed51b3 | 3493 | } |
a0ed51b3 | 3494 | } |
626727d5 | 3495 | else { |
10516c54 | 3496 | U8 *s; |
014822e4 | 3497 | if (!SvPADTMP(sv) || SvREADONLY(sv)) { |
31351b04 | 3498 | dTARGET; |
7e2040f0 | 3499 | SvUTF8_off(TARG); /* decontaminate */ |
d104a74c | 3500 | sv_setsv_nomg(TARG, sv); |
31351b04 JS |
3501 | sv = TARG; |
3502 | SETs(sv); | |
3503 | } | |
d104a74c | 3504 | s = (U8*)SvPV_force_nomg(sv, len); |
31351b04 | 3505 | if (len) { |
0d46e09a | 3506 | register const U8 *send = s + len; |
31351b04 | 3507 | |
2de3dbcc | 3508 | if (IN_LOCALE_RUNTIME) { |
31351b04 JS |
3509 | TAINT; |
3510 | SvTAINTED_on(sv); | |
3511 | for (; s < send; s++) | |
3512 | *s = toUPPER_LC(*s); | |
3513 | } | |
3514 | else { | |
3515 | for (; s < send; s++) | |
3516 | *s = toUPPER(*s); | |
3517 | } | |
bbce6d69 | 3518 | } |
79072805 | 3519 | } |
d104a74c | 3520 | SvSETMAGIC(sv); |
79072805 LW |
3521 | RETURN; |
3522 | } | |
3523 | ||
3524 | PP(pp_lc) | |
3525 | { | |
39644a26 | 3526 | dSP; |
79072805 | 3527 | SV *sv = TOPs; |
463ee0b2 | 3528 | STRLEN len; |
79072805 | 3529 | |
d104a74c | 3530 | SvGETMAGIC(sv); |
7e2040f0 | 3531 | if (DO_UTF8(sv)) { |
a0ed51b3 | 3532 | dTARGET; |
10516c54 | 3533 | const U8 *s; |
ba210ebe | 3534 | STRLEN ulen; |
a0ed51b3 | 3535 | register U8 *d; |
10516c54 | 3536 | const U8 *send; |
89ebb4a3 | 3537 | U8 tmpbuf[UTF8_MAXBYTES_CASE+1]; |
a0ed51b3 | 3538 | |
10516c54 | 3539 | s = (const U8*)SvPV_nomg_const(sv,len); |
a5a20234 | 3540 | if (!len) { |
7e2040f0 | 3541 | SvUTF8_off(TARG); /* decontaminate */ |
a5a20234 LW |
3542 | sv_setpvn(TARG, "", 0); |
3543 | SETs(TARG); | |
a0ed51b3 LW |
3544 | } |
3545 | else { | |
128c9517 JH |
3546 | STRLEN min = len + 1; |
3547 | ||
862a34c6 | 3548 | SvUPGRADE(TARG, SVt_PV); |
128c9517 | 3549 | SvGROW(TARG, min); |
31351b04 JS |
3550 | (void)SvPOK_only(TARG); |
3551 | d = (U8*)SvPVX(TARG); | |
3552 | send = s + len; | |
a2a2844f | 3553 | while (s < send) { |
1b6737cc AL |
3554 | const STRLEN u = UTF8SKIP(s); |
3555 | const UV uv = toLOWER_utf8(s, tmpbuf, &ulen); | |
89ebb4a3 JH |
3556 | |
3557 | #define GREEK_CAPITAL_LETTER_SIGMA 0x03A3 /* Unicode U+03A3 */ | |
6fdb5f96 JH |
3558 | if (uv == GREEK_CAPITAL_LETTER_SIGMA) { |
3559 | /* | |
3560 | * Now if the sigma is NOT followed by | |
3561 | * /$ignorable_sequence$cased_letter/; | |
3562 | * and it IS preceded by | |
3563 | * /$cased_letter$ignorable_sequence/; | |
3564 | * where $ignorable_sequence is | |
3565 | * [\x{2010}\x{AD}\p{Mn}]* | |
3566 | * and $cased_letter is | |
3567 | * [\p{Ll}\p{Lo}\p{Lt}] | |
3568 | * then it should be mapped to 0x03C2, | |
3569 | * (GREEK SMALL LETTER FINAL SIGMA), | |
3570 | * instead of staying 0x03A3. | |
89ebb4a3 JH |
3571 | * "should be": in other words, |
3572 | * this is not implemented yet. | |
3573 | * See lib/unicore/SpecialCasing.txt. | |
6fdb5f96 JH |
3574 | */ |
3575 | } | |
128c9517 JH |
3576 | if (ulen > u && (SvLEN(TARG) < (min += ulen - u))) { |
3577 | /* If the eventually required minimum size outgrows | |
3578 | * the available space, we need to grow. */ | |
0bd48802 | 3579 | const UV o = d - (U8*)SvPVX_const(TARG); |
89ebb4a3 JH |
3580 | |
3581 | /* If someone lowercases one million U+0130s we | |
3582 | * SvGROW() one million times. Or we could try | |
32c480af JH |
3583 | * guessing how much to allocate without allocating. |
3584 | * too much. Such is life. */ | |
128c9517 | 3585 | SvGROW(TARG, min); |
89ebb4a3 JH |
3586 | d = (U8*)SvPVX(TARG) + o; |
3587 | } | |
a2a2844f JH |
3588 | Copy(tmpbuf, d, ulen, U8); |
3589 | d += ulen; | |
89ebb4a3 | 3590 | s += u; |
a0ed51b3 | 3591 | } |
31351b04 | 3592 | *d = '\0'; |
7e2040f0 | 3593 | SvUTF8_on(TARG); |
349d4f2f | 3594 | SvCUR_set(TARG, d - (U8*)SvPVX_const(TARG)); |
31351b04 | 3595 | SETs(TARG); |
a0ed51b3 | 3596 | } |
79072805 | 3597 | } |
626727d5 | 3598 | else { |
10516c54 | 3599 | U8 *s; |
014822e4 | 3600 | if (!SvPADTMP(sv) || SvREADONLY(sv)) { |
31351b04 | 3601 | dTARGET; |
7e2040f0 | 3602 | SvUTF8_off(TARG); /* decontaminate */ |
d104a74c | 3603 | sv_setsv_nomg(TARG, sv); |
31351b04 JS |
3604 | sv = TARG; |
3605 | SETs(sv); | |
a0ed51b3 | 3606 | } |
bbce6d69 | 3607 | |
d104a74c | 3608 | s = (U8*)SvPV_force_nomg(sv, len); |
31351b04 | 3609 | if (len) { |
1b6737cc | 3610 | register const U8 * const send = s + len; |
bbce6d69 | 3611 | |
2de3dbcc | 3612 | if (IN_LOCALE_RUNTIME) { |
31351b04 JS |
3613 | TAINT; |
3614 | SvTAINTED_on(sv); | |
3615 | for (; s < send; s++) | |
3616 | *s = toLOWER_LC(*s); | |
3617 | } | |
3618 | else { | |
3619 | for (; s < send; s++) | |
3620 | *s = toLOWER(*s); | |
3621 | } | |
bbce6d69 | 3622 | } |
79072805 | 3623 | } |
d104a74c | 3624 | SvSETMAGIC(sv); |
79072805 LW |
3625 | RETURN; |
3626 | } | |
3627 | ||
a0d0e21e | 3628 | PP(pp_quotemeta) |
79072805 | 3629 | { |
39644a26 | 3630 | dSP; dTARGET; |
1b6737cc | 3631 | SV * const sv = TOPs; |
a0d0e21e | 3632 | STRLEN len; |
0d46e09a | 3633 | register const char *s = SvPV_const(sv,len); |
79072805 | 3634 | |
7e2040f0 | 3635 | SvUTF8_off(TARG); /* decontaminate */ |
a0d0e21e | 3636 | if (len) { |
1b6737cc | 3637 | register char *d; |
862a34c6 | 3638 | SvUPGRADE(TARG, SVt_PV); |
c07a80fd | 3639 | SvGROW(TARG, (len * 2) + 1); |
a0d0e21e | 3640 | d = SvPVX(TARG); |
7e2040f0 | 3641 | if (DO_UTF8(sv)) { |
0dd2cdef | 3642 | while (len) { |
fd400ab9 | 3643 | if (UTF8_IS_CONTINUED(*s)) { |
0dd2cdef LW |
3644 | STRLEN ulen = UTF8SKIP(s); |
3645 | if (ulen > len) | |
3646 | ulen = len; | |
3647 | len -= ulen; | |