threads / discuss / 59531

-P '\d' in GNU and git grep

Subject: -P '\d' in GNU and git grep

## tl;dr

19 messages between Apr 3, 2023 and Apr 8, 2023.

replies: 18people: 5as markdown or json

Paul Eggert· Apr 3, 2023, 21:38 UTC · lore

I've recently done some bug-report maintenance about a set of GNU grep bug reports related to whether whether "grep -P '\d'" should match non-ASCII digits, and have some thoughts about coordinating GNU grep with git grep in this department.

GNU Bug#62605[1] "`[\d]` does not work with PCRE" has been fixed on Savannah's copy of GNU grep, and some sort of fix should appear in the next grep release. However, I'm leaving the GNU grep bug report open for now because it's related to Bug#60690[2] "[PATCH v2] grep: correctly identify utf-8 characters with \{b,w} in -P" and to Bug#62552[3] "Bug found in latest stable release v3.10 of grep". I merged these related bug reports, and the oldest one, Bug#60690, is now the representative displayed in the GNU grep bug list[4].

For this set of grep bug reports there's still a pending issue discussed in my recent email[5], which proposes a patch so I've tagged Bug#60690 with "patch". The proposal is that GNU grep -P '\d' should revert to the grep 3.9 behavior, i.e., that in a UTF-8 locale, \d should also match non-ASCII decimal digits.

In researching this a bit further, I found that on March 23 Git disabled the use of PCRE2_UCP in PCRE2 10.34 or earlier[6], due to a PCRE2 bug that can cause a crash when PCRE2_UCP is used[7]. A bug fix[8] should appear in the next PCRE2 release.

When PCRE2 10.35 comes out, it appears that 'git grep -P' will behave like 'grep -P' only if GNU grep adopts something like the solution proposed in [5].

[1]: https://bugs.gnu.org/62605 [2]: https://bugs.gnu.org/60690 [3]: https://bugs.gnu.org/62552 [4]: https://debbugs.gnu.org/cgi/pkgreport.cgi?package=grep [5]: https://lists.gnu.org/archive/html/grep-devel/2023-04/msg00004.html [6]: https://github.com/git/git/commit/14b9a044798ebb3858a1f1a1377309a3d6054ac8 [7]: https://lore.kernel.org/git/7E83DAA1-F9A9-4151-8D07-D80EA6D59EEA@clumio.com/ [8]: https://github.com/git/git/commit/14b9a044798ebb3858a1f1a1377309a3d6054ac8

Jim Meyering· Apr 4, 2023, 03:30 UTC · re: Paul Eggert · lore

Re: bug#60690: -P '\d' in GNU and git grep

On Mon, Apr 3, 2023 at 2:39 PM Paul Eggert <eggert@cs.ucla.edu> wrote:
Show 26 quoted lines
> I've recently done some bug-report maintenance about a set of GNU grep
> bug reports related to whether whether "grep -P '\d'" should match
> non-ASCII digits, and have some thoughts about coordinating GNU grep
> with git grep in this department.
>
> GNU Bug#62605[1] "`[\d]` does not work with PCRE" has been fixed on
> Savannah's copy of GNU grep, and some sort of fix should appear in the
> next grep release. However, I'm leaving the GNU grep bug report open for
> now because it's related to Bug#60690[2] "[PATCH v2] grep: correctly
> identify utf-8 characters with \{b,w} in -P" and to Bug#62552[3] "Bug
> found in latest stable release v3.10 of grep". I merged these related
> bug reports, and the oldest one, Bug#60690, is now the representative
> displayed in the GNU grep bug list[4].
>
> For this set of grep bug reports there's still a pending issue discussed
> in my recent email[5], which proposes a patch so I've tagged Bug#60690
> with "patch". The proposal is that GNU grep -P '\d' should revert to the
> grep 3.9 behavior, i.e., that in a UTF-8 locale, \d should also match
> non-ASCII decimal digits.
>
> In researching this a bit further, I found that on March 23 Git disabled
> the use of PCRE2_UCP in PCRE2 10.34 or earlier[6], due to a PCRE2 bug
> that can cause a crash when PCRE2_UCP is used[7]. A bug fix[8] should
> appear in the next PCRE2 release.
>
> When PCRE2 10.35 comes out,

Thanks for finding that. It's clearly a good idea to disable PCRE2_UCP for those using those older, known-buggy versions of pcre2.

The latest is 10.42, per https://github.com/PCRE2Project/pcre2/releases
Show 15 quoted lines
> it appears that 'git grep -P' will behave
> like 'grep -P' only if GNU grep adopts something like the solution
> proposed in [5].
>
> [1]: https://bugs.gnu.org/62605
> [2]: https://bugs.gnu.org/60690
> [3]: https://bugs.gnu.org/62552
> [4]: https://debbugs.gnu.org/cgi/pkgreport.cgi?package=grep
> [5]: https://lists.gnu.org/archive/html/grep-devel/2023-04/msg00004.html
> [6]:
> https://github.com/git/git/commit/14b9a044798ebb3858a1f1a1377309a3d6054ac8
> [7]:
> https://lore.kernel.org/git/7E83DAA1-F9A9-4151-8D07-D80EA6D59EEA@clumio.com/
> [8]:
> https://github.com/git/git/commit/14b9a044798ebb3858a1f1a1377309a3d6054ac8

