4 #include "text-regex.h"
5 #include "text-motions.h"
9 tre_str_source str_source
;
15 size_t text_regex_nsub(Regex
*r
) {
18 return r
->regex
.re_nsub
;
21 static int str_next_char(tre_char_t
*c
, unsigned int *pos_add
, void *context
) {
24 if (r
->it
.pos
< r
->end
&& text_iterator_byte_get(&r
->it
, (char*)c
)) {
25 text_iterator_byte_next(&r
->it
, NULL
);
33 static void str_rewind(size_t pos
, void *context
) {
35 r
->it
= text_iterator_get(r
->text
, pos
);
38 static int str_compare(size_t pos1
, size_t pos2
, size_t len
, void *context
) {
41 void *buf1
= malloc(len
), *buf2
= malloc(len
);
44 text_bytes_get(r
->text
, pos1
, len
, buf1
);
45 text_bytes_get(r
->text
, pos2
, len
, buf2
);
46 ret
= memcmp(buf1
, buf2
, len
);
53 Regex
*text_regex_new(void) {
54 Regex
*r
= calloc(1, sizeof(*r
));
57 r
->str_source
= (tre_str_source
) {
58 .get_next_char
= str_next_char
,
60 .compare
= str_compare
,
66 void text_regex_free(Regex
*r
) {
69 tre_regfree(&r
->regex
);
73 int text_regex_compile(Regex
*regex
, const char *string
, int cflags
) {
74 int r
= tre_regcomp(®ex
->regex
, string
, cflags
);
76 tre_regcomp(®ex
->regex
, "\0\0", 0);
80 int text_regex_match(Regex
*r
, const char *data
, int eflags
) {
81 return tre_regexec(&r
->regex
, data
, 0, NULL
, eflags
);
84 int text_search_range_forward(Text
*txt
, size_t pos
, size_t len
, Regex
*r
, size_t nmatch
, RegexMatch pmatch
[], int eflags
) {
86 r
->it
= text_iterator_get(txt
, pos
);
89 regmatch_t match
[nmatch
];
90 int ret
= tre_reguexec(&r
->regex
, &r
->str_source
, nmatch
, match
, eflags
);
92 for (size_t i
= 0; i
< nmatch
; i
++) {
93 pmatch
[i
].start
= match
[i
].rm_so
== -1 ? EPOS
: pos
+ match
[i
].rm_so
;
94 pmatch
[i
].end
= match
[i
].rm_eo
== -1 ? EPOS
: pos
+ match
[i
].rm_eo
;
100 int text_search_range_backward(Text
*txt
, size_t pos
, size_t len
, Regex
*r
, size_t nmatch
, RegexMatch pmatch
[], int eflags
) {
101 int ret
= REG_NOMATCH
;
102 size_t end
= pos
+ len
;
104 while (pos
< end
&& !text_search_range_forward(txt
, pos
, len
, r
, nmatch
, pmatch
, eflags
)) {
106 // FIXME: assumes nmatch >= 1
107 size_t next
= pmatch
[0].end
;
109 next
= text_line_next(txt
, pos
);