From 160eaaddcfed8075abd06624835a2d49548eb5de Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sun, 21 Oct 2018 11:51:38 +0200 Subject: Add strcasechr and strcasechrnul MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- strcasechr.c | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 strcasechr.c (limited to 'strcasechr.c') diff --git a/strcasechr.c b/strcasechr.c new file mode 100644 index 0000000..83e8f73 --- /dev/null +++ b/strcasechr.c @@ -0,0 +1,41 @@ +/* See LICENSE file for copyright and license details. */ +#include "libsimple.h" +#ifndef TEST + + +char * +libsimple_strcasechr(const char *s_, int c) +{ + char *s = *(char **)(void *)&s_, lc = (char)tolower(c), uc = (char)toupper(c); + if (lc != uc) + for (; *s && *s != lc && *s != uc; s++); + else + for (; *s && *s != lc; s++); + return *s ? s : NULL; +} + + +#else +#include "test.h" + +int +main(void) +{ + assert(!strcmpnul(libsimple_strcasechr("abcABCabcABCx123123", 'c'), "cABCabcABCx123123")); + assert(!strcmpnul(libsimple_strcasechr("abcABCabcABCx123123", 'a'), "abcABCabcABCx123123")); + assert(!strcmpnul(libsimple_strcasechr("abcABCabcABCx123123", 'x'), "x123123")); + assert(!strcmpnul(libsimple_strcasechr("abcABCabcABCx123123", 'C'), "cABCabcABCx123123")); + assert(!strcmpnul(libsimple_strcasechr("abcABCabcABCx123123", 'A'), "abcABCabcABCx123123")); + assert(!strcmpnul(libsimple_strcasechr("abcABCabcABCx123123", 'X'), "x123123")); + assert(!strcmpnul(libsimple_strcasechr("ABCabcABCabcX123123", 'c'), "CabcABCabcX123123")); + assert(!strcmpnul(libsimple_strcasechr("ABCabcABCabcX123123", 'a'), "ABCabcABCabcX123123")); + assert(!strcmpnul(libsimple_strcasechr("ABCabcABCabcX123123", 'x'), "X123123")); + assert(!strcmpnul(libsimple_strcasechr("ABCabcABCabcX123123", 'C'), "CabcABCabcX123123")); + assert(!strcmpnul(libsimple_strcasechr("ABCabcABCabcX123123", 'A'), "ABCabcABCabcX123123")); + assert(!strcmpnul(libsimple_strcasechr("ABCabcABCabcX123123", 'X'), "X123123")); + assert(!libsimple_strcasechr("ABCabcABCabcX123123", '0')); + assert(!strcmpnul(libsimple_strcasechr("ABCabcABCabcX123123", '1'), "123123")); + return 0; +} + +#endif -- cgit v1.2.3-70-g09d2