branch update for HEAD-2003021201
[reactos.git] / lib / ntdll / string / wstring.c
1 /* $Id$
2  *
3  * COPYRIGHT:       See COPYING in the top level directory
4  * PROJECT:         ReactOS kernel
5  * FILE:            lib/ntdll/string/wstring.c
6  * PURPOSE:         Wide string functions
7  * PROGRAMMER:      David Welch (welch@mcmail.com)
8  * UPDATE HISTORY:
9  *                  Created 22/05/98
10  *   1998/12/04  RJJ  Cleaned up and added i386 def checks
11  */
12
13 /* INCLUDES *****************************************************************/
14
15 #include <ddk/ntddk.h>
16 #include <wchar.h>
17
18
19 /* FUNCTIONS *****************************************************************/
20
21 int _wcsicmp (const wchar_t* cs, const wchar_t * ct)
22 {
23         while (towlower(*cs) == towlower(*ct))
24         {
25                 if (*cs == 0)
26                         return 0;
27                 cs++;
28                 ct++;
29         }
30         return towlower(*cs) - towlower(*ct);
31 }
32
33
34 wchar_t *_wcslwr (wchar_t *x)
35 {
36         wchar_t *y=x;
37
38         while (*y) {
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 *_wcsupr(wchar_t *x)
61 {
62         wchar_t  *y=x;
63
64         while (*y) {
65                 *y=towupper(*y);
66                 y++;
67         }
68         return x;
69 }
70
71
72 wchar_t *wcscat (wchar_t *dest, const wchar_t *src)
73 {
74   int i, j;
75
76   for (j = 0; dest[j] != 0; j++)
77     ;
78   for (i = 0; src[i] != 0; i++)
79     {
80       dest[j + i] = src[i];
81     }
82   dest[j + i] = 0;
83
84   return dest;
85 }
86
87 wchar_t *wcschr (const wchar_t *str, wchar_t ch)
88 {
89   while ((*str) != ((wchar_t) 0))
90     {
91       if ((*str) == ch)
92         {
93           return (wchar_t *)str;
94         }
95       str++;
96     }
97
98   return NULL;
99 }
100
101 int wcscmp(const wchar_t *cs, const wchar_t *ct)
102 {
103   while (*cs != '\0' && *ct != '\0' && *cs == *ct)
104     {
105       cs++;
106       ct++;
107     }
108   return *cs - *ct;
109 }
110
111 wchar_t* wcscpy(wchar_t* str1, const wchar_t* str2)
112 {
113    wchar_t* s = str1;
114    while ((*str2)!=0)
115      {
116         *s = *str2;
117         s++;
118         str2++;
119      }
120    *s = 0;
121    return(str1);
122 }
123
124
125 size_t wcscspn(const wchar_t *str,const wchar_t *reject)
126 {
127         wchar_t *s;
128         wchar_t *t;
129         s=(wchar_t *)str;
130         do {
131                 t=(wchar_t *)reject;
132                 while (*t) {
133                         if (*t==*s)
134                                 break;
135                         t++;
136                 }
137                 if (*t)
138                         break;
139                 s++;
140         } while (*s);
141         return s-str; /* nr of wchars */
142 }
143
144
145 size_t wcslen(const wchar_t *s)
146 {
147   unsigned int len = 0;
148
149   while (s[len] != 0)
150     {
151       len++;
152     }
153
154   return len;
155 }
156
157 wchar_t *wcsncat(wchar_t *dest, const wchar_t *src, size_t count)
158 {
159   int i, j;
160
161   for (j = 0; dest[j] != 0; j++)
162     ;
163   for (i = 0; i < count; i++)
164     {
165       dest[j + i] = src[i];
166       if (src[i] == 0)
167         {
168           return dest;
169         }
170     }
171   dest[j + i] = 0;
172
173   return dest;
174 }
175
176
177 int wcsncmp(const wchar_t * cs,const wchar_t * ct,size_t count)
178 {
179   while ((*cs) == (*ct) && count > 0)
180   {
181     if (*cs == 0)
182       return 0;
183     cs++;
184     ct++;
185     count--;
186   }
187   return (*cs) - (*ct);
188 }
189
190
191 wchar_t* wcsncpy(wchar_t *dest, const wchar_t *src, size_t count)
192 {
193   int i;
194    
195   for (i = 0; i < count; i++)
196     {
197       dest[i] = src[i];
198       if (src[i] == 0)
199         {
200           return dest;
201         }
202     }
203
204   return dest;
205 }
206
207
208 wchar_t *wcspbrk(const wchar_t *s1, const wchar_t *s2)
209 {
210   const wchar_t *scanp;
211   int c, sc;
212
213   while ((c = *s1++) != 0)
214   {
215     for (scanp = s2; (sc = *scanp++) != 0;)
216       if (sc == c)
217       {
218         return (wchar_t *)(--s1);
219       }
220   }
221   return 0;
222 }
223
224
225 wchar_t * wcsrchr(const wchar_t *str, wchar_t ch)
226 {
227   unsigned int len = 0;
228   while (str[len] != ((wchar_t)0))
229     {
230       len++;
231     }
232    
233   for (; len > 0; len--)
234     {
235       if (str[len-1]==ch)
236         {
237           return (wchar_t *) &str[len - 1];
238         }
239     }
240
241   return NULL;
242 }
243
244
245 size_t wcsspn(const wchar_t *str,const wchar_t *accept)
246 {
247         wchar_t  *s;
248         wchar_t  *t;
249         s=(wchar_t *)str;
250         do {
251                 t=(wchar_t *)accept;
252                 while (*t) {
253                         if (*t==*s)
254                                 break;
255                         t++;
256                 }
257                 if (!*t)
258                         break;
259                 s++;
260         } while (*s);
261         return s-str; /* nr of wchars */
262 }
263
264
265 wchar_t *wcsstr(const wchar_t *s,const wchar_t *b)
266 {
267         wchar_t *x;
268         wchar_t *y;
269         wchar_t *c;
270         x=(wchar_t *)s;
271         while (*x) {
272                 if (*x==*b) {
273                         y=x;
274                         c=(wchar_t *)b;
275                         while (*y && *c && *y==*c) {
276                                 c++;
277                                 y++;
278                         }
279                         if (!*c)
280                                 return x;
281                 }
282                 x++;
283         }
284         return NULL;
285 }
286
287 /* EOF */