From e6b37b4aff1a673cb735b9b287aa637142120f6d Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sat, 21 Dec 2019 16:33:34 -0800 Subject: [PATCH] Handle block boundaries in utf8_to_utf16 --- include/fmt/format-inl.h | 7 +++++-- test/format-test.cc | 1 + 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/include/fmt/format-inl.h b/include/fmt/format-inl.h index 1a1428ea..ec5da1db 100644 --- a/include/fmt/format-inl.h +++ b/include/fmt/format-inl.h @@ -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); } 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); - transcode(buf); + p = buf; + do { + p = transcode(p); + } while (p - buf < num_chars_left); } buffer_.push_back(0); } diff --git a/test/format-test.cc b/test/format-test.cc index cd2653b1..050a442e 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -410,6 +410,7 @@ TEST(UtilTest, UTF8ToUTF16) { "invalid utf8"); EXPECT_THROW_MSG(fmt::internal::utf8_to_utf16(fmt::string_view("ะป", 1)), std::runtime_error, "invalid utf8"); + EXPECT_EQ(L"123456", fmt::internal::utf8_to_utf16("123456").str()); } TEST(UtilTest, UTF8ToUTF16EmptyString) {