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