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 wchar_t *_wcslwr (wchar_t *x)
34 {
35         wchar_t  *y=x;
36
37         while (*y)
38         {
39                 *y=towlower(*y);
40                 y++;
41         }
42         return x;
43 }
44
45
46 int _wcsnicmp (const wchar_t * cs,const wchar_t * ct,size_t count)
47 {
48         if (count == 0)
49                 return 0;
50         do {
51                 if (towupper(*cs) != towupper(*ct++))
52                         return towupper(*cs) - towupper(*--ct);
53                 if (*cs++ == 0)
54                         break;
55         } while (--count != 0);
56         return 0;
57 }
58
59
60 wchar_t *_wcsnset (wchar_t* wsToFill, wchar_t wcFill, size_t sizeMaxFill)
61 {
62         wchar_t *t = wsToFill;
63         int i = 0;
64         while( *wsToFill != 0 && i < (int) sizeMaxFill)
65         {
66                 *wsToFill = wcFill;
67                 wsToFill++;
68                 i++;
69         }
70         return t;
71 }
72
73
74 wchar_t *_wcsrev(wchar_t *s)
75 {
76         wchar_t  *e;
77         wchar_t   a;
78         e=s;
79         while (*e)
80                 e++;
81         while (s<e)
82         {
83                 a=*s;
84                 *s=*e;
85                 *e=a;
86                 s++;
87                 e--;
88         }
89         return s;
90 }
91
92
93 wchar_t *_wcsupr(wchar_t *x)
94 {
95         wchar_t *y=x;
96
97         while (*y)
98         {
99                 *y=towupper(*y);
100                 y++;
101         }
102         return x;
103 }
104
105
106 wchar_t * wcscat(wchar_t *dest, const wchar_t *src)
107 {
108   int i, j;
109    
110   for (j = 0; dest[j] != 0; j++)
111     ;
112   for (i = 0; src[i] != 0; i++)
113     {
114       dest[j + i] = src[i];
115     }
116   dest[j + i] = 0;
117
118   return dest;
119 }
120
121 wchar_t * wcschr(const wchar_t *str, wchar_t ch)
122 {
123   while ((*str) != ((wchar_t) 0))
124     {
125       if ((*str) == ch)
126         {
127           return (wchar_t *) str;
128         }
129       str++;
130     }
131
132   return NULL;
133 }
134
135
136 int wcscmp(const wchar_t *cs, const wchar_t *ct)
137 {
138   while (*cs != '\0' && *ct != '\0' && *cs == *ct)
139     {
140       cs++;
141       ct++;
142     }
143   return *cs - *ct;
144 }
145
146
147 wchar_t* wcscpy(wchar_t* str1, const wchar_t* str2)
148 {
149    wchar_t* s = str1;
150    DPRINT("wcscpy(str1 %S, str2 %S)\n",str1,str2);
151    while ((*str2)!=0)
152      {
153         *s = *str2;
154         s++;
155         str2++;
156      }
157    *s = 0;
158    return(str1);
159 }
160
161
162 size_t wcscspn(const wchar_t *str,const wchar_t *reject)
163 {
164         wchar_t *s;
165         wchar_t *t;
166         s=(wchar_t *)str;
167         do {
168                 t=(wchar_t *)reject;
169                 while (*t) { 
170                         if (*t==*s) 
171                                 break;
172                         t++;
173                 }
174                 if (*t) 
175                         break;
176                 s++;
177         } while (*s);
178         return s-str; /* nr of wchars */
179 }
180
181
182 size_t wcslen(const wchar_t *s)
183 {
184   unsigned int len = 0;
185
186   while (s[len] != 0) 
187     {
188       len++;
189     }
190
191   return len;
192 }
193
194
195 wchar_t * wcsncat(wchar_t *dest, const wchar_t *src, size_t count)
196 {
197   unsigned int i, j;
198    
199   for (j = 0; dest[j] != 0; j++)
200     ;
201   for (i = 0; i < count; i++)
202     {
203       dest[j + i] = src[i];
204       if (src[i] == 0)
205         {
206           return dest;
207         }
208     }
209   dest[j + i] = 0;
210
211   return dest;
212 }
213
214
215 int wcsncmp(const wchar_t *cs, const wchar_t *ct, size_t count)
216 {
217   while (*cs != '\0' && *ct != '\0' && *cs == *ct && --count)
218     {
219       cs++;
220       ct++;
221     }
222   return *cs - *ct;
223 }
224
225
226 wchar_t *wcsncpy(wchar_t *dest, const wchar_t *src, size_t count)
227 {
228   unsigned int i;
229    
230   for (i = 0; i < count; i++)
231     {
232       dest[i] = src[i];
233       if (src[i] == 0)
234         {
235           return dest;
236         }
237     }
238
239   return dest;
240 }
241
242
243 wchar_t *wcsrchr(const wchar_t *str, wchar_t ch)
244 {
245   unsigned int len = 0;
246   while (str[len] != ((wchar_t)0))
247     {
248       len++;
249     }
250    
251   for (; len > 0; len--)
252     {
253       if (str[len-1]==ch)
254         {
255           return (wchar_t *) &str[len - 1];
256         }
257     }
258
259   return NULL;
260 }
261
262
263 size_t wcsspn(const wchar_t *str,const wchar_t *accept)
264 {
265         wchar_t  *s;
266         wchar_t  *t;
267         s=(wchar_t *)str;
268         do
269         {
270                 t=(wchar_t *)accept;
271                 while (*t)
272                 {
273                         if (*t==*s)
274                                 break;
275                         t++;
276                 }
277                 if (!*t)
278                         break;
279                 s++;
280         } while (*s);
281         return s-str; /* nr of wchars */
282 }
283
284
285 wchar_t *wcsstr(const wchar_t *s,const wchar_t *b)
286 {
287         wchar_t *x;
288         wchar_t *y;
289         wchar_t *c;
290         x=(wchar_t *)s;
291         while (*x)
292         {
293                 if (*x==*b)
294                 {
295                         y=x;
296                         c=(wchar_t *)b;
297                         while (*y && *c && *y==*c)
298                         {
299                                 c++;
300                                 y++;
301                         }
302                         if (!*c)
303                                 return x;
304                 }
305                 x++;
306         }
307         return NULL;
308 }