4 #include "text-regex.h"
10 Regex
*text_regex_new(void) {
11 Regex
*r
= calloc(1, sizeof(Regex
));
14 regcomp(&r
->regex
, "\0\0", 0); /* this should not match anything */
18 int text_regex_compile(Regex
*regex
, const char *string
, int cflags
) {
19 int r
= regcomp(®ex
->regex
, string
, cflags
);
21 regcomp(®ex
->regex
, "\0\0", 0);
25 void text_regex_free(Regex
*r
) {
32 int text_search_range_forward(Text
*txt
, size_t pos
, size_t len
, Regex
*r
, size_t nmatch
, RegexMatch pmatch
[], int eflags
) {
33 char *buf
= text_bytes_alloc0(txt
, pos
, len
);
36 regmatch_t match
[nmatch
];
37 int ret
= regexec(&r
->regex
, buf
, nmatch
, match
, eflags
);
39 for (size_t i
= 0; i
< nmatch
; i
++) {
40 pmatch
[i
].start
= match
[i
].rm_so
== -1 ? EPOS
: pos
+ match
[i
].rm_so
;
41 pmatch
[i
].end
= match
[i
].rm_eo
== -1 ? EPOS
: pos
+ match
[i
].rm_eo
;
48 int text_search_range_backward(Text
*txt
, size_t pos
, size_t len
, Regex
*r
, size_t nmatch
, RegexMatch pmatch
[], int eflags
) {
49 char *buf
= text_bytes_alloc0(txt
, pos
, len
);
52 regmatch_t match
[nmatch
];
54 int ret
= REG_NOMATCH
;
55 while (!regexec(&r
->regex
, cur
, nmatch
, match
, eflags
)) {
57 for (size_t i
= 0; i
< nmatch
; i
++) {
58 pmatch
[i
].start
= match
[i
].rm_so
== -1 ? EPOS
: pos
+ (size_t)(cur
- buf
) + match
[i
].rm_so
;
59 pmatch
[i
].end
= match
[i
].rm_eo
== -1 ? EPOS
: pos
+ (size_t)(cur
- buf
) + match
[i
].rm_eo
;
61 cur
+= match
[0].rm_eo
;