Module: Y::Lib0::Encoding

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

Defined Under Namespace

Classes: Encoder

Class Method Summary collapse

Class Method Details

.create_encoderObject



16
17
18
# File 'lib/y/lib0/encoding.rb', line 16

def self.create_encoder
  Encoder.new
end

.encode_uint8_array_to_base64(arr) ⇒ Object



137
138
139
# File 'lib/y/lib0/encoding.rb', line 137

def self.encode_uint8_array_to_base64(arr)
  Base64.strict_encode64(arr.pack("C*"))
end

.length(encoder) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/y/lib0/encoding.rb', line 20

def self.length(encoder)
  size = encoder.cpos
  i = 0
  while i < encoder.bufs.size
    size += encoder.bufs[i].size
    i += 1
  end
  size
end

.set(encoder, pos, num) ⇒ Object

rubocop:disable Metrics/MethodLength



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/y/lib0/encoding.rb', line 73

def self.set(encoder, pos, num) # rubocop:disable Metrics/MethodLength
  buffer = nil
  i = 0
  while i < encoder.bufs.size && buffer.nil?
    b = encoder.bufs[i]
    if pos < b.size
      buffer = b
    else
      pos -= b.size
    end

    i += 1
  end

  buffer = encoder.cbuf if buffer.nil?
  buffer[pos] = num
end

.set_uint8(encoder, pos, num) ⇒ Object



95
96
97
# File 'lib/y/lib0/encoding.rb', line 95

def self.set_uint8(encoder, pos, num)
  set(encoder, pos, num)
end

.to_uint8_array(encoder) ⇒ Object

rubocop:disable Metrics/MethodLength



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/y/lib0/encoding.rb', line 30

def self.to_uint8_array(encoder) # rubocop:disable Metrics/MethodLength
  typed_arr = TypedArray.new(length(encoder))
  cur_pos = 0
  i = 0
  while i < encoder.bufs.size
    d = encoder.bufs[i]
    typed_arr.replace_with(d, cur_pos)
    cur_pos += d.size
    i += 1
  end
  typed_arr.replace_with(
    Buffer.create_uint8_array_view_from_buffer(
      encoder.cbuf,
      0,
      encoder.cpos
    ),
    cur_pos
  )
  typed_arr
end

.unsigned_right_shift(value, amount) ⇒ Object



132
133
134
135
# File 'lib/y/lib0/encoding.rb', line 132

def self.unsigned_right_shift(value, amount)
  mask = (1 << (32 - amount)) - 1
  (value >> amount) & mask
end

.verify_size(encoder, size) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/y/lib0/encoding.rb', line 51

def self.verify_size(encoder, size)
  buffer_size = encoder.cbuf.size

  return unless buffer_size - encoder.cpos < size

  encoder.bufs << Buffer.create_uint8_array_view_from_buffer(encoder.cbuf, 0, encoder.cpos)
  encoder.cbuf = TypedArray.new([buffer_size, size].max * 2)
  encoder.cpos = 0
end

.write(encoder, num) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/y/lib0/encoding.rb', line 61

def self.write(encoder, num)
  buffer_size = encoder.cbuf.size
  if encoder.cpos == buffer_size
    encoder.bufs << encoder.cbuf
    encoder.cbuf = TypedArray.new(buffer_size * 2)
    encoder.cpos = 0
  end

  encoder.cbuf[encoder.cpos] = num
  encoder.cpos += 1
end

.write_uint8(encoder, num) ⇒ Object



91
92
93
# File 'lib/y/lib0/encoding.rb', line 91

def self.write_uint8(encoder, num)
  write(encoder, num)
end

.write_uint8_array(encoder, uint8_array) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/MethodLength



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/y/lib0/encoding.rb', line 107

def self.write_uint8_array(encoder, uint8_array) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  buffer_size = encoder.cbuf.size
  cpos = encoder.cpos
  left_copy_size = [buffer_size - cpos, uint8_array.size].min
  right_copy_size = uint8_array.size - left_copy_size
  encoder.cbuf.replace_with(uint8_array.slice(0, left_copy_size), cpos)
  encoder.cpos += left_copy_size

  return unless right_copy_size.positive?

  # Still something to write, write right half..
  # Append new buffer
  encoder.bufs.push(encoder.cbuf)
  # must have at least size of remaining buffer
  encoder.cbuf = TypedArray.new([buffer_size * 2, right_copy_size].max)
  # copy array
  encoder.cbuf.replace_with(uint8_array[left_copy_size..])
  encoder.cpos = right_copy_size
end

.write_var_uint(encoder, num) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/y/lib0/encoding.rb', line 99

def self.write_var_uint(encoder, num)
  while num > Binary::BITS7
    write(encoder, Binary::BIT8 | (Binary::BITS7 & num))
    num = (num / 128.0).floor # shift >>> 7
  end
  write(encoder, Binary::BITS7 & num)
end

.write_var_uint8_array(encoder, uint8_array) ⇒ Object



127
128
129
130
# File 'lib/y/lib0/encoding.rb', line 127

def self.write_var_uint8_array(encoder, uint8_array)
  write_var_uint(encoder, uint8_array.size)
  write_uint8_array(encoder, uint8_array)
end