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
)
186 while (n
-- > 0) if (!(*s1
++ = *s2
++)) break;
187 while (n
-- > 0) *s1
++ = 0;
192 /*********************************************************************
195 LPWSTR __cdecl
NTDLL_wcspbrk( LPCWSTR str
, LPCWSTR accept
)
200 for (p
= accept
; *p
; p
++) if (*p
== *str
) return (LPWSTR
)str
;
207 /*********************************************************************
210 LPWSTR __cdecl
NTDLL_wcsrchr( LPWSTR str
, WCHAR ch
)
215 if (*str
== ch
) last
= str
;
222 /*********************************************************************
225 INT __cdecl
NTDLL_wcsspn( LPCWSTR str
, LPCWSTR accept
)
231 while (*p
&& (*p
!= *str
)) p
++;
239 /*********************************************************************
242 LPWSTR __cdecl
NTDLL_wcsstr( LPCWSTR str
, LPCWSTR sub
)
244 return strstrW( str
, sub
);
248 /*********************************************************************
251 LPWSTR __cdecl
NTDLL_wcstok( LPWSTR str
, LPCWSTR delim
)
253 static LPWSTR next
= NULL
;
257 if (!(str
= next
)) return NULL
;
259 while (*str
&& NTDLL_wcschr( delim
, *str
)) str
++;
260 if (!*str
) return NULL
;
262 while (*str
&& !NTDLL_wcschr( delim
, *str
)) str
++;
263 if (*str
) *str
++ = 0;
269 /*********************************************************************
272 INT __cdecl
NTDLL_wcstombs( LPSTR dst
, LPCWSTR src
, INT n
)
278 RtlUnicodeToMultiByteSize( &len
, src
, strlenW(src
)*sizeof(WCHAR
) );
283 if (n
<= 0) return 0;
284 RtlUnicodeToMultiByteN( dst
, n
, &len
, src
, strlenW(src
)*sizeof(WCHAR
) );
285 if (len
< n
) dst
[len
] = 0;
291 /*********************************************************************
294 INT __cdecl
NTDLL_mbstowcs( LPWSTR dst
, LPCSTR src
, INT n
)
300 RtlMultiByteToUnicodeSize( &len
, src
, strlen(src
) );
304 if (n
<= 0) return 0;
305 RtlMultiByteToUnicodeN( dst
, n
*sizeof(WCHAR
), &len
, src
, strlen(src
) );
306 if (len
/ sizeof(WCHAR
) < n
) dst
[len
/ sizeof(WCHAR
)] = 0;
308 return len
/ sizeof(WCHAR
);
312 /*********************************************************************
315 long __cdecl
NTDLL_wcstol(LPCWSTR s
,LPWSTR
*end
,INT base
)
317 return strtolW( s
, end
, base
);
321 /*********************************************************************
324 unsigned long __cdecl
NTDLL_wcstoul(LPCWSTR s
,LPWSTR
*end
,INT base
)
326 return strtoulW( s
, end
, base
);
330 /*********************************************************************
333 INT __cdecl
NTDLL_iswctype( WCHAR wc
, WCHAR wct
)
335 return (get_char_typeW(wc
) & 0xfff) & wct
;
339 /*********************************************************************
342 * Checks if an unicode char wc is a letter
345 * TRUE: The unicode char wc is a letter.
348 INT __cdecl
NTDLL_iswalpha( WCHAR wc
)
354 /*********************************************************************
357 * Checks if an unicode char wc is a digit
360 * TRUE: The unicode char wc is a digit.
363 INT __cdecl
NTDLL_iswdigit( WCHAR wc
)
369 /*********************************************************************
372 * Checks if an unicode char wc is a lower case letter
375 * TRUE: The unicode char wc is a lower case letter.
378 INT __cdecl
NTDLL_iswlower( WCHAR wc
)
384 /*********************************************************************
387 * Checks if an unicode char wc is a white space character
390 * TRUE: The unicode char wc is a white space character.
393 INT __cdecl
NTDLL_iswspace( WCHAR wc
)
399 /*********************************************************************
400 * iswxdigit (NTDLL.@)
402 * Checks if an unicode char wc is an extended digit
405 * TRUE: The unicode char wc is an extended digit.
408 INT __cdecl
NTDLL_iswxdigit( WCHAR wc
)
410 return isxdigitW(wc
);
414 /*********************************************************************
417 * Converts an unsigned long integer to an unicode string.
420 * Always returns str.
423 * Converts value to a '\0' terminated wstring which is copied to str.
424 * The maximum length of the copied str is 33 bytes.
425 * Does not check if radix is in the range of 2 to 36.
426 * If str is NULL it just returns NULL.
428 LPWSTR __cdecl
_ultow(
429 unsigned long value
, /* [I] Value to be converted */
430 LPWSTR str
, /* [O] Destination for the converted value */
431 INT radix
) /* [I] Number base for conversion */
441 digit
= value
% radix
;
442 value
= value
/ radix
;
444 *--pos
= '0' + digit
;
446 *--pos
= 'a' + digit
- 10;
448 } while (value
!= 0L);
451 memcpy(str
, pos
, (&buffer
[32] - pos
+ 1) * sizeof(WCHAR
));
457 /*********************************************************************
460 * Converts a long integer to an unicode string.
463 * Always returns str.
466 * Converts value to a '\0' terminated wstring which is copied to str.
467 * The maximum length of the copied str is 33 bytes. If radix
468 * is 10 and value is negative, the value is converted with sign.
469 * Does not check if radix is in the range of 2 to 36.
470 * If str is NULL it just returns NULL.
472 LPWSTR __cdecl
_ltow(
473 long value
, /* [I] Value to be converted */
474 LPWSTR str
, /* [O] Destination for the converted value */
475 INT radix
) /* [I] Number base for conversion */
483 if (value
< 0 && radix
== 10) {
498 *--pos
= '0' + digit
;
500 *--pos
= 'a' + digit
- 10;
509 memcpy(str
, pos
, (&buffer
[32] - pos
+ 1) * sizeof(WCHAR
));
515 /*********************************************************************
518 * Converts an integer to an unicode string.
521 * Always returns str.
524 * Converts value to a '\0' terminated wstring which is copied to str.
525 * The maximum length of the copied str is 33 bytes. If radix
526 * is 10 and value is negative, the value is converted with sign.
527 * Does not check if radix is in the range of 2 to 36.
528 * If str is NULL it just returns NULL.
531 * - The native function crashes when the string is longer than 19 chars.
532 * This function does not have this bug.
534 LPWSTR __cdecl
_itow(
535 int value
, /* [I] Value to be converted */
536 LPWSTR str
, /* [O] Destination for the converted value */
537 INT radix
) /* [I] Number base for conversion */
539 return _ltow(value
, str
, radix
);
543 /*********************************************************************
546 * Converts a large unsigned integer to an unicode string.
549 * Always returns str.
552 * Converts value to a '\0' terminated wstring which is copied to str.
553 * The maximum length of the copied str is 33 bytes.
554 * Does not check if radix is in the range of 2 to 36.
555 * If str is NULL it just returns NULL.
558 * - This function does not exist in the native DLL (but in msvcrt).
559 * But since the maintenance of all these functions is better done
560 * in one place we implement it here.
562 LPWSTR __cdecl
_ui64tow(
563 ULONGLONG value
, /* [I] Value to be converted */
564 LPWSTR str
, /* [O] Destination for the converted value */
565 INT radix
) /* [I] Number base for conversion */
575 digit
= value
% radix
;
576 value
= value
/ radix
;
578 *--pos
= '0' + digit
;
580 *--pos
= 'a' + digit
- 10;
582 } while (value
!= 0L);
585 memcpy(str
, pos
, (&buffer
[64] - pos
+ 1) * sizeof(WCHAR
));
591 /*********************************************************************
594 * Converts a large integer to an unicode string.
597 * Always returns str.
600 * Converts value to a '\0' terminated wstring which is copied to str.
601 * The maximum length of the copied str is 33 bytes. If radix
602 * is 10 and value is negative, the value is converted with sign.
603 * Does not check if radix is in the range of 2 to 36.
604 * If str is NULL it just returns NULL.
607 * - The native DLL converts negative values (for base 10) wrong:
608 * -1 is converted to -18446744073709551615
609 * -2 is converted to -18446744073709551614
610 * -9223372036854775807 is converted to -9223372036854775809
611 * -9223372036854775808 is converted to -9223372036854775808
612 * The native msvcrt _i64tow function and our ntdll function do
615 LPWSTR __cdecl
_i64tow(
616 LONGLONG value
, /* [I] Value to be converted */
617 LPWSTR str
, /* [O] Destination for the converted value */
618 INT radix
) /* [I] Number base for conversion */
626 if (value
< 0 && radix
== 10) {
641 *--pos
= '0' + digit
;
643 *--pos
= 'a' + digit
- 10;
652 memcpy(str
, pos
, (&buffer
[64] - pos
+ 1) * sizeof(WCHAR
));
658 /*********************************************************************
661 * Converts an unicode string to a long integer.
664 * str [I] Wstring to be converted
667 * On success it returns the integer value otherwise it returns 0.
670 * Accepts: {whitespace} [+|-] {digits}
671 * No check is made for value overflow, only the lower 32 bits are assigned.
672 * If str is NULL it crashes, as the native function does.
674 LONG __cdecl
_wtol( LPCWSTR str
)
676 ULONG RunningTotal
= 0;
679 while (isspaceW(*str
)) {
685 } else if (*str
== '-') {
690 while (*str
>= '0' && *str
<= '9') {
691 RunningTotal
= RunningTotal
* 10 + *str
- '0';
695 return bMinus
? -RunningTotal
: RunningTotal
;
699 /*********************************************************************
702 * Converts an unicode string to an integer.
705 * str [I] Wstring to be converted
708 * On success it returns the integer value otherwise it returns 0.
711 * Accepts: {whitespace} [+|-] {digits}
712 * No check is made for value overflow, only the lower 32 bits are assigned.
713 * If str is NULL it crashes, as the native function does.
715 int __cdecl
_wtoi( LPCWSTR str
)
721 /*********************************************************************
724 * Converts an unicode string to a large integer.
727 * str [I] Wstring to be converted
730 * On success it returns the integer value otherwise it returns 0.
733 * Accepts: {whitespace} [+|-] {digits}
734 * No check is made for value overflow, only the lower 64 bits are assigned.
735 * If str is NULL it crashes, as the native function does.
737 LONGLONG __cdecl
_wtoi64( LPCWSTR str
)
739 ULONGLONG RunningTotal
= 0;
742 while (isspaceW(*str
)) {
748 } else if (*str
== '-') {
753 while (*str
>= '0' && *str
<= '9') {
754 RunningTotal
= RunningTotal
* 10 + *str
- '0';
758 return bMinus
? -RunningTotal
: RunningTotal
;
762 /***********************************************************************
763 * _snwprintf (NTDLL.@)
765 int __cdecl
_snwprintf(WCHAR
*str
, unsigned int len
, const WCHAR
*format
, ...)
769 va_start(valist
, format
);
770 retval
= vsnprintfW(str
, len
, format
, valist
);
776 /***********************************************************************
779 int __cdecl
NTDLL_swprintf(WCHAR
*str
, const WCHAR
*format
, ...)
783 va_start(valist
, format
);
784 retval
= vsnprintfW(str
, INT_MAX
, format
, valist
);