Skip to content

Commit

Permalink
Add format_to_n overload that accepts FMT_COMPILE (from #1767) (#1869)
Browse files Browse the repository at this point in the history
* Add format_to_n overload that accepts FMT_COMPILE

* add FormatToNWithCompileMacro test into CompileTest

Co-authored-by: Dmitriy Kurkin <[email protected]>
  • Loading branch information
alexezeder and Kurkin authored Sep 15, 2020
1 parent 5b5a597 commit f674434
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
10 changes: 10 additions & 0 deletions include/fmt/compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,16 @@ format_to_n_result<OutputIt> format_to_n(OutputIt out, size_t n,
return {it.base(), it.count()};
}

template <typename OutputIt, typename S, typename... Args,
FMT_ENABLE_IF(detail::is_compiled_string<S>::value)>
format_to_n_result<OutputIt> format_to_n(OutputIt out, size_t n, const S&,
const Args&... args) {
constexpr auto compiled = detail::compile<Args...>(S());
auto it = format_to(detail::truncating_iterator<OutputIt>(out, n), compiled,
args...);
return {it.base(), it.count()};
}

template <typename CompiledFormat, typename... Args>
size_t formatted_size(const CompiledFormat& cf, const Args&... args) {
return format_to(detail::counting_iterator(), cf, args...).count();
Expand Down
8 changes: 8 additions & 0 deletions test/compile-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,14 @@ TEST(CompileTest, FormatTo) {
EXPECT_STREQ("42", buf);
}

TEST(CompileTest, FormatToNWithCompileMacro) {
constexpr auto buffer_size = 8;
char buffer[buffer_size];
auto res = fmt::format_to_n(buffer, buffer_size, FMT_COMPILE("{}"), 42);
*res.out = '\0';
EXPECT_STREQ("42", buffer);
}

TEST(CompileTest, TextAndArg) {
EXPECT_EQ(">>>42<<<", fmt::format(FMT_COMPILE(">>>{}<<<"), 42));
EXPECT_EQ("42!", fmt::format(FMT_COMPILE("{}!"), 42));
Expand Down

0 comments on commit f674434

Please sign in to comment.