From 4d179689d54888724d9a87d40b64010abfd9542b Mon Sep 17 00:00:00 2001
From: anon <anon@anon.anon>
Date: Sat, 26 Aug 2023 22:39:07 +0200
Subject: [PATCH] regex lenghts work

---
 source/hl.h    |  3 ++-
 source/regex.c | 14 +++++++-------
 source/regex.h |  3 ++-
 3 files changed, 11 insertions(+), 9 deletions(-)

diff --git a/source/hl.h b/source/hl.h
index a46080b..d927075 100644
--- a/source/hl.h
+++ b/source/hl.h
@@ -187,9 +187,10 @@ int token_fits(const token_t * const       token,
                const char    * const       to,
                const int                   string_offset,
 			         int     *             match_offset) {
+	UNUSED(match_offset);
 
 	//return regex_match(pattern, to, string_offset, match_offset);
-	return regex_search(token->syntax, to + string_offset);
+	return regex_match(token->syntax, to + string_offset);
 }
 
 void render_string(const char * const string,
diff --git a/source/regex.c b/source/regex.c
index 6dea9eb..876a296 100644
--- a/source/regex.c
+++ b/source/regex.c
@@ -458,7 +458,7 @@ static bool catch_(const regex_t * const regex,
 static int regex_assert(const regex_t * const  regex,
                          const char    * const string,
                                int              state,
-							   int     *        width) {
+							   int              width) {
 
 	for (const char * s = string; *s != '\00'; s++) {
 		// delta
@@ -466,8 +466,9 @@ static int regex_assert(const regex_t * const  regex,
 			const delta_t * const delta = *(delta_t**)vector_get(&regex->delta_table, i);
 			if ((delta->in == state) 
 			&&  (delta->input == *s)) {
-				if(regex_assert(regex, s + delta->width, delta->to)){
-					return true;
+				int r = regex_assert(regex, s + delta->width, delta->to, width + 1);
+				if(r){
+					return r;
 				}
 			}
 		}
@@ -476,7 +477,7 @@ static int regex_assert(const regex_t * const  regex,
 			continue;
 		}
 
-		return (state == regex->accepting_state);
+		return (state == regex->accepting_state) ? width : false;
 	}
 
 	return false;
@@ -492,13 +493,12 @@ int regex_match(      regex_t *        regex,
 		return true;
 	}
 
-	int r = 0;
-	return regex_assert(regex, string, 0, &r);
+	return regex_assert(regex, string, 0, 0);
 }
 
 bool regex_search(      regex_t *        regex,
                   const char    * const string) {
 
-	return (bool)regex_match(regex, string, 0);
+	return (bool)regex_match(regex, string);
 }
 
diff --git a/source/regex.h b/source/regex.h
index 52d1b12..0049fcc 100644
--- a/source/regex.h
+++ b/source/regex.h
@@ -14,8 +14,9 @@ typedef struct {
 } regex_t;
 
 extern regex_t * regex_compile(const char * const pattern);
-extern bool regex_search(regex_t * regex, const char * const string);
 extern int       regex_free(regex_t * const regex);
+extern bool      regex_search(regex_t * regex, const char * const string);
+extern int       regex_match(regex_t * regex, const char * const string);
 
 extern bool is_magic(const char c);