2022-05-17 00:32:25 +00:00
|
|
|
// Formatting library for C++ - formatters for standard library types
|
|
|
|
//
|
|
|
|
// Copyright (c) 2012 - present, Victor Zverovich
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// For the license information refer to format.h.
|
|
|
|
|
|
|
|
#ifndef FMT_STD_H_
|
|
|
|
#define FMT_STD_H_
|
|
|
|
|
2024-06-16 17:59:06 +00:00
|
|
|
#include "format.h"
|
|
|
|
#include "ostream.h"
|
|
|
|
|
2024-06-14 17:53:51 +00:00
|
|
|
#ifndef FMT_MODULE
|
2024-04-10 18:48:32 +00:00
|
|
|
# include <atomic>
|
|
|
|
# include <bitset>
|
|
|
|
# include <complex>
|
|
|
|
# include <cstdlib>
|
|
|
|
# include <exception>
|
|
|
|
# include <memory>
|
|
|
|
# include <thread>
|
|
|
|
# include <type_traits>
|
|
|
|
# include <typeinfo>
|
|
|
|
# include <utility>
|
|
|
|
# include <vector>
|
2022-05-17 23:31:06 +00:00
|
|
|
|
2024-06-17 13:58:33 +00:00
|
|
|
// Check FMT_CPLUSPLUS to suppress a bogus warning in MSVC.
|
2024-04-10 18:48:32 +00:00
|
|
|
# if FMT_CPLUSPLUS >= 201703L
|
|
|
|
# if FMT_HAS_INCLUDE(<filesystem>)
|
|
|
|
# include <filesystem>
|
|
|
|
# endif
|
|
|
|
# if FMT_HAS_INCLUDE(<variant>)
|
|
|
|
# include <variant>
|
|
|
|
# endif
|
|
|
|
# if FMT_HAS_INCLUDE(<optional>)
|
|
|
|
# include <optional>
|
|
|
|
# endif
|
2024-06-19 15:49:43 +00:00
|
|
|
# endif
|
|
|
|
// Use > instead of >= in the version check because <source_location> may be
|
|
|
|
// available after C++17 but before C++20 is marked as implemented.
|
|
|
|
# if FMT_CPLUSPLUS > 201703L && FMT_HAS_INCLUDE(<source_location>)
|
|
|
|
# include <source_location>
|
2023-02-25 14:45:56 +00:00
|
|
|
# endif
|
2024-06-16 18:10:38 +00:00
|
|
|
# if FMT_CPLUSPLUS > 202002L && FMT_HAS_INCLUDE(<expected>)
|
2024-04-10 18:48:32 +00:00
|
|
|
# include <expected>
|
|
|
|
# endif
|
2024-06-16 17:59:06 +00:00
|
|
|
#endif // FMT_MODULE
|
|
|
|
|
|
|
|
#if FMT_HAS_INCLUDE(<version>)
|
|
|
|
# include <version>
|
2023-11-30 18:05:56 +00:00
|
|
|
#endif
|
|
|
|
|
2022-09-10 15:04:00 +00:00
|
|
|
// GCC 4 does not support FMT_HAS_INCLUDE.
|
|
|
|
#if FMT_HAS_INCLUDE(<cxxabi.h>) || defined(__GLIBCXX__)
|
|
|
|
# include <cxxabi.h>
|
2023-02-21 23:14:41 +00:00
|
|
|
// Android NDK with gabi++ library on some architectures does not implement
|
2022-09-10 15:04:00 +00:00
|
|
|
// abi::__cxa_demangle().
|
|
|
|
# ifndef __GABIXX_CXXABI_H__
|
|
|
|
# define FMT_HAS_ABI_CXA_DEMANGLE
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2023-11-15 17:19:08 +00:00
|
|
|
// For older Xcode versions, __cpp_lib_xxx flags are inaccurately defined.
|
|
|
|
#ifndef FMT_CPP_LIB_FILESYSTEM
|
|
|
|
# ifdef __cpp_lib_filesystem
|
|
|
|
# define FMT_CPP_LIB_FILESYSTEM __cpp_lib_filesystem
|
|
|
|
# else
|
|
|
|
# define FMT_CPP_LIB_FILESYSTEM 0
|
2023-11-30 15:34:26 +00:00
|
|
|
# endif
|
2023-11-15 17:19:08 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef FMT_CPP_LIB_VARIANT
|
|
|
|
# ifdef __cpp_lib_variant
|
|
|
|
# define FMT_CPP_LIB_VARIANT __cpp_lib_variant
|
|
|
|
# else
|
|
|
|
# define FMT_CPP_LIB_VARIANT 0
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if FMT_CPP_LIB_FILESYSTEM
|
2022-05-18 13:04:47 +00:00
|
|
|
FMT_BEGIN_NAMESPACE
|
2022-05-21 15:12:32 +00:00
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
|
2023-11-30 15:34:26 +00:00
|
|
|
template <typename Char, typename PathChar>
|
|
|
|
auto get_path_string(const std::filesystem::path& p,
|
|
|
|
const std::basic_string<PathChar>& native) {
|
2023-11-07 00:28:46 +00:00
|
|
|
if constexpr (std::is_same_v<Char, char> && std::is_same_v<PathChar, wchar_t>)
|
|
|
|
return to_utf8<wchar_t>(native, to_utf8_error_policy::replace);
|
|
|
|
else
|
|
|
|
return p.string<Char>();
|
2023-07-21 00:18:03 +00:00
|
|
|
}
|
|
|
|
|
2023-11-07 15:46:15 +00:00
|
|
|
template <typename Char, typename PathChar>
|
2022-05-21 15:12:32 +00:00
|
|
|
void write_escaped_path(basic_memory_buffer<Char>& quoted,
|
2023-11-07 15:46:15 +00:00
|
|
|
const std::filesystem::path& p,
|
|
|
|
const std::basic_string<PathChar>& native) {
|
2023-11-30 15:34:26 +00:00
|
|
|
if constexpr (std::is_same_v<Char, char> &&
|
|
|
|
std::is_same_v<PathChar, wchar_t>) {
|
2023-11-07 15:46:15 +00:00
|
|
|
auto buf = basic_memory_buffer<wchar_t>();
|
|
|
|
write_escaped_string<wchar_t>(std::back_inserter(buf), native);
|
|
|
|
bool valid = to_utf8<wchar_t>::convert(quoted, {buf.data(), buf.size()});
|
|
|
|
FMT_ASSERT(valid, "invalid utf16");
|
|
|
|
} else if constexpr (std::is_same_v<Char, PathChar>) {
|
|
|
|
write_escaped_string<std::filesystem::path::value_type>(
|
|
|
|
std::back_inserter(quoted), native);
|
|
|
|
} else {
|
|
|
|
write_escaped_string<Char>(std::back_inserter(quoted), p.string<Char>());
|
|
|
|
}
|
2022-05-21 15:12:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
|
2023-05-17 14:37:53 +00:00
|
|
|
FMT_EXPORT
|
2023-07-20 22:36:05 +00:00
|
|
|
template <typename Char> struct formatter<std::filesystem::path, Char> {
|
2023-07-20 22:25:35 +00:00
|
|
|
private:
|
2024-01-17 15:15:50 +00:00
|
|
|
format_specs specs_;
|
2023-07-20 22:25:35 +00:00
|
|
|
detail::arg_ref<Char> width_ref_;
|
2023-07-21 00:18:03 +00:00
|
|
|
bool debug_ = false;
|
2023-12-28 15:27:33 +00:00
|
|
|
char path_type_ = 0;
|
2023-07-20 22:25:35 +00:00
|
|
|
|
|
|
|
public:
|
2023-07-21 00:18:03 +00:00
|
|
|
FMT_CONSTEXPR void set_debug_format(bool set = true) { debug_ = set; }
|
|
|
|
|
2024-09-01 21:32:55 +00:00
|
|
|
FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) {
|
2023-07-20 22:25:35 +00:00
|
|
|
auto it = ctx.begin(), end = ctx.end();
|
|
|
|
if (it == end) return it;
|
|
|
|
|
|
|
|
it = detail::parse_align(it, end, specs_);
|
|
|
|
if (it == end) return it;
|
|
|
|
|
2024-07-27 16:54:40 +00:00
|
|
|
Char c = *it;
|
|
|
|
if ((c >= '0' && c <= '9') || c == '{')
|
2024-08-10 15:10:55 +00:00
|
|
|
it = detail::parse_width(it, end, specs_, width_ref_, ctx);
|
2023-07-21 00:18:03 +00:00
|
|
|
if (it != end && *it == '?') {
|
|
|
|
debug_ = true;
|
|
|
|
++it;
|
|
|
|
}
|
2024-01-13 16:02:31 +00:00
|
|
|
if (it != end && (*it == 'g')) path_type_ = detail::to_ascii(*it++);
|
2023-07-20 22:25:35 +00:00
|
|
|
return it;
|
2023-03-05 16:01:06 +00:00
|
|
|
}
|
2023-07-20 22:25:35 +00:00
|
|
|
|
2022-05-21 15:12:32 +00:00
|
|
|
template <typename FormatContext>
|
2023-07-20 22:25:35 +00:00
|
|
|
auto format(const std::filesystem::path& p, FormatContext& ctx) const {
|
|
|
|
auto specs = specs_;
|
2024-03-18 22:47:42 +00:00
|
|
|
auto path_string =
|
|
|
|
!path_type_ ? p.native()
|
|
|
|
: p.generic_string<std::filesystem::path::value_type>();
|
2023-11-29 22:49:36 +00:00
|
|
|
|
2024-08-10 15:10:55 +00:00
|
|
|
detail::handle_dynamic_spec(specs.dynamic_width(), specs.width, width_ref_,
|
|
|
|
ctx);
|
2023-07-21 00:18:03 +00:00
|
|
|
if (!debug_) {
|
2023-11-29 22:49:36 +00:00
|
|
|
auto s = detail::get_path_string<Char>(p, path_string);
|
2023-07-21 00:18:03 +00:00
|
|
|
return detail::write(ctx.out(), basic_string_view<Char>(s), specs);
|
|
|
|
}
|
2023-04-30 16:09:35 +00:00
|
|
|
auto quoted = basic_memory_buffer<Char>();
|
2023-11-29 22:49:36 +00:00
|
|
|
detail::write_escaped_path(quoted, p, path_string);
|
2023-07-20 22:36:05 +00:00
|
|
|
return detail::write(ctx.out(),
|
|
|
|
basic_string_view<Char>(quoted.data(), quoted.size()),
|
|
|
|
specs);
|
2022-05-21 15:12:32 +00:00
|
|
|
}
|
2022-05-18 13:07:24 +00:00
|
|
|
};
|
2024-06-21 15:02:12 +00:00
|
|
|
|
|
|
|
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(); }
|
|
|
|
};
|
|
|
|
|
2022-05-18 13:04:47 +00:00
|
|
|
FMT_END_NAMESPACE
|
2023-11-30 15:34:26 +00:00
|
|
|
#endif // FMT_CPP_LIB_FILESYSTEM
|
2022-05-17 00:32:25 +00:00
|
|
|
|
2022-05-17 23:31:06 +00:00
|
|
|
FMT_BEGIN_NAMESPACE
|
2023-10-03 16:53:47 +00:00
|
|
|
FMT_EXPORT
|
|
|
|
template <std::size_t N, typename Char>
|
|
|
|
struct formatter<std::bitset<N>, Char> : nested_formatter<string_view> {
|
|
|
|
private:
|
|
|
|
// Functor because C++11 doesn't support generic lambdas.
|
|
|
|
struct writer {
|
|
|
|
const std::bitset<N>& bs;
|
|
|
|
|
|
|
|
template <typename OutputIt>
|
2023-12-20 01:51:41 +00:00
|
|
|
FMT_CONSTEXPR auto operator()(OutputIt out) -> OutputIt {
|
2023-10-03 16:53:47 +00:00
|
|
|
for (auto pos = N; pos > 0; --pos) {
|
|
|
|
out = detail::write<Char>(out, bs[pos - 1] ? Char('1') : Char('0'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
template <typename FormatContext>
|
|
|
|
auto format(const std::bitset<N>& bs, FormatContext& ctx) const
|
|
|
|
-> decltype(ctx.out()) {
|
|
|
|
return write_padded(ctx, writer{bs});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-05-17 14:37:53 +00:00
|
|
|
FMT_EXPORT
|
2022-05-18 13:07:24 +00:00
|
|
|
template <typename Char>
|
|
|
|
struct formatter<std::thread::id, Char> : basic_ostream_formatter<Char> {};
|
2022-05-17 23:31:06 +00:00
|
|
|
FMT_END_NAMESPACE
|
|
|
|
|
2023-02-25 14:45:56 +00:00
|
|
|
#ifdef __cpp_lib_optional
|
|
|
|
FMT_BEGIN_NAMESPACE
|
2023-05-17 14:37:53 +00:00
|
|
|
FMT_EXPORT
|
2023-02-25 14:45:56 +00:00
|
|
|
template <typename T, typename Char>
|
|
|
|
struct formatter<std::optional<T>, Char,
|
|
|
|
std::enable_if_t<is_formattable<T, Char>::value>> {
|
|
|
|
private:
|
|
|
|
formatter<T, Char> underlying_;
|
|
|
|
static constexpr basic_string_view<Char> optional =
|
|
|
|
detail::string_literal<Char, 'o', 'p', 't', 'i', 'o', 'n', 'a', 'l',
|
|
|
|
'('>{};
|
|
|
|
static constexpr basic_string_view<Char> none =
|
|
|
|
detail::string_literal<Char, 'n', 'o', 'n', 'e'>{};
|
|
|
|
|
|
|
|
template <class U>
|
|
|
|
FMT_CONSTEXPR static auto maybe_set_debug_format(U& u, bool set)
|
|
|
|
-> decltype(u.set_debug_format(set)) {
|
|
|
|
u.set_debug_format(set);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class U>
|
|
|
|
FMT_CONSTEXPR static void maybe_set_debug_format(U&, ...) {}
|
|
|
|
|
|
|
|
public:
|
2024-09-01 21:32:55 +00:00
|
|
|
FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) {
|
2023-02-25 14:45:56 +00:00
|
|
|
maybe_set_debug_format(underlying_, true);
|
|
|
|
return underlying_.parse(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename FormatContext>
|
2023-11-30 16:21:57 +00:00
|
|
|
auto format(const std::optional<T>& opt, FormatContext& ctx) const
|
2023-02-25 14:45:56 +00:00
|
|
|
-> decltype(ctx.out()) {
|
|
|
|
if (!opt) return detail::write<Char>(ctx.out(), none);
|
|
|
|
|
|
|
|
auto out = ctx.out();
|
|
|
|
out = detail::write<Char>(out, optional);
|
|
|
|
ctx.advance_to(out);
|
|
|
|
out = underlying_.format(*opt, ctx);
|
|
|
|
return detail::write(out, ')');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
FMT_END_NAMESPACE
|
|
|
|
#endif // __cpp_lib_optional
|
|
|
|
|
2024-02-03 17:06:15 +00:00
|
|
|
#if defined(__cpp_lib_expected) || FMT_CPP_LIB_VARIANT
|
|
|
|
|
|
|
|
FMT_BEGIN_NAMESPACE
|
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
template <typename Char, typename OutputIt, typename T>
|
|
|
|
auto write_escaped_alternative(OutputIt out, const T& v) -> OutputIt {
|
|
|
|
if constexpr (has_to_string_view<T>::value)
|
|
|
|
return write_escaped_string<Char>(out, detail::to_string_view(v));
|
|
|
|
if constexpr (std::is_same_v<T, Char>) return write_escaped_char(out, v);
|
|
|
|
return write<Char>(out, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
FMT_END_NAMESPACE
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef __cpp_lib_expected
|
|
|
|
FMT_BEGIN_NAMESPACE
|
|
|
|
|
|
|
|
FMT_EXPORT
|
|
|
|
template <typename T, typename E, typename Char>
|
2024-02-19 18:19:39 +00:00
|
|
|
struct formatter<std::expected<T, E>, Char,
|
2024-09-03 16:44:10 +00:00
|
|
|
std::enable_if_t<(std::is_void<T>::value ||
|
|
|
|
is_formattable<T, Char>::value) &&
|
2024-02-19 18:19:39 +00:00
|
|
|
is_formattable<E, Char>::value>> {
|
2024-09-01 21:32:55 +00:00
|
|
|
FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) -> const Char* {
|
2024-02-03 17:06:15 +00:00
|
|
|
return ctx.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename FormatContext>
|
|
|
|
auto format(const std::expected<T, E>& value, FormatContext& ctx) const
|
|
|
|
-> decltype(ctx.out()) {
|
|
|
|
auto out = ctx.out();
|
|
|
|
|
|
|
|
if (value.has_value()) {
|
|
|
|
out = detail::write<Char>(out, "expected(");
|
2024-09-03 16:44:10 +00:00
|
|
|
if constexpr (!std::is_void<T>::value)
|
|
|
|
out = detail::write_escaped_alternative<Char>(out, *value);
|
2024-02-03 17:06:15 +00:00
|
|
|
} else {
|
|
|
|
out = detail::write<Char>(out, "unexpected(");
|
|
|
|
out = detail::write_escaped_alternative<Char>(out, value.error());
|
|
|
|
}
|
|
|
|
*out++ = ')';
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
FMT_END_NAMESPACE
|
|
|
|
#endif // __cpp_lib_expected
|
|
|
|
|
2023-11-30 18:05:56 +00:00
|
|
|
#ifdef __cpp_lib_source_location
|
|
|
|
FMT_BEGIN_NAMESPACE
|
|
|
|
FMT_EXPORT
|
2023-12-13 14:56:42 +00:00
|
|
|
template <> struct formatter<std::source_location> {
|
2024-09-01 21:32:55 +00:00
|
|
|
FMT_CONSTEXPR auto parse(parse_context<>& ctx) { return ctx.begin(); }
|
2023-11-30 18:05:56 +00:00
|
|
|
|
|
|
|
template <typename FormatContext>
|
|
|
|
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<char>(out, loc.line());
|
|
|
|
out = detail::write(out, ':');
|
|
|
|
out = detail::write<char>(out, loc.column());
|
|
|
|
out = detail::write(out, ": ");
|
|
|
|
out = detail::write(out, loc.function_name());
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
FMT_END_NAMESPACE
|
|
|
|
#endif
|
|
|
|
|
2023-11-15 17:19:08 +00:00
|
|
|
#if FMT_CPP_LIB_VARIANT
|
2022-06-26 14:28:01 +00:00
|
|
|
FMT_BEGIN_NAMESPACE
|
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
using variant_index_sequence =
|
|
|
|
std::make_index_sequence<std::variant_size<T>::value>;
|
|
|
|
|
2022-09-03 13:35:55 +00:00
|
|
|
template <typename> struct is_variant_like_ : std::false_type {};
|
2022-09-03 04:08:07 +00:00
|
|
|
template <typename... Types>
|
|
|
|
struct is_variant_like_<std::variant<Types...>> : std::true_type {};
|
2022-06-26 14:28:01 +00:00
|
|
|
|
2022-09-03 04:08:07 +00:00
|
|
|
// formattable element check.
|
2022-06-26 14:28:01 +00:00
|
|
|
template <typename T, typename C> class is_variant_formattable_ {
|
2022-09-03 13:35:55 +00:00
|
|
|
template <std::size_t... Is>
|
2022-06-26 14:28:01 +00:00
|
|
|
static std::conjunction<
|
2022-09-03 13:35:55 +00:00
|
|
|
is_formattable<std::variant_alternative_t<Is, T>, C>...>
|
|
|
|
check(std::index_sequence<Is...>);
|
2022-06-26 14:28:01 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
static constexpr const bool value =
|
|
|
|
decltype(check(variant_index_sequence<T>{}))::value;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace detail
|
2023-07-20 23:26:27 +00:00
|
|
|
|
2022-06-26 14:28:01 +00:00
|
|
|
template <typename T> struct is_variant_like {
|
|
|
|
static constexpr const bool value = detail::is_variant_like_<T>::value;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T, typename C> struct is_variant_formattable {
|
|
|
|
static constexpr const bool value =
|
|
|
|
detail::is_variant_formattable_<T, C>::value;
|
|
|
|
};
|
|
|
|
|
2023-07-20 23:26:27 +00:00
|
|
|
FMT_EXPORT
|
|
|
|
template <typename Char> struct formatter<std::monostate, Char> {
|
2024-09-01 21:32:55 +00:00
|
|
|
FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) -> const Char* {
|
2023-07-20 23:26:27 +00:00
|
|
|
return ctx.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename FormatContext>
|
|
|
|
auto format(const std::monostate&, FormatContext& ctx) const
|
|
|
|
-> decltype(ctx.out()) {
|
|
|
|
return detail::write<Char>(ctx.out(), "monostate");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-05-17 14:37:53 +00:00
|
|
|
FMT_EXPORT
|
2022-06-26 14:28:01 +00:00
|
|
|
template <typename Variant, typename Char>
|
|
|
|
struct formatter<
|
|
|
|
Variant, Char,
|
|
|
|
std::enable_if_t<std::conjunction_v<
|
|
|
|
is_variant_like<Variant>, is_variant_formattable<Variant, Char>>>> {
|
2024-09-01 21:32:55 +00:00
|
|
|
FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) -> const Char* {
|
2022-06-26 14:28:01 +00:00
|
|
|
return ctx.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename FormatContext>
|
|
|
|
auto format(const Variant& value, FormatContext& ctx) const
|
|
|
|
-> decltype(ctx.out()) {
|
|
|
|
auto out = ctx.out();
|
|
|
|
|
|
|
|
out = detail::write<Char>(out, "variant(");
|
2023-06-11 04:28:13 +00:00
|
|
|
FMT_TRY {
|
2023-03-18 14:07:06 +00:00
|
|
|
std::visit(
|
|
|
|
[&](const auto& v) {
|
2024-02-03 17:06:15 +00:00
|
|
|
out = detail::write_escaped_alternative<Char>(out, v);
|
2023-03-18 14:07:06 +00:00
|
|
|
},
|
|
|
|
value);
|
2023-06-11 04:28:13 +00:00
|
|
|
}
|
|
|
|
FMT_CATCH(const std::bad_variant_access&) {
|
2023-03-18 14:07:06 +00:00
|
|
|
detail::write<Char>(out, "valueless by exception");
|
|
|
|
}
|
2022-06-26 14:28:01 +00:00
|
|
|
*out++ = ')';
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
FMT_END_NAMESPACE
|
2023-11-15 17:19:08 +00:00
|
|
|
#endif // FMT_CPP_LIB_VARIANT
|
2022-09-02 15:33:37 +00:00
|
|
|
|
|
|
|
FMT_BEGIN_NAMESPACE
|
2023-05-17 14:37:53 +00:00
|
|
|
FMT_EXPORT
|
2022-10-02 00:46:09 +00:00
|
|
|
template <typename Char> struct formatter<std::error_code, Char> {
|
2024-09-01 21:32:55 +00:00
|
|
|
FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) -> const Char* {
|
2022-10-02 00:46:09 +00:00
|
|
|
return ctx.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename FormatContext>
|
|
|
|
FMT_CONSTEXPR auto format(const std::error_code& ec, FormatContext& ctx) const
|
|
|
|
-> decltype(ctx.out()) {
|
|
|
|
auto out = ctx.out();
|
2024-01-17 15:15:50 +00:00
|
|
|
out = detail::write_bytes<Char>(out, ec.category().name(), format_specs());
|
2022-10-02 00:46:09 +00:00
|
|
|
out = detail::write<Char>(out, Char(':'));
|
|
|
|
out = detail::write<Char>(out, ec.value());
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-05-28 17:57:08 +00:00
|
|
|
#if FMT_USE_RTTI
|
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
template <typename Char, typename OutputIt>
|
|
|
|
auto write_demangled_name(OutputIt out, const std::type_info& ti) -> OutputIt {
|
|
|
|
# ifdef FMT_HAS_ABI_CXA_DEMANGLE
|
|
|
|
int status = 0;
|
|
|
|
std::size_t size = 0;
|
|
|
|
std::unique_ptr<char, void (*)(void*)> demangled_name_ptr(
|
|
|
|
abi::__cxa_demangle(ti.name(), nullptr, &size, &status), &std::free);
|
|
|
|
|
|
|
|
string_view demangled_name_view;
|
|
|
|
if (demangled_name_ptr) {
|
|
|
|
demangled_name_view = demangled_name_ptr.get();
|
|
|
|
|
|
|
|
// Normalization of stdlib inline namespace names.
|
|
|
|
// libc++ inline namespaces.
|
|
|
|
// std::__1::* -> std::*
|
|
|
|
// std::__1::__fs::* -> std::*
|
|
|
|
// libstdc++ inline namespaces.
|
|
|
|
// std::__cxx11::* -> std::*
|
|
|
|
// std::filesystem::__cxx11::* -> std::filesystem::*
|
|
|
|
if (demangled_name_view.starts_with("std::")) {
|
|
|
|
char* begin = demangled_name_ptr.get();
|
|
|
|
char* to = begin + 5; // std::
|
|
|
|
for (char *from = to, *end = begin + demangled_name_view.size();
|
|
|
|
from < end;) {
|
|
|
|
// This is safe, because demangled_name is NUL-terminated.
|
|
|
|
if (from[0] == '_' && from[1] == '_') {
|
|
|
|
char* next = from + 1;
|
|
|
|
while (next < end && *next != ':') next++;
|
|
|
|
if (next[0] == ':' && next[1] == ':') {
|
|
|
|
from = next + 2;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*to++ = *from++;
|
|
|
|
}
|
|
|
|
demangled_name_view = {begin, detail::to_unsigned(to - begin)};
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
demangled_name_view = string_view(ti.name());
|
|
|
|
}
|
|
|
|
return detail::write_bytes<Char>(out, demangled_name_view);
|
|
|
|
# elif FMT_MSC_VERSION
|
|
|
|
const string_view demangled_name(ti.name());
|
|
|
|
for (std::size_t i = 0; i < demangled_name.size(); ++i) {
|
|
|
|
auto sub = demangled_name;
|
|
|
|
sub.remove_prefix(i);
|
|
|
|
if (sub.starts_with("enum ")) {
|
|
|
|
i += 4;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (sub.starts_with("class ") || sub.starts_with("union ")) {
|
|
|
|
i += 5;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (sub.starts_with("struct ")) {
|
|
|
|
i += 6;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (*sub.begin() != ' ') *out++ = *sub.begin();
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
# else
|
|
|
|
return detail::write_bytes<Char>(out, string_view(ti.name()));
|
|
|
|
# endif
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
FMT_EXPORT
|
|
|
|
template <typename Char>
|
|
|
|
struct formatter<std::type_info, Char // DEPRECATED! Mixing code unit types.
|
|
|
|
> {
|
|
|
|
public:
|
2024-09-01 21:32:55 +00:00
|
|
|
FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) -> const Char* {
|
2024-05-28 17:57:08 +00:00
|
|
|
return ctx.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Context>
|
|
|
|
auto format(const std::type_info& ti, Context& ctx) const
|
|
|
|
-> decltype(ctx.out()) {
|
|
|
|
return detail::write_demangled_name<Char>(ctx.out(), ti);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2023-05-17 14:37:53 +00:00
|
|
|
FMT_EXPORT
|
2022-09-02 15:33:37 +00:00
|
|
|
template <typename T, typename Char>
|
|
|
|
struct formatter<
|
2023-12-13 14:56:42 +00:00
|
|
|
T, Char, // DEPRECATED! Mixing code unit types.
|
2022-09-10 15:04:00 +00:00
|
|
|
typename std::enable_if<std::is_base_of<std::exception, T>::value>::type> {
|
|
|
|
private:
|
|
|
|
bool with_typename_ = false;
|
|
|
|
|
|
|
|
public:
|
2024-09-01 21:32:55 +00:00
|
|
|
FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) -> const Char* {
|
2022-09-10 15:04:00 +00:00
|
|
|
auto it = ctx.begin();
|
|
|
|
auto end = ctx.end();
|
|
|
|
if (it == end || *it == '}') return it;
|
|
|
|
if (*it == 't') {
|
|
|
|
++it;
|
2024-05-19 18:21:55 +00:00
|
|
|
with_typename_ = FMT_USE_RTTI != 0;
|
2022-09-10 15:04:00 +00:00
|
|
|
}
|
|
|
|
return it;
|
|
|
|
}
|
|
|
|
|
2024-01-08 13:56:07 +00:00
|
|
|
template <typename Context>
|
|
|
|
auto format(const std::exception& ex, Context& ctx) const
|
|
|
|
-> decltype(ctx.out()) {
|
2022-09-10 15:04:00 +00:00
|
|
|
auto out = ctx.out();
|
2024-05-19 18:21:55 +00:00
|
|
|
#if FMT_USE_RTTI
|
2024-05-28 17:57:08 +00:00
|
|
|
if (with_typename_) {
|
|
|
|
out = detail::write_demangled_name<Char>(out, typeid(ex));
|
|
|
|
*out++ = ':';
|
|
|
|
*out++ = ' ';
|
2022-09-29 16:52:38 +00:00
|
|
|
}
|
2023-05-31 01:38:39 +00:00
|
|
|
#endif
|
2024-05-28 17:57:08 +00:00
|
|
|
return detail::write_bytes<Char>(out, string_view(ex.what()));
|
2022-09-02 15:33:37 +00:00
|
|
|
}
|
|
|
|
};
|
2023-08-06 18:36:30 +00:00
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
template <typename T, typename Enable = void>
|
|
|
|
struct has_flip : std::false_type {};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct has_flip<T, void_t<decltype(std::declval<T>().flip())>>
|
|
|
|
: std::true_type {};
|
|
|
|
|
|
|
|
template <typename T> struct is_bit_reference_like {
|
|
|
|
static constexpr const bool value =
|
|
|
|
std::is_convertible<T, bool>::value &&
|
|
|
|
std::is_nothrow_assignable<T, bool>::value && has_flip<T>::value;
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef _LIBCPP_VERSION
|
|
|
|
|
|
|
|
// Workaround for libc++ incompatibility with C++ standard.
|
|
|
|
// According to the Standard, `bitset::operator[] const` returns bool.
|
|
|
|
template <typename C>
|
|
|
|
struct is_bit_reference_like<std::__bit_const_reference<C>> {
|
|
|
|
static constexpr const bool value = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
// We can't use std::vector<bool, Allocator>::reference and
|
|
|
|
// std::bitset<N>::reference because the compiler can't deduce Allocator and N
|
|
|
|
// in partial specialization.
|
|
|
|
FMT_EXPORT
|
|
|
|
template <typename BitRef, typename Char>
|
|
|
|
struct formatter<BitRef, Char,
|
|
|
|
enable_if_t<detail::is_bit_reference_like<BitRef>::value>>
|
|
|
|
: formatter<bool, Char> {
|
|
|
|
template <typename FormatContext>
|
|
|
|
FMT_CONSTEXPR auto format(const BitRef& v, FormatContext& ctx) const
|
|
|
|
-> decltype(ctx.out()) {
|
|
|
|
return formatter<bool, Char>::format(v, ctx);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-01-11 03:34:31 +00:00
|
|
|
template <typename T, typename Deleter>
|
|
|
|
auto ptr(const std::unique_ptr<T, Deleter>& p) -> const void* {
|
|
|
|
return p.get();
|
|
|
|
}
|
|
|
|
template <typename T> auto ptr(const std::shared_ptr<T>& p) -> const void* {
|
|
|
|
return p.get();
|
|
|
|
}
|
|
|
|
|
2023-08-14 15:34:31 +00:00
|
|
|
FMT_EXPORT
|
|
|
|
template <typename T, typename Char>
|
|
|
|
struct formatter<std::atomic<T>, Char,
|
|
|
|
enable_if_t<is_formattable<T, Char>::value>>
|
|
|
|
: formatter<T, Char> {
|
|
|
|
template <typename FormatContext>
|
|
|
|
auto format(const std::atomic<T>& v, FormatContext& ctx) const
|
|
|
|
-> decltype(ctx.out()) {
|
|
|
|
return formatter<T, Char>::format(v.load(), ctx);
|
|
|
|
}
|
|
|
|
};
|
2022-06-26 14:28:01 +00:00
|
|
|
|
2023-08-25 15:06:10 +00:00
|
|
|
#ifdef __cpp_lib_atomic_flag_test
|
|
|
|
FMT_EXPORT
|
|
|
|
template <typename Char>
|
2023-10-14 13:52:43 +00:00
|
|
|
struct formatter<std::atomic_flag, Char> : formatter<bool, Char> {
|
2023-08-25 15:06:10 +00:00
|
|
|
template <typename FormatContext>
|
|
|
|
auto format(const std::atomic_flag& v, FormatContext& ctx) const
|
|
|
|
-> decltype(ctx.out()) {
|
|
|
|
return formatter<bool, Char>::format(v.test(), ctx);
|
|
|
|
}
|
|
|
|
};
|
2023-10-14 13:52:43 +00:00
|
|
|
#endif // __cpp_lib_atomic_flag_test
|
2023-08-25 15:06:10 +00:00
|
|
|
|
2024-03-09 07:27:50 +00:00
|
|
|
FMT_EXPORT
|
2024-07-05 00:24:01 +00:00
|
|
|
template <typename T, typename Char> struct formatter<std::complex<T>, Char> {
|
2024-03-09 07:27:50 +00:00
|
|
|
private:
|
2024-07-05 00:24:01 +00:00
|
|
|
detail::dynamic_format_specs<Char> specs_;
|
|
|
|
|
|
|
|
template <typename FormatContext, typename OutputIt>
|
|
|
|
FMT_CONSTEXPR auto do_format(const std::complex<T>& c,
|
|
|
|
detail::dynamic_format_specs<Char>& specs,
|
|
|
|
FormatContext& ctx, OutputIt out) const
|
|
|
|
-> OutputIt {
|
|
|
|
if (c.real() != 0) {
|
|
|
|
*out++ = Char('(');
|
|
|
|
out = detail::write<Char>(out, c.real(), specs, ctx.locale());
|
2024-08-11 17:13:17 +00:00
|
|
|
specs.set_sign(sign::plus);
|
2024-07-05 00:24:01 +00:00
|
|
|
out = detail::write<Char>(out, c.imag(), specs, ctx.locale());
|
|
|
|
if (!detail::isfinite(c.imag())) *out++ = Char(' ');
|
|
|
|
*out++ = Char('i');
|
|
|
|
*out++ = Char(')');
|
|
|
|
return out;
|
2024-03-09 07:27:50 +00:00
|
|
|
}
|
2024-07-05 00:24:01 +00:00
|
|
|
out = detail::write<Char>(out, c.imag(), specs, ctx.locale());
|
|
|
|
if (!detail::isfinite(c.imag())) *out++ = Char(' ');
|
|
|
|
*out++ = Char('i');
|
|
|
|
return out;
|
|
|
|
}
|
2024-03-09 07:27:50 +00:00
|
|
|
|
|
|
|
public:
|
2024-09-01 21:32:55 +00:00
|
|
|
FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) -> const Char* {
|
2024-07-05 00:24:01 +00:00
|
|
|
if (ctx.begin() == ctx.end() || *ctx.begin() == '}') return ctx.begin();
|
|
|
|
return parse_format_specs(ctx.begin(), ctx.end(), specs_, ctx,
|
|
|
|
detail::type_constant<T, Char>::value);
|
|
|
|
}
|
|
|
|
|
2024-03-09 07:27:50 +00:00
|
|
|
template <typename FormatContext>
|
2024-07-05 00:24:01 +00:00
|
|
|
auto format(const std::complex<T>& c, FormatContext& ctx) const
|
2024-03-09 07:27:50 +00:00
|
|
|
-> decltype(ctx.out()) {
|
2024-07-05 00:24:01 +00:00
|
|
|
auto specs = specs_;
|
2024-08-11 17:13:17 +00:00
|
|
|
if (specs.dynamic()) {
|
2024-08-10 15:10:55 +00:00
|
|
|
detail::handle_dynamic_spec(specs.dynamic_width(), specs.width,
|
|
|
|
specs.width_ref, ctx);
|
|
|
|
detail::handle_dynamic_spec(specs.dynamic_precision(), specs.precision,
|
|
|
|
specs.precision_ref, ctx);
|
2024-07-05 00:24:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (specs.width == 0) return do_format(c, specs, ctx, ctx.out());
|
|
|
|
auto buf = basic_memory_buffer<Char>();
|
|
|
|
|
|
|
|
auto outer_specs = format_specs();
|
|
|
|
outer_specs.width = specs.width;
|
2024-08-11 17:13:17 +00:00
|
|
|
auto fill = specs.template fill<Char>();
|
|
|
|
if (fill)
|
|
|
|
outer_specs.set_fill(basic_string_view<Char>(fill, specs.fill_size()));
|
|
|
|
outer_specs.set_align(specs.align());
|
2024-07-05 00:24:01 +00:00
|
|
|
|
|
|
|
specs.width = 0;
|
2024-08-11 17:13:17 +00:00
|
|
|
specs.set_fill({});
|
|
|
|
specs.set_align(align::none);
|
2024-07-05 00:24:01 +00:00
|
|
|
|
|
|
|
do_format(c, specs, ctx, basic_appender<Char>(buf));
|
|
|
|
return detail::write<Char>(ctx.out(),
|
|
|
|
basic_string_view<Char>(buf.data(), buf.size()),
|
|
|
|
outer_specs);
|
2024-03-09 07:27:50 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-08-14 15:34:31 +00:00
|
|
|
FMT_END_NAMESPACE
|
2022-05-17 00:32:25 +00:00
|
|
|
#endif // FMT_STD_H_
|