mirror of
https://git.notmuchmail.org/git/notmuch
synced 2024-11-26 20:57:58 +01:00
util: add strsplit_len: simplified strtok with delimiter escaping
This will be used to make iterators for configuration values.
This commit is contained in:
parent
319efe21c9
commit
3fb123f215
2 changed files with 37 additions and 0 deletions
|
@ -24,6 +24,7 @@
|
|||
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
char *
|
||||
strtok_len (char *s, const char *delim, size_t *len)
|
||||
|
@ -37,6 +38,28 @@ strtok_len (char *s, const char *delim, size_t *len)
|
|||
return *len ? s : NULL;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue