module ADSP::Stream::ReaderHelpers
ADSP::Stream::ReaderHelpers
module.
Public Class Methods
included(klass)
click to toggle source
Extends target klass
with additional class methods.
# File lib/adsp/stream/reader_helpers.rb, line 214 def self.included(klass) klass.extend ClassMethods end
Public Instance Methods
each_byte(&block)
click to toggle source
Yields each byte.
# File lib/adsp/stream/reader_helpers.rb, line 18 def each_byte(&block) each_string method(:getbyte), &block end
each_char(&block)
click to toggle source
Yields each char.
# File lib/adsp/stream/reader_helpers.rb, line 71 def each_char(&block) each_string method(:getc), &block end
each_line(&block)
click to toggle source
Yields each line.
# File lib/adsp/stream/reader_helpers.rb, line 139 def each_line(&block) each_string method(:gets), &block end
Also aliased as: each
getbyte()
click to toggle source
Returns next byte.
# File lib/adsp/stream/reader_helpers.rb, line 13 def getbyte read 1 end
getc()
click to toggle source
Returns next char.
# File lib/adsp/stream/reader_helpers.rb, line 40 def getc if @external_encoding.nil? byte = getbyte return nil if byte.nil? return transcode_to_internal byte end char = ::String.new :encoding => ::Encoding::BINARY # Read one byte until valid string will appear. loop do byte = getbyte return nil if byte.nil? char << byte char.force_encoding @external_encoding return transcode_to_internal char if char.valid_encoding? char.force_encoding ::Encoding::BINARY end end
gets(separator = $OUTPUT_RECORD_SEPARATOR, limit = nil)
click to toggle source
Returns next line by separator
. Line length is limited by limit
.
# File lib/adsp/stream/reader_helpers.rb, line 84 def gets(separator = $OUTPUT_RECORD_SEPARATOR, limit = nil) # Limit can be a first argument. if separator.is_a? ::Numeric limit = separator separator = $OUTPUT_RECORD_SEPARATOR end line_ending = if separator.nil? nil else Validation.validate_string separator ::String.new separator, :encoding => target_encoding end Validation.validate_positive_integer limit unless limit.nil? line = ::String.new :encoding => target_encoding loop do char = getc if char.nil? return nil if line.empty? break end line << char break if (!line_ending.nil? && line.end_with?(line_ending)) || (!limit.nil? && line.length >= limit) end @lineno += 1 line end
readbyte()
click to toggle source
Returns next byte. Raises ::EOFError
when no data available.
# File lib/adsp/stream/reader_helpers.rb, line 24 def readbyte readstring method(:getbyte) end
readchar()
click to toggle source
Returns next char. Raises ::EOFError
when no data available.
# File lib/adsp/stream/reader_helpers.rb, line 66 def readchar readstring method(:getc) end
readline()
click to toggle source
Returns next line. Raises ::EOFError
when no data available.
# File lib/adsp/stream/reader_helpers.rb, line 126 def readline readstring method(:gets) end
readlines()
click to toggle source
Returns all available lines.
# File lib/adsp/stream/reader_helpers.rb, line 131 def readlines lines = [] each_line { |line| lines << line } lines end
ungetbyte(byte)
click to toggle source
Pushes back byte
.
# File lib/adsp/stream/reader_helpers.rb, line 29 def ungetbyte(byte) Validation.validate_string byte @buffer.prepend byte nil end
ungetc(char)
click to toggle source
Pushes back char
.
# File lib/adsp/stream/reader_helpers.rb, line 76 def ungetc(char) ungetstring char end
ungetline(line)
click to toggle source
Pushes back line
.
# File lib/adsp/stream/reader_helpers.rb, line 146 def ungetline(line) ungetstring line @lineno -= 1 nil end
Protected Instance Methods
each_string(each_proc) { |string| ... }
click to toggle source
Yields each string by each_proc
.
# File lib/adsp/stream/reader_helpers.rb, line 166 def each_string(each_proc, &block) return enum_for __method__, each_proc unless block.is_a? ::Proc loop do string = each_proc.call break if string.nil? yield string end nil end
readstring(each_proc)
click to toggle source
Returns next string by each_proc
. Raises ::EOFError
when no data available.
# File lib/adsp/stream/reader_helpers.rb, line 158 def readstring(each_proc) string = each_proc.call raise ::EOFError if string.nil? string end
ungetstring(string)
click to toggle source
Pushes back string
.
# File lib/adsp/stream/reader_helpers.rb, line 180 def ungetstring(string) Validation.validate_string string string = ::String.new string, :encoding => @internal_encoding unless @internal_encoding.nil? string = transcode_to_external string unless @external_encoding.nil? string.force_encoding ::Encoding::BINARY @buffer.prepend string nil end