aboutsummaryrefslogtreecommitdiffstats
path: root/memelemcpy.c
blob: 19c937c53d4ab13cb17f4eadcf664621b7a355dc (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/* See LICENSE file for copyright and license details. */
#include "common.h"
#ifndef TEST


static inline uint8_t *
memelemcpy8(uint8_t *restrict d, const uint8_t *restrict s, uint8_t elem, size_t n)
{
	for (; n; s++, n--)
		if ((*d++ = *s) == elem)
			return d;
	return NULL;
}


static inline uint16_t *
memelemcpy16(uint16_t *restrict d, const uint16_t *restrict s, uint16_t elem, size_t n)
{
	for (; n; s++, n--)
		if ((*d++ = *s) == elem)
			return d;
	return NULL;
}


static inline uint32_t *
memelemcpy32(uint32_t *restrict d, const uint32_t *restrict s, uint32_t elem, size_t n)
{
	for (; n; s++, n--)
		if ((*d++ = *s) == elem)
			return d;
	return NULL;
}


static inline uint64_t *
memelemcpy64(uint64_t *restrict d, const uint64_t *restrict s, uint64_t elem, size_t n)
{
	for (; n; s++, n--)
		if ((*d++ = *s) == elem)
			return d;
	return NULL;
}


static inline char *
memelemcpyx(char *restrict d, const char *restrict s, const char *restrict elem, size_t size, size_t n)
{
	size_t i;
	for (; n; s += size, n--) {
		for (i = 0; i < size; i++)
			d[i] = s[i];
		for (i = 0; i < size; i++)
			if (d[i] != elem[i])
				goto next;
		d += size;
		return d;
	next:
		d += size;
	}
	return NULL;
}


void *
libsimple_memelemcpy(void *restrict d, const void *restrict s, const void *restrict elem, size_t size, size_t n)
{
	switch (size) {
	case 0:
		return d;
	case 1:
		return memelemcpy8(d, s, *(const uint8_t *)elem, n);
	case 2:
		return memelemcpy16(d, s, *(const uint16_t *)elem, n);
	case 4:
		return memelemcpy32(d, s, *(const uint32_t *)elem, n);
	case 8:
		return memelemcpy64(d, s, *(const uint64_t *)elem, n);
	default:
		return memelemcpyx(d, s, elem, size, n);
	}
}


#else
#include "test.h"

int
main(void)
{
	char buf[1024];


	memset(buf, 'x', sizeof(buf));
	buf[sizeof(buf) - 1] = '\0';
	assert(libsimple_memelemcpy(buf, "hello", "o", 0, 5) == &buf[0]);
	assert(!strncmp(buf, "x", 1));

	memset(buf, 'x', sizeof(buf));
	buf[sizeof(buf) - 1] = '\0';
	assert(libsimple_memelemcpy(buf, "hello", "l", 0, 5) == &buf[0]);
	assert(!strncmp(buf, "x", 1));

	memset(buf, 'x', sizeof(buf));
	buf[sizeof(buf) - 1] = '\0';
	assert(libsimple_memelemcpy(buf, "hello", "x", 0, 5) == &buf[0]);
	assert(!strncmp(buf, "x", 1));


	memset(buf, 'x', sizeof(buf));
	buf[sizeof(buf) - 1] = '\0';
	assert(libsimple_memelemcpy(buf, "hello", "o", 1, 5) == &buf[5]);
	assert(!strncmp(buf, "hellox", 6));

	memset(buf, 'x', sizeof(buf));
	buf[sizeof(buf) - 1] = '\0';
	assert(libsimple_memelemcpy(buf, "hello", "l", 1, 5) == &buf[3]);
	assert(!strncmp(buf, "helx", 4));

	memset(buf, 'x', sizeof(buf));
	buf[sizeof(buf) - 1] = '\0';
	assert(libsimple_memelemcpy(buf, "hello", "x", 1, 5) == NULL);
	assert(!strncmp(buf, "hellox", 6));


	memset(buf, 'x', sizeof(buf));
	buf[sizeof(buf) - 1] = '\0';
	assert(libsimple_memelemcpy(buf, ".h.e.l.l.o", ".o", 2, 5) == &buf[5 * 2]);
	assert(!strncmp(buf, ".h.e.l.l.ox", 11));

	memset(buf, 'x', sizeof(buf));
	buf[sizeof(buf) - 1] = '\0';
	assert(libsimple_memelemcpy(buf, ".h.e.l.l.o", ".l", 2, 5) == &buf[3 * 2]);
	assert(!strncmp(buf, ".h.e.lx", 7));

	memset(buf, 'x', sizeof(buf));
	buf[sizeof(buf) - 1] = '\0';
	assert(libsimple_memelemcpy(buf, ".h.e.l.l.o", ".x", 2, 5) == NULL);
	assert(!strncmp(buf, ".h.e.l.l.ox", 11));

	memset(buf, 'x', sizeof(buf));
	buf[sizeof(buf) - 1] = '\0';
	assert(libsimple_memelemcpy(buf, ".h.l.ll..o", "l.", 2, 5) == &buf[4 * 2]);
	assert(!strncmp(buf, ".h.l.ll.x", 9));


	memset(buf, 'x', sizeof(buf));
	buf[sizeof(buf) - 1] = '\0';
	assert(libsimple_memelemcpy(buf, "..h..e..l..l..o", "..o", 3, 5) == &buf[5 * 3]);
	assert(!strncmp(buf, "..h..e..l..l..ox", 16));

	memset(buf, 'x', sizeof(buf));
	buf[sizeof(buf) - 1] = '\0';
	assert(libsimple_memelemcpy(buf, "..h..e..l..l..o", "..l", 3, 5) == &buf[3 * 3]);
	assert(!strncmp(buf, "..h..e..lx", 10));

	memset(buf, 'x', sizeof(buf));
	buf[sizeof(buf) - 1] = '\0';
	assert(libsimple_memelemcpy(buf, "..h..e..l..l..o", "..x", 3, 5) == NULL);
	assert(!strncmp(buf, "..h..e..l..l..ox", 16));

	memset(buf, 'x', sizeof(buf));
	buf[sizeof(buf) - 1] = '\0';
	assert(libsimple_memelemcpy(buf, "..h..l..ll....o", "l..", 3, 5) == &buf[4 * 3]);
	assert(!strncmp(buf, "..h..l..ll..x", 13));


	memset(buf, 'x', sizeof(buf));
	buf[sizeof(buf) - 1] = '\0';
	assert(libsimple_memelemcpy(buf, "...h...e...l...l...o", "...o", 4, 5) == &buf[5 * 4]);
	assert(!strncmp(buf, "...h...e...l...l...ox", 21));

	memset(buf, 'x', sizeof(buf));
	buf[sizeof(buf) - 1] = '\0';
	assert(libsimple_memelemcpy(buf, "...h...e...l...l...o", "...l", 4, 5) == &buf[3 * 4]);
	assert(!strncmp(buf, "...h...e...lx", 13));

	memset(buf, 'x', sizeof(buf));
	buf[sizeof(buf) - 1] = '\0';
	assert(libsimple_memelemcpy(buf, "...h...e...l...l...o", "...x", 4, 5) == NULL);
	assert(!strncmp(buf, "...h...e...l...l...ox", 21));

	memset(buf, 'x', sizeof(buf));
	buf[sizeof(buf) - 1] = '\0';
	assert(libsimple_memelemcpy(buf, "...h...l...ll......o", "l...", 4, 5) == &buf[4 * 4]);
	assert(!strncmp(buf, "...h...l...ll...x", 17));


	memset(buf, 'x', sizeof(buf));
	buf[sizeof(buf) - 1] = '\0';
	assert(libsimple_memelemcpy(buf, ".......h.......e.......l.......l.......o", ".......o", 8, 5) == &buf[5 * 8]);
	assert(!strncmp(buf, ".......h.......e.......l.......l.......ox", 21));

	memset(buf, 'x', sizeof(buf));
	buf[sizeof(buf) - 1] = '\0';
	assert(libsimple_memelemcpy(buf, ".......h.......e.......l.......l.......o", ".......l", 8, 5) == &buf[3 * 8]);
	assert(!strncmp(buf, ".......h.......e.......lx", 13));

	memset(buf, 'x', sizeof(buf));
	buf[sizeof(buf) - 1] = '\0';
	assert(libsimple_memelemcpy(buf, ".......h.......e.......l.......l.......o", ".......x", 8, 5) == NULL);
	assert(!strncmp(buf, ".......h.......e.......l.......l.......ox", 21));

	memset(buf, 'x', sizeof(buf));
	buf[sizeof(buf) - 1] = '\0';
	assert(libsimple_memelemcpy(buf, ".......h.......l.......ll..............o", "l.......", 8, 5) == &buf[4 * 8]);
	assert(!strncmp(buf, ".......h.......l.......ll.......x", 21));


	return 0;
}

#endif