Skip to content

Commit

Permalink
locale.c: Use get_category_index()
Browse files Browse the repository at this point in the history
This creates the first uses of the function added in the previous commit.

It changes the name of a function that now takes an index to have the
suffix _i to indicate its calling parameter is a category index rather
than a category.  This will become a common paradigm in this file in
later commits.

Two macros are also created to call that function; they have suffixes _c
(to indicate the parameter is a category known at compile time, and _r
(to indicate it needs to be computed at runtime).  This is in keeping
with the already existing paradigm in this file.
  • Loading branch information
khwilliamson committed May 6, 2021
1 parent b70ece7 commit d1790e1
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 37 deletions.
2 changes: 1 addition & 1 deletion embed.fnc
Expand Up @@ -3252,7 +3252,7 @@ S |void |print_collxfrm_input_and_return \
S |void |print_bytes_for_locale |NN const char * const s \
|NN const char * const e \
|const bool is_utf8
STR |char * |setlocale_debug_string |const int category \
STR |char * |setlocale_debug_string_i|const unsigned cat_index \
|NULLOK const char* const locale \
|NULLOK const char* const retval
# endif
Expand Down
2 changes: 1 addition & 1 deletion embed.h
Expand Up @@ -1569,7 +1569,7 @@
# if defined(USE_LOCALE)
#define print_bytes_for_locale(a,b,c) S_print_bytes_for_locale(aTHX_ a,b,c)
#define print_collxfrm_input_and_return(a,b,c,d) S_print_collxfrm_input_and_return(aTHX_ a,b,c,d)
#define setlocale_debug_string S_setlocale_debug_string
#define setlocale_debug_string_i S_setlocale_debug_string_i
# endif
# endif
# if defined(PERL_IN_PAD_C)
Expand Down
65 changes: 32 additions & 33 deletions locale.c
Expand Up @@ -121,6 +121,14 @@ STATIC_ASSERT_DECL(STRLENs(UTF8NESS_PREFIX) == 1);
* kept there always. The remining portion of the cache is LRU, with the
* oldest looked-up locale at the tail end */

# ifdef DEBUGGING
# define setlocale_debug_string_c(category, locale, result) \
setlocale_debug_string_i(category##_INDEX_, locale, result)
# define setlocale_debug_string_r(category, locale, result) \
setlocale_debug_string_i(get_category_index(category, locale), \
locale, result)
# endif

STATIC char *
S_stdize_locale(pTHX_ char *locs)
{
Expand Down Expand Up @@ -533,31 +541,21 @@ S_emulate_setlocale(const int category,

/* If the input mask might be incorrect, calculate the correct one */
if (! is_index_valid) {
unsigned int i;
index = get_category_index(category, locale);

DEBUG_Lv(PerlIO_printf(Perl_debug_log,
"%s:%d: finding index of category %d (%s)\n",
__FILE__, __LINE__, category, category_name(category)));

for (i = 0; i <= LC_ALL_INDEX_; i++) {
if (category == categories[i]) {
index = i;
goto found_index;
}
}
if (index > NOMINAL_LC_ALL_INDEX) {

/* Here, we don't know about this category, so can't handle it.
* Fallback to the early POSIX usages */
Perl_warner(aTHX_ packWARN(WARN_LOCALE),
"Unknown locale category %d; can't set it to %s\n",
category, locale);
return NULL;

found_index: ;
}

DEBUG_Lv(PerlIO_printf(Perl_debug_log,
"%s:%d: index is %d for %s\n",
__FILE__, __LINE__, index, category_name(category)));
__FILE__, __LINE__, index, category_names[index]));
}

mask = category_masks[index];
Expand Down Expand Up @@ -1959,7 +1957,7 @@ S_win32_setlocale(pTHX_ int category, const char* locale)
DEBUG_L(STMT_START {
dSAVE_ERRNO;
PerlIO_printf(Perl_debug_log, "%s:%d: %s\n", __FILE__, __LINE__,
setlocale_debug_string(category, locale, result));
setlocale_debug_string_r(category, locale, result));
RESTORE_ERRNO;
} STMT_END);

Expand All @@ -1983,7 +1981,7 @@ S_win32_setlocale(pTHX_ int category, const char* locale)
#endif
DEBUG_Lv(PerlIO_printf(Perl_debug_log, "%s:%d: %s\n",
__FILE__, __LINE__,
setlocale_debug_string(categories[i], result, "not captured")));
setlocale_debug_string_i(i, result, "not captured")));
}
}

