]> Savannah Git Hosting - gnulib.git/commitdiff
wchar-single: a new module to enable optimizations in wchar replacements
authorPádraig Brady <P@draigBrady.com>
Mon, 21 May 2018 05:11:12 +0000 (22:11 -0700)
committerPádraig Brady <P@draigBrady.com>
Sun, 24 Jun 2018 02:55:33 +0000 (19:55 -0700)
* lib/mbrtowc.c (mbrtowc): Only check locale_charset() once if
GNULIB_WCHAR_SINGLE is enabled.
* lib/wcwidth.c (wcwidth): Likewise.

ChangeLog
lib/mbrtowc.c
lib/wcwidth.c
modules/wchar
modules/wchar-single [new file with mode: 0644]

index e421f118769d19fbc2e561cb5a7ee31bc41b4144..1b74543ad840ce96c162b70a7bc100ab02c45755 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2018-06-23  Pádraig Brady  <P@draigBrady.com>
+
+       wchar-single: a new module to enable optimizations in wchar replacements
+       * lib/mbrtowc.c (mbrtowc): Only check locale_charset() once if
+       GNULIB_WCHAR_SINGLE is enabled.
+       * lib/wcwidth.c (wcwidth): Likewise.
+
 2018-06-23  Bruno Haible  <bruno@clisp.org>
 
        libc-config: Fix conflict with FreeBSD include files.
index 22ac2c49a9faf7bcf6b3674634fc6d6a43892ed0..dc3597f29da1a9232578ddac37b12917aa6ddaea 100644 (file)
@@ -138,9 +138,19 @@ mbrtowc (wchar_t *pwc, const char *s, size_t n, mbstate_t *ps)
         goto invalid;
       /* Here MB_CUR_MAX > 1 and 0 < m < 4.  */
       {
-        const char *encoding = locale_charset ();
+        static int utf8_charset = -1;
+        static const char *encoding;
 
-        if (STREQ_OPT (encoding, "UTF-8", 'U', 'T', 'F', '-', '8', 0, 0, 0, 0))
+# if GNULIB_WCHAR_SINGLE
+        if (utf8_charset == -1)
+# endif
+          {
+            encoding = locale_charset ();
+            utf8_charset = STREQ_OPT (encoding,
+                                      "UTF-8", 'U', 'T', 'F', '-', '8', 0, 0, 0 ,0);
+          }
+
+        if (utf8_charset)
           {
             /* Cf. unistr/u8-mblen.c.  */
             unsigned char c = (unsigned char) p[0];
index 5fb090ec5d6b7e7e6c6dc029f12bca163e0c4ed7..d03a50f3eb9708c90410ddb7e1302c32184b002b 100644 (file)
@@ -30,9 +30,20 @@ int
 wcwidth (wchar_t wc)
 #undef wcwidth
 {
+  static int utf8_charset = -1;
+  static const char *encoding;
+
+#if GNULIB_WCHAR_SINGLE
+  if (utf8_charset == -1)
+#endif
+    {
+      encoding = locale_charset ();
+      utf8_charset = STREQ_OPT (encoding,
+                                "UTF-8", 'U', 'T', 'F', '-', '8', 0, 0, 0 ,0);
+    }
+
   /* In UTF-8 locales, use a Unicode aware width function.  */
-  const char *encoding = locale_charset ();
-  if (STREQ_OPT (encoding, "UTF-8", 'U', 'T', 'F', '-', '8', 0, 0, 0 ,0))
+  if (utf8_charset)
     {
       /* We assume that in a UTF-8 locale, a wide character is the same as a
          Unicode character.  */
index 31770d8949c7412abda1c7c042bdb155eba5c810..29c67e84795d2eea2822b7f5768e238e6631e2e8 100644 (file)
@@ -1,5 +1,6 @@
 Description:
 A <wchar.h> that works around platform issues.
+Note also the wchar-single module.
 
 Files:
 lib/wchar.in.h
diff --git a/modules/wchar-single b/modules/wchar-single
new file mode 100644 (file)
index 0000000..e047de0
--- /dev/null
@@ -0,0 +1,21 @@
+Description:
+Enable more efficient wchar replacements, where we know
+the locale charset will not change between calls.
+
+Files:
+
+Depends-on:
+wchar
+
+configure.ac:
+gl_MODULE_INDICATOR([wchar-single])
+
+Makefile.am:
+
+Include:
+
+License:
+LGPLv2+
+
+Maintainer:
+all