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