From 9427f15bef6e5d937d5a4919e5ed5281fa703005 Mon Sep 17 00:00:00 2001 From: stryku Date: Tue, 21 May 2019 21:03:57 +0200 Subject: [PATCH] Fixed issue with formatting to an array of chars --- include/fmt/prepare.h | 18 ++++++++------ test/prepare-test.cc | 57 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 67 insertions(+), 8 deletions(-) diff --git a/include/fmt/prepare.h b/include/fmt/prepare.h index 0ac92188..791858af 100644 --- a/include/fmt/prepare.h +++ b/include/fmt/prepare.h @@ -217,7 +217,8 @@ class prepared_format { std::basic_string format(const Args&... args) const { basic_memory_buffer buffer; typedef back_insert_range> range; - this->vformat_to(range(buffer), make_args_checked(format_, args...)); + this->vformat_to(range(buffer), basic_format_args{ + make_args_checked(format_, args...)}); return to_string(buffer); } @@ -226,7 +227,8 @@ class prepared_format { std::back_insert_iterator out, const Args&... args) const { internal::container_buffer buffer(internal::get_container(out)); typedef back_insert_range> range; - this->vformat_to(range(buffer), make_args_checked(format_, args...)); + this->vformat_to(range(buffer), basic_format_args{ + make_args_checked(format_, args...)}); return out; } @@ -242,18 +244,20 @@ class prepared_format { inline typename buffer_context::iterator format_to( basic_memory_buffer& buf, const Args&... args) const { typedef back_insert_range> range; - return this->vformat_to(range(buf), make_args_checked(format_, args...)); + return this->vformat_to( + range(buf), + basic_format_args{make_args_checked(format_, args...)}); } private: typedef buffer_context context; - template - typename context::iterator vformat_to(Range out, - basic_format_args args) const { + template + auto vformat_to(Range out, basic_format_args args) const -> + typename Context::iterator { const auto format_view = internal::to_string_view(format_); basic_parse_context parse_ctx(format_view); - context ctx(out.begin(), args); + Context ctx(out.begin(), args); const auto& parts = parts_provider_.parts(); for (auto part_it = parts.begin(); part_it != parts.end(); ++part_it) { diff --git a/test/prepare-test.cc b/test/prepare-test.cc index 64fe98c1..a210178b 100644 --- a/test/prepare-test.cc +++ b/test/prepare-test.cc @@ -475,7 +475,7 @@ TEST(PrepareTest, CopyPreparedFormat_InternalStringViewsAreNotInvalidated) { } } -TEST(PepareTest, ReusedPreparedFormatType) { +TEST(PrepareTest, ReusedPreparedFormatType) { typedef fmt::prepared_format::type prepared_format; prepared_format prepared = fmt::prepare("The {} is {}."); @@ -637,3 +637,58 @@ TEST(PrepareTest, PassUserTypeFormat) { const auto prepared = fmt::prepare(user_format("test {}")); EXPECT_EQ("test 42", prepared.format(42)); } + +TEST(PrepareTest, FormatToArrayOfChars) { + char buffer[32] = {0}; + const auto prepared = fmt::prepare("4{}"); + prepared.format_to(buffer, 2); + EXPECT_EQ(std::string("42"), buffer); + wchar_t wbuffer[32] = {0}; + const auto wprepared = fmt::prepare(L"4{}"); + wprepared.format_to(wbuffer, 2); + EXPECT_EQ(std::wstring(L"42"), wbuffer); +} + +TEST(PrepareTest, FormatToIterator) { + std::string s(2, ' '); + const auto prepared = fmt::prepare("4{}"); + prepared.format_to(s.begin(), 2); + EXPECT_EQ("42", s); + std::wstring ws(2, L' '); + const auto wprepared = fmt::prepare(L"4{}"); + wprepared.format_to(ws.begin(), 2); + EXPECT_EQ(L"42", ws); +} + +TEST(PrepareTest, FormatToBackInserter) { + std::string s; + const auto prepared = fmt::prepare("4{}"); + prepared.format_to(std::back_inserter(s), 2); + EXPECT_EQ("42", s); + std::wstring ws; + const auto wprepared = fmt::prepare(L"4{}"); + wprepared.format_to(std::back_inserter(ws), 2); + EXPECT_EQ(L"42", ws); +} + +TEST(PrepareTest, FormatToBasicMemoryBuffer) { + fmt::basic_memory_buffer buffer; + const auto prepared = fmt::prepare("4{}"); + prepared.format_to(buffer, 2); + EXPECT_EQ("42", to_string(buffer)); + fmt::basic_memory_buffer wbuffer; + const auto wprepared = fmt::prepare(L"4{}"); + wprepared.format_to(wbuffer, 2); + EXPECT_EQ(L"42", to_string(wbuffer)); +} + +TEST(PrepareTest, FormatToMemoryBuffer) { + fmt::memory_buffer buffer; + const auto prepared = fmt::prepare("4{}"); + prepared.format_to(buffer, 2); + EXPECT_EQ("42", to_string(buffer)); + fmt::wmemory_buffer wbuffer; + const auto wprepared = fmt::prepare(L"4{}"); + wprepared.format_to(wbuffer, 2); + EXPECT_EQ(L"42", to_string(wbuffer)); +}