Thanks for all of the links. However, have you seen justification (other than for compatibility with some other tool or language) for allowing \d to match non-ASCII by default, in spite of the risks? IMHO, we have an obligation to retain compatibility with how grep -P '\d' has worked since -P was added. I'd be happy to see an option to enable the match-multibyte-digits behavior, but making it the default seems too likely to introduce unwarranted risk.

Paul Eggert· Apr 4, 2023, 06:46 UTC · re: Jim Meyering · lore

Re: bug#60690: -P '\d' in GNU and git grep

On 2023-04-03 20:30, Jim Meyering wrote:
> have you seen justification
> (other than for compatibility with some other tool or language) for
> allowing \d to match non-ASCII by default, in spite of the risks?

In the example Ævar supplied in <https://bugs.gnu.org/60690>, my impression was that it was better when \d matched non-ASCII digits. That is, in a UTF-8 locale it's better when \d finds matches in these lines:

>> 	> git-gui/po/ja.po:"- 第1行: 何をしたか、を1行で要約。\n"
>> 	> git-gui/po/ja.po:"- 第2行: 空白\n"

because they contain the Japanese digits "1" and "2". This was the only example I recall being given.

Also, I find it odd that grep -P '^[\w\d]*$' matches lines containing any sort of Arabic word characters, but it rejects lines containing Arabic digits like "٣" that are perfectly reasonable in Arabic-language text. I also find it odd that [\d] and [[:digit:]] mean different things.

There are arguments on the other side, otherwise we wouldn't be having this discussion. And it's true that grep -P '\d' formerly rejected Arabic digits (though it's also true that grep -P '\w' formerly rejected Arabic letters...). Still, the cure's oddness and incompatibility with Git, Perl, etc. appears to me to be worse than the disease of dealing with grep -P invocations that need to use [0-9] or LC_ALL="C" anyway if they want to be portable to any program other than GNU grep.

Jim Meyering· Apr 4, 2023, 15:31 UTC · re: Paul Eggert · lore

Re: bug#60690: -P '\d' in GNU and git grep

On Mon, Apr 3, 2023 at 11:47 PM Paul Eggert <eggert@cs.ucla.edu> wrote:
Show 14 quoted lines
> On 2023-04-03 20:30, Jim Meyering wrote:
> > have you seen justification
> > (other than for compatibility with some other tool or language) for
> > allowing \d to match non-ASCII by default, in spite of the risks?
>
> In the example Ævar supplied in <https://bugs.gnu.org/60690>, my
> impression was that it was better when \d matched non-ASCII digits. That
> is, in a UTF-8 locale it's better when \d finds matches in these lines:
>
> >>      > git-gui/po/ja.po:"- 第1行: 何をしたか、を1行で要約。\n"
> >>      > git-gui/po/ja.po:"- 第2行: 空白\n"
>
> because they contain the Japanese digits "1" and "2". This was the only
> example I recall being given.

Before it was unintentionally enabled in grep-3.9, lines like that have never been matched by grep -P's '\d'. By relaxing \d, we'd weaken any application that uses say grep -P '^\d+$' to perform input validation intending to ensure that some input is all ASCII digits. It's not a big stretch to imagine that some downstream processor of that "verified" data is not prepared to deal with multi-byte digits.

Show 12 quoted lines
> Also, I find it odd that grep -P '^[\w\d]*$' matches lines containing
> any sort of Arabic word characters, but it rejects lines containing
> Arabic digits like "٣" that are perfectly reasonable in Arabic-language
> text. I also find it odd that [\d] and [[:digit:]] mean different things.
>
> There are arguments on the other side, otherwise we wouldn't be having
> this discussion. And it's true that grep -P '\d' formerly rejected
> Arabic digits (though it's also true that grep -P '\w' formerly rejected
> Arabic letters...). Still, the cure's oddness and incompatibility with
> Git, Perl, etc. appears to me to be worse than the disease of dealing
> with grep -P invocations that need to use [0-9] or LC_ALL="C" anyway if
> they want to be portable to any program other than GNU grep.

I'm primarily concerned about not introducing a persistent regression in how GNU grep's -P '\d' works in multibyte locales. The corner cases you mention do matter, of course, but are far less likely to matter in practice.

Carlo Arenas· Apr 4, 2023, 06:56 UTC · re: Paul Eggert · lore

Re: -P '\d' in GNU and git grep

On Mon, Apr 3, 2023 at 2:38 PM Paul Eggert <eggert@cs.ucla.edu> wrote:
Show 5 quoted lines
>
> In researching this a bit further, I found that on March 23 Git disabled
> the use of PCRE2_UCP in PCRE2 10.34 or earlier[6], due to a PCRE2 bug
> that can cause a crash when PCRE2_UCP is used[7]. A bug fix[8] should
> appear in the next PCRE2 release.
Presume PCRE2 is a typo and should have been "git" here?

FWIW the PCRE2 fix[1] has been released already with 10.35 and backporting to the Ubuntu 20.04 package that crashed in the original report would also solve the crash with 10.34.

Carlo
[1] https://github.com/PCRE2Project/pcre2/commit/c21bd977547d
Paul Eggert· Apr 4, 2023, 18:25 UTC · re: Carlo Arenas · lore

Re: bug#60690: -P '\d' in GNU and git grep

On 4/3/23 23:56, Carlo Arenas wrote:
Show 8 quoted lines
> On Mon, Apr 3, 2023 at 2:38 PM Paul Eggert <eggert@cs.ucla.edu> wrote:
>>
>> on March 23 Git disabled
>> the use of PCRE2_UCP in PCRE2 10.34 or earlier[6], due to a PCRE2 bug
>> that can cause a crash when PCRE2_UCP is used[7]. A bug fix[8] should
>> appear in the next PCRE2 release.
> 
> Presume PCRE2 is a typo and should have been "git" here?

No, I was talking about what options Git uses when it calls PCRE2 functions. In other words, this is about whether GNU 'grep -P' should be compatible with 'git grep -P' (as well as with Perl and with pcregrep), when interpreting \d and similar constructs.

This is an evolving area. Git master is fiddling with flags and options, and so is GNU grep master, and so is PCRE2, and there are bugs. If you're running bleeding-edge versions of this code you'll get different behavior than if you're running grep 3.8, pcregrep 8.45, Perl 5.36, and git 2.39.2 (which is what Fedora 37 has).

What I'm fearing is that we may evolve into mutually incompatible interpretations of how Perl regular expressions deal with UTF-8 text. That'd be a recipe for confusion down the road.

Junio C Hamano· Apr 4, 2023, 19:31 UTC · re: Paul Eggert · lore

Re: bug#60690: -P '\d' in GNU and git grep

Paul Eggert <eggert@cs.ucla.edu> writes:
Show 9 quoted lines
> This is an evolving area. Git master is fiddling with flags and
> options, and so is GNU grep master, and so is PCRE2, and there are
> bugs. If you're running bleeding-edge versions of this code you'll get
> different behavior than if you're running grep 3.8, pcregrep 8.45,
> Perl 5.36, and git 2.39.2 (which is what Fedora 37 has).
>
> What I'm fearing is that we may evolve into mutually incompatible
> interpretations of how Perl regular expressions deal with UTF-8
> text. That'd be a recipe for confusion down the road.

Nicely said. My personal inclination is to let Perl folks decide and follow them (even though I am skeptical about the wisdom of letting '\d' match anything other than [0-9]), but even in Git circle there would be different opinions, so I am glad that the discussion is visible on the list to those who are intrested.

Paul Eggert· Apr 5, 2023, 18:32 UTC · re: Junio C Hamano · lore

Re: bug#60690: -P '\d' in GNU and git grep

On 2023-04-04 12:31, Junio C Hamano wrote:
> My personal inclination is to let Perl folks decide
> and follow them (even though I am skeptical about the wisdom of
> letting '\d' match anything other than [0-9])

I looked into what pcre2grep does. It has always done only 8-bit processing unless you use the -u or --utf option, so plain "pcre2grep '\d'" matches only ASCII digits.

Although this causes pcre2grep to mishandle Unicode characters:
   $ echo 'Ævar' | pcre2grep '[Ssß]'
   Ævar
it mimics Perl 5.36:
   $ echo 'Ævar' | perl -ne 'print $_ if /[Ssß]/'
   Ævar
so this seems to be what Perl users expect, despite its infelicities.

For better Unicode handling one can use pcre2grep's -u or --utf option, which causes pcre2grep to behave more like GNU grep -P and git grep -P: "echo 'Ævar' | pcre2grep -u '[Ssß]'" outputs nothing, which I think is what most people would expect (unless they're Perl users :-).

