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
24 #include "wine/unicode.h"
26 int strcmpiW( const WCHAR *str1, const WCHAR *str2 )
30 int ret = tolowerW(*str1) - tolowerW(*str2);
31 if (ret || !*str1) return ret;
37 int strncmpiW( const WCHAR *str1, const WCHAR *str2, int n )
40 for ( ; n > 0; n--, str1++, str2++)
41 if ((ret = tolowerW(*str1) - tolowerW(*str2)) || !*str1) break;
45 WCHAR *strstrW( const WCHAR *str, const WCHAR *sub )
49 const WCHAR *p1 = str, *p2 = sub;
50 while (*p1 && *p2 && *p1 == *p2) { p1++; p2++; }
51 if (!*p2) return (WCHAR *)str;
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 )
63 register unsigned long int cutoff;
64 register unsigned int cutlim;
65 register unsigned long int i;
66 register const WCHAR *s;
68 const WCHAR *save, *end;
71 if (base < 0 || base == 1 || base > 36) return 0;
75 /* Skip white space. */
80 /* Check for a sign. */
90 /* Recognize number prefix and if BASE is zero, figure it out ourselves. */
93 if ((base == 0 || base == 16) && toupperW(s[1]) == 'X')
104 /* Save the pointer so we can check later if anything happened. */
108 cutoff = ULONG_MAX / (unsigned long int) base;
109 cutlim = ULONG_MAX % (unsigned long int) base;
114 for (;c != '\0'; c = *++s)
118 if (c >= '0' && c <= '9')
120 else if (isalphaW (c))
121 c = toupperW (c) - 'A' + 10;
126 /* Check for overflow. */
127 if (i > cutoff || (i == cutoff && c > cutlim))
131 i *= (unsigned long int) base;
136 /* Check if anything actually happened. */
140 /* Store in ENDPTR the address of one character
141 past the last character we converted. */
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'. */
149 ? -((unsigned long int) (LONG_MIN + 1)) + 1
150 : (unsigned long int) LONG_MAX))
155 return negative ? LONG_MIN : LONG_MAX;
158 /* Return the result of the appropriate sign. */
159 return negative ? -i : i;
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 not
164 hexadecimal digits. This is no error case. We return 0 and
165 ENDPTR points to the `x`. */
168 if (save - nptr >= 2 && toupperW (save[-1]) == 'X'
170 *endptr = (WCHAR *)&save[-1];
172 /* There was no number to convert. */
173 *endptr = (WCHAR *)nptr;
180 unsigned long int strtoulW( const WCHAR *nptr, WCHAR **endptr, int base )
183 register unsigned long int cutoff;
184 register unsigned int cutlim;
185 register unsigned long int i;
186 register const WCHAR *s;
188 const WCHAR *save, *end;
191 if (base < 0 || base == 1 || base > 36) return 0;
195 /* Skip white space. */
196 while (isspaceW (*s))
198 if (!*s) goto noconv;
200 /* Check for a sign. */
210 /* Recognize number prefix and if BASE is zero, figure it out ourselves. */
213 if ((base == 0 || base == 16) && toupperW(s[1]) == 'X')
224 /* Save the pointer so we can check later if anything happened. */
228 cutoff = ULONG_MAX / (unsigned long int) base;
229 cutlim = ULONG_MAX % (unsigned long int) base;
234 for (;c != '\0'; c = *++s)
238 if (c >= '0' && c <= '9')
240 else if (isalphaW (c))
241 c = toupperW (c) - 'A' + 10;
246 /* Check for overflow. */
247 if (i > cutoff || (i == cutoff && c > cutlim))
251 i *= (unsigned long int) base;
256 /* Check if anything actually happened. */
260 /* Store in ENDPTR the address of one character
261 past the last character we converted. */
263 *endptr = (WCHAR *)s;
270 /* Return the result of the appropriate sign. */
271 return negative ? -i : i;
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 not
276 hexadecimal digits. This is no error case. We return 0 and
277 ENDPTR points to the `x`. */
280 if (save - nptr >= 2 && toupperW (save[-1]) == 'X'
282 *endptr = (WCHAR *)&save[-1];
284 /* There was no number to convert. */
285 *endptr = (WCHAR *)nptr;
292 int vsnprintfW(WCHAR *str, size_t len, const WCHAR *format, va_list valist)
294 unsigned int written = 0;
295 const WCHAR *iter = format;
296 char bufa[256], fmtbufa[64], *fmta;
300 while (*iter && *iter != '%')
302 if (written++ >= len)
310 if (written++ >= len)
312 *str++ = '%'; /* "%%"->'%' */
319 while (*iter == '0' ||
328 char *buffiter = bufa;
329 int fieldlen = va_arg(valist, int);
330 sprintf(buffiter, "%d", fieldlen);
332 *fmta++ = *buffiter++;
339 while (isdigit(*iter))
347 char *buffiter = bufa;
348 int fieldlen = va_arg(valist, int);
349 sprintf(buffiter, "%d", fieldlen);
351 *fmta++ = *buffiter++;
354 while (isdigit(*iter))
357 if (*iter == 'h' || *iter == 'l')
364 static const WCHAR none[] = { '(','n','u','l','l',')',0 };
365 const WCHAR *wstr = va_arg(valist, const WCHAR *);
366 const WCHAR *striter = wstr ? wstr : none;
369 if (written++ >= len)
378 if (written++ >= len)
380 *str++ = (WCHAR)va_arg(valist, int);
386 /* For non wc types, use system sprintf and append to wide char output */
387 /* FIXME: for unrecognised types, should ignore % when printing */
388 char *bufaiter = bufa;
390 sprintf(bufaiter, "%08lX", va_arg(valist, long));
395 if (*iter == 'a' || *iter == 'A' ||
396 *iter == 'e' || *iter == 'E' ||
397 *iter == 'f' || *iter == 'F' ||
398 *iter == 'g' || *iter == 'G')
399 sprintf(bufaiter, fmtbufa, va_arg(valist, double));
402 /* FIXME: On 32 bit systems this doesn't handle int 64's.
403 * on 64 bit systems this doesn't work for 32 bit types
405 sprintf(bufaiter, fmtbufa, va_arg(valist, void *));
410 if (written++ >= len)
412 *str++ = *bufaiter++;
426 int vsprintfW( WCHAR *str, const WCHAR *format, va_list valist )
428 return vsnprintfW( str, INT_MAX, format, valist );
431 int snprintfW( WCHAR *str, size_t len, const WCHAR *format, ...)
435 va_start(valist, format);
436 retval = vsnprintfW(str, len, format, valist);
441 int sprintfW( WCHAR *str, const WCHAR *format, ...)
445 va_start(valist, format);
446 retval = vsnprintfW(str, INT_MAX, format, valist);