blob: d4e24b6c30fcf9fed052000ca4f7c663ed5a73f4 [file] [log] [blame]
Mark de Weverdf5fd832020-11-26 19:12:18 +01001// -*- C++ -*-
2//===--------------------------- format -----------------------------------===//
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
10#ifndef _LIBCPP_FORMAT
11#define _LIBCPP_FORMAT
12
13/*
14
15namespace std {
Mark de Wevercba61372020-12-05 11:45:21 +010016 // [format.context], class template basic_format_context
17 template<class Out, class charT>
18 class basic_format_context {
19 basic_format_args<basic_format_context> args_; // exposition only
20 Out out_; // exposition only
21
22 public:
23 using iterator = Out;
24 using char_type = charT;
25 template<class T> using formatter_type = formatter<T, charT>;
26
27 basic_format_arg<basic_format_context> arg(size_t id) const;
28 std::locale locale();
29
30 iterator out();
31 void advance_to(iterator it);
32 };
33 using format_context = basic_format_context<unspecified, char>;
34 using wformat_context = basic_format_context<unspecified, wchar_t>;
35
36 // [format.args], class template basic_format_args
37 template<class Context>
38 class basic_format_args {
39 size_t size_; // exposition only
40 const basic_format_arg<Context>* data_; // exposition only
41
42 public:
43 basic_format_args() noexcept;
44
45 template<class... Args>
46 basic_format_args(const format-arg-store<Context, Args...>& store) noexcept;
47
48 basic_format_arg<Context> get(size_t i) const noexcept;
49 };
50 using format_args = basic_format_args<format_context>;
51 using wformat_args = basic_format_args<wformat_context>;
52
53
54 template<class Out, class charT>
55 using format_args_t = basic_format_args<basic_format_context<Out, charT>>;
56
Mark de Wevercb50a052020-12-19 13:52:07 +010057 // [format.functions], formatting functions
58 template<class... Args>
59 string format(string_view fmt, const Args&... args);
60 template<class... Args>
61 wstring format(wstring_view fmt, const Args&... args);
62 template<class... Args>
63 string format(const locale& loc, string_view fmt, const Args&... args);
64 template<class... Args>
65 wstring format(const locale& loc, wstring_view fmt, const Args&... args);
66
67 string vformat(string_view fmt, format_args args);
68 wstring vformat(wstring_view fmt, wformat_args args);
69 string vformat(const locale& loc, string_view fmt, format_args args);
70 wstring vformat(const locale& loc, wstring_view fmt, wformat_args args);
71
72 template<class Out, class... Args>
73 Out format_to(Out out, string_view fmt, const Args&... args);
74 template<class Out, class... Args>
75 Out format_to(Out out, wstring_view fmt, const Args&... args);
76 template<class Out, class... Args>
77 Out format_to(Out out, const locale& loc, string_view fmt, const Args&... args);
78 template<class Out, class... Args>
79 Out format_to(Out out, const locale& loc, wstring_view fmt, const Args&... args);
80
81 template<class Out>
82 Out vformat_to(Out out, string_view fmt,
83 format_args_t<type_identity_t<Out>, char> args);
84 template<class Out>
85 Out vformat_to(Out out, wstring_view fmt,
86 format_args_t<type_identity_t<Out>, wchar_t> args);
87 template<class Out>
88 Out vformat_to(Out out, const locale& loc, string_view fmt,
89 format_args_t<type_identity_t<Out>, char> args);
90 template<class Out>
91 Out vformat_to(Out out, const locale& loc, wstring_view fmt,
92 format_args_t<type_identity_t<Out>, wchar_t> args);
93
94 template<class Out> struct format_to_n_result {
95 Out out;
96 iter_difference_t<Out> size;
97 };
98
99 template<class Out, class... Args>
100 format_to_n_result<Out> format_to_n(Out out, iter_difference_t<Out> n,
101 string_view fmt, const Args&... args);
102 template<class Out, class... Args>
103 format_to_n_result<Out> format_to_n(Out out, iter_difference_t<Out> n,
104 wstring_view fmt, const Args&... args);
105 template<class Out, class... Args>
106 format_to_n_result<Out> format_to_n(Out out, iter_difference_t<Out> n,
107 const locale& loc, string_view fmt,
108 const Args&... args);
109 template<class Out, class... Args>
110 format_to_n_result<Out> format_to_n(Out out, iter_difference_t<Out> n,
111 const locale& loc, wstring_view fmt,
112 const Args&... args);
113
114 template<class... Args>
115 size_t formatted_size(string_view fmt, const Args&... args);
116 template<class... Args>
117 size_t formatted_size(wstring_view fmt, const Args&... args);
118 template<class... Args>
119 size_t formatted_size(const locale& loc, string_view fmt, const Args&... args);
120 template<class... Args>
121 size_t formatted_size(const locale& loc, wstring_view fmt, const Args&... args);
122
123 // [format.formatter], formatter
124 template<> struct formatter<char, char>;
125 template<> struct formatter<char, wchar_t>;
126 template<> struct formatter<wchar_t, wchar_t>;
127
128 template<> struct formatter<charT*, charT>;
129 template<> struct formatter<const charT*, charT>;
130 template<size_t N> struct formatter<const charT[N], charT>;
131 template<class traits, class Allocator>
132 struct formatter<basic_string<charT, traits, Allocator>, charT>;
133 template<class traits>
134 struct formatter<basic_string_view<charT, traits>, charT>;
135
Mark de Wevercba61372020-12-05 11:45:21 +0100136 // [format.parse.ctx], class template basic_format_parse_context
137 template<class charT>
138 class basic_format_parse_context {
139 public:
140 using char_type = charT;
141 using const_iterator = typename basic_string_view<charT>::const_iterator;
142 using iterator = const_iterator;
143
144 private:
145 iterator begin_; // exposition only
146 iterator end_; // exposition only
147 enum indexing { unknown, manual, automatic }; // exposition only
148 indexing indexing_; // exposition only
149 size_t next_arg_id_; // exposition only
150 size_t num_args_; // exposition only
151
152 public:
153 constexpr explicit basic_format_parse_context(basic_string_view<charT> fmt,
154 size_t num_args = 0) noexcept;
155 basic_format_parse_context(const basic_format_parse_context&) = delete;
156 basic_format_parse_context& operator=(const basic_format_parse_context&) = delete;
157
158 constexpr const_iterator begin() const noexcept;
159 constexpr const_iterator end() const noexcept;
160 constexpr void advance_to(const_iterator it);
161
162 constexpr size_t next_arg_id();
163 constexpr void check_arg_id(size_t id);
164 };
165 using format_parse_context = basic_format_parse_context<char>;
166 using wformat_parse_context = basic_format_parse_context<wchar_t>;
167
168 // [format.arguments], arguments
169 // [format.arg], class template basic_format_arg
170 template<class Context>
171 class basic_format_arg {
172 public:
173 class handle;
174
175 private:
176 using char_type = typename Context::char_type; // exposition only
177
178 variant<monostate, bool, char_type,
179 int, unsigned int, long long int, unsigned long long int,
180 float, double, long double,
181 const char_type*, basic_string_view<char_type>,
182 const void*, handle> value; // exposition only
183
184 template<class T> explicit basic_format_arg(const T& v) noexcept; // exposition only
185 explicit basic_format_arg(float n) noexcept; // exposition only
186 explicit basic_format_arg(double n) noexcept; // exposition only
187 explicit basic_format_arg(long double n) noexcept; // exposition only
188 explicit basic_format_arg(const char_type* s); // exposition only
189
190 template<class traits>
191 explicit basic_format_arg(
192 basic_string_view<char_type, traits> s) noexcept; // exposition only
193
194 template<class traits, class Allocator>
195 explicit basic_format_arg(
196 const basic_string<char_type, traits, Allocator>& s) noexcept; // exposition only
197
198 explicit basic_format_arg(nullptr_t) noexcept; // exposition only
199
200 template<class T>
201 explicit basic_format_arg(const T* p) noexcept; // exposition only
202
203 public:
204 basic_format_arg() noexcept;
205
206 explicit operator bool() const noexcept;
207 };
208
209 template<class Visitor, class Context>
210 see below visit_format_arg(Visitor&& vis, basic_format_arg<Context> arg);
211
212 // [format.arg.store], class template format-arg-store
213 template<class Context, class... Args>
214 struct format-arg-store { // exposition only
215 array<basic_format_arg<Context>, sizeof...(Args)> args;
216 };
217
218 template<class Context = format_context, class... Args>
219 format-arg-store<Context, Args...>
220 make_format_args(const Args&... args);
221 template<class... Args>
222 format-arg-store<wformat_context, Args...>
223 make_wformat_args(const Args&... args);
224
Mark de Weverdf5fd832020-11-26 19:12:18 +0100225 // [format.error], class format_error
226 class format_error : public runtime_error {
227 public:
228 explicit format_error(const string& what_arg);
229 explicit format_error(const char* what_arg);
230 };
Mark de Wever6a67a5f2021-02-02 18:10:33 +0100231
232 // [format.parse.ctx], class template basic_format_parse_context
233 template<class charT>
234 class basic_format_parse_context {
235 public:
236 using char_type = charT;
237 using const_iterator = typename basic_string_view<charT>::const_iterator;
238 using iterator = const_iterator;
239
240 private:
241 iterator begin_; // exposition only
242 iterator end_; // exposition only
243 enum indexing { unknown, manual, automatic }; // exposition only
244 indexing indexing_; // exposition only
245 size_t next_arg_id_; // exposition only
246 size_t num_args_; // exposition only
247
248 public:
249 constexpr explicit basic_format_parse_context(basic_string_view<charT> fmt,
250 size_t num_args = 0) noexcept;
251 basic_format_parse_context(const basic_format_parse_context&) = delete;
252 basic_format_parse_context& operator=(const basic_format_parse_context&) = delete;
253
254 constexpr const_iterator begin() const noexcept;
255 constexpr const_iterator end() const noexcept;
256 constexpr void advance_to(const_iterator it);
257
258 constexpr size_t next_arg_id();
259 constexpr void check_arg_id(size_t id);
260 };
261 using format_parse_context = basic_format_parse_context<char>;
262 using wformat_parse_context = basic_format_parse_context<wchar_t>;
Mark de Weverdf5fd832020-11-26 19:12:18 +0100263}
264
265*/
266
Mark de Weverbe38c382021-08-11 17:33:54 +0200267// Make sure all feature-test macros are available.
Mark de Wever14151d22021-07-30 14:35:37 -0400268#include <version>
Mark de Weverbe38c382021-08-11 17:33:54 +0200269// Enable the contents of the header only when libc++ was built with LIBCXX_ENABLE_INCOMPLETE_FEATURES.
Mark de Wever14151d22021-07-30 14:35:37 -0400270#if !defined(_LIBCPP_HAS_NO_INCOMPLETE_FORMAT)
271
Mark de Weverdf5fd832020-11-26 19:12:18 +0100272#include <__config>
Mark de Wevercb50a052020-12-19 13:52:07 +0100273#include <__debug>
Mark de Wevercba61372020-12-05 11:45:21 +0100274#include <__format/format_arg.h>
275#include <__format/format_args.h>
276#include <__format/format_context.h>
Mark de Wever1aef5d02021-04-25 17:58:03 +0200277#include <__format/format_error.h>
Mark de Wever29307fd2020-12-14 17:39:15 +0100278#include <__format/format_fwd.h>
Mark de Wevere26bdbb2021-04-25 18:23:42 +0200279#include <__format/format_parse_context.h>
Mark de Wevercb50a052020-12-19 13:52:07 +0100280#include <__format/format_string.h>
281#include <__format/formatter.h>
Mark de Weverb9d8e2f2020-12-14 17:39:15 +0100282#include <__format/formatter_char.h>
Mark de Wever29307fd2020-12-14 17:39:15 +0100283#include <__format/formatter_integer.h>
Mark de Weverf82551e2020-12-14 17:39:15 +0100284#include <__format/formatter_string.h>
285#include <__format/parser_std_format_spec.h>
Mark de Wevercb50a052020-12-19 13:52:07 +0100286#include <__variant/monostate.h>
Mark de Wevercba61372020-12-05 11:45:21 +0100287#include <array>
Mark de Wevercb50a052020-12-19 13:52:07 +0100288#include <concepts>
289#include <string>
290#include <string_view>
291#include <type_traits>
292
293#ifndef _LIBCPP_HAS_NO_LOCALIZATION
294#include <locale>
295#endif
Mark de Wever8a0a1882021-07-25 09:18:53 +0200296
Mark de Weverdf5fd832020-11-26 19:12:18 +0100297#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Mark de Wevercba61372020-12-05 11:45:21 +0100298#pragma GCC system_header
Mark de Weverdf5fd832020-11-26 19:12:18 +0100299#endif
300
Mark de Wevercb50a052020-12-19 13:52:07 +0100301_LIBCPP_PUSH_MACROS
302#include <__undef_macros>
303
Mark de Wevercba61372020-12-05 11:45:21 +0100304_LIBCPP_BEGIN_NAMESPACE_STD
305
306#if _LIBCPP_STD_VER > 17
307
308// TODO FMT Remove this once we require compilers with proper C++20 support.
309// If the compiler has no concepts support, the format header will be disabled.
310// Without concepts support enable_if needs to be used and that too much effort
311// to support compilers with partial C++20 support.
312#if !defined(_LIBCPP_HAS_NO_CONCEPTS)
313
314// TODO FMT Evaluate which templates should be external templates. This
315// improves the efficiency of the header. However since the header is still
316// under heavy development and not all classes are stable it makes no sense
317// to do this optimization now.
318
319using format_args = basic_format_args<format_context>;
320using wformat_args = basic_format_args<wformat_context>;
321
322template <class _OutIt, class _CharT>
323using format_args_t = basic_format_args<basic_format_context<_OutIt, _CharT>>;
324
325template <class _Context, class... _Args>
326struct _LIBCPP_TEMPLATE_VIS __format_arg_store {
327 // TODO FMT Use a built-in array.
328 array<basic_format_arg<_Context>, sizeof...(_Args)> __args;
329};
330
331template <class _Context = format_context, class... _Args>
332_LIBCPP_HIDE_FROM_ABI __format_arg_store<_Context, _Args...>
333make_format_args(const _Args&... __args) {
334 return {basic_format_arg<_Context>(__args)...};
335}
336
337template <class... _Args>
338_LIBCPP_HIDE_FROM_ABI __format_arg_store<wformat_context, _Args...>
339make_wformat_args(const _Args&... __args) {
340 return _VSTD::make_format_args<wformat_context>(__args...);
341}
Mark de Wevercb50a052020-12-19 13:52:07 +0100342namespace __format {
Mark de Wevercb50a052020-12-19 13:52:07 +0100343
Mark de Wevercb50a052020-12-19 13:52:07 +0100344template <class _Tp, class _CharT>
345requires(is_arithmetic_v<_Tp> &&
346 !same_as<_Tp, bool>) struct _LIBCPP_HIDE_FROM_ABI
347 __formatter_arithmetic {
348 _LIBCPP_HIDE_FROM_ABI
349 auto parse(auto& __parse_ctx) -> decltype(__parse_ctx.begin()) {
350 // TODO FMT Implement
351 return __parse_ctx.begin();
352 }
353
354 _LIBCPP_HIDE_FROM_ABI
355 auto format(_Tp __value, auto& __ctx) -> decltype(__ctx.out()) {
356 return __handle_format(__value, __ctx);
357 }
358
359private:
360 template <class _Uv>
361 _LIBCPP_HIDDEN static string
362 __convert(_Uv __value) requires(same_as<_CharT, char>) {
363 return _VSTD::to_string(__value);
364 }
365 template <class _Uv>
366 _LIBCPP_HIDDEN static wstring
367 __convert(_Uv __value) requires(same_as<_CharT, wchar_t>) {
368 return _VSTD::to_wstring(__value);
369 }
370
371 template <class _Uv>
372 _LIBCPP_HIDDEN auto __handle_format(_Uv __value, auto& __ctx)
373 -> decltype(__ctx.out())
Mark de Wevercb50a052020-12-19 13:52:07 +0100374 {
375 // TODO FMT Implement using formatting arguments
376 // TODO FMT Improve PoC since using std::to_string is inefficient.
377 // Note the code doesn't use std::string::iterator since the unit tests
378 // test with debug iterators and they fail with strings created from
379 // std::to_string.
380 auto __str = __convert(__value);
381 auto __out_it = __ctx.out();
382 for (size_t __i = 0, __e = __str.size(); __i != __e; ++__i)
383 *__out_it++ = __str[__i];
384 return __out_it;
385 }
Mark de Wevercb50a052020-12-19 13:52:07 +0100386};
387} // namespace __format
388
389// These specializations are helper stubs and not proper formatters.
390// TODO FMT Implement the proper formatter specializations.
391
Mark de Wevercb50a052020-12-19 13:52:07 +0100392// [format.formatter.spec]/2.3
393// For each charT, for each cv-unqualified arithmetic type ArithmeticT other
394// than char, wchar_t, char8_t, char16_t, or char32_t, a specialization
395
396// Boolean.
397template <class _CharT>
398struct _LIBCPP_TEMPLATE_VIS formatter<bool, _CharT> {
399 _LIBCPP_HIDE_FROM_ABI
400 auto parse(auto& __parse_ctx) -> decltype(__parse_ctx.begin()) {
401 // TODO FMT Implement
402 return __parse_ctx.begin();
403 }
404
405 _LIBCPP_HIDE_FROM_ABI
406 auto format(bool __b, auto& __ctx) -> decltype(__ctx.out()) {
407 // TODO FMT Implement using formatting arguments
408 auto __out_it = __ctx.out();
409 *__out_it++ = _CharT('0') + __b;
410 return __out_it;
411 }
412};
413
Mark de Wevercb50a052020-12-19 13:52:07 +0100414// Floating point types.
415// TODO FMT There are no replacements for the floating point stubs due to not
416// having floating point support in std::to_chars yet. These stubs aren't
417// removed since they are useful for developing the real versions.
418// Ultimately the stubs should be implemented properly and this code can be
419// removed.
420#if 0
421template <class _CharT>
422struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<float, _CharT>
423 : public __format::__formatter_arithmetic<float, _CharT> {};
424template <class _CharT>
425struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT
426 formatter<double, _CharT>
427 : public __format::__formatter_arithmetic<double, _CharT> {};
428template <class _CharT>
429struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT
430 formatter<long double, _CharT>
431 : public __format::__formatter_arithmetic<long double, _CharT> {};
432#endif
433
434namespace __format {
435
436template <class _CharT, class _ParseCtx, class _Ctx>
437_LIBCPP_HIDE_FROM_ABI const _CharT*
438__handle_replacement_field(const _CharT* __begin, const _CharT* __end,
439 _ParseCtx& __parse_ctx, _Ctx& __ctx) {
440 __format::__parse_number_result __r =
441 __format::__parse_arg_id(__begin, __end, __parse_ctx);
442
443 switch (*__r.__ptr) {
444 case _CharT(':'):
445 // The arg-id has a format-specifier, advance the input to the format-spec.
446 __parse_ctx.advance_to(__r.__ptr + 1);
447 break;
448 case _CharT('}'):
449 // The arg-id has no format-specifier.
450 __parse_ctx.advance_to(__r.__ptr);
451 break;
452 default:
453 __throw_format_error(
454 "The replacement field arg-id should terminate at a ':' or '}'");
455 }
456
457 _VSTD::visit_format_arg(
458 [&](auto __arg) {
459 if constexpr (same_as<decltype(__arg), monostate>)
460 __throw_format_error("Argument index out of bounds");
461 else {
462 formatter<decltype(__arg), _CharT> __formatter;
463 __parse_ctx.advance_to(__formatter.parse(__parse_ctx));
464 __ctx.advance_to(__formatter.format(__arg, __ctx));
465 }
466 },
467 __ctx.arg(__r.__value));
468
469 __begin = __parse_ctx.begin();
470 if (__begin == __end || *__begin != _CharT('}'))
471 __throw_format_error("The replacement field misses a terminating '}'");
472
473 return ++__begin;
474}
475
476template <class _ParseCtx, class _Ctx>
477_LIBCPP_HIDE_FROM_ABI typename _Ctx::iterator
478__vformat_to(_ParseCtx&& __parse_ctx, _Ctx&& __ctx) {
479 using _CharT = typename _ParseCtx::char_type;
480 static_assert(same_as<typename _Ctx::char_type, _CharT>);
481
482 const _CharT* __begin = __parse_ctx.begin();
483 const _CharT* __end = __parse_ctx.end();
484 typename _Ctx::iterator __out_it = __ctx.out();
485 while (__begin != __end) {
486 switch (*__begin) {
487 case _CharT('{'):
488 ++__begin;
489 if (__begin == __end)
490 __throw_format_error("The format string terminates at a '{'");
491
492 if (*__begin != _CharT('{')) [[likely]] {
493 __ctx.advance_to(_VSTD::move(__out_it));
494 __begin =
495 __handle_replacement_field(__begin, __end, __parse_ctx, __ctx);
496 __out_it = __ctx.out();
497
498 // The output is written and __begin points to the next character. So
499 // start the next iteration.
500 continue;
501 }
502 // The string is an escape character.
503 break;
504
505 case _CharT('}'):
506 ++__begin;
507 if (__begin == __end || *__begin != _CharT('}'))
508 __throw_format_error(
509 "The format string contains an invalid escape sequence");
510
511 break;
512 }
513
514 // Copy the character to the output verbatim.
515 *__out_it++ = *__begin++;
516 }
517 return __out_it;
518}
519
520} // namespace __format
521
522template <class _OutIt, class _CharT>
523requires(output_iterator<_OutIt, const _CharT&>) _LIBCPP_HIDE_FROM_ABI _OutIt
524 __vformat_to(_OutIt __out_it, basic_string_view<_CharT> __fmt,
525 format_args_t<type_identity_t<_OutIt>, _CharT> __args) {
526 return __format::__vformat_to(
527 basic_format_parse_context{__fmt, __args.__size()},
528 _VSTD::__format_context_create(_VSTD::move(__out_it), __args));
529}
530
531template <output_iterator<const char&> _OutIt>
532_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT _OutIt
533vformat_to(_OutIt __out_it, string_view __fmt,
534 format_args_t<type_identity_t<_OutIt>, char> __args) {
535 return _VSTD::__vformat_to(_VSTD::move(__out_it), __fmt, __args);
536}
537
538template <output_iterator<const wchar_t&> _OutIt>
539_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT _OutIt
540vformat_to(_OutIt __out_it, wstring_view __fmt,
541 format_args_t<type_identity_t<_OutIt>, wchar_t> __args) {
542 return _VSTD::__vformat_to(_VSTD::move(__out_it), __fmt, __args);
543}
544
545template <output_iterator<const char&> _OutIt, class... _Args>
546_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT _OutIt
547format_to(_OutIt __out_it, string_view __fmt, const _Args&... __args) {
548 return _VSTD::vformat_to(
549 _VSTD::move(__out_it), __fmt,
550 _VSTD::make_format_args<basic_format_context<_OutIt, char>>(__args...));
551}
552
553template <output_iterator<const wchar_t&> _OutIt, class... _Args>
554_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT _OutIt
555format_to(_OutIt __out_it, wstring_view __fmt, const _Args&... __args) {
556 return _VSTD::vformat_to(
557 _VSTD::move(__out_it), __fmt,
558 _VSTD::make_format_args<basic_format_context<_OutIt, wchar_t>>(
559 __args...));
560}
561
562inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT string
563vformat(string_view __fmt, format_args __args) {
564 string __res;
565 _VSTD::vformat_to(_VSTD::back_inserter(__res), __fmt, __args);
566 return __res;
567}
568
569inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT wstring
570vformat(wstring_view __fmt, wformat_args __args) {
571 wstring __res;
572 _VSTD::vformat_to(_VSTD::back_inserter(__res), __fmt, __args);
573 return __res;
574}
575
576template <class... _Args>
577_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT string
578format(string_view __fmt, const _Args&... __args) {
579 return _VSTD::vformat(__fmt, _VSTD::make_format_args(__args...));
580}
581
582template <class... _Args>
583_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT wstring
584format(wstring_view __fmt, const _Args&... __args) {
585 return _VSTD::vformat(__fmt, _VSTD::make_wformat_args(__args...));
586}
587
588template <class _OutIt>
589struct _LIBCPP_TEMPLATE_VIS format_to_n_result {
590 _OutIt out;
591 iter_difference_t<_OutIt> size;
592};
593
594template <output_iterator<const char&> _OutIt, class... _Args>
595_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT format_to_n_result<_OutIt>
596format_to_n(_OutIt __out_it, iter_difference_t<_OutIt> __n, string_view __fmt,
597 const _Args&... __args) {
598 // TODO FMT Improve PoC: using std::string is inefficient.
599 string __str = _VSTD::vformat(__fmt, _VSTD::make_format_args(__args...));
600 iter_difference_t<_OutIt> __s = __str.size();
601 iter_difference_t<_OutIt> __m =
602 _VSTD::clamp(__n, iter_difference_t<_OutIt>(0), __s);
603 __out_it = _VSTD::copy_n(__str.begin(), __m, _VSTD::move(__out_it));
604 return {_VSTD::move(__out_it), __s};
605}
606
607template <output_iterator<const wchar_t&> _OutIt, class... _Args>
608_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT format_to_n_result<_OutIt>
609format_to_n(_OutIt __out_it, iter_difference_t<_OutIt> __n, wstring_view __fmt,
610 const _Args&... __args) {
611 // TODO FMT Improve PoC: using std::string is inefficient.
612 wstring __str = _VSTD::vformat(__fmt, _VSTD::make_wformat_args(__args...));
613 iter_difference_t<_OutIt> __s = __str.size();
614 iter_difference_t<_OutIt> __m =
615 _VSTD::clamp(__n, iter_difference_t<_OutIt>(0), __s);
616 __out_it = _VSTD::copy_n(__str.begin(), __m, _VSTD::move(__out_it));
617 return {_VSTD::move(__out_it), __s};
618}
619
620template <class... _Args>
621_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT size_t
622formatted_size(string_view __fmt, const _Args&... __args) {
623 // TODO FMT Improve PoC: using std::string is inefficient.
624 return _VSTD::vformat(__fmt, _VSTD::make_format_args(__args...)).size();
625}
626
627template <class... _Args>
628_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT size_t
629formatted_size(wstring_view __fmt, const _Args&... __args) {
630 // TODO FMT Improve PoC: using std::string is inefficient.
631 return _VSTD::vformat(__fmt, _VSTD::make_wformat_args(__args...)).size();
632}
633
634#ifndef _LIBCPP_HAS_NO_LOCALIZATION
635
636template <class _OutIt, class _CharT>
637requires(output_iterator<_OutIt, const _CharT&>) _LIBCPP_HIDE_FROM_ABI _OutIt
638 __vformat_to(_OutIt __out_it, locale __loc, basic_string_view<_CharT> __fmt,
639 format_args_t<type_identity_t<_OutIt>, _CharT> __args) {
640 return __format::__vformat_to(
641 basic_format_parse_context{__fmt, __args.__size()},
642 _VSTD::__format_context_create(_VSTD::move(__out_it), __args,
643 _VSTD::move(__loc)));
644}
645
646template <output_iterator<const char&> _OutIt>
647_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT _OutIt
648vformat_to(_OutIt __out_it, locale __loc, string_view __fmt,
649 format_args_t<type_identity_t<_OutIt>, char> __args) {
650 return _VSTD::__vformat_to(_VSTD::move(__out_it), _VSTD::move(__loc), __fmt,
651 __args);
652}
653
654template <output_iterator<const wchar_t&> _OutIt>
655_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT _OutIt
656vformat_to(_OutIt __out_it, locale __loc, wstring_view __fmt,
657 format_args_t<type_identity_t<_OutIt>, wchar_t> __args) {
658 return _VSTD::__vformat_to(_VSTD::move(__out_it), _VSTD::move(__loc), __fmt,
659 __args);
660}
661
662template <output_iterator<const char&> _OutIt, class... _Args>
663_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT _OutIt format_to(
664 _OutIt __out_it, locale __loc, string_view __fmt, const _Args&... __args) {
665 return _VSTD::vformat_to(
666 _VSTD::move(__out_it), _VSTD::move(__loc), __fmt,
667 _VSTD::make_format_args<basic_format_context<_OutIt, char>>(__args...));
668}
669
670template <output_iterator<const wchar_t&> _OutIt, class... _Args>
671_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT _OutIt format_to(
672 _OutIt __out_it, locale __loc, wstring_view __fmt, const _Args&... __args) {
673 return _VSTD::vformat_to(
674 _VSTD::move(__out_it), _VSTD::move(__loc), __fmt,
675 _VSTD::make_format_args<basic_format_context<_OutIt, wchar_t>>(
676 __args...));
677}
678
679inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT string
680vformat(locale __loc, string_view __fmt, format_args __args) {
681 string __res;
682 _VSTD::vformat_to(_VSTD::back_inserter(__res), _VSTD::move(__loc), __fmt,
683 __args);
684 return __res;
685}
686
687inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT wstring
688vformat(locale __loc, wstring_view __fmt, wformat_args __args) {
689 wstring __res;
690 _VSTD::vformat_to(_VSTD::back_inserter(__res), _VSTD::move(__loc), __fmt,
691 __args);
692 return __res;
693}
694
695template <class... _Args>
696_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT string
697format(locale __loc, string_view __fmt, const _Args&... __args) {
698 return _VSTD::vformat(_VSTD::move(__loc), __fmt,
699 _VSTD::make_format_args(__args...));
700}
701
702template <class... _Args>
703_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT wstring
704format(locale __loc, wstring_view __fmt, const _Args&... __args) {
705 return _VSTD::vformat(_VSTD::move(__loc), __fmt,
706 _VSTD::make_wformat_args(__args...));
707}
708
709template <output_iterator<const char&> _OutIt, class... _Args>
710_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT format_to_n_result<_OutIt>
711format_to_n(_OutIt __out_it, iter_difference_t<_OutIt> __n, locale __loc,
712 string_view __fmt, const _Args&... __args) {
713 // TODO FMT Improve PoC: using std::string is inefficient.
714 string __str = _VSTD::vformat(_VSTD::move(__loc), __fmt,
715 _VSTD::make_format_args(__args...));
716 iter_difference_t<_OutIt> __s = __str.size();
717 iter_difference_t<_OutIt> __m =
718 _VSTD::clamp(__n, iter_difference_t<_OutIt>(0), __s);
719 __out_it = _VSTD::copy_n(__str.begin(), __m, _VSTD::move(__out_it));
720 return {_VSTD::move(__out_it), __s};
721}
722
723template <output_iterator<const wchar_t&> _OutIt, class... _Args>
724_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT format_to_n_result<_OutIt>
725format_to_n(_OutIt __out_it, iter_difference_t<_OutIt> __n, locale __loc,
726 wstring_view __fmt, const _Args&... __args) {
727 // TODO FMT Improve PoC: using std::string is inefficient.
728 wstring __str = _VSTD::vformat(_VSTD::move(__loc), __fmt,
729 _VSTD::make_wformat_args(__args...));
730 iter_difference_t<_OutIt> __s = __str.size();
731 iter_difference_t<_OutIt> __m =
732 _VSTD::clamp(__n, iter_difference_t<_OutIt>(0), __s);
733 __out_it = _VSTD::copy_n(__str.begin(), __m, _VSTD::move(__out_it));
734 return {_VSTD::move(__out_it), __s};
735}
736
737template <class... _Args>
738_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT size_t
739formatted_size(locale __loc, string_view __fmt, const _Args&... __args) {
740 // TODO FMT Improve PoC: using std::string is inefficient.
741 return _VSTD::vformat(_VSTD::move(__loc), __fmt,
742 _VSTD::make_format_args(__args...))
743 .size();
744}
745
746template <class... _Args>
747_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT size_t
748formatted_size(locale __loc, wstring_view __fmt, const _Args&... __args) {
749 // TODO FMT Improve PoC: using std::string is inefficient.
750 return _VSTD::vformat(_VSTD::move(__loc), __fmt,
751 _VSTD::make_wformat_args(__args...))
752 .size();
753}
754
Louis Dionneaf2c8142021-09-09 14:43:02 -0400755#endif // _LIBCPP_HAS_NO_LOCALIZATION
Mark de Wevercb50a052020-12-19 13:52:07 +0100756
Mark de Wevercba61372020-12-05 11:45:21 +0100757#endif // !defined(_LIBCPP_HAS_NO_CONCEPTS)
758#endif //_LIBCPP_STD_VER > 17
759
760_LIBCPP_END_NAMESPACE_STD
761
Mark de Wevercb50a052020-12-19 13:52:07 +0100762_LIBCPP_POP_MACROS
763
Mark de Wever14151d22021-07-30 14:35:37 -0400764#endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_FORMAT)
765
Mark de Weverdf5fd832020-11-26 19:12:18 +0100766#endif // _LIBCPP_FORMAT