Class: Diff::GDiff::Operations::Data

Inherits:
Object
  • Object
show all
Defined in:
lib/gdiff.rb

Overview

Insert Data operation

Automatically splits itself into multiple operations on pack if neccessary. #pack chooses smallest possible representation.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Data

Create a new Data operation that inserts the given data at this point



29
30
31
# File 'lib/gdiff.rb', line 29

def initialize(data)
  @data = data
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



26
27
28
# File 'lib/gdiff.rb', line 26

def data
  @data
end

Class Method Details

.unpack_if_match(string, position) ⇒ Object

Takes a string and a position and returns the position after this operation and a new operation object if the operation starting at the position is a Data operation. Otherwise returns nil.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/gdiff.rb', line 66

def self.unpack_if_match(string, position)
  if string[position] < 245
    length = string[position]
    operation = self.new(string[position+1, length])
    position += length + 1
    return position, operation
  elsif (type = {247 => USHORT, 248 => INT}[string[position]])
    length = string[position + 1, type[2]].unpack(type[0])[0]
    operation = self.new(string[position + 1 + type[2], length])
    position += operation.data.length + 1 + type[2]
    return position, operation
  end
  nil
end

Instance Method Details

#lengthObject

return the length of the data object to insert



34
35
36
# File 'lib/gdiff.rb', line 34

def length
  @data.length
end

#packObject

Return a binary representation in gdiff format of the Data operation

The binary representation may consist of multiple Data operations if neccessary.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/gdiff.rb', line 42

def pack
  result = ""
  d = @data
  while d.length > 0
    if d.length < 246
      result << [d.length].pack("C") << d
      d = ""
    elsif d.length < USHORT[1]
      result << [247, d.length].pack("C" << USHORT[0]) << d
      d = ""
    elsif d.length < INT[1]
      result << [248, d.length].pack("C" << INT[0]) << d
      d = ""
    else
      result << [248, INT[1]].pack("C" << INT[0]) << d[0...INT[1]]
      d = d[INT[1]..-1]
    end
  end
  result
end

#to_sObject

Return a readable representation of the operation



82
83
84
85
# File 'lib/gdiff.rb', line 82

def to_s
  d = @data.inspect
  "Insert " << (d.length < 60 ? d : d[0..20] << "..." << d[-37..-1])
end