xref: /freebsd/contrib/llvm-project/libcxx/include/format (revision d4eeb02986980bf33dd56c41ceb9fc5f180c0d47)
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// Make sure all feature-test macros are available.
121#include <version>
122// Enable the contents of the header only when libc++ was built with LIBCXX_ENABLE_INCOMPLETE_FEATURES.
123#if !defined(_LIBCPP_HAS_NO_INCOMPLETE_FORMAT)
124
125#include <__config>
126#include <__debug>
127#include <__format/format_arg.h>
128#include <__format/format_args.h>
129#include <__format/format_context.h>
130#include <__format/format_error.h>
131#include <__format/format_fwd.h>
132#include <__format/format_parse_context.h>
133#include <__format/format_string.h>
134#include <__format/format_to_n_result.h>
135#include <__format/formatter.h>
136#include <__format/formatter_bool.h>
137#include <__format/formatter_char.h>
138#include <__format/formatter_floating_point.h>
139#include <__format/formatter_integer.h>
140#include <__format/formatter_pointer.h>
141#include <__format/formatter_string.h>
142#include <__format/parser_std_format_spec.h>
143#include <__variant/monostate.h>
144#include <array>
145#include <concepts>
146#include <string>
147#include <string_view>
148#include <type_traits>
149
150#ifndef _LIBCPP_HAS_NO_LOCALIZATION
151#include <locale>
152#endif
153
154#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
155#pragma GCC system_header
156#endif
157
158_LIBCPP_PUSH_MACROS
159#include <__undef_macros>
160
161_LIBCPP_BEGIN_NAMESPACE_STD
162
163#if _LIBCPP_STD_VER > 17
164
165// TODO FMT Remove this once we require compilers with proper C++20 support.
166// If the compiler has no concepts support, the format header will be disabled.
167// Without concepts support enable_if needs to be used and that too much effort
168// to support compilers with partial C++20 support.
169#if !defined(_LIBCPP_HAS_NO_CONCEPTS)
170
171// TODO FMT Move the implementation in this file to its own granular headers.
172
173// TODO FMT Evaluate which templates should be external templates. This
174// improves the efficiency of the header. However since the header is still
175// under heavy development and not all classes are stable it makes no sense
176// to do this optimization now.
177
178using format_args = basic_format_args<format_context>;
179#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
180using wformat_args = basic_format_args<wformat_context>;
181#endif
182
183template <class _Context, class... _Args>
184struct _LIBCPP_TEMPLATE_VIS __format_arg_store {
185  // TODO FMT Use a built-in array.
186  array<basic_format_arg<_Context>, sizeof...(_Args)> __args;
187};
188
189template <class _Context = format_context, class... _Args>
190_LIBCPP_HIDE_FROM_ABI __format_arg_store<_Context, _Args...>
191make_format_args(const _Args&... __args) {
192  return {basic_format_arg<_Context>(__args)...};
193}
194
195#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
196template <class... _Args>
197_LIBCPP_HIDE_FROM_ABI __format_arg_store<wformat_context, _Args...>
198make_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    basic_string<_CharT> __str;
304    _VSTD::__format::__vformat_to(
305        basic_format_parse_context{__fmt, __args.__size()},
306        _VSTD::__format_context_create(_VSTD::back_inserter(__str), __args));
307    return _VSTD::copy_n(__str.begin(), __str.size(), _VSTD::move(__out_it));
308  }
309}
310
311template <output_iterator<const char&> _OutIt>
312_LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT _OutIt
313vformat_to(_OutIt __out_it, string_view __fmt, format_args __args) {
314  return _VSTD::__vformat_to(_VSTD::move(__out_it), __fmt, __args);
315}
316
317#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
318template <output_iterator<const wchar_t&> _OutIt>
319_LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT _OutIt
320vformat_to(_OutIt __out_it, wstring_view __fmt, wformat_args __args) {
321  return _VSTD::__vformat_to(_VSTD::move(__out_it), __fmt, __args);
322}
323#endif
324
325template <output_iterator<const char&> _OutIt, class... _Args>
326_LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT _OutIt
327format_to(_OutIt __out_it, string_view __fmt, const _Args&... __args) {
328  return _VSTD::vformat_to(_VSTD::move(__out_it), __fmt,
329                           _VSTD::make_format_args(__args...));
330}
331
332#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
333template <output_iterator<const wchar_t&> _OutIt, class... _Args>
334_LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT _OutIt
335format_to(_OutIt __out_it, wstring_view __fmt, const _Args&... __args) {
336  return _VSTD::vformat_to(_VSTD::move(__out_it), __fmt,
337                           _VSTD::make_wformat_args(__args...));
338}
339#endif
340
341_LIBCPP_ALWAYS_INLINE inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT string
342vformat(string_view __fmt, format_args __args) {
343  string __res;
344  _VSTD::vformat_to(_VSTD::back_inserter(__res), __fmt, __args);
345  return __res;
346}
347
348#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
349_LIBCPP_ALWAYS_INLINE inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT wstring
350vformat(wstring_view __fmt, wformat_args __args) {
351  wstring __res;
352  _VSTD::vformat_to(_VSTD::back_inserter(__res), __fmt, __args);
353  return __res;
354}
355#endif
356
357template <class... _Args>
358_LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT string
359format(string_view __fmt, const _Args&... __args) {
360  return _VSTD::vformat(__fmt, _VSTD::make_format_args(__args...));
361}
362
363#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
364template <class... _Args>
365_LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT wstring
366format(wstring_view __fmt, const _Args&... __args) {
367  return _VSTD::vformat(__fmt, _VSTD::make_wformat_args(__args...));
368}
369#endif
370
371template <output_iterator<const char&> _OutIt, class... _Args>
372_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT format_to_n_result<_OutIt>
373format_to_n(_OutIt __out_it, iter_difference_t<_OutIt> __n, string_view __fmt,
374            const _Args&... __args) {
375  // TODO FMT Improve PoC: using std::string is inefficient.
376  string __str = _VSTD::vformat(__fmt, _VSTD::make_format_args(__args...));
377  iter_difference_t<_OutIt> __s = __str.size();
378  iter_difference_t<_OutIt> __m =
379      _VSTD::clamp(__n, iter_difference_t<_OutIt>(0), __s);
380  __out_it = _VSTD::copy_n(__str.begin(), __m, _VSTD::move(__out_it));
381  return {_VSTD::move(__out_it), __s};
382}
383
384#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
385template <output_iterator<const wchar_t&> _OutIt, class... _Args>
386_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT format_to_n_result<_OutIt>
387format_to_n(_OutIt __out_it, iter_difference_t<_OutIt> __n, wstring_view __fmt,
388            const _Args&... __args) {
389  // TODO FMT Improve PoC: using std::string is inefficient.
390  wstring __str = _VSTD::vformat(__fmt, _VSTD::make_wformat_args(__args...));
391  iter_difference_t<_OutIt> __s = __str.size();
392  iter_difference_t<_OutIt> __m =
393      _VSTD::clamp(__n, iter_difference_t<_OutIt>(0), __s);
394  __out_it = _VSTD::copy_n(__str.begin(), __m, _VSTD::move(__out_it));
395  return {_VSTD::move(__out_it), __s};
396}
397#endif
398
399template <class... _Args>
400_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT size_t
401formatted_size(string_view __fmt, const _Args&... __args) {
402  // TODO FMT Improve PoC: using std::string is inefficient.
403  return _VSTD::vformat(__fmt, _VSTD::make_format_args(__args...)).size();
404}
405
406#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
407template <class... _Args>
408_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT size_t
409formatted_size(wstring_view __fmt, const _Args&... __args) {
410  // TODO FMT Improve PoC: using std::string is inefficient.
411  return _VSTD::vformat(__fmt, _VSTD::make_wformat_args(__args...)).size();
412}
413#endif
414
415#ifndef _LIBCPP_HAS_NO_LOCALIZATION
416
417template <class _OutIt, class _CharT, class _FormatOutIt>
418requires(output_iterator<_OutIt, const _CharT&>) _LIBCPP_HIDE_FROM_ABI _OutIt
419    __vformat_to(
420        _OutIt __out_it, locale __loc, basic_string_view<_CharT> __fmt,
421        basic_format_args<basic_format_context<_FormatOutIt, _CharT>> __args) {
422  if constexpr (same_as<_OutIt, _FormatOutIt>)
423    return _VSTD::__format::__vformat_to(
424        basic_format_parse_context{__fmt, __args.__size()},
425        _VSTD::__format_context_create(_VSTD::move(__out_it), __args,
426                                       _VSTD::move(__loc)));
427  else {
428    basic_string<_CharT> __str;
429    _VSTD::__format::__vformat_to(
430        basic_format_parse_context{__fmt, __args.__size()},
431        _VSTD::__format_context_create(_VSTD::back_inserter(__str), __args,
432                                       _VSTD::move(__loc)));
433    return _VSTD::copy_n(__str.begin(), __str.size(), _VSTD::move(__out_it));
434  }
435}
436
437template <output_iterator<const char&> _OutIt>
438_LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT _OutIt vformat_to(
439    _OutIt __out_it, locale __loc, string_view __fmt, format_args __args) {
440  return _VSTD::__vformat_to(_VSTD::move(__out_it), _VSTD::move(__loc), __fmt,
441                             __args);
442}
443
444#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
445template <output_iterator<const wchar_t&> _OutIt>
446_LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT _OutIt vformat_to(
447    _OutIt __out_it, locale __loc, wstring_view __fmt, wformat_args __args) {
448  return _VSTD::__vformat_to(_VSTD::move(__out_it), _VSTD::move(__loc), __fmt,
449                             __args);
450}
451#endif
452
453template <output_iterator<const char&> _OutIt, class... _Args>
454_LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT _OutIt format_to(
455    _OutIt __out_it, locale __loc, string_view __fmt, const _Args&... __args) {
456  return _VSTD::vformat_to(_VSTD::move(__out_it), _VSTD::move(__loc), __fmt,
457                           _VSTD::make_format_args(__args...));
458}
459
460#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
461template <output_iterator<const wchar_t&> _OutIt, class... _Args>
462_LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT _OutIt format_to(
463    _OutIt __out_it, locale __loc, wstring_view __fmt, const _Args&... __args) {
464  return _VSTD::vformat_to(_VSTD::move(__out_it), _VSTD::move(__loc), __fmt,
465                           _VSTD::make_wformat_args(__args...));
466}
467#endif
468
469_LIBCPP_ALWAYS_INLINE inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT string
470vformat(locale __loc, string_view __fmt, format_args __args) {
471  string __res;
472  _VSTD::vformat_to(_VSTD::back_inserter(__res), _VSTD::move(__loc), __fmt,
473                    __args);
474  return __res;
475}
476
477#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
478_LIBCPP_ALWAYS_INLINE inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT wstring
479vformat(locale __loc, wstring_view __fmt, wformat_args __args) {
480  wstring __res;
481  _VSTD::vformat_to(_VSTD::back_inserter(__res), _VSTD::move(__loc), __fmt,
482                    __args);
483  return __res;
484}
485#endif
486
487template <class... _Args>
488_LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT string
489format(locale __loc, string_view __fmt, const _Args&... __args) {
490  return _VSTD::vformat(_VSTD::move(__loc), __fmt,
491                        _VSTD::make_format_args(__args...));
492}
493
494#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
495template <class... _Args>
496_LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT wstring
497format(locale __loc, wstring_view __fmt, const _Args&... __args) {
498  return _VSTD::vformat(_VSTD::move(__loc), __fmt,
499                        _VSTD::make_wformat_args(__args...));
500}
501#endif
502
503template <output_iterator<const char&> _OutIt, class... _Args>
504_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT format_to_n_result<_OutIt>
505format_to_n(_OutIt __out_it, iter_difference_t<_OutIt> __n, locale __loc,
506            string_view __fmt, const _Args&... __args) {
507  // TODO FMT Improve PoC: using std::string is inefficient.
508  string __str = _VSTD::vformat(_VSTD::move(__loc), __fmt,
509                                _VSTD::make_format_args(__args...));
510  iter_difference_t<_OutIt> __s = __str.size();
511  iter_difference_t<_OutIt> __m =
512      _VSTD::clamp(__n, iter_difference_t<_OutIt>(0), __s);
513  __out_it = _VSTD::copy_n(__str.begin(), __m, _VSTD::move(__out_it));
514  return {_VSTD::move(__out_it), __s};
515}
516
517#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
518template <output_iterator<const wchar_t&> _OutIt, class... _Args>
519_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT format_to_n_result<_OutIt>
520format_to_n(_OutIt __out_it, iter_difference_t<_OutIt> __n, locale __loc,
521            wstring_view __fmt, const _Args&... __args) {
522  // TODO FMT Improve PoC: using std::string is inefficient.
523  wstring __str = _VSTD::vformat(_VSTD::move(__loc), __fmt,
524                                 _VSTD::make_wformat_args(__args...));
525  iter_difference_t<_OutIt> __s = __str.size();
526  iter_difference_t<_OutIt> __m =
527      _VSTD::clamp(__n, iter_difference_t<_OutIt>(0), __s);
528  __out_it = _VSTD::copy_n(__str.begin(), __m, _VSTD::move(__out_it));
529  return {_VSTD::move(__out_it), __s};
530}
531#endif
532
533template <class... _Args>
534_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT size_t
535formatted_size(locale __loc, string_view __fmt, const _Args&... __args) {
536  // TODO FMT Improve PoC: using std::string is inefficient.
537  return _VSTD::vformat(_VSTD::move(__loc), __fmt,
538                        _VSTD::make_format_args(__args...))
539      .size();
540}
541
542#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
543template <class... _Args>
544_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT size_t
545formatted_size(locale __loc, wstring_view __fmt, const _Args&... __args) {
546  // TODO FMT Improve PoC: using std::string is inefficient.
547  return _VSTD::vformat(_VSTD::move(__loc), __fmt,
548                        _VSTD::make_wformat_args(__args...))
549      .size();
550}
551#endif
552
553#endif // _LIBCPP_HAS_NO_LOCALIZATION
554
555#endif // !defined(_LIBCPP_HAS_NO_CONCEPTS)
556#endif //_LIBCPP_STD_VER > 17
557
558_LIBCPP_END_NAMESPACE_STD
559
560_LIBCPP_POP_MACROS
561
562#endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_FORMAT)
563
564#endif // _LIBCPP_FORMAT
565