mstask/tests: Add more tests for IEnumWorkItems interface.
[wine.git] / libs / wine / string.c
blob7aa981f0a625e770a7d6e0104e1db645d5fb5d82
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <assert.h>
22 #include <errno.h>
23 #include <limits.h>
24 #include <stdio.h>
26 #define WINE_UNICODE_INLINE /* nothing */
27 #include "wine/unicode.h"
29 int strcmpiW( const WCHAR *str1, const WCHAR *str2 )
31 for (;;)
33 int ret = tolowerW(*str1) - tolowerW(*str2);
34 if (ret || !*str1) return ret;
35 str1++;
36 str2++;
40 int strncmpiW( const WCHAR *str1, const WCHAR *str2, int n )
42 int ret = 0;
43 for ( ; n > 0; n--, str1++, str2++)
44 if ((ret = tolowerW(*str1) - tolowerW(*str2)) || !*str1) break;
45 return ret;
48 int memicmpW( const WCHAR *str1, const WCHAR *str2, int n )
50 int ret = 0;
51 for ( ; n > 0; n--, str1++, str2++)
52 if ((ret = tolowerW(*str1) - tolowerW(*str2))) break;
53 return ret;
56 WCHAR *strstrW( const WCHAR *str, const WCHAR *sub )
58 while (*str)
60 const WCHAR *p1 = str, *p2 = sub;
61 while (*p1 && *p2 && *p1 == *p2) { p1++; p2++; }
62 if (!*p2) return (WCHAR *)str;
63 str++;
65 return NULL;
68 /* strtolW and strtoulW implementation based on the GNU C library code */
69 /* Copyright (C) 1991,92,94,95,96,97,98,99,2000,2001 Free Software Foundation, Inc. */
71 long int strtolW( const WCHAR *nptr, WCHAR **endptr, int base )
73 int negative;
74 register unsigned long int cutoff;
75 register unsigned int cutlim;
76 register unsigned long int i;
77 register const WCHAR *s;
78 register WCHAR c;
79 const WCHAR *save, *end;
80 int overflow;
82 if (base < 0 || base == 1 || base > 36) return 0;
84 save = s = nptr;
86 /* Skip white space. */
87 while (isspaceW (*s))
88 ++s;
89 if (!*s) goto noconv;
91 /* Check for a sign. */
92 negative = 0;
93 if (*s == '-')
95 negative = 1;
96 ++s;
98 else if (*s == '+')
99 ++s;
101 /* Recognize number prefix and if BASE is zero, figure it out ourselves. */
102 if (*s == '0')
104 if ((base == 0 || base == 16) && toupperW(s[1]) == 'X')
106 s += 2;
107 base = 16;
109 else if (base == 0)
110 base = 8;
112 else if (base == 0)
113 base = 10;
115 /* Save the pointer so we can check later if anything happened. */
116 save = s;
117 end = NULL;
119 cutoff = ULONG_MAX / (unsigned long int) base;
120 cutlim = ULONG_MAX % (unsigned long int) base;
122 overflow = 0;
123 i = 0;
124 c = *s;
125 for (;c != '\0'; c = *++s)
127 if (s == end)
128 break;
129 if (c >= '0' && c <= '9')
130 c -= '0';
131 else if (isalphaW (c))
132 c = toupperW (c) - 'A' + 10;
133 else
134 break;
135 if ((int) c >= base)
136 break;
137 /* Check for overflow. */
138 if (i > cutoff || (i == cutoff && c > cutlim))
139 overflow = 1;
140 else
142 i *= (unsigned long int) base;
143 i += c;
147 /* Check if anything actually happened. */
148 if (s == save)
149 goto noconv;
151 /* Store in ENDPTR the address of one character
152 past the last character we converted. */
153 if (endptr != NULL)
154 *endptr = (WCHAR *)s;
156 /* Check for a value that is within the range of
157 `unsigned LONG int', but outside the range of `LONG int'. */
158 if (overflow == 0
159 && i > (negative
160 ? -((unsigned long int) (LONG_MIN + 1)) + 1
161 : (unsigned long int) LONG_MAX))
162 overflow = 1;
164 if (overflow)
166 errno = ERANGE;
167 return negative ? LONG_MIN : LONG_MAX;
170 /* Return the result of the appropriate sign. */
171 return negative ? -i : i;
173 noconv:
174 /* We must handle a special case here: the base is 0 or 16 and the
175 first two characters are '0' and 'x', but the rest are not
176 hexadecimal digits. This is no error case. We return 0 and
177 ENDPTR points to the `x`. */
178 if (endptr != NULL)
180 if (save - nptr >= 2 && toupperW (save[-1]) == 'X'
181 && save[-2] == '0')
182 *endptr = (WCHAR *)&save[-1];
183 else
184 /* There was no number to convert. */
185 *endptr = (WCHAR *)nptr;
188 return 0L;
192 unsigned long int strtoulW( const WCHAR *nptr, WCHAR **endptr, int base )
194 int negative;
195 register unsigned long int cutoff;
196 register unsigned int cutlim;
197 register unsigned long int i;
198 register const WCHAR *s;
199 register WCHAR c;
200 const WCHAR *save, *end;
201 int overflow;
203 if (base < 0 || base == 1 || base > 36) return 0;
205 save = s = nptr;
207 /* Skip white space. */
208 while (isspaceW (*s))
209 ++s;
210 if (!*s) goto noconv;
212 /* Check for a sign. */
213 negative = 0;
214 if (*s == '-')
216 negative = 1;
217 ++s;
219 else if (*s == '+')
220 ++s;
222 /* Recognize number prefix and if BASE is zero, figure it out ourselves. */
223 if (*s == '0')
225 if ((base == 0 || base == 16) && toupperW(s[1]) == 'X')
227 s += 2;
228 base = 16;
230 else if (base == 0)
231 base = 8;
233 else if (base == 0)
234 base = 10;
236 /* Save the pointer so we can check later if anything happened. */
237 save = s;
238 end = NULL;
240 cutoff = ULONG_MAX / (unsigned long int) base;
241 cutlim = ULONG_MAX % (unsigned long int) base;
243 overflow = 0;
244 i = 0;
245 c = *s;
246 for (;c != '\0'; c = *++s)
248 if (s == end)
249 break;
250 if (c >= '0' && c <= '9')
251 c -= '0';
252 else if (isalphaW (c))
253 c = toupperW (c) - 'A' + 10;
254 else
255 break;
256 if ((int) c >= base)
257 break;
258 /* Check for overflow. */
259 if (i > cutoff || (i == cutoff && c > cutlim))
260 overflow = 1;
261 else
263 i *= (unsigned long int) base;
264 i += c;
268 /* Check if anything actually happened. */
269 if (s == save)
270 goto noconv;
272 /* Store in ENDPTR the address of one character
273 past the last character we converted. */
274 if (endptr != NULL)
275 *endptr = (WCHAR *)s;
277 if (overflow)
279 errno = ERANGE;
280 return ULONG_MAX;
283 /* Return the result of the appropriate sign. */
284 return negative ? -i : i;
286 noconv:
287 /* We must handle a special case here: the base is 0 or 16 and the
288 first two characters are '0' and 'x', but the rest are not
289 hexadecimal digits. This is no error case. We return 0 and
290 ENDPTR points to the `x`. */
291 if (endptr != NULL)
293 if (save - nptr >= 2 && toupperW (save[-1]) == 'X'
294 && save[-2] == '0')
295 *endptr = (WCHAR *)&save[-1];
296 else
297 /* There was no number to convert. */
298 *endptr = (WCHAR *)nptr;
301 return 0L;
305 /* format a WCHAR string according to a printf format; helper for vsnprintfW */
306 static size_t format_string( WCHAR *buffer, size_t len, const char *format, const WCHAR *str, int str_len )
308 size_t count = 0;
309 int i, left_align = 0, width = 0, max = 0;
311 assert( *format == '%' );
312 format++; /* skip '%' */
314 while (*format == '0' || *format == '+' || *format == '-' || *format == ' ' || *format == '#')
316 if (*format == '-') left_align = 1;
317 format++;
320 while (isdigit(*format)) width = width * 10 + *format++ - '0';
322 if (str_len == -1) str_len = strlenW( str );
323 if (*format == '.')
325 format++;
326 while (isdigit(*format)) max = max * 10 + *format++ - '0';
327 if (max > str_len) max = str_len;
329 else max = str_len;
331 if (*format == 'h' || *format == 'l') format++;
333 assert( *format == 's' );
335 if (!left_align && width > max)
337 for (i = 0; i < width - max; i++)
339 if (count++ < len)
340 *buffer++ = ' ';
344 if (count < len)
345 memcpy( buffer, str, min( max, len - count ) * sizeof(WCHAR) );
346 count += max;
347 buffer += max;
349 if (left_align && width > max)
351 for (i = 0; i < width - max; i++)
353 if (count++ < len)
354 *buffer++ = ' ';
357 return count;
360 int vsnprintfW(WCHAR *str, size_t len, const WCHAR *format, va_list valist)
362 unsigned int written = 0;
363 const WCHAR *iter = format;
364 char bufa[512], fmtbufa[64], *fmta;
366 while (*iter)
368 while (*iter && *iter != '%')
370 if (written++ < len)
371 *str++ = *iter;
372 iter++;
374 if (*iter == '%')
376 if (iter[1] == '%')
378 if (written++ < len)
379 *str++ = '%'; /* "%%"->'%' */
380 iter += 2;
381 continue;
384 fmta = fmtbufa;
385 *fmta++ = *iter++;
386 while (*iter == '0' ||
387 *iter == '+' ||
388 *iter == '-' ||
389 *iter == ' ' ||
390 *iter == '*' ||
391 *iter == '#')
393 if (*iter == '*')
395 char *buffiter = bufa;
396 int fieldlen = va_arg(valist, int);
397 sprintf(buffiter, "%d", fieldlen);
398 while (*buffiter)
399 *fmta++ = *buffiter++;
401 else
402 *fmta++ = *iter;
403 iter++;
406 while (isdigit(*iter))
407 *fmta++ = *iter++;
409 if (*iter == '.')
411 *fmta++ = *iter++;
412 if (*iter == '*')
414 char *buffiter = bufa;
415 int fieldlen = va_arg(valist, int);
416 sprintf(buffiter, "%d", fieldlen);
417 while (*buffiter)
418 *fmta++ = *buffiter++;
419 iter++;
421 else
422 while (isdigit(*iter))
423 *fmta++ = *iter++;
425 if (*iter == 'h' || *iter == 'l')
426 *fmta++ = *iter++;
428 switch (*iter)
430 case 's':
432 static const WCHAR none[] = { '(','n','u','l','l',')',0 };
433 const WCHAR *wstr = va_arg(valist, const WCHAR *);
434 size_t remaining = written < len ? len - written : 0;
435 size_t count;
437 *fmta++ = 's';
438 *fmta = 0;
439 count = format_string( str, remaining, fmtbufa, wstr ? wstr : none, -1 );
440 str += min( count, remaining );
441 written += count;
442 iter++;
443 break;
446 case 'c':
448 WCHAR wstr;
449 size_t remaining = written < len ? len - written : 0;
450 size_t count;
452 wstr = va_arg(valist, int);
453 *fmta++ = 's';
454 *fmta = 0;
455 count = format_string( str, remaining, fmtbufa, &wstr, 1 );
456 str += min( count, remaining );
457 written += count;
458 iter++;
459 break;
462 default:
464 /* For non wc types, use system sprintf and append to wide char output */
465 /* FIXME: for unrecognised types, should ignore % when printing */
466 char *bufaiter = bufa;
467 if (*iter == 'p')
468 sprintf(bufaiter, "%0*lX", 2 * (int)sizeof(void*),
469 (unsigned long)va_arg(valist, void *));
470 else
472 *fmta++ = *iter;
473 *fmta = '\0';
474 if (*iter == 'a' || *iter == 'A' ||
475 *iter == 'e' || *iter == 'E' ||
476 *iter == 'f' || *iter == 'F' ||
477 *iter == 'g' || *iter == 'G')
478 sprintf(bufaiter, fmtbufa, va_arg(valist, double));
479 else
481 /* FIXME: On 32 bit systems this doesn't handle int 64's. */
482 sprintf(bufaiter, fmtbufa, va_arg(valist, void *));
485 while (*bufaiter)
487 if (written++ < len)
488 *str++ = *bufaiter;
489 bufaiter++;
491 iter++;
492 break;
497 if (len)
499 if (written >= len)
500 str--;
501 *str++ = 0;
504 /* FIXME: POSIX [v]snprintf() returns the equivalent of written, not -1, on short buffer. */
505 return written < len ? (int)written : -1;
508 int vsprintfW( WCHAR *str, const WCHAR *format, va_list valist )
510 return vsnprintfW( str, INT_MAX, format, valist );
513 int snprintfW( WCHAR *str, size_t len, const WCHAR *format, ...)
515 int retval;
516 va_list valist;
517 va_start(valist, format);
518 retval = vsnprintfW(str, len, format, valist);
519 va_end(valist);
520 return retval;
523 int sprintfW( WCHAR *str, const WCHAR *format, ...)
525 int retval;
526 va_list valist;
527 va_start(valist, format);
528 retval = vsnprintfW(str, INT_MAX, format, valist);
529 va_end(valist);
530 return retval;