Expand All @@ -1992,7 +1990,7 @@ S_win32_setlocale(pTHX_ int category, const char* locale)
dSAVE_ERRNO;
PerlIO_printf(Perl_debug_log, "%s:%d: %s\n",
__FILE__, __LINE__,
setlocale_debug_string(LC_ALL, NULL, result));
setlocale_debug_string_c(LC_ALL, NULL, result));
RESTORE_ERRNO;
} STMT_END);

Expand Down Expand Up @@ -2098,7 +2096,7 @@ Perl_setlocale(const int category, const char * locale)

DEBUG_L(PerlIO_printf(Perl_debug_log,
"%s:%d: %s\n", __FILE__, __LINE__,
setlocale_debug_string(category, locale, retval)));
setlocale_debug_string_r(category, locale, retval)));

RESTORE_ERRNO;

Expand Down Expand Up @@ -3109,10 +3107,10 @@ Perl_init_i18nl10n(pTHX_ int printwarn)

DEBUG_INITIALIZATION_set(cBOOL(PerlEnv_getenv("PERL_DEBUG_LOCALE_INIT")));

# define DEBUG_LOCALE_INIT(category, locale, result) \
# define DEBUG_LOCALE_INIT(cat_index, locale, result) \
DEBUG_L(PerlIO_printf(Perl_debug_log, \
"%s:%d: %s\n", __FILE__, __LINE__, \
setlocale_debug_string(category, locale, result)));
setlocale_debug_string_i(cat_index, locale, result)));

/* Make sure the parallel arrays are properly set up */
# ifdef USE_LOCALE_NUMERIC
Expand Down Expand Up @@ -3280,7 +3278,8 @@ Perl_init_i18nl10n(pTHX_ int printwarn)
bool done = FALSE;
if (lang) {
sl_result[LC_ALL_INDEX_] = do_setlocale_c(LC_ALL, setlocale_init);
DEBUG_LOCALE_INIT(LC_ALL, setlocale_init, sl_result[LC_ALL_INDEX_]);
DEBUG_LOCALE_INIT(LC_ALL_INDEX_, setlocale_init,
sl_result[LC_ALL_INDEX_]);
if (sl_result[LC_ALL_INDEX_])
done = TRUE;
else
Expand All @@ -3296,7 +3295,7 @@ Perl_init_i18nl10n(pTHX_ int printwarn)
if (! sl_result[i]) {
setlocale_failure = TRUE;
}
DEBUG_LOCALE_INIT(categories[i], locale_param, sl_result[i]);
DEBUG_LOCALE_INIT(i, locale_param, sl_result[i]);
}
}
}
Expand Down Expand Up @@ -3332,7 +3331,7 @@ Perl_init_i18nl10n(pTHX_ int printwarn)
/* Note that this may change the locale, but we are going to do
* that anyway just below */
system_default_locale = do_setlocale_c(LC_ALL, "");
DEBUG_LOCALE_INIT(LC_ALL, "", system_default_locale);
DEBUG_LOCALE_INIT(LC_ALL_INDEX_, "", system_default_locale);

/* Skip if invalid or if it's already on the list of locales to
* try */
Expand All @@ -3357,7 +3356,7 @@ Perl_init_i18nl10n(pTHX_ int printwarn)
# ifdef LC_ALL

sl_result[LC_ALL_INDEX_] = do_setlocale_c(LC_ALL, trial_locale);
DEBUG_LOCALE_INIT(LC_ALL, trial_locale, sl_result[LC_ALL_INDEX_]);
DEBUG_LOCALE_INIT(LC_ALL_INDEX_, trial_locale, sl_result[LC_ALL_INDEX_]);
if (! sl_result[LC_ALL_INDEX_]) {
setlocale_failure = TRUE;
}
Expand All @@ -3382,7 +3381,7 @@ Perl_init_i18nl10n(pTHX_ int printwarn)
if (! curlocales[j]) {
setlocale_failure = TRUE;
}
DEBUG_LOCALE_INIT(categories[j], trial_locale, curlocales[j]);
DEBUG_LOCALE_INIT(j, trial_locale, curlocales[j]);
}

