]> git.ozlabs.org Git - ccan/blobdiff - ccan/bytestring/bytestring.h
tal: allow notifiers on NULL.
[ccan] / ccan / bytestring / bytestring.h
index fd5187701b3430bd03f5cc60be080f0dfdc57b49..bc99e7951c739e94221148e12491ea994642fbae 100644 (file)
@@ -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
@@ -82,8 +94,7 @@ static inline struct bytestring bytestring_from_string(const char *s)
  */
 static inline bool bytestring_eq(struct bytestring a, struct bytestring b)
 {
-       return (a.len == b.len)
-               && (memcmp(a.ptr, b.ptr, a.len) == 0);
+       return memeq(a.ptr, a.len, b.ptr, b.len);
 }
 
 /**
@@ -137,8 +148,7 @@ static inline struct bytestring bytestring_slice(struct bytestring s,
 static inline bool bytestring_starts(struct bytestring s,
                                     struct bytestring prefix)
 {
-       return (s.len >= prefix.len) && (memcmp(s.ptr,
-                                               prefix.ptr, prefix.len) == 0);
+       return memstarts(s.ptr, s.len, prefix.ptr, prefix.len);
 }
 
 /**
@@ -151,8 +161,7 @@ static inline bool bytestring_starts(struct bytestring s,
 static inline bool bytestring_ends(struct bytestring s,
                                   struct bytestring suffix)
 {
-       return (s.len >= suffix.len) && (memcmp(s.ptr + s.len - suffix.len,
-                                               suffix.ptr, suffix.len) == 0);
+       return memends(s.ptr, s.len, suffix.ptr, suffix.len);
 }
 
 /**