This commit is contained in:
Victor Zverovich 2014-07-27 12:53:42 -07:00
parent 4c563de76f
commit 5d4803a567
4 changed files with 20 additions and 20 deletions

View File

@ -304,13 +304,13 @@ fmt::internal::UTF8ToUTF16::UTF8ToUTF16(fmt::StringRef s) {
} }
fmt::internal::UTF16ToUTF8::UTF16ToUTF8(fmt::WStringRef s) { fmt::internal::UTF16ToUTF8::UTF16ToUTF8(fmt::WStringRef s) {
if (int error_code = Convert(s)) { if (int error_code = convert(s)) {
throw WindowsError(error_code, throw WindowsError(error_code,
"cannot convert string from UTF-16 to UTF-8"); "cannot convert string from UTF-16 to UTF-8");
} }
} }
int fmt::internal::UTF16ToUTF8::Convert(fmt::WStringRef s) { int fmt::internal::UTF16ToUTF8::convert(fmt::WStringRef s) {
int length = WideCharToMultiByte(CP_UTF8, 0, s.c_str(), -1, 0, 0, 0, 0); int length = WideCharToMultiByte(CP_UTF8, 0, s.c_str(), -1, 0, 0, 0, 0);
if (length == 0) if (length == 0)
return GetLastError(); return GetLastError();
@ -333,7 +333,7 @@ void fmt::WindowsError::init(
#endif #endif
int fmt::internal::StrError( int fmt::internal::safe_strerror(
int error_code, char *&buffer, std::size_t buffer_size) FMT_NOEXCEPT(true) { int error_code, char *&buffer, std::size_t buffer_size) FMT_NOEXCEPT(true) {
assert(buffer != 0 && buffer_size != 0); assert(buffer != 0 && buffer_size != 0);
int result = 0; int result = 0;
@ -368,7 +368,7 @@ void fmt::internal::FormatSystemErrorMessage(
char *system_message = 0; char *system_message = 0;
for (;;) { for (;;) {
system_message = &buffer[0]; system_message = &buffer[0];
int result = StrError(error_code, system_message, buffer.size()); int result = safe_strerror(error_code, system_message, buffer.size());
if (result == 0) if (result == 0)
break; break;
if (result != ERANGE) { if (result != ERANGE) {
@ -400,7 +400,7 @@ void fmt::internal::FormatWinErrorMessage(
error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
reinterpret_cast<LPWSTR>(system_message.ptr()), 0, 0)) { reinterpret_cast<LPWSTR>(system_message.ptr()), 0, 0)) {
UTF16ToUTF8 utf8_message; UTF16ToUTF8 utf8_message;
if (!utf8_message.Convert(system_message.c_str())) { if (!utf8_message.convert(system_message.c_str())) {
out << message << ": " << utf8_message; out << message << ": " << utf8_message;
return; return;
} }

View File

@ -521,7 +521,7 @@ class UTF16ToUTF8 {
// Performs conversion returning a system error code instead of // Performs conversion returning a system error code instead of
// throwing exception on error. // throwing exception on error.
int Convert(WStringRef s); int convert(WStringRef s);
}; };
#endif #endif
@ -534,7 +534,7 @@ class UTF16ToUTF8 {
// ERANGE - buffer is not large enough to store the error message // ERANGE - buffer is not large enough to store the error message
// other - failure // other - failure
// Buffer should be at least of size 1. // Buffer should be at least of size 1.
int StrError(int error_code, int safe_strerror(int error_code,
char *&buffer, std::size_t buffer_size) FMT_NOEXCEPT(true); char *&buffer, std::size_t buffer_size) FMT_NOEXCEPT(true);
void FormatSystemErrorMessage( void FormatSystemErrorMessage(

View File

@ -57,11 +57,11 @@ namespace {
#ifdef _WIN32 #ifdef _WIN32
// On Windows the count argument to read and write is unsigned, so convert // On Windows the count argument to read and write is unsigned, so convert
// it from size_t preventing integer overflow. // it from size_t preventing integer overflow.
inline unsigned ConvertRWCount(std::size_t count) { inline unsigned convert_rwcount(std::size_t count) {
return count <= UINT_MAX ? static_cast<unsigned>(count) : UINT_MAX; return count <= UINT_MAX ? static_cast<unsigned>(count) : UINT_MAX;
} }
#else #else
inline std::size_t ConvertRWCount(std::size_t count) { return count; } inline std::size_t convert_rwcount(std::size_t count) { return count; }
#endif #endif
} }
@ -118,7 +118,7 @@ void fmt::File::close() {
std::streamsize fmt::File::read(void *buffer, std::size_t count) { std::streamsize fmt::File::read(void *buffer, std::size_t count) {
std::streamsize result = 0; std::streamsize result = 0;
FMT_RETRY(result, FMT_POSIX_CALL(read(fd_, buffer, ConvertRWCount(count)))); FMT_RETRY(result, FMT_POSIX_CALL(read(fd_, buffer, convert_rwcount(count))));
if (result == -1) if (result == -1)
throw SystemError(errno, "cannot read from file"); throw SystemError(errno, "cannot read from file");
return result; return result;
@ -126,7 +126,7 @@ std::streamsize fmt::File::read(void *buffer, std::size_t count) {
std::streamsize fmt::File::write(const void *buffer, std::size_t count) { std::streamsize fmt::File::write(const void *buffer, std::size_t count) {
std::streamsize result = 0; std::streamsize result = 0;
FMT_RETRY(result, FMT_POSIX_CALL(write(fd_, buffer, ConvertRWCount(count)))); FMT_RETRY(result, FMT_POSIX_CALL(write(fd_, buffer, convert_rwcount(count))));
if (result == -1) if (result == -1)
throw SystemError(errno, "cannot write to file"); throw SystemError(errno, "cannot write to file");
return result; return result;

View File

@ -410,38 +410,38 @@ TEST(UtilTest, UTF8ToUTF16Error) {
TEST(UtilTest, UTF16ToUTF8Convert) { TEST(UtilTest, UTF16ToUTF8Convert) {
fmt::internal::UTF16ToUTF8 u; fmt::internal::UTF16ToUTF8 u;
EXPECT_EQ(ERROR_INVALID_PARAMETER, u.Convert(0)); EXPECT_EQ(ERROR_INVALID_PARAMETER, u.convert(0));
} }
#endif // _WIN32 #endif // _WIN32
TEST(UtilTest, StrError) { TEST(UtilTest, StrError) {
using fmt::internal::StrError; using fmt::internal::safe_strerror;
char *message = 0; char *message = 0;
char buffer[BUFFER_SIZE]; char buffer[BUFFER_SIZE];
#ifndef NDEBUG #ifndef NDEBUG
EXPECT_DEBUG_DEATH(StrError(EDOM, message = 0, 0), "Assertion"); EXPECT_DEBUG_DEATH(safe_strerror(EDOM, message = 0, 0), "Assertion");
EXPECT_DEBUG_DEATH(StrError(EDOM, message = buffer, 0), "Assertion"); EXPECT_DEBUG_DEATH(safe_strerror(EDOM, message = buffer, 0), "Assertion");
#endif #endif
buffer[0] = 'x'; buffer[0] = 'x';
#ifdef _GNU_SOURCE #ifdef _GNU_SOURCE
// Use invalid error code to make sure that StrError returns an error // Use invalid error code to make sure that safe_strerror returns an error
// message in the buffer rather than a pointer to a static string. // message in the buffer rather than a pointer to a static string.
int error_code = -1; int error_code = -1;
#else #else
int error_code = EDOM; int error_code = EDOM;
#endif #endif
int result = StrError(error_code, message = buffer, BUFFER_SIZE); int result = safe_strerror(error_code, message = buffer, BUFFER_SIZE);
EXPECT_EQ(0, result); EXPECT_EQ(0, result);
std::size_t message_size = std::strlen(message); std::size_t message_size = std::strlen(message);
EXPECT_GE(BUFFER_SIZE - 1u, message_size); EXPECT_GE(BUFFER_SIZE - 1u, message_size);
EXPECT_EQ(GetSystemErrorMessage(error_code), message); EXPECT_EQ(GetSystemErrorMessage(error_code), message);
// StrError never uses buffer on MinGW. // safe_strerror never uses buffer on MinGW.
#ifndef __MINGW32__ #ifndef __MINGW32__
result = StrError(error_code, message = buffer, message_size); result = safe_strerror(error_code, message = buffer, message_size);
EXPECT_EQ(ERANGE, result); EXPECT_EQ(ERANGE, result);
result = StrError(error_code, message = buffer, 1); result = safe_strerror(error_code, message = buffer, 1);
EXPECT_EQ(buffer, message); // Message should point to buffer. EXPECT_EQ(buffer, message); // Message should point to buffer.
EXPECT_EQ(ERANGE, result); EXPECT_EQ(ERANGE, result);
EXPECT_STREQ("", message); EXPECT_STREQ("", message);