@@ -15,7 +15,7 @@ local function diff_u(fn1,fn2)
1515 local function eol ()
1616 if c == ' \r ' then
1717 local d = f :read (1 )
18- f :seek (- 1 , " cur" )
18+ f :seek ( " cur" , - 1 )
1919 return d ~= ' \n '
2020 end
2121 return c == ' \n '
@@ -32,34 +32,19 @@ local function diff_u(fn1,fn2)
3232 f :close ()
3333 return packed
3434 end
35- -- TODO: Refactor code to work with packed data.
36- local f1 ,f2 ,p ,a ,d ,p1 ,p2 ,x ,y ,B ,h ,l1 ,l2 = open (fn1 ),open (fn2 ),{},{},{},0 ,0 ,0 ,0 ,{a = 0 ,d = 0 ,l = {}}
37- local function FP ()
38- local function SW (s ) return string.match (s ," %s" )and ' "' .. s .. ' "' or s end
39- if not h then print (" --- " .. SW (fn1 ).. " \n +++ " .. SW (fn2 ))h = true end
40- if # p > 0 then for _ ,v in ipairs (p )do table.insert (B .l ," " .. v )end B .o ,B .n = x -# p ,y -# p end p = {}end
41- local function FH ()
42- for _ ,v in ipairs (d )do table.insert (B .l ," -" .. v )end
43- for _ ,v in ipairs (a )do table.insert (B .l ," +" .. v )end
44- B .a ,B .d = B .a +# a ,B .d +# d d ,a = {},{}end
45- local function FB ()
46- if # B .l > 0 then B .d ,B .a =# B .l - B .a ,# B .l - B .d
47- local he = " @@ -" .. (B .o or B .d > 0 and " 1" or " 0" ).. (B .d == 1 and " " or " ," .. B .d ).. " +" .. (B .n or B .a > 0 and " 1" or " 0" ).. (B .a == 1 and " " or " ," .. B .a ).. " @@"
48- table.insert (B .l ,1 ,he )for _ ,v in ipairs (B .l )do print (v )end end B .l ,B .a ,B .d ,B .o ,B .n = {},0 ,0 ,nil ,nil end
49- local function di ()
50- FH ()if # p > CL then table.remove (p ,1 )end
51- if p1 > 0 or p2 > 0 then
52- if p1 > 0 and p2 > 0 then if p1 > CL or p2 > CL then table.insert (B .l ," " .. l2 )end p1 ,p2 = p1 - 1 ,p2 - 1
53- elseif p1 > 0 then if p1 > CL then table.insert (B .l ," " .. l1 )end p1 = p1 - 1
54- elseif p2 > 0 then if p2 > CL then table.insert (B .l ," " .. l2 )end p2 = p2 - 1
55- elseif p2 <= CL then table.insert (p ,l2 )
56- elseif p1 <= CL then table.insert (p ,l1 )end
57- if p1 == 0 or p2 == 0 then FB ()table.insert (p ,l2 )end
58- else table.insert (p ,l2 )end x ,y ,l1 ,l2 = x + 1 ,y + 1 ,f1 :read (" *l" ),f2 :read (" *l" )end
59- local function ri ()FP ()p1 = (CL * 2 )+ 1 table.insert (d ,l1 )x ,l1 = x + 1 ,f1 :read (" *l" )end
60- local function dn ()FP ()p2 = (CL * 2 )+ 1 table.insert (a ,l2 )y ,l2 = y + 1 ,f2 :read (" *l" )end
61- -- TODO: Implement myers logic off of packed CRC32
62- repeat if l1 == l2 then di ()elseif not l2 then ri ()elseif not l1 then dn ()else ri ()dn ()end until not l1 and not l2 f1 :close ()f2 :close ()if # a > 0 or # d > 0 then FP ()FH ()end if # B .l > 0 then FB ()end end
35+ -- TODO: Replace this code with myers algorithm
36+ 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
45+ printFile (f1 )
46+ printFile (f2 )
47+ end
6348if # arg < 2 or # arg % 2 ~= 0 then print (arg [- 1 ].. " " .. arg [0 ].. [[ old new...
6449
6550Compare differences between files, print them as unified context.
0 commit comments