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