Class: EncodeM::Decoder

Inherits:
Object
  • Object
show all
Defined in:
lib/encode_m/decoder.rb

Constant Summary collapse

POS_DECODE =
Encoder::POS_CODE.each_with_index.map { |v, i| [v, i] }.to_h.freeze
NEG_DECODE =
Encoder::NEG_CODE.each_with_index.map { |v, i| [v, i] }.to_h.freeze

Class Method Summary collapse

Class Method Details

.decode(encoded_bytes) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/encode_m/decoder.rb', line 7

def self.decode(encoded_bytes)
  bytes = encoded_bytes.unpack('C*')
  
  # Check for string prefix
  if bytes[0] == Encoder::STR_SUB_PREFIX
    decode_string(bytes)
  elsif bytes[0] == Encoder::SUBSCRIPT_ZERO
    0
  else
    decode_numeric(bytes)
  end
end

.decode_composite(encoded_bytes) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/encode_m/decoder.rb', line 20

def self.decode_composite(encoded_bytes)
  components = []
  bytes = encoded_bytes.unpack('C*')
  current = []
  
  bytes.each do |byte|
    if byte == Encoder::KEY_DELIMITER
      # End of component
      unless current.empty?
        components << decode(current.pack('C*'))
        current = []
      end
    else
      current << byte
    end
  end
  
  # Don't forget the last component
  components << decode(current.pack('C*')) unless current.empty?
  
  components
end