class CsvReader::Buffer

Public Class Methods

new( data ) click to toggle source
BufferedReader
BufferedInput
BufferI
  • why? why not? is really just for reading (keep io?)

# File lib/csvreader/buffer.rb, line 10
def initialize( data )
  # create the IO object we will read from
  @io = data.is_a?(String) ? StringIO.new(data) : data
  @buf = [] ## last (buffer) chars (used for peek)
end

Public Instance Methods

eof?() click to toggle source
# File lib/csvreader/buffer.rb, line 16
def eof?()   @buf.size == 0 && @io.eof?;  end
getc() click to toggle source
# File lib/csvreader/buffer.rb, line 18
def getc
  if @buf.size > 0
    @buf.shift  ## get first char from buffer
  else
    @io.getc
  end
end
peek()
Alias for: peek1
peek1() click to toggle source
# File lib/csvreader/buffer.rb, line 48
def peek1
  if @buf.size == 0 && @io.eof?
    ## puts "peek - hitting eof!!!"
    return  "\0"   ## return NUL char (0) for now
  end

  if @buf.size == 0
      c = @io.getc
      @buf.push( c )
      ## puts "peek - fill buffer >#{c}< (#{c.ord})"
  end

  @buf[0]    ## @buf.first
end
Also aliased as: peek
peekn( lookahead ) click to toggle source
# File lib/csvreader/buffer.rb, line 27
def peekn( lookahead )
  ## todo/check:  use a new method peekstr or match or something
  ##    for more than
    if @buf.size == 0 && @io.eof?
      ## puts "peek - hitting eof!!!"
      return  "\0"   ## return NUL char (0) for now
    end

    while @buf.size < lookahead do
       ## todo/check: add/append NUL char (0) - why? why not?
       break if @io.eof?    ## nothing more to read; break out of filling up buffer

       c = @io.getc
       @buf.push( c )
       ## puts "peek - fill buffer >#{c}< (#{c.ord})"
    end

    @buf[0,lookahead].join
end