Skip to content

Commit

Permalink
Increment output iterator in basic_writer::write for character types (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
abolz authored and vitaut committed Feb 26, 2019
1 parent a977577 commit 287eaab
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
8 changes: 6 additions & 2 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -2755,10 +2755,14 @@ template <typename Range> class basic_writer {
void write(long double value) { write_double(value, format_specs()); }

/** Writes a character to the buffer. */
void write(char value) { *reserve(1) = value; }
void write(char value) {
auto&& it = reserve(1);
*it++ = value;
}
void write(wchar_t value) {
static_assert(std::is_same<char_type, wchar_t>::value, "");
*reserve(1) = value;
auto&& it = reserve(1);
*it++ = value;
}

/**
Expand Down
22 changes: 22 additions & 0 deletions test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2040,6 +2040,17 @@ TEST(FormatTest, FormatToN) {
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';
result = fmt::format_to_n(buffer, 3, "{}", 'A');
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));
}

TEST(FormatTest, WideFormatToN) {
Expand All @@ -2049,6 +2060,17 @@ TEST(FormatTest, WideFormatToN) {
EXPECT_EQ(5u, result.size);
EXPECT_EQ(buffer + 3, result.out);
EXPECT_EQ(L"123x", fmt::wstring_view(buffer, 4));
buffer[0] = L'x';
buffer[1] = L'x';
buffer[2] = L'x';
result = fmt::format_to_n(buffer, 3, L"{}", L'A');
EXPECT_EQ(1u, result.size);
EXPECT_EQ(buffer + 1, result.out);
EXPECT_EQ(L"Axxx", fmt::wstring_view(buffer, 4));
result = fmt::format_to_n(buffer, 3, L"{}{} ", L'B', L'C');
EXPECT_EQ(3u, result.size);
EXPECT_EQ(buffer + 3, result.out);
EXPECT_EQ(L"BC x", fmt::wstring_view(buffer, 4));
}

#if FMT_USE_CONSTEXPR
Expand Down

0 comments on commit 287eaab

Please sign in to comment.