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
= malloc(len
+ 1);
38 len
= text_bytes_get(txt
, pos
, len
, buf
);
40 regmatch_t match
[nmatch
];
41 int ret
= regexec(&r
->regex
, buf
, nmatch
, match
, eflags
);
43 for (size_t i
= 0; i
< nmatch
; i
++) {
44 pmatch
[i
].start
= match
[i
].rm_so
== -1 ? EPOS
: pos
+ match
[i
].rm_so
;
45 pmatch
[i
].end
= match
[i
].rm_eo
== -1 ? EPOS
: pos
+ match
[i
].rm_eo
;
52 int text_search_range_backward(Text
*txt
, size_t pos
, size_t len
, Regex
*r
, size_t nmatch
, RegexMatch pmatch
[], int eflags
) {
53 char *buf
= malloc(len
+ 1);
56 len
= text_bytes_get(txt
, pos
, len
, buf
);
58 regmatch_t match
[nmatch
];
60 int ret
= REG_NOMATCH
;
61 while (!regexec(&r
->regex
, cur
, nmatch
, match
, eflags
)) {
63 for (size_t i
= 0; i
< nmatch
; i
++) {
64 pmatch
[i
].start
= match
[i
].rm_so
== -1 ? EPOS
: pos
+ (size_t)(cur
- buf
) + match
[i
].rm_so
;
65 pmatch
[i
].end
= match
[i
].rm_eo
== -1 ? EPOS
: pos
+ (size_t)(cur
- buf
) + match
[i
].rm_eo
;
67 cur
+= match
[0].rm_eo
;