Class: Arcana::Cursor
- Inherits:
-
Object
- Object
- Arcana::Cursor
- Defined in:
- lib/arcana.rb
Instance Attribute Summary collapse
-
#buf ⇒ Object
readonly
Returns the value of attribute buf.
-
#offset ⇒ Object
readonly
Returns the value of attribute offset.
Instance Method Summary collapse
- #eof? ⇒ Boolean
-
#initialize(buf) ⇒ Cursor
constructor
A new instance of Cursor.
- #inspect ⇒ Object
- #mark_base ⇒ Object
- #peek(n) ⇒ Object
- #read(n) ⇒ Object
- #restore ⇒ Object
- #seek_absolute(offset) ⇒ Object
- #seek_pos(offset) ⇒ Object
- #seek_relative(offset) ⇒ Object
Constructor Details
#initialize(buf) ⇒ Cursor
Returns a new instance of Cursor.
7 8 9 10 |
# File 'lib/arcana.rb', line 7 def initialize(buf) @buf = buf @base = @offset = 0 end |
Instance Attribute Details
#buf ⇒ Object (readonly)
Returns the value of attribute buf.
5 6 7 |
# File 'lib/arcana.rb', line 5 def buf @buf end |
#offset ⇒ Object (readonly)
Returns the value of attribute offset.
5 6 7 |
# File 'lib/arcana.rb', line 5 def offset @offset end |
Instance Method Details
#eof? ⇒ Boolean
12 13 14 |
# File 'lib/arcana.rb', line 12 def eof? @offset >= @buf.size end |
#inspect ⇒ Object
54 55 56 |
# File 'lib/arcana.rb', line 54 def inspect "#<#{self.class} offset=#{@offset}>" end |
#mark_base ⇒ Object
26 27 28 29 |
# File 'lib/arcana.rb', line 26 def mark_base @base += @offset @offset = 0 end |
#peek(n) ⇒ Object
22 23 24 |
# File 'lib/arcana.rb', line 22 def peek(n) @buf[@offset, n] end |
#read(n) ⇒ Object
16 17 18 19 20 |
# File 'lib/arcana.rb', line 16 def read(n) ret = peek(n) seek_relative(n) ret end |
#restore ⇒ Object
47 48 49 50 51 52 |
# File 'lib/arcana.rb', line 47 def restore prev = @offset, @base yield ensure @offset, @base = prev end |
#seek_absolute(offset) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/arcana.rb', line 31 def seek_absolute(offset) if offset < 0 @offset = @buf.size + offset else @offset = offset end end |
#seek_pos(offset) ⇒ Object
39 40 41 |
# File 'lib/arcana.rb', line 39 def seek_pos(offset) seek_absolute(@base + offset) end |
#seek_relative(offset) ⇒ Object
43 44 45 |
# File 'lib/arcana.rb', line 43 def seek_relative(offset) @offset += offset end |