Skip to content

Commit 87b0d08

Browse files
committed
Consolidated loop logic.
1 parent 3509c6f commit 87b0d08

File tree

1 file changed

+3
-15
lines changed

1 file changed

+3
-15
lines changed

xtra/DIFF.LUA

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,23 @@ local function diff_u(fn1,fn2)
1818
f:seek("set",o)local g=f:read(l)f:close()return g end
1919
local b1,e,b2=line(fn1,o1,l1)if not b1 then error(e)end b2,e=line(fn2,o2,l2)if not b2 then error(e)end
2020
return b1==b2 end
21-
local h,w,m=#f1//ps,#f2//ps,{}for i=1,h do for j=1,w do if cmp(i,j)then table.insert(m,j+(h*(i-1)))end end end
22-
local function scr(x,y)local i,j=y+h*(x-1),0 for k=1,#m do if m[k]<=i then j=j+1 else return j,m[k]end end return j end
23-
local x,y,path=h,w,{}
24-
while x>0 and y>0 do
25-
local s=scr(x,y)
26-
if scr(x,y-1)==s-1 then
27-
table.insert(path,1,{x=x,y=y})x,y=x-1,y-1
28-
elseif scr(x-1,y-1)==s then
29-
x=x-1
30-
else
31-
y=y-1
32-
end
33-
end
21+
local h,w,m=#f1//ps,#f2//ps,{}for i=1,h do for j=1,w do if cmp(i,j)then table.insert(m,{y=j,x=i})end end end
3422
local function get(fn,p,i)
3523
local l,_,o=string.unpack(PF,p,(i-1)*ps+1)
3624
local f=assert(io.open(fn,"rb"))f:seek("set",o)
3725
local r=f:read(l)f:close()return r end
3826
local function pfl(p,s)return(p..s:gsub('\n$',''):gsub('\r$',''))end
3927
local i,j,u=1,1
4028
local function flush()if #u.l>0 then
41-
if u.e==4 then table.remove(u.l)u.o.l,u.n.l=u.o.l-1,u.n.l-1 end
29+
if u.e>=4 then table.remove(u.l)u.o.l,u.n.l=u.o.l-1,u.n.l-1 end
4230
table.insert(u.l,1,"@@ -"..u.o.s..(u.o.l==1 and""or","..u.o.l).." +"..u.n.s..(u.n.l==1 and""or","..u.n.l).. " @@")
4331
if not fh then
4432
local function fn(s)return string.match(s,"%s")and '"'..s..'"'or s end
4533
table.insert(u.l,1,"+++ "..fn(fn2))table.insert(u.l,1,"--- "..fn(fn1))fh=true end
4634
for _,v in ipairs(u.l) do print(v)end end u=nil end
4735
local function init()if not u then local k,l=math.max(i-3,1),math.max(j-3,1)u={e=0,l={},o={s=k,l=0},n={s=l,l=0}}
4836
for z=k,i-1 do table.insert(u.l,pfl(" ",get(fn1,f1,z)))u.o.l,u.n.l=u.o.l+1,u.n.l+1 end end end
49-
for _,p in ipairs(path)do
37+
for _,p in ipairs(m)do
5038
while i<p.x do init()table.insert(u.l,pfl("-",get(fn1,f1,i)))i,u.o.l,u.e=i+1,u.o.l+1,0 end
5139
while j<p.y do init()table.insert(u.l,pfl("+",get(fn2,f2,j)))j,u.n.l,u.e=j+1,u.n.l+1,0 end
5240
if u then if u.e<4 then table.insert(u.l,pfl(" ",get(fn1,f1,i)))u.e,u.o.l,u.n.l=u.e+1,u.o.l+1,u.n.l+1 else flush()end end i,j=i+1,j+1 end

0 commit comments

Comments
 (0)