Class: Gqldb::Reader
- Inherits:
-
Object
- Object
- Gqldb::Reader
- Defined in:
- lib/tina4_ultipa/pb.rb
Overview
---- wire reader ----------------------------------------------------------
Instance Method Summary collapse
- #eof? ⇒ Boolean
-
#initialize(bytes) ⇒ Reader
constructor
A new instance of Reader.
- #read_len ⇒ Object
- #read_signed_varint ⇒ Object
- #read_string ⇒ Object
- #read_varint ⇒ Object
- #skip(wire_type) ⇒ Object
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
129 130 131 |
# File 'lib/tina4_ultipa/pb.rb', line 129 def eof? @p >= @end end |
#read_len ⇒ Object
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_varint ⇒ Object
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_string ⇒ Object
158 159 160 |
# File 'lib/tina4_ultipa/pb.rb', line 158 def read_string read_len.force_encoding("UTF-8") end |
#read_varint ⇒ Object
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 |