diff --git a/include/fmt/std.h b/include/fmt/std.h index f43dc74d..d8093a9b 100644 --- a/include/fmt/std.h +++ b/include/fmt/std.h @@ -23,7 +23,6 @@ # include # include # include -# include // Check FMT_CPLUSPLUS to suppress a bogus warning in MSVC. # if FMT_CPLUSPLUS >= 201703L @@ -79,11 +78,11 @@ # endif #endif -#if FMT_CPP_LIB_FILESYSTEM FMT_BEGIN_NAMESPACE - namespace detail { +#if FMT_CPP_LIB_FILESYSTEM + template auto get_path_string(const std::filesystem::path& p, const std::basic_string& native) { @@ -111,154 +110,9 @@ void write_escaped_path(basic_memory_buffer& quoted, } } -} // namespace detail - -template struct formatter { - private: - format_specs specs_; - detail::arg_ref width_ref_; - bool debug_ = false; - char path_type_ = 0; - - public: - FMT_CONSTEXPR void set_debug_format(bool set = true) { debug_ = set; } - - FMT_CONSTEXPR auto parse(parse_context& ctx) { - auto it = ctx.begin(), end = ctx.end(); - if (it == end) return it; - - it = detail::parse_align(it, end, specs_); - if (it == end) return it; - - Char c = *it; - if ((c >= '0' && c <= '9') || c == '{') - it = detail::parse_width(it, end, specs_, width_ref_, ctx); - if (it != end && *it == '?') { - debug_ = true; - ++it; - } - if (it != end && (*it == 'g')) path_type_ = detail::to_ascii(*it++); - return it; - } - - template - auto format(const std::filesystem::path& p, FormatContext& ctx) const { - auto specs = specs_; - auto path_string = - !path_type_ ? p.native() - : p.generic_string(); - - detail::handle_dynamic_spec(specs.dynamic_width(), specs.width, width_ref_, - ctx); - if (!debug_) { - auto s = detail::get_path_string(p, path_string); - return detail::write(ctx.out(), basic_string_view(s), specs); - } - auto quoted = basic_memory_buffer(); - detail::write_escaped_path(quoted, p, path_string); - return detail::write(ctx.out(), - basic_string_view(quoted.data(), quoted.size()), - specs); - } -}; - -class path : public std::filesystem::path { - public: - auto display_string() const -> std::string { - const std::filesystem::path& base = *this; - return fmt::format(FMT_STRING("{}"), base); - } - auto system_string() const -> std::string { return string(); } - - auto generic_display_string() const -> std::string { - const std::filesystem::path& base = *this; - return fmt::format(FMT_STRING("{:g}"), base); - } - auto generic_system_string() const -> std::string { return generic_string(); } -}; - -FMT_END_NAMESPACE #endif // FMT_CPP_LIB_FILESYSTEM -FMT_BEGIN_NAMESPACE -template -struct formatter, Char> - : nested_formatter, Char> { - private: - // Functor because C++11 doesn't support generic lambdas. - struct writer { - const std::bitset& bs; - - template - FMT_CONSTEXPR auto operator()(OutputIt out) -> OutputIt { - for (auto pos = N; pos > 0; --pos) { - out = detail::write(out, bs[pos - 1] ? Char('1') : Char('0')); - } - - return out; - } - }; - - public: - template - auto format(const std::bitset& bs, FormatContext& ctx) const - -> decltype(ctx.out()) { - return this->write_padded(ctx, writer{bs}); - } -}; - -template -struct formatter : basic_ostream_formatter {}; -FMT_END_NAMESPACE - -#ifdef __cpp_lib_optional -FMT_BEGIN_NAMESPACE -template -struct formatter, Char, - std::enable_if_t::value>> { - private: - formatter underlying_; - static constexpr basic_string_view optional = - detail::string_literal{}; - static constexpr basic_string_view none = - detail::string_literal{}; - - template - FMT_CONSTEXPR static auto maybe_set_debug_format(U& u, bool set) - -> decltype(u.set_debug_format(set)) { - u.set_debug_format(set); - } - - template - FMT_CONSTEXPR static void maybe_set_debug_format(U&, ...) {} - - public: - FMT_CONSTEXPR auto parse(parse_context& ctx) { - maybe_set_debug_format(underlying_, true); - return underlying_.parse(ctx); - } - - template - auto format(const std::optional& opt, FormatContext& ctx) const - -> decltype(ctx.out()) { - if (!opt) return detail::write(ctx.out(), none); - - auto out = ctx.out(); - out = detail::write(out, optional); - ctx.advance_to(out); - out = underlying_.format(*opt, ctx); - return detail::write(out, ')'); - } -}; -FMT_END_NAMESPACE -#endif // __cpp_lib_optional - #if defined(__cpp_lib_expected) || FMT_CPP_LIB_VARIANT - -FMT_BEGIN_NAMESPACE -namespace detail { - template auto write_escaped_alternative(OutputIt out, const T& v) -> OutputIt { if constexpr (has_to_string_view::value) @@ -266,69 +120,9 @@ auto write_escaped_alternative(OutputIt out, const T& v) -> OutputIt { if constexpr (std::is_same_v) return write_escaped_char(out, v); return write(out, v); } - -} // namespace detail - -FMT_END_NAMESPACE -#endif - -#ifdef __cpp_lib_expected -FMT_BEGIN_NAMESPACE - -template -struct formatter, Char, - std::enable_if_t<(std::is_void::value || - is_formattable::value) && - is_formattable::value>> { - FMT_CONSTEXPR auto parse(parse_context& ctx) -> const Char* { - return ctx.begin(); - } - - template - auto format(const std::expected& value, FormatContext& ctx) const - -> decltype(ctx.out()) { - auto out = ctx.out(); - - if (value.has_value()) { - out = detail::write(out, "expected("); - if constexpr (!std::is_void::value) - out = detail::write_escaped_alternative(out, *value); - } else { - out = detail::write(out, "unexpected("); - out = detail::write_escaped_alternative(out, value.error()); - } - *out++ = ')'; - return out; - } -}; -FMT_END_NAMESPACE -#endif // __cpp_lib_expected - -#ifdef __cpp_lib_source_location -FMT_BEGIN_NAMESPACE -template <> struct formatter { - FMT_CONSTEXPR auto parse(parse_context<>& ctx) { return ctx.begin(); } - - template - auto format(const std::source_location& loc, FormatContext& ctx) const - -> decltype(ctx.out()) { - auto out = ctx.out(); - out = detail::write(out, loc.file_name()); - out = detail::write(out, ':'); - out = detail::write(out, loc.line()); - out = detail::write(out, ':'); - out = detail::write(out, loc.column()); - out = detail::write(out, ": "); - out = detail::write(out, loc.function_name()); - return out; - } -}; -FMT_END_NAMESPACE #endif #if FMT_CPP_LIB_VARIANT -FMT_BEGIN_NAMESPACE -namespace detail { template using variant_index_sequence = @@ -350,116 +144,9 @@ template class is_variant_formattable_ { decltype(check(variant_index_sequence{}))::value; }; -} // namespace detail - -template struct is_variant_like { - static constexpr const bool value = detail::is_variant_like_::value; -}; - -template struct is_variant_formattable { - static constexpr const bool value = - detail::is_variant_formattable_::value; -}; - -template struct formatter { - FMT_CONSTEXPR auto parse(parse_context& ctx) -> const Char* { - return ctx.begin(); - } - - template - auto format(const std::monostate&, FormatContext& ctx) const - -> decltype(ctx.out()) { - return detail::write(ctx.out(), "monostate"); - } -}; - -template -struct formatter< - Variant, Char, - std::enable_if_t, is_variant_formattable>>> { - FMT_CONSTEXPR auto parse(parse_context& ctx) -> const Char* { - return ctx.begin(); - } - - template - auto format(const Variant& value, FormatContext& ctx) const - -> decltype(ctx.out()) { - auto out = ctx.out(); - - out = detail::write(out, "variant("); - FMT_TRY { - std::visit( - [&](const auto& v) { - out = detail::write_escaped_alternative(out, v); - }, - value); - } - FMT_CATCH(const std::bad_variant_access&) { - detail::write(out, "valueless by exception"); - } - *out++ = ')'; - return out; - } -}; -FMT_END_NAMESPACE #endif // FMT_CPP_LIB_VARIANT -FMT_BEGIN_NAMESPACE -template <> struct formatter { - private: - format_specs specs_; - detail::arg_ref width_ref_; - bool debug_ = false; - - public: - FMT_CONSTEXPR auto parse(parse_context<>& ctx) -> const char* { - auto it = ctx.begin(), end = ctx.end(); - if (it == end) return it; - - it = detail::parse_align(it, end, specs_); - - char c = *it; - if (it != end && ((c >= '0' && c <= '9') || c == '{')) - it = detail::parse_width(it, end, specs_, width_ref_, ctx); - - if (it != end && *it == '?') { - debug_ = true; - ++it; - } - if (it != end && *it == 's') { - specs_.set_type(presentation_type::string); - ++it; - } - return it; - } - - template - FMT_CONSTEXPR20 auto format(const std::error_code& ec, - FormatContext& ctx) const -> decltype(ctx.out()) { - auto specs = specs_; - detail::handle_dynamic_spec(specs.dynamic_width(), specs.width, width_ref_, - ctx); - auto buf = memory_buffer(); - if (specs_.type() == presentation_type::string) { - buf.append(ec.message()); - } else { - buf.append(string_view(ec.category().name())); - buf.push_back(':'); - detail::write(appender(buf), ec.value()); - } - auto quoted = memory_buffer(); - auto str = string_view(buf.data(), buf.size()); - if (debug_) { - detail::write_escaped_string(std::back_inserter(quoted), str); - str = string_view(quoted.data(), quoted.size()); - } - return detail::write(ctx.out(), str, specs); - } -}; - #if FMT_USE_RTTI -namespace detail { template auto write_demangled_name(OutputIt out, const std::type_info& ti) -> OutputIt { @@ -527,8 +214,327 @@ auto write_demangled_name(OutputIt out, const std::type_info& ti) -> OutputIt { # endif } +#endif // FMT_USE_RTTI + +template +struct has_flip : std::false_type {}; + +template +struct has_flip().flip())>> + : std::true_type {}; + +template struct is_bit_reference_like { + static constexpr const bool value = + std::is_convertible::value && + std::is_nothrow_assignable::value && has_flip::value; +}; + +// Workaround for libc++ incompatibility with C++ standard. +// According to the Standard, `bitset::operator[] const` returns bool. +#ifdef _LIBCPP_VERSION +template +struct is_bit_reference_like> { + static constexpr const bool value = true; +}; +#endif + } // namespace detail +#if FMT_CPP_LIB_FILESYSTEM + +template struct formatter { + private: + format_specs specs_; + detail::arg_ref width_ref_; + bool debug_ = false; + char path_type_ = 0; + + public: + FMT_CONSTEXPR void set_debug_format(bool set = true) { debug_ = set; } + + FMT_CONSTEXPR auto parse(parse_context& ctx) { + auto it = ctx.begin(), end = ctx.end(); + if (it == end) return it; + + it = detail::parse_align(it, end, specs_); + if (it == end) return it; + + Char c = *it; + if ((c >= '0' && c <= '9') || c == '{') + it = detail::parse_width(it, end, specs_, width_ref_, ctx); + if (it != end && *it == '?') { + debug_ = true; + ++it; + } + if (it != end && (*it == 'g')) path_type_ = detail::to_ascii(*it++); + return it; + } + + template + auto format(const std::filesystem::path& p, FormatContext& ctx) const { + auto specs = specs_; + auto path_string = + !path_type_ ? p.native() + : p.generic_string(); + + detail::handle_dynamic_spec(specs.dynamic_width(), specs.width, width_ref_, + ctx); + if (!debug_) { + auto s = detail::get_path_string(p, path_string); + return detail::write(ctx.out(), basic_string_view(s), specs); + } + auto quoted = basic_memory_buffer(); + detail::write_escaped_path(quoted, p, path_string); + return detail::write(ctx.out(), + basic_string_view(quoted.data(), quoted.size()), + specs); + } +}; + +class path : public std::filesystem::path { + public: + auto display_string() const -> std::string { + const std::filesystem::path& base = *this; + return fmt::format(FMT_STRING("{}"), base); + } + auto system_string() const -> std::string { return string(); } + + auto generic_display_string() const -> std::string { + const std::filesystem::path& base = *this; + return fmt::format(FMT_STRING("{:g}"), base); + } + auto generic_system_string() const -> std::string { return generic_string(); } +}; + +#endif // FMT_CPP_LIB_FILESYSTEM + +template +struct formatter, Char> + : nested_formatter, Char> { + private: + // Functor because C++11 doesn't support generic lambdas. + struct writer { + const std::bitset& bs; + + template + FMT_CONSTEXPR auto operator()(OutputIt out) -> OutputIt { + for (auto pos = N; pos > 0; --pos) { + out = detail::write(out, bs[pos - 1] ? Char('1') : Char('0')); + } + + return out; + } + }; + + public: + template + auto format(const std::bitset& bs, FormatContext& ctx) const + -> decltype(ctx.out()) { + return this->write_padded(ctx, writer{bs}); + } +}; + +template +struct formatter : basic_ostream_formatter {}; + +#ifdef __cpp_lib_optional +template +struct formatter, Char, + std::enable_if_t::value>> { + private: + formatter underlying_; + static constexpr basic_string_view optional = + detail::string_literal{}; + static constexpr basic_string_view none = + detail::string_literal{}; + + template + FMT_CONSTEXPR static auto maybe_set_debug_format(U& u, bool set) + -> decltype(u.set_debug_format(set)) { + u.set_debug_format(set); + } + + template + FMT_CONSTEXPR static void maybe_set_debug_format(U&, ...) {} + + public: + FMT_CONSTEXPR auto parse(parse_context& ctx) { + maybe_set_debug_format(underlying_, true); + return underlying_.parse(ctx); + } + + template + auto format(const std::optional& opt, FormatContext& ctx) const + -> decltype(ctx.out()) { + if (!opt) return detail::write(ctx.out(), none); + + auto out = ctx.out(); + out = detail::write(out, optional); + ctx.advance_to(out); + out = underlying_.format(*opt, ctx); + return detail::write(out, ')'); + } +}; +#endif // __cpp_lib_optional + +#ifdef __cpp_lib_expected +template +struct formatter, Char, + std::enable_if_t<(std::is_void::value || + is_formattable::value) && + is_formattable::value>> { + FMT_CONSTEXPR auto parse(parse_context& ctx) -> const Char* { + return ctx.begin(); + } + + template + auto format(const std::expected& value, FormatContext& ctx) const + -> decltype(ctx.out()) { + auto out = ctx.out(); + + if (value.has_value()) { + out = detail::write(out, "expected("); + if constexpr (!std::is_void::value) + out = detail::write_escaped_alternative(out, *value); + } else { + out = detail::write(out, "unexpected("); + out = detail::write_escaped_alternative(out, value.error()); + } + *out++ = ')'; + return out; + } +}; +#endif // __cpp_lib_expected + +#ifdef __cpp_lib_source_location +template <> struct formatter { + FMT_CONSTEXPR auto parse(parse_context<>& ctx) { return ctx.begin(); } + + template + auto format(const std::source_location& loc, FormatContext& ctx) const + -> decltype(ctx.out()) { + auto out = ctx.out(); + out = detail::write(out, loc.file_name()); + out = detail::write(out, ':'); + out = detail::write(out, loc.line()); + out = detail::write(out, ':'); + out = detail::write(out, loc.column()); + out = detail::write(out, ": "); + out = detail::write(out, loc.function_name()); + return out; + } +}; +#endif + +#if FMT_CPP_LIB_VARIANT + +template struct is_variant_like { + static constexpr const bool value = detail::is_variant_like_::value; +}; + +template struct is_variant_formattable { + static constexpr const bool value = + detail::is_variant_formattable_::value; +}; + +template struct formatter { + FMT_CONSTEXPR auto parse(parse_context& ctx) -> const Char* { + return ctx.begin(); + } + + template + auto format(const std::monostate&, FormatContext& ctx) const + -> decltype(ctx.out()) { + return detail::write(ctx.out(), "monostate"); + } +}; + +template +struct formatter< + Variant, Char, + std::enable_if_t, is_variant_formattable>>> { + FMT_CONSTEXPR auto parse(parse_context& ctx) -> const Char* { + return ctx.begin(); + } + + template + auto format(const Variant& value, FormatContext& ctx) const + -> decltype(ctx.out()) { + auto out = ctx.out(); + + out = detail::write(out, "variant("); + FMT_TRY { + std::visit( + [&](const auto& v) { + out = detail::write_escaped_alternative(out, v); + }, + value); + } + FMT_CATCH(const std::bad_variant_access&) { + detail::write(out, "valueless by exception"); + } + *out++ = ')'; + return out; + } +}; + +#endif // FMT_CPP_LIB_VARIANT + +template <> struct formatter { + private: + format_specs specs_; + detail::arg_ref width_ref_; + bool debug_ = false; + + public: + FMT_CONSTEXPR auto parse(parse_context<>& ctx) -> const char* { + auto it = ctx.begin(), end = ctx.end(); + if (it == end) return it; + + it = detail::parse_align(it, end, specs_); + + char c = *it; + if (it != end && ((c >= '0' && c <= '9') || c == '{')) + it = detail::parse_width(it, end, specs_, width_ref_, ctx); + + if (it != end && *it == '?') { + debug_ = true; + ++it; + } + if (it != end && *it == 's') { + specs_.set_type(presentation_type::string); + ++it; + } + return it; + } + + template + FMT_CONSTEXPR20 auto format(const std::error_code& ec, + FormatContext& ctx) const -> decltype(ctx.out()) { + auto specs = specs_; + detail::handle_dynamic_spec(specs.dynamic_width(), specs.width, width_ref_, + ctx); + auto buf = memory_buffer(); + if (specs_.type() == presentation_type::string) { + buf.append(ec.message()); + } else { + buf.append(string_view(ec.category().name())); + buf.push_back(':'); + detail::write(appender(buf), ec.value()); + } + auto quoted = memory_buffer(); + auto str = string_view(buf.data(), buf.size()); + if (debug_) { + detail::write_escaped_string(std::back_inserter(quoted), str); + str = string_view(quoted.data(), quoted.size()); + } + return detail::write(ctx.out(), str, specs); + } +}; + +#if FMT_USE_RTTI template struct formatter { @@ -543,7 +549,7 @@ struct formatter(ctx.out(), ti); } }; -#endif +#endif // FMT_USE_RTTI template struct formatter< @@ -579,34 +585,6 @@ struct formatter< } }; -namespace detail { - -template -struct has_flip : std::false_type {}; - -template -struct has_flip().flip())>> - : std::true_type {}; - -template struct is_bit_reference_like { - static constexpr const bool value = - std::is_convertible::value && - std::is_nothrow_assignable::value && has_flip::value; -}; - -#ifdef _LIBCPP_VERSION - -// Workaround for libc++ incompatibility with C++ standard. -// According to the Standard, `bitset::operator[] const` returns bool. -template -struct is_bit_reference_like> { - static constexpr const bool value = true; -}; - -#endif - -} // namespace detail - // We can't use std::vector::reference and // std::bitset::reference because the compiler can't deduce Allocator and N // in partial specialization. @@ -725,4 +703,5 @@ struct formatter, Char, }; FMT_END_NAMESPACE + #endif // FMT_STD_H_