summaryrefslogtreecommitdiff
path: root/src/basic/string-util.h
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2018-05-30 13:07:37 +0200
committerSven Eden <yamakuzure@gmx.net>2018-08-24 16:47:08 +0200
commitd88be29380c987f130a7abaa007e998f0e5c8413 (patch)
tree286556938ec969cfbf305fc22e683ab3ff975756 /src/basic/string-util.h
parente0859c38297467dc93b122aea38c806f2483e7a3 (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.h18
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;
+}