Module: Protobuf::VarintPure
- Included in:
- Varint
- Defined in:
- lib/protobuf/varint_pure.rb
Constant Summary collapse
- CACHE_LIMIT =
2048
Instance Method Summary collapse
Instance Method Details
#cached_varint(value) ⇒ Object
5 6 7 8 |
# File 'lib/protobuf/varint_pure.rb', line 5 def cached_varint(value) @_varint_cache ||= {} (@_varint_cache[value] ||= encode(value, false)).dup end |
#decode(stream) ⇒ Object
10 11 12 13 14 15 16 17 18 |
# File 'lib/protobuf/varint_pure.rb', line 10 def decode(stream) value = index = 0 begin byte = stream.readbyte value |= (byte & 0x7f) << (7 * index) index += 1 end while (byte & 0x80).nonzero? value end |
#encode(value, use_cache = true) ⇒ Object
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/protobuf/varint_pure.rb', line 20 def encode(value, use_cache = true) return cached_varint(value) if use_cache && value >= 0 && value <= CACHE_LIMIT bytes = [] until value < 128 bytes << (0x80 | (value & 0x7f)) value >>= 7 end (bytes << value).pack('C*') end |