This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
locale.c: Improve debugging output
authorKarl Williamson <khw@cpan.org>
Sun, 31 Dec 2017 14:53:32 +0000 (07:53 -0700)
committerKarl Williamson <khw@cpan.org>
Mon, 1 Jan 2018 19:37:49 +0000 (12:37 -0700)
This extracts some debugging output code into a function (so that it can
be called from more than one place) and add some detail to the output.

embed.fnc
embed.h
locale.c
proto.h

index 7f2a77d..25cad01 100644 (file)
--- a/embed.fnc
+++ b/embed.fnc
@@ -2758,6 +2758,7 @@ s |bool   |isa_lookup     |NN HV *stash|NN const char * const name \
 #endif
 
 #if defined(PERL_IN_LOCALE_C)
+sn     |const char*|category_name |const int category
 #  ifdef HAS_NL_LANGINFO
 sn     |const char*|my_nl_langinfo|const nl_item item|bool toggle
 #  else
diff --git a/embed.h b/embed.h
index 9d061fb..44f16af 100644 (file)
--- a/embed.h
+++ b/embed.h
 #define unshare_hek_or_pvn(a,b,c,d)    S_unshare_hek_or_pvn(aTHX_ a,b,c,d)
 #  endif
 #  if defined(PERL_IN_LOCALE_C)
+#define category_name          S_category_name
 #define save_to_buffer         S_save_to_buffer
 #    if defined(USE_LOCALE)
 #define new_collate(a)         S_new_collate(aTHX_ a)
index a4ed7ab..3756f5a 100644 (file)
--- a/locale.c
+++ b/locale.c
@@ -201,6 +201,50 @@ const char * category_names[] = {
  * checked for at compile time by using the #define LC_ALL_INDEX which is only
  * defined if we do have LC_ALL. */
 
+STATIC const char *
+S_category_name(const int category)
+{
+    unsigned int i;
+
+#ifdef LC_ALL
+
+    if (category == LC_ALL) {
+        return "LC_ALL";
+    }
+
+#endif
+
+    for (i = 0; i < NOMINAL_LC_ALL_INDEX; i++) {
+        if (category == categories[i]) {
+            return category_names[i];
+        }
+    }
+
+    {
+        const char suffix[] = " (unknown)";
+        int temp = category;
+        Size_t length = sizeof(suffix) + 1;
+        char * unknown;
+        dTHX;
+
+        if (temp < 0) {
+            length++;
+            temp = - temp;
+        }
+
+        /* Calculate the number of digits */
+        while (temp >= 10) {
+            temp /= 10;
+            length++;
+        }
+
+        Newx(unknown, length, char);
+        my_snprintf(unknown, length, "%d%s", category, suffix);
+        SAVEFREEPV(unknown);
+        return unknown;
+    }
+}
+
 /* Now create LC_foo_INDEX #defines for just those categories on this system */
 #  ifdef USE_LOCALE_NUMERIC
 #    define LC_NUMERIC_INDEX            0
@@ -3614,34 +3658,9 @@ S_setlocale_debug_string(const int category,        /* category number,
     static char ret[128] = "If you can read this, thank your buggy C"
                            " library strlcpy(), and change your hints file"
                            " to undef it";
-    unsigned int i;
-
-#  ifdef LC_ALL
-
-    const unsigned int highest_index = LC_ALL_INDEX;
-
-#  else
-
-    const unsigned int highest_index = NOMINAL_LC_ALL_INDEX - 1;
-
-#endif
-
 
     my_strlcpy(ret, "setlocale(", sizeof(ret));
-
-    /* Look for category in our list, and if found, add its name */
-    for (i = 0; i <= highest_index; i++) {
-        if (category == categories[i]) {
-            my_strlcat(ret, category_names[i], sizeof(ret));
-            goto found_category;
-        }
-    }
-
-    /* Unknown category to us */
-    my_snprintf(ret, sizeof(ret), "%s? %d", ret, category);
-
-  found_category:
-
+    my_strlcat(ret, category_name(category), sizeof(ret));
     my_strlcat(ret, ", ", sizeof(ret));
 
     if (locale) {
diff --git a/proto.h b/proto.h
index ed77042..7d99cfb 100644 (file)
--- a/proto.h
+++ b/proto.h
@@ -4679,6 +4679,7 @@ PERL_CALLCONV SV* Perl_hfree_next_entry(pTHX_ HV *hv, STRLEN *indexp);
        assert(hv); assert(indexp)
 #endif
 #if defined(PERL_IN_LOCALE_C)
+STATIC const char*     S_category_name(const int category);
 #ifndef PERL_NO_INLINE_FUNCTIONS
 PERL_STATIC_INLINE const char *        S_save_to_buffer(const char * string, char **buf, Size_t *buf_size, const Size_t offset);
 #define PERL_ARGS_ASSERT_SAVE_TO_BUFFER        \