Sha256: cf845bb1b42a880e158f081e4cd0305b89cdf9a55911dbfded21d3e1946008d0

Contents?: true

Size: 695 Bytes

Versions: 1

Compression:

Stored size: 695 Bytes

Contents

module Bychar
  class ReaderIOBuf
    
    def initialize(with_io, buffer_size = DEFAULT_BUFFER_SIZE)
      @io = with_io
      @bufsize = buffer_size
      cache
    end
    
    # Since you parse char by char, you will be tempted to do it in a tight loop
    # and to call eof? on each iteration. Don't. Instead. allow it to raise and do not check.
    # This takes the profile time down from 36 seconds to 30 seconds for a large file.
    def read_one_char
      cache if @buf.eos?
      return nil if @buf.eos?
      
      @buf.getch
    end
    
    private
    
    def cache
      data = @io.read(@bufsize)
      @buf = StringScanner.new(data.to_s) # Make nil become ""
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bychar-3.0.0 lib/impls/reader_iobuf.rb