[ccan] [PATCH 1/3] mem: add mem helper functions

David Gibson david at gibson.dropbear.id.au
Wed Aug 19 09:08:13 AEST 2015


On Mon, Aug 17, 2015 at 08:33:29PM -0400, Cody P Schafer wrote:
> Signed-off-by: Cody P Schafer <dev at codyps.com>

Concept looks fine.  A number of minor nits.

> ---
>  ccan/mem/mem.c      |  23 +++++++++
>  ccan/mem/mem.h      | 146 ++++++++++++++++++++++++++++++++++++++++++++++++++++
>  ccan/mem/test/api.c |  37 ++++++++++++-
>  3 files changed, 205 insertions(+), 1 deletion(-)
> 
> diff --git a/ccan/mem/mem.c b/ccan/mem/mem.c
> index 853f975..148dbed 100644
> --- a/ccan/mem/mem.c
> +++ b/ccan/mem/mem.c
> @@ -40,3 +40,26 @@ void *memrchr(const void *s, int c, size_t n)
>  	return NULL;
>  }
>  #endif
> +
> +void *mempbrkm(const void *data_, size_t len, const void *accept_, size_t accept_len)
> +{
> +	const char *data = data_, *accept = accept_;
> +	size_t i, j;

Blank line after definitions, please.

> +	for (i = 0; i < len; i++)
> +		for (j = 0; j < accept_len; j++)
> +			if (accept[j] == data[i])
> +				return (void *)&data[i];
> +	return NULL;
> +}
> +
> +void *memcchr(void const *data, int c, size_t data_len)
> +{
> +	char const *p = data;

Ditto.

> +	while((size_t)(p - ((char const *)data)) < data_len) {

Just use a size_t i and a for loop.  It's slightly longer but clearer
and more consistent with the function above.


> +		if (*p != c)
> +			return (char *)p;
> +		p++;
> +	}
> +
> +	return NULL;
> +}
> diff --git a/ccan/mem/mem.h b/ccan/mem/mem.h
> index dcb44b8..8f68a06 100644
> --- a/ccan/mem/mem.h
> +++ b/ccan/mem/mem.h
> @@ -5,6 +5,7 @@
>  #include "config.h"
>  
>  #include <string.h>
> +#include <stdbool.h>
>  
>  #if !HAVE_MEMMEM
>  void *memmem(const void *haystack, size_t haystacklen,
> @@ -15,4 +16,149 @@ void *memmem(const void *haystack, size_t haystacklen,
>  void *memrchr(const void *s, int c, size_t n);
>  #endif
>  
> +/**
> + * mempbrkm - locates the first occurrence in @data of any bytes in @accept
> + * @data: where we search
> + * @len: length of data in bytes
> + * @accept: array of bytes we search for
> + * @accept_len: # of bytes in accept
> + *
> + * Returns a pointer to the byte in @data that matches one of the bytes in
> + * @accept, or NULL if no such byte is found.
> + *
> + * Example:
> + *	char somebytes[] = "HI";
> + *	size_t bytes_len = strlen(somebytes);
> + *	char otherbytes[] = "Hello world";
> + *	size_t otherbytes_len = strlen(otherbytes);
> + *	char *r = mempbrkm(somebytes, bytes_len, otherbytes, otherbytes_len);

It would be nice to have an example where both strings include \0 to
demonstrate the use of mempbrkm() as opposed to mempbrk().

> + *	if (r) {
> + *		printf("Found %c\n", *r);
> + *	} else {
> + *		printf("Nada\n");
> + *	}
> + *
> + */
> +void *mempbrkm(const void *data, size_t len, const void *accept, size_t accept_len);
> +
> +/**
> + * mempbrk - locates the first occurrence in @data of any bytes in @accept
> + * @data: where we search
> + * @len: length of data in bytes
> + * @accept: NUL terminated string containing the bytes we search for
> + *
> + * Returns a pointer to the byte in @data that matches one of the bytes in
> + * @accept, or NULL if no such byte is found.
> + *
> + * Example:
> + *
> + *	r = mempbrk(somebytes, bytes_len, "world");
> + *	if (r) {
> + *		printf("Found %c\n", *r);
> + *	} else {
> + *		printf("Nada\n");
> + *	}
> + */
> +#define mempbrk(data, len, accept) mempbrkm(data, len, accept, strlen(accept))
> +
> +/**
> + * memcchr - scan memory until a character does _not_ match @c
> + * @data: pointer to memory to scan
> + * @data_len: length of data
> + * @c: character to scan for
> + *
> + * The compliment of memchr().

s/compliment/complement/.

> + * Returns a pointer to the first character which is _not_ @c. If all memory in
> + * @data is @c, returns NULL.
> + *
> + * Example:
> + *	r = memcchr(somebytes, ' ', bytes_len);
> + *	if (r) {
> + *		printf("Found %c after trimming spaces\n", *r);
> + *	}
> + */
> +void *memcchr(void const *data, int c, size_t data_len);
> +
> +/**
> + * memstarts - determine if @data starts with @prefix
> + * @data: does this begin with @prefix?
> + * @data_len: bytes in @data
> + * @prefix: does @data begin with these bytes?
> + * @prefix_len: bytes in @prefix
> + *
> + * Returns true if @data starts with @prefix, otherwise return false.
> + *
> + * Example:
> + *	if (memstarts(somebytes, bytes_len, otherbytes, otherbytes_len)) {
> + *		printf("somebytes starts with otherbytes!\n");
> + *	}
> + */
> +static inline bool memstarts(void const *data, size_t data_len,
> +		void const *prefix, size_t prefix_len)
> +{
> +	if (prefix_len > data_len)
> +		return false;
> +	return !memcmp(data, prefix, prefix_len);

I'd prefer to see the order changed, and use memeq() here.

> +}
> +
> +/**
> + * memeq - Are two byte arrays equal?
> + * @a: first array
> + * @al: bytes in first array
> + * @b: second array
> + * @bl: bytes in second array
> + *
> + * Example:
> + *	if (memeq(somebytes, bytes_len, otherbytes, otherbytes_len)) {
> + *		printf("memory blocks are the same!\n");
> + *	}
> + */
> +#define memeq(a, al, b, bl) (al == bl && !memcmp(a, b, bl))

Needs brackeds around the macro arguments, in case they're complex
expressions.

> +
> +/**
> + * memeqstr - Is a byte array equal to a NUL terminated string?
> + * @bytes: byte array
> + * @length: byte array length in bytes
> + * @string: NUL terminated string
> + *
> + * The '\0' byte is ignored when checking if @bytes == @string.
> + *
> + * Example:
> + *	if (memeqstr(somebytes, bytes_len, "foo")) {
> + *		printf("somebytes == 'foo'!\n");
> + *	}
> + */
> +#define memeqstr(bytes, length, string) \
> +	memeq(bytes, length, string, strlen(string))
> +
> +/**
> + * memstarts_str - Does this byte array start with a string prefix?
> + * @a: byte array
> + * @al: length in bytes
> + * @s: string prefix
> + *
> + * Example:
> + *	if (memstarts_str(somebytes, bytes_len, "It")) {
> + *		printf("somebytes starts with 'It'\n");
> + *	}
> + */
> +#define memstarts_str(a, al, s) memstarts(a, al, s, strlen(s))
> +
> +/**
> + * memends - Does this byte array end with a given byte-array suffix?
> + * @s: byte array
> + * @s_len: length in bytes
> + * @suffix: byte array suffix
> + * @suffix_len: length of suffix in bytes
> + *
> + * Returns true if @suffix appears as a substring at the end of @s,
> + * false otherwise.
> + */
> +static inline bool memends(const void *s, size_t s_len, const void *suffix, size_t suffix_len)
> +{
> +	return (s_len >= suffix_len) && (memcmp((const char *)s + s_len - suffix_len,
> +						suffix, suffix_len) == 0);
> +}
> +
>  #endif /* CCAN_MEM_H */
> diff --git a/ccan/mem/test/api.c b/ccan/mem/test/api.c
> index 2ee15c5..89b6acb 100644
> --- a/ccan/mem/test/api.c
> +++ b/ccan/mem/test/api.c
> @@ -8,9 +8,11 @@ int main(void)
>  	char haystack2[] = "ab\0ab\0ab\0ab";
>  	char needle1[] = "ab";
>  	char needle2[] = "d\0e";
> +	char scan1[] = "aaaab";
> +	char scan2[] = "\0\0\0b";
>  
>  	/* This is how many tests you plan to run */
> -	plan_tests(19);
> +	plan_tests(42);
>  
>  	ok1(memmem(haystack1, sizeof(haystack1), needle1, 2) == haystack1);
>  	ok1(memmem(haystack1, sizeof(haystack1), needle1, 3) == NULL);
> @@ -37,6 +39,39 @@ int main(void)
>  
>  	ok1(memrchr(needle1, '\0', 2) == NULL);
>  
> +#define S(x) (x), sizeof(x) - 1
> +	ok1(mempbrkm(S(haystack1), S("\0efgh")) == haystack1 + 4);
> +	ok1(mempbrkm(S(haystack1), S("jklmn")) == NULL);
> +	ok1(mempbrkm(S(haystack1), S("sd\0a")) == haystack1 + 0);
> +
> +	ok1(mempbrk(haystack1, sizeof(haystack1), "bcd\0a") == haystack1 + 1);
> +	ok1(mempbrk(haystack1, sizeof(haystack1), "\0") == NULL);
> +
> +	ok1(memcchr(scan1, 'a', sizeof(scan1)) == scan1 + 4);
> +	ok1(memcchr(scan1, 'b', sizeof(scan1)) == scan1);
> +	ok1(memcchr(scan2, '\0', sizeof(scan2)) == scan2 + 3);
> +	ok1(memcchr(scan2, '\0', sizeof(scan2) - 2) == NULL);
> +
> +	ok1(memeq(haystack1, sizeof(haystack1), haystack1, sizeof(haystack1)));
> +	ok1(!memeq(haystack1, sizeof(haystack1), haystack2, sizeof(haystack2)));
> +
> +	ok1(memeqstr(scan1, sizeof(scan1) - 1, scan1));
> +	ok1(!memeqstr(scan1, sizeof(scan1), scan1));
> +	ok1(!memeqstr(scan1, sizeof(scan1), "aaaa"));
> +
> +	ok1(memstarts(S("a\0bcdef"), S("a\0bc")));
> +	ok1(!memstarts(S("a\0bcdef"), S("a\0bcG")));
> +	ok1(!memstarts(S("a\0bcdef"), S("a\0bcdefg")));
> +
> +	ok1(memstarts_str(scan1, sizeof(scan1), scan1));
> +	ok1(!memstarts_str(scan1, sizeof(scan1), "ab"));
> +
> +	ok1(memends(S("abcdef"), S("abcdef")));
> +	ok1(!memends(S("abcdef"), S("abcdefg")));
> +	ok1(!memends(S("a\0bcdef"), S("a\0b")));
> +	ok1(memends(S("a\0bcdef"), S("ef")));
> +
> +
>  	/* This exits depending on whether all tests passed */
>  	return exit_status();
>  }

-- 
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.ozlabs.org/pipermail/ccan/attachments/20150818/61a3c7ed/attachment.sig>


More information about the ccan mailing list