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_
|
|
|
|
|
2022-09-10 15:04:00 +00:00
|
|
|
#include <cstdlib>
|
2022-09-02 15:33:37 +00:00
|
|
|
#include <exception>
|
2022-09-10 15:04:00 +00:00
|
|
|
#include <memory>
|
2022-05-17 23:31:06 +00:00
|
|
|
#include <thread>
|
2022-06-26 14:28:01 +00:00
|
|
|
#include <type_traits>
|
2022-09-10 15:04:00 +00:00
|
|
|
#include <typeinfo>
|
2022-06-26 14:28:01 +00:00
|
|
|
#include <utility>
|
2022-05-17 23:31:06 +00:00
|
|
|
|
|
|
|
#include "ostream.h"
|
2022-05-17 21:23:56 +00:00
|
|
|
|
|
|
|
#if FMT_HAS_INCLUDE(<version>)
|
|
|
|
# include <version>
|
|
|
|
#endif
|
2022-06-28 17:26:03 +00:00
|
|
|
// Checking FMT_CPLUSPLUS for warning suppression in MSVC.
|
|
|
|
#if FMT_CPLUSPLUS >= 201703L
|
|
|
|
# if FMT_HAS_INCLUDE(<filesystem>)
|
|
|
|
# include <filesystem>
|
|
|
|
# endif
|
|
|
|
# if FMT_HAS_INCLUDE(<variant>)
|
|
|
|
# include <variant>
|
|
|
|
# endif
|
2023-02-25 14:45:56 +00:00
|
|
|
# if FMT_HAS_INCLUDE(<optional>)
|
|
|
|
# include <optional>
|
|
|
|
# endif
|
2022-06-28 17:26:03 +00:00
|
|
|
#endif
|
2022-05-17 00:32:25 +00:00
|
|
|
|
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-05-31 01:38:39 +00:00
|
|
|
// Check if typeid is available.
|
|
|
|
#ifndef FMT_USE_TYPEID
|
2023-06-01 16:32:44 +00:00
|
|
|
// __RTTI is for EDG compilers. In MSVC typeid is available without RTTI.
|
|
|
|
# if defined(__GXX_RTTI) || FMT_HAS_FEATURE(cxx_rtti) || FMT_MSC_VERSION || \
|
|
|
|
defined(__INTEL_RTTI__) || defined(__RTTI)
|
2023-05-31 01:38:39 +00:00
|
|
|
# define FMT_USE_TYPEID 1
|
|
|
|
# else
|
|
|
|
# define FMT_USE_TYPEID 0
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2022-05-17 00:32:25 +00:00
|
|
|
#ifdef __cpp_lib_filesystem
|
2022-05-18 13:04:47 +00:00
|
|
|
FMT_BEGIN_NAMESPACE
|
2022-05-21 15:12:32 +00:00
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
template <typename Char>
|
|
|
|
void write_escaped_path(basic_memory_buffer<Char>& quoted,
|
|
|
|
const std::filesystem::path& p) {
|
|
|
|
write_escaped_string<Char>(std::back_inserter(quoted), p.string<Char>());
|
|
|
|
}
|
2022-05-31 16:38:24 +00:00
|
|
|
# ifdef _WIN32
|
|
|
|
template <>
|
2023-04-30 16:09:35 +00:00
|
|
|
inline void write_escaped_path<char>(memory_buffer& quoted,
|
2022-06-05 13:00:58 +00:00
|
|
|
const std::filesystem::path& p) {
|
2023-04-30 16:09:35 +00:00
|
|
|
auto buf = basic_memory_buffer<wchar_t>();
|
|
|
|
write_escaped_string<wchar_t>(std::back_inserter(buf), p.native());
|
2023-05-02 16:29:02 +00:00
|
|
|
// Convert UTF-16 to UTF-8.
|
2023-05-10 17:58:09 +00:00
|
|
|
if (!to_utf8<wchar_t>::convert(quoted, {buf.data(), buf.size()}))
|
2023-05-02 16:29:02 +00:00
|
|
|
FMT_THROW(std::runtime_error("invalid utf16"));
|
2022-05-31 16:38:24 +00:00
|
|
|
}
|
|
|
|
# endif
|
2022-05-21 15:12:32 +00:00
|
|
|
template <>
|
2022-06-05 13:00:58 +00:00
|
|
|
inline void write_escaped_path<std::filesystem::path::value_type>(
|
2022-05-21 15:12:32 +00:00
|
|
|
basic_memory_buffer<std::filesystem::path::value_type>& quoted,
|
|
|
|
const std::filesystem::path& p) {
|
|
|
|
write_escaped_string<std::filesystem::path::value_type>(
|
|
|
|
std::back_inserter(quoted), p.native());
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
|
2023-05-17 14:37:53 +00:00
|
|
|
FMT_EXPORT
|
2022-05-18 13:07:24 +00:00
|
|
|
template <typename Char>
|
2022-05-21 15:12:32 +00:00
|
|
|
struct formatter<std::filesystem::path, Char>
|
2023-06-02 21:12:57 +00:00
|
|
|
: formatter<basic_string_view<Char>, Char> {
|
2023-03-05 16:01:06 +00:00
|
|
|
template <typename ParseContext> FMT_CONSTEXPR auto parse(ParseContext& ctx) {
|
2023-06-02 21:12:57 +00:00
|
|
|
auto out = formatter<basic_string_view<Char>, Char>::parse(ctx);
|
2023-03-05 16:01:06 +00:00
|
|
|
this->set_debug_format(false);
|
|
|
|
return out;
|
|
|
|
}
|
2022-05-21 15:12:32 +00:00
|
|
|
template <typename FormatContext>
|
|
|
|
auto format(const std::filesystem::path& p, FormatContext& ctx) const ->
|
|
|
|
typename FormatContext::iterator {
|
2023-04-30 16:09:35 +00:00
|
|
|
auto quoted = basic_memory_buffer<Char>();
|
2022-05-21 15:12:32 +00:00
|
|
|
detail::write_escaped_path(quoted, p);
|
2023-06-02 21:12:57 +00:00
|
|
|
return formatter<basic_string_view<Char>, Char>::format(
|
2022-05-21 15:12:32 +00:00
|
|
|
basic_string_view<Char>(quoted.data(), quoted.size()), ctx);
|
|
|
|
}
|
2022-05-18 13:07:24 +00:00
|
|
|
};
|
2022-05-18 13:04:47 +00:00
|
|
|
FMT_END_NAMESPACE
|
2022-05-17 00:32:25 +00:00
|
|
|
#endif
|
|
|
|
|
2022-05-17 23:31:06 +00:00
|
|
|
FMT_BEGIN_NAMESPACE
|
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:
|
|
|
|
template <typename ParseContext> FMT_CONSTEXPR auto parse(ParseContext& ctx) {
|
|
|
|
maybe_set_debug_format(underlying_, true);
|
|
|
|
return underlying_.parse(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename FormatContext>
|
|
|
|
auto format(std::optional<T> const& opt, FormatContext& ctx) const
|
|
|
|
-> 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
|
|
|
|
|
2022-06-26 14:28:01 +00:00
|
|
|
#ifdef __cpp_lib_variant
|
|
|
|
FMT_BEGIN_NAMESPACE
|
2023-05-17 14:37:53 +00:00
|
|
|
FMT_EXPORT
|
2022-06-26 14:28:01 +00:00
|
|
|
template <typename Char> struct formatter<std::monostate, Char> {
|
|
|
|
template <typename ParseContext>
|
|
|
|
FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
|
|
|
|
return ctx.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename FormatContext>
|
|
|
|
auto format(const std::monostate&, FormatContext& ctx) const
|
|
|
|
-> decltype(ctx.out()) {
|
|
|
|
auto out = ctx.out();
|
|
|
|
out = detail::write<Char>(out, "monostate");
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Char, typename OutputIt, typename T>
|
|
|
|
auto write_variant_alternative(OutputIt out, const T& v) -> OutputIt {
|
|
|
|
if constexpr (is_string<T>::value)
|
|
|
|
return write_escaped_string<Char>(out, detail::to_string_view(v));
|
|
|
|
else if constexpr (std::is_same_v<T, Char>)
|
|
|
|
return write_escaped_char(out, v);
|
|
|
|
else
|
|
|
|
return write<Char>(out, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
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-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>>>> {
|
|
|
|
template <typename ParseContext>
|
|
|
|
FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
|
|
|
|
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-03-18 14:07:06 +00:00
|
|
|
try {
|
|
|
|
std::visit(
|
|
|
|
[&](const auto& v) {
|
|
|
|
out = detail::write_variant_alternative<Char>(out, v);
|
|
|
|
},
|
|
|
|
value);
|
|
|
|
} catch (const std::bad_variant_access&) {
|
|
|
|
detail::write<Char>(out, "valueless by exception");
|
|
|
|
}
|
2022-06-26 14:28:01 +00:00
|
|
|
*out++ = ')';
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
FMT_END_NAMESPACE
|
2022-09-02 15:33:37 +00:00
|
|
|
#endif // __cpp_lib_variant
|
|
|
|
|
|
|
|
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> {
|
|
|
|
template <typename ParseContext>
|
|
|
|
FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
|
|
|
|
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();
|
2022-12-24 22:34:50 +00:00
|
|
|
out = detail::write_bytes(out, ec.category().name(), format_specs<Char>());
|
2022-10-02 00:46:09 +00:00
|
|
|
out = detail::write<Char>(out, Char(':'));
|
|
|
|
out = detail::write<Char>(out, ec.value());
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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<
|
|
|
|
T, Char,
|
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:
|
|
|
|
FMT_CONSTEXPR auto parse(basic_format_parse_context<Char>& ctx)
|
|
|
|
-> decltype(ctx.begin()) {
|
|
|
|
auto it = ctx.begin();
|
|
|
|
auto end = ctx.end();
|
|
|
|
if (it == end || *it == '}') return it;
|
|
|
|
if (*it == 't') {
|
|
|
|
++it;
|
2023-05-31 01:38:39 +00:00
|
|
|
with_typename_ = FMT_USE_TYPEID != 0;
|
2022-09-10 15:04:00 +00:00
|
|
|
}
|
|
|
|
return it;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename OutputIt>
|
|
|
|
auto format(const std::exception& ex,
|
|
|
|
basic_format_context<OutputIt, Char>& ctx) const -> OutputIt {
|
2022-12-24 22:34:50 +00:00
|
|
|
format_specs<Char> spec;
|
2022-09-10 15:04:00 +00:00
|
|
|
auto out = ctx.out();
|
|
|
|
if (!with_typename_)
|
|
|
|
return detail::write_bytes(out, string_view(ex.what()), spec);
|
|
|
|
|
2023-05-31 01:38:39 +00:00
|
|
|
#if FMT_USE_TYPEID
|
2022-09-10 15:04:00 +00:00
|
|
|
const std::type_info& ti = typeid(ex);
|
2023-05-31 01:38:39 +00:00
|
|
|
# ifdef FMT_HAS_ABI_CXA_DEMANGLE
|
2022-09-10 15:04:00 +00:00
|
|
|
int status = 0;
|
|
|
|
std::size_t size = 0;
|
|
|
|
std::unique_ptr<char, decltype(&std::free)> demangled_name_ptr(
|
|
|
|
abi::__cxa_demangle(ti.name(), nullptr, &size, &status), &std::free);
|
2022-09-29 16:52:38 +00:00
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
out = detail::write_bytes(out, demangled_name_view, spec);
|
2023-05-31 01:38:39 +00:00
|
|
|
# elif FMT_MSC_VERSION
|
2022-09-10 15:04:00 +00:00
|
|
|
string_view demangled_name_view(ti.name());
|
|
|
|
if (demangled_name_view.starts_with("class "))
|
|
|
|
demangled_name_view.remove_prefix(6);
|
|
|
|
else if (demangled_name_view.starts_with("struct "))
|
|
|
|
demangled_name_view.remove_prefix(7);
|
|
|
|
out = detail::write_bytes(out, demangled_name_view, spec);
|
2023-05-31 01:38:39 +00:00
|
|
|
# else
|
2022-09-10 15:04:00 +00:00
|
|
|
out = detail::write_bytes(out, string_view(ti.name()), spec);
|
2023-05-31 01:38:39 +00:00
|
|
|
# endif
|
2022-09-10 15:04:00 +00:00
|
|
|
out = detail::write<Char>(out, Char(':'));
|
|
|
|
out = detail::write<Char>(out, Char(' '));
|
|
|
|
out = detail::write_bytes(out, string_view(ex.what()), spec);
|
|
|
|
|
|
|
|
return out;
|
2023-05-31 01:38:39 +00:00
|
|
|
#endif
|
2022-09-02 15:33:37 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
FMT_END_NAMESPACE
|
2022-06-26 14:28:01 +00:00
|
|
|
|
2022-05-17 00:32:25 +00:00
|
|
|
#endif // FMT_STD_H_
|