this repo has no description
1package addons
2
3import "core:fmt"
4import "core:strings"
5import "core:testing"
6import "core:text/regex"
7
8Capture_Error :: enum {
9 None,
10 No_Capture,
11}
12
13Regex_Error :: union #shared_nil {
14 Capture_Error,
15 regex.Error,
16}
17
18@(private)
19match_and_return_capture :: proc(pattern: string, str: string) -> (string, Regex_Error) {
20 defer free_all(context.temp_allocator)
21 regexp, err := regex.create(pattern, permanent_allocator = context.temp_allocator)
22 if err != nil {
23 return "", err
24 }
25
26 capture, ok := regex.match_and_allocate_capture(regexp, str, context.temp_allocator)
27 if !ok {
28 return "", .No_Capture
29 }
30
31
32 return strings.clone(capture.groups[1]), nil
33}
34
35@(test)
36match_and_return_capture_test__no_capture :: proc(t: ^testing.T) {
37 capture, err := match_and_return_capture(`\<(.*)\>`, "hello world")
38 testing.expect_value(t, err, Capture_Error.No_Capture)
39}
40
41@(test)
42match_and_return_capture_test__malformed_regex :: proc(t: ^testing.T) {
43 capture, err := match_and_return_capture(`?\<(.*)\>`, "<hello world>")
44 testing.expect(t, err != nil)
45}
46
47@(test)
48match_and_return_capture_test :: proc(t: ^testing.T) {
49 capture, err := match_and_return_capture(`\<(.*)\>`, "<hello world>")
50 testing.expect(t, err == nil)
51 testing.expect(t, capture == "hello world")
52 delete(capture)
53}