From 9a0ee7624c19acf7fabb311e6c01c45cc72a1da3 Mon Sep 17 00:00:00 2001 From: Phil Jones Date: Sun, 7 Nov 2021 18:12:52 +0000 Subject: Initial working build. --- src/string_vec.c | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) (limited to 'src/string_vec.c') diff --git a/src/string_vec.c b/src/string_vec.c index 94f41fa..f747339 100644 --- a/src/string_vec.c +++ b/src/string_vec.c @@ -1,15 +1,16 @@ +#define _GNU_SOURCE #include #include #include "string_vec.h" -static int cmpstringp(const void *a, const void *b) +static int cmpstringp(const void *restrict a, const void *restrict b) { /* * We receive pointers to the array elements (which are pointers to * char), so convert and dereference them for comparison. */ - const char *str1 = *(const char **)a; - const char *str2 = *(const char **)b; + const char *restrict str1 = *(const char **)a; + const char *restrict str2 = *(const char **)b; /* * Ensure any NULL strings are shoved to the end. @@ -20,10 +21,10 @@ static int cmpstringp(const void *a, const void *b) if (str2 == NULL) { return -1; } - return strcmp(str1, str2); + return strcasecmp(str1, str2); } -struct string_vec string_vec_create() +struct string_vec string_vec_create(void) { struct string_vec vec = { .count = 0, @@ -41,6 +42,21 @@ void string_vec_destroy(struct string_vec *restrict vec) free(vec->buf); } +struct string_vec string_vec_copy(struct string_vec *restrict vec) +{ + struct string_vec copy = { + .count = vec->count, + .size = vec->size, + .buf = calloc(vec->size, sizeof(char *)) + }; + + for (size_t i = 0; i < vec->count; i++) { + copy.buf[i] = strdup(vec->buf[i]); + } + + return copy; +} + void string_vec_add(struct string_vec *restrict vec, const char *restrict str) { if (vec->count == vec->size) { @@ -71,12 +87,12 @@ void string_vec_uniq(struct string_vec *restrict vec) } struct string_vec string_vec_filter( - struct string_vec *restrict vec, + const struct string_vec *restrict vec, const char *restrict substr) { struct string_vec filt = string_vec_create(); for (size_t i = 0; i < vec->count; i++) { - if (strstr(vec->buf[i], substr) != NULL) { + if (strcasestr(vec->buf[i], substr) != NULL) { string_vec_add(&filt, vec->buf[i]); } } -- cgit v1.2.3