This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
I don't think it's sensible or portable to test the strtou*
[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)
1c846c1f 837
d33b2eba
GS
838#define new_XPVMG() my_safemalloc(sizeof(XPVMG))
839#define del_XPVMG(p) my_safefree(p)
840
841#define new_XPVLV() my_safemalloc(sizeof(XPVLV))
842#define del_XPVLV(p) my_safefree(p)
843
844#define new_XPVBM() my_safemalloc(sizeof(XPVBM))
845#define del_XPVBM(p) my_safefree(p)
846
847#else /* !PURIFY */
848
849#define new_XIV() (void*)new_xiv()
850#define del_XIV(p) del_xiv((XPVIV*) p)
851
852#define new_XNV() (void*)new_xnv()
853#define del_XNV(p) del_xnv((XPVNV*) p)
9b94d1dd 854
d33b2eba
GS
855#define new_XRV() (void*)new_xrv()
856#define del_XRV(p) del_xrv((XRV*) p)
9b94d1dd 857
d33b2eba
GS
858#define new_XPV() (void*)new_xpv()
859#define del_XPV(p) del_xpv((XPV *)p)
860
861#define new_XPVIV() (void*)new_xpviv()
862#define del_XPVIV(p) del_xpviv((XPVIV *)p)
863
864#define new_XPVNV() (void*)new_xpvnv()
865#define del_XPVNV(p) del_xpvnv((XPVNV *)p)
866
867#define new_XPVCV() (void*)new_xpvcv()
868#define del_XPVCV(p) del_xpvcv((XPVCV *)p)
869
870#define new_XPVAV() (void*)new_xpvav()
871#define del_XPVAV(p) del_xpvav((XPVAV *)p)
872
873#define new_XPVHV() (void*)new_xpvhv()
874#define del_XPVHV(p) del_xpvhv((XPVHV *)p)
1c846c1f 875
d33b2eba
GS
876#define new_XPVMG() (void*)new_xpvmg()
877#define del_XPVMG(p) del_xpvmg((XPVMG *)p)
878
879#define new_XPVLV() (void*)new_xpvlv()
880#define del_XPVLV(p) del_xpvlv((XPVLV *)p)
881
882#define new_XPVBM() (void*)new_xpvbm()
883#define del_XPVBM(p) del_xpvbm((XPVBM *)p)
884
885#endif /* PURIFY */
9b94d1dd 886
d33b2eba
GS
887#define new_XPVGV() my_safemalloc(sizeof(XPVGV))
888#define del_XPVGV(p) my_safefree(p)
1c846c1f 889
d33b2eba
GS
890#define new_XPVFM() my_safemalloc(sizeof(XPVFM))
891#define del_XPVFM(p) my_safefree(p)
1c846c1f 892
d33b2eba
GS
893#define new_XPVIO() my_safemalloc(sizeof(XPVIO))
894#define del_XPVIO(p) my_safefree(p)
8990e307 895
954c1994
GS
896/*
897=for apidoc sv_upgrade
898
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
f130fd45
NIS
916 if (mt != SVt_PV && SvREADONLY(sv) && SvFAKE(sv)) {
917 sv_force_normal(sv);
918 }
919
79072805
LW
920 if (SvTYPE(sv) == mt)
921 return TRUE;
922
a5f75d66
AD
923 if (mt < SVt_PVIV)
924 (void)SvOOK_off(sv);
925
79072805
LW
926 switch (SvTYPE(sv)) {
927 case SVt_NULL:
928 pv = 0;
929 cur = 0;
930 len = 0;
931 iv = 0;
932 nv = 0.0;
933 magic = 0;
934 stash = 0;
935 break;
79072805
LW
936 case SVt_IV:
937 pv = 0;
938 cur = 0;
939 len = 0;
463ee0b2 940 iv = SvIVX(sv);
65202027 941 nv = (NV)SvIVX(sv);
79072805
LW
942 del_XIV(SvANY(sv));
943 magic = 0;
944 stash = 0;
ed6116ce 945 if (mt == SVt_NV)
463ee0b2 946 mt = SVt_PVNV;
ed6116ce
LW
947 else if (mt < SVt_PVIV)
948 mt = SVt_PVIV;
79072805
LW
949 break;
950 case SVt_NV:
951 pv = 0;
952 cur = 0;
953 len = 0;
463ee0b2 954 nv = SvNVX(sv);
1bd302c3 955 iv = I_V(nv);
79072805
LW
956 magic = 0;
957 stash = 0;
958 del_XNV(SvANY(sv));
959 SvANY(sv) = 0;
ed6116ce 960 if (mt < SVt_PVNV)
79072805
LW
961 mt = SVt_PVNV;
962 break;
ed6116ce
LW
963 case SVt_RV:
964 pv = (char*)SvRV(sv);
965 cur = 0;
966 len = 0;
56431972
RB
967 iv = PTR2IV(pv);
968 nv = PTR2NV(pv);
ed6116ce
LW
969 del_XRV(SvANY(sv));
970 magic = 0;
971 stash = 0;
972 break;
79072805 973 case SVt_PV:
463ee0b2 974 pv = SvPVX(sv);
79072805
LW
975 cur = SvCUR(sv);
976 len = SvLEN(sv);
977 iv = 0;
978 nv = 0.0;
979 magic = 0;
980 stash = 0;
981 del_XPV(SvANY(sv));
748a9306
LW
982 if (mt <= SVt_IV)
983 mt = SVt_PVIV;
984 else if (mt == SVt_NV)
985 mt = SVt_PVNV;
79072805
LW
986 break;
987 case SVt_PVIV:
463ee0b2 988 pv = SvPVX(sv);
79072805
LW
989 cur = SvCUR(sv);
990 len = SvLEN(sv);
463ee0b2 991 iv = SvIVX(sv);
79072805
LW
992 nv = 0.0;
993 magic = 0;
994 stash = 0;
995 del_XPVIV(SvANY(sv));
996 break;
997 case SVt_PVNV:
463ee0b2 998 pv = SvPVX(sv);
79072805
LW
999 cur = SvCUR(sv);
1000 len = SvLEN(sv);
463ee0b2
LW
1001 iv = SvIVX(sv);
1002 nv = SvNVX(sv);
79072805
LW
1003 magic = 0;
1004 stash = 0;
1005 del_XPVNV(SvANY(sv));
1006 break;
1007 case SVt_PVMG:
463ee0b2 1008 pv = SvPVX(sv);
79072805
LW
1009 cur = SvCUR(sv);
1010 len = SvLEN(sv);
463ee0b2
LW
1011 iv = SvIVX(sv);
1012 nv = SvNVX(sv);
79072805
LW
1013 magic = SvMAGIC(sv);
1014 stash = SvSTASH(sv);
1015 del_XPVMG(SvANY(sv));
1016 break;
1017 default:
cea2e8a9 1018 Perl_croak(aTHX_ "Can't upgrade that kind of scalar");
79072805
LW
1019 }
1020
1021 switch (mt) {
1022 case SVt_NULL:
cea2e8a9 1023 Perl_croak(aTHX_ "Can't upgrade to undef");
79072805
LW
1024 case SVt_IV:
1025 SvANY(sv) = new_XIV();
463ee0b2 1026 SvIVX(sv) = iv;
79072805
LW
1027 break;
1028 case SVt_NV:
1029 SvANY(sv) = new_XNV();
463ee0b2 1030 SvNVX(sv) = nv;
79072805 1031 break;
ed6116ce
LW
1032 case SVt_RV:
1033 SvANY(sv) = new_XRV();
1034 SvRV(sv) = (SV*)pv;
ed6116ce 1035 break;
79072805
LW
1036 case SVt_PV:
1037 SvANY(sv) = new_XPV();
463ee0b2 1038 SvPVX(sv) = pv;
79072805
LW
1039 SvCUR(sv) = cur;
1040 SvLEN(sv) = len;
1041 break;
1042 case SVt_PVIV:
1043 SvANY(sv) = new_XPVIV();
463ee0b2 1044 SvPVX(sv) = pv;
79072805
LW
1045 SvCUR(sv) = cur;
1046 SvLEN(sv) = len;
463ee0b2 1047 SvIVX(sv) = iv;
79072805 1048 if (SvNIOK(sv))
a0d0e21e 1049 (void)SvIOK_on(sv);
79072805
LW
1050 SvNOK_off(sv);
1051 break;
1052 case SVt_PVNV:
1053 SvANY(sv) = new_XPVNV();
463ee0b2 1054 SvPVX(sv) = pv;
79072805
LW
1055 SvCUR(sv) = cur;
1056 SvLEN(sv) = len;
463ee0b2
LW
1057 SvIVX(sv) = iv;
1058 SvNVX(sv) = nv;
79072805
LW
1059 break;
1060 case SVt_PVMG:
1061 SvANY(sv) = new_XPVMG();
463ee0b2 1062 SvPVX(sv) = pv;
79072805
LW
1063 SvCUR(sv) = cur;
1064 SvLEN(sv) = len;
463ee0b2
LW
1065 SvIVX(sv) = iv;
1066 SvNVX(sv) = nv;
79072805
LW
1067 SvMAGIC(sv) = magic;
1068 SvSTASH(sv) = stash;
1069 break;
1070 case SVt_PVLV:
1071 SvANY(sv) = new_XPVLV();
463ee0b2 1072 SvPVX(sv) = pv;
79072805
LW
1073 SvCUR(sv) = cur;
1074 SvLEN(sv) = len;
463ee0b2
LW
1075 SvIVX(sv) = iv;
1076 SvNVX(sv) = nv;
79072805
LW
1077 SvMAGIC(sv) = magic;
1078 SvSTASH(sv) = stash;
1079 LvTARGOFF(sv) = 0;
1080 LvTARGLEN(sv) = 0;
1081 LvTARG(sv) = 0;
1082 LvTYPE(sv) = 0;
1083 break;
1084 case SVt_PVAV:
1085 SvANY(sv) = new_XPVAV();
463ee0b2
LW
1086 if (pv)
1087 Safefree(pv);
2304df62 1088 SvPVX(sv) = 0;
d1bf51dd 1089 AvMAX(sv) = -1;
93965878 1090 AvFILLp(sv) = -1;
463ee0b2
LW
1091 SvIVX(sv) = 0;
1092 SvNVX(sv) = 0.0;
1093 SvMAGIC(sv) = magic;
1094 SvSTASH(sv) = stash;
1095 AvALLOC(sv) = 0;
79072805
LW
1096 AvARYLEN(sv) = 0;
1097 AvFLAGS(sv) = 0;
1098 break;
1099 case SVt_PVHV:
1100 SvANY(sv) = new_XPVHV();
463ee0b2
LW
1101 if (pv)
1102 Safefree(pv);
1103 SvPVX(sv) = 0;
1104 HvFILL(sv) = 0;
1105 HvMAX(sv) = 0;
1106 HvKEYS(sv) = 0;
1107 SvNVX(sv) = 0.0;
79072805
LW
1108 SvMAGIC(sv) = magic;
1109 SvSTASH(sv) = stash;
79072805
LW
1110 HvRITER(sv) = 0;
1111 HvEITER(sv) = 0;
1112 HvPMROOT(sv) = 0;
1113 HvNAME(sv) = 0;
79072805
LW
1114 break;
1115 case SVt_PVCV:
1116 SvANY(sv) = new_XPVCV();
748a9306 1117 Zero(SvANY(sv), 1, XPVCV);
463ee0b2 1118 SvPVX(sv) = pv;
79072805
LW
1119 SvCUR(sv) = cur;
1120 SvLEN(sv) = len;
463ee0b2
LW
1121 SvIVX(sv) = iv;
1122 SvNVX(sv) = nv;
79072805
LW
1123 SvMAGIC(sv) = magic;
1124 SvSTASH(sv) = stash;
79072805
LW
1125 break;
1126 case SVt_PVGV:
1127 SvANY(sv) = new_XPVGV();
463ee0b2 1128 SvPVX(sv) = pv;
79072805
LW
1129 SvCUR(sv) = cur;
1130 SvLEN(sv) = len;
463ee0b2
LW
1131 SvIVX(sv) = iv;
1132 SvNVX(sv) = nv;
79072805
LW
1133 SvMAGIC(sv) = magic;
1134 SvSTASH(sv) = stash;
93a17b20 1135 GvGP(sv) = 0;
79072805
LW
1136 GvNAME(sv) = 0;
1137 GvNAMELEN(sv) = 0;
1138 GvSTASH(sv) = 0;
a5f75d66 1139 GvFLAGS(sv) = 0;
79072805
LW
1140 break;
1141 case SVt_PVBM:
1142 SvANY(sv) = new_XPVBM();
463ee0b2 1143 SvPVX(sv) = pv;
79072805
LW
1144 SvCUR(sv) = cur;
1145 SvLEN(sv) = len;
463ee0b2
LW
1146 SvIVX(sv) = iv;
1147 SvNVX(sv) = nv;
79072805
LW
1148 SvMAGIC(sv) = magic;
1149 SvSTASH(sv) = stash;
1150 BmRARE(sv) = 0;
1151 BmUSEFUL(sv) = 0;
1152 BmPREVIOUS(sv) = 0;
1153 break;
1154 case SVt_PVFM:
1155 SvANY(sv) = new_XPVFM();
748a9306 1156 Zero(SvANY(sv), 1, XPVFM);
463ee0b2 1157 SvPVX(sv) = pv;
79072805
LW
1158 SvCUR(sv) = cur;
1159 SvLEN(sv) = len;
463ee0b2
LW
1160 SvIVX(sv) = iv;
1161 SvNVX(sv) = nv;
79072805
LW
1162 SvMAGIC(sv) = magic;
1163 SvSTASH(sv) = stash;
79072805 1164 break;
8990e307
LW
1165 case SVt_PVIO:
1166 SvANY(sv) = new_XPVIO();
748a9306 1167 Zero(SvANY(sv), 1, XPVIO);
8990e307
LW
1168 SvPVX(sv) = pv;
1169 SvCUR(sv) = cur;
1170 SvLEN(sv) = len;
1171 SvIVX(sv) = iv;
1172 SvNVX(sv) = nv;
1173 SvMAGIC(sv) = magic;
1174 SvSTASH(sv) = stash;
85e6fe83 1175 IoPAGE_LEN(sv) = 60;
8990e307
LW
1176 break;
1177 }
1178 SvFLAGS(sv) &= ~SVTYPEMASK;
1179 SvFLAGS(sv) |= mt;
79072805
LW
1180 return TRUE;
1181}
1182
79072805 1183int
864dbfa3 1184Perl_sv_backoff(pTHX_ register SV *sv)
79072805
LW
1185{
1186 assert(SvOOK(sv));
463ee0b2
LW
1187 if (SvIVX(sv)) {
1188 char *s = SvPVX(sv);
1189 SvLEN(sv) += SvIVX(sv);
1190 SvPVX(sv) -= SvIVX(sv);
79072805 1191 SvIV_set(sv, 0);
463ee0b2 1192 Move(s, SvPVX(sv), SvCUR(sv)+1, char);
79072805
LW
1193 }
1194 SvFLAGS(sv) &= ~SVf_OOK;
a0d0e21e 1195 return 0;
79072805
LW
1196}
1197
954c1994
GS
1198/*
1199=for apidoc sv_grow
1200
1201Expands the character buffer in the SV. This will use C<sv_unref> and will
1202upgrade the SV to C<SVt_PV>. Returns a pointer to the character buffer.
1203Use C<SvGROW>.
1204
1205=cut
1206*/
1207
79072805 1208char *
864dbfa3 1209Perl_sv_grow(pTHX_ register SV *sv, register STRLEN newlen)
79072805
LW
1210{
1211 register char *s;
1212
55497cff 1213#ifdef HAS_64K_LIMIT
79072805 1214 if (newlen >= 0x10000) {
1d7c1841
GS
1215 PerlIO_printf(Perl_debug_log,
1216 "Allocation too large: %"UVxf"\n", (UV)newlen);
79072805
LW
1217 my_exit(1);
1218 }
55497cff 1219#endif /* HAS_64K_LIMIT */
a0d0e21e
LW
1220 if (SvROK(sv))
1221 sv_unref(sv);
79072805
LW
1222 if (SvTYPE(sv) < SVt_PV) {
1223 sv_upgrade(sv, SVt_PV);
463ee0b2 1224 s = SvPVX(sv);
79072805
LW
1225 }
1226 else if (SvOOK(sv)) { /* pv is offset? */
1227 sv_backoff(sv);
463ee0b2 1228 s = SvPVX(sv);
79072805
LW
1229 if (newlen > SvLEN(sv))
1230 newlen += 10 * (newlen - SvCUR(sv)); /* avoid copy each time */
c6f8c383
GA
1231#ifdef HAS_64K_LIMIT
1232 if (newlen >= 0x10000)
1233 newlen = 0xFFFF;
1234#endif
79072805
LW
1235 }
1236 else
463ee0b2 1237 s = SvPVX(sv);
79072805 1238 if (newlen > SvLEN(sv)) { /* need more room? */
8d6dde3e 1239 if (SvLEN(sv) && s) {
f5a32c7f 1240#if defined(MYMALLOC) && !defined(LEAKTEST)
8d6dde3e
IZ
1241 STRLEN l = malloced_size((void*)SvPVX(sv));
1242 if (newlen <= l) {
1243 SvLEN_set(sv, l);
1244 return s;
1245 } else
c70c8a0a 1246#endif
79072805 1247 Renew(s,newlen,char);
8d6dde3e 1248 }
79072805
LW
1249 else
1250 New(703,s,newlen,char);
1251 SvPV_set(sv, s);
1252 SvLEN_set(sv, newlen);
1253 }
1254 return s;
1255}
1256
954c1994
GS
1257/*
1258=for apidoc sv_setiv
1259
1260Copies an integer into the given SV. Does not handle 'set' magic. See
1261C<sv_setiv_mg>.
1262
1263=cut
1264*/
1265
79072805 1266void
864dbfa3 1267Perl_sv_setiv(pTHX_ register SV *sv, IV i)
79072805 1268{
2213622d 1269 SV_CHECK_THINKFIRST(sv);
463ee0b2
LW
1270 switch (SvTYPE(sv)) {
1271 case SVt_NULL:
79072805 1272 sv_upgrade(sv, SVt_IV);
463ee0b2
LW
1273 break;
1274 case SVt_NV:
1275 sv_upgrade(sv, SVt_PVNV);
1276 break;
ed6116ce 1277 case SVt_RV:
463ee0b2 1278 case SVt_PV:
79072805 1279 sv_upgrade(sv, SVt_PVIV);
463ee0b2 1280 break;
a0d0e21e
LW
1281
1282 case SVt_PVGV:
a0d0e21e
LW
1283 case SVt_PVAV:
1284 case SVt_PVHV:
1285 case SVt_PVCV:
1286 case SVt_PVFM:
1287 case SVt_PVIO:
411caa50
JH
1288 Perl_croak(aTHX_ "Can't coerce %s to integer in %s", sv_reftype(sv,0),
1289 PL_op_desc[PL_op->op_type]);
463ee0b2 1290 }
a0d0e21e 1291 (void)SvIOK_only(sv); /* validate number */
a5f75d66 1292 SvIVX(sv) = i;
463ee0b2 1293 SvTAINT(sv);
79072805
LW
1294}
1295
954c1994
GS
1296/*
1297=for apidoc sv_setiv_mg
1298
1299Like C<sv_setiv>, but also handles 'set' magic.
1300
1301=cut
1302*/
1303
79072805 1304void
864dbfa3 1305Perl_sv_setiv_mg(pTHX_ register SV *sv, IV i)
ef50df4b
GS
1306{
1307 sv_setiv(sv,i);
1308 SvSETMAGIC(sv);
1309}
1310
954c1994
GS
1311/*
1312=for apidoc sv_setuv
1313
1314Copies an unsigned integer into the given SV. Does not handle 'set' magic.
1315See C<sv_setuv_mg>.
1316
1317=cut
1318*/
1319
ef50df4b 1320void
864dbfa3 1321Perl_sv_setuv(pTHX_ register SV *sv, UV u)
55497cff 1322{
25da4f38
IZ
1323 sv_setiv(sv, 0);
1324 SvIsUV_on(sv);
1325 SvUVX(sv) = u;
55497cff
PP
1326}
1327
954c1994
GS
1328/*
1329=for apidoc sv_setuv_mg
1330
1331Like C<sv_setuv>, but also handles 'set' magic.
1332
1333=cut
1334*/
1335
55497cff 1336void
864dbfa3 1337Perl_sv_setuv_mg(pTHX_ register SV *sv, UV u)
ef50df4b 1338{
f7bbb42a 1339 sv_setuv(sv,u);
ef50df4b
GS
1340 SvSETMAGIC(sv);
1341}
1342
954c1994
GS
1343/*
1344=for apidoc sv_setnv
1345
1346Copies a double into the given SV. Does not handle 'set' magic. See
1347C<sv_setnv_mg>.
1348
1349=cut
1350*/
1351
ef50df4b 1352void
65202027 1353Perl_sv_setnv(pTHX_ register SV *sv, NV num)
79072805 1354{
2213622d 1355 SV_CHECK_THINKFIRST(sv);
a0d0e21e
LW
1356 switch (SvTYPE(sv)) {
1357 case SVt_NULL:
1358 case SVt_IV:
79072805 1359 sv_upgrade(sv, SVt_NV);
a0d0e21e 1360 break;
a0d0e21e
LW
1361 case SVt_RV:
1362 case SVt_PV:
1363 case SVt_PVIV:
79072805 1364 sv_upgrade(sv, SVt_PVNV);
a0d0e21e 1365 break;
827b7e14 1366
a0d0e21e 1367 case SVt_PVGV:
a0d0e21e
LW
1368 case SVt_PVAV:
1369 case SVt_PVHV:
1370 case SVt_PVCV:
1371 case SVt_PVFM:
1372 case SVt_PVIO:
411caa50
JH
1373 Perl_croak(aTHX_ "Can't coerce %s to number in %s", sv_reftype(sv,0),
1374 PL_op_name[PL_op->op_type]);
79072805 1375 }
463ee0b2 1376 SvNVX(sv) = num;
a0d0e21e 1377 (void)SvNOK_only(sv); /* validate number */
463ee0b2 1378 SvTAINT(sv);
79072805
LW
1379}
1380
954c1994
GS
1381/*
1382=for apidoc sv_setnv_mg
1383
1384Like C<sv_setnv>, but also handles 'set' magic.
1385
1386=cut
1387*/
1388
ef50df4b 1389void
65202027 1390Perl_sv_setnv_mg(pTHX_ register SV *sv, NV num)
ef50df4b
GS
1391{
1392 sv_setnv(sv,num);
1393 SvSETMAGIC(sv);
1394}
1395
76e3520e 1396STATIC void
cea2e8a9 1397S_not_a_number(pTHX_ SV *sv)
a0d0e21e
LW
1398{
1399 char tmpbuf[64];
1400 char *d = tmpbuf;
1401 char *s;
dc28f22b
GA
1402 char *limit = tmpbuf + sizeof(tmpbuf) - 8;
1403 /* each *s can expand to 4 chars + "...\0",
1404 i.e. need room for 8 chars */
a0d0e21e 1405
dc28f22b 1406 for (s = SvPVX(sv); *s && d < limit; s++) {
bbce6d69
PP
1407 int ch = *s & 0xFF;
1408 if (ch & 128 && !isPRINT_LC(ch)) {
a0d0e21e
LW
1409 *d++ = 'M';
1410 *d++ = '-';
1411 ch &= 127;
1412 }
bbce6d69
PP
1413 if (ch == '\n') {
1414 *d++ = '\\';
1415 *d++ = 'n';
1416 }
1417 else if (ch == '\r') {
1418 *d++ = '\\';
1419 *d++ = 'r';
1420 }
1421 else if (ch == '\f') {
1422 *d++ = '\\';
1423 *d++ = 'f';
1424 }
1425 else if (ch == '\\') {
1426 *d++ = '\\';
1427 *d++ = '\\';
1428 }
1429 else if (isPRINT_LC(ch))
a0d0e21e
LW
1430 *d++ = ch;
1431 else {
1432 *d++ = '^';
bbce6d69 1433 *d++ = toCTRL(ch);
a0d0e21e
LW
1434 }
1435 }
1436 if (*s) {
1437 *d++ = '.';
1438 *d++ = '.';
1439 *d++ = '.';
1440 }
1441 *d = '\0';
1442
533c011a 1443 if (PL_op)
42d38218
MS
1444 Perl_warner(aTHX_ WARN_NUMERIC,
1445 "Argument \"%s\" isn't numeric in %s", tmpbuf,
1446 PL_op_desc[PL_op->op_type]);
a0d0e21e 1447 else
42d38218
MS
1448 Perl_warner(aTHX_ WARN_NUMERIC,
1449 "Argument \"%s\" isn't numeric", tmpbuf);
a0d0e21e
LW
1450}
1451
f7bbb42a
JH
1452/* the number can be converted to integer with atol() or atoll() */
1453#define IS_NUMBER_TO_INT_BY_ATOL 0x01
1454#define IS_NUMBER_TO_INT_BY_ATOF 0x02 /* atol() may be != atof() */
1455#define IS_NUMBER_NOT_IV 0x04 /* (IV)atof() may be != atof() */
1456#define IS_NUMBER_NEG 0x08 /* not good to cache UV */
1457#define IS_NUMBER_INFINITY 0x10 /* this is big */
25da4f38
IZ
1458
1459/* Actually, ISO C leaves conversion of UV to IV undefined, but
1460 until proven guilty, assume that things are not that bad... */
1461
a0d0e21e 1462IV
864dbfa3 1463Perl_sv_2iv(pTHX_ register SV *sv)
79072805
LW
1464{
1465 if (!sv)
1466 return 0;
8990e307 1467 if (SvGMAGICAL(sv)) {
463ee0b2
LW
1468 mg_get(sv);
1469 if (SvIOKp(sv))
1470 return SvIVX(sv);
748a9306 1471 if (SvNOKp(sv)) {
25da4f38 1472 return I_V(SvNVX(sv));
748a9306 1473 }
36477c24
PP
1474 if (SvPOKp(sv) && SvLEN(sv))
1475 return asIV(sv);
3fe9a6f1 1476 if (!SvROK(sv)) {
d008e5eb 1477 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
d008e5eb 1478 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
1d7c1841 1479 report_uninit();
c6ee37c5 1480 }
36477c24 1481 return 0;
3fe9a6f1 1482 }
463ee0b2 1483 }
ed6116ce 1484 if (SvTHINKFIRST(sv)) {
a0d0e21e 1485 if (SvROK(sv)) {
a0d0e21e 1486 SV* tmpstr;
1554e226
DC
1487 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
1488 (SvRV(tmpstr) != SvRV(sv)))
9e7bc3e8 1489 return SvIV(tmpstr);
56431972 1490 return PTR2IV(SvRV(sv));
a0d0e21e 1491 }
47deb5e7
NIS
1492 if (SvREADONLY(sv) && SvFAKE(sv)) {
1493 sv_force_normal(sv);
1494 }
0336b60e 1495 if (SvREADONLY(sv) && !SvOK(sv)) {
0336b60e 1496 if (ckWARN(WARN_UNINITIALIZED))
1d7c1841 1497 report_uninit();
ed6116ce
LW
1498 return 0;
1499 }
79072805 1500 }
25da4f38
IZ
1501 if (SvIOKp(sv)) {
1502 if (SvIsUV(sv)) {
1503 return (IV)(SvUVX(sv));
1504 }
1505 else {
1506 return SvIVX(sv);
1507 }
463ee0b2 1508 }
748a9306 1509 if (SvNOKp(sv)) {
f7bbb42a
JH
1510 /* We can cache the IV/UV value even if it not good enough
1511 * to reconstruct NV, since the conversion to PV will prefer
1512 * NV over IV/UV.
1513 */
25da4f38
IZ
1514
1515 if (SvTYPE(sv) == SVt_NV)
1516 sv_upgrade(sv, SVt_PVNV);
1517
f7bbb42a
JH
1518 (void)SvIOK_on(sv);
1519 if (SvNVX(sv) < (NV)IV_MAX + 0.5)
748a9306 1520 SvIVX(sv) = I_V(SvNVX(sv));
25da4f38 1521 else {
ff68c719 1522 SvUVX(sv) = U_V(SvNVX(sv));
25da4f38
IZ
1523 SvIsUV_on(sv);
1524 ret_iv_max:
1c846c1f 1525 DEBUG_c(PerlIO_printf(Perl_debug_log,
57def98f 1526 "0x%"UVxf" 2iv(%"UVuf" => %"IVdf") (as unsigned)\n",
56431972 1527 PTR2UV(sv),
57def98f
JH
1528 SvUVX(sv),
1529 SvUVX(sv)));
25da4f38
IZ
1530 return (IV)SvUVX(sv);
1531 }
748a9306
LW
1532 }
1533 else if (SvPOKp(sv) && SvLEN(sv)) {
25da4f38
IZ
1534 I32 numtype = looks_like_number(sv);
1535
1536 /* We want to avoid a possible problem when we cache an IV which
1537 may be later translated to an NV, and the resulting NV is not
1538 the translation of the initial data.
1c846c1f 1539
25da4f38
IZ
1540 This means that if we cache such an IV, we need to cache the
1541 NV as well. Moreover, we trade speed for space, and do not
f7bbb42a 1542 cache the NV if not needed.
25da4f38 1543 */
f7bbb42a
JH
1544 if (numtype & IS_NUMBER_NOT_IV) {
1545 /* May be not an integer. Need to cache NV if we cache IV
1546 * - otherwise future conversion to NV will be wrong. */
1547 NV d;
25da4f38 1548
f7bbb42a 1549 d = Atof(SvPVX(sv));
16b7a9a4 1550
f7bbb42a
JH
1551 if (SvTYPE(sv) < SVt_PVNV)
1552 sv_upgrade(sv, SVt_PVNV);
1553 SvNVX(sv) = d;
1554 (void)SvNOK_on(sv);
1555 (void)SvIOK_on(sv);
65202027 1556#if defined(USE_LONG_DOUBLE)
f7bbb42a
JH
1557 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2nv(%" PERL_PRIgldbl ")\n",
1558 PTR2UV(sv), SvNVX(sv)));
65202027 1559#else
f7bbb42a
JH
1560 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2nv(%g)\n",
1561 PTR2UV(sv), SvNVX(sv)));
65202027 1562#endif
f7bbb42a
JH
1563 if (SvNVX(sv) < (NV)IV_MAX + 0.5)
1564 SvIVX(sv) = I_V(SvNVX(sv));
1565 else {
1566 SvUVX(sv) = U_V(SvNVX(sv));
1567 SvIsUV_on(sv);
1568 goto ret_iv_max;
25da4f38
IZ
1569 }
1570 }
f7bbb42a
JH
1571 else { /* The NV may be reconstructed from IV - safe to cache IV,
1572 which may be calculated by atol(). */
1573 if (SvTYPE(sv) < SVt_PVIV)
1574 sv_upgrade(sv, SVt_PVIV);
1575 (void)SvIOK_on(sv);
1576 SvIVX(sv) = Atol(SvPVX(sv));
1577 if (! numtype && ckWARN(WARN_NUMERIC))
1578 not_a_number(sv);
1579 }
1580 }
1581 else {
599cee73 1582 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
1d7c1841 1583 report_uninit();
25da4f38
IZ
1584 if (SvTYPE(sv) < SVt_IV)
1585 /* Typically the caller expects that sv_any is not NULL now. */
1586 sv_upgrade(sv, SVt_IV);
a0d0e21e 1587 return 0;
79072805 1588 }
1d7c1841
GS
1589 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"IVdf")\n",
1590 PTR2UV(sv),SvIVX(sv)));
25da4f38 1591 return SvIsUV(sv) ? (IV)SvUVX(sv) : SvIVX(sv);
79072805
LW
1592}
1593
ff68c719 1594UV
864dbfa3 1595Perl_sv_2uv(pTHX_ register SV *sv)
ff68c719
PP
1596{
1597 if (!sv)
1598 return 0;
1599 if (SvGMAGICAL(sv)) {
1600 mg_get(sv);
1601 if (SvIOKp(sv))
1602 return SvUVX(sv);
1603 if (SvNOKp(sv))
1604 return U_V(SvNVX(sv));
36477c24
PP
1605 if (SvPOKp(sv) && SvLEN(sv))
1606 return asUV(sv);
3fe9a6f1 1607 if (!SvROK(sv)) {
d008e5eb 1608 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
d008e5eb 1609 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
1d7c1841 1610 report_uninit();
c6ee37c5 1611 }
36477c24 1612 return 0;
3fe9a6f1 1613 }
ff68c719
PP
1614 }
1615 if (SvTHINKFIRST(sv)) {
1616 if (SvROK(sv)) {
ff68c719 1617 SV* tmpstr;
1554e226
DC
1618 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
1619 (SvRV(tmpstr) != SvRV(sv)))
9e7bc3e8 1620 return SvUV(tmpstr);
56431972 1621 return PTR2UV(SvRV(sv));
ff68c719 1622 }
8a818333
NIS
1623 if (SvREADONLY(sv) && SvFAKE(sv)) {
1624 sv_force_normal(sv);
1625 }
0336b60e 1626 if (SvREADONLY(sv) && !SvOK(sv)) {
0336b60e 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)) {
f7bbb42a
JH
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
1643 * NV over IV/UV.
1644 */
25da4f38
IZ
1645 if (SvTYPE(sv) == SVt_NV)
1646 sv_upgrade(sv, SVt_PVNV);
f7bbb42a
JH
1647 (void)SvIOK_on(sv);
1648 if (SvNVX(sv) >= -0.5) {
16b7a9a4 1649 SvIsUV_on(sv);
f7bbb42a
JH
1650 SvUVX(sv) = U_V(SvNVX(sv));
1651 }
1652 else {
1653 SvIVX(sv) = I_V(SvNVX(sv));
1654 ret_zero:
1c846c1f 1655 DEBUG_c(PerlIO_printf(Perl_debug_log,
f7bbb42a 1656 "0x%"UVxf" 2uv(%"IVdf" => %"IVdf") (as signed)\n",
57def98f 1657 PTR2UV(sv),
f7bbb42a
JH
1658 SvIVX(sv),
1659 (IV)(UV)SvIVX(sv)));
1660 return (UV)SvIVX(sv);
25da4f38 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.
1c846c1f 1669
25da4f38
IZ
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 */
f7bbb42a
JH
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. */
1677 NV d;
25da4f38 1678
f7bbb42a 1679 d = Atof(SvPVX(sv));
16b7a9a4 1680
f7bbb42a
JH
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);
16b7a9a4 1686#if defined(USE_LONG_DOUBLE)
f7bbb42a
JH
1687 DEBUG_c(PerlIO_printf(Perl_debug_log,
1688 "0x%"UVxf" 2nv(%" PERL_PRIgldbl ")\n",
1689 PTR2UV(sv), SvNVX(sv)));
16b7a9a4 1690#else
f7bbb42a
JH
1691 DEBUG_c(PerlIO_printf(Perl_debug_log,
1692 "0x%"UVxf" 2nv(%g)\n",
1693 PTR2UV(sv), SvNVX(sv)));
16b7a9a4 1694#endif
f7bbb42a
JH
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);
16b7a9a4 1701 }
25da4f38 1702 }
f7bbb42a
JH
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);
1709 SvIVX(sv) = (IV)Atol(SvPVX(sv));
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
1719 SvUVX(sv) = Strtoul(SvPVX(sv), Null(char**), 10);
1720#else /* no atou(), but we know the number fits into IV... */
1721 /* The only problem may be if it is negative... */
1722 SvUVX(sv) = (UV)Atol(SvPVX(sv));
1723#endif
1724 }
1725 else { /* Not a number. Cache 0. */
1726 if (SvTYPE(sv) < SVt_PVIV)
1727 sv_upgrade(sv, SVt_PVIV);
1728 (void)SvIOK_on(sv);
1729 (void)SvIsUV_on(sv);
1730 SvUVX(sv) = 0; /* We assume that 0s have the
1731 same bitmap in IV and UV. */
1732 if (ckWARN(WARN_NUMERIC))
1733 not_a_number(sv);
1734 }
ff68c719
PP
1735 }
1736 else {
d008e5eb 1737 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
d008e5eb 1738 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
1d7c1841 1739 report_uninit();
c6ee37c5 1740 }
25da4f38
IZ
1741 if (SvTYPE(sv) < SVt_IV)
1742 /* Typically the caller expects that sv_any is not NULL now. */
1743 sv_upgrade(sv, SVt_IV);
ff68c719
PP
1744 return 0;
1745 }
25da4f38 1746
1d7c1841
GS
1747 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"UVuf")\n",
1748 PTR2UV(sv),SvUVX(sv)));
25da4f38 1749 return SvIsUV(sv) ? SvUVX(sv) : (UV)SvIVX(sv);
ff68c719
PP
1750}
1751
65202027 1752NV
864dbfa3 1753Perl_sv_2nv(pTHX_ register SV *sv)
79072805
LW
1754{
1755 if (!sv)
1756 return 0.0;
8990e307 1757 if (SvGMAGICAL(sv)) {
463ee0b2
LW
1758 mg_get(sv);
1759 if (SvNOKp(sv))
1760 return SvNVX(sv);
a0d0e21e 1761 if (SvPOKp(sv) && SvLEN(sv)) {
599cee73 1762 if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) && !looks_like_number(sv))
a0d0e21e 1763 not_a_number(sv);
097ee67d 1764 return Atof(SvPVX(sv));
a0d0e21e 1765 }
25da4f38 1766 if (SvIOKp(sv)) {
1c846c1f 1767 if (SvIsUV(sv))
65202027 1768 return (NV)SvUVX(sv);
25da4f38 1769 else
65202027 1770 return (NV)SvIVX(sv);
25da4f38 1771 }
16d20bd9 1772 if (!SvROK(sv)) {
d008e5eb 1773 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
d008e5eb 1774 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
1d7c1841 1775 report_uninit();
c6ee37c5 1776 }
16d20bd9
AD
1777 return 0;
1778 }
463ee0b2 1779 }
ed6116ce 1780 if (SvTHINKFIRST(sv)) {
a0d0e21e 1781 if (SvROK(sv)) {
a0d0e21e 1782 SV* tmpstr;
1554e226
DC
1783 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
1784 (SvRV(tmpstr) != SvRV(sv)))
9e7bc3e8 1785 return SvNV(tmpstr);
56431972 1786 return PTR2NV(SvRV(sv));
a0d0e21e 1787 }
8a818333
NIS
1788 if (SvREADONLY(sv) && SvFAKE(sv)) {
1789 sv_force_normal(sv);
1790 }
0336b60e 1791 if (SvREADONLY(sv) && !SvOK(sv)) {
599cee73 1792 if (ckWARN(WARN_UNINITIALIZED))
1d7c1841 1793 report_uninit();
ed6116ce
LW
1794 return 0.0;
1795 }
79072805
LW
1796 }
1797 if (SvTYPE(sv) < SVt_NV) {
463ee0b2
LW
1798 if (SvTYPE(sv) == SVt_IV)
1799 sv_upgrade(sv, SVt_PVNV);
1800 else
1801 sv_upgrade(sv, SVt_NV);
572bbb43 1802#if defined(USE_LONG_DOUBLE)
097ee67d 1803 DEBUG_c({
f93f4e46 1804 STORE_NUMERIC_LOCAL_SET_STANDARD();
1d7c1841
GS
1805 PerlIO_printf(Perl_debug_log,
1806 "0x%"UVxf" num(%" PERL_PRIgldbl ")\n",
1807 PTR2UV(sv), SvNVX(sv));
572bbb43
GS
1808 RESTORE_NUMERIC_LOCAL();
1809 });
65202027 1810#else
572bbb43 1811 DEBUG_c({
f93f4e46 1812 STORE_NUMERIC_LOCAL_SET_STANDARD();
1d7c1841
GS
1813 PerlIO_printf(Perl_debug_log, "0x%"UVxf" num(%g)\n",
1814 PTR2UV(sv), SvNVX(sv));
097ee67d
JH
1815 RESTORE_NUMERIC_LOCAL();
1816 });
572bbb43 1817#endif
79072805
LW
1818 }
1819 else if (SvTYPE(sv) < SVt_PVNV)
1820 sv_upgrade(sv, SVt_PVNV);
748a9306
LW
1821 if (SvIOKp(sv) &&
1822 (!SvPOKp(sv) || !strchr(SvPVX(sv),'.') || !looks_like_number(sv)))
93a17b20 1823 {
65202027 1824 SvNVX(sv) = SvIsUV(sv) ? (NV)SvUVX(sv) : (NV)SvIVX(sv);
93a17b20 1825 }
748a9306 1826 else if (SvPOKp(sv) && SvLEN(sv)) {
599cee73 1827 if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) && !looks_like_number(sv))
a0d0e21e 1828 not_a_number(sv);
097ee67d 1829 SvNVX(sv) = Atof(SvPVX(sv));
93a17b20 1830 }
79072805 1831 else {
599cee73 1832 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
1d7c1841 1833 report_uninit();
25da4f38
IZ
1834 if (SvTYPE(sv) < SVt_NV)
1835 /* Typically the caller expects that sv_any is not NULL now. */
1836 sv_upgrade(sv, SVt_NV);
a0d0e21e 1837 return 0.0;
79072805 1838 }
f7bbb42a 1839 SvNOK_on(sv);
572bbb43 1840#if defined(USE_LONG_DOUBLE)
097ee67d 1841 DEBUG_c({
f93f4e46 1842 STORE_NUMERIC_LOCAL_SET_STANDARD();
1d7c1841
GS
1843 PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2nv(%" PERL_PRIgldbl ")\n",
1844 PTR2UV(sv), SvNVX(sv));
572bbb43
GS
1845 RESTORE_NUMERIC_LOCAL();
1846 });
65202027 1847#else
572bbb43 1848 DEBUG_c({
f93f4e46 1849 STORE_NUMERIC_LOCAL_SET_STANDARD();
1d7c1841
GS
1850 PerlIO_printf(Perl_debug_log, "0x%"UVxf" 1nv(%g)\n",
1851 PTR2UV(sv), SvNVX(sv));
097ee67d
JH
1852 RESTORE_NUMERIC_LOCAL();
1853 });
572bbb43 1854#endif
463ee0b2 1855 return SvNVX(sv);
79072805
LW
1856}
1857
76e3520e 1858STATIC IV
cea2e8a9 1859S_asIV(pTHX_ SV *sv)
36477c24
PP
1860{
1861 I32 numtype = looks_like_number(sv);
65202027 1862 NV d;
36477c24 1863
25da4f38 1864 if (numtype & IS_NUMBER_TO_INT_BY_ATOL)
cf2093f6 1865 return Atol(SvPVX(sv));
d008e5eb 1866 if (!numtype) {
d008e5eb
GS
1867 if (ckWARN(WARN_NUMERIC))
1868 not_a_number(sv);
1869 }
097ee67d 1870 d = Atof(SvPVX(sv));
25da4f38 1871 return I_V(d);
36477c24
PP
1872}
1873
76e3520e 1874STATIC UV
cea2e8a9 1875S_asUV(pTHX_ SV *sv)
36477c24
PP
1876{
1877 I32 numtype = looks_like_number(sv);
1878
84902520 1879#ifdef HAS_STRTOUL
25da4f38 1880 if (numtype & IS_NUMBER_TO_INT_BY_ATOL)
cf2093f6 1881 return Strtoul(SvPVX(sv), Null(char**), 10);
84902520 1882#endif
d008e5eb 1883 if (!numtype) {
d008e5eb
GS
1884 if (ckWARN(WARN_NUMERIC))
1885 not_a_number(sv);
1886 }
097ee67d 1887 return U_V(Atof(SvPVX(sv)));
36477c24
PP
1888}
1889
25da4f38
IZ
1890/*
1891 * Returns a combination of (advisory only - can get false negatives)
f7bbb42a
JH
1892 * IS_NUMBER_TO_INT_BY_ATOL, IS_NUMBER_TO_INT_BY_ATOF, IS_NUMBER_NOT_IV,
1893 * IS_NUMBER_NEG
25da4f38
IZ
1894 * 0 if does not look like number.
1895 *
f7bbb42a
JH
1896 * In fact possible values are 0 and
1897 * IS_NUMBER_TO_INT_BY_ATOL 123
1898 * IS_NUMBER_TO_INT_BY_ATOL | IS_NUMBER_NOT_IV 123.1
1899 * IS_NUMBER_TO_INT_BY_ATOF | IS_NUMBER_NOT_IV 123e0
300aed98 1900 * IS_NUMBER_INFINITY
f7bbb42a 1901 * with a possible addition of IS_NUMBER_NEG.
25da4f38
IZ
1902 */
1903
954c1994
GS
1904/*
1905=for apidoc looks_like_number
1906
1907Test if an the content of an SV looks like a number (or is a
f7bbb42a 1908number).
954c1994
GS
1909
1910=cut
1911*/
1912
36477c24 1913I32
864dbfa3 1914Perl_looks_like_number(pTHX_ SV *sv)
36477c24
PP
1915{
1916 register char *s;
1917 register char *send;
1918 register char *sbegin;
25da4f38
IZ
1919 register char *nbegin;
1920 I32 numtype = 0;
300aed98 1921 I32 sawinf = 0;
36477c24
PP
1922 STRLEN len;
1923
1924 if (SvPOK(sv)) {
1c846c1f 1925 sbegin = SvPVX(sv);
36477c24
PP
1926 len = SvCUR(sv);
1927 }
1928 else if (SvPOKp(sv))
1929 sbegin = SvPV(sv, len);
1930 else
1931 return 1;
1932 send = sbegin + len;
1933
1934 s = sbegin;
1935 while (isSPACE(*s))
1936 s++;
25da4f38
IZ
1937 if (*s == '-') {
1938 s++;
1939 numtype = IS_NUMBER_NEG;
1940 }
1941 else if (*s == '+')
36477c24 1942 s++;
ff0cee69 1943
25da4f38
IZ
1944 nbegin = s;
1945 /*
f7bbb42a
JH
1946 * we return IS_NUMBER_TO_INT_BY_ATOL if the number can be converted
1947 * to _integer_ with atol() and IS_NUMBER_TO_INT_BY_ATOF if you need
1948 * (int)atof().
25da4f38
IZ
1949 */
1950
300aed98 1951 /* next must be digit or the radix separator or beginning of infinity */
ff0cee69
PP
1952 if (isDIGIT(*s)) {
1953 do {
1954 s++;
1955 } while (isDIGIT(*s));
25da4f38 1956
f7bbb42a
JH
1957 if (s - nbegin >= TYPE_DIGITS(IV)) /* Cannot cache ato[ul]() */
1958 numtype |= IS_NUMBER_TO_INT_BY_ATOF | IS_NUMBER_NOT_IV;
16b7a9a4 1959 else
f7bbb42a 1960 numtype |= IS_NUMBER_TO_INT_BY_ATOL;
25da4f38 1961
097ee67d 1962 if (*s == '.'
1c846c1f 1963#ifdef USE_LOCALE_NUMERIC
097ee67d
JH
1964 || IS_NUMERIC_RADIX(*s)
1965#endif
1966 ) {
ff0cee69 1967 s++;
f7bbb42a 1968 numtype |= IS_NUMBER_NOT_IV;
097ee67d 1969 while (isDIGIT(*s)) /* optional digits after the radix */
ff0cee69
PP
1970 s++;
1971 }
36477c24 1972 }
097ee67d 1973 else if (*s == '.'
1c846c1f 1974#ifdef USE_LOCALE_NUMERIC
097ee67d
JH
1975 || IS_NUMERIC_RADIX(*s)
1976#endif
1977 ) {
ff0cee69 1978 s++;
f7bbb42a 1979 numtype |= IS_NUMBER_TO_INT_BY_ATOL | IS_NUMBER_NOT_IV;
097ee67d 1980 /* no digits before the radix means we need digits after it */
ff0cee69
PP
1981 if (isDIGIT(*s)) {
1982 do {
1983 s++;
1984 } while (isDIGIT(*s));
1985 }
1986 else
1987 return 0;
1988 }
300aed98
JH
1989 else if (*s == 'I' || *s == 'i') {
1990 s++; if (*s != 'N' && *s != 'n') return 0;
1991 s++; if (*s != 'F' && *s != 'f') return 0;
1992 s++; if (*s == 'I' || *s == 'i') {
1993 s++; if (*s != 'N' && *s != 'n') return 0;
1994 s++; if (*s != 'I' && *s != 'i') return 0;
1995 s++; if (*s != 'T' && *s != 't') return 0;
1996 s++; if (*s != 'Y' && *s != 'y') return 0;
99938567 1997 s++;
300aed98
JH
1998 }
1999 sawinf = 1;
2000 }
ff0cee69
PP
2001 else
2002 return 0;
2003
300aed98 2004 if (sawinf)
f7bbb42a 2005 numtype = IS_NUMBER_INFINITY;
300aed98
JH
2006 else {
2007 /* we can have an optional exponent part */
2008 if (*s == 'e' || *s == 'E') {
f7bbb42a
JH
2009 numtype &= ~IS_NUMBER_NEG;
2010 numtype |= IS_NUMBER_TO_INT_BY_ATOF | IS_NUMBER_NOT_IV;
36477c24 2011 s++;
300aed98
JH
2012 if (*s == '+' || *s == '-')
2013 s++;
2014 if (isDIGIT(*s)) {
2015 do {
2016 s++;
2017 } while (isDIGIT(*s));
2018 }
2019 else
2020 return 0;
2021 }
36477c24
PP
2022 }
2023 while (isSPACE(*s))
2024 s++;
80f3f388 2025 if (s >= send)
36477c24
PP
2026 return numtype;
2027 if (len == 10 && memEQ(sbegin, "0 but true", 10))
25da4f38 2028 return IS_NUMBER_TO_INT_BY_ATOL;
36477c24
PP
2029 return 0;
2030}
2031
79072805 2032char *
864dbfa3 2033Perl_sv_2pv_nolen(pTHX_ register SV *sv)
1fa8b10d
JD
2034{
2035 STRLEN n_a;
2036 return sv_2pv(sv, &n_a);
2037}
2038
25da4f38 2039/* We assume that buf is at least TYPE_CHARS(UV) long. */
864dbfa3 2040static char *
25da4f38
IZ
2041uiv_2buf(char *buf, IV iv, UV uv, int is_uv, char **peob)
2042{
25da4f38
IZ
2043 char *ptr = buf + TYPE_CHARS(UV);
2044 char *ebuf = ptr;
2045 int sign;
25da4f38
IZ
2046
2047 if (is_uv)
2048 sign = 0;
2049 else if (iv >= 0) {
2050 uv = iv;
2051 sign = 0;
2052 } else {
2053 uv = -iv;
2054 sign = 1;
2055 }
2056 do {
2057 *--ptr = '0' + (uv % 10);
2058 } while (uv /= 10);
2059 if (sign)
2060 *--ptr = '-';
2061 *peob = ebuf;
2062 return ptr;
2063}
2064
1fa8b10d 2065char *
864dbfa3 2066Perl_sv_2pv(pTHX_ register SV *sv, STRLEN *lp)
79072805
LW
2067{
2068 register char *s;
2069 int olderrno;
46fc3d4c 2070 SV *tsv;
25da4f38
IZ
2071 char tbuf[64]; /* Must fit sprintf/Gconvert of longest IV/NV */
2072 char *tmpbuf = tbuf;
79072805 2073
463ee0b2
LW
2074 if (!sv) {
2075 *lp = 0;
2076 return "";
2077 }
8990e307 2078 if (SvGMAGICAL(sv)) {
463ee0b2
LW
2079 mg_get(sv);
2080 if (SvPOKp(sv)) {
2081 *lp = SvCUR(sv);
2082 return SvPVX(sv);
2083 }
cf2093f6 2084 if (SvIOKp(sv)) {
1c846c1f 2085 if (SvIsUV(sv))
57def98f 2086 (void)sprintf(tmpbuf,"%"UVuf, (UV)SvUVX(sv));
cf2093f6 2087 else
57def98f 2088 (void)sprintf(tmpbuf,"%"IVdf, (IV)SvIVX(sv));
46fc3d4c 2089 tsv = Nullsv;
a0d0e21e 2090 goto tokensave;
463ee0b2
LW
2091 }
2092 if (SvNOKp(sv)) {
2d4389e4 2093 Gconvert(SvNVX(sv), NV_DIG, 0, tmpbuf);
46fc3d4c 2094 tsv = Nullsv;
a0d0e21e 2095 goto tokensave;
463ee0b2 2096 }
16d20bd9 2097 if (!SvROK(sv)) {
d008e5eb 2098 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
d008e5eb 2099 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
1d7c1841 2100 report_uninit();
c6ee37c5 2101 }
16d20bd9
AD
2102 *lp = 0;
2103 return "";
2104 }
463ee0b2 2105 }
ed6116ce
LW
2106 if (SvTHINKFIRST(sv)) {
2107 if (SvROK(sv)) {
a0d0e21e 2108 SV* tmpstr;
1554e226
DC
2109 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,string)) &&
2110 (SvRV(tmpstr) != SvRV(sv)))
9e7bc3e8 2111 return SvPV(tmpstr,*lp);
ed6116ce
LW
2112 sv = (SV*)SvRV(sv);
2113 if (!sv)
2114 s = "NULLREF";
2115 else {
f9277f47
IZ
2116 MAGIC *mg;
2117
ed6116ce 2118 switch (SvTYPE(sv)) {
f9277f47
IZ
2119 case SVt_PVMG:
2120 if ( ((SvFLAGS(sv) &
1c846c1f 2121 (SVs_OBJECT|SVf_OK|SVs_GMG|SVs_SMG|SVs_RMG))
3149a8e4 2122 == (SVs_OBJECT|SVs_RMG))
57668c4d 2123 && strEQ(s=HvNAME(SvSTASH(sv)), "Regexp")
f9277f47 2124 && (mg = mg_find(sv, 'r'))) {
2cd61cdb 2125 regexp *re = (regexp *)mg->mg_obj;
1bd3ad17 2126
2cd61cdb 2127 if (!mg->mg_ptr) {
8782bef2
GB
2128 char *fptr = "msix";
2129 char reflags[6];
2130 char ch;
2131 int left = 0;
2132 int right = 4;
2133 U16 reganch = (re->reganch & PMf_COMPILETIME) >> 12;
2134
155aba94 2135 while((ch = *fptr++)) {
8782bef2
GB
2136 if(reganch & 1) {
2137 reflags[left++] = ch;
2138 }
2139 else {
2140 reflags[right--] = ch;
2141 }
2142 reganch >>= 1;
2143 }
2144 if(left != 4) {
2145 reflags[left] = '-';
2146 left = 5;
2147 }
2148
2149 mg->mg_len = re->prelen + 4 + left;
2150 New(616, mg->mg_ptr, mg->mg_len + 1 + left, char);
2151 Copy("(?", mg->mg_ptr, 2, char);
2152 Copy(reflags, mg->mg_ptr+2, left, char);
2153 Copy(":", mg->mg_ptr+left+2, 1, char);
2154 Copy(re->precomp, mg->mg_ptr+3+left, re->prelen, char);
1bd3ad17
IZ
2155 mg->mg_ptr[mg->mg_len - 1] = ')';
2156 mg->mg_ptr[mg->mg_len] = 0;
2157 }
3280af22 2158 PL_reginterp_cnt += re->program[0].next_off;
1bd3ad17
IZ
2159 *lp = mg->mg_len;
2160 return mg->mg_ptr;
f9277f47
IZ
2161 }
2162 /* Fall through */
ed6116ce
LW
2163 case SVt_NULL:
2164 case SVt_IV:
2165 case SVt_NV:
2166 case SVt_RV:
2167 case SVt_PV:
2168 case SVt_PVIV:
2169 case SVt_PVNV:
81689caa
HS
2170 case SVt_PVBM: if (SvROK(sv))
2171 s = "REF";
2172 else
2173 s = "SCALAR"; break;
ed6116ce
LW
2174 case SVt_PVLV: s = "LVALUE"; break;
2175 case SVt_PVAV: s = "ARRAY"; break;
2176 case SVt_PVHV: s = "HASH"; break;
2177 case SVt_PVCV: s = "CODE"; break;
2178 case SVt_PVGV: s = "GLOB"; break;
1d2dff63 2179 case SVt_PVFM: s = "FORMAT"; break;
36477c24 2180 case SVt_PVIO: s = "IO"; break;
ed6116ce
LW
2181 default: s = "UNKNOWN"; break;
2182 }
46fc3d4c 2183 tsv = NEWSV(0,0);
ed6116ce 2184 if (SvOBJECT(sv))
cea2e8a9 2185 Perl_sv_setpvf(aTHX_ tsv, "%s=%s", HvNAME(SvSTASH(sv)), s);
ed6116ce 2186 else
46fc3d4c 2187 sv_setpv(tsv, s);
57def98f 2188 Perl_sv_catpvf(aTHX_ tsv, "(0x%"UVxf")", PTR2UV(sv));
a0d0e21e 2189 goto tokensaveref;
463ee0b2 2190 }
ed6116ce
LW
2191 *lp = strlen(s);
2192 return s;
79072805 2193 }
0336b60e 2194 if (SvREADONLY(sv) && !SvOK(sv)) {
0336b60e 2195 if (ckWARN(WARN_UNINITIALIZED))
1d7c1841 2196 report_uninit();
ed6116ce
LW
2197 *lp = 0;
2198 return "";
79072805 2199 }
79072805 2200 }
f7bbb42a
JH
2201 if (SvPOK(sv)) {
2202 *lp = SvCUR(sv);
2203 return SvPVX(sv);
2204 }
2205 else if (SvNOKp(sv)) { /* See note in sv_2uv() */
2206 /* XXXX 64-bit? IV may have better precision... */
2207 /* I tried changing this to be 64-bit-aware and
2208 * the t/op/numconvert.t became very, very, angry.
2209 * --jhi Sep 1999 */
79072805
LW
2210 if (SvTYPE(sv) < SVt_PVNV)
2211 sv_upgrade(sv, SVt_PVNV);
1c846c1f 2212 /* The +20 is pure guesswork. Configure test needed. --jhi */
59155cc0 2213 SvGROW(sv, NV_DIG + 20);
463ee0b2 2214 s = SvPVX(sv);
79072805 2215 olderrno = errno; /* some Xenix systems wipe out errno here */
79072805 2216#ifdef apollo
463ee0b2 2217 if (SvNVX(sv) == 0.0)
79072805
LW
2218 (void)strcpy(s,"0");
2219 else
2220#endif /*apollo*/
bbce6d69 2221 {
2d4389e4 2222 Gconvert(SvNVX(sv), NV_DIG, 0, s);
bbce6d69 2223 }
79072805 2224 errno = olderrno;
a0d0e21e
LW
2225#ifdef FIXNEGATIVEZERO
2226 if (*s == '-' && s[1] == '0' && !s[2])
2227 strcpy(s,"0");
2228#endif
79072805
LW
2229 while (*s) s++;
2230#ifdef hcx
2231 if (s[-1] == '.')
46fc3d4c 2232 *--s = '\0';
79072805
LW
2233#endif
2234 }
f7bbb42a
JH
2235 else if (SvIOKp(sv)) {
2236 U32 isIOK = SvIOK(sv);
2237 U32 isUIOK = SvIsUV(sv);
2238 char buf[TYPE_CHARS(UV)];
2239 char *ebuf, *ptr;
2240
2241 if (SvTYPE(sv) < SVt_PVIV)
2242 sv_upgrade(sv, SVt_PVIV);
2243 if (isUIOK)
2244 ptr = uiv_2buf(buf, 0, SvUVX(sv), 1, &ebuf);
2245 else
2246 ptr = uiv_2buf(buf, SvIVX(sv), 0, 0, &ebuf);
2247 SvGROW(sv, ebuf - ptr + 1); /* inlined from sv_setpvn */
2248 Move(ptr,SvPVX(sv),ebuf - ptr,char);
2249 SvCUR_set(sv, ebuf - ptr);
2250 s = SvEND(sv);
2251 *s = '\0';
2252 if (isIOK)
2253 SvIOK_on(sv);
2254 else
2255 SvIOKp_on(sv);
2256 if (isUIOK)
2257 SvIsUV_on(sv);
2258 SvPOK_on(sv);
2259 }
79072805 2260 else {
0336b60e
IZ
2261 if (ckWARN(WARN_UNINITIALIZED)
2262 && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
1d7c1841 2263 report_uninit();
a0d0e21e 2264 *lp = 0;
25da4f38
IZ
2265 if (SvTYPE(sv) < SVt_PV)
2266 /* Typically the caller expects that sv_any is not NULL now. */
2267 sv_upgrade(sv, SVt_PV);
a0d0e21e 2268 return "";
79072805 2269 }
463ee0b2
LW
2270 *lp = s - SvPVX(sv);
2271 SvCUR_set(sv, *lp);
79072805 2272 SvPOK_on(sv);
1d7c1841
GS
2273 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n",
2274 PTR2UV(sv),SvPVX(sv)));
463ee0b2 2275 return SvPVX(sv);
a0d0e21e
LW
2276
2277 tokensave:
2278 if (SvROK(sv)) { /* XXX Skip this when sv_pvn_force calls */
2279 /* Sneaky stuff here */
2280
2281 tokensaveref:
46fc3d4c 2282 if (!tsv)
96827780 2283 tsv = newSVpv(tmpbuf, 0);
46fc3d4c
PP
2284 sv_2mortal(tsv);
2285 *lp = SvCUR(tsv);
2286 return SvPVX(tsv);
a0d0e21e
LW
2287 }
2288 else {
2289 STRLEN len;
46fc3d4c
PP
2290 char *t;
2291
2292 if (tsv) {
2293 sv_2mortal(tsv);
2294 t = SvPVX(tsv);
2295 len = SvCUR(tsv);
2296 }
2297 else {
96827780
MB
2298 t = tmpbuf;
2299 len = strlen(tmpbuf);
46fc3d4c 2300 }
a0d0e21e 2301#ifdef FIXNEGATIVEZERO
46fc3d4c
PP
2302 if (len == 2 && t[0] == '-' && t[1] == '0') {
2303 t = "0";
2304 len = 1;
2305 }
a0d0e21e
LW
2306#endif
2307 (void)SvUPGRADE(sv, SVt_PV);
46fc3d4c 2308 *lp = len;
a0d0e21e
LW
2309 s = SvGROW(sv, len + 1);
2310 SvCUR_set(sv, len);
46fc3d4c 2311 (void)strcpy(s, t);
6bf554b4 2312 SvPOKp_on(sv);
a0d0e21e
LW
2313 return s;
2314 }
463ee0b2
LW
2315}
2316
7340a771
GS
2317char *
2318Perl_sv_2pvbyte_nolen(pTHX_ register SV *sv)
2319{
560a288e
GS
2320 STRLEN n_a;
2321 return sv_2pvbyte(sv, &n_a);
7340a771
GS
2322}
2323
2324char *
2325Perl_sv_2pvbyte(pTHX_ register SV *sv, STRLEN *lp)
2326{
2327 return sv_2pv(sv,lp);
2328}
2329
2330char *
2331Perl_sv_2pvutf8_nolen(pTHX_ register SV *sv)
2332{
560a288e
GS
2333 STRLEN n_a;
2334 return sv_2pvutf8(sv, &n_a);
7340a771
GS
2335}
2336
2337char *
2338Perl_sv_2pvutf8(pTHX_ register SV *sv, STRLEN *lp)
2339{
560a288e 2340 sv_utf8_upgrade(sv);
7d59b7e4 2341 return SvPV(sv,*lp);
7340a771 2342}
1c846c1f 2343
463ee0b2
LW
2344/* This function is only called on magical items */
2345bool
864dbfa3 2346Perl_sv_2bool(pTHX_ register SV *sv)
463ee0b2 2347{
8990e307 2348 if (SvGMAGICAL(sv))
463ee0b2
LW
2349 mg_get(sv);
2350
a0d0e21e
LW
2351 if (!SvOK(sv))
2352 return 0;
2353 if (SvROK(sv)) {
a0d0e21e 2354 SV* tmpsv;
1554e226
DC
2355 if (SvAMAGIC(sv) && (tmpsv=AMG_CALLun(sv,bool_)) &&
2356 (SvRV(tmpsv) != SvRV(sv)))
9e7bc3e8 2357 return SvTRUE(tmpsv);
a0d0e21e
LW
2358 return SvRV(sv) != 0;
2359 }
463ee0b2 2360 if (SvPOKp(sv)) {
11343788
MB
2361 register XPV* Xpvtmp;
2362 if ((Xpvtmp = (XPV*)SvANY(sv)) &&
2363 (*Xpvtmp->xpv_pv > '0' ||
2364 Xpvtmp->xpv_cur > 1 ||
2365 (Xpvtmp->xpv_cur && *Xpvtmp->xpv_pv != '0')))
463ee0b2
LW
2366 return 1;
2367 else
2368 return 0;
2369 }
2370 else {
2371 if (SvIOKp(sv))
2372 return SvIVX(sv) != 0;
2373 else {
2374 if (SvNOKp(sv))
2375 return SvNVX(sv) != 0.0;
2376 else
2377 return FALSE;
2378 }
2379 }
79072805
LW
2380}
2381
c461cf8f
JH
2382/*
2383=for apidoc sv_utf8_upgrade
2384
2385Convert the PV of an SV to its UTF8-encoded form.
2386
2387=cut
2388*/
2389
560a288e
GS
2390void
2391Perl_sv_utf8_upgrade(pTHX_ register SV *sv)
2392{
511c2ff0
NIS
2393 char *s, *t, *e;
2394 int hibit = 0;
560a288e
GS
2395
2396 if (!sv || !SvPOK(sv) || SvUTF8(sv))
2397 return;
2398
40826f67
JH
2399 /* This function could be much more efficient if we had a FLAG in SVs
2400 * to signal if there are any hibit chars in the PV.
511c2ff0 2401 * Given that there isn't make loop fast as possible
560a288e 2402 */
511c2ff0
NIS
2403 s = SvPVX(sv);
2404 e = SvEND(sv);
2405 t = s;
2406 while (t < e) {
2407 if ((hibit = *t++ & 0x80))
8a818333 2408 break;
8a818333 2409 }
560a288e 2410
40826f67 2411 if (hibit) {
8a818333
NIS
2412 STRLEN len;
2413 if (SvREADONLY(sv) && SvFAKE(sv)) {
8a818333
NIS
2414 sv_force_normal(sv);
2415 s = SvPVX(sv);
2416 }
2417 len = SvCUR(sv) + 1; /* Plus the \0 */
00df9076 2418 SvPVX(sv) = (char*)bytes_to_utf8((U8*)s, &len);
841d7a39 2419 SvCUR(sv) = len - 1;
511c2ff0
NIS
2420 if (SvLEN(sv) != 0)
2421 Safefree(s); /* No longer using what was there before. */
841d7a39 2422 SvLEN(sv) = len; /* No longer know the real size. */
560a288e
GS
2423 SvUTF8_on(sv);
2424 }
2425}
2426
c461cf8f
JH
2427/*
2428=for apidoc sv_utf8_downgrade
2429
2430Attempt to convert the PV of an SV from UTF8-encoded to byte encoding.
2431This may not be possible if the PV contains non-byte encoding characters;
2432if this is the case, either returns false or, if C<fail_ok> is not
2433true, croaks.
2434
2435=cut
2436*/
2437
560a288e
GS
2438bool
2439Perl_sv_utf8_downgrade(pTHX_ register SV* sv, bool fail_ok)
2440{
2441 if (SvPOK(sv) && SvUTF8(sv)) {
fa301091
JH
2442 if (SvCUR(sv)) {
2443 char *c = SvPVX(sv);
2444 STRLEN len = SvCUR(sv);
2445
2446 if (!utf8_to_bytes((U8*)c, &len)) {
2447 if (fail_ok)
2448 return FALSE;
2449 else {
2450 if (PL_op)
2451 Perl_croak(aTHX_ "Wide character in %s",
2452 PL_op_desc[PL_op->op_type]);
2453 else
2454 Perl_croak(aTHX_ "Wide character");
2455 }
4b3603a4 2456 }
fa301091 2457 SvCUR(sv) = len;
67e989fb 2458 }
9f9ab905 2459 SvUTF8_off(sv);
560a288e 2460 }
fa301091 2461
560a288e
GS
2462 return TRUE;
2463}
2464
c461cf8f
JH
2465/*
2466=for apidoc sv_utf8_encode
2467
2468Convert the PV of an SV to UTF8-encoded, but then turn off the C<SvUTF8>
1c846c1f 2469flag so that it looks like bytes again. Nothing calls this.
c461cf8f
JH
2470
2471=cut
2472*/
2473
560a288e
GS
2474void
2475Perl_sv_utf8_encode(pTHX_ register SV *sv)
2476{
2477 sv_utf8_upgrade(sv);
2478 SvUTF8_off(sv);
2479}
2480
2481bool
2482Perl_sv_utf8_decode(pTHX_ register SV *sv)
2483{
2484 if (SvPOK(sv)) {
2485 char *c;
511c2ff0 2486 char *e;
560a288e
GS
2487 bool has_utf = FALSE;
2488 if (!sv_utf8_downgrade(sv, TRUE))
2489 return FALSE;
2490
2491 /* it is actually just a matter of turning the utf8 flag on, but
2492 * we want to make sure everything inside is valid utf8 first.
2493 */
2494 c = SvPVX(sv);
00df9076 2495 if (!is_utf8_string((U8*)c, SvCUR(sv)+1))
67e989fb 2496 return FALSE;
511c2ff0
NIS
2497 e = SvEND(sv);
2498 while (c < e) {
67e989fb
JH
2499 if (*c++ & 0x80) {
2500 SvUTF8_on(sv);
2501 break;
2502 }
560a288e 2503 }
560a288e
GS
2504 }
2505 return TRUE;
2506}
2507
2508
79072805 2509/* Note: sv_setsv() should not be called with a source string that needs
463ee0b2 2510 * to be reused, since it may destroy the source string if it is marked
79072805
LW
2511 * as temporary.
2512 */
2513
954c1994
GS
2514/*
2515=for apidoc sv_setsv
2516
2517Copies the contents of the source SV C<ssv> into the destination SV C<dsv>.
2518The source SV may be destroyed if it is mortal. Does not handle 'set'
2519magic. See the macro forms C<SvSetSV>, C<SvSetSV_nosteal> and
2520C<sv_setsv_mg>.
2521
2522=cut
2523*/
2524
79072805 2525void
864dbfa3 2526Perl_sv_setsv(pTHX_ SV *dstr, register SV *sstr)
79072805 2527{
8990e307
LW
2528 register U32 sflags;
2529 register int dtype;
2530 register int stype;
463ee0b2 2531
79072805
LW
2532 if (sstr == dstr)
2533 return;
2213622d 2534 SV_CHECK_THINKFIRST(dstr);
79072805 2535 if (!sstr)
3280af22 2536 sstr = &PL_sv_undef;
8990e307
LW
2537 stype = SvTYPE(sstr);
2538 dtype = SvTYPE(dstr);
79072805 2539
a0d0e21e 2540 SvAMAGIC_off(dstr);
9e7bc3e8 2541
463ee0b2 2542 /* There's a lot of redundancy below but we're going for speed here */
79072805 2543
8990e307 2544 switch (stype) {
79072805 2545 case SVt_NULL:
aece5585 2546 undef_sstr:
20408e3c
GS
2547 if (dtype != SVt_PVGV) {
2548 (void)SvOK_off(dstr);
2549 return;
2550 }
2551 break;
463ee0b2 2552 case SVt_IV:
aece5585
GA
2553 if (SvIOK(sstr)) {
2554 switch (dtype) {
2555 case SVt_NULL:
8990e307 2556 sv_upgrade(dstr, SVt_IV);
aece5585
GA
2557 break;
2558 case SVt_NV:
8990e307 2559 sv_upgrade(dstr, SVt_PVNV);
aece5585
GA
2560 break;
2561 case SVt_RV:
2562 case SVt_PV:
a0d0e21e 2563 sv_upgrade(dstr, SVt_PVIV);
aece5585
GA
2564 break;
2565 }
2566 (void)SvIOK_only(dstr);
2567 SvIVX(dstr) = SvIVX(sstr);
25da4f38
IZ
2568 if (SvIsUV(sstr))
2569 SvIsUV_on(dstr);
27c9684d
AP
2570 if (SvTAINTED(sstr))
2571 SvTAINT(dstr);
aece5585 2572 return;
8990e307 2573 }
aece5585
GA
2574 goto undef_sstr;
2575
463ee0b2 2576 case SVt_NV:
aece5585
GA
2577 if (SvNOK(sstr)) {
2578 switch (dtype) {
2579 case SVt_NULL:
2580 case SVt_IV:
8990e307 2581 sv_upgrade(dstr, SVt_NV);
aece5585
GA
2582 break;
2583 case SVt_RV:
2584 case SVt_PV:
2585 case SVt_PVIV:
a0d0e21e 2586 sv_upgrade(dstr, SVt_PVNV);
aece5585
GA
2587 break;
2588 }
2589 SvNVX(dstr) = SvNVX(sstr);
2590 (void)SvNOK_only(dstr);
27c9684d
AP
2591 if (SvTAINTED(sstr))
2592 SvTAINT(dstr);
aece5585 2593 return;
8990e307 2594 }
aece5585
GA
2595 goto undef_sstr;
2596
ed6116ce 2597 case SVt_RV:
8990e307 2598 if (dtype < SVt_RV)
ed6116ce 2599 sv_upgrade(dstr, SVt_RV);
c07a80fd
PP
2600 else if (dtype == SVt_PVGV &&
2601 SvTYPE(SvRV(sstr)) == SVt_PVGV) {
2602 sstr = SvRV(sstr);
a5f75d66 2603 if (sstr == dstr) {
1d7c1841
GS
2604 if (GvIMPORTED(dstr) != GVf_IMPORTED
2605 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
2606 {
a5f75d66 2607 GvIMPORTED_on(dstr);
1d7c1841 2608 }
a5f75d66
AD
2609 GvMULTI_on(dstr);
2610 return;
2611 }
c07a80fd
PP
2612 goto glob_assign;
2613 }
ed6116ce 2614 break;
463ee0b2 2615 case SVt_PV:
fc36a67e 2616 case SVt_PVFM:
8990e307 2617 if (dtype < SVt_PV)
463ee0b2 2618 sv_upgrade(dstr, SVt_PV);
463ee0b2
LW
2619 break;
2620 case SVt_PVIV:
8990e307 2621 if (dtype < SVt_PVIV)
463ee0b2 2622 sv_upgrade(dstr, SVt_PVIV);
463ee0b2
LW
2623 break;
2624 case SVt_PVNV:
8990e307 2625 if (dtype < SVt_PVNV)
463ee0b2 2626 sv_upgrade(dstr, SVt_PVNV);
463ee0b2 2627 break;
4633a7c4
LW
2628 case SVt_PVAV:
2629 case SVt_PVHV:
2630 case SVt_PVCV:
4633a7c4 2631 case SVt_PVIO:
533c011a 2632 if (PL_op)
cea2e8a9 2633 Perl_croak(aTHX_ "Bizarre copy of %s in %s", sv_reftype(sstr, 0),
22c35a8c 2634 PL_op_name[PL_op->op_type]);
4633a7c4 2635 else
cea2e8a9 2636 Perl_croak(aTHX_ "Bizarre copy of %s", sv_reftype(sstr, 0));
4633a7c4
LW
2637 break;
2638
79072805 2639 case SVt_PVGV:
8990e307 2640 if (dtype <= SVt_PVGV) {
c07a80fd 2641 glob_assign:
a5f75d66 2642 if (dtype != SVt_PVGV) {
a0d0e21e
LW
2643 char *name = GvNAME(sstr);
2644 STRLEN len = GvNAMELEN(sstr);
463ee0b2 2645 sv_upgrade(dstr, SVt_PVGV);
6662521e 2646 sv_magic(dstr, dstr, '*', Nullch, 0);
85aff577 2647 GvSTASH(dstr) = (HV*)SvREFCNT_inc(GvSTASH(sstr));
a0d0e21e
LW
2648 GvNAME(dstr) = savepvn(name, len);
2649 GvNAMELEN(dstr) = len;
2650 SvFAKE_on(dstr); /* can coerce to non-glob */
2651 }
7bac28a0 2652 /* ahem, death to those who redefine active sort subs */
3280af22
NIS
2653 else if (PL_curstackinfo->si_type == PERLSI_SORT
2654 && GvCV(dstr) && PL_sortcop == CvSTART(GvCV(dstr)))
cea2e8a9 2655 Perl_croak(aTHX_ "Can't redefine active sort subroutine %s",
7bac28a0 2656 GvNAME(dstr));
a0d0e21e 2657 (void)SvOK_off(dstr);
a5f75d66 2658 GvINTRO_off(dstr); /* one-shot flag */
1edc1566 2659 gp_free((GV*)dstr);
79072805 2660 GvGP(dstr) = gp_ref(GvGP(sstr));
27c9684d
AP
2661 if (SvTAINTED(sstr))
2662 SvTAINT(dstr);
1d7c1841
GS
2663 if (GvIMPORTED(dstr) != GVf_IMPORTED
2664 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
2665 {
a5f75d66 2666 GvIMPORTED_on(dstr);
1d7c1841 2667 }
a5f75d66 2668 GvMULTI_on(dstr);
79072805
LW
2669 return;
2670 }
2671 /* FALL THROUGH */
2672
2673 default:
973f89ab
CS
2674 if (SvGMAGICAL(sstr)) {
2675 mg_get(sstr);
2676 if (SvTYPE(sstr) != stype) {
2677 stype = SvTYPE(sstr);
2678 if (stype == SVt_PVGV && dtype <= SVt_PVGV)
2679 goto glob_assign;
2680 }
2681 }
ded42b9f 2682 if (stype == SVt_PVLV)
6fc92669 2683 (void)SvUPGRADE(dstr, SVt_PVNV);
ded42b9f 2684 else
6fc92669 2685 (void)SvUPGRADE(dstr, stype);
79072805
LW
2686 }
2687
8990e307
LW
2688 sflags = SvFLAGS(sstr);
2689
2690 if (sflags & SVf_ROK) {
2691 if (dtype >= SVt_PV) {
2692 if (dtype == SVt_PVGV) {
2693 SV *sref = SvREFCNT_inc(SvRV(sstr));
2694 SV *dref = 0;
a5f75d66 2695 int intro = GvINTRO(dstr);
a0d0e21e
LW
2696
2697 if (intro) {
2698 GP *gp;
1d7c1841 2699 gp_free((GV*)dstr);
a5f75d66 2700 GvINTRO_off(dstr); /* one-shot flag */
a0d0e21e 2701 Newz(602,gp, 1, GP);
44a8e56a 2702 GvGP(dstr) = gp_ref(gp);
a0d0e21e 2703 GvSV(dstr) = NEWSV(72,0);
1d7c1841 2704 GvLINE(dstr) = CopLINE(PL_curcop);
1edc1566 2705 GvEGV(dstr) = (GV*)dstr;
a0d0e21e 2706 }
a5f75d66 2707 GvMULTI_on(dstr);
8990e307
LW
2708 switch (SvTYPE(sref)) {
2709 case SVt_PVAV:
a0d0e21e
LW
2710 if (intro)
2711 SAVESPTR(GvAV(dstr));
2712 else
2713 dref = (SV*)GvAV(dstr);
8990e307 2714 GvAV(dstr) = (AV*)sref;
39bac7f7 2715 if (!GvIMPORTED_AV(dstr)
1d7c1841
GS
2716 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
2717 {
a5f75d66 2718 GvIMPORTED_AV_on(dstr);
1d7c1841 2719 }
8990e307
LW
2720 break;
2721 case SVt_PVHV:
a0d0e21e
LW
2722 if (intro)
2723 SAVESPTR(GvHV(dstr));
2724 else
2725 dref = (SV*)GvHV(dstr);
8990e307 2726 GvHV(dstr) = (HV*)sref;
39bac7f7 2727 if (!GvIMPORTED_HV(dstr)
1d7c1841
GS
2728 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
2729 {
a5f75d66 2730 GvIMPORTED_HV_on(dstr);
1d7c1841 2731 }
8990e307
LW
2732 break;
2733 case SVt_PVCV:
8ebc5c01
PP
2734 if (intro) {
2735 if (GvCVGEN(dstr) && GvCV(dstr) != (CV*)sref) {
2736 SvREFCNT_dec(GvCV(dstr));
2737 GvCV(dstr) = Nullcv;
68dc0745 2738 GvCVGEN(dstr) = 0; /* Switch off cacheness. */
3280af22 2739 PL_sub_generation++;
8ebc5c01 2740 }
a0d0e21e 2741 SAVESPTR(GvCV(dstr));
8ebc5c01 2742 }
68dc0745
PP
2743 else
2744 dref = (SV*)GvCV(dstr);
2745 if (GvCV(dstr) != (CV*)sref) {
748a9306 2746 CV* cv = GvCV(dstr);
4633a7c4 2747 if (cv) {
68dc0745
PP
2748 if (!GvCVGEN((GV*)dstr) &&
2749 (CvROOT(cv) || CvXSUB(cv)))
2750 {
beab0874 2751 SV *const_sv;
7bac28a0
PP
2752 /* ahem, death to those who redefine
2753 * active sort subs */
3280af22
NIS
2754 if (PL_curstackinfo->si_type == PERLSI_SORT &&
2755 PL_sortcop == CvSTART(cv))
1c846c1f 2756 Perl_croak(aTHX_
7bac28a0
PP
2757 "Can't redefine active sort subroutine %s",
2758 GvENAME((GV*)dstr));
beab0874
JT
2759 /* Redefining a sub - warning is mandatory if
2760 it was a const and its value changed. */
2761 if (ckWARN(WARN_REDEFINE)
2762 || (CvCONST(cv)
2763 && (!CvCONST((CV*)sref)
2764 || sv_cmp(cv_const_sv(cv),
2765 cv_const_sv((CV*)sref)))))
2766 {
2767 Perl_warner(aTHX_ WARN_REDEFINE,
2768 CvCONST(cv)
2769 ? "Constant subroutine %s redefined"
47deb5e7 2770 : "Subroutine %s redefined",
beab0874
JT
2771 GvENAME((GV*)dstr));
2772 }
9607fc9c 2773 }
3fe9a6f1
PP
2774 cv_ckproto(cv, (GV*)dstr,
2775 SvPOK(sref) ? SvPVX(sref) : Nullch);
4633a7c4 2776 }
a5f75d66 2777 GvCV(dstr) = (CV*)sref;
7a4c00b4 2778 GvCVGEN(dstr) = 0; /* Switch off cacheness. */
a5f75d66 2779 GvASSUMECV_on(dstr);
3280af22 2780 PL_sub_generation++;
a5f75d66 2781 }
39bac7f7 2782 if (!GvIMPORTED_CV(dstr)
1d7c1841
GS
2783 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
2784 {
a5f75d66 2785 GvIMPORTED_CV_on(dstr);
1d7c1841 2786 }
8990e307 2787 break;
91bba347
LW
2788 case SVt_PVIO:
2789 if (intro)
2790 SAVESPTR(GvIOp(dstr));
2791 else
2792 dref = (SV*)GvIOp(dstr);
2793 GvIOp(dstr) = (IO*)sref;
2794 break;
f4d13ee9
JH
2795 case SVt_PVFM:
2796 if (intro)
2797 SAVESPTR(GvFORM(dstr));
2798 else
2799 dref = (SV*)GvFORM(dstr);
2800 GvFORM(dstr) = (CV*)sref;
2801 break;
8990e307 2802 default:
a0d0e21e
LW
2803 if (intro)
2804 SAVESPTR(GvSV(dstr));
2805 else
2806 dref = (SV*)GvSV(dstr);
8990e307 2807 GvSV(dstr) = sref;
39bac7f7 2808 if (!GvIMPORTED_SV(dstr)
1d7c1841
GS
2809 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
2810 {
a5f75d66 2811 GvIMPORTED_SV_on(dstr);
1d7c1841 2812 }
8990e307
LW
2813 break;
2814 }
2815 if (dref)
2816 SvREFCNT_dec(dref);
a0d0e21e
LW
2817 if (intro)
2818 SAVEFREESV(sref);
27c9684d
AP
2819 if (SvTAINTED(sstr))
2820 SvTAINT(dstr);
8990e307
LW
2821 return;
2822 }
a0d0e21e 2823 if (SvPVX(dstr)) {
760ac839 2824 (void)SvOOK_off(dstr); /* backoff */
50483b2c
JD
2825 if (SvLEN(dstr))
2826 Safefree(SvPVX(dstr));
a0d0e21e
LW
2827 SvLEN(dstr)=SvCUR(dstr)=0;
2828 }
8990e307 2829 }
a0d0e21e 2830 (void)SvOK_off(dstr);
8990e307 2831 SvRV(dstr) = SvREFCNT_inc(SvRV(sstr));
ed6116ce 2832 SvROK_on(dstr);
8990e307 2833 if (sflags & SVp_NOK) {
ed6116ce
LW
2834 SvNOK_on(dstr);
2835 SvNVX(dstr) = SvNVX(sstr);
2836 }
8990e307 2837 if (sflags & SVp_IOK) {
a0d0e21e 2838 (void)SvIOK_on(dstr);
ed6116ce 2839 SvIVX(dstr) = SvIVX(sstr);
2b1c7e3e 2840 if (sflags & SVf_IVisUV)
25da4f38 2841 SvIsUV_on(dstr);
ed6116ce 2842 }
a0d0e21e
LW
2843 if (SvAMAGIC(sstr)) {
2844 SvAMAGIC_on(dstr);
2845 }
ed6116ce 2846 }
8990e307 2847 else if (sflags & SVp_POK) {
79072805
LW
2848
2849 /*
2850 * Check to see if we can just swipe the string. If so, it's a
2851 * possible small lose on short strings, but a big win on long ones.
463ee0b2
LW
2852 * It might even be a win on short strings if SvPVX(dstr)
2853 * has to be allocated and SvPVX(sstr) has to be freed.
79072805
LW
2854 */
2855
ff68c719 2856 if (SvTEMP(sstr) && /* slated for free anyway? */
01b73108 2857 SvREFCNT(sstr) == 1 && /* and no other references to it? */
1c846c1f 2858 !(sflags & SVf_OOK) && /* and not involved in OOK hack? */
4c8f17b9
BH
2859 SvLEN(sstr) && /* and really is a string */
2860 !(PL_op && PL_op->op_type == OP_AASSIGN)) /* and won't be needed again, potentially */
a5f75d66 2861 {
adbc6bb1 2862 if (SvPVX(dstr)) { /* we know that dtype >= SVt_PV */
a5f75d66
AD
2863 if (SvOOK(dstr)) {
2864 SvFLAGS(dstr) &= ~SVf_OOK;
2865 Safefree(SvPVX(dstr) - SvIVX(dstr));
2866 }
50483b2c 2867 else if (SvLEN(dstr))
a5f75d66 2868 Safefree(SvPVX(dstr));
79072805 2869 }
a5f75d66 2870 (void)SvPOK_only(dstr);
463ee0b2 2871 SvPV_set(dstr, SvPVX(sstr));
79072805
LW
2872 SvLEN_set(dstr, SvLEN(sstr));
2873 SvCUR_set(dstr, SvCUR(sstr));
f4e86e0f 2874
79072805 2875 SvTEMP_off(dstr);
2b1c7e3e 2876 (void)SvOK_off(sstr); /* NOTE: nukes most SvFLAGS on sstr */
79072805
LW
2877 SvPV_set(sstr, Nullch);
2878 SvLEN_set(sstr, 0);
a5f75d66
AD
2879 SvCUR_set(sstr, 0);
2880 SvTEMP_off(sstr);
79072805
LW
2881 }
2882 else { /* have to copy actual string */
8990e307
LW
2883 STRLEN len = SvCUR(sstr);
2884
2885 SvGROW(dstr, len + 1); /* inlined from sv_setpvn */
2886 Move(SvPVX(sstr),SvPVX(dstr),len,char);
2887 SvCUR_set(dstr, len);
2888 *SvEND(dstr) = '\0';
a0d0e21e 2889 (void)SvPOK_only(dstr);
79072805 2890 }
3aa33fe5 2891 if ((sflags & SVf_UTF8) && !IN_BYTE)
a7cb1f99 2892 SvUTF8_on(dstr);
79072805 2893 /*SUPPRESS 560*/
8990e307 2894 if (sflags & SVp_NOK) {
79072805 2895 SvNOK_on(dstr);
463ee0b2 2896 SvNVX(dstr) = SvNVX(sstr);
79072805 2897 }
8990e307 2898 if (sflags & SVp_IOK) {
a0d0e21e 2899 (void)SvIOK_on(dstr);
463ee0b2 2900 SvIVX(dstr) = SvIVX(sstr);
2b1c7e3e 2901 if (sflags & SVf_IVisUV)
25da4f38 2902 SvIsUV_on(dstr);
79072805
LW
2903 }
2904 }
8990e307 2905 else if (sflags & SVp_NOK) {
463ee0b2 2906 SvNVX(dstr) = SvNVX(sstr);
a0d0e21e 2907 (void)SvNOK_only(dstr);
2b1c7e3e 2908 if (sflags & SVf_IOK) {
a0d0e21e 2909 (void)SvIOK_on(dstr);
463ee0b2 2910 SvIVX(dstr) = SvIVX(sstr);
25da4f38 2911 /* XXXX Do we want to set IsUV for IV(ROK)? Be extra safe... */
2b1c7e3e 2912 if (sflags & SVf_IVisUV)
25da4f38 2913 SvIsUV_on(dstr);
79072805
LW
2914 }
2915 }
8990e307 2916 else if (sflags & SVp_IOK) {
a0d0e21e 2917 (void)SvIOK_only(dstr);
463ee0b2 2918 SvIVX(dstr) = SvIVX(sstr);
2b1c7e3e 2919 if (sflags & SVf_IVisUV)
25da4f38 2920 SvIsUV_on(dstr);
79072805
LW
2921 }
2922 else {
20408e3c 2923 if (dtype == SVt_PVGV) {
e476b1b5
GS
2924 if (ckWARN(WARN_MISC))
2925 Perl_warner(aTHX_ WARN_MISC, "Undefined value assigned to typeglob");
20408e3c
GS
2926 }
2927 else
2928 (void)SvOK_off(dstr);
a0d0e21e 2929 }
27c9684d
AP
2930 if (SvTAINTED(sstr))
2931 SvTAINT(dstr);
79072805
LW
2932}
2933
954c1994
GS
2934/*
2935=for apidoc sv_setsv_mg
2936
2937Like C<sv_setsv>, but also handles 'set' magic.
2938
2939=cut
2940*/
2941
79072805 2942void
864dbfa3 2943Perl_sv_setsv_mg(pTHX_ SV *dstr, register SV *sstr)
ef50df4b
GS
2944{
2945 sv_setsv(dstr,sstr);
2946 SvSETMAGIC(dstr);
2947}
2948
954c1994
GS
2949/*
2950=for apidoc sv_setpvn
2951
2952Copies a string into an SV. The C<len> parameter indicates the number of
2953bytes to be copied. Does not handle 'set' magic. See C<sv_setpvn_mg>.
2954
2955=cut
2956*/
2957
ef50df4b 2958void
864dbfa3 2959Perl_sv_setpvn(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
79072805 2960{
c6f8c383 2961 register char *dptr;
793db0cb
JH
2962 {
2963 /* len is STRLEN which is unsigned, need to copy to signed */
2964 IV iv = len;
2965 assert(iv >= 0);
2966 }
2213622d 2967 SV_CHECK_THINKFIRST(sv);
463ee0b2 2968 if (!ptr) {
a0d0e21e 2969 (void)SvOK_off(sv);
463ee0b2
LW
2970 return;
2971 }
6fc92669 2972 (void)SvUPGRADE(sv, SVt_PV);
c6f8c383 2973
79072805 2974 SvGROW(sv, len + 1);
c6f8c383
GA
2975 dptr = SvPVX(sv);
2976 Move(ptr,dptr,len,char);
2977 dptr[len] = '\0';
79072805 2978 SvCUR_set(sv, len);
a0d0e21e 2979 (void)SvPOK_only(sv); /* validate pointer */
463ee0b2 2980 SvTAINT(sv);
79072805
LW
2981}
2982
954c1994
GS
2983/*
2984=for apidoc sv_setpvn_mg
2985
2986Like C<sv_setpvn>, but also handles 'set' magic.
2987
2988=cut
2989*/
2990
79072805 2991void
864dbfa3 2992Perl_sv_setpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
ef50df4b
GS
2993{
2994 sv_setpvn(sv,ptr,len);
2995 SvSETMAGIC(sv);
2996}
2997
954c1994
GS
2998/*
2999=for apidoc sv_setpv
3000
3001Copies a string into an SV. The string must be null-terminated. Does not
3002handle 'set' magic. See C<sv_setpv_mg>.
3003
3004=cut
3005*/
3006
ef50df4b 3007void
864dbfa3 3008Perl_sv_setpv(pTHX_ register SV *sv, register const char *ptr)
79072805
LW
3009{
3010 register STRLEN len;
3011
2213622d 3012 SV_CHECK_THINKFIRST(sv);
463ee0b2 3013 if (!ptr) {
a0d0e21e 3014 (void)SvOK_off(sv);
463ee0b2
LW
3015 return;
3016 }
79072805 3017 len = strlen(ptr);
6fc92669 3018 (void)SvUPGRADE(sv, SVt_PV);
c6f8c383 3019
79072805 3020 SvGROW(sv, len + 1);
463ee0b2 3021 Move(ptr,SvPVX(sv),len+1,char);
79072805 3022 SvCUR_set(sv, len);
a0d0e21e 3023 (void)SvPOK_only(sv); /* validate pointer */
463ee0b2
LW
3024 SvTAINT(sv);
3025}
3026
954c1994
GS
3027/*
3028=for apidoc sv_setpv_mg
3029
3030Like C<sv_setpv>, but also handles 'set' magic.
3031
3032=cut
3033*/
3034
463ee0b2 3035void
864dbfa3 3036Perl_sv_setpv_mg(pTHX_ register SV *sv, register const char *ptr)
ef50df4b
GS
3037{
3038 sv_setpv(sv,ptr);
3039 SvSETMAGIC(sv);
3040}
3041
954c1994
GS
3042/*
3043=for apidoc sv_usepvn
3044
3045Tells an SV to use C<ptr> to find its string value. Normally the string is
1c846c1f 3046stored inside the SV but sv_usepvn allows the SV to use an outside string.
954c1994
GS
3047The C<ptr> should point to memory that was allocated by C<malloc>. The
3048string length, C<len>, must be supplied. This function will realloc the
3049memory pointed to by C<ptr>, so that pointer should not be freed or used by
3050the programmer after giving it to sv_usepvn. Does not handle 'set' magic.
3051See C<sv_usepvn_mg>.
3052
3053=cut
3054*/
3055
ef50df4b 3056void
864dbfa3 3057Perl_sv_usepvn(pTHX_ register SV *sv, register char *ptr, register STRLEN len)
463ee0b2 3058{
2213622d 3059 SV_CHECK_THINKFIRST(sv);
c6f8c383 3060 (void)SvUPGRADE(sv, SVt_PV);
463ee0b2 3061 if (!ptr) {
a0d0e21e 3062 (void)SvOK_off(sv);
463ee0b2
LW
3063 return;
3064 }
a0ed51b3 3065 (void)SvOOK_off(sv);
50483b2c 3066 if (SvPVX(sv) && SvLEN(sv))
463ee0b2
LW
3067 Safefree(SvPVX(sv));
3068 Renew(ptr, len+1, char);
3069 SvPVX(sv) = ptr;
3070 SvCUR_set(sv, len);
3071 SvLEN_set(sv, len+1);
3072 *SvEND(sv) = '\0';
a0d0e21e 3073 (void)SvPOK_only(sv); /* validate pointer */
463ee0b2 3074 SvTAINT(sv);
79072805
LW
3075}
3076
954c1994
GS
3077/*
3078=for apidoc sv_usepvn_mg
3079
3080Like C<sv_usepvn>, but also handles 'set' magic.
3081
3082=cut
3083*/
3084
ef50df4b 3085void
864dbfa3 3086Perl_sv_usepvn_mg(pTHX_ register SV *sv, register char *ptr, register STRLEN len)
ef50df4b 3087{
51c1089b 3088 sv_usepvn(sv,ptr,len);
ef50df4b
GS
3089 SvSETMAGIC(sv);
3090}
3091
6fc92669 3092void
840a7b70 3093Perl_sv_force_normal_flags(pTHX_ register SV *sv, U32 flags)
0f15f207 3094{
2213622d 3095 if (SvREADONLY(sv)) {
1c846c1f
NIS
3096 if (SvFAKE(sv)) {
3097 char *pvx = SvPVX(sv);
3098 STRLEN len = SvCUR(sv);
3099 U32 hash = SvUVX(sv);
3100 SvGROW(sv, len + 1);
3101 Move(pvx,SvPVX(sv),len,char);
3102 *SvEND(sv) = '\0';
3103 SvFAKE_off(sv);
3104 SvREADONLY_off(sv);
c3654f1a 3105 unsharepvn(pvx,SvUTF8(sv)?-len:len,hash);
1c846c1f
NIS
3106 }
3107 else if (PL_curcop != &PL_compiling)
cea2e8a9 3108 Perl_croak(aTHX_ PL_no_modify);
0f15f207 3109 }
2213622d 3110 if (SvROK(sv))
840a7b70 3111 sv_unref_flags(sv, flags);
6fc92669
GS
3112 else if (SvFAKE(sv) && SvTYPE(sv) == SVt_PVGV)
3113 sv_unglob(sv);
0f15f207 3114}
1c846c1f 3115
840a7b70
IZ
3116void
3117Perl_sv_force_normal(pTHX_ register SV *sv)
3118{
3119 sv_force_normal_flags(sv, 0);
3120}
3121
954c1994
GS
3122/*
3123=for apidoc sv_chop
3124
1c846c1f 3125Efficient removal of characters from the beginning of the string buffer.
954c1994
GS
3126SvPOK(sv) must be true and the C<ptr> must be a pointer to somewhere inside
3127the string buffer. The C<ptr> becomes the first character of the adjusted
3128string.
3129
3130=cut
3131*/
3132
79072805 3133void
864dbfa3 3134Perl_sv_chop(pTHX_ register SV *sv, register char *ptr) /* like set but assuming ptr is in sv */
1c846c1f
NIS
3135
3136
79072805
LW
3137{
3138 register STRLEN delta;
3139
a0d0e21e 3140 if (!ptr || !SvPOKp(sv))
79072805 3141 return;
2213622d 3142 SV_CHECK_THINKFIRST(sv);
79072805
LW
3143 if (SvTYPE(sv) < SVt_PVIV)
3144 sv_upgrade(sv,SVt_PVIV);
3145
3146 if (!SvOOK(sv)) {
50483b2c
JD
3147 if (!SvLEN(sv)) { /* make copy of shared string */
3148 char *pvx = SvPVX(sv);
3149 STRLEN len = SvCUR(sv);
3150 SvGROW(sv, len + 1);
3151 Move(pvx,SvPVX(sv),len,char);
3152 *SvEND(sv) = '\0';
3153 }
463ee0b2 3154 SvIVX(sv) = 0;
79072805
LW
3155 SvFLAGS(sv) |= SVf_OOK;
3156 }
25da4f38 3157 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVp_IOK|SVp_NOK|SVf_IVisUV);
463ee0b2 3158 delta = ptr - SvPVX(sv);
79072805
LW
3159 SvLEN(sv) -= delta;
3160 SvCUR(sv) -= delta;
463ee0b2
LW
3161 SvPVX(sv) += delta;
3162 SvIVX(sv) += delta;
79072805
LW
3163}
3164
954c1994
GS
3165/*
3166=for apidoc sv_catpvn
3167
3168Concatenates the string onto the end of the string which is in the SV. The
3169C<len> indicates number of bytes to copy. Handles 'get' magic, but not
3170'set' magic. See C<sv_catpvn_mg>.
3171
3172=cut
3173*/
3174
79072805 3175void
864dbfa3 3176Perl_sv_catpvn(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
79072805 3177{
463ee0b2 3178 STRLEN tlen;
748a9306 3179 char *junk;
a0d0e21e 3180
748a9306 3181 junk = SvPV_force(sv, tlen);
463ee0b2 3182 SvGROW(sv, tlen + len + 1);
4633a7c4
LW
3183 if (ptr == junk)
3184 ptr = SvPVX(sv);
463ee0b2 3185 Move(ptr,SvPVX(sv)+tlen,len,char);
79072805
LW
3186 SvCUR(sv) += len;
3187 *SvEND(sv) = '\0';
d41ff1b8 3188 (void)SvPOK_only_UTF8(sv); /* validate pointer */
463ee0b2 3189 SvTAINT(sv);
79072805
LW
3190}
3191
954c1994
GS
3192/*
3193=for apidoc sv_catpvn_mg
3194
3195Like C<sv_catpvn>, but also handles 'set' magic.
3196
3197=cut
3198*/
3199
79072805 3200void
864dbfa3 3201Perl_sv_catpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
ef50df4b
GS
3202{
3203 sv_catpvn(sv,ptr,len);
3204 SvSETMAGIC(sv);
3205}
3206
954c1994
GS
3207/*
3208=for apidoc sv_catsv
3209
3210Concatenates the string from SV C<ssv> onto the end of the string in SV
3211C<dsv>. Handles 'get' magic, but not 'set' magic. See C<sv_catsv_mg>.
3212
3213=cut
3214*/
3215
ef50df4b 3216void
864dbfa3 3217Perl_sv_catsv(pTHX_ SV *dstr, register SV *sstr)
79072805
LW
3218{
3219 char *s;
463ee0b2 3220 STRLEN len;
79072805
LW
3221 if (!sstr)
3222 return;
155aba94 3223 if ((s = SvPV(sstr, len))) {
e84ff256 3224 if (DO_UTF8(sstr)) {
560a288e 3225 sv_utf8_upgrade(dstr);
e84ff256 3226 sv_catpvn(dstr,s,len);
560a288e 3227 SvUTF8_on(dstr);
e84ff256
GS
3228 }
3229 else
3230 sv_catpvn(dstr,s,len);
560a288e 3231 }
79072805
LW
3232}
3233
954c1994
GS
3234/*
3235=for apidoc sv_catsv_mg
3236
3237Like C<sv_catsv>, but also handles 'set' magic.
3238
3239=cut
3240*/
3241
79072805 3242void
864dbfa3 3243Perl_sv_catsv_mg(pTHX_ SV *dstr, register SV *sstr)
ef50df4b
GS
3244{
3245 sv_catsv(dstr,sstr);
3246 SvSETMAGIC(dstr);
3247}
3248
954c1994
GS
3249/*
3250=for apidoc sv_catpv
3251
3252Concatenates the string onto the end of the string which is in the SV.
3253Handles 'get' magic, but not 'set' magic. See C<sv_catpv_mg>.
3254
3255=cut
3256*/
3257
ef50df4b 3258void
864dbfa3 3259Perl_sv_catpv(pTHX_ register SV *sv, register const char *ptr)
79072805
LW
3260{
3261 register STRLEN len;
463ee0b2 3262 STRLEN tlen;
748a9306 3263 char *junk;
79072805 3264
79072805
LW
3265 if (!ptr)
3266 return;
748a9306 3267 junk = SvPV_force(sv, tlen);
79072805 3268 len = strlen(ptr);
463ee0b2 3269 SvGROW(sv, tlen + len + 1);
4633a7c4
LW
3270 if (ptr == junk)
3271 ptr = SvPVX(sv);
463ee0b2 3272 Move(ptr,SvPVX(sv)+tlen,len+1,char);
79072805 3273 SvCUR(sv) += len;
d41ff1b8 3274 (void)SvPOK_only_UTF8(sv); /* validate pointer */
463ee0b2 3275 SvTAINT(sv);
79072805
LW
3276}
3277
954c1994
GS
3278/*
3279=for apidoc sv_catpv_mg
3280
3281Like C<sv_catpv>, but also handles 'set' magic.
3282
3283=cut
3284*/
3285
ef50df4b 3286void
864dbfa3 3287Perl_sv_catpv_mg(pTHX_ register SV *sv, register const char *ptr)
ef50df4b 3288{
51c1089b 3289 sv_catpv(sv,ptr);
ef50df4b
GS
3290 SvSETMAGIC(sv);
3291}
3292
79072805 3293SV *
864dbfa3 3294Perl_newSV(pTHX_ STRLEN len)
79072805
LW
3295{
3296 register SV *sv;
1c846c1f 3297
4561caa4 3298 new_SV(sv);
79072805
LW
3299 if (len) {
3300 sv_upgrade(sv, SVt_PV);
3301 SvGROW(sv, len + 1);
3302 }
3303 return sv;
3304}
3305
1edc1566
PP
3306/* name is assumed to contain an SV* if (name && namelen == HEf_SVKEY) */
3307
954c1994
GS
3308/*
3309=for apidoc sv_magic
3310
3311Adds magic to an SV.
3312
3313=cut
3314*/
3315
79072805 3316void
864dbfa3 3317Perl_sv_magic(pTHX_ register SV *sv, SV *obj, int how, const char *name, I32 namlen)
79072805
LW
3318{
3319 MAGIC* mg;
1c846c1f 3320
0f15f207 3321 if (SvREADONLY(sv)) {
3280af22 3322 if (PL_curcop != &PL_compiling && !strchr("gBf", how))
cea2e8a9 3323 Perl_croak(aTHX_ PL_no_modify);
0f15f207 3324 }
4633a7c4 3325 if (SvMAGICAL(sv) || (how == 't' && SvTYPE(sv) >= SVt_PVMG)) {
748a9306
LW
3326 if (SvMAGIC(sv) && (mg = mg_find(sv, how))) {
3327 if (how == 't')
565764a8 3328 mg->mg_len |= 1;
463ee0b2 3329 return;
748a9306 3330 }
463ee0b2
LW
3331 }
3332 else {
c6f8c383 3333 (void)SvUPGRADE(sv, SVt_PVMG);
463ee0b2 3334 }
79072805
LW
3335 Newz(702,mg, 1, MAGIC);
3336 mg->mg_moremagic = SvMAGIC(sv);
463ee0b2 3337
79072805 3338 SvMAGIC(sv) = mg;
c277df42 3339 if (!obj || obj == sv || how == '#' || how == 'r')
8990e307 3340 mg->mg_obj = obj;
85e6fe83 3341 else {
8990e307 3342 mg->mg_obj = SvREFCNT_inc(obj);
85e6fe83
LW
3343 mg->mg_flags |= MGf_REFCOUNTED;
3344 }
79072805 3345 mg->mg_type = how;
565764a8 3346 mg->mg_len = namlen;
1edc1566
PP
3347 if (name)
3348 if (namlen >= 0)
3349 mg->mg_ptr = savepvn(name, namlen);
c6ee37c5 3350 else if (namlen == HEf_SVKEY)
1edc1566 3351 mg->mg_ptr = (char*)SvREFCNT_inc((SV*)name);
1c846c1f 3352
79072805
LW
3353 switch (how) {
3354 case 0:
22c35a8c 3355 mg->mg_virtual = &PL_vtbl_sv;
79072805 3356 break;
a0d0e21e 3357 case 'A':
22c35a8c 3358 mg->mg_virtual = &PL_vtbl_amagic;
a0d0e21e
LW
3359 break;
3360 case 'a':
22c35a8c 3361 mg->mg_virtual = &PL_vtbl_amagicelem;
a0d0e21e
LW
3362 break;
3363 case 'c':
3364 mg->mg_virtual = 0;
3365 break;
79072805 3366 case 'B':
22c35a8c 3367 mg->mg_virtual = &PL_vtbl_bm;
79072805 3368 break;
6cef1e77 3369 case 'D':
22c35a8c 3370 mg->mg_virtual = &PL_vtbl_regdata;
6cef1e77
IZ
3371 break;
3372 case 'd':
22c35a8c 3373 mg->mg_virtual = &PL_vtbl_regdatum;
6cef1e77 3374 break;
79072805 3375 case 'E':
22c35a8c 3376 mg->mg_virtual = &PL_vtbl_env;
79072805 3377 break;
55497cff 3378 case 'f':
22c35a8c 3379 mg->mg_virtual = &PL_vtbl_fm;
55497cff 3380 break;
79072805 3381 case 'e':
22c35a8c 3382 mg->mg_virtual = &PL_vtbl_envelem;
79072805 3383 break;
93a17b20 3384 case 'g':
22c35a8c 3385 mg->mg_virtual = &PL_vtbl_mglob;
93a17b20 3386 break;
463ee0b2 3387 case 'I':
22c35a8c 3388 mg->mg_virtual = &PL_vtbl_isa;
463ee0b2
LW
3389 break;
3390 case 'i':
22c35a8c 3391 mg->mg_virtual = &PL_vtbl_isaelem;
463ee0b2 3392 break;
16660edb 3393 case 'k':
22c35a8c 3394 mg->mg_virtual = &PL_vtbl_nkeys;
16660edb 3395 break;
79072805 3396 case 'L':
a0d0e21e 3397 SvRMAGICAL_on(sv);
93a17b20
LW
3398 mg->mg_virtual = 0;
3399 break;
3400 case 'l':
22c35a8c 3401 mg->mg_virtual = &PL_vtbl_dbline;
79072805 3402 break;
f93b4edd
MB
3403#ifdef USE_THREADS
3404 case 'm':
22c35a8c 3405 mg->mg_virtual = &PL_vtbl_mutex;
f93b4edd
MB
3406 break;
3407#endif /* USE_THREADS */
36477c24 3408#ifdef USE_LOCALE_COLLATE
bbce6d69 3409 case 'o':
22c35a8c 3410 mg->mg_virtual = &PL_vtbl_collxfrm;
bbce6d69 3411 break;
36477c24 3412#endif /* USE_LOCALE_COLLATE */
463ee0b2 3413 case 'P':
22c35a8c 3414 mg->mg_virtual = &PL_vtbl_pack;
463ee0b2
LW
3415 break;
3416 case 'p':
a0d0e21e 3417 case 'q':
22c35a8c 3418 mg->mg_virtual = &PL_vtbl_packelem;
463ee0b2 3419 break;
c277df42 3420 case 'r':
22c35a8c 3421 mg->mg_virtual = &PL_vtbl_regexp;
c277df42 3422 break;
79072805 3423 case 'S':
22c35a8c 3424 mg->mg_virtual = &PL_vtbl_sig;
79072805
LW
3425 break;
3426 case 's':
22c35a8c 3427 mg->mg_virtual = &PL_vtbl_sigelem;
79072805 3428 break;
463ee0b2 3429 case 't':
22c35a8c 3430 mg->mg_virtual = &PL_vtbl_taint;
565764a8 3431 mg->mg_len = 1;
463ee0b2 3432 break;
79072805 3433 case 'U':
22c35a8c 3434 mg->mg_virtual = &PL_vtbl_uvar;
79072805
LW
3435 break;
3436 case 'v':
22c35a8c 3437 mg->mg_virtual = &PL_vtbl_vec;
79072805
LW
3438 break;
3439 case 'x':
22c35a8c 3440 mg->mg_virtual = &PL_vtbl_substr;
79072805 3441 break;
5f05dabc 3442 case 'y':
22c35a8c 3443 mg->mg_virtual = &PL_vtbl_defelem;
5f05dabc 3444 break;
79072805 3445 case '*':
22c35a8c 3446 mg->mg_virtual = &PL_vtbl_glob;
79072805
LW
3447 break;
3448 case '#':
22c35a8c 3449 mg->mg_virtual = &PL_vtbl_arylen;
79072805 3450 break;
a0d0e21e 3451 case '.':
22c35a8c 3452 mg->mg_virtual = &PL_vtbl_pos;
a0d0e21e 3453 break;
810b8aa5
GS
3454 case '<':
3455 mg->mg_virtual = &PL_vtbl_backref;
3456 break;
4633a7c4
LW
3457 case '~': /* Reserved for use by extensions not perl internals. */
3458 /* Useful for attaching extension internal data to perl vars. */
3459 /* Note that multiple extensions may clash if magical scalars */
3460 /* etc holding private data from one are passed to another. */
3461 SvRMAGICAL_on(sv);
a0d0e21e 3462 break;
79072805 3463 default:
cea2e8a9 3464 Perl_croak(aTHX_ "Don't know how to handle magic of type '%c'", how);
463ee0b2 3465 }
8990e307
LW
3466 mg_magical(sv);
3467 if (SvGMAGICAL(sv))
3468 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
463ee0b2
LW
3469}
3470
c461cf8f
JH
3471/*
3472=for apidoc sv_unmagic
3473
3474Removes magic from an SV.
3475
3476=cut
3477*/
3478
463ee0b2 3479int
864dbfa3 3480Perl_sv_unmagic(pTHX_ SV *sv, int type)
463ee0b2
LW
3481{
3482 MAGIC* mg;
3483 MAGIC** mgp;
91bba347 3484 if (SvTYPE(sv) < SVt_PVMG || !SvMAGIC(sv))
463ee0b2
LW
3485 return 0;
3486 mgp = &SvMAGIC(sv);
3487 for (mg = *mgp; mg; mg = *mgp) {
3488 if (mg->mg_type == type) {
3489 MGVTBL* vtbl = mg->mg_virtual;
3490 *mgp = mg->mg_moremagic;
1d7c1841 3491 if (vtbl && vtbl->svt_free)
fc0dc3b3 3492 CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg);
463ee0b2 3493 if (mg->mg_ptr && mg->mg_type != 'g')
565764a8 3494 if (mg->mg_len >= 0)
1edc1566 3495 Safefree(mg->mg_ptr);
565764a8 3496 else if (mg->mg_len == HEf_SVKEY)
1edc1566 3497 SvREFCNT_dec((SV*)mg->mg_ptr);
a0d0e21e
LW
3498 if (mg->mg_flags & MGf_REFCOUNTED)
3499 SvREFCNT_dec(mg->mg_obj);
463ee0b2
LW
3500 Safefree(mg);
3501 }
3502 else
3503 mgp = &mg->mg_moremagic;
79072805 3504 }
91bba347 3505 if (!SvMAGIC(sv)) {
463ee0b2 3506 SvMAGICAL_off(sv);
8990e307 3507 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
463ee0b2
LW
3508 }
3509
3510 return 0;
79072805
LW
3511}
3512
c461cf8f
JH
3513/*
3514=for apidoc sv_rvweaken
3515
3516Weaken a reference.
3517
3518=cut
3519*/
3520
810b8aa5 3521SV *
864dbfa3 3522Perl_sv_rvweaken(pTHX_ SV *sv)
810b8aa5
GS
3523{
3524 SV *tsv;
3525 if (!SvOK(sv)) /* let undefs pass */
3526 return sv;
3527 if (!SvROK(sv))
cea2e8a9 3528 Perl_croak(aTHX_ "Can't weaken a nonreference");
810b8aa5 3529 else if (SvWEAKREF(sv)) {
810b8aa5 3530 if (ckWARN(WARN_MISC))
cea2e8a9 3531 Perl_warner(aTHX_ WARN_MISC, "Reference is already weak");
810b8aa5
GS
3532 return sv;
3533 }
3534 tsv = SvRV(sv);
3535 sv_add_backref(tsv, sv);
3536 SvWEAKREF_on(sv);
1c846c1f 3537 SvREFCNT_dec(tsv);
810b8aa5
GS
3538 return sv;
3539}
3540
3541STATIC void
cea2e8a9 3542S_sv_add_backref(pTHX_ SV *tsv, SV *sv)
810b8aa5
GS
3543{
3544 AV *av;
3545 MAGIC *mg;
3546 if (SvMAGICAL(tsv) && (mg = mg_find(tsv, '<')))
3547 av = (AV*)mg->mg_obj;
3548 else {
3549 av = newAV();
3550 sv_magic(tsv, (SV*)av, '<', NULL, 0);
3551 SvREFCNT_dec(av); /* for sv_magic */
3552 }
3553 av_push(av,sv);
3554}
3555
1c846c1f 3556STATIC void
cea2e8a9 3557S_sv_del_backref(pTHX_ SV *sv)
810b8aa5
GS
3558{
3559 AV *av;
3560 SV **svp;
3561 I32 i;
3562 SV *tsv = SvRV(sv);
3563 MAGIC *mg;
3564 if (!SvMAGICAL(tsv) || !(mg = mg_find(tsv, '<')))
cea2e8a9 3565 Perl_croak(aTHX_ "panic: del_backref");
810b8aa5
GS
3566 av = (AV *)mg->mg_obj;
3567 svp = AvARRAY(av);
3568 i = AvFILLp(av);
3569 while (i >= 0) {
3570 if (svp[i] == sv) {
3571 svp[i] = &PL_sv_undef; /* XXX */
3572 }
3573 i--;
3574 }
3575}
3576
954c1994
GS
3577/*
3578=for apidoc sv_insert
3579
3580Inserts a string at the specified offset/length within the SV. Similar to
3581the Perl substr() function.
3582
3583=cut
3584*/
3585
79072805 3586void
864dbfa3 3587Perl_sv_insert(pTHX_ SV *bigstr, STRLEN offset, STRLEN len, char *little, STRLEN littlelen)
79072805
LW
3588{
3589 register char *big;
3590 register char *mid;
3591 register char *midend;
3592 register char *bigend;
3593 register I32 i;
6ff81951 3594 STRLEN curlen;
1c846c1f 3595
79072805 3596
8990e307 3597 if (!bigstr)
cea2e8a9 3598 Perl_croak(aTHX_ "Can't modify non-existent substring");
6ff81951 3599 SvPV_force(bigstr, curlen);
60fa28ff 3600 (void)SvPOK_only_UTF8(bigstr);
6ff81951
GS
3601 if (offset + len > curlen) {
3602 SvGROW(bigstr, offset+len+1);
3603 Zero(SvPVX(bigstr)+curlen, offset+len-curlen, char);
3604 SvCUR_set(bigstr, offset+len);
3605 }
79072805 3606
69b47968 3607 SvTAINT(bigstr);
79072805
LW
3608 i = littlelen - len;
3609 if (i > 0) { /* string might grow */
a0d0e21e 3610 big = SvGROW(bigstr, SvCUR(bigstr) + i + 1);
79072805
LW
3611 mid = big + offset + len;
3612 midend = bigend = big + SvCUR(bigstr);
3613 bigend += i;
3614 *bigend = '\0';
3615 while (midend > mid) /* shove everything down */
3616 *--bigend = *--midend;
3617 Move(little,big+offset,littlelen,char);
3618 SvCUR(bigstr) += i;
3619 SvSETMAGIC(bigstr);
3620 return;
3621 }
3622 else if (i == 0) {
463ee0b2 3623 Move(little,SvPVX(bigstr)+offset,len,char);
79072805
LW
3624 SvSETMAGIC(bigstr);
3625 return;
3626 }
3627
463ee0b2 3628 big = SvPVX(bigstr);
79072805
LW
3629 mid = big + offset;
3630 midend = mid + len;
3631 bigend = big + SvCUR(bigstr);
3632
3633 if (midend > bigend)
cea2e8a9 3634 Perl_croak(aTHX_ "panic: sv_insert");
79072805
LW
3635
3636 if (mid - big > bigend - midend) { /* faster to shorten from end */
3637 if (littlelen) {
3638 Move(little, mid, littlelen,char);
3639 mid += littlelen;
3640 }
3641 i = bigend - midend;
3642 if (i > 0) {
3643 Move(midend, mid, i,char);
3644 mid += i;
3645 }
3646 *mid = '\0';
3647 SvCUR_set(bigstr, mid - big);
3648 }
3649 /*SUPPRESS 560*/
155aba94 3650 else if ((i = mid - big)) { /* faster from front */
79072805
LW
3651 midend -= littlelen;
3652 mid = midend;
3653 sv_chop(bigstr,midend-i);
3654 big += i;
3655 while (i--)
3656 *--midend = *--big;
3657 if (littlelen)
3658 Move(little, mid, littlelen,char);
3659 }
3660 else if (littlelen) {
3661 midend -= littlelen;
3662 sv_chop(bigstr,midend);
3663 Move(little,midend,littlelen,char);
3664 }
3665 else {
3666 sv_chop(bigstr,midend);
3667 }
3668 SvSETMAGIC(bigstr);
3669}
3670
c461cf8f
JH
3671/*
3672=for apidoc sv_replace
3673
3674Make the first argument a copy of the second, then delete the original.
3675
3676=cut
3677*/
79072805
LW
3678
3679void
864dbfa3 3680Perl_sv_replace(pTHX_ register SV *sv, register SV *nsv)
79072805
LW
3681{
3682 U32 refcnt = SvREFCNT(sv);
2213622d 3683 SV_CHECK_THINKFIRST(sv);
0453d815
PM
3684 if (SvREFCNT(nsv) != 1 && ckWARN_d(WARN_INTERNAL))
3685 Perl_warner(aTHX_ WARN_INTERNAL, "Reference miscount in sv_replace()");
93a17b20 3686 if (SvMAGICAL(sv)) {
a0d0e21e
LW
3687 if (SvMAGICAL(nsv))
3688 mg_free(nsv);
3689 else
3690 sv_upgrade(nsv, SVt_PVMG);
93a17b20 3691 SvMAGIC(nsv) = SvMAGIC(sv);
a0d0e21e 3692 SvFLAGS(nsv) |= SvMAGICAL(sv);
93a17b20
LW
3693 SvMAGICAL_off(sv);
3694 SvMAGIC(sv) = 0;
3695 }
79072805
LW
3696 SvREFCNT(sv) = 0;
3697 sv_clear(sv);
477f5d66 3698 assert(!SvREFCNT(sv));
79072805
LW
3699 StructCopy(nsv,sv,SV);
3700 SvREFCNT(sv) = refcnt;
1edc1566 3701 SvFLAGS(nsv) |= SVTYPEMASK; /* Mark as freed */
463ee0b2 3702 del_SV(nsv);
79072805
LW
3703}
3704
c461cf8f
JH
3705/*
3706=for apidoc sv_clear
3707
3708Clear an SV, making it empty. Does not free the memory used by the SV
3709itself.
3710
3711=cut
3712*/
3713
79072805 3714void
864dbfa3 3715Perl_sv_clear(pTHX_ register SV *sv)
79072805 3716{
ec12f114 3717 HV* stash;
79072805
LW
3718 assert(sv);
3719 assert(SvREFCNT(sv) == 0);
3720
ed6116ce 3721 if (SvOBJECT(sv)) {
3280af22 3722 if (PL_defstash) { /* Still have a symbol table? */
4e35701f 3723 djSP;
32251b26 3724 CV* destructor;