mirror of
https://github.com/fmtlib/fmt.git
synced 2024-10-02 04:52:03 +00:00
Add FMT_COMPILE support to format_to
This commit is contained in:
parent
5ddf9ee1bd
commit
9a4cc88426
@ -24,7 +24,9 @@ struct is_compiled_string : std::is_base_of<compiled_string, S> {};
|
|||||||
#define FMT_COMPILE(s) FMT_STRING_IMPL(s, fmt::detail::compiled_string)
|
#define FMT_COMPILE(s) FMT_STRING_IMPL(s, fmt::detail::compiled_string)
|
||||||
|
|
||||||
template <typename T, typename... Tail>
|
template <typename T, typename... Tail>
|
||||||
const T& first(const T& value, const Tail&...) { return value; }
|
const T& first(const T& value, const Tail&...) {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
// Part of a compiled format string. It can be either literal text or a
|
// Part of a compiled format string. It can be either literal text or a
|
||||||
// replacement field.
|
// replacement field.
|
||||||
@ -556,7 +558,8 @@ std::basic_string<Char> format(const CompiledFormat& cf, const Args&... args) {
|
|||||||
|
|
||||||
template <typename S, typename... Args,
|
template <typename S, typename... Args,
|
||||||
FMT_ENABLE_IF(detail::is_compiled_string<S>::value)>
|
FMT_ENABLE_IF(detail::is_compiled_string<S>::value)>
|
||||||
FMT_INLINE std::basic_string<typename S::char_type> format(S, Args&&... args) {
|
FMT_INLINE std::basic_string<typename S::char_type> format(const S&,
|
||||||
|
Args&&... args) {
|
||||||
constexpr basic_string_view<typename S::char_type> str = S();
|
constexpr basic_string_view<typename S::char_type> str = S();
|
||||||
if (str.size() == 2 && str[0] == '{' && str[1] == '}')
|
if (str.size() == 2 && str[0] == '{' && str[1] == '}')
|
||||||
return fmt::to_string(detail::first(args...));
|
return fmt::to_string(detail::first(args...));
|
||||||
@ -575,6 +578,13 @@ OutputIt format_to(OutputIt out, const CompiledFormat& cf,
|
|||||||
make_format_args<context>(args...));
|
make_format_args<context>(args...));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename OutputIt, typename S, typename... Args,
|
||||||
|
FMT_ENABLE_IF(detail::is_compiled_string<S>::value)>
|
||||||
|
OutputIt format_to(OutputIt out, const S&, const Args&... args) {
|
||||||
|
constexpr auto compiled = compile<Args...>(S());
|
||||||
|
return format_to(out, compiled, args...);
|
||||||
|
}
|
||||||
|
|
||||||
template <
|
template <
|
||||||
typename OutputIt, typename CompiledFormat, typename... Args,
|
typename OutputIt, typename CompiledFormat, typename... Args,
|
||||||
FMT_ENABLE_IF(detail::is_output_iterator<OutputIt>::value&& std::is_base_of<
|
FMT_ENABLE_IF(detail::is_output_iterator<OutputIt>::value&& std::is_base_of<
|
||||||
|
@ -156,6 +156,13 @@ TEST(CompileTest, FormatDefault) {
|
|||||||
EXPECT_EQ("foo", fmt::format(FMT_COMPILE("{}"), std::string("foo")));
|
EXPECT_EQ("foo", fmt::format(FMT_COMPILE("{}"), std::string("foo")));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(CompileTest, FormatTo) {
|
||||||
|
char buf[8];
|
||||||
|
auto end = fmt::format_to(buf, FMT_COMPILE("{}"), 42);
|
||||||
|
*end = '\0';
|
||||||
|
EXPECT_STREQ("42", buf);
|
||||||
|
}
|
||||||
|
|
||||||
TEST(CompileTest, TextAndArg) {
|
TEST(CompileTest, TextAndArg) {
|
||||||
EXPECT_EQ(">>>42<<<", fmt::format(FMT_COMPILE(">>>{}<<<"), 42));
|
EXPECT_EQ(">>>42<<<", fmt::format(FMT_COMPILE(">>>{}<<<"), 42));
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user