[ccan] [PATCH 11/11] bytestring: Add initializer macro

David Gibson david at gibson.dropbear.id.au
Sun Oct 12 03:43:36 AEDT 2014


From: David Gibson <dgibson at redhat.com>

The BYTESTRING macro is constant, since it's designed to take a string
literal, but it doesn't count as constant since it involves an (inlined)
function call.  That means it can't be used in a static storage duration
initializer.

Unfortunately, I can't see a portable way to make something which can be
used as an initializer, but which can also be used in other contexts where
BYTESTRING() can be used, so this patch introduces BYTESTRING_INIT() which
is similar to BYTESTRING() but produces an initializer.

Signed-off-by: David Gibson <dgibson at redhat.com>
---
 ccan/bytestring/bytestring.h | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/ccan/bytestring/bytestring.h b/ccan/bytestring/bytestring.h
index 512787d..b3dfcb1 100644
--- a/ccan/bytestring/bytestring.h
+++ b/ccan/bytestring/bytestring.h
@@ -55,6 +55,18 @@ bytestring(const char *p, size_t l)
  */
 #define BYTESTRING(s) (bytestring((s), ARRAY_SIZE(s) - 1))
 
+/**
+ * BYTESTRING_INIT - bytestring initializer
+ * @s: string literal
+ *
+ * Produces an initializer for a bytestring from a literal string.
+ * The resulting bytestring will not include the terminating \0, but
+ * will include any internal \0s.
+ *
+ * Example:
+ *	static const struct bytestring CONSTANT = BYTESTRING_INIT("CONSTANT");
+ */
+#define BYTESTRING_INIT(s) { .ptr = (s), .len = ARRAY_SIZE(s) - 1}
 
 /**
  * bytestring_from_string - construct a bytestring from a NUL terminated string
-- 
1.9.3



More information about the ccan mailing list