Class: Diff::GDiff::Operations::Copy
- Inherits:
-
Object
- Object
- Diff::GDiff::Operations::Copy
- Defined in:
- lib/gdiff.rb
Overview
A Copy operation copies part of the source file to the target file.
The #pack operation may create multiple copy operations if neccessary.
Constant Summary collapse
- TYPES =
Codes and maximum sizes
[ [249, USHORT, UBYTE], [250, USHORT, USHORT], [251, USHORT, INT], [252, INT, UBYTE], [253, INT, USHORT], [254, INT, INT], [255, LONG, INT] ]
Instance Attribute Summary collapse
-
#length ⇒ Object
readonly
Returns the value of attribute length.
-
#position ⇒ Object
readonly
Returns the value of attribute position.
Class Method Summary collapse
-
.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 Copy operation.
Instance Method Summary collapse
-
#initialize(position, length) ⇒ Copy
constructor
Create a new Copy object that copies length bytes from the source string at position.
-
#pack ⇒ Object
Create a binary representation in gdiff format.
-
#to_s ⇒ Object
Return a readable representation of the operation.
Constructor Details
#initialize(position, length) ⇒ Copy
Create a new Copy object that copies length bytes from the source string at position.
104 105 106 107 |
# File 'lib/gdiff.rb', line 104 def initialize(position, length) @position = position @length = length end |
Instance Attribute Details
#length ⇒ Object (readonly)
Returns the value of attribute length.
92 93 94 |
# File 'lib/gdiff.rb', line 92 def length @length end |
#position ⇒ Object (readonly)
Returns the value of attribute position.
92 93 94 |
# File 'lib/gdiff.rb', line 92 def position @position 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 Copy operation. Otherwise returns nil.
138 139 140 141 142 143 144 145 146 147 |
# File 'lib/gdiff.rb', line 138 def self.unpack_if_match(string, position) TYPES.each do | (c, (p_pack, _, p_size), (l_pack, _, l_size)) | if c == string[position] operation = self.new(*string[position+1, p_size + l_size].unpack("" << p_pack << l_pack)) position += p_size + l_size + 1 return position, operation end end nil end |
Instance Method Details
#pack ⇒ Object
Create a binary representation in gdiff format.
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/gdiff.rb', line 110 def pack result = "" l = @length p = @position while l > 0 raise "Can't express position" if p > LONG[1] matched = TYPES.inject(nil) { | r, (c, (p_pack, p_max), (l_pack, l_max)) | next r if r if l <= l_max and p <= p_max result << [c, p, l].pack("C" << p_pack << l_pack) p += l l = 0 end } unless matched result << [255, p, INT[1]].pack("C" << LONG[0] << INT[0]) p += INT[1] l -= INT[1] end end result end |
#to_s ⇒ Object
Return a readable representation of the operation
150 151 152 |
# File 'lib/gdiff.rb', line 150 def to_s "Copy: #{position}, #{length}" end |