This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Integrate mainline
[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; \
66 if (PL_debug & 32768) \
67 del_sv(p); \
68 else \
69 plant_SV(p); \
70 UNLOCK_SV_MUTEX; \
71 } STMT_END
a0d0e21e 72
76e3520e 73STATIC void
cea2e8a9 74S_del_sv(pTHX_ SV *p)
463ee0b2 75{
3280af22 76 if (PL_debug & 32768) {
4633a7c4 77 SV* sva;
a0d0e21e
LW
78 SV* sv;
79 SV* svend;
80 int ok = 0;
3280af22 81 for (sva = PL_sv_arenaroot; sva; sva = (SV *) SvANY(sva)) {
4633a7c4
LW
82 sv = sva + 1;
83 svend = &sva[SvREFCNT(sva)];
a0d0e21e
LW
84 if (p >= sv && p < svend)
85 ok = 1;
86 }
87 if (!ok) {
0453d815
PM
88 if (ckWARN_d(WARN_INTERNAL))
89 Perl_warner(aTHX_ WARN_INTERNAL,
1d7c1841
GS
90 "Attempt to free non-arena SV: 0x%"UVxf,
91 PTR2UV(p));
a0d0e21e
LW
92 return;
93 }
94 }
4561caa4 95 plant_SV(p);
463ee0b2 96}
a0d0e21e 97
4561caa4
CS
98#else /* ! DEBUGGING */
99
100#define del_SV(p) plant_SV(p)
101
102#endif /* DEBUGGING */
463ee0b2 103
4633a7c4 104void
864dbfa3 105Perl_sv_add_arena(pTHX_ char *ptr, U32 size, U32 flags)
463ee0b2 106{
4633a7c4 107 SV* sva = (SV*)ptr;
463ee0b2
LW
108 register SV* sv;
109 register SV* svend;
14dd3ad8 110 Zero(ptr, size, char);
4633a7c4
LW
111
112 /* The first SV in an arena isn't an SV. */
3280af22 113 SvANY(sva) = (void *) PL_sv_arenaroot; /* ptr to next arena */
4633a7c4
LW
114 SvREFCNT(sva) = size / sizeof(SV); /* number of SV slots */
115 SvFLAGS(sva) = flags; /* FAKE if not to be freed */
116
3280af22
NIS
117 PL_sv_arenaroot = sva;
118 PL_sv_root = sva + 1;
4633a7c4
LW
119
120 svend = &sva[SvREFCNT(sva) - 1];
121 sv = sva + 1;
463ee0b2 122 while (sv < svend) {
a0d0e21e 123 SvANY(sv) = (void *)(SV*)(sv + 1);
8990e307 124 SvFLAGS(sv) = SVTYPEMASK;
463ee0b2
LW
125 sv++;
126 }
127 SvANY(sv) = 0;
4633a7c4
LW
128 SvFLAGS(sv) = SVTYPEMASK;
129}
130
fba3b22e 131/* sv_mutex must be held while calling more_sv() */
76e3520e 132STATIC SV*
cea2e8a9 133S_more_sv(pTHX)
4633a7c4 134{
4561caa4
CS
135 register SV* sv;
136
3280af22
NIS
137 if (PL_nice_chunk) {
138 sv_add_arena(PL_nice_chunk, PL_nice_chunk_size, 0);
139 PL_nice_chunk = Nullch;
c07a80fd 140 }
1edc1566
PP
141 else {
142 char *chunk; /* must use New here to match call to */
143 New(704,chunk,1008,char); /* Safefree() in sv_free_arenas() */
144 sv_add_arena(chunk, 1008, 0);
145 }
4561caa4
CS
146 uproot_SV(sv);
147 return sv;
463ee0b2
LW
148}
149
76e3520e 150STATIC void
cea2e8a9 151S_visit(pTHX_ SVFUNC_t f)
8990e307 152{
4633a7c4 153 SV* sva;
8990e307
LW
154 SV* sv;
155 register SV* svend;
156
3280af22 157 for (sva = PL_sv_arenaroot; sva; sva = (SV*)SvANY(sva)) {
4633a7c4 158 svend = &sva[SvREFCNT(sva)];
4561caa4
CS
159 for (sv = sva + 1; sv < svend; ++sv) {
160 if (SvTYPE(sv) != SVTYPEMASK)
51371543 161 (FCALL)(aTHXo_ sv);
8990e307
LW
162 }
163 }
164}
165
166void
864dbfa3 167Perl_sv_report_used(pTHX)
4561caa4 168{
0b94c7bb 169 visit(do_report_used);
4561caa4
CS
170}
171
4561caa4 172void
864dbfa3 173Perl_sv_clean_objs(pTHX)
4561caa4 174{
3280af22 175 PL_in_clean_objs = TRUE;
0b94c7bb 176 visit(do_clean_objs);
4561caa4 177#ifndef DISABLE_DESTRUCTOR_KLUDGE
2d0f3c12 178 /* some barnacles may yet remain, clinging to typeglobs */
0b94c7bb 179 visit(do_clean_named_objs);
4561caa4 180#endif
3280af22 181 PL_in_clean_objs = FALSE;
4561caa4
CS
182}
183
8990e307 184void
864dbfa3 185Perl_sv_clean_all(pTHX)
8990e307 186{
3280af22 187 PL_in_clean_all = TRUE;
0b94c7bb 188 visit(do_clean_all);
3280af22 189 PL_in_clean_all = FALSE;
8990e307 190}
463ee0b2 191
4633a7c4 192void
864dbfa3 193Perl_sv_free_arenas(pTHX)
4633a7c4
LW
194{
195 SV* sva;
196 SV* svanext;
612f20c3 197 XPV *arena, *arenanext;
4633a7c4
LW
198
199 /* Free arenas here, but be careful about fake ones. (We assume
200 contiguity of the fake ones with the corresponding real ones.) */
201
3280af22 202 for (sva = PL_sv_arenaroot; sva; sva = svanext) {
4633a7c4
LW
203 svanext = (SV*) SvANY(sva);
204 while (svanext && SvFAKE(svanext))
205 svanext = (SV*) SvANY(svanext);
206
207 if (!SvFAKE(sva))
1edc1566 208 Safefree((void *)sva);
4633a7c4 209 }
5f05dabc 210
612f20c3
GS
211 for (arena = PL_xiv_arenaroot; arena; arena = arenanext) {
212 arenanext = (XPV*)arena->xpv_pv;
213 Safefree(arena);
214 }
215 PL_xiv_arenaroot = 0;
216
217 for (arena = PL_xnv_arenaroot; arena; arena = arenanext) {
218 arenanext = (XPV*)arena->xpv_pv;
219 Safefree(arena);
220 }
221 PL_xnv_arenaroot = 0;
222
223 for (arena = PL_xrv_arenaroot; arena; arena = arenanext) {
224 arenanext = (XPV*)arena->xpv_pv;
225 Safefree(arena);
226 }
227 PL_xrv_arenaroot = 0;
228
229 for (arena = PL_xpv_arenaroot; arena; arena = arenanext) {
230 arenanext = (XPV*)arena->xpv_pv;
231 Safefree(arena);
232 }
233 PL_xpv_arenaroot = 0;
234
235 for (arena = (XPV*)PL_xpviv_arenaroot; arena; arena = arenanext) {
236 arenanext = (XPV*)arena->xpv_pv;
237 Safefree(arena);
238 }
239 PL_xpviv_arenaroot = 0;
240
241 for (arena = (XPV*)PL_xpvnv_arenaroot; arena; arena = arenanext) {
242 arenanext = (XPV*)arena->xpv_pv;
243 Safefree(arena);
244 }
245 PL_xpvnv_arenaroot = 0;
246
247 for (arena = (XPV*)PL_xpvcv_arenaroot; arena; arena = arenanext) {
248 arenanext = (XPV*)arena->xpv_pv;
249 Safefree(arena);
250 }
251 PL_xpvcv_arenaroot = 0;
252
253 for (arena = (XPV*)PL_xpvav_arenaroot; arena; arena = arenanext) {
254 arenanext = (XPV*)arena->xpv_pv;
255 Safefree(arena);
256 }
257 PL_xpvav_arenaroot = 0;
258
259 for (arena = (XPV*)PL_xpvhv_arenaroot; arena; arena = arenanext) {
260 arenanext = (XPV*)arena->xpv_pv;
261 Safefree(arena);
262 }
263 PL_xpvhv_arenaroot = 0;
264
265 for (arena = (XPV*)PL_xpvmg_arenaroot; arena; arena = arenanext) {
266 arenanext = (XPV*)arena->xpv_pv;
267 Safefree(arena);
268 }
269 PL_xpvmg_arenaroot = 0;
270
271 for (arena = (XPV*)PL_xpvlv_arenaroot; arena; arena = arenanext) {
272 arenanext = (XPV*)arena->xpv_pv;
273 Safefree(arena);
274 }
275 PL_xpvlv_arenaroot = 0;
276
277 for (arena = (XPV*)PL_xpvbm_arenaroot; arena; arena = arenanext) {
278 arenanext = (XPV*)arena->xpv_pv;
279 Safefree(arena);
280 }
281 PL_xpvbm_arenaroot = 0;
282
283 for (arena = (XPV*)PL_he_arenaroot; arena; arena = arenanext) {
284 arenanext = (XPV*)arena->xpv_pv;
285 Safefree(arena);
286 }
287 PL_he_arenaroot = 0;
288
3280af22
NIS
289 if (PL_nice_chunk)
290 Safefree(PL_nice_chunk);
291 PL_nice_chunk = Nullch;
292 PL_nice_chunk_size = 0;
293 PL_sv_arenaroot = 0;
294 PL_sv_root = 0;
4633a7c4
LW
295}
296
1d7c1841
GS
297void
298Perl_report_uninit(pTHX)
299{
300 if (PL_op)
301 Perl_warner(aTHX_ WARN_UNINITIALIZED, PL_warn_uninit,
302 " in ", PL_op_desc[PL_op->op_type]);
303 else
304 Perl_warner(aTHX_ WARN_UNINITIALIZED, PL_warn_uninit, "", "");
305}
306
76e3520e 307STATIC XPVIV*
cea2e8a9 308S_new_xiv(pTHX)
463ee0b2 309{
ea7c11a3 310 IV* xiv;
cbe51380
GS
311 LOCK_SV_MUTEX;
312 if (!PL_xiv_root)
313 more_xiv();
314 xiv = PL_xiv_root;
315 /*
316 * See comment in more_xiv() -- RAM.
317 */
318 PL_xiv_root = *(IV**)xiv;
319 UNLOCK_SV_MUTEX;
320 return (XPVIV*)((char*)xiv - STRUCT_OFFSET(XPVIV, xiv_iv));
463ee0b2
LW
321}
322
76e3520e 323STATIC void
cea2e8a9 324S_del_xiv(pTHX_ XPVIV *p)
463ee0b2 325{
23e6a22f 326 IV* xiv = (IV*)((char*)(p) + STRUCT_OFFSET(XPVIV, xiv_iv));
cbe51380 327 LOCK_SV_MUTEX;
3280af22
NIS
328 *(IV**)xiv = PL_xiv_root;
329 PL_xiv_root = xiv;
cbe51380 330 UNLOCK_SV_MUTEX;
463ee0b2
LW
331}
332
cbe51380 333STATIC void
cea2e8a9 334S_more_xiv(pTHX)
463ee0b2 335{
ea7c11a3
SM
336 register IV* xiv;
337 register IV* xivend;
8c52afec
IZ
338 XPV* ptr;
339 New(705, ptr, 1008/sizeof(XPV), XPV);
3280af22
NIS
340 ptr->xpv_pv = (char*)PL_xiv_arenaroot; /* linked list of xiv arenas */
341 PL_xiv_arenaroot = ptr; /* to keep Purify happy */
a0d0e21e 342
ea7c11a3
SM
343 xiv = (IV*) ptr;
344 xivend = &xiv[1008 / sizeof(IV) - 1];
345 xiv += (sizeof(XPV) - 1) / sizeof(IV) + 1; /* fudge by size of XPV */
3280af22 346 PL_xiv_root = xiv;
463ee0b2 347 while (xiv < xivend) {
ea7c11a3 348 *(IV**)xiv = (IV *)(xiv + 1);
463ee0b2
LW
349 xiv++;
350 }
ea7c11a3 351 *(IV**)xiv = 0;
463ee0b2
LW
352}
353
76e3520e 354STATIC XPVNV*
cea2e8a9 355S_new_xnv(pTHX)
463ee0b2 356{
65202027 357 NV* xnv;
cbe51380
GS
358 LOCK_SV_MUTEX;
359 if (!PL_xnv_root)
360 more_xnv();
361 xnv = PL_xnv_root;
65202027 362 PL_xnv_root = *(NV**)xnv;
cbe51380
GS
363 UNLOCK_SV_MUTEX;
364 return (XPVNV*)((char*)xnv - STRUCT_OFFSET(XPVNV, xnv_nv));
463ee0b2
LW
365}
366
76e3520e 367STATIC void
cea2e8a9 368S_del_xnv(pTHX_ XPVNV *p)
463ee0b2 369{
65202027 370 NV* xnv = (NV*)((char*)(p) + STRUCT_OFFSET(XPVNV, xnv_nv));
cbe51380 371 LOCK_SV_MUTEX;
65202027 372 *(NV**)xnv = PL_xnv_root;
3280af22 373 PL_xnv_root = xnv;
cbe51380 374 UNLOCK_SV_MUTEX;
463ee0b2
LW
375}
376
cbe51380 377STATIC void
cea2e8a9 378S_more_xnv(pTHX)
463ee0b2 379{
65202027
DS
380 register NV* xnv;
381 register NV* xnvend;
612f20c3
GS
382 XPV *ptr;
383 New(711, ptr, 1008/sizeof(XPV), XPV);
384 ptr->xpv_pv = (char*)PL_xnv_arenaroot;
385 PL_xnv_arenaroot = ptr;
386
387 xnv = (NV*) ptr;
65202027
DS
388 xnvend = &xnv[1008 / sizeof(NV) - 1];
389 xnv += (sizeof(XPVIV) - 1) / sizeof(NV) + 1; /* fudge by sizeof XPVIV */
3280af22 390 PL_xnv_root = xnv;
463ee0b2 391 while (xnv < xnvend) {
65202027 392 *(NV**)xnv = (NV*)(xnv + 1);
463ee0b2
LW
393 xnv++;
394 }
65202027 395 *(NV**)xnv = 0;
463ee0b2
LW
396}
397
76e3520e 398STATIC XRV*
cea2e8a9 399S_new_xrv(pTHX)
ed6116ce
LW
400{
401 XRV* xrv;
cbe51380
GS
402 LOCK_SV_MUTEX;
403 if (!PL_xrv_root)
404 more_xrv();
405 xrv = PL_xrv_root;
406 PL_xrv_root = (XRV*)xrv->xrv_rv;
407 UNLOCK_SV_MUTEX;
408 return xrv;
ed6116ce
LW
409}
410
76e3520e 411STATIC void
cea2e8a9 412S_del_xrv(pTHX_ XRV *p)
ed6116ce 413{
cbe51380 414 LOCK_SV_MUTEX;
3280af22
NIS
415 p->xrv_rv = (SV*)PL_xrv_root;
416 PL_xrv_root = p;
cbe51380 417 UNLOCK_SV_MUTEX;
ed6116ce
LW
418}
419
cbe51380 420STATIC void
cea2e8a9 421S_more_xrv(pTHX)
ed6116ce 422{
ed6116ce
LW
423 register XRV* xrv;
424 register XRV* xrvend;
612f20c3
GS
425 XPV *ptr;
426 New(712, ptr, 1008/sizeof(XPV), XPV);
427 ptr->xpv_pv = (char*)PL_xrv_arenaroot;
428 PL_xrv_arenaroot = ptr;
429
430 xrv = (XRV*) ptr;
ed6116ce 431 xrvend = &xrv[1008 / sizeof(XRV) - 1];
612f20c3
GS
432 xrv += (sizeof(XPV) - 1) / sizeof(XRV) + 1;
433 PL_xrv_root = xrv;
ed6116ce
LW
434 while (xrv < xrvend) {
435 xrv->xrv_rv = (SV*)(xrv + 1);
436 xrv++;
437 }
438 xrv->xrv_rv = 0;
ed6116ce
LW
439}
440
76e3520e 441STATIC XPV*
cea2e8a9 442S_new_xpv(pTHX)
463ee0b2
LW
443{
444 XPV* xpv;
cbe51380
GS
445 LOCK_SV_MUTEX;
446 if (!PL_xpv_root)
447 more_xpv();
448 xpv = PL_xpv_root;
449 PL_xpv_root = (XPV*)xpv->xpv_pv;
450 UNLOCK_SV_MUTEX;
451 return xpv;
463ee0b2
LW
452}
453
76e3520e 454STATIC void
cea2e8a9 455S_del_xpv(pTHX_ XPV *p)
463ee0b2 456{
cbe51380 457 LOCK_SV_MUTEX;
3280af22
NIS
458 p->xpv_pv = (char*)PL_xpv_root;
459 PL_xpv_root = p;
cbe51380 460 UNLOCK_SV_MUTEX;
463ee0b2
LW
461}
462
cbe51380 463STATIC void
cea2e8a9 464S_more_xpv(pTHX)
463ee0b2 465{
463ee0b2
LW
466 register XPV* xpv;
467 register XPV* xpvend;
612f20c3
GS
468 New(713, xpv, 1008/sizeof(XPV), XPV);
469 xpv->xpv_pv = (char*)PL_xpv_arenaroot;
470 PL_xpv_arenaroot = xpv;
471
463ee0b2 472 xpvend = &xpv[1008 / sizeof(XPV) - 1];
612f20c3 473 PL_xpv_root = ++xpv;
463ee0b2
LW
474 while (xpv < xpvend) {
475 xpv->xpv_pv = (char*)(xpv + 1);
476 xpv++;
477 }
478 xpv->xpv_pv = 0;
463ee0b2
LW
479}
480
932e9ff9
VB
481STATIC XPVIV*
482S_new_xpviv(pTHX)
483{
484 XPVIV* xpviv;
485 LOCK_SV_MUTEX;
486 if (!PL_xpviv_root)
487 more_xpviv();
488 xpviv = PL_xpviv_root;
489 PL_xpviv_root = (XPVIV*)xpviv->xpv_pv;
490 UNLOCK_SV_MUTEX;
491 return xpviv;
492}
493
494STATIC void
495S_del_xpviv(pTHX_ XPVIV *p)
496{
497 LOCK_SV_MUTEX;
498 p->xpv_pv = (char*)PL_xpviv_root;
499 PL_xpviv_root = p;
500 UNLOCK_SV_MUTEX;
501}
502
932e9ff9
VB
503STATIC void
504S_more_xpviv(pTHX)
505{
506 register XPVIV* xpviv;
507 register XPVIV* xpvivend;
612f20c3
GS
508 New(714, xpviv, 1008/sizeof(XPVIV), XPVIV);
509 xpviv->xpv_pv = (char*)PL_xpviv_arenaroot;
510 PL_xpviv_arenaroot = xpviv;
511
932e9ff9 512 xpvivend = &xpviv[1008 / sizeof(XPVIV) - 1];
612f20c3 513 PL_xpviv_root = ++xpviv;
932e9ff9
VB
514 while (xpviv < xpvivend) {
515 xpviv->xpv_pv = (char*)(xpviv + 1);
516 xpviv++;
517 }
518 xpviv->xpv_pv = 0;
519}
520
932e9ff9
VB
521STATIC XPVNV*
522S_new_xpvnv(pTHX)
523{
524 XPVNV* xpvnv;
525 LOCK_SV_MUTEX;
526 if (!PL_xpvnv_root)
527 more_xpvnv();
528 xpvnv = PL_xpvnv_root;
529 PL_xpvnv_root = (XPVNV*)xpvnv->xpv_pv;
530 UNLOCK_SV_MUTEX;
531 return xpvnv;
532}
533
534STATIC void
535S_del_xpvnv(pTHX_ XPVNV *p)
536{
537 LOCK_SV_MUTEX;
538 p->xpv_pv = (char*)PL_xpvnv_root;
539 PL_xpvnv_root = p;
540 UNLOCK_SV_MUTEX;
541}
542
932e9ff9
VB
543STATIC void
544S_more_xpvnv(pTHX)
545{
546 register XPVNV* xpvnv;
547 register XPVNV* xpvnvend;
612f20c3
GS
548 New(715, xpvnv, 1008/sizeof(XPVNV), XPVNV);
549 xpvnv->xpv_pv = (char*)PL_xpvnv_arenaroot;
550 PL_xpvnv_arenaroot = xpvnv;
551
932e9ff9 552 xpvnvend = &xpvnv[1008 / sizeof(XPVNV) - 1];
612f20c3 553 PL_xpvnv_root = ++xpvnv;
932e9ff9
VB
554 while (xpvnv < xpvnvend) {
555 xpvnv->xpv_pv = (char*)(xpvnv + 1);
556 xpvnv++;
557 }
558 xpvnv->xpv_pv = 0;
559}
560
932e9ff9
VB
561STATIC XPVCV*
562S_new_xpvcv(pTHX)
563{
564 XPVCV* xpvcv;
565 LOCK_SV_MUTEX;
566 if (!PL_xpvcv_root)
567 more_xpvcv();
568 xpvcv = PL_xpvcv_root;
569 PL_xpvcv_root = (XPVCV*)xpvcv->xpv_pv;
570 UNLOCK_SV_MUTEX;
571 return xpvcv;
572}
573
574STATIC void
575S_del_xpvcv(pTHX_ XPVCV *p)
576{
577 LOCK_SV_MUTEX;
578 p->xpv_pv = (char*)PL_xpvcv_root;
579 PL_xpvcv_root = p;
580 UNLOCK_SV_MUTEX;
581}
582
932e9ff9
VB
583STATIC void
584S_more_xpvcv(pTHX)
585{
586 register XPVCV* xpvcv;
587 register XPVCV* xpvcvend;
612f20c3
GS
588 New(716, xpvcv, 1008/sizeof(XPVCV), XPVCV);
589 xpvcv->xpv_pv = (char*)PL_xpvcv_arenaroot;
590 PL_xpvcv_arenaroot = xpvcv;
591
932e9ff9 592 xpvcvend = &xpvcv[1008 / sizeof(XPVCV) - 1];
612f20c3 593 PL_xpvcv_root = ++xpvcv;
932e9ff9
VB
594 while (xpvcv < xpvcvend) {
595 xpvcv->xpv_pv = (char*)(xpvcv + 1);
596 xpvcv++;
597 }
598 xpvcv->xpv_pv = 0;
599}
600
932e9ff9
VB
601STATIC XPVAV*
602S_new_xpvav(pTHX)
603{
604 XPVAV* xpvav;
605 LOCK_SV_MUTEX;
606 if (!PL_xpvav_root)
607 more_xpvav();
608 xpvav = PL_xpvav_root;
609 PL_xpvav_root = (XPVAV*)xpvav->xav_array;
610 UNLOCK_SV_MUTEX;
611 return xpvav;
612}
613
614STATIC void
615S_del_xpvav(pTHX_ XPVAV *p)
616{
617 LOCK_SV_MUTEX;
618 p->xav_array = (char*)PL_xpvav_root;
619 PL_xpvav_root = p;
620 UNLOCK_SV_MUTEX;
621}
622
932e9ff9
VB
623STATIC void
624S_more_xpvav(pTHX)
625{
626 register XPVAV* xpvav;
627 register XPVAV* xpvavend;
612f20c3
GS
628 New(717, xpvav, 1008/sizeof(XPVAV), XPVAV);
629 xpvav->xav_array = (char*)PL_xpvav_arenaroot;
630 PL_xpvav_arenaroot = xpvav;
631
932e9ff9 632 xpvavend = &xpvav[1008 / sizeof(XPVAV) - 1];
612f20c3 633 PL_xpvav_root = ++xpvav;
932e9ff9
VB
634 while (xpvav < xpvavend) {
635 xpvav->xav_array = (char*)(xpvav + 1);
636 xpvav++;
637 }
638 xpvav->xav_array = 0;
639}
640
932e9ff9
VB
641STATIC XPVHV*
642S_new_xpvhv(pTHX)
643{
644 XPVHV* xpvhv;
645 LOCK_SV_MUTEX;
646 if (!PL_xpvhv_root)
647 more_xpvhv();
648 xpvhv = PL_xpvhv_root;
649 PL_xpvhv_root = (XPVHV*)xpvhv->xhv_array;
650 UNLOCK_SV_MUTEX;
651 return xpvhv;
652}
653
654STATIC void
655S_del_xpvhv(pTHX_ XPVHV *p)
656{
657 LOCK_SV_MUTEX;
658 p->xhv_array = (char*)PL_xpvhv_root;
659 PL_xpvhv_root = p;
660 UNLOCK_SV_MUTEX;
661}
662
932e9ff9
VB
663STATIC void
664S_more_xpvhv(pTHX)
665{
666 register XPVHV* xpvhv;
667 register XPVHV* xpvhvend;
612f20c3
GS
668 New(718, xpvhv, 1008/sizeof(XPVHV), XPVHV);
669 xpvhv->xhv_array = (char*)PL_xpvhv_arenaroot;
670 PL_xpvhv_arenaroot = xpvhv;
671
932e9ff9 672 xpvhvend = &xpvhv[1008 / sizeof(XPVHV) - 1];
612f20c3 673 PL_xpvhv_root = ++xpvhv;
932e9ff9
VB
674 while (xpvhv < xpvhvend) {
675 xpvhv->xhv_array = (char*)(xpvhv + 1);
676 xpvhv++;
677 }
678 xpvhv->xhv_array = 0;
679}
680
932e9ff9
VB
681STATIC XPVMG*
682S_new_xpvmg(pTHX)
683{
684 XPVMG* xpvmg;
685 LOCK_SV_MUTEX;
686 if (!PL_xpvmg_root)
687 more_xpvmg();
688 xpvmg = PL_xpvmg_root;
689 PL_xpvmg_root = (XPVMG*)xpvmg->xpv_pv;
690 UNLOCK_SV_MUTEX;
691 return xpvmg;
692}
693
694STATIC void
695S_del_xpvmg(pTHX_ XPVMG *p)
696{
697 LOCK_SV_MUTEX;
698 p->xpv_pv = (char*)PL_xpvmg_root;
699 PL_xpvmg_root = p;
700 UNLOCK_SV_MUTEX;
701}
702
932e9ff9
VB
703STATIC void
704S_more_xpvmg(pTHX)
705{
706 register XPVMG* xpvmg;
707 register XPVMG* xpvmgend;
612f20c3
GS
708 New(719, xpvmg, 1008/sizeof(XPVMG), XPVMG);
709 xpvmg->xpv_pv = (char*)PL_xpvmg_arenaroot;
710 PL_xpvmg_arenaroot = xpvmg;
711
932e9ff9 712 xpvmgend = &xpvmg[1008 / sizeof(XPVMG) - 1];
612f20c3 713 PL_xpvmg_root = ++xpvmg;
932e9ff9
VB
714 while (xpvmg < xpvmgend) {
715 xpvmg->xpv_pv = (char*)(xpvmg + 1);
716 xpvmg++;
717 }
718 xpvmg->xpv_pv = 0;
719}
720
932e9ff9
VB
721STATIC XPVLV*
722S_new_xpvlv(pTHX)
723{
724 XPVLV* xpvlv;
725 LOCK_SV_MUTEX;
726 if (!PL_xpvlv_root)
727 more_xpvlv();
728 xpvlv = PL_xpvlv_root;
729 PL_xpvlv_root = (XPVLV*)xpvlv->xpv_pv;
730 UNLOCK_SV_MUTEX;
731 return xpvlv;
732}
733
734STATIC void
735S_del_xpvlv(pTHX_ XPVLV *p)
736{
737 LOCK_SV_MUTEX;
738 p->xpv_pv = (char*)PL_xpvlv_root;
739 PL_xpvlv_root = p;
740 UNLOCK_SV_MUTEX;
741}
742
932e9ff9
VB
743STATIC void
744S_more_xpvlv(pTHX)
745{
746 register XPVLV* xpvlv;
747 register XPVLV* xpvlvend;
612f20c3
GS
748 New(720, xpvlv, 1008/sizeof(XPVLV), XPVLV);
749 xpvlv->xpv_pv = (char*)PL_xpvlv_arenaroot;
750 PL_xpvlv_arenaroot = xpvlv;
751
932e9ff9 752 xpvlvend = &xpvlv[1008 / sizeof(XPVLV) - 1];
612f20c3 753 PL_xpvlv_root = ++xpvlv;
932e9ff9
VB
754 while (xpvlv < xpvlvend) {
755 xpvlv->xpv_pv = (char*)(xpvlv + 1);
756 xpvlv++;
757 }
758 xpvlv->xpv_pv = 0;
759}
760
932e9ff9
VB
761STATIC XPVBM*
762S_new_xpvbm(pTHX)
763{
764 XPVBM* xpvbm;
765 LOCK_SV_MUTEX;
766 if (!PL_xpvbm_root)
767 more_xpvbm();
768 xpvbm = PL_xpvbm_root;
769 PL_xpvbm_root = (XPVBM*)xpvbm->xpv_pv;
770 UNLOCK_SV_MUTEX;
771 return xpvbm;
772}
773
774STATIC void
775S_del_xpvbm(pTHX_ XPVBM *p)
776{
777 LOCK_SV_MUTEX;
778 p->xpv_pv = (char*)PL_xpvbm_root;
779 PL_xpvbm_root = p;
780 UNLOCK_SV_MUTEX;
781}
782
932e9ff9
VB
783STATIC void
784S_more_xpvbm(pTHX)
785{
786 register XPVBM* xpvbm;
787 register XPVBM* xpvbmend;
612f20c3
GS
788 New(721, xpvbm, 1008/sizeof(XPVBM), XPVBM);
789 xpvbm->xpv_pv = (char*)PL_xpvbm_arenaroot;
790 PL_xpvbm_arenaroot = xpvbm;
791
932e9ff9 792 xpvbmend = &xpvbm[1008 / sizeof(XPVBM) - 1];
612f20c3 793 PL_xpvbm_root = ++xpvbm;
932e9ff9
VB
794 while (xpvbm < xpvbmend) {
795 xpvbm->xpv_pv = (char*)(xpvbm + 1);
796 xpvbm++;
797 }
798 xpvbm->xpv_pv = 0;
799}
800
d33b2eba
GS
801#ifdef LEAKTEST
802# define my_safemalloc(s) (void*)safexmalloc(717,s)
803# define my_safefree(p) safexfree((char*)p)
804#else
805# define my_safemalloc(s) (void*)safemalloc(s)
806# define my_safefree(p) safefree((char*)p)
807#endif
463ee0b2 808
d33b2eba 809#ifdef PURIFY
463ee0b2 810
d33b2eba
GS
811#define new_XIV() my_safemalloc(sizeof(XPVIV))
812#define del_XIV(p) my_safefree(p)
ed6116ce 813
d33b2eba
GS
814#define new_XNV() my_safemalloc(sizeof(XPVNV))
815#define del_XNV(p) my_safefree(p)
463ee0b2 816
d33b2eba
GS
817#define new_XRV() my_safemalloc(sizeof(XRV))
818#define del_XRV(p) my_safefree(p)
8c52afec 819
d33b2eba
GS
820#define new_XPV() my_safemalloc(sizeof(XPV))
821#define del_XPV(p) my_safefree(p)
9b94d1dd 822
d33b2eba
GS
823#define new_XPVIV() my_safemalloc(sizeof(XPVIV))
824#define del_XPVIV(p) my_safefree(p)
932e9ff9 825
d33b2eba
GS
826#define new_XPVNV() my_safemalloc(sizeof(XPVNV))
827#define del_XPVNV(p) my_safefree(p)
932e9ff9 828
d33b2eba
GS
829#define new_XPVCV() my_safemalloc(sizeof(XPVCV))
830#define del_XPVCV(p) my_safefree(p)
932e9ff9 831
d33b2eba
GS
832#define new_XPVAV() my_safemalloc(sizeof(XPVAV))
833#define del_XPVAV(p) my_safefree(p)
834
835#define new_XPVHV() my_safemalloc(sizeof(XPVHV))
836#define del_XPVHV(p) my_safefree(p)
1c846c1f 837
d33b2eba
GS
838#define new_XPVMG() my_safemalloc(sizeof(XPVMG))
839#define del_XPVMG(p) my_safefree(p)
840
841#define new_XPVLV() my_safemalloc(sizeof(XPVLV))
842#define del_XPVLV(p) my_safefree(p)
843
844#define new_XPVBM() my_safemalloc(sizeof(XPVBM))
845#define del_XPVBM(p) my_safefree(p)
846
847#else /* !PURIFY */
848
849#define new_XIV() (void*)new_xiv()
850#define del_XIV(p) del_xiv((XPVIV*) p)
851
852#define new_XNV() (void*)new_xnv()
853#define del_XNV(p) del_xnv((XPVNV*) p)
9b94d1dd 854
d33b2eba
GS
855#define new_XRV() (void*)new_xrv()
856#define del_XRV(p) del_xrv((XRV*) p)
9b94d1dd 857
d33b2eba
GS
858#define new_XPV() (void*)new_xpv()
859#define del_XPV(p) del_xpv((XPV *)p)
860
861#define new_XPVIV() (void*)new_xpviv()
862#define del_XPVIV(p) del_xpviv((XPVIV *)p)
863
864#define new_XPVNV() (void*)new_xpvnv()
865#define del_XPVNV(p) del_xpvnv((XPVNV *)p)
866
867#define new_XPVCV() (void*)new_xpvcv()
868#define del_XPVCV(p) del_xpvcv((XPVCV *)p)
869
870#define new_XPVAV() (void*)new_xpvav()
871#define del_XPVAV(p) del_xpvav((XPVAV *)p)
872
873#define new_XPVHV() (void*)new_xpvhv()
874#define del_XPVHV(p) del_xpvhv((XPVHV *)p)
1c846c1f 875
d33b2eba
GS
876#define new_XPVMG() (void*)new_xpvmg()
877#define del_XPVMG(p) del_xpvmg((XPVMG *)p)
878
879#define new_XPVLV() (void*)new_xpvlv()
880#define del_XPVLV(p) del_xpvlv((XPVLV *)p)
881
882#define new_XPVBM() (void*)new_xpvbm()
883#define del_XPVBM(p) del_xpvbm((XPVBM *)p)
884
885#endif /* PURIFY */
9b94d1dd 886
d33b2eba
GS
887#define new_XPVGV() my_safemalloc(sizeof(XPVGV))
888#define del_XPVGV(p) my_safefree(p)
1c846c1f 889
d33b2eba
GS
890#define new_XPVFM() my_safemalloc(sizeof(XPVFM))
891#define del_XPVFM(p) my_safefree(p)
1c846c1f 892
d33b2eba
GS
893#define new_XPVIO() my_safemalloc(sizeof(XPVIO))
894#define del_XPVIO(p) my_safefree(p)
8990e307 895
954c1994
GS
896/*
897=for apidoc sv_upgrade
898
899Upgrade an SV to a more complex form. Use C<SvUPGRADE>. See
900C<svtype>.
901
902=cut
903*/
904
79072805 905bool
864dbfa3 906Perl_sv_upgrade(pTHX_ register SV *sv, U32 mt)
79072805
LW
907{
908 char* pv;
909 U32 cur;
910 U32 len;
a0d0e21e 911 IV iv;
65202027 912 NV nv;
79072805
LW
913 MAGIC* magic;
914 HV* stash;
915
f130fd45
NIS
916 if (mt != SVt_PV && SvREADONLY(sv) && SvFAKE(sv)) {
917 sv_force_normal(sv);
918 }
919
79072805
LW
920 if (SvTYPE(sv) == mt)
921 return TRUE;
922
a5f75d66
AD
923 if (mt < SVt_PVIV)
924 (void)SvOOK_off(sv);
925
79072805
LW
926 switch (SvTYPE(sv)) {
927 case SVt_NULL:
928 pv = 0;
929 cur = 0;
930 len = 0;
931 iv = 0;
932 nv = 0.0;
933 magic = 0;
934 stash = 0;
935 break;
79072805
LW
936 case SVt_IV:
937 pv = 0;
938 cur = 0;
939 len = 0;
463ee0b2 940 iv = SvIVX(sv);
65202027 941 nv = (NV)SvIVX(sv);
79072805
LW
942 del_XIV(SvANY(sv));
943 magic = 0;
944 stash = 0;
ed6116ce 945 if (mt == SVt_NV)
463ee0b2 946 mt = SVt_PVNV;
ed6116ce
LW
947 else if (mt < SVt_PVIV)
948 mt = SVt_PVIV;
79072805
LW
949 break;
950 case SVt_NV:
951 pv = 0;
952 cur = 0;
953 len = 0;
463ee0b2 954 nv = SvNVX(sv);
1bd302c3 955 iv = I_V(nv);
79072805
LW
956 magic = 0;
957 stash = 0;
958 del_XNV(SvANY(sv));
959 SvANY(sv) = 0;
ed6116ce 960 if (mt < SVt_PVNV)
79072805
LW
961 mt = SVt_PVNV;
962 break;
ed6116ce
LW
963 case SVt_RV:
964 pv = (char*)SvRV(sv);
965 cur = 0;
966 len = 0;
56431972
RB
967 iv = PTR2IV(pv);
968 nv = PTR2NV(pv);
ed6116ce
LW
969 del_XRV(SvANY(sv));
970 magic = 0;
971 stash = 0;
972 break;
79072805 973 case SVt_PV:
463ee0b2 974 pv = SvPVX(sv);
79072805
LW
975 cur = SvCUR(sv);
976 len = SvLEN(sv);
977 iv = 0;
978 nv = 0.0;
979 magic = 0;
980 stash = 0;
981 del_XPV(SvANY(sv));
748a9306
LW
982 if (mt <= SVt_IV)
983 mt = SVt_PVIV;
984 else if (mt == SVt_NV)
985 mt = SVt_PVNV;
79072805
LW
986 break;
987 case SVt_PVIV:
463ee0b2 988 pv = SvPVX(sv);
79072805
LW
989 cur = SvCUR(sv);
990 len = SvLEN(sv);
463ee0b2 991 iv = SvIVX(sv);
79072805
LW
992 nv = 0.0;
993 magic = 0;
994 stash = 0;
995 del_XPVIV(SvANY(sv));
996 break;
997 case SVt_PVNV:
463ee0b2 998 pv = SvPVX(sv);
79072805
LW
999 cur = SvCUR(sv);
1000 len = SvLEN(sv);
463ee0b2
LW
1001 iv = SvIVX(sv);
1002 nv = SvNVX(sv);
79072805
LW
1003 magic = 0;
1004 stash = 0;
1005 del_XPVNV(SvANY(sv));
1006 break;
1007 case SVt_PVMG:
463ee0b2 1008 pv = SvPVX(sv);
79072805
LW
1009 cur = SvCUR(sv);
1010 len = SvLEN(sv);
463ee0b2
LW
1011 iv = SvIVX(sv);
1012 nv = SvNVX(sv);
79072805
LW
1013 magic = SvMAGIC(sv);
1014 stash = SvSTASH(sv);
1015 del_XPVMG(SvANY(sv));
1016 break;
1017 default:
cea2e8a9 1018 Perl_croak(aTHX_ "Can't upgrade that kind of scalar");
79072805
LW
1019 }
1020
1021 switch (mt) {
1022 case SVt_NULL:
cea2e8a9 1023 Perl_croak(aTHX_ "Can't upgrade to undef");
79072805
LW
1024 case SVt_IV:
1025 SvANY(sv) = new_XIV();
463ee0b2 1026 SvIVX(sv) = iv;
79072805
LW
1027 break;
1028 case SVt_NV:
1029 SvANY(sv) = new_XNV();
463ee0b2 1030 SvNVX(sv) = nv;
79072805 1031 break;
ed6116ce
LW
1032 case SVt_RV:
1033 SvANY(sv) = new_XRV();
1034 SvRV(sv) = (SV*)pv;
ed6116ce 1035 break;
79072805
LW
1036 case SVt_PV:
1037 SvANY(sv) = new_XPV();
463ee0b2 1038 SvPVX(sv) = pv;
79072805
LW
1039 SvCUR(sv) = cur;
1040 SvLEN(sv) = len;
1041 break;
1042 case SVt_PVIV:
1043 SvANY(sv) = new_XPVIV();
463ee0b2 1044 SvPVX(sv) = pv;
79072805
LW
1045 SvCUR(sv) = cur;
1046 SvLEN(sv) = len;
463ee0b2 1047 SvIVX(sv) = iv;
79072805 1048 if (SvNIOK(sv))
a0d0e21e 1049 (void)SvIOK_on(sv);
79072805
LW
1050 SvNOK_off(sv);
1051 break;
1052 case SVt_PVNV:
1053 SvANY(sv) = new_XPVNV();
463ee0b2 1054 SvPVX(sv) = pv;
79072805
LW
1055 SvCUR(sv) = cur;
1056 SvLEN(sv) = len;
463ee0b2
LW
1057 SvIVX(sv) = iv;
1058 SvNVX(sv) = nv;
79072805
LW
1059 break;
1060 case SVt_PVMG:
1061 SvANY(sv) = new_XPVMG();
463ee0b2 1062 SvPVX(sv) = pv;
79072805
LW
1063 SvCUR(sv) = cur;
1064 SvLEN(sv) = len;
463ee0b2
LW
1065 SvIVX(sv) = iv;
1066 SvNVX(sv) = nv;
79072805
LW
1067 SvMAGIC(sv) = magic;
1068 SvSTASH(sv) = stash;
1069 break;
1070 case SVt_PVLV:
1071 SvANY(sv) = new_XPVLV();
463ee0b2 1072 SvPVX(sv) = pv;
79072805
LW
1073 SvCUR(sv) = cur;
1074 SvLEN(sv) = len;
463ee0b2
LW
1075 SvIVX(sv) = iv;
1076 SvNVX(sv) = nv;
79072805
LW
1077 SvMAGIC(sv) = magic;
1078 SvSTASH(sv) = stash;
1079 LvTARGOFF(sv) = 0;
1080 LvTARGLEN(sv) = 0;
1081 LvTARG(sv) = 0;
1082 LvTYPE(sv) = 0;
1083 break;
1084 case SVt_PVAV:
1085 SvANY(sv) = new_XPVAV();
463ee0b2
LW
1086 if (pv)
1087 Safefree(pv);
2304df62 1088 SvPVX(sv) = 0;
d1bf51dd 1089 AvMAX(sv) = -1;
93965878 1090 AvFILLp(sv) = -1;
463ee0b2
LW
1091 SvIVX(sv) = 0;
1092 SvNVX(sv) = 0.0;
1093 SvMAGIC(sv) = magic;
1094 SvSTASH(sv) = stash;
1095 AvALLOC(sv) = 0;
79072805
LW
1096 AvARYLEN(sv) = 0;
1097 AvFLAGS(sv) = 0;
1098 break;
1099 case SVt_PVHV:
1100 SvANY(sv) = new_XPVHV();
463ee0b2
LW
1101 if (pv)
1102 Safefree(pv);
1103 SvPVX(sv) = 0;
1104 HvFILL(sv) = 0;
1105 HvMAX(sv) = 0;
1106 HvKEYS(sv) = 0;
1107 SvNVX(sv) = 0.0;
79072805
LW
1108 SvMAGIC(sv) = magic;
1109 SvSTASH(sv) = stash;
79072805
LW
1110 HvRITER(sv) = 0;
1111 HvEITER(sv) = 0;
1112 HvPMROOT(sv) = 0;
1113 HvNAME(sv) = 0;
79072805
LW
1114 break;
1115 case SVt_PVCV:
1116 SvANY(sv) = new_XPVCV();
748a9306 1117 Zero(SvANY(sv), 1, XPVCV);
463ee0b2 1118 SvPVX(sv) = pv;
79072805
LW
1119 SvCUR(sv) = cur;
1120 SvLEN(sv) = len;
463ee0b2
LW
1121 SvIVX(sv) = iv;
1122 SvNVX(sv) = nv;
79072805
LW
1123 SvMAGIC(sv) = magic;
1124 SvSTASH(sv) = stash;
79072805
LW
1125 break;
1126 case SVt_PVGV:
1127 SvANY(sv) = new_XPVGV();
463ee0b2 1128 SvPVX(sv) = pv;
79072805
LW
1129 SvCUR(sv) = cur;
1130 SvLEN(sv) = len;
463ee0b2
LW
1131 SvIVX(sv) = iv;
1132 SvNVX(sv) = nv;
79072805
LW
1133 SvMAGIC(sv) = magic;
1134 SvSTASH(sv) = stash;
93a17b20 1135 GvGP(sv) = 0;
79072805
LW
1136 GvNAME(sv) = 0;
1137 GvNAMELEN(sv) = 0;
1138 GvSTASH(sv) = 0;
a5f75d66 1139 GvFLAGS(sv) = 0;
79072805
LW
1140 break;
1141 case SVt_PVBM:
1142 SvANY(sv) = new_XPVBM();
463ee0b2 1143 SvPVX(sv) = pv;
79072805
LW
1144 SvCUR(sv) = cur;
1145 SvLEN(sv) = len;
463ee0b2
LW
1146 SvIVX(sv) = iv;
1147 SvNVX(sv) = nv;
79072805
LW
1148 SvMAGIC(sv) = magic;
1149 SvSTASH(sv) = stash;
1150 BmRARE(sv) = 0;
1151 BmUSEFUL(sv) = 0;
1152 BmPREVIOUS(sv) = 0;
1153 break;
1154 case SVt_PVFM:
1155 SvANY(sv) = new_XPVFM();
748a9306 1156 Zero(SvANY(sv), 1, XPVFM);
463ee0b2 1157 SvPVX(sv) = pv;
79072805
LW
1158 SvCUR(sv) = cur;
1159 SvLEN(sv) = len;
463ee0b2
LW
1160 SvIVX(sv) = iv;
1161 SvNVX(sv) = nv;
79072805
LW
1162 SvMAGIC(sv) = magic;
1163 SvSTASH(sv) = stash;
79072805 1164 break;
8990e307
LW
1165 case SVt_PVIO:
1166 SvANY(sv) = new_XPVIO();
748a9306 1167 Zero(SvANY(sv), 1, XPVIO);
8990e307
LW
1168 SvPVX(sv) = pv;
1169 SvCUR(sv) = cur;
1170 SvLEN(sv) = len;
1171 SvIVX(sv) = iv;
1172 SvNVX(sv) = nv;
1173 SvMAGIC(sv) = magic;
1174 SvSTASH(sv) = stash;
85e6fe83 1175 IoPAGE_LEN(sv) = 60;
8990e307
LW
1176 break;
1177 }
1178 SvFLAGS(sv) &= ~SVTYPEMASK;
1179 SvFLAGS(sv) |= mt;
79072805
LW
1180 return TRUE;
1181}
1182
79072805 1183int
864dbfa3 1184Perl_sv_backoff(pTHX_ register SV *sv)
79072805
LW
1185{
1186 assert(SvOOK(sv));
463ee0b2
LW
1187 if (SvIVX(sv)) {
1188 char *s = SvPVX(sv);
1189 SvLEN(sv) += SvIVX(sv);
1190 SvPVX(sv) -= SvIVX(sv);
79072805 1191 SvIV_set(sv, 0);
463ee0b2 1192 Move(s, SvPVX(sv), SvCUR(sv)+1, char);
79072805
LW
1193 }
1194 SvFLAGS(sv) &= ~SVf_OOK;
a0d0e21e 1195 return 0;
79072805
LW
1196}
1197
954c1994
GS
1198/*
1199=for apidoc sv_grow
1200
1201Expands the character buffer in the SV. This will use C<sv_unref> and will
1202upgrade the SV to C<SVt_PV>. Returns a pointer to the character buffer.
1203Use C<SvGROW>.
1204
1205=cut
1206*/
1207
79072805 1208char *
864dbfa3 1209Perl_sv_grow(pTHX_ register SV *sv, register STRLEN newlen)
79072805
LW
1210{
1211 register char *s;
1212
55497cff 1213#ifdef HAS_64K_LIMIT
79072805 1214 if (newlen >= 0x10000) {
1d7c1841
GS
1215 PerlIO_printf(Perl_debug_log,
1216 "Allocation too large: %"UVxf"\n", (UV)newlen);
79072805
LW
1217 my_exit(1);
1218 }
55497cff 1219#endif /* HAS_64K_LIMIT */
a0d0e21e
LW
1220 if (SvROK(sv))
1221 sv_unref(sv);
79072805
LW
1222 if (SvTYPE(sv) < SVt_PV) {
1223 sv_upgrade(sv, SVt_PV);
463ee0b2 1224 s = SvPVX(sv);
79072805
LW
1225 }
1226 else if (SvOOK(sv)) { /* pv is offset? */
1227 sv_backoff(sv);
463ee0b2 1228 s = SvPVX(sv);
79072805
LW
1229 if (newlen > SvLEN(sv))
1230 newlen += 10 * (newlen - SvCUR(sv)); /* avoid copy each time */
c6f8c383
GA
1231#ifdef HAS_64K_LIMIT
1232 if (newlen >= 0x10000)
1233 newlen = 0xFFFF;
1234#endif
79072805
LW
1235 }
1236 else
463ee0b2 1237 s = SvPVX(sv);
79072805 1238 if (newlen > SvLEN(sv)) { /* need more room? */
8d6dde3e 1239 if (SvLEN(sv) && s) {
f5a32c7f 1240#if defined(MYMALLOC) && !defined(LEAKTEST)
8d6dde3e
IZ
1241 STRLEN l = malloced_size((void*)SvPVX(sv));
1242 if (newlen <= l) {
1243 SvLEN_set(sv, l);
1244 return s;
1245 } else
c70c8a0a 1246#endif
79072805 1247 Renew(s,newlen,char);
8d6dde3e 1248 }
79072805
LW
1249 else
1250 New(703,s,newlen,char);
1251 SvPV_set(sv, s);
1252 SvLEN_set(sv, newlen);
1253 }
1254 return s;
1255}
1256
954c1994
GS
1257/*
1258=for apidoc sv_setiv
1259
1260Copies an integer into the given SV. Does not handle 'set' magic. See
1261C<sv_setiv_mg>.
1262
1263=cut
1264*/
1265
79072805 1266void
864dbfa3 1267Perl_sv_setiv(pTHX_ register SV *sv, IV i)
79072805 1268{
2213622d 1269 SV_CHECK_THINKFIRST(sv);
463ee0b2
LW
1270 switch (SvTYPE(sv)) {
1271 case SVt_NULL:
79072805 1272 sv_upgrade(sv, SVt_IV);
463ee0b2
LW
1273 break;
1274 case SVt_NV:
1275 sv_upgrade(sv, SVt_PVNV);
1276 break;
ed6116ce 1277 case SVt_RV:
463ee0b2 1278 case SVt_PV:
79072805 1279 sv_upgrade(sv, SVt_PVIV);
463ee0b2 1280 break;
a0d0e21e
LW
1281
1282 case SVt_PVGV:
a0d0e21e
LW
1283 case SVt_PVAV:
1284 case SVt_PVHV:
1285 case SVt_PVCV:
1286 case SVt_PVFM:
1287 case SVt_PVIO:
411caa50
JH
1288 Perl_croak(aTHX_ "Can't coerce %s to integer in %s", sv_reftype(sv,0),
1289 PL_op_desc[PL_op->op_type]);
463ee0b2 1290 }
a0d0e21e 1291 (void)SvIOK_only(sv); /* validate number */
a5f75d66 1292 SvIVX(sv) = i;
463ee0b2 1293 SvTAINT(sv);
79072805
LW
1294}
1295
954c1994
GS
1296/*
1297=for apidoc sv_setiv_mg
1298
1299Like C<sv_setiv>, but also handles 'set' magic.
1300
1301=cut
1302*/
1303
79072805 1304void
864dbfa3 1305Perl_sv_setiv_mg(pTHX_ register SV *sv, IV i)
ef50df4b
GS
1306{
1307 sv_setiv(sv,i);
1308 SvSETMAGIC(sv);
1309}
1310
954c1994
GS
1311/*
1312=for apidoc sv_setuv
1313
1314Copies an unsigned integer into the given SV. Does not handle 'set' magic.
1315See C<sv_setuv_mg>.
1316
1317=cut
1318*/
1319
ef50df4b 1320void
864dbfa3 1321Perl_sv_setuv(pTHX_ register SV *sv, UV u)
55497cff 1322{
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{
2887 return sv_2pv(sv,lp);
2888}
2889
2890char *
2891Perl_sv_2pvutf8_nolen(pTHX_ register SV *sv)
2892{
560a288e
GS
2893 STRLEN n_a;
2894 return sv_2pvutf8(sv, &n_a);
7340a771
GS
2895}
2896
2897char *
2898Perl_sv_2pvutf8(pTHX_ register SV *sv, STRLEN *lp)
2899{
560a288e 2900 sv_utf8_upgrade(sv);
7d59b7e4 2901 return SvPV(sv,*lp);
7340a771 2902}
1c846c1f 2903
463ee0b2
LW
2904/* This function is only called on magical items */
2905bool
864dbfa3 2906Perl_sv_2bool(pTHX_ register SV *sv)
463ee0b2 2907{
8990e307 2908 if (SvGMAGICAL(sv))
463ee0b2
LW
2909 mg_get(sv);
2910
a0d0e21e
LW
2911 if (!SvOK(sv))
2912 return 0;
2913 if (SvROK(sv)) {
a0d0e21e 2914 SV* tmpsv;
1554e226
DC
2915 if (SvAMAGIC(sv) && (tmpsv=AMG_CALLun(sv,bool_)) &&
2916 (SvRV(tmpsv) != SvRV(sv)))
9e7bc3e8 2917 return SvTRUE(tmpsv);
a0d0e21e
LW
2918 return SvRV(sv) != 0;
2919 }
463ee0b2 2920 if (SvPOKp(sv)) {
11343788
MB
2921 register XPV* Xpvtmp;
2922 if ((Xpvtmp = (XPV*)SvANY(sv)) &&
2923 (*Xpvtmp->xpv_pv > '0' ||
2924 Xpvtmp->xpv_cur > 1 ||
2925 (Xpvtmp->xpv_cur && *Xpvtmp->xpv_pv != '0')))
463ee0b2
LW
2926 return 1;
2927 else
2928 return 0;
2929 }
2930 else {
2931 if (SvIOKp(sv))
2932 return SvIVX(sv) != 0;
2933 else {
2934 if (SvNOKp(sv))
2935 return SvNVX(sv) != 0.0;
2936 else
2937 return FALSE;
2938 }
2939 }
79072805
LW
2940}
2941
c461cf8f
JH
2942/*
2943=for apidoc sv_utf8_upgrade
2944
2945Convert the PV of an SV to its UTF8-encoded form.
2946
2947=cut
2948*/
2949
560a288e
GS
2950void
2951Perl_sv_utf8_upgrade(pTHX_ register SV *sv)
2952{
511c2ff0
NIS
2953 char *s, *t, *e;
2954 int hibit = 0;
560a288e 2955
0c981600 2956 if (!sv || !SvPOK(sv) || SvUTF8(sv))
560a288e
GS
2957 return;
2958
40826f67
JH
2959 /* This function could be much more efficient if we had a FLAG in SVs
2960 * to signal if there are any hibit chars in the PV.
511c2ff0 2961 * Given that there isn't make loop fast as possible
560a288e 2962 */
511c2ff0
NIS
2963 s = SvPVX(sv);
2964 e = SvEND(sv);
2965 t = s;
2966 while (t < e) {
fd400ab9 2967 if ((hibit = UTF8_IS_CONTINUED(*t++)))
8a818333 2968 break;
8a818333 2969 }
560a288e 2970
40826f67 2971 if (hibit) {
8a818333 2972 STRLEN len;
652088fc 2973
8a818333 2974 if (SvREADONLY(sv) && SvFAKE(sv)) {
8a818333
NIS
2975 sv_force_normal(sv);
2976 s = SvPVX(sv);
2977 }
2978 len = SvCUR(sv) + 1; /* Plus the \0 */
00df9076 2979 SvPVX(sv) = (char*)bytes_to_utf8((U8*)s, &len);
841d7a39 2980 SvCUR(sv) = len - 1;
511c2ff0
NIS
2981 if (SvLEN(sv) != 0)
2982 Safefree(s); /* No longer using what was there before. */
841d7a39 2983 SvLEN(sv) = len; /* No longer know the real size. */
560a288e
GS
2984 SvUTF8_on(sv);
2985 }
2986}
2987
c461cf8f
JH
2988/*
2989=for apidoc sv_utf8_downgrade
2990
2991Attempt to convert the PV of an SV from UTF8-encoded to byte encoding.
2992This may not be possible if the PV contains non-byte encoding characters;
2993if this is the case, either returns false or, if C<fail_ok> is not
2994true, croaks.
2995
2996=cut
2997*/
2998
560a288e
GS
2999bool
3000Perl_sv_utf8_downgrade(pTHX_ register SV* sv, bool fail_ok)
3001{
3002 if (SvPOK(sv) && SvUTF8(sv)) {
fa301091 3003 if (SvCUR(sv)) {
652088fc
JH
3004 char *s;
3005 STRLEN len;
fa301091 3006
652088fc
JH
3007 if (SvREADONLY(sv) && SvFAKE(sv))
3008 sv_force_normal(sv);
3009 s = SvPV(sv, len);
3010 if (!utf8_to_bytes((U8*)s, &len)) {
fa301091
JH
3011 if (fail_ok)
3012 return FALSE;
3013 else {
3014 if (PL_op)
3015 Perl_croak(aTHX_ "Wide character in %s",
3016 PL_op_desc[PL_op->op_type]);
3017 else
3018 Perl_croak(aTHX_ "Wide character");
3019 }
4b3603a4 3020 }
fa301091 3021 SvCUR(sv) = len;
67e989fb 3022 }
9f9ab905 3023 SvUTF8_off(sv);
560a288e 3024 }
fa301091 3025
560a288e
GS
3026 return TRUE;
3027}
3028
c461cf8f
JH
3029/*
3030=for apidoc sv_utf8_encode
3031
3032Convert the PV of an SV to UTF8-encoded, but then turn off the C<SvUTF8>
1c846c1f 3033flag so that it looks like bytes again. Nothing calls this.
c461cf8f
JH
3034
3035=cut
3036*/
3037
560a288e
GS
3038void
3039Perl_sv_utf8_encode(pTHX_ register SV *sv)
3040{
3041 sv_utf8_upgrade(sv);
3042 SvUTF8_off(sv);
3043}
3044
3045bool
3046Perl_sv_utf8_decode(pTHX_ register SV *sv)
3047{
3048 if (SvPOK(sv)) {
3049 char *c;
511c2ff0 3050 char *e;
9cbac4c7 3051
560a288e
GS
3052 if (!sv_utf8_downgrade(sv, TRUE))
3053 return FALSE;
3054
3055 /* it is actually just a matter of turning the utf8 flag on, but
3056 * we want to make sure everything inside is valid utf8 first.
3057 */
3058 c = SvPVX(sv);
00df9076 3059 if (!is_utf8_string((U8*)c, SvCUR(sv)+1))
67e989fb 3060 return FALSE;
511c2ff0
NIS
3061 e = SvEND(sv);
3062 while (c < e) {
fd400ab9 3063 if (UTF8_IS_CONTINUED(*c++)) {
67e989fb
JH
3064 SvUTF8_on(sv);
3065 break;
3066 }
560a288e 3067 }
560a288e
GS
3068 }
3069 return TRUE;
3070}
3071
3072
79072805 3073/* Note: sv_setsv() should not be called with a source string that needs
463ee0b2 3074 * to be reused, since it may destroy the source string if it is marked
79072805
LW
3075 * as temporary.
3076 */
3077
954c1994
GS
3078/*
3079=for apidoc sv_setsv
3080
3081Copies the contents of the source SV C<ssv> into the destination SV C<dsv>.
3082The source SV may be destroyed if it is mortal. Does not handle 'set'
3083magic. See the macro forms C<SvSetSV>, C<SvSetSV_nosteal> and
3084C<sv_setsv_mg>.
3085
3086=cut
3087*/
3088
79072805 3089void
864dbfa3 3090Perl_sv_setsv(pTHX_ SV *dstr, register SV *sstr)
79072805 3091{
8990e307
LW
3092 register U32 sflags;
3093 register int dtype;
3094 register int stype;
463ee0b2 3095
79072805
LW
3096 if (sstr == dstr)
3097 return;
2213622d 3098 SV_CHECK_THINKFIRST(dstr);
79072805 3099 if (!sstr)
3280af22 3100 sstr = &PL_sv_undef;
8990e307
LW
3101 stype = SvTYPE(sstr);
3102 dtype = SvTYPE(dstr);
79072805 3103
a0d0e21e 3104 SvAMAGIC_off(dstr);
9e7bc3e8 3105
463ee0b2 3106 /* There's a lot of redundancy below but we're going for speed here */
79072805 3107
8990e307 3108 switch (stype) {
79072805 3109 case SVt_NULL:
aece5585 3110 undef_sstr:
20408e3c
GS
3111 if (dtype != SVt_PVGV) {
3112 (void)SvOK_off(dstr);
3113 return;
3114 }
3115 break;
463ee0b2 3116 case SVt_IV:
aece5585
GA
3117 if (SvIOK(sstr)) {
3118 switch (dtype) {
3119 case SVt_NULL:
8990e307 3120 sv_upgrade(dstr, SVt_IV);
aece5585
GA
3121 break;
3122 case SVt_NV:
8990e307 3123 sv_upgrade(dstr, SVt_PVNV);
aece5585
GA
3124 break;
3125 case SVt_RV:
3126 case SVt_PV:
a0d0e21e 3127 sv_upgrade(dstr, SVt_PVIV);
aece5585
GA
3128 break;
3129 }
3130 (void)SvIOK_only(dstr);
3131 SvIVX(dstr) = SvIVX(sstr);
25da4f38
IZ
3132 if (SvIsUV(sstr))
3133 SvIsUV_on(dstr);
27c9684d
AP
3134 if (SvTAINTED(sstr))
3135 SvTAINT(dstr);
aece5585 3136 return;
8990e307 3137 }
aece5585
GA
3138 goto undef_sstr;
3139
463ee0b2 3140 case SVt_NV:
aece5585
GA
3141 if (SvNOK(sstr)) {
3142 switch (dtype) {
3143 case SVt_NULL:
3144 case SVt_IV:
8990e307 3145 sv_upgrade(dstr, SVt_NV);
aece5585
GA
3146 break;
3147 case SVt_RV:
3148 case SVt_PV:
3149 case SVt_PVIV:
a0d0e21e 3150 sv_upgrade(dstr, SVt_PVNV);
aece5585
GA
3151 break;
3152 }
3153 SvNVX(dstr) = SvNVX(sstr);
3154 (void)SvNOK_only(dstr);
27c9684d
AP
3155 if (SvTAINTED(sstr))
3156 SvTAINT(dstr);
aece5585 3157 return;
8990e307 3158 }
aece5585
GA
3159 goto undef_sstr;
3160
ed6116ce 3161 case SVt_RV:
8990e307 3162 if (dtype < SVt_RV)
ed6116ce 3163 sv_upgrade(dstr, SVt_RV);
c07a80fd
PP
3164 else if (dtype == SVt_PVGV &&
3165 SvTYPE(SvRV(sstr)) == SVt_PVGV) {
3166 sstr = SvRV(sstr);
a5f75d66 3167 if (sstr == dstr) {
1d7c1841
GS
3168 if (GvIMPORTED(dstr) != GVf_IMPORTED
3169 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3170 {
a5f75d66 3171 GvIMPORTED_on(dstr);
1d7c1841 3172 }
a5f75d66
AD
3173 GvMULTI_on(dstr);
3174 return;
3175 }
c07a80fd
PP
3176 goto glob_assign;
3177 }
ed6116ce 3178 break;
463ee0b2 3179 case SVt_PV:
fc36a67e 3180 case SVt_PVFM:
8990e307 3181 if (dtype < SVt_PV)
463ee0b2 3182 sv_upgrade(dstr, SVt_PV);
463ee0b2
LW
3183 break;
3184 case SVt_PVIV:
8990e307 3185 if (dtype < SVt_PVIV)
463ee0b2 3186 sv_upgrade(dstr, SVt_PVIV);
463ee0b2
LW
3187 break;
3188 case SVt_PVNV:
8990e307 3189 if (dtype < SVt_PVNV)
463ee0b2 3190 sv_upgrade(dstr, SVt_PVNV);
463ee0b2 3191 break;
4633a7c4
LW
3192 case SVt_PVAV:
3193 case SVt_PVHV:
3194 case SVt_PVCV:
4633a7c4 3195 case SVt_PVIO:
533c011a 3196 if (PL_op)
cea2e8a9 3197 Perl_croak(aTHX_ "Bizarre copy of %s in %s", sv_reftype(sstr, 0),
22c35a8c 3198 PL_op_name[PL_op->op_type]);
4633a7c4 3199 else
cea2e8a9 3200 Perl_croak(aTHX_ "Bizarre copy of %s", sv_reftype(sstr, 0));
4633a7c4
LW
3201 break;
3202
79072805 3203 case SVt_PVGV:
8990e307 3204 if (dtype <= SVt_PVGV) {
c07a80fd 3205 glob_assign:
a5f75d66 3206 if (dtype != SVt_PVGV) {
a0d0e21e
LW
3207 char *name = GvNAME(sstr);
3208 STRLEN len = GvNAMELEN(sstr);
463ee0b2 3209 sv_upgrade(dstr, SVt_PVGV);
6662521e 3210 sv_magic(dstr, dstr, '*', Nullch, 0);
85aff577 3211 GvSTASH(dstr) = (HV*)SvREFCNT_inc(GvSTASH(sstr));
a0d0e21e
LW
3212 GvNAME(dstr) = savepvn(name, len);
3213 GvNAMELEN(dstr) = len;
3214 SvFAKE_on(dstr); /* can coerce to non-glob */
3215 }
7bac28a0 3216 /* ahem, death to those who redefine active sort subs */
3280af22
NIS
3217 else if (PL_curstackinfo->si_type == PERLSI_SORT
3218 && GvCV(dstr) && PL_sortcop == CvSTART(GvCV(dstr)))
cea2e8a9 3219 Perl_croak(aTHX_ "Can't redefine active sort subroutine %s",
7bac28a0 3220 GvNAME(dstr));
5bd07a3d
DM
3221
3222#ifdef GV_SHARED_CHECK
3223 if (GvSHARED((GV*)dstr)) {
3224 Perl_croak(aTHX_ PL_no_modify);
3225 }
3226#endif
3227
a0d0e21e 3228 (void)SvOK_off(dstr);
a5f75d66 3229 GvINTRO_off(dstr); /* one-shot flag */
1edc1566 3230 gp_free((GV*)dstr);
79072805 3231 GvGP(dstr) = gp_ref(GvGP(sstr));
27c9684d
AP
3232 if (SvTAINTED(sstr))
3233 SvTAINT(dstr);
1d7c1841
GS
3234 if (GvIMPORTED(dstr) != GVf_IMPORTED
3235 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3236 {
a5f75d66 3237 GvIMPORTED_on(dstr);
1d7c1841 3238 }
a5f75d66 3239 GvMULTI_on(dstr);
79072805
LW
3240 return;
3241 }
3242 /* FALL THROUGH */
3243
3244 default:
973f89ab
CS
3245 if (SvGMAGICAL(sstr)) {
3246 mg_get(sstr);
3247 if (SvTYPE(sstr) != stype) {
3248 stype = SvTYPE(sstr);
3249 if (stype == SVt_PVGV && dtype <= SVt_PVGV)
3250 goto glob_assign;
3251 }
3252 }
ded42b9f 3253 if (stype == SVt_PVLV)
6fc92669 3254 (void)SvUPGRADE(dstr, SVt_PVNV);
ded42b9f 3255 else
6fc92669 3256 (void)SvUPGRADE(dstr, stype);
79072805
LW
3257 }
3258
8990e307
LW
3259 sflags = SvFLAGS(sstr);
3260
3261 if (sflags & SVf_ROK) {
3262 if (dtype >= SVt_PV) {
3263 if (dtype == SVt_PVGV) {
3264 SV *sref = SvREFCNT_inc(SvRV(sstr));
3265 SV *dref = 0;
a5f75d66 3266 int intro = GvINTRO(dstr);
a0d0e21e 3267
5bd07a3d
DM
3268#ifdef GV_SHARED_CHECK
3269 if (GvSHARED((GV*)dstr)) {
3270 Perl_croak(aTHX_ PL_no_modify);
3271 }
3272#endif
3273
a0d0e21e
LW
3274 if (intro) {
3275 GP *gp;
1d7c1841 3276 gp_free((GV*)dstr);
a5f75d66 3277 GvINTRO_off(dstr); /* one-shot flag */
a0d0e21e 3278 Newz(602,gp, 1, GP);
44a8e56a 3279 GvGP(dstr) = gp_ref(gp);
a0d0e21e 3280 GvSV(dstr) = NEWSV(72,0);
1d7c1841 3281 GvLINE(dstr) = CopLINE(PL_curcop);
1edc1566 3282 GvEGV(dstr) = (GV*)dstr;
a0d0e21e 3283 }
a5f75d66 3284 GvMULTI_on(dstr);
8990e307
LW
3285 switch (SvTYPE(sref)) {
3286 case SVt_PVAV:
a0d0e21e
LW
3287 if (intro)
3288 SAVESPTR(GvAV(dstr));
3289 else
3290 dref = (SV*)GvAV(dstr);
8990e307 3291 GvAV(dstr) = (AV*)sref;
39bac7f7 3292 if (!GvIMPORTED_AV(dstr)
1d7c1841
GS
3293 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3294 {
a5f75d66 3295 GvIMPORTED_AV_on(dstr);
1d7c1841 3296 }
8990e307
LW
3297 break;
3298 case SVt_PVHV:
a0d0e21e
LW
3299 if (intro)
3300 SAVESPTR(GvHV(dstr));
3301 else
3302 dref = (SV*)GvHV(dstr);
8990e307 3303 GvHV(dstr) = (HV*)sref;
39bac7f7 3304 if (!GvIMPORTED_HV(dstr)
1d7c1841
GS
3305 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3306 {
a5f75d66 3307 GvIMPORTED_HV_on(dstr);
1d7c1841 3308 }
8990e307
LW
3309 break;
3310 case SVt_PVCV:
8ebc5c01
PP
3311 if (intro) {
3312 if (GvCVGEN(dstr) && GvCV(dstr) != (CV*)sref) {
3313 SvREFCNT_dec(GvCV(dstr));
3314 GvCV(dstr) = Nullcv;
68dc0745 3315 GvCVGEN(dstr) = 0; /* Switch off cacheness. */
3280af22 3316 PL_sub_generation++;
8ebc5c01 3317 }
a0d0e21e 3318 SAVESPTR(GvCV(dstr));
8ebc5c01 3319 }
68dc0745
PP
3320 else
3321 dref = (SV*)GvCV(dstr);
3322 if (GvCV(dstr) != (CV*)sref) {
748a9306 3323 CV* cv = GvCV(dstr);
4633a7c4 3324 if (cv) {
68dc0745
PP
3325 if (!GvCVGEN((GV*)dstr) &&
3326 (CvROOT(cv) || CvXSUB(cv)))
3327 {
7bac28a0
PP
3328 /* ahem, death to those who redefine
3329 * active sort subs */
3280af22
NIS
3330 if (PL_curstackinfo->si_type == PERLSI_SORT &&
3331 PL_sortcop == CvSTART(cv))
1c846c1f 3332 Perl_croak(aTHX_
7bac28a0
PP
3333 "Can't redefine active sort subroutine %s",
3334 GvENAME((GV*)dstr));
beab0874
JT
3335 /* Redefining a sub - warning is mandatory if
3336 it was a const and its value changed. */
3337 if (ckWARN(WARN_REDEFINE)
3338 || (CvCONST(cv)
3339 && (!CvCONST((CV*)sref)
3340 || sv_cmp(cv_const_sv(cv),
3341 cv_const_sv((CV*)sref)))))
3342 {
3343 Perl_warner(aTHX_ WARN_REDEFINE,
3344 CvCONST(cv)
3345 ? "Constant subroutine %s redefined"
47deb5e7 3346 : "Subroutine %s redefined",
beab0874
JT
3347 GvENAME((GV*)dstr));
3348 }
9607fc9c 3349 }
3fe9a6f1
PP
3350 cv_ckproto(cv, (GV*)dstr,
3351 SvPOK(sref) ? SvPVX(sref) : Nullch);
4633a7c4 3352 }
a5f75d66 3353 GvCV(dstr) = (CV*)sref;
7a4c00b4 3354 GvCVGEN(dstr) = 0; /* Switch off cacheness. */
a5f75d66 3355 GvASSUMECV_on(dstr);
3280af22 3356 PL_sub_generation++;
a5f75d66 3357 }
39bac7f7 3358 if (!GvIMPORTED_CV(dstr)
1d7c1841
GS
3359 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3360 {
a5f75d66 3361 GvIMPORTED_CV_on(dstr);
1d7c1841 3362 }
8990e307 3363 break;
91bba347
LW
3364 case SVt_PVIO:
3365 if (intro)
3366 SAVESPTR(GvIOp(dstr));
3367 else
3368 dref = (SV*)GvIOp(dstr);
3369 GvIOp(dstr) = (IO*)sref;
3370 break;
f4d13ee9
JH
3371 case SVt_PVFM:
3372 if (intro)
3373 SAVESPTR(GvFORM(dstr));
3374 else
3375 dref = (SV*)GvFORM(dstr);
3376 GvFORM(dstr) = (CV*)sref;
3377 break;
8990e307 3378 default:
a0d0e21e
LW
3379 if (intro)
3380 SAVESPTR(GvSV(dstr));
3381 else
3382 dref = (SV*)GvSV(dstr);
8990e307 3383 GvSV(dstr) = sref;
39bac7f7 3384 if (!GvIMPORTED_SV(dstr)
1d7c1841
GS
3385 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3386 {
a5f75d66 3387 GvIMPORTED_SV_on(dstr);
1d7c1841 3388 }
8990e307
LW
3389 break;
3390 }
3391 if (dref)
3392 SvREFCNT_dec(dref);
a0d0e21e
LW
3393 if (intro)
3394 SAVEFREESV(sref);
27c9684d
AP
3395 if (SvTAINTED(sstr))
3396 SvTAINT(dstr);
8990e307
LW
3397 return;
3398 }
a0d0e21e 3399 if (SvPVX(dstr)) {
760ac839 3400 (void)SvOOK_off(dstr); /* backoff */
50483b2c
JD
3401 if (SvLEN(dstr))
3402 Safefree(SvPVX(dstr));
a0d0e21e
LW
3403 SvLEN(dstr)=SvCUR(dstr)=0;
3404 }
8990e307 3405 }
a0d0e21e 3406 (void)SvOK_off(dstr);
8990e307 3407 SvRV(dstr) = SvREFCNT_inc(SvRV(sstr));
ed6116ce 3408 SvROK_on(dstr);
8990e307 3409 if (sflags & SVp_NOK) {
3332b3c1
JH
3410 SvNOKp_on(dstr);
3411 /* Only set the public OK flag if the source has public OK. */
3412 if (sflags & SVf_NOK)
3413 SvFLAGS(dstr) |= SVf_NOK;
ed6116ce
LW
3414 SvNVX(dstr) = SvNVX(sstr);
3415 }
8990e307 3416 if (sflags & SVp_IOK) {
3332b3c1
JH
3417 (void)SvIOKp_on(dstr);
3418 if (sflags & SVf_IOK)
3419 SvFLAGS(dstr) |= SVf_IOK;
2b1c7e3e 3420 if (sflags & SVf_IVisUV)
25da4f38 3421 SvIsUV_on(dstr);
3332b3c1 3422 SvIVX(dstr) = SvIVX(sstr);
ed6116ce 3423 }
a0d0e21e
LW
3424 if (SvAMAGIC(sstr)) {
3425 SvAMAGIC_on(dstr);
3426 }
ed6116ce 3427 }
8990e307 3428 else if (sflags & SVp_POK) {
79072805
LW
3429
3430 /*
3431 * Check to see if we can just swipe the string. If so, it's a
3432 * possible small lose on short strings, but a big win on long ones.
463ee0b2
LW
3433 * It might even be a win on short strings if SvPVX(dstr)
3434 * has to be allocated and SvPVX(sstr) has to be freed.
79072805
LW
3435 */
3436
ff68c719 3437 if (SvTEMP(sstr) && /* slated for free anyway? */
01b73108 3438 SvREFCNT(sstr) == 1 && /* and no other references to it? */
1c846c1f 3439 !(sflags & SVf_OOK) && /* and not involved in OOK hack? */
4c8f17b9
BH
3440 SvLEN(sstr) && /* and really is a string */
3441 !(PL_op && PL_op->op_type == OP_AASSIGN)) /* and won't be needed again, potentially */
a5f75d66 3442 {
adbc6bb1 3443 if (SvPVX(dstr)) { /* we know that dtype >= SVt_PV */
a5f75d66
AD
3444 if (SvOOK(dstr)) {
3445 SvFLAGS(dstr) &= ~SVf_OOK;
3446 Safefree(SvPVX(dstr) - SvIVX(dstr));
3447 }
50483b2c 3448 else if (SvLEN(dstr))
a5f75d66 3449 Safefree(SvPVX(dstr));
79072805 3450 }
a5f75d66 3451 (void)SvPOK_only(dstr);
463ee0b2 3452 SvPV_set(dstr, SvPVX(sstr));
79072805
LW
3453 SvLEN_set(dstr, SvLEN(sstr));
3454 SvCUR_set(dstr, SvCUR(sstr));
f4e86e0f 3455
79072805 3456 SvTEMP_off(dstr);
2b1c7e3e 3457 (void)SvOK_off(sstr); /* NOTE: nukes most SvFLAGS on sstr */
79072805
LW
3458 SvPV_set(sstr, Nullch);
3459 SvLEN_set(sstr, 0);
a5f75d66
AD
3460 SvCUR_set(sstr, 0);
3461 SvTEMP_off(sstr);
79072805
LW
3462 }
3463 else { /* have to copy actual string */
8990e307
LW
3464 STRLEN len = SvCUR(sstr);
3465
3466 SvGROW(dstr, len + 1); /* inlined from sv_setpvn */
3467 Move(SvPVX(sstr),SvPVX(dstr),len,char);
3468 SvCUR_set(dstr, len);
3469 *SvEND(dstr) = '\0';
a0d0e21e 3470 (void)SvPOK_only(dstr);
79072805 3471 }
9aa983d2 3472 if (sflags & SVf_UTF8)
a7cb1f99 3473 SvUTF8_on(dstr);
79072805 3474 /*SUPPRESS 560*/
8990e307 3475 if (sflags & SVp_NOK) {
3332b3c1
JH
3476 SvNOKp_on(dstr);
3477 if (sflags & SVf_NOK)
3478 SvFLAGS(dstr) |= SVf_NOK;
463ee0b2 3479 SvNVX(dstr) = SvNVX(sstr);
79072805 3480 }
8990e307 3481 if (sflags & SVp_IOK) {
3332b3c1
JH
3482 (void)SvIOKp_on(dstr);
3483 if (sflags & SVf_IOK)
3484 SvFLAGS(dstr) |= SVf_IOK;
2b1c7e3e 3485 if (sflags & SVf_IVisUV)
25da4f38 3486 SvIsUV_on(dstr);
463ee0b2 3487 SvIVX(dstr) = SvIVX(sstr);
79072805
LW
3488 }
3489 }
8990e307 3490 else if (sflags & SVp_IOK) {
3332b3c1
JH
3491 if (sflags & SVf_IOK)
3492 (void)SvIOK_only(dstr);
3493 else {
9cbac4c7
DM
3494 (void)SvOK_off(dstr);
3495 (void)SvIOKp_on(dstr);
3332b3c1
JH
3496 }
3497 /* XXXX Do we want to set IsUV for IV(ROK)? Be extra safe... */
2b1c7e3e 3498 if (sflags & SVf_IVisUV)
25da4f38 3499 SvIsUV_on(dstr);
3332b3c1
JH
3500 SvIVX(dstr) = SvIVX(sstr);
3501 if (sflags & SVp_NOK) {
3502 if (sflags & SVf_NOK)
3503 (void)SvNOK_on(dstr);
3504 else
3505 (void)SvNOKp_on(dstr);
3506 SvNVX(dstr) = SvNVX(sstr);
3507 }
3508 }
3509 else if (sflags & SVp_NOK) {
3510 if (sflags & SVf_NOK)
3511 (void)SvNOK_only(dstr);
3512 else {
9cbac4c7 3513 (void)SvOK_off(dstr);
3332b3c1
JH
3514 SvNOKp_on(dstr);
3515 }
3516 SvNVX(dstr) = SvNVX(sstr);
79072805
LW
3517 }
3518 else {
20408e3c 3519 if (dtype == SVt_PVGV) {
e476b1b5
GS
3520 if (ckWARN(WARN_MISC))
3521 Perl_warner(aTHX_ WARN_MISC, "Undefined value assigned to typeglob");
20408e3c
GS
3522 }
3523 else
3524 (void)SvOK_off(dstr);
a0d0e21e 3525 }
27c9684d
AP
3526 if (SvTAINTED(sstr))
3527 SvTAINT(dstr);
79072805
LW
3528}
3529
954c1994
GS
3530/*
3531=for apidoc sv_setsv_mg
3532
3533Like C<sv_setsv>, but also handles 'set' magic.
3534
3535=cut
3536*/
3537
79072805 3538void
864dbfa3 3539Perl_sv_setsv_mg(pTHX_ SV *dstr, register SV *sstr)
ef50df4b
GS
3540{
3541 sv_setsv(dstr,sstr);
3542 SvSETMAGIC(dstr);
3543}
3544
954c1994
GS
3545/*
3546=for apidoc sv_setpvn
3547
3548Copies a string into an SV. The C<len> parameter indicates the number of
3549bytes to be copied. Does not handle 'set' magic. See C<sv_setpvn_mg>.
3550
3551=cut
3552*/
3553
ef50df4b 3554void
864dbfa3 3555Perl_sv_setpvn(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
79072805 3556{
c6f8c383 3557 register char *dptr;
22c522df 3558
2213622d 3559 SV_CHECK_THINKFIRST(sv);
463ee0b2 3560 if (!ptr) {
a0d0e21e 3561 (void)SvOK_off(sv);
463ee0b2
LW
3562 return;
3563 }
22c522df
JH
3564 else {
3565 /* len is STRLEN which is unsigned, need to copy to signed */
3566 IV iv = len;
3567 assert(iv >= 0);
3568 }
6fc92669 3569 (void)SvUPGRADE(sv, SVt_PV);
c6f8c383 3570
79072805 3571 SvGROW(sv, len + 1);
c6f8c383
GA
3572 dptr = SvPVX(sv);
3573 Move(ptr,dptr,len,char);
3574 dptr[len] = '\0';
79072805 3575 SvCUR_set(sv, len);
1aa99e6b 3576 (void)SvPOK_only_UTF8(sv); /* validate pointer */
463ee0b2 3577 SvTAINT(sv);
79072805
LW
3578}
3579
954c1994
GS
3580/*
3581=for apidoc sv_setpvn_mg
3582
3583Like C<sv_setpvn>, but also handles 'set' magic.
3584
3585=cut
3586*/
3587
79072805 3588void
864dbfa3 3589Perl_sv_setpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
ef50df4b
GS
3590{
3591 sv_setpvn(sv,ptr,len);
3592 SvSETMAGIC(sv);
3593}
3594
954c1994
GS
3595/*
3596=for apidoc sv_setpv
3597
3598Copies a string into an SV. The string must be null-terminated. Does not
3599handle 'set' magic. See C<sv_setpv_mg>.
3600
3601=cut
3602*/
3603
ef50df4b 3604void
864dbfa3 3605Perl_sv_setpv(pTHX_ register SV *sv, register const char *ptr)
79072805
LW
3606{
3607 register STRLEN len;
3608
2213622d 3609 SV_CHECK_THINKFIRST(sv);
463ee0b2 3610 if (!ptr) {
a0d0e21e 3611 (void)SvOK_off(sv);
463ee0b2
LW
3612 return;
3613 }
79072805 3614 len = strlen(ptr);
6fc92669 3615 (void)SvUPGRADE(sv, SVt_PV);
c6f8c383 3616
79072805 3617 SvGROW(sv, len + 1);
463ee0b2 3618 Move(ptr,SvPVX(sv),len+1,char);
79072805 3619 SvCUR_set(sv, len);
1aa99e6b 3620 (void)SvPOK_only_UTF8(sv); /* validate pointer */
463ee0b2
LW
3621 SvTAINT(sv);
3622}
3623
954c1994
GS
3624/*
3625=for apidoc sv_setpv_mg
3626
3627Like C<sv_setpv>, but also handles 'set' magic.
3628
3629=cut
3630*/
3631
463ee0b2 3632void
864dbfa3 3633Perl_sv_setpv_mg(pTHX_ register SV *sv, register const char *ptr)
ef50df4b
GS
3634{
3635 sv_setpv(sv,ptr);
3636 SvSETMAGIC(sv);
3637}
3638
954c1994
GS
3639/*
3640=for apidoc sv_usepvn
3641
3642Tells an SV to use C<ptr> to find its string value. Normally the string is
1c846c1f 3643stored inside the SV but sv_usepvn allows the SV to use an outside string.
954c1994
GS
3644The C<ptr> should point to memory that was allocated by C<malloc>. The
3645string length, C<len>, must be supplied. This function will realloc the
3646memory pointed to by C<ptr>, so that pointer should not be freed or used by
3647the programmer after giving it to sv_usepvn. Does not handle 'set' magic.
3648See C<sv_usepvn_mg>.
3649
3650=cut
3651*/
3652
ef50df4b 3653void
864dbfa3 3654Perl_sv_usepvn(pTHX_ register SV *sv, register char *ptr, register STRLEN len)
463ee0b2 3655{
2213622d 3656 SV_CHECK_THINKFIRST(sv);
c6f8c383 3657 (void)SvUPGRADE(sv, SVt_PV);
463ee0b2 3658 if (!ptr) {
a0d0e21e 3659 (void)SvOK_off(sv);
463ee0b2
LW
3660 return;
3661 }
a0ed51b3 3662 (void)SvOOK_off(sv);
50483b2c 3663 if (SvPVX(sv) && SvLEN(sv))
463ee0b2
LW
3664 Safefree(SvPVX(sv));
3665 Renew(ptr, len+1, char);
3666 SvPVX(sv) = ptr;
3667 SvCUR_set(sv, len);
3668 SvLEN_set(sv, len+1);
3669 *SvEND(sv) = '\0';
1aa99e6b 3670 (void)SvPOK_only_UTF8(sv); /* validate pointer */
463ee0b2 3671 SvTAINT(sv);
79072805
LW
3672}
3673
954c1994
GS
3674/*
3675=for apidoc sv_usepvn_mg
3676
3677Like C<sv_usepvn>, but also handles 'set' magic.
3678
3679=cut
3680*/
3681
ef50df4b 3682void
864dbfa3 3683Perl_sv_usepvn_mg(pTHX_ register SV *sv, register char *ptr, register STRLEN len)
ef50df4b 3684{
51c1089b 3685 sv_usepvn(sv,ptr,len);
ef50df4b
GS
3686 SvSETMAGIC(sv);
3687}
3688
6fc92669 3689void
840a7b70 3690Perl_sv_force_normal_flags(pTHX_ register SV *sv, U32 flags)
0f15f207 3691{
2213622d 3692 if (SvREADONLY(sv)) {
1c846c1f
NIS
3693 if (SvFAKE(sv)) {
3694 char *pvx = SvPVX(sv);
3695 STRLEN len = SvCUR(sv);
3696 U32 hash = SvUVX(sv);
3697 SvGROW(sv, len + 1);
3698 Move(pvx,SvPVX(sv),len,char);
3699 *SvEND(sv) = '\0';
3700 SvFAKE_off(sv);
3701 SvREADONLY_off(sv);
c3654f1a 3702 unsharepvn(pvx,SvUTF8(sv)?-len:len,hash);
1c846c1f
NIS
3703 }
3704 else if (PL_curcop != &PL_compiling)
cea2e8a9 3705 Perl_croak(aTHX_ PL_no_modify);
0f15f207 3706 }
2213622d 3707 if (SvROK(sv))
840a7b70 3708 sv_unref_flags(sv, flags);
6fc92669
GS
3709 else if (SvFAKE(sv) && SvTYPE(sv) == SVt_PVGV)
3710 sv_unglob(sv);
0f15f207 3711}
1c846c1f 3712
840a7b70
IZ
3713void
3714Perl_sv_force_normal(pTHX_ register SV *sv)
3715{
3716 sv_force_normal_flags(sv, 0);
3717}
3718
954c1994
GS
3719/*
3720=for apidoc sv_chop
3721
1c846c1f 3722Efficient removal of characters from the beginning of the string buffer.
954c1994
GS
3723SvPOK(sv) must be true and the C<ptr> must be a pointer to somewhere inside
3724the string buffer. The C<ptr> becomes the first character of the adjusted
3725string.
3726
3727=cut
3728*/
3729
79072805 3730void
864dbfa3 3731Perl_sv_chop(pTHX_ register SV *sv, register char *ptr) /* like set but assuming ptr is in sv */
1c846c1f
NIS
3732
3733
79072805
LW
3734{
3735 register STRLEN delta;
3736
a0d0e21e 3737 if (!ptr || !SvPOKp(sv))
79072805 3738 return;
2213622d 3739 SV_CHECK_THINKFIRST(sv);
79072805
LW
3740 if (SvTYPE(sv) < SVt_PVIV)
3741 sv_upgrade(sv,SVt_PVIV);
3742
3743 if (!SvOOK(sv)) {
50483b2c
JD
3744 if (!SvLEN(sv)) { /* make copy of shared string */
3745 char *pvx = SvPVX(sv);
3746 STRLEN len = SvCUR(sv);
3747 SvGROW(sv, len + 1);
3748 Move(pvx,SvPVX(sv),len,char);
3749 *SvEND(sv) = '\0';
3750 }
463ee0b2 3751 SvIVX(sv) = 0;
79072805
LW
3752 SvFLAGS(sv) |= SVf_OOK;
3753 }
25da4f38 3754 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVp_IOK|SVp_NOK|SVf_IVisUV);
463ee0b2 3755 delta = ptr - SvPVX(sv);
79072805
LW
3756 SvLEN(sv) -= delta;
3757 SvCUR(sv) -= delta;
463ee0b2
LW
3758 SvPVX(sv) += delta;
3759 SvIVX(sv) += delta;
79072805
LW
3760}
3761
954c1994
GS
3762/*
3763=for apidoc sv_catpvn
3764
3765Concatenates the string onto the end of the string which is in the SV. The
3766C<len> indicates number of bytes to copy. Handles 'get' magic, but not
3767'set' magic. See C<sv_catpvn_mg>.
3768
3769=cut
3770*/
3771
79072805 3772void
864dbfa3 3773Perl_sv_catpvn(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
79072805 3774{
463ee0b2 3775 STRLEN tlen;
748a9306 3776 char *junk;
a0d0e21e 3777
748a9306 3778 junk = SvPV_force(sv, tlen);
463ee0b2 3779 SvGROW(sv, tlen + len + 1);
4633a7c4
LW
3780 if (ptr == junk)
3781 ptr = SvPVX(sv);
463ee0b2 3782 Move(ptr,SvPVX(sv)+tlen,len,char);
79072805
LW
3783 SvCUR(sv) += len;
3784 *SvEND(sv) = '\0';
d41ff1b8 3785 (void)SvPOK_only_UTF8(sv); /* validate pointer */
463ee0b2 3786 SvTAINT(sv);
79072805
LW
3787}
3788
954c1994
GS
3789/*
3790=for apidoc sv_catpvn_mg
3791
3792Like C<sv_catpvn>, but also handles 'set' magic.
3793
3794=cut
3795*/
3796
79072805 3797void
864dbfa3 3798Perl_sv_catpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
ef50df4b
GS
3799{
3800 sv_catpvn(sv,ptr,len);
3801 SvSETMAGIC(sv);
3802}
3803
954c1994
GS
3804/*
3805=for apidoc sv_catsv
3806
13e8c8e3
JH
3807Concatenates the string from SV C<ssv> onto the end of the string in
3808SV C<dsv>. Modifies C<dsv> but not C<ssv>. Handles 'get' magic, but
3809not 'set' magic. See C<sv_catsv_mg>.
954c1994 3810
13e8c8e3 3811=cut */
954c1994 3812
ef50df4b 3813void
46199a12 3814Perl_sv_catsv(pTHX_ SV *dsv, register SV *ssv)
79072805 3815{
13e8c8e3
JH
3816 char *spv;
3817 STRLEN slen;
46199a12 3818 if (!ssv)
79072805 3819 return;
46199a12
JH
3820 if ((spv = SvPV(ssv, slen))) {
3821 bool dutf8 = DO_UTF8(dsv);
3822 bool sutf8 = DO_UTF8(ssv);
13e8c8e3
JH
3823
3824 if (dutf8 == sutf8)
46199a12 3825 sv_catpvn(dsv,spv,slen);