aboutsummaryrefslogtreecommitdiffstats
path: root/strncasechrnul.c
blob: 608f097bd7776975bee43bbc8fee01af44786279 (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
/* See LICENSE file for copyright and license details. */
#include "libsimple.h"
#ifndef TEST


char *
libsimple_strncasechrnul(const char *s_, int c, size_t n)
{
	char *s = *(char **)(void *)&s_;
	char *end = &s[n];
	c = tolower(c);
	for (; s != end && *s && tolower(*s) != c; s++);
	return s;
}


#else
#include "test.h"

int
main(void)
{
	assert(!strcmpnul(libsimple_strncasechrnul("abcABCabcABC", 'a', 13), "abcABCabcABC"));
	assert(!strcmpnul(libsimple_strncasechrnul("abcABCabcABC", 'c', 13), "cABCabcABC"));
	assert(!strcmpnul(libsimple_strncasechrnul("abcABCabcABC", 'A', 13), "abcABCabcABC"));
	assert(!strcmpnul(libsimple_strncasechrnul("abcABCabcABC", 'C', 13), "cABCabcABC"));
	assert(!strcmpnul(libsimple_strncasechrnul("ABCabcABCabc", 'a', 13), "ABCabcABCabc"));
	assert(!strcmpnul(libsimple_strncasechrnul("ABCabcABCabc", 'c', 13), "CabcABCabc"));
	assert(!strcmpnul(libsimple_strncasechrnul("ABCabcABCabc", 'A', 13), "ABCabcABCabc"));
	assert(!strcmpnul(libsimple_strncasechrnul("ABCabcABCabc", 'C', 13), "CabcABCabc"));
	assert(!strcmpnul(libsimple_strncasechrnul("abcABCabcABC", 'x', 13), ""));
	assert(!strcmpnul(libsimple_strncasechrnul("abcABCabcABC", 'x', 20), ""));
	assert(!strcmpnul(libsimple_strncasechrnul("abcABCabcABC", '\0', 13), ""));
	assert(!strcmpnul(libsimple_strncasechrnul("abcABCabcABC", '\0', 20), ""));
	assert(!strcmpnul(libsimple_strncasechrnul("abcABCabcABC", '\0', 3), "ABCabcABC"));
	assert(!strcmpnul(libsimple_strncasechrnul("abcdef", 'd', 3), "def"));
	assert(!strcmpnul(libsimple_strncasechrnul("abcdef", 'e', 3), "def"));
	assert(!strcmpnul(libsimple_strncasechrnul("123123", '1', 7), "123123"));
	assert(!strcmpnul(libsimple_strncasechrnul("123123", '3', 7), "3123"));
	return 0;
}

#endif