From 34341459ff0d45c3167c3b325efac089372c3c92 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Mon, 17 Feb 2025 13:13:20 +0900 Subject: [PATCH] WIP white/black-list: Add helper fns and tests --- src/helpers.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/helpers.h | 12 +++++++++ src/test.c | 20 +++++++++++++++ 3 files changed, 103 insertions(+) diff --git a/src/helpers.c b/src/helpers.c index 0b41a56..6c65f27 100644 --- a/src/helpers.c +++ b/src/helpers.c @@ -611,3 +611,74 @@ char *simple_archiver_helper_string_parts_combine( buf[size] = 0; return buf; } + +uint_fast8_t simple_archiver_helper_string_contains(const char *cstring, + const char *contains) { + const size_t cstring_size = strlen(cstring); + const size_t contains_size = strlen(contains); + size_t contains_match_start = 0; + size_t contains_match_idx = 0; + for (size_t idx = 0; idx < cstring_size; ++idx) { + if (cstring[idx] == contains[contains_match_idx]) { + if (contains_match_idx == 0) { + contains_match_start = idx; + } + ++contains_match_idx; + if (contains_match_idx == contains_size) { + return 1; + } + } else { + if (contains_match_idx != 0) { + idx = contains_match_start; + } + contains_match_idx = 0; + } + } + + return 0; +} + +uint_fast8_t simple_archiver_helper_string_starts(const char *cstring, + const char *starts) { + const size_t cstring_len = strlen(cstring); + const size_t starts_len = strlen(starts); + size_t starts_match_idx = 0; + + for (size_t idx = 0; idx < cstring_len; ++idx) { + if (cstring[idx] == starts[starts_match_idx]) { + if (starts_match_idx == 0) { + if (idx != 0) { + return 0; + } + } + ++starts_match_idx; + if (starts_match_idx == starts_len) { + return 1; + } + } else { + return 0; + } + } + + return 0; +} + +uint_fast8_t simple_archiver_helper_string_ends(const char *cstring, + const char *ends) { + const size_t cstring_len = strlen(cstring); + const size_t ends_len = strlen(ends); + size_t ends_idx = 0; + + for (size_t idx = cstring_len - ends_len; idx < cstring_len; ++idx) { + if (cstring[idx] == ends[ends_idx]) { + ++ends_idx; + if (ends_idx == ends_len) { + return 1; + } + } else { + return 0; + } + } + + return 0; +} diff --git a/src/helpers.h b/src/helpers.h index f1bd808..8a5f18e 100644 --- a/src/helpers.h +++ b/src/helpers.h @@ -119,4 +119,16 @@ void simple_archiver_helper_string_parts_add(SAHelperStringParts string_parts, char *simple_archiver_helper_string_parts_combine( SAHelperStringParts string_parts); +// Returns non-zero if "cstring" contains string "contains". +uint_fast8_t simple_archiver_helper_string_contains(const char *cstring, + const char *contains); + +// Returns non-zero if "cstring" starts with string "starts". +uint_fast8_t simple_archiver_helper_string_starts(const char *cstring, + const char *starts); + +// Returns non-zero if "cstring" ends with string "ends". +uint_fast8_t simple_archiver_helper_string_ends(const char *cstring, + const char *ends); + #endif diff --git a/src/test.c b/src/test.c index 2b53a32..a84ca34 100644 --- a/src/test.c +++ b/src/test.c @@ -964,6 +964,26 @@ TEST_HELPERS_PREFIX_END: free(buf); } + // Test contains/starts/ends helpers. + { + CHECK_TRUE(simple_archiver_helper_string_contains( + "The string is this.", " is ")); + CHECK_FALSE(simple_archiver_helper_string_contains( + "The string is this.", " is d")); + CHECK_TRUE(simple_archiver_helper_string_contains( + "TheseTheThesesThe", "Theses")); + + CHECK_TRUE(simple_archiver_helper_string_starts( + "The string is this.", "The ")); + CHECK_FALSE(simple_archiver_helper_string_starts( + "The string is this.", "tThe ")); + + CHECK_TRUE(simple_archiver_helper_string_ends( + "The string is this.", " this.")); + CHECK_FALSE(simple_archiver_helper_string_ends( + "The string is this.", " this")); + } + printf("Checks checked: %" PRId32 "\n", checks_checked); printf("Checks passed: %" PRId32 "\n", checks_passed); return checks_passed == checks_checked ? 0 : 1; -- 2.49.0