Project

General

Profile

Feature #32938 » 0001-Refactor-lib-diff.rb.patch

Marius BĂLTEANU, 2021-10-26 09:32

View differences:

.rubocop.yml
133 133
Style/BlockComments:
134 134
  Enabled: true
135 135
  Exclude:
136
    - 'lib/diff.rb'
136
    - 'lib/redmine/string_array_diff/diff.rb'
137
    - 'lib/redmine/string_array_diff/diffable.rb'
137 138

  
138 139
Style/BlockDelimiters:
139 140
  Enabled: true
140 141
  Exclude:
141 142
    - 'db/migrate/007_create_journals.rb'
142
    - 'lib/diff.rb'
143
    - 'lib/redmine/string_array_diff/diff.rb'
144
    - 'lib/redmine/string_array_diff/diffable.rb'
143 145

  
144 146
Style/EmptyElse:
145 147
  EnforcedStyle: empty
.rubocop_todo.yml
22 22
Layout/EmptyLineAfterGuardClause:
23 23
  Exclude:
24 24
    - 'db/migrate/101_populate_changesets_user_id.rb'
25
    - 'lib/diff.rb'
25
    - 'lib/redmine/string_array_diff/diff.rb'
26
    - 'lib/redmine/string_array_diff/diffable.rb'
26 27
    - 'lib/redmine/default_data/loader.rb'
27 28
    - 'lib/redmine/safe_attributes.rb'
28 29
    - 'lib/redmine/wiki_formatting/textile/redcloth3.rb'
......
225 226
Layout/SpaceInsideArrayLiteralBrackets:
226 227
  Exclude:
227 228
    - 'app/models/query.rb'
228
    - 'lib/diff.rb'
229
    - 'lib/redmine/string_array_diff/diff.rb'
229 230
    - 'test/unit/mailer_localisation_test.rb'
230 231

  
231 232
# Cop supports --auto-correct.
......
1122 1123
# Cop supports --auto-correct.
1123 1124
Style/ClassMethods:
1124 1125
  Exclude:
1125
    - 'lib/diff.rb'
1126
    - 'lib/redmine/string_array_diff/diff.rb'
1126 1127

  
1127 1128
Style/ClassVars:
1128 1129
  Exclude:
......
1439 1440
    - 'app/models/custom_field.rb'
1440 1441
    - 'app/models/project.rb'
1441 1442
    - 'app/models/repository/cvs.rb'
1442
    - 'lib/diff.rb'
1443
    - 'lib/redmine/string_array_diff/diff.rb'
1443 1444
    - 'lib/redmine/codeset_util.rb'
1444 1445
    - 'lib/redmine/thumbnail.rb'
1445 1446

  
app/models/wiki_page.rb
17 17
# along with this program; if not, write to the Free Software
18 18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 19

  
20
require 'diff'
20
require 'redmine/string_array_diff/diff'
21 21

  
22 22
class WikiPage < ActiveRecord::Base
23 23
  include Redmine::SafeAttributes
lib/diff.rb
1
# frozen_string_literal: true
2

  
3
module RedmineDiff
4
  class Diff
5
    VERSION = 0.3
6

  
7
    def Diff.lcs(a, b)
8
      astart = 0
9
      bstart = 0
10
      afinish = a.length-1
11
      bfinish = b.length-1
12
      mvector = []
13

  
14
      # First we prune off any common elements at the beginning
15
      while (astart <= afinish) && (bstart <= afinish) && (a[astart] == b[bstart])
16
        mvector[astart] = bstart
17
        astart += 1
18
        bstart += 1
19
      end
20

  
21
      # now the end
22
      while (astart <= afinish) && (bstart <= bfinish) && (a[afinish] == b[bfinish])
23
        mvector[afinish] = bfinish
24
        afinish -= 1
25
        bfinish -= 1
26
      end
27

  
28
      bmatches = b.reverse_hash(bstart..bfinish)
29
      thresh = []
30
      links = []
