From 168b7a317cce20048b319c7e835fc8588a44d5c8 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sat, 17 Nov 2018 10:57:02 +0100 Subject: Add memscan and memcasescan MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- Makefile | 2 + libsimple/mem.h | 46 +++++++++++++++++++--- man0/libsimple.h.0 | 14 +++++++ man3/libsimple_memcasechr.3 | 1 + man3/libsimple_memcasescan.3 | 86 +++++++++++++++++++++++++++++++++++++++++ man3/libsimple_memscan.3 | 84 ++++++++++++++++++++++++++++++++++++++++ man3/libsimple_rawmemcasechr.3 | 1 + man3/libsimple_rawmemchr.3 | 1 + man3/libsimple_strcasechrnul.3 | 1 + man3/libsimple_strchrnul.3 | 1 + man3/libsimple_strncasechrnul.3 | 3 +- man3/libsimple_strnchrnul.3 | 3 +- man3/memcasescan.3libsimple | 1 + man3/memscan.3libsimple | 1 + memcasescan.c | 35 +++++++++++++++++ memscan.c | 34 ++++++++++++++++ 16 files changed, 307 insertions(+), 7 deletions(-) create mode 100644 man3/libsimple_memcasescan.3 create mode 100644 man3/libsimple_memscan.3 create mode 120000 man3/memcasescan.3libsimple create mode 120000 man3/memscan.3libsimple create mode 100644 memcasescan.c create mode 100644 memscan.c diff --git a/Makefile b/Makefile index f35fc4e..3428753 100644 --- a/Makefile +++ b/Makefile @@ -63,6 +63,7 @@ OBJ =\ memcaseends.o\ memcaseeqlen.o\ memcasemem.o\ + memcasescan.o\ memcasestarts.o\ memdup.o\ memelem.o\ @@ -78,6 +79,7 @@ OBJ =\ memrelem.o\ memreqlen.o\ memrmem.o\ + memscan.o\ memstarts.o\ minimise_number_string.o\ multimespec.o\ diff --git a/libsimple/mem.h b/libsimple/mem.h index fea97f6..dacc78c 100644 --- a/libsimple/mem.h +++ b/libsimple/mem.h @@ -1,6 +1,24 @@ /* See LICENSE file for copyright and license details. */ +/** + * Finds the first occurence of a byte value in an array of bytes, + * the comparison is case-sensitive + * + * @param s The array of bytes to search + * @param c The byte value to search for + * @param n The number of bytes in the byte array + * @return `s` with a minimal offset such that `*r == c`, + * where `r` is the returned pointer, `&s[n]` if + * no such offset exists within [s, &s[n]) + */ +_LIBSIMPLE_GCC_ONLY(__attribute__((__pure__, __returns_nonnull__, __warn_unused_result__))) +void *libsimple_memscan(const void *, int, size_t); +#ifndef memscan +# define memscan libsimple_memscan +#endif + + /** * Finds the first occurence of a byte value in an array of bytes, * the comparison is case-insensitive @@ -9,16 +27,34 @@ * @param c The byte value to search for * @param n The number of bytes in the byte array * @return `s` with a minimal offset such that `tolower(*r) == tolower(c)`, - * where `r` is the returned pointer `NULL` if no - * such offset exists within [s, &s[n]) + * where `r` is the returned pointer, `NULL` if + * no such offset exists within [s, &s[n]) */ -_LIBSIMPLE_GCC_ONLY(__attribute__((__pure__, __nonnull__, __warn_unused_result__))) +_LIBSIMPLE_GCC_ONLY(__attribute__((__pure__, __warn_unused_result__))) void *libsimple_memcasechr(const void *, int, size_t); #ifndef memcasechr # define memcasechr libsimple_memcasechr #endif +/** + * Finds the first occurence of a byte value in an array of bytes, + * the comparison is case-insensitive + * + * @param s The array of bytes to search + * @param c The byte value to search for + * @param n The number of bytes in the byte array + * @return `s` with a minimal offset such that `tolower(*r) == tolower(c)`, + * where `r` is the returned pointer, `&s[n]` if + * no such offset exists within [s, &s[n]) + */ +_LIBSIMPLE_GCC_ONLY(__attribute__((__pure__, __nonnull__, __returns_nonnull__, __warn_unused_result__))) +void *libsimple_memcasescan(const void *, int, size_t); +#ifndef memcasescan +# define memcasescan libsimple_memcasescan +#endif + + /** * Finds the first occurence of a byte value in an array of bytes, * the comparison is case-sensitive @@ -72,7 +108,7 @@ void *libsimple_rawmemcasechr(const void *, int); * where `r` is the returned pointer `NULL` if no * such offset exists within [s, &s[n]) */ -_LIBSIMPLE_GCC_ONLY(__attribute__((__pure__, __nonnull__, __warn_unused_result__))) +_LIBSIMPLE_GCC_ONLY(__attribute__((__pure__, __warn_unused_result__))) void *libsimple_memrchr(const void *, int, size_t); #ifndef memrchr # define memrchr libsimple_memrchr @@ -90,7 +126,7 @@ void *libsimple_memrchr(const void *, int, size_t); * where `r` is the returned pointer `NULL` if no * such offset exists within [s, &s[n]) */ -_LIBSIMPLE_GCC_ONLY(__attribute__((__pure__, __nonnull__, __warn_unused_result__))) +_LIBSIMPLE_GCC_ONLY(__attribute__((__pure__, __warn_unused_result__))) void *libsimple_memrcasechr(const void *, int, size_t); #ifndef memrcasechr # define memrcasechr libsimple_memrcasechr diff --git a/man0/libsimple.h.0 b/man0/libsimple.h.0 index a1e1cd4..7cb93dd 100644 --- a/man0/libsimple.h.0 +++ b/man0/libsimple.h.0 @@ -637,6 +637,20 @@ Find the last occurrence of string of byte in an array of bytes. .RE +.TP +.BR libsimple_memscan (3), +.RS 0 +.BR libsimple_memcasescan (3) +.RE +.RS +Versions of +.BR memchr (3) +and +.BR libsimple_memcasechr (3) +that return the end of the array +if the byte is not found. +.RE + .TP .BR libsimple_memsetelem (3) Fill en array. diff --git a/man3/libsimple_memcasechr.3 b/man3/libsimple_memcasechr.3 index 3934598..5c6e5d9 100644 --- a/man3/libsimple_memcasechr.3 +++ b/man3/libsimple_memcasechr.3 @@ -78,6 +78,7 @@ None. .BR libsimple_memrcasechr (3), .BR libsimple_rawmemcasechr (3), .BR libsimple_memcasemem (3), +.BR libsimple_memcasescan (3), .BR libsimple_strncasechr (3), .BR libsimple_strcasechr (3), .BR memchr (3) diff --git a/man3/libsimple_memcasescan.3 b/man3/libsimple_memcasescan.3 new file mode 100644 index 0000000..9c11c2e --- /dev/null +++ b/man3/libsimple_memcasescan.3 @@ -0,0 +1,86 @@ +.TH LIBSIMPLE_MEMCASESCAN 3 2018-11-17 libsimple +.SH NAME +libsimple_memcasescan \- find byte in memory +.SH SYNOPSIS +.nf +#include + +void *libsimple_memcasescan(const void *\fIs\fP, int \fIc\fP, size_t \fIn\fP); + +#ifndef memcasescan +# define memcasescan libsimple_memcasescan +#endif +.fi +.PP +Link with +.IR \-lsimple . +.SH DESCRIPTION +The +.BR libsimple_memcasescan () +function scans the memory segment +.IR s , +with the size +.IR n , +for the first occurence of the byte +.I c +(it is converted to a +.BR char ). +If no such character exist in the memory +segment, the memory segment's end is returned. +.PP +The comparison is case-insensitive. +.SH RETURN VALUE +The +.BR libsimple_memcasescan () +function returns the pointer +.I s +with a minimal offset such that +.IR tolower(*r)==tolower(c) , +where +.I r +is the returned pointer. +If no such offset exists, +.I &s[n] +is returned. +.SH ERRORS +The +.BR libsimple_memcasescan () +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_memcasescan () +T} Thread safety MT-Safe +T{ +.BR libsimple_memcasescan () +T} Async-signal safety AS-Safe +T{ +.BR libsimple_memcasescan () +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_memscan (3), +.BR libsimple_memcasechr (3), +.BR libsimple_memrcasechr (3), +.BR libsimple_rawmemcasechr (3), +.BR libsimple_memcasemem (3), +.BR libsimple_strncasechrnul (3), +.BR libsimple_strcasechrnul (3) diff --git a/man3/libsimple_memscan.3 b/man3/libsimple_memscan.3 new file mode 100644 index 0000000..c9db7da --- /dev/null +++ b/man3/libsimple_memscan.3 @@ -0,0 +1,84 @@ +.TH LIBSIMPLE_MEMSCAN 3 2018-11-17 libsimple +.SH NAME +libsimple_memscan \- find byte in memory +.SH SYNOPSIS +.nf +#include + +void *libsimple_memscan(const void *\fIs\fP, int \fIc\fP, size_t \fIn\fP); + +#ifndef memscan +# define memscan libsimple_memscan +#endif +.fi +.PP +Link with +.IR \-lsimple . +.SH DESCRIPTION +The +.BR libsimple_memscan () +function scans the memory segment +.IR s , +with the size +.IR n , +for the first occurence of the byte +.I c +(it is converted to a +.BR char ). +.PP +The comparison is case-sensitive. +.SH RETURN VALUE +The +.BR libsimple_memscan () +function returns the pointer +.I s +with a minimal offset such that +.IR *r==c , +where +.I r +is the returned pointer. +If no such offset exists, +.B NULL +is returned. +.SH ERRORS +The +.BR libsimple_memscan () +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_memscan () +T} Thread safety MT-Safe +T{ +.BR libsimple_memscan () +T} Async-signal safety AS-Safe +T{ +.BR libsimple_memscan () +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_memcasescan (3), +.BR libsimple_memrchr (3), +.BR libsimple_rawmemchr (3), +.BR libsimple_memmem (3), +.BR libsimple_strnchrnul (3), +.BR libsimple_strchrnul (3), +.BR memchr (3) diff --git a/man3/libsimple_rawmemcasechr.3 b/man3/libsimple_rawmemcasechr.3 index 2e06898..308ea91 100644 --- a/man3/libsimple_rawmemcasechr.3 +++ b/man3/libsimple_rawmemcasechr.3 @@ -79,4 +79,5 @@ None. .BR libsimple_memcasechr (3), .BR libsimple_rawmemchr (3), .BR libsimple_rawmemrcasechr (3), +.BR libsimple_memcasescan (3), .BR libsimple_memcasechr (3) diff --git a/man3/libsimple_rawmemchr.3 b/man3/libsimple_rawmemchr.3 index 79b480e..3cf13dd 100644 --- a/man3/libsimple_rawmemchr.3 +++ b/man3/libsimple_rawmemchr.3 @@ -78,4 +78,5 @@ None. .SH SEE ALSO .BR libsimple_rawmemcasechr (3), .BR libsimple_rawmemrchr (3), +.BR libsimple_memscan (3), .BR memchr (3) diff --git a/man3/libsimple_strcasechrnul.3 b/man3/libsimple_strcasechrnul.3 index d2098bb..4f4badc 100644 --- a/man3/libsimple_strcasechrnul.3 +++ b/man3/libsimple_strcasechrnul.3 @@ -75,4 +75,5 @@ None. .BR libsimple_strcasechr (3), .BR libsimple_strchrnul (3), .BR libsimple_strncasechrnul (3), +.BR libsimple_memcasescan (3), .BR libsimple_inchrcaseset (3) diff --git a/man3/libsimple_strchrnul.3 b/man3/libsimple_strchrnul.3 index 2196933..846895b 100644 --- a/man3/libsimple_strchrnul.3 +++ b/man3/libsimple_strchrnul.3 @@ -75,5 +75,6 @@ None. .BR libsimple_strcasechrnul (3), .BR libsimple_strend (3), .BR libsimple_strnchrnul (3), +.BR libsimple_memscan (3), .BR libsimple_inchrset (3), .BR strchr (3) diff --git a/man3/libsimple_strncasechrnul.3 b/man3/libsimple_strncasechrnul.3 index 241b61a..ed0fa03 100644 --- a/man3/libsimple_strncasechrnul.3 +++ b/man3/libsimple_strncasechrnul.3 @@ -82,4 +82,5 @@ None. .SH SEE ALSO .BR libsimple_strncasechr (3), .BR libsimple_strnchrnul (3), -.BR libsimple_strcasechrnul (3) +.BR libsimple_strcasechrnul (3), +.BR libsimple_memcasescan (3) diff --git a/man3/libsimple_strnchrnul.3 b/man3/libsimple_strnchrnul.3 index ae0b994..6a26bd2 100644 --- a/man3/libsimple_strnchrnul.3 +++ b/man3/libsimple_strnchrnul.3 @@ -83,4 +83,5 @@ None. I promise! .BR libsimple_strncasechrnul (3), .BR libsimple_strnchr (3), .BR libsimple_strnend (3), -.BR libsimple_strchrnul (3) +.BR libsimple_strchrnul (3), +.BR libsimple_memscan (3) diff --git a/man3/memcasescan.3libsimple b/man3/memcasescan.3libsimple new file mode 120000 index 0000000..86e265e --- /dev/null +++ b/man3/memcasescan.3libsimple @@ -0,0 +1 @@ +libsimple_memcasescan.3 \ No newline at end of file diff --git a/man3/memscan.3libsimple b/man3/memscan.3libsimple new file mode 120000 index 0000000..41ccaab --- /dev/null +++ b/man3/memscan.3libsimple @@ -0,0 +1 @@ +libsimple_memscan.3 \ No newline at end of file diff --git a/memcasescan.c b/memcasescan.c new file mode 100644 index 0000000..a5b22b3 --- /dev/null +++ b/memcasescan.c @@ -0,0 +1,35 @@ +/* See LICENSE file for copyright and license details. */ +#include "libsimple.h" +#ifndef TEST + + +void * +libsimple_memcasescan(const void *s_, int c, size_t n) +{ + char *s = *(char **)(void *)&s_; + size_t i = 0; + c = tolower(c); + for (; i < n && tolower(s[i]) != c; i++); + return &s[i]; +} + + +#else +#include "test.h" + +int +main(void) +{ + assert(!strcmpnul(libsimple_memcasescan("abcABCabcABC", 'a', sizeof("abcABCabcABC") - 1), "abcABCabcABC")); + assert(!strcmpnul(libsimple_memcasescan("abcABCabcABC", 'c', sizeof("abcABCabcABC") - 1), "cABCabcABC")); + assert(!strcmpnul(libsimple_memcasescan("abcABCabcABC", 'A', sizeof("abcABCabcABC") - 1), "abcABCabcABC")); + assert(!strcmpnul(libsimple_memcasescan("abcABCabcABC", 'C', sizeof("abcABCabcABC") - 1), "cABCabcABC")); + assert(!strcmpnul(libsimple_memcasescan("ABCabcABCabc", 'a', sizeof("ABCabcABCabc") - 1), "ABCabcABCabc")); + assert(!strcmpnul(libsimple_memcasescan("ABCabcABCabc", 'c', sizeof("ABCabcABCabc") - 1), "CabcABCabc")); + assert(!strcmpnul(libsimple_memcasescan("ABCabcABCabc", 'A', sizeof("ABCabcABCabc") - 1), "ABCabcABCabc")); + assert(!strcmpnul(libsimple_memcasescan("ABCabcABCabc", 'C', sizeof("ABCabcABCabc") - 1), "CabcABCabc")); + assert(!strcmpnul(libsimple_memcasescan("abcABCabcABC", 'x', sizeof("abcABCabcABC") - 1), "")); + return 0; +} + +#endif diff --git a/memscan.c b/memscan.c new file mode 100644 index 0000000..3fb2a2c --- /dev/null +++ b/memscan.c @@ -0,0 +1,34 @@ +/* See LICENSE file for copyright and license details. */ +#include "libsimple.h" +#ifndef TEST + + +void * +libsimple_memscan(const void *s_, int c_, size_t n) +{ + char *s = *(char **)(void *)&s_, c = (char)c_; + size_t i = 0; + for (; i < n && s[i] != c; i++); + return &s[i]; +} + + +#else +#include "test.h" + +int +main(void) +{ + assert(!strcmpnul(libsimple_memscan("abcABCabcABC", 'a', sizeof("abcABCabcABC") - 1), "abcABCabcABC")); + assert(!strcmpnul(libsimple_memscan("abcABCabcABC", 'c', sizeof("abcABCabcABC") - 1), "cABCabcABC")); + assert(!strcmpnul(libsimple_memscan("abcABCabcABC", 'A', sizeof("abcABCabcABC") - 1), "ABCabcABC")); + assert(!strcmpnul(libsimple_memscan("abcABCabcABC", 'C', sizeof("abcABCabcABC") - 1), "CabcABC")); + assert(!strcmpnul(libsimple_memscan("ABCabcABCabc", 'a', sizeof("ABCabcABCabc") - 1), "abcABCabc")); + assert(!strcmpnul(libsimple_memscan("ABCabcABCabc", 'c', sizeof("ABCabcABCabc") - 1), "cABCabc")); + assert(!strcmpnul(libsimple_memscan("ABCabcABCabc", 'A', sizeof("ABCabcABCabc") - 1), "ABCabcABCabc")); + assert(!strcmpnul(libsimple_memscan("ABCabcABCabc", 'C', sizeof("ABCabcABCabc") - 1), "CabcABCabc")); + assert(!strcmpnul(libsimple_memscan("abcABCabcABC", 'x', sizeof("abcABCabcABC") - 1), "")); + return 0; +} + +#endif -- cgit v1.2.3-70-g09d2