git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2] grep: correctly identify utf-8 characters with \{b,w} in -P

From
Carlo Marcelo Arenas Belón <carenas@gmail.com>
Date
Jan 8, 2023, 15:52 UTC
Message-ID
<20230108155217.2817-1-carenas@gmail.com>
In-Reply-To
<20230108062335.72114-1-carenas@gmail.com>

When UTF is enabled for a PCRE match, the corresponding flags are added to the pcre2_compile() call, but PCRE2_UCP wasn't included.

This prevents extending the meaning of the character classes to include those new valid characters and therefore result in failed matches for expressions that rely on that extention, for ex:

  $ git grep -P '\bÆvar'

Add PCRE2_UCP so that \w will include Æ and therefore \b could correctly match the beginning of that word.

This has an impact on performance that has been estimated to be between 20% to 40% and that is shown through the added performance test.

Signed-off-by: Carlo Marcelo Arenas Belón <carenas@gmail.com>
---
 grep.c                              |  2 +-
 t/perf/p7822-grep-perl-character.sh | 42 +++++++++++++++++++++++++++++
 2 files changed, 43 insertions(+), 1 deletion(-)
 create mode 100755 t/perf/p7822-grep-perl-character.sh
diff --git a/grep.c b/grep.c
index 06eed69493..1687f65b64 100644
--- a/grep.c
+++ b/grep.c
@@ -293,7 +293,7 @@ static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt
 		options |= PCRE2_CASELESS;
 	}
 	if (!opt->ignore_locale && is_utf8_locale() && !literal)
-		options |= (PCRE2_UTF | PCRE2_MATCH_INVALID_UTF);
+		options |= (PCRE2_UTF | PCRE2_UCP | PCRE2_MATCH_INVALID_UTF);
 
 #ifndef GIT_PCRE2_VERSION_10_36_OR_HIGHER
 	/* Work around https://bugs.exim.org/show_bug.cgi?id=2642 fixed in 10.36 */
diff --git a/t/perf/p7822-grep-perl-character.sh b/t/perf/p7822-grep-perl-character.sh
new file mode 100755
index 0000000000..87009c60df
--- /dev/null
+++ b/t/perf/p7822-grep-perl-character.sh
@@ -0,0 +1,42 @@
+#!/bin/sh
+
+test_description="git-grep's perl regex
+
+If GIT_PERF_GREP_THREADS is set to a list of threads (e.g. '1 4 8'
+etc.) we will test the patterns under those numbers of threads.
+"
+
+. ./perf-lib.sh
+
+test_perf_large_repo
+test_checkout_worktree
+
+if test -n "$GIT_PERF_GREP_THREADS"
+then
+	test_set_prereq PERF_GREP_ENGINES_THREADS
+fi
+
+for pattern in \
+	'\\bhow' \
+	'\\bÆvar' \
+	'\\d+ \\bÆvar' \
+	'\\bBelón\\b' \
+	'\\w{12}\\b'
+do
+	echo '$pattern' >pat
+	if ! test_have_prereq PERF_GREP_ENGINES_THREADS
+	then
+		test_perf "grep -P '$pattern'" --prereq PCRE "
+			git -P grep -f pat || :
+		"
+	else
+		for threads in $GIT_PERF_GREP_THREADS
+		do
+			test_perf "grep -P '$pattern' with $threads threads" --prereq PTHREADS,PCRE "
+				git -c grep.threads=$threads -P grep -f pat || :
+			"
+		done
+	fi
+done
+
+test_done
-- 
2.39.0.199.g555ddd67e6
Previous: Junio C HamanoNext: Ævar Arnfjörð Bjarmason
Message 3 of 17 in “grep: correctly identify utf-8 characters with \{b,w} in -P”
  1. grep: correctly identify utf-8 characters with \{b,w} in -PCarlo Marcelo Arenas Belón, Jan 8, 2023
  2. Junio C HamanoJan 8, 2023
  3. grep: correctly identify utf-8 characters with \{b,w} in -PCarlo Marcelo Arenas Belón, Jan 8, 2023
  4. Ævar Arnfjörð BjarmasonJan 9, 2023
  5. Paul EggertJan 9, 2023
  6. Ævar Arnfjörð BjarmasonJan 9, 2023
  7. Paul EggertJan 9, 2023
  8. Carlo ArenasJan 10, 2023
  9. Junio C HamanoJan 16, 2023
  10. grep: correctly identify utf-8 characters with \{b,w} in -PCarlo Marcelo Arenas Belón, Jan 17, 2023
  11. Ævar Arnfjörð BjarmasonJan 17, 2023
  12. Junio C HamanoJan 17, 2023
  13. Carlo ArenasJan 18, 2023
  14. Ævar Arnfjörð BjarmasonJan 18, 2023
  15. Junio C HamanoJan 18, 2023
  16. Ævar Arnfjörð BjarmasonJan 18, 2023
  17. Junio C HamanoJan 18, 2023

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.