Sha256: ed94dbec347209d8ed6d7ac1c06983f3ee559a2b12f04734d0efdc3649b7df05

Contents?: true

Size: 1.28 KB

Versions: 78

Compression:

Stored size: 1.28 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)
        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

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

    end
  end
end

Version data entries

78 entries across 78 versions & 1 rubygems

Version Path
protobuf-2.7.12 lib/protobuf/field/varint_field.rb
protobuf-2.8.0.beta9-java lib/protobuf/field/varint_field.rb
protobuf-2.8.0.beta9 lib/protobuf/field/varint_field.rb
protobuf-2.8.0.beta8-java lib/protobuf/field/varint_field.rb
protobuf-2.8.0.beta8 lib/protobuf/field/varint_field.rb
protobuf-2.8.0.beta6-java lib/protobuf/field/varint_field.rb
protobuf-2.8.0.beta6 lib/protobuf/field/varint_field.rb
protobuf-2.8.0.beta5-java lib/protobuf/field/varint_field.rb
protobuf-2.8.0.beta5 lib/protobuf/field/varint_field.rb
protobuf-2.8.0.beta4-java lib/protobuf/field/varint_field.rb
protobuf-2.8.0.beta4 lib/protobuf/field/varint_field.rb
protobuf-2.8.0.beta3-java lib/protobuf/field/varint_field.rb
protobuf-2.8.0.beta3 lib/protobuf/field/varint_field.rb
protobuf-2.8.0.beta2-java lib/protobuf/field/varint_field.rb
protobuf-2.8.0.beta2 lib/protobuf/field/varint_field.rb
protobuf-2.7.11-java lib/protobuf/field/varint_field.rb
protobuf-2.7.11 lib/protobuf/field/varint_field.rb
protobuf-2.8.0.beta1-java lib/protobuf/field/varint_field.rb
protobuf-2.8.0.beta1 lib/protobuf/field/varint_field.rb
protobuf-2.7.10-java lib/protobuf/field/varint_field.rb