summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCody P Schafer <dev@codyps.com>2015-09-05 21:21:06 -0400
committerDavid Gibson <david@gibson.dropbear.id.au>2015-09-07 00:16:47 +1000
commit4ad5144790a12523f8a7c24c469a34907b6942a6 (patch)
tree3acee826565acf27496824ac51a2e40ad7b90993
parent2c20abc2e2fdaf1888472968e811d00260dcf8fa (diff)
mem: add memends_str() helper for symmetry
Signed-off-by: Cody P Schafer <dev@codyps.com> Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
-rw-r--r--ccan/mem/mem.h17
-rw-r--r--ccan/mem/test/api.c6
2 files changed, 22 insertions, 1 deletions
diff --git a/ccan/mem/mem.h b/ccan/mem/mem.h
index 8d6bba94..1afe508b 100644
--- a/ccan/mem/mem.h
+++ b/ccan/mem/mem.h
@@ -183,4 +183,21 @@ static inline bool memends(const void *s, size_t s_len, const void *suffix, size
suffix, suffix_len) == 0);
}
+/**
+ * memends_str - Does this byte array end with a string suffix?
+ * @a: byte array
+ * @al: length in bytes
+ * @s: string suffix
+ *
+ * Example:
+ * if (memends_str(somebytes, bytes_len, "It")) {
+ * printf("somebytes ends with with 'It'\n");
+ * }
+ */
+PURE_FUNCTION
+static inline bool memends_str(const void *a, size_t al, const char *s)
+{
+ return memends(a, al, s, strlen(s));
+}
+
#endif /* CCAN_MEM_H */
diff --git a/ccan/mem/test/api.c b/ccan/mem/test/api.c
index 89b6acb4..9ec226da 100644
--- a/ccan/mem/test/api.c
+++ b/ccan/mem/test/api.c
@@ -12,7 +12,7 @@ int main(void)
char scan2[] = "\0\0\0b";
/* This is how many tests you plan to run */
- plan_tests(42);
+ plan_tests(46);
ok1(memmem(haystack1, sizeof(haystack1), needle1, 2) == haystack1);
ok1(memmem(haystack1, sizeof(haystack1), needle1, 3) == NULL);
@@ -71,6 +71,10 @@ int main(void)
ok1(!memends(S("a\0bcdef"), S("a\0b")));
ok1(memends(S("a\0bcdef"), S("ef")));
+ ok1(memends_str(S("abcdef"), "abcdef"));
+ ok1(!memends_str(S("abcde\0f"), "d\0f"));
+ ok1(!memends_str(S("a\0bcdef"), "a"));
+ ok1(memends_str(S("a\0bcdef"), "ef"));
/* This exits depending on whether all tests passed */
return exit_status();