core/text/match
text_match
Types
6Capture
Capture :: struct {
init: int,
len: int,
}SourceError
Error :: enum int {
OK = 0,
OOB = 1,
Invalid_Capture_Index = 2,
Invalid_Pattern_Capture = 3,
Unfinished_Capture = 4,
Malformed_Pattern = 5,
Rune_Error = 6,
Match_Invalid = 7,
}SourceGsub_Proc
Gsub_Proc :: proc(
// optional passed data
data: rawptr,
// word match found
word: string,
// current haystack for found captures
haystack: string,
// found captures - empty for no captures
captures: []Match,
)SourceMatch
Match :: struct {
byte_start: int,
byte_end: int,
}SourceMatch_State
Match_State :: struct {
src: string,
pattern: string,
level: int,
capture: [32]Capture,
}SourceMatcher
Matcher :: struct {
haystack: string,
pattern: string,
captures: [32]Match,
captures_length: int,
offset: int,
err: Error,
// changing content for iterators
iter: string,
iter_index: int,
}SourceMatcher helper struct that stores optional data you might want to use or not as lua is far more dynamic this helps dealing with too much data this also allows use of find/match/gmatch at through one struct
Constants
6Variables
1SPECIALS_TABLE
SPECIALS_TABLE :: [256]bool = [256]bool {
'^' = true,
'$' = true,
'*' = true,
'+' = true,
'?' = true,
'.' = true,
'(' = true,
'[' = true,
'%' = true,
'-' = true,
}SourceSPECIALS := "^$*+?.([%-" all special characters inside a small ascii array
Procedures
48capture_to_close
capture_to_close :: proc(ms: ^Match_State) -> (Error, int)Sourcecheck_capture
check_capture :: proc(ms: ^Match_State, l: rune) -> (Error, int)Sourceclass_end
class_end :: proc(ms: ^Match_State, p: int) -> (step: int, err: Error)Sourceend_capture
end_capture :: proc(ms: ^Match_State, s: int, p: int) -> (res: int, err: Error)Sourcefind_aux
find_aux :: proc(haystack: string, pattern: string, offset: int, allow_memfind: bool, matches: ^[32]Match) -> (captures: int, err: Error)Sourcefind a pattern with in a haystack with an offset allow_memfind will speed up simple searches
gfind
gfind :: proc(haystack: ^string, pattern: string, captures: ^[32]Match) -> (res: string, ok: bool)Sourceiterative find with zeroth capture only assumes captures is zeroed on first iteration resets captures to zero on last iteration
gmatch
gmatch :: proc(haystack: ^string, pattern: string, captures: ^[32]Match) -> (res: string, ok: bool)Sourceiterative matching which returns the 0th/1st match rest has to be used from captures assumes captures is zeroed on first iteration resets captures to zero on last iteration
gsub_allocator
gsub_allocator :: proc(haystack: string, pattern: string, replace: string, allocator: mem.Allocator = context.allocator) -> (string)Sourceuses temp builder to build initial string - then allocates the result
gsub_builder
gsub_builder :: proc(builder: ^strings.Builder, haystack: string, pattern: string, replace: string) -> (string)Sourcegsub with builder, replace patterns found with the replace content
gsub_with
gsub_with :: proc(haystack: string, pattern: string, data: rawptr, call: Gsub_Proc)Sourcecall a procedure on every match in the haystack
index_special
index_special :: proc(text: string) -> (int)Sourcehelper call to quick search for special characters
is_alnum
is_alnum :: proc(c: rune) -> (bool)Sourceis_cntrl
is_cntrl :: proc(r: rune) -> (bool)Sourceis_cont
is_cont :: proc(b: u8) -> (bool)Sourcecontinuation byte?
is_digit
is_digit :: proc(r: rune) -> (bool)Sourceis_graph
is_graph :: proc(c: rune) -> (bool)Sourceis_lower
is_lower :: proc(r: rune) -> (bool)Sourceis_punct
is_punct :: proc(r: rune) -> (bool)Sourceis_space
is_space :: proc(r: rune) -> (bool)Sourceis_upper
is_upper :: proc(r: rune) -> (bool)Sourceis_xdigit
is_xdigit :: proc(c: rune) -> (bool)Sourcelmem_find
lmem_find :: proc(s1: string, s2: string) -> (int)Sourcematch
match :: proc(ms: ^Match_State, s: int, p: int) -> (unused: int, err: Error)Sourcematch_balance
match_balance :: proc(ms: ^Match_State, s: int, p: int) -> (unused: int, err: Error)Sourcematch_bracket_class
match_bracket_class :: proc(ms: ^Match_State, c: rune, p: int, ec: int) -> (sig: bool, err: Error)Sourcematch_capture
match_capture :: proc(ms: ^Match_State, s: int, char: rune) -> (res: int, err: Error)Sourcematch_class
match_class :: proc(c: rune, cl: rune) -> (res: bool)Sourcematch_default
match_default :: proc(ms: ^Match_State, s: int, p: int) -> (unused: int, err: Error)Sourcematcher_capture
matcher_capture :: proc(matcher: ^Matcher, index: int, loc = #caller_location) -> (string)Sourceget the capture at the "correct" spot, as spot 0 is reserved for the first match
matcher_capture_raw
matcher_capture_raw :: proc(matcher: ^Matcher, index: int, loc = #caller_location) -> (Match)Sourceget the raw match out of the captures, skipping spot 0
matcher_captures_slice
matcher_captures_slice :: proc(matcher: ^Matcher) -> ([]Match)Sourceget a slice of all valid captures above the first match
matcher_find
matcher_find :: proc(matcher: ^Matcher) -> (start: int, end: int, ok: bool)Sourcefind the first match and return the byte start / end position in the string, true on success
matcher_gmatch
matcher_gmatch :: proc(matcher: ^Matcher) -> (res: string, index: int, ok: bool)Sourcealias
matcher_init
matcher_init :: proc(haystack: string, pattern: string, offset: int) -> (res: Matcher)Sourceinit using haystack & pattern and an optional byte offset
matcher_match
matcher_match :: proc(matcher: ^Matcher) -> (word: string, ok: bool)Sourcefind the first match and return the matched word, true on success
matcher_match_iter
matcher_match_iter :: proc(matcher: ^Matcher) -> (res: string, index: int, ok: bool)Sourceiteratively match the haystack till it cant find any matches
max_expand
max_expand :: proc(ms: ^Match_State, s: int, p: int, ep: int) -> (res: int, err: Error)Sourcemin_expand
min_expand :: proc(ms: ^Match_State, s: int, p: int, ep: int) -> (res: int, err: Error)Sourcepattern_case_insensitive_allocator
pattern_case_insensitive_allocator :: proc(pattern: string, cap: int, allocator: mem.Allocator = context.allocator) -> (string)Sourcepattern_case_insensitive_builder
pattern_case_insensitive_builder :: proc(builder: ^strings.Builder, pattern: string) -> (string)Sourcerebuilds a pattern into a case insensitive pattern
push_captures
push_captures :: proc(ms: ^Match_State, s: int, e: int, matches: []Match) -> (nlevels: int, err: Error)Sourcepush_onecapture
push_onecapture :: proc(ms: ^Match_State, i: int, s: int, e: int, matches: []Match) -> (err: Error)Sourcesingle_match
single_match :: proc(ms: ^Match_State, s: int, p: int, ep: int) -> (matched: bool, schar_size: int, err: Error)Sourcestart_capture
start_capture :: proc(ms: ^Match_State, s: int, p: int, what: int) -> (res: int, err: Error)Sourceutf8_advance
utf8_advance :: proc(bytes: string, index: ^int) -> (c: rune, err: Error)Sourcefind the first utf8 charater and its size and advance the index return an error if the character is an error
utf8_next
utf8_next :: proc(bytes: string, a: int) -> (int)Sourceutf8_peek
utf8_peek :: proc(bytes: string) -> (c: rune, size: int, err: Error)Sourcefind the first utf8 charater and its size, return an error if the character is an error
utf8_prev
utf8_prev :: proc(bytes: string, a: int, b: int) -> (int)Source