string-util: add new memory_startswith() helper

We have code like this at various placer, let's make things shorter and
more readable with a helper for it.
This commit is contained in:
Lennart Poettering
2018-05-30 13:07:40 +02:00
parent e6ebebbe6a
commit 9b8ff18319
2 changed files with 31 additions and 0 deletions
+18
View File
@@ -209,3 +209,21 @@ static inline size_t strlen_ptr(const char *s) {
return strlen(s);
}
/* Like startswith(), but operates on arbitrary memory blocks */
static inline void *memory_startswith(const void *p, size_t sz, const char *token) {
size_t n;
assert(token);
n = strlen(token);
if (sz < n)
return NULL;
assert(p);
if (memcmp(p, token, n) != 0)
return NULL;
return (uint8_t*) p + n;
}