Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue #563 (Count only one match per line for --count option, not all the matches) #574

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions ack
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
25 changes: 25 additions & 0 deletions t/issue563.t
Original file line number Diff line number Diff line change
@@ -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' );