Neither git grep -P nor the current release of pcre2grep -u have \d matching non-ASCII digits, because they do not use PCRE2_UCP. However, in a February 8 commit[1], Philip Hazel changed pcre2grep to use PCRE2_UCP, so this will mean 10.43 pcre2grep -u will behave like 3.9 GNU grep -P did (though 3.10 has changed this).

That February commit also added a --no-ucp option, to disable PCRE2_UCP. So as I understand it, if you're in a UTF-8 locale:

* 10.43 pcre2grep -u will behave like 3.9 GNU grep -P.
* 10.43 pcre2grep -u --no-ucp will behave like git grep -P.
* Current GNU grep -P is different from everybody else.
This incompatibility is not good.

Here are two ways forward to fix this incompatibility (there are other possibilities of course):

(A) GNU grep adds a --no-ucp option that acts like 10.43 pcre2grep --no-ucp, and git grep -P follows suit. That is, both GNU and git grep act like 10.43 pcre2grep -u, in that they enable PCRE2_UTF, and also enable PCRE2_UCP unless --no-ucp is given. This would cause \d to match non-ASCII digits unless --no-ucp is given.

(B) GNU grep -P and git grep -P mimic pcre2grep in both -u and --no-ucp. That is, they would both do 8-bit-only by default, and use PCRE2_UTF only when -u or --utf is given, and use PCRE2_UCP only when --no-ucp is absent. This would cause \d to match non-ASCII digits only when -u is given but --no-ucp is not.

Under either (A) or (B), future pcre2grep -u, GNU grep -P, and git grep -P would be consistent.

I mildly prefer (B) but (A) would also work. (One advantage of (B) is that it should be faster....)

