Class: Gqldb::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/tina4_ultipa/pb.rb

Overview

---- wire reader ----------------------------------------------------------

Instance Method Summary collapse

Constructor Details

#initialize(bytes) ⇒ Reader

Returns a new instance of Reader.



123
124
125
126
127
# File 'lib/tina4_ultipa/pb.rb', line 123

def initialize(bytes)
  @b = bytes.dup.force_encoding("BINARY")
  @p = 0
  @end = @b.bytesize
end

Instance Method Details

#eof?Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/tina4_ultipa/pb.rb', line 129

def eof?
  @p >= @end
end

#read_lenObject



151
152
153
154
155
156
# File 'lib/tina4_ultipa/pb.rb', line 151

def read_len
  n = read_varint
  s = @b.byteslice(@p, n) || ""
  @p += n
  s.dup.force_encoding("BINARY")
end

#read_signed_varintObject



145
146
147
148
149
# File 'lib/tina4_ultipa/pb.rb', line 145

def read_signed_varint
  v = read_varint
  v -= (1 << 64) if v >= (1 << 63)
  v
end

#read_stringObject



158
159
160
# File 'lib/tina4_ultipa/pb.rb', line 158

def read_string
  read_len.force_encoding("UTF-8")
end

#read_varintObject



133
134
135
136
137
138
139
140
141
142
143
# File 'lib/tina4_ultipa/pb.rb', line 133

def read_varint
  r = 0
  sh = 0
  loop do
    byte = @b.getbyte(@p)
    @p += 1
    r |= (byte & 0x7F) << sh
    return r if (byte & 0x80).zero?
    sh += 7
  end
end

#skip(wire_type) ⇒ Object



162
163
164
165
166
167
168
169
170
# File 'lib/tina4_ultipa/pb.rb', line 162

def skip(wire_type)
  case wire_type
  when 0 then read_varint
  when 2 then @p += read_varint
  when 5 then @p += 4
  when 1 then @p += 8
  else raise ArgumentError, "unknown wire type #{wire_type}"
  end
end