Class: Protobuf::Field::VarintField
- Defined in:
- lib/protobuf/field/varint_field.rb
Direct Known Subclasses
BoolField, IntegerField, SignedIntegerField, Uint32Field, Uint64Field
Constant Summary collapse
- CACHE_LIMIT =
Constants
2048
- INT32_MAX =
2**31 - 1
- INT32_MIN =
-2**31
- INT64_MAX =
2**63 - 1
- INT64_MIN =
-2**63
- UINT32_MAX =
2**32 - 1
- UINT64_MAX =
2**64 - 1
Constants inherited from BaseField
Instance Attribute Summary
Attributes inherited from BaseField
#fully_qualified_name, #message_class, #name, #options, #rule, #tag, #type_class
Class Method Summary collapse
-
.cached_varint(value) ⇒ Object
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.
-
.default ⇒ Object
Class Methods.
- .encode(value, use_cache = true) ⇒ Object
Instance Method Summary collapse
-
#acceptable?(val) ⇒ Boolean
Public Instance Methods.
- #coerce!(val) ⇒ Object
- #decode(value) ⇒ Object
- #encode(value) ⇒ Object
- #wire_type ⇒ Object
Methods inherited from BaseField
#default, #default_value, #deprecated?, #enum?, #extension?, #fully_qualified_name_only!, #initialize, #message?, #optional?, #packed?, #repeated?, #repeated_message?, #required?, #set, #tag_encoded, #to_s
Methods included from Logging
initialize_logger, #log_exception, #log_signature, #logger, #sign_message
Constructor Details
This class inherits a constructor from Protobuf::Field::BaseField
Class Method Details
.cached_varint(value) ⇒ Object
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
29 30 31 32 |
# 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 ⇒ Object
Class Methods
23 24 25 |
# File 'lib/protobuf/field/varint_field.rb', line 23 def self.default 0 end |
.encode(value, use_cache = true) ⇒ Object
34 35 36 37 38 39 40 41 42 43 |
# 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 |
Instance Method Details
#acceptable?(val) ⇒ Boolean
Public Instance Methods
53 54 55 56 57 58 59 60 61 62 63 64 |
# 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) ⇒ Object
66 67 68 69 |
# 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) ⇒ Object
71 72 73 |
# File 'lib/protobuf/field/varint_field.rb', line 71 def decode(value) value end |
#encode(value) ⇒ Object
75 76 77 |
# File 'lib/protobuf/field/varint_field.rb', line 75 def encode(value) ::Protobuf::Field::VarintField.encode(value) end |