Skip to content

Commit

Permalink
locale.c: Add static fcn to analyze locale codeset
Browse files Browse the repository at this point in the history
It determines if the name indicates it is UTF-8 or not.  There are
several variant spellings in use, and this hides that from the the
callers.

It won't be actually used until the next commit
  • Loading branch information
khwilliamson committed May 6, 2021
1 parent a0a7d23 commit a224eff
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions embed.fnc
Expand Up @@ -3254,6 +3254,7 @@ Sr |void |setlocale_failure_panic_i|const unsigned int cat_index \
S |void |set_numeric_radix|const bool use_locale
S |void |new_numeric |NULLOK const char* newnum
S |void |new_LC_ALL |NULLOK const char* unused
ST |bool |is_codeset_name_UTF8|NN const char * name
# ifdef USE_POSIX_2008_LOCALE
S |const char*|emulate_setlocale_i|const unsigned int index \
|NULLOK const char* new_locale \
Expand Down
1 change: 1 addition & 0 deletions embed.h
Expand Up @@ -1712,6 +1712,7 @@
# if defined(USE_LOCALE)
#define category_name S_category_name
#define get_category_index S_get_category_index
#define is_codeset_name_UTF8 S_is_codeset_name_UTF8
#define new_LC_ALL(a) S_new_LC_ALL(aTHX_ a)
#define new_collate(a) S_new_collate(aTHX_ a)
#define new_ctype(a) S_new_ctype(aTHX_ a)
Expand Down
25 changes: 25 additions & 0 deletions locale.c
Expand Up @@ -5534,6 +5534,31 @@ Perl__is_cur_LC_category_utf8(pTHX_ int category)
return is_utf8;
}

STATIC bool
S_is_codeset_name_UTF8(const char * name)
{
/* Return a boolean as to if the passed-in name indicates it is a UTF-8
* code set. Several variants are possible */
const Size_t len = strlen(name);

PERL_ARGS_ASSERT_IS_CODESET_NAME_UTF8;

# ifdef WIN32

/* http://msdn.microsoft.com/en-us/library/windows/desktop/dd317756.aspx */
if (memENDs(name, len, "65001")) {
return TRUE;
}

# endif
/* 'UTF8' or 'UTF-8' */
return ( inRANGE(len, 4, 5)
&& name[len-1] == '8'
&& ( memBEGINs(name, len, "UTF")
|| memBEGINs(name, len, "utf"))
&& (len == 4 || name[3] == '-'));
}

#endif

bool
Expand Down
3 changes: 3 additions & 0 deletions proto.h
Expand Up @@ -5133,6 +5133,9 @@ STATIC const char* S_category_name(const int category);
#define PERL_ARGS_ASSERT_CATEGORY_NAME
STATIC unsigned int S_get_category_index(const int category, const char * locale);
#define PERL_ARGS_ASSERT_GET_CATEGORY_INDEX
STATIC bool S_is_codeset_name_UTF8(const char * name);
#define PERL_ARGS_ASSERT_IS_CODESET_NAME_UTF8 \
assert(name)
STATIC void S_new_LC_ALL(pTHX_ const char* unused);
#define PERL_ARGS_ASSERT_NEW_LC_ALL
STATIC void S_new_collate(pTHX_ const char* newcoll);
Expand Down

0 comments on commit a224eff

Please sign in to comment.