pass format string to format_to() inside format_to_n() in compile-time API
instead of compiling it inside format_to_n(), to eliminate code duplication
This commit is contained in:
parent
46f2173388
commit
b2bb2cb049
@ -844,25 +844,9 @@ 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&,
|
||||
Args&&... args) {
|
||||
constexpr auto compiled = detail::compile<Args...>(S());
|
||||
#ifdef __cpp_if_constexpr
|
||||
if constexpr (std::is_same<remove_cvref_t<decltype(compiled)>,
|
||||
detail::unknown_format>()) {
|
||||
auto it =
|
||||
format_to(detail::truncating_iterator<OutputIt>(out, n),
|
||||
static_cast<basic_string_view<typename S::char_type>>(S()),
|
||||
std::forward<Args>(args)...);
|
||||
return {it.base(), it.count()};
|
||||
} else {
|
||||
auto it = format_to(detail::truncating_iterator<OutputIt>(out, n), compiled,
|
||||
std::forward<Args>(args)...);
|
||||
return {it.base(), it.count()};
|
||||
}
|
||||
#else
|
||||
auto it = format_to(detail::truncating_iterator<OutputIt>(out, n), compiled,
|
||||
auto it = format_to(detail::truncating_iterator<OutputIt>(out, n), S(),
|
||||
std::forward<Args>(args)...);
|
||||
return {it.base(), it.count()};
|
||||
#endif
|
||||
}
|
||||
|
||||
template <typename CompiledFormat, typename... Args>
|
||||
|
||||
@ -239,9 +239,9 @@ TEST(CompileTest, UnknownFormatFallback) {
|
||||
EXPECT_EQ(" 42 ", fmt::string_view(v.data(), v.size()));
|
||||
|
||||
char buffer[4];
|
||||
auto result = fmt::format_to_n(buffer, 4, FMT_COMPILE("{name:^4}"),
|
||||
auto result = fmt::format_to_n(buffer, 4, FMT_COMPILE("{name:^5}"),
|
||||
fmt::arg("name", 42));
|
||||
EXPECT_EQ(4u, result.size);
|
||||
EXPECT_EQ(5u, result.size);
|
||||
EXPECT_EQ(buffer + 4, result.out);
|
||||
EXPECT_EQ(" 42 ", fmt::string_view(buffer, 4));
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user