// Formatting library for C++ - the standard API implementation // // Copyright (c) 2012 - present, Victor Zverovich // All rights reserved. // // For the license information refer to format.h. #ifndef FMT_FORMAT_ #define FMT_FORMAT_ #include #include "fmt/format.h" // This implementation verifies the correctness of the standard API proposed in // P0645 Text Formatting and is optimized for copy-pasting from the paper, not // for efficiency or readability. An efficient implementation should not use // std::variant and should store packed argument type tags separately from // values in basic_format_args for small number of arguments. #define FMT_REQUIRES(...) #define FMT_CONCEPT(C) typename namespace std { template constexpr bool Integral = is_integral_v; template using iter_difference_t = ptrdiff_t; } // http://fmtlib.net/Text%20Formatting.html#format.syn namespace std { // [format.error], class format_error class format_error; // [format.formatter], formatter template class basic_format_parse_context; using format_parse_context = basic_format_parse_context; using wformat_parse_context = basic_format_parse_context; template FMT_REQUIRES(OutputIterator) class basic_format_context; using format_context = basic_format_context< /* unspecified */ std::back_insert_iterator>, char>; using wformat_context = basic_format_context< /* unspecified */ std::back_insert_iterator>, wchar_t>; template struct formatter { formatter() = delete; }; // [format.arguments], arguments template class basic_format_arg; template /* see below */ auto visit_format_arg(Visitor&& vis, basic_format_arg arg); template struct format_arg_store; // exposition only template class basic_format_args; using format_args = basic_format_args; using wformat_args = basic_format_args; template using format_args_t = basic_format_args>; template format_arg_store make_format_args(const Args&... args); template format_arg_store make_wformat_args(const Args&... args); // [format.functions], formatting functions template string format(string_view fmt, const Args&... args); template wstring format(wstring_view fmt, const Args&... args); string vformat(string_view fmt, format_args args); wstring vformat(wstring_view fmt, wformat_args args); template) O, class... Args> O format_to(O out, string_view fmt, const Args&... args); template) O, class... Args> O format_to(O out, wstring_view fmt, const Args&... args); template) O> O vformat_to(O out, string_view fmt, format_args_t args); template) O> O vformat_to(O out, wstring_view fmt, format_args_t args); template struct format_to_n_result { O out; iter_difference_t size; }; template) O, class... Args> format_to_n_result format_to_n(O out, iter_difference_t n, string_view fmt, const Args&... args); template) O, class... Args> format_to_n_result format_to_n(O out, iter_difference_t n, wstring_view fmt, const Args&... args); template size_t formatted_size(string_view fmt, const Args&... args); template size_t formatted_size(wstring_view fmt, const Args&... args); } // http://fmtlib.net/Text%20Formatting.html#format.error namespace std { class format_error : public runtime_error { public: explicit format_error(const string& what_arg) : runtime_error(what_arg) {} explicit format_error(const char* what_arg) : runtime_error(what_arg) {} }; } // http://fmtlib.net/Text%20Formatting.html#format.parse_context namespace std { template class basic_format_parse_context { public: using char_type = charT; using const_iterator = typename basic_string_view::const_iterator; using iterator = const_iterator; private: iterator begin_; // exposition only iterator end_; // exposition only enum indexing { unknown, manual, automatic }; // exposition only indexing indexing_; // exposition only size_t next_arg_id_; // exposition only public: explicit constexpr basic_format_parse_context(basic_string_view fmt) noexcept; basic_format_parse_context(const basic_format_parse_context&) = delete; basic_format_parse_context& operator=(const basic_format_parse_context&) = delete; constexpr const_iterator begin() const noexcept; constexpr const_iterator end() const noexcept; constexpr void advance_to(iterator it); constexpr size_t next_arg_id(); constexpr void check_arg_id(size_t id); // Implementation detail: constexpr void check_arg_id(fmt::string_view) {} fmt::internal::error_handler error_handler() const { return {}; } void on_error(const char* msg) { error_handler().on_error(msg); } }; } namespace std { template /* explicit */ constexpr basic_format_parse_context::basic_format_parse_context(basic_string_view fmt) noexcept : begin_(fmt.begin()), end_(fmt.end()), indexing_(unknown), next_arg_id_(0) {} template constexpr typename basic_format_parse_context::const_iterator basic_format_parse_context::begin() const noexcept { return begin_; } template constexpr typename basic_format_parse_context::const_iterator basic_format_parse_context::end() const noexcept { return end_; } template constexpr void basic_format_parse_context::advance_to(typename basic_format_parse_context::iterator it) { begin_ = it; } template constexpr size_t basic_format_parse_context::next_arg_id() { if (indexing_ == manual) throw format_error("manual to automatic indexing"); if (indexing_ == unknown) indexing_ = automatic; return next_arg_id_++; } template constexpr void basic_format_parse_context::check_arg_id(size_t) { if (indexing_ == automatic) throw format_error("automatic to manual indexing"); if (indexing_ == unknown) indexing_ = manual; } } // http://fmtlib.net/Text%20Formatting.html#format.context namespace std { template FMT_REQUIRES(OutputIterator) class basic_format_context { basic_format_args args_; // exposition only O out_; // exposition only public: using iterator = O; using char_type = charT; template using formatter_type = formatter; basic_format_arg arg(size_t id) const; iterator out(); void advance_to(iterator it); // Implementation details: using format_arg = basic_format_arg; basic_format_context(O out, basic_format_args args, fmt::internal::locale_ref) : args_(args), out_(out) {} fmt::internal::error_handler error_handler() const { return {}; } basic_format_arg arg(fmt::basic_string_view) const { return {}; // unused: named arguments are not supported yet } void on_error(const char* msg) { error_handler().on_error(msg); } }; } namespace std { template basic_format_arg> basic_format_context::arg(size_t id) const { return args_.get(id); } template typename basic_format_context::iterator basic_format_context::out() { return out_; } template void basic_format_context::advance_to(typename basic_format_context::iterator it) { out_ = it; } } // http://fmtlib.net/Text%20Formatting.html#format.arg namespace std { template class basic_format_arg { public: class handle; using char_type = typename Context::char_type; // exposition only variant, const void*, handle> value; // exposition only basic_format_arg() noexcept; template>> explicit basic_format_arg(I n) noexcept; // exposition only explicit basic_format_arg(float n) noexcept; // exposition only explicit basic_format_arg(double n) noexcept; // exposition only explicit basic_format_arg(long double n) noexcept; // exposition only explicit basic_format_arg(const char_type* s) noexcept; // exposition only explicit basic_format_arg(nullptr_t) noexcept; // exposition only template explicit basic_format_arg( basic_string_view s) noexcept; // exposition only template explicit basic_format_arg( const basic_string& s) noexcept; // exposition only template>> explicit basic_format_arg(const T* p) noexcept; // exposition only template && is_default_constructible_v>>> explicit basic_format_arg(const T& v) noexcept; // exposition only explicit operator bool() const noexcept; }; } namespace std { template basic_format_arg::basic_format_arg() noexcept {} template constexpr bool is_standard_integer_v = std::is_same_v || std::is_same_v || std::is_same_v || std::is_same_v || std::is_same_v; template constexpr bool is_standard_unsigned_integer_v = std::is_same_v || std::is_same_v || std::is_same_v || std::is_same_v || std::is_same_v; template template /* explicit */ basic_format_arg::basic_format_arg(I n) noexcept { if (std::is_same_v || std::is_same_v) value = n; else if (std::is_same_v || std::is_same_v) value = static_cast(n); else if (std::is_standard_integer_v && sizeof(I) <= sizeof(int)) value = static_cast(n); else if (std::is_standard_integer_v && sizeof(I) <= sizeof(unsigned)) value = static_cast(n); else if (std::is_standard_integer_v) value = static_cast(n); else if (std::is_standard_integer_v) value = static_cast(n); else assert(false); // should be a compile-time error instead } template /* explicit */ basic_format_arg::basic_format_arg(float n) noexcept : value(static_cast(n)) {} template /* explicit */ basic_format_arg::basic_format_arg(double n) noexcept : value(n) {} template /* explicit */ basic_format_arg::basic_format_arg(long double n) noexcept : value(n) {} template /* explicit */ basic_format_arg::basic_format_arg(const typename basic_format_arg::char_type* s) noexcept : value(s) { assert(s != nullptr); } template template /* explicit */ basic_format_arg::basic_format_arg(basic_string_view s) noexcept : value(s) {} template template /* explicit */ basic_format_arg::basic_format_arg( const basic_string& s) noexcept : value(basic_string_view(s.data(), s.size())) {} template /* explicit */ basic_format_arg::basic_format_arg(nullptr_t) noexcept : value(static_cast(nullptr)) {} template template /* explicit */ basic_format_arg::basic_format_arg(const T* p) noexcept : value(p) {} template template /* explicit */ basic_format_arg::basic_format_arg(const T& v) noexcept : value(handle(v)) {} template /* explicit */ basic_format_arg::operator bool() const noexcept { return !holds_alternative(value); } } namespace std { template class basic_format_arg::handle { const void* ptr_; // exposition only void (*format_)(basic_format_parse_context&, Context&, const void*); // exposition only public: template explicit handle(const T& val) noexcept; // exposition only void format(basic_format_parse_context&, Context& ctx) const; }; } namespace std { template template /* explicit */ basic_format_arg::handle::handle(const T& val) noexcept : ptr_(&val), format_([](basic_format_parse_context& parse_ctx, Context& format_ctx, const void* ptr) { typename Context::template formatter_type f; parse_ctx.advance_to(f.parse(parse_ctx)); format_ctx.advance_to(f.format(*static_cast(ptr), format_ctx)); }) {} template void basic_format_arg::handle::format(basic_format_parse_context& parse_ctx, Context& format_ctx) const { format_(parse_ctx, format_ctx, ptr_); } // http://fmtlib.net/Text%20Formatting.html#format.visit template auto visit_format_arg(Visitor&& vis, basic_format_arg arg) { return visit(vis, arg.value); } } // http://fmtlib.net/Text%20Formatting.html#format.store namespace std { template struct format_arg_store { // exposition only array, sizeof...(Args)> args; }; } // http://fmtlib.net/Text%20Formatting.html#format.basic_args namespace std { template class basic_format_args { size_t size_; // exposition only const basic_format_arg* data_; // exposition only public: basic_format_args() noexcept; template basic_format_args(const format_arg_store& store) noexcept; basic_format_arg get(size_t i) const noexcept; }; } namespace std { template basic_format_args::basic_format_args() noexcept : size_(0) {} template template basic_format_args::basic_format_args(const format_arg_store& store) noexcept : size_(sizeof...(Args)), data_(store.args.data()) {} template basic_format_arg basic_format_args::get(size_t i) const noexcept { return i < size_ ? data_[i] : basic_format_arg(); } } namespace std { // http://fmtlib.net/Text%20Formatting.html#format.make_args template format_arg_store make_format_args(const Args&... args) { return {basic_format_arg(args)...}; } // http://fmtlib.net/Text%20Formatting.html#format.make_wargs template format_arg_store make_wformat_args(const Args&... args) { return {basic_format_arg(args)...}; } } namespace std { namespace detail { template class arg_formatter : public fmt::internal::function< typename fmt::internal::arg_formatter_base::iterator>, public fmt::internal::arg_formatter_base { private: using char_type = typename Range::value_type; using base = fmt::internal::arg_formatter_base; using format_context = std::basic_format_context; using parse_context = basic_format_parse_context; parse_context& parse_ctx_; format_context& ctx_; public: typedef Range range; typedef typename base::iterator iterator; typedef typename base::format_specs format_specs; /** \rst Constructs an argument formatter object. *ctx* is a reference to the formatting context, *spec* contains format specifier information for standard argument types. \endrst */ arg_formatter(parse_context& parse_ctx, format_context& ctx, fmt::format_specs* spec = FMT_NULL) : base(Range(ctx.out()), spec, {}), parse_ctx_(parse_ctx), ctx_(ctx) {} using base::operator(); /** Formats an argument of a user-defined type. */ iterator operator()(typename std::basic_format_arg::handle handle) { handle.format(parse_ctx_, ctx_); return this->out(); } iterator operator()(monostate) { throw format_error(""); } }; template inline fmt::internal::type get_type(basic_format_arg arg) { return visit_format_arg([&] (auto val) { using char_type = typename Context::char_type; using T = decltype(val); if (std::is_same_v) return fmt::internal::none_type; if (std::is_same_v) return fmt::internal::bool_type; if (std::is_same_v) return fmt::internal::char_type; if (std::is_same_v) return fmt::internal::int_type; if (std::is_same_v) return fmt::internal::uint_type; if (std::is_same_v) return fmt::internal::long_long_type; if (std::is_same_v) return fmt::internal::ulong_long_type; if (std::is_same_v) return fmt::internal::double_type; if (std::is_same_v) return fmt::internal::long_double_type; if (std::is_same_v) return fmt::internal::cstring_type; if (std::is_same_v>) return fmt::internal::string_type; if (std::is_same_v) return fmt::internal::pointer_type; assert(arg.value.index() == 12); return fmt::internal::custom_type; }, arg); } template class custom_formatter { private: using parse_context = basic_format_parse_context; parse_context& parse_ctx_; Context& format_ctx_; public: custom_formatter(parse_context& parse_ctx, Context& ctx) : parse_ctx_(parse_ctx), format_ctx_(ctx) {} bool operator()(typename basic_format_arg::handle h) const { h.format(parse_ctx_, format_ctx_); return true; } template bool operator()(T) const { return false; } }; template struct format_handler : fmt::internal::error_handler { typedef typename ArgFormatter::range range; format_handler(range r, basic_string_view str, basic_format_args format_args, fmt::internal::locale_ref loc) : parse_ctx(str), context(r.begin(), format_args, loc) {} void on_text(const Char* begin, const Char* end) { auto size = fmt::internal::to_unsigned(end - begin); auto out = context.out(); auto&& it = fmt::internal::reserve(out, size); it = std::copy_n(begin, size, it); context.advance_to(out); } void on_arg_id() { arg = context.arg(parse_ctx.next_arg_id()); } void on_arg_id(unsigned id) { parse_ctx.check_arg_id(id); arg = context.arg(id); } void on_arg_id(fmt::basic_string_view) {} void on_replacement_field(const Char* p) { parse_ctx.advance_to(p); custom_formatter f(parse_ctx, context); if (!visit_format_arg(f, arg)) context.advance_to(visit_format_arg(ArgFormatter(parse_ctx, context), arg)); } const Char* on_format_specs(const Char* begin, const Char* end) { parse_ctx.advance_to(begin); custom_formatter f(parse_ctx, context); if (visit_format_arg(f, arg)) return parse_ctx.begin(); fmt::basic_format_specs specs; using fmt::internal::specs_handler; using parse_context = basic_format_parse_context; fmt::internal::specs_checker> handler( specs_handler(specs, parse_ctx, context), get_type(arg)); begin = parse_format_specs(begin, end, handler); if (begin == end || *begin != '}') on_error("missing '}' in format string"); parse_ctx.advance_to(begin); context.advance_to(visit_format_arg(ArgFormatter(parse_ctx, context, &specs), arg)); return begin; } basic_format_parse_context parse_ctx; Context context; basic_format_arg arg; }; template struct formatter { // Parses format specifiers stopping either at the end of the range or at the // terminating '}'. template FMT_CONSTEXPR typename ParseContext::iterator parse(ParseContext& ctx) { namespace internal = fmt::internal; typedef internal::dynamic_specs_handler handler_type; auto type = internal::get_type::type, T>::value; internal::specs_checker handler(handler_type(specs_, ctx), type); auto it = parse_format_specs(ctx.begin(), ctx.end(), handler); auto type_spec = specs_.type; auto eh = ctx.error_handler(); switch (type) { case internal::none_type: case internal::named_arg_type: FMT_ASSERT(false, "invalid argument type"); break; case internal::int_type: case internal::uint_type: case internal::long_long_type: case internal::ulong_long_type: case internal::bool_type: handle_int_type_spec(type_spec, internal::int_type_checker(eh)); break; case internal::char_type: handle_char_specs( &specs_, internal::char_specs_checker(type_spec, eh)); break; case internal::double_type: case internal::long_double_type: handle_float_type_spec(type_spec, internal::float_type_checker(eh)); break; case internal::cstring_type: internal::handle_cstring_type_spec( type_spec, internal::cstring_type_checker(eh)); break; case internal::string_type: internal::check_string_type_spec(type_spec, eh); break; case internal::pointer_type: internal::check_pointer_type_spec(type_spec, eh); break; case internal::custom_type: // Custom format specifiers should be checked in parse functions of // formatter specializations. break; } return it; } template auto format(const T& val, FormatContext& ctx) -> decltype(ctx.out()) { fmt::internal::handle_dynamic_spec( specs_.width_, specs_.width_ref, ctx); fmt::internal::handle_dynamic_spec( specs_.precision, specs_.precision_ref, ctx); typedef fmt::output_range range_type; return visit_format_arg(arg_formatter(ctx, &specs_), basic_format_arg(val)); } private: fmt::internal::dynamic_format_specs specs_; }; } // namespace detail // http://fmtlib.net/Text%20Formatting.html#format.functions template string format(string_view fmt, const Args&... args) { return vformat(fmt, make_format_args(args...)); } template wstring format(wstring_view fmt, const Args&... args) { return vformat(fmt, make_wformat_args(args...)); } string vformat(string_view fmt, format_args args) { fmt::memory_buffer mbuf; fmt::internal::buffer& buf = mbuf; typedef fmt::back_insert_range range; detail::format_handler, char, format_context> h(range(std::back_inserter(buf)), fmt, args, {}); fmt::internal::parse_format_string(fmt::to_string_view(fmt), h); return to_string(mbuf); } wstring vformat(wstring_view fmt, wformat_args args); template) O, class... Args> O format_to(O out, string_view fmt, const Args&... args) { return vformat_to(out, fmt, {make_format_args>(args...)}); } template) O, class... Args> O format_to(O out, wstring_view fmt, const Args&... args) { return vformat_to(out, fmt, {make_format_args>(args...)}); } template) O> O vformat_to(O out, string_view fmt, format_args_t args) { typedef fmt::output_range range; detail::format_handler, char, basic_format_context> h(range(out), fmt, args, {}); fmt::internal::parse_format_string(fmt::to_string_view(fmt), h); return h.context.out(); } template) O> O vformat_to(O out, wstring_view fmt, format_args_t args); template) O, class... Args> format_to_n_result format_to_n(O out, iter_difference_t n, string_view fmt, const Args&... args); template) O, class... Args> format_to_n_result format_to_n(O out, iter_difference_t n, wstring_view fmt, const Args&... args); template size_t formatted_size(string_view fmt, const Args&... args); template size_t formatted_size(wstring_view fmt, const Args&... args); #define charT char template<> struct formatter : detail::formatter {}; template<> struct formatter; template<> struct formatter : detail::formatter {}; template<> struct formatter : detail::formatter {}; template struct formatter : detail::formatter, charT> {}; template struct formatter, charT> : detail::formatter, charT> {}; template struct formatter, charT> : detail::formatter, charT> {}; template <> struct formatter : detail::formatter {}; template <> struct formatter : detail::formatter {}; template <> struct formatter : detail::formatter {}; template <> struct formatter : detail::formatter {}; template <> struct formatter : detail::formatter {}; template <> struct formatter : detail::formatter {}; template <> struct formatter : detail::formatter {}; template <> struct formatter : detail::formatter, charT> {}; template <> struct formatter : detail::formatter {}; template <> struct formatter : detail::formatter {}; template <> struct formatter : detail::formatter {}; template <> struct formatter : detail::formatter {}; template <> struct formatter : detail::formatter, charT> {}; template <> struct formatter : detail::formatter {}; template <> struct formatter : detail::formatter {}; template <> struct formatter : detail::formatter {}; template <> struct formatter : detail::formatter {}; #undef charT #define charT wchar_t template<> struct formatter : detail::formatter {}; template<> struct formatter : detail::formatter {}; template<> struct formatter : detail::formatter {}; template<> struct formatter : detail::formatter {}; template struct formatter : detail::formatter, charT> {}; template struct formatter, charT> : detail::formatter, charT> {}; template struct formatter, charT> : detail::formatter, charT> {}; template <> struct formatter : detail::formatter {}; template <> struct formatter : detail::formatter {}; template <> struct formatter : detail::formatter {}; template <> struct formatter : detail::formatter {}; template <> struct formatter : detail::formatter {}; template <> struct formatter : detail::formatter {}; template <> struct formatter : detail::formatter {}; template <> struct formatter : detail::formatter, charT> {}; template <> struct formatter : detail::formatter {}; template <> struct formatter : detail::formatter {}; template <> struct formatter : detail::formatter {}; template <> struct formatter : detail::formatter {}; template <> struct formatter : detail::formatter, charT> {}; template <> struct formatter : detail::formatter {}; template <> struct formatter : detail::formatter {}; template <> struct formatter : detail::formatter {}; template <> struct formatter : detail::formatter {}; #undef charT template<> struct formatter { formatter() = delete; }; } #endif // FMT_FORMAT_