Class: CsvReader::Buffer

Inherits:
Object
  • Object
show all
Defined in:
lib/csvreader/buffer.rb

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Buffer

todo: find a better name:

BufferedReader
BufferedInput
BufferI
  • why? why not? is really just for reading (keep io?)



10
11
12
13
14
# 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

Instance Method Details

#eof?Boolean

Returns:

  • (Boolean)


16
# File 'lib/csvreader/buffer.rb', line 16

def eof?()   @buf.size == 0 && @io.eof?;  end

#getcObject



18
19
20
21
22
23
24
# 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

#peek1Object Also known as: peek



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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

#peekn(lookahead) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# 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