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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
36 #include "wine/unicode.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
42 /*********************************************************************
45 INT __cdecl NTDLL__wcsicmp( LPCWSTR str1, LPCWSTR str2 )
47 return strcmpiW( str1, str2 );
51 /*********************************************************************
54 LPWSTR __cdecl NTDLL__wcslwr( LPWSTR str )
56 return strlwrW( str );
60 /*********************************************************************
63 INT __cdecl NTDLL__wcsnicmp( LPCWSTR str1, LPCWSTR str2, INT n )
65 return strncmpiW( str1, str2, n );
69 /*********************************************************************
72 LPWSTR __cdecl NTDLL__wcsupr( LPWSTR str )
74 return struprW( str );
78 /*********************************************************************
81 WCHAR __cdecl NTDLL_towlower( WCHAR ch )
87 /*********************************************************************
90 WCHAR __cdecl NTDLL_towupper( WCHAR ch )
96 /***********************************************************************
99 LPWSTR __cdecl NTDLL_wcscat( LPWSTR dst, LPCWSTR src )
101 return strcatW( dst, src );
105 /*********************************************************************
108 LPWSTR __cdecl NTDLL_wcschr( LPCWSTR str, WCHAR ch )
110 return strchrW( str, ch );
114 /*********************************************************************
117 INT __cdecl NTDLL_wcscmp( LPCWSTR str1, LPCWSTR str2 )
119 return strcmpW( str1, str2 );
123 /***********************************************************************
126 LPWSTR __cdecl NTDLL_wcscpy( LPWSTR dst, LPCWSTR src )
128 return strcpyW( dst, src );
132 /*********************************************************************
135 INT __cdecl NTDLL_wcscspn( LPCWSTR str, LPCWSTR reject )
141 while (*p && (*p != *str)) p++;
149 /***********************************************************************
152 INT __cdecl NTDLL_wcslen( LPCWSTR str )
154 return strlenW( str );
158 /*********************************************************************
161 LPWSTR __cdecl NTDLL_wcsncat( LPWSTR s1, LPCWSTR s2, INT n )
165 while (n-- > 0) if (!(*s1++ = *s2++)) return ret;
171 /*********************************************************************
174 INT __cdecl NTDLL_wcsncmp( LPCWSTR str1, LPCWSTR str2, INT n )
176 return strncmpW( str1, str2, n );
180 /*********************************************************************
183 LPWSTR __cdecl NTDLL_wcsncpy( LPWSTR s1, LPCWSTR s2, INT n )
185 return strncpyW( s1, s2, n );
189 /*********************************************************************
192 LPWSTR __cdecl NTDLL_wcspbrk( LPCWSTR str, LPCWSTR accept )
197 for (p = accept; *p; p++) if (*p == *str) return (LPWSTR)str;
204 /*********************************************************************
207 LPWSTR __cdecl NTDLL_wcsrchr( LPWSTR str, WCHAR ch )
212 if (*str == ch) last = str;
219 /*********************************************************************
222 INT __cdecl NTDLL_wcsspn( LPCWSTR str, LPCWSTR accept )
228 while (*p && (*p != *str)) p++;
236 /*********************************************************************
239 LPWSTR __cdecl NTDLL_wcsstr( LPCWSTR str, LPCWSTR sub )
241 return strstrW( str, sub );
245 /*********************************************************************
248 LPWSTR __cdecl NTDLL_wcstok( LPWSTR str, LPCWSTR delim )
250 static LPWSTR next = NULL;
254 if (!(str = next)) return NULL;
256 while (*str && NTDLL_wcschr( delim, *str )) str++;
257 if (!*str) return NULL;
259 while (*str && !NTDLL_wcschr( delim, *str )) str++;
260 if (*str) *str++ = 0;
266 /*********************************************************************
269 INT __cdecl NTDLL_wcstombs( LPSTR dst, LPCWSTR src, INT n )
275 RtlUnicodeToMultiByteSize( &len, src, strlenW(src)*sizeof(WCHAR) );
280 if (n <= 0) return 0;
281 RtlUnicodeToMultiByteN( dst, n, &len, src, strlenW(src)*sizeof(WCHAR) );
282 if (len < n) dst[len] = 0;
288 /*********************************************************************
291 INT __cdecl NTDLL_mbstowcs( LPWSTR dst, LPCSTR src, INT n )
297 RtlMultiByteToUnicodeSize( &len, src, strlen(src) );
301 if (n <= 0) return 0;
302 RtlMultiByteToUnicodeN( dst, n*sizeof(WCHAR), &len, src, strlen(src) );
303 if (len / sizeof(WCHAR) < n) dst[len / sizeof(WCHAR)] = 0;
305 return len / sizeof(WCHAR);
309 /*********************************************************************
312 long __cdecl NTDLL_wcstol(LPCWSTR s,LPWSTR *end,INT base)
314 return strtolW( s, end, base );
318 /*********************************************************************
321 unsigned long __cdecl NTDLL_wcstoul(LPCWSTR s,LPWSTR *end,INT base)
323 return strtoulW( s, end, base );
327 /*********************************************************************
330 INT __cdecl NTDLL_iswctype( WCHAR wc, WCHAR wct )
332 return (get_char_typeW(wc) & 0xfff) & wct;
336 /*********************************************************************
339 * Checks if an unicode char wc is a letter
342 * TRUE: The unicode char wc is a letter.
345 INT __cdecl NTDLL_iswalpha( WCHAR wc )
351 /*********************************************************************
354 * Checks if an unicode char wc is a digit
357 * TRUE: The unicode char wc is a digit.
360 INT __cdecl NTDLL_iswdigit( WCHAR wc )
366 /*********************************************************************
369 * Checks if an unicode char wc is a lower case letter
372 * TRUE: The unicode char wc is a lower case letter.
375 INT __cdecl NTDLL_iswlower( WCHAR wc )
381 /*********************************************************************
384 * Checks if an unicode char wc is a white space character
387 * TRUE: The unicode char wc is a white space character.
390 INT __cdecl NTDLL_iswspace( WCHAR wc )
396 /*********************************************************************
397 * iswxdigit (NTDLL.@)
399 * Checks if an unicode char wc is an extended digit
402 * TRUE: The unicode char wc is an extended digit.
405 INT __cdecl NTDLL_iswxdigit( WCHAR wc )
407 return isxdigitW(wc);
411 /*********************************************************************
414 * Converts an unsigned long integer to an unicode string.
417 * Always returns str.
420 * Converts value to a '\0' terminated wstring which is copied to str.
421 * The maximum length of the copied str is 33 bytes.
422 * Does not check if radix is in the range of 2 to 36.
423 * If str is NULL it just returns NULL.
425 LPWSTR __cdecl _ultow(
426 unsigned long value, /* [I] Value to be converted */
427 LPWSTR str, /* [O] Destination for the converted value */
428 INT radix) /* [I] Number base for conversion */
438 digit = value % radix;
439 value = value / radix;
441 *--pos = '0' + digit;
443 *--pos = 'a' + digit - 10;
445 } while (value != 0L);
448 memcpy(str, pos, (&buffer[32] - pos + 1) * sizeof(WCHAR));
454 /*********************************************************************
457 * Converts a long integer to an unicode string.
460 * Always returns str.
463 * Converts value to a '\0' terminated wstring which is copied to str.
464 * The maximum length of the copied str is 33 bytes. If radix
465 * is 10 and value is negative, the value is converted with sign.
466 * Does not check if radix is in the range of 2 to 36.
467 * If str is NULL it just returns NULL.
469 LPWSTR __cdecl _ltow(
470 long value, /* [I] Value to be converted */
471 LPWSTR str, /* [O] Destination for the converted value */
472 INT radix) /* [I] Number base for conversion */
480 if (value < 0 && radix == 10) {
495 *--pos = '0' + digit;
497 *--pos = 'a' + digit - 10;
506 memcpy(str, pos, (&buffer[32] - pos + 1) * sizeof(WCHAR));
512 /*********************************************************************
515 * Converts an integer to an unicode string.
518 * Always returns str.
521 * Converts value to a '\0' terminated wstring which is copied to str.
522 * The maximum length of the copied str is 33 bytes. If radix
523 * is 10 and value is negative, the value is converted with sign.
524 * Does not check if radix is in the range of 2 to 36.
525 * If str is NULL it just returns NULL.
528 * - The native function crashes when the string is longer than 19 chars.
529 * This function does not have this bug.
531 LPWSTR __cdecl _itow(
532 int value, /* [I] Value to be converted */
533 LPWSTR str, /* [O] Destination for the converted value */
534 INT radix) /* [I] Number base for conversion */
536 return _ltow(value, str, radix);
540 /*********************************************************************
543 * Converts a large unsigned integer to an unicode string.
546 * Always returns str.
549 * Converts value to a '\0' terminated wstring which is copied to str.
550 * The maximum length of the copied str is 33 bytes.
551 * Does not check if radix is in the range of 2 to 36.
552 * If str is NULL it just returns NULL.
555 * - This function does not exist in the native DLL (but in msvcrt).
556 * But since the maintenance of all these functions is better done
557 * in one place we implement it here.
559 LPWSTR __cdecl _ui64tow(
560 ULONGLONG value, /* [I] Value to be converted */
561 LPWSTR str, /* [O] Destination for the converted value */
562 INT radix) /* [I] Number base for conversion */
572 digit = value % radix;
573 value = value / radix;
575 *--pos = '0' + digit;
577 *--pos = 'a' + digit - 10;
579 } while (value != 0L);
582 memcpy(str, pos, (&buffer[64] - pos + 1) * sizeof(WCHAR));
588 /*********************************************************************
591 * Converts a large integer to an unicode string.
594 * Always returns str.
597 * Converts value to a '\0' terminated wstring which is copied to str.
598 * The maximum length of the copied str is 33 bytes. If radix
599 * is 10 and value is negative, the value is converted with sign.
600 * Does not check if radix is in the range of 2 to 36.
601 * If str is NULL it just returns NULL.
604 * - The native DLL converts negative values (for base 10) wrong:
605 * -1 is converted to -18446744073709551615
606 * -2 is converted to -18446744073709551614
607 * -9223372036854775807 is converted to -9223372036854775809
608 * -9223372036854775808 is converted to -9223372036854775808
609 * The native msvcrt _i64tow function and our ntdll function do
612 LPWSTR __cdecl _i64tow(
613 LONGLONG value, /* [I] Value to be converted */
614 LPWSTR str, /* [O] Destination for the converted value */
615 INT radix) /* [I] Number base for conversion */
623 if (value < 0 && radix == 10) {
638 *--pos = '0' + digit;
640 *--pos = 'a' + digit - 10;
649 memcpy(str, pos, (&buffer[64] - pos + 1) * sizeof(WCHAR));
655 /*********************************************************************
658 * Converts an unicode string to a long integer.
661 * str [I] Wstring to be converted
664 * On success it returns the integer value otherwise it returns 0.
667 * Accepts: {whitespace} [+|-] {digits}
668 * No check is made for value overflow, only the lower 32 bits are assigned.
669 * If str is NULL it crashes, as the native function does.
671 LONG __cdecl _wtol( LPCWSTR str )
673 ULONG RunningTotal = 0;
676 while (isspaceW(*str)) {
682 } else if (*str == '-') {
687 while (*str >= '0' && *str <= '9') {
688 RunningTotal = RunningTotal * 10 + *str - '0';
692 return bMinus ? -RunningTotal : RunningTotal;
696 /*********************************************************************
699 * Converts an unicode string to an integer.
702 * str [I] Wstring to be converted
705 * On success it returns the integer value otherwise it returns 0.
708 * Accepts: {whitespace} [+|-] {digits}
709 * No check is made for value overflow, only the lower 32 bits are assigned.
710 * If str is NULL it crashes, as the native function does.
712 int __cdecl _wtoi( LPCWSTR str )
718 /*********************************************************************
721 * Converts an unicode string to a large integer.
724 * str [I] Wstring to be converted
727 * On success it returns the integer value otherwise it returns 0.
730 * Accepts: {whitespace} [+|-] {digits}
731 * No check is made for value overflow, only the lower 64 bits are assigned.
732 * If str is NULL it crashes, as the native function does.
734 LONGLONG __cdecl _wtoi64( LPCWSTR str )
736 ULONGLONG RunningTotal = 0;
739 while (isspaceW(*str)) {
745 } else if (*str == '-') {
750 while (*str >= '0' && *str <= '9') {
751 RunningTotal = RunningTotal * 10 + *str - '0';
755 return bMinus ? -RunningTotal : RunningTotal;
759 /***********************************************************************
760 * _snwprintf (NTDLL.@)
762 int __cdecl _snwprintf(WCHAR *str, unsigned int len, const WCHAR *format, ...)
766 va_start(valist, format);
767 retval = vsnprintfW(str, len, format, valist);
773 /***********************************************************************
776 int __cdecl NTDLL_swprintf(WCHAR *str, const WCHAR *format, ...)
780 va_start(valist, format);
781 retval = vsnprintfW(str, INT_MAX, format, valist);