2 //===----------------------------------------------------------------------===//
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
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP___FORMAT_FORMAT_FUNCTIONS
11 #define _LIBCPP___FORMAT_FORMAT_FUNCTIONS
13 #include <__algorithm/clamp.h>
14 #include <__concepts/convertible_to.h>
15 #include <__concepts/same_as.h>
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>
41 #include <string_view>
43 #if _LIBCPP_HAS_LOCALIZATION
47 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
48 # pragma GCC system_header
52 #include <__undef_macros>
54 _LIBCPP_BEGIN_NAMESPACE_STD
56 #if _LIBCPP_STD_VER >= 20
58 // TODO FMT Evaluate which templates should be external templates. This
59 // improves the efficiency of the header. However since the header is still
60 // under heavy development and not all classes are stable it makes no sense
61 // to do this optimization now.
63 using format_args
= basic_format_args
<format_context
>;
64 # if _LIBCPP_HAS_WIDE_CHARACTERS
65 using wformat_args
= basic_format_args
<wformat_context
>;
68 template <class _Context
= format_context
, class... _Args
>
69 [[nodiscard
]] _LIBCPP_HIDE_FROM_ABI __format_arg_store
<_Context
, _Args
...> make_format_args(_Args
&... __args
) {
70 return std::__format_arg_store
<_Context
, _Args
...>(__args
...);
73 # if _LIBCPP_HAS_WIDE_CHARACTERS
74 template <class... _Args
>
75 [[nodiscard
]] _LIBCPP_HIDE_FROM_ABI __format_arg_store
<wformat_context
, _Args
...> make_wformat_args(_Args
&... __args
) {
76 return std::__format_arg_store
<wformat_context
, _Args
...>(__args
...);
82 /// Helper class parse and handle argument.
84 /// When parsing a handle which is not enabled the code is ill-formed.
85 /// This helper uses the parser of the appropriate formatter for the stored type.
86 template <class _CharT
>
87 class _LIBCPP_TEMPLATE_VIS __compile_time_handle
{
89 template <class _ParseContext
>
90 _LIBCPP_HIDE_FROM_ABI
constexpr void __parse(_ParseContext
& __ctx
) const {
95 _LIBCPP_HIDE_FROM_ABI
constexpr void __enable() {
96 __parse_
= [](basic_format_parse_context
<_CharT
>& __ctx
) {
97 formatter
<_Tp
, _CharT
> __f
;
98 __ctx
.advance_to(__f
.parse(__ctx
));
102 // Before calling __parse the proper handler needs to be set with __enable.
103 // The default handler isn't a core constant expression.
104 _LIBCPP_HIDE_FROM_ABI
constexpr __compile_time_handle()
105 : __parse_([](basic_format_parse_context
<_CharT
>&) { std::__throw_format_error("Not a handle"); }) {}
108 void (*__parse_
)(basic_format_parse_context
<_CharT
>&);
111 // Dummy format_context only providing the parts used during constant
112 // validation of the basic_format_string.
113 template <class _CharT
>
114 struct _LIBCPP_TEMPLATE_VIS __compile_time_basic_format_context
{
116 using char_type
= _CharT
;
118 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __compile_time_basic_format_context(
119 const __arg_t
* __args
, const __compile_time_handle
<_CharT
>* __handles
, size_t __size
)
120 : __args_(__args
), __handles_(__handles
), __size_(__size
) {}
122 // During the compile-time validation nothing needs to be written.
123 // Therefore all operations of this iterator are a NOP.
125 _LIBCPP_HIDE_FROM_ABI
constexpr iterator
& operator=(_CharT
) { return *this; }
126 _LIBCPP_HIDE_FROM_ABI
constexpr iterator
& operator*() { return *this; }
127 _LIBCPP_HIDE_FROM_ABI
constexpr iterator
operator++(int) { return *this; }
130 _LIBCPP_HIDE_FROM_ABI
constexpr __arg_t
arg(size_t __id
) const {
132 std::__throw_format_error("The argument index value is too large for the number of arguments supplied");
133 return __args_
[__id
];
136 _LIBCPP_HIDE_FROM_ABI
constexpr const __compile_time_handle
<_CharT
>& __handle(size_t __id
) const {
138 std::__throw_format_error("The argument index value is too large for the number of arguments supplied");
139 return __handles_
[__id
];
142 _LIBCPP_HIDE_FROM_ABI
constexpr iterator
out() { return {}; }
143 _LIBCPP_HIDE_FROM_ABI
constexpr void advance_to(iterator
) {}
146 const __arg_t
* __args_
;
147 const __compile_time_handle
<_CharT
>* __handles_
;
151 // [format.string.std]/8
152 // If { arg-idopt } is used in a width or precision, the value of the
153 // corresponding formatting argument is used in its place. If the
154 // corresponding formatting argument is not of standard signed or unsigned
155 // integer type, or its value is negative for precision or non-positive for
156 // width, an exception of type format_error is thrown.
158 // _HasPrecision does the formatter have a precision?
159 template <class _CharT
, class _Tp
, bool _HasPrecision
= false>
160 _LIBCPP_HIDE_FROM_ABI
constexpr void __compile_time_validate_argument(
161 basic_format_parse_context
<_CharT
>& __parse_ctx
, __compile_time_basic_format_context
<_CharT
>& __ctx
) {
162 auto __validate_type
= [](__arg_t __type
) {
163 // LWG3720 originally allowed "signed or unsigned integer types", however
164 // the final version explicitly changed it to "*standard* signed or unsigned
165 // integer types". It's trivial to use 128-bit integrals in libc++'s
166 // implementation, but other implementations may not implement it.
167 // (Using a width or precision, that does not fit in 64-bits, sounds very
168 // unlikely in real world code.)
171 case __arg_t::__long_long
:
172 case __arg_t::__unsigned
:
173 case __arg_t::__unsigned_long_long
:
177 std::__throw_format_error("Replacement argument isn't a standard signed or unsigned integer type");
181 formatter
<_Tp
, _CharT
> __formatter
;
182 __parse_ctx
.advance_to(__formatter
.parse(__parse_ctx
));
183 if (__formatter
.__parser_
.__width_as_arg_
)
184 __validate_type(__ctx
.arg(__formatter
.__parser_
.__width_
));
186 if constexpr (_HasPrecision
)
187 if (__formatter
.__parser_
.__precision_as_arg_
)
188 __validate_type(__ctx
.arg(__formatter
.__parser_
.__precision_
));
191 // This function is not user facing, so it can directly use the non-standard types of the "variant".
192 template <class _CharT
>
193 _LIBCPP_HIDE_FROM_ABI
constexpr void __compile_time_visit_format_arg(
194 basic_format_parse_context
<_CharT
>& __parse_ctx
,
195 __compile_time_basic_format_context
<_CharT
>& __ctx
,
198 case __arg_t::__none
:
199 std::__throw_format_error("Invalid argument");
200 case __arg_t::__boolean
:
201 return __format::__compile_time_validate_argument
<_CharT
, bool>(__parse_ctx
, __ctx
);
202 case __arg_t::__char_type
:
203 return __format::__compile_time_validate_argument
<_CharT
, _CharT
>(__parse_ctx
, __ctx
);
205 return __format::__compile_time_validate_argument
<_CharT
, int>(__parse_ctx
, __ctx
);
206 case __arg_t::__long_long
:
207 return __format::__compile_time_validate_argument
<_CharT
, long long>(__parse_ctx
, __ctx
);
208 case __arg_t::__i128
:
209 # if _LIBCPP_HAS_INT128
210 return __format::__compile_time_validate_argument
<_CharT
, __int128_t
>(__parse_ctx
, __ctx
);
212 std::__throw_format_error("Invalid argument");
215 case __arg_t::__unsigned
:
216 return __format::__compile_time_validate_argument
<_CharT
, unsigned>(__parse_ctx
, __ctx
);
217 case __arg_t::__unsigned_long_long
:
218 return __format::__compile_time_validate_argument
<_CharT
, unsigned long long>(__parse_ctx
, __ctx
);
219 case __arg_t::__u128
:
220 # if _LIBCPP_HAS_INT128
221 return __format::__compile_time_validate_argument
<_CharT
, __uint128_t
>(__parse_ctx
, __ctx
);
223 std::__throw_format_error("Invalid argument");
226 case __arg_t::__float
:
227 return __format::__compile_time_validate_argument
<_CharT
, float, true>(__parse_ctx
, __ctx
);
228 case __arg_t::__double
:
229 return __format::__compile_time_validate_argument
<_CharT
, double, true>(__parse_ctx
, __ctx
);
230 case __arg_t::__long_double
:
231 return __format::__compile_time_validate_argument
<_CharT
, long double, true>(__parse_ctx
, __ctx
);
232 case __arg_t::__const_char_type_ptr
:
233 return __format::__compile_time_validate_argument
<_CharT
, const _CharT
*, true>(__parse_ctx
, __ctx
);
234 case __arg_t::__string_view
:
235 return __format::__compile_time_validate_argument
<_CharT
, basic_string_view
<_CharT
>, true>(__parse_ctx
, __ctx
);
237 return __format::__compile_time_validate_argument
<_CharT
, const void*>(__parse_ctx
, __ctx
);
238 case __arg_t::__handle
:
239 std::__throw_format_error("Handle should use __compile_time_validate_handle_argument");
241 std::__throw_format_error("Invalid argument");
244 template <contiguous_iterator _Iterator
, class _ParseCtx
, class _Ctx
>
245 _LIBCPP_HIDE_FROM_ABI
constexpr _Iterator
246 __handle_replacement_field(_Iterator __begin
, _Iterator __end
, _ParseCtx
& __parse_ctx
, _Ctx
& __ctx
) {
247 using _CharT
= iter_value_t
<_Iterator
>;
248 __format::__parse_number_result __r
= __format::__parse_arg_id(__begin
, __end
, __parse_ctx
);
250 if (__r
.__last
== __end
)
251 std::__throw_format_error("The argument index should end with a ':' or a '}'");
253 bool __parse
= *__r
.__last
== _CharT(':');
254 switch (*__r
.__last
) {
256 // The arg-id has a format-specifier, advance the input to the format-spec.
257 __parse_ctx
.advance_to(__r
.__last
+ 1);
260 // The arg-id has no format-specifier.
261 __parse_ctx
.advance_to(__r
.__last
);
264 std::__throw_format_error("The argument index should end with a ':' or a '}'");
267 if constexpr (same_as
<_Ctx
, __compile_time_basic_format_context
<_CharT
>>) {
268 __arg_t __type
= __ctx
.arg(__r
.__value
);
269 if (__type
== __arg_t::__none
)
270 std::__throw_format_error("The argument index value is too large for the number of arguments supplied");
271 else if (__type
== __arg_t::__handle
)
272 __ctx
.__handle(__r
.__value
).__parse(__parse_ctx
);
274 __format::__compile_time_visit_format_arg(__parse_ctx
, __ctx
, __type
);
276 std::__visit_format_arg(
278 if constexpr (same_as
<decltype(__arg
), monostate
>)
279 std::__throw_format_error("The argument index value is too large for the number of arguments supplied");
280 else if constexpr (same_as
<decltype(__arg
), typename basic_format_arg
<_Ctx
>::handle
>)
281 __arg
.format(__parse_ctx
, __ctx
);
283 formatter
<decltype(__arg
), _CharT
> __formatter
;
285 __parse_ctx
.advance_to(__formatter
.parse(__parse_ctx
));
286 __ctx
.advance_to(__formatter
.format(__arg
, __ctx
));
289 __ctx
.arg(__r
.__value
));
291 __begin
= __parse_ctx
.begin();
292 if (__begin
== __end
|| *__begin
!= _CharT('}'))
293 std::__throw_format_error("The replacement field misses a terminating '}'");
298 template <class _ParseCtx
, class _Ctx
>
299 _LIBCPP_HIDE_FROM_ABI
constexpr typename
_Ctx::iterator
__vformat_to(_ParseCtx
&& __parse_ctx
, _Ctx
&& __ctx
) {
300 using _CharT
= typename
_ParseCtx::char_type
;
301 static_assert(same_as
<typename
_Ctx::char_type
, _CharT
>);
303 auto __begin
= __parse_ctx
.begin();
304 auto __end
= __parse_ctx
.end();
305 typename
_Ctx::iterator __out_it
= __ctx
.out();
306 while (__begin
!= __end
) {
310 if (__begin
== __end
)
311 std::__throw_format_error("The format string terminates at a '{'");
313 if (*__begin
!= _CharT('{')) [[likely
]] {
314 __ctx
.advance_to(std::move(__out_it
));
315 __begin
= __format::__handle_replacement_field(__begin
, __end
, __parse_ctx
, __ctx
);
316 __out_it
= __ctx
.out();
318 // The output is written and __begin points to the next character. So
319 // start the next iteration.
322 // The string is an escape character.
327 if (__begin
== __end
|| *__begin
!= _CharT('}'))
328 std::__throw_format_error("The format string contains an invalid escape sequence");
333 // Copy the character to the output verbatim.
334 *__out_it
++ = *__begin
++;
339 } // namespace __format
341 # if _LIBCPP_STD_VER >= 26
342 template <class _CharT
>
343 struct _LIBCPP_TEMPLATE_VIS __runtime_format_string
{
345 basic_string_view
<_CharT
> __str_
;
347 template <class _Cp
, class... _Args
>
348 friend struct _LIBCPP_TEMPLATE_VIS basic_format_string
;
351 _LIBCPP_HIDE_FROM_ABI
__runtime_format_string(basic_string_view
<_CharT
> __s
) noexcept
: __str_(__s
) {}
353 __runtime_format_string(const __runtime_format_string
&) = delete;
354 __runtime_format_string
& operator=(const __runtime_format_string
&) = delete;
357 _LIBCPP_HIDE_FROM_ABI
inline __runtime_format_string
<char> runtime_format(string_view __fmt
) noexcept
{ return __fmt
; }
358 # if _LIBCPP_HAS_WIDE_CHARACTERS
359 _LIBCPP_HIDE_FROM_ABI
inline __runtime_format_string
<wchar_t> runtime_format(wstring_view __fmt
) noexcept
{
363 # endif // _LIBCPP_STD_VER >= 26
365 template <class _CharT
, class... _Args
>
366 struct _LIBCPP_TEMPLATE_VIS basic_format_string
{
368 requires convertible_to
<const _Tp
&, basic_string_view
<_CharT
>>
369 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
)});
374 _LIBCPP_HIDE_FROM_ABI
constexpr basic_string_view
<_CharT
> get() const noexcept
{ return __str_
; }
375 # if _LIBCPP_STD_VER >= 26
376 _LIBCPP_HIDE_FROM_ABI
basic_format_string(__runtime_format_string
<_CharT
> __s
) noexcept
: __str_(__s
.__str_
) {}
380 basic_string_view
<_CharT
> __str_
;
382 using _Context
= __format::__compile_time_basic_format_context
<_CharT
>;
384 static constexpr array
<__format::__arg_t
, sizeof...(_Args
)> __types_
{
385 __format::__determine_arg_t
<_Context
, remove_cvref_t
<_Args
>>()...};
387 static constexpr array
<__format::__compile_time_handle
<_CharT
>, sizeof...(_Args
)> __handles_
{[] {
388 using _Tp
= remove_cvref_t
<_Args
>;
389 __format::__compile_time_handle
<_CharT
> __handle
;
390 if (__format::__determine_arg_t
<_Context
, _Tp
>() == __format::__arg_t::__handle
)
391 __handle
.template __enable
<_Tp
>();
397 template <class... _Args
>
398 using format_string
= basic_format_string
<char, type_identity_t
<_Args
>...>;
400 # if _LIBCPP_HAS_WIDE_CHARACTERS
401 template <class... _Args
>
402 using wformat_string
= basic_format_string
<wchar_t, type_identity_t
<_Args
>...>;
405 template <class _OutIt
, class _CharT
, class _FormatOutIt
>
406 requires(output_iterator
<_OutIt
, const _CharT
&>)
407 _LIBCPP_HIDE_FROM_ABI _OutIt
__vformat_to(_OutIt __out_it
,
408 basic_string_view
<_CharT
> __fmt
,
409 basic_format_args
<basic_format_context
<_FormatOutIt
, _CharT
>> __args
) {
410 if constexpr (same_as
<_OutIt
, _FormatOutIt
>)
411 return std::__format::__vformat_to(
412 basic_format_parse_context
{__fmt
, __args
.__size()}, std::__format_context_create(std::move(__out_it
), __args
));
414 __format::__format_buffer
<_OutIt
, _CharT
> __buffer
{std::move(__out_it
)};
415 std::__format::__vformat_to(basic_format_parse_context
{__fmt
, __args
.__size()},
416 std::__format_context_create(__buffer
.__make_output_iterator(), __args
));
417 return std::move(__buffer
).__out_it();
421 // The function is _LIBCPP_ALWAYS_INLINE since the compiler is bad at inlining
422 // https://reviews.llvm.org/D110499#inline-1180704
423 // TODO FMT Evaluate whether we want to file a Clang bug report regarding this.
424 template <output_iterator
<const char&> _OutIt
>
425 _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _OutIt
vformat_to(_OutIt __out_it
, string_view __fmt
, format_args __args
) {
426 return std::__vformat_to(std::move(__out_it
), __fmt
, __args
);
429 # if _LIBCPP_HAS_WIDE_CHARACTERS
430 template <output_iterator
<const wchar_t&> _OutIt
>
431 _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _OutIt
432 vformat_to(_OutIt __out_it
, wstring_view __fmt
, wformat_args __args
) {
433 return std::__vformat_to(std::move(__out_it
), __fmt
, __args
);
437 template <output_iterator
<const char&> _OutIt
, class... _Args
>
438 _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _OutIt
439 format_to(_OutIt __out_it
, format_string
<_Args
...> __fmt
, _Args
&&... __args
) {
440 return std::vformat_to(std::move(__out_it
), __fmt
.get(), std::make_format_args(__args
...));
443 # if _LIBCPP_HAS_WIDE_CHARACTERS
444 template <output_iterator
<const wchar_t&> _OutIt
, class... _Args
>
445 _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _OutIt
446 format_to(_OutIt __out_it
, wformat_string
<_Args
...> __fmt
, _Args
&&... __args
) {
447 return std::vformat_to(std::move(__out_it
), __fmt
.get(), std::make_wformat_args(__args
...));
451 // TODO FMT This needs to be a template or std::to_chars(floating-point) availability markup
452 // fires too eagerly, see http://llvm.org/PR61563.
453 template <class = void>
454 [[nodiscard
]] _LIBCPP_ALWAYS_INLINE
inline _LIBCPP_HIDE_FROM_ABI string
vformat(string_view __fmt
, format_args __args
) {
456 std::vformat_to(std::back_inserter(__res
), __fmt
, __args
);
460 # if _LIBCPP_HAS_WIDE_CHARACTERS
461 // TODO FMT This needs to be a template or std::to_chars(floating-point) availability markup
462 // fires too eagerly, see http://llvm.org/PR61563.
463 template <class = void>
464 [[nodiscard
]] _LIBCPP_ALWAYS_INLINE
inline _LIBCPP_HIDE_FROM_ABI wstring
465 vformat(wstring_view __fmt
, wformat_args __args
) {
467 std::vformat_to(std::back_inserter(__res
), __fmt
, __args
);
472 template <class... _Args
>
473 [[nodiscard
]] _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI string
474 format(format_string
<_Args
...> __fmt
, _Args
&&... __args
) {
475 return std::vformat(__fmt
.get(), std::make_format_args(__args
...));
478 # if _LIBCPP_HAS_WIDE_CHARACTERS
479 template <class... _Args
>
480 [[nodiscard
]] _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI wstring
481 format(wformat_string
<_Args
...> __fmt
, _Args
&&... __args
) {
482 return std::vformat(__fmt
.get(), std::make_wformat_args(__args
...));
486 template <class _Context
, class _OutIt
, class _CharT
>
487 _LIBCPP_HIDE_FROM_ABI format_to_n_result
<_OutIt
>
488 __vformat_to_n(_OutIt __out_it
,
489 iter_difference_t
<_OutIt
> __n
,
490 basic_string_view
<_CharT
> __fmt
,
491 basic_format_args
<_Context
> __args
) {
492 __format::__format_to_n_buffer
<_OutIt
, _CharT
> __buffer
{std::move(__out_it
), __n
};
493 std::__format::__vformat_to(basic_format_parse_context
{__fmt
, __args
.__size()},
494 std::__format_context_create(__buffer
.__make_output_iterator(), __args
));
495 return std::move(__buffer
).__result();
498 template <output_iterator
<const char&> _OutIt
, class... _Args
>
499 _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI format_to_n_result
<_OutIt
>
500 format_to_n(_OutIt __out_it
, iter_difference_t
<_OutIt
> __n
, format_string
<_Args
...> __fmt
, _Args
&&... __args
) {
501 return std::__vformat_to_n
<format_context
>(std::move(__out_it
), __n
, __fmt
.get(), std::make_format_args(__args
...));
504 # if _LIBCPP_HAS_WIDE_CHARACTERS
505 template <output_iterator
<const wchar_t&> _OutIt
, class... _Args
>
506 _LIBCPP_HIDE_FROM_ABI format_to_n_result
<_OutIt
>
507 format_to_n(_OutIt __out_it
, iter_difference_t
<_OutIt
> __n
, wformat_string
<_Args
...> __fmt
, _Args
&&... __args
) {
508 return std::__vformat_to_n
<wformat_context
>(std::move(__out_it
), __n
, __fmt
.get(), std::make_wformat_args(__args
...));
512 template <class _CharT
>
513 _LIBCPP_HIDE_FROM_ABI
size_t __vformatted_size(basic_string_view
<_CharT
> __fmt
, auto __args
) {
514 __format::__formatted_size_buffer
<_CharT
> __buffer
;
515 std::__format::__vformat_to(basic_format_parse_context
{__fmt
, __args
.__size()},
516 std::__format_context_create(__buffer
.__make_output_iterator(), __args
));
517 return std::move(__buffer
).__result();
520 template <class... _Args
>
521 [[nodiscard
]] _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI
size_t
522 formatted_size(format_string
<_Args
...> __fmt
, _Args
&&... __args
) {
523 return std::__vformatted_size(__fmt
.get(), basic_format_args
{std::make_format_args(__args
...)});
526 # if _LIBCPP_HAS_WIDE_CHARACTERS
527 template <class... _Args
>
528 [[nodiscard
]] _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI
size_t
529 formatted_size(wformat_string
<_Args
...> __fmt
, _Args
&&... __args
) {
530 return std::__vformatted_size(__fmt
.get(), basic_format_args
{std::make_wformat_args(__args
...)});
534 # if _LIBCPP_HAS_LOCALIZATION
536 template <class _OutIt
, class _CharT
, class _FormatOutIt
>
537 requires(output_iterator
<_OutIt
, const _CharT
&>)
538 _LIBCPP_HIDE_FROM_ABI _OutIt
__vformat_to(
541 basic_string_view
<_CharT
> __fmt
,
542 basic_format_args
<basic_format_context
<_FormatOutIt
, _CharT
>> __args
) {
543 if constexpr (same_as
<_OutIt
, _FormatOutIt
>)
544 return std::__format::__vformat_to(basic_format_parse_context
{__fmt
, __args
.__size()},
545 std::__format_context_create(std::move(__out_it
), __args
, std::move(__loc
)));
547 __format::__format_buffer
<_OutIt
, _CharT
> __buffer
{std::move(__out_it
)};
548 std::__format::__vformat_to(
549 basic_format_parse_context
{__fmt
, __args
.__size()},
550 std::__format_context_create(__buffer
.__make_output_iterator(), __args
, std::move(__loc
)));
551 return std::move(__buffer
).__out_it();
555 template <output_iterator
<const char&> _OutIt
>
556 _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _OutIt
557 vformat_to(_OutIt __out_it
, locale __loc
, string_view __fmt
, format_args __args
) {
558 return std::__vformat_to(std::move(__out_it
), std::move(__loc
), __fmt
, __args
);
561 # if _LIBCPP_HAS_WIDE_CHARACTERS
562 template <output_iterator
<const wchar_t&> _OutIt
>
563 _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _OutIt
564 vformat_to(_OutIt __out_it
, locale __loc
, wstring_view __fmt
, wformat_args __args
) {
565 return std::__vformat_to(std::move(__out_it
), std::move(__loc
), __fmt
, __args
);
569 template <output_iterator
<const char&> _OutIt
, class... _Args
>
570 _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _OutIt
571 format_to(_OutIt __out_it
, locale __loc
, format_string
<_Args
...> __fmt
, _Args
&&... __args
) {
572 return std::vformat_to(std::move(__out_it
), std::move(__loc
), __fmt
.get(), std::make_format_args(__args
...));
575 # if _LIBCPP_HAS_WIDE_CHARACTERS
576 template <output_iterator
<const wchar_t&> _OutIt
, class... _Args
>
577 _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _OutIt
578 format_to(_OutIt __out_it
, locale __loc
, wformat_string
<_Args
...> __fmt
, _Args
&&... __args
) {
579 return std::vformat_to(std::move(__out_it
), std::move(__loc
), __fmt
.get(), std::make_wformat_args(__args
...));
583 // TODO FMT This needs to be a template or std::to_chars(floating-point) availability markup
584 // fires too eagerly, see http://llvm.org/PR61563.
585 template <class = void>
586 [[nodiscard
]] _LIBCPP_ALWAYS_INLINE
inline _LIBCPP_HIDE_FROM_ABI string
587 vformat(locale __loc
, string_view __fmt
, format_args __args
) {
589 std::vformat_to(std::back_inserter(__res
), std::move(__loc
), __fmt
, __args
);
593 # if _LIBCPP_HAS_WIDE_CHARACTERS
594 // TODO FMT This needs to be a template or std::to_chars(floating-point) availability markup
595 // fires too eagerly, see http://llvm.org/PR61563.
596 template <class = void>
597 [[nodiscard
]] _LIBCPP_ALWAYS_INLINE
inline _LIBCPP_HIDE_FROM_ABI wstring
598 vformat(locale __loc
, wstring_view __fmt
, wformat_args __args
) {
600 std::vformat_to(std::back_inserter(__res
), std::move(__loc
), __fmt
, __args
);
605 template <class... _Args
>
606 [[nodiscard
]] _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI string
607 format(locale __loc
, format_string
<_Args
...> __fmt
, _Args
&&... __args
) {
608 return std::vformat(std::move(__loc
), __fmt
.get(), std::make_format_args(__args
...));
611 # if _LIBCPP_HAS_WIDE_CHARACTERS
612 template <class... _Args
>
613 [[nodiscard
]] _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI wstring
614 format(locale __loc
, wformat_string
<_Args
...> __fmt
, _Args
&&... __args
) {
615 return std::vformat(std::move(__loc
), __fmt
.get(), std::make_wformat_args(__args
...));
619 template <class _Context
, class _OutIt
, class _CharT
>
620 _LIBCPP_HIDE_FROM_ABI format_to_n_result
<_OutIt
> __vformat_to_n(
622 iter_difference_t
<_OutIt
> __n
,
624 basic_string_view
<_CharT
> __fmt
,
625 basic_format_args
<_Context
> __args
) {
626 __format::__format_to_n_buffer
<_OutIt
, _CharT
> __buffer
{std::move(__out_it
), __n
};
627 std::__format::__vformat_to(
628 basic_format_parse_context
{__fmt
, __args
.__size()},
629 std::__format_context_create(__buffer
.__make_output_iterator(), __args
, std::move(__loc
)));
630 return std::move(__buffer
).__result();
633 template <output_iterator
<const char&> _OutIt
, class... _Args
>
634 _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI format_to_n_result
<_OutIt
> format_to_n(
635 _OutIt __out_it
, iter_difference_t
<_OutIt
> __n
, locale __loc
, format_string
<_Args
...> __fmt
, _Args
&&... __args
) {
636 return std::__vformat_to_n
<format_context
>(
637 std::move(__out_it
), __n
, std::move(__loc
), __fmt
.get(), std::make_format_args(__args
...));
640 # if _LIBCPP_HAS_WIDE_CHARACTERS
641 template <output_iterator
<const wchar_t&> _OutIt
, class... _Args
>
642 _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI format_to_n_result
<_OutIt
> format_to_n(
643 _OutIt __out_it
, iter_difference_t
<_OutIt
> __n
, locale __loc
, wformat_string
<_Args
...> __fmt
, _Args
&&... __args
) {
644 return std::__vformat_to_n
<wformat_context
>(
645 std::move(__out_it
), __n
, std::move(__loc
), __fmt
.get(), std::make_wformat_args(__args
...));
649 template <class _CharT
>
650 _LIBCPP_HIDE_FROM_ABI
size_t __vformatted_size(locale __loc
, basic_string_view
<_CharT
> __fmt
, auto __args
) {
651 __format::__formatted_size_buffer
<_CharT
> __buffer
;
652 std::__format::__vformat_to(
653 basic_format_parse_context
{__fmt
, __args
.__size()},
654 std::__format_context_create(__buffer
.__make_output_iterator(), __args
, std::move(__loc
)));
655 return std::move(__buffer
).__result();
658 template <class... _Args
>
659 [[nodiscard
]] _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI
size_t
660 formatted_size(locale __loc
, format_string
<_Args
...> __fmt
, _Args
&&... __args
) {
661 return std::__vformatted_size(std::move(__loc
), __fmt
.get(), basic_format_args
{std::make_format_args(__args
...)});
664 # if _LIBCPP_HAS_WIDE_CHARACTERS
665 template <class... _Args
>
666 [[nodiscard
]] _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI
size_t
667 formatted_size(locale __loc
, wformat_string
<_Args
...> __fmt
, _Args
&&... __args
) {
668 return std::__vformatted_size(std::move(__loc
), __fmt
.get(), basic_format_args
{std::make_wformat_args(__args
...)});
672 # endif // _LIBCPP_HAS_LOCALIZATION
674 #endif // _LIBCPP_STD_VER >= 20
676 _LIBCPP_END_NAMESPACE_STD
680 #endif // _LIBCPP___FORMAT_FORMAT_FUNCTIONS