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