]> git.seodisparate.com - SimpleArchiver/commitdiff
WIP white/black-list: Add helper fns and tests
authorStephen Seo <seo.disparate@gmail.com>
Mon, 17 Feb 2025 04:13:20 +0000 (13:13 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Mon, 17 Feb 2025 04:33:50 +0000 (13:33 +0900)
src/helpers.c
src/helpers.h
src/test.c

index 0b41a56a8fdb28324047919440bf6ee14dc0af6a..6c65f27419ec3ccd85a35d7e414ddb0cc4e28d88 100644 (file)
@@ -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;
+}
index f1bd808aa0ae65d35cf4036d4bd19a2f96f8227b..8a5f18e03f5b3104998152ddd469c23e3dc7e1d7 100644 (file)
@@ -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
index 2b53a322b331e8c542b044abb5afa576d8bcfa42..a84ca34c7bb7407eb87a213534c79f2c4eb58608 100644 (file)
@@ -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;