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
|
#ifndef _STRING_H
#define _STRING_H
#include <stdc_common.h>
void *memmove(void *s1, const void *s2, size_t n) {
if (s1 < s2) return memcpy(s1, s2, n); // our memcpy does a forwards copy
// backwards copy
char *p = (char*)s1 + n, *q = (char*)s2 + n;
while (p > s1)
*--p = *--q;
return s1;
}
char *strcpy(char *s1, const char *s2) {
char *p = s1 - 1, *q = s2 - 1;
while ((*++p = *++q));
return s1;
}
char *strncpy(char *s1, const char *s2, size_t n) {
char *p = s1 - 1, *q = s2 - 1;
size_t i;
for (i = 0; i < n; ++i)
if (!(*++p = *++q))
break;
for (; i < n; ++i)
*++p = 0;
return s1;
}
char *strcat(char *s1, const char *s2) {
return strcpy(s1 + strlen(s1), s2);
}
char *strncat(char *s1, const char *s2, size_t n) {
// oddly, not equivalent to strncpy(s1 + strlen(s1), s2, n)
char *p = s1 + strlen(s1) - 1, *q = s2 - 1;
size_t i;
for (i = 0; i < n; ++i)
if (!(*++p = *++q))
break;
*++p = 0;
return s1;
}
int memcmp(const void *s1, const void *s2, size_t n) {
char *p = s1, *q = s2;
size_t i;
for (i = 0; i < n; ++i, ++p, ++q) {
if (*p > *q)
return 1;
if (*p < *q)
return -1;
}
return 0;
}
int strcmp(const char *s1, const char *s2) {
char *p = s1, *q = s2;
for (; ; ++p, ++q) {
if (*p > *q)
return 1;
if (*p < *q)
return -1;
if (!*p) break;
}
return 0;
}
int strcoll(const char *s1, const char *s2) {
// we only support the C locale
return strcmp(s1, s2);
}
int strncmp(const char *s1, const char *s2, size_t n) {
char *p = s1, *q = s2;
size_t i;
for (i = 0; i < n; ++i, ++p, ++q) {
if (*p > *q)
return 1;
if (*p < *q)
return -1;
if (!*p) break;
}
return 0;
}
size_t strxfrm(char *s1, const char *s2, size_t n) {
// we only support the C locale
size_t l = strlen(s2);
if (l >= n) return l;
strcpy(s1, s2);
return l;
}
void *memchr(const void *s, int c, size_t n) {
char *p = s, *end = p + n;
while (p < end) {
if ((unsigned char)*p == c)
return p;
++p;
}
return NULL;
}
char *strchr(const char *s, int c) {
return memchr(s, c, strlen(s)+1);
}
size_t strcspn(const char *s1, const char *s2) {
const char *p, *q;
for (p = s1; *p; ++p) {
for (q = s2; *q; ++q) {
if (*p == *q)
goto ret;
}
}
ret:
return p - s1;
}
char *strpbrk(const char *s1, const char *s2) {
const char *p, *q;
for (p = s1; *p; ++p) {
for (q = s2; *q; ++q) {
if (*p == *q)
return p;
}
}
return NULL;
}
char *strrchr(const char *s, int c) {
char *p;
for (p = s + strlen(s); p >= s; --p) {
if (*p == c)
return p;
}
return NULL;
}
size_t strspn(const char *s1, const char *s2) {
const char *p, *q;
for (p = s1; *p; ++p) {
for (q = s2; *q; ++q) {
if (*p == *q) break;
}
if (!*q) break;
}
return p - s1;
}
char *strstr(const char *s1, const char *s2) {
char *p;
size_t l = strlen(s2);
for (p = s1; *p; ++p) {
if (memcmp(p, s2, l) == 0)
return p;
}
return NULL;
}
char *strtok(char *s1, const char *s2) {
static char *str;
if (s1) str = s1;
if (!str) return NULL;
char *p = str + strspn(str, s2);
if (!*p) {
str = NULL;
return NULL;
}
char *q = strpbrk(p, s2);
if (q) {
*q = 0;
str = q + 1;
} else {
str = NULL;
}
return p;
}
#endif // _STRING_H
|