Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/fmt/compile.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // Formatting library for C++ - experimental format string compilation
0002 //
0003 // Copyright (c) 2012 - present, Victor Zverovich and fmt contributors
0004 // All rights reserved.
0005 //
0006 // For the license information refer to format.h.
0007 
0008 #ifndef FMT_COMPILE_H_
0009 #define FMT_COMPILE_H_
0010 
0011 #ifndef FMT_MODULE
0012 #  include <iterator>  // std::back_inserter
0013 #endif
0014 
0015 #include "format.h"
0016 
0017 FMT_BEGIN_NAMESPACE
0018 
0019 // A compile-time string which is compiled into fast formatting code.
0020 FMT_EXPORT class compiled_string {};
0021 
0022 template <typename S>
0023 struct is_compiled_string : std::is_base_of<compiled_string, S> {};
0024 
0025 namespace detail {
0026 
0027 /**
0028  * Converts a string literal `s` into a format string that will be parsed at
0029  * compile time and converted into efficient formatting code. Requires C++17
0030  * `constexpr if` compiler support.
0031  *
0032  * **Example**:
0033  *
0034  *     // Converts 42 into std::string using the most efficient method and no
0035  *     // runtime format string processing.
0036  *     std::string s = fmt::format(FMT_COMPILE("{}"), 42);
0037  */
0038 #if defined(__cpp_if_constexpr) && defined(__cpp_return_type_deduction)
0039 #  define FMT_COMPILE(s) FMT_STRING_IMPL(s, fmt::compiled_string)
0040 #else
0041 #  define FMT_COMPILE(s) FMT_STRING(s)
0042 #endif
0043 
0044 template <typename T, typename... Tail>
0045 auto first(const T& value, const Tail&...) -> const T& {
0046   return value;
0047 }
0048 
0049 #if defined(__cpp_if_constexpr) && defined(__cpp_return_type_deduction)
0050 template <typename... Args> struct type_list {};
0051 
0052 // Returns a reference to the argument at index N from [first, rest...].
0053 template <int N, typename T, typename... Args>
0054 constexpr const auto& get([[maybe_unused]] const T& first,
0055                           [[maybe_unused]] const Args&... rest) {
0056   static_assert(N < 1 + sizeof...(Args), "index is out of bounds");
0057   if constexpr (N == 0)
0058     return first;
0059   else
0060     return detail::get<N - 1>(rest...);
0061 }
0062 
0063 #  if FMT_USE_NONTYPE_TEMPLATE_ARGS
0064 template <int N, typename T, typename... Args, typename Char>
0065 constexpr auto get_arg_index_by_name(basic_string_view<Char> name) -> int {
0066   if constexpr (is_static_named_arg<T>()) {
0067     if (name == T::name) return N;
0068   }
0069   if constexpr (sizeof...(Args) > 0)
0070     return get_arg_index_by_name<N + 1, Args...>(name);
0071   (void)name;  // Workaround an MSVC bug about "unused" parameter.
0072   return -1;
0073 }
0074 #  endif
0075 
0076 template <typename... Args, typename Char>
0077 FMT_CONSTEXPR auto get_arg_index_by_name(basic_string_view<Char> name) -> int {
0078 #  if FMT_USE_NONTYPE_TEMPLATE_ARGS
0079   if constexpr (sizeof...(Args) > 0)
0080     return get_arg_index_by_name<0, Args...>(name);
0081 #  endif
0082   (void)name;
0083   return -1;
0084 }
0085 
0086 template <typename Char, typename... Args>
0087 constexpr int get_arg_index_by_name(basic_string_view<Char> name,
0088                                     type_list<Args...>) {
0089   return get_arg_index_by_name<Args...>(name);
0090 }
0091 
0092 template <int N, typename> struct get_type_impl;
0093 
0094 template <int N, typename... Args> struct get_type_impl<N, type_list<Args...>> {
0095   using type =
0096       remove_cvref_t<decltype(detail::get<N>(std::declval<Args>()...))>;
0097 };
0098 
0099 template <int N, typename T>
0100 using get_type = typename get_type_impl<N, T>::type;
0101 
0102 template <typename T> struct is_compiled_format : std::false_type {};
0103 
0104 template <typename Char> struct text {
0105   basic_string_view<Char> data;
0106   using char_type = Char;
0107 
0108   template <typename OutputIt, typename... Args>
0109   constexpr OutputIt format(OutputIt out, const Args&...) const {
0110     return write<Char>(out, data);
0111   }
0112 };
0113 
0114 template <typename Char>
0115 struct is_compiled_format<text<Char>> : std::true_type {};
0116 
0117 template <typename Char>
0118 constexpr text<Char> make_text(basic_string_view<Char> s, size_t pos,
0119                                size_t size) {
0120   return {{&s[pos], size}};
0121 }
0122 
0123 template <typename Char> struct code_unit {
0124   Char value;
0125   using char_type = Char;
0126 
0127   template <typename OutputIt, typename... Args>
0128   constexpr OutputIt format(OutputIt out, const Args&...) const {
0129     *out++ = value;
0130     return out;
0131   }
0132 };
0133 
0134 // This ensures that the argument type is convertible to `const T&`.
0135 template <typename T, int N, typename... Args>
0136 constexpr const T& get_arg_checked(const Args&... args) {
0137   const auto& arg = detail::get<N>(args...);
0138   if constexpr (detail::is_named_arg<remove_cvref_t<decltype(arg)>>()) {
0139     return arg.value;
0140   } else {
0141     return arg;
0142   }
0143 }
0144 
0145 template <typename Char>
0146 struct is_compiled_format<code_unit<Char>> : std::true_type {};
0147 
0148 // A replacement field that refers to argument N.
0149 template <typename Char, typename T, int N> struct field {
0150   using char_type = Char;
0151 
0152   template <typename OutputIt, typename... Args>
0153   constexpr OutputIt format(OutputIt out, const Args&... args) const {
0154     const T& arg = get_arg_checked<T, N>(args...);
0155     if constexpr (std::is_convertible<T, basic_string_view<Char>>::value) {
0156       auto s = basic_string_view<Char>(arg);
0157       return copy<Char>(s.begin(), s.end(), out);
0158     } else {
0159       return write<Char>(out, arg);
0160     }
0161   }
0162 };
0163 
0164 template <typename Char, typename T, int N>
0165 struct is_compiled_format<field<Char, T, N>> : std::true_type {};
0166 
0167 // A replacement field that refers to argument with name.
0168 template <typename Char> struct runtime_named_field {
0169   using char_type = Char;
0170   basic_string_view<Char> name;
0171 
0172   template <typename OutputIt, typename T>
0173   constexpr static bool try_format_argument(
0174       OutputIt& out,
0175       // [[maybe_unused]] due to unused-but-set-parameter warning in GCC 7,8,9
0176       [[maybe_unused]] basic_string_view<Char> arg_name, const T& arg) {
0177     if constexpr (is_named_arg<typename std::remove_cv<T>::type>::value) {
0178       if (arg_name == arg.name) {
0179         out = write<Char>(out, arg.value);
0180         return true;
0181       }
0182     }
0183     return false;
0184   }
0185 
0186   template <typename OutputIt, typename... Args>
0187   constexpr OutputIt format(OutputIt out, const Args&... args) const {
0188     bool found = (try_format_argument(out, name, args) || ...);
0189     if (!found) {
0190       FMT_THROW(format_error("argument with specified name is not found"));
0191     }
0192     return out;
0193   }
0194 };
0195 
0196 template <typename Char>
0197 struct is_compiled_format<runtime_named_field<Char>> : std::true_type {};
0198 
0199 // A replacement field that refers to argument N and has format specifiers.
0200 template <typename Char, typename T, int N> struct spec_field {
0201   using char_type = Char;
0202   formatter<T, Char> fmt;
0203 
0204   template <typename OutputIt, typename... Args>
0205   constexpr FMT_INLINE OutputIt format(OutputIt out,
0206                                        const Args&... args) const {
0207     const auto& vargs =
0208         fmt::make_format_args<basic_format_context<OutputIt, Char>>(args...);
0209     basic_format_context<OutputIt, Char> ctx(out, vargs);
0210     return fmt.format(get_arg_checked<T, N>(args...), ctx);
0211   }
0212 };
0213 
0214 template <typename Char, typename T, int N>
0215 struct is_compiled_format<spec_field<Char, T, N>> : std::true_type {};
0216 
0217 template <typename L, typename R> struct concat {
0218   L lhs;
0219   R rhs;
0220   using char_type = typename L::char_type;
0221 
0222   template <typename OutputIt, typename... Args>
0223   constexpr OutputIt format(OutputIt out, const Args&... args) const {
0224     out = lhs.format(out, args...);
0225     return rhs.format(out, args...);
0226   }
0227 };
0228 
0229 template <typename L, typename R>
0230 struct is_compiled_format<concat<L, R>> : std::true_type {};
0231 
0232 template <typename L, typename R>
0233 constexpr concat<L, R> make_concat(L lhs, R rhs) {
0234   return {lhs, rhs};
0235 }
0236 
0237 struct unknown_format {};
0238 
0239 template <typename Char>
0240 constexpr size_t parse_text(basic_string_view<Char> str, size_t pos) {
0241   for (size_t size = str.size(); pos != size; ++pos) {
0242     if (str[pos] == '{' || str[pos] == '}') break;
0243   }
0244   return pos;
0245 }
0246 
0247 template <typename Args, size_t POS, int ID, typename S>
0248 constexpr auto compile_format_string(S fmt);
0249 
0250 template <typename Args, size_t POS, int ID, typename T, typename S>
0251 constexpr auto parse_tail(T head, S fmt) {
0252   if constexpr (POS != basic_string_view<typename S::char_type>(fmt).size()) {
0253     constexpr auto tail = compile_format_string<Args, POS, ID>(fmt);
0254     if constexpr (std::is_same<remove_cvref_t<decltype(tail)>,
0255                                unknown_format>())
0256       return tail;
0257     else
0258       return make_concat(head, tail);
0259   } else {
0260     return head;
0261   }
0262 }
0263 
0264 template <typename T, typename Char> struct parse_specs_result {
0265   formatter<T, Char> fmt;
0266   size_t end;
0267   int next_arg_id;
0268 };
0269 
0270 enum { manual_indexing_id = -1 };
0271 
0272 template <typename T, typename Char>
0273 constexpr parse_specs_result<T, Char> parse_specs(basic_string_view<Char> str,
0274                                                   size_t pos, int next_arg_id) {
0275   str.remove_prefix(pos);
0276   auto ctx =
0277       compile_parse_context<Char>(str, max_value<int>(), nullptr, next_arg_id);
0278   auto f = formatter<T, Char>();
0279   auto end = f.parse(ctx);
0280   return {f, pos + fmt::detail::to_unsigned(end - str.data()),
0281           next_arg_id == 0 ? manual_indexing_id : ctx.next_arg_id()};
0282 }
0283 
0284 template <typename Char> struct arg_id_handler {
0285   arg_id_kind kind;
0286   arg_ref<Char> arg_id;
0287 
0288   constexpr int on_auto() {
0289     FMT_ASSERT(false, "handler cannot be used with automatic indexing");
0290     return 0;
0291   }
0292   constexpr int on_index(int id) {
0293     kind = arg_id_kind::index;
0294     arg_id = arg_ref<Char>(id);
0295     return 0;
0296   }
0297   constexpr int on_name(basic_string_view<Char> id) {
0298     kind = arg_id_kind::name;
0299     arg_id = arg_ref<Char>(id);
0300     return 0;
0301   }
0302 };
0303 
0304 template <typename Char> struct parse_arg_id_result {
0305   arg_id_kind kind;
0306   arg_ref<Char> arg_id;
0307   const Char* arg_id_end;
0308 };
0309 
0310 template <int ID, typename Char>
0311 constexpr auto parse_arg_id(const Char* begin, const Char* end) {
0312   auto handler = arg_id_handler<Char>{arg_id_kind::none, arg_ref<Char>{}};
0313   auto arg_id_end = parse_arg_id(begin, end, handler);
0314   return parse_arg_id_result<Char>{handler.kind, handler.arg_id, arg_id_end};
0315 }
0316 
0317 template <typename T, typename Enable = void> struct field_type {
0318   using type = remove_cvref_t<T>;
0319 };
0320 
0321 template <typename T>
0322 struct field_type<T, enable_if_t<detail::is_named_arg<T>::value>> {
0323   using type = remove_cvref_t<decltype(T::value)>;
0324 };
0325 
0326 template <typename T, typename Args, size_t END_POS, int ARG_INDEX, int NEXT_ID,
0327           typename S>
0328 constexpr auto parse_replacement_field_then_tail(S fmt) {
0329   using char_type = typename S::char_type;
0330   constexpr auto str = basic_string_view<char_type>(fmt);
0331   constexpr char_type c = END_POS != str.size() ? str[END_POS] : char_type();
0332   if constexpr (c == '}') {
0333     return parse_tail<Args, END_POS + 1, NEXT_ID>(
0334         field<char_type, typename field_type<T>::type, ARG_INDEX>(), fmt);
0335   } else if constexpr (c != ':') {
0336     FMT_THROW(format_error("expected ':'"));
0337   } else {
0338     constexpr auto result = parse_specs<typename field_type<T>::type>(
0339         str, END_POS + 1, NEXT_ID == manual_indexing_id ? 0 : NEXT_ID);
0340     if constexpr (result.end >= str.size() || str[result.end] != '}') {
0341       FMT_THROW(format_error("expected '}'"));
0342       return 0;
0343     } else {
0344       return parse_tail<Args, result.end + 1, result.next_arg_id>(
0345           spec_field<char_type, typename field_type<T>::type, ARG_INDEX>{
0346               result.fmt},
0347           fmt);
0348     }
0349   }
0350 }
0351 
0352 // Compiles a non-empty format string and returns the compiled representation
0353 // or unknown_format() on unrecognized input.
0354 template <typename Args, size_t POS, int ID, typename S>
0355 constexpr auto compile_format_string(S fmt) {
0356   using char_type = typename S::char_type;
0357   constexpr auto str = basic_string_view<char_type>(fmt);
0358   if constexpr (str[POS] == '{') {
0359     if constexpr (POS + 1 == str.size())
0360       FMT_THROW(format_error("unmatched '{' in format string"));
0361     if constexpr (str[POS + 1] == '{') {
0362       return parse_tail<Args, POS + 2, ID>(make_text(str, POS, 1), fmt);
0363     } else if constexpr (str[POS + 1] == '}' || str[POS + 1] == ':') {
0364       static_assert(ID != manual_indexing_id,
0365                     "cannot switch from manual to automatic argument indexing");
0366       constexpr auto next_id =
0367           ID != manual_indexing_id ? ID + 1 : manual_indexing_id;
0368       return parse_replacement_field_then_tail<get_type<ID, Args>, Args,
0369                                                POS + 1, ID, next_id>(fmt);
0370     } else {
0371       constexpr auto arg_id_result =
0372           parse_arg_id<ID>(str.data() + POS + 1, str.data() + str.size());
0373       constexpr auto arg_id_end_pos = arg_id_result.arg_id_end - str.data();
0374       constexpr char_type c =
0375           arg_id_end_pos != str.size() ? str[arg_id_end_pos] : char_type();
0376       static_assert(c == '}' || c == ':', "missing '}' in format string");
0377       if constexpr (arg_id_result.kind == arg_id_kind::index) {
0378         static_assert(
0379             ID == manual_indexing_id || ID == 0,
0380             "cannot switch from automatic to manual argument indexing");
0381         constexpr auto arg_index = arg_id_result.arg_id.index;
0382         return parse_replacement_field_then_tail<get_type<arg_index, Args>,
0383                                                  Args, arg_id_end_pos,
0384                                                  arg_index, manual_indexing_id>(
0385             fmt);
0386       } else if constexpr (arg_id_result.kind == arg_id_kind::name) {
0387         constexpr auto arg_index =
0388             get_arg_index_by_name(arg_id_result.arg_id.name, Args{});
0389         if constexpr (arg_index >= 0) {
0390           constexpr auto next_id =
0391               ID != manual_indexing_id ? ID + 1 : manual_indexing_id;
0392           return parse_replacement_field_then_tail<
0393               decltype(get_type<arg_index, Args>::value), Args, arg_id_end_pos,
0394               arg_index, next_id>(fmt);
0395         } else if constexpr (c == '}') {
0396           return parse_tail<Args, arg_id_end_pos + 1, ID>(
0397               runtime_named_field<char_type>{arg_id_result.arg_id.name}, fmt);
0398         } else if constexpr (c == ':') {
0399           return unknown_format();  // no type info for specs parsing
0400         }
0401       }
0402     }
0403   } else if constexpr (str[POS] == '}') {
0404     if constexpr (POS + 1 == str.size())
0405       FMT_THROW(format_error("unmatched '}' in format string"));
0406     return parse_tail<Args, POS + 2, ID>(make_text(str, POS, 1), fmt);
0407   } else {
0408     constexpr auto end = parse_text(str, POS + 1);
0409     if constexpr (end - POS > 1) {
0410       return parse_tail<Args, end, ID>(make_text(str, POS, end - POS), fmt);
0411     } else {
0412       return parse_tail<Args, end, ID>(code_unit<char_type>{str[POS]}, fmt);
0413     }
0414   }
0415 }
0416 
0417 template <typename... Args, typename S,
0418           FMT_ENABLE_IF(is_compiled_string<S>::value)>
0419 constexpr auto compile(S fmt) {
0420   constexpr auto str = basic_string_view<typename S::char_type>(fmt);
0421   if constexpr (str.size() == 0) {
0422     return detail::make_text(str, 0, 0);
0423   } else {
0424     constexpr auto result =
0425         detail::compile_format_string<detail::type_list<Args...>, 0, 0>(fmt);
0426     return result;
0427   }
0428 }
0429 #endif  // defined(__cpp_if_constexpr) && defined(__cpp_return_type_deduction)
0430 }  // namespace detail
0431 
0432 FMT_BEGIN_EXPORT
0433 
0434 #if defined(__cpp_if_constexpr) && defined(__cpp_return_type_deduction)
0435 
0436 template <typename CompiledFormat, typename... Args,
0437           typename Char = typename CompiledFormat::char_type,
0438           FMT_ENABLE_IF(detail::is_compiled_format<CompiledFormat>::value)>
0439 FMT_INLINE std::basic_string<Char> format(const CompiledFormat& cf,
0440                                           const Args&... args) {
0441   auto s = std::basic_string<Char>();
0442   cf.format(std::back_inserter(s), args...);
0443   return s;
0444 }
0445 
0446 template <typename OutputIt, typename CompiledFormat, typename... Args,
0447           FMT_ENABLE_IF(detail::is_compiled_format<CompiledFormat>::value)>
0448 constexpr FMT_INLINE OutputIt format_to(OutputIt out, const CompiledFormat& cf,
0449                                         const Args&... args) {
0450   return cf.format(out, args...);
0451 }
0452 
0453 template <typename S, typename... Args,
0454           FMT_ENABLE_IF(is_compiled_string<S>::value)>
0455 FMT_INLINE std::basic_string<typename S::char_type> format(const S&,
0456                                                            Args&&... args) {
0457   if constexpr (std::is_same<typename S::char_type, char>::value) {
0458     constexpr auto str = basic_string_view<typename S::char_type>(S());
0459     if constexpr (str.size() == 2 && str[0] == '{' && str[1] == '}') {
0460       const auto& first = detail::first(args...);
0461       if constexpr (detail::is_named_arg<
0462                         remove_cvref_t<decltype(first)>>::value) {
0463         return fmt::to_string(first.value);
0464       } else {
0465         return fmt::to_string(first);
0466       }
0467     }
0468   }
0469   constexpr auto compiled = detail::compile<Args...>(S());
0470   if constexpr (std::is_same<remove_cvref_t<decltype(compiled)>,
0471                              detail::unknown_format>()) {
0472     return fmt::format(
0473         static_cast<basic_string_view<typename S::char_type>>(S()),
0474         std::forward<Args>(args)...);
0475   } else {
0476     return fmt::format(compiled, std::forward<Args>(args)...);
0477   }
0478 }
0479 
0480 template <typename OutputIt, typename S, typename... Args,
0481           FMT_ENABLE_IF(is_compiled_string<S>::value)>
0482 FMT_CONSTEXPR OutputIt format_to(OutputIt out, const S&, Args&&... args) {
0483   constexpr auto compiled = detail::compile<Args...>(S());
0484   if constexpr (std::is_same<remove_cvref_t<decltype(compiled)>,
0485                              detail::unknown_format>()) {
0486     return fmt::format_to(
0487         out, static_cast<basic_string_view<typename S::char_type>>(S()),
0488         std::forward<Args>(args)...);
0489   } else {
0490     return fmt::format_to(out, compiled, std::forward<Args>(args)...);
0491   }
0492 }
0493 #endif
0494 
0495 template <typename OutputIt, typename S, typename... Args,
0496           FMT_ENABLE_IF(is_compiled_string<S>::value)>
0497 auto format_to_n(OutputIt out, size_t n, const S& fmt, Args&&... args)
0498     -> format_to_n_result<OutputIt> {
0499   using traits = detail::fixed_buffer_traits;
0500   auto buf = detail::iterator_buffer<OutputIt, char, traits>(out, n);
0501   fmt::format_to(std::back_inserter(buf), fmt, std::forward<Args>(args)...);
0502   return {buf.out(), buf.count()};
0503 }
0504 
0505 template <typename S, typename... Args,
0506           FMT_ENABLE_IF(is_compiled_string<S>::value)>
0507 FMT_CONSTEXPR20 auto formatted_size(const S& fmt, const Args&... args)
0508     -> size_t {
0509   auto buf = detail::counting_buffer<>();
0510   fmt::format_to(appender(buf), fmt, args...);
0511   return buf.count();
0512 }
0513 
0514 template <typename S, typename... Args,
0515           FMT_ENABLE_IF(is_compiled_string<S>::value)>
0516 void print(std::FILE* f, const S& fmt, const Args&... args) {
0517   auto buf = memory_buffer();
0518   fmt::format_to(appender(buf), fmt, args...);
0519   detail::print(f, {buf.data(), buf.size()});
0520 }
0521 
0522 template <typename S, typename... Args,
0523           FMT_ENABLE_IF(is_compiled_string<S>::value)>
0524 void print(const S& fmt, const Args&... args) {
0525   print(stdout, fmt, args...);
0526 }
0527 
0528 #if FMT_USE_NONTYPE_TEMPLATE_ARGS
0529 inline namespace literals {
0530 template <detail::fixed_string Str> constexpr auto operator""_cf() {
0531   return FMT_COMPILE(Str.data);
0532 }
0533 }  // namespace literals
0534 #endif
0535 
0536 FMT_END_EXPORT
0537 FMT_END_NAMESPACE
0538 
0539 #endif  // FMT_COMPILE_H_