This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Swap the ordering of two locale category indices
authorKarl Williamson <khw@cpan.org>
Sun, 21 Feb 2021 16:15:48 +0000 (09:15 -0700)
committerKarl Williamson <khw@cpan.org>
Wed, 10 Aug 2022 00:05:59 +0000 (18:05 -0600)
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, then by doing
it first and caching the result, the other categories likely
automatically will use the same value, without having to recalculate.

locale.c
perl.h

index 0177afb..57ca7de 100644 (file)
--- a/locale.c
+++ b/locale.c
@@ -180,12 +180,12 @@ STATIC_ASSERT_DECL(STRLENs(UTF8NESS_PREFIX) == 1);
 
 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
@@ -232,12 +232,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
@@ -283,12 +283,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
@@ -605,12 +605,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
diff --git a/perl.h b/perl.h
index 3117e58..058d569 100644 (file)
--- a/perl.h
+++ b/perl.h
@@ -1210,23 +1210,23 @@ violations are fatal.
 #   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