diff --git a/Unix/cloc b/Unix/cloc index 05b3d5af..4eac343b 100755 --- a/Unix/cloc +++ b/Unix/cloc @@ -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; } diff --git a/Unix/t/01_opts.t b/Unix/t/01_opts.t index d321432a..b77f51f0 100755 --- a/Unix/t/01_opts.t +++ b/Unix/t/01_opts.t @@ -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', diff --git a/cloc b/cloc index b20a6dcc..f70fff8c 100755 --- a/cloc +++ b/cloc @@ -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; } diff --git a/tests/inputs/issues/350/fs.go b/tests/inputs/issues/350/fs.go new file mode 100644 index 00000000..88de3d9e --- /dev/null +++ b/tests/inputs/issues/350/fs.go @@ -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 diff --git a/tests/outputs/issues/245/CRS.scala.yaml b/tests/outputs/issues/245/CRS.scala.yaml index 901ed4df..ebb1e8db 100644 --- a/tests/outputs/issues/245/CRS.scala.yaml +++ b/tests/outputs/issues/245/CRS.scala.yaml @@ -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 diff --git a/tests/outputs/issues/350/fs.go.yaml b/tests/outputs/issues/350/fs.go.yaml new file mode 100644 index 00000000..1169a7cd --- /dev/null +++ b/tests/outputs/issues/350/fs.go.yaml @@ -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