Sha256: f77b0329cd1a0600988f218ce9ff30e8be95929ce0a5da0b10013f0795eb98c4

Contents?: true

Size: 1.1 KB

Versions: 36

Compression:

Stored size: 1.1 KB

Contents

require 'protobuf/field/base_field'

module Protobuf
  module Field
    class VarintField < BaseField
      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

      ##
      # Class Methods
      #
      def self.default
        0
      end

      def self.decode(bytes)
        value = 0
        bytes.each_with_index do |byte, index|
          value |= byte << (7 * index)
        end
        value
      end

      def self.encode(value)
        return [value].pack('C') if value < 128

        bytes = []
        until value == 0
          bytes << (0x80 | (value & 0x7f))
          value >>= 7
        end
        bytes[-1] &= 0x7f
        bytes.pack('C*')
      end

      ##
      # Public Instance Methods
      #
      def acceptable?(val)
        (val > min || val < max) rescue false
      end

      def decode(value)
        value
      end

      def encode(value)
        self.class.encode(value)
      end

      def wire_type
        ::Protobuf::WireType::VARINT
      end

    end
  end
end

Version data entries

36 entries across 36 versions & 1 rubygems

Version Path
protobuf-2.2.3 lib/protobuf/field/varint_field.rb
protobuf-2.2.2 lib/protobuf/field/varint_field.rb
protobuf-2.2.1 lib/protobuf/field/varint_field.rb
protobuf-2.2.0 lib/protobuf/field/varint_field.rb
protobuf-2.1.3 lib/protobuf/field/varint_field.rb
protobuf-2.1.2 lib/protobuf/field/varint_field.rb
protobuf-2.1.1 lib/protobuf/field/varint_field.rb
protobuf-2.1.0 lib/protobuf/field/varint_field.rb
protobuf-2.0.3 lib/protobuf/field/varint_field.rb
protobuf-2.0.2 lib/protobuf/field/varint_field.rb
protobuf-2.0.1 lib/protobuf/field/varint_field.rb
protobuf-2.0.0 lib/protobuf/field/varint_field.rb
protobuf-2.0.0.rc6 lib/protobuf/field/varint_field.rb
protobuf-2.0.0.rc5 lib/protobuf/field/varint_field.rb
protobuf-2.0.0.rc4 lib/protobuf/field/varint_field.rb
protobuf-2.0.0.rc3 lib/protobuf/field/varint_field.rb