]> git.cworth.org Git - notmuch/blobdiff - util/string-util.c
emacs: Add new option notmuch-search-hide-excluded
[notmuch] / util / string-util.c
index de8430b2add34dea3fdf52507cf442c420cd170f..03d7648d329a2e7b3667a894b558e93d440d1a6a 100644 (file)
@@ -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,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);