Class: RadixEncoding::Encoding

Inherits:
Object
  • Object
show all
Includes:
Bits, Chunks, EncodedPoints, Validations
Defined in:
lib/radix_encoding/encoding.rb,
lib/radix_encoding/encoding/bits.rb,
lib/radix_encoding/encoding/chunks.rb,
lib/radix_encoding/encoding/errors.rb,
lib/radix_encoding/encoding/validations.rb,
lib/radix_encoding/encoding/encoded_points.rb

Defined Under Namespace

Modules: Bits, Chunks, EncodedPoints, Validations Classes: AlphabetTooShortError, DataTypeNotSupportedError

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Validations

#validate!, #validate_alphabet_length!

Methods included from EncodedPoints

#bits_from_encoded_points, #clean_encoded_points, #count_encoded_points_in, #encoded_points_for, #pad_encoded_points, #unpad_encoded_points

Methods included from Chunks

#chunks_total_bitsize_for, #count_chunks_in

Methods included from Bits

#bits_for

Constructor Details

#initialize(alphabet:, radix:, padding: nil, prefix: nil) ⇒ Encoding

Returns a new instance of Encoding.



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

def initialize(alphabet:, radix:, padding: nil, prefix: nil)
  @alphabet = alphabet
  @padding = padding
  @prefix = prefix
  @radix = radix

  validate!
end

Instance Attribute Details

#alphabetObject (readonly)

Returns the value of attribute alphabet.



18
19
20
# File 'lib/radix_encoding/encoding.rb', line 18

def alphabet
  @alphabet
end

#paddingObject (readonly)

Returns the value of attribute padding.



18
19
20
# File 'lib/radix_encoding/encoding.rb', line 18

def padding
  @padding
end

#prefixObject (readonly)

Returns the value of attribute prefix.



18
19
20
# File 'lib/radix_encoding/encoding.rb', line 18

def prefix
  @prefix
end

#radixObject (readonly)

Returns the value of attribute radix.



18
19
20
# File 'lib/radix_encoding/encoding.rb', line 18

def radix
  @radix
end

Instance Method Details

#bytes_for(bits) ⇒ Object



24
25
26
27
28
# File 'lib/radix_encoding/encoding/bits.rb', line 24

def bytes_for(bits)
  bits.each_slice(8).map do |byte_bits|
    byte_bits.join.to_i(2)
  end
end

#decode(data) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/radix_encoding/encoding.rb', line 29

def decode(data)
  if prefix.present? && data.start_with?(prefix)
    data = data[prefix.size..-1]
  end

  decoded = data.chars
  decoded = unpad_encoded_points(decoded)
  decoded = bits_from_encoded_points(decoded)
  decoded = unpad_bits(decoded)
  decoded = bytes_for(decoded)
  decoded = decoded.pack("c*")
  decoded
end

#encode(data) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/radix_encoding/encoding.rb', line 43

def encode(data)
  case data
  when String
    encode_bytes(data.bytes)
  else
    raise DataTypeNotSupportedError.new(data_type: data.class)
  end
end

#pad_bits(bits, padded_bitsize) ⇒ Object



30
31
32
# File 'lib/radix_encoding/encoding/bits.rb', line 30

def pad_bits(bits, padded_bitsize)
  Subvisual::ArrayUtils.pad_right(bits, padded_bitsize, "0")
end

#unpad_bits(bits) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/radix_encoding/encoding/bits.rb', line 34

def unpad_bits(bits)
  excess_bits = bits.size % 8

  return bits if excess_bits.zero?

  bits[0...-excess_bits]
end