Skip to content

Commit

Permalink
locale.c: Change API of S_calculate_LC_ALL_string
Browse files Browse the repository at this point in the history
This function looks at all the individual locale categories on the
system.  So far it is called with either an array of the current values
for them, or an object from which those values can be derived.

But the object is always the current locale object in effect.  The
querylocale_i() macro can be used to get this information without
needing to have to name the object.  Therefore the object parameter is
superfluous, and the function could be notified to use querylocale_i by
making the array parameter NULL.

But right now you get one or the other.  There are two signatures to
this function, depending on the platform capabilities.

This commit collapses those into one signature, the one with an array
parameter.  And then NULL is used to trigger calling querylocale_i().

The reason to do this is that future commits will want to pass an array
to this function, even when it is compiled on a platform that currently
doesn't allow that option.
  • Loading branch information
khwilliamson committed May 22, 2023
1 parent 45d5dac commit 999b4c8
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 63 deletions.
19 changes: 5 additions & 14 deletions embed.fnc
Original file line number Diff line number Diff line change
Expand Up @@ -4325,6 +4325,8 @@ S |void |populate_hash_from_localeconv \
|NULLOK const lconv_offset_t *integers
# endif
# if defined(USE_LOCALE)
S |const char *|calculate_LC_ALL_string \
|NULLOK const char **category_locales_list
RS |unsigned int|get_category_index_helper \
|const int category \
|NULLOK bool *success \
Expand Down Expand Up @@ -4419,31 +4421,20 @@ S |const char *|setlocale_from_aggregate_LC_ALL \
|NN const char *locale \
|const line_t line
S |locale_t|use_curlocale_scratch
# if defined(USE_QUERYLOCALE)
S |const char *|calculate_LC_ALL_string \
|const locale_t cur_obj
# else
# if !defined(USE_QUERYLOCALE)
S |void |update_PL_curlocales_i \
|const unsigned int index \
|NN const char *new_locale
# endif
# elif defined(USE_LOCALE_THREADS) && \
!defined(USE_THREAD_SAFE_LOCALE) && \
!defined(USE_THREAD_SAFE_LOCALE_EMULATION) /* &&
!defined(USE_POSIX_2008_LOCALE) */
# elif defined(USE_LOCALE_THREADS) && !defined(USE_THREAD_SAFE_LOCALE) && \
!defined(USE_THREAD_SAFE_LOCALE_EMULATION)
S |bool |less_dicey_bool_setlocale_r \
|const int cat \
|NN const char *locale
S |const char *|less_dicey_setlocale_r \
|const int category \
|NULLOK const char *locale
# endif
# if !( defined(USE_POSIX_2008_LOCALE) && defined(USE_QUERYLOCALE) ) && \
( !defined(LC_ALL) || defined(USE_POSIX_2008_LOCALE) || \
defined(WIN32) )
S |const char *|calculate_LC_ALL_string \
|NN const char **category_locales_list
# endif
# if defined(WIN32)
ST |wchar_t *|Win_byte_string_to_wstring \
|const UINT code_page \
Expand Down
17 changes: 5 additions & 12 deletions embed.h
Original file line number Diff line number Diff line change
Expand Up @@ -1269,6 +1269,7 @@
# define populate_hash_from_localeconv(a,b,c,d,e) S_populate_hash_from_localeconv(aTHX_ a,b,c,d,e)
# endif
# if defined(USE_LOCALE)
# define calculate_LC_ALL_string(a) S_calculate_LC_ALL_string(aTHX_ a)
# define get_category_index_helper(a,b,c) S_get_category_index_helper(aTHX_ a,b,c)
# define mortalized_pv_copy(a) S_mortalized_pv_copy(aTHX_ a)
# define new_LC_ALL(a,b) S_new_LC_ALL(aTHX_ a,b)
Expand Down Expand Up @@ -1304,23 +1305,15 @@
# define querylocale_2008_i(a) S_querylocale_2008_i(aTHX_ a)
# define setlocale_from_aggregate_LC_ALL(a,b) S_setlocale_from_aggregate_LC_ALL(aTHX_ a,b)
# define use_curlocale_scratch() S_use_curlocale_scratch(aTHX)
# if defined(USE_QUERYLOCALE)
# define calculate_LC_ALL_string(a) S_calculate_LC_ALL_string(aTHX_ a)
# else
# if !defined(USE_QUERYLOCALE)
# define update_PL_curlocales_i(a,b) S_update_PL_curlocales_i(aTHX_ a,b)
# endif
# elif defined(USE_LOCALE_THREADS) && \
!defined(USE_THREAD_SAFE_LOCALE) && \
!defined(USE_THREAD_SAFE_LOCALE_EMULATION) /* &&
!defined(USE_POSIX_2008_LOCALE) */
# elif defined(USE_LOCALE_THREADS) && \
!defined(USE_THREAD_SAFE_LOCALE) && \
!defined(USE_THREAD_SAFE_LOCALE_EMULATION)
# define less_dicey_bool_setlocale_r(a,b) S_less_dicey_bool_setlocale_r(aTHX_ a,b)
# define less_dicey_setlocale_r(a,b) S_less_dicey_setlocale_r(aTHX_ a,b)
# endif
# if !( defined(USE_POSIX_2008_LOCALE) && defined(USE_QUERYLOCALE) ) && \
( !defined(LC_ALL) || defined(USE_POSIX_2008_LOCALE) || \
defined(WIN32) )
# define calculate_LC_ALL_string(a) S_calculate_LC_ALL_string(aTHX_ a)
# endif
# if defined(WIN32)
# define Win_byte_string_to_wstring S_Win_byte_string_to_wstring
# define Win_wstring_to_byte_string S_Win_wstring_to_byte_string
Expand Down
38 changes: 16 additions & 22 deletions locale.c
Original file line number Diff line number Diff line change
Expand Up @@ -1346,7 +1346,7 @@ S_querylocale_2008_i(pTHX_ const unsigned int index)
/* We don't currently keep records when there is querylocale(), so have
* to get it anew each time */
retval = (index == LC_ALL_INDEX_)
? calculate_LC_ALL_string(cur_obj)
? calculate_LC_ALL_string(NULL)
: querylocale_l(index, cur_obj);
# else

