diff --git a/include/fmt/core.h b/include/fmt/core.h index e76d01fd..ece6c479 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -2258,6 +2258,49 @@ void check_format_string(S format_str) { (void)invalid_format; } +// Converts string literals to basic_string_view. +template +FMT_CONSTEXPR basic_string_view compile_string_to_view( + const Char (&s)[N]) { + // Remove trailing null character if needed. Won't be present if this is used + // with raw character array (i.e. not defined as a string). + return {s, + N - ((std::char_traits::to_int_type(s[N - 1]) == 0) ? 1 : 0)}; +} + +// Converts string_view to basic_string_view. +template +FMT_CONSTEXPR basic_string_view compile_string_to_view( + const std_string_view& s) { + return {s.data(), s.size()}; +} + +#define FMT_STRING_IMPL(s, base) \ + [] { \ + /* Use the hidden visibility as a workaround for a GCC bug (#1973). */ \ + /* Use a macro-like name to avoid shadowing warnings. */ \ + struct FMT_GCC_VISIBILITY_HIDDEN FMT_COMPILE_STRING : base { \ + using char_type = fmt::remove_cvref_t; \ + FMT_MAYBE_UNUSED FMT_CONSTEXPR \ + operator fmt::basic_string_view() const { \ + return fmt::detail::compile_string_to_view(s); \ + } \ + }; \ + return FMT_COMPILE_STRING(); \ + }() + +/** + \rst + Constructs a compile-time format string from a string literal *s*. + + **Example**:: + + // A compile-time error because 'd' is an invalid specifier for strings. + std::string s = fmt::format(FMT_STRING("{:d}"), "foo"); + \endrst + */ +#define FMT_STRING(s) FMT_STRING_IMPL(s, fmt::compile_string) + template ::value)> std::basic_string vformat( basic_string_view format_str, diff --git a/include/fmt/format.h b/include/fmt/format.h index 3a74f41e..628d7caf 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -2704,49 +2704,6 @@ struct format_handler : detail::error_handler { } }; -// Converts string literals to basic_string_view. -template -FMT_CONSTEXPR basic_string_view compile_string_to_view( - const Char (&s)[N]) { - // Remove trailing null character if needed. Won't be present if this is used - // with raw character array (i.e. not defined as a string). - return {s, - N - ((std::char_traits::to_int_type(s[N - 1]) == 0) ? 1 : 0)}; -} - -// Converts string_view to basic_string_view. -template -FMT_CONSTEXPR basic_string_view compile_string_to_view( - const std_string_view& s) { - return {s.data(), s.size()}; -} - -#define FMT_STRING_IMPL(s, base) \ - [] { \ - /* Use the hidden visibility as a workaround for a GCC bug (#1973). */ \ - /* Use a macro-like name to avoid shadowing warnings. */ \ - struct FMT_GCC_VISIBILITY_HIDDEN FMT_COMPILE_STRING : base { \ - using char_type = fmt::remove_cvref_t; \ - FMT_MAYBE_UNUSED FMT_CONSTEXPR \ - operator fmt::basic_string_view() const { \ - return fmt::detail::compile_string_to_view(s); \ - } \ - }; \ - return FMT_COMPILE_STRING(); \ - }() - -/** - \rst - Constructs a compile-time format string from a string literal *s*. - - **Example**:: - - // A compile-time error because 'd' is an invalid specifier for strings. - std::string s = fmt::format(FMT_STRING("{:d}"), "foo"); - \endrst - */ -#define FMT_STRING(s) FMT_STRING_IMPL(s, fmt::compile_string) - template