core/text/regex
regex
Types
5Capture
Capture :: struct {
pos: [][2]int,
groups: []string,
}SourceThis struct corresponds to a set of string captures from a RegEx match.
pos will contain the start and end positions for each string in groups, such that str[pos[0][0]:pos[0][1]] == groups[0].
Creation_Error
Creation_Error :: enum int {
None = 0,
// A `\` was supplied as the delimiter to `create_by_user`.
Bad_Delimiter = 1,
// A pair of delimiters for `create_by_user` was not found.
Expected_Delimiter = 2,
// An unknown letter was supplied to `create_by_user` after the last delimiter.
Unknown_Flag = 3,
}SourceError
Error :: union {
// An error that can occur in the pattern parsing phase.
//
// Most of these are regular expression syntax errors and are either
// context-dependent as to what they mean or have self-explanatory names.
Parser_Error,
// An error that can occur in the pattern compiling phase.
//
// Of the two that can be returned, they have to do with exceeding the
// limitations of the Virtual Machine.
Compiler_Error,
// An error that occurs only for `create_by_user`.
Creation_Error,
}SourceMatch_Iterator
Match_Iterator :: struct {
regex: Regular_Expression,
capture: Capture,
vm: virtual_machine.Machine,
idx: int,
temp: runtime.Allocator,
threads: int,
done: bool,
}SourceAn iterator to repeatedly match a pattern against a string, to be used with *_iterator procedures.
Regular_Expression
Regular_Expression :: struct {
flags: Flags,
class_data: []virtual_machine.Rune_Class_Data,
program: []virtual_machine.Opcode,
}SourceA compiled Regular Expression value, to be used with the match_* procedures.
Constants
4Compiler_Error
Compiler_Error :: compiler.ErrorSourceFlag
Flag :: common.FlagSourceFlags
Flags :: common.FlagsSourceParser_Error
Parser_Error :: parser.ErrorSourceProcedures
11create
create :: proc(pattern: string, flags: Flags, permanent_allocator: mem.Allocator = context.allocator, temporary_allocator = context.temp_allocator) -> (result: Regular_Expression, err: Error)SourceCreate a regular expression from a string pattern and a set of flags.
Allocates Using Provided Allocators
Inputs:
- pattern: The pattern to compile.
- flags: A
bit_setof RegEx flags. - permanent_allocator: The allocator to use for the final regular expression. (default: context.allocator)
- temporary_allocator: The allocator to use for the intermediate compilation stages. (default: context.temp_allocator)
Returns:
- result: The regular expression.
- err: An error, if one occurred.
create_by_user
create_by_user :: proc(pattern: string, permanent_allocator: mem.Allocator = context.allocator, temporary_allocator = context.temp_allocator) -> (result: Regular_Expression, err: Error)SourceCreate a regular expression from a delimited string pattern, such as one provided by users of a program or those found in a configuration file.
They are in the form of:
[DELIMITER] [regular expression] [DELIMITER] [flags]
For example, the following strings are valid:
/hellope/i
#hellope#i
â¢hellopeâ¢i
ã¤hellopeã¤i
The delimiter is determined by the very first rune in the string.
The only restriction is that the delimiter cannot be `\`, as that rune is used
to escape the delimiter if found in the middle of the string.
All runes after the closing delimiter will be parsed as flags:
- 'm': Multiline
- 'i': Case_Insensitive
- 'x': Ignore_Whitespace
- 'u': Unicode
- 'n': No_Capture
- '-': No_Optimization
*Allocates Using Provided Allocators*
Inputs:
- pattern: The delimited pattern with optional flags to compile.
- str: The string to match against.
- permanent_allocator: The allocator to use for the final regular expression. (default: context.allocator)
- temporary_allocator: The allocator to use for the intermediate compilation stages. (default: context.temp_allocator)
Returns:
- result: The regular expression.
- err: An error, if one occurred.create_iterator
create_iterator :: proc(str: string, pattern: string, flags: Flags, permanent_allocator: mem.Allocator = context.allocator, temporary_allocator = context.temp_allocator) -> (result: Match_Iterator, err: Error)SourceCreate a Match_Iterator using a string to search, a regular expression to match against it, and a set of flags.
Allocates Using Provided Allocators
Inputs:
- str: The string to iterate over.
- pattern: The pattern to match.
- flags: A
bit_setof RegEx flags. - permanent_allocator: The allocator to use for the compiled regular expression. (default: context.allocator)
- temporary_allocator: The allocator to use for the intermediate compilation and iteration stages. (default: context.temp_allocator)
Returns:
- result: The
Match_Iterator. - err: An error, if one occurred.
destroy_capture
destroy_capture :: proc(capture: Capture, allocator: mem.Allocator = context.allocator)SourceFree all data allocated by the match_and_allocate_capture procedure.
Frees Using Provided Allocator
Inputs:
- capture: A
Capture. - allocator: (default: context.allocator)
destroy_iterator
destroy_iterator :: proc(it: Match_Iterator, allocator: mem.Allocator = context.allocator)SourceFree all data allocated by the create_iterator procedure.
Frees Using Provided Allocator
Inputs:
- it: A
Match_Iterator - allocator: (default: context.allocator)
destroy_regex
destroy_regex :: proc(regex: Regular_Expression, allocator: mem.Allocator = context.allocator)SourceFree all data allocated by the create* procedures.
Frees Using Provided Allocator
Inputs:
- regex: A regular expression.
- allocator: (default: context.allocator)
match_and_allocate_capture
match_and_allocate_capture :: proc(regex: Regular_Expression, str: string, permanent_allocator: mem.Allocator = context.allocator, temporary_allocator = context.temp_allocator) -> (capture: Capture, success: bool)SourceMatch a regular expression against a string and allocate the results into the returned capture structure.
The resulting capture strings will be slices to the string str, not wholly copied strings, so they won't need to be individually deleted.
Allocates Using Provided Allocators
Inputs:
- regex: The regular expression.
- str: The string to match against.
- permanent_allocator: The allocator to use for the capture results. (default: context.allocator)
- temporary_allocator: The allocator to use for the virtual machine. (default: context.temp_allocator)
Returns:
- capture: The capture groups found in the string.
- success: True if the regex matched the string.
match_iterator
match_iterator :: proc(it: ^Match_Iterator) -> (result: Capture, index: int, ok: bool)SourceIterate over a Match_Iterator and return successive captures.
Inputs:
- it: Pointer to the
Match_Iteratorto iterate over.
Returns:
- result:
Capturefor this iteration. - ok: A bool indicating if there was a match, stopping the iteration on
false.
match_with_preallocated_capture
match_with_preallocated_capture :: proc(regex: Regular_Expression, str: string, capture: ^Capture, temporary_allocator = context.temp_allocator) -> (num_groups: int, success: bool)SourceMatch a regular expression against a string and save the capture results into the provided capture structure.
The resulting capture strings will be slices to the string str, not wholly copied strings, so they won't need to be individually deleted.
Allocates Using Provided Allocator
Inputs:
- regex: The regular expression.
- str: The string to match against.
- capture: A pointer to a Capture structure with
groupsandposalready allocated. - temporary_allocator: The allocator to use for the virtual machine. (default: context.temp_allocator)
Returns:
- num_groups: The number of capture groups set into
capture. - success: True if the regex matched the string.
preallocate_capture
preallocate_capture :: proc(allocator: mem.Allocator = context.allocator) -> (result: Capture)SourceAllocate a Capture in advance for use with match. This can save some time if you plan on performing several matches at once and only need the results between matches.
Inputs:
- allocator: (default: context.allocator)
Returns:
- result: The
Capturewith the maximum number of groups allocated.
reset
reset :: proc(it: ^Match_Iterator)SourceReset an iterator, allowing it to be run again as if new.
Inputs:
- it: The iterator to reset.