aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@users.noreply.github.com>2025-02-12 17:05:02 -0500
committerGitHub <noreply@github.com>2025-02-12 17:05:02 -0500
commit0e834d092251a8dfcb120e4274e2088945ffc015 (patch)
tree06a510c0ae8af2b8a29450b981749dc8fba05b55
parent3c62bb8b063898e66d816020fc630a672eb34afc (diff)
parentd716b2a5f7e9f4b2cf3f5d4efbcbd2f07bb5ccb9 (diff)
downloadstasis-0e834d092251a8dfcb120e4274e2088945ffc015.tar.gz
Merge pull request #84 from jhunkeler/add-strlist-contains
Add strlist_contains function
-rw-r--r--src/lib/core/include/strlist.h1
-rw-r--r--src/lib/core/strlist.c42
-rw-r--r--tests/test_strlist.c48
3 files changed, 91 insertions, 0 deletions
diff --git a/src/lib/core/include/strlist.h b/src/lib/core/include/strlist.h
index cdbfc01..18c60eb 100644
--- a/src/lib/core/include/strlist.h
+++ b/src/lib/core/include/strlist.h
@@ -40,6 +40,7 @@ void strlist_set(struct StrList **pStrList, size_t index, char *value);
size_t strlist_count(struct StrList *pStrList);
void strlist_reverse(struct StrList *pStrList);
void strlist_sort(struct StrList *pStrList, unsigned int mode);
+int strlist_contains(struct StrList *pStrList, const char *value, size_t *index_of);
int strlist_append_file(struct StrList *pStrList, char *path, ReaderFn *readerFn);
void strlist_append_strlist(struct StrList *pStrList1, struct StrList *pStrList2);
void strlist_append(struct StrList **pStrList, char *str);
diff --git a/src/lib/core/strlist.c b/src/lib/core/strlist.c
index 0436f54..08f5893 100644
--- a/src/lib/core/strlist.c
+++ b/src/lib/core/strlist.c
@@ -130,6 +130,48 @@ fatal:
}
/**
+ * Is `value` present in `pStrList`?
+ * The caller should test for success before using the value `index_of`
+ *
+ * ```c
+ * const char *needle = "world!";
+ *
+ * struct StrList *haystack = strlist_init();
+ * strlist_append(&haystack, "hello");
+ * strlist_append(&haystack, "world!");
+ *
+ * size_t index_of_item;
+ * if (strlist_contains(haystack, needle, &index_of_item)) {
+ * const char *item = strlist_item(haystack, index_of_item);
+ * // index_of_item == 1
+ * // item is "world!"
+ * } else {
+ * fprintf(stderr, "%s not found\n", needle);
+ * }
+ * ```
+ *
+ * @param pStrList pointer to `StrList`
+ * @param index_of (result) index of string in `pStrList`, if found
+ * @param value string to search for in `pStrList`
+ * @return 1 found
+ * @return 0 not found
+ */
+int strlist_contains(struct StrList *pStrList, const char *value, size_t *index_of) {
+ if (pStrList == NULL) {
+ return 0;
+ }
+
+ for (size_t i = 0; i < strlist_count(pStrList); i++) {
+ const char *item = strlist_item(pStrList, i);
+ if (!strcmp(item, value)) {
+ *index_of = i;
+ return 1;
+ }
+ }
+ return 0;
+}
+
+/**
* Append the contents of `pStrList2` to `pStrList1`
* @param pStrList1 `StrList`
* @param pStrList2 `StrList`
diff --git a/tests/test_strlist.c b/tests/test_strlist.c
index 90219bd..ce38ff6 100644
--- a/tests/test_strlist.c
+++ b/tests/test_strlist.c
@@ -247,6 +247,53 @@ void test_strlist_remove() {
guard_strlist_free(&list);
}
+void test_strlist_contains() {
+ struct StrList *list = strlist_init();
+ strlist_append(&list, "abc"); // 0
+ strlist_append(&list, "abc123"); // 1
+ strlist_append(&list, "abcdef"); // 2
+ strlist_append(&list, "abc123def456"); // 3
+ strlist_append(&list, "abcdefghi"); // 4
+ strlist_append(&list, "abc123def456ghi789"); // 5
+
+ struct testcase {
+ struct StrList *haystack;
+ const char *needle;
+ int expected_retval;
+ size_t expected_index;
+ };
+
+ struct testcase tc[] = {
+ // found
+ {.haystack = list, .needle = "abc", .expected_retval = 1, .expected_index = 0},
+ {.haystack = list, .needle = "abc123", .expected_retval = 1, .expected_index = 1},
+ {.haystack = list, .needle = "abcdef", .expected_retval = 1, .expected_index = 2},
+ {.haystack = list, .needle = "abc123def456", .expected_retval = 1, .expected_index = 3},
+ {.haystack = list, .needle = "abcdefghi", .expected_retval = 1, .expected_index = 4},
+ {.haystack = list, .needle = "abc123def456ghi789", .expected_retval = 1, .expected_index = 5},
+ // not found
+ {.haystack = list, .needle = "ZabcZ", .expected_retval = 0, .expected_index = 0},
+ {.haystack = list, .needle = "Zabc123Z", .expected_retval = 0, .expected_index = 0},
+ {.haystack = list, .needle = "ZabcdefZ", .expected_retval = 0, .expected_index = 0},
+ {.haystack = list, .needle = "Zabc123def456Z", .expected_retval = 0, .expected_index = 0},
+ {.haystack = list, .needle = "ZabcdefghiZ", .expected_retval = 0, .expected_index = 0},
+ {.haystack = list, .needle = "Zabc123def456ghi789Z", .expected_retval = 0, .expected_index = 0},
+ };
+
+ for (size_t i = 0; i < strlist_count(list); i++) {
+ struct testcase *t = &tc[i];
+ size_t index_of = 0;
+ int result = strlist_contains(t->haystack, t->needle, &index_of);
+
+ STASIS_ASSERT(result == t->expected_retval, "unexpected return value");
+ if (result) {
+ STASIS_ASSERT(index_of == t->expected_index && strcmp(strlist_item(t->haystack, index_of), t->needle) == 0, "value at index is the wrong item in the list");
+ }
+ }
+
+ guard_strlist_free(&list);
+}
+
void test_strlist_cmp() {
struct StrList *left;
struct StrList *right;
@@ -589,6 +636,7 @@ int main(int argc, char *argv[]) {
test_strlist_append_strlist,
test_strlist_append_tokenize,
test_strlist_append_array,
+ test_strlist_contains,
test_strlist_copy,
test_strlist_remove,
test_strlist_cmp,