Sha256: 3df36f0053068261a52e9206286fe8d59edc9a599aae8877006963266f0adb9a

Contents?: true

Size: 1.46 KB

Versions: 13

Compression:

Stored size: 1.46 KB

Contents

require_relative "categories/constants"

module Unicode
  module Categories
    def self.categories(string, **options)
      res = []
      string.each_char{ |char|
        category_name = category(char, **options)
        res << category_name unless res.include?(category_name)
      }   
      res.sort
    end 
    class << self; alias of categories; end 

    def self.category(char, format: :short)
      require_relative 'categories/index' unless defined? ::Unicode::Categories::INDEX
      codepoint_depth_offset = char.ord or
          raise(ArgumentError, "Unicode::Categories.category must be given a valid char")
      index_or_value = INDEX[:CATEGORIES]
      [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 || "Cn"
          return format == :long ? INDEX[:CATEGORY_NAMES][res] : res
        end
      }

      res = index_or_value[codepoint_depth_offset] || "Cn"
      format == :long ? INDEX[:CATEGORY_NAMES][res] : res
    end 

    def self.names(format: :short)
      require_relative 'categories/index' unless defined? ::Unicode::Categories::INDEX
      case format
      when :long
        INDEX[:CATEGORY_NAMES].values.sort
      when :short
        INDEX[:CATEGORY_NAMES].keys.sort
      when :table
        INDEX[:CATEGORY_NAMES].dup
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
unicode-categories-1.10.0 lib/unicode/categories.rb
unicode-categories-1.9.0 lib/unicode/categories.rb
unicode-categories-1.8.0 lib/unicode/categories.rb
unicode-categories-1.7.0 lib/unicode/categories.rb
unicode-categories-1.6.0 lib/unicode/categories.rb
unicode-categories-1.5.0 lib/unicode/categories.rb
unicode-categories-1.4.0 lib/unicode/categories.rb
unicode-categories-1.3.0 lib/unicode/categories.rb
unicode-categories-1.2.2 lib/unicode/categories.rb
unicode-categories-1.2.1 lib/unicode/categories.rb
unicode-categories-1.2.0 lib/unicode/categories.rb
unicode-categories-1.1.2 lib/unicode/categories.rb
unicode-categories-1.1.1 lib/unicode/categories.rb