]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: ist: add ist0() to add a trailing zero to a string.
authorWilly Tarreau <w@1wt.eu>
Thu, 19 Oct 2017 04:28:23 +0000 (06:28 +0200)
committerWilly Tarreau <w@1wt.eu>
Thu, 19 Oct 2017 13:01:08 +0000 (15:01 +0200)
This function modifies the string to add a zero after the end, and returns
the start pointer. The purpose is to use it on strings extracted by parsers
from larger strings cut with delimiters that are not important and can be
destroyed. It allows any such string to be used with regular string
functions. It's also convenient to use with printf() to show data extracted
from writable areas.

include/common/ist.h

index c57aa8977a45726c1aa88639d28b89dc56cd43d9..bd98ab008a0a892047cc8a0b647d59faeb2201dc 100644 (file)
@@ -98,6 +98,21 @@ static inline struct ist ist2(const void *ptr, size_t len)
        return (struct ist){ .ptr = (char *)ptr, .len = len };
 }
 
+/* This function MODIFIES the string to add a zero AFTER the end, and returns
+ * the start pointer. The purpose is to use it on strings extracted by parsers
+ * from larger strings cut with delimiters that are not important and can be
+ * destroyed. It allows any such string to be used with regular string
+ * functions. It's also convenient to use with printf() to show data extracted
+ * from writable areas. The caller is obviously responsible for ensuring that
+ * the string is valid and that the first byte past the end is writable. If
+ * these conditions cannot be satisfied, use istpad() below instead.
+ */
+static inline char *ist0(struct ist ist)
+{
+       ist.ptr[ist.len] = 0;
+       return ist.ptr;
+}
+
 /* returns the length of the string */
 static inline size_t istlen(const struct ist ist)
 {