d595991ae618ce5f57957ee5336df0961efd7472
[reactos.git] / reactos / ntoskrnl / rtl / string.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: ntoskrnl/rtl/string.c
5 * PURPOSE: Ascii string functions
6 * PROGRAMMER: Eric Kohl (ekohl@abo.rhein-zeitung.de)
7 * UPDATE HISTORY:
8 * 1999/07/29 ekohl Created
9 */
10
11 /* INCLUDES *****************************************************************/
12
13 #include <ctype.h>
14 #include <string.h>
15
16 /* FUNCTIONS *****************************************************************/
17
18 int _stricmp(const char *s1, const char *s2)
19 {
20 while (toupper(*s1) == toupper(*s2))
21 {
22 if (*s1 == 0)
23 return 0;
24 s1++;
25 s2++;
26 }
27 return toupper(*(unsigned const char *)s1) - toupper(*(unsigned const char *)(s2));
28 }
29
30
31 char * _strlwr(char *x)
32 {
33 char *y=x;
34
35 while (*y)
36 {
37 *y=tolower(*y);
38 y++;
39 }
40 return x;
41 }
42
43
44 int _strnicmp(const char *s1, const char *s2, size_t n)
45 {
46 if (n == 0)
47 return 0;
48 do
49 {
50 if (toupper(*s1) != toupper(*s2++))
51 return toupper(*(unsigned const char *)s1) - toupper(*(unsigned const char *)--s2);
52 if (*s1++ == 0)
53 break;
54 }
55 while (--n != 0);
56 return 0;
57 }
58
59 char* _strnset(char* szToFill, int szFill, size_t sizeMaxFill)
60 {
61 char *t = szToFill;
62 int i = 0;
63 while (*szToFill != 0 && i < sizeMaxFill)
64 {
65 *szToFill = szFill;
66 szToFill++;
67 i++;
68
69 }
70 return t;
71 }
72
73
74 char * _strrev(char *s)
75 {
76 char *e;
77 char a;
78
79 e = s;
80 while (*e)
81 e++;
82
83 while (s<e)
84 {
85 a = *s;
86 *s = *e;
87 *e = a;
88 s++;
89 e--;
90 }
91 return s;
92 }
93
94
95 char* _strset(char* szToFill, int szFill)
96 {
97 char *t = szToFill;
98 while (*szToFill != 0)
99 {
100 *szToFill = szFill;
101 szToFill++;
102
103 }
104 return t;
105 }
106
107
108 char *_strupr(char *x)
109 {
110 char *y=x;
111
112 while (*y)
113 {
114 *y=toupper(*y);
115 y++;
116 }
117 return x;
118 }
119
120
121 char *strcat(char *s, const char *append)
122 {
123 char *save = s;
124
125 for (; *s; ++s);
126 while ((*s++ = *append++));
127 return save;
128 }
129
130
131 char *strchr(const char *s, int c)
132 {
133 char cc = c;
134
135 while (*s)
136 {
137 if (*s == cc)
138 return (char *)s;
139 s++;
140 }
141
142 if (cc == 0)
143 return (char *)s;
144
145 return 0;
146 }
147
148
149 int strcmp(const char *s1, const char *s2)
150 {
151 while (*s1 == *s2)
152 {
153 if (*s1 == 0)
154 return 0;
155 s1++;
156 s2++;
157 }
158
159 return *(unsigned const char *)s1 - *(unsigned const char *)(s2);
160 }
161
162
163 char* strcpy(char *to, const char *from)
164 {
165 char *save = to;
166
167 for (; (*to = *from); ++from, ++to);
168
169 return save;
170 }
171
172
173 size_t strlen(const char *str)
174 {
175 const char *s;
176
177 if (str == 0)
178 return 0;
179 for (s = str; *s; ++s);
180
181 return s-str;
182 }
183
184
185 char *strncat(char *dst, const char *src, size_t n)
186 {
187 if (n != 0)
188 {
189 char *d = dst;
190 const char *s = src;
191
192 while (*d != 0)
193 d++;
194 do
195 {
196 if ((*d = *s++) == 0)
197 break;
198 d++;
199 }
200 while (--n != 0);
201 *d = 0;
202 }
203
204 return dst;
205 }
206
207
208 int strncmp(const char *s1, const char *s2, size_t n)
209 {
210 if (n == 0)
211 return 0;
212 do
213 {
214 if (*s1 != *s2++)
215 return *(unsigned const char *)s1 - *(unsigned const char *)--s2;
216 if (*s1++ == 0)
217 break;
218 }
219 while (--n != 0);
220
221 return 0;
222 }
223
224
225 char *strncpy(char *dst, const char *src, size_t n)
226 {
227 if (n != 0)
228 {
229 char *d = dst;
230 const char *s = src;
231
232 do
233 {
234 if ((*d++ = *s++) == 0)
235 {
236 while (--n != 0)
237 *d++ = 0;
238 break;
239 }
240 }
241 while (--n != 0);
242 }
243
244 return dst;
245 }
246
247
248 char *strrchr(const char *s, int c)
249 {
250 char cc = c;
251 const char *sp=(char *)0;
252
253 while (*s)
254 {
255 if (*s == cc)
256 sp = s;
257 s++;
258 }
259
260 if (cc == 0)
261 sp = s;
262
263 return (char *)sp;
264 }
265
266
267 size_t strspn(const char *s1, const char *s2)
268 {
269 const char *p = s1, *spanp;
270 char c, sc;
271
272 cont:
273 c = *p++;
274 for (spanp = s2; (sc = *spanp++) != 0;)
275 if (sc == c)
276 goto cont;
277
278 return (p - 1 - s1);
279 }
280
281
282 char *strstr(const char *s, const char *find)
283 {
284 char c, sc;
285 size_t len;
286
287 if ((c = *find++) != 0)
288 {
289 len = strlen(find);
290 do
291 {
292 do
293 {
294 if ((sc = *s++) == 0)
295 return 0;
296 }
297 while (sc != c);
298 }
299 while (strncmp(s, find, len) != 0);
300 s--;
301 }
302
303 return (char *)s;
304 }