From: Cody P Schafer <dev@codyps.com>
To: ccan@lists.ozlabs.org
Cc: Cody P Schafer <dev@codyps.com>
Subject: [PATCH v4 4/4] mem: add memends_str() helper for symmetry
Date: Sat, 5 Sep 2015 21:21:06 -0400 [thread overview]
Message-ID: <1441502466-4754-5-git-send-email-dev@codyps.com> (raw)
In-Reply-To: <1441502466-4754-1-git-send-email-dev@codyps.com>
Signed-off-by: Cody P Schafer <dev@codyps.com>
---
ccan/mem/mem.h | 17 +++++++++++++++++
ccan/mem/test/api.c | 6 +++++-
2 files changed, 22 insertions(+), 1 deletion(-)
diff --git a/ccan/mem/mem.h b/ccan/mem/mem.h
index 8d6bba9..1afe508 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 89b6acb..9ec226d 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();
--
2.5.1
_______________________________________________
ccan mailing list
ccan@lists.ozlabs.org
https://lists.ozlabs.org/listinfo/ccan
next prev parent reply other threads:[~2015-09-06 1:21 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-09-06 1:21 [PATCH v4 0/4] mem: extend with a few additional helpers similar to existing string-only functions Cody P Schafer
2015-09-06 1:21 ` [PATCH v4 1/4] mem: add mem helper functions Cody P Schafer
2015-09-06 1:21 ` [PATCH v4 2/4] bytestring: use newly added mem helpers Cody P Schafer
2015-09-06 1:21 ` [PATCH v4 3/4] mem: mark all functions as PURE Cody P Schafer
2015-09-06 1:21 ` Cody P Schafer [this message]
2015-09-06 14:24 ` [PATCH v4 0/4] mem: extend with a few additional helpers similar to existing string-only functions David Gibson
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1441502466-4754-5-git-send-email-dev@codyps.com \
--to=dev@codyps.com \
--cc=ccan@lists.ozlabs.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.