From 77e1a77e02aef7674b3301da2755b09b6c3641a5 Mon Sep 17 00:00:00 2001 From: Camden Dixie O'Brien Date: Sun, 3 Nov 2024 11:59:56 +0000 Subject: [PATCH] Support wildcards in construct --- lib/construct.c | 10 ++++++++-- tests/construct_tests.c | 26 ++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/lib/construct.c b/lib/construct.c index 4332c8f..f0b2417 100644 --- a/lib/construct.c +++ b/lib/construct.c @@ -164,6 +164,13 @@ static void construct_class(fsa_t *out, const regex_class_t *class) } } +static void construct_wildcard(fsa_t *out) +{ + construct_base(out); + for (int i = 0; i < CHAR_COUNT; ++i) + fsa_add_rule(out, out->initial, 0, i); +} + static void construct_term(const regex_term_t *term, fsa_t *out) { switch (term->type) { @@ -179,9 +186,8 @@ static void construct_term(const regex_term_t *term, fsa_t *out) case REGEX_TERM_CLASS: construct_class(out, &term->class); break; - case REGEX_TERM_WILDCARD: - assert(false); + construct_wildcard(out); break; } diff --git a/tests/construct_tests.c b/tests/construct_tests.c index 20f7f87..155330d 100644 --- a/tests/construct_tests.c +++ b/tests/construct_tests.c @@ -53,6 +53,31 @@ static void test_empty_expression(void) fsa_free(&fsa); } +static void test_wildcard(void) +{ + regex_term_t *terms = malloc(1 * sizeof(regex_term_t)); + terms[0].quantifier = REGEX_QUANTIFIER_NONE; + terms[0].type = REGEX_TERM_WILDCARD; + regex_sequence_t *alternatives = malloc(1 * sizeof(regex_sequence_t)); + alternatives[0].count = alternatives[0].capacity = 1; + alternatives[0].contents = terms; + const regex_t regex + = { .count = 1, .capacity = 1, .contents = alternatives }; + + fsa_t fsa; + construct_nfa(®ex, &fsa); + + ASSERT_TRUE(accepts(&fsa, "a")); + ASSERT_TRUE(accepts(&fsa, "b")); + ASSERT_TRUE(accepts(&fsa, "c")); + ASSERT_TRUE(accepts(&fsa, "d")); + ASSERT_FALSE(accepts(&fsa, "")); + ASSERT_FALSE(accepts(&fsa, "aa")); + + regex_free(®ex); + fsa_free(&fsa); +} + static void test_literal_expression(void) { regex_term_t *terms = malloc(1 * sizeof(regex_term_t)); @@ -386,6 +411,7 @@ int main(void) // Base cases test_empty_expression(); test_literal_expression(); + test_wildcard(); test_sequence(); test_union(); test_star();