Skip to content

Commit

Permalink
locale.c: Extract code into a function
Browse files Browse the repository at this point in the history
This is in preparation for a future commit where it will be called from
more than one place.
  • Loading branch information
khwilliamson committed May 6, 2023
1 parent 7fbed96 commit 8412a47
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 38 deletions.
22 changes: 13 additions & 9 deletions embed.fnc
Expand Up @@ -4324,15 +4324,6 @@ S |void |populate_hash_from_localeconv \
|NN const lconv_offset_t *strings[2] \
|NULLOK const lconv_offset_t *integers
# endif
# if defined(LC_ALL) && \
( defined(USE_FAKE_LC_ALL_POSITIONAL_NOTATION) || \
defined(USE_POSIX_2008_LOCALE) || \
( defined(USE_LOCALE) && defined(USE_STDIZE_LOCALE) ) )
S |parse_LC_ALL_string_return|parse_LC_ALL_string \
|NN const char *string \
|NN const char **output \
|const line_t caller_line
# endif
# if defined(USE_LOCALE)
S |const char *|calculate_LC_ALL_string \
|NULLOK const char **category_locales_list \
Expand Down Expand Up @@ -4390,6 +4381,19 @@ S |const char *|my_langinfo_i \
|NULLOK Size_t *retbuf_sizep \
|NULLOK utf8ness_t *utf8ness
# endif
# if defined(LC_ALL)
S |void |give_perl_locale_control \
|NN const char *lc_all_string \
|const line_t caller_line
S |parse_LC_ALL_string_return|parse_LC_ALL_string \
|NN const char *string \
|NN const char **output \
|const line_t caller_line
# else
S |void |give_perl_locale_control \
|NN const char **curlocales \
|const line_t caller_line
# endif
# if defined(USE_LOCALE_COLLATE)
S |void |new_collate |NN const char *newcoll \
|bool force
Expand Down
9 changes: 9 additions & 0 deletions embed.h
Expand Up @@ -1268,12 +1268,15 @@
# define my_localeconv(a) S_my_localeconv(aTHX_ a)
# define populate_hash_from_localeconv(a,b,c,d,e) S_populate_hash_from_localeconv(aTHX_ a,b,c,d,e)
# endif
<<<<<<< HEAD
# if defined(LC_ALL) && \
( defined(USE_FAKE_LC_ALL_POSITIONAL_NOTATION) || \
defined(USE_POSIX_2008_LOCALE) || \
( defined(USE_LOCALE) && defined(USE_STDIZE_LOCALE) ) )
# define parse_LC_ALL_string(a,b,c) S_parse_LC_ALL_string(aTHX_ a,b,c)
# endif
=======
>>>>>>> 6d4a269fb2 (locale.c: Extract code into a function)
# if defined(USE_LOCALE)
# define calculate_LC_ALL_string(a,b,c) S_calculate_LC_ALL_string(aTHX_ a,b,c)
# define get_category_index_helper(a,b,c) S_get_category_index_helper(aTHX_ a,b,c)
Expand All @@ -1289,6 +1292,12 @@
# else
# define my_langinfo_i(a,b,c,d,e,f) S_my_langinfo_i(aTHX_ a,b,c,d,e,f)
# endif
# if defined(LC_ALL)
# define give_perl_locale_control(a,b) S_give_perl_locale_control(aTHX_ a,b)
# define parse_LC_ALL_string(a,b,c,d) S_parse_LC_ALL_string(aTHX_ a,b,c,d)
# else
# define give_perl_locale_control(a,b) S_give_perl_locale_control(aTHX_ a,b)
# endif
# if defined(USE_LOCALE_COLLATE)
# define new_collate(a,b) S_new_collate(aTHX_ a,b)
# if defined(DEBUGGING)
Expand Down
94 changes: 65 additions & 29 deletions locale.c
Expand Up @@ -1082,10 +1082,7 @@ Perl_locale_panic(const char * msg,
#define setlocale_failure_panic_c(cat, cur, fail, line, higher_line) \
setlocale_failure_panic_i(cat##_INDEX_, cur, fail, line, higher_line)

#if defined(LC_ALL) \
&& ( defined(USE_FAKE_LC_ALL_POSITIONAL_NOTATION) \
|| defined(USE_POSIX_2008_LOCALE) \
|| (defined(USE_STDIZE_LOCALE) && defined(USE_LOCALE)))
#if defined(LC_ALL) && defined(USE_LOCALE)

STATIC parse_LC_ALL_string_return
S_parse_LC_ALL_string(pTHX_ const char * string,
Expand Down Expand Up @@ -5862,6 +5859,65 @@ Perl_my_strftime8_temp(pTHX_ const char *fmt, int sec, int min, int hour, int md
return retval;
}

#ifdef USE_LOCALE

STATIC void
S_give_perl_locale_control(pTHX_
# ifdef LC_ALL
const char * lc_all_string,
# else
const char ** locales,
# endif
const line_t caller_line)
{
PERL_UNUSED_ARG(caller_line);


/* This is called when the program is in the global locale and are
* switching to per-thread (if available). And it is called at
* initialization time to do the same.
*/

# if defined(WIN32) && defined(USE_THREAD_SAFE_LOCALE)

/* On Windows, convert to per-thread behavior. This isn't necessary in
* POSIX 2008, as the conversion gets done automatically in the
* void_setlocale_i() calls below. */
if (_configthreadlocale(_ENABLE_PER_THREAD_LOCALE) == -1) {
locale_panic_("_configthreadlocale returned an error");
}

# endif
# if ! defined(USE_THREAD_SAFE_LOCALE) \
&& ! defined(USE_POSIX_2008_LOCALE)
# if defined(LC_ALL)
PERL_UNUSED_ARG(lc_all_string);
# else
PERL_UNUSED_ARG(locales);
# endif
# else

/* This platform has per-thread locale handling. Do the conversion. */

# if defined(LC_ALL)

void_setlocale_c_with_caller(LC_ALL, lc_all_string, __FILE__, caller_line);

# else

for (unsigned int i = 0; i < LC_ALL_INDEX_; i++) {
void_setlocale_i_with_caller(i, locales[i], __FILE__, caller_line);
}

# endif
# endif

/* Finally, update our remaining records. 'true' => force recalculation */
new_LC_ALL(NULL, true);
}

#endif

/*
* Initialize locale awareness.
*/
Expand Down Expand Up @@ -7969,14 +8025,17 @@ Perl_sync_locale(pTHX)
# endif /* USE_THREAD_SAFE_LOCALE */

/* Here, we are in the global locale. Get and save the values for each
* category. */
* category, and convert the current thread to use them */

# ifdef LC_ALL

STDIZED_SETLOCALE_LOCK;
const char * lc_all_string = savepv(stdized_setlocale(LC_ALL, NULL));
STDIZED_SETLOCALE_UNLOCK;

give_perl_locale_control(lc_all_string, __LINE__);
Safefree(lc_all_string);

# else

const char * current_globals[LC_ALL_INDEX_];
Expand All @@ -7986,37 +8045,14 @@ Perl_sync_locale(pTHX)
STDIZED_SETLOCALE_UNLOCK;
}

# endif

/* Now we have to convert the current thread to use them */

# if defined(WIN32)

/* On Windows, convert to per-thread behavior. This isn't necessary in
* POSIX 2008, as the conversion gets done automatically below in the
* void_X calls. */
if (_configthreadlocale(_ENABLE_PER_THREAD_LOCALE) == -1) {
locale_panic_("_configthreadlocale returned an error");
}

# endif
# ifdef LC_ALL

void_setlocale_c(LC_ALL, lc_all_string);
Safefree(lc_all_string);

# else
give_perl_locale_control((const char **) &current_globals, __LINE__);

for (unsigned i = 0; i < LC_ALL_INDEX_; i++) {
void_setlocale_i(i, current_globals[i]);
Safefree(current_globals[i]);
}

# endif

/* And update our remaining records. 'true' => force recalculation */
new_LC_ALL(NULL, true);

return was_in_global;

#endif
Expand Down
21 changes: 21 additions & 0 deletions proto.h

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

0 comments on commit 8412a47

Please sign in to comment.