From e281749c5de4c69dce12f78ec5ceb08ede49a8c9 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sat, 9 Mar 2024 08:20:27 -0800 Subject: [PATCH] Simplify range formatter --- include/fmt/base.h | 10 ++++++---- test/base-test.cc | 12 ++++++------ 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/include/fmt/base.h b/include/fmt/base.h index 9bbf88fd..6dedd4ec 100644 --- a/include/fmt/base.h +++ b/include/fmt/base.h @@ -2961,8 +2961,8 @@ template struct format_to_result { /** Iterator pointing to just after the last successful write in the range. */ OutputIt out; - /** Sentinel indicating the end of the output range. */ - Sentinel out_last; + /** Specifies if the output was truncated. */ + bool truncated; FMT_CONSTEXPR operator OutputIt&() & noexcept { return out; } FMT_CONSTEXPR operator const OutputIt&() const& noexcept { return out; } @@ -2974,13 +2974,15 @@ struct format_to_result { template auto vformat_to(char (&out)[N], string_view fmt, format_args args) -> format_to_result { - return {vformat_to_n(out, N, fmt, args).out, out + N}; + auto result = vformat_to_n(out, N, fmt, args); + return {result.out, result.size > N}; } template FMT_INLINE auto format_to(char (&out)[N], format_string fmt, T&&... args) -> format_to_result { - return vformat_to(out, fmt, fmt::make_format_args(args...)); + auto result = fmt::format_to_n(out, N, fmt, static_cast(args)...); + return {result.out, result.size > N}; } /** Returns the number of chars in the output of ``format(fmt, args...)``. */ diff --git a/test/base-test.cc b/test/base-test.cc index 1a9776c8..b8ec60d8 100644 --- a/test/base-test.cc +++ b/test/base-test.cc @@ -697,13 +697,13 @@ TEST(core_test, format_to_c_array) { char buffer[4]; auto result = fmt::format_to(buffer, "{}", 12345); EXPECT_EQ(4, std::distance(&buffer[0], result.out)); - EXPECT_EQ(0, std::distance(result.out, result.out_last)); + EXPECT_TRUE(result.truncated); EXPECT_EQ(buffer + 4, result.out); EXPECT_EQ("1234", fmt::string_view(buffer, 4)); result = fmt::format_to(buffer, "{:s}", "foobar"); EXPECT_EQ(4, std::distance(&buffer[0], result.out)); - EXPECT_EQ(0, std::distance(result.out, result.out_last)); + EXPECT_TRUE(result.truncated); EXPECT_EQ(buffer + 4, result.out); EXPECT_EQ("foob", fmt::string_view(buffer, 4)); @@ -713,24 +713,24 @@ TEST(core_test, format_to_c_array) { buffer[3] = 'x'; result = fmt::format_to(buffer, "{}", 'A'); EXPECT_EQ(1, std::distance(&buffer[0], result.out)); - EXPECT_EQ(3, std::distance(result.out, result.out_last)); + EXPECT_FALSE(result.truncated); EXPECT_EQ(buffer + 1, result.out); EXPECT_EQ("Axxx", fmt::string_view(buffer, 4)); result = fmt::format_to(buffer, "{}{} ", 'B', 'C'); EXPECT_EQ(3, std::distance(&buffer[0], result.out)); - EXPECT_EQ(1, std::distance(result.out, result.out_last)); + EXPECT_FALSE(result.truncated); EXPECT_EQ(buffer + 3, result.out); EXPECT_EQ("BC x", fmt::string_view(buffer, 4)); result = fmt::format_to(buffer, "{}", "ABCDE"); EXPECT_EQ(4, std::distance(&buffer[0], result.out)); - EXPECT_EQ(0, std::distance(result.out, result.out_last)); + EXPECT_TRUE(result.truncated); EXPECT_EQ("ABCD", fmt::string_view(buffer, 4)); result = fmt::format_to(buffer, "{}", std::string(1000, '*')); EXPECT_EQ(4, std::distance(&buffer[0], result.out)); - EXPECT_EQ(0, std::distance(result.out, result.out_last)); + EXPECT_TRUE(result.truncated); EXPECT_EQ("****", fmt::string_view(buffer, 4)); }