Skip to content

Commit 26d0db3

Browse files
authored
grep: fix regression for -F search (#633)
* The following search should match, but did not because of the '#' character: perl grep -F '#include' a.c * This was because quotemeta() was escaping it as \# which was then expanded between single quotes * The $matcher closure can refer to patterns list directly, which removes the need for pattern strings to be expanded with quotes * This version is easier to follow because duplicate statements are not added to the closure code if many patterns are used
1 parent ddc35c7 commit 26d0db3

File tree

1 file changed

+2
-12
lines changed

1 file changed

+2
-12
lines changed

bin/grep

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -210,22 +210,12 @@ sub parse_args {
210210
if ($opt{'w'} || $opt{'x'} || $opt{'H'} || $opt{'u'}) {
211211
die("$Me: -H, -u, -w and -x are incompatible with -F\n");
212212
}
213-
for (0 .. $#patterns) {
214-
$patterns[$_] = quotemeta $patterns[$_];
215-
}
216213
my $testop = $opt{'v'} ? '==' : '!=';
217214
if ($opt{'i'}) {
218-
for (0 .. $#patterns) {
219-
$patterns[$_] = lc $patterns[$_];
220-
}
221-
for my $pattern (@patterns) {
222-
$match_code .= "\$Matches++ if (index(lc(\$_), '$pattern') $testop -1);";
223-
}
215+
$match_code = "for my \$pat (\@patterns) { \$Matches++ if (index(lc \$_, lc \$pat) $testop -1) }";
224216
}
225217
else { # case sensitive
226-
for my $pattern (@patterns) {
227-
$match_code .= "\$Matches++ if (index(\$_, '$pattern') $testop -1);";
228-
}
218+
$match_code = "for my \$pat (\@patterns) { \$Matches++ if (index(\$_, \$pat) $testop -1) }";
229219
}
230220
$matcher = eval "sub { $match_code }";
231221
die if $@;

0 commit comments

Comments
 (0)