37#if defined(__cpp_if_constexpr) && defined(__cpp_return_type_deduction)
38# define FMT_COMPILE(s) FMT_STRING_IMPL(s, fmt::compiled_string)
40# define FMT_COMPILE(s) FMT_STRING(s)
55#if FMT_USE_NONTYPE_TEMPLATE_ARGS
57template <detail::fixed_
string Str>
constexpr auto operator""_cf() {
67template <
typename T,
typename... Tail>
68constexpr auto first(
const T&
value,
const Tail&...) ->
const T& {
72#if defined(__cpp_if_constexpr) && defined(__cpp_return_type_deduction)
73template <
typename... T>
struct type_list {};
76template <
int N,
typename T,
typename... Args>
77constexpr auto get([[maybe_unused]]
const T&
first,
78 [[maybe_unused]]
const Args&... rest) ->
const auto& {
79 static_assert(N < 1 +
sizeof...(Args),
"index is out of bounds");
83 return detail::get<N - 1>(rest...);
86# if FMT_USE_NONTYPE_TEMPLATE_ARGS
87template <
int N,
typename T,
typename... Args,
typename Char>
88constexpr auto get_arg_index_by_name(basic_string_view<Char>
name) ->
int {
90 if (
name == T::name)
return N;
92 if constexpr (
sizeof...(Args) > 0)
93 return get_arg_index_by_name<N + 1, Args...>(
name);
99template <
typename... Args,
typename Char>
101# if FMT_USE_NONTYPE_TEMPLATE_ARGS
102 if constexpr (
sizeof...(Args) > 0)
103 return get_arg_index_by_name<0, Args...>(
name);
109template <
typename Char,
typename... Args>
110constexpr auto get_arg_index_by_name(basic_string_view<Char>
name,
111 type_list<Args...>) ->
int {
112 return get_arg_index_by_name<Args...>(
name);
115template <
int N,
typename>
struct get_type_impl;
117template <
int N,
typename... Args>
struct get_type_impl<N, type_list<Args...>> {
122template <
int N,
typename T>
123using get_type =
typename get_type_impl<N, T>::type;
125template <
typename T>
struct is_compiled_format : std::false_type {};
127template <
typename Char>
struct text {
128 basic_string_view<Char> data;
131 template <
typename OutputIt,
typename... T>
132 constexpr auto format(OutputIt out,
const T&...)
const -> OutputIt {
133 return write<Char>(out, data);
137template <
typename Char>
138struct is_compiled_format<text<Char>> : std::true_type {};
140template <
typename Char>
141constexpr auto make_text(basic_string_view<Char> s,
size_t pos,
size_t size)
143 return {{&s[pos], size}};
146template <
typename Char>
struct code_unit {
150 template <
typename OutputIt,
typename... T>
151 constexpr auto format(OutputIt out,
const T&...)
const -> OutputIt {
158template <
typename T,
int N,
typename... Args>
159constexpr auto get_arg_checked(
const Args&... args) ->
const T& {
160 const auto&
arg = detail::get<N>(args...);
168template <
typename Char>
169struct is_compiled_format<code_unit<Char>> : std::true_type {};
172template <
typename Char,
typename V,
int N>
struct field {
175 template <
typename OutputIt,
typename... T>
176 constexpr auto format(OutputIt out,
const T&... args)
const -> OutputIt {
177 const V&
arg = get_arg_checked<V, N>(args...);
178 if constexpr (std::is_convertible<V, basic_string_view<Char>>::value) {
179 auto s = basic_string_view<Char>(
arg);
180 return copy<Char>(s.begin(), s.end(), out);
182 return write<Char>(out,
arg);
187template <
typename Char,
typename T,
int N>
188struct is_compiled_format<field<Char, T, N>> : std::true_type {};
191template <
typename Char>
struct runtime_named_field {
193 basic_string_view<Char>
name;
195 template <
typename OutputIt,
typename T>
196 constexpr static auto try_format_argument(
199 [[maybe_unused]] basic_string_view<Char> arg_name,
const T&
arg) ->
bool {
200 if constexpr (is_named_arg<typename std::remove_cv<T>::type>::value) {
201 if (arg_name ==
arg.name) {
202 out = write<Char>(out,
arg.value);
209 template <
typename OutputIt,
typename... T>
210 constexpr auto format(OutputIt out,
const T&... args)
const -> OutputIt {
211 bool found = (try_format_argument(out,
name, args) || ...);
213 FMT_THROW(format_error(
"argument with specified name is not found"));
219template <
typename Char>
220struct is_compiled_format<runtime_named_field<Char>> : std::true_type {};
223template <
typename Char,
typename V,
int N>
struct spec_field {
225 formatter<V, Char> fmt;
227 template <
typename OutputIt,
typename... T>
231 fmt::make_format_args<basic_format_context<OutputIt, Char>>(args...);
233 return fmt.format(get_arg_checked<V, N>(args...), ctx);
237template <
typename Char,
typename T,
int N>
238struct is_compiled_format<spec_field<Char, T, N>> : std::true_type {};
240template <
typename L,
typename R>
struct concat {
245 template <
typename OutputIt,
typename... T>
246 constexpr auto format(OutputIt out,
const T&... args)
const -> OutputIt {
247 out = lhs.format(out, args...);
248 return rhs.format(out, args...);
252template <
typename L,
typename R>
253struct is_compiled_format<concat<L, R>> : std::true_type {};
255template <
typename L,
typename R>
256constexpr auto make_concat(L lhs, R rhs) -> concat<L, R> {
260struct unknown_format {};
262template <
typename Char>
263constexpr auto parse_text(basic_string_view<Char> str,
size_t pos) ->
size_t {
264 for (
size_t size = str.size(); pos != size; ++pos) {
265 if (str[pos] ==
'{' || str[pos] ==
'}')
break;
270template <
typename Args,
size_t POS,
int ID,
typename S>
271constexpr auto compile_format_string(
S fmt);
273template <
typename Args,
size_t POS,
int ID,
typename T,
typename S>
274constexpr auto parse_tail(T head,
S fmt) {
275 if constexpr (POS != basic_string_view<typename S::char_type>(fmt).size()) {
276 constexpr auto tail = compile_format_string<Args, POS, ID>(fmt);
281 return make_concat(head, tail);
287template <
typename T,
typename Char>
struct parse_specs_result {
288 formatter<T, Char> fmt;
293enum { manual_indexing_id = -1 };
295template <
typename T,
typename Char>
296constexpr auto parse_specs(basic_string_view<Char> str,
size_t pos,
297 int next_arg_id) -> parse_specs_result<T, Char> {
298 str.remove_prefix(pos);
301 auto f = formatter<T, Char>();
302 auto end = f.parse(ctx);
303 return {f, pos + fmt::detail::to_unsigned(end - str.data()),
304 next_arg_id == 0 ? manual_indexing_id : ctx.next_arg_id()};
307template <
typename Char>
struct arg_id_handler {
309 arg_ref<Char> arg_id;
311 constexpr auto on_auto() ->
int {
312 FMT_ASSERT(
false,
"handler cannot be used with automatic indexing");
315 constexpr auto on_index(
int id) ->
int {
317 arg_id = arg_ref<Char>(
id);
320 constexpr auto on_name(basic_string_view<Char>
id) ->
int {
322 arg_id = arg_ref<Char>(
id);
327template <
typename Char>
struct parse_arg_id_result {
329 arg_ref<Char> arg_id;
330 const Char* arg_id_end;
333template <
int ID,
typename Char>
334constexpr auto parse_arg_id(
const Char* begin,
const Char* end) {
337 return parse_arg_id_result<Char>{handler.kind, handler.arg_id, arg_id_end};
340template <
typename T,
typename Enable =
void>
struct field_type {
349template <
typename T,
typename Args,
size_t END_POS,
int ARG_INDEX,
int NEXT_ID,
351constexpr auto parse_replacement_field_then_tail(
S fmt) {
353 constexpr auto str = basic_string_view<char_type>(fmt);
355 if constexpr (c ==
'}') {
356 return parse_tail<Args, END_POS + 1, NEXT_ID>(
357 field<
char_type,
typename field_type<T>::type, ARG_INDEX>(), fmt);
358 }
else if constexpr (c !=
':') {
361 constexpr auto result = parse_specs<typename field_type<T>::type>(
362 str, END_POS + 1, NEXT_ID == manual_indexing_id ? 0 : NEXT_ID);
363 if constexpr (result.end >= str.size() || str[result.end] !=
'}') {
367 return parse_tail<Args, result.end + 1, result.next_arg_id>(
368 spec_field<char_type, typename field_type<T>::type, ARG_INDEX>{
377template <
typename Args,
size_t POS,
int ID,
typename S>
378constexpr auto compile_format_string(
S fmt) {
380 constexpr auto str = basic_string_view<char_type>(fmt);
381 if constexpr (str[POS] ==
'{') {
382 if constexpr (POS + 1 == str.size())
383 FMT_THROW(format_error(
"unmatched '{' in format string"));
384 if constexpr (str[POS + 1] ==
'{') {
385 return parse_tail<Args, POS + 2, ID>(make_text(str, POS, 1), fmt);
386 }
else if constexpr (str[POS + 1] ==
'}' || str[POS + 1] ==
':') {
387 static_assert(ID != manual_indexing_id,
388 "cannot switch from manual to automatic argument indexing");
389 constexpr auto next_id =
390 ID != manual_indexing_id ? ID + 1 : manual_indexing_id;
391 return parse_replacement_field_then_tail<get_type<ID, Args>, Args,
392 POS + 1, ID, next_id>(fmt);
394 constexpr auto arg_id_result =
396 constexpr auto arg_id_end_pos = arg_id_result.arg_id_end - str.data();
398 arg_id_end_pos != str.size() ? str[arg_id_end_pos] :
char_type();
399 static_assert(c ==
'}' || c ==
':',
"missing '}' in format string");
402 ID == manual_indexing_id || ID == 0,
403 "cannot switch from automatic to manual argument indexing");
404 constexpr auto arg_index = arg_id_result.arg_id.index;
405 return parse_replacement_field_then_tail<get_type<arg_index, Args>,
406 Args, arg_id_end_pos,
407 arg_index, manual_indexing_id>(
410 constexpr auto arg_index =
411 get_arg_index_by_name(arg_id_result.arg_id.name, Args{});
412 if constexpr (arg_index >= 0) {
413 constexpr auto next_id =
414 ID != manual_indexing_id ? ID + 1 : manual_indexing_id;
415 return parse_replacement_field_then_tail<
416 decltype(get_type<arg_index, Args>::value), Args, arg_id_end_pos,
417 arg_index, next_id>(fmt);
418 }
else if constexpr (c ==
'}') {
419 return parse_tail<Args, arg_id_end_pos + 1, ID>(
420 runtime_named_field<char_type>{arg_id_result.arg_id.name}, fmt);
421 }
else if constexpr (c ==
':') {
422 return unknown_format();
426 }
else if constexpr (str[POS] ==
'}') {
427 if constexpr (POS + 1 == str.size())
428 FMT_THROW(format_error(
"unmatched '}' in format string"));
429 return parse_tail<Args, POS + 2, ID>(make_text(str, POS, 1), fmt);
431 constexpr auto end = parse_text(str, POS + 1);
432 if constexpr (end - POS > 1) {
433 return parse_tail<Args, end, ID>(make_text(str, POS, end - POS), fmt);
435 return parse_tail<Args, end, ID>(code_unit<char_type>{str[POS]}, fmt);
440template <
typename... Args,
typename S,
442constexpr auto compile(
S fmt) {
443 constexpr auto str = basic_string_view<typename S::char_type>(fmt);
444 if constexpr (str.size() == 0) {
445 return detail::make_text(str, 0, 0);
447 constexpr auto result =
448 detail::compile_format_string<detail::type_list<Args...>, 0, 0>(fmt);
457#if defined(__cpp_if_constexpr) && defined(__cpp_return_type_deduction)
459template <
typename CompiledFormat,
typename... T,
460 typename Char =
typename CompiledFormat::char_type,
461 FMT_ENABLE_IF(detail::is_compiled_format<CompiledFormat>::value)>
464 -> std::basic_string<Char> {
465 auto s = std::basic_string<Char>();
466 cf.format(std::back_inserter(s), args...);
470template <
typename OutputIt,
typename CompiledFormat,
typename... T,
471 FMT_ENABLE_IF(detail::is_compiled_format<CompiledFormat>::value)>
473 const T&... args) -> OutputIt {
474 return cf.format(out, args...);
477template <
typename S,
typename... T,
480 -> std::basic_string<typename S::char_type> {
481 if constexpr (std::is_same<typename S::char_type, char>::value) {
483 if constexpr (str.size() == 2 && str[0] ==
'{' && str[1] ==
'}') {
487 return fmt::to_string(
first.value);
489 return fmt::to_string(first);
493 constexpr auto compiled = detail::compile<T...>(
S());
495 detail::unknown_format>()) {
498 std::forward<T>(args)...);
500 return fmt::format(compiled, std::forward<T>(args)...);
504template <
typename OutputIt,
typename S,
typename... T,
507 constexpr auto compiled = detail::compile<T...>(
S());
509 detail::unknown_format>()) {
510 return fmt::format_to(
512 std::forward<T>(args)...);
514 return fmt::format_to(out, compiled, std::forward<T>(args)...);
519template <
typename OutputIt,
typename S,
typename... T,
525 fmt::format_to(std::back_inserter(buf), fmt, std::forward<T>(args)...);
526 return {buf.out(), buf.count()};
529template <
typename S,
typename... T,
533 fmt::format_to(
appender(buf), fmt, std::forward<T>(args)...);
537template <
typename S,
typename... T,
539void print(std::FILE* f,
const S& fmt, T&&... args) {
541 fmt::format_to(
appender(buf), fmt, std::forward<T>(args)...);
545template <
typename S,
typename... T,
548 print(stdout, fmt, std::forward<T>(args)...);
556 template <
typename S,
typename... T,
559 *fmt::format_to(data, fmt, std::forward<T>(args)...) =
'\0';
563 auto c_str() const -> const
char* {
return data; }
580#define FMT_STATIC_FORMAT(fmt_str, ...) \
581 fmt::static_format_result< \
582 fmt::formatted_size(FMT_COMPILE(fmt_str), __VA_ARGS__) + 1>( \
583 FMT_COMPILE(fmt_str), __VA_ARGS__)
#define S(label, offset, message)
Definition Errors.hpp:113
constexpr T & get(wpi::util::array< T, N > &arr) noexcept
Definition array.hpp:66
typename std::enable_if< B, T >::type enable_if_t
Definition base.h:307
#define FMT_ASSERT(condition, message)
Definition base.h:394
#define FMT_END_EXPORT
Definition base.h:267
basic_string_view< char > string_view
Definition base.h:620
auto arg(const Char *name, const T &arg) -> detail::named_arg< Char, T >
Returns a named argument to be used in a formatting function.
Definition base.h:2846
detail::format_arg_store< context, sizeof...(T), detail::count_named_args< T... >(), detail::make_descriptor< context, T... >()> vargs
Definition base.h:2832
basic_appender< char > appender
Definition base.h:623
typename std::remove_cv< remove_reference_t< T > >::type remove_cvref_t
Definition base.h:316
#define FMT_CONSTEXPR
Definition base.h:113
arg_id_kind
Definition base.h:690
@ none
Definition base.h:690
@ index
Definition base.h:690
@ name
Definition base.h:690
#define FMT_BEGIN_NAMESPACE
Definition base.h:256
conditional_t< std::is_same< OutputIt, appender >::value, context, generic_context< OutputIt, Char > > basic_format_context
Definition base.h:636
#define FMT_ENABLE_IF(...)
Definition base.h:344
#define FMT_BEGIN_EXPORT
Definition base.h:266
#define FMT_INLINE
Definition base.h:250
#define FMT_CONSTEXPR20
Definition base.h:143
#define FMT_END_NAMESPACE
Definition base.h:259
An implementation of std::basic_string_view for pre-C++17.
Definition base.h:522
#define FMT_COMPILE(s)
Converts a string literal s into a format string that will be parsed at compile time and converted in...
Definition compile.h:40
void print(std::FILE *f, const S &fmt, T &&... args)
Definition compile.h:539
FMT_CONSTEXPR20 auto formatted_size(const S &fmt, T &&... args) -> size_t
Definition compile.h:531
FMT_BEGIN_EXPORT auto format_to_n(OutputIt out, size_t n, const S &fmt, T &&... args) -> format_to_n_result< OutputIt >
Definition compile.h:521
Converts a string literal into a format string that will be parsed at compile time and converted into...
Definition printf.h:50
FMT_API void print(FILE *, string_view)
constexpr auto max_value() -> T
Definition format.h:427
constexpr Char string_literal< Char, C... >::value[sizeof...(C)]
Definition format.h:275
constexpr auto first(const T &value, const Tail &...) -> const T &
Definition compile.h:68
FMT_CONSTEXPR auto parse_arg_id(const Char *begin, const Char *end, Handler &&handler) -> const Char *
Definition base.h:1358
type
Definition base.h:985
@ char_type
Definition base.h:995