summaryrefslogtreecommitdiff
path: root/src/string_vec.c
diff options
context:
space:
mode:
authorPhil Jones <philj56@gmail.com>2021-11-17 01:15:13 +0000
committerPhil Jones <philj56@gmail.com>2021-11-17 01:15:13 +0000
commit7562d7b539d8013376de2cff494231ba307f4ee1 (patch)
tree1eb4e8b28ed10da0e7a582cc4a38cfc37cef3ff8 /src/string_vec.c
parent49c7405b6a88e56bb69e12189adb719927343e07 (diff)
Add sorting by run frequency.
This implements a rofi-like run cache. Other smaller changes include simplification of resize logic now that there's only one surface.
Diffstat (limited to 'src/string_vec.c')
-rw-r--r--src/string_vec.c16
1 files changed, 11 insertions, 5 deletions
diff --git a/src/string_vec.c b/src/string_vec.c
index d3ac9fe..3c77f6f 100644
--- a/src/string_vec.c
+++ b/src/string_vec.c
@@ -1,4 +1,5 @@
-#define _GNU_SOURCE
+#define _GNU_SOURCE /* Required for strcasecmp */
+#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "string_vec.h"
@@ -7,8 +8,8 @@
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.
+ * For qsort we receive pointers to the array elements (which are
+ * pointers to char), so convert and dereference them for comparison.
*/
const char *restrict str1 = *(const char **)a;
const char *restrict str2 = *(const char **)b;
@@ -52,7 +53,7 @@ struct string_vec string_vec_copy(struct string_vec *restrict vec)
};
for (size_t i = 0; i < vec->count; i++) {
- copy.buf[i] = strdup(vec->buf[i]);
+ copy.buf[i] = xstrdup(vec->buf[i]);
}
return copy;
@@ -64,7 +65,7 @@ void string_vec_add(struct string_vec *restrict vec, const char *restrict str)
vec->size *= 2;
vec->buf = xrealloc(vec->buf, vec->size * sizeof(vec->buf[0]));
}
- vec->buf[vec->count] = strdup(str);
+ vec->buf[vec->count] = xstrdup(str);
vec->count++;
}
@@ -87,6 +88,11 @@ void string_vec_uniq(struct string_vec *restrict vec)
vec->count = count;
}
+char **string_vec_find(struct string_vec *restrict vec, const char * str)
+{
+ return bsearch(&str, vec->buf, vec->count, sizeof(vec->buf[0]), cmpstringp);
+}
+
struct string_vec string_vec_filter(
const struct string_vec *restrict vec,
const char *restrict substr)