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
committerLennart Poettering <lennart@poettering.net>2018-05-30 13:07:40 +0200
commit9b8ff18319965c7e599a70c2a1fab65823b53536 (patch)
treee80be9dab77e326172caf0e5ceaf823b4fad32d5 /src/basic/string-util.h
parente6ebebbe6aef5830793af515c2e3a998bde920b9 (diff)
downloadsystemd-9b8ff18319965c7e599a70c2a1fab65823b53536.tar.gz
systemd-9b8ff18319965c7e599a70c2a1fab65823b53536.tar.bz2
systemd-9b8ff18319965c7e599a70c2a1fab65823b53536.zip
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 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;
+}