Skip to content

Commit

Permalink
Fixed nfa_to_dfa issues found by sonarcloud
Browse files Browse the repository at this point in the history
  • Loading branch information
d06alexandrov committed Jul 18, 2023
1 parent c20670e commit 7557635
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 35 deletions.
12 changes: 6 additions & 6 deletions lib/nfa.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ void nfa_free(struct nfa *nfa)
}
}

size_t nfa_state_count(struct nfa *nfa)
size_t nfa_state_count(const struct nfa *nfa)
{
return nfa->node_cnt;
}

size_t nfa_get_initial_state(struct nfa *nfa)
size_t nfa_get_initial_state(const struct nfa *nfa)
{
return nfa->first_index;
}
Expand Down Expand Up @@ -205,7 +205,7 @@ int nfa_rebuild(struct nfa *nfa)
return 0;
}

int nfa_join(struct nfa *dst, struct nfa *src)
int nfa_join(struct nfa *dst, const struct nfa *src)
{
size_t offset;

Expand Down Expand Up @@ -272,7 +272,7 @@ int nfa_add_node_n(struct nfa *nfa, size_t cnt, size_t *index)
return 0;
}

int nfa_state_is_final(struct nfa *nfa, size_t state)
int nfa_state_is_final(const struct nfa *nfa, size_t state)
{
if (nfa->nodes[state].isfinal)
return 1;
Expand Down Expand Up @@ -305,7 +305,7 @@ int nfa_add_lambda_trans(struct nfa *nfa, size_t from, size_t to)
return 0;
}

