Module: BaseHangul
- Defined in:
- lib/basehangul.rb,
lib/basehangul/cli.rb,
lib/basehangul/utils.rb,
lib/basehangul/option.rb,
lib/basehangul/version.rb
Overview
Human-readable binary encoding.
Defined Under Namespace
Modules: Utils, Version Classes: CLI, Option
Constant Summary collapse
- REGEX_BASEHANGUL =
Regular expression for BaseHangul.
Regexp.new('^(?:[^빎빔빕빗흐]{4})*' \ '(?:[^빎빔빕빗흐]흐{3}|[^빎빔빕빗흐]{2}흐{2}|' \ '[^빎빔빕빗흐]{3}[빎빔빕빗흐])?$').freeze
Class Method Summary collapse
-
.decode(str) ⇒ Object
Public: Decode BaseHangul string.
-
.encode(bin) ⇒ Object
Public: Encode binary with BaseHangul.
-
.strict_decode(str) ⇒ Object
Public: Decode BaseHangul string.
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.
50 51 52 |
# File 'lib/basehangul.rb', line 50 def self.decode(str) Utils.decode_indices(str.each_char.map { |ch| Utils.to_index(ch) }) end |
.encode(bin) ⇒ Object
Public: Encode binary with BaseHangul.
bin - A String binary to encode.
Returns the String encoded hangul.
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/basehangul.rb', line 31 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.
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/basehangul.rb', line 60 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 Utils.decode_indices(indices) end |