struct

String::Grapheme

Inherits Struct / Value / Object

Grapheme represents a Unicode grapheme cluster, which describes the smallest functional unit of a writing system. This is also called a user-perceived character.

In the latin alphabet, most graphemes consist of a single Unicode codepoint (equivalent to Char). But a grapheme can also consist of a sequence of codepoints, which combine into a single unit.

For example, the string "e\u0301" consists of two characters, the latin small letter e and the combining acute accent ´. Together, they form a single grapheme: . That same grapheme could alternatively be described in a single codepoint, \u00E9 (latin small letter e with acute). But the combinatory possibilities are far bigger than the amount of directly available codepoints.

"e\u0301".size # => 2
"é".size       # => 1

"e\u0301".grapheme_size # => 1
"é".grapheme_size       # => 1

This combination of codepoints is common in some non-latin scripts. It's also often used with emojis to create customized combination. For example, the thumbs up sign 👍 (U+1F44D) combined with an emoji modifier such as U+1F3FC assign a colour to the emoji.

Instances of this type can be acquired via String#each_grapheme or String#graphemes.

The algorithm to determine boundaries between grapheme clusters is specified in the Unicode Standard Annex #29.

Instance methods

==(other : self) : Bool

Returns true if other is equivalent to self.

Two graphemes are considered equivalent if they contain the same sequence of codepoints.

Source
bytesize

Returns the number of bytes in the UTF-8 representation of this grapheme cluster.

Source
inspect(io : IO) : Nil

Appends a representation of this grapheme cluster to io.

Source
size

Returns the number of characters in this grapheme cluster.

Source
to_s(io : IO) : Nil

Appends the characters in this grapheme cluster to io.

Source
to_s

Returns the characters in this grapheme cluster.

Source