16template <
typename Char,
typename InputIt>
19 return it + (end - begin);
41#if defined(__cpp_if_constexpr) && defined(__cpp_return_type_deduction)
42# define FMT_COMPILE(s) \
43 FMT_STRING_IMPL(s, fmt::detail::compiled_string, explicit)
45# define FMT_COMPILE(s) FMT_STRING(s)
48#if FMT_USE_NONTYPE_TEMPLATE_ARGS
49template <
typename Char,
size_t N,
50 fmt::detail_exported::fixed_string<Char, N> Str>
51struct udl_compiled_string : compiled_string {
52 using char_type = Char;
54 return {Str.
data, N - 1};
59template <
typename T,
typename... Tail>
64#if defined(__cpp_if_constexpr) && defined(__cpp_return_type_deduction)
65template <
typename... Args>
struct type_list {};
68template <
int N,
typename T,
typename... Args>
69constexpr const auto&
get([[maybe_unused]]
const T&
first,
70 [[maybe_unused]]
const Args&... rest) {
71 static_assert(N < 1 +
sizeof...(Args),
"index is out of bounds");
78template <
typename Char,
typename... Args>
84template <
int N,
typename>
struct get_type_impl;
86template <
int N,
typename... Args>
struct get_type_impl<N, type_list<Args...>> {
91template <
int N,
typename T>
94template <
typename T>
struct is_compiled_format : std::false_type {};
96template <
typename Char>
struct text {
100 template <
typename OutputIt,
typename... Args>
101 constexpr OutputIt
format(OutputIt out,
const Args&...)
const {
102 return write<Char>(out, data);
106template <
typename Char>
107struct is_compiled_format<text<Char>> : std::true_type {};
109template <
typename Char>
112 return {{&s[pos], size}};
115template <
typename Char>
struct code_unit {
119 template <
typename OutputIt,
typename... Args>
120 constexpr OutputIt
format(OutputIt out,
const Args&...)
const {
127template <
typename T,
int N,
typename... Args>
128constexpr const T& get_arg_checked(
const Args&... args) {
129 const auto&
arg = detail::get<N>(args...);
137template <
typename Char>
138struct is_compiled_format<code_unit<Char>> : std::true_type {};
141template <
typename Char,
typename T,
int N>
struct field {
144 template <
typename OutputIt,
typename... Args>
145 constexpr OutputIt
format(OutputIt out,
const Args&... args)
const {
146 const T&
arg = get_arg_checked<T, N>(args...);
147 if constexpr (std::is_convertible_v<T, basic_string_view<Char>>) {
149 return copy_str<Char>(s.begin(), s.end(), out);
151 return write<Char>(out,
arg);
155template <
typename Char,
typename T,
int N>
156struct is_compiled_format<field<Char, T, N>> : std::true_type {};
159template <
typename Char>
struct runtime_named_field {
163 template <
typename OutputIt,
typename T>
164 constexpr static bool try_format_argument(
169 if (arg_name ==
arg.name) {
170 out = write<Char>(out,
arg.value);
177 template <
typename OutputIt,
typename... Args>
178 constexpr OutputIt
format(OutputIt out,
const Args&... args)
const {
179 bool found = (try_format_argument(out, name, args) || ...);
181 FMT_THROW(format_error(
"argument with specified name is not found"));
187template <
typename Char>
188struct is_compiled_format<runtime_named_field<Char>> : std::true_type {};
191template <
typename Char,
typename T,
int N>
struct spec_field {
195 template <
typename OutputIt,
typename... Args>
197 const Args&... args)
const {
199 fmt::make_format_args<basic_format_context<OutputIt, Char>>(args...);
201 return fmt.format(get_arg_checked<T, N>(args...), ctx);
205template <
typename Char,
typename T,
int N>
206struct is_compiled_format<spec_field<Char, T, N>> : std::true_type {};
208template <
typename L,
typename R>
struct concat {
213 template <
typename OutputIt,
typename... Args>
214 constexpr OutputIt
format(OutputIt out,
const Args&... args)
const {
215 out = lhs.format(out, args...);
216 return rhs.format(out, args...);
220template <
typename L,
typename R>
221struct is_compiled_format<
concat<
L,
R>> : std::true_type {};
223template <
typename L,
typename R>
224constexpr concat<L, R> make_concat(
L lhs,
R rhs) {
228struct unknown_format {};
230template <
typename Char>
232 for (
size_t size = str.
size(); pos != size; ++pos) {
233 if (str[pos] ==
'{' || str[pos] ==
'}')
break;
238template <
typename Args,
size_t POS,
int ID,
typename S>
239constexpr auto compile_format_string(
S format_str);
241template <
typename Args,
size_t POS,
int ID,
typename T,
typename S>
242constexpr auto parse_tail(T head,
S format_str) {
245 constexpr auto tail = compile_format_string<Args, POS, ID>(format_str);
250 return make_concat(head, tail);
256template <
typename T,
typename Char>
struct parse_specs_result {
262enum { manual_indexing_id = -1 };
264template <
typename T,
typename Char>
266 size_t pos,
int next_arg_id) {
269 compile_parse_context<Char>(str, max_value<int>(),
nullptr, next_arg_id);
271 auto end = f.parse(ctx);
273 next_arg_id == 0 ? manual_indexing_id : ctx.next_arg_id()};
276template <
typename Char>
struct arg_id_handler {
277 arg_ref<Char> arg_id;
279 constexpr int on_auto() {
280 FMT_ASSERT(
false,
"handler cannot be used with automatic indexing");
283 constexpr int on_index(
int id) {
284 arg_id = arg_ref<Char>(
id);
288 arg_id = arg_ref<Char>(
id);
293template <
typename Char>
struct parse_arg_id_result {
294 arg_ref<Char> arg_id;
295 const Char* arg_id_end;
298template <
int ID,
typename Char>
299constexpr auto parse_arg_id(
const Char* begin,
const Char* end) {
300 auto handler = arg_id_handler<Char>{arg_ref<Char>{}};
302 return parse_arg_id_result<Char>{handler.arg_id, arg_id_end};
305template <
typename T,
typename Enable =
void>
struct field_type {
314template <
typename T,
typename Args,
size_t END_POS,
int ARG_INDEX,
int NEXT_ID,
316constexpr auto parse_replacement_field_then_tail(
S format_str) {
320 if constexpr (
c ==
'}') {
321 return parse_tail<Args, END_POS + 1, NEXT_ID>(
324 }
else if constexpr (
c !=
':') {
328 str, END_POS + 1, NEXT_ID == manual_indexing_id ? 0 : NEXT_ID);
329 if constexpr (result.end >= str.size() || str[result.end] !=
'}') {
333 return parse_tail<Args, result.end + 1, result.next_arg_id>(
343template <
typename Args,
size_t POS,
int ID,
typename S>
344constexpr auto compile_format_string(
S format_str) {
347 if constexpr (str[POS] ==
'{') {
348 if constexpr (POS + 1 == str.size())
349 FMT_THROW(format_error(
"unmatched '{' in format string"));
350 if constexpr (str[POS + 1] ==
'{') {
351 return parse_tail<Args, POS + 2, ID>(make_text(str, POS, 1), format_str);
352 }
else if constexpr (str[POS + 1] ==
'}' || str[POS + 1] ==
':') {
353 static_assert(ID != manual_indexing_id,
354 "cannot switch from manual to automatic argument indexing");
355 constexpr auto next_id =
356 ID != manual_indexing_id ? ID + 1 : manual_indexing_id;
357 return parse_replacement_field_then_tail<get_type<ID, Args>, Args,
358 POS + 1, ID, next_id>(
361 constexpr auto arg_id_result =
362 parse_arg_id<ID>(str.data() + POS + 1, str.data() + str.size());
363 constexpr auto arg_id_end_pos = arg_id_result.arg_id_end - str.data();
365 arg_id_end_pos != str.size() ? str[arg_id_end_pos] :
char_type();
366 static_assert(
c ==
'}' ||
c ==
':',
"missing '}' in format string");
369 ID == manual_indexing_id || ID == 0,
370 "cannot switch from automatic to manual argument indexing");
371 constexpr auto arg_index = arg_id_result.arg_id.val.index;
372 return parse_replacement_field_then_tail<get_type<arg_index, Args>,
373 Args, arg_id_end_pos,
374 arg_index, manual_indexing_id>(
377 constexpr auto arg_index =
379 if constexpr (arg_index >= 0) {
380 constexpr auto next_id =
381 ID != manual_indexing_id ? ID + 1 : manual_indexing_id;
382 return parse_replacement_field_then_tail<
383 decltype(get_type<arg_index, Args>::value), Args, arg_id_end_pos,
384 arg_index, next_id>(format_str);
385 }
else if constexpr (
c ==
'}') {
386 return parse_tail<Args, arg_id_end_pos + 1, ID>(
387 runtime_named_field<char_type>{arg_id_result.arg_id.val.name},
389 }
else if constexpr (
c ==
':') {
390 return unknown_format();
394 }
else if constexpr (str[POS] ==
'}') {
395 if constexpr (POS + 1 == str.size())
396 FMT_THROW(format_error(
"unmatched '}' in format string"));
397 return parse_tail<Args, POS + 2, ID>(make_text(str, POS, 1), format_str);
399 constexpr auto end = parse_text(str, POS + 1);
400 if constexpr (end - POS > 1) {
401 return parse_tail<Args, end, ID>(make_text(str, POS, end - POS),
404 return parse_tail<Args, end, ID>(code_unit<char_type>{str[POS]},
410template <
typename... Args,
typename S,
412constexpr auto compile(
S format_str) {
414 if constexpr (str.size() == 0) {
415 return detail::make_text(str, 0, 0);
417 constexpr auto result =
418 detail::compile_format_string<detail::type_list<Args...>, 0, 0>(
428#if defined(__cpp_if_constexpr) && defined(__cpp_return_type_deduction)
430template <
typename CompiledFormat,
typename... Args,
431 typename Char =
typename CompiledFormat::char_type,
432 FMT_ENABLE_IF(detail::is_compiled_format<CompiledFormat>::value)>
434 const Args&... args) {
435 auto s = std::basic_string<Char>();
436 cf.format(std::back_inserter(s), args...);
440template <
typename OutputIt,
typename CompiledFormat,
typename... Args,
441 FMT_ENABLE_IF(detail::is_compiled_format<CompiledFormat>::value)>
443 const Args&... args) {
444 return cf.format(out, args...);
447template <
typename S,
typename... Args,
451 if constexpr (std::is_same<typename S::char_type, char>::value) {
453 if constexpr (str.size() == 2 && str[0] ==
'{' && str[1] ==
'}') {
463 constexpr auto compiled = detail::compile<Args...>(
S());
465 detail::unknown_format>()) {
468 std::forward<Args>(args)...);
470 return fmt::format(compiled, std::forward<Args>(args)...);
474template <
typename OutputIt,
typename S,
typename... Args,
477 constexpr auto compiled = detail::compile<Args...>(
S());
479 detail::unknown_format>()) {
482 std::forward<Args>(args)...);
484 return fmt::format_to(out, compiled, std::forward<Args>(args)...);
489template <
typename OutputIt,
typename S,
typename... Args,
492 const S& format_str, Args&&... args) {
495 format_to(std::back_inserter(buf), format_str, std::forward<Args>(args)...);
496 return {buf.out(), buf.count()};
499template <
typename S,
typename... Args,
502 const Args&... args) {
507template <
typename S,
typename... Args,
509void print(std::FILE* f,
const S& format_str,
const Args&... args) {
515template <
typename S,
typename... Args,
517void print(
const S& format_str,
const Args&... args) {
518 print(stdout, format_str, args...);
521#if FMT_USE_NONTYPE_TEMPLATE_ARGS
523template <detail_exported::fixed_
string Str>
constexpr auto operator""_cf() {
525 return detail::udl_compiled_string<
char_t,
sizeof(Str.data) /
sizeof(
char_t),
\rst A dynamically growing memory buffer for trivially copyable/constructible types with the first SI...
Definition: format.h:918
constexpr auto size() const noexcept -> size_t
Returns the string size.
Definition: core.h:443
constexpr auto data() const noexcept -> const Char *
Returns a pointer to the string data.
Definition: core.h:440
FMT_CONSTEXPR void remove_prefix(size_t n) noexcept
Definition: core.h:452
constexpr auto size() const noexcept -> size_t
Returns the size of this buffer.
Definition: core.h:838
FMT_CONSTEXPR auto data() noexcept -> T *
Returns a pointer to the buffer data (not null-terminated).
Definition: core.h:844
Definition: format.h:2300
void print(std::FILE *f, const S &format_str, const Args &... args)
Definition: compile.h:509
FMT_CONSTEXPR20 size_t formatted_size(const S &format_str, const Args &... args)
Definition: compile.h:501
FMT_BEGIN_EXPORT format_to_n_result< OutputIt > format_to_n(OutputIt out, size_t n, const S &format_str, Args &&... args)
Definition: compile.h:491
typename std::enable_if< B, T >::type enable_if_t
Definition: core.h:256
#define FMT_ASSERT(condition, message)
Definition: core.h:336
#define FMT_END_EXPORT
Definition: core.h:185
auto arg(const Char *name, const T &arg) -> detail::named_arg< Char, T >
\rst Returns a named argument to be used in a formatting function.
Definition: core.h:1841
typename detail::char_t_impl< S >::type char_t
String's character type.
Definition: core.h:646
#define FMT_CONSTEXPR
Definition: core.h:105
#define FMT_BEGIN_NAMESPACE
Definition: core.h:174
#define FMT_ENABLE_IF(...)
Definition: core.h:286
#define FMT_BEGIN_EXPORT
Definition: core.h:184
#define FMT_INLINE
Definition: core.h:162
#define FMT_CONSTEXPR20
Definition: core.h:113
typename std::remove_cv< remove_reference_t< T > >::type remove_cvref_t
Definition: core.h:265
#define FMT_END_NAMESPACE
Definition: core.h:177
detail namespace with internal helper functions
Definition: xchar.h:20
OutStringType concat(Args &&... args)
Definition: string_concat.h:137
FMT_CONSTEXPR counting_iterator copy_str(InputIt begin, InputIt end, counting_iterator it)
Definition: compile.h:17
@ value
the parser finished reading a JSON value
FMT_FUNC void print(std::FILE *f, string_view text)
Definition: format-inl.h:1452
const T & first(const T &value, const Tail &...)
Definition: compile.h:60
auto get(const wpi::detail::iteration_proxy_value< IteratorType > &i) -> decltype(i.key())
Definition: iteration_proxy.h:193
FMT_CONSTEXPR FMT_INLINE auto parse_arg_id(const Char *begin, const Char *end, Handler &&handler) -> const Char *
Definition: core.h:2206
type
Definition: core.h:556
FMT_CONSTEXPR auto get_arg_index_by_name(basic_string_view< Char > name) -> int
Definition: core.h:2583
FMT_CONSTEXPR auto to_unsigned(Int value) -> typename std::make_unsigned< Int >::type
Definition: core.h:374
static constexpr const unit_t< compound_unit< energy::joules, inverse< temperature::kelvin >, inverse< substance::moles > > > R(8.3144598)
Gas constant.
static constexpr const velocity::meters_per_second_t c(299792458.0)
Speed of light in vacuum.
std::string to_string(const T &t)
Definition: base.h:92
cubed< length::millimeter > L
Definition: volume.h:49
constexpr const char * name(const T &)
#define S(label, offset, message)
Definition: Errors.h:119
auto format(wformat_string< T... > fmt, T &&... args) -> std::wstring
Definition: xchar.h:108
auto format_to(OutputIt out, const S &fmt, T &&... args) -> OutputIt
Definition: xchar.h:156