diff --git a/core/text/lua/strlib.odin b/core/text/lua/strlib.odin index 52b669f6e..703da671b 100644 --- a/core/text/lua/strlib.odin +++ b/core/text/lua/strlib.odin @@ -952,6 +952,7 @@ Matcher :: struct { // changing content for iterators iter: string, + iter_index: int, } // matcher @@ -1006,7 +1007,7 @@ matcher_capture_raw :: proc(matcher: ^Matcher, index: int, loc := #caller_locati 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 { matcher.captures_length, matcher.err = find_aux( matcher.iter, @@ -1020,7 +1021,13 @@ matcher_match_iter :: proc(matcher: ^Matcher) -> (res: string, ok: bool) { ok = true first := matcher.captures_length > 1 ? 1 : 0 match := matcher.captures[first] + + // output 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:] } } diff --git a/tests/core/text/lua/test_core_text_lua.odin b/tests/core/text/lua/test_core_text_lua.odin index ed7d6c58f..b8e561765 100644 --- a/tests/core/text/lua/test_core_text_lua.odin +++ b/tests/core/text/lua/test_core_text_lua.odin @@ -61,21 +61,18 @@ test_find :: proc(t: ^testing.T) { { "helelo", "h.-l", 0, { 0, 3, true } }, } - captures: [lua.MAXCAPTURES]lua.Match for entry, i in ENTRIES { - captures[0] = {} - length, err := lua.find_aux(entry.s, entry.p, entry.offset, true, &captures) - cap := captures[0] - ok := length > 0 && err == .OK - success := entry.match.ok == ok && entry.match.start == cap.byte_start && entry.match.end == cap.byte_end + matcher := lua.matcher_init(entry.s, entry.p, entry.offset) + start, end, ok := lua.matcher_find(&matcher) + success := entry.match.ok == ok && start == entry.match.start && end == entry.match.end if failed(t, success) { logf(t, "Find %d failed!\n", i) 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, "\tEND: %d == %d?\n", entry.match.end, cap.byte_end) - logf(t, "\tErr: %v\tLength %d\n", err, length) - } + logf(t, "\tSTART: %d == %d?\n", entry.match.start, start) + logf(t, "\tEND: %d == %d?\n", entry.match.end, end) + 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 }, } - captures: [lua.MAXCAPTURES]lua.Match for entry, i in ENTRIES { - captures[0] = {} - length, err := lua.find_aux(entry.s, entry.p, 0, false, &captures) - ok := length > 0 && err == .OK - result := entry.s[captures[0].byte_start:captures[0].byte_end] + matcher := lua.matcher_init(entry.s, entry.p) + result, ok := lua.matcher_match(&matcher) success := entry.ok == ok && result == entry.result if failed(t, success) { logf(t, "Match %d failed!\n", i) logf(t, "\tHAYSTACK %s\tPATTERN %s\n", entry.s, entry.p) 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_gmatch :: proc(t: ^testing.T) { - { - haystack := "testing this out 123" - pattern := "%w+" - s := &haystack - captures: [lua.MAXCAPTURES]lua.Match + matcher := lua.matcher_init("testing this out 123", "%w+") output := [?]string { "testing", "this", "out", "123" } - index: int - - for match in lua.gmatch(s, pattern, &captures) { + + for match, index in lua.matcher_gmatch(&matcher) { gmatch_check(t, index, output[:], match) - index += 1 } } { - haystack := "#afdde6" - pattern := "%x%x" - s := &haystack - captures: [lua.MAXCAPTURES]lua.Match + matcher := lua.matcher_init("#afdde6", "%x%x") output := [?]string { "af", "dd", "e6" } - index: int - - for match in lua.gmatch(s, pattern, &captures) { + + for match, index in lua.matcher_gmatch(&matcher) { gmatch_check(t, index, output[:], match) - index += 1 } } { - haystack := "testing outz captures yo outz outtz" - pattern := "(out)z" - s := &haystack - captures: [lua.MAXCAPTURES]lua.Match + matcher := lua.matcher_init("testing outz captures yo outz outtz", "(out)z") 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) - index += 1 } } } @@ -374,17 +352,11 @@ test_frontier :: proc(t: ^testing.T) { @test test_utf8 :: proc(t: ^testing.T) { - { - haystack := "恥ず べき恥 フク恥ロ" - s := &haystack - captures: [lua.MAXCAPTURES]lua.Match - output := [?]string { "恥ず", "べき恥", "フク恥ロ" } - index: int + matcher := lua.matcher_init("恥ず べき恥 フク恥ロ", "%w+") + output := [?]string { "恥ず", "べき恥", "フク恥ロ" } - for word in lua.gmatch(s, "%w+", &captures) { - gmatch_check(t, index, output[:], word) - index += 1 - } + for match, index in lua.matcher_gmatch(&matcher) { + gmatch_check(t, index, output[:], match) } }