rsaenh/tests: Fix a test failure on W2K and below.
[wine/wine64.git] / dlls / ntdll / wcstring.c
blobf987fc25854fb8587bc9f5b3e6f6c687faa488c1
1 /*
2 * NTDLL wide-char functions
4 * Copyright 2000 Alexandre Julliard
5 * Copyright 2000 Jon Griffiths
6 * Copyright 2003 Thomas Mertes
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
25 #include <ctype.h>
26 #include <limits.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdarg.h>
30 #include <stdio.h>
32 #include "windef.h"
33 #include "winternl.h"
34 #include "wine/unicode.h"
36 /*********************************************************************
37 * _wcsicmp (NTDLL.@)
39 INT __cdecl NTDLL__wcsicmp( LPCWSTR str1, LPCWSTR str2 )
41 return strcmpiW( str1, str2 );
45 /*********************************************************************
46 * _wcslwr (NTDLL.@)
48 LPWSTR __cdecl NTDLL__wcslwr( LPWSTR str )
50 return strlwrW( str );
54 /*********************************************************************
55 * _wcsnicmp (NTDLL.@)
57 INT __cdecl NTDLL__wcsnicmp( LPCWSTR str1, LPCWSTR str2, INT n )
59 return strncmpiW( str1, str2, n );
63 /*********************************************************************
64 * _wcsupr (NTDLL.@)
66 LPWSTR __cdecl NTDLL__wcsupr( LPWSTR str )
68 return struprW( str );
72 /*********************************************************************
73 * towlower (NTDLL.@)
75 WCHAR __cdecl NTDLL_towlower( WCHAR ch )
77 return tolowerW(ch);
81 /*********************************************************************
82 * towupper (NTDLL.@)
84 WCHAR __cdecl NTDLL_towupper( WCHAR ch )
86 return toupperW(ch);
90 /***********************************************************************
91 * wcscat (NTDLL.@)
93 LPWSTR __cdecl NTDLL_wcscat( LPWSTR dst, LPCWSTR src )
95 return strcatW( dst, src );
99 /*********************************************************************
100 * wcschr (NTDLL.@)
102 LPWSTR __cdecl NTDLL_wcschr( LPCWSTR str, WCHAR ch )
104 return strchrW( str, ch );
108 /*********************************************************************
109 * wcscmp (NTDLL.@)
111 INT __cdecl NTDLL_wcscmp( LPCWSTR str1, LPCWSTR str2 )
113 return strcmpW( str1, str2 );
117 /***********************************************************************
118 * wcscpy (NTDLL.@)
120 LPWSTR __cdecl NTDLL_wcscpy( LPWSTR dst, LPCWSTR src )
122 return strcpyW( dst, src );
126 /*********************************************************************
127 * wcscspn (NTDLL.@)
129 INT __cdecl NTDLL_wcscspn( LPCWSTR str, LPCWSTR reject )
131 return strcspnW( str, reject );
135 /***********************************************************************
136 * wcslen (NTDLL.@)
138 INT __cdecl NTDLL_wcslen( LPCWSTR str )
140 return strlenW( str );
144 /*********************************************************************
145 * wcsncat (NTDLL.@)
147 LPWSTR __cdecl NTDLL_wcsncat( LPWSTR s1, LPCWSTR s2, INT n )
149 LPWSTR ret = s1;
150 while (*s1) s1++;
151 while (n-- > 0) if (!(*s1++ = *s2++)) return ret;
152 *s1 = 0;
153 return ret;
157 /*********************************************************************
158 * wcsncmp (NTDLL.@)
160 INT __cdecl NTDLL_wcsncmp( LPCWSTR str1, LPCWSTR str2, INT n )
162 return strncmpW( str1, str2, n );
166 /*********************************************************************
167 * wcsncpy (NTDLL.@)
169 LPWSTR __cdecl NTDLL_wcsncpy( LPWSTR s1, LPCWSTR s2, INT n )
171 WCHAR *ret = s1;
172 while (n-- > 0) if (!(*s1++ = *s2++)) break;
173 while (n-- > 0) *s1++ = 0;
174 return ret;
178 /*********************************************************************
179 * wcspbrk (NTDLL.@)
181 LPWSTR __cdecl NTDLL_wcspbrk( LPCWSTR str, LPCWSTR accept )
183 return strpbrkW( str, accept );
187 /*********************************************************************
188 * wcsrchr (NTDLL.@)
190 LPWSTR __cdecl NTDLL_wcsrchr( LPWSTR str, WCHAR ch )
192 return strrchrW( str, ch );
196 /*********************************************************************
197 * wcsspn (NTDLL.@)
199 INT __cdecl NTDLL_wcsspn( LPCWSTR str, LPCWSTR accept )
201 return strspnW( str, accept );
205 /*********************************************************************
206 * wcsstr (NTDLL.@)
208 LPWSTR __cdecl NTDLL_wcsstr( LPCWSTR str, LPCWSTR sub )
210 return strstrW( str, sub );
214 /*********************************************************************
215 * wcstok (NTDLL.@)
217 LPWSTR __cdecl NTDLL_wcstok( LPWSTR str, LPCWSTR delim )
219 static LPWSTR next = NULL;
220 LPWSTR ret;
222 if (!str)
223 if (!(str = next)) return NULL;
225 while (*str && NTDLL_wcschr( delim, *str )) str++;
226 if (!*str) return NULL;
227 ret = str++;
228 while (*str && !NTDLL_wcschr( delim, *str )) str++;
229 if (*str) *str++ = 0;
230 next = str;
231 return ret;
235 /*********************************************************************
236 * wcstombs (NTDLL.@)
238 INT __cdecl NTDLL_wcstombs( LPSTR dst, LPCWSTR src, INT n )
240 DWORD len;
242 if (!dst)
244 RtlUnicodeToMultiByteSize( &len, src, strlenW(src)*sizeof(WCHAR) );
245 return len;
247 else
249 if (n <= 0) return 0;
250 RtlUnicodeToMultiByteN( dst, n, &len, src, strlenW(src)*sizeof(WCHAR) );
251 if (len < n) dst[len] = 0;
253 return len;
257 /*********************************************************************
258 * mbstowcs (NTDLL.@)
260 INT __cdecl NTDLL_mbstowcs( LPWSTR dst, LPCSTR src, INT n )
262 DWORD len;
264 if (!dst)
266 RtlMultiByteToUnicodeSize( &len, src, strlen(src) );
268 else
270 if (n <= 0) return 0;
271 RtlMultiByteToUnicodeN( dst, n*sizeof(WCHAR), &len, src, strlen(src) );
272 if (len / sizeof(WCHAR) < n) dst[len / sizeof(WCHAR)] = 0;
274 return len / sizeof(WCHAR);
278 /*********************************************************************
279 * wcstol (NTDLL.@)
281 LONG __cdecl NTDLL_wcstol(LPCWSTR s, LPWSTR *end, INT base)
283 return strtolW( s, end, base );
287 /*********************************************************************
288 * wcstoul (NTDLL.@)
290 ULONG __cdecl NTDLL_wcstoul(LPCWSTR s, LPWSTR *end, INT base)
292 return strtoulW( s, end, base );
296 /*********************************************************************
297 * iswctype (NTDLL.@)
299 INT __cdecl NTDLL_iswctype( WCHAR wc, WCHAR wct )
301 return (get_char_typeW(wc) & 0xfff) & wct;
305 /*********************************************************************
306 * iswalpha (NTDLL.@)
308 * Checks if a unicode char wc is a letter
310 * RETURNS
311 * TRUE: The unicode char wc is a letter.
312 * FALSE: Otherwise
314 INT __cdecl NTDLL_iswalpha( WCHAR wc )
316 return isalphaW(wc);
320 /*********************************************************************
321 * iswdigit (NTDLL.@)
323 * Checks if a unicode char wc is a digit
325 * RETURNS
326 * TRUE: The unicode char wc is a digit.
327 * FALSE: Otherwise
329 INT __cdecl NTDLL_iswdigit( WCHAR wc )
331 return isdigitW(wc);
335 /*********************************************************************
336 * iswlower (NTDLL.@)
338 * Checks if a unicode char wc is a lower case letter
340 * RETURNS
341 * TRUE: The unicode char wc is a lower case letter.
342 * FALSE: Otherwise
344 INT __cdecl NTDLL_iswlower( WCHAR wc )
346 return islowerW(wc);
350 /*********************************************************************
351 * iswspace (NTDLL.@)
353 * Checks if a unicode char wc is a white space character
355 * RETURNS
356 * TRUE: The unicode char wc is a white space character.
357 * FALSE: Otherwise
359 INT __cdecl NTDLL_iswspace( WCHAR wc )
361 return isspaceW(wc);
365 /*********************************************************************
366 * iswxdigit (NTDLL.@)
368 * Checks if a unicode char wc is an extended digit
370 * RETURNS
371 * TRUE: The unicode char wc is an extended digit.
372 * FALSE: Otherwise
374 INT __cdecl NTDLL_iswxdigit( WCHAR wc )
376 return isxdigitW(wc);
380 /*********************************************************************
381 * _ultow (NTDLL.@)
383 * Converts an unsigned long integer to a unicode string.
385 * RETURNS
386 * Always returns str.
388 * NOTES
389 * Converts value to a '\0' terminated wstring which is copied to str.
390 * The maximum length of the copied str is 33 bytes.
391 * Does not check if radix is in the range of 2 to 36.
392 * If str is NULL it just returns NULL.
394 LPWSTR __cdecl _ultow(
395 ULONG value, /* [I] Value to be converted */
396 LPWSTR str, /* [O] Destination for the converted value */
397 INT radix) /* [I] Number base for conversion */
399 WCHAR buffer[33];
400 PWCHAR pos;
401 WCHAR digit;
403 pos = &buffer[32];
404 *pos = '\0';
406 do {
407 digit = value % radix;
408 value = value / radix;
409 if (digit < 10) {
410 *--pos = '0' + digit;
411 } else {
412 *--pos = 'a' + digit - 10;
413 } /* if */
414 } while (value != 0L);
416 if (str != NULL) {
417 memcpy(str, pos, (&buffer[32] - pos + 1) * sizeof(WCHAR));
418 } /* if */
419 return str;
423 /*********************************************************************
424 * _ltow (NTDLL.@)
426 * Converts a long integer to a unicode string.
428 * RETURNS
429 * Always returns str.
431 * NOTES
432 * Converts value to a '\0' terminated wstring which is copied to str.
433 * The maximum length of the copied str is 33 bytes. If radix
434 * is 10 and value is negative, the value is converted with sign.
435 * Does not check if radix is in the range of 2 to 36.
436 * If str is NULL it just returns NULL.
438 LPWSTR __cdecl _ltow(
439 LONG value, /* [I] Value to be converted */
440 LPWSTR str, /* [O] Destination for the converted value */
441 INT radix) /* [I] Number base for conversion */
443 ULONG val;
444 int negative;
445 WCHAR buffer[33];
446 PWCHAR pos;
447 WCHAR digit;
449 if (value < 0 && radix == 10) {
450 negative = 1;
451 val = -value;
452 } else {
453 negative = 0;
454 val = value;
455 } /* if */
457 pos = &buffer[32];
458 *pos = '\0';
460 do {
461 digit = val % radix;
462 val = val / radix;
463 if (digit < 10) {
464 *--pos = '0' + digit;
465 } else {
466 *--pos = 'a' + digit - 10;
467 } /* if */
468 } while (val != 0L);
470 if (negative) {
471 *--pos = '-';
472 } /* if */
474 if (str != NULL) {
475 memcpy(str, pos, (&buffer[32] - pos + 1) * sizeof(WCHAR));
476 } /* if */
477 return str;
481 /*********************************************************************
482 * _itow (NTDLL.@)
484 * Converts an integer to a unicode string.
486 * RETURNS
487 * Always returns str.
489 * NOTES
490 * Converts value to a '\0' terminated wstring which is copied to str.
491 * The maximum length of the copied str is 33 bytes. If radix
492 * is 10 and value is negative, the value is converted with sign.
493 * Does not check if radix is in the range of 2 to 36.
494 * If str is NULL it just returns NULL.
496 * DIFFERENCES
497 * - The native function crashes when the string is longer than 19 chars.
498 * This function does not have this bug.
500 LPWSTR __cdecl _itow(
501 int value, /* [I] Value to be converted */
502 LPWSTR str, /* [O] Destination for the converted value */
503 INT radix) /* [I] Number base for conversion */
505 return _ltow(value, str, radix);
509 /*********************************************************************
510 * _ui64tow (NTDLL.@)
512 * Converts a large unsigned integer to a unicode string.
514 * RETURNS
515 * Always returns str.
517 * NOTES
518 * Converts value to a '\0' terminated wstring which is copied to str.
519 * The maximum length of the copied str is 33 bytes.
520 * Does not check if radix is in the range of 2 to 36.
521 * If str is NULL it just returns NULL.
523 * DIFFERENCES
524 * - This function does not exist in the native DLL (but in msvcrt).
525 * But since the maintenance of all these functions is better done
526 * in one place we implement it here.
528 LPWSTR __cdecl _ui64tow(
529 ULONGLONG value, /* [I] Value to be converted */
530 LPWSTR str, /* [O] Destination for the converted value */
531 INT radix) /* [I] Number base for conversion */
533 WCHAR buffer[65];
534 PWCHAR pos;
535 WCHAR digit;
537 pos = &buffer[64];
538 *pos = '\0';
540 do {
541 digit = value % radix;
542 value = value / radix;
543 if (digit < 10) {
544 *--pos = '0' + digit;
545 } else {
546 *--pos = 'a' + digit - 10;
547 } /* if */
548 } while (value != 0L);
550 if (str != NULL) {
551 memcpy(str, pos, (&buffer[64] - pos + 1) * sizeof(WCHAR));
552 } /* if */
553 return str;
557 /*********************************************************************
558 * _i64tow (NTDLL.@)
560 * Converts a large integer to a unicode string.
562 * RETURNS
563 * Always returns str.
565 * NOTES
566 * Converts value to a '\0' terminated wstring which is copied to str.
567 * The maximum length of the copied str is 33 bytes. If radix
568 * is 10 and value is negative, the value is converted with sign.
569 * Does not check if radix is in the range of 2 to 36.
570 * If str is NULL it just returns NULL.
572 * DIFFERENCES
573 * - The native DLL converts negative values (for base 10) wrong:
574 * -1 is converted to -18446744073709551615
575 * -2 is converted to -18446744073709551614
576 * -9223372036854775807 is converted to -9223372036854775809
577 * -9223372036854775808 is converted to -9223372036854775808
578 * The native msvcrt _i64tow function and our ntdll function do
579 * not have this bug.
581 LPWSTR __cdecl _i64tow(
582 LONGLONG value, /* [I] Value to be converted */
583 LPWSTR str, /* [O] Destination for the converted value */
584 INT radix) /* [I] Number base for conversion */
586 ULONGLONG val;
587 int negative;
588 WCHAR buffer[65];
589 PWCHAR pos;
590 WCHAR digit;
592 if (value < 0 && radix == 10) {
593 negative = 1;
594 val = -value;
595 } else {
596 negative = 0;
597 val = value;
598 } /* if */
600 pos = &buffer[64];
601 *pos = '\0';
603 do {
604 digit = val % radix;
605 val = val / radix;
606 if (digit < 10) {
607 *--pos = '0' + digit;
608 } else {
609 *--pos = 'a' + digit - 10;
610 } /* if */
611 } while (val != 0L);
613 if (negative) {
614 *--pos = '-';
615 } /* if */
617 if (str != NULL) {
618 memcpy(str, pos, (&buffer[64] - pos + 1) * sizeof(WCHAR));
619 } /* if */
620 return str;
624 /*********************************************************************
625 * _wtol (NTDLL.@)
627 * Converts a unicode string to a long integer.
629 * PARAMS
630 * str [I] Wstring to be converted
632 * RETURNS
633 * On success it returns the integer value otherwise it returns 0.
635 * NOTES
636 * Accepts: {whitespace} [+|-] {digits}
637 * No check is made for value overflow, only the lower 32 bits are assigned.
638 * If str is NULL it crashes, as the native function does.
640 LONG __cdecl _wtol( LPCWSTR str )
642 ULONG RunningTotal = 0;
643 char bMinus = 0;
645 while (isspaceW(*str)) {
646 str++;
647 } /* while */
649 if (*str == '+') {
650 str++;
651 } else if (*str == '-') {
652 bMinus = 1;
653 str++;
654 } /* if */
656 while (*str >= '0' && *str <= '9') {
657 RunningTotal = RunningTotal * 10 + *str - '0';
658 str++;
659 } /* while */
661 return bMinus ? -RunningTotal : RunningTotal;
665 /*********************************************************************
666 * _wtoi (NTDLL.@)
668 * Converts a unicode string to an integer.
670 * PARAMS
671 * str [I] Wstring to be converted
673 * RETURNS
674 * On success it returns the integer value otherwise it returns 0.
676 * NOTES
677 * Accepts: {whitespace} [+|-] {digits}
678 * No check is made for value overflow, only the lower 32 bits are assigned.
679 * If str is NULL it crashes, as the native function does.
681 int __cdecl _wtoi( LPCWSTR str )
683 return _wtol(str);
687 /*********************************************************************
688 * _wtoi64 (NTDLL.@)
690 * Converts a unicode string to a large integer.
692 * PARAMS
693 * str [I] Wstring to be converted
695 * RETURNS
696 * On success it returns the integer value otherwise it returns 0.
698 * NOTES
699 * Accepts: {whitespace} [+|-] {digits}
700 * No check is made for value overflow, only the lower 64 bits are assigned.
701 * If str is NULL it crashes, as the native function does.
703 LONGLONG __cdecl _wtoi64( LPCWSTR str )
705 ULONGLONG RunningTotal = 0;
706 char bMinus = 0;
708 while (isspaceW(*str)) {
709 str++;
710 } /* while */
712 if (*str == '+') {
713 str++;
714 } else if (*str == '-') {
715 bMinus = 1;
716 str++;
717 } /* if */
719 while (*str >= '0' && *str <= '9') {
720 RunningTotal = RunningTotal * 10 + *str - '0';
721 str++;
722 } /* while */
724 return bMinus ? -RunningTotal : RunningTotal;
728 /***********************************************************************
729 * _snwprintf (NTDLL.@)
731 int __cdecl _snwprintf(WCHAR *str, unsigned int len, const WCHAR *format, ...)
733 int retval;
734 va_list valist;
735 va_start(valist, format);
736 retval = vsnprintfW(str, len, format, valist);
737 va_end(valist);
738 return retval;
742 /***********************************************************************
743 * swprintf (NTDLL.@)
745 int __cdecl NTDLL_swprintf(WCHAR *str, const WCHAR *format, ...)
747 int retval;
748 va_list valist;
749 va_start(valist, format);
750 retval = vsnprintfW(str, INT_MAX, format, valist);
751 va_end(valist);
752 return retval;
756 /***********************************************************************
757 * _vsnwprintf (NTDLL.@)
759 int __cdecl _vsnwprintf( WCHAR *str, unsigned int len, const WCHAR *format, va_list args )
761 return vsnprintfW( str, len, format, args );