:pserver:cvsanon@mok.lvcm.com:/CVS/ReactOS reactos
[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 wchar_t *_wcslwr (wchar_t *x)
33 {
34         wchar_t  *y=x;
35
36         while (*y)
37         {
38                 *y=towlower(*y);
39                 y++;
40         }
41         return x;
42 }
43
44
45 int _wcsnicmp (const wchar_t * cs,const wchar_t * ct,size_t count)
46 {
47         if (count == 0)
48                 return 0;
49         do {
50                 if (towupper(*cs) != towupper(*ct++))
51                         return towupper(*cs) - towupper(*--ct);
52                 if (*cs++ == 0)
53                         break;
54         } while (--count != 0);
55         return 0;
56 }
57
58
59 wchar_t *_wcsnset (wchar_t* wsToFill, wchar_t wcFill, size_t sizeMaxFill)
60 {
61         wchar_t *t = wsToFill;
62         int i = 0;
63         while( *wsToFill != 0 && i < sizeMaxFill)
64         {
65                 *wsToFill = wcFill;
66                 wsToFill++;
67                 i++;
68         }
69         return t;
70 }
71
72
73 wchar_t *_wcsrev(wchar_t *s)
74 {
75         wchar_t  *e;
76         wchar_t   a;
77         e=s;
78         while (*e)
79                 e++;
80         while (s<e)
81         {
82                 a=*s;
83                 *s=*e;
84                 *e=a;
85                 s++;
86                 e--;
87         }
88         return s;
89 }
90
91
92 wchar_t *_wcsupr(wchar_t *x)
93 {
94         wchar_t *y=x;
95
96         while (*y)
97         {
98                 *y=towupper(*y);
99                 y++;
100         }
101         return x;
102 }
103
104
105 wchar_t * wcscat(wchar_t *dest, const wchar_t *src)
106 {
107   int i, j;
108    
109   for (j = 0; dest[j] != 0; j++)
110     ;
111   for (i = 0; src[i] != 0; i++)
112     {
113       dest[j + i] = src[i];
114     }
115   dest[j + i] = 0;
116
117   return dest;
118 }
119
120 wchar_t * wcschr(const wchar_t *str, wchar_t ch)
121 {
122   while ((*str) != ((wchar_t) 0))
123     {
124       if ((*str) == ch)
125         {
126           return (wchar_t *) str;
127         }
128       str++;
129     }
130
131   return NULL;
132 }
133
134
135 int wcscmp(const wchar_t *cs, const wchar_t *ct)
136 {
137   while (*cs != '\0' && *ct != '\0' && *cs == *ct)
138     {
139       cs++;
140       ct++;
141     }
142   return *cs - *ct;
143 }
144
145
146 wchar_t* wcscpy(wchar_t* str1, const wchar_t* str2)
147 {
148    wchar_t* s = str1;
149    DPRINT("wcscpy(str1 %S, str2 %S)\n",str1,str2);
150    while ((*str2)!=0)
151      {
152         *s = *str2;
153         s++;
154         str2++;
155      }
156    *s = 0;
157    return(str1);
158 }
159
160
161 size_t wcscspn(const wchar_t *str,const wchar_t *reject)
162 {
163         wchar_t *s;
164         wchar_t *t;
165         s=(wchar_t *)str;
166         do {
167                 t=(wchar_t *)reject;
168                 while (*t) { 
169                         if (*t==*s) 
170                                 break;
171                         t++;
172                 }
173                 if (*t) 
174                         break;
175                 s++;
176         } while (*s);
177         return s-str; /* nr of wchars */
178 }
179
180
181 size_t wcslen(const wchar_t *s)
182 {
183   unsigned int len = 0;
184
185   while (s[len] != 0) 
186     {
187       len++;
188     }
189
190   return len;
191 }
192
193
194 wchar_t * wcsncat(wchar_t *dest, const wchar_t *src, size_t count)
195 {
196   int i, j;
197    
198   for (j = 0; dest[j] != 0; j++)
199     ;
200   for (i = 0; i < count; i++)
201     {
202       dest[j + i] = src[i];
203       if (src[i] == 0)
204         {
205           return dest;
206         }
207     }
208   dest[j + i] = 0;
209
210   return dest;
211 }
212
213
214 int wcsncmp(const wchar_t *cs, const wchar_t *ct, size_t count)
215 {
216   while (*cs != '\0' && *ct != '\0' && *cs == *ct && --count)
217     {
218       cs++;
219       ct++;
220     }
221   return *cs - *ct;
222 }
223
224
225 wchar_t *wcsncpy(wchar_t *dest, const wchar_t *src, size_t count)
226 {
227   int i;
228    
229   for (i = 0; i < count; i++)
230     {
231       dest[i] = src[i];
232       if (src[i] == 0)
233         {
234           return dest;
235         }
236     }
237   dest[i] = 0;
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 }