[1]: https://github.com/PCRE2Project/pcre2/commit/8385df8c97b6f8069a48e600c7e4e94cc3e3ebd9ht

Paul Eggert· Apr 5, 2023, 19:04 UTC · re: Paul Eggert · lore

Re: bug#60690: -P '\d' in GNU and git grep

On 2023-04-05 11:32, Paul Eggert wrote:
> in a February 8 commit[1], Philip Hazel changed pcre2grep to use 
> PCRE2_UCP, so this will mean 10.43 pcre2grep -u will behave like 3.9 GNU 
> grep -P did (though 3.10 has changed this).

Sorry, due to fumblefingers I gave the wrong URL for [1]. Here's a corrected URL:

https://github.com/PCRE2Project/pcre2/commit/8385df8c97b6f8069a48e600c7e4e94cc3e3ebd9

It also mentions a new --case-restrict option, intended for 10.43 pcre2grep. Given Perl's and PCRE2's plethora of options I suppose one could imagine several other options of that ilk.

Junio C Hamano· Apr 5, 2023, 19:37 UTC · re: Paul Eggert · lore

Re: bug#60690: -P '\d' in GNU and git grep

Paul Eggert <eggert@cs.ucla.edu> writes:
Show 20 quoted lines
> Here are two ways forward to fix this incompatibility (there are other
> possibilities of course):
>
> (A) GNU grep adds a --no-ucp option that acts like 10.43 pcre2grep
> --no-ucp, and git grep -P follows suit. That is, both GNU and git grep
> act like 10.43 pcre2grep -u, in that they enable PCRE2_UTF, and also
> enable PCRE2_UCP unless --no-ucp is given. This would cause \d to
> match non-ASCII digits unless --no-ucp is given.
>
> (B) GNU grep -P and git grep -P mimic pcre2grep in both -u and
> --no-ucp. That is, they would both do 8-bit-only by default, and use
> PCRE2_UTF only when -u or --utf is given, and use PCRE2_UCP only when
> --no-ucp is absent. This would cause \d to match non-ASCII digits only
> when -u is given but --no-ucp is not.
>
> Under either (A) or (B), future pcre2grep -u, GNU grep -P, and git
> grep -P would be consistent.
>
> I mildly prefer (B) but (A) would also work. (One advantage of (B) is
> that it should be faster....)

For "git grep -P", I would like to hear from Carlo and Ævar; I agree both (A) and (B) would be workable solutions, and have a slight preference on a solution that does not add more options that take only in effect when -P is given, simply because these options are cumbersome to document and explain, but that is a very minor point.

Thanks.
Jim Meyering· Apr 5, 2023, 19:40 UTC · re: Paul Eggert · lore

Re: bug#60690: -P '\d' in GNU and git grep

