class ICU::Collator
- ICU::Collator
- Reference
- Object
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.crConstant 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
-
.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.
Class Method Summary
-
.functional_equivalent(locale : String, keyword : String = KEYWORDS.keys.first)
Returns a functional equivalent to a given locale
Instance Method Summary
-
#[](attribute : Attribute) : AttributeValue
Get the value of the specified attribute
-
#[]=(attribute : Attribute, value : AttributeValue)
Set a value to the specified attribute
-
#compare(s1 : String, s2 : String) : Int
Compares two strings
-
#equals?(s1 : String, s2 : String) : Bool
Returns
true
if the two strings are equivalent - #finalize
- #locale : String?
- #reorder_codes : Array(ReorderCode)
- #reorder_codes=(codes : Array(ReorderCode))
- #rules : String?
- #strength : Strength
- #strength=(value : Strength)
- #to_unsafe : LibICU::UCollator
Constructor Detail
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")
Class Method Detail
Returns a functional equivalent to a given locale
ICU::Collator.functional_equivalent("en") # => "root"
Instance Method Detail
Get the value of the specified attribute
Set a value to the specified attribute
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
)
Returns true
if the two strings are equivalent
col = ICU::Collator.new(rules: "&a = b")
col.equals?("a", "b") # => true