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


void *
libsimple_memreplaceelem(void *restrict s_, const void *old_, const void *new_, size_t width, size_t n)
{
	switch (width) {
	case 0:
		return s_;
	case 1:
		{
			uint8_t *restrict s = s_;
			uint8_t old = *(const uint8_t *)old_;
			uint8_t new = *(const uint8_t *)new_;
			for (; n; s++, n--)
				if (*s == old)
					*s = new;
			return s;
		}
	case 2:
		{
			uint16_t *restrict s = s_;
			uint16_t old = *(const uint16_t *)old_;
			uint16_t new = *(const uint16_t *)new_;
			for (; n; s++, n--)
				if (*s == old)
					*s = new;
			return s;
		}
	case 4:
		{
			uint32_t *restrict s = s_;
			uint32_t old = *(const uint32_t *)old_;
			uint32_t new = *(const uint32_t *)new_;
			for (; n; s++, n--)
				if (*s == old)
					*s = new;
			return s;
		}
	case 8:
		{
			uint64_t *restrict s = s_;
			uint64_t old = *(const uint64_t *)old_;
			uint64_t new = *(const uint64_t *)new_;
			for (; n; s++, n--)
				if (*s == old)
					*s = new;
			return s;
		}
	default:
		{
			char *restrict s = s_;
			const char *old = old_;
			const char *new = new_;
			size_t i;
			for (; n; s += width, n--) {
				if (*s == *old) {
					for (i = 0; i < width; i++)
						if (s[i] != old[i])
							goto next;
					for (i = 0; i < width; i++)
						s[i] = new[i];
				}
			next:;
			}
			return s;
		}
	}
}


