std.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. // Formatting library for C++ - formatters for standard library types
  2. //
  3. // Copyright (c) 2012 - present, Victor Zverovich
  4. // All rights reserved.
  5. //
  6. // For the license information refer to format.h.
  7. #ifndef FMT_STD_H_
  8. #define FMT_STD_H_
  9. #include "format.h"
  10. #include "ostream.h"
  11. #ifndef FMT_MODULE
  12. # include <atomic>
  13. # include <bitset>
  14. # include <complex>
  15. # include <cstdlib>
  16. # include <exception>
  17. # include <memory>
  18. # include <thread>
  19. # include <type_traits>
  20. # include <typeinfo>
  21. # include <utility>
  22. # include <vector>
  23. // Check FMT_CPLUSPLUS to suppress a bogus warning in MSVC.
  24. # if FMT_CPLUSPLUS >= 201703L
  25. # if FMT_HAS_INCLUDE(<filesystem>)
  26. # include <filesystem>
  27. # endif
  28. # if FMT_HAS_INCLUDE(<variant>)
  29. # include <variant>
  30. # endif
  31. # if FMT_HAS_INCLUDE(<optional>)
  32. # include <optional>
  33. # endif
  34. # endif
  35. // Use > instead of >= in the version check because <source_location> may be
  36. // available after C++17 but before C++20 is marked as implemented.
  37. # if FMT_CPLUSPLUS > 201703L && FMT_HAS_INCLUDE(<source_location>)
  38. # include <source_location>
  39. # endif
  40. # if FMT_CPLUSPLUS > 202002L && FMT_HAS_INCLUDE(<expected>)
  41. # include <expected>
  42. # endif
  43. #endif // FMT_MODULE
  44. #if FMT_HAS_INCLUDE(<version>)
  45. # include <version>
  46. #endif
  47. // GCC 4 does not support FMT_HAS_INCLUDE.
  48. #if FMT_HAS_INCLUDE(<cxxabi.h>) || defined(__GLIBCXX__)
  49. # include <cxxabi.h>
  50. // Android NDK with gabi++ library on some architectures does not implement
  51. // abi::__cxa_demangle().
  52. # ifndef __GABIXX_CXXABI_H__
  53. # define FMT_HAS_ABI_CXA_DEMANGLE
  54. # endif
  55. #endif
  56. // For older Xcode versions, __cpp_lib_xxx flags are inaccurately defined.
  57. #ifndef FMT_CPP_LIB_FILESYSTEM
  58. # ifdef __cpp_lib_filesystem
  59. # define FMT_CPP_LIB_FILESYSTEM __cpp_lib_filesystem
  60. # else
  61. # define FMT_CPP_LIB_FILESYSTEM 0
  62. # endif
  63. #endif
  64. #ifndef FMT_CPP_LIB_VARIANT
  65. # ifdef __cpp_lib_variant
  66. # define FMT_CPP_LIB_VARIANT __cpp_lib_variant
  67. # else
  68. # define FMT_CPP_LIB_VARIANT 0
  69. # endif
  70. #endif
  71. #if FMT_CPP_LIB_FILESYSTEM
  72. FMT_BEGIN_NAMESPACE
  73. namespace detail {
  74. template <typename Char, typename PathChar>
  75. auto get_path_string(const std::filesystem::path& p,
  76. const std::basic_string<PathChar>& native) {
  77. if constexpr (std::is_same_v<Char, char> && std::is_same_v<PathChar, wchar_t>)
  78. return to_utf8<wchar_t>(native, to_utf8_error_policy::replace);
  79. else
  80. return p.string<Char>();
  81. }
  82. template <typename Char, typename PathChar>
  83. void write_escaped_path(basic_memory_buffer<Char>& quoted,
  84. const std::filesystem::path& p,
  85. const std::basic_string<PathChar>& native) {
  86. if constexpr (std::is_same_v<Char, char> &&
  87. std::is_same_v<PathChar, wchar_t>) {
  88. auto buf = basic_memory_buffer<wchar_t>();
  89. write_escaped_string<wchar_t>(std::back_inserter(buf), native);
  90. bool valid = to_utf8<wchar_t>::convert(quoted, {buf.data(), buf.size()});
  91. FMT_ASSERT(valid, "invalid utf16");
  92. } else if constexpr (std::is_same_v<Char, PathChar>) {
  93. write_escaped_string<std::filesystem::path::value_type>(
  94. std::back_inserter(quoted), native);
  95. } else {
  96. write_escaped_string<Char>(std::back_inserter(quoted), p.string<Char>());
  97. }
  98. }
  99. } // namespace detail
  100. FMT_EXPORT
  101. template <typename Char> struct formatter<std::filesystem::path, Char> {
  102. private:
  103. format_specs specs_;
  104. detail::arg_ref<Char> width_ref_;
  105. bool debug_ = false;
  106. char path_type_ = 0;
  107. public:
  108. FMT_CONSTEXPR void set_debug_format(bool set = true) { debug_ = set; }
  109. template <typename ParseContext> FMT_CONSTEXPR auto parse(ParseContext& ctx) {
  110. auto it = ctx.begin(), end = ctx.end();
  111. if (it == end) return it;
  112. it = detail::parse_align(it, end, specs_);
  113. if (it == end) return it;
  114. it = detail::parse_dynamic_spec(it, end, specs_.width, width_ref_, ctx);
  115. if (it != end && *it == '?') {
  116. debug_ = true;
  117. ++it;
  118. }
  119. if (it != end && (*it == 'g')) path_type_ = detail::to_ascii(*it++);
  120. return it;
  121. }
  122. template <typename FormatContext>
  123. auto format(const std::filesystem::path& p, FormatContext& ctx) const {
  124. auto specs = specs_;
  125. auto path_string =
  126. !path_type_ ? p.native()
  127. : p.generic_string<std::filesystem::path::value_type>();
  128. detail::handle_dynamic_spec<detail::width_checker>(specs.width, width_ref_,
  129. ctx);
  130. if (!debug_) {
  131. auto s = detail::get_path_string<Char>(p, path_string);
  132. return detail::write(ctx.out(), basic_string_view<Char>(s), specs);
  133. }
  134. auto quoted = basic_memory_buffer<Char>();
  135. detail::write_escaped_path(quoted, p, path_string);
  136. return detail::write(ctx.out(),
  137. basic_string_view<Char>(quoted.data(), quoted.size()),
  138. specs);
  139. }
  140. };
  141. class path : public std::filesystem::path {
  142. public:
  143. auto display_string() const -> std::string {
  144. const std::filesystem::path& base = *this;
  145. return fmt::format(FMT_STRING("{}"), base);
  146. }
  147. auto system_string() const -> std::string { return string(); }
  148. auto generic_display_string() const -> std::string {
  149. const std::filesystem::path& base = *this;
  150. return fmt::format(FMT_STRING("{:g}"), base);
  151. }
  152. auto generic_system_string() const -> std::string { return generic_string(); }
  153. };
  154. FMT_END_NAMESPACE
  155. #endif // FMT_CPP_LIB_FILESYSTEM
  156. FMT_BEGIN_NAMESPACE
  157. FMT_EXPORT
  158. template <std::size_t N, typename Char>
  159. struct formatter<std::bitset<N>, Char> : nested_formatter<string_view> {
  160. private:
  161. // Functor because C++11 doesn't support generic lambdas.
  162. struct writer {
  163. const std::bitset<N>& bs;
  164. template <typename OutputIt>
  165. FMT_CONSTEXPR auto operator()(OutputIt out) -> OutputIt {
  166. for (auto pos = N; pos > 0; --pos) {
  167. out = detail::write<Char>(out, bs[pos - 1] ? Char('1') : Char('0'));
  168. }
  169. return out;
  170. }
  171. };
  172. public:
  173. template <typename FormatContext>
  174. auto format(const std::bitset<N>& bs, FormatContext& ctx) const
  175. -> decltype(ctx.out()) {
  176. return write_padded(ctx, writer{bs});
  177. }
  178. };
  179. FMT_EXPORT
  180. template <typename Char>
  181. struct formatter<std::thread::id, Char> : basic_ostream_formatter<Char> {};
  182. FMT_END_NAMESPACE
  183. #ifdef __cpp_lib_optional
  184. FMT_BEGIN_NAMESPACE
  185. FMT_EXPORT
  186. template <typename T, typename Char>
  187. struct formatter<std::optional<T>, Char,
  188. std::enable_if_t<is_formattable<T, Char>::value>> {
  189. private:
  190. formatter<T, Char> underlying_;
  191. static constexpr basic_string_view<Char> optional =
  192. detail::string_literal<Char, 'o', 'p', 't', 'i', 'o', 'n', 'a', 'l',
  193. '('>{};
  194. static constexpr basic_string_view<Char> none =
  195. detail::string_literal<Char, 'n', 'o', 'n', 'e'>{};
  196. template <class U>
  197. FMT_CONSTEXPR static auto maybe_set_debug_format(U& u, bool set)
  198. -> decltype(u.set_debug_format(set)) {
  199. u.set_debug_format(set);
  200. }
  201. template <class U>
  202. FMT_CONSTEXPR static void maybe_set_debug_format(U&, ...) {}
  203. public:
  204. template <typename ParseContext> FMT_CONSTEXPR auto parse(ParseContext& ctx) {
  205. maybe_set_debug_format(underlying_, true);
  206. return underlying_.parse(ctx);
  207. }
  208. template <typename FormatContext>
  209. auto format(const std::optional<T>& opt, FormatContext& ctx) const
  210. -> decltype(ctx.out()) {
  211. if (!opt) return detail::write<Char>(ctx.out(), none);
  212. auto out = ctx.out();
  213. out = detail::write<Char>(out, optional);
  214. ctx.advance_to(out);
  215. out = underlying_.format(*opt, ctx);
  216. return detail::write(out, ')');
  217. }
  218. };
  219. FMT_END_NAMESPACE
  220. #endif // __cpp_lib_optional
  221. #if defined(__cpp_lib_expected) || FMT_CPP_LIB_VARIANT
  222. FMT_BEGIN_NAMESPACE
  223. namespace detail {
  224. template <typename Char, typename OutputIt, typename T>
  225. auto write_escaped_alternative(OutputIt out, const T& v) -> OutputIt {
  226. if constexpr (has_to_string_view<T>::value)
  227. return write_escaped_string<Char>(out, detail::to_string_view(v));
  228. if constexpr (std::is_same_v<T, Char>) return write_escaped_char(out, v);
  229. return write<Char>(out, v);
  230. }
  231. } // namespace detail
  232. FMT_END_NAMESPACE
  233. #endif
  234. #ifdef __cpp_lib_expected
  235. FMT_BEGIN_NAMESPACE
  236. FMT_EXPORT
  237. template <typename T, typename E, typename Char>
  238. struct formatter<std::expected<T, E>, Char,
  239. std::enable_if_t<is_formattable<T, Char>::value &&
  240. is_formattable<E, Char>::value>> {
  241. template <typename ParseContext>
  242. FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
  243. return ctx.begin();
  244. }
  245. template <typename FormatContext>
  246. auto format(const std::expected<T, E>& value, FormatContext& ctx) const
  247. -> decltype(ctx.out()) {
  248. auto out = ctx.out();
  249. if (value.has_value()) {
  250. out = detail::write<Char>(out, "expected(");
  251. out = detail::write_escaped_alternative<Char>(out, *value);
  252. } else {
  253. out = detail::write<Char>(out, "unexpected(");
  254. out = detail::write_escaped_alternative<Char>(out, value.error());
  255. }
  256. *out++ = ')';
  257. return out;
  258. }
  259. };
  260. FMT_END_NAMESPACE
  261. #endif // __cpp_lib_expected
  262. #ifdef __cpp_lib_source_location
  263. FMT_BEGIN_NAMESPACE
  264. FMT_EXPORT
  265. template <> struct formatter<std::source_location> {
  266. template <typename ParseContext> FMT_CONSTEXPR auto parse(ParseContext& ctx) {
  267. return ctx.begin();
  268. }
  269. template <typename FormatContext>
  270. auto format(const std::source_location& loc, FormatContext& ctx) const
  271. -> decltype(ctx.out()) {
  272. auto out = ctx.out();
  273. out = detail::write(out, loc.file_name());
  274. out = detail::write(out, ':');
  275. out = detail::write<char>(out, loc.line());
  276. out = detail::write(out, ':');
  277. out = detail::write<char>(out, loc.column());
  278. out = detail::write(out, ": ");
  279. out = detail::write(out, loc.function_name());
  280. return out;
  281. }
  282. };
  283. FMT_END_NAMESPACE
  284. #endif
  285. #if FMT_CPP_LIB_VARIANT
  286. FMT_BEGIN_NAMESPACE
  287. namespace detail {
  288. template <typename T>
  289. using variant_index_sequence =
  290. std::make_index_sequence<std::variant_size<T>::value>;
  291. template <typename> struct is_variant_like_ : std::false_type {};
  292. template <typename... Types>
  293. struct is_variant_like_<std::variant<Types...>> : std::true_type {};
  294. // formattable element check.
  295. template <typename T, typename C> class is_variant_formattable_ {
  296. template <std::size_t... Is>
  297. static std::conjunction<
  298. is_formattable<std::variant_alternative_t<Is, T>, C>...>
  299. check(std::index_sequence<Is...>);
  300. public:
  301. static constexpr const bool value =
  302. decltype(check(variant_index_sequence<T>{}))::value;
  303. };
  304. } // namespace detail
  305. template <typename T> struct is_variant_like {
  306. static constexpr const bool value = detail::is_variant_like_<T>::value;
  307. };
  308. template <typename T, typename C> struct is_variant_formattable {
  309. static constexpr const bool value =
  310. detail::is_variant_formattable_<T, C>::value;
  311. };
  312. FMT_EXPORT
  313. template <typename Char> struct formatter<std::monostate, Char> {
  314. template <typename ParseContext>
  315. FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
  316. return ctx.begin();
  317. }
  318. template <typename FormatContext>
  319. auto format(const std::monostate&, FormatContext& ctx) const
  320. -> decltype(ctx.out()) {
  321. return detail::write<Char>(ctx.out(), "monostate");
  322. }
  323. };
  324. FMT_EXPORT
  325. template <typename Variant, typename Char>
  326. struct formatter<
  327. Variant, Char,
  328. std::enable_if_t<std::conjunction_v<
  329. is_variant_like<Variant>, is_variant_formattable<Variant, Char>>>> {
  330. template <typename ParseContext>
  331. FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
  332. return ctx.begin();
  333. }
  334. template <typename FormatContext>
  335. auto format(const Variant& value, FormatContext& ctx) const
  336. -> decltype(ctx.out()) {
  337. auto out = ctx.out();
  338. out = detail::write<Char>(out, "variant(");
  339. FMT_TRY {
  340. std::visit(
  341. [&](const auto& v) {
  342. out = detail::write_escaped_alternative<Char>(out, v);
  343. },
  344. value);
  345. }
  346. FMT_CATCH(const std::bad_variant_access&) {
  347. detail::write<Char>(out, "valueless by exception");
  348. }
  349. *out++ = ')';
  350. return out;
  351. }
  352. };
  353. FMT_END_NAMESPACE
  354. #endif // FMT_CPP_LIB_VARIANT
  355. FMT_BEGIN_NAMESPACE
  356. FMT_EXPORT
  357. template <typename Char> struct formatter<std::error_code, Char> {
  358. template <typename ParseContext>
  359. FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
  360. return ctx.begin();
  361. }
  362. template <typename FormatContext>
  363. FMT_CONSTEXPR auto format(const std::error_code& ec, FormatContext& ctx) const
  364. -> decltype(ctx.out()) {
  365. auto out = ctx.out();
  366. out = detail::write_bytes<Char>(out, ec.category().name(), format_specs());
  367. out = detail::write<Char>(out, Char(':'));
  368. out = detail::write<Char>(out, ec.value());
  369. return out;
  370. }
  371. };
  372. #if FMT_USE_RTTI
  373. namespace detail {
  374. template <typename Char, typename OutputIt>
  375. auto write_demangled_name(OutputIt out, const std::type_info& ti) -> OutputIt {
  376. # ifdef FMT_HAS_ABI_CXA_DEMANGLE
  377. int status = 0;
  378. std::size_t size = 0;
  379. std::unique_ptr<char, void (*)(void*)> demangled_name_ptr(
  380. abi::__cxa_demangle(ti.name(), nullptr, &size, &status), &std::free);
  381. string_view demangled_name_view;
  382. if (demangled_name_ptr) {
  383. demangled_name_view = demangled_name_ptr.get();
  384. // Normalization of stdlib inline namespace names.
  385. // libc++ inline namespaces.
  386. // std::__1::* -> std::*
  387. // std::__1::__fs::* -> std::*
  388. // libstdc++ inline namespaces.
  389. // std::__cxx11::* -> std::*
  390. // std::filesystem::__cxx11::* -> std::filesystem::*
  391. if (demangled_name_view.starts_with("std::")) {
  392. char* begin = demangled_name_ptr.get();
  393. char* to = begin + 5; // std::
  394. for (char *from = to, *end = begin + demangled_name_view.size();
  395. from < end;) {
  396. // This is safe, because demangled_name is NUL-terminated.
  397. if (from[0] == '_' && from[1] == '_') {
  398. char* next = from + 1;
  399. while (next < end && *next != ':') next++;
  400. if (next[0] == ':' && next[1] == ':') {
  401. from = next + 2;
  402. continue;
  403. }
  404. }
  405. *to++ = *from++;
  406. }
  407. demangled_name_view = {begin, detail::to_unsigned(to - begin)};
  408. }
  409. } else {
  410. demangled_name_view = string_view(ti.name());
  411. }
  412. return detail::write_bytes<Char>(out, demangled_name_view);
  413. # elif FMT_MSC_VERSION
  414. const string_view demangled_name(ti.name());
  415. for (std::size_t i = 0; i < demangled_name.size(); ++i) {
  416. auto sub = demangled_name;
  417. sub.remove_prefix(i);
  418. if (sub.starts_with("enum ")) {
  419. i += 4;
  420. continue;
  421. }
  422. if (sub.starts_with("class ") || sub.starts_with("union ")) {
  423. i += 5;
  424. continue;
  425. }
  426. if (sub.starts_with("struct ")) {
  427. i += 6;
  428. continue;
  429. }
  430. if (*sub.begin() != ' ') *out++ = *sub.begin();
  431. }
  432. return out;
  433. # else
  434. return detail::write_bytes<Char>(out, string_view(ti.name()));
  435. # endif
  436. }
  437. } // namespace detail
  438. FMT_EXPORT
  439. template <typename Char>
  440. struct formatter<std::type_info, Char // DEPRECATED! Mixing code unit types.
  441. > {
  442. public:
  443. FMT_CONSTEXPR auto parse(basic_format_parse_context<Char>& ctx)
  444. -> decltype(ctx.begin()) {
  445. return ctx.begin();
  446. }
  447. template <typename Context>
  448. auto format(const std::type_info& ti, Context& ctx) const
  449. -> decltype(ctx.out()) {
  450. return detail::write_demangled_name<Char>(ctx.out(), ti);
  451. }
  452. };
  453. #endif
  454. FMT_EXPORT
  455. template <typename T, typename Char>
  456. struct formatter<
  457. T, Char, // DEPRECATED! Mixing code unit types.
  458. typename std::enable_if<std::is_base_of<std::exception, T>::value>::type> {
  459. private:
  460. bool with_typename_ = false;
  461. public:
  462. FMT_CONSTEXPR auto parse(basic_format_parse_context<Char>& ctx)
  463. -> decltype(ctx.begin()) {
  464. auto it = ctx.begin();
  465. auto end = ctx.end();
  466. if (it == end || *it == '}') return it;
  467. if (*it == 't') {
  468. ++it;
  469. with_typename_ = FMT_USE_RTTI != 0;
  470. }
  471. return it;
  472. }
  473. template <typename Context>
  474. auto format(const std::exception& ex, Context& ctx) const
  475. -> decltype(ctx.out()) {
  476. auto out = ctx.out();
  477. #if FMT_USE_RTTI
  478. if (with_typename_) {
  479. out = detail::write_demangled_name<Char>(out, typeid(ex));
  480. *out++ = ':';
  481. *out++ = ' ';
  482. }
  483. #endif
  484. return detail::write_bytes<Char>(out, string_view(ex.what()));
  485. }
  486. };
  487. namespace detail {
  488. template <typename T, typename Enable = void>
  489. struct has_flip : std::false_type {};
  490. template <typename T>
  491. struct has_flip<T, void_t<decltype(std::declval<T>().flip())>>
  492. : std::true_type {};
  493. template <typename T> struct is_bit_reference_like {
  494. static constexpr const bool value =
  495. std::is_convertible<T, bool>::value &&
  496. std::is_nothrow_assignable<T, bool>::value && has_flip<T>::value;
  497. };
  498. #ifdef _LIBCPP_VERSION
  499. // Workaround for libc++ incompatibility with C++ standard.
  500. // According to the Standard, `bitset::operator[] const` returns bool.
  501. template <typename C>
  502. struct is_bit_reference_like<std::__bit_const_reference<C>> {
  503. static constexpr const bool value = true;
  504. };
  505. #endif
  506. } // namespace detail
  507. // We can't use std::vector<bool, Allocator>::reference and
  508. // std::bitset<N>::reference because the compiler can't deduce Allocator and N
  509. // in partial specialization.
  510. FMT_EXPORT
  511. template <typename BitRef, typename Char>
  512. struct formatter<BitRef, Char,
  513. enable_if_t<detail::is_bit_reference_like<BitRef>::value>>
  514. : formatter<bool, Char> {
  515. template <typename FormatContext>
  516. FMT_CONSTEXPR auto format(const BitRef& v, FormatContext& ctx) const
  517. -> decltype(ctx.out()) {
  518. return formatter<bool, Char>::format(v, ctx);
  519. }
  520. };
  521. template <typename T, typename Deleter>
  522. auto ptr(const std::unique_ptr<T, Deleter>& p) -> const void* {
  523. return p.get();
  524. }
  525. template <typename T> auto ptr(const std::shared_ptr<T>& p) -> const void* {
  526. return p.get();
  527. }
  528. FMT_EXPORT
  529. template <typename T, typename Char>
  530. struct formatter<std::atomic<T>, Char,
  531. enable_if_t<is_formattable<T, Char>::value>>
  532. : formatter<T, Char> {
  533. template <typename FormatContext>
  534. auto format(const std::atomic<T>& v, FormatContext& ctx) const
  535. -> decltype(ctx.out()) {
  536. return formatter<T, Char>::format(v.load(), ctx);
  537. }
  538. };
  539. #ifdef __cpp_lib_atomic_flag_test
  540. FMT_EXPORT
  541. template <typename Char>
  542. struct formatter<std::atomic_flag, Char> : formatter<bool, Char> {
  543. template <typename FormatContext>
  544. auto format(const std::atomic_flag& v, FormatContext& ctx) const
  545. -> decltype(ctx.out()) {
  546. return formatter<bool, Char>::format(v.test(), ctx);
  547. }
  548. };
  549. #endif // __cpp_lib_atomic_flag_test
  550. FMT_EXPORT
  551. template <typename T, typename Char> struct formatter<std::complex<T>, Char> {
  552. private:
  553. detail::dynamic_format_specs<Char> specs_;
  554. template <typename FormatContext, typename OutputIt>
  555. FMT_CONSTEXPR auto do_format(const std::complex<T>& c,
  556. detail::dynamic_format_specs<Char>& specs,
  557. FormatContext& ctx, OutputIt out) const
  558. -> OutputIt {
  559. if (c.real() != 0) {
  560. *out++ = Char('(');
  561. out = detail::write<Char>(out, c.real(), specs, ctx.locale());
  562. specs.sign = sign::plus;
  563. out = detail::write<Char>(out, c.imag(), specs, ctx.locale());
  564. if (!detail::isfinite(c.imag())) *out++ = Char(' ');
  565. *out++ = Char('i');
  566. *out++ = Char(')');
  567. return out;
  568. }
  569. out = detail::write<Char>(out, c.imag(), specs, ctx.locale());
  570. if (!detail::isfinite(c.imag())) *out++ = Char(' ');
  571. *out++ = Char('i');
  572. return out;
  573. }
  574. public:
  575. FMT_CONSTEXPR auto parse(basic_format_parse_context<Char>& ctx)
  576. -> decltype(ctx.begin()) {
  577. if (ctx.begin() == ctx.end() || *ctx.begin() == '}') return ctx.begin();
  578. return parse_format_specs(ctx.begin(), ctx.end(), specs_, ctx,
  579. detail::type_constant<T, Char>::value);
  580. }
  581. template <typename FormatContext>
  582. auto format(const std::complex<T>& c, FormatContext& ctx) const
  583. -> decltype(ctx.out()) {
  584. auto specs = specs_;
  585. if (specs.width_ref.kind != detail::arg_id_kind::none ||
  586. specs.precision_ref.kind != detail::arg_id_kind::none) {
  587. detail::handle_dynamic_spec<detail::width_checker>(specs.width,
  588. specs.width_ref, ctx);
  589. detail::handle_dynamic_spec<detail::precision_checker>(
  590. specs.precision, specs.precision_ref, ctx);
  591. }
  592. if (specs.width == 0) return do_format(c, specs, ctx, ctx.out());
  593. auto buf = basic_memory_buffer<Char>();
  594. auto outer_specs = format_specs();
  595. outer_specs.width = specs.width;
  596. outer_specs.fill = specs.fill;
  597. outer_specs.align = specs.align;
  598. specs.width = 0;
  599. specs.fill = {};
  600. specs.align = align::none;
  601. do_format(c, specs, ctx, basic_appender<Char>(buf));
  602. return detail::write<Char>(ctx.out(),
  603. basic_string_view<Char>(buf.data(), buf.size()),
  604. outer_specs);
  605. }
  606. };
  607. FMT_END_NAMESPACE
  608. #endif // FMT_STD_H_