Rename free procedures

This commit is contained in:
Camden Dixie O'Brien 2024-10-26 19:36:42 +01:00
parent e508cc62f5
commit 0ecab6c142
5 changed files with 40 additions and 41 deletions

View File

@ -29,7 +29,7 @@ static bool desugar_class(regex_term_t *term)
alternatives[i].contents = terms; alternatives[i].contents = terms;
} }
regex_free_class_children(&term->class); regex_class_free(&term->class);
term->type = REGEX_TERM_SUBEXPR; term->type = REGEX_TERM_SUBEXPR;
term->subexpr.count = term->subexpr.capacity = count; term->subexpr.count = term->subexpr.capacity = count;
term->subexpr.contents = alternatives; term->subexpr.contents = alternatives;

View File

@ -7,25 +7,16 @@
#include <stdlib.h> #include <stdlib.h>
void regex_free_children(const regex_t *t) static void sequence_free(const regex_sequence_t *s)
{
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)
{ {
if (NULL != s->contents) { if (NULL != s->contents) {
for (int i = 0; i < s->count; ++i) { for (int i = 0; i < s->count; ++i) {
switch (s->contents[i].type) { switch (s->contents[i].type) {
case REGEX_TERM_CLASS: case REGEX_TERM_CLASS:
regex_free_class_children(&s->contents[i].class); regex_class_free(&s->contents[i].class);
break; break;
case REGEX_TERM_SUBEXPR: case REGEX_TERM_SUBEXPR:
regex_free_children(&s->contents[i].subexpr); regex_free(&s->contents[i].subexpr);
break; break;
default: default:
break; 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) if (NULL != c->contents)
free(c->contents); free(c->contents);

View File

@ -50,8 +50,7 @@ typedef struct _regex_term {
}; };
} regex_term_t; } regex_term_t;
void regex_free_children(const regex_t *t); void regex_free(const regex_t *t);
void regex_free_sequence_children(const regex_sequence_t *s); void regex_class_free(const regex_class_t *c);
void regex_free_class_children(const regex_class_t *c);
#endif #endif

View File

@ -30,7 +30,7 @@ static void a_is_unchanged(void)
ASSERT_EQ(REGEX_TERM_LITERAL, t.contents[0].contents[0].type); ASSERT_EQ(REGEX_TERM_LITERAL, t.contents[0].contents[0].type);
ASSERT_EQ('a', t.contents[0].contents[0].literal); ASSERT_EQ('a', t.contents[0].contents[0].literal);
regex_free_children(&t); regex_free(&t);
} }
static void abc_is_unchanged(void) 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(REGEX_TERM_LITERAL, t.contents[0].contents[2].type);
ASSERT_EQ('c', t.contents[0].contents[2].literal); ASSERT_EQ('c', t.contents[0].contents[2].literal);
regex_free_children(&t); regex_free(&t);
} }
static void a_star_is_unchanged(void) 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(REGEX_TERM_LITERAL, t.contents[0].contents[0].type);
ASSERT_EQ('a', t.contents[0].contents[0].literal); 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) 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); ASSERT_EQ(literals[i], t.contents[i].contents[0].literal);
} }
regex_free_children(&t); regex_free(&t);
} }
static void subexpr_a_is_unchanged(void) 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(REGEX_TERM_LITERAL, inner->contents[0].contents[0].type);
ASSERT_EQ('a', inner->contents[0].contents[0].literal); 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) 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(REGEX_TERM_LITERAL, inner->contents[0].contents[1].type);
ASSERT_EQ('a', inner->contents[0].contents[1].literal); 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) 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(REGEX_TERM_LITERAL, inner->contents[1].contents[0].type);
ASSERT_EQ('a', inner->contents[1].contents[0].literal); 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) 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(REGEX_TERM_LITERAL, inner->contents[2].contents[0].type);
ASSERT_EQ('c', inner->contents[2].contents[0].literal); ASSERT_EQ('c', inner->contents[2].contents[0].literal);
regex_free_children(&t); regex_free(&t);
} }
int main(void) int main(void)

View File

@ -14,7 +14,7 @@ static void a_has_1_alternative(void)
const int result = PARSE_EXPR_STRING("a", &t); const int result = PARSE_EXPR_STRING("a", &t);
ASSERT_NE(-1, result); ASSERT_NE(-1, result);
ASSERT_EQ(1, t.count); ASSERT_EQ(1, t.count);
regex_free_children(&t); regex_free(&t);
} }
static void a_pipe_b_has_2_alternatives(void) 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); const int result = PARSE_EXPR_STRING("a|b", &t);
ASSERT_NE(-1, result); ASSERT_NE(-1, result);
ASSERT_EQ(2, t.count); ASSERT_EQ(2, t.count);
regex_free_children(&t); regex_free(&t);
} }
static void a_pipe_b_pipe_c_has_3_alternatives(void) 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); const int result = PARSE_EXPR_STRING("a|b|c", &t);
ASSERT_NE(-1, result); ASSERT_NE(-1, result);
ASSERT_EQ(3, t.count); ASSERT_EQ(3, t.count);
regex_free_children(&t); regex_free(&t);
} }
static void a_is_parsed_as_unquantified_literal(void) 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(REGEX_TERM_LITERAL, t.contents[0].contents[0].type);
ASSERT_EQ('a', t.contents[0].contents[0].literal); 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) 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(REGEX_TERM_LITERAL, t.contents[0].contents[0].type);
ASSERT_EQ('b', t.contents[0].contents[0].literal); 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) 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(REGEX_TERM_LITERAL, t.contents[0].contents[2].type);
ASSERT_EQ('c', t.contents[0].contents[2].literal); 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) 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_QUANTIFIER_NONE, t.contents[0].contents[0].quantifier);
ASSERT_EQ(REGEX_TERM_WILDCARD, t.contents[0].contents[0].type); 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) 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(REGEX_TERM_LITERAL, t.contents[0].contents[0].type);
ASSERT_EQ('.', t.contents[0].contents[0].literal); 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) 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(REGEX_TERM_LITERAL, t.contents[0].contents[0].type);
ASSERT_EQ('\\', t.contents[0].contents[0].literal); 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) 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(REGEX_TERM_LITERAL, inner->contents[1].contents[0].type);
ASSERT_EQ('b', inner->contents[1].contents[0].literal); 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) 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(REGEX_TERM_LITERAL, inner->contents[0].contents[0].type);
ASSERT_EQ('a', inner->contents[0].contents[0].literal); 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) 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_QUANTIFIER_STAR, t.contents[0].contents[0].quantifier);
ASSERT_EQ(REGEX_TERM_WILDCARD, t.contents[0].contents[0].type); 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) 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_QUANTIFIER_PLUS, t.contents[0].contents[0].quantifier);
ASSERT_EQ(REGEX_TERM_WILDCARD, t.contents[0].contents[0].type); 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) 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_QUANTIFIER_QMARK, t.contents[0].contents[0].quantifier);
ASSERT_EQ(REGEX_TERM_WILDCARD, t.contents[0].contents[0].type); 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) 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_NOT_NULL(t.contents[0].contents[0].class.contents);
ASSERT_EQ('a', t.contents[0].contents[0].class.contents[0]); 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) 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_NOT_NULL(t.contents[0].contents[0].class.contents);
ASSERT_EQ('a', t.contents[0].contents[0].class.contents[0]); ASSERT_EQ('a', t.contents[0].contents[0].class.contents[0]);
regex_free_children(&t); regex_free(&t);
} }
int main(void) int main(void)