Module: UnicodedataRb

Defined in:
lib/unicodedata_rb/codepoint.rb,
lib/unicodedata_rb.rb,
lib/unicodedata_rb/version.rb,
lib/unicodedata_rb/constants.rb,
lib/unicodedata_rb/generate_index.rb

Overview

Format of UnicodeData.txt: www.unicode.org/L2/L1999/UnicodeData.html

Defined Under Namespace

Classes: Codepoint, Constants, GenerateIndex

Constant Summary collapse

VERSION =
"0.1.0"
CODEPOINT_FIELDS =
[
  :codepoint, :name, :category, :combining_class, :bidi_class,
  :decomposition, :digit_value, :non_decimal_digit_value,
  :numeric_value, :bidi_mirrored, :unicode1_name, :iso_comment,
  :simple_uppercase_map, :simple_lowercase_map, :simple_titlecase_map,
]
NUMERIC_FIELDS =
[:digit_value, :non_decimal_digit_value, :numeric_value]

Class Method Summary collapse

Class Method Details

._unicodedata_indexObject



39
40
41
# File 'lib/unicodedata_rb.rb', line 39

def self._unicodedata_index
  @@_unicodedata_index ||= Marshal.load(File.binread(UnicodedataRb::Constants::UNICODEDATA_INDEX_PATH))
end

._unicodedata_txt_fileObject



35
36
37
# File 'lib/unicodedata_rb.rb', line 35

def self._unicodedata_txt_file
  @@_unicodedata_txt_file ||= File.open(UnicodedataRb::Constants::UNICODEDATA_TXT_PATH)
end

._unicodedata_txt_line_from(codepoint: nil, name: nil) ⇒ Object

Raises:

  • (ArgumentError)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/unicodedata_rb.rb', line 19

def self._unicodedata_txt_line_from(codepoint: nil, name: nil)
  raise ArgumentError if (codepoint.nil? && name.nil?) || (!codepoint.nil? && !name.nil?)
  _unicodedata_txt_file.rewind
  offset =
    if !codepoint.nil?
      _unicodedata_index[:codepoint][codepoint]
    else
      _unicodedata_index[:name][name]
    end

  raise ArgumentError if offset.nil?

  _unicodedata_txt_file.seek offset
  _unicodedata_txt_file.readline.chomp
end

.codepoint(num) ⇒ Object



7
8
9
# File 'lib/unicodedata_rb.rb', line 7

def self.codepoint(num)
  UnicodedataRb::Codepoint.from_line _unicodedata_txt_line_from(codepoint: num)
end

.codepoint_from_char(c) ⇒ Object



11
12
13
# File 'lib/unicodedata_rb.rb', line 11

def self.codepoint_from_char(c)
  UnicodedataRb::Codepoint.from_line _unicodedata_txt_line_from(codepoint: c.ord)
end

.codepoint_from_name(name) ⇒ Object



15
16
17
# File 'lib/unicodedata_rb.rb', line 15

def self.codepoint_from_name(name)
  UnicodedataRb::Codepoint.from_line _unicodedata_txt_line_from(name:)
end

.generate_indexObject



43
44
45
# File 'lib/unicodedata_rb.rb', line 43

def self.generate_index
  UnicodedataRb::GenerateIndex.call
end