diff options
| author | Lennart Poettering <lennart@poettering.net> | 2018-05-30 13:07:37 +0200 |
|---|---|---|
| committer | Lennart Poettering <lennart@poettering.net> | 2018-05-30 13:07:40 +0200 |
| commit | 9b8ff18319965c7e599a70c2a1fab65823b53536 (patch) | |
| tree | e80be9dab77e326172caf0e5ceaf823b4fad32d5 /src/basic/string-util.h | |
| parent | e6ebebbe6aef5830793af515c2e3a998bde920b9 (diff) | |
| download | systemd-9b8ff18319965c7e599a70c2a1fab65823b53536.tar.gz | |
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 5a10eeabfe..aa00724266 100644 --- a/src/basic/string-util.h +++ b/src/basic/string-util.h @@ -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; +} |
