Unicode.WordBreak (Unicode v2.0.0)

Copy Markdown View Source

Functions to introspect the Unicode word break property for binaries (Strings) and codepoints.

The primary API is word_break/1 which returns the word break property for a codepoint or the list of word break properties for a string.

The functions fetch/1, get/1 and count/1 provide introspection of the codepoint ranges associated with a given word break property. word_breaks/0, known_word_breaks/0 and aliases/0 return the underlying property data.

Summary

Functions

Returns a map of aliases for Unicode word breaks.

Returns the count of the number of characters for a given word break.

Returns the Unicode codepoint ranges for a given word break.

Returns the Unicode codepoint ranges for a given word break.

Returns a list of known Unicode word break names.

Returns the word break name(s) for the given binary or codepoint.

Returns the map of Unicode word breaks.

Functions

aliases()

Returns a map of aliases for Unicode word breaks.

An alias is an alternative name for referring to a word break. Aliases are resolved by the fetch/1 and get/1 functions.

Returns

  • A map with the alias as a string key and the word break name as an atom value.

Examples

iex> Unicode.WordBreak.aliases() |> Map.get("le")
:aletter

count(word_break)

Returns the count of the number of characters for a given word break.

Arguments

  • word_break is any word break name as an atom, or a string alias for a word break.

Returns

  • The number of codepoints that have the given word break.

  • :error if the word break name is not known.

Examples

iex> Unicode.WordBreak.count(:aletter)
33973

fetch(word_break)

Returns the Unicode codepoint ranges for a given word break.

Aliases are resolved by this function.

Arguments

  • word_break is any word break name as an atom, or a string alias for a word break.

Returns

  • {:ok, range_list} where range_list is a list of codepoint ranges as 2-tuples.

  • :error if the word break name is not known.

Examples

iex> Unicode.WordBreak.fetch(:zwj)
{:ok, [{8205, 8205}]}

iex> Unicode.WordBreak.fetch(:invalid)
:error

get(word_break)

Returns the Unicode codepoint ranges for a given word break.

Aliases are resolved by this function.

Arguments

  • word_break is any word break name as an atom, or a string alias for a word break.

Returns

  • range_list which is a list of codepoint ranges as 2-tuples.

  • nil if the word break name is not known.

Examples

iex> Unicode.WordBreak.get(:zwj)
[{8205, 8205}]

iex> Unicode.WordBreak.get(:invalid)
nil

known_word_breaks()

Returns a list of known Unicode word break names.

This function does not return the names of any word break aliases.

Returns

  • A list of word break names as atoms.

Examples

iex> Unicode.WordBreak.known_word_breaks() |> Enum.sort() |> Enum.take(3)
[:aletter, :cr, :double_quote]

word_break(string)

Returns the word break name(s) for the given binary or codepoint.

Arguments

  • codepoint_or_string is either an integer codepoint or a string.

Returns

  • In the case of a codepoint, a single word break name as an atom. Codepoints with no explicit word break property default to :xx.

  • In the case of a string, a list of the word break names for each codepoint in the string, in order. One entry is returned for each codepoint.

Examples

iex> Unicode.WordBreak.word_break(0x200D)
:zwj

iex> Unicode.WordBreak.word_break("A")
[:aletter]

word_breaks()

Returns the map of Unicode word breaks.

Returns

  • A map with the word break name as the key and a list of codepoint ranges as 2-tuples as the value.

Examples

iex> Unicode.WordBreak.word_breaks() |> Map.get(:zwj)
[{8205, 8205}]