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