2019-06-24 01:54:46 +00:00
|
|
|
// Formatting library for C++ - experimental format string compilation
|
2018-11-27 10:52:00 +00:00
|
|
|
//
|
|
|
|
// Copyright (c) 2012 - present, Victor Zverovich and fmt contributors
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// For the license information refer to format.h.
|
|
|
|
|
2019-07-25 16:01:21 +00:00
|
|
|
#ifndef FMT_COMPILE_H_
|
|
|
|
#define FMT_COMPILE_H_
|
2018-11-27 10:52:00 +00:00
|
|
|
|
2020-11-11 16:31:34 +00:00
|
|
|
#include <algorithm>
|
2018-11-27 10:52:00 +00:00
|
|
|
#include <vector>
|
2020-01-15 16:09:48 +00:00
|
|
|
|
2019-07-25 17:27:27 +00:00
|
|
|
#include "format.h"
|
2018-11-27 10:52:00 +00:00
|
|
|
|
2020-12-07 23:53:11 +00:00
|
|
|
#ifndef FMT_USE_NONTYPE_TEMPLATE_PARAMETERS
|
|
|
|
# if defined(__cpp_nontype_template_parameter_class) && \
|
|
|
|
(!FMT_GCC_VERSION || FMT_GCC_VERSION >= 903)
|
|
|
|
# define FMT_USE_NONTYPE_TEMPLATE_PARAMETERS 1
|
|
|
|
# else
|
|
|
|
# define FMT_USE_NONTYPE_TEMPLATE_PARAMETERS 0
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2018-11-27 10:52:00 +00:00
|
|
|
FMT_BEGIN_NAMESPACE
|
2020-05-10 14:25:42 +00:00
|
|
|
namespace detail {
|
2018-11-27 10:52:00 +00:00
|
|
|
|
2020-06-12 14:05:49 +00:00
|
|
|
// A compile-time string which is compiled into fast formatting code.
|
|
|
|
class compiled_string {};
|
|
|
|
|
|
|
|
template <typename S>
|
|
|
|
struct is_compiled_string : std::is_base_of<compiled_string, S> {};
|
|
|
|
|
2020-06-25 13:57:23 +00:00
|
|
|
/**
|
|
|
|
\rst
|
2020-06-25 18:29:49 +00:00
|
|
|
Converts a string literal *s* into a format string that will be parsed at
|
|
|
|
compile time and converted into efficient formatting code. Requires C++17
|
|
|
|
``constexpr if`` compiler support.
|
2020-06-25 13:57:23 +00:00
|
|
|
|
|
|
|
**Example**::
|
|
|
|
|
2020-06-25 18:29:49 +00:00
|
|
|
// Converts 42 into std::string using the most efficient method and no
|
|
|
|
// runtime format string processing.
|
2020-06-25 13:57:23 +00:00
|
|
|
std::string s = fmt::format(FMT_COMPILE("{}"), 42);
|
|
|
|
\endrst
|
|
|
|
*/
|
2020-06-12 14:05:49 +00:00
|
|
|
#define FMT_COMPILE(s) FMT_STRING_IMPL(s, fmt::detail::compiled_string)
|
|
|
|
|
2020-12-07 23:53:11 +00:00
|
|
|
#if FMT_USE_NONTYPE_TEMPLATE_PARAMETERS
|
|
|
|
template <typename Char, size_t N> struct fixed_string {
|
|
|
|
constexpr fixed_string(const Char (&str)[N]) {
|
|
|
|
copy_str<Char, const Char*, Char*>(static_cast<const Char*>(str), str + N,
|
|
|
|
data);
|
|
|
|
}
|
|
|
|
Char data[N]{};
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Char, size_t N, fixed_string<Char, N> Str>
|
|
|
|
struct udl_compiled_string : compiled_string {
|
|
|
|
using char_type = Char;
|
|
|
|
constexpr operator basic_string_view<char_type>() const {
|
|
|
|
return {Str.data, N - 1};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2020-06-12 14:05:49 +00:00
|
|
|
template <typename T, typename... Tail>
|
2020-06-20 15:50:02 +00:00
|
|
|
const T& first(const T& value, const Tail&...) {
|
|
|
|
return value;
|
|
|
|
}
|
2020-06-12 14:05:49 +00:00
|
|
|
|
2019-09-01 16:05:24 +00:00
|
|
|
// Part of a compiled format string. It can be either literal text or a
|
|
|
|
// replacement field.
|
2018-11-27 10:52:00 +00:00
|
|
|
template <typename Char> struct format_part {
|
2019-09-01 16:05:24 +00:00
|
|
|
enum class kind { arg_index, arg_name, text, replacement };
|
2018-11-27 10:52:00 +00:00
|
|
|
|
2019-08-31 17:23:42 +00:00
|
|
|
struct replacement {
|
|
|
|
arg_ref<Char> arg_id;
|
2019-09-01 16:05:24 +00:00
|
|
|
dynamic_format_specs<Char> specs;
|
2018-11-27 10:52:00 +00:00
|
|
|
};
|
|
|
|
|
2019-08-31 17:23:42 +00:00
|
|
|
kind part_kind;
|
|
|
|
union value {
|
2019-11-30 01:57:14 +00:00
|
|
|
int arg_index;
|
2019-09-01 18:10:49 +00:00
|
|
|
basic_string_view<Char> str;
|
2019-08-31 17:23:42 +00:00
|
|
|
replacement repl;
|
2019-09-01 16:05:24 +00:00
|
|
|
|
2019-11-30 01:57:14 +00:00
|
|
|
FMT_CONSTEXPR value(int index = 0) : arg_index(index) {}
|
2019-09-01 18:10:49 +00:00
|
|
|
FMT_CONSTEXPR value(basic_string_view<Char> s) : str(s) {}
|
2019-09-01 16:05:24 +00:00
|
|
|
FMT_CONSTEXPR value(replacement r) : repl(r) {}
|
2019-08-31 17:23:42 +00:00
|
|
|
} val;
|
2019-09-01 19:12:19 +00:00
|
|
|
// Position past the end of the argument id.
|
|
|
|
const Char* arg_id_end = nullptr;
|
2018-11-27 10:52:00 +00:00
|
|
|
|
2019-09-01 16:05:24 +00:00
|
|
|
FMT_CONSTEXPR format_part(kind k = kind::arg_index, value v = {})
|
|
|
|
: part_kind(k), val(v) {}
|
|
|
|
|
2019-11-30 01:57:14 +00:00
|
|
|
static FMT_CONSTEXPR format_part make_arg_index(int index) {
|
2019-09-01 16:05:24 +00:00
|
|
|
return format_part(kind::arg_index, index);
|
|
|
|
}
|
2019-09-01 18:10:49 +00:00
|
|
|
static FMT_CONSTEXPR format_part make_arg_name(basic_string_view<Char> name) {
|
2019-09-01 16:05:24 +00:00
|
|
|
return format_part(kind::arg_name, name);
|
|
|
|
}
|
2019-09-01 18:10:49 +00:00
|
|
|
static FMT_CONSTEXPR format_part make_text(basic_string_view<Char> text) {
|
2019-09-01 16:05:24 +00:00
|
|
|
return format_part(kind::text, text);
|
|
|
|
}
|
|
|
|
static FMT_CONSTEXPR format_part make_replacement(replacement repl) {
|
|
|
|
return format_part(kind::replacement, repl);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Char> struct part_counter {
|
|
|
|
unsigned num_parts = 0;
|
|
|
|
|
|
|
|
FMT_CONSTEXPR void on_text(const Char* begin, const Char* end) {
|
|
|
|
if (begin != end) ++num_parts;
|
|
|
|
}
|
2018-11-27 10:52:00 +00:00
|
|
|
|
2020-06-06 14:13:38 +00:00
|
|
|
FMT_CONSTEXPR int on_arg_id() { return ++num_parts, 0; }
|
|
|
|
FMT_CONSTEXPR int on_arg_id(int) { return ++num_parts, 0; }
|
|
|
|
FMT_CONSTEXPR int on_arg_id(basic_string_view<Char>) {
|
|
|
|
return ++num_parts, 0;
|
|
|
|
}
|
2018-11-27 10:52:00 +00:00
|
|
|
|
2020-06-06 14:13:38 +00:00
|
|
|
FMT_CONSTEXPR void on_replacement_field(int, const Char*) {}
|
2018-11-27 10:52:00 +00:00
|
|
|
|
2020-06-06 14:13:38 +00:00
|
|
|
FMT_CONSTEXPR const Char* on_format_specs(int, const Char* begin,
|
2019-09-01 16:05:24 +00:00
|
|
|
const Char* end) {
|
|
|
|
// Find the matching brace.
|
|
|
|
unsigned brace_counter = 0;
|
|
|
|
for (; begin != end; ++begin) {
|
|
|
|
if (*begin == '{') {
|
|
|
|
++brace_counter;
|
|
|
|
} else if (*begin == '}') {
|
|
|
|
if (brace_counter == 0u) break;
|
|
|
|
--brace_counter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return begin;
|
|
|
|
}
|
2018-11-27 10:52:00 +00:00
|
|
|
|
2019-09-01 16:05:24 +00:00
|
|
|
FMT_CONSTEXPR void on_error(const char*) {}
|
2018-11-27 10:52:00 +00:00
|
|
|
};
|
|
|
|
|
2019-09-01 16:05:24 +00:00
|
|
|
// Counts the number of parts in a format string.
|
|
|
|
template <typename Char>
|
|
|
|
FMT_CONSTEXPR unsigned count_parts(basic_string_view<Char> format_str) {
|
|
|
|
part_counter<Char> counter;
|
|
|
|
parse_format_string<true>(format_str, counter);
|
|
|
|
return counter.num_parts;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Char, typename PartHandler>
|
|
|
|
class format_string_compiler : public error_handler {
|
2019-01-13 02:27:38 +00:00
|
|
|
private:
|
2019-08-04 18:15:07 +00:00
|
|
|
using part = format_part<Char>;
|
2018-11-27 10:52:00 +00:00
|
|
|
|
2019-09-01 19:12:19 +00:00
|
|
|
PartHandler handler_;
|
|
|
|
part part_;
|
|
|
|
basic_string_view<Char> format_str_;
|
2019-11-04 11:37:40 +00:00
|
|
|
basic_format_parse_context<Char> parse_context_;
|
2019-09-01 19:12:19 +00:00
|
|
|
|
2019-01-13 02:27:38 +00:00
|
|
|
public:
|
2019-09-01 16:05:24 +00:00
|
|
|
FMT_CONSTEXPR format_string_compiler(basic_string_view<Char> format_str,
|
|
|
|
PartHandler handler)
|
|
|
|
: handler_(handler),
|
|
|
|
format_str_(format_str),
|
|
|
|
parse_context_(format_str) {}
|
2018-11-27 10:52:00 +00:00
|
|
|
|
2019-01-13 02:27:38 +00:00
|
|
|
FMT_CONSTEXPR void on_text(const Char* begin, const Char* end) {
|
2019-09-01 18:10:49 +00:00
|
|
|
if (begin != end)
|
|
|
|
handler_(part::make_text({begin, to_unsigned(end - begin)}));
|
2018-11-27 10:52:00 +00:00
|
|
|
}
|
|
|
|
|
2020-06-06 14:13:38 +00:00
|
|
|
FMT_CONSTEXPR int on_arg_id() {
|
2019-09-01 16:05:24 +00:00
|
|
|
part_ = part::make_arg_index(parse_context_.next_arg_id());
|
2020-06-06 14:13:38 +00:00
|
|
|
return 0;
|
2018-11-27 10:52:00 +00:00
|
|
|
}
|
|
|
|
|
2020-06-06 14:13:38 +00:00
|
|
|
FMT_CONSTEXPR int on_arg_id(int id) {
|
2018-11-27 10:52:00 +00:00
|
|
|
parse_context_.check_arg_id(id);
|
2019-09-01 16:05:24 +00:00
|
|
|
part_ = part::make_arg_index(id);
|
2020-06-06 14:13:38 +00:00
|
|
|
return 0;
|
2018-11-27 10:52:00 +00:00
|
|
|
}
|
|
|
|
|
2020-06-06 14:13:38 +00:00
|
|
|
FMT_CONSTEXPR int on_arg_id(basic_string_view<Char> id) {
|
2019-09-01 18:10:49 +00:00
|
|
|
part_ = part::make_arg_name(id);
|
2020-06-06 14:13:38 +00:00
|
|
|
return 0;
|
2018-11-27 10:52:00 +00:00
|
|
|
}
|
|
|
|
|
2020-06-06 14:13:38 +00:00
|
|
|
FMT_CONSTEXPR void on_replacement_field(int, const Char* ptr) {
|
2019-09-01 19:12:19 +00:00
|
|
|
part_.arg_id_end = ptr;
|
2019-09-01 16:05:24 +00:00
|
|
|
handler_(part_);
|
2018-11-27 10:52:00 +00:00
|
|
|
}
|
|
|
|
|
2020-06-06 14:13:38 +00:00
|
|
|
FMT_CONSTEXPR const Char* on_format_specs(int, const Char* begin,
|
2019-01-13 02:27:38 +00:00
|
|
|
const Char* end) {
|
2019-09-01 16:05:24 +00:00
|
|
|
auto repl = typename part::replacement();
|
2019-11-04 11:37:40 +00:00
|
|
|
dynamic_specs_handler<basic_format_parse_context<Char>> handler(
|
|
|
|
repl.specs, parse_context_);
|
2019-09-01 19:12:19 +00:00
|
|
|
auto it = parse_format_specs(begin, end, handler);
|
|
|
|
if (*it != '}') on_error("missing '}' in format string");
|
2019-09-01 16:05:24 +00:00
|
|
|
repl.arg_id = part_.part_kind == part::kind::arg_index
|
|
|
|
? arg_ref<Char>(part_.val.arg_index)
|
|
|
|
: arg_ref<Char>(part_.val.str);
|
2020-10-28 03:55:44 +00:00
|
|
|
auto replacement_part = part::make_replacement(repl);
|
|
|
|
replacement_part.arg_id_end = begin;
|
|
|
|
handler_(replacement_part);
|
2019-09-01 19:12:19 +00:00
|
|
|
return it;
|
2018-11-27 10:52:00 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-09-01 16:05:24 +00:00
|
|
|
// Compiles a format string and invokes handler(part) for each parsed part.
|
|
|
|
template <bool IS_CONSTEXPR, typename Char, typename PartHandler>
|
|
|
|
FMT_CONSTEXPR void compile_format_string(basic_string_view<Char> format_str,
|
|
|
|
PartHandler handler) {
|
|
|
|
parse_format_string<IS_CONSTEXPR>(
|
|
|
|
format_str,
|
|
|
|
format_string_compiler<Char, PartHandler>(format_str, handler));
|
|
|
|
}
|
|
|
|
|
2020-05-30 04:30:20 +00:00
|
|
|
template <typename OutputIt, typename Context, typename Id>
|
2019-11-04 11:37:40 +00:00
|
|
|
void format_arg(
|
2020-05-30 04:30:20 +00:00
|
|
|
basic_format_parse_context<typename Context::char_type>& parse_ctx,
|
2019-11-04 11:37:40 +00:00
|
|
|
Context& ctx, Id arg_id) {
|
2020-05-29 23:51:45 +00:00
|
|
|
ctx.advance_to(visit_format_arg(
|
2020-06-06 14:13:38 +00:00
|
|
|
arg_formatter<OutputIt, typename Context::char_type>(ctx, &parse_ctx),
|
2020-05-29 23:51:45 +00:00
|
|
|
ctx.arg(arg_id)));
|
2019-09-01 19:12:19 +00:00
|
|
|
}
|
2018-11-27 10:52:00 +00:00
|
|
|
|
2019-09-01 19:12:19 +00:00
|
|
|
// vformat_to is defined in a subnamespace to prevent ADL.
|
|
|
|
namespace cf {
|
2020-05-30 04:30:20 +00:00
|
|
|
template <typename Context, typename OutputIt, typename CompiledFormat>
|
2020-06-06 14:13:38 +00:00
|
|
|
auto vformat_to(OutputIt out, CompiledFormat& cf,
|
|
|
|
basic_format_args<Context> args) -> typename Context::iterator {
|
2019-09-01 19:12:19 +00:00
|
|
|
using char_type = typename Context::char_type;
|
2019-11-04 11:37:40 +00:00
|
|
|
basic_format_parse_context<char_type> parse_ctx(
|
|
|
|
to_string_view(cf.format_str_));
|
2020-05-30 04:30:20 +00:00
|
|
|
Context ctx(out, args);
|
2019-09-01 19:12:19 +00:00
|
|
|
|
2019-09-01 21:57:36 +00:00
|
|
|
const auto& parts = cf.parts();
|
|
|
|
for (auto part_it = std::begin(parts); part_it != std::end(parts);
|
|
|
|
++part_it) {
|
2019-09-01 19:12:19 +00:00
|
|
|
const auto& part = *part_it;
|
|
|
|
const auto& value = part.val;
|
|
|
|
|
|
|
|
using format_part_t = format_part<char_type>;
|
|
|
|
switch (part.part_kind) {
|
|
|
|
case format_part_t::kind::text: {
|
|
|
|
const auto text = value.str;
|
|
|
|
auto output = ctx.out();
|
|
|
|
auto&& it = reserve(output, text.size());
|
|
|
|
it = std::copy_n(text.begin(), text.size(), it);
|
|
|
|
ctx.advance_to(output);
|
2019-09-01 21:57:36 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-09-01 19:12:19 +00:00
|
|
|
|
|
|
|
case format_part_t::kind::arg_index:
|
|
|
|
advance_to(parse_ctx, part.arg_id_end);
|
2020-05-30 04:30:20 +00:00
|
|
|
detail::format_arg<OutputIt>(parse_ctx, ctx, value.arg_index);
|
2019-09-01 19:12:19 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case format_part_t::kind::arg_name:
|
|
|
|
advance_to(parse_ctx, part.arg_id_end);
|
2020-05-30 04:30:20 +00:00
|
|
|
detail::format_arg<OutputIt>(parse_ctx, ctx, value.str);
|
2019-09-01 19:12:19 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case format_part_t::kind::replacement: {
|
|
|
|
const auto& arg_id_value = value.repl.arg_id.val;
|
|
|
|
const auto arg = value.repl.arg_id.kind == arg_id_kind::index
|
|
|
|
? ctx.arg(arg_id_value.index)
|
|
|
|
: ctx.arg(arg_id_value.name);
|
|
|
|
|
|
|
|
auto specs = value.repl.specs;
|
|
|
|
|
|
|
|
handle_dynamic_spec<width_checker>(specs.width, specs.width_ref, ctx);
|
|
|
|
handle_dynamic_spec<precision_checker>(specs.precision,
|
|
|
|
specs.precision_ref, ctx);
|
|
|
|
|
|
|
|
error_handler h;
|
|
|
|
numeric_specs_checker<error_handler> checker(h, arg.type());
|
|
|
|
if (specs.align == align::numeric) checker.require_numeric_argument();
|
|
|
|
if (specs.sign != sign::none) checker.check_sign();
|
|
|
|
if (specs.alt) checker.require_numeric_argument();
|
|
|
|
if (specs.precision >= 0) checker.check_precision();
|
|
|
|
|
|
|
|
advance_to(parse_ctx, part.arg_id_end);
|
2020-06-06 14:13:38 +00:00
|
|
|
ctx.advance_to(
|
|
|
|
visit_format_arg(arg_formatter<OutputIt, typename Context::char_type>(
|
|
|
|
ctx, nullptr, &specs),
|
|
|
|
arg));
|
2019-09-01 21:57:36 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-11-27 10:52:00 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-01 19:12:19 +00:00
|
|
|
return ctx.out();
|
|
|
|
}
|
|
|
|
} // namespace cf
|
2018-11-27 10:52:00 +00:00
|
|
|
|
2019-09-07 02:16:11 +00:00
|
|
|
struct basic_compiled_format {};
|
|
|
|
|
|
|
|
template <typename S, typename = void>
|
|
|
|
struct compiled_format_base : basic_compiled_format {
|
2019-09-01 19:12:19 +00:00
|
|
|
using char_type = char_t<S>;
|
2020-05-10 14:25:42 +00:00
|
|
|
using parts_container = std::vector<detail::format_part<char_type>>;
|
2018-11-27 10:52:00 +00:00
|
|
|
|
2019-09-01 21:57:36 +00:00
|
|
|
parts_container compiled_parts;
|
2018-11-27 10:52:00 +00:00
|
|
|
|
2019-09-01 21:57:36 +00:00
|
|
|
explicit compiled_format_base(basic_string_view<char_type> format_str) {
|
|
|
|
compile_format_string<false>(format_str,
|
|
|
|
[this](const format_part<char_type>& part) {
|
|
|
|
compiled_parts.push_back(part);
|
|
|
|
});
|
|
|
|
}
|
2018-11-27 10:52:00 +00:00
|
|
|
|
2019-09-01 21:57:36 +00:00
|
|
|
const parts_container& parts() const { return compiled_parts; }
|
|
|
|
};
|
2018-11-27 10:52:00 +00:00
|
|
|
|
2019-09-01 21:57:36 +00:00
|
|
|
template <typename Char, unsigned N> struct format_part_array {
|
|
|
|
format_part<Char> data[N] = {};
|
|
|
|
FMT_CONSTEXPR format_part_array() = default;
|
2018-11-27 10:52:00 +00:00
|
|
|
};
|
|
|
|
|
2019-09-01 21:57:36 +00:00
|
|
|
template <typename Char, unsigned N>
|
|
|
|
FMT_CONSTEXPR format_part_array<Char, N> compile_to_parts(
|
|
|
|
basic_string_view<Char> format_str) {
|
|
|
|
format_part_array<Char, N> parts;
|
|
|
|
unsigned counter = 0;
|
2019-09-01 16:05:24 +00:00
|
|
|
// This is not a lambda for compatibility with older compilers.
|
2019-09-01 21:57:36 +00:00
|
|
|
struct {
|
|
|
|
format_part<Char>* parts;
|
|
|
|
unsigned* counter;
|
2019-09-01 16:05:24 +00:00
|
|
|
FMT_CONSTEXPR void operator()(const format_part<Char>& part) {
|
2019-09-01 21:57:36 +00:00
|
|
|
parts[(*counter)++] = part;
|
2019-09-01 16:05:24 +00:00
|
|
|
}
|
2019-09-01 21:57:36 +00:00
|
|
|
} collector{parts.data, &counter};
|
|
|
|
compile_format_string<true>(format_str, collector);
|
|
|
|
if (counter < N) {
|
|
|
|
parts.data[counter] =
|
|
|
|
format_part<Char>::make_text(basic_string_view<Char>());
|
|
|
|
}
|
2018-11-27 10:52:00 +00:00
|
|
|
return parts;
|
|
|
|
}
|
|
|
|
|
2019-09-01 21:57:36 +00:00
|
|
|
template <typename T> constexpr const T& constexpr_max(const T& a, const T& b) {
|
|
|
|
return (a < b) ? b : a;
|
|
|
|
}
|
2018-11-27 10:52:00 +00:00
|
|
|
|
2019-09-01 21:57:36 +00:00
|
|
|
template <typename S>
|
2019-09-07 02:16:11 +00:00
|
|
|
struct compiled_format_base<S, enable_if_t<is_compile_string<S>::value>>
|
|
|
|
: basic_compiled_format {
|
2019-09-01 21:57:36 +00:00
|
|
|
using char_type = char_t<S>;
|
2018-11-27 10:52:00 +00:00
|
|
|
|
2019-09-01 21:57:36 +00:00
|
|
|
FMT_CONSTEXPR explicit compiled_format_base(basic_string_view<char_type>) {}
|
2018-11-27 10:52:00 +00:00
|
|
|
|
2019-09-01 21:57:36 +00:00
|
|
|
// Workaround for old compilers. Format string compilation will not be
|
|
|
|
// performed there anyway.
|
|
|
|
#if FMT_USE_CONSTEXPR
|
|
|
|
static FMT_CONSTEXPR_DECL const unsigned num_format_parts =
|
|
|
|
constexpr_max(count_parts(to_string_view(S())), 1u);
|
|
|
|
#else
|
|
|
|
static const unsigned num_format_parts = 1;
|
|
|
|
#endif
|
2019-08-31 17:23:42 +00:00
|
|
|
|
2019-09-01 21:57:36 +00:00
|
|
|
using parts_container = format_part<char_type>[num_format_parts];
|
2018-11-27 10:52:00 +00:00
|
|
|
|
2019-08-31 17:23:42 +00:00
|
|
|
const parts_container& parts() const {
|
2019-09-01 21:57:36 +00:00
|
|
|
static FMT_CONSTEXPR_DECL const auto compiled_parts =
|
|
|
|
compile_to_parts<char_type, num_format_parts>(
|
2020-05-10 14:25:42 +00:00
|
|
|
detail::to_string_view(S()));
|
2019-09-01 21:57:36 +00:00
|
|
|
return compiled_parts.data;
|
2018-11-27 10:52:00 +00:00
|
|
|
}
|
|
|
|
};
|
2019-09-01 21:57:36 +00:00
|
|
|
|
|
|
|
template <typename S, typename... Args>
|
|
|
|
class compiled_format : private compiled_format_base<S> {
|
|
|
|
public:
|
|
|
|
using typename compiled_format_base<S>::char_type;
|
|
|
|
|
|
|
|
private:
|
|
|
|
basic_string_view<char_type> format_str_;
|
|
|
|
|
2020-05-30 04:30:20 +00:00
|
|
|
template <typename Context, typename OutputIt, typename CompiledFormat>
|
|
|
|
friend auto cf::vformat_to(OutputIt out, CompiledFormat& cf,
|
2019-09-01 21:57:36 +00:00
|
|
|
basic_format_args<Context> args) ->
|
|
|
|
typename Context::iterator;
|
|
|
|
|
|
|
|
public:
|
|
|
|
compiled_format() = delete;
|
|
|
|
explicit constexpr compiled_format(basic_string_view<char_type> format_str)
|
|
|
|
: compiled_format_base<S>(format_str), format_str_(format_str) {}
|
|
|
|
};
|
2019-09-03 00:08:58 +00:00
|
|
|
|
|
|
|
#ifdef __cpp_if_constexpr
|
|
|
|
template <typename... Args> struct type_list {};
|
|
|
|
|
|
|
|
// Returns a reference to the argument at index N from [first, rest...].
|
|
|
|
template <int N, typename T, typename... Args>
|
2020-08-12 13:57:22 +00:00
|
|
|
constexpr const auto& get([[maybe_unused]] const T& first,
|
|
|
|
[[maybe_unused]] const Args&... rest) {
|
2019-09-24 09:53:55 +00:00
|
|
|
static_assert(N < 1 + sizeof...(Args), "index is out of bounds");
|
|
|
|
if constexpr (N == 0)
|
2019-09-03 00:08:58 +00:00
|
|
|
return first;
|
|
|
|
else
|
2019-09-24 09:53:55 +00:00
|
|
|
return get<N - 1>(rest...);
|
2019-09-03 00:08:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <int N, typename> struct get_type_impl;
|
|
|
|
|
|
|
|
template <int N, typename... Args> struct get_type_impl<N, type_list<Args...>> {
|
|
|
|
using type = remove_cvref_t<decltype(get<N>(std::declval<Args>()...))>;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <int N, typename T>
|
|
|
|
using get_type = typename get_type_impl<N, T>::type;
|
|
|
|
|
2020-03-06 15:25:34 +00:00
|
|
|
template <typename T> struct is_compiled_format : std::false_type {};
|
2020-03-05 19:43:33 +00:00
|
|
|
|
2019-09-03 00:08:58 +00:00
|
|
|
template <typename Char> struct text {
|
|
|
|
basic_string_view<Char> data;
|
2019-09-07 02:16:11 +00:00
|
|
|
using char_type = Char;
|
2019-09-03 00:08:58 +00:00
|
|
|
|
|
|
|
template <typename OutputIt, typename... Args>
|
2020-11-29 16:59:11 +00:00
|
|
|
constexpr OutputIt format(OutputIt out, const Args&...) const {
|
2020-06-14 18:04:41 +00:00
|
|
|
return write<Char>(out, data);
|
2019-09-03 00:08:58 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-03-05 19:43:33 +00:00
|
|
|
template <typename Char>
|
2020-03-06 15:25:34 +00:00
|
|
|
struct is_compiled_format<text<Char>> : std::true_type {};
|
2020-03-05 19:43:33 +00:00
|
|
|
|
2019-09-03 00:08:58 +00:00
|
|
|
template <typename Char>
|
|
|
|
constexpr text<Char> make_text(basic_string_view<Char> s, size_t pos,
|
|
|
|
size_t size) {
|
|
|
|
return {{&s[pos], size}};
|
|
|
|
}
|
|
|
|
|
2020-07-11 15:21:14 +00:00
|
|
|
template <typename Char> struct code_unit {
|
|
|
|
Char value;
|
|
|
|
using char_type = Char;
|
|
|
|
|
|
|
|
template <typename OutputIt, typename... Args>
|
2020-11-29 16:59:11 +00:00
|
|
|
constexpr OutputIt format(OutputIt out, const Args&...) const {
|
2020-07-11 15:21:14 +00:00
|
|
|
return write<Char>(out, value);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Char>
|
|
|
|
struct is_compiled_format<code_unit<Char>> : std::true_type {};
|
|
|
|
|
2019-09-03 00:08:58 +00:00
|
|
|
// A replacement field that refers to argument N.
|
|
|
|
template <typename Char, typename T, int N> struct field {
|
2019-09-07 02:16:11 +00:00
|
|
|
using char_type = Char;
|
|
|
|
|
2019-09-03 00:08:58 +00:00
|
|
|
template <typename OutputIt, typename... Args>
|
2020-11-29 16:59:11 +00:00
|
|
|
constexpr OutputIt format(OutputIt out, const Args&... args) const {
|
2021-02-16 04:04:52 +00:00
|
|
|
if constexpr (is_named_arg<typename std::remove_cv<T>::type>::value) {
|
|
|
|
decltype(T::value) arg = get<N>(args...).value;
|
|
|
|
return write<Char>(out, arg);
|
|
|
|
} else {
|
|
|
|
// This ensures that the argument type is convertile to `const T&`.
|
|
|
|
const T& arg = get<N>(args...);
|
|
|
|
return write<Char>(out, arg);
|
|
|
|
}
|
2019-09-03 00:08:58 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-03-05 19:43:33 +00:00
|
|
|
template <typename Char, typename T, int N>
|
2020-03-06 15:25:34 +00:00
|
|
|
struct is_compiled_format<field<Char, T, N>> : std::true_type {};
|
2020-03-05 19:43:33 +00:00
|
|
|
|
2021-02-07 20:43:52 +00:00
|
|
|
// A replacement field that refers to argument with name.
|
|
|
|
template <typename Char> struct runtime_named_field {
|
|
|
|
using char_type = Char;
|
|
|
|
basic_string_view<Char> name;
|
|
|
|
|
|
|
|
template <typename OutputIt, typename T>
|
2021-02-14 22:44:04 +00:00
|
|
|
constexpr static bool try_format_argument(OutputIt& out,
|
2021-02-07 20:43:52 +00:00
|
|
|
basic_string_view<Char> arg_name,
|
|
|
|
const T& arg) {
|
2021-02-14 22:44:04 +00:00
|
|
|
if constexpr (is_named_arg<typename std::remove_cv<T>::type>::value) {
|
2021-02-07 20:43:52 +00:00
|
|
|
if (arg_name == arg.name) {
|
2021-02-14 22:44:04 +00:00
|
|
|
out = write<Char>(out, arg.value);
|
2021-02-07 20:43:52 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2021-02-14 22:44:04 +00:00
|
|
|
return false;
|
2021-02-07 20:43:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename OutputIt, typename... Args>
|
|
|
|
constexpr OutputIt format(OutputIt out, const Args&... args) const {
|
2021-02-14 22:44:04 +00:00
|
|
|
bool found = (try_format_argument(out, name, args) || ...);
|
2021-02-07 20:43:52 +00:00
|
|
|
if (!found) {
|
|
|
|
throw format_error("argument with specified name is not found");
|
|
|
|
}
|
2021-02-14 22:44:04 +00:00
|
|
|
return out;
|
2021-02-07 20:43:52 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Char>
|
|
|
|
struct is_compiled_format<runtime_named_field<Char>> : std::true_type {};
|
|
|
|
|
2020-06-23 19:05:37 +00:00
|
|
|
// A replacement field that refers to argument N and has format specifiers.
|
|
|
|
template <typename Char, typename T, int N> struct spec_field {
|
|
|
|
using char_type = Char;
|
2020-12-25 14:40:03 +00:00
|
|
|
formatter<T, Char> fmt;
|
2020-06-23 19:05:37 +00:00
|
|
|
|
|
|
|
template <typename OutputIt, typename... Args>
|
2020-12-25 14:40:03 +00:00
|
|
|
constexpr OutputIt format(OutputIt out, const Args&... args) const {
|
2020-06-23 19:05:37 +00:00
|
|
|
// This ensures that the argument type is convertile to `const T&`.
|
|
|
|
const T& arg = get<N>(args...);
|
2020-09-20 13:51:11 +00:00
|
|
|
const auto& vargs =
|
|
|
|
make_format_args<basic_format_context<OutputIt, Char>>(args...);
|
2020-08-10 16:13:13 +00:00
|
|
|
basic_format_context<OutputIt, Char> ctx(out, vargs);
|
2020-06-23 19:05:37 +00:00
|
|
|
return fmt.format(arg, ctx);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Char, typename T, int N>
|
|
|
|
struct is_compiled_format<spec_field<Char, T, N>> : std::true_type {};
|
|
|
|
|
2019-09-03 00:08:58 +00:00
|
|
|
template <typename L, typename R> struct concat {
|
|
|
|
L lhs;
|
|
|
|
R rhs;
|
2019-09-07 02:16:11 +00:00
|
|
|
using char_type = typename L::char_type;
|
2019-09-03 00:08:58 +00:00
|
|
|
|
|
|
|
template <typename OutputIt, typename... Args>
|
2020-11-29 16:59:11 +00:00
|
|
|
constexpr OutputIt format(OutputIt out, const Args&... args) const {
|
2019-09-03 00:08:58 +00:00
|
|
|
out = lhs.format(out, args...);
|
|
|
|
return rhs.format(out, args...);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-03-05 19:43:33 +00:00
|
|
|
template <typename L, typename R>
|
2020-03-06 15:25:34 +00:00
|
|
|
struct is_compiled_format<concat<L, R>> : std::true_type {};
|
2020-03-05 19:43:33 +00:00
|
|
|
|
2019-09-03 00:08:58 +00:00
|
|
|
template <typename L, typename R>
|
|
|
|
constexpr concat<L, R> make_concat(L lhs, R rhs) {
|
|
|
|
return {lhs, rhs};
|
|
|
|
}
|
|
|
|
|
2019-09-24 09:54:32 +00:00
|
|
|
struct unknown_format {};
|
2019-09-07 02:16:11 +00:00
|
|
|
|
2019-09-03 00:08:58 +00:00
|
|
|
template <typename Char>
|
|
|
|
constexpr size_t parse_text(basic_string_view<Char> str, size_t pos) {
|
|
|
|
for (size_t size = str.size(); pos != size; ++pos) {
|
|
|
|
if (str[pos] == '{' || str[pos] == '}') break;
|
|
|
|
}
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Args, size_t POS, int ID, typename S>
|
|
|
|
constexpr auto compile_format_string(S format_str);
|
|
|
|
|
|
|
|
template <typename Args, size_t POS, int ID, typename T, typename S>
|
2019-09-07 02:16:11 +00:00
|
|
|
constexpr auto parse_tail(T head, S format_str) {
|
2020-06-12 14:05:49 +00:00
|
|
|
if constexpr (POS !=
|
|
|
|
basic_string_view<typename S::char_type>(format_str).size()) {
|
2019-09-07 02:16:11 +00:00
|
|
|
constexpr auto tail = compile_format_string<Args, POS, ID>(format_str);
|
2019-09-24 09:54:32 +00:00
|
|
|
if constexpr (std::is_same<remove_cvref_t<decltype(tail)>,
|
|
|
|
unknown_format>())
|
2019-09-07 02:16:11 +00:00
|
|
|
return tail;
|
|
|
|
else
|
|
|
|
return make_concat(head, tail);
|
|
|
|
} else {
|
2019-09-03 00:08:58 +00:00
|
|
|
return head;
|
2019-09-07 02:16:11 +00:00
|
|
|
}
|
2019-09-03 00:08:58 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:37 +00:00
|
|
|
template <typename T, typename Char> struct parse_specs_result {
|
|
|
|
formatter<T, Char> fmt;
|
|
|
|
size_t end;
|
2020-08-10 16:13:13 +00:00
|
|
|
int next_arg_id;
|
2020-06-23 19:05:37 +00:00
|
|
|
};
|
|
|
|
|
2021-02-14 22:44:04 +00:00
|
|
|
constexpr int manual_indexing_id = -1;
|
2021-02-07 20:43:52 +00:00
|
|
|
|
2020-06-23 19:05:37 +00:00
|
|
|
template <typename T, typename Char>
|
|
|
|
constexpr parse_specs_result<T, Char> parse_specs(basic_string_view<Char> str,
|
2021-02-07 20:43:52 +00:00
|
|
|
size_t pos, int next_arg_id) {
|
2020-06-23 19:05:37 +00:00
|
|
|
str.remove_prefix(pos);
|
2021-02-07 20:43:52 +00:00
|
|
|
auto ctx = basic_format_parse_context<Char>(str, {}, next_arg_id);
|
2020-06-23 19:05:37 +00:00
|
|
|
auto f = formatter<T, Char>();
|
|
|
|
auto end = f.parse(ctx);
|
:bug: Implicit sign conversion warning in clang in c++17 and 20 modes (#2009)
Problem:
- On Apple clang version 11.0.3 (clang-1103.0.32.62) in C++17 and C++20
mode, clang 11.0.0 in C++17 and C++20 mode, and clang 9.0.1 in C++17
mode, the following error is generated:
In file included from test/compile-test.cc:16:
include/fmt/compile.h:518:25: error: implicit conversion changes signedness: 'long' to 'unsigned long' [-Werror,-Wsign-conversion]
return {f, pos + (end - str.data()) + 1, ctx.next_arg_id()};
~ ~~~~^~~~~~~~~~~~
include/fmt/compile.h:538:31: note: in instantiation of function template specialization
'fmt::v7::detail::parse_specs<int, char>' requested here
constexpr auto result = parse_specs<id_type>(str, POS + 2, ID);
^
include/fmt/compile.h:569:17: note: in instantiation of function template specialization
'fmt::v7::detail::compile_format_string<fmt::v7::detail::type_list<int>, 0, 0, FMT_COMPILE_STRING>' requested here
detail::compile_format_string<detail::type_list<Args...>, 0, 0>(
^
include/fmt/compile.h:648:37: note: in instantiation of function template specialization
'fmt::v7::detail::compile<int, FMT_COMPILE_STRING, 0>' requested here
constexpr auto compiled = detail::compile<Args...>(S());
^
test/compile-test.cc:140:24: note: in instantiation of function template specialization
'fmt::v7::format<FMT_COMPILE_STRING, int, 0>' requested here
EXPECT_EQ("42", fmt::format(FMT_COMPILE("{:x}"), 0x42));
^
In file included from test/compile-test.cc:16:
include/fmt/compile.h:518:25: error: implicit conversion changes signedness: 'long' to 'unsigned long' [-Werror,-Wsign-conversion]
return {f, pos + (end - str.data()) + 1, ctx.next_arg_id()};
~ ~~~~^~~~~~~~~~~~
include/fmt/compile.h:538:31: note: in instantiation of function template specialization
'fmt::v7::detail::parse_specs<char [4], char>' requested here
constexpr auto result = parse_specs<id_type>(str, POS + 2, ID);
^
include/fmt/compile.h:494:27: note: in instantiation of function template specialization
'fmt::v7::detail::compile_format_string<fmt::v7::detail::type_list<int, int, char const (&)[4], int>, 5, 2, FMT_COMPILE_STRING>'
requested here
constexpr auto tail = compile_format_string<Args, POS, ID>(format_str);
^
include/fmt/compile.h:539:14: note: in instantiation of function template specialization
'fmt::v7::detail::parse_tail<fmt::v7::detail::type_list<int, int, char const (&)[4], int>, 5, 2, fmt::v7::detail::spec_field<char, int, 0>, FMT_COMPILE_STRING>' requested here
return parse_tail<Args, result.end, result.next_arg_id>(
^
include/fmt/compile.h:569:17: note: in instantiation of function template specialization
'fmt::v7::detail::compile_format_string<fmt::v7::detail::type_list<int, int, char const (&)[4], int>, 0, 0, FMT_COMPILE_STRING>'
requested here
detail::compile_format_string<detail::type_list<Args...>, 0, 0>(
^
include/fmt/compile.h:648:37: note: in instantiation of function template specialization
'fmt::v7::detail::compile<int, int, char const (&)[4], int, FMT_COMPILE_STRING, 0>' requested here
constexpr auto compiled = detail::compile<Args...>(S());
^
test/compile-test.cc:145:18: note: in instantiation of function template specialization
'fmt::v7::format<FMT_COMPILE_STRING, int, int, char const (&)[4], int, 0>' requested here
fmt::format(FMT_COMPILE("{:{}}{:{}}"), 42, 4, "foo", 5));
^
2 errors generated.
Solution:
- Explicitly cast the result of the subtraction to the (unsigned) outer
type.
Co-authored-by: Jonathan Gopel <jgopel@quantlab.com>
2020-11-11 15:57:52 +00:00
|
|
|
return {f, pos + fmt::detail::to_unsigned(end - str.data()) + 1,
|
2021-02-14 22:44:04 +00:00
|
|
|
next_arg_id == 0 ? manual_indexing_id : ctx.next_arg_id()};
|
2021-02-07 20:43:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Char> struct arg_id_handler {
|
|
|
|
constexpr void on_error(const char* message) { throw format_error(message); }
|
|
|
|
|
|
|
|
constexpr int on_arg_id() {
|
|
|
|
throw format_error("handler cannot be used for empty arg_id");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr int on_arg_id(int id) {
|
|
|
|
arg_id = arg_ref<Char>(id);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr int on_arg_id(basic_string_view<Char> id) {
|
|
|
|
arg_id = arg_ref<Char>(id);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
arg_ref<Char> arg_id;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Char> struct parse_arg_id_result {
|
|
|
|
arg_ref<Char> arg_id;
|
|
|
|
const Char* arg_id_end;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <int ID, typename Char>
|
|
|
|
constexpr auto parse_arg_id(const Char* begin, const Char* end) {
|
|
|
|
auto handler = arg_id_handler<Char>{arg_ref<Char>{}};
|
|
|
|
auto adapter = id_adapter<arg_id_handler<Char>, Char>{handler, 0};
|
|
|
|
auto arg_id_end = parse_arg_id(begin, end, adapter);
|
|
|
|
return parse_arg_id_result<Char>{handler.arg_id, arg_id_end};
|
2020-06-23 19:05:37 +00:00
|
|
|
}
|
|
|
|
|
2019-09-07 02:16:11 +00:00
|
|
|
// Compiles a non-empty format string and returns the compiled representation
|
2019-09-24 09:54:32 +00:00
|
|
|
// or unknown_format() on unrecognized input.
|
2019-09-03 00:08:58 +00:00
|
|
|
template <typename Args, size_t POS, int ID, typename S>
|
|
|
|
constexpr auto compile_format_string(S format_str) {
|
2019-09-07 02:16:11 +00:00
|
|
|
using char_type = typename S::char_type;
|
2019-09-03 00:08:58 +00:00
|
|
|
constexpr basic_string_view<char_type> str = format_str;
|
2019-09-07 02:16:11 +00:00
|
|
|
if constexpr (str[POS] == '{') {
|
2021-01-28 14:58:49 +00:00
|
|
|
if constexpr (POS + 1 == str.size())
|
2019-09-03 00:08:58 +00:00
|
|
|
throw format_error("unmatched '{' in format string");
|
|
|
|
if constexpr (str[POS + 1] == '{') {
|
|
|
|
return parse_tail<Args, POS + 2, ID>(make_text(str, POS, 1), format_str);
|
2021-02-07 20:43:52 +00:00
|
|
|
} else if constexpr (str[POS + 1] == '}' || str[POS + 1] == ':') {
|
2021-02-14 22:44:04 +00:00
|
|
|
static_assert(ID != manual_indexing_id,
|
2021-02-07 20:43:52 +00:00
|
|
|
"cannot switch from manual to automatic argument indexing");
|
2020-10-28 03:55:44 +00:00
|
|
|
using id_type = get_type<ID, Args>;
|
2021-02-07 20:43:52 +00:00
|
|
|
if constexpr (str[POS + 1] == '}') {
|
|
|
|
constexpr auto next_id =
|
2021-02-14 22:44:04 +00:00
|
|
|
ID != manual_indexing_id ? ID + 1 : manual_indexing_id;
|
2021-02-07 20:43:52 +00:00
|
|
|
return parse_tail<Args, POS + 2, next_id>(
|
|
|
|
field<char_type, id_type, ID>(), format_str);
|
|
|
|
} else {
|
|
|
|
constexpr auto result = parse_specs<id_type>(str, POS + 2, ID + 1);
|
|
|
|
return parse_tail<Args, result.end, result.next_arg_id>(
|
|
|
|
spec_field<char_type, id_type, ID>{result.fmt}, format_str);
|
|
|
|
}
|
2019-09-03 00:08:58 +00:00
|
|
|
} else {
|
2021-02-07 20:43:52 +00:00
|
|
|
constexpr auto arg_id_result =
|
|
|
|
parse_arg_id<ID>(str.data() + POS + 1, str.data() + str.size());
|
|
|
|
constexpr auto arg_id_end_pos = arg_id_result.arg_id_end - str.data();
|
|
|
|
constexpr char_type c =
|
|
|
|
arg_id_end_pos != str.size() ? str[arg_id_end_pos] : char_type();
|
|
|
|
static_assert(c == '}' || c == ':', "missing '}' in format string");
|
|
|
|
if constexpr (arg_id_result.arg_id.kind == arg_id_kind::index) {
|
|
|
|
static_assert(
|
2021-02-14 22:44:04 +00:00
|
|
|
ID == manual_indexing_id || ID == 0,
|
2021-02-07 20:43:52 +00:00
|
|
|
"cannot switch from automatic to manual argument indexing");
|
|
|
|
constexpr auto arg_index = arg_id_result.arg_id.val.index;
|
|
|
|
using id_type = get_type<arg_index, Args>;
|
|
|
|
if constexpr (c == '}') {
|
2021-02-14 22:44:04 +00:00
|
|
|
return parse_tail<Args, arg_id_end_pos + 1, manual_indexing_id>(
|
2021-02-07 20:43:52 +00:00
|
|
|
field<char_type, id_type, arg_index>(), format_str);
|
|
|
|
} else if constexpr (c == ':') {
|
|
|
|
constexpr auto result =
|
|
|
|
parse_specs<id_type>(str, arg_id_end_pos + 1, 0);
|
|
|
|
return parse_tail<Args, result.end, result.next_arg_id>(
|
|
|
|
spec_field<char_type, id_type, arg_index>{result.fmt},
|
|
|
|
format_str);
|
|
|
|
}
|
|
|
|
} else if constexpr (arg_id_result.arg_id.kind == arg_id_kind::name) {
|
|
|
|
if constexpr (c == '}') {
|
2021-02-16 04:16:57 +00:00
|
|
|
return parse_tail<Args, arg_id_end_pos + 1, ID>(
|
2021-02-07 20:43:52 +00:00
|
|
|
runtime_named_field<char_type>{arg_id_result.arg_id.val.name},
|
|
|
|
format_str);
|
|
|
|
} else if constexpr (c == ':') {
|
|
|
|
return unknown_format(); // no type info for specs parsing
|
|
|
|
}
|
|
|
|
}
|
2019-09-03 00:08:58 +00:00
|
|
|
}
|
|
|
|
} else if constexpr (str[POS] == '}') {
|
2021-01-28 14:58:49 +00:00
|
|
|
if constexpr (POS + 1 == str.size())
|
2019-09-03 00:08:58 +00:00
|
|
|
throw format_error("unmatched '}' in format string");
|
|
|
|
return parse_tail<Args, POS + 2, ID>(make_text(str, POS, 1), format_str);
|
|
|
|
} else {
|
|
|
|
constexpr auto end = parse_text(str, POS + 1);
|
2020-07-11 15:21:14 +00:00
|
|
|
if constexpr (end - POS > 1) {
|
|
|
|
return parse_tail<Args, end, ID>(make_text(str, POS, end - POS),
|
|
|
|
format_str);
|
|
|
|
} else {
|
2020-07-11 15:35:26 +00:00
|
|
|
return parse_tail<Args, end, ID>(code_unit<char_type>{str[POS]},
|
2020-07-11 15:21:14 +00:00
|
|
|
format_str);
|
|
|
|
}
|
2019-09-03 00:08:58 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-27 10:52:00 +00:00
|
|
|
|
2019-08-10 20:18:11 +00:00
|
|
|
template <typename... Args, typename S,
|
2020-06-12 14:05:49 +00:00
|
|
|
FMT_ENABLE_IF(is_compile_string<S>::value ||
|
|
|
|
detail::is_compiled_string<S>::value)>
|
2019-09-07 02:16:11 +00:00
|
|
|
constexpr auto compile(S format_str) {
|
|
|
|
constexpr basic_string_view<typename S::char_type> str = format_str;
|
|
|
|
if constexpr (str.size() == 0) {
|
2020-05-10 14:25:42 +00:00
|
|
|
return detail::make_text(str, 0, 0);
|
2019-09-07 02:16:11 +00:00
|
|
|
} else {
|
|
|
|
constexpr auto result =
|
2020-05-10 14:25:42 +00:00
|
|
|
detail::compile_format_string<detail::type_list<Args...>, 0, 0>(
|
2019-09-07 02:16:11 +00:00
|
|
|
format_str);
|
|
|
|
if constexpr (std::is_same<remove_cvref_t<decltype(result)>,
|
2020-05-10 14:25:42 +00:00
|
|
|
detail::unknown_format>()) {
|
|
|
|
return detail::compiled_format<S, Args...>(to_string_view(format_str));
|
2019-09-07 02:16:11 +00:00
|
|
|
} else {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-23 21:03:37 +00:00
|
|
|
#else
|
|
|
|
template <typename... Args, typename S,
|
|
|
|
FMT_ENABLE_IF(is_compile_string<S>::value)>
|
|
|
|
constexpr auto compile(S format_str) -> detail::compiled_format<S, Args...> {
|
|
|
|
return detail::compiled_format<S, Args...>(to_string_view(format_str));
|
|
|
|
}
|
|
|
|
#endif // __cpp_if_constexpr
|
|
|
|
|
|
|
|
// Compiles the format string which must be a string literal.
|
|
|
|
template <typename... Args, typename Char, size_t N>
|
|
|
|
auto compile(const Char (&format_str)[N])
|
|
|
|
-> detail::compiled_format<const Char*, Args...> {
|
|
|
|
return detail::compiled_format<const Char*, Args...>(
|
|
|
|
basic_string_view<Char>(format_str, N - 1));
|
|
|
|
}
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
// DEPRECATED! use FMT_COMPILE instead.
|
|
|
|
template <typename... Args>
|
2020-06-25 14:14:55 +00:00
|
|
|
FMT_DEPRECATED auto compile(const Args&... args)
|
|
|
|
-> decltype(detail::compile(args...)) {
|
2020-06-23 21:03:37 +00:00
|
|
|
return detail::compile(args...);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if FMT_USE_CONSTEXPR
|
|
|
|
# ifdef __cpp_if_constexpr
|
2019-09-07 02:16:11 +00:00
|
|
|
|
2020-03-06 15:25:34 +00:00
|
|
|
template <typename CompiledFormat, typename... Args,
|
|
|
|
typename Char = typename CompiledFormat::char_type,
|
2020-05-10 14:25:42 +00:00
|
|
|
FMT_ENABLE_IF(detail::is_compiled_format<CompiledFormat>::value)>
|
2020-06-14 18:38:39 +00:00
|
|
|
FMT_INLINE std::basic_string<Char> format(const CompiledFormat& cf,
|
|
|
|
const Args&... args) {
|
2019-09-07 02:16:11 +00:00
|
|
|
basic_memory_buffer<Char> buffer;
|
2020-07-10 14:50:37 +00:00
|
|
|
cf.format(detail::buffer_appender<Char>(buffer), args...);
|
2019-09-07 02:16:11 +00:00
|
|
|
return to_string(buffer);
|
|
|
|
}
|
|
|
|
|
2020-03-06 15:25:34 +00:00
|
|
|
template <typename OutputIt, typename CompiledFormat, typename... Args,
|
2020-05-10 14:25:42 +00:00
|
|
|
FMT_ENABLE_IF(detail::is_compiled_format<CompiledFormat>::value)>
|
2020-11-29 16:59:11 +00:00
|
|
|
constexpr OutputIt format_to(OutputIt out, const CompiledFormat& cf,
|
|
|
|
const Args&... args) {
|
2019-09-07 02:16:11 +00:00
|
|
|
return cf.format(out, args...);
|
|
|
|
}
|
|
|
|
# endif // __cpp_if_constexpr
|
|
|
|
#endif // FMT_USE_CONSTEXPR
|
2018-11-27 10:52:00 +00:00
|
|
|
|
2019-08-03 13:28:31 +00:00
|
|
|
template <typename CompiledFormat, typename... Args,
|
2019-09-07 02:16:11 +00:00
|
|
|
typename Char = typename CompiledFormat::char_type,
|
2020-05-10 14:25:42 +00:00
|
|
|
FMT_ENABLE_IF(std::is_base_of<detail::basic_compiled_format,
|
2019-09-07 02:16:11 +00:00
|
|
|
CompiledFormat>::value)>
|
2019-08-03 13:28:31 +00:00
|
|
|
std::basic_string<Char> format(const CompiledFormat& cf, const Args&... args) {
|
|
|
|
basic_memory_buffer<Char> buffer;
|
|
|
|
using context = buffer_context<Char>;
|
2020-07-09 15:41:05 +00:00
|
|
|
detail::cf::vformat_to<context>(detail::buffer_appender<Char>(buffer), cf,
|
2020-05-10 14:25:42 +00:00
|
|
|
make_format_args<context>(args...));
|
2019-08-03 13:28:31 +00:00
|
|
|
return to_string(buffer);
|
|
|
|
}
|
|
|
|
|
2020-06-12 14:05:49 +00:00
|
|
|
template <typename S, typename... Args,
|
|
|
|
FMT_ENABLE_IF(detail::is_compiled_string<S>::value)>
|
2020-06-20 15:50:02 +00:00
|
|
|
FMT_INLINE std::basic_string<typename S::char_type> format(const S&,
|
|
|
|
Args&&... args) {
|
2020-09-17 13:35:33 +00:00
|
|
|
#ifdef __cpp_if_constexpr
|
|
|
|
if constexpr (std::is_same<typename S::char_type, char>::value) {
|
|
|
|
constexpr basic_string_view<typename S::char_type> str = S();
|
2021-02-16 04:04:52 +00:00
|
|
|
if constexpr (str.size() == 2 && str[0] == '{' && str[1] == '}') {
|
|
|
|
auto first = detail::first(args...);
|
|
|
|
if constexpr (detail::is_named_arg<typename std::remove_cv<
|
|
|
|
decltype(first)>::type>::value) {
|
|
|
|
return fmt::to_string(first.value);
|
|
|
|
} else {
|
|
|
|
return fmt::to_string(first);
|
|
|
|
}
|
|
|
|
}
|
2020-09-17 13:35:33 +00:00
|
|
|
}
|
|
|
|
#endif
|
2020-06-23 21:03:37 +00:00
|
|
|
constexpr auto compiled = detail::compile<Args...>(S());
|
2020-06-12 20:24:49 +00:00
|
|
|
return format(compiled, std::forward<Args>(args)...);
|
2020-06-12 14:05:49 +00:00
|
|
|
}
|
|
|
|
|
2019-09-07 02:16:11 +00:00
|
|
|
template <typename OutputIt, typename CompiledFormat, typename... Args,
|
2020-05-10 14:25:42 +00:00
|
|
|
FMT_ENABLE_IF(std::is_base_of<detail::basic_compiled_format,
|
2019-09-07 02:16:11 +00:00
|
|
|
CompiledFormat>::value)>
|
2020-11-29 16:59:11 +00:00
|
|
|
constexpr OutputIt format_to(OutputIt out, const CompiledFormat& cf,
|
|
|
|
const Args&... args) {
|
2019-08-03 15:35:02 +00:00
|
|
|
using char_type = typename CompiledFormat::char_type;
|
|
|
|
using context = format_context_t<OutputIt, char_type>;
|
2020-05-30 04:30:20 +00:00
|
|
|
return detail::cf::vformat_to<context>(out, cf,
|
2020-05-10 14:25:42 +00:00
|
|
|
make_format_args<context>(args...));
|
2019-08-03 15:35:02 +00:00
|
|
|
}
|
|
|
|
|
2020-06-20 15:50:02 +00:00
|
|
|
template <typename OutputIt, typename S, typename... Args,
|
|
|
|
FMT_ENABLE_IF(detail::is_compiled_string<S>::value)>
|
2020-11-29 16:59:11 +00:00
|
|
|
FMT_CONSTEXPR OutputIt format_to(OutputIt out, const S&, const Args&... args) {
|
2020-06-23 21:03:37 +00:00
|
|
|
constexpr auto compiled = detail::compile<Args...>(S());
|
2020-06-20 15:50:02 +00:00
|
|
|
return format_to(out, compiled, args...);
|
|
|
|
}
|
|
|
|
|
2020-10-27 08:55:26 +00:00
|
|
|
template <typename OutputIt, typename CompiledFormat, typename... Args>
|
2020-10-27 14:19:28 +00:00
|
|
|
auto format_to_n(OutputIt out, size_t n, const CompiledFormat& cf,
|
|
|
|
const Args&... args) ->
|
|
|
|
typename std::enable_if<
|
|
|
|
detail::is_output_iterator<OutputIt,
|
|
|
|
typename CompiledFormat::char_type>::value &&
|
|
|
|
std::is_base_of<detail::basic_compiled_format,
|
|
|
|
CompiledFormat>::value,
|
|
|
|
format_to_n_result<OutputIt>>::type {
|
2019-08-03 14:54:49 +00:00
|
|
|
auto it =
|
2020-05-10 14:25:42 +00:00
|
|
|
format_to(detail::truncating_iterator<OutputIt>(out, n), cf, args...);
|
2019-08-03 14:54:49 +00:00
|
|
|
return {it.base(), it.count()};
|
|
|
|
}
|
|
|
|
|
2020-09-15 14:28:06 +00:00
|
|
|
template <typename OutputIt, typename S, typename... Args,
|
|
|
|
FMT_ENABLE_IF(detail::is_compiled_string<S>::value)>
|
|
|
|
format_to_n_result<OutputIt> format_to_n(OutputIt out, size_t n, const S&,
|
|
|
|
const Args&... args) {
|
|
|
|
constexpr auto compiled = detail::compile<Args...>(S());
|
|
|
|
auto it = format_to(detail::truncating_iterator<OutputIt>(out, n), compiled,
|
|
|
|
args...);
|
|
|
|
return {it.base(), it.count()};
|
|
|
|
}
|
|
|
|
|
2019-08-03 14:54:49 +00:00
|
|
|
template <typename CompiledFormat, typename... Args>
|
2020-05-07 22:59:46 +00:00
|
|
|
size_t formatted_size(const CompiledFormat& cf, const Args&... args) {
|
2020-05-10 14:25:42 +00:00
|
|
|
return format_to(detail::counting_iterator(), cf, args...).count();
|
2019-08-03 14:54:49 +00:00
|
|
|
}
|
|
|
|
|
2020-12-07 23:53:11 +00:00
|
|
|
#if FMT_USE_NONTYPE_TEMPLATE_PARAMETERS
|
|
|
|
inline namespace literals {
|
|
|
|
template <detail::fixed_string Str>
|
|
|
|
constexpr detail::udl_compiled_string<remove_cvref_t<decltype(Str.data[0])>,
|
|
|
|
sizeof(Str.data), Str>
|
|
|
|
operator""_cf() {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
} // namespace literals
|
|
|
|
#endif
|
|
|
|
|
2018-11-27 10:52:00 +00:00
|
|
|
FMT_END_NAMESPACE
|
|
|
|
|
2019-07-25 16:01:21 +00:00
|
|
|
#endif // FMT_COMPILE_H_
|