RavEngine
Loading...
Searching...
No Matches
compile.h
1// Formatting library for C++ - experimental format string compilation
2//
3// Copyright (c) 2012 - present, Victor Zverovich and fmt contributors
4// All rights reserved.
5//
6// For the license information refer to format.h.
7
8#ifndef FMT_COMPILE_H_
9#define FMT_COMPILE_H_
10
11#ifndef FMT_IMPORT_STD
12# include <iterator> // std::back_inserter
13#endif
14
15#include "format.h"
16
17FMT_BEGIN_NAMESPACE
18namespace detail {
19
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);
24}
25
26// A compile-time string which is compiled into fast formatting code.
28
29template <typename S>
30struct is_compiled_string : std::is_base_of<compiled_string, S> {};
31
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)
48#else
49# define FMT_COMPILE(s) FMT_STRING(s)
50#endif
51
52#if FMT_USE_NONTYPE_TEMPLATE_ARGS
53template <typename Char, size_t N,
54 fmt::detail_exported::fixed_string<Char, N> Str>
55struct udl_compiled_string : compiled_string {
56 using char_type = Char;
57 explicit constexpr operator basic_string_view<char_type>() const {
58 return {Str.data, N - 1};
59 }
60};
61#endif
62
63template <typename T, typename... Tail>
64auto first(const T& value, const Tail&...) -> const T& {
65 return value;
66}
67
68#if defined(__cpp_if_constexpr) && defined(__cpp_return_type_deduction)
69template <typename... Args> struct type_list {};
70
71// Returns a reference to the argument at index N from [first, rest...].
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");
76 if constexpr (N == 0)
77 return first;
78 else
79 return detail::get<N - 1>(rest...);
80}
81
82template <typename Char, typename... Args>
83constexpr int get_arg_index_by_name(basic_string_view<Char> name,
84 type_list<Args...>) {
85 return get_arg_index_by_name<Args...>(name);
86}
87
88template <int N, typename> struct get_type_impl;
89
90template <int N, typename... Args> struct get_type_impl<N, type_list<Args...>> {
91 using type =
92 remove_cvref_t<decltype(detail::get<N>(std::declval<Args>()...))>;
93};
94
95template <int N, typename T>
96using get_type = typename get_type_impl<N, T>::type;
97
98template <typename T> struct is_compiled_format : std::false_type {};
99
100template <typename Char> struct text {
102 using char_type = Char;
103
104 template <typename OutputIt, typename... Args>
105 constexpr OutputIt format(OutputIt out, const Args&...) const {
106 return write<Char>(out, data);
107 }
108};
109
110template <typename Char>
111struct is_compiled_format<text<Char>> : std::true_type {};
112
113template <typename Char>
114constexpr text<Char> make_text(basic_string_view<Char> s, size_t pos,
115 size_t size) {
116 return {{&s[pos], size}};
117}
118
119template <typename Char> struct code_unit {
120 Char value;
121 using char_type = Char;
122
123 template <typename OutputIt, typename... Args>
124 constexpr OutputIt format(OutputIt out, const Args&...) const {
125 *out++ = value;
126 return out;
127 }
128};
129
130// This ensures that the argument type is convertible to `const T&`.
131template <typename T, int N, typename... Args>
132constexpr const T& get_arg_checked(const Args&... args) {
133 const auto& arg = detail::get<N>(args...);
134 if constexpr (detail::is_named_arg<remove_cvref_t<decltype(arg)>>()) {
135 return arg.value;
136 } else {
137 return arg;
138 }
139}
140
141template <typename Char>
142struct is_compiled_format<code_unit<Char>> : std::true_type {};
143
144// A replacement field that refers to argument N.
145template <typename Char, typename T, int N> struct field {
146 using char_type = Char;
147
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>>) {
152 auto s = basic_string_view<Char>(arg);
153 return copy<Char>(s.begin(), s.end(), out);
154 }
155 return write<Char>(out, arg);
156 }
157};
158
159template <typename Char, typename T, int N>
160struct is_compiled_format<field<Char, T, N>> : std::true_type {};
161
162// A replacement field that refers to argument with name.
163template <typename Char> struct runtime_named_field {
164 using char_type = Char;
166
167 template <typename OutputIt, typename T>
168 constexpr static bool try_format_argument(
169 OutputIt& out,
170 // [[maybe_unused]] due to unused-but-set-parameter warning in GCC 7,8,9
171 [[maybe_unused]] basic_string_view<Char> arg_name, const T& arg) {
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);
175 return true;
176 }
177 }
178 return false;
179 }
180
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) || ...);
184 if (!found) {
185 FMT_THROW(format_error("argument with specified name is not found"));
186 }
187 return out;
188 }
189};
190
191template <typename Char>
192struct is_compiled_format<runtime_named_field<Char>> : std::true_type {};
193
194// A replacement field that refers to argument N and has format specifiers.
195template <typename Char, typename T, int N> struct spec_field {
196 using char_type = Char;
198
199 template <typename OutputIt, typename... Args>
200 constexpr FMT_INLINE OutputIt format(OutputIt out,
201 const Args&... args) const {
202 const auto& vargs =
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);
206 }
207};
208
209template <typename Char, typename T, int N>
210struct is_compiled_format<spec_field<Char, T, N>> : std::true_type {};
211
212template <typename L, typename R> struct concat {
213 L lhs;
214 R rhs;
215 using char_type = typename L::char_type;
216
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...);
221 }
222};
223
224template <typename L, typename R>
225struct is_compiled_format<concat<L, R>> : std::true_type {};
226
227template <typename L, typename R>
228constexpr concat<L, R> make_concat(L lhs, R rhs) {
229 return {lhs, rhs};
230}
231
232struct unknown_format {};
233
234template <typename Char>
235constexpr size_t parse_text(basic_string_view<Char> str, size_t pos) {
236 for (size_t size = str.size(); pos != size; ++pos) {
237 if (str[pos] == '{' || str[pos] == '}') break;
238 }
239 return pos;
240}
241
242template <typename Args, size_t POS, int ID, typename S>
243constexpr auto compile_format_string(S format_str);
244
245template <typename Args, size_t POS, int ID, typename T, typename S>
246constexpr auto parse_tail(T head, S format_str) {
247 if constexpr (POS !=
249 constexpr auto tail = compile_format_string<Args, POS, ID>(format_str);
250 if constexpr (std::is_same<remove_cvref_t<decltype(tail)>,
251 unknown_format>())
252 return tail;
253 else
254 return make_concat(head, tail);
255 } else {
256 return head;
257 }
258}
259
260template <typename T, typename Char> struct parse_specs_result {
262 size_t end;
263 int next_arg_id;
264};
265
266enum { manual_indexing_id = -1 };
267
268template <typename T, typename Char>
269constexpr parse_specs_result<T, Char> parse_specs(basic_string_view<Char> str,
270 size_t pos, int next_arg_id) {
271 str.remove_prefix(pos);
272 auto ctx =
273 compile_parse_context<Char>(str, max_value<int>(), nullptr, next_arg_id);
274 auto f = formatter<T, Char>();
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()};
278}
279
280template <typename Char> struct arg_id_handler {
281 arg_ref<Char> arg_id;
282
283 constexpr int on_auto() {
284 FMT_ASSERT(false, "handler cannot be used with automatic indexing");
285 return 0;
286 }
287 constexpr int on_index(int id) {
288 arg_id = arg_ref<Char>(id);
289 return 0;
290 }
291 constexpr int on_name(basic_string_view<Char> id) {
292 arg_id = arg_ref<Char>(id);
293 return 0;
294 }
295};
296
297template <typename Char> struct parse_arg_id_result {
298 arg_ref<Char> arg_id;
299 const Char* arg_id_end;
300};
301
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};
307}
308
309template <typename T, typename Enable = void> struct field_type {
310 using type = remove_cvref_t<T>;
311};
312
313template <typename T>
314struct field_type<T, enable_if_t<detail::is_named_arg<T>::value>> {
315 using type = remove_cvref_t<decltype(T::value)>;
316};
317
318template <typename T, typename Args, size_t END_POS, int ARG_INDEX, int NEXT_ID,
319 typename S>
320constexpr auto parse_replacement_field_then_tail(S format_str) {
321 using char_type = typename S::char_type;
322 constexpr auto str = basic_string_view<char_type>(format_str);
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>(),
327 format_str);
328 } else if constexpr (c != ':') {
329 FMT_THROW(format_error("expected ':'"));
330 } else {
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 '}'"));
335 return 0;
336 } else {
337 return parse_tail<Args, result.end + 1, result.next_arg_id>(
338 spec_field<char_type, typename field_type<T>::type, ARG_INDEX>{
339 result.fmt},
340 format_str);
341 }
342 }
343}
344
345// Compiles a non-empty format string and returns the compiled representation
346// or unknown_format() on unrecognized input.
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;
350 constexpr auto str = basic_string_view<char_type>(format_str);
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>(
363 format_str);
364 } else {
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) {
372 static_assert(
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>(
379 format_str);
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},
392 format_str);
393 } else if constexpr (c == ':') {
394 return unknown_format(); // no type info for specs parsing
395 }
396 }
397 }
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);
402 } else {
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),
406 format_str);
407 } else {
408 return parse_tail<Args, end, ID>(code_unit<char_type>{str[POS]},
409 format_str);
410 }
411 }
412}
413
414template <typename... Args, typename S,
416constexpr auto compile(S format_str) {
417 constexpr auto str = basic_string_view<typename S::char_type>(format_str);
418 if constexpr (str.size() == 0) {
419 return detail::make_text(str, 0, 0);
420 } else {
421 constexpr auto result =
422 detail::compile_format_string<detail::type_list<Args...>, 0, 0>(
423 format_str);
424 return result;
425 }
426}
427#endif // defined(__cpp_if_constexpr) && defined(__cpp_return_type_deduction)
428} // namespace detail
429
430FMT_BEGIN_EXPORT
431
432#if defined(__cpp_if_constexpr) && defined(__cpp_return_type_deduction)
433
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...);
441 return s;
442}
443
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...);
449}
450
451template <typename S, typename... Args,
453FMT_INLINE std::basic_string<typename S::char_type> format(const S&,
454 Args&&... args) {
455 if constexpr (std::is_same<typename S::char_type, char>::value) {
456 constexpr auto str = basic_string_view<typename S::char_type>(S());
457 if constexpr (str.size() == 2 && str[0] == '{' && str[1] == '}') {
458 const auto& first = detail::first(args...);
459 if constexpr (detail::is_named_arg<
460 remove_cvref_t<decltype(first)>>::value) {
461 return fmt::to_string(first.value);
462 } else {
463 return fmt::to_string(first);
464 }
465 }
466 }
467 constexpr auto compiled = detail::compile<Args...>(S());
468 if constexpr (std::is_same<remove_cvref_t<decltype(compiled)>,
469 detail::unknown_format>()) {
470 return fmt::format(
472 std::forward<Args>(args)...);
473 } else {
474 return fmt::format(compiled, std::forward<Args>(args)...);
475 }
476}
477
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(
485 out, static_cast<basic_string_view<typename S::char_type>>(S()),
486 std::forward<Args>(args)...);
487 } else {
488 return fmt::format_to(out, compiled, std::forward<Args>(args)...);
489 }
490}
491#endif
492
493template <typename OutputIt, typename S, typename... Args,
495auto format_to_n(OutputIt out, size_t n, const S& format_str, Args&&... args)
497 using traits = detail::fixed_buffer_traits;
499 fmt::format_to(std::back_inserter(buf), format_str,
500 std::forward<Args>(args)...);
501 return {buf.out(), buf.count()};
502}
503
504template <typename S, typename... Args,
506FMT_CONSTEXPR20 auto formatted_size(const S& format_str, const Args&... args)
507 -> size_t {
508 return fmt::format_to(detail::counting_iterator(), format_str, args...)
509 .count();
510}
511
512template <typename S, typename... Args,
514void print(std::FILE* f, const S& format_str, const Args&... args) {
515 memory_buffer buffer;
516 fmt::format_to(std::back_inserter(buffer), format_str, args...);
517 detail::print(f, {buffer.data(), buffer.size()});
518}
519
520template <typename S, typename... Args,
522void print(const S& format_str, const Args&... args) {
523 print(stdout, format_str, args...);
524}
525
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),
531 Str>();
532}
533} // namespace literals
534#endif
535
536FMT_END_EXPORT
537FMT_END_NAMESPACE
538
539#endif // FMT_COMPILE_H_
Definition format.h:838
Definition base.h:484
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
Definition compile.h:27
Definition format.h:2253
Definition base.h:945
Definition base.h:962
Definition compile.h:30
Definition base.h:1269
Definition base.h:2942
Definition base.h:1130