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