31

  
32
      (astart..afinish).each { |aindex|
33
        aelem = a[aindex]
34
        next unless bmatches.has_key? aelem
35
        k = nil
36
        bmatches[aelem].reverse_each { |bindex|
37
          if k && (thresh[k] > bindex) && (thresh[k-1] < bindex)
38
            thresh[k] = bindex
39
          else
40
            k = thresh.replacenextlarger(bindex, k)
41
          end
42
          links[k] = [ (k==0) ? nil : links[k-1], aindex, bindex ] if k
43
        }
44
      }
45

  
46
      if !thresh.empty?
47
        link = links[thresh.length-1]
48
        while link
49
          mvector[link[1]] = link[2]
50
          link = link[0]
51
        end
52
      end
53

  
54
      return mvector
55
    end
56

  
57
    def makediff(a, b)
58
      mvector = Diff.lcs(a, b)
59
      ai = bi = 0
60
      while ai < mvector.length
61
        bline = mvector[ai]
62
        if bline
63
          while bi < bline
64
            discardb(bi, b[bi])
65
            bi += 1
66
          end
67
          match(ai, bi)
68
          bi += 1
69
        else
70
          discarda(ai, a[ai])
71
        end
72
        ai += 1
73
      end
74
      while ai < a.length
75
        discarda(ai, a[ai])
76
        ai += 1
77
      end
78
      while bi < b.length
79
        discardb(bi, b[bi])
80
        bi += 1
81
      end
82
      match(ai, bi)
83
      1
84
    end
85

  
86
    def compactdiffs
87
      diffs = []
88
      @diffs.each { |df|
89
        i = 0
90
        curdiff = []
91
        while i < df.length
92
          whot = df[i][0]
93
          s = @isstring ? df[i][2].chr : [df[i][2]]
94
          p = df[i][1]
95
          last = df[i][1]
96
          i += 1
97
          while df[i] && df[i][0] == whot && df[i][1] == last+1
98
            s << df[i][2]
99
            last  = df[i][1]
100
            i += 1
101
          end
102
          curdiff.push [whot, p, s]
103
        end
104
        diffs.push curdiff
105
      }
106
      return diffs
107
    end
108

  
109
    attr_reader :diffs, :difftype
110

  
111
    def initialize(diffs_or_a, b = nil, isstring = nil)
112
      if b.nil?
113
        @diffs = diffs_or_a
114
        @isstring = isstring
115
      else
116
        @diffs = []
117
        @curdiffs = []
118
        makediff(diffs_or_a, b)
119
        @difftype = diffs_or_a.class
120
      end
121
    end
122

  
123
    def match(ai, bi)
124
      @diffs.push @curdiffs unless @curdiffs.empty?
125
      @curdiffs = []
126
    end
127

  
128
    def discarda(i, elem)
129
      @curdiffs.push ['-', i, elem]
130
    end
131

  
132
    def discardb(i, elem)
133
      @curdiffs.push ['+', i, elem]
134
    end
135

  
136
    def compact
137
      return Diff.new(compactdiffs)
138
    end
139

  
140
    def compact!
141
      @diffs = compactdiffs
142
    end
143

  
144
    def inspect
145
      @diffs.inspect
146
    end
147
  end
148
end
149

  
150
module Diffable
151
  def diff(b)
152
    RedmineDiff::Diff.new(self, b)
153
  end
154

  
155
  # Create a hash that maps elements of the array to arrays of indices
156
  # where the elements are found.
157

  
158
  def reverse_hash(range = (0...self.length))
159
    revmap = {}
160
    range.each { |i|
161
      elem = self[i]
162
      if revmap.has_key? elem
163
        revmap[elem].push i
164
      else
165
        revmap[elem] = [i]
166
      end
167
    }
168
    return revmap
169
  end
170

  
171
  def replacenextlarger(value, high = nil)
172
    high ||= self.length
173
    if self.empty? || value > self[-1]
174
      push value
175
      return high
176
    end
177
    # binary search for replacement point
178
    low = 0
179
    while low < high
180
      index = (high+low)/2
181
      found = self[index]
182
      return nil if value == found
183
      if value > found
184
        low = index + 1
185
      else
186
        high = index
187
      end
188
    end
189

  
190
    self[low] = value
191
    #  $stderr << "replace #{value} : 0/#{low}/#{init_high} (#{steps} steps) (#{init_high-low} off )\n"
192
    #  $stderr.puts self.inspect
193
    # gets
194
    # p length - low
195
    return low
196
  end
197

  
198
  def patch(diff)
199
    newary = nil
200
    if diff.difftype == String
201
      newary = diff.difftype.new('')
202
    else
203
      newary = diff.difftype.new
204
    end
205
    ai = 0
206
    bi = 0
207
    diff.diffs.each { |d|
208
      d.each { |mod|
209
        case mod[0]
210
        when '-'
211
          while ai < mod[1]
212
            newary << self[ai]
213
            ai += 1
214
            bi += 1
215
          end
216
          ai += 1
217
        when '+'
218
          while bi < mod[1]
219
            newary << self[ai]
220
            ai += 1
221
            bi += 1
222
          end
223
          newary << mod[2]
224
          bi += 1
225
        else
226
          raise "Unknown diff action"
227
        end
228
      }
229
    }
230
    while ai < self.length
231
      newary << self[ai]
232
      ai += 1
233
      bi += 1
234
    end
235
    return newary
236
  end
237
end
238

  
239
class Array
240
  include Diffable
241
end
242

  
243
class String
244
  include Diffable
245
end
246

  
247
=begin
248
  = Diff
249
  (({diff.rb})) - computes the differences between two arrays or
250
  strings. Copyright (C) 2001 Lars Christensen
251

  
252
  == Synopsis
253

  
254
      diff = Diff.new(a, b)
255
      b = a.patch(diff)
256

  
257
  == Class Diff
258
  === Class Methods
259
  --- Diff.new(a, b)
260
  --- a.diff(b)
261
        Creates a Diff object which represent the differences between
262
        ((|a|)) and ((|b|)). ((|a|)) and ((|b|)) can be either be arrays
263
        of any objects, strings, or object of any class that include
264
        module ((|Diffable|))
265

  
266
  == Module Diffable
267
  The module ((|Diffable|)) is intended to be included in any class for
268
  which differences are to be computed. Diffable is included into String
269
  and Array when (({diff.rb})) is (({require}))'d.
270

  
271
  Classes including Diffable should implement (({[]})) to get element at
272
  integer indices, (({<<})) to append elements to the object and
273
  (({ClassName#new})) should accept 0 arguments to create a new empty
274
  object.
275

  
276
  === Instance Methods
277
  --- Diffable#patch(diff)
278
        Applies the differences from ((|diff|)) to the object ((|obj|))
279
        and return the result. ((|obj|)) is not changed. ((|obj|)) and
280
        can be either an array or a string, but must match the object
281
        from which the ((|diff|)) was created.
282
=end
lib/redmine/helpers/diff.rb
17 17
# along with this program; if not, write to the Free Software
18 18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 19

  
20
require 'diff'
20
require 'redmine/string_array_diff/diff'
21 21

  
22 22
module Redmine
23 23
  module Helpers
lib/redmine/string_array_diff/diff.rb
1
# frozen_string_literal: true
2

  
3
=begin
4
  = Diff
5
  (({diff.rb})) - computes the differences between two arrays or
6
  strings. Copyright (C) 2001 Lars Christensen
7

  
8
  == Synopsis
9

  
10
      diff = Diff.new(a, b)
11
      b = a.patch(diff)
12

  
13
  == Class Diff
14
  === Class Methods
15
  --- Diff.new(a, b)
16
  --- a.diff(b)
17
        Creates a Diff object which represent the differences between
18
        ((|a|)) and ((|b|)). ((|a|)) and ((|b|)) can be either be arrays
19
        of any objects, strings, or object of any class that include
20
        module ((|Diffable|))
21

  
22
  == Module Diffable
23
  The module ((|Diffable|)) is intended to be included in any class for
24
  which differences are to be computed. Diffable is included into String
25
  and Array when (({diff.rb})) is (({require}))'d.
26

  
27
  Classes including Diffable should implement (({[]})) to get element at
28
  integer indices, (({<<})) to append elements to the object and
29
  (({ClassName#new})) should accept 0 arguments to create a new empty
30
  object.
31

  
32
  === Instance Methods
33
  --- Diffable#patch(diff)
34
        Applies the differences from ((|diff|)) to the object ((|obj|))
35
        and return the result. ((|obj|)) is not changed. ((|obj|)) and
36
        can be either an array or a string, but must match the object
37
        from which the ((|diff|)) was created.
38
=end
39

  
40
module Redmine
41
  module StringArrayDiff
42
    class Diff
43
      VERSION = 0.3
44

  
45
      def Diff.lcs(a, b)
46
        astart = 0
47
        bstart = 0
48
        afinish = a.length-1
49
        bfinish = b.length-1
50
        mvector = []
51

  
52
        # First we prune off any common elements at the beginning
53
        while (astart <= afinish) && (bstart <= afinish) && (a[astart] == b[bstart])
54
          mvector[astart] = bstart
55
          astart += 1
56
          bstart += 1
57
        end
58

  
59
        # now the end
60
        while (astart <= afinish) && (bstart <= bfinish) && (a[afinish] == b[bfinish])
61
          mvector[afinish] = bfinish
62
          afinish -= 1
63
          bfinish -= 1
64
        end
65

  
66
        bmatches = b.reverse_hash(bstart..bfinish)
67
        thresh = []
68
        links = []
69

  
70
        (astart..afinish).each { |aindex|
71
          aelem = a[aindex]
72
          next unless bmatches.has_key? aelem
73
          k = nil
74
          bmatches[aelem].reverse_each { |bindex|
75
            if k && (thresh[k] > bindex) && (thresh[k-1] < bindex)
76
              thresh[k] = bindex
77
            else
78
              k = thresh.replacenextlarger(bindex, k)
79
            end
80
            links[k] = [ (k==0) ? nil : links[k-1], aindex, bindex ] if k
81
          }
82
        }
83

  
84
        if !thresh.empty?
85
          link = links[thresh.length-1]
86
          while link
87
            mvector[link[1]] = link[2]
88
            link = link[0]
89
          end
90
        end
91

  
92
        return mvector
93
      end
94

  
95
      def makediff(a, b)
96
        mvector = Diff.lcs(a, b)
97
        ai = bi = 0
98
        while ai < mvector.length
99
          bline = mvector[ai]
100
          if bline
101
            while bi < bline
102
              discardb(bi, b[bi])
103
              bi += 1
104
            end
105
            match(ai, bi)
106
            bi += 1
107
          else
108
            discarda(ai, a[ai])
109
          end
110
          ai += 1
111
        end
112
        while ai < a.length
113
          discarda(ai, a[ai])
114
          ai += 1
115
        end
116
        while bi < b.length
117
          discardb(bi, b[bi])
118
          bi += 1
119
        end
120
        match(ai, bi)
121
        1
122
      end
123

  
124
      def compactdiffs
125
        diffs = []
126
        @diffs.each { |df|
127
          i = 0
128
          curdiff = []
129
          while i < df.length
130
            whot = df[i][0]
131
            s = @isstring ? df[i][2].chr : [df[i][2]]
132
            p = df[i][1]
133
            last = df[i][1]
134
            i += 1
135
            while df[i] && df[i][0] == whot && df[i][1] == last+1
136
              s << df[i][2]
137
              last  = df[i][1]
138
              i += 1
139
            end
140
            curdiff.push [whot, p, s]
141
          end
142
          diffs.push curdiff
143
        }
144
        return diffs
145
      end
146

  
147
      attr_reader :diffs, :difftype
148

  
149
      def initialize(diffs_or_a, b = nil, isstring = nil)
150
        if b.nil?
151
          @diffs = diffs_or_a
152
          @isstring = isstring
153
        else
154
          @diffs = []
155
          @curdiffs = []
156
          makediff(diffs_or_a, b)
157
          @difftype = diffs_or_a.class
158
        end
159
      end
160

  
161
      def match(ai, bi)
162
        @diffs.push @curdiffs unless @curdiffs.empty?
163
        @curdiffs = []
164
      end
165

  
166
      def discarda(i, elem)
167
        @curdiffs.push ['-', i, elem]
168
      end
169

  
170
      def discardb(i, elem)
171
        @curdiffs.push ['+', i, elem]
172
      end
173

  
174
      def compact
175
        return Diff.new(compactdiffs)
176
      end
177

  
178
      def compact!
179
        @diffs = compactdiffs
180
      end
181

  
182
      def inspect
183
        @diffs.inspect
184
      end
185
    end
186
  end
187
end
188

  
189
class Array
190
  include Redmine::StringArrayDiff::Diffable
191
end
192

  
193
class String
194
  include Redmine::StringArrayDiff::Diffable
195
end
lib/redmine/string_array_diff/diffable.rb
1
# frozen_string_literal: true
2

  
3
=begin
4
  = Diff
5
  (({diff.rb})) - computes the differences between two arrays or
6
  strings. Copyright (C) 2001 Lars Christensen
7

  
8
  == Synopsis
9

  
10
      diff = Diff.new(a, b)
11
      b = a.patch(diff)
12

  
13
  == Class Diff
14
  === Class Methods
15
  --- Diff.new(a, b)
16
  --- a.diff(b)
17
        Creates a Diff object which represent the differences between
18
        ((|a|)) and ((|b|)). ((|a|)) and ((|b|)) can be either be arrays
19
        of any objects, strings, or object of any class that include
20
        module ((|Diffable|))
21

  
22
  == Module Diffable
23
  The module ((|Diffable|)) is intended to be included in any class for
24
  which differences are to be computed. Diffable is included into String
25
  and Array when (({diff.rb})) is (({require}))'d.
26

  
27
  Classes including Diffable should implement (({[]})) to get element at
28
  integer indices, (({<<})) to append elements to the object and
29
  (({ClassName#new})) should accept 0 arguments to create a new empty
30
  object.
31

  
32
  === Instance Methods
33
  --- Diffable#patch(diff)
34
        Applies the differences from ((|diff|)) to the object ((|obj|))
35
        and return the result. ((|obj|)) is not changed. ((|obj|)) and
36
        can be either an array or a string, but must match the object
37
        from which the ((|diff|)) was created.
38
=end
39

  
40
module Redmine
41
  module StringArrayDiff
42
    module Diffable
43
      def diff(b)
44
        Redmine::StringArrayDiff::Diff.new(self, b)
45
      end
46

  
47
      # Create a hash that maps elements of the array to arrays of indices
48
      # where the elements are found.
49

  
50
      def reverse_hash(range = (0...self.length))
51
        revmap = {}
52
        range.each { |i|
53
          elem = self[i]
54
          if revmap.has_key? elem
55
            revmap[elem].push i
56
          else
57
            revmap[elem] = [i]
58
          end
59
        }
60
        return revmap
61
      end
62

  
63
      def replacenextlarger(value, high = nil)
64
        high ||= self.length
65
        if self.empty? || value > self[-1]
66
          push value
67
          return high
68
        end
69
        # binary search for replacement point
70
        low = 0
71
        while low < high
72
          index = (high+low)/2
73
          found = self[index]
74
          return nil if value == found
75
          if value > found
76
            low = index + 1
77
          else
78
            high = index
79
          end
80
        end
81

  
82
        self[low] = value
83
        #  $stderr << "replace #{value} : 0/#{low}/#{init_high} (#{steps} steps) (#{init_high-low} off )\n"
84
        #  $stderr.puts self.inspect
85
        # gets
86
        # p length - low
87
        return low
88
      end
89

  
90
      def patch(diff)
91
        newary = nil
92
        if diff.difftype == String
93
          newary = diff.difftype.new('')
94
        else
95
          newary = diff.difftype.new
96
        end
97
        ai = 0
98
        bi = 0
99
        diff.diffs.each { |d|
100
          d.each { |mod|
101
            case mod[0]
102
            when '-'
103
              while ai < mod[1]
104
                newary << self[ai]
105
                ai += 1
106
                bi += 1
107
              end
108
              ai += 1
109
            when '+'
110
              while bi < mod[1]
111
                newary << self[ai]
112
                ai += 1
113
                bi += 1
114
              end
115
              newary << mod[2]
116
              bi += 1
117
            else
118
              raise "Unknown diff action"
119
            end
120
          }
121
        }
122
        while ai < self.length
123
          newary << self[ai]
124
          ai += 1
125
          bi += 1
126
        end
127
        return newary
128
      end
129
    end
130
  end
131
end
(11-11/16)