diff options
author | Lennart Poettering <lennart@poettering.net> | 2018-05-30 13:07:37 +0200 |
---|---|---|
committer | Sven Eden <yamakuzure@gmx.net> | 2018-08-24 16:47:08 +0200 |
commit | d88be29380c987f130a7abaa007e998f0e5c8413 (patch) | |
tree | 286556938ec969cfbf305fc22e683ab3ff975756 /src/basic/string-util.h | |
parent | e0859c38297467dc93b122aea38c806f2483e7a3 (diff) |
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.
Diffstat (limited to 'src/basic/string-util.h')
-rw-r--r-- | src/basic/string-util.h | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/src/basic/string-util.h b/src/basic/string-util.h index 56a64d7f5..52a1d7f2b 100644 --- a/src/basic/string-util.h +++ b/src/basic/string-util.h @@ -217,3 +217,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; +} |