Hyperscan (hyperscan v0.1.5)

Summary

Functions

Allocate a scratch memory buffer for running the regex.

Create a new scratch buffer of the same size.

Compile a regular expression into a database.

Compile multiple regular expressions into a database.

Returns a map with debugging info about a regular expression.

Look up a flag constant by name.

Test whether the given compiled regex matches a string.

Test whether multiple compiled regexes match a string.

Look up a mode constant by name.

Re-allocate a scratch memory buffer.

Replace parts of a string that match a regular expression.

Check the size of a scratch buffer.

Check if the current system architecture supports Hyperscan.

Returns the compiled Hyperscan version as a string.

Functions

Link to this function

alloc_scratch(db)

Allocate a scratch memory buffer for running the regex.

Link to this function

clone_scratch(scratch)

Create a new scratch buffer of the same size.

Link to this function

compile(expression, flags, mode)

Compile a regular expression into a database.

  • expression is a regular expression, without the leading and trailing / characters or flags.
  • flags is an integer. Use 0 for no flags, or bitwise-or together one or more results of the flag/1 function.
  • mode is an integer similar to flags.

Returns {:ok, db} where db represents the compiled regex, or {:error, reason} on error.

See the docs for more information:

Example

iex> flags = Hyperscan.flag("HS_FLAG_CASELESS")
iex> mode = Hyperscan.mode("HS_MODE_BLOCK")
iex> {:ok, _db} = Hyperscan.compile("foo", flags, mode)
Link to this function

compile_multi(expression_list, flags_list, id_list, mode)

Compile multiple regular expressions into a database.

  • expression_list is a list of regular expressions, without the leading and trailing / characters or flags.
  • flags_list is a list of flags, one for each expression in expression_list. To use multiple flags for a single expression, bitwise-or them togther. Zero means no flags.
  • id_list is a list of arbitrary integers, with one value for each expression in expression_list. An ID will be returned from match_multi/3 if its corresponding expression matches the input string.
  • mode is an integer returned by mode/1.

See the docs for more information:

Example

iex> flags = Hyperscan.flag("HS_FLAG_CASELESS")
iex> mode = Hyperscan.mode("HS_MODE_BLOCK")
iex> {:ok, db} = Hyperscan.compile_multi(["foo", "bar"], [flags, flags], [1, 2], mode)
iex> {:ok, scratch} = Hyperscan.alloc_scratch(db)
iex> Hyperscan.match_multi(db, "Foo", scratch)
{:ok, [1]}
iex> Hyperscan.match_multi(db, "Bar", scratch)
{:ok, [2]}
iex> Hyperscan.match_multi(db, "Baz", scratch)
{:ok, []}
Link to this function

expression_info(expression, flags)

Returns a map with debugging info about a regular expression.

Look up a flag constant by name.

Returns the integer value of the Hyperscan flag constant with the given name. Names follow the pattern HS_FLAG_*. See the docs for available flag names.

The value is used by the compile and compile_multi functions. To use multiple flags simultaneously, combine their values with Bitwise.bor/2.

Raises :badarg if it is not a valid flag name.

Example

iex> Hyperscan.flag("HS_FLAG_CASELESS")
1
Link to this function

match(db, string, scratch)

Test whether the given compiled regex matches a string.

Requires a scratch buffer allocated by alloc_scratch/1.

Link to this function

match_multi(db, string, scratch)

Test whether multiple compiled regexes match a string.

The regex should be compiled by compile_multi. Returns a list of expression IDs that matched the string. Note that the order of the returned IDs is undefined.

See the docs for compile_multi/4 for more info.

Look up a mode constant by name.

Returns the integer value of the Hyperscan mode constant with the given name. Names follow the pattern HS_MODE_*. See the docs for available mode names.

The value is used by the compile and compile_multi functions. To use multiple modes simultaneously, combine their values with Bitwise.bor/2.

Raises :badarg if it is not a valid mode name.

Example

iex> Hyperscan.mode("HS_MODE_BLOCK")
1
Link to this function

realloc_scratch(db, scratch)

Re-allocate a scratch memory buffer.

If the scratch was allocated for a different database and is too small for the given one, it will be reallocated. Either way, the scratch is now safe to use for the given database. Returns :ok.

Link to this function

replace(db, string, replacement, scratch)

Replace parts of a string that match a regular expression.

The database must have been compiled with HS_FLAG_SOM_LEFTMOST.

Requires a scratch buffer allocated by alloc_scratch/1.

Example

iex> {:ok, db} = compile("b", flag("HS_FLAG_SOM_LEFTMOST"), mode("HS_MODE_BLOCK"))
iex> {:ok, scratch} = alloc_scratch(db)
iex> Hyperscan.replace(db, "a b c", "x", scratch)
{:ok, "a x c"}
Link to this function

scratch_size(scratch)

Check the size of a scratch buffer.

Link to this function

valid_platform()

Check if the current system architecture supports Hyperscan.

Returns the compiled Hyperscan version as a string.