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