mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-06 15:48:51 +00:00
add iter_index and update tests to use easier matcher setup
This commit is contained in:
@@ -952,6 +952,7 @@ Matcher :: struct {
|
|||||||
|
|
||||||
// changing content for iterators
|
// changing content for iterators
|
||||||
iter: string,
|
iter: string,
|
||||||
|
iter_index: int,
|
||||||
}
|
}
|
||||||
|
|
||||||
// matcher
|
// matcher
|
||||||
@@ -1006,7 +1007,7 @@ matcher_capture_raw :: proc(matcher: ^Matcher, index: int, loc := #caller_locati
|
|||||||
|
|
||||||
matcher_gmatch :: matcher_match_iter
|
matcher_gmatch :: matcher_match_iter
|
||||||
|
|
||||||
matcher_match_iter :: proc(matcher: ^Matcher) -> (res: string, 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(
|
||||||
matcher.iter,
|
matcher.iter,
|
||||||
@@ -1020,7 +1021,13 @@ matcher_match_iter :: proc(matcher: ^Matcher) -> (res: string, ok: bool) {
|
|||||||
ok = true
|
ok = true
|
||||||
first := matcher.captures_length > 1 ? 1 : 0
|
first := matcher.captures_length > 1 ? 1 : 0
|
||||||
match := matcher.captures[first]
|
match := matcher.captures[first]
|
||||||
|
|
||||||
|
// output
|
||||||
res = matcher.iter[match.byte_start:match.byte_end]
|
res = matcher.iter[match.byte_start:match.byte_end]
|
||||||
|
index = matcher.iter_index
|
||||||
|
|
||||||
|
// advance
|
||||||
|
matcher.iter_index += 1
|
||||||
matcher.iter = matcher.iter[match.byte_end:]
|
matcher.iter = matcher.iter[match.byte_end:]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -61,21 +61,18 @@ test_find :: proc(t: ^testing.T) {
|
|||||||
{ "helelo", "h.-l", 0, { 0, 3, true } },
|
{ "helelo", "h.-l", 0, { 0, 3, true } },
|
||||||
}
|
}
|
||||||
|
|
||||||
captures: [lua.MAXCAPTURES]lua.Match
|
|
||||||
for entry, i in ENTRIES {
|
for entry, i in ENTRIES {
|
||||||
captures[0] = {}
|
matcher := lua.matcher_init(entry.s, entry.p, entry.offset)
|
||||||
length, err := lua.find_aux(entry.s, entry.p, entry.offset, true, &captures)
|
start, end, ok := lua.matcher_find(&matcher)
|
||||||
cap := captures[0]
|
success := entry.match.ok == ok && start == entry.match.start && end == entry.match.end
|
||||||
ok := length > 0 && err == .OK
|
|
||||||
success := entry.match.ok == ok && entry.match.start == cap.byte_start && entry.match.end == cap.byte_end
|
|
||||||
|
|
||||||
if failed(t, success) {
|
if failed(t, success) {
|
||||||
logf(t, "Find %d failed!\n", i)
|
logf(t, "Find %d failed!\n", i)
|
||||||
logf(t, "\tHAYSTACK %s\tPATTERN %s\n", entry.s, entry.p)
|
logf(t, "\tHAYSTACK %s\tPATTERN %s\n", entry.s, entry.p)
|
||||||
logf(t, "\tSTART: %d == %d?\n", entry.match.start, cap.byte_start)
|
logf(t, "\tSTART: %d == %d?\n", entry.match.start, start)
|
||||||
logf(t, "\tEND: %d == %d?\n", entry.match.end, cap.byte_end)
|
logf(t, "\tEND: %d == %d?\n", entry.match.end, end)
|
||||||
logf(t, "\tErr: %v\tLength %d\n", err, length)
|
logf(t, "\tErr: %v\tLength %d\n", matcher.err, matcher.captures_length)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -181,19 +178,16 @@ test_match :: proc(t: ^testing.T) {
|
|||||||
{ "testing _this_ out", "%b_", "", false },
|
{ "testing _this_ out", "%b_", "", false },
|
||||||
}
|
}
|
||||||
|
|
||||||
captures: [lua.MAXCAPTURES]lua.Match
|
|
||||||
for entry, i in ENTRIES {
|
for entry, i in ENTRIES {
|
||||||
captures[0] = {}
|
matcher := lua.matcher_init(entry.s, entry.p)
|
||||||
length, err := lua.find_aux(entry.s, entry.p, 0, false, &captures)
|
result, ok := lua.matcher_match(&matcher)
|
||||||
ok := length > 0 && err == .OK
|
|
||||||
result := entry.s[captures[0].byte_start:captures[0].byte_end]
|
|
||||||
success := entry.ok == ok && result == entry.result
|
success := entry.ok == ok && result == entry.result
|
||||||
|
|
||||||
if failed(t, success) {
|
if failed(t, success) {
|
||||||
logf(t, "Match %d failed!\n", i)
|
logf(t, "Match %d failed!\n", i)
|
||||||
logf(t, "\tHAYSTACK %s\tPATTERN %s\n", entry.s, entry.p)
|
logf(t, "\tHAYSTACK %s\tPATTERN %s\n", entry.s, entry.p)
|
||||||
logf(t, "\tResults: WANTED %s\tGOT %s\n", entry.result, result)
|
logf(t, "\tResults: WANTED %s\tGOT %s\n", entry.result, result)
|
||||||
logf(t, "\tErr: %v\tLength %d\n", err, length)
|
logf(t, "\tErr: %v\tLength %d\n", matcher.err, matcher.captures_length)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -270,46 +264,30 @@ gmatch_check :: proc(t: ^testing.T, index: int, a: []string, b: string) {
|
|||||||
|
|
||||||
@test
|
@test
|
||||||
test_gmatch :: proc(t: ^testing.T) {
|
test_gmatch :: proc(t: ^testing.T) {
|
||||||
|
|
||||||
{
|
{
|
||||||
haystack := "testing this out 123"
|
matcher := lua.matcher_init("testing this out 123", "%w+")
|
||||||
pattern := "%w+"
|
|
||||||
s := &haystack
|
|
||||||
captures: [lua.MAXCAPTURES]lua.Match
|
|
||||||
output := [?]string { "testing", "this", "out", "123" }
|
output := [?]string { "testing", "this", "out", "123" }
|
||||||
index: int
|
|
||||||
|
for match, index in lua.matcher_gmatch(&matcher) {
|
||||||
for match in lua.gmatch(s, pattern, &captures) {
|
|
||||||
gmatch_check(t, index, output[:], match)
|
gmatch_check(t, index, output[:], match)
|
||||||
index += 1
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
haystack := "#afdde6"
|
matcher := lua.matcher_init("#afdde6", "%x%x")
|
||||||
pattern := "%x%x"
|
|
||||||
s := &haystack
|
|
||||||
captures: [lua.MAXCAPTURES]lua.Match
|
|
||||||
output := [?]string { "af", "dd", "e6" }
|
output := [?]string { "af", "dd", "e6" }
|
||||||
index: int
|
|
||||||
|
for match, index in lua.matcher_gmatch(&matcher) {
|
||||||
for match in lua.gmatch(s, pattern, &captures) {
|
|
||||||
gmatch_check(t, index, output[:], match)
|
gmatch_check(t, index, output[:], match)
|
||||||
index += 1
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
haystack := "testing outz captures yo outz outtz"
|
matcher := lua.matcher_init("testing outz captures yo outz outtz", "(out)z")
|
||||||
pattern := "(out)z"
|
|
||||||
s := &haystack
|
|
||||||
captures: [lua.MAXCAPTURES]lua.Match
|
|
||||||
output := [?]string { "out", "out" }
|
output := [?]string { "out", "out" }
|
||||||
index: int
|
|
||||||
|
|
||||||
for match in lua.gmatch(s, pattern, &captures) {
|
for match, index in lua.matcher_gmatch(&matcher) {
|
||||||
gmatch_check(t, index, output[:], match)
|
gmatch_check(t, index, output[:], match)
|
||||||
index += 1
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -374,17 +352,11 @@ test_frontier :: proc(t: ^testing.T) {
|
|||||||
|
|
||||||
@test
|
@test
|
||||||
test_utf8 :: proc(t: ^testing.T) {
|
test_utf8 :: proc(t: ^testing.T) {
|
||||||
{
|
matcher := lua.matcher_init("恥ず べき恥 フク恥ロ", "%w+")
|
||||||
haystack := "恥ず べき恥 フク恥ロ"
|
output := [?]string { "恥ず", "べき恥", "フク恥ロ" }
|
||||||
s := &haystack
|
|
||||||
captures: [lua.MAXCAPTURES]lua.Match
|
|
||||||
output := [?]string { "恥ず", "べき恥", "フク恥ロ" }
|
|
||||||
index: int
|
|
||||||
|
|
||||||
for word in lua.gmatch(s, "%w+", &captures) {
|
for match, index in lua.matcher_gmatch(&matcher) {
|
||||||
gmatch_check(t, index, output[:], word)
|
gmatch_check(t, index, output[:], match)
|
||||||
index += 1
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user