zsh-workers
 help / color / mirror / code / Atom feed
From: "Jun. T" <takimoto-j@kba.biglobe.ne.jp>
To: zsh-workers@zsh.org
Subject: Re: [bug] locale ctype not always honoured properly in pcre matching
Date: Thu, 22 Sep 2022 02:41:39 +0900	[thread overview]
Message-ID: <689789F5-F64A-4692-9973-8045A777CD2F@kba.biglobe.ne.jp> (raw)
In-Reply-To: <CAH+w=7YCobo8V2DiiuVVeQWB4uwc+rUuk=XVuAmU31RqRz9M8Q@mail.gmail.com>


> 2022/09/21 8:08, Bart Schaefer <schaefer@brasslantern.com> 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;






      reply	other threads:[~2022-09-21 17:42 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-20 13:54 Stephane Chazelas
2022-09-20 23:08 ` Bart Schaefer
2022-09-21 17:41   ` Jun. T [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=689789F5-F64A-4692-9973-8045A777CD2F@kba.biglobe.ne.jp \
    --to=takimoto-j@kba.biglobe.ne.jp \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).