|
1 | 1 | #!/usr/bin/env lua |
2 | 2 |
|
3 | | -local CL,PF,CT,M=tonumber(os.getenv("DIFF_CONTEXT"))or 3,"I2I4I8",{},0xFFFFFFFF |
| 3 | +local PF,CT,M="I2I4I8",{},0xFFFFFFFF |
4 | 4 | local function diff_u(fn1,fn2) |
5 | 5 | local function open(fn) |
6 | 6 | local f,e=io.open(fn, "rb") |
7 | 7 | if not f then print(e)os.exit(1)end |
8 | | - local offset,packed=0,"" |
| 8 | + local o,p=0,"" |
9 | 9 | 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 |
11 | | - local ls,l = offset,{} |
12 | | - while true do |
13 | | - local c = f:read(1) |
14 | | - if not c then break end |
15 | | - local function eol() |
16 | | - if c == '\r' then |
17 | | - local d = f:read(1) |
18 | | - f:seek( "cur", -1) |
19 | | - return d ~= '\n' |
20 | | - end |
21 | | - return c == '\n' |
22 | | - end |
23 | | - offset = offset + 1 |
24 | | - table.insert(l, c) |
25 | | - if eol() then break end |
26 | | - end |
27 | | - if #l == 0 then break end |
28 | | - local full_line = table.concat(l) |
29 | | - local length, crc = #full_line, crc32(full_line) |
30 | | - packed = packed .. string.pack(PF, length, crc, ls) |
31 | | - end |
32 | | - f:close() |
33 | | - return packed |
34 | | - end |
| 10 | + while true do local ls,l=o,{} |
| 11 | + while true do local c = f:read(1)if not c then break end |
| 12 | + 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 | + o=o+1 table.insert(l,c)if eol()then break end end |
| 14 | + 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 |
| 15 | + f:close()return p end |
35 | 16 | --TODO: Replace this code with myers algorithm |
36 | 17 | local f1,f2,size=open(fn1),open(fn2),string.packsize(PF) |
37 | | - local function printFile(f) |
38 | | - local len = #f // size |
39 | | - for i=1,len do |
40 | | - local pos = (i - 1) * size + 1 |
41 | | - local length, crc, offset = string.unpack(PF, f, pos) |
42 | | - print(("Offset: %06d | Length: %03d | CRC32: %08X"):format(offset, length, crc)) |
43 | | - end |
44 | | - end |
| 18 | + local function printFile(f)local len=#f//size |
| 19 | + for i=1,len do local length,crc,offset=string.unpack(PF,f,(i-1)*size+1) |
| 20 | + print(("Offset: %06d | Length: %03d | CRC32: %08X"):format(offset,length,crc))end end |
45 | 21 | printFile(f1) |
46 | 22 | printFile(f2) |
47 | 23 | end |
|
0 commit comments