zsh-workers
 help / color / mirror / code / Atom feed
* [bug] locale ctype not always honoured properly in pcre matching
@ 2022-09-20 13:54 Stephane Chazelas
  2022-09-20 23:08 ` Bart Schaefer
  0 siblings, 1 reply; 3+ messages in thread
From: Stephane Chazelas @ 2022-09-20 13:54 UTC (permalink / raw)
  To: Zsh hackers list

$ locale charmap
UTF-8
$ set -o rematchpcre
$ LC_ALL=C [ $'\xc3\xa9' '=~' '^..\z' ] && echo yes
yes

OK, in C locale, those two bytes are considered as two characters.

$ [ $'\xc3\xa9' '=~' '^..\z' ] && echo yes
$

OK, in UTF-8, those two bytes form one é character

$ LC_ALL=C [ $'\xc3\xa9' '=~' '^..\z' ] && echo yes
$

Same command as above, but now it doesn't match (?!) and instead:

$ LC_ALL=C [ $'\xc3\xa9' '=~' '^.\z' ] && echo yes
yes

Behaves  as if doing a match in UTF-8.

Same goes with:

$ PS1='$ ' zsh -f
$ set -o rematchpcre
$ (LC_ALL=C; [[ $'\xc3\xa9' =~ '^..\z' ]] && echo yes )
yes
$ [[ $'\xc3\xa9' =~ '^..\z' ]] && echo yes
$ (LC_ALL=C; [[ $'\xc3\xa9' =~ '^..\z' ]] && echo yes )
$

-- 
Stephane



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [bug] locale ctype not always honoured properly in pcre matching
  2022-09-20 13:54 [bug] locale ctype not always honoured properly in pcre matching Stephane Chazelas
@ 2022-09-20 23:08 ` Bart Schaefer
  2022-09-21 17:41   ` Jun. T
  0 siblings, 1 reply; 3+ messages in thread
From: Bart Schaefer @ 2022-09-20 23:08 UTC (permalink / raw)
  To: Zsh hackers list

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.


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [bug] locale ctype not always honoured properly in pcre matching
  2022-09-20 23:08 ` Bart Schaefer
@ 2022-09-21 17:41   ` Jun. T
  0 siblings, 0 replies; 3+ messages in thread
From: Jun. T @ 2022-09-21 17:41 UTC (permalink / raw)
  To: zsh-workers


> 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;






^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-09-21 17:42 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-20 13:54 [bug] locale ctype not always honoured properly in pcre matching Stephane Chazelas
2022-09-20 23:08 ` Bart Schaefer
2022-09-21 17:41   ` Jun. T

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).