size_t nfa_get_lambda_trans(struct nfa *nfa, size_t from, size_t **trans)
size_t nfa_get_lambda_trans(const struct nfa *nfa, size_t from, size_t **trans)
{
if (from >= nfa_state_count(nfa)) {
return 0;
Expand All @@ -330,7 +330,7 @@ int nfa_add_trans(struct nfa *dst, size_t from, unsigned char mark, size_t to)
return 0;
}

size_t nfa_get_trans(struct nfa *nfa, size_t from, unsigned char mark,
size_t nfa_get_trans(const struct nfa *nfa, size_t from, unsigned char mark,
size_t **trans)
{
if (from >= nfa_state_count(nfa)) {
Expand Down
12 changes: 6 additions & 6 deletions lib/nfa.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ void nfa_free(struct nfa *nfa);
* @param nfa pointer to the nfa structure
* @return total number of states
*/
size_t nfa_state_count(struct nfa *nfa);
size_t nfa_state_count(const struct nfa *nfa);

/**
* Get initial NFA's state.
Expand All @@ -133,7 +133,7 @@ size_t nfa_state_count(struct nfa *nfa);
* @param nfa pointer to the nfa structure
* @return index of the initial state
*/
size_t nfa_get_initial_state(struct nfa *nfa);
size_t nfa_get_initial_state(const struct nfa *nfa);

/**
* Set initial NFA's state.
Expand Down Expand Up @@ -165,7 +165,7 @@ int nfa_rebuild(struct nfa *nfa);
* @param src pointer to the nfa structure that will be joined to the dst
* @return 0 on success
*/
int nfa_join(struct nfa *dst, struct nfa *src);
int nfa_join(struct nfa *dst, const struct nfa *src);

/**
* Add new node (state) to the NFA.
Expand Down Expand Up @@ -200,7 +200,7 @@ int nfa_add_node_n(struct nfa *nfa, size_t cnt, size_t *index);
* @param state index of the NFA's state
* @return 1 if the state with provided index is final
*/
int nfa_state_is_final(struct nfa *nfa, size_t state);
int nfa_state_is_final(const struct nfa *nfa, size_t state);

/**
* Mark or unmark the NFA's state as a final state.
Expand Down Expand Up @@ -237,7 +237,7 @@ int nfa_add_lambda_trans(struct nfa *nfa, size_t from, size_t to);
* @param trans if not NULL then it will point to the list of states
* @return 0 on success
*/
size_t nfa_get_lambda_trans(struct nfa *nfa, size_t from, size_t **trans);
size_t nfa_get_lambda_trans(const struct nfa *nfa, size_t from, size_t **trans);

/**
* Add transition to NFA.
Expand Down Expand Up @@ -265,7 +265,7 @@ int nfa_add_trans(struct nfa *nfa, size_t from, unsigned char mark, size_t to);
* @param trans if not NULL then it will point to the list of states
* @return 0 on success
*/
size_t nfa_get_trans(struct nfa *nfa, size_t from, unsigned char mark,
size_t nfa_get_trans(const struct nfa *nfa, size_t from, unsigned char mark,
size_t **trans);

/**
Expand Down
56 changes: 34 additions & 22 deletions lib/nfa_to_dfa.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ struct ptr_queue {
/**
* @brief Size of one node of a queue.
*/
long pagesize;
unsigned long pagesize;

/**
* @brief Number of elements in one node of a queue.
Expand Down Expand Up @@ -74,7 +74,7 @@ struct ptr_queue {
*/
static int ptr_queue_init(struct ptr_queue *q)
{
q->pagesize = sysconf(_SC_PAGESIZE);
q->pagesize = (unsigned long)sysconf(_SC_PAGESIZE);
q->node_elements = (q->pagesize - sizeof(struct ptr_queue_node))
/ sizeof(void *);

Expand Down Expand Up @@ -102,7 +102,8 @@ static int ptr_queue_init(struct ptr_queue *q)
*/
static void ptr_queue_deinit(struct ptr_queue *q)
{
struct ptr_queue_node *next_node, *cur_node;
struct ptr_queue_node *next_node;
struct ptr_queue_node *cur_node;

next_node = q->first_node;

Expand Down Expand Up @@ -228,10 +229,13 @@ static struct nfa_dfa_pair *nfa_dfa_pair_alloc()
struct nfa_dfa_pair *pair;

pair = malloc(NFA_DFA_PAIR_STEP * sizeof(size_t));
pair->nfa_count_reserved = NFA_DFA_PAIR_STEP
- sizeof(struct nfa_dfa_pair) / sizeof(size_t);
pair->nfa_count = 0;
pair->dfa_state = 0;

if (pair != NULL) {
pair->nfa_count_reserved = NFA_DFA_PAIR_STEP
- sizeof(struct nfa_dfa_pair) / sizeof(size_t);
pair->nfa_count = 0;
pair->dfa_state = 0;
}

return pair;
}
Expand Down Expand Up @@ -260,7 +264,10 @@ static void nfa_dfa_pair_free(struct nfa_dfa_pair *pair)
*/
static int nfa_dfa_pair_add(struct nfa_dfa_pair **pair, size_t element)
{
size_t new_pos, left, right, middle;
size_t new_pos;
size_t left;
size_t right;
size_t middle;
size_t new_mem_size;
struct nfa_dfa_pair *mem;

Expand Down Expand Up @@ -341,9 +348,7 @@ static int nfa_dfa_pair_add(struct nfa_dfa_pair **pair, size_t element)
*/
static int compare_nfa_dfa_pair(struct nfa_dfa_pair *p1, struct nfa_dfa_pair *p2)
{
size_t i;

for (i = 0; i < p1->nfa_count && i < p2->nfa_count; i++) {
for (size_t i = 0; i < p1->nfa_count && i < p2->nfa_count; i++) {
if (p1->nfa_states[i] < p2->nfa_states[i]) {
return -1;
} else if (p1->nfa_states[i] > p2->nfa_states[i]) {
Expand Down Expand Up @@ -426,7 +431,8 @@ static int rb_tree_init(struct rb_tree *tree)
*/
static void rb_tree_deinit(struct rb_tree *tree)
{
struct rb_node *iter, *parent;
struct rb_node *iter;
struct rb_node *parent;

if (tree->root == &tree->nil) {
return;
Expand Down Expand Up @@ -607,7 +613,9 @@ static void rb_tree_fix(struct rb_tree *tree, struct rb_node *node)
*/
static int rb_tree_try_add(struct rb_tree *tree, struct nfa_dfa_pair *pair)
{
struct rb_node *iter, *parent, *new_node;
struct rb_node *iter;
struct rb_node *parent;
struct rb_node *new_node;
int cmp_res;

iter = tree->root;
Expand Down Expand Up @@ -653,16 +661,17 @@ static int rb_tree_try_add(struct rb_tree *tree, struct nfa_dfa_pair *pair)
return 0;
}

int convert_nfa_to_dfa(struct dfa *dst, struct nfa *src)
int convert_nfa_to_dfa(struct dfa *dst, const struct nfa *src)
{
struct ptr_queue q;
struct rb_tree t;
struct nfa_dfa_pair *pair, *next_pair;
size_t nfa_index, dfa_index, nfa_index_next;
size_t state, trans_cnt;
size_t *nfa_trans;
unsigned int i;
int ret = 0, rb_added;
const struct nfa_dfa_pair *pair;
struct nfa_dfa_pair *next_pair;
size_t nfa_index;
size_t dfa_index;
size_t nfa_index_next;
int ret = 0;
int rb_added;
bool final;

if (ptr_queue_init(&q) != 0) {
Expand Down Expand Up @@ -707,15 +716,18 @@ int convert_nfa_to_dfa(struct dfa *dst, struct nfa *src)
}

while ((pair = ptr_queue_pop(&q)) != NULL) {
for (i = 0; i < 256; i++) {
for (unsigned int i = 0; i < 256; i++) {
next_pair = nfa_dfa_pair_alloc();
if (next_pair == NULL) {
ret = 4;
goto fail;
}
final = false;

for (state = 0; state < pair->nfa_count; state++) {
for (size_t state = 0; state < pair->nfa_count; state++) {
size_t trans_cnt;
size_t *nfa_trans;

nfa_index = pair->nfa_states[state];

trans_cnt = nfa_get_trans(src, nfa_index,
Expand Down
2 changes: 1 addition & 1 deletion lib/nfa_to_dfa.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@
* @param nfa pointer to the source NFA without lambda-transitions
* @return 0 on success
*/
int convert_nfa_to_dfa(struct dfa *dfa, struct nfa *nfa);
int convert_nfa_to_dfa(struct dfa *dfa, const struct nfa *nfa);

#endif /** REFA_NFA_TO_DFA_H @} */

0 comments on commit 7557635

Please sign in to comment.