diff options
Diffstat (limited to '')
-rw-r--r-- | Makefile | 1 | ||||
-rw-r--r-- | libsimple/mem.h | 31 | ||||
-rw-r--r-- | man/libsimple_mempcpy.3 | 4 | ||||
-rw-r--r-- | man/libsimple_mempset.3 | 5 | ||||
-rw-r--r-- | man/libsimple_mempsetelem.3 | 72 | ||||
-rw-r--r-- | man/libsimple_memsetelem.3 | 69 | ||||
-rw-r--r-- | memelem.c | 28 | ||||
-rw-r--r-- | mempsetelem.c | 58 | ||||
-rw-r--r-- | memrelem.c | 26 |
9 files changed, 285 insertions, 9 deletions
@@ -63,6 +63,7 @@ OBJ =\ memelem.o\ memends.o\ memmem.o\ + mempsetelem.o\ memrchr.o\ memrelem.o\ memrmem.o\ diff --git a/libsimple/mem.h b/libsimple/mem.h index 1458700..6782c90 100644 --- a/libsimple/mem.h +++ b/libsimple/mem.h @@ -211,3 +211,34 @@ static inline void *libsimple_mempset(void *__s, int __c, size_t __n) #ifndef mempset # define mempset libsimple_mempset #endif + + +/** + * Fills an array with a number of copies of an item + * + * @param buf The array to fill + * @param item The element to fill `buf` with + * @param size The size of `item` + * @param nitems The number of copies to fill `buf` with + * @return `&buf[nelems * size]` + */ +void *libsimple_mempsetelem(void *__buf, const void *__item, size_t __size, size_t __nitems); +#ifndef libsimple_mempsetelem +# define libsimple_mempsetelem libsimple_mempsetelem +#endif + + +/** + * Fills an array with a number of copies of an item + * + * @param buf The array to fill + * @param item The element to fill `buf` with + * @param size The size of `item` + * @param nitems The number of copies to fill `buf` with + * @return `buf` + */ +static inline void *libsimple_memsetelem(void *__buf, const void *__item, size_t __size, size_t __nitems) +{ return libsimple_mempsetelem(__buf, __item, __size, __nitems), __buf; } +#ifndef libsimple_memsetelem +# define libsimple_memsetelem libsimple_memsetelem +#endif diff --git a/man/libsimple_mempcpy.3 b/man/libsimple_mempcpy.3 index 40b1e63..4d4c514 100644 --- a/man/libsimple_mempcpy.3 +++ b/man/libsimple_mempcpy.3 @@ -1,6 +1,6 @@ .TH LIBSIMPLE_MEMPCPY 3 2018-10-20 libsimple .SH NAME -libsimple_mempcpy \- check two memory segments for equality +libsimple_mempcpy \- copy an array of bytes into another .SH SYNOPSIS .nf #include <libsimple.h> @@ -66,5 +66,5 @@ None. .SH BUGS None. .SH SEE ALSO -.BR libsimple_mempset (3) +.BR libsimple_mempset (3), .BR memcpy (3) diff --git a/man/libsimple_mempset.3 b/man/libsimple_mempset.3 index e4d7fda..33b610c 100644 --- a/man/libsimple_mempset.3 +++ b/man/libsimple_mempset.3 @@ -1,6 +1,6 @@ .TH LIBSIMPLE_MEMPSET 3 2018-10-20 libsimple .SH NAME -libsimple_mempset \- check two memory segments for equality +libsimple_mempset \- fill an array of bytes .SH SYNOPSIS .nf #include <libsimple.h> @@ -68,5 +68,6 @@ None. .SH BUGS None. .SH SEE ALSO -.BR libsimple_mempcpy (3) +.BR libsimple_mempsetelem (3), +.BR libsimple_mempcpy (3), .BR memset (3) diff --git a/man/libsimple_mempsetelem.3 b/man/libsimple_mempsetelem.3 new file mode 100644 index 0000000..ca52a2e --- /dev/null +++ b/man/libsimple_mempsetelem.3 @@ -0,0 +1,72 @@ +.TH LIBSIMPLE_MEMPSETELEM 3 2018-10-20 libsimple +.SH NAME +libsimple_mempsetelem \- fill an array with a value +.SH SYNOPSIS +.nf +#include <libsimple.h> + +void *libsimple_mempsetelem(void *\fIbuf\fP, const void *\fIitem\fP, size_t \fIsize\fP, size_t \fInitems\fP); + +#ifndef mempsetelem +# define mempsetelem libsimple_mempsetelem +#endif +.fi + +Link with +.IR \-lsimple . +.SH DESCRIPTION +The +.BR libsimple_mempsetelem () +function fills the beginning of +.I buf +with +.I nitems +copies of the element +.I item +with the size +.IR size . +.SH RETURN VALUE +The +.BR libsimple_mempsetelem () +function returns the pointer +.I buf +with the offset +.I nitems*size +(the byte where the copy stopped). +.SH ERRORS +The +.BR libsimple_mempsetelem () +function cannot fail. +.SH ATTRIBUTES +For an explanation of the terms used in this section, see +.BR attributes (7). +.TS +allbox; +lb lb lb +l l l. +Interface Attribute Value +T{ +.BR libsimple_mempsetelem () +T} Thread safety MT-Safe +T{ +.BR libsimple_mempsetelem () +T} Async-signal safety AS-Safe +T{ +.BR libsimple_mempsetelem () +T} Async-cancel safety AC-Safe +.TE +.SH EXAMPLES +None. +.SH APPLICATION USAGE +None. +.SH RATIONALE +None. +.SH FUTURE DIRECTIONS +None. +.SH NOTES +None. +.SH BUGS +None. +.SH SEE ALSO +.BR libsimple_memsetelem (3), +.BR libsimple_mempset (3) diff --git a/man/libsimple_memsetelem.3 b/man/libsimple_memsetelem.3 new file mode 100644 index 0000000..700310a --- /dev/null +++ b/man/libsimple_memsetelem.3 @@ -0,0 +1,69 @@ +.TH LIBSIMPLE_MEMSETELEM 3 2018-10-20 libsimple +.SH NAME +libsimple_memsetelem \- fill an array with a value +.SH SYNOPSIS +.nf +#include <libsimple.h> + +static inline void *libsimple_memsetelem(void *\fIbuf\fP, const void *\fIitem\fP, size_t \fIsize\fP, size_t \fInitems\fP); + +#ifndef memsetelem +# define memsetelem libsimple_memsetelem +#endif +.fi + +Link with +.IR \-lsimple . +.SH DESCRIPTION +The +.BR libsimple_memsetelem () +function fills the beginning of +.I buf +with +.I nitems +copies of the element +.I item +with the size +.IR size . +.SH RETURN VALUE +The +.BR libsimple_memsetelem () +function returns the pointer +.IR buf . +.SH ERRORS +The +.BR libsimple_memsetelem () +function cannot fail. +.SH ATTRIBUTES +For an explanation of the terms used in this section, see +.BR attributes (7). +.TS +allbox; +lb lb lb +l l l. +Interface Attribute Value +T{ +.BR libsimple_memsetelem () +T} Thread safety MT-Safe +T{ +.BR libsimple_memsetelem () +T} Async-signal safety AS-Safe +T{ +.BR libsimple_memsetelem () +T} Async-cancel safety AC-Safe +.TE +.SH EXAMPLES +None. +.SH APPLICATION USAGE +None. +.SH RATIONALE +None. +.SH FUTURE DIRECTIONS +None. +.SH NOTES +None. +.SH BUGS +None. +.SH SEE ALSO +.BR libsimple_mempsetelem (3), +.BR memset (3) @@ -11,7 +11,7 @@ libsimple_memelem(const void *hay_, size_t hayn, const void *sub_, size_t subn) switch (subn) { case 1: - return memchr(hay_, hayn, *(char *)sub_); + return memchr(hay_, *(char *)sub_, hayn); case 2: { uint16_t *hay = (void *)hay_; @@ -61,12 +61,34 @@ libsimple_memelem(const void *hay_, size_t hayn, const void *sub_, size_t subn) int main(void) { - assert(!strcmpnul(libsimple_memelem("12345634", 4, "", 0), "12345634")); + assert(!strcmpnul(libsimple_memelem("12345634", 8, "", 0), "12345634")); + assert(!strcmpnul(libsimple_memelem("12345634", 0, "", 0), "12345634")); + + assert(!strcmpnul(libsimple_memelem("12345634", 8, "3", 1), "345634")); + assert(!libsimple_memelem("12345634", 8, "x", 1));; + assert(!strcmpnul(libsimple_memelem("13456342", 8, "3", 1), "3456342")); + assert(!libsimple_memelem("12345634", 0, "3", 1)); + assert(!strcmpnul(libsimple_memelem("12345634", 4, "34", 2), "345634")); assert(!libsimple_memelem("12345634", 4, "xx", 2)); assert(!libsimple_memelem("13456342", 4, "34", 2)); assert(!libsimple_memelem("12345634", 0, "34", 2)); - assert(!strcmpnul(libsimple_memelem("12345634", 0, "34", 0), "12345634")); + + assert(!strcmpnul(libsimple_memelem("abcd1234abcd1234", 4, "1234", 4), "1234abcd1234")); + assert(!libsimple_memelem("abcd1234abcd1234", 4, "zzzz", 4)); + assert(!libsimple_memelem("cd1234abcd1234ab", 4, "1234", 4)); + assert(!libsimple_memelem("abcd1234abcd1234", 0, "1234", 4)); + + assert(!strcmpnul(libsimple_memelem("abcdefgh12345678abcdefgh12345678", 4, "12345678", 8), "12345678abcdefgh12345678")); + assert(!libsimple_memelem("abcdefgh12345678abcdefgh12345678", 4, "zzzzzzzz", 8)); + assert(!libsimple_memelem("efgh12345678abcdefgh12345678abcd", 4, "12345678", 8)); + assert(!libsimple_memelem("abcdefgh12345678abcdefgh12345678", 0, "12345678", 8)); + + assert(!strcmpnul(libsimple_memelem("abc123abc123", 4, "123", 3), "123abc123")); + assert(!libsimple_memelem("abc123abc123", 4, "zzz", 3)); + assert(!libsimple_memelem("bc123abc123a", 4, "123", 3)); + assert(!libsimple_memelem("abc123abc123", 0, "123", 3)); + return 0; } diff --git a/mempsetelem.c b/mempsetelem.c new file mode 100644 index 0000000..d431604 --- /dev/null +++ b/mempsetelem.c @@ -0,0 +1,58 @@ +/* See LICENSE file for copyright and license details. */ +#include "libsimple.h" +#ifndef TEST + + +void * +libsimple_mempsetelem(void *buf_, const void *item, size_t size, size_t nitems) +{ + switch (size) { + case 0: + return buf_; + case 1: + return &((char *)memset(buf_, *(char *)item, nitems))[nitems]; + case 2: + { + uint16_t *buf = buf_, e = *(uint16_t *)item; + uint16_t *end = &buf[nitems]; + for (; buf != end; buf++) + *buf = e; + return buf; + } + case 4: + { + uint32_t *buf = buf_, e = *(uint32_t *)item; + uint32_t *end = &buf[nitems]; + for (; buf != end; buf++) + *buf = e; + return buf; + } + case 8: + { + uint64_t *buf = buf_, e = *(uint64_t *)item; + uint64_t *end = &buf[nitems]; + for (; buf != end; buf++) + *buf = e; + return buf; + } + default: + { + char *buf = buf_; + for (; nitems--; buf += size) + memcpy(buf, item, size); + return buf; + } + } +} + + +#else +#include "test.h" + +int +main(void) +{ + return 0; +} + +#endif @@ -70,12 +70,34 @@ libsimple_memrelem(const void *hay_, size_t hayn, const void *sub_, size_t subn) int main(void) { - assert(!strcmpnul(libsimple_memrelem("12345634", 4, "", 0), "12345634")); + assert(!strcmpnul(libsimple_memrelem("12345634", 8, "", 0), "12345634")); + assert(!strcmpnul(libsimple_memrelem("12345634", 0, "", 0), "12345634")); + + assert(!strcmpnul(libsimple_memrelem("12345634", 8, "3", 1), "34")); + assert(!libsimple_memrelem("12345634", 8, "x", 1));; + assert(!strcmpnul(libsimple_memrelem("13456342", 8, "3", 1), "342")); + assert(!libsimple_memrelem("12345634", 0, "3", 1)); + assert(!strcmpnul(libsimple_memrelem("12345634", 4, "34", 2), "34")); assert(!libsimple_memrelem("12345634", 4, "xx", 2)); assert(!libsimple_memrelem("13456342", 4, "34", 2)); assert(!libsimple_memrelem("12345634", 0, "34", 2)); - assert(!strcmpnul(libsimple_memrelem("12345634", 0, "34", 0), "12345634")); + + assert(!strcmpnul(libsimple_memrelem("abcd1234abcd1234", 4, "1234", 4), "1234")); + assert(!libsimple_memrelem("abcd1234abcd1234", 4, "zzzz", 4)); + assert(!libsimple_memrelem("cd1234abcd1234ab", 4, "1234", 4)); + assert(!libsimple_memrelem("abcd1234abcd1234", 0, "1234", 4)); + + assert(!strcmpnul(libsimple_memrelem("abcdefgh12345678abcdefgh12345678", 4, "12345678", 8), "12345678")); + assert(!libsimple_memrelem("abcdefgh12345678abcdefgh12345678", 4, "zzzzzzzz", 8)); + assert(!libsimple_memrelem("efgh12345678abcdefgh12345678abcd", 4, "12345678", 8)); + assert(!libsimple_memrelem("abcdefgh12345678abcdefgh12345678", 0, "12345678", 8)); + + assert(!strcmpnul(libsimple_memrelem("abc123abc123", 4, "123", 3), "123")); + assert(!libsimple_memrelem("abc123abc123", 4, "zzz", 3)); + assert(!libsimple_memrelem("bc123abc123a", 4, "123", 3)); + assert(!libsimple_memrelem("abc123abc123", 0, "123", 3)); + return 0; } |