Skip to content

Commit

Permalink
new option --skip-leading, #530
Browse files Browse the repository at this point in the history
  • Loading branch information
AlDanial committed Nov 16, 2020
1 parent db5482d commit 02aae74
Show file tree
Hide file tree
Showing 9 changed files with 210 additions and 6 deletions.
41 changes: 38 additions & 3 deletions Unix/cloc
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,13 @@ Usage: $script [options] <file(s)/dir(s)/git hash(es)> | <set 1> <set 2> | <repo
letting File::Temp chose the location. Files
written to this location are not removed at
the end of the run (as they are with File::Temp).
--skip-leading=<N[,ext]> Skip the first <N> lines of each file. If a
comma separated list of extensions is also given,
only skip lines from those file types. Example:
--skip-leading=10,cpp,h
will skip the first ten lines of *.cpp and *.h
files. This is useful for ignoring boilerplate
text.
--skip-uniqueness Skip the file uniqueness check. This will give
a performance boost at the expense of counting
files with identical contents multiple times
Expand Down Expand Up @@ -737,6 +744,7 @@ my (
$opt_docstring_as_code ,
$opt_stat ,
$opt_summary_cutoff ,
$opt_skip_leading ,
);

my $getopt_success = GetOptions( # {{{1
Expand Down Expand Up @@ -835,6 +843,7 @@ my $getopt_success = GetOptions( # {{{1
"docstring_as_code|docstring-as-code" => \$opt_docstring_as_code ,
"stat" => \$opt_stat ,
"summary_cutoff|summary-cutoff=s" => \$opt_summary_cutoff ,
"skip_leading|skip-leading:s" => \$opt_skip_leading ,
);
# 1}}}
$config_file = $opt_config_file if defined $opt_config_file;
Expand Down Expand Up @@ -5910,11 +5919,12 @@ sub call_counter { # {{{1
) = @_;

# Logic: pass the file through the following filters:
# 1. remove blank lines
# 2. remove comments using each filter defined for this language
# 1. remove leading lines (if --skip-leading)
# 2. remove blank lines
# 3. remove comments using each filter defined for this language
# (example: SQL has two, remove_starts_with(--) and
# remove_c_comments() )
# 3. compute comment lines as
# 4. compute comment lines as
# total lines - blank lines - lines left over after all
# comment filters have been applied

Expand Down Expand Up @@ -5944,6 +5954,17 @@ sub call_counter { # {{{1

# implement --perl-ignore-data here

if ($opt_skip_leading) {
my $strip = 1;
my ($N, @exts) = split(/,/, $opt_skip_leading);
if (@exts) {
# only apply if this file's extension is listed
my $this_file_ext = file_extension($file);
$strip = grep(/^${this_file_ext}$/, @exts);
}
@lines = remove_first_n($N, \@lines) if $strip;
}

my @original_lines = @lines;
my $total_lines = scalar @lines;

Expand Down Expand Up @@ -6367,6 +6388,20 @@ sub rm_comments { # {{{1
print "<- rm_comments\n" if $opt_v > 2;
return @lines;
} # 1}}}
sub remove_first_n { # {{{1
my ($n, $ra_lines, ) = @_;
print "-> remove_first_n\n" if $opt_v > 2;

my @save_lines = ();
if (scalar @{$ra_lines} > $n) {
for (my $i = $n; $i < scalar @{$ra_lines}; $i++) {
push @save_lines, $ra_lines->[$i];
}
}

print "<- remove_first_n\n" if $opt_v > 2;
return @save_lines;
} # 1}}}
sub remove_f77_comments { # {{{1
my ($ra_lines, ) = @_;
print "-> remove_f77_comments\n" if $opt_v > 2;
Expand Down
28 changes: 28 additions & 0 deletions Unix/t/01_opts.t
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,34 @@ my @Tests = (
'ref' => '../tests/outputs/issues/528/cutoff_code_50pct.yaml',
},

{
'name' => '--skip-leading 2 #530 1/4',
'cd' => '../tests/inputs/issues/530',
'args' => '--skip-leading 2 .',
'ref' => '../tests/outputs/issues/530/case_1.yaml',
},

{
'name' => '--skip-leading 100 #530 2/4',
'cd' => '../tests/inputs/issues/530',
'args' => '--skip-leading 100 .',
'ref' => '../tests/outputs/issues/530/case_2.yaml',
},

{
'name' => '--skip-leading 2,c,h #530 3/4',
'cd' => '../tests/inputs/issues/530',
'args' => '--skip-leading 2,c,h .',
'ref' => '../tests/outputs/issues/530/case_3.yaml',
},

{
'name' => '--skip-leading 2,C,H #530 4/4',
'cd' => '../tests/inputs/issues/530',
'args' => '--skip-leading 2,C,H .',
'ref' => '../tests/outputs/issues/530/case_4.yaml',
},

);

# Create test input for issue #132 which needs data not in the git repo.
Expand Down
41 changes: 38 additions & 3 deletions cloc
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,13 @@ Usage: $script [options] <file(s)/dir(s)/git hash(es)> | <set 1> <set 2> | <repo
letting File::Temp chose the location. Files
written to this location are not removed at
the end of the run (as they are with File::Temp).
--skip-leading=<N[,ext]> Skip the first <N> lines of each file. If a
comma separated list of extensions is also given,
only skip lines from those file types. Example:
--skip-leading=10,cpp,h
will skip the first ten lines of *.cpp and *.h
files. This is useful for ignoring boilerplate
text.
--skip-uniqueness Skip the file uniqueness check. This will give
a performance boost at the expense of counting
files with identical contents multiple times
Expand Down Expand Up @@ -737,6 +744,7 @@ my (
$opt_docstring_as_code ,
$opt_stat ,
$opt_summary_cutoff ,
$opt_skip_leading ,
);

my $getopt_success = GetOptions( # {{{1
Expand Down Expand Up @@ -835,6 +843,7 @@ my $getopt_success = GetOptions( # {{{1
"docstring_as_code|docstring-as-code" => \$opt_docstring_as_code ,
"stat" => \$opt_stat ,
"summary_cutoff|summary-cutoff=s" => \$opt_summary_cutoff ,
"skip_leading|skip-leading:s" => \$opt_skip_leading ,
);
# 1}}}
$config_file = $opt_config_file if defined $opt_config_file;
Expand Down Expand Up @@ -5900,11 +5909,12 @@ sub call_counter { # {{{1
) = @_;

# Logic: pass the file through the following filters:
# 1. remove blank lines
# 2. remove comments using each filter defined for this language
# 1. remove leading lines (if --skip-leading)
# 2. remove blank lines
# 3. remove comments using each filter defined for this language
# (example: SQL has two, remove_starts_with(--) and
# remove_c_comments() )
# 3. compute comment lines as
# 4. compute comment lines as
# total lines - blank lines - lines left over after all
# comment filters have been applied

Expand Down Expand Up @@ -5934,6 +5944,17 @@ sub call_counter { # {{{1

# implement --perl-ignore-data here

if ($opt_skip_leading) {
my $strip = 1;
my ($N, @exts) = split(/,/, $opt_skip_leading);
if (@exts) {
# only apply if this file's extension is listed
my $this_file_ext = file_extension($file);
$strip = grep(/^${this_file_ext}$/, @exts);
}
@lines = remove_first_n($N, \@lines) if $strip;
}

my @original_lines = @lines;
my $total_lines = scalar @lines;

Expand Down Expand Up @@ -6357,6 +6378,20 @@ sub rm_comments { # {{{1
print "<- rm_comments\n" if $opt_v > 2;
return @lines;
} # 1}}}
sub remove_first_n { # {{{1
my ($n, $ra_lines, ) = @_;
print "-> remove_first_n\n" if $opt_v > 2;

my @save_lines = ();
if (scalar @{$ra_lines} > $n) {
for (my $i = $n; $i < scalar @{$ra_lines}; $i++) {
push @save_lines, $ra_lines->[$i];
}
}

print "<- remove_first_n\n" if $opt_v > 2;
return @save_lines;
} # 1}}}
sub remove_f77_comments { # {{{1
my ($ra_lines, ) = @_;
print "-> remove_f77_comments\n" if $opt_v > 2;
Expand Down
11 changes: 11 additions & 0 deletions tests/inputs/issues/530/C-Ansi.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* Hello World in C, Ansi-style */
/* from http://www.roesler-ac.de/wolfram/hello.htm */

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
puts("Hello World!");
return EXIT_SUCCESS;
}
21 changes: 21 additions & 0 deletions tests/outputs/issues/530/case_1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
# github.com/AlDanial/cloc
header :
cloc_url : github.com/AlDanial/cloc
cloc_version : 1.89
elapsed_seconds : 0.00850200653076172
n_files : 1
n_lines : 9
files_per_second : 117.619293325855
lines_per_second : 1058.5736399327
report_file : case_1.yaml
'C' :
nFiles: 1
blank: 2
comment: 0
code: 7
SUM:
blank: 2
comment: 0
code: 7
nFiles: 1
21 changes: 21 additions & 0 deletions tests/outputs/issues/530/case_2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
# github.com/AlDanial/cloc
header :
cloc_url : github.com/AlDanial/cloc
cloc_version : 1.89
elapsed_seconds : 0.00734210014343262
n_files : 1
n_lines : 0
files_per_second : 136.200811820101
lines_per_second : 0
report_file : case_2.yaml
'C' :
nFiles: 1
blank: 0
comment: 0
code: 0
SUM:
blank: 0
comment: 0
code: 0
nFiles: 1
21 changes: 21 additions & 0 deletions tests/outputs/issues/530/case_3.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
# github.com/AlDanial/cloc
header :
cloc_url : github.com/AlDanial/cloc
cloc_version : 1.89
elapsed_seconds : 0.00851607322692871
n_files : 1
n_lines : 9
files_per_second : 117.425011898429
lines_per_second : 1056.82510708586
report_file : case_3.yaml
'C' :
nFiles: 1
blank: 2
comment: 0
code: 7
SUM:
blank: 2
comment: 0
code: 7
nFiles: 1
21 changes: 21 additions & 0 deletions tests/outputs/issues/530/case_4.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
# github.com/AlDanial/cloc
header :
cloc_url : github.com/AlDanial/cloc
cloc_version : 1.89
elapsed_seconds : 0.00853896141052246
n_files : 1
n_lines : 11
files_per_second : 117.1102610638
lines_per_second : 1288.2128717018
report_file : case_4.yaml
'C' :
nFiles: 1
blank: 2
comment: 2
code: 7
SUM:
blank: 2
comment: 2
code: 7
nFiles: 1
11 changes: 11 additions & 0 deletions tests/outputs/issues/530/tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# nominal; skip first two
cloc --yaml --out case_1.yaml --skip-leading 2 ../../../inputs/issues/530

# more lines than in the file; null return
cloc --yaml --out case_2.yaml --skip-leading 100 ../../../inputs/issues/530

# in extension list; skip first two
cloc --yaml --out case_3.yaml --skip-leading 2,c,h ../../../inputs/issues/530

# not in extension list; skip nothing
cloc --yaml --out case_4.yaml --skip-leading 2,C,H ../../../inputs/issues/530

0 comments on commit 02aae74

Please sign in to comment.