Skip to content

Commit 92b7ed5

Browse files
committed
Squish rewritten open and printFile functions.
1 parent 4996a71 commit 92b7ed5

File tree

1 file changed

+11
-35
lines changed

1 file changed

+11
-35
lines changed

xtra/DIFF.LUA

Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,23 @@
11
#!/usr/bin/env lua
22

3-
local CL,PF,CT,M=tonumber(os.getenv("DIFF_CONTEXT"))or 3,"I2I4I8",{},0xFFFFFFFF
3+
local PF,CT,M="I2I4I8",{},0xFFFFFFFF
44
local function diff_u(fn1,fn2)
55
local function open(fn)
66
local f,e=io.open(fn, "rb")
77
if not f then print(e)os.exit(1)end
8-
local offset,packed=0,""
8+
local o,p=0,""
99
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
3516
--TODO: Replace this code with myers algorithm
3617
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
4521
printFile(f1)
4622
printFile(f2)
4723
end

0 commit comments

Comments
 (0)