Module: ADSP::Stream::ReaderHelpers
- Included in:
- Reader
- Defined in:
- lib/adsp/stream/reader_helpers.rb
Overview
ADSP::Stream::ReaderHelpers module.
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
-
.included(klass) ⇒ Object
Extends target
klasswith additional class methods.
Instance Method Summary collapse
-
#each_byte(&block) ⇒ Object
Yields each byte.
-
#each_char(&block) ⇒ Object
Yields each char.
-
#each_line(&block) ⇒ Object
(also: #each)
Yields each line.
-
#getbyte ⇒ Object
Returns next byte.
-
#getc ⇒ Object
Returns next char.
-
#gets(separator = $OUTPUT_RECORD_SEPARATOR, limit = nil) ⇒ Object
Returns next line by
separator. -
#readbyte ⇒ Object
Returns next byte.
-
#readchar ⇒ Object
Returns next char.
-
#readline ⇒ Object
Returns next line.
-
#readlines ⇒ Object
Returns all available lines.
-
#ungetbyte(byte) ⇒ Object
Pushes back
byte. -
#ungetc(char) ⇒ Object
Pushes back
char. -
#ungetline(line) ⇒ Object
Pushes back
line.
Class Method Details
.included(klass) ⇒ Object
Extends target klass with additional class methods.
214 215 216 |
# File 'lib/adsp/stream/reader_helpers.rb', line 214 def self.included(klass) klass.extend ClassMethods end |
Instance Method Details
#each_byte(&block) ⇒ Object
Yields each byte.
18 19 20 |
# File 'lib/adsp/stream/reader_helpers.rb', line 18 def each_byte(&block) each_string method(:getbyte), &block end |
#each_char(&block) ⇒ Object
Yields each char.
71 72 73 |
# File 'lib/adsp/stream/reader_helpers.rb', line 71 def each_char(&block) each_string method(:getc), &block end |
#each_line(&block) ⇒ Object Also known as: each
Yields each line.
139 140 141 |
# File 'lib/adsp/stream/reader_helpers.rb', line 139 def each_line(&block) each_string method(:gets), &block end |
#getbyte ⇒ Object
Returns next byte.
13 14 15 |
# File 'lib/adsp/stream/reader_helpers.rb', line 13 def getbyte read 1 end |
#getc ⇒ Object
Returns next char.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# 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) ⇒ Object
Returns next line by separator. Line length is limited by limit.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# 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 ⇒ Object
Returns next byte. Raises ::EOFError when no data available.
24 25 26 |
# File 'lib/adsp/stream/reader_helpers.rb', line 24 def readbyte readstring method(:getbyte) end |
#readchar ⇒ Object
Returns next char. Raises ::EOFError when no data available.
66 67 68 |
# File 'lib/adsp/stream/reader_helpers.rb', line 66 def readchar readstring method(:getc) end |
#readline ⇒ Object
Returns next line. Raises ::EOFError when no data available.
126 127 128 |
# File 'lib/adsp/stream/reader_helpers.rb', line 126 def readline readstring method(:gets) end |
#readlines ⇒ Object
Returns all available lines.
131 132 133 134 135 136 |
# File 'lib/adsp/stream/reader_helpers.rb', line 131 def readlines lines = [] each_line { |line| lines << line } lines end |
#ungetbyte(byte) ⇒ Object
Pushes back byte.
29 30 31 32 33 34 35 |
# File 'lib/adsp/stream/reader_helpers.rb', line 29 def ungetbyte(byte) Validation.validate_string byte @buffer.prepend byte nil end |
#ungetc(char) ⇒ Object
Pushes back char.
76 77 78 |
# File 'lib/adsp/stream/reader_helpers.rb', line 76 def ungetc(char) ungetstring char end |
#ungetline(line) ⇒ Object
Pushes back line.
146 147 148 149 150 151 152 |
# File 'lib/adsp/stream/reader_helpers.rb', line 146 def ungetline(line) ungetstring line @lineno -= 1 nil end |