On Wed, Apr 5, 2023 at 11:33 AM Paul Eggert <eggert@cs.ucla.edu> wrote:
Show 25 quoted lines
> On 2023-04-04 12:31, Junio C Hamano wrote:
> > My personal inclination is to let Perl folks decide
> > and follow them (even though I am skeptical about the wisdom of
> > letting '\d' match anything other than [0-9])
>
> I looked into what pcre2grep does. It has always done only 8-bit
> processing unless you use the -u or --utf option, so plain "pcre2grep
> '\d'" matches only ASCII digits.
>
> Although this causes pcre2grep to mishandle Unicode characters:
>
>    $ echo 'Ævar' | pcre2grep '[Ssß]'
>    Ævar
>
> it mimics Perl 5.36:
>
>    $ echo 'Ævar' | perl -ne 'print $_ if /[Ssß]/'
>    Ævar
>
> so this seems to be what Perl users expect, despite its infelicities.
>
> For better Unicode handling one can use pcre2grep's -u or --utf option,
> which causes pcre2grep to behave more like GNU grep -P and git grep -P:
> "echo 'Ævar' | pcre2grep -u '[Ssß]'" outputs nothing, which I think is
> what most people would expect (unless they're Perl users :-).
Good argument for making PCRE2_UCP the default.
Show 31 quoted lines
> Neither git grep -P nor the current release of pcre2grep -u have \d
> matching non-ASCII digits, because they do not use PCRE2_UCP. However,
> in a February 8 commit[1], Philip Hazel changed pcre2grep to use
> PCRE2_UCP, so this will mean 10.43 pcre2grep -u will behave like 3.9 GNU
> grep -P did (though 3.10 has changed this).
>
> That February commit also added a --no-ucp option, to disable PCRE2_UCP.
> So as I understand it, if you're in a UTF-8 locale:
>
> * 10.43 pcre2grep -u will behave like 3.9 GNU grep -P.
>
> * 10.43 pcre2grep -u --no-ucp will behave like git grep -P.
>
> * Current GNU grep -P is different from everybody else.
>
> This incompatibility is not good.
>
> Here are two ways forward to fix this incompatibility (there are other
> possibilities of course):
>
> (A) GNU grep adds a --no-ucp option that acts like 10.43 pcre2grep
> --no-ucp, and git grep -P follows suit. That is, both GNU and git grep
> act like 10.43 pcre2grep -u, in that they enable PCRE2_UTF, and also
> enable PCRE2_UCP unless --no-ucp is given. This would cause \d to match
> non-ASCII digits unless --no-ucp is given.
>
> (B) GNU grep -P and git grep -P mimic pcre2grep in both -u and --no-ucp.
> That is, they would both do 8-bit-only by default, and use PCRE2_UTF
> only when -u or --utf is given, and use PCRE2_UCP only when --no-ucp is
> absent. This would cause \d to match non-ASCII digits only when -u is
> given but --no-ucp is not.

Changing grep -P's \d to match multibyte digits by default would break an important contract. Avoiding that feels like it must outweigh any cross-tool portability concern.

(C) preserve grep -P's tradition of \d matching only 0..9, and once grep uses 10.43 or newer, \b and \w will also work as desired.

> Under either (A) or (B), future pcre2grep -u, GNU grep -P, and git grep
> -P would be consistent.

I hope git grep -P's \d will also stick to ASCII-only by default. Those rare few who desire multibyte matches can always specify \p{Nd} instead of \d, or (with new enough PCRE2), use (?-aD) and (?aD) to toggle the digit-matching mode.

Paul Eggert· Apr 5, 2023, 20:03 UTC · re: Jim Meyering · lore

Re: bug#60690: -P '\d' in GNU and git grep

On 2023-04-05 12:40, Jim Meyering wrote:
> (C)  preserve grep -P's tradition of \d matching only 0..9, and once
> grep uses 10.43 or newer, \b and \w will also work as desired.

If I understand you correctly, (C) would mean that GNU grep -P, git grep -P, and pcre2grep -u would all use PCRE2_UTF | PCRE2_UCP, and would also use the extra option PCRE2_EXTRA_ASCII_BSD that is planned for 10.43 PCRE2.

This would require changes to bleeding-edge pcre2grep -u (since it would need to add PCRE2_EXTRA_ASCII_BSD unless --no-ucp is also given), and to git grep -P (which would need to add PCRE2_UCP and PCRE2_EXTRA_ASCII_BSD, when libpcre2 is new enough to #define PCRE2_EXTRA_ASCII_BSD).

This option works for me as well. In fact it's the least work for me since I already implemented it in bleeding-edge GNU grep (so it works this way already :-).

Carlo Arenas· Apr 5, 2023, 21:20 UTC · re: Jim Meyering · lore

Re: bug#60690: -P '\d' in GNU and git grep

On Wed, Apr 5, 2023 at 12:40 PM Jim Meyering <jim@meyering.net> wrote:
>
> Changing grep -P's \d to match multibyte digits by default would break
> an important contract.

While I tend to agree[1] (and indeed that is why PCRE2_EXTRA_ASCII_BSD was invented), it would be also important to note that it goes against the Unicode recommendation[2] and it is actually not true already[3] for Python, .NET or Rust (which means ripgrep behaves like GNU grep -P 3.9).

FWIW I also agree that (at least `git grep -P`) should use PCRE2_EXTRA_ASCII_BSD by default as that is what makes more sense in the context of matching source code and using instead `\P{Nd}` if you really want all Unicode digits is not much of a burden, but I am also not sure if that makes sense in other contexts, specially considering that I am obviously biased since the languages I mostly interact with ONLY use arabic numerals and therefore `\d` meaning `[0-9]` seems "normal".

Carlo
CC: changed to the real email address for PCRE2 development, for full
context on this thread use [4]

[1] https://github.com/PCRE2Project/pcre2/pull/186 [2] https://unicode.org/reports/tr18/ [3] https://regex101.com/r/S5RW4c/1 [4] https://lore.kernel.org/git/230109.86v8lf297g.gmgdl@evledraar.gmail.com/T/

demerphq· Apr 6, 2023, 15:45 UTC · re: Paul Eggert · lore

Re: bug#60690: -P '\d' in GNU and git grep

On Wed, 5 Apr 2023 at 20:32, Paul Eggert <eggert@cs.ucla.edu> wrote:
Show 22 quoted lines
>
> On 2023-04-04 12:31, Junio C Hamano wrote:
>
> > My personal inclination is to let Perl folks decide
> > and follow them (even though I am skeptical about the wisdom of
> > letting '\d' match anything other than [0-9])
>
> I looked into what pcre2grep does. It has always done only 8-bit
> processing unless you use the -u or --utf option, so plain "pcre2grep
> '\d'" matches only ASCII digits.
>
> Although this causes pcre2grep to mishandle Unicode characters:
>
>    $ echo 'Ævar' | pcre2grep '[Ssß]'
>    Ævar
>
> it mimics Perl 5.36:
>
>    $ echo 'Ævar' | perl -ne 'print $_ if /[Ssß]/'
>    Ævar
>
> so this seems to be what Perl users expect, despite its infelicities.

Actually no, I think you have misunderstood what is happening at the different layers involved here.

Your terminal is rendering ß as a glyph. But it is almost certainly actually the octets C3 9F (which is the UTF8 canonical representation of the codepoint U+DF). So the code you provided to perl is close to the equivalent of

echo 'Ævar' | perl -ne 'print $_ if /[Ss\x{C3}\x{9F}]/'

And if you check, you will see that U+C6 "Æ" in utf8 is represented as the octets C3 86.

So what you have done is the equivalent of:
perl -le'print "\x{C3}\x{86}"' | perl -ne'print $_ if /[Ss\x{C3}\x{9F}]/'
which of course matches. \x{C3} matches \x{C3} always and everywhere.
What you should have done is something like this:

$ echo 'Ævar' | perl -ne 'utf8::decode($_); print $_ if /[Ss\x{DF}]/u' $ echo 'baß' | perl -MEncode -ne 'utf8::decode($_); print encode_utf8($_) if /[Ss\x{DF}]/u' baß $ echo 'Ævar' | perl -MEncode -ne 'utf8::decode($_); print encode_utf8($_) if /[Ss\x{C6}]/u' Ævar $ echo 'Ævar' | perl -MEncode -ne 'utf8::decode($_); print encode_utf8($_) if /[Ss\x{e6}]/ui' Ævar

The "utf8::decode($_)" tells perl to decode the input string as though it contained utf8 (which in this case it does). THe /u suffix tells the regex engine that you want Unicode semantics.

I believe that the same thing is true of your pcre2grep example. You simply aren't checking what you think you are checking. You terminal renders UTF8 as glyphs, but the programs you are feeding those glyphs to aren't seeing glyphs, they are seeing UTF8 sequences as distinct octets, and are not decoding their input back as codepoints.

You could have checked your assumptions by using the -Mre=debug option to perl:
$ echo 'Ævar' | perl -Mre=debug -ne 'print $_ if /[Ssß]/'
Compiling REx "[Ss%x{c3}%x{9f}]"
Final program:
   1: ANYOF[Ss\x9F\xC3] (11)
  11: END (0)
stclass ANYOF[Ss\x9F\xC3] minlen 1
Matching REx "[Ss%x{c3}%x{9f}]" against "%x{c3}%x{86}var%n"
Matching stclass ANYOF[Ss\x9F\xC3] against "%x{c3}%x{86}var%n" (6 bytes)
   0 <> <%x{c3}>             |   0| 1:ANYOF[Ss\x9F\xC3](11)
   1 <%x{c3}> <%x{86}var>    |   0| 11:END(0)
Match successful!
Ævar
Freeing REx: "[Ss%x{c3}%x{9f}]"
The line: Matching REx "[Ss%x{c3}%x{9f}]" against "%x{c3}%x{86}var%n"

basically says it all. Perl has not decoded the UTF8 into U+C6, and it has not decoded the UTF8 for U+DF either. Instead you have asked it if the UTF8 sequence that represents U+C6 contains any of the same octets as the UTF8 representation of U+53, U+73 and U+DF would. Which gives the common octet of \x{c3}.

> For better Unicode handling one can use pcre2grep's -u or --utf option,
> which causes pcre2grep to behave more like GNU grep -P and git grep -P:
> "echo 'Ævar' | pcre2grep -u '[Ssß]'" outputs nothing, which I think is
> what most people would expect (unless they're Perl users :-).

It is what Perl users would expect also, assuming you actually wrote the character class [Ss\x{DF}] and asked for unicode semantics. \x{DF} is the Latin1 codepoint range, so perl will assume that you meant ASCII semantics unless you tell it otherwise.

Basically these tests you have quoted here are just examples of garbage in garbage out.

Perl has been working together with the Unicode consortium for over 20 years. Afaik we were and are the reference implementation for the spec on regular expression matching in Unicode and we have a long history of working together with the Unicode consortium to refine and implement the spec. You should assume that if Perl seems to have made a gross error in how it does Unicode matching that you are simply using it wrong, we take a great deal of pride in having the best Unicode support there is.

https://unicode.org/reports/tr18/

FWIW, i think this email nicely illustrates the issues with git and regular expressions. To do regular expressions properly you need to know a) what semantics do you expect, b) how to decode the text you are matching against. If you want unicode semantics you need to have a way to ask for it. If you want to match against Unicode data then you need a way to determine which of the 6 possible encodings[1] of Unicode data you are using. If you get either wrong you will not get the results you expect. You may even want to deal with cases where you want Unicode semantics, but to match against non-unicode data. For instance Latin-1. In Latin-1 the codepoint U+DF is the *octet* 0xDF. Maybe you want that octet to match "ss" case-insensitively, as a German speaker would expect and as Unicode specifies is correct. Or vice versa, maybe you are like some of the posters to this thread who seem to expect that \d should not match U+16B51 (as a Hmong speaker might expect). Perl resolves these problems at the pattern level by supporting the suffixes /a and /u (for ascii and unicode), and at the string level it supports two type of string, unicode strings, and binary/ASCII strings. By default input is the latter but there are a variety of ways of saying that a file handle should decode to Unicode instead.

cheers, Yves [1] UTF-EBCDIC, UTF-8, UTF-16LE, UTF-16BE, UTF-32LE, UTF-32BE.

-- perl -Mre=debug -e "/just|another|perl|hacker/"

Paul Eggert· Apr 7, 2023, 16:48 UTC · re: demerphq · lore

Re: bug#60690: -P '\d' in GNU and git grep

On 2023-04-06 08:45, demerphq wrote:
Show 13 quoted lines
>> Although this causes pcre2grep to mishandle Unicode characters:
>>
>>     $ echo 'Ævar' | pcre2grep '[Ssß]'
>>     Ævar
>>
>> it mimics Perl 5.36:
>>
>>     $ echo 'Ævar' | perl -ne 'print $_ if /[Ssß]/'
>>     Ævar
>>
>> so this seems to be what Perl users expect, despite its infelicities.
> Actually no, I think you have misunderstood what is happening at the
> different layers involved here.

No, I understood what was going on. My point was that Perl users seem to have accepted this behavior, even though it does not match what people would ordinarily expect.

> What you should have done is something like this:

No, for two reasons. First, I'm no Perl expert and so I don't know (and don't particularly want to learn) its complicated Unicode options and calls. Second, /[Ss\x{DF}]/u is hard to read. If I want the S letters of traditional German, I'll write them in the obvious way, as [Ssß]. No doubt Perl will let me do this somehow - but it is telling that none of your examples do it in such a straightforward way.

Show 10 quoted lines
> $ echo 'Ævar' | perl -ne 'utf8::decode($_); print $_ if /[Ss\x{DF}]/u'
> $ echo 'baß' | perl -MEncode -ne 'utf8::decode($_); print
> encode_utf8($_) if /[Ss\x{DF}]/u'
> baß
> $ echo 'Ævar' | perl -MEncode -ne 'utf8::decode($_); print
> encode_utf8($_) if /[Ss\x{C6}]/u'
> Ævar
> $ echo 'Ævar' | perl -MEncode -ne 'utf8::decode($_); print
> encode_utf8($_) if /[Ss\x{e6}]/ui'
> Ævar
demerphq· Apr 6, 2023, 13:39 UTC · re: Junio C Hamano · lore

Re: bug#60690: -P '\d' in GNU and git grep

On Tue, 4 Apr 2023 at 21:31, Junio C Hamano <gitster@pobox.com> wrote:
Show 18 quoted lines
>
> Paul Eggert <eggert@cs.ucla.edu> writes:
>
> > This is an evolving area. Git master is fiddling with flags and
> > options, and so is GNU grep master, and so is PCRE2, and there are
> > bugs. If you're running bleeding-edge versions of this code you'll get
> > different behavior than if you're running grep 3.8, pcregrep 8.45,
> > Perl 5.36, and git 2.39.2 (which is what Fedora 37 has).
> >
> > What I'm fearing is that we may evolve into mutually incompatible
> > interpretations of how Perl regular expressions deal with UTF-8
> > text. That'd be a recipe for confusion down the road.
>
> Nicely said.  My personal inclination is to let Perl folks decide
> and follow them (even though I am skeptical about the wisdom of
> letting '\d' match anything other than [0-9]), but even in Git
> circle there would be different opinions, so I am glad that the
> discussion is visible on the list to those who are intrested.

Perl matches Unicode text according to the rules specified by the Unicode consortium. It is the reference implementation for Unicode regular expression matching. Unicode specifies that \d match any digit in any script that it supports. Thus \d matches far more codepoints than \p{PosixDigit} or [0-9] would. Be aware that Unicode contains and separates numbers and digits, eg, \x{1EC9E} represents a Lakh, which is used in many Indian languages for 100,000, but which is not considered a *digit* for obvious reasons.

FWIW, someone mentioned [[:digit:]] which matches the same as \d does on Unicode strings and under the /u matching flag for regexes in Perl. Arguably this was a mistake, [[:digit:]] is a POSIX character class, and POSIX doesn't support Unicode so it should have matched [0-9] or \p{PosixDigit}. But historically \d and [[:digit:]] in Perl were the same and when \d was extended to meet the Unicode specification [[:digit:]] came along for the ride likely inadvertently, thus \p{PosixDigit} is equivalent to [0-9], but \p{XPosixDigit} is equivalent to \d and [[:digit:]].

I notice that other posts in this thread have moved the conversation on, and covered most of the points I wanted to make here. However I wanted to say that there seem to be two different issues here. The first is "what semantics do i expect from my regular expressions", Unicode or legacy-ASCII, mostly this relates to case-insensitive matching, but things like \d also surface discrepancies. The second is "what encodings does the regular expression engine understand". Unfortunately on *nix there is no tradition of using BOM's to distinguish the 6 different possible encodings of Unicode (UTF-8, UTF-EBCDIC, UTF-16LE, UTF-16BE, UTF-32LE, UTF-32BE), and there seems to be some level of desire of matching with unicode semantics against files that are not uniformly encoded in one of these formats.

So the question comes up, A) how do you tell the regular expression engine what semantics you want and B) how does the regular expression library identify the encoding in the file, and how does it handle malformed content in that file. For instance if I have a file which contains snippets of UTF8 encoded data, *and* snippets of data that is illegal in UTF8, what should the regular expression engine do if it is asked to do a case insensitive match against that file.

