Skip to content

Instantly share code, notes, and snippets.

@funasoul
Created December 28, 2012 06:38
Show Gist options
  • Save funasoul/4395089 to your computer and use it in GitHub Desktop.
Save funasoul/4395089 to your computer and use it in GitHub Desktop.
Print diff between function a() and b() in foo.c file.
#!/usr/bin/env perl
#
# Last modified: Fri, 28 Dec 2012 15:37:46 +0900
#
# Usage: perl diff.pl foo.c func1 func2
# will compare func1() and func2() in foo.c
#
# Note: The function must finish with "}" at the beggining of line, like:
# int func(int a) {
# ...
# } <== Here!
use strict;
use warnings;
use Text::Diff 'diff';
my $f1 = 0;
my $f2 = 0;
my $str1 = "";
my $str2 = "";
my $file = $ARGV[0];
my $name1 = $ARGV[1];
my $name2;
if (@ARGV < 3) {
$name2 = $name1 . "f";
} else {
$name2 = $ARGV[2];
}
open(IN, "<$file") || die;
while(<IN>) {
if (/^\s\t*$/ || /^\s*\/\*/ || /^\s*\/\//) {
next;
}
if (/$name1\s*\(.*{/) {
$f1 = 1;
}
if (/$name2\s*\(.*{/) {
$f2 = 1;
}
if ($f1) {
$str1 .= $_;
if (/^}/) {
$f1 = 0;
}
}
if ($f2) {
$str2 .= $_;
if (/^}/) {
$f2 = 0;
}
}
}
my $diff = diff \$str1, \$str2;
print $diff;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment