Class: DiffMatchPatchES::Patch

Inherits:
Object
  • Object
show all
Defined in:
lib/diff_match_patch_es/patch.rb,
sig/diff_match_patch_es/patch.rbs

Overview

One patch operation. start1/start2 are nil until the patch is initialized by patch_make (or parsed by patch_from_text).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePatch

Returns a new instance of Patch.



8
9
10
11
12
13
14
# File 'lib/diff_match_patch_es/patch.rb', line 8

def initialize
  @diffs = []
  @start1 = nil
  @start2 = nil
  @length1 = 0
  @length2 = 0
end

Instance Attribute Details

#diffsArray[diff]

Returns the value of attribute diffs.

Returns:



6
7
8
# File 'lib/diff_match_patch_es/patch.rb', line 6

def diffs
  @diffs
end

#length1Integer

Returns the value of attribute length1.

Returns:

  • (Integer)


6
7
8
# File 'lib/diff_match_patch_es/patch.rb', line 6

def length1
  @length1
end

#length2Integer

Returns the value of attribute length2.

Returns:

  • (Integer)


6
7
8
# File 'lib/diff_match_patch_es/patch.rb', line 6

def length2
  @length2
end

#start1Integer?

Returns the value of attribute start1.

Returns:

  • (Integer, nil)


6
7
8
# File 'lib/diff_match_patch_es/patch.rb', line 6

def start1
  @start1
end

#start2Integer?

Returns the value of attribute start2.

Returns:

  • (Integer, nil)


6
7
8
# File 'lib/diff_match_patch_es/patch.rb', line 6

def start2
  @start2
end

Instance Method Details

#==(other) ⇒ Boolean

Parameters:

  • other (Object)

Returns:

  • (Boolean)


48
49
50
51
52
53
54
55
# File 'lib/diff_match_patch_es/patch.rb', line 48

def ==(other)
  other.is_a?(Patch) &&
    diffs == other.diffs &&
    start1 == other.start1 &&
    start2 == other.start2 &&
    length1 == other.length1 &&
    length2 == other.length2
end

#to_sString

Emulate GNU diff's format.

Returns:

  • (String)


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
# File 'lib/diff_match_patch_es/patch.rb', line 19

def to_s
  coords1 = if length1.zero?
              "#{start1},0"
            elsif length1 == 1
              (start1 + 1).to_s
            else
              "#{start1 + 1},#{length1}"
            end
  coords2 = if length2.zero?
              "#{start2},0"
            elsif length2 == 1
              (start2 + 1).to_s
            else
              "#{start2 + 1},#{length2}"
            end

  text = +"@@ -#{coords1} +#{coords2} @@\n"
  # Escape the body of the patch with %xx notation.
  diffs.each do |(op, data)|
    sign = case op
           when DIFF_INSERT then '+'
           when DIFF_DELETE then '-'
           when DIFF_EQUAL then ' '
           end
    text << sign << JsString.encode_uri(data) << "\n"
  end
  text.gsub('%20', ' ')
end