37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
# File 'lib/text_diff.rb', line 37
def self.array_diff(a, b)
diff = ArrayDiff.new(a, b)
return nil if diff.diffs.empty?
result = []
from = to = nextfrom = 0
offset = 0
diff.diffs.each do |tuple|
first = tuple[0][1]
length = tuple.length
action = tuple[0][0]
addcount = 0
remcount = 0
tuple.each do |l|
if l[0] == "+"
addcount += 1
elsif l[0] == "-"
remcount += 1
end
end
if remcount == 0
to = first-offset-1
nextfrom = to+1
else
to = first-1
nextfrom = to+remcount+1
end
result += self.chunk(a, from, to)
from = nextfrom
lastdel = (tuple[0][0] == "-")
tuple.each do |l|
if l[0] == "-"
offset -= 1
else
offset += 1
end
result << [l[0], l[2]]
end
end
if (a.length - from) > 2
result += self.chunk(a, from, from+2)
elsif a.length > 0
result += self.chunk(a, from, a.length-1)
end
linecount = addcount = remcount = offset = current_offset = 0
info_index = nil
result.each_with_index do |l, i|
if l[0] == '@'
result[info_index] = self.record_stat(result, info_index, remcount, addcount,
linecount, offset) if info_index
info_index = i
offset += addcount - remcount
linecount = 0
addcount = remcount = 0
elsif l[0] == '-'
remcount += 1
linecount += 1
elsif l[0] == '+'
addcount += 1
linecount += 1
else
linecount += 1
end
end
result[info_index] = self.record_stat(result, info_index, remcount, addcount, linecount, offset) if info_index
return result.map{|x| x.join('')}.join("\n")
end
|