// The {fmt} API subset suyu's shader_recompiler uses, on C++20 std::format (no external {fmt} needed). // Types given a fmt::formatter specialization are formattable by std::format too (the bridge at the end). #pragma once #include #include #include #include #include #include namespace fmt { using std::format_context; using std::format_parse_context; template struct formatter; // specialized by the code that uses it template <> struct formatter : std::formatter {}; struct runtime_format_string { std::string_view str; }; inline runtime_format_string runtime(std::string_view s) { return {s}; } // (hookshot) Formats read as they run, not checked at compile time: std::format_string is missing from Visual Studio's // C++ library before 17.5, and a player's build stopped on it with 303 files failed (Hookshot issue 39) template std::string format(std::string_view f, Args&&... args) { return std::vformat(f, std::make_format_args(args...)); } template std::string format(runtime_format_string f, Args&&... args) { return std::vformat(f.str, std::make_format_args(args...)); } template Out format_to(Out out, std::string_view f, Args&&... args) { return std::vformat_to(std::move(out), f, std::make_format_args(args...)); } template auto format_to_n(Out out, std::iter_difference_t n, std::string_view f, Args&&... args) { // (mkw-recomp: Eden's const std::string text = std::vformat(f, std::make_format_args(args...)); const auto size = (std::iter_difference_t)text.size(); for (std::iter_difference_t i = 0; i < size && i < n; i++) *out++ = text[(size_t)i]; return std::format_to_n_result{std::move(out), size}; } template Out format_to(Out out, runtime_format_string f, Args&&... args) { return std::vformat_to(std::move(out), f.str, std::make_format_args(args...)); } template std::string to_string(const T& value) { return std::format("{}", value); } namespace detail { template concept has_fmt_formatter = requires { sizeof(::fmt::formatter); }; template concept has_format_as = requires(const T& t) { format_as(t); }; // {fmt} 10's customization point (ADL) } } // namespace fmt template requires(fmt::detail::has_fmt_formatter && !std::is_same_v) struct std::formatter : ::fmt::formatter {}; // {fmt} before 10 formatted enums as their underlying integer, and suyu relies on it; std::format does not. template requires(std::is_enum_v && !fmt::detail::has_fmt_formatter && !fmt::detail::has_format_as) struct std::formatter : std::formatter, char> { template auto format(T v, Ctx& ctx) const { return std::formatter, char>::format(static_cast>(v), ctx); } }; template requires(fmt::detail::has_format_as && !fmt::detail::has_fmt_formatter) struct std::formatter : std::formatter()))>, char> { template auto format(const T& v, Ctx& ctx) const { using Base = std::formatter()))>, char>; return Base::format(format_as(v), ctx); } };