mirror of
https://github.com/fmtlib/fmt.git
synced 2024-11-19 20:18:49 +00:00
More fixes for https://github.com/cppformat/cppformat/issues/50.
This commit is contained in:
parent
4c563de76f
commit
5d4803a567
10
format.cc
10
format.cc
@ -304,13 +304,13 @@ fmt::internal::UTF8ToUTF16::UTF8ToUTF16(fmt::StringRef s) {
|
||||
}
|
||||
|
||||
fmt::internal::UTF16ToUTF8::UTF16ToUTF8(fmt::WStringRef s) {
|
||||
if (int error_code = Convert(s)) {
|
||||
if (int error_code = convert(s)) {
|
||||
throw WindowsError(error_code,
|
||||
"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);
|
||||
if (length == 0)
|
||||
return GetLastError();
|
||||
@ -333,7 +333,7 @@ void fmt::WindowsError::init(
|
||||
|
||||
#endif
|
||||
|
||||
int fmt::internal::StrError(
|
||||
int fmt::internal::safe_strerror(
|
||||
int error_code, char *&buffer, std::size_t buffer_size) FMT_NOEXCEPT(true) {
|
||||
assert(buffer != 0 && buffer_size != 0);
|
||||
int result = 0;
|
||||
@ -368,7 +368,7 @@ void fmt::internal::FormatSystemErrorMessage(
|
||||
char *system_message = 0;
|
||||
for (;;) {
|
||||
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)
|
||||
break;
|
||||
if (result != ERANGE) {
|
||||
@ -400,7 +400,7 @@ void fmt::internal::FormatWinErrorMessage(
|
||||
error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
||||
reinterpret_cast<LPWSTR>(system_message.ptr()), 0, 0)) {
|
||||
UTF16ToUTF8 utf8_message;
|
||||
if (!utf8_message.Convert(system_message.c_str())) {
|
||||
if (!utf8_message.convert(system_message.c_str())) {
|
||||
out << message << ": " << utf8_message;
|
||||
return;
|
||||
}
|
||||
|
4
format.h
4
format.h
@ -521,7 +521,7 @@ class UTF16ToUTF8 {
|
||||
|
||||
// Performs conversion returning a system error code instead of
|
||||
// throwing exception on error.
|
||||
int Convert(WStringRef s);
|
||||
int convert(WStringRef s);
|
||||
};
|
||||
#endif
|
||||
|
||||
@ -534,7 +534,7 @@ class UTF16ToUTF8 {
|
||||
// ERANGE - buffer is not large enough to store the error message
|
||||
// other - failure
|
||||
// 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);
|
||||
|
||||
void FormatSystemErrorMessage(
|
||||
|
8
posix.cc
8
posix.cc
@ -57,11 +57,11 @@ namespace {
|
||||
#ifdef _WIN32
|
||||
// On Windows the count argument to read and write is unsigned, so convert
|
||||
// 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;
|
||||
}
|
||||
#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
|
||||
}
|
||||
|
||||
@ -118,7 +118,7 @@ void fmt::File::close() {
|
||||
|
||||
std::streamsize fmt::File::read(void *buffer, std::size_t count) {
|
||||
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)
|
||||
throw SystemError(errno, "cannot read from file");
|
||||
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 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)
|
||||
throw SystemError(errno, "cannot write to file");
|
||||
return result;
|
||||
|
@ -410,38 +410,38 @@ TEST(UtilTest, UTF8ToUTF16Error) {
|
||||
|
||||
TEST(UtilTest, UTF16ToUTF8Convert) {
|
||||
fmt::internal::UTF16ToUTF8 u;
|
||||
EXPECT_EQ(ERROR_INVALID_PARAMETER, u.Convert(0));
|
||||
EXPECT_EQ(ERROR_INVALID_PARAMETER, u.convert(0));
|
||||
}
|
||||
#endif // _WIN32
|
||||
|
||||
TEST(UtilTest, StrError) {
|
||||
using fmt::internal::StrError;
|
||||
using fmt::internal::safe_strerror;
|
||||
char *message = 0;
|
||||
char buffer[BUFFER_SIZE];
|
||||
#ifndef NDEBUG
|
||||
EXPECT_DEBUG_DEATH(StrError(EDOM, message = 0, 0), "Assertion");
|
||||
EXPECT_DEBUG_DEATH(StrError(EDOM, message = buffer, 0), "Assertion");
|
||||
EXPECT_DEBUG_DEATH(safe_strerror(EDOM, message = 0, 0), "Assertion");
|
||||
EXPECT_DEBUG_DEATH(safe_strerror(EDOM, message = buffer, 0), "Assertion");
|
||||
#endif
|
||||
buffer[0] = 'x';
|
||||
#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.
|
||||
int error_code = -1;
|
||||
#else
|
||||
int error_code = EDOM;
|
||||
#endif
|
||||
|
||||
int result = StrError(error_code, message = buffer, BUFFER_SIZE);
|
||||
int result = safe_strerror(error_code, message = buffer, BUFFER_SIZE);
|
||||
EXPECT_EQ(0, result);
|
||||
std::size_t message_size = std::strlen(message);
|
||||
EXPECT_GE(BUFFER_SIZE - 1u, message_size);
|
||||
EXPECT_EQ(GetSystemErrorMessage(error_code), message);
|
||||
|
||||
// StrError never uses buffer on MinGW.
|
||||
// safe_strerror never uses buffer on MinGW.
|
||||
#ifndef __MINGW32__
|
||||
result = StrError(error_code, message = buffer, message_size);
|
||||
result = safe_strerror(error_code, message = buffer, message_size);
|
||||
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(ERANGE, result);
|
||||
EXPECT_STREQ("", message);
|
||||
|
Loading…
Reference in New Issue
Block a user