Module: BaseHangul

Defined in:
lib/basehangul.rb,
lib/basehangul/utils.rb,
lib/basehangul/version.rb

Overview

Human-readable binary encoding.

Defined Under Namespace

Modules: Utils, Version

Constant Summary collapse

REGEX_BASEHANGUL =

Regular expression for BaseHangul.

Regexp.new('^(?:[^빎빔빕빗흐]{4})*' \
'(?:[^빎빔빕빗흐]흐{3}|[^빎빔빕빗흐]{2}흐{2}|' \
'[^빎빔빕빗흐]{3}[빎빔빕빗흐])?$').freeze

Class Method Summary collapse

Class Method Details

.decode(str) ⇒ Object

Public: Decode BaseHangul string. Characters outside the BaseHangul are ignored.

str - A String encoded with BaseHangul.

Returns the String decoded binary.



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/basehangul.rb', line 48

def self.decode(str)
  indices = str.each_char.map { |ch| Utils.to_index(ch) }
  binary = indices.map do |index|
    case index
    when 0..1023    then index.to_s(2).rjust(10, '0')
    when 1024..1027 then (index - 1024).to_s(2).rjust(2, '0')
    end
  end.join
  binary = binary[0..-(binary.size % 8 + 1)]
  [binary].pack('B*')
end

.encode(bin) ⇒ Object

Public: Encode binary with BaseHangul.

bin - A String binary to encode.

Returns the String encoded hangul.



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

def self.encode(bin)
  chunks = Utils.chunks(bin.unpack('B*').first, 10)
  padding = ''
  last = chunks.last
  if last
    case last.size
    when 2       then chunks[-1] = '1' + last.rjust(10, '0')
    when 4, 6, 8 then padding = PADDING * (last.size / 2 - 1)
    end
  end
  chunks.map { |b| Utils.to_hangul(b.ljust(10, '0').to_i(2)) }.join + padding
end

.strict_decode(str) ⇒ Object

Public: Decode BaseHangul string.

str - A String encoded with BaseHangul.

Returns the String decoded binary. Raises ArgumentError if str is invalid BaseHangul.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/basehangul.rb', line 66

def self.strict_decode(str)
  indices = []
  str.each_char do |ch|
    index = Utils.to_index(ch)
    fail ArgumentError, MSG_INVALID_CHAR if index.nil?
    indices << index
  end
  fail ArgumentError, MSG_INVALID_PADDING unless str =~ REGEX_BASEHANGUL
  binary = indices.map do |index|
    case index
    when 0..1023    then index.to_s(2).rjust(10, '0')
    when 1024..1027 then (index - 1024).to_s(2).rjust(2, '0')
    end
  end.join
  binary = binary[0..-(binary.size % 8 + 1)]
  [binary].pack('B*')
end