Merge pull request #4555 from ycros/fix-regex-num-groups

fix: regex's match_with_preallocated_capture returning num_groups
This commit is contained in:
Laytan
2024-12-04 12:46:57 +01:00
committed by GitHub
2 changed files with 38 additions and 0 deletions

View File

@@ -381,6 +381,7 @@ match_with_preallocated_capture :: proc(
capture.pos[n] = {a, b}
n += 1
}
num_groups = n
}
return

View File

@@ -1043,3 +1043,40 @@ test_us_phone_number :: proc(t: ^testing.T) {
EXPR :: `^[2-9]\d{2}-\d{3}-\d{4}$`
check_expression(t, EXPR, "650-253-0001", "650-253-0001")
}
@test
test_preallocated_capture :: proc(t: ^testing.T) {
capture := regex.preallocate_capture()
defer regex.destroy(capture)
for pos in capture.pos {
testing.expect_value(t, pos, [2]int{0, 0})
}
for group in capture.groups {
testing.expect_value(t, group, "")
}
rex, parse_err := regex.create(`f(o)ob(ar)`)
if !testing.expect_value(t, parse_err, nil) {
return
}
defer regex.destroy(rex)
num_groups, success := regex.match_with_preallocated_capture(rex, "foobar", &capture)
testing.expect_value(t, num_groups, 3)
testing.expect_value(t, success, true)
testing.expect_value(t, capture.pos[0], [2]int{0, 6})
testing.expect_value(t, capture.pos[1], [2]int{1, 2})
testing.expect_value(t, capture.pos[2], [2]int{4, 6})
for pos in capture.pos[3:] {
testing.expect_value(t, pos, [2]int{0, 0})
}
testing.expect_value(t, capture.groups[0], "foobar")
testing.expect_value(t, capture.groups[1], "o")
testing.expect_value(t, capture.groups[2], "ar")
for groups in capture.groups[3:] {
testing.expect_value(t, groups, "")
}
}