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