Skip to content

Commit

Permalink
Improve width estimation (#2033)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Feb 13, 2021
1 parent 13b117b commit 7e72673
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
23 changes: 22 additions & 1 deletion include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,28 @@ FMT_CONSTEXPR inline size_t compute_width(string_view s) {
// It is not a lambda for compatibility with C++14.
struct count_code_points {
size_t* count;
FMT_CONSTEXPR void operator()(uint32_t, int) const { ++*count; }
FMT_CONSTEXPR void operator()(uint32_t cp, int error) const {
*count +=
1 +
(error == 0 && cp >= 0x1100 &&
(cp <= 0x115f || // Hangul Jamo init. consonants
cp == 0x2329 || // LEFT-POINTING ANGLE BRACKET〈
cp == 0x232a || // RIGHT-POINTING ANGLE BRACKET 〉
// CJK ... Yi except Unicode Character “〿”:
(cp >= 0x2e80 && cp <= 0xa4cf && cp != 0x303f) ||
(cp >= 0xac00 && cp <= 0xd7a3) || // Hangul Syllables
(cp >= 0xf900 && cp <= 0xfaff) || // CJK Compatibility Ideographs
(cp >= 0xfe10 && cp <= 0xfe19) || // Vertical Forms
(cp >= 0xfe30 && cp <= 0xfe6f) || // CJK Compatibility Forms
(cp >= 0xff00 && cp <= 0xff60) || // Fullwidth Forms
(cp >= 0xffe0 && cp <= 0xffe6) || // Fullwidth Forms
(cp >= 0x20000 && cp <= 0x2fffd) || // CJK
(cp >= 0x30000 && cp <= 0x3fffd) ||
// Miscellaneous Symbols and Pictographs + Emoticons:
(cp >= 0x1f300 && cp <= 0x1f64f) ||
// Supplemental Symbols and Pictographs:
(cp >= 0x1f900 && cp <= 0x1f9ff)));
}
};
for_each_codepoint(s, count_code_points{&num_code_points});
return num_code_points;
Expand Down
2 changes: 1 addition & 1 deletion test/compile-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ TEST(CompileTimeFormattingTest, String) {
test_format<17>(FMT_COMPILE("{} is {}"), "The answer", "42"));

EXPECT_EQ("abc**", test_format<6>(FMT_COMPILE("{:*<5}"), "abc"));
EXPECT_EQ("**🤡**", test_format<9>(FMT_COMPILE("{:*^5}"), "🤡"));
EXPECT_EQ("**🤡**", test_format<9>(FMT_COMPILE("{:*^6}"), "🤡"));
}

TEST(CompileTimeFormattingTest, Combination) {
Expand Down
3 changes: 2 additions & 1 deletion test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -868,7 +868,8 @@ TEST(FormatterTest, Width) {
EXPECT_EQ(" 0xcafe", format("{0:10}", reinterpret_cast<void*>(0xcafe)));
EXPECT_EQ("x ", format("{0:11}", 'x'));
EXPECT_EQ("str ", format("{0:12}", "str"));
EXPECT_EQ(format("{:*^5}", "🤡"), "**🤡**");
EXPECT_EQ(format("{:*^6}", "🤡"), "**🤡**");
EXPECT_EQ(format("{:*^8}", "你好"), "**你好**");
EXPECT_EQ(format("{:#6}", 42.0), " 42.0");
EXPECT_EQ(format("{:6c}", static_cast<int>('x')), "x ");
EXPECT_EQ(format("{:>06.0f}", 0.00884311), "000000");
Expand Down

0 comments on commit 7e72673

Please sign in to comment.