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
32 #include "wine/unicode.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
38 /*********************************************************************
41 INT __cdecl NTDLL__wcsicmp( LPCWSTR str1, LPCWSTR str2 )
43 return strcmpiW( str1, str2 );
47 /*********************************************************************
50 LPWSTR __cdecl NTDLL__wcslwr( LPWSTR str )
52 return strlwrW( str );
56 /*********************************************************************
59 INT __cdecl NTDLL__wcsnicmp( LPCWSTR str1, LPCWSTR str2, INT n )
61 return strncmpiW( str1, str2, n );
65 /*********************************************************************
68 LPWSTR __cdecl NTDLL__wcsupr( LPWSTR str )
70 return struprW( str );
74 /*********************************************************************
77 WCHAR __cdecl NTDLL_towlower( WCHAR ch )
83 /*********************************************************************
86 WCHAR __cdecl NTDLL_towupper( WCHAR ch )
92 /***********************************************************************
95 LPWSTR __cdecl NTDLL_wcscat( LPWSTR dst, LPCWSTR src )
97 return strcatW( dst, src );
101 /*********************************************************************
104 LPWSTR __cdecl NTDLL_wcschr( LPCWSTR str, WCHAR ch )
106 return strchrW( str, ch );
110 /*********************************************************************
113 INT __cdecl NTDLL_wcscmp( LPCWSTR str1, LPCWSTR str2 )
115 return strcmpW( str1, str2 );
119 /***********************************************************************
122 LPWSTR __cdecl NTDLL_wcscpy( LPWSTR dst, LPCWSTR src )
124 return strcpyW( dst, src );
128 /*********************************************************************
131 INT __cdecl NTDLL_wcscspn( LPCWSTR str, LPCWSTR reject )
137 while (*p && (*p != *str)) p++;
145 /***********************************************************************
148 INT __cdecl NTDLL_wcslen( LPCWSTR str )
150 return strlenW( str );
154 /*********************************************************************
157 LPWSTR __cdecl NTDLL_wcsncat( LPWSTR s1, LPCWSTR s2, INT n )
161 while (n-- > 0) if (!(*s1++ = *s2++)) return ret;
167 /*********************************************************************
170 INT __cdecl NTDLL_wcsncmp( LPCWSTR str1, LPCWSTR str2, INT n )
172 return strncmpW( str1, str2, n );
176 /*********************************************************************
179 LPWSTR __cdecl NTDLL_wcsncpy( LPWSTR s1, LPCWSTR s2, INT n )
181 return strncpyW( s1, s2, n );
185 /*********************************************************************
188 LPWSTR __cdecl NTDLL_wcspbrk( LPCWSTR str, LPCWSTR accept )
193 for (p = accept; *p; p++) if (*p == *str) return (LPWSTR)str;
200 /*********************************************************************
203 LPWSTR __cdecl NTDLL_wcsrchr( LPWSTR str, WCHAR ch )
208 if (*str == ch) last = str;
215 /*********************************************************************
218 INT __cdecl NTDLL_wcsspn( LPCWSTR str, LPCWSTR accept )
224 while (*p && (*p != *str)) p++;
232 /*********************************************************************
235 LPWSTR __cdecl NTDLL_wcsstr( LPCWSTR str, LPCWSTR sub )
237 return strstrW( str, sub );
241 /*********************************************************************
244 LPWSTR __cdecl NTDLL_wcstok( LPWSTR str, LPCWSTR delim )
246 static LPWSTR next = NULL;
250 if (!(str = next)) return NULL;
252 while (*str && NTDLL_wcschr( delim, *str )) str++;
253 if (!*str) return NULL;
255 while (*str && !NTDLL_wcschr( delim, *str )) str++;
256 if (*str) *str++ = 0;
262 /*********************************************************************
265 INT __cdecl NTDLL_wcstombs( LPSTR dst, LPCWSTR src, INT n )
271 RtlUnicodeToMultiByteSize( &len, src, strlenW(src)*sizeof(WCHAR) );
276 if (n <= 0) return 0;
277 RtlUnicodeToMultiByteN( dst, n, &len, src, strlenW(src)*sizeof(WCHAR) );
278 if (len < n) dst[len] = 0;
284 /*********************************************************************
287 INT __cdecl NTDLL_mbstowcs( LPWSTR dst, LPCSTR src, INT n )
293 RtlMultiByteToUnicodeSize( &len, src, strlen(src) );
297 if (n <= 0) return 0;
298 RtlMultiByteToUnicodeN( dst, n*sizeof(WCHAR), &len, src, strlen(src) );
299 if (len / sizeof(WCHAR) < n) dst[len / sizeof(WCHAR)] = 0;
301 return len / sizeof(WCHAR);
305 /*********************************************************************
308 long __cdecl NTDLL_wcstol(LPCWSTR s,LPWSTR *end,INT base)
310 return strtolW( s, end, base );
314 /*********************************************************************
317 unsigned long __cdecl NTDLL_wcstoul(LPCWSTR s,LPWSTR *end,INT base)
319 return strtoulW( s, end, base );
323 /*********************************************************************
326 INT __cdecl NTDLL_iswctype( WCHAR wc, WCHAR wct )
328 return (get_char_typeW(wc) & 0xfff) & wct;
332 /*********************************************************************
335 INT __cdecl NTDLL_iswalpha( WCHAR wc )
337 return get_char_typeW(wc) & C1_ALPHA;
341 /*********************************************************************
344 * Converts an unsigned long integer to an unicode string.
346 * Assigns a '\0' terminated string to str and returns str.
347 * Does not check if radix is in the range of 2 to 36 (as native DLL).
348 * For str == NULL just returns NULL (as native DLL).
350 LPWSTR __cdecl _ultow( unsigned long value, LPWSTR str, INT radix )
360 digit = value % radix;
361 value = value / radix;
363 *--pos = '0' + digit;
365 *--pos = 'a' + digit - 10;
367 } while (value != 0L);
370 memcpy(str, pos, (&buffer[32] - pos + 1) * sizeof(WCHAR));
376 /*********************************************************************
379 * Converts a long integer to an unicode string.
381 * Assigns a '\0' terminated string to str and returns str. If radix
382 * is 10 and value is negative, the value is converted with sign.
383 * Does not check if radix is in the range of 2 to 36 (as native DLL).
384 * For str == NULL just returns NULL (as native DLL).
386 LPWSTR __cdecl _ltow( long value, LPWSTR str, INT radix )
394 if (value < 0 && radix == 10) {
409 *--pos = '0' + digit;
411 *--pos = 'a' + digit - 10;
420 memcpy(str, pos, (&buffer[32] - pos + 1) * sizeof(WCHAR));
426 /*********************************************************************
429 * Converts an integer to an unicode string.
431 * Assigns a '\0' terminated wstring to str and returns str. If radix
432 * is 10 and value is negative, the value is converted with sign.
433 * Does not check if radix is in the range of 2 to 36 (as native DLL).
434 * For str == NULL just returns NULL (as native DLL).
437 * - The native DLL crashes when the string is longer than 19 chars.
438 * This function does not have this bug.
440 LPWSTR __cdecl _itow( int value, LPWSTR str, INT radix )
442 return _ltow(value, str, radix);
446 /*********************************************************************
449 * Converts a large unsigned integer to an unicode string.
451 * Assigns a '\0' terminated wstring to str and returns str.
452 * Does not check if radix is in the range of 2 to 36 (as native DLL).
453 * For str == NULL just returns NULL (as native DLL).
456 * - This function does not exist in the native DLL (but in msvcrt).
457 * But since the maintenance of all these functions is better done
458 * in one place we implement it here.
460 LPWSTR __cdecl _ui64tow( ULONGLONG value, LPWSTR str, INT radix )
470 digit = value % radix;
471 value = value / radix;
473 *--pos = '0' + digit;
475 *--pos = 'a' + digit - 10;
477 } while (value != 0L);
480 memcpy(str, pos, (&buffer[64] - pos + 1) * sizeof(WCHAR));
486 /*********************************************************************
489 * Converts a large integer to an unicode string.
491 * Assigns a '\0' terminated wstring to str and returns str. 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 (as native DLL).
494 * For str == NULL just returns NULL (as native DLL).
497 * - The native DLL converts negative values (for base 10) wrong:
498 * -1 is converted to -18446744073709551615
499 * -2 is converted to -18446744073709551614
500 * -9223372036854775807 is converted to -9223372036854775809
501 * -9223372036854775808 is converted to -9223372036854775808
502 * The native msvcrt _i64tow function and our ntdll function do
505 LPWSTR __cdecl _i64tow( LONGLONG value, LPWSTR str, INT radix )
513 if (value < 0 && radix == 10) {
528 *--pos = '0' + digit;
530 *--pos = 'a' + digit - 10;
539 memcpy(str, pos, (&buffer[64] - pos + 1) * sizeof(WCHAR));
545 /*********************************************************************
548 * Converts an unicode string to a long integer.
550 * On success it returns the integer value otherwise it returns 0.
551 * Accepts: {whitespace} [+|-] {digits}
552 * No check of overflow: Just assigns lower 32 bits (as native DLL).
553 * Does not check for str != NULL (as native DLL).
555 LONG __cdecl _wtol( LPWSTR str )
557 ULONG RunningTotal = 0;
560 while (isspaceW(*str)) {
566 } else if (*str == '-') {
571 while (*str >= '0' && *str <= '9') {
572 RunningTotal = RunningTotal * 10 + *str - '0';
576 return bMinus ? -RunningTotal : RunningTotal;
580 /*********************************************************************
583 * Converts an unicode string to an integer.
585 * On success it returns the integer value otherwise it returns 0.
586 * Accepts: {whitespace} [+|-] {digits}
587 * No check of overflow: Just assigns lower 32 bits (as native DLL).
588 * Does not check for str != NULL (as native DLL).
590 int __cdecl _wtoi( LPWSTR string )
592 return _wtol(string);
596 /*********************************************************************
599 * Converts an unicode string to a large integer.
601 * On success it returns the integer value otherwise it returns 0.
602 * Accepts: {whitespace} [+|-] {digits}
603 * No check of overflow: Just assigns lower 64 bits (as native DLL).
604 * Does not check for str != NULL (as native DLL).
606 LONGLONG __cdecl _wtoi64( LPWSTR str )
608 ULONGLONG RunningTotal = 0;
611 while (isspaceW(*str)) {
617 } else if (*str == '-') {
622 while (*str >= '0' && *str <= '9') {
623 RunningTotal = RunningTotal * 10 + *str - '0';
627 return bMinus ? -RunningTotal : RunningTotal;
632 /***********************************************************************
633 * _snwprintf (NTDLL.@)
635 int __cdecl _snwprintf(WCHAR *str, unsigned int len, const WCHAR *format, ...)
639 va_start(valist, format);
640 retval = vsnprintfW(str, len, format, valist);
646 /***********************************************************************
649 int __cdecl NTDLL_swprintf(WCHAR *str, const WCHAR *format, ...)
653 va_start(valist, format);
654 retval = vsnprintfW(str, INT_MAX, format, valist);