[Patch v8 05/18] util/string-util: add strnspn

david at tethera.net david at tethera.net
Fri Dec 21 05:08:14 PST 2012


From: David Bremner <bremner at debian.org>

this is like the standard strspn, but with a limit on how much of a
prefix is tested.
---
 util/string-util.c |   12 ++++++++++++
 util/string-util.h |    5 +++++
 2 files changed, 17 insertions(+)

diff --git a/util/string-util.c b/util/string-util.c
index b9039f4..7d5deaf 100644
--- a/util/string-util.c
+++ b/util/string-util.c
@@ -35,6 +35,18 @@ strtok_len (char *s, const char *delim, size_t *len)
 }
 
 
+size_t
+strnspn (const char *str, size_t len,  const char *accept)
+{
+    size_t count;
+
+    for (count = 0; count < len && str[count] != '\0'; count++) {
+	if (strchr (accept, str[count]) == NULL)
+	    break;
+    }
+    return count;
+}
+
 int
 double_quote_str (void *ctx, const char *str,
 		  char **buf, size_t *len)
diff --git a/util/string-util.h b/util/string-util.h
index 4fc7942..b46f981 100644
--- a/util/string-util.h
+++ b/util/string-util.h
@@ -19,6 +19,11 @@
 
 char *strtok_len (char *s, const char *delim, size_t *len);
 
+/* like strspn, but consider at most `n' characters of 's'. `accept'
+ * must be null terminated; `s' may be null terminated.
+ */
+size_t strnspn (const char *s, size_t n, const char *accept);
+
 /* Copy str to dest, surrounding with double quotes.
  * Any internal double-quotes are doubled, i.e. a"b -> "a""b"
  *
-- 
1.7.10.4



More information about the notmuch mailing list