Browse Source

util: bytes_eq and bytes_assimilate

Luke Dashjr 11 years ago
parent
commit
d392332c5c
1 changed files with 21 additions and 0 deletions
  1. 21 0
      util.h

+ 21 - 0
util.h

@@ -448,6 +448,14 @@ ssize_t bytes_find(const bytes_t * const b, const uint8_t needle)
 	return -1;
 }
 
+static inline
+bool bytes_eq(const bytes_t * const a, const bytes_t * const b)
+{
+	if (a->sz != b->sz)
+		return false;
+	return !memcmp(a->buf, b->buf, a->sz);
+}
+
 extern void _bytes_alloc_failure(size_t);
 
 static inline
@@ -519,6 +527,19 @@ void bytes_cpy(bytes_t *dst, const bytes_t *src)
 	memcpy(dst->buf, src->buf, dst->sz);
 }
 
+// Efficiently moves the data from src to dst, emptying src in the process
+static inline
+void bytes_assimilate(bytes_t * const dst, bytes_t * const src)
+{
+	void * const buf = dst->buf;
+	const size_t allocsz = dst->allocsz;
+	*dst = *src;
+	*src = (bytes_t){
+		.buf = buf,
+		.allocsz = allocsz,
+	};
+}
+
 static inline
 void bytes_assimilate_raw(bytes_t * const b, void * const buf, const size_t bufsz, const size_t buflen)
 {