|
2 | 2 |
|
3 | 3 | local PF,CT,M="I2I4I8",{},0xFFFFFFFF |
4 | 4 | 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 |
9 | 6 | 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 |
12 | 9 | local function eol()if c=='\r'then local d=f:read(1)f:seek("cur",-1)return d~='\n'end return c=='\n'end |
13 | 10 | o=o+1 table.insert(l,c)if eol()then break end end |
14 | 11 | 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 |
16 | 12 | 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 |
21 | 33 | if#arg<2 or#arg%2~=0 then print(arg[-1].." "..arg[0]..[[ old new... |
22 | 34 |
|
23 | 35 | Compare differences between files, print them as unified context. |
|
0 commit comments