[Gllug] diff of two functions

ccooke ccookei-gllug at gkhs.net
Wed Sep 14 15:22:06 UTC 2005


On Tue, Sep 13, 2005 at 10:41:20PM +0100, Martin N Stevens wrote:
> Hi,
> 
> I've got a question on using diff
> 
> I've got two functions in a file and I want to compare them, is diff
> the best way to do this and how, or is there some funky function in
> vim ?
> 
> eg.
> 
> function1(){
>  do this
> }
> 
> function2(){
>  do that
> }
> 
> What are the differences ? can diff do diff on the same file but
> between two groups of line numbers ?


Try this little bit of shell:

(usage: $0 file1:function1 file2:function2 $diffoptions
if either file2 or function2 are blank, they default to file1 and
function1 respectively. It's currently configured to work with shell
functions, since you seemed to be asking about them. Also, it *will*
break if you are doing cute formatting tricks. However, for the general
case, this will do what you asked)

#!/bin/bash

FUNCSTART="1,/^[[:space:]]*\(function %%\|%%()\).*$/d"
FUNCEND="/^}[[:space:]]*$/,\$d"

file1="${1%:*}"
files="${2%:*}"

func1="${1#*:}"
func2="${2#*:}"

[ "$file2" ] || file2="$file1"
[ "$func2" ] || func2="$func1"

shift
shift

if [ -f "$file1" -a -f "$file2" ]
then
	diff "${@}" \
	<( sed -e "${FUNCSTART//\%\%/$func1}" -e "${FUNCEND//\%\%/$func1}" "$file1" ) \
	<( sed -e "${FUNCSTART//\%\%/$func2}" -e "${FUNCEND//\%\%/$func2}" "$file1" )

fi

-- 
for((P=10**8,Q=P/100,X=320*Q/(`tput cols`-1),Y=210*Q/`tput lines`,y=-105*Q,v=\
-220*Q,x=v;y<105*Q;x=v,y+=Y));do for((;x<P;a=b=i=k=c=0,x+=X));do for((;a*a+b*\
b<2*P*P&&i++<99;a=((c=a)*a-b*b)/P+x,b=2*c*b/P+y));do :;done;(((j=(i<99?i%16:0\
)+30)>37?k=1,j-=8:0));echo -ne "\E[$k;$j"mE;done;echo -e \\E[0m;done # ccooke
-- 
Gllug mailing list  -  Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug




More information about the GLLUG mailing list