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 int memicmpW( const WCHAR
*str1
, const WCHAR
*str2
, int n
)
48 for ( ; n
> 0; n
--, str1
++, str2
++)
49 if ((ret
= tolowerW(*str1
) - tolowerW(*str2
))) break;
53 WCHAR
*strstrW( const WCHAR
*str
, const WCHAR
*sub
)
57 const WCHAR
*p1
= str
, *p2
= sub
;
58 while (*p1
&& *p2
&& *p1
== *p2
) { p1
++; p2
++; }
59 if (!*p2
) return (WCHAR
*)str
;
65 /* strtolW and strtoulW implementation based on the GNU C library code */
66 /* Copyright (C) 1991,92,94,95,96,97,98,99,2000,2001 Free Software Foundation, Inc. */
68 long int strtolW( const WCHAR
*nptr
, WCHAR
**endptr
, int base
)
71 register unsigned long int cutoff
;
72 register unsigned int cutlim
;
73 register unsigned long int i
;
74 register const WCHAR
*s
;
76 const WCHAR
*save
, *end
;
79 if (base
< 0 || base
== 1 || base
> 36) return 0;
83 /* Skip white space. */
88 /* Check for a sign. */
98 /* Recognize number prefix and if BASE is zero, figure it out ourselves. */
101 if ((base
== 0 || base
== 16) && toupperW(s
[1]) == 'X')
112 /* Save the pointer so we can check later if anything happened. */
116 cutoff
= ULONG_MAX
/ (unsigned long int) base
;
117 cutlim
= ULONG_MAX
% (unsigned long int) base
;
122 for (;c
!= '\0'; c
= *++s
)
126 if (c
>= '0' && c
<= '9')
128 else if (isalphaW (c
))
129 c
= toupperW (c
) - 'A' + 10;
134 /* Check for overflow. */
135 if (i
> cutoff
|| (i
== cutoff
&& c
> cutlim
))
139 i
*= (unsigned long int) base
;
144 /* Check if anything actually happened. */
148 /* Store in ENDPTR the address of one character
149 past the last character we converted. */
151 *endptr
= (WCHAR
*)s
;
153 /* Check for a value that is within the range of
154 `unsigned LONG int', but outside the range of `LONG int'. */
157 ? -((unsigned long int) (LONG_MIN
+ 1)) + 1
158 : (unsigned long int) LONG_MAX
))
163 return negative
? LONG_MIN
: LONG_MAX
;
166 /* Return the result of the appropriate sign. */
167 return negative
? -i
: i
;
170 /* We must handle a special case here: the base is 0 or 16 and the
171 first two characters are '0' and 'x', but the rest are not
172 hexadecimal digits. This is no error case. We return 0 and
173 ENDPTR points to the `x`. */
176 if (save
- nptr
>= 2 && toupperW (save
[-1]) == 'X'
178 *endptr
= (WCHAR
*)&save
[-1];
180 /* There was no number to convert. */
181 *endptr
= (WCHAR
*)nptr
;
188 unsigned long int strtoulW( const WCHAR
*nptr
, WCHAR
**endptr
, int base
)
191 register unsigned long int cutoff
;
192 register unsigned int cutlim
;
193 register unsigned long int i
;
194 register const WCHAR
*s
;
196 const WCHAR
*save
, *end
;
199 if (base
< 0 || base
== 1 || base
> 36) return 0;
203 /* Skip white space. */
204 while (isspaceW (*s
))
206 if (!*s
) goto noconv
;
208 /* Check for a sign. */
218 /* Recognize number prefix and if BASE is zero, figure it out ourselves. */
221 if ((base
== 0 || base
== 16) && toupperW(s
[1]) == 'X')
232 /* Save the pointer so we can check later if anything happened. */
236 cutoff
= ULONG_MAX
/ (unsigned long int) base
;
237 cutlim
= ULONG_MAX
% (unsigned long int) base
;
242 for (;c
!= '\0'; c
= *++s
)
246 if (c
>= '0' && c
<= '9')
248 else if (isalphaW (c
))
249 c
= toupperW (c
) - 'A' + 10;
254 /* Check for overflow. */
255 if (i
> cutoff
|| (i
== cutoff
&& c
> cutlim
))
259 i
*= (unsigned long int) base
;
264 /* Check if anything actually happened. */
268 /* Store in ENDPTR the address of one character
269 past the last character we converted. */
271 *endptr
= (WCHAR
*)s
;
278 /* Return the result of the appropriate sign. */
279 return negative
? -i
: i
;
282 /* We must handle a special case here: the base is 0 or 16 and the
283 first two characters are '0' and 'x', but the rest are not
284 hexadecimal digits. This is no error case. We return 0 and
285 ENDPTR points to the `x`. */
288 if (save
- nptr
>= 2 && toupperW (save
[-1]) == 'X'
290 *endptr
= (WCHAR
*)&save
[-1];
292 /* There was no number to convert. */
293 *endptr
= (WCHAR
*)nptr
;
300 int vsnprintfW(WCHAR
*str
, size_t len
, const WCHAR
*format
, va_list valist
)
302 unsigned int written
= 0;
303 const WCHAR
*iter
= format
;
304 char bufa
[256], fmtbufa
[64], *fmta
;
308 while (*iter
&& *iter
!= '%')
310 if (written
++ >= len
)
318 if (written
++ >= len
)
320 *str
++ = '%'; /* "%%"->'%' */
327 while (*iter
== '0' ||
336 char *buffiter
= bufa
;
337 int fieldlen
= va_arg(valist
, int);
338 sprintf(buffiter
, "%d", fieldlen
);
340 *fmta
++ = *buffiter
++;
347 while (isdigit(*iter
))
355 char *buffiter
= bufa
;
356 int fieldlen
= va_arg(valist
, int);
357 sprintf(buffiter
, "%d", fieldlen
);
359 *fmta
++ = *buffiter
++;
362 while (isdigit(*iter
))
365 if (*iter
== 'h' || *iter
== 'l')
372 static const WCHAR none
[] = { '(','n','u','l','l',')',0 };
373 const WCHAR
*wstr
= va_arg(valist
, const WCHAR
*);
374 const WCHAR
*striter
= wstr
? wstr
: none
;
377 if (written
++ >= len
)
386 if (written
++ >= len
)
388 *str
++ = (WCHAR
)va_arg(valist
, int);
394 /* For non wc types, use system sprintf and append to wide char output */
395 /* FIXME: for unrecognised types, should ignore % when printing */
396 char *bufaiter
= bufa
;
398 sprintf(bufaiter
, "%08lX", va_arg(valist
, long));
403 if (*iter
== 'a' || *iter
== 'A' ||
404 *iter
== 'e' || *iter
== 'E' ||
405 *iter
== 'f' || *iter
== 'F' ||
406 *iter
== 'g' || *iter
== 'G')
407 sprintf(bufaiter
, fmtbufa
, va_arg(valist
, double));
410 /* FIXME: On 32 bit systems this doesn't handle int 64's.
411 * on 64 bit systems this doesn't work for 32 bit types
413 sprintf(bufaiter
, fmtbufa
, va_arg(valist
, void *));
418 if (written
++ >= len
)
420 *str
++ = *bufaiter
++;
434 int vsprintfW( WCHAR
*str
, const WCHAR
*format
, va_list valist
)
436 return vsnprintfW( str
, INT_MAX
, format
, valist
);
439 int snprintfW( WCHAR
*str
, size_t len
, const WCHAR
*format
, ...)
443 va_start(valist
, format
);
444 retval
= vsnprintfW(str
, len
, format
, valist
);
449 int sprintfW( WCHAR
*str
, const WCHAR
*format
, ...)
453 va_start(valist
, format
);
454 retval
= vsnprintfW(str
, INT_MAX
, format
, valist
);