aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@gmail.com>2025-01-31 11:23:50 -0500
committerJoseph Hunkeler <jhunkeler@gmail.com>2025-01-31 11:23:50 -0500
commit727e176e30cf05ee2d88c28bb44bb0364d8e4e72 (patch)
tree7fde41c3f1a6f1b2f8147c134400776fe014020e
parent3ef26a80fc365ec9c857aaf40741fa6acf8987fa (diff)
downloadstasis-727e176e30cf05ee2d88c28bb44bb0364d8e4e72.tar.gz
Implements function strlist_contains
-rw-r--r--src/lib/core/include/strlist.h1
-rw-r--r--src/lib/core/strlist.c42
2 files changed, 43 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 ec7b3f4..704cf9c 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`