class Protobuf::Field::VarintField

Constants

CACHE_LIMIT

Constants

INT32_MAX
INT32_MIN
INT64_MAX
INT64_MIN
UINT32_MAX
UINT64_MAX

Public Class Methods

cached_varint(value) click to toggle source

Because all tags and enums are calculated as VarInt it is “most common” to have values < CACHE_LIMIT (low numbers) which is defaulting to 1024

# File lib/protobuf/field/varint_field.rb, line 29
def self.cached_varint(value)
  @_varint_cache ||= {}
  (@_varint_cache[value] ||= encode(value, false)).dup
end
default() click to toggle source

Class Methods

# File lib/protobuf/field/varint_field.rb, line 23
def self.default
  0
end
encode(value, use_cache = true) click to toggle source
# File lib/protobuf/field/varint_field.rb, line 34
def self.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

Public Instance Methods

acceptable?(val) click to toggle source

Public Instance Methods

# File lib/protobuf/field/varint_field.rb, line 53
def acceptable?(val)
  int_val = if val.is_a?(Integer)
              return true if val >= 0 && val < INT32_MAX # return quickly for smallest integer size, hot code path
              val
            else
              coerce!(val)
            end

  int_val >= self.class.min && int_val <= self.class.max
rescue
  false
end
coerce!(val) click to toggle source
# File lib/protobuf/field/varint_field.rb, line 66
def coerce!(val)
  return val.to_i if val.is_a?(Numeric)
  Integer(val, 10)
end
decode(value) click to toggle source
# File lib/protobuf/field/varint_field.rb, line 71
def decode(value)
  value
end
encode(value) click to toggle source
# File lib/protobuf/field/varint_field.rb, line 75
def encode(value)
  ::Protobuf::Field::VarintField.encode(value)
end
wire_type() click to toggle source
# File lib/protobuf/field/varint_field.rb, line 79
def wire_type
  ::Protobuf::WireType::VARINT
end