diff --git a/lib/desugar.c b/lib/desugar.c index 6b41d4d..5636070 100644 --- a/lib/desugar.c +++ b/lib/desugar.c @@ -29,7 +29,7 @@ static bool desugar_class(regex_term_t *term) alternatives[i].contents = terms; } - regex_free_class_children(&term->class); + regex_class_free(&term->class); term->type = REGEX_TERM_SUBEXPR; term->subexpr.count = term->subexpr.capacity = count; term->subexpr.contents = alternatives; diff --git a/lib/regex.c b/lib/regex.c index 9ae6a08..74d7d1c 100644 --- a/lib/regex.c +++ b/lib/regex.c @@ -7,25 +7,16 @@ #include -void regex_free_children(const regex_t *t) -{ - if (NULL != t->contents) { - for (int i = 0; i < t->count; ++i) - regex_free_sequence_children(&t->contents[i]); - free(t->contents); - } -} - -void regex_free_sequence_children(const regex_sequence_t *s) +static void sequence_free(const regex_sequence_t *s) { if (NULL != s->contents) { for (int i = 0; i < s->count; ++i) { switch (s->contents[i].type) { case REGEX_TERM_CLASS: - regex_free_class_children(&s->contents[i].class); + regex_class_free(&s->contents[i].class); break; case REGEX_TERM_SUBEXPR: - regex_free_children(&s->contents[i].subexpr); + regex_free(&s->contents[i].subexpr); break; default: break; @@ -35,7 +26,16 @@ void regex_free_sequence_children(const regex_sequence_t *s) } } -void regex_free_class_children(const regex_class_t *c) +void regex_free(const regex_t *t) +{ + if (NULL != t->contents) { + for (int i = 0; i < t->count; ++i) + sequence_free(&t->contents[i]); + free(t->contents); + } +} + +void regex_class_free(const regex_class_t *c) { if (NULL != c->contents) free(c->contents); diff --git a/lib/regex.h b/lib/regex.h index 0189cd9..4fbc2b3 100644 --- a/lib/regex.h +++ b/lib/regex.h @@ -50,8 +50,7 @@ typedef struct _regex_term { }; } regex_term_t; -void regex_free_children(const regex_t *t); -void regex_free_sequence_children(const regex_sequence_t *s); -void regex_free_class_children(const regex_class_t *c); +void regex_free(const regex_t *t); +void regex_class_free(const regex_class_t *c); #endif diff --git a/tests/desugar_tests.c b/tests/desugar_tests.c index afb95cd..7605d75 100644 --- a/tests/desugar_tests.c +++ b/tests/desugar_tests.c @@ -30,7 +30,7 @@ static void a_is_unchanged(void) ASSERT_EQ(REGEX_TERM_LITERAL, t.contents[0].contents[0].type); ASSERT_EQ('a', t.contents[0].contents[0].literal); - regex_free_children(&t); + regex_free(&t); } static void abc_is_unchanged(void) @@ -63,7 +63,7 @@ static void abc_is_unchanged(void) ASSERT_EQ(REGEX_TERM_LITERAL, t.contents[0].contents[2].type); ASSERT_EQ('c', t.contents[0].contents[2].literal); - regex_free_children(&t); + regex_free(&t); } static void a_star_is_unchanged(void) @@ -88,7 +88,7 @@ static void a_star_is_unchanged(void) ASSERT_EQ(REGEX_TERM_LITERAL, t.contents[0].contents[0].type); ASSERT_EQ('a', t.contents[0].contents[0].literal); - regex_free_children(&t); + regex_free(&t); } static void a_or_b_or_c_is_unchanged(void) @@ -120,7 +120,7 @@ static void a_or_b_or_c_is_unchanged(void) ASSERT_EQ(literals[i], t.contents[i].contents[0].literal); } - regex_free_children(&t); + regex_free(&t); } static void subexpr_a_is_unchanged(void) @@ -163,7 +163,7 @@ static void subexpr_a_is_unchanged(void) ASSERT_EQ(REGEX_TERM_LITERAL, inner->contents[0].contents[0].type); ASSERT_EQ('a', inner->contents[0].contents[0].literal); - regex_free_children(&t); + regex_free(&t); } static void a_plus_becomes_subexpr_aa_star(void) @@ -201,7 +201,7 @@ static void a_plus_becomes_subexpr_aa_star(void) ASSERT_EQ(REGEX_TERM_LITERAL, inner->contents[0].contents[1].type); ASSERT_EQ('a', inner->contents[0].contents[1].literal); - regex_free_children(&t); + regex_free(&t); } static void a_qmark_becomes_subexpr_empty_or_a(void) @@ -240,7 +240,7 @@ static void a_qmark_becomes_subexpr_empty_or_a(void) ASSERT_EQ(REGEX_TERM_LITERAL, inner->contents[1].contents[0].type); ASSERT_EQ('a', inner->contents[1].contents[0].literal); - regex_free_children(&t); + regex_free(&t); } static void class_abc_becomes_subexpr_a_or_b_or_c(void) @@ -292,7 +292,7 @@ static void class_abc_becomes_subexpr_a_or_b_or_c(void) ASSERT_EQ(REGEX_TERM_LITERAL, inner->contents[2].contents[0].type); ASSERT_EQ('c', inner->contents[2].contents[0].literal); - regex_free_children(&t); + regex_free(&t); } int main(void) diff --git a/tests/parse_tests.c b/tests/parse_tests.c index ce2068f..5e4bb6e 100644 --- a/tests/parse_tests.c +++ b/tests/parse_tests.c @@ -14,7 +14,7 @@ static void a_has_1_alternative(void) const int result = PARSE_EXPR_STRING("a", &t); ASSERT_NE(-1, result); ASSERT_EQ(1, t.count); - regex_free_children(&t); + regex_free(&t); } static void a_pipe_b_has_2_alternatives(void) @@ -23,7 +23,7 @@ static void a_pipe_b_has_2_alternatives(void) const int result = PARSE_EXPR_STRING("a|b", &t); ASSERT_NE(-1, result); ASSERT_EQ(2, t.count); - regex_free_children(&t); + regex_free(&t); } static void a_pipe_b_pipe_c_has_3_alternatives(void) @@ -32,7 +32,7 @@ static void a_pipe_b_pipe_c_has_3_alternatives(void) const int result = PARSE_EXPR_STRING("a|b|c", &t); ASSERT_NE(-1, result); ASSERT_EQ(3, t.count); - regex_free_children(&t); + regex_free(&t); } static void a_is_parsed_as_unquantified_literal(void) @@ -48,7 +48,7 @@ static void a_is_parsed_as_unquantified_literal(void) ASSERT_EQ(REGEX_TERM_LITERAL, t.contents[0].contents[0].type); ASSERT_EQ('a', t.contents[0].contents[0].literal); - regex_free_children(&t); + regex_free(&t); } static void b_is_parsed_as_unquantified_literal(void) @@ -64,7 +64,7 @@ static void b_is_parsed_as_unquantified_literal(void) ASSERT_EQ(REGEX_TERM_LITERAL, t.contents[0].contents[0].type); ASSERT_EQ('b', t.contents[0].contents[0].literal); - regex_free_children(&t); + regex_free(&t); } static void abc_is_parsed_as_sequence_of_unquantified_literals(void) @@ -86,7 +86,7 @@ static void abc_is_parsed_as_sequence_of_unquantified_literals(void) ASSERT_EQ(REGEX_TERM_LITERAL, t.contents[0].contents[2].type); ASSERT_EQ('c', t.contents[0].contents[2].literal); - regex_free_children(&t); + regex_free(&t); } static void dot_is_parsed_as_unquantified_wildcard_term(void) @@ -101,7 +101,7 @@ static void dot_is_parsed_as_unquantified_wildcard_term(void) ASSERT_EQ(REGEX_QUANTIFIER_NONE, t.contents[0].contents[0].quantifier); ASSERT_EQ(REGEX_TERM_WILDCARD, t.contents[0].contents[0].type); - regex_free_children(&t); + regex_free(&t); } static void backslash_dot_is_parsed_as_unquantified_literal(void) @@ -117,7 +117,7 @@ static void backslash_dot_is_parsed_as_unquantified_literal(void) ASSERT_EQ(REGEX_TERM_LITERAL, t.contents[0].contents[0].type); ASSERT_EQ('.', t.contents[0].contents[0].literal); - regex_free_children(&t); + regex_free(&t); } static void backslash_backslash_is_parsed_as_unquantified_literal(void) @@ -133,7 +133,7 @@ static void backslash_backslash_is_parsed_as_unquantified_literal(void) ASSERT_EQ(REGEX_TERM_LITERAL, t.contents[0].contents[0].type); ASSERT_EQ('\\', t.contents[0].contents[0].literal); - regex_free_children(&t); + regex_free(&t); } static void a_pipe_b_in_parens_is_parsed_as_subexpr_term(void) @@ -163,7 +163,7 @@ static void a_pipe_b_in_parens_is_parsed_as_subexpr_term(void) ASSERT_EQ(REGEX_TERM_LITERAL, inner->contents[1].contents[0].type); ASSERT_EQ('b', inner->contents[1].contents[0].literal); - regex_free_children(&t); + regex_free(&t); } static void a_in_parens_b_is_parsed_as_sequence_with_subexpr_term(void) @@ -188,7 +188,7 @@ static void a_in_parens_b_is_parsed_as_sequence_with_subexpr_term(void) ASSERT_EQ(REGEX_TERM_LITERAL, inner->contents[0].contents[0].type); ASSERT_EQ('a', inner->contents[0].contents[0].literal); - regex_free_children(&t); + regex_free(&t); } static void dot_star_is_parsed_as_star_quantified_wildcard(void) @@ -203,7 +203,7 @@ static void dot_star_is_parsed_as_star_quantified_wildcard(void) ASSERT_EQ(REGEX_QUANTIFIER_STAR, t.contents[0].contents[0].quantifier); ASSERT_EQ(REGEX_TERM_WILDCARD, t.contents[0].contents[0].type); - regex_free_children(&t); + regex_free(&t); } static void dot_plus_is_parsed_as_plus_quantified_wildcard(void) @@ -218,7 +218,7 @@ static void dot_plus_is_parsed_as_plus_quantified_wildcard(void) ASSERT_EQ(REGEX_QUANTIFIER_PLUS, t.contents[0].contents[0].quantifier); ASSERT_EQ(REGEX_TERM_WILDCARD, t.contents[0].contents[0].type); - regex_free_children(&t); + regex_free(&t); } static void dot_question_mark_is_parsed_as_qmrk_quantified_wildcard(void) @@ -233,7 +233,7 @@ static void dot_question_mark_is_parsed_as_qmrk_quantified_wildcard(void) ASSERT_EQ(REGEX_QUANTIFIER_QMARK, t.contents[0].contents[0].quantifier); ASSERT_EQ(REGEX_TERM_WILDCARD, t.contents[0].contents[0].type); - regex_free_children(&t); + regex_free(&t); } static void a_in_brackets_is_parsed_as_class_containing_only_a(void) @@ -252,7 +252,7 @@ static void a_in_brackets_is_parsed_as_class_containing_only_a(void) ASSERT_NOT_NULL(t.contents[0].contents[0].class.contents); ASSERT_EQ('a', t.contents[0].contents[0].class.contents[0]); - regex_free_children(&t); + regex_free(&t); } static void caret_a_in_brackets_parses_as_negated_class(void) @@ -271,7 +271,7 @@ static void caret_a_in_brackets_parses_as_negated_class(void) ASSERT_NOT_NULL(t.contents[0].contents[0].class.contents); ASSERT_EQ('a', t.contents[0].contents[0].class.contents[0]); - regex_free_children(&t); + regex_free(&t); } int main(void)