format 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP_FORMAT
  10. #define _LIBCPP_FORMAT
  11. /*
  12. namespace std {
  13. // [format.context], class template basic_format_context
  14. template<class Out, class charT> class basic_format_context;
  15. using format_context = basic_format_context<unspecified, char>;
  16. using wformat_context = basic_format_context<unspecified, wchar_t>;
  17. // [format.args], class template basic_format_args
  18. template<class Context> class basic_format_args;
  19. using format_args = basic_format_args<format_context>;
  20. using wformat_args = basic_format_args<wformat_context>;
  21. // [format.fmt.string], class template basic-format-string
  22. template<class charT, class... Args>
  23. struct basic-format-string; // exposition only
  24. template<class... Args>
  25. using format-string = // exposition only
  26. basic-format-string<char, type_identity_t<Args>...>;
  27. template<class... Args>
  28. using wformat-string = // exposition only
  29. basic-format-string<wchar_t, type_identity_t<Args>...>;
  30. // [format.functions], formatting functions
  31. template<class... Args>
  32. string format(format-string<Args...> fmt, Args&&... args);
  33. template<class... Args>
  34. wstring format(wformat-string<Args...> fmt, Args&&... args);
  35. template<class... Args>
  36. string format(const locale& loc, format-string<Args...> fmt, Args&&... args);
  37. template<class... Args>
  38. wstring format(const locale& loc, wformat-string<Args...> fmt, Args&&... args);
  39. string vformat(string_view fmt, format_args args);
  40. wstring vformat(wstring_view fmt, wformat_args args);
  41. string vformat(const locale& loc, string_view fmt, format_args args);
  42. wstring vformat(const locale& loc, wstring_view fmt, wformat_args args);
  43. template<class Out, class... Args>
  44. Out format_to(Out out, format-string<Args...> fmt, Args&&... args);
  45. template<class Out, class... Args>
  46. Out format_to(Out out, wformat-string<Args...> fmt, Args&&... args);
  47. template<class Out, class... Args>
  48. Out format_to(Out out, const locale& loc, format-string<Args...> fmt, Args&&... args);
  49. template<class Out, class... Args>
  50. Out format_to(Out out, const locale& loc, wformat-string<Args...> fmt, Args&&... args);
  51. template<class Out>
  52. Out vformat_to(Out out, string_view fmt, format_args args);
  53. template<class Out>
  54. Out vformat_to(Out out, wstring_view fmt, wformat_args args);
  55. template<class Out>
  56. Out vformat_to(Out out, const locale& loc, string_view fmt,
  57. format_args char> args);
  58. template<class Out>
  59. Out vformat_to(Out out, const locale& loc, wstring_view fmt,
  60. wformat_args args);
  61. template<class Out> struct format_to_n_result {
  62. Out out;
  63. iter_difference_t<Out> size;
  64. };
  65. template<class Out, class... Args>
  66. format_to_n_result<Out> format_to_n(Out out, iter_difference_t<Out> n,
  67. format-string<Args...> fmt, Args&&... args);
  68. template<class Out, class... Args>
  69. format_to_n_result<Out> format_to_n(Out out, iter_difference_t<Out> n,
  70. wformat-string<Args...> fmt, Args&&... args);
  71. template<class Out, class... Args>
  72. format_to_n_result<Out> format_to_n(Out out, iter_difference_t<Out> n,
  73. const locale& loc, format-string<Args...> fmt,
  74. Args&&... args);
  75. template<class Out, class... Args>
  76. format_to_n_result<Out> format_to_n(Out out, iter_difference_t<Out> n,
  77. const locale& loc, wformat-string<Args...> fmt,
  78. Args&&... args);
  79. template<class... Args>
  80. size_t formatted_size(format-string<Args...> fmt, Args&&... args);
  81. template<class... Args>
  82. size_t formatted_size(wformat-string<Args...> fmt, Args&&... args);
  83. template<class... Args>
  84. size_t formatted_size(const locale& loc, format-string<Args...> fmt, Args&&... args);
  85. template<class... Args>
  86. size_t formatted_size(const locale& loc, wformat-string<Args...> fmt, Args&&... args);
  87. // [format.formatter], formatter
  88. template<class T, class charT = char> struct formatter;
  89. // [format.parse.ctx], class template basic_format_parse_context
  90. template<class charT> class basic_format_parse_context;
  91. using format_parse_context = basic_format_parse_context<char>;
  92. using wformat_parse_context = basic_format_parse_context<wchar_t>;
  93. // [format.arguments], arguments
  94. // [format.arg], class template basic_format_arg
  95. template<class Context> class basic_format_arg;
  96. template<class Visitor, class Context>
  97. see below visit_format_arg(Visitor&& vis, basic_format_arg<Context> arg);
  98. // [format.arg.store], class template format-arg-store
  99. template<class Context, class... Args> struct format-arg-store; // exposition only
  100. template<class Context = format_context, class... Args>
  101. format-arg-store<Context, Args...>
  102. make_format_args(Args&&... args);
  103. template<class... Args>
  104. format-arg-store<wformat_context, Args...>
  105. make_wformat_args(Args&&... args);
  106. // [format.error], class format_error
  107. class format_error;
  108. }
  109. */
  110. #include <__assert> // all public C++ headers provide the assertion handler
  111. // Make sure all feature-test macros are available.
  112. #include <version>
  113. // Enable the contents of the header only when libc++ was built with LIBCXX_ENABLE_INCOMPLETE_FEATURES.
  114. #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_FORMAT)
  115. #include <__algorithm/clamp.h>
  116. #include <__config>
  117. #include <__debug>
  118. #include <__format/buffer.h>
  119. #include <__format/concepts.h>
  120. #include <__format/enable_insertable.h>
  121. #include <__format/format_arg.h>
  122. #include <__format/format_arg_store.h>
  123. #include <__format/format_args.h>
  124. #include <__format/format_context.h>
  125. #include <__format/format_error.h>
  126. #include <__format/format_fwd.h>
  127. #include <__format/format_parse_context.h>
  128. #include <__format/format_string.h>
  129. #include <__format/format_to_n_result.h>
  130. #include <__format/formatter.h>
  131. #include <__format/formatter_bool.h>
  132. #include <__format/formatter_char.h>
  133. #include <__format/formatter_floating_point.h>
  134. #include <__format/formatter_integer.h>
  135. #include <__format/formatter_pointer.h>
  136. #include <__format/formatter_string.h>
  137. #include <__format/parser_std_format_spec.h>
  138. #include <__iterator/back_insert_iterator.h>
  139. #include <__iterator/incrementable_traits.h>
  140. #include <__variant/monostate.h>
  141. #include <array>
  142. #include <concepts>
  143. #include <string>
  144. #include <string_view>
  145. #include <type_traits>
  146. #ifndef _LIBCPP_HAS_NO_LOCALIZATION
  147. #include <locale>
  148. #endif
  149. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  150. # pragma GCC system_header
  151. #endif
  152. _LIBCPP_BEGIN_NAMESPACE_STD
  153. #if _LIBCPP_STD_VER > 17
  154. // TODO FMT Move the implementation in this file to its own granular headers.
  155. // TODO FMT Evaluate which templates should be external templates. This
  156. // improves the efficiency of the header. However since the header is still
  157. // under heavy development and not all classes are stable it makes no sense
  158. // to do this optimization now.
  159. using format_args = basic_format_args<format_context>;
  160. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  161. using wformat_args = basic_format_args<wformat_context>;
  162. #endif
  163. template <class _Context = format_context, class... _Args>
  164. _LIBCPP_HIDE_FROM_ABI __format_arg_store<_Context, _Args...> make_format_args(_Args&&... __args) {
  165. return _VSTD::__format_arg_store<_Context, _Args...>(__args...);
  166. }
  167. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  168. template <class... _Args>
  169. _LIBCPP_HIDE_FROM_ABI __format_arg_store<wformat_context, _Args...> make_wformat_args(_Args&&... __args) {
  170. return _VSTD::__format_arg_store<wformat_context, _Args...>(__args...);
  171. }
  172. #endif
  173. namespace __format {
  174. /// Helper class parse and handle argument.
  175. ///
  176. /// When parsing a handle which is not enabled the code is ill-formed.
  177. /// This helper uses the parser of the appropriate formatter for the stored type.
  178. template <class _CharT>
  179. class _LIBCPP_TEMPLATE_VIS __compile_time_handle {
  180. public:
  181. _LIBCPP_HIDE_FROM_ABI
  182. constexpr void __parse(basic_format_parse_context<_CharT>& __parse_ctx) const { __parse_(__parse_ctx); }
  183. template <class _Tp>
  184. _LIBCPP_HIDE_FROM_ABI constexpr void __enable() {
  185. __parse_ = [](basic_format_parse_context<_CharT>& __parse_ctx) {
  186. formatter<_Tp, _CharT> __f;
  187. __parse_ctx.advance_to(__f.parse(__parse_ctx));
  188. };
  189. }
  190. // Before calling __parse the proper handler needs to be set with __enable.
  191. // The default handler isn't a core constant expression.
  192. _LIBCPP_HIDE_FROM_ABI constexpr __compile_time_handle()
  193. : __parse_([](basic_format_parse_context<_CharT>&) { __throw_format_error("Not a handle"); }) {}
  194. private:
  195. void (*__parse_)(basic_format_parse_context<_CharT>&);
  196. };
  197. // Dummy format_context only providing the parts used during constant
  198. // validation of the basic-format-string.
  199. template <class _CharT>
  200. struct _LIBCPP_TEMPLATE_VIS __compile_time_basic_format_context {
  201. public:
  202. using char_type = _CharT;
  203. _LIBCPP_HIDE_FROM_ABI constexpr explicit __compile_time_basic_format_context(
  204. const __arg_t* __args, const __compile_time_handle<_CharT>* __handles, size_t __size)
  205. : __args_(__args), __handles_(__handles), __size_(__size) {}
  206. // During the compile-time validation nothing needs to be written.
  207. // Therefore all operations of this iterator are a NOP.
  208. struct iterator {
  209. _LIBCPP_HIDE_FROM_ABI constexpr iterator& operator=(_CharT) { return *this; }
  210. _LIBCPP_HIDE_FROM_ABI constexpr iterator& operator*() { return *this; }
  211. _LIBCPP_HIDE_FROM_ABI constexpr iterator operator++(int) { return *this; }
  212. };
  213. _LIBCPP_HIDE_FROM_ABI constexpr __arg_t arg(size_t __id) const {
  214. if (__id >= __size_)
  215. __throw_format_error("Argument index out of bounds");
  216. return __args_[__id];
  217. }
  218. _LIBCPP_HIDE_FROM_ABI constexpr const __compile_time_handle<_CharT>& __handle(size_t __id) const {
  219. if (__id >= __size_)
  220. __throw_format_error("Argument index out of bounds");
  221. return __handles_[__id];
  222. }
  223. _LIBCPP_HIDE_FROM_ABI constexpr iterator out() { return {}; }
  224. _LIBCPP_HIDE_FROM_ABI constexpr void advance_to(iterator) {}
  225. private:
  226. const __arg_t* __args_;
  227. const __compile_time_handle<_CharT>* __handles_;
  228. size_t __size_;
  229. };
  230. _LIBCPP_HIDE_FROM_ABI
  231. constexpr void __compile_time_validate_integral(__arg_t __type) {
  232. switch (__type) {
  233. case __arg_t::__int:
  234. case __arg_t::__long_long:
  235. case __arg_t::__i128:
  236. case __arg_t::__unsigned:
  237. case __arg_t::__unsigned_long_long:
  238. case __arg_t::__u128:
  239. return;
  240. default:
  241. __throw_format_error("Argument isn't an integral type");
  242. }
  243. }
  244. // _HasPrecision does the formatter have a precision?
  245. template <class _CharT, class _Tp, bool _HasPrecision = false>
  246. _LIBCPP_HIDE_FROM_ABI constexpr void
  247. __compile_time_validate_argument(basic_format_parse_context<_CharT>& __parse_ctx,
  248. __compile_time_basic_format_context<_CharT>& __ctx) {
  249. formatter<_Tp, _CharT> __formatter;
  250. __parse_ctx.advance_to(__formatter.parse(__parse_ctx));
  251. // [format.string.std]/7
  252. // ... If the corresponding formatting argument is not of integral type, or
  253. // its value is negative for precision or non-positive for width, an
  254. // exception of type format_error is thrown.
  255. //
  256. // Validate whether the arguments are integrals.
  257. if constexpr (requires(formatter<_Tp, _CharT> __f) { __f.__width_needs_substitution(); }) {
  258. // TODO FMT Remove this when parser v1 has been phased out.
  259. if (__formatter.__width_needs_substitution())
  260. __format::__compile_time_validate_integral(__ctx.arg(__formatter.__width));
  261. if constexpr (_HasPrecision)
  262. if (__formatter.__precision_needs_substitution())
  263. __format::__compile_time_validate_integral(__ctx.arg(__formatter.__precision));
  264. } else {
  265. if (__formatter.__parser_.__width_as_arg_)
  266. __format::__compile_time_validate_integral(__ctx.arg(__formatter.__parser_.__width_));
  267. if constexpr (_HasPrecision)
  268. if (__formatter.__parser_.__precision_as_arg_)
  269. __format::__compile_time_validate_integral(__ctx.arg(__formatter.__parser_.__precision_));
  270. }
  271. }
  272. template <class _CharT>
  273. _LIBCPP_HIDE_FROM_ABI constexpr void __compile_time_visit_format_arg(basic_format_parse_context<_CharT>& __parse_ctx,
  274. __compile_time_basic_format_context<_CharT>& __ctx,
  275. __arg_t __type) {
  276. switch (__type) {
  277. case __arg_t::__none:
  278. __throw_format_error("Invalid argument");
  279. case __arg_t::__boolean:
  280. return __format::__compile_time_validate_argument<_CharT, bool>(__parse_ctx, __ctx);
  281. case __arg_t::__char_type:
  282. return __format::__compile_time_validate_argument<_CharT, _CharT>(__parse_ctx, __ctx);
  283. case __arg_t::__int:
  284. return __format::__compile_time_validate_argument<_CharT, int>(__parse_ctx, __ctx);
  285. case __arg_t::__long_long:
  286. return __format::__compile_time_validate_argument<_CharT, long long>(__parse_ctx, __ctx);
  287. case __arg_t::__i128:
  288. # ifndef _LIBCPP_HAS_NO_INT128
  289. return __format::__compile_time_validate_argument<_CharT, __int128_t>(__parse_ctx, __ctx);
  290. # else
  291. __throw_format_error("Invalid argument");
  292. # endif
  293. return;
  294. case __arg_t::__unsigned:
  295. return __format::__compile_time_validate_argument<_CharT, unsigned>(__parse_ctx, __ctx);
  296. case __arg_t::__unsigned_long_long:
  297. return __format::__compile_time_validate_argument<_CharT, unsigned long long>(__parse_ctx, __ctx);
  298. case __arg_t::__u128:
  299. # ifndef _LIBCPP_HAS_NO_INT128
  300. return __format::__compile_time_validate_argument<_CharT, __uint128_t>(__parse_ctx, __ctx);
  301. # else
  302. __throw_format_error("Invalid argument");
  303. # endif
  304. return;
  305. case __arg_t::__float:
  306. return __format::__compile_time_validate_argument<_CharT, float, true>(__parse_ctx, __ctx);
  307. case __arg_t::__double:
  308. return __format::__compile_time_validate_argument<_CharT, double, true>(__parse_ctx, __ctx);
  309. case __arg_t::__long_double:
  310. return __format::__compile_time_validate_argument<_CharT, long double, true>(__parse_ctx, __ctx);
  311. case __arg_t::__const_char_type_ptr:
  312. return __format::__compile_time_validate_argument<_CharT, const _CharT*, true>(__parse_ctx, __ctx);
  313. case __arg_t::__string_view:
  314. return __format::__compile_time_validate_argument<_CharT, basic_string_view<_CharT>, true>(__parse_ctx, __ctx);
  315. case __arg_t::__ptr:
  316. return __format::__compile_time_validate_argument<_CharT, const void*>(__parse_ctx, __ctx);
  317. case __arg_t::__handle:
  318. __throw_format_error("Handle should use __compile_time_validate_handle_argument");
  319. }
  320. __throw_format_error("Invalid argument");
  321. }
  322. template <class _CharT, class _ParseCtx, class _Ctx>
  323. _LIBCPP_HIDE_FROM_ABI constexpr const _CharT*
  324. __handle_replacement_field(const _CharT* __begin, const _CharT* __end,
  325. _ParseCtx& __parse_ctx, _Ctx& __ctx) {
  326. __format::__parse_number_result __r =
  327. __format::__parse_arg_id(__begin, __end, __parse_ctx);
  328. switch (*__r.__ptr) {
  329. case _CharT(':'):
  330. // The arg-id has a format-specifier, advance the input to the format-spec.
  331. __parse_ctx.advance_to(__r.__ptr + 1);
  332. break;
  333. case _CharT('}'):
  334. // The arg-id has no format-specifier.
  335. __parse_ctx.advance_to(__r.__ptr);
  336. break;
  337. default:
  338. __throw_format_error(
  339. "The replacement field arg-id should terminate at a ':' or '}'");
  340. }
  341. if constexpr (same_as<_Ctx, __compile_time_basic_format_context<_CharT>>) {
  342. __arg_t __type = __ctx.arg(__r.__value);
  343. if (__type == __arg_t::__handle)
  344. __ctx.__handle(__r.__value).__parse(__parse_ctx);
  345. else
  346. __format::__compile_time_visit_format_arg(__parse_ctx, __ctx, __type);
  347. } else
  348. _VSTD::visit_format_arg(
  349. [&](auto __arg) {
  350. if constexpr (same_as<decltype(__arg), monostate>)
  351. __throw_format_error("Argument index out of bounds");
  352. else if constexpr (same_as<decltype(__arg), typename basic_format_arg<_Ctx>::handle>)
  353. __arg.format(__parse_ctx, __ctx);
  354. else {
  355. formatter<decltype(__arg), _CharT> __formatter;
  356. __parse_ctx.advance_to(__formatter.parse(__parse_ctx));
  357. __ctx.advance_to(__formatter.format(__arg, __ctx));
  358. }
  359. },
  360. __ctx.arg(__r.__value));
  361. __begin = __parse_ctx.begin();
  362. if (__begin == __end || *__begin != _CharT('}'))
  363. __throw_format_error("The replacement field misses a terminating '}'");
  364. return ++__begin;
  365. }
  366. template <class _ParseCtx, class _Ctx>
  367. _LIBCPP_HIDE_FROM_ABI constexpr typename _Ctx::iterator
  368. __vformat_to(_ParseCtx&& __parse_ctx, _Ctx&& __ctx) {
  369. using _CharT = typename _ParseCtx::char_type;
  370. static_assert(same_as<typename _Ctx::char_type, _CharT>);
  371. const _CharT* __begin = __parse_ctx.begin();
  372. const _CharT* __end = __parse_ctx.end();
  373. typename _Ctx::iterator __out_it = __ctx.out();
  374. while (__begin != __end) {
  375. switch (*__begin) {
  376. case _CharT('{'):
  377. ++__begin;
  378. if (__begin == __end)
  379. __throw_format_error("The format string terminates at a '{'");
  380. if (*__begin != _CharT('{')) [[likely]] {
  381. __ctx.advance_to(_VSTD::move(__out_it));
  382. __begin =
  383. __handle_replacement_field(__begin, __end, __parse_ctx, __ctx);
  384. __out_it = __ctx.out();
  385. // The output is written and __begin points to the next character. So
  386. // start the next iteration.
  387. continue;
  388. }
  389. // The string is an escape character.
  390. break;
  391. case _CharT('}'):
  392. ++__begin;
  393. if (__begin == __end || *__begin != _CharT('}'))
  394. __throw_format_error(
  395. "The format string contains an invalid escape sequence");
  396. break;
  397. }
  398. // Copy the character to the output verbatim.
  399. *__out_it++ = *__begin++;
  400. }
  401. return __out_it;
  402. }
  403. } // namespace __format
  404. template <class _CharT, class... _Args>
  405. struct _LIBCPP_TEMPLATE_VIS __basic_format_string {
  406. basic_string_view<_CharT> __str_;
  407. template <class _Tp>
  408. requires convertible_to<const _Tp&, basic_string_view<_CharT>>
  409. consteval __basic_format_string(const _Tp& __str) : __str_{__str} {
  410. __format::__vformat_to(basic_format_parse_context<_CharT>{__str_, sizeof...(_Args)},
  411. _Context{__types_.data(), __handles_.data(), sizeof...(_Args)});
  412. }
  413. private:
  414. using _Context = __format::__compile_time_basic_format_context<_CharT>;
  415. static constexpr array<__format::__arg_t, sizeof...(_Args)> __types_{
  416. __format::__determine_arg_t<_Context, remove_cvref_t<_Args>>()...};
  417. // TODO FMT remove this work-around when the AIX ICE has been resolved.
  418. # if defined(_AIX) && defined(_LIBCPP_CLANG_VER) && _LIBCPP_CLANG_VER < 1400
  419. template <class _Tp>
  420. static constexpr __format::__compile_time_handle<_CharT> __get_handle() {
  421. __format::__compile_time_handle<_CharT> __handle;
  422. if (__format::__determine_arg_t<_Context, _Tp>() == __format::__arg_t::__handle)
  423. __handle.template __enable<_Tp>();
  424. return __handle;
  425. }
  426. static constexpr array<__format::__compile_time_handle<_CharT>, sizeof...(_Args)> __handles_{
  427. __get_handle<_Args>()...};
  428. # else
  429. static constexpr array<__format::__compile_time_handle<_CharT>, sizeof...(_Args)> __handles_{[] {
  430. using _Tp = remove_cvref_t<_Args>;
  431. __format::__compile_time_handle<_CharT> __handle;
  432. if (__format::__determine_arg_t<_Context, _Tp>() == __format::__arg_t::__handle)
  433. __handle.template __enable<_Tp>();
  434. return __handle;
  435. }()...};
  436. # endif
  437. };
  438. template <class... _Args>
  439. using __format_string_t = __basic_format_string<char, type_identity_t<_Args>...>;
  440. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  441. template <class... _Args>
  442. using __wformat_string_t = __basic_format_string<wchar_t, type_identity_t<_Args>...>;
  443. #endif
  444. template <class _OutIt, class _CharT, class _FormatOutIt>
  445. requires(output_iterator<_OutIt, const _CharT&>) _LIBCPP_HIDE_FROM_ABI _OutIt
  446. __vformat_to(
  447. _OutIt __out_it, basic_string_view<_CharT> __fmt,
  448. basic_format_args<basic_format_context<_FormatOutIt, _CharT>> __args) {
  449. if constexpr (same_as<_OutIt, _FormatOutIt>)
  450. return _VSTD::__format::__vformat_to(
  451. basic_format_parse_context{__fmt, __args.__size()},
  452. _VSTD::__format_context_create(_VSTD::move(__out_it), __args));
  453. else {
  454. __format::__format_buffer<_OutIt, _CharT> __buffer{_VSTD::move(__out_it)};
  455. _VSTD::__format::__vformat_to(
  456. basic_format_parse_context{__fmt, __args.__size()},
  457. _VSTD::__format_context_create(__buffer.make_output_iterator(),
  458. __args));
  459. return _VSTD::move(__buffer).out();
  460. }
  461. }
  462. // The function is _LIBCPP_ALWAYS_INLINE since the compiler is bad at inlining
  463. // https://reviews.llvm.org/D110499#inline-1180704
  464. // TODO FMT Evaluate whether we want to file a Clang bug report regarding this.
  465. template <output_iterator<const char&> _OutIt>
  466. _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT _OutIt
  467. vformat_to(_OutIt __out_it, string_view __fmt, format_args __args) {
  468. return _VSTD::__vformat_to(_VSTD::move(__out_it), __fmt, __args);
  469. }
  470. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  471. template <output_iterator<const wchar_t&> _OutIt>
  472. _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT _OutIt
  473. vformat_to(_OutIt __out_it, wstring_view __fmt, wformat_args __args) {
  474. return _VSTD::__vformat_to(_VSTD::move(__out_it), __fmt, __args);
  475. }
  476. #endif
  477. template <output_iterator<const char&> _OutIt, class... _Args>
  478. _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT _OutIt
  479. format_to(_OutIt __out_it, __format_string_t<_Args...> __fmt, _Args&&... __args) {
  480. return _VSTD::vformat_to(_VSTD::move(__out_it), __fmt.__str_,
  481. _VSTD::make_format_args(__args...));
  482. }
  483. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  484. template <output_iterator<const wchar_t&> _OutIt, class... _Args>
  485. _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT _OutIt
  486. format_to(_OutIt __out_it, __wformat_string_t<_Args...> __fmt, _Args&&... __args) {
  487. return _VSTD::vformat_to(_VSTD::move(__out_it), __fmt.__str_,
  488. _VSTD::make_wformat_args(__args...));
  489. }
  490. #endif
  491. _LIBCPP_ALWAYS_INLINE inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT string
  492. vformat(string_view __fmt, format_args __args) {
  493. string __res;
  494. _VSTD::vformat_to(_VSTD::back_inserter(__res), __fmt, __args);
  495. return __res;
  496. }
  497. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  498. _LIBCPP_ALWAYS_INLINE inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT wstring
  499. vformat(wstring_view __fmt, wformat_args __args) {
  500. wstring __res;
  501. _VSTD::vformat_to(_VSTD::back_inserter(__res), __fmt, __args);
  502. return __res;
  503. }
  504. #endif
  505. template <class... _Args>
  506. _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT string format(__format_string_t<_Args...> __fmt,
  507. _Args&&... __args) {
  508. return _VSTD::vformat(__fmt.__str_, _VSTD::make_format_args(__args...));
  509. }
  510. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  511. template <class... _Args>
  512. _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT wstring
  513. format(__wformat_string_t<_Args...> __fmt, _Args&&... __args) {
  514. return _VSTD::vformat(__fmt.__str_, _VSTD::make_wformat_args(__args...));
  515. }
  516. #endif
  517. template <class _Context, class _OutIt, class _CharT>
  518. _LIBCPP_HIDE_FROM_ABI format_to_n_result<_OutIt> __vformat_to_n(_OutIt __out_it, iter_difference_t<_OutIt> __n,
  519. basic_string_view<_CharT> __fmt,
  520. basic_format_args<_Context> __args) {
  521. __format::__format_to_n_buffer<_OutIt, _CharT> __buffer{_VSTD::move(__out_it), __n};
  522. _VSTD::__format::__vformat_to(basic_format_parse_context{__fmt, __args.__size()},
  523. _VSTD::__format_context_create(__buffer.make_output_iterator(), __args));
  524. return _VSTD::move(__buffer).result();
  525. }
  526. template <output_iterator<const char&> _OutIt, class... _Args>
  527. _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT format_to_n_result<_OutIt>
  528. format_to_n(_OutIt __out_it, iter_difference_t<_OutIt> __n, __format_string_t<_Args...> __fmt, _Args&&... __args) {
  529. return _VSTD::__vformat_to_n<format_context>(_VSTD::move(__out_it), __n, __fmt.__str_, _VSTD::make_format_args(__args...));
  530. }
  531. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  532. template <output_iterator<const wchar_t&> _OutIt, class... _Args>
  533. _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT format_to_n_result<_OutIt>
  534. format_to_n(_OutIt __out_it, iter_difference_t<_OutIt> __n, __wformat_string_t<_Args...> __fmt,
  535. _Args&&... __args) {
  536. return _VSTD::__vformat_to_n<wformat_context>(_VSTD::move(__out_it), __n, __fmt.__str_, _VSTD::make_wformat_args(__args...));
  537. }
  538. #endif
  539. template <class _CharT>
  540. _LIBCPP_HIDE_FROM_ABI size_t __vformatted_size(basic_string_view<_CharT> __fmt, auto __args) {
  541. __format::__formatted_size_buffer<_CharT> __buffer;
  542. _VSTD::__format::__vformat_to(basic_format_parse_context{__fmt, __args.__size()},
  543. _VSTD::__format_context_create(__buffer.make_output_iterator(), __args));
  544. return _VSTD::move(__buffer).result();
  545. }
  546. template <class... _Args>
  547. _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT size_t
  548. formatted_size(__format_string_t<_Args...> __fmt, _Args&&... __args) {
  549. return _VSTD::__vformatted_size(__fmt.__str_, basic_format_args{_VSTD::make_format_args(__args...)});
  550. }
  551. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  552. template <class... _Args>
  553. _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT size_t
  554. formatted_size(__wformat_string_t<_Args...> __fmt, _Args&&... __args) {
  555. return _VSTD::__vformatted_size(__fmt.__str_, basic_format_args{_VSTD::make_wformat_args(__args...)});
  556. }
  557. #endif
  558. #ifndef _LIBCPP_HAS_NO_LOCALIZATION
  559. template <class _OutIt, class _CharT, class _FormatOutIt>
  560. requires(output_iterator<_OutIt, const _CharT&>) _LIBCPP_HIDE_FROM_ABI _OutIt
  561. __vformat_to(
  562. _OutIt __out_it, locale __loc, basic_string_view<_CharT> __fmt,
  563. basic_format_args<basic_format_context<_FormatOutIt, _CharT>> __args) {
  564. if constexpr (same_as<_OutIt, _FormatOutIt>)
  565. return _VSTD::__format::__vformat_to(
  566. basic_format_parse_context{__fmt, __args.__size()},
  567. _VSTD::__format_context_create(_VSTD::move(__out_it), __args,
  568. _VSTD::move(__loc)));
  569. else {
  570. __format::__format_buffer<_OutIt, _CharT> __buffer{_VSTD::move(__out_it)};
  571. _VSTD::__format::__vformat_to(
  572. basic_format_parse_context{__fmt, __args.__size()},
  573. _VSTD::__format_context_create(__buffer.make_output_iterator(),
  574. __args, _VSTD::move(__loc)));
  575. return _VSTD::move(__buffer).out();
  576. }
  577. }
  578. template <output_iterator<const char&> _OutIt>
  579. _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT _OutIt vformat_to(
  580. _OutIt __out_it, locale __loc, string_view __fmt, format_args __args) {
  581. return _VSTD::__vformat_to(_VSTD::move(__out_it), _VSTD::move(__loc), __fmt,
  582. __args);
  583. }
  584. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  585. template <output_iterator<const wchar_t&> _OutIt>
  586. _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT _OutIt vformat_to(
  587. _OutIt __out_it, locale __loc, wstring_view __fmt, wformat_args __args) {
  588. return _VSTD::__vformat_to(_VSTD::move(__out_it), _VSTD::move(__loc), __fmt,
  589. __args);
  590. }
  591. #endif
  592. template <output_iterator<const char&> _OutIt, class... _Args>
  593. _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT _OutIt
  594. format_to(_OutIt __out_it, locale __loc, __format_string_t<_Args...> __fmt, _Args&&... __args) {
  595. return _VSTD::vformat_to(_VSTD::move(__out_it), _VSTD::move(__loc), __fmt.__str_,
  596. _VSTD::make_format_args(__args...));
  597. }
  598. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  599. template <output_iterator<const wchar_t&> _OutIt, class... _Args>
  600. _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT _OutIt
  601. format_to(_OutIt __out_it, locale __loc, __wformat_string_t<_Args...> __fmt, _Args&&... __args) {
  602. return _VSTD::vformat_to(_VSTD::move(__out_it), _VSTD::move(__loc), __fmt.__str_,
  603. _VSTD::make_wformat_args(__args...));
  604. }
  605. #endif
  606. _LIBCPP_ALWAYS_INLINE inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT string
  607. vformat(locale __loc, string_view __fmt, format_args __args) {
  608. string __res;
  609. _VSTD::vformat_to(_VSTD::back_inserter(__res), _VSTD::move(__loc), __fmt,
  610. __args);
  611. return __res;
  612. }
  613. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  614. _LIBCPP_ALWAYS_INLINE inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT wstring
  615. vformat(locale __loc, wstring_view __fmt, wformat_args __args) {
  616. wstring __res;
  617. _VSTD::vformat_to(_VSTD::back_inserter(__res), _VSTD::move(__loc), __fmt,
  618. __args);
  619. return __res;
  620. }
  621. #endif
  622. template <class... _Args>
  623. _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT string format(locale __loc,
  624. __format_string_t<_Args...> __fmt,
  625. _Args&&... __args) {
  626. return _VSTD::vformat(_VSTD::move(__loc), __fmt.__str_,
  627. _VSTD::make_format_args(__args...));
  628. }
  629. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  630. template <class... _Args>
  631. _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT wstring
  632. format(locale __loc, __wformat_string_t<_Args...> __fmt, _Args&&... __args) {
  633. return _VSTD::vformat(_VSTD::move(__loc), __fmt.__str_,
  634. _VSTD::make_wformat_args(__args...));
  635. }
  636. #endif
  637. template <class _Context, class _OutIt, class _CharT>
  638. _LIBCPP_HIDE_FROM_ABI format_to_n_result<_OutIt> __vformat_to_n(_OutIt __out_it, iter_difference_t<_OutIt> __n,
  639. locale __loc, basic_string_view<_CharT> __fmt,
  640. basic_format_args<_Context> __args) {
  641. __format::__format_to_n_buffer<_OutIt, _CharT> __buffer{_VSTD::move(__out_it), __n};
  642. _VSTD::__format::__vformat_to(
  643. basic_format_parse_context{__fmt, __args.__size()},
  644. _VSTD::__format_context_create(__buffer.make_output_iterator(), __args, _VSTD::move(__loc)));
  645. return _VSTD::move(__buffer).result();
  646. }
  647. template <output_iterator<const char&> _OutIt, class... _Args>
  648. _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT format_to_n_result<_OutIt>
  649. format_to_n(_OutIt __out_it, iter_difference_t<_OutIt> __n, locale __loc, __format_string_t<_Args...> __fmt,
  650. _Args&&... __args) {
  651. return _VSTD::__vformat_to_n<format_context>(_VSTD::move(__out_it), __n, _VSTD::move(__loc), __fmt.__str_,
  652. _VSTD::make_format_args(__args...));
  653. }
  654. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  655. template <output_iterator<const wchar_t&> _OutIt, class... _Args>
  656. _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT format_to_n_result<_OutIt>
  657. format_to_n(_OutIt __out_it, iter_difference_t<_OutIt> __n, locale __loc, __wformat_string_t<_Args...> __fmt,
  658. _Args&&... __args) {
  659. return _VSTD::__vformat_to_n<wformat_context>(_VSTD::move(__out_it), __n, _VSTD::move(__loc), __fmt.__str_,
  660. _VSTD::make_wformat_args(__args...));
  661. }
  662. #endif
  663. template <class _CharT>
  664. _LIBCPP_HIDE_FROM_ABI size_t __vformatted_size(locale __loc, basic_string_view<_CharT> __fmt, auto __args) {
  665. __format::__formatted_size_buffer<_CharT> __buffer;
  666. _VSTD::__format::__vformat_to(
  667. basic_format_parse_context{__fmt, __args.__size()},
  668. _VSTD::__format_context_create(__buffer.make_output_iterator(), __args, _VSTD::move(__loc)));
  669. return _VSTD::move(__buffer).result();
  670. }
  671. template <class... _Args>
  672. _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT size_t
  673. formatted_size(locale __loc, __format_string_t<_Args...> __fmt, _Args&&... __args) {
  674. return _VSTD::__vformatted_size(_VSTD::move(__loc), __fmt.__str_, basic_format_args{_VSTD::make_format_args(__args...)});
  675. }
  676. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  677. template <class... _Args>
  678. _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT size_t
  679. formatted_size(locale __loc, __wformat_string_t<_Args...> __fmt, _Args&&... __args) {
  680. return _VSTD::__vformatted_size(_VSTD::move(__loc), __fmt.__str_, basic_format_args{_VSTD::make_wformat_args(__args...)});
  681. }
  682. #endif
  683. #endif // _LIBCPP_HAS_NO_LOCALIZATION
  684. #endif //_LIBCPP_STD_VER > 17
  685. _LIBCPP_END_NAMESPACE_STD
  686. #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_FORMAT)
  687. #endif // _LIBCPP_FORMAT