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
|
/* See LICENSE file for copyright and license details. */
#include "libsimple.h"
#ifndef TEST
char *
libsimple_strcmove(char *d, const char *s, int c_)
{
char c = (char)c_, *p;
size_t n;
if (d <= s) {
do {
if ((*d++ = *s) == c) {
*d = '\0';
return d;
}
} while (*s++);
} else {
for (p = *(char **)(void *)&s;; p++) {
if (*p == c || !*p) {
n = (size_t)(p - s);
p = &d[n];
do { d[n] = s[n]; } while (n--);
if (*p)
p[1] = '\0';
return *p == c ? &p[1] : NULL;
}
}
}
return NULL;
}
#else
#include "test.h"
int
main(void)
{
char buf[1024];
memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
strcpy(&buf[5], "hello");
assert(libsimple_strcmove(&buf[5], &buf[5], '\0') == &buf[5 + 6]);
assert(!strcmp(buf, "-----hello"));
memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
strcpy(&buf[5], "hello");
assert(libsimple_strcmove(&buf[5], &buf[5], 'o') == &buf[5 + 5]);
assert(!strcmp(buf, "-----hello"));
memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
strcpy(&buf[5], "hello");
assert(libsimple_strcmove(&buf[5], &buf[5], 'l') == &buf[5 + 3]);
assert(!strcmp(buf, "-----hel"));
memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
strcpy(&buf[5], "hello");
assert(libsimple_strcmove(&buf[5], &buf[5], 'x') == NULL);
assert(!strcmp(buf, "-----hello"));
memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
strcpy(&buf[5], "hello");
assert(libsimple_strcmove(&buf[3], &buf[5], '\0') == &buf[3 + 6]);
assert(!strcmp(buf, "---hello"));
memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
strcpy(&buf[5], "hello");
assert(libsimple_strcmove(&buf[3], &buf[5], 'o') == &buf[3 + 5]);
assert(!strcmp(buf, "---hello"));
memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
strcpy(&buf[5], "hello");
assert(libsimple_strcmove(&buf[3], &buf[5], 'l') == &buf[3 + 3]);
assert(!strcmp(buf, "---hel"));
memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
strcpy(&buf[5], "hello");
assert(libsimple_strcmove(&buf[3], &buf[5], 'x') == NULL);
assert(!strcmp(buf, "---hello"));
memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
strcpy(&buf[5], "hello");
assert(libsimple_strcmove(&buf[8], &buf[5], '\0') == &buf[8 + 6]);
assert(!strcmp(buf, "-----helhello"));
memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
strcpy(&buf[5], "hello");
assert(libsimple_strcmove(&buf[8], &buf[5], 'o') == &buf[8 + 5]);
assert(!strcmp(buf, "-----helhello"));
memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
strcpy(&buf[5], "hello");
assert(libsimple_strcmove(&buf[8], &buf[5], 'l') == &buf[8 + 3]);
assert(!strcmp(buf, "-----helhel"));
memset(buf, '-', sizeof(buf)), buf[sizeof(buf) - 1] = '\0';
strcpy(&buf[5], "hello");
assert(libsimple_strcmove(&buf[8], &buf[5], 'x') == NULL);
assert(!strcmp(buf, "-----helhello"));
return 0;
}
#endif
|