Unicode.Guards (Unicode v2.0.0)

Copy Markdown View Source

Defines a set of guards that can be used with Elixir functions to test whether a codepoint is a member of a Unicode set.

Each guard operates on a single integer codepoint since the operators permitted in a guard clause are restricted to simple comparisons that do not include string comparators.

The data that underpins these guards is generated from the Unicode character database and therefore includes a broad range of scripts well beyond the basic ASCII definitions.

Usage

Import or require the module and use the guards in a function clause when expression:

defmodule MyModule do
  import Unicode.Guards

  def character_type(codepoint) when is_upper(codepoint), do: :upper
  def character_type(codepoint) when is_lower(codepoint), do: :lower
  def character_type(codepoint) when is_digit(codepoint), do: :digit
  def character_type(_codepoint), do: :other
end

Implementation

The guards are defined as macros. Each expands, at its call site, to a membership test built as a balanced tree of comparisons over the ranges of its Unicode set. Some sets are large (is_graph/1 spans ~740 ranges), so prefer using a guard in a single function clause rather than repeating it at many when sites, since every use re-expands the membership test.

Because they are macros rather than defguard/1 definitions, they can be used in any when clause but cannot themselves be used to define another defguard.

Summary

Functions

Guards whether a codepoint is a blank character.

Guards whether a codepoint is a currency symbol character.

Guards whether a codepoint is a decimal digit character.

Guards whether a codepoint is a graphic character.

Guards whether a codepoint is a lower case character.

Guards whether a codepoint is a printing character.

Guards whether a codepoint is printable.

Guards whether a codepoint is a quotation mark.

Guards whether a codepoint is a quotation mark that may be used as either a left or right mark.

Guards whether a codepoint is a double quotation mark.

Guards whether a codepoint is a left quotation mark.

Guards whether a codepoint is a right quotation mark.

Guards whether a codepoint is a single quotation mark.

Guards whether a codepoint is a space separator character.

Guards whether a codepoint is an upper case character.

Guards whether a codepoint is a whitespace character.

Functions

is_blank(codepoint)

(macro)

Guards whether a codepoint is a blank character.

Matches the space separators (Zs) together with the tab character, matching the POSIX [:blank:] class.

Examples

iex> import Unicode.Guards
iex> match?(codepoint when is_blank(codepoint), ?\t)
true

is_currency_symbol(codepoint)

(macro)

Guards whether a codepoint is a currency symbol character.

Matches any currency symbol (general category Sc).

Examples

iex> import Unicode.Guards
iex> match?(codepoint when is_currency_symbol(codepoint), ?$)
true

is_digit(codepoint)

(macro)

Guards whether a codepoint is a decimal digit character.

Matches any decimal digit (general category Nd) from any Unicode script, not only the ASCII digits.

Examples

iex> import Unicode.Guards
iex> match?(codepoint when is_digit(codepoint), ?5)
true
iex> match?(codepoint when is_digit(codepoint), ?x)
false

is_graph(codepoint)

(macro)

Guards whether a codepoint is a graphic character.

Graphic characters are those that are non-space, non-control, non-surrogate and assigned, as modelled by the :Graph derived general category.

Examples

iex> import Unicode.Guards
iex> match?(codepoint when is_graph(codepoint), ?A)
true
iex> match?(codepoint when is_graph(codepoint), ?\s)
false

is_lower(codepoint)

(macro)

Guards whether a codepoint is a lower case character.

Matches any codepoint that Unicode defines as a lower case letter (general category Ll) in any script.

Examples

iex> import Unicode.Guards
iex> match?(codepoint when is_lower(codepoint), ?a)
true
iex> match?(codepoint when is_lower(codepoint), ?A)
false

is_print(codepoint)

(macro)

Guards whether a codepoint is a printing character.

Matches the combination of the graphic characters and the space separators, matching the POSIX [:print:] class.

Examples

iex> import Unicode.Guards
iex> match?(codepoint when is_print(codepoint), ?A)
true
iex> match?(codepoint when is_print(codepoint), ?\s)
true

is_printable(codepoint)

(macro)

Guards whether a codepoint is printable.

Uses the same definition of printable as String.printable?/1.

Examples

iex> import Unicode.Guards
iex> match?(codepoint when is_printable(codepoint), ?A)
true

is_quote_mark(codepoint)

(macro)

Guards whether a codepoint is a quotation mark.

See also Unicode.Category.QuoteMarks.

Examples

iex> import Unicode.Guards
iex> match?(codepoint when is_quote_mark(codepoint), ?")
true

is_quote_mark_ambidextrous(codepoint)

(macro)

Guards whether a codepoint is a quotation mark that may be used as either a left or right mark.

See also Unicode.Category.QuoteMarks.

Examples

iex> import Unicode.Guards
iex> match?(codepoint when is_quote_mark_ambidextrous(codepoint), ?")
true

is_quote_mark_double(codepoint)

(macro)

Guards whether a codepoint is a double quotation mark.

See also Unicode.Category.QuoteMarks.

Examples

iex> import Unicode.Guards
iex> match?(codepoint when is_quote_mark_double(codepoint), ?")
true

is_quote_mark_left(codepoint)

(macro)

Guards whether a codepoint is a left quotation mark.

See also Unicode.Category.QuoteMarks.

Examples

iex> import Unicode.Guards
iex> match?(codepoint when is_quote_mark_left(codepoint), 0x00AB)
true

is_quote_mark_right(codepoint)

(macro)

Guards whether a codepoint is a right quotation mark.

See also Unicode.Category.QuoteMarks.

Examples

iex> import Unicode.Guards
iex> match?(codepoint when is_quote_mark_right(codepoint), 0x00BB)
true

is_quote_mark_single(codepoint)

(macro)

Guards whether a codepoint is a single quotation mark.

See also Unicode.Category.QuoteMarks.

Examples

iex> import Unicode.Guards
iex> match?(codepoint when is_quote_mark_single(codepoint), ?')
true

is_separator(codepoint)

(macro)

Guards whether a codepoint is a space separator character.

Matches the Unicode Zs category.

Examples

iex> import Unicode.Guards
iex> match?(codepoint when is_separator(codepoint), ?\s)
true

is_upper(codepoint)

(macro)

Guards whether a codepoint is an upper case character.

Matches any codepoint that Unicode defines as an upper case letter (general category Lu) in any script.

Examples

iex> import Unicode.Guards
iex> match?(codepoint when is_upper(codepoint), ?A)
true
iex> match?(codepoint when is_upper(codepoint), ?a)
false

is_whitespace(codepoint)

(macro)

Guards whether a codepoint is a whitespace character.

Matches the Unicode Zs category plus the range 0x09..0x0d which includes tab, newline and carriage return.

Examples

iex> import Unicode.Guards
iex> match?(codepoint when is_whitespace(codepoint), ?\s)
true
iex> match?(codepoint when is_whitespace(codepoint), ?\t)
true