4 #include "text-regex.h"
11 Regex
*text_regex_new(void) {
12 Regex
*r
= calloc(1, sizeof(Regex
));
15 regcomp(&r
->regex
, "\0\0", 0); /* this should not match anything */
19 int text_regex_compile(Regex
*regex
, const char *string
, int cflags
) {
20 regex
->string
= string
;
21 int r
= regcomp(®ex
->regex
, string
, cflags
);
23 regcomp(®ex
->regex
, "\0\0", 0);
27 void text_regex_free(Regex
*r
) {
34 int text_search_range_forward(Text
*txt
, size_t pos
, size_t len
, Regex
*r
, size_t nmatch
, RegexMatch pmatch
[], int eflags
) {
35 char *buf
= text_bytes_alloc0(txt
, pos
, len
);
38 regmatch_t match
[nmatch
];
39 int ret
= regexec(&r
->regex
, buf
, nmatch
, match
, eflags
);
41 for (size_t i
= 0; i
< nmatch
; i
++) {
42 pmatch
[i
].start
= match
[i
].rm_so
== -1 ? EPOS
: pos
+ match
[i
].rm_so
;
43 pmatch
[i
].end
= match
[i
].rm_eo
== -1 ? EPOS
: pos
+ match
[i
].rm_eo
;
50 int text_search_range_backward(Text
*txt
, size_t pos
, size_t len
, Regex
*r
, size_t nmatch
, RegexMatch pmatch
[], int eflags
) {
51 char *buf
= text_bytes_alloc0(txt
, pos
, len
);
54 regmatch_t match
[nmatch
];
56 int ret
= REG_NOMATCH
;
57 while (!regexec(&r
->regex
, cur
, nmatch
, match
, eflags
)) {
59 for (size_t i
= 0; i
< nmatch
; i
++) {
60 pmatch
[i
].start
= match
[i
].rm_so
== -1 ? EPOS
: pos
+ (size_t)(cur
- buf
) + match
[i
].rm_so
;
61 pmatch
[i
].end
= match
[i
].rm_eo
== -1 ? EPOS
: pos
+ (size_t)(cur
- buf
) + match
[i
].rm_eo
;
63 cur
+= match
[0].rm_eo
;