11#! /usr/bin/env lua
22
33local CL ,PF ,CT ,M = tonumber (os.getenv (" DIFF_CONTEXT" ))or 3 ," I2I4I8" ,{},0xFFFFFFFF
4- 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
54local function diff_u (fn1 ,fn2 )
6- local function open (fn )local f ,e = io.open (fn )if not f then print (e )os.exit (1 )end return f end
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 offset ,packed = 0 ," "
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 (- 1 , " cur" )
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
35+ -- TODO: Refactor code to work with packed data.
736 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 = {}}
837 local function FP ()
938 local function SW (s ) return string.match (s ," %s" )and ' "' .. s .. ' "' or s end
@@ -29,6 +58,7 @@ local function diff_u(fn1,fn2)
2958 else table.insert (p ,l2 )end x ,y ,l1 ,l2 = x + 1 ,y + 1 ,f1 :read (" *l" ),f2 :read (" *l" )end
3059 local function ri ()FP ()p1 = (CL * 2 )+ 1 table.insert (d ,l1 )x ,l1 = x + 1 ,f1 :read (" *l" )end
3160 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
3262 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
3363if # arg < 2 or # arg % 2 ~= 0 then print (arg [- 1 ].. " " .. arg [0 ].. [[ old new...
3464
0 commit comments