Expand Down Expand Up @@ -1837,19 +1837,14 @@ S_bool_setlocale_2008_i(pTHX_
(__ASSERT_(i != LC_ALL_INDEX_) querylocale_i(i))
#endif

#if defined(USE_LOCALE) \
&& (defined(WIN32) || defined(USE_POSIX_2008_LOCALE) || ! defined(LC_ALL))
#if defined(USE_LOCALE)

STATIC
const char *

# ifdef USE_QUERYLOCALE
S_calculate_LC_ALL_string(pTHX_ const locale_t cur_obj)
# else
S_calculate_LC_ALL_string(pTHX_ const char ** category_locales_list)
# endif

{
PERL_ARGS_ASSERT_CALCULATE_LC_ALL_STRING;

/* For POSIX 2008, we have to figure out LC_ALL ourselves when needed.
* querylocale(), on systems that have it, doesn't tend to work for LC_ALL.
* So we have to construct the answer ourselves based on the passed in
Expand Down Expand Up @@ -1889,6 +1884,16 @@ S_calculate_LC_ALL_string(pTHX_ const char ** category_locales_list)
* categories, adding new ones as they show up on obscure platforms.
*/

const char * my_category_locales_list[LOCALE_CATEGORIES_COUNT_];
const char ** locales_list = category_locales_list;
if (locales_list == NULL) {
locales_list = (const char **) &my_category_locales_list;

for (unsigned i = 0; i < LC_ALL_INDEX_; i++) {
locales_list[i] = querylocale_i(i);
}
}

unsigned int i;
Size_t names_len = 0;
bool are_all_categories_the_same_locale = TRUE;
Expand All @@ -1897,17 +1902,11 @@ S_calculate_LC_ALL_string(pTHX_ const char ** category_locales_list)
char * this_start = NULL;
Size_t entry_len = 0;

PERL_ARGS_ASSERT_CALCULATE_LC_ALL_STRING;

/* First calculate the needed size for the string listing the categories
* and their locales. */
for (i = 0; i < LC_ALL_INDEX_; i++) {

# ifdef USE_QUERYLOCALE
const char * entry = querylocale_l(i, cur_obj);
# else
const char * entry = category_locales_list[i];
# endif
const char * entry = locales_list[i];

names_len += category_name_lengths[i]
+ 1 /* '=' */
Expand All @@ -1924,12 +1923,7 @@ S_calculate_LC_ALL_string(pTHX_ const char ** category_locales_list)
/* Then fill it in */
for (i = 0; i < LC_ALL_INDEX_; i++) {
Size_t new_len;

# ifdef USE_QUERYLOCALE
const char * entry = querylocale_l(i, cur_obj);
# else
const char * entry = category_locales_list[i];
# endif
const char * entry = locales_list[i];

new_len = my_strlcat(aggregate_locale, category_names[i], names_len);
assert(new_len <= names_len);
Expand Down
20 changes: 5 additions & 15 deletions proto.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 999b4c8

Please sign in to comment.