From 03dee1f14157719abe4c16abf7dcf6333fa363c2 Mon Sep 17 00:00:00 2001 From: fluks Date: Sun, 4 Oct 2015 18:49:46 +0300 Subject: [PATCH] Fix issue #563 Count only one match per line for --count option, not all the matches. --- ack | 8 +++----- t/issue563.t | 25 +++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 t/issue563.t diff --git a/ack b/ack index 90d0ca85..852658f5 100644 --- a/ack +++ b/ack @@ -891,11 +891,9 @@ sub count_matches_in_resource { } } else { - my $content = do { - local $/; - <$fh>; - }; - $nmatches =()= ($content =~ /$opt_regex/og); + while ( <$fh> ) { + ++$nmatches if (/$opt_regex/o); + } } close $fh; } diff --git a/t/issue563.t b/t/issue563.t new file mode 100644 index 00000000..d45f6b16 --- /dev/null +++ b/t/issue563.t @@ -0,0 +1,25 @@ + +#!perl -T + +# https://github.com/petdance/ack2/issues/563 + +use strict; +use warnings; +use lib 't'; + +use Test::More tests => 4; +use Util; + +prep_environment(); + +my @result = pipe_into_ack(\'aa', '-c', 'a'); +is( $result[0], 1, 'Only one match counts per line' ); + +@result = pipe_into_ack(\'abcdgh', '-c', 'ab(c(d|e)|ef)gh'); +is( $result[0], 1, 'Nested groups with alternation in pattern match only once per line' ); + +@result = pipe_into_ack(\'abcdgh', '-c', 'abc(((d)))gh'); +is( $result[0], 1, 'Nested groups in pattern match only once per line' ); + +@result = pipe_into_ack(\'abcdgh', '-c', 'abc(d)(g)h'); +is( $result[0], 1, 'Many groups in pattern match only once per line' );