Handle block boundaries in utf8_to_utf16

This commit is contained in:
Victor Zverovich 2019-12-21 16:33:34 -08:00
parent 8cf4c52068
commit e6b37b4aff
2 changed files with 6 additions and 2 deletions

View File

@ -1292,9 +1292,12 @@ FMT_FUNC internal::utf8_to_utf16::utf8_to_utf16(string_view s) {
for (auto end = p + s.size() - block_size + 1; p < end;) p = transcode(p); for (auto end = p + s.size() - block_size + 1; p < end;) p = transcode(p);
} }
if (auto num_chars_left = s.data() + s.size() - p) { if (auto num_chars_left = s.data() + s.size() - p) {
char buf[4] = {}; char buf[2 * block_size - 1] = {};
memcpy(buf, p, num_chars_left); memcpy(buf, p, num_chars_left);
transcode(buf); p = buf;
do {
p = transcode(p);
} while (p - buf < num_chars_left);
} }
buffer_.push_back(0); buffer_.push_back(0);
} }

View File

@ -410,6 +410,7 @@ TEST(UtilTest, UTF8ToUTF16) {
"invalid utf8"); "invalid utf8");
EXPECT_THROW_MSG(fmt::internal::utf8_to_utf16(fmt::string_view("л", 1)), EXPECT_THROW_MSG(fmt::internal::utf8_to_utf16(fmt::string_view("л", 1)),
std::runtime_error, "invalid utf8"); std::runtime_error, "invalid utf8");
EXPECT_EQ(L"123456", fmt::internal::utf8_to_utf16("123456").str());
} }
TEST(UtilTest, UTF8ToUTF16EmptyString) { TEST(UtilTest, UTF8ToUTF16EmptyString) {