mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-05 15:18:49 +00:00
fix styling issues and use switches in cases its necessary, add comments to helpers
This commit is contained in:
+200
-291
@@ -5,7 +5,7 @@ import "core:unicode"
|
|||||||
import "core:unicode/utf8"
|
import "core:unicode/utf8"
|
||||||
import "core:strings"
|
import "core:strings"
|
||||||
|
|
||||||
MAXCAPTURES :: 32
|
MAX_CAPTURES :: 32
|
||||||
|
|
||||||
Capture :: struct {
|
Capture :: struct {
|
||||||
init: int,
|
init: int,
|
||||||
@@ -32,71 +32,52 @@ CAP_POSITION :: -2
|
|||||||
CAP_UNFINISHED :: -1
|
CAP_UNFINISHED :: -1
|
||||||
INVALID :: -1
|
INVALID :: -1
|
||||||
|
|
||||||
MatchState :: struct {
|
Match_State :: struct {
|
||||||
src: string,
|
src: string,
|
||||||
pattern: string,
|
pattern: string,
|
||||||
level: int,
|
level: int,
|
||||||
capture: [MAXCAPTURES]Capture,
|
capture: [MAX_CAPTURES]Capture,
|
||||||
}
|
}
|
||||||
|
|
||||||
match_class :: proc(c: rune, cl: rune) -> (res: bool) {
|
match_class :: proc(c: rune, cl: rune) -> (res: bool) {
|
||||||
switch unicode.to_lower(cl) {
|
switch unicode.to_lower(cl) {
|
||||||
case 'a': res = isalpha(c)
|
case 'a': res = is_alpha(c)
|
||||||
case 'c': res = iscntrl(c)
|
case 'c': res = is_cntrl(c)
|
||||||
case 'd': res = isdigit(c)
|
case 'd': res = is_digit(c)
|
||||||
case 'g': res = isgraph(c)
|
case 'g': res = is_graph(c)
|
||||||
case 'l': res = islower(c)
|
case 'l': res = is_lower(c)
|
||||||
case 'p': res = ispunct(c)
|
case 'p': res = is_punct(c)
|
||||||
case 's': res = isspace(c)
|
case 's': res = is_space(c)
|
||||||
case 'u': res = isupper(c)
|
case 'u': res = is_upper(c)
|
||||||
case 'w': res = isalnum(c)
|
case 'w': res = is_alnum(c)
|
||||||
case 'x': res = isxdigit(c)
|
case 'x': res = is_xdigit(c)
|
||||||
case: return cl == c
|
case: return cl == c
|
||||||
}
|
}
|
||||||
|
|
||||||
return islower(cl) ? res : !res
|
return is_lower(cl) ? res : !res
|
||||||
}
|
}
|
||||||
|
|
||||||
isalpha :: proc(c: rune) -> bool {
|
is_alpha :: unicode.is_alpha
|
||||||
return unicode.is_alpha(c)
|
is_digit :: unicode.is_digit
|
||||||
}
|
is_lower :: unicode.is_lower
|
||||||
|
is_upper :: unicode.is_upper
|
||||||
|
is_punct :: unicode.is_punct
|
||||||
|
is_space :: unicode.is_space
|
||||||
|
is_cntrl :: unicode.is_control
|
||||||
|
|
||||||
isdigit :: proc(c: rune) -> bool {
|
is_alnum :: proc(c: rune) -> bool {
|
||||||
return unicode.is_digit(c)
|
|
||||||
}
|
|
||||||
|
|
||||||
isalnum :: proc(c: rune) -> bool {
|
|
||||||
return unicode.is_alpha(c) || unicode.is_digit(c)
|
return unicode.is_alpha(c) || unicode.is_digit(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
iscntrl :: proc(c: rune) -> bool {
|
is_graph :: proc(c: rune) -> bool {
|
||||||
return unicode.is_control(c)
|
return (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F') || unicode.is_digit(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
islower :: proc(c: rune) -> bool {
|
is_xdigit :: proc(c: rune) -> bool {
|
||||||
return unicode.is_lower(c)
|
return (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F') || unicode.is_digit(c)
|
||||||
}
|
|
||||||
|
|
||||||
isupper :: proc(c: rune) -> bool {
|
|
||||||
return unicode.is_upper(c)
|
|
||||||
}
|
|
||||||
|
|
||||||
isgraph :: proc(c: rune) -> bool {
|
|
||||||
return unicode.is_digit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')
|
|
||||||
}
|
|
||||||
|
|
||||||
ispunct :: proc(c: rune) -> bool {
|
|
||||||
return unicode.is_punct(c)
|
|
||||||
}
|
|
||||||
|
|
||||||
isxdigit :: proc(c: rune) -> bool {
|
|
||||||
return unicode.is_digit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')
|
|
||||||
}
|
|
||||||
|
|
||||||
isspace :: proc(c: rune) -> bool {
|
|
||||||
return unicode.is_space(c)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// find the first utf8 charater and its size, return an error if the character is an error
|
||||||
utf8_peek :: proc(bytes: string) -> (c: rune, size: int, err: Error) {
|
utf8_peek :: proc(bytes: string) -> (c: rune, size: int, err: Error) {
|
||||||
c, size = utf8.decode_rune_in_string(bytes)
|
c, size = utf8.decode_rune_in_string(bytes)
|
||||||
|
|
||||||
@@ -107,6 +88,8 @@ utf8_peek :: proc(bytes: string) -> (c: rune, size: int, err: Error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// find the first utf8 charater and its size and advance the index
|
||||||
|
// return an error if the character is an error
|
||||||
utf8_advance :: proc(bytes: string, index: ^int) -> (c: rune, err: Error) {
|
utf8_advance :: proc(bytes: string, index: ^int) -> (c: rune, err: Error) {
|
||||||
size: int
|
size: int
|
||||||
c, size = utf8.decode_rune_in_string(bytes[index^:])
|
c, size = utf8.decode_rune_in_string(bytes[index^:])
|
||||||
@@ -145,7 +128,7 @@ utf8_next :: proc(bytes: string, a: int) -> int {
|
|||||||
return a < b ? a + 1 : b
|
return a < b ? a + 1 : b
|
||||||
}
|
}
|
||||||
|
|
||||||
check_capture :: proc(ms: ^MatchState, l: rune) -> (int, Error) {
|
check_capture :: proc(ms: ^Match_State, l: rune) -> (int, Error) {
|
||||||
l := int(l - '1')
|
l := int(l - '1')
|
||||||
|
|
||||||
if l < 0 || l >= ms.level || ms.capture[l].len == CAP_UNFINISHED {
|
if l < 0 || l >= ms.level || ms.capture[l].len == CAP_UNFINISHED {
|
||||||
@@ -155,7 +138,7 @@ check_capture :: proc(ms: ^MatchState, l: rune) -> (int, Error) {
|
|||||||
return l, .OK
|
return l, .OK
|
||||||
}
|
}
|
||||||
|
|
||||||
capture_to_close :: proc(ms: ^MatchState) -> (int, Error) {
|
capture_to_close :: proc(ms: ^Match_State) -> (int, Error) {
|
||||||
level := ms.level - 1
|
level := ms.level - 1
|
||||||
|
|
||||||
for level >= 0 {
|
for level >= 0 {
|
||||||
@@ -169,55 +152,53 @@ capture_to_close :: proc(ms: ^MatchState) -> (int, Error) {
|
|||||||
return 0, .Invalid_Pattern_Capture
|
return 0, .Invalid_Pattern_Capture
|
||||||
}
|
}
|
||||||
|
|
||||||
classend :: proc(ms: ^MatchState, p: int) -> (step: int, err: Error) {
|
class_end :: proc(ms: ^Match_State, p: int) -> (step: int, err: Error) {
|
||||||
step = p
|
step = p
|
||||||
ch := utf8_advance(ms.pattern, &step) or_return
|
ch := utf8_advance(ms.pattern, &step) or_return
|
||||||
|
|
||||||
switch ch {
|
switch ch {
|
||||||
case L_ESC: {
|
case L_ESC:
|
||||||
|
if step == len(ms.pattern) {
|
||||||
|
err = .Malformed_Pattern
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
utf8_advance(ms.pattern, &step) or_return
|
||||||
|
|
||||||
|
case '[':
|
||||||
|
// fine with step by 1
|
||||||
|
if step + 1 < len(ms.pattern) && ms.pattern[step] == '^' {
|
||||||
|
step += 1
|
||||||
|
}
|
||||||
|
|
||||||
|
// run till end is reached
|
||||||
|
for {
|
||||||
if step == len(ms.pattern) {
|
if step == len(ms.pattern) {
|
||||||
err = .Malformed_Pattern
|
err = .Malformed_Pattern
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
utf8_advance(ms.pattern, &step) or_return
|
if ms.pattern[step] == ']' {
|
||||||
}
|
break
|
||||||
|
|
||||||
case '[': {
|
|
||||||
// fine with step by 1
|
|
||||||
if step + 1 < len(ms.pattern) && ms.pattern[step] == '^' {
|
|
||||||
step += 1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// run till end is reached
|
// dont care about utf8 here
|
||||||
for {
|
|
||||||
if step == len(ms.pattern) {
|
|
||||||
err = .Malformed_Pattern
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if ms.pattern[step] == ']' {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
// dont care about utf8 here
|
|
||||||
step += 1
|
|
||||||
|
|
||||||
if step < len(ms.pattern) && ms.pattern[step] == L_ESC {
|
|
||||||
// skip escapes like '%'
|
|
||||||
step += 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// advance last time
|
|
||||||
step += 1
|
step += 1
|
||||||
|
|
||||||
|
if step < len(ms.pattern) && ms.pattern[step] == L_ESC {
|
||||||
|
// skip escapes like '%'
|
||||||
|
step += 1
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// advance last time
|
||||||
|
step += 1
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
matchbracketclass :: proc(ms: ^MatchState, c: rune, p, ec: int) -> (sig: bool, err: Error) {
|
match_bracket_class :: proc(ms: ^Match_State, c: rune, p, ec: int) -> (sig: bool, err: Error) {
|
||||||
sig = true
|
sig = true
|
||||||
p := p
|
p := p
|
||||||
|
|
||||||
@@ -259,7 +240,7 @@ matchbracketclass :: proc(ms: ^MatchState, c: rune, p, ec: int) -> (sig: bool, e
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
singlematch :: proc(ms: ^MatchState, s, p, ep: int) -> (matched: bool, schar_size: int, err: Error) {
|
single_match :: proc(ms: ^Match_State, s, p, ep: int) -> (matched: bool, schar_size: int, err: Error) {
|
||||||
if s >= len(ms.src) {
|
if s >= len(ms.src) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -269,23 +250,18 @@ singlematch :: proc(ms: ^MatchState, s, p, ep: int) -> (matched: bool, schar_siz
|
|||||||
schar_size = ssize
|
schar_size = ssize
|
||||||
|
|
||||||
switch pchar {
|
switch pchar {
|
||||||
case '.': matched = true
|
case '.': matched = true
|
||||||
case L_ESC: {
|
case L_ESC:
|
||||||
pchar_next, _ := utf8_peek(ms.pattern[p + psize:]) or_return
|
pchar_next, _ := utf8_peek(ms.pattern[p + psize:]) or_return
|
||||||
matched = match_class(schar, pchar_next)
|
matched = match_class(schar, pchar_next)
|
||||||
}
|
case '[': matched = match_bracket_class(ms, schar, p, ep - 1) or_return
|
||||||
case '[': {
|
case: matched = schar == pchar
|
||||||
matched = matchbracketclass(ms, schar, p, ep - 1) or_return
|
|
||||||
}
|
|
||||||
case: {
|
|
||||||
matched = schar == pchar
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
matchbalance :: proc(ms: ^MatchState, s, p: int) -> (unused: int, err: Error) {
|
match_balance :: proc(ms: ^Match_State, s, p: int) -> (unused: int, err: Error) {
|
||||||
if p >= len(ms.pattern) - 1 {
|
if p >= len(ms.pattern) - 1 {
|
||||||
return INVALID, .Invalid_Pattern_Capture
|
return INVALID, .Invalid_Pattern_Capture
|
||||||
}
|
}
|
||||||
@@ -307,13 +283,15 @@ matchbalance :: proc(ms: ^MatchState, s, p: int) -> (unused: int, err: Error) {
|
|||||||
for s < len(ms.src) {
|
for s < len(ms.src) {
|
||||||
ch := utf8_advance(ms.src, &s) or_return
|
ch := utf8_advance(ms.src, &s) or_return
|
||||||
|
|
||||||
if ch == end {
|
switch ch{
|
||||||
|
case end:
|
||||||
cont -= 1
|
cont -= 1
|
||||||
|
|
||||||
if cont == 0 {
|
if cont == 0 {
|
||||||
return s, .OK
|
return s, .OK
|
||||||
}
|
}
|
||||||
} else if ch == begin {
|
|
||||||
|
case begin:
|
||||||
cont += 1
|
cont += 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -321,12 +299,12 @@ matchbalance :: proc(ms: ^MatchState, s, p: int) -> (unused: int, err: Error) {
|
|||||||
return INVALID, .OK
|
return INVALID, .OK
|
||||||
}
|
}
|
||||||
|
|
||||||
max_expand :: proc(ms: ^MatchState, s, p, ep: int) -> (res: int, err: Error) {
|
max_expand :: proc(ms: ^Match_State, s, p, ep: int) -> (res: int, err: Error) {
|
||||||
m := s
|
m := s
|
||||||
|
|
||||||
// count up matches
|
// count up matches
|
||||||
for {
|
for {
|
||||||
matched, size := singlematch(ms, m, p, ep) or_return
|
matched, size := single_match(ms, m, p, ep) or_return
|
||||||
|
|
||||||
if !matched {
|
if !matched {
|
||||||
break
|
break
|
||||||
@@ -352,7 +330,7 @@ max_expand :: proc(ms: ^MatchState, s, p, ep: int) -> (res: int, err: Error) {
|
|||||||
return INVALID, .OK
|
return INVALID, .OK
|
||||||
}
|
}
|
||||||
|
|
||||||
min_expand :: proc(ms: ^MatchState, s, p, ep: int) -> (res: int, err: Error) {
|
min_expand :: proc(ms: ^Match_State, s, p, ep: int) -> (res: int, err: Error) {
|
||||||
s := s
|
s := s
|
||||||
|
|
||||||
for {
|
for {
|
||||||
@@ -362,7 +340,7 @@ min_expand :: proc(ms: ^MatchState, s, p, ep: int) -> (res: int, err: Error) {
|
|||||||
return result, .OK
|
return result, .OK
|
||||||
} else {
|
} else {
|
||||||
// TODO receive next step maybe?
|
// TODO receive next step maybe?
|
||||||
matched, rune_size := singlematch(ms, s, p, ep) or_return
|
matched, rune_size := single_match(ms, s, p, ep) or_return
|
||||||
|
|
||||||
if matched {
|
if matched {
|
||||||
s += rune_size
|
s += rune_size
|
||||||
@@ -373,7 +351,7 @@ min_expand :: proc(ms: ^MatchState, s, p, ep: int) -> (res: int, err: Error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
start_capture :: proc(ms: ^MatchState, s, p, what: int) -> (res: int, err: Error) {
|
start_capture :: proc(ms: ^Match_State, s, p, what: int) -> (res: int, err: Error) {
|
||||||
level := ms.level
|
level := ms.level
|
||||||
|
|
||||||
ms.capture[level].init = s
|
ms.capture[level].init = s
|
||||||
@@ -387,7 +365,7 @@ start_capture :: proc(ms: ^MatchState, s, p, what: int) -> (res: int, err: Error
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
end_capture :: proc(ms: ^MatchState, s, p: int) -> (res: int, err: Error) {
|
end_capture :: proc(ms: ^Match_State, s, p: int) -> (res: int, err: Error) {
|
||||||
l := capture_to_close(ms) or_return
|
l := capture_to_close(ms) or_return
|
||||||
|
|
||||||
// TODO double check, could do string as int index
|
// TODO double check, could do string as int index
|
||||||
@@ -400,7 +378,7 @@ end_capture :: proc(ms: ^MatchState, s, p: int) -> (res: int, err: Error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
match_capture :: proc(ms: ^MatchState, s: int, char: rune) -> (res: int, err: Error) {
|
match_capture :: proc(ms: ^Match_State, s: int, char: rune) -> (res: int, err: Error) {
|
||||||
index := check_capture(ms, char) or_return
|
index := check_capture(ms, char) or_return
|
||||||
length := ms.capture[index].len
|
length := ms.capture[index].len
|
||||||
|
|
||||||
@@ -411,7 +389,7 @@ match_capture :: proc(ms: ^MatchState, s: int, char: rune) -> (res: int, err: Er
|
|||||||
return INVALID, .OK
|
return INVALID, .OK
|
||||||
}
|
}
|
||||||
|
|
||||||
match :: proc(ms: ^MatchState, s, p: int) -> (unused: int, err: Error) {
|
match :: proc(ms: ^Match_State, s, p: int) -> (unused: int, err: Error) {
|
||||||
s := s
|
s := s
|
||||||
p := p
|
p := p
|
||||||
|
|
||||||
@@ -422,156 +400,127 @@ match :: proc(ms: ^MatchState, s, p: int) -> (unused: int, err: Error) {
|
|||||||
// NOTE we can walk by ascii steps if we know the characters are ascii
|
// NOTE we can walk by ascii steps if we know the characters are ascii
|
||||||
char, _ := utf8_peek(ms.pattern[p:]) or_return
|
char, _ := utf8_peek(ms.pattern[p:]) or_return
|
||||||
switch char {
|
switch char {
|
||||||
case '(': {
|
case '(':
|
||||||
if p + 1 < len(ms.pattern) && ms.pattern[p + 1] == ')' {
|
if p + 1 < len(ms.pattern) && ms.pattern[p + 1] == ')' {
|
||||||
s = start_capture(ms, s, p + 2, CAP_POSITION) or_return
|
s = start_capture(ms, s, p + 2, CAP_POSITION) or_return
|
||||||
} else {
|
} else {
|
||||||
s = start_capture(ms, s, p + 1, CAP_UNFINISHED) or_return
|
s = start_capture(ms, s, p + 1, CAP_UNFINISHED) or_return
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
case ')': {
|
case ')':
|
||||||
s = end_capture(ms, s, p + 1) or_return
|
s = end_capture(ms, s, p + 1) or_return
|
||||||
}
|
|
||||||
|
|
||||||
case '$': {
|
case '$':
|
||||||
if p + 1 != len(ms.pattern) {
|
if p + 1 != len(ms.pattern) {
|
||||||
return match_default(ms, s, p)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(ms.src) != s {
|
|
||||||
s = INVALID
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
case L_ESC: {
|
|
||||||
// stop short patterns like "%" only
|
|
||||||
if p + 1 >= len(ms.pattern) {
|
|
||||||
err = .OOB
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
switch ms.pattern[p + 1] {
|
|
||||||
// balanced string
|
|
||||||
case 'b': {
|
|
||||||
s = matchbalance(ms, s, p + 2) or_return
|
|
||||||
|
|
||||||
if s != INVALID {
|
|
||||||
// eg after %b()
|
|
||||||
return match(ms, s, p + 4)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// frontier
|
|
||||||
case 'f': {
|
|
||||||
p += 2
|
|
||||||
|
|
||||||
if ms.pattern[p] != '[' {
|
|
||||||
return INVALID, .Invalid_Pattern_Capture
|
|
||||||
}
|
|
||||||
|
|
||||||
ep := classend(ms, p) or_return
|
|
||||||
previous, current: rune
|
|
||||||
|
|
||||||
// get previous
|
|
||||||
if s != 0 {
|
|
||||||
temp := utf8_prev(ms.src, 0, s)
|
|
||||||
previous, _ = utf8_peek(ms.src[temp:]) or_return
|
|
||||||
}
|
|
||||||
|
|
||||||
// get current
|
|
||||||
if s != len(ms.src) {
|
|
||||||
current, _ = utf8_peek(ms.src[s:]) or_return
|
|
||||||
}
|
|
||||||
|
|
||||||
m1 := matchbracketclass(ms, previous, p, ep - 1) or_return
|
|
||||||
m2 := matchbracketclass(ms, current, p, ep - 1) or_return
|
|
||||||
|
|
||||||
if !m1 && m2 {
|
|
||||||
return match(ms, s, ep)
|
|
||||||
}
|
|
||||||
|
|
||||||
s = INVALID
|
|
||||||
}
|
|
||||||
|
|
||||||
// capture group
|
|
||||||
case '0'..<'9': {
|
|
||||||
s = match_capture(ms, s, rune(ms.pattern[p + 1])) or_return
|
|
||||||
|
|
||||||
if s != INVALID {
|
|
||||||
return match(ms, s, p + 2)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
case: {
|
|
||||||
return match_default(ms, s, p)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
case: {
|
|
||||||
return match_default(ms, s, p)
|
return match_default(ms, s, p)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(ms.src) != s {
|
||||||
|
s = INVALID
|
||||||
|
}
|
||||||
|
|
||||||
|
case L_ESC:
|
||||||
|
// stop short patterns like "%" only
|
||||||
|
if p + 1 >= len(ms.pattern) {
|
||||||
|
err = .OOB
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
switch ms.pattern[p + 1] {
|
||||||
|
// balanced string
|
||||||
|
case 'b':
|
||||||
|
s = match_balance(ms, s, p + 2) or_return
|
||||||
|
|
||||||
|
if s != INVALID {
|
||||||
|
// eg after %b()
|
||||||
|
return match(ms, s, p + 4)
|
||||||
|
}
|
||||||
|
|
||||||
|
// frontier
|
||||||
|
case 'f':
|
||||||
|
p += 2
|
||||||
|
|
||||||
|
if ms.pattern[p] != '[' {
|
||||||
|
return INVALID, .Invalid_Pattern_Capture
|
||||||
|
}
|
||||||
|
|
||||||
|
ep := class_end(ms, p) or_return
|
||||||
|
previous, current: rune
|
||||||
|
|
||||||
|
// get previous
|
||||||
|
if s != 0 {
|
||||||
|
temp := utf8_prev(ms.src, 0, s)
|
||||||
|
previous, _ = utf8_peek(ms.src[temp:]) or_return
|
||||||
|
}
|
||||||
|
|
||||||
|
// get current
|
||||||
|
if s != len(ms.src) {
|
||||||
|
current, _ = utf8_peek(ms.src[s:]) or_return
|
||||||
|
}
|
||||||
|
|
||||||
|
m1 := match_bracket_class(ms, previous, p, ep - 1) or_return
|
||||||
|
m2 := match_bracket_class(ms, current, p, ep - 1) or_return
|
||||||
|
|
||||||
|
if !m1 && m2 {
|
||||||
|
return match(ms, s, ep)
|
||||||
|
}
|
||||||
|
|
||||||
|
s = INVALID
|
||||||
|
|
||||||
|
// capture group
|
||||||
|
case '0'..<'9':
|
||||||
|
s = match_capture(ms, s, rune(ms.pattern[p + 1])) or_return
|
||||||
|
|
||||||
|
if s != INVALID {
|
||||||
|
return match(ms, s, p + 2)
|
||||||
|
}
|
||||||
|
|
||||||
|
case: return match_default(ms, s, p)
|
||||||
|
}
|
||||||
|
|
||||||
|
case:
|
||||||
|
return match_default(ms, s, p)
|
||||||
}
|
}
|
||||||
|
|
||||||
return s, .OK
|
return s, .OK
|
||||||
}
|
}
|
||||||
|
|
||||||
match_default :: proc(ms: ^MatchState, s, p: int) -> (unused: int, err: Error) {
|
match_default :: proc(ms: ^Match_State, s, p: int) -> (unused: int, err: Error) {
|
||||||
s := s
|
s := s
|
||||||
ep := classend(ms, p) or_return
|
ep := class_end(ms, p) or_return
|
||||||
single_matched, ssize := singlematch(ms, s, p, ep) or_return
|
single_matched, ssize := single_match(ms, s, p, ep) or_return
|
||||||
|
|
||||||
if !single_matched {
|
if !single_matched {
|
||||||
epc := ep < len(ms.pattern) ? ms.pattern[ep] : 0
|
epc := ep < len(ms.pattern) ? ms.pattern[ep] : 0
|
||||||
|
|
||||||
if epc == '*' || epc == '?' || epc == '-' {
|
switch epc {
|
||||||
return match(ms, s, ep + 1)
|
case '*', '?', '-': return match(ms, s, ep + 1)
|
||||||
} else {
|
case: s = INVALID
|
||||||
s = INVALID
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
epc := ep < len(ms.pattern) ? ms.pattern[ep] : 0
|
epc := ep < len(ms.pattern) ? ms.pattern[ep] : 0
|
||||||
|
|
||||||
switch epc {
|
switch epc {
|
||||||
case '?': {
|
case '?':
|
||||||
result := match(ms, s + ssize, ep + 1) or_return
|
result := match(ms, s + ssize, ep + 1) or_return
|
||||||
|
|
||||||
if result != INVALID {
|
if result != INVALID {
|
||||||
s = result
|
s = result
|
||||||
} else {
|
} else {
|
||||||
return match(ms, s, ep + 1)
|
return match(ms, s, ep + 1)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
case '+': {
|
case '+': s = max_expand(ms, s + ssize, p, ep) or_return
|
||||||
s = max_expand(ms, s + ssize, p, ep) or_return
|
case '*': s = max_expand(ms, s, p, ep) or_return
|
||||||
}
|
case '-': s = min_expand(ms, s, p, ep) or_return
|
||||||
|
case: return match(ms, s + ssize, ep)
|
||||||
case '*': {
|
|
||||||
s = max_expand(ms, s, p, ep) or_return
|
|
||||||
}
|
|
||||||
|
|
||||||
case '-': {
|
|
||||||
s = min_expand(ms, s, p, ep) or_return
|
|
||||||
}
|
|
||||||
|
|
||||||
case: {
|
|
||||||
return match(ms, s + ssize, ep)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return s, .OK
|
return s, .OK
|
||||||
}
|
}
|
||||||
|
|
||||||
push_onecapture :: proc(
|
push_onecapture :: proc(ms: ^Match_State, i: int, s: int, e: int, matches: []Match) -> (err: Error) {
|
||||||
ms: ^MatchState,
|
|
||||||
i: int,
|
|
||||||
s: int,
|
|
||||||
e: int,
|
|
||||||
matches: []Match,
|
|
||||||
) -> (err: Error) {
|
|
||||||
if i >= ms.level {
|
if i >= ms.level {
|
||||||
if i == 0 {
|
if i == 0 {
|
||||||
matches[0] = { 0, e - s }
|
matches[0] = { 0, e - s }
|
||||||
@@ -583,17 +532,9 @@ push_onecapture :: proc(
|
|||||||
length := ms.capture[i].len
|
length := ms.capture[i].len
|
||||||
|
|
||||||
switch length {
|
switch length {
|
||||||
case CAP_UNFINISHED: {
|
case CAP_UNFINISHED: err = .Unfinished_Capture
|
||||||
err = .Unfinished_Capture
|
case CAP_POSITION: matches[i] = { init, init + 1 }
|
||||||
}
|
case: matches[i] = { init, init + length }
|
||||||
|
|
||||||
case CAP_POSITION: {
|
|
||||||
matches[i] = { init, init + 1 }
|
|
||||||
}
|
|
||||||
|
|
||||||
case: {
|
|
||||||
matches[i] = { init, init + length }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -601,7 +542,7 @@ push_onecapture :: proc(
|
|||||||
}
|
}
|
||||||
|
|
||||||
push_captures :: proc(
|
push_captures :: proc(
|
||||||
ms: ^MatchState,
|
ms: ^Match_State,
|
||||||
s: int,
|
s: int,
|
||||||
e: int,
|
e: int,
|
||||||
matches: []Match,
|
matches: []Match,
|
||||||
@@ -621,8 +562,8 @@ SPECIALS_TABLE := [256]u8 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
|||||||
|
|
||||||
// helper call to quick search for special characters
|
// helper call to quick search for special characters
|
||||||
index_special :: proc(text: string) -> int {
|
index_special :: proc(text: string) -> int {
|
||||||
|
// TODO is this utf8 safe?
|
||||||
for i in 0..<len(text) {
|
for i in 0..<len(text) {
|
||||||
// TODO utf8
|
|
||||||
if SPECIALS_TABLE[text[i]] == 1 {
|
if SPECIALS_TABLE[text[i]] == 1 {
|
||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
@@ -631,7 +572,7 @@ index_special :: proc(text: string) -> int {
|
|||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
|
|
||||||
lmemfind :: proc(s1, s2: string) -> int {
|
lmem_find :: proc(s1, s2: string) -> int {
|
||||||
l1 := len(s1)
|
l1 := len(s1)
|
||||||
l2 := len(s2)
|
l2 := len(s2)
|
||||||
|
|
||||||
@@ -671,14 +612,14 @@ find_aux :: proc(
|
|||||||
pattern: string,
|
pattern: string,
|
||||||
offset: int,
|
offset: int,
|
||||||
allow_memfind: bool,
|
allow_memfind: bool,
|
||||||
matches: ^[MAXCAPTURES]Match,
|
matches: ^[MAX_CAPTURES]Match,
|
||||||
) -> (captures: int, err: Error) {
|
) -> (captures: int, err: Error) {
|
||||||
s := offset
|
s := offset
|
||||||
p := 0
|
p := 0
|
||||||
|
|
||||||
specials_idx := index_special(pattern)
|
specials_idx := index_special(pattern)
|
||||||
if allow_memfind && specials_idx == -1 {
|
if allow_memfind && specials_idx == -1 {
|
||||||
if index := lmemfind(haystack[s:], pattern); index != -1 {
|
if index := lmem_find(haystack[s:], pattern); index != -1 {
|
||||||
matches[0] = { index + s, index + s + len(pattern) }
|
matches[0] = { index + s, index + s + len(pattern) }
|
||||||
captures = 1
|
captures = 1
|
||||||
return
|
return
|
||||||
@@ -694,7 +635,7 @@ find_aux :: proc(
|
|||||||
pattern = pattern[1:]
|
pattern = pattern[1:]
|
||||||
}
|
}
|
||||||
|
|
||||||
ms := MatchState {
|
ms := Match_State {
|
||||||
src = haystack,
|
src = haystack,
|
||||||
pattern = pattern,
|
pattern = pattern,
|
||||||
}
|
}
|
||||||
@@ -733,7 +674,7 @@ find_aux :: proc(
|
|||||||
gmatch :: proc(
|
gmatch :: proc(
|
||||||
haystack: ^string,
|
haystack: ^string,
|
||||||
pattern: string,
|
pattern: string,
|
||||||
captures: ^[MAXCAPTURES]Match,
|
captures: ^[MAX_CAPTURES]Match,
|
||||||
) -> (res: string, ok: bool) {
|
) -> (res: string, ok: bool) {
|
||||||
if len(haystack) > 0 {
|
if len(haystack) > 0 {
|
||||||
length, err := find_aux(haystack^, pattern, 0, false, captures)
|
length, err := find_aux(haystack^, pattern, 0, false, captures)
|
||||||
@@ -758,7 +699,7 @@ gsub_builder :: proc(
|
|||||||
replace: string,
|
replace: string,
|
||||||
) -> string {
|
) -> string {
|
||||||
// find matches
|
// find matches
|
||||||
captures: [MAXCAPTURES]Match
|
captures: [MAX_CAPTURES]Match
|
||||||
haystack := haystack
|
haystack := haystack
|
||||||
|
|
||||||
for {
|
for {
|
||||||
@@ -819,7 +760,7 @@ gsub_with :: proc(
|
|||||||
call: Gsub_Proc,
|
call: Gsub_Proc,
|
||||||
) {
|
) {
|
||||||
// find matches
|
// find matches
|
||||||
captures: [MAXCAPTURES]Match
|
captures: [MAX_CAPTURES]Match
|
||||||
haystack := haystack
|
haystack := haystack
|
||||||
|
|
||||||
for {
|
for {
|
||||||
@@ -846,7 +787,7 @@ gsub :: proc { gsub_builder, gsub_allocator }
|
|||||||
gfind :: proc(
|
gfind :: proc(
|
||||||
haystack: ^string,
|
haystack: ^string,
|
||||||
pattern: string,
|
pattern: string,
|
||||||
captures: ^[MAXCAPTURES]Match,
|
captures: ^[MAX_CAPTURES]Match,
|
||||||
) -> (res: string, ok: bool) {
|
) -> (res: string, ok: bool) {
|
||||||
if len(haystack) > 0 {
|
if len(haystack) > 0 {
|
||||||
length, err := find_aux(haystack^, pattern, 0, true, captures)
|
length, err := find_aux(haystack^, pattern, 0, true, captures)
|
||||||
@@ -901,51 +842,13 @@ pattern_case_insensitive_allocator :: proc(
|
|||||||
|
|
||||||
pattern_case_insensitive :: proc { pattern_case_insensitive_builder, pattern_case_insensitive_allocator }
|
pattern_case_insensitive :: proc { pattern_case_insensitive_builder, pattern_case_insensitive_allocator }
|
||||||
|
|
||||||
find_test :: proc(
|
// Matcher helper struct that stores optional data you might want to use or not
|
||||||
haystack: string,
|
// as lua is far more dynamic this helps dealing with too much data
|
||||||
pattern: string,
|
// this also allows use of find/match/gmatch at through one struct
|
||||||
offset: int = 0,
|
|
||||||
captures: ..^Match,
|
|
||||||
) -> (start, end: int, ok: bool) #no_bounds_check {
|
|
||||||
matches: [MAXCAPTURES]Match
|
|
||||||
length, err := find_aux(haystack, pattern, offset, true, &matches)
|
|
||||||
|
|
||||||
ok = length > 0 && err == .OK
|
|
||||||
match := matches[0]
|
|
||||||
start = match.byte_start
|
|
||||||
end = match.byte_end
|
|
||||||
|
|
||||||
for arg, i in captures {
|
|
||||||
arg^ = matches[i + 1]
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
match_test :: proc(
|
|
||||||
haystack: string,
|
|
||||||
pattern: string,
|
|
||||||
offset: int = 0,
|
|
||||||
captures: ..^Match,
|
|
||||||
) -> (word: string, ok: bool) #no_bounds_check {
|
|
||||||
matches: [MAXCAPTURES]Match
|
|
||||||
length, err := find_aux(haystack, pattern, offset, true, &matches)
|
|
||||||
|
|
||||||
ok = length > 0 && err == .OK
|
|
||||||
match := matches[0]
|
|
||||||
word = haystack[match.byte_start:match.byte_end]
|
|
||||||
|
|
||||||
for arg, i in captures {
|
|
||||||
arg^ = matches[i + 1]
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
Matcher :: struct {
|
Matcher :: struct {
|
||||||
haystack: string,
|
haystack: string,
|
||||||
pattern: string,
|
pattern: string,
|
||||||
captures: [MAXCAPTURES]Match,
|
captures: [MAX_CAPTURES]Match,
|
||||||
captures_length: int,
|
captures_length: int,
|
||||||
offset: int,
|
offset: int,
|
||||||
err: Error,
|
err: Error,
|
||||||
@@ -955,7 +858,7 @@ Matcher :: struct {
|
|||||||
iter_index: int,
|
iter_index: int,
|
||||||
}
|
}
|
||||||
|
|
||||||
// matcher
|
// init using haystack & pattern and an optional byte offset
|
||||||
matcher_init :: proc(haystack, pattern: string, offset: int = 0) -> (res: Matcher) {
|
matcher_init :: proc(haystack, pattern: string, offset: int = 0) -> (res: Matcher) {
|
||||||
res.haystack = haystack
|
res.haystack = haystack
|
||||||
res.pattern = pattern
|
res.pattern = pattern
|
||||||
@@ -964,6 +867,7 @@ matcher_init :: proc(haystack, pattern: string, offset: int = 0) -> (res: Matche
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// find the first match and return the byte start / end position in the string, true on success
|
||||||
matcher_find :: proc(matcher: ^Matcher) -> (start, end: int, ok: bool) #no_bounds_check {
|
matcher_find :: proc(matcher: ^Matcher) -> (start, end: int, ok: bool) #no_bounds_check {
|
||||||
matcher.captures_length, matcher.err = find_aux(
|
matcher.captures_length, matcher.err = find_aux(
|
||||||
matcher.haystack,
|
matcher.haystack,
|
||||||
@@ -979,6 +883,7 @@ matcher_find :: proc(matcher: ^Matcher) -> (start, end: int, ok: bool) #no_bound
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// find the first match and return the matched word, true on success
|
||||||
matcher_match :: proc(matcher: ^Matcher) -> (word: string, ok: bool) #no_bounds_check {
|
matcher_match :: proc(matcher: ^Matcher) -> (word: string, ok: bool) #no_bounds_check {
|
||||||
matcher.captures_length, matcher.err = find_aux(
|
matcher.captures_length, matcher.err = find_aux(
|
||||||
matcher.haystack,
|
matcher.haystack,
|
||||||
@@ -993,20 +898,23 @@ matcher_match :: proc(matcher: ^Matcher) -> (word: string, ok: bool) #no_bounds_
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// get the capture at the correct spot
|
// get the capture at the "correct" spot, as spot 0 is reserved for the first match
|
||||||
matcher_capture :: proc(matcher: ^Matcher, index: int, loc := #caller_location) -> string #no_bounds_check {
|
matcher_capture :: proc(matcher: ^Matcher, index: int, loc := #caller_location) -> string #no_bounds_check {
|
||||||
runtime.bounds_check_error_loc(loc, index + 1, MAXCAPTURES - 1)
|
runtime.bounds_check_error_loc(loc, index + 1, MAX_CAPTURES - 1)
|
||||||
cap := matcher.captures[index + 1]
|
cap := matcher.captures[index + 1]
|
||||||
return matcher.haystack[cap.byte_start:cap.byte_end]
|
return matcher.haystack[cap.byte_start:cap.byte_end]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// get the raw match out of the captures, skipping spot 0
|
||||||
matcher_capture_raw :: proc(matcher: ^Matcher, index: int, loc := #caller_location) -> Match #no_bounds_check {
|
matcher_capture_raw :: proc(matcher: ^Matcher, index: int, loc := #caller_location) -> Match #no_bounds_check {
|
||||||
runtime.bounds_check_error_loc(loc, index + 1, MAXCAPTURES - 1)
|
runtime.bounds_check_error_loc(loc, index + 1, MAX_CAPTURES - 1)
|
||||||
return matcher.captures[index + 1]
|
return matcher.captures[index + 1]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// alias
|
||||||
matcher_gmatch :: matcher_match_iter
|
matcher_gmatch :: matcher_match_iter
|
||||||
|
|
||||||
|
// iteratively match the haystack till it cant find any matches
|
||||||
matcher_match_iter :: proc(matcher: ^Matcher) -> (res: string, index: int, ok: bool) {
|
matcher_match_iter :: proc(matcher: ^Matcher) -> (res: string, index: int, ok: bool) {
|
||||||
if len(matcher.iter) > 0 {
|
if len(matcher.iter) > 0 {
|
||||||
matcher.captures_length, matcher.err = find_aux(
|
matcher.captures_length, matcher.err = find_aux(
|
||||||
@@ -1035,6 +943,7 @@ matcher_match_iter :: proc(matcher: ^Matcher) -> (res: string, index: int, ok: b
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// get a slice of all valid captures above the first match
|
||||||
matcher_captures_slice :: proc(matcher: ^Matcher) -> []Match {
|
matcher_captures_slice :: proc(matcher: ^Matcher) -> []Match {
|
||||||
return matcher.captures[1:matcher.captures_length]
|
return matcher.captures[1:matcher.captures_length]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -196,7 +196,7 @@ test_match :: proc(t: ^testing.T) {
|
|||||||
test_captures :: proc(t: ^testing.T) {
|
test_captures :: proc(t: ^testing.T) {
|
||||||
Temp :: struct {
|
Temp :: struct {
|
||||||
pattern: string,
|
pattern: string,
|
||||||
captures: [lua.MAXCAPTURES]lua.Match,
|
captures: [lua.MAX_CAPTURES]lua.Match,
|
||||||
}
|
}
|
||||||
|
|
||||||
// match all captures
|
// match all captures
|
||||||
@@ -244,7 +244,7 @@ test_captures :: proc(t: ^testing.T) {
|
|||||||
{
|
{
|
||||||
haystack := " 233 hello dolly"
|
haystack := " 233 hello dolly"
|
||||||
pattern := "%s*(%d+)%s+(%S+)"
|
pattern := "%s*(%d+)%s+(%S+)"
|
||||||
captures: [lua.MAXCAPTURES]lua.Match
|
captures: [lua.MAX_CAPTURES]lua.Match
|
||||||
lua.find_aux(haystack, pattern, 0, false, &captures)
|
lua.find_aux(haystack, pattern, 0, false, &captures)
|
||||||
cap1 := captures[1]
|
cap1 := captures[1]
|
||||||
cap2 := captures[2]
|
cap2 := captures[2]
|
||||||
@@ -304,7 +304,7 @@ test_gsub :: proc(t: ^testing.T) {
|
|||||||
test_gfind :: proc(t: ^testing.T) {
|
test_gfind :: proc(t: ^testing.T) {
|
||||||
haystack := "test1 123 test2 123 test3"
|
haystack := "test1 123 test2 123 test3"
|
||||||
pattern := "%w+"
|
pattern := "%w+"
|
||||||
captures: [lua.MAXCAPTURES]lua.Match
|
captures: [lua.MAX_CAPTURES]lua.Match
|
||||||
s := &haystack
|
s := &haystack
|
||||||
output := [?]string { "test1", "123", "test2", "123", "test3" }
|
output := [?]string { "test1", "123", "test2", "123", "test3" }
|
||||||
index: int
|
index: int
|
||||||
|
|||||||
Reference in New Issue
Block a user