aboutsummaryrefslogtreecommitdiffstats
path: root/strrcasechr.c
blob: 26abb7ee93f508af58fa203d481a0adf30a1b97b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/* See LICENSE file for copyright and license details. */
#include "common.h"
#ifndef TEST


char *
libsimple_strrcasechr(const char *s_, int c)
{
	char *s = *(char **)(void *)&s_, lc = (char)tolower(c), uc = (char)toupper(c), *r = NULL;
	if (lc != uc) {
		for (; *s; s++)
			if (*s == lc || *s == uc)
				r = s;
	} else {
		for (; *s; s++)
			if (*s == lc)
				r = s;
	}
	return r;
}


#else
#include "test.h"

int
main(void)
{
	assert(!strcmpnul(libsimple_strrcasechr("abcABCabcABCx123123", 'c'), "Cx123123"));
	assert(!strcmpnul(libsimple_strrcasechr("abcABCabcABCx123123", 'a'), "ABCx123123"));
	assert(!strcmpnul(libsimple_strrcasechr("abcABCabcABCx123123", 'x'), "x123123"));
	assert(!strcmpnul(libsimple_strrcasechr("abcABCabcABCx123123", 'C'), "Cx123123"));
	assert(!strcmpnul(libsimple_strrcasechr("abcABCabcABCx123123", 'A'), "ABCx123123"));
	assert(!strcmpnul(libsimple_strrcasechr("abcABCabcABCx123123", 'X'), "x123123"));
	assert(!strcmpnul(libsimple_strrcasechr("ABCabcABCabcX123123", 'c'), "cX123123"));
	assert(!strcmpnul(libsimple_strrcasechr("ABCabcABCabcX123123", 'a'), "abcX123123"));
	assert(!strcmpnul(libsimple_strrcasechr("ABCabcABCabcX123123", 'x'), "X123123"));
	assert(!strcmpnul(libsimple_strrcasechr("ABCabcABCabcX123123", 'C'), "cX123123"));
	assert(!strcmpnul(libsimple_strrcasechr("ABCabcABCabcX123123", 'A'), "abcX123123"));
	assert(!strcmpnul(libsimple_strrcasechr("ABCabcABCabcX123123", 'X'), "X123123"));
	assert(!libsimple_strrcasechr("ABCabcABCabcX123123", '0'));
	assert(!strcmpnul(libsimple_strrcasechr("ABCabcABCabcX123123", '1'), "123"));
	return 0;
}

#endif