Skip to content

Instantly share code, notes, and snippets.

@bikong0411
Created November 2, 2016 10:20
Show Gist options
  • Save bikong0411/7eeefbc6f51da508515966fcb75813be to your computer and use it in GitHub Desktop.
Save bikong0411/7eeefbc6f51da508515966fcb75813be to your computer and use it in GitHub Desktop.
统计删除代码行数
#!/usr/bin/env perl
use feature 'say';
use Carp (carp,croak);
use IO::File;
sub getLines($$);
croak "Usage: file" unless @ARGV ==1;
my $file = shift;
my $fh = IO::File->new($file,'r');
my $total=0;
while(!$fh->eof) {
chomp(my $line = $fh->getline());
my($file,$func) = split /\s+/, $line;
my $delLine= getLines($file, $func);
#say "$file $func $delLine";
$total += $delLine;
}
say "Total Delete line: $total";
$fh->close;
sub getLines($$) {
my($file, $func) = @_;
my $f = IO::File->new($file,'r');
my $flag = 0;
my $line_no = 0;
my $branch_num = 0;
while(!$f->eof) {
chomp(my $line = $f->getline());
if ( $line =~ /\s+$func/ && $line =~ /function/) { #当前行
$flag = 1;
}
if ( $flag == 0 ) {
next;
}
#say "flag=$flag line_no=$line_no branch_num=$branch_num line=$line";
$line_no += 1;
if(my $n=()=$line =~ /\{/g) {
$branch_num+=$n;
}
if(my $n = () = $line =~ /\}/g) {
$branch_num-=$n;
if ( $flag ==1 && $branch_num == 0) {
$f->close;
return $line_no;
}
}
}
$f->close;
return $line_no;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment