Class: Hiredis::Ruby::Reader::Buffer

Inherits:
Object
  • Object
show all
Defined in:
lib/hiredis/ruby/reader.rb

Constant Summary collapse

CRLF =
"\r\n".freeze

Instance Method Summary collapse

Constructor Details

#initializeBuffer

Returns a new instance of Buffer.



116
117
118
119
# File 'lib/hiredis/ruby/reader.rb', line 116

def initialize
  @buffer = ""
  @length = @pos = 0
end

Instance Method Details

#<<(data) ⇒ Object



121
122
123
124
# File 'lib/hiredis/ruby/reader.rb', line 121

def <<(data)
  @length += data.length
  @buffer << data
end

#discard!Object



134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/hiredis/ruby/reader.rb', line 134

def discard!
  if @length == 0
    @buffer = ""
    @length = @pos = 0
  else
    if @pos >= 1024
      @buffer.slice!(0, @pos)
      @length -= @pos
      @pos = 0
    end
  end
end

#empty?Boolean

Returns:

  • (Boolean)


130
131
132
# File 'lib/hiredis/ruby/reader.rb', line 130

def empty?
  @length == 0
end

#lengthObject



126
127
128
# File 'lib/hiredis/ruby/reader.rb', line 126

def length
  @length
end

#read(bytes, skip = 0) ⇒ Object



147
148
149
150
151
152
153
154
# File 'lib/hiredis/ruby/reader.rb', line 147

def read(bytes, skip = 0)
  start = @pos
  stop = start + bytes + skip
  return false if @length < stop

  @pos = stop
  force_encoding @buffer[start, bytes]
end

#read_lineObject



156
157
158
159
160
161
162
163
# File 'lib/hiredis/ruby/reader.rb', line 156

def read_line
  start = @pos
  stop = @buffer.index(CRLF, @pos)
  return false unless stop

  @pos = stop + 2 # include CRLF
  force_encoding @buffer[start, stop - start]
end