This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Perl_locale(): Refactor for clarity
authorKarl Williamson <khw@cpan.org>
Mon, 24 Jul 2017 04:08:50 +0000 (22:08 -0600)
committerKarl Williamson <khw@cpan.org>
Thu, 9 Nov 2017 02:50:29 +0000 (19:50 -0700)
This code is full of 'if's interrupted by #ifdefs, which makes it hard
to read.  Changing it to a switch() makes it much easier to understand.

locale.c

index 7d29aef..364eb0b 100644 (file)
--- a/locale.c
+++ b/locale.c
@@ -946,6 +946,7 @@ Perl_setlocale(int category, const char * locale)
     /* This wraps POSIX::setlocale() */
 
     char * retval;
+    char * newlocale;
     dTHX;
 
 #ifdef USE_LOCALE_NUMERIC
@@ -996,103 +997,63 @@ Perl_setlocale(int category, const char * locale)
         SET_NUMERIC_STANDARD();
         return retval;
     }
-    else {  /* Now that have switched locales, we have to update our records to
-               correspond */
-
-#ifdef USE_LOCALE_CTYPE
 
-        if (   category == LC_CTYPE
-
-#  ifdef LC_ALL
+    /* Now that have switched locales, we have to update our records to
+     * correspond. */
 
-            || category == LC_ALL
-
-#  endif
-
-            )
-        {
-            char *newctype;
-
-#  ifdef LC_ALL
-
-            if (category == LC_ALL) {
-                newctype = do_setlocale_c(LC_CTYPE, NULL);
-                DEBUG_Lv(PerlIO_printf(Perl_debug_log,
-                    "%s:%d: %s\n", __FILE__, __LINE__,
-                    setlocale_debug_string(LC_CTYPE, NULL, newctype)));
-            }
-            else
+    switch (category) {
 
-#  endif
+#ifdef USE_LOCALE_CTYPE
 
-                newctype = retval;
-            new_ctype(newctype);
-        }
+        case LC_CTYPE:
+            new_ctype(retval);
+            break;
 
-#endif /* USE_LOCALE_CTYPE */
+#endif
 #ifdef USE_LOCALE_COLLATE
 
-        if (   category == LC_COLLATE
-
-#  ifdef LC_ALL
-
-            || category == LC_ALL
-
-#  endif
-
-            )
-        {
-            char *newcoll;
-
-#  ifdef LC_ALL
-
-            if (category == LC_ALL) {
-                newcoll = do_setlocale_c(LC_COLLATE, NULL);
-                DEBUG_Lv(PerlIO_printf(Perl_debug_log,
-                    "%s:%d: %s\n", __FILE__, __LINE__,
-                    setlocale_debug_string(LC_COLLATE, NULL, newcoll)));
-            }
-            else
-
-#  endif
-
-                newcoll = retval;
-            new_collate(newcoll);
-        }
+        case LC_COLLATE:
+            new_collate(retval);
+            break;
 
-#endif /* USE_LOCALE_COLLATE */
+#endif
 #ifdef USE_LOCALE_NUMERIC
 
-        if (   category == LC_NUMERIC
+        case LC_NUMERIC:
+            new_numeric(retval);
+            break;
+
+#endif
+#ifdef LC_ALL
 
-#  ifdef LC_ALL
+        case LC_ALL:
 
-            || category == LC_ALL
+            /* LC_ALL updates all the things we care about.  The values may not
+             * be the same as 'retval', as the locale "" may have set things
+             * individually */
 
-#  endif
+#  ifdef USE_LOCALE_CTYPE
 
-            )
-        {
-            char *newnum;
+            newlocale = do_setlocale_c(LC_CTYPE, NULL);
+            new_ctype(newlocale);
 
-#  ifdef LC_ALL
+#  endif /* USE_LOCALE_CTYPE */
+#  ifdef USE_LOCALE_COLLATE
 
-            if (category == LC_ALL) {
-                newnum = do_setlocale_c(LC_NUMERIC, NULL);
-                DEBUG_Lv(PerlIO_printf(Perl_debug_log,
-                    "%s:%d: %s\n", __FILE__, __LINE__,
-                    setlocale_debug_string(LC_NUMERIC, NULL, newnum)));
-            }
-            else
+            newlocale = do_setlocale_c(LC_COLLATE, NULL);
+            new_collate(newlocale);
 
 #  endif
+#  ifdef USE_LOCALE_NUMERIC
 
-                newnum = retval;
-            new_numeric(newnum);
-        }
+            newlocale = do_setlocale_c(LC_NUMERIC, NULL);
+            new_numeric(newlocale);
 
-#endif /* USE_LOCALE_NUMERIC */
+#  endif /* USE_LOCALE_NUMERIC */
+#endif /* LC_ALL */
 
+        default:
+            break;
     }
 
     return retval;