Module: Unicode::Confusable

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

Constant Summary collapse

VERSION =
"1.6.0"
UNICODE_VERSION =
"12.1.0"
DATA_DIRECTORY =
File.expand_path(File.dirname(__FILE__) + "/../../../data/").freeze
INDEX_FILENAME =
(DATA_DIRECTORY + "/confusable.marshal.gz").freeze

Class Method Summary collapse

Class Method Details

.confusable?(string1, string2) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.confusable?(string1, string2)
  skeleton(string1) == skeleton(string2)
end

.list(char, partial_mapping_allowed = true) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/unicode/confusable.rb', line 20

def self.list(char, partial_mapping_allowed = true)
  require_relative 'confusable/index' unless defined? ::Unicode::Confusable::INDEX
  codepoint = char.codepoints.first or raise ArgumentError, "no data given to Unicode::Confusable.list"
  if partial_mapping_allowed
    INDEX.select{ |k,v| v == codepoint || v.is_a?(Array) && v.include?(codepoint) }.keys.map{ |codepoint| [codepoint].pack("U*") }
  else
    INDEX.select{ |k,v| v == codepoint }.keys.map{ |codepoint| [codepoint].pack("U") }
  end
end

.skeleton(string) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/unicode/confusable.rb', line 11

def self.skeleton(string)
  require_relative 'confusable/index' unless defined? ::Unicode::Confusable::INDEX
  UnicodeNormalize.normalize(
    UnicodeNormalize.normalize(string, :nfd).each_codepoint.map{ |codepoint|
      INDEX[codepoint] || codepoint
    }.flatten.pack("U*"), :nfd
  )
end