Module: Unicode::Scripts

Defined in:
lib/unicode/scripts.rb,
lib/unicode/scripts/index.rb,
lib/unicode/scripts/constants.rb

Constant Summary collapse

VERSION =
"1.9.0"
UNICODE_VERSION =
"15.1.0"
DATA_DIRECTORY =
File.expand_path(File.dirname(__FILE__) + "/../../../data/").freeze
INDEX_FILENAME =
(DATA_DIRECTORY + "/scripts.marshal.gz").freeze

Class Method Summary collapse

Class Method Details

.names(format: :long) ⇒ Object



49
50
51
52
53
54
# File 'lib/unicode/scripts.rb', line 49

def self.names(format: :long)
  require_relative 'scripts/index' unless defined? ::Unicode::Scripts::INDEX
  format == :long ?
      INDEX[:SCRIPT_NAMES].sort :
      INDEX[:SCRIPT_ALIASES].keys.sort
end

.script(char, format: :long) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/unicode/scripts.rb', line 15

def self.script(char, format: :long)
  require_relative 'scripts/index' unless defined? ::Unicode::Scripts::INDEX
  codepoint_depth_offset = char.unpack("U")[0] or
      raise(ArgumentError, "Unicode::Scripts.script must be given a valid char")
  index_or_value = INDEX[:SCRIPTS]
  [0x10000, 0x1000, 0x100, 0x10].each{ |depth|
    index_or_value         = index_or_value[codepoint_depth_offset / depth]
    codepoint_depth_offset = codepoint_depth_offset % depth
    unless index_or_value.is_a? Array
      res = index_or_value || INDEX[:SCRIPT_ALIASES]["Zzzz"]
      return format == :long ? INDEX[:SCRIPT_NAMES][res] : INDEX[:SCRIPT_ALIASES].key(res)
    end
  }   

  res = index_or_value[codepoint_depth_offset] || INDEX[:SCRIPT_ALIASES]["Zzzz"]
  format == :long ? INDEX[:SCRIPT_NAMES][res] : INDEX[:SCRIPT_ALIASES].key(res)
end

.script_extensions(string, format: :long) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/unicode/scripts.rb', line 33

def self.script_extensions(string, format: :long)
  require_relative 'scripts/index' unless defined? ::Unicode::Scripts::INDEX

  string.each_codepoint.inject([]){ |res, codepoint|
    if new_scripts = INDEX[:SCRIPT_EXTENSIONS][codepoint]
      script_extension_names = new_scripts.map{ |new_script|
        format == :long ? INDEX[:SCRIPT_NAMES][new_script] : INDEX[:SCRIPT_ALIASES].key(new_script)
      }
    else
      script_extension_names = scripts([codepoint].pack("U"), format: format)
    end

    res | script_extension_names
  }.sort
end

.scripts(string, **options) ⇒ Object Also known as: of



5
6
7
8
9
10
11
12
# File 'lib/unicode/scripts.rb', line 5

def self.scripts(string, **options)
  res = []
  string.each_char{ |char|
    script_name = script(char, **options)
    res << script_name unless res.include?(script_name)
  }
  res.sort
end