Skip to content

Commit

Permalink
Add support for code units > 0xFFFF in fill
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Oct 20, 2024
1 parent 58c185b commit 201d173
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
4 changes: 3 additions & 1 deletion include/fmt/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,8 @@ class basic_specs {
template <typename Char> constexpr auto fill_unit() const -> Char {
using uchar = unsigned char;
return static_cast<Char>(static_cast<uchar>(fill_data_[0]) |
(static_cast<uchar>(fill_data_[1]) << 8));
(static_cast<uchar>(fill_data_[1]) << 8) |
(static_cast<uchar>(fill_data_[2]) << 16));
}

FMT_CONSTEXPR void set_fill(char c) {
Expand All @@ -816,6 +817,7 @@ class basic_specs {
unsigned uchar = static_cast<detail::unsigned_char<Char>>(s[0]);
fill_data_[0] = static_cast<char>(uchar);
fill_data_[1] = static_cast<char>(uchar >> 8);
fill_data_[2] = static_cast<char>(uchar >> 16);
return;
}
FMT_ASSERT(size <= max_fill_size, "invalid fill");
Expand Down
3 changes: 2 additions & 1 deletion include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,8 @@ FMT_CONSTEXPR void for_each_codepoint(string_view s, F f) {
auto num_chars_left = to_unsigned(s.data() + s.size() - p);
if (num_chars_left == 0) return;

FMT_ASSERT(num_chars_left < block_size, "");
// Suppress bogus -Wstringop-overflow.
if (FMT_GCC_VERSION) num_chars_left &= 3;
char buf[2 * block_size - 1] = {};
copy<char>(p, p + num_chars_left, buf);
const char* buf_ptr = buf;
Expand Down
19 changes: 10 additions & 9 deletions test/xchar-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,17 @@ TEST(xchar_test, format_explicitly_convertible_to_wstring_view) {
#endif

TEST(xchar_test, format) {
EXPECT_EQ(L"42", fmt::format(L"{}", 42));
EXPECT_EQ(L"4.2", fmt::format(L"{}", 4.2));
EXPECT_EQ(L"abc", fmt::format(L"{}", L"abc"));
EXPECT_EQ(L"z", fmt::format(L"{}", L'z'));
EXPECT_EQ(fmt::format(L"{}", 42), L"42");
EXPECT_EQ(fmt::format(L"{}", 4.2), L"4.2");
EXPECT_EQ(fmt::format(L"{}", L"abc"), L"abc");
EXPECT_EQ(fmt::format(L"{}", L'z'), L"z");
EXPECT_THROW(fmt::format(fmt::runtime(L"{:*\x343E}"), 42), fmt::format_error);
EXPECT_EQ(L"true", fmt::format(L"{}", true));
EXPECT_EQ(L"a", fmt::format(L"{0}", L'a'));
EXPECT_EQ(L"Cyrillic letter \x42e",
fmt::format(L"Cyrillic letter {}", L'\x42e'));
EXPECT_EQ(L"abc1", fmt::format(L"{}c{}", L"ab", 1));
EXPECT_EQ(fmt::format(L"{}", true), L"true");
EXPECT_EQ(fmt::format(L"{0}", L'a'), L"a");
EXPECT_EQ(fmt::format(L"Letter {}", L'\x40e'), L"Letter \x40e"); // Ў
if (sizeof(wchar_t) == 4)
EXPECT_EQ(fmt::format(fmt::runtime(L"{:𓀨>3}"), 42), L"𓀨42");
EXPECT_EQ(fmt::format(L"{}c{}", L"ab", 1), L"abc1");
}

TEST(xchar_test, is_formattable) {
Expand Down

0 comments on commit 201d173

Please sign in to comment.