+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)
+{
+ /* 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 *out, *loop;
+
+ if (! str)
+ return NULL;
+
+ out = talloc_strdup (ctx, str);
+ if (! out)
+ return NULL;
+
+ for (loop = out; *loop; loop++) {
+ if (*loop == '\t' || *loop == '\n')
+ *loop = ' ';
+ else if ((unsigned char) (*loop) < 32)
+ *loop = '?';
+ }
+
+ return out;
+}
+