Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increment output iterator in basic_writer::write for character types … #1058

Merged
merged 1 commit into from
Feb 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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