Module: Koota::Decode Private

Defined in:
lib/koota/decode.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Various helper decoding methods.

Class Method Summary collapse

Class Method Details

.short(array, offset = 0) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



9
10
11
# File 'lib/koota/decode.rb', line 9

def short(array, offset = 0)
  (array[offset] << 8) | array[offset + 1]
end

.utf8(array, offset = 0) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/koota/decode.rb', line 13

def utf8(array, offset = 0)
  # Determine the length of the UTF-8 sequence using the first byte, as per
  # the table here: https://en.wikipedia.org/wiki/UTF-8#Description
  first = array[offset]
  seq_length = if first <= 0b0111_1111
                 1
               elsif first <= 0b1101_1111
                 2
               elsif first <= 0b1110_1111
                 3
               else
                 4
               end

  # With the length of the UTF-8 sequence determined, we can transform the
  # sequence to a string with #pack interpreting each number as an 8-bit
  # unsigned number and #force_encoding using UTF-8, completing the
  # decoding.
  decoded = array[offset, seq_length].pack('C*').force_encoding('UTF-8')

  # Return also the sequence length so the user knows how long it was.
  [decoded, seq_length]
end