Skip to content

Commit

Permalink
locale.c: Slight refactoring of S_querylocale_2008_i
Browse files Browse the repository at this point in the history
This just moves some asserts, adds comments and a branch prediction, and
replaces a variable used in only one place by it value.
  • Loading branch information
khwilliamson committed May 12, 2023
1 parent 0d79d27 commit 0dfd8fb
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions locale.c
Expand Up @@ -1278,6 +1278,9 @@ S_less_dicey_bool_setlocale_r(pTHX_ const int cat, const char * locale)
STATIC const char *
S_querylocale_2008_i(pTHX_ const unsigned int index)
{
PERL_ARGS_ASSERT_QUERYLOCALE_2008_I;
assert(index <= LC_ALL_INDEX_);

/* This function returns the name of the locale category given by the input
* index into our parallel tables of them.
*
Expand All @@ -1293,20 +1296,19 @@ S_querylocale_2008_i(pTHX_ const unsigned int index)
* libraries do use setlocale(), but that changes the global locale, and
* threads using per-thread locales will just ignore those changes. */

int category;
const locale_t cur_obj = uselocale((locale_t) 0);
const char * retval;

PERL_ARGS_ASSERT_QUERYLOCALE_2008_I;
assert(index <= LC_ALL_INDEX_);

category = categories[index];

DEBUG_Lv(PerlIO_printf(Perl_debug_log, "querylocale_2008_i(%s) on %p\n",
category_names[index], cur_obj));
if (cur_obj == LC_GLOBAL_LOCALE) {
if (UNLIKELY(cur_obj == LC_GLOBAL_LOCALE)) {

/* Even on platforms that have querylocale(), it is unclear if they
* work in the global locale, and we have the means to get the correct
* answer anyway. khw is unsure this situation even comes up these
* days, hence the branch prediction */
POSIX_SETLOCALE_LOCK;
retval = mortalized_pv_copy(posix_setlocale(category, NULL));
retval = mortalized_pv_copy(posix_setlocale(categories[index], NULL));
POSIX_SETLOCALE_UNLOCK;
}
else {
Expand Down

0 comments on commit 0dfd8fb

Please sign in to comment.