From 452b6e660ef679c8b1efbd25474fa5c6e647b94c Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Fri, 6 Jan 2017 15:23:38 -0800 Subject: [PATCH] Write more utility functions, such as creating a chain. --- src/libregex.c | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/src/libregex.c b/src/libregex.c index 9eb1387..fbfbc29 100644 --- a/src/libregex.c +++ b/src/libregex.c @@ -1,6 +1,7 @@ #include "libregex.h"#include "libregex.h" #include #include +#include "ll.h" libregex_result _regex_node_create_clear(regex_node** node){ libregex_result result = LIBREGEX_SUCCESS; @@ -81,6 +82,98 @@ libregex_result _regex_node_create_group(regex_node** open, regex_node** close, return result; } +libregex_result _regex_chain_create(regex_chain** new_chain, regex_node* head, regex_node* tail){ + libregex_result result = LIBREGEX_MALLOC; + *new_chain = malloc(sizeof(**new_chain)); + if(*new_chain){ + result = LIBREGEX_SUCCESS; + (*new_chain)->head = head; + (*new_chain)->tail = tail; + } + return result; +} + +regex_node** _regex_node_get_next(regex_node* node){ + regex_node** to_return = NULL; + if(node->type == REGEX_CONNECT){ + to_return = &node->data_u.connect_s.next; + } else if(node->type == REGEX_VALUE){ + to_return = &node->data_u.value_s.next; + } else if(node->type == REGEX_RANGE){ + to_return = &node->data_u.range_s.next; + } else if(node->type == REGEX_ANY){ + to_return = &node->data_u.any_s.next; + } else if(node->type == REGEX_GROUP) { + to_return = &node->data_u.group_s.next; + } + return to_return; +} + +void _regex_node_append_node(regex_node* append_to, regex_node* to_append){ + regex_node** next = _regex_node_get_next(append_to); + if(next){ + *next = to_append; + } +} + +void _regex_chain_append_node(regex_chain* append_to, regex_node* to_append){ + if(append_to && to_append){ + regex_node** to_set = append_to->head ? _regex_node_get_next(append_to->tail) : &append_to->head; + if(to_set) { + *to_set = append_to->tail = to_append; + } + } +} + +void _regex_chain_prepend_node(regex_chain* prepend_to, regex_node* to_prepend){ + if(prepend_to && to_prepend){ + regex_node** next = _regex_node_get_next(to_prepend); + if(next){ + *next = prepend_to->head; + prepend_to->head = to_prepend; + if(prepend_to->tail == NULL){ + prepend_to->tail = to_prepend; + } + } + } +} + +void _regex_chain_append_chain(regex_chain* append_to, regex_chain* to_append){ + if(append_to && to_append){ + _regex_chain_append_node(append_to, to_append->head); + if(to_append->tail) { + append_to->tail = to_append->tail; + } + } +} + +libregex_result _regex_find_all(regex_node* node, ll* append_to, int tag_with){ + libregex_result result = LIBREGEX_SUCCESS; + if(node->list_id != tag_with){ + node->list_id = tag_with; + if(node->type == REGEX_VALUE){ + result = _regex_find_all(node->data_u.value_s.next, append_to, tag_with); + } else if(node->type == REGEX_RANGE){ + result = _regex_find_all(node->data_u.range_s.next, append_to, tag_with); + } else if(node->type == REGEX_ANY){ + result = _regex_find_all(node->data_u.any_s.next, append_to, tag_with); + } else if(node->type == REGEX_GROUP){ + result = _regex_find_all(node->data_u.group_s.next, append_to, tag_with); + } else if(node->type == REGEX_CONNECT){ + result = _regex_find_all(node->data_u.connect_s.next, append_to, tag_with); + } else if(node->type == REGEX_FORK){ + result = _regex_find_all(node->data_u.fork_s.left, append_to, tag_with); + if(result == LIBREGEX_SUCCESS){ + result = _regex_find_all(node->data_u.fork_s.right, append_to, tag_with); + } + } + if(result == LIBREGEX_SUCCESS){ + result = (ll_append(append_to, node) == LIBDS_SUCCESS) ? LIBREGEX_SUCCESS : LIBREGEX_MALLOC; + } + } + return result; +} + void regex_node_clear(regex_node* node){ node->type = REGEX_CLEAR; node->list_id = -1;