commit 88c361d98175622a93cca38c824cea335435cfff
parent 808d5506ce033fee5b1e8b002e9d47a92355a582
Author: Louis Burda <quent.burda@gmail.com>
Date: Sun, 4 Jun 2023 03:18:32 +0200
Add strvec_find
Diffstat:
4 files changed, 21 insertions(+), 2 deletions(-)
diff --git a/include/strvec.h b/include/strvec.h
@@ -60,6 +60,7 @@ void strvec_replace(struct strvec *strvec, size_t index, const char *str);
void strvec_remove(struct strvec *strvec, size_t index, size_t n);
int strvec_remove_str(struct strvec *strvec, const char *str,
const struct allocator *allocator);
+ssize_t strvec_find(struct strvec *strvec, size_t start, const char *str);
char *strvec_join(struct strvec *strvec, const char *sep,
const struct allocator *allocator, int *rc);
diff --git a/libstrvec.api b/libstrvec.api
@@ -21,7 +21,7 @@ strvec_replace
strvec_remove
strvec_remove_str
-
+strvec_find
strvec_join
strvec_iter_fwd
diff --git a/libstrvec.lds b/libstrvec.lds
@@ -23,7 +23,7 @@ LIBSTRVEC_1.0.0 {
strvec_remove;
strvec_remove_str;
-
+ strvec_find;
strvec_join;
strvec_iter_fwd;
diff --git a/src/strvec.c b/src/strvec.c
@@ -2,6 +2,7 @@
#include "dvec.h"
#include "allocator.h"
+#include <stddef.h>
#include <string.h>
int
@@ -229,6 +230,23 @@ strvec_remove_str(struct strvec *strvec, const char *str,
return 0;
}
+ssize_t
+strvec_find(struct strvec *strvec, size_t start, const char *str)
+{
+ const char **ent;
+ size_t i;
+
+ LIBSTRVEC_ABORT_ON_ARGS(!strvec);
+
+ for (i = start; i < strvec->vec.len; i++) {
+ ent = strvec_at(strvec, i);
+ if (!str && !*ent) return (ssize_t) i;
+ if (!strcmp(str, *ent)) return (ssize_t) i;
+ }
+
+ return -1;
+}
+
char *
strvec_join(struct strvec *strvec, const char *sep,
const struct allocator *allocator, int *_rc)