cheers, yves

Paul Eggert· Apr 7, 2023, 19:00 UTC · re: demerphq · lore

Re: bug#60690: -P '\d' in GNU and git grep

On 2023-04-06 06:39, demerphq wrote:
> Unicode specifies that \d match any digit
> in any script that it supports.

"Specifies" is too strong. The Unicode Regular Expressions technical standard (UTS#18) mentions \d only in Annex C[1], next to the word "digit" in a column labeled "Property" (even though \d is really syntax not a property). This is at best an informal recommendation, not a requirement, as UTS#18 0.2[2] says that UTS#18's syntax is only for illustration and that although it's similar to Perl's, the two syntax forms may not be exactly the same. So we can't look to UTS#18 for a definitive way out of the \d mess, as the Unicode folks specifically delegated matters to us.

Even ignoring the \d issue the digit situation is messy. UTS#18 Annex C says "\p{gc=Decimal_Number}" is the standard recommended syntax assignment for digits. However, PCRE2 does not support this syntax; it supports another variant \p{Nd} that UTS#18 also recommends. So it appears that PCRE2 already does not implement every recommended aspect of UTS#18 syntax. PCRE2 also doesn't match Perl, which does support "\p{gc=Decimal_Number}".

Anyway, since grep -P '\p{Nd}' implements Unicode's decimal digit class, that's clearly enough for grep -P to conform to UTS#18 with respect to digits.

> A) how do you tell the regular expression
> engine what semantics you want and B) how does the regular expression
> library identify the encoding in the file, and how does it handle
> malformed content in that file.
Here's how GNU grep does it:
* RE semantics are specified via command-line options like -P.
* Text encoding is specified by locale, e.g., LC_ALL='en_US.utf8'.
* REs do not match encoding errors.
> on *nix there is no tradition of using BOM's to
> distinguish the 6 different possible encodings of Unicode (UTF-8,
> UTF-EBCDIC, UTF-16LE, UTF-16BE, UTF-32LE, UTF-32BE)

