| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#ifndef FMT_STD_H_ |
| 9 |
#define FMT_STD_H_ |
| 10 |
|
| 11 |
#include <bitset> |
| 12 |
#include <cstdlib> |
| 13 |
#include <exception> |
| 14 |
#include <memory> |
| 15 |
#include <thread> |
| 16 |
#include <type_traits> |
| 17 |
#include <typeinfo> |
| 18 |
#include <utility> |
| 19 |
#include <vector> |
| 20 |
|
| 21 |
#include "format.h" |
| 22 |
#include "ostream.h" |
| 23 |
|
| 24 |
#if FMT_HAS_INCLUDE(<version>) |
| 25 |
# include <version> |
| 26 |
#endif |
| 27 |
|
| 28 |
#if FMT_CPLUSPLUS >= 201703L |
| 29 |
# if FMT_HAS_INCLUDE(<filesystem>) |
| 30 |
# include <filesystem> |
| 31 |
# endif |
| 32 |
# if FMT_HAS_INCLUDE(<variant>) |
| 33 |
# include <variant> |
| 34 |
# endif |
| 35 |
# if FMT_HAS_INCLUDE(<optional>) |
| 36 |
# include <optional> |
| 37 |
# endif |
| 38 |
#endif |
| 39 |
|
| 40 |
|
| 41 |
#if FMT_HAS_INCLUDE(<cxxabi.h>) || defined(__GLIBCXX__) |
| 42 |
# include <cxxabi.h> |
| 43 |
|
| 44 |
|
| 45 |
# ifndef __GABIXX_CXXABI_H__ |
| 46 |
# define FMT_HAS_ABI_CXA_DEMANGLE |
| 47 |
# endif |
| 48 |
#endif |
| 49 |
|
| 50 |
|
| 51 |
#ifndef FMT_USE_TYPEID |
| 52 |
|
| 53 |
# if defined(__GXX_RTTI) || FMT_HAS_FEATURE(cxx_rtti) || FMT_MSC_VERSION || \ |
| 54 |
defined(__INTEL_RTTI__) || defined(__RTTI) |
| 55 |
# define FMT_USE_TYPEID 1 |
| 56 |
# else |
| 57 |
# define FMT_USE_TYPEID 0 |
| 58 |
# endif |
| 59 |
#endif |
| 60 |
|
| 61 |
#ifdef __cpp_lib_filesystem |
| 62 |
FMT_BEGIN_NAMESPACE |
| 63 |
|
| 64 |
namespace detail { |
| 65 |
|
| 66 |
template <typename Char> auto get_path_string(const std::filesystem::path& p) { |
| 67 |
return p.string<Char>(); |
| 68 |
} |
| 69 |
|
| 70 |
template <typename Char> |
| 71 |
void write_escaped_path(basic_memory_buffer<Char>& quoted, |
| 72 |
const std::filesystem::path& p) { |
| 73 |
write_escaped_string<Char>(std::back_inserter(quoted), p.string<Char>()); |
| 74 |
} |
| 75 |
|
| 76 |
# ifdef _WIN32 |
| 77 |
template <> |
| 78 |
inline auto get_path_string<char>(const std::filesystem::path& p) { |
| 79 |
return to_utf8<wchar_t>(p.native(), to_utf8_error_policy::replace); |
| 80 |
} |
| 81 |
|
| 82 |
template <> |
| 83 |
inline void write_escaped_path<char>(memory_buffer& quoted, |
| 84 |
const std::filesystem::path& p) { |
| 85 |
auto buf = basic_memory_buffer<wchar_t>(); |
| 86 |
write_escaped_string<wchar_t>(std::back_inserter(buf), p.native()); |
| 87 |
bool valid = to_utf8<wchar_t>::convert(quoted, {buf.data(), buf.size()}); |
| 88 |
FMT_ASSERT(valid, "invalid utf16"); |
| 89 |
} |
| 90 |
# endif |
| 91 |
|
| 92 |
template <> |
| 93 |
inline void write_escaped_path<std::filesystem::path::value_type>( |
| 94 |
basic_memory_buffer<std::filesystem::path::value_type>& quoted, |
| 95 |
const std::filesystem::path& p) { |
| 96 |
write_escaped_string<std::filesystem::path::value_type>( |
| 97 |
std::back_inserter(quoted), p.native()); |
| 98 |
} |
| 99 |
|
| 100 |
} |
| 101 |
|
| 102 |
FMT_EXPORT |
| 103 |
template <typename Char> struct formatter<std::filesystem::path, Char> { |
| 104 |
private: |
| 105 |
format_specs<Char> specs_; |
| 106 |
detail::arg_ref<Char> width_ref_; |
| 107 |
bool debug_ = false; |
| 108 |
|
| 109 |
public: |
| 110 |
FMT_CONSTEXPR void set_debug_format(bool set = true) { debug_ = set; } |
| 111 |
|
| 112 |
template <typename ParseContext> FMT_CONSTEXPR auto parse(ParseContext& ctx) { |
| 113 |
auto it = ctx.begin(), end = ctx.end(); |
| 114 |
if (it == end) return it; |
| 115 |
|
| 116 |
it = detail::parse_align(it, end, specs_); |
| 117 |
if (it == end) return it; |
| 118 |
|
| 119 |
it = detail::parse_dynamic_spec(it, end, specs_.width, width_ref_, ctx); |
| 120 |
if (it != end && *it == '?') { |
| 121 |
debug_ = true; |
| 122 |
++it; |
| 123 |
} |
| 124 |
return it; |
| 125 |
} |
| 126 |
|
| 127 |
template <typename FormatContext> |
| 128 |
auto format(const std::filesystem::path& p, FormatContext& ctx) const { |
| 129 |
auto specs = specs_; |
| 130 |
detail::handle_dynamic_spec<detail::width_checker>(specs.width, width_ref_, |
| 131 |
ctx); |
| 132 |
if (!debug_) { |
| 133 |
auto s = detail::get_path_string<Char>(p); |
| 134 |
return detail::write(ctx.out(), basic_string_view<Char>(s), specs); |
| 135 |
} |
| 136 |
auto quoted = basic_memory_buffer<Char>(); |
| 137 |
detail::write_escaped_path(quoted, p); |
| 138 |
return detail::write(ctx.out(), |
| 139 |
basic_string_view<Char>(quoted.data(), quoted.size()), |
| 140 |
specs); |
| 141 |
} |
| 142 |
}; |
| 143 |
FMT_END_NAMESPACE |
| 144 |
#endif |
| 145 |
|
| 146 |
FMT_BEGIN_NAMESPACE |
| 147 |
FMT_EXPORT |
| 148 |
template <typename Char> |
| 149 |
struct formatter<std::thread::id, Char> : basic_ostream_formatter<Char> {}; |
| 150 |
FMT_END_NAMESPACE |
| 151 |
|
| 152 |
#ifdef __cpp_lib_optional |
| 153 |
FMT_BEGIN_NAMESPACE |
| 154 |
FMT_EXPORT |
| 155 |
template <typename T, typename Char> |
| 156 |
struct formatter<std::optional<T>, Char, |
| 157 |
std::enable_if_t<is_formattable<T, Char>::value>> { |
| 158 |
private: |
| 159 |
formatter<T, Char> underlying_; |
| 160 |
static constexpr basic_string_view<Char> optional = |
| 161 |
detail::string_literal<Char, 'o', 'p', 't', 'i', 'o', 'n', 'a', 'l', |
| 162 |
'('>{}; |
| 163 |
static constexpr basic_string_view<Char> none = |
| 164 |
detail::string_literal<Char, 'n', 'o', 'n', 'e'>{}; |
| 165 |
|
| 166 |
template <class U> |
| 167 |
FMT_CONSTEXPR static auto maybe_set_debug_format(U& u, bool set) |
| 168 |
-> decltype(u.set_debug_format(set)) { |
| 169 |
u.set_debug_format(set); |
| 170 |
} |
| 171 |
|
| 172 |
template <class U> |
| 173 |
FMT_CONSTEXPR static void maybe_set_debug_format(U&, ...) {} |
| 174 |
|
| 175 |
public: |
| 176 |
template <typename ParseContext> FMT_CONSTEXPR auto parse(ParseContext& ctx) { |
| 177 |
maybe_set_debug_format(underlying_, true); |
| 178 |
return underlying_.parse(ctx); |
| 179 |
} |
| 180 |
|
| 181 |
template <typename FormatContext> |
| 182 |
auto format(std::optional<T> const& opt, FormatContext& ctx) const |
| 183 |
-> decltype(ctx.out()) { |
| 184 |
if (!opt) return detail::write<Char>(ctx.out(), none); |
| 185 |
|
| 186 |
auto out = ctx.out(); |
| 187 |
out = detail::write<Char>(out, optional); |
| 188 |
ctx.advance_to(out); |
| 189 |
out = underlying_.format(*opt, ctx); |
| 190 |
return detail::write(out, ')'); |
| 191 |
} |
| 192 |
}; |
| 193 |
FMT_END_NAMESPACE |
| 194 |
#endif |
| 195 |
|
| 196 |
#ifdef __cpp_lib_variant |
| 197 |
FMT_BEGIN_NAMESPACE |
| 198 |
namespace detail { |
| 199 |
|
| 200 |
template <typename T> |
| 201 |
using variant_index_sequence = |
| 202 |
std::make_index_sequence<std::variant_size<T>::value>; |
| 203 |
|
| 204 |
template <typename> struct is_variant_like_ : std::false_type {}; |
| 205 |
template <typename... Types> |
| 206 |
struct is_variant_like_<std::variant<Types...>> : std::true_type {}; |
| 207 |
|
| 208 |
|
| 209 |
template <typename T, typename C> class is_variant_formattable_ { |
| 210 |
template <std::size_t... Is> |
| 211 |
static std::conjunction< |
| 212 |
is_formattable<std::variant_alternative_t<Is, T>, C>...> |
| 213 |
check(std::index_sequence<Is...>); |
| 214 |
|
| 215 |
public: |
| 216 |
static constexpr const bool value = |
| 217 |
decltype(check(variant_index_sequence<T>{}))::value; |
| 218 |
}; |
| 219 |
|
| 220 |
template <typename Char, typename OutputIt, typename T> |
| 221 |
auto write_variant_alternative(OutputIt out, const T& v) -> OutputIt { |
| 222 |
if constexpr (is_string<T>::value) |
| 223 |
return write_escaped_string<Char>(out, detail::to_string_view(v)); |
| 224 |
else if constexpr (std::is_same_v<T, Char>) |
| 225 |
return write_escaped_char(out, v); |
| 226 |
else |
| 227 |
return write<Char>(out, v); |
| 228 |
} |
| 229 |
|
| 230 |
} |
| 231 |
|
| 232 |
template <typename T> struct is_variant_like { |
| 233 |
static constexpr const bool value = detail::is_variant_like_<T>::value; |
| 234 |
}; |
| 235 |
|
| 236 |
template <typename T, typename C> struct is_variant_formattable { |
| 237 |
static constexpr const bool value = |
| 238 |
detail::is_variant_formattable_<T, C>::value; |
| 239 |
}; |
| 240 |
|
| 241 |
FMT_EXPORT |
| 242 |
template <typename Char> struct formatter<std::monostate, Char> { |
| 243 |
template <typename ParseContext> |
| 244 |
FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { |
| 245 |
return ctx.begin(); |
| 246 |
} |
| 247 |
|
| 248 |
template <typename FormatContext> |
| 249 |
auto format(const std::monostate&, FormatContext& ctx) const |
| 250 |
-> decltype(ctx.out()) { |
| 251 |
return detail::write<Char>(ctx.out(), "monostate"); |
| 252 |
} |
| 253 |
}; |
| 254 |
|
| 255 |
FMT_EXPORT |
| 256 |
template <typename Variant, typename Char> |
| 257 |
struct formatter< |
| 258 |
Variant, Char, |
| 259 |
std::enable_if_t<std::conjunction_v< |
| 260 |
is_variant_like<Variant>, is_variant_formattable<Variant, Char>>>> { |
| 261 |
template <typename ParseContext> |
| 262 |
FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { |
| 263 |
return ctx.begin(); |
| 264 |
} |
| 265 |
|
| 266 |
template <typename FormatContext> |
| 267 |
auto format(const Variant& value, FormatContext& ctx) const |
| 268 |
-> decltype(ctx.out()) { |
| 269 |
auto out = ctx.out(); |
| 270 |
|
| 271 |
out = detail::write<Char>(out, "variant("); |
| 272 |
FMT_TRY { |
| 273 |
std::visit( |
| 274 |
[&](const auto& v) { |
| 275 |
out = detail::write_variant_alternative<Char>(out, v); |
| 276 |
}, |
| 277 |
value); |
| 278 |
} |
| 279 |
FMT_CATCH(const std::bad_variant_access&) { |
| 280 |
detail::write<Char>(out, "valueless by exception"); |
| 281 |
} |
| 282 |
*out++ = ')'; |
| 283 |
return out; |
| 284 |
} |
| 285 |
}; |
| 286 |
FMT_END_NAMESPACE |
| 287 |
#endif |
| 288 |
|
| 289 |
FMT_BEGIN_NAMESPACE |
| 290 |
FMT_EXPORT |
| 291 |
template <typename Char> struct formatter<std::error_code, Char> { |
| 292 |
template <typename ParseContext> |
| 293 |
FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { |
| 294 |
return ctx.begin(); |
| 295 |
} |
| 296 |
|
| 297 |
template <typename FormatContext> |
| 298 |
FMT_CONSTEXPR auto format(const std::error_code& ec, FormatContext& ctx) const |
| 299 |
-> decltype(ctx.out()) { |
| 300 |
auto out = ctx.out(); |
| 301 |
out = detail::write_bytes(out, ec.category().name(), format_specs<Char>()); |
| 302 |
out = detail::write<Char>(out, Char(':')); |
| 303 |
out = detail::write<Char>(out, ec.value()); |
| 304 |
return out; |
| 305 |
} |
| 306 |
}; |
| 307 |
|
| 308 |
FMT_EXPORT |
| 309 |
template <typename T, typename Char> |
| 310 |
struct formatter< |
| 311 |
T, Char, |
| 312 |
typename std::enable_if<std::is_base_of<std::exception, T>::value>::type> { |
| 313 |
private: |
| 314 |
bool with_typename_ = false; |
| 315 |
|
| 316 |
public: |
| 317 |
FMT_CONSTEXPR auto parse(basic_format_parse_context<Char>& ctx) |
| 318 |
-> decltype(ctx.begin()) { |
| 319 |
auto it = ctx.begin(); |
| 320 |
auto end = ctx.end(); |
| 321 |
if (it == end || *it == '}') return it; |
| 322 |
if (*it == 't') { |
| 323 |
++it; |
| 324 |
with_typename_ = FMT_USE_TYPEID != 0; |
| 325 |
} |
| 326 |
return it; |
| 327 |
} |
| 328 |
|
| 329 |
template <typename OutputIt> |
| 330 |
auto format(const std::exception& ex, |
| 331 |
basic_format_context<OutputIt, Char>& ctx) const -> OutputIt { |
| 332 |
format_specs<Char> spec; |
| 333 |
auto out = ctx.out(); |
| 334 |
if (!with_typename_) |
| 335 |
return detail::write_bytes(out, string_view(ex.what()), spec); |
| 336 |
|
| 337 |
#if FMT_USE_TYPEID |
| 338 |
const std::type_info& ti = typeid(ex); |
| 339 |
# ifdef FMT_HAS_ABI_CXA_DEMANGLE |
| 340 |
int status = 0; |
| 341 |
std::size_t size = 0; |
| 342 |
std::unique_ptr<char, decltype(&std::free)> demangled_name_ptr( |
| 343 |
abi::__cxa_demangle(ti.name(), nullptr, &size, &status), &std::free); |
| 344 |
|
| 345 |
string_view demangled_name_view; |
| 346 |
if (demangled_name_ptr) { |
| 347 |
demangled_name_view = demangled_name_ptr.get(); |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
if (demangled_name_view.starts_with("std::")) { |
| 357 |
char* begin = demangled_name_ptr.get(); |
| 358 |
char* to = begin + 5; |
| 359 |
for (char *from = to, *end = begin + demangled_name_view.size(); |
| 360 |
from < end;) { |
| 361 |
|
| 362 |
if (from[0] == '_' && from[1] == '_') { |
| 363 |
char* next = from + 1; |
| 364 |
while (next < end && *next != ':') next++; |
| 365 |
if (next[0] == ':' && next[1] == ':') { |
| 366 |
from = next + 2; |
| 367 |
continue; |
| 368 |
} |
| 369 |
} |
| 370 |
*to++ = *from++; |
| 371 |
} |
| 372 |
demangled_name_view = {begin, detail::to_unsigned(to - begin)}; |
| 373 |
} |
| 374 |
} else { |
| 375 |
demangled_name_view = string_view(ti.name()); |
| 376 |
} |
| 377 |
out = detail::write_bytes(out, demangled_name_view, spec); |
| 378 |
# elif FMT_MSC_VERSION |
| 379 |
string_view demangled_name_view(ti.name()); |
| 380 |
if (demangled_name_view.starts_with("class ")) |
| 381 |
demangled_name_view.remove_prefix(6); |
| 382 |
else if (demangled_name_view.starts_with("struct ")) |
| 383 |
demangled_name_view.remove_prefix(7); |
| 384 |
out = detail::write_bytes(out, demangled_name_view, spec); |
| 385 |
# else |
| 386 |
out = detail::write_bytes(out, string_view(ti.name()), spec); |
| 387 |
# endif |
| 388 |
*out++ = ':'; |
| 389 |
*out++ = ' '; |
| 390 |
return detail::write_bytes(out, string_view(ex.what()), spec); |
| 391 |
#endif |
| 392 |
} |
| 393 |
}; |
| 394 |
|
| 395 |
namespace detail { |
| 396 |
|
| 397 |
template <typename T, typename Enable = void> |
| 398 |
struct has_flip : std::false_type {}; |
| 399 |
|
| 400 |
template <typename T> |
| 401 |
struct has_flip<T, void_t<decltype(std::declval<T>().flip())>> |
| 402 |
: std::true_type {}; |
| 403 |
|
| 404 |
template <typename T> struct is_bit_reference_like { |
| 405 |
static constexpr const bool value = |
| 406 |
std::is_convertible<T, bool>::value && |
| 407 |
std::is_nothrow_assignable<T, bool>::value && has_flip<T>::value; |
| 408 |
}; |
| 409 |
|
| 410 |
#ifdef _LIBCPP_VERSION |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
template <typename C> |
| 415 |
struct is_bit_reference_like<std::__bit_const_reference<C>> { |
| 416 |
static constexpr const bool value = true; |
| 417 |
}; |
| 418 |
|
| 419 |
#endif |
| 420 |
|
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
FMT_EXPORT |
| 427 |
template <typename BitRef, typename Char> |
| 428 |
struct formatter<BitRef, Char, |
| 429 |
enable_if_t<detail::is_bit_reference_like<BitRef>::value>> |
| 430 |
: formatter<bool, Char> { |
| 431 |
template <typename FormatContext> |
| 432 |
FMT_CONSTEXPR auto format(const BitRef& v, FormatContext& ctx) const |
| 433 |
-> decltype(ctx.out()) { |
| 434 |
return formatter<bool, Char>::format(v, ctx); |
| 435 |
} |
| 436 |
}; |
| 437 |
|
| 438 |
FMT_END_NAMESPACE |
| 439 |
|
| 440 |
#endif |