Sha256: ba7c59a52536e7364218907fc0e5d5fdb6450c4eebee5a2a61e49745a85dbb0c

Contents?: true

Size: 690 Bytes

Versions: 29

Compression:

Stored size: 690 Bytes

Contents

module Protobuf
  module VarintPure
    CACHE_LIMIT = 2048

    def cached_varint(value)
      @_varint_cache ||= {}
      (@_varint_cache[value] ||= encode(value, false)).dup
    end

    def decode(stream)
      value = index = 0
      begin
        byte = stream.readbyte
        value |= (byte & 0x7f) << (7 * index)
        index += 1
      end while (byte & 0x80).nonzero?
      value
    end

    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
  end
end

Version data entries

29 entries across 29 versions & 3 rubygems

Version Path
protobuf-3.10.9 lib/protobuf/varint_pure.rb
protobuf-3.10.8 lib/protobuf/varint_pure.rb
contrast-agent-6.7.0 lib/protobuf/varint_pure.rb
protobuf-3.10.7 lib/protobuf/varint_pure.rb
contrast-agent-6.6.5 lib/protobuf/varint_pure.rb
contrast-agent-6.6.4 lib/protobuf/varint_pure.rb
contrast-agent-6.6.3 lib/protobuf/varint_pure.rb
contrast-agent-6.6.2 lib/protobuf/varint_pure.rb
contrast-agent-6.6.1 lib/protobuf/varint_pure.rb
contrast-agent-6.6.0 lib/protobuf/varint_pure.rb
contrast-agent-6.5.1 lib/protobuf/varint_pure.rb
contrast-agent-6.5.0 lib/protobuf/varint_pure.rb
contrast-agent-6.4.0 lib/protobuf/varint_pure.rb
protobuf-3.10.6 lib/protobuf/varint_pure.rb
protobuf-3.10.5 lib/protobuf/varint_pure.rb
protobuf-3.10.4 lib/protobuf/varint_pure.rb
protobuf-cucumber-3.10.8 lib/protobuf/varint_pure.rb
protobuf-cucumber-3.10.7 lib/protobuf/varint_pure.rb
protobuf-cucumber-3.10.6 lib/protobuf/varint_pure.rb
protobuf-cucumber-3.10.5 lib/protobuf/varint_pure.rb