Sha256: 814aef6fa2239e7665a4d4de9f9c90bb8601442b5d4a5fadc37cbfa33d981493

Contents?: true

Size: 1.06 KB

Versions: 11

Compression:

Stored size: 1.06 KB

Contents

require_relative "confusable/constants"

require 'unicode_normalize/normalize'

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

    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

    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
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
unicode-confusable-1.11.0 lib/unicode/confusable.rb
unicode-confusable-1.10.0 lib/unicode/confusable.rb
unicode-confusable-1.9.0 lib/unicode/confusable.rb
unicode-confusable-1.8.0 lib/unicode/confusable.rb
unicode-confusable-1.7.0 lib/unicode/confusable.rb
unicode-confusable-1.6.0 lib/unicode/confusable.rb
unicode-confusable-1.5.0 lib/unicode/confusable.rb
unicode-confusable-1.4.0 lib/unicode/confusable.rb
unicode-confusable-1.3.2 lib/unicode/confusable.rb
unicode-confusable-1.3.1 lib/unicode/confusable.rb
unicode-confusable-1.3.0 lib/unicode/confusable.rb