bootstrap
[reactos.git] / ntoskrnl / rtl / wstring.c
1 /*
2  * COPYRIGHT:       See COPYING in the top level directory
3  * PROJECT:         ReactOS kernel
4  * FILE:            ntoskrnl/rtl/wstring.c
5  * PURPOSE:         Wide string functions
6  * PROGRAMMER:      David Welch (welch@cwcom.net)
7  * UPDATE HISTORY:
8  *                  Created 22/05/98
9  *   1998/12/04  RJJ    Cleaned up and added i386 def checks.
10  *   1999/07/29  ekohl  Added missing functions.
11  */
12
13 /* INCLUDES *****************************************************************/
14
15 #include <ddk/ntddk.h>
16
17 #define NDEBUG
18 #include <internal/debug.h>
19
20 /* FUNCTIONS *****************************************************************/
21
22 int _wcsicmp (const wchar_t* cs, const wchar_t* ct)
23 {
24         while (*cs != '\0' && *ct != '\0' && towupper(*cs) == towupper(*ct))
25         {
26                 cs++;
27                 ct++;
28         }
29         return *cs - *ct;
30 }
31
32 #ifndef LIBCAPTIVE
33
34 wchar_t *_wcslwr (wchar_t *x)
35 {
36         wchar_t  *y=x;
37
38         while (*y)
39         {
40                 *y=towlower(*y);
41                 y++;
42         }
43         return x;
44 }
45
46
47 int _wcsnicmp (const wchar_t * cs,const wchar_t * ct,size_t count)
48 {
49         if (count == 0)
50                 return 0;
51         do {
52                 if (towupper(*cs) != towupper(*ct++))
53                         return towupper(*cs) - towupper(*--ct);
54                 if (*cs++ == 0)
55                         break;
56         } while (--count != 0);
57         return 0;
58 }
59
60
61 wchar_t *_wcsnset (wchar_t* wsToFill, wchar_t wcFill, size_t sizeMaxFill)
62 {
63         wchar_t *t = wsToFill;
64         int i = 0;
65         while( *wsToFill != 0 && i < sizeMaxFill)
66         {
67                 *wsToFill = wcFill;
68                 wsToFill++;
69                 i++;
70         }
71         return t;
72 }
73
74
75 wchar_t *_wcsrev(wchar_t *s)
76 {
77         wchar_t  *e;
78         wchar_t   a;
79         e=s;
80         while (*e)
81                 e++;
82         while (s<e)
83         {
84                 a=*s;
85                 *s=*e;
86                 *e=a;
87                 s++;
88                 e--;
89         }
90         return s;
91 }
92
93
94 wchar_t *_wcsupr(wchar_t *x)
95 {
96         wchar_t *y=x;
97
98         while (*y)
99         {
100                 *y=towupper(*y);
101                 y++;
102         }
103         return x;
104 }
105
106 #endif /* LIBCAPTIVE */
107
108 wchar_t * wcscat(wchar_t *dest, const wchar_t *src)
109 {
110   int i, j;
111    
112   for (j = 0; dest[j] != 0; j++)
113     ;
114   for (i = 0; src[i] != 0; i++)
115     {
116       dest[j + i] = src[i];
117     }
118   dest[j + i] = 0;
119
120   return dest;
121 }
122
123 wchar_t * wcschr(const wchar_t *str, wchar_t ch)
124 {
125   while ((*str) != ((wchar_t) 0))
126     {
127       if ((*str) == ch)
128         {
129           return (wchar_t *) str;
130         }
131       str++;
132     }
133
134   return NULL;
135 }
136
137 #ifndef LIBCAPTIVE
138
139 int wcscmp(const wchar_t *cs, const wchar_t *ct)
140 {
141   while (*cs != '\0' && *ct != '\0' && *cs == *ct)
142     {
143       cs++;
144       ct++;
145     }
146   return *cs - *ct;
147 }
148
149 #endif /* LIBCAPTIVE */
150
151 wchar_t* wcscpy(wchar_t* str1, const wchar_t* str2)
152 {
153    wchar_t* s = str1;
154    DPRINT("wcscpy(str1 %S, str2 %S)\n",str1,str2);
155    while ((*str2)!=0)
156      {
157         *s = *str2;
158         s++;
159         str2++;
160      }
161    *s = 0;
162    return(str1);
163 }
164
165 #ifndef LIBCAPTIVE
166
167 size_t wcscspn(const wchar_t *str,const wchar_t *reject)
168 {
169         wchar_t *s;
170         wchar_t *t;
171         s=(wchar_t *)str;
172         do {
173                 t=(wchar_t *)reject;
174                 while (*t) { 
175                         if (*t==*s) 
176                                 break;
177                         t++;
178                 }
179                 if (*t) 
180                         break;
181                 s++;
182         } while (*s);
183         return s-str; /* nr of wchars */
184 }
185
186 #endif /* LIBCAPTIVE */
187
188 size_t wcslen(const wchar_t *s)
189 {
190   unsigned int len = 0;
191
192   while (s[len] != 0) 
193     {
194       len++;
195     }
196
197   return len;
198 }
199
200 #ifndef LIBCAPTIVE
201
202 wchar_t * wcsncat(wchar_t *dest, const wchar_t *src, size_t count)
203 {
204   int i, j;
205    
206   for (j = 0; dest[j] != 0; j++)
207     ;
208   for (i = 0; i < count; i++)
209     {
210       dest[j + i] = src[i];
211       if (src[i] == 0)
212         {
213           return dest;
214         }
215     }
216   dest[j + i] = 0;
217
218   return dest;
219 }
220
221
222 int wcsncmp(const wchar_t *cs, const wchar_t *ct, size_t count)
223 {
224   while (*cs != '\0' && *ct != '\0' && *cs == *ct && --count)
225     {
226       cs++;
227       ct++;
228     }
229   return *cs - *ct;
230 }
231
232
233 wchar_t *wcsncpy(wchar_t *dest, const wchar_t *src, size_t count)
234 {
235   int i;
236    
237   for (i = 0; i < count; i++)
238     {
239       dest[i] = src[i];
240       if (src[i] == 0)
241         {
242           return dest;
243         }
244     }
245   dest[i] = 0;
246
247   return dest;
248 }
249
250 #endif /* LIBCAPTIVE */
251
252 wchar_t *wcsrchr(const wchar_t *str, wchar_t ch)
253 {
254   unsigned int len = 0;
255   while (str[len] != ((wchar_t)0))
256     {
257       len++;
258     }
259    
260   for (; len > 0; len--)
261     {
262       if (str[len-1]==ch)
263         {
264           return (wchar_t *) &str[len - 1];
265         }
266     }
267
268   return NULL;
269 }
270
271 #ifndef LIBCAPTIVE
272
273 size_t wcsspn(const wchar_t *str,const wchar_t *accept)
274 {
275         wchar_t  *s;
276         wchar_t  *t;
277         s=(wchar_t *)str;
278         do
279         {
280                 t=(wchar_t *)accept;
281                 while (*t)
282                 {
283                         if (*t==*s)
284                                 break;
285                         t++;
286                 }
287                 if (!*t)
288                         break;
289                 s++;
290         } while (*s);
291         return s-str; /* nr of wchars */
292 }
293
294
295 wchar_t *wcsstr(const wchar_t *s,const wchar_t *b)
296 {
297         wchar_t *x;
298         wchar_t *y;
299         wchar_t *c;
300         x=(wchar_t *)s;
301         while (*x)
302         {
303                 if (*x==*b)
304                 {
305                         y=x;
306                         c=(wchar_t *)b;
307                         while (*y && *c && *y==*c)
308                         {
309                                 c++;
310                                 y++;
311                         }
312                         if (!*c)
313                                 return x;
314                 }
315                 x++;
316         }
317         return NULL;
318 }
319
320 #endif /* LIBCAPTIVE */