This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
locale.c: Avoid extra call to mbtowc()
authorKarl Williamson <khw@cpan.org>
Mon, 11 Sep 2017 23:32:42 +0000 (17:32 -0600)
committerKarl Williamson <khw@cpan.org>
Thu, 9 Nov 2017 03:52:52 +0000 (20:52 -0700)
This is done only when debugging, but in some locales that have shift
states, the extra call could blow up.  Instead save the result of the
mbtowc() call we care about.

locale.c

index 3d68958..d04fb66 100644 (file)
--- a/locale.c
+++ b/locale.c
@@ -3055,17 +3055,20 @@ Perl__is_cur_LC_category_utf8(pTHX_ int category)
          * result */
         if (is_utf8) {
             wchar_t wc;
+            int len;
+
             PERL_UNUSED_RESULT(mbtowc(&wc, NULL, 0));/* Reset any shift state */
             errno = 0;
-            if ((size_t)mbtowc(&wc, HYPHEN_UTF8, strlen(HYPHEN_UTF8))
-                                                        != strlen(HYPHEN_UTF8)
+            len = mbtowc(&wc, STR_WITH_LEN(HYPHEN_UTF8));
+
+            if (   len != STRLENs(HYPHEN_UTF8)
                 || wc != (wchar_t) 0x2010)
             {
                 is_utf8 = FALSE;
                 DEBUG_L(PerlIO_printf(Perl_debug_log, "\thyphen=U+%x\n", (unsigned int)wc));
                 DEBUG_L(PerlIO_printf(Perl_debug_log,
                         "\treturn from mbtowc=%d; errno=%d; ?UTF8 locale=0\n",
-                        mbtowc(&wc, HYPHEN_UTF8, strlen(HYPHEN_UTF8)), errno));
+                                               len,      errno));
             }
         }