Class: Terminalwire::V2::Server::Rack::Parser::Cursor

Inherits:
Object
  • Object
show all
Defined in:
lib/terminalwire/v2/server/rack.rb

Overview

A non-destructive read cursor over the buffer. Each read either returns the next bytes and advances, or throws :incomplete — meaning a whole frame isn't buffered yet, so we must leave @buf untouched and wait for more. That "consume nothing until the frame is complete" rule is the parser's one sharp edge; the cursor makes it structural instead of a hand-checked guard before every slice.

Instance Method Summary collapse

Constructor Details

#initialize(buf) ⇒ Cursor

Returns a new instance of Cursor.



213
214
215
216
# File 'lib/terminalwire/v2/server/rack.rb', line 213

def initialize(buf)
  @buf = buf
  @off = 0
end

Instance Method Details

#byteObject



218
219
220
221
222
223
# File 'lib/terminalwire/v2/server/rack.rb', line 218

def byte
  throw :incomplete if @off >= @buf.bytesize
  b = @buf.getbyte(@off)
  @off += 1
  b
end

#restObject

The unconsumed tail — what's left after a frame is committed.



233
# File 'lib/terminalwire/v2/server/rack.rb', line 233

def rest = @buf.byteslice(@off..) || "".b

#take(n) ⇒ Object



225
226
227
228
229
230
# File 'lib/terminalwire/v2/server/rack.rb', line 225

def take(n)
  throw :incomplete if @buf.bytesize < @off + n
  slice = @buf.byteslice(@off, n)
  @off += n
  slice
end