format 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. #ifndef _LIBCPP_FORMAT
  10. #define _LIBCPP_FORMAT
  11. /*
  12. namespace std {
  13. // [format.context], class template basic_format_context
  14. template<class Out, class charT> class basic_format_context;
  15. using format_context = basic_format_context<unspecified, char>;
  16. using wformat_context = basic_format_context<unspecified, wchar_t>;
  17. // [format.args], class template basic_format_args
  18. template<class Context> class basic_format_args;
  19. using format_args = basic_format_args<format_context>;
  20. using wformat_args = basic_format_args<wformat_context>;
  21. // [format.fmt.string], class template basic_format_string
  22. template<class charT, class... Args>
  23. struct basic_format_string { // since C++23, exposition only before C++23
  24. private:
  25. basic_string_view<charT> str; // exposition only
  26. public:
  27. template<class T> consteval basic_format_string(const T& s);
  28. basic_format_string(runtime-format-string<charT> s) noexcept : str(s.str) {} // since C++26
  29. constexpr basic_string_view<charT> get() const noexcept { return str; }
  30. };
  31. template<class... Args>
  32. using format_string = // since C++23, exposition only before C++23
  33. basic_format_string<char, type_identity_t<Args>...>;
  34. template<class... Args>
  35. using wformat_string = // since C++23, exposition only before C++23
  36. basic_format_string<wchar_t, type_identity_t<Args>...>;
  37. template<class charT> struct runtime-format-string { // since C++26, exposition-only
  38. private:
  39. basic_string_view<charT> str; // exposition-only
  40. public:
  41. runtime-format-string(basic_string_view<charT> s) noexcept : str(s) {}
  42. runtime-format-string(const runtime-format-string&) = delete;
  43. runtime-format-string& operator=(const runtime-format-string&) = delete;
  44. };
  45. runtime-format-string<char> runtime_format(string_view fmt) noexcept {
  46. return fmt;
  47. }
  48. runtime-format-string<wchar_t> runtime_format(wstring_view fmt) noexcept {
  49. return fmt;
  50. }
  51. // [format.functions], formatting functions
  52. template<class... Args>
  53. string format(format-string<Args...> fmt, Args&&... args);
  54. template<class... Args>
  55. wstring format(wformat-string<Args...> fmt, Args&&... args);
  56. template<class... Args>
  57. string format(const locale& loc, format-string<Args...> fmt, Args&&... args);
  58. template<class... Args>
  59. wstring format(const locale& loc, wformat-string<Args...> fmt, Args&&... args);
  60. string vformat(string_view fmt, format_args args);
  61. wstring vformat(wstring_view fmt, wformat_args args);
  62. string vformat(const locale& loc, string_view fmt, format_args args);
  63. wstring vformat(const locale& loc, wstring_view fmt, wformat_args args);
  64. template<class Out, class... Args>
  65. Out format_to(Out out, format-string<Args...> fmt, Args&&... args);
  66. template<class Out, class... Args>
  67. Out format_to(Out out, wformat-string<Args...> fmt, Args&&... args);
  68. template<class Out, class... Args>
  69. Out format_to(Out out, const locale& loc, format-string<Args...> fmt, Args&&... args);
  70. template<class Out, class... Args>
  71. Out format_to(Out out, const locale& loc, wformat-string<Args...> fmt, Args&&... args);
  72. template<class Out>
  73. Out vformat_to(Out out, string_view fmt, format_args args);
  74. template<class Out>
  75. Out vformat_to(Out out, wstring_view fmt, wformat_args args);
  76. template<class Out>
  77. Out vformat_to(Out out, const locale& loc, string_view fmt,
  78. format_args char> args);
  79. template<class Out>
  80. Out vformat_to(Out out, const locale& loc, wstring_view fmt,
  81. wformat_args args);
  82. template<class Out> struct format_to_n_result {
  83. Out out;
  84. iter_difference_t<Out> size;
  85. };
  86. template<class Out, class... Args>
  87. format_to_n_result<Out> format_to_n(Out out, iter_difference_t<Out> n,
  88. format-string<Args...> fmt, Args&&... args);
  89. template<class Out, class... Args>
  90. format_to_n_result<Out> format_to_n(Out out, iter_difference_t<Out> n,
  91. wformat-string<Args...> fmt, Args&&... args);
  92. template<class Out, class... Args>
  93. format_to_n_result<Out> format_to_n(Out out, iter_difference_t<Out> n,
  94. const locale& loc, format-string<Args...> fmt,
  95. Args&&... args);
  96. template<class Out, class... Args>
  97. format_to_n_result<Out> format_to_n(Out out, iter_difference_t<Out> n,
  98. const locale& loc, wformat-string<Args...> fmt,
  99. Args&&... args);
  100. template<class... Args>
  101. size_t formatted_size(format-string<Args...> fmt, Args&&... args);
  102. template<class... Args>
  103. size_t formatted_size(wformat-string<Args...> fmt, Args&&... args);
  104. template<class... Args>
  105. size_t formatted_size(const locale& loc, format-string<Args...> fmt, Args&&... args);
  106. template<class... Args>
  107. size_t formatted_size(const locale& loc, wformat-string<Args...> fmt, Args&&... args);
  108. // [format.formatter], formatter
  109. template<class T, class charT = char> struct formatter;
  110. // [format.parse.ctx], class template basic_format_parse_context
  111. template<class charT> class basic_format_parse_context;
  112. using format_parse_context = basic_format_parse_context<char>;
  113. using wformat_parse_context = basic_format_parse_context<wchar_t>;
  114. // [format.range], formatting of ranges
  115. // [format.range.fmtkind], variable template format_kind
  116. enum class range_format { // since C++23
  117. disabled,
  118. map,
  119. set,
  120. sequence,
  121. string,
  122. debug_string
  123. };
  124. template<class R>
  125. constexpr unspecified format_kind = unspecified; // since C++23
  126. template<ranges::input_range R>
  127. requires same_as<R, remove_cvref_t<R>>
  128. constexpr range_format format_kind<R> = see below; // since C++23
  129. // [format.range.formatter], class template range_formatter
  130. template<class T, class charT = char>
  131. requires same_as<remove_cvref_t<T>, T> && formattable<T, charT>
  132. class range_formatter; // since C++23
  133. // [format.range.fmtdef], class template range-default-formatter
  134. template<range_format K, ranges::input_range R, class charT>
  135. struct range-default-formatter; // exposition only, since C++23
  136. // [format.range.fmtmap], [format.range.fmtset], [format.range.fmtstr],
  137. // specializations for maps, sets, and strings
  138. template<ranges::input_range R, class charT>
  139. requires (format_kind<R> != range_format::disabled) &&
  140. formattable<ranges::range_reference_t<R>, charT>
  141. struct formatter<R, charT> : range-default-formatter<format_kind<R>, R, charT> { }; // since C++23
  142. // [format.arguments], arguments
  143. // [format.arg], class template basic_format_arg
  144. template<class Context> class basic_format_arg;
  145. template<class Visitor, class Context>
  146. see below visit_format_arg(Visitor&& vis, basic_format_arg<Context> arg); // Deprecated in C++26
  147. // [format.arg.store], class template format-arg-store
  148. template<class Context, class... Args> struct format-arg-store; // exposition only
  149. template<class Context = format_context, class... Args>
  150. format-arg-store<Context, Args...>
  151. make_format_args(Args&... args);
  152. template<class... Args>
  153. format-arg-store<wformat_context, Args...>
  154. make_wformat_args(Args&... args);
  155. // [format.error], class format_error
  156. class format_error;
  157. }
  158. */
  159. #include <__config>
  160. #include <__format/buffer.h>
  161. #include <__format/concepts.h>
  162. #include <__format/container_adaptor.h>
  163. #include <__format/enable_insertable.h>
  164. #include <__format/format_arg.h>
  165. #include <__format/format_arg_store.h>
  166. #include <__format/format_args.h>
  167. #include <__format/format_context.h>
  168. #include <__format/format_error.h>
  169. #include <__format/format_functions.h>
  170. #include <__format/format_parse_context.h>
  171. #include <__format/format_string.h>
  172. #include <__format/format_to_n_result.h>
  173. #include <__format/formatter.h>
  174. #include <__format/formatter_bool.h>
  175. #include <__format/formatter_char.h>
  176. #include <__format/formatter_floating_point.h>
  177. #include <__format/formatter_integer.h>
  178. #include <__format/formatter_pointer.h>
  179. #include <__format/formatter_string.h>
  180. #include <__format/formatter_tuple.h>
  181. #include <__format/parser_std_format_spec.h>
  182. #include <__format/range_default_formatter.h>
  183. #include <__format/range_formatter.h>
  184. #include <__format/unicode.h>
  185. #include <__fwd/format.h>
  186. #include <version>
  187. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  188. # pragma GCC system_header
  189. #endif
  190. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
  191. # include <locale>
  192. # include <queue>
  193. # include <stack>
  194. #endif
  195. #endif // _LIBCPP_FORMAT