Yes, GNU/Linux never really experienced the joys of UTF-EBCDIC, Oracle UTFE, UTF-16LE vs UTF-16BE etc. If you're running legacy IBM mainframe or MS-Windows code these legacy encodings are obviously a big deal. However, there seems little reason to force their nontrivial hassles onto every GNU/Linux program that processes text. A few specialized apps like 'iconv' deal with offbeat encodings, and that is probably a better approach all around.

> there seems
> to be some level of desire of matching with unicode semantics against
> files that are not uniformly encoded in one of these formats.
That is a use case, yes. It's what 'strings' and 'grep' do.

[1]: https://unicode.org/reports/tr18/#Compatibility_Properties [2]: https://unicode.org/reports/tr18/#Conformance

Carlo Arenas· Apr 8, 2023, 05:01 UTC · re: Paul Eggert · lore

Re: bug#60690: -P '\d' in GNU and git grep

On Fri, Apr 7, 2023 at 12:00 PM Paul Eggert <eggert@cs.ucla.edu> wrote:
Show 23 quoted lines
>
> On 2023-04-06 06:39, demerphq wrote:
>
> > Unicode specifies that \d match any digit
> > in any script that it supports.
>
> "Specifies" is too strong. The Unicode Regular Expressions technical
> standard (UTS#18) mentions \d only in Annex C[1], next to the word
> "digit" in a column labeled "Property" (even though \d is really syntax
> not a property). This is at best an informal recommendation, not a
> requirement, as UTS#18 0.2[2] says that UTS#18's syntax is only for
> illustration and that although it's similar to Perl's, the two syntax
> forms may not be exactly the same. So we can't look to UTS#18 for a
> definitive way out of the \d mess, as the Unicode folks specifically
> delegated matters to us.
>
> Even ignoring the \d issue the digit situation is messy. UTS#18 Annex C
> says "\p{gc=Decimal_Number}" is the standard recommended syntax
> assignment for digits. However, PCRE2 does not support this syntax; it
> supports another variant \p{Nd} that UTS#18 also recommends. So it
> appears that PCRE2 already does not implement every recommended aspect
> of UTS#18 syntax. PCRE2 also doesn't match Perl, which does support
> "\p{gc=Decimal_Number}".

Not sure I follow the whole logic here, but PCRE2[3] (search for "general category" which is what the "gc" above stands for) only supports the abbreviated form of the unicode classes and `Nd` is indeed the one that corresponds to `Decimal_Number`.

Carlo

[1]: https://unicode.org/reports/tr18/#Compatibility_Properties [2]: https://unicode.org/reports/tr18/#Conformance [3]: https://pcre2project.github.io/pcre2/doc/html/pcre2pattern.html

Paul Eggert· Apr 8, 2023, 22:45 UTC · re: Carlo Arenas · lore

Re: bug#60690: -P '\d' in GNU and git grep

On 2023-04-07 22:01, Carlo Arenas wrote:
> Not sure I follow the whole logic here, but PCRE2[3] (search for
> "general category" which is what the "gc" above stands for) only
> supports the abbreviated form of the unicode classes and `Nd` is
> indeed the one that corresponds to `Decimal_Number`.

That's fine: all that UTS#18[1] requires is that PCRE2 provide syntax for a regular expression that matches the Decimal Number class. Which PCRE2 does, via \p{Nd}.

The logic is that UTF#18 does not require that \d must behave like \p{Nd}, or even that \p{gc=Decimal_Number} must behave like \p{Nd}. It merely requires that there be some syntax for matching Decimal Number, and it says the choice of syntax is up to the implementer. This is why UTF#18 doesn't require that \d must also match non-ASCII digits (which is what I think Yves was saying).

[1]: https://unicode.org/reports/tr18/

← back to recent threads