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
35 #include "wine/unicode.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
41 /*********************************************************************
44 INT __cdecl NTDLL__wcsicmp( LPCWSTR str1, LPCWSTR str2 )
46 return strcmpiW( str1, str2 );
50 /*********************************************************************
53 LPWSTR __cdecl NTDLL__wcslwr( LPWSTR str )
55 return strlwrW( str );
59 /*********************************************************************
62 INT __cdecl NTDLL__wcsnicmp( LPCWSTR str1, LPCWSTR str2, INT n )
64 return strncmpiW( str1, str2, n );
68 /*********************************************************************
71 LPWSTR __cdecl NTDLL__wcsupr( LPWSTR str )
73 return struprW( str );
77 /*********************************************************************
80 WCHAR __cdecl NTDLL_towlower( WCHAR ch )
86 /*********************************************************************
89 WCHAR __cdecl NTDLL_towupper( WCHAR ch )
95 /***********************************************************************
98 LPWSTR __cdecl NTDLL_wcscat( LPWSTR dst, LPCWSTR src )
100 return strcatW( dst, src );
104 /*********************************************************************
107 LPWSTR __cdecl NTDLL_wcschr( LPCWSTR str, WCHAR ch )
109 return strchrW( str, ch );
113 /*********************************************************************
116 INT __cdecl NTDLL_wcscmp( LPCWSTR str1, LPCWSTR str2 )
118 return strcmpW( str1, str2 );
122 /***********************************************************************
125 LPWSTR __cdecl NTDLL_wcscpy( LPWSTR dst, LPCWSTR src )
127 return strcpyW( dst, src );
131 /*********************************************************************
134 INT __cdecl NTDLL_wcscspn( LPCWSTR str, LPCWSTR reject )
140 while (*p && (*p != *str)) p++;
148 /***********************************************************************
151 INT __cdecl NTDLL_wcslen( LPCWSTR str )
153 return strlenW( str );
157 /*********************************************************************
160 LPWSTR __cdecl NTDLL_wcsncat( LPWSTR s1, LPCWSTR s2, INT n )
164 while (n-- > 0) if (!(*s1++ = *s2++)) return ret;
170 /*********************************************************************
173 INT __cdecl NTDLL_wcsncmp( LPCWSTR str1, LPCWSTR str2, INT n )
175 return strncmpW( str1, str2, n );
179 /*********************************************************************
182 LPWSTR __cdecl NTDLL_wcsncpy( LPWSTR s1, LPCWSTR s2, INT n )
185 while (n-- > 0) if (!(*s1++ = *s2++)) break;
186 while (n-- > 0) *s1++ = 0;
191 /*********************************************************************
194 LPWSTR __cdecl NTDLL_wcspbrk( LPCWSTR str, LPCWSTR accept )
199 for (p = accept; *p; p++) if (*p == *str) return (LPWSTR)str;
206 /*********************************************************************
209 LPWSTR __cdecl NTDLL_wcsrchr( LPWSTR str, WCHAR ch )
214 if (*str == ch) last = str;
221 /*********************************************************************
224 INT __cdecl NTDLL_wcsspn( LPCWSTR str, LPCWSTR accept )
230 while (*p && (*p != *str)) p++;
238 /*********************************************************************
241 LPWSTR __cdecl NTDLL_wcsstr( LPCWSTR str, LPCWSTR sub )
243 return strstrW( str, sub );
247 /*********************************************************************
250 LPWSTR __cdecl NTDLL_wcstok( LPWSTR str, LPCWSTR delim )
252 static LPWSTR next = NULL;
256 if (!(str = next)) return NULL;
258 while (*str && NTDLL_wcschr( delim, *str )) str++;
259 if (!*str) return NULL;
261 while (*str && !NTDLL_wcschr( delim, *str )) str++;
262 if (*str) *str++ = 0;
268 /*********************************************************************
271 INT __cdecl NTDLL_wcstombs( LPSTR dst, LPCWSTR src, INT n )
277 RtlUnicodeToMultiByteSize( &len, src, strlenW(src)*sizeof(WCHAR) );
282 if (n <= 0) return 0;
283 RtlUnicodeToMultiByteN( dst, n, &len, src, strlenW(src)*sizeof(WCHAR) );
284 if (len < n) dst[len] = 0;
290 /*********************************************************************
293 INT __cdecl NTDLL_mbstowcs( LPWSTR dst, LPCSTR src, INT n )
299 RtlMultiByteToUnicodeSize( &len, src, strlen(src) );
303 if (n <= 0) return 0;
304 RtlMultiByteToUnicodeN( dst, n*sizeof(WCHAR), &len, src, strlen(src) );
305 if (len / sizeof(WCHAR) < n) dst[len / sizeof(WCHAR)] = 0;
307 return len / sizeof(WCHAR);
311 /*********************************************************************
314 long __cdecl NTDLL_wcstol(LPCWSTR s,LPWSTR *end,INT base)
316 return strtolW( s, end, base );
320 /*********************************************************************
323 unsigned long __cdecl NTDLL_wcstoul(LPCWSTR s,LPWSTR *end,INT base)
325 return strtoulW( s, end, base );
329 /*********************************************************************
332 INT __cdecl NTDLL_iswctype( WCHAR wc, WCHAR wct )
334 return (get_char_typeW(wc) & 0xfff) & wct;
338 /*********************************************************************
341 * Checks if an unicode char wc is a letter
344 * TRUE: The unicode char wc is a letter.
347 INT __cdecl NTDLL_iswalpha( WCHAR wc )
353 /*********************************************************************
356 * Checks if an unicode char wc is a digit
359 * TRUE: The unicode char wc is a digit.
362 INT __cdecl NTDLL_iswdigit( WCHAR wc )
368 /*********************************************************************
371 * Checks if an unicode char wc is a lower case letter
374 * TRUE: The unicode char wc is a lower case letter.
377 INT __cdecl NTDLL_iswlower( WCHAR wc )
383 /*********************************************************************
386 * Checks if an unicode char wc is a white space character
389 * TRUE: The unicode char wc is a white space character.
392 INT __cdecl NTDLL_iswspace( WCHAR wc )
398 /*********************************************************************
399 * iswxdigit (NTDLL.@)
401 * Checks if an unicode char wc is an extended digit
404 * TRUE: The unicode char wc is an extended digit.
407 INT __cdecl NTDLL_iswxdigit( WCHAR wc )
409 return isxdigitW(wc);
413 /*********************************************************************
416 * Converts an unsigned long integer to an unicode string.
419 * Always returns str.
422 * Converts value to a '\0' terminated wstring which is copied to str.
423 * The maximum length of the copied str is 33 bytes.
424 * Does not check if radix is in the range of 2 to 36.
425 * If str is NULL it just returns NULL.
427 LPWSTR __cdecl _ultow(
428 unsigned long value, /* [I] Value to be converted */
429 LPWSTR str, /* [O] Destination for the converted value */
430 INT radix) /* [I] Number base for conversion */
440 digit = value % radix;
441 value = value / radix;
443 *--pos = '0' + digit;
445 *--pos = 'a' + digit - 10;
447 } while (value != 0L);
450 memcpy(str, pos, (&buffer[32] - pos + 1) * sizeof(WCHAR));
456 /*********************************************************************
459 * Converts a long integer to an unicode string.
462 * Always returns str.
465 * Converts value to a '\0' terminated wstring which is copied to str.
466 * The maximum length of the copied str is 33 bytes. If radix
467 * is 10 and value is negative, the value is converted with sign.
468 * Does not check if radix is in the range of 2 to 36.
469 * If str is NULL it just returns NULL.
471 LPWSTR __cdecl _ltow(
472 long value, /* [I] Value to be converted */
473 LPWSTR str, /* [O] Destination for the converted value */
474 INT radix) /* [I] Number base for conversion */
482 if (value < 0 && radix == 10) {
497 *--pos = '0' + digit;
499 *--pos = 'a' + digit - 10;
508 memcpy(str, pos, (&buffer[32] - pos + 1) * sizeof(WCHAR));
514 /*********************************************************************
517 * Converts an integer to an unicode string.
520 * Always returns str.
523 * Converts value to a '\0' terminated wstring which is copied to str.
524 * The maximum length of the copied str is 33 bytes. If radix
525 * is 10 and value is negative, the value is converted with sign.
526 * Does not check if radix is in the range of 2 to 36.
527 * If str is NULL it just returns NULL.
530 * - The native function crashes when the string is longer than 19 chars.
531 * This function does not have this bug.
533 LPWSTR __cdecl _itow(
534 int value, /* [I] Value to be converted */
535 LPWSTR str, /* [O] Destination for the converted value */
536 INT radix) /* [I] Number base for conversion */
538 return _ltow(value, str, radix);
542 /*********************************************************************
545 * Converts a large unsigned integer to an unicode string.
548 * Always returns str.
551 * Converts value to a '\0' terminated wstring which is copied to str.
552 * The maximum length of the copied str is 33 bytes.
553 * Does not check if radix is in the range of 2 to 36.
554 * If str is NULL it just returns NULL.
557 * - This function does not exist in the native DLL (but in msvcrt).
558 * But since the maintenance of all these functions is better done
559 * in one place we implement it here.
561 LPWSTR __cdecl _ui64tow(
562 ULONGLONG value, /* [I] Value to be converted */
563 LPWSTR str, /* [O] Destination for the converted value */
564 INT radix) /* [I] Number base for conversion */
574 digit = value % radix;
575 value = value / radix;
577 *--pos = '0' + digit;
579 *--pos = 'a' + digit - 10;
581 } while (value != 0L);
584 memcpy(str, pos, (&buffer[64] - pos + 1) * sizeof(WCHAR));
590 /*********************************************************************
593 * Converts a large integer to an unicode string.
596 * Always returns str.
599 * Converts value to a '\0' terminated wstring which is copied to str.
600 * The maximum length of the copied str is 33 bytes. If radix
601 * is 10 and value is negative, the value is converted with sign.
602 * Does not check if radix is in the range of 2 to 36.
603 * If str is NULL it just returns NULL.
606 * - The native DLL converts negative values (for base 10) wrong:
607 * -1 is converted to -18446744073709551615
608 * -2 is converted to -18446744073709551614
609 * -9223372036854775807 is converted to -9223372036854775809
610 * -9223372036854775808 is converted to -9223372036854775808
611 * The native msvcrt _i64tow function and our ntdll function do
614 LPWSTR __cdecl _i64tow(
615 LONGLONG value, /* [I] Value to be converted */
616 LPWSTR str, /* [O] Destination for the converted value */
617 INT radix) /* [I] Number base for conversion */
625 if (value < 0 && radix == 10) {
640 *--pos = '0' + digit;
642 *--pos = 'a' + digit - 10;
651 memcpy(str, pos, (&buffer[64] - pos + 1) * sizeof(WCHAR));
657 /*********************************************************************
660 * Converts an unicode string to a long integer.
663 * str [I] Wstring to be converted
666 * On success it returns the integer value otherwise it returns 0.
669 * Accepts: {whitespace} [+|-] {digits}
670 * No check is made for value overflow, only the lower 32 bits are assigned.
671 * If str is NULL it crashes, as the native function does.
673 LONG __cdecl _wtol( LPCWSTR str )
675 ULONG RunningTotal = 0;
678 while (isspaceW(*str)) {
684 } else if (*str == '-') {
689 while (*str >= '0' && *str <= '9') {
690 RunningTotal = RunningTotal * 10 + *str - '0';
694 return bMinus ? -RunningTotal : RunningTotal;
698 /*********************************************************************
701 * Converts an unicode string to an integer.
704 * str [I] Wstring to be converted
707 * On success it returns the integer value otherwise it returns 0.
710 * Accepts: {whitespace} [+|-] {digits}
711 * No check is made for value overflow, only the lower 32 bits are assigned.
712 * If str is NULL it crashes, as the native function does.
714 int __cdecl _wtoi( LPCWSTR str )
720 /*********************************************************************
723 * Converts an unicode string to a large integer.
726 * str [I] Wstring to be converted
729 * On success it returns the integer value otherwise it returns 0.
732 * Accepts: {whitespace} [+|-] {digits}
733 * No check is made for value overflow, only the lower 64 bits are assigned.
734 * If str is NULL it crashes, as the native function does.
736 LONGLONG __cdecl _wtoi64( LPCWSTR str )
738 ULONGLONG RunningTotal = 0;
741 while (isspaceW(*str)) {
747 } else if (*str == '-') {
752 while (*str >= '0' && *str <= '9') {
753 RunningTotal = RunningTotal * 10 + *str - '0';
757 return bMinus ? -RunningTotal : RunningTotal;
761 /***********************************************************************
762 * _snwprintf (NTDLL.@)
764 int __cdecl _snwprintf(WCHAR *str, unsigned int len, const WCHAR *format, ...)
768 va_start(valist, format);
769 retval = vsnprintfW(str, len, format, valist);
775 /***********************************************************************
778 int __cdecl NTDLL_swprintf(WCHAR *str, const WCHAR *format, ...)
782 va_start(valist, format);
783 retval = vsnprintfW(str, INT_MAX, format, valist);