Module: Y::Lib0::Decoding

Defined in:
lib/y/lib0/decoding.rb

Defined Under Namespace

Classes: Decoder

Class Method Summary collapse

Class Method Details

.clone(decoder, new_pos = decoder.pos) ⇒ Object



23
24
25
26
27
# File 'lib/y/lib0/decoding.rb', line 23

def self.clone(decoder, new_pos = decoder.pos)
  decoder = create_decoder(decoder.arr)
  decoder.pos = new_pos
  decoder
end

.create_decoder(uint8_array) ⇒ Object



15
16
17
# File 'lib/y/lib0/decoding.rb', line 15

def self.create_decoder(uint8_array)
  Decoder.new(uint8_array)
end

.decode_base64_to_uint8_array(str) ⇒ Object



54
55
56
# File 'lib/y/lib0/decoding.rb', line 54

def self.decode_base64_to_uint8_array(str)
  Base64.strict_decode64(str).unpack("C*")
end

.has_content(decoder) ⇒ Object



19
20
21
# File 'lib/y/lib0/decoding.rb', line 19

def self.has_content(decoder)
  decoder.pos != decoder.arr.size
end

.read_uint8_array(decoder, size) ⇒ Object



29
30
31
32
33
# File 'lib/y/lib0/decoding.rb', line 29

def self.read_uint8_array(decoder, size)
  view = Buffer.create_uint8_array_view_from_buffer(decoder.arr, decoder.pos + 0, size)
  decoder.pos += size
  view
end

.read_var_uint(decoder) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/y/lib0/decoding.rb', line 39

def self.read_var_uint(decoder)
  num = 0
  mult = 1
  size = decoder.arr.size
  while decoder.pos < size
    r = decoder.arr[decoder.pos]
    decoder.pos += 1
    num += ((r & Binary::BITS7) * mult)
    mult *= 128 # next iteration, shift 7 "more" to the left
    return num if r < Binary::BIT8
    raise "integer out of range" if num > Integer::MAX
  end
  raise "unexpected end of array"
end

.read_var_uint8_array(decoder) ⇒ Object



35
36
37
# File 'lib/y/lib0/decoding.rb', line 35

def self.read_var_uint8_array(decoder)
  read_uint8_array(decoder, read_var_uint(decoder))
end