]> git.notmuchmail.org Git - notmuch/commitdiff
util: Const version of strtok_len
authorAustin Clements <amdragon@MIT.EDU>
Fri, 1 Aug 2014 02:09:52 +0000 (22:09 -0400)
committerDavid Bremner <david@tethera.net>
Wed, 6 Aug 2014 12:56:36 +0000 (09:56 -0300)
Because of limitations in the C type system, we can't a strtok_len
that can work on both const string and non-const strings.  The C
library solves this by taking a const char* and returning a char*
in functions like this (e.g., strchr), but that's not const-safe.
Solve it by introducing strtok_len_c, a version of strtok_len for
const strings.

util/string-util.c
util/string-util.h

index 3e7066cd58ea191a9e647f5a0177f5547f662388..a90501ee3e70198e652599e6da841ffe95db8f85 100644 (file)
@@ -37,6 +37,14 @@ strtok_len (char *s, const char *delim, size_t *len)
     return *len ? s : NULL;
 }
 
     return *len ? s : NULL;
 }
 
+const char *
+strtok_len_c (const char *s, const char *delim, size_t *len)
+{
+    /* strtok_len is already const-safe, but we can't express both
+     * versions in the C type system. */
+    return strtok_len ((char*)s, delim, len);
+}
+
 char *
 sanitize_string (const void *ctx, const char *str)
 {
 char *
 sanitize_string (const void *ctx, const char *str)
 {
index ccad17f1ced4f5f2a639d7891681438aaea09b56..e409cb3d2ab154664a24873cfe50e3ac34a23513 100644 (file)
@@ -23,6 +23,9 @@ extern "C" {
 
 char *strtok_len (char *s, const char *delim, size_t *len);
 
 
 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);
+
 /* Return a talloced string with str sanitized.
  *
  * Whitespace characters (tabs and newlines) are replaced with spaces,
 /* Return a talloced string with str sanitized.
  *
  * Whitespace characters (tabs and newlines) are replaced with spaces,