Skip to content

Commit

Permalink
Swap the ordering of two locale category indices
Browse files Browse the repository at this point in the history
Perl internally uses a mapping of locale category values into a
consecutive sequence of indices starting at 0.  These are used as
indexes into arrays.  The reason is that the category numbers are
opaque, vary by platform, aren't necessarily sequential, and hence are
hard to make table driven code for.

This commit makes the LC_CTYPE index 0, and LC_NUMERIC equal to 1;
swapping them.  The reason is to cause LC_CTYPE to get done first in the
many loops through the categories.  The UTF8ness of categories is an
often needed value, and most of the time the categories will have the
same locale.  LC_CTYPE is needed to calculate the UTF8ness, and by doing
it first and caching the result, the other categories likely
automatically will use the same value, without having to recalculate.
  • Loading branch information
khwilliamson committed Apr 30, 2021
1 parent 91d6689 commit 29a7b0d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
24 changes: 12 additions & 12 deletions locale.c
Expand Up @@ -181,12 +181,12 @@ static const char C_thousands_sep[] = "";

STATIC const int categories[] = {

# ifdef USE_LOCALE_NUMERIC
LC_NUMERIC,
# endif
# ifdef USE_LOCALE_CTYPE
LC_CTYPE,
# endif
# ifdef USE_LOCALE_NUMERIC
LC_NUMERIC,
# endif
# ifdef USE_LOCALE_COLLATE
LC_COLLATE,
# endif
Expand Down Expand Up @@ -233,12 +233,12 @@ STATIC const int categories[] = {

STATIC const char * const category_names[] = {

# ifdef USE_LOCALE_NUMERIC
"LC_NUMERIC",
# endif
# ifdef USE_LOCALE_CTYPE
"LC_CTYPE",
# endif
# ifdef USE_LOCALE_NUMERIC
"LC_NUMERIC",
# endif
# ifdef USE_LOCALE_COLLATE
"LC_COLLATE",
# endif
Expand Down Expand Up @@ -284,12 +284,12 @@ STATIC const char * const category_names[] = {
/* A few categories require additional setup when they are changed. This table
* points to the functions that do that setup */
STATIC void (*update_functions[]) (pTHX_ const char *) = {
# ifdef USE_LOCALE_NUMERIC
S_new_numeric,
# endif
# ifdef USE_LOCALE_CTYPE
S_new_ctype,
# endif
# ifdef USE_LOCALE_NUMERIC
S_new_numeric,
# endif
# ifdef USE_LOCALE_COLLATE
S_new_collate,
# endif
Expand Down Expand Up @@ -591,12 +591,12 @@ Perl_locale_panic(const char * msg,
/* A fourth array, parallel to the ones above to map from category to its
* equivalent mask */
STATIC const int category_masks[] = {
# ifdef USE_LOCALE_NUMERIC
LC_NUMERIC_MASK,
# endif
# ifdef USE_LOCALE_CTYPE
LC_CTYPE_MASK,
# endif
# ifdef USE_LOCALE_NUMERIC
LC_NUMERIC_MASK,
# endif
# ifdef USE_LOCALE_COLLATE
LC_COLLATE_MASK,
# endif
Expand Down
20 changes: 10 additions & 10 deletions perl.h
Expand Up @@ -991,23 +991,23 @@ Example usage:
# endif

/* Now create LC_foo_INDEX_ #defines for just those categories on this system */
# ifdef USE_LOCALE_NUMERIC
# define LC_NUMERIC_INDEX_ 0
# define PERL_DUMMY_NUMERIC_ LC_NUMERIC_INDEX_
# else
# define PERL_DUMMY_NUMERIC_ -1
# endif
# ifdef USE_LOCALE_CTYPE
# define LC_CTYPE_INDEX_ PERL_DUMMY_NUMERIC_ + 1
# define LC_CTYPE_INDEX_ 0
# define PERL_DUMMY_CTYPE_ LC_CTYPE_INDEX_
# else
# define PERL_DUMMY_CTYPE_ PERL_DUMMY_NUMERIC_
# define PERL_DUMMY_CTYPE_ -1
# endif
# ifdef USE_LOCALE_NUMERIC
# define LC_NUMERIC_INDEX_ PERL_DUMMY_CTYPE_ + 1
# define PERL_DUMMY_NUMERIC_ LC_NUMERIC_INDEX_
# else
# define PERL_DUMMY_NUMERIC_ PERL_DUMMY_CTYPE_
# endif
# ifdef USE_LOCALE_COLLATE
# define LC_COLLATE_INDEX_ PERL_DUMMY_CTYPE_ + 1
# define LC_COLLATE_INDEX_ PERL_DUMMY_NUMERIC_ + 1
# define PERL_DUMMY_COLLATE_ LC_COLLATE_INDEX_
# else
# define PERL_DUMMY_COLLATE_ PERL_DUMMY_CTYPE_
# define PERL_DUMMY_COLLATE_ PERL_DUMMY_NUMERIC_
# endif
# ifdef USE_LOCALE_TIME
# define LC_TIME_INDEX_ PERL_DUMMY_COLLATE_ + 1
Expand Down

0 comments on commit 29a7b0d

Please sign in to comment.