summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKarl Williamson <khw@cpan.org>2020-12-22 09:29:12 -0700
committerKarl Williamson <khw@cpan.org>2022-06-19 13:29:35 -0600
commit0bb379424f96daa3b0e41d5183447145c926b52d (patch)
tree8225c5431e0b12cd313f7c2e9fb64843b8598ce2
parentc7dc1ea8f2e8d3c4ec322e7fc2154bd801049902 (diff)
downloadperl-0bb379424f96daa3b0e41d5183447145c926b52d.tar.gz
Perl_setlocale(): Same code for all param2 == NULL
Calling Perl_setlocale() with a NULL 2nd parameter returns the current locale, rather than changing it. Previously LC_NUMERIC and LC_ALL were treated specially; other categories were lumped in with the code that changes the locale. Changing some categories involves a non-trivial amount of work. This commit avoids that by moving all queries to the same 'if' branch. LC_NUMERIC and LC_ALL still have to be treated specially, but now it's all within the same outer 'if', and the unnecessarily executing code for when the locale changes is avoided.
-rw-r--r--locale.c48
1 files changed, 26 insertions, 22 deletions
diff --git a/locale.c b/locale.c
index 7e34b9fb78..353e5b20f5 100644
--- a/locale.c
+++ b/locale.c
@@ -2228,16 +2228,19 @@ Perl_setlocale(const int category, const char * locale)
unsigned int cat_index;
dSAVEDERRNO;
dTHX;
+
+ /* A NULL locale means only query what the current one is. */
+ if (locale == NULL) {
+
+# ifdef USE_LOCALE_NUMERIC
+# ifdef LC_ALL
+
DECLARATION_FOR_LC_NUMERIC_MANIPULATION;
-#ifdef USE_LOCALE_NUMERIC
+# endif
- /* A NULL locale means only query what the current one is. We have the
- * LC_NUMERIC name saved, because we are normally switched into the C
- * (or equivalent) locale for it. For an LC_ALL query, switch back to get
- * the correct results. All other categories don't require special
- * handling */
- if (locale == NULL) {
+ /* We have the LC_NUMERIC name saved, because we are normally switched
+ * into the C locale (or equivalent) for it. */
if (category == LC_NUMERIC) {
/* We don't have to copy this return value, as it is a per-thread
@@ -2247,29 +2250,35 @@ Perl_setlocale(const int category, const char * locale)
# ifdef LC_ALL
- else if (category == LC_ALL) {
+ /* For an LC_ALL query, switch back to the underlying numeric locale
+ * (if we aren't there already) so as to get the correct results. Our
+ * records for all the other categories are valid without switching */
+ if (category == LC_ALL) {
STORE_LC_NUMERIC_FORCE_TO_UNDERLYING();
}
# endif
+# endif /* End of has LC_NUMERIC */
+
+ retval = save_to_buffer(querylocale_r(category),
+ &PL_setlocale_buf, &PL_setlocale_bufsize, 0);
+# if defined(USE_LOCALE_NUMERIC) && defined(LC_ALL)
+
+ if (category == LC_ALL) {
+ RESTORE_LC_NUMERIC();
}
-#endif
+# endif
+
+ return retval;
+ } /* End of querying the current locale */
cat_index = get_category_index(category, NULL);
retval = save_to_buffer(setlocale_i(cat_index, locale),
&PL_setlocale_buf, &PL_setlocale_bufsize, 0);
SAVE_ERRNO;
-#if defined(USE_LOCALE_NUMERIC) && defined(LC_ALL)
-
- if (locale == NULL && category == LC_ALL) {
- RESTORE_LC_NUMERIC();
- }
-
-#endif
-
DEBUG_L(PerlIO_printf(Perl_debug_log,
"%s:%d: %s\n", __FILE__, __LINE__,
setlocale_debug_string_r(category, locale, retval)));
@@ -2280,11 +2289,6 @@ Perl_setlocale(const int category, const char * locale)
return NULL;
}
- /* If locale == NULL, we are just querying the state */
- if (locale == NULL) {
- return retval;
- }
-
/* Now that have changed locales, we have to update our records to
* correspond. Only certain categories have extra work to update. */
if (update_functions[cat_index]) {