From d1ef29d67946317a57d7f9f4bc9ef95dd3c11c87 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sun, 8 Nov 2020 08:08:55 -0800 Subject: [PATCH] Fix initialization of iterator_buffer (#1996) --- include/fmt/core.h | 2 +- test/format-test.cc | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/include/fmt/core.h b/include/fmt/core.h index 262a94f9..5135f1f9 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -784,7 +784,7 @@ class iterator_buffer final : public Traits, public buffer { public: explicit iterator_buffer(OutputIt out, size_t n = buffer_size) : Traits(n), - buffer(data_, 0, n < size_t(buffer_size) ? n : size_t(buffer_size)), + buffer(data_, 0, buffer_size), out_(out) {} ~iterator_buffer() { flush(); } diff --git a/test/format-test.cc b/test/format-test.cc index 0f87a23c..5626fa28 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1945,10 +1945,12 @@ TEST(FormatTest, FormatToN) { EXPECT_EQ(5u, result.size); EXPECT_EQ(buffer + 3, result.out); EXPECT_EQ("123x", fmt::string_view(buffer, 4)); + result = fmt::format_to_n(buffer, 3, "{:s}", "foobar"); EXPECT_EQ(6u, result.size); EXPECT_EQ(buffer + 3, result.out); EXPECT_EQ("foox", fmt::string_view(buffer, 4)); + buffer[0] = 'x'; buffer[1] = 'x'; buffer[2] = 'x'; @@ -1956,10 +1958,15 @@ TEST(FormatTest, FormatToN) { EXPECT_EQ(1u, result.size); EXPECT_EQ(buffer + 1, result.out); EXPECT_EQ("Axxx", fmt::string_view(buffer, 4)); + result = fmt::format_to_n(buffer, 3, "{}{} ", 'B', 'C'); EXPECT_EQ(3u, result.size); EXPECT_EQ(buffer + 3, result.out); EXPECT_EQ("BC x", fmt::string_view(buffer, 4)); + + result = fmt::format_to_n(buffer, 4, "{}", "ABCDE"); + EXPECT_EQ(5u, result.size); + EXPECT_EQ("ABCD", fmt::string_view(buffer, 4)); } TEST(FormatTest, WideFormatToN) {