Method: Ecoji.decode

Defined in:
lib/ecoji.rb

.decode(data, encoding: Encoding::UTF_8) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/ecoji.rb', line 63

def self.decode(data, encoding: Encoding::UTF_8)
  expected_version = 3
  chars = data.chars
  result = []

  loop do
    emojis, expected_version = read_four(chars, expected_version)
    break if emojis.empty?

    bits = (emojis[0][:ordinal] << 30) |
           (emojis[1][:ordinal] << 20) |
           (emojis[2][:ordinal] << 10) |
           emojis[3][:ordinal]
    out = [
      bits >> 32,
      0xff & (bits >> 24),
      0xff & (bits >> 16),
      0xff & (bits >> 8),
      0xff & bits
    ]

    if emojis[1][:padding] == :fill
      out = out[0...1]
    elsif emojis[2][:padding] == :fill
      out = out[0...2]
    elsif emojis[3][:padding] == :fill
      out = out[0...3]
    elsif emojis[3][:padding] == :last
      out = out[0...4]
    end

    result.concat(out)
  end

  result.pack('C*').force_encoding(encoding)
end