summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog7
-rw-r--r--lib/mbrtowc.c14
-rw-r--r--lib/wcwidth.c15
-rw-r--r--modules/wchar1
-rw-r--r--modules/wchar-single21
5 files changed, 54 insertions, 4 deletions
diff --git a/ChangeLog b/ChangeLog
index e421f11876..1b74543ad8 100644
--- 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.
diff --git a/lib/mbrtowc.c b/lib/mbrtowc.c
index 22ac2c49a9..dc3597f29d 100644
--- a/lib/mbrtowc.c
+++ b/lib/mbrtowc.c
@@ -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];
diff --git a/lib/wcwidth.c b/lib/wcwidth.c
index 5fb090ec5d..d03a50f3eb 100644
--- a/lib/wcwidth.c
+++ b/lib/wcwidth.c
@@ -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. */
diff --git a/modules/wchar b/modules/wchar
index 31770d8949..29c67e8479 100644
--- a/modules/wchar
+++ b/modules/wchar
@@ -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
index 0000000000..e047de008b
--- /dev/null
+++ b/modules/wchar-single
@@ -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