From 2923e6c41e7f421cef7ccf25572bf476c4f28672 Mon Sep 17 00:00:00 2001 From: Karl Williamson Date: Wed, 24 Feb 2021 04:24:48 -0700 Subject: [PATCH] locale.c: Use get_category_index() 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. --- embed.fnc | 2 +- embed.h | 2 +- locale.c | 65 +++++++++++++++++++++++++++---------------------------- proto.h | 4 ++-- 4 files changed, 36 insertions(+), 37 deletions(-) diff --git a/embed.fnc b/embed.fnc index 018616500793..8d2341880c12 100644 --- a/embed.fnc +++ b/embed.fnc @@ -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 diff --git a/embed.h b/embed.h index 2a8d9dc75071..1cdfdb8ef586 100644 --- a/embed.h +++ b/embed.h @@ -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) diff --git a/locale.c b/locale.c index 7c4045c87b6c..60d9f33acfc1 100644 --- a/locale.c +++ b/locale.c @@ -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) { @@ -533,18 +541,9 @@ 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 */ @@ -552,12 +551,11 @@ S_emulate_setlocale(const int category, "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]; @@ -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); @@ -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"))); } } @@ -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); @@ -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; @@ -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 @@ -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 @@ -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]); } } } @@ -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 */ @@ -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; } @@ -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 */ @@ -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]); } } @@ -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); @@ -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); @@ -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); @@ -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' */ @@ -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) { diff --git a/proto.h b/proto.h index 849ea9baa23a..673b464166dd 100644 --- a/proto.h +++ b/proto.h @@ -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