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