Glen: This patch gets the string conversion functions working on Windows. It also refactors repetitive code in string.cpp do greatly reduce the repetitiveness, increasing maintainability.

llvm-svn: 182026
This commit is contained in:
Howard Hinnant 2013-05-16 17:13:40 +00:00
parent 8b2a1d69cb
commit 9daaf5775c
4 changed files with 393 additions and 538 deletions

View File

@ -15,26 +15,23 @@
Functions and constants used in libc++ that are missing from the Windows C library. Functions and constants used in libc++ that are missing from the Windows C library.
*/ */
#include <__config> #include <cwchar> // mbstate_t
#include <wchar.h> // mbstate_t #include <cstdarg> // va_ macros
#include <stdio.h> // _snwprintf
#define swprintf _snwprintf #define swprintf _snwprintf
#define vswprintf _vsnwprintf #define vswprintf _vsnwprintf
#define vfscnaf fscanf
int vasprintf( char **sptr, const char *__restrict fmt , va_list ap ); extern "C" {
int vasprintf( char **sptr, const char *__restrict fmt, va_list ap );
int asprintf( char **sptr, const char *__restrict fmt, ...); int asprintf( char **sptr, const char *__restrict fmt, ...);
//int vfscanf( FILE *__restrict stream, const char *__restrict format,
// va_list arg);
size_t mbsnrtowcs( wchar_t *__restrict dst, const char **__restrict src, size_t mbsnrtowcs( wchar_t *__restrict dst, const char **__restrict src,
size_t nmc, size_t len, mbstate_t *__restrict ps ); size_t nmc, size_t len, mbstate_t *__restrict ps );
size_t wcsnrtombs( char *__restrict dst, const wchar_t **__restrict src, size_t wcsnrtombs( char *__restrict dst, const wchar_t **__restrict src,
size_t nwc, size_t len, mbstate_t *__restrict ps ); size_t nwc, size_t len, mbstate_t *__restrict ps );
}
#if defined(_MSC_VER) #if defined(_MSC_VER)
#define snprintf _snprintf #define snprintf _snprintf
#include <xlocinfo.h> #include <xlocinfo.h>
#define atoll _atoi64 #define atoll _atoi64
#define strtoll _strtoi64 #define strtoll _strtoi64
@ -85,9 +82,11 @@ _LIBCPP_ALWAYS_INLINE int __builtin_ctz( unsigned int x )
_BitScanReverse(&r, x); _BitScanReverse(&r, x);
return static_cast<int>(r); return static_cast<int>(r);
} }
// sizeof(long) == sizeof(int) on Windows // sizeof(long) == sizeof(int) on Windows
_LIBCPP_ALWAYS_INLINE int __builtin_ctzl( unsigned long x ) _LIBCPP_ALWAYS_INLINE int __builtin_ctzl( unsigned long x )
{ return __builtin_ctz( static_cast<int>(x) ); } { return __builtin_ctz( static_cast<int>(x) ); }
_LIBCPP_ALWAYS_INLINE int __builtin_ctzll( unsigned long long x ) _LIBCPP_ALWAYS_INLINE int __builtin_ctzll( unsigned long long x )
{ {
DWORD r = 0; DWORD r = 0;

File diff suppressed because it is too large Load Diff

View File

@ -9,8 +9,8 @@
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#include "support/win32/locale_win32.h" #include "support/win32/locale_win32.h"
#include <cstdarg> // va_start, va_end
#include <stdarg.h> // va_start, va_end #include <cwchar> // mbstate_t
// FIXME: base currently unused. Needs manual work to construct the new locale // FIXME: base currently unused. Needs manual work to construct the new locale
locale_t newlocale( int mask, const char * locale, locale_t /*base*/ ) locale_t newlocale( int mask, const char * locale, locale_t /*base*/ )

View File

@ -8,38 +8,55 @@
// //
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#include <support/win32/support.h> #include <cstdarg> // va_start, va_end
#include <stdarg.h> // va_start, va_end #include <cstddef> // size_t
#include <stddef.h> // size_t #include <cstdlib> // malloc
#include <stdlib.h> // malloc #include <cstdio> // vsprintf, vsnprintf
#include <stdio.h> // vsprintf, vsnprintf #include <cstring> // strcpy, wcsncpy
#include <string.h> // strcpy, wcsncpy #include <cwchar> // mbstate_t
#include <memory> // unique_ptr
int asprintf(char **sptr, const char *__restrict fmt, ...) namespace { // Private
struct free_deleter {
inline void operator()(char* p) { free(p); }
};
}
// Some of these functions aren't standard or if they conform, the name does not.
int asprintf(char **sptr, const char *__restrict format, ...)
{ {
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, format);
int result = vasprintf(sptr, fmt, ap); int result;
#ifndef _LIBCPP_NO_EXCEPTIONS
try {
#endif
result = vasprintf(sptr, format, ap);
#ifndef _LIBCPP_NO_EXCEPTIONS
} catch( ... ) {
va_end(ap);
throw;
}
#endif
va_end(ap); va_end(ap);
return result; return result;
} }
// Like sprintf, but when return value >= 0 it returns a pointer to a malloc'd string in *sptr. // Like sprintf, but when return value >= 0 it returns a pointer to a malloc'd string in *sptr.
// If return >= 0, use free to delete *sptr. // If return >= 0, use free to delete *sptr.
int vasprintf( char **sptr, const char *__restrict fmt, va_list ap ) int vasprintf( char **sptr, const char *__restrict format, va_list ap )
{ {
*sptr = NULL; *sptr = NULL;
int count = vsnprintf( NULL, 0, fmt, ap ); // Query the buffer size required. int count = _vsnprintf( NULL, 0, format, ap ); // Query the buffer size required.
if( count >= 0 ) { if( count >= 0 ) {
char* p = static_cast<char*>(malloc(count+1)); // Allocate memory for it and the terminator. std::unique_ptr<char, free_deleter> p( static_cast<char*>(malloc(count+1)) );
if ( p == NULL ) if ( ! p )
return -1; return -1;
if ( vsnprintf( p, count+1, fmt, ap ) == count ) // We should have used exactly what was required. if ( vsnprintf( p.get(), count+1, format, ap ) == count ) // We should have used exactly what was required.
*sptr = p; *sptr = p.release();
else { // Otherwise something is wrong, likely a bug in vsnprintf. If so free the memory and report the error. else // Otherwise something is wrong, likely a bug in vsnprintf. If so free the memory and report the error.
free(p); return -1; // Pointer will get automaticlaly deleted.
return -1;
}
} }
return count; return count;
@ -120,7 +137,7 @@ size_t wcsnrtombs( char *__restrict dst, const wchar_t **__restrict src,
if ( dst ) if ( dst )
result = wcrtomb_s( &char_size, dst + dest_converted, dest_remaining, c, ps); result = wcrtomb_s( &char_size, dst + dest_converted, dest_remaining, c, ps);
else else
result = wcrtomb_s( &char_size, NULL, 0, c, ps); result = wcrtomb_s( &char_size, NULL, 0, c, ps);
// If result is zero there is no error and char_size contains the size of the multi-byte-sequence converted. // If result is zero there is no error and char_size contains the size of the multi-byte-sequence converted.
// Otherwise result indicates an errno type error. // Otherwise result indicates an errno type error.
if ( result == no_error ) { if ( result == no_error ) {