Module: Pack::Numbers::VarIntLE

Defined in:
lib/pack/numbers.rb

Class Method Summary collapse

Class Method Details

.read(input, shift) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/pack/numbers.rb', line 19

def self.read(input, shift)
  first = input.readbyte
  value = first & (2 ** shift - 1)

  byte = first

  until byte < 0x80
    byte   = input.readbyte
    value |= (byte & 0x7f) << shift
    shift += 7
  end

  [first, value]
end

.write(value, shift) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/pack/numbers.rb', line 5

def self.write(value, shift)
  bytes = []
  mask  = 2 ** shift - 1

  until value <= mask
    bytes.push(0x80 | value & mask)
    value >>= shift

    mask, shift = 0x7f, 7
  end

  bytes + [value]
end