Class: AssertValue::TextDiff
- Inherits:
-
Object
- Object
- AssertValue::TextDiff
- Defined in:
- lib/text_diff.rb
Class Method Summary collapse
- .array_diff(a, b) ⇒ Object
- .chunk(arr, from, to) ⇒ Object
- .diff(a_text, b_text) ⇒ Object
- .mapped_chunk(arr, from, to) ⇒ Object
- .record_stat(arr, at, remcount, addcount, linecount, offset) ⇒ Object
Class Method Details
.array_diff(a, b) ⇒ Object
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 |
# 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] 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 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 = 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 |
.chunk(arr, from, to) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/text_diff.rb', line 14 def self.chunk(arr, from, to) result = [] if to - from > 2 result += self.mapped_chunk(arr, from, from+2) if from > 0 result << ['@', to-2] result += self.mapped_chunk(arr, to-2, to) else result << ['@', from] if from == 0 result += self.mapped_chunk(arr, from, to) end end |
.diff(a_text, b_text) ⇒ Object
31 32 33 34 35 |
# File 'lib/text_diff.rb', line 31 def self.diff(a_text, b_text) a = a_text.split("\n") b = b_text.split("\n") self.array_diff(a, b) end |
.mapped_chunk(arr, from, to) ⇒ Object
9 10 11 12 |
# File 'lib/text_diff.rb', line 9 def self.mapped_chunk(arr, from, to) return [] if to < from arr[from, to-from+1].map{|x| [' ', x]} end |
.record_stat(arr, at, remcount, addcount, linecount, offset) ⇒ Object
26 27 28 29 |
# File 'lib/text_diff.rb', line 26 def self.record_stat(arr, at, remcount, addcount, linecount, offset) index = arr[at][1]+1 ['', "@@ -#{index},#{linecount-addcount}, +#{index+offset},#{linecount-remcount} @@"] end |