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

[PATCH v7 05/12] grep/icase: avoid kwsset on literal non-ascii strings

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Feb 14, 2016, 11:49 UTC
Message-ID
<1455450596-11904-6-git-send-email-pclouds@gmail.com>
In-Reply-To
<1455450596-11904-1-git-send-email-pclouds@gmail.com>

When we detect the pattern is just a literal string, we avoid heavy regex engine and use fast substring search implemented in kwsset.c. But kws uses git-ctype which is locale-independent so it does not know how to fold case properly outside ascii range. Let regcomp or pcre take care of this case instead. Slower, but accurate.

Noticed-by: Plamen Totev <plamen.totev@abv.bg>
Helped-by: René Scharfe <l.s.r@web.de>
Helped-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 grep.c                                   |  7 ++++++-
 t/t7812-grep-icase-non-ascii.sh (new +x) | 23 +++++++++++++++++++++++
 2 files changed, 29 insertions(+), 1 deletion(-)
 create mode 100755 t/t7812-grep-icase-non-ascii.sh
diff --git a/grep.c b/grep.c
index 609f218..f192727 100644
--- a/grep.c
+++ b/grep.c
@@ -4,6 +4,7 @@
 #include "xdiff-interface.h"
 #include "diff.h"
 #include "diffcore.h"
+#include "commit.h"
 
 static int grep_source_load(struct grep_source *gs);
 static int grep_source_is_binary(struct grep_source *gs);
@@ -398,14 +399,18 @@ static int is_fixed(const char *s, size_t len)
 
 static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
 {
+	int icase, ascii_only;
 	int err;
 
 	p->word_regexp = opt->word_regexp;
 	p->ignore_case = opt->ignore_case;
+	icase	       = opt->regflags & REG_ICASE || p->ignore_case;
+	ascii_only     = !has_non_ascii(p->pattern);
 
 	if (opt->fixed) {
 		p->fixed = 1;
-	} else if (is_fixed(p->pattern, p->patternlen))
+	} else if ((!icase || ascii_only) &&
+		   is_fixed(p->pattern, p->patternlen))
 		p->fixed = 1;
 	else
 		p->fixed = 0;
diff --git a/t/t7812-grep-icase-non-ascii.sh b/t/t7812-grep-icase-non-ascii.sh
new file mode 100755
index 0000000..6eff490
--- /dev/null
+++ b/t/t7812-grep-icase-non-ascii.sh
@@ -0,0 +1,23 @@
+#!/bin/sh
+
+test_description='grep icase on non-English locales'
+
+. ./lib-gettext.sh
+
+test_expect_success GETTEXT_LOCALE 'setup' '
+	printf "TILRAUN: Halló Heimur!" >file &&
+	git add file &&
+	LC_ALL="$is_IS_locale" &&
+	export LC_ALL
+'
+
+test_have_prereq GETTEXT_LOCALE &&
+test-regex "HALLÓ" "Halló" ICASE &&
+test_set_prereq REGEX_LOCALE
+
+test_expect_success REGEX_LOCALE 'grep literal string, no -F' '
+	git grep -i "TILRAUN: Halló Heimur!" &&
+	git grep -i "TILRAUN: HALLÓ HEIMUR!"
+'
+
+test_done
-- 
2.7.0.377.g4cd97dd
Previous: Nguyễn Thái Ngọc DuyNext: Nguyễn Thái Ngọc Duy
Message 42 of 56 in “Fix icase grep on non-ascii”
  1. 00/10 Fix icase grep on non-asciiNguyễn Thái Ngọc Duy, Jan 28, 2016
  2. 01/10 grep: allow -F -i combinationNguyễn Thái Ngọc Duy, Jan 28, 2016
  3. 02/10 grep: break down an "if" stmt in preparation for next changesNguyễn Thái Ngọc Duy, Jan 28, 2016
  4. 03/10 test-regex: expose full regcomp() to the command lineNguyễn Thái Ngọc Duy, Jan 28, 2016
  5. Eric SunshineJan 29, 2016
  6. Ramsay JonesJan 29, 2016
  7. 04/10 grep/icase: avoid kwsset on literal non-ascii stringsNguyễn Thái Ngọc Duy, Jan 28, 2016
  8. Eric SunshineJan 29, 2016
  9. Eric SunshineJan 29, 2016
  10. 05/10 grep/icase: avoid kwsset when -F is specifiedNguyễn Thái Ngọc Duy, Jan 28, 2016
  11. Eric SunshineJan 29, 2016
  12. 06/10 grep/pcre: prepare locale-dependent tables for icase matchingNguyễn Thái Ngọc Duy, Jan 28, 2016
  13. 07/10 gettext: add is_utf8_locale()Nguyễn Thái Ngọc Duy, Jan 28, 2016
  14. 08/10 grep/pcre: support utf-8Nguyễn Thái Ngọc Duy, Jan 28, 2016
  15. 09/10 diffcore-pickaxe: "share" regex error handling codeNguyễn Thái Ngọc Duy, Jan 28, 2016
  16. 10/10 diffcore-pickaxe: support case insensitive match on non-asciiNguyễn Thái Ngọc Duy, Jan 28, 2016
  17. Eric SunshineJan 29, 2016
  18. Junio C HamanoJan 28, 2016
  19. 00/11 Fix icase grep on non-asciiNguyễn Thái Ngọc Duy, Feb 6, 2016
  20. 01/11 grep: allow -F -i combinationNguyễn Thái Ngọc Duy, Feb 6, 2016
  21. Junio C HamanoJun 17, 2016
  22. Duy NguyenJun 18, 2016
  23. 02/11 grep: break down an "if" stmt in preparation for next changesNguyễn Thái Ngọc Duy, Feb 6, 2016
  24. Junio C HamanoFeb 9, 2016
  25. 03/11 test-regex: isolate the bug test codeNguyễn Thái Ngọc Duy, Feb 6, 2016
  26. 04/11 test-regex: expose full regcomp() to the command lineNguyễn Thái Ngọc Duy, Feb 6, 2016
  27. Eric SunshineFeb 7, 2016
  28. Junio C HamanoFeb 9, 2016
  29. 05/11 grep/icase: avoid kwsset on literal non-ascii stringsNguyễn Thái Ngọc Duy, Feb 6, 2016
  30. 06/11 grep/icase: avoid kwsset when -F is specifiedNguyễn Thái Ngọc Duy, Feb 6, 2016
  31. 07/11 grep/pcre: prepare locale-dependent tables for icase matchingNguyễn Thái Ngọc Duy, Feb 6, 2016
  32. 08/11 gettext: add is_utf8_locale()Nguyễn Thái Ngọc Duy, Feb 6, 2016
  33. 09/11 grep/pcre: support utf-8Nguyễn Thái Ngọc Duy, Feb 6, 2016
  34. 10/11 diffcore-pickaxe: "share" regex error handling codeNguyễn Thái Ngọc Duy, Feb 6, 2016
  35. 11/11 diffcore-pickaxe: support case insensitive match on non-asciiNguyễn Thái Ngọc Duy, Feb 6, 2016
  36. Eric SunshineFeb 7, 2016
  37. 00/12 nd/icase updatesNguyễn Thái Ngọc Duy, Feb 14, 2016
  38. 01/12 grep: allow -F -i combinationNguyễn Thái Ngọc Duy, Feb 14, 2016
  39. 02/12 grep: break down an "if" stmt in preparation for next changesNguyễn Thái Ngọc Duy, Feb 14, 2016
  40. 03/12 test-regex: isolate the bug test codeNguyễn Thái Ngọc Duy, Feb 14, 2016
  41. 04/12 test-regex: expose full regcomp() to the command lineNguyễn Thái Ngọc Duy, Feb 14, 2016
  42. 05/12 grep/icase: avoid kwsset on literal non-ascii stringsNguyễn Thái Ngọc Duy, Feb 14, 2016
  43. 06/12 grep/icase: avoid kwsset when -F is specifiedNguyễn Thái Ngọc Duy, Feb 14, 2016
  44. 07/12 grep/pcre: prepare locale-dependent tables for icase matchingNguyễn Thái Ngọc Duy, Feb 14, 2016
  45. 08/12 gettext: add is_utf8_locale()Nguyễn Thái Ngọc Duy, Feb 14, 2016
  46. 09/12 grep/pcre: support utf-8Nguyễn Thái Ngọc Duy, Feb 14, 2016
  47. 10/12 diffcore-pickaxe: "share" regex error handling codeNguyễn Thái Ngọc Duy, Feb 14, 2016
  48. 11/12 diffcore-pickaxe: support case insensitive match on non-asciiNguyễn Thái Ngọc Duy, Feb 14, 2016
  49. 12/12 grep.c: reuse "icase" variableNguyễn Thái Ngọc Duy, Feb 14, 2016
  50. Junio C HamanoJun 17, 2016
  51. Duy NguyenJun 18, 2016
  52. Duy NguyenJun 22, 2016
  53. Junio C HamanoJun 22, 2016
  54. Duy NguyenJun 22, 2016
  55. Junio C HamanoJun 22, 2016
  56. Junio C HamanoJun 22, 2016

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.