2020-11-26 18:12:18 +00:00
|
|
|
// -*- C++ -*-
|
2021-11-17 21:25:01 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2020-11-26 18:12:18 +00:00
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef _LIBCPP_FORMAT
|
|
|
|
#define _LIBCPP_FORMAT
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
namespace std {
|
2020-12-05 10:45:21 +00:00
|
|
|
// [format.context], class template basic_format_context
|
2021-12-18 14:03:26 +00:00
|
|
|
template<class Out, class charT> class basic_format_context;
|
2020-12-05 10:45:21 +00:00
|
|
|
using format_context = basic_format_context<unspecified, char>;
|
|
|
|
using wformat_context = basic_format_context<unspecified, wchar_t>;
|
|
|
|
|
|
|
|
// [format.args], class template basic_format_args
|
2021-12-18 14:03:26 +00:00
|
|
|
template<class Context> class basic_format_args;
|
2020-12-05 10:45:21 +00:00
|
|
|
using format_args = basic_format_args<format_context>;
|
|
|
|
using wformat_args = basic_format_args<wformat_context>;
|
|
|
|
|
2022-07-15 05:42:17 +00:00
|
|
|
// [format.fmt.string], class template basic_format_string
|
2021-10-02 10:38:46 +00:00
|
|
|
template<class charT, class... Args>
|
2022-07-15 05:42:17 +00:00
|
|
|
struct basic_format_string { // since C++23, exposition only before C++23
|
|
|
|
private:
|
|
|
|
basic_string_view<charT> str; // exposition only
|
2021-10-02 10:38:46 +00:00
|
|
|
|
2022-07-15 05:42:17 +00:00
|
|
|
public:
|
|
|
|
template<class T> consteval basic_format_string(const T& s);
|
2023-11-24 16:30:33 +00:00
|
|
|
basic_format_string(runtime-format-string<charT> s) noexcept : str(s.str) {} // since C++26
|
2022-07-15 05:42:17 +00:00
|
|
|
|
|
|
|
constexpr basic_string_view<charT> get() const noexcept { return str; }
|
|
|
|
};
|
2021-10-02 10:38:46 +00:00
|
|
|
template<class... Args>
|
2022-07-15 05:42:17 +00:00
|
|
|
using format_string = // since C++23, exposition only before C++23
|
|
|
|
basic_format_string<char, type_identity_t<Args>...>;
|
2021-10-02 10:38:46 +00:00
|
|
|
template<class... Args>
|
2022-07-15 05:42:17 +00:00
|
|
|
using wformat_string = // since C++23, exposition only before C++23
|
|
|
|
basic_format_string<wchar_t, type_identity_t<Args>...>;
|
2021-10-02 10:38:46 +00:00
|
|
|
|
2023-11-24 16:30:33 +00:00
|
|
|
template<class charT> struct runtime-format-string { // since C++26, exposition-only
|
|
|
|
private:
|
|
|
|
basic_string_view<charT> str; // exposition-only
|
|
|
|
|
|
|
|
public:
|
|
|
|
runtime-format-string(basic_string_view<charT> s) noexcept : str(s) {}
|
|
|
|
|
|
|
|
runtime-format-string(const runtime-format-string&) = delete;
|
|
|
|
runtime-format-string& operator=(const runtime-format-string&) = delete;
|
|
|
|
};
|
|
|
|
|
|
|
|
runtime-format-string<char> runtime_format(string_view fmt) noexcept {
|
|
|
|
return fmt;
|
|
|
|
}
|
|
|
|
runtime-format-string<wchar_t> runtime_format(wstring_view fmt) noexcept {
|
|
|
|
return fmt;
|
|
|
|
}
|
|
|
|
|
2020-12-19 12:52:07 +00:00
|
|
|
// [format.functions], formatting functions
|
|
|
|
template<class... Args>
|
2021-10-05 17:25:37 +00:00
|
|
|
string format(format-string<Args...> fmt, Args&&... args);
|
2020-12-19 12:52:07 +00:00
|
|
|
template<class... Args>
|
2021-10-05 17:25:37 +00:00
|
|
|
wstring format(wformat-string<Args...> fmt, Args&&... args);
|
2020-12-19 12:52:07 +00:00
|
|
|
template<class... Args>
|
2021-10-05 17:25:37 +00:00
|
|
|
string format(const locale& loc, format-string<Args...> fmt, Args&&... args);
|
2020-12-19 12:52:07 +00:00
|
|
|
template<class... Args>
|
2021-10-05 17:25:37 +00:00
|
|
|
wstring format(const locale& loc, wformat-string<Args...> fmt, Args&&... args);
|
2020-12-19 12:52:07 +00:00
|
|
|
|
|
|
|
string vformat(string_view fmt, format_args args);
|
|
|
|
wstring vformat(wstring_view fmt, wformat_args args);
|
|
|
|
string vformat(const locale& loc, string_view fmt, format_args args);
|
|
|
|
wstring vformat(const locale& loc, wstring_view fmt, wformat_args args);
|
|
|
|
|
|
|
|
template<class Out, class... Args>
|
2021-10-05 17:25:37 +00:00
|
|
|
Out format_to(Out out, format-string<Args...> fmt, Args&&... args);
|
2020-12-19 12:52:07 +00:00
|
|
|
template<class Out, class... Args>
|
2021-10-05 17:25:37 +00:00
|
|
|
Out format_to(Out out, wformat-string<Args...> fmt, Args&&... args);
|
2020-12-19 12:52:07 +00:00
|
|
|
template<class Out, class... Args>
|
2021-10-05 17:25:37 +00:00
|
|
|
Out format_to(Out out, const locale& loc, format-string<Args...> fmt, Args&&... args);
|
2020-12-19 12:52:07 +00:00
|
|
|
template<class Out, class... Args>
|
2021-10-05 17:25:37 +00:00
|
|
|
Out format_to(Out out, const locale& loc, wformat-string<Args...> fmt, Args&&... args);
|
2020-12-19 12:52:07 +00:00
|
|
|
|
|
|
|
template<class Out>
|
2021-09-04 11:26:58 +00:00
|
|
|
Out vformat_to(Out out, string_view fmt, format_args args);
|
2020-12-19 12:52:07 +00:00
|
|
|
template<class Out>
|
2021-09-04 11:26:58 +00:00
|
|
|
Out vformat_to(Out out, wstring_view fmt, wformat_args args);
|
2020-12-19 12:52:07 +00:00
|
|
|
template<class Out>
|
|
|
|
Out vformat_to(Out out, const locale& loc, string_view fmt,
|
2021-09-04 11:26:58 +00:00
|
|
|
format_args char> args);
|
2020-12-19 12:52:07 +00:00
|
|
|
template<class Out>
|
|
|
|
Out vformat_to(Out out, const locale& loc, wstring_view fmt,
|
2021-09-04 11:26:58 +00:00
|
|
|
wformat_args args);
|
2020-12-19 12:52:07 +00:00
|
|
|
|
|
|
|
template<class Out> struct format_to_n_result {
|
|
|
|
Out out;
|
|
|
|
iter_difference_t<Out> size;
|
|
|
|
};
|
2021-12-18 14:03:26 +00:00
|
|
|
template<class Out, class... Args>
|
2020-12-19 12:52:07 +00:00
|
|
|
format_to_n_result<Out> format_to_n(Out out, iter_difference_t<Out> n,
|
2021-10-05 17:25:37 +00:00
|
|
|
format-string<Args...> fmt, Args&&... args);
|
2020-12-19 12:52:07 +00:00
|
|
|
template<class Out, class... Args>
|
|
|
|
format_to_n_result<Out> format_to_n(Out out, iter_difference_t<Out> n,
|
2021-10-05 17:25:37 +00:00
|
|
|
wformat-string<Args...> fmt, Args&&... args);
|
2020-12-19 12:52:07 +00:00
|
|
|
template<class Out, class... Args>
|
|
|
|
format_to_n_result<Out> format_to_n(Out out, iter_difference_t<Out> n,
|
2021-10-02 10:38:46 +00:00
|
|
|
const locale& loc, format-string<Args...> fmt,
|
2021-10-05 17:25:37 +00:00
|
|
|
Args&&... args);
|
2020-12-19 12:52:07 +00:00
|
|
|
template<class Out, class... Args>
|
|
|
|
format_to_n_result<Out> format_to_n(Out out, iter_difference_t<Out> n,
|
2021-10-02 10:38:46 +00:00
|
|
|
const locale& loc, wformat-string<Args...> fmt,
|
2021-10-05 17:25:37 +00:00
|
|
|
Args&&... args);
|
2020-12-19 12:52:07 +00:00
|
|
|
|
|
|
|
template<class... Args>
|
2021-10-05 17:25:37 +00:00
|
|
|
size_t formatted_size(format-string<Args...> fmt, Args&&... args);
|
2020-12-19 12:52:07 +00:00
|
|
|
template<class... Args>
|
2021-10-05 17:25:37 +00:00
|
|
|
size_t formatted_size(wformat-string<Args...> fmt, Args&&... args);
|
2020-12-19 12:52:07 +00:00
|
|
|
template<class... Args>
|
2021-10-05 17:25:37 +00:00
|
|
|
size_t formatted_size(const locale& loc, format-string<Args...> fmt, Args&&... args);
|
2020-12-19 12:52:07 +00:00
|
|
|
template<class... Args>
|
2021-10-05 17:25:37 +00:00
|
|
|
size_t formatted_size(const locale& loc, wformat-string<Args...> fmt, Args&&... args);
|
2020-12-19 12:52:07 +00:00
|
|
|
|
|
|
|
// [format.formatter], formatter
|
2021-12-18 14:03:26 +00:00
|
|
|
template<class T, class charT = char> struct formatter;
|
2020-12-19 12:52:07 +00:00
|
|
|
|
2020-12-05 10:45:21 +00:00
|
|
|
// [format.parse.ctx], class template basic_format_parse_context
|
2021-12-18 14:03:26 +00:00
|
|
|
template<class charT> class basic_format_parse_context;
|
2020-12-05 10:45:21 +00:00
|
|
|
using format_parse_context = basic_format_parse_context<char>;
|
|
|
|
using wformat_parse_context = basic_format_parse_context<wchar_t>;
|
|
|
|
|
2022-10-19 17:50:48 +00:00
|
|
|
// [format.range], formatting of ranges
|
|
|
|
// [format.range.fmtkind], variable template format_kind
|
|
|
|
enum class range_format { // since C++23
|
|
|
|
disabled,
|
|
|
|
map,
|
|
|
|
set,
|
|
|
|
sequence,
|
|
|
|
string,
|
|
|
|
debug_string
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class R>
|
|
|
|
constexpr unspecified format_kind = unspecified; // since C++23
|
|
|
|
|
|
|
|
template<ranges::input_range R>
|
|
|
|
requires same_as<R, remove_cvref_t<R>>
|
|
|
|
constexpr range_format format_kind<R> = see below; // since C++23
|
|
|
|
|
2022-05-05 16:57:32 +00:00
|
|
|
// [format.range.formatter], class template range_formatter
|
|
|
|
template<class T, class charT = char>
|
|
|
|
requires same_as<remove_cvref_t<T>, T> && formattable<T, charT>
|
|
|
|
class range_formatter; // since C++23
|
|
|
|
|
2022-10-19 17:50:48 +00:00
|
|
|
// [format.range.fmtdef], class template range-default-formatter
|
|
|
|
template<range_format K, ranges::input_range R, class charT>
|
|
|
|
struct range-default-formatter; // exposition only, since C++23
|
|
|
|
|
|
|
|
// [format.range.fmtmap], [format.range.fmtset], [format.range.fmtstr],
|
|
|
|
// specializations for maps, sets, and strings
|
|
|
|
template<ranges::input_range R, class charT>
|
|
|
|
requires (format_kind<R> != range_format::disabled) &&
|
|
|
|
formattable<ranges::range_reference_t<R>, charT>
|
|
|
|
struct formatter<R, charT> : range-default-formatter<format_kind<R>, R, charT> { }; // since C++23
|
|
|
|
|
2020-12-05 10:45:21 +00:00
|
|
|
// [format.arguments], arguments
|
|
|
|
// [format.arg], class template basic_format_arg
|
2021-12-18 14:03:26 +00:00
|
|
|
template<class Context> class basic_format_arg;
|
2020-12-05 10:45:21 +00:00
|
|
|
|
|
|
|
template<class Visitor, class Context>
|
2024-01-22 16:55:12 +00:00
|
|
|
see below visit_format_arg(Visitor&& vis, basic_format_arg<Context> arg);
|
2020-12-05 10:45:21 +00:00
|
|
|
|
|
|
|
// [format.arg.store], class template format-arg-store
|
2021-12-18 14:03:26 +00:00
|
|
|
template<class Context, class... Args> struct format-arg-store; // exposition only
|
2020-12-05 10:45:21 +00:00
|
|
|
|
|
|
|
template<class Context = format_context, class... Args>
|
|
|
|
format-arg-store<Context, Args...>
|
2023-12-09 11:32:17 +00:00
|
|
|
make_format_args(Args&... args);
|
2020-12-05 10:45:21 +00:00
|
|
|
template<class... Args>
|
|
|
|
format-arg-store<wformat_context, Args...>
|
2023-12-09 11:32:17 +00:00
|
|
|
make_wformat_args(Args&... args);
|
2020-12-05 10:45:21 +00:00
|
|
|
|
2020-11-26 18:12:18 +00:00
|
|
|
// [format.error], class format_error
|
2021-12-18 14:03:26 +00:00
|
|
|
class format_error;
|
2020-11-26 18:12:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2022-03-25 16:55:36 +00:00
|
|
|
#include <__assert> // all public C++ headers provide the assertion handler
|
2020-11-26 18:12:18 +00:00
|
|
|
#include <__config>
|
2021-09-04 19:05:23 +00:00
|
|
|
#include <__format/buffer.h>
|
2022-01-30 15:08:19 +00:00
|
|
|
#include <__format/concepts.h>
|
2022-05-05 16:57:32 +00:00
|
|
|
#include <__format/container_adaptor.h>
|
2021-09-26 13:47:42 +00:00
|
|
|
#include <__format/enable_insertable.h>
|
2020-12-05 10:45:21 +00:00
|
|
|
#include <__format/format_arg.h>
|
2021-10-03 11:11:53 +00:00
|
|
|
#include <__format/format_arg_store.h>
|
2020-12-05 10:45:21 +00:00
|
|
|
#include <__format/format_args.h>
|
|
|
|
#include <__format/format_context.h>
|
2021-04-25 15:58:03 +00:00
|
|
|
#include <__format/format_error.h>
|
2022-09-11 11:05:26 +00:00
|
|
|
#include <__format/format_functions.h>
|
[libc++][format] Adds integer formatter.
Implements the formatter for all fundamental integer types
(except `char`, `wchar_t`, and `bool`).
[format.formatter.spec]/2.3
For each charT, for each cv-unqualified arithmetic type ArithmeticT other
than char, wchar_t, char8_t, char16_t, or char32_t, a specialization
```
template<> struct formatter<ArithmeticT, charT>;
```
This removes the stub implemented in D96664.
As an extension it adds partial support for 128-bit integer types.
Implements parts of:
- P0645 Text Formatting
- P1652 Printf corner cases in std::format
Completes:
- LWG-3248 #b, #B, #o, #x, and #X presentation types misformat negative numbers
Reviewed By: #libc, ldionne, vitaut
Differential Revision: https://reviews.llvm.org/D103433
2020-12-14 16:39:15 +00:00
|
|
|
#include <__format/format_fwd.h>
|
2021-04-25 16:23:42 +00:00
|
|
|
#include <__format/format_parse_context.h>
|
2020-12-19 12:52:07 +00:00
|
|
|
#include <__format/format_string.h>
|
2021-11-13 18:43:32 +00:00
|
|
|
#include <__format/format_to_n_result.h>
|
2020-12-19 12:52:07 +00:00
|
|
|
#include <__format/formatter.h>
|
2020-12-14 16:39:15 +00:00
|
|
|
#include <__format/formatter_bool.h>
|
2020-12-14 16:39:15 +00:00
|
|
|
#include <__format/formatter_char.h>
|
2020-12-14 16:39:15 +00:00
|
|
|
#include <__format/formatter_floating_point.h>
|
[libc++][format] Adds integer formatter.
Implements the formatter for all fundamental integer types
(except `char`, `wchar_t`, and `bool`).
[format.formatter.spec]/2.3
For each charT, for each cv-unqualified arithmetic type ArithmeticT other
than char, wchar_t, char8_t, char16_t, or char32_t, a specialization
```
template<> struct formatter<ArithmeticT, charT>;
```
This removes the stub implemented in D96664.
As an extension it adds partial support for 128-bit integer types.
Implements parts of:
- P0645 Text Formatting
- P1652 Printf corner cases in std::format
Completes:
- LWG-3248 #b, #B, #o, #x, and #X presentation types misformat negative numbers
Reviewed By: #libc, ldionne, vitaut
Differential Revision: https://reviews.llvm.org/D103433
2020-12-14 16:39:15 +00:00
|
|
|
#include <__format/formatter_integer.h>
|
2021-11-28 13:43:43 +00:00
|
|
|
#include <__format/formatter_pointer.h>
|
[libc++][format] Adds string formatter.
Implements the formatter for all string types.
[format.formatter.spec]/2.2
For each charT, the string type specializations
```
template<> struct formatter<charT*, charT>;
template<> struct formatter<const charT*, charT>;
template<size_t N> struct formatter<const charT[N], charT>;
template<class traits, class Allocator>
struct formatter<basic_string<charT, traits, Allocator>, charT>;
template<class traits>
struct formatter<basic_string_view<charT, traits>, charT>;
```
This removes the stub implemented in D96664.
Implements parts of:
- P0645 Text Formatting
- P1868 width: clarifying units of width and precision in std::format
Reviewed By: #libc, ldionne, vitaut
Differential Revision: https://reviews.llvm.org/D103425
2020-12-14 16:39:15 +00:00
|
|
|
#include <__format/formatter_string.h>
|
2022-05-05 16:57:32 +00:00
|
|
|
#include <__format/formatter_tuple.h>
|
[libc++][format] Adds string formatter.
Implements the formatter for all string types.
[format.formatter.spec]/2.2
For each charT, the string type specializations
```
template<> struct formatter<charT*, charT>;
template<> struct formatter<const charT*, charT>;
template<size_t N> struct formatter<const charT[N], charT>;
template<class traits, class Allocator>
struct formatter<basic_string<charT, traits, Allocator>, charT>;
template<class traits>
struct formatter<basic_string_view<charT, traits>, charT>;
```
This removes the stub implemented in D96664.
Implements parts of:
- P0645 Text Formatting
- P1868 width: clarifying units of width and precision in std::format
Reviewed By: #libc, ldionne, vitaut
Differential Revision: https://reviews.llvm.org/D103425
2020-12-14 16:39:15 +00:00
|
|
|
#include <__format/parser_std_format_spec.h>
|
2022-10-19 17:50:48 +00:00
|
|
|
#include <__format/range_default_formatter.h>
|
2022-05-05 16:57:32 +00:00
|
|
|
#include <__format/range_formatter.h>
|
2022-05-28 13:30:10 +00:00
|
|
|
#include <__format/unicode.h>
|
2023-05-17 17:17:52 +00:00
|
|
|
#include <version>
|
2021-07-25 07:18:53 +00:00
|
|
|
|
2020-11-26 18:12:18 +00:00
|
|
|
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
|
2022-02-02 01:16:40 +00:00
|
|
|
# pragma GCC system_header
|
2020-11-26 18:12:18 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif // _LIBCPP_FORMAT
|