From 3d7c289a0c358da4413e66d0624790e0409c3833 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Mon, 27 Jul 2026 02:38:50 -0500 Subject: [PATCH] Fix grep fastpath: ^. wasn't falling back to regex. (Fast path does a bucket sort on first char, so it can't be a wildcard.) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reported by Jörgen Cederlöf --- tests/grep.test | 1 + toys/posix/grep.c | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/grep.test b/tests/grep.test index 7d0aac97..be2414aa 100755 --- a/tests/grep.test +++ b/tests/grep.test @@ -108,6 +108,7 @@ testcmd "-w '$'" "-w '\$'" '' '' 'abc abc\n' testcmd "-w '$' 2" "-w '\$'" 'abc \n' '' 'abc \n' testcmd "'^' is ''" "'^'" 'potato\n' '' 'potato\n' testcmd "'^' is '' 2" "'^x*'" 'potato\n' '' 'potato\n' +testcmd "'^.' fastpath" "'^.'" 'hello\n' '' 'hello\n' testcmd "-w '^'" "-w '^'" '' '' 'abc abc\n' testcmd "-w '^' 2" "-w '^'" ' abc\n' '' ' abc\n' testcmd "-w \\1" "-wo '\\(x\\)\\1'" "xx\n" "" "xx" diff --git a/toys/posix/grep.c b/toys/posix/grep.c index eeaff601..d1ccbaed 100644 --- a/toys/posix/grep.c +++ b/toys/posix/grep.c @@ -100,6 +100,7 @@ static void outline(char *line, char dash, char *name, long lcount, long bcount, } } +// -w whole word match static int matchw(char *line, char *start, long so, long eo) { if (FLAG(w)) { @@ -403,7 +404,7 @@ static void parse_regex(void) for (last = &TT.e; *last;) { // Can we use the fast path? s = (*last)->arg; - if ('.'!=*s && !FLAG(F) && strcmp(s, "^$")) for (; *s; s++) { + if ('.'!=s[*s=='^'] && !FLAG(F) && strcmp(s, "^$")) for (; *s; s++) { if (*s=='\\') { if (!s[1] || !strchr(special, *++s)) break; if (!FLAG(E) && *s=='(') break; -- 2.39.5