2 * msvcrt.dll wide-char functions
4 * Copyright 1999 Alexandre Julliard
5 * Copyright 2000 Jon Griffiths
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "wine/unicode.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt
);
33 /* INTERNAL: MSVCRT_malloc() based wstrndup */
34 MSVCRT_wchar_t
* msvcrt_wstrndup(const MSVCRT_wchar_t
*buf
, unsigned int size
)
37 unsigned int len
= strlenW(buf
), max_len
;
39 max_len
= size
<= len
? size
: len
+ 1;
41 ret
= MSVCRT_malloc(max_len
* sizeof (MSVCRT_wchar_t
));
44 memcpy(ret
,buf
,max_len
* sizeof (MSVCRT_wchar_t
));
50 /*********************************************************************
53 MSVCRT_wchar_t
* CDECL
_wcsdup( const MSVCRT_wchar_t
* str
)
55 MSVCRT_wchar_t
* ret
= NULL
;
58 int size
= (strlenW(str
) + 1) * sizeof(MSVCRT_wchar_t
);
59 ret
= MSVCRT_malloc( size
);
60 if (ret
) memcpy( ret
, str
, size
);
65 /*********************************************************************
66 * _wcsicoll (MSVCRT.@)
68 INT CDECL
_wcsicoll( const MSVCRT_wchar_t
* str1
, const MSVCRT_wchar_t
* str2
)
70 /* FIXME: handle collates */
71 return strcmpiW( str1
, str2
);
74 /*********************************************************************
77 MSVCRT_wchar_t
* CDECL
_wcsnset( MSVCRT_wchar_t
* str
, MSVCRT_wchar_t c
, MSVCRT_size_t n
)
79 MSVCRT_wchar_t
* ret
= str
;
80 while ((n
-- > 0) && *str
) *str
++ = c
;
84 /*********************************************************************
87 MSVCRT_wchar_t
* CDECL
_wcsrev( MSVCRT_wchar_t
* str
)
89 MSVCRT_wchar_t
* ret
= str
;
90 MSVCRT_wchar_t
* end
= str
+ strlenW(str
) - 1;
93 MSVCRT_wchar_t t
= *end
;
100 /*********************************************************************
103 MSVCRT_wchar_t
* CDECL
_wcsset( MSVCRT_wchar_t
* str
, MSVCRT_wchar_t c
)
105 MSVCRT_wchar_t
* ret
= str
;
106 while (*str
) *str
++ = c
;
110 /*********************************************************************
113 double CDECL
MSVCRT_wcstod(const MSVCRT_wchar_t
* lpszStr
, MSVCRT_wchar_t
** end
)
115 const MSVCRT_wchar_t
* str
= lpszStr
;
117 double ret
= 0, divisor
= 10.0;
119 TRACE("(%s,%p) semi-stub\n", debugstr_w(lpszStr
), end
);
122 * - Should set errno on failure
123 * - Should fail on overflow
124 * - Need to check which input formats are allowed
126 while (isspaceW(*str
))
135 while (isdigitW(*str
))
137 ret
= ret
* 10.0 + (*str
- '0');
142 while (isdigitW(*str
))
144 ret
= ret
+ (*str
- '0') / divisor
;
149 if (*str
== 'E' || *str
== 'e' || *str
== 'D' || *str
== 'd')
151 int negativeExponent
= 0;
155 negativeExponent
= 1;
158 while (isdigitW(*str
))
160 exponent
= exponent
* 10 + (*str
- '0');
165 if (negativeExponent
)
166 ret
= ret
/ pow(10.0, exponent
);
168 ret
= ret
* pow(10.0, exponent
);
176 *end
= (MSVCRT_wchar_t
*)str
;
178 TRACE("returning %g\n", ret
);
183 typedef struct pf_output_t
194 typedef struct pf_flags_t
196 char Sign
, LeftAlign
, Alternate
, PadZero
;
197 int FieldLength
, Precision
;
198 char IntegerLength
, IntegerDouble
;
204 * writes a string of characters to the output
205 * returns -1 if the string doesn't fit in the output buffer
206 * return the length of the string if all characters were written
208 static inline int pf_output_stringW( pf_output
*out
, LPCWSTR str
, int len
)
210 int space
= out
->len
- out
->used
;
213 len
= strlenW( str
);
216 LPWSTR p
= out
->buf
.W
+ out
->used
;
220 memcpy( p
, str
, len
*sizeof(WCHAR
) );
225 memcpy( p
, str
, space
*sizeof(WCHAR
) );
230 int n
= WideCharToMultiByte( CP_ACP
, 0, str
, len
, NULL
, 0, NULL
, NULL
);
231 LPSTR p
= out
->buf
.A
+ out
->used
;
235 WideCharToMultiByte( CP_ACP
, 0, str
, len
, p
, n
, NULL
, NULL
);
240 WideCharToMultiByte( CP_ACP
, 0, str
, len
, p
, space
, NULL
, NULL
);
246 static inline int pf_output_stringA( pf_output
*out
, LPCSTR str
, int len
)
248 int space
= out
->len
- out
->used
;
254 LPSTR p
= out
->buf
.A
+ out
->used
;
258 memcpy( p
, str
, len
);
263 memcpy( p
, str
, space
);
268 int n
= MultiByteToWideChar( CP_ACP
, 0, str
, len
, NULL
, 0 );
269 LPWSTR p
= out
->buf
.W
+ out
->used
;
273 MultiByteToWideChar( CP_ACP
, 0, str
, len
, p
, n
);
278 MultiByteToWideChar( CP_ACP
, 0, str
, len
, p
, space
);
284 /* pf_fill: takes care of signs, alignment, zero and field padding */
285 static inline int pf_fill( pf_output
*out
, int len
, pf_flags
*flags
, char left
)
289 if( flags
->Sign
&& !( flags
->Format
== 'd' || flags
->Format
== 'i' ) )
292 if( left
&& flags
->Sign
)
294 flags
->FieldLength
--;
296 r
= pf_output_stringA( out
, &flags
->Sign
, 1 );
299 if( ( !left
&& flags
->LeftAlign
) ||
300 ( left
&& !flags
->LeftAlign
))
302 for( i
=0; (i
<(flags
->FieldLength
-len
)) && (r
>=0); i
++ )
304 if( left
&& flags
->PadZero
)
305 r
= pf_output_stringA( out
, "0", 1 );
307 r
= pf_output_stringA( out
, " ", 1 );
311 if( left
&& flags
->Sign
&& !flags
->PadZero
)
312 r
= pf_output_stringA( out
, &flags
->Sign
, 1 );
317 static inline int pf_output_format_W( pf_output
*out
, LPCWSTR str
,
318 int len
, pf_flags
*flags
)
323 len
= strlenW( str
);
325 if (flags
->Precision
>= 0 && flags
->Precision
< len
)
326 len
= flags
->Precision
;
328 r
= pf_fill( out
, len
, flags
, 1 );
331 r
= pf_output_stringW( out
, str
, len
);
334 r
= pf_fill( out
, len
, flags
, 0 );
339 static inline int pf_output_format_A( pf_output
*out
, LPCSTR str
,
340 int len
, pf_flags
*flags
)
347 if (flags
->Precision
>= 0 && flags
->Precision
< len
)
348 len
= flags
->Precision
;
350 r
= pf_fill( out
, len
, flags
, 1 );
353 r
= pf_output_stringA( out
, str
, len
);
356 r
= pf_fill( out
, len
, flags
, 0 );
361 static int pf_handle_string_format( pf_output
*out
, const void* str
, int len
,
362 pf_flags
*flags
, BOOL capital_letter
)
364 if(str
== NULL
) /* catch NULL pointer */
365 return pf_output_format_A( out
, "(null)", -1, flags
);
367 /* prefixes take priority over %c,%s vs. %C,%S, so we handle them first */
368 if(flags
->WideString
|| flags
->IntegerLength
== 'l')
369 return pf_output_format_W( out
, str
, len
, flags
);
370 if(flags
->IntegerLength
== 'h')
371 return pf_output_format_A( out
, str
, len
, flags
);
373 /* %s,%c -> chars in ansi functions & wchars in unicode
374 * %S,%C -> wchars in ansi functions & chars in unicode */
375 if( capital_letter
== out
->unicode
) /* either both TRUE or both FALSE */
376 return pf_output_format_A( out
, str
, len
, flags
);
378 return pf_output_format_W( out
, str
, len
, flags
);
381 static inline BOOL
pf_is_integer_format( char fmt
)
383 static const char float_fmts
[] = "diouxX";
386 return strchr( float_fmts
, fmt
) ? TRUE
: FALSE
;
389 static inline BOOL
pf_is_double_format( char fmt
)
391 static const char float_fmts
[] = "aeEfgG";
394 return strchr( float_fmts
, fmt
) ? TRUE
: FALSE
;
397 static inline BOOL
pf_is_valid_format( char fmt
)
399 static const char float_fmts
[] = "acCdeEfgGinouxX";
402 return strchr( float_fmts
, fmt
) ? TRUE
: FALSE
;
405 static void pf_rebuild_format_string( char *p
, pf_flags
*flags
)
410 if( flags
->LeftAlign
)
411 *p
++ = flags
->LeftAlign
;
412 if( flags
->Alternate
)
413 *p
++ = flags
->Alternate
;
415 *p
++ = flags
->PadZero
;
416 if( flags
->FieldLength
)
418 sprintf(p
, "%d", flags
->FieldLength
);
421 if( flags
->Precision
>= 0 )
423 sprintf(p
, ".%d", flags
->Precision
);
426 *p
++ = flags
->Format
;
430 /* pf_integer_conv: prints x to buf, including alternate formats and
431 additional precision digits, but not field characters or the sign */
432 static void pf_integer_conv( char *buf
, int buf_len
, pf_flags
*flags
,
439 char number
[40], *tmp
= number
;
441 if( buf_len
> sizeof number
)
442 tmp
= HeapAlloc( GetProcessHeap(), 0, buf_len
);
445 if( flags
->Format
== 'o' )
447 else if( flags
->Format
== 'x' || flags
->Format
== 'X' )
450 if( flags
->Format
== 'X' )
451 digits
= "0123456789ABCDEFX";
453 digits
= "0123456789abcdefx";
455 if( x
< 0 && ( flags
->Format
== 'd' || flags
->Format
== 'i' ) )
461 /* Do conversion (backwards) */
463 if( x
== 0 && flags
->Precision
)
468 j
= (ULONGLONG
) x
% base
;
469 x
= (ULONGLONG
) x
/ base
;
470 tmp
[i
++] = digits
[j
];
472 k
= flags
->Precision
- i
;
475 if( flags
->Alternate
)
479 tmp
[i
++] = digits
[16];
482 else if( base
== 8 && tmp
[i
-1] != '0' )
486 /* Reverse for buf */
492 /* Adjust precision so pf_fill won't truncate the number later */
493 flags
->Precision
= strlen( buf
);
496 HeapFree( GetProcessHeap(), 0, tmp
);
501 /*********************************************************************
502 * pf_vsnprintf (INTERNAL)
504 * implements both A and W vsnprintf functions
506 static int pf_vsnprintf( pf_output
*out
, const WCHAR
*format
, va_list valist
)
509 LPCWSTR q
, p
= format
;
512 TRACE("format is %s\n",debugstr_w(format
));
515 q
= strchrW( p
, '%' );
517 /* there's no % characters left, output the rest of the string */
520 r
= pf_output_stringW(out
, p
, -1);
527 /* there's characters before the %, output them */
530 r
= pf_output_stringW(out
, p
, q
- p
);
536 /* we must be at a % now, skip over it */
540 /* output a single % character */
543 r
= pf_output_stringW(out
, p
++, 1);
549 /* parse the flags */
550 memset( &flags
, 0, sizeof flags
);
553 if( *p
== '+' || *p
== ' ' )
555 if ( flags
.Sign
!= '+' )
559 flags
.LeftAlign
= *p
;
563 flags
.Alternate
= *p
;
569 /* deal with the field width specifier */
570 flags
.FieldLength
= 0;
573 flags
.FieldLength
= va_arg( valist
, int );
576 else while( isdigit(*p
) )
578 flags
.FieldLength
*= 10;
579 flags
.FieldLength
+= *p
++ - '0';
582 /* deal with precision */
583 flags
.Precision
= -1;
590 flags
.Precision
= va_arg( valist
, int );
593 else while( isdigit(*p
) )
595 flags
.Precision
*= 10;
596 flags
.Precision
+= *p
++ - '0';
600 /* deal with integer width modifier */
603 if( *p
== 'h' || *p
== 'l' || *p
== 'L' )
605 flags
.IntegerLength
= *p
;
610 if( *(p
+1) == '6' && *(p
+2) == '4' )
612 flags
.IntegerDouble
++;
615 else if( *(p
+1) == '3' && *(p
+2) == '2' )
617 else if( isdigit(*(p
+1)) || *(p
+1) == 0 )
623 flags
.WideString
= *p
++;
633 /* output a string */
634 if( flags
.Format
== 's' || flags
.Format
== 'S' )
635 r
= pf_handle_string_format( out
, va_arg(valist
, const void*), -1,
636 &flags
, (flags
.Format
== 'S') );
638 /* output a single character */
639 else if( flags
.Format
== 'c' || flags
.Format
== 'C' )
641 INT ch
= va_arg( valist
, int );
643 r
= pf_handle_string_format( out
, &ch
, 1, &flags
, (flags
.Format
== 'C') );
646 /* output a pointer */
647 else if( flags
.Format
== 'p' )
652 if( flags
.Alternate
)
653 sprintf(pointer
, "0X%08lX", va_arg(valist
, long));
655 sprintf(pointer
, "%08lX", va_arg(valist
, long));
656 r
= pf_output_format_A( out
, pointer
, -1, &flags
);
660 else if( flags
.Format
== 'n' )
662 int *x
= va_arg(valist
, int *);
666 /* deal with 64-bit integers */
667 else if( pf_is_integer_format( flags
.Format
) && flags
.IntegerDouble
)
669 char number
[40], *x
= number
;
671 /* Estimate largest possible required buffer size:
672 * Chooses the larger of the field or precision
673 * Includes extra bytes: 1 byte for null, 1 byte for sign,
674 4 bytes for exponent, 2 bytes for alternate formats, 1 byte
675 for a decimal, and 1 byte for an additional float digit. */
676 int x_len
= ((flags
.FieldLength
> flags
.Precision
) ?
677 flags
.FieldLength
: flags
.Precision
) + 10;
679 if( x_len
>= sizeof number
)
680 x
= HeapAlloc( GetProcessHeap(), 0, x_len
);
682 pf_integer_conv( x
, x_len
, &flags
, va_arg(valist
, LONGLONG
) );
684 r
= pf_output_format_A( out
, x
, -1, &flags
);
686 HeapFree( GetProcessHeap(), 0, x
);
689 /* deal with integers and floats using libc's printf */
690 else if( pf_is_valid_format( flags
.Format
) )
692 char fmt
[20], number
[40], *x
= number
;
694 /* Estimate largest possible required buffer size:
695 * Chooses the larger of the field or precision
696 * Includes extra bytes: 1 byte for null, 1 byte for sign,
697 4 bytes for exponent, 2 bytes for alternate formats, 1 byte
698 for a decimal, and 1 byte for an additional float digit. */
699 int x_len
= ((flags
.FieldLength
> flags
.Precision
) ?
700 flags
.FieldLength
: flags
.Precision
) + 10;
702 if( x_len
>= sizeof number
)
703 x
= HeapAlloc( GetProcessHeap(), 0, x_len
);
705 pf_rebuild_format_string( fmt
, &flags
);
707 if( pf_is_double_format( flags
.Format
) )
708 sprintf( x
, fmt
, va_arg(valist
, double) );
710 sprintf( x
, fmt
, va_arg(valist
, int) );
712 r
= pf_output_stringA( out
, x
, -1 );
714 HeapFree( GetProcessHeap(), 0, x
);
724 /* check we reached the end, and null terminate the string */
726 pf_output_stringW( out
, p
, 1 );
728 return out
->used
- 1;
731 /*********************************************************************
732 * _vsnprintf (MSVCRT.@)
734 int CDECL
MSVCRT_vsnprintf( char *str
, unsigned int len
,
735 const char *format
, va_list valist
)
738 LPWSTR formatW
= NULL
;
749 sz
= MultiByteToWideChar( CP_ACP
, 0, format
, -1, NULL
, 0 );
750 formatW
= HeapAlloc( GetProcessHeap(), 0, sz
*sizeof(WCHAR
) );
751 MultiByteToWideChar( CP_ACP
, 0, format
, -1, formatW
, sz
);
754 r
= pf_vsnprintf( &out
, formatW
, valist
);
756 HeapFree( GetProcessHeap(), 0, formatW
);
761 /*********************************************************************
762 * vsprintf (MSVCRT.@)
764 int CDECL
MSVCRT_vsprintf( char *str
, const char *format
, va_list valist
)
766 return MSVCRT_vsnprintf(str
, INT_MAX
, format
, valist
);
769 /*********************************************************************
770 * _snprintf (MSVCRT.@)
772 int CDECL
MSVCRT__snprintf(char *str
, unsigned int len
, const char *format
, ...)
776 va_start(valist
, format
);
777 retval
= MSVCRT_vsnprintf(str
, len
, format
, valist
);
782 /*********************************************************************
783 * _vsnwsprintf (MSVCRT.@)
785 int CDECL
MSVCRT_vsnwprintf( MSVCRT_wchar_t
*str
, unsigned int len
,
786 const MSVCRT_wchar_t
*format
, va_list valist
)
795 return pf_vsnprintf( &out
, format
, valist
);
798 /*********************************************************************
799 * _snwprintf (MSVCRT.@)
801 int CDECL
MSVCRT__snwprintf( MSVCRT_wchar_t
*str
, unsigned int len
, const MSVCRT_wchar_t
*format
, ...)
805 va_start(valist
, format
);
806 retval
= MSVCRT_vsnwprintf(str
, len
, format
, valist
);
811 /*********************************************************************
814 int CDECL
MSVCRT_sprintf( char *str
, const char *format
, ... )
819 va_start( ap
, format
);
820 r
= MSVCRT_vsnprintf( str
, INT_MAX
, format
, ap
);
825 /*********************************************************************
826 * swprintf (MSVCRT.@)
828 int CDECL
MSVCRT_swprintf( MSVCRT_wchar_t
*str
, const MSVCRT_wchar_t
*format
, ... )
833 va_start( ap
, format
);
834 r
= MSVCRT_vsnwprintf( str
, INT_MAX
, format
, ap
);
839 /*********************************************************************
840 * vswprintf (MSVCRT.@)
842 int CDECL
MSVCRT_vswprintf( MSVCRT_wchar_t
* str
, const MSVCRT_wchar_t
* format
, va_list args
)
844 return MSVCRT_vsnwprintf( str
, INT_MAX
, format
, args
);
847 /*********************************************************************
850 int CDECL
MSVCRT_wcscoll( const MSVCRT_wchar_t
* str1
, const MSVCRT_wchar_t
* str2
)
852 /* FIXME: handle collates */
853 return strcmpW( str1
, str2
);
856 /*********************************************************************
859 MSVCRT_wchar_t
* CDECL
MSVCRT_wcspbrk( const MSVCRT_wchar_t
* str
, const MSVCRT_wchar_t
* accept
)
861 const MSVCRT_wchar_t
* p
;
864 for (p
= accept
; *p
; p
++) if (*p
== *str
) return (MSVCRT_wchar_t
*)str
;
870 /*********************************************************************
873 MSVCRT_wchar_t
* CDECL
MSVCRT_wcstok( MSVCRT_wchar_t
*str
, const MSVCRT_wchar_t
*delim
)
875 thread_data_t
*data
= msvcrt_get_thread_data();
879 if (!(str
= data
->wcstok_next
)) return NULL
;
881 while (*str
&& strchrW( delim
, *str
)) str
++;
882 if (!*str
) return NULL
;
884 while (*str
&& !strchrW( delim
, *str
)) str
++;
885 if (*str
) *str
++ = 0;
886 data
->wcstok_next
= str
;
891 /*********************************************************************
894 INT CDECL
MSVCRT_wctomb( char *dst
, MSVCRT_wchar_t ch
)
896 return WideCharToMultiByte( CP_ACP
, 0, &ch
, 1, dst
, 6, NULL
, NULL
);
899 /*********************************************************************
900 * iswalnum (MSVCRT.@)
902 INT CDECL
MSVCRT_iswalnum( MSVCRT_wchar_t wc
)
904 return isalnumW( wc
);
907 /*********************************************************************
908 * iswalpha (MSVCRT.@)
910 INT CDECL
MSVCRT_iswalpha( MSVCRT_wchar_t wc
)
912 return isalphaW( wc
);
915 /*********************************************************************
916 * iswcntrl (MSVCRT.@)
918 INT CDECL
MSVCRT_iswcntrl( MSVCRT_wchar_t wc
)
920 return iscntrlW( wc
);
923 /*********************************************************************
924 * iswdigit (MSVCRT.@)
926 INT CDECL
MSVCRT_iswdigit( MSVCRT_wchar_t wc
)
928 return isdigitW( wc
);
931 /*********************************************************************
932 * iswgraph (MSVCRT.@)
934 INT CDECL
MSVCRT_iswgraph( MSVCRT_wchar_t wc
)
936 return isgraphW( wc
);
939 /*********************************************************************
940 * iswlower (MSVCRT.@)
942 INT CDECL
MSVCRT_iswlower( MSVCRT_wchar_t wc
)
944 return islowerW( wc
);
947 /*********************************************************************
948 * iswprint (MSVCRT.@)
950 INT CDECL
MSVCRT_iswprint( MSVCRT_wchar_t wc
)
952 return isprintW( wc
);
955 /*********************************************************************
956 * iswpunct (MSVCRT.@)
958 INT CDECL
MSVCRT_iswpunct( MSVCRT_wchar_t wc
)
960 return ispunctW( wc
);
963 /*********************************************************************
964 * iswspace (MSVCRT.@)
966 INT CDECL
MSVCRT_iswspace( MSVCRT_wchar_t wc
)
968 return isspaceW( wc
);
971 /*********************************************************************
972 * iswupper (MSVCRT.@)
974 INT CDECL
MSVCRT_iswupper( MSVCRT_wchar_t wc
)
976 return isupperW( wc
);
979 /*********************************************************************
980 * iswxdigit (MSVCRT.@)
982 INT CDECL
MSVCRT_iswxdigit( MSVCRT_wchar_t wc
)
984 return isxdigitW( wc
);