From 4e94c649f9a6767b2e359842aee0a2e771ae1835 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Tue, 23 Jun 2020 14:03:37 -0700 Subject: [PATCH] Deprecate compile --- include/fmt/compile.h | 47 +++++++++++++++++++++++++------------------ test/compile-test.cc | 31 ++++++++++------------------ 2 files changed, 38 insertions(+), 40 deletions(-) diff --git a/include/fmt/compile.h b/include/fmt/compile.h index ffc8c91f..67f7a7f7 100644 --- a/include/fmt/compile.h +++ b/include/fmt/compile.h @@ -521,11 +521,7 @@ constexpr auto compile_format_string(S format_str) { format_str); } } -#endif // __cpp_if_constexpr -} // namespace detail -#if FMT_USE_CONSTEXPR -# ifdef __cpp_if_constexpr template ::value || detail::is_compiled_string::value)> @@ -545,6 +541,31 @@ constexpr auto compile(S format_str) { } } } +#else +template ::value)> +constexpr auto compile(S format_str) -> detail::compiled_format { + return detail::compiled_format(to_string_view(format_str)); +} +#endif // __cpp_if_constexpr + +// Compiles the format string which must be a string literal. +template +auto compile(const Char (&format_str)[N]) + -> detail::compiled_format { + return detail::compiled_format( + basic_string_view(format_str, N - 1)); +} +} // namespace detail + +// DEPRECATED! use FMT_COMPILE instead. +template +FMT_DEPRECATED auto compile(const Args&... args) -> decltype(detail::compile(args...)) { + return detail::compile(args...); +} + +#if FMT_USE_CONSTEXPR +# ifdef __cpp_if_constexpr template ::value)> -constexpr auto compile(S format_str) -> detail::compiled_format { - return detail::compiled_format(to_string_view(format_str)); -} # endif // __cpp_if_constexpr #endif // FMT_USE_CONSTEXPR -// Compiles the format string which must be a string literal. -template -auto compile(const Char (&format_str)[N]) - -> detail::compiled_format { - return detail::compiled_format( - basic_string_view(format_str, N - 1)); -} - template format(const S&, constexpr basic_string_view str = S(); if (str.size() == 2 && str[0] == '{' && str[1] == '}') return fmt::to_string(detail::first(args...)); - constexpr auto compiled = compile(S()); + constexpr auto compiled = detail::compile(S()); return format(compiled, std::forward(args)...); } @@ -618,7 +625,7 @@ OutputIt format_to(OutputIt out, const CompiledFormat& cf, template ::value)> OutputIt format_to(OutputIt out, const S&, const Args&... args) { - constexpr auto compiled = compile(S()); + constexpr auto compiled = detail::compile(S()); return format_to(out, compiled, args...); } diff --git a/test/compile-test.cc b/test/compile-test.cc index 8004fff3..a47cea54 100644 --- a/test/compile-test.cc +++ b/test/compile-test.cc @@ -67,46 +67,37 @@ TEST(CompileTest, CompileTimePreparedPartsTypeProvider) { #endif TEST(CompileTest, PassStringLiteralFormat) { - const auto prepared = fmt::compile("test {}"); + const auto prepared = fmt::detail::compile("test {}"); EXPECT_EQ("test 42", fmt::format(prepared, 42)); - const auto wprepared = fmt::compile(L"test {}"); + const auto wprepared = fmt::detail::compile(L"test {}"); EXPECT_EQ(L"test 42", fmt::format(wprepared, 42)); } -#if FMT_USE_CONSTEXPR -TEST(CompileTest, PassCompileString) { - const auto prepared = fmt::compile(FMT_STRING("test {}")); - EXPECT_EQ("test 42", fmt::format(prepared, 42)); - const auto wprepared = fmt::compile(FMT_STRING(L"test {}")); - EXPECT_EQ(L"test 42", fmt::format(wprepared, 42)); -} -#endif - TEST(CompileTest, FormatToArrayOfChars) { char buffer[32] = {0}; - const auto prepared = fmt::compile("4{}"); + const auto prepared = fmt::detail::compile("4{}"); fmt::format_to(fmt::detail::make_checked(buffer, 32), prepared, 2); EXPECT_EQ(std::string("42"), buffer); wchar_t wbuffer[32] = {0}; - const auto wprepared = fmt::compile(L"4{}"); + const auto wprepared = fmt::detail::compile(L"4{}"); fmt::format_to(fmt::detail::make_checked(wbuffer, 32), wprepared, 2); EXPECT_EQ(std::wstring(L"42"), wbuffer); } TEST(CompileTest, FormatToIterator) { std::string s(2, ' '); - const auto prepared = fmt::compile("4{}"); + const auto prepared = fmt::detail::compile("4{}"); fmt::format_to(s.begin(), prepared, 2); EXPECT_EQ("42", s); std::wstring ws(2, L' '); - const auto wprepared = fmt::compile(L"4{}"); + const auto wprepared = fmt::detail::compile(L"4{}"); fmt::format_to(ws.begin(), wprepared, 2); EXPECT_EQ(L"42", ws); } TEST(CompileTest, FormatToN) { char buf[5]; - auto f = fmt::compile("{:10}"); + auto f = fmt::detail::compile("{:10}"); auto result = fmt::format_to_n(buf, 5, f, 42); EXPECT_EQ(result.size, 10); EXPECT_EQ(result.out, buf + 5); @@ -114,12 +105,12 @@ TEST(CompileTest, FormatToN) { } TEST(CompileTest, FormattedSize) { - auto f = fmt::compile("{:10}"); + auto f = fmt::detail::compile("{:10}"); EXPECT_EQ(fmt::formatted_size(f, 42), 10); } TEST(CompileTest, MultipleTypes) { - auto f = fmt::compile("{} {}"); + auto f = fmt::detail::compile("{} {}"); EXPECT_EQ(fmt::format(f, 42, 42), "42 42"); } @@ -135,12 +126,12 @@ template <> struct formatter : formatter { FMT_END_NAMESPACE TEST(CompileTest, FormatUserDefinedType) { - auto f = fmt::compile("{}"); + auto f = fmt::detail::compile("{}"); EXPECT_EQ(fmt::format(f, formattable()), "foo"); } TEST(CompileTest, EmptyFormatString) { - auto f = fmt::compile<>(""); + auto f = fmt::detail::compile<>(""); EXPECT_EQ(fmt::format(f), ""); }