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 11#define _LIBCPP_FORMAT 12 13/* 14 15namespace std { 16 // [format.context], class template basic_format_context 17 template<class Out, class charT> class basic_format_context; 18 using format_context = basic_format_context<unspecified, char>; 19 using wformat_context = basic_format_context<unspecified, wchar_t>; 20 21 // [format.args], class template basic_format_args 22 template<class Context> class basic_format_args; 23 using format_args = basic_format_args<format_context>; 24 using wformat_args = basic_format_args<wformat_context>; 25 26 // [format.fmt.string], class template basic-format-string 27 template<class charT, class... Args> 28 struct basic-format-string; // exposition only 29 30 template<class... Args> 31 using format-string = // exposition only 32 basic-format-string<char, type_identity_t<Args>...>; 33 template<class... Args> 34 using wformat-string = // exposition only 35 basic-format-string<wchar_t, type_identity_t<Args>...>; 36 37 // [format.functions], formatting functions 38 template<class... Args> 39 string format(format-string<Args...> fmt, Args&&... args); 40 template<class... Args> 41 wstring format(wformat-string<Args...> fmt, Args&&... args); 42 template<class... Args> 43 string format(const locale& loc, format-string<Args...> fmt, Args&&... args); 44 template<class... Args> 45 wstring format(const locale& loc, wformat-string<Args...> fmt, Args&&... args); 46 47 string vformat(string_view fmt, format_args args); 48 wstring vformat(wstring_view fmt, wformat_args args); 49 string vformat(const locale& loc, string_view fmt, format_args args); 50 wstring vformat(const locale& loc, wstring_view fmt, wformat_args args); 51 52 template<class Out, class... Args> 53 Out format_to(Out out, format-string<Args...> fmt, Args&&... args); 54 template<class Out, class... Args> 55 Out format_to(Out out, wformat-string<Args...> fmt, Args&&... args); 56 template<class Out, class... Args> 57 Out format_to(Out out, const locale& loc, format-string<Args...> fmt, Args&&... args); 58 template<class Out, class... Args> 59 Out format_to(Out out, const locale& loc, wformat-string<Args...> fmt, Args&&... args); 60 61 template<class Out> 62 Out vformat_to(Out out, string_view fmt, format_args args); 63 template<class Out> 64 Out vformat_to(Out out, wstring_view fmt, wformat_args args); 65 template<class Out> 66 Out vformat_to(Out out, const locale& loc, string_view fmt, 67 format_args char> args); 68 template<class Out> 69 Out vformat_to(Out out, const locale& loc, wstring_view fmt, 70 wformat_args args); 71 72 template<class Out> struct format_to_n_result { 73 Out out; 74 iter_difference_t<Out> size; 75 }; 76 template<class Out, class... Args> 77 format_to_n_result<Out> format_to_n(Out out, iter_difference_t<Out> n, 78 format-string<Args...> fmt, Args&&... args); 79 template<class Out, class... Args> 80 format_to_n_result<Out> format_to_n(Out out, iter_difference_t<Out> n, 81 wformat-string<Args...> fmt, Args&&... args); 82 template<class Out, class... Args> 83 format_to_n_result<Out> format_to_n(Out out, iter_difference_t<Out> n, 84 const locale& loc, format-string<Args...> fmt, 85 Args&&... args); 86 template<class Out, class... Args> 87 format_to_n_result<Out> format_to_n(Out out, iter_difference_t<Out> n, 88 const locale& loc, wformat-string<Args...> fmt, 89 Args&&... args); 90 91 template<class... Args> 92 size_t formatted_size(format-string<Args...> fmt, Args&&... args); 93 template<class... Args> 94 size_t formatted_size(wformat-string<Args...> fmt, Args&&... args); 95 template<class... Args> 96 size_t formatted_size(const locale& loc, format-string<Args...> fmt, Args&&... args); 97 template<class... Args> 98 size_t formatted_size(const locale& loc, wformat-string<Args...> fmt, Args&&... args); 99 100 // [format.formatter], formatter 101 template<class T, class charT = char> struct formatter; 102 103 // [format.parse.ctx], class template basic_format_parse_context 104 template<class charT> class basic_format_parse_context; 105 using format_parse_context = basic_format_parse_context<char>; 106 using wformat_parse_context = basic_format_parse_context<wchar_t>; 107 108 // [format.arguments], arguments 109 // [format.arg], class template basic_format_arg 110 template<class Context> class basic_format_arg; 111 112 template<class Visitor, class Context> 113 see below visit_format_arg(Visitor&& vis, basic_format_arg<Context> arg); 114 115 // [format.arg.store], class template format-arg-store 116 template<class Context, class... Args> struct format-arg-store; // exposition only 117 118 template<class Context = format_context, class... Args> 119 format-arg-store<Context, Args...> 120 make_format_args(Args&&... args); 121 template<class... Args> 122 format-arg-store<wformat_context, Args...> 123 make_wformat_args(Args&&... args); 124 125 // [format.error], class format_error 126 class format_error; 127} 128 129*/ 130 131#include <__assert> // all public C++ headers provide the assertion handler 132// Make sure all feature-test macros are available. 133#include <version> 134// Enable the contents of the header only when libc++ was built with LIBCXX_ENABLE_INCOMPLETE_FEATURES. 135#if !defined(_LIBCPP_HAS_NO_INCOMPLETE_FORMAT) 136 137#include <__algorithm/clamp.h> 138#include <__config> 139#include <__debug> 140#include <__format/buffer.h> 141#include <__format/concepts.h> 142#include <__format/enable_insertable.h> 143#include <__format/format_arg.h> 144#include <__format/format_arg_store.h> 145#include <__format/format_args.h> 146#include <__format/format_context.h> 147#include <__format/format_error.h> 148#include <__format/format_fwd.h> 149#include <__format/format_parse_context.h> 150#include <__format/format_string.h> 151#include <__format/format_to_n_result.h> 152#include <__format/formatter.h> 153#include <__format/formatter_bool.h> 154#include <__format/formatter_char.h> 155#include <__format/formatter_floating_point.h> 156#include <__format/formatter_integer.h> 157#include <__format/formatter_pointer.h> 158#include <__format/formatter_string.h> 159#include <__format/parser_std_format_spec.h> 160#include <__iterator/back_insert_iterator.h> 161#include <__iterator/incrementable_traits.h> 162#include <__variant/monostate.h> 163#include <array> 164#include <concepts> 165#include <string> 166#include <string_view> 167#include <type_traits> 168 169#ifndef _LIBCPP_HAS_NO_LOCALIZATION 170#include <locale> 171#endif 172 173#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 174# pragma GCC system_header 175#endif 176 177_LIBCPP_BEGIN_NAMESPACE_STD 178 179#if _LIBCPP_STD_VER > 17 180 181// TODO FMT Move the implementation in this file to its own granular headers. 182 183// TODO FMT Evaluate which templates should be external templates. This 184// improves the efficiency of the header. However since the header is still 185// under heavy development and not all classes are stable it makes no sense 186// to do this optimization now. 187 188using format_args = basic_format_args<format_context>; 189#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS 190using wformat_args = basic_format_args<wformat_context>; 191#endif 192 193template <class _Context = format_context, class... _Args> 194_LIBCPP_HIDE_FROM_ABI __format_arg_store<_Context, _Args...> make_format_args(_Args&&... __args) { 195 return _VSTD::__format_arg_store<_Context, _Args...>(__args...); 196} 197 198#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS 199template <class... _Args> 200_LIBCPP_HIDE_FROM_ABI __format_arg_store<wformat_context, _Args...> make_wformat_args(_Args&&... __args) { 201 return _VSTD::__format_arg_store<wformat_context, _Args...>(__args...); 202} 203#endif 204 205namespace __format { 206 207/// Helper class parse and handle argument. 208/// 209/// When parsing a handle which is not enabled the code is ill-formed. 210/// This helper uses the parser of the appropriate formatter for the stored type. 211template <class _CharT> 212class _LIBCPP_TEMPLATE_VIS __compile_time_handle { 213public: 214 _LIBCPP_HIDE_FROM_ABI 215 constexpr void __parse(basic_format_parse_context<_CharT>& __parse_ctx) const { __parse_(__parse_ctx); } 216 217 template <class _Tp> 218 _LIBCPP_HIDE_FROM_ABI constexpr void __enable() { 219 __parse_ = [](basic_format_parse_context<_CharT>& __parse_ctx) { 220 formatter<_Tp, _CharT> __f; 221 __parse_ctx.advance_to(__f.parse(__parse_ctx)); 222 }; 223 } 224 225 // Before calling __parse the proper handler needs to be set with __enable. 226 // The default handler isn't a core constant expression. 227 _LIBCPP_HIDE_FROM_ABI constexpr __compile_time_handle() 228 : __parse_([](basic_format_parse_context<_CharT>&) { __throw_format_error("Not a handle"); }) {} 229 230private: 231 void (*__parse_)(basic_format_parse_context<_CharT>&); 232}; 233 234// Dummy format_context only providing the parts used during constant 235// validation of the basic-format-string. 236template <class _CharT> 237struct _LIBCPP_TEMPLATE_VIS __compile_time_basic_format_context { 238public: 239 using char_type = _CharT; 240 241 _LIBCPP_HIDE_FROM_ABI constexpr explicit __compile_time_basic_format_context( 242 const __arg_t* __args, const __compile_time_handle<_CharT>* __handles, size_t __size) 243 : __args_(__args), __handles_(__handles), __size_(__size) {} 244 245 // During the compile-time validation nothing needs to be written. 246 // Therefore all operations of this iterator are a NOP. 247 struct iterator { 248 _LIBCPP_HIDE_FROM_ABI constexpr iterator& operator=(_CharT) { return *this; } 249 _LIBCPP_HIDE_FROM_ABI constexpr iterator& operator*() { return *this; } 250 _LIBCPP_HIDE_FROM_ABI constexpr iterator operator++(int) { return *this; } 251 }; 252 253 _LIBCPP_HIDE_FROM_ABI constexpr __arg_t arg(size_t __id) const { 254 if (__id >= __size_) 255 __throw_format_error("Argument index out of bounds"); 256 return __args_[__id]; 257 } 258 259 _LIBCPP_HIDE_FROM_ABI constexpr const __compile_time_handle<_CharT>& __handle(size_t __id) const { 260 if (__id >= __size_) 261 __throw_format_error("Argument index out of bounds"); 262 return __handles_[__id]; 263 } 264 265 _LIBCPP_HIDE_FROM_ABI constexpr iterator out() { return {}; } 266 _LIBCPP_HIDE_FROM_ABI constexpr void advance_to(iterator) {} 267 268private: 269 const __arg_t* __args_; 270 const __compile_time_handle<_CharT>* __handles_; 271 size_t __size_; 272}; 273 274_LIBCPP_HIDE_FROM_ABI 275constexpr void __compile_time_validate_integral(__arg_t __type) { 276 switch (__type) { 277 case __arg_t::__int: 278 case __arg_t::__long_long: 279 case __arg_t::__i128: 280 case __arg_t::__unsigned: 281 case __arg_t::__unsigned_long_long: 282 case __arg_t::__u128: 283 return; 284 285 default: 286 __throw_format_error("Argument isn't an integral type"); 287 } 288} 289 290// _HasPrecision does the formatter have a precision? 291template <class _CharT, class _Tp, bool _HasPrecision = false> 292_LIBCPP_HIDE_FROM_ABI constexpr void 293__compile_time_validate_argument(basic_format_parse_context<_CharT>& __parse_ctx, 294 __compile_time_basic_format_context<_CharT>& __ctx) { 295 formatter<_Tp, _CharT> __formatter; 296 __parse_ctx.advance_to(__formatter.parse(__parse_ctx)); 297 // [format.string.std]/7 298 // ... If the corresponding formatting argument is not of integral type, or 299 // its value is negative for precision or non-positive for width, an 300 // exception of type format_error is thrown. 301 // 302 // Validate whether the arguments are integrals. 303 if constexpr (requires(formatter<_Tp, _CharT> __f) { __f.__width_needs_substitution(); }) { 304 // TODO FMT Remove this when parser v1 has been phased out. 305 if (__formatter.__width_needs_substitution()) 306 __format::__compile_time_validate_integral(__ctx.arg(__formatter.__width)); 307 308 if constexpr (_HasPrecision) 309 if (__formatter.__precision_needs_substitution()) 310 __format::__compile_time_validate_integral(__ctx.arg(__formatter.__precision)); 311 } else { 312 if (__formatter.__parser_.__width_as_arg_) 313 __format::__compile_time_validate_integral(__ctx.arg(__formatter.__parser_.__width_)); 314 315 if constexpr (_HasPrecision) 316 if (__formatter.__parser_.__precision_as_arg_) 317 __format::__compile_time_validate_integral(__ctx.arg(__formatter.__parser_.__precision_)); 318 } 319} 320 321template <class _CharT> 322_LIBCPP_HIDE_FROM_ABI constexpr void __compile_time_visit_format_arg(basic_format_parse_context<_CharT>& __parse_ctx, 323 __compile_time_basic_format_context<_CharT>& __ctx, 324 __arg_t __type) { 325 switch (__type) { 326 case __arg_t::__none: 327 __throw_format_error("Invalid argument"); 328 case __arg_t::__boolean: 329 return __format::__compile_time_validate_argument<_CharT, bool>(__parse_ctx, __ctx); 330 case __arg_t::__char_type: 331 return __format::__compile_time_validate_argument<_CharT, _CharT>(__parse_ctx, __ctx); 332 case __arg_t::__int: 333 return __format::__compile_time_validate_argument<_CharT, int>(__parse_ctx, __ctx); 334 case __arg_t::__long_long: 335 return __format::__compile_time_validate_argument<_CharT, long long>(__parse_ctx, __ctx); 336 case __arg_t::__i128: 337# ifndef _LIBCPP_HAS_NO_INT128 338 return __format::__compile_time_validate_argument<_CharT, __int128_t>(__parse_ctx, __ctx); 339# else 340 __throw_format_error("Invalid argument"); 341# endif 342 return; 343 case __arg_t::__unsigned: 344 return __format::__compile_time_validate_argument<_CharT, unsigned>(__parse_ctx, __ctx); 345 case __arg_t::__unsigned_long_long: 346 return __format::__compile_time_validate_argument<_CharT, unsigned long long>(__parse_ctx, __ctx); 347 case __arg_t::__u128: 348# ifndef _LIBCPP_HAS_NO_INT128 349 return __format::__compile_time_validate_argument<_CharT, __uint128_t>(__parse_ctx, __ctx); 350# else 351 __throw_format_error("Invalid argument"); 352# endif 353 return; 354 case __arg_t::__float: 355 return __format::__compile_time_validate_argument<_CharT, float, true>(__parse_ctx, __ctx); 356 case __arg_t::__double: 357 return __format::__compile_time_validate_argument<_CharT, double, true>(__parse_ctx, __ctx); 358 case __arg_t::__long_double: 359 return __format::__compile_time_validate_argument<_CharT, long double, true>(__parse_ctx, __ctx); 360 case __arg_t::__const_char_type_ptr: 361 return __format::__compile_time_validate_argument<_CharT, const _CharT*, true>(__parse_ctx, __ctx); 362 case __arg_t::__string_view: 363 return __format::__compile_time_validate_argument<_CharT, basic_string_view<_CharT>, true>(__parse_ctx, __ctx); 364 case __arg_t::__ptr: 365 return __format::__compile_time_validate_argument<_CharT, const void*>(__parse_ctx, __ctx); 366 case __arg_t::__handle: 367 __throw_format_error("Handle should use __compile_time_validate_handle_argument"); 368 } 369 __throw_format_error("Invalid argument"); 370} 371 372template <class _CharT, class _ParseCtx, class _Ctx> 373_LIBCPP_HIDE_FROM_ABI constexpr const _CharT* 374__handle_replacement_field(const _CharT* __begin, const _CharT* __end, 375 _ParseCtx& __parse_ctx, _Ctx& __ctx) { 376 __format::__parse_number_result __r = 377 __format::__parse_arg_id(__begin, __end, __parse_ctx); 378 379 bool __parse = *__r.__ptr == _CharT(':'); 380 switch (*__r.__ptr) { 381 case _CharT(':'): 382 // The arg-id has a format-specifier, advance the input to the format-spec. 383 __parse_ctx.advance_to(__r.__ptr + 1); 384 break; 385 case _CharT('}'): 386 // The arg-id has no format-specifier. 387 __parse_ctx.advance_to(__r.__ptr); 388 break; 389 default: 390 __throw_format_error( 391 "The replacement field arg-id should terminate at a ':' or '}'"); 392 } 393 394 if constexpr (same_as<_Ctx, __compile_time_basic_format_context<_CharT>>) { 395 __arg_t __type = __ctx.arg(__r.__value); 396 if (__type == __arg_t::__handle) 397 __ctx.__handle(__r.__value).__parse(__parse_ctx); 398 else 399 __format::__compile_time_visit_format_arg(__parse_ctx, __ctx, __type); 400 } else 401 _VSTD::visit_format_arg( 402 [&](auto __arg) { 403 if constexpr (same_as<decltype(__arg), monostate>) 404 __throw_format_error("Argument index out of bounds"); 405 else if constexpr (same_as<decltype(__arg), typename basic_format_arg<_Ctx>::handle>) 406 __arg.format(__parse_ctx, __ctx); 407 else { 408 formatter<decltype(__arg), _CharT> __formatter; 409 if (__parse) 410 __parse_ctx.advance_to(__formatter.parse(__parse_ctx)); 411 __ctx.advance_to(__formatter.format(__arg, __ctx)); 412 } 413 }, 414 __ctx.arg(__r.__value)); 415 416 __begin = __parse_ctx.begin(); 417 if (__begin == __end || *__begin != _CharT('}')) 418 __throw_format_error("The replacement field misses a terminating '}'"); 419 420 return ++__begin; 421} 422 423template <class _ParseCtx, class _Ctx> 424_LIBCPP_HIDE_FROM_ABI constexpr typename _Ctx::iterator 425__vformat_to(_ParseCtx&& __parse_ctx, _Ctx&& __ctx) { 426 using _CharT = typename _ParseCtx::char_type; 427 static_assert(same_as<typename _Ctx::char_type, _CharT>); 428 429 const _CharT* __begin = __parse_ctx.begin(); 430 const _CharT* __end = __parse_ctx.end(); 431 typename _Ctx::iterator __out_it = __ctx.out(); 432 while (__begin != __end) { 433 switch (*__begin) { 434 case _CharT('{'): 435 ++__begin; 436 if (__begin == __end) 437 __throw_format_error("The format string terminates at a '{'"); 438 439 if (*__begin != _CharT('{')) [[likely]] { 440 __ctx.advance_to(_VSTD::move(__out_it)); 441 __begin = 442 __handle_replacement_field(__begin, __end, __parse_ctx, __ctx); 443 __out_it = __ctx.out(); 444 445 // The output is written and __begin points to the next character. So 446 // start the next iteration. 447 continue; 448 } 449 // The string is an escape character. 450 break; 451 452 case _CharT('}'): 453 ++__begin; 454 if (__begin == __end || *__begin != _CharT('}')) 455 __throw_format_error( 456 "The format string contains an invalid escape sequence"); 457 458 break; 459 } 460 461 // Copy the character to the output verbatim. 462 *__out_it++ = *__begin++; 463 } 464 return __out_it; 465} 466 467} // namespace __format 468 469template <class _CharT, class... _Args> 470struct _LIBCPP_TEMPLATE_VIS __basic_format_string { 471 basic_string_view<_CharT> __str_; 472 473 template <class _Tp> 474 requires convertible_to<const _Tp&, basic_string_view<_CharT>> 475 consteval __basic_format_string(const _Tp& __str) : __str_{__str} { 476 __format::__vformat_to(basic_format_parse_context<_CharT>{__str_, sizeof...(_Args)}, 477 _Context{__types_.data(), __handles_.data(), sizeof...(_Args)}); 478 } 479 480private: 481 using _Context = __format::__compile_time_basic_format_context<_CharT>; 482 483 static constexpr array<__format::__arg_t, sizeof...(_Args)> __types_{ 484 __format::__determine_arg_t<_Context, remove_cvref_t<_Args>>()...}; 485 486 // TODO FMT remove this work-around when the AIX ICE has been resolved. 487# if defined(_AIX) && defined(_LIBCPP_CLANG_VER) && _LIBCPP_CLANG_VER < 1400 488 template <class _Tp> 489 static constexpr __format::__compile_time_handle<_CharT> __get_handle() { 490 __format::__compile_time_handle<_CharT> __handle; 491 if (__format::__determine_arg_t<_Context, _Tp>() == __format::__arg_t::__handle) 492 __handle.template __enable<_Tp>(); 493 494 return __handle; 495 } 496 497 static constexpr array<__format::__compile_time_handle<_CharT>, sizeof...(_Args)> __handles_{ 498 __get_handle<_Args>()...}; 499# else 500 static constexpr array<__format::__compile_time_handle<_CharT>, sizeof...(_Args)> __handles_{[] { 501 using _Tp = remove_cvref_t<_Args>; 502 __format::__compile_time_handle<_CharT> __handle; 503 if (__format::__determine_arg_t<_Context, _Tp>() == __format::__arg_t::__handle) 504 __handle.template __enable<_Tp>(); 505 506 return __handle; 507 }()...}; 508# endif 509}; 510 511template <class... _Args> 512using __format_string_t = __basic_format_string<char, type_identity_t<_Args>...>; 513 514#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS 515template <class... _Args> 516using __wformat_string_t = __basic_format_string<wchar_t, type_identity_t<_Args>...>; 517#endif 518 519template <class _OutIt, class _CharT, class _FormatOutIt> 520requires(output_iterator<_OutIt, const _CharT&>) _LIBCPP_HIDE_FROM_ABI _OutIt 521 __vformat_to( 522 _OutIt __out_it, basic_string_view<_CharT> __fmt, 523 basic_format_args<basic_format_context<_FormatOutIt, _CharT>> __args) { 524 if constexpr (same_as<_OutIt, _FormatOutIt>) 525 return _VSTD::__format::__vformat_to( 526 basic_format_parse_context{__fmt, __args.__size()}, 527 _VSTD::__format_context_create(_VSTD::move(__out_it), __args)); 528 else { 529 __format::__format_buffer<_OutIt, _CharT> __buffer{_VSTD::move(__out_it)}; 530 _VSTD::__format::__vformat_to( 531 basic_format_parse_context{__fmt, __args.__size()}, 532 _VSTD::__format_context_create(__buffer.make_output_iterator(), 533 __args)); 534 return _VSTD::move(__buffer).out(); 535 } 536} 537 538// The function is _LIBCPP_ALWAYS_INLINE since the compiler is bad at inlining 539// https://reviews.llvm.org/D110499#inline-1180704 540// TODO FMT Evaluate whether we want to file a Clang bug report regarding this. 541template <output_iterator<const char&> _OutIt> 542_LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT _OutIt 543vformat_to(_OutIt __out_it, string_view __fmt, format_args __args) { 544 return _VSTD::__vformat_to(_VSTD::move(__out_it), __fmt, __args); 545} 546 547#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS 548template <output_iterator<const wchar_t&> _OutIt> 549_LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT _OutIt 550vformat_to(_OutIt __out_it, wstring_view __fmt, wformat_args __args) { 551 return _VSTD::__vformat_to(_VSTD::move(__out_it), __fmt, __args); 552} 553#endif 554 555template <output_iterator<const char&> _OutIt, class... _Args> 556_LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT _OutIt 557format_to(_OutIt __out_it, __format_string_t<_Args...> __fmt, _Args&&... __args) { 558 return _VSTD::vformat_to(_VSTD::move(__out_it), __fmt.__str_, 559 _VSTD::make_format_args(__args...)); 560} 561 562#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS 563template <output_iterator<const wchar_t&> _OutIt, class... _Args> 564_LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT _OutIt 565format_to(_OutIt __out_it, __wformat_string_t<_Args...> __fmt, _Args&&... __args) { 566 return _VSTD::vformat_to(_VSTD::move(__out_it), __fmt.__str_, 567 _VSTD::make_wformat_args(__args...)); 568} 569#endif 570 571_LIBCPP_ALWAYS_INLINE inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT string 572vformat(string_view __fmt, format_args __args) { 573 string __res; 574 _VSTD::vformat_to(_VSTD::back_inserter(__res), __fmt, __args); 575 return __res; 576} 577 578#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS 579_LIBCPP_ALWAYS_INLINE inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT wstring 580vformat(wstring_view __fmt, wformat_args __args) { 581 wstring __res; 582 _VSTD::vformat_to(_VSTD::back_inserter(__res), __fmt, __args); 583 return __res; 584} 585#endif 586 587template <class... _Args> 588_LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT string format(__format_string_t<_Args...> __fmt, 589 _Args&&... __args) { 590 return _VSTD::vformat(__fmt.__str_, _VSTD::make_format_args(__args...)); 591} 592 593#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS 594template <class... _Args> 595_LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT wstring 596format(__wformat_string_t<_Args...> __fmt, _Args&&... __args) { 597 return _VSTD::vformat(__fmt.__str_, _VSTD::make_wformat_args(__args...)); 598} 599#endif 600 601template <class _Context, class _OutIt, class _CharT> 602_LIBCPP_HIDE_FROM_ABI format_to_n_result<_OutIt> __vformat_to_n(_OutIt __out_it, iter_difference_t<_OutIt> __n, 603 basic_string_view<_CharT> __fmt, 604 basic_format_args<_Context> __args) { 605 __format::__format_to_n_buffer<_OutIt, _CharT> __buffer{_VSTD::move(__out_it), __n}; 606 _VSTD::__format::__vformat_to(basic_format_parse_context{__fmt, __args.__size()}, 607 _VSTD::__format_context_create(__buffer.make_output_iterator(), __args)); 608 return _VSTD::move(__buffer).result(); 609} 610 611template <output_iterator<const char&> _OutIt, class... _Args> 612_LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT format_to_n_result<_OutIt> 613format_to_n(_OutIt __out_it, iter_difference_t<_OutIt> __n, __format_string_t<_Args...> __fmt, _Args&&... __args) { 614 return _VSTD::__vformat_to_n<format_context>(_VSTD::move(__out_it), __n, __fmt.__str_, _VSTD::make_format_args(__args...)); 615} 616 617#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS 618template <output_iterator<const wchar_t&> _OutIt, class... _Args> 619_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT format_to_n_result<_OutIt> 620format_to_n(_OutIt __out_it, iter_difference_t<_OutIt> __n, __wformat_string_t<_Args...> __fmt, 621 _Args&&... __args) { 622 return _VSTD::__vformat_to_n<wformat_context>(_VSTD::move(__out_it), __n, __fmt.__str_, _VSTD::make_wformat_args(__args...)); 623} 624#endif 625 626template <class _CharT> 627_LIBCPP_HIDE_FROM_ABI size_t __vformatted_size(basic_string_view<_CharT> __fmt, auto __args) { 628 __format::__formatted_size_buffer<_CharT> __buffer; 629 _VSTD::__format::__vformat_to(basic_format_parse_context{__fmt, __args.__size()}, 630 _VSTD::__format_context_create(__buffer.make_output_iterator(), __args)); 631 return _VSTD::move(__buffer).result(); 632} 633 634template <class... _Args> 635_LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT size_t 636formatted_size(__format_string_t<_Args...> __fmt, _Args&&... __args) { 637 return _VSTD::__vformatted_size(__fmt.__str_, basic_format_args{_VSTD::make_format_args(__args...)}); 638} 639 640#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS 641template <class... _Args> 642_LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT size_t 643formatted_size(__wformat_string_t<_Args...> __fmt, _Args&&... __args) { 644 return _VSTD::__vformatted_size(__fmt.__str_, basic_format_args{_VSTD::make_wformat_args(__args...)}); 645} 646#endif 647 648#ifndef _LIBCPP_HAS_NO_LOCALIZATION 649 650template <class _OutIt, class _CharT, class _FormatOutIt> 651requires(output_iterator<_OutIt, const _CharT&>) _LIBCPP_HIDE_FROM_ABI _OutIt 652 __vformat_to( 653 _OutIt __out_it, locale __loc, basic_string_view<_CharT> __fmt, 654 basic_format_args<basic_format_context<_FormatOutIt, _CharT>> __args) { 655 if constexpr (same_as<_OutIt, _FormatOutIt>) 656 return _VSTD::__format::__vformat_to( 657 basic_format_parse_context{__fmt, __args.__size()}, 658 _VSTD::__format_context_create(_VSTD::move(__out_it), __args, 659 _VSTD::move(__loc))); 660 else { 661 __format::__format_buffer<_OutIt, _CharT> __buffer{_VSTD::move(__out_it)}; 662 _VSTD::__format::__vformat_to( 663 basic_format_parse_context{__fmt, __args.__size()}, 664 _VSTD::__format_context_create(__buffer.make_output_iterator(), 665 __args, _VSTD::move(__loc))); 666 return _VSTD::move(__buffer).out(); 667 } 668} 669 670template <output_iterator<const char&> _OutIt> 671_LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT _OutIt vformat_to( 672 _OutIt __out_it, locale __loc, string_view __fmt, format_args __args) { 673 return _VSTD::__vformat_to(_VSTD::move(__out_it), _VSTD::move(__loc), __fmt, 674 __args); 675} 676 677#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS 678template <output_iterator<const wchar_t&> _OutIt> 679_LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT _OutIt vformat_to( 680 _OutIt __out_it, locale __loc, wstring_view __fmt, wformat_args __args) { 681 return _VSTD::__vformat_to(_VSTD::move(__out_it), _VSTD::move(__loc), __fmt, 682 __args); 683} 684#endif 685 686template <output_iterator<const char&> _OutIt, class... _Args> 687_LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT _OutIt 688format_to(_OutIt __out_it, locale __loc, __format_string_t<_Args...> __fmt, _Args&&... __args) { 689 return _VSTD::vformat_to(_VSTD::move(__out_it), _VSTD::move(__loc), __fmt.__str_, 690 _VSTD::make_format_args(__args...)); 691} 692 693#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS 694template <output_iterator<const wchar_t&> _OutIt, class... _Args> 695_LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT _OutIt 696format_to(_OutIt __out_it, locale __loc, __wformat_string_t<_Args...> __fmt, _Args&&... __args) { 697 return _VSTD::vformat_to(_VSTD::move(__out_it), _VSTD::move(__loc), __fmt.__str_, 698 _VSTD::make_wformat_args(__args...)); 699} 700#endif 701 702_LIBCPP_ALWAYS_INLINE inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT string 703vformat(locale __loc, string_view __fmt, format_args __args) { 704 string __res; 705 _VSTD::vformat_to(_VSTD::back_inserter(__res), _VSTD::move(__loc), __fmt, 706 __args); 707 return __res; 708} 709 710#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS 711_LIBCPP_ALWAYS_INLINE inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT wstring 712vformat(locale __loc, wstring_view __fmt, wformat_args __args) { 713 wstring __res; 714 _VSTD::vformat_to(_VSTD::back_inserter(__res), _VSTD::move(__loc), __fmt, 715 __args); 716 return __res; 717} 718#endif 719 720template <class... _Args> 721_LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT string format(locale __loc, 722 __format_string_t<_Args...> __fmt, 723 _Args&&... __args) { 724 return _VSTD::vformat(_VSTD::move(__loc), __fmt.__str_, 725 _VSTD::make_format_args(__args...)); 726} 727 728#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS 729template <class... _Args> 730_LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT wstring 731format(locale __loc, __wformat_string_t<_Args...> __fmt, _Args&&... __args) { 732 return _VSTD::vformat(_VSTD::move(__loc), __fmt.__str_, 733 _VSTD::make_wformat_args(__args...)); 734} 735#endif 736 737template <class _Context, class _OutIt, class _CharT> 738_LIBCPP_HIDE_FROM_ABI format_to_n_result<_OutIt> __vformat_to_n(_OutIt __out_it, iter_difference_t<_OutIt> __n, 739 locale __loc, basic_string_view<_CharT> __fmt, 740 basic_format_args<_Context> __args) { 741 __format::__format_to_n_buffer<_OutIt, _CharT> __buffer{_VSTD::move(__out_it), __n}; 742 _VSTD::__format::__vformat_to( 743 basic_format_parse_context{__fmt, __args.__size()}, 744 _VSTD::__format_context_create(__buffer.make_output_iterator(), __args, _VSTD::move(__loc))); 745 return _VSTD::move(__buffer).result(); 746} 747 748template <output_iterator<const char&> _OutIt, class... _Args> 749_LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT format_to_n_result<_OutIt> 750format_to_n(_OutIt __out_it, iter_difference_t<_OutIt> __n, locale __loc, __format_string_t<_Args...> __fmt, 751 _Args&&... __args) { 752 return _VSTD::__vformat_to_n<format_context>(_VSTD::move(__out_it), __n, _VSTD::move(__loc), __fmt.__str_, 753 _VSTD::make_format_args(__args...)); 754} 755 756#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS 757template <output_iterator<const wchar_t&> _OutIt, class... _Args> 758_LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT format_to_n_result<_OutIt> 759format_to_n(_OutIt __out_it, iter_difference_t<_OutIt> __n, locale __loc, __wformat_string_t<_Args...> __fmt, 760 _Args&&... __args) { 761 return _VSTD::__vformat_to_n<wformat_context>(_VSTD::move(__out_it), __n, _VSTD::move(__loc), __fmt.__str_, 762 _VSTD::make_wformat_args(__args...)); 763} 764#endif 765 766template <class _CharT> 767_LIBCPP_HIDE_FROM_ABI size_t __vformatted_size(locale __loc, basic_string_view<_CharT> __fmt, auto __args) { 768 __format::__formatted_size_buffer<_CharT> __buffer; 769 _VSTD::__format::__vformat_to( 770 basic_format_parse_context{__fmt, __args.__size()}, 771 _VSTD::__format_context_create(__buffer.make_output_iterator(), __args, _VSTD::move(__loc))); 772 return _VSTD::move(__buffer).result(); 773} 774 775template <class... _Args> 776_LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT size_t 777formatted_size(locale __loc, __format_string_t<_Args...> __fmt, _Args&&... __args) { 778 return _VSTD::__vformatted_size(_VSTD::move(__loc), __fmt.__str_, basic_format_args{_VSTD::make_format_args(__args...)}); 779} 780 781#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS 782template <class... _Args> 783_LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT size_t 784formatted_size(locale __loc, __wformat_string_t<_Args...> __fmt, _Args&&... __args) { 785 return _VSTD::__vformatted_size(_VSTD::move(__loc), __fmt.__str_, basic_format_args{_VSTD::make_wformat_args(__args...)}); 786} 787#endif 788 789#endif // _LIBCPP_HAS_NO_LOCALIZATION 790 791#endif //_LIBCPP_STD_VER > 17 792 793_LIBCPP_END_NAMESPACE_STD 794 795#endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_FORMAT) 796 797#endif // _LIBCPP_FORMAT 798