File: diff.rb

package info (click to toggle)
ruby-ansi 1.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 404 kB
  • sloc: ruby: 1,880; makefile: 5
file content (215 lines) | stat: -rw-r--r-- 5,418 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
require 'ansi/code'

module ANSI

  # Diff produces colorized differences of two string or objects.
  #
  class Diff

    # Highlights the differnce between two strings.
    #
    # This class method is equivalent to calling:
    #
    #   ANSI::Diff.new(object1, object2).to_a
    #
    def self.diff(object1, object2, options={})
      new(object1, object2, options={}).to_a
    end

    # Setup new Diff object. If the objects given are not Strings
    # and do not have `#to_str` defined to coerce them to such, then
    # their `#inspect` methods are used to convert them to strings
    # for comparison.
    #
    # @param [Object] object1
    #   First object to compare.
    #
    # @param [Object] object2
    #   Second object to compare.
    #
    # @param [Hash] options
    #   Options for contoller the way difference is shown. (Not yet used.)
    #
    def initialize(object1, object2, options={})
      @object1 = convert(object1)
      @object2 = convert(object2)

      @diff1, @diff2 = diff_string(@object1, @object2)
    end

    # Returns the first object's difference string.
    def diff1
      @diff1
    end

    # Returns the second object's difference string.
    def diff2
      @diff2
    end

    # Returns both first and second difference strings separated by a
    # new line character.
    #
    # @todo Should we use `$/` record separator instead?
    #
    # @return [String] Joined difference strings.
    def to_s
      "#{@diff1}\n#{@diff2}"
    end

    # Returns both first and second difference strings separated by a
    # the given `separator`. The default is `$/`, the record separator.
    #
    # @param [String] separator
    #   The string to use as the separtor between the difference strings.
    #
    # @return [String] Joined difference strings.
    def join(separator=$/)
      "#{@diff1}#{separator}#{@diff2}"
    end

    # Returns the first and second difference strings in an array.
    #
    # @return [Array] Both difference strings.
    def to_a
      [diff1, diff2]
    end

  private

    # Take two plain strings and produce colorized
    # versions of each highlighting their differences.
    #
    # @param [String] string1
    #   First string to compare.
    #
    # @param [String] string2
    #   Second string to compare.
    #
    # @return [Array<String>] The two difference strings.
    def diff_string(string1, string2)
      compare(string1, string2)
    end

    # Ensure the object of comparison is a string. If +object+ is not
    # an instance of String then it wll be converted to one by calling
    # either #to_str, if the object responds to it, or #inspect.
    def convert(object)
      if String === object
        object
      elsif object.respond_to?(:to_str)
        object.to_str
      else
        object.inspect
      end
    end

    # Rotation of colors for diff output.
    COLORS = [:red, :yellow, :magenta]

    # Take two plain strings and produce colorized
    # versions of each highlighting their differences.
    #
    # @param [String] x
    #   First string to compare.
    #
    # @param [String] y
    #   Second string to compare.
    #
    # @return [Array<String>] The two difference strings.
    def compare(x, y)
      c = common(x, y)
      a = x.dup
      b = y.dup
      oi = 0
      oj = 0
      c.each_with_index do |m, q|
        i = a.index(m, oi)
        j = b.index(m, oj)
        a[i,m.size] = ANSI.ansi(m, COLORS[q%3]) if i
        b[j,m.size] = ANSI.ansi(m, COLORS[q%3]) if j
        oi = i + m.size if i
        oj = j + m.size if j
      end
      return a, b
    end

    # Oh, I should have documented this will I knew what the
    # hell it was doing ;)
    def common(x,y)
      c = lcs(x, y)

      i = x.index(c)
      j = y.index(c)

      ix = i + c.size
      jx = j + c.size

      if i == 0 
        l = y[0...j]
      elsif j == 0
        l = x[0...i]
      else
        l = common(x[0...i], y[0...j])
      end

      if ix == x.size - 1
        r = y[jx..-1]
      elsif jx = y.size - 1
        r = x[ix..-1]
      else
        r = common(x[ix..-1], y[jx..-1])
      end

      [l, c, r].flatten.reject{ |s| s.empty? }
    end

    # Least common string.
    def lcs(s1, s2)
      res="" 
      num=Array.new(s1.size){Array.new(s2.size)}
      len,ans=0
      lastsub=0
      s1.scan(/./).each_with_index do |l1,i |
        s2.scan(/./).each_with_index do |l2,j |
          unless l1==l2
            num[i][j]=0
          else
            (i==0 || j==0)? num[i][j]=1 : num[i][j]=1 + num[i-1][j-1]
            if num[i][j] > len
              len = ans = num[i][j]
              thissub = i
              thissub -= num[i-1][j-1] unless num[i-1][j-1].nil?  
              if lastsub==thissub
                res+=s1[i,1]
              else
                lastsub=thissub
                res=s1[lastsub, (i+1)-lastsub]
              end
            end
          end
        end
      end
      res
    end

    # Hmm... is this even useful?
    def lcs_size(s1, s2)
      num=Array.new(s1.size){Array.new(s2.size)}
      len,ans=0,0
      s1.scan(/./).each_with_index do |l1,i |
        s2.scan(/./).each_with_index do |l2,j |
          unless l1==l2
            num[i][j]=0
          else
            (i==0 || j==0)? num[i][j]=1 : num[i][j]=1 + num[i-1][j-1]
            len = ans = num[i][j] if num[i][j] > len
          end
        end
      end
      ans
    end

  end

end