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.functions], formatting functions 27 template<class... Args> 28 string format(string_view fmt, const Args&... args); 29 template<class... Args> 30 wstring format(wstring_view fmt, const Args&... args); 31 template<class... Args> 32 string format(const locale& loc, string_view fmt, const Args&... args); 33 template<class... Args> 34 wstring format(const locale& loc, wstring_view fmt, const Args&... args); 35 36 string vformat(string_view fmt, format_args args); 37 wstring vformat(wstring_view fmt, wformat_args args); 38 string vformat(const locale& loc, string_view fmt, format_args args); 39 wstring vformat(const locale& loc, wstring_view fmt, wformat_args args); 40 41 template<class Out, class... Args> 42 Out format_to(Out out, string_view fmt, const Args&... args); 43 template<class Out, class... Args> 44 Out format_to(Out out, wstring_view fmt, const Args&... args); 45 template<class Out, class... Args> 46 Out format_to(Out out, const locale& loc, string_view fmt, const Args&... args); 47 template<class Out, class... Args> 48 Out format_to(Out out, const locale& loc, wstring_view fmt, const Args&... args); 49 50 template<class Out> 51 Out vformat_to(Out out, string_view fmt, format_args args); 52 template<class Out> 53 Out vformat_to(Out out, wstring_view fmt, wformat_args args); 54 template<class Out> 55 Out vformat_to(Out out, const locale& loc, string_view fmt, 56 format_args char> args); 57 template<class Out> 58 Out vformat_to(Out out, const locale& loc, wstring_view fmt, 59 wformat_args args); 60 61 template<class Out> struct format_to_n_result { 62 Out out; 63 iter_difference_t<Out> size; 64 }; 65 template<class Out, class... Args> 66 format_to_n_result<Out> format_to_n(Out out, iter_difference_t<Out> n, 67 string_view fmt, const Args&... args); 68 template<class Out, class... Args> 69 format_to_n_result<Out> format_to_n(Out out, iter_difference_t<Out> n, 70 wstring_view fmt, const Args&... args); 71 template<class Out, class... Args> 72 format_to_n_result<Out> format_to_n(Out out, iter_difference_t<Out> n, 73 const locale& loc, string_view fmt, 74 const Args&... args); 75 template<class Out, class... Args> 76 format_to_n_result<Out> format_to_n(Out out, iter_difference_t<Out> n, 77 const locale& loc, wstring_view fmt, 78 const Args&... args); 79 80 template<class... Args> 81 size_t formatted_size(string_view fmt, const Args&... args); 82 template<class... Args> 83 size_t formatted_size(wstring_view fmt, const Args&... args); 84 template<class... Args> 85 size_t formatted_size(const locale& loc, string_view fmt, const Args&... args); 86 template<class... Args> 87 size_t formatted_size(const locale& loc, wstring_view fmt, const Args&... args); 88 89 // [format.formatter], formatter 90 template<class T, class charT = char> struct formatter; 91 92 // [format.parse.ctx], class template basic_format_parse_context 93 template<class charT> class basic_format_parse_context; 94 using format_parse_context = basic_format_parse_context<char>; 95 using wformat_parse_context = basic_format_parse_context<wchar_t>; 96 97 // [format.arguments], arguments 98 // [format.arg], class template basic_format_arg 99 template<class Context> class basic_format_arg; 100 101 template<class Visitor, class Context> 102 see below visit_format_arg(Visitor&& vis, basic_format_arg<Context> arg); 103 104 // [format.arg.store], class template format-arg-store 105 template<class Context, class... Args> struct format-arg-store; // exposition only 106 107 template<class Context = format_context, class... Args> 108 format-arg-store<Context, Args...> 109 make_format_args(const Args&... args); 110 template<class... Args> 111 format-arg-store<wformat_context, Args...> 112 make_wformat_args(const Args&... args); 113 114 // [format.error], class format_error 115 class format_error; 116} 117 118*/ 119 120#include <__assert> // all public C++ headers provide the assertion handler 121// Make sure all feature-test macros are available. 122#include <version> 123// Enable the contents of the header only when libc++ was built with LIBCXX_ENABLE_INCOMPLETE_FEATURES. 124#if !defined(_LIBCPP_HAS_NO_INCOMPLETE_FORMAT) 125 126#include <__algorithm/clamp.h> 127#include <__config> 128#include <__debug> 129#include <__format/buffer.h> 130#include <__format/concepts.h> 131#include <__format/enable_insertable.h> 132#include <__format/format_arg.h> 133#include <__format/format_arg_store.h> 134#include <__format/format_args.h> 135#include <__format/format_context.h> 136#include <__format/format_error.h> 137#include <__format/format_fwd.h> 138#include <__format/format_parse_context.h> 139#include <__format/format_string.h> 140#include <__format/format_to_n_result.h> 141#include <__format/formatter.h> 142#include <__format/formatter_bool.h> 143#include <__format/formatter_char.h> 144#include <__format/formatter_floating_point.h> 145#include <__format/formatter_integer.h> 146#include <__format/formatter_pointer.h> 147#include <__format/formatter_string.h> 148#include <__format/parser_std_format_spec.h> 149#include <__variant/monostate.h> 150#include <array> 151#include <concepts> 152#include <string> 153#include <string_view> 154#include <type_traits> 155 156#ifndef _LIBCPP_HAS_NO_LOCALIZATION 157#include <locale> 158#endif 159 160#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 161# pragma GCC system_header 162#endif 163 164_LIBCPP_BEGIN_NAMESPACE_STD 165 166#if _LIBCPP_STD_VER > 17 167 168// TODO FMT Move the implementation in this file to its own granular headers. 169 170// TODO FMT Evaluate which templates should be external templates. This 171// improves the efficiency of the header. However since the header is still 172// under heavy development and not all classes are stable it makes no sense 173// to do this optimization now. 174 175using format_args = basic_format_args<format_context>; 176#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS 177using wformat_args = basic_format_args<wformat_context>; 178#endif 179 180// TODO FMT This helper wrapper can probably be removed after P2418 has been 181// implemented. 182template <class _Context, class... _Args> 183_LIBCPP_HIDE_FROM_ABI __format_arg_store<_Context, _Args...> 184__make_format_args(_Args&&... __args) { 185 return _VSTD::__format_arg_store<_Context, _Args...>( 186 _VSTD::forward<_Args>(__args)...); 187} 188 189// TODO FMT After P2418 specify the return type instead of using auto. 190template <class _Context = format_context, class... _Args> 191_LIBCPP_HIDE_FROM_ABI auto make_format_args(const _Args&... __args) { 192 return _VSTD::__make_format_args<_Context>(__args...); 193} 194 195#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS 196// TODO FMT After P2418 specify the return type instead of using auto. 197template <class... _Args> 198_LIBCPP_HIDE_FROM_ABI auto make_wformat_args(const _Args&... __args) { 199 return _VSTD::make_format_args<wformat_context>(__args...); 200} 201#endif 202 203namespace __format { 204 205template <class _CharT, class _ParseCtx, class _Ctx> 206_LIBCPP_HIDE_FROM_ABI const _CharT* 207__handle_replacement_field(const _CharT* __begin, const _CharT* __end, 208 _ParseCtx& __parse_ctx, _Ctx& __ctx) { 209 __format::__parse_number_result __r = 210 __format::__parse_arg_id(__begin, __end, __parse_ctx); 211 212 switch (*__r.__ptr) { 213 case _CharT(':'): 214 // The arg-id has a format-specifier, advance the input to the format-spec. 215 __parse_ctx.advance_to(__r.__ptr + 1); 216 break; 217 case _CharT('}'): 218 // The arg-id has no format-specifier. 219 __parse_ctx.advance_to(__r.__ptr); 220 break; 221 default: 222 __throw_format_error( 223 "The replacement field arg-id should terminate at a ':' or '}'"); 224 } 225 226 _VSTD::visit_format_arg( 227 [&](auto __arg) { 228 if constexpr (same_as<decltype(__arg), monostate>) 229 __throw_format_error("Argument index out of bounds"); 230 else if constexpr (same_as<decltype(__arg), typename basic_format_arg<_Ctx>::handle>) 231 __arg.format(__parse_ctx, __ctx); 232 else { 233 formatter<decltype(__arg), _CharT> __formatter; 234 __parse_ctx.advance_to(__formatter.parse(__parse_ctx)); 235 __ctx.advance_to(__formatter.format(__arg, __ctx)); 236 } 237 }, 238 __ctx.arg(__r.__value)); 239 240 __begin = __parse_ctx.begin(); 241 if (__begin == __end || *__begin != _CharT('}')) 242 __throw_format_error("The replacement field misses a terminating '}'"); 243 244 return ++__begin; 245} 246 247template <class _ParseCtx, class _Ctx> 248_LIBCPP_HIDE_FROM_ABI typename _Ctx::iterator 249__vformat_to(_ParseCtx&& __parse_ctx, _Ctx&& __ctx) { 250 using _CharT = typename _ParseCtx::char_type; 251 static_assert(same_as<typename _Ctx::char_type, _CharT>); 252 253 const _CharT* __begin = __parse_ctx.begin(); 254 const _CharT* __end = __parse_ctx.end(); 255 typename _Ctx::iterator __out_it = __ctx.out(); 256 while (__begin != __end) { 257 switch (*__begin) { 258 case _CharT('{'): 259 ++__begin; 260 if (__begin == __end) 261 __throw_format_error("The format string terminates at a '{'"); 262 263 if (*__begin != _CharT('{')) [[likely]] { 264 __ctx.advance_to(_VSTD::move(__out_it)); 265 __begin = 266 __handle_replacement_field(__begin, __end, __parse_ctx, __ctx); 267 __out_it = __ctx.out(); 268 269 // The output is written and __begin points to the next character. So 270 // start the next iteration. 271 continue; 272 } 273 // The string is an escape character. 274 break; 275 276 case _CharT('}'): 277 ++__begin; 278 if (__begin == __end || *__begin != _CharT('}')) 279 __throw_format_error( 280 "The format string contains an invalid escape sequence"); 281 282 break; 283 } 284 285 // Copy the character to the output verbatim. 286 *__out_it++ = *__begin++; 287 } 288 return __out_it; 289} 290 291} // namespace __format 292 293template <class _OutIt, class _CharT, class _FormatOutIt> 294requires(output_iterator<_OutIt, const _CharT&>) _LIBCPP_HIDE_FROM_ABI _OutIt 295 __vformat_to( 296 _OutIt __out_it, basic_string_view<_CharT> __fmt, 297 basic_format_args<basic_format_context<_FormatOutIt, _CharT>> __args) { 298 if constexpr (same_as<_OutIt, _FormatOutIt>) 299 return _VSTD::__format::__vformat_to( 300 basic_format_parse_context{__fmt, __args.__size()}, 301 _VSTD::__format_context_create(_VSTD::move(__out_it), __args)); 302 else { 303 __format::__format_buffer<_OutIt, _CharT> __buffer{_VSTD::move(__out_it)}; 304 _VSTD::__format::__vformat_to( 305 basic_format_parse_context{__fmt, __args.__size()}, 306 _VSTD::__format_context_create(__buffer.make_output_iterator(), 307 __args)); 308 return _VSTD::move(__buffer).out(); 309 } 310} 311 312template <output_iterator<const char&> _OutIt> 313_LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT _OutIt 314vformat_to(_OutIt __out_it, string_view __fmt, format_args __args) { 315 return _VSTD::__vformat_to(_VSTD::move(__out_it), __fmt, __args); 316} 317 318#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS 319template <output_iterator<const wchar_t&> _OutIt> 320_LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT _OutIt 321vformat_to(_OutIt __out_it, wstring_view __fmt, wformat_args __args) { 322 return _VSTD::__vformat_to(_VSTD::move(__out_it), __fmt, __args); 323} 324#endif 325 326template <output_iterator<const char&> _OutIt, class... _Args> 327_LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT _OutIt 328format_to(_OutIt __out_it, string_view __fmt, const _Args&... __args) { 329 return _VSTD::vformat_to(_VSTD::move(__out_it), __fmt, 330 _VSTD::make_format_args(__args...)); 331} 332 333#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS 334template <output_iterator<const wchar_t&> _OutIt, class... _Args> 335_LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT _OutIt 336format_to(_OutIt __out_it, wstring_view __fmt, const _Args&... __args) { 337 return _VSTD::vformat_to(_VSTD::move(__out_it), __fmt, 338 _VSTD::make_wformat_args(__args...)); 339} 340#endif 341 342_LIBCPP_ALWAYS_INLINE inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT string 343vformat(string_view __fmt, format_args __args) { 344 string __res; 345 _VSTD::vformat_to(_VSTD::back_inserter(__res), __fmt, __args); 346 return __res; 347} 348 349#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS 350_LIBCPP_ALWAYS_INLINE inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT wstring 351vformat(wstring_view __fmt, wformat_args __args) { 352 wstring __res; 353 _VSTD::vformat_to(_VSTD::back_inserter(__res), __fmt, __args); 354 return __res; 355} 356#endif 357 358template <class... _Args> 359_LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT string 360format(string_view __fmt, const _Args&... __args) { 361 return _VSTD::vformat(__fmt, _VSTD::make_format_args(__args...)); 362} 363 364#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS 365template <class... _Args> 366_LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT wstring 367format(wstring_view __fmt, const _Args&... __args) { 368 return _VSTD::vformat(__fmt, _VSTD::make_wformat_args(__args...)); 369} 370#endif 371 372template <output_iterator<const char&> _OutIt, class... _Args> 373_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT format_to_n_result<_OutIt> 374format_to_n(_OutIt __out_it, iter_difference_t<_OutIt> __n, string_view __fmt, 375 const _Args&... __args) { 376 // TODO FMT Improve PoC: using std::string is inefficient. 377 string __str = _VSTD::vformat(__fmt, _VSTD::make_format_args(__args...)); 378 iter_difference_t<_OutIt> __s = __str.size(); 379 iter_difference_t<_OutIt> __m = 380 _VSTD::clamp(__n, iter_difference_t<_OutIt>(0), __s); 381 __out_it = _VSTD::copy_n(__str.begin(), __m, _VSTD::move(__out_it)); 382 return {_VSTD::move(__out_it), __s}; 383} 384 385#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS 386template <output_iterator<const wchar_t&> _OutIt, class... _Args> 387_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT format_to_n_result<_OutIt> 388format_to_n(_OutIt __out_it, iter_difference_t<_OutIt> __n, wstring_view __fmt, 389 const _Args&... __args) { 390 // TODO FMT Improve PoC: using std::string is inefficient. 391 wstring __str = _VSTD::vformat(__fmt, _VSTD::make_wformat_args(__args...)); 392 iter_difference_t<_OutIt> __s = __str.size(); 393 iter_difference_t<_OutIt> __m = 394 _VSTD::clamp(__n, iter_difference_t<_OutIt>(0), __s); 395 __out_it = _VSTD::copy_n(__str.begin(), __m, _VSTD::move(__out_it)); 396 return {_VSTD::move(__out_it), __s}; 397} 398#endif 399 400template <class _CharT> 401_LIBCPP_HIDE_FROM_ABI size_t __vformatted_size(basic_string_view<_CharT> __fmt, auto __args) { 402 __format::__formatted_size_buffer<_CharT> __buffer; 403 _VSTD::__format::__vformat_to(basic_format_parse_context{__fmt, __args.__size()}, 404 _VSTD::__format_context_create(__buffer.make_output_iterator(), __args)); 405 return _VSTD::move(__buffer).result(); 406} 407 408template <class... _Args> 409_LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT size_t formatted_size(string_view __fmt, 410 const _Args&... __args) { 411 return _VSTD::__vformatted_size(__fmt, basic_format_args{_VSTD::make_format_args(__args...)}); 412} 413 414#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS 415template <class... _Args> 416_LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT size_t formatted_size(wstring_view __fmt, 417 const _Args&... __args) { 418 return _VSTD::__vformatted_size(__fmt, basic_format_args{_VSTD::make_wformat_args(__args...)}); 419} 420#endif 421 422#ifndef _LIBCPP_HAS_NO_LOCALIZATION 423 424template <class _OutIt, class _CharT, class _FormatOutIt> 425requires(output_iterator<_OutIt, const _CharT&>) _LIBCPP_HIDE_FROM_ABI _OutIt 426 __vformat_to( 427 _OutIt __out_it, locale __loc, basic_string_view<_CharT> __fmt, 428 basic_format_args<basic_format_context<_FormatOutIt, _CharT>> __args) { 429 if constexpr (same_as<_OutIt, _FormatOutIt>) 430 return _VSTD::__format::__vformat_to( 431 basic_format_parse_context{__fmt, __args.__size()}, 432 _VSTD::__format_context_create(_VSTD::move(__out_it), __args, 433 _VSTD::move(__loc))); 434 else { 435 __format::__format_buffer<_OutIt, _CharT> __buffer{_VSTD::move(__out_it)}; 436 _VSTD::__format::__vformat_to( 437 basic_format_parse_context{__fmt, __args.__size()}, 438 _VSTD::__format_context_create(__buffer.make_output_iterator(), 439 __args, _VSTD::move(__loc))); 440 return _VSTD::move(__buffer).out(); 441 } 442} 443 444template <output_iterator<const char&> _OutIt> 445_LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT _OutIt vformat_to( 446 _OutIt __out_it, locale __loc, string_view __fmt, format_args __args) { 447 return _VSTD::__vformat_to(_VSTD::move(__out_it), _VSTD::move(__loc), __fmt, 448 __args); 449} 450 451#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS 452template <output_iterator<const wchar_t&> _OutIt> 453_LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT _OutIt vformat_to( 454 _OutIt __out_it, locale __loc, wstring_view __fmt, wformat_args __args) { 455 return _VSTD::__vformat_to(_VSTD::move(__out_it), _VSTD::move(__loc), __fmt, 456 __args); 457} 458#endif 459 460template <output_iterator<const char&> _OutIt, class... _Args> 461_LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT _OutIt format_to( 462 _OutIt __out_it, locale __loc, string_view __fmt, const _Args&... __args) { 463 return _VSTD::vformat_to(_VSTD::move(__out_it), _VSTD::move(__loc), __fmt, 464 _VSTD::make_format_args(__args...)); 465} 466 467#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS 468template <output_iterator<const wchar_t&> _OutIt, class... _Args> 469_LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT _OutIt format_to( 470 _OutIt __out_it, locale __loc, wstring_view __fmt, const _Args&... __args) { 471 return _VSTD::vformat_to(_VSTD::move(__out_it), _VSTD::move(__loc), __fmt, 472 _VSTD::make_wformat_args(__args...)); 473} 474#endif 475 476_LIBCPP_ALWAYS_INLINE inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT string 477vformat(locale __loc, string_view __fmt, format_args __args) { 478 string __res; 479 _VSTD::vformat_to(_VSTD::back_inserter(__res), _VSTD::move(__loc), __fmt, 480 __args); 481 return __res; 482} 483 484#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS 485_LIBCPP_ALWAYS_INLINE inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT wstring 486vformat(locale __loc, wstring_view __fmt, wformat_args __args) { 487 wstring __res; 488 _VSTD::vformat_to(_VSTD::back_inserter(__res), _VSTD::move(__loc), __fmt, 489 __args); 490 return __res; 491} 492#endif 493 494template <class... _Args> 495_LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT string 496format(locale __loc, string_view __fmt, const _Args&... __args) { 497 return _VSTD::vformat(_VSTD::move(__loc), __fmt, 498 _VSTD::make_format_args(__args...)); 499} 500 501#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS 502template <class... _Args> 503_LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT wstring 504format(locale __loc, wstring_view __fmt, const _Args&... __args) { 505 return _VSTD::vformat(_VSTD::move(__loc), __fmt, 506 _VSTD::make_wformat_args(__args...)); 507} 508#endif 509 510template <output_iterator<const char&> _OutIt, class... _Args> 511_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT format_to_n_result<_OutIt> 512format_to_n(_OutIt __out_it, iter_difference_t<_OutIt> __n, locale __loc, 513 string_view __fmt, const _Args&... __args) { 514 // TODO FMT Improve PoC: using std::string is inefficient. 515 string __str = _VSTD::vformat(_VSTD::move(__loc), __fmt, 516 _VSTD::make_format_args(__args...)); 517 iter_difference_t<_OutIt> __s = __str.size(); 518 iter_difference_t<_OutIt> __m = 519 _VSTD::clamp(__n, iter_difference_t<_OutIt>(0), __s); 520 __out_it = _VSTD::copy_n(__str.begin(), __m, _VSTD::move(__out_it)); 521 return {_VSTD::move(__out_it), __s}; 522} 523 524#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS 525template <output_iterator<const wchar_t&> _OutIt, class... _Args> 526_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT format_to_n_result<_OutIt> 527format_to_n(_OutIt __out_it, iter_difference_t<_OutIt> __n, locale __loc, 528 wstring_view __fmt, const _Args&... __args) { 529 // TODO FMT Improve PoC: using std::string is inefficient. 530 wstring __str = _VSTD::vformat(_VSTD::move(__loc), __fmt, 531 _VSTD::make_wformat_args(__args...)); 532 iter_difference_t<_OutIt> __s = __str.size(); 533 iter_difference_t<_OutIt> __m = 534 _VSTD::clamp(__n, iter_difference_t<_OutIt>(0), __s); 535 __out_it = _VSTD::copy_n(__str.begin(), __m, _VSTD::move(__out_it)); 536 return {_VSTD::move(__out_it), __s}; 537} 538#endif 539 540template <class _CharT> 541_LIBCPP_HIDE_FROM_ABI size_t __vformatted_size(locale __loc, basic_string_view<_CharT> __fmt, auto __args) { 542 __format::__formatted_size_buffer<_CharT> __buffer; 543 _VSTD::__format::__vformat_to( 544 basic_format_parse_context{__fmt, __args.__size()}, 545 _VSTD::__format_context_create(__buffer.make_output_iterator(), __args, _VSTD::move(__loc))); 546 return _VSTD::move(__buffer).result(); 547} 548 549template <class... _Args> 550_LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT size_t formatted_size(locale __loc, 551 string_view __fmt, 552 const _Args&... __args) { 553 return _VSTD::__vformatted_size(_VSTD::move(__loc), __fmt, basic_format_args{_VSTD::make_format_args(__args...)}); 554} 555 556#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS 557template <class... _Args> 558_LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT size_t formatted_size(locale __loc, 559 wstring_view __fmt, 560 const _Args&... __args) { 561 return _VSTD::__vformatted_size(_VSTD::move(__loc), __fmt, basic_format_args{_VSTD::make_wformat_args(__args...)}); 562} 563#endif 564 565#endif // _LIBCPP_HAS_NO_LOCALIZATION 566 567#endif //_LIBCPP_STD_VER > 17 568 569_LIBCPP_END_NAMESPACE_STD 570 571#endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_FORMAT) 572 573#endif // _LIBCPP_FORMAT 574