Skip to content

Commit

Permalink
add is_cur_locale_utf8
Browse files Browse the repository at this point in the history
  • Loading branch information
khwilliamson committed Nov 13, 2023
1 parent f0372d2 commit f73b3a9
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 0 deletions.
2 changes: 2 additions & 0 deletions embed.fnc
Expand Up @@ -1607,6 +1607,8 @@ ATdip |bool |is_c9strict_utf8_string_loclen \
|STRLEN len \
|NULLOK const U8 **ep \
|NULLOK STRLEN *el
Ap |bool |is_cur_locale_utf8 \
|const int category

APTdp |bool |isinfnan |NV nv
dp |bool |isinfnansv |NN SV *sv
Expand Down
1 change: 1 addition & 0 deletions embed.h
Expand Up @@ -314,6 +314,7 @@
# define isUTF8_CHAR Perl_isUTF8_CHAR
# define isUTF8_CHAR_flags Perl_isUTF8_CHAR_flags
# define is_c9strict_utf8_string_loclen Perl_is_c9strict_utf8_string_loclen
# define is_cur_locale_utf8(a) Perl_is_cur_locale_utf8(aTHX_ a)
# define is_lvalue_sub() Perl_is_lvalue_sub(aTHX)
# define is_safe_syscall(a,b,c,d) Perl_is_safe_syscall(aTHX_ a,b,c,d)
# define is_strict_utf8_string_loclen Perl_is_strict_utf8_string_loclen
Expand Down
81 changes: 81 additions & 0 deletions locale.c
Expand Up @@ -5014,6 +5014,87 @@ S_is_locale_utf8(pTHX_ const char * locale)
}

#endif

bool
Perl_is_cur_locale_utf8(pTHX_ const int category)
{
/* Returns a bool as to whether or not the locale for the input category is
* a UTF-8 one. The input may be LC_ALL, as long as all categories have
* the same locale; otherwise the answer is undefined, and this function
* returns false, while setting errno to EINVAL. */

#ifndef USE_LOCALE

PERL_UNUSED_ARG(category);
return false;

#else

const locale_category_index index = get_category_index(category);

if (index < LC_ALL_INDEX_) {
return is_locale_utf8(query_nominal_locale_i(index));
}

# ifndef LC_ALL

SET_EINVAL;
return false;

# else

if (index > LC_ALL_INDEX_) {
SET_EINVAL;
return false;
}

/* Parse the current LC_ALL settings to determine if all categories have
* the same locale or not */

const char * individ_locales[LC_ALL_INDEX_] = { NULL };
const char * lc_all = calculate_LC_ALL_string(NULL,
EXTERNAL_FORMAT_FOR_QUERY,
WANT_TEMP_PV,
__LINE__);
bool is_utf8 = false; /* Initialized because some compilers aren't smart
enough to realize it always is set by the
switch() below */
switch (parse_LC_ALL_string(lc_all,
(const char **) &individ_locales,
no_override,
false, /* Return only [0] if suffices */
false, /* Don't panic on error */
__LINE__))
{
case no_array: /* LC_ALL is a single locale */
is_utf8 = is_locale_utf8(lc_all);
break;

case only_element_0: /* element[0] is a single locale valid for all
categories */
is_utf8 = is_locale_utf8(individ_locales[0]);
Safefree(individ_locales[0]);
break;

case full_array:
for_all_individual_category_indexes(j) {
Safefree(individ_locales[j]);
}
/* FALLTHROUGH */

case invalid:
SET_EINVAL;
is_utf8 = false;
break;
}

return is_utf8;

# endif
#endif

}

#ifdef USE_LOCALE

STATIC void
Expand Down
4 changes: 4 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 f73b3a9

Please sign in to comment.