]> git.xolatile.top Git - public-libhl.git/commitdiff
it no longer segfaults
authoranon <anon@anon.anon>
Fri, 25 Aug 2023 19:06:26 +0000 (21:06 +0200)
committeranon <anon@anon.anon>
Fri, 25 Aug 2023 19:06:26 +0000 (21:06 +0200)
source/hl.h
source/regex.c

index cbb672c854b7cd0f8c7b62c2c196a696858f228e..7b007149f39b9a071dafe1bd7c35c0e428730888 100644 (file)
@@ -69,7 +69,7 @@ int free_token(token_t * token) {
 }
 
 int append_token(token_t * token) {
-       vector_push(&token_table, token);
+       vector_push(&token_table, &token);
 
        return 0;
 }
@@ -196,8 +196,8 @@ void render_string(const char * const string,
                int offset;
 
                for (; token_index < token_table.element_count; token_index++) {
-                       token_t * t = vector_get(&token_table,
-                                                 token_index);
+                       token_t * t = *(token_t**)vector_get(&token_table,
+                                                            token_index);
                        f = token_fits(t, string, s - string, &offset);
                        if (f) {
                                break;
@@ -211,14 +211,14 @@ void render_string(const char * const string,
                //
                if (f) {
                        for (int i = 0; i < offset; i++) {
-                               token_t * t = vector_get(&token_table,
-                                                         token_index);
+                               token_t * t = *(token_t**)vector_get(&token_table,
+                                                                    token_index);
                                display->callback(s + i,
                                                  0,
                                                  t->hl->attributes);
                        }
-                       token_t * t = vector_get(&token_table,
-                                                 token_index);
+                       token_t * t = *(token_t**)vector_get(&token_table,
+                                                            token_index);
                        display->callback(s + offset,
                                          f,
                                          t->hl->attributes);
@@ -240,15 +240,12 @@ hl_group_t * preprocessor_hl = NULL;
 hl_group_t * symbol_hl = NULL;
 
 int hl_init(void) {
-       vector_init(&token_table,
-                               token_table.element_size,
-                               token_table.element_count);
        return 0;
 }
 
 int hl_deinit(void) {
        for (size_t i = 0; i < token_table.element_count; i++) {
-               free_token(vector_get(&token_table, i));
+               free_token(*(token_t**)vector_get(&token_table, i));
        }
 
        return 0;
index b34b2ee41af88ebd728f47c238a06a4ff61b9c89..36e6a429ba706334f425f1bdf88f8a06141401d7 100644 (file)
@@ -315,7 +315,7 @@ static bool catch_(const regex_t * const regex,
                          int     * const state) {
 
        for (size_t i = 0; i < regex->catch_table.element_size; i++){
-               const offshoot_t * const offshoot = (vector_get(&regex->catch_table, i));
+               const offshoot_t * const offshoot = *(offshoot_t**)vector_get(&regex->catch_table, i);
                if (offshoot->in == *state) {
                        *state = offshoot->to;
                        return true;
@@ -333,15 +333,22 @@ void HOOK_ALL(int              from,
 
        int hook_to = (*cs->is_negative) ? HALT_AND_CATCH_FIRE : *cs->state + to;
 
+
        for (const char * s = str; *s != '\0'; s++) {
+               delta_t * delta = malloc(sizeof(delta_t));
+               delta->in    = *cs->state + from;
+               delta->input = *s;
+               delta->to    = hook_to;
+               delta->width = *cs->width;
                vector_push(&cs->regex->delta_table,
-                       &(delta_t){*cs->state + from, *s, hook_to, *cs->width}
-               );
+                           &delta);
        }
        if (cs->do_catch || cs->is_negative) {
+               offshoot_t * offshoot = malloc(sizeof(offshoot_t));
+               offshoot->in = *cs->state + from; 
+               offshoot->to   = hook_to;
                vector_push(&cs->regex->catch_table,
-                       &(offshoot_t){*cs->state + from, hook_to}
-               );
+                           &offshoot);
        }
 }
 
@@ -352,8 +359,8 @@ void HOOK_ALL(int              from,
 regex_t * regex_compile(const char * const pattern) {
        regex_t * regex = (regex_t *)malloc(sizeof(regex_t));
        regex->str = strdup(pattern);
-       vector_init(&regex->delta_table, sizeof(delta_t), 0);
-       vector_init(&regex->catch_table, sizeof(offshoot_t), 0);
+       vector_init(&regex->delta_table, sizeof(delta_t), 0UL);
+       vector_init(&regex->catch_table, sizeof(offshoot_t), 0UL);
 
        int state = 0;
 
@@ -454,7 +461,7 @@ static bool regex_assert(const regex_t * const  regex,
        for (const char * s = string; *s != '\00'; s++) {
                // delta
                for (size_t i = 0; i < regex->delta_table.element_count; i++) {
-                       const delta_t * const delta = (delta_t *)(vector_get(&regex->delta_table, i));
+                       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)){