c7c76a03d2a60f02f1629423d51000c3e931a70c
[reactos.git] / 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 < (int) 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                 d[0] = 0;
243         }
244       else
245         {
246           dst[0] = 0;
247         }
248         return dst;
249 }
250
251
252 char *strrchr(const char *s, int c)
253 {
254         char cc = c;
255         const char *sp=(char *)0;
256
257         while (*s)
258         {
259                 if (*s == cc)
260                         sp = s;
261                 s++;
262         }
263
264         if (cc == 0)
265                 sp = s;
266
267         return (char *)sp;
268 }
269
270
271 size_t strspn(const char *s1, const char *s2)
272 {
273         const char *p = s1, *spanp;
274         char c, sc;
275
276   cont:
277         c = *p++;
278         for (spanp = s2; (sc = *spanp++) != 0;)
279                 if (sc == c)
280                         goto cont;
281
282         return (p - 1 - s1);
283 }
284
285
286 char *strstr(const char *s, const char *find)
287 {
288         char c, sc;
289         size_t len;
290
291         if ((c = *find++) != 0)
292         {
293                 len = strlen(find);
294                 do
295                 {
296                         do
297                         {
298                                 if ((sc = *s++) == 0)
299                                         return 0;
300                         }
301                         while (sc != c);
302                 }
303                 while (strncmp(s, find, len) != 0);
304                 s--;
305         }
306
307         return (char *)s;
308 }