3 * Copyright (c) 1991-2001, Larry Wall
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.
11 * "I wonder what the Entish is for 'yes' and 'no'," he thought.
19 #define SV_CHECK_THINKFIRST(sv) if (SvTHINKFIRST(sv)) sv_force_normal(sv)
21 static void do_report_used(pTHXo_ SV *sv);
22 static void do_clean_objs(pTHXo_ SV *sv);
23 #ifndef DISABLE_DESTRUCTOR_KLUDGE
24 static void do_clean_named_objs(pTHXo_ SV *sv);
26 static void do_clean_all(pTHXo_ SV *sv);
29 * "A time to plant, and a time to uproot what was planted..."
34 SvANY(p) = (void *)PL_sv_root; \
35 SvFLAGS(p) = SVTYPEMASK; \
40 /* sv_mutex must be held while calling uproot_SV() */
41 #define uproot_SV(p) \
44 PL_sv_root = (SV*)SvANY(p); \
81 for (sva = PL_sv_arenaroot; sva; sva = (SV *) SvANY(sva)) {
83 svend = &sva[SvREFCNT(sva)];
84 if (p >= sv && p < svend)
88 if (ckWARN_d(WARN_INTERNAL))
89 Perl_warner(aTHX_ WARN_INTERNAL,
90 "Attempt to free non-arena SV: 0x%"UVxf,
98 #else /* ! DEBUGGING */
100 #define del_SV(p) plant_SV(p)
102 #endif /* DEBUGGING */
105 Perl_sv_add_arena(pTHX_ char *ptr, U32 size, U32 flags)
110 Zero(ptr, size, char);
112 /* The first SV in an arena isn't an SV. */
113 SvANY(sva) = (void *) PL_sv_arenaroot; /* ptr to next arena */
114 SvREFCNT(sva) = size / sizeof(SV); /* number of SV slots */
115 SvFLAGS(sva) = flags; /* FAKE if not to be freed */
117 PL_sv_arenaroot = sva;
118 PL_sv_root = sva + 1;
120 svend = &sva[SvREFCNT(sva) - 1];
123 SvANY(sv) = (void *)(SV*)(sv + 1);
124 SvFLAGS(sv) = SVTYPEMASK;
128 SvFLAGS(sv) = SVTYPEMASK;
131 /* sv_mutex must be held while calling more_sv() */
138 sv_add_arena(PL_nice_chunk, PL_nice_chunk_size, 0);
139 PL_nice_chunk = Nullch;
140 PL_nice_chunk_size = 0;
143 char *chunk; /* must use New here to match call to */
144 New(704,chunk,1008,char); /* Safefree() in sv_free_arenas() */
145 sv_add_arena(chunk, 1008, 0);
152 S_visit(pTHX_ SVFUNC_t f)
159 for (sva = PL_sv_arenaroot; sva; sva = (SV*)SvANY(sva)) {
160 svend = &sva[SvREFCNT(sva)];
161 for (sv = sva + 1; sv < svend; ++sv) {
162 if (SvTYPE(sv) != SVTYPEMASK && SvREFCNT(sv)) {
172 Perl_sv_report_used(pTHX)
174 visit(do_report_used);
178 Perl_sv_clean_objs(pTHX)
180 PL_in_clean_objs = TRUE;
181 visit(do_clean_objs);
182 #ifndef DISABLE_DESTRUCTOR_KLUDGE
183 /* some barnacles may yet remain, clinging to typeglobs */
184 visit(do_clean_named_objs);
186 PL_in_clean_objs = FALSE;
190 Perl_sv_clean_all(pTHX)
193 PL_in_clean_all = TRUE;
194 cleaned = visit(do_clean_all);
195 PL_in_clean_all = FALSE;
200 Perl_sv_free_arenas(pTHX)
204 XPV *arena, *arenanext;
206 /* Free arenas here, but be careful about fake ones. (We assume
207 contiguity of the fake ones with the corresponding real ones.) */
209 for (sva = PL_sv_arenaroot; sva; sva = svanext) {
210 svanext = (SV*) SvANY(sva);
211 while (svanext && SvFAKE(svanext))
212 svanext = (SV*) SvANY(svanext);
215 Safefree((void *)sva);
218 for (arena = PL_xiv_arenaroot; arena; arena = arenanext) {
219 arenanext = (XPV*)arena->xpv_pv;
222 PL_xiv_arenaroot = 0;
224 for (arena = PL_xnv_arenaroot; arena; arena = arenanext) {
225 arenanext = (XPV*)arena->xpv_pv;
228 PL_xnv_arenaroot = 0;
230 for (arena = PL_xrv_arenaroot; arena; arena = arenanext) {
231 arenanext = (XPV*)arena->xpv_pv;
234 PL_xrv_arenaroot = 0;
236 for (arena = PL_xpv_arenaroot; arena; arena = arenanext) {
237 arenanext = (XPV*)arena->xpv_pv;
240 PL_xpv_arenaroot = 0;
242 for (arena = (XPV*)PL_xpviv_arenaroot; arena; arena = arenanext) {
243 arenanext = (XPV*)arena->xpv_pv;
246 PL_xpviv_arenaroot = 0;
248 for (arena = (XPV*)PL_xpvnv_arenaroot; arena; arena = arenanext) {
249 arenanext = (XPV*)arena->xpv_pv;
252 PL_xpvnv_arenaroot = 0;
254 for (arena = (XPV*)PL_xpvcv_arenaroot; arena; arena = arenanext) {
255 arenanext = (XPV*)arena->xpv_pv;
258 PL_xpvcv_arenaroot = 0;
260 for (arena = (XPV*)PL_xpvav_arenaroot; arena; arena = arenanext) {
261 arenanext = (XPV*)arena->xpv_pv;
264 PL_xpvav_arenaroot = 0;
266 for (arena = (XPV*)PL_xpvhv_arenaroot; arena; arena = arenanext) {
267 arenanext = (XPV*)arena->xpv_pv;
270 PL_xpvhv_arenaroot = 0;
272 for (arena = (XPV*)PL_xpvmg_arenaroot; arena; arena = arenanext) {
273 arenanext = (XPV*)arena->xpv_pv;
276 PL_xpvmg_arenaroot = 0;
278 for (arena = (XPV*)PL_xpvlv_arenaroot; arena; arena = arenanext) {
279 arenanext = (XPV*)arena->xpv_pv;
282 PL_xpvlv_arenaroot = 0;
284 for (arena = (XPV*)PL_xpvbm_arenaroot; arena; arena = arenanext) {
285 arenanext = (XPV*)arena->xpv_pv;
288 PL_xpvbm_arenaroot = 0;
290 for (arena = (XPV*)PL_he_arenaroot; arena; arena = arenanext) {
291 arenanext = (XPV*)arena->xpv_pv;
297 Safefree(PL_nice_chunk);
298 PL_nice_chunk = Nullch;
299 PL_nice_chunk_size = 0;
305 Perl_report_uninit(pTHX)
308 Perl_warner(aTHX_ WARN_UNINITIALIZED, PL_warn_uninit,
309 " in ", PL_op_desc[PL_op->op_type]);
311 Perl_warner(aTHX_ WARN_UNINITIALIZED, PL_warn_uninit, "", "");
323 * See comment in more_xiv() -- RAM.
325 PL_xiv_root = *(IV**)xiv;
327 return (XPVIV*)((char*)xiv - STRUCT_OFFSET(XPVIV, xiv_iv));
331 S_del_xiv(pTHX_ XPVIV *p)
333 IV* xiv = (IV*)((char*)(p) + STRUCT_OFFSET(XPVIV, xiv_iv));
335 *(IV**)xiv = PL_xiv_root;
346 New(705, ptr, 1008/sizeof(XPV), XPV);
347 ptr->xpv_pv = (char*)PL_xiv_arenaroot; /* linked list of xiv arenas */
348 PL_xiv_arenaroot = ptr; /* to keep Purify happy */
351 xivend = &xiv[1008 / sizeof(IV) - 1];
352 xiv += (sizeof(XPV) - 1) / sizeof(IV) + 1; /* fudge by size of XPV */
354 while (xiv < xivend) {
355 *(IV**)xiv = (IV *)(xiv + 1);
369 PL_xnv_root = *(NV**)xnv;
371 return (XPVNV*)((char*)xnv - STRUCT_OFFSET(XPVNV, xnv_nv));
375 S_del_xnv(pTHX_ XPVNV *p)
377 NV* xnv = (NV*)((char*)(p) + STRUCT_OFFSET(XPVNV, xnv_nv));
379 *(NV**)xnv = PL_xnv_root;
390 New(711, ptr, 1008/sizeof(XPV), XPV);
391 ptr->xpv_pv = (char*)PL_xnv_arenaroot;
392 PL_xnv_arenaroot = ptr;
395 xnvend = &xnv[1008 / sizeof(NV) - 1];
396 xnv += (sizeof(XPVIV) - 1) / sizeof(NV) + 1; /* fudge by sizeof XPVIV */
398 while (xnv < xnvend) {
399 *(NV**)xnv = (NV*)(xnv + 1);
413 PL_xrv_root = (XRV*)xrv->xrv_rv;
419 S_del_xrv(pTHX_ XRV *p)
422 p->xrv_rv = (SV*)PL_xrv_root;
431 register XRV* xrvend;
433 New(712, ptr, 1008/sizeof(XPV), XPV);
434 ptr->xpv_pv = (char*)PL_xrv_arenaroot;
435 PL_xrv_arenaroot = ptr;
438 xrvend = &xrv[1008 / sizeof(XRV) - 1];
439 xrv += (sizeof(XPV) - 1) / sizeof(XRV) + 1;
441 while (xrv < xrvend) {
442 xrv->xrv_rv = (SV*)(xrv + 1);
456 PL_xpv_root = (XPV*)xpv->xpv_pv;
462 S_del_xpv(pTHX_ XPV *p)
465 p->xpv_pv = (char*)PL_xpv_root;
474 register XPV* xpvend;
475 New(713, xpv, 1008/sizeof(XPV), XPV);
476 xpv->xpv_pv = (char*)PL_xpv_arenaroot;
477 PL_xpv_arenaroot = xpv;
479 xpvend = &xpv[1008 / sizeof(XPV) - 1];
481 while (xpv < xpvend) {
482 xpv->xpv_pv = (char*)(xpv + 1);
495 xpviv = PL_xpviv_root;
496 PL_xpviv_root = (XPVIV*)xpviv->xpv_pv;
502 S_del_xpviv(pTHX_ XPVIV *p)
505 p->xpv_pv = (char*)PL_xpviv_root;
513 register XPVIV* xpviv;
514 register XPVIV* xpvivend;
515 New(714, xpviv, 1008/sizeof(XPVIV), XPVIV);
516 xpviv->xpv_pv = (char*)PL_xpviv_arenaroot;
517 PL_xpviv_arenaroot = xpviv;
519 xpvivend = &xpviv[1008 / sizeof(XPVIV) - 1];
520 PL_xpviv_root = ++xpviv;
521 while (xpviv < xpvivend) {
522 xpviv->xpv_pv = (char*)(xpviv + 1);
535 xpvnv = PL_xpvnv_root;
536 PL_xpvnv_root = (XPVNV*)xpvnv->xpv_pv;
542 S_del_xpvnv(pTHX_ XPVNV *p)
545 p->xpv_pv = (char*)PL_xpvnv_root;
553 register XPVNV* xpvnv;
554 register XPVNV* xpvnvend;
555 New(715, xpvnv, 1008/sizeof(XPVNV), XPVNV);
556 xpvnv->xpv_pv = (char*)PL_xpvnv_arenaroot;
557 PL_xpvnv_arenaroot = xpvnv;
559 xpvnvend = &xpvnv[1008 / sizeof(XPVNV) - 1];
560 PL_xpvnv_root = ++xpvnv;
561 while (xpvnv < xpvnvend) {
562 xpvnv->xpv_pv = (char*)(xpvnv + 1);
575 xpvcv = PL_xpvcv_root;
576 PL_xpvcv_root = (XPVCV*)xpvcv->xpv_pv;
582 S_del_xpvcv(pTHX_ XPVCV *p)
585 p->xpv_pv = (char*)PL_xpvcv_root;
593 register XPVCV* xpvcv;
594 register XPVCV* xpvcvend;
595 New(716, xpvcv, 1008/sizeof(XPVCV), XPVCV);
596 xpvcv->xpv_pv = (char*)PL_xpvcv_arenaroot;
597 PL_xpvcv_arenaroot = xpvcv;
599 xpvcvend = &xpvcv[1008 / sizeof(XPVCV) - 1];
600 PL_xpvcv_root = ++xpvcv;
601 while (xpvcv < xpvcvend) {
602 xpvcv->xpv_pv = (char*)(xpvcv + 1);
615 xpvav = PL_xpvav_root;
616 PL_xpvav_root = (XPVAV*)xpvav->xav_array;
622 S_del_xpvav(pTHX_ XPVAV *p)
625 p->xav_array = (char*)PL_xpvav_root;
633 register XPVAV* xpvav;
634 register XPVAV* xpvavend;
635 New(717, xpvav, 1008/sizeof(XPVAV), XPVAV);
636 xpvav->xav_array = (char*)PL_xpvav_arenaroot;
637 PL_xpvav_arenaroot = xpvav;
639 xpvavend = &xpvav[1008 / sizeof(XPVAV) - 1];
640 PL_xpvav_root = ++xpvav;
641 while (xpvav < xpvavend) {
642 xpvav->xav_array = (char*)(xpvav + 1);
645 xpvav->xav_array = 0;
655 xpvhv = PL_xpvhv_root;
656 PL_xpvhv_root = (XPVHV*)xpvhv->xhv_array;
662 S_del_xpvhv(pTHX_ XPVHV *p)
665 p->xhv_array = (char*)PL_xpvhv_root;
673 register XPVHV* xpvhv;
674 register XPVHV* xpvhvend;
675 New(718, xpvhv, 1008/sizeof(XPVHV), XPVHV);
676 xpvhv->xhv_array = (char*)PL_xpvhv_arenaroot;
677 PL_xpvhv_arenaroot = xpvhv;
679 xpvhvend = &xpvhv[1008 / sizeof(XPVHV) - 1];
680 PL_xpvhv_root = ++xpvhv;
681 while (xpvhv < xpvhvend) {
682 xpvhv->xhv_array = (char*)(xpvhv + 1);
685 xpvhv->xhv_array = 0;
695 xpvmg = PL_xpvmg_root;
696 PL_xpvmg_root = (XPVMG*)xpvmg->xpv_pv;
702 S_del_xpvmg(pTHX_ XPVMG *p)
705 p->xpv_pv = (char*)PL_xpvmg_root;
713 register XPVMG* xpvmg;
714 register XPVMG* xpvmgend;
715 New(719, xpvmg, 1008/sizeof(XPVMG), XPVMG);
716 xpvmg->xpv_pv = (char*)PL_xpvmg_arenaroot;
717 PL_xpvmg_arenaroot = xpvmg;
719 xpvmgend = &xpvmg[1008 / sizeof(XPVMG) - 1];
720 PL_xpvmg_root = ++xpvmg;
721 while (xpvmg < xpvmgend) {
722 xpvmg->xpv_pv = (char*)(xpvmg + 1);
735 xpvlv = PL_xpvlv_root;
736 PL_xpvlv_root = (XPVLV*)xpvlv->xpv_pv;
742 S_del_xpvlv(pTHX_ XPVLV *p)
745 p->xpv_pv = (char*)PL_xpvlv_root;
753 register XPVLV* xpvlv;
754 register XPVLV* xpvlvend;
755 New(720, xpvlv, 1008/sizeof(XPVLV), XPVLV);
756 xpvlv->xpv_pv = (char*)PL_xpvlv_arenaroot;
757 PL_xpvlv_arenaroot = xpvlv;
759 xpvlvend = &xpvlv[1008 / sizeof(XPVLV) - 1];
760 PL_xpvlv_root = ++xpvlv;
761 while (xpvlv < xpvlvend) {
762 xpvlv->xpv_pv = (char*)(xpvlv + 1);
775 xpvbm = PL_xpvbm_root;
776 PL_xpvbm_root = (XPVBM*)xpvbm->xpv_pv;
782 S_del_xpvbm(pTHX_ XPVBM *p)
785 p->xpv_pv = (char*)PL_xpvbm_root;
793 register XPVBM* xpvbm;
794 register XPVBM* xpvbmend;
795 New(721, xpvbm, 1008/sizeof(XPVBM), XPVBM);
796 xpvbm->xpv_pv = (char*)PL_xpvbm_arenaroot;
797 PL_xpvbm_arenaroot = xpvbm;
799 xpvbmend = &xpvbm[1008 / sizeof(XPVBM) - 1];
800 PL_xpvbm_root = ++xpvbm;
801 while (xpvbm < xpvbmend) {
802 xpvbm->xpv_pv = (char*)(xpvbm + 1);
809 # define my_safemalloc(s) (void*)safexmalloc(717,s)
810 # define my_safefree(p) safexfree((char*)p)
812 # define my_safemalloc(s) (void*)safemalloc(s)
813 # define my_safefree(p) safefree((char*)p)
818 #define new_XIV() my_safemalloc(sizeof(XPVIV))
819 #define del_XIV(p) my_safefree(p)
821 #define new_XNV() my_safemalloc(sizeof(XPVNV))
822 #define del_XNV(p) my_safefree(p)
824 #define new_XRV() my_safemalloc(sizeof(XRV))
825 #define del_XRV(p) my_safefree(p)
827 #define new_XPV() my_safemalloc(sizeof(XPV))
828 #define del_XPV(p) my_safefree(p)
830 #define new_XPVIV() my_safemalloc(sizeof(XPVIV))
831 #define del_XPVIV(p) my_safefree(p)
833 #define new_XPVNV() my_safemalloc(sizeof(XPVNV))
834 #define del_XPVNV(p) my_safefree(p)
836 #define new_XPVCV() my_safemalloc(sizeof(XPVCV))
837 #define del_XPVCV(p) my_safefree(p)
839 #define new_XPVAV() my_safemalloc(sizeof(XPVAV))
840 #define del_XPVAV(p) my_safefree(p)
842 #define new_XPVHV() my_safemalloc(sizeof(XPVHV))
843 #define del_XPVHV(p) my_safefree(p)
845 #define new_XPVMG() my_safemalloc(sizeof(XPVMG))
846 #define del_XPVMG(p) my_safefree(p)
848 #define new_XPVLV() my_safemalloc(sizeof(XPVLV))
849 #define del_XPVLV(p) my_safefree(p)
851 #define new_XPVBM() my_safemalloc(sizeof(XPVBM))
852 #define del_XPVBM(p) my_safefree(p)
856 #define new_XIV() (void*)new_xiv()
857 #define del_XIV(p) del_xiv((XPVIV*) p)
859 #define new_XNV() (void*)new_xnv()
860 #define del_XNV(p) del_xnv((XPVNV*) p)
862 #define new_XRV() (void*)new_xrv()
863 #define del_XRV(p) del_xrv((XRV*) p)
865 #define new_XPV() (void*)new_xpv()
866 #define del_XPV(p) del_xpv((XPV *)p)
868 #define new_XPVIV() (void*)new_xpviv()
869 #define del_XPVIV(p) del_xpviv((XPVIV *)p)
871 #define new_XPVNV() (void*)new_xpvnv()
872 #define del_XPVNV(p) del_xpvnv((XPVNV *)p)
874 #define new_XPVCV() (void*)new_xpvcv()
875 #define del_XPVCV(p) del_xpvcv((XPVCV *)p)
877 #define new_XPVAV() (void*)new_xpvav()
878 #define del_XPVAV(p) del_xpvav((XPVAV *)p)
880 #define new_XPVHV() (void*)new_xpvhv()
881 #define del_XPVHV(p) del_xpvhv((XPVHV *)p)
883 #define new_XPVMG() (void*)new_xpvmg()
884 #define del_XPVMG(p) del_xpvmg((XPVMG *)p)
886 #define new_XPVLV() (void*)new_xpvlv()
887 #define del_XPVLV(p) del_xpvlv((XPVLV *)p)
889 #define new_XPVBM() (void*)new_xpvbm()
890 #define del_XPVBM(p) del_xpvbm((XPVBM *)p)
894 #define new_XPVGV() my_safemalloc(sizeof(XPVGV))
895 #define del_XPVGV(p) my_safefree(p)
897 #define new_XPVFM() my_safemalloc(sizeof(XPVFM))
898 #define del_XPVFM(p) my_safefree(p)
900 #define new_XPVIO() my_safemalloc(sizeof(XPVIO))
901 #define del_XPVIO(p) my_safefree(p)
904 =for apidoc sv_upgrade
906 Upgrade an SV to a more complex form. Use C<SvUPGRADE>. See
913 Perl_sv_upgrade(pTHX_ register SV *sv, U32 mt)
923 if (mt != SVt_PV && SvREADONLY(sv) && SvFAKE(sv)) {
927 if (SvTYPE(sv) == mt)
933 switch (SvTYPE(sv)) {
954 else if (mt < SVt_PVIV)
971 pv = (char*)SvRV(sv);
991 else if (mt == SVt_NV)
1002 del_XPVIV(SvANY(sv));
1012 del_XPVNV(SvANY(sv));
1020 magic = SvMAGIC(sv);
1021 stash = SvSTASH(sv);
1022 del_XPVMG(SvANY(sv));
1025 Perl_croak(aTHX_ "Can't upgrade that kind of scalar");
1030 Perl_croak(aTHX_ "Can't upgrade to undef");
1032 SvANY(sv) = new_XIV();
1036 SvANY(sv) = new_XNV();
1040 SvANY(sv) = new_XRV();
1044 SvANY(sv) = new_XPV();
1050 SvANY(sv) = new_XPVIV();
1060 SvANY(sv) = new_XPVNV();
1068 SvANY(sv) = new_XPVMG();
1074 SvMAGIC(sv) = magic;
1075 SvSTASH(sv) = stash;
1078 SvANY(sv) = new_XPVLV();
1084 SvMAGIC(sv) = magic;
1085 SvSTASH(sv) = stash;
1092 SvANY(sv) = new_XPVAV();
1100 SvMAGIC(sv) = magic;
1101 SvSTASH(sv) = stash;
1107 SvANY(sv) = new_XPVHV();
1115 SvMAGIC(sv) = magic;
1116 SvSTASH(sv) = stash;
1123 SvANY(sv) = new_XPVCV();
1124 Zero(SvANY(sv), 1, XPVCV);
1130 SvMAGIC(sv) = magic;
1131 SvSTASH(sv) = stash;
1134 SvANY(sv) = new_XPVGV();
1140 SvMAGIC(sv) = magic;
1141 SvSTASH(sv) = stash;
1149 SvANY(sv) = new_XPVBM();
1155 SvMAGIC(sv) = magic;
1156 SvSTASH(sv) = stash;
1162 SvANY(sv) = new_XPVFM();
1163 Zero(SvANY(sv), 1, XPVFM);
1169 SvMAGIC(sv) = magic;
1170 SvSTASH(sv) = stash;
1173 SvANY(sv) = new_XPVIO();
1174 Zero(SvANY(sv), 1, XPVIO);
1180 SvMAGIC(sv) = magic;
1181 SvSTASH(sv) = stash;
1182 IoPAGE_LEN(sv) = 60;
1185 SvFLAGS(sv) &= ~SVTYPEMASK;
1191 Perl_sv_backoff(pTHX_ register SV *sv)
1195 char *s = SvPVX(sv);
1196 SvLEN(sv) += SvIVX(sv);
1197 SvPVX(sv) -= SvIVX(sv);
1199 Move(s, SvPVX(sv), SvCUR(sv)+1, char);
1201 SvFLAGS(sv) &= ~SVf_OOK;
1208 Expands the character buffer in the SV. This will use C<sv_unref> and will
1209 upgrade the SV to C<SVt_PV>. Returns a pointer to the character buffer.
1216 Perl_sv_grow(pTHX_ register SV *sv, register STRLEN newlen)
1220 #ifdef HAS_64K_LIMIT
1221 if (newlen >= 0x10000) {
1222 PerlIO_printf(Perl_debug_log,
1223 "Allocation too large: %"UVxf"\n", (UV)newlen);
1226 #endif /* HAS_64K_LIMIT */
1229 if (SvTYPE(sv) < SVt_PV) {
1230 sv_upgrade(sv, SVt_PV);
1233 else if (SvOOK(sv)) { /* pv is offset? */
1236 if (newlen > SvLEN(sv))
1237 newlen += 10 * (newlen - SvCUR(sv)); /* avoid copy each time */
1238 #ifdef HAS_64K_LIMIT
1239 if (newlen >= 0x10000)
1245 if (newlen > SvLEN(sv)) { /* need more room? */
1246 if (SvLEN(sv) && s) {
1247 #if defined(MYMALLOC) && !defined(LEAKTEST)
1248 STRLEN l = malloced_size((void*)SvPVX(sv));
1254 Renew(s,newlen,char);
1257 New(703,s,newlen,char);
1259 SvLEN_set(sv, newlen);
1265 =for apidoc sv_setiv
1267 Copies an integer into the given SV. Does not handle 'set' magic. See
1274 Perl_sv_setiv(pTHX_ register SV *sv, IV i)
1276 SV_CHECK_THINKFIRST(sv);
1277 switch (SvTYPE(sv)) {
1279 sv_upgrade(sv, SVt_IV);
1282 sv_upgrade(sv, SVt_PVNV);
1286 sv_upgrade(sv, SVt_PVIV);
1295 Perl_croak(aTHX_ "Can't coerce %s to integer in %s", sv_reftype(sv,0),
1296 PL_op_desc[PL_op->op_type]);
1298 (void)SvIOK_only(sv); /* validate number */
1304 =for apidoc sv_setiv_mg
1306 Like C<sv_setiv>, but also handles 'set' magic.
1312 Perl_sv_setiv_mg(pTHX_ register SV *sv, IV i)
1319 =for apidoc sv_setuv
1321 Copies an unsigned integer into the given SV. Does not handle 'set' magic.
1328 Perl_sv_setuv(pTHX_ register SV *sv, UV u)
1330 /* With these two if statements:
1331 u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865
1334 u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865
1336 If you wish to remove them, please benchmark to see what the effect is
1338 if (u <= (UV)IV_MAX) {
1339 sv_setiv(sv, (IV)u);
1348 =for apidoc sv_setuv_mg
1350 Like C<sv_setuv>, but also handles 'set' magic.
1356 Perl_sv_setuv_mg(pTHX_ register SV *sv, UV u)
1358 /* With these two if statements:
1359 u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865
1362 u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865
1364 If you wish to remove them, please benchmark to see what the effect is
1366 if (u <= (UV)IV_MAX) {
1367 sv_setiv(sv, (IV)u);
1377 =for apidoc sv_setnv
1379 Copies a double into the given SV. Does not handle 'set' magic. See
1386 Perl_sv_setnv(pTHX_ register SV *sv, NV num)
1388 SV_CHECK_THINKFIRST(sv);
1389 switch (SvTYPE(sv)) {
1392 sv_upgrade(sv, SVt_NV);
1397 sv_upgrade(sv, SVt_PVNV);
1406 Perl_croak(aTHX_ "Can't coerce %s to number in %s", sv_reftype(sv,0),
1407 PL_op_name[PL_op->op_type]);
1410 (void)SvNOK_only(sv); /* validate number */
1415 =for apidoc sv_setnv_mg
1417 Like C<sv_setnv>, but also handles 'set' magic.
1423 Perl_sv_setnv_mg(pTHX_ register SV *sv, NV num)
1430 S_not_a_number(pTHX_ SV *sv)
1434 char *limit = tmpbuf + sizeof(tmpbuf) - 8;
1435 /* each *s can expand to 4 chars + "...\0",
1436 i.e. need room for 8 chars */
1439 for (s = SvPVX(sv), end = s + SvCUR(sv); s < end && d < limit; s++) {
1441 if (ch & 128 && !isPRINT_LC(ch)) {
1450 else if (ch == '\r') {
1454 else if (ch == '\f') {
1458 else if (ch == '\\') {
1462 else if (ch == '\0') {
1466 else if (isPRINT_LC(ch))
1481 Perl_warner(aTHX_ WARN_NUMERIC,
1482 "Argument \"%s\" isn't numeric in %s", tmpbuf,
1483 PL_op_desc[PL_op->op_type]);
1485 Perl_warner(aTHX_ WARN_NUMERIC,
1486 "Argument \"%s\" isn't numeric", tmpbuf);
1490 =for apidoc looks_like_number
1492 Test if an the content of an SV looks like a number (or is a
1493 number). C<Inf> and C<Infinity> are treated as numbers (so will not
1494 issue a non-numeric warning), even if your atof() doesn't grok them.
1500 Perl_looks_like_number(pTHX_ SV *sv)
1502 register char *sbegin;
1509 else if (SvPOKp(sv))
1510 sbegin = SvPV(sv, len);
1512 return 1; /* Historic. Wrong? */
1513 return grok_number(sbegin, len, NULL);
1516 /* Actually, ISO C leaves conversion of UV to IV undefined, but
1517 until proven guilty, assume that things are not that bad... */
1519 /* As 64 bit platforms often have an NV that doesn't preserve all bits of
1520 an IV (an assumption perl has been based on to date) it becomes necessary
1521 to remove the assumption that the NV always carries enough precision to
1522 recreate the IV whenever needed, and that the NV is the canonical form.
1523 Instead, IV/UV and NV need to be given equal rights. So as to not lose
1524 precision as an side effect of conversion (which would lead to insanity
1525 and the dragon(s) in t/op/numconvert.t getting very angry) the intent is
1526 1) to distinguish between IV/UV/NV slots that have cached a valid
1527 conversion where precision was lost and IV/UV/NV slots that have a
1528 valid conversion which has lost no precision
1529 2) to ensure that if a numeric conversion to one form is request that
1530 would lose precision, the precise conversion (or differently
1531 imprecise conversion) is also performed and cached, to prevent
1532 requests for different numeric formats on the same SV causing
1533 lossy conversion chains. (lossless conversion chains are perfectly
1538 SvIOKp is true if the IV slot contains a valid value
1539 SvIOK is true only if the IV value is accurate (UV if SvIOK_UV true)
1540 SvNOKp is true if the NV slot contains a valid value
1541 SvNOK is true only if the NV value is accurate
1544 while converting from PV to NV check to see if converting that NV to an
1545 IV(or UV) would lose accuracy over a direct conversion from PV to
1546 IV(or UV). If it would, cache both conversions, return NV, but mark
1547 SV as IOK NOKp (ie not NOK).
1549 while converting from PV to IV check to see if converting that IV to an
1550 NV would lose accuracy over a direct conversion from PV to NV. If it
1551 would, cache both conversions, flag similarly.
1553 Before, the SV value "3.2" could become NV=3.2 IV=3 NOK, IOK quite
1554 correctly because if IV & NV were set NV *always* overruled.
1555 Now, "3.2" will become NV=3.2 IV=3 NOK, IOKp, because the flags meaning
1556 changes - now IV and NV together means that the two are interchangeable
1557 SvIVX == (IV) SvNVX && SvNVX == (NV) SvIVX;
1559 The benefit of this is operations such as pp_add know that if SvIOK is
1560 true for both left and right operands, then integer addition can be
1561 used instead of floating point. (for cases where the result won't
1562 overflow) Before, floating point was always used, which could lead to
1563 loss of precision compared with integer addition.
1565 * making IV and NV equal status should make maths accurate on 64 bit
1567 * may speed up maths somewhat if pp_add and friends start to use
1568 integers when possible instead of fp. (hopefully the overhead in
1569 looking for SvIOK and checking for overflow will not outweigh the
1570 fp to integer speedup)
1571 * will slow down integer operations (callers of SvIV) on "inaccurate"
1572 values, as the change from SvIOK to SvIOKp will cause a call into
1573 sv_2iv each time rather than a macro access direct to the IV slot
1574 * should speed up number->string conversion on integers as IV is
1575 favoured when IV and NV equally accurate
1577 ####################################################################
1578 You had better be using SvIOK_notUV if you want an IV for arithmetic
1579 SvIOK is true if (IV or UV), so you might be getting (IV)SvUV
1580 SvUOK is true iff UV.
1581 ####################################################################
1583 Your mileage will vary depending your CPUs relative fp to integer
1587 #ifndef NV_PRESERVES_UV
1588 #define IS_NUMBER_UNDERFLOW_IV 1
1589 #define IS_NUMBER_UNDERFLOW_UV 2
1590 #define IS_NUMBER_IV_AND_UV 2
1591 #define IS_NUMBER_OVERFLOW_IV 4
1592 #define IS_NUMBER_OVERFLOW_UV 5
1594 /* For sv_2nv these three cases are "SvNOK and don't bother casting" */
1596 S_sv_2iuv_non_preserve (pTHX_ register SV *sv, I32 numtype)
1598 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));
1599 if (SvNVX(sv) < (NV)IV_MIN) {
1600 (void)SvIOKp_on(sv);
1603 return IS_NUMBER_UNDERFLOW_IV;
1605 if (SvNVX(sv) > (NV)UV_MAX) {
1606 (void)SvIOKp_on(sv);
1610 return IS_NUMBER_OVERFLOW_UV;
1612 (void)SvIOKp_on(sv);
1614 /* Can't use strtol etc to convert this string. (See truth table in
1616 if (SvNVX(sv) <= (UV)IV_MAX) {
1617 SvIVX(sv) = I_V(SvNVX(sv));
1618 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
1619 SvIOK_on(sv); /* Integer is precise. NOK, IOK */
1621 /* Integer is imprecise. NOK, IOKp */
1623 return SvNVX(sv) < 0 ? IS_NUMBER_UNDERFLOW_UV : IS_NUMBER_IV_AND_UV;
1626 SvUVX(sv) = U_V(SvNVX(sv));
1627 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
1628 if (SvUVX(sv) == UV_MAX) {
1629 /* As we know that NVs don't preserve UVs, UV_MAX cannot
1630 possibly be preserved by NV. Hence, it must be overflow.
1632 return IS_NUMBER_OVERFLOW_UV;
1634 SvIOK_on(sv); /* Integer is precise. NOK, UOK */
1636 /* Integer is imprecise. NOK, IOKp */
1638 return IS_NUMBER_OVERFLOW_IV;
1640 #endif /* NV_PRESERVES_UV*/
1643 Perl_sv_2iv(pTHX_ register SV *sv)
1647 if (SvGMAGICAL(sv)) {
1652 return I_V(SvNVX(sv));
1654 if (SvPOKp(sv) && SvLEN(sv))
1657 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
1658 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
1664 if (SvTHINKFIRST(sv)) {
1667 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
1668 (SvTYPE(tmpstr) != SVt_RV || (SvRV(tmpstr) != SvRV(sv))))
1669 return SvIV(tmpstr);
1670 return PTR2IV(SvRV(sv));
1672 if (SvREADONLY(sv) && SvFAKE(sv)) {
1673 sv_force_normal(sv);
1675 if (SvREADONLY(sv) && !SvOK(sv)) {
1676 if (ckWARN(WARN_UNINITIALIZED))
1683 return (IV)(SvUVX(sv));
1690 /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv
1691 * without also getting a cached IV/UV from it at the same time
1692 * (ie PV->NV conversion should detect loss of accuracy and cache
1693 * IV or UV at same time to avoid this. NWC */
1695 if (SvTYPE(sv) == SVt_NV)
1696 sv_upgrade(sv, SVt_PVNV);
1698 (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */
1699 /* < not <= as for NV doesn't preserve UV, ((NV)IV_MAX+1) will almost
1700 certainly cast into the IV range at IV_MAX, whereas the correct
1701 answer is the UV IV_MAX +1. Hence < ensures that dodgy boundary
1703 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
1704 SvIVX(sv) = I_V(SvNVX(sv));
1705 if (SvNVX(sv) == (NV) SvIVX(sv)
1706 #ifndef NV_PRESERVES_UV
1707 && (((UV)1 << NV_PRESERVES_UV_BITS) >
1708 (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv)))
1709 /* Don't flag it as "accurately an integer" if the number
1710 came from a (by definition imprecise) NV operation, and
1711 we're outside the range of NV integer precision */
1714 SvIOK_on(sv); /* Can this go wrong with rounding? NWC */
1715 DEBUG_c(PerlIO_printf(Perl_debug_log,
1716 "0x%"UVxf" iv(%g => %"IVdf") (precise)\n",
1722 /* IV not precise. No need to convert from PV, as NV
1723 conversion would already have cached IV if it detected
1724 that PV->IV would be better than PV->NV->IV
1725 flags already correct - don't set public IOK. */
1726 DEBUG_c(PerlIO_printf(Perl_debug_log,
1727 "0x%"UVxf" iv(%g => %"IVdf") (imprecise)\n",
1732 /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN,
1733 but the cast (NV)IV_MIN rounds to a the value less (more
1734 negative) than IV_MIN which happens to be equal to SvNVX ??
1735 Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and
1736 NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and
1737 (NV)UVX == NVX are both true, but the values differ. :-(
1738 Hopefully for 2s complement IV_MIN is something like
1739 0x8000000000000000 which will be exact. NWC */
1742 SvUVX(sv) = U_V(SvNVX(sv));
1744 (SvNVX(sv) == (NV) SvUVX(sv))
1745 #ifndef NV_PRESERVES_UV
1746 /* Make sure it's not 0xFFFFFFFFFFFFFFFF */
1747 /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */
1748 && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv))
1749 /* Don't flag it as "accurately an integer" if the number
1750 came from a (by definition imprecise) NV operation, and
1751 we're outside the range of NV integer precision */
1757 DEBUG_c(PerlIO_printf(Perl_debug_log,
1758 "0x%"UVxf" 2iv(%"UVuf" => %"IVdf") (as unsigned)\n",
1762 return (IV)SvUVX(sv);
1765 else if (SvPOKp(sv) && SvLEN(sv)) {
1767 int numtype = grok_number(SvPVX(sv), SvCUR(sv), &value);
1768 /* We want to avoid a possible problem when we cache an IV which
1769 may be later translated to an NV, and the resulting NV is not
1770 the same as the direct translation of the initial string
1771 (eg 123.456 can shortcut to the IV 123 with atol(), but we must
1772 be careful to ensure that the value with the .456 is around if the
1773 NV value is requested in the future).
1775 This means that if we cache such an IV, we need to cache the
1776 NV as well. Moreover, we trade speed for space, and do not
1777 cache the NV if we are sure it's not needed.
1780 /* SVt_PVNV is one higher than SVt_PVIV, hence this order */
1781 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
1782 == IS_NUMBER_IN_UV) {
1783 /* It's defintately an integer, only upgrade to PVIV */
1784 if (SvTYPE(sv) < SVt_PVIV)
1785 sv_upgrade(sv, SVt_PVIV);
1787 } else if (SvTYPE(sv) < SVt_PVNV)
1788 sv_upgrade(sv, SVt_PVNV);
1790 /* If NV preserves UV then we only use the UV value if we know that
1791 we aren't going to call atof() below. If NVs don't preserve UVs
1792 then the value returned may have more precision than atof() will
1793 return, even though value isn't perfectly accurate. */
1794 if ((numtype & (IS_NUMBER_IN_UV
1795 #ifdef NV_PRESERVES_UV
1798 )) == IS_NUMBER_IN_UV) {
1799 /* This won't turn off the public IOK flag if it was set above */
1800 (void)SvIOKp_on(sv);
1802 if (!(numtype & IS_NUMBER_NEG)) {
1804 if (value <= (UV)IV_MAX) {
1805 SvIVX(sv) = (IV)value;
1811 /* 2s complement assumption */
1812 if (value <= (UV)IV_MIN) {
1813 SvIVX(sv) = -(IV)value;
1815 /* Too negative for an IV. This is a double upgrade, but
1816 I'm assuming it will be be rare. */
1817 if (SvTYPE(sv) < SVt_PVNV)
1818 sv_upgrade(sv, SVt_PVNV);
1822 SvNVX(sv) = -(NV)value;
1827 /* For !NV_PRESERVES_UV and IS_NUMBER_IN_UV and IS_NUMBER_NOT_INT we
1828 will be in the previous block to set the IV slot, and the next
1829 block to set the NV slot. So no else here. */
1831 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
1832 != IS_NUMBER_IN_UV) {
1833 /* It wasn't an (integer that doesn't overflow the UV). */
1834 SvNVX(sv) = Atof(SvPVX(sv));
1836 if (! numtype && ckWARN(WARN_NUMERIC))
1839 #if defined(USE_LONG_DOUBLE)
1840 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%" PERL_PRIgldbl ")\n",
1841 PTR2UV(sv), SvNVX(sv)));
1843 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%g)\n",
1844 PTR2UV(sv), SvNVX(sv)));
1848 #ifdef NV_PRESERVES_UV
1849 (void)SvIOKp_on(sv);
1851 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
1852 SvIVX(sv) = I_V(SvNVX(sv));
1853 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
1856 /* Integer is imprecise. NOK, IOKp */
1858 /* UV will not work better than IV */
1860 if (SvNVX(sv) > (NV)UV_MAX) {
1862 /* Integer is inaccurate. NOK, IOKp, is UV */
1866 SvUVX(sv) = U_V(SvNVX(sv));
1867 /* 0xFFFFFFFFFFFFFFFF not an issue in here */
1868 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
1872 /* Integer is imprecise. NOK, IOKp, is UV */
1878 #else /* NV_PRESERVES_UV */
1879 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
1880 == (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) {
1881 /* The IV slot will have been set from value returned by
1882 grok_number above. The NV slot has just been set using
1885 assert (SvIOKp(sv));
1887 if (((UV)1 << NV_PRESERVES_UV_BITS) >
1888 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
1889 /* Small enough to preserve all bits. */
1890 (void)SvIOKp_on(sv);
1892 SvIVX(sv) = I_V(SvNVX(sv));
1893 if ((NV)(SvIVX(sv)) == SvNVX(sv))
1895 /* Assumption: first non-preserved integer is < IV_MAX,
1896 this NV is in the preserved range, therefore: */
1897 if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))
1899 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);
1903 0 0 already failed to read UV.
1904 0 1 already failed to read UV.
1905 1 0 you won't get here in this case. IV/UV
1906 slot set, public IOK, Atof() unneeded.
1907 1 1 already read UV.
1908 so there's no point in sv_2iuv_non_preserve() attempting
1909 to use atol, strtol, strtoul etc. */
1910 if (sv_2iuv_non_preserve (sv, numtype)
1911 >= IS_NUMBER_OVERFLOW_IV)
1915 #endif /* NV_PRESERVES_UV */
1918 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
1920 if (SvTYPE(sv) < SVt_IV)
1921 /* Typically the caller expects that sv_any is not NULL now. */
1922 sv_upgrade(sv, SVt_IV);
1925 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"IVdf")\n",
1926 PTR2UV(sv),SvIVX(sv)));
1927 return SvIsUV(sv) ? (IV)SvUVX(sv) : SvIVX(sv);
1931 Perl_sv_2uv(pTHX_ register SV *sv)
1935 if (SvGMAGICAL(sv)) {
1940 return U_V(SvNVX(sv));
1941 if (SvPOKp(sv) && SvLEN(sv))
1944 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
1945 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
1951 if (SvTHINKFIRST(sv)) {
1954 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
1955 (SvTYPE(tmpstr) != SVt_RV || (SvRV(tmpstr) != SvRV(sv))))
1956 return SvUV(tmpstr);
1957 return PTR2UV(SvRV(sv));
1959 if (SvREADONLY(sv) && SvFAKE(sv)) {
1960 sv_force_normal(sv);
1962 if (SvREADONLY(sv) && !SvOK(sv)) {
1963 if (ckWARN(WARN_UNINITIALIZED))
1973 return (UV)SvIVX(sv);
1977 /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv
1978 * without also getting a cached IV/UV from it at the same time
1979 * (ie PV->NV conversion should detect loss of accuracy and cache
1980 * IV or UV at same time to avoid this. */
1981 /* IV-over-UV optimisation - choose to cache IV if possible */
1983 if (SvTYPE(sv) == SVt_NV)
1984 sv_upgrade(sv, SVt_PVNV);
1986 (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */
1987 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
1988 SvIVX(sv) = I_V(SvNVX(sv));
1989 if (SvNVX(sv) == (NV) SvIVX(sv)
1990 #ifndef NV_PRESERVES_UV
1991 && (((UV)1 << NV_PRESERVES_UV_BITS) >
1992 (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv)))
1993 /* Don't flag it as "accurately an integer" if the number
1994 came from a (by definition imprecise) NV operation, and
1995 we're outside the range of NV integer precision */
1998 SvIOK_on(sv); /* Can this go wrong with rounding? NWC */
1999 DEBUG_c(PerlIO_printf(Perl_debug_log,
2000 "0x%"UVxf" uv(%g => %"IVdf") (precise)\n",
2006 /* IV not precise. No need to convert from PV, as NV
2007 conversion would already have cached IV if it detected
2008 that PV->IV would be better than PV->NV->IV
2009 flags already correct - don't set public IOK. */
2010 DEBUG_c(PerlIO_printf(Perl_debug_log,
2011 "0x%"UVxf" uv(%g => %"IVdf") (imprecise)\n",
2016 /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN,
2017 but the cast (NV)IV_MIN rounds to a the value less (more
2018 negative) than IV_MIN which happens to be equal to SvNVX ??
2019 Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and
2020 NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and
2021 (NV)UVX == NVX are both true, but the values differ. :-(
2022 Hopefully for 2s complement IV_MIN is something like
2023 0x8000000000000000 which will be exact. NWC */
2026 SvUVX(sv) = U_V(SvNVX(sv));
2028 (SvNVX(sv) == (NV) SvUVX(sv))
2029 #ifndef NV_PRESERVES_UV
2030 /* Make sure it's not 0xFFFFFFFFFFFFFFFF */
2031 /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */
2032 && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv))
2033 /* Don't flag it as "accurately an integer" if the number
2034 came from a (by definition imprecise) NV operation, and
2035 we're outside the range of NV integer precision */
2040 DEBUG_c(PerlIO_printf(Perl_debug_log,
2041 "0x%"UVxf" 2uv(%"UVuf" => %"IVdf") (as unsigned)\n",
2047 else if (SvPOKp(sv) && SvLEN(sv)) {
2049 int numtype = grok_number(SvPVX(sv), SvCUR(sv), &value);
2051 /* We want to avoid a possible problem when we cache a UV which
2052 may be later translated to an NV, and the resulting NV is not
2053 the translation of the initial data.
2055 This means that if we cache such a UV, we need to cache the
2056 NV as well. Moreover, we trade speed for space, and do not
2057 cache the NV if not needed.
2060 /* SVt_PVNV is one higher than SVt_PVIV, hence this order */
2061 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2062 == IS_NUMBER_IN_UV) {
2063 /* It's defintately an integer, only upgrade to PVIV */
2064 if (SvTYPE(sv) < SVt_PVIV)
2065 sv_upgrade(sv, SVt_PVIV);
2067 } else if (SvTYPE(sv) < SVt_PVNV)
2068 sv_upgrade(sv, SVt_PVNV);
2070 /* If NV preserves UV then we only use the UV value if we know that
2071 we aren't going to call atof() below. If NVs don't preserve UVs
2072 then the value returned may have more precision than atof() will
2073 return, even though it isn't accurate. */
2074 if ((numtype & (IS_NUMBER_IN_UV
2075 #ifdef NV_PRESERVES_UV
2078 )) == IS_NUMBER_IN_UV) {
2079 /* This won't turn off the public IOK flag if it was set above */
2080 (void)SvIOKp_on(sv);
2082 if (!(numtype & IS_NUMBER_NEG)) {
2084 if (value <= (UV)IV_MAX) {
2085 SvIVX(sv) = (IV)value;
2087 /* it didn't overflow, and it was positive. */
2092 /* 2s complement assumption */
2093 if (value <= (UV)IV_MIN) {
2094 SvIVX(sv) = -(IV)value;
2096 /* Too negative for an IV. This is a double upgrade, but
2097 I'm assuming it will be be rare. */
2098 if (SvTYPE(sv) < SVt_PVNV)
2099 sv_upgrade(sv, SVt_PVNV);
2103 SvNVX(sv) = -(NV)value;
2109 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2110 != IS_NUMBER_IN_UV) {
2111 /* It wasn't an integer, or it overflowed the UV. */
2112 SvNVX(sv) = Atof(SvPVX(sv));
2114 if (! numtype && ckWARN(WARN_NUMERIC))
2117 #if defined(USE_LONG_DOUBLE)
2118 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%" PERL_PRIgldbl ")\n",
2119 PTR2UV(sv), SvNVX(sv)));
2121 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%g)\n",
2122 PTR2UV(sv), SvNVX(sv)));
2125 #ifdef NV_PRESERVES_UV
2126 (void)SvIOKp_on(sv);
2128 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2129 SvIVX(sv) = I_V(SvNVX(sv));
2130 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
2133 /* Integer is imprecise. NOK, IOKp */
2135 /* UV will not work better than IV */
2137 if (SvNVX(sv) > (NV)UV_MAX) {
2139 /* Integer is inaccurate. NOK, IOKp, is UV */
2143 SvUVX(sv) = U_V(SvNVX(sv));
2144 /* 0xFFFFFFFFFFFFFFFF not an issue in here, NVs
2145 NV preservse UV so can do correct comparison. */
2146 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
2150 /* Integer is imprecise. NOK, IOKp, is UV */
2155 #else /* NV_PRESERVES_UV */
2156 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2157 == (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) {
2158 /* The UV slot will have been set from value returned by
2159 grok_number above. The NV slot has just been set using
2162 assert (SvIOKp(sv));
2164 if (((UV)1 << NV_PRESERVES_UV_BITS) >
2165 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
2166 /* Small enough to preserve all bits. */
2167 (void)SvIOKp_on(sv);
2169 SvIVX(sv) = I_V(SvNVX(sv));
2170 if ((NV)(SvIVX(sv)) == SvNVX(sv))
2172 /* Assumption: first non-preserved integer is < IV_MAX,
2173 this NV is in the preserved range, therefore: */
2174 if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))
2176 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);
2179 sv_2iuv_non_preserve (sv, numtype);
2181 #endif /* NV_PRESERVES_UV */
2185 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
2186 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
2189 if (SvTYPE(sv) < SVt_IV)
2190 /* Typically the caller expects that sv_any is not NULL now. */
2191 sv_upgrade(sv, SVt_IV);
2195 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"UVuf")\n",
2196 PTR2UV(sv),SvUVX(sv)));
2197 return SvIsUV(sv) ? SvUVX(sv) : (UV)SvIVX(sv);
2201 Perl_sv_2nv(pTHX_ register SV *sv)
2205 if (SvGMAGICAL(sv)) {
2209 if (SvPOKp(sv) && SvLEN(sv)) {
2210 if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) &&
2211 !grok_number(SvPVX(sv), SvCUR(sv), NULL))
2213 return Atof(SvPVX(sv));
2217 return (NV)SvUVX(sv);
2219 return (NV)SvIVX(sv);
2222 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
2223 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
2229 if (SvTHINKFIRST(sv)) {
2232 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
2233 (SvTYPE(tmpstr) != SVt_RV || (SvRV(tmpstr) != SvRV(sv))))
2234 return SvNV(tmpstr);
2235 return PTR2NV(SvRV(sv));
2237 if (SvREADONLY(sv) && SvFAKE(sv)) {
2238 sv_force_normal(sv);
2240 if (SvREADONLY(sv) && !SvOK(sv)) {
2241 if (ckWARN(WARN_UNINITIALIZED))
2246 if (SvTYPE(sv) < SVt_NV) {
2247 if (SvTYPE(sv) == SVt_IV)
2248 sv_upgrade(sv, SVt_PVNV);
2250 sv_upgrade(sv, SVt_NV);
2251 #ifdef USE_LONG_DOUBLE
2253 STORE_NUMERIC_LOCAL_SET_STANDARD();
2254 PerlIO_printf(Perl_debug_log,
2255 "0x%"UVxf" num(%" PERL_PRIgldbl ")\n",
2256 PTR2UV(sv), SvNVX(sv));
2257 RESTORE_NUMERIC_LOCAL();
2261 STORE_NUMERIC_LOCAL_SET_STANDARD();
2262 PerlIO_printf(Perl_debug_log, "0x%"UVxf" num(%g)\n",
2263 PTR2UV(sv), SvNVX(sv));
2264 RESTORE_NUMERIC_LOCAL();
2268 else if (SvTYPE(sv) < SVt_PVNV)
2269 sv_upgrade(sv, SVt_PVNV);
2270 if (SvNOKp(sv) && !(SvIOK(sv) || SvPOK(sv))) {
2273 else if (SvIOKp(sv)) {
2274 SvNVX(sv) = SvIsUV(sv) ? (NV)SvUVX(sv) : (NV)SvIVX(sv);
2275 #ifdef NV_PRESERVES_UV
2278 /* Only set the public NV OK flag if this NV preserves the IV */
2279 /* Check it's not 0xFFFFFFFFFFFFFFFF */
2280 if (SvIsUV(sv) ? ((SvUVX(sv) != UV_MAX)&&(SvUVX(sv) == U_V(SvNVX(sv))))
2281 : (SvIVX(sv) == I_V(SvNVX(sv))))
2287 else if (SvPOKp(sv) && SvLEN(sv)) {
2289 int numtype = grok_number(SvPVX(sv), SvCUR(sv), &value);
2290 if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) && !numtype)
2292 #ifdef NV_PRESERVES_UV
2293 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2294 == IS_NUMBER_IN_UV) {
2295 /* It's defintately an integer */
2296 SvNVX(sv) = (numtype & IS_NUMBER_NEG) ? -(NV)value : (NV)value;
2298 SvNVX(sv) = Atof(SvPVX(sv));
2301 SvNVX(sv) = Atof(SvPVX(sv));
2302 /* Only set the public NV OK flag if this NV preserves the value in
2303 the PV at least as well as an IV/UV would.
2304 Not sure how to do this 100% reliably. */
2305 /* if that shift count is out of range then Configure's test is
2306 wonky. We shouldn't be in here with NV_PRESERVES_UV_BITS ==
2308 if (((UV)1 << NV_PRESERVES_UV_BITS) >
2309 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
2310 SvNOK_on(sv); /* Definitely small enough to preserve all bits */
2311 } else if (!(numtype & IS_NUMBER_IN_UV)) {
2312 /* Can't use strtol etc to convert this string, so don't try.
2313 sv_2iv and sv_2uv will use the NV to convert, not the PV. */
2316 /* value has been set. It may not be precise. */
2317 if ((numtype & IS_NUMBER_NEG) && (value > (UV)IV_MIN)) {
2318 /* 2s complement assumption for (UV)IV_MIN */
2319 SvNOK_on(sv); /* Integer is too negative. */
2324 if (numtype & IS_NUMBER_NEG) {
2325 SvIVX(sv) = -(IV)value;
2326 } else if (value <= (UV)IV_MAX) {
2327 SvIVX(sv) = (IV)value;
2333 if (numtype & IS_NUMBER_NOT_INT) {
2334 /* I believe that even if the original PV had decimals,
2335 they are lost beyond the limit of the FP precision.
2336 However, neither is canonical, so both only get p
2337 flags. NWC, 2000/11/25 */
2338 /* Both already have p flags, so do nothing */
2341 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2342 if (SvIVX(sv) == I_V(nv)) {
2347 /* It had no "." so it must be integer. */
2350 /* between IV_MAX and NV(UV_MAX).
2351 Could be slightly > UV_MAX */
2353 if (numtype & IS_NUMBER_NOT_INT) {
2354 /* UV and NV both imprecise. */
2356 UV nv_as_uv = U_V(nv);
2358 if (value == nv_as_uv && SvUVX(sv) != UV_MAX) {
2369 #endif /* NV_PRESERVES_UV */
2372 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
2374 if (SvTYPE(sv) < SVt_NV)
2375 /* Typically the caller expects that sv_any is not NULL now. */
2376 /* XXX Ilya implies that this is a bug in callers that assume this
2377 and ideally should be fixed. */
2378 sv_upgrade(sv, SVt_NV);
2381 #if defined(USE_LONG_DOUBLE)
2383 STORE_NUMERIC_LOCAL_SET_STANDARD();
2384 PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2nv(%" PERL_PRIgldbl ")\n",
2385 PTR2UV(sv), SvNVX(sv));
2386 RESTORE_NUMERIC_LOCAL();
2390 STORE_NUMERIC_LOCAL_SET_STANDARD();
2391 PerlIO_printf(Perl_debug_log, "0x%"UVxf" 1nv(%g)\n",
2392 PTR2UV(sv), SvNVX(sv));
2393 RESTORE_NUMERIC_LOCAL();
2399 /* Caller must validate PVX */
2401 S_asIV(pTHX_ SV *sv)
2404 int numtype = grok_number(SvPVX(sv), SvCUR(sv), &value);
2406 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2407 == IS_NUMBER_IN_UV) {
2408 /* It's defintately an integer */
2409 if (numtype & IS_NUMBER_NEG) {
2410 if (value < (UV)IV_MIN)
2413 if (value < (UV)IV_MAX)
2418 if (ckWARN(WARN_NUMERIC))
2421 return I_V(Atof(SvPVX(sv)));
2425 S_asUV(pTHX_ SV *sv)
2428 int numtype = grok_number(SvPVX(sv), SvCUR(sv), &value);
2430 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2431 == IS_NUMBER_IN_UV) {
2432 /* It's defintately an integer */
2433 if (!(numtype & IS_NUMBER_NEG))
2437 if (ckWARN(WARN_NUMERIC))
2440 return U_V(Atof(SvPVX(sv)));
2444 Perl_sv_2pv_nolen(pTHX_ register SV *sv)
2447 return sv_2pv(sv, &n_a);
2450 /* We assume that buf is at least TYPE_CHARS(UV) long. */
2452 uiv_2buf(char *buf, IV iv, UV uv, int is_uv, char **peob)
2454 char *ptr = buf + TYPE_CHARS(UV);
2468 *--ptr = '0' + (uv % 10);
2477 Perl_sv_2pv(pTHX_ register SV *sv, STRLEN *lp)
2479 return sv_2pv_flags(sv, lp, SV_GMAGIC);
2483 Perl_sv_2pv_flags(pTHX_ register SV *sv, STRLEN *lp, I32 flags)
2488 char tbuf[64]; /* Must fit sprintf/Gconvert of longest IV/NV */
2489 char *tmpbuf = tbuf;
2495 if (SvGMAGICAL(sv)) {
2496 if (flags & SV_GMAGIC)
2504 (void)sprintf(tmpbuf,"%"UVuf, (UV)SvUVX(sv));
2506 (void)sprintf(tmpbuf,"%"IVdf, (IV)SvIVX(sv));
2511 Gconvert(SvNVX(sv), NV_DIG, 0, tmpbuf);
2516 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
2517 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
2524 if (SvTHINKFIRST(sv)) {
2527 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,string)) &&
2528 (SvTYPE(tmpstr) != SVt_RV || (SvRV(tmpstr) != SvRV(sv))))
2529 return SvPV(tmpstr,*lp);
2536 switch (SvTYPE(sv)) {
2538 if ( ((SvFLAGS(sv) &
2539 (SVs_OBJECT|SVf_OK|SVs_GMG|SVs_SMG|SVs_RMG))
2540 == (SVs_OBJECT|SVs_RMG))
2541 && strEQ(s=HvNAME(SvSTASH(sv)), "Regexp")
2542 && (mg = mg_find(sv, PERL_MAGIC_qr))) {
2543 regexp *re = (regexp *)mg->mg_obj;
2546 char *fptr = "msix";
2551 U16 reganch = (re->reganch & PMf_COMPILETIME) >> 12;
2553 while((ch = *fptr++)) {
2555 reflags[left++] = ch;
2558 reflags[right--] = ch;
2563 reflags[left] = '-';
2567 mg->mg_len = re->prelen + 4 + left;
2568 New(616, mg->mg_ptr, mg->mg_len + 1 + left, char);
2569 Copy("(?", mg->mg_ptr, 2, char);
2570 Copy(reflags, mg->mg_ptr+2, left, char);
2571 Copy(":", mg->mg_ptr+left+2, 1, char);
2572 Copy(re->precomp, mg->mg_ptr+3+left, re->prelen, char);
2573 mg->mg_ptr[mg->mg_len - 1] = ')';
2574 mg->mg_ptr[mg->mg_len] = 0;
2576 PL_reginterp_cnt += re->program[0].next_off;
2588 case SVt_PVBM: if (SvROK(sv))
2591 s = "SCALAR"; break;
2592 case SVt_PVLV: s = "LVALUE"; break;
2593 case SVt_PVAV: s = "ARRAY"; break;
2594 case SVt_PVHV: s = "HASH"; break;
2595 case SVt_PVCV: s = "CODE"; break;
2596 case SVt_PVGV: s = "GLOB"; break;
2597 case SVt_PVFM: s = "FORMAT"; break;
2598 case SVt_PVIO: s = "IO"; break;
2599 default: s = "UNKNOWN"; break;
2603 Perl_sv_setpvf(aTHX_ tsv, "%s=%s", HvNAME(SvSTASH(sv)), s);
2606 Perl_sv_catpvf(aTHX_ tsv, "(0x%"UVxf")", PTR2UV(sv));
2612 if (SvREADONLY(sv) && !SvOK(sv)) {
2613 if (ckWARN(WARN_UNINITIALIZED))
2619 if (SvIOK(sv) || ((SvIOKp(sv) && !SvNOKp(sv)))) {
2620 /* I'm assuming that if both IV and NV are equally valid then
2621 converting the IV is going to be more efficient */
2622 U32 isIOK = SvIOK(sv);
2623 U32 isUIOK = SvIsUV(sv);
2624 char buf[TYPE_CHARS(UV)];
2627 if (SvTYPE(sv) < SVt_PVIV)
2628 sv_upgrade(sv, SVt_PVIV);
2630 ptr = uiv_2buf(buf, 0, SvUVX(sv), 1, &ebuf);
2632 ptr = uiv_2buf(buf, SvIVX(sv), 0, 0, &ebuf);
2633 SvGROW(sv, ebuf - ptr + 1); /* inlined from sv_setpvn */
2634 Move(ptr,SvPVX(sv),ebuf - ptr,char);
2635 SvCUR_set(sv, ebuf - ptr);
2645 else if (SvNOKp(sv)) {
2646 if (SvTYPE(sv) < SVt_PVNV)
2647 sv_upgrade(sv, SVt_PVNV);
2648 /* The +20 is pure guesswork. Configure test needed. --jhi */
2649 SvGROW(sv, NV_DIG + 20);
2651 olderrno = errno; /* some Xenix systems wipe out errno here */
2653 if (SvNVX(sv) == 0.0)
2654 (void)strcpy(s,"0");
2658 Gconvert(SvNVX(sv), NV_DIG, 0, s);
2661 #ifdef FIXNEGATIVEZERO
2662 if (*s == '-' && s[1] == '0' && !s[2])
2672 if (ckWARN(WARN_UNINITIALIZED)
2673 && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
2676 if (SvTYPE(sv) < SVt_PV)
2677 /* Typically the caller expects that sv_any is not NULL now. */
2678 sv_upgrade(sv, SVt_PV);
2681 *lp = s - SvPVX(sv);
2684 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n",
2685 PTR2UV(sv),SvPVX(sv)));
2689 if (SvROK(sv)) { /* XXX Skip this when sv_pvn_force calls */
2690 /* Sneaky stuff here */
2694 tsv = newSVpv(tmpbuf, 0);
2710 len = strlen(tmpbuf);
2712 #ifdef FIXNEGATIVEZERO
2713 if (len == 2 && t[0] == '-' && t[1] == '0') {
2718 (void)SvUPGRADE(sv, SVt_PV);
2720 s = SvGROW(sv, len + 1);
2729 Perl_sv_2pvbyte_nolen(pTHX_ register SV *sv)
2732 return sv_2pvbyte(sv, &n_a);
2736 Perl_sv_2pvbyte(pTHX_ register SV *sv, STRLEN *lp)
2738 sv_utf8_downgrade(sv,0);
2739 return SvPV(sv,*lp);
2743 Perl_sv_2pvutf8_nolen(pTHX_ register SV *sv)
2746 return sv_2pvutf8(sv, &n_a);
2750 Perl_sv_2pvutf8(pTHX_ register SV *sv, STRLEN *lp)
2752 sv_utf8_upgrade(sv);
2753 return SvPV(sv,*lp);
2756 /* This function is only called on magical items */
2758 Perl_sv_2bool(pTHX_ register SV *sv)
2767 if (SvAMAGIC(sv) && (tmpsv=AMG_CALLun(sv,bool_)) &&
2768 (SvTYPE(tmpsv) != SVt_RV || (SvRV(tmpsv) != SvRV(sv))))
2769 return SvTRUE(tmpsv);
2770 return SvRV(sv) != 0;
2773 register XPV* Xpvtmp;
2774 if ((Xpvtmp = (XPV*)SvANY(sv)) &&
2775 (*Xpvtmp->xpv_pv > '0' ||
2776 Xpvtmp->xpv_cur > 1 ||
2777 (Xpvtmp->xpv_cur && *Xpvtmp->xpv_pv != '0')))
2784 return SvIVX(sv) != 0;
2787 return SvNVX(sv) != 0.0;
2795 =for apidoc sv_utf8_upgrade
2797 Convert the PV of an SV to its UTF8-encoded form.
2798 Forces the SV to string form it it is not already.
2799 Always sets the SvUTF8 flag to avoid future validity checks even
2800 if all the bytes have hibit clear.
2806 Perl_sv_utf8_upgrade(pTHX_ register SV *sv)
2808 return sv_utf8_upgrade_flags(sv, SV_GMAGIC);
2812 =for apidoc sv_utf8_upgrade_flags
2814 Convert the PV of an SV to its UTF8-encoded form.
2815 Forces the SV to string form it it is not already.
2816 Always sets the SvUTF8 flag to avoid future validity checks even
2817 if all the bytes have hibit clear. If C<flags> has C<SV_GMAGIC> bit set,
2818 will C<mg_get> on C<sv> if appropriate, else not. C<sv_utf8_upgrade> and
2819 C<sv_utf8_upgrade_nomg> are implemented in terms of this function.
2825 Perl_sv_utf8_upgrade_flags(pTHX_ register SV *sv, I32 flags)
2835 (void) sv_2pv_flags(sv,&len, flags);
2843 if (SvREADONLY(sv) && SvFAKE(sv)) {
2844 sv_force_normal(sv);
2847 /* This function could be much more efficient if we had a FLAG in SVs
2848 * to signal if there are any hibit chars in the PV.
2849 * Given that there isn't make loop fast as possible
2851 s = (U8 *) SvPVX(sv);
2852 e = (U8 *) SvEND(sv);
2856 if ((hibit = !NATIVE_IS_INVARIANT(ch)))
2862 len = SvCUR(sv) + 1; /* Plus the \0 */
2863 SvPVX(sv) = (char*)bytes_to_utf8((U8*)s, &len);
2864 SvCUR(sv) = len - 1;
2866 Safefree(s); /* No longer using what was there before. */
2867 SvLEN(sv) = len; /* No longer know the real size. */
2869 /* Mark as UTF-8 even if no hibit - saves scanning loop */
2875 =for apidoc sv_utf8_downgrade
2877 Attempt to convert the PV of an SV from UTF8-encoded to byte encoding.
2878 This may not be possible if the PV contains non-byte encoding characters;
2879 if this is the case, either returns false or, if C<fail_ok> is not
2886 Perl_sv_utf8_downgrade(pTHX_ register SV* sv, bool fail_ok)
2888 if (SvPOK(sv) && SvUTF8(sv)) {
2893 if (SvREADONLY(sv) && SvFAKE(sv))
2894 sv_force_normal(sv);
2895 s = (U8 *) SvPV(sv, len);
2896 if (!utf8_to_bytes(s, &len)) {
2899 #ifdef USE_BYTES_DOWNGRADES
2900 else if (IN_BYTES) {
2902 U8 *e = (U8 *) SvEND(sv);
2905 UV ch = utf8n_to_uvchr(s,(e-s),&len,0);
2906 if (first && ch > 255) {
2908 Perl_warner(aTHX_ WARN_UTF8, "Wide character in byte %s",
2909 PL_op_desc[PL_op->op_type]);
2911 Perl_warner(aTHX_ WARN_UTF8, "Wide character in byte");
2918 len = (d - (U8 *) SvPVX(sv));
2923 Perl_croak(aTHX_ "Wide character in %s",
2924 PL_op_desc[PL_op->op_type]);
2926 Perl_croak(aTHX_ "Wide character");
2937 =for apidoc sv_utf8_encode
2939 Convert the PV of an SV to UTF8-encoded, but then turn off the C<SvUTF8>
2940 flag so that it looks like octets again. Used as a building block
2941 for encode_utf8 in Encode.xs
2947 Perl_sv_utf8_encode(pTHX_ register SV *sv)
2949 (void) sv_utf8_upgrade(sv);
2954 =for apidoc sv_utf8_decode
2956 Convert the octets in the PV from UTF-8 to chars. Scan for validity and then
2957 turn of SvUTF8 if needed so that we see characters. Used as a building block
2958 for decode_utf8 in Encode.xs
2966 Perl_sv_utf8_decode(pTHX_ register SV *sv)
2972 /* The octets may have got themselves encoded - get them back as bytes */
2973 if (!sv_utf8_downgrade(sv, TRUE))
2976 /* it is actually just a matter of turning the utf8 flag on, but
2977 * we want to make sure everything inside is valid utf8 first.
2979 c = (U8 *) SvPVX(sv);
2980 if (!is_utf8_string(c, SvCUR(sv)+1))
2982 e = (U8 *) SvEND(sv);
2985 if (!UTF8_IS_INVARIANT(ch)) {
2995 /* Note: sv_setsv() should not be called with a source string that needs
2996 * to be reused, since it may destroy the source string if it is marked
3001 =for apidoc sv_setsv
3003 Copies the contents of the source SV C<ssv> into the destination SV C<dsv>.
3004 The source SV may be destroyed if it is mortal. Does not handle 'set'
3005 magic. See the macro forms C<SvSetSV>, C<SvSetSV_nosteal> and
3011 /* sv_setsv() is aliased to Perl_sv_setsv_macro; this function provided
3012 for binary compatibility only
3015 Perl_sv_setsv(pTHX_ SV *dstr, register SV *sstr)
3017 sv_setsv_flags(dstr, sstr, SV_GMAGIC);
3021 =for apidoc sv_setsv_flags
3023 Copies the contents of the source SV C<ssv> into the destination SV C<dsv>.
3024 The source SV may be destroyed if it is mortal. Does not handle 'set'
3025 magic. If C<flags> has C<SV_GMAGIC> bit set, will C<mg_get> on C<ssv> if
3026 appropriate, else not. C<sv_setsv> and C<sv_setsv_nomg> are implemented
3027 in terms of this function.
3033 Perl_sv_setsv_flags(pTHX_ SV *dstr, register SV *sstr, I32 flags)
3035 register U32 sflags;
3041 SV_CHECK_THINKFIRST(dstr);
3043 sstr = &PL_sv_undef;
3044 stype = SvTYPE(sstr);
3045 dtype = SvTYPE(dstr);
3049 /* There's a lot of redundancy below but we're going for speed here */
3054 if (dtype != SVt_PVGV) {
3055 (void)SvOK_off(dstr);
3063 sv_upgrade(dstr, SVt_IV);
3066 sv_upgrade(dstr, SVt_PVNV);
3070 sv_upgrade(dstr, SVt_PVIV);
3073 (void)SvIOK_only(dstr);
3074 SvIVX(dstr) = SvIVX(sstr);
3077 if (SvTAINTED(sstr))
3088 sv_upgrade(dstr, SVt_NV);
3093 sv_upgrade(dstr, SVt_PVNV);
3096 SvNVX(dstr) = SvNVX(sstr);
3097 (void)SvNOK_only(dstr);
3098 if (SvTAINTED(sstr))
3106 sv_upgrade(dstr, SVt_RV);
3107 else if (dtype == SVt_PVGV &&
3108 SvTYPE(SvRV(sstr)) == SVt_PVGV) {
3111 if (GvIMPORTED(dstr) != GVf_IMPORTED
3112 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3114 GvIMPORTED_on(dstr);
3125 sv_upgrade(dstr, SVt_PV);
3128 if (dtype < SVt_PVIV)
3129 sv_upgrade(dstr, SVt_PVIV);
3132 if (dtype < SVt_PVNV)
3133 sv_upgrade(dstr, SVt_PVNV);
3140 Perl_croak(aTHX_ "Bizarre copy of %s in %s", sv_reftype(sstr, 0),
3141 PL_op_name[PL_op->op_type]);
3143 Perl_croak(aTHX_ "Bizarre copy of %s", sv_reftype(sstr, 0));
3147 if (dtype <= SVt_PVGV) {
3149 if (dtype != SVt_PVGV) {
3150 char *name = GvNAME(sstr);
3151 STRLEN len = GvNAMELEN(sstr);
3152 sv_upgrade(dstr, SVt_PVGV);
3153 sv_magic(dstr, dstr, PERL_MAGIC_glob, Nullch, 0);
3154 GvSTASH(dstr) = (HV*)SvREFCNT_inc(GvSTASH(sstr));
3155 GvNAME(dstr) = savepvn(name, len);
3156 GvNAMELEN(dstr) = len;
3157 SvFAKE_on(dstr); /* can coerce to non-glob */
3159 /* ahem, death to those who redefine active sort subs */
3160 else if (PL_curstackinfo->si_type == PERLSI_SORT
3161 && GvCV(dstr) && PL_sortcop == CvSTART(GvCV(dstr)))
3162 Perl_croak(aTHX_ "Can't redefine active sort subroutine %s",
3165 #ifdef GV_SHARED_CHECK
3166 if (GvSHARED((GV*)dstr)) {
3167 Perl_croak(aTHX_ PL_no_modify);
3171 (void)SvOK_off(dstr);
3172 GvINTRO_off(dstr); /* one-shot flag */
3174 GvGP(dstr) = gp_ref(GvGP(sstr));
3175 if (SvTAINTED(sstr))
3177 if (GvIMPORTED(dstr) != GVf_IMPORTED
3178 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3180 GvIMPORTED_on(dstr);
3188 if (SvGMAGICAL(sstr) && (flags & SV_GMAGIC)) {
3190 if (SvTYPE(sstr) != stype) {
3191 stype = SvTYPE(sstr);
3192 if (stype == SVt_PVGV && dtype <= SVt_PVGV)
3196 if (stype == SVt_PVLV)
3197 (void)SvUPGRADE(dstr, SVt_PVNV);
3199 (void)SvUPGRADE(dstr, stype);
3202 sflags = SvFLAGS(sstr);
3204 if (sflags & SVf_ROK) {
3205 if (dtype >= SVt_PV) {
3206 if (dtype == SVt_PVGV) {
3207 SV *sref = SvREFCNT_inc(SvRV(sstr));
3209 int intro = GvINTRO(dstr);
3211 #ifdef GV_SHARED_CHECK
3212 if (GvSHARED((GV*)dstr)) {
3213 Perl_croak(aTHX_ PL_no_modify);
3218 GvINTRO_off(dstr); /* one-shot flag */
3219 GvLINE(dstr) = CopLINE(PL_curcop);
3220 GvEGV(dstr) = (GV*)dstr;
3223 switch (SvTYPE(sref)) {
3226 SAVESPTR(GvAV(dstr));
3228 dref = (SV*)GvAV(dstr);
3229 GvAV(dstr) = (AV*)sref;
3230 if (!GvIMPORTED_AV(dstr)
3231 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3233 GvIMPORTED_AV_on(dstr);
3238 SAVESPTR(GvHV(dstr));
3240 dref = (SV*)GvHV(dstr);
3241 GvHV(dstr) = (HV*)sref;
3242 if (!GvIMPORTED_HV(dstr)
3243 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3245 GvIMPORTED_HV_on(dstr);
3250 if (GvCVGEN(dstr) && GvCV(dstr) != (CV*)sref) {
3251 SvREFCNT_dec(GvCV(dstr));
3252 GvCV(dstr) = Nullcv;
3253 GvCVGEN(dstr) = 0; /* Switch off cacheness. */
3254 PL_sub_generation++;
3256 SAVESPTR(GvCV(dstr));
3259 dref = (SV*)GvCV(dstr);
3260 if (GvCV(dstr) != (CV*)sref) {
3261 CV* cv = GvCV(dstr);
3263 if (!GvCVGEN((GV*)dstr) &&
3264 (CvROOT(cv) || CvXSUB(cv)))
3266 /* ahem, death to those who redefine
3267 * active sort subs */
3268 if (PL_curstackinfo->si_type == PERLSI_SORT &&
3269 PL_sortcop == CvSTART(cv))
3271 "Can't redefine active sort subroutine %s",
3272 GvENAME((GV*)dstr));
3273 /* Redefining a sub - warning is mandatory if
3274 it was a const and its value changed. */
3275 if (ckWARN(WARN_REDEFINE)
3277 && (!CvCONST((CV*)sref)
3278 || sv_cmp(cv_const_sv(cv),
3279 cv_const_sv((CV*)sref)))))
3281 Perl_warner(aTHX_ WARN_REDEFINE,
3283 ? "Constant subroutine %s redefined"
3284 : "Subroutine %s redefined",
3285 GvENAME((GV*)dstr));
3288 cv_ckproto(cv, (GV*)dstr,
3289 SvPOK(sref) ? SvPVX(sref) : Nullch);
3291 GvCV(dstr) = (CV*)sref;
3292 GvCVGEN(dstr) = 0; /* Switch off cacheness. */
3293 GvASSUMECV_on(dstr);
3294 PL_sub_generation++;
3296 if (!GvIMPORTED_CV(dstr)
3297 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3299 GvIMPORTED_CV_on(dstr);
3304 SAVESPTR(GvIOp(dstr));
3306 dref = (SV*)GvIOp(dstr);
3307 GvIOp(dstr) = (IO*)sref;
3311 SAVESPTR(GvFORM(dstr));
3313 dref = (SV*)GvFORM(dstr);
3314 GvFORM(dstr) = (CV*)sref;
3318 SAVESPTR(GvSV(dstr));
3320 dref = (SV*)GvSV(dstr);
3322 if (!GvIMPORTED_SV(dstr)
3323 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3325 GvIMPORTED_SV_on(dstr);
3333 if (SvTAINTED(sstr))
3338 (void)SvOOK_off(dstr); /* backoff */
3340 Safefree(SvPVX(dstr));
3341 SvLEN(dstr)=SvCUR(dstr)=0;
3344 (void)SvOK_off(dstr);
3345 SvRV(dstr) = SvREFCNT_inc(SvRV(sstr));
3347 if (sflags & SVp_NOK) {
3349 /* Only set the public OK flag if the source has public OK. */
3350 if (sflags & SVf_NOK)
3351 SvFLAGS(dstr) |= SVf_NOK;
3352 SvNVX(dstr) = SvNVX(sstr);
3354 if (sflags & SVp_IOK) {
3355 (void)SvIOKp_on(dstr);
3356 if (sflags & SVf_IOK)
3357 SvFLAGS(dstr) |= SVf_IOK;
3358 if (sflags & SVf_IVisUV)
3360 SvIVX(dstr) = SvIVX(sstr);
3362 if (SvAMAGIC(sstr)) {
3366 else if (sflags & SVp_POK) {
3369 * Check to see if we can just swipe the string. If so, it's a
3370 * possible small lose on short strings, but a big win on long ones.
3371 * It might even be a win on short strings if SvPVX(dstr)
3372 * has to be allocated and SvPVX(sstr) has to be freed.
3375 if (SvTEMP(sstr) && /* slated for free anyway? */
3376 SvREFCNT(sstr) == 1 && /* and no other references to it? */
3377 !(sflags & SVf_OOK) && /* and not involved in OOK hack? */
3378 SvLEN(sstr) && /* and really is a string */
3379 !(PL_op && PL_op->op_type == OP_AASSIGN)) /* and won't be needed again, potentially */
3381 if (SvPVX(dstr)) { /* we know that dtype >= SVt_PV */
3383 SvFLAGS(dstr) &= ~SVf_OOK;
3384 Safefree(SvPVX(dstr) - SvIVX(dstr));
3386 else if (SvLEN(dstr))
3387 Safefree(SvPVX(dstr));
3389 (void)SvPOK_only(dstr);
3390 SvPV_set(dstr, SvPVX(sstr));
3391 SvLEN_set(dstr, SvLEN(sstr));
3392 SvCUR_set(dstr, SvCUR(sstr));
3395 (void)SvOK_off(sstr); /* NOTE: nukes most SvFLAGS on sstr */
3396 SvPV_set(sstr, Nullch);
3401 else { /* have to copy actual string */
3402 STRLEN len = SvCUR(sstr);
3404 SvGROW(dstr, len + 1); /* inlined from sv_setpvn */
3405 Move(SvPVX(sstr),SvPVX(dstr),len,char);
3406 SvCUR_set(dstr, len);
3407 *SvEND(dstr) = '\0';
3408 (void)SvPOK_only(dstr);
3410 if (sflags & SVf_UTF8)
3413 if (sflags & SVp_NOK) {
3415 if (sflags & SVf_NOK)
3416 SvFLAGS(dstr) |= SVf_NOK;
3417 SvNVX(dstr) = SvNVX(sstr);
3419 if (sflags & SVp_IOK) {
3420 (void)SvIOKp_on(dstr);
3421 if (sflags & SVf_IOK)
3422 SvFLAGS(dstr) |= SVf_IOK;
3423 if (sflags & SVf_IVisUV)
3425 SvIVX(dstr) = SvIVX(sstr);
3428 else if (sflags & SVp_IOK) {
3429 if (sflags & SVf_IOK)
3430 (void)SvIOK_only(dstr);
3432 (void)SvOK_off(dstr);
3433 (void)SvIOKp_on(dstr);
3435 /* XXXX Do we want to set IsUV for IV(ROK)? Be extra safe... */
3436 if (sflags & SVf_IVisUV)
3438 SvIVX(dstr) = SvIVX(sstr);
3439 if (sflags & SVp_NOK) {
3440 if (sflags & SVf_NOK)
3441 (void)SvNOK_on(dstr);
3443 (void)SvNOKp_on(dstr);
3444 SvNVX(dstr) = SvNVX(sstr);
3447 else if (sflags & SVp_NOK) {
3448 if (sflags & SVf_NOK)
3449 (void)SvNOK_only(dstr);
3451 (void)SvOK_off(dstr);
3454 SvNVX(dstr) = SvNVX(sstr);
3457 if (dtype == SVt_PVGV) {
3458 if (ckWARN(WARN_MISC))
3459 Perl_warner(aTHX_ WARN_MISC, "Undefined value assigned to typeglob");
3462 (void)SvOK_off(dstr);
3464 if (SvTAINTED(sstr))
3469 =for apidoc sv_setsv_mg
3471 Like C<sv_setsv>, but also handles 'set' magic.
3477 Perl_sv_setsv_mg(pTHX_ SV *dstr, register SV *sstr)
3479 sv_setsv(dstr,sstr);
3484 =for apidoc sv_setpvn
3486 Copies a string into an SV. The C<len> parameter indicates the number of
3487 bytes to be copied. Does not handle 'set' magic. See C<sv_setpvn_mg>.
3493 Perl_sv_setpvn(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
3495 register char *dptr;
3497 SV_CHECK_THINKFIRST(sv);
3503 /* len is STRLEN which is unsigned, need to copy to signed */
3506 Perl_croak(aTHX_ "panic: sv_setpvn called with negative strlen");
3508 (void)SvUPGRADE(sv, SVt_PV);
3510 SvGROW(sv, len + 1);
3512 Move(ptr,dptr,len,char);
3515 (void)SvPOK_only_UTF8(sv); /* validate pointer */
3520 =for apidoc sv_setpvn_mg
3522 Like C<sv_setpvn>, but also handles 'set' magic.
3528 Perl_sv_setpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
3530 sv_setpvn(sv,ptr,len);
3535 =for apidoc sv_setpv
3537 Copies a string into an SV. The string must be null-terminated. Does not
3538 handle 'set' magic. See C<sv_setpv_mg>.
3544 Perl_sv_setpv(pTHX_ register SV *sv, register const char *ptr)
3546 register STRLEN len;
3548 SV_CHECK_THINKFIRST(sv);
3554 (void)SvUPGRADE(sv, SVt_PV);
3556 SvGROW(sv, len + 1);
3557 Move(ptr,SvPVX(sv),len+1,char);
3559 (void)SvPOK_only_UTF8(sv); /* validate pointer */
3564 =for apidoc sv_setpv_mg
3566 Like C<sv_setpv>, but also handles 'set' magic.
3572 Perl_sv_setpv_mg(pTHX_ register SV *sv, register const char *ptr)
3579 =for apidoc sv_usepvn
3581 Tells an SV to use C<ptr> to find its string value. Normally the string is
3582 stored inside the SV but sv_usepvn allows the SV to use an outside string.
3583 The C<ptr> should point to memory that was allocated by C<malloc>. The
3584 string length, C<len>, must be supplied. This function will realloc the
3585 memory pointed to by C<ptr>, so that pointer should not be freed or used by
3586 the programmer after giving it to sv_usepvn. Does not handle 'set' magic.
3587 See C<sv_usepvn_mg>.
3593 Perl_sv_usepvn(pTHX_ register SV *sv, register char *ptr, register STRLEN len)
3595 SV_CHECK_THINKFIRST(sv);
3596 (void)SvUPGRADE(sv, SVt_PV);
3601 (void)SvOOK_off(sv);
3602 if (SvPVX(sv) && SvLEN(sv))
3603 Safefree(SvPVX(sv));
3604 Renew(ptr, len+1, char);
3607 SvLEN_set(sv, len+1);
3609 (void)SvPOK_only_UTF8(sv); /* validate pointer */
3614 =for apidoc sv_usepvn_mg
3616 Like C<sv_usepvn>, but also handles 'set' magic.
3622 Perl_sv_usepvn_mg(pTHX_ register SV *sv, register char *ptr, register STRLEN len)
3624 sv_usepvn(sv,ptr,len);
3629 Perl_sv_force_normal_flags(pTHX_ register SV *sv, U32 flags)
3631 if (SvREADONLY(sv)) {
3633 char *pvx = SvPVX(sv);
3634 STRLEN len = SvCUR(sv);
3635 U32 hash = SvUVX(sv);
3636 SvGROW(sv, len + 1);
3637 Move(pvx,SvPVX(sv),len,char);
3641 unsharepvn(pvx,SvUTF8(sv)?-len:len,hash);
3643 else if (PL_curcop != &PL_compiling)
3644 Perl_croak(aTHX_ PL_no_modify);
3647 sv_unref_flags(sv, flags);
3648 else if (SvFAKE(sv) && SvTYPE(sv) == SVt_PVGV)
3653 Perl_sv_force_normal(pTHX_ register SV *sv)
3655 sv_force_normal_flags(sv, 0);
3661 Efficient removal of characters from the beginning of the string buffer.
3662 SvPOK(sv) must be true and the C<ptr> must be a pointer to somewhere inside
3663 the string buffer. The C<ptr> becomes the first character of the adjusted
3670 Perl_sv_chop(pTHX_ register SV *sv, register char *ptr) /* like set but assuming ptr is in sv */
3674 register STRLEN delta;
3676 if (!ptr || !SvPOKp(sv))
3678 SV_CHECK_THINKFIRST(sv);
3679 if (SvTYPE(sv) < SVt_PVIV)
3680 sv_upgrade(sv,SVt_PVIV);
3683 if (!SvLEN(sv)) { /* make copy of shared string */
3684 char *pvx = SvPVX(sv);
3685 STRLEN len = SvCUR(sv);
3686 SvGROW(sv, len + 1);
3687 Move(pvx,SvPVX(sv),len,char);
3691 SvFLAGS(sv) |= SVf_OOK;
3693 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVp_IOK|SVp_NOK|SVf_IVisUV);
3694 delta = ptr - SvPVX(sv);
3702 =for apidoc sv_catpvn
3704 Concatenates the string onto the end of the string which is in the SV. The
3705 C<len> indicates number of bytes to copy. If the SV has the UTF8
3706 status set, then the bytes appended should be valid UTF8.
3707 Handles 'get' magic, but not 'set' magic. See C<sv_catpvn_mg>.
3712 /* sv_catpvn() is aliased to Perl_sv_catpvn_macro; this function provided
3713 for binary compatibility only
3716 Perl_sv_catpvn(pTHX_ SV *dsv, const char* sstr, STRLEN slen)
3718 sv_catpvn_flags(dsv, sstr, slen, SV_GMAGIC);
3722 =for apidoc sv_catpvn_flags
3724 Concatenates the string onto the end of the string which is in the SV. The
3725 C<len> indicates number of bytes to copy. If the SV has the UTF8
3726 status set, then the bytes appended should be valid UTF8.
3727 If C<flags> has C<SV_GMAGIC> bit set, will C<mg_get> on C<dsv> if
3728 appropriate, else not. C<sv_catpvn> and C<sv_catpvn_nomg> are implemented
3729 in terms of this function.
3735 Perl_sv_catpvn_flags(pTHX_ register SV *dsv, register const char *sstr, register STRLEN slen, I32 flags)
3740 dstr = SvPV_force_flags(dsv, dlen, flags);
3741 SvGROW(dsv, dlen + slen + 1);
3744 Move(sstr, SvPVX(dsv) + dlen, slen, char);
3747 (void)SvPOK_only_UTF8(dsv); /* validate pointer */
3752 =for apidoc sv_catpvn_mg
3754 Like C<sv_catpvn>, but also handles 'set' magic.
3760 Perl_sv_catpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
3762 sv_catpvn(sv,ptr,len);
3767 =for apidoc sv_catsv
3769 Concatenates the string from SV C<ssv> onto the end of the string in
3770 SV C<dsv>. Modifies C<dsv> but not C<ssv>. Handles 'get' magic, but
3771 not 'set' magic. See C<sv_catsv_mg>.
3775 /* sv_catsv() is aliased to Perl_sv_catsv_macro; this function provided
3776 for binary compatibility only
3779 Perl_sv_catsv(pTHX_ SV *dstr, register SV *sstr)
3781 sv_catsv_flags(dstr, sstr, SV_GMAGIC);
3785 =for apidoc sv_catsv_flags
3787 Concatenates the string from SV C<ssv> onto the end of the string in
3788 SV C<dsv>. Modifies C<dsv> but not C<ssv>. If C<flags> has C<SV_GMAGIC>
3789 bit set, will C<mg_get> on the SVs if appropriate, else not. C<sv_catsv>
3790 and C<sv_catsv_nomg> are implemented in terms of this function.
3795 Perl_sv_catsv_flags(pTHX_ SV *dsv, register SV *ssv, I32 flags)
3801 if ((spv = SvPV(ssv, slen))) {
3802 bool sutf8 = DO_UTF8(ssv);
3805 if (SvGMAGICAL(dsv) && (flags & SV_GMAGIC))
3807 dutf8 = DO_UTF8(dsv);
3809 if (dutf8 != sutf8) {
3811 /* Not modifying source SV, so taking a temporary copy. */
3812 SV* csv = sv_2mortal(newSVpvn(spv, slen));
3814 sv_utf8_upgrade(csv);
3815 spv = SvPV(csv, slen);
3818 sv_utf8_upgrade_nomg(dsv);
3820 sv_catpvn_nomg(dsv, spv, slen);
3825 =for apidoc sv_catsv_mg
3827 Like C<sv_catsv>, but also handles 'set' magic.
3833 Perl_sv_catsv_mg(pTHX_ SV *dsv, register SV *ssv)
3840 =for apidoc sv_catpv
3842 Concatenates the string onto the end of the string which is in the SV.
3843 If the SV has the UTF8 status set, then the bytes appended should be
3844 valid UTF8. Handles 'get' magic, but not 'set' magic. See C<sv_catpv_mg>.
3849 Perl_sv_catpv(pTHX_ register SV *sv, register const char *ptr)
3851 register STRLEN len;
3857 junk = SvPV_force(sv, tlen);
3859 SvGROW(sv, tlen + len + 1);
3862 Move(ptr,SvPVX(sv)+tlen,len+1,char);
3864 (void)SvPOK_only_UTF8(sv); /* validate pointer */
3869 =for apidoc sv_catpv_mg
3871 Like C<sv_catpv>, but also handles 'set' magic.
3877 Perl_sv_catpv_mg(pTHX_ register SV *sv, register const char *ptr)
3884 Perl_newSV(pTHX_ STRLEN len)
3890 sv_upgrade(sv, SVt_PV);
3891 SvGROW(sv, len + 1);
3896 /* name is assumed to contain an SV* if (name && namelen == HEf_SVKEY) */
3899 =for apidoc sv_magic
3901 Adds magic to an SV.
3907 Perl_sv_magic(pTHX_ register SV *sv, SV *obj, int how, const char *name, I32 namlen)
3911 if (SvREADONLY(sv)) {
3912 if (PL_curcop != &PL_compiling
3913 /* XXX this used to be !strchr("gBf", how), which seems to
3914 * implicity be equal to !strchr("gBf\0", how), ie \0 matches
3915 * too. I find this suprising, but have hadded PERL_MAGIC_sv
3916 * to the list of things to check - DAPM 19-May-01 */
3917 && how != PERL_MAGIC_regex_global
3918 && how != PERL_MAGIC_bm
3919 && how != PERL_MAGIC_fm
3920 && how != PERL_MAGIC_sv
3923 Perl_croak(aTHX_ PL_no_modify);
3926 if (SvMAGICAL(sv) || (how == PERL_MAGIC_taint && SvTYPE(sv) >= SVt_PVMG)) {
3927 if (SvMAGIC(sv) && (mg = mg_find(sv, how))) {
3928 if (how == PERL_MAGIC_taint)
3934 (void)SvUPGRADE(sv, SVt_PVMG);
3936 Newz(702,mg, 1, MAGIC);
3937 mg->mg_moremagic = SvMAGIC(sv);
3940 /* Some magic sontains a reference loop, where the sv and object refer to
3941 each other. To prevent a avoid a reference loop that would prevent such
3942 objects being freed, we look for such loops and if we find one we avoid
3943 incrementing the object refcount. */
3944 if (!obj || obj == sv ||
3945 how == PERL_MAGIC_arylen ||
3946 how == PERL_MAGIC_qr ||
3947 (SvTYPE(obj) == SVt_PVGV &&
3948 (GvSV(obj) == sv || GvHV(obj) == (HV*)sv || GvAV(obj) == (AV*)sv ||
3949 GvCV(obj) == (CV*)sv || GvIOp(obj) == (IO*)sv ||
3950 GvFORM(obj) == (CV*)sv)))
3955 mg->mg_obj = SvREFCNT_inc(obj);
3956 mg->mg_flags |= MGf_REFCOUNTED;
3959 mg->mg_len = namlen;
3962 mg->mg_ptr = savepvn(name, namlen);
3963 else if (namlen == HEf_SVKEY)
3964 mg->mg_ptr = (char*)SvREFCNT_inc((SV*)name);
3969 mg->mg_virtual = &PL_vtbl_sv;
3971 case PERL_MAGIC_overload:
3972 mg->mg_virtual = &PL_vtbl_amagic;
3974 case PERL_MAGIC_overload_elem:
3975 mg->mg_virtual = &PL_vtbl_amagicelem;
3977 case PERL_MAGIC_overload_table:
3978 mg->mg_virtual = &PL_vtbl_ovrld;
3981 mg->mg_virtual = &PL_vtbl_bm;
3983 case PERL_MAGIC_regdata:
3984 mg->mg_virtual = &PL_vtbl_regdata;
3986 case PERL_MAGIC_regdatum:
3987 mg->mg_virtual = &PL_vtbl_regdatum;
3989 case PERL_MAGIC_env:
3990 mg->mg_virtual = &PL_vtbl_env;
3993 mg->mg_virtual = &PL_vtbl_fm;
3995 case PERL_MAGIC_envelem:
3996 mg->mg_virtual = &PL_vtbl_envelem;
3998 case PERL_MAGIC_regex_global:
3999 mg->mg_virtual = &PL_vtbl_mglob;
4001 case PERL_MAGIC_isa:
4002 mg->mg_virtual = &PL_vtbl_isa;
4004 case PERL_MAGIC_isaelem:
4005 mg->mg_virtual = &PL_vtbl_isaelem;
4007 case PERL_MAGIC_nkeys:
4008 mg->mg_virtual = &PL_vtbl_nkeys;
4010 case PERL_MAGIC_dbfile:
4014 case PERL_MAGIC_dbline:
4015 mg->mg_virtual = &PL_vtbl_dbline;
4018 case PERL_MAGIC_mutex:
4019 mg->mg_virtual = &PL_vtbl_mutex;
4021 #endif /* USE_THREADS */
4022 #ifdef USE_LOCALE_COLLATE
4023 case PERL_MAGIC_collxfrm:
4024 mg->mg_virtual = &PL_vtbl_collxfrm;
4026 #endif /* USE_LOCALE_COLLATE */
4027 case PERL_MAGIC_tied:
4028 mg->mg_virtual = &PL_vtbl_pack;
4030 case PERL_MAGIC_tiedelem:
4031 case PERL_MAGIC_tiedscalar:
4032 mg->mg_virtual = &PL_vtbl_packelem;
4035 mg->mg_virtual = &PL_vtbl_regexp;
4037 case PERL_MAGIC_sig:
4038 mg->mg_virtual = &PL_vtbl_sig;
4040 case PERL_MAGIC_sigelem:
4041 mg->mg_virtual = &PL_vtbl_sigelem;
4043 case PERL_MAGIC_taint:
4044 mg->mg_virtual = &PL_vtbl_taint;
4047 case PERL_MAGIC_uvar:
4048 mg->mg_virtual = &PL_vtbl_uvar;
4050 case PERL_MAGIC_vec:
4051 mg->mg_virtual = &PL_vtbl_vec;
4053 case PERL_MAGIC_substr:
4054 mg->mg_virtual = &PL_vtbl_substr;
4056 case PERL_MAGIC_defelem:
4057 mg->mg_virtual = &PL_vtbl_defelem;
4059 case PERL_MAGIC_glob:
4060 mg->mg_virtual = &PL_vtbl_glob;
4062 case PERL_MAGIC_arylen:
4063 mg->mg_virtual = &PL_vtbl_arylen;
4065 case PERL_MAGIC_pos:
4066 mg->mg_virtual = &PL_vtbl_pos;
4068 case PERL_MAGIC_backref:
4069 mg->mg_virtual = &PL_vtbl_backref;
4071 case PERL_MAGIC_ext:
4072 /* Reserved for use by extensions not perl internals. */
4073 /* Useful for attaching extension internal data to perl vars. */
4074 /* Note that multiple extensions may clash if magical scalars */
4075 /* etc holding private data from one are passed to another. */
4079 Perl_croak(aTHX_ "Don't know how to handle magic of type \\%o", how);
4083 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
4087 =for apidoc sv_unmagic
4089 Removes magic from an SV.
4095 Perl_sv_unmagic(pTHX_ SV *sv, int type)
4099 if (SvTYPE(sv) < SVt_PVMG || !SvMAGIC(sv))
4102 for (mg = *mgp; mg; mg = *mgp) {
4103 if (mg->mg_type == type) {
4104 MGVTBL* vtbl = mg->mg_virtual;
4105 *mgp = mg->mg_moremagic;
4106 if (vtbl && vtbl->svt_free)
4107 CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg);
4108 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
4109 if (mg->mg_len >= 0)
4110 Safefree(mg->mg_ptr);
4111 else if (mg->mg_len == HEf_SVKEY)
4112 SvREFCNT_dec((SV*)mg->mg_ptr);
4114 if (mg->mg_flags & MGf_REFCOUNTED)
4115 SvREFCNT_dec(mg->mg_obj);
4119 mgp = &mg->mg_moremagic;
4123 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_NOK|SVp_POK)) >> PRIVSHIFT;
4130 =for apidoc sv_rvweaken
4138 Perl_sv_rvweaken(pTHX_ SV *sv)
4141 if (!SvOK(sv)) /* let undefs pass */
4144 Perl_croak(aTHX_ "Can't weaken a nonreference");
4145 else if (SvWEAKREF(sv)) {
4146 if (ckWARN(WARN_MISC))
4147 Perl_warner(aTHX_ WARN_MISC, "Reference is already weak");
4151 sv_add_backref(tsv, sv);
4158 S_sv_add_backref(pTHX_ SV *tsv, SV *sv)
4162 if (SvMAGICAL(tsv) && (mg = mg_find(tsv, PERL_MAGIC_backref)))
4163 av = (AV*)mg->mg_obj;
4166 sv_magic(tsv, (SV*)av, PERL_MAGIC_backref, NULL, 0);
4167 SvREFCNT_dec(av); /* for sv_magic */
4173 S_sv_del_backref(pTHX_ SV *sv)
4180 if (!SvMAGICAL(tsv) || !(mg = mg_find(tsv, PERL_MAGIC_backref)))
4181 Perl_croak(aTHX_ "panic: del_backref");
4182 av = (AV *)mg->mg_obj;
4187 svp[i] = &PL_sv_undef; /* XXX */
4194 =for apidoc sv_insert
4196 Inserts a string at the specified offset/length within the SV. Similar to
4197 the Perl substr() function.
4203 Perl_sv_insert(pTHX_ SV *bigstr, STRLEN offset, STRLEN len, char *little, STRLEN littlelen)
4207 register char *midend;
4208 register char *bigend;
4214 Perl_croak(aTHX_ "Can't modify non-existent substring");
4215 SvPV_force(bigstr, curlen);
4216 (void)SvPOK_only_UTF8(bigstr);
4217 if (offset + len > curlen) {
4218 SvGROW(bigstr, offset+len+1);
4219 Zero(SvPVX(bigstr)+curlen, offset+len-curlen, char);
4220 SvCUR_set(bigstr, offset+len);
4224 i = littlelen - len;
4225 if (i > 0) { /* string might grow */
4226 big = SvGROW(bigstr, SvCUR(bigstr) + i + 1);
4227 mid = big + offset + len;
4228 midend = bigend = big + SvCUR(bigstr);
4231 while (midend > mid) /* shove everything down */
4232 *--bigend = *--midend;
4233 Move(little,big+offset,littlelen,char);
4239 Move(little,SvPVX(bigstr)+offset,len,char);
4244 big = SvPVX(bigstr);
4247 bigend = big + SvCUR(bigstr);
4249 if (midend > bigend)
4250 Perl_croak(aTHX_ "panic: sv_insert");
4252 if (mid - big > bigend - midend) { /* faster to shorten from end */
4254 Move(little, mid, littlelen,char);
4257 i = bigend - midend;
4259 Move(midend, mid, i,char);
4263 SvCUR_set(bigstr, mid - big);
4266 else if ((i = mid - big)) { /* faster from front */
4267 midend -= littlelen;
4269 sv_chop(bigstr,midend-i);
4274 Move(little, mid, littlelen,char);
4276 else if (littlelen) {
4277 midend -= littlelen;
4278 sv_chop(bigstr,midend);
4279 Move(little,midend,littlelen,char);
4282 sv_chop(bigstr,midend);
4288 =for apidoc sv_replace
4290 Make the first argument a copy of the second, then delete the original.
4296 Perl_sv_replace(pTHX_ register SV *sv, register SV *nsv)
4298 U32 refcnt = SvREFCNT(sv);
4299 SV_CHECK_THINKFIRST(sv);
4300 if (SvREFCNT(nsv) != 1 && ckWARN_d(WARN_INTERNAL))
4301 Perl_warner(aTHX_ WARN_INTERNAL, "Reference miscount in sv_replace()");
4302 if (SvMAGICAL(sv)) {
4306 sv_upgrade(nsv, SVt_PVMG);
4307 SvMAGIC(nsv) = SvMAGIC(sv);
4308 SvFLAGS(nsv) |= SvMAGICAL(sv);
4314 assert(!SvREFCNT(sv));
4315 StructCopy(nsv,sv,SV);
4316 SvREFCNT(sv) = refcnt;
4317 SvFLAGS(nsv) |= SVTYPEMASK; /* Mark as freed */
4322 =for apidoc sv_clear
4324 Clear an SV, making it empty. Does not free the memory used by the SV
4331 Perl_sv_clear(pTHX_ register SV *sv)
4335 assert(SvREFCNT(sv) == 0);
4338 if (PL_defstash) { /* Still have a symbol table? */
4343 Zero(&tmpref, 1, SV);
4344 sv_upgrade(&tmpref, SVt_RV);
4346 SvREADONLY_on(&tmpref); /* DESTROY() could be naughty */
4347 SvREFCNT(&tmpref) = 1;
4350 stash = SvSTASH(sv);
4351 destructor = StashHANDLER(stash,DESTROY);
4354 PUSHSTACKi(PERLSI_DESTROY);
4355 SvRV(&tmpref) = SvREFCNT_inc(sv);
4360 call_sv((SV*)destructor, G_DISCARD|G_EVAL|G_KEEPERR);
4366 } while (SvOBJECT(sv) && SvSTASH(sv) != stash);
4368 del_XRV(SvANY(&tmpref));
4371 if (PL_in_clean_objs)
4372 Perl_croak(aTHX_ "DESTROY created new reference to dead object '%s'",
4374 /* DESTROY gave object new lease on life */
4380 SvREFCNT_dec(SvSTASH(sv)); /* possibly of changed persuasion */
4381 SvOBJECT_off(sv); /* Curse the object. */
4382 if (SvTYPE(sv) != SVt_PVIO)
4383 --PL_sv_objcount; /* XXX Might want something more general */
4386 if (SvTYPE(sv) >= SVt_PVMG) {
4389 if (SvFLAGS(sv) & SVpad_TYPED)
4390 SvREFCNT_dec(SvSTASH(sv));
4393 switch (SvTYPE(sv)) {
4396 IoIFP(sv) != PerlIO_stdin() &&
4397 IoIFP(sv) != PerlIO_stdout() &&
4398 IoIFP(sv) != PerlIO_stderr())
4400 io_close((IO*)sv, FALSE);
4402 if (IoDIRP(sv) && !(IoFLAGS(sv) & IOf_FAKE_DIRP))
4403 PerlDir_close(IoDIRP(sv));
4404 IoDIRP(sv) = (DIR*)NULL;
4405 Safefree(IoTOP_NAME(sv));
4406 Safefree(IoFMT_NAME(sv));
4407 Safefree(IoBOTTOM_NAME(sv));
4422 SvREFCNT_dec(LvTARG(sv));
4426 Safefree(GvNAME(sv));
4427 /* cannot decrease stash refcount yet, as we might recursively delete
4428 ourselves when the refcnt drops to zero. Delay SvREFCNT_dec
4429 of stash until current sv is completely gone.
4430 -- JohnPC, 27 Mar 1998 */
4431 stash = GvSTASH(sv);
4437 (void)SvOOK_off(sv);
4445 SvREFCNT_dec(SvRV(sv));
4447 else if (SvPVX(sv) && SvLEN(sv))
4448 Safefree(SvPVX(sv));
4449 else if (SvPVX(sv) && SvREADONLY(sv) && SvFAKE(sv)) {
4450 unsharepvn(SvPVX(sv),SvUTF8(sv)?-SvCUR(sv):SvCUR(sv),SvUVX(sv));
4462 switch (SvTYPE(sv)) {
4478 del_XPVIV(SvANY(sv));
4481 del_XPVNV(SvANY(sv));
4484 del_XPVMG(SvANY(sv));
4487 del_XPVLV(SvANY(sv));
4490 del_XPVAV(SvANY(sv));
4493 del_XPVHV(SvANY(sv));
4496 del_XPVCV(SvANY(sv));
4499 del_XPVGV(SvANY(sv));
4500 /* code duplication for increased performance. */
4501 SvFLAGS(sv) &= SVf_BREAK;
4502 SvFLAGS(sv) |= SVTYPEMASK;
4503 /* decrease refcount of the stash that owns this GV, if any */
4505 SvREFCNT_dec(stash);
4506 return; /* not break, SvFLAGS reset already happened */
4508 del_XPVBM(SvANY(sv));
4511 del_XPVFM(SvANY(sv));
4514 del_XPVIO(SvANY(sv));
4517 SvFLAGS(sv) &= SVf_BREAK;
4518 SvFLAGS(sv) |= SVTYPEMASK;
4522 Perl_sv_newref(pTHX_ SV *sv)
4525 ATOMIC_INC(SvREFCNT(sv));
4532 Free the memory used by an SV.
4538 Perl_sv_free(pTHX_ SV *sv)
4540 int refcount_is_zero;
4544 if (SvREFCNT(sv) == 0) {
4545 if (SvFLAGS(sv) & SVf_BREAK)
4547 if (PL_in_clean_all) /* All is fair */
4549 if (SvREADONLY(sv) && SvIMMORTAL(sv)) {
4550 /* make sure SvREFCNT(sv)==0 happens very seldom */
4551 SvREFCNT(sv) = (~(U32)0)/2;
4554 if (ckWARN_d(WARN_INTERNAL))
4555 Perl_warner(aTHX_ WARN_INTERNAL, "Attempt to free unreferenced scalar");
4558 ATOMIC_DEC_AND_TEST(refcount_is_zero, SvREFCNT(sv));
4559 if (!refcount_is_zero)
4563 if (ckWARN_d(WARN_DEBUGGING))
4564 Perl_warner(aTHX_ WARN_DEBUGGING,
4565 "Attempt to free temp prematurely: SV 0x%"UVxf,
4570 if (SvREADONLY(sv) && SvIMMORTAL(sv)) {
4571 /* make sure SvREFCNT(sv)==0 happens very seldom */
4572 SvREFCNT(sv) = (~(U32)0)/2;
4583 Returns the length of the string in the SV. See also C<SvCUR>.
4589 Perl_sv_len(pTHX_ register SV *sv)
4598 len = mg_length(sv);
4600 junk = SvPV(sv, len);
4605 =for apidoc sv_len_utf8
4607 Returns the number of characters in the string in an SV, counting wide
4608 UTF8 bytes as a single character.
4614 Perl_sv_len_utf8(pTHX_ register SV *sv)
4620 return mg_length(sv);
4624 U8 *s = (U8*)SvPV(sv, len);
4626 return Perl_utf8_length(aTHX_ s, s + len);
4631 Perl_sv_pos_u2b(pTHX_ register SV *sv, I32* offsetp, I32* lenp)
4636 I32 uoffset = *offsetp;
4642 start = s = (U8*)SvPV(sv, len);
4644 while (s < send && uoffset--)
4648 *offsetp = s - start;
4652 while (s < send && ulen--)
4662 Perl_sv_pos_b2u(pTHX_ register SV *sv, I32* offsetp)
4671 s = (U8*)SvPV(sv, len);
4673 Perl_croak(aTHX_ "panic: sv_pos_b2u: bad byte offset");
4674 send = s + *offsetp;
4678 /* Call utf8n_to_uvchr() to validate the sequence */
4679 utf8n_to_uvchr(s, UTF8SKIP(s), &n, 0);
4694 Returns a boolean indicating whether the strings in the two SVs are
4701 Perl_sv_eq(pTHX_ register SV *sv1, register SV *sv2)
4715 pv1 = SvPV(sv1, cur1);
4722 pv2 = SvPV(sv2, cur2);
4724 /* do not utf8ize the comparands as a side-effect */
4725 if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTES) {
4726 bool is_utf8 = TRUE;
4727 /* UTF-8ness differs */
4728 if (PL_hints & HINT_UTF8_DISTINCT)
4732 /* sv1 is the UTF-8 one , If is equal it must be downgrade-able */
4733 char *pv = (char*)bytes_from_utf8((U8*)pv1, &cur1, &is_utf8);
4738 /* sv2 is the UTF-8 one , If is equal it must be downgrade-able */
4739 char *pv = (char *)bytes_from_utf8((U8*)pv2, &cur2, &is_utf8);
4744 /* Downgrade not possible - cannot be eq */
4750 eq = memEQ(pv1, pv2, cur1);
4761 Compares the strings in two SVs. Returns -1, 0, or 1 indicating whether the
4762 string in C<sv1> is less than, equal to, or greater than the string in
4769 Perl_sv_cmp(pTHX_ register SV *sv1, register SV *sv2)
4774 bool pv1tmp = FALSE;
4775 bool pv2tmp = FALSE;
4782 pv1 = SvPV(sv1, cur1);
4789 pv2 = SvPV(sv2, cur2);
4791 /* do not utf8ize the comparands as a side-effect */
4792 if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTES) {
4793 if (PL_hints & HINT_UTF8_DISTINCT)
4794 return SvUTF8(sv1) ? 1 : -1;
4797 pv2 = (char*)bytes_to_utf8((U8*)pv2, &cur2);
4801 pv1 = (char*)bytes_to_utf8((U8*)pv1, &cur1);
4807 cmp = cur2 ? -1 : 0;
4811 I32 retval = memcmp((void*)pv1, (void*)pv2, cur1 < cur2 ? cur1 : cur2);
4814 cmp = retval < 0 ? -1 : 1;
4815 } else if (cur1 == cur2) {
4818 cmp = cur1 < cur2 ? -1 : 1;
4831 =for apidoc sv_cmp_locale
4833 Compares the strings in two SVs in a locale-aware manner. See
4840 Perl_sv_cmp_locale(pTHX_ register SV *sv1, register SV *sv2)
4842 #ifdef USE_LOCALE_COLLATE
4848 if (PL_collation_standard)
4852 pv1 = sv1 ? sv_collxfrm(sv1, &len1) : (char *) NULL;
4854 pv2 = sv2 ? sv_collxfrm(sv2, &len2) : (char *) NULL;
4856 if (!pv1 || !len1) {
4867 retval = memcmp((void*)pv1, (void*)pv2, len1 < len2 ? len1 : len2);
4870 return retval < 0 ? -1 : 1;
4873 * When the result of collation is equality, that doesn't mean
4874 * that there are no differences -- some locales exclude some
4875 * characters from consideration. So to avoid false equalities,
4876 * we use the raw string as a tiebreaker.