Skip to content

Commit d786fc3

Browse files
committed
Added myers comparison table.
1 parent a3dc134 commit d786fc3

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

xtra/DIFF.LUA

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,34 @@
22

33
local PF,CT,M="I2I4I8",{},0xFFFFFFFF
44
local function diff_u(fn1,fn2)
5-
local function open(fn)
6-
local f,e=io.open(fn, "rb")
7-
if not f then print(e)os.exit(1)end
8-
local o,p=0,""
5+
local function open(fn)local f,e=io.open(fn, "rb")if not f then print(e)os.exit(1)end
96
local function crc32(s)local c=M for i=1,#s do local byte=s:byte(i)c=(c >> 8)~CT[(c~byte)&0xFF]end return(~c)&M end
10-
while true do local ls,l=o,{}
11-
while true do local c=f:read(1)if not c then break end
7+
local o,p=0,""while 1 do local ls,l=o,{}
8+
while 1 do local c=f:read(1)if not c then break end
129
local function eol()if c=='\r'then local d=f:read(1)f:seek("cur",-1)return d~='\n'end return c=='\n'end
1310
o=o+1 table.insert(l,c)if eol()then break end end
1411
if #l==0 then break end l=table.concat(l)local ln,c=#l,crc32(l)p=p..string.pack(PF,ln,c,ls)end f:close()return p end
15-
--TODO: Replace this code with myers algorithm
1612
local f1,f2,size=open(fn1),open(fn2),string.packsize(PF)
17-
local function printFile(f)local len=#f//size
18-
for i=1,len do local length,crc,offset=string.unpack(PF,f,(i-1)*size+1)
19-
print(("Offset: %06d | Length: %03d | CRC32: %08X"):format(offset,length,crc))end end
20-
printFile(f1)printFile(f2)end
13+
local function cmp(x,y)
14+
local l1,c1,o1=string.unpack(PF,f1,(x-1)*size+1)
15+
local l2,c2,o2=string.unpack(PF,f2,(y-1)*size+1)
16+
if l1~=l2 or c1~=c2 then return false end
17+
local function bin()
18+
local function line(fn,o,l)
19+
local f,e=io.open(fn,"rb")if not f then return nil,e end
20+
f:seek("set",o)local get=f:read(l)f:close()return get end
21+
local b1,e,b2=line(fn1,o1,l1)if not b1 then error(e)end
22+
b2,e=line(fn2,o2,l2)if not b2 then error(e)end
23+
return b1==b2 end return bin()end
24+
local function print_table()
25+
--TODO: store and reverse lookup this table to find the least amount of changes
26+
local il,jl=#f1//size,#f2//size
27+
for i=1,il do
28+
local row = {}
29+
for j=1,jl do table.insert(row,cmp(i,j) and "1" or "0")end
30+
print(table.concat(row, " "))end end
31+
print_table()
32+
end
2133
if#arg<2 or#arg%2~=0 then print(arg[-1].." "..arg[0]..[[ old new...
2234
2335
Compare differences between files, print them as unified context.

0 commit comments

Comments
 (0)