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