5 #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 int r
= regcomp(®ex
->regex
, string
, cflags
);
22 regcomp(®ex
->regex
, "\0\0", 0);
26 void text_regex_free(Regex
*r
) {
33 int text_search_range_forward(Text
*txt
, size_t pos
, size_t len
, Regex
*r
, size_t nmatch
, RegexMatch pmatch
[], int eflags
) {
34 char *buf
= text_bytes_alloc0(txt
, pos
, len
);
37 regmatch_t match
[nmatch
];
38 int ret
= regexec(&r
->regex
, buf
, nmatch
, match
, eflags
);
40 for (size_t i
= 0; i
< nmatch
; i
++) {
41 pmatch
[i
].start
= match
[i
].rm_so
== -1 ? EPOS
: pos
+ match
[i
].rm_so
;
42 pmatch
[i
].end
= match
[i
].rm_eo
== -1 ? EPOS
: pos
+ match
[i
].rm_eo
;
49 int text_search_range_backward(Text
*txt
, size_t pos
, size_t len
, Regex
*r
, size_t nmatch
, RegexMatch pmatch
[], int eflags
) {
50 char *buf
= text_bytes_alloc0(txt
, pos
, len
);
53 regmatch_t match
[nmatch
];
55 int ret
= REG_NOMATCH
;
56 while (!regexec(&r
->regex
, cur
, nmatch
, match
, eflags
)) {
58 for (size_t i
= 0; i
< nmatch
; i
++) {
59 pmatch
[i
].start
= match
[i
].rm_so
== -1 ? EPOS
: pos
+ (size_t)(cur
- buf
) + match
[i
].rm_so
;
60 pmatch
[i
].end
= match
[i
].rm_eo
== -1 ? EPOS
: pos
+ (size_t)(cur
- buf
) + match
[i
].rm_eo
;
62 if (match
[0].rm_so
== 0 && match
[0].rm_eo
== 0) {
63 /* empty match at the beginning of cur, advance to next line */
64 if ((cur
= strchr(cur
, '\n')))
70 cur
+= match
[0].rm_eo
;