#else
#include "test.h"

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

	stpcpy(mempcpy(buf, "hello world", 12), "goodbye world");
	assert(libsimple_memreplaceelem(buf, "o", "x", 0, 46) == &buf[0]);
	assert(!memcmp(buf, "hello world\0goodbye world", 26));

	stpcpy(mempcpy(buf, "hello world", 12), "goodbye world");
	assert(libsimple_memreplaceelem(buf, "o", "x", 0, 12) == &buf[0]);
	assert(!memcmp(buf, "hello world\0goodbye world", 26));


	stpcpy(mempcpy(buf, "hello world", 12), "goodbye world");
	assert(libsimple_memreplaceelem(buf, "o", "x", 1, 46) == &buf[46]);
	assert(!memcmp(buf, "hellx wxrld\0gxxdbye wxrld", 26));

	stpcpy(mempcpy(buf, "hello world", 12), "goodbye world");
	assert(libsimple_memreplaceelem(buf, "o", "x", 1, 12) == &buf[12]);
	assert(!memcmp(buf, "hellx wxrld\0goodbye world", 26));


	stpcpy(mempcpy(buf, "-h-e-l-l-o- -w-o-r-l-d\0", 12 * 2), "-g-o-o-d-b-y-e- -w-o-r-l-d");
	assert(libsimple_memreplaceelem(buf, "-o", "=x", 2, 46) == &buf[46 * 2]);
	assert(!memcmp(buf, "-h-e-l-l=x- -w=x-r-l-d\0\0-g=x=x-d-b-y-e- -w=x-r-l-d", 25 * 2 + 1));

	stpcpy(mempcpy(buf, "-h-e-l-l-o- -w-o-r-l-d\0", 12 * 2), "-g-o-o-d-b-y-e- -w-o-r-l-d");
	assert(libsimple_memreplaceelem(buf, "-o", "=x", 2, 12) == &buf[12 * 2]);
	assert(!memcmp(buf, "-h-e-l-l=x- -w=x-r-l-d\0\0-g-o-o-d-b-y-e- -w-o-r-l-d", 25 * 2 + 1));

	stpcpy(mempcpy(buf, "-h-e-l-l-o- -w-o-r-l-d\0", 12 * 2), "-g-o-o-d-b-y-e- -w-o-r-l-d");
	assert(libsimple_memreplaceelem(buf, "o-", "x=", 2, 12) == &buf[12 * 2]);
	assert(!memcmp(buf, "-h-e-l-l-o- -w-o-r-l-d\0\0-g-o-o-d-b-y-e- -w-o-r-l-d", 25 * 2 + 1));


	stpcpy(mempcpy(buf, "--h--e--l--l--o-- --w--o--r--l--d\0\0", 12 * 3), "--g--o--o--d--b--y--e-- --w--o--r--l--d");
	assert(libsimple_memreplaceelem(buf, "--o", "==x", 3, 46) == &buf[46 * 3]);
	assert(!memcmp(buf, "--h--e--l--l==x-- --w==x--r--l--d\0\0\0--g==x==x--d--b--y--e-- --w==x--r--l--d", 25 * 3 + 1));

	stpcpy(mempcpy(buf, "--h--e--l--l--o-- --w--o--r--l--d\0\0", 12 * 3), "--g--o--o--d--b--y--e-- --w--o--r--l--d");
	assert(libsimple_memreplaceelem(buf, "--o", "==x", 3, 12) == &buf[12 * 3]);
	assert(!memcmp(buf, "--h--e--l--l==x-- --w==x--r--l--d\0\0\0--g--o--o--d--b--y--e-- --w--o--r--l--d", 25 * 3 + 1));

	stpcpy(mempcpy(buf, "--h--e--l--l--o-- --w--o--r--l--d\0\0", 12 * 3), "--g--o--o--d--b--y--e-- --w--o--r--l--d");
	assert(libsimple_memreplaceelem(buf, "o--", "x==", 3, 12) == &buf[12 * 3]);
	assert(!memcmp(buf, "--h--e--l--l--o-- --w--o--r--l--d\0\0\0--g--o--o--d--b--y--e-- --w--o--r--l--d", 25 * 3 + 1));


	stpcpy(mempcpy(buf, "---h---e---l---l---o--- ---w---o---r---l---d\0\0\0", 12 * 4),
	       "---g---o---o---d---b---y---e--- ---w---o---r---l---d");
	assert(libsimple_memreplaceelem(buf, "---o", "===x", 4, 46) == &buf[46 * 4]);
	assert(!memcmp(buf, "---h---e---l---l===x--- ---w===x---r---l---d\0\0\0\0"
	                    "---g===x===x---d---b---y---e--- ---w===x---r---l---d", 25 * 4 + 1));

	stpcpy(mempcpy(buf, "---h---e---l---l---o--- ---w---o---r---l---d\0\0\0", 12 * 4),
	       "---g---o---o---d---b---y---e--- ---w---o---r---l---d");
	assert(libsimple_memreplaceelem(buf, "---o", "===x", 4, 12) == &buf[12 * 4]);
	assert(!memcmp(buf, "---h---e---l---l===x--- ---w===x---r---l---d\0\0\0\0"
	                    "---g---o---o---d---b---y---e--- ---w---o---r---l---d", 25 * 4 + 1));

	stpcpy(mempcpy(buf, "---h---e---l---l---o--- ---w---o---r---l---d\0\0\0", 12 * 4),
	       "---g---o---o---d---b---y---e--- ---w---o---r---l---d");
	assert(libsimple_memreplaceelem(buf, "o---", "x===", 4, 12) == &buf[12 * 4]);
	assert(!memcmp(buf, "---h---e---l---l---o--- ---w---o---r---l---d\0\0\0\0"
	                    "---g---o---o---d---b---y---e--- ---w---o---r---l---d", 25 * 4 + 1));


	stpcpy(mempcpy(buf, "-------h-------e-------l-------l-------o------- -------w-------o-------r-------l-------d\0\0\0\0\0\0\0",
	               12 * 8),
	       "-------g-------o-------o-------d-------b-------y-------e------- -------w-------o-------r-------l-------d");
	assert(libsimple_memreplaceelem(buf, "-------o", "=======x", 8, 46) == &buf[46 * 8]);
	assert(!memcmp(buf, "-------h-------e-------l-------l=======x------- -------w=======x-------r-------l-------d\0\0\0\0\0\0\0\0"
	                    "-------g=======x=======x-------d-------b-------y-------e------- "
	                    "-------w=======x-------r-------l-------d", 25 * 8 + 1));

	stpcpy(mempcpy(buf, "-------h-------e-------l-------l-------o------- -------w-------o-------r-------l-------d\0\0\0\0\0\0\0",
	               12 * 8),
	       "-------g-------o-------o-------d-------b-------y-------e------- -------w-------o-------r-------l-------d");
	assert(libsimple_memreplaceelem(buf, "-------o", "=======x", 8, 12) == &buf[12 * 8]);
	assert(!memcmp(buf, "-------h-------e-------l-------l=======x------- -------w=======x-------r-------l-------d\0\0\0\0\0\0\0\0"
	                    "-------g-------o-------o-------d-------b-------y-------e------- "
	                    "-------w-------o-------r-------l-------d", 25 * 8 + 1));

	stpcpy(mempcpy(buf, "-------h-------e-------l-------l-------o------- -------w-------o-------r-------l-------d\0\0\0\0\0\0\0",
	               12 * 8),
	       "-------g-------o-------o-------d-------b-------y-------e------- -------w-------o-------r-------l-------d");
	assert(libsimple_memreplaceelem(buf, "o-------", "x=======", 8, 12) == &buf[12 * 8]);
	assert(!memcmp(buf, "-------h-------e-------l-------l-------o------- -------w-------o-------r-------l-------d\0\0\0\0\0\0\0\0"
	                    "-------g-------o-------o-------d-------b-------y-------e------- "
	                    "-------w-------o-------r-------l-------d", 25 * 8 + 1));

	return 0;
}

#endif