Module: ZSTDS::Stream::ReaderHelpers

Included in:
Reader
Defined in:
lib/zstds/stream/reader_helpers.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



189
190
191
# File 'lib/zstds/stream/reader_helpers.rb', line 189

def self.included(klass)
  klass.extend ClassMethods
end

Instance Method Details

#each_byte(&block) ⇒ Object



15
16
17
# File 'lib/zstds/stream/reader_helpers.rb', line 15

def each_byte(&block)
  each_string method(:getbyte), &block
end

#each_char(&block) ⇒ Object



61
62
63
# File 'lib/zstds/stream/reader_helpers.rb', line 61

def each_char(&block)
  each_string method(:getc), &block
end

#each_line(&block) ⇒ Object Also known as: each



122
123
124
# File 'lib/zstds/stream/reader_helpers.rb', line 122

def each_line(&block)
  each_string method(:gets), &block
end

#getbyteObject



11
12
13
# File 'lib/zstds/stream/reader_helpers.rb', line 11

def getbyte
  read 1
end

#getcObject

– char –



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/zstds/stream/reader_helpers.rb', line 33

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

– lines –



71
72
73
74
75
76
77
78
79
80
81
82
83
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
# File 'lib/zstds/stream/reader_helpers.rb', line 71

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

#readbyteObject



19
20
21
# File 'lib/zstds/stream/reader_helpers.rb', line 19

def readbyte
  readstring method(:getbyte)
end

#readcharObject



57
58
59
# File 'lib/zstds/stream/reader_helpers.rb', line 57

def readchar
  readstring method(:getc)
end

#readlineObject



111
112
113
# File 'lib/zstds/stream/reader_helpers.rb', line 111

def readline
  readstring method(:gets)
end

#readlinesObject



115
116
117
118
119
120
# File 'lib/zstds/stream/reader_helpers.rb', line 115

def readlines
  lines = []
  each_line { |line| lines << line }

  lines
end

#ungetbyte(byte) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/zstds/stream/reader_helpers.rb', line 23

def ungetbyte(byte)
  Validation.validate_string byte

  @buffer.prepend byte

  nil
end

#ungetc(char) ⇒ Object



65
66
67
# File 'lib/zstds/stream/reader_helpers.rb', line 65

def ungetc(char)
  ungetstring char
end

#ungetline(line) ⇒ Object



128
129
130
131
132
133
134
# File 'lib/zstds/stream/reader_helpers.rb', line 128

def ungetline(line)
  ungetstring line

  @lineno -= 1

  nil
end