Skip to content

Unicode Set version 0.1.0

Choose a tag to compare

@kipcole9 kipcole9 released this 22 Nov 21:30

Unicode Set

A Unicode Set is a representation of a set of Unicode characters or character strings. The contents of that set are specified by patterns or by building them programmatically. This library implements parsing of unicode sets, resolving them to a list of codepoints and matching a given codepoint to that list. This is all consoldited into a single primary macro, Unicode.Set.match?/2.

The implementation conforms closely to the Unicode Set specification but currently omits support for the \N{codepoint_name} syntax.

Usage

The primary api is the macro Unicode.Set.match?/2 that returns a boolean based upon whether a given codepoint matches a unicode set.

Function guards

This is helpful in defining function guards. For example:

defmodule Guards do
  require Unicode.Set

  # Define a guard that checks if a codepoint is a unicode digit
  defguard digit?(x) when Unicode.Set.match?(x, "[[:Nd:]]")
end

defmodule MyModule do
  require Guards

  # Define a function using the previously defined guard
  def my_function(<< x :: utf8, _rest :: binary>>) when Guards.digit?(x) do
    IO.puts "Its a digit!"
  end

  # Define a guard directly on the function
  def my_other_function_(<< x :: utf8, _rest :: binary>>) when Unicode.Set.match?(x, "[[:Nd:]]") do
    IO.puts "Its also a digit!"
  end
end

Other Examples

These examples show how to combine sets (union, difference and intersection) to deliver a flexible targeting of the required match.

# The character "๓" is the thai digit `1`
iex> Unicode.Set.match? ?๓, "[[:digit:]]"
true

# Set operations allow union, insersection and difference
# This example matches on digits, but not the Thai script
iex> Unicode.Set.match? ?๓, "[[:digit:]-[:thai:]]"
false