Skip to content

Commit

Permalink
fix logic error in --strip-str-comments handling, #350
Browse files Browse the repository at this point in the history
  • Loading branch information
AlDanial committed Dec 2, 2018
1 parent cf96f71 commit 89a803d
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 22 deletions.
20 changes: 14 additions & 6 deletions Unix/cloc
Original file line number Diff line number Diff line change
Expand Up @@ -5981,15 +5981,23 @@ sub rm_comments_in_strings { # {{{1
if $opt_v > 2;

my @save_lines = ();
foreach (@{$ra_lines}) {

my ($line, $string_marker, $start_comment, $end_comment) = @_;
foreach my $line (@{$ra_lines}) {
#print "line=[$line]\n";
if ($line !~ /${string_marker}/) {
# short circuit; no strings on this line
push @save_lines, $line;
next;
}
my @tokens = split(/(${string_marker}.*?${string_marker})/, $line);
my $new_line = "";
foreach my $t (@tokens) {
# printf " t = [$t]\n";
$t =~ s/\Q${start_comment}\E/xx/g;
$t =~ s/\Q${end_comment}\E/xx/g if $end_comment;
#printf " t0 = [$t]\n";
if ($t =~ /${string_marker}.*${string_marker}$/) {
# enclosed in quotes; process this token
$t =~ s/\Q${start_comment}\E/xx/g;
$t =~ s/\Q${end_comment}\E/xx/g if $end_comment;
}
#printf " t1 = [$t]\n";
$new_line .= $t;
}

Expand Down
6 changes: 6 additions & 0 deletions Unix/t/01_opts.t
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,12 @@ my @Tests = (
'ref' => '../tests/outputs/issues/341/results.yaml',
},

{
'name' => '--strip-str-comments (github issue #350)',
'cd' => '../tests/inputs/issues/350',
'args' => '--strip-str-comments .',
'ref' => '../tests/outputs/issues/350/fs.go.yaml',
},

# {
# 'name' => '--count-and--diff with --out',
Expand Down
20 changes: 14 additions & 6 deletions cloc
Original file line number Diff line number Diff line change
Expand Up @@ -5971,15 +5971,23 @@ sub rm_comments_in_strings { # {{{1
if $opt_v > 2;

my @save_lines = ();
foreach (@{$ra_lines}) {

my ($line, $string_marker, $start_comment, $end_comment) = @_;
foreach my $line (@{$ra_lines}) {
#print "line=[$line]\n";
if ($line !~ /${string_marker}/) {
# short circuit; no strings on this line
push @save_lines, $line;
next;
}
my @tokens = split(/(${string_marker}.*?${string_marker})/, $line);
my $new_line = "";
foreach my $t (@tokens) {
# printf " t = [$t]\n";
$t =~ s/\Q${start_comment}\E/xx/g;
$t =~ s/\Q${end_comment}\E/xx/g if $end_comment;
#printf " t0 = [$t]\n";
if ($t =~ /${string_marker}.*${string_marker}$/) {
# enclosed in quotes; process this token
$t =~ s/\Q${start_comment}\E/xx/g;
$t =~ s/\Q${end_comment}\E/xx/g if $end_comment;
}
#printf " t1 = [$t]\n";
$new_line .= $t;
}

Expand Down
26 changes: 26 additions & 0 deletions tests/inputs/issues/350/fs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package fs

func List() []string {
afero.Glob("/*")
}

func (fs aferoFs) AtomicCreateWith(fname string, data []byte) {
tmpFile := Sprintf("%s.tmp", fname)
}

// this is a comment
// this is a "comment"
// "this is a comment"
"// this is a comment"

func deleteTmpFiles(fs afero.Fs) {
if err != nil {
panic(err)
}
for _, n := range tmpFiles {
fs.Remove()
if err != nil {
}
}
}
// https://github.com/AlDanial/cloc/issues/350
20 changes: 10 additions & 10 deletions tests/outputs/issues/245/CRS.scala.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
# github.com/AlDanial/cloc
header :
cloc_url : github.com/AlDanial/cloc
cloc_version : 1.77
elapsed_seconds : 0.00808191299438477
cloc_version : 1.81
elapsed_seconds : 0.00722098350524902
n_files : 1
n_lines : 41
files_per_second : 123.733081597734
lines_per_second : 5073.05634550711
report_file : CRS.scala.yaml
Scala :
files_per_second : 138.48529071879
lines_per_second : 5677.8969194704
report_file : ../tests/outputs/issues/245/CRS.scala.yaml
'Scala' :
nFiles: 1
blank: 8
comment: 0
code: 33
comment: 8
code: 25
SUM:
blank: 8
comment: 0
code: 33
comment: 8
code: 25
nFiles: 1
21 changes: 21 additions & 0 deletions tests/outputs/issues/350/fs.go.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.81
elapsed_seconds : 0.00793290138244629
n_files : 1
n_lines : 26
files_per_second : 126.057283683467
lines_per_second : 3277.48937577014
report_file : ../../../outputs/issues/350/fs.go.yaml
'Go' :
nFiles: 1
blank: 4
comment: 4
code: 18
SUM:
blank: 4
comment: 4
code: 18
nFiles: 1

0 comments on commit 89a803d

Please sign in to comment.