Skip to content

Commit 2591369

Browse files
khwilliamsonLeont
authored andcommitted
vutil.c: Simplify locale handling when using POSIX 2008
I read the code over and realized that there was a much simpler way to do things when using POSIX 2008 locales. Instead of all the rigamorole about saving/restoring locales, just toggle to the C object using the thread-safe uselocale() libc function. This trivally gets us to the C locale, and hence a dot radix.
1 parent 615f3b4 commit 2591369

File tree

1 file changed

+7
-60
lines changed

1 file changed

+7
-60
lines changed

vutil/vutil.c

Lines changed: 7 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -668,79 +668,26 @@ Perl_upg_version(pTHX_ SV *ver, bool qv)
668668
locale_name_on_entry = NULL;
669669
}
670670

671-
# else
672-
const locale_t locale_obj_on_entry = uselocale((locale_t) 0);
673-
const char * locale_name_on_entry = NULL;
674-
DECLARATION_FOR_LC_NUMERIC_MANIPULATION;
675-
676-
if (locale_obj_on_entry == LC_GLOBAL_LOCALE) {
677-
678-
/* in the global locale, we can call system setlocale and if it
679-
* isn't C, set it to C. */
680-
LC_NUMERIC_LOCK(0);
681-
682-
locale_name_on_entry = setlocale(LC_NUMERIC, NULL);
683-
if ( strNE(locale_name_on_entry, "C")
684-
&& strNE(locale_name_on_entry, "POSIX"))
685-
{
686-
/* the setlocale() call might free or overwrite the name */
687-
locale_name_on_entry = savepv(locale_name_on_entry);
688-
setlocale(LC_NUMERIC, "C");
689-
}
690-
else { /* This value indicates to the restore code that we
691-
didn't change the locale */
692-
locale_name_on_entry = NULL;
693-
}
694-
}
695-
else if (locale_obj_on_entry == PL_underlying_numeric_obj) {
696-
/* Here, the locale appears to have been changed to use the
697-
* program's underlying locale. Just use our mechanisms to
698-
* switch back to C. It might be possible for this pointer to
699-
* actually refer to something else if it got released and
700-
* reused somehow. But it doesn't matter, our mechanisms will
701-
* work even so */
702-
STORE_LC_NUMERIC_SET_STANDARD();
703-
}
704-
else if (locale_obj_on_entry != PL_C_locale_obj) {
705-
/* The C object should be unchanged during a program's
706-
* execution, so it should be safe to assume it means what it
707-
* says, so if we are in it, no locale change is required.
708-
* Otherwise, simply use the thread-safe operation. */
709-
uselocale(PL_C_locale_obj);
710-
}
711-
712-
# endif
713671
/* Prevent recursed calls from trying to change back */
714672
LOCK_LC_NUMERIC_STANDARD();
715-
#endif
716-
GET_NUMERIC_VERSION(ver, sv, tbuf, buf, len);
717673

718-
#ifdef USE_LOCALE_NUMERIC
674+
GET_NUMERIC_VERSION(ver, sv, tbuf, buf, len);
719675

720676
UNLOCK_LC_NUMERIC_STANDARD();
721677

722-
# ifndef USE_POSIX_2008_LOCALE
723-
724678
if (locale_name_on_entry) {
725679
setlocale(LC_NUMERIC, locale_name_on_entry);
726680
Safefree(locale_name_on_entry);
727681
}
728682

729683
LC_NUMERIC_UNLOCK; /* End critical section */
730-
# else
731-
732-
if (locale_name_on_entry) {
733-
setlocale(LC_NUMERIC, locale_name_on_entry);
734-
Safefree(locale_name_on_entry);
735-
LC_NUMERIC_UNLOCK;
736-
}
737-
else if (locale_obj_on_entry == PL_underlying_numeric_obj) {
738-
RESTORE_LC_NUMERIC();
739-
}
740-
else if (locale_obj_on_entry != PL_C_locale_obj) {
741-
uselocale(locale_obj_on_entry);
742-
}
743684

685+
# else
686+
/* With POSIX 2008, all we have to do is toggle to the C locale
687+
* just long enough to get the value (which should have a dot). */
688+
const locale_t locale_obj_on_entry = uselocale(PL_C_locale_obj);
689+
GET_NUMERIC_VERSION(ver, sv, tbuf, buf, len);
690+
uselocale(locale_obj_on_entry);
744691
# endif
745692

746693
}

0 commit comments

Comments
 (0)