| 1 |
// Formatting library for C++ - formatters for standard library types |
| 2 |
// |
| 3 |
// Copyright (c) 2012 - present, Victor Zverovich and {fmt} contributors |
| 4 |
// All rights reserved. |
| 5 |
// |
| 6 |
// For the license information refer to format.h. |
| 7 |
|
| 8 |
#ifndef FMT_STD_H_ |
| 9 |
#define FMT_STD_H_ |
| 10 |
|
| 11 |
#include "format.h" |
| 12 |
#include "ostream.h" |
| 13 |
|
| 14 |
#ifndef FMT_MODULE |
| 15 |
# include <atomic> |
| 16 |
# include <bitset> |
| 17 |
# include <complex> |
| 18 |
# include <cstddef> // std::byte |
| 19 |
# include <exception> // std::exception |
| 20 |
# include <functional> // std::reference_wrapper |
| 21 |
# include <memory> |
| 22 |
# include <thread> |
| 23 |
# include <type_traits> |
| 24 |
# include <typeinfo> // std::type_info |
| 25 |
# include <utility> // std::make_index_sequence |
| 26 |
|
| 27 |
// Check FMT_CPLUSPLUS to suppress a bogus warning in MSVC. |
| 28 |
# if FMT_CPLUSPLUS >= 201703L |
| 29 |
# if FMT_HAS_INCLUDE(<filesystem>) && \ |
| 30 |
(!defined(FMT_CPP_LIB_FILESYSTEM) || FMT_CPP_LIB_FILESYSTEM != 0) |
| 31 |
# include <filesystem> |
| 32 |
# endif |
| 33 |
# if FMT_HAS_INCLUDE(<variant>) |
| 34 |
# include <variant> |
| 35 |
# endif |
| 36 |
# if FMT_HAS_INCLUDE(<optional>) |
| 37 |
# include <optional> |
| 38 |
# endif |
| 39 |
# endif |
| 40 |
// Use > instead of >= in the version check because <source_location> may be |
| 41 |
// available after C++17 but before C++20 is marked as implemented. |
| 42 |
# if FMT_CPLUSPLUS > 201703L && FMT_HAS_INCLUDE(<source_location>) |
| 43 |
# include <source_location> |
| 44 |
# endif |
| 45 |
# if FMT_CPLUSPLUS > 202002L && FMT_HAS_INCLUDE(<expected>) |
| 46 |
# include <expected> |
| 47 |
# endif |
| 48 |
#endif // FMT_MODULE |
| 49 |
|
| 50 |
#if FMT_HAS_INCLUDE(<version>) |
| 51 |
# include <version> |
| 52 |
#endif |
| 53 |
|
| 54 |
// GCC 4 does not support FMT_HAS_INCLUDE. |
| 55 |
#if FMT_HAS_INCLUDE(<cxxabi.h>) || defined(__GLIBCXX__) |
| 56 |
# include <cxxabi.h> |
| 57 |
// Android NDK with gabi++ library on some architectures does not implement |
| 58 |
// abi::__cxa_demangle(). |
| 59 |
# ifndef __GABIXX_CXXABI_H__ |
| 60 |
# define FMT_HAS_ABI_CXA_DEMANGLE |
| 61 |
# endif |
| 62 |
#endif |
| 63 |
|
| 64 |
#ifdef FMT_CPP_LIB_FILESYSTEM |
| 65 |
// Use the provided definition. |
| 66 |
#elif defined(__cpp_lib_filesystem) |
| 67 |
# define FMT_CPP_LIB_FILESYSTEM __cpp_lib_filesystem |
| 68 |
#else |
| 69 |
# define FMT_CPP_LIB_FILESYSTEM 0 |
| 70 |
#endif |
| 71 |
|
| 72 |
#ifdef FMT_CPP_LIB_VARIANT |
| 73 |
// Use the provided definition. |
| 74 |
#elif defined(__cpp_lib_variant) |
| 75 |
# define FMT_CPP_LIB_VARIANT __cpp_lib_variant |
| 76 |
#else |
| 77 |
# define FMT_CPP_LIB_VARIANT 0 |
| 78 |
#endif |
| 79 |
|
| 80 |
FMT_BEGIN_NAMESPACE |
| 81 |
namespace detail { |
| 82 |
|
| 83 |
#ifdef FMT_USE_BITINT |
| 84 |
// Use the provided definition. |
| 85 |
#elif FMT_CLANG_VERSION >= 1500 && !defined(__CUDACC__) |
| 86 |
# define FMT_USE_BITINT 1 |
| 87 |
#else |
| 88 |
# define FMT_USE_BITINT 0 |
| 89 |
#endif |
| 90 |
|
| 91 |
#if FMT_USE_BITINT |
| 92 |
FMT_PRAGMA_CLANG(diagnostic ignored "-Wbit-int-extension") |
| 93 |
template <int N> using bitint = _BitInt(N); |
| 94 |
template <int N> using ubitint = unsigned _BitInt(N); |
| 95 |
#else |
| 96 |
template <int N> struct bitint {}; |
| 97 |
template <int N> struct ubitint {}; |
| 98 |
#endif // FMT_USE_BITINT |
| 99 |
|
| 100 |
#if FMT_CPP_LIB_FILESYSTEM |
| 101 |
|
| 102 |
template <typename Char, typename PathChar> |
| 103 |
auto get_path_string(const std::filesystem::path& p, |
| 104 |
const std::basic_string<PathChar>& native) { |
| 105 |
if constexpr (std::is_same_v<Char, char> && |
| 106 |
std::is_same_v<PathChar, wchar_t>) { |
| 107 |
return to_utf8<wchar_t>(native, to_utf8_error_policy::wtf); |
| 108 |
} else { |
| 109 |
return p.string<Char>(); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
template <typename Char, typename PathChar> |
| 114 |
void write_escaped_path(basic_memory_buffer<Char>& quoted, |
| 115 |
const std::filesystem::path& p, |
| 116 |
const std::basic_string<PathChar>& native) { |
| 117 |
if constexpr (std::is_same_v<Char, char> && |
| 118 |
std::is_same_v<PathChar, wchar_t>) { |
| 119 |
auto buf = basic_memory_buffer<wchar_t>(); |
| 120 |
write_escaped_string<wchar_t>(std::back_inserter(buf), native); |
| 121 |
bool valid = to_utf8<wchar_t>::convert(quoted, {buf.data(), buf.size()}); |
| 122 |
FMT_ASSERT(valid, "invalid utf16"); |
| 123 |
} else if constexpr (std::is_same_v<Char, PathChar>) { |
| 124 |
write_escaped_string<std::filesystem::path::value_type>( |
| 125 |
std::back_inserter(quoted), native); |
| 126 |
} else { |
| 127 |
write_escaped_string<Char>(std::back_inserter(quoted), p.string<Char>()); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
#endif // FMT_CPP_LIB_FILESYSTEM |
| 132 |
|
| 133 |
#if defined(__cpp_lib_expected) || FMT_CPP_LIB_VARIANT |
| 134 |
|
| 135 |
template <typename Char, typename OutputIt, typename T, typename FormatContext> |
| 136 |
FMT_CONSTEXPR auto write_escaped_alternative(OutputIt out, const T& v, |
| 137 |
FormatContext& ctx) -> OutputIt { |
| 138 |
if constexpr (has_to_string_view<T>::value) |
| 139 |
return write_escaped_string<Char>(out, detail::to_string_view(v)); |
| 140 |
if constexpr (std::is_same_v<T, Char>) return write_escaped_char(out, v); |
| 141 |
|
| 142 |
formatter<std::remove_cv_t<T>, Char> underlying; |
| 143 |
maybe_set_debug_format(underlying, true); |
| 144 |
return underlying.format(v, ctx); |
| 145 |
} |
| 146 |
#endif |
| 147 |
|
| 148 |
#if FMT_CPP_LIB_VARIANT |
| 149 |
|
| 150 |
template <typename> struct is_variant_like_ : std::false_type {}; |
| 151 |
template <typename... Types> |
| 152 |
struct is_variant_like_<std::variant<Types...>> : std::true_type {}; |
| 153 |
|
| 154 |
template <typename Variant, typename Char> class is_variant_formattable { |
| 155 |
template <size_t... Is> |
| 156 |
static auto check(std::index_sequence<Is...>) -> std::conjunction< |
| 157 |
is_formattable<std::variant_alternative_t<Is, Variant>, Char>...>; |
| 158 |
|
| 159 |
public: |
| 160 |
static constexpr bool value = decltype(check( |
| 161 |
std::make_index_sequence<std::variant_size<Variant>::value>()))::value; |
| 162 |
}; |
| 163 |
|
| 164 |
#endif // FMT_CPP_LIB_VARIANT |
| 165 |
|
| 166 |
#if FMT_USE_RTTI |
| 167 |
inline auto normalize_libcxx_inline_namespaces(string_view demangled_name_view, |
| 168 |
char* begin) -> string_view { |
| 169 |
// Normalization of stdlib inline namespace names. |
| 170 |
// libc++ inline namespaces. |
| 171 |
// std::__1::* -> std::* |
| 172 |
// std::__1::__fs::* -> std::* |
| 173 |
// libstdc++ inline namespaces. |
| 174 |
// std::__cxx11::* -> std::* |
| 175 |
// std::filesystem::__cxx11::* -> std::filesystem::* |
| 176 |
if (demangled_name_view.starts_with("std::")) { |
| 177 |
char* to = begin + 5; // std:: |
| 178 |
for (const char *from = to, *end = begin + demangled_name_view.size(); |
| 179 |
from < end;) { |
| 180 |
// This is safe, because demangled_name is NUL-terminated. |
| 181 |
if (from[0] == '_' && from[1] == '_') { |
| 182 |
const char* next = from + 1; |
| 183 |
while (next < end && *next != ':') next++; |
| 184 |
if (next[0] == ':' && next[1] == ':') { |
| 185 |
from = next + 2; |
| 186 |
continue; |
| 187 |
} |
| 188 |
} |
| 189 |
*to++ = *from++; |
| 190 |
} |
| 191 |
demangled_name_view = {begin, detail::to_unsigned(to - begin)}; |
| 192 |
} |
| 193 |
return demangled_name_view; |
| 194 |
} |
| 195 |
|
| 196 |
template <class OutputIt> |
| 197 |
auto normalize_msvc_abi_name(string_view abi_name_view, OutputIt out) |
| 198 |
-> OutputIt { |
| 199 |
const string_view demangled_name(abi_name_view); |
| 200 |
for (size_t i = 0; i < demangled_name.size(); ++i) { |
| 201 |
auto sub = demangled_name; |
| 202 |
sub.remove_prefix(i); |
| 203 |
if (sub.starts_with("enum ")) { |
| 204 |
i += 4; |
| 205 |
continue; |
| 206 |
} |
| 207 |
if (sub.starts_with("class ") || sub.starts_with("union ")) { |
| 208 |
i += 5; |
| 209 |
continue; |
| 210 |
} |
| 211 |
if (sub.starts_with("struct ")) { |
| 212 |
i += 6; |
| 213 |
continue; |
| 214 |
} |
| 215 |
if (*sub.begin() != ' ') *out++ = *sub.begin(); |
| 216 |
} |
| 217 |
return out; |
| 218 |
} |
| 219 |
|
| 220 |
template <typename OutputIt> |
| 221 |
auto write_demangled_name(OutputIt out, const std::type_info& ti) -> OutputIt { |
| 222 |
# ifdef FMT_HAS_ABI_CXA_DEMANGLE |
| 223 |
int status = 0; |
| 224 |
size_t size = 0; |
| 225 |
std::unique_ptr<char, void (*)(void*)> demangled_name_ptr( |
| 226 |
abi::__cxa_demangle(ti.name(), nullptr, &size, &status), &free); |
| 227 |
|
| 228 |
string_view demangled_name_view; |
| 229 |
if (demangled_name_ptr) { |
| 230 |
demangled_name_view = normalize_libcxx_inline_namespaces( |
| 231 |
demangled_name_ptr.get(), demangled_name_ptr.get()); |
| 232 |
} else { |
| 233 |
demangled_name_view = string_view(ti.name()); |
| 234 |
} |
| 235 |
return detail::write_bytes<char>(out, demangled_name_view); |
| 236 |
# elif FMT_MSC_VERSION && defined(_MSVC_STL_UPDATE) |
| 237 |
return normalize_msvc_abi_name(ti.name(), out); |
| 238 |
# elif FMT_MSC_VERSION && defined(_LIBCPP_VERSION) |
| 239 |
const string_view demangled_name = ti.name(); |
| 240 |
std::string name_copy(demangled_name.size(), '\0'); |
| 241 |
// normalize_msvc_abi_name removes class, struct, union etc that MSVC has in |
| 242 |
// front of types |
| 243 |
name_copy.erase(normalize_msvc_abi_name(demangled_name, name_copy.begin()), |
| 244 |
name_copy.end()); |
| 245 |
// normalize_libcxx_inline_namespaces removes the inline __1, __2, etc |
| 246 |
// namespaces libc++ uses for ABI versioning On MSVC ABI + libc++ |
| 247 |
// environments, we need to eliminate both of them. |
| 248 |
const string_view normalized_name = |
| 249 |
normalize_libcxx_inline_namespaces(name_copy, name_copy.data()); |
| 250 |
return detail::write_bytes<char>(out, normalized_name); |
| 251 |
# else |
| 252 |
return detail::write_bytes<char>(out, string_view(ti.name())); |
| 253 |
# endif |
| 254 |
} |
| 255 |
|
| 256 |
#endif // FMT_USE_RTTI |
| 257 |
|
| 258 |
template <typename T, typename Enable = void> |
| 259 |
struct has_flip : std::false_type {}; |
| 260 |
|
| 261 |
template <typename T> |
| 262 |
struct has_flip<T, void_t<decltype(std::declval<T>().flip())>> |
| 263 |
: std::true_type {}; |
| 264 |
|
| 265 |
template <typename T> struct is_bit_reference_like { |
| 266 |
static constexpr bool value = std::is_convertible<T, bool>::value && |
| 267 |
std::is_nothrow_assignable<T, bool>::value && |
| 268 |
has_flip<T>::value; |
| 269 |
}; |
| 270 |
|
| 271 |
// Workaround for libc++ incompatibility with C++ standard. |
| 272 |
// According to the Standard, `bitset::operator[] const` returns bool. |
| 273 |
#if defined(_LIBCPP_VERSION) && !defined(FMT_IMPORT_STD) |
| 274 |
template <typename C> |
| 275 |
struct is_bit_reference_like<std::__bit_const_reference<C>> { |
| 276 |
static constexpr bool value = true; |
| 277 |
}; |
| 278 |
#endif |
| 279 |
|
| 280 |
template <typename T, typename Enable = void> |
| 281 |
struct has_format_as : std::false_type {}; |
| 282 |
template <typename T> |
| 283 |
struct has_format_as<T, void_t<decltype(format_as(std::declval<const T&>()))>> |
| 284 |
: std::true_type {}; |
| 285 |
|
| 286 |
template <typename T, typename Enable = void> |
| 287 |
struct has_format_as_member : std::false_type {}; |
| 288 |
template <typename T> |
| 289 |
struct has_format_as_member< |
| 290 |
T, void_t<decltype(formatter<T>::format_as(std::declval<const T&>()))>> |
| 291 |
: std::true_type {}; |
| 292 |
|
| 293 |
} // namespace detail |
| 294 |
|
| 295 |
template <typename T, typename Deleter> |
| 296 |
auto ptr(const std::unique_ptr<T, Deleter>& p) -> const void* { |
| 297 |
return p.get(); |
| 298 |
} |
| 299 |
template <typename T> auto ptr(const std::shared_ptr<T>& p) -> const void* { |
| 300 |
return p.get(); |
| 301 |
} |
| 302 |
|
| 303 |
#if FMT_CPP_LIB_FILESYSTEM |
| 304 |
|
| 305 |
template <typename Char> struct formatter<std::filesystem::path, Char> { |
| 306 |
private: |
| 307 |
format_specs specs_; |
| 308 |
detail::arg_ref<Char> width_ref_; |
| 309 |
bool debug_ = false; |
| 310 |
char path_type_ = 0; |
| 311 |
|
| 312 |
public: |
| 313 |
FMT_CONSTEXPR void set_debug_format(bool set = true) { debug_ = set; } |
| 314 |
|
| 315 |
FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) { |
| 316 |
auto it = ctx.begin(), end = ctx.end(); |
| 317 |
if (it == end) return it; |
| 318 |
|
| 319 |
it = detail::parse_align(it, end, specs_); |
| 320 |
if (it == end) return it; |
| 321 |
|
| 322 |
Char c = *it; |
| 323 |
if ((c >= '0' && c <= '9') || c == '{') |
| 324 |
it = detail::parse_width(it, end, specs_, width_ref_, ctx); |
| 325 |
if (it != end && *it == '?') { |
| 326 |
debug_ = true; |
| 327 |
++it; |
| 328 |
} |
| 329 |
if (it != end && (*it == 'g')) path_type_ = detail::to_ascii(*it++); |
| 330 |
return it; |
| 331 |
} |
| 332 |
|
| 333 |
template <typename FormatContext> |
| 334 |
auto format(const std::filesystem::path& p, FormatContext& ctx) const { |
| 335 |
auto specs = specs_; |
| 336 |
auto path_string = |
| 337 |
!path_type_ ? p.native() |
| 338 |
: p.generic_string<std::filesystem::path::value_type>(); |
| 339 |
|
| 340 |
detail::handle_dynamic_spec(specs.dynamic_width(), specs.width, width_ref_, |
| 341 |
ctx); |
| 342 |
if (!debug_) { |
| 343 |
auto s = detail::get_path_string<Char>(p, path_string); |
| 344 |
return detail::write(ctx.out(), basic_string_view<Char>(s), specs); |
| 345 |
} |
| 346 |
auto quoted = basic_memory_buffer<Char>(); |
| 347 |
detail::write_escaped_path(quoted, p, path_string); |
| 348 |
return detail::write(ctx.out(), |
| 349 |
basic_string_view<Char>(quoted.data(), quoted.size()), |
| 350 |
specs); |
| 351 |
} |
| 352 |
}; |
| 353 |
|
| 354 |
class path : public std::filesystem::path { |
| 355 |
public: |
| 356 |
auto display_string() const -> std::string { |
| 357 |
const std::filesystem::path& base = *this; |
| 358 |
return fmt::format(FMT_STRING("{}"), base); |
| 359 |
} |
| 360 |
auto system_string() const -> std::string { return string(); } |
| 361 |
|
| 362 |
auto generic_display_string() const -> std::string { |
| 363 |
const std::filesystem::path& base = *this; |
| 364 |
return fmt::format(FMT_STRING("{:g}"), base); |
| 365 |
} |
| 366 |
auto generic_system_string() const -> std::string { return generic_string(); } |
| 367 |
}; |
| 368 |
|
| 369 |
#endif // FMT_CPP_LIB_FILESYSTEM |
| 370 |
|
| 371 |
template <size_t N, typename Char> |
| 372 |
struct formatter<std::bitset<N>, Char> |
| 373 |
: nested_formatter<basic_string_view<Char>, Char> { |
| 374 |
private: |
| 375 |
// This is a functor because C++11 doesn't support generic lambdas. |
| 376 |
struct writer { |
| 377 |
const std::bitset<N>& bs; |
| 378 |
|
| 379 |
template <typename OutputIt> |
| 380 |
FMT_CONSTEXPR auto operator()(OutputIt out) -> OutputIt { |
| 381 |
for (auto pos = N; pos > 0; --pos) |
| 382 |
out = detail::write<Char>(out, bs[pos - 1] ? Char('1') : Char('0')); |
| 383 |
return out; |
| 384 |
} |
| 385 |
}; |
| 386 |
|
| 387 |
public: |
| 388 |
template <typename FormatContext> |
| 389 |
auto format(const std::bitset<N>& bs, FormatContext& ctx) const |
| 390 |
-> decltype(ctx.out()) { |
| 391 |
return this->write_padded(ctx, writer{bs}); |
| 392 |
} |
| 393 |
}; |
| 394 |
|
| 395 |
template <typename Char> |
| 396 |
struct formatter<std::thread::id, Char> : basic_ostream_formatter<Char> {}; |
| 397 |
|
| 398 |
#ifdef __cpp_lib_optional |
| 399 |
template <typename T, typename Char> |
| 400 |
struct formatter<std::optional<T>, Char, |
| 401 |
std::enable_if_t<is_formattable<T, Char>::value>> { |
| 402 |
private: |
| 403 |
formatter<std::remove_cv_t<T>, Char> underlying_; |
| 404 |
static constexpr basic_string_view<Char> optional = |
| 405 |
detail::string_literal<Char, 'o', 'p', 't', 'i', 'o', 'n', 'a', 'l', |
| 406 |
'('>{}; |
| 407 |
static constexpr basic_string_view<Char> none = |
| 408 |
detail::string_literal<Char, 'n', 'o', 'n', 'e'>{}; |
| 409 |
|
| 410 |
public: |
| 411 |
FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) { |
| 412 |
detail::maybe_set_debug_format(underlying_, true); |
| 413 |
return underlying_.parse(ctx); |
| 414 |
} |
| 415 |
|
| 416 |
template <typename FormatContext> |
| 417 |
auto format(const std::optional<T>& opt, FormatContext& ctx) const |
| 418 |
-> decltype(ctx.out()) { |
| 419 |
if (!opt) return detail::write<Char>(ctx.out(), none); |
| 420 |
|
| 421 |
auto out = ctx.out(); |
| 422 |
out = detail::write<Char>(out, optional); |
| 423 |
ctx.advance_to(out); |
| 424 |
out = underlying_.format(*opt, ctx); |
| 425 |
return detail::write(out, ')'); |
| 426 |
} |
| 427 |
}; |
| 428 |
#endif // __cpp_lib_optional |
| 429 |
|
| 430 |
#ifdef __cpp_lib_expected |
| 431 |
template <typename T, typename E, typename Char> |
| 432 |
struct formatter<std::expected<T, E>, Char, |
| 433 |
std::enable_if_t<(std::is_void<T>::value || |
| 434 |
is_formattable<T, Char>::value) && |
| 435 |
is_formattable<E, Char>::value>> { |
| 436 |
FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) -> const Char* { |
| 437 |
return ctx.begin(); |
| 438 |
} |
| 439 |
|
| 440 |
template <typename FormatContext> |
| 441 |
auto format(const std::expected<T, E>& value, FormatContext& ctx) const |
| 442 |
-> decltype(ctx.out()) { |
| 443 |
auto out = ctx.out(); |
| 444 |
|
| 445 |
if (value.has_value()) { |
| 446 |
out = detail::write<Char>(out, "expected("); |
| 447 |
if constexpr (!std::is_void<T>::value) |
| 448 |
out = detail::write_escaped_alternative<Char>(out, *value, ctx); |
| 449 |
} else { |
| 450 |
out = detail::write<Char>(out, "unexpected("); |
| 451 |
out = detail::write_escaped_alternative<Char>(out, value.error(), ctx); |
| 452 |
} |
| 453 |
*out++ = ')'; |
| 454 |
return out; |
| 455 |
} |
| 456 |
}; |
| 457 |
|
| 458 |
template <typename E, typename Char> |
| 459 |
struct formatter<std::unexpected<E>, Char, |
| 460 |
std::enable_if_t<is_formattable<E, Char>::value>> { |
| 461 |
FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) -> const Char* { |
| 462 |
return ctx.begin(); |
| 463 |
} |
| 464 |
|
| 465 |
template <typename FormatContext> |
| 466 |
auto format(const std::unexpected<E>& value, FormatContext& ctx) const |
| 467 |
-> decltype(ctx.out()) { |
| 468 |
auto out = ctx.out(); |
| 469 |
|
| 470 |
out = detail::write<Char>(out, "unexpected("); |
| 471 |
out = detail::write_escaped_alternative<Char>(out, value.error(), ctx); |
| 472 |
|
| 473 |
*out++ = ')'; |
| 474 |
return out; |
| 475 |
} |
| 476 |
}; |
| 477 |
#endif // __cpp_lib_expected |
| 478 |
|
| 479 |
#ifdef __cpp_lib_source_location |
| 480 |
template <> struct formatter<std::source_location> { |
| 481 |
FMT_CONSTEXPR auto parse(parse_context<>& ctx) { return ctx.begin(); } |
| 482 |
|
| 483 |
template <typename FormatContext> |
| 484 |
auto format(const std::source_location& loc, FormatContext& ctx) const |
| 485 |
-> decltype(ctx.out()) { |
| 486 |
auto out = ctx.out(); |
| 487 |
out = detail::write(out, loc.file_name()); |
| 488 |
out = detail::write(out, ':'); |
| 489 |
out = detail::write<char>(out, loc.line()); |
| 490 |
out = detail::write(out, ':'); |
| 491 |
out = detail::write<char>(out, loc.column()); |
| 492 |
out = detail::write(out, ": "); |
| 493 |
out = detail::write(out, loc.function_name()); |
| 494 |
return out; |
| 495 |
} |
| 496 |
}; |
| 497 |
#endif |
| 498 |
|
| 499 |
#if FMT_CPP_LIB_VARIANT |
| 500 |
|
| 501 |
template <typename T> struct is_variant_like { |
| 502 |
static constexpr bool value = detail::is_variant_like_<T>::value; |
| 503 |
}; |
| 504 |
|
| 505 |
template <typename Char> struct formatter<std::monostate, Char> { |
| 506 |
FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) -> const Char* { |
| 507 |
return ctx.begin(); |
| 508 |
} |
| 509 |
|
| 510 |
template <typename FormatContext> |
| 511 |
FMT_CONSTEXPR auto format(const std::monostate&, FormatContext& ctx) const |
| 512 |
-> decltype(ctx.out()) { |
| 513 |
return detail::write<Char>(ctx.out(), "monostate"); |
| 514 |
} |
| 515 |
}; |
| 516 |
|
| 517 |
template <typename Variant, typename Char> |
| 518 |
struct formatter<Variant, Char, |
| 519 |
std::enable_if_t<std::conjunction_v< |
| 520 |
is_variant_like<Variant>, |
| 521 |
detail::is_variant_formattable<Variant, Char>>>> { |
| 522 |
FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) -> const Char* { |
| 523 |
return ctx.begin(); |
| 524 |
} |
| 525 |
|
| 526 |
template <typename FormatContext> |
| 527 |
FMT_CONSTEXPR20 auto format(const Variant& value, FormatContext& ctx) const |
| 528 |
-> decltype(ctx.out()) { |
| 529 |
auto out = ctx.out(); |
| 530 |
|
| 531 |
out = detail::write<Char>(out, "variant("); |
| 532 |
FMT_TRY { |
| 533 |
std::visit( |
| 534 |
[&](const auto& v) { |
| 535 |
out = detail::write_escaped_alternative<Char>(out, v, ctx); |
| 536 |
}, |
| 537 |
value); |
| 538 |
} |
| 539 |
FMT_CATCH(const std::bad_variant_access&) { |
| 540 |
detail::write<Char>(out, "valueless by exception"); |
| 541 |
} |
| 542 |
*out++ = ')'; |
| 543 |
return out; |
| 544 |
} |
| 545 |
}; |
| 546 |
|
| 547 |
#endif // FMT_CPP_LIB_VARIANT |
| 548 |
|
| 549 |
template <> struct formatter<std::error_code> { |
| 550 |
private: |
| 551 |
format_specs specs_; |
| 552 |
detail::arg_ref<char> width_ref_; |
| 553 |
bool debug_ = false; |
| 554 |
|
| 555 |
public: |
| 556 |
FMT_CONSTEXPR void set_debug_format(bool set = true) { debug_ = set; } |
| 557 |
|
| 558 |
FMT_CONSTEXPR auto parse(parse_context<>& ctx) -> const char* { |
| 559 |
auto it = ctx.begin(), end = ctx.end(); |
| 560 |
if (it == end) return it; |
| 561 |
|
| 562 |
it = detail::parse_align(it, end, specs_); |
| 563 |
|
| 564 |
char c = *it; |
| 565 |
if (it != end && ((c >= '0' && c <= '9') || c == '{')) |
| 566 |
it = detail::parse_width(it, end, specs_, width_ref_, ctx); |
| 567 |
|
| 568 |
if (it != end && *it == '?') { |
| 569 |
debug_ = true; |
| 570 |
++it; |
| 571 |
} |
| 572 |
if (it != end && *it == 's') { |
| 573 |
specs_.set_type(presentation_type::string); |
| 574 |
++it; |
| 575 |
} |
| 576 |
return it; |
| 577 |
} |
| 578 |
|
| 579 |
template <typename FormatContext> |
| 580 |
FMT_CONSTEXPR20 auto format(const std::error_code& ec, |
| 581 |
FormatContext& ctx) const -> decltype(ctx.out()) { |
| 582 |
auto specs = specs_; |
| 583 |
detail::handle_dynamic_spec(specs.dynamic_width(), specs.width, width_ref_, |
| 584 |
ctx); |
| 585 |
auto buf = memory_buffer(); |
| 586 |
if (specs_.type() == presentation_type::string) { |
| 587 |
buf.append(ec.message()); |
| 588 |
} else { |
| 589 |
buf.append(string_view(ec.category().name())); |
| 590 |
buf.push_back(':'); |
| 591 |
detail::write<char>(appender(buf), ec.value()); |
| 592 |
} |
| 593 |
auto quoted = memory_buffer(); |
| 594 |
auto str = string_view(buf.data(), buf.size()); |
| 595 |
if (debug_) { |
| 596 |
detail::write_escaped_string<char>(std::back_inserter(quoted), str); |
| 597 |
str = string_view(quoted.data(), quoted.size()); |
| 598 |
} |
| 599 |
return detail::write<char>(ctx.out(), str, specs); |
| 600 |
} |
| 601 |
}; |
| 602 |
|
| 603 |
#if FMT_USE_RTTI |
| 604 |
template <> struct formatter<std::type_info> { |
| 605 |
public: |
| 606 |
FMT_CONSTEXPR auto parse(parse_context<>& ctx) -> const char* { |
| 607 |
return ctx.begin(); |
| 608 |
} |
| 609 |
|
| 610 |
template <typename Context> |
| 611 |
auto format(const std::type_info& ti, Context& ctx) const |
| 612 |
-> decltype(ctx.out()) { |
| 613 |
return detail::write_demangled_name(ctx.out(), ti); |
| 614 |
} |
| 615 |
}; |
| 616 |
#endif // FMT_USE_RTTI |
| 617 |
|
| 618 |
template <typename T> |
| 619 |
struct formatter< |
| 620 |
T, char, |
| 621 |
typename std::enable_if<std::is_base_of<std::exception, T>::value>::type> { |
| 622 |
private: |
| 623 |
bool with_typename_ = false; |
| 624 |
|
| 625 |
public: |
| 626 |
FMT_CONSTEXPR auto parse(parse_context<>& ctx) -> const char* { |
| 627 |
auto it = ctx.begin(); |
| 628 |
auto end = ctx.end(); |
| 629 |
if (it == end || *it == '}') return it; |
| 630 |
if (*it == 't') { |
| 631 |
++it; |
| 632 |
with_typename_ = FMT_USE_RTTI != 0; |
| 633 |
} |
| 634 |
return it; |
| 635 |
} |
| 636 |
|
| 637 |
template <typename Context> |
| 638 |
auto format(const std::exception& ex, Context& ctx) const |
| 639 |
-> decltype(ctx.out()) { |
| 640 |
auto out = ctx.out(); |
| 641 |
#if FMT_USE_RTTI |
| 642 |
if (with_typename_) { |
| 643 |
out = detail::write_demangled_name(out, typeid(ex)); |
| 644 |
*out++ = ':'; |
| 645 |
*out++ = ' '; |
| 646 |
} |
| 647 |
#endif |
| 648 |
return detail::write_bytes<char>(out, string_view(ex.what())); |
| 649 |
} |
| 650 |
}; |
| 651 |
|
| 652 |
template <int N, typename Char> |
| 653 |
struct formatter<detail::bitint<N>, Char> : formatter<long long, Char> { |
| 654 |
static_assert(N <= 64, "unsupported _BitInt"); |
| 655 |
static auto format_as(detail::bitint<N> x) -> long long { |
| 656 |
return static_cast<long long>(x); |
| 657 |
} |
| 658 |
template <typename Context> |
| 659 |
auto format(detail::bitint<N> x, Context& ctx) const -> decltype(ctx.out()) { |
| 660 |
return formatter<long long, Char>::format(format_as(x), ctx); |
| 661 |
} |
| 662 |
}; |
| 663 |
|
| 664 |
template <int N, typename Char> |
| 665 |
struct formatter<detail::ubitint<N>, Char> : formatter<ullong, Char> { |
| 666 |
static_assert(N <= 64, "unsupported _BitInt"); |
| 667 |
static auto format_as(detail::ubitint<N> x) -> ullong { |
| 668 |
return static_cast<ullong>(x); |
| 669 |
} |
| 670 |
template <typename Context> |
| 671 |
auto format(detail::ubitint<N> x, Context& ctx) const -> decltype(ctx.out()) { |
| 672 |
return formatter<ullong, Char>::format(format_as(x), ctx); |
| 673 |
} |
| 674 |
}; |
| 675 |
|
| 676 |
// We can't use std::vector<bool, Allocator>::reference and |
| 677 |
// std::bitset<N>::reference because the compiler can't deduce Allocator and N |
| 678 |
// in partial specialization. |
| 679 |
template <typename BitRef, typename Char> |
| 680 |
struct formatter<BitRef, Char, |
| 681 |
enable_if_t<detail::is_bit_reference_like<BitRef>::value>> |
| 682 |
: formatter<bool, Char> { |
| 683 |
template <typename FormatContext> |
| 684 |
FMT_CONSTEXPR auto format(const BitRef& v, FormatContext& ctx) const |
| 685 |
-> decltype(ctx.out()) { |
| 686 |
return formatter<bool, Char>::format(v, ctx); |
| 687 |
} |
| 688 |
}; |
| 689 |
|
| 690 |
#ifdef __cpp_lib_byte |
| 691 |
template <typename Char> |
| 692 |
struct formatter<std::byte, Char> : formatter<unsigned, Char> { |
| 693 |
FMT_CONSTEXPR static auto format_as(std::byte b) -> unsigned char { |
| 694 |
return static_cast<unsigned char>(b); |
| 695 |
} |
| 696 |
template <typename Context> |
| 697 |
FMT_CONSTEXPR auto format(std::byte b, Context& ctx) const |
| 698 |
-> decltype(ctx.out()) { |
| 699 |
return formatter<unsigned, Char>::format(format_as(b), ctx); |
| 700 |
} |
| 701 |
}; |
| 702 |
#endif |
| 703 |
|
| 704 |
template <typename T, typename Char> |
| 705 |
struct formatter<std::atomic<T>, Char, |
| 706 |
enable_if_t<is_formattable<T, Char>::value>> |
| 707 |
: formatter<T, Char> { |
| 708 |
template <typename FormatContext> |
| 709 |
auto format(const std::atomic<T>& v, FormatContext& ctx) const |
| 710 |
-> decltype(ctx.out()) { |
| 711 |
return formatter<T, Char>::format(v.load(), ctx); |
| 712 |
} |
| 713 |
}; |
| 714 |
|
| 715 |
#ifdef __cpp_lib_atomic_flag_test |
| 716 |
template <typename Char> |
| 717 |
struct formatter<std::atomic_flag, Char> : formatter<bool, Char> { |
| 718 |
template <typename FormatContext> |
| 719 |
auto format(const std::atomic_flag& v, FormatContext& ctx) const |
| 720 |
-> decltype(ctx.out()) { |
| 721 |
return formatter<bool, Char>::format(v.test(), ctx); |
| 722 |
} |
| 723 |
}; |
| 724 |
#endif // __cpp_lib_atomic_flag_test |
| 725 |
|
| 726 |
template <typename T> struct is_tuple_like; |
| 727 |
|
| 728 |
template <typename T> |
| 729 |
struct is_tuple_like<std::complex<T>> : std::false_type {}; |
| 730 |
|
| 731 |
template <typename T, typename Char> struct formatter<std::complex<T>, Char> { |
| 732 |
private: |
| 733 |
detail::dynamic_format_specs<Char> specs_; |
| 734 |
|
| 735 |
template <typename FormatContext, typename OutputIt> |
| 736 |
FMT_CONSTEXPR auto do_format(const std::complex<T>& c, |
| 737 |
detail::dynamic_format_specs<Char>& specs, |
| 738 |
FormatContext& ctx, OutputIt out) const |
| 739 |
-> OutputIt { |
| 740 |
if (c.real() != 0) { |
| 741 |
*out++ = Char('('); |
| 742 |
out = detail::write<Char>(out, c.real(), specs, ctx.locale()); |
| 743 |
specs.set_sign(sign::plus); |
| 744 |
out = detail::write<Char>(out, c.imag(), specs, ctx.locale()); |
| 745 |
if (!detail::isfinite(c.imag())) *out++ = Char(' '); |
| 746 |
*out++ = Char('i'); |
| 747 |
*out++ = Char(')'); |
| 748 |
return out; |
| 749 |
} |
| 750 |
out = detail::write<Char>(out, c.imag(), specs, ctx.locale()); |
| 751 |
if (!detail::isfinite(c.imag())) *out++ = Char(' '); |
| 752 |
*out++ = Char('i'); |
| 753 |
return out; |
| 754 |
} |
| 755 |
|
| 756 |
public: |
| 757 |
FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) -> const Char* { |
| 758 |
if (ctx.begin() == ctx.end() || *ctx.begin() == '}') return ctx.begin(); |
| 759 |
return parse_format_specs(ctx.begin(), ctx.end(), specs_, ctx, |
| 760 |
detail::type_constant<T, Char>::value); |
| 761 |
} |
| 762 |
|
| 763 |
template <typename FormatContext> |
| 764 |
auto format(const std::complex<T>& c, FormatContext& ctx) const |
| 765 |
-> decltype(ctx.out()) { |
| 766 |
auto specs = specs_; |
| 767 |
if (specs.dynamic()) { |
| 768 |
detail::handle_dynamic_spec(specs.dynamic_width(), specs.width, |
| 769 |
specs.width_ref, ctx); |
| 770 |
detail::handle_dynamic_spec(specs.dynamic_precision(), specs.precision, |
| 771 |
specs.precision_ref, ctx); |
| 772 |
} |
| 773 |
|
| 774 |
if (specs.width == 0) return do_format(c, specs, ctx, ctx.out()); |
| 775 |
auto buf = basic_memory_buffer<Char>(); |
| 776 |
|
| 777 |
auto outer_specs = format_specs(); |
| 778 |
outer_specs.width = specs.width; |
| 779 |
outer_specs.copy_fill_from(specs); |
| 780 |
outer_specs.set_align(specs.align()); |
| 781 |
|
| 782 |
specs.width = 0; |
| 783 |
specs.set_fill({}); |
| 784 |
specs.set_align(align::none); |
| 785 |
|
| 786 |
do_format(c, specs, ctx, basic_appender<Char>(buf)); |
| 787 |
return detail::write<Char>(ctx.out(), |
| 788 |
basic_string_view<Char>(buf.data(), buf.size()), |
| 789 |
outer_specs); |
| 790 |
} |
| 791 |
}; |
| 792 |
|
| 793 |
template <typename T, typename Char> |
| 794 |
struct formatter<std::reference_wrapper<T>, Char, |
| 795 |
// Guard against format_as because reference_wrapper is |
| 796 |
// implicitly convertible to T&. |
| 797 |
enable_if_t<is_formattable<remove_cvref_t<T>, Char>::value && |
| 798 |
!detail::has_format_as<T>::value && |
| 799 |
!detail::has_format_as_member<T>::value>> |
| 800 |
: formatter<remove_cvref_t<T>, Char> { |
| 801 |
template <typename FormatContext> |
| 802 |
auto format(std::reference_wrapper<T> ref, FormatContext& ctx) const |
| 803 |
-> decltype(ctx.out()) { |
| 804 |
return formatter<remove_cvref_t<T>, Char>::format(ref.get(), ctx); |
| 805 |
} |
| 806 |
}; |
| 807 |
|
| 808 |
FMT_END_NAMESPACE |
| 809 |
|
| 810 |
#endif // FMT_STD_H_ |