class ICU::USet

Overview

Sets of Unicode Code Points and Strings

ICU::USet is a mutable set of Unicode code points (characters) and multi-character strings. It is used internally by ICU APIs such as ICU::Collator and ICU::Transliterator, and is exposed here as an Enumerable(Char) so that callers can work with ordinary Crystal types.

Sets can be constructed from a character range, from a UnicodeSet pattern string (e.g. "[\\p{L}]", "[a-zA-Z0-9]"), or built up programmatically via #add / #add_range.

For simple inspection you can call #to_set to obtain a plain Set(Char), or iterate directly with #each.

Usage

s = ICU::USet.new('a', 'z') # characters a–z
s.includes?('m')            # => true
s.size                      # => 26
s.to_set                    # => Set{'a', 'b', ..., 'z'}

vowels = ICU::USet.new("[aeiouAEIOU]")
vowels.includes?('e') # => true

See also

Included Modules

Defined in:

icu/uset.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(from : Char, to : Char) #

Creates a set containing all characters in the range from..to (inclusive).

ICU::USet.new('a', 'z').size # => 26

[View source]
def self.new(pattern : String) #

Creates a set from a UnicodeSet pattern.

Raises ICU::Error if the pattern is invalid.

ICU::USet.new("[\\p{Lu}]") # all uppercase letters
ICU::USet.new("[aeiou]")   # vowels

[View source]
def self.new #

Creates an empty set.

ICU::USet.new

[View source]

Instance Method Detail

def add(char : Char) : self #

Adds char to this set.

s = ICU::USet.new
s.add('x')
s.includes?('x') # => true

[View source]
def add(string : String) : self #

Adds a multi-character string element to this set.


[View source]
def add_all(other : USet) : self #

Adds all members of other to this set (union in place).

a = ICU::USet.new('a', 'c')
b = ICU::USet.new('d', 'f')
a.add_all(b).size # => 6

[View source]
def add_range(from : Char, to : Char) : self #

Adds all characters in the range from..to (inclusive) to this set.

s = ICU::USet.new
s.add_range('a', 'f')
s.size # => 6

[View source]
def clear : self #

Removes all members, leaving an empty set.


[View source]
def complement : self #

Complements this set: every character previously absent is added, and every character previously present is removed.


[View source]
def disjoint?(other : USet) : Bool #

Returns true if this set and other have no characters in common.


[View source]
def each(& : Char -> ) : Nil #

Yields each character in this set (string elements are skipped).

ICU::USet.new('a', 'c').each { |c| print c } # => abc

[View source]
def empty? : Bool #

Returns true if the set contains no characters or strings.

ICU::USet.new.empty? # => true

[View source]
def finalize #

[View source]
def includes?(char : Char) : Bool #

Returns true if char is a member of this set.

ICU::USet.new('a', 'z').includes?('m') # => true
ICU::USet.new('a', 'z').includes?('M') # => false

[View source]
def includes?(string : String) : Bool #

Returns true if string (as a multi-character string element) is in this set.


[View source]
def includes_all_of?(string : String) : Bool #

Returns true if every character of string is individually in this set.

ICU::USet.new('a', 'z').includes_all_of?("hello") # => true
ICU::USet.new('a', 'z').includes_all_of?("Hello") # => false

[View source]
def intersects?(other : USet) : Bool #

Returns true if this set and other have at least one character in common.


[View source]
def remove(char : Char) : self #

Removes char from this set.


[View source]
def remove_all(other : USet) : self #

Removes all members of other from this set (set difference in place).


[View source]
def remove_range(from : Char, to : Char) : self #

Removes all characters in the range from..to from this set.


[View source]
def retain_all(other : USet) : self #

Retains only the members of other (intersection in place).

a = ICU::USet.new('a', 'f')
b = ICU::USet.new('d', 'z')
a.retain_all(b).to_set # => Set{'d', 'e', 'f'}

[View source]
def size : Int32 #

Returns the number of characters (and strings) in this set.

ICU::USet.new('a', 'z').size # => 26

[View source]
def superset?(other : USet) : Bool #

Returns true if every member of other is also in this set.


[View source]
def to_pattern(escape_unprintable : Bool = false) : String #

Returns the UnicodeSet pattern string representing this set.

ICU::USet.new('a', 'c').to_pattern # => "[a-c]"

[View source]
def to_set : Set(Char) #

Returns a Crystal Set(Char) with all characters from this set. Multi-character string elements are omitted.

ICU::USet.new('a', 'c').to_set # => Set{'a', 'b', 'c'}

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

[View source]