Class: CsvReader::BufferIO
- Inherits:
-
Object
- Object
- CsvReader::BufferIO
- Defined in:
- lib/csvreader/buffer.rb
Overview
todo: find a better name - why? why not? is really just for reading (keep io?)
Instance Method Summary collapse
- #eof? ⇒ Boolean
- #getc ⇒ Object
-
#initialize(data) ⇒ BufferIO
constructor
A new instance of BufferIO.
- #peek ⇒ Object
- #ungetc(c) ⇒ Object
Constructor Details
#initialize(data) ⇒ BufferIO
Returns a new instance of BufferIO.
5 6 7 8 9 |
# File 'lib/csvreader/buffer.rb', line 5 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
11 |
# File 'lib/csvreader/buffer.rb', line 11 def eof?() @buf.size == 0 && @io.eof?; end |
#getc ⇒ Object
13 14 15 16 17 18 19 |
# File 'lib/csvreader/buffer.rb', line 13 def getc if @buf.size > 0 @buf.shift ## get first char from buffer else @io.getc end end |
#peek ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/csvreader/buffer.rb', line 30 def peek ## todo/fix: ## use Hexadecimal code: 1A, U+001A for eof char - why? why not? if @buf.size == 0 && @io.eof? puts "peek - hitting eof!!!" ## return eof char(s) - exits? is \0 ?? double check return "\0" end if @buf.size == 0 c = @io.getc @buf.push( c ) ## puts "peek - fill buffer >#{c}< (#{c.ord})" end @buf.first end |
#ungetc(c) ⇒ Object
22 23 24 25 26 27 |
# File 'lib/csvreader/buffer.rb', line 22 def ungetc( c ) ## add upfront as first char in buffer ## last in/first out queue!!!! @buf.unshift( c ) ## puts "ungetc - >#{c} (#{c.ord})< => >#{@buf}<" end |