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 #ifndef _LIBCPP_HAS_NO_LOCALIZATION
47 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
48 # pragma GCC system_header
51 _LIBCPP_BEGIN_NAMESPACE_STD
53 #if _LIBCPP_STD_VER >= 20
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.
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
>;
65 template <class _Context
= format_context
, class... _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
...);
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
...>
73 make_wformat_args(_Args
&&... __args
) {
74 return _VSTD::__format_arg_store
<wformat_context
, _Args
...>(__args
...);
80 /// Helper class parse and handle argument.
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
{
87 template <class _ParseContext
>
88 _LIBCPP_HIDE_FROM_ABI
constexpr void __parse(_ParseContext
& __ctx
) const {
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
));
100 // Before calling __parse the proper handler needs to be set with __enable.
101 // The default handler isn't a core constant expression.
102 _LIBCPP_HIDE_FROM_ABI
constexpr __compile_time_handle()
103 : __parse_([](basic_format_parse_context
<_CharT
>&) { std::__throw_format_error("Not a handle"); }) {}
106 void (*__parse_
)(basic_format_parse_context
<_CharT
>&);
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
{
114 using char_type
= _CharT
;
116 _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
) {}
120 // During the compile-time validation nothing needs to be written.
121 // Therefore all operations of this iterator are a NOP.
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; }
128 _LIBCPP_HIDE_FROM_ABI
constexpr __arg_t
arg(size_t __id
) const {
130 std::__throw_format_error("The argument index value is too large for the number of arguments supplied");
131 return __args_
[__id
];
134 _LIBCPP_HIDE_FROM_ABI
constexpr const __compile_time_handle
<_CharT
>& __handle(size_t __id
) const {
136 std::__throw_format_error("The argument index value is too large for the number of arguments supplied");
137 return __handles_
[__id
];
140 _LIBCPP_HIDE_FROM_ABI
constexpr iterator
out() { return {}; }
141 _LIBCPP_HIDE_FROM_ABI
constexpr void advance_to(iterator
) {}
144 const __arg_t
* __args_
;
145 const __compile_time_handle
<_CharT
>* __handles_
;
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.
156 // _HasPrecision does the formatter have a precision?
157 template <class _CharT
, class _Tp
, bool _HasPrecision
= false>
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.)
169 case __arg_t::__long_long
:
170 case __arg_t::__unsigned
:
171 case __arg_t::__unsigned_long_long
:
175 std::__throw_format_error("Replacement argument isn't a standard signed or unsigned integer type");
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_
));
184 if constexpr (_HasPrecision
)
185 if (__formatter
.__parser_
.__precision_as_arg_
)
186 __validate_type(__ctx
.arg(__formatter
.__parser_
.__precision_
));
189 // This function is not user facing, so it can directly use the non-standard types of the "variant".
190 template <class _CharT
>
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
,
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
);
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
);
209 std::__throw_format_error("Invalid argument");
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
);
220 std::__throw_format_error("Invalid argument");
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
);
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");
238 std::__throw_format_error("Invalid argument");
241 template <contiguous_iterator _Iterator
, class _ParseCtx
, class _Ctx
>
242 _LIBCPP_HIDE_FROM_ABI
constexpr _Iterator
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
);
248 if (__r
.__last
== __end
)
249 std::__throw_format_error("The argument index should end with a ':' or a '}'");
251 bool __parse
= *__r
.__last
== _CharT(':');
252 switch (*__r
.__last
) {
254 // The arg-id has a format-specifier, advance the input to the format-spec.
255 __parse_ctx
.advance_to(__r
.__last
+ 1);
258 // The arg-id has no format-specifier.
259 __parse_ctx
.advance_to(__r
.__last
);
262 std::__throw_format_error("The argument index should end with a ':' or a '}'");
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
);
272 __format::__compile_time_visit_format_arg(__parse_ctx
, __ctx
, __type
);
274 _VSTD::__visit_format_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
);
281 formatter
<decltype(__arg
), _CharT
> __formatter
;
283 __parse_ctx
.advance_to(__formatter
.parse(__parse_ctx
));
284 __ctx
.advance_to(__formatter
.format(__arg
, __ctx
));
287 __ctx
.arg(__r
.__value
));
289 __begin
= __parse_ctx
.begin();
290 if (__begin
== __end
|| *__begin
!= _CharT('}'))
291 std::__throw_format_error("The replacement field misses a terminating '}'");
296 template <class _ParseCtx
, class _Ctx
>
297 _LIBCPP_HIDE_FROM_ABI
constexpr typename
_Ctx::iterator
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
>);
302 auto __begin
= __parse_ctx
.begin();
303 auto __end
= __parse_ctx
.end();
304 typename
_Ctx::iterator __out_it
= __ctx
.out();
305 while (__begin
!= __end
) {
309 if (__begin
== __end
)
310 std::__throw_format_error("The format string terminates at a '{'");
312 if (*__begin
!= _CharT('{')) [[likely
]] {
313 __ctx
.advance_to(_VSTD::move(__out_it
));
315 __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 template <class _CharT
, class... _Args
>
342 struct _LIBCPP_TEMPLATE_VIS basic_format_string
{
344 requires convertible_to
<const _Tp
&, basic_string_view
<_CharT
>>
345 consteval
basic_format_string(const _Tp
& __str
) : __str_
{__str
} {
346 __format::__vformat_to(basic_format_parse_context
<_CharT
>{__str_
, sizeof...(_Args
)},
347 _Context
{__types_
.data(), __handles_
.data(), sizeof...(_Args
)});
350 _LIBCPP_HIDE_FROM_ABI
constexpr basic_string_view
<_CharT
> get() const noexcept
{
355 basic_string_view
<_CharT
> __str_
;
357 using _Context
= __format::__compile_time_basic_format_context
<_CharT
>;
359 static constexpr array
<__format::__arg_t
, sizeof...(_Args
)> __types_
{
360 __format::__determine_arg_t
<_Context
, remove_cvref_t
<_Args
>>()...};
362 static constexpr array
<__format::__compile_time_handle
<_CharT
>, sizeof...(_Args
)> __handles_
{[] {
363 using _Tp
= remove_cvref_t
<_Args
>;
364 __format::__compile_time_handle
<_CharT
> __handle
;
365 if (__format::__determine_arg_t
<_Context
, _Tp
>() == __format::__arg_t::__handle
)
366 __handle
.template __enable
<_Tp
>();
372 template <class... _Args
>
373 using format_string
= basic_format_string
<char, type_identity_t
<_Args
>...>;
375 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
376 template <class... _Args
>
377 using wformat_string
= basic_format_string
<wchar_t, type_identity_t
<_Args
>...>;
380 template <class _OutIt
, class _CharT
, class _FormatOutIt
>
381 requires(output_iterator
<_OutIt
, const _CharT
&>) _LIBCPP_HIDE_FROM_ABI _OutIt
383 _OutIt __out_it
, basic_string_view
<_CharT
> __fmt
,
384 basic_format_args
<basic_format_context
<_FormatOutIt
, _CharT
>> __args
) {
385 if constexpr (same_as
<_OutIt
, _FormatOutIt
>)
386 return _VSTD::__format::__vformat_to(basic_format_parse_context
{__fmt
, __args
.__size()},
387 _VSTD::__format_context_create(_VSTD::move(__out_it
), __args
));
389 __format::__format_buffer
<_OutIt
, _CharT
> __buffer
{_VSTD::move(__out_it
)};
390 _VSTD::__format::__vformat_to(basic_format_parse_context
{__fmt
, __args
.__size()},
391 _VSTD::__format_context_create(__buffer
.__make_output_iterator(), __args
));
392 return _VSTD::move(__buffer
).__out_it();
396 // The function is _LIBCPP_ALWAYS_INLINE since the compiler is bad at inlining
397 // https://reviews.llvm.org/D110499#inline-1180704
398 // TODO FMT Evaluate whether we want to file a Clang bug report regarding this.
399 template <output_iterator
<const char&> _OutIt
>
400 _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _OutIt
401 vformat_to(_OutIt __out_it
, string_view __fmt
, format_args __args
) {
402 return _VSTD::__vformat_to(_VSTD::move(__out_it
), __fmt
, __args
);
405 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
406 template <output_iterator
<const wchar_t&> _OutIt
>
407 _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _OutIt
408 vformat_to(_OutIt __out_it
, wstring_view __fmt
, wformat_args __args
) {
409 return _VSTD::__vformat_to(_VSTD::move(__out_it
), __fmt
, __args
);
413 template <output_iterator
<const char&> _OutIt
, class... _Args
>
414 _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _OutIt
415 format_to(_OutIt __out_it
, format_string
<_Args
...> __fmt
, _Args
&&... __args
) {
416 return _VSTD::vformat_to(_VSTD::move(__out_it
), __fmt
.get(),
417 _VSTD::make_format_args(__args
...));
420 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
421 template <output_iterator
<const wchar_t&> _OutIt
, class... _Args
>
422 _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _OutIt
423 format_to(_OutIt __out_it
, wformat_string
<_Args
...> __fmt
, _Args
&&... __args
) {
424 return _VSTD::vformat_to(_VSTD::move(__out_it
), __fmt
.get(),
425 _VSTD::make_wformat_args(__args
...));
429 // TODO FMT This needs to be a template or std::to_chars(floating-point) availability markup
430 // fires too eagerly, see http://llvm.org/PR61563.
431 template <class = void>
432 _LIBCPP_NODISCARD_EXT _LIBCPP_ALWAYS_INLINE
inline _LIBCPP_HIDE_FROM_ABI string
433 vformat(string_view __fmt
, format_args __args
) {
435 _VSTD::vformat_to(_VSTD::back_inserter(__res
), __fmt
, __args
);
439 # ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
440 // TODO FMT This needs to be a template or std::to_chars(floating-point) availability markup
441 // fires too eagerly, see http://llvm.org/PR61563.
442 template <class = void>
443 _LIBCPP_NODISCARD_EXT _LIBCPP_ALWAYS_INLINE
inline _LIBCPP_HIDE_FROM_ABI wstring
444 vformat(wstring_view __fmt
, wformat_args __args
) {
446 _VSTD::vformat_to(_VSTD::back_inserter(__res
), __fmt
, __args
);
451 template <class... _Args
>
452 _LIBCPP_NODISCARD_EXT _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI string
453 format(format_string
<_Args
...> __fmt
, _Args
&&... __args
) {
454 return _VSTD::vformat(__fmt
.get(), _VSTD::make_format_args(__args
...));
457 # ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
458 template <class... _Args
>
459 _LIBCPP_NODISCARD_EXT _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI wstring
460 format(wformat_string
<_Args
...> __fmt
, _Args
&&... __args
) {
461 return _VSTD::vformat(__fmt
.get(), _VSTD::make_wformat_args(__args
...));
465 template <class _Context
, class _OutIt
, class _CharT
>
466 _LIBCPP_HIDE_FROM_ABI format_to_n_result
<_OutIt
> __vformat_to_n(_OutIt __out_it
, iter_difference_t
<_OutIt
> __n
,
467 basic_string_view
<_CharT
> __fmt
,
468 basic_format_args
<_Context
> __args
) {
469 __format::__format_to_n_buffer
<_OutIt
, _CharT
> __buffer
{_VSTD::move(__out_it
), __n
};
470 _VSTD::__format::__vformat_to(basic_format_parse_context
{__fmt
, __args
.__size()},
471 _VSTD::__format_context_create(__buffer
.__make_output_iterator(), __args
));
472 return _VSTD::move(__buffer
).__result();
475 template <output_iterator
<const char&> _OutIt
, class... _Args
>
476 _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI format_to_n_result
<_OutIt
>
477 format_to_n(_OutIt __out_it
, iter_difference_t
<_OutIt
> __n
, format_string
<_Args
...> __fmt
, _Args
&&... __args
) {
478 return _VSTD::__vformat_to_n
<format_context
>(_VSTD::move(__out_it
), __n
, __fmt
.get(), _VSTD::make_format_args(__args
...));
481 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
482 template <output_iterator
<const wchar_t&> _OutIt
, class... _Args
>
483 _LIBCPP_HIDE_FROM_ABI format_to_n_result
<_OutIt
>
484 format_to_n(_OutIt __out_it
, iter_difference_t
<_OutIt
> __n
, wformat_string
<_Args
...> __fmt
,
486 return _VSTD::__vformat_to_n
<wformat_context
>(_VSTD::move(__out_it
), __n
, __fmt
.get(), _VSTD::make_wformat_args(__args
...));
490 template <class _CharT
>
491 _LIBCPP_HIDE_FROM_ABI
size_t __vformatted_size(basic_string_view
<_CharT
> __fmt
, auto __args
) {
492 __format::__formatted_size_buffer
<_CharT
> __buffer
;
493 _VSTD::__format::__vformat_to(basic_format_parse_context
{__fmt
, __args
.__size()},
494 _VSTD::__format_context_create(__buffer
.__make_output_iterator(), __args
));
495 return _VSTD::move(__buffer
).__result();
498 template <class... _Args
>
499 _LIBCPP_NODISCARD_EXT _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI
size_t
500 formatted_size(format_string
<_Args
...> __fmt
, _Args
&&... __args
) {
501 return _VSTD::__vformatted_size(__fmt
.get(), basic_format_args
{_VSTD::make_format_args(__args
...)});
504 # ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
505 template <class... _Args
>
506 _LIBCPP_NODISCARD_EXT _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI
size_t
507 formatted_size(wformat_string
<_Args
...> __fmt
, _Args
&&... __args
) {
508 return _VSTD::__vformatted_size(__fmt
.get(), basic_format_args
{_VSTD::make_wformat_args(__args
...)});
512 # ifndef _LIBCPP_HAS_NO_LOCALIZATION
514 template <class _OutIt
, class _CharT
, class _FormatOutIt
>
515 requires(output_iterator
<_OutIt
, const _CharT
&>) _LIBCPP_HIDE_FROM_ABI _OutIt
517 _OutIt __out_it
, locale __loc
, basic_string_view
<_CharT
> __fmt
,
518 basic_format_args
<basic_format_context
<_FormatOutIt
, _CharT
>> __args
) {
519 if constexpr (same_as
<_OutIt
, _FormatOutIt
>)
520 return _VSTD::__format::__vformat_to(
521 basic_format_parse_context
{__fmt
, __args
.__size()},
522 _VSTD::__format_context_create(_VSTD::move(__out_it
), __args
, _VSTD::move(__loc
)));
524 __format::__format_buffer
<_OutIt
, _CharT
> __buffer
{_VSTD::move(__out_it
)};
525 _VSTD::__format::__vformat_to(
526 basic_format_parse_context
{__fmt
, __args
.__size()},
527 _VSTD::__format_context_create(__buffer
.__make_output_iterator(), __args
, _VSTD::move(__loc
)));
528 return _VSTD::move(__buffer
).__out_it();
532 template <output_iterator
<const char&> _OutIt
>
533 _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _OutIt
vformat_to(
534 _OutIt __out_it
, locale __loc
, string_view __fmt
, format_args __args
) {
535 return _VSTD::__vformat_to(_VSTD::move(__out_it
), _VSTD::move(__loc
), __fmt
,
539 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
540 template <output_iterator
<const wchar_t&> _OutIt
>
541 _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _OutIt
vformat_to(
542 _OutIt __out_it
, locale __loc
, wstring_view __fmt
, wformat_args __args
) {
543 return _VSTD::__vformat_to(_VSTD::move(__out_it
), _VSTD::move(__loc
), __fmt
,
548 template <output_iterator
<const char&> _OutIt
, class... _Args
>
549 _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _OutIt
550 format_to(_OutIt __out_it
, locale __loc
, format_string
<_Args
...> __fmt
, _Args
&&... __args
) {
551 return _VSTD::vformat_to(_VSTD::move(__out_it
), _VSTD::move(__loc
), __fmt
.get(),
552 _VSTD::make_format_args(__args
...));
555 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
556 template <output_iterator
<const wchar_t&> _OutIt
, class... _Args
>
557 _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _OutIt
558 format_to(_OutIt __out_it
, locale __loc
, wformat_string
<_Args
...> __fmt
, _Args
&&... __args
) {
559 return _VSTD::vformat_to(_VSTD::move(__out_it
), _VSTD::move(__loc
), __fmt
.get(),
560 _VSTD::make_wformat_args(__args
...));
564 // TODO FMT This needs to be a template or std::to_chars(floating-point) availability markup
565 // fires too eagerly, see http://llvm.org/PR61563.
566 template <class = void>
567 _LIBCPP_NODISCARD_EXT _LIBCPP_ALWAYS_INLINE
inline _LIBCPP_HIDE_FROM_ABI string
568 vformat(locale __loc
, string_view __fmt
, format_args __args
) {
570 _VSTD::vformat_to(_VSTD::back_inserter(__res
), _VSTD::move(__loc
), __fmt
,
575 # ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
576 // TODO FMT This needs to be a template or std::to_chars(floating-point) availability markup
577 // fires too eagerly, see http://llvm.org/PR61563.
578 template <class = void>
579 _LIBCPP_NODISCARD_EXT _LIBCPP_ALWAYS_INLINE
inline _LIBCPP_HIDE_FROM_ABI wstring
580 vformat(locale __loc
, wstring_view __fmt
, wformat_args __args
) {
582 _VSTD::vformat_to(_VSTD::back_inserter(__res
), _VSTD::move(__loc
), __fmt
,
588 template <class... _Args
>
589 _LIBCPP_NODISCARD_EXT _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI string
590 format(locale __loc
, format_string
<_Args
...> __fmt
, _Args
&&... __args
) {
591 return _VSTD::vformat(_VSTD::move(__loc
), __fmt
.get(),
592 _VSTD::make_format_args(__args
...));
595 # ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
596 template <class... _Args
>
597 _LIBCPP_NODISCARD_EXT _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI wstring
598 format(locale __loc
, wformat_string
<_Args
...> __fmt
, _Args
&&... __args
) {
599 return _VSTD::vformat(_VSTD::move(__loc
), __fmt
.get(),
600 _VSTD::make_wformat_args(__args
...));
604 template <class _Context
, class _OutIt
, class _CharT
>
605 _LIBCPP_HIDE_FROM_ABI format_to_n_result
<_OutIt
> __vformat_to_n(_OutIt __out_it
, iter_difference_t
<_OutIt
> __n
,
606 locale __loc
, basic_string_view
<_CharT
> __fmt
,
607 basic_format_args
<_Context
> __args
) {
608 __format::__format_to_n_buffer
<_OutIt
, _CharT
> __buffer
{_VSTD::move(__out_it
), __n
};
609 _VSTD::__format::__vformat_to(
610 basic_format_parse_context
{__fmt
, __args
.__size()},
611 _VSTD::__format_context_create(__buffer
.__make_output_iterator(), __args
, _VSTD::move(__loc
)));
612 return _VSTD::move(__buffer
).__result();
615 template <output_iterator
<const char&> _OutIt
, class... _Args
>
616 _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI format_to_n_result
<_OutIt
>
617 format_to_n(_OutIt __out_it
, iter_difference_t
<_OutIt
> __n
, locale __loc
, format_string
<_Args
...> __fmt
,
619 return _VSTD::__vformat_to_n
<format_context
>(_VSTD::move(__out_it
), __n
, _VSTD::move(__loc
), __fmt
.get(),
620 _VSTD::make_format_args(__args
...));
623 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
624 template <output_iterator
<const wchar_t&> _OutIt
, class... _Args
>
625 _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI format_to_n_result
<_OutIt
>
626 format_to_n(_OutIt __out_it
, iter_difference_t
<_OutIt
> __n
, locale __loc
, wformat_string
<_Args
...> __fmt
,
628 return _VSTD::__vformat_to_n
<wformat_context
>(_VSTD::move(__out_it
), __n
, _VSTD::move(__loc
), __fmt
.get(),
629 _VSTD::make_wformat_args(__args
...));
633 template <class _CharT
>
634 _LIBCPP_HIDE_FROM_ABI
size_t __vformatted_size(locale __loc
, basic_string_view
<_CharT
> __fmt
, auto __args
) {
635 __format::__formatted_size_buffer
<_CharT
> __buffer
;
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();
642 template <class... _Args
>
643 _LIBCPP_NODISCARD_EXT _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI
size_t
644 formatted_size(locale __loc
, format_string
<_Args
...> __fmt
, _Args
&&... __args
) {
645 return _VSTD::__vformatted_size(_VSTD::move(__loc
), __fmt
.get(), basic_format_args
{_VSTD::make_format_args(__args
...)});
648 # ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
649 template <class... _Args
>
650 _LIBCPP_NODISCARD_EXT _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI
size_t
651 formatted_size(locale __loc
, wformat_string
<_Args
...> __fmt
, _Args
&&... __args
) {
652 return _VSTD::__vformatted_size(_VSTD::move(__loc
), __fmt
.get(), basic_format_args
{_VSTD::make_wformat_args(__args
...)});
656 # endif // _LIBCPP_HAS_NO_LOCALIZATION
658 #endif //_LIBCPP_STD_VER >= 20
660 _LIBCPP_END_NAMESPACE_STD
662 #endif // _LIBCPP___FORMAT_FORMAT_FUNCTIONS