Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: [bug] locale ctype not always honoured properly in pcre matching



> 2022/09/21 8:08, Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
> 
> I'm not 100% sure, but I think this is because in Src/Modules/pcre.c
> the state of UTF-8 parsing is cached and only changes when the
> MULTIBYTE option is different upon re-entry.  Changing the locale
> doesn't have that effect.

Yes.
The following patch seems to solve the problem.
With this patch strcmp(nl_langinfo(CODESET),..) is called every time
pcre matching is used, but I think the overhead is negligible.
For example, I tried

time (repeat 1000000; do [[ 'a' =~ '^.\z' ]]; done)

before and after the patch, but the time difference was negligible
at least on my Mac (both are about 3 seconds).



diff --git a/Src/Modules/pcre.c b/Src/Modules/pcre.c
index 6289e003e..46875a59b 100644
--- a/Src/Modules/pcre.c
+++ b/Src/Modules/pcre.c
@@ -47,8 +47,6 @@ zpcre_utf8_enabled(void)
 #if defined(MULTIBYTE_SUPPORT) && defined(HAVE_NL_LANGINFO) && defined(CODESET)
     static int have_utf8_pcre = -1;
 
-    /* value can toggle based on MULTIBYTE, so don't
-     * be too eager with caching */
     if (have_utf8_pcre < -1)
 	return 0;
 
@@ -56,15 +54,11 @@ zpcre_utf8_enabled(void)
 	return 0;
 
     if ((have_utf8_pcre == -1) &&
-        (!strcmp(nl_langinfo(CODESET), "UTF-8"))) {
-
-	if (pcre_config(PCRE_CONFIG_UTF8, &have_utf8_pcre))
+	(pcre_config(PCRE_CONFIG_UTF8, &have_utf8_pcre))) {
 	    have_utf8_pcre = -2; /* erk, failed to ask */
     }
 
-    if (have_utf8_pcre < 0)
-	return 0;
-    return have_utf8_pcre;
+    return (have_utf8_pcre == 1) && (!strcmp(nl_langinfo(CODESET), "UTF-8"));
 
 #else
     return 0;








Messages sorted by: Reverse Date, Date, Thread, Author