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