]> git.ozlabs.org Git - ccan/blobdiff - ccan/bytestring/bytestring.h
bytestring: Add bytestring_byte() function
[ccan] / ccan / bytestring / bytestring.h
index 63472fe24d6cdfd6e84802f6103b2658a610b098..bd525601eb3a71cb40c050838db1f76268b5530b 100644 (file)
@@ -5,6 +5,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <stdbool.h>
+#include <assert.h>
 
 #include <ccan/array_size/array_size.h>
 
@@ -80,4 +81,18 @@ static inline bool bytestring_eq(struct bytestring a, struct bytestring b)
                && (memcmp(a.ptr, b.ptr, a.len) == 0);
 }
 
+/**
+ * bytestring_byte - get a byte from a bytestring
+ * @s: bytestring
+ * @n: index
+ *
+ * Return the @n-th byte from @s.  Aborts (via assert) if @n is out of
+ * range for the length of @s.
+ */
+static inline char bytestring_byte(struct bytestring s, size_t n)
+{
+       assert(n < s.len);
+       return s.ptr[n];
+}
+
 #endif /* CCAN_BYTESTRING_H_ */