summaryrefslogtreecommitdiff
path: root/regex.c
diff options
context:
space:
mode:
authorWormHeamer2025-12-31 05:43:46 -0500
committerWormHeamer2025-12-31 05:43:46 -0500
commit83df7c5a4bed79c53357fd91c393071de1eb60a2 (patch)
treebdbeffd79f60743cae260c4c78d509474d54ce1e /regex.c
parentd723f8a5d54f098f0cf378e5dcf3a7d4ec049822 (diff)
add (hacky and slow) reverse regex search
Diffstat (limited to 'regex.c')
-rw-r--r--regex.c25
1 files changed, 18 insertions, 7 deletions
diff --git a/regex.c b/regex.c
index 710593f..6a362e4 100644
--- a/regex.c
+++ b/regex.c
@@ -846,18 +846,13 @@ void re_search_last_chunk(ReSearch *s) {
* check s->flags to find if the match is done yet
*/
-int re_search_match(ReSearch *s, ReMatch *m) {
+int re_search_match_at_start(ReSearch *s, ReMatch *m) {
size_t i = s->buf_idx;
size_t n = s->buf_len;
while (i < n) {
- if (s->re->first_byte && (~s->flags & RE_SEARCH_MID_MATCH)) {
- const char *p = memchr(s->buf + i, s->re->first_byte, n - i);
- if (!p) break;
- i = p - s->buf;
- }
isize r = re_search_try_match(s, i, n);
if (r < 0) {
- i++;
+ return 0;
} else {
if (i == (usize)r) r++;
if (s->flags & RE_SEARCH_MID_MATCH) {
@@ -877,6 +872,22 @@ int re_search_match(ReSearch *s, ReMatch *m) {
return 0;
}
+int re_search_match(ReSearch *s, ReMatch *m) {
+ u32 i = s->buf_idx;
+ u32 n = s->buf_len;
+ while (i < n) {
+ if (s->re->first_byte && (~s->flags & RE_SEARCH_MID_MATCH)) {
+ const char *p = memchr(s->buf + i, s->re->first_byte, n - i);
+ if (!p) break;
+ i = p - s->buf;
+ }
+ s->buf_idx = i++;
+ if (re_search_match_at_start(s, m)) return 1;
+ }
+ s->buf_idx = n;
+ return 0;
+}
+
/* convenience wrappers */
ReMatchList re_match_all(RegEx *re, Str s, Arena *a) {