diff --git a/include/aho_trie.h b/include/aho_trie.h index b9c3736..93add9d 100644 --- a/include/aho_trie.h +++ b/include/aho_trie.h @@ -32,7 +32,7 @@ bool aho_add_trie_node(struct aho_trie * restrict t, struct aho_text_t * restric void aho_connect_link(struct aho_trie * restrict t); void aho_clean_trie_node(struct aho_trie * restrict t); -struct aho_text_t* aho_find_trie_node(struct aho_trie_node** restrict start, const unsigned char text); +struct aho_trie_node* aho_find_trie_node(struct aho_trie_node** restrict start, const unsigned char text); void aho_print_trie(struct aho_trie * restrict t); diff --git a/src/aho_trie.c b/src/aho_trie.c index 2117eba..1fe64e6 100644 --- a/src/aho_trie.c +++ b/src/aho_trie.c @@ -235,7 +235,7 @@ bool __aho_find_trie_node(struct aho_trie_node** restrict start, const unsigned return false; } -struct aho_text_t* aho_find_trie_node(struct aho_trie_node** restrict start, const unsigned char text) +struct aho_trie_node* aho_find_trie_node(struct aho_trie_node** restrict start, const unsigned char text) { while (__aho_find_trie_node(start, text) == false) { @@ -253,20 +253,7 @@ struct aho_text_t* aho_find_trie_node(struct aho_trie_node** restrict start, con } /* found node... */ - /* match case1: find text end! */ - if ((*start)->text_end) - { - return (*start)->output_text; - } - - /* match case2: exist output_link */ - if ((*start)->output_link) - { - return (*start)->output_link->output_text; - } - - /* keep going */ - return NULL; + return *start; } void aho_print_trie(struct aho_trie * restrict t) diff --git a/src/ahocorasick.c b/src/ahocorasick.c index 3f9ba7e..3fab2d2 100644 --- a/src/ahocorasick.c +++ b/src/ahocorasick.c @@ -29,7 +29,7 @@ int aho_add_match_text(struct ahocorasick * restrict aho, const char* text, unsi if (!a_text) goto lack_free_mem; - a_text->text = (char*) malloc(sizeof(char)*len); + a_text->text = (char*) malloc(len+1); if (!a_text->text) goto lack_free_mem; @@ -127,6 +127,30 @@ void aho_clear_trie(struct ahocorasick * restrict aho) aho_destroy_trie(&aho->trie); } +unsigned int aho_trie_node_recursive_callback(struct ahocorasick * restrict aho, struct aho_trie_node * restrict node, int offset) +{ + int retval = 0; + if (node) { + if (node->text_end) { + retval++; + if (aho->callback_match) { + //~ result->output_text->text + struct aho_match_t match; + match.id = node->output_text->id; + match.len = node->output_text->len; + match.pos = offset - node->output_text->len + 1; + aho->callback_match(aho->callback_arg, &match); + } + } + + /* match case2: exist output_link */ + if (node->output_link) { + retval += aho_trie_node_recursive_callback(aho, node->output_link, offset); + } + } + return retval; +} + unsigned int aho_findtext(struct ahocorasick * restrict aho, const char* data, unsigned long long data_len) { int i = 0; @@ -137,28 +161,10 @@ unsigned int aho_findtext(struct ahocorasick * restrict aho, const char* data, u for (i = 0; i < data_len; i++) { - struct aho_match_t match; - struct aho_text_t* result; - - result = aho_find_trie_node(&travasal_node, data[i]); - if (result == NULL) - { - continue; - } - - match.id = result->id; - match.len = result->len; - - match.pos = i - result->len + 1; - if (result->len == 1) - { - match.pos = i; - } - - match_count++; - if (aho->callback_match) + struct aho_trie_node* result = aho_find_trie_node(&travasal_node, data[i]); + if (result) { - aho->callback_match(aho->callback_arg, &match); + match_count += aho_trie_node_recursive_callback(aho, result, i); } }