This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Integrate:
[perl5.git] / sv.c
CommitLineData
a0d0e21e 1/* sv.c
79072805 2 *
e6906430 3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
f27f8384 4 * 2000, 2001, 2002, 2003, 2004, 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
645c22ef 26
47185da9
NC
27#ifdef __Lynx__
28/* Missing proto on LynxOS */
29 char *gconvert(double, int, int, char *);
30#endif
31
d6218095
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
645c22ef
DM
50/* ============================================================================
51
52=head1 Allocation and deallocation of SVs.
53
5e045b90
AMS
54An SV (or AV, HV, etc.) is allocated in two parts: the head (struct sv,
55av, hv...) contains type and reference count information, as well as a
56pointer to the body (struct xrv, xpv, xpviv...), which contains fields
57specific to each type.
58
59Normally, this allocation is done using arenas, which are approximately
601K chunks of memory parcelled up into N heads or bodies. The first slot
61in each arena is reserved, and is used to hold a link to the next arena.
62In the case of heads, the unused first slot also contains some flags and
63a note of the number of slots. Snaked through each arena chain is a
64linked list of free items; when this becomes empty, an extra arena is
65allocated and divided up into N items which are threaded into the free
66list.
645c22ef
DM
67
68The following global variables are associated with arenas:
69
70 PL_sv_arenaroot pointer to list of SV arenas
71 PL_sv_root pointer to list of free SV structures
72
73 PL_foo_arenaroot pointer to list of foo arenas,
74 PL_foo_root pointer to list of free foo bodies
75 ... for foo in xiv, xnv, xrv, xpv etc.
76
77Note that some of the larger and more rarely used body types (eg xpvio)
78are not allocated using arenas, but are instead just malloc()/free()ed as
79required. Also, if PURIFY is defined, arenas are abandoned altogether,
80with all items individually malloc()ed. In addition, a few SV heads are
81not allocated from an arena, but are instead directly created as static
82or auto variables, eg PL_sv_undef.
83
84The SV arena serves the secondary purpose of allowing still-live SVs
85to be located and destroyed during final cleanup.
86
87At the lowest level, the macros new_SV() and del_SV() grab and free
88an SV head. (If debugging with -DD, del_SV() calls the function S_del_sv()
89to return the SV to the free list with error checking.) new_SV() calls
90more_sv() / sv_add_arena() to add an extra arena if the free list is empty.
91SVs in the free list have their SvTYPE field set to all ones.
92
93Similarly, there are macros new_XIV()/del_XIV(), new_XNV()/del_XNV() etc
94that allocate and return individual body types. Normally these are mapped
ff276b08
RG
95to the arena-manipulating functions new_xiv()/del_xiv() etc, but may be
96instead mapped directly to malloc()/free() if PURIFY is defined. The
645c22ef
DM
97new/del functions remove from, or add to, the appropriate PL_foo_root
98list, and call more_xiv() etc to add a new arena if the list is empty.
99
ff276b08 100At the time of very final cleanup, sv_free_arenas() is called from
645c22ef
DM
101perl_destruct() to physically free all the arenas allocated since the
102start of the interpreter. Note that this also clears PL_he_arenaroot,
103which is otherwise dealt with in hv.c.
104
105Manipulation of any of the PL_*root pointers is protected by enclosing
106LOCK_SV_MUTEX; ... UNLOCK_SV_MUTEX calls which should Do the Right Thing
107if threads are enabled.
108
109The function visit() scans the SV arenas list, and calls a specified
110function for each SV it finds which is still live - ie which has an SvTYPE
111other than all 1's, and a non-zero SvREFCNT. visit() is used by the
112following functions (specified as [function that calls visit()] / [function
113called by visit() for each SV]):
114
115 sv_report_used() / do_report_used()
116 dump all remaining SVs (debugging aid)
117
118 sv_clean_objs() / do_clean_objs(),do_clean_named_objs()
119 Attempt to free all objects pointed to by RVs,
120 and, unless DISABLE_DESTRUCTOR_KLUDGE is defined,
121 try to do the same for all objects indirectly
122 referenced by typeglobs too. Called once from
123 perl_destruct(), prior to calling sv_clean_all()
124 below.
125
126 sv_clean_all() / do_clean_all()
127 SvREFCNT_dec(sv) each remaining SV, possibly
128 triggering an sv_free(). It also sets the
129 SVf_BREAK flag on the SV to indicate that the
130 refcnt has been artificially lowered, and thus
131 stopping sv_free() from giving spurious warnings
132 about SVs which unexpectedly have a refcnt
133 of zero. called repeatedly from perl_destruct()
134 until there are no SVs left.
135
136=head2 Summary
137
138Private API to rest of sv.c
139
140 new_SV(), del_SV(),
141
142 new_XIV(), del_XIV(),
143 new_XNV(), del_XNV(),
144 etc
145
146Public API:
147
8cf8f3d1 148 sv_report_used(), sv_clean_objs(), sv_clean_all(), sv_free_arenas()
645c22ef
DM
149
150
151=cut
152
153============================================================================ */
154
155
51371543 156
4561caa4
CS
157/*
158 * "A time to plant, and a time to uproot what was planted..."
159 */
160
053fc874
GS
161#define plant_SV(p) \
162 STMT_START { \
163 SvANY(p) = (void *)PL_sv_root; \
164 SvFLAGS(p) = SVTYPEMASK; \
165 PL_sv_root = (p); \
166 --PL_sv_count; \
167 } STMT_END
a0d0e21e 168
fba3b22e 169/* sv_mutex must be held while calling uproot_SV() */
053fc874
GS
170#define uproot_SV(p) \
171 STMT_START { \
172 (p) = PL_sv_root; \
173 PL_sv_root = (SV*)SvANY(p); \
174 ++PL_sv_count; \
175 } STMT_END
176
645c22ef
DM
177
178/* new_SV(): return a new, empty SV head */
179
c240c76d
JH
180#ifdef DEBUG_LEAKING_SCALARS
181/* provide a real function for a debugger to play with */
182STATIC SV*
183S_new_SV(pTHX)
184{
185 SV* sv;
186
187 LOCK_SV_MUTEX;
188 if (PL_sv_root)
189 uproot_SV(sv);
190 else
191 sv = more_sv();
192 UNLOCK_SV_MUTEX;
193 SvANY(sv) = 0;
194 SvREFCNT(sv) = 1;
195 SvFLAGS(sv) = 0;
196 return sv;
197}
198# define new_SV(p) (p)=S_new_SV(aTHX)
199
200#else
201# define new_SV(p) \
053fc874
GS
202 STMT_START { \
203 LOCK_SV_MUTEX; \
204 if (PL_sv_root) \
205 uproot_SV(p); \
206 else \
207 (p) = more_sv(); \
208 UNLOCK_SV_MUTEX; \
209 SvANY(p) = 0; \
210 SvREFCNT(p) = 1; \
211 SvFLAGS(p) = 0; \
212 } STMT_END
c240c76d 213#endif
463ee0b2 214
645c22ef
DM
215
216/* del_SV(): return an empty SV head to the free list */
217
a0d0e21e 218#ifdef DEBUGGING
4561caa4 219
053fc874
GS
220#define del_SV(p) \
221 STMT_START { \
222 LOCK_SV_MUTEX; \
aea4f609 223 if (DEBUG_D_TEST) \
053fc874
GS
224 del_sv(p); \
225 else \
226 plant_SV(p); \
227 UNLOCK_SV_MUTEX; \
228 } STMT_END
a0d0e21e 229
76e3520e 230STATIC void
cea2e8a9 231S_del_sv(pTHX_ SV *p)
463ee0b2 232{
aea4f609 233 if (DEBUG_D_TEST) {
4633a7c4 234 SV* sva;
a0d0e21e
LW
235 SV* sv;
236 SV* svend;
237 int ok = 0;
3280af22 238 for (sva = PL_sv_arenaroot; sva; sva = (SV *) SvANY(sva)) {
4633a7c4
LW
239 sv = sva + 1;
240 svend = &sva[SvREFCNT(sva)];
a0d0e21e
LW
241 if (p >= sv && p < svend)
242 ok = 1;
243 }
244 if (!ok) {
0453d815 245 if (ckWARN_d(WARN_INTERNAL))
9014280d 246 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
b035a42e
NC
247 "Attempt to free non-arena SV: 0x%"UVxf
248 pTHX__FORMAT, PTR2UV(p) pTHX__VALUE);
a0d0e21e
LW
249 return;
250 }
251 }
4561caa4 252 plant_SV(p);
463ee0b2 253}
a0d0e21e 254
4561caa4
CS
255#else /* ! DEBUGGING */
256
257#define del_SV(p) plant_SV(p)
258
259#endif /* DEBUGGING */
463ee0b2 260
645c22ef
DM
261
262/*
ccfc67b7
JH
263=head1 SV Manipulation Functions
264
645c22ef
DM
265=for apidoc sv_add_arena
266
267Given a chunk of memory, link it to the head of the list of arenas,
268and split it into a list of free SVs.
269
270=cut
271*/
272
4633a7c4 273void
864dbfa3 274Perl_sv_add_arena(pTHX_ char *ptr, U32 size, U32 flags)
463ee0b2 275{
4633a7c4 276 SV* sva = (SV*)ptr;
463ee0b2
LW
277 register SV* sv;
278 register SV* svend;
4633a7c4
LW
279
280 /* The first SV in an arena isn't an SV. */
3280af22 281 SvANY(sva) = (void *) PL_sv_arenaroot; /* ptr to next arena */
4633a7c4
LW
282 SvREFCNT(sva) = size / sizeof(SV); /* number of SV slots */
283 SvFLAGS(sva) = flags; /* FAKE if not to be freed */
284
3280af22
NIS
285 PL_sv_arenaroot = sva;
286 PL_sv_root = sva + 1;
4633a7c4
LW
287
288 svend = &sva[SvREFCNT(sva) - 1];
289 sv = sva + 1;
463ee0b2 290 while (sv < svend) {
a0d0e21e 291 SvANY(sv) = (void *)(SV*)(sv + 1);
8b3a4b74 292 SvREFCNT(sv) = 0;
8990e307 293 SvFLAGS(sv) = SVTYPEMASK;
463ee0b2
LW
294 sv++;
295 }
296 SvANY(sv) = 0;
4633a7c4
LW
297 SvFLAGS(sv) = SVTYPEMASK;
298}
299
645c22ef
DM
300/* make some more SVs by adding another arena */
301
fba3b22e 302/* sv_mutex must be held while calling more_sv() */
76e3520e 303STATIC SV*
cea2e8a9 304S_more_sv(pTHX)
4633a7c4 305{
4561caa4
CS
306 register SV* sv;
307
3280af22
NIS
308 if (PL_nice_chunk) {
309 sv_add_arena(PL_nice_chunk, PL_nice_chunk_size, 0);
310 PL_nice_chunk = Nullch;
30ad99e7 311 PL_nice_chunk_size = 0;
c07a80fd 312 }
1edc1566 313 else {
314 char *chunk; /* must use New here to match call to */
315 New(704,chunk,1008,char); /* Safefree() in sv_free_arenas() */
316 sv_add_arena(chunk, 1008, 0);
317 }
4561caa4
CS
318 uproot_SV(sv);
319 return sv;
463ee0b2
LW
320}
321
b035a42e
NC
322/* visit(): call the named function for each non-free SV in the arenas
323 * whose flags field matches the flags/mask args. */
645c22ef 324
5226ed68 325STATIC I32
b035a42e 326S_visit(pTHX_ SVFUNC_t f, U32 flags, U32 mask)
8990e307 327{
4633a7c4 328 SV* sva;
8990e307
LW
329 SV* sv;
330 register SV* svend;
5226ed68 331 I32 visited = 0;
8990e307 332
3280af22 333 for (sva = PL_sv_arenaroot; sva; sva = (SV*)SvANY(sva)) {
4633a7c4 334 svend = &sva[SvREFCNT(sva)];
4561caa4 335 for (sv = sva + 1; sv < svend; ++sv) {
b035a42e
NC
336 if (SvTYPE(sv) != SVTYPEMASK
337 && (sv->sv_flags & mask) == flags
338 && SvREFCNT(sv))
339 {
acfe0abc 340 (FCALL)(aTHX_ sv);
5226ed68
JH
341 ++visited;
342 }
8990e307
LW
343 }
344 }
5226ed68 345 return visited;
8990e307
LW
346}
347
758a08c3
JH
348#ifdef DEBUGGING
349
645c22ef
DM
350/* called by sv_report_used() for each live SV */
351
352static void
acfe0abc 353do_report_used(pTHX_ SV *sv)
645c22ef
DM
354{
355 if (SvTYPE(sv) != SVTYPEMASK) {
356 PerlIO_printf(Perl_debug_log, "****\n");
357 sv_dump(sv);
358 }
359}
758a08c3 360#endif
645c22ef
DM
361
362/*
363=for apidoc sv_report_used
364
365Dump the contents of all SVs not yet freed. (Debugging aid).
366
367=cut
368*/
369
8990e307 370void
864dbfa3 371Perl_sv_report_used(pTHX)
4561caa4 372{
ff270d3a 373#ifdef DEBUGGING
b035a42e 374 visit(do_report_used, 0, 0);
ff270d3a 375#endif
4561caa4
CS
376}
377
645c22ef
DM
378/* called by sv_clean_objs() for each live SV */
379
380static void
acfe0abc 381do_clean_objs(pTHX_ SV *sv)
645c22ef
DM
382{
383 SV* rv;
384
385 if (SvROK(sv) && SvOBJECT(rv = SvRV(sv))) {
386 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning object ref:\n "), sv_dump(sv)));
387 if (SvWEAKREF(sv)) {
388 sv_del_backref(sv);
389 SvWEAKREF_off(sv);
390 SvRV(sv) = 0;
391 } else {
392 SvROK_off(sv);
393 SvRV(sv) = 0;
394 SvREFCNT_dec(rv);
395 }
396 }
397
398 /* XXX Might want to check arrays, etc. */
399}
400
401/* called by sv_clean_objs() for each live SV */
402
403#ifndef DISABLE_DESTRUCTOR_KLUDGE
404static void
acfe0abc 405do_clean_named_objs(pTHX_ SV *sv)
645c22ef
DM
406{
407 if (SvTYPE(sv) == SVt_PVGV && GvGP(sv)) {
408 if ( SvOBJECT(GvSV(sv)) ||
409 (GvAV(sv) && SvOBJECT(GvAV(sv))) ||
410 (GvHV(sv) && SvOBJECT(GvHV(sv))) ||
411 (GvIO(sv) && SvOBJECT(GvIO(sv))) ||
412 (GvCV(sv) && SvOBJECT(GvCV(sv))) )
413 {
414 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning named glob object:\n "), sv_dump(sv)));
1f26b251 415 SvFLAGS(sv) |= SVf_BREAK;
645c22ef
DM
416 SvREFCNT_dec(sv);
417 }
418 }
419}
420#endif
421
422/*
423=for apidoc sv_clean_objs
424
425Attempt to destroy all objects not yet freed
426
427=cut
428*/
429
4561caa4 430void
864dbfa3 431Perl_sv_clean_objs(pTHX)
4561caa4 432{
3280af22 433 PL_in_clean_objs = TRUE;
b035a42e 434 visit(do_clean_objs, SVf_ROK, SVf_ROK);
4561caa4 435#ifndef DISABLE_DESTRUCTOR_KLUDGE
2d0f3c12 436 /* some barnacles may yet remain, clinging to typeglobs */
b035a42e 437 visit(do_clean_named_objs, SVt_PVGV, SVTYPEMASK);
4561caa4 438#endif
3280af22 439 PL_in_clean_objs = FALSE;
4561caa4
CS
440}
441
645c22ef
DM
442/* called by sv_clean_all() for each live SV */
443
444static void
acfe0abc 445do_clean_all(pTHX_ SV *sv)
645c22ef
DM
446{
447 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning loops: SV at 0x%"UVxf"\n", PTR2UV(sv)) ));
448 SvFLAGS(sv) |= SVf_BREAK;
449 SvREFCNT_dec(sv);
450}
451
452/*
453=for apidoc sv_clean_all
454
455Decrement the refcnt of each remaining SV, possibly triggering a
456cleanup. This function may have to be called multiple times to free
ff276b08 457SVs which are in complex self-referential hierarchies.
645c22ef
DM
458
459=cut
460*/
461
5226ed68 462I32
864dbfa3 463Perl_sv_clean_all(pTHX)
8990e307 464{
5226ed68 465 I32 cleaned;
3280af22 466 PL_in_clean_all = TRUE;
b035a42e 467 cleaned = visit(do_clean_all, 0,0);
3280af22 468 PL_in_clean_all = FALSE;
5226ed68 469 return cleaned;
8990e307 470}
463ee0b2 471
645c22ef
DM
472/*
473=for apidoc sv_free_arenas
474
475Deallocate the memory used by all arenas. Note that all the individual SV
476heads and bodies within the arenas must already have been freed.
477
478=cut
479*/
480
4633a7c4 481void
864dbfa3 482Perl_sv_free_arenas(pTHX)
4633a7c4
LW
483{
484 SV* sva;
485 SV* svanext;
612f20c3 486 XPV *arena, *arenanext;
4633a7c4
LW
487
488 /* Free arenas here, but be careful about fake ones. (We assume
489 contiguity of the fake ones with the corresponding real ones.) */
490
3280af22 491 for (sva = PL_sv_arenaroot; sva; sva = svanext) {
4633a7c4
LW
492 svanext = (SV*) SvANY(sva);
493 while (svanext && SvFAKE(svanext))
494 svanext = (SV*) SvANY(svanext);
495
496 if (!SvFAKE(sva))
1edc1566 497 Safefree((void *)sva);
4633a7c4 498 }
5f05dabc 499
612f20c3
GS
500 for (arena = PL_xiv_arenaroot; arena; arena = arenanext) {
501 arenanext = (XPV*)arena->xpv_pv;
502 Safefree(arena);
503 }
504 PL_xiv_arenaroot = 0;
d5aea225 505 PL_xiv_root = 0;
612f20c3
GS
506
507 for (arena = PL_xnv_arenaroot; arena; arena = arenanext) {
508 arenanext = (XPV*)arena->xpv_pv;
509 Safefree(arena);
510 }
511 PL_xnv_arenaroot = 0;
d5aea225 512 PL_xnv_root = 0;
612f20c3
GS
513
514 for (arena = PL_xrv_arenaroot; arena; arena = arenanext) {
515 arenanext = (XPV*)arena->xpv_pv;
516 Safefree(arena);
517 }
518 PL_xrv_arenaroot = 0;
d5aea225 519 PL_xrv_root = 0;
612f20c3
GS
520
521 for (arena = PL_xpv_arenaroot; arena; arena = arenanext) {
522 arenanext = (XPV*)arena->xpv_pv;
523 Safefree(arena);
524 }
525 PL_xpv_arenaroot = 0;
d5aea225 526 PL_xpv_root = 0;
612f20c3
GS
527
528 for (arena = (XPV*)PL_xpviv_arenaroot; arena; arena = arenanext) {
529 arenanext = (XPV*)arena->xpv_pv;
530 Safefree(arena);
531 }
532 PL_xpviv_arenaroot = 0;
d5aea225 533 PL_xpviv_root = 0;
612f20c3
GS
534
535 for (arena = (XPV*)PL_xpvnv_arenaroot; arena; arena = arenanext) {
536 arenanext = (XPV*)arena->xpv_pv;
537 Safefree(arena);
538 }
539 PL_xpvnv_arenaroot = 0;
d5aea225 540 PL_xpvnv_root = 0;
612f20c3
GS
541
542 for (arena = (XPV*)PL_xpvcv_arenaroot; arena; arena = arenanext) {
543 arenanext = (XPV*)arena->xpv_pv;
544 Safefree(arena);
545 }
546 PL_xpvcv_arenaroot = 0;
d5aea225 547 PL_xpvcv_root = 0;
612f20c3
GS
548
549 for (arena = (XPV*)PL_xpvav_arenaroot; arena; arena = arenanext) {
550 arenanext = (XPV*)arena->xpv_pv;
551 Safefree(arena);
552 }
553 PL_xpvav_arenaroot = 0;
d5aea225 554 PL_xpvav_root = 0;
612f20c3
GS
555
556 for (arena = (XPV*)PL_xpvhv_arenaroot; arena; arena = arenanext) {
557 arenanext = (XPV*)arena->xpv_pv;
558 Safefree(arena);
559 }
560 PL_xpvhv_arenaroot = 0;
d5aea225 561 PL_xpvhv_root = 0;
612f20c3
GS
562
563 for (arena = (XPV*)PL_xpvmg_arenaroot; arena; arena = arenanext) {
564 arenanext = (XPV*)arena->xpv_pv;
565 Safefree(arena);
566 }
567 PL_xpvmg_arenaroot = 0;
d5aea225 568 PL_xpvmg_root = 0;
612f20c3
GS
569
570 for (arena = (XPV*)PL_xpvlv_arenaroot; arena; arena = arenanext) {
571 arenanext = (XPV*)arena->xpv_pv;
572 Safefree(arena);
573 }
574 PL_xpvlv_arenaroot = 0;
d5aea225 575 PL_xpvlv_root = 0;
612f20c3
GS
576
577 for (arena = (XPV*)PL_xpvbm_arenaroot; arena; arena = arenanext) {
578 arenanext = (XPV*)arena->xpv_pv;
579 Safefree(arena);
580 }
581 PL_xpvbm_arenaroot = 0;
d5aea225 582 PL_xpvbm_root = 0;
612f20c3
GS
583
584 for (arena = (XPV*)PL_he_arenaroot; arena; arena = arenanext) {
585 arenanext = (XPV*)arena->xpv_pv;
586 Safefree(arena);
587 }
588 PL_he_arenaroot = 0;
d5aea225 589 PL_he_root = 0;
612f20c3 590
3280af22
NIS
591 if (PL_nice_chunk)
592 Safefree(PL_nice_chunk);
593 PL_nice_chunk = Nullch;
594 PL_nice_chunk_size = 0;
595 PL_sv_arenaroot = 0;
596 PL_sv_root = 0;
4633a7c4
LW
597}
598
645c22ef
DM
599/*
600=for apidoc report_uninit
601
602Print appropriate "Use of uninitialized variable" warning
603
604=cut
605*/
606
1d7c1841
GS
607void
608Perl_report_uninit(pTHX)
609{
610 if (PL_op)
9014280d 611 Perl_warner(aTHX_ packWARN(WARN_UNINITIALIZED), PL_warn_uninit,
53e06cf0 612 " in ", OP_DESC(PL_op));
1d7c1841 613 else
9014280d 614 Perl_warner(aTHX_ packWARN(WARN_UNINITIALIZED), PL_warn_uninit, "", "");
1d7c1841
GS
615}
616
645c22ef
DM
617/* grab a new IV body from the free list, allocating more if necessary */
618
76e3520e 619STATIC XPVIV*
cea2e8a9 620S_new_xiv(pTHX)
463ee0b2 621{
ea7c11a3 622 IV* xiv;
cbe51380
GS
623 LOCK_SV_MUTEX;
624 if (!PL_xiv_root)
625 more_xiv();
626 xiv = PL_xiv_root;
627 /*
628 * See comment in more_xiv() -- RAM.
629 */
630 PL_xiv_root = *(IV**)xiv;
631 UNLOCK_SV_MUTEX;
632 return (XPVIV*)((char*)xiv - STRUCT_OFFSET(XPVIV, xiv_iv));
463ee0b2
LW
633}
634
645c22ef
DM
635/* return an IV body to the free list */
636
76e3520e 637STATIC void
cea2e8a9 638S_del_xiv(pTHX_ XPVIV *p)
463ee0b2 639{
23e6a22f 640 IV* xiv = (IV*)((char*)(p) + STRUCT_OFFSET(XPVIV, xiv_iv));
cbe51380 641 LOCK_SV_MUTEX;
3280af22
NIS
642 *(IV**)xiv = PL_xiv_root;
643 PL_xiv_root = xiv;
cbe51380 644 UNLOCK_SV_MUTEX;
463ee0b2
LW
645}
646
645c22ef
DM
647/* allocate another arena's worth of IV bodies */
648
cbe51380 649STATIC void
cea2e8a9 650S_more_xiv(pTHX)
463ee0b2 651{
ea7c11a3
SM
652 register IV* xiv;
653 register IV* xivend;
8c52afec
IZ
654 XPV* ptr;
655 New(705, ptr, 1008/sizeof(XPV), XPV);
645c22ef 656 ptr->xpv_pv = (char*)PL_xiv_arenaroot; /* linked list of xiv arenas */
3280af22 657 PL_xiv_arenaroot = ptr; /* to keep Purify happy */
a0d0e21e 658
ea7c11a3
SM
659 xiv = (IV*) ptr;
660 xivend = &xiv[1008 / sizeof(IV) - 1];
645c22ef 661 xiv += (sizeof(XPV) - 1) / sizeof(IV) + 1; /* fudge by size of XPV */
3280af22 662 PL_xiv_root = xiv;
463ee0b2 663 while (xiv < xivend) {
ea7c11a3 664 *(IV**)xiv = (IV *)(xiv + 1);
463ee0b2
LW
665 xiv++;
666 }
ea7c11a3 667 *(IV**)xiv = 0;
463ee0b2
LW
668}
669
645c22ef
DM
670/* grab a new NV body from the free list, allocating more if necessary */
671
76e3520e 672STATIC XPVNV*
cea2e8a9 673S_new_xnv(pTHX)
463ee0b2 674{
65202027 675 NV* xnv;
cbe51380
GS
676 LOCK_SV_MUTEX;
677 if (!PL_xnv_root)
678 more_xnv();
679 xnv = PL_xnv_root;
65202027 680 PL_xnv_root = *(NV**)xnv;
cbe51380
GS
681 UNLOCK_SV_MUTEX;
682 return (XPVNV*)((char*)xnv - STRUCT_OFFSET(XPVNV, xnv_nv));
463ee0b2
LW
683}
684
645c22ef
DM
685/* return an NV body to the free list */
686
76e3520e 687STATIC void
cea2e8a9 688S_del_xnv(pTHX_ XPVNV *p)
463ee0b2 689{
65202027 690 NV* xnv = (NV*)((char*)(p) + STRUCT_OFFSET(XPVNV, xnv_nv));
cbe51380 691 LOCK_SV_MUTEX;
65202027 692 *(NV**)xnv = PL_xnv_root;
3280af22 693 PL_xnv_root = xnv;
cbe51380 694 UNLOCK_SV_MUTEX;
463ee0b2
LW
695}
696
645c22ef
DM
697/* allocate another arena's worth of NV bodies */
698
cbe51380 699STATIC void
cea2e8a9 700S_more_xnv(pTHX)
463ee0b2 701{
65202027
DS
702 register NV* xnv;
703 register NV* xnvend;
612f20c3
GS
704 XPV *ptr;
705 New(711, ptr, 1008/sizeof(XPV), XPV);
706 ptr->xpv_pv = (char*)PL_xnv_arenaroot;
707 PL_xnv_arenaroot = ptr;
708
709 xnv = (NV*) ptr;
65202027
DS
710 xnvend = &xnv[1008 / sizeof(NV) - 1];
711 xnv += (sizeof(XPVIV) - 1) / sizeof(NV) + 1; /* fudge by sizeof XPVIV */
3280af22 712 PL_xnv_root = xnv;
463ee0b2 713 while (xnv < xnvend) {
65202027 714 *(NV**)xnv = (NV*)(xnv + 1);
463ee0b2
LW
715 xnv++;
716 }
65202027 717 *(NV**)xnv = 0;
463ee0b2
LW
718}
719
645c22ef
DM
720/* grab a new struct xrv from the free list, allocating more if necessary */
721
76e3520e 722STATIC XRV*
cea2e8a9 723S_new_xrv(pTHX)
ed6116ce
LW
724{
725 XRV* xrv;
cbe51380
GS
726 LOCK_SV_MUTEX;
727 if (!PL_xrv_root)
728 more_xrv();
729 xrv = PL_xrv_root;
730 PL_xrv_root = (XRV*)xrv->xrv_rv;
731 UNLOCK_SV_MUTEX;
732 return xrv;
ed6116ce
LW
733}
734
645c22ef
DM
735/* return a struct xrv to the free list */
736
76e3520e 737STATIC void
cea2e8a9 738S_del_xrv(pTHX_ XRV *p)
ed6116ce 739{
cbe51380 740 LOCK_SV_MUTEX;
3280af22
NIS
741 p->xrv_rv = (SV*)PL_xrv_root;
742 PL_xrv_root = p;
cbe51380 743 UNLOCK_SV_MUTEX;
ed6116ce
LW
744}
745
645c22ef
DM
746/* allocate another arena's worth of struct xrv */
747
cbe51380 748STATIC void
cea2e8a9 749S_more_xrv(pTHX)
ed6116ce 750{
ed6116ce
LW
751 register XRV* xrv;
752 register XRV* xrvend;
612f20c3
GS
753 XPV *ptr;
754 New(712, ptr, 1008/sizeof(XPV), XPV);
755 ptr->xpv_pv = (char*)PL_xrv_arenaroot;
756 PL_xrv_arenaroot = ptr;
757
758 xrv = (XRV*) ptr;
ed6116ce 759 xrvend = &xrv[1008 / sizeof(XRV) - 1];
612f20c3
GS
760 xrv += (sizeof(XPV) - 1) / sizeof(XRV) + 1;
761 PL_xrv_root = xrv;
ed6116ce
LW
762 while (xrv < xrvend) {
763 xrv->xrv_rv = (SV*)(xrv + 1);
764 xrv++;
765 }
766 xrv->xrv_rv = 0;
ed6116ce
LW
767}
768
645c22ef
DM
769/* grab a new struct xpv from the free list, allocating more if necessary */
770
76e3520e 771STATIC XPV*
cea2e8a9 772S_new_xpv(pTHX)
463ee0b2
LW
773{
774 XPV* xpv;
cbe51380
GS
775 LOCK_SV_MUTEX;
776 if (!PL_xpv_root)
777 more_xpv();
778 xpv = PL_xpv_root;
779 PL_xpv_root = (XPV*)xpv->xpv_pv;
780 UNLOCK_SV_MUTEX;
781 return xpv;
463ee0b2
LW
782}
783
645c22ef
DM
784/* return a struct xpv to the free list */
785
76e3520e 786STATIC void
cea2e8a9 787S_del_xpv(pTHX_ XPV *p)
463ee0b2 788{
cbe51380 789 LOCK_SV_MUTEX;
3280af22
NIS
790 p->xpv_pv = (char*)PL_xpv_root;
791 PL_xpv_root = p;
cbe51380 792 UNLOCK_SV_MUTEX;
463ee0b2
LW
793}
794
645c22ef
DM
795/* allocate another arena's worth of struct xpv */
796
cbe51380 797STATIC void
cea2e8a9 798S_more_xpv(pTHX)
463ee0b2 799{
463ee0b2
LW
800 register XPV* xpv;
801 register XPV* xpvend;
612f20c3
GS
802 New(713, xpv, 1008/sizeof(XPV), XPV);
803 xpv->xpv_pv = (char*)PL_xpv_arenaroot;
804 PL_xpv_arenaroot = xpv;
805
463ee0b2 806 xpvend = &xpv[1008 / sizeof(XPV) - 1];
612f20c3 807 PL_xpv_root = ++xpv;
463ee0b2
LW
808 while (xpv < xpvend) {
809 xpv->xpv_pv = (char*)(xpv + 1);
810 xpv++;
811 }
812 xpv->xpv_pv = 0;
463ee0b2
LW
813}
814
645c22ef
DM
815/* grab a new struct xpviv from the free list, allocating more if necessary */
816
932e9ff9
VB
817STATIC XPVIV*
818S_new_xpviv(pTHX)
819{
820 XPVIV* xpviv;
821 LOCK_SV_MUTEX;
822 if (!PL_xpviv_root)
823 more_xpviv();
824 xpviv = PL_xpviv_root;
825 PL_xpviv_root = (XPVIV*)xpviv->xpv_pv;
826 UNLOCK_SV_MUTEX;
827 return xpviv;
828}
829
645c22ef
DM
830/* return a struct xpviv to the free list */
831
932e9ff9
VB
832STATIC void
833S_del_xpviv(pTHX_ XPVIV *p)
834{
835 LOCK_SV_MUTEX;
836 p->xpv_pv = (char*)PL_xpviv_root;
837 PL_xpviv_root = p;
838 UNLOCK_SV_MUTEX;
839}
840
645c22ef
DM
841/* allocate another arena's worth of struct xpviv */
842
932e9ff9
VB
843STATIC void
844S_more_xpviv(pTHX)
845{
846 register XPVIV* xpviv;
847 register XPVIV* xpvivend;
612f20c3
GS
848 New(714, xpviv, 1008/sizeof(XPVIV), XPVIV);
849 xpviv->xpv_pv = (char*)PL_xpviv_arenaroot;
850 PL_xpviv_arenaroot = xpviv;
851
932e9ff9 852 xpvivend = &xpviv[1008 / sizeof(XPVIV) - 1];
612f20c3 853 PL_xpviv_root = ++xpviv;
932e9ff9
VB
854 while (xpviv < xpvivend) {
855 xpviv->xpv_pv = (char*)(xpviv + 1);
856 xpviv++;
857 }
858 xpviv->xpv_pv = 0;
859}
860
645c22ef
DM
861/* grab a new struct xpvnv from the free list, allocating more if necessary */
862
932e9ff9
VB
863STATIC XPVNV*
864S_new_xpvnv(pTHX)
865{
866 XPVNV* xpvnv;
867 LOCK_SV_MUTEX;
868 if (!PL_xpvnv_root)
869 more_xpvnv();
870 xpvnv = PL_xpvnv_root;
871 PL_xpvnv_root = (XPVNV*)xpvnv->xpv_pv;
872 UNLOCK_SV_MUTEX;
873 return xpvnv;
874}
875
645c22ef
DM
876/* return a struct xpvnv to the free list */
877
932e9ff9
VB
878STATIC void
879S_del_xpvnv(pTHX_ XPVNV *p)
880{
881 LOCK_SV_MUTEX;
882 p->xpv_pv = (char*)PL_xpvnv_root;
883 PL_xpvnv_root = p;
884 UNLOCK_SV_MUTEX;
885}
886
645c22ef
DM
887/* allocate another arena's worth of struct xpvnv */
888
932e9ff9
VB
889STATIC void
890S_more_xpvnv(pTHX)
891{
892 register XPVNV* xpvnv;
893 register XPVNV* xpvnvend;
612f20c3
GS
894 New(715, xpvnv, 1008/sizeof(XPVNV), XPVNV);
895 xpvnv->xpv_pv = (char*)PL_xpvnv_arenaroot;
896 PL_xpvnv_arenaroot = xpvnv;
897
932e9ff9 898 xpvnvend = &xpvnv[1008 / sizeof(XPVNV) - 1];
612f20c3 899 PL_xpvnv_root = ++xpvnv;
932e9ff9
VB
900 while (xpvnv < xpvnvend) {
901 xpvnv->xpv_pv = (char*)(xpvnv + 1);
902 xpvnv++;
903 }
904 xpvnv->xpv_pv = 0;
905}
906
645c22ef
DM
907/* grab a new struct xpvcv from the free list, allocating more if necessary */
908
932e9ff9
VB
909STATIC XPVCV*
910S_new_xpvcv(pTHX)
911{
912 XPVCV* xpvcv;
913 LOCK_SV_MUTEX;
914 if (!PL_xpvcv_root)
915 more_xpvcv();
916 xpvcv = PL_xpvcv_root;
917 PL_xpvcv_root = (XPVCV*)xpvcv->xpv_pv;
918 UNLOCK_SV_MUTEX;
919 return xpvcv;
920}
921
645c22ef
DM
922/* return a struct xpvcv to the free list */
923
932e9ff9
VB
924STATIC void
925S_del_xpvcv(pTHX_ XPVCV *p)
926{
927 LOCK_SV_MUTEX;
928 p->xpv_pv = (char*)PL_xpvcv_root;
929 PL_xpvcv_root = p;
930 UNLOCK_SV_MUTEX;
931}
932
645c22ef
DM
933/* allocate another arena's worth of struct xpvcv */
934
932e9ff9
VB
935STATIC void
936S_more_xpvcv(pTHX)
937{
938 register XPVCV* xpvcv;
939 register XPVCV* xpvcvend;
612f20c3
GS
940 New(716, xpvcv, 1008/sizeof(XPVCV), XPVCV);
941 xpvcv->xpv_pv = (char*)PL_xpvcv_arenaroot;
942 PL_xpvcv_arenaroot = xpvcv;
943
932e9ff9 944 xpvcvend = &xpvcv[1008 / sizeof(XPVCV) - 1];
612f20c3 945 PL_xpvcv_root = ++xpvcv;
932e9ff9
VB
946 while (xpvcv < xpvcvend) {
947 xpvcv->xpv_pv = (char*)(xpvcv + 1);
948 xpvcv++;
949 }
950 xpvcv->xpv_pv = 0;
951}
952
645c22ef
DM
953/* grab a new struct xpvav from the free list, allocating more if necessary */
954
932e9ff9
VB
955STATIC XPVAV*
956S_new_xpvav(pTHX)
957{
958 XPVAV* xpvav;
959 LOCK_SV_MUTEX;
960 if (!PL_xpvav_root)
961 more_xpvav();
962 xpvav = PL_xpvav_root;
963 PL_xpvav_root = (XPVAV*)xpvav->xav_array;
964 UNLOCK_SV_MUTEX;
965 return xpvav;
966}
967
645c22ef
DM
968/* return a struct xpvav to the free list */
969
932e9ff9
VB
970STATIC void
971S_del_xpvav(pTHX_ XPVAV *p)
972{
973 LOCK_SV_MUTEX;
974 p->xav_array = (char*)PL_xpvav_root;
975 PL_xpvav_root = p;
976 UNLOCK_SV_MUTEX;
977}
978
645c22ef
DM
979/* allocate another arena's worth of struct xpvav */
980
932e9ff9
VB
981STATIC void
982S_more_xpvav(pTHX)
983{
984 register XPVAV* xpvav;
985 register XPVAV* xpvavend;
612f20c3
GS
986 New(717, xpvav, 1008/sizeof(XPVAV), XPVAV);
987 xpvav->xav_array = (char*)PL_xpvav_arenaroot;
988 PL_xpvav_arenaroot = xpvav;
989
932e9ff9 990 xpvavend = &xpvav[1008 / sizeof(XPVAV) - 1];
612f20c3 991 PL_xpvav_root = ++xpvav;
932e9ff9
VB
992 while (xpvav < xpvavend) {
993 xpvav->xav_array = (char*)(xpvav + 1);
994 xpvav++;
995 }
996 xpvav->xav_array = 0;
997}
998
645c22ef
DM
999/* grab a new struct xpvhv from the free list, allocating more if necessary */
1000
932e9ff9
VB
1001STATIC XPVHV*
1002S_new_xpvhv(pTHX)
1003{
1004 XPVHV* xpvhv;
1005 LOCK_SV_MUTEX;
1006 if (!PL_xpvhv_root)
1007 more_xpvhv();
1008 xpvhv = PL_xpvhv_root;
1009 PL_xpvhv_root = (XPVHV*)xpvhv->xhv_array;
1010 UNLOCK_SV_MUTEX;
1011 return xpvhv;
1012}
1013
645c22ef
DM
1014/* return a struct xpvhv to the free list */
1015
932e9ff9
VB
1016STATIC void
1017S_del_xpvhv(pTHX_ XPVHV *p)
1018{
1019 LOCK_SV_MUTEX;
1020 p->xhv_array = (char*)PL_xpvhv_root;
1021 PL_xpvhv_root = p;
1022 UNLOCK_SV_MUTEX;
1023}
1024
645c22ef
DM
1025/* allocate another arena's worth of struct xpvhv */
1026
932e9ff9
VB
1027STATIC void
1028S_more_xpvhv(pTHX)
1029{
1030 register XPVHV* xpvhv;
1031 register XPVHV* xpvhvend;
612f20c3
GS
1032 New(718, xpvhv, 1008/sizeof(XPVHV), XPVHV);
1033 xpvhv->xhv_array = (char*)PL_xpvhv_arenaroot;
1034 PL_xpvhv_arenaroot = xpvhv;
1035
932e9ff9 1036 xpvhvend = &xpvhv[1008 / sizeof(XPVHV) - 1];
612f20c3 1037 PL_xpvhv_root = ++xpvhv;
932e9ff9
VB
1038 while (xpvhv < xpvhvend) {
1039 xpvhv->xhv_array = (char*)(xpvhv + 1);
1040 xpvhv++;
1041 }
1042 xpvhv->xhv_array = 0;
1043}
1044
645c22ef
DM
1045/* grab a new struct xpvmg from the free list, allocating more if necessary */
1046
932e9ff9
VB
1047STATIC XPVMG*
1048S_new_xpvmg(pTHX)
1049{
1050 XPVMG* xpvmg;
1051 LOCK_SV_MUTEX;
1052 if (!PL_xpvmg_root)
1053 more_xpvmg();
1054 xpvmg = PL_xpvmg_root;
1055 PL_xpvmg_root = (XPVMG*)xpvmg->xpv_pv;
1056 UNLOCK_SV_MUTEX;
1057 return xpvmg;
1058}
1059
645c22ef
DM
1060/* return a struct xpvmg to the free list */
1061
932e9ff9
VB
1062STATIC void
1063S_del_xpvmg(pTHX_ XPVMG *p)
1064{
1065 LOCK_SV_MUTEX;
1066 p->xpv_pv = (char*)PL_xpvmg_root;
1067 PL_xpvmg_root = p;
1068 UNLOCK_SV_MUTEX;
1069}
1070
645c22ef
DM
1071/* allocate another arena's worth of struct xpvmg */
1072
932e9ff9
VB
1073STATIC void
1074S_more_xpvmg(pTHX)
1075{
1076 register XPVMG* xpvmg;
1077 register XPVMG* xpvmgend;
612f20c3
GS
1078 New(719, xpvmg, 1008/sizeof(XPVMG), XPVMG);
1079 xpvmg->xpv_pv = (char*)PL_xpvmg_arenaroot;
1080 PL_xpvmg_arenaroot = xpvmg;
1081
932e9ff9 1082 xpvmgend = &xpvmg[1008 / sizeof(XPVMG) - 1];
612f20c3 1083 PL_xpvmg_root = ++xpvmg;
932e9ff9
VB
1084 while (xpvmg < xpvmgend) {
1085 xpvmg->xpv_pv = (char*)(xpvmg + 1);
1086 xpvmg++;
1087 }
1088 xpvmg->xpv_pv = 0;
1089}
1090
645c22ef
DM
1091/* grab a new struct xpvlv from the free list, allocating more if necessary */
1092
932e9ff9
VB
1093STATIC XPVLV*
1094S_new_xpvlv(pTHX)
1095{
1096 XPVLV* xpvlv;
1097 LOCK_SV_MUTEX;
1098 if (!PL_xpvlv_root)
1099 more_xpvlv();
1100 xpvlv = PL_xpvlv_root;
1101 PL_xpvlv_root = (XPVLV*)xpvlv->xpv_pv;
1102 UNLOCK_SV_MUTEX;
1103 return xpvlv;
1104}
1105
645c22ef
DM
1106/* return a struct xpvlv to the free list */
1107
932e9ff9
VB
1108STATIC void
1109S_del_xpvlv(pTHX_ XPVLV *p)
1110{
1111 LOCK_SV_MUTEX;
1112 p->xpv_pv = (char*)PL_xpvlv_root;
1113 PL_xpvlv_root = p;
1114 UNLOCK_SV_MUTEX;
1115}
1116
645c22ef
DM
1117/* allocate another arena's worth of struct xpvlv */
1118
932e9ff9
VB
1119STATIC void
1120S_more_xpvlv(pTHX)
1121{
1122 register XPVLV* xpvlv;
1123 register XPVLV* xpvlvend;
612f20c3
GS
1124 New(720, xpvlv, 1008/sizeof(XPVLV), XPVLV);
1125 xpvlv->xpv_pv = (char*)PL_xpvlv_arenaroot;
1126 PL_xpvlv_arenaroot = xpvlv;
1127
932e9ff9 1128 xpvlvend = &xpvlv[1008 / sizeof(XPVLV) - 1];
612f20c3 1129 PL_xpvlv_root = ++xpvlv;
932e9ff9
VB
1130 while (xpvlv < xpvlvend) {
1131 xpvlv->xpv_pv = (char*)(xpvlv + 1);
1132 xpvlv++;
1133 }
1134 xpvlv->xpv_pv = 0;
1135}
1136
645c22ef
DM
1137/* grab a new struct xpvbm from the free list, allocating more if necessary */
1138
932e9ff9
VB
1139STATIC XPVBM*
1140S_new_xpvbm(pTHX)
1141{
1142 XPVBM* xpvbm;
1143 LOCK_SV_MUTEX;
1144 if (!PL_xpvbm_root)
1145 more_xpvbm();
1146 xpvbm = PL_xpvbm_root;
1147 PL_xpvbm_root = (XPVBM*)xpvbm->xpv_pv;
1148 UNLOCK_SV_MUTEX;
1149 return xpvbm;
1150}
1151
645c22ef
DM
1152/* return a struct xpvbm to the free list */
1153
932e9ff9
VB
1154STATIC void
1155S_del_xpvbm(pTHX_ XPVBM *p)
1156{
1157 LOCK_SV_MUTEX;
1158 p->xpv_pv = (char*)PL_xpvbm_root;
1159 PL_xpvbm_root = p;
1160 UNLOCK_SV_MUTEX;
1161}
1162
645c22ef
DM
1163/* allocate another arena's worth of struct xpvbm */
1164
932e9ff9
VB
1165STATIC void
1166S_more_xpvbm(pTHX)
1167{
1168 register XPVBM* xpvbm;
1169 register XPVBM* xpvbmend;
612f20c3
GS
1170 New(721, xpvbm, 1008/sizeof(XPVBM), XPVBM);
1171 xpvbm->xpv_pv = (char*)PL_xpvbm_arenaroot;
1172 PL_xpvbm_arenaroot = xpvbm;
1173
932e9ff9 1174 xpvbmend = &xpvbm[1008 / sizeof(XPVBM) - 1];
612f20c3 1175 PL_xpvbm_root = ++xpvbm;
932e9ff9
VB
1176 while (xpvbm < xpvbmend) {
1177 xpvbm->xpv_pv = (char*)(xpvbm + 1);
1178 xpvbm++;
1179 }
1180 xpvbm->xpv_pv = 0;
1181}
1182
2fe0f101
JH
1183#define my_safemalloc(s) (void*)safemalloc(s)
1184#define my_safefree(p) safefree((char*)p)
463ee0b2 1185
d33b2eba 1186#ifdef PURIFY
463ee0b2 1187
d33b2eba
GS
1188#define new_XIV() my_safemalloc(sizeof(XPVIV))
1189#define del_XIV(p) my_safefree(p)
ed6116ce 1190
d33b2eba
GS
1191#define new_XNV() my_safemalloc(sizeof(XPVNV))
1192#define del_XNV(p) my_safefree(p)
463ee0b2 1193
d33b2eba
GS
1194#define new_XRV() my_safemalloc(sizeof(XRV))
1195#define del_XRV(p) my_safefree(p)
8c52afec 1196
d33b2eba
GS
1197#define new_XPV() my_safemalloc(sizeof(XPV))
1198#define del_XPV(p) my_safefree(p)
9b94d1dd 1199
d33b2eba
GS
1200#define new_XPVIV() my_safemalloc(sizeof(XPVIV))
1201#define del_XPVIV(p) my_safefree(p)
932e9ff9 1202
d33b2eba
GS
1203#define new_XPVNV() my_safemalloc(sizeof(XPVNV))
1204#define del_XPVNV(p) my_safefree(p)
932e9ff9 1205
d33b2eba
GS
1206#define new_XPVCV() my_safemalloc(sizeof(XPVCV))
1207#define del_XPVCV(p) my_safefree(p)
932e9ff9 1208
d33b2eba
GS
1209#define new_XPVAV() my_safemalloc(sizeof(XPVAV))
1210#define del_XPVAV(p) my_safefree(p)
1211
1212#define new_XPVHV() my_safemalloc(sizeof(XPVHV))
1213#define del_XPVHV(p) my_safefree(p)
1c846c1f 1214
d33b2eba
GS
1215#define new_XPVMG() my_safemalloc(sizeof(XPVMG))
1216#define del_XPVMG(p) my_safefree(p)
1217
1218#define new_XPVLV() my_safemalloc(sizeof(XPVLV))
1219#define del_XPVLV(p) my_safefree(p)
1220
1221#define new_XPVBM() my_safemalloc(sizeof(XPVBM))
1222#define del_XPVBM(p) my_safefree(p)
1223
1224#else /* !PURIFY */
1225
1226#define new_XIV() (void*)new_xiv()
1227#define del_XIV(p) del_xiv((XPVIV*) p)
1228
1229#define new_XNV() (void*)new_xnv()
1230#define del_XNV(p) del_xnv((XPVNV*) p)
9b94d1dd 1231
d33b2eba
GS
1232#define new_XRV() (void*)new_xrv()
1233#define del_XRV(p) del_xrv((XRV*) p)
9b94d1dd 1234
d33b2eba
GS
1235#define new_XPV() (void*)new_xpv()
1236#define del_XPV(p) del_xpv((XPV *)p)
1237
1238#define new_XPVIV() (void*)new_xpviv()
1239#define del_XPVIV(p) del_xpviv((XPVIV *)p)
1240
1241#define new_XPVNV() (void*)new_xpvnv()
1242#define del_XPVNV(p) del_xpvnv((XPVNV *)p)
1243
1244#define new_XPVCV() (void*)new_xpvcv()
1245#define del_XPVCV(p) del_xpvcv((XPVCV *)p)
1246
1247#define new_XPVAV() (void*)new_xpvav()
1248#define del_XPVAV(p) del_xpvav((XPVAV *)p)
1249
1250#define new_XPVHV() (void*)new_xpvhv()
1251#define del_XPVHV(p) del_xpvhv((XPVHV *)p)
1c846c1f 1252
d33b2eba
GS
1253#define new_XPVMG() (void*)new_xpvmg()
1254#define del_XPVMG(p) del_xpvmg((XPVMG *)p)
1255
1256#define new_XPVLV() (void*)new_xpvlv()
1257#define del_XPVLV(p) del_xpvlv((XPVLV *)p)
1258
1259#define new_XPVBM() (void*)new_xpvbm()
1260#define del_XPVBM(p) del_xpvbm((XPVBM *)p)
1261
1262#endif /* PURIFY */
9b94d1dd 1263
d33b2eba
GS
1264#define new_XPVGV() my_safemalloc(sizeof(XPVGV))
1265#define del_XPVGV(p) my_safefree(p)
1c846c1f 1266
d33b2eba
GS
1267#define new_XPVFM() my_safemalloc(sizeof(XPVFM))
1268#define del_XPVFM(p) my_safefree(p)
1c846c1f 1269
d33b2eba
GS
1270#define new_XPVIO() my_safemalloc(sizeof(XPVIO))
1271#define del_XPVIO(p) my_safefree(p)
8990e307 1272
954c1994
GS
1273/*
1274=for apidoc sv_upgrade
1275
ff276b08 1276Upgrade an SV to a more complex form. Generally adds a new body type to the
645c22ef 1277SV, then copies across as much information as possible from the old body.
ff276b08 1278You generally want to use the C<SvUPGRADE> macro wrapper. See also C<svtype>.
954c1994
GS
1279
1280=cut
1281*/
1282
79072805 1283bool
864dbfa3 1284Perl_sv_upgrade(pTHX_ register SV *sv, U32 mt)
79072805 1285{
c04a4dfe
JH
1286 char* pv = NULL;
1287 U32 cur = 0;
1288 U32 len = 0;
1289 IV iv = 0;
1290 NV nv = 0.0;
1291 MAGIC* magic = NULL;
1292 HV* stash = Nullhv;
79072805 1293
f130fd45
NIS
1294 if (mt != SVt_PV && SvREADONLY(sv) && SvFAKE(sv)) {
1295 sv_force_normal(sv);
1296 }
1297
79072805
LW
1298 if (SvTYPE(sv) == mt)
1299 return TRUE;
1300
a5f75d66
AD
1301 if (mt < SVt_PVIV)
1302 (void)SvOOK_off(sv);
1303
79072805
LW
1304 switch (SvTYPE(sv)) {
1305 case SVt_NULL:
1306 pv = 0;
1307 cur = 0;
1308 len = 0;
1309 iv = 0;
1310 nv = 0.0;
1311 magic = 0;
1312 stash = 0;
1313 break;
79072805
LW
1314 case SVt_IV:
1315 pv = 0;
1316 cur = 0;
1317 len = 0;
463ee0b2 1318 iv = SvIVX(sv);
65202027 1319 nv = (NV)SvIVX(sv);
79072805
LW
1320 del_XIV(SvANY(sv));
1321 magic = 0;
1322 stash = 0;
ed6116ce 1323 if (mt == SVt_NV)
463ee0b2 1324 mt = SVt_PVNV;
ed6116ce
LW
1325 else if (mt < SVt_PVIV)
1326 mt = SVt_PVIV;
79072805
LW
1327 break;
1328 case SVt_NV:
1329 pv = 0;
1330 cur = 0;
1331 len = 0;
463ee0b2 1332 nv = SvNVX(sv);
1bd302c3 1333 iv = I_V(nv);
79072805
LW
1334 magic = 0;
1335 stash = 0;
1336 del_XNV(SvANY(sv));
1337 SvANY(sv) = 0;
ed6116ce 1338 if (mt < SVt_PVNV)
79072805
LW
1339 mt = SVt_PVNV;
1340 break;
ed6116ce
LW
1341 case SVt_RV:
1342 pv = (char*)SvRV(sv);
1343 cur = 0;
1344 len = 0;
56431972
RB
1345 iv = PTR2IV(pv);
1346 nv = PTR2NV(pv);
ed6116ce
LW
1347 del_XRV(SvANY(sv));
1348 magic = 0;
1349 stash = 0;
1350 break;
79072805 1351 case SVt_PV:
463ee0b2 1352 pv = SvPVX(sv);
79072805
LW
1353 cur = SvCUR(sv);
1354 len = SvLEN(sv);
1355 iv = 0;
1356 nv = 0.0;
1357 magic = 0;
1358 stash = 0;
1359 del_XPV(SvANY(sv));
748a9306
LW
1360 if (mt <= SVt_IV)
1361 mt = SVt_PVIV;
1362 else if (mt == SVt_NV)
1363 mt = SVt_PVNV;
79072805
LW
1364 break;
1365 case SVt_PVIV:
463ee0b2 1366 pv = SvPVX(sv);
79072805
LW
1367 cur = SvCUR(sv);
1368 len = SvLEN(sv);
463ee0b2 1369 iv = SvIVX(sv);
79072805
LW
1370 nv = 0.0;
1371 magic = 0;
1372 stash = 0;
1373 del_XPVIV(SvANY(sv));
1374 break;
1375 case SVt_PVNV:
463ee0b2 1376 pv = SvPVX(sv);
79072805
LW
1377 cur = SvCUR(sv);
1378 len = SvLEN(sv);
463ee0b2
LW
1379 iv = SvIVX(sv);
1380 nv = SvNVX(sv);
79072805
LW
1381 magic = 0;
1382 stash = 0;
1383 del_XPVNV(SvANY(sv));
1384 break;
1385 case SVt_PVMG:
463ee0b2 1386 pv = SvPVX(sv);
79072805
LW
1387 cur = SvCUR(sv);
1388 len = SvLEN(sv);
463ee0b2
LW
1389 iv = SvIVX(sv);
1390 nv = SvNVX(sv);
79072805
LW
1391 magic = SvMAGIC(sv);
1392 stash = SvSTASH(sv);
1393 del_XPVMG(SvANY(sv));
1394 break;
1395 default:
cea2e8a9 1396 Perl_croak(aTHX_ "Can't upgrade that kind of scalar");
79072805
LW
1397 }
1398
1399 switch (mt) {
1400 case SVt_NULL:
cea2e8a9 1401 Perl_croak(aTHX_ "Can't upgrade to undef");
79072805
LW
1402 case SVt_IV:
1403 SvANY(sv) = new_XIV();
463ee0b2 1404 SvIVX(sv) = iv;
79072805
LW
1405 break;
1406 case SVt_NV:
1407 SvANY(sv) = new_XNV();
463ee0b2 1408 SvNVX(sv) = nv;
79072805 1409 break;
ed6116ce
LW
1410 case SVt_RV:
1411 SvANY(sv) = new_XRV();
1412 SvRV(sv) = (SV*)pv;
ed6116ce 1413 break;
79072805
LW
1414 case SVt_PV:
1415 SvANY(sv) = new_XPV();
463ee0b2 1416 SvPVX(sv) = pv;
79072805
LW
1417 SvCUR(sv) = cur;
1418 SvLEN(sv) = len;
1419 break;
1420 case SVt_PVIV:
1421 SvANY(sv) = new_XPVIV();
463ee0b2 1422 SvPVX(sv) = pv;
79072805
LW
1423 SvCUR(sv) = cur;
1424 SvLEN(sv) = len;
463ee0b2 1425 SvIVX(sv) = iv;
79072805 1426 if (SvNIOK(sv))
a0d0e21e 1427 (void)SvIOK_on(sv);
79072805
LW
1428 SvNOK_off(sv);
1429 break;
1430 case SVt_PVNV:
1431 SvANY(sv) = new_XPVNV();
463ee0b2 1432 SvPVX(sv) = pv;
79072805
LW
1433 SvCUR(sv) = cur;
1434 SvLEN(sv) = len;
463ee0b2
LW
1435 SvIVX(sv) = iv;
1436 SvNVX(sv) = nv;
79072805
LW
1437 break;
1438 case SVt_PVMG:
1439 SvANY(sv) = new_XPVMG();
463ee0b2 1440 SvPVX(sv) = pv;
79072805
LW
1441 SvCUR(sv) = cur;
1442 SvLEN(sv) = len;
463ee0b2
LW
1443 SvIVX(sv) = iv;
1444 SvNVX(sv) = nv;
79072805
LW
1445 SvMAGIC(sv) = magic;
1446 SvSTASH(sv) = stash;
1447 break;
1448 case SVt_PVLV:
1449 SvANY(sv) = new_XPVLV();
463ee0b2 1450 SvPVX(sv) = pv;
79072805
LW
1451 SvCUR(sv) = cur;
1452 SvLEN(sv) = len;
463ee0b2
LW
1453 SvIVX(sv) = iv;
1454 SvNVX(sv) = nv;
79072805
LW
1455 SvMAGIC(sv) = magic;
1456 SvSTASH(sv) = stash;
1457 LvTARGOFF(sv) = 0;
1458 LvTARGLEN(sv) = 0;
1459 LvTARG(sv) = 0;
1460 LvTYPE(sv) = 0;
1461 break;
1462 case SVt_PVAV:
1463 SvANY(sv) = new_XPVAV();
463ee0b2
LW
1464 if (pv)
1465 Safefree(pv);
2304df62 1466 SvPVX(sv) = 0;
d1bf51dd 1467 AvMAX(sv) = -1;
93965878 1468 AvFILLp(sv) = -1;
463ee0b2
LW
1469 SvIVX(sv) = 0;
1470 SvNVX(sv) = 0.0;
1471 SvMAGIC(sv) = magic;
1472 SvSTASH(sv) = stash;
1473 AvALLOC(sv) = 0;
79072805
LW
1474 AvARYLEN(sv) = 0;
1475 AvFLAGS(sv) = 0;
1476 break;
1477 case SVt_PVHV:
1478 SvANY(sv) = new_XPVHV();
463ee0b2
LW
1479 if (pv)
1480 Safefree(pv);
1481 SvPVX(sv) = 0;
1482 HvFILL(sv) = 0;
1483 HvMAX(sv) = 0;
8aacddc1
NIS
1484 HvTOTALKEYS(sv) = 0;
1485 HvPLACEHOLDERS(sv) = 0;
79072805
LW
1486 SvMAGIC(sv) = magic;
1487 SvSTASH(sv) = stash;
79072805
LW
1488 HvRITER(sv) = 0;
1489 HvEITER(sv) = 0;
1490 HvPMROOT(sv) = 0;
1491 HvNAME(sv) = 0;
79072805
LW
1492 break;
1493 case SVt_PVCV:
1494 SvANY(sv) = new_XPVCV();
748a9306 1495 Zero(SvANY(sv), 1, XPVCV);
463ee0b2 1496 SvPVX(sv) = pv;
79072805
LW
1497 SvCUR(sv) = cur;
1498 SvLEN(sv) = len;
463ee0b2
LW
1499 SvIVX(sv) = iv;
1500 SvNVX(sv) = nv;
79072805
LW
1501 SvMAGIC(sv) = magic;
1502 SvSTASH(sv) = stash;
79072805
LW
1503 break;
1504 case SVt_PVGV:
1505 SvANY(sv) = new_XPVGV();
463ee0b2 1506 SvPVX(sv) = pv;
79072805
LW
1507 SvCUR(sv) = cur;
1508 SvLEN(sv) = len;
463ee0b2
LW
1509 SvIVX(sv) = iv;
1510 SvNVX(sv) = nv;
79072805
LW
1511 SvMAGIC(sv) = magic;
1512 SvSTASH(sv) = stash;
93a17b20 1513 GvGP(sv) = 0;
79072805
LW
1514 GvNAME(sv) = 0;
1515 GvNAMELEN(sv) = 0;
1516 GvSTASH(sv) = 0;
a5f75d66 1517 GvFLAGS(sv) = 0;
79072805
LW
1518 break;
1519 case SVt_PVBM:
1520 SvANY(sv) = new_XPVBM();
463ee0b2 1521 SvPVX(sv) = pv;
79072805
LW
1522 SvCUR(sv) = cur;
1523 SvLEN(sv) = len;
463ee0b2
LW
1524 SvIVX(sv) = iv;
1525 SvNVX(sv) = nv;
79072805
LW
1526 SvMAGIC(sv) = magic;
1527 SvSTASH(sv) = stash;
1528 BmRARE(sv) = 0;
1529 BmUSEFUL(sv) = 0;
1530 BmPREVIOUS(sv) = 0;
1531 break;
1532 case SVt_PVFM:
1533 SvANY(sv) = new_XPVFM();
748a9306 1534 Zero(SvANY(sv), 1, XPVFM);
463ee0b2 1535 SvPVX(sv) = pv;
79072805
LW
1536 SvCUR(sv) = cur;
1537 SvLEN(sv) = len;
463ee0b2
LW
1538 SvIVX(sv) = iv;
1539 SvNVX(sv) = nv;
79072805
LW
1540 SvMAGIC(sv) = magic;
1541 SvSTASH(sv) = stash;
79072805 1542 break;
8990e307
LW
1543 case SVt_PVIO:
1544 SvANY(sv) = new_XPVIO();
748a9306 1545 Zero(SvANY(sv), 1, XPVIO);
8990e307
LW
1546 SvPVX(sv) = pv;
1547 SvCUR(sv) = cur;
1548 SvLEN(sv) = len;
1549 SvIVX(sv) = iv;
1550 SvNVX(sv) = nv;
1551 SvMAGIC(sv) = magic;
1552 SvSTASH(sv) = stash;
85e6fe83 1553 IoPAGE_LEN(sv) = 60;
8990e307
LW
1554 break;
1555 }
1556 SvFLAGS(sv) &= ~SVTYPEMASK;
1557 SvFLAGS(sv) |= mt;
79072805
LW
1558 return TRUE;
1559}
1560
645c22ef
DM
1561/*
1562=for apidoc sv_backoff
1563
1564Remove any string offset. You should normally use the C<SvOOK_off> macro
1565wrapper instead.
1566
1567=cut
1568*/
1569
79072805 1570int
864dbfa3 1571Perl_sv_backoff(pTHX_ register SV *sv)
79072805
LW
1572{
1573 assert(SvOOK(sv));
463ee0b2
LW
1574 if (SvIVX(sv)) {
1575 char *s = SvPVX(sv);
1576 SvLEN(sv) += SvIVX(sv);
1577 SvPVX(sv) -= SvIVX(sv);
79072805 1578 SvIV_set(sv, 0);
463ee0b2 1579 Move(s, SvPVX(sv), SvCUR(sv)+1, char);
79072805
LW
1580 }
1581 SvFLAGS(sv) &= ~SVf_OOK;
a0d0e21e 1582 return 0;
79072805
LW
1583}
1584
954c1994
GS
1585/*
1586=for apidoc sv_grow
1587
645c22ef
DM
1588Expands the character buffer in the SV. If necessary, uses C<sv_unref> and
1589upgrades the SV to C<SVt_PV>. Returns a pointer to the character buffer.
1590Use the C<SvGROW> wrapper instead.
954c1994
GS
1591
1592=cut
1593*/
1594
79072805 1595char *
864dbfa3 1596Perl_sv_grow(pTHX_ register SV *sv, register STRLEN newlen)
79072805
LW
1597{
1598 register char *s;
1599
54f0641b
NIS
1600
1601
55497cff 1602#ifdef HAS_64K_LIMIT
79072805 1603 if (newlen >= 0x10000) {
1d7c1841
GS
1604 PerlIO_printf(Perl_debug_log,
1605 "Allocation too large: %"UVxf"\n", (UV)newlen);
79072805
LW
1606 my_exit(1);
1607 }
55497cff 1608#endif /* HAS_64K_LIMIT */
a0d0e21e
LW
1609 if (SvROK(sv))
1610 sv_unref(sv);
79072805
LW
1611 if (SvTYPE(sv) < SVt_PV) {
1612 sv_upgrade(sv, SVt_PV);
463ee0b2 1613 s = SvPVX(sv);
79072805
LW
1614 }
1615 else if (SvOOK(sv)) { /* pv is offset? */
1616 sv_backoff(sv);
463ee0b2 1617 s = SvPVX(sv);
79072805
LW
1618 if (newlen > SvLEN(sv))
1619 newlen += 10 * (newlen - SvCUR(sv)); /* avoid copy each time */
c6f8c383
GA
1620#ifdef HAS_64K_LIMIT
1621 if (newlen >= 0x10000)
1622 newlen = 0xFFFF;
1623#endif
79072805
LW
1624 }
1625 else
463ee0b2 1626 s = SvPVX(sv);
54f0641b 1627
79072805 1628 if (newlen > SvLEN(sv)) { /* need more room? */
8d6dde3e 1629 if (SvLEN(sv) && s) {
2fe0f101 1630#ifdef MYMALLOC
8d6dde3e
IZ
1631 STRLEN l = malloced_size((void*)SvPVX(sv));
1632 if (newlen <= l) {
1633 SvLEN_set(sv, l);
1634 return s;
1635 } else
c70c8a0a 1636#endif
79072805 1637 Renew(s,newlen,char);
8d6dde3e 1638 }
4e83176d 1639 else {
ee5f0761
AMS
1640 /* sv_force_normal_flags() must not try to unshare the new
1641 PVX we allocate below. AMS 20010713 */
4e83176d 1642 if (SvREADONLY(sv) && SvFAKE(sv)) {
4e83176d
AMS
1643 SvFAKE_off(sv);
1644 SvREADONLY_off(sv);
4e83176d
AMS
1645 }
1646 New(703, s, newlen, char);
40565179 1647 if (SvPVX(sv) && SvCUR(sv)) {
54f0641b 1648 Move(SvPVX(sv), s, (newlen < SvCUR(sv)) ? newlen : SvCUR(sv), char);
40565179 1649 }
4e83176d 1650 }
79072805
LW
1651 SvPV_set(sv, s);
1652 SvLEN_set(sv, newlen);
1653 }
1654 return s;
1655}
1656
954c1994
GS
1657/*
1658=for apidoc sv_setiv
1659
645c22ef
DM
1660Copies an integer into the given SV, upgrading first if necessary.
1661Does not handle 'set' magic. See also C<sv_setiv_mg>.
954c1994
GS
1662
1663=cut
1664*/
1665
79072805 1666void
864dbfa3 1667Perl_sv_setiv(pTHX_ register SV *sv, IV i)
79072805 1668{
2213622d 1669 SV_CHECK_THINKFIRST(sv);
463ee0b2
LW
1670 switch (SvTYPE(sv)) {
1671 case SVt_NULL:
79072805 1672 sv_upgrade(sv, SVt_IV);
463ee0b2
LW
1673 break;
1674 case SVt_NV:
1675 sv_upgrade(sv, SVt_PVNV);
1676 break;
ed6116ce 1677 case SVt_RV:
463ee0b2 1678 case SVt_PV:
79072805 1679 sv_upgrade(sv, SVt_PVIV);
463ee0b2 1680 break;
a0d0e21e
LW
1681
1682 case SVt_PVGV:
a0d0e21e
LW
1683 case SVt_PVAV:
1684 case SVt_PVHV:
1685 case SVt_PVCV:
1686 case SVt_PVFM:
1687 case SVt_PVIO:
411caa50 1688 Perl_croak(aTHX_ "Can't coerce %s to integer in %s", sv_reftype(sv,0),
53e06cf0 1689 OP_DESC(PL_op));
463ee0b2 1690 }
a0d0e21e 1691 (void)SvIOK_only(sv); /* validate number */
a5f75d66 1692 SvIVX(sv) = i;
463ee0b2 1693 SvTAINT(sv);
79072805
LW
1694}
1695
954c1994
GS
1696/*
1697=for apidoc sv_setiv_mg
1698
1699Like C<sv_setiv>, but also handles 'set' magic.
1700
1701=cut
1702*/
1703
79072805 1704void
864dbfa3 1705Perl_sv_setiv_mg(pTHX_ register SV *sv, IV i)
ef50df4b
GS
1706{
1707 sv_setiv(sv,i);
1708 SvSETMAGIC(sv);
1709}
1710
954c1994
GS
1711/*
1712=for apidoc sv_setuv
1713
645c22ef
DM
1714Copies an unsigned integer into the given SV, upgrading first if necessary.
1715Does not handle 'set' magic. See also C<sv_setuv_mg>.
954c1994
GS
1716
1717=cut
1718*/
1719
ef50df4b 1720void
864dbfa3 1721Perl_sv_setuv(pTHX_ register SV *sv, UV u)
55497cff 1722{
55ada374
NC
1723 /* With these two if statements:
1724 u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865
d460ef45 1725
55ada374
NC
1726 without
1727 u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865
d460ef45 1728
55ada374
NC
1729 If you wish to remove them, please benchmark to see what the effect is
1730 */
28e5dec8
JH
1731 if (u <= (UV)IV_MAX) {
1732 sv_setiv(sv, (IV)u);
1733 return;
1734 }
25da4f38
IZ
1735 sv_setiv(sv, 0);
1736 SvIsUV_on(sv);
1737 SvUVX(sv) = u;
55497cff 1738}
1739
954c1994
GS
1740/*
1741=for apidoc sv_setuv_mg
1742
1743Like C<sv_setuv>, but also handles 'set' magic.
1744
1745=cut
1746*/
1747
55497cff 1748void
864dbfa3 1749Perl_sv_setuv_mg(pTHX_ register SV *sv, UV u)
ef50df4b 1750{
55ada374
NC
1751 /* With these two if statements:
1752 u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865
d460ef45 1753
55ada374
NC
1754 without
1755 u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865
d460ef45 1756
55ada374
NC
1757 If you wish to remove them, please benchmark to see what the effect is
1758 */
28e5dec8
JH
1759 if (u <= (UV)IV_MAX) {
1760 sv_setiv(sv, (IV)u);
1761 } else {
1762 sv_setiv(sv, 0);
1763 SvIsUV_on(sv);
1764 sv_setuv(sv,u);
1765 }
ef50df4b
GS
1766 SvSETMAGIC(sv);
1767}
1768
954c1994
GS
1769/*
1770=for apidoc sv_setnv
1771
645c22ef
DM
1772Copies a double into the given SV, upgrading first if necessary.
1773Does not handle 'set' magic. See also C<sv_setnv_mg>.
954c1994
GS
1774
1775=cut
1776*/
1777
ef50df4b 1778void
65202027 1779Perl_sv_setnv(pTHX_ register SV *sv, NV num)
79072805 1780{
2213622d 1781 SV_CHECK_THINKFIRST(sv);
a0d0e21e
LW
1782 switch (SvTYPE(sv)) {
1783 case SVt_NULL:
1784 case SVt_IV:
79072805 1785 sv_upgrade(sv, SVt_NV);
a0d0e21e 1786 break;
a0d0e21e
LW
1787 case SVt_RV:
1788 case SVt_PV:
1789 case SVt_PVIV:
79072805 1790 sv_upgrade(sv, SVt_PVNV);
a0d0e21e 1791 break;
827b7e14 1792
a0d0e21e 1793 case SVt_PVGV:
a0d0e21e
LW
1794 case SVt_PVAV:
1795 case SVt_PVHV:
1796 case SVt_PVCV:
1797 case SVt_PVFM:
1798 case SVt_PVIO:
411caa50 1799 Perl_croak(aTHX_ "Can't coerce %s to number in %s", sv_reftype(sv,0),
53e06cf0 1800 OP_NAME(PL_op));
79072805 1801 }
463ee0b2 1802 SvNVX(sv) = num;
a0d0e21e 1803 (void)SvNOK_only(sv); /* validate number */
463ee0b2 1804 SvTAINT(sv);
79072805
LW
1805}
1806
954c1994
GS
1807/*
1808=for apidoc sv_setnv_mg
1809
1810Like C<sv_setnv>, but also handles 'set' magic.
1811
1812=cut
1813*/
1814
ef50df4b 1815void
65202027 1816Perl_sv_setnv_mg(pTHX_ register SV *sv, NV num)
ef50df4b
GS
1817{
1818 sv_setnv(sv,num);
1819 SvSETMAGIC(sv);
1820}
1821
645c22ef
DM
1822/* Print an "isn't numeric" warning, using a cleaned-up,
1823 * printable version of the offending string
1824 */
1825
76e3520e 1826STATIC void
cea2e8a9 1827S_not_a_number(pTHX_ SV *sv)
a0d0e21e 1828{
94463019
JH
1829 SV *dsv;
1830 char tmpbuf[64];
1831 char *pv;
1832
1833 if (DO_UTF8(sv)) {
1834 dsv = sv_2mortal(newSVpv("", 0));
1835 pv = sv_uni_display(dsv, sv, 10, 0);
1836 } else {
1837 char *d = tmpbuf;
1838 char *limit = tmpbuf + sizeof(tmpbuf) - 8;
1839 /* each *s can expand to 4 chars + "...\0",
1840 i.e. need room for 8 chars */
ecdeb87c 1841
94463019
JH
1842 char *s, *end;
1843 for (s = SvPVX(sv), end = s + SvCUR(sv); s < end && d < limit; s++) {
1844 int ch = *s & 0xFF;
1845 if (ch & 128 && !isPRINT_LC(ch)) {
1846 *d++ = 'M';
1847 *d++ = '-';
1848 ch &= 127;
1849 }
1850 if (ch == '\n') {
1851 *d++ = '\\';
1852 *d++ = 'n';
1853 }
1854 else if (ch == '\r') {
1855 *d++ = '\\';
1856 *d++ = 'r';
1857 }
1858 else if (ch == '\f') {
1859 *d++ = '\\';
1860 *d++ = 'f';
1861 }
1862 else if (ch == '\\') {
1863 *d++ = '\\';
1864 *d++ = '\\';
1865 }
1866 else if (ch == '\0') {
1867 *d++ = '\\';
1868 *d++ = '0';
1869 }
1870 else if (isPRINT_LC(ch))
1871 *d++ = ch;
1872 else {
1873 *d++ = '^';
1874 *d++ = toCTRL(ch);
1875 }
1876 }
1877 if (s < end) {
1878 *d++ = '.';
1879 *d++ = '.';
1880 *d++ = '.';
1881 }
1882 *d = '\0';
1883 pv = tmpbuf;
a0d0e21e 1884 }
a0d0e21e 1885
533c011a 1886 if (PL_op)
9014280d 1887 Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
94463019
JH
1888 "Argument \"%s\" isn't numeric in %s", pv,
1889 OP_DESC(PL_op));
a0d0e21e 1890 else
9014280d 1891 Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
94463019 1892 "Argument \"%s\" isn't numeric", pv);
a0d0e21e
LW
1893}
1894
c2988b20
NC
1895/*
1896=for apidoc looks_like_number
1897
645c22ef
DM
1898Test if the content of an SV looks like a number (or is a number).
1899C<Inf> and C<Infinity> are treated as numbers (so will not issue a
1900non-numeric warning), even if your atof() doesn't grok them.
c2988b20
NC
1901
1902=cut
1903*/
1904
1905I32
1906Perl_looks_like_number(pTHX_ SV *sv)
1907{
1908 register char *sbegin;
1909 STRLEN len;
1910
1911 if (SvPOK(sv)) {
1912 sbegin = SvPVX(sv);
1913 len = SvCUR(sv);
1914 }
1915 else if (SvPOKp(sv))
1916 sbegin = SvPV(sv, len);
1917 else
fc3d430d 1918 return SvFLAGS(sv) & (SVf_NOK|SVp_NOK|SVf_IOK|SVp_IOK);
c2988b20
NC
1919 return grok_number(sbegin, len, NULL);
1920}
25da4f38
IZ
1921
1922/* Actually, ISO C leaves conversion of UV to IV undefined, but
1923 until proven guilty, assume that things are not that bad... */
1924
645c22ef
DM
1925/*
1926 NV_PRESERVES_UV:
1927
1928 As 64 bit platforms often have an NV that doesn't preserve all bits of
28e5dec8
JH
1929 an IV (an assumption perl has been based on to date) it becomes necessary
1930 to remove the assumption that the NV always carries enough precision to
1931 recreate the IV whenever needed, and that the NV is the canonical form.
1932 Instead, IV/UV and NV need to be given equal rights. So as to not lose
645c22ef 1933 precision as a side effect of conversion (which would lead to insanity
28e5dec8
JH
1934 and the dragon(s) in t/op/numconvert.t getting very angry) the intent is
1935 1) to distinguish between IV/UV/NV slots that have cached a valid
1936 conversion where precision was lost and IV/UV/NV slots that have a
1937 valid conversion which has lost no precision
645c22ef 1938 2) to ensure that if a numeric conversion to one form is requested that
28e5dec8
JH
1939 would lose precision, the precise conversion (or differently
1940 imprecise conversion) is also performed and cached, to prevent
1941 requests for different numeric formats on the same SV causing
1942 lossy conversion chains. (lossless conversion chains are perfectly
1943 acceptable (still))
1944
1945
1946 flags are used:
1947 SvIOKp is true if the IV slot contains a valid value
1948 SvIOK is true only if the IV value is accurate (UV if SvIOK_UV true)
1949 SvNOKp is true if the NV slot contains a valid value
1950 SvNOK is true only if the NV value is accurate
1951
1952 so
645c22ef 1953 while converting from PV to NV, check to see if converting that NV to an
28e5dec8
JH
1954 IV(or UV) would lose accuracy over a direct conversion from PV to
1955 IV(or UV). If it would, cache both conversions, return NV, but mark
1956 SV as IOK NOKp (ie not NOK).
1957
645c22ef 1958 While converting from PV to IV, check to see if converting that IV to an
28e5dec8
JH
1959 NV would lose accuracy over a direct conversion from PV to NV. If it
1960 would, cache both conversions, flag similarly.
1961
1962 Before, the SV value "3.2" could become NV=3.2 IV=3 NOK, IOK quite
1963 correctly because if IV & NV were set NV *always* overruled.
645c22ef
DM
1964 Now, "3.2" will become NV=3.2 IV=3 NOK, IOKp, because the flag's meaning
1965 changes - now IV and NV together means that the two are interchangeable:
28e5dec8 1966 SvIVX == (IV) SvNVX && SvNVX == (NV) SvIVX;
d460ef45 1967
645c22ef
DM
1968 The benefit of this is that operations such as pp_add know that if
1969 SvIOK is true for both left and right operands, then integer addition
1970 can be used instead of floating point (for cases where the result won't
1971 overflow). Before, floating point was always used, which could lead to
28e5dec8
JH
1972 loss of precision compared with integer addition.
1973
1974 * making IV and NV equal status should make maths accurate on 64 bit
1975 platforms
1976 * may speed up maths somewhat if pp_add and friends start to use
645c22ef 1977 integers when possible instead of fp. (Hopefully the overhead in
28e5dec8
JH
1978 looking for SvIOK and checking for overflow will not outweigh the
1979 fp to integer speedup)
1980 * will slow down integer operations (callers of SvIV) on "inaccurate"
1981 values, as the change from SvIOK to SvIOKp will cause a call into
1982 sv_2iv each time rather than a macro access direct to the IV slot
1983 * should speed up number->string conversion on integers as IV is
645c22ef 1984 favoured when IV and NV are equally accurate
28e5dec8
JH
1985
1986 ####################################################################
645c22ef
DM
1987 You had better be using SvIOK_notUV if you want an IV for arithmetic:
1988 SvIOK is true if (IV or UV), so you might be getting (IV)SvUV.
1989 On the other hand, SvUOK is true iff UV.
28e5dec8
JH
1990 ####################################################################
1991
645c22ef 1992 Your mileage will vary depending your CPU's relative fp to integer
28e5dec8
JH
1993 performance ratio.
1994*/
1995
1996#ifndef NV_PRESERVES_UV
645c22ef
DM
1997# define IS_NUMBER_UNDERFLOW_IV 1
1998# define IS_NUMBER_UNDERFLOW_UV 2
1999# define IS_NUMBER_IV_AND_UV 2
2000# define IS_NUMBER_OVERFLOW_IV 4
2001# define IS_NUMBER_OVERFLOW_UV 5
2002
2003/* sv_2iuv_non_preserve(): private routine for use by sv_2iv() and sv_2uv() */
28e5dec8
JH
2004
2005/* For sv_2nv these three cases are "SvNOK and don't bother casting" */
2006STATIC int
645c22ef 2007S_sv_2iuv_non_preserve(pTHX_ register SV *sv, I32 numtype)
28e5dec8 2008{
1779d84d 2009 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
2010 if (SvNVX(sv) < (NV)IV_MIN) {
2011 (void)SvIOKp_on(sv);
2012 (void)SvNOK_on(sv);
2013 SvIVX(sv) = IV_MIN;
2014 return IS_NUMBER_UNDERFLOW_IV;
2015 }
2016 if (SvNVX(sv) > (NV)UV_MAX) {
2017 (void)SvIOKp_on(sv);
2018 (void)SvNOK_on(sv);
2019 SvIsUV_on(sv);
2020 SvUVX(sv) = UV_MAX;
2021 return IS_NUMBER_OVERFLOW_UV;
2022 }
c2988b20
NC
2023 (void)SvIOKp_on(sv);
2024 (void)SvNOK_on(sv);
2025 /* Can't use strtol etc to convert this string. (See truth table in
2026 sv_2iv */
2027 if (SvNVX(sv) <= (UV)IV_MAX) {
2028 SvIVX(sv) = I_V(SvNVX(sv));
2029 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
2030 SvIOK_on(sv); /* Integer is precise. NOK, IOK */
2031 } else {
2032 /* Integer is imprecise. NOK, IOKp */
2033 }
2034 return SvNVX(sv) < 0 ? IS_NUMBER_UNDERFLOW_UV : IS_NUMBER_IV_AND_UV;
2035 }
2036 SvIsUV_on(sv);
2037 SvUVX(sv) = U_V(SvNVX(sv));
2038 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
2039 if (SvUVX(sv) == UV_MAX) {
2040 /* As we know that NVs don't preserve UVs, UV_MAX cannot
2041 possibly be preserved by NV. Hence, it must be overflow.
2042 NOK, IOKp */
2043 return IS_NUMBER_OVERFLOW_UV;
2044 }
2045 SvIOK_on(sv); /* Integer is precise. NOK, UOK */
2046 } else {
2047 /* Integer is imprecise. NOK, IOKp */
28e5dec8 2048 }
c2988b20 2049 return IS_NUMBER_OVERFLOW_IV;
28e5dec8 2050}
645c22ef
DM
2051#endif /* !NV_PRESERVES_UV*/
2052
2053/*
2054=for apidoc sv_2iv
2055
2056Return the integer value of an SV, doing any necessary string conversion,
2057magic etc. Normally used via the C<SvIV(sv)> and C<SvIVx(sv)> macros.
2058
2059=cut
2060*/
28e5dec8 2061
a0d0e21e 2062IV
864dbfa3 2063Perl_sv_2iv(pTHX_ register SV *sv)
79072805
LW
2064{
2065 if (!sv)
2066 return 0;
8990e307 2067 if (SvGMAGICAL(sv)) {
463ee0b2
LW
2068 mg_get(sv);
2069 if (SvIOKp(sv))
2070 return SvIVX(sv);
748a9306 2071 if (SvNOKp(sv)) {
25da4f38 2072 return I_V(SvNVX(sv));
748a9306 2073 }
36477c24 2074 if (SvPOKp(sv) && SvLEN(sv))
2075 return asIV(sv);
3fe9a6f1 2076 if (!SvROK(sv)) {
d008e5eb 2077 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
d008e5eb 2078 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
1d7c1841 2079 report_uninit();
c6ee37c5 2080 }
36477c24 2081 return 0;
3fe9a6f1 2082 }
463ee0b2 2083 }
ed6116ce 2084 if (SvTHINKFIRST(sv)) {
a0d0e21e 2085 if (SvROK(sv)) {
a0d0e21e 2086 SV* tmpstr;
1554e226 2087 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
b4b9a328 2088 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv))))
9e7bc3e8 2089 return SvIV(tmpstr);
56431972 2090 return PTR2IV(SvRV(sv));
a0d0e21e 2091 }
47deb5e7
NIS
2092 if (SvREADONLY(sv) && SvFAKE(sv)) {
2093 sv_force_normal(sv);
2094 }
0336b60e 2095 if (SvREADONLY(sv) && !SvOK(sv)) {
0336b60e 2096 if (ckWARN(WARN_UNINITIALIZED))
1d7c1841 2097 report_uninit();
ed6116ce
LW
2098 return 0;
2099 }
79072805 2100 }
25da4f38
IZ
2101 if (SvIOKp(sv)) {
2102 if (SvIsUV(sv)) {
2103 return (IV)(SvUVX(sv));
2104 }
2105 else {
2106 return SvIVX(sv);
2107 }
463ee0b2 2108 }
748a9306 2109 if (SvNOKp(sv)) {
28e5dec8
JH
2110 /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv
2111 * without also getting a cached IV/UV from it at the same time
2112 * (ie PV->NV conversion should detect loss of accuracy and cache
2113 * IV or UV at same time to avoid this. NWC */
25da4f38
IZ
2114
2115 if (SvTYPE(sv) == SVt_NV)
2116 sv_upgrade(sv, SVt_PVNV);
2117
28e5dec8
JH
2118 (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */
2119 /* < not <= as for NV doesn't preserve UV, ((NV)IV_MAX+1) will almost
2120 certainly cast into the IV range at IV_MAX, whereas the correct
2121 answer is the UV IV_MAX +1. Hence < ensures that dodgy boundary
2122 cases go to UV */
2123 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
748a9306 2124 SvIVX(sv) = I_V(SvNVX(sv));
28e5dec8
JH
2125 if (SvNVX(sv) == (NV) SvIVX(sv)
2126#ifndef NV_PRESERVES_UV
2127 && (((UV)1 << NV_PRESERVES_UV_BITS) >
2128 (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv)))
2129 /* Don't flag it as "accurately an integer" if the number
2130 came from a (by definition imprecise) NV operation, and
2131 we're outside the range of NV integer precision */
2132#endif
2133 ) {
2134 SvIOK_on(sv); /* Can this go wrong with rounding? NWC */
2135 DEBUG_c(PerlIO_printf(Perl_debug_log,
7234c960 2136 "0x%"UVxf" iv(%"NVgf" => %"IVdf") (precise)\n",
28e5dec8
JH
2137 PTR2UV(sv),
2138 SvNVX(sv),
2139 SvIVX(sv)));
2140
2141 } else {
2142 /* IV not precise. No need to convert from PV, as NV
2143 conversion would already have cached IV if it detected
2144 that PV->IV would be better than PV->NV->IV
2145 flags already correct - don't set public IOK. */
2146 DEBUG_c(PerlIO_printf(Perl_debug_log,
7234c960 2147 "0x%"UVxf" iv(%"NVgf" => %"IVdf") (imprecise)\n",
28e5dec8
JH
2148 PTR2UV(sv),
2149 SvNVX(sv),
2150 SvIVX(sv)));
2151 }
2152 /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN,
2153 but the cast (NV)IV_MIN rounds to a the value less (more
2154 negative) than IV_MIN which happens to be equal to SvNVX ??
2155 Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and
2156 NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and
2157 (NV)UVX == NVX are both true, but the values differ. :-(
2158 Hopefully for 2s complement IV_MIN is something like
2159 0x8000000000000000 which will be exact. NWC */
d460ef45 2160 }
25da4f38 2161 else {
ff68c719 2162 SvUVX(sv) = U_V(SvNVX(sv));
28e5dec8
JH
2163 if (
2164 (SvNVX(sv) == (NV) SvUVX(sv))
2165#ifndef NV_PRESERVES_UV
2166 /* Make sure it's not 0xFFFFFFFFFFFFFFFF */
2167 /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */
2168 && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv))
2169 /* Don't flag it as "accurately an integer" if the number
2170 came from a (by definition imprecise) NV operation, and
2171 we're outside the range of NV integer precision */
2172#endif
2173 )
2174 SvIOK_on(sv);
25da4f38
IZ
2175 SvIsUV_on(sv);
2176 ret_iv_max:
1c846c1f 2177 DEBUG_c(PerlIO_printf(Perl_debug_log,
57def98f 2178 "0x%"UVxf" 2iv(%"UVuf" => %"IVdf") (as unsigned)\n",
56431972 2179 PTR2UV(sv),
57def98f
JH
2180 SvUVX(sv),
2181 SvUVX(sv)));
25da4f38
IZ
2182 return (IV)SvUVX(sv);
2183 }
748a9306
LW
2184 }
2185 else if (SvPOKp(sv) && SvLEN(sv)) {
c2988b20
NC
2186 UV value;
2187 int numtype = grok_number(SvPVX(sv), SvCUR(sv), &value);
25da4f38
IZ
2188 /* We want to avoid a possible problem when we cache an IV which
2189 may be later translated to an NV, and the resulting NV is not
c2988b20
NC
2190 the same as the direct translation of the initial string
2191 (eg 123.456 can shortcut to the IV 123 with atol(), but we must
2192 be careful to ensure that the value with the .456 is around if the
2193 NV value is requested in the future).
1c846c1f 2194
25da4f38
IZ
2195 This means that if we cache such an IV, we need to cache the
2196 NV as well. Moreover, we trade speed for space, and do not
28e5dec8 2197 cache the NV if we are sure it's not needed.
25da4f38 2198 */
16b7a9a4 2199
c2988b20
NC
2200 /* SVt_PVNV is one higher than SVt_PVIV, hence this order */
2201 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2202 == IS_NUMBER_IN_UV) {
5e045b90 2203 /* It's definitely an integer, only upgrade to PVIV */
28e5dec8
JH
2204 if (SvTYPE(sv) < SVt_PVIV)
2205 sv_upgrade(sv, SVt_PVIV);
f7bbb42a 2206 (void)SvIOK_on(sv);
c2988b20
NC
2207 } else if (SvTYPE(sv) < SVt_PVNV)
2208 sv_upgrade(sv, SVt_PVNV);
28e5dec8 2209
c2988b20
NC
2210 /* If NV preserves UV then we only use the UV value if we know that
2211 we aren't going to call atof() below. If NVs don't preserve UVs
2212 then the value returned may have more precision than atof() will
2213 return, even though value isn't perfectly accurate. */
2214 if ((numtype & (IS_NUMBER_IN_UV
2215#ifdef NV_PRESERVES_UV
2216 | IS_NUMBER_NOT_INT
2217#endif
2218 )) == IS_NUMBER_IN_UV) {
2219 /* This won't turn off the public IOK flag if it was set above */
2220 (void)SvIOKp_on(sv);
2221
2222 if (!(numtype & IS_NUMBER_NEG)) {
2223 /* positive */;
2224 if (value <= (UV)IV_MAX) {
2225 SvIVX(sv) = (IV)value;
2226 } else {
2227 SvUVX(sv) = value;
2228 SvIsUV_on(sv);
2229 }
2230 } else {
2231 /* 2s complement assumption */
2232 if (value <= (UV)IV_MIN) {
2233 SvIVX(sv) = -(IV)value;
2234 } else {
2235 /* Too negative for an IV. This is a double upgrade, but
d1be9408 2236 I'm assuming it will be rare. */
c2988b20
NC
2237 if (SvTYPE(sv) < SVt_PVNV)
2238 sv_upgrade(sv, SVt_PVNV);
2239 SvNOK_on(sv);
2240 SvIOK_off(sv);
2241 SvIOKp_on(sv);
2242 SvNVX(sv) = -(NV)value;
2243 SvIVX(sv) = IV_MIN;
2244 }
2245 }
2246 }
2247 /* For !NV_PRESERVES_UV and IS_NUMBER_IN_UV and IS_NUMBER_NOT_INT we
2248 will be in the previous block to set the IV slot, and the next
2249 block to set the NV slot. So no else here. */
2250
2251 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2252 != IS_NUMBER_IN_UV) {
2253 /* It wasn't an (integer that doesn't overflow the UV). */
2254 SvNVX(sv) = Atof(SvPVX(sv));
28e5dec8 2255
c2988b20
NC
2256 if (! numtype && ckWARN(WARN_NUMERIC))
2257 not_a_number(sv);
28e5dec8 2258
65202027 2259#if defined(USE_LONG_DOUBLE)
c2988b20
NC
2260 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%" PERL_PRIgldbl ")\n",
2261 PTR2UV(sv), SvNVX(sv)));
65202027 2262#else
1779d84d 2263 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"NVgf")\n",
c2988b20 2264 PTR2UV(sv), SvNVX(sv)));
65202027 2265#endif
28e5dec8
JH
2266
2267
2268#ifdef NV_PRESERVES_UV
c2988b20
NC
2269 (void)SvIOKp_on(sv);
2270 (void)SvNOK_on(sv);
2271 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2272 SvIVX(sv) = I_V(SvNVX(sv));
2273 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
2274 SvIOK_on(sv);
28e5dec8 2275 } else {
c2988b20
NC
2276 /* Integer is imprecise. NOK, IOKp */
2277 }
2278 /* UV will not work better than IV */
2279 } else {
2280 if (SvNVX(sv) > (NV)UV_MAX) {
2281 SvIsUV_on(sv);
2282 /* Integer is inaccurate. NOK, IOKp, is UV */
2283 SvUVX(sv) = UV_MAX;
2284 SvIsUV_on(sv);
2285 } else {
2286 SvUVX(sv) = U_V(SvNVX(sv));
2287 /* 0xFFFFFFFFFFFFFFFF not an issue in here */
2288 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
2289 SvIOK_on(sv);
28e5dec8
JH
2290 SvIsUV_on(sv);
2291 } else {
c2988b20
NC
2292 /* Integer is imprecise. NOK, IOKp, is UV */
2293 SvIsUV_on(sv);
28e5dec8 2294 }
28e5dec8 2295 }
c2988b20
NC
2296 goto ret_iv_max;
2297 }
28e5dec8 2298#else /* NV_PRESERVES_UV */
c2988b20
NC
2299 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2300 == (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) {
2301 /* The IV slot will have been set from value returned by
2302 grok_number above. The NV slot has just been set using
2303 Atof. */
560b0c46 2304 SvNOK_on(sv);
c2988b20
NC
2305 assert (SvIOKp(sv));
2306 } else {
2307 if (((UV)1 << NV_PRESERVES_UV_BITS) >
2308 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
2309 /* Small enough to preserve all bits. */
2310 (void)SvIOKp_on(sv);
2311 SvNOK_on(sv);
2312 SvIVX(sv) = I_V(SvNVX(sv));
2313 if ((NV)(SvIVX(sv)) == SvNVX(sv))
2314 SvIOK_on(sv);
2315 /* Assumption: first non-preserved integer is < IV_MAX,
2316 this NV is in the preserved range, therefore: */
2317 if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))
2318 < (UV)IV_MAX)) {
6b6e5432 2319 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
2320 }
2321 } else {
2322 /* IN_UV NOT_INT
2323 0 0 already failed to read UV.
2324 0 1 already failed to read UV.
2325 1 0 you won't get here in this case. IV/UV
2326 slot set, public IOK, Atof() unneeded.
2327 1 1 already read UV.
2328 so there's no point in sv_2iuv_non_preserve() attempting
2329 to use atol, strtol, strtoul etc. */
2330 if (sv_2iuv_non_preserve (sv, numtype)
2331 >= IS_NUMBER_OVERFLOW_IV)
2332 goto ret_iv_max;
2333 }
2334 }
28e5dec8 2335#endif /* NV_PRESERVES_UV */
25da4f38 2336 }
28e5dec8 2337 } else {
599cee73 2338 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
1d7c1841 2339 report_uninit();
25da4f38
IZ
2340 if (SvTYPE(sv) < SVt_IV)
2341 /* Typically the caller expects that sv_any is not NULL now. */
2342 sv_upgrade(sv, SVt_IV);
a0d0e21e 2343 return 0;
79072805 2344 }
1d7c1841
GS
2345 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"IVdf")\n",
2346 PTR2UV(sv),SvIVX(sv)));
25da4f38 2347 return SvIsUV(sv) ? (IV)SvUVX(sv) : SvIVX(sv);
79072805
LW
2348}
2349
645c22ef
DM
2350/*
2351=for apidoc sv_2uv
2352
2353Return the unsigned integer value of an SV, doing any necessary string
2354conversion, magic etc. Normally used via the C<SvUV(sv)> and C<SvUVx(sv)>
2355macros.
2356
2357=cut
2358*/
2359
ff68c719 2360UV
864dbfa3 2361Perl_sv_2uv(pTHX_ register SV *sv)
ff68c719 2362{
2363 if (!sv)
2364 return 0;
2365 if (SvGMAGICAL(sv)) {
2366 mg_get(sv);
2367 if (SvIOKp(sv))
2368 return SvUVX(sv);
2369 if (SvNOKp(sv))
2370 return U_V(SvNVX(sv));
36477c24 2371 if (SvPOKp(sv) && SvLEN(sv))
2372 return asUV(sv);
3fe9a6f1 2373 if (!SvROK(sv)) {
d008e5eb 2374 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
d008e5eb 2375 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
1d7c1841 2376 report_uninit();
c6ee37c5 2377 }
36477c24 2378 return 0;
3fe9a6f1 2379 }
ff68c719 2380 }
2381 if (SvTHINKFIRST(sv)) {
2382 if (SvROK(sv)) {
ff68c719 2383 SV* tmpstr;
1554e226 2384 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
b4b9a328 2385 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv))))
9e7bc3e8 2386 return SvUV(tmpstr);
56431972 2387 return PTR2UV(SvRV(sv));
ff68c719 2388 }
8a818333
NIS
2389 if (SvREADONLY(sv) && SvFAKE(sv)) {
2390 sv_force_normal(sv);
2391 }
0336b60e 2392 if (SvREADONLY(sv) && !SvOK(sv)) {
0336b60e 2393 if (ckWARN(WARN_UNINITIALIZED))
1d7c1841 2394 report_uninit();
ff68c719 2395 return 0;
2396 }
2397 }
25da4f38
IZ
2398 if (SvIOKp(sv)) {
2399 if (SvIsUV(sv)) {
2400 return SvUVX(sv);
2401 }
2402 else {
2403 return (UV)SvIVX(sv);
2404 }
ff68c719 2405 }
2406 if (SvNOKp(sv)) {
28e5dec8
JH
2407 /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv
2408 * without also getting a cached IV/UV from it at the same time
2409 * (ie PV->NV conversion should detect loss of accuracy and cache
2410 * IV or UV at same time to avoid this. */
2411 /* IV-over-UV optimisation - choose to cache IV if possible */
2412
25da4f38
IZ
2413 if (SvTYPE(sv) == SVt_NV)
2414 sv_upgrade(sv, SVt_PVNV);
28e5dec8
JH
2415
2416 (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */
2417 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
f7bbb42a 2418 SvIVX(sv) = I_V(SvNVX(sv));
28e5dec8
JH
2419 if (SvNVX(sv) == (NV) SvIVX(sv)
2420#ifndef NV_PRESERVES_UV
2421 && (((UV)1 << NV_PRESERVES_UV_BITS) >
2422 (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv)))
2423 /* Don't flag it as "accurately an integer" if the number
2424 came from a (by definition imprecise) NV operation, and
2425 we're outside the range of NV integer precision */
2426#endif
2427 ) {
2428 SvIOK_on(sv); /* Can this go wrong with rounding? NWC */
2429 DEBUG_c(PerlIO_printf(Perl_debug_log,
7234c960 2430 "0x%"UVxf" uv(%"NVgf" => %"IVdf") (precise)\n",
28e5dec8
JH
2431 PTR2UV(sv),
2432 SvNVX(sv),
2433 SvIVX(sv)));
2434
2435 } else {
2436 /* IV not precise. No need to convert from PV, as NV
2437 conversion would already have cached IV if it detected
2438 that PV->IV would be better than PV->NV->IV
2439 flags already correct - don't set public IOK. */
2440 DEBUG_c(PerlIO_printf(Perl_debug_log,
7234c960 2441 "0x%"UVxf" uv(%"NVgf" => %"IVdf") (imprecise)\n",
28e5dec8
JH
2442 PTR2UV(sv),
2443 SvNVX(sv),
2444 SvIVX(sv)));
2445 }
2446 /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN,
2447 but the cast (NV)IV_MIN rounds to a the value less (more
2448 negative) than IV_MIN which happens to be equal to SvNVX ??
2449 Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and
2450 NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and
2451 (NV)UVX == NVX are both true, but the values differ. :-(
2452 Hopefully for 2s complement IV_MIN is something like
2453 0x8000000000000000 which will be exact. NWC */
d460ef45 2454 }
28e5dec8
JH
2455 else {
2456 SvUVX(sv) = U_V(SvNVX(sv));
2457 if (
2458 (SvNVX(sv) == (NV) SvUVX(sv))
2459#ifndef NV_PRESERVES_UV
2460 /* Make sure it's not 0xFFFFFFFFFFFFFFFF */
2461 /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */
2462 && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv))
2463 /* Don't flag it as "accurately an integer" if the number
2464 came from a (by definition imprecise) NV operation, and
2465 we're outside the range of NV integer precision */
2466#endif
2467 )
2468 SvIOK_on(sv);
2469 SvIsUV_on(sv);
1c846c1f 2470 DEBUG_c(PerlIO_printf(Perl_debug_log,
28e5dec8 2471 "0x%"UVxf" 2uv(%"UVuf" => %"IVdf") (as unsigned)\n",
57def98f 2472 PTR2UV(sv),
28e5dec8
JH
2473 SvUVX(sv),
2474 SvUVX(sv)));
25da4f38 2475 }
ff68c719 2476 }
2477 else if (SvPOKp(sv) && SvLEN(sv)) {
c2988b20
NC
2478 UV value;
2479 int numtype = grok_number(SvPVX(sv), SvCUR(sv), &value);
25da4f38
IZ
2480
2481 /* We want to avoid a possible problem when we cache a UV which
2482 may be later translated to an NV, and the resulting NV is not
2483 the translation of the initial data.
1c846c1f 2484
25da4f38
IZ
2485 This means that if we cache such a UV, we need to cache the
2486 NV as well. Moreover, we trade speed for space, and do not
2487 cache the NV if not needed.
2488 */
16b7a9a4 2489
c2988b20
NC
2490 /* SVt_PVNV is one higher than SVt_PVIV, hence this order */
2491 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2492 == IS_NUMBER_IN_UV) {
5e045b90 2493 /* It's definitely an integer, only upgrade to PVIV */
28e5dec8 2494 if (SvTYPE(sv) < SVt_PVIV)
f7bbb42a
JH
2495 sv_upgrade(sv, SVt_PVIV);
2496 (void)SvIOK_on(sv);
c2988b20
NC
2497 } else if (SvTYPE(sv) < SVt_PVNV)
2498 sv_upgrade(sv, SVt_PVNV);
d460ef45 2499
c2988b20
NC
2500 /* If NV preserves UV then we only use the UV value if we know that
2501 we aren't going to call atof() below. If NVs don't preserve UVs
2502 then the value returned may have more precision than atof() will
2503 return, even though it isn't accurate. */
2504 if ((numtype & (IS_NUMBER_IN_UV
2505#ifdef NV_PRESERVES_UV
2506 | IS_NUMBER_NOT_INT
2507#endif
2508 )) == IS_NUMBER_IN_UV) {
2509 /* This won't turn off the public IOK flag if it was set above */
2510 (void)SvIOKp_on(sv);
2511
2512 if (!(numtype & IS_NUMBER_NEG)) {
2513 /* positive */;
2514 if (value <= (UV)IV_MAX) {
2515 SvIVX(sv) = (IV)value;
28e5dec8
JH
2516 } else {
2517 /* it didn't overflow, and it was positive. */
c2988b20 2518 SvUVX(sv) = value;
28e5dec8
JH
2519 SvIsUV_on(sv);
2520 }
c2988b20
NC
2521 } else {
2522 /* 2s complement assumption */
2523 if (value <= (UV)IV_MIN) {
2524 SvIVX(sv) = -(IV)value;
2525 } else {
2526 /* Too negative for an IV. This is a double upgrade, but
d1be9408 2527 I'm assuming it will be rare. */
c2988b20
NC
2528 if (SvTYPE(sv) < SVt_PVNV)
2529 sv_upgrade(sv, SVt_PVNV);
2530 SvNOK_on(sv);
2531 SvIOK_off(sv);
2532 SvIOKp_on(sv);
2533 SvNVX(sv) = -(NV)value;
2534 SvIVX(sv) = IV_MIN;
2535 }
2536 }
2537 }
2538
2539 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2540 != IS_NUMBER_IN_UV) {
2541 /* It wasn't an integer, or it overflowed the UV. */
2542 SvNVX(sv) = Atof(SvPVX(sv));
28e5dec8 2543
c2988b20 2544 if (! numtype && ckWARN(WARN_NUMERIC))
28e5dec8
JH
2545 not_a_number(sv);
2546
2547#if defined(USE_LONG_DOUBLE)
c2988b20
NC
2548 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%" PERL_PRIgldbl ")\n",
2549 PTR2UV(sv), SvNVX(sv)));
28e5dec8 2550#else
1779d84d 2551 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"NVgf")\n",
c2988b20 2552 PTR2UV(sv), SvNVX(sv)));
28e5dec8
JH
2553#endif
2554
2555#ifdef NV_PRESERVES_UV
c2988b20
NC
2556 (void)SvIOKp_on(sv);
2557 (void)SvNOK_on(sv);
2558 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2559 SvIVX(sv) = I_V(SvNVX(sv));
2560 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
2561 SvIOK_on(sv);
2562 } else {
2563 /* Integer is imprecise. NOK, IOKp */
2564 }
2565 /* UV will not work better than IV */
2566 } else {
2567 if (SvNVX(sv) > (NV)UV_MAX) {
2568 SvIsUV_on(sv);
2569 /* Integer is inaccurate. NOK, IOKp, is UV */
2570 SvUVX(sv) = UV_MAX;
2571 SvIsUV_on(sv);
2572 } else {
2573 SvUVX(sv) = U_V(SvNVX(sv));
2574 /* 0xFFFFFFFFFFFFFFFF not an issue in here, NVs
2575 NV preservse UV so can do correct comparison. */
2576 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
2577 SvIOK_on(sv);
2578 SvIsUV_on(sv);
2579 } else {
2580 /* Integer is imprecise. NOK, IOKp, is UV */
2581 SvIsUV_on(sv);
2582 }
2583 }
2584 }
28e5dec8 2585#else /* NV_PRESERVES_UV */
c2988b20
NC
2586 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2587 == (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) {
2588 /* The UV slot will have been set from value returned by
2589 grok_number above. The NV slot has just been set using
2590 Atof. */
560b0c46 2591 SvNOK_on(sv);
c2988b20
NC
2592 assert (SvIOKp(sv));
2593 } else {
2594 if (((UV)1 << NV_PRESERVES_UV_BITS) >
2595 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
2596 /* Small enough to preserve all bits. */
2597 (void)SvIOKp_on(sv);
2598 SvNOK_on(sv);
2599 SvIVX(sv) = I_V(SvNVX(sv));
2600 if ((NV)(SvIVX(sv)) == SvNVX(sv))
2601 SvIOK_on(sv);
2602 /* Assumption: first non-preserved integer is < IV_MAX,
2603 this NV is in the preserved range, therefore: */
2604 if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))
2605 < (UV)IV_MAX)) {
6b6e5432 2606 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
2607 }
2608 } else
2609 sv_2iuv_non_preserve (sv, numtype);
2610 }
28e5dec8 2611#endif /* NV_PRESERVES_UV */
f7bbb42a 2612 }
ff68c719 2613 }
2614 else {
d008e5eb 2615 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
d008e5eb 2616 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
1d7c1841 2617 report_uninit();
c6ee37c5 2618 }
25da4f38
IZ
2619 if (SvTYPE(sv) < SVt_IV)
2620 /* Typically the caller expects that sv_any is not NULL now. */
2621 sv_upgrade(sv, SVt_IV);
ff68c719 2622 return 0;
2623 }
25da4f38 2624
1d7c1841
GS
2625 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"UVuf")\n",
2626 PTR2UV(sv),SvUVX(sv)));
25da4f38 2627 return SvIsUV(sv) ? SvUVX(sv) : (UV)SvIVX(sv);
ff68c719 2628}
2629
645c22ef
DM
2630/*
2631=for apidoc sv_2nv
2632
2633Return the num value of an SV, doing any necessary string or integer
2634conversion, magic etc. Normally used via the C<SvNV(sv)> and C<SvNVx(sv)>
2635macros.
2636
2637=cut
2638*/
2639
65202027 2640NV
864dbfa3 2641Perl_sv_2nv(pTHX_ register SV *sv)
79072805
LW
2642{
2643 if (!sv)
2644 return 0.0;
8990e307 2645 if (SvGMAGICAL(sv)) {
463ee0b2
LW
2646 mg_get(sv);
2647 if (SvNOKp(sv))
2648 return SvNVX(sv);
a0d0e21e 2649 if (SvPOKp(sv) && SvLEN(sv)) {
c2988b20
NC
2650 if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) &&
2651 !grok_number(SvPVX(sv), SvCUR(sv), NULL))
a0d0e21e 2652 not_a_number(sv);
097ee67d 2653 return Atof(SvPVX(sv));
a0d0e21e 2654 }
25da4f38 2655 if (SvIOKp(sv)) {
1c846c1f 2656 if (SvIsUV(sv))
65202027 2657 return (NV)SvUVX(sv);
25da4f38 2658 else
65202027 2659 return (NV)SvIVX(sv);
25da4f38 2660 }
16d20bd9 2661 if (!SvROK(sv)) {
d008e5eb 2662 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
d008e5eb 2663 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
1d7c1841 2664 report_uninit();
c6ee37c5 2665 }
16d20bd9
AD
2666 return 0;
2667 }
463ee0b2 2668 }
ed6116ce 2669 if (SvTHINKFIRST(sv)) {
a0d0e21e 2670 if (SvROK(sv)) {
a0d0e21e 2671 SV* tmpstr;
1554e226 2672 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
b4b9a328 2673 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv))))
9e7bc3e8 2674 return SvNV(tmpstr);
56431972 2675 return PTR2NV(SvRV(sv));
a0d0e21e 2676 }
8a818333
NIS
2677 if (SvREADONLY(sv) && SvFAKE(sv)) {
2678 sv_force_normal(sv);
2679 }
0336b60e 2680 if (SvREADONLY(sv) && !SvOK(sv)) {
599cee73 2681 if (ckWARN(WARN_UNINITIALIZED))
1d7c1841 2682 report_uninit();
ed6116ce
LW
2683 return 0.0;
2684 }
79072805
LW
2685 }
2686 if (SvTYPE(sv) < SVt_NV) {
463ee0b2
LW
2687 if (SvTYPE(sv) == SVt_IV)
2688 sv_upgrade(sv, SVt_PVNV);
2689 else
2690 sv_upgrade(sv, SVt_NV);
906f284f 2691#ifdef USE_LONG_DOUBLE
097ee67d 2692 DEBUG_c({
f93f4e46 2693 STORE_NUMERIC_LOCAL_SET_STANDARD();
1d7c1841
GS
2694 PerlIO_printf(Perl_debug_log,
2695 "0x%"UVxf" num(%" PERL_PRIgldbl ")\n",
2696 PTR2UV(sv), SvNVX(sv));
572bbb43
GS
2697 RESTORE_NUMERIC_LOCAL();
2698 });
65202027 2699#else
572bbb43 2700 DEBUG_c({
f93f4e46 2701 STORE_NUMERIC_LOCAL_SET_STANDARD();
1779d84d 2702 PerlIO_printf(Perl_debug_log, "0x%"UVxf" num(%"NVgf")\n",
1d7c1841 2703 PTR2UV(sv), SvNVX(sv));
097ee67d
JH
2704 RESTORE_NUMERIC_LOCAL();
2705 });
572bbb43 2706#endif
79072805
LW
2707 }
2708 else if (SvTYPE(sv) < SVt_PVNV)
2709 sv_upgrade(sv, SVt_PVNV);
59d8ce62
NC
2710 if (SvNOKp(sv)) {
2711 return SvNVX(sv);
61604483 2712 }
59d8ce62 2713 if (SvIOKp(sv)) {
65202027 2714 SvNVX(sv) = SvIsUV(sv) ? (NV)SvUVX(sv) : (NV)SvIVX(sv);
28e5dec8
JH
2715#ifdef NV_PRESERVES_UV
2716 SvNOK_on(sv);
2717#else
2718 /* Only set the public NV OK flag if this NV preserves the IV */
2719 /* Check it's not 0xFFFFFFFFFFFFFFFF */
2720 if (SvIsUV(sv) ? ((SvUVX(sv) != UV_MAX)&&(SvUVX(sv) == U_V(SvNVX(sv))))
2721 : (SvIVX(sv) == I_V(SvNVX(sv))))
2722 SvNOK_on(sv);
2723 else
2724 SvNOKp_on(sv);
2725#endif
93a17b20 2726 }
748a9306 2727 else if (SvPOKp(sv) && SvLEN(sv)) {
c2988b20
NC
2728 UV value;
2729 int numtype = grok_number(SvPVX(sv), SvCUR(sv), &value);
2730 if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) && !numtype)
a0d0e21e 2731 not_a_number(sv);
28e5dec8 2732#ifdef NV_PRESERVES_UV
c2988b20
NC
2733 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2734 == IS_NUMBER_IN_UV) {
5e045b90 2735 /* It's definitely an integer */
c2988b20
NC
2736 SvNVX(sv) = (numtype & IS_NUMBER_NEG) ? -(NV)value : (NV)value;
2737 } else
2738 SvNVX(sv) = Atof(SvPVX(sv));
28e5dec8
JH
2739 SvNOK_on(sv);
2740#else
c2988b20 2741 SvNVX(sv) = Atof(SvPVX(sv));
28e5dec8
JH
2742 /* Only set the public NV OK flag if this NV preserves the value in
2743 the PV at least as well as an IV/UV would.
2744 Not sure how to do this 100% reliably. */
2745 /* if that shift count is out of range then Configure's test is
2746 wonky. We shouldn't be in here with NV_PRESERVES_UV_BITS ==
2747 UV_BITS */
2748 if (((UV)1 << NV_PRESERVES_UV_BITS) >
c2988b20 2749 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
28e5dec8 2750 SvNOK_on(sv); /* Definitely small enough to preserve all bits */
c2988b20
NC
2751 } else if (!(numtype & IS_NUMBER_IN_UV)) {
2752 /* Can't use strtol etc to convert this string, so don't try.
2753 sv_2iv and sv_2uv will use the NV to convert, not the PV. */
2754 SvNOK_on(sv);
2755 } else {
2756 /* value has been set. It may not be precise. */
2757 if ((numtype & IS_NUMBER_NEG) && (value > (UV)IV_MIN)) {
2758 /* 2s complement assumption for (UV)IV_MIN */
2759 SvNOK_on(sv); /* Integer is too negative. */
2760 } else {
2761 SvNOKp_on(sv);
2762 SvIOKp_on(sv);
6fa402ec 2763
c2988b20
NC
2764 if (numtype & IS_NUMBER_NEG) {
2765 SvIVX(sv) = -(IV)value;
2766 } else if (value <= (UV)IV_MAX) {
2767 SvIVX(sv) = (IV)value;
2768 } else {
2769 SvUVX(sv) = value;
2770 SvIsUV_on(sv);
2771 }
2772
2773 if (numtype & IS_NUMBER_NOT_INT) {
2774 /* I believe that even if the original PV had decimals,
2775 they are lost beyond the limit of the FP precision.
2776 However, neither is canonical, so both only get p
2777 flags. NWC, 2000/11/25 */
2778 /* Both already have p flags, so do nothing */
2779 } else {
2780 NV nv = SvNVX(sv);
2781 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2782 if (SvIVX(sv) == I_V(nv)) {
2783 SvNOK_on(sv);
2784 SvIOK_on(sv);
2785 } else {
2786 SvIOK_on(sv);
2787 /* It had no "." so it must be integer. */
2788 }
2789 } else {
2790 /* between IV_MAX and NV(UV_MAX).
2791 Could be slightly > UV_MAX */
6fa402ec 2792
c2988b20
NC
2793 if (numtype & IS_NUMBER_NOT_INT) {
2794 /* UV and NV both imprecise. */
2795 } else {
2796 UV nv_as_uv = U_V(nv);
2797
2798 if (value == nv_as_uv && SvUVX(sv) != UV_MAX) {
2799 SvNOK_on(sv);
2800 SvIOK_on(sv);
2801 } else {
2802 SvIOK_on(sv);
2803 }
2804 }
2805 }
2806 }
2807 }
2808 }
28e5dec8 2809#endif /* NV_PRESERVES_UV */
93a17b20 2810 }
79072805 2811 else {
599cee73 2812 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
1d7c1841 2813 report_uninit();
25da4f38
IZ
2814 if (SvTYPE(sv) < SVt_NV)
2815 /* Typically the caller expects that sv_any is not NULL now. */
28e5dec8
JH
2816 /* XXX Ilya implies that this is a bug in callers that assume this
2817 and ideally should be fixed. */
25da4f38 2818 sv_upgrade(sv, SVt_NV);
a0d0e21e 2819 return 0.0;
79072805 2820 }
572bbb43 2821#if defined(USE_LONG_DOUBLE)
097ee67d 2822 DEBUG_c({
f93f4e46 2823 STORE_NUMERIC_LOCAL_SET_STANDARD();
1d7c1841
GS
2824 PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2nv(%" PERL_PRIgldbl ")\n",
2825 PTR2UV(sv), SvNVX(sv));
572bbb43
GS
2826 RESTORE_NUMERIC_LOCAL();
2827 });
65202027 2828#else
572bbb43 2829 DEBUG_c({
f93f4e46 2830 STORE_NUMERIC_LOCAL_SET_STANDARD();
1779d84d 2831 PerlIO_printf(Perl_debug_log, "0x%"UVxf" 1nv(%"NVgf")\n",
1d7c1841 2832 PTR2UV(sv), SvNVX(sv));
097ee67d
JH
2833 RESTORE_NUMERIC_LOCAL();
2834 });
572bbb43 2835#endif
463ee0b2 2836 return SvNVX(sv);
79072805
LW
2837}
2838
645c22ef
DM
2839/* asIV(): extract an integer from the string value of an SV.
2840 * Caller must validate PVX */
2841
76e3520e 2842STATIC IV
cea2e8a9 2843S_asIV(pTHX_ SV *sv)
36477c24 2844{
c2988b20
NC
2845 UV value;
2846 int numtype = grok_number(SvPVX(sv), SvCUR(sv), &value);
2847
2848 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2849 == IS_NUMBER_IN_UV) {
645c22ef 2850 /* It's definitely an integer */
c2988b20
NC
2851 if (numtype & IS_NUMBER_NEG) {
2852 if (value < (UV)IV_MIN)
2853 return -(IV)value;
2854 } else {
2855 if (value < (UV)IV_MAX)
2856 return (IV)value;
2857 }
2858 }
d008e5eb 2859 if (!numtype) {
d008e5eb
GS
2860 if (ckWARN(WARN_NUMERIC))
2861 not_a_number(sv);
2862 }
c2988b20 2863 return I_V(Atof(SvPVX(sv)));
36477c24 2864}
2865
645c22ef
DM
2866/* asUV(): extract an unsigned integer from the string value of an SV
2867 * Caller must validate PVX */
2868
76e3520e 2869STATIC UV
cea2e8a9 2870S_asUV(pTHX_ SV *sv)
36477c24 2871{
c2988b20
NC
2872 UV value;
2873 int numtype = grok_number(SvPVX(sv), SvCUR(sv), &value);
36477c24 2874
c2988b20
NC
2875 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2876 == IS_NUMBER_IN_UV) {
645c22ef 2877 /* It's definitely an integer */
6fa402ec 2878 if (!(numtype & IS_NUMBER_NEG))
c2988b20
NC
2879 return value;
2880 }
d008e5eb 2881 if (!numtype) {
d008e5eb
GS
2882 if (ckWARN(WARN_NUMERIC))
2883 not_a_number(sv);
2884 }
097ee67d 2885 return U_V(Atof(SvPVX(sv)));
36477c24 2886}
2887
645c22ef
DM
2888/*
2889=for apidoc sv_2pv_nolen
2890
2891Like C<sv_2pv()>, but doesn't return the length too. You should usually
2892use the macro wrapper C<SvPV_nolen(sv)> instead.
2893=cut
2894*/
2895
79072805 2896char *
864dbfa3 2897Perl_sv_2pv_nolen(pTHX_ register SV *sv)
1fa8b10d
JD
2898{
2899 STRLEN n_a;
2900 return sv_2pv(sv, &n_a);
2901}
2902
645c22ef
DM
2903/* uiv_2buf(): private routine for use by sv_2pv_flags(): print an IV or
2904 * UV as a string towards the end of buf, and return pointers to start and
2905 * end of it.
2906 *
2907 * We assume that buf is at least TYPE_CHARS(UV) long.
2908 */
2909
864dbfa3 2910static char *
25da4f38
IZ
2911uiv_2buf(char *buf, IV iv, UV uv, int is_uv, char **peob)
2912{
25da4f38
IZ
2913 char *ptr = buf + TYPE_CHARS(UV);
2914 char *ebuf = ptr;
2915 int sign;
25da4f38
IZ
2916
2917 if (is_uv)
2918 sign = 0;
2919 else if (iv >= 0) {
2920 uv = iv;
2921 sign = 0;
2922 } else {
2923 uv = -iv;
2924 sign = 1;
2925 }
2926 do {
eb160463 2927 *--ptr = '0' + (char)(uv % 10);
25da4f38
IZ
2928 } while (uv /= 10);
2929 if (sign)
2930 *--ptr = '-';
2931 *peob = ebuf;
2932 return ptr;
2933}
2934
d34f9d2e
JH
2935/* sv_2pv() is now a macro using Perl_sv_2pv_flags();
2936 * this function provided for binary compatibility only
2937 */
2938
2939char *
2940Perl_sv_2pv(pTHX_ register SV *sv, STRLEN *lp)
2941{
2942 return sv_2pv_flags(sv, lp, SV_GMAGIC);
2943}
2944
645c22ef
DM
2945/*
2946=for apidoc sv_2pv_flags
2947
ff276b08 2948Returns a pointer to the string value of an SV, and sets *lp to its length.
645c22ef
DM
2949If flags includes SV_GMAGIC, does an mg_get() first. Coerces sv to a string
2950if necessary.
2951Normally invoked via the C<SvPV_flags> macro. C<sv_2pv()> and C<sv_2pv_nomg>
2952usually end up here too.
2953
2954=cut
2955*/
2956
8d6d96c1
HS
2957char *
2958Perl_sv_2pv_flags(pTHX_ register SV *sv, STRLEN *lp, I32 flags)
2959{
79072805
LW
2960 register char *s;
2961 int olderrno;
5b7ea690 2962 SV *tsv, *origsv;
25da4f38
IZ
2963 char tbuf[64]; /* Must fit sprintf/Gconvert of longest IV/NV */
2964 char *tmpbuf = tbuf;
79072805 2965
463ee0b2
LW
2966 if (!sv) {
2967 *lp = 0;
2968 return "";
2969 }
8990e307 2970 if (SvGMAGICAL(sv)) {
8d6d96c1
HS
2971 if (flags & SV_GMAGIC)
2972 mg_get(sv);
463ee0b2
LW
2973 if (SvPOKp(sv)) {
2974 *lp = SvCUR(sv);
2975 return SvPVX(sv);
2976 }
cf2093f6 2977 if (SvIOKp(sv)) {
1c846c1f 2978 if (SvIsUV(sv))
57def98f 2979 (void)sprintf(tmpbuf,"%"UVuf, (UV)SvUVX(sv));
cf2093f6 2980 else
57def98f 2981 (void)sprintf(tmpbuf,"%"IVdf, (IV)SvIVX(sv));
46fc3d4c 2982 tsv = Nullsv;
a0d0e21e 2983 goto tokensave;
463ee0b2
LW
2984 }
2985 if (SvNOKp(sv)) {
2d4389e4 2986 Gconvert(SvNVX(sv), NV_DIG, 0, tmpbuf);
46fc3d4c 2987 tsv = Nullsv;
a0d0e21e 2988 goto tokensave;
463ee0b2 2989 }
16d20bd9 2990 if (!SvROK(sv)) {
d008e5eb 2991 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
d008e5eb 2992 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
1d7c1841 2993 report_uninit();
c6ee37c5 2994 }
16d20bd9
AD
2995 *lp = 0;
2996 return "";
2997 }
463ee0b2 2998 }
ed6116ce
LW
2999 if (SvTHINKFIRST(sv)) {
3000 if (SvROK(sv)) {
a0d0e21e 3001 SV* tmpstr;
1554e226 3002 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,string)) &&
b4b9a328 3003 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
5b7ea690
JH
3004 char *pv = SvPV(tmpstr, *lp);
3005 if (SvUTF8(tmpstr))
3006 SvUTF8_on(sv);
3007 else
3008 SvUTF8_off(sv);
3009 return pv;
3010 }
3011 origsv = sv;
ed6116ce
LW
3012 sv = (SV*)SvRV(sv);
3013 if (!sv)
3014 s = "NULLREF";
3015 else {
f9277f47
IZ
3016 MAGIC *mg;
3017
ed6116ce 3018 switch (SvTYPE(sv)) {
f9277f47
IZ
3019 case SVt_PVMG:
3020 if ( ((SvFLAGS(sv) &
1c846c1f 3021 (SVs_OBJECT|SVf_OK|SVs_GMG|SVs_SMG|SVs_RMG))
5835a535 3022 == (SVs_OBJECT|SVs_SMG))
14befaf4 3023 && (mg = mg_find(sv, PERL_MAGIC_qr))) {
2cd61cdb 3024 regexp *re = (regexp *)mg->mg_obj;
1bd3ad17 3025
2cd61cdb 3026 if (!mg->mg_ptr) {
8782bef2
GB
3027 char *fptr = "msix";
3028 char reflags[6];
3029 char ch;
3030 int left = 0;
3031 int right = 4;
ff385a1b 3032 char need_newline = 0;
eb160463 3033 U16 reganch = (U16)((re->reganch & PMf_COMPILETIME) >> 12);
8782bef2 3034
155aba94 3035 while((ch = *fptr++)) {
8782bef2
GB
3036 if(reganch & 1) {
3037 reflags[left++] = ch;
3038 }
3039 else {
3040 reflags[right--] = ch;
3041 }
3042 reganch >>= 1;
3043 }
3044 if(left != 4) {
3045 reflags[left] = '-';
3046 left = 5;
3047 }
3048
3049 mg->mg_len = re->prelen + 4 + left;
ff385a1b
JF
3050 /*
3051 * If /x was used, we have to worry about a regex
3052 * ending with a comment later being embedded
3053 * within another regex. If so, we don't want this
3054 * regex's "commentization" to leak out to the
3055 * right part of the enclosing regex, we must cap
3056 * it with a newline.
3057 *
3058 * So, if /x was used, we scan backwards from the
3059 * end of the regex. If we find a '#' before we
3060 * find a newline, we need to add a newline
3061 * ourself. If we find a '\n' first (or if we
3062 * don't find '#' or '\n'), we don't need to add
3063 * anything. -jfriedl
3064 */
3065 if (PMf_EXTENDED & re->reganch)
3066 {
3067 char *endptr = re->precomp + re->prelen;
3068 while (endptr >= re->precomp)
3069 {
3070 char c = *(endptr--);
3071 if (c == '\n')
3072 break; /* don't need another */
3073 if (c == '#') {
3074 /* we end while in a comment, so we
3075 need a newline */
3076 mg->mg_len++; /* save space for it */
3077 need_newline = 1; /* note to add it */
5b7ea690 3078 break;
ff385a1b
JF
3079 }
3080 }
3081 }
3082
8782bef2
GB
3083 New(616, mg->mg_ptr, mg->mg_len + 1 + left, char);
3084 Copy("(?", mg->mg_ptr, 2, char);
3085 Copy(reflags, mg->mg_ptr+2, left, char);
3086 Copy(":", mg->mg_ptr+left+2, 1, char);
3087 Copy(re->precomp, mg->mg_ptr+3+left, re->prelen, char);
ff385a1b
JF
3088 if (need_newline)
3089 mg->mg_ptr[mg->mg_len - 2] = '\n';
1bd3ad17
IZ
3090 mg->mg_ptr[mg->mg_len - 1] = ')';
3091 mg->mg_ptr[mg->mg_len] = 0;
3092 }
3280af22 3093 PL_reginterp_cnt += re->program[0].next_off;
5b7ea690
JH
3094
3095 if (re->reganch & ROPT_UTF8)
3096 SvUTF8_on(origsv);
3097 else
3098 SvUTF8_off(origsv);
1bd3ad17
IZ
3099 *lp = mg->mg_len;
3100 return mg->mg_ptr;
f9277f47
IZ
3101 }
3102 /* Fall through */
ed6116ce
LW
3103 case SVt_NULL:
3104 case SVt_IV:
3105 case SVt_NV:
3106 case SVt_RV:
3107 case SVt_PV:
3108 case SVt_PVIV:
3109 case SVt_PVNV:
81689caa
HS
3110 case SVt_PVBM: if (SvROK(sv))
3111 s = "REF";
3112 else
3113 s = "SCALAR"; break;
ac388100
JH
3114 case SVt_PVLV: s = SvROK(sv) ? "REF"
3115 /* tied lvalues should appear to be
3116 * scalars for backwards compatitbility */
3117 : (LvTYPE(sv) == 't' || LvTYPE(sv) == 'T')
3118 ? "SCALAR" : "LVALUE"; break;
ed6116ce
LW
3119 case SVt_PVAV: s = "ARRAY"; break;
3120 case SVt_PVHV: s = "HASH"; break;
3121 case SVt_PVCV: s = "CODE"; break;
3122 case SVt_PVGV: s = "GLOB"; break;
1d2dff63 3123 case SVt_PVFM: s = "FORMAT"; break;
36477c24 3124 case SVt_PVIO: s = "IO"; break;
ed6116ce
LW
3125 default: s = "UNKNOWN"; break;
3126 }
46fc3d4c 3127 tsv = NEWSV(0,0);
c86bf373
AMS
3128 if (SvOBJECT(sv)) {
3129 HV *svs = SvSTASH(sv);
3130 Perl_sv_setpvf(
3131 aTHX_ tsv, "%s=%s",
3132 /* [20011101.072] This bandaid for C<package;>
3133 should eventually be removed. AMS 20011103 */
3134 (svs ? HvNAME(svs) : "<none>"), s
3135 );
3136 }
ed6116ce 3137 else
46fc3d4c 3138 sv_setpv(tsv, s);
57def98f 3139 Perl_sv_catpvf(aTHX_ tsv, "(0x%"UVxf")", PTR2UV(sv));
a0d0e21e 3140 goto tokensaveref;
463ee0b2 3141 }
ed6116ce
LW
3142 *lp = strlen(s);
3143 return s;
79072805 3144 }
0336b60e 3145 if (SvREADONLY(sv) && !SvOK(sv)) {
0336b60e 3146 if (ckWARN(WARN_UNINITIALIZED))
1d7c1841 3147 report_uninit();
ed6116ce
LW
3148 *lp = 0;
3149 return "";
79072805 3150 }
79072805 3151 }
28e5dec8
JH
3152 if (SvIOK(sv) || ((SvIOKp(sv) && !SvNOKp(sv)))) {
3153 /* I'm assuming that if both IV and NV are equally valid then
3154 converting the IV is going to be more efficient */
3155 U32 isIOK = SvIOK(sv);
3156 U32 isUIOK = SvIsUV(sv);
3157 char buf[TYPE_CHARS(UV)];
3158 char *ebuf, *ptr;
3159
3160 if (SvTYPE(sv) < SVt_PVIV)
3161 sv_upgrade(sv, SVt_PVIV);
3162 if (isUIOK)
3163 ptr = uiv_2buf(buf, 0, SvUVX(sv), 1, &ebuf);
3164 else
3165 ptr = uiv_2buf(buf, SvIVX(sv), 0, 0, &ebuf);
eb160463 3166 SvGROW(sv, (STRLEN)(ebuf - ptr + 1)); /* inlined from sv_setpvn */
28e5dec8
JH
3167 Move(ptr,SvPVX(sv),ebuf - ptr,char);
3168 SvCUR_set(sv, ebuf - ptr);
3169 s = SvEND(sv);
3170 *s = '\0';
3171 if (isIOK)
3172 SvIOK_on(sv);
3173 else
3174 SvIOKp_on(sv);
3175 if (isUIOK)
3176 SvIsUV_on(sv);
3177 }
3178 else if (SvNOKp(sv)) {
79072805
LW
3179 if (SvTYPE(sv) < SVt_PVNV)
3180 sv_upgrade(sv, SVt_PVNV);
1c846c1f 3181 /* The +20 is pure guesswork. Configure test needed. --jhi */
59155cc0 3182 SvGROW(sv, NV_DIG + 20);
463ee0b2 3183 s = SvPVX(sv);
79072805 3184 olderrno = errno; /* some Xenix systems wipe out errno here */
79072805 3185#ifdef apollo
463ee0b2 3186 if (SvNVX(sv) == 0.0)
79072805
LW
3187 (void)strcpy(s,"0");
3188 else
3189#endif /*apollo*/
bbce6d69 3190 {
2d4389e4 3191 Gconvert(SvNVX(sv), NV_DIG, 0, s);
bbce6d69 3192 }
79072805 3193 errno = olderrno;
a0d0e21e
LW
3194#ifdef FIXNEGATIVEZERO
3195 if (*s == '-' && s[1] == '0' && !s[2])
3196 strcpy(s,"0");
3197#endif
79072805
LW
3198 while (*s) s++;
3199#ifdef hcx
3200 if (s[-1] == '.')
46fc3d4c 3201 *--s = '\0';
79072805
LW
3202#endif
3203 }
79072805 3204 else {
0336b60e
IZ
3205 if (ckWARN(WARN_UNINITIALIZED)
3206 && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
1d7c1841 3207 report_uninit();
a0d0e21e 3208 *lp = 0;
25da4f38
IZ
3209 if (SvTYPE(sv) < SVt_PV)
3210 /* Typically the caller expects that sv_any is not NULL now. */
3211 sv_upgrade(sv, SVt_PV);
a0d0e21e 3212 return "";
79072805 3213 }
463ee0b2
LW
3214 *lp = s - SvPVX(sv);
3215 SvCUR_set(sv, *lp);
79072805 3216 SvPOK_on(sv);
1d7c1841
GS
3217 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n",
3218 PTR2UV(sv),SvPVX(sv)));
463ee0b2 3219 return SvPVX(sv);
a0d0e21e
LW
3220
3221 tokensave:
3222 if (SvROK(sv)) { /* XXX Skip this when sv_pvn_force calls */
3223 /* Sneaky stuff here */
3224
3225 tokensaveref:
46fc3d4c 3226 if (!tsv)
96827780 3227 tsv = newSVpv(tmpbuf, 0);
46fc3d4c 3228 sv_2mortal(tsv);
3229 *lp = SvCUR(tsv);
3230 return SvPVX(tsv);
a0d0e21e
LW
3231 }
3232 else {
3233 STRLEN len;
46fc3d4c 3234 char *t;
3235
3236 if (tsv) {
3237 sv_2mortal(tsv);
3238 t = SvPVX(tsv);
3239 len = SvCUR(tsv);
3240 }
3241 else {
96827780
MB
3242 t = tmpbuf;
3243 len = strlen(tmpbuf);
46fc3d4c 3244 }
a0d0e21e 3245#ifdef FIXNEGATIVEZERO
46fc3d4c 3246 if (len == 2 && t[0] == '-' && t[1] == '0') {
3247 t = "0";
3248 len = 1;
3249 }
a0d0e21e
LW
3250#endif
3251 (void)SvUPGRADE(sv, SVt_PV);
46fc3d4c 3252 *lp = len;
a0d0e21e
LW
3253 s = SvGROW(sv, len + 1);
3254 SvCUR_set(sv, len);
46fc3d4c 3255 (void)strcpy(s, t);
6bf554b4 3256 SvPOKp_on(sv);
a0d0e21e
LW
3257 return s;
3258 }
463ee0b2
LW
3259}
3260
645c22ef 3261/*
6050d10e
JP
3262=for apidoc sv_copypv
3263
3264Copies a stringified representation of the source SV into the
3265destination SV. Automatically performs any necessary mg_get and
54f0641b 3266coercion of numeric values into strings. Guaranteed to preserve
6050d10e 3267UTF-8 flag even from overloaded objects. Similar in nature to
54f0641b
NIS
3268sv_2pv[_flags] but operates directly on an SV instead of just the
3269string. Mostly uses sv_2pv_flags to do its work, except when that
6050d10e
JP
3270would lose the UTF-8'ness of the PV.
3271
3272=cut
3273*/
3274
3275void
3276Perl_sv_copypv(pTHX_ SV *dsv, register SV *ssv)
3277{
5b7ea690
JH
3278 STRLEN len;
3279 char *s;
3280 s = SvPV(ssv,len);
3281 sv_setpvn(dsv,s,len);
3282 if (SvUTF8(ssv))
3283 SvUTF8_on(dsv);
3284 else
3285 SvUTF8_off(dsv);
6050d10e
JP
3286}
3287
3288/*
645c22ef
DM
3289=for apidoc sv_2pvbyte_nolen
3290
3291Return a pointer to the byte-encoded representation of the SV.
cd458e05 3292May cause the SV to be downgraded from UTF-8 as a side-effect.
645c22ef
DM
3293
3294Usually accessed via the C<SvPVbyte_nolen> macro.
3295
3296=cut
3297*/
3298
7340a771
GS
3299char *
3300Perl_sv_2pvbyte_nolen(pTHX_ register SV *sv)
3301{
560a288e
GS
3302 STRLEN n_a;
3303 return sv_2pvbyte(sv, &n_a);
7340a771
GS
3304}
3305
645c22ef
DM
3306/*
3307=for apidoc sv_2pvbyte
3308
3309Return a pointer to the byte-encoded representation of the SV, and set *lp
cd458e05 3310to its length. May cause the SV to be downgraded from UTF-8 as a
645c22ef
DM
3311side-effect.
3312
3313Usually accessed via the C<SvPVbyte> macro.
3314
3315=cut
3316*/
3317
7340a771
GS
3318char *
3319Perl_sv_2pvbyte(pTHX_ register SV *sv, STRLEN *lp)
3320{
0875d2fe
NIS
3321 sv_utf8_downgrade(sv,0);
3322 return SvPV(sv,*lp);
7340a771
GS
3323}
3324
645c22ef
DM
3325/*
3326=for apidoc sv_2pvutf8_nolen
3327
cd458e05
JH
3328Return a pointer to the UTF-8-encoded representation of the SV.
3329May cause the SV to be upgraded to UTF-8 as a side-effect.
645c22ef
DM
3330
3331Usually accessed via the C<SvPVutf8_nolen> macro.
3332
3333=cut
3334*/
3335
7340a771
GS
3336char *
3337Perl_sv_2pvutf8_nolen(pTHX_ register SV *sv)
3338{
560a288e
GS
3339 STRLEN n_a;
3340 return sv_2pvutf8(sv, &n_a);
7340a771
GS
3341}
3342
645c22ef
DM
3343/*
3344=for apidoc sv_2pvutf8
3345
cd458e05
JH
3346Return a pointer to the UTF-8-encoded representation of the SV, and set *lp
3347to its length. May cause the SV to be upgraded to UTF-8 as a side-effect.
645c22ef
DM
3348
3349Usually accessed via the C<SvPVutf8> macro.
3350
3351=cut
3352*/
3353
7340a771
GS
3354char *
3355Perl_sv_2pvutf8(pTHX_ register SV *sv, STRLEN *lp)
3356{
560a288e 3357 sv_utf8_upgrade(sv);
7d59b7e4 3358 return SvPV(sv,*lp);
7340a771 3359}
1c846c1f 3360
645c22ef
DM
3361/*
3362=for apidoc sv_2bool
3363
3364This function is only called on magical items, and is only used by
8cf8f3d1 3365sv_true() or its macro equivalent.
645c22ef
DM
3366
3367=cut
3368*/
3369
463ee0b2 3370bool
864dbfa3 3371Perl_sv_2bool(pTHX_ register SV *sv)
463ee0b2 3372{
8990e307 3373 if (SvGMAGICAL(sv))
463ee0b2
LW
3374 mg_get(sv);
3375
a0d0e21e
LW
3376 if (!SvOK(sv))
3377 return 0;
3378 if (SvROK(sv)) {
a0d0e21e 3379 SV* tmpsv;
1554e226 3380 if (SvAMAGIC(sv) && (tmpsv=AMG_CALLun(sv,bool_)) &&
9e3013b1 3381 (!SvROK(tmpsv) || (SvRV(tmpsv) != SvRV(sv))))
8a31060d 3382 return (bool)SvTRUE(tmpsv);
a0d0e21e
LW
3383 return SvRV(sv) != 0;
3384 }
463ee0b2 3385 if (SvPOKp(sv)) {
11343788
MB
3386 register XPV* Xpvtmp;
3387 if ((Xpvtmp = (XPV*)SvANY(sv)) &&
3388 (*Xpvtmp->xpv_pv > '0' ||
3389 Xpvtmp->xpv_cur > 1 ||
3390 (Xpvtmp->xpv_cur && *Xpvtmp->xpv_pv != '0')))
463ee0b2
LW
3391 return 1;
3392 else
3393 return 0;
3394 }
3395 else {
3396 if (SvIOKp(sv))
3397 return SvIVX(sv) != 0;
3398 else {
3399 if (SvNOKp(sv))
3400 return SvNVX(sv) != 0.0;
3401 else
3402 return FALSE;
3403 }
3404 }
79072805
LW
3405}
3406
d34f9d2e
JH
3407/* sv_utf8_upgrade() is now a macro using sv_utf8_upgrade_flags();
3408 * this function provided for binary compatibility only
3409 */
3410
3411
3412STRLEN
3413Perl_sv_utf8_upgrade(pTHX_ register SV *sv)
3414{
3415 return sv_utf8_upgrade_flags(sv, SV_GMAGIC);
3416}
3417
c461cf8f
JH
3418/*
3419=for apidoc sv_utf8_upgrade
3420
cd458e05 3421Convert the PV of an SV to its UTF-8-encoded form.
645c22ef 3422Forces the SV to string form if it is not already.
4411f3b6
NIS
3423Always sets the SvUTF8 flag to avoid future validity checks even
3424if all the bytes have hibit clear.
c461cf8f 3425
13a6c0e0
JH
3426This is not as a general purpose byte encoding to Unicode interface:
3427use the Encode extension for that.
3428
8d6d96c1
HS
3429=for apidoc sv_utf8_upgrade_flags
3430
cd458e05 3431Convert the PV of an SV to its UTF-8-encoded form.
645c22ef 3432Forces the SV to string form if it is not already.
8d6d96c1
HS
3433Always sets the SvUTF8 flag to avoid future validity checks even
3434if all the bytes have hibit clear. If C<flags> has C<SV_GMAGIC> bit set,
3435will C<mg_get> on C<sv> if appropriate, else not. C<sv_utf8_upgrade> and
3436C<sv_utf8_upgrade_nomg> are implemented in terms of this function.
3437
13a6c0e0
JH
3438This is not as a general purpose byte encoding to Unicode interface:
3439use the Encode extension for that.
3440
8d6d96c1
HS
3441=cut
3442*/
3443
3444STRLEN
3445Perl_sv_utf8_upgrade_flags(pTHX_ register SV *sv, I32 flags)
3446{
db42d148 3447 U8 *s, *t, *e;
511c2ff0 3448 int hibit = 0;
560a288e 3449
e0e62c2a
NIS
3450 if (!SvPOK(sv)) {
3451 STRLEN len = 0;
548f1cb8 3452 (void) SvPV_force(sv,len);
e0e62c2a 3453 }
4411f3b6 3454
548f1cb8
TS
3455 if (SvUTF8(sv)) {
3456 SvSETMAGIC(sv);
4411f3b6 3457 return SvCUR(sv);
548f1cb8 3458 }
560a288e 3459
db42d148
NIS
3460 if (SvREADONLY(sv) && SvFAKE(sv)) {
3461 sv_force_normal(sv);
3462 }
3463
6563884d 3464 if (PL_encoding && !(flags & SV_UTF8_NO_ENCODING))
799ef3cb 3465 sv_recode_to_utf8(sv, PL_encoding);
9f4817db 3466 else { /* Assume Latin-1/EBCDIC */
0a378802
JH
3467 /* This function could be much more efficient if we
3468 * had a FLAG in SVs to signal if there are any hibit
3469 * chars in the PV. Given that there isn't such a flag
3470 * make the loop as fast as possible. */
3471 s = (U8 *) SvPVX(sv);
3472 e = (U8 *) SvEND(sv);
3473 t = s;
3474 while (t < e) {
3475 U8 ch = *t++;
3476 if ((hibit = !NATIVE_IS_INVARIANT(ch)))
3477 break;
3478 }
3479 if (hibit) {
3480 STRLEN len;
2c2a9fc1
TS
3481 (void)SvOOK_off(sv);
3482 s = (U8*)SvPVX(sv);
0a378802
JH
3483 len = SvCUR(sv) + 1; /* Plus the \0 */
3484 SvPVX(sv) = (char*)bytes_to_utf8((U8*)s, &len);
3485 SvCUR(sv) = len - 1;
3486 if (SvLEN(sv) != 0)
3487 Safefree(s); /* No longer using what was there before. */
3488 SvLEN(sv) = len; /* No longer know the real size. */
3489 }
9f4817db
JH
3490 /* Mark as UTF-8 even if no hibit - saves scanning loop */
3491 SvUTF8_on(sv);
560a288e 3492 }
548f1cb8 3493 SvSETMAGIC(sv);
4411f3b6 3494 return SvCUR(sv);
560a288e
GS
3495}
3496
c461cf8f
JH
3497/*
3498=for apidoc sv_utf8_downgrade
3499
cd458e05 3500Attempt to convert the PV of an SV from UTF-8-encoded to byte encoding.
c461cf8f
JH
3501This may not be possible if the PV contains non-byte encoding characters;
3502if this is the case, either returns false or, if C<fail_ok> is not
3503true, croaks.
3504
13a6c0e0
JH
3505This is not as a general purpose Unicode to byte encoding interface:
3506use the Encode extension for that.
3507
c461cf8f
JH
3508=cut
3509*/
3510
560a288e
GS
3511bool
3512Perl_sv_utf8_downgrade(pTHX_ register SV* sv, bool fail_ok)
3513{
3514 if (SvPOK(sv) && SvUTF8(sv)) {
fa301091 3515 if (SvCUR(sv)) {
03cfe0ae 3516 U8 *s;
652088fc 3517 STRLEN len;
fa301091 3518
652088fc
JH
3519 if (SvREADONLY(sv) && SvFAKE(sv))
3520 sv_force_normal(sv);
03cfe0ae
NIS
3521 s = (U8 *) SvPV(sv, len);
3522 if (!utf8_to_bytes(s, &len)) {
fa301091
JH
3523 if (fail_ok)
3524 return FALSE;
3525 else {
3526 if (PL_op)
3527 Perl_croak(aTHX_ "Wide character in %s",
53e06cf0 3528 OP_DESC(PL_op));
fa301091
JH
3529 else
3530 Perl_croak(aTHX_ "Wide character");
3531 }
4b3603a4 3532 }
fa301091 3533 SvCUR(sv) = len;
67e989fb 3534 }
560a288e 3535 }
ffebcc3e 3536 SvUTF8_off(sv);
560a288e
GS
3537 return TRUE;
3538}
3539
c461cf8f
JH
3540/*
3541=for apidoc sv_utf8_encode
3542
cd458e05 3543Convert the PV of an SV to UTF-8-encoded, but then turn off the C<SvUTF8>
4411f3b6
NIS
3544flag so that it looks like octets again. Used as a building block
3545for encode_utf8 in Encode.xs
c461cf8f
JH
3546
3547=cut
3548*/
3549
560a288e
GS
3550void
3551Perl_sv_utf8_encode(pTHX_ register SV *sv)
3552{
4411f3b6 3553 (void) sv_utf8_upgrade(sv);
71410450
NC
3554 if (SvIsCOW(sv)) {
3555 sv_force_normal_flags(sv, 0);
3556 }
3557 if (SvREADONLY(sv)) {
3558 Perl_croak(aTHX_ PL_no_modify);
3559 }
560a288e
GS
3560 SvUTF8_off(sv);
3561}
3562
4411f3b6
NIS
3563/*
3564=for apidoc sv_utf8_decode
3565
3566Convert the octets in the PV from UTF-8 to chars. Scan for validity and then
645c22ef 3567turn off SvUTF8 if needed so that we see characters. Used as a building block
4411f3b6
NIS
3568for decode_utf8 in Encode.xs
3569
3570=cut
3571*/
3572
560a288e
GS
3573bool
3574Perl_sv_utf8_decode(pTHX_ register SV *sv)
3575{
3576 if (SvPOK(sv)) {
63cd0674
NIS
3577 U8 *c;
3578 U8 *e;
9cbac4c7 3579
645c22ef
DM
3580 /* The octets may have got themselves encoded - get them back as
3581 * bytes
3582 */
3583 if (!sv_utf8_downgrade(sv, TRUE))
560a288e
GS
3584 return FALSE;
3585
3586 /* it is actually just a matter of turning the utf8 flag on, but
3587 * we want to make sure everything inside is valid utf8 first.
3588 */
63cd0674
NIS
3589 c = (U8 *) SvPVX(sv);
3590 if (!is_utf8_string(c, SvCUR(sv)+1))
67e989fb 3591 return FALSE;
63cd0674 3592 e = (U8 *) SvEND(sv);
511c2ff0 3593 while (c < e) {
c4d5f83a
NIS
3594 U8 ch = *c++;
3595 if (!UTF8_IS_INVARIANT(ch)) {
67e989fb
JH
3596 SvUTF8_on(sv);
3597 break;
3598 }
560a288e 3599 }
560a288e
GS
3600 }
3601 return TRUE;
3602}
3603
d34f9d2e
JH
3604/* sv_setsv() is now a macro using Perl_sv_setsv_flags();
3605 * this function provided for binary compatibility only
3606 */
3607
3608void
3609Perl_sv_setsv(pTHX_ SV *dstr, register SV *sstr)
3610{
3611 sv_setsv_flags(dstr, sstr, SV_GMAGIC);
3612}
3613
954c1994
GS
3614/*
3615=for apidoc sv_setsv
3616
645c22ef
DM
3617Copies the contents of the source SV C<ssv> into the destination SV
3618C<dsv>. The source SV may be destroyed if it is mortal, so don't use this
3619function if the source SV needs to be reused. Does not handle 'set' magic.
3620Loosely speaking, it performs a copy-by-value, obliterating any previous
3621content of the destination.
3622
3623You probably want to use one of the assortment of wrappers, such as
3624C<SvSetSV>, C<SvSetSV_nosteal>, C<SvSetMagicSV> and
3625C<SvSetMagicSV_nosteal>.
3626
8d6d96c1
HS
3627=for apidoc sv_setsv_flags
3628
645c22ef
DM
3629Copies the contents of the source SV C<ssv> into the destination SV
3630C<dsv>. The source SV may be destroyed if it is mortal, so don't use this
3631function if the source SV needs to be reused. Does not handle 'set' magic.
3632Loosely speaking, it performs a copy-by-value, obliterating any previous
3633content of the destination.
3634If the C<flags> parameter has the C<SV_GMAGIC> bit set, will C<mg_get> on
3635C<ssv> if appropriate, else not. C<sv_setsv> and C<sv_setsv_nomg> are
3636implemented in terms of this function.
3637
3638You probably want to use one of the assortment of wrappers, such as
3639C<SvSetSV>, C<SvSetSV_nosteal>, C<SvSetMagicSV> and
3640C<SvSetMagicSV_nosteal>.
3641
3642This is the primary function for copying scalars, and most other
3643copy-ish functions and macros use this underneath.
8d6d96c1
HS
3644
3645=cut
3646*/
3647
3648void
3649Perl_sv_setsv_flags(pTHX_ SV *dstr, register SV *sstr, I32 flags)
3650{
8990e307
LW
3651 register U32 sflags;
3652 register int dtype;
3653 register int stype;
463ee0b2 3654
79072805
LW
3655 if (sstr == dstr)
3656 return;
2213622d 3657 SV_CHECK_THINKFIRST(dstr);
79072805 3658 if (!sstr)
3280af22 3659 sstr = &PL_sv_undef;
8990e307
LW
3660 stype = SvTYPE(sstr);
3661 dtype = SvTYPE(dstr);
79072805 3662
a0d0e21e 3663 SvAMAGIC_off(dstr);
7d7ce6cc
JH
3664 if ( SvVOK(dstr) )
3665 {
3666 /* need to nuke the magic */
3667 mg_free(dstr);
3668 SvRMAGICAL_off(dstr);
3669 }
9e7bc3e8 3670
463ee0b2 3671 /* There's a lot of redundancy below but we're going for speed here */
79072805 3672
8990e307 3673 switch (stype) {
79072805 3674 case SVt_NULL:
aece5585 3675 undef_sstr:
20408e3c
GS
3676 if (dtype != SVt_PVGV) {
3677 (void)SvOK_off(dstr);
3678 return;
3679 }
3680 break;
463ee0b2 3681 case SVt_IV:
aece5585
GA
3682 if (SvIOK(sstr)) {
3683 switch (dtype) {
3684 case SVt_NULL:
8990e307 3685 sv_upgrade(dstr, SVt_IV);
aece5585
GA
3686 break;
3687 case SVt_NV:
8990e307 3688 sv_upgrade(dstr, SVt_PVNV);
aece5585
GA
3689 break;
3690 case SVt_RV:
3691 case SVt_PV:
a0d0e21e 3692 sv_upgrade(dstr, SVt_PVIV);
aece5585
GA
3693 break;
3694 }
3695 (void)SvIOK_only(dstr);
3696 SvIVX(dstr) = SvIVX(sstr);
25da4f38
IZ
3697 if (SvIsUV(sstr))
3698 SvIsUV_on(dstr);
27c9684d
AP
3699 if (SvTAINTED(sstr))
3700 SvTAINT(dstr);
aece5585 3701 return;
8990e307 3702 }
aece5585
GA
3703 goto undef_sstr;
3704
463ee0b2 3705 case SVt_NV:
aece5585
GA
3706 if (SvNOK(sstr)) {
3707 switch (dtype) {
3708 case SVt_NULL:
3709 case SVt_IV:
8990e307 3710 sv_upgrade(dstr, SVt_NV);
aece5585
GA
3711 break;
3712 case SVt_RV:
3713 case SVt_PV:
3714 case SVt_PVIV:
a0d0e21e 3715 sv_upgrade(dstr, SVt_PVNV);
aece5585
GA
3716 break;
3717 }
3718 SvNVX(dstr) = SvNVX(sstr);
3719 (void)SvNOK_only(dstr);
27c9684d
AP
3720 if (SvTAINTED(sstr))
3721 SvTAINT(dstr);
aece5585 3722 return;
8990e307 3723 }
aece5585
GA
3724 goto undef_sstr;
3725
ed6116ce 3726 case SVt_RV:
8990e307 3727 if (dtype < SVt_RV)
ed6116ce 3728 sv_upgrade(dstr, SVt_RV);
c07a80fd 3729 else if (dtype == SVt_PVGV &&
5e8f8cda 3730 SvROK(sstr) && SvTYPE(SvRV(sstr)) == SVt_PVGV) {
c07a80fd 3731 sstr = SvRV(sstr);
a5f75d66 3732 if (sstr == dstr) {
1d7c1841
GS
3733 if (GvIMPORTED(dstr) != GVf_IMPORTED
3734 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3735 {
a5f75d66 3736 GvIMPORTED_on(dstr);
1d7c1841 3737 }
a5f75d66
AD
3738 GvMULTI_on(dstr);
3739 return;
3740 }
c07a80fd 3741 goto glob_assign;
3742 }
ed6116ce 3743 break;
463ee0b2 3744 case SVt_PV:
fc36a67e 3745 case SVt_PVFM:
8990e307 3746 if (dtype < SVt_PV)
463ee0b2 3747 sv_upgrade(dstr, SVt_PV);
463ee0b2
LW
3748 break;
3749 case SVt_PVIV:
8990e307 3750 if (dtype < SVt_PVIV)
463ee0b2 3751 sv_upgrade(dstr, SVt_PVIV);
463ee0b2
LW
3752 break;
3753 case SVt_PVNV:
8990e307 3754 if (dtype < SVt_PVNV)
463ee0b2 3755 sv_upgrade(dstr, SVt_PVNV);
463ee0b2 3756 break;
4633a7c4
LW
3757 case SVt_PVAV:
3758 case SVt_PVHV:
3759 case SVt_PVCV:
4633a7c4 3760 case SVt_PVIO:
533c011a 3761 if (PL_op)
cea2e8a9 3762 Perl_croak(aTHX_ "Bizarre copy of %s in %s", sv_reftype(sstr, 0),
53e06cf0 3763 OP_NAME(PL_op));
4633a7c4 3764 else
cea2e8a9 3765 Perl_croak(aTHX_ "Bizarre copy of %s", sv_reftype(sstr, 0));
4633a7c4
LW
3766 break;
3767
79072805 3768 case SVt_PVGV:
8990e307 3769 if (dtype <= SVt_PVGV) {
c07a80fd 3770 glob_assign:
a5f75d66 3771 if (dtype != SVt_PVGV) {
a0d0e21e
LW
3772 char *name = GvNAME(sstr);
3773 STRLEN len = GvNAMELEN(sstr);
463ee0b2 3774 sv_upgrade(dstr, SVt_PVGV);
14befaf4 3775 sv_magic(dstr, dstr, PERL_MAGIC_glob, Nullch, 0);
85aff577 3776 GvSTASH(dstr) = (HV*)SvREFCNT_inc(GvSTASH(sstr));
a0d0e21e
LW
3777 GvNAME(dstr) = savepvn(name, len);
3778 GvNAMELEN(dstr) = len;
3779 SvFAKE_on(dstr); /* can coerce to non-glob */
3780 }
7bac28a0 3781 /* ahem, death to those who redefine active sort subs */
3280af22
NIS
3782 else if (PL_curstackinfo->si_type == PERLSI_SORT
3783 && GvCV(dstr) && PL_sortcop == CvSTART(GvCV(dstr)))
cea2e8a9 3784 Perl_croak(aTHX_ "Can't redefine active sort subroutine %s",
7bac28a0 3785 GvNAME(dstr));
5bd07a3d 3786
7fb37951
AMS
3787#ifdef GV_UNIQUE_CHECK
3788 if (GvUNIQUE((GV*)dstr)) {
5bd07a3d
DM
3789 Perl_croak(aTHX_ PL_no_modify);
3790 }
3791#endif
3792
a0d0e21e 3793 (void)SvOK_off(dstr);
a5f75d66 3794 GvINTRO_off(dstr); /* one-shot flag */
1edc1566 3795 gp_free((GV*)dstr);
79072805 3796 GvGP(dstr) = gp_ref(GvGP(sstr));
27c9684d
AP
3797 if (SvTAINTED(sstr))
3798 SvTAINT(dstr);
1d7c1841
GS
3799 if (GvIMPORTED(dstr) != GVf_IMPORTED
3800 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3801 {
a5f75d66 3802 GvIMPORTED_on(dstr);
1d7c1841 3803 }
a5f75d66 3804 GvMULTI_on(dstr);
79072805
LW
3805 return;
3806 }
3807 /* FALL THROUGH */
3808
3809 default:
8d6d96c1 3810 if (SvGMAGICAL(sstr) && (flags & SV_GMAGIC)) {
973f89ab 3811 mg_get(sstr);
eb160463 3812 if ((int)SvTYPE(sstr) != stype) {
973f89ab
CS
3813 stype = SvTYPE(sstr);
3814 if (stype == SVt_PVGV && dtype <= SVt_PVGV)
3815 goto glob_assign;
3816 }
3817 }
ded42b9f 3818 if (stype == SVt_PVLV)
6fc92669 3819 (void)SvUPGRADE(dstr, SVt_PVNV);
ded42b9f 3820 else
eb160463 3821 (void)SvUPGRADE(dstr, (U32)stype);
79072805
LW
3822 }
3823
8990e307
LW
3824 sflags = SvFLAGS(sstr);
3825
3826 if (sflags & SVf_ROK) {
3827 if (dtype >= SVt_PV) {
3828 if (dtype == SVt_PVGV) {
3829 SV *sref = SvREFCNT_inc(SvRV(sstr));
3830 SV *dref = 0;
a5f75d66 3831 int intro = GvINTRO(dstr);
a0d0e21e 3832
7fb37951
AMS
3833#ifdef GV_UNIQUE_CHECK
3834 if (GvUNIQUE((GV*)dstr)) {
5bd07a3d
DM
3835 Perl_croak(aTHX_ PL_no_modify);
3836 }
3837#endif
3838
a0d0e21e 3839 if (intro) {
a5f75d66 3840 GvINTRO_off(dstr); /* one-shot flag */
1d7c1841 3841 GvLINE(dstr) = CopLINE(PL_curcop);
1edc1566 3842 GvEGV(dstr) = (GV*)dstr;
a0d0e21e 3843 }
a5f75d66 3844 GvMULTI_on(dstr);
8990e307
LW
3845 switch (SvTYPE(sref)) {
3846 case SVt_PVAV:
a0d0e21e 3847 if (intro)
9b1c5f7e 3848 SAVEGENERICSV(GvAV(dstr));
a0d0e21e
LW
3849 else
3850 dref = (SV*)GvAV(dstr);
8990e307 3851 GvAV(dstr) = (AV*)sref;
39bac7f7 3852 if (!GvIMPORTED_AV(dstr)
1d7c1841
GS
3853 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3854 {
a5f75d66 3855 GvIMPORTED_AV_on(dstr);
1d7c1841 3856 }
8990e307
LW
3857 break;
3858 case SVt_PVHV:
a0d0e21e 3859 if (intro)
9b1c5f7e 3860 SAVEGENERICSV(GvHV(dstr));
a0d0e21e
LW
3861 else
3862 dref = (SV*)GvHV(dstr);
8990e307 3863 GvHV(dstr) = (HV*)sref;
39bac7f7 3864 if (!GvIMPORTED_HV(dstr)
1d7c1841
GS
3865 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3866 {
a5f75d66 3867 GvIMPORTED_HV_on(dstr);
1d7c1841 3868 }
8990e307
LW
3869 break;
3870 case SVt_PVCV:
8ebc5c01 3871 if (intro) {
3872 if (GvCVGEN(dstr) && GvCV(dstr) != (CV*)sref) {
3873 SvREFCNT_dec(GvCV(dstr));
3874 GvCV(dstr) = Nullcv;
68dc0745 3875 GvCVGEN(dstr) = 0; /* Switch off cacheness. */
3280af22 3876 PL_sub_generation++;
8ebc5c01 3877 }
9b1c5f7e 3878 SAVEGENERICSV(GvCV(dstr));
8ebc5c01 3879 }
68dc0745 3880 else
3881 dref = (SV*)GvCV(dstr);
3882 if (GvCV(dstr) != (CV*)sref) {
748a9306 3883 CV* cv = GvCV(dstr);
4633a7c4 3884 if (cv) {
68dc0745 3885 if (!GvCVGEN((GV*)dstr) &&
3886 (CvROOT(cv) || CvXSUB(cv)))
3887 {
7bac28a0 3888 /* ahem, death to those who redefine
3889 * active sort subs */
3280af22
NIS
3890 if (PL_curstackinfo->si_type == PERLSI_SORT &&
3891 PL_sortcop == CvSTART(cv))
1c846c1f 3892 Perl_croak(aTHX_
7bac28a0 3893 "Can't redefine active sort subroutine %s",
3894 GvENAME((GV*)dstr));
beab0874
JT
3895 /* Redefining a sub - warning is mandatory if
3896 it was a const and its value changed. */
3897 if (ckWARN(WARN_REDEFINE)
3898 || (CvCONST(cv)
3899 && (!CvCONST((CV*)sref)
3900 || sv_cmp(cv_const_sv(cv),
3901 cv_const_sv((CV*)sref)))))
3902 {
9014280d 3903 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
beab0874 3904 CvCONST(cv)
910764e6
RGS
3905 ? "Constant subroutine %s::%s redefined"
3906 : "Subroutine %s::%s redefined",
3907 HvNAME(GvSTASH((GV*)dstr)),
beab0874
JT
3908 GvENAME((GV*)dstr));
3909 }
9607fc9c 3910 }
fb24441d
RGS
3911 if (!intro)
3912 cv_ckproto(cv, (GV*)dstr,
3913 SvPOK(sref) ? SvPVX(sref) : Nullch);
4633a7c4 3914 }
a5f75d66 3915 GvCV(dstr) = (CV*)sref;
7a4c00b4 3916 GvCVGEN(dstr) = 0; /* Switch off cacheness. */
a5f75d66 3917 GvASSUMECV_on(dstr);
3280af22 3918 PL_sub_generation++;
a5f75d66 3919 }
39bac7f7 3920 if (!GvIMPORTED_CV(dstr)
1d7c1841
GS
3921 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3922 {
a5f75d66 3923 GvIMPORTED_CV_on(dstr);
1d7c1841 3924 }
8990e307 3925 break;
91bba347
LW
3926 case SVt_PVIO:
3927 if (intro)
9b1c5f7e 3928 SAVEGENERICSV(GvIOp(dstr));
91bba347
LW
3929 else
3930 dref = (SV*)GvIOp(dstr);
3931 GvIOp(dstr) = (IO*)sref;
3932 break;
f4d13ee9
JH
3933 case SVt_PVFM:
3934 if (intro)
9b1c5f7e 3935 SAVEGENERICSV(GvFORM(dstr));
f4d13ee9
JH
3936 else
3937 dref = (SV*)GvFORM(dstr);
3938 GvFORM(dstr) = (CV*)sref;
3939 break;
8990e307 3940 default:
a0d0e21e 3941 if (intro)
9b1c5f7e 3942 SAVEGENERICSV(GvSV(dstr));
a0d0e21e
LW
3943 else
3944 dref = (SV*)GvSV(dstr);
8990e307 3945 GvSV(dstr) = sref;
39bac7f7 3946 if (!GvIMPORTED_SV(dstr)
1d7c1841
GS
3947 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3948 {
a5f75d66 3949 GvIMPORTED_SV_on(dstr);
1d7c1841 3950 }
8990e307
LW
3951 break;
3952 }
3953 if (dref)
3954 SvREFCNT_dec(dref);
27c9684d
AP
3955 if (SvTAINTED(sstr))
3956 SvTAINT(dstr);
8990e307
LW
3957 return;
3958 }
a0d0e21e 3959 if (SvPVX(dstr)) {
760ac839 3960 (void)SvOOK_off(dstr); /* backoff */
50483b2c
JD
3961 if (SvLEN(dstr))
3962 Safefree(SvPVX(dstr));
a0d0e21e
LW
3963 SvLEN(dstr)=SvCUR(dstr)=0;
3964 }
8990e307 3965 }
a0d0e21e 3966 (void)SvOK_off(dstr);
8990e307 3967 SvRV(dstr) = SvREFCNT_inc(SvRV(sstr));
ed6116ce 3968 SvROK_on(dstr);
8990e307 3969 if (sflags & SVp_NOK) {
3332b3c1
JH
3970 SvNOKp_on(dstr);
3971 /* Only set the public OK flag if the source has public OK. */
3972 if (sflags & SVf_NOK)
3973 SvFLAGS(dstr) |= SVf_NOK;
ed6116ce
LW
3974 SvNVX(dstr) = SvNVX(sstr);
3975 }
8990e307 3976 if (sflags & SVp_IOK) {
3332b3c1
JH
3977 (void)SvIOKp_on(dstr);
3978 if (sflags & SVf_IOK)
3979 SvFLAGS(dstr) |= SVf_IOK;
2b1c7e3e 3980 if (sflags & SVf_IVisUV)
25da4f38 3981 SvIsUV_on(dstr);
3332b3c1 3982 SvIVX(dstr) = SvIVX(sstr);
ed6116ce 3983 }
a0d0e21e
LW
3984 if (SvAMAGIC(sstr)) {
3985 SvAMAGIC_on(dstr);
3986 }
ed6116ce 3987 }
8990e307 3988 else if (sflags & SVp_POK) {
79072805
LW
3989
3990 /*
3991 * Check to see if we can just swipe the string. If so, it's a
3992 * possible small lose on short strings, but a big win on long ones.
463ee0b2
LW
3993 * It might even be a win on short strings if SvPVX(dstr)
3994 * has to be allocated and SvPVX(sstr) has to be freed.
79072805
LW
3995 */
3996
ff68c719 3997 if (SvTEMP(sstr) && /* slated for free anyway? */
01b73108 3998 SvREFCNT(sstr) == 1 && /* and no other references to it? */
1c846c1f 3999 !(sflags & SVf_OOK) && /* and not involved in OOK hack? */
4c8f17b9 4000 SvLEN(sstr) && /* and really is a string */
645c22ef
DM
4001 /* and won't be needed again, potentially */
4002 !(PL_op && PL_op->op_type == OP_AASSIGN))
a5f75d66 4003 {
adbc6bb1 4004 if (SvPVX(dstr)) { /* we know that dtype >= SVt_PV */
a5f75d66
AD
4005 if (SvOOK(dstr)) {
4006 SvFLAGS(dstr) &= ~SVf_OOK;
4007 Safefree(SvPVX(dstr) - SvIVX(dstr));
4008 }
50483b2c 4009 else if (SvLEN(dstr))
a5f75d66 4010 Safefree(SvPVX(dstr));
79072805 4011 }
a5f75d66 4012 (void)SvPOK_only(dstr);
463ee0b2 4013 SvPV_set(dstr, SvPVX(sstr));
79072805
LW
4014 SvLEN_set(dstr, SvLEN(sstr));
4015 SvCUR_set(dstr, SvCUR(sstr));
f4e86e0f 4016
79072805 4017 SvTEMP_off(dstr);
645c22ef 4018 (void)SvOK_off(sstr); /* NOTE: nukes most SvFLAGS on sstr */
79072805
LW
4019 SvPV_set(sstr, Nullch);
4020 SvLEN_set(sstr, 0);
a5f75d66
AD
4021 SvCUR_set(sstr, 0);
4022 SvTEMP_off(sstr);
79072805 4023 }
645c22ef 4024 else { /* have to copy actual string */
8990e307 4025 STRLEN len = SvCUR(sstr);
645c22ef 4026 SvGROW(dstr, len + 1); /* inlined from sv_setpvn */
8990e307
LW
4027 Move(SvPVX(sstr),SvPVX(dstr),len,char);
4028 SvCUR_set(dstr, len);
4029 *SvEND(dstr) = '\0';
a0d0e21e 4030 (void)SvPOK_only(dstr);
79072805 4031 }
9aa983d2 4032 if (sflags & SVf_UTF8)
a7cb1f99 4033 SvUTF8_on(dstr);
79072805 4034 /*SUPPRESS 560*/
8990e307 4035 if (sflags & SVp_NOK) {
3332b3c1
JH
4036 SvNOKp_on(dstr);
4037 if (sflags & SVf_NOK)
4038 SvFLAGS(dstr) |= SVf_NOK;
463ee0b2 4039 SvNVX(dstr) = SvNVX(sstr);
79072805 4040 }
8990e307 4041 if (sflags & SVp_IOK) {
3332b3c1
JH
4042 (void)SvIOKp_on(dstr);
4043 if (sflags & SVf_IOK)
4044 SvFLAGS(dstr) |= SVf_IOK;
2b1c7e3e 4045 if (sflags & SVf_IVisUV)
25da4f38 4046 SvIsUV_on(dstr);
463ee0b2 4047 SvIVX(dstr) = SvIVX(sstr);
79072805 4048 }
7d7ce6cc
JH
4049 if ( SvVOK(sstr) ) {
4050 MAGIC *smg = mg_find(sstr,PERL_MAGIC_vstring);
4051 sv_magic(dstr, NULL, PERL_MAGIC_vstring,
4052 smg->mg_ptr, smg->mg_len);
4053 SvRMAGICAL_on(dstr);
4054 }
79072805 4055 }
8990e307 4056 else if (sflags & SVp_IOK) {
3332b3c1
JH
4057 if (sflags & SVf_IOK)
4058 (void)SvIOK_only(dstr);
4059 else {
9cbac4c7
DM
4060 (void)SvOK_off(dstr);
4061 (void)SvIOKp_on(dstr);
3332b3c1
JH
4062 }
4063 /* XXXX Do we want to set IsUV for IV(ROK)? Be extra safe... */
2b1c7e3e 4064 if (sflags & SVf_IVisUV)
25da4f38 4065 SvIsUV_on(dstr);
3332b3c1
JH
4066 SvIVX(dstr) = SvIVX(sstr);
4067 if (sflags & SVp_NOK) {
4068 if (sflags & SVf_NOK)
4069 (void)SvNOK_on(dstr);
4070 else
4071 (void)SvNOKp_on(dstr);
4072 SvNVX(dstr) = SvNVX(sstr);
4073 }
4074 }
4075 else if (sflags & SVp_NOK) {
4076 if (sflags & SVf_NOK)
4077 (void)SvNOK_only(dstr);
4078 else {
9cbac4c7 4079 (void)SvOK_off(dstr);
3332b3c1
JH
4080 SvNOKp_on(dstr);
4081 }
4082 SvNVX(dstr) = SvNVX(sstr);
79072805
LW
4083 }
4084 else {
20408e3c 4085 if (dtype == SVt_PVGV) {
e476b1b5 4086 if (ckWARN(WARN_MISC))
9014280d 4087 Perl_warner(aTHX_ packWARN(WARN_MISC), "Undefined value assigned to typeglob");
20408e3c
GS
4088 }
4089 else
4090 (void)SvOK_off(dstr);
a0d0e21e 4091 }
27c9684d
AP
4092 if (SvTAINTED(sstr))
4093 SvTAINT(dstr);
79072805
LW
4094}
4095
954c1994
GS
4096/*
4097=for apidoc sv_setsv_mg
4098
4099Like C<sv_setsv>, but also handles 'set' magic.
4100
4101=cut
4102*/
4103
79072805 4104void
864dbfa3 4105Perl_sv_setsv_mg(pTHX_ SV *dstr, register SV *sstr)
ef50df4b
GS
4106{
4107 sv_setsv(dstr,sstr);
4108 SvSETMAGIC(dstr);
4109}
4110
954c1994
GS
4111/*
4112=for apidoc sv_setpvn
4113
4114Copies a string into an SV. The C<len> parameter indicates the number of
611e9550
NC
4115bytes to be copied. If the C<ptr> argument is NULL the SV will become
4116undefined. Does not handle 'set' magic. See C<sv_setpvn_mg>.
954c1994
GS
4117
4118=cut
4119*/
4120
ef50df4b 4121void
864dbfa3 4122Perl_sv_setpvn(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
79072805 4123{
c6f8c383 4124 register char *dptr;
22c522df 4125
2213622d 4126 SV_CHECK_THINKFIRST(sv);
463ee0b2 4127 if (!ptr) {
a0d0e21e 4128 (void)SvOK_off(sv);
463ee0b2
LW
4129 return;
4130 }
22c522df
JH
4131 else {
4132 /* len is STRLEN which is unsigned, need to copy to signed */
4133 IV iv = len;
9c5ffd7c
JH
4134 if (iv < 0)
4135 Perl_croak(aTHX_ "panic: sv_setpvn called with negative strlen");
22c522df 4136 }
6fc92669 4137 (void)SvUPGRADE(sv, SVt_PV);
c6f8c383 4138
79072805 4139 SvGROW(sv, len + 1);
c6f8c383
GA
4140 dptr = SvPVX(sv);
4141 Move(ptr,dptr,len,char);
4142 dptr[len] = '\0';
79072805 4143 SvCUR_set(sv, len);
1aa99e6b 4144 (void)SvPOK_only_UTF8(sv); /* validate pointer */
463ee0b2 4145 SvTAINT(sv);
79072805
LW
4146}
4147
954c1994
GS
4148/*
4149=for apidoc sv_setpvn_mg
4150
4151Like C<sv_setpvn>, but also handles 'set' magic.
4152
4153=cut
4154*/
4155
79072805 4156void
864dbfa3 4157Perl_sv_setpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
ef50df4b
GS
4158{
4159 sv_setpvn(sv,ptr,len);
4160 SvSETMAGIC(sv);
4161}
4162
954c1994
GS
4163/*
4164=for apidoc sv_setpv
4165
4166Copies a string into an SV. The string must be null-terminated. Does not
4167handle 'set' magic. See C<sv_setpv_mg>.
4168
4169=cut
4170*/
4171
ef50df4b 4172void
864dbfa3 4173Perl_sv_setpv(pTHX_ register SV *sv, register const char *ptr)
79072805
LW
4174{
4175 register STRLEN len;
4176
2213622d 4177 SV_CHECK_THINKFIRST(sv);
463ee0b2 4178 if (!ptr) {
a0d0e21e 4179 (void)SvOK_off(sv);
463ee0b2
LW
4180 return;
4181 }
79072805 4182 len = strlen(ptr);
6fc92669 4183 (void)SvUPGRADE(sv, SVt_PV);
c6f8c383 4184
79072805 4185 SvGROW(sv, len + 1);
463ee0b2 4186 Move(ptr,SvPVX(sv),len+1,char);
79072805 4187 SvCUR_set(sv, len);
1aa99e6b 4188 (void)SvPOK_only_UTF8(sv); /* validate pointer */
463ee0b2
LW
4189 SvTAINT(sv);
4190}
4191
954c1994
GS
4192/*
4193=for apidoc sv_setpv_mg
4194
4195Like C<sv_setpv>, but also handles 'set' magic.
4196
4197=cut
4198*/
4199
463ee0b2 4200void
864dbfa3 4201Perl_sv_setpv_mg(pTHX_ register SV *sv, register const char *ptr)
ef50df4b
GS
4202{
4203 sv_setpv(sv,ptr);
4204 SvSETMAGIC(sv);
4205}
4206
954c1994
GS
4207/*
4208=for apidoc sv_usepvn
4209
4210Tells an SV to use C<ptr> to find its string value. Normally the string is
1c846c1f 4211stored inside the SV but sv_usepvn allows the SV to use an outside string.
954c1994
GS
4212The C<ptr> should point to memory that was allocated by C<malloc>. The
4213string length, C<len>, must be supplied. This function will realloc the
4214memory pointed to by C<ptr>, so that pointer should not be freed or used by
4215the programmer after giving it to sv_usepvn. Does not handle 'set' magic.
4216See C<sv_usepvn_mg>.
4217
4218=cut
4219*/
4220
ef50df4b 4221void
864dbfa3 4222Perl_sv_usepvn(pTHX_ register SV *sv, register char *ptr, register STRLEN len)
463ee0b2 4223{
2213622d 4224 SV_CHECK_THINKFIRST(sv);
c6f8c383 4225 (void)SvUPGRADE(sv, SVt_PV);
463ee0b2 4226 if (!ptr) {
a0d0e21e 4227 (void)SvOK_off(sv);
463ee0b2
LW
4228 return;
4229 }
a0ed51b3 4230 (void)SvOOK_off(sv);
50483b2c 4231 if (SvPVX(sv) && SvLEN(sv))
463ee0b2
LW
4232 Safefree(SvPVX(sv));
4233 Renew(ptr, len+1, char);
4234 SvPVX(sv) = ptr;
4235 SvCUR_set(sv, len);
4236 SvLEN_set(sv, len+1);
4237 *SvEND(sv) = '\0';
1aa99e6b 4238 (void)SvPOK_only_UTF8(sv); /* validate pointer */
463ee0b2 4239 SvTAINT(sv);
79072805
LW
4240}
4241
954c1994
GS
4242/*
4243=for apidoc sv_usepvn_mg
4244
4245Like C<sv_usepvn>, but also handles 'set' magic.
4246
4247=cut
4248*/
4249
ef50df4b 4250void
864dbfa3 4251Perl_sv_usepvn_mg(pTHX_ register SV *sv, register char *ptr, register STRLEN len)
ef50df4b 4252{
51c1089b 4253 sv_usepvn(sv,ptr,len);
ef50df4b
GS
4254 SvSETMAGIC(sv);
4255}
4256
645c22ef
DM
4257/*
4258=for apidoc sv_force_normal_flags
4259
4260Undo various types of fakery on an SV: if the PV is a shared string, make
4261a private copy; if we're a ref, stop refing; if we're a glob, downgrade to
4262an xpvmg. The C<flags> parameter gets passed to C<sv_unref_flags()>
4263when unrefing. C<sv_force_normal> calls this function with flags set to 0.
4264
4265=cut
4266*/
4267
6fc92669 4268void
840a7b70 4269Perl_sv_force_normal_flags(pTHX_ register SV *sv, U32 flags)
0f15f207 4270{
2213622d 4271 if (SvREADONLY(sv)) {
1c846c1f
NIS
4272 if (SvFAKE(sv)) {
4273 char *pvx = SvPVX(sv);
4274 STRLEN len = SvCUR(sv);
4275 U32 hash = SvUVX(sv);
1624793d
NC
4276 SvFAKE_off(sv);
4277 SvREADONLY_off(sv);
1c846c1f
NIS
4278 SvGROW(sv, len + 1);
4279 Move(pvx,SvPVX(sv),len,char);
4280 *SvEND(sv) = '\0';
25716404 4281 unsharepvn(pvx, SvUTF8(sv) ? -(I32)len : len, hash);
1c846c1f 4282 }
ef7b71f0 4283 else if (IN_PERL_RUNTIME)
cea2e8a9 4284 Perl_croak(aTHX_ PL_no_modify);
0f15f207 4285 }
2213622d 4286 if (SvROK(sv))
840a7b70 4287 sv_unref_flags(sv, flags);
6fc92669
GS
4288 else if (SvFAKE(sv) && SvTYPE(sv) == SVt_PVGV)
4289 sv_unglob(sv);
0f15f207 4290}
1c846c1f 4291
645c22ef
DM
4292/*
4293=for apidoc sv_force_normal
4294
4295Undo various types of fakery on an SV: if the PV is a shared string, make
4296a private copy; if we're a ref, stop refing; if we're a glob, downgrade to
4297an xpvmg. See also C<sv_force_normal_flags>.
4298
4299=cut
4300*/
4301
840a7b70
IZ
4302void
4303Perl_sv_force_normal(pTHX_ register SV *sv)
4304{
4305 sv_force_normal_flags(sv, 0);
4306}
4307
954c1994
GS
4308/*
4309=for apidoc sv_chop
4310
1c846c1f 4311Efficient removal of characters from the beginning of the string buffer.
954c1994
GS
4312SvPOK(sv) must be true and the C<ptr> must be a pointer to somewhere inside
4313the string buffer. The C<ptr> becomes the first character of the adjusted
645c22ef 4314string. Uses the "OOK hack".
b9219079
JH
4315Beware: after this function returns, C<ptr> and SvPVX(sv) may no longer
4316refer to the same chunk of data.
954c1994
GS
4317
4318=cut
4319*/
4320
79072805 4321void
645c22ef 4322Perl_sv_chop(pTHX_ register SV *sv, register char *ptr)
79072805
LW
4323{
4324 register STRLEN delta;
a0d0e21e 4325 if (!ptr || !SvPOKp(sv))
79072805 4326 return;
b9219079 4327 delta = ptr - SvPVX(sv);
2213622d 4328 SV_CHECK_THINKFIRST(sv);
79072805
LW
4329 if (SvTYPE(sv) < SVt_PVIV)
4330 sv_upgrade(sv,SVt_PVIV);
4331
4332 if (!SvOOK(sv)) {
50483b2c
JD
4333 if (!SvLEN(sv)) { /* make copy of shared string */
4334 char *pvx = SvPVX(sv);
4335 STRLEN len = SvCUR(sv);
4336 SvGROW(sv, len + 1);
4337 Move(pvx,SvPVX(sv),len,char);
4338 *SvEND(sv) = '\0';
4339 }
463ee0b2 4340 SvIVX(sv) = 0;
e33c26d0
JH
4341 /* Same SvOOK_on but SvOOK_on does a SvIOK_off
4342 and we do that anyway inside the SvNIOK_off
4343 */
4344 SvFLAGS(sv) |= SVf_OOK;
79072805 4345 }
e33c26d0 4346 SvNIOK_off(sv);
79072805
LW
4347 SvLEN(sv) -= delta;
4348 SvCUR(sv) -= delta;
463ee0b2
LW
4349 SvPVX(sv) += delta;
4350 SvIVX(sv) += delta;
79072805
LW
4351}
4352
d34f9d2e
JH
4353/* sv_catpvn() is now a macro using Perl_sv_catpvn_flags();
4354 * this function provided for binary compatibility only
4355 */
4356
4357void
4358Perl_sv_catpvn(pTHX_ SV *dsv, const char* sstr, STRLEN slen)
4359{
4360 sv_catpvn_flags(dsv, sstr, slen, SV_GMAGIC);
4361}
4362
954c1994
GS
4363/*
4364=for apidoc sv_catpvn
4365
4366Concatenates the string onto the end of the string which is in the SV. The
cd458e05
JH
4367C<len> indicates number of bytes to copy. If the SV has the UTF-8
4368status set, then the bytes appended should be valid UTF-8.
d5ce4a7c 4369Handles 'get' magic, but not 'set' magic. See C<sv_catpvn_mg>.
954c1994 4370
8d6d96c1
HS
4371=for apidoc sv_catpvn_flags
4372
4373Concatenates the string onto the end of the string which is in the SV. The
cd458e05
JH
4374C<len> indicates number of bytes to copy. If the SV has the UTF-8
4375status set, then the bytes appended should be valid UTF-8.
8d6d96c1
HS
4376If C<flags> has C<SV_GMAGIC> bit set, will C<mg_get> on C<dsv> if
4377appropriate, else not. C<sv_catpvn> and C<sv_catpvn_nomg> are implemented
4378in terms of this function.
4379
4380=cut
4381*/
4382
4383void
4384Perl_sv_catpvn_flags(pTHX_ register SV *dsv, register const char *sstr, register STRLEN slen, I32 flags)
4385{
4386 STRLEN dlen;
4387 char *dstr;
4388
4389 dstr = SvPV_force_flags(dsv, dlen, flags);
4390 SvGROW(dsv, dlen + slen + 1);
4391 if (sstr == dstr)
4392 sstr = SvPVX(dsv);
4393 Move(sstr, SvPVX(dsv) + dlen, slen, char);
4394 SvCUR(dsv) += slen;
4395 *SvEND(dsv) = '\0';
4396 (void)SvPOK_only_UTF8(dsv); /* validate pointer */
4397 SvTAINT(dsv);
79072805
LW
4398}
4399
954c1994
GS
4400/*
4401=for apidoc sv_catpvn_mg
4402
4403Like C<sv_catpvn>, but also handles 'set' magic.
4404
4405=cut
4406*/
4407
79072805 4408void
864dbfa3 4409Perl_sv_catpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
ef50df4b
GS
4410{
4411 sv_catpvn(sv,ptr,len);
4412 SvSETMAGIC(sv);
4413}
4414
d34f9d2e
JH
4415/* sv_catsv() is now a macro using Perl_sv_catsv_flags();
4416 * this function provided for binary compatibility only
4417 */
4418
4419void
4420Perl_sv_catsv(pTHX_ SV *dstr, register SV *sstr)
4421{
4422 sv_catsv_flags(dstr, sstr, SV_GMAGIC);
4423}
4424
954c1994
GS
4425/*
4426=for apidoc sv_catsv
4427
13e8c8e3
JH
4428Concatenates the string from SV C<ssv> onto the end of the string in
4429SV C<dsv>. Modifies C<dsv> but not C<ssv>. Handles 'get' magic, but
4430not 'set' magic. See C<sv_catsv_mg>.
954c1994 4431
8d6d96c1
HS
4432=for apidoc sv_catsv_flags
4433
4434Concatenates the string from SV C<ssv> onto the end of the string in
4435SV C<dsv>. Modifies C<dsv> but not C<ssv>. If C<flags> has C<SV_GMAGIC>
4436bit set, will C<mg_get> on the SVs if appropriate, else not. C<sv_catsv>
4437and C<sv_catsv_nomg> are implemented in terms of this function.
4438
4439=cut */
4440
ef50df4b 4441void
8d6d96c1 4442Perl_sv_catsv_flags(pTHX_ SV *dsv, register SV *ssv, I32 flags)
79072805 4443{
13e8c8e3
JH
4444 char *spv;
4445 STRLEN slen;
46199a12 4446 if (!ssv)
79072805 4447 return;
46199a12 4448 if ((spv = SvPV(ssv, slen))) {
4fd84b44
AD
4449 /* sutf8 and dutf8 were type bool, but under USE_ITHREADS,
4450 gcc version 2.95.2 20000220 (Debian GNU/Linux) for
8cf8f3d1
NIS
4451 Linux xxx 2.2.17 on sparc64 with gcc -O2, we erroneously
4452 get dutf8 = 0x20000000, (i.e. SVf_UTF8) even though
4fd84b44
AD
4453 dsv->sv_flags doesn't have that bit set.
4454 Andy Dougherty 12 Oct 2001
4455 */
4456 I32 sutf8 = DO_UTF8(ssv);
4457 I32 dutf8;
13e8c8e3 4458
8d6d96c1
HS
4459 if (SvGMAGICAL(dsv) && (flags & SV_GMAGIC))
4460 mg_get(dsv);
4461 dutf8 = DO_UTF8(dsv);
4462
4463 if (dutf8 != sutf8) {
13e8c8e3 4464 if (dutf8) {
46199a12 4465 /* Not modifying source SV, so taking a temporary copy. */
8d6d96c1 4466 SV* csv = sv_2mortal(newSVpvn(spv, slen));
13e8c8e3 4467
46199a12 4468 sv_utf8_upgrade(csv);
8d6d96c1 4469 spv = SvPV(csv, slen);
13e8c8e3 4470 }
8d6d96c1
HS
4471 else
4472 sv_utf8_upgrade_nomg(dsv);
e84ff256 4473 }
8d6d96c1 4474 sv_catpvn_nomg(dsv, spv, slen);
560a288e 4475 }
79072805
LW
4476}
4477
954c1994
GS
4478/*
4479=for apidoc sv_catsv_mg
4480
4481Like C<sv_catsv>, but also handles 'set' magic.
4482
4483=cut
4484*/
4485
79072805 4486void
46199a12 4487Perl_sv_catsv_mg(pTHX_ SV *dsv, register SV *ssv)
ef50df4b 4488{
46199a12
JH
4489 sv_catsv(dsv,ssv);
4490 SvSETMAGIC(dsv);
ef50df4b
GS
4491}
4492
954c1994
GS
4493/*
4494=for apidoc sv_catpv
4495
4496Concatenates the string onto the end of the string which is in the SV.
cd458e05
JH
4497If the SV has the UTF-8 status set, then the bytes appended should be
4498valid UTF-8. Handles 'get' magic, but not 'set' magic. See C<sv_catpv_mg>.
954c1994 4499
d5ce4a7c 4500=cut */
954c1994 4501
ef50df4b 4502void
0c981600 4503Perl_sv_catpv(pTHX_ register SV *sv, register const char *ptr)
79072805
LW
4504{
4505 register STRLEN len;
463ee0b2 4506 STRLEN tlen;
748a9306 4507 char *junk;
79072805 4508
0c981600 4509 if (!ptr)
79072805 4510 return;
748a9306 4511 junk = SvPV_force(sv, tlen);
0c981600 4512 len = strlen(ptr);
463ee0b2 4513 SvGROW(sv, tlen + len + 1);
0c981600
JH
4514 if (ptr == junk)
4515 ptr = SvPVX(sv);
4516 Move(ptr,SvPVX(sv)+tlen,len+1,char);
79072805 4517 SvCUR(sv) += len;
d41ff1b8 4518 (void)SvPOK_only_UTF8(sv); /* validate pointer */
463ee0b2 4519 SvTAINT(sv);
79072805
LW
4520}
4521
954c1994
GS
4522/*
4523=for apidoc sv_catpv_mg
4524
4525Like C<sv_catpv>, but also handles 'set' magic.
4526
4527=cut
4528*/
4529
ef50df4b 4530void
0c981600 4531Perl_sv_catpv_mg(pTHX_ register SV *sv, register const char *ptr)
ef50df4b 4532{
0c981600 4533 sv_catpv(sv,ptr);
ef50df4b
GS
4534 SvSETMAGIC(sv);
4535}
4536
645c22ef
DM
4537/*
4538=for apidoc newSV
4539
4540Create a new null SV, or if len > 0, create a new empty SVt_PV type SV
4541with an initial PV allocation of len+1. Normally accessed via the C<NEWSV>
4542macro.
4543
4544=cut
4545*/
4546
79072805 4547SV *
864dbfa3 4548Perl_newSV(pTHX_ STRLEN len)
79072805
LW
4549{
4550 register SV *sv;
1c846c1f 4551
4561caa4 4552 new_SV(sv);
79072805
LW
4553 if (len) {
4554 sv_upgrade(sv, SVt_PV);
4555 SvGROW(sv, len + 1);
4556 }
4557 return sv;
4558}
954c1994 4559/*
92110913 4560=for apidoc sv_magicext
954c1994 4561
68795e93 4562Adds magic to an SV, upgrading it if necessary. Applies the
92110913
NIS
4563supplied vtable and returns pointer to the magic added.
4564
4565Note that sv_magicext will allow things that sv_magic will not.
68795e93 4566In particular you can add magic to SvREADONLY SVs and and more than
92110913 4567one instance of the same 'how'
645c22ef 4568
92110913 4569I C<namelen> is greater then zero then a savepvn() I<copy> of C<name> is stored,
68795e93
NIS
4570if C<namelen> is zero then C<name> is stored as-is and - as another special
4571case - if C<(name && namelen == HEf_SVKEY)> then C<name> is assumed to contain
92110913
NIS
4572an C<SV*> and has its REFCNT incremented
4573
4574(This is now used as a subroutine by sv_magic.)
954c1994
GS
4575
4576=cut
4577*/
92110913
NIS
4578MAGIC *
4579Perl_sv_magicext(pTHX_ SV* sv, SV* obj, int how, MGVTBL *vtable,
4580 const char* name, I32 namlen)
79072805
LW
4581{
4582 MAGIC* mg;
68795e93 4583
92110913
NIS
4584 if (SvTYPE(sv) < SVt_PVMG) {
4585 (void)SvUPGRADE(sv, SVt_PVMG);
463ee0b2 4586 }
79072805
LW
4587 Newz(702,mg, 1, MAGIC);
4588 mg->mg_moremagic = SvMAGIC(sv);
79072805 4589 SvMAGIC(sv) = mg;
75f9d97a 4590
18808301 4591 /* Some magic sontains a reference loop, where the sv and object refer to
bb03859b
RS
4592 each other. To prevent a reference loop that would prevent such
4593 objects being freed, we look for such loops and if we find one we
87f0b213
JH
4594 avoid incrementing the object refcount.
4595
4596 Note we cannot do this to avoid self-tie loops as intervening RV must
8ccffa6a 4597 have its REFCNT incremented to keep it in existence.
87f0b213
JH
4598
4599 */
14befaf4
DM
4600 if (!obj || obj == sv ||
4601 how == PERL_MAGIC_arylen ||
4602 how == PERL_MAGIC_qr ||
75f9d97a
JH
4603 (SvTYPE(obj) == SVt_PVGV &&
4604 (GvSV(obj) == sv || GvHV(obj) == (HV*)sv || GvAV(obj) == (AV*)sv ||
4605 GvCV(obj) == (CV*)sv || GvIOp(obj) == (IO*)sv ||
2628be26 4606 GvFORM(obj) == (CV*)sv)))
75f9d97a 4607 {
8990e307 4608 mg->mg_obj = obj;
75f9d97a 4609 }
85e6fe83 4610 else {
8990e307 4611 mg->mg_obj = SvREFCNT_inc(obj);
85e6fe83
LW
4612 mg->mg_flags |= MGf_REFCOUNTED;
4613 }
8ccffa6a
YST
4614
4615 /* Normal self-ties simply pass a null object, and instead of
4616 using mg_obj directly, use the SvTIED_obj macro to produce a
4617 new RV as needed. For glob "self-ties", we are tieing the PVIO
4618 with an RV obj pointing to the glob containing the PVIO. In
4619 this case, to avoid a reference loop, we need to weaken the
4620 reference.
4621 */
4622
4623 if (how == PERL_MAGIC_tiedscalar && SvTYPE(sv) == SVt_PVIO &&
4624 obj && SvROK(obj) && GvIO(SvRV(obj)) == (IO*)sv)
4625 {
4626 sv_rvweaken(obj);
4627 }
4628
79072805 4629 mg->mg_type = how;
565764a8 4630 mg->mg_len = namlen;
9cbac4c7 4631 if (name) {
92110913 4632 if (namlen > 0)
1edc1566 4633 mg->mg_ptr = savepvn(name, namlen);
c6ee37c5 4634 else if (namlen == HEf_SVKEY)
1edc1566 4635 mg->mg_ptr = (char*)SvREFCNT_inc((SV*)name);
68795e93 4636 else
92110913 4637 mg->mg_ptr = (char *) name;
9cbac4c7 4638 }
92110913 4639 mg->mg_virtual = vtable;
68795e93 4640
92110913
NIS
4641 mg_magical(sv);
4642 if (SvGMAGICAL(sv))
4643 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
4644 return mg;
4645}
4646
4647/*
4648=for apidoc sv_magic
1c846c1f 4649
92110913
NIS
4650Adds magic to an SV. First upgrades C<sv> to type C<SVt_PVMG> if necessary,
4651then adds a new magic item of type C<how> to the head of the magic list.
4652
4653=cut
4654*/
4655
4656void
4657Perl_sv_magic(pTHX_ register SV *sv, SV *obj, int how, const char *name, I32 namlen)
68795e93 4658{
92110913
NIS
4659 MAGIC* mg;
4660 MGVTBL *vtable = 0;
4661
4662 if (SvREADONLY(sv)) {
ef7b71f0 4663 if (IN_PERL_RUNTIME
92110913
NIS
4664 && how != PERL_MAGIC_regex_global
4665 && how != PERL_MAGIC_bm
4666 && how != PERL_MAGIC_fm
4667 && how != PERL_MAGIC_sv
6125ae81 4668 && how != PERL_MAGIC_backref
92110913
NIS
4669 )
4670 {
4671 Perl_croak(aTHX_ PL_no_modify);
4672 }
4673 }
4674 if (SvMAGICAL(sv) || (how == PERL_MAGIC_taint && SvTYPE(sv) >= SVt_PVMG)) {
4675 if (SvMAGIC(sv) && (mg = mg_find(sv, how))) {
68795e93
NIS
4676 /* sv_magic() refuses to add a magic of the same 'how' as an
4677 existing one
92110913
NIS
4678 */
4679 if (how == PERL_MAGIC_taint)
4680 mg->mg_len |= 1;
4681 return;
4682 }
4683 }
68795e93 4684
79072805 4685 switch (how) {
14befaf4 4686 case PERL_MAGIC_sv:
92110913 4687 vtable = &PL_vtbl_sv;
79072805 4688 break;
14befaf4 4689 case PERL_MAGIC_overload:
92110913 4690 vtable = &PL_vtbl_amagic;
a0d0e21e 4691 break;
14befaf4 4692 case PERL_MAGIC_overload_elem:
92110913 4693 vtable = &PL_vtbl_amagicelem;
a0d0e21e 4694 break;
14befaf4 4695 case PERL_MAGIC_overload_table:
92110913 4696 vtable = &PL_vtbl_ovrld;
a0d0e21e 4697 break;
14befaf4 4698 case PERL_MAGIC_bm:
92110913 4699 vtable = &PL_vtbl_bm;
79072805 4700 break;
14befaf4 4701 case PERL_MAGIC_regdata:
92110913 4702 vtable = &PL_vtbl_regdata;
6cef1e77 4703 break;
14befaf4 4704 case PERL_MAGIC_regdatum:
92110913 4705 vtable = &PL_vtbl_regdatum;
6cef1e77 4706 break;
14befaf4 4707 case PERL_MAGIC_env:
92110913 4708 vtable = &PL_vtbl_env;
79072805 4709 break;
14befaf4 4710 case PERL_MAGIC_fm:
92110913 4711 vtable = &PL_vtbl_fm;
55497cff 4712 break;
14befaf4 4713 case PERL_MAGIC_envelem:
92110913 4714 vtable = &PL_vtbl_envelem;
79072805 4715 break;
14befaf4 4716 case PERL_MAGIC_regex_global:
92110913 4717 vtable = &PL_vtbl_mglob;
93a17b20 4718 break;
14befaf4 4719 case PERL_MAGIC_isa:
92110913 4720 vtable = &PL_vtbl_isa;
463ee0b2 4721 break;
14befaf4 4722 case PERL_MAGIC_isaelem:
92110913 4723 vtable = &PL_vtbl_isaelem;
463ee0b2 4724 break;
14befaf4 4725 case PERL_MAGIC_nkeys:
92110913 4726 vtable = &PL_vtbl_nkeys;
16660edb 4727 break;
14befaf4 4728 case PERL_MAGIC_dbfile:
92110913 4729 vtable = 0;
93a17b20 4730 break;
14befaf4 4731 case PERL_MAGIC_dbline:
92110913 4732 vtable = &PL_vtbl_dbline;
79072805 4733 break;
4d1ff10f 4734#ifdef USE_5005THREADS
14befaf4 4735 case PERL_MAGIC_mutex:
92110913 4736 vtable = &PL_vtbl_mutex;
f93b4edd 4737 break;
4d1ff10f 4738#endif /* USE_5005THREADS */
36477c24 4739#ifdef USE_LOCALE_COLLATE
14befaf4 4740 case PERL_MAGIC_collxfrm:
92110913 4741 vtable = &PL_vtbl_collxfrm;
bbce6d69 4742 break;
36477c24 4743#endif /* USE_LOCALE_COLLATE */
14befaf4 4744 case PERL_MAGIC_tied:
92110913 4745 vtable = &PL_vtbl_pack;
463ee0b2 4746 break;
14befaf4
DM
4747 case PERL_MAGIC_tiedelem:
4748 case PERL_MAGIC_tiedscalar:
92110913 4749 vtable = &PL_vtbl_packelem;
463ee0b2 4750 break;
14befaf4 4751 case PERL_MAGIC_qr:
92110913 4752 vtable = &PL_vtbl_regexp;
c277df42 4753 break;
14befaf4 4754 case PERL_MAGIC_sig:
92110913 4755 vtable = &PL_vtbl_sig;
79072805 4756 break;
14befaf4 4757 case PERL_MAGIC_sigelem:
92110913 4758 vtable = &PL_vtbl_sigelem;
79072805 4759 break;
14befaf4 4760 case PERL_MAGIC_taint:
92110913 4761 vtable = &PL_vtbl_taint;
463ee0b2 4762 break;
14befaf4 4763 case PERL_MAGIC_uvar:
92110913 4764 vtable = &PL_vtbl_uvar;
79072805 4765 break;
14befaf4 4766 case PERL_MAGIC_vec:
92110913 4767 vtable = &PL_vtbl_vec;
79072805 4768 break;
7d7ce6cc
JH
4769 case PERL_MAGIC_vstring:
4770 vtable = 0;
4771 break;
323eb6b5
JH
4772 case PERL_MAGIC_utf8:
4773 vtable = &PL_vtbl_utf8;
4774 break;
14befaf4 4775 case PERL_MAGIC_substr:
92110913 4776 vtable = &PL_vtbl_substr;
79072805 4777 break;
14befaf4 4778 case PERL_MAGIC_defelem:
92110913 4779 vtable = &PL_vtbl_defelem;
5f05dabc 4780 break;
14befaf4 4781 case PERL_MAGIC_glob:
92110913 4782 vtable = &PL_vtbl_glob;
79072805 4783 break;
14befaf4 4784 case PERL_MAGIC_arylen:
92110913 4785 vtable = &PL_vtbl_arylen;
79072805 4786 break;
14befaf4 4787 case PERL_MAGIC_pos:
92110913 4788 vtable = &PL_vtbl_pos;
a0d0e21e 4789 break;
14befaf4 4790 case PERL_MAGIC_backref:
92110913 4791 vtable = &PL_vtbl_backref;
810b8aa5 4792 break;
14befaf4
DM
4793 case PERL_MAGIC_ext:
4794 /* Reserved for use by extensions not perl internals. */
4633a7c4
LW
4795 /* Useful for attaching extension internal data to perl vars. */
4796 /* Note that multiple extensions may clash if magical scalars */
4797 /* etc holding private data from one are passed to another. */
a0d0e21e 4798 break;
79072805 4799 default:
14befaf4 4800 Perl_croak(aTHX_ "Don't know how to handle magic of type \\%o", how);
463ee0b2 4801 }
68795e93 4802
92110913
NIS
4803 /* Rest of work is done else where */
4804 mg = sv_magicext(sv,obj,how,vtable,name,namlen);
68795e93 4805
92110913
NIS
4806 switch (how) {
4807 case PERL_MAGIC_taint:
4808 mg->mg_len = 1;
4809 break;
4810 case PERL_MAGIC_ext:
4811 case PERL_MAGIC_dbfile:
4812 SvRMAGICAL_on(sv);
4813 break;
4814 }
463ee0b2
LW
4815}
4816
c461cf8f
JH
4817/*
4818=for apidoc sv_unmagic
4819
645c22ef 4820Removes all magic of type C<type> from an SV.
c461cf8f
JH
4821
4822=cut
4823*/
4824
463ee0b2 4825int
864dbfa3 4826Perl_sv_unmagic(pTHX_ SV *sv, int type)
463ee0b2
LW
4827{
4828 MAGIC* mg;
4829 MAGIC** mgp;
91bba347 4830 if (SvTYPE(sv) < SVt_PVMG || !SvMAGIC(sv))
463ee0b2
LW
4831 return 0;
4832 mgp = &SvMAGIC(sv);
4833 for (mg = *mgp; mg; mg = *mgp) {
4834 if (mg->mg_type == type) {
4835 MGVTBL* vtbl = mg->mg_virtual;
4836 *mgp = mg->mg_moremagic;
1d7c1841 4837 if (vtbl && vtbl->svt_free)
fc0dc3b3 4838 CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg);
14befaf4 4839 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
92110913 4840 if (mg->mg_len > 0)
1edc1566 4841 Safefree(mg->mg_ptr);
565764a8 4842 else if (mg->mg_len == HEf_SVKEY)
1edc1566 4843 SvREFCNT_dec((SV*)mg->mg_ptr);
323eb6b5
JH
4844 else if (mg->mg_type == PERL_MAGIC_utf8 && mg->mg_ptr)
4845 Safefree(mg->mg_ptr);
9cbac4c7 4846 }
a0d0e21e
LW
4847 if (mg->mg_flags & MGf_REFCOUNTED)
4848 SvREFCNT_dec(mg->mg_obj);
463ee0b2
LW
4849 Safefree(mg);
4850 }
4851 else
4852 mgp = &mg->mg_moremagic;
79072805 4853 }
91bba347 4854 if (!SvMAGIC(sv)) {
463ee0b2 4855 SvMAGICAL_off(sv);
06759ea0 4856 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_NOK|SVp_POK)) >> PRIVSHIFT;
463ee0b2
LW
4857 }
4858
4859 return 0;
79072805
LW
4860}
4861
c461cf8f
JH
4862/*
4863=for apidoc sv_rvweaken
4864
645c22ef
DM
4865Weaken a reference: set the C<SvWEAKREF> flag on this RV; give the
4866referred-to SV C<PERL_MAGIC_backref> magic if it hasn't already; and
4867push a back-reference to this RV onto the array of backreferences
4868associated with that magic.
c461cf8f
JH
4869
4870=cut
4871*/
4872
810b8aa5 4873SV *
864dbfa3 4874Perl_sv_rvweaken(pTHX_ SV *sv)
810b8aa5
GS
4875{
4876 SV *tsv;
4877 if (!SvOK(sv)) /* let undefs pass */
4878 return sv;
4879 if (!SvROK(sv))
cea2e8a9 4880 Perl_croak(aTHX_ "Can't weaken a nonreference");
810b8aa5 4881 else if (SvWEAKREF(sv)) {
810b8aa5 4882 if (ckWARN(WARN_MISC))
9014280d 4883 Perl_warner(aTHX_ packWARN(WARN_MISC), "Reference is already weak");
810b8aa5
GS
4884 return sv;
4885 }
4886 tsv = SvRV(sv);
4887 sv_add_backref(tsv, sv);
4888 SvWEAKREF_on(sv);
1c846c1f 4889 SvREFCNT_dec(tsv);
810b8aa5
GS
4890 return sv;
4891}
4892
645c22ef
DM
4893/* Give tsv backref magic if it hasn't already got it, then push a
4894 * back-reference to sv onto the array associated with the backref magic.
4895 */
4896
810b8aa5 4897STATIC void
cea2e8a9 4898S_sv_add_backref(pTHX_ SV *tsv, SV *sv)
810b8aa5
GS
4899{
4900 AV *av;
4901 MAGIC *mg;
14befaf4 4902 if (SvMAGICAL(tsv) && (mg = mg_find(tsv, PERL_MAGIC_backref)))
810b8aa5
GS
4903 av = (AV*)mg->mg_obj;
4904 else {
4905 av = newAV();
14befaf4 4906 sv_magic(tsv, (SV*)av, PERL_MAGIC_backref, NULL, 0);
219ef941
JH
4907 /* av now has a refcnt of 2, which avoids it getting freed
4908 * before us during global cleanup. The extra ref is removed
4909 * by magic_killbackrefs() when tsv is being freed */
810b8aa5 4910 }
f8cf5370
JH
4911 if (AvFILLp(av) >= AvMAX(av)) {
4912 SV **svp = AvARRAY(av);
f504ecf4
NC
4913 I32 i = AvFILLp(av);
4914 while (i >= 0) {
4915 if (svp[i] == &PL_sv_undef) {
f8cf5370
JH
4916 svp[i] = sv; /* reuse the slot */
4917 return;
4918 }
f504ecf4
NC
4919 i--;
4920 }
f8cf5370
JH
4921 av_extend(av, AvFILLp(av)+1);
4922 }
4923 AvARRAY(av)[++AvFILLp(av)] = sv; /* av_push() */
810b8aa5
GS
4924}
4925
645c22ef
DM
4926/* delete a back-reference to ourselves from the backref magic associated
4927 * with the SV we point to.
4928 */
4929
1c846c1f 4930STATIC void
cea2e8a9 4931S_sv_del_backref(pTHX_ SV *sv)
810b8aa5
GS
4932{
4933 AV *av;
4934 SV **svp;
4935 I32 i;
4936 SV *tsv = SvRV(sv);
c04a4dfe 4937 MAGIC *mg = NULL;
14befaf4 4938 if (!SvMAGICAL(tsv) || !(mg = mg_find(tsv, PERL_MAGIC_backref)))
cea2e8a9 4939 Perl_croak(aTHX_ "panic: del_backref");
810b8aa5
GS
4940 av = (AV *)mg->mg_obj;
4941 svp = AvARRAY(av);
f504ecf4
NC
4942 i = AvFILLp(av);
4943 while (i >= 0) {
4944 if (svp[i] == sv) {
4945 svp[i] = &PL_sv_undef; /* XXX */
4946 }
4947 i--;
4948 }
810b8aa5
GS
4949}
4950
954c1994
GS
4951/*
4952=for apidoc sv_insert
4953
4954Inserts a string at the specified offset/length within the SV. Similar to
4955the Perl substr() function.
4956
4957=cut
4958*/
4959
79072805 4960void
864dbfa3 4961Perl_sv_insert(pTHX_ SV *bigstr, STRLEN offset, STRLEN len, char *little, STRLEN littlelen)
79072805
LW
4962{
4963 register char *big;
4964 register char *mid;
4965 register char *midend;
4966 register char *bigend;
4967 register I32 i;
6ff81951 4968 STRLEN curlen;
1c846c1f 4969
79072805 4970
8990e307 4971 if (!bigstr)
cea2e8a9 4972 Perl_croak(aTHX_ "Can't modify non-existent substring");
6ff81951 4973 SvPV_force(bigstr, curlen);
60fa28ff 4974 (void)SvPOK_only_UTF8(bigstr);
6ff81951
GS
4975 if (offset + len > curlen) {
4976 SvGROW(bigstr, offset+len+1);
4977 Zero(SvPVX(bigstr)+curlen, offset+len-curlen, char);
4978 SvCUR_set(bigstr, offset+len);
4979 }
79072805 4980
69b47968 4981 SvTAINT(bigstr);
79072805
LW
4982 i = littlelen - len;
4983 if (i > 0) { /* string might grow */
a0d0e21e 4984 big = SvGROW(bigstr, SvCUR(bigstr) + i + 1);
79072805
LW
4985 mid = big + offset + len;
4986 midend = bigend = big + SvCUR(bigstr);
4987 bigend += i;
4988 *bigend = '\0';
4989 while (midend > mid) /* shove everything down */
4990 *--bigend = *--midend;
4991 Move(little,big+offset,littlelen,char);
4992 SvCUR(bigstr) += i;
4993 SvSETMAGIC(bigstr);
4994 return;
4995 }
4996 else if (i == 0) {
463ee0b2 4997 Move(little,SvPVX(bigstr)+offset,len,char);
79072805
LW
4998 SvSETMAGIC(bigstr);
4999 return;
5000 }
5001
463ee0b2 5002 big = SvPVX(bigstr);
79072805
LW
5003 mid = big + offset;
5004 midend = mid + len;
5005 bigend = big + SvCUR(bigstr);
5006
5007 if (midend > bigend)
cea2e8a9 5008 Perl_croak(aTHX_ "panic: sv_insert");
79072805
LW
5009
5010 if (mid - big > bigend - midend) { /* faster to shorten from end */
5011 if (littlelen) {
5012 Move(little, mid, littlelen,char);
5013 mid += littlelen;
5014 }
5015 i = bigend - midend;
5016 if (i > 0) {
5017 Move(midend, mid, i,char);
5018 mid += i;
5019 }
5020 *mid = '\0';
5021 SvCUR_set(bigstr, mid - big);
5022 }
5023 /*SUPPRESS 560*/
155aba94 5024 else if ((i = mid - big)) { /* faster from front */
79072805
LW
5025 midend -= littlelen;
5026 mid = midend;
5027 sv_chop(bigstr,midend-i);
5028 big += i;
5029 while (i--)
5030 *--midend = *--big;
5031 if (littlelen)
5032 Move(little, mid, littlelen,char);
5033 }
5034 else if (littlelen) {
5035 midend -= littlelen;
5036 sv_chop(bigstr,midend);
5037 Move(little,midend,littlelen,char);
5038 }
5039 else {
5040 sv_chop(bigstr,midend);
5041 }
5042 SvSETMAGIC(bigstr);
5043}
5044
c461cf8f
JH
5045/*
5046=for apidoc sv_replace
5047
5048Make the first argument a copy of the second, then delete the original.
645c22ef
DM
5049The target SV physically takes over ownership of the body of the source SV
5050and inherits its flags; however, the target keeps any magic it owns,
5051and any magic in the source is discarded.
ff276b08 5052Note that this is a rather specialist SV copying operation; most of the
645c22ef 5053time you'll want to use C<sv_setsv> or one of its many macro front-ends.
c461cf8f
JH
5054
5055=cut
5056*/
79072805
LW
5057
5058void
864dbfa3 5059Perl_sv_replace(pTHX_ register SV *sv, register SV *nsv)
79072805
LW
5060{
5061 U32 refcnt = SvREFCNT(sv);
2213622d 5062 SV_CHECK_THINKFIRST(sv);
0453d815 5063 if (SvREFCNT(nsv) != 1 && ckWARN_d(WARN_INTERNAL))
9014280d 5064 Perl_warner(aTHX_ packWARN(WARN_INTERNAL), "Reference miscount in sv_replace()");
93a17b20 5065 if (SvMAGICAL(sv)) {
a0d0e21e
LW
5066 if (SvMAGICAL(nsv))
5067 mg_free(nsv);
5068 else
5069 sv_upgrade(nsv, SVt_PVMG);
93a17b20 5070 SvMAGIC(nsv) = SvMAGIC(sv);
a0d0e21e 5071 SvFLAGS(nsv) |= SvMAGICAL(sv);
93a17b20
LW
5072 SvMAGICAL_off(sv);
5073 SvMAGIC(sv) = 0;
5074 }
79072805
LW
5075 SvREFCNT(sv) = 0;
5076 sv_clear(sv);
477f5d66 5077 assert(!SvREFCNT(sv));
79072805
LW
5078 StructCopy(nsv,sv,SV);
5079 SvREFCNT(sv) = refcnt;
1edc1566 5080 SvFLAGS(nsv) |= SVTYPEMASK; /* Mark as freed */
007262b0 5081 SvREFCNT(nsv) = 0;
463ee0b2 5082 del_SV(nsv);
79072805
LW
5083}
5084
c461cf8f
JH
5085/*
5086=for apidoc sv_clear
5087
645c22ef
DM
5088Clear an SV: call any destructors, free up any memory used by the body,
5089and free the body itself. The SV's head is I<not> freed, although
5090its type is set to all 1's so that it won't inadvertently be assumed
5091to be live during global destruction etc.
5092This function should only be called when REFCNT is zero. Most of the time
5093you'll want to call C<sv_free()> (or its macro wrapper C<SvREFCNT_dec>)
5094instead.
c461cf8f
JH
5095
5096=cut
5097*/
5098
79072805 5099void
864dbfa3 5100Perl_sv_clear(pTHX_ register SV *sv)
79072805 5101{
ec12f114 5102 HV* stash;
79072805
LW
5103 assert(sv);
5104 assert(SvREFCNT(sv) == 0);
5105
ed6116ce 5106 if (SvOBJECT(sv)) {
3280af22 5107 if (PL_defstash) { /* Still have a symbol table? */
39644a26 5108 dSP;
32251b26 5109 CV* destructor;
a0d0e21e 5110
3e5ba712 5111
8ebc5c01 5112
d460ef45 5113 do {
4e8e7886 5114 stash = SvSTASH(sv);
32251b26 5115 destructor = StashHANDLER(stash,DESTROY);
4e8e7886 5116 if (destructor) {
3e5ba712
JH
5117 SV* tmpref = newRV(sv);
5118 SvREADONLY_on(tmpref); /* DESTROY() could be naughty */
4e8e7886 5119 ENTER;
e788e7d3 5120 PUSHSTACKi(PERLSI_DESTROY);
4e8e7886
GS
5121 EXTEND(SP, 2);
5122 PUSHMARK(SP);
3e5ba712 5123 PUSHs(tmpref);
4e8e7886 5124 PUTBACK;
44389ee9 5125 call_sv((SV*)destructor, G_DISCARD|G_EVAL|G_KEEPERR|G_VOID);
3e5ba712
JH
5126
5127
d3acc0f7 5128 POPSTACK;
3095d977 5129 SPAGAIN;
4e8e7886 5130 LEAVE;
3e5ba712
JH
5131 if(SvREFCNT(tmpref) < 2) {
5132 /* tmpref is not kept alive! */
5133 SvREFCNT(sv)--;
5134 SvRV(tmpref) = 0;
5135 SvROK_off(tmpref);
5136 }
5137 SvREFCNT_dec(tmpref);
4e8e7886
GS
5138 }
5139 } while (SvOBJECT(sv) && SvSTASH(sv) != stash);
8ebc5c01 5140
6f44e0a4
JP
5141
5142 if (SvREFCNT(sv)) {
5143 if (PL_in_clean_objs)
cea2e8a9 5144 Perl_croak(aTHX_ "DESTROY created new reference to dead object '%s'",
6f44e0a4
JP
5145 HvNAME(stash));
5146 /* DESTROY gave object new lease on life */
5147 return;
5148 }
a0d0e21e 5149 }
4e8e7886 5150
a0d0e21e 5151 if (SvOBJECT(sv)) {
4e8e7886 5152 SvREFCNT_dec(SvSTASH(sv)); /* possibly of changed persuasion */
a0d0e21e
LW
5153 SvOBJECT_off(sv); /* Curse the object. */
5154 if (SvTYPE(sv) != SVt_PVIO)
3280af22 5155 --PL_sv_objcount; /* XXX Might want something more general */
a0d0e21e 5156 }
463ee0b2 5157 }
524189f1
JH
5158 if (SvTYPE(sv) >= SVt_PVMG) {
5159 if (SvMAGIC(sv))
5160 mg_free(sv);
5161 if (SvFLAGS(sv) & SVpad_TYPED)
5162 SvREFCNT_dec(SvSTASH(sv));
5163 }
ec12f114 5164 stash = NULL;
79072805 5165 switch (SvTYPE(sv)) {
8990e307 5166 case SVt_PVIO:
df0bd2f4
GS
5167 if (IoIFP(sv) &&
5168 IoIFP(sv) != PerlIO_stdin() &&
5f05dabc 5169 IoIFP(sv) != PerlIO_stdout() &&
5170 IoIFP(sv) != PerlIO_stderr())
93578b34 5171 {
f2b5be74 5172 io_close((IO*)sv, FALSE);
93578b34 5173 }
1d7c1841 5174 if (IoDIRP(sv) && !(IoFLAGS(sv) & IOf_FAKE_DIRP))
1236053a 5175 PerlDir_close(IoDIRP(sv));
1d7c1841 5176 IoDIRP(sv) = (DIR*)NULL;
8990e307
LW
5177 Safefree(IoTOP_NAME(sv));
5178 Safefree(IoFMT_NAME(sv));
5179 Safefree(IoBOTTOM_NAME(sv));
a0d0e21e 5180 /* FALL THROUGH */
79072805 5181 case SVt_PVBM:
a0d0e21e 5182 goto freescalar;
79072805 5183 case SVt_PVCV:
748a9306 5184 case SVt_PVFM:
85e6fe83 5185 cv_undef((CV*)sv);
a0d0e21e 5186 goto freescalar;
79072805 5187 case SVt_PVHV:
85e6fe83 5188 hv_undef((HV*)sv);
a0d0e21e 5189 break;
79072805 5190 case SVt_PVAV:
85e6fe83 5191 av_undef((AV*)sv);
a0d0e21e 5192 break;
02270b4e 5193 case SVt_PVLV:
73c86719
JH
5194 if (LvTYPE(sv) == 'T') { /* for tie: return HE to pool */
5195 SvREFCNT_dec(HeKEY_sv((HE*)LvTARG(sv)));
5196 HeNEXT((HE*)LvTARG(sv)) = PL_hv_fetch_ent_mh;
5197 PL_hv_fetch_ent_mh = (HE*)LvTARG(sv);
5198 }
5199 else if (LvTYPE(sv) != 't') /* unless tie: unrefcnted fake SV** */
5200 SvREFCNT_dec(LvTARG(sv));
02270b4e 5201 goto freescalar;
a0d0e21e 5202 case SVt_PVGV:
1edc1566 5203 gp_free((GV*)sv);
a0d0e21e 5204 Safefree(GvNAME(sv));
ec12f114
JPC
5205 /* cannot decrease stash refcount yet, as we might recursively delete
5206 ourselves when the refcnt drops to zero. Delay SvREFCNT_dec
5207 of stash until current sv is completely gone.
5208 -- JohnPC, 27 Mar 1998 */
5209 stash = GvSTASH(sv);
a0d0e21e 5210 /* FALL THROUGH */
79072805 5211 case SVt_PVMG:
79072805
LW
5212 case SVt_PVNV:
5213 case SVt_PVIV:
a0d0e21e
LW
5214 freescalar:
5215 (void)SvOOK_off(sv);
79072805
LW
5216 /* FALL THROUGH */
5217 case SVt_PV:
a0d0e21e 5218 case SVt_RV:
810b8aa5
GS
5219 if (SvROK(sv)) {
5220 if (SvWEAKREF(sv))
5221 sv_del_backref(sv);
5222 else
5223 SvREFCNT_dec(SvRV(sv));
5224 }
1edc1566 5225 else if (SvPVX(sv) && SvLEN(sv))
463ee0b2 5226 Safefree(SvPVX(sv));
1c846c1f 5227 else if (SvPVX(sv) && SvREADONLY(sv) && SvFAKE(sv)) {
25716404
GS
5228 unsharepvn(SvPVX(sv),
5229 SvUTF8(sv) ? -(I32)SvCUR(sv) : SvCUR(sv),
5230 SvUVX(sv));
1c846c1f
NIS
5231 SvFAKE_off(sv);
5232 }
79072805 5233 break;
a0d0e21e 5234/*
79072805 5235 case SVt_NV:
79072805 5236 case SVt_IV:
79072805
LW
5237 case SVt_NULL:
5238 break;
a0d0e21e 5239*/
79072805
LW
5240 }
5241
5242 switch (SvTYPE(sv)) {
5243 case SVt_NULL:
5244 break;
79072805
LW
5245 case SVt_IV:
5246 del_XIV(SvANY(sv));
5247 break;
5248 case SVt_NV:
5249 del_XNV(SvANY(sv));
5250 break;
ed6116ce
LW
5251 case SVt_RV:
5252 del_XRV(SvANY(sv));
5253 break;
79072805
LW
5254 case SVt_PV:
5255 del_XPV(SvANY(sv));
5256 break;
5257 case SVt_PVIV:
5258 del_XPVIV(SvANY(sv));
5259 break;
5260 case SVt_PVNV:
5261 del_XPVNV(SvANY(sv));
5262 break;
5263 case SVt_PVMG:
5264 del_XPVMG(SvANY(sv));
5265 break;
5266 case SVt_PVLV:
5267 del_XPVLV(SvANY(sv));
5268 break;
5269 case SVt_PVAV:
5270 del_XPVAV(SvANY(sv));
5271 break;
5272 case SVt_PVHV:
5273 del_XPVHV(SvANY(sv));
5274 break;
5275 case SVt_PVCV:
5276 del_XPVCV(SvANY(sv));
5277 break;
5278 case SVt_PVGV:
5279 del_XPVGV(SvANY(sv));
ec12f114
JPC
5280 /* code duplication for increased performance. */
5281 SvFLAGS(sv) &= SVf_BREAK;
5282 SvFLAGS(sv) |= SVTYPEMASK;
5283 /* decrease refcount of the stash that owns this GV, if any */
5284 if (stash)
5285 SvREFCNT_dec(stash);
5286 return; /* not break, SvFLAGS reset already happened */
79072805
LW
5287 case SVt_PVBM:
5288 del_XPVBM(SvANY(sv));
5289 break;
5290 case SVt_PVFM:
5291 del_XPVFM(SvANY(sv));
5292 break;
8990e307
LW
5293 case SVt_PVIO:
5294 del_XPVIO(SvANY(sv));
5295 break;
79072805 5296 }
a0d0e21e 5297 SvFLAGS(sv) &= SVf_BREAK;
8990e307 5298 SvFLAGS(sv) |= SVTYPEMASK;
79072805
LW
5299}
5300
645c22ef
DM
5301/*
5302=for apidoc sv_newref
5303
5304Increment an SV's reference count. Use the C<SvREFCNT_inc()> wrapper
5305instead.
5306
5307=cut
5308*/
5309
79072805 5310SV *
864dbfa3 5311Perl_sv_newref(pTHX_ SV *sv)
79072805 5312{
463ee0b2 5313 if (sv)
dce16143 5314 ATOMIC_INC(SvREFCNT(sv));
79072805
LW
5315 return sv;
5316}
5317
c461cf8f
JH
5318/*
5319=for apidoc sv_free
5320
645c22ef
DM
5321Decrement an SV's reference count, and if it drops to zero, call
5322C<sv_clear> to invoke destructors and free up any memory used by
5323the body; finally, deallocate the SV's head itself.
5324Normally called via a wrapper macro C<SvREFCNT_dec>.
c461cf8f
JH
5325
5326=cut
5327*/
5328
79072805 5329void
864dbfa3 5330Perl_sv_free(pTHX_ SV *sv)
79072805 5331{
dce16143
MB
5332 int refcount_is_zero;
5333
79072805
LW
5334 if (!sv)
5335 return;
a0d0e21e
LW
5336 if (SvREFCNT(sv) == 0) {
5337 if (SvFLAGS(sv) & SVf_BREAK)
645c22ef
DM
5338 /* this SV's refcnt has been artificially decremented to
5339 * trigger cleanup */
a0d0e21e 5340 return;
3280af22 5341 if (PL_in_clean_all) /* All is fair */
1edc1566 5342 return;
d689ffdd
JP
5343 if (SvREADONLY(sv) && SvIMMORTAL(sv)) {
5344 /* make sure SvREFCNT(sv)==0 happens very seldom */
5345 SvREFCNT(sv) = (~(U32)0)/2;
5346 return;
5347 }
0453d815 5348 if (ckWARN_d(WARN_INTERNAL))
14347439 5349 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
b035a42e
NC
5350 "Attempt to free unreferenced scalar: SV 0x%"UVxf
5351 pTHX__FORMAT, PTR2UV(sv) pTHX__VALUE);
79072805
LW
5352 return;
5353 }
dce16143 5354 ATOMIC_DEC_AND_TEST(refcount_is_zero, SvREFCNT(sv));
b881518d 5355 if (!refcount_is_zero)
8990e307 5356 return;
463ee0b2
LW
5357#ifdef DEBUGGING
5358 if (SvTEMP(sv)) {
0453d815 5359 if (ckWARN_d(WARN_DEBUGGING))
9014280d 5360 Perl_warner(aTHX_ packWARN(WARN_DEBUGGING),
b035a42e
NC
5361 "Attempt to free temp prematurely: SV 0x%"UVxf
5362 pTHX__FORMAT, PTR2UV(sv) pTHX__VALUE);
79072805 5363 return;
79072805 5364 }
463ee0b2 5365#endif
d689ffdd
JP
5366 if (SvREADONLY(sv) && SvIMMORTAL(sv)) {
5367 /* make sure SvREFCNT(sv)==0 happens very seldom */
5368 SvREFCNT(sv) = (~(U32)0)/2;
5369 return;
5370 }
79072805 5371 sv_clear(sv);
477f5d66
CS
5372 if (! SvREFCNT(sv))
5373 del_SV(sv);
79072805
LW
5374}
5375
954c1994
GS
5376/*
5377=for apidoc sv_len
5378
645c22ef
DM
5379Returns the length of the string in the SV. Handles magic and type
5380coercion. See also C<SvCUR>, which gives raw access to the xpv_cur slot.
954c1994
GS
5381
5382=cut
5383*/
5384
79072805 5385STRLEN
864dbfa3 5386Perl_sv_len(pTHX_ register SV *sv)
79072805 5387{
463ee0b2 5388 STRLEN len;
79072805
LW
5389
5390 if (!sv)
5391 return 0;
5392
8990e307 5393 if (SvGMAGICAL(sv))
565764a8 5394 len = mg_length(sv);
8990e307 5395 else
497b47a8 5396 (void)SvPV(sv, len);
463ee0b2 5397 return len;
79072805
LW
5398}
5399
c461cf8f
JH
5400/*
5401=for apidoc sv_len_utf8
5402
5403Returns the number of characters in the string in an SV, counting wide
cd458e05 5404UTF-8 bytes as a single character. Handles magic and type coercion.
c461cf8f
JH
5405
5406=cut
5407*/
5408
323eb6b5
JH
5409/*
5410 * The length is cached in PERL_UTF8_magic, in the mg_len field. Also the
5411 * mg_ptr is used, by sv_pos_u2b(), see the comments of S_utf8_mg_pos_init().
5412 * (Note that the mg_len is not the length of the mg_ptr field.)
5413 *
5414 */
5415
a0ed51b3 5416STRLEN
864dbfa3 5417Perl_sv_len_utf8(pTHX_ register SV *sv)
a0ed51b3 5418{
a0ed51b3
LW
5419 if (!sv)
5420 return 0;
5421
a0ed51b3 5422 if (SvGMAGICAL(sv))
b76347f2 5423 return mg_length(sv);
a0ed51b3 5424 else
b76347f2 5425 {
323eb6b5 5426 STRLEN len, ulen;
b76347f2 5427 U8 *s = (U8*)SvPV(sv, len);
323eb6b5
JH
5428 MAGIC *mg = SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_utf8) : 0;
5429
d6218095 5430 if (mg && mg->mg_len != -1 && (mg->mg_len > 0 || len == 0)) {
323eb6b5 5431 ulen = mg->mg_len;
d6218095
JH
5432#ifdef PERL_UTF8_CACHE_ASSERT
5433 assert(ulen == Perl_utf8_length(aTHX_ s, s + len));
5434#endif
5435 }
323eb6b5
JH
5436 else {
5437 ulen = Perl_utf8_length(aTHX_ s, s + len);
5438 if (!mg && !SvREADONLY(sv)) {
5439 sv_magic(sv, 0, PERL_MAGIC_utf8, 0, 0);
5440 mg = mg_find(sv, PERL_MAGIC_utf8);
5441 assert(mg);
5442 }
5443 if (mg)
5444 mg->mg_len = ulen;
5445 }
5446 return ulen;
5447 }
5448}
5449
5450/* S_utf8_mg_pos_init() is used to initialize the mg_ptr field of
5451 * a PERL_UTF8_magic. The mg_ptr is used to store the mapping
5452 * between UTF-8 and byte offsets. There are two (substr offset and substr
5453 * length, the i offset, PERL_MAGIC_UTF8_CACHESIZE) times two (UTF-8 offset
5454 * and byte offset) cache positions.
5455 *
5456 * The mg_len field is used by sv_len_utf8(), see its comments.
5457 * Note that the mg_len is not the length of the mg_ptr field.
5458 *
5459 */
5460STATIC bool
91eb8809 5461S_utf8_mg_pos_init(pTHX_ SV *sv, MAGIC **mgp, STRLEN **cachep, I32 i, I32 *offsetp, U8 *s, U8 *start)
323eb6b5
JH
5462{
5463 bool found = FALSE;
5464
5465 if (SvMAGICAL(sv) && !SvREADONLY(sv)) {
1fce4797
NC
5466 if (!*mgp)
5467 *mgp = sv_magicext(sv, 0, PERL_MAGIC_utf8, &PL_vtbl_utf8, 0, 0);
5468 assert(*mgp);
b76347f2 5469
323eb6b5
JH
5470 if ((*mgp)->mg_ptr)
5471 *cachep = (STRLEN *) (*mgp)->mg_ptr;
5472 else {
5473 Newz(0, *cachep, PERL_MAGIC_UTF8_CACHESIZE * 2, STRLEN);
5474 (*mgp)->mg_ptr = (char *) *cachep;
5475 }
5476 assert(*cachep);
5477
5478 (*cachep)[i] = *offsetp;
5479 (*cachep)[i+1] = s - start;
5480 found = TRUE;
5481 }
5482
5483 return found;
5484}
5485
5486/*
5487 * S_utf8_mg_pos() is used to query and update mg_ptr field of
5488 * a PERL_UTF8_magic. The mg_ptr is used to store the mapping
5489 * between UTF-8 and byte offsets. See also the comments of
5490 * S_utf8_mg_pos_init().
5491 *
5492 */
5493STATIC bool
91eb8809 5494S_utf8_mg_pos(pTHX_ SV *sv, MAGIC **mgp, STRLEN **cachep, I32 i, I32 *offsetp, I32 uoff, U8 **sp, U8 *start, U8 *send)
323eb6b5
JH
5495{
5496 bool found = FALSE;
5497
5498 if (SvMAGICAL(sv) && !SvREADONLY(sv)) {
5499 if (!*mgp)
5500 *mgp = mg_find(sv, PERL_MAGIC_utf8);
5501 if (*mgp && (*mgp)->mg_ptr) {
5502 *cachep = (STRLEN *) (*mgp)->mg_ptr;
d6218095 5503 ASSERT_UTF8_CACHE(*cachep);
91eb8809 5504 if ((*cachep)[i] == (STRLEN)uoff) /* An exact match. */
323eb6b5
JH
5505 found = TRUE;
5506 else { /* We will skip to the right spot. */
5507 STRLEN forw = 0;
5508 STRLEN backw = 0;
5509 U8* p = NULL;
5510
5511 /* The assumption is that going backward is half
5512 * the speed of going forward (that's where the
5513 * 2 * backw in the below comes from). (The real
5514 * figure of course depends on the UTF-8 data.) */
5515
91eb8809 5516 if ((*cachep)[i] > (STRLEN)uoff) {
323eb6b5 5517 forw = uoff;
91eb8809 5518 backw = (*cachep)[i] - (STRLEN)uoff;
323eb6b5
JH
5519
5520 if (forw < 2 * backw)
5521 p = start;
5522 else
5523 p = start + (*cachep)[i+1];
5524 }
5525 /* Try this only for the substr offset (i == 0),
5526 * not for the substr length (i == 2). */
5527 else if (i == 0) { /* (*cachep)[i] < uoff */
5528 STRLEN ulen = sv_len_utf8(sv);
5529
91eb8809
JH
5530 if ((STRLEN)uoff < ulen) {
5531 forw = (STRLEN)uoff - (*cachep)[i];
5532 backw = ulen - (STRLEN)uoff;
323eb6b5
JH
5533
5534 if (forw < 2 * backw)
5535 p = start + (*cachep)[i+1];
5536 else
5537 p = send;
5538 }
5539
5540 /* If the string is not long enough for uoff,
5541 * we could extend it, but not at this low a level. */
5542 }
5543
5544 if (p) {
5545 if (forw < 2 * backw) {
5546 while (forw--)
5547 p += UTF8SKIP(p);
5548 }
5549 else {
5550 while (backw--) {
5551 p--;
5552 while (UTF8_IS_CONTINUATION(*p))
5553 p--;
5554 }
5555 }
5556
5557 /* Update the cache. */
91eb8809 5558 (*cachep)[i] = (STRLEN)uoff;
323eb6b5
JH
5559 (*cachep)[i+1] = p - start;
5560
1fce4797
NC
5561 /* Drop the stale "length" cache */
5562 if (i == 0) {
5563 (*cachep)[2] = 0;
5564 (*cachep)[3] = 0;
5565 }
5566
323eb6b5
JH
5567 found = TRUE;
5568 }
5569 }
5570 if (found) { /* Setup the return values. */
5571 *offsetp = (*cachep)[i+1];
5572 *sp = start + *offsetp;
5573 if (*sp >= send) {
5574 *sp = send;
5575 *offsetp = send - start;
5576 }
5577 else if (*sp < start) {
5578 *sp = start;
5579 *offsetp = 0;
5580 }
5581 }
d6218095
JH
5582 }
5583#ifdef PERL_UTF8_CACHE_ASSERT
5584 if (found) {
5585 U8 *s = start;
5586 I32 n = uoff;
5587
5588 while (n-- && s < send)
5589 s += UTF8SKIP(s);
5590
5591 if (i == 0) {
5592 assert(*offsetp == s - start);
5593 assert((*cachep)[0] == (STRLEN)uoff);
5594 assert((*cachep)[1] == *offsetp);
5595 }
5596 ASSERT_UTF8_CACHE(*cachep);
5597 }
5598#endif
a0ed51b3 5599 }
323eb6b5
JH
5600
5601 return found;
a0ed51b3
LW
5602}
5603
645c22ef
DM
5604/*
5605=for apidoc sv_pos_u2b
5606
cd458e05 5607Converts the value pointed to by offsetp from a count of UTF-8 chars from
645c22ef
DM
5608the start of the string, to a count of the equivalent number of bytes; if
5609lenp is non-zero, it does the same to lenp, but this time starting from
5610the offset, rather than from the start of the string. Handles magic and
5611type coercion.
5612
5613=cut
5614*/
5615
323eb6b5
JH
5616/*
5617 * sv_pos_u2b() uses, like sv_pos_b2u(), the mg_ptr of the potential
5618 * PERL_UTF8_magic of the sv to store the mapping between UTF-8 and
5619 * byte offsets. See also the comments of S_utf8_mg_pos().
5620 *
5621 */
5622
a0ed51b3 5623void
864dbfa3 5624Perl_sv_pos_u2b(pTHX_ register SV *sv, I32* offsetp, I32* lenp)
a0ed51b3 5625{
dfe13c55
GS
5626 U8 *start;
5627 U8 *s;
a0ed51b3 5628 STRLEN len;
323eb6b5
JH
5629 STRLEN *cache = 0;
5630 STRLEN boffset = 0;
a0ed51b3
LW
5631
5632 if (!sv)
5633 return;
5634
dfe13c55 5635 start = s = (U8*)SvPV(sv, len);
323eb6b5
JH
5636 if (len) {
5637 I32 uoffset = *offsetp;
5638 U8 *send = s + len;
5639 MAGIC *mg = 0;
5640 bool found = FALSE;
5641
5eed253f 5642 if (utf8_mg_pos(sv, &mg, &cache, 0, offsetp, *offsetp, &s, start, send))
323eb6b5
JH
5643 found = TRUE;
5644 if (!found && uoffset > 0) {
5645 while (s < send && uoffset--)
5646 s += UTF8SKIP(s);
5647 if (s >= send)
5648 s = send;
5eed253f 5649 if (utf8_mg_pos_init(sv, &mg, &cache, 0, offsetp, s, start))
323eb6b5
JH
5650 boffset = cache[1];
5651 *offsetp = s - start;
5652 }
5653 if (lenp) {
5654 found = FALSE;
5655 start = s;
5eed253f 5656 if (utf8_mg_pos(sv, &mg, &cache, 2, lenp, *lenp + *offsetp, &s, start, send)) {
323eb6b5
JH
5657 *lenp -= boffset;
5658 found = TRUE;
5659 }
5660 if (!found && *lenp > 0) {
5661 I32 ulen = *lenp;
5662 if (ulen > 0)
5663 while (s < send && ulen--)
5664 s += UTF8SKIP(s);
5665 if (s >= send)
5666 s = send;
b035a42e 5667 utf8_mg_pos_init(sv, &mg, &cache, 2, lenp, s, start);
323eb6b5
JH
5668 }
5669 *lenp = s - start;
5670 }
d6218095 5671 ASSERT_UTF8_CACHE(cache);
323eb6b5
JH
5672 }
5673 else {
5674 *offsetp = 0;
5675 if (lenp)
5676 *lenp = 0;
a0ed51b3 5677 }
d6218095 5678
a0ed51b3
LW
5679 return;
5680}
5681
645c22ef
DM
5682/*
5683=for apidoc sv_pos_b2u
5684
5685Converts the value pointed to by offsetp from a count of bytes from the
cd458e05 5686start of the string, to a count of the equivalent number of UTF-8 chars.
645c22ef
DM
5687Handles magic and type coercion.
5688
5689=cut
5690*/
5691
323eb6b5
JH
5692/*
5693 * sv_pos_b2u() uses, like sv_pos_u2b(), the mg_ptr of the potential
5694 * PERL_UTF8_magic of the sv to store the mapping between UTF-8 and
5695 * byte offsets. See also the comments of S_utf8_mg_pos().
5696 *
5697 */
5698
a0ed51b3 5699void
323eb6b5 5700Perl_sv_pos_b2u(pTHX_ register SV* sv, I32* offsetp)
a0ed51b3 5701{
323eb6b5 5702 U8* s;
a0ed51b3
LW
5703 STRLEN len;
5704
5705 if (!sv)
5706 return;
5707
dfe13c55 5708 s = (U8*)SvPV(sv, len);
eb160463 5709 if ((I32)len < *offsetp)
a0dbb045 5710 Perl_croak(aTHX_ "panic: sv_pos_b2u: bad byte offset");
323eb6b5 5711 else {
4a9f3d2e
NC
5712 U8* send = s + *offsetp;
5713 MAGIC* mg = NULL;
5714 STRLEN *cache = NULL;
323eb6b5 5715
4a9f3d2e 5716 len = 0;
323eb6b5 5717
4a9f3d2e
NC
5718 if (SvMAGICAL(sv) && !SvREADONLY(sv)) {
5719 mg = mg_find(sv, PERL_MAGIC_utf8);
5720 if (mg && mg->mg_ptr) {
5721 cache = (STRLEN *) mg->mg_ptr;
5722 if (cache[1] == (STRLEN)*offsetp) {
5723 /* An exact match. */
5724 *offsetp = cache[0];
323eb6b5 5725
4a9f3d2e
NC
5726 return;
5727 }
5728 else if (cache[1] < (STRLEN)*offsetp) {
5729 /* We already know part of the way. */
5730 len = cache[0];
5731 s += cache[1];
5732 /* Let the below loop do the rest. */
5733 }
5734 else { /* cache[1] > *offsetp */
5735 /* We already know all of the way, now we may
5736 * be able to walk back. The same assumption
5737 * is made as in S_utf8_mg_pos(), namely that
5738 * walking backward is twice slower than
5739 * walking forward. */
5740 STRLEN forw = *offsetp;
5741 STRLEN backw = cache[1] - *offsetp;
5742
5743 if (!(forw < 2 * backw)) {
5744 U8 *p = s + cache[1];
5745 STRLEN ubackw = 0;
323eb6b5 5746
4a9f3d2e 5747 cache[1] -= backw;
338501c1 5748
4a9f3d2e 5749 while (backw--) {
b9219079
JH
5750 p--;
5751 while (UTF8_IS_CONTINUATION(*p)) {
5752 p--;
5753 backw--;
5754 }
5755 ubackw++;
323eb6b5 5756 }
b9219079
JH
5757
5758 cache[0] -= ubackw;
5759 *offsetp = cache[0];
b035a42e
NC
5760
5761 /* Drop the stale "length" cache */
5762 cache[2] = 0;
5763 cache[3] = 0;
5764
b9219079
JH
5765 return;
5766 }
5767 }
5768 }
d6218095 5769 ASSERT_UTF8_CACHE(cache);
323eb6b5
JH
5770 }
5771
4a9f3d2e
NC
5772 while (s < send) {
5773 STRLEN n = 1;
323eb6b5 5774
4a9f3d2e
NC
5775 /* Call utf8n_to_uvchr() to validate the sequence
5776 * (unless a simple non-UTF character) */
5777 if (!UTF8_IS_INVARIANT(*s))
5778 utf8n_to_uvchr(s, UTF8SKIP(s), &n, 0);
5779 if (n > 0) {
5780 s += n;
5781 len++;
5782 }
5783 else
5784 break;
5785 }
323eb6b5 5786
4a9f3d2e
NC
5787 if (!SvREADONLY(sv)) {
5788 if (!mg) {
5789 sv_magic(sv, 0, PERL_MAGIC_utf8, 0, 0);
5790 mg = mg_find(sv, PERL_MAGIC_utf8);
5791 }
5792 assert(mg);
323eb6b5 5793
4a9f3d2e
NC
5794 if (!mg->mg_ptr) {
5795 Newz(0, cache, PERL_MAGIC_UTF8_CACHESIZE * 2, STRLEN);
5796 mg->mg_ptr = (char *) cache;
5797 }
5798 assert(cache);
323eb6b5 5799
4a9f3d2e
NC
5800 cache[0] = len;
5801 cache[1] = *offsetp;
b035a42e
NC
5802 /* Drop the stale "length" cache */
5803 cache[2] = 0;
5804 cache[3] = 0;
4a9f3d2e 5805 }
323eb6b5 5806
4a9f3d2e 5807 *offsetp = len;
a0ed51b3 5808 }
323eb6b5 5809
a0ed51b3
LW
5810 return;
5811}
5812
954c1994
GS
5813/*
5814=for apidoc sv_eq
5815
5816Returns a boolean indicating whether the strings in the two SVs are
645c22ef
DM
5817identical. Is UTF-8 and 'use bytes' aware, handles get magic, and will
5818coerce its args to strings if necessary.
954c1994
GS
5819
5820=cut
5821*/
5822
79072805 5823I32
e01b9e88 5824Perl_sv_eq(pTHX_ register SV *sv1, register SV *sv2)
79072805
LW
5825{
5826 char *pv1;
463ee0b2 5827 STRLEN cur1;
79072805 5828 char *pv2;
463ee0b2 5829 STRLEN cur2;
e01b9e88 5830 I32 eq = 0;
553e1bcc
AT
5831 char *tpv = Nullch;
5832 SV* svrecode = Nullsv;
79072805 5833
e01b9e88 5834 if (!sv1) {
79072805
LW
5835 pv1 = "";
5836 cur1 = 0;
5837 }
463ee0b2 5838 else
e01b9e88 5839 pv1 = SvPV(sv1, cur1);
79072805 5840
e01b9e88
SC
5841 if (!sv2){
5842 pv2 = "";
5843 cur2 = 0;
92d29cee 5844 }
e01b9e88
SC
5845 else
5846 pv2 = SvPV(sv2, cur2);
79072805 5847
cf48d248 5848 if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTES) {
799ef3cb
JH
5849 /* Differing utf8ness.
5850 * Do not UTF8size the comparands as a side-effect. */
5851 if (PL_encoding) {
5852 if (SvUTF8(sv1)) {
553e1bcc
AT
5853 svrecode = newSVpvn(pv2, cur2);
5854 sv_recode_to_utf8(svrecode, PL_encoding);
5855 pv2 = SvPV(svrecode, cur2);
799ef3cb
JH
5856 }
5857 else {
553e1bcc
AT
5858 svrecode = newSVpvn(pv1, cur1);
5859 sv_recode_to_utf8(svrecode, PL_encoding);
5860 pv1 = SvPV(svrecode, cur1);
799ef3cb
JH
5861 }
5862 /* Now both are in UTF-8. */
5863 if (cur1 != cur2)
5864 return FALSE;
5865 }
5866 else {
5867 bool is_utf8 = TRUE;
5868
5869 if (SvUTF8(sv1)) {
5870 /* sv1 is the UTF-8 one,
5871 * if is equal it must be downgrade-able */
5872 char *pv = (char*)bytes_from_utf8((U8*)pv1,
5873 &cur1, &is_utf8);
5874 if (pv != pv1)
553e1bcc 5875 pv1 = tpv = pv;
799ef3cb
JH
5876 }
5877 else {
5878 /* sv2 is the UTF-8 one,
5879 * if is equal it must be downgrade-able */
5880 char *pv = (char *)bytes_from_utf8((U8*)pv2,
5881 &cur2, &is_utf8);
5882 if (pv != pv2)
553e1bcc 5883 pv2 = tpv = pv;
799ef3cb
JH
5884 }
5885 if (is_utf8) {
5886 /* Downgrade not possible - cannot be eq */
5887 return FALSE;
5888 }
5889 }
cf48d248
JH
5890 }
5891
5892 if (cur1 == cur2)
5893 eq = memEQ(pv1, pv2, cur1);
e01b9e88 5894
553e1bcc
AT
5895 if (svrecode)
5896 SvREFCNT_dec(svrecode);
799ef3cb 5897
553e1bcc
AT
5898 if (tpv)
5899 Safefree(tpv);
cf48d248 5900
e01b9e88 5901 return eq;
79072805
LW
5902}
5903
954c1994
GS
5904/*
5905=for apidoc sv_cmp
5906
5907Compares the strings in two SVs. Returns -1, 0, or 1 indicating whether the
5908string in C<sv1> is less than, equal to, or greater than the string in
645c22ef
DM
5909C<sv2>. Is UTF-8 and 'use bytes' aware, handles get magic, and will
5910coerce its args to strings if necessary. See also C<sv_cmp_locale>.
954c1994
GS
5911
5912=cut
5913*/
5914
79072805 5915I32
e01b9e88 5916Perl_sv_cmp(pTHX_ register SV *sv1, register SV *sv2)
79072805 5917{
560a288e 5918 STRLEN cur1, cur2;
553e1bcc 5919 char *pv1, *pv2, *tpv = Nullch;
cf48d248 5920 I32 cmp;
553e1bcc 5921 SV *svrecode = Nullsv;
560a288e 5922
e01b9e88
SC
5923 if (!sv1) {
5924 pv1 = "";
560a288e
GS
5925 cur1 = 0;
5926 }
e01b9e88
SC
5927 else
5928 pv1 = SvPV(sv1, cur1);
560a288e 5929
553e1bcc 5930 if (!sv2) {
e01b9e88 5931 pv2 = "";
560a288e
GS
5932 cur2 = 0;
5933 }
e01b9e88
SC
5934 else
5935 pv2 = SvPV(sv2, cur2);
79072805 5936
cf48d248 5937 if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTES) {
799ef3cb
JH
5938 /* Differing utf8ness.
5939 * Do not UTF8size the comparands as a side-effect. */
cf48d248 5940 if (SvUTF8(sv1)) {
799ef3cb 5941 if (PL_encoding) {
553e1bcc
AT
5942 svrecode = newSVpvn(pv2, cur2);
5943 sv_recode_to_utf8(svrecode, PL_encoding);
5944 pv2 = SvPV(svrecode, cur2);
799ef3cb
JH
5945 }
5946 else {
553e1bcc 5947 pv2 = tpv = (char*)bytes_to_utf8((U8*)pv2, &cur2);
799ef3cb 5948 }
cf48d248
JH
5949 }
5950 else {
799ef3cb 5951 if (PL_encoding) {
553e1bcc
AT
5952 svrecode = newSVpvn(pv1, cur1);
5953 sv_recode_to_utf8(svrecode, PL_encoding);
5954 pv1 = SvPV(svrecode, cur1);
799ef3cb
JH
5955 }
5956 else {
553e1bcc 5957 pv1 = tpv = (char*)bytes_to_utf8((U8*)pv1, &cur1);
799ef3cb 5958 }
cf48d248
JH
5959 }
5960 }
5961
e01b9e88 5962 if (!cur1) {
cf48d248 5963 cmp = cur2 ? -1 : 0;
e01b9e88 5964 } else if (!cur2) {
cf48d248
JH
5965 cmp = 1;
5966 } else {
5967 I32 retval = memcmp((void*)pv1, (void*)pv2, cur1 < cur2 ? cur1 : cur2);
e01b9e88
SC
5968
5969 if (retval) {
cf48d248 5970 cmp = retval < 0 ? -1 : 1;
e01b9e88 5971 } else if (cur1 == cur2) {
cf48d248
JH
5972 cmp = 0;
5973 } else {
5974 cmp = cur1 < cur2 ? -1 : 1;
e01b9e88 5975 }
cf48d248 5976 }
16660edb 5977
553e1bcc
AT
5978 if (svrecode)
5979 SvREFCNT_dec(svrecode);
799ef3cb 5980
553e1bcc
AT
5981 if (tpv)
5982 Safefree(tpv);
cf48d248
JH
5983
5984 return cmp;
bbce6d69 5985}
16660edb 5986
c461cf8f
JH
5987/*
5988=for apidoc sv_cmp_locale
5989
645c22ef
DM
5990Compares the strings in two SVs in a locale-aware manner. Is UTF-8 and
5991'use bytes' aware, handles get magic, and will coerce its args to strings
5992if necessary. See also C<sv_cmp_locale>. See also C<sv_cmp>.
c461cf8f
JH
5993
5994=cut
5995*/
5996
bbce6d69 5997I32
864dbfa3 5998Perl_sv_cmp_locale(pTHX_ register SV *sv1, register SV *sv2)
bbce6d69 5999{
36477c24 6000#ifdef USE_LOCALE_COLLATE
16660edb 6001
bbce6d69 6002 char *pv1, *pv2;
6003 STRLEN len1, len2;
6004 I32 retval;
16660edb 6005
3280af22 6006 if (PL_collation_standard)
bbce6d69 6007 goto raw_compare;
16660edb 6008
bbce6d69 6009 len1 = 0;
8ac85365 6010 pv1 = sv1 ? sv_collxfrm(sv1, &len1) : (char *) NULL;
bbce6d69 6011 len2 = 0;
8ac85365 6012 pv2 = sv2 ? sv_collxfrm(sv2, &len2) : (char *) NULL;
16660edb 6013
bbce6d69 6014 if (!pv1 || !len1) {
6015 if (pv2 && len2)
6016 return -1;
6017 else
6018 goto raw_compare;
6019 }
6020 else {
6021 if (!pv2 || !len2)
6022 return 1;
6023 }
16660edb 6024
bbce6d69 6025 retval = memcmp((void*)pv1, (void*)pv2, len1 < len2 ? len1 : len2);
16660edb 6026
bbce6d69 6027 if (retval)
16660edb 6028 return retval < 0 ? -1 : 1;
6029
bbce6d69 6030 /*
6031 * When the result of collation is equality, that doesn't mean
6032 * that there are no differences -- some locales exclude some
6033 * characters from consideration. So to avoid false equalities,
6034 * we use the raw string as a tiebreaker.
6035 */
16660edb 6036
bbce6d69 6037 raw_compare:
6038 /* FALL THROUGH */
16660edb 6039
36477c24 6040#endif /* USE_LOCALE_COLLATE */
16660edb 6041
bbce6d69 6042 return sv_cmp(sv1, sv2);
6043}
79072805 6044
645c22ef 6045
36477c24 6046#ifdef USE_LOCALE_COLLATE
645c22ef 6047
7a4c00b4 6048/*
645c22ef
DM
6049=for apidoc sv_collxfrm
6050
6051Add Collate Transform magic to an SV if it doesn't already have it.
6052
6053Any scalar variable may carry PERL_MAGIC_collxfrm magic that contains the
6054scalar data of the variable, but transformed to such a format that a normal
6055memory comparison can be used to compare the data according to the locale
6056settings.
6057
6058=cut
6059*/
6060
bbce6d69 6061char *
864dbfa3 6062Perl_sv_collxfrm(pTHX_ SV *sv, STRLEN *nxp)
bbce6d69 6063{
7a4c00b4 6064 MAGIC *mg;
16660edb 6065
14befaf4 6066 mg = SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_collxfrm) : (MAGIC *) NULL;
3280af22 6067 if (!mg || !mg->mg_ptr || *(U32*)mg->mg_ptr != PL_collation_ix) {
bbce6d69 6068 char *s, *xf;
6069 STRLEN len, xlen;
6070
7a4c00b4 6071 if (mg)
6072 Safefree(mg->mg_ptr);
bbce6d69 6073 s = SvPV(sv, len);
6074 if ((xf = mem_collxfrm(s, len, &xlen))) {
ff0cee69 6075 if (SvREADONLY(sv)) {
6076 SAVEFREEPV(xf);
6077 *nxp = xlen;
3280af22 6078 return xf + sizeof(PL_collation_ix);
ff0cee69 6079 }
7a4c00b4 6080 if (! mg) {
14befaf4
DM
6081 sv_magic(sv, 0, PERL_MAGIC_collxfrm, 0, 0);
6082 mg = mg_find(sv, PERL_MAGIC_collxfrm);
7a4c00b4 6083 assert(mg);
bbce6d69 6084 }
7a4c00b4 6085 mg->mg_ptr = xf;
565764a8 6086 mg->mg_len = xlen;
7a4c00b4 6087 }
6088 else {
ff0cee69 6089 if (mg) {
6090 mg->mg_ptr = NULL;
565764a8 6091 mg->mg_len = -1;
ff0cee69 6092 }
bbce6d69 6093 }
6094 }
7a4c00b4 6095 if (mg && mg->mg_ptr) {
565764a8 6096 *nxp = mg->mg_len;
3280af22 6097 return mg->mg_ptr + sizeof(PL_collation_ix);
bbce6d69 6098 }
6099 else {
6100 *nxp = 0;
6101 return NULL;
16660edb 6102 }
79072805
LW
6103}
6104
36477c24 6105#endif /* USE_LOCALE_COLLATE */
bbce6d69 6106
c461cf8f
JH
6107/*
6108=for apidoc sv_gets
6109
6110Get a line from the filehandle and store it into the SV, optionally
6111appending to the currently-stored string.
6112
6113=cut
6114*/
6115
79072805 6116char *
864dbfa3 6117Perl_sv_gets(pTHX_ register SV *sv, register PerlIO *fp, I32 append)
79072805 6118{
c07a80fd 6119 char *rsptr;
6120 STRLEN rslen;
6121 register STDCHAR rslast;
6122 register STDCHAR *bp;
6123 register I32 cnt;
9c5ffd7c 6124 I32 i = 0;
8bfdd7d9 6125 I32 rspara = 0;
fe1d0b35 6126 I32 recsize;
c07a80fd 6127
1624793d
NC
6128 if (SvTHINKFIRST(sv))
6129 sv_force_normal_flags(sv, append ? 0 : SV_COW_DROP_PV);
6130 /* XXX. If you make this PVIV, then copy on write can copy scalars read
6131 from <>.
6132 However, perlbench says it's slower, because the existing swipe code
6133 is faster than copy on write.
6134 Swings and roundabouts. */
6fc92669 6135 (void)SvUPGRADE(sv, SVt_PV);
99491443 6136
ff68c719 6137 SvSCREAM_off(sv);
251c53ad
AE
6138
6139 if (append) {
6140 if (PerlIO_isutf8(fp)) {
6141 if (!SvUTF8(sv)) {
6142 sv_utf8_upgrade_nomg(sv);
6143 sv_pos_u2b(sv,&append,0);
6144 }
6145 } else if (SvUTF8(sv)) {
6146 SV *tsv = NEWSV(0,0);
6147 sv_gets(tsv, fp, 0);
6148 sv_utf8_upgrade_nomg(tsv);
6149 SvCUR_set(sv,append);
6150 sv_catsv(sv,tsv);
6151 sv_free(tsv);
6152 goto return_string_or_null;
6153 }
6154 }
6155
6156 SvPOK_only(sv);
6157 if (PerlIO_isutf8(fp))
6158 SvUTF8_on(sv);
c07a80fd 6159
ef7b71f0 6160 if (IN_PERL_COMPILETIME) {
8bfdd7d9
HS
6161 /* we always read code in line mode */
6162 rsptr = "\n";
6163 rslen = 1;
6164 }
6165 else if (RsSNARF(PL_rs)) {
338501c1
JH
6166 /* If it is a regular disk file use size from stat() as estimate
6167 of amount we are going to read - may result in malloc-ing
6168 more memory than we realy need if layers bellow reduce
6169 size we read (e.g. CRLF or a gzip layer)
6170 */
fe1d0b35 6171 Stat_t st;
338501c1
JH
6172 if (!PerlLIO_fstat(PerlIO_fileno(fp), &st) && S_ISREG(st.st_mode)) {
6173 Off_t offset = PerlIO_tell(fp);
bfd7eeef 6174 if (offset != (Off_t) -1 && st.st_size + append > offset) {
338501c1
JH
6175 (void) SvGROW(sv, (STRLEN)((st.st_size - offset) + append + 1));
6176 }
6177 }
c07a80fd 6178 rsptr = NULL;
6179 rslen = 0;
6180 }
3280af22 6181 else if (RsRECORD(PL_rs)) {
fe1d0b35 6182 I32 bytesread;
5b2b9c68
HM
6183 char *buffer;
6184
6185 /* Grab the size of the record we're getting */
3280af22 6186 recsize = SvIV(SvRV(PL_rs));
fe1d0b35 6187 buffer = SvGROW(sv, (STRLEN)(recsize + append + 1)) + append;
5b2b9c68
HM
6188 /* Go yank in */
6189#ifdef VMS
6190 /* VMS wants read instead of fread, because fread doesn't respect */
6191 /* RMS record boundaries. This is not necessarily a good thing to be */
338501c1
JH
6192 /* doing, but we've got no other real choice - except avoid stdio
6193 as implementation - perhaps write a :vms layer ?
6194 */
5b2b9c68
HM
6195 bytesread = PerlLIO_read(PerlIO_fileno(fp), buffer, recsize);
6196#else
6197 bytesread = PerlIO_read(fp, buffer, recsize);
6198#endif
e3b9cd39
JH
6199 if (bytesread < 0)
6200 bytesread = 0;
fe1d0b35 6201 SvCUR_set(sv, bytesread += append);
e670df4e 6202 buffer[bytesread] = '\0';
251c53ad 6203 goto return_string_or_null;
5b2b9c68 6204 }
3280af22 6205 else if (RsPARA(PL_rs)) {
c07a80fd 6206 rsptr = "\n\n";
6207 rslen = 2;
8bfdd7d9 6208 rspara = 1;
c07a80fd 6209 }
7d59b7e4
NIS
6210 else {
6211 /* Get $/ i.e. PL_rs into same encoding as stream wants */
6212 if (PerlIO_isutf8(fp)) {
6213 rsptr = SvPVutf8(PL_rs, rslen);
6214 }
6215 else {
6216 if (SvUTF8(PL_rs)) {
6217 if (!sv_utf8_downgrade(PL_rs, TRUE)) {
6218 Perl_croak(aTHX_ "Wide character in $/");
6219 }
6220 }
6221 rsptr = SvPV(PL_rs, rslen);
6222 }
6223 }
6224
c07a80fd 6225 rslast = rslen ? rsptr[rslen - 1] : '\0';
6226
8bfdd7d9 6227 if (rspara) { /* have to do this both before and after */
79072805 6228 do { /* to make sure file boundaries work right */
760ac839 6229 if (PerlIO_eof(fp))
a0d0e21e 6230 return 0;
760ac839 6231 i = PerlIO_getc(fp);
79072805 6232 if (i != '\n') {
a0d0e21e
LW
6233 if (i == -1)
6234 return 0;
760ac839 6235 PerlIO_ungetc(fp,i);
79072805
LW
6236 break;
6237 }
6238 } while (i != EOF);
6239 }
c07a80fd 6240
760ac839
LW
6241 /* See if we know enough about I/O mechanism to cheat it ! */
6242
6243 /* This used to be #ifdef test - it is made run-time test for ease
1c846c1f 6244 of abstracting out stdio interface. One call should be cheap
760ac839
LW
6245 enough here - and may even be a macro allowing compile
6246 time optimization.
6247 */
6248
6249 if (PerlIO_fast_gets(fp)) {
6250
6251 /*
6252 * We're going to steal some values from the stdio struct
6253 * and put EVERYTHING in the innermost loop into registers.
6254 */
6255 register STDCHAR *ptr;
6256 STRLEN bpx;
6257 I32 shortbuffered;
6258
16660edb 6259#if defined(VMS) && defined(PERLIO_IS_STDIO)
6260 /* An ungetc()d char is handled separately from the regular
6261 * buffer, so we getc() it back out and stuff it in the buffer.
6262 */
6263 i = PerlIO_getc(fp);
6264 if (i == EOF) return 0;
6265 *(--((*fp)->_ptr)) = (unsigned char) i;
6266 (*fp)->_cnt++;
6267#endif
c07a80fd 6268
c2960299 6269 /* Here is some breathtakingly efficient cheating */
c07a80fd 6270
a20bf0c3 6271 cnt = PerlIO_get_cnt(fp); /* get count into register */
338501c1
JH
6272 /* make sure we have the room */
6273 if ((I32)(SvLEN(sv) - append) <= cnt + 1) {
6274 /* Not room for all of it
6275 if we are looking for a separator and room for some
6276 */
6277 if (rslen && cnt > 80 && (I32)SvLEN(sv) > append) {
6278 /* just process what we have room for */
79072805
LW
6279 shortbuffered = cnt - SvLEN(sv) + append + 1;
6280 cnt -= shortbuffered;
6281 }
6282 else {
6283 shortbuffered = 0;
bbce6d69 6284 /* remember that cnt can be negative */
eb160463 6285 SvGROW(sv, (STRLEN)(append + (cnt <= 0 ? 2 : (cnt + 1))));
79072805
LW
6286 }
6287 }
338501c1 6288 else
79072805 6289 shortbuffered = 0;
c07a80fd 6290 bp = (STDCHAR*)SvPVX(sv) + append; /* move these two too to registers */
a20bf0c3 6291 ptr = (STDCHAR*)PerlIO_get_ptr(fp);
16660edb 6292 DEBUG_P(PerlIO_printf(Perl_debug_log,
1d7c1841 6293 "Screamer: entering, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt));
16660edb 6294 DEBUG_P(PerlIO_printf(Perl_debug_log,
ba7abf9d 6295 "Screamer: entering: PerlIO * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n",
1c846c1f 6296 PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp),
1d7c1841 6297 PTR2UV(PerlIO_has_base(fp) ? PerlIO_get_base(fp) : 0)));
79072805
LW
6298 for (;;) {
6299 screamer:
93a17b20 6300 if (cnt > 0) {
c07a80fd 6301 if (rslen) {
760ac839
LW
6302 while (cnt > 0) { /* this | eat */
6303 cnt--;
c07a80fd 6304 if ((*bp++ = *ptr++) == rslast) /* really | dust */
6305 goto thats_all_folks; /* screams | sed :-) */
6306 }
6307 }
6308 else {
1c846c1f
NIS
6309 Copy(ptr, bp, cnt, char); /* this | eat */
6310 bp += cnt; /* screams | dust */
c07a80fd 6311 ptr += cnt; /* louder | sed :-) */
a5f75d66 6312 cnt = 0;
93a17b20 6313 }
79072805
LW
6314 }
6315
748a9306 6316 if (shortbuffered) { /* oh well, must extend */
79072805
LW
6317 cnt = shortbuffered;
6318 shortbuffered = 0;
c07a80fd 6319 bpx = bp - (STDCHAR*)SvPVX(sv); /* box up before relocation */
79072805
LW
6320 SvCUR_set(sv, bpx);
6321 SvGROW(sv, SvLEN(sv) + append + cnt + 2);
c07a80fd 6322 bp = (STDCHAR*)SvPVX(sv) + bpx; /* unbox after relocation */
79072805
LW
6323 continue;
6324 }
6325
16660edb 6326 DEBUG_P(PerlIO_printf(Perl_debug_log,
1d7c1841
GS
6327 "Screamer: going to getc, ptr=%"UVuf", cnt=%ld\n",
6328 PTR2UV(ptr),(long)cnt));
cc00df79 6329 PerlIO_set_ptrcnt(fp, (STDCHAR*)ptr, cnt); /* deregisterize cnt and ptr */
ba7abf9d 6330#if 0
16660edb 6331 DEBUG_P(PerlIO_printf(Perl_debug_log,
1d7c1841 6332 "Screamer: pre: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n",
1c846c1f 6333 PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp),
1d7c1841 6334 PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
ba7abf9d 6335#endif
1c846c1f 6336 /* This used to call 'filbuf' in stdio form, but as that behaves like
774d564b 6337 getc when cnt <= 0 we use PerlIO_getc here to avoid introducing
6338 another abstraction. */
760ac839 6339 i = PerlIO_getc(fp); /* get more characters */
ba7abf9d 6340#if 0
16660edb 6341 DEBUG_P(PerlIO_printf(Perl_debug_log,
1d7c1841 6342 "Screamer: post: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n",
1c846c1f 6343 PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp),
1d7c1841 6344 PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
ba7abf9d 6345#endif
a20bf0c3
JH
6346 cnt = PerlIO_get_cnt(fp);
6347 ptr = (STDCHAR*)PerlIO_get_ptr(fp); /* reregisterize cnt and ptr */
16660edb 6348 DEBUG_P(PerlIO_printf(Perl_debug_log,
1d7c1841 6349 "Screamer: after getc, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt));
79072805 6350
748a9306
LW
6351 if (i == EOF) /* all done for ever? */
6352 goto thats_really_all_folks;
6353
c07a80fd 6354 bpx = bp - (STDCHAR*)SvPVX(sv); /* box up before relocation */
79072805
LW
6355 SvCUR_set(sv, bpx);
6356 SvGROW(sv, bpx + cnt + 2);
c07a80fd 6357 bp = (STDCHAR*)SvPVX(sv) + bpx; /* unbox after relocation */
6358
eb160463 6359 *bp++ = (STDCHAR)i; /* store character from PerlIO_getc */
79072805 6360
c07a80fd 6361 if (rslen && (STDCHAR)i == rslast) /* all done for now? */
79072805 6362 goto thats_all_folks;
79072805
LW
6363 }
6364
6365thats_all_folks:
eb160463 6366 if ((rslen > 1 && (STRLEN)(bp - (STDCHAR*)SvPVX(sv)) < rslen) ||
36477c24 6367 memNE((char*)bp - rslen, rsptr, rslen))
760ac839 6368 goto screamer; /* go back to the fray */
79072805
LW
6369thats_really_all_folks:
6370 if (shortbuffered)
6371 cnt += shortbuffered;
16660edb 6372 DEBUG_P(PerlIO_printf(Perl_debug_log,
1d7c1841 6373 "Screamer: quitting, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt));
cc00df79 6374 PerlIO_set_ptrcnt(fp, (STDCHAR*)ptr, cnt); /* put these back or we're in trouble */
16660edb 6375 DEBUG_P(PerlIO_printf(Perl_debug_log,
1d7c1841 6376 "Screamer: end: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n",
1c846c1f 6377 PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp),
1d7c1841 6378 PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
79072805 6379 *bp = '\0';
760ac839 6380 SvCUR_set(sv, bp - (STDCHAR*)SvPVX(sv)); /* set length */
16660edb 6381 DEBUG_P(PerlIO_printf(Perl_debug_log,
fb73857a 6382 "Screamer: done, len=%ld, string=|%.*s|\n",
6383 (long)SvCUR(sv),(int)SvCUR(sv),SvPVX(sv)));
760ac839
LW
6384 }
6385 else
79072805 6386 {
2ee2a917
JH
6387 /*The big, slow, and stupid way. */
6388
6389 /* Any stack-challenged places. */
f73bace3 6390#if defined(EPOC)
2ee2a917
JH
6391 /* EPOC: need to work around SDK features. *
6392 * On WINS: MS VC5 generates calls to _chkstk, *
6393 * if a "large" stack frame is allocated. *
6394 * gcc on MARM does not generate calls like these. */
6395# define USEHEAPINSTEADOFSTACK
6396#endif
6397
6398#ifdef USEHEAPINSTEADOFSTACK
6399 STDCHAR *buf = 0;
6400 New(0, buf, 8192, STDCHAR);
6401 assert(buf);
4d2c4e07 6402#else
2ee2a917 6403 STDCHAR buf[8192];
4d2c4e07 6404#endif
79072805 6405
760ac839 6406screamer2:
c07a80fd 6407 if (rslen) {
760ac839
LW
6408 register STDCHAR *bpe = buf + sizeof(buf);
6409 bp = buf;
eb160463 6410 while ((i = PerlIO_getc(fp)) != EOF && (*bp++ = (STDCHAR)i) != rslast && bp < bpe)
760ac839
LW
6411 ; /* keep reading */
6412 cnt = bp - buf;
c07a80fd 6413 }
6414 else {
760ac839 6415 cnt = PerlIO_read(fp,(char*)buf, sizeof(buf));
16660edb 6416 /* Accomodate broken VAXC compiler, which applies U8 cast to
6417 * both args of ?: operator, causing EOF to change into 255
6418 */
37be0adf 6419 if (cnt > 0)
cbe9e203
JH
6420 i = (U8)buf[cnt - 1];
6421 else
37be0adf 6422 i = EOF;
c07a80fd 6423 }
79072805 6424
cbe9e203
JH
6425 if (cnt < 0)
6426 cnt = 0; /* we do need to re-set the sv even when cnt <= 0 */
6427 if (append)
6428 sv_catpvn(sv, (char *) buf, cnt);
6429 else
6430 sv_setpvn(sv, (char *) buf, cnt);
c07a80fd 6431
6432 if (i != EOF && /* joy */
6433 (!rslen ||
6434 SvCUR(sv) < rslen ||
36477c24 6435 memNE(SvPVX(sv) + SvCUR(sv) - rslen, rsptr, rslen)))
79072805
LW
6436 {
6437 append = -1;
63e4d877
CS
6438 /*
6439 * If we're reading from a TTY and we get a short read,
6440 * indicating that the user hit his EOF character, we need
6441 * to notice it now, because if we try to read from the TTY
6442 * again, the EOF condition will disappear.
6443 *
6444 * The comparison of cnt to sizeof(buf) is an optimization
6445 * that prevents unnecessary calls to feof().
6446 *
6447 * - jik 9/25/96
6448 */
6449 if (!(cnt < sizeof(buf) && PerlIO_eof(fp)))
6450 goto screamer2;
79072805 6451 }
2ee2a917
JH
6452
6453#ifdef USEHEAPINSTEADOFSTACK
6454 Safefree(buf);
6455#endif
79072805
LW
6456 }
6457
8bfdd7d9 6458 if (rspara) { /* have to do this both before and after */
c07a80fd 6459 while (i != EOF) { /* to make sure file boundaries work right */
760ac839 6460 i = PerlIO_getc(fp);
79072805 6461 if (i != '\n') {
760ac839 6462 PerlIO_ungetc(fp,i);
79072805
LW
6463 break;
6464 }
6465 }
6466 }
c07a80fd 6467
251c53ad 6468return_string_or_null:
c07a80fd 6469 return (SvCUR(sv) - append) ? SvPVX(sv) : Nullch;
79072805
LW
6470}
6471
954c1994
GS
6472/*
6473=for apidoc sv_inc
6474
645c22ef
DM
6475Auto-increment of the value in the SV, doing string to numeric conversion
6476if necessary. Handles 'get' magic.
954c1994
GS
6477
6478=cut
6479*/
6480
79072805 6481void
864dbfa3 6482Perl_sv_inc(pTHX_ register SV *sv)
79072805
LW
6483{
6484 register char *d;
463ee0b2 6485 int flags;
79072805
LW
6486
6487 if (!sv)
6488 return;
b23a5f78
GB
6489 if (SvGMAGICAL(sv))
6490 mg_get(sv);
ed6116ce 6491 if (SvTHINKFIRST(sv)) {
3510b4a1
NC
6492 if (SvREADONLY(sv) && SvFAKE(sv))
6493 sv_force_normal(sv);
0f15f207 6494 if (SvREADONLY(sv)) {
ef7b71f0 6495 if (IN_PERL_RUNTIME)
cea2e8a9 6496 Perl_croak(aTHX_ PL_no_modify);
0f15f207 6497 }
a0d0e21e 6498 if (SvROK(sv)) {
b5be31e9 6499 IV i;
9e7bc3e8
JD
6500 if (SvAMAGIC(sv) && AMG_CALLun(sv,inc))
6501 return;
56431972 6502 i = PTR2IV(SvRV(sv));
b5be31e9
SM
6503 sv_unref(sv);
6504 sv_setiv(sv, i);
a0d0e21e 6505 }
ed6116ce 6506 }
8990e307 6507 flags = SvFLAGS(sv);
28e5dec8
JH
6508 if ((flags & (SVp_NOK|SVp_IOK)) == SVp_NOK) {
6509 /* It's (privately or publicly) a float, but not tested as an
6510 integer, so test it to see. */
d460ef45 6511 (void) SvIV(sv);
28e5dec8
JH
6512 flags = SvFLAGS(sv);
6513 }
6514 if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
6515 /* It's publicly an integer, or privately an integer-not-float */
59d8ce62 6516#ifdef PERL_PRESERVE_IVUV
28e5dec8 6517 oops_its_int:
59d8ce62 6518#endif
25da4f38
IZ
6519 if (SvIsUV(sv)) {
6520 if (SvUVX(sv) == UV_MAX)
a1e868e7 6521 sv_setnv(sv, UV_MAX_P1);
25da4f38
IZ
6522 else
6523 (void)SvIOK_only_UV(sv);
6524 ++SvUVX(sv);
6525 } else {
6526 if (SvIVX(sv) == IV_MAX)
28e5dec8 6527 sv_setuv(sv, (UV)IV_MAX + 1);
25da4f38
IZ
6528 else {
6529 (void)SvIOK_only(sv);
6530 ++SvIVX(sv);
1c846c1f 6531 }
55497cff 6532 }
79072805
LW
6533 return;
6534 }
28e5dec8
JH
6535 if (flags & SVp_NOK) {
6536 (void)SvNOK_only(sv);
6537 SvNVX(sv) += 1.0;
6538 return;
6539 }
6540
8990e307 6541 if (!(flags & SVp_POK) || !*SvPVX(sv)) {
28e5dec8
JH
6542 if ((flags & SVTYPEMASK) < SVt_PVIV)
6543 sv_upgrade(sv, SVt_IV);
6544 (void)SvIOK_only(sv);
6545 SvIVX(sv) = 1;
79072805
LW
6546 return;
6547 }
463ee0b2 6548 d = SvPVX(sv);
79072805
LW
6549 while (isALPHA(*d)) d++;
6550 while (isDIGIT(*d)) d++;
6551 if (*d) {
28e5dec8 6552#ifdef PERL_PRESERVE_IVUV
d1be9408 6553 /* Got to punt this as an integer if needs be, but we don't issue
28e5dec8
JH
6554 warnings. Probably ought to make the sv_iv_please() that does
6555 the conversion if possible, and silently. */
c2988b20 6556 int numtype = grok_number(SvPVX(sv), SvCUR(sv), NULL);
28e5dec8
JH
6557 if (numtype && !(numtype & IS_NUMBER_INFINITY)) {
6558 /* Need to try really hard to see if it's an integer.
6559 9.22337203685478e+18 is an integer.
6560 but "9.22337203685478e+18" + 0 is UV=9223372036854779904
6561 so $a="9.22337203685478e+18"; $a+0; $a++
6562 needs to be the same as $a="9.22337203685478e+18"; $a++
6563 or we go insane. */
d460ef45 6564
28e5dec8
JH
6565 (void) sv_2iv(sv);
6566 if (SvIOK(sv))
6567 goto oops_its_int;
6568
6569 /* sv_2iv *should* have made this an NV */
6570 if (flags & SVp_NOK) {
6571 (void)SvNOK_only(sv);
6572 SvNVX(sv) += 1.0;
6573 return;
6574 }
6575 /* I don't think we can get here. Maybe I should assert this
6576 And if we do get here I suspect that sv_setnv will croak. NWC
6577 Fall through. */
6578#if defined(USE_LONG_DOUBLE)
6579 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_inc punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%"PERL_PRIgldbl"\n",
6580 SvPVX(sv), SvIVX(sv), SvNVX(sv)));
6581#else
1779d84d 6582 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_inc punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%"NVgf"\n",
28e5dec8
JH
6583 SvPVX(sv), SvIVX(sv), SvNVX(sv)));
6584#endif
6585 }
6586#endif /* PERL_PRESERVE_IVUV */
6587 sv_setnv(sv,Atof(SvPVX(sv)) + 1.0);
79072805
LW
6588 return;
6589 }
6590 d--;
463ee0b2 6591 while (d >= SvPVX(sv)) {
79072805
LW
6592 if (isDIGIT(*d)) {
6593 if (++*d <= '9')
6594 return;
6595 *(d--) = '0';
6596 }
6597 else {
9d116dd7
JH
6598#ifdef EBCDIC
6599 /* MKS: The original code here died if letters weren't consecutive.
6600 * at least it didn't have to worry about non-C locales. The
6601 * new code assumes that ('z'-'a')==('Z'-'A'), letters are
1c846c1f 6602 * arranged in order (although not consecutively) and that only
9d116dd7
JH
6603 * [A-Za-z] are accepted by isALPHA in the C locale.
6604 */
6605 if (*d != 'z' && *d != 'Z') {
6606 do { ++*d; } while (!isALPHA(*d));
6607 return;
6608 }
6609 *(d--) -= 'z' - 'a';
6610#else
79072805
LW
6611 ++*d;
6612 if (isALPHA(*d))
6613 return;
6614 *(d--) -= 'z' - 'a' + 1;
9d116dd7 6615#endif
79072805
LW
6616 }
6617 }
6618 /* oh,oh, the number grew */
6619 SvGROW(sv, SvCUR(sv) + 2);
6620 SvCUR(sv)++;
463ee0b2 6621 for (d = SvPVX(sv) + SvCUR(sv); d > SvPVX(sv); d--)
79072805
LW
6622 *d = d[-1];
6623 if (isDIGIT(d[1]))
6624 *d = '1';
6625 else
6626 *d = d[1];
6627}
6628
954c1994
GS
6629/*
6630=for apidoc sv_dec
6631
645c22ef
DM
6632Auto-decrement of the value in the SV, doing string to numeric conversion
6633if necessary. Handles 'get' magic.
954c1994
GS
6634
6635=cut
6636*/
6637
79072805 6638void
864dbfa3 6639Perl_sv_dec(pTHX_ register SV *sv)
79072805 6640{
463ee0b2
LW
6641 int flags;
6642
79072805
LW
6643 if (!sv)
6644 return;
b23a5f78
GB
6645 if (SvGMAGICAL(sv))
6646 mg_get(sv);
ed6116ce 6647 if (SvTHINKFIRST(sv)) {
3510b4a1
NC
6648 if (SvREADONLY(sv) && SvFAKE(sv))
6649 sv_force_normal(sv);
0f15f207 6650 if (SvREADONLY(sv)) {
ef7b71f0 6651 if (IN_PERL_RUNTIME)
cea2e8a9 6652 Perl_croak(aTHX_ PL_no_modify);
0f15f207 6653 }
a0d0e21e 6654 if (SvROK(sv)) {
b5be31e9 6655 IV i;
9e7bc3e8
JD
6656 if (SvAMAGIC(sv) && AMG_CALLun(sv,dec))
6657 return;
56431972 6658 i = PTR2IV(SvRV(sv));
b5be31e9
SM
6659 sv_unref(sv);
6660 sv_setiv(sv, i);
a0d0e21e 6661 }
ed6116ce 6662 }
28e5dec8
JH
6663 /* Unlike sv_inc we don't have to worry about string-never-numbers
6664 and keeping them magic. But we mustn't warn on punting */
8990e307 6665 flags = SvFLAGS(sv);
28e5dec8
JH
6666 if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
6667 /* It's publicly an integer, or privately an integer-not-float */
59d8ce62 6668#ifdef PERL_PRESERVE_IVUV
28e5dec8 6669 oops_its_int:
59d8ce62 6670#endif
25da4f38
IZ
6671 if (SvIsUV(sv)) {
6672 if (SvUVX(sv) == 0) {
6673 (void)SvIOK_only(sv);
6674 SvIVX(sv) = -1;
6675 }
6676 else {
6677 (void)SvIOK_only_UV(sv);
6678 --SvUVX(sv);
1c846c1f 6679 }
25da4f38
IZ
6680 } else {
6681 if (SvIVX(sv) == IV_MIN)
65202027 6682 sv_setnv(sv, (NV)IV_MIN - 1.0);
25da4f38
IZ
6683 else {
6684 (void)SvIOK_only(sv);
6685 --SvIVX(sv);
1c846c1f 6686 }
55497cff 6687 }
6688 return;
6689 }
28e5dec8
JH
6690 if (flags & SVp_NOK) {
6691 SvNVX(sv) -= 1.0;
6692 (void)SvNOK_only(sv);
6693 return;
6694 }
8990e307 6695 if (!(flags & SVp_POK)) {
4633a7c4
LW
6696 if ((flags & SVTYPEMASK) < SVt_PVNV)
6697 sv_upgrade(sv, SVt_NV);
463ee0b2 6698 SvNVX(sv) = -1.0;
a0d0e21e 6699 (void)SvNOK_only(sv);
79072805
LW
6700 return;
6701 }
28e5dec8
JH
6702#ifdef PERL_PRESERVE_IVUV
6703 {
c2988b20 6704 int numtype = grok_number(SvPVX(sv), SvCUR(sv), NULL);
28e5dec8
JH
6705 if (numtype && !(numtype & IS_NUMBER_INFINITY)) {
6706 /* Need to try really hard to see if it's an integer.
6707 9.22337203685478e+18 is an integer.
6708 but "9.22337203685478e+18" + 0 is UV=9223372036854779904
6709 so $a="9.22337203685478e+18"; $a+0; $a--
6710 needs to be the same as $a="9.22337203685478e+18"; $a--
6711 or we go insane. */
d460ef45 6712
28e5dec8
JH
6713 (void) sv_2iv(sv);
6714 if (SvIOK(sv))
6715 goto oops_its_int;
6716
6717 /* sv_2iv *should* have made this an NV */
6718 if (flags & SVp_NOK) {
6719 (void)SvNOK_only(sv);
6720 SvNVX(sv) -= 1.0;
6721 return;
6722 }
6723 /* I don't think we can get here. Maybe I should assert this
6724 And if we do get here I suspect that sv_setnv will croak. NWC
6725 Fall through. */
6726#if defined(USE_LONG_DOUBLE)
6727 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_dec punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%"PERL_PRIgldbl"\n",
6728 SvPVX(sv), SvIVX(sv), SvNVX(sv)));
6729#else
1779d84d 6730 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_dec punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%"NVgf"\n",
28e5dec8
JH
6731 SvPVX(sv), SvIVX(sv), SvNVX(sv)));
6732#endif
6733 }
6734 }
6735#endif /* PERL_PRESERVE_IVUV */
097ee67d 6736 sv_setnv(sv,Atof(SvPVX(sv)) - 1.0); /* punt */
79072805
LW
6737}
6738
954c1994
GS
6739/*
6740=for apidoc sv_mortalcopy
6741
645c22ef 6742Creates a new SV which is a copy of the original SV (using C<sv_setsv>).
d4236ebc
DM
6743The new SV is marked as mortal. It will be destroyed "soon", either by an
6744explicit call to FREETMPS, or by an implicit call at places such as
6745statement boundaries. See also C<sv_newmortal> and C<sv_2mortal>.
954c1994
GS
6746
6747=cut
6748*/
6749
79072805
LW
6750/* Make a string that will exist for the duration of the expression
6751 * evaluation. Actually, it may have to last longer than that, but
6752 * hopefully we won't free it until it has been assigned to a
6753 * permanent location. */
6754
6755SV *
864dbfa3 6756Perl_sv_mortalcopy(pTHX_ SV *oldstr)
79072805 6757{
463ee0b2 6758 register SV *sv;
b881518d 6759
4561caa4 6760 new_SV(sv);
79072805 6761 sv_setsv(sv,oldstr);
677b06e3
GS
6762 EXTEND_MORTAL(1);
6763 PL_tmps_stack[++PL_tmps_ix] = sv;
8990e307
LW
6764 SvTEMP_on(sv);
6765 return sv;
6766}
6767
954c1994
GS
6768/*
6769=for apidoc sv_newmortal
6770
645c22ef 6771Creates a new null SV which is mortal. The reference count of the SV is
d4236ebc
DM
6772set to 1. It will be destroyed "soon", either by an explicit call to
6773FREETMPS, or by an implicit call at places such as statement boundaries.
6774See also C<sv_mortalcopy> and C<sv_2mortal>.
954c1994
GS
6775
6776=cut
6777*/
6778
8990e307 6779SV *
864dbfa3 6780Perl_sv_newmortal(pTHX)
8990e307
LW
6781{
6782 register SV *sv;
6783
4561caa4 6784 new_SV(sv);
8990e307 6785 SvFLAGS(sv) = SVs_TEMP;
677b06e3
GS
6786 EXTEND_MORTAL(1);
6787 PL_tmps_stack[++PL_tmps_ix] = sv;
79072805
LW
6788 return sv;
6789}
6790
954c1994
GS
6791/*
6792=for apidoc sv_2mortal
6793
d4236ebc
DM
6794Marks an existing SV as mortal. The SV will be destroyed "soon", either
6795by an explicit call to FREETMPS, or by an implicit call at places such as
6796statement boundaries. See also C<sv_newmortal> and C<sv_mortalcopy>.
954c1994
GS
6797
6798=cut
6799*/
6800
79072805 6801SV *
864dbfa3 6802Perl_sv_2mortal(pTHX_ register SV *sv)
79072805
LW
6803{
6804 if (!sv)
6805 return sv;
d689ffdd 6806 if (SvREADONLY(sv) && SvIMMORTAL(sv))
11162842 6807 return sv;
677b06e3
GS
6808 EXTEND_MORTAL(1);
6809 PL_tmps_stack[++PL_tmps_ix] = sv;
8990e307 6810 SvTEMP_on(sv);
79072805
LW
6811 return sv;
6812}
6813
954c1994
GS
6814/*
6815=for apidoc newSVpv
6816
6817Creates a new SV and copies a string into it. The reference count for the
6818SV is set to 1. If C<len> is zero, Perl will compute the length using
6819strlen(). For efficiency, consider using C<newSVpvn> instead.
6820
6821=cut
6822*/
6823
79072805 6824SV *
864dbfa3 6825Perl_newSVpv(pTHX_ const char *s, STRLEN len)
79072805 6826{
463ee0b2 6827 register SV *sv;
79072805 6828
4561caa4 6829 new_SV(sv);
79072805
LW
6830 if (!len)
6831 len = strlen(s);
6832 sv_setpvn(sv,s,len);
6833 return sv;
6834}
6835
954c1994
GS
6836/*
6837=for apidoc newSVpvn
6838
6839Creates a new SV and copies a string into it. The reference count for the
1c846c1f 6840SV is set to 1. Note that if C<len> is zero, Perl will create a zero length
954c1994 6841string. You are responsible for ensuring that the source string is at least
611e9550 6842C<len> bytes long. If the C<s> argument is NULL the new SV will be undefined.
954c1994
GS
6843
6844=cut
6845*/
6846
9da1e3b5 6847SV *
864dbfa3 6848Perl_newSVpvn(pTHX_ const char *s, STRLEN len)
9da1e3b5
MUN
6849{
6850 register SV *sv;
6851
6852 new_SV(sv);
9da1e3b5
MUN
6853 sv_setpvn(sv,s,len);
6854 return sv;
6855}
6856
1c846c1f
NIS
6857/*
6858=for apidoc newSVpvn_share
6859
645c22ef
DM
6860Creates a new SV with its SvPVX pointing to a shared string in the string
6861table. If the string does not already exist in the table, it is created
6862first. Turns on READONLY and FAKE. The string's hash is stored in the UV
6863slot of the SV; if the C<hash> parameter is non-zero, that value is used;
6864otherwise the hash is computed. The idea here is that as the string table
6865is used for shared hash keys these strings will have SvPVX == HeKEY and
6866hash lookup will avoid string compare.
1c846c1f
NIS
6867
6868=cut
6869*/
6870
6871SV *
c3654f1a 6872Perl_newSVpvn_share(pTHX_ const char *src, I32 len, U32 hash)
1c846c1f
NIS
6873{
6874 register SV *sv;
c3654f1a
IH
6875 bool is_utf8 = FALSE;
6876 if (len < 0) {
77caf834 6877 STRLEN tmplen = -len;
c3654f1a 6878 is_utf8 = TRUE;
75a54232
JH
6879 /* See the note in hv.c:hv_fetch() --jhi */
6880 src = (char*)bytes_from_utf8((U8*)src, &tmplen, &is_utf8);
6881 len = tmplen;
6882 }
1c846c1f 6883 if (!hash)
5afd6d42 6884 PERL_HASH(hash, src, len);
1c846c1f
NIS
6885 new_SV(sv);
6886 sv_upgrade(sv, SVt_PVIV);
c3654f1a 6887 SvPVX(sv) = sharepvn(src, is_utf8?-len:len, hash);
1c846c1f
NIS
6888 SvCUR(sv) = len;
6889 SvUVX(sv) = hash;
6890 SvLEN(sv) = 0;
6891 SvREADONLY_on(sv);
6892 SvFAKE_on(sv);
6893 SvPOK_on(sv);
c3654f1a
IH
6894 if (is_utf8)
6895 SvUTF8_on(sv);
1c846c1f
NIS
6896 return sv;
6897}
6898
645c22ef 6899
cea2e8a9 6900#if defined(PERL_IMPLICIT_CONTEXT)
645c22ef
DM
6901
6902/* pTHX_ magic can't cope with varargs, so this is a no-context
6903 * version of the main function, (which may itself be aliased to us).
6904 * Don't access this version directly.
6905 */
6906
46fc3d4c 6907SV *
cea2e8a9 6908Perl_newSVpvf_nocontext(const char* pat, ...)
46fc3d4c 6909{
cea2e8a9 6910 dTHX;
46fc3d4c 6911 register SV *sv;
6912 va_list args;
46fc3d4c 6913 va_start(args, pat);
c5be433b 6914 sv = vnewSVpvf(pat, &args);
46fc3d4c 6915 va_end(args);
6916 return sv;
6917}
cea2e8a9 6918#endif
46fc3d4c 6919
954c1994
GS
6920/*
6921=for apidoc newSVpvf
6922
645c22ef 6923Creates a new SV and initializes it with the string formatted like
954c1994
GS
6924C<sprintf>.
6925
6926=cut
6927*/
6928
cea2e8a9
GS
6929SV *
6930Perl_newSVpvf(pTHX_ const char* pat, ...)
6931{
6932 register SV *sv;
6933 va_list args;
cea2e8a9 6934 va_start(args, pat);
c5be433b 6935 sv = vnewSVpvf(pat, &args);
cea2e8a9
GS
6936 va_end(args);
6937 return sv;
6938}
46fc3d4c 6939
645c22ef
DM
6940/* backend for newSVpvf() and newSVpvf_nocontext() */
6941
79072805 6942SV *
c5be433b
GS
6943Perl_vnewSVpvf(pTHX_ const char* pat, va_list* args)
6944{
6945 register SV *sv;
6946 new_SV(sv);
6947 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
6948 return sv;
6949}
6950
954c1994
GS
6951/*
6952=for apidoc newSVnv
6953
6954Creates a new SV and copies a floating point value into it.
6955The reference count for the SV is set to 1.
6956
6957=cut
6958*/
6959
c5be433b 6960SV *
65202027 6961Perl_newSVnv(pTHX_ NV n)
79072805 6962{
463ee0b2 6963 register SV *sv;
79072805 6964
4561caa4 6965 new_SV(sv);
79072805
LW
6966 sv_setnv(sv,n);
6967 return sv;
6968}
6969
954c1994
GS
6970/*
6971=for apidoc newSViv
6972
6973Creates a new SV and copies an integer into it. The reference count for the
6974SV is set to 1.
6975
6976=cut
6977*/
6978
79072805 6979SV *
864dbfa3 6980Perl_newSViv(pTHX_ IV i)
79072805 6981{
463ee0b2 6982 register SV *sv;
79072805 6983
4561caa4 6984 new_SV(sv);
79072805
LW
6985 sv_setiv(sv,i);
6986 return sv;
6987}
6988
954c1994 6989/*
1a3327fb
JH
6990=for apidoc newSVuv
6991
6992Creates a new SV and copies an unsigned integer into it.
6993The reference count for the SV is set to 1.
6994
6995=cut
6996*/
6997
6998SV *
6999Perl_newSVuv(pTHX_ UV u)
7000{
7001 register SV *sv;
7002
7003 new_SV(sv);
7004 sv_setuv(sv,u);
7005 return sv;
7006}
7007
7008/*
954c1994
GS
7009=for apidoc newRV_noinc
7010
7011Creates an RV wrapper for an SV. The reference count for the original
7012SV is B<not> incremented.
7013
7014=cut
7015*/
7016
2304df62 7017SV *
864dbfa3 7018Perl_newRV_noinc(pTHX_ SV *tmpRef)
2304df62
AD
7019{
7020 register SV *sv;
7021
4561caa4 7022 new_SV(sv);
2304df62 7023 sv_upgrade(sv, SVt_RV);
76e3520e 7024 SvTEMP_off(tmpRef);
d689ffdd 7025 SvRV(sv) = tmpRef;
2304df62 7026 SvROK_on(sv);
2304df62
AD
7027 return sv;
7028}
7029
ff276b08 7030/* newRV_inc is the official function name to use now.
645c22ef
DM
7031 * newRV_inc is in fact #defined to newRV in sv.h
7032 */
7033
5f05dabc 7034SV *
864dbfa3 7035Perl_newRV(pTHX_ SV *tmpRef)
5f05dabc 7036{
5f6447b6 7037 return newRV_noinc(SvREFCNT_inc(tmpRef));
5f05dabc 7038}
5f05dabc 7039
954c1994
GS
7040/*
7041=for apidoc newSVsv
7042
7043Creates a new SV which is an exact duplicate of the original SV.
645c22ef 7044(Uses C<sv_setsv>).
954c1994
GS
7045
7046=cut
7047*/
7048
79072805 7049SV *
864dbfa3 7050Perl_newSVsv(pTHX_ register SV *old)
79072805 7051{
463ee0b2 7052 register SV *sv;
79072805
LW
7053
7054 if (!old)
7055 return Nullsv;
8990e307 7056 if (SvTYPE(old) == SVTYPEMASK) {
0453d815 7057 if (ckWARN_d(WARN_INTERNAL))
9014280d 7058 Perl_warner(aTHX_ packWARN(WARN_INTERNAL), "semi-panic: attempt to dup freed string");
79072805
LW
7059 return Nullsv;
7060 }
4561caa4 7061 new_SV(sv);
ff68c719 7062 if (SvTEMP(old)) {
7063 SvTEMP_off(old);
463ee0b2 7064 sv_setsv(sv,old);
ff68c719 7065 SvTEMP_on(old);
79072805
LW
7066 }
7067 else
463ee0b2
LW
7068 sv_setsv(sv,old);
7069 return sv;
79072805
LW
7070}
7071
645c22ef
DM
7072/*
7073=for apidoc sv_reset
7074
7075Underlying implementation for the C<reset> Perl function.
7076Note that the perl-level function is vaguely deprecated.
7077
7078=cut
7079*/
7080
79072805 7081void
864dbfa3 7082Perl_sv_reset(pTHX_ register char *s, HV *stash)
79072805
LW
7083{
7084 register HE *entry;
7085 register GV *gv;
7086 register SV *sv;
7087 register I32 i;
7088 register PMOP *pm;
7089 register I32 max;
4802d5d7 7090 char todo[PERL_UCHAR_MAX+1];
79072805 7091
49d8d3a1
MB
7092 if (!stash)
7093 return;
7094
79072805
LW
7095 if (!*s) { /* reset ?? searches */
7096 for (pm = HvPMROOT(stash); pm; pm = pm->op_pmnext) {
48c036b1 7097 pm->op_pmdynflags &= ~PMdf_USED;
79072805
LW
7098 }
7099 return;
7100 }
7101
7102 /* reset variables */
7103
7104 if (!HvARRAY(stash))
7105 return;
463ee0b2
LW
7106
7107 Zero(todo, 256, char);
79072805 7108 while (*s) {
4802d5d7 7109 i = (unsigned char)*s;
79072805
LW
7110 if (s[1] == '-') {
7111 s += 2;
7112 }
4802d5d7 7113 max = (unsigned char)*s++;
79072805 7114 for ( ; i <= max; i++) {
463ee0b2
LW
7115 todo[i] = 1;
7116 }
a0d0e21e 7117 for (i = 0; i <= (I32) HvMAX(stash); i++) {
79072805 7118 for (entry = HvARRAY(stash)[i];
9e35f4b3
GS
7119 entry;
7120 entry = HeNEXT(entry))
7121 {
1edc1566 7122 if (!todo[(U8)*HeKEY(entry)])
463ee0b2 7123 continue;
1edc1566 7124 gv = (GV*)HeVAL(entry);
79072805 7125 sv = GvSV(gv);
9e35f4b3
GS
7126 if (SvTHINKFIRST(sv)) {
7127 if (!SvREADONLY(sv) && SvROK(sv))
7128 sv_unref(sv);
7129 continue;
7130 }
a0d0e21e 7131 (void)SvOK_off(sv);
79072805
LW
7132 if (SvTYPE(sv) >= SVt_PV) {
7133 SvCUR_set(sv, 0);
463ee0b2
LW
7134 if (SvPVX(sv) != Nullch)
7135 *SvPVX(sv) = '\0';
44a8e56a 7136 SvTAINT(sv);
79072805
LW
7137 }
7138 if (GvAV(gv)) {
7139 av_clear(GvAV(gv));
7140 }
44a8e56a 7141 if (GvHV(gv) && !HvNAME(GvHV(gv))) {
463ee0b2 7142 hv_clear(GvHV(gv));
75a5c1c6 7143#ifndef PERL_MICRO
fa6a1c44 7144#ifdef USE_ENVIRON_ARRAY
4efc5df6
GS
7145 if (gv == PL_envgv
7146# ifdef USE_ITHREADS
7147 && PL_curinterp == aTHX
7148# endif
7149 )
7150 {
79072805 7151 environ[0] = Nullch;
4efc5df6 7152 }
a0d0e21e 7153#endif
75a5c1c6 7154#endif /* !PERL_MICRO */
79072805
LW
7155 }
7156 }
7157 }
7158 }
7159}
7160
645c22ef
DM
7161/*
7162=for apidoc sv_2io
7163
7164Using various gambits, try to get an IO from an SV: the IO slot if its a
7165GV; or the recursive result if we're an RV; or the IO slot of the symbol
7166named after the PV if we're a string.
7167
7168=cut
7169*/
7170
46fc3d4c 7171IO*
864dbfa3 7172Perl_sv_2io(pTHX_ SV *sv)
46fc3d4c 7173{
7174 IO* io;
7175 GV* gv;
2d8e6c8d 7176 STRLEN n_a;
46fc3d4c 7177
7178 switch (SvTYPE(sv)) {
7179 case SVt_PVIO:
7180 io = (IO*)sv;
7181 break;
7182 case SVt_PVGV:
7183 gv = (GV*)sv;
7184 io = GvIO(gv);
7185 if (!io)
cea2e8a9 7186 Perl_croak(aTHX_ "Bad filehandle: %s", GvNAME(gv));
46fc3d4c 7187 break;
7188 default:
7189 if (!SvOK(sv))
cea2e8a9 7190 Perl_croak(aTHX_ PL_no_usym, "filehandle");
46fc3d4c 7191 if (SvROK(sv))
7192 return sv_2io(SvRV(sv));
2d8e6c8d 7193 gv = gv_fetchpv(SvPV(sv,n_a), FALSE, SVt_PVIO);
46fc3d4c 7194 if (gv)
7195 io = GvIO(gv);
7196 else
7197 io = 0;
7198 if (!io)
c293eb2b 7199 Perl_croak(aTHX_ "Bad filehandle: %"SVf, sv);
46fc3d4c 7200 break;
7201 }
7202 return io;
7203}
7204
645c22ef
DM
7205/*
7206=for apidoc sv_2cv
7207
7208Using various gambits, try to get a CV from an SV; in addition, try if
7209possible to set C<*st> and C<*gvp> to the stash and GV associated with it.
7210
7211=cut
7212*/
7213
79072805 7214CV *
864dbfa3 7215Perl_sv_2cv(pTHX_ SV *sv, HV **st, GV **gvp, I32 lref)
79072805 7216{
c04a4dfe
JH
7217 GV *gv = Nullgv;
7218 CV *cv = Nullcv;
2d8e6c8d 7219 STRLEN n_a;
79072805
LW
7220
7221 if (!sv)
93a17b20 7222 return *gvp = Nullgv, Nullcv;
79072805 7223 switch (SvTYPE(sv)) {
79072805
LW
7224 case SVt_PVCV:
7225 *st = CvSTASH(sv);
7226 *gvp = Nullgv;
7227 return (CV*)sv;
7228 case SVt_PVHV:
7229 case SVt_PVAV:
7230 *gvp = Nullgv;
7231 return Nullcv;
8990e307
LW
7232 case SVt_PVGV:
7233 gv = (GV*)sv;
a0d0e21e 7234 *gvp = gv;
8990e307
LW
7235 *st = GvESTASH(gv);
7236 goto fix_gv;
7237
79072805 7238 default:
a0d0e21e
LW
7239 if (SvGMAGICAL(sv))
7240 mg_get(sv);
7241 if (SvROK(sv)) {
f5284f61
IZ
7242 SV **sp = &sv; /* Used in tryAMAGICunDEREF macro. */
7243 tryAMAGICunDEREF(to_cv);
7244
62f274bf
GS
7245 sv = SvRV(sv);
7246 if (SvTYPE(sv) == SVt_PVCV) {
7247 cv = (CV*)sv;
7248 *gvp = Nullgv;
7249 *st = CvSTASH(cv);
7250 return cv;
7251 }
7252 else if(isGV(sv))
7253 gv = (GV*)sv;
7254 else
cea2e8a9 7255 Perl_croak(aTHX_ "Not a subroutine reference");
a0d0e21e 7256 }
62f274bf 7257 else if (isGV(sv))
79072805
LW
7258 gv = (GV*)sv;
7259 else
2d8e6c8d 7260 gv = gv_fetchpv(SvPV(sv, n_a), lref, SVt_PVCV);
79072805
LW
7261 *gvp = gv;
7262 if (!gv)
7263 return Nullcv;
7264 *st = GvESTASH(gv);
8990e307 7265 fix_gv:
8ebc5c01 7266 if (lref && !GvCVu(gv)) {
4633a7c4 7267 SV *tmpsv;
748a9306 7268 ENTER;
4633a7c4 7269 tmpsv = NEWSV(704,0);
16660edb 7270 gv_efullname3(tmpsv, gv, Nullch);
f6ec51f7
GS
7271 /* XXX this is probably not what they think they're getting.
7272 * It has the same effect as "sub name;", i.e. just a forward
7273 * declaration! */
774d564b 7274 newSUB(start_subparse(FALSE, 0),
4633a7c4
LW
7275 newSVOP(OP_CONST, 0, tmpsv),
7276 Nullop,
8990e307 7277 Nullop);
748a9306 7278 LEAVE;
8ebc5c01 7279 if (!GvCVu(gv))
c293eb2b
NC
7280 Perl_croak(aTHX_ "Unable to create sub named \"%"SVf"\"",
7281 sv);
8990e307 7282 }
8ebc5c01 7283 return GvCVu(gv);
79072805
LW
7284 }
7285}
7286
c461cf8f
JH
7287/*
7288=for apidoc sv_true
7289
7290Returns true if the SV has a true value by Perl's rules.
645c22ef
DM
7291Use the C<SvTRUE> macro instead, which may call C<sv_true()> or may
7292instead use an in-line version.
c461cf8f
JH
7293
7294=cut
7295*/
7296
79072805 7297I32
864dbfa3 7298Perl_sv_true(pTHX_ register SV *sv)
79072805 7299{
8990e307
LW
7300 if (!sv)
7301 return 0;
79072805 7302 if (SvPOK(sv)) {
4e35701f
NIS
7303 register XPV* tXpv;
7304 if ((tXpv = (XPV*)SvANY(sv)) &&
c2f1de04 7305 (tXpv->xpv_cur > 1 ||
4e35701f 7306 (tXpv->xpv_cur && *tXpv->xpv_pv != '0')))
79072805
LW
7307 return 1;
7308 else
7309 return 0;
7310 }
7311 else {
7312 if (SvIOK(sv))
463ee0b2 7313 return SvIVX(sv) != 0;
79072805
LW
7314 else {
7315 if (SvNOK(sv))
463ee0b2 7316 return SvNVX(sv) != 0.0;
79072805 7317 else
463ee0b2 7318 return sv_2bool(sv);
79072805
LW
7319 }
7320 }
7321}
79072805 7322
645c22ef
DM
7323/*
7324=for apidoc sv_iv
7325
7326A private implementation of the C<SvIVx> macro for compilers which can't
7327cope with complex macro expressions. Always use the macro instead.
7328
7329=cut
7330*/
7331
ff68c719 7332IV
864dbfa3 7333Perl_sv_iv(pTHX_ register SV *sv)
85e6fe83 7334{
25da4f38
IZ
7335 if (SvIOK(sv)) {
7336 if (SvIsUV(sv))
7337 return (IV)SvUVX(sv);
ff68c719 7338 return SvIVX(sv);
25da4f38 7339 }
ff68c719 7340 return sv_2iv(sv);
85e6fe83 7341}
85e6fe83 7342
645c22ef
DM
7343/*
7344=for apidoc sv_uv
7345
7346A private implementation of the C<SvUVx> macro for compilers which can't
7347cope with complex macro expressions. Always use the macro instead.
7348
7349=cut
7350*/
7351
ff68c719 7352UV
864dbfa3 7353Perl_sv_uv(pTHX_ register SV *sv)
ff68c719 7354{
25da4f38
IZ
7355 if (SvIOK(sv)) {
7356 if (SvIsUV(sv))
7357 return SvUVX(sv);
7358 return (UV)SvIVX(sv);
7359 }
ff68c719 7360 return sv_2uv(sv);
7361}
85e6fe83 7362
645c22ef
DM
7363/*
7364=for apidoc sv_nv
7365
7366A private implementation of the C<SvNVx> macro for compilers which can't
7367cope with complex macro expressions. Always use the macro instead.
7368
7369=cut
7370*/
7371
65202027 7372NV
864dbfa3 7373Perl_sv_nv(pTHX_ register SV *sv)
79072805 7374{
ff68c719 7375 if (SvNOK(sv))
7376 return SvNVX(sv);
7377 return sv_2nv(sv);
79072805 7378}
79072805 7379
d34f9d2e
JH
7380/* sv_pv() is now a macro using SvPV_nolen();
7381 * this function provided for binary compatibility only
7382 */
7383
7384char *
7385Perl_sv_pv(pTHX_ SV *sv)
7386{
7387 STRLEN n_a;
7388
7389 if (SvPOK(sv))
7390 return SvPVX(sv);
7391
7392 return sv_2pv(sv, &n_a);
7393}
7394
645c22ef
DM
7395/*
7396=for apidoc sv_pv
7397
baca2b92 7398Use the C<SvPV_nolen> macro instead
645c22ef 7399
645c22ef
DM
7400=for apidoc sv_pvn
7401
7402A private implementation of the C<SvPV> macro for compilers which can't
7403cope with complex macro expressions. Always use the macro instead.
7404
7405=cut
7406*/
7407
1fa8b10d 7408char *
864dbfa3 7409Perl_sv_pvn(pTHX_ SV *sv, STRLEN *lp)
79072805 7410{
85e6fe83
LW
7411 if (SvPOK(sv)) {
7412 *lp = SvCUR(sv);
a0d0e21e 7413 return SvPVX(sv);
85e6fe83 7414 }
463ee0b2 7415 return sv_2pv(sv, lp);
79072805 7416}
79072805 7417
6e9d1081
NC
7418
7419char *
7420Perl_sv_pvn_nomg(pTHX_ register SV *sv, STRLEN *lp)
7421{
7422 if (SvPOK(sv)) {
7423 *lp = SvCUR(sv);
7424 return SvPVX(sv);
7425 }
7426 return sv_2pv_flags(sv, lp, 0);
7427}
7428
d34f9d2e
JH
7429/* sv_pvn_force() is now a macro using Perl_sv_pvn_force_flags();
7430 * this function provided for binary compatibility only
7431 */
7432
7433char *
7434Perl_sv_pvn_force(pTHX_ SV *sv, STRLEN *lp)
7435{
7436 return sv_pvn_force_flags(sv, lp, SV_GMAGIC);
7437}
7438
c461cf8f
JH
7439/*
7440=for apidoc sv_pvn_force
7441
7442Get a sensible string out of the SV somehow.
645c22ef
DM
7443A private implementation of the C<SvPV_force> macro for compilers which
7444can't cope with complex macro expressions. Always use the macro instead.
c461cf8f 7445
8d6d96c1
HS
7446=for apidoc sv_pvn_force_flags
7447
7448Get a sensible string out of the SV somehow.
7449If C<flags> has C<SV_GMAGIC> bit set, will C<mg_get> on C<sv> if
7450appropriate, else not. C<sv_pvn_force> and C<sv_pvn_force_nomg> are
7451implemented in terms of this function.
645c22ef
DM
7452You normally want to use the various wrapper macros instead: see
7453C<SvPV_force> and C<SvPV_force_nomg>
8d6d96c1
HS
7454
7455=cut
7456*/
7457
7458char *
7459Perl_sv_pvn_force_flags(pTHX_ SV *sv, STRLEN *lp, I32 flags)
7460{
c04a4dfe 7461 char *s = NULL;
a0d0e21e 7462
6fc92669
GS
7463 if (SvTHINKFIRST(sv) && !SvROK(sv))
7464 sv_force_normal(sv);
1c846c1f 7465
a0d0e21e
LW
7466 if (SvPOK(sv)) {
7467 *lp = SvCUR(sv);
7468 }
7469 else {
748a9306 7470 if (SvTYPE(sv) > SVt_PVLV && SvTYPE(sv) != SVt_PVFM) {
cea2e8a9 7471 Perl_croak(aTHX_ "Can't coerce %s to string in %s", sv_reftype(sv,0),
53e06cf0 7472 OP_NAME(PL_op));
a0d0e21e 7473 }
4633a7c4 7474 else
8d6d96c1 7475 s = sv_2pv_flags(sv, lp, flags);
a0d0e21e
LW
7476 if (s != SvPVX(sv)) { /* Almost, but not quite, sv_setpvn() */
7477 STRLEN len = *lp;
1c846c1f 7478
a0d0e21e
LW
7479 if (SvROK(sv))
7480 sv_unref(sv);
7481 (void)SvUPGRADE(sv, SVt_PV); /* Never FALSE */
7482 SvGROW(sv, len + 1);
7483 Move(s,SvPVX(sv),len,char);
7484 SvCUR_set(sv, len);
7485 *SvEND(sv) = '\0';
7486 }
7487 if (!SvPOK(sv)) {
7488 SvPOK_on(sv); /* validate pointer */
7489 SvTAINT(sv);
1d7c1841
GS
7490 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n",
7491 PTR2UV(sv),SvPVX(sv)));
a0d0e21e
LW
7492 }
7493 }
7494 return SvPVX(sv);
7495}
7496
d34f9d2e
JH
7497/* sv_pvbyte () is now a macro using Perl_sv_2pv_flags();
7498 * this function provided for binary compatibility only
7499 */
7500
7501char *
7502Perl_sv_pvbyte(pTHX_ SV *sv)
7503{
7504 sv_utf8_downgrade(sv,0);
7505 return sv_pv(sv);
7506}
7507
645c22ef
DM
7508/*
7509=for apidoc sv_pvbyte
7510
baca2b92 7511Use C<SvPVbyte_nolen> instead.
645c22ef 7512
645c22ef
DM
7513=for apidoc sv_pvbyten
7514
7515A private implementation of the C<SvPVbyte> macro for compilers
7516which can't cope with complex macro expressions. Always use the macro
7517instead.
7518
7519=cut
7520*/
7521
7340a771
GS
7522char *
7523Perl_sv_pvbyten(pTHX_ SV *sv, STRLEN *lp)
7524{
ffebcc3e 7525 sv_utf8_downgrade(sv,0);
7340a771
GS
7526 return sv_pvn(sv,lp);
7527}
7528
645c22ef
DM
7529/*
7530=for apidoc sv_pvbyten_force
7531
7532A private implementation of the C<SvPVbytex_force> macro for compilers
7533which can't cope with complex macro expressions. Always use the macro
7534instead.
7535
7536=cut
7537*/
7538
7340a771
GS
7539char *
7540Perl_sv_pvbyten_force(pTHX_ SV *sv, STRLEN *lp)
7541{
b035a42e 7542 sv_pvn_force(sv,lp);
ffebcc3e 7543 sv_utf8_downgrade(sv,0);
b035a42e
NC
7544 *lp = SvCUR(sv);
7545 return SvPVX(sv);
7340a771
GS
7546}
7547
d34f9d2e
JH
7548/* sv_pvutf8 () is now a macro using Perl_sv_2pv_flags();
7549 * this function provided for binary compatibility only
7550 */
7551
7552char *
7553Perl_sv_pvutf8(pTHX_ SV *sv)
7554{
7555 sv_utf8_upgrade(sv);
7556 return sv_pv(sv);
7557}
7558
645c22ef
DM
7559/*
7560=for apidoc sv_pvutf8
7561
baca2b92 7562Use the C<SvPVutf8_nolen> macro instead
645c22ef 7563
645c22ef
DM
7564=for apidoc sv_pvutf8n
7565
7566A private implementation of the C<SvPVutf8> macro for compilers
7567which can't cope with complex macro expressions. Always use the macro
7568instead.
7569
7570=cut
7571*/
7572
7340a771
GS
7573char *
7574Perl_sv_pvutf8n(pTHX_ SV *sv, STRLEN *lp)
7575{
560a288e 7576 sv_utf8_upgrade(sv);
7340a771
GS
7577 return sv_pvn(sv,lp);
7578}
7579
c461cf8f
JH
7580/*
7581=for apidoc sv_pvutf8n_force
7582
645c22ef
DM
7583A private implementation of the C<SvPVutf8_force> macro for compilers
7584which can't cope with complex macro expressions. Always use the macro
7585instead.
c461cf8f
JH
7586
7587=cut
7588*/
7589
7340a771
GS
7590char *
7591Perl_sv_pvutf8n_force(pTHX_ SV *sv, STRLEN *lp)
7592{
b035a42e 7593 sv_pvn_force(sv,lp);
560a288e 7594 sv_utf8_upgrade(sv);
b035a42e
NC
7595 *lp = SvCUR(sv);
7596 return SvPVX(sv);
7340a771
GS
7597}
7598
c461cf8f
JH
7599/*
7600=for apidoc sv_reftype
7601
7602Returns a string describing what the SV is a reference to.
7603
7604=cut
7605*/
7606
7340a771 7607char *
864dbfa3 7608Perl_sv_reftype(pTHX_ SV *sv, int ob)
a0d0e21e 7609{
c86bf373
AMS
7610 if (ob && SvOBJECT(sv)) {
7611 HV *svs = SvSTASH(sv);
7612 /* [20011101.072] This bandaid for C<package;> should eventually
7613 be removed. AMS 20011103 */
7614 return (svs ? HvNAME(svs) : "<none>");
7615 }
a0d0e21e
LW
7616 else {
7617 switch (SvTYPE(sv)) {
7618 case SVt_NULL:
7619 case SVt_IV:
7620 case SVt_NV:
7621 case SVt_RV:
7622 case SVt_PV:
7623 case SVt_PVIV:
7624 case SVt_PVNV:
7625 case SVt_PVMG:
7626 case SVt_PVBM:
7627 if (SvROK(sv))
7628 return "REF";
7629 else
7630 return "SCALAR";
ac388100
JH
7631
7632 case SVt_PVLV: return SvROK(sv) ? "REF"
7633 /* tied lvalues should appear to be
7634 * scalars for backwards compatitbility */
7635 : (LvTYPE(sv) == 't' || LvTYPE(sv) == 'T')
7636 ? "SCALAR" : "LVALUE";
a0d0e21e
LW
7637 case SVt_PVAV: return "ARRAY";
7638 case SVt_PVHV: return "HASH";
7639 case SVt_PVCV: return "CODE";
7640 case SVt_PVGV: return "GLOB";
1d2dff63 7641 case SVt_PVFM: return "FORMAT";
27f9d8f3 7642 case SVt_PVIO: return "IO";
a0d0e21e
LW
7643 default: return "UNKNOWN";
7644 }
7645 }
7646}
7647
954c1994
GS
7648/*
7649=for apidoc sv_isobject
7650
7651Returns a boolean indicating whether the SV is an RV pointing to a blessed
7652object. If the SV is not an RV, or if the object is not blessed, then this
7653will return false.
7654
7655=cut
7656*/
7657
463ee0b2 7658int
864dbfa3 7659Perl_sv_isobject(pTHX_ SV *sv)
85e6fe83 7660{
68dc0745 7661 if (!sv)
7662 return 0;
7663 if (SvGMAGICAL(sv))
7664 mg_get(sv);
85e6fe83
LW
7665 if (!SvROK(sv))
7666 return 0;
7667 sv = (SV*)SvRV(sv);
7668 if (!SvOBJECT(sv))
7669 return 0;
7670 return 1;
7671}
7672
954c1994
GS
7673/*
7674=for apidoc sv_isa
7675
7676Returns a boolean indicating whether the SV is blessed into the specified
7677class. This does not check for subtypes; use C<sv_derived_from> to verify
7678an inheritance relationship.
7679
7680=cut
7681*/
7682
85e6fe83 7683int
864dbfa3 7684Perl_sv_isa(pTHX_ SV *sv, const char *name)
463ee0b2 7685{
68dc0745 7686 if (!sv)
7687 return 0;
7688 if (SvGMAGICAL(sv))
7689 mg_get(sv);
ed6116ce 7690 if (!SvROK(sv))
463ee0b2 7691 return 0;
ed6116ce
LW
7692 sv = (SV*)SvRV(sv);
7693 if (!SvOBJECT(sv))
463ee0b2
LW
7694 return 0;
7695
7696 return strEQ(HvNAME(SvSTASH(sv)), name);
7697}
7698
954c1994
GS
7699/*
7700=for apidoc newSVrv
7701
7702Creates a new SV for the RV, C<rv>, to point to. If C<rv> is not an RV then
7703it will be upgraded to one. If C<classname> is non-null then the new SV will
7704be blessed in the specified package. The new SV is returned and its
7705reference count is 1.
7706
7707=cut
7708*/
7709
463ee0b2 7710SV*
864dbfa3 7711Perl_newSVrv(pTHX_ SV *rv, const char *classname)
463ee0b2 7712{
463ee0b2
LW
7713 SV *sv;
7714
4561caa4 7715 new_SV(sv);
51cf62d8 7716
2213622d 7717 SV_CHECK_THINKFIRST(rv);
51cf62d8 7718 SvAMAGIC_off(rv);
51cf62d8 7719
0199fce9
JD
7720 if (SvTYPE(rv) >= SVt_PVMG) {
7721 U32 refcnt = SvREFCNT(rv);
7722 SvREFCNT(rv) = 0;
7723 sv_clear(rv);
7724 SvFLAGS(rv) = 0;
7725 SvREFCNT(rv) = refcnt;
7726 }
7727
51cf62d8 7728 if (SvTYPE(rv) < SVt_RV)
0199fce9
JD
7729 sv_upgrade(rv, SVt_RV);
7730 else if (SvTYPE(rv) > SVt_RV) {
7731 (void)SvOOK_off(rv);
7732 if (SvPVX(rv) && SvLEN(rv))
7733 Safefree(SvPVX(rv));
7734 SvCUR_set(rv, 0);
7735 SvLEN_set(rv, 0);
7736 }
51cf62d8
OT
7737
7738 (void)SvOK_off(rv);
053fc874 7739 SvRV(rv) = sv;
ed6116ce 7740 SvROK_on(rv);
463ee0b2 7741
a0d0e21e
LW
7742 if (classname) {
7743 HV* stash = gv_stashpv(classname, TRUE);
7744 (void)sv_bless(rv, stash);
7745 }
7746 return sv;
7747}
7748
954c1994
GS
7749/*
7750=for apidoc sv_setref_pv
7751
7752Copies a pointer into a new SV, optionally blessing the SV. The C<rv>
7753argument will be upgraded to an RV. That RV will be modified to point to
7754the new SV. If the C<pv> argument is NULL then C<PL_sv_undef> will be placed
7755into the SV. The C<classname> argument indicates the package for the
7756blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV
89e79dea 7757will have a reference count of 1, and the RV will be returned.
954c1994
GS
7758
7759Do not use with other Perl types such as HV, AV, SV, CV, because those
7760objects will become corrupted by the pointer copy process.
7761
7762Note that C<sv_setref_pvn> copies the string while this copies the pointer.
7763
7764=cut
7765*/
7766
a0d0e21e 7767SV*
864dbfa3 7768Perl_sv_setref_pv(pTHX_ SV *rv, const char *classname, void *pv)
a0d0e21e 7769{
189b2af5 7770 if (!pv) {
3280af22 7771 sv_setsv(rv, &PL_sv_undef);
189b2af5
GS
7772 SvSETMAGIC(rv);
7773 }
a0d0e21e 7774 else
56431972 7775 sv_setiv(newSVrv(rv,classname), PTR2IV(pv));
a0d0e21e
LW
7776 return rv;
7777}
7778
954c1994
GS
7779/*
7780=for apidoc sv_setref_iv
7781
7782Copies an integer into a new SV, optionally blessing the SV. The C<rv>
7783argument will be upgraded to an RV. That RV will be modified to point to
7784the new SV. The C<classname> argument indicates the package for the
7785blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV
89e79dea 7786will have a reference count of 1, and the RV will be returned.
954c1994
GS
7787
7788=cut
7789*/
7790
a0d0e21e 7791SV*
864dbfa3 7792Perl_sv_setref_iv(pTHX_ SV *rv, const char *classname, IV iv)
a0d0e21e
LW
7793{
7794 sv_setiv(newSVrv(rv,classname), iv);
7795 return rv;
7796}
7797
954c1994 7798/*
e1c57cef
JH
7799=for apidoc sv_setref_uv
7800
7801Copies an unsigned integer into a new SV, optionally blessing the SV. The C<rv>
7802argument will be upgraded to an RV. That RV will be modified to point to
7803the new SV. The C<classname> argument indicates the package for the
7804blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV
89e79dea 7805will have a reference count of 1, and the RV will be returned.
e1c57cef
JH
7806
7807=cut
7808*/
7809
7810SV*
7811Perl_sv_setref_uv(pTHX_ SV *rv, const char *classname, UV uv)
7812{
7813 sv_setuv(newSVrv(rv,classname), uv);
7814 return rv;
7815}
7816
7817/*
954c1994
GS
7818=for apidoc sv_setref_nv
7819
7820Copies a double into a new SV, optionally blessing the SV. The C<rv>
7821argument will be upgraded to an RV. That RV will be modified to point to
7822the new SV. The C<classname> argument indicates the package for the
7823blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV
89e79dea 7824will have a reference count of 1, and the RV will be returned.
954c1994
GS
7825
7826=cut
7827*/
7828
a0d0e21e 7829SV*
65202027 7830Perl_sv_setref_nv(pTHX_ SV *rv, const char *classname, NV nv)
a0d0e21e
LW
7831{
7832 sv_setnv(newSVrv(rv,classname), nv);
7833 return rv;
7834}
463ee0b2 7835
954c1994
GS
7836/*
7837=for apidoc sv_setref_pvn
7838
7839Copies a string into a new SV, optionally blessing the SV. The length of the
7840string must be specified with C<n>. The C<rv> argument will be upgraded to
7841an RV. That RV will be modified to point to the new SV. The C<classname>
7842argument indicates the package for the blessing. Set C<classname> to
89e79dea
JH
7843C<Nullch> to avoid the blessing. The new SV will have a reference count
7844of 1, and the RV will be returned.
954c1994
GS
7845
7846Note that C<sv_setref_pv> copies the pointer while this copies the string.
7847
7848=cut
7849*/
7850
a0d0e21e 7851SV*
864dbfa3 7852Perl_sv_setref_pvn(pTHX_ SV *rv, const char *classname, char *pv, STRLEN n)
a0d0e21e
LW
7853{
7854 sv_setpvn(newSVrv(rv,classname), pv, n);
463ee0b2
LW
7855 return rv;
7856}
7857
954c1994
GS
7858/*
7859=for apidoc sv_bless
7860
7861Blesses an SV into a specified package. The SV must be an RV. The package
7862must be designated by its stash (see C<gv_stashpv()>). The reference count
7863of the SV is unaffected.
7864
7865=cut
7866*/
7867
a0d0e21e 7868SV*
864dbfa3 7869Perl_sv_bless(pTHX_ SV *sv, HV *stash)
a0d0e21e 7870{
76e3520e 7871 SV *tmpRef;
a0d0e21e 7872 if (!SvROK(sv))
cea2e8a9 7873 Perl_croak(aTHX_ "Can't bless non-reference value");
76e3520e
GS
7874 tmpRef = SvRV(sv);
7875 if (SvFLAGS(tmpRef) & (SVs_OBJECT|SVf_READONLY)) {
7876 if (SvREADONLY(tmpRef))
cea2e8a9 7877 Perl_croak(aTHX_ PL_no_modify);
76e3520e
GS
7878 if (SvOBJECT(tmpRef)) {
7879 if (SvTYPE(tmpRef) != SVt_PVIO)
3280af22 7880 --PL_sv_objcount;
76e3520e 7881 SvREFCNT_dec(SvSTASH(tmpRef));
2e3febc6 7882 }
a0d0e21e 7883 }
76e3520e
GS
7884 SvOBJECT_on(tmpRef);
7885 if (SvTYPE(tmpRef) != SVt_PVIO)
3280af22 7886 ++PL_sv_objcount;
76e3520e
GS
7887 (void)SvUPGRADE(tmpRef, SVt_PVMG);
7888 SvSTASH(tmpRef) = (HV*)SvREFCNT_inc(stash);
a0d0e21e 7889
2e3febc6
CS
7890 if (Gv_AMG(stash))
7891 SvAMAGIC_on(sv);
7892 else
7893 SvAMAGIC_off(sv);
a0d0e21e 7894
1edbfb88
AB
7895 if(SvSMAGICAL(tmpRef))
7896 if(mg_find(tmpRef, PERL_MAGIC_ext) || mg_find(tmpRef, PERL_MAGIC_uvar))
7897 mg_set(tmpRef);
7898
7899
ecdeb87c 7900
a0d0e21e
LW
7901 return sv;
7902}
7903
645c22ef 7904/* Downgrades a PVGV to a PVMG.
645c22ef
DM
7905 */
7906
76e3520e 7907STATIC void
cea2e8a9 7908S_sv_unglob(pTHX_ SV *sv)
a0d0e21e 7909{
850fabdf
GS
7910 void *xpvmg;
7911
a0d0e21e
LW
7912 assert(SvTYPE(sv) == SVt_PVGV);
7913 SvFAKE_off(sv);
7914 if (GvGP(sv))
1edc1566 7915 gp_free((GV*)sv);
e826b3c7
GS
7916 if (GvSTASH(sv)) {
7917 SvREFCNT_dec(GvSTASH(sv));
7918 GvSTASH(sv) = Nullhv;
7919 }
14befaf4 7920 sv_unmagic(sv, PERL_MAGIC_glob);
a0d0e21e 7921 Safefree(GvNAME(sv));
a5f75d66 7922 GvMULTI_off(sv);
850fabdf
GS
7923
7924 /* need to keep SvANY(sv) in the right arena */
7925 xpvmg = new_XPVMG();
7926 StructCopy(SvANY(sv), xpvmg, XPVMG);
7927 del_XPVGV(SvANY(sv));
7928 SvANY(sv) = xpvmg;
7929
a0d0e21e
LW
7930 SvFLAGS(sv) &= ~SVTYPEMASK;
7931 SvFLAGS(sv) |= SVt_PVMG;
7932}
7933
954c1994 7934/*
840a7b70 7935=for apidoc sv_unref_flags
954c1994
GS
7936
7937Unsets the RV status of the SV, and decrements the reference count of
7938whatever was being referenced by the RV. This can almost be thought of
840a7b70
IZ
7939as a reversal of C<newSVrv>. The C<cflags> argument can contain
7940C<SV_IMMEDIATE_UNREF> to force the reference count to be decremented
7941(otherwise the decrementing is conditional on the reference count being
7942different from one or the reference being a readonly SV).
7889fe52 7943See C<SvROK_off>.
954c1994
GS
7944
7945=cut
7946*/
7947
ed6116ce 7948void
840a7b70 7949Perl_sv_unref_flags(pTHX_ SV *sv, U32 flags)
ed6116ce 7950{
a0d0e21e 7951 SV* rv = SvRV(sv);
810b8aa5
GS
7952
7953 if (SvWEAKREF(sv)) {
7954 sv_del_backref(sv);
7955 SvWEAKREF_off(sv);
7956 SvRV(sv) = 0;
7957 return;
7958 }
ed6116ce
LW
7959 SvRV(sv) = 0;
7960 SvROK_off(sv);
5e8f8cda
JH
7961 /* You can't have a || SvREADONLY(rv) here, as $a = $$a, where $a was
7962 assigned to as BEGIN {$a = \"Foo"} will fail. */
7963 if (SvREFCNT(rv) != 1 || (flags & SV_IMMEDIATE_UNREF))
4633a7c4 7964 SvREFCNT_dec(rv);
840a7b70 7965 else /* XXX Hack, but hard to make $a=$a->[1] work otherwise */
4633a7c4 7966 sv_2mortal(rv); /* Schedule for freeing later */
ed6116ce 7967}
8990e307 7968
840a7b70
IZ
7969/*
7970=for apidoc sv_unref
7971
7972Unsets the RV status of the SV, and decrements the reference count of
7973whatever was being referenced by the RV. This can almost be thought of
7974as a reversal of C<newSVrv>. This is C<sv_unref_flags> with the C<flag>
7889fe52 7975being zero. See C<SvROK_off>.
840a7b70
IZ
7976
7977=cut
7978*/
7979
7980void
7981Perl_sv_unref(pTHX_ SV *sv)
7982{
7983 sv_unref_flags(sv, 0);
7984}
7985
645c22ef
DM
7986/*
7987=for apidoc sv_taint
7988
7989Taint an SV. Use C<SvTAINTED_on> instead.
7990=cut
7991*/
7992
bbce6d69 7993void
864dbfa3 7994Perl_sv_taint(pTHX_ SV *sv)
bbce6d69 7995{
14befaf4 7996 sv_magic((sv), Nullsv, PERL_MAGIC_taint, Nullch, 0);
bbce6d69 7997}
7998
645c22ef
DM
7999/*
8000=for apidoc sv_untaint
8001
8002Untaint an SV. Use C<SvTAINTED_off> instead.
8003=cut
8004*/
8005
bbce6d69 8006void
864dbfa3 8007Perl_sv_untaint(pTHX_ SV *sv)
bbce6d69 8008{
13f57bf8 8009 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
14befaf4 8010 MAGIC *mg = mg_find(sv, PERL_MAGIC_taint);
36477c24 8011 if (mg)
565764a8 8012 mg->mg_len &= ~1;
36477c24 8013 }
bbce6d69 8014}
8015
645c22ef
DM
8016/*
8017=for apidoc sv_tainted
8018
8019Test an SV for taintedness. Use C<SvTAINTED> instead.
8020=cut
8021*/
8022
bbce6d69 8023bool
864dbfa3 8024Perl_sv_tainted(pTHX_ SV *sv)
bbce6d69 8025{
13f57bf8 8026 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
14befaf4 8027 MAGIC *mg = mg_find(sv, PERL_MAGIC_taint);
155aba94 8028 if (mg && ((mg->mg_len & 1) || ((mg->mg_len & 2) && mg->mg_obj == sv)))
36477c24 8029 return TRUE;
8030 }
8031 return FALSE;
bbce6d69 8032}
8033
d34f9d2e
JH
8034/*
8035=for apidoc sv_setpviv
8036
8037Copies an integer into the given SV, also updating its string value.
8038Does not handle 'set' magic. See C<sv_setpviv_mg>.
8039
8040=cut
8041*/
8042
8043void
8044Perl_sv_setpviv(pTHX_ SV *sv, IV iv)
8045{
8046 char buf[TYPE_CHARS(UV)];
8047 char *ebuf;
8048 char *ptr = uiv_2buf(buf, iv, 0, 0, &ebuf);
8049
8050 sv_setpvn(sv, ptr, ebuf - ptr);
8051}
8052
8053/*
8054=for apidoc sv_setpviv_mg
8055
8056Like C<sv_setpviv>, but also handles 'set' magic.
8057
8058=cut
8059*/
8060
8061void
8062Perl_sv_setpviv_mg(pTHX_ SV *sv, IV iv)
8063{
8064 char buf[TYPE_CHARS(UV)];
8065 char *ebuf;
8066 char *ptr = uiv_2buf(buf, iv, 0, 0, &ebuf);
8067
8068 sv_setpvn(sv, ptr, ebuf - ptr);
8069 SvSETMAGIC(sv);
8070}
8071
cea2e8a9 8072#if defined(PERL_IMPLICIT_CONTEXT)
645c22ef
DM
8073
8074/* pTHX_ magic can't cope with varargs, so this is a no-context
8075 * version of the main function, (which may itself be aliased to us).
8076 * Don't access this version directly.
8077 */
8078
cea2e8a9
GS
8079void
8080Perl_sv_setpvf_nocontext(SV *sv, const char* pat, ...)
8081{
8082 dTHX;
8083 va_list args;
8084 va_start(args, pat);
c5be433b 8085 sv_vsetpvf(sv, pat, &args);
cea2e8a9
GS
8086 va_end(args);
8087}
8088
645c22ef
DM
8089/* pTHX_ magic can't cope with varargs, so this is a no-context
8090 * version of the main function, (which may itself be aliased to us).
8091 * Don't access this version directly.
8092 */
cea2e8a9
GS
8093
8094void
8095Perl_sv_setpvf_mg_nocontext(SV *sv, const char* pat, ...)
8096{
8097 dTHX;
8098 va_list args;
8099 va_start(args, pat);
c5be433b 8100 sv_vsetpvf_mg(sv, pat, &args);
cea2e8a9 8101 va_end(args);
cea2e8a9
GS
8102}
8103#endif
8104
954c1994
GS
8105/*
8106=for apidoc sv_setpvf
8107
8108Processes its arguments like C<sprintf> and sets an SV to the formatted
8109output. Does not handle 'set' magic. See C<sv_setpvf_mg>.
8110
8111=cut
8112*/
8113
46fc3d4c 8114void
864dbfa3 8115Perl_sv_setpvf(pTHX_ SV *sv, const char* pat, ...)
46fc3d4c 8116{
8117 va_list args;
46fc3d4c 8118 va_start(args, pat);
c5be433b 8119 sv_vsetpvf(sv, pat, &args);
46fc3d4c 8120 va_end(args);
8121}
8122
645c22ef
DM
8123/* backend for C<sv_setpvf> and C<sv_setpvf_nocontext> */
8124
c5be433b
GS
8125void
8126Perl_sv_vsetpvf(pTHX_ SV *sv, const char* pat, va_list* args)
8127{
8128 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
8129}
ef50df4b 8130
954c1994
GS
8131/*
8132=for apidoc sv_setpvf_mg
8133
8134Like C<sv_setpvf>, but also handles 'set' magic.
8135
8136=cut
8137*/
8138
ef50df4b 8139void
864dbfa3 8140Perl_sv_setpvf_mg(pTHX_ SV *sv, const char* pat, ...)
ef50df4b
GS
8141{
8142 va_list args;
ef50df4b 8143 va_start(args, pat);
c5be433b 8144 sv_vsetpvf_mg(sv, pat, &args);
ef50df4b 8145 va_end(args);
c5be433b
GS
8146}
8147
645c22ef
DM
8148/* backend for C<sv_setpvf_mg> C<setpvf_mg_nocontext> */
8149
c5be433b
GS
8150void
8151Perl_sv_vsetpvf_mg(pTHX_ SV *sv, const char* pat, va_list* args)
8152{
8153 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
ef50df4b
GS
8154 SvSETMAGIC(sv);
8155}
8156
cea2e8a9 8157#if defined(PERL_IMPLICIT_CONTEXT)
645c22ef
DM
8158
8159/* pTHX_ magic can't cope with varargs, so this is a no-context
8160 * version of the main function, (which may itself be aliased to us).
8161 * Don't access this version directly.
8162 */
8163
cea2e8a9
GS
8164void
8165Perl_sv_catpvf_nocontext(SV *sv, const char* pat, ...)
8166{
8167 dTHX;
8168 va_list args;
8169 va_start(args, pat);
c5be433b 8170 sv_vcatpvf(sv, pat, &args);
cea2e8a9
GS
8171 va_end(args);
8172}
8173
645c22ef
DM
8174/* pTHX_ magic can't cope with varargs, so this is a no-context
8175 * version of the main function, (which may itself be aliased to us).
8176 * Don't access this version directly.
8177 */
8178
cea2e8a9
GS
8179void
8180Perl_sv_catpvf_mg_nocontext(SV *sv, const char* pat, ...)
8181{
8182 dTHX;
8183 va_list args;
8184 va_start(args, pat);
c5be433b 8185 sv_vcatpvf_mg(sv, pat, &args);
cea2e8a9 8186 va_end(args);
cea2e8a9
GS
8187}
8188#endif
8189
954c1994
GS
8190/*
8191=for apidoc sv_catpvf
8192
d5ce4a7c
GA
8193Processes its arguments like C<sprintf> and appends the formatted
8194output to an SV. If the appended data contains "wide" characters
8195(including, but not limited to, SVs with a UTF-8 PV formatted with %s,
8196and characters >255 formatted with %c), the original SV might get
8197upgraded to UTF-8. Handles 'get' magic, but not 'set' magic.
8198C<SvSETMAGIC()> must typically be called after calling this function
8199to handle 'set' magic.
954c1994 8200
d5ce4a7c 8201=cut */
954c1994 8202
46fc3d4c 8203void
864dbfa3 8204Perl_sv_catpvf(pTHX_ SV *sv, const char* pat, ...)
46fc3d4c 8205{
8206 va_list args;
46fc3d4c 8207 va_start(args, pat);
c5be433b 8208 sv_vcatpvf(sv, pat, &args);
46fc3d4c 8209 va_end(args);
8210}
8211
645c22ef
DM
8212/* backend for C<sv_catpvf> and C<catpvf_mg_nocontext> */
8213
ef50df4b 8214void
c5be433b
GS
8215Perl_sv_vcatpvf(pTHX_ SV *sv, const char* pat, va_list* args)
8216{
8217 sv_vcatpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
8218}
8219
954c1994
GS
8220/*
8221=for apidoc sv_catpvf_mg
8222
8223Like C<sv_catpvf>, but also handles 'set' magic.
8224
8225=cut
8226*/
8227
c5be433b 8228void
864dbfa3 8229Perl_sv_catpvf_mg(pTHX_ SV *sv, const char* pat, ...)
ef50df4b
GS
8230{
8231 va_list args;
ef50df4b 8232 va_start(args, pat);
c5be433b 8233 sv_vcatpvf_mg(sv, pat, &args);
ef50df4b 8234 va_end(args);
c5be433b
GS
8235}
8236
645c22ef
DM
8237/* backend for C<catpvf_mg> and C<catpvf_mg_nocontext> */
8238
c5be433b
GS
8239void
8240Perl_sv_vcatpvf_mg(pTHX_ SV *sv, const char* pat, va_list* args)
8241{
8242 sv_vcatpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
ef50df4b
GS
8243 SvSETMAGIC(sv);
8244}
8245
954c1994
GS
8246/*
8247=for apidoc sv_vsetpvfn
8248
8249Works like C<vcatpvfn> but copies the text into the SV instead of
8250appending it.
8251
645c22ef
DM
8252Usually used via one of its frontends C<sv_setpvf> and C<sv_setpvf_mg>.
8253
954c1994
GS
8254=cut
8255*/
8256
46fc3d4c 8257void
7d5ea4e7 8258Perl_sv_vsetpvfn(pTHX_ SV *sv, const char *pat, STRLEN patlen, va_list *args, SV **svargs, I32 svmax, bool *maybe_tainted)
46fc3d4c 8259{
8260 sv_setpvn(sv, "", 0);
7d5ea4e7 8261 sv_vcatpvfn(sv, pat, patlen, args, svargs, svmax, maybe_tainted);
46fc3d4c 8262}
8263
645c22ef
DM
8264/* private function for use in sv_vcatpvfn via the EXPECT_NUMBER macro */
8265
2d00ba3b 8266STATIC I32
9dd79c3f 8267S_expect_number(pTHX_ char** pattern)
211dfcf1
HS
8268{
8269 I32 var = 0;
8270 switch (**pattern) {
8271 case '1': case '2': case '3':
8272 case '4': case '5': case '6':
8273 case '7': case '8': case '9':
8274 while (isDIGIT(**pattern))
8275 var = var * 10 + (*(*pattern)++ - '0');
8276 }
8277 return var;
8278}
9dd79c3f 8279#define EXPECT_NUMBER(pattern, var) (var = S_expect_number(aTHX_ &pattern))
211dfcf1 8280
48703b5e
NC
8281static char *
8282F0convert(NV nv, char *endbuf, STRLEN *len)
8283{
8284 int neg = nv < 0;
8285 UV uv;
8286 char *p = endbuf;
8287
8288 if (neg)
8289 nv = -nv;
8290 if (nv < UV_MAX) {
8291 nv += 0.5;
1ed1e641 8292 uv = (UV)nv;
48703b5e
NC
8293 if (uv & 1 && uv == nv)
8294 uv--; /* Round to even */
8295 do {
8296 unsigned dig = uv % 10;
8297 *--p = '0' + dig;
8298 } while (uv /= 10);
8299 if (neg)
8300 *--p = '-';
8301 *len = endbuf - p;
8302 return p;
8303 }
8304 return Nullch;
8305}
8306
8307
954c1994
GS
8308/*
8309=for apidoc sv_vcatpvfn
8310
8311Processes its arguments like C<vsprintf> and appends the formatted output
8312to an SV. Uses an array of SVs if the C style variable argument list is
8313missing (NULL). When running with taint checks enabled, indicates via
8314C<maybe_tainted> if results are untrustworthy (often due to the use of
8315locales).
8316
645c22ef
DM
8317Usually used via one of its frontends C<sv_catpvf> and C<sv_catpvf_mg>.
8318
954c1994
GS
8319=cut
8320*/
8321
46fc3d4c 8322void
7d5ea4e7 8323Perl_sv_vcatpvfn(pTHX_ SV *sv, const char *pat, STRLEN patlen, va_list *args, SV **svargs, I32 svmax, bool *maybe_tainted)
46fc3d4c 8324{
8325 char *p;
8326 char *q;
8327 char *patend;
fc36a67e 8328 STRLEN origlen;
46fc3d4c 8329 I32 svix = 0;
c635e13b 8330 static char nullstr[] = "(null)";
9c5ffd7c 8331 SV *argsv = Nullsv;
5835a535
JH
8332 bool has_utf8; /* has the result utf8? */
8333 bool pat_utf8; /* the pattern is in utf8? */
8334 SV *nsv = Nullsv;
48703b5e
NC
8335 /* Times 4: a decimal digit takes more than 3 binary digits.
8336 * NV_DIG: mantissa takes than many decimal digits.
8337 * Plus 32: Playing safe. */
8338 char ebuf[IV_DIG * 4 + NV_DIG + 32];
8339 /* large enough for "%#.#f" --chip */
8340 /* what about long double NVs? --jhi */
5835a535
JH
8341
8342 has_utf8 = pat_utf8 = DO_UTF8(sv);
46fc3d4c 8343
8344 /* no matter what, this is a string now */
fc36a67e 8345 (void)SvPV_force(sv, origlen);
46fc3d4c 8346
fc36a67e 8347 /* special-case "", "%s", and "%_" */
46fc3d4c 8348 if (patlen == 0)
8349 return;
fc36a67e 8350 if (patlen == 2 && pat[0] == '%') {
8351 switch (pat[1]) {
8352 case 's':
c635e13b 8353 if (args) {
8354 char *s = va_arg(*args, char*);
8355 sv_catpv(sv, s ? s : nullstr);
8356 }
7e2040f0 8357 else if (svix < svmax) {
fc36a67e 8358 sv_catsv(sv, *svargs);
7e2040f0
GS
8359 if (DO_UTF8(*svargs))
8360 SvUTF8_on(sv);
8361 }
fc36a67e 8362 return;
8363 case '_':
8364 if (args) {
7e2040f0
GS
8365 argsv = va_arg(*args, SV*);
8366 sv_catsv(sv, argsv);
8367 if (DO_UTF8(argsv))
8368 SvUTF8_on(sv);
fc36a67e 8369 return;
8370 }
8371 /* See comment on '_' below */
8372 break;
8373 }
46fc3d4c 8374 }
8375
1621ce87 8376#ifndef USE_LONG_DOUBLE
48703b5e
NC
8377 /* special-case "%.<number>[gf]" */
8378 if ( patlen <= 5 && pat[0] == '%' && pat[1] == '.'
8379 && (pat[patlen-1] == 'g' || pat[patlen-1] == 'f') ) {
8380 unsigned digits = 0;
8381 const char *pp;
8382
8383 pp = pat + 2;
8384 while (*pp >= '0' && *pp <= '9')
8385 digits = 10 * digits + (*pp++ - '0');
1ed1e641 8386 if (pp - pat == (int)patlen - 1) {
48703b5e
NC
8387 NV nv;
8388
8389 if (args)
8390 nv = (NV)va_arg(*args, double);
8391 else if (svix < svmax)
8392 nv = SvNV(*svargs);
8393 else
8394 return;
8395 if (*pp == 'g') {
5f658b8d
NC
8396 /* Add check for digits != 0 because it seems that some
8397 gconverts are buggy in this case, and we don't yet have
8398 a Configure test for this. */
8399 if (digits && digits < sizeof(ebuf) - NV_DIG - 10) {
8400 /* 0, point, slack */
1621ce87 8401 Gconvert(nv, (int)digits, 0, ebuf);
48703b5e
NC
8402 sv_catpv(sv, ebuf);
8403 if (*ebuf) /* May return an empty string for digits==0 */
8404 return;
8405 }
8406 } else if (!digits) {
8407 STRLEN l;
8408
8409 if ((p = F0convert(nv, ebuf + sizeof ebuf, &l))) {
8410 sv_catpvn(sv, p, l);
8411 return;
8412 }
8413 }
8414 }
8415 }
1621ce87 8416#endif /* !USE_LONG_DOUBLE */
48703b5e 8417
2cf2cfc6 8418 if (!args && svix < svmax && DO_UTF8(*svargs))
5b7ea690 8419 has_utf8 = TRUE;
2cf2cfc6 8420
46fc3d4c 8421 patend = (char*)pat + patlen;
8422 for (p = (char*)pat; p < patend; p = q) {
8423 bool alt = FALSE;
8424 bool left = FALSE;
b22c7a20 8425 bool vectorize = FALSE;
211dfcf1 8426 bool vectorarg = FALSE;
2cf2cfc6 8427 bool vec_utf8 = FALSE;
46fc3d4c 8428 char fill = ' ';
8429 char plus = 0;
8430 char intsize = 0;
8431 STRLEN width = 0;
fc36a67e 8432 STRLEN zeros = 0;
46fc3d4c 8433 bool has_precis = FALSE;
8434 STRLEN precis = 0;
d34f9d2e 8435 I32 osvix = svix;
2cf2cfc6 8436 bool is_utf8 = FALSE; /* is this item utf8? */
5b7ea690
JH
8437#ifdef HAS_LDBL_SPRINTF_BUG
8438 /* This is to try to fix a bug with irix/nonstop-ux/powerux and
8439 with sfio - Allen <allens@cpan.org> */
8440 bool fix_ldbl_sprintf_bug = FALSE;
8441#endif
8442
46fc3d4c 8443 char esignbuf[4];
ad391ad9 8444 U8 utf8buf[UTF8_MAXLEN+1];
46fc3d4c 8445 STRLEN esignlen = 0;
8446
8447 char *eptr = Nullch;
fc36a67e 8448 STRLEN elen = 0;
81f715da 8449 SV *vecsv = Nullsv;
a05b299f 8450 U8 *vecstr = Null(U8*);
b22c7a20 8451 STRLEN veclen = 0;
934abaf1 8452 char c = 0;
46fc3d4c 8453 int i;
9c5ffd7c 8454 unsigned base = 0;
8c8eb53c
RB
8455 IV iv = 0;
8456 UV uv = 0;
9e5b023a
JH
8457 /* we need a long double target in case HAS_LONG_DOUBLE but
8458 not USE_LONG_DOUBLE
8459 */
35fff930 8460#if defined(HAS_LONG_DOUBLE) && LONG_DOUBLESIZE > DOUBLESIZE
9e5b023a
JH
8461 long double nv;
8462#else
65202027 8463 NV nv;
9e5b023a 8464#endif
46fc3d4c 8465 STRLEN have;
8466 STRLEN need;
8467 STRLEN gap;
b22c7a20
GS
8468 char *dotstr = ".";
8469 STRLEN dotstrlen = 1;
211dfcf1 8470 I32 efix = 0; /* explicit format parameter index */
eb3fce90 8471 I32 ewix = 0; /* explicit width index */
211dfcf1
HS
8472 I32 epix = 0; /* explicit precision index */
8473 I32 evix = 0; /* explicit vector index */
eb3fce90 8474 bool asterisk = FALSE;
46fc3d4c 8475
211dfcf1 8476 /* echo everything up to the next format specification */
46fc3d4c 8477 for (q = p; q < patend && *q != '%'; ++q) ;
8478 if (q > p) {
5835a535
JH
8479 if (has_utf8 && !pat_utf8)
8480 sv_catpvn_utf8_upgrade(sv, p, q - p, nsv);
8481 else
8482 sv_catpvn(sv, p, q - p);
46fc3d4c 8483 p = q;
8484 }
8485 if (q++ >= patend)
8486 break;
8487
211dfcf1
HS
8488/*
8489 We allow format specification elements in this order:
8490 \d+\$ explicit format parameter index
8491 [-+ 0#]+ flags
a472f209 8492 v|\*(\d+\$)?v vector with optional (optionally specified) arg
f8cf5370 8493 0 flag (as above): repeated to allow "v02"
211dfcf1
HS
8494 \d+|\*(\d+\$)? width using optional (optionally specified) arg
8495 \.(\d*|\*(\d+\$)?) precision using optional (optionally specified) arg
8496 [hlqLV] size
8497 [%bcdefginopsux_DFOUX] format (mandatory)
8498*/
8499 if (EXPECT_NUMBER(q, width)) {
8500 if (*q == '$') {
8501 ++q;
8502 efix = width;
8503 } else {
8504 goto gotwidth;
8505 }
8506 }
8507
fc36a67e 8508 /* FLAGS */
8509
46fc3d4c 8510 while (*q) {
8511 switch (*q) {
8512 case ' ':
8513 case '+':
8514 plus = *q++;
8515 continue;
8516
8517 case '-':
8518 left = TRUE;
8519 q++;
8520 continue;
8521
8522 case '0':
8523 fill = *q++;
8524 continue;
8525
8526 case '#':
8527 alt = TRUE;
8528 q++;
8529 continue;
8530
fc36a67e 8531 default:
8532 break;
8533 }
8534 break;
8535 }
46fc3d4c 8536
211dfcf1 8537 tryasterisk:
eb3fce90 8538 if (*q == '*') {
211dfcf1
HS
8539 q++;
8540 if (EXPECT_NUMBER(q, ewix))
8541 if (*q++ != '$')
8542 goto unknown;
eb3fce90 8543 asterisk = TRUE;
211dfcf1
HS
8544 }
8545 if (*q == 'v') {
eb3fce90 8546 q++;
211dfcf1
HS
8547 if (vectorize)
8548 goto unknown;
9cbac4c7 8549 if ((vectorarg = asterisk)) {
211dfcf1
HS
8550 evix = ewix;
8551 ewix = 0;
8552 asterisk = FALSE;
8553 }
8554 vectorize = TRUE;
8555 goto tryasterisk;
eb3fce90
JH
8556 }
8557
211dfcf1 8558 if (!asterisk)
f8cf5370
JH
8559 if( *q == '0' )
8560 fill = *q++;
211dfcf1
HS
8561 EXPECT_NUMBER(q, width);
8562
8563 if (vectorize) {
8564 if (vectorarg) {
8565 if (args)
8566 vecsv = va_arg(*args, SV*);
8567 else
8568 vecsv = (evix ? evix <= svmax : svix < svmax) ?
219ef941 8569 svargs[evix ? evix-1 : svix++] : &PL_sv_undef;
4459522c 8570 dotstr = SvPVx(vecsv, dotstrlen);
211dfcf1 8571 if (DO_UTF8(vecsv))
2cf2cfc6 8572 is_utf8 = TRUE;
211dfcf1
HS
8573 }
8574 if (args) {
8575 vecsv = va_arg(*args, SV*);
8576 vecstr = (U8*)SvPVx(vecsv,veclen);
2cf2cfc6 8577 vec_utf8 = DO_UTF8(vecsv);
eb3fce90 8578 }
211dfcf1
HS
8579 else if (efix ? efix <= svmax : svix < svmax) {
8580 vecsv = svargs[efix ? efix-1 : svix++];
8581 vecstr = (U8*)SvPVx(vecsv,veclen);
2cf2cfc6 8582 vec_utf8 = DO_UTF8(vecsv);
211dfcf1
HS
8583 }
8584 else {
8585 vecstr = (U8*)"";
8586 veclen = 0;
8587 }
eb3fce90 8588 }
fc36a67e 8589
eb3fce90 8590 if (asterisk) {
fc36a67e 8591 if (args)
8592 i = va_arg(*args, int);
8593 else
eb3fce90
JH
8594 i = (ewix ? ewix <= svmax : svix < svmax) ?
8595 SvIVx(svargs[ewix ? ewix-1 : svix++]) : 0;
fc36a67e 8596 left |= (i < 0);
8597 width = (i < 0) ? -i : i;
fc36a67e 8598 }
211dfcf1 8599 gotwidth:
fc36a67e 8600
8601 /* PRECISION */
46fc3d4c 8602
fc36a67e 8603 if (*q == '.') {
8604 q++;
8605 if (*q == '*') {
211dfcf1 8606 q++;
7b8dd722
HS
8607 if (EXPECT_NUMBER(q, epix) && *q++ != '$')
8608 goto unknown;
8609 /* XXX: todo, support specified precision parameter */
8610 if (epix)
211dfcf1 8611 goto unknown;
46fc3d4c 8612 if (args)
8613 i = va_arg(*args, int);
8614 else
eb3fce90
JH
8615 i = (ewix ? ewix <= svmax : svix < svmax)
8616 ? SvIVx(svargs[ewix ? ewix-1 : svix++]) : 0;
fc36a67e 8617 precis = (i < 0) ? 0 : i;
fc36a67e 8618 }
8619 else {
8620 precis = 0;
8621 while (isDIGIT(*q))
8622 precis = precis * 10 + (*q++ - '0');
8623 }
8624 has_precis = TRUE;
8625 }
46fc3d4c 8626
fc36a67e 8627 /* SIZE */
46fc3d4c 8628
fc36a67e 8629 switch (*q) {
c623ac67
GS
8630#ifdef WIN32
8631 case 'I': /* Ix, I32x, and I64x */
8632# ifdef WIN64
8633 if (q[1] == '6' && q[2] == '4') {
8634 q += 3;
8635 intsize = 'q';
8636 break;
8637 }
8638# endif
8639 if (q[1] == '3' && q[2] == '2') {
8640 q += 3;
8641 break;
8642 }
8643# ifdef WIN64
8644 intsize = 'q';
8645# endif
8646 q++;
8647 break;
8648#endif
9e5b023a 8649#if defined(HAS_QUAD) || defined(HAS_LONG_DOUBLE)
6f9bb7fd 8650 case 'L': /* Ld */
e5c81feb 8651 /* FALL THROUGH */
e5c81feb 8652#ifdef HAS_QUAD
6f9bb7fd 8653 case 'q': /* qd */
9e5b023a 8654#endif
6f9bb7fd
GS
8655 intsize = 'q';
8656 q++;
8657 break;
8658#endif
fc36a67e 8659 case 'l':
9e5b023a 8660#if defined(HAS_QUAD) || defined(HAS_LONG_DOUBLE)
5b7ea690 8661 if (*(q + 1) == 'l') { /* lld, llf */
fc36a67e 8662 intsize = 'q';
8663 q += 2;
46fc3d4c 8664 break;
cf2093f6 8665 }
fc36a67e 8666#endif
6f9bb7fd 8667 /* FALL THROUGH */
fc36a67e 8668 case 'h':
cf2093f6 8669 /* FALL THROUGH */
fc36a67e 8670 case 'V':
8671 intsize = *q++;
46fc3d4c 8672 break;
8673 }
8674
fc36a67e 8675 /* CONVERSION */
8676
211dfcf1
HS
8677 if (*q == '%') {
8678 eptr = q++;
8679 elen = 1;
8680 goto string;
8681 }
8682
be75b157
HS
8683 if (vectorize)
8684 argsv = vecsv;
8685 else if (!args)
211dfcf1
HS
8686 argsv = (efix ? efix <= svmax : svix < svmax) ?
8687 svargs[efix ? efix-1 : svix++] : &PL_sv_undef;
8688
46fc3d4c 8689 switch (c = *q++) {
8690
8691 /* STRINGS */
8692
46fc3d4c 8693 case 'c':
be75b157 8694 uv = (args && !vectorize) ? va_arg(*args, int) : SvIVx(argsv);
1bd104fb
JH
8695 if ((uv > 255 ||
8696 (!UNI_IS_INVARIANT(uv) && SvUTF8(sv)))
0064a8a9 8697 && !IN_BYTES) {
dfe13c55 8698 eptr = (char*)utf8buf;
9041c2e3 8699 elen = uvchr_to_utf8((U8*)eptr, uv) - utf8buf;
2cf2cfc6 8700 is_utf8 = TRUE;
7e2040f0
GS
8701 }
8702 else {
8703 c = (char)uv;
8704 eptr = &c;
8705 elen = 1;
a0ed51b3 8706 }
46fc3d4c 8707 goto string;
8708
46fc3d4c 8709 case 's':
be75b157 8710 if (args && !vectorize) {
fc36a67e 8711 eptr = va_arg(*args, char*);
c635e13b 8712 if (eptr)
1d7c1841
GS
8713#ifdef MACOS_TRADITIONAL
8714 /* On MacOS, %#s format is used for Pascal strings */
8715 if (alt)
8716 elen = *eptr++;
8717 else
8718#endif
c635e13b 8719 elen = strlen(eptr);
8720 else {
8721 eptr = nullstr;
8722 elen = sizeof nullstr - 1;
8723 }
46fc3d4c 8724 }
211dfcf1 8725 else {
7e2040f0
GS
8726 eptr = SvPVx(argsv, elen);
8727 if (DO_UTF8(argsv)) {
a0ed51b3
LW
8728 if (has_precis && precis < elen) {
8729 I32 p = precis;
7e2040f0 8730 sv_pos_u2b(argsv, &p, 0); /* sticks at end */
a0ed51b3
LW
8731 precis = p;
8732 }
8733 if (width) { /* fudge width (can't fudge elen) */
7e2040f0 8734 width += elen - sv_len_utf8(argsv);
a0ed51b3 8735 }
2cf2cfc6 8736 is_utf8 = TRUE;
a0ed51b3
LW
8737 }
8738 }
46fc3d4c 8739 goto string;
8740
fc36a67e 8741 case '_':
8742 /*
8743 * The "%_" hack might have to be changed someday,
8744 * if ISO or ANSI decide to use '_' for something.
8745 * So we keep it hidden from users' code.
8746 */
be75b157 8747 if (!args || vectorize)
fc36a67e 8748 goto unknown;
211dfcf1 8749 argsv = va_arg(*args, SV*);
7e2040f0
GS
8750 eptr = SvPVx(argsv, elen);
8751 if (DO_UTF8(argsv))
2cf2cfc6 8752 is_utf8 = TRUE;
fc36a67e 8753
46fc3d4c 8754 string:
b22c7a20 8755 vectorize = FALSE;
46fc3d4c 8756 if (has_precis && elen > precis)
8757 elen = precis;
8758 break;
8759
8760 /* INTEGERS */
8761
fc36a67e 8762 case 'p':
be75b157 8763 if (alt || vectorize)
c2e66d9e 8764 goto unknown;
211dfcf1 8765 uv = PTR2UV(args ? va_arg(*args, void*) : argsv);
fc36a67e 8766 base = 16;
8767 goto integer;
8768
46fc3d4c 8769 case 'D':
29fe7a80 8770#ifdef IV_IS_QUAD
22f3ae8c 8771 intsize = 'q';
29fe7a80 8772#else
46fc3d4c 8773 intsize = 'l';
29fe7a80 8774#endif
46fc3d4c 8775 /* FALL THROUGH */
8776 case 'd':
8777 case 'i':
b22c7a20 8778 if (vectorize) {
ba210ebe 8779 STRLEN ulen;
211dfcf1
HS
8780 if (!veclen)
8781 continue;
2cf2cfc6
A
8782 if (vec_utf8)
8783 uv = utf8n_to_uvchr(vecstr, veclen, &ulen,
8784 UTF8_ALLOW_ANYUV);
b22c7a20 8785 else {
e83d50c9 8786 uv = *vecstr;
b22c7a20
GS
8787 ulen = 1;
8788 }
8789 vecstr += ulen;
8790 veclen -= ulen;
e83d50c9
JP
8791 if (plus)
8792 esignbuf[esignlen++] = plus;
b22c7a20
GS
8793 }
8794 else if (args) {
46fc3d4c 8795 switch (intsize) {
8796 case 'h': iv = (short)va_arg(*args, int); break;
46fc3d4c 8797 case 'l': iv = va_arg(*args, long); break;
fc36a67e 8798 case 'V': iv = va_arg(*args, IV); break;
1ed1e641 8799 default: iv = va_arg(*args, int); break;
cf2093f6
JH
8800#ifdef HAS_QUAD
8801 case 'q': iv = va_arg(*args, Quad_t); break;
8802#endif
46fc3d4c 8803 }
8804 }
8805 else {
1ed1e641 8806 IV tiv = SvIVx(argsv); /* work around GCC bug #13488 */
46fc3d4c 8807 switch (intsize) {
1ed1e641
MHM
8808 case 'h': iv = (short)tiv; break;
8809 case 'l': iv = (long)tiv; break;
8810 case 'V':
8811 default: iv = tiv; break;
cf2093f6 8812#ifdef HAS_QUAD
1ed1e641 8813 case 'q': iv = (Quad_t)tiv; break;
cf2093f6 8814#endif
46fc3d4c 8815 }
8816 }
e83d50c9
JP
8817 if ( !vectorize ) /* we already set uv above */
8818 {
8819 if (iv >= 0) {
8820 uv = iv;
8821 if (plus)
8822 esignbuf[esignlen++] = plus;
8823 }
8824 else {
8825 uv = -iv;
8826 esignbuf[esignlen++] = '-';
8827 }
46fc3d4c 8828 }
8829 base = 10;
8830 goto integer;
8831
fc36a67e 8832 case 'U':
29fe7a80 8833#ifdef IV_IS_QUAD
22f3ae8c 8834 intsize = 'q';
29fe7a80 8835#else
fc36a67e 8836 intsize = 'l';
29fe7a80 8837#endif
fc36a67e 8838 /* FALL THROUGH */
8839 case 'u':
8840 base = 10;
8841 goto uns_integer;
8842
4f19785b
WSI
8843 case 'b':
8844 base = 2;
8845 goto uns_integer;
8846
46fc3d4c 8847 case 'O':
29fe7a80 8848#ifdef IV_IS_QUAD
22f3ae8c 8849 intsize = 'q';
29fe7a80 8850#else
46fc3d4c 8851 intsize = 'l';
29fe7a80 8852#endif
46fc3d4c 8853 /* FALL THROUGH */
8854 case 'o':
8855 base = 8;
8856 goto uns_integer;
8857
8858 case 'X':
46fc3d4c 8859 case 'x':
8860 base = 16;
46fc3d4c 8861
8862 uns_integer:
b22c7a20 8863 if (vectorize) {
ba210ebe 8864 STRLEN ulen;
b22c7a20 8865 vector:
211dfcf1
HS
8866 if (!veclen)
8867 continue;
2cf2cfc6
A
8868 if (vec_utf8)
8869 uv = utf8n_to_uvchr(vecstr, veclen, &ulen,
8870 UTF8_ALLOW_ANYUV);
b22c7a20 8871 else {
a05b299f 8872 uv = *vecstr;
b22c7a20
GS
8873 ulen = 1;
8874 }
8875 vecstr += ulen;
8876 veclen -= ulen;
8877 }
8878 else if (args) {
46fc3d4c 8879 switch (intsize) {
8880 case 'h': uv = (unsigned short)va_arg(*args, unsigned); break;
46fc3d4c 8881 case 'l': uv = va_arg(*args, unsigned long); break;
fc36a67e 8882 case 'V': uv = va_arg(*args, UV); break;
1ed1e641 8883 default: uv = va_arg(*args, unsigned); break;
cf2093f6 8884#ifdef HAS_QUAD
1ed1e641 8885 case 'q': uv = va_arg(*args, Uquad_t); break;
cf2093f6 8886#endif
46fc3d4c 8887 }
8888 }
8889 else {
1ed1e641 8890 UV tuv = SvUVx(argsv); /* work around GCC bug #13488 */
46fc3d4c 8891 switch (intsize) {
1ed1e641
MHM
8892 case 'h': uv = (unsigned short)tuv; break;
8893 case 'l': uv = (unsigned long)tuv; break;
8894 case 'V':
8895 default: uv = tuv; break;
cf2093f6 8896#ifdef HAS_QUAD
1ed1e641 8897 case 'q': uv = (Uquad_t)tuv; break;
cf2093f6 8898#endif
46fc3d4c 8899 }
8900 }
8901
8902 integer:
46fc3d4c 8903 eptr = ebuf + sizeof ebuf;
fc36a67e 8904 switch (base) {
8905 unsigned dig;
8906 case 16:
c10ed8b9
HS
8907 if (!uv)
8908 alt = FALSE;
1d7c1841
GS
8909 p = (char*)((c == 'X')
8910 ? "0123456789ABCDEF" : "0123456789abcdef");
fc36a67e 8911 do {
8912 dig = uv & 15;
8913 *--eptr = p[dig];
8914 } while (uv >>= 4);
8915 if (alt) {
46fc3d4c 8916 esignbuf[esignlen++] = '0';
fc36a67e 8917 esignbuf[esignlen++] = c; /* 'x' or 'X' */
46fc3d4c 8918 }
fc36a67e 8919 break;
8920 case 8:
8921 do {
8922 dig = uv & 7;
8923 *--eptr = '0' + dig;
8924 } while (uv >>= 3);
8925 if (alt && *eptr != '0')
8926 *--eptr = '0';
8927 break;
4f19785b
WSI
8928 case 2:
8929 do {
8930 dig = uv & 1;
8931 *--eptr = '0' + dig;
8932 } while (uv >>= 1);
eda88b6d
JH
8933 if (alt) {
8934 esignbuf[esignlen++] = '0';
7481bb52 8935 esignbuf[esignlen++] = 'b';
eda88b6d 8936 }
4f19785b 8937 break;
fc36a67e 8938 default: /* it had better be ten or less */
6bc102ca 8939#if defined(PERL_Y2KWARN)
e476b1b5 8940 if (ckWARN(WARN_Y2K)) {
6bc102ca
GS
8941 STRLEN n;
8942 char *s = SvPV(sv,n);
8943 if (n >= 2 && s[n-2] == '1' && s[n-1] == '9'
8944 && (n == 2 || !isDIGIT(s[n-3])))
8945 {
9014280d 8946 Perl_warner(aTHX_ packWARN(WARN_Y2K),
6bc102ca
GS
8947 "Possible Y2K bug: %%%c %s",
8948 c, "format string following '19'");
8949 }
8950 }
8951#endif
fc36a67e 8952 do {
8953 dig = uv % base;
8954 *--eptr = '0' + dig;
8955 } while (uv /= base);
8956 break;
46fc3d4c 8957 }
8958 elen = (ebuf + sizeof ebuf) - eptr;
c10ed8b9
HS
8959 if (has_precis) {
8960 if (precis > elen)
8961 zeros = precis - elen;
8962 else if (precis == 0 && elen == 1 && *eptr == '0')
8963 elen = 0;
8964 }
46fc3d4c 8965 break;
8966
8967 /* FLOATING POINT */
8968
fc36a67e 8969 case 'F':
8970 c = 'f'; /* maybe %F isn't supported here */
8971 /* FALL THROUGH */
46fc3d4c 8972 case 'e': case 'E':
fc36a67e 8973 case 'f':
46fc3d4c 8974 case 'g': case 'G':
8975
8976 /* This is evil, but floating point is even more evil */
8977
9e5b023a
JH
8978 /* for SV-style calling, we can only get NV
8979 for C-style calling, we assume %f is double;
8980 for simplicity we allow any of %Lf, %llf, %qf for long double
8981 */
8982 switch (intsize) {
8983 case 'V':
8984#if defined(USE_LONG_DOUBLE)
8985 intsize = 'q';
8986#endif
8987 break;
f8cf5370
JH
8988/* [perl #20339] - we should accept and ignore %lf rather than die */
8989 case 'l':
8990 /* FALL THROUGH */
9e5b023a
JH
8991 default:
8992#if defined(USE_LONG_DOUBLE)
8993 intsize = args ? 0 : 'q';
8994#endif
8995 break;
8996 case 'q':
8997#if defined(HAS_LONG_DOUBLE)
8998 break;
8999#else
9000 /* FALL THROUGH */
9001#endif
9002 case 'h':
9e5b023a
JH
9003 goto unknown;
9004 }
9005
9006 /* now we need (long double) if intsize == 'q', else (double) */
be75b157 9007 nv = (args && !vectorize) ?
35fff930
JH
9008#if LONG_DOUBLESIZE > DOUBLESIZE
9009 intsize == 'q' ?
5b7ea690
JH
9010 va_arg(*args, long double) :
9011 va_arg(*args, double)
35fff930 9012#else
5b7ea690 9013 va_arg(*args, double)
35fff930 9014#endif
9e5b023a 9015 : SvNVx(argsv);
fc36a67e 9016
9017 need = 0;
be75b157 9018 vectorize = FALSE;
fc36a67e 9019 if (c != 'e' && c != 'E') {
9020 i = PERL_INT_MIN;
9e5b023a
JH
9021 /* FIXME: if HAS_LONG_DOUBLE but not USE_LONG_DOUBLE this
9022 will cast our (long double) to (double) */
73b309ea 9023 (void)Perl_frexp(nv, &i);
fc36a67e 9024 if (i == PERL_INT_MIN)
cea2e8a9 9025 Perl_die(aTHX_ "panic: frexp");
c635e13b 9026 if (i > 0)
fc36a67e 9027 need = BIT_DIGITS(i);
9028 }
9029 need += has_precis ? precis : 6; /* known default */
5b7ea690 9030
fc36a67e 9031 if (need < width)
9032 need = width;
9033
5b7ea690
JH
9034#ifdef HAS_LDBL_SPRINTF_BUG
9035 /* This is to try to fix a bug with irix/nonstop-ux/powerux and
9036 with sfio - Allen <allens@cpan.org> */
9037
9038# ifdef DBL_MAX
9039# define MY_DBL_MAX DBL_MAX
9040# else /* XXX guessing! HUGE_VAL may be defined as infinity, so not using */
9041# if DOUBLESIZE >= 8
9042# define MY_DBL_MAX 1.7976931348623157E+308L
9043# else
9044# define MY_DBL_MAX 3.40282347E+38L
9045# endif
9046# endif
9047
9048# ifdef HAS_LDBL_SPRINTF_BUG_LESS1 /* only between -1L & 1L - Allen */
9049# define MY_DBL_MAX_BUG 1L
9050# else
9051# define MY_DBL_MAX_BUG MY_DBL_MAX
9052# endif
9053
9054# ifdef DBL_MIN
9055# define MY_DBL_MIN DBL_MIN
9056# else /* XXX guessing! -Allen */
9057# if DOUBLESIZE >= 8
9058# define MY_DBL_MIN 2.2250738585072014E-308L
9059# else
9060# define MY_DBL_MIN 1.17549435E-38L
9061# endif
9062# endif
9063
9064 if ((intsize == 'q') && (c == 'f') &&
9065 ((nv < MY_DBL_MAX_BUG) && (nv > -MY_DBL_MAX_BUG)) &&
9066 (need < DBL_DIG)) {
9067 /* it's going to be short enough that
9068 * long double precision is not needed */
9069
9070 if ((nv <= 0L) && (nv >= -0L))
9071 fix_ldbl_sprintf_bug = TRUE; /* 0 is 0 - easiest */
9072 else {
9073 /* would use Perl_fp_class as a double-check but not
9074 * functional on IRIX - see perl.h comments */
9075
9076 if ((nv >= MY_DBL_MIN) || (nv <= -MY_DBL_MIN)) {
9077 /* It's within the range that a double can represent */
9078#if defined(DBL_MAX) && !defined(DBL_MIN)
9079 if ((nv >= ((long double)1/DBL_MAX)) ||
9080 (nv <= (-(long double)1/DBL_MAX)))
9081#endif
9082 fix_ldbl_sprintf_bug = TRUE;
9083 }
9084 }
9085 if (fix_ldbl_sprintf_bug == TRUE) {
9086 double temp;
9087
9088 intsize = 0;
9089 temp = (double)nv;
9090 nv = (NV)temp;
9091 }
9092 }
9093
9094# undef MY_DBL_MAX
9095# undef MY_DBL_MAX_BUG
9096# undef MY_DBL_MIN
9097
9098#endif /* HAS_LDBL_SPRINTF_BUG */
9099
46fc3d4c 9100 need += 20; /* fudge factor */
80252599
GS
9101 if (PL_efloatsize < need) {
9102 Safefree(PL_efloatbuf);
9103 PL_efloatsize = need + 20; /* more fudge */
9104 New(906, PL_efloatbuf, PL_efloatsize, char);
7d5ea4e7 9105 PL_efloatbuf[0] = '\0';
46fc3d4c 9106 }
9107
48703b5e
NC
9108 if ( !(width || left || plus || alt) && fill != '0'
9109 && has_precis && intsize != 'q' ) { /* Shortcuts */
5f658b8d
NC
9110 /* See earlier comment about buggy Gconvert when digits,
9111 aka precis is 0 */
9112 if ( c == 'g' && precis) {
1621ce87 9113 Gconvert((NV)nv, (int)precis, 0, PL_efloatbuf);
48703b5e
NC
9114 if (*PL_efloatbuf) /* May return an empty string for digits==0 */
9115 goto float_converted;
9116 } else if ( c == 'f' && !precis) {
9117 if ((eptr = F0convert(nv, ebuf + sizeof ebuf, &elen)))
9118 break;
9119 }
9120 }
46fc3d4c 9121 eptr = ebuf + sizeof ebuf;
9122 *--eptr = '\0';
9123 *--eptr = c;
9e5b023a
JH
9124 /* FIXME: what to do if HAS_LONG_DOUBLE but not PERL_PRIfldbl? */
9125#if defined(HAS_LONG_DOUBLE) && defined(PERL_PRIfldbl)
9126 if (intsize == 'q') {
e5c81feb
JH
9127 /* Copy the one or more characters in a long double
9128 * format before the 'base' ([efgEFG]) character to
9129 * the format string. */
9130 static char const prifldbl[] = PERL_PRIfldbl;
9131 char const *p = prifldbl + sizeof(prifldbl) - 3;
9132 while (p >= prifldbl) { *--eptr = *p--; }
cf2093f6 9133 }
65202027 9134#endif
46fc3d4c 9135 if (has_precis) {
9136 base = precis;
9137 do { *--eptr = '0' + (base % 10); } while (base /= 10);
9138 *--eptr = '.';
9139 }
9140 if (width) {
9141 base = width;
9142 do { *--eptr = '0' + (base % 10); } while (base /= 10);
9143 }
9144 if (fill == '0')
9145 *--eptr = fill;
84902520
TB
9146 if (left)
9147 *--eptr = '-';
46fc3d4c 9148 if (plus)
9149 *--eptr = plus;
9150 if (alt)
9151 *--eptr = '#';
9152 *--eptr = '%';
9153
ff9121f8
JH
9154 /* No taint. Otherwise we are in the strange situation
9155 * where printf() taints but print($float) doesn't.
bda0f7a5 9156 * --jhi */
9e5b023a
JH
9157#if defined(HAS_LONG_DOUBLE)
9158 if (intsize == 'q')
9159 (void)sprintf(PL_efloatbuf, eptr, nv);
9160 else
9161 (void)sprintf(PL_efloatbuf, eptr, (double)nv);
9162#else
dd8482fc 9163 (void)sprintf(PL_efloatbuf, eptr, nv);
9e5b023a 9164#endif
48703b5e 9165 float_converted:
80252599
GS
9166 eptr = PL_efloatbuf;
9167 elen = strlen(PL_efloatbuf);
46fc3d4c 9168 break;
9169
fc36a67e 9170 /* SPECIAL */
9171
9172 case 'n':
9173 i = SvCUR(sv) - origlen;
be75b157 9174 if (args && !vectorize) {
c635e13b 9175 switch (intsize) {
9176 case 'h': *(va_arg(*args, short*)) = i; break;
9177 default: *(va_arg(*args, int*)) = i; break;
9178 case 'l': *(va_arg(*args, long*)) = i; break;
9179 case 'V': *(va_arg(*args, IV*)) = i; break;
cf2093f6
JH
9180#ifdef HAS_QUAD
9181 case 'q': *(va_arg(*args, Quad_t*)) = i; break;
9182#endif
c635e13b 9183 }
fc36a67e 9184 }
9dd79c3f 9185 else
211dfcf1 9186 sv_setuv_mg(argsv, (UV)i);
be75b157 9187 vectorize = FALSE;
fc36a67e 9188 continue; /* not "break" */
9189
9190 /* UNKNOWN */
9191
46fc3d4c 9192 default:
fc36a67e 9193 unknown:
599cee73 9194 if (!args && ckWARN(WARN_PRINTF) &&
533c011a 9195 (PL_op->op_type == OP_PRTF || PL_op->op_type == OP_SPRINTF)) {
c635e13b 9196 SV *msg = sv_newmortal();
c293eb2b
NC
9197 Perl_sv_setpvf(aTHX_ msg, "Invalid conversion in %sprintf: ",
9198 (PL_op->op_type == OP_PRTF) ? "" : "s");
0f4b6630 9199 if (c) {
0f4b6630 9200 if (isPRINT(c))
1c846c1f 9201 Perl_sv_catpvf(aTHX_ msg,
0f4b6630
JH
9202 "\"%%%c\"", c & 0xFF);
9203 else
9204 Perl_sv_catpvf(aTHX_ msg,
57def98f 9205 "\"%%\\%03"UVof"\"",
0f4b6630 9206 (UV)c & 0xFF);
0f4b6630 9207 } else
c635e13b 9208 sv_catpv(msg, "end of string");
9014280d 9209 Perl_warner(aTHX_ packWARN(WARN_PRINTF), "%"SVf, msg); /* yes, this is reentrant */
c635e13b 9210 }
fb73857a 9211
9212 /* output mangled stuff ... */
9213 if (c == '\0')
9214 --q;
46fc3d4c 9215 eptr = p;
9216 elen = q - p;
fb73857a 9217
9218 /* ... right here, because formatting flags should not apply */
9219 SvGROW(sv, SvCUR(sv) + elen + 1);
9220 p = SvEND(sv);
4459522c 9221 Copy(eptr, p, elen, char);
fb73857a 9222 p += elen;
9223 *p = '\0';
9224 SvCUR(sv) = p - SvPVX(sv);
d34f9d2e 9225 svix = osvix;
fb73857a 9226 continue; /* not "break" */
46fc3d4c 9227 }
9228
29359924
HS
9229 /* calculate width before utf8_upgrade changes it */
9230 have = esignlen + zeros + elen;
9231
d2876be5
JH
9232 if (is_utf8 != has_utf8) {
9233 if (is_utf8) {
9234 if (SvCUR(sv))
9235 sv_utf8_upgrade(sv);
9236 }
9237 else {
9238 SV *nsv = sv_2mortal(newSVpvn(eptr, elen));
9239 sv_utf8_upgrade(nsv);
9240 eptr = SvPVX(nsv);
9241 elen = SvCUR(nsv);
9242 }
9243 SvGROW(sv, SvCUR(sv) + elen + 1);
9244 p = SvEND(sv);
9245 *p = '\0';
9246 }
18729d3e
JH
9247 /* Use memchr() instead of strchr(), as eptr is not guaranteed */
9248 /* to point to a null-terminated string. */
9249 if (left && ckWARN(WARN_PRINTF) && memchr(eptr, '\n', elen) &&
67bae6b3
JH
9250 (PL_op->op_type == OP_PRTF || PL_op->op_type == OP_SPRINTF))
9251 Perl_warner(aTHX_ packWARN(WARN_PRINTF),
9252 "Newline in left-justified string for %sprintf",
9253 (PL_op->op_type == OP_PRTF) ? "" : "s");
d2876be5 9254
46fc3d4c 9255 need = (have > width ? have : width);
9256 gap = need - have;
9257
b22c7a20 9258 SvGROW(sv, SvCUR(sv) + need + dotstrlen + 1);
46fc3d4c 9259 p = SvEND(sv);
9260 if (esignlen && fill == '0') {
eb160463 9261 for (i = 0; i < (int)esignlen; i++)
46fc3d4c 9262 *p++ = esignbuf[i];
9263 }
9264 if (gap && !left) {
9265 memset(p, fill, gap);
9266 p += gap;
9267 }
9268 if (esignlen && fill != '0') {
eb160463 9269 for (i = 0; i < (int)esignlen; i++)
46fc3d4c 9270 *p++ = esignbuf[i];
9271 }
fc36a67e 9272 if (zeros) {
9273 for (i = zeros; i; i--)
9274 *p++ = '0';
9275 }
46fc3d4c 9276 if (elen) {
4459522c 9277 Copy(eptr, p, elen, char);
46fc3d4c 9278 p += elen;
9279 }
9280 if (gap && left) {
9281 memset(p, ' ', gap);
9282 p += gap;
9283 }
b22c7a20
GS
9284 if (vectorize) {
9285 if (veclen) {
4459522c 9286 Copy(dotstr, p, dotstrlen, char);
b22c7a20
GS
9287 p += dotstrlen;
9288 }
9289 else
9290 vectorize = FALSE; /* done iterating over vecstr */
9291 }
2cf2cfc6
A
9292 if (is_utf8)
9293 has_utf8 = TRUE;
9294 if (has_utf8)
7e2040f0 9295 SvUTF8_on(sv);
46fc3d4c 9296 *p = '\0';
9297 SvCUR(sv) = p - SvPVX(sv);
b22c7a20
GS
9298 if (vectorize) {
9299 esignlen = 0;
9300 goto vector;
9301 }
46fc3d4c 9302 }
9303}
51371543 9304
645c22ef
DM
9305/* =========================================================================
9306
9307=head1 Cloning an interpreter
9308
9309All the macros and functions in this section are for the private use of
9310the main function, perl_clone().
9311
9312The foo_dup() functions make an exact copy of an existing foo thinngy.
9313During the course of a cloning, a hash table is used to map old addresses
9314to new addresses. The table is created and manipulated with the
9315ptr_table_* functions.
9316
9317=cut
9318
9319============================================================================*/
9320
9321
1d7c1841
GS
9322#if defined(USE_ITHREADS)
9323
4d1ff10f
AB
9324#if defined(USE_5005THREADS)
9325# include "error: USE_5005THREADS and USE_ITHREADS are incompatible"
1d7c1841
GS
9326#endif
9327
1d7c1841
GS
9328#ifndef GpREFCNT_inc
9329# define GpREFCNT_inc(gp) ((gp) ? (++(gp)->gp_refcnt, (gp)) : (GP*)NULL)
9330#endif
9331
9332
d2d73c3e
AB
9333#define sv_dup_inc(s,t) SvREFCNT_inc(sv_dup(s,t))
9334#define av_dup(s,t) (AV*)sv_dup((SV*)s,t)
9335#define av_dup_inc(s,t) (AV*)SvREFCNT_inc(sv_dup((SV*)s,t))
9336#define hv_dup(s,t) (HV*)sv_dup((SV*)s,t)
9337#define hv_dup_inc(s,t) (HV*)SvREFCNT_inc(sv_dup((SV*)s,t))
9338#define cv_dup(s,t) (CV*)sv_dup((SV*)s,t)
9339#define cv_dup_inc(s,t) (CV*)SvREFCNT_inc(sv_dup((SV*)s,t))
9340#define io_dup(s,t) (IO*)sv_dup((SV*)s,t)
9341#define io_dup_inc(s,t) (IO*)SvREFCNT_inc(sv_dup((SV*)s,t))
9342#define gv_dup(s,t) (GV*)sv_dup((SV*)s,t)
9343#define gv_dup_inc(s,t) (GV*)SvREFCNT_inc(sv_dup((SV*)s,t))
1d7c1841
GS
9344#define SAVEPV(p) (p ? savepv(p) : Nullch)
9345#define SAVEPVN(p,n) (p ? savepvn(p,n) : Nullch)
8cf8f3d1 9346
d2d73c3e 9347
d2f185dc
AMS
9348/* Duplicate a regexp. Required reading: pregcomp() and pregfree() in
9349 regcomp.c. AMS 20010712 */
645c22ef 9350
1d7c1841 9351REGEXP *
a8fc9800 9352Perl_re_dup(pTHX_ REGEXP *r, CLONE_PARAMS *param)
1d7c1841 9353{
d2f185dc
AMS
9354 REGEXP *ret;
9355 int i, len, npar;
9356 struct reg_substr_datum *s;
9357
9358 if (!r)
9359 return (REGEXP *)NULL;
9360
9361 if ((ret = (REGEXP *)ptr_table_fetch(PL_ptr_table, r)))
9362 return ret;
9363
9364 len = r->offsets[0];
9365 npar = r->nparens+1;
9366
9367 Newc(0, ret, sizeof(regexp) + (len+1)*sizeof(regnode), char, regexp);
9368 Copy(r->program, ret->program, len+1, regnode);
9369
9370 New(0, ret->startp, npar, I32);
9371 Copy(r->startp, ret->startp, npar, I32);
9372 New(0, ret->endp, npar, I32);
9373 Copy(r->startp, ret->startp, npar, I32);
9374
d2f185dc
AMS
9375 New(0, ret->substrs, 1, struct reg_substr_data);
9376 for (s = ret->substrs->data, i = 0; i < 3; i++, s++) {
9377 s->min_offset = r->substrs->data[i].min_offset;
9378 s->max_offset = r->substrs->data[i].max_offset;
9379 s->substr = sv_dup_inc(r->substrs->data[i].substr, param);
33b8afdf 9380 s->utf8_substr = sv_dup_inc(r->substrs->data[i].utf8_substr, param);
d2f185dc
AMS
9381 }
9382
70612e96 9383 ret->regstclass = NULL;
d2f185dc
AMS
9384 if (r->data) {
9385 struct reg_data *d;
9386 int count = r->data->count;
9387
9388 Newc(0, d, sizeof(struct reg_data) + count*sizeof(void *),
9389 char, struct reg_data);
9390 New(0, d->what, count, U8);
9391
9392 d->count = count;
9393 for (i = 0; i < count; i++) {
9394 d->what[i] = r->data->what[i];
9395 switch (d->what[i]) {
9396 case 's':
9397 d->data[i] = sv_dup_inc((SV *)r->data->data[i], param);
9398 break;
9399 case 'p':
9400 d->data[i] = av_dup_inc((AV *)r->data->data[i], param);
9401 break;
9402 case 'f':
9403 /* This is cheating. */
9404 New(0, d->data[i], 1, struct regnode_charclass_class);
9405 StructCopy(r->data->data[i], d->data[i],
9406 struct regnode_charclass_class);
70612e96 9407 ret->regstclass = (regnode*)d->data[i];
d2f185dc
AMS
9408 break;
9409 case 'o':
33773810
AMS
9410 /* Compiled op trees are readonly, and can thus be
9411 shared without duplication. */
9b978d73
DM
9412 d->data[i] = (void*)OpREFCNT_inc((OP*)r->data->data[i]);
9413 break;
d2f185dc
AMS
9414 case 'n':
9415 d->data[i] = r->data->data[i];
9416 break;
9417 }
9418 }
9419
9420 ret->data = d;
9421 }
9422 else
9423 ret->data = NULL;
9424
9425 New(0, ret->offsets, 2*len+1, U32);
9426 Copy(r->offsets, ret->offsets, 2*len+1, U32);
9427
65b7047c 9428 ret->precomp = SAVEPVN(r->precomp, r->prelen);
d2f185dc
AMS
9429 ret->refcnt = r->refcnt;
9430 ret->minlen = r->minlen;
9431 ret->prelen = r->prelen;
9432 ret->nparens = r->nparens;
9433 ret->lastparen = r->lastparen;
9434 ret->lastcloseparen = r->lastcloseparen;
9435 ret->reganch = r->reganch;
9436
70612e96
RG
9437 ret->sublen = r->sublen;
9438
9439 if (RX_MATCH_COPIED(ret))
65b7047c 9440 ret->subbeg = SAVEPVN(r->subbeg, r->sublen);
70612e96
RG
9441 else
9442 ret->subbeg = Nullch;
9443
d2f185dc
AMS
9444 ptr_table_store(PL_ptr_table, r, ret);
9445 return ret;
1d7c1841
GS
9446}
9447
d2d73c3e 9448/* duplicate a file handle */
645c22ef 9449
1d7c1841 9450PerlIO *
a8fc9800 9451Perl_fp_dup(pTHX_ PerlIO *fp, char type, CLONE_PARAMS *param)
1d7c1841
GS
9452{
9453 PerlIO *ret;
9454 if (!fp)
9455 return (PerlIO*)NULL;
9456
9457 /* look for it in the table first */
9458 ret = (PerlIO*)ptr_table_fetch(PL_ptr_table, fp);
9459 if (ret)
9460 return ret;
9461
9462 /* create anew and remember what it is */
ecdeb87c 9463 ret = PerlIO_fdupopen(aTHX_ fp, param, PERLIO_DUP_CLONE);
1d7c1841
GS
9464 ptr_table_store(PL_ptr_table, fp, ret);
9465 return ret;
9466}
9467
645c22ef
DM
9468/* duplicate a directory handle */
9469
1d7c1841
GS
9470DIR *
9471Perl_dirp_dup(pTHX_ DIR *dp)
9472{
9473 if (!dp)
9474 return (DIR*)NULL;
9475 /* XXX TODO */
9476 return dp;
9477}
9478
ff276b08 9479/* duplicate a typeglob */
645c22ef 9480
1d7c1841 9481GP *
a8fc9800 9482Perl_gp_dup(pTHX_ GP *gp, CLONE_PARAMS* param)
1d7c1841
GS
9483{
9484 GP *ret;
9485 if (!gp)
9486 return (GP*)NULL;
9487 /* look for it in the table first */
9488 ret = (GP*)ptr_table_fetch(PL_ptr_table, gp);
9489 if (ret)
9490 return ret;
9491
9492 /* create anew and remember what it is */
9493 Newz(0, ret, 1, GP);
9494 ptr_table_store(PL_ptr_table, gp, ret);
9495
9496 /* clone */
9497 ret->gp_refcnt = 0; /* must be before any other dups! */
d2d73c3e
AB
9498 ret->gp_sv = sv_dup_inc(gp->gp_sv, param);
9499 ret->gp_io = io_dup_inc(gp->gp_io, param);
9500 ret->gp_form = cv_dup_inc(gp->gp_form, param);
9501 ret->gp_av = av_dup_inc(gp->gp_av, param);
9502 ret->gp_hv = hv_dup_inc(gp->gp_hv, param);
9503 ret->gp_egv = gv_dup(gp->gp_egv, param);/* GvEGV is not refcounted */
9504 ret->gp_cv = cv_dup_inc(gp->gp_cv, param);
1d7c1841
GS
9505 ret->gp_cvgen = gp->gp_cvgen;
9506 ret->gp_flags = gp->gp_flags;
9507 ret->gp_line = gp->gp_line;
9508 ret->gp_file = gp->gp_file; /* points to COP.cop_file */
9509 return ret;
9510}
9511
645c22ef
DM
9512/* duplicate a chain of magic */
9513
1d7c1841 9514MAGIC *
a8fc9800 9515Perl_mg_dup(pTHX_ MAGIC *mg, CLONE_PARAMS* param)
1d7c1841 9516{
cb359b41
JH
9517 MAGIC *mgprev = (MAGIC*)NULL;
9518 MAGIC *mgret;
1d7c1841
GS
9519 if (!mg)
9520 return (MAGIC*)NULL;
9521 /* look for it in the table first */
9522 mgret = (MAGIC*)ptr_table_fetch(PL_ptr_table, mg);
9523 if (mgret)
9524 return mgret;
9525
9526 for (; mg; mg = mg->mg_moremagic) {
9527 MAGIC *nmg;
9528 Newz(0, nmg, 1, MAGIC);
cb359b41 9529 if (mgprev)
1d7c1841 9530 mgprev->mg_moremagic = nmg;
cb359b41
JH
9531 else
9532 mgret = nmg;
1d7c1841
GS
9533 nmg->mg_virtual = mg->mg_virtual; /* XXX copy dynamic vtable? */
9534 nmg->mg_private = mg->mg_private;
9535 nmg->mg_type = mg->mg_type;
9536 nmg->mg_flags = mg->mg_flags;
14befaf4 9537 if (mg->mg_type == PERL_MAGIC_qr) {
d2f185dc 9538 nmg->mg_obj = (SV*)re_dup((REGEXP*)mg->mg_obj, param);
1d7c1841 9539 }
05bd4103 9540 else if(mg->mg_type == PERL_MAGIC_backref) {
f504ecf4
NC
9541 AV *av = (AV*) mg->mg_obj;
9542 SV **svp;
9543 I32 i;
9544 nmg->mg_obj = (SV*)newAV();
9545 svp = AvARRAY(av);
9546 i = AvFILLp(av);
9547 while (i >= 0) {
9548 av_push((AV*)nmg->mg_obj,sv_dup(svp[i],param));
9549 i--;
9550 }
05bd4103 9551 }
1d7c1841
GS
9552 else {
9553 nmg->mg_obj = (mg->mg_flags & MGf_REFCOUNTED)
d2d73c3e
AB
9554 ? sv_dup_inc(mg->mg_obj, param)
9555 : sv_dup(mg->mg_obj, param);
1d7c1841
GS
9556 }
9557 nmg->mg_len = mg->mg_len;
9558 nmg->mg_ptr = mg->mg_ptr; /* XXX random ptr? */
14befaf4 9559 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
68795e93 9560 if (mg->mg_len > 0) {
1d7c1841 9561 nmg->mg_ptr = SAVEPVN(mg->mg_ptr, mg->mg_len);
14befaf4
DM
9562 if (mg->mg_type == PERL_MAGIC_overload_table &&
9563 AMT_AMAGIC((AMT*)mg->mg_ptr))
9564 {
1d7c1841
GS
9565 AMT *amtp = (AMT*)mg->mg_ptr;
9566 AMT *namtp = (AMT*)nmg->mg_ptr;
9567 I32 i;
9568 for (i = 1; i < NofAMmeth; i++) {
d2d73c3e 9569 namtp->table[i] = cv_dup_inc(amtp->table[i], param);
1d7c1841
GS
9570 }
9571 }
9572 }
9573 else if (mg->mg_len == HEf_SVKEY)
d2d73c3e 9574 nmg->mg_ptr = (char*)sv_dup_inc((SV*)mg->mg_ptr, param);
1d7c1841 9575 }
68795e93
NIS
9576 if ((mg->mg_flags & MGf_DUP) && mg->mg_virtual && mg->mg_virtual->svt_dup) {
9577 CALL_FPTR(nmg->mg_virtual->svt_dup)(aTHX_ nmg, param);
9578 }
1d7c1841
GS
9579 mgprev = nmg;
9580 }
9581 return mgret;
9582}
9583
645c22ef
DM
9584/* create a new pointer-mapping table */
9585
1d7c1841
GS
9586PTR_TBL_t *
9587Perl_ptr_table_new(pTHX)
9588{
9589 PTR_TBL_t *tbl;
9590 Newz(0, tbl, 1, PTR_TBL_t);
9591 tbl->tbl_max = 511;
9592 tbl->tbl_items = 0;
9593 Newz(0, tbl->tbl_ary, tbl->tbl_max + 1, PTR_TBL_ENT_t*);
9594 return tbl;
9595}
9596
df34f778
NC
9597#if (PTRSIZE == 8)
9598# define PTR_TABLE_HASH(ptr) (PTR2UV(ptr) >> 3)
9599#else
9600# define PTR_TABLE_HASH(ptr) (PTR2UV(ptr) >> 2)
9601#endif
9602
645c22ef
DM
9603/* map an existing pointer using a table */
9604
1d7c1841
GS
9605void *
9606Perl_ptr_table_fetch(pTHX_ PTR_TBL_t *tbl, void *sv)
9607{
9608 PTR_TBL_ENT_t *tblent;
df34f778 9609 UV hash = PTR_TABLE_HASH(sv);
1d7c1841
GS
9610 assert(tbl);
9611 tblent = tbl->tbl_ary[hash & tbl->tbl_max];
9612 for (; tblent; tblent = tblent->next) {
9613 if (tblent->oldval == sv)
9614 return tblent->newval;
9615 }
9616 return (void*)NULL;
9617}
9618
645c22ef
DM
9619/* add a new entry to a pointer-mapping table */
9620
1d7c1841
GS
9621void
9622Perl_ptr_table_store(pTHX_ PTR_TBL_t *tbl, void *oldv, void *newv)
9623{
9624 PTR_TBL_ENT_t *tblent, **otblent;
9625 /* XXX this may be pessimal on platforms where pointers aren't good
9626 * hash values e.g. if they grow faster in the most significant
9627 * bits */
df34f778
NC
9628 UV hash = PTR_TABLE_HASH(oldv);
9629 bool empty = 1;
1d7c1841
GS
9630
9631 assert(tbl);
9632 otblent = &tbl->tbl_ary[hash & tbl->tbl_max];
df34f778 9633 for (tblent = *otblent; tblent; empty=0, tblent = tblent->next) {
1d7c1841
GS
9634 if (tblent->oldval == oldv) {
9635 tblent->newval = newv;
1d7c1841
GS
9636 return;
9637 }
9638 }
9639 Newz(0, tblent, 1, PTR_TBL_ENT_t);
9640 tblent->oldval = oldv;
9641 tblent->newval = newv;
9642 tblent->next = *otblent;
9643 *otblent = tblent;
9644 tbl->tbl_items++;
df34f778 9645 if (!empty && tbl->tbl_items > tbl->tbl_max)
1d7c1841
GS
9646 ptr_table_split(tbl);
9647}
9648
645c22ef
DM
9649/* double the hash bucket size of an existing ptr table */
9650
1d7c1841
GS
9651void
9652Perl_ptr_table_split(pTHX_ PTR_TBL_t *tbl)
9653{
9654 PTR_TBL_ENT_t **ary = tbl->tbl_ary;
9655 UV oldsize = tbl->tbl_max + 1;
9656 UV newsize = oldsize * 2;
9657 UV i;
9658
9659 Renew(ary, newsize, PTR_TBL_ENT_t*);
9660 Zero(&ary[oldsize], newsize-oldsize, PTR_TBL_ENT_t*);
9661 tbl->tbl_max = --newsize;
9662 tbl->tbl_ary = ary;
9663 for (i=0; i < oldsize; i++, ary++) {
9664 PTR_TBL_ENT_t **curentp, **entp, *ent;
9665 if (!*ary)
9666 continue;
9667 curentp = ary + oldsize;
9668 for (entp = ary, ent = *ary; ent; ent = *entp) {
df34f778 9669 if ((newsize & PTR_TABLE_HASH(ent->oldval)) != i) {
1d7c1841
GS
9670 *entp = ent->next;
9671 ent->next = *curentp;
9672 *curentp = ent;
9673 continue;
9674 }
9675 else
9676 entp = &ent->next;
9677 }
9678 }
9679}
9680
645c22ef
DM
9681/* remove all the entries from a ptr table */
9682
a0739874
DM
9683void
9684Perl_ptr_table_clear(pTHX_ PTR_TBL_t *tbl)
9685{
9686 register PTR_TBL_ENT_t **array;
9687 register PTR_TBL_ENT_t *entry;
9688 register PTR_TBL_ENT_t *oentry = Null(PTR_TBL_ENT_t*);
9689 UV riter = 0;
9690 UV max;
9691
9692 if (!tbl || !tbl->tbl_items) {
9693 return;
9694 }
9695
9696 array = tbl->tbl_ary;
9697 entry = array[0];
9698 max = tbl->tbl_max;
9699
9700 for (;;) {
9701 if (entry) {
9702 oentry = entry;
9703 entry = entry->next;
9704 Safefree(oentry);
9705 }
9706 if (!entry) {
9707 if (++riter > max) {
9708 break;
9709 }
9710 entry = array[riter];
9711 }
9712 }
9713
9714 tbl->tbl_items = 0;
9715}
9716
645c22ef
DM
9717/* clear and free a ptr table */
9718
a0739874
DM
9719void
9720Perl_ptr_table_free(pTHX_ PTR_TBL_t *tbl)
9721{
9722 if (!tbl) {
9723 return;
9724 }
9725 ptr_table_clear(tbl);
9726 Safefree(tbl->tbl_ary);
9727 Safefree(tbl);
9728}
9729
1d7c1841
GS
9730#ifdef DEBUGGING
9731char *PL_watch_pvx;
9732#endif
9733
645c22ef
DM
9734/* attempt to make everything in the typeglob readonly */
9735
5bd07a3d 9736STATIC SV *
59b40662 9737S_gv_share(pTHX_ SV *sstr, CLONE_PARAMS *param)
5bd07a3d
DM
9738{
9739 GV *gv = (GV*)sstr;
59b40662 9740 SV *sv = &param->proto_perl->Isv_no; /* just need SvREADONLY-ness */
5bd07a3d
DM
9741
9742 if (GvIO(gv) || GvFORM(gv)) {
7fb37951 9743 GvUNIQUE_off(gv); /* GvIOs cannot be shared. nor can GvFORMs */
5bd07a3d
DM
9744 }
9745 else if (!GvCV(gv)) {
9746 GvCV(gv) = (CV*)sv;
9747 }
9748 else {
9749 /* CvPADLISTs cannot be shared */
37e20706 9750 if (!SvREADONLY(GvCV(gv)) && !CvXSUB(GvCV(gv))) {
7fb37951 9751 GvUNIQUE_off(gv);
5bd07a3d
DM
9752 }
9753 }
9754
7fb37951 9755 if (!GvUNIQUE(gv)) {
5bd07a3d
DM
9756#if 0
9757 PerlIO_printf(Perl_debug_log, "gv_share: unable to share %s::%s\n",
9758 HvNAME(GvSTASH(gv)), GvNAME(gv));
9759#endif
9760 return Nullsv;
9761 }
9762
4411f3b6 9763 /*
5bd07a3d
DM
9764 * write attempts will die with
9765 * "Modification of a read-only value attempted"
9766 */
9767 if (!GvSV(gv)) {
9768 GvSV(gv) = sv;
9769 }
9770 else {
9771 SvREADONLY_on(GvSV(gv));
9772 }
9773
9774 if (!GvAV(gv)) {
9775 GvAV(gv) = (AV*)sv;
9776 }
9777 else {
9778 SvREADONLY_on(GvAV(gv));
9779 }
9780
9781 if (!GvHV(gv)) {
9782 GvHV(gv) = (HV*)sv;
9783 }
9784 else {
fea227c9 9785 SvREADONLY_on(GvHV(gv));
5bd07a3d
DM
9786 }
9787
9788 return sstr; /* he_dup() will SvREFCNT_inc() */
9789}
9790
645c22ef
DM
9791/* duplicate an SV of any type (including AV, HV etc) */
9792
83841fad
NIS
9793void
9794Perl_rvpv_dup(pTHX_ SV *dstr, SV *sstr, CLONE_PARAMS* param)
9795{
9796 if (SvROK(sstr)) {
9797 SvRV(dstr) = SvWEAKREF(sstr)
9798 ? sv_dup(SvRV(sstr), param)
9799 : sv_dup_inc(SvRV(sstr), param);
9800 }
9801 else if (SvPVX(sstr)) {
9802 /* Has something there */
9803 if (SvLEN(sstr)) {
68795e93 9804 /* Normal PV - clone whole allocated space */
83841fad 9805 SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1);
68795e93 9806 }
83841fad
NIS
9807 else {
9808 /* Special case - not normally malloced for some reason */
9809 if (SvREADONLY(sstr) && SvFAKE(sstr)) {
9810 /* A "shared" PV - clone it as unshared string */
3e5ba712 9811 if(SvPADTMP(sstr)) {
efb84706
JH
9812 /* However, some of them live in the pad
9813 and they should not have these flags
9814 turned off */
3e5ba712
JH
9815
9816 SvPVX(dstr) = sharepvn(SvPVX(sstr), SvCUR(sstr),
9817 SvUVX(sstr));
9818 SvUVX(dstr) = SvUVX(sstr);
9819 } else {
9820
9821 SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvCUR(sstr));
9822 SvFAKE_off(dstr);
9823 SvREADONLY_off(dstr);
efb84706 9824 }
83841fad
NIS
9825 }
9826 else {
9827 /* Some other special case - random pointer */
9828 SvPVX(dstr) = SvPVX(sstr);
9829 }
9830 }
9831 }
9832 else {
9833 /* Copy the Null */
9834 SvPVX(dstr) = SvPVX(sstr);
9835 }
9836}
9837
1d7c1841 9838SV *
a8fc9800 9839Perl_sv_dup(pTHX_ SV *sstr, CLONE_PARAMS* param)
1d7c1841 9840{
1d7c1841
GS
9841 SV *dstr;
9842
9843 if (!sstr || SvTYPE(sstr) == SVTYPEMASK)
9844 return Nullsv;
9845 /* look for it in the table first */
9846 dstr = (SV*)ptr_table_fetch(PL_ptr_table, sstr);
9847 if (dstr)
9848 return dstr;
9849
457d3ae8
JH
9850 if(param->flags & CLONEf_JOIN_IN) {
9851 /** We are joining here so we don't want do clone
9852 something that is bad **/
9853
9854 if(SvTYPE(sstr) == SVt_PVHV &&
9855 HvNAME(sstr)) {
9856 /** don't clone stashes if they already exist **/
9857 HV* old_stash = gv_stashpv(HvNAME(sstr),0);
9858 return (SV*) old_stash;
9859 }
9860 }
9861
1d7c1841
GS
9862 /* create anew and remember what it is */
9863 new_SV(dstr);
9864 ptr_table_store(PL_ptr_table, sstr, dstr);
9865
9866 /* clone */
9867 SvFLAGS(dstr) = SvFLAGS(sstr);
9868 SvFLAGS(dstr) &= ~SVf_OOK; /* don't propagate OOK hack */
9869 SvREFCNT(dstr) = 0; /* must be before any other dups! */
9870
9871#ifdef DEBUGGING
9872 if (SvANY(sstr) && PL_watch_pvx && SvPVX(sstr) == PL_watch_pvx)
9873 PerlIO_printf(Perl_debug_log, "watch at %p hit, found string \"%s\"\n",
9874 PL_watch_pvx, SvPVX(sstr));
9875#endif
9876
9877 switch (SvTYPE(sstr)) {
9878 case SVt_NULL:
9879 SvANY(dstr) = NULL;
9880 break;
9881 case SVt_IV:
9882 SvANY(dstr) = new_XIV();
9883 SvIVX(dstr) = SvIVX(sstr);
9884 break;
9885 case SVt_NV:
9886 SvANY(dstr) = new_XNV();
9887 SvNVX(dstr) = SvNVX(sstr);
9888 break;
9889 case SVt_RV:
9890 SvANY(dstr) = new_XRV();
83841fad 9891 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
1d7c1841
GS
9892 break;
9893 case SVt_PV:
9894 SvANY(dstr) = new_XPV();
9895 SvCUR(dstr) = SvCUR(sstr);
9896 SvLEN(dstr) = SvLEN(sstr);
83841fad 9897 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
1d7c1841
GS
9898 break;
9899 case SVt_PVIV:
9900 SvANY(dstr) = new_XPVIV();
9901 SvCUR(dstr) = SvCUR(sstr);
9902 SvLEN(dstr) = SvLEN(sstr);
9903 SvIVX(dstr) = SvIVX(sstr);
83841fad 9904 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
1d7c1841
GS
9905 break;
9906 case SVt_PVNV:
9907 SvANY(dstr) = new_XPVNV();
9908 SvCUR(dstr) = SvCUR(sstr);
9909 SvLEN(dstr) = SvLEN(sstr);
9910 SvIVX(dstr) = SvIVX(sstr);
9911 SvNVX(dstr) = SvNVX(sstr);
83841fad 9912 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
1d7c1841
GS
9913 break;
9914 case SVt_PVMG:
9915 SvANY(dstr) = new_XPVMG();
9916 SvCUR(dstr) = SvCUR(sstr);
9917 SvLEN(dstr) = SvLEN(sstr);
9918 SvIVX(dstr) = SvIVX(sstr);
9919 SvNVX(dstr) = SvNVX(sstr);
d2d73c3e
AB
9920 SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr), param);
9921 SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr), param);
83841fad 9922 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
1d7c1841
GS
9923 break;
9924 case SVt_PVBM:
9925 SvANY(dstr) = new_XPVBM();
9926 SvCUR(dstr) = SvCUR(sstr);
9927 SvLEN(dstr) = SvLEN(sstr);
9928 SvIVX(dstr) = SvIVX(sstr);
9929 SvNVX(dstr) = SvNVX(sstr);
d2d73c3e
AB
9930 SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr), param);
9931 SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr), param);
83841fad 9932 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
1d7c1841
GS
9933 BmRARE(dstr) = BmRARE(sstr);
9934 BmUSEFUL(dstr) = BmUSEFUL(sstr);
9935 BmPREVIOUS(dstr)= BmPREVIOUS(sstr);
9936 break;
9937 case SVt_PVLV:
9938 SvANY(dstr) = new_XPVLV();
9939 SvCUR(dstr) = SvCUR(sstr);
9940 SvLEN(dstr) = SvLEN(sstr);
9941 SvIVX(dstr) = SvIVX(sstr);
9942 SvNVX(dstr) = SvNVX(sstr);
d2d73c3e
AB
9943 SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr), param);
9944 SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr), param);
83841fad 9945 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
1d7c1841
GS
9946 LvTARGOFF(dstr) = LvTARGOFF(sstr); /* XXX sometimes holds PMOP* when DEBUGGING */
9947 LvTARGLEN(dstr) = LvTARGLEN(sstr);
73c86719
JH
9948 if (LvTYPE(sstr) == 't') /* for tie: unrefcnted fake (SV**) */
9949 LvTARG(dstr) = dstr;
9950 else if (LvTYPE(sstr) == 'T') /* for tie: fake HE */
9951 LvTARG(dstr) = (SV*)he_dup((HE*)LvTARG(sstr), 0, param);
9952 else
9953 LvTARG(dstr) = sv_dup_inc(LvTARG(sstr), param);
1d7c1841
GS
9954 LvTYPE(dstr) = LvTYPE(sstr);
9955 break;
9956 case SVt_PVGV:
7fb37951 9957 if (GvUNIQUE((GV*)sstr)) {
5bd07a3d 9958 SV *share;
59b40662 9959 if ((share = gv_share(sstr, param))) {
5bd07a3d
DM
9960 del_SV(dstr);
9961 dstr = share;
37e20706 9962 ptr_table_store(PL_ptr_table, sstr, dstr);
5bd07a3d
DM
9963#if 0
9964 PerlIO_printf(Perl_debug_log, "sv_dup: sharing %s::%s\n",
9965 HvNAME(GvSTASH(share)), GvNAME(share));
9966#endif
9967 break;
9968 }
9969 }
1d7c1841
GS
9970 SvANY(dstr) = new_XPVGV();
9971 SvCUR(dstr) = SvCUR(sstr);
9972 SvLEN(dstr) = SvLEN(sstr);
9973 SvIVX(dstr) = SvIVX(sstr);
9974 SvNVX(dstr) = SvNVX(sstr);
d2d73c3e
AB
9975 SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr), param);
9976 SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr), param);
83841fad 9977 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
1d7c1841
GS
9978 GvNAMELEN(dstr) = GvNAMELEN(sstr);
9979 GvNAME(dstr) = SAVEPVN(GvNAME(sstr), GvNAMELEN(sstr));
d2d73c3e 9980 GvSTASH(dstr) = hv_dup_inc(GvSTASH(sstr), param);
1d7c1841 9981 GvFLAGS(dstr) = GvFLAGS(sstr);
d2d73c3e 9982 GvGP(dstr) = gp_dup(GvGP(sstr), param);
1d7c1841
GS
9983 (void)GpREFCNT_inc(GvGP(dstr));
9984 break;
9985 case SVt_PVIO:
9986 SvANY(dstr) = new_XPVIO();
9987 SvCUR(dstr) = SvCUR(sstr);
9988 SvLEN(dstr) = SvLEN(sstr);
9989 SvIVX(dstr) = SvIVX(sstr);
9990 SvNVX(dstr) = SvNVX(sstr);
d2d73c3e
AB
9991 SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr), param);
9992 SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr), param);
83841fad 9993 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
a8fc9800 9994 IoIFP(dstr) = fp_dup(IoIFP(sstr), IoTYPE(sstr), param);
1d7c1841
GS
9995 if (IoOFP(sstr) == IoIFP(sstr))
9996 IoOFP(dstr) = IoIFP(dstr);
9997 else
a8fc9800 9998 IoOFP(dstr) = fp_dup(IoOFP(sstr), IoTYPE(sstr), param);
1d7c1841
GS
9999 /* PL_rsfp_filters entries have fake IoDIRP() */
10000 if (IoDIRP(sstr) && !(IoFLAGS(sstr) & IOf_FAKE_DIRP))
10001 IoDIRP(dstr) = dirp_dup(IoDIRP(sstr));
10002 else
10003 IoDIRP(dstr) = IoDIRP(sstr);
10004 IoLINES(dstr) = IoLINES(sstr);
10005 IoPAGE(dstr) = IoPAGE(sstr);
10006 IoPAGE_LEN(dstr) = IoPAGE_LEN(sstr);
10007 IoLINES_LEFT(dstr) = IoLINES_LEFT(sstr);
4376702e
JH
10008 if(IoFLAGS(sstr) & IOf_FAKE_DIRP) {
10009 /* I have no idea why fake dirp (rsfps)
10010 should be treaded differently but otherwise
10011 we end up with leaks -- sky*/
10012 IoTOP_GV(dstr) = gv_dup_inc(IoTOP_GV(sstr), param);
10013 IoFMT_GV(dstr) = gv_dup_inc(IoFMT_GV(sstr), param);
10014 IoBOTTOM_GV(dstr) = gv_dup_inc(IoBOTTOM_GV(sstr), param);
10015 } else {
10016 IoTOP_GV(dstr) = gv_dup(IoTOP_GV(sstr), param);
10017 IoFMT_GV(dstr) = gv_dup(IoFMT_GV(sstr), param);
10018 IoBOTTOM_GV(dstr) = gv_dup(IoBOTTOM_GV(sstr), param);
10019 }
1d7c1841 10020 IoTOP_NAME(dstr) = SAVEPV(IoTOP_NAME(sstr));
1d7c1841 10021 IoFMT_NAME(dstr) = SAVEPV(IoFMT_NAME(sstr));
1d7c1841 10022 IoBOTTOM_NAME(dstr) = SAVEPV(IoBOTTOM_NAME(sstr));
1d7c1841
GS
10023 IoSUBPROCESS(dstr) = IoSUBPROCESS(sstr);
10024 IoTYPE(dstr) = IoTYPE(sstr);
10025 IoFLAGS(dstr) = IoFLAGS(sstr);
10026 break;
10027 case SVt_PVAV:
10028 SvANY(dstr) = new_XPVAV();
10029 SvCUR(dstr) = SvCUR(sstr);
10030 SvLEN(dstr) = SvLEN(sstr);
10031 SvIVX(dstr) = SvIVX(sstr);
10032 SvNVX(dstr) = SvNVX(sstr);
d2d73c3e
AB
10033 SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr), param);
10034 SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr), param);
10035 AvARYLEN((AV*)dstr) = sv_dup_inc(AvARYLEN((AV*)sstr), param);
1d7c1841
GS
10036 AvFLAGS((AV*)dstr) = AvFLAGS((AV*)sstr);
10037 if (AvARRAY((AV*)sstr)) {
10038 SV **dst_ary, **src_ary;
10039 SSize_t items = AvFILLp((AV*)sstr) + 1;
10040
10041 src_ary = AvARRAY((AV*)sstr);
10042 Newz(0, dst_ary, AvMAX((AV*)sstr)+1, SV*);
10043 ptr_table_store(PL_ptr_table, src_ary, dst_ary);
10044 SvPVX(dstr) = (char*)dst_ary;
10045 AvALLOC((AV*)dstr) = dst_ary;
10046 if (AvREAL((AV*)sstr)) {
10047 while (items-- > 0)
d2d73c3e 10048 *dst_ary++ = sv_dup_inc(*src_ary++, param);
1d7c1841
GS
10049 }
10050 else {
10051 while (items-- > 0)
d2d73c3e 10052 *dst_ary++ = sv_dup(*src_ary++, param);
1d7c1841
GS
10053 }
10054 items = AvMAX((AV*)sstr) - AvFILLp((AV*)sstr);
10055 while (items-- > 0) {
10056 *dst_ary++ = &PL_sv_undef;
10057 }
10058 }
10059 else {
10060 SvPVX(dstr) = Nullch;
10061 AvALLOC((AV*)dstr) = (SV**)NULL;
10062 }
10063 break;
10064 case SVt_PVHV:
10065 SvANY(dstr) = new_XPVHV();
10066 SvCUR(dstr) = SvCUR(sstr);
10067 SvLEN(dstr) = SvLEN(sstr);
10068 SvIVX(dstr) = SvIVX(sstr);
10069 SvNVX(dstr) = SvNVX(sstr);
d2d73c3e
AB
10070 SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr), param);
10071 SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr), param);
1d7c1841
GS
10072 HvRITER((HV*)dstr) = HvRITER((HV*)sstr);
10073 if (HvARRAY((HV*)sstr)) {
1d7c1841
GS
10074 STRLEN i = 0;
10075 XPVHV *dxhv = (XPVHV*)SvANY(dstr);
10076 XPVHV *sxhv = (XPVHV*)SvANY(sstr);
10077 Newz(0, dxhv->xhv_array,
10078 PERL_HV_ARRAY_ALLOC_BYTES(dxhv->xhv_max+1), char);
10079 while (i <= sxhv->xhv_max) {
10080 ((HE**)dxhv->xhv_array)[i] = he_dup(((HE**)sxhv->xhv_array)[i],
eb160463
GS
10081 (bool)!!HvSHAREKEYS(sstr),
10082 param);
1d7c1841
GS
10083 ++i;
10084 }
eb160463
GS
10085 dxhv->xhv_eiter = he_dup(sxhv->xhv_eiter,
10086 (bool)!!HvSHAREKEYS(sstr), param);
1d7c1841
GS
10087 }
10088 else {
10089 SvPVX(dstr) = Nullch;
10090 HvEITER((HV*)dstr) = (HE*)NULL;
10091 }
10092 HvPMROOT((HV*)dstr) = HvPMROOT((HV*)sstr); /* XXX */
10093 HvNAME((HV*)dstr) = SAVEPV(HvNAME((HV*)sstr));
c43294b8 10094 /* Record stashes for possible cloning in Perl_clone(). */
6676db26 10095 if(HvNAME((HV*)dstr))
d2d73c3e 10096 av_push(param->stashes, dstr);
1d7c1841
GS
10097 break;
10098 case SVt_PVFM:
10099 SvANY(dstr) = new_XPVFM();
10100 FmLINES(dstr) = FmLINES(sstr);
10101 goto dup_pvcv;
10102 /* NOTREACHED */
10103 case SVt_PVCV:
10104 SvANY(dstr) = new_XPVCV();
d2d73c3e 10105 dup_pvcv:
1d7c1841
GS
10106 SvCUR(dstr) = SvCUR(sstr);
10107 SvLEN(dstr) = SvLEN(sstr);
10108 SvIVX(dstr) = SvIVX(sstr);
10109 SvNVX(dstr) = SvNVX(sstr);
d2d73c3e
AB
10110 SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr), param);
10111 SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr), param);
83841fad 10112 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
d2d73c3e 10113 CvSTASH(dstr) = hv_dup(CvSTASH(sstr), param); /* NOTE: not refcounted */
1d7c1841
GS
10114 CvSTART(dstr) = CvSTART(sstr);
10115 CvROOT(dstr) = OpREFCNT_inc(CvROOT(sstr));
10116 CvXSUB(dstr) = CvXSUB(sstr);
10117 CvXSUBANY(dstr) = CvXSUBANY(sstr);
01485f8b
DM
10118 if (CvCONST(sstr)) {
10119 CvXSUBANY(dstr).any_ptr = GvUNIQUE(CvGV(sstr)) ?
10120 SvREFCNT_inc(CvXSUBANY(sstr).any_ptr) :
10121 sv_dup_inc(CvXSUBANY(sstr).any_ptr, param);
10122 }
a23cf089
JD
10123 /* don't dup if copying back - CvGV isn't refcounted, so the
10124 * duped GV may never be freed. A bit of a hack! DAPM */
10125 CvGV(dstr) = (param->flags & CLONEf_JOIN_IN) ?
10126 Nullgv : gv_dup(CvGV(sstr), param) ;
d2d73c3e
AB
10127 if (param->flags & CLONEf_COPY_STACKS) {
10128 CvDEPTH(dstr) = CvDEPTH(sstr);
10129 } else {
10130 CvDEPTH(dstr) = 0;
10131 }
9755d405 10132 PAD_DUP(CvPADLIST(dstr), CvPADLIST(sstr), param);
d7afa7f5
JH
10133 CvOUTSIDE_SEQ(dstr) = CvOUTSIDE_SEQ(sstr);
10134 CvOUTSIDE(dstr) =
10135 CvWEAKOUTSIDE(sstr)
10136 ? cv_dup( CvOUTSIDE(sstr), param)
10137 : cv_dup_inc(CvOUTSIDE(sstr), param);
1d7c1841 10138 CvFLAGS(dstr) = CvFLAGS(sstr);
54356c7d 10139 CvFILE(dstr) = CvXSUB(sstr) ? CvFILE(sstr) : SAVEPV(CvFILE(sstr));
1d7c1841
GS
10140 break;
10141 default:
c803eecc 10142 Perl_croak(aTHX_ "Bizarre SvTYPE [%" IVdf "]", (IV)SvTYPE(sstr));
1d7c1841
GS
10143 break;
10144 }
10145
10146 if (SvOBJECT(dstr) && SvTYPE(dstr) != SVt_PVIO)
10147 ++PL_sv_objcount;
10148
10149 return dstr;
d2d73c3e 10150 }
1d7c1841 10151
645c22ef
DM
10152/* duplicate a context */
10153
1d7c1841 10154PERL_CONTEXT *
a8fc9800 10155Perl_cx_dup(pTHX_ PERL_CONTEXT *cxs, I32 ix, I32 max, CLONE_PARAMS* param)
1d7c1841
GS
10156{
10157 PERL_CONTEXT *ncxs;
10158
10159 if (!cxs)
10160 return (PERL_CONTEXT*)NULL;
10161
10162 /* look for it in the table first */
10163 ncxs = (PERL_CONTEXT*)ptr_table_fetch(PL_ptr_table, cxs);
10164 if (ncxs)
10165 return ncxs;
10166
10167 /* create anew and remember what it is */
10168 Newz(56, ncxs, max + 1, PERL_CONTEXT);
10169 ptr_table_store(PL_ptr_table, cxs, ncxs);
10170
10171 while (ix >= 0) {
10172 PERL_CONTEXT *cx = &cxs[ix];
10173 PERL_CONTEXT *ncx = &ncxs[ix];
10174 ncx->cx_type = cx->cx_type;
10175 if (CxTYPE(cx) == CXt_SUBST) {
10176 Perl_croak(aTHX_ "Cloning substitution context is unimplemented");
10177 }
10178 else {
10179 ncx->blk_oldsp = cx->blk_oldsp;
10180 ncx->blk_oldcop = cx->blk_oldcop;
10181 ncx->blk_oldretsp = cx->blk_oldretsp;
10182 ncx->blk_oldmarksp = cx->blk_oldmarksp;
10183 ncx->blk_oldscopesp = cx->blk_oldscopesp;
10184 ncx->blk_oldpm = cx->blk_oldpm;
10185 ncx->blk_gimme = cx->blk_gimme;
10186 switch (CxTYPE(cx)) {
10187 case CXt_SUB:
10188 ncx->blk_sub.cv = (cx->blk_sub.olddepth == 0
d2d73c3e
AB
10189 ? cv_dup_inc(cx->blk_sub.cv, param)
10190 : cv_dup(cx->blk_sub.cv,param));
1d7c1841 10191 ncx->blk_sub.argarray = (cx->blk_sub.hasargs
d2d73c3e 10192 ? av_dup_inc(cx->blk_sub.argarray, param)
1d7c1841 10193 : Nullav);
d2d73c3e 10194 ncx->blk_sub.savearray = av_dup_inc(cx->blk_sub.savearray, param);
1d7c1841
GS
10195 ncx->blk_sub.olddepth = cx->blk_sub.olddepth;
10196 ncx->blk_sub.hasargs = cx->blk_sub.hasargs;
10197 ncx->blk_sub.lval = cx->blk_sub.lval;
10198 break;
10199 case CXt_EVAL:
10200 ncx->blk_eval.old_in_eval = cx->blk_eval.old_in_eval;
10201 ncx->blk_eval.old_op_type = cx->blk_eval.old_op_type;
b47cad08 10202 ncx->blk_eval.old_namesv = sv_dup_inc(cx->blk_eval.old_namesv, param);
1d7c1841 10203 ncx->blk_eval.old_eval_root = cx->blk_eval.old_eval_root;
d2d73c3e 10204 ncx->blk_eval.cur_text = sv_dup(cx->blk_eval.cur_text, param);
1d7c1841
GS
10205 break;
10206 case CXt_LOOP:
10207 ncx->blk_loop.label = cx->blk_loop.label;
10208 ncx->blk_loop.resetsp = cx->blk_loop.resetsp;
10209 ncx->blk_loop.redo_op = cx->blk_loop.redo_op;
10210 ncx->blk_loop.next_op = cx->blk_loop.next_op;
10211 ncx->blk_loop.last_op = cx->blk_loop.last_op;
10212 ncx->blk_loop.iterdata = (CxPADLOOP(cx)
10213 ? cx->blk_loop.iterdata
d2d73c3e 10214 : gv_dup((GV*)cx->blk_loop.iterdata, param));
d7afa7f5
JH
10215 ncx->blk_loop.oldcomppad
10216 = (PAD*)ptr_table_fetch(PL_ptr_table,
10217 cx->blk_loop.oldcomppad);
d2d73c3e
AB
10218 ncx->blk_loop.itersave = sv_dup_inc(cx->blk_loop.itersave, param);
10219 ncx->blk_loop.iterlval = sv_dup_inc(cx->blk_loop.iterlval, param);
10220 ncx->blk_loop.iterary = av_dup_inc(cx->blk_loop.iterary, param);
1d7c1841
GS
10221 ncx->blk_loop.iterix = cx->blk_loop.iterix;
10222 ncx->blk_loop.itermax = cx->blk_loop.itermax;
10223 break;
10224 case CXt_FORMAT:
d2d73c3e
AB
10225 ncx->blk_sub.cv = cv_dup(cx->blk_sub.cv, param);
10226 ncx->blk_sub.gv = gv_dup(cx->blk_sub.gv, param);
10227 ncx->blk_sub.dfoutgv = gv_dup_inc(cx->blk_sub.dfoutgv, param);
1d7c1841
GS
10228 ncx->blk_sub.hasargs = cx->blk_sub.hasargs;
10229 break;
10230 case CXt_BLOCK:
10231 case CXt_NULL:
10232 break;
10233 }
10234 }
10235 --ix;
10236 }
10237 return ncxs;
10238}
10239
645c22ef
DM
10240/* duplicate a stack info structure */
10241
1d7c1841 10242PERL_SI *
a8fc9800 10243Perl_si_dup(pTHX_ PERL_SI *si, CLONE_PARAMS* param)
1d7c1841
GS
10244{
10245 PERL_SI *nsi;
10246
10247 if (!si)
10248 return (PERL_SI*)NULL;
10249
10250 /* look for it in the table first */
10251 nsi = (PERL_SI*)ptr_table_fetch(PL_ptr_table, si);
10252 if (nsi)
10253 return nsi;
10254
10255 /* create anew and remember what it is */
10256 Newz(56, nsi, 1, PERL_SI);
10257 ptr_table_store(PL_ptr_table, si, nsi);
10258
d2d73c3e 10259 nsi->si_stack = av_dup_inc(si->si_stack, param);
1d7c1841
GS
10260 nsi->si_cxix = si->si_cxix;
10261 nsi->si_cxmax = si->si_cxmax;
d2d73c3e 10262 nsi->si_cxstack = cx_dup(si->si_cxstack, si->si_cxix, si->si_cxmax, param);
1d7c1841 10263 nsi->si_type = si->si_type;
d2d73c3e
AB
10264 nsi->si_prev = si_dup(si->si_prev, param);
10265 nsi->si_next = si_dup(si->si_next, param);
1d7c1841
GS
10266 nsi->si_markoff = si->si_markoff;
10267
10268 return nsi;
10269}
10270
10271#define POPINT(ss,ix) ((ss)[--(ix)].any_i32)
10272#define TOPINT(ss,ix) ((ss)[ix].any_i32)
10273#define POPLONG(ss,ix) ((ss)[--(ix)].any_long)
10274#define TOPLONG(ss,ix) ((ss)[ix].any_long)
10275#define POPIV(ss,ix) ((ss)[--(ix)].any_iv)
10276#define TOPIV(ss,ix) ((ss)[ix].any_iv)
21637969
YST
10277#define POPBOOL(ss,ix) ((ss)[--(ix)].any_bool)
10278#define TOPBOOL(ss,ix) ((ss)[ix].any_bool)
1d7c1841
GS
10279#define POPPTR(ss,ix) ((ss)[--(ix)].any_ptr)
10280#define TOPPTR(ss,ix) ((ss)[ix].any_ptr)
10281#define POPDPTR(ss,ix) ((ss)[--(ix)].any_dptr)
10282#define TOPDPTR(ss,ix) ((ss)[ix].any_dptr)
10283#define POPDXPTR(ss,ix) ((ss)[--(ix)].any_dxptr)
10284#define TOPDXPTR(ss,ix) ((ss)[ix].any_dxptr)
10285
10286/* XXXXX todo */
10287#define pv_dup_inc(p) SAVEPV(p)
10288#define pv_dup(p) SAVEPV(p)
10289#define svp_dup_inc(p,pp) any_dup(p,pp)
10290
645c22ef
DM
10291/* map any object to the new equivent - either something in the
10292 * ptr table, or something in the interpreter structure
10293 */
10294
1d7c1841
GS
10295void *
10296Perl_any_dup(pTHX_ void *v, PerlInterpreter *proto_perl)
10297{
10298 void *ret;
10299
10300 if (!v)
10301 return (void*)NULL;
10302
10303 /* look for it in the table first */
10304 ret = ptr_table_fetch(PL_ptr_table, v);
10305 if (ret)
10306 return ret;
10307
10308 /* see if it is part of the interpreter structure */
10309 if (v >= (void*)proto_perl && v < (void*)(proto_perl+1))
acfe0abc 10310 ret = (void*)(((char*)aTHX) + (((char*)v) - (char*)proto_perl));
05ec9bb3 10311 else {
1d7c1841 10312 ret = v;
05ec9bb3 10313 }
1d7c1841
GS
10314
10315 return ret;
10316}
10317
645c22ef
DM
10318/* duplicate the save stack */
10319
1d7c1841 10320ANY *
a8fc9800 10321Perl_ss_dup(pTHX_ PerlInterpreter *proto_perl, CLONE_PARAMS* param)
1d7c1841
GS
10322{
10323 ANY *ss = proto_perl->Tsavestack;
10324 I32 ix = proto_perl->Tsavestack_ix;
10325 I32 max = proto_perl->Tsavestack_max;
10326 ANY *nss;
10327 SV *sv;
10328 GV *gv;
10329 AV *av;
10330 HV *hv;
10331 void* ptr;
10332 int intval;
10333 long longval;
10334 GP *gp;
10335 IV iv;
10336 I32 i;
c4e33207 10337 char *c = NULL;
1d7c1841 10338 void (*dptr) (void*);
acfe0abc 10339 void (*dxptr) (pTHX_ void*);
e977893f 10340 OP *o;
1d7c1841
GS
10341
10342 Newz(54, nss, max, ANY);
10343
10344 while (ix > 0) {
10345 i = POPINT(ss,ix);
10346 TOPINT(nss,ix) = i;
10347 switch (i) {
10348 case SAVEt_ITEM: /* normal string */
10349 sv = (SV*)POPPTR(ss,ix);
d2d73c3e 10350 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
1d7c1841 10351 sv = (SV*)POPPTR(ss,ix);
d2d73c3e 10352 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
1d7c1841
GS
10353 break;
10354 case SAVEt_SV: /* scalar reference */
10355 sv = (SV*)POPPTR(ss,ix);
d2d73c3e 10356 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
1d7c1841 10357 gv = (GV*)POPPTR(ss,ix);
d2d73c3e 10358 TOPPTR(nss,ix) = gv_dup_inc(gv, param);
1d7c1841 10359 break;
f4dd75d9
GS
10360 case SAVEt_GENERIC_PVREF: /* generic char* */
10361 c = (char*)POPPTR(ss,ix);
10362 TOPPTR(nss,ix) = pv_dup(c);
10363 ptr = POPPTR(ss,ix);
10364 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
10365 break;
05ec9bb3
NIS
10366 case SAVEt_SHARED_PVREF: /* char* in shared space */
10367 c = (char*)POPPTR(ss,ix);
10368 TOPPTR(nss,ix) = savesharedpv(c);
10369 ptr = POPPTR(ss,ix);
10370 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
10371 break;
1d7c1841
GS
10372 case SAVEt_GENERIC_SVREF: /* generic sv */
10373 case SAVEt_SVREF: /* scalar reference */
10374 sv = (SV*)POPPTR(ss,ix);
d2d73c3e 10375 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
1d7c1841
GS
10376 ptr = POPPTR(ss,ix);
10377 TOPPTR(nss,ix) = svp_dup_inc((SV**)ptr, proto_perl);/* XXXXX */
10378 break;
10379 case SAVEt_AV: /* array reference */
10380 av = (AV*)POPPTR(ss,ix);
d2d73c3e 10381 TOPPTR(nss,ix) = av_dup_inc(av, param);
1d7c1841 10382 gv = (GV*)POPPTR(ss,ix);
d2d73c3e 10383 TOPPTR(nss,ix) = gv_dup(gv, param);
1d7c1841
GS
10384 break;
10385 case SAVEt_HV: /* hash reference */
10386 hv = (HV*)POPPTR(ss,ix);
d2d73c3e 10387 TOPPTR(nss,ix) = hv_dup_inc(hv, param);
1d7c1841 10388 gv = (GV*)POPPTR(ss,ix);
d2d73c3e 10389 TOPPTR(nss,ix) = gv_dup(gv, param);
1d7c1841
GS
10390 break;
10391 case SAVEt_INT: /* int reference */
10392 ptr = POPPTR(ss,ix);
10393 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
10394 intval = (int)POPINT(ss,ix);
10395 TOPINT(nss,ix) = intval;
10396 break;
10397 case SAVEt_LONG: /* long reference */
10398 ptr = POPPTR(ss,ix);
10399 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
10400 longval = (long)POPLONG(ss,ix);
10401 TOPLONG(nss,ix) = longval;
10402 break;
10403 case SAVEt_I32: /* I32 reference */
10404 case SAVEt_I16: /* I16 reference */
10405 case SAVEt_I8: /* I8 reference */
10406 ptr = POPPTR(ss,ix);
10407 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
10408 i = POPINT(ss,ix);
10409 TOPINT(nss,ix) = i;
10410 break;
10411 case SAVEt_IV: /* IV reference */
10412 ptr = POPPTR(ss,ix);
10413 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
10414 iv = POPIV(ss,ix);
10415 TOPIV(nss,ix) = iv;
10416 break;
10417 case SAVEt_SPTR: /* SV* reference */
10418 ptr = POPPTR(ss,ix);
10419 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
10420 sv = (SV*)POPPTR(ss,ix);
d2d73c3e 10421 TOPPTR(nss,ix) = sv_dup(sv, param);
1d7c1841
GS
10422 break;
10423 case SAVEt_VPTR: /* random* reference */
10424 ptr = POPPTR(ss,ix);
10425 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
10426 ptr = POPPTR(ss,ix);
10427 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
10428 break;
10429 case SAVEt_PPTR: /* char* reference */
10430 ptr = POPPTR(ss,ix);
10431 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
10432 c = (char*)POPPTR(ss,ix);
10433 TOPPTR(nss,ix) = pv_dup(c);
10434 break;
10435 case SAVEt_HPTR: /* HV* reference */
10436 ptr = POPPTR(ss,ix);
10437 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
10438 hv = (HV*)POPPTR(ss,ix);
d2d73c3e 10439 TOPPTR(nss,ix) = hv_dup(hv, param);
1d7c1841
GS
10440 break;
10441 case SAVEt_APTR: /* AV* reference */
10442 ptr = POPPTR(ss,ix);
10443 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
10444 av = (AV*)POPPTR(ss,ix);
d2d73c3e 10445 TOPPTR(nss,ix) = av_dup(av, param);
1d7c1841
GS
10446 break;
10447 case SAVEt_NSTAB:
10448 gv = (GV*)POPPTR(ss,ix);
d2d73c3e 10449 TOPPTR(nss,ix) = gv_dup(gv, param);
1d7c1841
GS
10450 break;
10451 case SAVEt_GP: /* scalar reference */
10452 gp = (GP*)POPPTR(ss,ix);
d2d73c3e 10453 TOPPTR(nss,ix) = gp = gp_dup(gp, param);
1d7c1841
GS
10454 (void)GpREFCNT_inc(gp);
10455 gv = (GV*)POPPTR(ss,ix);
2ed3c8fc 10456 TOPPTR(nss,ix) = gv_dup_inc(gv, param);
1d7c1841
GS
10457 c = (char*)POPPTR(ss,ix);
10458 TOPPTR(nss,ix) = pv_dup(c);
10459 iv = POPIV(ss,ix);
10460 TOPIV(nss,ix) = iv;
10461 iv = POPIV(ss,ix);
10462 TOPIV(nss,ix) = iv;
10463 break;
10464 case SAVEt_FREESV:
26d9b02f 10465 case SAVEt_MORTALIZESV:
1d7c1841 10466 sv = (SV*)POPPTR(ss,ix);
d2d73c3e 10467 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
1d7c1841
GS
10468 break;
10469 case SAVEt_FREEOP:
10470 ptr = POPPTR(ss,ix);
10471 if (ptr && (((OP*)ptr)->op_private & OPpREFCOUNTED)) {
10472 /* these are assumed to be refcounted properly */
10473 switch (((OP*)ptr)->op_type) {
10474 case OP_LEAVESUB:
10475 case OP_LEAVESUBLV:
10476 case OP_LEAVEEVAL:
10477 case OP_LEAVE:
10478 case OP_SCOPE:
10479 case OP_LEAVEWRITE:
e977893f
GS
10480 TOPPTR(nss,ix) = ptr;
10481 o = (OP*)ptr;
10482 OpREFCNT_inc(o);
1d7c1841
GS
10483 break;
10484 default:
10485 TOPPTR(nss,ix) = Nullop;
10486 break;
10487 }
10488 }
10489 else
10490 TOPPTR(nss,ix) = Nullop;
10491 break;
10492 case SAVEt_FREEPV:
10493 c = (char*)POPPTR(ss,ix);
10494 TOPPTR(nss,ix) = pv_dup_inc(c);
10495 break;
10496 case SAVEt_CLEARSV:
10497 longval = POPLONG(ss,ix);
10498 TOPLONG(nss,ix) = longval;
10499 break;
10500 case SAVEt_DELETE:
10501 hv = (HV*)POPPTR(ss,ix);
d2d73c3e 10502 TOPPTR(nss,ix) = hv_dup_inc(hv, param);
1d7c1841
GS
10503 c = (char*)POPPTR(ss,ix);
10504 TOPPTR(nss,ix) = pv_dup_inc(c);
10505 i = POPINT(ss,ix);
10506 TOPINT(nss,ix) = i;
10507 break;
10508 case SAVEt_DESTRUCTOR:
10509 ptr = POPPTR(ss,ix);
10510 TOPPTR(nss,ix) = any_dup(ptr, proto_perl); /* XXX quite arbitrary */
10511 dptr = POPDPTR(ss,ix);
ef75a179 10512 TOPDPTR(nss,ix) = (void (*)(void*))any_dup((void *)dptr, proto_perl);
1d7c1841
GS
10513 break;
10514 case SAVEt_DESTRUCTOR_X:
10515 ptr = POPPTR(ss,ix);
10516 TOPPTR(nss,ix) = any_dup(ptr, proto_perl); /* XXX quite arbitrary */
10517 dxptr = POPDXPTR(ss,ix);
acfe0abc 10518 TOPDXPTR(nss,ix) = (void (*)(pTHX_ void*))any_dup((void *)dxptr, proto_perl);
1d7c1841
GS
10519 break;
10520 case SAVEt_REGCONTEXT:
10521 case SAVEt_ALLOC:
10522 i = POPINT(ss,ix);
10523 TOPINT(nss,ix) = i;
10524 ix -= i;
10525 break;
10526 case SAVEt_STACK_POS: /* Position on Perl stack */
10527 i = POPINT(ss,ix);
10528 TOPINT(nss,ix) = i;
10529 break;
10530 case SAVEt_AELEM: /* array element */
10531 sv = (SV*)POPPTR(ss,ix);
d2d73c3e 10532 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
1d7c1841
GS
10533 i = POPINT(ss,ix);
10534 TOPINT(nss,ix) = i;
10535 av = (AV*)POPPTR(ss,ix);
d2d73c3e 10536 TOPPTR(nss,ix) = av_dup_inc(av, param);
1d7c1841
GS
10537 break;
10538 case SAVEt_HELEM: /* hash element */
10539 sv = (SV*)POPPTR(ss,ix);
d2d73c3e 10540 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
1d7c1841 10541 sv = (SV*)POPPTR(ss,ix);
d2d73c3e 10542 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
1d7c1841 10543 hv = (HV*)POPPTR(ss,ix);
d2d73c3e 10544 TOPPTR(nss,ix) = hv_dup_inc(hv, param);
1d7c1841
GS
10545 break;
10546 case SAVEt_OP:
10547 ptr = POPPTR(ss,ix);
10548 TOPPTR(nss,ix) = ptr;
10549 break;
10550 case SAVEt_HINTS:
10551 i = POPINT(ss,ix);
10552 TOPINT(nss,ix) = i;
10553 break;
c4410b1b
GS
10554 case SAVEt_COMPPAD:
10555 av = (AV*)POPPTR(ss,ix);
c2ef0052 10556 TOPPTR(nss,ix) = av_dup(av, param);
c4410b1b 10557 break;
c3564e5c
GS
10558 case SAVEt_PADSV:
10559 longval = (long)POPLONG(ss,ix);
10560 TOPLONG(nss,ix) = longval;
10561 ptr = POPPTR(ss,ix);
10562 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
10563 sv = (SV*)POPPTR(ss,ix);
d2d73c3e 10564 TOPPTR(nss,ix) = sv_dup(sv, param);
c3564e5c 10565 break;
4f4e7967 10566 case SAVEt_BOOL:
21637969 10567 ptr = POPPTR(ss,ix);
4f4e7967 10568 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
21637969 10569 longval = (long)POPBOOL(ss,ix);
4f4e7967
JH
10570 TOPBOOL(nss,ix) = (bool)longval;
10571 break;
1d7c1841
GS
10572 default:
10573 Perl_croak(aTHX_ "panic: ss_dup inconsistency");
10574 }
10575 }
10576
10577 return nss;
10578}
10579
645c22ef
DM
10580/*
10581=for apidoc perl_clone
10582
10583Create and return a new interpreter by cloning the current one.
10584
ac388100 10585perl_clone takes these flags as parameters:
4f4e7967
JH
10586
10587CLONEf_COPY_STACKS - is used to, well, copy the stacks also,
10588without it we only clone the data and zero the stacks,
10589with it we copy the stacks and the new perl interpreter is
10590ready to run at the exact same point as the previous one.
10591The pseudo-fork code uses COPY_STACKS while the
10592threads->new doesn't.
10593
10594CLONEf_KEEP_PTR_TABLE
10595perl_clone keeps a ptr_table with the pointer of the old
10596variable as a key and the new variable as a value,
10597this allows it to check if something has been cloned and not
10598clone it again but rather just use the value and increase the
10599refcount. If KEEP_PTR_TABLE is not set then perl_clone will kill
10600the ptr_table using the function
10601C<ptr_table_free(PL_ptr_table); PL_ptr_table = NULL;>,
10602reason to keep it around is if you want to dup some of your own
10603variable who are outside the graph perl scans, example of this
10604code is in threads.xs create
10605
10606CLONEf_CLONE_HOST
10607This is a win32 thing, it is ignored on unix, it tells perls
10608win32host code (which is c++) to clone itself, this is needed on
10609win32 if you want to run two threads at the same time,
10610if you just want to do some stuff in a separate perl interpreter
10611and then throw it away and return to the original one,
10612you don't need to do anything.
10613
645c22ef
DM
10614=cut
10615*/
10616
10617/* XXX the above needs expanding by someone who actually understands it ! */
3fc56081
NK
10618EXTERN_C PerlInterpreter *
10619perl_clone_host(PerlInterpreter* proto_perl, UV flags);
645c22ef 10620
1d7c1841
GS
10621PerlInterpreter *
10622perl_clone(PerlInterpreter *proto_perl, UV flags)
10623{
1d7c1841 10624#ifdef PERL_IMPLICIT_SYS
c43294b8
AB
10625
10626 /* perlhost.h so we need to call into it
10627 to clone the host, CPerlHost should have a c interface, sky */
10628
10629 if (flags & CLONEf_CLONE_HOST) {
10630 return perl_clone_host(proto_perl,flags);
10631 }
10632 return perl_clone_using(proto_perl, flags,
1d7c1841
GS
10633 proto_perl->IMem,
10634 proto_perl->IMemShared,
10635 proto_perl->IMemParse,
10636 proto_perl->IEnv,
10637 proto_perl->IStdIO,
10638 proto_perl->ILIO,
10639 proto_perl->IDir,
10640 proto_perl->ISock,
10641 proto_perl->IProc);
10642}
10643
10644PerlInterpreter *
10645perl_clone_using(PerlInterpreter *proto_perl, UV flags,
10646 struct IPerlMem* ipM, struct IPerlMem* ipMS,
10647 struct IPerlMem* ipMP, struct IPerlEnv* ipE,
10648 struct IPerlStdIO* ipStd, struct IPerlLIO* ipLIO,
10649 struct IPerlDir* ipD, struct IPerlSock* ipS,
10650 struct IPerlProc* ipP)
10651{
10652 /* XXX many of the string copies here can be optimized if they're
10653 * constants; they need to be allocated as common memory and just
10654 * their pointers copied. */
10655
10656 IV i;
64aa0685
GS
10657 CLONE_PARAMS clone_params;
10658 CLONE_PARAMS* param = &clone_params;
d2d73c3e 10659
1d7c1841 10660 PerlInterpreter *my_perl = (PerlInterpreter*)(*ipM->pMalloc)(ipM, sizeof(PerlInterpreter));
ba869deb 10661 PERL_SET_THX(my_perl);
1d7c1841 10662
acfe0abc 10663# ifdef DEBUGGING
a4530404 10664 Poison(my_perl, 1, PerlInterpreter);
1d7c1841
GS
10665 PL_markstack = 0;
10666 PL_scopestack = 0;
10667 PL_savestack = 0;
26776375
JH
10668 PL_savestack_ix = 0;
10669 PL_savestack_max = -1;
1d7c1841 10670 PL_retstack = 0;
66fe0623 10671 PL_sig_pending = 0;
25596c82 10672 Zero(&PL_debug_pad, 1, struct perl_debug_pad);
acfe0abc 10673# else /* !DEBUGGING */
1d7c1841 10674 Zero(my_perl, 1, PerlInterpreter);
acfe0abc 10675# endif /* DEBUGGING */
1d7c1841
GS
10676
10677 /* host pointers */
10678 PL_Mem = ipM;
10679 PL_MemShared = ipMS;
10680 PL_MemParse = ipMP;
10681 PL_Env = ipE;
10682 PL_StdIO = ipStd;
10683 PL_LIO = ipLIO;
10684 PL_Dir = ipD;
10685 PL_Sock = ipS;
10686 PL_Proc = ipP;
1d7c1841
GS
10687#else /* !PERL_IMPLICIT_SYS */
10688 IV i;
64aa0685
GS
10689 CLONE_PARAMS clone_params;
10690 CLONE_PARAMS* param = &clone_params;
1d7c1841 10691 PerlInterpreter *my_perl = (PerlInterpreter*)PerlMem_malloc(sizeof(PerlInterpreter));
ba869deb 10692 PERL_SET_THX(my_perl);
1d7c1841 10693
d2d73c3e
AB
10694
10695
1d7c1841 10696# ifdef DEBUGGING
a4530404 10697 Poison(my_perl, 1, PerlInterpreter);
1d7c1841
GS
10698 PL_markstack = 0;
10699 PL_scopestack = 0;
10700 PL_savestack = 0;
26776375
JH
10701 PL_savestack_ix = 0;
10702 PL_savestack_max = -1;
1d7c1841 10703 PL_retstack = 0;
66fe0623 10704 PL_sig_pending = 0;
25596c82 10705 Zero(&PL_debug_pad, 1, struct perl_debug_pad);
1d7c1841
GS
10706# else /* !DEBUGGING */
10707 Zero(my_perl, 1, PerlInterpreter);
10708# endif /* DEBUGGING */
10709#endif /* PERL_IMPLICIT_SYS */
83236556 10710 param->flags = flags;
59b40662 10711 param->proto_perl = proto_perl;
1d7c1841
GS
10712
10713 /* arena roots */
10714 PL_xiv_arenaroot = NULL;
10715 PL_xiv_root = NULL;
612f20c3 10716 PL_xnv_arenaroot = NULL;
1d7c1841 10717 PL_xnv_root = NULL;
612f20c3 10718 PL_xrv_arenaroot = NULL;
1d7c1841 10719 PL_xrv_root = NULL;
612f20c3 10720 PL_xpv_arenaroot = NULL;
1d7c1841 10721 PL_xpv_root = NULL;
612f20c3 10722 PL_xpviv_arenaroot = NULL;
1d7c1841 10723 PL_xpviv_root = NULL;
612f20c3 10724 PL_xpvnv_arenaroot = NULL;
1d7c1841 10725 PL_xpvnv_root = NULL;
612f20c3 10726 PL_xpvcv_arenaroot = NULL;
1d7c1841 10727 PL_xpvcv_root = NULL;
612f20c3 10728 PL_xpvav_arenaroot = NULL;
1d7c1841 10729 PL_xpvav_root = NULL;
612f20c3 10730 PL_xpvhv_arenaroot = NULL;
1d7c1841 10731 PL_xpvhv_root = NULL;
612f20c3 10732 PL_xpvmg_arenaroot = NULL;
1d7c1841 10733 PL_xpvmg_root = NULL;
612f20c3 10734 PL_xpvlv_arenaroot = NULL;
1d7c1841 10735 PL_xpvlv_root = NULL;
612f20c3 10736 PL_xpvbm_arenaroot = NULL;
1d7c1841 10737 PL_xpvbm_root = NULL;
612f20c3 10738 PL_he_arenaroot = NULL;
1d7c1841
GS
10739 PL_he_root = NULL;
10740 PL_nice_chunk = NULL;
10741 PL_nice_chunk_size = 0;
10742 PL_sv_count = 0;
10743 PL_sv_objcount = 0;
10744 PL_sv_root = Nullsv;
10745 PL_sv_arenaroot = Nullsv;
10746
10747 PL_debug = proto_perl->Idebug;
10748
e5dd39fc 10749#ifdef USE_REENTRANT_API
1c693a8c
NC
10750 /* XXX: things like -Dm will segfault here in perlio, but doing
10751 * PERL_SET_CONTEXT(proto_perl);
10752 * breaks too many other things
10753 */
59bd0823 10754 Perl_reentrant_init(aTHX);
e5dd39fc
AB
10755#endif
10756
1d7c1841
GS
10757 /* create SV map for pointer relocation */
10758 PL_ptr_table = ptr_table_new();
10759
10760 /* initialize these special pointers as early as possible */
10761 SvANY(&PL_sv_undef) = NULL;
10762 SvREFCNT(&PL_sv_undef) = (~(U32)0)/2;
10763 SvFLAGS(&PL_sv_undef) = SVf_READONLY|SVt_NULL;
10764 ptr_table_store(PL_ptr_table, &proto_perl->Isv_undef, &PL_sv_undef);
10765
1d7c1841 10766 SvANY(&PL_sv_no) = new_XPVNV();
1d7c1841
GS
10767 SvREFCNT(&PL_sv_no) = (~(U32)0)/2;
10768 SvFLAGS(&PL_sv_no) = SVp_NOK|SVf_NOK|SVp_POK|SVf_POK|SVf_READONLY|SVt_PVNV;
10769 SvPVX(&PL_sv_no) = SAVEPVN(PL_No, 0);
10770 SvCUR(&PL_sv_no) = 0;
10771 SvLEN(&PL_sv_no) = 1;
10772 SvNVX(&PL_sv_no) = 0;
10773 ptr_table_store(PL_ptr_table, &proto_perl->Isv_no, &PL_sv_no);
10774
1d7c1841 10775 SvANY(&PL_sv_yes) = new_XPVNV();
1d7c1841
GS
10776 SvREFCNT(&PL_sv_yes) = (~(U32)0)/2;
10777 SvFLAGS(&PL_sv_yes) = SVp_NOK|SVf_NOK|SVp_POK|SVf_POK|SVf_READONLY|SVt_PVNV;
10778 SvPVX(&PL_sv_yes) = SAVEPVN(PL_Yes, 1);
10779 SvCUR(&PL_sv_yes) = 1;
10780 SvLEN(&PL_sv_yes) = 2;
10781 SvNVX(&PL_sv_yes) = 1;
10782 ptr_table_store(PL_ptr_table, &proto_perl->Isv_yes, &PL_sv_yes);
10783
05ec9bb3 10784 /* create (a non-shared!) shared string table */
1d7c1841
GS
10785 PL_strtab = newHV();
10786 HvSHAREKEYS_off(PL_strtab);
10787 hv_ksplit(PL_strtab, 512);
10788 ptr_table_store(PL_ptr_table, proto_perl->Istrtab, PL_strtab);
10789
05ec9bb3
NIS
10790 PL_compiling = proto_perl->Icompiling;
10791
10792 /* These two PVs will be free'd special way so must set them same way op.c does */
10793 PL_compiling.cop_stashpv = savesharedpv(PL_compiling.cop_stashpv);
10794 ptr_table_store(PL_ptr_table, proto_perl->Icompiling.cop_stashpv, PL_compiling.cop_stashpv);
10795
10796 PL_compiling.cop_file = savesharedpv(PL_compiling.cop_file);
10797 ptr_table_store(PL_ptr_table, proto_perl->Icompiling.cop_file, PL_compiling.cop_file);
10798
1d7c1841
GS
10799 ptr_table_store(PL_ptr_table, &proto_perl->Icompiling, &PL_compiling);
10800 if (!specialWARN(PL_compiling.cop_warnings))
d2d73c3e 10801 PL_compiling.cop_warnings = sv_dup_inc(PL_compiling.cop_warnings, param);
ac27b0f5 10802 if (!specialCopIO(PL_compiling.cop_io))
d2d73c3e 10803 PL_compiling.cop_io = sv_dup_inc(PL_compiling.cop_io, param);
1d7c1841
GS
10804 PL_curcop = (COP*)any_dup(proto_perl->Tcurcop, proto_perl);
10805
10806 /* pseudo environmental stuff */
10807 PL_origargc = proto_perl->Iorigargc;
7223e9d8 10808 PL_origargv = proto_perl->Iorigargv;
d2d73c3e 10809
d2d73c3e
AB
10810 param->stashes = newAV(); /* Setup array of objects to call clone on */
10811
a1ea730d 10812#ifdef PERLIO_LAYERS
3a1ee7e8
NIS
10813 /* Clone PerlIO tables as soon as we can handle general xx_dup() */
10814 PerlIO_clone(aTHX_ proto_perl, param);
a1ea730d 10815#endif
d2d73c3e
AB
10816
10817 PL_envgv = gv_dup(proto_perl->Ienvgv, param);
10818 PL_incgv = gv_dup(proto_perl->Iincgv, param);
10819 PL_hintgv = gv_dup(proto_perl->Ihintgv, param);
1d7c1841 10820 PL_origfilename = SAVEPV(proto_perl->Iorigfilename);
d2d73c3e
AB
10821 PL_diehook = sv_dup_inc(proto_perl->Idiehook, param);
10822 PL_warnhook = sv_dup_inc(proto_perl->Iwarnhook, param);
1d7c1841
GS
10823
10824 /* switches */
10825 PL_minus_c = proto_perl->Iminus_c;
d2d73c3e 10826 PL_patchlevel = sv_dup_inc(proto_perl->Ipatchlevel, param);
1d7c1841
GS
10827 PL_localpatches = proto_perl->Ilocalpatches;
10828 PL_splitstr = proto_perl->Isplitstr;
10829 PL_preprocess = proto_perl->Ipreprocess;
10830 PL_minus_n = proto_perl->Iminus_n;
10831 PL_minus_p = proto_perl->Iminus_p;
10832 PL_minus_l = proto_perl->Iminus_l;
10833 PL_minus_a = proto_perl->Iminus_a;
10834 PL_minus_F = proto_perl->Iminus_F;
10835 PL_doswitches = proto_perl->Idoswitches;
10836 PL_dowarn = proto_perl->Idowarn;
10837 PL_doextract = proto_perl->Idoextract;
10838 PL_sawampersand = proto_perl->Isawampersand;
10839 PL_unsafe = proto_perl->Iunsafe;
10840 PL_inplace = SAVEPV(proto_perl->Iinplace);
d2d73c3e 10841 PL_e_script = sv_dup_inc(proto_perl->Ie_script, param);
1d7c1841
GS
10842 PL_perldb = proto_perl->Iperldb;
10843 PL_perl_destruct_level = proto_perl->Iperl_destruct_level;
1cbb0781 10844 PL_exit_flags = proto_perl->Iexit_flags;
1d7c1841
GS
10845
10846 /* magical thingies */
10847 /* XXX time(&PL_basetime) when asked for? */
10848 PL_basetime = proto_perl->Ibasetime;
d2d73c3e 10849 PL_formfeed = sv_dup(proto_perl->Iformfeed, param);
1d7c1841
GS
10850
10851 PL_maxsysfd = proto_perl->Imaxsysfd;
10852 PL_multiline = proto_perl->Imultiline;
10853 PL_statusvalue = proto_perl->Istatusvalue;
10854#ifdef VMS
10855 PL_statusvalue_vms = proto_perl->Istatusvalue_vms;
10856#endif
0a378802 10857 PL_encoding = sv_dup(proto_perl->Iencoding, param);
1d7c1841 10858
4a4c6fe3 10859 sv_setpvn(PERL_DEBUG_PAD(0), "", 0); /* For regex debugging. */
1f483ca1
JH
10860 sv_setpvn(PERL_DEBUG_PAD(1), "", 0); /* ext/re needs these */
10861 sv_setpvn(PERL_DEBUG_PAD(2), "", 0); /* even without DEBUGGING. */
4a4c6fe3 10862
d2f185dc
AMS
10863 /* Clone the regex array */
10864 PL_regex_padav = newAV();
10865 {
10866 I32 len = av_len((AV*)proto_perl->Iregex_padav);
10867 SV** regexen = AvARRAY((AV*)proto_perl->Iregex_padav);
0f95fc41
AB
10868 av_push(PL_regex_padav,
10869 sv_dup_inc(regexen[0],param));
10870 for(i = 1; i <= len; i++) {
10871 if(SvREPADTMP(regexen[i])) {
10872 av_push(PL_regex_padav, sv_dup_inc(regexen[i], param));
8cf8f3d1 10873 } else {
0f95fc41
AB
10874 av_push(PL_regex_padav,
10875 SvREFCNT_inc(
8cf8f3d1 10876 newSViv(PTR2IV(re_dup(INT2PTR(REGEXP *,
cbfa9890 10877 SvIVX(regexen[i])), param)))
0f95fc41
AB
10878 ));
10879 }
d2f185dc
AMS
10880 }
10881 }
10882 PL_regex_pad = AvARRAY(PL_regex_padav);
1fcf4c12 10883
1d7c1841 10884 /* shortcuts to various I/O objects */
d2d73c3e
AB
10885 PL_stdingv = gv_dup(proto_perl->Istdingv, param);
10886 PL_stderrgv = gv_dup(proto_perl->Istderrgv, param);
10887 PL_defgv = gv_dup(proto_perl->Idefgv, param);
10888 PL_argvgv = gv_dup(proto_perl->Iargvgv, param);
10889 PL_argvoutgv = gv_dup(proto_perl->Iargvoutgv, param);
10890 PL_argvout_stack = av_dup_inc(proto_perl->Iargvout_stack, param);
1d7c1841
GS
10891
10892 /* shortcuts to regexp stuff */
d2d73c3e 10893 PL_replgv = gv_dup(proto_perl->Ireplgv, param);
1d7c1841
GS
10894
10895 /* shortcuts to misc objects */
d2d73c3e 10896 PL_errgv = gv_dup(proto_perl->Ierrgv, param);
1d7c1841
GS
10897
10898 /* shortcuts to debugging objects */
d2d73c3e
AB
10899 PL_DBgv = gv_dup(proto_perl->IDBgv, param);
10900 PL_DBline = gv_dup(proto_perl->IDBline, param);
10901 PL_DBsub = gv_dup(proto_perl->IDBsub, param);
10902 PL_DBsingle = sv_dup(proto_perl->IDBsingle, param);
10903 PL_DBtrace = sv_dup(proto_perl->IDBtrace, param);
10904 PL_DBsignal = sv_dup(proto_perl->IDBsignal, param);
10905 PL_lineary = av_dup(proto_perl->Ilineary, param);
10906 PL_dbargs = av_dup(proto_perl->Idbargs, param);
1d7c1841
GS
10907
10908 /* symbol tables */
d2d73c3e
AB
10909 PL_defstash = hv_dup_inc(proto_perl->Tdefstash, param);
10910 PL_curstash = hv_dup(proto_perl->Tcurstash, param);
10911 PL_nullstash = hv_dup(proto_perl->Inullstash, param);
10912 PL_debstash = hv_dup(proto_perl->Idebstash, param);
10913 PL_globalstash = hv_dup(proto_perl->Iglobalstash, param);
10914 PL_curstname = sv_dup_inc(proto_perl->Icurstname, param);
10915
10916 PL_beginav = av_dup_inc(proto_perl->Ibeginav, param);
ee1c5a4e 10917 PL_beginav_save = av_dup_inc(proto_perl->Ibeginav_save, param);
5b7ea690 10918 PL_checkav_save = av_dup_inc(proto_perl->Icheckav_save, param);
d2d73c3e
AB
10919 PL_endav = av_dup_inc(proto_perl->Iendav, param);
10920 PL_checkav = av_dup_inc(proto_perl->Icheckav, param);
10921 PL_initav = av_dup_inc(proto_perl->Iinitav, param);
1d7c1841
GS
10922
10923 PL_sub_generation = proto_perl->Isub_generation;
10924
10925 /* funky return mechanisms */
10926 PL_forkprocess = proto_perl->Iforkprocess;
10927
10928 /* subprocess state */
d2d73c3e 10929 PL_fdpid = av_dup_inc(proto_perl->Ifdpid, param);
1d7c1841
GS
10930
10931 /* internal state */
10932 PL_tainting = proto_perl->Itainting;
654c77f7 10933 PL_taint_warn = proto_perl->Itaint_warn;
1d7c1841
GS
10934 PL_maxo = proto_perl->Imaxo;
10935 if (proto_perl->Iop_mask)
10936 PL_op_mask = SAVEPVN(proto_perl->Iop_mask, PL_maxo);
10937 else
10938 PL_op_mask = Nullch;
10939
10940 /* current interpreter roots */
d2d73c3e 10941 PL_main_cv = cv_dup_inc(proto_perl->Imain_cv, param);
1d7c1841
GS
10942 PL_main_root = OpREFCNT_inc(proto_perl->Imain_root);
10943 PL_main_start = proto_perl->Imain_start;
e977893f 10944 PL_eval_root = proto_perl->Ieval_root;
1d7c1841
GS
10945 PL_eval_start = proto_perl->Ieval_start;
10946
10947 /* runtime control stuff */
10948 PL_curcopdb = (COP*)any_dup(proto_perl->Icurcopdb, proto_perl);
10949 PL_copline = proto_perl->Icopline;
10950
10951 PL_filemode = proto_perl->Ifilemode;
10952 PL_lastfd = proto_perl->Ilastfd;
10953 PL_oldname = proto_perl->Ioldname; /* XXX not quite right */
10954 PL_Argv = NULL;
10955 PL_Cmd = Nullch;
10956 PL_gensym = proto_perl->Igensym;
10957 PL_preambled = proto_perl->Ipreambled;
d2d73c3e 10958 PL_preambleav = av_dup_inc(proto_perl->Ipreambleav, param);
1d7c1841
GS
10959 PL_laststatval = proto_perl->Ilaststatval;
10960 PL_laststype = proto_perl->Ilaststype;
10961 PL_mess_sv = Nullsv;
10962
d2d73c3e 10963 PL_ors_sv = sv_dup_inc(proto_perl->Iors_sv, param);
1d7c1841
GS
10964 PL_ofmt = SAVEPV(proto_perl->Iofmt);
10965
10966 /* interpreter atexit processing */
10967 PL_exitlistlen = proto_perl->Iexitlistlen;
10968 if (PL_exitlistlen) {
10969 New(0, PL_exitlist, PL_exitlistlen, PerlExitListEntry);
10970 Copy(proto_perl->Iexitlist, PL_exitlist, PL_exitlistlen, PerlExitListEntry);
10971 }
10972 else
10973 PL_exitlist = (PerlExitListEntry*)NULL;
d2d73c3e 10974 PL_modglobal = hv_dup_inc(proto_perl->Imodglobal, param);
19e8ce8e
AB
10975 PL_custom_op_names = hv_dup_inc(proto_perl->Icustom_op_names,param);
10976 PL_custom_op_descs = hv_dup_inc(proto_perl->Icustom_op_descs,param);
1d7c1841
GS
10977
10978 PL_profiledata = NULL;
a8fc9800 10979 PL_rsfp = fp_dup(proto_perl->Irsfp, '<', param);
1d7c1841 10980 /* PL_rsfp_filters entries have fake IoDIRP() */
d2d73c3e 10981 PL_rsfp_filters = av_dup_inc(proto_perl->Irsfp_filters, param);
1d7c1841 10982
d2d73c3e 10983 PL_compcv = cv_dup(proto_perl->Icompcv, param);
9755d405
JH
10984
10985 PAD_CLONE_VARS(proto_perl, param);
1d7c1841
GS
10986
10987#ifdef HAVE_INTERP_INTERN
10988 sys_intern_dup(&proto_perl->Isys_intern, &PL_sys_intern);
10989#endif
10990
10991 /* more statics moved here */
10992 PL_generation = proto_perl->Igeneration;
d2d73c3e 10993 PL_DBcv = cv_dup(proto_perl->IDBcv, param);
1d7c1841
GS
10994
10995 PL_in_clean_objs = proto_perl->Iin_clean_objs;
10996 PL_in_clean_all = proto_perl->Iin_clean_all;
10997
10998 PL_uid = proto_perl->Iuid;
10999 PL_euid = proto_perl->Ieuid;
11000 PL_gid = proto_perl->Igid;
11001 PL_egid = proto_perl->Iegid;
11002 PL_nomemok = proto_perl->Inomemok;
11003 PL_an = proto_perl->Ian;
1d7c1841
GS
11004 PL_op_seqmax = proto_perl->Iop_seqmax;
11005 PL_evalseq = proto_perl->Ievalseq;
11006 PL_origenviron = proto_perl->Iorigenviron; /* XXX not quite right */
11007 PL_origalen = proto_perl->Iorigalen;
11008 PL_pidstatus = newHV(); /* XXX flag for cloning? */
11009 PL_osname = SAVEPV(proto_perl->Iosname);
8257dec7 11010 PL_sh_path_compat = proto_perl->Ish_path_compat; /* XXX never deallocated */
1d7c1841
GS
11011 PL_sighandlerp = proto_perl->Isighandlerp;
11012
11013
11014 PL_runops = proto_perl->Irunops;
11015
11016 Copy(proto_perl->Itokenbuf, PL_tokenbuf, 256, char);
11017
11018#ifdef CSH
11019 PL_cshlen = proto_perl->Icshlen;
74f1b2b8 11020 PL_cshname = proto_perl->Icshname; /* XXX never deallocated */
1d7c1841
GS
11021#endif
11022
11023 PL_lex_state = proto_perl->Ilex_state;
11024 PL_lex_defer = proto_perl->Ilex_defer;
11025 PL_lex_expect = proto_perl->Ilex_expect;
11026 PL_lex_formbrack = proto_perl->Ilex_formbrack;
11027 PL_lex_dojoin = proto_perl->Ilex_dojoin;
11028 PL_lex_starts = proto_perl->Ilex_starts;
d2d73c3e
AB
11029 PL_lex_stuff = sv_dup_inc(proto_perl->Ilex_stuff, param);
11030 PL_lex_repl = sv_dup_inc(proto_perl->Ilex_repl, param);
1d7c1841
GS
11031 PL_lex_op = proto_perl->Ilex_op;
11032 PL_lex_inpat = proto_perl->Ilex_inpat;
11033 PL_lex_inwhat = proto_perl->Ilex_inwhat;
11034 PL_lex_brackets = proto_perl->Ilex_brackets;
11035 i = (PL_lex_brackets < 120 ? 120 : PL_lex_brackets);
11036 PL_lex_brackstack = SAVEPVN(proto_perl->Ilex_brackstack,i);
11037 PL_lex_casemods = proto_perl->Ilex_casemods;
11038 i = (PL_lex_casemods < 12 ? 12 : PL_lex_casemods);
11039 PL_lex_casestack = SAVEPVN(proto_perl->Ilex_casestack,i);
11040
11041 Copy(proto_perl->Inextval, PL_nextval, 5, YYSTYPE);
11042 Copy(proto_perl->Inexttype, PL_nexttype, 5, I32);
11043 PL_nexttoke = proto_perl->Inexttoke;
11044
1d773130
TB
11045 /* XXX This is probably masking the deeper issue of why
11046 * SvANY(proto_perl->Ilinestr) can be NULL at this point. For test case:
11047 * http://archive.develooper.com/perl5-porters%40perl.org/msg83298.html
11048 * (A little debugging with a watchpoint on it may help.)
11049 */
389edf32
TB
11050 if (SvANY(proto_perl->Ilinestr)) {
11051 PL_linestr = sv_dup_inc(proto_perl->Ilinestr, param);
11052 i = proto_perl->Ibufptr - SvPVX(proto_perl->Ilinestr);
11053 PL_bufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
11054 i = proto_perl->Ioldbufptr - SvPVX(proto_perl->Ilinestr);
11055 PL_oldbufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
11056 i = proto_perl->Ioldoldbufptr - SvPVX(proto_perl->Ilinestr);
11057 PL_oldoldbufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
11058 i = proto_perl->Ilinestart - SvPVX(proto_perl->Ilinestr);
11059 PL_linestart = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
11060 }
11061 else {
11062 PL_linestr = NEWSV(65,79);
11063 sv_upgrade(PL_linestr,SVt_PVIV);
11064 sv_setpvn(PL_linestr,"",0);
11065 PL_bufptr = PL_oldbufptr = PL_oldoldbufptr = PL_linestart = SvPVX(PL_linestr);
11066 }
1d7c1841 11067 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
1d7c1841
GS
11068 PL_pending_ident = proto_perl->Ipending_ident;
11069 PL_sublex_info = proto_perl->Isublex_info; /* XXX not quite right */
11070
11071 PL_expect = proto_perl->Iexpect;
11072
11073 PL_multi_start = proto_perl->Imulti_start;
11074 PL_multi_end = proto_perl->Imulti_end;
11075 PL_multi_open = proto_perl->Imulti_open;
11076 PL_multi_close = proto_perl->Imulti_close;
11077
11078 PL_error_count = proto_perl->Ierror_count;
11079 PL_subline = proto_perl->Isubline;
d2d73c3e 11080 PL_subname = sv_dup_inc(proto_perl->Isubname, param);
1d7c1841 11081
1d773130 11082 /* XXX See comment on SvANY(proto_perl->Ilinestr) above */
389edf32
TB
11083 if (SvANY(proto_perl->Ilinestr)) {
11084 i = proto_perl->Ilast_uni - SvPVX(proto_perl->Ilinestr);
11085 PL_last_uni = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
11086 i = proto_perl->Ilast_lop - SvPVX(proto_perl->Ilinestr);
11087 PL_last_lop = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
11088 PL_last_lop_op = proto_perl->Ilast_lop_op;
11089 }
11090 else {
11091 PL_last_uni = SvPVX(PL_linestr);
11092 PL_last_lop = SvPVX(PL_linestr);
11093 PL_last_lop_op = 0;
11094 }
1d7c1841 11095 PL_in_my = proto_perl->Iin_my;
d2d73c3e 11096 PL_in_my_stash = hv_dup(proto_perl->Iin_my_stash, param);
1d7c1841
GS
11097#ifdef FCRYPT
11098 PL_cryptseen = proto_perl->Icryptseen;
11099#endif
11100
11101 PL_hints = proto_perl->Ihints;
11102
11103 PL_amagic_generation = proto_perl->Iamagic_generation;
11104
11105#ifdef USE_LOCALE_COLLATE
11106 PL_collation_ix = proto_perl->Icollation_ix;
11107 PL_collation_name = SAVEPV(proto_perl->Icollation_name);
11108 PL_collation_standard = proto_perl->Icollation_standard;
11109 PL_collxfrm_base = proto_perl->Icollxfrm_base;
11110 PL_collxfrm_mult = proto_perl->Icollxfrm_mult;
11111#endif /* USE_LOCALE_COLLATE */
11112
11113#ifdef USE_LOCALE_NUMERIC
11114 PL_numeric_name = SAVEPV(proto_perl->Inumeric_name);
11115 PL_numeric_standard = proto_perl->Inumeric_standard;
11116 PL_numeric_local = proto_perl->Inumeric_local;
d2d73c3e 11117 PL_numeric_radix_sv = sv_dup_inc(proto_perl->Inumeric_radix_sv, param);
1d7c1841
GS
11118#endif /* !USE_LOCALE_NUMERIC */
11119
11120 /* utf8 character classes */
d2d73c3e
AB
11121 PL_utf8_alnum = sv_dup_inc(proto_perl->Iutf8_alnum, param);
11122 PL_utf8_alnumc = sv_dup_inc(proto_perl->Iutf8_alnumc, param);
11123 PL_utf8_ascii = sv_dup_inc(proto_perl->Iutf8_ascii, param);
11124 PL_utf8_alpha = sv_dup_inc(proto_perl->Iutf8_alpha, param);
11125 PL_utf8_space = sv_dup_inc(proto_perl->Iutf8_space, param);
11126 PL_utf8_cntrl = sv_dup_inc(proto_perl->Iutf8_cntrl, param);
11127 PL_utf8_graph = sv_dup_inc(proto_perl->Iutf8_graph, param);
11128 PL_utf8_digit = sv_dup_inc(proto_perl->Iutf8_digit, param);
11129 PL_utf8_upper = sv_dup_inc(proto_perl->Iutf8_upper, param);
11130 PL_utf8_lower = sv_dup_inc(proto_perl->Iutf8_lower, param);
11131 PL_utf8_print = sv_dup_inc(proto_perl->Iutf8_print, param);
11132 PL_utf8_punct = sv_dup_inc(proto_perl->Iutf8_punct, param);
11133 PL_utf8_xdigit = sv_dup_inc(proto_perl->Iutf8_xdigit, param);
11134 PL_utf8_mark = sv_dup_inc(proto_perl->Iutf8_mark, param);
11135 PL_utf8_toupper = sv_dup_inc(proto_perl->Iutf8_toupper, param);
11136 PL_utf8_totitle = sv_dup_inc(proto_perl->Iutf8_totitle, param);
11137 PL_utf8_tolower = sv_dup_inc(proto_perl->Iutf8_tolower, param);
b4e400f9 11138 PL_utf8_tofold = sv_dup_inc(proto_perl->Iutf8_tofold, param);
82686b01
JH
11139 PL_utf8_idstart = sv_dup_inc(proto_perl->Iutf8_idstart, param);
11140 PL_utf8_idcont = sv_dup_inc(proto_perl->Iutf8_idcont, param);
1d7c1841 11141
654c77f7 11142 /* Did the locale setup indicate UTF-8? */
30260be2 11143 PL_utf8locale = proto_perl->Iutf8locale;
654c77f7
JH
11144 /* Unicode features (see perlrun/-C) */
11145 PL_unicode = proto_perl->Iunicode;
11146
11147 /* Pre-5.8 signals control */
11148 PL_signals = proto_perl->Isignals;
11149
11150 /* times() ticks per second */
11151 PL_clocktick = proto_perl->Iclocktick;
11152
11153 /* Recursion stopper for PerlIO_find_layer */
11154 PL_in_load_module = proto_perl->Iin_load_module;
11155
11156 /* sort() routine */
11157 PL_sort_RealCmp = proto_perl->Isort_RealCmp;
11158
630cd32b
JH
11159 /* Not really needed/useful since the reenrant_retint is "volatile",
11160 * but do it for consistency's sake. */
11161 PL_reentrant_retint = proto_perl->Ireentrant_retint;
11162
0710cc63
JH
11163 /* Hooks to shared SVs and locks. */
11164 PL_sharehook = proto_perl->Isharehook;
11165 PL_lockhook = proto_perl->Ilockhook;
11166 PL_unlockhook = proto_perl->Iunlockhook;
11167 PL_threadhook = proto_perl->Ithreadhook;
11168
11169 PL_runops_std = proto_perl->Irunops_std;
11170 PL_runops_dbg = proto_perl->Irunops_dbg;
11171
11172#ifdef THREADS_HAVE_PIDS
11173 PL_ppid = proto_perl->Ippid;
11174#endif
11175
1d7c1841
GS
11176 /* swatch cache */
11177 PL_last_swash_hv = Nullhv; /* reinits on demand */
11178 PL_last_swash_klen = 0;
11179 PL_last_swash_key[0]= '\0';
11180 PL_last_swash_tmps = (U8*)NULL;
11181 PL_last_swash_slen = 0;
11182
11183 /* perly.c globals */
11184 PL_yydebug = proto_perl->Iyydebug;
11185 PL_yynerrs = proto_perl->Iyynerrs;
11186 PL_yyerrflag = proto_perl->Iyyerrflag;
11187 PL_yychar = proto_perl->Iyychar;
11188 PL_yyval = proto_perl->Iyyval;
11189 PL_yylval = proto_perl->Iyylval;
11190
11191 PL_glob_index = proto_perl->Iglob_index;
11192 PL_srand_called = proto_perl->Isrand_called;
c71f9582 11193 PL_hash_seed = proto_perl->Ihash_seed;
7f047bfa 11194 PL_rehash_seed = proto_perl->Irehash_seed;
1d7c1841
GS
11195 PL_uudmap['M'] = 0; /* reinits on demand */
11196 PL_bitcount = Nullch; /* reinits on demand */
11197
66fe0623
NIS
11198 if (proto_perl->Ipsig_pend) {
11199 Newz(0, PL_psig_pend, SIG_SIZE, int);
9dd79c3f 11200 }
66fe0623
NIS
11201 else {
11202 PL_psig_pend = (int*)NULL;
11203 }
11204
1d7c1841 11205 if (proto_perl->Ipsig_ptr) {
76d3c696
JH
11206 Newz(0, PL_psig_ptr, SIG_SIZE, SV*);
11207 Newz(0, PL_psig_name, SIG_SIZE, SV*);
76d3c696 11208 for (i = 1; i < SIG_SIZE; i++) {
d2d73c3e
AB
11209 PL_psig_ptr[i] = sv_dup_inc(proto_perl->Ipsig_ptr[i], param);
11210 PL_psig_name[i] = sv_dup_inc(proto_perl->Ipsig_name[i], param);
1d7c1841
GS
11211 }
11212 }
11213 else {
11214 PL_psig_ptr = (SV**)NULL;
11215 PL_psig_name = (SV**)NULL;
11216 }
11217
11218 /* thrdvar.h stuff */
11219
a0739874 11220 if (flags & CLONEf_COPY_STACKS) {
1d7c1841
GS
11221 /* next allocation will be PL_tmps_stack[PL_tmps_ix+1] */
11222 PL_tmps_ix = proto_perl->Ttmps_ix;
11223 PL_tmps_max = proto_perl->Ttmps_max;
11224 PL_tmps_floor = proto_perl->Ttmps_floor;
11225 Newz(50, PL_tmps_stack, PL_tmps_max, SV*);
11226 i = 0;
11227 while (i <= PL_tmps_ix) {
d2d73c3e 11228 PL_tmps_stack[i] = sv_dup_inc(proto_perl->Ttmps_stack[i], param);
1d7c1841
GS
11229 ++i;
11230 }
11231
11232 /* next PUSHMARK() sets *(PL_markstack_ptr+1) */
11233 i = proto_perl->Tmarkstack_max - proto_perl->Tmarkstack;
11234 Newz(54, PL_markstack, i, I32);
11235 PL_markstack_max = PL_markstack + (proto_perl->Tmarkstack_max
11236 - proto_perl->Tmarkstack);
11237 PL_markstack_ptr = PL_markstack + (proto_perl->Tmarkstack_ptr
11238 - proto_perl->Tmarkstack);
11239 Copy(proto_perl->Tmarkstack, PL_markstack,
11240 PL_markstack_ptr - PL_markstack + 1, I32);
11241
11242 /* next push_scope()/ENTER sets PL_scopestack[PL_scopestack_ix]
11243 * NOTE: unlike the others! */
11244 PL_scopestack_ix = proto_perl->Tscopestack_ix;
11245 PL_scopestack_max = proto_perl->Tscopestack_max;
11246 Newz(54, PL_scopestack, PL_scopestack_max, I32);
11247 Copy(proto_perl->Tscopestack, PL_scopestack, PL_scopestack_ix, I32);
11248
11249 /* next push_return() sets PL_retstack[PL_retstack_ix]
11250 * NOTE: unlike the others! */
11251 PL_retstack_ix = proto_perl->Tretstack_ix;
11252 PL_retstack_max = proto_perl->Tretstack_max;
11253 Newz(54, PL_retstack, PL_retstack_max, OP*);
ce0a1ae0 11254 Copy(proto_perl->Tretstack, PL_retstack, PL_retstack_ix, OP*);
1d7c1841
GS
11255
11256 /* NOTE: si_dup() looks at PL_markstack */
d2d73c3e 11257 PL_curstackinfo = si_dup(proto_perl->Tcurstackinfo, param);
1d7c1841
GS
11258
11259 /* PL_curstack = PL_curstackinfo->si_stack; */
d2d73c3e
AB
11260 PL_curstack = av_dup(proto_perl->Tcurstack, param);
11261 PL_mainstack = av_dup(proto_perl->Tmainstack, param);
1d7c1841
GS
11262
11263 /* next PUSHs() etc. set *(PL_stack_sp+1) */
11264 PL_stack_base = AvARRAY(PL_curstack);
11265 PL_stack_sp = PL_stack_base + (proto_perl->Tstack_sp
11266 - proto_perl->Tstack_base);
11267 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
11268
11269 /* next SSPUSHFOO() sets PL_savestack[PL_savestack_ix]
11270 * NOTE: unlike the others! */
11271 PL_savestack_ix = proto_perl->Tsavestack_ix;
11272 PL_savestack_max = proto_perl->Tsavestack_max;
11273 /*Newz(54, PL_savestack, PL_savestack_max, ANY);*/
d2d73c3e 11274 PL_savestack = ss_dup(proto_perl, param);
1d7c1841
GS
11275 }
11276 else {
11277 init_stacks();
985e7056 11278 ENTER; /* perl_destruct() wants to LEAVE; */
1d7c1841
GS
11279 }
11280
11281 PL_start_env = proto_perl->Tstart_env; /* XXXXXX */
11282 PL_top_env = &PL_start_env;
11283
11284 PL_op = proto_perl->Top;
11285
11286 PL_Sv = Nullsv;
11287 PL_Xpv = (XPV*)NULL;
11288 PL_na = proto_perl->Tna;
11289
11290 PL_statbuf = proto_perl->Tstatbuf;
11291 PL_statcache = proto_perl->Tstatcache;
d2d73c3e
AB
11292 PL_statgv = gv_dup(proto_perl->Tstatgv, param);
11293 PL_statname = sv_dup_inc(proto_perl->Tstatname, param);
1d7c1841
GS
11294#ifdef HAS_TIMES
11295 PL_timesbuf = proto_perl->Ttimesbuf;
11296#endif
11297
11298 PL_tainted = proto_perl->Ttainted;
11299 PL_curpm = proto_perl->Tcurpm; /* XXX No PMOP ref count */
d2d73c3e
AB
11300 PL_rs = sv_dup_inc(proto_perl->Trs, param);
11301 PL_last_in_gv = gv_dup(proto_perl->Tlast_in_gv, param);
11302 PL_ofs_sv = sv_dup_inc(proto_perl->Tofs_sv, param);
11303 PL_defoutgv = gv_dup_inc(proto_perl->Tdefoutgv, param);
1d7c1841 11304 PL_chopset = proto_perl->Tchopset; /* XXX never deallocated */
d2d73c3e
AB
11305 PL_toptarget = sv_dup_inc(proto_perl->Ttoptarget, param);
11306 PL_bodytarget = sv_dup_inc(proto_perl->Tbodytarget, param);
11307 PL_formtarget = sv_dup(proto_perl->Tformtarget, param);
1d7c1841
GS
11308
11309 PL_restartop = proto_perl->Trestartop;
11310 PL_in_eval = proto_perl->Tin_eval;
11311 PL_delaymagic = proto_perl->Tdelaymagic;
11312 PL_dirty = proto_perl->Tdirty;
11313 PL_localizing = proto_perl->Tlocalizing;
11314
14dd3ad8 11315#ifdef PERL_FLEXIBLE_EXCEPTIONS
1d7c1841 11316 PL_protect = proto_perl->Tprotect;
14dd3ad8 11317#endif
d2d73c3e 11318 PL_errors = sv_dup_inc(proto_perl->Terrors, param);
73c86719 11319 PL_hv_fetch_ent_mh = Nullhe;
1d7c1841
GS
11320 PL_modcount = proto_perl->Tmodcount;
11321 PL_lastgotoprobe = Nullop;
11322 PL_dumpindent = proto_perl->Tdumpindent;
11323
11324 PL_sortcop = (OP*)any_dup(proto_perl->Tsortcop, proto_perl);
d2d73c3e
AB
11325 PL_sortstash = hv_dup(proto_perl->Tsortstash, param);
11326 PL_firstgv = gv_dup(proto_perl->Tfirstgv, param);
11327 PL_secondgv = gv_dup(proto_perl->Tsecondgv, param);
1d7c1841
GS
11328 PL_sortcxix = proto_perl->Tsortcxix;
11329 PL_efloatbuf = Nullch; /* reinits on demand */
11330 PL_efloatsize = 0; /* reinits on demand */
11331
11332 /* regex stuff */
11333
11334 PL_screamfirst = NULL;
11335 PL_screamnext = NULL;
11336 PL_maxscream = -1; /* reinits on demand */
11337 PL_lastscream = Nullsv;
11338
11339 PL_watchaddr = NULL;
11340 PL_watchok = Nullch;
11341
11342 PL_regdummy = proto_perl->Tregdummy;
11343 PL_regcomp_parse = Nullch;
11344 PL_regxend = Nullch;
11345 PL_regcode = (regnode*)NULL;
11346 PL_regnaughty = 0;
11347 PL_regsawback = 0;
11348 PL_regprecomp = Nullch;
11349 PL_regnpar = 0;
11350 PL_regsize = 0;
11351 PL_regflags = 0;
11352 PL_regseen = 0;
11353 PL_seen_zerolen = 0;
11354 PL_seen_evals = 0;
11355 PL_regcomp_rx = (regexp*)NULL;
11356 PL_extralen = 0;
11357 PL_colorset = 0; /* reinits PL_colors[] */
11358 /*PL_colors[6] = {0,0,0,0,0,0};*/
11359 PL_reg_whilem_seen = 0;
11360 PL_reginput = Nullch;
11361 PL_regbol = Nullch;
11362 PL_regeol = Nullch;
11363 PL_regstartp = (I32*)NULL;
11364 PL_regendp = (I32*)NULL;
11365 PL_reglastparen = (U32*)NULL;
4e58e0cf 11366 PL_reglastcloseparen = (U32*)NULL;
1d7c1841 11367 PL_regtill = Nullch;
1d7c1841
GS
11368 PL_reg_start_tmp = (char**)NULL;
11369 PL_reg_start_tmpl = 0;
11370 PL_regdata = (struct reg_data*)NULL;
11371 PL_bostr = Nullch;
11372 PL_reg_flags = 0;
11373 PL_reg_eval_set = 0;
11374 PL_regnarrate = 0;
11375 PL_regprogram = (regnode*)NULL;
11376 PL_regindent = 0;
11377 PL_regcc = (CURCUR*)NULL;
11378 PL_reg_call_cc = (struct re_cc_state*)NULL;
11379 PL_reg_re = (regexp*)NULL;
11380 PL_reg_ganch = Nullch;
11381 PL_reg_sv = Nullsv;
53c4c00c 11382 PL_reg_match_utf8 = FALSE;
1d7c1841
GS
11383 PL_reg_magic = (MAGIC*)NULL;
11384 PL_reg_oldpos = 0;
11385 PL_reg_oldcurpm = (PMOP*)NULL;
11386 PL_reg_curpm = (PMOP*)NULL;
11387 PL_reg_oldsaved = Nullch;
11388 PL_reg_oldsavedlen = 0;
11389 PL_reg_maxiter = 0;
11390 PL_reg_leftiter = 0;
11391 PL_reg_poscache = Nullch;
11392 PL_reg_poscache_size= 0;
11393
11394 /* RE engine - function pointers */
11395 PL_regcompp = proto_perl->Tregcompp;
11396 PL_regexecp = proto_perl->Tregexecp;
11397 PL_regint_start = proto_perl->Tregint_start;
11398 PL_regint_string = proto_perl->Tregint_string;
11399 PL_regfree = proto_perl->Tregfree;
11400
11401 PL_reginterp_cnt = 0;
11402 PL_reg_starttry = 0;
11403
a2efc822
SC
11404 /* Pluggable optimizer */
11405 PL_peepp = proto_perl->Tpeepp;
11406
c0401c5d
JH
11407 PL_stashcache = newHV();
11408
a0739874
DM
11409 if (!(flags & CLONEf_KEEP_PTR_TABLE)) {
11410 ptr_table_free(PL_ptr_table);
11411 PL_ptr_table = NULL;
11412 }
8cf8f3d1 11413
f284b03f
AMS
11414 /* Call the ->CLONE method, if it exists, for each of the stashes
11415 identified by sv_dup() above.
11416 */
d2d73c3e
AB
11417 while(av_len(param->stashes) != -1) {
11418 HV* stash = (HV*) av_shift(param->stashes);
f284b03f
AMS
11419 GV* cloner = gv_fetchmethod_autoload(stash, "CLONE", 0);
11420 if (cloner && GvCV(cloner)) {
11421 dSP;
11422 ENTER;
11423 SAVETMPS;
11424 PUSHMARK(SP);
dc507217 11425 XPUSHs(sv_2mortal(newSVpv(HvNAME(stash), 0)));
f284b03f
AMS
11426 PUTBACK;
11427 call_sv((SV*)GvCV(cloner), G_DISCARD);
11428 FREETMPS;
11429 LEAVE;
11430 }
4a09accc 11431 }
a0739874 11432
dc507217 11433 SvREFCNT_dec(param->stashes);
dc507217 11434
1d7c1841 11435 return my_perl;
1d7c1841
GS
11436}
11437
1d7c1841 11438#endif /* USE_ITHREADS */
a0ae6670 11439
9f4817db 11440/*
ccfc67b7
JH
11441=head1 Unicode Support
11442
9f4817db
JH
11443=for apidoc sv_recode_to_utf8
11444
5d170f3a
JH
11445The encoding is assumed to be an Encode object, on entry the PV
11446of the sv is assumed to be octets in that encoding, and the sv
11447will be converted into Unicode (and UTF-8).
9f4817db 11448
5d170f3a
JH
11449If the sv already is UTF-8 (or if it is not POK), or if the encoding
11450is not a reference, nothing is done to the sv. If the encoding is not
1768d7eb
JH
11451an C<Encode::XS> Encoding object, bad things will happen.
11452(See F<lib/encoding.pm> and L<Encode>).
9f4817db 11453
5d170f3a 11454The PV of the sv is returned.
9f4817db 11455
5d170f3a
JH
11456=cut */
11457
11458char *
11459Perl_sv_recode_to_utf8(pTHX_ SV *sv, SV *encoding)
11460{
975adce1 11461 if (SvPOK(sv) && !SvUTF8(sv) && !IN_BYTES && SvROK(encoding)) {
5b7ea690
JH
11462 SV *uni;
11463 STRLEN len;
11464 char *s;
11465 dSP;
11466 ENTER;
11467 SAVETMPS;
975adce1 11468 save_re_context();
5b7ea690
JH
11469 PUSHMARK(sp);
11470 EXTEND(SP, 3);
11471 XPUSHs(encoding);
11472 XPUSHs(sv);
f9893866
NIS
11473/*
11474 NI-S 2002/07/09
11475 Passing sv_yes is wrong - it needs to be or'ed set of constants
11476 for Encode::XS, while UTf-8 decode (currently) assumes a true value means
11477 remove converted chars from source.
11478
11479 Both will default the value - let them.
11480
5b7ea690 11481 XPUSHs(&PL_sv_yes);
f9893866 11482*/
5b7ea690
JH
11483 PUTBACK;
11484 call_method("decode", G_SCALAR);
11485 SPAGAIN;
11486 uni = POPs;
11487 PUTBACK;
11488 s = SvPV(uni, len);
5b7ea690
JH
11489 if (s != SvPVX(sv)) {
11490 SvGROW(sv, len + 1);
11491 Move(s, SvPVX(sv), len, char);
11492 SvCUR_set(sv, len);
11493 SvPVX(sv)[len] = 0;
11494 }
11495 FREETMPS;
11496 LEAVE;
5b7ea690 11497 SvUTF8_on(sv);
f9893866
NIS
11498 }
11499 return SvPVX(sv);
9f4817db
JH
11500}
11501
975adce1
JH
11502/*
11503=for apidoc sv_cat_decode
11504
11505The encoding is assumed to be an Encode object, the PV of the ssv is
11506assumed to be octets in that encoding and decoding the input starts
11507from the position which (PV + *offset) pointed to. The dsv will be
11508concatenated the decoded UTF-8 string from ssv. Decoding will terminate
11509when the string tstr appears in decoding output or the input ends on
11510the PV of the ssv. The value which the offset points will be modified
11511to the last input position on the ssv.
68795e93 11512
975adce1
JH
11513Returns TRUE if the terminator was found, else returns FALSE.
11514
11515=cut */
11516
11517bool
11518Perl_sv_cat_decode(pTHX_ SV *dsv, SV *encoding,
11519 SV *ssv, int *offset, char *tstr, int tlen)
11520{
c04fee9e 11521 bool ret = FALSE;
975adce1 11522 if (SvPOK(ssv) && SvPOK(dsv) && SvROK(encoding) && offset) {
975adce1
JH
11523 SV *offsv;
11524 dSP;
11525 ENTER;
11526 SAVETMPS;
11527 save_re_context();
11528 PUSHMARK(sp);
11529 EXTEND(SP, 6);
11530 XPUSHs(encoding);
11531 XPUSHs(dsv);
11532 XPUSHs(ssv);
11533 XPUSHs(offsv = sv_2mortal(newSViv(*offset)));
11534 XPUSHs(sv_2mortal(newSVpvn(tstr, tlen)));
11535 PUTBACK;
11536 call_method("cat_decode", G_SCALAR);
11537 SPAGAIN;
11538 ret = SvTRUE(TOPs);
11539 *offset = SvIV(offsv);
11540 PUTBACK;
11541 FREETMPS;
11542 LEAVE;
975adce1 11543 }
c04fee9e
JH
11544 else
11545 Perl_croak(aTHX_ "Invalid argument to sv_cat_decode");
11546 return ret;
975adce1 11547}
f9893866 11548