class ICU::Collator

Overview

Collation

This class allows to perform locale-sensitive string comparison.

Sort ordering may be customized by providing your own set of rules (see CLDR root sort order).

Usage

ICU::Collator.new("en").compare("y", "k") # => 1
ICU::Collator.new("lt").compare("y", "k") # => -1

col = ICU::Collator.new(rules: "&c < b < a")
col.compare("a", "b") # => 1
col.compare("b", "c") # => 1
col.compare("d", "e") # => -1

NOTE the #compare method requires ICU >= 50

See also

Defined in:

icu/collator.cr

Constant Summary

DEFAULT = AttributeValue::Default
KEYWORDS = begin keywords = Hash(String, Set(String)).new ustatus = LibICU::UErrorCode::UZeroError kenum = LibICU.ucol_get_keywords(pointerof(ustatus)) ICU.check_error!(ustatus) (UEnum.new(kenum)).each do |keyword| ustatus = LibICU::UErrorCode::UZeroError venum = LibICU.ucol_get_keyword_values(keyword, pointerof(ustatus)) ICU.check_error!(ustatus) keywords[keyword] = Set(String).new((UEnum.new(venum)).to_a) end LibICU.uenum_close(kenum) keywords end
LOCALES = begin ustatus = LibICU::UErrorCode::UZeroError uenum = LibICU.ucol_open_available_locales(pointerof(ustatus)) ICU.check_error!(ustatus) locales = (UEnum.new(uenum)).to_a LibICU.uenum_close(uenum) Set(String).new(locales) end
OFF = AttributeValue::Off
ON = AttributeValue::On

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.new(locale : String? = nil, rules : String? = nil, normalization_mode : AttributeValue = DEFAULT, strength : Strength = AttributeValue::DefaultStrength) #

Initialize a new Collator specifying a locale or a set of rules. If none of them is specified it will be initialized with the default locale.

ICU::Collator.new
ICU::Collator.new("pt")
ICU::Collator.new(locale: "lt")
ICU::Collator.new(rules: "&c < b < a")

[View source]

Class Method Detail

def self.functional_equivalent(locale : String, keyword : String = KEYWORDS.keys.first) #

Returns a functional equivalent to a given locale

ICU::Collator.functional_equivalent("en") # => "root"

[View source]

Instance Method Detail

def [](attribute : Attribute) : AttributeValue #

Get the value of the specified attribute


[View source]
def []=(attribute : Attribute, value : AttributeValue) #

Set a value to the specified attribute


[View source]
def compare(s1 : String, s2 : String) : Int #

Compares two strings

ICU::Collator.new("en").compare("y", "k")       # => 1
ICU::Collator.new("lt").compare("y", "k")       # => -1
ICU::Collator.new("fr").compare("côte", "coté") # => -1

(see String#compare)


[View source]
def equals?(s1 : String, s2 : String) : Bool #

Returns true if the two strings are equivalent

col = ICU::Collator.new(rules: "&a = b")
col.equals?("a", "b") # => true

[View source]
def finalize #

[View source]
def locale : String? #

[View source]
def reorder_codes : Array(ReorderCode) #

[View source]
def reorder_codes=(codes : Array(ReorderCode)) #

[View source]
def rules : String? #

[View source]
def strength : Strength #

[View source]
def strength=(value : Strength) #

[View source]
def to_unsafe : LibICU::UCollator #

[View source]