This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix [perl #35929] : PerlIO::scalar didn't understand $/ = ""
[perl5.git] / sv.c
CommitLineData
a0d0e21e 1/* sv.c
79072805 2 *
4bb101f2 3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
241d1a3b 4 * 2000, 2001, 2002, 2003, 2004, 2005, by Larry Wall and others
79072805
LW
5 *
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
8 *
a0d0e21e 9 * "I wonder what the Entish is for 'yes' and 'no'," he thought.
645c22ef
DM
10 *
11 *
5e045b90
AMS
12 * This file contains the code that creates, manipulates and destroys
13 * scalar values (SVs). The other types (AV, HV, GV, etc.) reuse the
14 * structure of an SV, so their creation and destruction is handled
15 * here; higher-level functions are in av.c, hv.c, and so on. Opcode
16 * level functions (eg. substr, split, join) for each of the types are
17 * in the pp*.c files.
79072805
LW
18 */
19
20#include "EXTERN.h"
864dbfa3 21#define PERL_IN_SV_C
79072805 22#include "perl.h"
d2f185dc 23#include "regcomp.h"
79072805 24
51371543 25#define FCALL *f
2c5424a7 26
2f8ed50e
OS
27#ifdef __Lynx__
28/* Missing proto on LynxOS */
29 char *gconvert(double, int, int, char *);
30#endif
31
e23c8137
JH
32#ifdef PERL_UTF8_CACHE_ASSERT
33/* The cache element 0 is the Unicode offset;
34 * the cache element 1 is the byte offset of the element 0;
35 * the cache element 2 is the Unicode length of the substring;
36 * the cache element 3 is the byte length of the substring;
37 * The checking of the substring side would be good
38 * but substr() has enough code paths to make my head spin;
39 * if adding more checks watch out for the following tests:
40 * t/op/index.t t/op/length.t t/op/pat.t t/op/substr.t
41 * lib/utf8.t lib/Unicode/Collate/t/index.t
42 * --jhi
43 */
44#define ASSERT_UTF8_CACHE(cache) \
45 STMT_START { if (cache) { assert((cache)[0] <= (cache)[1]); } } STMT_END
46#else
47#define ASSERT_UTF8_CACHE(cache) NOOP
48#endif
49
765f542d
NC
50#ifdef PERL_COPY_ON_WRITE
51#define SV_COW_NEXT_SV(sv) INT2PTR(SV *,SvUVX(sv))
607fa7f2 52#define SV_COW_NEXT_SV_SET(current,next) SvUV_set(current, PTR2UV(next))
b5ccf5f2 53/* This is a pessimistic view. Scalar must be purely a read-write PV to copy-
765f542d 54 on-write. */
765f542d 55#endif
645c22ef
DM
56
57/* ============================================================================
58
59=head1 Allocation and deallocation of SVs.
60
5e045b90
AMS
61An SV (or AV, HV, etc.) is allocated in two parts: the head (struct sv,
62av, hv...) contains type and reference count information, as well as a
63pointer to the body (struct xrv, xpv, xpviv...), which contains fields
64specific to each type.
65
4977e971
NC
66Normally, this allocation is done using arenas, which by default are
67approximately 4K chunks of memory parcelled up into N heads or bodies. The
68first slot in each arena is reserved, and is used to hold a link to the next
69arena. In the case of heads, the unused first slot also contains some flags
70and a note of the number of slots. Snaked through each arena chain is a
5e045b90 71linked list of free items; when this becomes empty, an extra arena is
4977e971 72allocated and divided up into N items which are threaded into the free list.
645c22ef
DM
73
74The following global variables are associated with arenas:
75
76 PL_sv_arenaroot pointer to list of SV arenas
77 PL_sv_root pointer to list of free SV structures
78
79 PL_foo_arenaroot pointer to list of foo arenas,
80 PL_foo_root pointer to list of free foo bodies
81 ... for foo in xiv, xnv, xrv, xpv etc.
82
83Note that some of the larger and more rarely used body types (eg xpvio)
84are not allocated using arenas, but are instead just malloc()/free()ed as
85required. Also, if PURIFY is defined, arenas are abandoned altogether,
86with all items individually malloc()ed. In addition, a few SV heads are
87not allocated from an arena, but are instead directly created as static
4977e971
NC
88or auto variables, eg PL_sv_undef. The size of arenas can be changed from
89the default by setting PERL_ARENA_SIZE appropriately at compile time.
645c22ef
DM
90
91The SV arena serves the secondary purpose of allowing still-live SVs
92to be located and destroyed during final cleanup.
93
94At the lowest level, the macros new_SV() and del_SV() grab and free
95an SV head. (If debugging with -DD, del_SV() calls the function S_del_sv()
96to return the SV to the free list with error checking.) new_SV() calls
97more_sv() / sv_add_arena() to add an extra arena if the free list is empty.
98SVs in the free list have their SvTYPE field set to all ones.
99
100Similarly, there are macros new_XIV()/del_XIV(), new_XNV()/del_XNV() etc
101that allocate and return individual body types. Normally these are mapped
ff276b08
RG
102to the arena-manipulating functions new_xiv()/del_xiv() etc, but may be
103instead mapped directly to malloc()/free() if PURIFY is defined. The
645c22ef
DM
104new/del functions remove from, or add to, the appropriate PL_foo_root
105list, and call more_xiv() etc to add a new arena if the list is empty.
106
ff276b08 107At the time of very final cleanup, sv_free_arenas() is called from
645c22ef
DM
108perl_destruct() to physically free all the arenas allocated since the
109start of the interpreter. Note that this also clears PL_he_arenaroot,
110which is otherwise dealt with in hv.c.
111
112Manipulation of any of the PL_*root pointers is protected by enclosing
113LOCK_SV_MUTEX; ... UNLOCK_SV_MUTEX calls which should Do the Right Thing
114if threads are enabled.
115
116The function visit() scans the SV arenas list, and calls a specified
117function for each SV it finds which is still live - ie which has an SvTYPE
118other than all 1's, and a non-zero SvREFCNT. visit() is used by the
119following functions (specified as [function that calls visit()] / [function
120called by visit() for each SV]):
121
122 sv_report_used() / do_report_used()
123 dump all remaining SVs (debugging aid)
124
125 sv_clean_objs() / do_clean_objs(),do_clean_named_objs()
126 Attempt to free all objects pointed to by RVs,
127 and, unless DISABLE_DESTRUCTOR_KLUDGE is defined,
128 try to do the same for all objects indirectly
129 referenced by typeglobs too. Called once from
130 perl_destruct(), prior to calling sv_clean_all()
131 below.
132
133 sv_clean_all() / do_clean_all()
134 SvREFCNT_dec(sv) each remaining SV, possibly
135 triggering an sv_free(). It also sets the
136 SVf_BREAK flag on the SV to indicate that the
137 refcnt has been artificially lowered, and thus
138 stopping sv_free() from giving spurious warnings
139 about SVs which unexpectedly have a refcnt
140 of zero. called repeatedly from perl_destruct()
141 until there are no SVs left.
142
143=head2 Summary
144
145Private API to rest of sv.c
146
147 new_SV(), del_SV(),
148
149 new_XIV(), del_XIV(),
150 new_XNV(), del_XNV(),
151 etc
152
153Public API:
154
8cf8f3d1 155 sv_report_used(), sv_clean_objs(), sv_clean_all(), sv_free_arenas()
645c22ef
DM
156
157
158=cut
159
160============================================================================ */
161
162
51371543 163
4561caa4
CS
164/*
165 * "A time to plant, and a time to uproot what was planted..."
166 */
167
cac9b346 168
fd0854ff
DM
169#ifdef DEBUG_LEAKING_SCALARS
170# ifdef NETWARE
171# define FREE_SV_DEBUG_FILE(sv) PerlMemfree((sv)->sv_debug_file)
172# else
173# define FREE_SV_DEBUG_FILE(sv) PerlMemShared_free((sv)->sv_debug_file)
174# endif
175#else
176# define FREE_SV_DEBUG_FILE(sv)
177#endif
178
053fc874
GS
179#define plant_SV(p) \
180 STMT_START { \
fd0854ff 181 FREE_SV_DEBUG_FILE(p); \
053fc874
GS
182 SvANY(p) = (void *)PL_sv_root; \
183 SvFLAGS(p) = SVTYPEMASK; \
184 PL_sv_root = (p); \
185 --PL_sv_count; \
186 } STMT_END
a0d0e21e 187
fba3b22e 188/* sv_mutex must be held while calling uproot_SV() */
053fc874
GS
189#define uproot_SV(p) \
190 STMT_START { \
191 (p) = PL_sv_root; \
192 PL_sv_root = (SV*)SvANY(p); \
193 ++PL_sv_count; \
194 } STMT_END
195
645c22ef 196
cac9b346
NC
197/* make some more SVs by adding another arena */
198
199/* sv_mutex must be held while calling more_sv() */
200STATIC SV*
201S_more_sv(pTHX)
202{
203 SV* sv;
204
205 if (PL_nice_chunk) {
206 sv_add_arena(PL_nice_chunk, PL_nice_chunk_size, 0);
207 PL_nice_chunk = Nullch;
208 PL_nice_chunk_size = 0;
209 }
210 else {
211 char *chunk; /* must use New here to match call to */
2e7ed132
NC
212 New(704,chunk,PERL_ARENA_SIZE,char); /* Safefree() in sv_free_arenas() */
213 sv_add_arena(chunk, PERL_ARENA_SIZE, 0);
cac9b346
NC
214 }
215 uproot_SV(sv);
216 return sv;
217}
218
645c22ef
DM
219/* new_SV(): return a new, empty SV head */
220
eba0f806
DM
221#ifdef DEBUG_LEAKING_SCALARS
222/* provide a real function for a debugger to play with */
223STATIC SV*
224S_new_SV(pTHX)
225{
226 SV* sv;
227
228 LOCK_SV_MUTEX;
229 if (PL_sv_root)
230 uproot_SV(sv);
231 else
cac9b346 232 sv = S_more_sv(aTHX);
eba0f806
DM
233 UNLOCK_SV_MUTEX;
234 SvANY(sv) = 0;
235 SvREFCNT(sv) = 1;
236 SvFLAGS(sv) = 0;
fd0854ff
DM
237 sv->sv_debug_optype = PL_op ? PL_op->op_type : 0;
238 sv->sv_debug_line = (U16) ((PL_copline == NOLINE) ?
239 (PL_curcop ? CopLINE(PL_curcop) : 0) : PL_copline);
240 sv->sv_debug_inpad = 0;
241 sv->sv_debug_cloned = 0;
242# ifdef NETWARE
243 sv->sv_debug_file = PL_curcop ? savepv(CopFILE(PL_curcop)): NULL;
244# else
245 sv->sv_debug_file = PL_curcop ? savesharedpv(CopFILE(PL_curcop)): NULL;
246# endif
247
eba0f806
DM
248 return sv;
249}
250# define new_SV(p) (p)=S_new_SV(aTHX)
251
252#else
253# define new_SV(p) \
053fc874
GS
254 STMT_START { \
255 LOCK_SV_MUTEX; \
256 if (PL_sv_root) \
257 uproot_SV(p); \
258 else \
cac9b346 259 (p) = S_more_sv(aTHX); \
053fc874
GS
260 UNLOCK_SV_MUTEX; \
261 SvANY(p) = 0; \
262 SvREFCNT(p) = 1; \
263 SvFLAGS(p) = 0; \
264 } STMT_END
eba0f806 265#endif
463ee0b2 266
645c22ef
DM
267
268/* del_SV(): return an empty SV head to the free list */
269
a0d0e21e 270#ifdef DEBUGGING
4561caa4 271
053fc874
GS
272#define del_SV(p) \
273 STMT_START { \
274 LOCK_SV_MUTEX; \
aea4f609 275 if (DEBUG_D_TEST) \
053fc874
GS
276 del_sv(p); \
277 else \
278 plant_SV(p); \
279 UNLOCK_SV_MUTEX; \
280 } STMT_END
a0d0e21e 281
76e3520e 282STATIC void
cea2e8a9 283S_del_sv(pTHX_ SV *p)
463ee0b2 284{
aea4f609 285 if (DEBUG_D_TEST) {
4633a7c4 286 SV* sva;
a3b680e6 287 bool ok = 0;
3280af22 288 for (sva = PL_sv_arenaroot; sva; sva = (SV *) SvANY(sva)) {
a3b680e6
AL
289 SV *sv = sva + 1;
290 SV *svend = &sva[SvREFCNT(sva)];
c0ff570e 291 if (p >= sv && p < svend) {
a0d0e21e 292 ok = 1;
c0ff570e
NC
293 break;
294 }
a0d0e21e
LW
295 }
296 if (!ok) {
0453d815 297 if (ckWARN_d(WARN_INTERNAL))
9014280d 298 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
472d47bc
SB
299 "Attempt to free non-arena SV: 0x%"UVxf
300 pTHX__FORMAT, PTR2UV(p) pTHX__VALUE);
a0d0e21e
LW
301 return;
302 }
303 }
4561caa4 304 plant_SV(p);
463ee0b2 305}
a0d0e21e 306
4561caa4
CS
307#else /* ! DEBUGGING */
308
309#define del_SV(p) plant_SV(p)
310
311#endif /* DEBUGGING */
463ee0b2 312
645c22ef
DM
313
314/*
ccfc67b7
JH
315=head1 SV Manipulation Functions
316
645c22ef
DM
317=for apidoc sv_add_arena
318
319Given a chunk of memory, link it to the head of the list of arenas,
320and split it into a list of free SVs.
321
322=cut
323*/
324
4633a7c4 325void
864dbfa3 326Perl_sv_add_arena(pTHX_ char *ptr, U32 size, U32 flags)
463ee0b2 327{
4633a7c4 328 SV* sva = (SV*)ptr;
463ee0b2
LW
329 register SV* sv;
330 register SV* svend;
4633a7c4
LW
331
332 /* The first SV in an arena isn't an SV. */
3280af22 333 SvANY(sva) = (void *) PL_sv_arenaroot; /* ptr to next arena */
4633a7c4
LW
334 SvREFCNT(sva) = size / sizeof(SV); /* number of SV slots */
335 SvFLAGS(sva) = flags; /* FAKE if not to be freed */
336
3280af22
NIS
337 PL_sv_arenaroot = sva;
338 PL_sv_root = sva + 1;
4633a7c4
LW
339
340 svend = &sva[SvREFCNT(sva) - 1];
341 sv = sva + 1;
463ee0b2 342 while (sv < svend) {
a0d0e21e 343 SvANY(sv) = (void *)(SV*)(sv + 1);
03e36789 344#ifdef DEBUGGING
978b032e 345 SvREFCNT(sv) = 0;
03e36789
NC
346#endif
347 /* Must always set typemask because it's awlays checked in on cleanup
348 when the arenas are walked looking for objects. */
8990e307 349 SvFLAGS(sv) = SVTYPEMASK;
463ee0b2
LW
350 sv++;
351 }
352 SvANY(sv) = 0;
03e36789
NC
353#ifdef DEBUGGING
354 SvREFCNT(sv) = 0;
355#endif
4633a7c4
LW
356 SvFLAGS(sv) = SVTYPEMASK;
357}
358
055972dc
DM
359/* visit(): call the named function for each non-free SV in the arenas
360 * whose flags field matches the flags/mask args. */
645c22ef 361
5226ed68 362STATIC I32
055972dc 363S_visit(pTHX_ SVFUNC_t f, U32 flags, U32 mask)
8990e307 364{
4633a7c4 365 SV* sva;
5226ed68 366 I32 visited = 0;
8990e307 367
3280af22 368 for (sva = PL_sv_arenaroot; sva; sva = (SV*)SvANY(sva)) {
a3b680e6
AL
369 register SV * const svend = &sva[SvREFCNT(sva)];
370 register SV* sv;
4561caa4 371 for (sv = sva + 1; sv < svend; ++sv) {
055972dc
DM
372 if (SvTYPE(sv) != SVTYPEMASK
373 && (sv->sv_flags & mask) == flags
374 && SvREFCNT(sv))
375 {
acfe0abc 376 (FCALL)(aTHX_ sv);
5226ed68
JH
377 ++visited;
378 }
8990e307
LW
379 }
380 }
5226ed68 381 return visited;
8990e307
LW
382}
383
758a08c3
JH
384#ifdef DEBUGGING
385
645c22ef
DM
386/* called by sv_report_used() for each live SV */
387
388static void
acfe0abc 389do_report_used(pTHX_ SV *sv)
645c22ef
DM
390{
391 if (SvTYPE(sv) != SVTYPEMASK) {
392 PerlIO_printf(Perl_debug_log, "****\n");
393 sv_dump(sv);
394 }
395}
758a08c3 396#endif
645c22ef
DM
397
398/*
399=for apidoc sv_report_used
400
401Dump the contents of all SVs not yet freed. (Debugging aid).
402
403=cut
404*/
405
8990e307 406void
864dbfa3 407Perl_sv_report_used(pTHX)
4561caa4 408{
ff270d3a 409#ifdef DEBUGGING
055972dc 410 visit(do_report_used, 0, 0);
ff270d3a 411#endif
4561caa4
CS
412}
413
645c22ef
DM
414/* called by sv_clean_objs() for each live SV */
415
416static void
acfe0abc 417do_clean_objs(pTHX_ SV *sv)
645c22ef
DM
418{
419 SV* rv;
420
421 if (SvROK(sv) && SvOBJECT(rv = SvRV(sv))) {
422 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning object ref:\n "), sv_dump(sv)));
423 if (SvWEAKREF(sv)) {
424 sv_del_backref(sv);
425 SvWEAKREF_off(sv);
b162af07 426 SvRV_set(sv, NULL);
645c22ef
DM
427 } else {
428 SvROK_off(sv);
b162af07 429 SvRV_set(sv, NULL);
645c22ef
DM
430 SvREFCNT_dec(rv);
431 }
432 }
433
434 /* XXX Might want to check arrays, etc. */
435}
436
437/* called by sv_clean_objs() for each live SV */
438
439#ifndef DISABLE_DESTRUCTOR_KLUDGE
440static void
acfe0abc 441do_clean_named_objs(pTHX_ SV *sv)
645c22ef
DM
442{
443 if (SvTYPE(sv) == SVt_PVGV && GvGP(sv)) {
444 if ( SvOBJECT(GvSV(sv)) ||
445 (GvAV(sv) && SvOBJECT(GvAV(sv))) ||
446 (GvHV(sv) && SvOBJECT(GvHV(sv))) ||
447 (GvIO(sv) && SvOBJECT(GvIO(sv))) ||
448 (GvCV(sv) && SvOBJECT(GvCV(sv))) )
449 {
450 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning named glob object:\n "), sv_dump(sv)));
ec5f3c78 451 SvFLAGS(sv) |= SVf_BREAK;
645c22ef
DM
452 SvREFCNT_dec(sv);
453 }
454 }
455}
456#endif
457
458/*
459=for apidoc sv_clean_objs
460
461Attempt to destroy all objects not yet freed
462
463=cut
464*/
465
4561caa4 466void
864dbfa3 467Perl_sv_clean_objs(pTHX)
4561caa4 468{
3280af22 469 PL_in_clean_objs = TRUE;
055972dc 470 visit(do_clean_objs, SVf_ROK, SVf_ROK);
4561caa4 471#ifndef DISABLE_DESTRUCTOR_KLUDGE
2d0f3c12 472 /* some barnacles may yet remain, clinging to typeglobs */
055972dc 473 visit(do_clean_named_objs, SVt_PVGV, SVTYPEMASK);
4561caa4 474#endif
3280af22 475 PL_in_clean_objs = FALSE;
4561caa4
CS
476}
477
645c22ef
DM
478/* called by sv_clean_all() for each live SV */
479
480static void
acfe0abc 481do_clean_all(pTHX_ SV *sv)
645c22ef
DM
482{
483 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning loops: SV at 0x%"UVxf"\n", PTR2UV(sv)) ));
484 SvFLAGS(sv) |= SVf_BREAK;
0e705b3b
DM
485 if (PL_comppad == (AV*)sv) {
486 PL_comppad = Nullav;
487 PL_curpad = Null(SV**);
488 }
645c22ef
DM
489 SvREFCNT_dec(sv);
490}
491
492/*
493=for apidoc sv_clean_all
494
495Decrement the refcnt of each remaining SV, possibly triggering a
496cleanup. This function may have to be called multiple times to free
ff276b08 497SVs which are in complex self-referential hierarchies.
645c22ef
DM
498
499=cut
500*/
501
5226ed68 502I32
864dbfa3 503Perl_sv_clean_all(pTHX)
8990e307 504{
5226ed68 505 I32 cleaned;
3280af22 506 PL_in_clean_all = TRUE;
055972dc 507 cleaned = visit(do_clean_all, 0,0);
3280af22 508 PL_in_clean_all = FALSE;
5226ed68 509 return cleaned;
8990e307 510}
463ee0b2 511
645c22ef
DM
512/*
513=for apidoc sv_free_arenas
514
515Deallocate the memory used by all arenas. Note that all the individual SV
516heads and bodies within the arenas must already have been freed.
517
518=cut
519*/
520
4633a7c4 521void
864dbfa3 522Perl_sv_free_arenas(pTHX)
4633a7c4
LW
523{
524 SV* sva;
525 SV* svanext;
7b2c381c 526 void *arena, *arenanext;
4633a7c4
LW
527
528 /* Free arenas here, but be careful about fake ones. (We assume
529 contiguity of the fake ones with the corresponding real ones.) */
530
3280af22 531 for (sva = PL_sv_arenaroot; sva; sva = svanext) {
4633a7c4
LW
532 svanext = (SV*) SvANY(sva);
533 while (svanext && SvFAKE(svanext))
534 svanext = (SV*) SvANY(svanext);
535
536 if (!SvFAKE(sva))
1edc1566 537 Safefree((void *)sva);
4633a7c4 538 }
5f05dabc 539
612f20c3 540 for (arena = PL_xnv_arenaroot; arena; arena = arenanext) {
7b2c381c 541 arenanext = *(void **)arena;
612f20c3
GS
542 Safefree(arena);
543 }
544 PL_xnv_arenaroot = 0;
bf9cdc68 545 PL_xnv_root = 0;
612f20c3 546
612f20c3 547 for (arena = PL_xpv_arenaroot; arena; arena = arenanext) {
7b2c381c 548 arenanext = *(void **)arena;
612f20c3
GS
549 Safefree(arena);
550 }
551 PL_xpv_arenaroot = 0;
bf9cdc68 552 PL_xpv_root = 0;
612f20c3 553
7b2c381c
NC
554 for (arena = PL_xpviv_arenaroot; arena; arena = arenanext) {
555 arenanext = *(void **)arena;
612f20c3
GS
556 Safefree(arena);
557 }
558 PL_xpviv_arenaroot = 0;
bf9cdc68 559 PL_xpviv_root = 0;
612f20c3 560
7b2c381c
NC
561 for (arena = PL_xpvnv_arenaroot; arena; arena = arenanext) {
562 arenanext = *(void **)arena;
612f20c3
GS
563 Safefree(arena);
564 }
565 PL_xpvnv_arenaroot = 0;
bf9cdc68 566 PL_xpvnv_root = 0;
612f20c3 567
7b2c381c
NC
568 for (arena = PL_xpvcv_arenaroot; arena; arena = arenanext) {
569 arenanext = *(void **)arena;
612f20c3
GS
570 Safefree(arena);
571 }
572 PL_xpvcv_arenaroot = 0;
bf9cdc68 573 PL_xpvcv_root = 0;
612f20c3 574
7b2c381c
NC
575 for (arena = PL_xpvav_arenaroot; arena; arena = arenanext) {
576 arenanext = *(void **)arena;
612f20c3
GS
577 Safefree(arena);
578 }
579 PL_xpvav_arenaroot = 0;
bf9cdc68 580 PL_xpvav_root = 0;
612f20c3 581
7b2c381c
NC
582 for (arena = PL_xpvhv_arenaroot; arena; arena = arenanext) {
583 arenanext = *(void **)arena;
612f20c3
GS
584 Safefree(arena);
585 }
586 PL_xpvhv_arenaroot = 0;
bf9cdc68 587 PL_xpvhv_root = 0;
612f20c3 588
7b2c381c
NC
589 for (arena = PL_xpvmg_arenaroot; arena; arena = arenanext) {
590 arenanext = *(void **)arena;
612f20c3
GS
591 Safefree(arena);
592 }
593 PL_xpvmg_arenaroot = 0;
bf9cdc68 594 PL_xpvmg_root = 0;
612f20c3 595
7b2c381c
NC
596 for (arena = PL_xpvgv_arenaroot; arena; arena = arenanext) {
597 arenanext = *(void **)arena;
727879eb
NC
598 Safefree(arena);
599 }
600 PL_xpvgv_arenaroot = 0;
601 PL_xpvgv_root = 0;
602
7b2c381c
NC
603 for (arena = PL_xpvlv_arenaroot; arena; arena = arenanext) {
604 arenanext = *(void **)arena;
612f20c3
GS
605 Safefree(arena);
606 }
607 PL_xpvlv_arenaroot = 0;
bf9cdc68 608 PL_xpvlv_root = 0;
612f20c3 609
7b2c381c
NC
610 for (arena = PL_xpvbm_arenaroot; arena; arena = arenanext) {
611 arenanext = *(void **)arena;
612f20c3
GS
612 Safefree(arena);
613 }
614 PL_xpvbm_arenaroot = 0;
bf9cdc68 615 PL_xpvbm_root = 0;
612f20c3 616
b1135e3d
NC
617 {
618 HE *he;
619 HE *he_next;
620 for (he = PL_he_arenaroot; he; he = he_next) {
621 he_next = HeNEXT(he);
622 Safefree(he);
623 }
612f20c3
GS
624 }
625 PL_he_arenaroot = 0;
bf9cdc68 626 PL_he_root = 0;
612f20c3 627
892b45be 628#if defined(USE_ITHREADS)
b1135e3d
NC
629 {
630 struct ptr_tbl_ent *pte;
631 struct ptr_tbl_ent *pte_next;
632 for (pte = PL_pte_arenaroot; pte; pte = pte_next) {
633 pte_next = pte->next;
634 Safefree(pte);
635 }
32e691d0
NC
636 }
637 PL_pte_arenaroot = 0;
638 PL_pte_root = 0;
892b45be 639#endif
32e691d0 640
3280af22
NIS
641 if (PL_nice_chunk)
642 Safefree(PL_nice_chunk);
643 PL_nice_chunk = Nullch;
644 PL_nice_chunk_size = 0;
645 PL_sv_arenaroot = 0;
646 PL_sv_root = 0;
4633a7c4
LW
647}
648
29489e7c
DM
649/* ---------------------------------------------------------------------
650 *
651 * support functions for report_uninit()
652 */
653
654/* the maxiumum size of array or hash where we will scan looking
655 * for the undefined element that triggered the warning */
656
657#define FUV_MAX_SEARCH_SIZE 1000
658
659/* Look for an entry in the hash whose value has the same SV as val;
660 * If so, return a mortal copy of the key. */
661
662STATIC SV*
663S_find_hash_subscript(pTHX_ HV *hv, SV* val)
664{
27da23d5 665 dVAR;
29489e7c 666 register HE **array;
29489e7c
DM
667 I32 i;
668
669 if (!hv || SvMAGICAL(hv) || !HvARRAY(hv) ||
670 (HvTOTALKEYS(hv) > FUV_MAX_SEARCH_SIZE))
671 return Nullsv;
672
673 array = HvARRAY(hv);
674
675 for (i=HvMAX(hv); i>0; i--) {
f54cb97a 676 register HE *entry;
29489e7c
DM
677 for (entry = array[i]; entry; entry = HeNEXT(entry)) {
678 if (HeVAL(entry) != val)
679 continue;
680 if ( HeVAL(entry) == &PL_sv_undef ||
681 HeVAL(entry) == &PL_sv_placeholder)
682 continue;
683 if (!HeKEY(entry))
684 return Nullsv;
685 if (HeKLEN(entry) == HEf_SVKEY)
686 return sv_mortalcopy(HeKEY_sv(entry));
687 return sv_2mortal(newSVpvn(HeKEY(entry), HeKLEN(entry)));
688 }
689 }
690 return Nullsv;
691}
692
693/* Look for an entry in the array whose value has the same SV as val;
694 * If so, return the index, otherwise return -1. */
695
696STATIC I32
697S_find_array_subscript(pTHX_ AV *av, SV* val)
698{
699 SV** svp;
700 I32 i;
701 if (!av || SvMAGICAL(av) || !AvARRAY(av) ||
702 (AvFILLp(av) > FUV_MAX_SEARCH_SIZE))
703 return -1;
704
705 svp = AvARRAY(av);
706 for (i=AvFILLp(av); i>=0; i--) {
707 if (svp[i] == val && svp[i] != &PL_sv_undef)
708 return i;
709 }
710 return -1;
711}
712
713/* S_varname(): return the name of a variable, optionally with a subscript.
714 * If gv is non-zero, use the name of that global, along with gvtype (one
715 * of "$", "@", "%"); otherwise use the name of the lexical at pad offset
716 * targ. Depending on the value of the subscript_type flag, return:
717 */
718
719#define FUV_SUBSCRIPT_NONE 1 /* "@foo" */
720#define FUV_SUBSCRIPT_ARRAY 2 /* "$foo[aindex]" */
721#define FUV_SUBSCRIPT_HASH 3 /* "$foo{keyname}" */
722#define FUV_SUBSCRIPT_WITHIN 4 /* "within @foo" */
723
724STATIC SV*
bfed75c6 725S_varname(pTHX_ GV *gv, const char *gvtype, PADOFFSET targ,
29489e7c
DM
726 SV* keyname, I32 aindex, int subscript_type)
727{
728 AV *av;
a3b680e6 729 SV *sv;
29489e7c 730
a3b680e6 731 SV * const name = sv_newmortal();
29489e7c
DM
732 if (gv) {
733
734 /* simulate gv_fullname4(), but add literal '^' for $^FOO names
735 * XXX get rid of all this if gv_fullnameX() ever supports this
736 * directly */
737
bfed75c6 738 const char *p;
29489e7c
DM
739 HV *hv = GvSTASH(gv);
740 sv_setpv(name, gvtype);
741 if (!hv)
742 p = "???";
bfcb3514 743 else if (!(p=HvNAME_get(hv)))
29489e7c 744 p = "__ANON__";
29489e7c
DM
745 if (strNE(p, "main")) {
746 sv_catpv(name,p);
747 sv_catpvn(name,"::", 2);
748 }
749 if (GvNAMELEN(gv)>= 1 &&
750 ((unsigned int)*GvNAME(gv)) <= 26)
751 { /* handle $^FOO */
752 Perl_sv_catpvf(aTHX_ name,"^%c", *GvNAME(gv) + 'A' - 1);
753 sv_catpvn(name,GvNAME(gv)+1,GvNAMELEN(gv)-1);
754 }
755 else
756 sv_catpvn(name,GvNAME(gv),GvNAMELEN(gv));
757 }
758 else {
759 U32 u;
760 CV *cv = find_runcv(&u);
761 if (!cv || !CvPADLIST(cv))
762 return Nullsv;;
763 av = (AV*)(*av_fetch(CvPADLIST(cv), 0, FALSE));
764 sv = *av_fetch(av, targ, FALSE);
765 /* SvLEN in a pad name is not to be trusted */
766 sv_setpv(name, SvPV_nolen(sv));
767 }
768
769 if (subscript_type == FUV_SUBSCRIPT_HASH) {
770 *SvPVX(name) = '$';
771 sv = NEWSV(0,0);
772 Perl_sv_catpvf(aTHX_ name, "{%s}",
773 pv_display(sv,SvPVX(keyname), SvCUR(keyname), 0, 32));
774 SvREFCNT_dec(sv);
775 }
776 else if (subscript_type == FUV_SUBSCRIPT_ARRAY) {
777 *SvPVX(name) = '$';
265a12b8 778 Perl_sv_catpvf(aTHX_ name, "[%"IVdf"]", (IV)aindex);
29489e7c
DM
779 }
780 else if (subscript_type == FUV_SUBSCRIPT_WITHIN)
781 sv_insert(name, 0, 0, "within ", 7);
782
783 return name;
784}
785
786
787/*
788=for apidoc find_uninit_var
789
790Find the name of the undefined variable (if any) that caused the operator o
791to issue a "Use of uninitialized value" warning.
792If match is true, only return a name if it's value matches uninit_sv.
793So roughly speaking, if a unary operator (such as OP_COS) generates a
794warning, then following the direct child of the op may yield an
795OP_PADSV or OP_GV that gives the name of the undefined variable. On the
796other hand, with OP_ADD there are two branches to follow, so we only print
797the variable name if we get an exact match.
798
799The name is returned as a mortal SV.
800
801Assumes that PL_op is the op that originally triggered the error, and that
802PL_comppad/PL_curpad points to the currently executing pad.
803
804=cut
805*/
806
807STATIC SV *
808S_find_uninit_var(pTHX_ OP* obase, SV* uninit_sv, bool match)
809{
27da23d5 810 dVAR;
29489e7c
DM
811 SV *sv;
812 AV *av;
813 SV **svp;
814 GV *gv;
815 OP *o, *o2, *kid;
816
817 if (!obase || (match && (!uninit_sv || uninit_sv == &PL_sv_undef ||
818 uninit_sv == &PL_sv_placeholder)))
819 return Nullsv;
820
821 switch (obase->op_type) {
822
823 case OP_RV2AV:
824 case OP_RV2HV:
825 case OP_PADAV:
826 case OP_PADHV:
827 {
f54cb97a
AL
828 const bool pad = (obase->op_type == OP_PADAV || obase->op_type == OP_PADHV);
829 const bool hash = (obase->op_type == OP_PADHV || obase->op_type == OP_RV2HV);
112dcc46
RGS
830 I32 index = 0;
831 SV *keysv = Nullsv;
29489e7c
DM
832 int subscript_type = FUV_SUBSCRIPT_WITHIN;
833
834 if (pad) { /* @lex, %lex */
835 sv = PAD_SVl(obase->op_targ);
836 gv = Nullgv;
837 }
838 else {
839 if (cUNOPx(obase)->op_first->op_type == OP_GV) {
840 /* @global, %global */
841 gv = cGVOPx_gv(cUNOPx(obase)->op_first);
842 if (!gv)
843 break;
844 sv = hash ? (SV*)GvHV(gv): (SV*)GvAV(gv);
845 }
846 else /* @{expr}, %{expr} */
847 return find_uninit_var(cUNOPx(obase)->op_first,
848 uninit_sv, match);
849 }
850
851 /* attempt to find a match within the aggregate */
852 if (hash) {
853 keysv = S_find_hash_subscript(aTHX_ (HV*)sv, uninit_sv);
854 if (keysv)
855 subscript_type = FUV_SUBSCRIPT_HASH;
856 }
857 else {
858 index = S_find_array_subscript(aTHX_ (AV*)sv, uninit_sv);
859 if (index >= 0)
860 subscript_type = FUV_SUBSCRIPT_ARRAY;
861 }
862
863 if (match && subscript_type == FUV_SUBSCRIPT_WITHIN)
864 break;
865
866 return S_varname(aTHX_ gv, hash ? "%" : "@", obase->op_targ,
867 keysv, index, subscript_type);
868 }
869
870 case OP_PADSV:
871 if (match && PAD_SVl(obase->op_targ) != uninit_sv)
872 break;
873 return S_varname(aTHX_ Nullgv, "$", obase->op_targ,
874 Nullsv, 0, FUV_SUBSCRIPT_NONE);
875
876 case OP_GVSV:
877 gv = cGVOPx_gv(obase);
878 if (!gv || (match && GvSV(gv) != uninit_sv))
879 break;
880 return S_varname(aTHX_ gv, "$", 0, Nullsv, 0, FUV_SUBSCRIPT_NONE);
881
882 case OP_AELEMFAST:
883 if (obase->op_flags & OPf_SPECIAL) { /* lexical array */
884 if (match) {
885 av = (AV*)PAD_SV(obase->op_targ);
886 if (!av || SvRMAGICAL(av))
887 break;
888 svp = av_fetch(av, (I32)obase->op_private, FALSE);
889 if (!svp || *svp != uninit_sv)
890 break;
891 }
892 return S_varname(aTHX_ Nullgv, "$", obase->op_targ,
893 Nullsv, (I32)obase->op_private, FUV_SUBSCRIPT_ARRAY);
894 }
895 else {
896 gv = cGVOPx_gv(obase);
897 if (!gv)
898 break;
899 if (match) {
900 av = GvAV(gv);
901 if (!av || SvRMAGICAL(av))
902 break;
903 svp = av_fetch(av, (I32)obase->op_private, FALSE);
904 if (!svp || *svp != uninit_sv)
905 break;
906 }
907 return S_varname(aTHX_ gv, "$", 0,
908 Nullsv, (I32)obase->op_private, FUV_SUBSCRIPT_ARRAY);
909 }
910 break;
911
912 case OP_EXISTS:
913 o = cUNOPx(obase)->op_first;
914 if (!o || o->op_type != OP_NULL ||
915 ! (o->op_targ == OP_AELEM || o->op_targ == OP_HELEM))
916 break;
917 return find_uninit_var(cBINOPo->op_last, uninit_sv, match);
918
919 case OP_AELEM:
920 case OP_HELEM:
921 if (PL_op == obase)
922 /* $a[uninit_expr] or $h{uninit_expr} */
923 return find_uninit_var(cBINOPx(obase)->op_last, uninit_sv, match);
924
925 gv = Nullgv;
926 o = cBINOPx(obase)->op_first;
927 kid = cBINOPx(obase)->op_last;
928
929 /* get the av or hv, and optionally the gv */
930 sv = Nullsv;
931 if (o->op_type == OP_PADAV || o->op_type == OP_PADHV) {
932 sv = PAD_SV(o->op_targ);
933 }
934 else if ((o->op_type == OP_RV2AV || o->op_type == OP_RV2HV)
935 && cUNOPo->op_first->op_type == OP_GV)
936 {
937 gv = cGVOPx_gv(cUNOPo->op_first);
938 if (!gv)
939 break;
940 sv = o->op_type == OP_RV2HV ? (SV*)GvHV(gv) : (SV*)GvAV(gv);
941 }
942 if (!sv)
943 break;
944
945 if (kid && kid->op_type == OP_CONST && SvOK(cSVOPx_sv(kid))) {
946 /* index is constant */
947 if (match) {
948 if (SvMAGICAL(sv))
949 break;
950 if (obase->op_type == OP_HELEM) {
951 HE* he = hv_fetch_ent((HV*)sv, cSVOPx_sv(kid), 0, 0);
952 if (!he || HeVAL(he) != uninit_sv)
953 break;
954 }
955 else {
956 svp = av_fetch((AV*)sv, SvIV(cSVOPx_sv(kid)), FALSE);
957 if (!svp || *svp != uninit_sv)
958 break;
959 }
960 }
961 if (obase->op_type == OP_HELEM)
962 return S_varname(aTHX_ gv, "%", o->op_targ,
963 cSVOPx_sv(kid), 0, FUV_SUBSCRIPT_HASH);
964 else
965 return S_varname(aTHX_ gv, "@", o->op_targ, Nullsv,
966 SvIV(cSVOPx_sv(kid)), FUV_SUBSCRIPT_ARRAY);
967 ;
968 }
969 else {
970 /* index is an expression;
971 * attempt to find a match within the aggregate */
972 if (obase->op_type == OP_HELEM) {
973 SV *keysv = S_find_hash_subscript(aTHX_ (HV*)sv, uninit_sv);
974 if (keysv)
975 return S_varname(aTHX_ gv, "%", o->op_targ,
976 keysv, 0, FUV_SUBSCRIPT_HASH);
977 }
978 else {
f54cb97a 979 const I32 index = S_find_array_subscript(aTHX_ (AV*)sv, uninit_sv);
29489e7c 980 if (index >= 0)
f54cb97a 981 return S_varname(aTHX_ gv, "@", o->op_targ,
29489e7c
DM
982 Nullsv, index, FUV_SUBSCRIPT_ARRAY);
983 }
984 if (match)
985 break;
986 return S_varname(aTHX_ gv,
987 (o->op_type == OP_PADAV || o->op_type == OP_RV2AV)
988 ? "@" : "%",
989 o->op_targ, Nullsv, 0, FUV_SUBSCRIPT_WITHIN);
990 }
991
992 break;
993
994 case OP_AASSIGN:
995 /* only examine RHS */
996 return find_uninit_var(cBINOPx(obase)->op_first, uninit_sv, match);
997
998 case OP_OPEN:
999 o = cUNOPx(obase)->op_first;
1000 if (o->op_type == OP_PUSHMARK)
1001 o = o->op_sibling;
1002
1003 if (!o->op_sibling) {
1004 /* one-arg version of open is highly magical */
1005
1006 if (o->op_type == OP_GV) { /* open FOO; */
1007 gv = cGVOPx_gv(o);
1008 if (match && GvSV(gv) != uninit_sv)
1009 break;
7a5fa8a2 1010 return S_varname(aTHX_ gv, "$", 0,
29489e7c
DM
1011 Nullsv, 0, FUV_SUBSCRIPT_NONE);
1012 }
1013 /* other possibilities not handled are:
1014 * open $x; or open my $x; should return '${*$x}'
1015 * open expr; should return '$'.expr ideally
1016 */
1017 break;
1018 }
1019 goto do_op;
1020
1021 /* ops where $_ may be an implicit arg */
1022 case OP_TRANS:
1023 case OP_SUBST:
1024 case OP_MATCH:
1025 if ( !(obase->op_flags & OPf_STACKED)) {
1026 if (uninit_sv == ((obase->op_private & OPpTARGET_MY)
1027 ? PAD_SVl(obase->op_targ)
1028 : DEFSV))
1029 {
1030 sv = sv_newmortal();
1031 sv_setpv(sv, "$_");
1032 return sv;
1033 }
1034 }
1035 goto do_op;
1036
1037 case OP_PRTF:
1038 case OP_PRINT:
1039 /* skip filehandle as it can't produce 'undef' warning */
1040 o = cUNOPx(obase)->op_first;
1041 if ((obase->op_flags & OPf_STACKED) && o->op_type == OP_PUSHMARK)
1042 o = o->op_sibling->op_sibling;
1043 goto do_op2;
1044
1045
e21bd382 1046 case OP_RV2SV:
29489e7c
DM
1047 case OP_CUSTOM:
1048 case OP_ENTERSUB:
1049 match = 1; /* XS or custom code could trigger random warnings */
1050 goto do_op;
1051
1052 case OP_SCHOMP:
1053 case OP_CHOMP:
1054 if (SvROK(PL_rs) && uninit_sv == SvRV(PL_rs))
1055 return sv_2mortal(newSVpv("${$/}", 0));
1056 /* FALL THROUGH */
1057
1058 default:
1059 do_op:
1060 if (!(obase->op_flags & OPf_KIDS))
1061 break;
1062 o = cUNOPx(obase)->op_first;
1063
1064 do_op2:
1065 if (!o)
1066 break;
1067
1068 /* if all except one arg are constant, or have no side-effects,
1069 * or are optimized away, then it's unambiguous */
1070 o2 = Nullop;
1071 for (kid=o; kid; kid = kid->op_sibling) {
1072 if (kid &&
1073 ( (kid->op_type == OP_CONST && SvOK(cSVOPx_sv(kid)))
1074 || (kid->op_type == OP_NULL && ! (kid->op_flags & OPf_KIDS))
1075 || (kid->op_type == OP_PUSHMARK)
1076 )
1077 )
1078 continue;
1079 if (o2) { /* more than one found */
1080 o2 = Nullop;
1081 break;
1082 }
1083 o2 = kid;
1084 }
1085 if (o2)
1086 return find_uninit_var(o2, uninit_sv, match);
1087
1088 /* scan all args */
1089 while (o) {
1090 sv = find_uninit_var(o, uninit_sv, 1);
1091 if (sv)
1092 return sv;
1093 o = o->op_sibling;
1094 }
1095 break;
1096 }
1097 return Nullsv;
1098}
1099
1100
645c22ef
DM
1101/*
1102=for apidoc report_uninit
1103
1104Print appropriate "Use of uninitialized variable" warning
1105
1106=cut
1107*/
1108
1d7c1841 1109void
29489e7c
DM
1110Perl_report_uninit(pTHX_ SV* uninit_sv)
1111{
1112 if (PL_op) {
112dcc46 1113 SV* varname = Nullsv;
29489e7c
DM
1114 if (uninit_sv) {
1115 varname = find_uninit_var(PL_op, uninit_sv,0);
1116 if (varname)
1117 sv_insert(varname, 0, 0, " ", 1);
1118 }
9014280d 1119 Perl_warner(aTHX_ packWARN(WARN_UNINITIALIZED), PL_warn_uninit,
29489e7c
DM
1120 varname ? SvPV_nolen(varname) : "",
1121 " in ", OP_DESC(PL_op));
1122 }
1d7c1841 1123 else
29489e7c
DM
1124 Perl_warner(aTHX_ packWARN(WARN_UNINITIALIZED), PL_warn_uninit,
1125 "", "", "");
1d7c1841
GS
1126}
1127
645c22ef
DM
1128/* allocate another arena's worth of NV bodies */
1129
cbe51380 1130STATIC void
cea2e8a9 1131S_more_xnv(pTHX)
463ee0b2 1132{
cac9b346
NC
1133 NV* xnv;
1134 NV* xnvend;
7b2c381c
NC
1135 void *ptr;
1136 New(711, ptr, PERL_ARENA_SIZE/sizeof(NV), NV);
1137 *((void **) ptr) = (void *)PL_xnv_arenaroot;
612f20c3
GS
1138 PL_xnv_arenaroot = ptr;
1139
1140 xnv = (NV*) ptr;
9c17f24a 1141 xnvend = &xnv[PERL_ARENA_SIZE / sizeof(NV) - 1];
65202027 1142 xnv += (sizeof(XPVIV) - 1) / sizeof(NV) + 1; /* fudge by sizeof XPVIV */
3280af22 1143 PL_xnv_root = xnv;
463ee0b2 1144 while (xnv < xnvend) {
65202027 1145 *(NV**)xnv = (NV*)(xnv + 1);
463ee0b2
LW
1146 xnv++;
1147 }
cac9b346
NC
1148 *(NV**)xnv = 0;
1149}
1150
1151/* allocate another arena's worth of struct xpv */
1152
1153STATIC void
1154S_more_xpv(pTHX)
1155{
1156 XPV* xpv;
1157 XPV* xpvend;
1158 New(713, xpv, PERL_ARENA_SIZE/sizeof(XPV), XPV);
7b2c381c 1159 *((XPV**)xpv) = PL_xpv_arenaroot;
cac9b346
NC
1160 PL_xpv_arenaroot = xpv;
1161
1162 xpvend = &xpv[PERL_ARENA_SIZE / sizeof(XPV) - 1];
1163 PL_xpv_root = ++xpv;
1164 while (xpv < xpvend) {
7b2c381c 1165 *((XPV**)xpv) = xpv + 1;
cac9b346
NC
1166 xpv++;
1167 }
7b2c381c 1168 *((XPV**)xpv) = 0;
cac9b346
NC
1169}
1170
1171/* allocate another arena's worth of struct xpviv */
1172
1173STATIC void
1174S_more_xpviv(pTHX)
1175{
1176 XPVIV* xpviv;
1177 XPVIV* xpvivend;
1178 New(714, xpviv, PERL_ARENA_SIZE/sizeof(XPVIV), XPVIV);
7b2c381c 1179 *((XPVIV**)xpviv) = PL_xpviv_arenaroot;
cac9b346
NC
1180 PL_xpviv_arenaroot = xpviv;
1181
1182 xpvivend = &xpviv[PERL_ARENA_SIZE / sizeof(XPVIV) - 1];
1183 PL_xpviv_root = ++xpviv;
1184 while (xpviv < xpvivend) {
7b2c381c 1185 *((XPVIV**)xpviv) = xpviv + 1;
cac9b346
NC
1186 xpviv++;
1187 }
7b2c381c 1188 *((XPVIV**)xpviv) = 0;
cac9b346
NC
1189}
1190
1191/* allocate another arena's worth of struct xpvnv */
1192
1193STATIC void
1194S_more_xpvnv(pTHX)
1195{
1196 XPVNV* xpvnv;
1197 XPVNV* xpvnvend;
1198 New(715, xpvnv, PERL_ARENA_SIZE/sizeof(XPVNV), XPVNV);
7b2c381c 1199 *((XPVNV**)xpvnv) = PL_xpvnv_arenaroot;
cac9b346
NC
1200 PL_xpvnv_arenaroot = xpvnv;
1201
1202 xpvnvend = &xpvnv[PERL_ARENA_SIZE / sizeof(XPVNV) - 1];
1203 PL_xpvnv_root = ++xpvnv;
1204 while (xpvnv < xpvnvend) {
7b2c381c 1205 *((XPVNV**)xpvnv) = xpvnv + 1;
cac9b346
NC
1206 xpvnv++;
1207 }
7b2c381c 1208 *((XPVNV**)xpvnv) = 0;
cac9b346
NC
1209}
1210
1211/* allocate another arena's worth of struct xpvcv */
1212
1213STATIC void
1214S_more_xpvcv(pTHX)
1215{
1216 XPVCV* xpvcv;
1217 XPVCV* xpvcvend;
1218 New(716, xpvcv, PERL_ARENA_SIZE/sizeof(XPVCV), XPVCV);
7b2c381c 1219 *((XPVCV**)xpvcv) = PL_xpvcv_arenaroot;
cac9b346
NC
1220 PL_xpvcv_arenaroot = xpvcv;
1221
1222 xpvcvend = &xpvcv[PERL_ARENA_SIZE / sizeof(XPVCV) - 1];
1223 PL_xpvcv_root = ++xpvcv;
1224 while (xpvcv < xpvcvend) {
7b2c381c 1225 *((XPVCV**)xpvcv) = xpvcv + 1;
cac9b346
NC
1226 xpvcv++;
1227 }
7b2c381c 1228 *((XPVCV**)xpvcv) = 0;
cac9b346
NC
1229}
1230
1231/* allocate another arena's worth of struct xpvav */
1232
1233STATIC void
1234S_more_xpvav(pTHX)
1235{
1236 XPVAV* xpvav;
1237 XPVAV* xpvavend;
1238 New(717, xpvav, PERL_ARENA_SIZE/sizeof(XPVAV), XPVAV);
7b2c381c 1239 *((XPVAV**)xpvav) = PL_xpvav_arenaroot;
cac9b346
NC
1240 PL_xpvav_arenaroot = xpvav;
1241
1242 xpvavend = &xpvav[PERL_ARENA_SIZE / sizeof(XPVAV) - 1];
1243 PL_xpvav_root = ++xpvav;
1244 while (xpvav < xpvavend) {
7b2c381c 1245 *((XPVAV**)xpvav) = xpvav + 1;
cac9b346
NC
1246 xpvav++;
1247 }
7b2c381c 1248 *((XPVAV**)xpvav) = 0;
cac9b346
NC
1249}
1250
1251/* allocate another arena's worth of struct xpvhv */
1252
1253STATIC void
1254S_more_xpvhv(pTHX)
1255{
1256 XPVHV* xpvhv;
1257 XPVHV* xpvhvend;
1258 New(718, xpvhv, PERL_ARENA_SIZE/sizeof(XPVHV), XPVHV);
7b2c381c 1259 *((XPVHV**)xpvhv) = PL_xpvhv_arenaroot;
cac9b346
NC
1260 PL_xpvhv_arenaroot = xpvhv;
1261
1262 xpvhvend = &xpvhv[PERL_ARENA_SIZE / sizeof(XPVHV) - 1];
1263 PL_xpvhv_root = ++xpvhv;
1264 while (xpvhv < xpvhvend) {
7b2c381c 1265 *((XPVHV**)xpvhv) = xpvhv + 1;
cac9b346
NC
1266 xpvhv++;
1267 }
7b2c381c 1268 *((XPVHV**)xpvhv) = 0;
cac9b346
NC
1269}
1270
1271/* allocate another arena's worth of struct xpvmg */
1272
1273STATIC void
1274S_more_xpvmg(pTHX)
1275{
1276 XPVMG* xpvmg;
1277 XPVMG* xpvmgend;
1278 New(719, xpvmg, PERL_ARENA_SIZE/sizeof(XPVMG), XPVMG);
7b2c381c 1279 *((XPVMG**)xpvmg) = PL_xpvmg_arenaroot;
cac9b346
NC
1280 PL_xpvmg_arenaroot = xpvmg;
1281
1282 xpvmgend = &xpvmg[PERL_ARENA_SIZE / sizeof(XPVMG) - 1];
1283 PL_xpvmg_root = ++xpvmg;
1284 while (xpvmg < xpvmgend) {
7b2c381c 1285 *((XPVMG**)xpvmg) = xpvmg + 1;
cac9b346
NC
1286 xpvmg++;
1287 }
7b2c381c 1288 *((XPVMG**)xpvmg) = 0;
cac9b346
NC
1289}
1290
1291/* allocate another arena's worth of struct xpvgv */
1292
1293STATIC void
1294S_more_xpvgv(pTHX)
1295{
1296 XPVGV* xpvgv;
1297 XPVGV* xpvgvend;
1298 New(720, xpvgv, PERL_ARENA_SIZE/sizeof(XPVGV), XPVGV);
7b2c381c 1299 *((XPVGV**)xpvgv) = PL_xpvgv_arenaroot;
cac9b346
NC
1300 PL_xpvgv_arenaroot = xpvgv;
1301
1302 xpvgvend = &xpvgv[PERL_ARENA_SIZE / sizeof(XPVGV) - 1];
1303 PL_xpvgv_root = ++xpvgv;
1304 while (xpvgv < xpvgvend) {
7b2c381c 1305 *((XPVGV**)xpvgv) = xpvgv + 1;
cac9b346
NC
1306 xpvgv++;
1307 }
7b2c381c 1308 *((XPVGV**)xpvgv) = 0;
cac9b346
NC
1309}
1310
1311/* allocate another arena's worth of struct xpvlv */
1312
1313STATIC void
1314S_more_xpvlv(pTHX)
1315{
1316 XPVLV* xpvlv;
1317 XPVLV* xpvlvend;
1318 New(720, xpvlv, PERL_ARENA_SIZE/sizeof(XPVLV), XPVLV);
7b2c381c 1319 *((XPVLV**)xpvlv) = PL_xpvlv_arenaroot;
cac9b346
NC
1320 PL_xpvlv_arenaroot = xpvlv;
1321
1322 xpvlvend = &xpvlv[PERL_ARENA_SIZE / sizeof(XPVLV) - 1];
1323 PL_xpvlv_root = ++xpvlv;
1324 while (xpvlv < xpvlvend) {
7b2c381c 1325 *((XPVLV**)xpvlv) = xpvlv + 1;
cac9b346
NC
1326 xpvlv++;
1327 }
7b2c381c 1328 *((XPVLV**)xpvlv) = 0;
cac9b346
NC
1329}
1330
1331/* allocate another arena's worth of struct xpvbm */
1332
1333STATIC void
1334S_more_xpvbm(pTHX)
1335{
1336 XPVBM* xpvbm;
1337 XPVBM* xpvbmend;
1338 New(721, xpvbm, PERL_ARENA_SIZE/sizeof(XPVBM), XPVBM);
7b2c381c 1339 *((XPVBM**)xpvbm) = PL_xpvbm_arenaroot;
cac9b346
NC
1340 PL_xpvbm_arenaroot = xpvbm;
1341
1342 xpvbmend = &xpvbm[PERL_ARENA_SIZE / sizeof(XPVBM) - 1];
1343 PL_xpvbm_root = ++xpvbm;
1344 while (xpvbm < xpvbmend) {
7b2c381c 1345 *((XPVBM**)xpvbm) = xpvbm + 1;
cac9b346
NC
1346 xpvbm++;
1347 }
7b2c381c 1348 *((XPVBM**)xpvbm) = 0;
cac9b346 1349}
612f20c3 1350
cac9b346
NC
1351/* grab a new NV body from the free list, allocating more if necessary */
1352
1353STATIC XPVNV*
1354S_new_xnv(pTHX)
1355{
1356 NV* xnv;
1357 LOCK_SV_MUTEX;
1358 if (!PL_xnv_root)
1359 S_more_xnv(aTHX);
1360 xnv = PL_xnv_root;
1361 PL_xnv_root = *(NV**)xnv;
1362 UNLOCK_SV_MUTEX;
1363 return (XPVNV*)((char*)xnv - STRUCT_OFFSET(XPVNV, xnv_nv));
1364}
1365
1366/* return an NV body to the free list */
1367
1368STATIC void
1369S_del_xnv(pTHX_ XPVNV *p)
1370{
1371 NV* xnv = (NV*)((char*)(p) + STRUCT_OFFSET(XPVNV, xnv_nv));
1372 LOCK_SV_MUTEX;
1373 *(NV**)xnv = PL_xnv_root;
1374 PL_xnv_root = xnv;
1375 UNLOCK_SV_MUTEX;
ed6116ce
LW
1376}
1377
645c22ef
DM
1378/* grab a new struct xpv from the free list, allocating more if necessary */
1379
76e3520e 1380STATIC XPV*
cea2e8a9 1381S_new_xpv(pTHX)
463ee0b2
LW
1382{
1383 XPV* xpv;
cbe51380
GS
1384 LOCK_SV_MUTEX;
1385 if (!PL_xpv_root)
cac9b346 1386 S_more_xpv(aTHX);
cbe51380 1387 xpv = PL_xpv_root;
7b2c381c 1388 PL_xpv_root = *(XPV**)xpv;
cbe51380
GS
1389 UNLOCK_SV_MUTEX;
1390 return xpv;
463ee0b2
LW
1391}
1392
645c22ef
DM
1393/* return a struct xpv to the free list */
1394
76e3520e 1395STATIC void
cea2e8a9 1396S_del_xpv(pTHX_ XPV *p)
463ee0b2 1397{
cbe51380 1398 LOCK_SV_MUTEX;
7b2c381c 1399 *(XPV**)p = PL_xpv_root;
3280af22 1400 PL_xpv_root = p;
cbe51380 1401 UNLOCK_SV_MUTEX;
463ee0b2
LW
1402}
1403
645c22ef
DM
1404/* grab a new struct xpviv from the free list, allocating more if necessary */
1405
932e9ff9
VB
1406STATIC XPVIV*
1407S_new_xpviv(pTHX)
1408{
1409 XPVIV* xpviv;
1410 LOCK_SV_MUTEX;
1411 if (!PL_xpviv_root)
cac9b346 1412 S_more_xpviv(aTHX);
932e9ff9 1413 xpviv = PL_xpviv_root;
7b2c381c 1414 PL_xpviv_root = *(XPVIV**)xpviv;
932e9ff9
VB
1415 UNLOCK_SV_MUTEX;
1416 return xpviv;
1417}
1418
645c22ef
DM
1419/* return a struct xpviv to the free list */
1420
932e9ff9
VB
1421STATIC void
1422S_del_xpviv(pTHX_ XPVIV *p)
1423{
1424 LOCK_SV_MUTEX;
7b2c381c 1425 *(XPVIV**)p = PL_xpviv_root;
932e9ff9
VB
1426 PL_xpviv_root = p;
1427 UNLOCK_SV_MUTEX;
1428}
1429
645c22ef
DM
1430/* grab a new struct xpvnv from the free list, allocating more if necessary */
1431
932e9ff9
VB
1432STATIC XPVNV*
1433S_new_xpvnv(pTHX)
1434{
1435 XPVNV* xpvnv;
1436 LOCK_SV_MUTEX;
1437 if (!PL_xpvnv_root)
cac9b346 1438 S_more_xpvnv(aTHX);
932e9ff9 1439 xpvnv = PL_xpvnv_root;
7b2c381c 1440 PL_xpvnv_root = *(XPVNV**)xpvnv;
932e9ff9
VB
1441 UNLOCK_SV_MUTEX;
1442 return xpvnv;
1443}
1444
645c22ef
DM
1445/* return a struct xpvnv to the free list */
1446
932e9ff9
VB
1447STATIC void
1448S_del_xpvnv(pTHX_ XPVNV *p)
1449{
1450 LOCK_SV_MUTEX;
7b2c381c 1451 *(XPVNV**)p = PL_xpvnv_root;
932e9ff9
VB
1452 PL_xpvnv_root = p;
1453 UNLOCK_SV_MUTEX;
1454}
1455
645c22ef
DM
1456/* grab a new struct xpvcv from the free list, allocating more if necessary */
1457
932e9ff9
VB
1458STATIC XPVCV*
1459S_new_xpvcv(pTHX)
1460{
1461 XPVCV* xpvcv;
1462 LOCK_SV_MUTEX;
1463 if (!PL_xpvcv_root)
cac9b346 1464 S_more_xpvcv(aTHX);
932e9ff9 1465 xpvcv = PL_xpvcv_root;
7b2c381c 1466 PL_xpvcv_root = *(XPVCV**)xpvcv;
932e9ff9
VB
1467 UNLOCK_SV_MUTEX;
1468 return xpvcv;
1469}
1470
645c22ef
DM
1471/* return a struct xpvcv to the free list */
1472
932e9ff9
VB
1473STATIC void
1474S_del_xpvcv(pTHX_ XPVCV *p)
1475{
1476 LOCK_SV_MUTEX;
7b2c381c 1477 *(XPVCV**)p = PL_xpvcv_root;
932e9ff9
VB
1478 PL_xpvcv_root = p;
1479 UNLOCK_SV_MUTEX;
1480}
1481
645c22ef
DM
1482/* grab a new struct xpvav from the free list, allocating more if necessary */
1483
932e9ff9
VB
1484STATIC XPVAV*
1485S_new_xpvav(pTHX)
1486{
1487 XPVAV* xpvav;
1488 LOCK_SV_MUTEX;
1489 if (!PL_xpvav_root)
cac9b346 1490 S_more_xpvav(aTHX);
932e9ff9 1491 xpvav = PL_xpvav_root;
7b2c381c 1492 PL_xpvav_root = *(XPVAV**)xpvav;
932e9ff9
VB
1493 UNLOCK_SV_MUTEX;
1494 return xpvav;
1495}
1496
645c22ef
DM
1497/* return a struct xpvav to the free list */
1498
932e9ff9
VB
1499STATIC void
1500S_del_xpvav(pTHX_ XPVAV *p)
1501{
1502 LOCK_SV_MUTEX;
7b2c381c 1503 *(XPVAV**)p = PL_xpvav_root;
932e9ff9
VB
1504 PL_xpvav_root = p;
1505 UNLOCK_SV_MUTEX;
1506}
1507
645c22ef
DM
1508/* grab a new struct xpvhv from the free list, allocating more if necessary */
1509
932e9ff9
VB
1510STATIC XPVHV*
1511S_new_xpvhv(pTHX)
1512{
1513 XPVHV* xpvhv;
1514 LOCK_SV_MUTEX;
1515 if (!PL_xpvhv_root)
cac9b346 1516 S_more_xpvhv(aTHX);
932e9ff9 1517 xpvhv = PL_xpvhv_root;
7b2c381c 1518 PL_xpvhv_root = *(XPVHV**)xpvhv;
932e9ff9
VB
1519 UNLOCK_SV_MUTEX;
1520 return xpvhv;
1521}
1522
645c22ef
DM
1523/* return a struct xpvhv to the free list */
1524
932e9ff9
VB
1525STATIC void
1526S_del_xpvhv(pTHX_ XPVHV *p)
1527{
1528 LOCK_SV_MUTEX;
7b2c381c 1529 *(XPVHV**)p = PL_xpvhv_root;
932e9ff9
VB
1530 PL_xpvhv_root = p;
1531 UNLOCK_SV_MUTEX;
1532}
1533
645c22ef
DM
1534/* grab a new struct xpvmg from the free list, allocating more if necessary */
1535
932e9ff9
VB
1536STATIC XPVMG*
1537S_new_xpvmg(pTHX)
1538{
1539 XPVMG* xpvmg;
1540 LOCK_SV_MUTEX;
1541 if (!PL_xpvmg_root)
cac9b346 1542 S_more_xpvmg(aTHX);
932e9ff9 1543 xpvmg = PL_xpvmg_root;
7b2c381c 1544 PL_xpvmg_root = *(XPVMG**)xpvmg;
932e9ff9
VB
1545 UNLOCK_SV_MUTEX;
1546 return xpvmg;
1547}
1548
645c22ef
DM
1549/* return a struct xpvmg to the free list */
1550
932e9ff9
VB
1551STATIC void
1552S_del_xpvmg(pTHX_ XPVMG *p)
1553{
1554 LOCK_SV_MUTEX;
7b2c381c 1555 *(XPVMG**)p = PL_xpvmg_root;
932e9ff9
VB
1556 PL_xpvmg_root = p;
1557 UNLOCK_SV_MUTEX;
1558}
1559
727879eb
NC
1560/* grab a new struct xpvgv from the free list, allocating more if necessary */
1561
1562STATIC XPVGV*
1563S_new_xpvgv(pTHX)
1564{
1565 XPVGV* xpvgv;
1566 LOCK_SV_MUTEX;
1567 if (!PL_xpvgv_root)
cac9b346 1568 S_more_xpvgv(aTHX);
727879eb 1569 xpvgv = PL_xpvgv_root;
7b2c381c 1570 PL_xpvgv_root = *(XPVGV**)xpvgv;
727879eb
NC
1571 UNLOCK_SV_MUTEX;
1572 return xpvgv;
1573}
1574
1575/* return a struct xpvgv to the free list */
1576
1577STATIC void
1578S_del_xpvgv(pTHX_ XPVGV *p)
1579{
1580 LOCK_SV_MUTEX;
7b2c381c 1581 *(XPVGV**)p = PL_xpvgv_root;
727879eb
NC
1582 PL_xpvgv_root = p;
1583 UNLOCK_SV_MUTEX;
1584}
1585
645c22ef
DM
1586/* grab a new struct xpvlv from the free list, allocating more if necessary */
1587
932e9ff9
VB
1588STATIC XPVLV*
1589S_new_xpvlv(pTHX)
1590{
1591 XPVLV* xpvlv;
1592 LOCK_SV_MUTEX;
1593 if (!PL_xpvlv_root)
cac9b346 1594 S_more_xpvlv(aTHX);
932e9ff9 1595 xpvlv = PL_xpvlv_root;
7b2c381c 1596 PL_xpvlv_root = *(XPVLV**)xpvlv;
932e9ff9
VB
1597 UNLOCK_SV_MUTEX;
1598 return xpvlv;
1599}
1600
645c22ef
DM
1601/* return a struct xpvlv to the free list */
1602
932e9ff9
VB
1603STATIC void
1604S_del_xpvlv(pTHX_ XPVLV *p)
1605{
1606 LOCK_SV_MUTEX;
7b2c381c 1607 *(XPVLV**)p = PL_xpvlv_root;
932e9ff9
VB
1608 PL_xpvlv_root = p;
1609 UNLOCK_SV_MUTEX;
1610}
1611
645c22ef
DM
1612/* grab a new struct xpvbm from the free list, allocating more if necessary */
1613
932e9ff9
VB
1614STATIC XPVBM*
1615S_new_xpvbm(pTHX)
1616{
1617 XPVBM* xpvbm;
1618 LOCK_SV_MUTEX;
1619 if (!PL_xpvbm_root)
cac9b346 1620 S_more_xpvbm(aTHX);
932e9ff9 1621 xpvbm = PL_xpvbm_root;
7b2c381c 1622 PL_xpvbm_root = *(XPVBM**)xpvbm;
932e9ff9
VB
1623 UNLOCK_SV_MUTEX;
1624 return xpvbm;
1625}
1626
645c22ef
DM
1627/* return a struct xpvbm to the free list */
1628
932e9ff9
VB
1629STATIC void
1630S_del_xpvbm(pTHX_ XPVBM *p)
1631{
1632 LOCK_SV_MUTEX;
7b2c381c 1633 *(XPVBM**)p = PL_xpvbm_root;
932e9ff9
VB
1634 PL_xpvbm_root = p;
1635 UNLOCK_SV_MUTEX;
1636}
1637
7bab3ede
MB
1638#define my_safemalloc(s) (void*)safemalloc(s)
1639#define my_safefree(p) safefree((char*)p)
463ee0b2 1640
d33b2eba 1641#ifdef PURIFY
463ee0b2 1642
d33b2eba
GS
1643#define new_XNV() my_safemalloc(sizeof(XPVNV))
1644#define del_XNV(p) my_safefree(p)
463ee0b2 1645
d33b2eba
GS
1646#define new_XPV() my_safemalloc(sizeof(XPV))
1647#define del_XPV(p) my_safefree(p)
9b94d1dd 1648
d33b2eba
GS
1649#define new_XPVIV() my_safemalloc(sizeof(XPVIV))
1650#define del_XPVIV(p) my_safefree(p)
932e9ff9 1651
d33b2eba
GS
1652#define new_XPVNV() my_safemalloc(sizeof(XPVNV))
1653#define del_XPVNV(p) my_safefree(p)
932e9ff9 1654
d33b2eba
GS
1655#define new_XPVCV() my_safemalloc(sizeof(XPVCV))
1656#define del_XPVCV(p) my_safefree(p)
932e9ff9 1657
d33b2eba
GS
1658#define new_XPVAV() my_safemalloc(sizeof(XPVAV))
1659#define del_XPVAV(p) my_safefree(p)
1660
1661#define new_XPVHV() my_safemalloc(sizeof(XPVHV))
1662#define del_XPVHV(p) my_safefree(p)
1c846c1f 1663
d33b2eba
GS
1664#define new_XPVMG() my_safemalloc(sizeof(XPVMG))
1665#define del_XPVMG(p) my_safefree(p)
1666
727879eb
NC
1667#define new_XPVGV() my_safemalloc(sizeof(XPVGV))
1668#define del_XPVGV(p) my_safefree(p)
1669
d33b2eba
GS
1670#define new_XPVLV() my_safemalloc(sizeof(XPVLV))
1671#define del_XPVLV(p) my_safefree(p)
1672
1673#define new_XPVBM() my_safemalloc(sizeof(XPVBM))
1674#define del_XPVBM(p) my_safefree(p)
1675
1676#else /* !PURIFY */
1677
d33b2eba
GS
1678#define new_XNV() (void*)new_xnv()
1679#define del_XNV(p) del_xnv((XPVNV*) p)
9b94d1dd 1680
d33b2eba
GS
1681#define new_XPV() (void*)new_xpv()
1682#define del_XPV(p) del_xpv((XPV *)p)
1683
1684#define new_XPVIV() (void*)new_xpviv()
1685#define del_XPVIV(p) del_xpviv((XPVIV *)p)
1686
1687#define new_XPVNV() (void*)new_xpvnv()
1688#define del_XPVNV(p) del_xpvnv((XPVNV *)p)
1689
1690#define new_XPVCV() (void*)new_xpvcv()
1691#define del_XPVCV(p) del_xpvcv((XPVCV *)p)
1692
1693#define new_XPVAV() (void*)new_xpvav()
1694#define del_XPVAV(p) del_xpvav((XPVAV *)p)
1695
1696#define new_XPVHV() (void*)new_xpvhv()
1697#define del_XPVHV(p) del_xpvhv((XPVHV *)p)
1c846c1f 1698
d33b2eba
GS
1699#define new_XPVMG() (void*)new_xpvmg()
1700#define del_XPVMG(p) del_xpvmg((XPVMG *)p)
1701
727879eb
NC
1702#define new_XPVGV() (void*)new_xpvgv()
1703#define del_XPVGV(p) del_xpvgv((XPVGV *)p)
1704
d33b2eba
GS
1705#define new_XPVLV() (void*)new_xpvlv()
1706#define del_XPVLV(p) del_xpvlv((XPVLV *)p)
1707
1708#define new_XPVBM() (void*)new_xpvbm()
1709#define del_XPVBM(p) del_xpvbm((XPVBM *)p)
1710
1711#endif /* PURIFY */
9b94d1dd 1712
d33b2eba
GS
1713#define new_XPVFM() my_safemalloc(sizeof(XPVFM))
1714#define del_XPVFM(p) my_safefree(p)
1c846c1f 1715
d33b2eba
GS
1716#define new_XPVIO() my_safemalloc(sizeof(XPVIO))
1717#define del_XPVIO(p) my_safefree(p)
8990e307 1718
954c1994
GS
1719/*
1720=for apidoc sv_upgrade
1721
ff276b08 1722Upgrade an SV to a more complex form. Generally adds a new body type to the
645c22ef 1723SV, then copies across as much information as possible from the old body.
ff276b08 1724You generally want to use the C<SvUPGRADE> macro wrapper. See also C<svtype>.
954c1994
GS
1725
1726=cut
1727*/
1728
79072805 1729bool
864dbfa3 1730Perl_sv_upgrade(pTHX_ register SV *sv, U32 mt)
79072805 1731{
e763e3dc 1732
d2e56290
NC
1733 char* pv;
1734 U32 cur;
1735 U32 len;
1736 IV iv;
1737 NV nv;
1738 MAGIC* magic;
1739 HV* stash;
79072805 1740
765f542d
NC
1741 if (mt != SVt_PV && SvIsCOW(sv)) {
1742 sv_force_normal_flags(sv, 0);
f130fd45
NIS
1743 }
1744
79072805
LW
1745 if (SvTYPE(sv) == mt)
1746 return TRUE;
1747
d2e56290
NC
1748 pv = NULL;
1749 cur = 0;
1750 len = 0;
1751 iv = 0;
1752 nv = 0.0;
1753 magic = NULL;
1754 stash = Nullhv;
1755
79072805
LW
1756 switch (SvTYPE(sv)) {
1757 case SVt_NULL:
79072805 1758 break;
79072805 1759 case SVt_IV:
463ee0b2 1760 iv = SvIVX(sv);
ed6116ce 1761 if (mt == SVt_NV)
463ee0b2 1762 mt = SVt_PVNV;
ed6116ce
LW
1763 else if (mt < SVt_PVIV)
1764 mt = SVt_PVIV;
79072805
LW
1765 break;
1766 case SVt_NV:
463ee0b2 1767 nv = SvNVX(sv);
79072805 1768 del_XNV(SvANY(sv));
ed6116ce 1769 if (mt < SVt_PVNV)
79072805
LW
1770 mt = SVt_PVNV;
1771 break;
ed6116ce
LW
1772 case SVt_RV:
1773 pv = (char*)SvRV(sv);
ed6116ce 1774 break;
79072805 1775 case SVt_PV:
463ee0b2 1776 pv = SvPVX(sv);
79072805
LW
1777 cur = SvCUR(sv);
1778 len = SvLEN(sv);
79072805 1779 del_XPV(SvANY(sv));
748a9306
LW
1780 if (mt <= SVt_IV)
1781 mt = SVt_PVIV;
1782 else if (mt == SVt_NV)
1783 mt = SVt_PVNV;
79072805
LW
1784 break;
1785 case SVt_PVIV:
463ee0b2 1786 pv = SvPVX(sv);
79072805
LW
1787 cur = SvCUR(sv);
1788 len = SvLEN(sv);
463ee0b2 1789 iv = SvIVX(sv);
79072805
LW
1790 del_XPVIV(SvANY(sv));
1791 break;
1792 case SVt_PVNV:
463ee0b2 1793 pv = SvPVX(sv);
79072805
LW
1794 cur = SvCUR(sv);
1795 len = SvLEN(sv);
463ee0b2
LW
1796 iv = SvIVX(sv);
1797 nv = SvNVX(sv);
79072805
LW
1798 del_XPVNV(SvANY(sv));
1799 break;
1800 case SVt_PVMG:
0ec50a73
NC
1801 /* Because the XPVMG of PL_mess_sv isn't allocated from the arena,
1802 there's no way that it can be safely upgraded, because perl.c
1803 expects to Safefree(SvANY(PL_mess_sv)) */
1804 assert(sv != PL_mess_sv);
bce8f412
NC
1805 /* This flag bit is used to mean other things in other scalar types.
1806 Given that it only has meaning inside the pad, it shouldn't be set
1807 on anything that can get upgraded. */
1808 assert((SvFLAGS(sv) & SVpad_TYPED) == 0);
463ee0b2 1809 pv = SvPVX(sv);
79072805
LW
1810 cur = SvCUR(sv);
1811 len = SvLEN(sv);
463ee0b2
LW
1812 iv = SvIVX(sv);
1813 nv = SvNVX(sv);
79072805
LW
1814 magic = SvMAGIC(sv);
1815 stash = SvSTASH(sv);
1816 del_XPVMG(SvANY(sv));
1817 break;
1818 default:
cea2e8a9 1819 Perl_croak(aTHX_ "Can't upgrade that kind of scalar");
79072805
LW
1820 }
1821
ffb05e06
NC
1822 SvFLAGS(sv) &= ~SVTYPEMASK;
1823 SvFLAGS(sv) |= mt;
1824
79072805
LW
1825 switch (mt) {
1826 case SVt_NULL:
cea2e8a9 1827 Perl_croak(aTHX_ "Can't upgrade to undef");
79072805 1828 case SVt_IV:
7b2c381c 1829 SvANY(sv) = (XPVIV*)((char*)&(sv->sv_u.sv_iv) - STRUCT_OFFSET(XPVIV, xiv_iv));
45977657 1830 SvIV_set(sv, iv);
79072805
LW
1831 break;
1832 case SVt_NV:
1833 SvANY(sv) = new_XNV();
9d6ce603 1834 SvNV_set(sv, nv);
79072805 1835 break;
ed6116ce 1836 case SVt_RV:
7b2c381c 1837 SvANY(sv) = &sv->sv_u.sv_rv;
b162af07 1838 SvRV_set(sv, (SV*)pv);
ed6116ce 1839 break;
79072805
LW
1840 case SVt_PVHV:
1841 SvANY(sv) = new_XPVHV();
bfcb3514 1842 ((XPVHV*) SvANY(sv))->xhv_aux = 0;
463ee0b2
LW
1843 HvFILL(sv) = 0;
1844 HvMAX(sv) = 0;
8aacddc1 1845 HvTOTALKEYS(sv) = 0;
bd4b1eb5
NC
1846
1847 /* Fall through... */
1848 if (0) {
1849 case SVt_PVAV:
1850 SvANY(sv) = new_XPVAV();
1851 AvMAX(sv) = -1;
1852 AvFILLp(sv) = -1;
1853 AvALLOC(sv) = 0;
1854 AvARYLEN(sv)= 0;
11ca45c0 1855 AvREAL_only(sv);
bd4b1eb5
NC
1856 SvIV_set(sv, 0);
1857 SvNV_set(sv, 0.0);
1858 }
1859 /* to here. */
c2bfdfaf
NC
1860 /* XXX? Only SVt_NULL is ever upgraded to AV or HV? */
1861 assert(!pv);
8bd4d4c5
NC
1862 /* FIXME. Should be able to remove all this if()... if the above
1863 assertion is genuinely always true. */
1864 if(SvOOK(sv)) {
1865 pv -= iv;
1866 SvFLAGS(sv) &= ~SVf_OOK;
1867 }
1868 Safefree(pv);
bd4b1eb5 1869 SvPV_set(sv, (char*)0);
b162af07
SP
1870 SvMAGIC_set(sv, magic);
1871 SvSTASH_set(sv, stash);
79072805 1872 break;
bd4b1eb5
NC
1873
1874 case SVt_PVIO:
1875 SvANY(sv) = new_XPVIO();
1876 Zero(SvANY(sv), 1, XPVIO);
1877 IoPAGE_LEN(sv) = 60;
1878 goto set_magic_common;
1879 case SVt_PVFM:
1880 SvANY(sv) = new_XPVFM();
1881 Zero(SvANY(sv), 1, XPVFM);
1882 goto set_magic_common;
1883 case SVt_PVBM:
1884 SvANY(sv) = new_XPVBM();
1885 BmRARE(sv) = 0;
1886 BmUSEFUL(sv) = 0;
1887 BmPREVIOUS(sv) = 0;
1888 goto set_magic_common;
1889 case SVt_PVGV:
1890 SvANY(sv) = new_XPVGV();
1891 GvGP(sv) = 0;
1892 GvNAME(sv) = 0;
1893 GvNAMELEN(sv) = 0;
1894 GvSTASH(sv) = 0;
1895 GvFLAGS(sv) = 0;
1896 goto set_magic_common;
79072805
LW
1897 case SVt_PVCV:
1898 SvANY(sv) = new_XPVCV();
748a9306 1899 Zero(SvANY(sv), 1, XPVCV);
bd4b1eb5
NC
1900 goto set_magic_common;
1901 case SVt_PVLV:
1902 SvANY(sv) = new_XPVLV();
1903 LvTARGOFF(sv) = 0;
1904 LvTARGLEN(sv) = 0;
1905 LvTARG(sv) = 0;
1906 LvTYPE(sv) = 0;
93a17b20 1907 GvGP(sv) = 0;
79072805
LW
1908 GvNAME(sv) = 0;
1909 GvNAMELEN(sv) = 0;
1910 GvSTASH(sv) = 0;
a5f75d66 1911 GvFLAGS(sv) = 0;
bd4b1eb5
NC
1912 /* Fall through. */
1913 if (0) {
1914 case SVt_PVMG:
1915 SvANY(sv) = new_XPVMG();
1916 }
1917 set_magic_common:
b162af07
SP
1918 SvMAGIC_set(sv, magic);
1919 SvSTASH_set(sv, stash);
bd4b1eb5
NC
1920 /* Fall through. */
1921 if (0) {
1922 case SVt_PVNV:
1923 SvANY(sv) = new_XPVNV();
1924 }
9d6ce603 1925 SvNV_set(sv, nv);
bd4b1eb5
NC
1926 /* Fall through. */
1927 if (0) {
1928 case SVt_PVIV:
1929 SvANY(sv) = new_XPVIV();
1930 if (SvNIOK(sv))
1931 (void)SvIOK_on(sv);
1932 SvNOK_off(sv);
1933 }
1934 SvIV_set(sv, iv);
1935 /* Fall through. */
1936 if (0) {
1937 case SVt_PV:
1938 SvANY(sv) = new_XPV();
1939 }
f880fe2f 1940 SvPV_set(sv, pv);
b162af07
SP
1941 SvCUR_set(sv, cur);
1942 SvLEN_set(sv, len);
8990e307
LW
1943 break;
1944 }
79072805
LW
1945 return TRUE;
1946}
1947
645c22ef
DM
1948/*
1949=for apidoc sv_backoff
1950
1951Remove any string offset. You should normally use the C<SvOOK_off> macro
1952wrapper instead.
1953
1954=cut
1955*/
1956
79072805 1957int
864dbfa3 1958Perl_sv_backoff(pTHX_ register SV *sv)
79072805
LW
1959{
1960 assert(SvOOK(sv));
463ee0b2
LW
1961 if (SvIVX(sv)) {
1962 char *s = SvPVX(sv);
b162af07 1963 SvLEN_set(sv, SvLEN(sv) + SvIVX(sv));
f880fe2f 1964 SvPV_set(sv, SvPVX(sv) - SvIVX(sv));
79072805 1965 SvIV_set(sv, 0);
463ee0b2 1966 Move(s, SvPVX(sv), SvCUR(sv)+1, char);
79072805
LW
1967 }
1968 SvFLAGS(sv) &= ~SVf_OOK;
a0d0e21e 1969 return 0;
79072805
LW
1970}
1971
954c1994
GS
1972/*
1973=for apidoc sv_grow
1974
645c22ef
DM
1975Expands the character buffer in the SV. If necessary, uses C<sv_unref> and
1976upgrades the SV to C<SVt_PV>. Returns a pointer to the character buffer.
1977Use the C<SvGROW> wrapper instead.
954c1994
GS
1978
1979=cut
1980*/
1981
79072805 1982char *
864dbfa3 1983Perl_sv_grow(pTHX_ register SV *sv, register STRLEN newlen)
79072805
LW
1984{
1985 register char *s;
1986
55497cff 1987#ifdef HAS_64K_LIMIT
79072805 1988 if (newlen >= 0x10000) {
1d7c1841
GS
1989 PerlIO_printf(Perl_debug_log,
1990 "Allocation too large: %"UVxf"\n", (UV)newlen);
79072805
LW
1991 my_exit(1);
1992 }
55497cff 1993#endif /* HAS_64K_LIMIT */
a0d0e21e
LW
1994 if (SvROK(sv))
1995 sv_unref(sv);
79072805
LW
1996 if (SvTYPE(sv) < SVt_PV) {
1997 sv_upgrade(sv, SVt_PV);
463ee0b2 1998 s = SvPVX(sv);
79072805
LW
1999 }
2000 else if (SvOOK(sv)) { /* pv is offset? */
2001 sv_backoff(sv);
463ee0b2 2002 s = SvPVX(sv);
79072805
LW
2003 if (newlen > SvLEN(sv))
2004 newlen += 10 * (newlen - SvCUR(sv)); /* avoid copy each time */
c6f8c383
GA
2005#ifdef HAS_64K_LIMIT
2006 if (newlen >= 0x10000)
2007 newlen = 0xFFFF;
2008#endif
79072805 2009 }
bc44a8a2 2010 else
463ee0b2 2011 s = SvPVX(sv);
54f0641b 2012
79072805 2013 if (newlen > SvLEN(sv)) { /* need more room? */
8d6dde3e 2014 if (SvLEN(sv) && s) {
7bab3ede 2015#ifdef MYMALLOC
a3b680e6 2016 const STRLEN l = malloced_size((void*)SvPVX(sv));
8d6dde3e
IZ
2017 if (newlen <= l) {
2018 SvLEN_set(sv, l);
2019 return s;
2020 } else
c70c8a0a 2021#endif
79072805 2022 Renew(s,newlen,char);
8d6dde3e 2023 }
bfed75c6 2024 else {
4e83176d 2025 New(703, s, newlen, char);
40565179 2026 if (SvPVX(sv) && SvCUR(sv)) {
54f0641b 2027 Move(SvPVX(sv), s, (newlen < SvCUR(sv)) ? newlen : SvCUR(sv), char);
40565179 2028 }
4e83176d 2029 }
79072805 2030 SvPV_set(sv, s);
e1ec3a88 2031 SvLEN_set(sv, newlen);
79072805
LW
2032 }
2033 return s;
2034}
2035
954c1994
GS
2036/*
2037=for apidoc sv_setiv
2038
645c22ef
DM
2039Copies an integer into the given SV, upgrading first if necessary.
2040Does not handle 'set' magic. See also C<sv_setiv_mg>.
954c1994
GS
2041
2042=cut
2043*/
2044
79072805 2045void
864dbfa3 2046Perl_sv_setiv(pTHX_ register SV *sv, IV i)
79072805 2047{
765f542d 2048 SV_CHECK_THINKFIRST_COW_DROP(sv);
463ee0b2
LW
2049 switch (SvTYPE(sv)) {
2050 case SVt_NULL:
79072805 2051 sv_upgrade(sv, SVt_IV);
463ee0b2
LW
2052 break;
2053 case SVt_NV:
2054 sv_upgrade(sv, SVt_PVNV);
2055 break;
ed6116ce 2056 case SVt_RV:
463ee0b2 2057 case SVt_PV:
79072805 2058 sv_upgrade(sv, SVt_PVIV);
463ee0b2 2059 break;
a0d0e21e
LW
2060
2061 case SVt_PVGV:
a0d0e21e
LW
2062 case SVt_PVAV:
2063 case SVt_PVHV:
2064 case SVt_PVCV:
2065 case SVt_PVFM:
2066 case SVt_PVIO:
411caa50 2067 Perl_croak(aTHX_ "Can't coerce %s to integer in %s", sv_reftype(sv,0),
53e06cf0 2068 OP_DESC(PL_op));
463ee0b2 2069 }
a0d0e21e 2070 (void)SvIOK_only(sv); /* validate number */
45977657 2071 SvIV_set(sv, i);
463ee0b2 2072 SvTAINT(sv);
79072805
LW
2073}
2074
954c1994
GS
2075/*
2076=for apidoc sv_setiv_mg
2077
2078Like C<sv_setiv>, but also handles 'set' magic.
2079
2080=cut
2081*/
2082
79072805 2083void
864dbfa3 2084Perl_sv_setiv_mg(pTHX_ register SV *sv, IV i)
ef50df4b
GS
2085{
2086 sv_setiv(sv,i);
2087 SvSETMAGIC(sv);
2088}
2089
954c1994
GS
2090/*
2091=for apidoc sv_setuv
2092
645c22ef
DM
2093Copies an unsigned integer into the given SV, upgrading first if necessary.
2094Does not handle 'set' magic. See also C<sv_setuv_mg>.
954c1994
GS
2095
2096=cut
2097*/
2098
ef50df4b 2099void
864dbfa3 2100Perl_sv_setuv(pTHX_ register SV *sv, UV u)
55497cff 2101{
55ada374
NC
2102 /* With these two if statements:
2103 u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865
d460ef45 2104
55ada374
NC
2105 without
2106 u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865
d460ef45 2107
55ada374
NC
2108 If you wish to remove them, please benchmark to see what the effect is
2109 */
28e5dec8
JH
2110 if (u <= (UV)IV_MAX) {
2111 sv_setiv(sv, (IV)u);
2112 return;
2113 }
25da4f38
IZ
2114 sv_setiv(sv, 0);
2115 SvIsUV_on(sv);
607fa7f2 2116 SvUV_set(sv, u);
55497cff
PP
2117}
2118
954c1994
GS
2119/*
2120=for apidoc sv_setuv_mg
2121
2122Like C<sv_setuv>, but also handles 'set' magic.
2123
2124=cut
2125*/
2126
55497cff 2127void
864dbfa3 2128Perl_sv_setuv_mg(pTHX_ register SV *sv, UV u)
ef50df4b 2129{
55ada374
NC
2130 /* With these two if statements:
2131 u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865
d460ef45 2132
55ada374
NC
2133 without
2134 u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865
d460ef45 2135
55ada374
NC
2136 If you wish to remove them, please benchmark to see what the effect is
2137 */
28e5dec8
JH
2138 if (u <= (UV)IV_MAX) {
2139 sv_setiv(sv, (IV)u);
2140 } else {
2141 sv_setiv(sv, 0);
2142 SvIsUV_on(sv);
2143 sv_setuv(sv,u);
2144 }
ef50df4b
GS
2145 SvSETMAGIC(sv);
2146}
2147
954c1994
GS
2148/*
2149=for apidoc sv_setnv
2150
645c22ef
DM
2151Copies a double into the given SV, upgrading first if necessary.
2152Does not handle 'set' magic. See also C<sv_setnv_mg>.
954c1994
GS
2153
2154=cut
2155*/
2156
ef50df4b 2157void
65202027 2158Perl_sv_setnv(pTHX_ register SV *sv, NV num)
79072805 2159{
765f542d 2160 SV_CHECK_THINKFIRST_COW_DROP(sv);
a0d0e21e
LW
2161 switch (SvTYPE(sv)) {
2162 case SVt_NULL:
2163 case SVt_IV:
79072805 2164 sv_upgrade(sv, SVt_NV);
a0d0e21e 2165 break;
a0d0e21e
LW
2166 case SVt_RV:
2167 case SVt_PV:
2168 case SVt_PVIV:
79072805 2169 sv_upgrade(sv, SVt_PVNV);
a0d0e21e 2170 break;
827b7e14 2171
a0d0e21e 2172 case SVt_PVGV:
a0d0e21e
LW
2173 case SVt_PVAV:
2174 case SVt_PVHV:
2175 case SVt_PVCV:
2176 case SVt_PVFM:
2177 case SVt_PVIO:
411caa50 2178 Perl_croak(aTHX_ "Can't coerce %s to number in %s", sv_reftype(sv,0),
53e06cf0 2179 OP_NAME(PL_op));
79072805 2180 }
9d6ce603 2181 SvNV_set(sv, num);
a0d0e21e 2182 (void)SvNOK_only(sv); /* validate number */
463ee0b2 2183 SvTAINT(sv);
79072805
LW
2184}
2185
954c1994
GS
2186/*
2187=for apidoc sv_setnv_mg
2188
2189Like C<sv_setnv>, but also handles 'set' magic.
2190
2191=cut
2192*/
2193
ef50df4b 2194void
65202027 2195Perl_sv_setnv_mg(pTHX_ register SV *sv, NV num)
ef50df4b
GS
2196{
2197 sv_setnv(sv,num);
2198 SvSETMAGIC(sv);
2199}
2200
645c22ef
DM
2201/* Print an "isn't numeric" warning, using a cleaned-up,
2202 * printable version of the offending string
2203 */
2204
76e3520e 2205STATIC void
cea2e8a9 2206S_not_a_number(pTHX_ SV *sv)
a0d0e21e 2207{
94463019
JH
2208 SV *dsv;
2209 char tmpbuf[64];
2210 char *pv;
2211
2212 if (DO_UTF8(sv)) {
2213 dsv = sv_2mortal(newSVpv("", 0));
2214 pv = sv_uni_display(dsv, sv, 10, 0);
2215 } else {
2216 char *d = tmpbuf;
2217 char *limit = tmpbuf + sizeof(tmpbuf) - 8;
2218 /* each *s can expand to 4 chars + "...\0",
2219 i.e. need room for 8 chars */
ecdeb87c 2220
94463019
JH
2221 char *s, *end;
2222 for (s = SvPVX(sv), end = s + SvCUR(sv); s < end && d < limit; s++) {
2223 int ch = *s & 0xFF;
2224 if (ch & 128 && !isPRINT_LC(ch)) {
2225 *d++ = 'M';
2226 *d++ = '-';
2227 ch &= 127;
2228 }
2229 if (ch == '\n') {
2230 *d++ = '\\';
2231 *d++ = 'n';
2232 }
2233 else if (ch == '\r') {
2234 *d++ = '\\';
2235 *d++ = 'r';
2236 }
2237 else if (ch == '\f') {
2238 *d++ = '\\';
2239 *d++ = 'f';
2240 }
2241 else if (ch == '\\') {
2242 *d++ = '\\';
2243 *d++ = '\\';
2244 }
2245 else if (ch == '\0') {
2246 *d++ = '\\';
2247 *d++ = '0';
2248 }
2249 else if (isPRINT_LC(ch))
2250 *d++ = ch;
2251 else {
2252 *d++ = '^';
2253 *d++ = toCTRL(ch);
2254 }
2255 }
2256 if (s < end) {
2257 *d++ = '.';
2258 *d++ = '.';
2259 *d++ = '.';
2260 }
2261 *d = '\0';
2262 pv = tmpbuf;
a0d0e21e 2263 }
a0d0e21e 2264
533c011a 2265 if (PL_op)
9014280d 2266 Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
94463019
JH
2267 "Argument \"%s\" isn't numeric in %s", pv,
2268 OP_DESC(PL_op));
a0d0e21e 2269 else
9014280d 2270 Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
94463019 2271 "Argument \"%s\" isn't numeric", pv);
a0d0e21e
LW
2272}
2273
c2988b20
NC
2274/*
2275=for apidoc looks_like_number
2276
645c22ef
DM
2277Test if the content of an SV looks like a number (or is a number).
2278C<Inf> and C<Infinity> are treated as numbers (so will not issue a
2279non-numeric warning), even if your atof() doesn't grok them.
c2988b20
NC
2280
2281=cut
2282*/
2283
2284I32
2285Perl_looks_like_number(pTHX_ SV *sv)
2286{
a3b680e6 2287 register const char *sbegin;
c2988b20
NC
2288 STRLEN len;
2289
2290 if (SvPOK(sv)) {
2291 sbegin = SvPVX(sv);
2292 len = SvCUR(sv);
2293 }
2294 else if (SvPOKp(sv))
2295 sbegin = SvPV(sv, len);
2296 else
e0ab1c0e 2297 return SvFLAGS(sv) & (SVf_NOK|SVp_NOK|SVf_IOK|SVp_IOK);
c2988b20
NC
2298 return grok_number(sbegin, len, NULL);
2299}
25da4f38
IZ
2300
2301/* Actually, ISO C leaves conversion of UV to IV undefined, but
2302 until proven guilty, assume that things are not that bad... */
2303
645c22ef
DM
2304/*
2305 NV_PRESERVES_UV:
2306
2307 As 64 bit platforms often have an NV that doesn't preserve all bits of
28e5dec8
JH
2308 an IV (an assumption perl has been based on to date) it becomes necessary
2309 to remove the assumption that the NV always carries enough precision to
2310 recreate the IV whenever needed, and that the NV is the canonical form.
2311 Instead, IV/UV and NV need to be given equal rights. So as to not lose
645c22ef 2312 precision as a side effect of conversion (which would lead to insanity
28e5dec8
JH
2313 and the dragon(s) in t/op/numconvert.t getting very angry) the intent is
2314 1) to distinguish between IV/UV/NV slots that have cached a valid
2315 conversion where precision was lost and IV/UV/NV slots that have a
2316 valid conversion which has lost no precision
645c22ef 2317 2) to ensure that if a numeric conversion to one form is requested that
28e5dec8
JH
2318 would lose precision, the precise conversion (or differently
2319 imprecise conversion) is also performed and cached, to prevent
2320 requests for different numeric formats on the same SV causing
2321 lossy conversion chains. (lossless conversion chains are perfectly
2322 acceptable (still))
2323
2324
2325 flags are used:
2326 SvIOKp is true if the IV slot contains a valid value
2327 SvIOK is true only if the IV value is accurate (UV if SvIOK_UV true)
2328 SvNOKp is true if the NV slot contains a valid value
2329 SvNOK is true only if the NV value is accurate
2330
2331 so
645c22ef 2332 while converting from PV to NV, check to see if converting that NV to an
28e5dec8
JH
2333 IV(or UV) would lose accuracy over a direct conversion from PV to
2334 IV(or UV). If it would, cache both conversions, return NV, but mark
2335 SV as IOK NOKp (ie not NOK).
2336
645c22ef 2337 While converting from PV to IV, check to see if converting that IV to an
28e5dec8
JH
2338 NV would lose accuracy over a direct conversion from PV to NV. If it
2339 would, cache both conversions, flag similarly.
2340
2341 Before, the SV value "3.2" could become NV=3.2 IV=3 NOK, IOK quite
2342 correctly because if IV & NV were set NV *always* overruled.
645c22ef
DM
2343 Now, "3.2" will become NV=3.2 IV=3 NOK, IOKp, because the flag's meaning
2344 changes - now IV and NV together means that the two are interchangeable:
28e5dec8 2345 SvIVX == (IV) SvNVX && SvNVX == (NV) SvIVX;
d460ef45 2346
645c22ef
DM
2347 The benefit of this is that operations such as pp_add know that if
2348 SvIOK is true for both left and right operands, then integer addition
2349 can be used instead of floating point (for cases where the result won't
2350 overflow). Before, floating point was always used, which could lead to
28e5dec8
JH
2351 loss of precision compared with integer addition.
2352
2353 * making IV and NV equal status should make maths accurate on 64 bit
2354 platforms
2355 * may speed up maths somewhat if pp_add and friends start to use
645c22ef 2356 integers when possible instead of fp. (Hopefully the overhead in
28e5dec8
JH
2357 looking for SvIOK and checking for overflow will not outweigh the
2358 fp to integer speedup)
2359 * will slow down integer operations (callers of SvIV) on "inaccurate"
2360 values, as the change from SvIOK to SvIOKp will cause a call into
2361 sv_2iv each time rather than a macro access direct to the IV slot
2362 * should speed up number->string conversion on integers as IV is
645c22ef 2363 favoured when IV and NV are equally accurate
28e5dec8
JH
2364
2365 ####################################################################
645c22ef
DM
2366 You had better be using SvIOK_notUV if you want an IV for arithmetic:
2367 SvIOK is true if (IV or UV), so you might be getting (IV)SvUV.
2368 On the other hand, SvUOK is true iff UV.
28e5dec8
JH
2369 ####################################################################
2370
645c22ef 2371 Your mileage will vary depending your CPU's relative fp to integer
28e5dec8
JH
2372 performance ratio.
2373*/
2374
2375#ifndef NV_PRESERVES_UV
645c22ef
DM
2376# define IS_NUMBER_UNDERFLOW_IV 1
2377# define IS_NUMBER_UNDERFLOW_UV 2
2378# define IS_NUMBER_IV_AND_UV 2
2379# define IS_NUMBER_OVERFLOW_IV 4
2380# define IS_NUMBER_OVERFLOW_UV 5
2381
2382/* sv_2iuv_non_preserve(): private routine for use by sv_2iv() and sv_2uv() */
28e5dec8
JH
2383
2384/* For sv_2nv these three cases are "SvNOK and don't bother casting" */
2385STATIC int
645c22ef 2386S_sv_2iuv_non_preserve(pTHX_ register SV *sv, I32 numtype)
28e5dec8 2387{
1779d84d 2388 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_2iuv_non '%s', IV=0x%"UVxf" NV=%"NVgf" inttype=%"UVXf"\n", SvPVX(sv), SvIVX(sv), SvNVX(sv), (UV)numtype));
28e5dec8
JH
2389 if (SvNVX(sv) < (NV)IV_MIN) {
2390 (void)SvIOKp_on(sv);
2391 (void)SvNOK_on(sv);
45977657 2392 SvIV_set(sv, IV_MIN);
28e5dec8
JH
2393 return IS_NUMBER_UNDERFLOW_IV;
2394 }
2395 if (SvNVX(sv) > (NV)UV_MAX) {
2396 (void)SvIOKp_on(sv);
2397 (void)SvNOK_on(sv);
2398 SvIsUV_on(sv);
607fa7f2 2399 SvUV_set(sv, UV_MAX);
28e5dec8
JH
2400 return IS_NUMBER_OVERFLOW_UV;
2401 }
c2988b20
NC
2402 (void)SvIOKp_on(sv);
2403 (void)SvNOK_on(sv);
2404 /* Can't use strtol etc to convert this string. (See truth table in
2405 sv_2iv */
2406 if (SvNVX(sv) <= (UV)IV_MAX) {
45977657 2407 SvIV_set(sv, I_V(SvNVX(sv)));
c2988b20
NC
2408 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
2409 SvIOK_on(sv); /* Integer is precise. NOK, IOK */
2410 } else {
2411 /* Integer is imprecise. NOK, IOKp */
2412 }
2413 return SvNVX(sv) < 0 ? IS_NUMBER_UNDERFLOW_UV : IS_NUMBER_IV_AND_UV;
2414 }
2415 SvIsUV_on(sv);
607fa7f2 2416 SvUV_set(sv, U_V(SvNVX(sv)));
c2988b20
NC
2417 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
2418 if (SvUVX(sv) == UV_MAX) {
2419 /* As we know that NVs don't preserve UVs, UV_MAX cannot
2420 possibly be preserved by NV. Hence, it must be overflow.
2421 NOK, IOKp */
2422 return IS_NUMBER_OVERFLOW_UV;
2423 }
2424 SvIOK_on(sv); /* Integer is precise. NOK, UOK */
2425 } else {
2426 /* Integer is imprecise. NOK, IOKp */
28e5dec8 2427 }
c2988b20 2428 return IS_NUMBER_OVERFLOW_IV;
28e5dec8 2429}
645c22ef
DM
2430#endif /* !NV_PRESERVES_UV*/
2431
891f9566
YST
2432/* sv_2iv() is now a macro using Perl_sv_2iv_flags();
2433 * this function provided for binary compatibility only
2434 */
2435
2436IV
2437Perl_sv_2iv(pTHX_ register SV *sv)
2438{
2439 return sv_2iv_flags(sv, SV_GMAGIC);
2440}
2441
645c22ef 2442/*
891f9566 2443=for apidoc sv_2iv_flags
645c22ef 2444
891f9566
YST
2445Return the integer value of an SV, doing any necessary string
2446conversion. If flags includes SV_GMAGIC, does an mg_get() first.
2447Normally used via the C<SvIV(sv)> and C<SvIVx(sv)> macros.
645c22ef
DM
2448
2449=cut
2450*/
28e5dec8 2451
a0d0e21e 2452IV
891f9566 2453Perl_sv_2iv_flags(pTHX_ register SV *sv, I32 flags)
79072805
LW
2454{
2455 if (!sv)
2456 return 0;
8990e307 2457 if (SvGMAGICAL(sv)) {
891f9566
YST
2458 if (flags & SV_GMAGIC)
2459 mg_get(sv);
463ee0b2
LW
2460 if (SvIOKp(sv))
2461 return SvIVX(sv);
748a9306 2462 if (SvNOKp(sv)) {
25da4f38 2463 return I_V(SvNVX(sv));
748a9306 2464 }
36477c24
PP
2465 if (SvPOKp(sv) && SvLEN(sv))
2466 return asIV(sv);
3fe9a6f1 2467 if (!SvROK(sv)) {
d008e5eb 2468 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
d008e5eb 2469 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
29489e7c 2470 report_uninit(sv);
c6ee37c5 2471 }
36477c24 2472 return 0;
3fe9a6f1 2473 }
463ee0b2 2474 }
ed6116ce 2475 if (SvTHINKFIRST(sv)) {
a0d0e21e 2476 if (SvROK(sv)) {
a0d0e21e 2477 SV* tmpstr;
1554e226 2478 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
b4b9a328 2479 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv))))
9e7bc3e8 2480 return SvIV(tmpstr);
56431972 2481 return PTR2IV(SvRV(sv));
a0d0e21e 2482 }
765f542d
NC
2483 if (SvIsCOW(sv)) {
2484 sv_force_normal_flags(sv, 0);
47deb5e7 2485 }
0336b60e 2486 if (SvREADONLY(sv) && !SvOK(sv)) {
0336b60e 2487 if (ckWARN(WARN_UNINITIALIZED))
29489e7c 2488 report_uninit(sv);
ed6116ce
LW
2489 return 0;
2490 }
79072805 2491 }
25da4f38
IZ
2492 if (SvIOKp(sv)) {
2493 if (SvIsUV(sv)) {
2494 return (IV)(SvUVX(sv));
2495 }
2496 else {
2497 return SvIVX(sv);
2498 }
463ee0b2 2499 }
748a9306 2500 if (SvNOKp(sv)) {
28e5dec8
JH
2501 /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv
2502 * without also getting a cached IV/UV from it at the same time
2503 * (ie PV->NV conversion should detect loss of accuracy and cache
2504 * IV or UV at same time to avoid this. NWC */
25da4f38
IZ
2505
2506 if (SvTYPE(sv) == SVt_NV)
2507 sv_upgrade(sv, SVt_PVNV);
2508
28e5dec8
JH
2509 (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */
2510 /* < not <= as for NV doesn't preserve UV, ((NV)IV_MAX+1) will almost
2511 certainly cast into the IV range at IV_MAX, whereas the correct
2512 answer is the UV IV_MAX +1. Hence < ensures that dodgy boundary
2513 cases go to UV */
2514 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
45977657 2515 SvIV_set(sv, I_V(SvNVX(sv)));
28e5dec8
JH
2516 if (SvNVX(sv) == (NV) SvIVX(sv)
2517#ifndef NV_PRESERVES_UV
2518 && (((UV)1 << NV_PRESERVES_UV_BITS) >
2519 (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv)))
2520 /* Don't flag it as "accurately an integer" if the number
2521 came from a (by definition imprecise) NV operation, and
2522 we're outside the range of NV integer precision */
2523#endif
2524 ) {
2525 SvIOK_on(sv); /* Can this go wrong with rounding? NWC */
2526 DEBUG_c(PerlIO_printf(Perl_debug_log,
7234c960 2527 "0x%"UVxf" iv(%"NVgf" => %"IVdf") (precise)\n",
28e5dec8
JH
2528 PTR2UV(sv),
2529 SvNVX(sv),
2530 SvIVX(sv)));
2531
2532 } else {
2533 /* IV not precise. No need to convert from PV, as NV
2534 conversion would already have cached IV if it detected
2535 that PV->IV would be better than PV->NV->IV
2536 flags already correct - don't set public IOK. */
2537 DEBUG_c(PerlIO_printf(Perl_debug_log,
7234c960 2538 "0x%"UVxf" iv(%"NVgf" => %"IVdf") (imprecise)\n",
28e5dec8
JH
2539 PTR2UV(sv),
2540 SvNVX(sv),
2541 SvIVX(sv)));
2542 }
2543 /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN,
2544 but the cast (NV)IV_MIN rounds to a the value less (more
2545 negative) than IV_MIN which happens to be equal to SvNVX ??
2546 Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and
2547 NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and
2548 (NV)UVX == NVX are both true, but the values differ. :-(
2549 Hopefully for 2s complement IV_MIN is something like
2550 0x8000000000000000 which will be exact. NWC */
d460ef45 2551 }
25da4f38 2552 else {
607fa7f2 2553 SvUV_set(sv, U_V(SvNVX(sv)));
28e5dec8
JH
2554 if (
2555 (SvNVX(sv) == (NV) SvUVX(sv))
2556#ifndef NV_PRESERVES_UV
2557 /* Make sure it's not 0xFFFFFFFFFFFFFFFF */
2558 /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */
2559 && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv))
2560 /* Don't flag it as "accurately an integer" if the number
2561 came from a (by definition imprecise) NV operation, and
2562 we're outside the range of NV integer precision */
2563#endif
2564 )
2565 SvIOK_on(sv);
25da4f38
IZ
2566 SvIsUV_on(sv);
2567 ret_iv_max:
1c846c1f 2568 DEBUG_c(PerlIO_printf(Perl_debug_log,
57def98f 2569 "0x%"UVxf" 2iv(%"UVuf" => %"IVdf") (as unsigned)\n",
56431972 2570 PTR2UV(sv),
57def98f
JH
2571 SvUVX(sv),
2572 SvUVX(sv)));
25da4f38
IZ
2573 return (IV)SvUVX(sv);
2574 }
748a9306
LW
2575 }
2576 else if (SvPOKp(sv) && SvLEN(sv)) {
c2988b20 2577 UV value;
f54cb97a 2578 const int numtype = grok_number(SvPVX(sv), SvCUR(sv), &value);
25da4f38
IZ
2579 /* We want to avoid a possible problem when we cache an IV which
2580 may be later translated to an NV, and the resulting NV is not
c2988b20
NC
2581 the same as the direct translation of the initial string
2582 (eg 123.456 can shortcut to the IV 123 with atol(), but we must
2583 be careful to ensure that the value with the .456 is around if the
2584 NV value is requested in the future).
1c846c1f 2585
25da4f38
IZ
2586 This means that if we cache such an IV, we need to cache the
2587 NV as well. Moreover, we trade speed for space, and do not
28e5dec8 2588 cache the NV if we are sure it's not needed.
25da4f38 2589 */
16b7a9a4 2590
c2988b20
NC
2591 /* SVt_PVNV is one higher than SVt_PVIV, hence this order */
2592 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2593 == IS_NUMBER_IN_UV) {
5e045b90 2594 /* It's definitely an integer, only upgrade to PVIV */
28e5dec8
JH
2595 if (SvTYPE(sv) < SVt_PVIV)
2596 sv_upgrade(sv, SVt_PVIV);
f7bbb42a 2597 (void)SvIOK_on(sv);
c2988b20
NC
2598 } else if (SvTYPE(sv) < SVt_PVNV)
2599 sv_upgrade(sv, SVt_PVNV);
28e5dec8 2600
c2988b20
NC
2601 /* If NV preserves UV then we only use the UV value if we know that
2602 we aren't going to call atof() below. If NVs don't preserve UVs
2603 then the value returned may have more precision than atof() will
2604 return, even though value isn't perfectly accurate. */
2605 if ((numtype & (IS_NUMBER_IN_UV
2606#ifdef NV_PRESERVES_UV
2607 | IS_NUMBER_NOT_INT
2608#endif
2609 )) == IS_NUMBER_IN_UV) {
2610 /* This won't turn off the public IOK flag if it was set above */
2611 (void)SvIOKp_on(sv);
2612
2613 if (!(numtype & IS_NUMBER_NEG)) {
2614 /* positive */;
2615 if (value <= (UV)IV_MAX) {
45977657 2616 SvIV_set(sv, (IV)value);
c2988b20 2617 } else {
607fa7f2 2618 SvUV_set(sv, value);
c2988b20
NC
2619 SvIsUV_on(sv);
2620 }
2621 } else {
2622 /* 2s complement assumption */
2623 if (value <= (UV)IV_MIN) {
45977657 2624 SvIV_set(sv, -(IV)value);
c2988b20
NC
2625 } else {
2626 /* Too negative for an IV. This is a double upgrade, but
d1be9408 2627 I'm assuming it will be rare. */
c2988b20
NC
2628 if (SvTYPE(sv) < SVt_PVNV)
2629 sv_upgrade(sv, SVt_PVNV);
2630 SvNOK_on(sv);
2631 SvIOK_off(sv);
2632 SvIOKp_on(sv);
9d6ce603 2633 SvNV_set(sv, -(NV)value);
45977657 2634 SvIV_set(sv, IV_MIN);
c2988b20
NC
2635 }
2636 }
2637 }
2638 /* For !NV_PRESERVES_UV and IS_NUMBER_IN_UV and IS_NUMBER_NOT_INT we
2639 will be in the previous block to set the IV slot, and the next
2640 block to set the NV slot. So no else here. */
2641
2642 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2643 != IS_NUMBER_IN_UV) {
2644 /* It wasn't an (integer that doesn't overflow the UV). */
9d6ce603 2645 SvNV_set(sv, Atof(SvPVX(sv)));
28e5dec8 2646
c2988b20
NC
2647 if (! numtype && ckWARN(WARN_NUMERIC))
2648 not_a_number(sv);
28e5dec8 2649
65202027 2650#if defined(USE_LONG_DOUBLE)
c2988b20
NC
2651 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%" PERL_PRIgldbl ")\n",
2652 PTR2UV(sv), SvNVX(sv)));
65202027 2653#else
1779d84d 2654 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"NVgf")\n",
c2988b20 2655 PTR2UV(sv), SvNVX(sv)));
65202027 2656#endif
28e5dec8
JH
2657
2658
2659#ifdef NV_PRESERVES_UV
c2988b20
NC
2660 (void)SvIOKp_on(sv);
2661 (void)SvNOK_on(sv);
2662 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
45977657 2663 SvIV_set(sv, I_V(SvNVX(sv)));
c2988b20
NC
2664 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
2665 SvIOK_on(sv);
28e5dec8 2666 } else {
c2988b20
NC
2667 /* Integer is imprecise. NOK, IOKp */
2668 }
2669 /* UV will not work better than IV */
2670 } else {
2671 if (SvNVX(sv) > (NV)UV_MAX) {
2672 SvIsUV_on(sv);
2673 /* Integer is inaccurate. NOK, IOKp, is UV */
607fa7f2 2674 SvUV_set(sv, UV_MAX);
c2988b20
NC
2675 SvIsUV_on(sv);
2676 } else {
607fa7f2 2677 SvUV_set(sv, U_V(SvNVX(sv)));
c2988b20
NC
2678 /* 0xFFFFFFFFFFFFFFFF not an issue in here */
2679 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
2680 SvIOK_on(sv);
28e5dec8
JH
2681 SvIsUV_on(sv);
2682 } else {
c2988b20
NC
2683 /* Integer is imprecise. NOK, IOKp, is UV */
2684 SvIsUV_on(sv);
28e5dec8 2685 }
28e5dec8 2686 }
c2988b20
NC
2687 goto ret_iv_max;
2688 }
28e5dec8 2689#else /* NV_PRESERVES_UV */
c2988b20
NC
2690 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2691 == (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) {
2692 /* The IV slot will have been set from value returned by
2693 grok_number above. The NV slot has just been set using
2694 Atof. */
560b0c46 2695 SvNOK_on(sv);
c2988b20
NC
2696 assert (SvIOKp(sv));
2697 } else {
2698 if (((UV)1 << NV_PRESERVES_UV_BITS) >
2699 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
2700 /* Small enough to preserve all bits. */
2701 (void)SvIOKp_on(sv);
2702 SvNOK_on(sv);
45977657 2703 SvIV_set(sv, I_V(SvNVX(sv)));
c2988b20
NC
2704 if ((NV)(SvIVX(sv)) == SvNVX(sv))
2705 SvIOK_on(sv);
2706 /* Assumption: first non-preserved integer is < IV_MAX,
2707 this NV is in the preserved range, therefore: */
2708 if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))
2709 < (UV)IV_MAX)) {
32fdb065 2710 Perl_croak(aTHX_ "sv_2iv assumed (U_V(fabs((double)SvNVX(sv))) < (UV)IV_MAX) but SvNVX(sv)=%"NVgf" U_V is 0x%"UVxf", IV_MAX is 0x%"UVxf"\n", SvNVX(sv), U_V(SvNVX(sv)), (UV)IV_MAX);
c2988b20
NC
2711 }
2712 } else {
2713 /* IN_UV NOT_INT
2714 0 0 already failed to read UV.
2715 0 1 already failed to read UV.
2716 1 0 you won't get here in this case. IV/UV
2717 slot set, public IOK, Atof() unneeded.
2718 1 1 already read UV.
2719 so there's no point in sv_2iuv_non_preserve() attempting
2720 to use atol, strtol, strtoul etc. */
2721 if (sv_2iuv_non_preserve (sv, numtype)
2722 >= IS_NUMBER_OVERFLOW_IV)
2723 goto ret_iv_max;
2724 }
2725 }
28e5dec8 2726#endif /* NV_PRESERVES_UV */
25da4f38 2727 }
28e5dec8 2728 } else {
599cee73 2729 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
29489e7c 2730 report_uninit(sv);
25da4f38
IZ
2731 if (SvTYPE(sv) < SVt_IV)
2732 /* Typically the caller expects that sv_any is not NULL now. */
2733 sv_upgrade(sv, SVt_IV);
a0d0e21e 2734 return 0;
79072805 2735 }
1d7c1841
GS
2736 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"IVdf")\n",
2737 PTR2UV(sv),SvIVX(sv)));
25da4f38 2738 return SvIsUV(sv) ? (IV)SvUVX(sv) : SvIVX(sv);
79072805
LW
2739}
2740
891f9566
YST
2741/* sv_2uv() is now a macro using Perl_sv_2uv_flags();
2742 * this function provided for binary compatibility only
2743 */
2744
2745UV
2746Perl_sv_2uv(pTHX_ register SV *sv)
2747{
2748 return sv_2uv_flags(sv, SV_GMAGIC);
2749}
2750
645c22ef 2751/*
891f9566 2752=for apidoc sv_2uv_flags
645c22ef
DM
2753
2754Return the unsigned integer value of an SV, doing any necessary string
891f9566
YST
2755conversion. If flags includes SV_GMAGIC, does an mg_get() first.
2756Normally used via the C<SvUV(sv)> and C<SvUVx(sv)> macros.
645c22ef
DM
2757
2758=cut
2759*/
2760
ff68c719 2761UV
891f9566 2762Perl_sv_2uv_flags(pTHX_ register SV *sv, I32 flags)
ff68c719
PP
2763{
2764 if (!sv)
2765 return 0;
2766 if (SvGMAGICAL(sv)) {
891f9566
YST
2767 if (flags & SV_GMAGIC)
2768 mg_get(sv);
ff68c719
PP
2769 if (SvIOKp(sv))
2770 return SvUVX(sv);
2771 if (SvNOKp(sv))
2772 return U_V(SvNVX(sv));
36477c24
PP
2773 if (SvPOKp(sv) && SvLEN(sv))
2774 return asUV(sv);
3fe9a6f1 2775 if (!SvROK(sv)) {
d008e5eb 2776 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
d008e5eb 2777 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
29489e7c 2778 report_uninit(sv);
c6ee37c5 2779 }
36477c24 2780 return 0;
3fe9a6f1 2781 }
ff68c719
PP
2782 }
2783 if (SvTHINKFIRST(sv)) {
2784 if (SvROK(sv)) {
ff68c719 2785 SV* tmpstr;
1554e226 2786 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
b4b9a328 2787 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv))))
9e7bc3e8 2788 return SvUV(tmpstr);
56431972 2789 return PTR2UV(SvRV(sv));
ff68c719 2790 }
765f542d
NC
2791 if (SvIsCOW(sv)) {
2792 sv_force_normal_flags(sv, 0);
8a818333 2793 }
0336b60e 2794 if (SvREADONLY(sv) && !SvOK(sv)) {
0336b60e 2795 if (ckWARN(WARN_UNINITIALIZED))
29489e7c 2796 report_uninit(sv);
ff68c719
PP
2797 return 0;
2798 }
2799 }
25da4f38
IZ
2800 if (SvIOKp(sv)) {
2801 if (SvIsUV(sv)) {
2802 return SvUVX(sv);
2803 }
2804 else {
2805 return (UV)SvIVX(sv);
2806 }
ff68c719
PP
2807 }
2808 if (SvNOKp(sv)) {
28e5dec8
JH
2809 /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv
2810 * without also getting a cached IV/UV from it at the same time
2811 * (ie PV->NV conversion should detect loss of accuracy and cache
2812 * IV or UV at same time to avoid this. */
2813 /* IV-over-UV optimisation - choose to cache IV if possible */
2814
25da4f38
IZ
2815 if (SvTYPE(sv) == SVt_NV)
2816 sv_upgrade(sv, SVt_PVNV);
28e5dec8
JH
2817
2818 (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */
2819 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
45977657 2820 SvIV_set(sv, I_V(SvNVX(sv)));
28e5dec8
JH
2821 if (SvNVX(sv) == (NV) SvIVX(sv)
2822#ifndef NV_PRESERVES_UV
2823 && (((UV)1 << NV_PRESERVES_UV_BITS) >
2824 (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv)))
2825 /* Don't flag it as "accurately an integer" if the number
2826 came from a (by definition imprecise) NV operation, and
2827 we're outside the range of NV integer precision */
2828#endif
2829 ) {
2830 SvIOK_on(sv); /* Can this go wrong with rounding? NWC */
2831 DEBUG_c(PerlIO_printf(Perl_debug_log,
7234c960 2832 "0x%"UVxf" uv(%"NVgf" => %"IVdf") (precise)\n",
28e5dec8
JH
2833 PTR2UV(sv),
2834 SvNVX(sv),
2835 SvIVX(sv)));
2836
2837 } else {
2838 /* IV not precise. No need to convert from PV, as NV
2839 conversion would already have cached IV if it detected
2840 that PV->IV would be better than PV->NV->IV
2841 flags already correct - don't set public IOK. */
2842 DEBUG_c(PerlIO_printf(Perl_debug_log,
7234c960 2843 "0x%"UVxf" uv(%"NVgf" => %"IVdf") (imprecise)\n",
28e5dec8
JH
2844 PTR2UV(sv),
2845 SvNVX(sv),
2846 SvIVX(sv)));
2847 }
2848 /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN,
2849 but the cast (NV)IV_MIN rounds to a the value less (more
2850 negative) than IV_MIN which happens to be equal to SvNVX ??
2851 Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and
2852 NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and
2853 (NV)UVX == NVX are both true, but the values differ. :-(
2854 Hopefully for 2s complement IV_MIN is something like
2855 0x8000000000000000 which will be exact. NWC */
d460ef45 2856 }
28e5dec8 2857 else {
607fa7f2 2858 SvUV_set(sv, U_V(SvNVX(sv)));
28e5dec8
JH
2859 if (
2860 (SvNVX(sv) == (NV) SvUVX(sv))
2861#ifndef NV_PRESERVES_UV
2862 /* Make sure it's not 0xFFFFFFFFFFFFFFFF */
2863 /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */
2864 && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv))
2865 /* Don't flag it as "accurately an integer" if the number
2866 came from a (by definition imprecise) NV operation, and
2867 we're outside the range of NV integer precision */
2868#endif
2869 )
2870 SvIOK_on(sv);
2871 SvIsUV_on(sv);
1c846c1f 2872 DEBUG_c(PerlIO_printf(Perl_debug_log,
28e5dec8 2873 "0x%"UVxf" 2uv(%"UVuf" => %"IVdf") (as unsigned)\n",
57def98f 2874 PTR2UV(sv),
28e5dec8
JH
2875 SvUVX(sv),
2876 SvUVX(sv)));
25da4f38 2877 }
ff68c719
PP
2878 }
2879 else if (SvPOKp(sv) && SvLEN(sv)) {
c2988b20 2880 UV value;
f54cb97a 2881 const int numtype = grok_number(SvPVX(sv), SvCUR(sv), &value);
25da4f38
IZ
2882
2883 /* We want to avoid a possible problem when we cache a UV which
2884 may be later translated to an NV, and the resulting NV is not
2885 the translation of the initial data.
1c846c1f 2886
25da4f38
IZ
2887 This means that if we cache such a UV, we need to cache the
2888 NV as well. Moreover, we trade speed for space, and do not
2889 cache the NV if not needed.
2890 */
16b7a9a4 2891
c2988b20
NC
2892 /* SVt_PVNV is one higher than SVt_PVIV, hence this order */
2893 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2894 == IS_NUMBER_IN_UV) {
5e045b90 2895 /* It's definitely an integer, only upgrade to PVIV */
28e5dec8 2896 if (SvTYPE(sv) < SVt_PVIV)
f7bbb42a
JH
2897 sv_upgrade(sv, SVt_PVIV);
2898 (void)SvIOK_on(sv);
c2988b20
NC
2899 } else if (SvTYPE(sv) < SVt_PVNV)
2900 sv_upgrade(sv, SVt_PVNV);
d460ef45 2901
c2988b20
NC
2902 /* If NV preserves UV then we only use the UV value if we know that
2903 we aren't going to call atof() below. If NVs don't preserve UVs
2904 then the value returned may have more precision than atof() will
2905 return, even though it isn't accurate. */
2906 if ((numtype & (IS_NUMBER_IN_UV
2907#ifdef NV_PRESERVES_UV
2908 | IS_NUMBER_NOT_INT
2909#endif
2910 )) == IS_NUMBER_IN_UV) {
2911 /* This won't turn off the public IOK flag if it was set above */
2912 (void)SvIOKp_on(sv);
2913
2914 if (!(numtype & IS_NUMBER_NEG)) {
2915 /* positive */;
2916 if (value <= (UV)IV_MAX) {
45977657 2917 SvIV_set(sv, (IV)value);
28e5dec8
JH
2918 } else {
2919 /* it didn't overflow, and it was positive. */
607fa7f2 2920 SvUV_set(sv, value);
28e5dec8
JH
2921 SvIsUV_on(sv);
2922 }
c2988b20
NC
2923 } else {
2924 /* 2s complement assumption */
2925 if (value <= (UV)IV_MIN) {
45977657 2926 SvIV_set(sv, -(IV)value);
c2988b20
NC
2927 } else {
2928 /* Too negative for an IV. This is a double upgrade, but
d1be9408 2929 I'm assuming it will be rare. */
c2988b20
NC
2930 if (SvTYPE(sv) < SVt_PVNV)
2931 sv_upgrade(sv, SVt_PVNV);
2932 SvNOK_on(sv);
2933 SvIOK_off(sv);
2934 SvIOKp_on(sv);
9d6ce603 2935 SvNV_set(sv, -(NV)value);
45977657 2936 SvIV_set(sv, IV_MIN);
c2988b20
NC
2937 }
2938 }
2939 }
2940
2941 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2942 != IS_NUMBER_IN_UV) {
2943 /* It wasn't an integer, or it overflowed the UV. */
9d6ce603 2944 SvNV_set(sv, Atof(SvPVX(sv)));
28e5dec8 2945
c2988b20 2946 if (! numtype && ckWARN(WARN_NUMERIC))
28e5dec8
JH
2947 not_a_number(sv);
2948
2949#if defined(USE_LONG_DOUBLE)
c2988b20
NC
2950 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%" PERL_PRIgldbl ")\n",
2951 PTR2UV(sv), SvNVX(sv)));
28e5dec8 2952#else
1779d84d 2953 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"NVgf")\n",
c2988b20 2954 PTR2UV(sv), SvNVX(sv)));
28e5dec8
JH
2955#endif
2956
2957#ifdef NV_PRESERVES_UV
c2988b20
NC
2958 (void)SvIOKp_on(sv);
2959 (void)SvNOK_on(sv);
2960 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
45977657 2961 SvIV_set(sv, I_V(SvNVX(sv)));
c2988b20
NC
2962 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
2963 SvIOK_on(sv);
2964 } else {
2965 /* Integer is imprecise. NOK, IOKp */
2966 }
2967 /* UV will not work better than IV */
2968 } else {
2969 if (SvNVX(sv) > (NV)UV_MAX) {
2970 SvIsUV_on(sv);
2971 /* Integer is inaccurate. NOK, IOKp, is UV */
607fa7f2 2972 SvUV_set(sv, UV_MAX);
c2988b20
NC
2973 SvIsUV_on(sv);
2974 } else {
607fa7f2 2975 SvUV_set(sv, U_V(SvNVX(sv)));
c2988b20
NC
2976 /* 0xFFFFFFFFFFFFFFFF not an issue in here, NVs
2977 NV preservse UV so can do correct comparison. */
2978 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
2979 SvIOK_on(sv);
2980 SvIsUV_on(sv);
2981 } else {
2982 /* Integer is imprecise. NOK, IOKp, is UV */
2983 SvIsUV_on(sv);
2984 }
2985 }
2986 }
28e5dec8 2987#else /* NV_PRESERVES_UV */
c2988b20
NC
2988 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2989 == (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) {
2990 /* The UV slot will have been set from value returned by
2991 grok_number above. The NV slot has just been set using
2992 Atof. */
560b0c46 2993 SvNOK_on(sv);
c2988b20
NC
2994 assert (SvIOKp(sv));
2995 } else {
2996 if (((UV)1 << NV_PRESERVES_UV_BITS) >
2997 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
2998 /* Small enough to preserve all bits. */
2999 (void)SvIOKp_on(sv);
3000 SvNOK_on(sv);
45977657 3001 SvIV_set(sv, I_V(SvNVX(sv)));
c2988b20
NC
3002 if ((NV)(SvIVX(sv)) == SvNVX(sv))
3003 SvIOK_on(sv);
3004 /* Assumption: first non-preserved integer is < IV_MAX,
3005 this NV is in the preserved range, therefore: */
3006 if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))
3007 < (UV)IV_MAX)) {
32fdb065 3008 Perl_croak(aTHX_ "sv_2uv assumed (U_V(fabs((double)SvNVX(sv))) < (UV)IV_MAX) but SvNVX(sv)=%"NVgf" U_V is 0x%"UVxf", IV_MAX is 0x%"UVxf"\n", SvNVX(sv), U_V(SvNVX(sv)), (UV)IV_MAX);
c2988b20
NC
3009 }
3010 } else
3011 sv_2iuv_non_preserve (sv, numtype);
3012 }
28e5dec8 3013#endif /* NV_PRESERVES_UV */
f7bbb42a 3014 }
ff68c719
PP
3015 }
3016 else {
d008e5eb 3017 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
d008e5eb 3018 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
29489e7c 3019 report_uninit(sv);
c6ee37c5 3020 }
25da4f38
IZ
3021 if (SvTYPE(sv) < SVt_IV)
3022 /* Typically the caller expects that sv_any is not NULL now. */
3023 sv_upgrade(sv, SVt_IV);
ff68c719
PP
3024 return 0;
3025 }
25da4f38 3026
1d7c1841
GS
3027 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"UVuf")\n",
3028 PTR2UV(sv),SvUVX(sv)));
25da4f38 3029 return SvIsUV(sv) ? SvUVX(sv) : (UV)SvIVX(sv);
ff68c719
PP
3030}
3031
645c22ef
DM
3032/*
3033=for apidoc sv_2nv
3034
3035Return the num value of an SV, doing any necessary string or integer
3036conversion, magic etc. Normally used via the C<SvNV(sv)> and C<SvNVx(sv)>
3037macros.
3038
3039=cut
3040*/
3041
65202027 3042NV
864dbfa3 3043Perl_sv_2nv(pTHX_ register SV *sv)
79072805
LW
3044{
3045 if (!sv)
3046 return 0.0;
8990e307 3047 if (SvGMAGICAL(sv)) {
463ee0b2
LW
3048 mg_get(sv);
3049 if (SvNOKp(sv))
3050 return SvNVX(sv);
a0d0e21e 3051 if (SvPOKp(sv) && SvLEN(sv)) {
c2988b20
NC
3052 if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) &&
3053 !grok_number(SvPVX(sv), SvCUR(sv), NULL))
a0d0e21e 3054 not_a_number(sv);
097ee67d 3055 return Atof(SvPVX(sv));
a0d0e21e 3056 }
25da4f38 3057 if (SvIOKp(sv)) {
1c846c1f 3058 if (SvIsUV(sv))
65202027 3059 return (NV)SvUVX(sv);
25da4f38 3060 else
65202027 3061 return (NV)SvIVX(sv);
25da4f38 3062 }
16d20bd9 3063 if (!SvROK(sv)) {
d008e5eb 3064 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
d008e5eb 3065 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
29489e7c 3066 report_uninit(sv);
c6ee37c5 3067 }
16d20bd9
AD
3068 return 0;
3069 }
463ee0b2 3070 }
ed6116ce 3071 if (SvTHINKFIRST(sv)) {
a0d0e21e 3072 if (SvROK(sv)) {
a0d0e21e 3073 SV* tmpstr;
1554e226 3074 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
b4b9a328 3075 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv))))
9e7bc3e8 3076 return SvNV(tmpstr);
56431972 3077 return PTR2NV(SvRV(sv));
a0d0e21e 3078 }
765f542d
NC
3079 if (SvIsCOW(sv)) {
3080 sv_force_normal_flags(sv, 0);
8a818333 3081 }
0336b60e 3082 if (SvREADONLY(sv) && !SvOK(sv)) {
599cee73 3083 if (ckWARN(WARN_UNINITIALIZED))
29489e7c 3084 report_uninit(sv);
ed6116ce
LW
3085 return 0.0;
3086 }
79072805
LW
3087 }
3088 if (SvTYPE(sv) < SVt_NV) {
463ee0b2
LW
3089 if (SvTYPE(sv) == SVt_IV)
3090 sv_upgrade(sv, SVt_PVNV);
3091 else
3092 sv_upgrade(sv, SVt_NV);
906f284f 3093#ifdef USE_LONG_DOUBLE
097ee67d 3094 DEBUG_c({
f93f4e46 3095 STORE_NUMERIC_LOCAL_SET_STANDARD();
1d7c1841
GS
3096 PerlIO_printf(Perl_debug_log,
3097 "0x%"UVxf" num(%" PERL_PRIgldbl ")\n",
3098 PTR2UV(sv), SvNVX(sv));
572bbb43
GS
3099 RESTORE_NUMERIC_LOCAL();
3100 });
65202027 3101#else
572bbb43 3102 DEBUG_c({
f93f4e46 3103 STORE_NUMERIC_LOCAL_SET_STANDARD();
1779d84d 3104 PerlIO_printf(Perl_debug_log, "0x%"UVxf" num(%"NVgf")\n",
1d7c1841 3105 PTR2UV(sv), SvNVX(sv));
097ee67d
JH
3106 RESTORE_NUMERIC_LOCAL();
3107 });
572bbb43 3108#endif
79072805
LW
3109 }
3110 else if (SvTYPE(sv) < SVt_PVNV)
3111 sv_upgrade(sv, SVt_PVNV);
59d8ce62
NC
3112 if (SvNOKp(sv)) {
3113 return SvNVX(sv);
61604483 3114 }
59d8ce62 3115 if (SvIOKp(sv)) {
9d6ce603 3116 SvNV_set(sv, SvIsUV(sv) ? (NV)SvUVX(sv) : (NV)SvIVX(sv));
28e5dec8
JH
3117#ifdef NV_PRESERVES_UV
3118 SvNOK_on(sv);
3119#else
3120 /* Only set the public NV OK flag if this NV preserves the IV */
3121 /* Check it's not 0xFFFFFFFFFFFFFFFF */
3122 if (SvIsUV(sv) ? ((SvUVX(sv) != UV_MAX)&&(SvUVX(sv) == U_V(SvNVX(sv))))
3123 : (SvIVX(sv) == I_V(SvNVX(sv))))
3124 SvNOK_on(sv);
3125 else
3126 SvNOKp_on(sv);
3127#endif
93a17b20 3128 }
748a9306 3129 else if (SvPOKp(sv) && SvLEN(sv)) {
c2988b20 3130 UV value;
f54cb97a 3131 const int numtype = grok_number(SvPVX(sv), SvCUR(sv), &value);
c2988b20 3132 if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) && !numtype)
a0d0e21e 3133 not_a_number(sv);
28e5dec8 3134#ifdef NV_PRESERVES_UV
c2988b20
NC
3135 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
3136 == IS_NUMBER_IN_UV) {
5e045b90 3137 /* It's definitely an integer */
9d6ce603 3138 SvNV_set(sv, (numtype & IS_NUMBER_NEG) ? -(NV)value : (NV)value);
c2988b20 3139 } else
9d6ce603 3140 SvNV_set(sv, Atof(SvPVX(sv)));
28e5dec8
JH
3141 SvNOK_on(sv);
3142#else
9d6ce603 3143 SvNV_set(sv, Atof(SvPVX(sv)));
28e5dec8
JH
3144 /* Only set the public NV OK flag if this NV preserves the value in
3145 the PV at least as well as an IV/UV would.
3146 Not sure how to do this 100% reliably. */
3147 /* if that shift count is out of range then Configure's test is
3148 wonky. We shouldn't be in here with NV_PRESERVES_UV_BITS ==
3149 UV_BITS */
3150 if (((UV)1 << NV_PRESERVES_UV_BITS) >
c2988b20 3151 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
28e5dec8 3152 SvNOK_on(sv); /* Definitely small enough to preserve all bits */
c2988b20
NC
3153 } else if (!(numtype & IS_NUMBER_IN_UV)) {
3154 /* Can't use strtol etc to convert this string, so don't try.
3155 sv_2iv and sv_2uv will use the NV to convert, not the PV. */
3156 SvNOK_on(sv);
3157 } else {
3158 /* value has been set. It may not be precise. */
3159 if ((numtype & IS_NUMBER_NEG) && (value > (UV)IV_MIN)) {
3160 /* 2s complement assumption for (UV)IV_MIN */
3161 SvNOK_on(sv); /* Integer is too negative. */
3162 } else {
3163 SvNOKp_on(sv);
3164 SvIOKp_on(sv);
6fa402ec 3165
c2988b20 3166 if (numtype & IS_NUMBER_NEG) {
45977657 3167 SvIV_set(sv, -(IV)value);
c2988b20 3168 } else if (value <= (UV)IV_MAX) {
45977657 3169 SvIV_set(sv, (IV)value);
c2988b20 3170 } else {
607fa7f2 3171 SvUV_set(sv, value);
c2988b20
NC
3172 SvIsUV_on(sv);
3173 }
3174
3175 if (numtype & IS_NUMBER_NOT_INT) {
3176 /* I believe that even if the original PV had decimals,
3177 they are lost beyond the limit of the FP precision.
3178 However, neither is canonical, so both only get p
3179 flags. NWC, 2000/11/25 */
3180 /* Both already have p flags, so do nothing */
3181 } else {
3182 NV nv = SvNVX(sv);
3183 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
3184 if (SvIVX(sv) == I_V(nv)) {
3185 SvNOK_on(sv);
3186 SvIOK_on(sv);
3187 } else {
3188 SvIOK_on(sv);
3189 /* It had no "." so it must be integer. */
3190 }
3191 } else {
3192 /* between IV_MAX and NV(UV_MAX).
3193 Could be slightly > UV_MAX */
6fa402ec 3194
c2988b20
NC
3195 if (numtype & IS_NUMBER_NOT_INT) {
3196 /* UV and NV both imprecise. */
3197 } else {
3198 UV nv_as_uv = U_V(nv);
3199
3200 if (value == nv_as_uv && SvUVX(sv) != UV_MAX) {
3201 SvNOK_on(sv);
3202 SvIOK_on(sv);
3203 } else {
3204 SvIOK_on(sv);
3205 }
3206 }
3207 }
3208 }
3209 }
3210 }
28e5dec8 3211#endif /* NV_PRESERVES_UV */
93a17b20 3212 }
79072805 3213 else {
599cee73 3214 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
29489e7c 3215 report_uninit(sv);
25da4f38
IZ
3216 if (SvTYPE(sv) < SVt_NV)
3217 /* Typically the caller expects that sv_any is not NULL now. */
28e5dec8
JH
3218 /* XXX Ilya implies that this is a bug in callers that assume this
3219 and ideally should be fixed. */
25da4f38 3220 sv_upgrade(sv, SVt_NV);
a0d0e21e 3221 return 0.0;
79072805 3222 }
572bbb43 3223#if defined(USE_LONG_DOUBLE)
097ee67d 3224 DEBUG_c({
f93f4e46 3225 STORE_NUMERIC_LOCAL_SET_STANDARD();
1d7c1841
GS
3226 PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2nv(%" PERL_PRIgldbl ")\n",
3227 PTR2UV(sv), SvNVX(sv));
572bbb43
GS
3228 RESTORE_NUMERIC_LOCAL();
3229 });
65202027 3230#else
572bbb43 3231 DEBUG_c({
f93f4e46 3232 STORE_NUMERIC_LOCAL_SET_STANDARD();
1779d84d 3233 PerlIO_printf(Perl_debug_log, "0x%"UVxf" 1nv(%"NVgf")\n",
1d7c1841 3234 PTR2UV(sv), SvNVX(sv));
097ee67d
JH
3235 RESTORE_NUMERIC_LOCAL();
3236 });
572bbb43 3237#endif
463ee0b2 3238 return SvNVX(sv);
79072805
LW
3239}
3240
645c22ef
DM
3241/* asIV(): extract an integer from the string value of an SV.
3242 * Caller must validate PVX */
3243
76e3520e 3244STATIC IV
cea2e8a9 3245S_asIV(pTHX_ SV *sv)
36477c24 3246{
c2988b20
NC
3247 UV value;
3248 int numtype = grok_number(SvPVX(sv), SvCUR(sv), &value);
3249
3250 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
3251 == IS_NUMBER_IN_UV) {
645c22ef 3252 /* It's definitely an integer */
c2988b20
NC
3253 if (numtype & IS_NUMBER_NEG) {
3254 if (value < (UV)IV_MIN)
3255 return -(IV)value;
3256 } else {
3257 if (value < (UV)IV_MAX)
3258 return (IV)value;
3259 }
3260 }
d008e5eb 3261 if (!numtype) {
d008e5eb
GS
3262 if (ckWARN(WARN_NUMERIC))
3263 not_a_number(sv);
3264 }
c2988b20 3265 return I_V(Atof(SvPVX(sv)));
36477c24
PP
3266}
3267
645c22ef
DM
3268/* asUV(): extract an unsigned integer from the string value of an SV
3269 * Caller must validate PVX */
3270
76e3520e 3271STATIC UV
cea2e8a9 3272S_asUV(pTHX_ SV *sv)
36477c24 3273{
c2988b20
NC
3274 UV value;
3275 int numtype = grok_number(SvPVX(sv), SvCUR(sv), &value);
36477c24 3276
c2988b20
NC
3277 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
3278 == IS_NUMBER_IN_UV) {
645c22ef 3279 /* It's definitely an integer */
6fa402ec 3280 if (!(numtype & IS_NUMBER_NEG))
c2988b20
NC
3281 return value;
3282 }
d008e5eb 3283 if (!numtype) {
d008e5eb
GS
3284 if (ckWARN(WARN_NUMERIC))
3285 not_a_number(sv);
3286 }
097ee67d 3287 return U_V(Atof(SvPVX(sv)));
36477c24
PP
3288}
3289
645c22ef
DM
3290/*
3291=for apidoc sv_2pv_nolen
3292
3293Like C<sv_2pv()>, but doesn't return the length too. You should usually
3294use the macro wrapper C<SvPV_nolen(sv)> instead.
3295=cut
3296*/
3297
79072805 3298char *
864dbfa3 3299Perl_sv_2pv_nolen(pTHX_ register SV *sv)
1fa8b10d
JD
3300{
3301 STRLEN n_a;
3302 return sv_2pv(sv, &n_a);
3303}
3304
645c22ef
DM
3305/* uiv_2buf(): private routine for use by sv_2pv_flags(): print an IV or
3306 * UV as a string towards the end of buf, and return pointers to start and
3307 * end of it.
3308 *
3309 * We assume that buf is at least TYPE_CHARS(UV) long.
3310 */
3311
864dbfa3 3312static char *
25da4f38
IZ
3313uiv_2buf(char *buf, IV iv, UV uv, int is_uv, char **peob)
3314{
25da4f38
IZ
3315 char *ptr = buf + TYPE_CHARS(UV);
3316 char *ebuf = ptr;
3317 int sign;
25da4f38
IZ
3318
3319 if (is_uv)
3320 sign = 0;
3321 else if (iv >= 0) {
3322 uv = iv;
3323 sign = 0;
3324 } else {
3325 uv = -iv;
3326 sign = 1;
3327 }
3328 do {
eb160463 3329 *--ptr = '0' + (char)(uv % 10);
25da4f38
IZ
3330 } while (uv /= 10);
3331 if (sign)
3332 *--ptr = '-';
3333 *peob = ebuf;
3334 return ptr;
3335}
3336
09540bc3
JH
3337/* sv_2pv() is now a macro using Perl_sv_2pv_flags();
3338 * this function provided for binary compatibility only
3339 */
3340
3341char *
3342Perl_sv_2pv(pTHX_ register SV *sv, STRLEN *lp)
3343{
3344 return sv_2pv_flags(sv, lp, SV_GMAGIC);
3345}
3346
645c22ef
DM
3347/*
3348=for apidoc sv_2pv_flags
3349
ff276b08 3350Returns a pointer to the string value of an SV, and sets *lp to its length.
645c22ef
DM
3351If flags includes SV_GMAGIC, does an mg_get() first. Coerces sv to a string
3352if necessary.
3353Normally invoked via the C<SvPV_flags> macro. C<sv_2pv()> and C<sv_2pv_nomg>
3354usually end up here too.
3355
3356=cut
3357*/
3358
8d6d96c1
HS
3359char *
3360Perl_sv_2pv_flags(pTHX_ register SV *sv, STRLEN *lp, I32 flags)
3361{
79072805
LW
3362 register char *s;
3363 int olderrno;
cb50f42d 3364 SV *tsv, *origsv;
25da4f38
IZ
3365 char tbuf[64]; /* Must fit sprintf/Gconvert of longest IV/NV */
3366 char *tmpbuf = tbuf;
79072805 3367
463ee0b2
LW
3368 if (!sv) {
3369 *lp = 0;
73d840c0 3370 return (char *)"";
463ee0b2 3371 }
8990e307 3372 if (SvGMAGICAL(sv)) {
8d6d96c1
HS
3373 if (flags & SV_GMAGIC)
3374 mg_get(sv);
463ee0b2
LW
3375 if (SvPOKp(sv)) {
3376 *lp = SvCUR(sv);
3377 return SvPVX(sv);
3378 }
cf2093f6 3379 if (SvIOKp(sv)) {
1c846c1f 3380 if (SvIsUV(sv))
57def98f 3381 (void)sprintf(tmpbuf,"%"UVuf, (UV)SvUVX(sv));
cf2093f6 3382 else
57def98f 3383 (void)sprintf(tmpbuf,"%"IVdf, (IV)SvIVX(sv));
46fc3d4c 3384 tsv = Nullsv;
a0d0e21e 3385 goto tokensave;
463ee0b2
LW
3386 }
3387 if (SvNOKp(sv)) {
2d4389e4 3388 Gconvert(SvNVX(sv), NV_DIG, 0, tmpbuf);
46fc3d4c 3389 tsv = Nullsv;
a0d0e21e 3390 goto tokensave;
463ee0b2 3391 }
16d20bd9 3392 if (!SvROK(sv)) {
d008e5eb 3393 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
d008e5eb 3394 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
29489e7c 3395 report_uninit(sv);
c6ee37c5 3396 }
16d20bd9 3397 *lp = 0;
73d840c0 3398 return (char *)"";
16d20bd9 3399 }
463ee0b2 3400 }
ed6116ce
LW
3401 if (SvTHINKFIRST(sv)) {
3402 if (SvROK(sv)) {
a0d0e21e 3403 SV* tmpstr;
e1ec3a88 3404 register const char *typestr;
1554e226 3405 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,string)) &&
b4b9a328 3406 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
446eaa42
YST
3407 char *pv = SvPV(tmpstr, *lp);
3408 if (SvUTF8(tmpstr))
3409 SvUTF8_on(sv);
3410 else
3411 SvUTF8_off(sv);
3412 return pv;
3413 }
cb50f42d 3414 origsv = sv;
ed6116ce
LW
3415 sv = (SV*)SvRV(sv);
3416 if (!sv)
e1ec3a88 3417 typestr = "NULLREF";
ed6116ce 3418 else {
f9277f47
IZ
3419 MAGIC *mg;
3420
ed6116ce 3421 switch (SvTYPE(sv)) {
f9277f47
IZ
3422 case SVt_PVMG:
3423 if ( ((SvFLAGS(sv) &
1c846c1f 3424 (SVs_OBJECT|SVf_OK|SVs_GMG|SVs_SMG|SVs_RMG))
faf82a0b 3425 == (SVs_OBJECT|SVs_SMG))
14befaf4 3426 && (mg = mg_find(sv, PERL_MAGIC_qr))) {
e1ec3a88 3427 const regexp *re = (regexp *)mg->mg_obj;
1bd3ad17 3428
2cd61cdb 3429 if (!mg->mg_ptr) {
e1ec3a88 3430 const char *fptr = "msix";
8782bef2
GB
3431 char reflags[6];
3432 char ch;
3433 int left = 0;
3434 int right = 4;
ff385a1b 3435 char need_newline = 0;
eb160463 3436 U16 reganch = (U16)((re->reganch & PMf_COMPILETIME) >> 12);
8782bef2 3437
155aba94 3438 while((ch = *fptr++)) {
8782bef2
GB
3439 if(reganch & 1) {
3440 reflags[left++] = ch;
3441 }
3442 else {
3443 reflags[right--] = ch;
3444 }
3445 reganch >>= 1;
3446 }
3447 if(left != 4) {
3448 reflags[left] = '-';
3449 left = 5;
3450 }
3451
3452 mg->mg_len = re->prelen + 4 + left;
ff385a1b
JF
3453 /*
3454 * If /x was used, we have to worry about a regex
3455 * ending with a comment later being embedded
3456 * within another regex. If so, we don't want this
3457 * regex's "commentization" to leak out to the
3458 * right part of the enclosing regex, we must cap
3459 * it with a newline.
3460 *
3461 * So, if /x was used, we scan backwards from the
3462 * end of the regex. If we find a '#' before we
3463 * find a newline, we need to add a newline
3464 * ourself. If we find a '\n' first (or if we
3465 * don't find '#' or '\n'), we don't need to add
3466 * anything. -jfriedl
3467 */
3468 if (PMf_EXTENDED & re->reganch)
3469 {
e1ec3a88 3470 const char *endptr = re->precomp + re->prelen;
ff385a1b
JF
3471 while (endptr >= re->precomp)
3472 {
e1ec3a88 3473 const char c = *(endptr--);
ff385a1b
JF
3474 if (c == '\n')
3475 break; /* don't need another */
3476 if (c == '#') {
3477 /* we end while in a comment, so we
3478 need a newline */
3479 mg->mg_len++; /* save space for it */
3480 need_newline = 1; /* note to add it */
ab01544f 3481 break;
ff385a1b
JF
3482 }
3483 }
3484 }
3485
8782bef2
GB
3486 New(616, mg->mg_ptr, mg->mg_len + 1 + left, char);
3487 Copy("(?", mg->mg_ptr, 2, char);
3488 Copy(reflags, mg->mg_ptr+2, left, char);
3489 Copy(":", mg->mg_ptr+left+2, 1, char);
3490 Copy(re->precomp, mg->mg_ptr+3+left, re->prelen, char);
ff385a1b
JF
3491 if (need_newline)
3492 mg->mg_ptr[mg->mg_len - 2] = '\n';
1bd3ad17
IZ
3493 mg->mg_ptr[mg->mg_len - 1] = ')';
3494 mg->mg_ptr[mg->mg_len] = 0;
3495 }
3280af22 3496 PL_reginterp_cnt += re->program[0].next_off;
cb50f42d
YST
3497
3498 if (re->reganch & ROPT_UTF8)
3499 SvUTF8_on(origsv);
3500 else
3501 SvUTF8_off(origsv);
1bd3ad17
IZ
3502 *lp = mg->mg_len;
3503 return mg->mg_ptr;
f9277f47
IZ
3504 }
3505 /* Fall through */
ed6116ce
LW
3506 case SVt_NULL:
3507 case SVt_IV:
3508 case SVt_NV:
3509 case SVt_RV:
3510 case SVt_PV:
3511 case SVt_PVIV:
3512 case SVt_PVNV:
e1ec3a88
AL
3513 case SVt_PVBM: typestr = SvROK(sv) ? "REF" : "SCALAR"; break;
3514 case SVt_PVLV: typestr = SvROK(sv) ? "REF"
be65207d
DM
3515 /* tied lvalues should appear to be
3516 * scalars for backwards compatitbility */
3517 : (LvTYPE(sv) == 't' || LvTYPE(sv) == 'T')
3518 ? "SCALAR" : "LVALUE"; break;
e1ec3a88
AL
3519 case SVt_PVAV: typestr = "ARRAY"; break;
3520 case SVt_PVHV: typestr = "HASH"; break;
3521 case SVt_PVCV: typestr = "CODE"; break;
3522 case SVt_PVGV: typestr = "GLOB"; break;
3523 case SVt_PVFM: typestr = "FORMAT"; break;
3524 case SVt_PVIO: typestr = "IO"; break;
3525 default: typestr = "UNKNOWN"; break;
ed6116ce 3526 }
46fc3d4c 3527 tsv = NEWSV(0,0);
a5cb6b62 3528 if (SvOBJECT(sv)) {
bfcb3514 3529 const char *name = HvNAME_get(SvSTASH(sv));
a5cb6b62 3530 Perl_sv_setpvf(aTHX_ tsv, "%s=%s(0x%"UVxf")",
e1ec3a88 3531 name ? name : "__ANON__" , typestr, PTR2UV(sv));
a5cb6b62 3532 }
ed6116ce 3533 else
e1ec3a88 3534 Perl_sv_setpvf(aTHX_ tsv, "%s(0x%"UVxf")", typestr, PTR2UV(sv));
a0d0e21e 3535 goto tokensaveref;
463ee0b2 3536 }
e1ec3a88 3537 *lp = strlen(typestr);
73d840c0 3538 return (char *)typestr;
79072805 3539 }
0336b60e 3540 if (SvREADONLY(sv) && !SvOK(sv)) {
0336b60e 3541 if (ckWARN(WARN_UNINITIALIZED))
29489e7c 3542 report_uninit(sv);
ed6116ce 3543 *lp = 0;
73d840c0 3544 return (char *)"";
79072805 3545 }
79072805 3546 }
28e5dec8
JH
3547 if (SvIOK(sv) || ((SvIOKp(sv) && !SvNOKp(sv)))) {
3548 /* I'm assuming that if both IV and NV are equally valid then
3549 converting the IV is going to be more efficient */
e1ec3a88
AL
3550 const U32 isIOK = SvIOK(sv);
3551 const U32 isUIOK = SvIsUV(sv);
28e5dec8
JH
3552 char buf[TYPE_CHARS(UV)];
3553 char *ebuf, *ptr;
3554
3555 if (SvTYPE(sv) < SVt_PVIV)
3556 sv_upgrade(sv, SVt_PVIV);
3557 if (isUIOK)
3558 ptr = uiv_2buf(buf, 0, SvUVX(sv), 1, &ebuf);
3559 else
3560 ptr = uiv_2buf(buf, SvIVX(sv), 0, 0, &ebuf);
eb160463 3561 SvGROW(sv, (STRLEN)(ebuf - ptr + 1)); /* inlined from sv_setpvn */
28e5dec8
JH
3562 Move(ptr,SvPVX(sv),ebuf - ptr,char);
3563 SvCUR_set(sv, ebuf - ptr);
3564 s = SvEND(sv);
3565 *s = '\0';
3566 if (isIOK)
3567 SvIOK_on(sv);
3568 else
3569 SvIOKp_on(sv);
3570 if (isUIOK)
3571 SvIsUV_on(sv);
3572 }
3573 else if (SvNOKp(sv)) {
79072805
LW
3574 if (SvTYPE(sv) < SVt_PVNV)
3575 sv_upgrade(sv, SVt_PVNV);
1c846c1f 3576 /* The +20 is pure guesswork. Configure test needed. --jhi */
59155cc0 3577 SvGROW(sv, NV_DIG + 20);
463ee0b2 3578 s = SvPVX(sv);
79072805 3579 olderrno = errno; /* some Xenix systems wipe out errno here */
79072805 3580#ifdef apollo
463ee0b2 3581 if (SvNVX(sv) == 0.0)
79072805
LW
3582 (void)strcpy(s,"0");
3583 else
3584#endif /*apollo*/
bbce6d69 3585 {
2d4389e4 3586 Gconvert(SvNVX(sv), NV_DIG, 0, s);
bbce6d69 3587 }
79072805 3588 errno = olderrno;
a0d0e21e
LW
3589#ifdef FIXNEGATIVEZERO
3590 if (*s == '-' && s[1] == '0' && !s[2])
3591 strcpy(s,"0");
3592#endif
79072805
LW
3593 while (*s) s++;
3594#ifdef hcx
3595 if (s[-1] == '.')
46fc3d4c 3596 *--s = '\0';
79072805
LW
3597#endif
3598 }
79072805 3599 else {
0336b60e
IZ
3600 if (ckWARN(WARN_UNINITIALIZED)
3601 && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
29489e7c 3602 report_uninit(sv);
a0d0e21e 3603 *lp = 0;
25da4f38
IZ
3604 if (SvTYPE(sv) < SVt_PV)
3605 /* Typically the caller expects that sv_any is not NULL now. */
3606 sv_upgrade(sv, SVt_PV);
73d840c0 3607 return (char *)"";
79072805 3608 }
463ee0b2
LW
3609 *lp = s - SvPVX(sv);
3610 SvCUR_set(sv, *lp);
79072805 3611 SvPOK_on(sv);
1d7c1841
GS
3612 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n",
3613 PTR2UV(sv),SvPVX(sv)));
463ee0b2 3614 return SvPVX(sv);
a0d0e21e
LW
3615
3616 tokensave:
3617 if (SvROK(sv)) { /* XXX Skip this when sv_pvn_force calls */
3618 /* Sneaky stuff here */
3619
3620 tokensaveref:
46fc3d4c 3621 if (!tsv)
96827780 3622 tsv = newSVpv(tmpbuf, 0);
46fc3d4c
PP
3623 sv_2mortal(tsv);
3624 *lp = SvCUR(tsv);
3625 return SvPVX(tsv);
a0d0e21e
LW
3626 }
3627 else {
27da23d5 3628 dVAR;
a0d0e21e 3629 STRLEN len;
73d840c0 3630 const char *t;
46fc3d4c
PP
3631
3632 if (tsv) {
3633 sv_2mortal(tsv);
3634 t = SvPVX(tsv);
3635 len = SvCUR(tsv);
3636 }
3637 else {
96827780
MB
3638 t = tmpbuf;
3639 len = strlen(tmpbuf);
46fc3d4c 3640 }
a0d0e21e 3641#ifdef FIXNEGATIVEZERO
46fc3d4c
PP
3642 if (len == 2 && t[0] == '-' && t[1] == '0') {
3643 t = "0";
3644 len = 1;
3645 }
a0d0e21e
LW
3646#endif
3647 (void)SvUPGRADE(sv, SVt_PV);
46fc3d4c 3648 *lp = len;
a0d0e21e
LW
3649 s = SvGROW(sv, len + 1);
3650 SvCUR_set(sv, len);
6bf554b4 3651 SvPOKp_on(sv);
e90e2364 3652 return strcpy(s, t);
a0d0e21e 3653 }
463ee0b2
LW
3654}
3655
645c22ef 3656/*
6050d10e
JP
3657=for apidoc sv_copypv
3658
3659Copies a stringified representation of the source SV into the
3660destination SV. Automatically performs any necessary mg_get and
54f0641b 3661coercion of numeric values into strings. Guaranteed to preserve
6050d10e 3662UTF-8 flag even from overloaded objects. Similar in nature to
54f0641b
NIS
3663sv_2pv[_flags] but operates directly on an SV instead of just the
3664string. Mostly uses sv_2pv_flags to do its work, except when that
6050d10e
JP
3665would lose the UTF-8'ness of the PV.
3666
3667=cut
3668*/
3669
3670void
3671Perl_sv_copypv(pTHX_ SV *dsv, register SV *ssv)
3672{
446eaa42
YST
3673 STRLEN len;
3674 char *s;
3675 s = SvPV(ssv,len);
cb50f42d 3676 sv_setpvn(dsv,s,len);
446eaa42 3677 if (SvUTF8(ssv))
cb50f42d 3678 SvUTF8_on(dsv);
446eaa42 3679 else
cb50f42d 3680 SvUTF8_off(dsv);
6050d10e
JP
3681}
3682
3683/*
645c22ef
DM
3684=for apidoc sv_2pvbyte_nolen
3685
3686Return a pointer to the byte-encoded representation of the SV.
1e54db1a 3687May cause the SV to be downgraded from UTF-8 as a side-effect.
645c22ef
DM
3688
3689Usually accessed via the C<SvPVbyte_nolen> macro.
3690
3691=cut
3692*/
3693
7340a771
GS
3694char *
3695Perl_sv_2pvbyte_nolen(pTHX_ register SV *sv)
3696{
560a288e
GS
3697 STRLEN n_a;
3698 return sv_2pvbyte(sv, &n_a);
7340a771
GS
3699}
3700
645c22ef
DM
3701/*
3702=for apidoc sv_2pvbyte
3703
3704Return a pointer to the byte-encoded representation of the SV, and set *lp
1e54db1a 3705to its length. May cause the SV to be downgraded from UTF-8 as a
645c22ef
DM
3706side-effect.
3707
3708Usually accessed via the C<SvPVbyte> macro.
3709
3710=cut
3711*/
3712
7340a771
GS
3713char *
3714Perl_sv_2pvbyte(pTHX_ register SV *sv, STRLEN *lp)
3715{
0875d2fe
NIS
3716 sv_utf8_downgrade(sv,0);
3717 return SvPV(sv,*lp);
7340a771
GS
3718}
3719
645c22ef
DM
3720/*
3721=for apidoc sv_2pvutf8_nolen
3722
1e54db1a
JH
3723Return a pointer to the UTF-8-encoded representation of the SV.
3724May cause the SV to be upgraded to UTF-8 as a side-effect.
645c22ef
DM
3725
3726Usually accessed via the C<SvPVutf8_nolen> macro.
3727
3728=cut
3729*/
3730
7340a771
GS
3731char *
3732Perl_sv_2pvutf8_nolen(pTHX_ register SV *sv)
3733{
560a288e
GS
3734 STRLEN n_a;
3735 return sv_2pvutf8(sv, &n_a);
7340a771
GS
3736}
3737
645c22ef
DM
3738/*
3739=for apidoc sv_2pvutf8
3740
1e54db1a
JH
3741Return a pointer to the UTF-8-encoded representation of the SV, and set *lp
3742to its length. May cause the SV to be upgraded to UTF-8 as a side-effect.
645c22ef
DM
3743
3744Usually accessed via the C<SvPVutf8> macro.
3745
3746=cut
3747*/
3748
7340a771
GS
3749char *
3750Perl_sv_2pvutf8(pTHX_ register SV *sv, STRLEN *lp)
3751{
560a288e 3752 sv_utf8_upgrade(sv);
7d59b7e4 3753 return SvPV(sv,*lp);
7340a771 3754}
1c846c1f 3755
645c22ef
DM
3756/*
3757=for apidoc sv_2bool
3758
3759This function is only called on magical items, and is only used by
8cf8f3d1 3760sv_true() or its macro equivalent.
645c22ef
DM
3761
3762=cut
3763*/
3764
463ee0b2 3765bool
864dbfa3 3766Perl_sv_2bool(pTHX_ register SV *sv)
463ee0b2 3767{
8990e307 3768 if (SvGMAGICAL(sv))
463ee0b2
LW
3769 mg_get(sv);
3770
a0d0e21e
LW
3771 if (!SvOK(sv))
3772 return 0;
3773 if (SvROK(sv)) {
a0d0e21e 3774 SV* tmpsv;
1554e226 3775 if (SvAMAGIC(sv) && (tmpsv=AMG_CALLun(sv,bool_)) &&
9e3013b1 3776 (!SvROK(tmpsv) || (SvRV(tmpsv) != SvRV(sv))))
8a31060d 3777 return (bool)SvTRUE(tmpsv);
a0d0e21e
LW
3778 return SvRV(sv) != 0;
3779 }
463ee0b2 3780 if (SvPOKp(sv)) {
11343788
MB
3781 register XPV* Xpvtmp;
3782 if ((Xpvtmp = (XPV*)SvANY(sv)) &&
7b2c381c 3783 (*sv->sv_u.sv_pv > '0' ||
11343788 3784 Xpvtmp->xpv_cur > 1 ||
7b2c381c 3785 (Xpvtmp->xpv_cur && *sv->sv_u.sv_pv != '0')))
463ee0b2
LW
3786 return 1;
3787 else
3788 return 0;
3789 }
3790 else {
3791 if (SvIOKp(sv))
3792 return SvIVX(sv) != 0;
3793 else {
3794 if (SvNOKp(sv))
3795 return SvNVX(sv) != 0.0;
3796 else
3797 return FALSE;
3798 }
3799 }
79072805
LW
3800}
3801
09540bc3
JH
3802/* sv_utf8_upgrade() is now a macro using sv_utf8_upgrade_flags();
3803 * this function provided for binary compatibility only
3804 */
3805
3806
3807STRLEN
3808Perl_sv_utf8_upgrade(pTHX_ register SV *sv)
3809{
3810 return sv_utf8_upgrade_flags(sv, SV_GMAGIC);
3811}
3812
c461cf8f
JH
3813/*
3814=for apidoc sv_utf8_upgrade
3815
78ea37eb 3816Converts the PV of an SV to its UTF-8-encoded form.
645c22ef 3817Forces the SV to string form if it is not already.
4411f3b6
NIS
3818Always sets the SvUTF8 flag to avoid future validity checks even
3819if all the bytes have hibit clear.
c461cf8f 3820
13a6c0e0
JH
3821This is not as a general purpose byte encoding to Unicode interface:
3822use the Encode extension for that.
3823
8d6d96c1
HS
3824=for apidoc sv_utf8_upgrade_flags
3825
78ea37eb 3826Converts the PV of an SV to its UTF-8-encoded form.
645c22ef 3827Forces the SV to string form if it is not already.
8d6d96c1
HS
3828Always sets the SvUTF8 flag to avoid future validity checks even
3829if all the bytes have hibit clear. If C<flags> has C<SV_GMAGIC> bit set,
3830will C<mg_get> on C<sv> if appropriate, else not. C<sv_utf8_upgrade> and
3831C<sv_utf8_upgrade_nomg> are implemented in terms of this function.
3832
13a6c0e0
JH
3833This is not as a general purpose byte encoding to Unicode interface: