Skip to content

Commit 9d9babb

Browse files
pcloudsgitster
authored andcommitted
grep/pcre: prepare locale-dependent tables for icase matching
The default tables are usually built with C locale and only suitable for LANG=C or similar. This should make case insensitive search work correctly for all single-byte charsets. Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e944d9d commit 9d9babb

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

grep.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,11 +324,14 @@ static void compile_pcre_regexp(struct grep_pat *p, const struct grep_opt *opt)
324324
int erroffset;
325325
int options = PCRE_MULTILINE;
326326

327-
if (opt->ignore_case)
327+
if (opt->ignore_case) {
328+
if (has_non_ascii(p->pattern))
329+
p->pcre_tables = pcre_maketables();
328330
options |= PCRE_CASELESS;
331+
}
329332

330333
p->pcre_regexp = pcre_compile(p->pattern, options, &error, &erroffset,
331-
NULL);
334+
p->pcre_tables);
332335
if (!p->pcre_regexp)
333336
compile_regexp_failed(p, error);
334337

@@ -362,6 +365,7 @@ static void free_pcre_regexp(struct grep_pat *p)
362365
{
363366
pcre_free(p->pcre_regexp);
364367
pcre_free(p->pcre_extra_info);
368+
pcre_free((void *)p->pcre_tables);
365369
}
366370
#else /* !USE_LIBPCRE */
367371
static void compile_pcre_regexp(struct grep_pat *p, const struct grep_opt *opt)

grep.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ struct grep_pat {
4848
regex_t regexp;
4949
pcre *pcre_regexp;
5050
pcre_extra *pcre_extra_info;
51+
const unsigned char *pcre_tables;
5152
kwset_t kws;
5253
unsigned fixed:1;
5354
unsigned ignore_case:1;

t/t7813-grep-icase-iso.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/sh
2+
3+
test_description='grep icase on non-English locales'
4+
5+
. ./lib-gettext.sh
6+
7+
test_expect_success GETTEXT_ISO_LOCALE 'setup' '
8+
printf "TILRAUN: Halló Heimur!" >file &&
9+
git add file &&
10+
LC_ALL="$is_IS_iso_locale" &&
11+
export LC_ALL
12+
'
13+
14+
test_expect_success GETTEXT_ISO_LOCALE,LIBPCRE 'grep pcre string' '
15+
git grep --perl-regexp -i "TILRAUN: H.lló Heimur!" &&
16+
git grep --perl-regexp -i "TILRAUN: H.LLÓ HEIMUR!"
17+
'
18+
19+
test_done

0 commit comments

Comments
 (0)