Class: Protobuf::Field::VarintField

Inherits:
BaseField
  • Object
show all
Defined in:
lib/protobuf/message/field.rb

Constant Summary

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

Instance Attribute Summary

Attributes inherited from BaseField

#default, #default_value, #extension, #message_class, #name, #rule, #tag, #type

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from BaseField

descriptor, #descriptor, #initialize, #initialized?, #max, #merge, #min, #optional?, #packed?, #ready?, #repeated?, #required?, #to_s

Constructor Details

This class inherits a constructor from Protobuf::Field::BaseField

Class Method Details

+ (Object) decode(bytes)



353
354
355
356
357
358
359
# File 'lib/protobuf/message/field.rb', line 353

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

+ (Object) default



349
350
351
# File 'lib/protobuf/message/field.rb', line 349

def default
  0
end

+ (Object) encode(value)

Raises:

  • (RangeError)


361
362
363
364
365
366
367
368
369
370
371
# File 'lib/protobuf/message/field.rb', line 361

def encode(value)
  raise RangeError, "#{value} is negative" if value < 0
  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

Instance Method Details

- (Boolean) acceptable?(val)

Returns:

  • (Boolean)

Raises:

  • (TypeError)


386
387
388
389
390
# File 'lib/protobuf/message/field.rb', line 386

def acceptable?(val)
  raise TypeError, val.class.name unless val.is_a?(Integer)
  raise RangeError if val < min || max < val
  true
end

- (Object) decode(value)



378
379
380
# File 'lib/protobuf/message/field.rb', line 378

def decode(value)
  value
end

- (Object) encode(value)



382
383
384
# File 'lib/protobuf/message/field.rb', line 382

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

- (Object) wire_type



374
375
376
# File 'lib/protobuf/message/field.rb', line 374

def wire_type
  WireType::VARINT
end