Commit | Line | Data |
---|---|---|
a0d0e21e | 1 | /* pp.c |
79072805 | 2 | * |
4bb101f2 JH |
3 | * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, |
4 | * 2000, 2001, 2002, 2003, 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 LW |
15 | |
16 | #include "EXTERN.h" | |
864dbfa3 | 17 | #define PERL_IN_PP_C |
79072805 | 18 | #include "perl.h" |
77bc9082 | 19 | #include "keywords.h" |
79072805 | 20 | |
a4af207c JH |
21 | #include "reentr.h" |
22 | ||
dfe9444c AD |
23 | /* XXX I can't imagine anyone who doesn't have this actually _needs_ |
24 | it, since pid_t is an integral type. | |
25 | --AD 2/20/1998 | |
26 | */ | |
27 | #ifdef NEED_GETPID_PROTO | |
28 | extern Pid_t getpid (void); | |
8ac85365 NIS |
29 | #endif |
30 | ||
13017935 SM |
31 | /* variations on pp_null */ |
32 | ||
93a17b20 LW |
33 | PP(pp_stub) |
34 | { | |
39644a26 | 35 | dSP; |
54310121 | 36 | if (GIMME_V == G_SCALAR) |
3280af22 | 37 | XPUSHs(&PL_sv_undef); |
93a17b20 LW |
38 | RETURN; |
39 | } | |
40 | ||
79072805 LW |
41 | PP(pp_scalar) |
42 | { | |
43 | return NORMAL; | |
44 | } | |
45 | ||
46 | /* Pushy stuff. */ | |
47 | ||
93a17b20 LW |
48 | PP(pp_padav) |
49 | { | |
39644a26 | 50 | dSP; dTARGET; |
13017935 | 51 | I32 gimme; |
533c011a | 52 | if (PL_op->op_private & OPpLVAL_INTRO) |
dd2155a4 | 53 | SAVECLEARSV(PAD_SVl(PL_op->op_targ)); |
85e6fe83 | 54 | EXTEND(SP, 1); |
533c011a | 55 | if (PL_op->op_flags & OPf_REF) { |
85e6fe83 | 56 | PUSHs(TARG); |
93a17b20 | 57 | RETURN; |
78f9721b SM |
58 | } else if (LVRET) { |
59 | if (GIMME == G_SCALAR) | |
60 | Perl_croak(aTHX_ "Can't return array to lvalue scalar context"); | |
61 | PUSHs(TARG); | |
62 | RETURN; | |
85e6fe83 | 63 | } |
13017935 SM |
64 | gimme = GIMME_V; |
65 | if (gimme == G_ARRAY) { | |
85e6fe83 LW |
66 | I32 maxarg = AvFILL((AV*)TARG) + 1; |
67 | EXTEND(SP, maxarg); | |
93965878 NIS |
68 | if (SvMAGICAL(TARG)) { |
69 | U32 i; | |
eb160463 | 70 | for (i=0; i < (U32)maxarg; i++) { |
93965878 | 71 | SV **svp = av_fetch((AV*)TARG, i, FALSE); |
3280af22 | 72 | SP[i+1] = (svp) ? *svp : &PL_sv_undef; |
93965878 NIS |
73 | } |
74 | } | |
75 | else { | |
76 | Copy(AvARRAY((AV*)TARG), SP+1, maxarg, SV*); | |
77 | } | |
85e6fe83 LW |
78 | SP += maxarg; |
79 | } | |
13017935 | 80 | else if (gimme == G_SCALAR) { |
85e6fe83 LW |
81 | SV* sv = sv_newmortal(); |
82 | I32 maxarg = AvFILL((AV*)TARG) + 1; | |
83 | sv_setiv(sv, maxarg); | |
84 | PUSHs(sv); | |
85 | } | |
86 | RETURN; | |
93a17b20 LW |
87 | } |
88 | ||
89 | PP(pp_padhv) | |
90 | { | |
39644a26 | 91 | dSP; dTARGET; |
54310121 | 92 | I32 gimme; |
93 | ||
93a17b20 | 94 | XPUSHs(TARG); |
533c011a | 95 | if (PL_op->op_private & OPpLVAL_INTRO) |
dd2155a4 | 96 | SAVECLEARSV(PAD_SVl(PL_op->op_targ)); |
533c011a | 97 | if (PL_op->op_flags & OPf_REF) |
93a17b20 | 98 | RETURN; |
78f9721b SM |
99 | else if (LVRET) { |
100 | if (GIMME == G_SCALAR) | |
101 | Perl_croak(aTHX_ "Can't return hash to lvalue scalar context"); | |
102 | RETURN; | |
103 | } | |
54310121 | 104 | gimme = GIMME_V; |
105 | if (gimme == G_ARRAY) { | |
cea2e8a9 | 106 | RETURNOP(do_kv()); |
85e6fe83 | 107 | } |
54310121 | 108 | else if (gimme == G_SCALAR) { |
85e6fe83 | 109 | SV* sv = sv_newmortal(); |
46fc3d4c | 110 | if (HvFILL((HV*)TARG)) |
cea2e8a9 | 111 | Perl_sv_setpvf(aTHX_ sv, "%ld/%ld", |
46fc3d4c | 112 | (long)HvFILL((HV*)TARG), (long)HvMAX((HV*)TARG) + 1); |
85e6fe83 LW |
113 | else |
114 | sv_setiv(sv, 0); | |
115 | SETs(sv); | |
85e6fe83 | 116 | } |
54310121 | 117 | RETURN; |
93a17b20 LW |
118 | } |
119 | ||
ed6116ce LW |
120 | PP(pp_padany) |
121 | { | |
cea2e8a9 | 122 | DIE(aTHX_ "NOT IMPL LINE %d",__LINE__); |
ed6116ce 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) { |
137 | GV *gv = (GV*) sv_newmortal(); | |
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) { |
748a9306 | 148 | char *sym; |
c9d5ac95 | 149 | STRLEN len; |
748a9306 | 150 | |
a0d0e21e LW |
151 | if (SvGMAGICAL(sv)) { |
152 | mg_get(sv); | |
153 | if (SvROK(sv)) | |
154 | goto wasref; | |
155 | } | |
afd1915d | 156 | if (!SvOK(sv) && sv != &PL_sv_undef) { |
b13b2135 | 157 | /* If this is a 'my' scalar and flag is set then vivify |
853846ea | 158 | * NI-S 1999/05/07 |
b13b2135 | 159 | */ |
1d8d4d2a | 160 | if (PL_op->op_private & OPpDEREF) { |
2c8ac474 GS |
161 | char *name; |
162 | GV *gv; | |
163 | if (cUNOP->op_targ) { | |
164 | STRLEN len; | |
dd2155a4 | 165 | SV *namesv = PAD_SV(cUNOP->op_targ); |
2c8ac474 | 166 | name = SvPV(namesv, len); |
2d6d9f7a | 167 | gv = (GV*)NEWSV(0,0); |
2c8ac474 GS |
168 | gv_init(gv, CopSTASH(PL_curcop), name, len, 0); |
169 | } | |
170 | else { | |
171 | name = CopSTASHPV(PL_curcop); | |
172 | gv = newGVgen(name); | |
1d8d4d2a | 173 | } |
b13b2135 NIS |
174 | if (SvTYPE(sv) < SVt_RV) |
175 | sv_upgrade(sv, SVt_RV); | |
2c8ac474 | 176 | SvRV(sv) = (SV*)gv; |
853846ea | 177 | SvROK_on(sv); |
1d8d4d2a | 178 | SvSETMAGIC(sv); |
853846ea | 179 | goto wasref; |
2c8ac474 | 180 | } |
533c011a NIS |
181 | if (PL_op->op_flags & OPf_REF || |
182 | PL_op->op_private & HINT_STRICT_REFS) | |
cea2e8a9 | 183 | DIE(aTHX_ PL_no_usym, "a symbol"); |
599cee73 | 184 | if (ckWARN(WARN_UNINITIALIZED)) |
b89fed5f | 185 | report_uninit(); |
a0d0e21e LW |
186 | RETSETUNDEF; |
187 | } | |
c9d5ac95 | 188 | sym = SvPV(sv,len); |
35cd451c GS |
189 | if ((PL_op->op_flags & OPf_SPECIAL) && |
190 | !(PL_op->op_flags & OPf_MOD)) | |
191 | { | |
192 | sv = (SV*)gv_fetchpv(sym, FALSE, SVt_PVGV); | |
c9d5ac95 GS |
193 | if (!sv |
194 | && (!is_gv_magical(sym,len,0) | |
195 | || !(sv = (SV*)gv_fetchpv(sym, TRUE, SVt_PVGV)))) | |
196 | { | |
35cd451c | 197 | RETSETUNDEF; |
c9d5ac95 | 198 | } |
35cd451c GS |
199 | } |
200 | else { | |
201 | if (PL_op->op_private & HINT_STRICT_REFS) | |
cea2e8a9 | 202 | DIE(aTHX_ PL_no_symref, sym, "a symbol"); |
35cd451c GS |
203 | sv = (SV*)gv_fetchpv(sym, TRUE, SVt_PVGV); |
204 | } | |
93a17b20 | 205 | } |
79072805 | 206 | } |
533c011a NIS |
207 | if (PL_op->op_private & OPpLVAL_INTRO) |
208 | save_gp((GV*)sv, !(PL_op->op_flags & OPf_SPECIAL)); | |
79072805 LW |
209 | SETs(sv); |
210 | RETURN; | |
211 | } | |
212 | ||
79072805 LW |
213 | PP(pp_rv2sv) |
214 | { | |
82d03984 | 215 | GV *gv = Nullgv; |
39644a26 | 216 | dSP; dTOPss; |
79072805 | 217 | |
ed6116ce | 218 | if (SvROK(sv)) { |
a0d0e21e | 219 | wasref: |
f5284f61 IZ |
220 | tryAMAGICunDEREF(to_sv); |
221 | ||
ed6116ce | 222 | sv = SvRV(sv); |
79072805 LW |
223 | switch (SvTYPE(sv)) { |
224 | case SVt_PVAV: | |
225 | case SVt_PVHV: | |
226 | case SVt_PVCV: | |
cea2e8a9 | 227 | DIE(aTHX_ "Not a SCALAR reference"); |
79072805 LW |
228 | } |
229 | } | |
230 | else { | |
748a9306 | 231 | char *sym; |
c9d5ac95 | 232 | STRLEN len; |
82d03984 | 233 | gv = (GV*)sv; |
748a9306 | 234 | |
463ee0b2 | 235 | if (SvTYPE(gv) != SVt_PVGV) { |
a0d0e21e LW |
236 | if (SvGMAGICAL(sv)) { |
237 | mg_get(sv); | |
238 | if (SvROK(sv)) | |
239 | goto wasref; | |
240 | } | |
241 | if (!SvOK(sv)) { | |
533c011a NIS |
242 | if (PL_op->op_flags & OPf_REF || |
243 | PL_op->op_private & HINT_STRICT_REFS) | |
cea2e8a9 | 244 | DIE(aTHX_ PL_no_usym, "a SCALAR"); |
599cee73 | 245 | if (ckWARN(WARN_UNINITIALIZED)) |
b89fed5f | 246 | report_uninit(); |
a0d0e21e LW |
247 | RETSETUNDEF; |
248 | } | |
c9d5ac95 | 249 | sym = SvPV(sv, len); |
35cd451c GS |
250 | if ((PL_op->op_flags & OPf_SPECIAL) && |
251 | !(PL_op->op_flags & OPf_MOD)) | |
252 | { | |
253 | gv = (GV*)gv_fetchpv(sym, FALSE, SVt_PV); | |
c9d5ac95 GS |
254 | if (!gv |
255 | && (!is_gv_magical(sym,len,0) | |
256 | || !(gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PV)))) | |
257 | { | |
35cd451c | 258 | RETSETUNDEF; |
c9d5ac95 | 259 | } |
35cd451c GS |
260 | } |
261 | else { | |
262 | if (PL_op->op_private & HINT_STRICT_REFS) | |
cea2e8a9 | 263 | DIE(aTHX_ PL_no_symref, sym, "a SCALAR"); |
35cd451c GS |
264 | gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PV); |
265 | } | |
463ee0b2 LW |
266 | } |
267 | sv = GvSV(gv); | |
a0d0e21e | 268 | } |
533c011a | 269 | if (PL_op->op_flags & OPf_MOD) { |
82d03984 RGS |
270 | if (PL_op->op_private & OPpLVAL_INTRO) { |
271 | if (cUNOP->op_first->op_type == OP_NULL) | |
272 | sv = save_scalar((GV*)TOPs); | |
273 | else if (gv) | |
274 | sv = save_scalar(gv); | |
275 | else | |
276 | Perl_croak(aTHX_ PL_no_localize_ref); | |
277 | } | |
533c011a NIS |
278 | else if (PL_op->op_private & OPpDEREF) |
279 | vivify_ref(sv, PL_op->op_private & OPpDEREF); | |
79072805 | 280 | } |
a0d0e21e | 281 | SETs(sv); |
79072805 LW |
282 | RETURN; |
283 | } | |
284 | ||
285 | PP(pp_av2arylen) | |
286 | { | |
39644a26 | 287 | dSP; |
79072805 LW |
288 | AV *av = (AV*)TOPs; |
289 | SV *sv = AvARYLEN(av); | |
290 | if (!sv) { | |
291 | AvARYLEN(av) = sv = NEWSV(0,0); | |
292 | sv_upgrade(sv, SVt_IV); | |
14befaf4 | 293 | sv_magic(sv, (SV*)av, PERL_MAGIC_arylen, Nullch, 0); |
79072805 LW |
294 | } |
295 | SETs(sv); | |
296 | RETURN; | |
297 | } | |
298 | ||
a0d0e21e LW |
299 | PP(pp_pos) |
300 | { | |
39644a26 | 301 | dSP; dTARGET; dPOPss; |
8ec5e241 | 302 | |
78f9721b | 303 | if (PL_op->op_flags & OPf_MOD || LVRET) { |
5f05dabc | 304 | if (SvTYPE(TARG) < SVt_PVLV) { |
305 | sv_upgrade(TARG, SVt_PVLV); | |
14befaf4 | 306 | sv_magic(TARG, Nullsv, PERL_MAGIC_pos, Nullch, 0); |
5f05dabc | 307 | } |
308 | ||
309 | LvTYPE(TARG) = '.'; | |
6ff81951 GS |
310 | if (LvTARG(TARG) != sv) { |
311 | if (LvTARG(TARG)) | |
312 | SvREFCNT_dec(LvTARG(TARG)); | |
313 | LvTARG(TARG) = SvREFCNT_inc(sv); | |
314 | } | |
a0d0e21e LW |
315 | PUSHs(TARG); /* no SvSETMAGIC */ |
316 | RETURN; | |
317 | } | |
318 | else { | |
8ec5e241 | 319 | MAGIC* mg; |
a0d0e21e LW |
320 | |
321 | if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) { | |
14befaf4 | 322 | mg = mg_find(sv, PERL_MAGIC_regex_global); |
565764a8 | 323 | if (mg && mg->mg_len >= 0) { |
a0ed51b3 | 324 | I32 i = mg->mg_len; |
7e2040f0 | 325 | if (DO_UTF8(sv)) |
a0ed51b3 LW |
326 | sv_pos_b2u(sv, &i); |
327 | PUSHi(i + PL_curcop->cop_arybase); | |
a0d0e21e LW |
328 | RETURN; |
329 | } | |
330 | } | |
331 | RETPUSHUNDEF; | |
332 | } | |
333 | } | |
334 | ||
79072805 LW |
335 | PP(pp_rv2cv) |
336 | { | |
39644a26 | 337 | dSP; |
79072805 LW |
338 | GV *gv; |
339 | HV *stash; | |
8990e307 | 340 | |
4633a7c4 LW |
341 | /* We usually try to add a non-existent subroutine in case of AUTOLOAD. */ |
342 | /* (But not in defined().) */ | |
533c011a | 343 | CV *cv = sv_2cv(TOPs, &stash, &gv, !(PL_op->op_flags & OPf_SPECIAL)); |
07055b4c CS |
344 | if (cv) { |
345 | if (CvCLONE(cv)) | |
346 | cv = (CV*)sv_2mortal((SV*)cv_clone(cv)); | |
d32f2495 SC |
347 | if ((PL_op->op_private & OPpLVAL_INTRO)) { |
348 | if (gv && GvCV(gv) == cv && (gv = gv_autoload4(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv), FALSE))) | |
349 | cv = GvCV(gv); | |
350 | if (!CvLVALUE(cv)) | |
351 | DIE(aTHX_ "Can't modify non-lvalue subroutine call"); | |
352 | } | |
07055b4c CS |
353 | } |
354 | else | |
3280af22 | 355 | cv = (CV*)&PL_sv_undef; |
79072805 LW |
356 | SETs((SV*)cv); |
357 | RETURN; | |
358 | } | |
359 | ||
c07a80fd | 360 | PP(pp_prototype) |
361 | { | |
39644a26 | 362 | dSP; |
c07a80fd | 363 | CV *cv; |
364 | HV *stash; | |
365 | GV *gv; | |
366 | SV *ret; | |
367 | ||
3280af22 | 368 | ret = &PL_sv_undef; |
b6c543e3 IZ |
369 | if (SvPOK(TOPs) && SvCUR(TOPs) >= 7) { |
370 | char *s = SvPVX(TOPs); | |
371 | if (strnEQ(s, "CORE::", 6)) { | |
372 | int code; | |
b13b2135 | 373 | |
b6c543e3 IZ |
374 | code = keyword(s + 6, SvCUR(TOPs) - 6); |
375 | if (code < 0) { /* Overridable. */ | |
376 | #define MAX_ARGS_OP ((sizeof(I32) - 1) * 2) | |
377 | int i = 0, n = 0, seen_question = 0; | |
378 | I32 oa; | |
379 | char str[ MAX_ARGS_OP * 2 + 2 ]; /* One ';', one '\0' */ | |
380 | ||
77bc9082 RGS |
381 | if (code == -KEY_chop || code == -KEY_chomp) |
382 | goto set; | |
b6c543e3 | 383 | while (i < MAXO) { /* The slow way. */ |
22c35a8c GS |
384 | if (strEQ(s + 6, PL_op_name[i]) |
385 | || strEQ(s + 6, PL_op_desc[i])) | |
386 | { | |
b6c543e3 | 387 | goto found; |
22c35a8c | 388 | } |
b6c543e3 IZ |
389 | i++; |
390 | } | |
391 | goto nonesuch; /* Should not happen... */ | |
392 | found: | |
22c35a8c | 393 | oa = PL_opargs[i] >> OASHIFT; |
b6c543e3 | 394 | while (oa) { |
3012a639 | 395 | if (oa & OA_OPTIONAL && !seen_question) { |
b6c543e3 IZ |
396 | seen_question = 1; |
397 | str[n++] = ';'; | |
ef54e1a4 | 398 | } |
b13b2135 | 399 | else if (n && str[0] == ';' && seen_question) |
b6c543e3 | 400 | goto set; /* XXXX system, exec */ |
b13b2135 | 401 | if ((oa & (OA_OPTIONAL - 1)) >= OA_AVREF |
6e97e420 SC |
402 | && (oa & (OA_OPTIONAL - 1)) <= OA_SCALARREF |
403 | /* But globs are already references (kinda) */ | |
404 | && (oa & (OA_OPTIONAL - 1)) != OA_FILEREF | |
405 | ) { | |
b6c543e3 IZ |
406 | str[n++] = '\\'; |
407 | } | |
b6c543e3 IZ |
408 | str[n++] = ("?$@@%&*$")[oa & (OA_OPTIONAL - 1)]; |
409 | oa = oa >> 4; | |
410 | } | |
411 | str[n++] = '\0'; | |
79cb57f6 | 412 | ret = sv_2mortal(newSVpvn(str, n - 1)); |
ef54e1a4 JH |
413 | } |
414 | else if (code) /* Non-Overridable */ | |
b6c543e3 IZ |
415 | goto set; |
416 | else { /* None such */ | |
417 | nonesuch: | |
d470f89e | 418 | DIE(aTHX_ "Can't find an opnumber for \"%s\"", s+6); |
b6c543e3 IZ |
419 | } |
420 | } | |
421 | } | |
c07a80fd | 422 | cv = sv_2cv(TOPs, &stash, &gv, FALSE); |
5f05dabc | 423 | if (cv && SvPOK(cv)) |
79cb57f6 | 424 | ret = sv_2mortal(newSVpvn(SvPVX(cv), SvCUR(cv))); |
b6c543e3 | 425 | set: |
c07a80fd | 426 | SETs(ret); |
427 | RETURN; | |
428 | } | |
429 | ||
a0d0e21e LW |
430 | PP(pp_anoncode) |
431 | { | |
39644a26 | 432 | dSP; |
dd2155a4 | 433 | CV* cv = (CV*)PAD_SV(PL_op->op_targ); |
a5f75d66 | 434 | if (CvCLONE(cv)) |
b355b4e0 | 435 | cv = (CV*)sv_2mortal((SV*)cv_clone(cv)); |
5f05dabc | 436 | EXTEND(SP,1); |
748a9306 | 437 | PUSHs((SV*)cv); |
a0d0e21e LW |
438 | RETURN; |
439 | } | |
440 | ||
441 | PP(pp_srefgen) | |
79072805 | 442 | { |
39644a26 | 443 | dSP; |
71be2cbc | 444 | *SP = refto(*SP); |
79072805 | 445 | RETURN; |
8ec5e241 | 446 | } |
a0d0e21e LW |
447 | |
448 | PP(pp_refgen) | |
449 | { | |
39644a26 | 450 | dSP; dMARK; |
a0d0e21e | 451 | if (GIMME != G_ARRAY) { |
5f0b1d4e GS |
452 | if (++MARK <= SP) |
453 | *MARK = *SP; | |
454 | else | |
3280af22 | 455 | *MARK = &PL_sv_undef; |
5f0b1d4e GS |
456 | *MARK = refto(*MARK); |
457 | SP = MARK; | |
458 | RETURN; | |
a0d0e21e | 459 | } |
bbce6d69 | 460 | EXTEND_MORTAL(SP - MARK); |
71be2cbc | 461 | while (++MARK <= SP) |
462 | *MARK = refto(*MARK); | |
a0d0e21e | 463 | RETURN; |
79072805 LW |
464 | } |
465 | ||
76e3520e | 466 | STATIC SV* |
cea2e8a9 | 467 | S_refto(pTHX_ SV *sv) |
71be2cbc | 468 | { |
469 | SV* rv; | |
470 | ||
471 | if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') { | |
472 | if (LvTARGLEN(sv)) | |
68dc0745 | 473 | vivify_defelem(sv); |
474 | if (!(sv = LvTARG(sv))) | |
3280af22 | 475 | sv = &PL_sv_undef; |
0dd88869 | 476 | else |
a6c40364 | 477 | (void)SvREFCNT_inc(sv); |
71be2cbc | 478 | } |
d8b46c1b GS |
479 | else if (SvTYPE(sv) == SVt_PVAV) { |
480 | if (!AvREAL((AV*)sv) && AvREIFY((AV*)sv)) | |
481 | av_reify((AV*)sv); | |
482 | SvTEMP_off(sv); | |
483 | (void)SvREFCNT_inc(sv); | |
484 | } | |
f2933f5f DM |
485 | else if (SvPADTMP(sv) && !IS_PADGV(sv)) |
486 | sv = newSVsv(sv); | |
71be2cbc | 487 | else { |
488 | SvTEMP_off(sv); | |
489 | (void)SvREFCNT_inc(sv); | |
490 | } | |
491 | rv = sv_newmortal(); | |
492 | sv_upgrade(rv, SVt_RV); | |
493 | SvRV(rv) = sv; | |
494 | SvROK_on(rv); | |
495 | return rv; | |
496 | } | |
497 | ||
79072805 LW |
498 | PP(pp_ref) |
499 | { | |
39644a26 | 500 | dSP; dTARGET; |
463ee0b2 | 501 | SV *sv; |
79072805 LW |
502 | char *pv; |
503 | ||
a0d0e21e | 504 | sv = POPs; |
f12c7020 | 505 | |
506 | if (sv && SvGMAGICAL(sv)) | |
8ec5e241 | 507 | mg_get(sv); |
f12c7020 | 508 | |
a0d0e21e | 509 | if (!sv || !SvROK(sv)) |
4633a7c4 | 510 | RETPUSHNO; |
79072805 | 511 | |
ed6116ce | 512 | sv = SvRV(sv); |
a0d0e21e | 513 | pv = sv_reftype(sv,TRUE); |
463ee0b2 | 514 | PUSHp(pv, strlen(pv)); |
79072805 LW |
515 | RETURN; |
516 | } | |
517 | ||
518 | PP(pp_bless) | |
519 | { | |
39644a26 | 520 | dSP; |
463ee0b2 | 521 | HV *stash; |
79072805 | 522 | |
463ee0b2 | 523 | if (MAXARG == 1) |
11faa288 | 524 | stash = CopSTASH(PL_curcop); |
7b8d334a GS |
525 | else { |
526 | SV *ssv = POPs; | |
527 | STRLEN len; | |
81689caa HS |
528 | char *ptr; |
529 | ||
016a42f3 | 530 | if (ssv && !SvGMAGICAL(ssv) && !SvAMAGIC(ssv) && SvROK(ssv)) |
81689caa HS |
531 | Perl_croak(aTHX_ "Attempt to bless into a reference"); |
532 | ptr = SvPV(ssv,len); | |
e476b1b5 | 533 | if (ckWARN(WARN_MISC) && len == 0) |
9014280d | 534 | Perl_warner(aTHX_ packWARN(WARN_MISC), |
599cee73 | 535 | "Explicit blessing to '' (assuming package main)"); |
7b8d334a GS |
536 | stash = gv_stashpvn(ptr, len, TRUE); |
537 | } | |
a0d0e21e | 538 | |
5d3fdfeb | 539 | (void)sv_bless(TOPs, stash); |
79072805 LW |
540 | RETURN; |
541 | } | |
542 | ||
fb73857a | 543 | PP(pp_gelem) |
544 | { | |
545 | GV *gv; | |
546 | SV *sv; | |
76e3520e | 547 | SV *tmpRef; |
fb73857a | 548 | char *elem; |
39644a26 | 549 | dSP; |
2d8e6c8d | 550 | STRLEN n_a; |
b13b2135 | 551 | |
fb73857a | 552 | sv = POPs; |
2d8e6c8d | 553 | elem = SvPV(sv, n_a); |
fb73857a | 554 | gv = (GV*)POPs; |
76e3520e | 555 | tmpRef = Nullsv; |
fb73857a | 556 | sv = Nullsv; |
557 | switch (elem ? *elem : '\0') | |
558 | { | |
559 | case 'A': | |
560 | if (strEQ(elem, "ARRAY")) | |
76e3520e | 561 | tmpRef = (SV*)GvAV(gv); |
fb73857a | 562 | break; |
563 | case 'C': | |
564 | if (strEQ(elem, "CODE")) | |
76e3520e | 565 | tmpRef = (SV*)GvCVu(gv); |
fb73857a | 566 | break; |
567 | case 'F': | |
39b99f21 | 568 | if (strEQ(elem, "FILEHANDLE")) { |
569 | /* finally deprecated in 5.8.0 */ | |
9014280d | 570 | deprecate("*glob{FILEHANDLE}"); |
76e3520e | 571 | tmpRef = (SV*)GvIOp(gv); |
39b99f21 | 572 | } |
f4d13ee9 JH |
573 | else |
574 | if (strEQ(elem, "FORMAT")) | |
575 | tmpRef = (SV*)GvFORM(gv); | |
fb73857a | 576 | break; |
577 | case 'G': | |
578 | if (strEQ(elem, "GLOB")) | |
76e3520e | 579 | tmpRef = (SV*)gv; |
fb73857a | 580 | break; |
581 | case 'H': | |
582 | if (strEQ(elem, "HASH")) | |
76e3520e | 583 | tmpRef = (SV*)GvHV(gv); |
fb73857a | 584 | break; |
585 | case 'I': | |
586 | if (strEQ(elem, "IO")) | |
76e3520e | 587 | tmpRef = (SV*)GvIOp(gv); |
fb73857a | 588 | break; |
589 | case 'N': | |
590 | if (strEQ(elem, "NAME")) | |
79cb57f6 | 591 | sv = newSVpvn(GvNAME(gv), GvNAMELEN(gv)); |
fb73857a | 592 | break; |
593 | case 'P': | |
594 | if (strEQ(elem, "PACKAGE")) | |
595 | sv = newSVpv(HvNAME(GvSTASH(gv)), 0); | |
596 | break; | |
597 | case 'S': | |
598 | if (strEQ(elem, "SCALAR")) | |
76e3520e | 599 | tmpRef = GvSV(gv); |
fb73857a | 600 | break; |
601 | } | |
76e3520e GS |
602 | if (tmpRef) |
603 | sv = newRV(tmpRef); | |
fb73857a | 604 | if (sv) |
605 | sv_2mortal(sv); | |
606 | else | |
3280af22 | 607 | sv = &PL_sv_undef; |
fb73857a | 608 | XPUSHs(sv); |
609 | RETURN; | |
610 | } | |
611 | ||
a0d0e21e | 612 | /* Pattern matching */ |
79072805 | 613 | |
a0d0e21e | 614 | PP(pp_study) |
79072805 | 615 | { |
39644a26 | 616 | dSP; dPOPss; |
a0d0e21e LW |
617 | register unsigned char *s; |
618 | register I32 pos; | |
619 | register I32 ch; | |
620 | register I32 *sfirst; | |
621 | register I32 *snext; | |
a0d0e21e LW |
622 | STRLEN len; |
623 | ||
3280af22 | 624 | if (sv == PL_lastscream) { |
1e422769 | 625 | if (SvSCREAM(sv)) |
626 | RETPUSHYES; | |
627 | } | |
c07a80fd | 628 | else { |
3280af22 NIS |
629 | if (PL_lastscream) { |
630 | SvSCREAM_off(PL_lastscream); | |
631 | SvREFCNT_dec(PL_lastscream); | |
c07a80fd | 632 | } |
3280af22 | 633 | PL_lastscream = SvREFCNT_inc(sv); |
c07a80fd | 634 | } |
1e422769 | 635 | |
636 | s = (unsigned char*)(SvPV(sv, len)); | |
637 | pos = len; | |
638 | if (pos <= 0) | |
639 | RETPUSHNO; | |
3280af22 NIS |
640 | if (pos > PL_maxscream) { |
641 | if (PL_maxscream < 0) { | |
642 | PL_maxscream = pos + 80; | |
643 | New(301, PL_screamfirst, 256, I32); | |
644 | New(302, PL_screamnext, PL_maxscream, I32); | |
79072805 LW |
645 | } |
646 | else { | |
3280af22 NIS |
647 | PL_maxscream = pos + pos / 4; |
648 | Renew(PL_screamnext, PL_maxscream, I32); | |
79072805 | 649 | } |
79072805 | 650 | } |
a0d0e21e | 651 | |
3280af22 NIS |
652 | sfirst = PL_screamfirst; |
653 | snext = PL_screamnext; | |
a0d0e21e LW |
654 | |
655 | if (!sfirst || !snext) | |
cea2e8a9 | 656 | DIE(aTHX_ "do_study: out of memory"); |
a0d0e21e LW |
657 | |
658 | for (ch = 256; ch; --ch) | |
659 | *sfirst++ = -1; | |
660 | sfirst -= 256; | |
661 | ||
662 | while (--pos >= 0) { | |
663 | ch = s[pos]; | |
664 | if (sfirst[ch] >= 0) | |
665 | snext[pos] = sfirst[ch] - pos; | |
666 | else | |
667 | snext[pos] = -pos; | |
668 | sfirst[ch] = pos; | |
79072805 LW |
669 | } |
670 | ||
c07a80fd | 671 | SvSCREAM_on(sv); |
14befaf4 DM |
672 | /* piggyback on m//g magic */ |
673 | sv_magic(sv, Nullsv, PERL_MAGIC_regex_global, Nullch, 0); | |
1e422769 | 674 | RETPUSHYES; |
79072805 LW |
675 | } |
676 | ||
a0d0e21e | 677 | PP(pp_trans) |
79072805 | 678 | { |
39644a26 | 679 | dSP; dTARG; |
a0d0e21e LW |
680 | SV *sv; |
681 | ||
533c011a | 682 | if (PL_op->op_flags & OPf_STACKED) |
a0d0e21e | 683 | sv = POPs; |
79072805 | 684 | else { |
54b9620d | 685 | sv = DEFSV; |
a0d0e21e | 686 | EXTEND(SP,1); |
79072805 | 687 | } |
adbc6bb1 | 688 | TARG = sv_newmortal(); |
4757a243 | 689 | PUSHi(do_trans(sv)); |
a0d0e21e | 690 | RETURN; |
79072805 LW |
691 | } |
692 | ||
a0d0e21e | 693 | /* Lvalue operators. */ |
79072805 | 694 | |
a0d0e21e LW |
695 | PP(pp_schop) |
696 | { | |
39644a26 | 697 | dSP; dTARGET; |
a0d0e21e LW |
698 | do_chop(TARG, TOPs); |
699 | SETTARG; | |
700 | RETURN; | |
79072805 LW |
701 | } |
702 | ||
a0d0e21e | 703 | PP(pp_chop) |
79072805 | 704 | { |
2ec6af5f RG |
705 | dSP; dMARK; dTARGET; dORIGMARK; |
706 | while (MARK < SP) | |
707 | do_chop(TARG, *++MARK); | |
708 | SP = ORIGMARK; | |
a0d0e21e LW |
709 | PUSHTARG; |
710 | RETURN; | |
79072805 LW |
711 | } |
712 | ||
a0d0e21e | 713 | PP(pp_schomp) |
79072805 | 714 | { |
39644a26 | 715 | dSP; dTARGET; |
a0d0e21e LW |
716 | SETi(do_chomp(TOPs)); |
717 | RETURN; | |
79072805 LW |
718 | } |
719 | ||
a0d0e21e | 720 | PP(pp_chomp) |
79072805 | 721 | { |
39644a26 | 722 | dSP; dMARK; dTARGET; |
a0d0e21e | 723 | register I32 count = 0; |
8ec5e241 | 724 | |
a0d0e21e LW |
725 | while (SP > MARK) |
726 | count += do_chomp(POPs); | |
727 | PUSHi(count); | |
728 | RETURN; | |
79072805 LW |
729 | } |
730 | ||
a0d0e21e | 731 | PP(pp_defined) |
463ee0b2 | 732 | { |
39644a26 | 733 | dSP; |
a0d0e21e LW |
734 | register SV* sv; |
735 | ||
736 | sv = POPs; | |
737 | if (!sv || !SvANY(sv)) | |
738 | RETPUSHNO; | |
739 | switch (SvTYPE(sv)) { | |
740 | case SVt_PVAV: | |
14befaf4 DM |
741 | if (AvMAX(sv) >= 0 || SvGMAGICAL(sv) |
742 | || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied))) | |
a0d0e21e LW |
743 | RETPUSHYES; |
744 | break; | |
745 | case SVt_PVHV: | |
14befaf4 DM |
746 | if (HvARRAY(sv) || SvGMAGICAL(sv) |
747 | || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied))) | |
a0d0e21e LW |
748 | RETPUSHYES; |
749 | break; | |
750 | case SVt_PVCV: | |
751 | if (CvROOT(sv) || CvXSUB(sv)) | |
752 | RETPUSHYES; | |
753 | break; | |
754 | default: | |
755 | if (SvGMAGICAL(sv)) | |
756 | mg_get(sv); | |
757 | if (SvOK(sv)) | |
758 | RETPUSHYES; | |
759 | } | |
760 | RETPUSHNO; | |
463ee0b2 LW |
761 | } |
762 | ||
a0d0e21e LW |
763 | PP(pp_undef) |
764 | { | |
39644a26 | 765 | dSP; |
a0d0e21e LW |
766 | SV *sv; |
767 | ||
533c011a | 768 | if (!PL_op->op_private) { |
774d564b | 769 | EXTEND(SP, 1); |
a0d0e21e | 770 | RETPUSHUNDEF; |
774d564b | 771 | } |
79072805 | 772 | |
a0d0e21e LW |
773 | sv = POPs; |
774 | if (!sv) | |
775 | RETPUSHUNDEF; | |
85e6fe83 | 776 | |
765f542d | 777 | SV_CHECK_THINKFIRST_COW_DROP(sv); |
85e6fe83 | 778 | |
a0d0e21e LW |
779 | switch (SvTYPE(sv)) { |
780 | case SVt_NULL: | |
781 | break; | |
782 | case SVt_PVAV: | |
783 | av_undef((AV*)sv); | |
784 | break; | |
785 | case SVt_PVHV: | |
786 | hv_undef((HV*)sv); | |
787 | break; | |
788 | case SVt_PVCV: | |
e476b1b5 | 789 | if (ckWARN(WARN_MISC) && cv_const_sv((CV*)sv)) |
9014280d | 790 | Perl_warner(aTHX_ packWARN(WARN_MISC), "Constant subroutine %s undefined", |
54310121 | 791 | CvANON((CV*)sv) ? "(anonymous)" : GvENAME(CvGV((CV*)sv))); |
9607fc9c | 792 | /* FALL THROUGH */ |
793 | case SVt_PVFM: | |
6fc92669 GS |
794 | { |
795 | /* let user-undef'd sub keep its identity */ | |
65c50114 | 796 | GV* gv = CvGV((CV*)sv); |
6fc92669 GS |
797 | cv_undef((CV*)sv); |
798 | CvGV((CV*)sv) = gv; | |
799 | } | |
a0d0e21e | 800 | break; |
8e07c86e | 801 | case SVt_PVGV: |
44a8e56a | 802 | if (SvFAKE(sv)) |
3280af22 | 803 | SvSetMagicSV(sv, &PL_sv_undef); |
20408e3c GS |
804 | else { |
805 | GP *gp; | |
806 | gp_free((GV*)sv); | |
807 | Newz(602, gp, 1, GP); | |
808 | GvGP(sv) = gp_ref(gp); | |
809 | GvSV(sv) = NEWSV(72,0); | |
57843af0 | 810 | GvLINE(sv) = CopLINE(PL_curcop); |
20408e3c GS |
811 | GvEGV(sv) = (GV*)sv; |
812 | GvMULTI_on(sv); | |
813 | } | |
44a8e56a | 814 | break; |
a0d0e21e | 815 | default: |
1e422769 | 816 | if (SvTYPE(sv) >= SVt_PV && SvPVX(sv) && SvLEN(sv)) { |
4633a7c4 LW |
817 | (void)SvOOK_off(sv); |
818 | Safefree(SvPVX(sv)); | |
819 | SvPV_set(sv, Nullch); | |
820 | SvLEN_set(sv, 0); | |
a0d0e21e | 821 | } |
4633a7c4 LW |
822 | (void)SvOK_off(sv); |
823 | SvSETMAGIC(sv); | |
79072805 | 824 | } |
a0d0e21e LW |
825 | |
826 | RETPUSHUNDEF; | |
79072805 LW |
827 | } |
828 | ||
a0d0e21e | 829 | PP(pp_predec) |
79072805 | 830 | { |
39644a26 | 831 | dSP; |
3510b4a1 | 832 | if (SvTYPE(TOPs) > SVt_PVLV) |
d470f89e | 833 | DIE(aTHX_ PL_no_modify); |
3510b4a1 NC |
834 | if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) |
835 | && SvIVX(TOPs) != IV_MIN) | |
55497cff | 836 | { |
748a9306 | 837 | --SvIVX(TOPs); |
55497cff | 838 | SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK); |
748a9306 LW |
839 | } |
840 | else | |
841 | sv_dec(TOPs); | |
a0d0e21e LW |
842 | SvSETMAGIC(TOPs); |
843 | return NORMAL; | |
844 | } | |
79072805 | 845 | |
a0d0e21e LW |
846 | PP(pp_postinc) |
847 | { | |
39644a26 | 848 | dSP; dTARGET; |
3510b4a1 | 849 | if (SvTYPE(TOPs) > SVt_PVLV) |
d470f89e | 850 | DIE(aTHX_ PL_no_modify); |
a0d0e21e | 851 | sv_setsv(TARG, TOPs); |
3510b4a1 NC |
852 | if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) |
853 | && SvIVX(TOPs) != IV_MAX) | |
55497cff | 854 | { |
748a9306 | 855 | ++SvIVX(TOPs); |
55497cff | 856 | SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK); |
748a9306 LW |
857 | } |
858 | else | |
859 | sv_inc(TOPs); | |
a0d0e21e | 860 | SvSETMAGIC(TOPs); |
1e54a23f | 861 | /* special case for undef: see thread at 2003-03/msg00536.html in archive */ |
a0d0e21e LW |
862 | if (!SvOK(TARG)) |
863 | sv_setiv(TARG, 0); | |
864 | SETs(TARG); | |
865 | return NORMAL; | |
866 | } | |
79072805 | 867 | |
a0d0e21e LW |
868 | PP(pp_postdec) |
869 | { | |
39644a26 | 870 | dSP; dTARGET; |
3510b4a1 | 871 | if (SvTYPE(TOPs) > SVt_PVLV) |
d470f89e | 872 | DIE(aTHX_ PL_no_modify); |
a0d0e21e | 873 | sv_setsv(TARG, TOPs); |
3510b4a1 NC |
874 | if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) |
875 | && SvIVX(TOPs) != IV_MIN) | |
55497cff | 876 | { |
748a9306 | 877 | --SvIVX(TOPs); |
55497cff | 878 | SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK); |
748a9306 LW |
879 | } |
880 | else | |
881 | sv_dec(TOPs); | |
a0d0e21e LW |
882 | SvSETMAGIC(TOPs); |
883 | SETs(TARG); | |
884 | return NORMAL; | |
885 | } | |
79072805 | 886 | |
a0d0e21e LW |
887 | /* Ordinary operators. */ |
888 | ||
889 | PP(pp_pow) | |
890 | { | |
52a96ae6 | 891 | dSP; dATARGET; |
58d76dfd | 892 | #ifdef PERL_PRESERVE_IVUV |
52a96ae6 HS |
893 | bool is_int = 0; |
894 | #endif | |
895 | tryAMAGICbin(pow,opASSIGN); | |
896 | #ifdef PERL_PRESERVE_IVUV | |
897 | /* For integer to integer power, we do the calculation by hand wherever | |
898 | we're sure it is safe; otherwise we call pow() and try to convert to | |
899 | integer afterwards. */ | |
58d76dfd JH |
900 | { |
901 | SvIV_please(TOPm1s); | |
902 | if (SvIOK(TOPm1s)) { | |
903 | bool baseuok = SvUOK(TOPm1s); | |
904 | UV baseuv; | |
905 | ||
906 | if (baseuok) { | |
907 | baseuv = SvUVX(TOPm1s); | |
908 | } else { | |
909 | IV iv = SvIVX(TOPm1s); | |
910 | if (iv >= 0) { | |
911 | baseuv = iv; | |
912 | baseuok = TRUE; /* effectively it's a UV now */ | |
913 | } else { | |
914 | baseuv = -iv; /* abs, baseuok == false records sign */ | |
915 | } | |
916 | } | |
917 | SvIV_please(TOPs); | |
918 | if (SvIOK(TOPs)) { | |
919 | UV power; | |
920 | ||
921 | if (SvUOK(TOPs)) { | |
922 | power = SvUVX(TOPs); | |
923 | } else { | |
924 | IV iv = SvIVX(TOPs); | |
925 | if (iv >= 0) { | |
926 | power = iv; | |
927 | } else { | |
928 | goto float_it; /* Can't do negative powers this way. */ | |
929 | } | |
930 | } | |
52a96ae6 HS |
931 | /* now we have integer ** positive integer. */ |
932 | is_int = 1; | |
933 | ||
934 | /* foo & (foo - 1) is zero only for a power of 2. */ | |
58d76dfd | 935 | if (!(baseuv & (baseuv - 1))) { |
52a96ae6 | 936 | /* We are raising power-of-2 to a positive integer. |
58d76dfd JH |
937 | The logic here will work for any base (even non-integer |
938 | bases) but it can be less accurate than | |
939 | pow (base,power) or exp (power * log (base)) when the | |
940 | intermediate values start to spill out of the mantissa. | |
941 | With powers of 2 we know this can't happen. | |
942 | And powers of 2 are the favourite thing for perl | |
943 | programmers to notice ** not doing what they mean. */ | |
944 | NV result = 1.0; | |
945 | NV base = baseuok ? baseuv : -(NV)baseuv; | |
946 | int n = 0; | |
947 | ||
58d76dfd JH |
948 | for (; power; base *= base, n++) { |
949 | /* Do I look like I trust gcc with long longs here? | |
950 | Do I hell. */ | |
951 | UV bit = (UV)1 << (UV)n; | |
952 | if (power & bit) { | |
953 | result *= base; | |
954 | /* Only bother to clear the bit if it is set. */ | |
52a96ae6 | 955 | power -= bit; |
90fcb902 CB |
956 | /* Avoid squaring base again if we're done. */ |
957 | if (power == 0) break; | |
58d76dfd JH |
958 | } |
959 | } | |
960 | SP--; | |
961 | SETn( result ); | |
52a96ae6 | 962 | SvIV_please(TOPs); |
58d76dfd | 963 | RETURN; |
52a96ae6 HS |
964 | } else { |
965 | register unsigned int highbit = 8 * sizeof(UV); | |
966 | register unsigned int lowbit = 0; | |
967 | register unsigned int diff; | |
968 | while ((diff = (highbit - lowbit) >> 1)) { | |
969 | if (baseuv & ~((1 << (lowbit + diff)) - 1)) | |
970 | lowbit += diff; | |
971 | else | |
972 | highbit -= diff; | |
973 | } | |
974 | /* we now have baseuv < 2 ** highbit */ | |
975 | if (power * highbit <= 8 * sizeof(UV)) { | |
976 | /* result will definitely fit in UV, so use UV math | |
977 | on same algorithm as above */ | |
978 | register UV result = 1; | |
979 | register UV base = baseuv; | |
980 | register int n = 0; | |
981 | for (; power; base *= base, n++) { | |
982 | register UV bit = (UV)1 << (UV)n; | |
983 | if (power & bit) { | |
984 | result *= base; | |
985 | power -= bit; | |
986 | if (power == 0) break; | |
987 | } | |
988 | } | |
989 | SP--; | |
990 | if (baseuok || !(power & 1)) | |
991 | /* answer is positive */ | |
992 | SETu( result ); | |
993 | else if (result <= (UV)IV_MAX) | |
994 | /* answer negative, fits in IV */ | |
995 | SETi( -(IV)result ); | |
996 | else if (result == (UV)IV_MIN) | |
997 | /* 2's complement assumption: special case IV_MIN */ | |
998 | SETi( IV_MIN ); | |
999 | else | |
1000 | /* answer negative, doesn't fit */ | |
1001 | SETn( -(NV)result ); | |
1002 | RETURN; | |
1003 | } | |
1004 | } | |
1005 | } | |
1006 | } | |
58d76dfd | 1007 | } |
52a96ae6 | 1008 | float_it: |
58d76dfd | 1009 | #endif |
a0d0e21e | 1010 | { |
52a96ae6 HS |
1011 | dPOPTOPnnrl; |
1012 | SETn( Perl_pow( left, right) ); | |
1013 | #ifdef PERL_PRESERVE_IVUV | |
1014 | if (is_int) | |
1015 | SvIV_please(TOPs); | |
1016 | #endif | |
1017 | RETURN; | |
93a17b20 | 1018 | } |
a0d0e21e LW |
1019 | } |
1020 | ||
1021 | PP(pp_multiply) | |
1022 | { | |
39644a26 | 1023 | dSP; dATARGET; tryAMAGICbin(mult,opASSIGN); |
28e5dec8 JH |
1024 | #ifdef PERL_PRESERVE_IVUV |
1025 | SvIV_please(TOPs); | |
1026 | if (SvIOK(TOPs)) { | |
1027 | /* Unless the left argument is integer in range we are going to have to | |
1028 | use NV maths. Hence only attempt to coerce the right argument if | |
1029 | we know the left is integer. */ | |
1030 | /* Left operand is defined, so is it IV? */ | |
1031 | SvIV_please(TOPm1s); | |
1032 | if (SvIOK(TOPm1s)) { | |
1033 | bool auvok = SvUOK(TOPm1s); | |
1034 | bool buvok = SvUOK(TOPs); | |
1035 | const UV topmask = (~ (UV)0) << (4 * sizeof (UV)); | |
1036 | const UV botmask = ~((~ (UV)0) << (4 * sizeof (UV))); | |
1037 | UV alow; | |
1038 | UV ahigh; | |
1039 | UV blow; | |
1040 | UV bhigh; | |
1041 | ||
1042 | if (auvok) { | |
1043 | alow = SvUVX(TOPm1s); | |
1044 | } else { | |
1045 | IV aiv = SvIVX(TOPm1s); | |
1046 | if (aiv >= 0) { | |
1047 | alow = aiv; | |
1048 | auvok = TRUE; /* effectively it's a UV now */ | |
1049 | } else { | |
1050 | alow = -aiv; /* abs, auvok == false records sign */ | |
1051 | } | |
1052 | } | |
1053 | if (buvok) { | |
1054 | blow = SvUVX(TOPs); | |
1055 | } else { | |
1056 | IV biv = SvIVX(TOPs); | |
1057 | if (biv >= 0) { | |
1058 | blow = biv; | |
1059 | buvok = TRUE; /* effectively it's a UV now */ | |
1060 | } else { | |
1061 | blow = -biv; /* abs, buvok == false records sign */ | |
1062 | } | |
1063 | } | |
1064 | ||
1065 | /* If this does sign extension on unsigned it's time for plan B */ | |
1066 | ahigh = alow >> (4 * sizeof (UV)); | |
1067 | alow &= botmask; | |
1068 | bhigh = blow >> (4 * sizeof (UV)); | |
1069 | blow &= botmask; | |
1070 | if (ahigh && bhigh) { | |
1071 | /* eg 32 bit is at least 0x10000 * 0x10000 == 0x100000000 | |
1072 | which is overflow. Drop to NVs below. */ | |
1073 | } else if (!ahigh && !bhigh) { | |
1074 | /* eg 32 bit is at most 0xFFFF * 0xFFFF == 0xFFFE0001 | |
1075 | so the unsigned multiply cannot overflow. */ | |
1076 | UV product = alow * blow; | |
1077 | if (auvok == buvok) { | |
1078 | /* -ve * -ve or +ve * +ve gives a +ve result. */ | |
1079 | SP--; | |
1080 | SETu( product ); | |
1081 | RETURN; | |
1082 | } else if (product <= (UV)IV_MIN) { | |
1083 | /* 2s complement assumption that (UV)-IV_MIN is correct. */ | |
1084 | /* -ve result, which could overflow an IV */ | |
1085 | SP--; | |
25716404 | 1086 | SETi( -(IV)product ); |
28e5dec8 JH |
1087 | RETURN; |
1088 | } /* else drop to NVs below. */ | |
1089 | } else { | |
1090 | /* One operand is large, 1 small */ | |
1091 | UV product_middle; | |
1092 | if (bhigh) { | |
1093 | /* swap the operands */ | |
1094 | ahigh = bhigh; | |
1095 | bhigh = blow; /* bhigh now the temp var for the swap */ | |
1096 | blow = alow; | |
1097 | alow = bhigh; | |
1098 | } | |
1099 | /* now, ((ahigh * blow) << half_UV_len) + (alow * blow) | |
1100 | multiplies can't overflow. shift can, add can, -ve can. */ | |
1101 | product_middle = ahigh * blow; | |
1102 | if (!(product_middle & topmask)) { | |
1103 | /* OK, (ahigh * blow) won't lose bits when we shift it. */ | |
1104 | UV product_low; | |
1105 | product_middle <<= (4 * sizeof (UV)); | |
1106 | product_low = alow * blow; | |
1107 | ||
1108 | /* as for pp_add, UV + something mustn't get smaller. | |
1109 | IIRC ANSI mandates this wrapping *behaviour* for | |
1110 | unsigned whatever the actual representation*/ | |
1111 | product_low += product_middle; | |
1112 | if (product_low >= product_middle) { | |
1113 | /* didn't overflow */ | |
1114 | if (auvok == buvok) { | |
1115 | /* -ve * -ve or +ve * +ve gives a +ve result. */ | |
1116 | SP--; | |
1117 | SETu( product_low ); | |
1118 | RETURN; | |
1119 | } else if (product_low <= (UV)IV_MIN) { | |
1120 | /* 2s complement assumption again */ | |
1121 | /* -ve result, which could overflow an IV */ | |
1122 | SP--; | |
25716404 | 1123 | SETi( -(IV)product_low ); |
28e5dec8 JH |
1124 | RETURN; |
1125 | } /* else drop to NVs below. */ | |
1126 | } | |
1127 | } /* product_middle too large */ | |
1128 | } /* ahigh && bhigh */ | |
1129 | } /* SvIOK(TOPm1s) */ | |
1130 | } /* SvIOK(TOPs) */ | |
1131 | #endif | |
a0d0e21e LW |
1132 | { |
1133 | dPOPTOPnnrl; | |
1134 | SETn( left * right ); | |
1135 | RETURN; | |
79072805 | 1136 | } |
a0d0e21e LW |
1137 | } |
1138 | ||
1139 | PP(pp_divide) | |
1140 | { | |
39644a26 | 1141 | dSP; dATARGET; tryAMAGICbin(div,opASSIGN); |
5479d192 | 1142 | /* Only try to do UV divide first |
68795e93 | 1143 | if ((SLOPPYDIVIDE is true) or |
5479d192 NC |
1144 | (PERL_PRESERVE_IVUV is true and one or both SV is a UV too large |
1145 | to preserve)) | |
1146 | The assumption is that it is better to use floating point divide | |
1147 | whenever possible, only doing integer divide first if we can't be sure. | |
1148 | If NV_PRESERVES_UV is true then we know at compile time that no UV | |
1149 | can be too large to preserve, so don't need to compile the code to | |
1150 | test the size of UVs. */ | |
1151 | ||
a0d0e21e | 1152 | #ifdef SLOPPYDIVIDE |
5479d192 NC |
1153 | # define PERL_TRY_UV_DIVIDE |
1154 | /* ensure that 20./5. == 4. */ | |
a0d0e21e | 1155 | #else |
5479d192 NC |
1156 | # ifdef PERL_PRESERVE_IVUV |
1157 | # ifndef NV_PRESERVES_UV | |
1158 | # define PERL_TRY_UV_DIVIDE | |
1159 | # endif | |
1160 | # endif | |
a0d0e21e | 1161 | #endif |
5479d192 NC |
1162 | |
1163 | #ifdef PERL_TRY_UV_DIVIDE | |
1164 | SvIV_please(TOPs); | |
1165 | if (SvIOK(TOPs)) { | |
1166 | SvIV_please(TOPm1s); | |
1167 | if (SvIOK(TOPm1s)) { | |
1168 | bool left_non_neg = SvUOK(TOPm1s); | |
1169 | bool right_non_neg = SvUOK(TOPs); | |
1170 | UV left; | |
1171 | UV right; | |
1172 | ||
1173 | if (right_non_neg) { | |
1174 | right = SvUVX(TOPs); | |
1175 | } | |
1176 | else { | |
1177 | IV biv = SvIVX(TOPs); | |
1178 | if (biv >= 0) { | |
1179 | right = biv; | |
1180 | right_non_neg = TRUE; /* effectively it's a UV now */ | |
1181 | } | |
1182 | else { | |
1183 | right = -biv; | |
1184 | } | |
1185 | } | |
1186 | /* historically undef()/0 gives a "Use of uninitialized value" | |
1187 | warning before dieing, hence this test goes here. | |
1188 | If it were immediately before the second SvIV_please, then | |
1189 | DIE() would be invoked before left was even inspected, so | |
1190 | no inpsection would give no warning. */ | |
1191 | if (right == 0) | |
1192 | DIE(aTHX_ "Illegal division by zero"); | |
1193 | ||
1194 | if (left_non_neg) { | |
1195 | left = SvUVX(TOPm1s); | |
1196 | } | |
1197 | else { | |
1198 | IV aiv = SvIVX(TOPm1s); | |
1199 | if (aiv >= 0) { | |
1200 | left = aiv; | |
1201 | left_non_neg = TRUE; /* effectively it's a UV now */ | |
1202 | } | |
1203 | else { | |
1204 | left = -aiv; | |
1205 | } | |
1206 | } | |
1207 | ||
1208 | if (left >= right | |
1209 | #ifdef SLOPPYDIVIDE | |
1210 | /* For sloppy divide we always attempt integer division. */ | |
1211 | #else | |
1212 | /* Otherwise we only attempt it if either or both operands | |
1213 | would not be preserved by an NV. If both fit in NVs | |
0c2ee62a NC |
1214 | we fall through to the NV divide code below. However, |
1215 | as left >= right to ensure integer result here, we know that | |
1216 | we can skip the test on the right operand - right big | |
1217 | enough not to be preserved can't get here unless left is | |
1218 | also too big. */ | |
1219 | ||
1220 | && (left > ((UV)1 << NV_PRESERVES_UV_BITS)) | |
5479d192 NC |
1221 | #endif |
1222 | ) { | |
1223 | /* Integer division can't overflow, but it can be imprecise. */ | |
1224 | UV result = left / right; | |
1225 | if (result * right == left) { | |
1226 | SP--; /* result is valid */ | |
1227 | if (left_non_neg == right_non_neg) { | |
1228 | /* signs identical, result is positive. */ | |
1229 | SETu( result ); | |
1230 | RETURN; | |
1231 | } | |
1232 | /* 2s complement assumption */ | |
1233 | if (result <= (UV)IV_MIN) | |
91f3b821 | 1234 | SETi( -(IV)result ); |
5479d192 NC |
1235 | else { |
1236 | /* It's exact but too negative for IV. */ | |
1237 | SETn( -(NV)result ); | |
1238 | } | |
1239 | RETURN; | |
1240 | } /* tried integer divide but it was not an integer result */ | |
32fdb065 | 1241 | } /* else (PERL_ABS(result) < 1.0) or (both UVs in range for NV) */ |
5479d192 NC |
1242 | } /* left wasn't SvIOK */ |
1243 | } /* right wasn't SvIOK */ | |
1244 | #endif /* PERL_TRY_UV_DIVIDE */ | |
1245 | { | |
1246 | dPOPPOPnnrl; | |
1247 | if (right == 0.0) | |
1248 | DIE(aTHX_ "Illegal division by zero"); | |
1249 | PUSHn( left / right ); | |
1250 | RETURN; | |
79072805 | 1251 | } |
a0d0e21e LW |
1252 | } |
1253 | ||
1254 | PP(pp_modulo) | |
1255 | { | |
39644a26 | 1256 | dSP; dATARGET; tryAMAGICbin(modulo,opASSIGN); |
a0d0e21e | 1257 | { |
9c5ffd7c JH |
1258 | UV left = 0; |
1259 | UV right = 0; | |
dc656993 JH |
1260 | bool left_neg = FALSE; |
1261 | bool right_neg = FALSE; | |
e2c88acc NC |
1262 | bool use_double = FALSE; |
1263 | bool dright_valid = FALSE; | |
9c5ffd7c JH |
1264 | NV dright = 0.0; |
1265 | NV dleft = 0.0; | |
787eafbd | 1266 | |
e2c88acc NC |
1267 | SvIV_please(TOPs); |
1268 | if (SvIOK(TOPs)) { | |
1269 | right_neg = !SvUOK(TOPs); | |
1270 | if (!right_neg) { | |
1271 | right = SvUVX(POPs); | |
1272 | } else { | |
1273 | IV biv = SvIVX(POPs); | |
1274 | if (biv >= 0) { | |
1275 | right = biv; | |
1276 | right_neg = FALSE; /* effectively it's a UV now */ | |
1277 | } else { | |
1278 | right = -biv; | |
1279 | } | |
1280 | } | |
1281 | } | |
1282 | else { | |
787eafbd | 1283 | dright = POPn; |
787eafbd IZ |
1284 | right_neg = dright < 0; |
1285 | if (right_neg) | |
1286 | dright = -dright; | |
e2c88acc NC |
1287 | if (dright < UV_MAX_P1) { |
1288 | right = U_V(dright); | |
1289 | dright_valid = TRUE; /* In case we need to use double below. */ | |
1290 | } else { | |
1291 | use_double = TRUE; | |
1292 | } | |
787eafbd | 1293 | } |
a0d0e21e | 1294 | |
e2c88acc NC |
1295 | /* At this point use_double is only true if right is out of range for |
1296 | a UV. In range NV has been rounded down to nearest UV and | |
1297 | use_double false. */ | |
1298 | SvIV_please(TOPs); | |
1299 | if (!use_double && SvIOK(TOPs)) { | |
1300 | if (SvIOK(TOPs)) { | |
1301 | left_neg = !SvUOK(TOPs); | |
1302 | if (!left_neg) { | |
1303 | left = SvUVX(POPs); | |
1304 | } else { | |
1305 | IV aiv = SvIVX(POPs); | |
1306 | if (aiv >= 0) { | |
1307 | left = aiv; | |
1308 | left_neg = FALSE; /* effectively it's a UV now */ | |
1309 | } else { | |
1310 | left = -aiv; | |
1311 | } | |
1312 | } | |
1313 | } | |
1314 | } | |
787eafbd IZ |
1315 | else { |
1316 | dleft = POPn; | |
787eafbd IZ |
1317 | left_neg = dleft < 0; |
1318 | if (left_neg) | |
1319 | dleft = -dleft; | |
68dc0745 | 1320 | |
e2c88acc NC |
1321 | /* This should be exactly the 5.6 behaviour - if left and right are |
1322 | both in range for UV then use U_V() rather than floor. */ | |
1323 | if (!use_double) { | |
1324 | if (dleft < UV_MAX_P1) { | |
1325 | /* right was in range, so is dleft, so use UVs not double. | |
1326 | */ | |
1327 | left = U_V(dleft); | |
1328 | } | |
1329 | /* left is out of range for UV, right was in range, so promote | |
1330 | right (back) to double. */ | |
1331 | else { | |
1332 | /* The +0.5 is used in 5.6 even though it is not strictly | |
1333 | consistent with the implicit +0 floor in the U_V() | |
1334 | inside the #if 1. */ | |
1335 | dleft = Perl_floor(dleft + 0.5); | |
1336 | use_double = TRUE; | |
1337 | if (dright_valid) | |
1338 | dright = Perl_floor(dright + 0.5); | |
1339 | else | |
1340 | dright = right; | |
1341 | } | |
1342 | } | |
1343 | } | |
787eafbd | 1344 | if (use_double) { |
65202027 | 1345 | NV dans; |
787eafbd | 1346 | |
787eafbd | 1347 | if (!dright) |
cea2e8a9 | 1348 | DIE(aTHX_ "Illegal modulus zero"); |
787eafbd | 1349 | |
65202027 | 1350 | dans = Perl_fmod(dleft, dright); |
787eafbd IZ |
1351 | if ((left_neg != right_neg) && dans) |
1352 | dans = dright - dans; | |
1353 | if (right_neg) | |
1354 | dans = -dans; | |
1355 | sv_setnv(TARG, dans); | |
1356 | } | |
1357 | else { | |
1358 | UV ans; | |
1359 | ||
787eafbd | 1360 | if (!right) |
cea2e8a9 | 1361 | DIE(aTHX_ "Illegal modulus zero"); |
787eafbd IZ |
1362 | |
1363 | ans = left % right; | |
1364 | if ((left_neg != right_neg) && ans) | |
1365 | ans = right - ans; | |
1366 | if (right_neg) { | |
1367 | /* XXX may warn: unary minus operator applied to unsigned type */ | |
1368 | /* could change -foo to be (~foo)+1 instead */ | |
1369 | if (ans <= ~((UV)IV_MAX)+1) | |
1370 | sv_setiv(TARG, ~ans+1); | |
1371 | else | |
65202027 | 1372 | sv_setnv(TARG, -(NV)ans); |
787eafbd IZ |
1373 | } |
1374 | else | |
1375 | sv_setuv(TARG, ans); | |
1376 | } | |
1377 | PUSHTARG; | |
1378 | RETURN; | |
79072805 | 1379 | } |
a0d0e21e | 1380 | } |
79072805 | 1381 | |
a0d0e21e LW |
1382 | PP(pp_repeat) |
1383 | { | |
39644a26 | 1384 | dSP; dATARGET; tryAMAGICbin(repeat,opASSIGN); |
748a9306 | 1385 | { |
467f0320 | 1386 | register IV count = POPi; |
533c011a | 1387 | if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) { |
a0d0e21e LW |
1388 | dMARK; |
1389 | I32 items = SP - MARK; | |
1390 | I32 max; | |
79072805 | 1391 | |
a0d0e21e LW |
1392 | max = items * count; |
1393 | MEXTEND(MARK, max); | |
1394 | if (count > 1) { | |
1395 | while (SP > MARK) { | |
976c8a39 JH |
1396 | #if 0 |
1397 | /* This code was intended to fix 20010809.028: | |
1398 | ||
1399 | $x = 'abcd'; | |
1400 | for (($x =~ /./g) x 2) { | |
1401 | print chop; # "abcdabcd" expected as output. | |
1402 | } | |
1403 | ||
1404 | * but that change (#11635) broke this code: | |
1405 | ||
1406 | $x = [("foo")x2]; # only one "foo" ended up in the anonlist. | |
1407 | ||
1408 | * I can't think of a better fix that doesn't introduce | |
1409 | * an efficiency hit by copying the SVs. The stack isn't | |
1410 | * refcounted, and mortalisation obviously doesn't | |
1411 | * Do The Right Thing when the stack has more than | |
1412 | * one pointer to the same mortal value. | |
1413 | * .robin. | |
1414 | */ | |
e30acc16 RH |
1415 | if (*SP) { |
1416 | *SP = sv_2mortal(newSVsv(*SP)); | |
1417 | SvREADONLY_on(*SP); | |
1418 | } | |
976c8a39 JH |
1419 | #else |
1420 | if (*SP) | |
1421 | SvTEMP_off((*SP)); | |
1422 | #endif | |
a0d0e21e | 1423 | SP--; |
79072805 | 1424 | } |
a0d0e21e LW |
1425 | MARK++; |
1426 | repeatcpy((char*)(MARK + items), (char*)MARK, | |
1427 | items * sizeof(SV*), count - 1); | |
1428 | SP += max; | |
79072805 | 1429 | } |
a0d0e21e LW |
1430 | else if (count <= 0) |
1431 | SP -= items; | |
79072805 | 1432 | } |
a0d0e21e | 1433 | else { /* Note: mark already snarfed by pp_list */ |
dfcb284a | 1434 | SV *tmpstr = POPs; |
a0d0e21e | 1435 | STRLEN len; |
9b877dbb | 1436 | bool isutf; |
a0d0e21e | 1437 | |
a0d0e21e LW |
1438 | SvSetSV(TARG, tmpstr); |
1439 | SvPV_force(TARG, len); | |
9b877dbb | 1440 | isutf = DO_UTF8(TARG); |
8ebc5c01 | 1441 | if (count != 1) { |
1442 | if (count < 1) | |
1443 | SvCUR_set(TARG, 0); | |
1444 | else { | |
1445 | SvGROW(TARG, (count * len) + 1); | |
a0d0e21e | 1446 | repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1); |
8ebc5c01 | 1447 | SvCUR(TARG) *= count; |
7a4c00b4 | 1448 | } |
a0d0e21e | 1449 | *SvEND(TARG) = '\0'; |
a0d0e21e | 1450 | } |
dfcb284a GS |
1451 | if (isutf) |
1452 | (void)SvPOK_only_UTF8(TARG); | |
1453 | else | |
1454 | (void)SvPOK_only(TARG); | |
b80b6069 RH |
1455 | |
1456 | if (PL_op->op_private & OPpREPEAT_DOLIST) { | |
1457 | /* The parser saw this as a list repeat, and there | |
1458 | are probably several items on the stack. But we're | |
1459 | in scalar context, and there's no pp_list to save us | |
1460 | now. So drop the rest of the items -- robin@kitsite.com | |
1461 | */ | |
1462 | dMARK; | |
1463 | SP = MARK; | |
1464 | } | |
a0d0e21e | 1465 | PUSHTARG; |
79072805 | 1466 | } |
a0d0e21e | 1467 | RETURN; |
748a9306 | 1468 | } |
a0d0e21e | 1469 | } |
79072805 | 1470 | |
a0d0e21e LW |
1471 | PP(pp_subtract) |
1472 | { | |
39644a26 | 1473 | dSP; dATARGET; bool useleft; tryAMAGICbin(subtr,opASSIGN); |
28e5dec8 JH |
1474 | useleft = USE_LEFT(TOPm1s); |
1475 | #ifdef PERL_PRESERVE_IVUV | |
7dca457a NC |
1476 | /* See comments in pp_add (in pp_hot.c) about Overflow, and how |
1477 | "bad things" happen if you rely on signed integers wrapping. */ | |
28e5dec8 JH |
1478 | SvIV_please(TOPs); |
1479 | if (SvIOK(TOPs)) { | |
1480 | /* Unless the left argument is integer in range we are going to have to | |
1481 | use NV maths. Hence only attempt to coerce the right argument if | |
1482 | we know the left is integer. */ | |
9c5ffd7c JH |
1483 | register UV auv = 0; |
1484 | bool auvok = FALSE; | |
7dca457a NC |
1485 | bool a_valid = 0; |
1486 | ||
28e5dec8 | 1487 | if (!useleft) { |
7dca457a NC |
1488 | auv = 0; |
1489 | a_valid = auvok = 1; | |
1490 | /* left operand is undef, treat as zero. */ | |
28e5dec8 JH |
1491 | } else { |
1492 | /* Left operand is defined, so is it IV? */ | |
1493 | SvIV_please(TOPm1s); | |
1494 | if (SvIOK(TOPm1s)) { | |
7dca457a NC |
1495 | if ((auvok = SvUOK(TOPm1s))) |
1496 | auv = SvUVX(TOPm1s); | |
1497 | else { | |
1498 | register IV aiv = SvIVX(TOPm1s); | |
1499 | if (aiv >= 0) { | |
1500 | auv = aiv; | |
1501 | auvok = 1; /* Now acting as a sign flag. */ | |
1502 | } else { /* 2s complement assumption for IV_MIN */ | |
1503 | auv = (UV)-aiv; | |
28e5dec8 | 1504 | } |
7dca457a NC |
1505 | } |
1506 | a_valid = 1; | |
1507 | } | |
1508 | } | |
1509 | if (a_valid) { | |
1510 | bool result_good = 0; | |
1511 | UV result; | |
1512 | register UV buv; | |
1513 | bool buvok = SvUOK(TOPs); | |
9041c2e3 | 1514 | |
7dca457a NC |
1515 | if (buvok) |
1516 | buv = SvUVX(TOPs); | |
1517 | else { | |
1518 | register IV biv = SvIVX(TOPs); | |
1519 | if (biv >= 0) { | |
1520 | buv = biv; | |
1521 | buvok = 1; | |
1522 | } else | |
1523 | buv = (UV)-biv; | |
1524 | } | |
1525 | /* ?uvok if value is >= 0. basically, flagged as UV if it's +ve, | |
602f51c4 | 1526 | else "IV" now, independent of how it came in. |
7dca457a NC |
1527 | if a, b represents positive, A, B negative, a maps to -A etc |
1528 | a - b => (a - b) | |
1529 | A - b => -(a + b) | |
1530 | a - B => (a + b) | |
1531 | A - B => -(a - b) | |
1532 | all UV maths. negate result if A negative. | |
1533 | subtract if signs same, add if signs differ. */ | |
1534 | ||
1535 | if (auvok ^ buvok) { | |
1536 | /* Signs differ. */ | |
1537 | result = auv + buv; | |
1538 | if (result >= auv) | |
1539 | result_good = 1; | |
1540 | } else { | |
1541 | /* Signs same */ | |
1542 | if (auv >= buv) { | |
1543 | result = auv - buv; | |
1544 | /* Must get smaller */ | |
1545 | if (result <= auv) | |
1546 | result_good = 1; | |
1547 | } else { | |
1548 | result = buv - auv; | |
1549 | if (result <= buv) { | |
1550 | /* result really should be -(auv-buv). as its negation | |
1551 | of true value, need to swap our result flag */ | |
1552 | auvok = !auvok; | |
1553 | result_good = 1; | |
28e5dec8 | 1554 | } |
28e5dec8 JH |
1555 | } |
1556 | } | |
7dca457a NC |
1557 | if (result_good) { |
1558 | SP--; | |
1559 | if (auvok) | |
1560 | SETu( result ); | |
1561 | else { | |
1562 | /* Negate result */ | |
1563 | if (result <= (UV)IV_MIN) | |
1564 | SETi( -(IV)result ); | |
1565 | else { | |
1566 | /* result valid, but out of range for IV. */ | |
1567 | SETn( -(NV)result ); | |
1568 | } | |
1569 | } | |
1570 | RETURN; | |
1571 | } /* Overflow, drop through to NVs. */ | |
28e5dec8 JH |
1572 | } |
1573 | } | |
1574 | #endif | |
7dca457a | 1575 | useleft = USE_LEFT(TOPm1s); |
a0d0e21e | 1576 | { |
28e5dec8 JH |
1577 | dPOPnv; |
1578 | if (!useleft) { | |
1579 | /* left operand is undef, treat as zero - value */ | |
1580 | SETn(-value); | |
1581 | RETURN; | |
1582 | } | |
1583 | SETn( TOPn - value ); | |
1584 | RETURN; | |
79072805 | 1585 | } |
a0d0e21e | 1586 | } |
79072805 | 1587 | |
a0d0e21e LW |
1588 | PP(pp_left_shift) |
1589 | { | |
39644a26 | 1590 | dSP; dATARGET; tryAMAGICbin(lshift,opASSIGN); |
a0d0e21e | 1591 | { |
972b05a9 | 1592 | IV shift = POPi; |
d0ba1bd2 | 1593 | if (PL_op->op_private & HINT_INTEGER) { |
972b05a9 JH |
1594 | IV i = TOPi; |
1595 | SETi(i << shift); | |
d0ba1bd2 JH |
1596 | } |
1597 | else { | |
972b05a9 JH |
1598 | UV u = TOPu; |
1599 | SETu(u << shift); | |
d0ba1bd2 | 1600 | } |
55497cff | 1601 | RETURN; |
79072805 | 1602 | } |
a0d0e21e | 1603 | } |
79072805 | 1604 | |
a0d0e21e LW |
1605 | PP(pp_right_shift) |
1606 | { | |
39644a26 | 1607 | dSP; dATARGET; tryAMAGICbin(rshift,opASSIGN); |
a0d0e21e | 1608 | { |
972b05a9 | 1609 | IV shift = POPi; |
d0ba1bd2 | 1610 | if (PL_op->op_private & HINT_INTEGER) { |
972b05a9 JH |
1611 | IV i = TOPi; |
1612 | SETi(i >> shift); | |
d0ba1bd2 JH |
1613 | } |
1614 | else { | |
972b05a9 JH |
1615 | UV u = TOPu; |
1616 | SETu(u >> shift); | |
d0ba1bd2 | 1617 | } |
a0d0e21e | 1618 | RETURN; |
93a17b20 | 1619 | } |
79072805 LW |
1620 | } |
1621 | ||
a0d0e21e | 1622 | PP(pp_lt) |
79072805 | 1623 | { |
39644a26 | 1624 | dSP; tryAMAGICbinSET(lt,0); |
28e5dec8 JH |
1625 | #ifdef PERL_PRESERVE_IVUV |
1626 | SvIV_please(TOPs); | |
1627 | if (SvIOK(TOPs)) { | |
1628 | SvIV_please(TOPm1s); | |
1629 | if (SvIOK(TOPm1s)) { | |
1630 | bool auvok = SvUOK(TOPm1s); | |
1631 | bool buvok = SvUOK(TOPs); | |
a227d84d | 1632 | |
28e5dec8 JH |
1633 | if (!auvok && !buvok) { /* ## IV < IV ## */ |
1634 | IV aiv = SvIVX(TOPm1s); | |
1635 | IV biv = SvIVX(TOPs); | |
1636 | ||
1637 | SP--; | |
1638 | SETs(boolSV(aiv < biv)); | |
1639 | RETURN; | |
1640 | } | |
1641 | if (auvok && buvok) { /* ## UV < UV ## */ | |
1642 | UV auv = SvUVX(TOPm1s); | |
1643 | UV buv = SvUVX(TOPs); | |
1644 | ||
1645 | SP--; | |
1646 | SETs(boolSV(auv < buv)); | |
1647 | RETURN; | |
1648 | } | |
1649 | if (auvok) { /* ## UV < IV ## */ | |
1650 | UV auv; | |
1651 | IV biv; | |
1652 | ||
1653 | biv = SvIVX(TOPs); | |
1654 | SP--; | |
1655 | if (biv < 0) { | |
1656 | /* As (a) is a UV, it's >=0, so it cannot be < */ | |
1657 | SETs(&PL_sv_no); | |
1658 | RETURN; | |
1659 | } | |
1660 | auv = SvUVX(TOPs); | |
28e5dec8 JH |
1661 | SETs(boolSV(auv < (UV)biv)); |
1662 | RETURN; | |
1663 | } | |
1664 | { /* ## IV < UV ## */ | |
1665 | IV aiv; | |
1666 | UV buv; | |
1667 | ||
1668 | aiv = SvIVX(TOPm1s); | |
1669 | if (aiv < 0) { | |
1670 | /* As (b) is a UV, it's >=0, so it must be < */ | |
1671 | SP--; | |
1672 | SETs(&PL_sv_yes); | |
1673 | RETURN; | |
1674 | } | |
1675 | buv = SvUVX(TOPs); | |
1676 | SP--; | |
28e5dec8 JH |
1677 | SETs(boolSV((UV)aiv < buv)); |
1678 | RETURN; | |
1679 | } | |
1680 | } | |
1681 | } | |
1682 | #endif | |
30de85b6 | 1683 | #ifndef NV_PRESERVES_UV |
50fb3111 NC |
1684 | #ifdef PERL_PRESERVE_IVUV |
1685 | else | |
1686 | #endif | |
1687 | if (SvROK(TOPs) && SvROK(TOPm1s)) { | |
1688 | SP--; | |
1689 | SETs(boolSV(SvRV(TOPs) < SvRV(TOPp1s))); | |
1690 | RETURN; | |
1691 | } | |
30de85b6 | 1692 | #endif |
a0d0e21e LW |
1693 | { |
1694 | dPOPnv; | |
54310121 | 1695 | SETs(boolSV(TOPn < value)); |
a0d0e21e | 1696 | RETURN; |
79072805 | 1697 | } |
a0d0e21e | 1698 | } |
79072805 | 1699 | |
a0d0e21e LW |
1700 | PP(pp_gt) |
1701 | { | |
39644a26 | 1702 | dSP; tryAMAGICbinSET(gt,0); |
28e5dec8 JH |
1703 | #ifdef PERL_PRESERVE_IVUV |
1704 | SvIV_please(TOPs); | |
1705 | if (SvIOK(TOPs)) { | |
1706 | SvIV_please(TOPm1s); | |
1707 | if (SvIOK(TOPm1s)) { | |
1708 | bool auvok = SvUOK(TOPm1s); | |
1709 | bool buvok = SvUOK(TOPs); | |
a227d84d | 1710 | |
28e5dec8 JH |
1711 | if (!auvok && !buvok) { /* ## IV > IV ## */ |
1712 | IV aiv = SvIVX(TOPm1s); | |
1713 | IV biv = SvIVX(TOPs); | |
1714 | ||
1715 | SP--; | |
1716 | SETs(boolSV(aiv > biv)); | |
1717 | RETURN; | |
1718 | } | |
1719 | if (auvok && buvok) { /* ## UV > UV ## */ | |
1720 | UV auv = SvUVX(TOPm1s); | |
1721 | UV buv = SvUVX(TOPs); | |
1722 | ||
1723 | SP--; | |
1724 | SETs(boolSV(auv > buv)); | |
1725 | RETURN; | |
1726 | } | |
1727 | if (auvok) { /* ## UV > IV ## */ | |
1728 | UV auv; | |
1729 | IV biv; | |
1730 | ||
1731 | biv = SvIVX(TOPs); | |
1732 | SP--; | |
1733 | if (biv < 0) { | |
1734 | /* As (a) is a UV, it's >=0, so it must be > */ | |
1735 | SETs(&PL_sv_yes); | |
1736 | RETURN; | |
1737 | } | |
1738 | auv = SvUVX(TOPs); | |
28e5dec8 JH |
1739 | SETs(boolSV(auv > (UV)biv)); |
1740 | RETURN; | |
1741 | } | |
1742 | { /* ## IV > UV ## */ | |
1743 | IV aiv; | |
1744 | UV buv; | |
1745 | ||
1746 | aiv = SvIVX(TOPm1s); | |
1747 | if (aiv < 0) { | |
1748 | /* As (b) is a UV, it's >=0, so it cannot be > */ | |
1749 | SP--; | |
1750 | SETs(&PL_sv_no); | |
1751 | RETURN; | |
1752 | } | |
1753 | buv = SvUVX(TOPs); | |
1754 | SP--; | |
28e5dec8 JH |
1755 | SETs(boolSV((UV)aiv > buv)); |
1756 | RETURN; | |
1757 | } | |
1758 | } | |
1759 | } | |
1760 | #endif | |
30de85b6 | 1761 | #ifndef NV_PRESERVES_UV |
50fb3111 NC |
1762 | #ifdef PERL_PRESERVE_IVUV |
1763 | else | |
1764 | #endif | |
1765 | if (SvROK(TOPs) && SvROK(TOPm1s)) { | |
30de85b6 NC |
1766 | SP--; |
1767 | SETs(boolSV(SvRV(TOPs) > SvRV(TOPp1s))); | |
1768 | RETURN; | |
1769 | } | |
1770 | #endif | |
a0d0e21e LW |
1771 | { |
1772 | dPOPnv; | |
54310121 | 1773 | SETs(boolSV(TOPn > value)); |
a0d0e21e | 1774 | RETURN; |
79072805 | 1775 | } |
a0d0e21e LW |
1776 | } |
1777 | ||
1778 | PP(pp_le) | |
1779 | { | |
39644a26 | 1780 | dSP; tryAMAGICbinSET(le,0); |
28e5dec8 JH |
1781 | #ifdef PERL_PRESERVE_IVUV |
1782 | SvIV_please(TOPs); | |
1783 | if (SvIOK(TOPs)) { | |
1784 | SvIV_please(TOPm1s); | |
1785 | if (SvIOK(TOPm1s)) { | |
1786 | bool auvok = SvUOK(TOPm1s); | |
1787 | bool buvok = SvUOK(TOPs); | |
a227d84d | 1788 | |
28e5dec8 JH |
1789 | if (!auvok && !buvok) { /* ## IV <= IV ## */ |
1790 | IV aiv = SvIVX(TOPm1s); | |
1791 | IV biv = SvIVX(TOPs); | |
1792 | ||
1793 | SP--; | |
1794 | SETs(boolSV(aiv <= biv)); | |
1795 | RETURN; | |
1796 | } | |
1797 | if (auvok && buvok) { /* ## UV <= UV ## */ | |
1798 | UV auv = SvUVX(TOPm1s); | |
1799 | UV buv = SvUVX(TOPs); | |
1800 | ||
1801 | SP--; | |
1802 | SETs(boolSV(auv <= buv)); | |
1803 | RETURN; | |
1804 | } | |
1805 | if (auvok) { /* ## UV <= IV ## */ | |
1806 | UV auv; | |
1807 | IV biv; | |
1808 | ||
1809 | biv = SvIVX(TOPs); | |
1810 | SP--; | |
1811 | if (biv < 0) { | |
1812 | /* As (a) is a UV, it's >=0, so a cannot be <= */ | |
1813 | SETs(&PL_sv_no); | |
1814 | RETURN; | |
1815 | } | |
1816 | auv = SvUVX(TOPs); | |
28e5dec8 JH |
1817 | SETs(boolSV(auv <= (UV)biv)); |
1818 | RETURN; | |
1819 | } | |
1820 | { /* ## IV <= UV ## */ | |
1821 | IV aiv; | |
1822 | UV buv; | |
1823 | ||
1824 | aiv = SvIVX(TOPm1s); | |
1825 | if (aiv < 0) { | |
1826 | /* As (b) is a UV, it's >=0, so a must be <= */ | |
1827 | SP--; | |
1828 | SETs(&PL_sv_yes); | |
1829 | RETURN; | |
1830 | } | |
1831 | buv = SvUVX(TOPs); | |
1832 | SP--; | |
28e5dec8 JH |
1833 | SETs(boolSV((UV)aiv <= buv)); |
1834 | RETURN; | |
1835 | } | |
1836 | } | |
1837 | } | |
1838 | #endif | |
30de85b6 | 1839 | #ifndef NV_PRESERVES_UV |
50fb3111 NC |
1840 | #ifdef PERL_PRESERVE_IVUV |
1841 | else | |
1842 | #endif | |
1843 | if (SvROK(TOPs) && SvROK(TOPm1s)) { | |
30de85b6 NC |
1844 | SP--; |
1845 | SETs(boolSV(SvRV(TOPs) <= SvRV(TOPp1s))); | |
1846 | RETURN; | |
1847 | } | |
1848 | #endif | |
a0d0e21e LW |
1849 | { |
1850 | dPOPnv; | |
54310121 | 1851 | SETs(boolSV(TOPn <= value)); |
a0d0e21e | 1852 | RETURN; |
79072805 | 1853 | } |
a0d0e21e LW |
1854 | } |
1855 | ||
1856 | PP(pp_ge) | |
1857 | { | |
39644a26 | 1858 | dSP; tryAMAGICbinSET(ge,0); |
28e5dec8 JH |
1859 | #ifdef PERL_PRESERVE_IVUV |
1860 | SvIV_please(TOPs); | |
1861 | if (SvIOK(TOPs)) { | |
1862 | SvIV_please(TOPm1s); | |
1863 | if (SvIOK(TOPm1s)) { | |
1864 | bool auvok = SvUOK(TOPm1s); | |
1865 | bool buvok = SvUOK(TOPs); | |
a227d84d | 1866 | |
28e5dec8 JH |
1867 | if (!auvok && !buvok) { /* ## IV >= IV ## */ |
1868 | IV aiv = SvIVX(TOPm1s); | |
1869 | IV biv = SvIVX(TOPs); | |
1870 | ||
1871 | SP--; | |
1872 | SETs(boolSV(aiv >= biv)); | |
1873 | RETURN; | |
1874 | } | |
1875 | if (auvok && buvok) { /* ## UV >= UV ## */ | |
1876 | UV auv = SvUVX(TOPm1s); | |
1877 | UV buv = SvUVX(TOPs); | |
1878 | ||
1879 | SP--; | |
1880 | SETs(boolSV(auv >= buv)); | |
1881 | RETURN; | |
1882 | } | |
1883 | if (auvok) { /* ## UV >= IV ## */ | |
1884 | UV auv; | |
1885 | IV biv; | |
1886 | ||
1887 | biv = SvIVX(TOPs); | |
1888 | SP--; | |
1889 | if (biv < 0) { | |
1890 | /* As (a) is a UV, it's >=0, so it must be >= */ | |
1891 | SETs(&PL_sv_yes); | |
1892 | RETURN; | |
1893 | } | |
1894 | auv = SvUVX(TOPs); | |
28e5dec8 JH |
1895 | SETs(boolSV(auv >= (UV)biv)); |
1896 | RETURN; | |
1897 | } | |
1898 | { /* ## IV >= UV ## */ | |
1899 | IV aiv; | |
1900 | UV buv; | |
1901 | ||
1902 | aiv = SvIVX(TOPm1s); | |
1903 | if (aiv < 0) { | |
1904 | /* As (b) is a UV, it's >=0, so a cannot be >= */ | |
1905 | SP--; | |
1906 | SETs(&PL_sv_no); | |
1907 | RETURN; | |
1908 | } | |
1909 | buv = SvUVX(TOPs); | |
1910 | SP--; | |
28e5dec8 JH |
1911 | SETs(boolSV((UV)aiv >= buv)); |
1912 | RETURN; | |
1913 | } | |
1914 | } | |
1915 | } | |
1916 | #endif | |
30de85b6 | 1917 | #ifndef NV_PRESERVES_UV |
50fb3111 NC |
1918 | #ifdef PERL_PRESERVE_IVUV |
1919 | else | |
1920 | #endif | |
1921 | if (SvROK(TOPs) && SvROK(TOPm1s)) { | |
30de85b6 NC |
1922 | SP--; |
1923 | SETs(boolSV(SvRV(TOPs) >= SvRV(TOPp1s))); | |
1924 | RETURN; | |
1925 | } | |
1926 | #endif | |
a0d0e21e LW |
1927 | { |
1928 | dPOPnv; | |
54310121 | 1929 | SETs(boolSV(TOPn >= value)); |
a0d0e21e | 1930 | RETURN; |
79072805 | 1931 | } |
a0d0e21e | 1932 | } |
79072805 | 1933 | |
a0d0e21e LW |
1934 | PP(pp_ne) |
1935 | { | |
16303949 | 1936 | dSP; tryAMAGICbinSET(ne,0); |
3bb2c415 JH |
1937 | #ifndef NV_PRESERVES_UV |
1938 | if (SvROK(TOPs) && SvROK(TOPm1s)) { | |
e61d22ef NC |
1939 | SP--; |
1940 | SETs(boolSV(SvRV(TOPs) != SvRV(TOPp1s))); | |
3bb2c415 JH |
1941 | RETURN; |
1942 | } | |
1943 | #endif | |
28e5dec8 JH |
1944 | #ifdef PERL_PRESERVE_IVUV |
1945 | SvIV_please(TOPs); | |
1946 | if (SvIOK(TOPs)) { | |
1947 | SvIV_please(TOPm1s); | |
1948 | if (SvIOK(TOPm1s)) { | |
1949 | bool auvok = SvUOK(TOPm1s); | |
1950 | bool buvok = SvUOK(TOPs); | |
a227d84d | 1951 | |
30de85b6 NC |
1952 | if (auvok == buvok) { /* ## IV == IV or UV == UV ## */ |
1953 | /* Casting IV to UV before comparison isn't going to matter | |
1954 | on 2s complement. On 1s complement or sign&magnitude | |
1955 | (if we have any of them) it could make negative zero | |
1956 | differ from normal zero. As I understand it. (Need to | |
1957 | check - is negative zero implementation defined behaviour | |
1958 | anyway?). NWC */ | |
1959 | UV buv = SvUVX(POPs); | |
1960 | UV auv = SvUVX(TOPs); | |
28e5dec8 | 1961 | |
28e5dec8 JH |
1962 | SETs(boolSV(auv != buv)); |
1963 | RETURN; | |
1964 | } | |
1965 | { /* ## Mixed IV,UV ## */ | |
1966 | IV iv; | |
1967 | UV uv; | |
1968 | ||
1969 | /* != is commutative so swap if needed (save code) */ | |
1970 | if (auvok) { | |
1971 | /* swap. top of stack (b) is the iv */ | |
1972 | iv = SvIVX(TOPs); | |
1973 | SP--; | |
1974 | if (iv < 0) { | |
1975 | /* As (a) is a UV, it's >0, so it cannot be == */ | |
1976 | SETs(&PL_sv_yes); | |
1977 | RETURN; | |
1978 | } | |
1979 | uv = SvUVX(TOPs); | |
1980 | } else { | |
1981 | iv = SvIVX(TOPm1s); | |
1982 | SP--; | |
1983 | if (iv < 0) { | |
1984 | /* As (b) is a UV, it's >0, so it cannot be == */ | |
1985 | SETs(&PL_sv_yes); | |
1986 | RETURN; | |
1987 | } | |
1988 | uv = SvUVX(*(SP+1)); /* Do I want TOPp1s() ? */ | |
1989 | } | |
28e5dec8 JH |
1990 | SETs(boolSV((UV)iv != uv)); |
1991 | RETURN; | |
1992 | } | |
1993 | } | |
1994 | } | |
1995 | #endif | |
a0d0e21e LW |
1996 | { |
1997 | dPOPnv; | |
54310121 | 1998 | SETs(boolSV(TOPn != value)); |
a0d0e21e LW |
1999 | RETURN; |
2000 | } | |
79072805 LW |
2001 | } |
2002 | ||
a0d0e21e | 2003 | PP(pp_ncmp) |
79072805 | 2004 | { |
39644a26 | 2005 | dSP; dTARGET; tryAMAGICbin(ncmp,0); |
d8c7644e JH |
2006 | #ifndef NV_PRESERVES_UV |
2007 | if (SvROK(TOPs) && SvROK(TOPm1s)) { | |
e61d22ef NC |
2008 | UV right = PTR2UV(SvRV(POPs)); |
2009 | UV left = PTR2UV(SvRV(TOPs)); | |
2010 | SETi((left > right) - (left < right)); | |
d8c7644e JH |
2011 | RETURN; |
2012 | } | |
2013 | #endif | |
28e5dec8 JH |
2014 | #ifdef PERL_PRESERVE_IVUV |
2015 | /* Fortunately it seems NaN isn't IOK */ | |
2016 | SvIV_please(TOPs); | |
2017 | if (SvIOK(TOPs)) { | |
2018 | SvIV_please(TOPm1s); | |
2019 | if (SvIOK(TOPm1s)) { | |
2020 | bool leftuvok = SvUOK(TOPm1s); | |
2021 | bool rightuvok = SvUOK(TOPs); | |
2022 | I32 value; | |
2023 | if (!leftuvok && !rightuvok) { /* ## IV <=> IV ## */ | |
2024 | IV leftiv = SvIVX(TOPm1s); | |
2025 | IV rightiv = SvIVX(TOPs); | |
2026 | ||
2027 | if (leftiv > rightiv) | |
2028 | value = 1; | |
2029 | else if (leftiv < rightiv) | |
2030 | value = -1; | |
2031 | else | |
2032 | value = 0; | |
2033 | } else if (leftuvok && rightuvok) { /* ## UV <=> UV ## */ | |
2034 | UV leftuv = SvUVX(TOPm1s); | |
2035 | UV rightuv = SvUVX(TOPs); | |
2036 | ||
2037 | if (leftuv > rightuv) | |
2038 | value = 1; | |
2039 | else if (leftuv < rightuv) | |
2040 | value = -1; | |
2041 | else | |
2042 | value = 0; | |
2043 | } else if (leftuvok) { /* ## UV <=> IV ## */ | |
2044 | UV leftuv; | |
2045 | IV rightiv; | |
2046 | ||
2047 | rightiv = SvIVX(TOPs); | |
2048 | if (rightiv < 0) { | |
2049 | /* As (a) is a UV, it's >=0, so it cannot be < */ | |
2050 | value = 1; | |
2051 | } else { | |
2052 | leftuv = SvUVX(TOPm1s); | |
83bac5dd | 2053 | if (leftuv > (UV)rightiv) { |
28e5dec8 JH |
2054 | value = 1; |
2055 | } else if (leftuv < (UV)rightiv) { | |
2056 | value = -1; | |
2057 | } else { | |
2058 | value = 0; | |
2059 | } | |
2060 | } | |
2061 | } else { /* ## IV <=> UV ## */ | |
2062 | IV leftiv; | |
2063 | UV rightuv; | |
2064 | ||
2065 | leftiv = SvIVX(TOPm1s); | |
2066 | if (leftiv < 0) { | |
2067 | /* As (b) is a UV, it's >=0, so it must be < */ | |
2068 | value = -1; | |
2069 | } else { | |
2070 | rightuv = SvUVX(TOPs); | |
83bac5dd | 2071 | if ((UV)leftiv > rightuv) { |
28e5dec8 | 2072 | value = 1; |
83bac5dd | 2073 | } else if ((UV)leftiv < rightuv) { |
28e5dec8 JH |
2074 | value = -1; |
2075 | } else { | |
2076 | value = 0; | |
2077 | } | |
2078 | } | |
2079 | } | |
2080 | SP--; | |
2081 | SETi(value); | |
2082 | RETURN; | |
2083 | } | |
2084 | } | |
2085 | #endif | |
a0d0e21e LW |
2086 | { |
2087 | dPOPTOPnnrl; | |
2088 | I32 value; | |
79072805 | 2089 | |
a3540c92 | 2090 | #ifdef Perl_isnan |
1ad04cfd JH |
2091 | if (Perl_isnan(left) || Perl_isnan(right)) { |
2092 | SETs(&PL_sv_undef); | |
2093 | RETURN; | |
2094 | } | |
2095 | value = (left > right) - (left < right); | |
2096 | #else | |
ff0cee69 | 2097 | if (left == right) |
a0d0e21e | 2098 | value = 0; |
a0d0e21e LW |
2099 | else if (left < right) |
2100 | value = -1; | |
44a8e56a | 2101 | else if (left > right) |
2102 | value = 1; | |
2103 | else { | |
3280af22 | 2104 | SETs(&PL_sv_undef); |
44a8e56a | 2105 | RETURN; |
2106 | } | |
1ad04cfd | 2107 | #endif |
a0d0e21e LW |
2108 | SETi(value); |
2109 | RETURN; | |
79072805 | 2110 | } |
a0d0e21e | 2111 | } |
79072805 | 2112 | |
a0d0e21e LW |
2113 | PP(pp_slt) |
2114 | { | |
39644a26 | 2115 | dSP; tryAMAGICbinSET(slt,0); |
a0d0e21e LW |
2116 | { |
2117 | dPOPTOPssrl; | |
2de3dbcc | 2118 | int cmp = (IN_LOCALE_RUNTIME |
bbce6d69 | 2119 | ? sv_cmp_locale(left, right) |
2120 | : sv_cmp(left, right)); | |
54310121 | 2121 | SETs(boolSV(cmp < 0)); |
a0d0e21e LW |
2122 | RETURN; |
2123 | } | |
79072805 LW |
2124 | } |
2125 | ||
a0d0e21e | 2126 | PP(pp_sgt) |
79072805 | 2127 | { |
39644a26 | 2128 | dSP; tryAMAGICbinSET(sgt,0); |
a0d0e21e LW |
2129 | { |
2130 | dPOPTOPssrl; | |
2de3dbcc | 2131 | int cmp = (IN_LOCALE_RUNTIME |
bbce6d69 | 2132 | ? sv_cmp_locale(left, right) |
2133 | : sv_cmp(left, right)); | |
54310121 | 2134 | SETs(boolSV(cmp > 0)); |
a0d0e21e LW |
2135 | RETURN; |
2136 | } | |
2137 | } | |
79072805 | 2138 | |
a0d0e21e LW |
2139 | PP(pp_sle) |
2140 | { | |
39644a26 | 2141 | dSP; tryAMAGICbinSET(sle,0); |
a0d0e21e LW |
2142 | { |
2143 | dPOPTOPssrl; | |
2de3dbcc | 2144 | int cmp = (IN_LOCALE_RUNTIME |
bbce6d69 | 2145 | ? sv_cmp_locale(left, right) |
2146 | : sv_cmp(left, right)); | |
54310121 | 2147 | SETs(boolSV(cmp <= 0)); |
a0d0e21e | 2148 | RETURN; |
79072805 | 2149 | } |
79072805 LW |
2150 | } |
2151 | ||
a0d0e21e LW |
2152 | PP(pp_sge) |
2153 | { | |
39644a26 | 2154 | dSP; tryAMAGICbinSET(sge,0); |
a0d0e21e LW |
2155 | { |
2156 | dPOPTOPssrl; | |
2de3dbcc | 2157 | int cmp = (IN_LOCALE_RUNTIME |
bbce6d69 | 2158 | ? sv_cmp_locale(left, right) |
2159 | : sv_cmp(left, right)); | |
54310121 | 2160 | SETs(boolSV(cmp >= 0)); |
a0d0e21e LW |
2161 | RETURN; |
2162 | } | |
2163 | } | |
79072805 | 2164 | |
36477c24 | 2165 | PP(pp_seq) |
2166 | { | |
39644a26 | 2167 | dSP; tryAMAGICbinSET(seq,0); |
36477c24 | 2168 | { |
2169 | dPOPTOPssrl; | |
54310121 | 2170 | SETs(boolSV(sv_eq(left, right))); |
a0d0e21e LW |
2171 | RETURN; |
2172 | } | |
2173 | } | |
79072805 | 2174 | |
a0d0e21e | 2175 | PP(pp_sne) |
79072805 | 2176 | { |
39644a26 | 2177 | dSP; tryAMAGICbinSET(sne,0); |
a0d0e21e LW |
2178 | { |
2179 | dPOPTOPssrl; | |
54310121 | 2180 | SETs(boolSV(!sv_eq(left, right))); |
a0d0e21e | 2181 | RETURN; |
463ee0b2 | 2182 | } |
79072805 LW |
2183 | } |
2184 | ||
a0d0e21e | 2185 | PP(pp_scmp) |
79072805 | 2186 | { |
39644a26 | 2187 | dSP; dTARGET; tryAMAGICbin(scmp,0); |
a0d0e21e LW |
2188 | { |
2189 | dPOPTOPssrl; | |
2de3dbcc | 2190 | int cmp = (IN_LOCALE_RUNTIME |
bbce6d69 | 2191 | ? sv_cmp_locale(left, right) |
2192 | : sv_cmp(left, right)); | |
2193 | SETi( cmp ); | |
a0d0e21e LW |
2194 | RETURN; |
2195 | } | |
2196 | } | |
79072805 | 2197 | |
55497cff | 2198 | PP(pp_bit_and) |
2199 | { | |
39644a26 | 2200 | dSP; dATARGET; tryAMAGICbin(band,opASSIGN); |
a0d0e21e LW |
2201 | { |
2202 | dPOPTOPssrl; | |
4633a7c4 | 2203 | if (SvNIOKp(left) || SvNIOKp(right)) { |
d0ba1bd2 | 2204 | if (PL_op->op_private & HINT_INTEGER) { |
972b05a9 JH |
2205 | IV i = SvIV(left) & SvIV(right); |
2206 | SETi(i); | |
d0ba1bd2 JH |
2207 | } |
2208 | else { | |
972b05a9 JH |
2209 | UV u = SvUV(left) & SvUV(right); |
2210 | SETu(u); | |
d0ba1bd2 | 2211 | } |
a0d0e21e LW |
2212 | } |
2213 | else { | |
533c011a | 2214 | do_vop(PL_op->op_type, TARG, left, right); |
a0d0e21e LW |
2215 | SETTARG; |
2216 | } | |
2217 | RETURN; | |
2218 | } | |
2219 | } | |
79072805 | 2220 | |
a0d0e21e LW |
2221 | PP(pp_bit_xor) |
2222 | { | |
39644a26 | 2223 | dSP; dATARGET; tryAMAGICbin(bxor,opASSIGN); |
a0d0e21e LW |
2224 | { |
2225 | dPOPTOPssrl; | |
4633a7c4 | 2226 | if (SvNIOKp(left) || SvNIOKp(right)) { |
d0ba1bd2 | 2227 | if (PL_op->op_private & HINT_INTEGER) { |
972b05a9 JH |
2228 | IV i = (USE_LEFT(left) ? SvIV(left) : 0) ^ SvIV(right); |
2229 | SETi(i); | |
d0ba1bd2 JH |
2230 | } |
2231 | else { | |
972b05a9 JH |
2232 | UV u = (USE_LEFT(left) ? SvUV(left) : 0) ^ SvUV(right); |
2233 | SETu(u); | |
d0ba1bd2 | 2234 | } |
a0d0e21e LW |
2235 | } |
2236 | else { | |
533c011a | 2237 | do_vop(PL_op->op_type, TARG, left, right); |
a0d0e21e LW |
2238 | SETTARG; |
2239 | } | |
2240 | RETURN; | |
2241 | } | |
2242 | } | |
79072805 | 2243 | |
a0d0e21e LW |
2244 | PP(pp_bit_or) |
2245 | { | |
39644a26 | 2246 | dSP; dATARGET; tryAMAGICbin(bor,opASSIGN); |
a0d0e21e LW |
2247 | { |
2248 | dPOPTOPssrl; | |
4633a7c4 | 2249 | if (SvNIOKp(left) || SvNIOKp(right)) { |
d0ba1bd2 | 2250 | if (PL_op->op_private & HINT_INTEGER) { |
972b05a9 JH |
2251 | IV i = (USE_LEFT(left) ? SvIV(left) : 0) | SvIV(right); |
2252 | SETi(i); | |
d0ba1bd2 JH |
2253 | } |
2254 | else { | |
972b05a9 JH |
2255 | UV u = (USE_LEFT(left) ? SvUV(left) : 0) | SvUV(right); |
2256 | SETu(u); | |
d0ba1bd2 | 2257 | } |
a0d0e21e LW |
2258 | } |
2259 | else { | |
533c011a | 2260 | do_vop(PL_op->op_type, TARG, left, right); |
a0d0e21e LW |
2261 | SETTARG; |
2262 | } | |
2263 | RETURN; | |
79072805 | 2264 | } |
a0d0e21e | 2265 | } |
79072805 | 2266 | |
a0d0e21e LW |
2267 | PP(pp_negate) |
2268 | { | |
39644a26 | 2269 | dSP; dTARGET; tryAMAGICun(neg); |
a0d0e21e LW |
2270 | { |
2271 | dTOPss; | |
28e5dec8 | 2272 | int flags = SvFLAGS(sv); |
4633a7c4 LW |
2273 | if (SvGMAGICAL(sv)) |
2274 | mg_get(sv); | |
28e5dec8 JH |
2275 | if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) { |
2276 | /* It's publicly an integer, or privately an integer-not-float */ | |
2277 | oops_its_an_int: | |
9b0e499b GS |
2278 | if (SvIsUV(sv)) { |
2279 | if (SvIVX(sv) == IV_MIN) { | |
28e5dec8 | 2280 | /* 2s complement assumption. */ |
9b0e499b GS |
2281 | SETi(SvIVX(sv)); /* special case: -((UV)IV_MAX+1) == IV_MIN */ |
2282 | RETURN; | |
2283 | } | |
2284 | else if (SvUVX(sv) <= IV_MAX) { | |
beccb14c | 2285 | SETi(-SvIVX(sv)); |
9b0e499b GS |
2286 | RETURN; |
2287 | } | |
2288 | } | |
2289 | else if (SvIVX(sv) != IV_MIN) { | |
2290 | SETi(-SvIVX(sv)); | |
2291 | RETURN; | |
2292 | } | |
28e5dec8 JH |
2293 | #ifdef PERL_PRESERVE_IVUV |
2294 | else { | |
2295 | SETu((UV)IV_MIN); | |
2296 | RETURN; | |
2297 | } | |
2298 | #endif | |
9b0e499b GS |
2299 | } |
2300 | if (SvNIOKp(sv)) | |
a0d0e21e | 2301 | SETn(-SvNV(sv)); |
4633a7c4 | 2302 | else if (SvPOKp(sv)) { |
a0d0e21e LW |
2303 | STRLEN len; |
2304 | char *s = SvPV(sv, len); | |
bbce6d69 | 2305 | if (isIDFIRST(*s)) { |
a0d0e21e LW |
2306 | sv_setpvn(TARG, "-", 1); |
2307 | sv_catsv(TARG, sv); | |
79072805 | 2308 | } |
a0d0e21e LW |
2309 | else if (*s == '+' || *s == '-') { |
2310 | sv_setsv(TARG, sv); | |
2311 | *SvPV_force(TARG, len) = *s == '-' ? '+' : '-'; | |
79072805 | 2312 | } |
8eb28a70 JH |
2313 | else if (DO_UTF8(sv)) { |
2314 | SvIV_please(sv); | |
2315 | if (SvIOK(sv)) | |
2316 | goto oops_its_an_int; | |
2317 | if (SvNOK(sv)) | |
2318 | sv_setnv(TARG, -SvNV(sv)); | |
2319 | else { | |
2320 | sv_setpvn(TARG, "-", 1); | |
2321 | sv_catsv(TARG, sv); | |
2322 | } | |
834a4ddd | 2323 | } |
28e5dec8 | 2324 | else { |
8eb28a70 JH |
2325 | SvIV_please(sv); |
2326 | if (SvIOK(sv)) | |
2327 | goto oops_its_an_int; | |
2328 | sv_setnv(TARG, -SvNV(sv)); | |
28e5dec8 | 2329 | } |
a0d0e21e | 2330 | SETTARG; |
79072805 | 2331 | } |
4633a7c4 LW |
2332 | else |
2333 | SETn(-SvNV(sv)); | |
79072805 | 2334 | } |
a0d0e21e | 2335 | RETURN; |
79072805 LW |
2336 | } |
2337 | ||
a0d0e21e | 2338 | PP(pp_not) |
79072805 | 2339 | { |
39644a26 | 2340 | dSP; tryAMAGICunSET(not); |
3280af22 | 2341 | *PL_stack_sp = boolSV(!SvTRUE(*PL_stack_sp)); |
a0d0e21e | 2342 | return NORMAL; |
79072805 LW |
2343 | } |
2344 | ||
a0d0e21e | 2345 | PP(pp_complement) |
79072805 | 2346 | { |
39644a26 | 2347 | dSP; dTARGET; tryAMAGICun(compl); |
a0d0e21e LW |
2348 | { |
2349 | dTOPss; | |
4633a7c4 | 2350 | if (SvNIOKp(sv)) { |
d0ba1bd2 | 2351 | if (PL_op->op_private & HINT_INTEGER) { |
972b05a9 JH |
2352 | IV i = ~SvIV(sv); |
2353 | SETi(i); | |
d0ba1bd2 JH |
2354 | } |
2355 | else { | |
972b05a9 JH |
2356 | UV u = ~SvUV(sv); |
2357 | SETu(u); | |
d0ba1bd2 | 2358 | } |
a0d0e21e LW |
2359 | } |
2360 | else { | |
51723571 | 2361 | register U8 *tmps; |
55497cff | 2362 | register I32 anum; |
a0d0e21e LW |
2363 | STRLEN len; |
2364 | ||
2365 | SvSetSV(TARG, sv); | |
51723571 | 2366 | tmps = (U8*)SvPV_force(TARG, len); |
a0d0e21e | 2367 | anum = len; |
1d68d6cd | 2368 | if (SvUTF8(TARG)) { |
a1ca4561 | 2369 | /* Calculate exact length, let's not estimate. */ |
1d68d6cd SC |
2370 | STRLEN targlen = 0; |
2371 | U8 *result; | |
51723571 | 2372 | U8 *send; |
ba210ebe | 2373 | STRLEN l; |
a1ca4561 YST |
2374 | UV nchar = 0; |
2375 | UV nwide = 0; | |
1d68d6cd SC |
2376 | |
2377 | send = tmps + len; | |
2378 | while (tmps < send) { | |
9041c2e3 | 2379 | UV c = utf8n_to_uvchr(tmps, send-tmps, &l, UTF8_ALLOW_ANYUV); |
1d68d6cd | 2380 | tmps += UTF8SKIP(tmps); |
5bbb0b5a | 2381 | targlen += UNISKIP(~c); |
a1ca4561 YST |
2382 | nchar++; |
2383 | if (c > 0xff) | |
2384 | nwide++; | |
1d68d6cd SC |
2385 | } |
2386 | ||
2387 | /* Now rewind strings and write them. */ | |
2388 | tmps -= len; | |
a1ca4561 YST |
2389 | |
2390 | if (nwide) { | |
2391 | Newz(0, result, targlen + 1, U8); | |
2392 | while (tmps < send) { | |
9041c2e3 | 2393 | UV c = utf8n_to_uvchr(tmps, send-tmps, &l, UTF8_ALLOW_ANYUV); |
a1ca4561 | 2394 | tmps += UTF8SKIP(tmps); |
b851fbc1 | 2395 | result = uvchr_to_utf8_flags(result, ~c, UNICODE_ALLOW_ANY); |
a1ca4561 YST |
2396 | } |
2397 | *result = '\0'; | |
2398 | result -= targlen; | |
2399 | sv_setpvn(TARG, (char*)result, targlen); | |
2400 | SvUTF8_on(TARG); | |
2401 | } | |
2402 | else { | |
2403 | Newz(0, result, nchar + 1, U8); | |
2404 | while (tmps < send) { | |
9041c2e3 | 2405 | U8 c = (U8)utf8n_to_uvchr(tmps, 0, &l, UTF8_ALLOW_ANY); |
a1ca4561 YST |
2406 | tmps += UTF8SKIP(tmps); |
2407 | *result++ = ~c; | |
2408 | } | |
2409 | *result = '\0'; | |
2410 | result -= nchar; | |
2411 | sv_setpvn(TARG, (char*)result, nchar); | |
1d68d6cd | 2412 | } |
1d68d6cd SC |
2413 | Safefree(result); |
2414 | SETs(TARG); | |
2415 | RETURN; | |
2416 | } | |
a0d0e21e | 2417 | #ifdef LIBERAL |
51723571 JH |
2418 | { |
2419 | register long *tmpl; | |
2420 | for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++) | |
2421 | *tmps = ~*tmps; | |
2422 | tmpl = (long*)tmps; | |
2423 | for ( ; anum >= sizeof(long); anum -= sizeof(long), tmpl++) | |
2424 | *tmpl = ~*tmpl; | |
2425 | tmps = (U8*)tmpl; | |
2426 | } | |
a0d0e21e LW |
2427 | #endif |
2428 | for ( ; anum > 0; anum--, tmps++) | |
2429 | *tmps = ~*tmps; | |
2430 | ||
2431 | SETs(TARG); | |
2432 | } | |
2433 | RETURN; | |
2434 | } | |
79072805 LW |
2435 | } |
2436 | ||
a0d0e21e LW |
2437 | /* integer versions of some of the above */ |
2438 | ||
a0d0e21e | 2439 | PP(pp_i_multiply) |
79072805 | 2440 | { |
39644a26 | 2441 | dSP; dATARGET; tryAMAGICbin(mult,opASSIGN); |
a0d0e21e LW |
2442 | { |
2443 | dPOPTOPiirl; | |
2444 | SETi( left * right ); | |
2445 | RETURN; | |
2446 | } | |
79072805 LW |
2447 | } |
2448 | ||
a0d0e21e | 2449 | PP(pp_i_divide) |
79072805 | 2450 | { |
39644a26 | 2451 | dSP; dATARGET; tryAMAGICbin(div,opASSIGN); |
a0d0e21e LW |
2452 | { |
2453 | dPOPiv; | |
2454 | if (value == 0) | |
cea2e8a9 | 2455 | DIE(aTHX_ "Illegal division by zero"); |
a0d0e21e LW |
2456 | value = POPi / value; |
2457 | PUSHi( value ); | |
2458 | RETURN; | |
2459 | } | |
79072805 LW |
2460 | } |
2461 | ||
224ec323 JH |
2462 | STATIC |
2463 | PP(pp_i_modulo_0) | |
2464 | { | |
2465 | /* This is the vanilla old i_modulo. */ | |
2466 | dSP; dATARGET; tryAMAGICbin(modulo,opASSIGN); | |
2467 | { | |
2468 | dPOPTOPiirl; | |
2469 | if (!right) | |
2470 | DIE(aTHX_ "Illegal modulus zero"); | |
2471 | SETi( left % right ); | |
2472 | RETURN; | |
2473 | } | |
2474 | } | |
2475 | ||
11010fa3 | 2476 | #if defined(__GLIBC__) && IVSIZE == 8 |
224ec323 JH |
2477 | STATIC |
2478 | PP(pp_i_modulo_1) | |
2479 | { | |
224ec323 | 2480 | /* This is the i_modulo with the workaround for the _moddi3 bug |
fce2b89e | 2481 | * in (at least) glibc 2.2.5 (the PERL_ABS() the workaround). |
224ec323 JH |
2482 | * See below for pp_i_modulo. */ |
2483 | dSP; dATARGET; tryAMAGICbin(modulo,opASSIGN); | |
2484 | { | |
2485 | dPOPTOPiirl; | |
2486 | if (!right) | |
2487 | DIE(aTHX_ "Illegal modulus zero"); | |
32fdb065 | 2488 | SETi( left % PERL_ABS(right) ); |
224ec323 JH |
2489 | RETURN; |
2490 | } | |
224ec323 | 2491 | } |
fce2b89e | 2492 | #endif |
224ec323 | 2493 | |
a0d0e21e | 2494 | PP(pp_i_modulo) |
79072805 | 2495 | { |
224ec323 JH |
2496 | dSP; dATARGET; tryAMAGICbin(modulo,opASSIGN); |
2497 | { | |
2498 | dPOPTOPiirl; | |
2499 | if (!right) | |
2500 | DIE(aTHX_ "Illegal modulus zero"); | |
2501 | /* The assumption is to use hereafter the old vanilla version... */ | |
2502 | PL_op->op_ppaddr = | |
2503 | PL_ppaddr[OP_I_MODULO] = | |
2504 | &Perl_pp_i_modulo_0; | |
2505 | /* .. but if we have glibc, we might have a buggy _moddi3 | |
2506 | * (at least glicb 2.2.5 is known to have this bug), in other | |
2507 | * words our integer modulus with negative quad as the second | |
2508 | * argument might be broken. Test for this and re-patch the | |
2509 | * opcode dispatch table if that is the case, remembering to | |
2510 | * also apply the workaround so that this first round works | |
2511 | * right, too. See [perl #9402] for more information. */ | |
2512 | #if defined(__GLIBC__) && IVSIZE == 8 | |
2513 | { | |
2514 | IV l = 3; | |
2515 | IV r = -10; | |
2516 | /* Cannot do this check with inlined IV constants since | |
2517 | * that seems to work correctly even with the buggy glibc. */ | |
2518 | if (l % r == -3) { | |
2519 | /* Yikes, we have the bug. | |
2520 | * Patch in the workaround version. */ | |
2521 | PL_op->op_ppaddr = | |
2522 | PL_ppaddr[OP_I_MODULO] = | |
2523 | &Perl_pp_i_modulo_1; | |
2524 | /* Make certain we work right this time, too. */ | |
32fdb065 | 2525 | right = PERL_ABS(right); |
224ec323 JH |
2526 | } |
2527 | } | |
2528 | #endif | |
2529 | SETi( left % right ); | |
2530 | RETURN; | |
2531 | } | |
79072805 LW |
2532 | } |
2533 | ||
a0d0e21e | 2534 | PP(pp_i_add) |
79072805 | 2535 | { |
39644a26 | 2536 | dSP; dATARGET; tryAMAGICbin(add,opASSIGN); |
a0d0e21e | 2537 | { |
5e66d4f1 | 2538 | dPOPTOPiirl_ul; |
a0d0e21e LW |
2539 | SETi( left + right ); |
2540 | RETURN; | |
79072805 | 2541 | } |
79072805 LW |
2542 | } |
2543 | ||
a0d0e21e | 2544 | PP(pp_i_subtract) |
79072805 | 2545 | { |
39644a26 | 2546 | dSP; dATARGET; tryAMAGICbin(subtr,opASSIGN); |
a0d0e21e | 2547 | { |
5e66d4f1 | 2548 | dPOPTOPiirl_ul; |
a0d0e21e LW |
2549 | SETi( left - right ); |
2550 | RETURN; | |
79072805 | 2551 | } |
79072805 LW |
2552 | } |
2553 | ||
a0d0e21e | 2554 | PP(pp_i_lt) |
79072805 | 2555 | { |
39644a26 | 2556 | dSP; tryAMAGICbinSET(lt,0); |
a0d0e21e LW |
2557 | { |
2558 | dPOPTOPiirl; | |
54310121 | 2559 | SETs(boolSV(left < right)); |
a0d0e21e LW |
2560 | RETURN; |
2561 | } | |
79072805 LW |
2562 | } |
2563 | ||
a0d0e21e | 2564 | PP(pp_i_gt) |
79072805 | 2565 | { |
39644a26 | 2566 | dSP; tryAMAGICbinSET(gt,0); |
a0d0e21e LW |
2567 | { |
2568 | dPOPTOPiirl; | |
54310121 | 2569 | SETs(boolSV(left > right)); |
a0d0e21e LW |
2570 | RETURN; |
2571 | } | |
79072805 LW |
2572 | } |
2573 | ||
a0d0e21e | 2574 | PP(pp_i_le) |
79072805 | 2575 | { |
39644a26 | 2576 | dSP; tryAMAGICbinSET(le,0); |
a0d0e21e LW |
2577 | { |
2578 | dPOPTOPiirl; | |
54310121 | 2579 | SETs(boolSV(left <= right)); |
a0d0e21e | 2580 | RETURN; |
85e6fe83 | 2581 | } |
79072805 LW |
2582 | } |
2583 | ||
a0d0e21e | 2584 | PP(pp_i_ge) |
79072805 | 2585 | { |
39644a26 | 2586 | dSP; tryAMAGICbinSET(ge,0); |
a0d0e21e LW |
2587 | { |
2588 | dPOPTOPiirl; | |
54310121 | 2589 | SETs(boolSV(left >= right)); |
a0d0e21e LW |
2590 | RETURN; |
2591 | } | |
79072805 LW |
2592 | } |
2593 | ||
a0d0e21e | 2594 | PP(pp_i_eq) |
79072805 | 2595 | { |
39644a26 | 2596 | dSP; tryAMAGICbinSET(eq,0); |
a0d0e21e LW |
2597 | { |
2598 | dPOPTOPiirl; | |
54310121 | 2599 | SETs(boolSV(left == right)); |
a0d0e21e LW |
2600 | RETURN; |
2601 | } | |
79072805 LW |
2602 | } |
2603 | ||
a0d0e21e | 2604 | PP(pp_i_ne) |
79072805 | 2605 | { |
39644a26 | 2606 | dSP; tryAMAGICbinSET(ne,0); |
a0d0e21e LW |
2607 | { |
2608 | dPOPTOPiirl; | |
54310121 | 2609 | SETs(boolSV(left != right)); |
a0d0e21e LW |
2610 | RETURN; |
2611 | } | |
79072805 LW |
2612 | } |
2613 | ||
a0d0e21e | 2614 | PP(pp_i_ncmp) |
79072805 | 2615 | { |
39644a26 | 2616 | dSP; dTARGET; tryAMAGICbin(ncmp,0); |
a0d0e21e LW |
2617 | { |
2618 | dPOPTOPiirl; | |
2619 | I32 value; | |
79072805 | 2620 | |
a0d0e21e | 2621 | if (left > right) |
79072805 | 2622 | value = 1; |
a0d0e21e | 2623 | else if (left < right) |
79072805 | 2624 | value = -1; |
a0d0e21e | 2625 | else |
79072805 | 2626 | value = 0; |
a0d0e21e LW |
2627 | SETi(value); |
2628 | RETURN; | |
79072805 | 2629 | } |
85e6fe83 LW |
2630 | } |
2631 | ||
2632 | PP(pp_i_negate) | |
2633 | { | |
39644a26 | 2634 | dSP; dTARGET; tryAMAGICun(neg); |
85e6fe83 LW |
2635 | SETi(-TOPi); |
2636 | RETURN; | |
2637 | } | |
2638 | ||
79072805 LW |
2639 | /* High falutin' math. */ |
2640 | ||
2641 | PP(pp_atan2) | |
2642 | { | |
39644a26 | 2643 | dSP; dTARGET; tryAMAGICbin(atan2,0); |
a0d0e21e LW |
2644 | { |
2645 | dPOPTOPnnrl; | |
65202027 | 2646 | SETn(Perl_atan2(left, right)); |
a0d0e21e LW |
2647 | RETURN; |
2648 | } | |
79072805 LW |
2649 | } |
2650 | ||
2651 | PP(pp_sin) | |
2652 | { | |
39644a26 | 2653 | dSP; dTARGET; tryAMAGICun(sin); |
a0d0e21e | 2654 | { |
65202027 | 2655 | NV value; |
a0d0e21e | 2656 | value = POPn; |
65202027 | 2657 | value = Perl_sin(value); |
a0d0e21e LW |
2658 | XPUSHn(value); |
2659 | RETURN; | |
2660 | } | |
79072805 LW |
2661 | } |
2662 | ||
2663 | PP(pp_cos) | |
2664 | { | |
39644a26 | 2665 | dSP; dTARGET; tryAMAGICun(cos); |
a0d0e21e | 2666 | { |
65202027 | 2667 | NV value; |
a0d0e21e | 2668 | value = POPn; |
65202027 | 2669 | value = Perl_cos(value); |
a0d0e21e LW |
2670 | XPUSHn(value); |
2671 | RETURN; | |
2672 | } | |
79072805 LW |
2673 | } |
2674 | ||
56cb0a1c AD |
2675 | /* Support Configure command-line overrides for rand() functions. |
2676 | After 5.005, perhaps we should replace this by Configure support | |
2677 | for drand48(), random(), or rand(). For 5.005, though, maintain | |
2678 | compatibility by calling rand() but allow the user to override it. | |
2679 | See INSTALL for details. --Andy Dougherty 15 July 1998 | |
2680 | */ | |
85ab1d1d JH |
2681 | /* Now it's after 5.005, and Configure supports drand48() and random(), |
2682 | in addition to rand(). So the overrides should not be needed any more. | |
2683 | --Jarkko Hietaniemi 27 September 1998 | |
2684 | */ | |
2685 | ||
2686 | #ifndef HAS_DRAND48_PROTO | |
20ce7b12 | 2687 | extern double drand48 (void); |
56cb0a1c AD |
2688 | #endif |
2689 | ||
79072805 LW |
2690 | PP(pp_rand) |
2691 | { | |
39644a26 | 2692 | dSP; dTARGET; |
65202027 | 2693 | NV value; |
79072805 LW |
2694 | if (MAXARG < 1) |
2695 | value = 1.0; | |
2696 | else | |
2697 | value = POPn; | |
2698 | if (value == 0.0) | |
2699 | value = 1.0; | |
80252599 | 2700 | if (!PL_srand_called) { |
85ab1d1d | 2701 | (void)seedDrand01((Rand_seed_t)seed()); |
80252599 | 2702 | PL_srand_called = TRUE; |
93dc8474 | 2703 | } |
85ab1d1d | 2704 | value *= Drand01(); |
79072805 LW |
2705 | XPUSHn(value); |
2706 | RETURN; | |
2707 | } | |
2708 | ||
2709 | PP(pp_srand) | |
2710 | { | |
39644a26 | 2711 | dSP; |
93dc8474 CS |
2712 | UV anum; |
2713 | if (MAXARG < 1) | |
2714 | anum = seed(); | |
79072805 | 2715 | else |
93dc8474 | 2716 | anum = POPu; |
85ab1d1d | 2717 | (void)seedDrand01((Rand_seed_t)anum); |
80252599 | 2718 | PL_srand_called = TRUE; |
79072805 LW |
2719 | EXTEND(SP, 1); |
2720 | RETPUSHYES; | |
2721 | } | |
2722 | ||
76e3520e | 2723 | STATIC U32 |
cea2e8a9 | 2724 | S_seed(pTHX) |
93dc8474 | 2725 | { |
54310121 | 2726 | /* |
2727 | * This is really just a quick hack which grabs various garbage | |
2728 | * values. It really should be a real hash algorithm which | |
2729 | * spreads the effect of every input bit onto every output bit, | |
85ab1d1d | 2730 | * if someone who knows about such things would bother to write it. |
54310121 | 2731 | * Might be a good idea to add that function to CORE as well. |
85ab1d1d | 2732 | * No numbers below come from careful analysis or anything here, |
54310121 | 2733 | * except they are primes and SEED_C1 > 1E6 to get a full-width |
2734 | * value from (tv_sec * SEED_C1 + tv_usec). The multipliers should | |
2735 | * probably be bigger too. | |
2736 | */ | |
2737 | #if RANDBITS > 16 | |
2738 | # define SEED_C1 1000003 | |
2739 | #define SEED_C4 73819 | |
2740 | #else | |
2741 | # define SEED_C1 25747 | |
2742 | #define SEED_C4 20639 | |
2743 | #endif | |
2744 | #define SEED_C2 3 | |
2745 | #define SEED_C3 269 | |
2746 | #define SEED_C5 26107 | |
2747 | ||
73c60299 RS |
2748 | #ifndef PERL_NO_DEV_RANDOM |
2749 | int fd; | |
2750 | #endif | |
93dc8474 | 2751 | U32 u; |
f12c7020 | 2752 | #ifdef VMS |
2753 | # include <starlet.h> | |
43c92808 HF |
2754 | /* when[] = (low 32 bits, high 32 bits) of time since epoch |
2755 | * in 100-ns units, typically incremented ever 10 ms. */ | |
93dc8474 | 2756 | unsigned int when[2]; |
73c60299 RS |
2757 | #else |
2758 | # ifdef HAS_GETTIMEOFDAY | |
2759 | struct timeval when; | |
2760 | # else | |
2761 | Time_t when; | |
2762 | # endif | |
2763 | #endif | |
2764 | ||
2765 | /* This test is an escape hatch, this symbol isn't set by Configure. */ | |
2766 | #ifndef PERL_NO_DEV_RANDOM | |
2767 | #ifndef PERL_RANDOM_DEVICE | |
2768 | /* /dev/random isn't used by default because reads from it will block | |
2769 | * if there isn't enough entropy available. You can compile with | |
2770 | * PERL_RANDOM_DEVICE to it if you'd prefer Perl to block until there | |
2771 | * is enough real entropy to fill the seed. */ | |
2772 | # define PERL_RANDOM_DEVICE "/dev/urandom" | |
2773 | #endif | |
2774 | fd = PerlLIO_open(PERL_RANDOM_DEVICE, 0); | |
2775 | if (fd != -1) { | |
2776 | if (PerlLIO_read(fd, &u, sizeof u) != sizeof u) | |
2777 | u = 0; | |
2778 | PerlLIO_close(fd); | |
2779 | if (u) | |
2780 | return u; | |
2781 | } | |
2782 | #endif | |
2783 | ||
2784 | #ifdef VMS | |
93dc8474 | 2785 | _ckvmssts(sys$gettim(when)); |
54310121 | 2786 | u = (U32)SEED_C1 * when[0] + (U32)SEED_C2 * when[1]; |
f12c7020 | 2787 | #else |
5f05dabc | 2788 | # ifdef HAS_GETTIMEOFDAY |
57ab3dfe | 2789 | PerlProc_gettimeofday(&when,NULL); |
54310121 | 2790 | u = (U32)SEED_C1 * when.tv_sec + (U32)SEED_C2 * when.tv_usec; |
f12c7020 | 2791 | # else |
93dc8474 | 2792 | (void)time(&when); |
54310121 | 2793 | u = (U32)SEED_C1 * when; |
f12c7020 | 2794 | # endif |
2795 | #endif | |
7766f137 | 2796 | u += SEED_C3 * (U32)PerlProc_getpid(); |
56431972 | 2797 | u += SEED_C4 * (U32)PTR2UV(PL_stack_sp); |
54310121 | 2798 | #ifndef PLAN9 /* XXX Plan9 assembler chokes on this; fix needed */ |
56431972 | 2799 | u += SEED_C5 * (U32)PTR2UV(&when); |
f12c7020 | 2800 | #endif |
93dc8474 | 2801 | return u; |
79072805 LW |
2802 | } |
2803 | ||
2804 | PP(pp_exp) | |
2805 | { | |
39644a26 | 2806 | dSP; dTARGET; tryAMAGICun(exp); |
a0d0e21e | 2807 | { |
65202027 | 2808 | NV value; |
a0d0e21e | 2809 | value = POPn; |
65202027 | 2810 | value = Perl_exp(value); |
a0d0e21e LW |
2811 | XPUSHn(value); |
2812 | RETURN; | |
2813 | } | |
79072805 LW |
2814 | } |
2815 | ||
2816 | PP(pp_log) | |
2817 | { | |
39644a26 | 2818 | dSP; dTARGET; tryAMAGICun(log); |
a0d0e21e | 2819 | { |
65202027 | 2820 | NV value; |
a0d0e21e | 2821 | value = POPn; |
bbce6d69 | 2822 | if (value <= 0.0) { |
f93f4e46 | 2823 | SET_NUMERIC_STANDARD(); |
1779d84d | 2824 | DIE(aTHX_ "Can't take log of %"NVgf, value); |
bbce6d69 | 2825 | } |
65202027 | 2826 | value = Perl_log(value); |
a0d0e21e LW |
2827 | XPUSHn(value); |
2828 | RETURN; | |
2829 | } | |
79072805 LW |
2830 | } |
2831 | ||
2832 | PP(pp_sqrt) | |
2833 | { | |
39644a26 | 2834 | dSP; dTARGET; tryAMAGICun(sqrt); |
a0d0e21e | 2835 | { |
65202027 | 2836 | NV value; |
a0d0e21e | 2837 | value = POPn; |
bbce6d69 | 2838 | if (value < 0.0) { |
f93f4e46 | 2839 | SET_NUMERIC_STANDARD(); |
1779d84d | 2840 | DIE(aTHX_ "Can't take sqrt of %"NVgf, value); |
bbce6d69 | 2841 | } |
65202027 | 2842 | value = Perl_sqrt(value); |
a0d0e21e LW |
2843 | XPUSHn(value); |
2844 | RETURN; | |
2845 | } | |
79072805 LW |
2846 | } |
2847 | ||
24da999b JH |
2848 | /* |
2849 | * There are strange code-generation bugs caused on sparc64 by gcc-2.95.2. | |
2850 | * These need to be revisited when a newer toolchain becomes available. | |
2851 | */ | |
2852 | #if defined(__sparc64__) && defined(__GNUC__) | |
2853 | # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 96) | |
2854 | # undef SPARC64_MODF_WORKAROUND | |
2855 | # define SPARC64_MODF_WORKAROUND 1 | |
2856 | # endif | |
2857 | #endif | |
2858 | ||
2859 | #if defined(SPARC64_MODF_WORKAROUND) | |
2860 | static NV | |
2861 | sparc64_workaround_modf(NV theVal, NV *theIntRes) | |
2862 | { | |
2863 | NV res, ret; | |
2864 | ret = Perl_modf(theVal, &res); | |
2865 | *theIntRes = res; | |
2866 | return ret; | |
2867 | } | |
2868 | #endif | |
2869 | ||
79072805 LW |
2870 | PP(pp_int) |
2871 | { | |
39644a26 | 2872 | dSP; dTARGET; tryAMAGICun(int); |
774d564b | 2873 | { |
28e5dec8 JH |
2874 | NV value; |
2875 | IV iv = TOPi; /* attempt to convert to IV if possible. */ | |
2876 | /* XXX it's arguable that compiler casting to IV might be subtly | |
2877 | different from modf (for numbers inside (IV_MIN,UV_MAX)) in which | |
2878 | else preferring IV has introduced a subtle behaviour change bug. OTOH | |
2879 | relying on floating point to be accurate is a bug. */ | |
2880 | ||
2881 | if (SvIOK(TOPs)) { | |
2882 | if (SvIsUV(TOPs)) { | |
2883 | UV uv = TOPu; | |
2884 | SETu(uv); | |
2885 | } else | |
2886 | SETi(iv); | |
2887 | } else { | |
2888 | value = TOPn; | |
1048ea30 | 2889 | if (value >= 0.0) { |
28e5dec8 JH |
2890 | if (value < (NV)UV_MAX + 0.5) { |
2891 | SETu(U_V(value)); | |
2892 | } else { | |
24da999b | 2893 | #if defined(SPARC64_MODF_WORKAROUND) |
55954f19 JH |
2894 | (void)sparc64_workaround_modf(value, &value); |
2895 | #elif defined(HAS_MODFL_POW32_BUG) | |
e67aeab1 | 2896 | /* some versions of glibc split (i + d) into (i-1, d+1) for 2^32 <= i < 2^64 */ |
55954f19 JH |
2897 | NV offset = Perl_modf(value, &value); |
2898 | (void)Perl_modf(offset, &offset); | |
2899 | value += offset; | |
2900 | #else | |
2901 | (void)Perl_modf(value, &value); | |
1048ea30 | 2902 | #endif |
2d9af89d | 2903 | SETn(value); |
28e5dec8 | 2904 | } |
1048ea30 | 2905 | } |
28e5dec8 JH |
2906 | else { |
2907 | if (value > (NV)IV_MIN - 0.5) { | |
2908 | SETi(I_V(value)); | |
2909 | } else { | |
55954f19 JH |
2910 | #if defined(SPARC64_MODF_WORKAROUND) |
2911 | (void)sparc64_workaround_modf(-value, &value); | |
2912 | #elif defined(HAS_MODFL_POW32_BUG) | |
e67aeab1 | 2913 | /* some versions of glibc split (i + d) into (i-1, d+1) for 2^32 <= i < 2^64 */ |
55954f19 JH |
2914 | NV offset = Perl_modf(-value, &value); |
2915 | (void)Perl_modf(offset, &offset); | |
2916 | value += offset; | |
1048ea30 | 2917 | #else |
55954f19 | 2918 | (void)Perl_modf(-value, &value); |
1048ea30 | 2919 | #endif |
55954f19 | 2920 | SETn(-value); |
28e5dec8 JH |
2921 | } |
2922 | } | |
774d564b | 2923 | } |
79072805 | 2924 | } |
79072805 LW |
2925 | RETURN; |
2926 | } | |
2927 | ||
463ee0b2 LW |
2928 | PP(pp_abs) |
2929 | { | |
39644a26 | 2930 | dSP; dTARGET; tryAMAGICun(abs); |
a0d0e21e | 2931 | { |
28e5dec8 JH |
2932 | /* This will cache the NV value if string isn't actually integer */ |
2933 | IV iv = TOPi; | |
a227d84d | 2934 | |
28e5dec8 JH |
2935 | if (SvIOK(TOPs)) { |
2936 | /* IVX is precise */ | |
2937 | if (SvIsUV(TOPs)) { | |
2938 | SETu(TOPu); /* force it to be numeric only */ | |
2939 | } else { | |
2940 | if (iv >= 0) { | |
2941 | SETi(iv); | |
2942 | } else { | |
2943 | if (iv != IV_MIN) { | |
2944 | SETi(-iv); | |
2945 | } else { | |
2946 | /* 2s complement assumption. Also, not really needed as | |
2947 | IV_MIN and -IV_MIN should both be %100...00 and NV-able */ | |
2948 | SETu(IV_MIN); | |
2949 | } | |
a227d84d | 2950 | } |
28e5dec8 JH |
2951 | } |
2952 | } else{ | |
2953 | NV value = TOPn; | |
774d564b | 2954 | if (value < 0.0) |
28e5dec8 | 2955 | value = -value; |
774d564b | 2956 | SETn(value); |
2957 | } | |
a0d0e21e | 2958 | } |
774d564b | 2959 | RETURN; |
463ee0b2 LW |
2960 | } |
2961 | ||
53305cf1 | 2962 | |
79072805 LW |
2963 | PP(pp_hex) |
2964 | { | |
39644a26 | 2965 | dSP; dTARGET; |
79072805 | 2966 | char *tmps; |
53305cf1 | 2967 | I32 flags = PERL_SCAN_ALLOW_UNDERSCORES; |
6f894ead | 2968 | STRLEN len; |
53305cf1 NC |
2969 | NV result_nv; |
2970 | UV result_uv; | |
2bc69dc4 | 2971 | SV* sv = POPs; |
79072805 | 2972 | |
2bc69dc4 NIS |
2973 | tmps = (SvPVx(sv, len)); |
2974 | if (DO_UTF8(sv)) { | |
2975 | /* If Unicode, try to downgrade | |
2976 | * If not possible, croak. */ | |
2977 | SV* tsv = sv_2mortal(newSVsv(sv)); | |
2978 | ||
2979 | SvUTF8_on(tsv); | |
2980 | sv_utf8_downgrade(tsv, FALSE); | |
2981 | tmps = SvPVX(tsv); | |
2982 | } | |
53305cf1 NC |
2983 | result_uv = grok_hex (tmps, &len, &flags, &result_nv); |
2984 | if (flags & PERL_SCAN_GREATER_THAN_UV_MAX) { | |
2985 | XPUSHn(result_nv); | |
2986 | } | |
2987 | else { | |
2988 | XPUSHu(result_uv); | |
2989 | } | |
79072805 LW |
2990 | RETURN; |
2991 | } | |
2992 | ||
2993 | PP(pp_oct) | |
2994 | { | |
39644a26 | 2995 | dSP; dTARGET; |
79072805 | 2996 | char *tmps; |
53305cf1 | 2997 | I32 flags = PERL_SCAN_ALLOW_UNDERSCORES; |
6f894ead | 2998 | STRLEN len; |
53305cf1 NC |
2999 | NV result_nv; |
3000 | UV result_uv; | |
2bc69dc4 | 3001 | SV* sv = POPs; |
79072805 | 3002 | |
2bc69dc4 NIS |
3003 | tmps = (SvPVx(sv, len)); |
3004 | if (DO_UTF8(sv)) { | |
3005 | /* If Unicode, try to downgrade | |
3006 | * If not possible, croak. */ | |
3007 | SV* tsv = sv_2mortal(newSVsv(sv)); | |
3008 | ||
3009 | SvUTF8_on(tsv); | |
3010 | sv_utf8_downgrade(tsv, FALSE); | |
3011 | tmps = SvPVX(tsv); | |
3012 | } | |
6f894ead | 3013 | while (*tmps && len && isSPACE(*tmps)) |
53305cf1 | 3014 | tmps++, len--; |
9e24b6e2 | 3015 | if (*tmps == '0') |
53305cf1 | 3016 | tmps++, len--; |
9e24b6e2 | 3017 | if (*tmps == 'x') |
53305cf1 | 3018 | result_uv = grok_hex (tmps, &len, &flags, &result_nv); |
9e24b6e2 | 3019 | else if (*tmps == 'b') |
53305cf1 | 3020 | result_uv = grok_bin (tmps, &len, &flags, &result_nv); |
464e2e8a | 3021 | else |
53305cf1 NC |
3022 | result_uv = grok_oct (tmps, &len, &flags, &result_nv); |
3023 | ||
3024 | if (flags & PERL_SCAN_GREATER_THAN_UV_MAX) { | |
3025 | XPUSHn(result_nv); | |
3026 | } | |
3027 | else { | |
3028 | XPUSHu(result_uv); | |
3029 | } | |
79072805 LW |
3030 | RETURN; |
3031 | } | |
3032 | ||
3033 | /* String stuff. */ | |
3034 | ||
3035 | PP(pp_length) | |
3036 | { | |
39644a26 | 3037 | dSP; dTARGET; |
7e2040f0 | 3038 | SV *sv = TOPs; |
a0ed51b3 | 3039 | |
7e2040f0 GS |
3040 | if (DO_UTF8(sv)) |
3041 | SETi(sv_len_utf8(sv)); | |
3042 | else | |
3043 | SETi(sv_len(sv)); | |
79072805 LW |
3044 | RETURN; |
3045 | } | |
3046 | ||
3047 | PP(pp_substr) | |
3048 | { | |
39644a26 | 3049 | dSP; dTARGET; |
79072805 | 3050 | SV *sv; |
9c5ffd7c | 3051 | I32 len = 0; |
463ee0b2 | 3052 | STRLEN curlen; |
9402d6ed | 3053 | STRLEN utf8_curlen; |
79072805 LW |
3054 | I32 pos; |
3055 | I32 rem; | |
84902520 | 3056 | I32 fail; |
78f9721b | 3057 | I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET; |
79072805 | 3058 | char *tmps; |
3280af22 | 3059 | I32 arybase = PL_curcop->cop_arybase; |
9402d6ed | 3060 | SV *repl_sv = NULL; |
7b8d334a GS |
3061 | char *repl = 0; |
3062 | STRLEN repl_len; | |
78f9721b | 3063 | int num_args = PL_op->op_private & 7; |
13e30c65 | 3064 | bool repl_need_utf8_upgrade = FALSE; |
9402d6ed | 3065 | bool repl_is_utf8 = FALSE; |
79072805 | 3066 | |
20408e3c | 3067 | SvTAINTED_off(TARG); /* decontaminate */ |
7e2040f0 | 3068 | SvUTF8_off(TARG); /* decontaminate */ |
78f9721b SM |
3069 | if (num_args > 2) { |
3070 | if (num_args > 3) { | |
9402d6ed JH |
3071 | repl_sv = POPs; |
3072 | repl = SvPV(repl_sv, repl_len); | |
3073 | repl_is_utf8 = DO_UTF8(repl_sv) && SvCUR(repl_sv); | |
7b8d334a | 3074 | } |
79072805 | 3075 | len = POPi; |
5d82c453 | 3076 | } |
84902520 | 3077 | pos = POPi; |
79072805 | 3078 | sv = POPs; |
849ca7ee | 3079 | PUTBACK; |
9402d6ed JH |
3080 | if (repl_sv) { |
3081 | if (repl_is_utf8) { | |
3082 | if (!DO_UTF8(sv)) | |
3083 | sv_utf8_upgrade(sv); | |
3084 | } | |
13e30c65 JH |
3085 | else if (DO_UTF8(sv)) |
3086 | repl_need_utf8_upgrade = TRUE; | |
9402d6ed | 3087 | } |
a0d0e21e | 3088 | tmps = SvPV(sv, curlen); |
7e2040f0 | 3089 | if (DO_UTF8(sv)) { |
9402d6ed JH |
3090 | utf8_curlen = sv_len_utf8(sv); |
3091 | if (utf8_curlen == curlen) | |
3092 | utf8_curlen = 0; | |
a0ed51b3 | 3093 | else |
9402d6ed | 3094 | curlen = utf8_curlen; |
a0ed51b3 | 3095 | } |
d1c2b58a | 3096 | else |
9402d6ed | 3097 | utf8_curlen = 0; |
a0ed51b3 | 3098 | |
84902520 TB |
3099 | if (pos >= arybase) { |
3100 | pos -= arybase; | |
3101 | rem = curlen-pos; | |
3102 | fail = rem; | |
78f9721b | 3103 | if (num_args > 2) { |
5d82c453 GA |
3104 | if (len < 0) { |
3105 | rem += len; | |
3106 | if (rem < 0) | |
3107 | rem = 0; | |
3108 | } | |
3109 | else if (rem > len) | |
3110 | rem = len; | |
3111 | } | |
68dc0745 | 3112 | } |
84902520 | 3113 | else { |
5d82c453 | 3114 | pos += curlen; |
78f9721b | 3115 | if (num_args < 3) |
5d82c453 GA |
3116 | rem = curlen; |
3117 | else if (len >= 0) { | |
3118 | rem = pos+len; | |
3119 | if (rem > (I32)curlen) | |
3120 | rem = curlen; | |
3121 | } | |
3122 | else { | |
3123 | rem = curlen+len; | |
3124 | if (rem < pos) | |
3125 | rem = pos; | |
3126 | } | |
3127 | if (pos < 0) | |
3128 | pos = 0; | |
3129 | fail = rem; | |
3130 | rem -= pos; | |
84902520 TB |
3131 | } |
3132 | if (fail < 0) { | |
e476b1b5 GS |
3133 | if (lvalue || repl) |
3134 | Perl_croak(aTHX_ "substr outside of string"); | |
3135 | if (ckWARN(WARN_SUBSTR)) | |
9014280d | 3136 | Perl_warner(aTHX_ packWARN(WARN_SUBSTR), "substr outside of string"); |
2304df62 AD |
3137 | RETPUSHUNDEF; |
3138 | } | |
79072805 | 3139 | else { |
9aa983d2 JH |
3140 | I32 upos = pos; |
3141 | I32 urem = rem; | |
9402d6ed | 3142 | if (utf8_curlen) |
a0ed51b3 | 3143 | sv_pos_u2b(sv, &pos, &rem); |
79072805 | 3144 | tmps += pos; |
79072805 | 3145 | sv_setpvn(TARG, tmps, rem); |
12aa1545 | 3146 | #ifdef USE_LOCALE_COLLATE |
14befaf4 | 3147 | sv_unmagic(TARG, PERL_MAGIC_collxfrm); |
12aa1545 | 3148 | #endif |
9402d6ed | 3149 | if (utf8_curlen) |
7f66633b | 3150 | SvUTF8_on(TARG); |
f7928d6c | 3151 | if (repl) { |
13e30c65 JH |
3152 | SV* repl_sv_copy = NULL; |
3153 | ||
3154 | if (repl_need_utf8_upgrade) { | |
3155 | repl_sv_copy = newSVsv(repl_sv); | |
3156 | sv_utf8_upgrade(repl_sv_copy); | |
3157 | repl = SvPV(repl_sv_copy, repl_len); | |
3158 | repl_is_utf8 = DO_UTF8(repl_sv_copy) && SvCUR(sv); | |
3159 | } | |
c8faf1c5 | 3160 | sv_insert(sv, pos, rem, repl, repl_len); |
9402d6ed | 3161 | if (repl_is_utf8) |
f7928d6c | 3162 | SvUTF8_on(sv); |
9402d6ed JH |
3163 | if (repl_sv_copy) |
3164 | SvREFCNT_dec(repl_sv_copy); | |
f7928d6c | 3165 | } |
c8faf1c5 | 3166 | else if (lvalue) { /* it's an lvalue! */ |
dedeecda | 3167 | if (!SvGMAGICAL(sv)) { |
3168 | if (SvROK(sv)) { | |
2d8e6c8d GS |
3169 | STRLEN n_a; |
3170 | SvPV_force(sv,n_a); | |
599cee73 | 3171 | if (ckWARN(WARN_SUBSTR)) |
9014280d | 3172 | Perl_warner(aTHX_ packWARN(WARN_SUBSTR), |
599cee73 | 3173 | "Attempt to use reference as lvalue in substr"); |
dedeecda | 3174 | } |
3175 | if (SvOK(sv)) /* is it defined ? */ | |
7f66633b | 3176 | (void)SvPOK_only_UTF8(sv); |
dedeecda | 3177 | else |
3178 | sv_setpvn(sv,"",0); /* avoid lexical reincarnation */ | |
3179 | } | |
5f05dabc | 3180 | |
24aef97f HS |
3181 | if (SvREFCNT(TARG) > 1) /* don't share the TARG (#20933) */ |
3182 | TARG = sv_newmortal(); | |
a0d0e21e LW |
3183 | if (SvTYPE(TARG) < SVt_PVLV) { |
3184 | sv_upgrade(TARG, SVt_PVLV); | |
14befaf4 | 3185 | sv_magic(TARG, Nullsv, PERL_MAGIC_substr, Nullch, 0); |
ed6116ce | 3186 | } |
a0d0e21e | 3187 | |
5f05dabc | 3188 | LvTYPE(TARG) = 'x'; |
6ff81951 GS |
3189 | if (LvTARG(TARG) != sv) { |
3190 | if (LvTARG(TARG)) | |
3191 | SvREFCNT_dec(LvTARG(TARG)); | |
3192 | LvTARG(TARG) = SvREFCNT_inc(sv); | |
3193 | } | |
9aa983d2 JH |
3194 | LvTARGOFF(TARG) = upos; |
3195 | LvTARGLEN(TARG) = urem; | |
79072805 LW |
3196 | } |
3197 | } | |
849ca7ee | 3198 | SPAGAIN; |
79072805 LW |
3199 | PUSHs(TARG); /* avoid SvSETMAGIC here */ |
3200 | RETURN; | |
3201 | } | |
3202 | ||
3203 | PP(pp_vec) | |
3204 | { | |
39644a26 | 3205 | dSP; dTARGET; |
467f0320 JH |
3206 | register IV size = POPi; |
3207 | register IV offset = POPi; | |
79072805 | 3208 | register SV *src = POPs; |
78f9721b | 3209 | I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET; |
a0d0e21e | 3210 | |
81e118e0 JH |
3211 | SvTAINTED_off(TARG); /* decontaminate */ |
3212 | if (lvalue) { /* it's an lvalue! */ | |
24aef97f HS |
3213 | if (SvREFCNT(TARG) > 1) /* don't share the TARG (#20933) */ |
3214 | TARG = sv_newmortal(); | |
81e118e0 JH |
3215 | if (SvTYPE(TARG) < SVt_PVLV) { |
3216 | sv_upgrade(TARG, SVt_PVLV); | |
14befaf4 | 3217 | sv_magic(TARG, Nullsv, PERL_MAGIC_vec, Nullch, 0); |
79072805 | 3218 | } |
81e118e0 JH |
3219 | LvTYPE(TARG) = 'v'; |
3220 | if (LvTARG(TARG) != src) { | |
3221 | if (LvTARG(TARG)) | |
3222 | SvREFCNT_dec(LvTARG(TARG)); | |
3223 | LvTARG(TARG) = SvREFCNT_inc(src); | |
79072805 | 3224 | } |
81e118e0 JH |
3225 | LvTARGOFF(TARG) = offset; |
3226 | LvTARGLEN(TARG) = size; | |
79072805 LW |
3227 | } |
3228 | ||
81e118e0 | 3229 | sv_setuv(TARG, do_vecget(src, offset, size)); |
79072805 LW |
3230 | PUSHs(TARG); |
3231 | RETURN; | |
3232 | } | |
3233 | ||
3234 | PP(pp_index) | |
3235 | { | |
39644a26 | 3236 | dSP; dTARGET; |
79072805 LW |
3237 | SV *big; |
3238 | SV *little; | |
3239 | I32 offset; | |
3240 | I32 retval; | |
3241 | char *tmps; | |
3242 | char *tmps2; | |
463ee0b2 | 3243 | STRLEN biglen; |
3280af22 | 3244 | I32 arybase = PL_curcop->cop_arybase; |
79072805 LW |
3245 | |
3246 | if (MAXARG < 3) | |
3247 | offset = 0; | |
3248 | else | |
3249 | offset = POPi - arybase; | |
3250 | little = POPs; | |
3251 | big = POPs; | |
463ee0b2 | 3252 | tmps = SvPV(big, biglen); |
7e2040f0 | 3253 | if (offset > 0 && DO_UTF8(big)) |
a0ed51b3 | 3254 | sv_pos_u2b(big, &offset, 0); |
79072805 LW |
3255 | if (offset < 0) |
3256 | offset = 0; | |
eb160463 | 3257 | else if (offset > (I32)biglen) |
93a17b20 | 3258 | offset = biglen; |
79072805 | 3259 | if (!(tmps2 = fbm_instr((unsigned char*)tmps + offset, |
411d5715 | 3260 | (unsigned char*)tmps + biglen, little, 0))) |
a0ed51b3 | 3261 | retval = -1; |
79072805 | 3262 | else |
a0ed51b3 | 3263 | retval = tmps2 - tmps; |
7e2040f0 | 3264 | if (retval > 0 && DO_UTF8(big)) |
a0ed51b3 LW |
3265 | sv_pos_b2u(big, &retval); |
3266 | PUSHi(retval + arybase); | |
79072805 LW |
3267 | RETURN; |
3268 | } | |
3269 | ||
3270 | PP(pp_rindex) | |
3271 | { | |
39644a26 | 3272 | dSP; dTARGET; |
79072805 LW |
3273 | SV *big; |
3274 | SV *little; | |
463ee0b2 LW |
3275 | STRLEN blen; |
3276 | STRLEN llen; | |
79072805 LW |
3277 | I32 offset; |
3278 | I32 retval; | |
3279 | char *tmps; | |
3280 | char *tmps2; | |
3280af22 | 3281 | I32 arybase = PL_curcop->cop_arybase; |
79072805 | 3282 | |
a0d0e21e | 3283 | if (MAXARG >= 3) |
a0ed51b3 | 3284 | offset = POPi; |
79072805 LW |
3285 | little = POPs; |
3286 | big = POPs; | |
463ee0b2 LW |
3287 | tmps2 = SvPV(little, llen); |
3288 | tmps = SvPV(big, blen); | |
79072805 | 3289 | if (MAXARG < 3) |
463ee0b2 | 3290 | offset = blen; |
a0ed51b3 | 3291 | else { |
7e2040f0 | 3292 | if (offset > 0 && DO_UTF8(big)) |
a0ed51b3 LW |
3293 | sv_pos_u2b(big, &offset, 0); |
3294 | offset = offset - arybase + llen; | |
3295 | } | |
79072805 LW |
3296 | if (offset < 0) |
3297 | offset = 0; | |
eb160463 | 3298 | else if (offset > (I32)blen) |
463ee0b2 | 3299 | offset = blen; |
79072805 | 3300 | if (!(tmps2 = rninstr(tmps, tmps + offset, |
463ee0b2 | 3301 | tmps2, tmps2 + llen))) |
a0ed51b3 | 3302 | retval = -1; |
79072805 | 3303 | else |
a0ed51b3 | 3304 | retval = tmps2 - tmps; |
7e2040f0 | 3305 | if (retval > 0 && DO_UTF8(big)) |
a0ed51b3 LW |
3306 | sv_pos_b2u(big, &retval); |
3307 | PUSHi(retval + arybase); | |
79072805 LW |
3308 | RETURN; |
3309 | } | |
3310 | ||
3311 | PP(pp_sprintf) | |
3312 | { | |
39644a26 | 3313 | dSP; dMARK; dORIGMARK; dTARGET; |
79072805 | 3314 | do_sprintf(TARG, SP-MARK, MARK+1); |
bbce6d69 | 3315 | TAINT_IF(SvTAINTED(TARG)); |
6ee35fb7 JH |
3316 | if (DO_UTF8(*(MARK+1))) |
3317 | SvUTF8_on(TARG); | |
79072805 LW |
3318 | SP = ORIGMARK; |
3319 | PUSHTARG; | |
3320 | RETURN; | |
3321 | } | |
3322 | ||
79072805 LW |
3323 | PP(pp_ord) |
3324 | { | |
39644a26 | 3325 | dSP; dTARGET; |
7df053ec | 3326 | SV *argsv = POPs; |
ba210ebe | 3327 | STRLEN len; |
7df053ec | 3328 | U8 *s = (U8*)SvPVx(argsv, len); |
121910a4 JH |
3329 | SV *tmpsv; |
3330 | ||
799ef3cb | 3331 | if (PL_encoding && SvPOK(argsv) && !DO_UTF8(argsv)) { |
121910a4 | 3332 | tmpsv = sv_2mortal(newSVsv(argsv)); |
799ef3cb | 3333 | s = (U8*)sv_recode_to_utf8(tmpsv, PL_encoding); |
121910a4 JH |
3334 | argsv = tmpsv; |
3335 | } | |
79072805 | 3336 | |
872c91ae JH |
3337 | XPUSHu(DO_UTF8(argsv) ? |
3338 | utf8n_to_uvchr(s, UTF8_MAXLEN, 0, UTF8_ALLOW_ANYUV) : | |
3339 | (*s & 0xff)); | |
68795e93 | 3340 | |
79072805 LW |
3341 | RETURN; |
3342 | } | |
3343 | ||
463ee0b2 LW |
3344 | PP(pp_chr) |
3345 | { | |
39644a26 | 3346 | dSP; dTARGET; |
463ee0b2 | 3347 | char *tmps; |
467f0320 | 3348 | UV value = POPu; |
463ee0b2 | 3349 | |
748a9306 | 3350 | (void)SvUPGRADE(TARG,SVt_PV); |
a0ed51b3 | 3351 | |
0064a8a9 | 3352 | if (value > 255 && !IN_BYTES) { |
eb160463 | 3353 | SvGROW(TARG, (STRLEN)UNISKIP(value)+1); |
62961d2e | 3354 | tmps = (char*)uvchr_to_utf8_flags((U8*)SvPVX(TARG), value, 0); |
a0ed51b3 LW |
3355 | SvCUR_set(TARG, tmps - SvPVX(TARG)); |
3356 | *tmps = '\0'; | |
3357 | (void)SvPOK_only(TARG); | |
aa6ffa16 | 3358 | SvUTF8_on(TARG); |
a0ed51b3 LW |
3359 | XPUSHs(TARG); |
3360 | RETURN; | |
3361 | } | |
3362 | ||
748a9306 | 3363 | SvGROW(TARG,2); |
463ee0b2 LW |
3364 | SvCUR_set(TARG, 1); |
3365 | tmps = SvPVX(TARG); | |
eb160463 | 3366 | *tmps++ = (char)value; |
748a9306 | 3367 | *tmps = '\0'; |
a0d0e21e | 3368 | (void)SvPOK_only(TARG); |
88632417 | 3369 | if (PL_encoding && !IN_BYTES) { |
799ef3cb | 3370 | sv_recode_to_utf8(TARG, PL_encoding); |
88632417 JH |
3371 | tmps = SvPVX(TARG); |
3372 | if (SvCUR(TARG) == 0 || !is_utf8_string((U8*)tmps, SvCUR(TARG)) || | |
3373 | memEQ(tmps, "\xef\xbf\xbd\0", 4)) { | |
d5a15ac2 JH |
3374 | SvGROW(TARG, 3); |
3375 | tmps = SvPVX(TARG); | |
88632417 JH |
3376 | SvCUR_set(TARG, 2); |
3377 | *tmps++ = (U8)UTF8_EIGHT_BIT_HI(value); | |
3378 | *tmps++ = (U8)UTF8_EIGHT_BIT_LO(value); | |
3379 | *tmps = '\0'; | |
3380 | SvUTF8_on(TARG); | |
3381 | } | |
3382 | } | |
463ee0b2 LW |
3383 | XPUSHs(TARG); |
3384 | RETURN; | |
3385 | } | |
3386 | ||
79072805 LW |
3387 | PP(pp_crypt) |
3388 | { | |
5f74f29c | 3389 | dSP; dTARGET; |
79072805 | 3390 | #ifdef HAS_CRYPT |
5f74f29c JH |
3391 | dPOPTOPssrl; |
3392 | STRLEN n_a; | |
85c16d83 JH |
3393 | STRLEN len; |
3394 | char *tmps = SvPV(left, len); | |
2bc69dc4 | 3395 | |
85c16d83 | 3396 | if (DO_UTF8(left)) { |
2bc69dc4 | 3397 | /* If Unicode, try to downgrade. |
f2791508 JH |
3398 | * If not possible, croak. |
3399 | * Yes, we made this up. */ | |
3400 | SV* tsv = sv_2mortal(newSVsv(left)); | |
2bc69dc4 | 3401 | |
f2791508 | 3402 | SvUTF8_on(tsv); |
2bc69dc4 | 3403 | sv_utf8_downgrade(tsv, FALSE); |
f2791508 | 3404 | tmps = SvPVX(tsv); |
85c16d83 | 3405 | } |
05404ffe JH |
3406 | # ifdef USE_ITHREADS |
3407 | # ifdef HAS_CRYPT_R | |
3408 | if (!PL_reentrant_buffer->_crypt_struct_buffer) { | |
3409 | /* This should be threadsafe because in ithreads there is only | |
3410 | * one thread per interpreter. If this would not be true, | |
3411 | * we would need a mutex to protect this malloc. */ | |
3412 | PL_reentrant_buffer->_crypt_struct_buffer = | |
3413 | (struct crypt_data *)safemalloc(sizeof(struct crypt_data)); | |
3414 | #if defined(__GLIBC__) || defined(__EMX__) | |
3415 | if (PL_reentrant_buffer->_crypt_struct_buffer) { | |
3416 | PL_reentrant_buffer->_crypt_struct_buffer->initialized = 0; | |
3417 | /* work around glibc-2.2.5 bug */ | |
3418 | PL_reentrant_buffer->_crypt_struct_buffer->current_saltbits = 0; | |
3419 | } | |
05404ffe | 3420 | #endif |
6ab58e4d | 3421 | } |
05404ffe JH |
3422 | # endif /* HAS_CRYPT_R */ |
3423 | # endif /* USE_ITHREADS */ | |
5f74f29c | 3424 | # ifdef FCRYPT |
2d8e6c8d | 3425 | sv_setpv(TARG, fcrypt(tmps, SvPV(right, n_a))); |
5f74f29c | 3426 | # else |
2d8e6c8d | 3427 | sv_setpv(TARG, PerlProc_crypt(tmps, SvPV(right, n_a))); |
5f74f29c | 3428 | # endif |
4808266b JH |
3429 | SETs(TARG); |
3430 | RETURN; | |
79072805 | 3431 | #else |
b13b2135 | 3432 | DIE(aTHX_ |
79072805 LW |
3433 | "The crypt() function is unimplemented due to excessive paranoia."); |
3434 | #endif | |
79072805 LW |
3435 | } |
3436 | ||
3437 | PP(pp_ucfirst) | |
3438 | { | |
39644a26 | 3439 | dSP; |
79072805 | 3440 | SV *sv = TOPs; |
a0ed51b3 LW |
3441 | register U8 *s; |
3442 | STRLEN slen; | |
3443 | ||
d104a74c | 3444 | SvGETMAGIC(sv); |
3a2263fe RGS |
3445 | if (DO_UTF8(sv) && |
3446 | (s = (U8*)SvPV_nomg(sv, slen)) && slen && | |
3447 | UTF8_IS_START(*s)) { | |
e7ae6809 | 3448 | U8 tmpbuf[UTF8_MAXLEN_UCLC+1]; |
44bc797b JH |
3449 | STRLEN ulen; |
3450 | STRLEN tculen; | |
a0ed51b3 | 3451 | |
44bc797b | 3452 | utf8_to_uvchr(s, &ulen); |
44bc797b JH |
3453 | toTITLE_utf8(s, tmpbuf, &tculen); |
3454 | utf8_to_uvchr(tmpbuf, 0); | |
3455 | ||
3456 | if (!SvPADTMP(sv) || SvREADONLY(sv)) { | |
a0ed51b3 | 3457 | dTARGET; |
3a2263fe RGS |
3458 | /* slen is the byte length of the whole SV. |
3459 | * ulen is the byte length of the original Unicode character | |
3460 | * stored as UTF-8 at s. | |
3461 | * tculen is the byte length of the freshly titlecased | |
3462 | * Unicode character stored as UTF-8 at tmpbuf. | |
3463 | * We first set the result to be the titlecased character, | |
3464 | * and then append the rest of the SV data. */ | |
44bc797b | 3465 | sv_setpvn(TARG, (char*)tmpbuf, tculen); |
3a2263fe RGS |
3466 | if (slen > ulen) |
3467 | sv_catpvn(TARG, (char*)(s + ulen), slen - ulen); | |
7e2040f0 | 3468 | SvUTF8_on(TARG); |
a0ed51b3 LW |
3469 | SETs(TARG); |
3470 | } | |
3471 | else { | |
d104a74c | 3472 | s = (U8*)SvPV_force_nomg(sv, slen); |
44bc797b | 3473 | Copy(tmpbuf, s, tculen, U8); |
a0ed51b3 | 3474 | } |
a0ed51b3 | 3475 | } |
626727d5 | 3476 | else { |
014822e4 | 3477 | if (!SvPADTMP(sv) || SvREADONLY(sv)) { |
31351b04 | 3478 | dTARGET; |
7e2040f0 | 3479 | SvUTF8_off(TARG); /* decontaminate */ |
d104a74c | 3480 | sv_setsv_nomg(TARG, sv); |
31351b04 JS |
3481 | sv = TARG; |
3482 | SETs(sv); | |
3483 | } | |
d104a74c | 3484 | s = (U8*)SvPV_force_nomg(sv, slen); |
31351b04 | 3485 | if (*s) { |
2de3dbcc | 3486 | if (IN_LOCALE_RUNTIME) { |
31351b04 JS |
3487 | TAINT; |
3488 | SvTAINTED_on(sv); | |
3489 | *s = toUPPER_LC(*s); | |
3490 | } | |
3491 | else | |
3492 | *s = toUPPER(*s); | |
bbce6d69 | 3493 | } |
bbce6d69 | 3494 | } |
d104a74c | 3495 | SvSETMAGIC(sv); |
79072805 LW |
3496 | RETURN; |
3497 | } | |
3498 | ||
3499 | PP(pp_lcfirst) | |
3500 | { | |
39644a26 | 3501 | dSP; |
79072805 | 3502 | SV *sv = TOPs; |
a0ed51b3 LW |
3503 | register U8 *s; |
3504 | STRLEN slen; | |
3505 | ||
d104a74c | 3506 | SvGETMAGIC(sv); |
3a2263fe RGS |
3507 | if (DO_UTF8(sv) && |
3508 | (s = (U8*)SvPV_nomg(sv, slen)) && slen && | |
3509 | UTF8_IS_START(*s)) { | |
ba210ebe | 3510 | STRLEN ulen; |
e7ae6809 | 3511 | U8 tmpbuf[UTF8_MAXLEN_UCLC+1]; |
a0ed51b3 | 3512 | U8 *tend; |
9041c2e3 | 3513 | UV uv; |
a0ed51b3 | 3514 | |
44bc797b | 3515 | toLOWER_utf8(s, tmpbuf, &ulen); |
a2a2844f | 3516 | uv = utf8_to_uvchr(tmpbuf, 0); |
9041c2e3 | 3517 | tend = uvchr_to_utf8(tmpbuf, uv); |
a0ed51b3 | 3518 | |
eb160463 | 3519 | if (!SvPADTMP(sv) || (STRLEN)(tend - tmpbuf) != ulen || SvREADONLY(sv)) { |
a0ed51b3 | 3520 | dTARGET; |
dfe13c55 | 3521 | sv_setpvn(TARG, (char*)tmpbuf, tend - tmpbuf); |
3a2263fe RGS |
3522 | if (slen > ulen) |
3523 | sv_catpvn(TARG, (char*)(s + ulen), slen - ulen); | |
7e2040f0 | 3524 | SvUTF8_on(TARG); |
a0ed51b3 LW |
3525 | SETs(TARG); |
3526 | } | |
3527 | else { | |
d104a74c | 3528 | s = (U8*)SvPV_force_nomg(sv, slen); |
a0ed51b3 LW |
3529 | Copy(tmpbuf, s, ulen, U8); |
3530 | } | |
a0ed51b3 | 3531 | } |
626727d5 | 3532 | else { |
014822e4 | 3533 | if (!SvPADTMP(sv) || SvREADONLY(sv)) { |
31351b04 | 3534 | dTARGET; |
7e2040f0 | 3535 | SvUTF8_off(TARG); /* decontaminate */ |
d104a74c | 3536 | sv_setsv_nomg(TARG, sv); |
31351b04 JS |
3537 | sv = TARG; |
3538 | SETs(sv); | |
3539 | } | |
d104a74c | 3540 | s = (U8*)SvPV_force_nomg(sv, slen); |
31351b04 | 3541 | if (*s) { |
2de3dbcc | 3542 | if (IN_LOCALE_RUNTIME) { |
31351b04 JS |
3543 | TAINT; |
3544 | SvTAINTED_on(sv); | |
3545 | *s = toLOWER_LC(*s); | |
3546 | } | |
3547 | else | |
3548 | *s = toLOWER(*s); | |
bbce6d69 | 3549 | } |
bbce6d69 | 3550 | } |
d104a74c | 3551 | SvSETMAGIC(sv); |
79072805 LW |
3552 | RETURN; |
3553 | } | |
3554 | ||
3555 | PP(pp_uc) | |
3556 | { | |
39644a26 | 3557 | dSP; |
79072805 | 3558 | SV *sv = TOPs; |
a0ed51b3 | 3559 | register U8 *s; |
463ee0b2 | 3560 | STRLEN len; |
79072805 | 3561 | |
d104a74c | 3562 | SvGETMAGIC(sv); |
7e2040f0 | 3563 | if (DO_UTF8(sv)) { |
a0ed51b3 | 3564 | dTARGET; |
ba210ebe | 3565 | STRLEN ulen; |
a0ed51b3 LW |
3566 | register U8 *d; |
3567 | U8 *send; | |
e7ae6809 | 3568 | U8 tmpbuf[UTF8_MAXLEN_UCLC+1]; |
a0ed51b3 | 3569 | |
d104a74c | 3570 | s = (U8*)SvPV_nomg(sv,len); |
a5a20234 | 3571 | if (!len) { |
7e2040f0 | 3572 | SvUTF8_off(TARG); /* decontaminate */ |
a5a20234 LW |
3573 | sv_setpvn(TARG, "", 0); |
3574 | SETs(TARG); | |
a0ed51b3 LW |
3575 | } |
3576 | else { | |
98b27f73 JH |
3577 | STRLEN nchar = utf8_length(s, s + len); |
3578 | ||
31351b04 | 3579 | (void)SvUPGRADE(TARG, SVt_PV); |
98b27f73 | 3580 | SvGROW(TARG, (nchar * UTF8_MAXLEN_UCLC) + 1); |
31351b04 JS |
3581 | (void)SvPOK_only(TARG); |
3582 | d = (U8*)SvPVX(TARG); | |
3583 | send = s + len; | |
a2a2844f | 3584 | while (s < send) { |
6fdb5f96 | 3585 | toUPPER_utf8(s, tmpbuf, &ulen); |
a2a2844f JH |
3586 | Copy(tmpbuf, d, ulen, U8); |
3587 | d += ulen; | |
3588 | s += UTF8SKIP(s); | |
a0ed51b3 | 3589 | } |
31351b04 | 3590 | *d = '\0'; |
7e2040f0 | 3591 | SvUTF8_on(TARG); |
31351b04 JS |
3592 | SvCUR_set(TARG, d - (U8*)SvPVX(TARG)); |
3593 | SETs(TARG); | |
a0ed51b3 | 3594 | } |
a0ed51b3 | 3595 | } |
626727d5 | 3596 | else { |
014822e4 | 3597 | if (!SvPADTMP(sv) || SvREADONLY(sv)) { |
31351b04 | 3598 | dTARGET; |
7e2040f0 | 3599 | SvUTF8_off(TARG); /* decontaminate */ |
d104a74c | 3600 | sv_setsv_nomg(TARG, sv); |
31351b04 JS |
3601 | sv = TARG; |
3602 | SETs(sv); | |
3603 | } | |
d104a74c | 3604 | s = (U8*)SvPV_force_nomg(sv, len); |
31351b04 JS |
3605 | if (len) { |
3606 | register U8 *send = s + len; | |
3607 | ||
2de3dbcc | 3608 | if (IN_LOCALE_RUNTIME) { |
31351b04 JS |
3609 | TAINT; |
3610 | SvTAINTED_on(sv); | |
3611 | for (; s < send; s++) | |
3612 | *s = toUPPER_LC(*s); | |
3613 | } | |
3614 | else { | |
3615 | for (; s < send; s++) | |
3616 | *s = toUPPER(*s); | |
3617 | } | |
bbce6d69 | 3618 | } |
79072805 | 3619 | } |
d104a74c | 3620 | SvSETMAGIC(sv); |
79072805 LW |
3621 | RETURN; |
3622 | } | |
3623 | ||
3624 | PP(pp_lc) | |
3625 | { | |
39644a26 | 3626 | dSP; |
79072805 | 3627 | SV *sv = TOPs; |
a0ed51b3 | 3628 | register U8 *s; |
463ee0b2 | 3629 | STRLEN len; |
79072805 | 3630 | |
d104a74c | 3631 | SvGETMAGIC(sv); |
7e2040f0 | 3632 | if (DO_UTF8(sv)) { |
a0ed51b3 | 3633 | dTARGET; |
ba210ebe | 3634 | STRLEN ulen; |
a0ed51b3 LW |
3635 | register U8 *d; |
3636 | U8 *send; | |
e7ae6809 | 3637 | U8 tmpbuf[UTF8_MAXLEN_UCLC+1]; |
a0ed51b3 | 3638 | |
d104a74c | 3639 | s = (U8*)SvPV_nomg(sv,len); |
a5a20234 | 3640 | if (!len) { |
7e2040f0 | 3641 | SvUTF8_off(TARG); /* decontaminate */ |
a5a20234 LW |
3642 | sv_setpvn(TARG, "", 0); |
3643 | SETs(TARG); | |
a0ed51b3 LW |
3644 | } |
3645 | else { | |
98b27f73 JH |
3646 | STRLEN nchar = utf8_length(s, s + len); |
3647 | ||
31351b04 | 3648 | (void)SvUPGRADE(TARG, SVt_PV); |
98b27f73 | 3649 | SvGROW(TARG, (nchar * UTF8_MAXLEN_UCLC) + 1); |
31351b04 JS |
3650 | (void)SvPOK_only(TARG); |
3651 | d = (U8*)SvPVX(TARG); | |
3652 | send = s + len; | |
a2a2844f | 3653 | while (s < send) { |
6fdb5f96 JH |
3654 | UV uv = toLOWER_utf8(s, tmpbuf, &ulen); |
3655 | #define GREEK_CAPITAL_LETTER_SIGMA 0x03A3 /* Unicode */ | |
3656 | if (uv == GREEK_CAPITAL_LETTER_SIGMA) { | |
3657 | /* | |
3658 | * Now if the sigma is NOT followed by | |
3659 | * /$ignorable_sequence$cased_letter/; | |
3660 | * and it IS preceded by | |
3661 | * /$cased_letter$ignorable_sequence/; | |
3662 | * where $ignorable_sequence is | |
3663 | * [\x{2010}\x{AD}\p{Mn}]* | |
3664 | * and $cased_letter is | |
3665 | * [\p{Ll}\p{Lo}\p{Lt}] | |
3666 | * then it should be mapped to 0x03C2, | |
3667 | * (GREEK SMALL LETTER FINAL SIGMA), | |
3668 | * instead of staying 0x03A3. | |
3669 | * See lib/unicore/SpecCase.txt. | |
3670 | */ | |
3671 | } | |
a2a2844f JH |
3672 | Copy(tmpbuf, d, ulen, U8); |
3673 | d += ulen; | |
3674 | s += UTF8SKIP(s); | |
a0ed51b3 | 3675 | } |
31351b04 | 3676 | *d = '\0'; |
7e2040f0 | 3677 | SvUTF8_on(TARG); |
31351b04 JS |
3678 | SvCUR_set(TARG, d - (U8*)SvPVX(TARG)); |
3679 | SETs(TARG); | |
a0ed51b3 | 3680 | } |
79072805 | 3681 | } |
626727d5 | 3682 | else { |
014822e4 | 3683 | if (!SvPADTMP(sv) || SvREADONLY(sv)) { |
31351b04 | 3684 | dTARGET; |
7e2040f0 | 3685 | SvUTF8_off(TARG); /* decontaminate */ |
d104a74c | 3686 | sv_setsv_nomg(TARG, sv); |
31351b04 JS |
3687 | sv = TARG; |
3688 | SETs(sv); | |
a0ed51b3 | 3689 | } |
bbce6d69 | 3690 | |
d104a74c | 3691 | s = (U8*)SvPV_force_nomg(sv, len); |
31351b04 JS |
3692 | if (len) { |
3693 | register U8 *send = s + len; | |
bbce6d69 | 3694 | |
2de3dbcc | 3695 | if (IN_LOCALE_RUNTIME) { |
31351b04 JS |
3696 | TAINT; |
3697 | SvTAINTED_on(sv); | |
3698 | for (; s < send; s++) | |
3699 | *s = toLOWER_LC(*s); | |
3700 | } | |
3701 | else { | |
3702 | for (; s < send; s++) | |
3703 | *s = toLOWER(*s); | |
3704 | } | |
bbce6d69 | 3705 | } |
79072805 | 3706 | } |
d104a74c | 3707 | SvSETMAGIC(sv); |
79072805 LW |
3708 | RETURN; |
3709 | } | |
3710 | ||
a0d0e21e | 3711 | PP(pp_quotemeta) |
79072805 | 3712 | { |
39644a26 | 3713 | dSP; dTARGET; |
a0d0e21e LW |
3714 | SV *sv = TOPs; |
3715 | STRLEN len; | |
3716 | register char *s = SvPV(sv,len); | |
3717 | register char *d; | |
79072805 | 3718 | |
7e2040f0 | 3719 | SvUTF8_off(TARG); /* decontaminate */ |
a0d0e21e LW |
3720 | if (len) { |
3721 | (void)SvUPGRADE(TARG, SVt_PV); | |
c07a80fd | 3722 | SvGROW(TARG, (len * 2) + 1); |
a0d0e21e | 3723 | d = SvPVX(TARG); |
7e2040f0 | 3724 | if (DO_UTF8(sv)) { |
0dd2cdef | 3725 | while (len) { |
fd400ab9 | 3726 | if (UTF8_IS_CONTINUED(*s)) { |
0dd2cdef LW |
3727 | STRLEN ulen = UTF8SKIP(s); |
3728 | if (ulen > len) | |
3729 | ulen = len; | |
3730 | len -= ulen; | |
3731 | while (ulen--) | |
3732 | *d++ = *s++; | |
3733 | } | |
3734 | else { | |
3735 | if (!isALNUM(*s)) | |
3736 | *d++ = '\\'; | |
3737 | *d++ = *s++; | |
3738 | len--; | |
3739 | } | |
3740 | } | |
7e2040f0 | 3741 | SvUTF8_on(TARG); |
0dd2cdef LW |
3742 | } |
3743 | else { | |
3744 | while (len--) { | |
3745 | if (!isALNUM(*s)) | |
3746 | *d++ = '\\'; | |
3747 | *d++ = *s++; | |
3748 | } | |
79072805 | 3749 | } |
a0d0e21e LW |
3750 | *d = '\0'; |
3751 | SvCUR_set(TARG, d - SvPVX(TARG)); | |
3aa33fe5 | 3752 | (void)SvPOK_only_UTF8(TARG); |
79072805 | 3753 | } |
a0d0e21e LW |
3754 | else |
3755 | sv_setpvn(TARG, s, len); | |
3756 | SETs(TARG); | |
31351b04 JS |
3757 | if (SvSMAGICAL(TARG)) |
3758 | mg_set(TARG); | |
79072805 LW |
3759 | RETURN; |
3760 | } | |
3761 | ||
a0d0e21e | 3762 | /* Arrays. */ |
79072805 | 3763 | |
a0d0e21e | 3764 | PP(pp_aslice) |
79072805 | 3765 | { |
39644a26 | 3766 | dSP; dMARK; dORIGMARK; |
a0d0e21e LW |
3767 | register SV** svp; |
3768 | register AV* av = (AV*)POPs; | |
78f9721b | 3769 | register I32 lval = (PL_op->op_flags & OPf_MOD || LVRET); |
3280af22 | 3770 | I32 arybase = PL_curcop->cop_arybase; |
748a9306 | 3771 | I32 elem; |
79072805 | 3772 | |
a0d0e21e | 3773 | if (SvTYPE(av) == SVt_PVAV) { |
533c011a | 3774 | if (lval && PL_op->op_private & OPpLVAL_INTRO) { |
748a9306 | 3775 | I32 max = -1; |
924508f0 | 3776 | for (svp = MARK + 1; svp <= SP; svp++) { |
748a9306 LW |
3777 | elem = SvIVx(*svp); |
3778 | if (elem > max) | |
3779 | max = elem; | |
3780 | } | |
3781 | if (max > AvMAX(av)) | |
3782 | av_extend(av, max); | |
3783 | } | |
a0d0e21e | 3784 | while (++MARK <= SP) { |
748a9306 | 3785 | elem = SvIVx(*MARK); |
a0d0e21e | 3786 | |
748a9306 LW |
3787 | if (elem > 0) |
3788 | elem -= arybase; | |
a0d0e21e LW |
3789 | svp = av_fetch(av, elem, lval); |
3790 | if (lval) { | |
3280af22 | 3791 | if (!svp || *svp == &PL_sv_undef) |
cea2e8a9 | 3792 | DIE(aTHX_ PL_no_aelem, elem); |
533c011a | 3793 | if (PL_op->op_private & OPpLVAL_INTRO) |
161b7d16 | 3794 | save_aelem(av, elem, svp); |
79072805 | 3795 | } |
3280af22 | 3796 | *MARK = svp ? *svp : &PL_sv_undef; |
79072805 LW |
3797 | } |
3798 | } | |
748a9306 | 3799 | if (GIMME != G_ARRAY) { |
a0d0e21e LW |
3800 | MARK = ORIGMARK; |
3801 | *++MARK = *SP; | |
3802 | SP = MARK; | |
3803 | } | |
79072805 LW |
3804 | RETURN; |
3805 | } | |
3806 | ||
3807 | /* Associative arrays. */ | |
3808 | ||
3809 | PP(pp_each) | |
3810 | { | |
39644a26 | 3811 | dSP; |
79072805 | 3812 | HV *hash = (HV*)POPs; |
c07a80fd | 3813 | HE *entry; |
54310121 | 3814 | I32 gimme = GIMME_V; |
8ec5e241 | 3815 | |
c07a80fd | 3816 | PUTBACK; |
c750a3ec | 3817 | /* might clobber stack_sp */ |
6d822dc4 | 3818 | entry = hv_iternext(hash); |
c07a80fd | 3819 | SPAGAIN; |
79072805 | 3820 | |
79072805 LW |
3821 | EXTEND(SP, 2); |
3822 | if (entry) { | |
574c8022 | 3823 | SV* sv = hv_iterkeysv(entry); |
574c8022 | 3824 | PUSHs(sv); /* won't clobber stack_sp */ |
54310121 | 3825 | if (gimme == G_ARRAY) { |
59af0135 | 3826 | SV *val; |
c07a80fd | 3827 | PUTBACK; |
c750a3ec | 3828 | /* might clobber stack_sp */ |
6d822dc4 | 3829 | val = hv_iterval(hash, entry); |
c07a80fd | 3830 | SPAGAIN; |
59af0135 | 3831 | PUSHs(val); |
79072805 | 3832 | } |
79072805 | 3833 | } |
54310121 | 3834 | else if (gimme == G_SCALAR) |
79072805 LW |
3835 | RETPUSHUNDEF; |
3836 | ||
3837 | RETURN; | |
3838 | } | |
3839 | ||
3840 | PP(pp_values) | |
3841 | { | |
cea2e8a9 | 3842 | return do_kv(); |
79072805 LW |
3843 | } |
3844 | ||
3845 | PP(pp_keys) | |
3846 | { | |
cea2e8a9 | 3847 | return do_kv(); |
79072805 LW |
3848 | } |
3849 | ||
3850 | PP(pp_delete) | |
3851 | { | |
39644a26 | 3852 | dSP; |
54310121 | 3853 | I32 gimme = GIMME_V; |
3854 | I32 discard = (gimme == G_VOID) ? G_DISCARD : 0; | |
79072805 | 3855 | SV *sv; |
5f05dabc | 3856 | HV *hv; |
3857 | ||
533c011a | 3858 | if (PL_op->op_private & OPpSLICE) { |
5f05dabc | 3859 | dMARK; dORIGMARK; |
97fcbf96 | 3860 | U32 hvtype; |
5f05dabc | 3861 | hv = (HV*)POPs; |
97fcbf96 | 3862 | hvtype = SvTYPE(hv); |
01020589 GS |
3863 | if (hvtype == SVt_PVHV) { /* hash element */ |
3864 | while (++MARK <= SP) { | |
ae77835f | 3865 | sv = hv_delete_ent(hv, *MARK, discard, 0); |
01020589 GS |
3866 | *MARK = sv ? sv : &PL_sv_undef; |
3867 | } | |
5f05dabc | 3868 | } |
6d822dc4 MS |
3869 | else if (hvtype == SVt_PVAV) { /* array element */ |
3870 | if (PL_op->op_flags & OPf_SPECIAL) { | |
3871 | while (++MARK <= SP) { | |
3872 | sv = av_delete((AV*)hv, SvIV(*MARK), discard); | |
3873 | *MARK = sv ? sv : &PL_sv_undef; | |
3874 | } | |
3875 | } | |
01020589 GS |
3876 | } |
3877 | else | |
3878 | DIE(aTHX_ "Not a HASH reference"); | |
54310121 | 3879 | if (discard) |
3880 | SP = ORIGMARK; | |
3881 | else if (gimme == G_SCALAR) { | |
5f05dabc | 3882 | MARK = ORIGMARK; |
3883 | *++MARK = *SP; | |
3884 | SP = MARK; | |
3885 | } | |
3886 | } | |
3887 | else { | |
3888 | SV *keysv = POPs; | |
3889 | hv = (HV*)POPs; | |
97fcbf96 MB |
3890 | if (SvTYPE(hv) == SVt_PVHV) |
3891 | sv = hv_delete_ent(hv, keysv, discard, 0); | |
01020589 GS |
3892 | else if (SvTYPE(hv) == SVt_PVAV) { |
3893 | if (PL_op->op_flags & OPf_SPECIAL) | |
3894 | sv = av_delete((AV*)hv, SvIV(keysv), discard); | |
af288a60 HS |
3895 | else |
3896 | DIE(aTHX_ "panic: avhv_delete no longer supported"); | |
01020589 | 3897 | } |
97fcbf96 | 3898 | else |
cea2e8a9 | 3899 | DIE(aTHX_ "Not a HASH reference"); |
5f05dabc | 3900 | if (!sv) |
3280af22 | 3901 | sv = &PL_sv_undef; |
54310121 | 3902 | if (!discard) |
3903 | PUSHs(sv); | |
79072805 | 3904 | } |
79072805 LW |
3905 | RETURN; |
3906 | } | |
3907 | ||
a0d0e21e | 3908 | PP(pp_exists) |
79072805 | 3909 | { |
39644a26 | 3910 | dSP; |
afebc493 GS |
3911 | SV *tmpsv; |
3912 | HV *hv; | |
3913 | ||
3914 | if (PL_op->op_private & OPpEXISTS_SUB) { | |
3915 | GV *gv; | |
3916 | CV *cv; | |
3917 | SV *sv = POPs; | |
3918 | cv = sv_2cv(sv, &hv, &gv, FALSE); | |
3919 | if (cv) | |
3920 | RETPUSHYES; | |
3921 | if (gv && isGV(gv) && GvCV(gv) && !GvCVGEN(gv)) | |
3922 | RETPUSHYES; | |
3923 | RETPUSHNO; | |
3924 | } | |
3925 | tmpsv = POPs; | |
3926 | hv = (HV*)POPs; | |
c750a3ec | 3927 | if (SvTYPE(hv) == SVt_PVHV) { |
ae77835f | 3928 | if (hv_exists_ent(hv, tmpsv, 0)) |
c750a3ec | 3929 | RETPUSHYES; |
ef54e1a4 JH |
3930 | } |
3931 | else if (SvTYPE(hv) == SVt_PVAV) { | |
01020589 GS |
3932 | if (PL_op->op_flags & OPf_SPECIAL) { /* array element */ |
3933 | if (av_exists((AV*)hv, SvIV(tmpsv))) | |
3934 | RETPUSHYES; | |
3935 | } | |
ef54e1a4 JH |
3936 | } |
3937 | else { | |
cea2e8a9 | 3938 | DIE(aTHX_ "Not a HASH reference"); |
a0d0e21e | 3939 | } |
a0d0e21e LW |
3940 | RETPUSHNO; |
3941 | } | |
79072805 | 3942 | |
a0d0e21e LW |
3943 | PP(pp_hslice) |
3944 | { | |
39644a26 | 3945 | dSP; dMARK; dORIGMARK; |
a0d0e21e | 3946 | register HV *hv = (HV*)POPs; |
78f9721b | 3947 | register I32 lval = (PL_op->op_flags & OPf_MOD || LVRET); |
eb85dfd3 DM |
3948 | bool localizing = PL_op->op_private & OPpLVAL_INTRO ? TRUE : FALSE; |
3949 | bool other_magic = FALSE; | |
79072805 | 3950 | |
eb85dfd3 DM |
3951 | if (localizing) { |
3952 | MAGIC *mg; | |
3953 | HV *stash; | |
3954 | ||
3955 | other_magic = mg_find((SV*)hv, PERL_MAGIC_env) || | |
3956 | ((mg = mg_find((SV*)hv, PERL_MAGIC_tied)) | |
3957 | /* Try to preserve the existenceness of a tied hash | |
3958 | * element by using EXISTS and DELETE if possible. | |
3959 | * Fallback to FETCH and STORE otherwise */ | |
3960 | && (stash = SvSTASH(SvRV(SvTIED_obj((SV*)hv, mg)))) | |
3961 | && gv_fetchmethod_autoload(stash, "EXISTS", TRUE) | |
3962 | && gv_fetchmethod_autoload(stash, "DELETE", TRUE)); | |
3963 | } | |
3964 | ||
6d822dc4 MS |
3965 | while (++MARK <= SP) { |
3966 | SV *keysv = *MARK; | |
3967 | SV **svp; | |
3968 | HE *he; | |
3969 | bool preeminent = FALSE; | |
0ebe0038 | 3970 | |
6d822dc4 MS |
3971 | if (localizing) { |
3972 | preeminent = SvRMAGICAL(hv) && !other_magic ? 1 : | |
3973 | hv_exists_ent(hv, keysv, 0); | |
3974 | } | |
eb85dfd3 | 3975 | |
6d822dc4 MS |
3976 | he = hv_fetch_ent(hv, keysv, lval, 0); |
3977 | svp = he ? &HeVAL(he) : 0; | |
eb85dfd3 | 3978 | |
6d822dc4 MS |
3979 | if (lval) { |
3980 | if (!svp || *svp == &PL_sv_undef) { | |
3981 | STRLEN n_a; | |
3982 | DIE(aTHX_ PL_no_helem, SvPV(keysv, n_a)); | |
3983 | } | |
3984 | if (localizing) { | |
3985 | if (preeminent) | |
3986 | save_helem(hv, keysv, svp); | |
3987 | else { | |
3988 | STRLEN keylen; | |
3989 | char *key = SvPV(keysv, keylen); | |
3990 | SAVEDELETE(hv, savepvn(key,keylen), keylen); | |
1f5346dc | 3991 | } |
6d822dc4 MS |
3992 | } |
3993 | } | |
3994 | *MARK = svp ? *svp : &PL_sv_undef; | |
79072805 | 3995 | } |
a0d0e21e LW |
3996 | if (GIMME != G_ARRAY) { |
3997 | MARK = ORIGMARK; | |
3998 | *++MARK = *SP; | |
3999 | SP = MARK; | |
79072805 | 4000 | } |
a0d0e21e LW |
4001 | RETURN; |
4002 | } | |
4003 | ||
4004 | /* List operators. */ | |
4005 | ||
4006 | PP(pp_list) | |
4007 | { | |
39644a26 | 4008 | dSP; dMARK; |
a0d0e21e LW |
4009 | if (GIMME != G_ARRAY) { |
4010 | if (++MARK <= SP) | |
4011 | *MARK = *SP; /* unwanted list, return last item */ | |
8990e307 | 4012 | else |
3280af22 | 4013 | *MARK = &PL_sv_undef; |
a0d0e21e | 4014 | SP = MARK; |
79072805 | 4015 | } |
a0d0e21e | 4016 | RETURN; |
79072805 LW |
4017 | } |
4018 | ||
a0d0e21e | 4019 | PP(pp_lslice) |
79072805 | 4020 | { |
39644a26 | 4021 | dSP; |
3280af22 NIS |
4022 | SV **lastrelem = PL_stack_sp; |
4023 | SV **lastlelem = PL_stack_base + POPMARK; | |
4024 | SV **firstlelem = PL_stack_base + POPMARK + 1; | |
a0d0e21e | 4025 | register SV **firstrelem = lastlelem + 1; |
3280af22 | 4026 | I32 arybase = PL_curcop->cop_arybase; |
533c011a | 4027 | I32 lval = PL_op->op_flags & OPf_MOD; |
4633a7c4 | 4028 | I32 is_something_there = lval; |
79072805 | 4029 | |
a0d0e21e LW |
4030 | register I32 max = lastrelem - lastlelem; |
4031 | register SV **lelem; | |
4032 | register I32 ix; | |
4033 | ||
4034 | if (GIMME != G_ARRAY) { | |
748a9306 LW |
4035 | ix = SvIVx(*lastlelem); |
4036 | if (ix < 0) | |
4037 | ix += max; | |
4038 | else | |
4039 | ix -= arybase; | |
a0d0e21e | 4040 | if (ix < 0 || ix >= max) |
3280af22 | 4041 | *firstlelem = &PL_sv_undef; |
a0d0e21e LW |
4042 | else |
4043 | *firstlelem = firstrelem[ix]; | |
4044 | SP = firstlelem; | |
4045 | RETURN; | |
4046 | } | |
4047 | ||
4048 | if (max == 0) { | |
4049 | SP = firstlelem - 1; | |
4050 | RETURN; | |
4051 | } | |
4052 | ||
4053 | for (lelem = firstlelem; lelem <= lastlelem; lelem++) { | |
748a9306 | 4054 | ix = SvIVx(*lelem); |
c73bf8e3 | 4055 | if (ix < 0) |
a0d0e21e | 4056 | ix += max; |
b13b2135 | 4057 | else |
748a9306 | 4058 | ix -= arybase; |
c73bf8e3 HS |
4059 | if (ix < 0 || ix >= max) |
4060 | *lelem = &PL_sv_undef; | |
4061 | else { | |
4062 | is_something_there = TRUE; | |
4063 | if (!(*lelem = firstrelem[ix])) | |
3280af22 | 4064 | *lelem = &PL_sv_undef; |
748a9306 | 4065 | } |
79072805 | 4066 | } |
4633a7c4 LW |
4067 | if (is_something_there) |
4068 | SP = lastlelem; | |
4069 | else | |
4070 | SP = firstlelem - 1; | |
79072805 LW |
4071 | RETURN; |
4072 | } | |
4073 | ||
a0d0e21e LW |
4074 | PP(pp_anonlist) |
4075 | { | |
39644a26 | 4076 | dSP; dMARK; dORIGMARK; |
a0d0e21e | 4077 | I32 items = SP - MARK; |
44a8e56a | 4078 | SV *av = sv_2mortal((SV*)av_make(items, MARK+1)); |
4079 | SP = ORIGMARK; /* av_make() might realloc stack_sp */ | |
4080 | XPUSHs(av); | |
a0d0e21e LW |
4081 | RETURN; |
4082 | } | |
4083 | ||
4084 | PP(pp_anonhash) | |
79072805 | 4085 | { |
39644a26 | 4086 | dSP; dMARK; dORIGMARK; |
a0d0e21e LW |
4087 | HV* hv = (HV*)sv_2mortal((SV*)newHV()); |
4088 | ||
4089 | while (MARK < SP) { | |
4090 | SV* key = *++MARK; | |
a0d0e21e LW |
4091 | SV *val = NEWSV(46, 0); |
4092 | if (MARK < SP) | |
4093 | sv_setsv(val, *++MARK); | |
e476b1b5 | 4094 | else if (ckWARN(WARN_MISC)) |
9014280d | 4095 | Perl_warner(aTHX_ packWARN(WARN_MISC), "Odd number of elements in anonymous hash"); |
f12c7020 | 4096 | (void)hv_store_ent(hv,key,val,0); |
79072805 | 4097 | } |
a0d0e21e LW |
4098 | SP = ORIGMARK; |
4099 | XPUSHs((SV*)hv); | |
79072805 LW |
4100 | RETURN; |
4101 | } | |
4102 | ||
a0d0e21e | 4103 | PP(pp_splice) |
79072805 | 4104 | { |
39644a26 | 4105 | dSP; dMARK; dORIGMARK; |
a0d0e21e LW |
4106 | register AV *ary = (AV*)*++MARK; |
4107 | register SV **src; | |
4108 | register SV **dst; | |
4109 | register I32 i; | |
4110 | register I32 offset; | |
4111 | register I32 length; | |
4112 | I32 newlen; | |
4113 | I32 after; | |
4114 | I32 diff; | |
4115 | SV **tmparyval = 0; | |
93965878 NIS |
4116 | MAGIC *mg; |
4117 | ||
14befaf4 | 4118 | if ((mg = SvTIED_mg((SV*)ary, PERL_MAGIC_tied))) { |
33c27489 | 4119 | *MARK-- = SvTIED_obj((SV*)ary, mg); |
93965878 | 4120 | PUSHMARK(MARK); |
8ec5e241 | 4121 | PUTBACK; |
a60c0954 | 4122 | ENTER; |
864dbfa3 | 4123 | call_method("SPLICE",GIMME_V); |
a60c0954 | 4124 | LEAVE; |
93965878 NIS |
4125 | SPAGAIN; |
4126 | RETURN; | |
4127 | } | |
79072805 | 4128 | |
a0d0e21e | 4129 | SP++; |
79072805 | 4130 | |
a0d0e21e | 4131 | if (++MARK < SP) { |
84902520 | 4132 | offset = i = SvIVx(*MARK); |
a0d0e21e | 4133 | if (offset < 0) |
93965878 | 4134 | offset += AvFILLp(ary) + 1; |
a0d0e21e | 4135 | else |
3280af22 | 4136 | offset -= PL_curcop->cop_arybase; |
84902520 | 4137 | if (offset < 0) |
cea2e8a9 | 4138 | DIE(aTHX_ PL_no_aelem, i); |
a0d0e21e LW |
4139 | if (++MARK < SP) { |
4140 | length = SvIVx(*MARK++); | |
48cdf507 GA |
4141 | if (length < 0) { |
4142 | length += AvFILLp(ary) - offset + 1; | |
4143 | if (length < 0) | |
4144 | length = 0; | |
4145 | } | |
79072805 LW |
4146 | } |
4147 | else | |
a0d0e21e | 4148 | length = AvMAX(ary) + 1; /* close enough to infinity */ |
79072805 | 4149 | } |
a0d0e21e LW |
4150 | else { |
4151 | offset = 0; | |
4152 | length = AvMAX(ary) + 1; | |
4153 | } | |
8cbc2e3b JH |
4154 | if (offset > AvFILLp(ary) + 1) { |
4155 | if (ckWARN(WARN_MISC)) | |
9014280d | 4156 | Perl_warner(aTHX_ packWARN(WARN_MISC), "splice() offset past end of array" ); |
93965878 | 4157 | offset = AvFILLp(ary) + 1; |
8cbc2e3b | 4158 | } |
93965878 | 4159 | after = AvFILLp(ary) + 1 - (offset + length); |
a0d0e21e LW |
4160 | if (after < 0) { /* not that much array */ |
4161 | length += after; /* offset+length now in array */ | |
4162 | after = 0; | |
4163 | if (!AvALLOC(ary)) | |
4164 | av_extend(ary, 0); | |
4165 | } | |
4166 | ||
4167 | /* At this point, MARK .. SP-1 is our new LIST */ | |
4168 | ||
4169 | newlen = SP - MARK; | |
4170 | diff = newlen - length; | |
13d7cbc1 GS |
4171 | if (newlen && !AvREAL(ary) && AvREIFY(ary)) |
4172 | av_reify(ary); | |
a0d0e21e LW |
4173 | |
4174 | if (diff < 0) { /* shrinking the area */ | |
4175 | if (newlen) { | |
4176 | New(451, tmparyval, newlen, SV*); /* so remember insertion */ | |
4177 | Copy(MARK, tmparyval, newlen, SV*); | |
79072805 | 4178 | } |
a0d0e21e LW |
4179 | |
4180 | MARK = ORIGMARK + 1; | |
4181 | if (GIMME == G_ARRAY) { /* copy return vals to stack */ | |
4182 | MEXTEND(MARK, length); | |
4183 | Copy(AvARRAY(ary)+offset, MARK, length, SV*); | |
4184 | if (AvREAL(ary)) { | |
bbce6d69 | 4185 | EXTEND_MORTAL(length); |
36477c24 | 4186 | for (i = length, dst = MARK; i; i--) { |
d689ffdd | 4187 | sv_2mortal(*dst); /* free them eventualy */ |
36477c24 | 4188 | dst++; |
4189 | } | |
a0d0e21e LW |
4190 | } |
4191 | MARK += length - 1; | |
79072805 | 4192 | } |
a0d0e21e LW |
4193 | else { |
4194 | *MARK = AvARRAY(ary)[offset+length-1]; | |
4195 | if (AvREAL(ary)) { | |
d689ffdd | 4196 | sv_2mortal(*MARK); |
a0d0e21e LW |
4197 | for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--) |
4198 | SvREFCNT_dec(*dst++); /* free them now */ | |
79072805 | 4199 | } |
a0d0e21e | 4200 | } |
93965878 | 4201 | AvFILLp(ary) += diff; |
a0d0e21e LW |
4202 | |
4203 | /* pull up or down? */ | |
4204 | ||
4205 | if (offset < after) { /* easier to pull up */ | |
4206 | if (offset) { /* esp. if nothing to pull */ | |
4207 | src = &AvARRAY(ary)[offset-1]; | |
4208 | dst = src - diff; /* diff is negative */ | |
4209 | for (i = offset; i > 0; i--) /* can't trust Copy */ | |
4210 | *dst-- = *src--; | |
79072805 | 4211 | } |
a0d0e21e LW |
4212 | dst = AvARRAY(ary); |
4213 | SvPVX(ary) = (char*)(AvARRAY(ary) - diff); /* diff is negative */ | |
4214 | AvMAX(ary) += diff; | |
4215 | } | |
4216 | else { | |
4217 | if (after) { /* anything to pull down? */ | |
4218 | src = AvARRAY(ary) + offset + length; | |
4219 | dst = src + diff; /* diff is negative */ | |
4220 | Move(src, dst, after, SV*); | |
79072805 | 4221 | } |
93965878 | 4222 | dst = &AvARRAY(ary)[AvFILLp(ary)+1]; |
a0d0e21e LW |
4223 | /* avoid later double free */ |
4224 | } | |
4225 | i = -diff; | |
4226 | while (i) | |
3280af22 | 4227 | dst[--i] = &PL_sv_undef; |
a0d0e21e LW |
4228 | |
4229 | if (newlen) { | |
4230 | for (src = tmparyval, dst = AvARRAY(ary) + offset; | |
4231 | newlen; newlen--) { | |
4232 | *dst = NEWSV(46, 0); | |
4233 | sv_setsv(*dst++, *src++); | |
79072805 | 4234 | } |
a0d0e21e LW |
4235 | Safefree(tmparyval); |
4236 | } | |
4237 | } | |
4238 | else { /* no, expanding (or same) */ | |
4239 | if (length) { | |
4240 | New(452, tmparyval, length, SV*); /* so remember deletion */ | |
4241 | Copy(AvARRAY(ary)+offset, tmparyval, length, SV*); | |
4242 | } | |
4243 | ||
4244 | if (diff > 0) { /* expanding */ | |
4245 | ||
4246 | /* push up or down? */ | |
4247 | ||
4248 | if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) { | |
4249 | if (offset) { | |
4250 | src = AvARRAY(ary); | |
4251 | dst = src - diff; | |
4252 | Move(src, dst, offset, SV*); | |
79072805 | 4253 | } |
a0d0e21e LW |
4254 | SvPVX(ary) = (char*)(AvARRAY(ary) - diff);/* diff is positive */ |
4255 | AvMAX(ary) += diff; | |
93965878 | 4256 | AvFILLp(ary) += diff; |
79072805 LW |
4257 | } |
4258 | else { | |
93965878 NIS |
4259 | if (AvFILLp(ary) + diff >= AvMAX(ary)) /* oh, well */ |
4260 | av_extend(ary, AvFILLp(ary) + diff); | |
4261 | AvFILLp(ary) += diff; | |
a0d0e21e LW |
4262 | |
4263 | if (after) { | |
93965878 | 4264 | dst = AvARRAY(ary) + AvFILLp(ary); |
a0d0e21e LW |
4265 | src = dst - diff; |
4266 | for (i = after; i; i--) { | |
4267 | *dst-- = *src--; | |
4268 | } | |
79072805 LW |
4269 | } |
4270 | } | |
a0d0e21e LW |
4271 | } |
4272 | ||
4273 | for (src = MARK, dst = AvARRAY(ary) + offset; newlen; newlen--) { | |
4274 | *dst = NEWSV(46, 0); | |
4275 | sv_setsv(*dst++, *src++); | |
4276 | } | |
4277 | MARK = ORIGMARK + 1; | |
4278 | if (GIMME == G_ARRAY) { /* copy return vals to stack */ | |
4279 | if (length) { | |
4280 | Copy(tmparyval, MARK, length, SV*); | |
4281 | if (AvREAL(ary)) { | |
bbce6d69 | 4282 | EXTEND_MORTAL(length); |
36477c24 | 4283 | for (i = length, dst = MARK; i; i--) { |
d689ffdd | 4284 | sv_2mortal(*dst); /* free them eventualy */ |
36477c24 | 4285 | dst++; |
4286 | } | |
79072805 | 4287 | } |
a0d0e21e | 4288 | Safefree(tmparyval); |
79072805 | 4289 | } |
a0d0e21e LW |
4290 | MARK += length - 1; |
4291 | } | |
4292 | else if (length--) { | |
4293 | *MARK = tmparyval[length]; | |
4294 | if (AvREAL(ary)) { | |
d689ffdd | 4295 | sv_2mortal(*MARK); |
a0d0e21e LW |
4296 | while (length-- > 0) |
4297 | SvREFCNT_dec(tmparyval[length]); | |
79072805 | 4298 | } |
a0d0e21e | 4299 | Safefree(tmparyval); |
79072805 | 4300 | } |
a0d0e21e | 4301 | else |
3280af22 | 4302 | *MARK = &PL_sv_undef; |
79072805 | 4303 | } |
a0d0e21e | 4304 | SP = MARK; |
79072805 LW |
4305 | RETURN; |
4306 | } | |
4307 | ||
a0d0e21e | 4308 | PP(pp_push) |
79072805 | 4309 | { |
39644a26 | 4310 | dSP; dMARK; dORIGMARK; dTARGET; |
a0d0e21e | 4311 | register AV *ary = (AV*)*++MARK; |
3280af22 | 4312 | register SV *sv = &PL_sv_undef; |
93965878 | 4313 | MAGIC *mg; |
79072805 | 4314 | |
14befaf4 | 4315 | if ((mg = SvTIED_mg((SV*)ary, PERL_MAGIC_tied))) { |
33c27489 | 4316 | *MARK-- = SvTIED_obj((SV*)ary, mg); |
93965878 NIS |
4317 | PUSHMARK(MARK); |
4318 | PUTBACK; | |
a60c0954 | 4319 | ENTER; |
864dbfa3 | 4320 | call_method("PUSH",G_SCALAR|G_DISCARD); |
a60c0954 | 4321 | LEAVE; |
93965878 | 4322 | SPAGAIN; |
93965878 | 4323 | } |
a60c0954 NIS |
4324 | else { |
4325 | /* Why no pre-extend of ary here ? */ | |
4326 | for (++MARK; MARK <= SP; MARK++) { | |
4327 | sv = NEWSV(51, 0); | |
4328 | if (*MARK) | |
4329 | sv_setsv(sv, *MARK); | |
4330 | av_push(ary, sv); | |
4331 | } | |
79072805 LW |
4332 | } |
4333 | SP = ORIGMARK; | |
a0d0e21e | 4334 | PUSHi( AvFILL(ary) + 1 ); |
79072805 LW |
4335 | RETURN; |
4336 | } | |
4337 | ||
a0d0e21e | 4338 | PP(pp_pop) |
79072805 | 4339 | { |
39644a26 | 4340 | dSP; |
a0d0e21e LW |
4341 | AV *av = (AV*)POPs; |
4342 | SV *sv = av_pop(av); | |
d689ffdd | 4343 | if (AvREAL(av)) |
a0d0e21e LW |
4344 | (void)sv_2mortal(sv); |
4345 | PUSHs(sv); | |
79072805 | 4346 | RETURN; |
79072805 LW |
4347 | } |
4348 | ||
a0d0e21e | 4349 | PP(pp_shift) |
79072805 | 4350 | { |
39644a26 | 4351 | dSP; |
a0d0e21e LW |
4352 | AV *av = (AV*)POPs; |
4353 | SV *sv = av_shift(av); | |
79072805 | 4354 | EXTEND(SP, 1); |
a0d0e21e | 4355 | if (!sv) |
79072805 | 4356 | RETPUSHUNDEF; |
d689ffdd | 4357 | if (AvREAL(av)) |
a0d0e21e LW |
4358 | (void)sv_2mortal(sv); |
4359 | PUSHs(sv); | |
79072805 | 4360 | RETURN; |
79072805 LW |
4361 | } |
4362 | ||
a0d0e21e | 4363 | PP(pp_unshift) |
79072805 | 4364 | { |
39644a26 | 4365 | dSP; dMARK; dORIGMARK; dTARGET; |
a0d0e21e LW |
4366 | register AV *ary = (AV*)*++MARK; |
4367 | register SV *sv; | |
4368 | register I32 i = 0; | |
93965878 NIS |
4369 | MAGIC *mg; |
4370 | ||
14befaf4 | 4371 | if ((mg = SvTIED_mg((SV*)ary, PERL_MAGIC_tied))) { |
33c27489 | 4372 | *MARK-- = SvTIED_obj((SV*)ary, mg); |
7fd66d9d | 4373 | PUSHMARK(MARK); |
93965878 | 4374 | PUTBACK; |
a60c0954 | 4375 | ENTER; |
864dbfa3 | 4376 | call_method("UNSHIFT",G_SCALAR|G_DISCARD); |
a60c0954 | 4377 | LEAVE; |
93965878 | 4378 | SPAGAIN; |
93965878 | 4379 | } |
a60c0954 NIS |
4380 | else { |
4381 | av_unshift(ary, SP - MARK); | |
4382 | while (MARK < SP) { | |
4383 | sv = NEWSV(27, 0); | |
4384 | sv_setsv(sv, *++MARK); | |
4385 | (void)av_store(ary, i++, sv); | |
4386 | } | |
79072805 | 4387 | } |
a0d0e21e LW |
4388 | SP = ORIGMARK; |
4389 | PUSHi( AvFILL(ary) + 1 ); | |
79072805 | 4390 | RETURN; |
79072805 LW |
4391 | } |
4392 | ||
a0d0e21e | 4393 | PP(pp_reverse) |
79072805 | 4394 | { |
39644a26 | 4395 | dSP; dMARK; |
a0d0e21e LW |
4396 | register SV *tmp; |
4397 | SV **oldsp = SP; | |
79072805 | 4398 | |
a0d0e21e LW |
4399 | if (GIMME == G_ARRAY) { |
4400 | MARK++; | |
4401 | while (MARK < SP) { | |
4402 | tmp = *MARK; | |
4403 | *MARK++ = *SP; | |
4404 | *SP-- = tmp; | |
4405 | } | |
dd58a1ab | 4406 | /* safe as long as stack cannot get extended in the above */ |
a0d0e21e | 4407 | SP = oldsp; |
79072805 LW |
4408 | } |
4409 | else { | |
a0d0e21e LW |
4410 | register char *up; |
4411 | register char *down; | |
4412 | register I32 tmp; | |
4413 | dTARGET; | |
4414 | STRLEN len; | |
79072805 | 4415 | |
7e2040f0 | 4416 | SvUTF8_off(TARG); /* decontaminate */ |
a0d0e21e | 4417 | if (SP - MARK > 1) |
3280af22 | 4418 | do_join(TARG, &PL_sv_no, MARK, SP); |
a0d0e21e | 4419 | else |
54b9620d | 4420 | sv_setsv(TARG, (SP > MARK) ? *SP : DEFSV); |
a0d0e21e LW |
4421 | up = SvPV_force(TARG, len); |
4422 | if (len > 1) { | |
7e2040f0 | 4423 | if (DO_UTF8(TARG)) { /* first reverse each character */ |
dfe13c55 GS |
4424 | U8* s = (U8*)SvPVX(TARG); |
4425 | U8* send = (U8*)(s + len); | |
a0ed51b3 | 4426 | while (s < send) { |
d742c382 | 4427 | if (UTF8_IS_INVARIANT(*s)) { |
a0ed51b3 LW |
4428 | s++; |
4429 | continue; | |
4430 | } | |
4431 | else { | |
9041c2e3 | 4432 | if (!utf8_to_uvchr(s, 0)) |
a0dbb045 | 4433 | break; |
dfe13c55 | 4434 | up = (char*)s; |
a0ed51b3 | 4435 | s += UTF8SKIP(s); |
dfe13c55 | 4436 | down = (char*)(s - 1); |
a0dbb045 | 4437 | /* reverse this character */ |
a0ed51b3 LW |
4438 | while (down > up) { |
4439 | tmp = *up; | |
4440 | *up++ = *down; | |
eb160463 | 4441 | *down-- = (char)tmp; |
a0ed51b3 LW |
4442 | } |
4443 | } | |
4444 | } | |
4445 | up = SvPVX(TARG); | |
4446 | } | |
a0d0e21e LW |
4447 | down = SvPVX(TARG) + len - 1; |
4448 | while (down > up) { | |
4449 | tmp = *up; | |
4450 | *up++ = *down; | |
eb160463 | 4451 | *down-- = (char)tmp; |
a0d0e21e | 4452 | } |
3aa33fe5 | 4453 | (void)SvPOK_only_UTF8(TARG); |
79072805 | 4454 | } |
a0d0e21e LW |
4455 | SP = MARK + 1; |
4456 | SETTARG; | |
79072805 | 4457 | } |
a0d0e21e | 4458 | RETURN; |
79072805 LW |
4459 | } |
4460 | ||
a0d0e21e | 4461 | PP(pp_split) |
79072805 | 4462 | { |
39644a26 | 4463 | dSP; dTARG; |
a0d0e21e | 4464 | AV *ary; |
467f0320 | 4465 | register IV limit = POPi; /* note, negative is forever */ |
a0d0e21e LW |
4466 | SV *sv = POPs; |
4467 | STRLEN len; | |
4468 | register char *s = SvPV(sv, len); | |
1aa99e6b | 4469 | bool do_utf8 = DO_UTF8(sv); |
a0d0e21e | 4470 | char *strend = s + len; |
44a8e56a | 4471 | register PMOP *pm; |
d9f97599 | 4472 | register REGEXP *rx; |
a0d0e21e LW |
4473 | register SV *dstr; |
4474 | register char *m; | |
4475 | I32 iters = 0; | |
792b2c16 JH |
4476 | STRLEN slen = do_utf8 ? utf8_length((U8*)s, (U8*)strend) : (strend - s); |
4477 | I32 maxiters = slen + 10; | |
a0d0e21e LW |
4478 | I32 i; |
4479 | char *orig; | |
4480 | I32 origlimit = limit; | |
4481 | I32 realarray = 0; | |
4482 | I32 base; | |
3280af22 | 4483 | AV *oldstack = PL_curstack; |
54310121 | 4484 | I32 gimme = GIMME_V; |
3280af22 | 4485 | I32 oldsave = PL_savestack_ix; |
8ec5e241 NIS |
4486 | I32 make_mortal = 1; |
4487 | MAGIC *mg = (MAGIC *) NULL; | |
79072805 | 4488 | |
44a8e56a | 4489 | #ifdef DEBUGGING |
4490 | Copy(&LvTARGOFF(POPs), &pm, 1, PMOP*); | |
4491 | #else | |
4492 | pm = (PMOP*)POPs; | |
4493 | #endif | |
a0d0e21e | 4494 | if (!pm || !s) |
2269b42e | 4495 | DIE(aTHX_ "panic: pp_split"); |
aaa362c4 | 4496 | rx = PM_GETRE(pm); |
bbce6d69 | 4497 | |
4498 | TAINT_IF((pm->op_pmflags & PMf_LOCALE) && | |
4499 | (pm->op_pmflags & (PMf_WHITE | PMf_SKIPWHITE))); | |
4500 | ||
a30b2f1f | 4501 | RX_MATCH_UTF8_set(rx, do_utf8); |
d9f424b2 | 4502 | |
971a9dd3 GS |
4503 | if (pm->op_pmreplroot) { |
4504 | #ifdef USE_ITHREADS | |
dd2155a4 | 4505 | ary = GvAVn((GV*)PAD_SVl(INT2PTR(PADOFFSET, pm->op_pmreplroot))); |
971a9dd3 | 4506 | #else |
a0d0e21e | 4507 | ary = GvAVn((GV*)pm->op_pmreplroot); |
971a9dd3 GS |
4508 | #endif |
4509 | } | |
a0d0e21e | 4510 | else if (gimme != G_ARRAY) |
3280af22 | 4511 | ary = GvAVn(PL_defgv); |
79072805 | 4512 | else |
a0d0e21e LW |
4513 | ary = Nullav; |
4514 | if (ary && (gimme != G_ARRAY || (pm->op_pmflags & PMf_ONCE))) { | |
4515 | realarray = 1; | |
8ec5e241 | 4516 | PUTBACK; |
a0d0e21e LW |
4517 | av_extend(ary,0); |
4518 | av_clear(ary); | |
8ec5e241 | 4519 | SPAGAIN; |
14befaf4 | 4520 | if ((mg = SvTIED_mg((SV*)ary, PERL_MAGIC_tied))) { |
8ec5e241 | 4521 | PUSHMARK(SP); |
33c27489 | 4522 | XPUSHs(SvTIED_obj((SV*)ary, mg)); |
8ec5e241 NIS |
4523 | } |
4524 | else { | |
1c0b011c NIS |
4525 | if (!AvREAL(ary)) { |
4526 | AvREAL_on(ary); | |
abff13bb | 4527 | AvREIFY_off(ary); |
1c0b011c | 4528 | for (i = AvFILLp(ary); i >= 0; i--) |
3280af22 | 4529 | AvARRAY(ary)[i] = &PL_sv_undef; /* don't free mere refs */ |
1c0b011c NIS |
4530 | } |
4531 | /* temporarily switch stacks */ | |
3280af22 | 4532 | SWITCHSTACK(PL_curstack, ary); |
3b0d546b | 4533 | PL_curstackinfo->si_stack = ary; |
8ec5e241 | 4534 | make_mortal = 0; |
1c0b011c | 4535 | } |
79072805 | 4536 | } |
3280af22 | 4537 | base = SP - PL_stack_base; |
a0d0e21e LW |
4538 | orig = s; |
4539 | if (pm->op_pmflags & PMf_SKIPWHITE) { | |
bbce6d69 | 4540 | if (pm->op_pmflags & PMf_LOCALE) { |
4541 | while (isSPACE_LC(*s)) | |
4542 | s++; | |
4543 | } | |
4544 | else { | |
4545 | while (isSPACE(*s)) | |
4546 | s++; | |
4547 | } | |
a0d0e21e | 4548 | } |
c07a80fd | 4549 | if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) { |
3280af22 NIS |
4550 | SAVEINT(PL_multiline); |
4551 | PL_multiline = pm->op_pmflags & PMf_MULTILINE; | |
c07a80fd | 4552 | } |
4553 | ||
a0d0e21e LW |
4554 | if (!limit) |
4555 | limit = maxiters + 2; | |
4556 | if (pm->op_pmflags & PMf_WHITE) { | |
4557 | while (--limit) { | |
bbce6d69 | 4558 | m = s; |
4559 | while (m < strend && | |
4560 | !((pm->op_pmflags & PMf_LOCALE) | |
4561 | ? isSPACE_LC(*m) : isSPACE(*m))) | |
4562 | ++m; | |
a0d0e21e LW |
4563 | if (m >= strend) |
4564 | break; | |
bbce6d69 | 4565 | |
a0d0e21e LW |
4566 | dstr = NEWSV(30, m-s); |
4567 | sv_setpvn(dstr, s, m-s); | |
8ec5e241 | 4568 | if (make_mortal) |
a0d0e21e | 4569 | sv_2mortal(dstr); |
792b2c16 | 4570 | if (do_utf8) |
28cb3359 | 4571 | (void)SvUTF8_on(dstr); |
a0d0e21e | 4572 | XPUSHs(dstr); |
bbce6d69 | 4573 | |
4574 | s = m + 1; | |
4575 | while (s < strend && | |
4576 | ((pm->op_pmflags & PMf_LOCALE) | |
4577 | ? isSPACE_LC(*s) : isSPACE(*s))) | |
4578 | ++s; | |
79072805 LW |
4579 | } |
4580 | } | |
f4091fba | 4581 | else if (strEQ("^", rx->precomp)) { |
a0d0e21e LW |
4582 | while (--limit) { |
4583 | /*SUPPRESS 530*/ | |
4584 | for (m = s; m < strend && *m != '\n'; m++) ; | |
4585 | m++; | |
4586 | if (m >= strend) | |
4587 | break; | |
4588 | dstr = NEWSV(30, m-s); | |
4589 | sv_setpvn(dstr, s, m-s); | |
8ec5e241 | 4590 | if (make_mortal) |
a0d0e21e | 4591 | sv_2mortal(dstr); |
792b2c16 | 4592 | if (do_utf8) |
28cb3359 | 4593 | (void)SvUTF8_on(dstr); |
a0d0e21e LW |
4594 | XPUSHs(dstr); |
4595 | s = m; | |
4596 | } | |
4597 | } | |
699c3c34 JH |
4598 | else if (do_utf8 == ((rx->reganch & ROPT_UTF8) != 0) && |
4599 | (rx->reganch & RE_USE_INTUIT) && !rx->nparens | |
d9f97599 GS |
4600 | && (rx->reganch & ROPT_CHECK_ALL) |
4601 | && !(rx->reganch & ROPT_ANCH)) { | |
f722798b IZ |
4602 | int tail = (rx->reganch & RE_INTUIT_TAIL); |
4603 | SV *csv = CALLREG_INTUIT_STRING(aTHX_ rx); | |
cf93c79d | 4604 | |
ca5b42cb | 4605 | len = rx->minlen; |
1aa99e6b | 4606 | if (len == 1 && !(rx->reganch & ROPT_UTF8) && !tail) { |
93f04dac JH |
4607 | STRLEN n_a; |
4608 | char c = *SvPV(csv, n_a); | |
a0d0e21e | 4609 | while (--limit) { |
bbce6d69 | 4610 | /*SUPPRESS 530*/ |
f722798b | 4611 | for (m = s; m < strend && *m != c; m++) ; |
a0d0e21e LW |
4612 | if (m >= strend) |
4613 | break; | |
4614 | dstr = NEWSV(30, m-s); | |
4615 | sv_setpvn(dstr, s, m-s); | |
8ec5e241 | 4616 | if (make_mortal) |
a0d0e21e | 4617 | sv_2mortal(dstr); |
792b2c16 | 4618 | if (do_utf8) |
28cb3359 | 4619 | (void)SvUTF8_on(dstr); |
a0d0e21e | 4620 | XPUSHs(dstr); |
93f04dac JH |
4621 | /* The rx->minlen is in characters but we want to step |
4622 | * s ahead by bytes. */ | |
1aa99e6b IH |
4623 | if (do_utf8) |
4624 | s = (char*)utf8_hop((U8*)m, len); | |
4625 | else | |
4626 | s = m + len; /* Fake \n at the end */ | |
a0d0e21e LW |
4627 | } |
4628 | } | |
4629 | else { | |
4630 | #ifndef lint | |
4631 | while (s < strend && --limit && | |
f722798b IZ |
4632 | (m = fbm_instr((unsigned char*)s, (unsigned char*)strend, |
4633 | csv, PL_multiline ? FBMrf_MULTILINE : 0)) ) | |
79072805 | 4634 | #endif |
a0d0e21e LW |
4635 | { |
4636 | dstr = NEWSV(31, m-s); | |
4637 | sv_setpvn(dstr, s, m-s); | |
8ec5e241 | 4638 | if (make_mortal) |
a0d0e21e | 4639 | sv_2mortal(dstr); |
792b2c16 | 4640 | if (do_utf8) |
28cb3359 | 4641 | (void)SvUTF8_on(dstr); |
a0d0e21e | 4642 | XPUSHs(dstr); |
93f04dac JH |
4643 | /* The rx->minlen is in characters but we want to step |
4644 | * s ahead by bytes. */ | |
1aa99e6b IH |
4645 | if (do_utf8) |
4646 | s = (char*)utf8_hop((U8*)m, len); | |
4647 | else | |
4648 | s = m + len; /* Fake \n at the end */ | |
a0d0e21e | 4649 | } |
463ee0b2 | 4650 | } |
463ee0b2 | 4651 | } |
a0d0e21e | 4652 | else { |
792b2c16 | 4653 | maxiters += slen * rx->nparens; |
f722798b | 4654 | while (s < strend && --limit |
b13b2135 | 4655 | /* && (!rx->check_substr |
f722798b IZ |
4656 | || ((s = CALLREG_INTUIT_START(aTHX_ rx, sv, s, strend, |
4657 | 0, NULL)))) | |
4658 | */ && CALLREGEXEC(aTHX_ rx, s, strend, orig, | |
4659 | 1 /* minend */, sv, NULL, 0)) | |
bbce6d69 | 4660 | { |
d9f97599 | 4661 | TAINT_IF(RX_MATCH_TAINTED(rx)); |
cf93c79d | 4662 | if (RX_MATCH_COPIED(rx) && rx->subbeg != orig) { |
a0d0e21e LW |
4663 | m = s; |
4664 | s = orig; | |
cf93c79d | 4665 | orig = rx->subbeg; |
a0d0e21e LW |
4666 | s = orig + (m - s); |
4667 | strend = s + (strend - m); | |
4668 | } | |
cf93c79d | 4669 | m = rx->startp[0] + orig; |
a0d0e21e LW |
4670 | dstr = NEWSV(32, m-s); |
4671 | sv_setpvn(dstr, s, m-s); | |
8ec5e241 | 4672 | if (make_mortal) |
a0d0e21e | 4673 | sv_2mortal(dstr); |
792b2c16 | 4674 | if (do_utf8) |
28cb3359 | 4675 | (void)SvUTF8_on(dstr); |
a0d0e21e | 4676 | XPUSHs(dstr); |
d9f97599 | 4677 | if (rx->nparens) { |
eb160463 | 4678 | for (i = 1; i <= (I32)rx->nparens; i++) { |
cf93c79d IZ |
4679 | s = rx->startp[i] + orig; |
4680 | m = rx->endp[i] + orig; | |
6de67870 JP |
4681 | |
4682 | /* japhy (07/27/01) -- the (m && s) test doesn't catch | |
4683 | parens that didn't match -- they should be set to | |
4684 | undef, not the empty string */ | |
4685 | if (m >= orig && s >= orig) { | |
748a9306 LW |
4686 | dstr = NEWSV(33, m-s); |
4687 | sv_setpvn(dstr, s, m-s); | |
4688 | } | |
4689 | else | |
6de67870 | 4690 | dstr = &PL_sv_undef; /* undef, not "" */ |
8ec5e241 | 4691 | if (make_mortal) |
a0d0e21e | 4692 | sv_2mortal(dstr); |
792b2c16 | 4693 | if (do_utf8) |
28cb3359 | 4694 | (void)SvUTF8_on(dstr); |
a0d0e21e LW |
4695 | XPUSHs(dstr); |
4696 | } | |
4697 | } | |
cf93c79d | 4698 | s = rx->endp[0] + orig; |
91e09a61 | 4699 | PUTBACK; |
a0d0e21e | 4700 | } |
79072805 | 4701 | } |
8ec5e241 | 4702 | |
c07a80fd | 4703 | LEAVE_SCOPE(oldsave); |
3280af22 | 4704 | iters = (SP - PL_stack_base) - base; |
a0d0e21e | 4705 | if (iters > maxiters) |
cea2e8a9 | 4706 | DIE(aTHX_ "Split loop"); |
8ec5e241 | 4707 | |
a0d0e21e LW |
4708 | /* keep field after final delim? */ |
4709 | if (s < strend || (iters && origlimit)) { | |
93f04dac JH |
4710 | STRLEN l = strend - s; |
4711 | dstr = NEWSV(34, l); | |
4712 | sv_setpvn(dstr, s, l); | |
8ec5e241 | 4713 | if (make_mortal) |
a0d0e21e | 4714 | sv_2mortal(dstr); |
792b2c16 | 4715 | if (do_utf8) |
28cb3359 | 4716 | (void)SvUTF8_on(dstr); |
a0d0e21e LW |
4717 | XPUSHs(dstr); |
4718 | iters++; | |
79072805 | 4719 | } |
a0d0e21e | 4720 | else if (!origlimit) { |
89900bd3 SR |
4721 | while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0)) { |
4722 | if (TOPs && !make_mortal) | |
4723 | sv_2mortal(TOPs); | |
4724 | iters--; | |
4725 | SP--; | |
4726 | } | |
a0d0e21e | 4727 | } |
8ec5e241 | 4728 | |
a0d0e21e | 4729 | if (realarray) { |
8ec5e241 | 4730 | if (!mg) { |
1c0b011c | 4731 | SWITCHSTACK(ary, oldstack); |
3b0d546b | 4732 | PL_curstackinfo->si_stack = oldstack; |
1c0b011c NIS |
4733 | if (SvSMAGICAL(ary)) { |
4734 | PUTBACK; | |
4735 | mg_set((SV*)ary); | |
4736 | SPAGAIN; | |
4737 | } | |
4738 | if (gimme == G_ARRAY) { | |
4739 | EXTEND(SP, iters); | |
4740 | Copy(AvARRAY(ary), SP + 1, iters, SV*); | |
4741 | SP += iters; | |
4742 | RETURN; | |
4743 | } | |
8ec5e241 | 4744 | } |
1c0b011c | 4745 | else { |
fb73857a | 4746 | PUTBACK; |
8ec5e241 | 4747 | ENTER; |
864dbfa3 | 4748 | call_method("PUSH",G_SCALAR|G_DISCARD); |
8ec5e241 | 4749 | LEAVE; |
fb73857a | 4750 | SPAGAIN; |
8ec5e241 NIS |
4751 | if (gimme == G_ARRAY) { |
4752 | /* EXTEND should not be needed - we just popped them */ | |
4753 | EXTEND(SP, iters); | |
4754 | for (i=0; i < iters; i++) { | |
4755 | SV **svp = av_fetch(ary, i, FALSE); | |
3280af22 | 4756 | PUSHs((svp) ? *svp : &PL_sv_undef); |
8ec5e241 | 4757 | } |
1c0b011c NIS |
4758 | RETURN; |
4759 | } | |
a0d0e21e LW |
4760 | } |
4761 | } | |
4762 | else { | |
4763 | if (gimme == G_ARRAY) | |
4764 | RETURN; | |
4765 | } | |
7f18b612 YST |
4766 | |
4767 | GETTARGET; | |
4768 | PUSHi(iters); | |
4769 | RETURN; | |
79072805 | 4770 | } |
85e6fe83 | 4771 | |
c0329465 MB |
4772 | PP(pp_lock) |
4773 | { | |
39644a26 | 4774 | dSP; |
c0329465 | 4775 | dTOPss; |
e55aaa0e | 4776 | SV *retsv = sv; |
68795e93 | 4777 | SvLOCK(sv); |
e55aaa0e MB |
4778 | if (SvTYPE(retsv) == SVt_PVAV || SvTYPE(retsv) == SVt_PVHV |
4779 | || SvTYPE(retsv) == SVt_PVCV) { | |
4780 | retsv = refto(retsv); | |
4781 | } | |
4782 | SETs(retsv); | |
c0329465 MB |
4783 | RETURN; |
4784 | } | |
a863c7d1 | 4785 | |
2faa37cc | 4786 | PP(pp_threadsv) |
a863c7d1 | 4787 | { |
cea2e8a9 | 4788 | DIE(aTHX_ "tried to access per-thread data in non-threaded perl"); |
a863c7d1 | 4789 | } |