Functions to operate on Unicode sets:
- Intersection
- Difference
- Union
- Inversion
Summary
Functions
Combines the terms of a union-only expression tree into a
flat list of {:in, ranges} and {:not_in, ranges} terms.
Merges overlapping and adjacent code point ranges.
Returns the code point complement of a set or a range list.
Returns the difference of two lists of code point ranges.
Expands a reduced set, or a reduced expression tree, into a single positive list of code point ranges.
Expands any string ranges in a range list into their individual string members.
Returns whether an expression tree contains an intersection or a difference.
Returns the intersection of two lists of code point ranges.
Evaluates the set operations of a parsed set, leaving a list of code point ranges.
Returns the symmetric difference of two lists of code point ranges.
Walks a reduced set, invoking a function on each code point range, and returns the results.
Returns the union of two lists of code point ranges.
Functions
Combines the terms of a union-only expression tree into a
flat list of {:in, ranges} and {:not_in, ranges} terms.
This is the fast path used by reduce/1 when an expression
contains no intersection or difference; otherwise the tree
must be expanded with expand/1.
Arguments
astis a parsed expression term.
Returns
- A list of
{:in, ranges}and{:not_in, ranges}terms, or a single such term.
Merges overlapping and adjacent code point ranges.
Arguments
rangesis a sorted list of{first, last}code point ranges and string members, or an{:in, ranges}or{:not_in, ranges}term, or a list of such terms.
Returns
- The same shape with overlapping and adjacent code point ranges merged. String members are deduplicated but otherwise kept as written.
Examples
iex> Unicode.Set.Operation.compact_ranges([{1, 2}, {3, 4}, {4, 9}])
[{1, 9}]
iex> Unicode.Set.Operation.compact_ranges({:in, [{97, 98}, {99, 99}]})
{:in, [{97, 99}]}
Returns the code point complement of a set or a range list.
Arguments
setis aUnicode.Set.t/0in any state, or a list of{first, last}code point ranges.
Returns
For a
Unicode.Set.t/0, the set with{:in, ranges}and{:not_in, ranges}exchanged.For a range list, the list of code points from U+0000 to U+10FFFF that are not in it.
Examples
iex> Unicode.Set.Operation.complement([{0, 96}, {98, 1114111}])
[{97, 97}]
Returns the difference of two lists of code point ranges.
Arguments
aandbare sorted lists of{first, last}code point ranges with no overlapping ranges, ascompact_ranges/1returns them.
Returns
- A sorted list of the code point ranges in
athat are not inb.
Examples
iex> Unicode.Set.Operation.difference([{1, 10}], [{3, 4}, {8, 20}])
[{1, 2}, {5, 7}]
Expands a reduced set, or a reduced expression tree, into a single positive list of code point ranges.
Arguments
unicode_setis a reducedUnicode.Set.t/0, or a reduced expression term such as{:not_in, ranges}or{:union, [this, that]}.
Returns
For a
Unicode.Set.t/0, the set with its:parsedfield replaced by the expanded range list and its:stateset to:expanded.For a term, the expanded range list.
Examples
iex> Unicode.Set.parse_and_reduce!("[^a]") |> Unicode.Set.Operation.expand() |> Map.get(:parsed)
[{0, 96}, {98, 1114111}]
Expands any string ranges in a range list into their individual string members.
Arguments
rangesis a list of code point ranges and string ranges, where a string range is a{from, to}pair of charlists of the same length.
Returns
- The range list with each string range replaced by one
{string, string}member per string in the range.
Examples
iex> Unicode.Set.Operation.expand_string_ranges([{97, 97}, {~c"ab", ~c"ad"}])
[{97, 97}, {~c"ab", ~c"ab"}, {~c"ac", ~c"ac"}, {~c"ad", ~c"ad"}]
Returns whether an expression tree contains an intersection or a difference.
When it does, every range, including a complement, must be
expanded before the operation can be evaluated. When it does
not, a complement can be passed through as {:not_in, ranges}.
Arguments
astis a parsed expression term.
Returns
trueorfalse.
Examples
iex> Unicode.Set.Operation.has_difference_or_intersection?(Unicode.Set.parse!("[[a-z]&[m-z]]").parsed)
true
iex> Unicode.Set.Operation.has_difference_or_intersection?(Unicode.Set.parse!("[[a-z][m-z]]").parsed)
false
Returns the intersection of two lists of code point ranges.
Arguments
aandbare sorted lists of{first, last}code point ranges with no overlapping ranges, ascompact_ranges/1returns them.
Returns
- A sorted list of the code point ranges common to both.
Examples
iex> Unicode.Set.Operation.intersect([{1, 5}, {10, 20}], [{3, 12}])
[{3, 5}, {10, 12}]
Evaluates the set operations of a parsed set, leaving a list of code point ranges.
A complement is preserved as {:not_in, ranges} for as
long as possible, since guards, regexes and nimble_parsec
can all consume an exclusion directly. Only an intersection
or difference forces the ranges to be expanded into
positive form.
Arguments
unicode_setis aUnicode.Set.t/0returned byUnicode.Set.parse/1. A set that is already reduced or expanded is returned unchanged.
Returns
- The
Unicode.Set.t/0with its:parsedfield replaced by{:in, ranges},{:not_in, ranges}or a list of such terms, and its:stateset to:reduced.
Examples
iex> Unicode.Set.parse!("[[a-z]&[m-z]]") |> Unicode.Set.Operation.reduce() |> Map.get(:parsed)
{:in, [{109, 122}]}
iex> Unicode.Set.parse!("[^a-z]") |> Unicode.Set.Operation.reduce() |> Map.get(:parsed)
{:not_in, [{97, 122}]}
Returns the symmetric difference of two lists of code point ranges.
Arguments
thisandthatare lists of{first, last}code point ranges.
Returns
- A sorted list of the code point ranges in either list but not in both.
Examples
iex> Unicode.Set.Operation.symmetric_difference([{1, 5}], [{3, 8}])
[{1, 2}, {6, 8}]
Walks a reduced set, invoking a function on each code point range, and returns the results.
This is how the transforms to guards, patterns, utf8_char/1
lists and regex strings are built.
Arguments
setis a reducedUnicode.Set.t/0, or its:parsedfield.varis an optional term passed through tofun, used to build guard clauses around a variable.funis a function of a range, the accumulated result andvar.
Returns
- The result of applying
funacross the ranges.
Returns the union of two lists of code point ranges.
Arguments
a_listandb_listare lists of{first, last}code point ranges.
Returns
- A sorted, compacted list of the code point ranges in either list.
Examples
iex> Unicode.Set.Operation.union([{1, 3}, {10, 12}], [{4, 6}])
[{1, 6}, {10, 12}]