mirror of
https://github.com/fmtlib/fmt.git
synced 2024-12-27 03:21:34 +00:00
Fixed UTF8/16 converters to support empty string input
Previously an exception was thrown since Win32 WideCharToMultiByte API returns error on zero-length input.
This commit is contained in:
parent
c37c4c437e
commit
acb469ae2e
@ -283,6 +283,13 @@ FMT_FUNC internal::utf8_to_utf16::utf8_to_utf16(string_view s) {
|
|||||||
if (s.size() > INT_MAX)
|
if (s.size() > INT_MAX)
|
||||||
FMT_THROW(windows_error(ERROR_INVALID_PARAMETER, ERROR_MSG));
|
FMT_THROW(windows_error(ERROR_INVALID_PARAMETER, ERROR_MSG));
|
||||||
int s_size = static_cast<int>(s.size());
|
int s_size = static_cast<int>(s.size());
|
||||||
|
if (s_size == 0) {
|
||||||
|
// MultiByteToWideChar does not support zero length, handle separately.
|
||||||
|
buffer_.resize(1);
|
||||||
|
buffer_[0] = 0;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
int length = MultiByteToWideChar(
|
int length = MultiByteToWideChar(
|
||||||
CP_UTF8, MB_ERR_INVALID_CHARS, s.data(), s_size, FMT_NULL, 0);
|
CP_UTF8, MB_ERR_INVALID_CHARS, s.data(), s_size, FMT_NULL, 0);
|
||||||
if (length == 0)
|
if (length == 0)
|
||||||
@ -306,6 +313,13 @@ FMT_FUNC int internal::utf16_to_utf8::convert(wstring_view s) {
|
|||||||
if (s.size() > INT_MAX)
|
if (s.size() > INT_MAX)
|
||||||
return ERROR_INVALID_PARAMETER;
|
return ERROR_INVALID_PARAMETER;
|
||||||
int s_size = static_cast<int>(s.size());
|
int s_size = static_cast<int>(s.size());
|
||||||
|
if (s_size == 0) {
|
||||||
|
// WideCharToMultiByte does not support zero length, handle separately.
|
||||||
|
buffer_.resize(1);
|
||||||
|
buffer_[0] = 0;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int length = WideCharToMultiByte(
|
int length = WideCharToMultiByte(
|
||||||
CP_UTF8, 0, s.data(), s_size, FMT_NULL, 0, FMT_NULL, FMT_NULL);
|
CP_UTF8, 0, s.data(), s_size, FMT_NULL, 0, FMT_NULL, FMT_NULL);
|
||||||
if (length == 0)
|
if (length == 0)
|
||||||
|
@ -665,6 +665,13 @@ TEST(UtilTest, UTF16ToUTF8) {
|
|||||||
EXPECT_EQ(s.size(), u.size());
|
EXPECT_EQ(s.size(), u.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(UtilTest, UTF16ToUTF8EmptyString) {
|
||||||
|
std::string s = "";
|
||||||
|
fmt::internal::utf16_to_utf8 u(L"");
|
||||||
|
EXPECT_EQ(s, u.str());
|
||||||
|
EXPECT_EQ(s.size(), u.size());
|
||||||
|
}
|
||||||
|
|
||||||
TEST(UtilTest, UTF8ToUTF16) {
|
TEST(UtilTest, UTF8ToUTF16) {
|
||||||
std::string s = "лошадка";
|
std::string s = "лошадка";
|
||||||
fmt::internal::utf8_to_utf16 u(s.c_str());
|
fmt::internal::utf8_to_utf16 u(s.c_str());
|
||||||
@ -672,6 +679,13 @@ TEST(UtilTest, UTF8ToUTF16) {
|
|||||||
EXPECT_EQ(7, u.size());
|
EXPECT_EQ(7, u.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(UtilTest, UTF8ToUTF16EmptyString) {
|
||||||
|
std::string s = "";
|
||||||
|
fmt::internal::utf8_to_utf16 u(s.c_str());
|
||||||
|
EXPECT_EQ(L"", u.str());
|
||||||
|
EXPECT_EQ(s.size(), u.size());
|
||||||
|
}
|
||||||
|
|
||||||
template <typename Converter, typename Char>
|
template <typename Converter, typename Char>
|
||||||
void check_utf_conversion_error(
|
void check_utf_conversion_error(
|
||||||
const char *message,
|
const char *message,
|
||||||
|
Loading…
Reference in New Issue
Block a user