class Bagua::Tri

Provides encoding and decoding functions for trigrams.

Constants

TRI_BASE

Decimal representation of the Unicode code point of the first trigram character. The code point for any trigram can by obtained by adding an offset between 0 and 7 to TRI_BASE.

Public Class Methods

decode(grams) click to toggle source

Decodes a string of trigrams into a UTF-8 string. @note Chomps trailing null characters, as they are the result of zero

padding added when the number of bits being encoded in the trigrams
is not an even multiple of 3.

@todo Add an option to choose the encoding.

# File lib/bagua/tri.rb, line 15
def self.decode(grams)
  return to_bytes(grams).pack('C*').chomp("\000").force_encoding('utf-8')
end
encode(str) click to toggle source

Encodes a string into a string of trigrams representing the byte values of each character in the string.

# File lib/bagua/tri.rb, line 6
def self.encode(str)
  return from_bytes(str.unpack('C*'))
end
from_bytes(bytes) click to toggle source

Returns the trigram representation of the input bytes.

# File lib/bagua/tri.rb, line 20
def self.from_bytes(bytes)
  trios = Bagua::bytes_to_ntets(bytes, 3)
  grams = trios.map { |trio| gram(trio) }
  return grams.join("")
end
to_bytes(grams) click to toggle source

Returns the byte representation of a string of trigrams.

# File lib/bagua/tri.rb, line 27
def self.to_bytes(grams)
  ucodes = grams.unpack('U*')
  trios = ucodes.map { |ucode| ucode - TRI_BASE }
  bytes = Bagua::ntets_to_bytes(trios, 3)
  return bytes
end

Private Class Methods

gram(num) click to toggle source

Returns the trigram representation of num. If num >= 8, only the lowest three bits are used. @note The trigrams are arranged in binary order, as they are arranged in

Unicode.
# File lib/bagua/tri.rb, line 45
def self.gram(num)
  return [TRI_BASE + (num & 7)].pack('U')
end