Skip to content

Commit

Permalink
Merge pull request #436 from stsnel/fix-strip-str-comments
Browse files Browse the repository at this point in the history
Fix strip str comments
  • Loading branch information
AlDanial committed Nov 30, 2019
2 parents 93f2389 + ed27441 commit f70c94c
Showing 1 changed file with 42 additions and 29 deletions.
71 changes: 42 additions & 29 deletions cloc
Expand Up @@ -6199,53 +6199,66 @@ sub rm_comments_in_strings { # {{{1
foreach my $line (@{$ra_lines}) {
#print "line=[$line]\n";
my $new_line = "";

if ($line !~ /${string_marker}/) {
# short circuit; no strings on this line
if ( $in_ml_string ) {
$line =~ s/\Q${start_comment}\E/xx/g;
if ( $in_ml_string ) {
$line =~ s/\Q${start_comment}\E/xx/g;
$line =~ s/\Q${end_comment}\E/xx/g if $end_comment;
}
}
push @save_lines, $line;
next;
}

# replace backslashed string markers with 'Q'
$line =~ s/\\${string_marker}/Q/g;
if ( $in_ml_string and $line =~ /^(.*?)(${string_marker})(.*)$/ ) {
# A multiline string ends on this line. Process the part
# until the end of the multiline string first.

if ( $in_ml_string and $line =~ /^(.*?)(${string_marker})(.*)$/ ) {
# A multiline string ends on this line. Process the part
# until the end of the multiline string first.
my ($lastpart_ml_string, $firstpart_marker, $rest_of_line ) = ($1, $2, $3);
$lastpart_ml_string =~ s/\Q${start_comment}\E/xx/g;
$lastpart_ml_string =~ s/\Q${end_comment}\E/xx/g if $end_comment;
$new_line = $lastpart_ml_string . $firstpart_marker;
$line = $rest_of_line;
$in_ml_string = 0;
}
$lastpart_ml_string =~ s/\Q${start_comment}\E/xx/g;
$lastpart_ml_string =~ s/\Q${end_comment}\E/xx/g if $end_comment;
$new_line = $lastpart_ml_string . $firstpart_marker;
$line = $rest_of_line;
$in_ml_string = 0;
}

my @tokens = split(/(${string_marker}.*?${string_marker})/, $line);
foreach my $t (@tokens) {
#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;
}
elsif ( $multiline_mode and $t =~ /(${string_marker})/ ) {
# Unclosed quote present in line. If multiline_mode is enabled,
# consider it the start of a multiline string.
my $firstpart_marker = $1;
}
elsif ( $multiline_mode and $t =~ /(${string_marker})/ ) {
# Unclosed quote present in line. If multiline_mode is enabled,
# consider it the start of a multiline string.
my $firstpart_marker = $1;
my @sub_token = split(/${string_marker}/, $t );
if ( scalar @sub_token == 2 ) {
$t = $sub_token[0] . $firstpart_marker;
$sub_token[1] =~ s/\Q${start_comment}\E/xx/g;
$sub_token[1] =~ s/\Q${end_comment}\E/xx/g if $end_comment;
$t .= $sub_token[1];
$in_ml_string = 1;
} else {
print "Warning: rm_comments_in_string length \@sub_token > 2\n";
}

}
#printf " t1 = [$t]\n";
$new_line .= $t;
if ( scalar @sub_token == 1 ) {
# The line ends with a string marker that starts
# a multiline string.
$t = $sub_token[0] . $firstpart_marker;
$in_ml_string = 1;
}
elsif ( scalar @sub_token == 2 ) {
# The line has some more content after the string
# marker that starts a multiline string
$t = $sub_token[0] . $firstpart_marker;
$sub_token[1] =~ s/\Q${start_comment}\E/xx/g;
$sub_token[1] =~ s/\Q${end_comment}\E/xx/g if $end_comment;
$t .= $sub_token[1];
$in_ml_string = 1;
} else {
print "Warning: rm_comments_in_string length \@sub_token > 2\n";
}

}
#printf " t1 = [$t]\n";
$new_line .= $t;
}
push @save_lines, $new_line;
}
Expand Down

0 comments on commit f70c94c

Please sign in to comment.