Class: Canoser::Cursor

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

Instance Method Summary collapse

Constructor Details

#initialize(bytes, offset = 0) ⇒ Cursor

Returns a new instance of Cursor.



3
4
5
6
# File 'lib/canoser/cursor.rb', line 3

def initialize(bytes, offset=0)
	@bytes = bytes
	@offset = offset
end

Instance Method Details

#finished?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/canoser/cursor.rb', line 20

def finished?
	@offset == @bytes.size
end

#peek_bytes(size) ⇒ Object

Raises:



15
16
17
18
# File 'lib/canoser/cursor.rb', line 15

def peek_bytes(size)
  raise ParseError.new("#{@offset+size} exceed bytes size:#{@bytes.size}") if @offset+size > @bytes.size
  @bytes[@offset, size]
end

#read_bytes(size) ⇒ Object

Raises:



8
9
10
11
12
13
# File 'lib/canoser/cursor.rb', line 8

def read_bytes(size)
	raise ParseError.new("#{@offset+size} exceed bytes size:#{@bytes.size}") if @offset+size > @bytes.size
	ret = @bytes[@offset, size]
	@offset += size
	ret
end