aboutsummaryrefslogtreecommitdiffstats
path: root/memelem.c
blob: 3344cd8d4c0d99d193acfe6ea55be3c97339cf60 (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/* See LICENSE file for copyright and license details. */
#include "libsimple.h"
#ifndef TEST


void *
libsimple_memelem(const void *hay_, const void *sub_, size_t width, size_t n)
{
	switch (width) {
	case 0:
		return REMOVE_CONST(hay_, void *);
	case 1:
		return memchr(hay_, *(const char *)sub_, n);
	case 2:
		{
			const uint16_t *hay = hay_;
			uint16_t sub = *(const uint16_t *)sub_;
			for (; n--; hay++)
				if (*hay == sub)
					return REMOVE_CONST(hay, uint16_t *);
			break;
		}
	case 4:
		{
			const uint32_t *hay = hay_;
			uint32_t sub = *(const uint32_t *)sub_;
			for (; n--; hay++)
				if (*hay == sub)
					return REMOVE_CONST(hay, uint32_t *);
			break;
		}
	case 8:
		{
			const uint64_t *hay = hay_;
			uint64_t sub = *(const uint64_t *)sub_;
			for (; n--; hay++)
				if (*hay == sub)
					return REMOVE_CONST(hay, uint64_t *);
			break;
		}
	default:
		{
			const char *hay = hay_;
			const char *sub = sub_;
			size_t i;
			for (; n--; hay += width) {
				for (i = 0; i < width; i++)
					if (hay[i] != sub[i])
						goto next;
				return REMOVE_CONST(hay, char *);
			next:;
			}
			break;
		}
	}

	return NULL;
}


#else
#include "test.h"

int
main(void)
{
	assert(!strcmpnul(libsimple_memelem("12345634", "", 0, 8), "12345634"));
	assert(!strcmpnul(libsimple_memelem("12345634", "", 0, 0), "12345634"));

	assert(!strcmpnul(libsimple_memelem("12345634", "3", 1, 8), "345634"));
	assert(!libsimple_memelem("12345634", "x", 1, 8));
	assert(!strcmpnul(libsimple_memelem("13456342", "3", 1, 8), "3456342"));
	assert(!libsimple_memelem("12345634", "3", 1, 0));

	assert(!strcmpnul(libsimple_memelem("12345634", "34", 2, 4), "345634"));
	assert(!libsimple_memelem("12345634", "xx", 2, 4));
	assert(!libsimple_memelem("13456342", "34", 2, 4));
	assert(!libsimple_memelem("12345634", "34", 2, 0));

	assert(!strcmpnul(libsimple_memelem("abcd1234abcd1234", "1234", 4, 4), "1234abcd1234"));
	assert(!libsimple_memelem("abcd1234abcd1234", "zzzz", 4, 4));
	assert(!libsimple_memelem("cd1234abcd1234ab", "1234", 4, 4));
	assert(!libsimple_memelem("abcd1234abcd1234", "1234", 4, 0));

	assert(!strcmpnul(libsimple_memelem("abcdefgh12345678abcdefgh12345678", "12345678", 8, 4), "12345678abcdefgh12345678"));
	assert(!libsimple_memelem("abcdefgh12345678abcdefgh12345678", "zzzzzzzz", 8, 4));
	assert(!libsimple_memelem("efgh12345678abcdefgh12345678abcd", "12345678", 8, 4));
	assert(!libsimple_memelem("abcdefgh12345678abcdefgh12345678", "12345678", 8, 0));

	assert(!strcmpnul(libsimple_memelem("abc123abc123", "123", 3, 4), "123abc123"));
	assert(!libsimple_memelem("abc123abc123", "zzz", 3, 4));
	assert(!libsimple_memelem("bc123abc123a", "123", 3, 4));
	assert(!libsimple_memelem("abc123abc123", "123", 3, 0));

	assert(!strcmpnul(libsimple_memelem("-a-aa--a", "a-", 2, 4), "a--a"));
	assert(!strcmpnul(libsimple_memelem("--a--aa----a", "a--", 3, 4), "a----a"));
	assert(!strcmpnul(libsimple_memelem("---a---aa------a", "a---", 4, 4), "a------a"));
	assert(!strcmpnul(libsimple_memelem("-------a-------aa--------------a", "a-------", 8, 4), "a--------------a"));

	return 0;
}

#endif