module Unicode::Categories

Constants

DATA_DIRECTORY
INDEX
INDEX_FILENAME
UNICODE_VERSION
VERSION

Public Class Methods

categories(string, **options) click to toggle source
# File lib/unicode/categories.rb, line 5
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
Also aliased as: of
category(char, format: :short) click to toggle source
# File lib/unicode/categories.rb, line 15
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
names(format: :short) click to toggle source
# File lib/unicode/categories.rb, line 33
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
of(string, **options)
Alias for: categories