[PATCH 5/7] grep: continue case insensitive fixed string search after NUL chars
- From
René Scharfe <rene.scharfe@lsrfire.ath.cx>
- Date
- May 13, 2010, 20:39 UTC
- Message-ID
- <4BEC6385.8000106@lsrfire.ath.cx>
- In-Reply-To
- <4BEC6211.2000309@lsrfire.ath.cx>
Functions for C strings, like strcasestr(), can't see beyond NUL characters. Check if there is such an obstacle on the line and try again behind it.
Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx> --- grep.c | 12 +++++++++--- t/t7008-grep-binary.sh | 4 ++++ 2 files changed, 13 insertions(+), 3 deletions(-)
diff --git a/grep.c b/grep.c index 4633b63..20a02a2 100644 --- a/grep.c +++ b/grep.c @@ -334,9 +334,15 @@ static int fixmatch(const char *pattern, char *line, char *eol, { char *hit; - if (ignore_case) - hit = strcasestr(line, pattern); - else + if (ignore_case) { + char *s = line; + do { + hit = strcasestr(s, pattern); + if (hit) + break; + s += strlen(s) + 1; + } while (s < eol); + } else hit = memmem(line, eol - line, pattern, strlen(pattern)); if (!hit) { diff --git a/t/t7008-grep-binary.sh b/t/t7008-grep-binary.sh index ad97720..1143903 100755 --- a/t/t7008-grep-binary.sh +++ b/t/t7008-grep-binary.sh @@ -37,4 +37,8 @@ test_expect_success 'git grep -F ile a' ' git grep -F ile a ' +test_expect_success 'git grep -Fi iLE a' ' + git grep -Fi iLE a +' + test_done
-- 1.7.1