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
37 #include "wine/unicode.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
43 /*********************************************************************
46 INT __cdecl NTDLL__wcsicmp( LPCWSTR str1, LPCWSTR str2 )
48 return strcmpiW( str1, str2 );
52 /*********************************************************************
55 LPWSTR __cdecl NTDLL__wcslwr( LPWSTR str )
57 return strlwrW( str );
61 /*********************************************************************
64 INT __cdecl NTDLL__wcsnicmp( LPCWSTR str1, LPCWSTR str2, INT n )
66 return strncmpiW( str1, str2, n );
70 /*********************************************************************
73 LPWSTR __cdecl NTDLL__wcsupr( LPWSTR str )
75 return struprW( str );
79 /*********************************************************************
82 WCHAR __cdecl NTDLL_towlower( WCHAR ch )
88 /*********************************************************************
91 WCHAR __cdecl NTDLL_towupper( WCHAR ch )
97 /***********************************************************************
100 LPWSTR __cdecl NTDLL_wcscat( LPWSTR dst, LPCWSTR src )
102 return strcatW( dst, src );
106 /*********************************************************************
109 LPWSTR __cdecl NTDLL_wcschr( LPCWSTR str, WCHAR ch )
111 return strchrW( str, ch );
115 /*********************************************************************
118 INT __cdecl NTDLL_wcscmp( LPCWSTR str1, LPCWSTR str2 )
120 return strcmpW( str1, str2 );
124 /***********************************************************************
127 LPWSTR __cdecl NTDLL_wcscpy( LPWSTR dst, LPCWSTR src )
129 return strcpyW( dst, src );
133 /*********************************************************************
136 INT __cdecl NTDLL_wcscspn( LPCWSTR str, LPCWSTR reject )
142 while (*p && (*p != *str)) p++;
150 /***********************************************************************
153 INT __cdecl NTDLL_wcslen( LPCWSTR str )
155 return strlenW( str );
159 /*********************************************************************
162 LPWSTR __cdecl NTDLL_wcsncat( LPWSTR s1, LPCWSTR s2, INT n )
166 while (n-- > 0) if (!(*s1++ = *s2++)) return ret;
172 /*********************************************************************
175 INT __cdecl NTDLL_wcsncmp( LPCWSTR str1, LPCWSTR str2, INT n )
177 return strncmpW( str1, str2, n );
181 /*********************************************************************
184 LPWSTR __cdecl NTDLL_wcsncpy( LPWSTR s1, LPCWSTR s2, INT n )
186 return strncpyW( s1, s2, n );
190 /*********************************************************************
193 LPWSTR __cdecl NTDLL_wcspbrk( LPCWSTR str, LPCWSTR accept )
198 for (p = accept; *p; p++) if (*p == *str) return (LPWSTR)str;
205 /*********************************************************************
208 LPWSTR __cdecl NTDLL_wcsrchr( LPWSTR str, WCHAR ch )
213 if (*str == ch) last = str;
220 /*********************************************************************
223 INT __cdecl NTDLL_wcsspn( LPCWSTR str, LPCWSTR accept )
229 while (*p && (*p != *str)) p++;
237 /*********************************************************************
240 LPWSTR __cdecl NTDLL_wcsstr( LPCWSTR str, LPCWSTR sub )
242 return strstrW( str, sub );
246 /*********************************************************************
249 LPWSTR __cdecl NTDLL_wcstok( LPWSTR str, LPCWSTR delim )
251 static LPWSTR next = NULL;
255 if (!(str = next)) return NULL;
257 while (*str && NTDLL_wcschr( delim, *str )) str++;
258 if (!*str) return NULL;
260 while (*str && !NTDLL_wcschr( delim, *str )) str++;
261 if (*str) *str++ = 0;
267 /*********************************************************************
270 INT __cdecl NTDLL_wcstombs( LPSTR dst, LPCWSTR src, INT n )
276 RtlUnicodeToMultiByteSize( &len, src, strlenW(src)*sizeof(WCHAR) );
281 if (n <= 0) return 0;
282 RtlUnicodeToMultiByteN( dst, n, &len, src, strlenW(src)*sizeof(WCHAR) );
283 if (len < n) dst[len] = 0;
289 /*********************************************************************
292 INT __cdecl NTDLL_mbstowcs( LPWSTR dst, LPCSTR src, INT n )
298 RtlMultiByteToUnicodeSize( &len, src, strlen(src) );
302 if (n <= 0) return 0;
303 RtlMultiByteToUnicodeN( dst, n*sizeof(WCHAR), &len, src, strlen(src) );
304 if (len / sizeof(WCHAR) < n) dst[len / sizeof(WCHAR)] = 0;
306 return len / sizeof(WCHAR);
310 /*********************************************************************
313 long __cdecl NTDLL_wcstol(LPCWSTR s,LPWSTR *end,INT base)
315 return strtolW( s, end, base );
319 /*********************************************************************
322 unsigned long __cdecl NTDLL_wcstoul(LPCWSTR s,LPWSTR *end,INT base)
324 return strtoulW( s, end, base );
328 /*********************************************************************
331 INT __cdecl NTDLL_iswctype( WCHAR wc, WCHAR wct )
333 return (get_char_typeW(wc) & 0xfff) & wct;
337 /*********************************************************************
340 * Checks if an unicode char wc is a letter
343 * TRUE: The unicode char wc is a letter.
346 INT __cdecl NTDLL_iswalpha( WCHAR wc )
352 /*********************************************************************
355 * Checks if an unicode char wc is a digit
358 * TRUE: The unicode char wc is a digit.
361 INT __cdecl NTDLL_iswdigit( WCHAR wc )
367 /*********************************************************************
370 * Checks if an unicode char wc is a lower case letter
373 * TRUE: The unicode char wc is a lower case letter.
376 INT __cdecl NTDLL_iswlower( WCHAR wc )
382 /*********************************************************************
385 * Checks if an unicode char wc is a white space character
388 * TRUE: The unicode char wc is a white space character.
391 INT __cdecl NTDLL_iswspace( WCHAR wc )
397 /*********************************************************************
398 * iswxdigit (NTDLL.@)
400 * Checks if an unicode char wc is an extended digit
403 * TRUE: The unicode char wc is an extended digit.
406 INT __cdecl NTDLL_iswxdigit( WCHAR wc )
408 return isxdigitW(wc);
412 /*********************************************************************
415 * Converts an unsigned long integer to an unicode string.
418 * Always returns str.
421 * Converts value to a '\0' terminated wstring which is copied to str.
422 * The maximum length of the copied str is 33 bytes.
423 * Does not check if radix is in the range of 2 to 36.
424 * If str is NULL it just returns NULL.
426 LPWSTR __cdecl _ultow(
427 unsigned long value, /* [I] Value to be converted */
428 LPWSTR str, /* [O] Destination for the converted value */
429 INT radix) /* [I] Number base for conversion */
439 digit = value % radix;
440 value = value / radix;
442 *--pos = '0' + digit;
444 *--pos = 'a' + digit - 10;
446 } while (value != 0L);
449 memcpy(str, pos, (&buffer[32] - pos + 1) * sizeof(WCHAR));
455 /*********************************************************************
458 * Converts a long integer to an unicode string.
461 * Always returns str.
464 * Converts value to a '\0' terminated wstring which is copied to str.
465 * The maximum length of the copied str is 33 bytes. If radix
466 * is 10 and value is negative, the value is converted with sign.
467 * Does not check if radix is in the range of 2 to 36.
468 * If str is NULL it just returns NULL.
470 LPWSTR __cdecl _ltow(
471 long value, /* [I] Value to be converted */
472 LPWSTR str, /* [O] Destination for the converted value */
473 INT radix) /* [I] Number base for conversion */
481 if (value < 0 && radix == 10) {
496 *--pos = '0' + digit;
498 *--pos = 'a' + digit - 10;
507 memcpy(str, pos, (&buffer[32] - pos + 1) * sizeof(WCHAR));
513 /*********************************************************************
516 * Converts an integer to an unicode string.
519 * Always returns str.
522 * Converts value to a '\0' terminated wstring which is copied to str.
523 * The maximum length of the copied str is 33 bytes. If radix
524 * is 10 and value is negative, the value is converted with sign.
525 * Does not check if radix is in the range of 2 to 36.
526 * If str is NULL it just returns NULL.
529 * - The native function crashes when the string is longer than 19 chars.
530 * This function does not have this bug.
532 LPWSTR __cdecl _itow(
533 int value, /* [I] Value to be converted */
534 LPWSTR str, /* [O] Destination for the converted value */
535 INT radix) /* [I] Number base for conversion */
537 return _ltow(value, str, radix);
541 /*********************************************************************
544 * Converts a large unsigned integer to an unicode string.
547 * Always returns str.
550 * Converts value to a '\0' terminated wstring which is copied to str.
551 * The maximum length of the copied str is 33 bytes.
552 * Does not check if radix is in the range of 2 to 36.
553 * If str is NULL it just returns NULL.
556 * - This function does not exist in the native DLL (but in msvcrt).
557 * But since the maintenance of all these functions is better done
558 * in one place we implement it here.
560 LPWSTR __cdecl _ui64tow(
561 ULONGLONG value, /* [I] Value to be converted */
562 LPWSTR str, /* [O] Destination for the converted value */
563 INT radix) /* [I] Number base for conversion */
573 digit = value % radix;
574 value = value / radix;
576 *--pos = '0' + digit;
578 *--pos = 'a' + digit - 10;
580 } while (value != 0L);
583 memcpy(str, pos, (&buffer[64] - pos + 1) * sizeof(WCHAR));
589 /*********************************************************************
592 * Converts a large integer to an unicode string.
595 * Always returns str.
598 * Converts value to a '\0' terminated wstring which is copied to str.
599 * The maximum length of the copied str is 33 bytes. If radix
600 * is 10 and value is negative, the value is converted with sign.
601 * Does not check if radix is in the range of 2 to 36.
602 * If str is NULL it just returns NULL.
605 * - The native DLL converts negative values (for base 10) wrong:
606 * -1 is converted to -18446744073709551615
607 * -2 is converted to -18446744073709551614
608 * -9223372036854775807 is converted to -9223372036854775809
609 * -9223372036854775808 is converted to -9223372036854775808
610 * The native msvcrt _i64tow function and our ntdll function do
613 LPWSTR __cdecl _i64tow(
614 LONGLONG value, /* [I] Value to be converted */
615 LPWSTR str, /* [O] Destination for the converted value */
616 INT radix) /* [I] Number base for conversion */
624 if (value < 0 && radix == 10) {
639 *--pos = '0' + digit;
641 *--pos = 'a' + digit - 10;
650 memcpy(str, pos, (&buffer[64] - pos + 1) * sizeof(WCHAR));
656 /*********************************************************************
659 * Converts an unicode string to a long integer.
662 * str [I] Wstring to be converted
665 * On success it returns the integer value otherwise it returns 0.
668 * Accepts: {whitespace} [+|-] {digits}
669 * No check is made for value overflow, only the lower 32 bits are assigned.
670 * If str is NULL it crashes, as the native function does.
672 LONG __cdecl _wtol( LPCWSTR str )
674 ULONG RunningTotal = 0;
677 while (isspaceW(*str)) {
683 } else if (*str == '-') {
688 while (*str >= '0' && *str <= '9') {
689 RunningTotal = RunningTotal * 10 + *str - '0';
693 return bMinus ? -RunningTotal : RunningTotal;
697 /*********************************************************************
700 * Converts an unicode string to an integer.
703 * str [I] Wstring to be converted
706 * On success it returns the integer value otherwise it returns 0.
709 * Accepts: {whitespace} [+|-] {digits}
710 * No check is made for value overflow, only the lower 32 bits are assigned.
711 * If str is NULL it crashes, as the native function does.
713 int __cdecl _wtoi( LPCWSTR str )
719 /*********************************************************************
722 * Converts an unicode string to a large integer.
725 * str [I] Wstring to be converted
728 * On success it returns the integer value otherwise it returns 0.
731 * Accepts: {whitespace} [+|-] {digits}
732 * No check is made for value overflow, only the lower 64 bits are assigned.
733 * If str is NULL it crashes, as the native function does.
735 LONGLONG __cdecl _wtoi64( LPCWSTR str )
737 ULONGLONG RunningTotal = 0;
740 while (isspaceW(*str)) {
746 } else if (*str == '-') {
751 while (*str >= '0' && *str <= '9') {
752 RunningTotal = RunningTotal * 10 + *str - '0';
756 return bMinus ? -RunningTotal : RunningTotal;
760 /***********************************************************************
761 * _snwprintf (NTDLL.@)
763 int __cdecl _snwprintf(WCHAR *str, unsigned int len, const WCHAR *format, ...)
767 va_start(valist, format);
768 retval = vsnprintfW(str, len, format, valist);
774 /***********************************************************************
777 int __cdecl NTDLL_swprintf(WCHAR *str, const WCHAR *format, ...)
781 va_start(valist, format);
782 retval = vsnprintfW(str, INT_MAX, format, valist);