From fc6e0fe992156935bfb66bb2121c62ee8446758e Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Thu, 7 Jun 2018 18:41:40 +0200 Subject: [PATCH] Fix FP formatting to a non-back_insert_iterator with sign & numeric alignment (#756) --- include/fmt/format.h | 3 ++- test/format-test.cc | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index 3d6ee245..fe2a140b 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -2927,7 +2927,8 @@ void basic_writer::write_double(T value, const format_specs &spec) { align_spec as = spec; if (spec.align() == ALIGN_NUMERIC) { if (sign) { - *reserve(1) = sign; + auto &&it = reserve(1); + *it++ = sign; sign = 0; if (as.width_) --as.width_; diff --git a/test/format-test.cc b/test/format-test.cc index 7fd66643..769e5718 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -232,6 +232,12 @@ TEST(FormatToTest, Format) { EXPECT_EQ("part1part2", s); } +TEST(FormatToTest, FormatToNonbackInsertIteratorWithSignAndNumericAlignment) { + char buffer[16] = {}; + fmt::format_to(buffer, "{: =+}", 42.0); + EXPECT_STREQ("+42", buffer); +} + TEST(FormatterTest, Escape) { EXPECT_EQ("{", format("{{")); EXPECT_EQ("before {", format("before {{"));