X-Git-Url: https://git.cworth.org/git?p=notmuch;a=blobdiff_plain;f=util%2Fstring-util.c;h=03d7648d329a2e7b3667a894b558e93d440d1a6a;hp=de8430b2add34dea3fdf52507cf442c420cd170f;hb=HEAD;hpb=b23902a61158ecdbca2d9d96c6eaf509d6e3d205 diff --git a/util/string-util.c b/util/string-util.c index de8430b2..03d7648d 100644 --- a/util/string-util.c +++ b/util/string-util.c @@ -24,6 +24,7 @@ #include #include +#include char * strtok_len (char *s, const char *delim, size_t *len) @@ -37,6 +38,30 @@ 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, last_nonspace = 0; + + /* Skip initial unescaped delimiters and whitespace */ + while (*s && (*s == delim || isspace (*s))) + s++; + + while (s[count] && (escaping || s[count] != delim)) { + if (! isspace (s[count])) + last_nonspace = count; + escaping = (s[count] == '\\'); + count++; + } + + if (count == 0) + return NULL; + + *len = last_nonspace + 1; + return s; +} + const char * strtok_len_c (const char *s, const char *delim, size_t *len) { @@ -160,6 +185,7 @@ parse_boolean_term (void *ctx, const char *str, /* Parse prefix */ str = skip_space (str); const char *pos = strchr (str, ':'); + if (! pos || pos == str) goto FAIL; *prefix_out = talloc_strndup (ctx, str, pos - str);