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.h | |
| 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.h')
| -rw-r--r-- | util/string-util.h | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/util/string-util.h b/util/string-util.h index fb95a740..80647c5f 100644 --- a/util/string-util.h +++ b/util/string-util.h @@ -26,6 +26,20 @@ char *strtok_len (char *s, const char *delim, size_t *len); /* Const version of strtok_len. */ const char *strtok_len_c (const char *s, const char *delim, size_t *len); +/* Simplified version of strtok_len, with a single delimiter. + * Handles escaping delimiters with \ + * Usage pattern: + * + * const char *tok = input; + * const char *delim = ';'; + * size_t tok_len = 0; + * + * while ((tok = strsplit_len (tok + tok_len, delim, &tok_len)) != NULL) { + * // do stuff with string tok of length tok_len + * } + */ +const char *strsplit_len (const char *s, char delim, size_t *len); + /* Return a talloced string with str sanitized. * * Whitespace characters (tabs and newlines) are replaced with spaces, |
