Commit | Line | Data |
---|---|---|
a0d0e21e | 1 | /* sv.c |
79072805 | 2 | * |
3818b22b | 3 | * Copyright (c) 1991-2000, Larry Wall |
79072805 LW |
4 | * |
5 | * You may distribute under the terms of either the GNU General Public | |
6 | * License or the Artistic License, as specified in the README file. | |
7 | * | |
a0d0e21e LW |
8 | */ |
9 | ||
10 | /* | |
11 | * "I wonder what the Entish is for 'yes' and 'no'," he thought. | |
79072805 LW |
12 | */ |
13 | ||
14 | #include "EXTERN.h" | |
864dbfa3 | 15 | #define PERL_IN_SV_C |
79072805 | 16 | #include "perl.h" |
79072805 | 17 | |
51371543 | 18 | #define FCALL *f |
6fc92669 | 19 | #define SV_CHECK_THINKFIRST(sv) if (SvTHINKFIRST(sv)) sv_force_normal(sv) |
2c5424a7 | 20 | |
51371543 GS |
21 | static void do_report_used(pTHXo_ SV *sv); |
22 | static void do_clean_objs(pTHXo_ SV *sv); | |
23 | #ifndef DISABLE_DESTRUCTOR_KLUDGE | |
24 | static void do_clean_named_objs(pTHXo_ SV *sv); | |
25 | #endif | |
26 | static void do_clean_all(pTHXo_ SV *sv); | |
27 | ||
4561caa4 CS |
28 | /* |
29 | * "A time to plant, and a time to uproot what was planted..." | |
30 | */ | |
31 | ||
053fc874 GS |
32 | #define plant_SV(p) \ |
33 | STMT_START { \ | |
34 | SvANY(p) = (void *)PL_sv_root; \ | |
35 | SvFLAGS(p) = SVTYPEMASK; \ | |
36 | PL_sv_root = (p); \ | |
37 | --PL_sv_count; \ | |
38 | } STMT_END | |
a0d0e21e | 39 | |
fba3b22e | 40 | /* sv_mutex must be held while calling uproot_SV() */ |
053fc874 GS |
41 | #define uproot_SV(p) \ |
42 | STMT_START { \ | |
43 | (p) = PL_sv_root; \ | |
44 | PL_sv_root = (SV*)SvANY(p); \ | |
45 | ++PL_sv_count; \ | |
46 | } STMT_END | |
47 | ||
48 | #define new_SV(p) \ | |
49 | STMT_START { \ | |
50 | LOCK_SV_MUTEX; \ | |
51 | if (PL_sv_root) \ | |
52 | uproot_SV(p); \ | |
53 | else \ | |
54 | (p) = more_sv(); \ | |
55 | UNLOCK_SV_MUTEX; \ | |
56 | SvANY(p) = 0; \ | |
57 | SvREFCNT(p) = 1; \ | |
58 | SvFLAGS(p) = 0; \ | |
59 | } STMT_END | |
463ee0b2 | 60 | |
a0d0e21e | 61 | #ifdef DEBUGGING |
4561caa4 | 62 | |
053fc874 GS |
63 | #define del_SV(p) \ |
64 | STMT_START { \ | |
65 | LOCK_SV_MUTEX; \ | |
66 | if (PL_debug & 32768) \ | |
67 | del_sv(p); \ | |
68 | else \ | |
69 | plant_SV(p); \ | |
70 | UNLOCK_SV_MUTEX; \ | |
71 | } STMT_END | |
a0d0e21e | 72 | |
76e3520e | 73 | STATIC void |
cea2e8a9 | 74 | S_del_sv(pTHX_ SV *p) |
463ee0b2 | 75 | { |
3280af22 | 76 | if (PL_debug & 32768) { |
4633a7c4 | 77 | SV* sva; |
a0d0e21e LW |
78 | SV* sv; |
79 | SV* svend; | |
80 | int ok = 0; | |
3280af22 | 81 | for (sva = PL_sv_arenaroot; sva; sva = (SV *) SvANY(sva)) { |
4633a7c4 LW |
82 | sv = sva + 1; |
83 | svend = &sva[SvREFCNT(sva)]; | |
a0d0e21e LW |
84 | if (p >= sv && p < svend) |
85 | ok = 1; | |
86 | } | |
87 | if (!ok) { | |
0453d815 PM |
88 | if (ckWARN_d(WARN_INTERNAL)) |
89 | Perl_warner(aTHX_ WARN_INTERNAL, | |
1d7c1841 GS |
90 | "Attempt to free non-arena SV: 0x%"UVxf, |
91 | PTR2UV(p)); | |
a0d0e21e LW |
92 | return; |
93 | } | |
94 | } | |
4561caa4 | 95 | plant_SV(p); |
463ee0b2 | 96 | } |
a0d0e21e | 97 | |
4561caa4 CS |
98 | #else /* ! DEBUGGING */ |
99 | ||
100 | #define del_SV(p) plant_SV(p) | |
101 | ||
102 | #endif /* DEBUGGING */ | |
463ee0b2 | 103 | |
4633a7c4 | 104 | void |
864dbfa3 | 105 | Perl_sv_add_arena(pTHX_ char *ptr, U32 size, U32 flags) |
463ee0b2 | 106 | { |
4633a7c4 | 107 | SV* sva = (SV*)ptr; |
463ee0b2 LW |
108 | register SV* sv; |
109 | register SV* svend; | |
14dd3ad8 | 110 | Zero(ptr, size, char); |
4633a7c4 LW |
111 | |
112 | /* The first SV in an arena isn't an SV. */ | |
3280af22 | 113 | SvANY(sva) = (void *) PL_sv_arenaroot; /* ptr to next arena */ |
4633a7c4 LW |
114 | SvREFCNT(sva) = size / sizeof(SV); /* number of SV slots */ |
115 | SvFLAGS(sva) = flags; /* FAKE if not to be freed */ | |
116 | ||
3280af22 NIS |
117 | PL_sv_arenaroot = sva; |
118 | PL_sv_root = sva + 1; | |
4633a7c4 LW |
119 | |
120 | svend = &sva[SvREFCNT(sva) - 1]; | |
121 | sv = sva + 1; | |
463ee0b2 | 122 | while (sv < svend) { |
a0d0e21e | 123 | SvANY(sv) = (void *)(SV*)(sv + 1); |
8990e307 | 124 | SvFLAGS(sv) = SVTYPEMASK; |
463ee0b2 LW |
125 | sv++; |
126 | } | |
127 | SvANY(sv) = 0; | |
4633a7c4 LW |
128 | SvFLAGS(sv) = SVTYPEMASK; |
129 | } | |
130 | ||
fba3b22e | 131 | /* sv_mutex must be held while calling more_sv() */ |
76e3520e | 132 | STATIC SV* |
cea2e8a9 | 133 | S_more_sv(pTHX) |
4633a7c4 | 134 | { |
4561caa4 CS |
135 | register SV* sv; |
136 | ||
3280af22 NIS |
137 | if (PL_nice_chunk) { |
138 | sv_add_arena(PL_nice_chunk, PL_nice_chunk_size, 0); | |
139 | PL_nice_chunk = Nullch; | |
c07a80fd | 140 | } |
1edc1566 | 141 | else { |
142 | char *chunk; /* must use New here to match call to */ | |
143 | New(704,chunk,1008,char); /* Safefree() in sv_free_arenas() */ | |
144 | sv_add_arena(chunk, 1008, 0); | |
145 | } | |
4561caa4 CS |
146 | uproot_SV(sv); |
147 | return sv; | |
463ee0b2 LW |
148 | } |
149 | ||
76e3520e | 150 | STATIC void |
cea2e8a9 | 151 | S_visit(pTHX_ SVFUNC_t f) |
8990e307 | 152 | { |
4633a7c4 | 153 | SV* sva; |
8990e307 LW |
154 | SV* sv; |
155 | register SV* svend; | |
156 | ||
3280af22 | 157 | for (sva = PL_sv_arenaroot; sva; sva = (SV*)SvANY(sva)) { |
4633a7c4 | 158 | svend = &sva[SvREFCNT(sva)]; |
4561caa4 CS |
159 | for (sv = sva + 1; sv < svend; ++sv) { |
160 | if (SvTYPE(sv) != SVTYPEMASK) | |
51371543 | 161 | (FCALL)(aTHXo_ sv); |
8990e307 LW |
162 | } |
163 | } | |
164 | } | |
165 | ||
166 | void | |
864dbfa3 | 167 | Perl_sv_report_used(pTHX) |
4561caa4 | 168 | { |
0b94c7bb | 169 | visit(do_report_used); |
4561caa4 CS |
170 | } |
171 | ||
4561caa4 | 172 | void |
864dbfa3 | 173 | Perl_sv_clean_objs(pTHX) |
4561caa4 | 174 | { |
3280af22 | 175 | PL_in_clean_objs = TRUE; |
0b94c7bb | 176 | visit(do_clean_objs); |
4561caa4 | 177 | #ifndef DISABLE_DESTRUCTOR_KLUDGE |
2d0f3c12 | 178 | /* some barnacles may yet remain, clinging to typeglobs */ |
0b94c7bb | 179 | visit(do_clean_named_objs); |
4561caa4 | 180 | #endif |
3280af22 | 181 | PL_in_clean_objs = FALSE; |
4561caa4 CS |
182 | } |
183 | ||
8990e307 | 184 | void |
864dbfa3 | 185 | Perl_sv_clean_all(pTHX) |
8990e307 | 186 | { |
3280af22 | 187 | PL_in_clean_all = TRUE; |
0b94c7bb | 188 | visit(do_clean_all); |
3280af22 | 189 | PL_in_clean_all = FALSE; |
8990e307 | 190 | } |
463ee0b2 | 191 | |
4633a7c4 | 192 | void |
864dbfa3 | 193 | Perl_sv_free_arenas(pTHX) |
4633a7c4 LW |
194 | { |
195 | SV* sva; | |
196 | SV* svanext; | |
612f20c3 | 197 | XPV *arena, *arenanext; |
4633a7c4 LW |
198 | |
199 | /* Free arenas here, but be careful about fake ones. (We assume | |
200 | contiguity of the fake ones with the corresponding real ones.) */ | |
201 | ||
3280af22 | 202 | for (sva = PL_sv_arenaroot; sva; sva = svanext) { |
4633a7c4 LW |
203 | svanext = (SV*) SvANY(sva); |
204 | while (svanext && SvFAKE(svanext)) | |
205 | svanext = (SV*) SvANY(svanext); | |
206 | ||
207 | if (!SvFAKE(sva)) | |
1edc1566 | 208 | Safefree((void *)sva); |
4633a7c4 | 209 | } |
5f05dabc | 210 | |
612f20c3 GS |
211 | for (arena = PL_xiv_arenaroot; arena; arena = arenanext) { |
212 | arenanext = (XPV*)arena->xpv_pv; | |
213 | Safefree(arena); | |
214 | } | |
215 | PL_xiv_arenaroot = 0; | |
216 | ||
217 | for (arena = PL_xnv_arenaroot; arena; arena = arenanext) { | |
218 | arenanext = (XPV*)arena->xpv_pv; | |
219 | Safefree(arena); | |
220 | } | |
221 | PL_xnv_arenaroot = 0; | |
222 | ||
223 | for (arena = PL_xrv_arenaroot; arena; arena = arenanext) { | |
224 | arenanext = (XPV*)arena->xpv_pv; | |
225 | Safefree(arena); | |
226 | } | |
227 | PL_xrv_arenaroot = 0; | |
228 | ||
229 | for (arena = PL_xpv_arenaroot; arena; arena = arenanext) { | |
230 | arenanext = (XPV*)arena->xpv_pv; | |
231 | Safefree(arena); | |
232 | } | |
233 | PL_xpv_arenaroot = 0; | |
234 | ||
235 | for (arena = (XPV*)PL_xpviv_arenaroot; arena; arena = arenanext) { | |
236 | arenanext = (XPV*)arena->xpv_pv; | |
237 | Safefree(arena); | |
238 | } | |
239 | PL_xpviv_arenaroot = 0; | |
240 | ||
241 | for (arena = (XPV*)PL_xpvnv_arenaroot; arena; arena = arenanext) { | |
242 | arenanext = (XPV*)arena->xpv_pv; | |
243 | Safefree(arena); | |
244 | } | |
245 | PL_xpvnv_arenaroot = 0; | |
246 | ||
247 | for (arena = (XPV*)PL_xpvcv_arenaroot; arena; arena = arenanext) { | |
248 | arenanext = (XPV*)arena->xpv_pv; | |
249 | Safefree(arena); | |
250 | } | |
251 | PL_xpvcv_arenaroot = 0; | |
252 | ||
253 | for (arena = (XPV*)PL_xpvav_arenaroot; arena; arena = arenanext) { | |
254 | arenanext = (XPV*)arena->xpv_pv; | |
255 | Safefree(arena); | |
256 | } | |
257 | PL_xpvav_arenaroot = 0; | |
258 | ||
259 | for (arena = (XPV*)PL_xpvhv_arenaroot; arena; arena = arenanext) { | |
260 | arenanext = (XPV*)arena->xpv_pv; | |
261 | Safefree(arena); | |
262 | } | |
263 | PL_xpvhv_arenaroot = 0; | |
264 | ||
265 | for (arena = (XPV*)PL_xpvmg_arenaroot; arena; arena = arenanext) { | |
266 | arenanext = (XPV*)arena->xpv_pv; | |
267 | Safefree(arena); | |
268 | } | |
269 | PL_xpvmg_arenaroot = 0; | |
270 | ||
271 | for (arena = (XPV*)PL_xpvlv_arenaroot; arena; arena = arenanext) { | |
272 | arenanext = (XPV*)arena->xpv_pv; | |
273 | Safefree(arena); | |
274 | } | |
275 | PL_xpvlv_arenaroot = 0; | |
276 | ||
277 | for (arena = (XPV*)PL_xpvbm_arenaroot; arena; arena = arenanext) { | |
278 | arenanext = (XPV*)arena->xpv_pv; | |
279 | Safefree(arena); | |
280 | } | |
281 | PL_xpvbm_arenaroot = 0; | |
282 | ||
283 | for (arena = (XPV*)PL_he_arenaroot; arena; arena = arenanext) { | |
284 | arenanext = (XPV*)arena->xpv_pv; | |
285 | Safefree(arena); | |
286 | } | |
287 | PL_he_arenaroot = 0; | |
288 | ||
3280af22 NIS |
289 | if (PL_nice_chunk) |
290 | Safefree(PL_nice_chunk); | |
291 | PL_nice_chunk = Nullch; | |
292 | PL_nice_chunk_size = 0; | |
293 | PL_sv_arenaroot = 0; | |
294 | PL_sv_root = 0; | |
4633a7c4 LW |
295 | } |
296 | ||
1d7c1841 GS |
297 | void |
298 | Perl_report_uninit(pTHX) | |
299 | { | |
300 | if (PL_op) | |
301 | Perl_warner(aTHX_ WARN_UNINITIALIZED, PL_warn_uninit, | |
302 | " in ", PL_op_desc[PL_op->op_type]); | |
303 | else | |
304 | Perl_warner(aTHX_ WARN_UNINITIALIZED, PL_warn_uninit, "", ""); | |
305 | } | |
306 | ||
76e3520e | 307 | STATIC XPVIV* |
cea2e8a9 | 308 | S_new_xiv(pTHX) |
463ee0b2 | 309 | { |
ea7c11a3 | 310 | IV* xiv; |
cbe51380 GS |
311 | LOCK_SV_MUTEX; |
312 | if (!PL_xiv_root) | |
313 | more_xiv(); | |
314 | xiv = PL_xiv_root; | |
315 | /* | |
316 | * See comment in more_xiv() -- RAM. | |
317 | */ | |
318 | PL_xiv_root = *(IV**)xiv; | |
319 | UNLOCK_SV_MUTEX; | |
320 | return (XPVIV*)((char*)xiv - STRUCT_OFFSET(XPVIV, xiv_iv)); | |
463ee0b2 LW |
321 | } |
322 | ||
76e3520e | 323 | STATIC void |
cea2e8a9 | 324 | S_del_xiv(pTHX_ XPVIV *p) |
463ee0b2 | 325 | { |
23e6a22f | 326 | IV* xiv = (IV*)((char*)(p) + STRUCT_OFFSET(XPVIV, xiv_iv)); |
cbe51380 | 327 | LOCK_SV_MUTEX; |
3280af22 NIS |
328 | *(IV**)xiv = PL_xiv_root; |
329 | PL_xiv_root = xiv; | |
cbe51380 | 330 | UNLOCK_SV_MUTEX; |
463ee0b2 LW |
331 | } |
332 | ||
cbe51380 | 333 | STATIC void |
cea2e8a9 | 334 | S_more_xiv(pTHX) |
463ee0b2 | 335 | { |
ea7c11a3 SM |
336 | register IV* xiv; |
337 | register IV* xivend; | |
8c52afec IZ |
338 | XPV* ptr; |
339 | New(705, ptr, 1008/sizeof(XPV), XPV); | |
3280af22 NIS |
340 | ptr->xpv_pv = (char*)PL_xiv_arenaroot; /* linked list of xiv arenas */ |
341 | PL_xiv_arenaroot = ptr; /* to keep Purify happy */ | |
a0d0e21e | 342 | |
ea7c11a3 SM |
343 | xiv = (IV*) ptr; |
344 | xivend = &xiv[1008 / sizeof(IV) - 1]; | |
345 | xiv += (sizeof(XPV) - 1) / sizeof(IV) + 1; /* fudge by size of XPV */ | |
3280af22 | 346 | PL_xiv_root = xiv; |
463ee0b2 | 347 | while (xiv < xivend) { |
ea7c11a3 | 348 | *(IV**)xiv = (IV *)(xiv + 1); |
463ee0b2 LW |
349 | xiv++; |
350 | } | |
ea7c11a3 | 351 | *(IV**)xiv = 0; |
463ee0b2 LW |
352 | } |
353 | ||
76e3520e | 354 | STATIC XPVNV* |
cea2e8a9 | 355 | S_new_xnv(pTHX) |
463ee0b2 | 356 | { |
65202027 | 357 | NV* xnv; |
cbe51380 GS |
358 | LOCK_SV_MUTEX; |
359 | if (!PL_xnv_root) | |
360 | more_xnv(); | |
361 | xnv = PL_xnv_root; | |
65202027 | 362 | PL_xnv_root = *(NV**)xnv; |
cbe51380 GS |
363 | UNLOCK_SV_MUTEX; |
364 | return (XPVNV*)((char*)xnv - STRUCT_OFFSET(XPVNV, xnv_nv)); | |
463ee0b2 LW |
365 | } |
366 | ||
76e3520e | 367 | STATIC void |
cea2e8a9 | 368 | S_del_xnv(pTHX_ XPVNV *p) |
463ee0b2 | 369 | { |
65202027 | 370 | NV* xnv = (NV*)((char*)(p) + STRUCT_OFFSET(XPVNV, xnv_nv)); |
cbe51380 | 371 | LOCK_SV_MUTEX; |
65202027 | 372 | *(NV**)xnv = PL_xnv_root; |
3280af22 | 373 | PL_xnv_root = xnv; |
cbe51380 | 374 | UNLOCK_SV_MUTEX; |
463ee0b2 LW |
375 | } |
376 | ||
cbe51380 | 377 | STATIC void |
cea2e8a9 | 378 | S_more_xnv(pTHX) |
463ee0b2 | 379 | { |
65202027 DS |
380 | register NV* xnv; |
381 | register NV* xnvend; | |
612f20c3 GS |
382 | XPV *ptr; |
383 | New(711, ptr, 1008/sizeof(XPV), XPV); | |
384 | ptr->xpv_pv = (char*)PL_xnv_arenaroot; | |
385 | PL_xnv_arenaroot = ptr; | |
386 | ||
387 | xnv = (NV*) ptr; | |
65202027 DS |
388 | xnvend = &xnv[1008 / sizeof(NV) - 1]; |
389 | xnv += (sizeof(XPVIV) - 1) / sizeof(NV) + 1; /* fudge by sizeof XPVIV */ | |
3280af22 | 390 | PL_xnv_root = xnv; |
463ee0b2 | 391 | while (xnv < xnvend) { |
65202027 | 392 | *(NV**)xnv = (NV*)(xnv + 1); |
463ee0b2 LW |
393 | xnv++; |
394 | } | |
65202027 | 395 | *(NV**)xnv = 0; |
463ee0b2 LW |
396 | } |
397 | ||
76e3520e | 398 | STATIC XRV* |
cea2e8a9 | 399 | S_new_xrv(pTHX) |
ed6116ce LW |
400 | { |
401 | XRV* xrv; | |
cbe51380 GS |
402 | LOCK_SV_MUTEX; |
403 | if (!PL_xrv_root) | |
404 | more_xrv(); | |
405 | xrv = PL_xrv_root; | |
406 | PL_xrv_root = (XRV*)xrv->xrv_rv; | |
407 | UNLOCK_SV_MUTEX; | |
408 | return xrv; | |
ed6116ce LW |
409 | } |
410 | ||
76e3520e | 411 | STATIC void |
cea2e8a9 | 412 | S_del_xrv(pTHX_ XRV *p) |
ed6116ce | 413 | { |
cbe51380 | 414 | LOCK_SV_MUTEX; |
3280af22 NIS |
415 | p->xrv_rv = (SV*)PL_xrv_root; |
416 | PL_xrv_root = p; | |
cbe51380 | 417 | UNLOCK_SV_MUTEX; |
ed6116ce LW |
418 | } |
419 | ||
cbe51380 | 420 | STATIC void |
cea2e8a9 | 421 | S_more_xrv(pTHX) |
ed6116ce | 422 | { |
ed6116ce LW |
423 | register XRV* xrv; |
424 | register XRV* xrvend; | |
612f20c3 GS |
425 | XPV *ptr; |
426 | New(712, ptr, 1008/sizeof(XPV), XPV); | |
427 | ptr->xpv_pv = (char*)PL_xrv_arenaroot; | |
428 | PL_xrv_arenaroot = ptr; | |
429 | ||
430 | xrv = (XRV*) ptr; | |
ed6116ce | 431 | xrvend = &xrv[1008 / sizeof(XRV) - 1]; |
612f20c3 GS |
432 | xrv += (sizeof(XPV) - 1) / sizeof(XRV) + 1; |
433 | PL_xrv_root = xrv; | |
ed6116ce LW |
434 | while (xrv < xrvend) { |
435 | xrv->xrv_rv = (SV*)(xrv + 1); | |
436 | xrv++; | |
437 | } | |
438 | xrv->xrv_rv = 0; | |
ed6116ce LW |
439 | } |
440 | ||
76e3520e | 441 | STATIC XPV* |
cea2e8a9 | 442 | S_new_xpv(pTHX) |
463ee0b2 LW |
443 | { |
444 | XPV* xpv; | |
cbe51380 GS |
445 | LOCK_SV_MUTEX; |
446 | if (!PL_xpv_root) | |
447 | more_xpv(); | |
448 | xpv = PL_xpv_root; | |
449 | PL_xpv_root = (XPV*)xpv->xpv_pv; | |
450 | UNLOCK_SV_MUTEX; | |
451 | return xpv; | |
463ee0b2 LW |
452 | } |
453 | ||
76e3520e | 454 | STATIC void |
cea2e8a9 | 455 | S_del_xpv(pTHX_ XPV *p) |
463ee0b2 | 456 | { |
cbe51380 | 457 | LOCK_SV_MUTEX; |
3280af22 NIS |
458 | p->xpv_pv = (char*)PL_xpv_root; |
459 | PL_xpv_root = p; | |
cbe51380 | 460 | UNLOCK_SV_MUTEX; |
463ee0b2 LW |
461 | } |
462 | ||
cbe51380 | 463 | STATIC void |
cea2e8a9 | 464 | S_more_xpv(pTHX) |
463ee0b2 | 465 | { |
463ee0b2 LW |
466 | register XPV* xpv; |
467 | register XPV* xpvend; | |
612f20c3 GS |
468 | New(713, xpv, 1008/sizeof(XPV), XPV); |
469 | xpv->xpv_pv = (char*)PL_xpv_arenaroot; | |
470 | PL_xpv_arenaroot = xpv; | |
471 | ||
463ee0b2 | 472 | xpvend = &xpv[1008 / sizeof(XPV) - 1]; |
612f20c3 | 473 | PL_xpv_root = ++xpv; |
463ee0b2 LW |
474 | while (xpv < xpvend) { |
475 | xpv->xpv_pv = (char*)(xpv + 1); | |
476 | xpv++; | |
477 | } | |
478 | xpv->xpv_pv = 0; | |
463ee0b2 LW |
479 | } |
480 | ||
932e9ff9 VB |
481 | STATIC XPVIV* |
482 | S_new_xpviv(pTHX) | |
483 | { | |
484 | XPVIV* xpviv; | |
485 | LOCK_SV_MUTEX; | |
486 | if (!PL_xpviv_root) | |
487 | more_xpviv(); | |
488 | xpviv = PL_xpviv_root; | |
489 | PL_xpviv_root = (XPVIV*)xpviv->xpv_pv; | |
490 | UNLOCK_SV_MUTEX; | |
491 | return xpviv; | |
492 | } | |
493 | ||
494 | STATIC void | |
495 | S_del_xpviv(pTHX_ XPVIV *p) | |
496 | { | |
497 | LOCK_SV_MUTEX; | |
498 | p->xpv_pv = (char*)PL_xpviv_root; | |
499 | PL_xpviv_root = p; | |
500 | UNLOCK_SV_MUTEX; | |
501 | } | |
502 | ||
932e9ff9 VB |
503 | STATIC void |
504 | S_more_xpviv(pTHX) | |
505 | { | |
506 | register XPVIV* xpviv; | |
507 | register XPVIV* xpvivend; | |
612f20c3 GS |
508 | New(714, xpviv, 1008/sizeof(XPVIV), XPVIV); |
509 | xpviv->xpv_pv = (char*)PL_xpviv_arenaroot; | |
510 | PL_xpviv_arenaroot = xpviv; | |
511 | ||
932e9ff9 | 512 | xpvivend = &xpviv[1008 / sizeof(XPVIV) - 1]; |
612f20c3 | 513 | PL_xpviv_root = ++xpviv; |
932e9ff9 VB |
514 | while (xpviv < xpvivend) { |
515 | xpviv->xpv_pv = (char*)(xpviv + 1); | |
516 | xpviv++; | |
517 | } | |
518 | xpviv->xpv_pv = 0; | |
519 | } | |
520 | ||
932e9ff9 VB |
521 | STATIC XPVNV* |
522 | S_new_xpvnv(pTHX) | |
523 | { | |
524 | XPVNV* xpvnv; | |
525 | LOCK_SV_MUTEX; | |
526 | if (!PL_xpvnv_root) | |
527 | more_xpvnv(); | |
528 | xpvnv = PL_xpvnv_root; | |
529 | PL_xpvnv_root = (XPVNV*)xpvnv->xpv_pv; | |
530 | UNLOCK_SV_MUTEX; | |
531 | return xpvnv; | |
532 | } | |
533 | ||
534 | STATIC void | |
535 | S_del_xpvnv(pTHX_ XPVNV *p) | |
536 | { | |
537 | LOCK_SV_MUTEX; | |
538 | p->xpv_pv = (char*)PL_xpvnv_root; | |
539 | PL_xpvnv_root = p; | |
540 | UNLOCK_SV_MUTEX; | |
541 | } | |
542 | ||
932e9ff9 VB |
543 | STATIC void |
544 | S_more_xpvnv(pTHX) | |
545 | { | |
546 | register XPVNV* xpvnv; | |
547 | register XPVNV* xpvnvend; | |
612f20c3 GS |
548 | New(715, xpvnv, 1008/sizeof(XPVNV), XPVNV); |
549 | xpvnv->xpv_pv = (char*)PL_xpvnv_arenaroot; | |
550 | PL_xpvnv_arenaroot = xpvnv; | |
551 | ||
932e9ff9 | 552 | xpvnvend = &xpvnv[1008 / sizeof(XPVNV) - 1]; |
612f20c3 | 553 | PL_xpvnv_root = ++xpvnv; |
932e9ff9 VB |
554 | while (xpvnv < xpvnvend) { |
555 | xpvnv->xpv_pv = (char*)(xpvnv + 1); | |
556 | xpvnv++; | |
557 | } | |
558 | xpvnv->xpv_pv = 0; | |
559 | } | |
560 | ||
932e9ff9 VB |
561 | STATIC XPVCV* |
562 | S_new_xpvcv(pTHX) | |
563 | { | |
564 | XPVCV* xpvcv; | |
565 | LOCK_SV_MUTEX; | |
566 | if (!PL_xpvcv_root) | |
567 | more_xpvcv(); | |
568 | xpvcv = PL_xpvcv_root; | |
569 | PL_xpvcv_root = (XPVCV*)xpvcv->xpv_pv; | |
570 | UNLOCK_SV_MUTEX; | |
571 | return xpvcv; | |
572 | } | |
573 | ||
574 | STATIC void | |
575 | S_del_xpvcv(pTHX_ XPVCV *p) | |
576 | { | |
577 | LOCK_SV_MUTEX; | |
578 | p->xpv_pv = (char*)PL_xpvcv_root; | |
579 | PL_xpvcv_root = p; | |
580 | UNLOCK_SV_MUTEX; | |
581 | } | |
582 | ||
932e9ff9 VB |
583 | STATIC void |
584 | S_more_xpvcv(pTHX) | |
585 | { | |
586 | register XPVCV* xpvcv; | |
587 | register XPVCV* xpvcvend; | |
612f20c3 GS |
588 | New(716, xpvcv, 1008/sizeof(XPVCV), XPVCV); |
589 | xpvcv->xpv_pv = (char*)PL_xpvcv_arenaroot; | |
590 | PL_xpvcv_arenaroot = xpvcv; | |
591 | ||
932e9ff9 | 592 | xpvcvend = &xpvcv[1008 / sizeof(XPVCV) - 1]; |
612f20c3 | 593 | PL_xpvcv_root = ++xpvcv; |
932e9ff9 VB |
594 | while (xpvcv < xpvcvend) { |
595 | xpvcv->xpv_pv = (char*)(xpvcv + 1); | |
596 | xpvcv++; | |
597 | } | |
598 | xpvcv->xpv_pv = 0; | |
599 | } | |
600 | ||
932e9ff9 VB |
601 | STATIC XPVAV* |
602 | S_new_xpvav(pTHX) | |
603 | { | |
604 | XPVAV* xpvav; | |
605 | LOCK_SV_MUTEX; | |
606 | if (!PL_xpvav_root) | |
607 | more_xpvav(); | |
608 | xpvav = PL_xpvav_root; | |
609 | PL_xpvav_root = (XPVAV*)xpvav->xav_array; | |
610 | UNLOCK_SV_MUTEX; | |
611 | return xpvav; | |
612 | } | |
613 | ||
614 | STATIC void | |
615 | S_del_xpvav(pTHX_ XPVAV *p) | |
616 | { | |
617 | LOCK_SV_MUTEX; | |
618 | p->xav_array = (char*)PL_xpvav_root; | |
619 | PL_xpvav_root = p; | |
620 | UNLOCK_SV_MUTEX; | |
621 | } | |
622 | ||
932e9ff9 VB |
623 | STATIC void |
624 | S_more_xpvav(pTHX) | |
625 | { | |
626 | register XPVAV* xpvav; | |
627 | register XPVAV* xpvavend; | |
612f20c3 GS |
628 | New(717, xpvav, 1008/sizeof(XPVAV), XPVAV); |
629 | xpvav->xav_array = (char*)PL_xpvav_arenaroot; | |
630 | PL_xpvav_arenaroot = xpvav; | |
631 | ||
932e9ff9 | 632 | xpvavend = &xpvav[1008 / sizeof(XPVAV) - 1]; |
612f20c3 | 633 | PL_xpvav_root = ++xpvav; |
932e9ff9 VB |
634 | while (xpvav < xpvavend) { |
635 | xpvav->xav_array = (char*)(xpvav + 1); | |
636 | xpvav++; | |
637 | } | |
638 | xpvav->xav_array = 0; | |
639 | } | |
640 | ||
932e9ff9 VB |
641 | STATIC XPVHV* |
642 | S_new_xpvhv(pTHX) | |
643 | { | |
644 | XPVHV* xpvhv; | |
645 | LOCK_SV_MUTEX; | |
646 | if (!PL_xpvhv_root) | |
647 | more_xpvhv(); | |
648 | xpvhv = PL_xpvhv_root; | |
649 | PL_xpvhv_root = (XPVHV*)xpvhv->xhv_array; | |
650 | UNLOCK_SV_MUTEX; | |
651 | return xpvhv; | |
652 | } | |
653 | ||
654 | STATIC void | |
655 | S_del_xpvhv(pTHX_ XPVHV *p) | |
656 | { | |
657 | LOCK_SV_MUTEX; | |
658 | p->xhv_array = (char*)PL_xpvhv_root; | |
659 | PL_xpvhv_root = p; | |
660 | UNLOCK_SV_MUTEX; | |
661 | } | |
662 | ||
932e9ff9 VB |
663 | STATIC void |
664 | S_more_xpvhv(pTHX) | |
665 | { | |
666 | register XPVHV* xpvhv; | |
667 | register XPVHV* xpvhvend; | |
612f20c3 GS |
668 | New(718, xpvhv, 1008/sizeof(XPVHV), XPVHV); |
669 | xpvhv->xhv_array = (char*)PL_xpvhv_arenaroot; | |
670 | PL_xpvhv_arenaroot = xpvhv; | |
671 | ||
932e9ff9 | 672 | xpvhvend = &xpvhv[1008 / sizeof(XPVHV) - 1]; |
612f20c3 | 673 | PL_xpvhv_root = ++xpvhv; |
932e9ff9 VB |
674 | while (xpvhv < xpvhvend) { |
675 | xpvhv->xhv_array = (char*)(xpvhv + 1); | |
676 | xpvhv++; | |
677 | } | |
678 | xpvhv->xhv_array = 0; | |
679 | } | |
680 | ||
932e9ff9 VB |
681 | STATIC XPVMG* |
682 | S_new_xpvmg(pTHX) | |
683 | { | |
684 | XPVMG* xpvmg; | |
685 | LOCK_SV_MUTEX; | |
686 | if (!PL_xpvmg_root) | |
687 | more_xpvmg(); | |
688 | xpvmg = PL_xpvmg_root; | |
689 | PL_xpvmg_root = (XPVMG*)xpvmg->xpv_pv; | |
690 | UNLOCK_SV_MUTEX; | |
691 | return xpvmg; | |
692 | } | |
693 | ||
694 | STATIC void | |
695 | S_del_xpvmg(pTHX_ XPVMG *p) | |
696 | { | |
697 | LOCK_SV_MUTEX; | |
698 | p->xpv_pv = (char*)PL_xpvmg_root; | |
699 | PL_xpvmg_root = p; | |
700 | UNLOCK_SV_MUTEX; | |
701 | } | |
702 | ||
932e9ff9 VB |
703 | STATIC void |
704 | S_more_xpvmg(pTHX) | |
705 | { | |
706 | register XPVMG* xpvmg; | |
707 | register XPVMG* xpvmgend; | |
612f20c3 GS |
708 | New(719, xpvmg, 1008/sizeof(XPVMG), XPVMG); |
709 | xpvmg->xpv_pv = (char*)PL_xpvmg_arenaroot; | |
710 | PL_xpvmg_arenaroot = xpvmg; | |
711 | ||
932e9ff9 | 712 | xpvmgend = &xpvmg[1008 / sizeof(XPVMG) - 1]; |
612f20c3 | 713 | PL_xpvmg_root = ++xpvmg; |
932e9ff9 VB |
714 | while (xpvmg < xpvmgend) { |
715 | xpvmg->xpv_pv = (char*)(xpvmg + 1); | |
716 | xpvmg++; | |
717 | } | |
718 | xpvmg->xpv_pv = 0; | |
719 | } | |
720 | ||
932e9ff9 VB |
721 | STATIC XPVLV* |
722 | S_new_xpvlv(pTHX) | |
723 | { | |
724 | XPVLV* xpvlv; | |
725 | LOCK_SV_MUTEX; | |
726 | if (!PL_xpvlv_root) | |
727 | more_xpvlv(); | |
728 | xpvlv = PL_xpvlv_root; | |
729 | PL_xpvlv_root = (XPVLV*)xpvlv->xpv_pv; | |
730 | UNLOCK_SV_MUTEX; | |
731 | return xpvlv; | |
732 | } | |
733 | ||
734 | STATIC void | |
735 | S_del_xpvlv(pTHX_ XPVLV *p) | |
736 | { | |
737 | LOCK_SV_MUTEX; | |
738 | p->xpv_pv = (char*)PL_xpvlv_root; | |
739 | PL_xpvlv_root = p; | |
740 | UNLOCK_SV_MUTEX; | |
741 | } | |
742 | ||
932e9ff9 VB |
743 | STATIC void |
744 | S_more_xpvlv(pTHX) | |
745 | { | |
746 | register XPVLV* xpvlv; | |
747 | register XPVLV* xpvlvend; | |
612f20c3 GS |
748 | New(720, xpvlv, 1008/sizeof(XPVLV), XPVLV); |
749 | xpvlv->xpv_pv = (char*)PL_xpvlv_arenaroot; | |
750 | PL_xpvlv_arenaroot = xpvlv; | |
751 | ||
932e9ff9 | 752 | xpvlvend = &xpvlv[1008 / sizeof(XPVLV) - 1]; |
612f20c3 | 753 | PL_xpvlv_root = ++xpvlv; |
932e9ff9 VB |
754 | while (xpvlv < xpvlvend) { |
755 | xpvlv->xpv_pv = (char*)(xpvlv + 1); | |
756 | xpvlv++; | |
757 | } | |
758 | xpvlv->xpv_pv = 0; | |
759 | } | |
760 | ||
932e9ff9 VB |
761 | STATIC XPVBM* |
762 | S_new_xpvbm(pTHX) | |
763 | { | |
764 | XPVBM* xpvbm; | |
765 | LOCK_SV_MUTEX; | |
766 | if (!PL_xpvbm_root) | |
767 | more_xpvbm(); | |
768 | xpvbm = PL_xpvbm_root; | |
769 | PL_xpvbm_root = (XPVBM*)xpvbm->xpv_pv; | |
770 | UNLOCK_SV_MUTEX; | |
771 | return xpvbm; | |
772 | } | |
773 | ||
774 | STATIC void | |
775 | S_del_xpvbm(pTHX_ XPVBM *p) | |
776 | { | |
777 | LOCK_SV_MUTEX; | |
778 | p->xpv_pv = (char*)PL_xpvbm_root; | |
779 | PL_xpvbm_root = p; | |
780 | UNLOCK_SV_MUTEX; | |
781 | } | |
782 | ||
932e9ff9 VB |
783 | STATIC void |
784 | S_more_xpvbm(pTHX) | |
785 | { | |
786 | register XPVBM* xpvbm; | |
787 | register XPVBM* xpvbmend; | |
612f20c3 GS |
788 | New(721, xpvbm, 1008/sizeof(XPVBM), XPVBM); |
789 | xpvbm->xpv_pv = (char*)PL_xpvbm_arenaroot; | |
790 | PL_xpvbm_arenaroot = xpvbm; | |
791 | ||
932e9ff9 | 792 | xpvbmend = &xpvbm[1008 / sizeof(XPVBM) - 1]; |
612f20c3 | 793 | PL_xpvbm_root = ++xpvbm; |
932e9ff9 VB |
794 | while (xpvbm < xpvbmend) { |
795 | xpvbm->xpv_pv = (char*)(xpvbm + 1); | |
796 | xpvbm++; | |
797 | } | |
798 | xpvbm->xpv_pv = 0; | |
799 | } | |
800 | ||
d33b2eba GS |
801 | #ifdef LEAKTEST |
802 | # define my_safemalloc(s) (void*)safexmalloc(717,s) | |
803 | # define my_safefree(p) safexfree((char*)p) | |
804 | #else | |
805 | # define my_safemalloc(s) (void*)safemalloc(s) | |
806 | # define my_safefree(p) safefree((char*)p) | |
807 | #endif | |
463ee0b2 | 808 | |
d33b2eba | 809 | #ifdef PURIFY |
463ee0b2 | 810 | |
d33b2eba GS |
811 | #define new_XIV() my_safemalloc(sizeof(XPVIV)) |
812 | #define del_XIV(p) my_safefree(p) | |
ed6116ce | 813 | |
d33b2eba GS |
814 | #define new_XNV() my_safemalloc(sizeof(XPVNV)) |
815 | #define del_XNV(p) my_safefree(p) | |
463ee0b2 | 816 | |
d33b2eba GS |
817 | #define new_XRV() my_safemalloc(sizeof(XRV)) |
818 | #define del_XRV(p) my_safefree(p) | |
8c52afec | 819 | |
d33b2eba GS |
820 | #define new_XPV() my_safemalloc(sizeof(XPV)) |
821 | #define del_XPV(p) my_safefree(p) | |
9b94d1dd | 822 | |
d33b2eba GS |
823 | #define new_XPVIV() my_safemalloc(sizeof(XPVIV)) |
824 | #define del_XPVIV(p) my_safefree(p) | |
932e9ff9 | 825 | |
d33b2eba GS |
826 | #define new_XPVNV() my_safemalloc(sizeof(XPVNV)) |
827 | #define del_XPVNV(p) my_safefree(p) | |
932e9ff9 | 828 | |
d33b2eba GS |
829 | #define new_XPVCV() my_safemalloc(sizeof(XPVCV)) |
830 | #define del_XPVCV(p) my_safefree(p) | |
932e9ff9 | 831 | |
d33b2eba GS |
832 | #define new_XPVAV() my_safemalloc(sizeof(XPVAV)) |
833 | #define del_XPVAV(p) my_safefree(p) | |
834 | ||
835 | #define new_XPVHV() my_safemalloc(sizeof(XPVHV)) | |
836 | #define del_XPVHV(p) my_safefree(p) | |
1c846c1f | 837 | |
d33b2eba GS |
838 | #define new_XPVMG() my_safemalloc(sizeof(XPVMG)) |
839 | #define del_XPVMG(p) my_safefree(p) | |
840 | ||
841 | #define new_XPVLV() my_safemalloc(sizeof(XPVLV)) | |
842 | #define del_XPVLV(p) my_safefree(p) | |
843 | ||
844 | #define new_XPVBM() my_safemalloc(sizeof(XPVBM)) | |
845 | #define del_XPVBM(p) my_safefree(p) | |
846 | ||
847 | #else /* !PURIFY */ | |
848 | ||
849 | #define new_XIV() (void*)new_xiv() | |
850 | #define del_XIV(p) del_xiv((XPVIV*) p) | |
851 | ||
852 | #define new_XNV() (void*)new_xnv() | |
853 | #define del_XNV(p) del_xnv((XPVNV*) p) | |
9b94d1dd | 854 | |
d33b2eba GS |
855 | #define new_XRV() (void*)new_xrv() |
856 | #define del_XRV(p) del_xrv((XRV*) p) | |
9b94d1dd | 857 | |
d33b2eba GS |
858 | #define new_XPV() (void*)new_xpv() |
859 | #define del_XPV(p) del_xpv((XPV *)p) | |
860 | ||
861 | #define new_XPVIV() (void*)new_xpviv() | |
862 | #define del_XPVIV(p) del_xpviv((XPVIV *)p) | |
863 | ||
864 | #define new_XPVNV() (void*)new_xpvnv() | |
865 | #define del_XPVNV(p) del_xpvnv((XPVNV *)p) | |
866 | ||
867 | #define new_XPVCV() (void*)new_xpvcv() | |
868 | #define del_XPVCV(p) del_xpvcv((XPVCV *)p) | |
869 | ||
870 | #define new_XPVAV() (void*)new_xpvav() | |
871 | #define del_XPVAV(p) del_xpvav((XPVAV *)p) | |
872 | ||
873 | #define new_XPVHV() (void*)new_xpvhv() | |
874 | #define del_XPVHV(p) del_xpvhv((XPVHV *)p) | |
1c846c1f | 875 | |
d33b2eba GS |
876 | #define new_XPVMG() (void*)new_xpvmg() |
877 | #define del_XPVMG(p) del_xpvmg((XPVMG *)p) | |
878 | ||
879 | #define new_XPVLV() (void*)new_xpvlv() | |
880 | #define del_XPVLV(p) del_xpvlv((XPVLV *)p) | |
881 | ||
882 | #define new_XPVBM() (void*)new_xpvbm() | |
883 | #define del_XPVBM(p) del_xpvbm((XPVBM *)p) | |
884 | ||
885 | #endif /* PURIFY */ | |
9b94d1dd | 886 | |
d33b2eba GS |
887 | #define new_XPVGV() my_safemalloc(sizeof(XPVGV)) |
888 | #define del_XPVGV(p) my_safefree(p) | |
1c846c1f | 889 | |
d33b2eba GS |
890 | #define new_XPVFM() my_safemalloc(sizeof(XPVFM)) |
891 | #define del_XPVFM(p) my_safefree(p) | |
1c846c1f | 892 | |
d33b2eba GS |
893 | #define new_XPVIO() my_safemalloc(sizeof(XPVIO)) |
894 | #define del_XPVIO(p) my_safefree(p) | |
8990e307 | 895 | |
954c1994 GS |
896 | /* |
897 | =for apidoc sv_upgrade | |
898 | ||
899 | Upgrade an SV to a more complex form. Use C<SvUPGRADE>. See | |
900 | C<svtype>. | |
901 | ||
902 | =cut | |
903 | */ | |
904 | ||
79072805 | 905 | bool |
864dbfa3 | 906 | Perl_sv_upgrade(pTHX_ register SV *sv, U32 mt) |
79072805 LW |
907 | { |
908 | char* pv; | |
909 | U32 cur; | |
910 | U32 len; | |
a0d0e21e | 911 | IV iv; |
65202027 | 912 | NV nv; |
79072805 LW |
913 | MAGIC* magic; |
914 | HV* stash; | |
915 | ||
f130fd45 NIS |
916 | if (mt != SVt_PV && SvREADONLY(sv) && SvFAKE(sv)) { |
917 | sv_force_normal(sv); | |
918 | } | |
919 | ||
79072805 LW |
920 | if (SvTYPE(sv) == mt) |
921 | return TRUE; | |
922 | ||
a5f75d66 AD |
923 | if (mt < SVt_PVIV) |
924 | (void)SvOOK_off(sv); | |
925 | ||
79072805 LW |
926 | switch (SvTYPE(sv)) { |
927 | case SVt_NULL: | |
928 | pv = 0; | |
929 | cur = 0; | |
930 | len = 0; | |
931 | iv = 0; | |
932 | nv = 0.0; | |
933 | magic = 0; | |
934 | stash = 0; | |
935 | break; | |
79072805 LW |
936 | case SVt_IV: |
937 | pv = 0; | |
938 | cur = 0; | |
939 | len = 0; | |
463ee0b2 | 940 | iv = SvIVX(sv); |
65202027 | 941 | nv = (NV)SvIVX(sv); |
79072805 LW |
942 | del_XIV(SvANY(sv)); |
943 | magic = 0; | |
944 | stash = 0; | |
ed6116ce | 945 | if (mt == SVt_NV) |
463ee0b2 | 946 | mt = SVt_PVNV; |
ed6116ce LW |
947 | else if (mt < SVt_PVIV) |
948 | mt = SVt_PVIV; | |
79072805 LW |
949 | break; |
950 | case SVt_NV: | |
951 | pv = 0; | |
952 | cur = 0; | |
953 | len = 0; | |
463ee0b2 | 954 | nv = SvNVX(sv); |
1bd302c3 | 955 | iv = I_V(nv); |
79072805 LW |
956 | magic = 0; |
957 | stash = 0; | |
958 | del_XNV(SvANY(sv)); | |
959 | SvANY(sv) = 0; | |
ed6116ce | 960 | if (mt < SVt_PVNV) |
79072805 LW |
961 | mt = SVt_PVNV; |
962 | break; | |
ed6116ce LW |
963 | case SVt_RV: |
964 | pv = (char*)SvRV(sv); | |
965 | cur = 0; | |
966 | len = 0; | |
56431972 RB |
967 | iv = PTR2IV(pv); |
968 | nv = PTR2NV(pv); | |
ed6116ce LW |
969 | del_XRV(SvANY(sv)); |
970 | magic = 0; | |
971 | stash = 0; | |
972 | break; | |
79072805 | 973 | case SVt_PV: |
463ee0b2 | 974 | pv = SvPVX(sv); |
79072805 LW |
975 | cur = SvCUR(sv); |
976 | len = SvLEN(sv); | |
977 | iv = 0; | |
978 | nv = 0.0; | |
979 | magic = 0; | |
980 | stash = 0; | |
981 | del_XPV(SvANY(sv)); | |
748a9306 LW |
982 | if (mt <= SVt_IV) |
983 | mt = SVt_PVIV; | |
984 | else if (mt == SVt_NV) | |
985 | mt = SVt_PVNV; | |
79072805 LW |
986 | break; |
987 | case SVt_PVIV: | |
463ee0b2 | 988 | pv = SvPVX(sv); |
79072805 LW |
989 | cur = SvCUR(sv); |
990 | len = SvLEN(sv); | |
463ee0b2 | 991 | iv = SvIVX(sv); |
79072805 LW |
992 | nv = 0.0; |
993 | magic = 0; | |
994 | stash = 0; | |
995 | del_XPVIV(SvANY(sv)); | |
996 | break; | |
997 | case SVt_PVNV: | |
463ee0b2 | 998 | pv = SvPVX(sv); |
79072805 LW |
999 | cur = SvCUR(sv); |
1000 | len = SvLEN(sv); | |
463ee0b2 LW |
1001 | iv = SvIVX(sv); |
1002 | nv = SvNVX(sv); | |
79072805 LW |
1003 | magic = 0; |
1004 | stash = 0; | |
1005 | del_XPVNV(SvANY(sv)); | |
1006 | break; | |
1007 | case SVt_PVMG: | |
463ee0b2 | 1008 | pv = SvPVX(sv); |
79072805 LW |
1009 | cur = SvCUR(sv); |
1010 | len = SvLEN(sv); | |
463ee0b2 LW |
1011 | iv = SvIVX(sv); |
1012 | nv = SvNVX(sv); | |
79072805 LW |
1013 | magic = SvMAGIC(sv); |
1014 | stash = SvSTASH(sv); | |
1015 | del_XPVMG(SvANY(sv)); | |
1016 | break; | |
1017 | default: | |
cea2e8a9 | 1018 | Perl_croak(aTHX_ "Can't upgrade that kind of scalar"); |
79072805 LW |
1019 | } |
1020 | ||
1021 | switch (mt) { | |
1022 | case SVt_NULL: | |
cea2e8a9 | 1023 | Perl_croak(aTHX_ "Can't upgrade to undef"); |
79072805 LW |
1024 | case SVt_IV: |
1025 | SvANY(sv) = new_XIV(); | |
463ee0b2 | 1026 | SvIVX(sv) = iv; |
79072805 LW |
1027 | break; |
1028 | case SVt_NV: | |
1029 | SvANY(sv) = new_XNV(); | |
463ee0b2 | 1030 | SvNVX(sv) = nv; |
79072805 | 1031 | break; |
ed6116ce LW |
1032 | case SVt_RV: |
1033 | SvANY(sv) = new_XRV(); | |
1034 | SvRV(sv) = (SV*)pv; | |
ed6116ce | 1035 | break; |
79072805 LW |
1036 | case SVt_PV: |
1037 | SvANY(sv) = new_XPV(); | |
463ee0b2 | 1038 | SvPVX(sv) = pv; |
79072805 LW |
1039 | SvCUR(sv) = cur; |
1040 | SvLEN(sv) = len; | |
1041 | break; | |
1042 | case SVt_PVIV: | |
1043 | SvANY(sv) = new_XPVIV(); | |
463ee0b2 | 1044 | SvPVX(sv) = pv; |
79072805 LW |
1045 | SvCUR(sv) = cur; |
1046 | SvLEN(sv) = len; | |
463ee0b2 | 1047 | SvIVX(sv) = iv; |
79072805 | 1048 | if (SvNIOK(sv)) |
a0d0e21e | 1049 | (void)SvIOK_on(sv); |
79072805 LW |
1050 | SvNOK_off(sv); |
1051 | break; | |
1052 | case SVt_PVNV: | |
1053 | SvANY(sv) = new_XPVNV(); | |
463ee0b2 | 1054 | SvPVX(sv) = pv; |
79072805 LW |
1055 | SvCUR(sv) = cur; |
1056 | SvLEN(sv) = len; | |
463ee0b2 LW |
1057 | SvIVX(sv) = iv; |
1058 | SvNVX(sv) = nv; | |
79072805 LW |
1059 | break; |
1060 | case SVt_PVMG: | |
1061 | SvANY(sv) = new_XPVMG(); | |
463ee0b2 | 1062 | SvPVX(sv) = pv; |
79072805 LW |
1063 | SvCUR(sv) = cur; |
1064 | SvLEN(sv) = len; | |
463ee0b2 LW |
1065 | SvIVX(sv) = iv; |
1066 | SvNVX(sv) = nv; | |
79072805 LW |
1067 | SvMAGIC(sv) = magic; |
1068 | SvSTASH(sv) = stash; | |
1069 | break; | |
1070 | case SVt_PVLV: | |
1071 | SvANY(sv) = new_XPVLV(); | |
463ee0b2 | 1072 | SvPVX(sv) = pv; |
79072805 LW |
1073 | SvCUR(sv) = cur; |
1074 | SvLEN(sv) = len; | |
463ee0b2 LW |
1075 | SvIVX(sv) = iv; |
1076 | SvNVX(sv) = nv; | |
79072805 LW |
1077 | SvMAGIC(sv) = magic; |
1078 | SvSTASH(sv) = stash; | |
1079 | LvTARGOFF(sv) = 0; | |
1080 | LvTARGLEN(sv) = 0; | |
1081 | LvTARG(sv) = 0; | |
1082 | LvTYPE(sv) = 0; | |
1083 | break; | |
1084 | case SVt_PVAV: | |
1085 | SvANY(sv) = new_XPVAV(); | |
463ee0b2 LW |
1086 | if (pv) |
1087 | Safefree(pv); | |
2304df62 | 1088 | SvPVX(sv) = 0; |
d1bf51dd | 1089 | AvMAX(sv) = -1; |
93965878 | 1090 | AvFILLp(sv) = -1; |
463ee0b2 LW |
1091 | SvIVX(sv) = 0; |
1092 | SvNVX(sv) = 0.0; | |
1093 | SvMAGIC(sv) = magic; | |
1094 | SvSTASH(sv) = stash; | |
1095 | AvALLOC(sv) = 0; | |
79072805 LW |
1096 | AvARYLEN(sv) = 0; |
1097 | AvFLAGS(sv) = 0; | |
1098 | break; | |
1099 | case SVt_PVHV: | |
1100 | SvANY(sv) = new_XPVHV(); | |
463ee0b2 LW |
1101 | if (pv) |
1102 | Safefree(pv); | |
1103 | SvPVX(sv) = 0; | |
1104 | HvFILL(sv) = 0; | |
1105 | HvMAX(sv) = 0; | |
1106 | HvKEYS(sv) = 0; | |
1107 | SvNVX(sv) = 0.0; | |
79072805 LW |
1108 | SvMAGIC(sv) = magic; |
1109 | SvSTASH(sv) = stash; | |
79072805 LW |
1110 | HvRITER(sv) = 0; |
1111 | HvEITER(sv) = 0; | |
1112 | HvPMROOT(sv) = 0; | |
1113 | HvNAME(sv) = 0; | |
79072805 LW |
1114 | break; |
1115 | case SVt_PVCV: | |
1116 | SvANY(sv) = new_XPVCV(); | |
748a9306 | 1117 | Zero(SvANY(sv), 1, XPVCV); |
463ee0b2 | 1118 | SvPVX(sv) = pv; |
79072805 LW |
1119 | SvCUR(sv) = cur; |
1120 | SvLEN(sv) = len; | |
463ee0b2 LW |
1121 | SvIVX(sv) = iv; |
1122 | SvNVX(sv) = nv; | |
79072805 LW |
1123 | SvMAGIC(sv) = magic; |
1124 | SvSTASH(sv) = stash; | |
79072805 LW |
1125 | break; |
1126 | case SVt_PVGV: | |
1127 | SvANY(sv) = new_XPVGV(); | |
463ee0b2 | 1128 | SvPVX(sv) = pv; |
79072805 LW |
1129 | SvCUR(sv) = cur; |
1130 | SvLEN(sv) = len; | |
463ee0b2 LW |
1131 | SvIVX(sv) = iv; |
1132 | SvNVX(sv) = nv; | |
79072805 LW |
1133 | SvMAGIC(sv) = magic; |
1134 | SvSTASH(sv) = stash; | |
93a17b20 | 1135 | GvGP(sv) = 0; |
79072805 LW |
1136 | GvNAME(sv) = 0; |
1137 | GvNAMELEN(sv) = 0; | |
1138 | GvSTASH(sv) = 0; | |
a5f75d66 | 1139 | GvFLAGS(sv) = 0; |
79072805 LW |
1140 | break; |
1141 | case SVt_PVBM: | |
1142 | SvANY(sv) = new_XPVBM(); | |
463ee0b2 | 1143 | SvPVX(sv) = pv; |
79072805 LW |
1144 | SvCUR(sv) = cur; |
1145 | SvLEN(sv) = len; | |
463ee0b2 LW |
1146 | SvIVX(sv) = iv; |
1147 | SvNVX(sv) = nv; | |
79072805 LW |
1148 | SvMAGIC(sv) = magic; |
1149 | SvSTASH(sv) = stash; | |
1150 | BmRARE(sv) = 0; | |
1151 | BmUSEFUL(sv) = 0; | |
1152 | BmPREVIOUS(sv) = 0; | |
1153 | break; | |
1154 | case SVt_PVFM: | |
1155 | SvANY(sv) = new_XPVFM(); | |
748a9306 | 1156 | Zero(SvANY(sv), 1, XPVFM); |
463ee0b2 | 1157 | SvPVX(sv) = pv; |
79072805 LW |
1158 | SvCUR(sv) = cur; |
1159 | SvLEN(sv) = len; | |
463ee0b2 LW |
1160 | SvIVX(sv) = iv; |
1161 | SvNVX(sv) = nv; | |
79072805 LW |
1162 | SvMAGIC(sv) = magic; |
1163 | SvSTASH(sv) = stash; | |
79072805 | 1164 | break; |
8990e307 LW |
1165 | case SVt_PVIO: |
1166 | SvANY(sv) = new_XPVIO(); | |
748a9306 | 1167 | Zero(SvANY(sv), 1, XPVIO); |
8990e307 LW |
1168 | SvPVX(sv) = pv; |
1169 | SvCUR(sv) = cur; | |
1170 | SvLEN(sv) = len; | |
1171 | SvIVX(sv) = iv; | |
1172 | SvNVX(sv) = nv; | |
1173 | SvMAGIC(sv) = magic; | |
1174 | SvSTASH(sv) = stash; | |
85e6fe83 | 1175 | IoPAGE_LEN(sv) = 60; |
8990e307 LW |
1176 | break; |
1177 | } | |
1178 | SvFLAGS(sv) &= ~SVTYPEMASK; | |
1179 | SvFLAGS(sv) |= mt; | |
79072805 LW |
1180 | return TRUE; |
1181 | } | |
1182 | ||
79072805 | 1183 | int |
864dbfa3 | 1184 | Perl_sv_backoff(pTHX_ register SV *sv) |
79072805 LW |
1185 | { |
1186 | assert(SvOOK(sv)); | |
463ee0b2 LW |
1187 | if (SvIVX(sv)) { |
1188 | char *s = SvPVX(sv); | |
1189 | SvLEN(sv) += SvIVX(sv); | |
1190 | SvPVX(sv) -= SvIVX(sv); | |
79072805 | 1191 | SvIV_set(sv, 0); |
463ee0b2 | 1192 | Move(s, SvPVX(sv), SvCUR(sv)+1, char); |
79072805 LW |
1193 | } |
1194 | SvFLAGS(sv) &= ~SVf_OOK; | |
a0d0e21e | 1195 | return 0; |
79072805 LW |
1196 | } |
1197 | ||
954c1994 GS |
1198 | /* |
1199 | =for apidoc sv_grow | |
1200 | ||
1201 | Expands the character buffer in the SV. This will use C<sv_unref> and will | |
1202 | upgrade the SV to C<SVt_PV>. Returns a pointer to the character buffer. | |
1203 | Use C<SvGROW>. | |
1204 | ||
1205 | =cut | |
1206 | */ | |
1207 | ||
79072805 | 1208 | char * |
864dbfa3 | 1209 | Perl_sv_grow(pTHX_ register SV *sv, register STRLEN newlen) |
79072805 LW |
1210 | { |
1211 | register char *s; | |
1212 | ||
55497cff | 1213 | #ifdef HAS_64K_LIMIT |
79072805 | 1214 | if (newlen >= 0x10000) { |
1d7c1841 GS |
1215 | PerlIO_printf(Perl_debug_log, |
1216 | "Allocation too large: %"UVxf"\n", (UV)newlen); | |
79072805 LW |
1217 | my_exit(1); |
1218 | } | |
55497cff | 1219 | #endif /* HAS_64K_LIMIT */ |
a0d0e21e LW |
1220 | if (SvROK(sv)) |
1221 | sv_unref(sv); | |
79072805 LW |
1222 | if (SvTYPE(sv) < SVt_PV) { |
1223 | sv_upgrade(sv, SVt_PV); | |
463ee0b2 | 1224 | s = SvPVX(sv); |
79072805 LW |
1225 | } |
1226 | else if (SvOOK(sv)) { /* pv is offset? */ | |
1227 | sv_backoff(sv); | |
463ee0b2 | 1228 | s = SvPVX(sv); |
79072805 LW |
1229 | if (newlen > SvLEN(sv)) |
1230 | newlen += 10 * (newlen - SvCUR(sv)); /* avoid copy each time */ | |
c6f8c383 GA |
1231 | #ifdef HAS_64K_LIMIT |
1232 | if (newlen >= 0x10000) | |
1233 | newlen = 0xFFFF; | |
1234 | #endif | |
79072805 LW |
1235 | } |
1236 | else | |
463ee0b2 | 1237 | s = SvPVX(sv); |
79072805 | 1238 | if (newlen > SvLEN(sv)) { /* need more room? */ |
8d6dde3e | 1239 | if (SvLEN(sv) && s) { |
f5a32c7f | 1240 | #if defined(MYMALLOC) && !defined(LEAKTEST) |
8d6dde3e IZ |
1241 | STRLEN l = malloced_size((void*)SvPVX(sv)); |
1242 | if (newlen <= l) { | |
1243 | SvLEN_set(sv, l); | |
1244 | return s; | |
1245 | } else | |
c70c8a0a | 1246 | #endif |
79072805 | 1247 | Renew(s,newlen,char); |
8d6dde3e | 1248 | } |
79072805 LW |
1249 | else |
1250 | New(703,s,newlen,char); | |
1251 | SvPV_set(sv, s); | |
1252 | SvLEN_set(sv, newlen); | |
1253 | } | |
1254 | return s; | |
1255 | } | |
1256 | ||
954c1994 GS |
1257 | /* |
1258 | =for apidoc sv_setiv | |
1259 | ||
1260 | Copies an integer into the given SV. Does not handle 'set' magic. See | |
1261 | C<sv_setiv_mg>. | |
1262 | ||
1263 | =cut | |
1264 | */ | |
1265 | ||
79072805 | 1266 | void |
864dbfa3 | 1267 | Perl_sv_setiv(pTHX_ register SV *sv, IV i) |
79072805 | 1268 | { |
2213622d | 1269 | SV_CHECK_THINKFIRST(sv); |
463ee0b2 LW |
1270 | switch (SvTYPE(sv)) { |
1271 | case SVt_NULL: | |
79072805 | 1272 | sv_upgrade(sv, SVt_IV); |
463ee0b2 LW |
1273 | break; |
1274 | case SVt_NV: | |
1275 | sv_upgrade(sv, SVt_PVNV); | |
1276 | break; | |
ed6116ce | 1277 | case SVt_RV: |
463ee0b2 | 1278 | case SVt_PV: |
79072805 | 1279 | sv_upgrade(sv, SVt_PVIV); |
463ee0b2 | 1280 | break; |
a0d0e21e LW |
1281 | |
1282 | case SVt_PVGV: | |
a0d0e21e LW |
1283 | case SVt_PVAV: |
1284 | case SVt_PVHV: | |
1285 | case SVt_PVCV: | |
1286 | case SVt_PVFM: | |
1287 | case SVt_PVIO: | |
411caa50 JH |
1288 | Perl_croak(aTHX_ "Can't coerce %s to integer in %s", sv_reftype(sv,0), |
1289 | PL_op_desc[PL_op->op_type]); | |
463ee0b2 | 1290 | } |
a0d0e21e | 1291 | (void)SvIOK_only(sv); /* validate number */ |
a5f75d66 | 1292 | SvIVX(sv) = i; |
463ee0b2 | 1293 | SvTAINT(sv); |
79072805 LW |
1294 | } |
1295 | ||
954c1994 GS |
1296 | /* |
1297 | =for apidoc sv_setiv_mg | |
1298 | ||
1299 | Like C<sv_setiv>, but also handles 'set' magic. | |
1300 | ||
1301 | =cut | |
1302 | */ | |
1303 | ||
79072805 | 1304 | void |
864dbfa3 | 1305 | Perl_sv_setiv_mg(pTHX_ register SV *sv, IV i) |
ef50df4b GS |
1306 | { |
1307 | sv_setiv(sv,i); | |
1308 | SvSETMAGIC(sv); | |
1309 | } | |
1310 | ||
954c1994 GS |
1311 | /* |
1312 | =for apidoc sv_setuv | |
1313 | ||
1314 | Copies an unsigned integer into the given SV. Does not handle 'set' magic. | |
1315 | See C<sv_setuv_mg>. | |
1316 | ||
1317 | =cut | |
1318 | */ | |
1319 | ||
ef50df4b | 1320 | void |
864dbfa3 | 1321 | Perl_sv_setuv(pTHX_ register SV *sv, UV u) |
55497cff | 1322 | { |
16b7a9a4 NC |
1323 | if (u <= (UV)IV_MAX) { |
1324 | sv_setiv(sv, (IV)u); | |
1325 | return; | |
1326 | } | |
25da4f38 IZ |
1327 | sv_setiv(sv, 0); |
1328 | SvIsUV_on(sv); | |
1329 | SvUVX(sv) = u; | |
55497cff | 1330 | } |
1331 | ||
954c1994 GS |
1332 | /* |
1333 | =for apidoc sv_setuv_mg | |
1334 | ||
1335 | Like C<sv_setuv>, but also handles 'set' magic. | |
1336 | ||
1337 | =cut | |
1338 | */ | |
1339 | ||
55497cff | 1340 | void |
864dbfa3 | 1341 | Perl_sv_setuv_mg(pTHX_ register SV *sv, UV u) |
ef50df4b | 1342 | { |
16b7a9a4 NC |
1343 | if (u <= (UV)IV_MAX) { |
1344 | sv_setiv(sv, (IV)u); | |
1345 | } else { | |
1346 | sv_setiv(sv, 0); | |
1347 | SvIsUV_on(sv); | |
1348 | sv_setuv(sv,u); | |
1349 | } | |
ef50df4b GS |
1350 | SvSETMAGIC(sv); |
1351 | } | |
1352 | ||
954c1994 GS |
1353 | /* |
1354 | =for apidoc sv_setnv | |
1355 | ||
1356 | Copies a double into the given SV. Does not handle 'set' magic. See | |
1357 | C<sv_setnv_mg>. | |
1358 | ||
1359 | =cut | |
1360 | */ | |
1361 | ||
ef50df4b | 1362 | void |
65202027 | 1363 | Perl_sv_setnv(pTHX_ register SV *sv, NV num) |
79072805 | 1364 | { |
2213622d | 1365 | SV_CHECK_THINKFIRST(sv); |
a0d0e21e LW |
1366 | switch (SvTYPE(sv)) { |
1367 | case SVt_NULL: | |
1368 | case SVt_IV: | |
79072805 | 1369 | sv_upgrade(sv, SVt_NV); |
a0d0e21e | 1370 | break; |
a0d0e21e LW |
1371 | case SVt_RV: |
1372 | case SVt_PV: | |
1373 | case SVt_PVIV: | |
79072805 | 1374 | sv_upgrade(sv, SVt_PVNV); |
a0d0e21e | 1375 | break; |
827b7e14 | 1376 | |
a0d0e21e | 1377 | case SVt_PVGV: |
a0d0e21e LW |
1378 | case SVt_PVAV: |
1379 | case SVt_PVHV: | |
1380 | case SVt_PVCV: | |
1381 | case SVt_PVFM: | |
1382 | case SVt_PVIO: | |
411caa50 JH |
1383 | Perl_croak(aTHX_ "Can't coerce %s to number in %s", sv_reftype(sv,0), |
1384 | PL_op_name[PL_op->op_type]); | |
79072805 | 1385 | } |
463ee0b2 | 1386 | SvNVX(sv) = num; |
a0d0e21e | 1387 | (void)SvNOK_only(sv); /* validate number */ |
463ee0b2 | 1388 | SvTAINT(sv); |
79072805 LW |
1389 | } |
1390 | ||
954c1994 GS |
1391 | /* |
1392 | =for apidoc sv_setnv_mg | |
1393 | ||
1394 | Like C<sv_setnv>, but also handles 'set' magic. | |
1395 | ||
1396 | =cut | |
1397 | */ | |
1398 | ||
ef50df4b | 1399 | void |
65202027 | 1400 | Perl_sv_setnv_mg(pTHX_ register SV *sv, NV num) |
ef50df4b GS |
1401 | { |
1402 | sv_setnv(sv,num); | |
1403 | SvSETMAGIC(sv); | |
1404 | } | |
1405 | ||
76e3520e | 1406 | STATIC void |
cea2e8a9 | 1407 | S_not_a_number(pTHX_ SV *sv) |
a0d0e21e LW |
1408 | { |
1409 | char tmpbuf[64]; | |
1410 | char *d = tmpbuf; | |
1411 | char *s; | |
dc28f22b GA |
1412 | char *limit = tmpbuf + sizeof(tmpbuf) - 8; |
1413 | /* each *s can expand to 4 chars + "...\0", | |
1414 | i.e. need room for 8 chars */ | |
a0d0e21e | 1415 | |
dc28f22b | 1416 | for (s = SvPVX(sv); *s && d < limit; s++) { |
bbce6d69 | 1417 | int ch = *s & 0xFF; |
1418 | if (ch & 128 && !isPRINT_LC(ch)) { | |
a0d0e21e LW |
1419 | *d++ = 'M'; |
1420 | *d++ = '-'; | |
1421 | ch &= 127; | |
1422 | } | |
bbce6d69 | 1423 | if (ch == '\n') { |
1424 | *d++ = '\\'; | |
1425 | *d++ = 'n'; | |
1426 | } | |
1427 | else if (ch == '\r') { | |
1428 | *d++ = '\\'; | |
1429 | *d++ = 'r'; | |
1430 | } | |
1431 | else if (ch == '\f') { | |
1432 | *d++ = '\\'; | |
1433 | *d++ = 'f'; | |
1434 | } | |
1435 | else if (ch == '\\') { | |
1436 | *d++ = '\\'; | |
1437 | *d++ = '\\'; | |
1438 | } | |
1439 | else if (isPRINT_LC(ch)) | |
a0d0e21e LW |
1440 | *d++ = ch; |
1441 | else { | |
1442 | *d++ = '^'; | |
bbce6d69 | 1443 | *d++ = toCTRL(ch); |
a0d0e21e LW |
1444 | } |
1445 | } | |
1446 | if (*s) { | |
1447 | *d++ = '.'; | |
1448 | *d++ = '.'; | |
1449 | *d++ = '.'; | |
1450 | } | |
1451 | *d = '\0'; | |
1452 | ||
533c011a | 1453 | if (PL_op) |
42d38218 MS |
1454 | Perl_warner(aTHX_ WARN_NUMERIC, |
1455 | "Argument \"%s\" isn't numeric in %s", tmpbuf, | |
1456 | PL_op_desc[PL_op->op_type]); | |
a0d0e21e | 1457 | else |
42d38218 MS |
1458 | Perl_warner(aTHX_ WARN_NUMERIC, |
1459 | "Argument \"%s\" isn't numeric", tmpbuf); | |
a0d0e21e LW |
1460 | } |
1461 | ||
16b7a9a4 NC |
1462 | /* the number can be converted to integer with atol() or atoll() although */ |
1463 | #define IS_NUMBER_TO_INT_BY_ATOL 0x01 /* integer (may have decimals) */ | |
1464 | #define IS_NUMBER_TO_INT_BY_STRTOL 0x02 /* it may exceed IV_MAX */ | |
1465 | #define IS_NUMBER_TO_INT_BY_ATOF 0x04 /* seen something like 123e4 */ | |
1466 | #define IS_NUMBER_LONGER_THAN_IV_MAX 0x08 /* more digits than IV_MAX */ | |
1467 | #define IS_NUMBER_AS_LONG_AS_IV_MAX 0x10 /* may(be not) larger than IV_MAX */ | |
1468 | #define IS_NUMBER_NOT_INT 0x20 /* seen a decimal point or e */ | |
1469 | #define IS_NUMBER_NEG 0x40 /* seen a leading - */ | |
1470 | #define IS_NUMBER_INFINITY 0x80 /* /^\s*-?Infinity\s*$/i */ | |
25da4f38 IZ |
1471 | |
1472 | /* Actually, ISO C leaves conversion of UV to IV undefined, but | |
1473 | until proven guilty, assume that things are not that bad... */ | |
1474 | ||
16b7a9a4 NC |
1475 | /* As 64 bit platforms often have an NV that doesn't preserve all bits of |
1476 | an IV (an assumption perl has been based on to date) it becomes necessary | |
1477 | to remove the assumption that the NV always carries enough precision to | |
1478 | recreate the IV whenever needed, and that the NV is the canonical form. | |
1479 | Instead, IV/UV and NV need to be given equal rights. So as to not lose | |
1480 | precision as an side effect of conversion (which would lead to insanity | |
1481 | and the dragon(s) in t/op/numconvert.t getting very angry) the intent is | |
1482 | 1) to distinguish between IV/UV/NV slots that have cached a valid | |
1483 | conversion where precision was lost and IV/UV/NV slots that have a | |
1484 | valid conversion which has lost no precision | |
1485 | 2) to ensure that if a numeric conversion to one form is request that | |
1486 | would lose precision, the precise conversion (or differently | |
1487 | imprecise conversion) is also performed and cached, to prevent | |
1488 | requests for different numeric formats on the same SV causing | |
1489 | lossy conversion chains. (lossless conversion chains are perfectly | |
1490 | acceptable (still)) | |
1491 | ||
1492 | ||
1493 | flags are used: | |
1494 | SvIOKp is true if the IV slot contains a valid value | |
1495 | SvIOK is true only if the IV value is accurate (UV if SvIOK_UV true) | |
1496 | SvNOKp is true if the NV slot contains a valid value | |
1497 | SvNOK is true only if the NV value is accurate | |
1498 | ||
1499 | so | |
1500 | while converting from PV to NV check to see if converting that NV to an | |
1501 | IV(or UV) would lose accuracy over a direct conversion from PV to | |
1502 | IV(or UV). If it would, cache both conversions, return NV, but mark | |
1503 | SV as IOK NOKp (ie not NOK). | |
1504 | ||
1505 | while converting from PV to IV check to see if converting that IV to an | |
1506 | NV would lose accuracy over a direct conversion from PV to NV. If it | |
1507 | would, cache both conversions, flag similarly. | |
1508 | ||
1509 | Before, the SV value "3.2" could become NV=3.2 IV=3 NOK, IOK quite | |
1510 | correctly because if IV & NV were set NV *always* overruled. | |
1511 | Now, "3.2" will become NV=3.2 IV=3 NOK, IOKp, because the flags meaning | |
1512 | changes - now IV and NV together means that the two are interchangeable | |
1513 | SvIVX == (IV) SvNVX && SvNVX == (NV) SvIVX; | |
1514 | ||
1515 | The benefit of this is operations such as pp_add know that if SvIOK is | |
1516 | true for both left and right operands, then integer addition can be | |
1517 | used instead of floating point. (for cases where the result won't | |
1518 | overflow) Before, floating point was always used, which could lead to | |
1519 | loss of precision compared with integer addition. | |
1520 | ||
1521 | * making IV and NV equal status should make maths accurate on 64 bit | |
1522 | platforms | |
1523 | * may speed up maths somewhat if pp_add and friends start to use | |
1524 | integers when possible instead of fp. (hopefully the overhead in | |
1525 | looking for SvIOK and checking for overflow will not outweigh the | |
1526 | fp to integer speedup) | |
1527 | * will slow down integer operations (callers of SvIV) on "inaccurate" | |
1528 | values, as the change from SvIOK to SvIOKp will cause a call into | |
1529 | sv_2iv each time rather than a macro access direct to the IV slot | |
1530 | * should speed up number->string conversion on integers as IV is | |
1531 | favoured when IV and NV equally accurate | |
1532 | ||
1533 | #################################################################### | |
1534 | You had better be using SvIOK_notUV if you want an IV for arithmetic | |
1535 | SvIOK is true if (IV or UV), so you might be getting (IV)SvUV | |
1536 | SvUOK is true iff UV. | |
1537 | #################################################################### | |
1538 | ||
1539 | Your mileage will vary depending your CPUs relative fp to integer | |
1540 | performance ratio. | |
1541 | */ | |
1542 | ||
1543 | #ifndef NV_PRESERVES_UV | |
1544 | #define IS_NUMBER_UNDERFLOW_IV 1 | |
1545 | #define IS_NUMBER_UNDERFLOW_UV 2 | |
1546 | #define IS_NUMBER_IV_AND_UV 2 | |
1547 | #define IS_NUMBER_OVERFLOW_IV 4 | |
1548 | #define IS_NUMBER_OVERFLOW_UV 5 | |
1549 | /* Hopefully your optimiser will consider inlining these two functions. */ | |
1550 | STATIC int | |
1551 | S_sv_2inuv_non_preserve (pTHX_ register SV *sv, I32 numtype) { | |
1552 | NV nv = SvNVX(sv); /* Code simpler and had compiler problems if */ | |
1553 | UV nv_as_uv = U_V(nv); /* these are not in simple variables. */ | |
1554 | DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_2inuv_non '%s', IV=0x%"UVxf" NV=%g inttype=%X\n", SvPVX(sv), SvIVX(sv), nv, numtype)); | |
1555 | if (nv_as_uv <= (UV)IV_MAX) { | |
1556 | (void)SvIOKp_on(sv); | |
1557 | (void)SvNOKp_on(sv); | |
1558 | /* Within suitable range to fit in an IV, atol won't overflow */ | |
1559 | /* XXX quite sure? Is that your final answer? not really, I'm | |
1560 | trusting that nv_as_uv to round down if NV is (IV_MAX + 1) */ | |
1561 | SvIVX(sv) = (IV)Atol(SvPVX(sv)); | |
1562 | if (numtype & IS_NUMBER_NOT_INT) { | |
1563 | /* I believe that even if the original PV had decimals, they | |
1564 | are lost beyond the limit of the FP precision. | |
1565 | However, neither is canonical, so both only get p flags. | |
1566 | NWC, 2000/11/25 */ | |
1567 | /* Both already have p flags, so do nothing */ | |
1568 | } else if (SvIVX(sv) == I_V(nv)) { | |
1569 | SvNOK_on(sv); | |
1570 | SvIOK_on(sv); | |
1571 | } else { | |
1572 | SvIOK_on(sv); | |
1573 | /* It had no "." so it must be integer. assert (get in here from | |
1574 | sv_2iv and sv_2uv only for ndef HAS_STRTOL and | |
1575 | IS_NUMBER_AS_LONG_AS_IV_MAX) or my logic is faulty and all | |
1576 | conversion routines need audit. */ | |
1577 | } | |
1578 | return nv < 0 ? IS_NUMBER_UNDERFLOW_UV : IS_NUMBER_IV_AND_UV; | |
1579 | } | |
1580 | /* between IV_MAX and NV(UV_MAX). Could be slightly> UV_MAX */ | |
1581 | (void)SvIOKp_on(sv); | |
1582 | (void)SvNOKp_on(sv); | |
1583 | #ifdef HAS_STRTOUL | |
1584 | { | |
1585 | int save_errno = errno; | |
1586 | errno = 0; | |
1587 | SvUVX(sv) = Strtoul(SvPVX(sv), Null(char**), 10); | |
1588 | if (errno == 0) { | |
1589 | if (numtype & IS_NUMBER_NOT_INT) { | |
1590 | /* UV and NV both imprecise. */ | |
1591 | SvIsUV_on(sv); | |
1592 | } else if (SvUVX(sv) == nv_as_uv && SvUVX(sv) != UV_MAX) { | |
1593 | SvNOK_on(sv); | |
1594 | SvIOK_on(sv); | |
1595 | SvIsUV_on(sv); | |
1596 | } else { | |
1597 | SvIOK_on(sv); | |
1598 | SvIsUV_on(sv); | |
1599 | } | |
1600 | errno = save_errno; | |
1601 | return IS_NUMBER_OVERFLOW_IV; | |
1602 | } | |
1603 | errno = save_errno; | |
1604 | SvNOK_on(sv); | |
1605 | /* Must have just overflowed UV, but not enough that an NV could spot | |
1606 | this.. */ | |
1607 | return IS_NUMBER_OVERFLOW_UV; | |
1608 | } | |
1609 | #else | |
1610 | /* We've just lost integer precision, nothing we could do. */ | |
1611 | SvUVX(sv) = nv_as_uv; | |
1612 | DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_2niuv_non UV? '%s', UV=0x%"UVxf" NV=%g U_V(NV)=0x%"UVxf" inttype=%X\n", SvPVX(sv), SvIVX(sv), nv, nv_as_uv, numtype)); | |
1613 | /* UV and NV slots equally valid only if we have casting symmetry. */ | |
1614 | if (numtype & IS_NUMBER_NOT_INT) { | |
1615 | SvIsUV_on(sv); | |
1616 | } else if (SvUVX(sv) == nv_as_uv && SvUVX(sv) != UV_MAX) { | |
1617 | /* UV_MAX can cast up to NV (UV_MAX+1), that NV casts down to UV_MAX | |
1618 | UV_MAX ought to be 0xFF...FFF which won't preserve (We only | |
1619 | get to this point if NVs don't preserve UVs) */ | |
1620 | SvNOK_on(sv); | |
1621 | SvIOK_on(sv); | |
1622 | SvIsUV_on(sv); | |
1623 | } else { | |
1624 | /* As above, I believe UV at least as good as NV */ | |
1625 | SvIsUV_on(sv); | |
1626 | } | |
1627 | #endif /* HAS_STRTOUL */ | |
1628 | return IS_NUMBER_OVERFLOW_IV; | |
1629 | } | |
1630 | ||
1631 | /* For sv_2nv these three cases are "SvNOK and don't bother casting" */ | |
1632 | STATIC int | |
1633 | S_sv_2iuv_non_preserve (pTHX_ register SV *sv, I32 numtype) | |
1634 | { | |
1635 | DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_2iuv_non '%s', IV=0x%"UVxf" NV=%g inttype=%X\n", SvPVX(sv), SvIVX(sv), SvNVX(sv), numtype)); | |
1636 | if (SvNVX(sv) < (NV)IV_MIN) { | |
1637 | (void)SvIOKp_on(sv); | |
1638 | (void)SvNOK_on(sv); | |
1639 | SvIVX(sv) = IV_MIN; | |
1640 | return IS_NUMBER_UNDERFLOW_IV; | |
1641 | } | |
1642 | if (SvNVX(sv) > (NV)UV_MAX) { | |
1643 | (void)SvIOKp_on(sv); | |
1644 | (void)SvNOK_on(sv); | |
1645 | SvIsUV_on(sv); | |
1646 | SvUVX(sv) = UV_MAX; | |
1647 | return IS_NUMBER_OVERFLOW_UV; | |
1648 | } | |
1649 | if (!(numtype & (IS_NUMBER_TO_INT_BY_ATOL | IS_NUMBER_TO_INT_BY_STRTOL))) { | |
1650 | (void)SvIOKp_on(sv); | |
1651 | (void)SvNOK_on(sv); | |
1652 | /* Can't use strtol etc to convert this string */ | |
1653 | if (SvNVX(sv) <= (UV)IV_MAX) { | |
1654 | SvIVX(sv) = I_V(SvNVX(sv)); | |
1655 | if ((NV)(SvIVX(sv)) == SvNVX(sv)) { | |
1656 | SvIOK_on(sv); /* Integer is precise. NOK, IOK */ | |
1657 | } else { | |
1658 | /* Integer is imprecise. NOK, IOKp */ | |
1659 | } | |
1660 | return SvNVX(sv) < 0 ? IS_NUMBER_UNDERFLOW_UV : IS_NUMBER_IV_AND_UV; | |
1661 | } | |
1662 | SvIsUV_on(sv); | |
1663 | SvUVX(sv) = U_V(SvNVX(sv)); | |
1664 | if ((NV)(SvUVX(sv)) == SvNVX(sv)) { | |
1665 | SvIOK_on(sv); /* Integer is precise. NOK, UOK */ | |
1666 | } else { | |
1667 | /* Integer is imprecise. NOK, IOKp */ | |
1668 | } | |
1669 | return IS_NUMBER_OVERFLOW_IV; | |
1670 | } | |
1671 | return S_sv_2inuv_non_preserve (sv, numtype); | |
1672 | } | |
1673 | #endif /* NV_PRESERVES_UV*/ | |
1674 | ||
1675 | ||
a0d0e21e | 1676 | IV |
864dbfa3 | 1677 | Perl_sv_2iv(pTHX_ register SV *sv) |
79072805 LW |
1678 | { |
1679 | if (!sv) | |
1680 | return 0; | |
8990e307 | 1681 | if (SvGMAGICAL(sv)) { |
463ee0b2 LW |
1682 | mg_get(sv); |
1683 | if (SvIOKp(sv)) | |
1684 | return SvIVX(sv); | |
748a9306 | 1685 | if (SvNOKp(sv)) { |
25da4f38 | 1686 | return I_V(SvNVX(sv)); |
748a9306 | 1687 | } |
36477c24 | 1688 | if (SvPOKp(sv) && SvLEN(sv)) |
1689 | return asIV(sv); | |
3fe9a6f1 | 1690 | if (!SvROK(sv)) { |
d008e5eb | 1691 | if (!(SvFLAGS(sv) & SVs_PADTMP)) { |
d008e5eb | 1692 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing) |
1d7c1841 | 1693 | report_uninit(); |
c6ee37c5 | 1694 | } |
36477c24 | 1695 | return 0; |
3fe9a6f1 | 1696 | } |
463ee0b2 | 1697 | } |
ed6116ce | 1698 | if (SvTHINKFIRST(sv)) { |
a0d0e21e | 1699 | if (SvROK(sv)) { |
a0d0e21e | 1700 | SV* tmpstr; |
1554e226 DC |
1701 | if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) && |
1702 | (SvRV(tmpstr) != SvRV(sv))) | |
9e7bc3e8 | 1703 | return SvIV(tmpstr); |
56431972 | 1704 | return PTR2IV(SvRV(sv)); |
a0d0e21e | 1705 | } |
47deb5e7 NIS |
1706 | if (SvREADONLY(sv) && SvFAKE(sv)) { |
1707 | sv_force_normal(sv); | |
1708 | } | |
0336b60e | 1709 | if (SvREADONLY(sv) && !SvOK(sv)) { |
0336b60e | 1710 | if (ckWARN(WARN_UNINITIALIZED)) |
1d7c1841 | 1711 | report_uninit(); |
ed6116ce LW |
1712 | return 0; |
1713 | } | |
79072805 | 1714 | } |
25da4f38 IZ |
1715 | if (SvIOKp(sv)) { |
1716 | if (SvIsUV(sv)) { | |
1717 | return (IV)(SvUVX(sv)); | |
1718 | } | |
1719 | else { | |
1720 | return SvIVX(sv); | |
1721 | } | |
463ee0b2 | 1722 | } |
748a9306 | 1723 | if (SvNOKp(sv)) { |
16b7a9a4 NC |
1724 | /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv |
1725 | * without also getting a cached IV/UV from it at the same time | |
1726 | * (ie PV->NV conversion should detect loss of accuracy and cache | |
1727 | * IV or UV at same time to avoid this. NWC */ | |
25da4f38 IZ |
1728 | |
1729 | if (SvTYPE(sv) == SVt_NV) | |
1730 | sv_upgrade(sv, SVt_PVNV); | |
1731 | ||
16b7a9a4 NC |
1732 | (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */ |
1733 | /* < not <= as for NV doesn't preserve UV, ((NV)IV_MAX+1) will almost | |
1734 | certainly cast into the IV range at IV_MAX, whereas the correct | |
1735 | answer is the UV IV_MAX +1. Hence < ensures that dodgy boundary | |
1736 | cases go to UV */ | |
1737 | if (SvNVX(sv) < (NV)IV_MAX + 0.5) { | |
748a9306 | 1738 | SvIVX(sv) = I_V(SvNVX(sv)); |
16b7a9a4 NC |
1739 | if (SvNVX(sv) == (NV) SvIVX(sv) |
1740 | #ifndef NV_PRESERVES_UV | |
1741 | && (((UV)1 << NV_PRESERVES_UV_BITS) > | |
1742 | (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv))) | |
1743 | /* Don't flag it as "accurately an integer" if the number | |
1744 | came from a (by definition imprecise) NV operation, and | |
1745 | we're outside the range of NV integer precision */ | |
1746 | #endif | |
1747 | ) { | |
1748 | SvIOK_on(sv); /* Can this go wrong with rounding? NWC */ | |
1749 | DEBUG_c(PerlIO_printf(Perl_debug_log, | |
1750 | "0x%"UVxf" iv(%g => %"IVdf") (precise)\n", | |
1751 | PTR2UV(sv), | |
1752 | SvNVX(sv), | |
1753 | SvIVX(sv))); | |
1754 | ||
1755 | } else { | |
1756 | /* IV not precise. No need to convert from PV, as NV | |
1757 | conversion would already have cached IV if it detected | |
1758 | that PV->IV would be better than PV->NV->IV | |
1759 | flags already correct - don't set public IOK. */ | |
1760 | DEBUG_c(PerlIO_printf(Perl_debug_log, | |
1761 | "0x%"UVxf" iv(%g => %"IVdf") (imprecise)\n", | |
1762 | PTR2UV(sv), | |
1763 | SvNVX(sv), | |
1764 | SvIVX(sv))); | |
1765 | } | |
1766 | /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN, | |
1767 | but the cast (NV)IV_MIN rounds to a the value less (more | |
1768 | negative) than IV_MIN which happens to be equal to SvNVX ?? | |
1769 | Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and | |
1770 | NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and | |
1771 | (NV)UVX == NVX are both true, but the values differ. :-( | |
1772 | Hopefully for 2s complement IV_MIN is something like | |
1773 | 0x8000000000000000 which will be exact. NWC */ | |
1774 | } | |
25da4f38 | 1775 | else { |
ff68c719 | 1776 | SvUVX(sv) = U_V(SvNVX(sv)); |
16b7a9a4 NC |
1777 | if ( |
1778 | (SvNVX(sv) == (NV) SvUVX(sv)) | |
1779 | #ifndef NV_PRESERVES_UV | |
1780 | /* Make sure it's not 0xFFFFFFFFFFFFFFFF */ | |
1781 | /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */ | |
1782 | && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv)) | |
1783 | /* Don't flag it as "accurately an integer" if the number | |
1784 | came from a (by definition imprecise) NV operation, and | |
1785 | we're outside the range of NV integer precision */ | |
1786 | #endif | |
1787 | ) | |
1788 | SvIOK_on(sv); | |
25da4f38 IZ |
1789 | SvIsUV_on(sv); |
1790 | ret_iv_max: | |
1c846c1f | 1791 | DEBUG_c(PerlIO_printf(Perl_debug_log, |
57def98f | 1792 | "0x%"UVxf" 2iv(%"UVuf" => %"IVdf") (as unsigned)\n", |
56431972 | 1793 | PTR2UV(sv), |
57def98f JH |
1794 | SvUVX(sv), |
1795 | SvUVX(sv))); | |
25da4f38 IZ |
1796 | return (IV)SvUVX(sv); |
1797 | } | |
748a9306 LW |
1798 | } |
1799 | else if (SvPOKp(sv) && SvLEN(sv)) { | |
25da4f38 IZ |
1800 | I32 numtype = looks_like_number(sv); |
1801 | ||
1802 | /* We want to avoid a possible problem when we cache an IV which | |
1803 | may be later translated to an NV, and the resulting NV is not | |
1804 | the translation of the initial data. | |
1c846c1f | 1805 | |
25da4f38 IZ |
1806 | This means that if we cache such an IV, we need to cache the |
1807 | NV as well. Moreover, we trade speed for space, and do not | |
16b7a9a4 | 1808 | cache the NV if we are sure it's not needed. |
25da4f38 | 1809 | */ |
25da4f38 | 1810 | |
16b7a9a4 NC |
1811 | if ((numtype & ~IS_NUMBER_NEG) == IS_NUMBER_TO_INT_BY_ATOL) { |
1812 | /* The NV may be reconstructed from IV - safe to cache IV, | |
1813 | which may be calculated by atol(). */ | |
1814 | if (SvTYPE(sv) < SVt_PVIV) | |
1815 | sv_upgrade(sv, SVt_PVIV); | |
25da4f38 | 1816 | (void)SvIOK_on(sv); |
16b7a9a4 NC |
1817 | SvIVX(sv) = Atol(SvPVX(sv)); |
1818 | } else { | |
1819 | #ifdef HAS_STRTOL | |
1820 | IV i; | |
1821 | int save_errno = errno; | |
1822 | /* Is it an integer that we could convert with strtol? | |
1823 | So try it, and if it doesn't set errno then it's pukka. | |
1824 | This should be faster than going atof and then thinking. */ | |
1825 | if (((numtype & (IS_NUMBER_TO_INT_BY_STRTOL | IS_NUMBER_NOT_INT)) | |
1826 | == IS_NUMBER_TO_INT_BY_STRTOL) | |
1827 | /* && is a sequence point. Without it not sure if I'm trying | |
1828 | to do too much between sequence points and hence going | |
1829 | undefined */ | |
1830 | && ((errno = 0), 1) /* , 1 so always true */ | |
1831 | && ((i = Strtol(SvPVX(sv), Null(char**), 10)), 1) | |
1832 | && (errno == 0)) { | |
1833 | if (SvTYPE(sv) < SVt_PVIV) | |
1834 | sv_upgrade(sv, SVt_PVIV); | |
1835 | (void)SvIOK_on(sv); | |
1836 | SvIVX(sv) = i; | |
1837 | errno = save_errno; | |
1838 | } else { | |
1839 | NV d; | |
1840 | /* Hopefully trace flow will optimise this away where possible | |
1841 | */ | |
1842 | errno = save_errno; | |
1843 | #else | |
1844 | NV d; | |
1845 | #endif | |
1846 | /* It wasn't an integer, or it overflowed, or we don't have | |
1847 | strtol. Do things the slow way - check if it's a UV etc. */ | |
1848 | d = Atof(SvPVX(sv)); | |
1849 | ||
1850 | if (SvTYPE(sv) < SVt_PVNV) | |
1851 | sv_upgrade(sv, SVt_PVNV); | |
1852 | SvNVX(sv) = d; | |
1853 | ||
1854 | if (! numtype && ckWARN(WARN_NUMERIC)) | |
1855 | not_a_number(sv); | |
1856 | ||
65202027 | 1857 | #if defined(USE_LONG_DOUBLE) |
16b7a9a4 NC |
1858 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%" PERL_PRIgldbl ")\n", |
1859 | PTR2UV(sv), SvNVX(sv))); | |
65202027 | 1860 | #else |
16b7a9a4 NC |
1861 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%g)\n", |
1862 | PTR2UV(sv), SvNVX(sv))); | |
65202027 | 1863 | #endif |
16b7a9a4 NC |
1864 | |
1865 | ||
1866 | #ifdef NV_PRESERVES_UV | |
1867 | (void)SvIOKp_on(sv); | |
1868 | (void)SvNOK_on(sv); | |
1869 | if (SvNVX(sv) < (NV)IV_MAX + 0.5) { | |
1870 | SvIVX(sv) = I_V(SvNVX(sv)); | |
1871 | if ((NV)(SvIVX(sv)) == SvNVX(sv)) { | |
1872 | SvIOK_on(sv); | |
1873 | } else { | |
1874 | /* Integer is imprecise. NOK, IOKp */ | |
1875 | } | |
1876 | /* UV will not work better than IV */ | |
1877 | } else { | |
1878 | if (SvNVX(sv) > (NV)UV_MAX) { | |
1879 | SvIsUV_on(sv); | |
1880 | /* Integer is inaccurate. NOK, IOKp, is UV */ | |
1881 | SvUVX(sv) = UV_MAX; | |
1882 | SvIsUV_on(sv); | |
1883 | } else { | |
1884 | SvUVX(sv) = U_V(SvNVX(sv)); | |
1885 | /* 0xFFFFFFFFFFFFFFFF not an issue in here */ | |
1886 | if ((NV)(SvUVX(sv)) == SvNVX(sv)) { | |
1887 | SvIOK_on(sv); | |
1888 | SvIsUV_on(sv); | |
1889 | } else { | |
1890 | /* Integer is imprecise. NOK, IOKp, is UV */ | |
1891 | SvIsUV_on(sv); | |
1892 | } | |
1893 | } | |
1894 | goto ret_iv_max; | |
1895 | } | |
1896 | #else /* NV_PRESERVES_UV */ | |
1897 | if (((UV)1 << NV_PRESERVES_UV_BITS) > | |
1898 | U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) { | |
1899 | /* Small enough to preserve all bits. */ | |
1900 | (void)SvIOKp_on(sv); | |
1901 | SvNOK_on(sv); | |
1902 | SvIVX(sv) = I_V(SvNVX(sv)); | |
1903 | if ((NV)(SvIVX(sv)) == SvNVX(sv)) | |
1904 | SvIOK_on(sv); | |
1905 | /* Assumption: first non-preserved integer is < IV_MAX, | |
1906 | this NV is in the preserved range, therefore: */ | |
1907 | if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv)) | |
1908 | < (UV)IV_MAX)) { | |
1909 | Perl_croak(aTHX_ "sv_2iv assumed (U_V(fabs(SvNVX(sv))) < (UV)IV_MAX) but SvNVX(sv)=%g U_V is 0x%"UVxf", IV_MAX is 0x%"UVxf"\n", SvNVX(sv), U_V(SvNVX(sv)), (UV)IV_MAX); | |
1910 | } | |
1911 | } else if (sv_2iuv_non_preserve (sv, numtype) | |
1912 | >= IS_NUMBER_OVERFLOW_IV) | |
1913 | goto ret_iv_max; | |
1914 | #endif /* NV_PRESERVES_UV */ | |
25da4f38 IZ |
1915 | } |
1916 | } | |
16b7a9a4 | 1917 | } else { |
599cee73 | 1918 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP)) |
1d7c1841 | 1919 | report_uninit(); |
25da4f38 IZ |
1920 | if (SvTYPE(sv) < SVt_IV) |
1921 | /* Typically the caller expects that sv_any is not NULL now. */ | |
1922 | sv_upgrade(sv, SVt_IV); | |
a0d0e21e | 1923 | return 0; |
79072805 | 1924 | } |
1d7c1841 GS |
1925 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"IVdf")\n", |
1926 | PTR2UV(sv),SvIVX(sv))); | |
25da4f38 | 1927 | return SvIsUV(sv) ? (IV)SvUVX(sv) : SvIVX(sv); |
79072805 LW |
1928 | } |
1929 | ||
ff68c719 | 1930 | UV |
864dbfa3 | 1931 | Perl_sv_2uv(pTHX_ register SV *sv) |
ff68c719 | 1932 | { |
1933 | if (!sv) | |
1934 | return 0; | |
1935 | if (SvGMAGICAL(sv)) { | |
1936 | mg_get(sv); | |
1937 | if (SvIOKp(sv)) | |
1938 | return SvUVX(sv); | |
1939 | if (SvNOKp(sv)) | |
1940 | return U_V(SvNVX(sv)); | |
36477c24 | 1941 | if (SvPOKp(sv) && SvLEN(sv)) |
1942 | return asUV(sv); | |
3fe9a6f1 | 1943 | if (!SvROK(sv)) { |
d008e5eb | 1944 | if (!(SvFLAGS(sv) & SVs_PADTMP)) { |
d008e5eb | 1945 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing) |
1d7c1841 | 1946 | report_uninit(); |
c6ee37c5 | 1947 | } |
36477c24 | 1948 | return 0; |
3fe9a6f1 | 1949 | } |
ff68c719 | 1950 | } |
1951 | if (SvTHINKFIRST(sv)) { | |
1952 | if (SvROK(sv)) { | |
ff68c719 | 1953 | SV* tmpstr; |
1554e226 DC |
1954 | if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) && |
1955 | (SvRV(tmpstr) != SvRV(sv))) | |
9e7bc3e8 | 1956 | return SvUV(tmpstr); |
56431972 | 1957 | return PTR2UV(SvRV(sv)); |
ff68c719 | 1958 | } |
8a818333 NIS |
1959 | if (SvREADONLY(sv) && SvFAKE(sv)) { |
1960 | sv_force_normal(sv); | |
1961 | } | |
0336b60e | 1962 | if (SvREADONLY(sv) && !SvOK(sv)) { |
0336b60e | 1963 | if (ckWARN(WARN_UNINITIALIZED)) |
1d7c1841 | 1964 | report_uninit(); |
ff68c719 | 1965 | return 0; |
1966 | } | |
1967 | } | |
25da4f38 IZ |
1968 | if (SvIOKp(sv)) { |
1969 | if (SvIsUV(sv)) { | |
1970 | return SvUVX(sv); | |
1971 | } | |
1972 | else { | |
1973 | return (UV)SvIVX(sv); | |
1974 | } | |
ff68c719 | 1975 | } |
1976 | if (SvNOKp(sv)) { | |
16b7a9a4 NC |
1977 | /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv |
1978 | * without also getting a cached IV/UV from it at the same time | |
1979 | * (ie PV->NV conversion should detect loss of accuracy and cache | |
1980 | * IV or UV at same time to avoid this. */ | |
1981 | /* IV-over-UV optimisation - choose to cache IV if possible */ | |
1982 | ||
25da4f38 IZ |
1983 | if (SvTYPE(sv) == SVt_NV) |
1984 | sv_upgrade(sv, SVt_PVNV); | |
16b7a9a4 NC |
1985 | |
1986 | (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */ | |
1987 | if (SvNVX(sv) < (NV)IV_MAX + 0.5) { | |
25da4f38 | 1988 | SvIVX(sv) = I_V(SvNVX(sv)); |
16b7a9a4 NC |
1989 | if (SvNVX(sv) == (NV) SvIVX(sv) |
1990 | #ifndef NV_PRESERVES_UV | |
1991 | && (((UV)1 << NV_PRESERVES_UV_BITS) > | |
1992 | (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv))) | |
1993 | /* Don't flag it as "accurately an integer" if the number | |
1994 | came from a (by definition imprecise) NV operation, and | |
1995 | we're outside the range of NV integer precision */ | |
1996 | #endif | |
1997 | ) { | |
1998 | SvIOK_on(sv); /* Can this go wrong with rounding? NWC */ | |
1999 | DEBUG_c(PerlIO_printf(Perl_debug_log, | |
2000 | "0x%"UVxf" uv(%g => %"IVdf") (precise)\n", | |
2001 | PTR2UV(sv), | |
2002 | SvNVX(sv), | |
2003 | SvIVX(sv))); | |
2004 | ||
2005 | } else { | |
2006 | /* IV not precise. No need to convert from PV, as NV | |
2007 | conversion would already have cached IV if it detected | |
2008 | that PV->IV would be better than PV->NV->IV | |
2009 | flags already correct - don't set public IOK. */ | |
2010 | DEBUG_c(PerlIO_printf(Perl_debug_log, | |
2011 | "0x%"UVxf" uv(%g => %"IVdf") (imprecise)\n", | |
2012 | PTR2UV(sv), | |
2013 | SvNVX(sv), | |
2014 | SvIVX(sv))); | |
2015 | } | |
2016 | /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN, | |
2017 | but the cast (NV)IV_MIN rounds to a the value less (more | |
2018 | negative) than IV_MIN which happens to be equal to SvNVX ?? | |
2019 | Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and | |
2020 | NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and | |
2021 | (NV)UVX == NVX are both true, but the values differ. :-( | |
2022 | Hopefully for 2s complement IV_MIN is something like | |
2023 | 0x8000000000000000 which will be exact. NWC */ | |
2024 | } | |
2025 | else { | |
2026 | SvUVX(sv) = U_V(SvNVX(sv)); | |
2027 | if ( | |
2028 | (SvNVX(sv) == (NV) SvUVX(sv)) | |
2029 | #ifndef NV_PRESERVES_UV | |
2030 | /* Make sure it's not 0xFFFFFFFFFFFFFFFF */ | |
2031 | /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */ | |
2032 | && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv)) | |
2033 | /* Don't flag it as "accurately an integer" if the number | |
2034 | came from a (by definition imprecise) NV operation, and | |
2035 | we're outside the range of NV integer precision */ | |
2036 | #endif | |
2037 | ) | |
2038 | SvIOK_on(sv); | |
2039 | SvIsUV_on(sv); | |
1c846c1f | 2040 | DEBUG_c(PerlIO_printf(Perl_debug_log, |
16b7a9a4 | 2041 | "0x%"UVxf" 2uv(%"UVuf" => %"IVdf") (as unsigned)\n", |
57def98f | 2042 | PTR2UV(sv), |
16b7a9a4 NC |
2043 | SvUVX(sv), |
2044 | SvUVX(sv))); | |
25da4f38 | 2045 | } |
ff68c719 | 2046 | } |
2047 | else if (SvPOKp(sv) && SvLEN(sv)) { | |
25da4f38 IZ |
2048 | I32 numtype = looks_like_number(sv); |
2049 | ||
2050 | /* We want to avoid a possible problem when we cache a UV which | |
2051 | may be later translated to an NV, and the resulting NV is not | |
2052 | the translation of the initial data. | |
1c846c1f | 2053 | |
25da4f38 IZ |
2054 | This means that if we cache such a UV, we need to cache the |
2055 | NV as well. Moreover, we trade speed for space, and do not | |
2056 | cache the NV if not needed. | |
2057 | */ | |
25da4f38 | 2058 | |
16b7a9a4 | 2059 | if ((numtype & ~IS_NUMBER_NEG) == IS_NUMBER_TO_INT_BY_ATOL) { |
25da4f38 | 2060 | /* The NV may be reconstructed from IV - safe to cache IV, |
16b7a9a4 NC |
2061 | which may be calculated by atol(). */ |
2062 | if (SvTYPE(sv) < SVt_PVIV) | |
25da4f38 IZ |
2063 | sv_upgrade(sv, SVt_PVIV); |
2064 | (void)SvIOK_on(sv); | |
16b7a9a4 NC |
2065 | SvIVX(sv) = Atol(SvPVX(sv)); |
2066 | } else { | |
16b7a9a4 | 2067 | int save_errno = errno; |
c7fc788e JH |
2068 | #ifdef HAS_STRTOUL |
2069 | { | |
2070 | UV u; | |
2071 | /* Is it an integer that we could convert with strtoul? | |
2072 | So try it, and if it doesn't set errno then it's pukka. | |
2073 | This should be faster than going atof and then thinking. */ | |
2074 | if (((numtype & | |
2075 | (IS_NUMBER_TO_INT_BY_STRTOL | IS_NUMBER_NOT_INT)) | |
2076 | == IS_NUMBER_TO_INT_BY_STRTOL) | |
2077 | && ((errno = 0), 1) /* always true */ | |
2078 | && ((u = Strtoul(SvPVX(sv), Null(char**), 10)), 1) /* ditto */ | |
2079 | && (errno == 0) | |
2080 | /* If known to be negative, check it didn't undeflow IV */ | |
2081 | && ((numtype & IS_NUMBER_NEG) ? ((IV)u <= 0) : 1)) { | |
2082 | errno = save_errno; | |
2083 | ||
2084 | if (SvTYPE(sv) < SVt_PVIV) | |
2085 | sv_upgrade(sv, SVt_PVIV); | |
2086 | (void)SvIOK_on(sv); | |
2087 | ||
2088 | /* If it's negative must use IV. | |
2089 | IV-over-UV optimisation */ | |
2090 | if (numtype & IS_NUMBER_NEG || u <= (UV) IV_MAX) { | |
2091 | /* strtoul is defined to return negated value if the | |
2092 | number starts with a minus sign. Assuming 2s | |
2093 | complement, this value will be in range for | |
2094 | a negative IV if casting the bit pattern to | |
2095 | IV doesn't produce a positive value. Allow -0 | |
2096 | by checking it's <= 0 | |
2097 | hence (numtype & IS_NUMBER_NEG) test above | |
2098 | */ | |
2099 | SvIVX(sv) = (IV)u; | |
2100 | } else { | |
2101 | /* it didn't overflow, and it was positive. */ | |
2102 | SvUVX(sv) = u; | |
2103 | SvIsUV_on(sv); | |
2104 | } | |
16b7a9a4 | 2105 | } |
c7fc788e JH |
2106 | } |
2107 | #endif | |
2108 | { | |
16b7a9a4 NC |
2109 | NV d; |
2110 | /* Hopefully trace flow will optimise this away where possible | |
2111 | */ | |
2112 | errno = save_errno; | |
c7fc788e | 2113 | |
16b7a9a4 NC |
2114 | /* It wasn't an integer, or it overflowed, or we don't have |
2115 | strtol. Do things the slow way - check if it's a IV etc. */ | |
2116 | d = Atof(SvPVX(sv)); | |
2117 | ||
2118 | if (SvTYPE(sv) < SVt_PVNV) | |
2119 | sv_upgrade(sv, SVt_PVNV); | |
2120 | SvNVX(sv) = d; | |
2121 | ||
2122 | if (! numtype && ckWARN(WARN_NUMERIC)) | |
2123 | not_a_number(sv); | |
2124 | ||
2125 | #if defined(USE_LONG_DOUBLE) | |
2126 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%" PERL_PRIgldbl ")\n", | |
2127 | PTR2UV(sv), SvNVX(sv))); | |
2128 | #else | |
2129 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%g)\n", | |
2130 | PTR2UV(sv), SvNVX(sv))); | |
2131 | #endif | |
2132 | ||
2133 | #ifdef NV_PRESERVES_UV | |
2134 | (void)SvIOKp_on(sv); | |
2135 | (void)SvNOK_on(sv); | |
2136 | if (SvNVX(sv) < (NV)IV_MAX + 0.5) { | |
2137 | SvIVX(sv) = I_V(SvNVX(sv)); | |
2138 | if ((NV)(SvIVX(sv)) == SvNVX(sv)) { | |
2139 | SvIOK_on(sv); | |
2140 | } else { | |
2141 | /* Integer is imprecise. NOK, IOKp */ | |
2142 | } | |
2143 | /* UV will not work better than IV */ | |
2144 | } else { | |
2145 | if (SvNVX(sv) > (NV)UV_MAX) { | |
2146 | SvIsUV_on(sv); | |
2147 | /* Integer is inaccurate. NOK, IOKp, is UV */ | |
2148 | SvUVX(sv) = UV_MAX; | |
2149 | SvIsUV_on(sv); | |
2150 | } else { | |
2151 | SvUVX(sv) = U_V(SvNVX(sv)); | |
2152 | /* 0xFFFFFFFFFFFFFFFF not an issue in here, NVs | |
2153 | NV preservse UV so can do correct comparison. */ | |
2154 | if ((NV)(SvUVX(sv)) == SvNVX(sv)) { | |
2155 | SvIOK_on(sv); | |
2156 | SvIsUV_on(sv); | |
2157 | } else { | |
2158 | /* Integer is imprecise. NOK, IOKp, is UV */ | |
2159 | SvIsUV_on(sv); | |
2160 | } | |
2161 | } | |
2162 | } | |
2163 | #else /* NV_PRESERVES_UV */ | |
2164 | if (((UV)1 << NV_PRESERVES_UV_BITS) > | |
2165 | U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) { | |
2166 | /* Small enough to preserve all bits. */ | |
2167 | (void)SvIOKp_on(sv); | |
2168 | SvNOK_on(sv); | |
2169 | SvIVX(sv) = I_V(SvNVX(sv)); | |
2170 | if ((NV)(SvIVX(sv)) == SvNVX(sv)) | |
2171 | SvIOK_on(sv); | |
2172 | /* Assumption: first non-preserved integer is < IV_MAX, | |
2173 | this NV is in the preserved range, therefore: */ | |
2174 | if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv)) | |
2175 | < (UV)IV_MAX)) { | |
2176 | Perl_croak(aTHX_ "sv_2uv assumed (U_V(fabs(SvNVX(sv))) < (UV)IV_MAX) but SvNVX(sv)=%g U_V is 0x%"UVxf", IV_MAX is 0x%"UVxf"\n", SvNVX(sv), U_V(SvNVX(sv)), (UV)IV_MAX); | |
2177 | } | |
2178 | } else | |
2179 | sv_2iuv_non_preserve (sv, numtype); | |
2180 | #endif /* NV_PRESERVES_UV */ | |
2181 | } | |
25da4f38 | 2182 | } |
ff68c719 | 2183 | } |
2184 | else { | |
d008e5eb | 2185 | if (!(SvFLAGS(sv) & SVs_PADTMP)) { |
d008e5eb | 2186 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing) |
1d7c1841 | 2187 | report_uninit(); |
c6ee37c5 | 2188 | } |
25da4f38 IZ |
2189 | if (SvTYPE(sv) < SVt_IV) |
2190 | /* Typically the caller expects that sv_any is not NULL now. */ | |
2191 | sv_upgrade(sv, SVt_IV); | |
ff68c719 | 2192 | return 0; |
2193 | } | |
25da4f38 | 2194 | |
1d7c1841 GS |
2195 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"UVuf")\n", |
2196 | PTR2UV(sv),SvUVX(sv))); | |
25da4f38 | 2197 | return SvIsUV(sv) ? SvUVX(sv) : (UV)SvIVX(sv); |
ff68c719 | 2198 | } |
2199 | ||
65202027 | 2200 | NV |
864dbfa3 | 2201 | Perl_sv_2nv(pTHX_ register SV *sv) |
79072805 LW |
2202 | { |
2203 | if (!sv) | |
2204 | return 0.0; | |
8990e307 | 2205 | if (SvGMAGICAL(sv)) { |
463ee0b2 LW |
2206 | mg_get(sv); |
2207 | if (SvNOKp(sv)) | |
2208 | return SvNVX(sv); | |
a0d0e21e | 2209 | if (SvPOKp(sv) && SvLEN(sv)) { |
599cee73 | 2210 | if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) && !looks_like_number(sv)) |
a0d0e21e | 2211 | not_a_number(sv); |
097ee67d | 2212 | return Atof(SvPVX(sv)); |
a0d0e21e | 2213 | } |
25da4f38 | 2214 | if (SvIOKp(sv)) { |
1c846c1f | 2215 | if (SvIsUV(sv)) |
65202027 | 2216 | return (NV)SvUVX(sv); |
25da4f38 | 2217 | else |
65202027 | 2218 | return (NV)SvIVX(sv); |
25da4f38 | 2219 | } |
16d20bd9 | 2220 | if (!SvROK(sv)) { |
d008e5eb | 2221 | if (!(SvFLAGS(sv) & SVs_PADTMP)) { |
d008e5eb | 2222 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing) |
1d7c1841 | 2223 | report_uninit(); |
c6ee37c5 | 2224 | } |
16d20bd9 AD |
2225 | return 0; |
2226 | } | |
463ee0b2 | 2227 | } |
ed6116ce | 2228 | if (SvTHINKFIRST(sv)) { |
a0d0e21e | 2229 | if (SvROK(sv)) { |
a0d0e21e | 2230 | SV* tmpstr; |
1554e226 DC |
2231 | if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) && |
2232 | (SvRV(tmpstr) != SvRV(sv))) | |
9e7bc3e8 | 2233 | return SvNV(tmpstr); |
56431972 | 2234 | return PTR2NV(SvRV(sv)); |
a0d0e21e | 2235 | } |
8a818333 NIS |
2236 | if (SvREADONLY(sv) && SvFAKE(sv)) { |
2237 | sv_force_normal(sv); | |
2238 | } | |
0336b60e | 2239 | if (SvREADONLY(sv) && !SvOK(sv)) { |
599cee73 | 2240 | if (ckWARN(WARN_UNINITIALIZED)) |
1d7c1841 | 2241 | report_uninit(); |
ed6116ce LW |
2242 | return 0.0; |
2243 | } | |
79072805 LW |
2244 | } |
2245 | if (SvTYPE(sv) < SVt_NV) { | |
463ee0b2 LW |
2246 | if (SvTYPE(sv) == SVt_IV) |
2247 | sv_upgrade(sv, SVt_PVNV); | |
2248 | else | |
2249 | sv_upgrade(sv, SVt_NV); | |
572bbb43 | 2250 | #if defined(USE_LONG_DOUBLE) |
097ee67d | 2251 | DEBUG_c({ |
f93f4e46 | 2252 | STORE_NUMERIC_LOCAL_SET_STANDARD(); |
1d7c1841 GS |
2253 | PerlIO_printf(Perl_debug_log, |
2254 | "0x%"UVxf" num(%" PERL_PRIgldbl ")\n", | |
2255 | PTR2UV(sv), SvNVX(sv)); | |
572bbb43 GS |
2256 | RESTORE_NUMERIC_LOCAL(); |
2257 | }); | |
65202027 | 2258 | #else |
572bbb43 | 2259 | DEBUG_c({ |
f93f4e46 | 2260 | STORE_NUMERIC_LOCAL_SET_STANDARD(); |
1d7c1841 GS |
2261 | PerlIO_printf(Perl_debug_log, "0x%"UVxf" num(%g)\n", |
2262 | PTR2UV(sv), SvNVX(sv)); | |
097ee67d JH |
2263 | RESTORE_NUMERIC_LOCAL(); |
2264 | }); | |
572bbb43 | 2265 | #endif |
79072805 LW |
2266 | } |
2267 | else if (SvTYPE(sv) < SVt_PVNV) | |
2268 | sv_upgrade(sv, SVt_PVNV); | |
748a9306 LW |
2269 | if (SvIOKp(sv) && |
2270 | (!SvPOKp(sv) || !strchr(SvPVX(sv),'.') || !looks_like_number(sv))) | |
93a17b20 | 2271 | { |
65202027 | 2272 | SvNVX(sv) = SvIsUV(sv) ? (NV)SvUVX(sv) : (NV)SvIVX(sv); |
16b7a9a4 NC |
2273 | #ifdef NV_PRESERVES_UV |
2274 | SvNOK_on(sv); | |
2275 | #else | |
2276 | /* Only set the public NV OK flag if this NV preserves the IV */ | |
2277 | /* Check it's not 0xFFFFFFFFFFFFFFFF */ | |
2278 | if (SvIsUV(sv) ? ((SvUVX(sv) != UV_MAX)&&(SvUVX(sv) == U_V(SvNVX(sv)))) | |
2279 | : (SvIVX(sv) == I_V(SvNVX(sv)))) | |
2280 | SvNOK_on(sv); | |
2281 | else | |
2282 | SvNOKp_on(sv); | |
2283 | #endif | |
93a17b20 | 2284 | } |
748a9306 | 2285 | else if (SvPOKp(sv) && SvLEN(sv)) { |
599cee73 | 2286 | if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) && !looks_like_number(sv)) |
a0d0e21e | 2287 | not_a_number(sv); |
097ee67d | 2288 | SvNVX(sv) = Atof(SvPVX(sv)); |
16b7a9a4 NC |
2289 | #ifdef NV_PRESERVES_UV |
2290 | SvNOK_on(sv); | |
2291 | #else | |
2292 | /* Only set the public NV OK flag if this NV preserves the value in | |
2293 | the PV at least as well as an IV/UV would. | |
2294 | Not sure how to do this 100% reliably. */ | |
2295 | /* if that shift count is out of range then Configure's test is | |
2296 | wonky. We shouldn't be in here with NV_PRESERVES_UV_BITS == | |
2297 | UV_BITS */ | |
2298 | if (((UV)1 << NV_PRESERVES_UV_BITS) > | |
2299 | U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) | |
2300 | SvNOK_on(sv); /* Definitely small enough to preserve all bits */ | |
2301 | else if (SvNVX(sv) < (NV)IV_MIN || SvNVX(sv) > (NV)UV_MAX) { | |
2302 | /* Definitely too large/small to fit in an integer, so no loss | |
2303 | of precision going to integer in the future via NV */ | |
2304 | SvNOK_on(sv); | |
2305 | } else { | |
2306 | /* Is it something we can run through strtol etc (ie no | |
2307 | trailing exponent part)? */ | |
2308 | int numtype = looks_like_number(sv); | |
2309 | /* XXX probably should cache this if called above */ | |
2310 | ||
2311 | if (!(numtype & | |
2312 | (IS_NUMBER_TO_INT_BY_ATOL | IS_NUMBER_TO_INT_BY_STRTOL))) { | |
2313 | /* Can't use strtol etc to convert this string, so don't try */ | |
2314 | SvNOK_on(sv); | |
2315 | } else | |
2316 | sv_2inuv_non_preserve (sv, numtype); | |
2317 | } | |
2318 | #endif /* NV_PRESERVES_UV */ | |
93a17b20 | 2319 | } |
79072805 | 2320 | else { |
599cee73 | 2321 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP)) |
1d7c1841 | 2322 | report_uninit(); |
25da4f38 IZ |
2323 | if (SvTYPE(sv) < SVt_NV) |
2324 | /* Typically the caller expects that sv_any is not NULL now. */ | |
16b7a9a4 NC |
2325 | /* XXX Ilya implies that this is a bug in callers that assume this |
2326 | and ideally should be fixed. */ | |
25da4f38 | 2327 | sv_upgrade(sv, SVt_NV); |
a0d0e21e | 2328 | return 0.0; |
79072805 | 2329 | } |
572bbb43 | 2330 | #if defined(USE_LONG_DOUBLE) |
097ee67d | 2331 | DEBUG_c({ |
f93f4e46 | 2332 | STORE_NUMERIC_LOCAL_SET_STANDARD(); |
1d7c1841 GS |
2333 | PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2nv(%" PERL_PRIgldbl ")\n", |
2334 | PTR2UV(sv), SvNVX(sv)); | |
572bbb43 GS |
2335 | RESTORE_NUMERIC_LOCAL(); |
2336 | }); | |
65202027 | 2337 | #else |
572bbb43 | 2338 | DEBUG_c({ |
f93f4e46 | 2339 | STORE_NUMERIC_LOCAL_SET_STANDARD(); |
1d7c1841 GS |
2340 | PerlIO_printf(Perl_debug_log, "0x%"UVxf" 1nv(%g)\n", |
2341 | PTR2UV(sv), SvNVX(sv)); | |
097ee67d JH |
2342 | RESTORE_NUMERIC_LOCAL(); |
2343 | }); | |
572bbb43 | 2344 | #endif |
463ee0b2 | 2345 | return SvNVX(sv); |
79072805 LW |
2346 | } |
2347 | ||
76e3520e | 2348 | STATIC IV |
cea2e8a9 | 2349 | S_asIV(pTHX_ SV *sv) |
36477c24 | 2350 | { |
2351 | I32 numtype = looks_like_number(sv); | |
65202027 | 2352 | NV d; |
36477c24 | 2353 | |
25da4f38 | 2354 | if (numtype & IS_NUMBER_TO_INT_BY_ATOL) |
cf2093f6 | 2355 | return Atol(SvPVX(sv)); |
d008e5eb | 2356 | if (!numtype) { |
d008e5eb GS |
2357 | if (ckWARN(WARN_NUMERIC)) |
2358 | not_a_number(sv); | |
2359 | } | |
097ee67d | 2360 | d = Atof(SvPVX(sv)); |
25da4f38 | 2361 | return I_V(d); |
36477c24 | 2362 | } |
2363 | ||
76e3520e | 2364 | STATIC UV |
cea2e8a9 | 2365 | S_asUV(pTHX_ SV *sv) |
36477c24 | 2366 | { |
2367 | I32 numtype = looks_like_number(sv); | |
2368 | ||
84902520 | 2369 | #ifdef HAS_STRTOUL |
25da4f38 | 2370 | if (numtype & IS_NUMBER_TO_INT_BY_ATOL) |
cf2093f6 | 2371 | return Strtoul(SvPVX(sv), Null(char**), 10); |
84902520 | 2372 | #endif |
d008e5eb | 2373 | if (!numtype) { |
d008e5eb GS |
2374 | if (ckWARN(WARN_NUMERIC)) |
2375 | not_a_number(sv); | |
2376 | } | |
097ee67d | 2377 | return U_V(Atof(SvPVX(sv))); |
36477c24 | 2378 | } |
2379 | ||
25da4f38 IZ |
2380 | /* |
2381 | * Returns a combination of (advisory only - can get false negatives) | |
16b7a9a4 NC |
2382 | * IS_NUMBER_TO_INT_BY_ATOL, IS_NUMBER_TO_INT_BY_ATOF |
2383 | * IS_NUMBER_LONGER_THAN_IV_MAX, IS_NUMBER_AS_LONG_AS_IV_MAX | |
2384 | * IS_NUMBER_NOT_INT, IS_NUMBER_NEG, IS_NUMBER_INFINITY | |
25da4f38 IZ |
2385 | * 0 if does not look like number. |
2386 | * | |
16b7a9a4 NC |
2387 | * (atol and strtol stop when they hit a decimal point. strtol will return |
2388 | * LONG_MAX and LONG_MIN when given out of range values. ANSI says they should | |
2389 | * do this, and vendors have had 11 years to get it right. | |
2390 | * However, will try to make it still work with only atol | |
2391 | * | |
2392 | * IS_NUMBER_TO_INT_BY_ATOL 123456789 or 123456789.3 definitely < IV_MAX | |
2393 | * IS_NUMBER_TO_INT_BY_STRTOL 123456789 or 123456789.3 if digits = IV_MAX | |
2394 | * IS_NUMBER_TO_INT_BY_ATOF 123456789e0 or >> IV_MAX | |
2395 | * IS_NUMBER_LONGER_THAN_IV_MAX lots of digits, don't bother with atol | |
2396 | * IS_NUMBER_AS_LONG_AS_IV_MAX atol might hit LONG_MAX, might not. | |
2397 | * IS_NUMBER_NOT_INT saw "." or "e" | |
2398 | * IS_NUMBER_NEG | |
300aed98 | 2399 | * IS_NUMBER_INFINITY |
25da4f38 IZ |
2400 | */ |
2401 | ||
954c1994 GS |
2402 | /* |
2403 | =for apidoc looks_like_number | |
2404 | ||
2405 | Test if an the content of an SV looks like a number (or is a | |
16b7a9a4 NC |
2406 | number). C<Inf> and C<Infinity> are treated as numbers (so will not |
2407 | issue a non-numeric warning), even if your atof() doesn't grok them. | |
954c1994 GS |
2408 | |
2409 | =cut | |
2410 | */ | |
2411 | ||
36477c24 | 2412 | I32 |
864dbfa3 | 2413 | Perl_looks_like_number(pTHX_ SV *sv) |
36477c24 | 2414 | { |
2415 | register char *s; | |
2416 | register char *send; | |
2417 | register char *sbegin; | |
25da4f38 IZ |
2418 | register char *nbegin; |
2419 | I32 numtype = 0; | |
300aed98 | 2420 | I32 sawinf = 0; |
36477c24 | 2421 | STRLEN len; |
2422 | ||
2423 | if (SvPOK(sv)) { | |
1c846c1f | 2424 | sbegin = SvPVX(sv); |
36477c24 | 2425 | len = SvCUR(sv); |
2426 | } | |
2427 | else if (SvPOKp(sv)) | |
2428 | sbegin = SvPV(sv, len); | |
2429 | else | |
2430 | return 1; | |
2431 | send = sbegin + len; | |
2432 | ||
2433 | s = sbegin; | |
2434 | while (isSPACE(*s)) | |
2435 | s++; | |
25da4f38 IZ |
2436 | if (*s == '-') { |
2437 | s++; | |
2438 | numtype = IS_NUMBER_NEG; | |
2439 | } | |
2440 | else if (*s == '+') | |
36477c24 | 2441 | s++; |
ff0cee69 | 2442 | |
25da4f38 IZ |
2443 | nbegin = s; |
2444 | /* | |
16b7a9a4 NC |
2445 | * we return IS_NUMBER_TO_INT_BY_ATOL if the number can converted to |
2446 | * integer with atol() without overflow, IS_NUMBER_TO_INT_BY_STRTOL if | |
2447 | * possibly slightly larger than max int, IS_NUMBER_TO_INT_BY_ATOF if you | |
2448 | * will need (int)atof(). | |
25da4f38 IZ |
2449 | */ |
2450 | ||
300aed98 | 2451 | /* next must be digit or the radix separator or beginning of infinity */ |
ff0cee69 | 2452 | if (isDIGIT(*s)) { |
2453 | do { | |
2454 | s++; | |
2455 | } while (isDIGIT(*s)); | |
25da4f38 | 2456 | |
d0e6d399 NC |
2457 | /* Aaargh. long long really is irritating. |
2458 | In the gospel according to ANSI 1989, it is an axiom that "long" | |
2459 | is the longest integer type, and that if you don't know how long | |
2460 | something is you can cast it to long, and nothing will be lost | |
2461 | (except possibly speed of execution if long is slower than the | |
2462 | type is was). | |
2463 | Now, one can't be sure if the old rules apply, or long long | |
2464 | (or some other newfangled thing) is actually longer than the | |
2465 | (formerly) longest thing. | |
2466 | */ | |
2467 | /* This lot will work for 64 bit *as long as* either | |
2468 | either long is 64 bit | |
2469 | or we can find both strtol/strtoq and strtoul/strtouq | |
2470 | If not, we really should refuse to let the user use 64 bit IVs | |
2471 | By "64 bit" I really mean IVs that don't get preserved by NVs | |
2472 | It also should work for 128 bit IVs. Can any lend me a machine to | |
2473 | test this? | |
2474 | */ | |
2475 | if (s - nbegin > TYPE_DIGITS(UV)) /* Cannot cache ato[ul]() */ | |
16b7a9a4 | 2476 | numtype |= IS_NUMBER_TO_INT_BY_ATOF | IS_NUMBER_LONGER_THAN_IV_MAX; |
d0e6d399 NC |
2477 | else if (s - nbegin < BIT_DIGITS(((sizeof (IV)>sizeof (long)) |
2478 | ? sizeof(long) : sizeof (IV))*8-1)) | |
25da4f38 | 2479 | numtype |= IS_NUMBER_TO_INT_BY_ATOL; |
16b7a9a4 NC |
2480 | else |
2481 | /* Can't be sure either way. (For 64 bit UV, 63 bit IV is 1 decimal | |
2482 | digit less (IV_MAX= 9223372036854775807, | |
2483 | UV_MAX= 18446744073709551615) so be cautious */ | |
2484 | numtype |= IS_NUMBER_TO_INT_BY_STRTOL | IS_NUMBER_AS_LONG_AS_IV_MAX; | |
25da4f38 | 2485 | |
097ee67d | 2486 | if (*s == '.' |
1c846c1f | 2487 | #ifdef USE_LOCALE_NUMERIC |
097ee67d JH |
2488 | || IS_NUMERIC_RADIX(*s) |
2489 | #endif | |
2490 | ) { | |
ff0cee69 | 2491 | s++; |
16b7a9a4 | 2492 | numtype |= IS_NUMBER_NOT_INT; |
097ee67d | 2493 | while (isDIGIT(*s)) /* optional digits after the radix */ |
ff0cee69 | 2494 | s++; |
2495 | } | |
36477c24 | 2496 | } |
097ee67d | 2497 | else if (*s == '.' |
1c846c1f | 2498 | #ifdef USE_LOCALE_NUMERIC |
097ee67d JH |
2499 | || IS_NUMERIC_RADIX(*s) |
2500 | #endif | |
2501 | ) { | |
ff0cee69 | 2502 | s++; |
16b7a9a4 | 2503 | numtype |= IS_NUMBER_TO_INT_BY_ATOL | IS_NUMBER_NOT_INT; |
097ee67d | 2504 | /* no digits before the radix means we need digits after it */ |
ff0cee69 | 2505 | if (isDIGIT(*s)) { |
2506 | do { | |
2507 | s++; | |
2508 | } while (isDIGIT(*s)); | |
2509 | } | |
2510 | else | |
2511 | return 0; | |
2512 | } | |
300aed98 JH |
2513 | else if (*s == 'I' || *s == 'i') { |
2514 | s++; if (*s != 'N' && *s != 'n') return 0; | |
2515 | s++; if (*s != 'F' && *s != 'f') return 0; | |
2516 | s++; if (*s == 'I' || *s == 'i') { | |
2517 | s++; if (*s != 'N' && *s != 'n') return 0; | |
2518 | s++; if (*s != 'I' && *s != 'i') return 0; | |
2519 | s++; if (*s != 'T' && *s != 't') return 0; | |
2520 | s++; if (*s != 'Y' && *s != 'y') return 0; | |
99938567 | 2521 | s++; |
300aed98 JH |
2522 | } |
2523 | sawinf = 1; | |
2524 | } | |
ff0cee69 | 2525 | else |
2526 | return 0; | |
2527 | ||
300aed98 | 2528 | if (sawinf) |
16b7a9a4 NC |
2529 | numtype = (numtype & IS_NUMBER_NEG) /* Keep track of sign */ |
2530 | | IS_NUMBER_INFINITY | IS_NUMBER_NOT_INT; | |
300aed98 JH |
2531 | else { |
2532 | /* we can have an optional exponent part */ | |
2533 | if (*s == 'e' || *s == 'E') { | |
16b7a9a4 NC |
2534 | numtype &= IS_NUMBER_NEG; |
2535 | numtype |= IS_NUMBER_TO_INT_BY_ATOF | IS_NUMBER_NOT_INT; | |
36477c24 | 2536 | s++; |
300aed98 JH |
2537 | if (*s == '+' || *s == '-') |
2538 | s++; | |
2539 | if (isDIGIT(*s)) { | |
2540 | do { | |
2541 | s++; | |
2542 | } while (isDIGIT(*s)); | |
2543 | } | |
2544 | else | |
2545 | return 0; | |
2546 | } | |
36477c24 | 2547 | } |
2548 | while (isSPACE(*s)) | |
2549 | s++; | |
80f3f388 | 2550 | if (s >= send) |
36477c24 | 2551 | return numtype; |
2552 | if (len == 10 && memEQ(sbegin, "0 but true", 10)) | |
25da4f38 | 2553 | return IS_NUMBER_TO_INT_BY_ATOL; |
36477c24 | 2554 | return 0; |
2555 | } | |
2556 | ||
79072805 | 2557 | char * |
864dbfa3 | 2558 | Perl_sv_2pv_nolen(pTHX_ register SV *sv) |
1fa8b10d JD |
2559 | { |
2560 | STRLEN n_a; | |
2561 | return sv_2pv(sv, &n_a); | |
2562 | } | |
2563 | ||
25da4f38 | 2564 | /* We assume that buf is at least TYPE_CHARS(UV) long. */ |
864dbfa3 | 2565 | static char * |
25da4f38 IZ |
2566 | uiv_2buf(char *buf, IV iv, UV uv, int is_uv, char **peob) |
2567 | { | |
25da4f38 IZ |
2568 | char *ptr = buf + TYPE_CHARS(UV); |
2569 | char *ebuf = ptr; | |
2570 | int sign; | |
25da4f38 IZ |
2571 | |
2572 | if (is_uv) | |
2573 | sign = 0; | |
2574 | else if (iv >= 0) { | |
2575 | uv = iv; | |
2576 | sign = 0; | |
2577 | } else { | |
2578 | uv = -iv; | |
2579 | sign = 1; | |
2580 | } | |
2581 | do { | |
2582 | *--ptr = '0' + (uv % 10); | |
2583 | } while (uv /= 10); | |
2584 | if (sign) | |
2585 | *--ptr = '-'; | |
2586 | *peob = ebuf; | |
2587 | return ptr; | |
2588 | } | |
2589 | ||
1fa8b10d | 2590 | char * |
864dbfa3 | 2591 | Perl_sv_2pv(pTHX_ register SV *sv, STRLEN *lp) |
79072805 LW |
2592 | { |
2593 | register char *s; | |
2594 | int olderrno; | |
46fc3d4c | 2595 | SV *tsv; |
25da4f38 IZ |
2596 | char tbuf[64]; /* Must fit sprintf/Gconvert of longest IV/NV */ |
2597 | char *tmpbuf = tbuf; | |
79072805 | 2598 | |
463ee0b2 LW |
2599 | if (!sv) { |
2600 | *lp = 0; | |
2601 | return ""; | |
2602 | } | |
8990e307 | 2603 | if (SvGMAGICAL(sv)) { |
463ee0b2 LW |
2604 | mg_get(sv); |
2605 | if (SvPOKp(sv)) { | |
2606 | *lp = SvCUR(sv); | |
2607 | return SvPVX(sv); | |
2608 | } | |
cf2093f6 | 2609 | if (SvIOKp(sv)) { |
1c846c1f | 2610 | if (SvIsUV(sv)) |
57def98f | 2611 | (void)sprintf(tmpbuf,"%"UVuf, (UV)SvUVX(sv)); |
cf2093f6 | 2612 | else |
57def98f | 2613 | (void)sprintf(tmpbuf,"%"IVdf, (IV)SvIVX(sv)); |
46fc3d4c | 2614 | tsv = Nullsv; |
a0d0e21e | 2615 | goto tokensave; |
463ee0b2 LW |
2616 | } |
2617 | if (SvNOKp(sv)) { | |
2d4389e4 | 2618 | Gconvert(SvNVX(sv), NV_DIG, 0, tmpbuf); |
46fc3d4c | 2619 | tsv = Nullsv; |
a0d0e21e | 2620 | goto tokensave; |
463ee0b2 | 2621 | } |
16d20bd9 | 2622 | if (!SvROK(sv)) { |
d008e5eb | 2623 | if (!(SvFLAGS(sv) & SVs_PADTMP)) { |
d008e5eb | 2624 | if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing) |
1d7c1841 | 2625 | report_uninit(); |
c6ee37c5 | 2626 | } |
16d20bd9 AD |
2627 | *lp = 0; |
2628 | return ""; | |
2629 | } | |
463ee0b2 | 2630 | } |
ed6116ce LW |
2631 | if (SvTHINKFIRST(sv)) { |
2632 | if (SvROK(sv)) { | |
a0d0e21e | 2633 | SV* tmpstr; |
1554e226 DC |
2634 | if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,string)) && |
2635 | (SvRV(tmpstr) != SvRV(sv))) | |
9e7bc3e8 | 2636 | return SvPV(tmpstr,*lp); |
ed6116ce LW |
2637 | sv = (SV*)SvRV(sv); |
2638 | if (!sv) | |
2639 | s = "NULLREF"; | |
2640 | else { | |
f9277f47 IZ |
2641 | MAGIC *mg; |
2642 | ||
ed6116ce | 2643 | switch (SvTYPE(sv)) { |
f9277f47 IZ |
2644 | case SVt_PVMG: |
2645 | if ( ((SvFLAGS(sv) & | |
1c846c1f | 2646 | (SVs_OBJECT|SVf_OK|SVs_GMG|SVs_SMG|SVs_RMG)) |
3149a8e4 | 2647 | == (SVs_OBJECT|SVs_RMG)) |
57668c4d | 2648 | && strEQ(s=HvNAME(SvSTASH(sv)), "Regexp") |
f9277f47 | 2649 | && (mg = mg_find(sv, 'r'))) { |
2cd61cdb | 2650 | regexp *re = (regexp *)mg->mg_obj; |
1bd3ad17 | 2651 | |
2cd61cdb | 2652 | if (!mg->mg_ptr) { |
8782bef2 GB |
2653 | char *fptr = "msix"; |
2654 | char reflags[6]; | |
2655 | char ch; | |
2656 | int left = 0; | |
2657 | int right = 4; | |
2658 | U16 reganch = (re->reganch & PMf_COMPILETIME) >> 12; | |
2659 | ||
155aba94 | 2660 | while((ch = *fptr++)) { |
8782bef2 GB |
2661 | if(reganch & 1) { |
2662 | reflags[left++] = ch; | |
2663 | } | |
2664 | else { | |
2665 | reflags[right--] = ch; | |
2666 | } | |
2667 | reganch >>= 1; | |
2668 | } | |
2669 | if(left != 4) { | |
2670 | reflags[left] = '-'; | |
2671 | left = 5; | |
2672 | } | |
2673 | ||
2674 | mg->mg_len = re->prelen + 4 + left; | |
2675 | New(616, mg->mg_ptr, mg->mg_len + 1 + left, char); | |
2676 | Copy("(?", mg->mg_ptr, 2, char); | |
2677 | Copy(reflags, mg->mg_ptr+2, left, char); | |
2678 | Copy(":", mg->mg_ptr+left+2, 1, char); | |
2679 | Copy(re->precomp, mg->mg_ptr+3+left, re->prelen, char); | |
1bd3ad17 IZ |
2680 | mg->mg_ptr[mg->mg_len - 1] = ')'; |
2681 | mg->mg_ptr[mg->mg_len] = 0; | |
2682 | } | |
3280af22 | 2683 | PL_reginterp_cnt += re->program[0].next_off; |
1bd3ad17 IZ |
2684 | *lp = mg->mg_len; |
2685 | return mg->mg_ptr; | |
f9277f47 IZ |
2686 | } |
2687 | /* Fall through */ | |
ed6116ce LW |
2688 | case SVt_NULL: |
2689 | case SVt_IV: | |
2690 | case SVt_NV: | |
2691 | case SVt_RV: | |
2692 | case SVt_PV: | |
2693 | case SVt_PVIV: | |
2694 | case SVt_PVNV: | |
81689caa HS |
2695 | case SVt_PVBM: if (SvROK(sv)) |
2696 | s = "REF"; | |
2697 | else | |
2698 | s = "SCALAR"; break; | |
ed6116ce LW |
2699 | case SVt_PVLV: s = "LVALUE"; break; |
2700 | case SVt_PVAV: s = "ARRAY"; break; | |
2701 | case SVt_PVHV: s = "HASH"; break; | |
2702 | case SVt_PVCV: s = "CODE"; break; | |
2703 | case SVt_PVGV: s = "GLOB"; break; | |
1d2dff63 | 2704 | case SVt_PVFM: s = "FORMAT"; break; |
36477c24 | 2705 | case SVt_PVIO: s = "IO"; break; |
ed6116ce LW |
2706 | default: s = "UNKNOWN"; break; |
2707 | } | |
46fc3d4c | 2708 | tsv = NEWSV(0,0); |
ed6116ce | 2709 | if (SvOBJECT(sv)) |
cea2e8a9 | 2710 | Perl_sv_setpvf(aTHX_ tsv, "%s=%s", HvNAME(SvSTASH(sv)), s); |
ed6116ce | 2711 | else |
46fc3d4c | 2712 | sv_setpv(tsv, s); |
57def98f | 2713 | Perl_sv_catpvf(aTHX_ tsv, "(0x%"UVxf")", PTR2UV(sv)); |
a0d0e21e | 2714 | goto tokensaveref; |
463ee0b2 | 2715 | } |
ed6116ce LW |
2716 | *lp = strlen(s); |
2717 | return s; | |
79072805 | 2718 | } |
0336b60e | 2719 | if (SvREADONLY(sv) && !SvOK(sv)) { |
0336b60e | 2720 | if (ckWARN(WARN_UNINITIALIZED)) |
1d7c1841 | 2721 | report_uninit(); |
ed6116ce LW |
2722 | *lp = 0; |
2723 | return ""; | |
79072805 | 2724 | } |
79072805 | 2725 | } |
16b7a9a4 NC |
2726 | if (SvIOK(sv) || ((SvIOKp(sv) && !SvNOKp(sv)))) { |
2727 | /* I'm assuming that if both IV and NV are equally valid then | |
2728 | converting the IV is going to be more efficient */ | |
2729 | U32 isIOK = SvIOK(sv); | |
2730 | U32 isUIOK = SvIsUV(sv); | |
2731 | char buf[TYPE_CHARS(UV)]; | |
2732 | char *ebuf, *ptr; | |
2733 | ||
2734 | if (SvTYPE(sv) < SVt_PVIV) | |
2735 | sv_upgrade(sv, SVt_PVIV); | |
2736 | if (isUIOK) | |
2737 | ptr = uiv_2buf(buf, 0, SvUVX(sv), 1, &ebuf); | |
2738 | else | |
2739 | ptr = uiv_2buf(buf, SvIVX(sv), 0, 0, &ebuf); | |
2740 | SvGROW(sv, ebuf - ptr + 1); /* inlined from sv_setpvn */ | |
2741 | Move(ptr,SvPVX(sv),ebuf - ptr,char); | |
2742 | SvCUR_set(sv, ebuf - ptr); | |
2743 | s = SvEND(sv); | |
2744 | *s = '\0'; | |
2745 | if (isIOK) | |
2746 | SvIOK_on(sv); | |
2747 | else | |
2748 | SvIOKp_on(sv); | |
2749 | if (isUIOK) | |
2750 | SvIsUV_on(sv); | |
2751 | } | |
2752 | else if (SvNOKp(sv)) { | |
79072805 LW |
2753 | if (SvTYPE(sv) < SVt_PVNV) |
2754 | sv_upgrade(sv, SVt_PVNV); | |
1c846c1f | 2755 | /* The +20 is pure guesswork. Configure test needed. --jhi */ |
59155cc0 | 2756 | SvGROW(sv, NV_DIG + 20); |
463ee0b2 | 2757 | s = SvPVX(sv); |
79072805 | 2758 | olderrno = errno; /* some Xenix systems wipe out errno here */ |
79072805 | 2759 | #ifdef apollo |
463ee0b2 | 2760 | if (SvNVX(sv) == 0.0) |
79072805 LW |
2761 | (void)strcpy(s,"0"); |
2762 | else | |
2763 | #endif /*apollo*/ | |
bbce6d69 | 2764 | { |
2d4389e4 | 2765 | Gconvert(SvNVX(sv), NV_DIG, 0, s); |
bbce6d69 | 2766 | } |
79072805 | 2767 | errno = olderrno; |
a0d0e21e LW |
2768 | #ifdef FIXNEGATIVEZERO |
2769 | if (*s == '-' && s[1] == '0' && !s[2]) | |
2770 | strcpy(s,"0"); | |
2771 | #endif | |
79072805 LW |
2772 | while (*s) s++; |
2773 | #ifdef hcx | |
2774 | if (s[-1] == '.') | |
46fc3d4c | 2775 | *--s = '\0'; |
79072805 LW |
2776 | #endif |
2777 | } | |
79072805 | 2778 | else { |
0336b60e IZ |
2779 | if (ckWARN(WARN_UNINITIALIZED) |
2780 | && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP)) | |
1d7c1841 | 2781 | report_uninit(); |
a0d0e21e | 2782 | *lp = 0; |
25da4f38 IZ |
2783 | if (SvTYPE(sv) < SVt_PV) |
2784 | /* Typically the caller expects that sv_any is not NULL now. */ | |
2785 | sv_upgrade(sv, SVt_PV); | |
a0d0e21e | 2786 | return ""; |
79072805 | 2787 | } |
463ee0b2 LW |
2788 | *lp = s - SvPVX(sv); |
2789 | SvCUR_set(sv, *lp); | |
79072805 | 2790 | SvPOK_on(sv); |
1d7c1841 GS |
2791 | DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n", |
2792 | PTR2UV(sv),SvPVX(sv))); | |
463ee0b2 | 2793 | return SvPVX(sv); |
a0d0e21e LW |
2794 | |
2795 | tokensave: | |
2796 | if (SvROK(sv)) { /* XXX Skip this when sv_pvn_force calls */ | |
2797 | /* Sneaky stuff here */ | |
2798 | ||
2799 | tokensaveref: | |
46fc3d4c | 2800 | if (!tsv) |
96827780 | 2801 | tsv = newSVpv(tmpbuf, 0); |
46fc3d4c | 2802 | sv_2mortal(tsv); |
2803 | *lp = SvCUR(tsv); | |
2804 | return SvPVX(tsv); | |
a0d0e21e LW |
2805 | } |
2806 | else { | |
2807 | STRLEN len; | |
46fc3d4c | 2808 | char *t; |
2809 | ||
2810 | if (tsv) { | |
2811 | sv_2mortal(tsv); | |
2812 | t = SvPVX(tsv); | |
2813 | len = SvCUR(tsv); | |
2814 | } | |
2815 | else { | |
96827780 MB |
2816 | t = tmpbuf; |
2817 | len = strlen(tmpbuf); | |
46fc3d4c | 2818 | } |
a0d0e21e | 2819 | #ifdef FIXNEGATIVEZERO |
46fc3d4c | 2820 | if (len == 2 && t[0] == '-' && t[1] == '0') { |
2821 | t = "0"; | |
2822 | len = 1; | |
2823 | } | |
a0d0e21e LW |
2824 | #endif |
2825 | (void)SvUPGRADE(sv, SVt_PV); | |
46fc3d4c | 2826 | *lp = len; |
a0d0e21e LW |
2827 | s = SvGROW(sv, len + 1); |
2828 | SvCUR_set(sv, len); | |
46fc3d4c | 2829 | (void)strcpy(s, t); |
6bf554b4 | 2830 | SvPOKp_on(sv); |
a0d0e21e LW |
2831 | return s; |
2832 | } | |
463ee0b2 LW |
2833 | } |
2834 | ||
7340a771 GS |
2835 | char * |
2836 | Perl_sv_2pvbyte_nolen(pTHX_ register SV *sv) | |
2837 | { | |
560a288e GS |
2838 | STRLEN n_a; |
2839 | return sv_2pvbyte(sv, &n_a); | |
7340a771 GS |
2840 | } |
2841 | ||
2842 | char * | |
2843 | Perl_sv_2pvbyte(pTHX_ register SV *sv, STRLEN *lp) | |
2844 | { | |
2845 | return sv_2pv(sv,lp); | |
2846 | } | |
2847 | ||
2848 | char * | |
2849 | Perl_sv_2pvutf8_nolen(pTHX_ register SV *sv) | |
2850 | { | |
560a288e GS |
2851 | STRLEN n_a; |
2852 | return sv_2pvutf8(sv, &n_a); | |
7340a771 GS |
2853 | } |
2854 | ||
2855 | char * | |
2856 | Perl_sv_2pvutf8(pTHX_ register SV *sv, STRLEN *lp) | |
2857 | { | |
560a288e | 2858 | sv_utf8_upgrade(sv); |
7d59b7e4 | 2859 | return SvPV(sv,*lp); |
7340a771 | 2860 | } |
1c846c1f | 2861 | |
463ee0b2 LW |
2862 | /* This function is only called on magical items */ |
2863 | bool | |
864dbfa3 | 2864 | Perl_sv_2bool(pTHX_ register SV *sv) |
463ee0b2 | 2865 | { |
8990e307 | 2866 | if (SvGMAGICAL(sv)) |
463ee0b2 LW |
2867 | mg_get(sv); |
2868 | ||
a0d0e21e LW |
2869 | if (!SvOK(sv)) |
2870 | return 0; | |
2871 | if (SvROK(sv)) { | |
a0d0e21e | 2872 | SV* tmpsv; |
1554e226 DC |
2873 | if (SvAMAGIC(sv) && (tmpsv=AMG_CALLun(sv,bool_)) && |
2874 | (SvRV(tmpsv) != SvRV(sv))) | |
9e7bc3e8 | 2875 | return SvTRUE(tmpsv); |
a0d0e21e LW |
2876 | return SvRV(sv) != 0; |
2877 | } | |
463ee0b2 | 2878 | if (SvPOKp(sv)) { |
11343788 MB |
2879 | register XPV* Xpvtmp; |
2880 | if ((Xpvtmp = (XPV*)SvANY(sv)) && | |
2881 | (*Xpvtmp->xpv_pv > '0' || | |
2882 | Xpvtmp->xpv_cur > 1 || | |
2883 | (Xpvtmp->xpv_cur && *Xpvtmp->xpv_pv != '0'))) | |
463ee0b2 LW |
2884 | return 1; |
2885 | else | |
2886 | return 0; | |
2887 | } | |
2888 | else { | |
2889 | if (SvIOKp(sv)) | |
2890 | return SvIVX(sv) != 0; | |
2891 | else { | |
2892 | if (SvNOKp(sv)) | |
2893 | return SvNVX(sv) != 0.0; | |
2894 | else | |
2895 | return FALSE; | |
2896 | } | |
2897 | } | |
79072805 LW |
2898 | } |
2899 | ||
c461cf8f JH |
2900 | /* |
2901 | =for apidoc sv_utf8_upgrade | |
2902 | ||
2903 | Convert the PV of an SV to its UTF8-encoded form. | |
2904 | ||
2905 | =cut | |
2906 | */ | |
2907 | ||
560a288e GS |
2908 | void |
2909 | Perl_sv_utf8_upgrade(pTHX_ register SV *sv) | |
2910 | { | |
511c2ff0 NIS |
2911 | char *s, *t, *e; |
2912 | int hibit = 0; | |
560a288e GS |
2913 | |
2914 | if (!sv || !SvPOK(sv) || SvUTF8(sv)) | |
2915 | return; | |
2916 | ||
40826f67 JH |
2917 | /* This function could be much more efficient if we had a FLAG in SVs |
2918 | * to signal if there are any hibit chars in the PV. | |
511c2ff0 | 2919 | * Given that there isn't make loop fast as possible |
560a288e | 2920 | */ |
511c2ff0 NIS |
2921 | s = SvPVX(sv); |
2922 | e = SvEND(sv); | |
2923 | t = s; | |
2924 | while (t < e) { | |
2925 | if ((hibit = *t++ & 0x80)) | |
8a818333 | 2926 | break; |
8a818333 | 2927 | } |
560a288e | 2928 | |
40826f67 | 2929 | if (hibit) { |
8a818333 NIS |
2930 | STRLEN len; |
2931 | if (SvREADONLY(sv) && SvFAKE(sv)) { | |
8a818333 NIS |
2932 | sv_force_normal(sv); |
2933 | s = SvPVX(sv); | |
2934 | } | |
2935 | len = SvCUR(sv) + 1; /* Plus the \0 */ | |
00df9076 | 2936 | SvPVX(sv) = (char*)bytes_to_utf8((U8*)s, &len); |
841d7a39 | 2937 | SvCUR(sv) = len - 1; |
511c2ff0 NIS |
2938 | if (SvLEN(sv) != 0) |
2939 | Safefree(s); /* No longer using what was there before. */ | |
841d7a39 | 2940 | SvLEN(sv) = len; /* No longer know the real size. */ |
560a288e GS |
2941 | SvUTF8_on(sv); |
2942 | } | |
2943 | } | |
2944 | ||
c461cf8f JH |
2945 | /* |
2946 | =for apidoc sv_utf8_downgrade | |
2947 | ||
2948 | Attempt to convert the PV of an SV from UTF8-encoded to byte encoding. | |
2949 | This may not be possible if the PV contains non-byte encoding characters; | |
2950 | if this is the case, either returns false or, if C<fail_ok> is not | |
2951 | true, croaks. | |
2952 | ||
2953 | =cut | |
2954 | */ | |
2955 | ||
560a288e GS |
2956 | bool |
2957 | Perl_sv_utf8_downgrade(pTHX_ register SV* sv, bool fail_ok) | |
2958 | { | |
2959 | if (SvPOK(sv) && SvUTF8(sv)) { | |
fa301091 JH |
2960 | if (SvCUR(sv)) { |
2961 | char *c = SvPVX(sv); | |
2962 | STRLEN len = SvCUR(sv); | |
2963 | ||
2964 | if (!utf8_to_bytes((U8*)c, &len)) { | |
2965 | if (fail_ok) | |
2966 | return FALSE; | |
2967 | else { | |
2968 | if (PL_op) | |
2969 | Perl_croak(aTHX_ "Wide character in %s", | |
2970 | PL_op_desc[PL_op->op_type]); | |
2971 | else | |
2972 | Perl_croak(aTHX_ "Wide character"); | |
2973 | } | |
4b3603a4 | 2974 | } |
fa301091 | 2975 | SvCUR(sv) = len; |
67e989fb | 2976 | } |
9f9ab905 | 2977 | SvUTF8_off(sv); |
560a288e | 2978 | } |
fa301091 | 2979 | |
560a288e GS |
2980 | return TRUE; |
2981 | } | |
2982 | ||
c461cf8f JH |
2983 | /* |
2984 | =for apidoc sv_utf8_encode | |
2985 | ||
2986 | Convert the PV of an SV to UTF8-encoded, but then turn off the C<SvUTF8> | |
1c846c1f | 2987 | flag so that it looks like bytes again. Nothing calls this. |
c461cf8f JH |
2988 | |
2989 | =cut | |
2990 | */ | |
2991 | ||
560a288e GS |
2992 | void |
2993 | Perl_sv_utf8_encode(pTHX_ register SV *sv) | |
2994 | { | |
2995 | sv_utf8_upgrade(sv); | |
2996 | SvUTF8_off(sv); | |
2997 | } | |
2998 | ||
2999 | bool | |
3000 | Perl_sv_utf8_decode(pTHX_ register SV *sv) | |
3001 | { | |
3002 | if (SvPOK(sv)) { | |
3003 | char *c; | |
511c2ff0 | 3004 | char *e; |
560a288e GS |
3005 | bool has_utf = FALSE; |
3006 | if (!sv_utf8_downgrade(sv, TRUE)) | |
3007 | return FALSE; | |
3008 | ||
3009 | /* it is actually just a matter of turning the utf8 flag on, but | |
3010 | * we want to make sure everything inside is valid utf8 first. | |
3011 | */ | |
3012 | c = SvPVX(sv); | |
00df9076 | 3013 | if (!is_utf8_string((U8*)c, SvCUR(sv)+1)) |
67e989fb | 3014 | return FALSE; |
511c2ff0 NIS |
3015 | e = SvEND(sv); |
3016 | while (c < e) { | |
67e989fb JH |
3017 | if (*c++ & 0x80) { |
3018 | SvUTF8_on(sv); | |
3019 | break; | |
3020 | } | |
560a288e | 3021 | } |
560a288e GS |
3022 | } |
3023 | return TRUE; | |
3024 | } | |
3025 | ||
3026 | ||
79072805 | 3027 | /* Note: sv_setsv() should not be called with a source string that needs |
463ee0b2 | 3028 | * to be reused, since it may destroy the source string if it is marked |
79072805 LW |
3029 | * as temporary. |
3030 | */ | |
3031 | ||
954c1994 GS |
3032 | /* |
3033 | =for apidoc sv_setsv | |
3034 | ||
3035 | Copies the contents of the source SV C<ssv> into the destination SV C<dsv>. | |
3036 | The source SV may be destroyed if it is mortal. Does not handle 'set' | |
3037 | magic. See the macro forms C<SvSetSV>, C<SvSetSV_nosteal> and | |
3038 | C<sv_setsv_mg>. | |
3039 | ||
3040 | =cut | |
3041 | */ | |
3042 | ||
79072805 | 3043 | void |
864dbfa3 | 3044 | Perl_sv_setsv(pTHX_ SV *dstr, register SV *sstr) |
79072805 | 3045 | { |
8990e307 LW |
3046 | register U32 sflags; |
3047 | register int dtype; | |
3048 | register int stype; | |
463ee0b2 | 3049 | |
79072805 LW |
3050 | if (sstr == dstr) |
3051 | return; | |
2213622d | 3052 | SV_CHECK_THINKFIRST(dstr); |
79072805 | 3053 | if (!sstr) |
3280af22 | 3054 | sstr = &PL_sv_undef; |
8990e307 LW |
3055 | stype = SvTYPE(sstr); |
3056 | dtype = SvTYPE(dstr); | |
79072805 | 3057 | |
a0d0e21e | 3058 | SvAMAGIC_off(dstr); |
9e7bc3e8 | 3059 | |
463ee0b2 | 3060 | /* There's a lot of redundancy below but we're going for speed here */ |
79072805 | 3061 | |
8990e307 | 3062 | switch (stype) { |
79072805 | 3063 | case SVt_NULL: |
aece5585 | 3064 | undef_sstr: |
20408e3c GS |
3065 | if (dtype != SVt_PVGV) { |
3066 | (void)SvOK_off(dstr); | |
3067 | return; | |
3068 | } | |
3069 | break; | |
463ee0b2 | 3070 | case SVt_IV: |
aece5585 GA |
3071 | if (SvIOK(sstr)) { |
3072 | switch (dtype) { | |
3073 | case SVt_NULL: | |
8990e307 | 3074 | sv_upgrade(dstr, SVt_IV); |
aece5585 GA |
3075 | break; |
3076 | case SVt_NV: | |
8990e307 | 3077 | sv_upgrade(dstr, SVt_PVNV); |
aece5585 GA |
3078 | break; |
3079 | case SVt_RV: | |
3080 | case SVt_PV: | |
a0d0e21e | 3081 | sv_upgrade(dstr, SVt_PVIV); |
aece5585 GA |
3082 | break; |
3083 | } | |
3084 | (void)SvIOK_only(dstr); | |
3085 | SvIVX(dstr) = SvIVX(sstr); | |
25da4f38 IZ |
3086 | if (SvIsUV(sstr)) |
3087 | SvIsUV_on(dstr); | |
27c9684d AP |
3088 | if (SvTAINTED(sstr)) |
3089 | SvTAINT(dstr); | |
aece5585 | 3090 | return; |
8990e307 | 3091 | } |
aece5585 GA |
3092 | goto undef_sstr; |
3093 | ||
463ee0b2 | 3094 | case SVt_NV: |
aece5585 GA |
3095 | if (SvNOK(sstr)) { |
3096 | switch (dtype) { | |
3097 | case SVt_NULL: | |
3098 | case SVt_IV: | |
8990e307 | 3099 | sv_upgrade(dstr, SVt_NV); |
aece5585 GA |
3100 | break; |
3101 | case SVt_RV: | |
3102 | case SVt_PV: | |
3103 | case SVt_PVIV: | |
a0d0e21e | 3104 | sv_upgrade(dstr, SVt_PVNV); |
aece5585 GA |
3105 | break; |
3106 | } | |
3107 | SvNVX(dstr) = SvNVX(sstr); | |
3108 | (void)SvNOK_only(dstr); | |
27c9684d AP |
3109 | if (SvTAINTED(sstr)) |
3110 | SvTAINT(dstr); | |
aece5585 | 3111 | return; |
8990e307 | 3112 | } |
aece5585 GA |
3113 | goto undef_sstr; |
3114 | ||
ed6116ce | 3115 | case SVt_RV: |
8990e307 | 3116 | if (dtype < SVt_RV) |
ed6116ce | 3117 | sv_upgrade(dstr, SVt_RV); |
c07a80fd | 3118 | else if (dtype == SVt_PVGV && |
3119 | SvTYPE(SvRV(sstr)) == SVt_PVGV) { | |
3120 | sstr = SvRV(sstr); | |
a5f75d66 | 3121 | if (sstr == dstr) { |
1d7c1841 GS |
3122 | if (GvIMPORTED(dstr) != GVf_IMPORTED |
3123 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) | |
3124 | { | |
a5f75d66 | 3125 | GvIMPORTED_on(dstr); |
1d7c1841 | 3126 | } |
a5f75d66 AD |
3127 | GvMULTI_on(dstr); |
3128 | return; | |
3129 | } | |
c07a80fd | 3130 | goto glob_assign; |
3131 | } | |
ed6116ce | 3132 | break; |
463ee0b2 | 3133 | case SVt_PV: |
fc36a67e | 3134 | case SVt_PVFM: |
8990e307 | 3135 | if (dtype < SVt_PV) |
463ee0b2 | 3136 | sv_upgrade(dstr, SVt_PV); |
463ee0b2 LW |
3137 | break; |
3138 | case SVt_PVIV: | |
8990e307 | 3139 | if (dtype < SVt_PVIV) |
463ee0b2 | 3140 | sv_upgrade(dstr, SVt_PVIV); |
463ee0b2 LW |
3141 | break; |
3142 | case SVt_PVNV: | |
8990e307 | 3143 | if (dtype < SVt_PVNV) |
463ee0b2 | 3144 | sv_upgrade(dstr, SVt_PVNV); |
463ee0b2 | 3145 | break; |
4633a7c4 LW |
3146 | case SVt_PVAV: |
3147 | case SVt_PVHV: | |
3148 | case SVt_PVCV: | |
4633a7c4 | 3149 | case SVt_PVIO: |
533c011a | 3150 | if (PL_op) |
cea2e8a9 | 3151 | Perl_croak(aTHX_ "Bizarre copy of %s in %s", sv_reftype(sstr, 0), |
22c35a8c | 3152 | PL_op_name[PL_op->op_type]); |
4633a7c4 | 3153 | else |
cea2e8a9 | 3154 | Perl_croak(aTHX_ "Bizarre copy of %s", sv_reftype(sstr, 0)); |
4633a7c4 LW |
3155 | break; |
3156 | ||
79072805 | 3157 | case SVt_PVGV: |
8990e307 | 3158 | if (dtype <= SVt_PVGV) { |
c07a80fd | 3159 | glob_assign: |
a5f75d66 | 3160 | if (dtype != SVt_PVGV) { |
a0d0e21e LW |
3161 | char *name = GvNAME(sstr); |
3162 | STRLEN len = GvNAMELEN(sstr); | |
463ee0b2 | 3163 | sv_upgrade(dstr, SVt_PVGV); |
6662521e | 3164 | sv_magic(dstr, dstr, '*', Nullch, 0); |
85aff577 | 3165 | GvSTASH(dstr) = (HV*)SvREFCNT_inc(GvSTASH(sstr)); |
a0d0e21e LW |
3166 | GvNAME(dstr) = savepvn(name, len); |
3167 | GvNAMELEN(dstr) = len; | |
3168 | SvFAKE_on(dstr); /* can coerce to non-glob */ | |
3169 | } | |
7bac28a0 | 3170 | /* ahem, death to those who redefine active sort subs */ |
3280af22 NIS |
3171 | else if (PL_curstackinfo->si_type == PERLSI_SORT |
3172 | && GvCV(dstr) && PL_sortcop == CvSTART(GvCV(dstr))) | |
cea2e8a9 | 3173 | Perl_croak(aTHX_ "Can't redefine active sort subroutine %s", |
7bac28a0 | 3174 | GvNAME(dstr)); |
a0d0e21e | 3175 | (void)SvOK_off(dstr); |
a5f75d66 | 3176 | GvINTRO_off(dstr); /* one-shot flag */ |
1edc1566 | 3177 | gp_free((GV*)dstr); |
79072805 | 3178 | GvGP(dstr) = gp_ref(GvGP(sstr)); |
27c9684d AP |
3179 | if (SvTAINTED(sstr)) |
3180 | SvTAINT(dstr); | |
1d7c1841 GS |
3181 | if (GvIMPORTED(dstr) != GVf_IMPORTED |
3182 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) | |
3183 | { | |
a5f75d66 | 3184 | GvIMPORTED_on(dstr); |
1d7c1841 | 3185 | } |
a5f75d66 | 3186 | GvMULTI_on(dstr); |
79072805 LW |
3187 | return; |
3188 | } | |
3189 | /* FALL THROUGH */ | |
3190 | ||
3191 | default: | |
973f89ab CS |
3192 | if (SvGMAGICAL(sstr)) { |
3193 | mg_get(sstr); | |
3194 | if (SvTYPE(sstr) != stype) { | |
3195 | stype = SvTYPE(sstr); | |
3196 | if (stype == SVt_PVGV && dtype <= SVt_PVGV) | |
3197 | goto glob_assign; | |
3198 | } | |
3199 | } | |
ded42b9f | 3200 | if (stype == SVt_PVLV) |
6fc92669 | 3201 | (void)SvUPGRADE(dstr, SVt_PVNV); |
ded42b9f | 3202 | else |
6fc92669 | 3203 | (void)SvUPGRADE(dstr, stype); |
79072805 LW |
3204 | } |
3205 | ||
8990e307 LW |
3206 | sflags = SvFLAGS(sstr); |
3207 | ||
3208 | if (sflags & SVf_ROK) { | |
3209 | if (dtype >= SVt_PV) { | |
3210 | if (dtype == SVt_PVGV) { | |
3211 | SV *sref = SvREFCNT_inc(SvRV(sstr)); | |
3212 | SV *dref = 0; | |
a5f75d66 | 3213 | int intro = GvINTRO(dstr); |
a0d0e21e LW |
3214 | |
3215 | if (intro) { | |
3216 | GP *gp; | |
1d7c1841 | 3217 | gp_free((GV*)dstr); |
a5f75d66 | 3218 | GvINTRO_off(dstr); /* one-shot flag */ |
a0d0e21e | 3219 | Newz(602,gp, 1, GP); |
44a8e56a | 3220 | GvGP(dstr) = gp_ref(gp); |
a0d0e21e | 3221 | GvSV(dstr) = NEWSV(72,0); |
1d7c1841 | 3222 | GvLINE(dstr) = CopLINE(PL_curcop); |
1edc1566 | 3223 | GvEGV(dstr) = (GV*)dstr; |
a0d0e21e | 3224 | } |
a5f75d66 | 3225 | GvMULTI_on(dstr); |
8990e307 LW |
3226 | switch (SvTYPE(sref)) { |
3227 | case SVt_PVAV: | |
a0d0e21e LW |
3228 | if (intro) |
3229 | SAVESPTR(GvAV(dstr)); | |
3230 | else | |
3231 | dref = (SV*)GvAV(dstr); | |
8990e307 | 3232 | GvAV(dstr) = (AV*)sref; |
39bac7f7 | 3233 | if (!GvIMPORTED_AV(dstr) |
1d7c1841 GS |
3234 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) |
3235 | { | |
a5f75d66 | 3236 | GvIMPORTED_AV_on(dstr); |
1d7c1841 | 3237 | } |
8990e307 LW |
3238 | break; |
3239 | case SVt_PVHV: | |
a0d0e21e LW |
3240 | if (intro) |
3241 | SAVESPTR(GvHV(dstr)); | |
3242 | else | |
3243 | dref = (SV*)GvHV(dstr); | |
8990e307 | 3244 | GvHV(dstr) = (HV*)sref; |
39bac7f7 | 3245 | if (!GvIMPORTED_HV(dstr) |
1d7c1841 GS |
3246 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) |
3247 | { | |
a5f75d66 | 3248 | GvIMPORTED_HV_on(dstr); |
1d7c1841 | 3249 | } |
8990e307 LW |
3250 | break; |
3251 | case SVt_PVCV: | |
8ebc5c01 | 3252 | if (intro) { |
3253 | if (GvCVGEN(dstr) && GvCV(dstr) != (CV*)sref) { | |
3254 | SvREFCNT_dec(GvCV(dstr)); | |
3255 | GvCV(dstr) = Nullcv; | |
68dc0745 | 3256 | GvCVGEN(dstr) = 0; /* Switch off cacheness. */ |
3280af22 | 3257 | PL_sub_generation++; |
8ebc5c01 | 3258 | } |
a0d0e21e | 3259 | SAVESPTR(GvCV(dstr)); |
8ebc5c01 | 3260 | } |
68dc0745 | 3261 | else |
3262 | dref = (SV*)GvCV(dstr); | |
3263 | if (GvCV(dstr) != (CV*)sref) { | |
748a9306 | 3264 | CV* cv = GvCV(dstr); |
4633a7c4 | 3265 | if (cv) { |
68dc0745 | 3266 | if (!GvCVGEN((GV*)dstr) && |
3267 | (CvROOT(cv) || CvXSUB(cv))) | |
3268 | { | |
beab0874 | 3269 | SV *const_sv; |
7bac28a0 | 3270 | /* ahem, death to those who redefine |
3271 | * active sort subs */ | |
3280af22 NIS |
3272 | if (PL_curstackinfo->si_type == PERLSI_SORT && |
3273 | PL_sortcop == CvSTART(cv)) | |
1c846c1f | 3274 | Perl_croak(aTHX_ |
7bac28a0 | 3275 | "Can't redefine active sort subroutine %s", |
3276 | GvENAME((GV*)dstr)); | |
beab0874 JT |
3277 | /* Redefining a sub - warning is mandatory if |
3278 | it was a const and its value changed. */ | |
3279 | if (ckWARN(WARN_REDEFINE) | |
3280 | || (CvCONST(cv) | |
3281 | && (!CvCONST((CV*)sref) | |
3282 | || sv_cmp(cv_const_sv(cv), | |
3283 | cv_const_sv((CV*)sref))))) | |
3284 | { | |
3285 | Perl_warner(aTHX_ WARN_REDEFINE, | |
3286 | CvCONST(cv) | |
3287 | ? "Constant subroutine %s redefined" | |
47deb5e7 | 3288 | : "Subroutine %s redefined", |
beab0874 JT |
3289 | GvENAME((GV*)dstr)); |
3290 | } | |
9607fc9c | 3291 | } |
3fe9a6f1 | 3292 | cv_ckproto(cv, (GV*)dstr, |
3293 | SvPOK(sref) ? SvPVX(sref) : Nullch); | |
4633a7c4 | 3294 | } |
a5f75d66 | 3295 | GvCV(dstr) = (CV*)sref; |
7a4c00b4 | 3296 | GvCVGEN(dstr) = 0; /* Switch off cacheness. */ |
a5f75d66 | 3297 | GvASSUMECV_on(dstr); |
3280af22 | 3298 | PL_sub_generation++; |
a5f75d66 | 3299 | } |
39bac7f7 | 3300 | if (!GvIMPORTED_CV(dstr) |
1d7c1841 GS |
3301 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) |
3302 | { | |
a5f75d66 | 3303 | GvIMPORTED_CV_on(dstr); |
1d7c1841 | 3304 | } |
8990e307 | 3305 | break; |
91bba347 LW |
3306 | case SVt_PVIO: |
3307 | if (intro) | |
3308 | SAVESPTR(GvIOp(dstr)); | |
3309 | else | |
3310 | dref = (SV*)GvIOp(dstr); | |
3311 | GvIOp(dstr) = (IO*)sref; | |
3312 | break; | |
f4d13ee9 JH |
3313 | case SVt_PVFM: |
3314 | if (intro) | |
3315 | SAVESPTR(GvFORM(dstr)); | |
3316 | else | |
3317 | dref = (SV*)GvFORM(dstr); | |
3318 | GvFORM(dstr) = (CV*)sref; | |
3319 | break; | |
8990e307 | 3320 | default: |
a0d0e21e LW |
3321 | if (intro) |
3322 | SAVESPTR(GvSV(dstr)); | |
3323 | else | |
3324 | dref = (SV*)GvSV(dstr); | |
8990e307 | 3325 | GvSV(dstr) = sref; |
39bac7f7 | 3326 | if (!GvIMPORTED_SV(dstr) |
1d7c1841 GS |
3327 | && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) |
3328 | { | |
a5f75d66 | 3329 | GvIMPORTED_SV_on(dstr); |
1d7c1841 | 3330 | } |
8990e307 LW |
3331 | break; |
3332 | } | |
3333 | if (dref) | |
3334 | SvREFCNT_dec(dref); | |
a0d0e21e LW |
3335 | if (intro) |
3336 | SAVEFREESV(sref); | |
27c9684d AP |
3337 | if (SvTAINTED(sstr)) |
3338 | SvTAINT(dstr); | |
8990e307 LW |
3339 | return; |
3340 | } | |
a0d0e21e | 3341 | if (SvPVX(dstr)) { |
760ac839 | 3342 | (void)SvOOK_off(dstr); /* backoff */ |
50483b2c JD |
3343 | if (SvLEN(dstr)) |
3344 | Safefree(SvPVX(dstr)); | |
a0d0e21e LW |
3345 | SvLEN(dstr)=SvCUR(dstr)=0; |
3346 | } | |
8990e307 | 3347 | } |
a0d0e21e | 3348 | (void)SvOK_off(dstr); |
8990e307 | 3349 | SvRV(dstr) = SvREFCNT_inc(SvRV(sstr)); |
ed6116ce | 3350 | SvROK_on(dstr); |
8990e307 | 3351 | if (sflags & SVp_NOK) { |
ed6116ce LW |
3352 | SvNOK_on(dstr); |
3353 | SvNVX(dstr) = SvNVX(sstr); | |
3354 | } | |
8990e307 | 3355 | if (sflags & SVp_IOK) { |
a0d0e21e | 3356 | (void)SvIOK_on(dstr); |
ed6116ce | 3357 | SvIVX(dstr) = SvIVX(sstr); |
2b1c7e3e | 3358 | if (sflags & SVf_IVisUV) |
25da4f38 | 3359 | SvIsUV_on(dstr); |
ed6116ce | 3360 | } |
a0d0e21e LW |
3361 | if (SvAMAGIC(sstr)) { |
3362 | SvAMAGIC_on(dstr); | |
3363 | } | |
ed6116ce | 3364 | } |
8990e307 | 3365 | else if (sflags & SVp_POK) { |
79072805 LW |
3366 | |
3367 | /* | |
3368 | * Check to see if we can just swipe the string. If so, it's a | |
3369 | * possible small lose on short strings, but a big win on long ones. | |
463ee0b2 LW |
3370 | * It might even be a win on short strings if SvPVX(dstr) |
3371 | * has to be allocated and SvPVX(sstr) has to be freed. | |
79072805 LW |
3372 | */ |
3373 | ||
ff68c719 | 3374 | if (SvTEMP(sstr) && /* slated for free anyway? */ |
01b73108 | 3375 | SvREFCNT(sstr) == 1 && /* and no other references to it? */ |
1c846c1f | 3376 | !(sflags & SVf_OOK) && /* and not involved in OOK hack? */ |
4c8f17b9 BH |
3377 | SvLEN(sstr) && /* and really is a string */ |
3378 | !(PL_op && PL_op->op_type == OP_AASSIGN)) /* and won't be needed again, potentially */ | |
a5f75d66 | 3379 | { |
adbc6bb1 | 3380 | if (SvPVX(dstr)) { /* we know that dtype >= SVt_PV */ |
a5f75d66 AD |
3381 | if (SvOOK(dstr)) { |
3382 | SvFLAGS(dstr) &= ~SVf_OOK; | |
3383 | Safefree(SvPVX(dstr) - SvIVX(dstr)); | |
3384 | } | |
50483b2c | 3385 | else if (SvLEN(dstr)) |
a5f75d66 | 3386 | Safefree(SvPVX(dstr)); |
79072805 | 3387 | } |
a5f75d66 | 3388 | (void)SvPOK_only(dstr); |
463ee0b2 | 3389 | SvPV_set(dstr, SvPVX(sstr)); |
79072805 LW |
3390 | SvLEN_set(dstr, SvLEN(sstr)); |
3391 | SvCUR_set(dstr, SvCUR(sstr)); | |
f4e86e0f | 3392 | |
79072805 | 3393 | SvTEMP_off(dstr); |
2b1c7e3e | 3394 | (void)SvOK_off(sstr); /* NOTE: nukes most SvFLAGS on sstr */ |
79072805 LW |
3395 | SvPV_set(sstr, Nullch); |
3396 | SvLEN_set(sstr, 0); | |
a5f75d66 AD |
3397 | SvCUR_set(sstr, 0); |
3398 | SvTEMP_off(sstr); | |
79072805 LW |
3399 | } |
3400 | else { /* have to copy actual string */ | |
8990e307 LW |
3401 | STRLEN len = SvCUR(sstr); |
3402 | ||
3403 | SvGROW(dstr, len + 1); /* inlined from sv_setpvn */ | |
3404 | Move(SvPVX(sstr),SvPVX(dstr),len,char); | |
3405 | SvCUR_set(dstr, len); | |
3406 | *SvEND(dstr) = '\0'; | |
a0d0e21e | 3407 | (void)SvPOK_only(dstr); |
79072805 | 3408 | } |
3aa33fe5 | 3409 | if ((sflags & SVf_UTF8) && !IN_BYTE) |
a7cb1f99 | 3410 | SvUTF8_on(dstr); |
79072805 | 3411 | /*SUPPRESS 560*/ |
8990e307 | 3412 | if (sflags & SVp_NOK) { |
79072805 | 3413 | SvNOK_on(dstr); |
463ee0b2 | 3414 | SvNVX(dstr) = SvNVX(sstr); |
79072805 | 3415 | } |
8990e307 | 3416 | if (sflags & SVp_IOK) { |
a0d0e21e | 3417 | (void)SvIOK_on(dstr); |
463ee0b2 | 3418 | SvIVX(dstr) = SvIVX(sstr); |
2b1c7e3e | 3419 | if (sflags & SVf_IVisUV) |
25da4f38 | 3420 | SvIsUV_on(dstr); |
79072805 LW |
3421 | } |
3422 | } | |
8990e307 | 3423 | else if (sflags & SVp_NOK) { |
463ee0b2 | 3424 | SvNVX(dstr) = SvNVX(sstr); |
a0d0e21e | 3425 | (void)SvNOK_only(dstr); |
2b1c7e3e | 3426 | if (sflags & SVf_IOK) { |
a0d0e21e | 3427 | (void)SvIOK_on(dstr); |
463ee0b2 | 3428 | SvIVX(dstr) = SvIVX(sstr); |
25da4f38 | 3429 | /* XXXX Do we want to set IsUV for IV(ROK)? Be extra safe... */ |
2b1c7e3e | 3430 | if (sflags & SVf_IVisUV) |
25da4f38 | 3431 | SvIsUV_on(dstr); |
79072805 LW |
3432 | } |
3433 | } | |
8990e307 | 3434 | else if (sflags & SVp_IOK) { |
a0d0e21e | 3435 | (void)SvIOK_only(dstr); |
463ee0b2 | 3436 | SvIVX(dstr) = SvIVX(sstr); |
2b1c7e3e | 3437 | if (sflags & SVf_IVisUV) |
25da4f38 | 3438 | SvIsUV_on(dstr); |
79072805 LW |
3439 | } |
3440 | else { | |
20408e3c | 3441 | if (dtype == SVt_PVGV) { |
e476b1b5 GS |
3442 | if (ckWARN(WARN_MISC)) |
3443 | Perl_warner(aTHX_ WARN_MISC, "Undefined value assigned to typeglob"); | |
20408e3c GS |
3444 | } |
3445 | else | |
3446 | (void)SvOK_off(dstr); | |
a0d0e21e | 3447 | } |
27c9684d AP |
3448 | if (SvTAINTED(sstr)) |
3449 | SvTAINT(dstr); | |
79072805 LW |
3450 | } |
3451 | ||
954c1994 GS |
3452 | /* |
3453 | =for apidoc sv_setsv_mg | |
3454 | ||
3455 | Like C<sv_setsv>, but also handles 'set' magic. | |
3456 | ||
3457 | =cut | |
3458 | */ | |
3459 | ||
79072805 | 3460 | void |
864dbfa3 | 3461 | Perl_sv_setsv_mg(pTHX_ SV *dstr, register SV *sstr) |
ef50df4b GS |
3462 | { |
3463 | sv_setsv(dstr,sstr); | |
3464 | SvSETMAGIC(dstr); | |
3465 | } | |
3466 | ||
954c1994 GS |
3467 | /* |
3468 | =for apidoc sv_setpvn | |
3469 | ||
3470 | Copies a string into an SV. The C<len> parameter indicates the number of | |
3471 | bytes to be copied. Does not handle 'set' magic. See C<sv_setpvn_mg>. | |
3472 | ||
3473 | =cut | |
3474 | */ | |
3475 | ||
ef50df4b | 3476 | void |
864dbfa3 | 3477 | Perl_sv_setpvn(pTHX_ register SV *sv, register const char *ptr, register STRLEN len) |
79072805 | 3478 | { |
c6f8c383 | 3479 | register char *dptr; |
793db0cb JH |
3480 | { |
3481 | /* len is STRLEN which is unsigned, need to copy to signed */ | |
3482 | IV iv = len; | |
3483 | assert(iv >= 0); | |
3484 | } | |
2213622d | 3485 | SV_CHECK_THINKFIRST(sv); |
463ee0b2 | 3486 | if (!ptr) { |
a0d0e21e | 3487 | (void)SvOK_off(sv); |
463ee0b2 LW |
3488 | return; |
3489 | } | |
6fc92669 | 3490 | (void)SvUPGRADE(sv, SVt_PV); |
c6f8c383 | 3491 | |
79072805 | 3492 | SvGROW(sv, len + 1); |
c6f8c383 GA |
3493 | dptr = SvPVX(sv); |
3494 | Move(ptr,dptr,len,char); | |
3495 | dptr[len] = '\0'; | |
79072805 | 3496 | SvCUR_set(sv, len); |
a0d0e21e | 3497 | (void)SvPOK_only(sv); /* validate pointer */ |
463ee0b2 | 3498 | SvTAINT(sv); |
79072805 LW |
3499 | } |
3500 | ||
954c1994 GS |
3501 | /* |
3502 | =for apidoc sv_setpvn_mg | |
3503 | ||
3504 | Like C<sv_setpvn>, but also handles 'set' magic. | |
3505 | ||
3506 | =cut | |
3507 | */ | |
3508 | ||
79072805 | 3509 | void |
864dbfa3 | 3510 | Perl_sv_setpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len) |
ef50df4b GS |
3511 | { |
3512 | sv_setpvn(sv,ptr,len); | |
3513 | SvSETMAGIC(sv); | |
3514 | } | |
3515 | ||
954c1994 GS |
3516 | /* |
3517 | =for apidoc sv_setpv | |
3518 | ||
3519 | Copies a string into an SV. The string must be null-terminated. Does not | |
3520 | handle 'set' magic. See C<sv_setpv_mg>. | |
3521 | ||
3522 | =cut | |
3523 | */ | |
3524 | ||
ef50df4b | 3525 | void |
864dbfa3 | 3526 | Perl_sv_setpv(pTHX_ register SV *sv, register const char *ptr) |
79072805 LW |
3527 | { |
3528 | register STRLEN len; | |
3529 | ||
2213622d | 3530 | SV_CHECK_THINKFIRST(sv); |
463ee0b2 | 3531 | if (!ptr) { |
a0d0e21e | 3532 | (void)SvOK_off(sv); |
463ee0b2 LW |
3533 | return; |
3534 | } | |
79072805 | 3535 | len = strlen(ptr); |
6fc92669 | 3536 | (void)SvUPGRADE(sv, SVt_PV); |
c6f8c383 | 3537 | |
79072805 | 3538 | SvGROW(sv, len + 1); |
463ee0b2 | 3539 | Move(ptr,SvPVX(sv),len+1,char); |
79072805 | 3540 | SvCUR_set(sv, len); |
a0d0e21e | 3541 | (void)SvPOK_only(sv); /* validate pointer */ |
463ee0b2 LW |
3542 | SvTAINT(sv); |
3543 | } | |
3544 | ||
954c1994 GS |
3545 | /* |
3546 | =for apidoc sv_setpv_mg | |
3547 | ||
3548 | Like C<sv_setpv>, but also handles 'set' magic. | |
3549 | ||
3550 | =cut | |
3551 | */ | |
3552 | ||
463ee0b2 | 3553 | void |
864dbfa3 | 3554 | Perl_sv_setpv_mg(pTHX_ register SV *sv, register const char *ptr) |
ef50df4b GS |
3555 | { |
3556 | sv_setpv(sv,ptr); | |
3557 | SvSETMAGIC(sv); | |
3558 | } | |
3559 | ||
954c1994 GS |
3560 | /* |
3561 | =for apidoc sv_usepvn | |
3562 | ||
3563 | Tells an SV to use C<ptr> to find its string value. Normally the string is | |
1c846c1f | 3564 | stored inside the SV but sv_usepvn allows the SV to use an outside string. |
954c1994 GS |
3565 | The C<ptr> should point to memory that was allocated by C<malloc>. The |
3566 | string length, C<len>, must be supplied. This function will realloc the | |
3567 | memory pointed to by C<ptr>, so that pointer should not be freed or used by | |
3568 | the programmer after giving it to sv_usepvn. Does not handle 'set' magic. | |
3569 | See C<sv_usepvn_mg>. | |
3570 | ||
3571 | =cut | |
3572 | */ | |
3573 | ||
ef50df4b | 3574 | void |
864dbfa3 | 3575 | Perl_sv_usepvn(pTHX_ register SV *sv, register char *ptr, register STRLEN len) |
463ee0b2 | 3576 | { |
2213622d | 3577 | SV_CHECK_THINKFIRST(sv); |
c6f8c383 | 3578 | (void)SvUPGRADE(sv, SVt_PV); |
463ee0b2 | 3579 | if (!ptr) { |
a0d0e21e | 3580 | (void)SvOK_off(sv); |
463ee0b2 LW |
3581 | return; |
3582 | } | |
a0ed51b3 | 3583 | (void)SvOOK_off(sv); |
50483b2c | 3584 | if (SvPVX(sv) && SvLEN(sv)) |
463ee0b2 LW |
3585 | Safefree(SvPVX(sv)); |
3586 | Renew(ptr, len+1, char); | |
3587 | SvPVX(sv) = ptr; | |
3588 | SvCUR_set(sv, len); | |
3589 | SvLEN_set(sv, len+1); | |
3590 | *SvEND(sv) = '\0'; | |
a0d0e21e | 3591 | (void)SvPOK_only(sv); /* validate pointer */ |
463ee0b2 | 3592 | SvTAINT(sv); |
79072805 LW |
3593 | } |
3594 | ||
954c1994 GS |
3595 | /* |
3596 | =for apidoc sv_usepvn_mg | |
3597 | ||
3598 | Like C<sv_usepvn>, but also handles 'set' magic. | |
3599 | ||
3600 | =cut | |
3601 | */ | |
3602 | ||
ef50df4b | 3603 | void |
864dbfa3 | 3604 | Perl_sv_usepvn_mg(pTHX_ register SV *sv, register char *ptr, register STRLEN len) |
ef50df4b | 3605 | { |
51c1089b | 3606 | sv_usepvn(sv,ptr,len); |
ef50df4b GS |
3607 | SvSETMAGIC(sv); |
3608 | } | |
3609 | ||
6fc92669 | 3610 | void |
840a7b70 | 3611 | Perl_sv_force_normal_flags(pTHX_ register SV *sv, U32 flags) |
0f15f207 | 3612 | { |
2213622d | 3613 | if (SvREADONLY(sv)) { |
1c846c1f NIS |
3614 | if (SvFAKE(sv)) { |
3615 | char *pvx = SvPVX(sv); | |
3616 | STRLEN len = SvCUR(sv); | |
3617 | U32 hash = SvUVX(sv); | |
3618 | SvGROW(sv, len + 1); | |
3619 | Move(pvx,SvPVX(sv),len,char); | |
3620 | *SvEND(sv) = '\0'; | |
3621 | SvFAKE_off(sv); | |
3622 | SvREADONLY_off(sv); | |
c3654f1a | 3623 | unsharepvn(pvx,SvUTF8(sv)?-len:len,hash); |
1c846c1f NIS |
3624 | } |
3625 | else if (PL_curcop != &PL_compiling) | |
cea2e8a9 | 3626 | Perl_croak(aTHX_ PL_no_modify); |
0f15f207 | 3627 | } |
2213622d | 3628 | if (SvROK(sv)) |
840a7b70 | 3629 | sv_unref_flags(sv, flags); |
6fc92669 GS |
3630 | else if (SvFAKE(sv) && SvTYPE(sv) == SVt_PVGV) |
3631 | sv_unglob(sv); | |
0f15f207 | 3632 | } |
1c846c1f | 3633 | |
840a7b70 IZ |
3634 | void |
3635 | Perl_sv_force_normal(pTHX_ register SV *sv) | |
3636 | { | |
3637 | sv_force_normal_flags(sv, 0); | |
3638 | } | |
3639 | ||
954c1994 GS |
3640 | /* |
3641 | =for apidoc sv_chop | |
3642 | ||
1c846c1f | 3643 | Efficient removal of characters from the beginning of the string buffer. |
954c1994 GS |
3644 | SvPOK(sv) must be true and the C<ptr> must be a pointer to somewhere inside |
3645 | the string buffer. The C<ptr> becomes the first character of the adjusted | |
3646 | string. | |
3647 | ||
3648 | =cut | |
3649 | */ | |
3650 | ||
79072805 | 3651 | void |
864dbfa3 | 3652 | Perl_sv_chop(pTHX_ register SV *sv, register char *ptr) /* like set but assuming ptr is in sv */ |
1c846c1f NIS |
3653 | |
3654 | ||
79072805 LW |
3655 | { |
3656 | register STRLEN delta; | |
3657 | ||
a0d0e21e | 3658 | if (!ptr || !SvPOKp(sv)) |
79072805 | 3659 | return; |
2213622d | 3660 | SV_CHECK_THINKFIRST(sv); |
79072805 LW |
3661 | if (SvTYPE(sv) < SVt_PVIV) |
3662 | sv_upgrade(sv,SVt_PVIV); | |
3663 | ||
3664 | if (!SvOOK(sv)) { | |
50483b2c JD |
3665 | if (!SvLEN(sv)) { /* make copy of shared string */ |
3666 | char *pvx = SvPVX(sv); | |
3667 | STRLEN len = SvCUR(sv); | |
3668 | SvGROW(sv, len + 1); | |
3669 | Move(pvx,SvPVX(sv),len,char); | |
3670 | *SvEND(sv) = '\0'; | |
3671 | } | |
463ee0b2 | 3672 | SvIVX(sv) = 0; |
79072805 LW |
3673 | SvFLAGS(sv) |= SVf_OOK; |
3674 | } | |
25da4f38 | 3675 | SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVp_IOK|SVp_NOK|SVf_IVisUV); |
463ee0b2 | 3676 | delta = ptr - SvPVX(sv); |
79072805 LW |
3677 | SvLEN(sv) -= delta; |
3678 | SvCUR(sv) -= delta; | |
463ee0b2 LW |
3679 | SvPVX(sv) += delta; |
3680 | SvIVX(sv) += delta; | |
79072805 LW |
3681 | } |
3682 | ||
954c1994 GS |
3683 | /* |
3684 | =for apidoc sv_catpvn | |
3685 | ||
3686 | Concatenates the string onto the end of the string which is in the SV. The | |
3687 | C<len> indicates number of bytes to copy. Handles 'get' magic, but not | |
3688 | 'set' magic. See C<sv_catpvn_mg>. | |
3689 | ||
3690 | =cut | |
3691 | */ | |
3692 | ||
79072805 | 3693 | void |
864dbfa3 | 3694 | Perl_sv_catpvn(pTHX_ register SV *sv, register const char *ptr, register STRLEN len) |
79072805 | 3695 | { |
463ee0b2 | 3696 | STRLEN tlen; |
748a9306 | 3697 | char *junk; |
a0d0e21e | 3698 | |
748a9306 | 3699 | junk = SvPV_force(sv, tlen); |
463ee0b2 | 3700 | SvGROW(sv, tlen + len + 1); |
4633a7c4 LW |
3701 | if (ptr == junk) |
3702 | ptr = SvPVX(sv); | |
463ee0b2 | 3703 | Move(ptr,SvPVX(sv)+tlen,len,char); |
79072805 LW |
3704 | SvCUR(sv) += len; |
3705 | *SvEND(sv) = '\0'; | |
d41ff1b8 | 3706 | (void)SvPOK_only_UTF8(sv); /* validate pointer */ |
463ee0b2 | 3707 | SvTAINT(sv); |
79072805 LW |
3708 | } |
3709 | ||
954c1994 GS |
3710 | /* |
3711 | =for apidoc sv_catpvn_mg | |
3712 | ||
3713 | Like C<sv_catpvn>, but also handles 'set' magic. | |
3714 | ||
3715 | =cut | |
3716 | */ | |
3717 | ||
79072805 | 3718 | void |
864dbfa3 | 3719 | Perl_sv_catpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len) |
ef50df4b GS |
3720 | { |
3721 | sv_catpvn(sv,ptr,len); | |
3722 | SvSETMAGIC(sv); | |
3723 | } | |
3724 | ||
954c1994 GS |
3725 | /* |
3726 | =for apidoc sv_catsv | |
3727 | ||
3728 | Concatenates the string from SV C<ssv> onto the end of the string in SV | |
3729 | C<dsv>. Handles 'get' magic, but not 'set' magic. See C<sv_catsv_mg>. | |
3730 | ||
3731 | =cut | |
3732 | */ | |
3733 | ||
ef50df4b | 3734 | void |
864dbfa3 | 3735 | Perl_sv_catsv(pTHX_ SV *dstr, register SV *sstr) |
79072805 LW |
3736 | { |
3737 | char *s; | |
463ee0b2 | 3738 | STRLEN len; |
79072805 LW |
3739 | if (!sstr) |
3740 | return; | |
155aba94 | 3741 | if ((s = SvPV(sstr, len))) { |
e84ff256 | 3742 | if (DO_UTF8(sstr)) { |
560a288e | 3743 | sv_utf8_upgrade(dstr); |
e84ff256 | 3744 | sv_catpvn(dstr,s,len); |
560a288e | 3745 | SvUTF8_on(dstr); |
e84ff256 GS |
3746 | } |
3747 | else | |
3748 | sv_catpvn(dstr,s,len); | |
560a288e | 3749 | } |
79072805 LW |
3750 | } |
3751 | ||
954c1994 GS |
3752 | /* |
3753 | =for apidoc sv_catsv_mg | |
3754 | ||
3755 | Like C<sv_catsv>, but also handles 'set' magic. | |
3756 | ||
3757 | =cut | |
3758 | */ | |
3759 | ||
79072805 | 3760 | void |
864dbfa3 | 3761 | Perl_sv_catsv_mg(pTHX_ SV *dstr, register SV *sstr) |
ef50df4b GS |
3762 | { |
3763 | sv_catsv(dstr,sstr); | |
3764 | SvSETMAGIC(dstr); | |
3765 | } | |
3766 | ||
954c1994 GS |
3767 | /* |
3768 | =for apidoc sv_catpv | |
3769 | ||
3770 | Concatenates the string onto the end of the string which is in the SV. | |
3771 | Handles 'get' magic, but not 'set' magic. See C<sv_catpv_mg>. | |
3772 | ||
3773 | =cut | |
3774 | */ | |
3775 | ||
ef50df4b | 3776 | void |
864dbfa3 | 3777 | Perl_sv_catpv(pTHX_ register SV *sv, register const char *ptr) |
79072805 LW |
3778 | { |
3779 | register STRLEN len; | |
463ee0b2 | 3780 | STRLEN tlen; |
748a9306 | 3781 | char *junk; |
79072805 | 3782 | |
79072805 LW |
3783 | if (!ptr) |
3784 | return; | |
748a9306 | 3785 | junk = SvPV_force(sv, tlen); |
79072805 | 3786 | len = strlen(ptr); |
463ee0b2 | 3787 | SvGROW(sv, tlen + len + 1); |
4633a7c4 LW |
3788 | if (ptr == junk) |
3789 | ptr = SvPVX(sv); | |
463ee0b2 | 3790 | Move(ptr,SvPVX(sv)+tlen,len+1,char); |
79072805 | 3791 | SvCUR(sv) += len; |
d41ff1b8 | 3792 | (void)SvPOK_only_UTF8(sv); /* validate pointer */ |
463ee0b2 | 3793 | SvTAINT(sv); |
79072805 LW |
3794 | } |
3795 | ||
954c1994 GS |
3796 | /* |
3797 | =for apidoc sv_catpv_mg | |
3798 | ||
3799 | Like C<sv_catpv>, but also handles 'set' magic. | |
3800 | ||
3801 | =cut | |
3802 | */ | |
3803 | ||
ef50df4b | 3804 | void |
864dbfa3 | 3805 | Perl_sv_catpv_mg(pTHX_ register SV *sv, register const char *ptr) |
ef50df4b | 3806 | { |
51c1089b | 3807 | sv_catpv(sv,ptr); |
ef50df4b GS |
3808 | SvSETMAGIC(sv); |
3809 | } | |
3810 | ||
79072805 | 3811 | SV * |
864dbfa3 | 3812 | Perl_newSV(pTHX_ STRLEN len) |
79072805 LW |
3813 | { |
3814 | register SV *sv; | |
1c846c1f | 3815 | |
4561caa4 | 3816 | new_SV(sv); |
79072805 LW |
3817 | if (len) { |
3818 | sv_upgrade(sv, SVt_PV); | |
3819 | SvGROW(sv, len + 1); | |
3820 | } | |
3821 | return sv; | |
3822 | } | |
3823 | ||
1edc1566 | 3824 | /* name is assumed to contain an SV* if (name && namelen == HEf_SVKEY) */ |
3825 | ||
954c1994 GS |
3826 | /* |
3827 | =for apidoc sv_magic | |
3828 | ||
3829 | Adds magic to an SV. | |
3830 | ||
3831 | =cut | |
3832 | */ | |
3833 | ||
79072805 | 3834 | void |
864dbfa3 | 3835 | Perl_sv_magic(pTHX_ register SV *sv, SV *obj, int how, const char *name, I32 namlen) |
79072805 LW |
3836 | { |
3837 | MAGIC* mg; | |
1c846c1f | 3838 | |
0f15f207 | 3839 | if (SvREADONLY(sv)) { |
3280af22 | 3840 | if (PL_curcop != &PL_compiling && !strchr("gBf", how)) |
cea2e8a9 | 3841 | Perl_croak(aTHX_ PL_no_modify); |
0f15f207 | 3842 | } |
4633a7c4 | 3843 | if (SvMAGICAL(sv) || (how == 't' && SvTYPE(sv) >= SVt_PVMG)) { |
748a9306 LW |
3844 | if (SvMAGIC(sv) && (mg = mg_find(sv, how))) { |