Implemented Rtl*ByteSwap() functions, based on a patch by Jon
[wine/multimedia.git] / unicode / string.c
blobae66ebc268ca81296b7e08879eac686af9331687
1 /*
2 * Unicode string manipulation functions
4 * Copyright 2000 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <limits.h>
22 #include <stdio.h>
24 #include "wine/unicode.h"
26 int strcmpiW( const WCHAR *str1, const WCHAR *str2 )
28 for (;;)
30 int ret = toupperW(*str1) - toupperW(*str2);
31 if (ret || !*str1) return ret;
32 str1++;
33 str2++;
37 int strncmpiW( const WCHAR *str1, const WCHAR *str2, int n )
39 int ret = 0;
40 for ( ; n > 0; n--, str1++, str2++)
41 if ((ret = toupperW(*str1) - toupperW(*str2)) || !*str1) break;
42 return ret;
45 WCHAR *strstrW( const WCHAR *str, const WCHAR *sub )
47 while (*str)
49 const WCHAR *p1 = str, *p2 = sub;
50 while (*p1 && *p2 && *p1 == *p2) { p1++; p2++; }
51 if (!*p2) return (WCHAR *)str;
52 str++;
54 return NULL;
57 /* strtolW and strtoulW implementation based on the GNU C library code */
58 /* Copyright (C) 1991,92,94,95,96,97,98,99,2000,2001 Free Software Foundation, Inc. */
60 long int strtolW( const WCHAR *nptr, WCHAR **endptr, int base )
62 int negative;
63 register unsigned long int cutoff;
64 register unsigned int cutlim;
65 register unsigned long int i;
66 register const WCHAR *s;
67 register WCHAR c;
68 const WCHAR *save, *end;
69 int overflow;
71 if (base < 0 || base == 1 || base > 36) return 0;
73 save = s = nptr;
75 /* Skip white space. */
76 while (isspaceW (*s))
77 ++s;
78 if (!*s) goto noconv;
80 /* Check for a sign. */
81 negative = 0;
82 if (*s == '-')
84 negative = 1;
85 ++s;
87 else if (*s == '+')
88 ++s;
90 /* Recognize number prefix and if BASE is zero, figure it out ourselves. */
91 if (*s == '0')
93 if ((base == 0 || base == 16) && toupperW(s[1]) == 'X')
95 s += 2;
96 base = 16;
98 else if (base == 0)
99 base = 8;
101 else if (base == 0)
102 base = 10;
104 /* Save the pointer so we can check later if anything happened. */
105 save = s;
106 end = NULL;
108 cutoff = ULONG_MAX / (unsigned long int) base;
109 cutlim = ULONG_MAX % (unsigned long int) base;
111 overflow = 0;
112 i = 0;
113 c = *s;
114 for (;c != '\0'; c = *++s)
116 if (s == end)
117 break;
118 if (c >= '0' && c <= '9')
119 c -= '0';
120 else if (isalphaW (c))
121 c = toupperW (c) - 'A' + 10;
122 else
123 break;
124 if ((int) c >= base)
125 break;
126 /* Check for overflow. */
127 if (i > cutoff || (i == cutoff && c > cutlim))
128 overflow = 1;
129 else
131 i *= (unsigned long int) base;
132 i += c;
136 /* Check if anything actually happened. */
137 if (s == save)
138 goto noconv;
140 /* Store in ENDPTR the address of one character
141 past the last character we converted. */
142 if (endptr != NULL)
143 *endptr = (WCHAR *)s;
145 /* Check for a value that is within the range of
146 `unsigned LONG int', but outside the range of `LONG int'. */
147 if (overflow == 0
148 && i > (negative
149 ? -((unsigned long int) (LONG_MIN + 1)) + 1
150 : (unsigned long int) LONG_MAX))
151 overflow = 1;
153 if (overflow)
155 return negative ? LONG_MIN : LONG_MAX;
158 /* Return the result of the appropriate sign. */
159 return negative ? -i : i;
161 noconv:
162 /* We must handle a special case here: the base is 0 or 16 and the
163 first two characters are '0' and 'x', but the rest are no
164 hexadecimal digits. This is no error case. We return 0 and
165 ENDPTR points to the `x`. */
166 if (endptr != NULL)
168 if (save - nptr >= 2 && toupperW (save[-1]) == 'X'
169 && save[-2] == '0')
170 *endptr = (WCHAR *)&save[-1];
171 else
172 /* There was no number to convert. */
173 *endptr = (WCHAR *)nptr;
176 return 0L;
180 unsigned long int strtoulW( const WCHAR *nptr, WCHAR **endptr, int base )
182 int negative;
183 register unsigned long int cutoff;
184 register unsigned int cutlim;
185 register unsigned long int i;
186 register const WCHAR *s;
187 register WCHAR c;
188 const WCHAR *save, *end;
189 int overflow;
191 if (base < 0 || base == 1 || base > 36) return 0;
193 save = s = nptr;
195 /* Skip white space. */
196 while (isspaceW (*s))
197 ++s;
198 if (!*s) goto noconv;
200 /* Check for a sign. */
201 negative = 0;
202 if (*s == '-')
204 negative = 1;
205 ++s;
207 else if (*s == '+')
208 ++s;
210 /* Recognize number prefix and if BASE is zero, figure it out ourselves. */
211 if (*s == '0')
213 if ((base == 0 || base == 16) && toupperW(s[1]) == 'X')
215 s += 2;
216 base = 16;
218 else if (base == 0)
219 base = 8;
221 else if (base == 0)
222 base = 10;
224 /* Save the pointer so we can check later if anything happened. */
225 save = s;
226 end = NULL;
228 cutoff = ULONG_MAX / (unsigned long int) base;
229 cutlim = ULONG_MAX % (unsigned long int) base;
231 overflow = 0;
232 i = 0;
233 c = *s;
234 for (;c != '\0'; c = *++s)
236 if (s == end)
237 break;
238 if (c >= '0' && c <= '9')
239 c -= '0';
240 else if (isalphaW (c))
241 c = toupperW (c) - 'A' + 10;
242 else
243 break;
244 if ((int) c >= base)
245 break;
246 /* Check for overflow. */
247 if (i > cutoff || (i == cutoff && c > cutlim))
248 overflow = 1;
249 else
251 i *= (unsigned long int) base;
252 i += c;
256 /* Check if anything actually happened. */
257 if (s == save)
258 goto noconv;
260 /* Store in ENDPTR the address of one character
261 past the last character we converted. */
262 if (endptr != NULL)
263 *endptr = (WCHAR *)s;
265 if (overflow)
267 return ULONG_MAX;
270 /* Return the result of the appropriate sign. */
271 return negative ? -i : i;
273 noconv:
274 /* We must handle a special case here: the base is 0 or 16 and the
275 first two characters are '0' and 'x', but the rest are no
276 hexadecimal digits. This is no error case. We return 0 and
277 ENDPTR points to the `x`. */
278 if (endptr != NULL)
280 if (save - nptr >= 2 && toupperW (save[-1]) == 'X'
281 && save[-2] == '0')
282 *endptr = (WCHAR *)&save[-1];
283 else
284 /* There was no number to convert. */
285 *endptr = (WCHAR *)nptr;
288 return 0L;
292 int vsnprintfW(WCHAR *str, unsigned int len, const WCHAR *format, va_list valist)
294 unsigned int written = 0;
295 const WCHAR *iter = format;
296 char bufa[256], fmtbufa[64], *fmta;
298 while (*iter)
300 while (*iter && *iter != '%')
302 if (written++ >= len)
303 return -1;
304 *str++ = *iter++;
306 if (*iter == '%')
308 fmta = fmtbufa;
309 *fmta++ = *iter++;
310 while (*iter == '0' ||
311 *iter == '+' ||
312 *iter == '-' ||
313 *iter == ' ' ||
314 *iter == '0' ||
315 *iter == '*' ||
316 *iter == '#')
318 if (*iter == '*')
320 char *buffiter = bufa;
321 int fieldlen = va_arg(valist, int);
322 sprintf(buffiter, "%d", fieldlen);
323 while (*buffiter)
324 *fmta++ = *buffiter++;
326 else
327 *fmta++ = *iter;
328 iter++;
331 while (isdigit(*iter))
332 *fmta++ = *iter++;
334 if (*iter == '.')
336 *fmta++ = *iter++;
337 if (*iter == '*')
339 char *buffiter = bufa;
340 int fieldlen = va_arg(valist, int);
341 sprintf(buffiter, "%d", fieldlen);
342 while (*buffiter)
343 *fmta++ = *buffiter++;
345 else
346 while (isdigit(*iter))
347 *fmta++ = *iter++;
349 if (*iter == 'h' || *iter == 'l')
350 *fmta++ = *iter++;
352 switch (*iter)
354 case 's':
356 static const WCHAR none[] = { '(','n','u','l','l',')',0 };
357 const WCHAR *wstr = va_arg(valist, const WCHAR *);
358 const WCHAR *striter = wstr ? wstr : none;
359 while (*striter)
361 if (written++ >= len)
362 return -1;
363 *str++ = *striter++;
365 iter++;
366 break;
369 case 'c':
370 if (written++ >= len)
371 return -1;
372 *str++ = (WCHAR)va_arg(valist, int);
373 iter++;
374 break;
376 default:
378 /* For non wc types, use system sprintf and append to wide char output */
379 /* FIXME: for unrecognised types, should ignore % when printing */
380 char *bufaiter = bufa;
381 if (*iter == 'p')
382 sprintf(bufaiter, "%08lX", va_arg(valist, long));
383 else
385 *fmta++ = *iter;
386 *fmta = '\0';
387 if (*iter == 'f')
388 sprintf(bufaiter, fmtbufa, va_arg(valist, double));
389 else
390 sprintf(bufaiter, fmtbufa, va_arg(valist, void *));
392 while (*bufaiter)
394 if (written++ >= len)
395 return -1;
396 *str++ = *bufaiter++;
398 iter++;
399 break;
404 if (written >= len)
405 return -1;
406 *str++ = 0;
407 return (int)written;
411 int snprintfW(WCHAR *str, unsigned int len, const WCHAR *format, ...)
413 int retval;
414 va_list valist;
415 va_start(valist, format);
416 retval = vsnprintfW(str, len, format, valist);
417 va_end(valist);
418 return retval;