mirror of
https://gitlab.freedesktop.org/libinput/libei.git
synced 2025-12-20 17:20:05 +01:00
test: add a bunch of strv helpers
Taken from libinput Part-of: <https://gitlab.freedesktop.org/libinput/libei/-/merge_requests/337>
This commit is contained in:
parent
efdc58e094
commit
dbeff9a90d
2 changed files with 87 additions and 0 deletions
|
|
@ -220,6 +220,86 @@ strreplace(const char *string, const char *separator, const char *replacement)
|
||||||
return tmp;
|
return tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t
|
||||||
|
strv_len(char **strv)
|
||||||
|
{
|
||||||
|
if (!strv)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
size_t size = 1;
|
||||||
|
while (*strv) {
|
||||||
|
size++;
|
||||||
|
strv++;
|
||||||
|
}
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
|
char **
|
||||||
|
strv_append_take(char **strv, char **str)
|
||||||
|
{
|
||||||
|
if (str && *str) {
|
||||||
|
size_t len = max(strv_len(strv) + 1, 2);
|
||||||
|
|
||||||
|
char **s = realloc(strv, len * sizeof(*strv));
|
||||||
|
if (!s)
|
||||||
|
abort();
|
||||||
|
s[len - 1] = NULL;
|
||||||
|
s[len - 2] = *str;
|
||||||
|
*str = NULL;
|
||||||
|
return s;
|
||||||
|
} else {
|
||||||
|
return strv;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
char **
|
||||||
|
strv_append_strdup(char **strv, const char *str)
|
||||||
|
{
|
||||||
|
char *dup = xstrdup(str);
|
||||||
|
return strv_append_take(strv, &dup);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
strv_find(char **strv, const char *needle, size_t *index_out)
|
||||||
|
{
|
||||||
|
if (!strv)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
size_t index = 0;
|
||||||
|
char **s = strv;
|
||||||
|
while (*s != NULL) {
|
||||||
|
if (streq(*s, needle)) {
|
||||||
|
if (index_out)
|
||||||
|
*index_out = index;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
s++;
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
strv_find_substring(char **strv, const char *needle, size_t *index_out)
|
||||||
|
{
|
||||||
|
if (!strv || !needle)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
size_t index = 0;
|
||||||
|
char **s = strv;
|
||||||
|
while (*s != NULL) {
|
||||||
|
if (strstr(*s, needle)) {
|
||||||
|
if (index_out)
|
||||||
|
*index_out = index;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
s++;
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
char **
|
char **
|
||||||
strv_from_mem(const uint8_t *buffer, size_t sz, size_t stride)
|
strv_from_mem(const uint8_t *buffer, size_t sz, size_t stride)
|
||||||
|
|
|
||||||
|
|
@ -254,9 +254,16 @@ xatod(const char *str, double *val)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t strv_len(char **strv);
|
||||||
|
/* Takes ownership of the string and appends it to strv, s is set to NULL */
|
||||||
|
char **strv_append_take(char **strv, char **s);
|
||||||
|
char **strv_append_strdup(char **strv, const char *s);
|
||||||
char **strv_from_string(const char *string, const char *separator);
|
char **strv_from_string(const char *string, const char *separator);
|
||||||
char *strv_join(char **strv, const char *separator);
|
char *strv_join(char **strv, const char *separator);
|
||||||
|
|
||||||
|
bool strv_find(char **strv, const char *needle, size_t *index_out);
|
||||||
|
bool strv_find_substring(char **strv, const char *needle, size_t *index_out);
|
||||||
|
|
||||||
static inline void
|
static inline void
|
||||||
strv_free(char **strv) {
|
strv_free(char **strv) {
|
||||||
char **s = strv;
|
char **s = strv;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue