bytestring.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /* Licensed under LGPLv2+ - see LICENSE file for details */
  2. #ifndef CCAN_BYTESTRING_H_
  3. #define CCAN_BYTESTRING_H_
  4. #include "config.h"
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <stdbool.h>
  8. #include <assert.h>
  9. #include <ccan/array_size/array_size.h>
  10. #include <ccan/mem/mem.h>
  11. #include <ccan/compiler/compiler.h>
  12. struct bytestring {
  13. const char *ptr;
  14. size_t len;
  15. };
  16. /**
  17. * bytestring - construct a new bytestring
  18. * @p: pointer to the content of the bytestring
  19. * @l: length of the bytestring
  20. *
  21. * Builds a new bytestring starting at p, of length l.
  22. *
  23. * Example:
  24. * char x[5] = "abcde";
  25. * struct bytestring bs = bytestring(x, 5);
  26. * assert(bs.len == 5);
  27. */
  28. static inline CONST_FUNCTION struct bytestring
  29. bytestring(const char *p, size_t l)
  30. {
  31. struct bytestring bs = {
  32. .ptr = p,
  33. .len = l,
  34. };
  35. return bs;
  36. }
  37. #define bytestring_NULL bytestring(NULL, 0)
  38. /**
  39. * BYTESTRING - construct a bytestring from a string literal
  40. * @s: string literal
  41. *
  42. * Builds a new bytestring containing the given literal string, not
  43. * including the terminating \0 (but including any internal \0s).
  44. *
  45. * Example:
  46. * assert(BYTESTRING("abc\0def").len == 7);
  47. */
  48. #define BYTESTRING(s) (bytestring((s), ARRAY_SIZE(s) - 1))
  49. /**
  50. * BYTESTRING_INIT - bytestring initializer
  51. * @s: string literal
  52. *
  53. * Produces an initializer for a bytestring from a literal string.
  54. * The resulting bytestring will not include the terminating \0, but
  55. * will include any internal \0s.
  56. *
  57. * Example:
  58. * static const struct bytestring CONSTANT = BYTESTRING_INIT("CONSTANT");
  59. */
  60. #define BYTESTRING_INIT(s) { .ptr = (s), .len = ARRAY_SIZE(s) - 1}
  61. /**
  62. * bytestring_from_string - construct a bytestring from a NUL terminated string
  63. * @s: NUL-terminated string pointer
  64. *
  65. * Builds a new bytestring containing the given NUL-terminated string,
  66. * up to, but not including, the terminating \0.
  67. *
  68. * Example:
  69. * assert(bytestring_from_string("abc\0def").len == 3);
  70. */
  71. static inline struct bytestring bytestring_from_string(const char *s)
  72. {
  73. if (!s)
  74. return bytestring_NULL;
  75. return bytestring(s, strlen(s));
  76. }
  77. /**
  78. * bytestring_eq - test if bytestrings have identical content
  79. * @a, @b: bytestrings
  80. *
  81. * Returns 1 if the given bytestrings have identical length and
  82. * content, 0 otherwise.
  83. */
  84. static inline bool bytestring_eq(struct bytestring a, struct bytestring b)
  85. {
  86. return memeq(a.ptr, a.len, b.ptr, b.len);
  87. }
  88. /**
  89. * bytestring_byte - get a byte from a bytestring
  90. * @s: bytestring
  91. * @n: index
  92. *
  93. * Return the @n-th byte from @s. Aborts (via assert) if @n is out of
  94. * range for the length of @s.
  95. */
  96. static inline char bytestring_byte(struct bytestring s, size_t n)
  97. {
  98. assert(n < s.len);
  99. return s.ptr[n];
  100. }
  101. /**
  102. * bytestring_slice - extract a substring from a bytestring
  103. * @s: bytestring
  104. * @start, @end: indexes
  105. *
  106. * Return a sub-bytestring of @s, starting at byte index @start, and
  107. * running to, but not including byte @end. If @end is before start,
  108. * returns a zero-length bytestring. If @start is out of range,
  109. * return a zero length bytestring at the end of @s.
  110. *
  111. * Note that this doesn't copy or allocate anything - the returned
  112. * bytestring occupies (some of) the same memory as the given
  113. * bytestring.
  114. */
  115. static inline struct bytestring bytestring_slice(struct bytestring s,
  116. size_t start, size_t end)
  117. {
  118. if (start > s.len)
  119. start = s.len;
  120. if (end > s.len)
  121. end = s.len;
  122. if (end < start)
  123. end = start;
  124. return bytestring(s.ptr + start, end - start);
  125. }
  126. /**
  127. * bytestring_starts - test if the start of one bytestring matches another
  128. * @s, @prefix: bytestrings
  129. *
  130. * Returns true if @prefix appears as a substring at the beginning of
  131. * @s, false otherwise.
  132. */
  133. static inline bool bytestring_starts(struct bytestring s,
  134. struct bytestring prefix)
  135. {
  136. return memstarts(s.ptr, s.len, prefix.ptr, prefix.len);
  137. }
  138. /**
  139. * bytestring_ends - test if the end of one bytestring matches another
  140. * @s, @suffix: bytestrings
  141. *
  142. * Returns true if @suffix appears as a substring at the end of @s,
  143. * false otherwise.
  144. */
  145. static inline bool bytestring_ends(struct bytestring s,
  146. struct bytestring suffix)
  147. {
  148. return memends(s.ptr, s.len, suffix.ptr, suffix.len);
  149. }
  150. /**
  151. * bytestring_index - locate character in bytestring
  152. * @haystack: a bytestring
  153. * @needle: a character or byte value
  154. *
  155. * Returns a pointer to the first occurrence of @needle within
  156. * @haystack, or NULL if @needle does not appear in @haystack.
  157. */
  158. static inline const char *bytestring_index(struct bytestring haystack,
  159. char needle)
  160. {
  161. return memchr(haystack.ptr, needle, haystack.len);
  162. }
  163. /**
  164. * bytestring_rindex - locate character in bytestring
  165. * @haystack: a bytestring
  166. * @needle: a character or byte value
  167. *
  168. * Returns a pointer to the last occurrence of @needle within
  169. * @haystack, or NULL if @needle does not appear in @haystack.
  170. */
  171. static inline const char *bytestring_rindex(struct bytestring haystack,
  172. char needle)
  173. {
  174. return memrchr(haystack.ptr, needle, haystack.len);
  175. }
  176. /*
  177. * bytestring_bytestring - search for a bytestring in another bytestring
  178. * @haystack, @needle: bytestrings
  179. *
  180. * Returns a bytestring corresponding to the first occurrence of
  181. * @needle in @haystack, or bytestring_NULL if @needle is not found
  182. * within @haystack.
  183. */
  184. static inline struct bytestring bytestring_bytestring(struct bytestring haystack,
  185. struct bytestring needle)
  186. {
  187. const char *p = memmem(haystack.ptr, haystack.len,
  188. needle.ptr, needle.len);
  189. if (p)
  190. return bytestring(p, needle.len);
  191. else
  192. return bytestring_NULL;
  193. }
  194. /**
  195. * bytestring_spn - search a bytestring for a set of bytes
  196. * @s: a bytestring
  197. * @accept: a bytestring containing a set of bytes to accept
  198. *
  199. * Returns the length, in bytes, of the initial segment of @s which
  200. * consists entirely of characters in @accept.
  201. */
  202. size_t bytestring_spn(struct bytestring s, struct bytestring accept);
  203. /**
  204. * bytestring_cspn - search a bytestring for a set of bytes (complemented)
  205. * @s: a bytestring
  206. * @reject: a bytestring containing a set of bytes to reject
  207. *
  208. * Returns the length, in bytes, of the initial segment of @s which
  209. * consists entirely of characters not in @reject.
  210. */
  211. size_t bytestring_cspn(struct bytestring s, struct bytestring reject);
  212. /**
  213. * bytestring_splitchr_first - split a bytestring on a single character delimiter
  214. * @whole: a bytestring
  215. * @delim: delimiter character
  216. *
  217. * Returns the first @delim delimited substring of @whole.
  218. */
  219. struct bytestring bytestring_splitchr_first(struct bytestring whole,
  220. char delim);
  221. /**
  222. * bytestring_splitchr_next - split a bytestring on a single character delimiter
  223. * @whole: a bytestring
  224. * @delim: delimiter character
  225. * @prev: last substring
  226. *
  227. * Returns the next @delim delimited substring of @whole after @prev.
  228. */
  229. struct bytestring bytestring_splitchr_next(struct bytestring whole,
  230. char delim, struct bytestring prev);
  231. #define bytestring_foreach_splitchr(_s, _w, _delim) \
  232. for ((_s) = bytestring_splitchr_first((_w), (_delim)); \
  233. (_s).ptr; \
  234. (_s) = bytestring_splitchr_next((_w), (_delim), (_s)))
  235. /**
  236. * bytestring_splitchrs_first - split a bytestring on a set of delimiter
  237. * characters
  238. * @whole: a bytestring
  239. * @delim: delimiter characters
  240. *
  241. * Returns the first substring of @whole delimited by any character in
  242. * @delim.
  243. */
  244. struct bytestring bytestring_splitchrs_first(struct bytestring whole,
  245. struct bytestring delim);
  246. /**
  247. * bytestring_splitchr_next - split a bytestring on a set of delimiter
  248. * characters
  249. * @whole: a bytestring
  250. * @delim: delimiter character
  251. * @prev: last substring
  252. *
  253. * Returns the next @delim delimited substring of @whole after @prev.
  254. */
  255. struct bytestring bytestring_splitchrs_next(struct bytestring whole,
  256. struct bytestring delim,
  257. struct bytestring prev);
  258. #define bytestring_foreach_splitchrs(_s, _w, _delim) \
  259. for ((_s) = bytestring_splitchrs_first((_w), (_delim)); \
  260. (_s).ptr; \
  261. (_s) = bytestring_splitchrs_next((_w), (_delim), (_s)))
  262. /**
  263. * bytestring_splitstr_first - split a bytestring on a delimiter string
  264. * @whole: a bytestring
  265. * @delim: delimiter substring
  266. *
  267. * Returns the first substring of @whole delimited by the substring in
  268. * @delim.
  269. */
  270. struct bytestring bytestring_splitstr_first(struct bytestring whole,
  271. struct bytestring delim);
  272. /**
  273. * bytestring_splitstr_next - split a bytestring on a delimiter string
  274. * @whole: a bytestring
  275. * @delim: delimiter string
  276. * @prev: last substring
  277. *
  278. * Returns the next @delim delimited substring of @whole after @prev.
  279. */
  280. struct bytestring bytestring_splitstr_next(struct bytestring whole,
  281. struct bytestring delim,
  282. struct bytestring prev);
  283. #define bytestring_foreach_splitstr(_s, _w, _delim) \
  284. for ((_s) = bytestring_splitstr_first((_w), (_delim)); \
  285. (_s).ptr; \
  286. (_s) = bytestring_splitstr_next((_w), (_delim), (_s)))
  287. #endif /* CCAN_BYTESTRING_H_ */