|
| 1 | +local function open(filename) |
| 2 | + local f,e=io.open(filename) |
| 3 | + if not f then print(e) os.exit(1) end |
| 4 | + return f |
| 5 | +end |
| 6 | + |
| 7 | +local function blocks(f1, f2) |
| 8 | + local bs, b = 4096, {} |
| 9 | + repeat |
| 10 | + local l, r, lines, data = f1:read(bs), f2:read(bs), {}, nil |
| 11 | + if l then _, lines.l = string.gsub(l,"\n", "\n") end |
| 12 | + if r then _, lines.r = string.gsub(r,"\n", "\n") end |
| 13 | + if l ~= r then data = { l = l, r = r } end |
| 14 | + if lines.l or lines.r then |
| 15 | + table.insert(b, { data = data, lines = lines }) |
| 16 | + end |
| 17 | + until not l and not r |
| 18 | + return b |
| 19 | +end |
| 20 | + |
| 21 | +local function lines(str, prev) |
| 22 | + local l, last = {} |
| 23 | + |
| 24 | + for line, newline in str:gmatch("([^\n]*)(\n?)") do |
| 25 | + if newline ~= "\n" then last = line else table.insert(l, line) end |
| 26 | + end |
| 27 | + |
| 28 | + if prev and #l > 0 then l[1] = prev .. l[1] end |
| 29 | + return l, last |
| 30 | +end |
| 31 | + |
| 32 | +local function diff_u(fn1,fn2) |
| 33 | + local f1, f2 = open(fn1), open(fn2) |
| 34 | + local diff_b, dl, dr, tl, tr = blocks(f1, f2) |
| 35 | + f1:close() f2:close() |
| 36 | + |
| 37 | + if diff_b then |
| 38 | + for _, v in ipairs(diff_b) do |
| 39 | + dl, tl = lines(v.data.l) |
| 40 | + dr, tr = lines(v.data.r) |
| 41 | + local max = #dl > #dr and #dl or #dr |
| 42 | + |
| 43 | + for k = 1, max do |
| 44 | + if dl[k] ~= dr[k] then |
| 45 | + print("-"..dl[k].."\n+"..dr[k].."\n") |
| 46 | + end |
| 47 | + end |
| 48 | + end |
| 49 | + end |
| 50 | +end |
| 51 | + |
| 52 | +if #arg < 2 then return end |
| 53 | +diff_u(arg[1], arg[2]) |
0 commit comments