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