format 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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.functions], formatting functions
  22. template<class... Args>
  23. string format(string_view fmt, const Args&... args);
  24. template<class... Args>
  25. wstring format(wstring_view fmt, const Args&... args);
  26. template<class... Args>
  27. string format(const locale& loc, string_view fmt, const Args&... args);
  28. template<class... Args>
  29. wstring format(const locale& loc, wstring_view fmt, const Args&... args);
  30. string vformat(string_view fmt, format_args args);
  31. wstring vformat(wstring_view fmt, wformat_args args);
  32. string vformat(const locale& loc, string_view fmt, format_args args);
  33. wstring vformat(const locale& loc, wstring_view fmt, wformat_args args);
  34. template<class Out, class... Args>
  35. Out format_to(Out out, string_view fmt, const Args&... args);
  36. template<class Out, class... Args>
  37. Out format_to(Out out, wstring_view fmt, const Args&... args);
  38. template<class Out, class... Args>
  39. Out format_to(Out out, const locale& loc, string_view fmt, const Args&... args);
  40. template<class Out, class... Args>
  41. Out format_to(Out out, const locale& loc, wstring_view fmt, const Args&... args);
  42. template<class Out>
  43. Out vformat_to(Out out, string_view fmt, format_args args);
  44. template<class Out>
  45. Out vformat_to(Out out, wstring_view fmt, wformat_args args);
  46. template<class Out>
  47. Out vformat_to(Out out, const locale& loc, string_view fmt,
  48. format_args char> args);
  49. template<class Out>
  50. Out vformat_to(Out out, const locale& loc, wstring_view fmt,
  51. wformat_args args);
  52. template<class Out> struct format_to_n_result {
  53. Out out;
  54. iter_difference_t<Out> size;
  55. };
  56. template<class Out, class... Args>
  57. format_to_n_result<Out> format_to_n(Out out, iter_difference_t<Out> n,
  58. string_view fmt, const Args&... args);
  59. template<class Out, class... Args>
  60. format_to_n_result<Out> format_to_n(Out out, iter_difference_t<Out> n,
  61. wstring_view fmt, const Args&... args);
  62. template<class Out, class... Args>
  63. format_to_n_result<Out> format_to_n(Out out, iter_difference_t<Out> n,
  64. const locale& loc, string_view fmt,
  65. const Args&... args);
  66. template<class Out, class... Args>
  67. format_to_n_result<Out> format_to_n(Out out, iter_difference_t<Out> n,
  68. const locale& loc, wstring_view fmt,
  69. const Args&... args);
  70. template<class... Args>
  71. size_t formatted_size(string_view fmt, const Args&... args);
  72. template<class... Args>
  73. size_t formatted_size(wstring_view fmt, const Args&... args);
  74. template<class... Args>
  75. size_t formatted_size(const locale& loc, string_view fmt, const Args&... args);
  76. template<class... Args>
  77. size_t formatted_size(const locale& loc, wstring_view fmt, const Args&... args);
  78. // [format.formatter], formatter
  79. template<class T, class charT = char> struct formatter;
  80. // [format.parse.ctx], class template basic_format_parse_context
  81. template<class charT> class basic_format_parse_context;
  82. using format_parse_context = basic_format_parse_context<char>;
  83. using wformat_parse_context = basic_format_parse_context<wchar_t>;
  84. // [format.arguments], arguments
  85. // [format.arg], class template basic_format_arg
  86. template<class Context> class basic_format_arg;
  87. template<class Visitor, class Context>
  88. see below visit_format_arg(Visitor&& vis, basic_format_arg<Context> arg);
  89. // [format.arg.store], class template format-arg-store
  90. template<class Context, class... Args> struct format-arg-store; // exposition only
  91. template<class Context = format_context, class... Args>
  92. format-arg-store<Context, Args...>
  93. make_format_args(const Args&... args);
  94. template<class... Args>
  95. format-arg-store<wformat_context, Args...>
  96. make_wformat_args(const Args&... args);
  97. // [format.error], class format_error
  98. class format_error;
  99. }
  100. */
  101. // Make sure all feature-test macros are available.
  102. #include <version>
  103. // Enable the contents of the header only when libc++ was built with LIBCXX_ENABLE_INCOMPLETE_FEATURES.
  104. #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_FORMAT)
  105. #include <__algorithm/clamp.h>
  106. #include <__config>
  107. #include <__debug>
  108. #include <__format/format_arg.h>
  109. #include <__format/format_args.h>
  110. #include <__format/format_context.h>
  111. #include <__format/format_error.h>
  112. #include <__format/format_fwd.h>
  113. #include <__format/format_parse_context.h>
  114. #include <__format/format_string.h>
  115. #include <__format/format_to_n_result.h>
  116. #include <__format/formatter.h>
  117. #include <__format/formatter_bool.h>
  118. #include <__format/formatter_char.h>
  119. #include <__format/formatter_floating_point.h>
  120. #include <__format/formatter_integer.h>
  121. #include <__format/formatter_pointer.h>
  122. #include <__format/formatter_string.h>
  123. #include <__format/parser_std_format_spec.h>
  124. #include <__variant/monostate.h>
  125. #include <array>
  126. #include <concepts>
  127. #include <string>
  128. #include <string_view>
  129. #include <type_traits>
  130. #ifndef _LIBCPP_HAS_NO_LOCALIZATION
  131. #include <locale>
  132. #endif
  133. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  134. # pragma GCC system_header
  135. #endif
  136. _LIBCPP_BEGIN_NAMESPACE_STD
  137. #if _LIBCPP_STD_VER > 17
  138. // TODO FMT Remove this once we require compilers with proper C++20 support.
  139. // If the compiler has no concepts support, the format header will be disabled.
  140. // Without concepts support enable_if needs to be used and that too much effort
  141. // to support compilers with partial C++20 support.
  142. #if !defined(_LIBCPP_HAS_NO_CONCEPTS)
  143. // TODO FMT Move the implementation in this file to its own granular headers.
  144. // TODO FMT Evaluate which templates should be external templates. This
  145. // improves the efficiency of the header. However since the header is still
  146. // under heavy development and not all classes are stable it makes no sense
  147. // to do this optimization now.
  148. using format_args = basic_format_args<format_context>;
  149. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  150. using wformat_args = basic_format_args<wformat_context>;
  151. #endif
  152. template <class _Context, class... _Args>
  153. struct _LIBCPP_TEMPLATE_VIS __format_arg_store {
  154. // TODO FMT Use a built-in array.
  155. array<basic_format_arg<_Context>, sizeof...(_Args)> __args;
  156. };
  157. template <class _Context = format_context, class... _Args>
  158. _LIBCPP_HIDE_FROM_ABI __format_arg_store<_Context, _Args...>
  159. make_format_args(const _Args&... __args) {
  160. return {basic_format_arg<_Context>(__args)...};
  161. }
  162. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  163. template <class... _Args>
  164. _LIBCPP_HIDE_FROM_ABI __format_arg_store<wformat_context, _Args...>
  165. make_wformat_args(const _Args&... __args) {
  166. return _VSTD::make_format_args<wformat_context>(__args...);
  167. }
  168. #endif
  169. namespace __format {
  170. template <class _CharT, class _ParseCtx, class _Ctx>
  171. _LIBCPP_HIDE_FROM_ABI const _CharT*
  172. __handle_replacement_field(const _CharT* __begin, const _CharT* __end,
  173. _ParseCtx& __parse_ctx, _Ctx& __ctx) {
  174. __format::__parse_number_result __r =
  175. __format::__parse_arg_id(__begin, __end, __parse_ctx);
  176. switch (*__r.__ptr) {
  177. case _CharT(':'):
  178. // The arg-id has a format-specifier, advance the input to the format-spec.
  179. __parse_ctx.advance_to(__r.__ptr + 1);
  180. break;
  181. case _CharT('}'):
  182. // The arg-id has no format-specifier.
  183. __parse_ctx.advance_to(__r.__ptr);
  184. break;
  185. default:
  186. __throw_format_error(
  187. "The replacement field arg-id should terminate at a ':' or '}'");
  188. }
  189. _VSTD::visit_format_arg(
  190. [&](auto __arg) {
  191. if constexpr (same_as<decltype(__arg), monostate>)
  192. __throw_format_error("Argument index out of bounds");
  193. else if constexpr (same_as<decltype(__arg), typename basic_format_arg<_Ctx>::handle>)
  194. __arg.format(__parse_ctx, __ctx);
  195. else {
  196. formatter<decltype(__arg), _CharT> __formatter;
  197. __parse_ctx.advance_to(__formatter.parse(__parse_ctx));
  198. __ctx.advance_to(__formatter.format(__arg, __ctx));
  199. }
  200. },
  201. __ctx.arg(__r.__value));
  202. __begin = __parse_ctx.begin();
  203. if (__begin == __end || *__begin != _CharT('}'))
  204. __throw_format_error("The replacement field misses a terminating '}'");
  205. return ++__begin;
  206. }
  207. template <class _ParseCtx, class _Ctx>
  208. _LIBCPP_HIDE_FROM_ABI typename _Ctx::iterator
  209. __vformat_to(_ParseCtx&& __parse_ctx, _Ctx&& __ctx) {
  210. using _CharT = typename _ParseCtx::char_type;
  211. static_assert(same_as<typename _Ctx::char_type, _CharT>);
  212. const _CharT* __begin = __parse_ctx.begin();
  213. const _CharT* __end = __parse_ctx.end();
  214. typename _Ctx::iterator __out_it = __ctx.out();
  215. while (__begin != __end) {
  216. switch (*__begin) {
  217. case _CharT('{'):
  218. ++__begin;
  219. if (__begin == __end)
  220. __throw_format_error("The format string terminates at a '{'");
  221. if (*__begin != _CharT('{')) [[likely]] {
  222. __ctx.advance_to(_VSTD::move(__out_it));
  223. __begin =
  224. __handle_replacement_field(__begin, __end, __parse_ctx, __ctx);
  225. __out_it = __ctx.out();
  226. // The output is written and __begin points to the next character. So
  227. // start the next iteration.
  228. continue;
  229. }
  230. // The string is an escape character.
  231. break;
  232. case _CharT('}'):
  233. ++__begin;
  234. if (__begin == __end || *__begin != _CharT('}'))
  235. __throw_format_error(
  236. "The format string contains an invalid escape sequence");
  237. break;
  238. }
  239. // Copy the character to the output verbatim.
  240. *__out_it++ = *__begin++;
  241. }
  242. return __out_it;
  243. }
  244. } // namespace __format
  245. template <class _OutIt, class _CharT, class _FormatOutIt>
  246. requires(output_iterator<_OutIt, const _CharT&>) _LIBCPP_HIDE_FROM_ABI _OutIt
  247. __vformat_to(
  248. _OutIt __out_it, basic_string_view<_CharT> __fmt,
  249. basic_format_args<basic_format_context<_FormatOutIt, _CharT>> __args) {
  250. if constexpr (same_as<_OutIt, _FormatOutIt>)
  251. return _VSTD::__format::__vformat_to(
  252. basic_format_parse_context{__fmt, __args.__size()},
  253. _VSTD::__format_context_create(_VSTD::move(__out_it), __args));
  254. else {
  255. basic_string<_CharT> __str;
  256. _VSTD::__format::__vformat_to(
  257. basic_format_parse_context{__fmt, __args.__size()},
  258. _VSTD::__format_context_create(_VSTD::back_inserter(__str), __args));
  259. return _VSTD::copy_n(__str.begin(), __str.size(), _VSTD::move(__out_it));
  260. }
  261. }
  262. template <output_iterator<const char&> _OutIt>
  263. _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT _OutIt
  264. vformat_to(_OutIt __out_it, string_view __fmt, format_args __args) {
  265. return _VSTD::__vformat_to(_VSTD::move(__out_it), __fmt, __args);
  266. }
  267. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  268. template <output_iterator<const wchar_t&> _OutIt>
  269. _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT _OutIt
  270. vformat_to(_OutIt __out_it, wstring_view __fmt, wformat_args __args) {
  271. return _VSTD::__vformat_to(_VSTD::move(__out_it), __fmt, __args);
  272. }
  273. #endif
  274. template <output_iterator<const char&> _OutIt, class... _Args>
  275. _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT _OutIt
  276. format_to(_OutIt __out_it, string_view __fmt, const _Args&... __args) {
  277. return _VSTD::vformat_to(_VSTD::move(__out_it), __fmt,
  278. _VSTD::make_format_args(__args...));
  279. }
  280. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  281. template <output_iterator<const wchar_t&> _OutIt, class... _Args>
  282. _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT _OutIt
  283. format_to(_OutIt __out_it, wstring_view __fmt, const _Args&... __args) {
  284. return _VSTD::vformat_to(_VSTD::move(__out_it), __fmt,
  285. _VSTD::make_wformat_args(__args...));
  286. }
  287. #endif
  288. _LIBCPP_ALWAYS_INLINE inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT string
  289. vformat(string_view __fmt, format_args __args) {
  290. string __res;
  291. _VSTD::vformat_to(_VSTD::back_inserter(__res), __fmt, __args);
  292. return __res;
  293. }
  294. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  295. _LIBCPP_ALWAYS_INLINE inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT wstring
  296. vformat(wstring_view __fmt, wformat_args __args) {
  297. wstring __res;
  298. _VSTD::vformat_to(_VSTD::back_inserter(__res), __fmt, __args);
  299. return __res;
  300. }
  301. #endif
  302. template <class... _Args>
  303. _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT string
  304. format(string_view __fmt, const _Args&... __args) {
  305. return _VSTD::vformat(__fmt, _VSTD::make_format_args(__args...));
  306. }
  307. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  308. template <class... _Args>
  309. _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT wstring
  310. format(wstring_view __fmt, const _Args&... __args) {
  311. return _VSTD::vformat(__fmt, _VSTD::make_wformat_args(__args...));
  312. }
  313. #endif
  314. template <output_iterator<const char&> _OutIt, class... _Args>
  315. _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT format_to_n_result<_OutIt>
  316. format_to_n(_OutIt __out_it, iter_difference_t<_OutIt> __n, string_view __fmt,
  317. const _Args&... __args) {
  318. // TODO FMT Improve PoC: using std::string is inefficient.
  319. string __str = _VSTD::vformat(__fmt, _VSTD::make_format_args(__args...));
  320. iter_difference_t<_OutIt> __s = __str.size();
  321. iter_difference_t<_OutIt> __m =
  322. _VSTD::clamp(__n, iter_difference_t<_OutIt>(0), __s);
  323. __out_it = _VSTD::copy_n(__str.begin(), __m, _VSTD::move(__out_it));
  324. return {_VSTD::move(__out_it), __s};
  325. }
  326. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  327. template <output_iterator<const wchar_t&> _OutIt, class... _Args>
  328. _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT format_to_n_result<_OutIt>
  329. format_to_n(_OutIt __out_it, iter_difference_t<_OutIt> __n, wstring_view __fmt,
  330. const _Args&... __args) {
  331. // TODO FMT Improve PoC: using std::string is inefficient.
  332. wstring __str = _VSTD::vformat(__fmt, _VSTD::make_wformat_args(__args...));
  333. iter_difference_t<_OutIt> __s = __str.size();
  334. iter_difference_t<_OutIt> __m =
  335. _VSTD::clamp(__n, iter_difference_t<_OutIt>(0), __s);
  336. __out_it = _VSTD::copy_n(__str.begin(), __m, _VSTD::move(__out_it));
  337. return {_VSTD::move(__out_it), __s};
  338. }
  339. #endif
  340. template <class... _Args>
  341. _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT size_t
  342. formatted_size(string_view __fmt, const _Args&... __args) {
  343. // TODO FMT Improve PoC: using std::string is inefficient.
  344. return _VSTD::vformat(__fmt, _VSTD::make_format_args(__args...)).size();
  345. }
  346. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  347. template <class... _Args>
  348. _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT size_t
  349. formatted_size(wstring_view __fmt, const _Args&... __args) {
  350. // TODO FMT Improve PoC: using std::string is inefficient.
  351. return _VSTD::vformat(__fmt, _VSTD::make_wformat_args(__args...)).size();
  352. }
  353. #endif
  354. #ifndef _LIBCPP_HAS_NO_LOCALIZATION
  355. template <class _OutIt, class _CharT, class _FormatOutIt>
  356. requires(output_iterator<_OutIt, const _CharT&>) _LIBCPP_HIDE_FROM_ABI _OutIt
  357. __vformat_to(
  358. _OutIt __out_it, locale __loc, basic_string_view<_CharT> __fmt,
  359. basic_format_args<basic_format_context<_FormatOutIt, _CharT>> __args) {
  360. if constexpr (same_as<_OutIt, _FormatOutIt>)
  361. return _VSTD::__format::__vformat_to(
  362. basic_format_parse_context{__fmt, __args.__size()},
  363. _VSTD::__format_context_create(_VSTD::move(__out_it), __args,
  364. _VSTD::move(__loc)));
  365. else {
  366. basic_string<_CharT> __str;
  367. _VSTD::__format::__vformat_to(
  368. basic_format_parse_context{__fmt, __args.__size()},
  369. _VSTD::__format_context_create(_VSTD::back_inserter(__str), __args,
  370. _VSTD::move(__loc)));
  371. return _VSTD::copy_n(__str.begin(), __str.size(), _VSTD::move(__out_it));
  372. }
  373. }
  374. template <output_iterator<const char&> _OutIt>
  375. _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT _OutIt vformat_to(
  376. _OutIt __out_it, locale __loc, string_view __fmt, format_args __args) {
  377. return _VSTD::__vformat_to(_VSTD::move(__out_it), _VSTD::move(__loc), __fmt,
  378. __args);
  379. }
  380. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  381. template <output_iterator<const wchar_t&> _OutIt>
  382. _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT _OutIt vformat_to(
  383. _OutIt __out_it, locale __loc, wstring_view __fmt, wformat_args __args) {
  384. return _VSTD::__vformat_to(_VSTD::move(__out_it), _VSTD::move(__loc), __fmt,
  385. __args);
  386. }
  387. #endif
  388. template <output_iterator<const char&> _OutIt, class... _Args>
  389. _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT _OutIt format_to(
  390. _OutIt __out_it, locale __loc, string_view __fmt, const _Args&... __args) {
  391. return _VSTD::vformat_to(_VSTD::move(__out_it), _VSTD::move(__loc), __fmt,
  392. _VSTD::make_format_args(__args...));
  393. }
  394. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  395. template <output_iterator<const wchar_t&> _OutIt, class... _Args>
  396. _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT _OutIt format_to(
  397. _OutIt __out_it, locale __loc, wstring_view __fmt, const _Args&... __args) {
  398. return _VSTD::vformat_to(_VSTD::move(__out_it), _VSTD::move(__loc), __fmt,
  399. _VSTD::make_wformat_args(__args...));
  400. }
  401. #endif
  402. _LIBCPP_ALWAYS_INLINE inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT string
  403. vformat(locale __loc, string_view __fmt, format_args __args) {
  404. string __res;
  405. _VSTD::vformat_to(_VSTD::back_inserter(__res), _VSTD::move(__loc), __fmt,
  406. __args);
  407. return __res;
  408. }
  409. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  410. _LIBCPP_ALWAYS_INLINE inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT wstring
  411. vformat(locale __loc, wstring_view __fmt, wformat_args __args) {
  412. wstring __res;
  413. _VSTD::vformat_to(_VSTD::back_inserter(__res), _VSTD::move(__loc), __fmt,
  414. __args);
  415. return __res;
  416. }
  417. #endif
  418. template <class... _Args>
  419. _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT string
  420. format(locale __loc, string_view __fmt, const _Args&... __args) {
  421. return _VSTD::vformat(_VSTD::move(__loc), __fmt,
  422. _VSTD::make_format_args(__args...));
  423. }
  424. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  425. template <class... _Args>
  426. _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT wstring
  427. format(locale __loc, wstring_view __fmt, const _Args&... __args) {
  428. return _VSTD::vformat(_VSTD::move(__loc), __fmt,
  429. _VSTD::make_wformat_args(__args...));
  430. }
  431. #endif
  432. template <output_iterator<const char&> _OutIt, class... _Args>
  433. _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT format_to_n_result<_OutIt>
  434. format_to_n(_OutIt __out_it, iter_difference_t<_OutIt> __n, locale __loc,
  435. string_view __fmt, const _Args&... __args) {
  436. // TODO FMT Improve PoC: using std::string is inefficient.
  437. string __str = _VSTD::vformat(_VSTD::move(__loc), __fmt,
  438. _VSTD::make_format_args(__args...));
  439. iter_difference_t<_OutIt> __s = __str.size();
  440. iter_difference_t<_OutIt> __m =
  441. _VSTD::clamp(__n, iter_difference_t<_OutIt>(0), __s);
  442. __out_it = _VSTD::copy_n(__str.begin(), __m, _VSTD::move(__out_it));
  443. return {_VSTD::move(__out_it), __s};
  444. }
  445. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  446. template <output_iterator<const wchar_t&> _OutIt, class... _Args>
  447. _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT format_to_n_result<_OutIt>
  448. format_to_n(_OutIt __out_it, iter_difference_t<_OutIt> __n, locale __loc,
  449. wstring_view __fmt, const _Args&... __args) {
  450. // TODO FMT Improve PoC: using std::string is inefficient.
  451. wstring __str = _VSTD::vformat(_VSTD::move(__loc), __fmt,
  452. _VSTD::make_wformat_args(__args...));
  453. iter_difference_t<_OutIt> __s = __str.size();
  454. iter_difference_t<_OutIt> __m =
  455. _VSTD::clamp(__n, iter_difference_t<_OutIt>(0), __s);
  456. __out_it = _VSTD::copy_n(__str.begin(), __m, _VSTD::move(__out_it));
  457. return {_VSTD::move(__out_it), __s};
  458. }
  459. #endif
  460. template <class... _Args>
  461. _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT size_t
  462. formatted_size(locale __loc, string_view __fmt, const _Args&... __args) {
  463. // TODO FMT Improve PoC: using std::string is inefficient.
  464. return _VSTD::vformat(_VSTD::move(__loc), __fmt,
  465. _VSTD::make_format_args(__args...))
  466. .size();
  467. }
  468. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  469. template <class... _Args>
  470. _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT size_t
  471. formatted_size(locale __loc, wstring_view __fmt, const _Args&... __args) {
  472. // TODO FMT Improve PoC: using std::string is inefficient.
  473. return _VSTD::vformat(_VSTD::move(__loc), __fmt,
  474. _VSTD::make_wformat_args(__args...))
  475. .size();
  476. }
  477. #endif
  478. #endif // _LIBCPP_HAS_NO_LOCALIZATION
  479. #endif // !defined(_LIBCPP_HAS_NO_CONCEPTS)
  480. #endif //_LIBCPP_STD_VER > 17
  481. _LIBCPP_END_NAMESPACE_STD
  482. #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_FORMAT)
  483. #endif // _LIBCPP_FORMAT