From b5ca6729c7b52e8cade6d3808ad8280845ac32ea Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Fri, 23 Nov 2018 20:38:54 +0100 Subject: Some fixes and tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- libsimple.c | 390 +++++++++++++++++++++++++++++++++++++++++++++++++++ libsimple/mem.h | 10 +- libsimple/str.h | 17 +-- libsimple/strn.h | 30 ++-- memcmove.c | 53 ++++++- memrcasechr_inv.c | 9 +- memrchr_inv.c | 9 +- rawmemrcasechr_inv.c | 8 +- rawmemrchr_inv.c | 8 +- strcmove.c | 78 ++++++++++- strncmove.c | 304 ++++++++++++++++++++++++++++++++++++++- strrcasechr_inv.c | 9 +- strrchr_inv.c | 9 +- strrncasechr_inv.c | 55 +++++++- strrnchr_inv.c | 55 +++++++- vmemalloc.c | 5 + 16 files changed, 996 insertions(+), 53 deletions(-) diff --git a/libsimple.c b/libsimple.c index a5015c3..4ef6ec2 100644 --- a/libsimple.c +++ b/libsimple.c @@ -119,7 +119,12 @@ main(void) size_t pagesize, cacheline; pagesize = (size_t)sysconf(_SC_PAGESIZE); + +#ifdef _SC_LEVEL1_DCACHE_LINESIZE cacheline = (size_t)sysconf(_SC_LEVEL1_DCACHE_LINESIZE); +#else + cacheline = 64; +#endif assert(libsimple_default_failure_exit == 1); @@ -2192,6 +2197,391 @@ main(void) assert(p[i] == 0); } + + stpcpy(mempcpy(buf, "hello world", 12), "goodbye world"); + assert(libsimple_strset(buf, 'x') == buf); + assert(!strcmp(buf, "xxxxxxxxxxx")); + assert(!strcmp(&buf[12], "goodbye world")); + + stpcpy(mempcpy(buf, "hello world", 12), "goodbye world"); + assert(libsimple_strset(buf, '\0') == buf); + assert(!memcmp(buf, "\0\0\0\0\0\0\0\0\0\0\0\0goodbye world", 26)); + + + stpcpy(mempcpy(buf, "hello world", 12), "goodbye world"); + assert(libsimple_stpset(buf, 'x') == &buf[11]); + assert(!strcmp(buf, "xxxxxxxxxxx")); + assert(!strcmp(&buf[12], "goodbye world")); + + stpcpy(mempcpy(buf, "hello world", 12), "goodbye world"); + assert(libsimple_stpset(buf, '\0') == &buf[11]); + assert(!memcmp(buf, "\0\0\0\0\0\0\0\0\0\0\0\0goodbye world", 26)); + + + stpcpy(mempcpy(buf, "hello world", 12), "goodbye world"); + assert(libsimple_strnset(buf, 'x', SIZE_MAX) == buf); + assert(!strcmp(buf, "xxxxxxxxxxx")); + assert(!strcmp(&buf[12], "goodbye world")); + + stpcpy(mempcpy(buf, "hello world", 12), "goodbye world"); + assert(libsimple_strnset(buf, '\0', SIZE_MAX) == buf); + assert(!memcmp(buf, "\0\0\0\0\0\0\0\0\0\0\0\0goodbye world", 26)); + + + stpcpy(mempcpy(buf, "hello world", 12), "goodbye world"); + assert(libsimple_stpnset(buf, 'x', SIZE_MAX) == &buf[11]); + assert(!strcmp(buf, "xxxxxxxxxxx")); + assert(!strcmp(&buf[12], "goodbye world")); + + stpcpy(mempcpy(buf, "hello world", 12), "goodbye world"); + assert(libsimple_stpnset(buf, '\0', SIZE_MAX) == &buf[11]); + assert(!memcmp(buf, "\0\0\0\0\0\0\0\0\0\0\0\0goodbye world", 26)); + + + stpcpy(mempcpy(buf, "hello world", 12), "goodbye world"); + assert(libsimple_strnset(buf, 'x', 5) == buf); + assert(!strcmp(buf, "xxxxx world")); + assert(!strcmp(&buf[12], "goodbye world")); + + stpcpy(mempcpy(buf, "hello world", 12), "goodbye world"); + assert(libsimple_strnset(buf, '\0', 5) == buf); + assert(!memcmp(buf, "\0\0\0\0\0 world\0goodbye world", 26)); + + + stpcpy(mempcpy(buf, "hello world", 12), "goodbye world"); + assert(libsimple_stpnset(buf, 'x', 5) == &buf[5]); + assert(!strcmp(buf, "xxxxx world")); + assert(!strcmp(&buf[12], "goodbye world")); + + stpcpy(mempcpy(buf, "hello world", 12), "goodbye world"); + assert(libsimple_stpnset(buf, '\0', 5) == &buf[5]); + assert(!memcmp(buf, "\0\0\0\0\0 world\0goodbye world", 26)); + + + stpcpy(mempcpy(buf, "hello world", 12), "goodbye world"); + assert(libsimple_memreplace(buf, 'o', 'x', 46) == &buf[46]); + assert(!memcmp(buf, "hellx wxrld\0gxxdbye wxrld", 26)); + + stpcpy(mempcpy(buf, "hello world", 12), "goodbye world"); + assert(libsimple_memreplace(buf, 'o', 'x', 12) == &buf[12]); + assert(!memcmp(buf, "hellx wxrld\0goodbye world", 26)); + + + stpcpy(mempcpy(buf, "hello world", 12), "goodbye world"); + assert(libsimple_strreplace(buf, 'o', 'x') == &buf[11]); + assert(!memcmp(buf, "hellx wxrld\0goodbye world", 26)); + + + stpcpy(mempcpy(buf, "hello world", 12), "goodbye world"); + assert(libsimple_strnreplace(buf, 'o', 'x', SIZE_MAX) == &buf[11]); + assert(!memcmp(buf, "hellx wxrld\0goodbye world", 26)); + + stpcpy(mempcpy(buf, "hello world", 12), "goodbye world"); + assert(libsimple_strnreplace(buf, 'o', 'x', 12) == &buf[11]); + assert(!memcmp(buf, "hellx wxrld\0goodbye world", 26)); + + stpcpy(mempcpy(buf, "hello world", 12), "goodbye world"); + assert(libsimple_strnreplace(buf, 'o', 'x', 11) == &buf[11]); + assert(!memcmp(buf, "hellx wxrld\0goodbye world", 26)); + + stpcpy(mempcpy(buf, "hello world", 12), "goodbye world"); + assert(libsimple_strnreplace(buf, 'o', 'x', 6) == &buf[6]); + assert(!memcmp(buf, "hellx world\0goodbye world", 26)); + + stpcpy(mempcpy(buf, "hello world", 12), "goodbye world"); + assert(libsimple_strnreplace(buf, 'o', 'x', 5) == &buf[5]); + assert(!memcmp(buf, "hellx world\0goodbye world", 26)); + + stpcpy(mempcpy(buf, "hello world", 12), "goodbye world"); + assert(libsimple_strnreplace(buf, 'o', 'x', 4) == &buf[4]); + assert(!memcmp(buf, "hello world\0goodbye world", 26)); + + + memset(buf, 'x', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(buf, "hello world"); + assert(libsimple_strmove(&buf[3], buf) == &buf[3]); + assert(!strcmp(buf, "helhello world")); + + memset(buf, 'x', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(buf, "hello world"); + assert(libsimple_strmove(buf, &buf[3]) == buf); + assert(!strcmp(buf, "lo world")); + + memset(buf, 'x', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(buf, "hello world"); + assert(libsimple_strmove(buf, buf) == buf); + assert(!strcmp(buf, "hello world")); + + + memset(buf, 'x', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(buf, "hello world"); + assert(libsimple_strnmove(&buf[3], buf, SIZE_MAX) == &buf[3]); + assert(!strcmp(buf, "helhello world")); + + memset(buf, 'x', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(buf, "hello world"); + assert(libsimple_strnmove(buf, &buf[3], SIZE_MAX) == buf); + assert(!strcmp(buf, "lo world")); + + memset(buf, 'x', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(buf, "hello world"); + assert(libsimple_strnmove(buf, buf, SIZE_MAX) == buf); + assert(!strcmp(buf, "hello world")); + + memset(buf, 'x', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(buf, "hello world"); + assert(libsimple_strnmove(&buf[3], buf, 12) == &buf[3]); + assert(!strcmp(buf, "helhello world")); + + memset(buf, 'x', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(buf, "hello world"); + assert(libsimple_strnmove(buf, &buf[3], 9) == buf); + assert(!strcmp(buf, "lo world")); + + memset(buf, 'x', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(buf, "hello world"); + assert(libsimple_strnmove(buf, buf, 12) == buf); + assert(!strcmp(buf, "hello world")); + + memset(buf, 'x', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(buf, "hello world"); + assert(libsimple_strnmove(&buf[3], buf, 11) == &buf[3]); + assert(!strncmp(buf, "helhello worldx", 15)); + + memset(buf, 'x', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(buf, "hello world"); + assert(libsimple_strnmove(buf, &buf[3], 8) == buf); + assert(!strcmp(buf, "lo worldrld")); + + memset(buf, 'x', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(buf, "hello world"); + assert(libsimple_strnmove(buf, buf, 11) == buf); + assert(!strcmp(buf, "hello world")); + + memset(buf, 'x', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(buf, "hello world"); + assert(libsimple_strnmove(&buf[3], buf, 2) == &buf[3]); + assert(!strcmp(buf, "helhe world")); + + memset(buf, 'x', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(buf, "hello world"); + assert(libsimple_strnmove(buf, &buf[3], 2) == buf); + assert(!strcmp(buf, "lollo world")); + + memset(buf, 'x', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(buf, "hello world"); + assert(libsimple_strnmove(buf, buf, 2) == buf); + assert(!strcmp(buf, "hello world")); + + + memset(buf, 'x', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(buf, "hello world"); + assert(libsimple_stpmove(&buf[3], buf) == &buf[11 + 3]); + assert(!strcmp(buf, "helhello world")); + + memset(buf, 'x', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(buf, "hello world"); + assert(libsimple_stpmove(buf, &buf[3]) == &buf[11 - 3]); + assert(!strcmp(buf, "lo world")); + + memset(buf, 'x', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(buf, "hello world"); + assert(libsimple_stpmove(buf, buf) == &buf[11]); + assert(!strcmp(buf, "hello world")); + + + memset(buf, 'x', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(buf, "hello world"); + assert(libsimple_stpnmove(&buf[3], buf, SIZE_MAX) == &buf[11 + 3]); + assert(!strcmp(buf, "helhello world")); + + memset(buf, 'x', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(buf, "hello world"); + assert(libsimple_stpnmove(buf, &buf[3], SIZE_MAX) == &buf[11 - 3]); + assert(!strcmp(buf, "lo world")); + + memset(buf, 'x', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(buf, "hello world"); + assert(libsimple_stpnmove(buf, buf, SIZE_MAX) == &buf[11]); + assert(!strcmp(buf, "hello world")); + + memset(buf, 'x', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(buf, "hello world"); + assert(libsimple_stpnmove(&buf[3], buf, 12) == &buf[11 + 3]); + assert(!strcmp(buf, "helhello world")); + + memset(buf, 'x', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(buf, "hello world"); + assert(libsimple_stpnmove(buf, &buf[3], 9) == &buf[11 - 3]); + assert(!strcmp(buf, "lo world")); + + memset(buf, 'x', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(buf, "hello world"); + assert(libsimple_stpnmove(buf, buf, 12) == &buf[11]); + assert(!strcmp(buf, "hello world")); + + memset(buf, 'x', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(buf, "hello world"); + assert(libsimple_stpnmove(&buf[3], buf, 11) == &buf[11 + 3]); + assert(!strncmp(buf, "helhello worldx", 15)); + + memset(buf, 'x', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(buf, "hello world"); + assert(libsimple_stpnmove(buf, &buf[3], 8) == &buf[8]); + assert(!strcmp(buf, "lo worldrld")); + + memset(buf, 'x', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(buf, "hello world"); + assert(libsimple_stpnmove(buf, buf, 11) == &buf[11]); + assert(!strcmp(buf, "hello world")); + + memset(buf, 'x', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(buf, "hello world"); + assert(libsimple_stpnmove(&buf[3], buf, 2) == &buf[3 + 2]); + assert(!strcmp(buf, "helhe world")); + + memset(buf, 'x', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(buf, "hello world"); + assert(libsimple_stpnmove(buf, &buf[3], 2) == &buf[2]); + assert(!strcmp(buf, "lollo world")); + + memset(buf, 'x', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(buf, "hello world"); + assert(libsimple_stpnmove(buf, buf, 2) == &buf[2]); + assert(!strcmp(buf, "hello world")); + + + memset(buf, 'x', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + assert(libsimple_strccpy(buf, "hello", '\0') == &buf[6]); + assert(!strcmp(buf, "hello")); + + memset(buf, 'x', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + assert(libsimple_strccpy(buf, "hello", 'o') == &buf[5]); + assert(!strcmp(buf, "hello")); + + memset(buf, 'x', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + assert(libsimple_strccpy(buf, "hello", 'l') == &buf[3]); + assert(!strcmp(buf, "hel")); + + memset(buf, 'x', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + assert(libsimple_strccpy(buf, "hello", 'x') == NULL); + assert(!strcmp(buf, "hello")); + + + memset(buf, 'x', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + assert(libsimple_strnccpy(buf, "hello", '\0', 1024) == &buf[6]); + assert(!strcmp(buf, "hello")); + + memset(buf, 'x', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + assert(libsimple_strnccpy(buf, "hello", 'o', 1024) == &buf[5]); + assert(!strcmp(buf, "hello")); + + memset(buf, 'x', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + assert(libsimple_strnccpy(buf, "hello", 'l', 1024) == &buf[3]); + assert(!strcmp(buf, "hel")); + + memset(buf, 'x', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + assert(libsimple_strnccpy(buf, "hello", 'x', 1024) == NULL); + assert(!strcmp(buf, "hello")); + + + memset(buf, 'x', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + assert(libsimple_strnccpy(buf, "hello", '\0', 6) == &buf[6]); + assert(!strcmp(buf, "hello")); + + memset(buf, 'x', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + assert(libsimple_strnccpy(buf, "hello", 'o', 6) == &buf[5]); + assert(!strcmp(buf, "hello")); + + memset(buf, 'x', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + assert(libsimple_strnccpy(buf, "hello", 'l', 6) == &buf[3]); + assert(!strcmp(buf, "hel")); + + memset(buf, 'x', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + assert(libsimple_strnccpy(buf, "hello", 'x', 6) == NULL); + assert(!strcmp(buf, "hello")); + + + memset(buf, 'x', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + assert(libsimple_strnccpy(buf, "hello", '\0', 5) == NULL); + assert(!strncmp(buf, "hellox", 6)); + + memset(buf, 'x', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + assert(libsimple_strnccpy(buf, "hello", 'o', 5) == &buf[5]); + assert(!strncmp(buf, "hellox", 6)); + + memset(buf, 'x', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + assert(libsimple_strnccpy(buf, "hello", 'l', 5) == &buf[3]); + assert(!strcmp(buf, "hel")); + + memset(buf, 'x', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + assert(libsimple_strnccpy(buf, "hello", 'x', 5) == NULL); + assert(!strncmp(buf, "hellox", 6)); + + + memset(buf, 'x', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + assert(libsimple_strnccpy(buf, "hello", 'o', 3) == NULL); + assert(!strncmp(buf, "helx", 4)); + + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + stpcpy(&buf[5], "hello")[0] = '-'; + assert(libsimple_rawmemcmove(&buf[5], &buf[5], 'o') == &buf[5 + 5]); + assert(!strncmp(buf, "-----hello-", 11)); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + stpcpy(&buf[5], "hello")[0] = '-';; + assert(libsimple_rawmemcmove(&buf[5], &buf[5], 'l') == &buf[5 + 3]); + assert(!strncmp(buf, "-----hello-", 11)); + + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + stpcpy(&buf[5], "hello")[0] = '-'; + assert(libsimple_rawmemcmove(&buf[3], &buf[5], 'o') == &buf[3 + 5]); + assert(!strncmp(buf, "---hellolo-", 11)); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + stpcpy(&buf[5], "hello")[0] = '-'; + assert(libsimple_rawmemcmove(&buf[3], &buf[5], 'l') == &buf[3 + 3]); + assert(!strncmp(buf, "---helello-", 11)); + + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + stpcpy(&buf[5], "hello")[0] = '-'; + assert(libsimple_rawmemcmove(&buf[8], &buf[5], 'o') == &buf[8 + 5]); + assert(!strncmp(buf, "-----helhello-", 14)); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + stpcpy(&buf[5], "hello")[0] = '-'; + assert(libsimple_rawmemcmove(&buf[8], &buf[5], 'l') == &buf[8 + 3]); + assert(!strncmp(buf, "-----helhel-", 12)); + + + memset(buf, 'x', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + assert(libsimple_rawmemccpy(buf, "hello", 'o') == &buf[5]); + assert(!strncmp(buf, "hellox", 6)); + + memset(buf, 'x', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + assert(libsimple_rawmemccpy(buf, "hello", 'l') == &buf[3]); + assert(!strncmp(buf, "helx", 4)); + + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + stpcpy(&buf[5], "hello")[0] = '-'; + assert(libsimple_mempmove(&buf[5], &buf[5], 5) == &buf[5 + 5]); + assert(!strncmp(buf, "-----hello-", 11)); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + stpcpy(&buf[5], "hello")[0] = '-'; + assert(libsimple_mempmove(&buf[3], &buf[5], 5) == &buf[3 + 5]); + assert(!strncmp(buf, "---hellolo-", 11)); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + stpcpy(&buf[5], "hello")[0] = '-'; + assert(libsimple_mempmove(&buf[8], &buf[5], 5) == &buf[8 + 5]); + assert(!strncmp(buf, "-----helhello-", 14)); + + if (!have_custom_malloc()) { stderr_real = 1; fprintf(stderr, "\nSome tests have not been ran because malloc(3) was not " diff --git a/libsimple/mem.h b/libsimple/mem.h index 4c98b29..eb99bbb 100644 --- a/libsimple/mem.h +++ b/libsimple/mem.h @@ -788,7 +788,7 @@ static inline void *libsimple_mempcpy(void *restrict __d, const void *restrict _ * @param n The number of bytes to move * @return `&d[n]` */ -static inline void *libsimple_mempmove(void *__d, const void *__s, size_t __n) /* TODO test */ +static inline void *libsimple_mempmove(void *__d, const void *__s, size_t __n) { return &((char *)memmove(__d, __s, __n))[__n]; } #ifndef mempmove # define mempmove libsimple_mempmove @@ -857,7 +857,7 @@ static inline void *libsimple_memsetelem(void *__buf, const void *__item, size_t * @param `&rawmemchr(d, c)[1]` (after copying) */ static inline void * -libsimple_rawmemccpy(void *restrict __d_, const void *restrict __s_, int __c_) /* TODO test, man */ +libsimple_rawmemccpy(void *restrict __d_, const void *restrict __s_, int __c_) /* TODO man */ { char __c = (char)__c_, *restrict __d = __d_; const char *restrict __s = __s_; @@ -900,12 +900,12 @@ void *libsimple_memcmove(void *__d_, const void *__s_, int __c_, size_t __n); * @param `&rawmemchr(d, c)[1]` (after copying) */ static inline void * -libsimple_rawmemcmove(void *__d_, const void *__s_, int __c_) /* TODO test, man */ +libsimple_rawmemcmove(void *__d_, const void *__s_, int __c_) /* TODO man */ { char *__d = __d_, *__p, __c = (char)__c_; const char *__s = __s_; size_t __n; - if (__d < __s) { + if (__d <= __s) { for (; (*__d++ = *__s) != __c; __s++); return __d; } else { @@ -934,7 +934,7 @@ libsimple_rawmemcmove(void *__d_, const void *__s_, int __c_) /* TODO test, man * @return `(void *)&((char *)s)[n]` */ static inline void * -libsimple_memreplace(void *__s_, int __old_, int __new_, size_t __n) /* TODO test, man */ +libsimple_memreplace(void *__s_, int __old_, int __new_, size_t __n) /* TODO man */ { char __old = (char)__old_, __new = (char)__new_, *__s = __s_; char *__ret = &__s[__n]; diff --git a/libsimple/str.h b/libsimple/str.h index 7120fb5..48d3aa9 100644 --- a/libsimple/str.h +++ b/libsimple/str.h @@ -479,7 +479,7 @@ static inline int libsimple_inchrcaseset(int __c, const char *__s) * @return `d` */ _LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__))) -static inline char *libsimple_strmove(char *__d, const char *__s) /* TODO test */ +static inline char *libsimple_strmove(char *__d, const char *__s) { return memmove(__d, __s, strlen(__s) + 1); } #ifndef strmove # define strmove libsimple_strmove @@ -495,7 +495,7 @@ static inline char *libsimple_strmove(char *__d, const char *__s) /* TODO test * * @return `&d[strlen(s)]` (this byte will be a NUL byte) */ _LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__))) -static inline char *libsimple_stpmove(char *__d, const char *__s) /* TODO test */ +static inline char *libsimple_stpmove(char *__d, const char *__s) { size_t __n = strlen(__s); memmove(__d, __s, __n + 1); return &__d[__n]; } #ifndef stpmove # define stpmove libsimple_stpmove @@ -511,7 +511,7 @@ static inline char *libsimple_stpmove(char *__d, const char *__s) /* TODO test * */ _LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__))) static inline char * -libsimple_strset(char *__s, int __c_) /* TODO test, man */ +libsimple_strset(char *__s, int __c_) /* TODO man */ { char __c = (char)__c_, *__r = __s; while (*__s) *__s++ = __c; @@ -531,7 +531,7 @@ libsimple_strset(char *__s, int __c_) /* TODO test, man */ */ _LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__))) static inline char * -libsimple_stpset(char *__s, int __c_) /* TODO test, man */ +libsimple_stpset(char *__s, int __c_) /* TODO man */ { char __c = (char)__c_; while (*__s) *__s++ = __c; @@ -543,7 +543,8 @@ libsimple_stpset(char *__s, int __c_) /* TODO test, man */ /** - * Copy a string, but stop after a specific character + * Copy a string, but stop after a specific character, + * the new string will be NUL-terminated * * @param d The location the string shall be copied to * @param s The string to copy @@ -553,12 +554,12 @@ libsimple_stpset(char *__s, int __c_) /* TODO test, man */ */ _LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__))) static inline char * -libsimple_strccpy(char *restrict __d, const char *restrict __s, int __c_) /* TODO test, man */ +libsimple_strccpy(char *restrict __d, const char *restrict __s, int __c_) /* TODO man */ { char __c = (char)__c_; do { if ((*__d++ = *__s) == __c) - return __d; + return *__d = '\0', __d; } while (*__s++); return NULL; } @@ -594,7 +595,7 @@ char *libsimple_strcmove(char *, const char *, int); */ _LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__, __returns_nonnull__))) static inline char * -libsimple_strreplace(char *__s, int __old_, int __new_) /* TODO test, man */ +libsimple_strreplace(char *__s, int __old_, int __new_) /* TODO man */ { char __old = (char)__old_, __new = (char)__new_; for (; *__s; __s++) diff --git a/libsimple/strn.h b/libsimple/strn.h index 5c23e3e..eaff234 100644 --- a/libsimple/strn.h +++ b/libsimple/strn.h @@ -545,7 +545,7 @@ static inline size_t libsimple_strrncaseeqlen(const char *__a, const char *__b, * @return `d` */ _LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__))) -static inline char *libsimple_strnmove(char *__d, const char *__s, size_t __n) /* TODO test */ +static inline char *libsimple_strnmove(char *__d, const char *__s, size_t __n) { size_t __len = strnlen(__s, __n); return memmove(__d, __s, __len + (__len < __n)); } #ifndef strnmove # define strnmove libsimple_strnmove @@ -563,7 +563,7 @@ static inline char *libsimple_strnmove(char *__d, const char *__s, size_t __n) / */ _LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__))) static inline char * -libsimple_stpnmove(char *__d, const char *__s, size_t __n) /* TODO test */ +libsimple_stpnmove(char *__d, const char *__s, size_t __n) { size_t __len = strnlen(__s, __n); memmove(__d, __s, __len + (__len < __n)); @@ -584,10 +584,10 @@ libsimple_stpnmove(char *__d, const char *__s, size_t __n) /* TODO test */ */ _LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__))) static inline char * -libsimple_strnset(char *__s, int __c_, size_t __n) /* TODO test, man */ +libsimple_strnset(char *__s, int __c_, size_t __n) /* TODO man */ { char __c = (char)__c_, *__r = __s; - while (__n-- && *__s) *__s++ = __c; + for (; __n && *__s; __n--) *__s++ = __c; return __r; } #ifndef strnset @@ -605,10 +605,10 @@ libsimple_strnset(char *__s, int __c_, size_t __n) /* TODO test, man */ */ _LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__))) static inline char * -libsimple_stpnset(char *__s, int __c_, size_t __n) /* TODO test, man */ +libsimple_stpnset(char *__s, int __c_, size_t __n) /* TODO man */ { char __c = (char)__c_; - while (__n-- && *__s) *__s++ = __c; + for (; __n && *__s; __n--) *__s++ = __c; return __s; } #ifndef stpnset @@ -618,6 +618,8 @@ libsimple_stpnset(char *__s, int __c_, size_t __n) /* TODO test, man */ /** * Copy a string, but stop after a specific character + * the new string will be NUL-terminated if it is + * shorter than `n` bytes * * @param d The location the string shall be copied to * @param s The string to copy @@ -628,13 +630,19 @@ libsimple_stpnset(char *__s, int __c_, size_t __n) /* TODO test, man */ */ _LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__))) static inline char * -libsimple_strnccpy(char *restrict __d, const char *restrict __s, int __c_, size_t __n) /* TODO test, man */ +libsimple_strnccpy(char *restrict __d, const char *restrict __s, int __c_, size_t __n) /* TODO man */ { - char __c = (char)__c_; + char __c = (char)__c_, *__end = &__d[__n]; do { - if ((*__d++ = *__s) == __c) + if (!__n) + break; + if ((*__d++ = *__s) == __c) { + if (__d != __end) + *__d = '\0'; return __d; - } while (*__s++ && __n--); + } + __n--; + } while (*__s++); return NULL; } #ifndef strnccpy @@ -672,7 +680,7 @@ char *libsimple_strncmove(char *, const char *, int, size_t); */ _LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__, __returns_nonnull__))) static inline char * -libsimple_strnreplace(char *__s, int __old_, int __new_, size_t __n) /* TODO test, man */ +libsimple_strnreplace(char *__s, int __old_, int __new_, size_t __n) /* TODO man */ { char __old = (char)__old_, __new = (char)__new_; for (; __n && *__s; __s++, __n--) diff --git a/memcmove.c b/memcmove.c index 3592445..53b1322 100644 --- a/memcmove.c +++ b/memcmove.c @@ -4,11 +4,11 @@ void * -libsimple_memcmove(void *d_, const void *s_, int c_, size_t n) /* TODO test, man */ +libsimple_memcmove(void *d_, const void *s_, int c_, size_t n) /* TODO man */ { char *d = d_, c = (char)c_, *p; const char *s = s_; - if (d < s) { + if (d <= s) { for (; n; n--, s++) if ((*d++ = *s) == c) return d; @@ -36,6 +36,55 @@ libsimple_memcmove(void *d_, const void *s_, int c_, size_t n) /* TODO test, man int main(void) { + char buf[1024]; + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + stpcpy(&buf[5], "hello")[0] = '-'; + assert(libsimple_memcmove(&buf[5], &buf[5], 'o', 5) == &buf[5 + 5]); + assert(!strncmp(buf, "-----hello-", 11)); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + stpcpy(&buf[5], "hello")[0] = '-';; + assert(libsimple_memcmove(&buf[5], &buf[5], 'l', 5) == &buf[5 + 3]); + assert(!strncmp(buf, "-----hello-", 11)); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + stpcpy(&buf[5], "hello")[0] = '-'; + assert(libsimple_memcmove(&buf[5], &buf[5], 'x', 5) == NULL); + assert(!strncmp(buf, "-----hello-", 11)); + + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + stpcpy(&buf[5], "hello")[0] = '-'; + assert(libsimple_memcmove(&buf[3], &buf[5], 'o', 5) == &buf[3 + 5]); + assert(!strncmp(buf, "---hellolo-", 11)); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + stpcpy(&buf[5], "hello")[0] = '-'; + assert(libsimple_memcmove(&buf[3], &buf[5], 'l', 5) == &buf[3 + 3]); + assert(!strncmp(buf, "---helello-", 11)); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + stpcpy(&buf[5], "hello")[0] = '-'; + assert(libsimple_memcmove(&buf[3], &buf[5], 'x', 5) == NULL); + assert(!strncmp(buf, "---hellolo-", 11)); + + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + stpcpy(&buf[5], "hello")[0] = '-'; + assert(libsimple_memcmove(&buf[8], &buf[5], 'o', 5) == &buf[8 + 5]); + assert(!strncmp(buf, "-----helhello-", 14)); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + stpcpy(&buf[5], "hello")[0] = '-'; + assert(libsimple_memcmove(&buf[8], &buf[5], 'l', 5) == &buf[8 + 3]); + assert(!strncmp(buf, "-----helhel-", 12)); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + stpcpy(&buf[5], "hello")[0] = '-'; + assert(libsimple_memcmove(&buf[8], &buf[5], 'x', 5) == NULL); + assert(!strncmp(buf, "-----helhello-", 14)); + return 0; } diff --git a/memrcasechr_inv.c b/memrcasechr_inv.c index f6c16e7..629bcc2 100644 --- a/memrcasechr_inv.c +++ b/memrcasechr_inv.c @@ -4,7 +4,7 @@ void * -libsimple_memrcasechr_inv(const void *s_, int c, size_t n_) /* TODO test, man */ +libsimple_memrcasechr_inv(const void *s_, int c, size_t n_) /* TODO man */ { char *s = *(char **)(void *)&s_; ssize_t n = n_; @@ -20,6 +20,13 @@ libsimple_memrcasechr_inv(const void *s_, int c, size_t n_) /* TODO test, man */ int main(void) { + assert(!strcmpnul(libsimple_memrcasechr_inv("aabbaabb", 'b', 8), "abb")); + assert(!strcmpnul(libsimple_memrcasechr_inv("aabbaabb", 'B', 8), "abb")); + assert(!strcmpnul(libsimple_memrcasechr_inv("AABBAABB", 'b', 8), "ABB")); + assert(!strcmpnul(libsimple_memrcasechr_inv("AABBAABB", 'B', 8), "ABB")); + assert(!strcmpnul(libsimple_memrcasechr_inv("aabbaabb", 'a', 8), "b")); + assert(!strcmpnul(libsimple_memrcasechr_inv("aabbbb\0", '\0', 8), "b")); + assert(!strcmpnul(libsimple_memrcasechr_inv("--------", '-', 8), NULL)); return 0; } diff --git a/memrchr_inv.c b/memrchr_inv.c index d7239b3..d49d878 100644 --- a/memrchr_inv.c +++ b/memrchr_inv.c @@ -4,7 +4,7 @@ void * -libsimple_memrchr_inv(const void *s_, int c_, size_t n_) /* TODO test, man */ +libsimple_memrchr_inv(const void *s_, int c_, size_t n_) /* TODO man */ { char *s = *(char **)(void *)&s_, c = (char)c_; ssize_t n = n_; @@ -19,6 +19,13 @@ libsimple_memrchr_inv(const void *s_, int c_, size_t n_) /* TODO test, man */ int main(void) { + assert(!strcmpnul(libsimple_memrchr_inv("aabbaabb", 'b', 8), "abb")); + assert(!strcmpnul(libsimple_memrchr_inv("aabbaabb", 'B', 8), "b")); + assert(!strcmpnul(libsimple_memrchr_inv("AABBAABB", 'b', 8), "B")); + assert(!strcmpnul(libsimple_memrchr_inv("AABBAABB", 'B', 8), "ABB")); + assert(!strcmpnul(libsimple_memrchr_inv("aabbaabb", 'a', 8), "b")); + assert(!strcmpnul(libsimple_memrchr_inv("aabbbb\0", '\0', 8), "b")); + assert(!strcmpnul(libsimple_memrchr_inv("--------", '-', 8), NULL)); return 0; } diff --git a/rawmemrcasechr_inv.c b/rawmemrcasechr_inv.c index 847bdea..2193181 100644 --- a/rawmemrcasechr_inv.c +++ b/rawmemrcasechr_inv.c @@ -4,7 +4,7 @@ void * -libsimple_rawmemrcasechr_inv(const void *s_, int c, size_t n) /* TODO test, man */ +libsimple_rawmemrcasechr_inv(const void *s_, int c, size_t n) /* TODO man */ { char *s = *(char **)(void *)&s_; c = tolower(c); @@ -19,6 +19,12 @@ libsimple_rawmemrcasechr_inv(const void *s_, int c, size_t n) /* TODO test, man int main(void) { + assert(!strcmpnul(libsimple_rawmemrcasechr_inv("aabbaabb", 'b', 8), "abb")); + assert(!strcmpnul(libsimple_rawmemrcasechr_inv("aabbaabb", 'B', 8), "abb")); + assert(!strcmpnul(libsimple_rawmemrcasechr_inv("AABBAABB", 'b', 8), "ABB")); + assert(!strcmpnul(libsimple_rawmemrcasechr_inv("AABBAABB", 'B', 8), "ABB")); + assert(!strcmpnul(libsimple_rawmemrcasechr_inv("aabbaabb", 'a', 8), "b")); + assert(!strcmpnul(libsimple_rawmemrcasechr_inv("aabbbb\0", '\0', 8), "b")); return 0; } diff --git a/rawmemrchr_inv.c b/rawmemrchr_inv.c index b60a4fe..6c6b9db 100644 --- a/rawmemrchr_inv.c +++ b/rawmemrchr_inv.c @@ -4,7 +4,7 @@ void * -libsimple_rawmemrchr_inv(const void *s_, int c_, size_t n) /* TODO test, man */ +libsimple_rawmemrchr_inv(const void *s_, int c_, size_t n) /* TODO man */ { char *s = *(char **)(void *)&s_, c = (char)c_; while (s[--n] == c); @@ -18,6 +18,12 @@ libsimple_rawmemrchr_inv(const void *s_, int c_, size_t n) /* TODO test, man */ int main(void) { + assert(!strcmpnul(libsimple_rawmemrchr_inv("aabbaabb", 'b', 8), "abb")); + assert(!strcmpnul(libsimple_rawmemrchr_inv("aabbaabb", 'B', 8), "b")); + assert(!strcmpnul(libsimple_rawmemrchr_inv("AABBAABB", 'b', 8), "B")); + assert(!strcmpnul(libsimple_rawmemrchr_inv("AABBAABB", 'B', 8), "ABB")); + assert(!strcmpnul(libsimple_rawmemrchr_inv("aabbaabb", 'a', 8), "b")); + assert(!strcmpnul(libsimple_rawmemrchr_inv("aabbbb\0", '\0', 8), "b")); return 0; } diff --git a/strcmove.c b/strcmove.c index 2adc49d..8737061 100644 --- a/strcmove.c +++ b/strcmove.c @@ -4,21 +4,25 @@ char * -libsimple_strcmove(char *d, const char *s, int c_) /* TODO test, man */ +libsimple_strcmove(char *d, const char *s, int c_) /* TODO man */ { char c = (char)c_, *p; size_t n; - if (d < s) { + if (d <= s) { do { - if ((*d++ = *s) == c) + if ((*d++ = *s) == c) { + *d = '\0'; return d; + } } while (*s++); } else { for (p = *(char **)(void *)&s;; p++) { - if (*p == c || *p) { + if (*p == c || !*p) { n = (size_t)(p - s); p = &d[n]; do { d[n] = s[n]; } while (n--); + if (*p) + p[1] = '\0'; return *p == c ? &p[1] : NULL; } } @@ -33,6 +37,72 @@ libsimple_strcmove(char *d, const char *s, int c_) /* TODO test, man */ int main(void) { + char buf[1024]; + + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strcmove(&buf[5], &buf[5], '\0') == &buf[5 + 6]); + assert(!strcmp(buf, "-----hello")); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strcmove(&buf[5], &buf[5], 'o') == &buf[5 + 5]); + assert(!strcmp(buf, "-----hello")); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strcmove(&buf[5], &buf[5], 'l') == &buf[5 + 3]); + assert(!strcmp(buf, "-----hel")); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strcmove(&buf[5], &buf[5], 'x') == NULL); + assert(!strcmp(buf, "-----hello")); + + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strcmove(&buf[3], &buf[5], '\0') == &buf[3 + 6]); + assert(!strcmp(buf, "---hello")); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strcmove(&buf[3], &buf[5], 'o') == &buf[3 + 5]); + assert(!strcmp(buf, "---hello")); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strcmove(&buf[3], &buf[5], 'l') == &buf[3 + 3]); + assert(!strcmp(buf, "---hel")); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strcmove(&buf[3], &buf[5], 'x') == NULL); + assert(!strcmp(buf, "---hello")); + + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strcmove(&buf[8], &buf[5], '\0') == &buf[8 + 6]); + assert(!strcmp(buf, "-----helhello")); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strcmove(&buf[8], &buf[5], 'o') == &buf[8 + 5]); + assert(!strcmp(buf, "-----helhello")); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strcmove(&buf[8], &buf[5], 'l') == &buf[8 + 3]); + assert(!strcmp(buf, "-----helhel")); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strcmove(&buf[8], &buf[5], 'x') == NULL); + assert(!strcmp(buf, "-----helhello")); + + return 0; } diff --git a/strncmove.c b/strncmove.c index ce53ed5..25f83bc 100644 --- a/strncmove.c +++ b/strncmove.c @@ -4,20 +4,31 @@ char * -libsimple_strncmove(char *d, const char *s, int c_, size_t n) /* TODO test, man */ +libsimple_strncmove(char *d, const char *s, int c_, size_t n) /* TODO man */ { - char c = (char)c_, *p; - if (d < s) { + char c = (char)c_, *p, *end = &d[n]; + if (d <= s) { do { - if ((*d++ = *s) == c) + if (!n--) + break; + if ((*d++ = *s) == c) { + if (n) + *d = '\0'; return d; - } while (*s++ && n--); + } + } while (*s++); } else { for (p = *(char **)(void *)&s; n; n--, p++) { - if (*p == c || *p) { + if (*p == c || !*p) { n = (size_t)(p - s); p = &d[n]; - do { d[n] = s[n]; } while (n--); + for (;; n--) { + d[n] = s[n]; + if (!n) + break; + } + if (&p[1] != end && *p) + p[1] = '\0'; return *p == c ? &p[1] : NULL; } } @@ -36,6 +47,285 @@ libsimple_strncmove(char *d, const char *s, int c_, size_t n) /* TODO test, man int main(void) { + char buf[1024]; + + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strncmove(&buf[5], &buf[5], '\0', 1024) == &buf[5 + 6]); + assert(!strcmp(buf, "-----hello")); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strncmove(&buf[5], &buf[5], 'o', 1024) == &buf[5 + 5]); + assert(!strcmp(buf, "-----hello")); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strncmove(&buf[5], &buf[5], 'l', 1024) == &buf[5 + 3]); + assert(!strcmp(buf, "-----hel")); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strncmove(&buf[5], &buf[5], 'x', 1024) == NULL); + assert(!strcmp(buf, "-----hello")); + + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strncmove(&buf[3], &buf[5], '\0', 1024) == &buf[3 + 6]); + assert(!strcmp(buf, "---hello")); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strncmove(&buf[3], &buf[5], 'o', 1024) == &buf[3 + 5]); + assert(!strcmp(buf, "---hello")); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strncmove(&buf[3], &buf[5], 'l', 1024) == &buf[3 + 3]); + assert(!strcmp(buf, "---hel")); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strncmove(&buf[3], &buf[5], 'x', 1024) == NULL); + assert(!strcmp(buf, "---hello")); + + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strncmove(&buf[8], &buf[5], '\0', 1024) == &buf[8 + 6]); + assert(!strcmp(buf, "-----helhello")); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strncmove(&buf[8], &buf[5], 'o', 1024) == &buf[8 + 5]); + assert(!strcmp(buf, "-----helhello")); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strncmove(&buf[8], &buf[5], 'l', 1024) == &buf[8 + 3]); + assert(!strcmp(buf, "-----helhel")); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strncmove(&buf[8], &buf[5], 'x', 1024) == NULL); + assert(!strcmp(buf, "-----helhello")); + + + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strncmove(&buf[5], &buf[5], '\0', 6) == &buf[5 + 6]); + assert(!strcmp(buf, "-----hello")); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strncmove(&buf[5], &buf[5], 'o', 6) == &buf[5 + 5]); + assert(!strcmp(buf, "-----hello")); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strncmove(&buf[5], &buf[5], 'l', 6) == &buf[5 + 3]); + assert(!strcmp(buf, "-----hel")); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strncmove(&buf[5], &buf[5], 'x', 6) == NULL); + assert(!strcmp(buf, "-----hello")); + + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strncmove(&buf[3], &buf[5], '\0', 6) == &buf[3 + 6]); + assert(!strcmp(buf, "---hello")); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strncmove(&buf[3], &buf[5], 'o', 6) == &buf[3 + 5]); + assert(!strcmp(buf, "---hello")); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strncmove(&buf[3], &buf[5], 'l', 6) == &buf[3 + 3]); + assert(!strcmp(buf, "---hel")); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strncmove(&buf[3], &buf[5], 'x', 6) == NULL); + assert(!strcmp(buf, "---hello")); + + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strncmove(&buf[8], &buf[5], '\0', 6) == &buf[8 + 6]); + assert(!strcmp(buf, "-----helhello")); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strncmove(&buf[8], &buf[5], 'o', 6) == &buf[8 + 5]); + assert(!strcmp(buf, "-----helhello")); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strncmove(&buf[8], &buf[5], 'l', 6) == &buf[8 + 3]); + assert(!strcmp(buf, "-----helhel")); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strncmove(&buf[8], &buf[5], 'x', 6) == NULL); + assert(!strcmp(buf, "-----helhello")); + + + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + stpcpy(&buf[5], "hello")[0] = '-'; + assert(libsimple_strncmove(&buf[5], &buf[5], '\0', 5) == NULL); + assert(!strncmp(buf, "-----hello-", 11)); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + stpcpy(&buf[5], "hello")[0] = '-'; + assert(libsimple_strncmove(&buf[5], &buf[5], 'o', 5) == &buf[5 + 5]); + assert(!strncmp(buf, "-----hello-", 11)); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + stpcpy(&buf[5], "hello")[0] = '-'; + assert(libsimple_strncmove(&buf[5], &buf[5], 'l', 5) == &buf[5 + 3]); + assert(!strcmp(buf, "-----hel")); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + stpcpy(&buf[5], "hello")[0] = '-'; + assert(libsimple_strncmove(&buf[5], &buf[5], 'x', 5) == NULL); + assert(!strncmp(buf, "-----hello-", 11)); + + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strncmove(&buf[5], &buf[5], '\0', 5) == NULL); + assert(!strcmp(buf, "-----hello")); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strncmove(&buf[5], &buf[5], 'o', 5) == &buf[5 + 5]); + assert(!strcmp(buf, "-----hello")); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strncmove(&buf[5], &buf[5], 'l', 5) == &buf[5 + 3]); + assert(!strcmp(buf, "-----hel")); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strncmove(&buf[5], &buf[5], 'x', 5) == NULL); + assert(!strcmp(buf, "-----hello")); + + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strncmove(&buf[3], &buf[5], '\0', 5) == NULL); + assert(!strcmp(buf, "---hellolo")); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strncmove(&buf[3], &buf[5], 'o', 5) == &buf[3 + 5]); + assert(!strcmp(buf, "---hellolo")); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strncmove(&buf[3], &buf[5], 'l', 5) == &buf[3 + 3]); + assert(!strcmp(buf, "---hel")); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strncmove(&buf[3], &buf[5], 'x', 5) == NULL); + assert(!strcmp(buf, "---hellolo")); + + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strncmove(&buf[8], &buf[5], '\0', 5) == NULL); + assert(!strncmp(buf, "-----helhello-", 14)); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strncmove(&buf[8], &buf[5], 'o', 5) == &buf[8 + 5]); + assert(!strncmp(buf, "-----helhello-", 14)); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strncmove(&buf[8], &buf[5], 'l', 5) == &buf[8 + 3]); + assert(!strcmp(buf, "-----helhel")); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strncmove(&buf[8], &buf[5], 'x', 5) == NULL); + assert(!strncmp(buf, "-----helhello-", 14)); + + + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strncmove(&buf[5], &buf[5], '\0', 3) == NULL); + assert(!strcmp(buf, "-----hello")); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strncmove(&buf[5], &buf[5], 'o', 3) == NULL); + assert(!strcmp(buf, "-----hello")); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strncmove(&buf[5], &buf[5], 'l', 3) == &buf[5 + 3]); + assert(!strcmp(buf, "-----hello")); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strncmove(&buf[5], &buf[5], 'x', 3) == NULL); + assert(!strcmp(buf, "-----hello")); + + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strncmove(&buf[3], &buf[5], '\0', 3) == NULL); + assert(!strcmp(buf, "---helello")); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strncmove(&buf[3], &buf[5], 'o', 3) == NULL); + assert(!strcmp(buf, "---helello")); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strncmove(&buf[3], &buf[5], 'l', 3) == &buf[3 + 3]); + assert(!strcmp(buf, "---helello")); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strncmove(&buf[3], &buf[5], 'x', 3) == NULL); + assert(!strcmp(buf, "---helello")); + + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strncmove(&buf[8], &buf[5], '\0', 3) == NULL); + assert(!strncmp(buf, "-----helhel-", 12)); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strncmove(&buf[8], &buf[5], 'o', 3) == NULL); + assert(!strncmp(buf, "-----helhel-", 12)); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strncmove(&buf[8], &buf[5], 'l', 3) == &buf[8 + 3]); + assert(!strncmp(buf, "-----helhel-", 12)); + + memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0'; + strcpy(&buf[5], "hello"); + assert(libsimple_strncmove(&buf[8], &buf[5], 'x', 3) == NULL); + assert(!strncmp(buf, "-----helhel-", 12)); + + return 0; } diff --git a/strrcasechr_inv.c b/strrcasechr_inv.c index fea9f21..66a3837 100644 --- a/strrcasechr_inv.c +++ b/strrcasechr_inv.c @@ -4,7 +4,7 @@ char * -libsimple_strrcasechr_inv(const char *s_, int c) /* TODO test, man */ +libsimple_strrcasechr_inv(const char *s_, int c) /* TODO man */ { char *s = *(char **)(void *)&s_, lc = (char)tolower(c), uc = (char)toupper(c), *r = NULL; if (lc != uc) { @@ -26,6 +26,13 @@ libsimple_strrcasechr_inv(const char *s_, int c) /* TODO test, man */ int main(void) { + assert(!strcmpnul(libsimple_strrcasechr_inv("aabbaabb", 'b'), "abb")); + assert(!strcmpnul(libsimple_strrcasechr_inv("aabbaabb", 'B'), "abb")); + assert(!strcmpnul(libsimple_strrcasechr_inv("AABBAABB", 'b'), "ABB")); + assert(!strcmpnul(libsimple_strrcasechr_inv("AABBAABB", 'B'), "ABB")); + assert(!strcmpnul(libsimple_strrcasechr_inv("aabbaabb", 'a'), "b")); + assert(!strcmpnul(libsimple_strrcasechr_inv("aabbaabb", '\0'), "b")); + assert(!strcmpnul(libsimple_strrcasechr_inv("--------", '-'), NULL)); return 0; } diff --git a/strrchr_inv.c b/strrchr_inv.c index 2b57fdf..3fdd1dc 100644 --- a/strrchr_inv.c +++ b/strrchr_inv.c @@ -4,7 +4,7 @@ char * -libsimple_strrchr_inv(const char *s_, int c_) /* TODO test, man */ +libsimple_strrchr_inv(const char *s_, int c_) /* TODO man */ { char *s = *(char **)(void *)&s_, c = (char)c_, *r = NULL; for (; *s; s++) @@ -20,6 +20,13 @@ libsimple_strrchr_inv(const char *s_, int c_) /* TODO test, man */ int main(void) { + assert(!strcmpnul(libsimple_strrchr_inv("aabbaabb", 'b'), "abb")); + assert(!strcmpnul(libsimple_strrchr_inv("aabbaabb", 'B'), "b")); + assert(!strcmpnul(libsimple_strrchr_inv("AABBAABB", 'b'), "B")); + assert(!strcmpnul(libsimple_strrchr_inv("AABBAABB", 'B'), "ABB")); + assert(!strcmpnul(libsimple_strrchr_inv("aabbaabb", 'a'), "b")); + assert(!strcmpnul(libsimple_strrchr_inv("aabbaabb", '\0'), "b")); + assert(!strcmpnul(libsimple_strrchr_inv("--------", '-'), NULL)); return 0; } diff --git a/strrncasechr_inv.c b/strrncasechr_inv.c index c300c43..62a20f1 100644 --- a/strrncasechr_inv.c +++ b/strrncasechr_inv.c @@ -4,17 +4,14 @@ char * -libsimple_strrncasechr_inv(const char *s_, int c, size_t n) /* TODO test, man */ +libsimple_strrncasechr_inv(const char *s_, int c, size_t n) /* TODO man */ { char *s = *(char **)(void *)&s_; char *end = &s[n], *r = NULL; c = tolower(c); - for (; s != end; s++) { + for (; *s && s != end; s++) if (tolower(*s) != c) r = s; - if (!*s) - break; - } return r; } @@ -25,6 +22,54 @@ libsimple_strrncasechr_inv(const char *s_, int c, size_t n) /* TODO test, man */ int main(void) { + assert(!strcmpnul(libsimple_strrncasechr_inv("aabbaabb", 'b', SIZE_MAX), "abb")); + assert(!strcmpnul(libsimple_strrncasechr_inv("aabbaabb", 'B', SIZE_MAX), "abb")); + assert(!strcmpnul(libsimple_strrncasechr_inv("AABBAABB", 'b', SIZE_MAX), "ABB")); + assert(!strcmpnul(libsimple_strrncasechr_inv("AABBAABB", 'B', SIZE_MAX), "ABB")); + assert(!strcmpnul(libsimple_strrncasechr_inv("aabbaabb", 'a', SIZE_MAX), "b")); + assert(!strcmpnul(libsimple_strrncasechr_inv("aabbaabb", '\0', SIZE_MAX), "b")); + assert(!strcmpnul(libsimple_strrncasechr_inv("--------", '-', SIZE_MAX), NULL)); + + assert(!strcmpnul(libsimple_strrncasechr_inv("aabbaabb", 'b', 9), "abb")); + assert(!strcmpnul(libsimple_strrncasechr_inv("aabbaabb", 'B', 9), "abb")); + assert(!strcmpnul(libsimple_strrncasechr_inv("AABBAABB", 'b', 9), "ABB")); + assert(!strcmpnul(libsimple_strrncasechr_inv("AABBAABB", 'B', 9), "ABB")); + assert(!strcmpnul(libsimple_strrncasechr_inv("aabbaabb", 'a', 9), "b")); + assert(!strcmpnul(libsimple_strrncasechr_inv("aabbaabb", '\0', 9), "b")); + assert(!strcmpnul(libsimple_strrncasechr_inv("--------", '-', 9), NULL)); + + assert(!strcmpnul(libsimple_strrncasechr_inv("aabbaabb", 'b', 8), "abb")); + assert(!strcmpnul(libsimple_strrncasechr_inv("aabbaabb", 'B', 8), "abb")); + assert(!strcmpnul(libsimple_strrncasechr_inv("AABBAABB", 'b', 8), "ABB")); + assert(!strcmpnul(libsimple_strrncasechr_inv("AABBAABB", 'B', 8), "ABB")); + assert(!strcmpnul(libsimple_strrncasechr_inv("aabbaabb", 'a', 8), "b")); + assert(!strcmpnul(libsimple_strrncasechr_inv("aabbaabb", '\0', 8), "b")); + assert(!strcmpnul(libsimple_strrncasechr_inv("--------", '-', 8), NULL)); + + assert(!strcmpnul(libsimple_strrncasechr_inv("aabbaabb", 'b', 7), "abb")); + assert(!strcmpnul(libsimple_strrncasechr_inv("aabbaabb", 'B', 7), "abb")); + assert(!strcmpnul(libsimple_strrncasechr_inv("AABBAABB", 'b', 7), "ABB")); + assert(!strcmpnul(libsimple_strrncasechr_inv("AABBAABB", 'B', 7), "ABB")); + assert(!strcmpnul(libsimple_strrncasechr_inv("aabbaabb", 'a', 7), "bb")); + assert(!strcmpnul(libsimple_strrncasechr_inv("aabbaabb", '\0', 7), "bb")); + assert(!strcmpnul(libsimple_strrncasechr_inv("--------", '-', 7), NULL)); + + assert(!strcmpnul(libsimple_strrncasechr_inv("aabbaabb", 'b', 4), "abbaabb")); + assert(!strcmpnul(libsimple_strrncasechr_inv("aabbaabb", 'B', 4), "abbaabb")); + assert(!strcmpnul(libsimple_strrncasechr_inv("AABBAABB", 'b', 4), "ABBAABB")); + assert(!strcmpnul(libsimple_strrncasechr_inv("AABBAABB", 'B', 4), "ABBAABB")); + assert(!strcmpnul(libsimple_strrncasechr_inv("aabbaabb", 'a', 4), "baabb")); + assert(!strcmpnul(libsimple_strrncasechr_inv("aabbaabb", '\0', 4), "baabb")); + assert(!strcmpnul(libsimple_strrncasechr_inv("--------", '-', 4), NULL)); + + assert(!strcmpnul(libsimple_strrncasechr_inv("aabbaabb", 'b', 0), NULL)); + assert(!strcmpnul(libsimple_strrncasechr_inv("aabbaabb", 'B', 0), NULL)); + assert(!strcmpnul(libsimple_strrncasechr_inv("AABBAABB", 'b', 0), NULL)); + assert(!strcmpnul(libsimple_strrncasechr_inv("AABBAABB", 'B', 0), NULL)); + assert(!strcmpnul(libsimple_strrncasechr_inv("aabbaabb", 'a', 0), NULL)); + assert(!strcmpnul(libsimple_strrncasechr_inv("aabbaabb", '\0', 0), NULL)); + assert(!strcmpnul(libsimple_strrncasechr_inv("--------", '-', 0), NULL)); + return 0; } diff --git a/strrnchr_inv.c b/strrnchr_inv.c index 8ab9965..80be865 100644 --- a/strrnchr_inv.c +++ b/strrnchr_inv.c @@ -4,16 +4,13 @@ char * -libsimple_strrnchr_inv(const char *s_, int c_, size_t n) /* TODO test, man */ +libsimple_strrnchr_inv(const char *s_, int c_, size_t n) /* TODO man */ { char *s = *(char **)(void *)&s_, c = (char)c_; char *end = &s[n], *r = NULL; - for (; s != end; s++) { + for (; *s && s != end; s++) if (*s != c) r = s; - if (!*s) - break; - } return r; } @@ -24,6 +21,54 @@ libsimple_strrnchr_inv(const char *s_, int c_, size_t n) /* TODO test, man */ int main(void) { + assert(!strcmpnul(libsimple_strrnchr_inv("aabbaabb", 'b', SIZE_MAX), "abb")); + assert(!strcmpnul(libsimple_strrnchr_inv("aabbaabb", 'B', SIZE_MAX), "b")); + assert(!strcmpnul(libsimple_strrnchr_inv("AABBAABB", 'b', SIZE_MAX), "B")); + assert(!strcmpnul(libsimple_strrnchr_inv("AABBAABB", 'B', SIZE_MAX), "ABB")); + assert(!strcmpnul(libsimple_strrnchr_inv("aabbaabb", 'a', SIZE_MAX), "b")); + assert(!strcmpnul(libsimple_strrnchr_inv("aabbaabb", '\0', SIZE_MAX), "b")); + assert(!strcmpnul(libsimple_strrnchr_inv("--------", '-', SIZE_MAX), NULL)); + + assert(!strcmpnul(libsimple_strrnchr_inv("aabbaabb", 'b', 9), "abb")); + assert(!strcmpnul(libsimple_strrnchr_inv("aabbaabb", 'B', 9), "b")); + assert(!strcmpnul(libsimple_strrnchr_inv("AABBAABB", 'b', 9), "B")); + assert(!strcmpnul(libsimple_strrnchr_inv("AABBAABB", 'B', 9), "ABB")); + assert(!strcmpnul(libsimple_strrnchr_inv("aabbaabb", 'a', 9), "b")); + assert(!strcmpnul(libsimple_strrnchr_inv("aabbaabb", '\0', 9), "b")); + assert(!strcmpnul(libsimple_strrnchr_inv("--------", '-', 9), NULL)); + + assert(!strcmpnul(libsimple_strrnchr_inv("aabbaabb", 'b', 8), "abb")); + assert(!strcmpnul(libsimple_strrnchr_inv("aabbaabb", 'B', 8), "b")); + assert(!strcmpnul(libsimple_strrnchr_inv("AABBAABB", 'b', 8), "B")); + assert(!strcmpnul(libsimple_strrnchr_inv("AABBAABB", 'B', 8), "ABB")); + assert(!strcmpnul(libsimple_strrnchr_inv("aabbaabb", 'a', 8), "b")); + assert(!strcmpnul(libsimple_strrnchr_inv("aabbaabb", '\0', 8), "b")); + assert(!strcmpnul(libsimple_strrnchr_inv("--------", '-', 8), NULL)); + + assert(!strcmpnul(libsimple_strrnchr_inv("aabbaabb", 'b', 7), "abb")); + assert(!strcmpnul(libsimple_strrnchr_inv("aabbaabb", 'B', 7), "bb")); + assert(!strcmpnul(libsimple_strrnchr_inv("AABBAABB", 'b', 7), "BB")); + assert(!strcmpnul(libsimple_strrnchr_inv("AABBAABB", 'B', 7), "ABB")); + assert(!strcmpnul(libsimple_strrnchr_inv("aabbaabb", 'a', 7), "bb")); + assert(!strcmpnul(libsimple_strrnchr_inv("aabbaabb", '\0', 7), "bb")); + assert(!strcmpnul(libsimple_strrnchr_inv("--------", '-', 7), NULL)); + + assert(!strcmpnul(libsimple_strrnchr_inv("aabbaabb", 'b', 4), "abbaabb")); + assert(!strcmpnul(libsimple_strrnchr_inv("aabbaabb", 'B', 4), "baabb")); + assert(!strcmpnul(libsimple_strrnchr_inv("AABBAABB", 'b', 4), "BAABB")); + assert(!strcmpnul(libsimple_strrnchr_inv("AABBAABB", 'B', 4), "ABBAABB")); + assert(!strcmpnul(libsimple_strrnchr_inv("aabbaabb", 'a', 4), "baabb")); + assert(!strcmpnul(libsimple_strrnchr_inv("aabbaabb", '\0', 4), "baabb")); + assert(!strcmpnul(libsimple_strrnchr_inv("--------", '-', 4), NULL)); + + assert(!strcmpnul(libsimple_strrnchr_inv("aabbaabb", 'b', 0), NULL)); + assert(!strcmpnul(libsimple_strrnchr_inv("aabbaabb", 'B', 0), NULL)); + assert(!strcmpnul(libsimple_strrnchr_inv("AABBAABB", 'b', 0), NULL)); + assert(!strcmpnul(libsimple_strrnchr_inv("AABBAABB", 'B', 0), NULL)); + assert(!strcmpnul(libsimple_strrnchr_inv("aabbaabb", 'a', 0), NULL)); + assert(!strcmpnul(libsimple_strrnchr_inv("aabbaabb", '\0', 0), NULL)); + assert(!strcmpnul(libsimple_strrnchr_inv("--------", '-', 0), NULL)); + return 0; } diff --git a/vmemalloc.c b/vmemalloc.c index b4e4ed9..f79ebb0 100644 --- a/vmemalloc.c +++ b/vmemalloc.c @@ -223,8 +223,13 @@ libsimple_vmemalloc(size_t n, va_list ap) /* TODO test ([v]{mem,array}alloc) */ n = n ? n : (state.if_zero > 0); if (state.cache_align || !state.cache_split) { +#ifdef _SC_LEVEL1_DCACHE_LINESIZE tmp = sysconf(_SC_LEVEL1_DCACHE_LINESIZE); cacheline = (size_t)(tmp < 1 ? 64L : tmp); +#else + (void) tmp; + cacheline = 64; +#endif } if (state.cache_align) { -- cgit v1.2.3-70-g09d2