diff options
| author | David Bremner <david@tethera.net> | 2020-08-08 11:16:48 -0300 |
|---|---|---|
| committer | David Bremner <david@tethera.net> | 2021-02-06 19:06:49 -0400 |
| commit | 3fb123f215668a54cd6084b2a520f767d2be6712 (patch) | |
| tree | b9b29a1f2392ab8bdb5dbcbce528ff7e4895c341 /util/string-util.c | |
| parent | 319efe21c9d2754c99951cecc71184faf7ca9054 (diff) | |
util: add strsplit_len: simplified strtok with delimiter escaping
This will be used to make iterators for configuration values.
Diffstat (limited to 'util/string-util.c')
| -rw-r--r-- | util/string-util.c | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/util/string-util.c b/util/string-util.c index de8430b2..27f8a26b 100644 --- a/util/string-util.c +++ b/util/string-util.c @@ -24,6 +24,7 @@ #include <ctype.h> #include <errno.h> +#include <stdbool.h> char * strtok_len (char *s, const char *delim, size_t *len) @@ -38,6 +39,28 @@ strtok_len (char *s, const char *delim, size_t *len) } const char * +strsplit_len (const char *s, char delim, size_t *len) +{ + bool escaping = false; + size_t count = 0; + + /* Skip initial unescaped delimiters */ + while (*s && *s == delim) + s++; + + while (s[count] && (escaping || s[count] != delim)) { + escaping = (s[count] == '\\'); + count++; + } + + if (count==0) + return NULL; + + *len = count; + return s; +} + +const char * strtok_len_c (const char *s, const char *delim, size_t *len) { /* strtok_len is already const-safe, but we can't express both |
