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
34 #include "wine/unicode.h"
36 /*********************************************************************
39 INT __cdecl NTDLL__wcsicmp( LPCWSTR str1, LPCWSTR str2 )
41 return strcmpiW( str1, str2 );
45 /*********************************************************************
48 LPWSTR __cdecl NTDLL__wcslwr( LPWSTR str )
50 return strlwrW( str );
54 /*********************************************************************
57 INT __cdecl NTDLL__wcsnicmp( LPCWSTR str1, LPCWSTR str2, INT n )
59 return strncmpiW( str1, str2, n );
63 /*********************************************************************
66 LPWSTR __cdecl NTDLL__wcsupr( LPWSTR str )
68 return struprW( str );
72 /*********************************************************************
75 WCHAR __cdecl NTDLL_towlower( WCHAR ch )
81 /*********************************************************************
84 WCHAR __cdecl NTDLL_towupper( WCHAR ch )
90 /***********************************************************************
93 LPWSTR __cdecl NTDLL_wcscat( LPWSTR dst, LPCWSTR src )
95 return strcatW( dst, src );
99 /*********************************************************************
102 LPWSTR __cdecl NTDLL_wcschr( LPCWSTR str, WCHAR ch )
104 return strchrW( str, ch );
108 /*********************************************************************
111 INT __cdecl NTDLL_wcscmp( LPCWSTR str1, LPCWSTR str2 )
113 return strcmpW( str1, str2 );
117 /***********************************************************************
120 LPWSTR __cdecl NTDLL_wcscpy( LPWSTR dst, LPCWSTR src )
122 return strcpyW( dst, src );
126 /*********************************************************************
129 INT __cdecl NTDLL_wcscspn( LPCWSTR str, LPCWSTR reject )
131 return strcspnW( str, reject );
135 /***********************************************************************
138 INT __cdecl NTDLL_wcslen( LPCWSTR str )
140 return strlenW( str );
144 /*********************************************************************
147 LPWSTR __cdecl NTDLL_wcsncat( LPWSTR s1, LPCWSTR s2, INT n )
151 while (n-- > 0) if (!(*s1++ = *s2++)) return ret;
157 /*********************************************************************
160 INT __cdecl NTDLL_wcsncmp( LPCWSTR str1, LPCWSTR str2, INT n )
162 return strncmpW( str1, str2, n );
166 /*********************************************************************
169 LPWSTR __cdecl NTDLL_wcsncpy( LPWSTR s1, LPCWSTR s2, INT n )
172 while (n-- > 0) if (!(*s1++ = *s2++)) break;
173 while (n-- > 0) *s1++ = 0;
178 /*********************************************************************
181 LPWSTR __cdecl NTDLL_wcspbrk( LPCWSTR str, LPCWSTR accept )
183 return strpbrkW( str, accept );
187 /*********************************************************************
190 LPWSTR __cdecl NTDLL_wcsrchr( LPWSTR str, WCHAR ch )
192 return strrchrW( str, ch );
196 /*********************************************************************
199 INT __cdecl NTDLL_wcsspn( LPCWSTR str, LPCWSTR accept )
201 return strspnW( str, accept );
205 /*********************************************************************
208 LPWSTR __cdecl NTDLL_wcsstr( LPCWSTR str, LPCWSTR sub )
210 return strstrW( str, sub );
214 /*********************************************************************
217 LPWSTR __cdecl NTDLL_wcstok( LPWSTR str, LPCWSTR delim )
219 static LPWSTR next = NULL;
223 if (!(str = next)) return NULL;
225 while (*str && NTDLL_wcschr( delim, *str )) str++;
226 if (!*str) return NULL;
228 while (*str && !NTDLL_wcschr( delim, *str )) str++;
229 if (*str) *str++ = 0;
235 /*********************************************************************
238 INT __cdecl NTDLL_wcstombs( LPSTR dst, LPCWSTR src, INT n )
244 RtlUnicodeToMultiByteSize( &len, src, strlenW(src)*sizeof(WCHAR) );
249 if (n <= 0) return 0;
250 RtlUnicodeToMultiByteN( dst, n, &len, src, strlenW(src)*sizeof(WCHAR) );
251 if (len < n) dst[len] = 0;
257 /*********************************************************************
260 INT __cdecl NTDLL_mbstowcs( LPWSTR dst, LPCSTR src, INT n )
266 RtlMultiByteToUnicodeSize( &len, src, strlen(src) );
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 /*********************************************************************
281 long __cdecl NTDLL_wcstol(LPCWSTR s,LPWSTR *end,INT base)
283 return strtolW( s, end, base );
287 /*********************************************************************
290 unsigned long __cdecl NTDLL_wcstoul(LPCWSTR s,LPWSTR *end,INT base)
292 return strtoulW( s, end, base );
296 /*********************************************************************
299 INT __cdecl NTDLL_iswctype( WCHAR wc, WCHAR wct )
301 return (get_char_typeW(wc) & 0xfff) & wct;
305 /*********************************************************************
308 * Checks if an unicode char wc is a letter
311 * TRUE: The unicode char wc is a letter.
314 INT __cdecl NTDLL_iswalpha( WCHAR wc )
320 /*********************************************************************
323 * Checks if an unicode char wc is a digit
326 * TRUE: The unicode char wc is a digit.
329 INT __cdecl NTDLL_iswdigit( WCHAR wc )
335 /*********************************************************************
338 * Checks if an unicode char wc is a lower case letter
341 * TRUE: The unicode char wc is a lower case letter.
344 INT __cdecl NTDLL_iswlower( WCHAR wc )
350 /*********************************************************************
353 * Checks if an unicode char wc is a white space character
356 * TRUE: The unicode char wc is a white space character.
359 INT __cdecl NTDLL_iswspace( WCHAR wc )
365 /*********************************************************************
366 * iswxdigit (NTDLL.@)
368 * Checks if an unicode char wc is an extended digit
371 * TRUE: The unicode char wc is an extended digit.
374 INT __cdecl NTDLL_iswxdigit( WCHAR wc )
376 return isxdigitW(wc);
380 /*********************************************************************
383 * Converts an unsigned long integer to an unicode string.
386 * Always returns str.
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 unsigned long value, /* [I] Value to be converted */
396 LPWSTR str, /* [O] Destination for the converted value */
397 INT radix) /* [I] Number base for conversion */
407 digit = value % radix;
408 value = value / radix;
410 *--pos = '0' + digit;
412 *--pos = 'a' + digit - 10;
414 } while (value != 0L);
417 memcpy(str, pos, (&buffer[32] - pos + 1) * sizeof(WCHAR));
423 /*********************************************************************
426 * Converts a long integer to an unicode string.
429 * Always returns str.
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 */
449 if (value < 0 && radix == 10) {
464 *--pos = '0' + digit;
466 *--pos = 'a' + digit - 10;
475 memcpy(str, pos, (&buffer[32] - pos + 1) * sizeof(WCHAR));
481 /*********************************************************************
484 * Converts an integer to an unicode string.
487 * Always returns str.
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.
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 /*********************************************************************
512 * Converts a large unsigned integer to an unicode string.
515 * Always returns str.
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.
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 */
541 digit = value % radix;
542 value = value / radix;
544 *--pos = '0' + digit;
546 *--pos = 'a' + digit - 10;
548 } while (value != 0L);
551 memcpy(str, pos, (&buffer[64] - pos + 1) * sizeof(WCHAR));
557 /*********************************************************************
560 * Converts a large integer to an unicode string.
563 * Always returns str.
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.
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
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 */
592 if (value < 0 && radix == 10) {
607 *--pos = '0' + digit;
609 *--pos = 'a' + digit - 10;
618 memcpy(str, pos, (&buffer[64] - pos + 1) * sizeof(WCHAR));
624 /*********************************************************************
627 * Converts an unicode string to a long integer.
630 * str [I] Wstring to be converted
633 * On success it returns the integer value otherwise it returns 0.
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;
645 while (isspaceW(*str)) {
651 } else if (*str == '-') {
656 while (*str >= '0' && *str <= '9') {
657 RunningTotal = RunningTotal * 10 + *str - '0';
661 return bMinus ? -RunningTotal : RunningTotal;
665 /*********************************************************************
668 * Converts an unicode string to an integer.
671 * str [I] Wstring to be converted
674 * On success it returns the integer value otherwise it returns 0.
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 )
687 /*********************************************************************
690 * Converts an unicode string to a large integer.
693 * str [I] Wstring to be converted
696 * On success it returns the integer value otherwise it returns 0.
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;
708 while (isspaceW(*str)) {
714 } else if (*str == '-') {
719 while (*str >= '0' && *str <= '9') {
720 RunningTotal = RunningTotal * 10 + *str - '0';
724 return bMinus ? -RunningTotal : RunningTotal;
728 /***********************************************************************
729 * _snwprintf (NTDLL.@)
731 int __cdecl _snwprintf(WCHAR *str, unsigned int len, const WCHAR *format, ...)
735 va_start(valist, format);
736 retval = vsnprintfW(str, len, format, valist);
742 /***********************************************************************
745 int __cdecl NTDLL_swprintf(WCHAR *str, const WCHAR *format, ...)
749 va_start(valist, format);
750 retval = vsnprintfW(str, INT_MAX, format, valist);
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 );