if (LIKELY(! setlocale_failure)) { /* All succeeded */
Expand Down Expand Up @@ -3565,7 +3564,7 @@ Perl_init_i18nl10n(pTHX_ int printwarn)
for (j = 0; j < NOMINAL_LC_ALL_INDEX; j++) {
Safefree(curlocales[j]);
curlocales[j] = savepv(do_setlocale_r(categories[j], NULL));
DEBUG_LOCALE_INIT(categories[j], NULL, curlocales[j]);
DEBUG_LOCALE_INIT(j, NULL, curlocales[j]);
}
}

Expand Down Expand Up @@ -5295,7 +5294,7 @@ Perl_sync_locale()
newlocale = savepv(do_setlocale_c(LC_CTYPE, NULL));
DEBUG_Lv(PerlIO_printf(Perl_debug_log,
"%s:%d: %s\n", __FILE__, __LINE__,
setlocale_debug_string(LC_CTYPE, NULL, newlocale)));
setlocale_debug_string_c(LC_CTYPE, NULL, newlocale)));
new_ctype(newlocale);
Safefree(newlocale);

Expand All @@ -5305,7 +5304,7 @@ Perl_sync_locale()
newlocale = savepv(do_setlocale_c(LC_COLLATE, NULL));
DEBUG_Lv(PerlIO_printf(Perl_debug_log,
"%s:%d: %s\n", __FILE__, __LINE__,
setlocale_debug_string(LC_COLLATE, NULL, newlocale)));
setlocale_debug_string_c(LC_COLLATE, NULL, newlocale)));
new_collate(newlocale);
Safefree(newlocale);

Expand All @@ -5315,7 +5314,7 @@ Perl_sync_locale()
newlocale = savepv(do_setlocale_c(LC_NUMERIC, NULL));
DEBUG_Lv(PerlIO_printf(Perl_debug_log,
"%s:%d: %s\n", __FILE__, __LINE__,
setlocale_debug_string(LC_NUMERIC, NULL, newlocale)));
setlocale_debug_string_c(LC_NUMERIC, NULL, newlocale)));
new_numeric(newlocale);
Safefree(newlocale);

Expand All @@ -5330,9 +5329,8 @@ Perl_sync_locale()
#if defined(DEBUGGING) && defined(USE_LOCALE)

STATIC char *
S_setlocale_debug_string(const int category, /* category number,
like LC_ALL */
const char* const locale, /* locale name */
S_setlocale_debug_string_i(const unsigned cat_index,
const char* const locale, /* Optional locale name */

/* return value from setlocale() when attempting to
* set 'category' to 'locale' */
Expand All @@ -5344,9 +5342,10 @@ S_setlocale_debug_string(const int category, /* category number,
* formulate a string to immediately print or savepv() on. */

static char ret[256];
assert(cat_index <= NOMINAL_LC_ALL_INDEX);

my_strlcpy(ret, "setlocale(", sizeof(ret));
my_strlcat(ret, category_name(category), sizeof(ret));
my_strlcat(ret, category_names[cat_index], sizeof(ret));
my_strlcat(ret, ", ", sizeof(ret));

if (locale) {
Expand Down
4 changes: 2 additions & 2 deletions proto.h
Expand Up @@ -4572,9 +4572,9 @@ STATIC void S_print_bytes_for_locale(pTHX_ const char * const s, const char * co
STATIC void S_print_collxfrm_input_and_return(pTHX_ const char * const s, const char * const e, const STRLEN * const xlen, const bool is_utf8);
#define PERL_ARGS_ASSERT_PRINT_COLLXFRM_INPUT_AND_RETURN \
assert(s); assert(e)
STATIC char * S_setlocale_debug_string(const int category, const char* const locale, const char* const retval)
STATIC char * S_setlocale_debug_string_i(const unsigned cat_index, const char* const locale, const char* const retval)
__attribute__warn_unused_result__;
#define PERL_ARGS_ASSERT_SETLOCALE_DEBUG_STRING
#define PERL_ARGS_ASSERT_SETLOCALE_DEBUG_STRING_I

# endif
# endif
Expand Down

0 comments on commit d1790e1

Please sign in to comment.