Class: Hokusai::Util::PieceTable
- Inherits:
-
Object
- Object
- Hokusai::Util::PieceTable
- Defined in:
- ui/src/hokusai/util/piece_table.rb
Instance Attribute Summary collapse
-
#buffer ⇒ Object
Returns the value of attribute buffer.
-
#buffer_add ⇒ Object
Returns the value of attribute buffer_add.
-
#last_piece_index ⇒ Object
Returns the value of attribute last_piece_index.
-
#pieces ⇒ Object
readonly
Returns the value of attribute pieces.
Instance Method Summary collapse
- #delete(offset, count) ⇒ Object
-
#initialize(buffer = "") ⇒ PieceTable
constructor
A new instance of PieceTable.
- #insert(text, offset = buffer.size - 1) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(buffer = "") ⇒ PieceTable
Returns a new instance of PieceTable.
8 9 10 11 12 13 |
# File 'ui/src/hokusai/util/piece_table.rb', line 8 def initialize(buffer = "") @pieces = [[:original, 0, buffer.size]] @buffer_add = "" @buffer = buffer @last_piece_index = nil end |
Instance Attribute Details
#buffer ⇒ Object
Returns the value of attribute buffer.
5 6 7 |
# File 'ui/src/hokusai/util/piece_table.rb', line 5 def buffer @buffer end |
#buffer_add ⇒ Object
Returns the value of attribute buffer_add.
5 6 7 |
# File 'ui/src/hokusai/util/piece_table.rb', line 5 def buffer_add @buffer_add end |
#last_piece_index ⇒ Object
Returns the value of attribute last_piece_index.
5 6 7 |
# File 'ui/src/hokusai/util/piece_table.rb', line 5 def last_piece_index @last_piece_index end |
#pieces ⇒ Object (readonly)
Returns the value of attribute pieces.
6 7 8 |
# File 'ui/src/hokusai/util/piece_table.rb', line 6 def pieces @pieces end |
Instance Method Details
#delete(offset, count) ⇒ Object
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 |
# File 'ui/src/hokusai/util/piece_table.rb', line 54 def delete(offset, count) piece_at_buffer_offset(offset) do |(piece_left, index_left, remainder_left)| piece_at_buffer_offset(offset + count) do |(piece_right, index_right, remainder_right)| if index_left == index_right if remainder_left == piece_left[1] pieces[index_left] = [piece_left[0], piece_left[1] + count, piece_left[2] - count] return elsif remainder_right == piece_left[1] + piece_left[2] pieces[index_left] = [piece_left[0], piece_left[1], piece_left[2] - count] return end end new_pieces = [] left = [piece_left[0], piece_left[1], remainder_left - piece_left[1]] left_condition = (remainder_left - piece_left[1] > 0) right = [piece_right[0], remainder_right, piece_right[2] - (remainder_right - piece_right[1])] right_condition = (piece_right[2] - (remainder_right - piece_right[1]) > 0) if !left_condition && !right_condition new_pieces << left end if left_condition new_pieces << left end if right_condition new_pieces << right end self.pieces[index_left..index_right] = new_pieces self.last_piece_index = nil end end end |
#insert(text, offset = buffer.size - 1) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'ui/src/hokusai/util/piece_table.rb', line 36 def insert(text, offset = buffer.size - 1) return nil if text.size.zero? piece_at_buffer_offset(offset) do |(piece, index, remainder)| which, start, size = piece length = remainder - start new_pieces = [] new_pieces << [which, start, length] if length > 0 new_pieces << [:add, buffer_add.size, text.size] new_pieces << [which, length + start, size - length] if size - length > 0 self.last_piece_index = index + 1 self.pieces[index..index] = new_pieces self.buffer_add += text end end |
#to_s ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'ui/src/hokusai/util/piece_table.rb', line 15 def to_s io = StringIO.open do |io| pieces.each do |(which, start, size)| case which when :original io.write buffer[start, size] else if buffer_add[start, size].nil? raise Hokusai::Error.new("#{which} Bad: #{start} #{size}") end io.write buffer_add[start, size] end end io.string end io end |