Sha256: 46b57508a473b5e61586dc0b07043d14c7415559c7c4fecaad9178b3bc959396

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?
      raise EOF 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-2.0.0 lib/impls/reader_iobuf.rb