20template <
typename T,
typename InputIt>
21FMT_CONSTEXPR
inline auto copy(InputIt begin, InputIt end, counting_iterator it)
22 -> counting_iterator {
23 return it + (end - begin);
45#if defined(__cpp_if_constexpr) && defined(__cpp_return_type_deduction)
46# define FMT_COMPILE(s) \
47 FMT_STRING_IMPL(s, fmt::detail::compiled_string, explicit)
49# define FMT_COMPILE(s) FMT_STRING(s)
52#if FMT_USE_NONTYPE_TEMPLATE_ARGS
53template <
typename Char,
size_t N,
54 fmt::detail_exported::fixed_string<Char, N> Str>
56 using char_type = Char;
58 return {Str.
data, N - 1};
63template <
typename T,
typename... Tail>
64auto first(
const T& value,
const Tail&...) ->
const T& {
68#if defined(__cpp_if_constexpr) && defined(__cpp_return_type_deduction)
69template <
typename... Args>
struct type_list {};
72template <
int N,
typename T,
typename... Args>
73constexpr const auto& get([[maybe_unused]]
const T& first,
74 [[maybe_unused]]
const Args&... rest) {
75 static_assert(N < 1 +
sizeof...(Args),
"index is out of bounds");
79 return detail::get<N - 1>(rest...);
82template <
typename Char,
typename... Args>
85 return get_arg_index_by_name<Args...>(name);
88template <
int N,
typename>
struct get_type_impl;
90template <
int N,
typename... Args>
struct get_type_impl<N, type_list<Args...>> {
92 remove_cvref_t<decltype(detail::get<N>(std::declval<Args>()...))>;
95template <
int N,
typename T>
96using get_type =
typename get_type_impl<N, T>::type;
98template <
typename T>
struct is_compiled_format : std::false_type {};
100template <
typename Char>
struct text {
102 using char_type = Char;
104 template <
typename OutputIt,
typename... Args>
105 constexpr OutputIt format(OutputIt out,
const Args&...)
const {
106 return write<Char>(out, data);
110template <
typename Char>
111struct is_compiled_format<text<Char>> : std::true_type {};
113template <
typename Char>
116 return {{&s[pos], size}};
119template <
typename Char>
struct code_unit {
121 using char_type = Char;
123 template <
typename OutputIt,
typename... Args>
124 constexpr OutputIt format(OutputIt out,
const Args&...)
const {
131template <
typename T,
int N,
typename... Args>
132constexpr const T& get_arg_checked(
const Args&... args) {
133 const auto& arg = detail::get<N>(args...);
141template <
typename Char>
142struct is_compiled_format<code_unit<Char>> : std::true_type {};
145template <
typename Char,
typename T,
int N>
struct field {
146 using char_type = Char;
148 template <
typename OutputIt,
typename... Args>
149 constexpr OutputIt format(OutputIt out,
const Args&... args)
const {
150 const T& arg = get_arg_checked<T, N>(args...);
151 if constexpr (std::is_convertible_v<T, basic_string_view<Char>>) {
153 return copy<Char>(s.begin(), s.end(), out);
155 return write<Char>(out, arg);
159template <
typename Char,
typename T,
int N>
160struct is_compiled_format<field<Char, T, N>> : std::true_type {};
163template <
typename Char>
struct runtime_named_field {
164 using char_type = Char;
167 template <
typename OutputIt,
typename T>
168 constexpr static bool try_format_argument(
172 if constexpr (is_named_arg<typename std::remove_cv<T>::type>::value) {
173 if (arg_name == arg.name) {
174 out = write<Char>(out, arg.value);
181 template <
typename OutputIt,
typename... Args>
182 constexpr OutputIt format(OutputIt out,
const Args&... args)
const {
183 bool found = (try_format_argument(out, name, args) || ...);
185 FMT_THROW(format_error(
"argument with specified name is not found"));
191template <
typename Char>
192struct is_compiled_format<runtime_named_field<Char>> : std::true_type {};
195template <
typename Char,
typename T,
int N>
struct spec_field {
196 using char_type = Char;
199 template <
typename OutputIt,
typename... Args>
200 constexpr FMT_INLINE OutputIt format(OutputIt out,
201 const Args&... args)
const {
203 fmt::make_format_args<basic_format_context<OutputIt, Char>>(args...);
204 basic_format_context<OutputIt, Char> ctx(out, vargs);
205 return fmt.format(get_arg_checked<T, N>(args...), ctx);
209template <
typename Char,
typename T,
int N>
210struct is_compiled_format<spec_field<Char, T, N>> : std::true_type {};
212template <
typename L,
typename R>
struct concat {
215 using char_type =
typename L::char_type;
217 template <
typename OutputIt,
typename... Args>
218 constexpr OutputIt format(OutputIt out,
const Args&... args)
const {
219 out = lhs.format(out, args...);
220 return rhs.format(out, args...);
224template <
typename L,
typename R>
225struct is_compiled_format<concat<L, R>> : std::true_type {};
227template <
typename L,
typename R>
228constexpr concat<L, R> make_concat(L lhs, R rhs) {
232struct unknown_format {};
234template <
typename Char>
236 for (
size_t size = str.
size(); pos != size; ++pos) {
237 if (str[pos] ==
'{' || str[pos] ==
'}')
break;
242template <
typename Args,
size_t POS,
int ID,
typename S>
243constexpr auto compile_format_string(S format_str);
245template <
typename Args,
size_t POS,
int ID,
typename T,
typename S>
246constexpr auto parse_tail(T head, S format_str) {
249 constexpr auto tail = compile_format_string<Args, POS, ID>(format_str);
250 if constexpr (std::is_same<remove_cvref_t<
decltype(tail)>,
254 return make_concat(head, tail);
260template <
typename T,
typename Char>
struct parse_specs_result {
266enum { manual_indexing_id = -1 };
268template <
typename T,
typename Char>
270 size_t pos,
int next_arg_id) {
271 str.remove_prefix(pos);
273 compile_parse_context<Char>(str, max_value<int>(),
nullptr, next_arg_id);
275 auto end = f.parse(ctx);
276 return {f, pos + fmt::detail::to_unsigned(end - str.
data()),
277 next_arg_id == 0 ? manual_indexing_id : ctx.next_arg_id()};
280template <
typename Char>
struct arg_id_handler {
281 arg_ref<Char> arg_id;
283 constexpr int on_auto() {
284 FMT_ASSERT(
false,
"handler cannot be used with automatic indexing");
287 constexpr int on_index(
int id) {
288 arg_id = arg_ref<Char>(
id);
292 arg_id = arg_ref<Char>(
id);
297template <
typename Char>
struct parse_arg_id_result {
298 arg_ref<Char> arg_id;
299 const Char* arg_id_end;
302template <
int ID,
typename Char>
303constexpr auto parse_arg_id(
const Char* begin,
const Char* end) {
304 auto handler = arg_id_handler<Char>{arg_ref<Char>{}};
305 auto arg_id_end = parse_arg_id(begin, end, handler);
306 return parse_arg_id_result<Char>{handler.arg_id, arg_id_end};
309template <
typename T,
typename Enable =
void>
struct field_type {
310 using type = remove_cvref_t<T>;
314struct field_type<T, enable_if_t<detail::is_named_arg<T>::value>> {
315 using type = remove_cvref_t<
decltype(T::value)>;
318template <
typename T,
typename Args,
size_t END_POS,
int ARG_INDEX,
int NEXT_ID,
320constexpr auto parse_replacement_field_then_tail(S format_str) {
321 using char_type =
typename S::char_type;
323 constexpr char_type c = END_POS != str.
size() ? str[END_POS] : char_type();
324 if constexpr (c ==
'}') {
325 return parse_tail<Args, END_POS + 1, NEXT_ID>(
326 field<char_type,
typename field_type<T>::type, ARG_INDEX>(),
328 }
else if constexpr (c !=
':') {
329 FMT_THROW(format_error(
"expected ':'"));
331 constexpr auto result = parse_specs<typename field_type<T>::type>(
332 str, END_POS + 1, NEXT_ID == manual_indexing_id ? 0 : NEXT_ID);
333 if constexpr (result.end >= str.size() || str[result.end] !=
'}') {
334 FMT_THROW(format_error(
"expected '}'"));
337 return parse_tail<Args, result.end + 1, result.next_arg_id>(
338 spec_field<char_type, typename field_type<T>::type, ARG_INDEX>{
347template <
typename Args,
size_t POS,
int ID,
typename S>
348constexpr auto compile_format_string(S format_str) {
349 using char_type =
typename S::char_type;
351 if constexpr (str[POS] ==
'{') {
352 if constexpr (POS + 1 == str.size())
353 FMT_THROW(format_error(
"unmatched '{' in format string"));
354 if constexpr (str[POS + 1] ==
'{') {
355 return parse_tail<Args, POS + 2, ID>(make_text(str, POS, 1), format_str);
356 }
else if constexpr (str[POS + 1] ==
'}' || str[POS + 1] ==
':') {
357 static_assert(ID != manual_indexing_id,
358 "cannot switch from manual to automatic argument indexing");
359 constexpr auto next_id =
360 ID != manual_indexing_id ? ID + 1 : manual_indexing_id;
361 return parse_replacement_field_then_tail<get_type<ID, Args>, Args,
362 POS + 1, ID, next_id>(
365 constexpr auto arg_id_result =
366 parse_arg_id<ID>(str.data() + POS + 1, str.data() + str.size());
367 constexpr auto arg_id_end_pos = arg_id_result.arg_id_end - str.data();
368 constexpr char_type c =
369 arg_id_end_pos != str.size() ? str[arg_id_end_pos] : char_type();
370 static_assert(c ==
'}' || c ==
':',
"missing '}' in format string");
371 if constexpr (arg_id_result.arg_id.kind == arg_id_kind::index) {
373 ID == manual_indexing_id || ID == 0,
374 "cannot switch from automatic to manual argument indexing");
375 constexpr auto arg_index = arg_id_result.arg_id.val.index;
376 return parse_replacement_field_then_tail<get_type<arg_index, Args>,
377 Args, arg_id_end_pos,
378 arg_index, manual_indexing_id>(
380 }
else if constexpr (arg_id_result.arg_id.kind == arg_id_kind::name) {
381 constexpr auto arg_index =
382 get_arg_index_by_name(arg_id_result.arg_id.val.name, Args{});
383 if constexpr (arg_index >= 0) {
384 constexpr auto next_id =
385 ID != manual_indexing_id ? ID + 1 : manual_indexing_id;
386 return parse_replacement_field_then_tail<
387 decltype(get_type<arg_index, Args>::value), Args, arg_id_end_pos,
388 arg_index, next_id>(format_str);
389 }
else if constexpr (c ==
'}') {
390 return parse_tail<Args, arg_id_end_pos + 1, ID>(
391 runtime_named_field<char_type>{arg_id_result.arg_id.val.name},
393 }
else if constexpr (c ==
':') {
394 return unknown_format();
398 }
else if constexpr (str[POS] ==
'}') {
399 if constexpr (POS + 1 == str.size())
400 FMT_THROW(format_error(
"unmatched '}' in format string"));
401 return parse_tail<Args, POS + 2, ID>(make_text(str, POS, 1), format_str);
403 constexpr auto end = parse_text(str, POS + 1);
404 if constexpr (end - POS > 1) {
405 return parse_tail<Args, end, ID>(make_text(str, POS, end - POS),
408 return parse_tail<Args, end, ID>(code_unit<char_type>{str[POS]},
414template <
typename... Args,
typename S,
416constexpr auto compile(S format_str) {
418 if constexpr (str.size() == 0) {
419 return detail::make_text(str, 0, 0);
421 constexpr auto result =
422 detail::compile_format_string<detail::type_list<Args...>, 0, 0>(
432#if defined(__cpp_if_constexpr) && defined(__cpp_return_type_deduction)
434template <
typename CompiledFormat,
typename... Args,
435 typename Char =
typename CompiledFormat::char_type,
436 FMT_ENABLE_IF(detail::is_compiled_format<CompiledFormat>::value)>
437FMT_INLINE std::basic_string<Char> format(
const CompiledFormat& cf,
438 const Args&... args) {
439 auto s = std::basic_string<Char>();
440 cf.format(std::back_inserter(s), args...);
444template <
typename OutputIt,
typename CompiledFormat,
typename... Args,
445 FMT_ENABLE_IF(detail::is_compiled_format<CompiledFormat>::value)>
446constexpr FMT_INLINE OutputIt format_to(OutputIt out,
const CompiledFormat& cf,
447 const Args&... args) {
448 return cf.format(out, args...);
451template <
typename S,
typename... Args,
453FMT_INLINE std::basic_string<typename S::char_type> format(
const S&,
455 if constexpr (std::is_same<typename S::char_type, char>::value) {
457 if constexpr (str.size() == 2 && str[0] ==
'{' && str[1] ==
'}') {
458 const auto& first = detail::first(args...);
460 remove_cvref_t<
decltype(first)>>::value) {
461 return fmt::to_string(first.value);
463 return fmt::to_string(first);
467 constexpr auto compiled = detail::compile<Args...>(S());
468 if constexpr (std::is_same<remove_cvref_t<
decltype(compiled)>,
469 detail::unknown_format>()) {
472 std::forward<Args>(args)...);
474 return fmt::format(compiled, std::forward<Args>(args)...);
478template <
typename OutputIt,
typename S,
typename... Args,
480FMT_CONSTEXPR OutputIt format_to(OutputIt out,
const S&, Args&&... args) {
481 constexpr auto compiled = detail::compile<Args...>(S());
482 if constexpr (std::is_same<remove_cvref_t<
decltype(compiled)>,
483 detail::unknown_format>()) {
484 return fmt::format_to(
486 std::forward<Args>(args)...);
488 return fmt::format_to(out, compiled, std::forward<Args>(args)...);
493template <
typename OutputIt,
typename S,
typename... Args,
495auto format_to_n(OutputIt out,
size_t n,
const S& format_str, Args&&... args)
499 fmt::format_to(std::back_inserter(buf), format_str,
500 std::forward<Args>(args)...);
501 return {buf.out(), buf.count()};
504template <
typename S,
typename... Args,
506FMT_CONSTEXPR20
auto formatted_size(
const S& format_str,
const Args&... args)
512template <
typename S,
typename... Args,
514void print(std::FILE* f,
const S& format_str,
const Args&... args) {
516 fmt::format_to(std::back_inserter(buffer), format_str, args...);
517 detail::print(f, {buffer.
data(), buffer.
size()});
520template <
typename S,
typename... Args,
522void print(
const S& format_str,
const Args&... args) {
523 print(stdout, format_str, args...);
526#if FMT_USE_NONTYPE_TEMPLATE_ARGS
527inline namespace literals {
528template <detail_exported::fixed_
string Str>
constexpr auto operator""_cf() {
529 using char_t = remove_cvref_t<
decltype(Str.data[0])>;
530 return detail::udl_compiled_string<char_t,
sizeof(Str.data) /
sizeof(char_t),
constexpr auto size() const noexcept -> size_t
Definition base.h:526
constexpr auto data() const noexcept -> const Char *
Definition base.h:523
constexpr auto size() const noexcept -> size_t
Definition base.h:880
FMT_CONSTEXPR auto data() noexcept -> T *
Definition base.h:886