|
|
|
@ -14,433 +14,557 @@
|
|
|
|
|
FMT_BEGIN_NAMESPACE
|
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
|
|
// Part of a compiled format string. It can be either literal text or a
|
|
|
|
|
// replacement field.
|
|
|
|
|
template <typename Char> struct format_part {
|
|
|
|
|
public:
|
|
|
|
|
struct named_argument_id {
|
|
|
|
|
FMT_CONSTEXPR named_argument_id(internal::string_view_metadata id)
|
|
|
|
|
: id(id) {}
|
|
|
|
|
internal::string_view_metadata id;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct argument_id {
|
|
|
|
|
FMT_CONSTEXPR argument_id() : argument_id(0u) {}
|
|
|
|
|
|
|
|
|
|
FMT_CONSTEXPR argument_id(unsigned id)
|
|
|
|
|
: which(which_arg_id::index), val(id) {}
|
|
|
|
|
|
|
|
|
|
FMT_CONSTEXPR argument_id(internal::string_view_metadata id)
|
|
|
|
|
: which(which_arg_id::named_index), val(id) {}
|
|
|
|
|
enum class kind { arg_index, arg_name, text, replacement };
|
|
|
|
|
|
|
|
|
|
enum class which_arg_id { index, named_index };
|
|
|
|
|
|
|
|
|
|
which_arg_id which;
|
|
|
|
|
|
|
|
|
|
union value {
|
|
|
|
|
FMT_CONSTEXPR value() : index(0u) {}
|
|
|
|
|
FMT_CONSTEXPR value(unsigned id) : index(id) {}
|
|
|
|
|
FMT_CONSTEXPR value(internal::string_view_metadata id)
|
|
|
|
|
: named_index(id) {}
|
|
|
|
|
|
|
|
|
|
unsigned index;
|
|
|
|
|
internal::string_view_metadata named_index;
|
|
|
|
|
} val;
|
|
|
|
|
struct replacement {
|
|
|
|
|
arg_ref<Char> arg_id;
|
|
|
|
|
dynamic_format_specs<Char> specs;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct specification {
|
|
|
|
|
FMT_CONSTEXPR specification() : arg_id(0u) {}
|
|
|
|
|
FMT_CONSTEXPR specification(unsigned id) : arg_id(id) {}
|
|
|
|
|
kind part_kind;
|
|
|
|
|
union value {
|
|
|
|
|
unsigned arg_index;
|
|
|
|
|
basic_string_view<Char> str;
|
|
|
|
|
replacement repl;
|
|
|
|
|
|
|
|
|
|
FMT_CONSTEXPR specification(internal::string_view_metadata id)
|
|
|
|
|
: arg_id(id) {}
|
|
|
|
|
FMT_CONSTEXPR value(unsigned index = 0) : arg_index(index) {}
|
|
|
|
|
FMT_CONSTEXPR value(basic_string_view<Char> s) : str(s) {}
|
|
|
|
|
FMT_CONSTEXPR value(replacement r) : repl(r) {}
|
|
|
|
|
} val;
|
|
|
|
|
// Position past the end of the argument id.
|
|
|
|
|
const Char* arg_id_end = nullptr;
|
|
|
|
|
|
|
|
|
|
argument_id arg_id;
|
|
|
|
|
internal::dynamic_format_specs<Char> parsed_specs;
|
|
|
|
|
};
|
|
|
|
|
FMT_CONSTEXPR format_part(kind k = kind::arg_index, value v = {})
|
|
|
|
|
: part_kind(k), val(v) {}
|
|
|
|
|
|
|
|
|
|
FMT_CONSTEXPR format_part()
|
|
|
|
|
: which(kind::argument_id), end_of_argument_id(0u), val(0u) {}
|
|
|
|
|
static FMT_CONSTEXPR format_part make_arg_index(unsigned index) {
|
|
|
|
|
return format_part(kind::arg_index, index);
|
|
|
|
|
}
|
|
|
|
|
static FMT_CONSTEXPR format_part make_arg_name(basic_string_view<Char> name) {
|
|
|
|
|
return format_part(kind::arg_name, name);
|
|
|
|
|
}
|
|
|
|
|
static FMT_CONSTEXPR format_part make_text(basic_string_view<Char> text) {
|
|
|
|
|
return format_part(kind::text, text);
|
|
|
|
|
}
|
|
|
|
|
static FMT_CONSTEXPR format_part make_replacement(replacement repl) {
|
|
|
|
|
return format_part(kind::replacement, repl);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
FMT_CONSTEXPR format_part(internal::string_view_metadata text)
|
|
|
|
|
: which(kind::text), end_of_argument_id(0u), val(text) {}
|
|
|
|
|
template <typename Char> struct part_counter {
|
|
|
|
|
unsigned num_parts = 0;
|
|
|
|
|
|
|
|
|
|
FMT_CONSTEXPR format_part(unsigned id)
|
|
|
|
|
: which(kind::argument_id), end_of_argument_id(0u), val(id) {}
|
|
|
|
|
FMT_CONSTEXPR void on_text(const Char* begin, const Char* end) {
|
|
|
|
|
if (begin != end) ++num_parts;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FMT_CONSTEXPR format_part(named_argument_id arg_id)
|
|
|
|
|
: which(kind::named_argument_id), end_of_argument_id(0u), val(arg_id) {}
|
|
|
|
|
FMT_CONSTEXPR void on_arg_id() { ++num_parts; }
|
|
|
|
|
FMT_CONSTEXPR void on_arg_id(unsigned) { ++num_parts; }
|
|
|
|
|
FMT_CONSTEXPR void on_arg_id(basic_string_view<Char>) { ++num_parts; }
|
|
|
|
|
|
|
|
|
|
FMT_CONSTEXPR format_part(specification spec)
|
|
|
|
|
: which(kind::specification), end_of_argument_id(0u), val(spec) {}
|
|
|
|
|
FMT_CONSTEXPR void on_replacement_field(const Char*) {}
|
|
|
|
|
|
|
|
|
|
enum class kind { argument_id, named_argument_id, text, specification };
|
|
|
|
|
FMT_CONSTEXPR const Char* on_format_specs(const Char* begin,
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
kind which;
|
|
|
|
|
std::size_t end_of_argument_id;
|
|
|
|
|
union value {
|
|
|
|
|
FMT_CONSTEXPR value() : arg_id(0u) {}
|
|
|
|
|
FMT_CONSTEXPR value(unsigned id) : arg_id(id) {}
|
|
|
|
|
FMT_CONSTEXPR value(named_argument_id named_id)
|
|
|
|
|
: named_arg_id(named_id.id) {}
|
|
|
|
|
FMT_CONSTEXPR value(internal::string_view_metadata t) : text(t) {}
|
|
|
|
|
FMT_CONSTEXPR value(specification s) : spec(s) {}
|
|
|
|
|
unsigned arg_id;
|
|
|
|
|
internal::string_view_metadata named_arg_id;
|
|
|
|
|
internal::string_view_metadata text;
|
|
|
|
|
specification spec;
|
|
|
|
|
} val;
|
|
|
|
|
FMT_CONSTEXPR void on_error(const char*) {}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <typename Char, typename PartsContainer>
|
|
|
|
|
class format_preparation_handler : public internal::error_handler {
|
|
|
|
|
// 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 {
|
|
|
|
|
private:
|
|
|
|
|
using part = format_part<Char>;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
using iterator = typename basic_string_view<Char>::iterator;
|
|
|
|
|
PartHandler handler_;
|
|
|
|
|
part part_;
|
|
|
|
|
basic_string_view<Char> format_str_;
|
|
|
|
|
basic_format_parse_context<Char> parse_context_;
|
|
|
|
|
|
|
|
|
|
FMT_CONSTEXPR format_preparation_handler(basic_string_view<Char> format,
|
|
|
|
|
PartsContainer& parts)
|
|
|
|
|
: parts_(parts), format_(format), parse_context_(format) {}
|
|
|
|
|
public:
|
|
|
|
|
FMT_CONSTEXPR format_string_compiler(basic_string_view<Char> format_str,
|
|
|
|
|
PartHandler handler)
|
|
|
|
|
: handler_(handler),
|
|
|
|
|
format_str_(format_str),
|
|
|
|
|
parse_context_(format_str) {}
|
|
|
|
|
|
|
|
|
|
FMT_CONSTEXPR void on_text(const Char* begin, const Char* end) {
|
|
|
|
|
if (begin == end) return;
|
|
|
|
|
const auto offset = begin - format_.data();
|
|
|
|
|
const auto size = end - begin;
|
|
|
|
|
parts_.push_back(part(string_view_metadata(offset, size)));
|
|
|
|
|
if (begin != end)
|
|
|
|
|
handler_(part::make_text({begin, to_unsigned(end - begin)}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FMT_CONSTEXPR void on_arg_id() {
|
|
|
|
|
parts_.push_back(part(parse_context_.next_arg_id()));
|
|
|
|
|
part_ = part::make_arg_index(parse_context_.next_arg_id());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FMT_CONSTEXPR void on_arg_id(unsigned id) {
|
|
|
|
|
parse_context_.check_arg_id(id);
|
|
|
|
|
parts_.push_back(part(id));
|
|
|
|
|
part_ = part::make_arg_index(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FMT_CONSTEXPR void on_arg_id(basic_string_view<Char> id) {
|
|
|
|
|
const auto view = string_view_metadata(format_, id);
|
|
|
|
|
const auto arg_id = typename part::named_argument_id(view);
|
|
|
|
|
parts_.push_back(part(arg_id));
|
|
|
|
|
part_ = part::make_arg_name(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FMT_CONSTEXPR void on_replacement_field(const Char* ptr) {
|
|
|
|
|
parts_.back().end_of_argument_id = ptr - format_.begin();
|
|
|
|
|
part_.arg_id_end = ptr;
|
|
|
|
|
handler_(part_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FMT_CONSTEXPR const Char* on_format_specs(const Char* begin,
|
|
|
|
|
const Char* end) {
|
|
|
|
|
const auto specs_offset = to_unsigned(begin - format_.begin());
|
|
|
|
|
|
|
|
|
|
using parse_context = basic_parse_context<Char>;
|
|
|
|
|
internal::dynamic_format_specs<Char> parsed_specs;
|
|
|
|
|
dynamic_specs_handler<parse_context> handler(parsed_specs, parse_context_);
|
|
|
|
|
begin = parse_format_specs(begin, end, handler);
|
|
|
|
|
|
|
|
|
|
if (*begin != '}') on_error("missing '}' in format string");
|
|
|
|
|
|
|
|
|
|
auto& last_part = parts_.back();
|
|
|
|
|
auto specs = last_part.which == part::kind::argument_id
|
|
|
|
|
? typename part::specification(last_part.val.arg_id)
|
|
|
|
|
: typename part::specification(last_part.val.named_arg_id);
|
|
|
|
|
specs.parsed_specs = parsed_specs;
|
|
|
|
|
last_part = part(specs);
|
|
|
|
|
last_part.end_of_argument_id = specs_offset;
|
|
|
|
|
return begin;
|
|
|
|
|
auto repl = typename part::replacement();
|
|
|
|
|
dynamic_specs_handler<basic_format_parse_context<Char>> handler(
|
|
|
|
|
repl.specs, parse_context_);
|
|
|
|
|
auto it = parse_format_specs(begin, end, handler);
|
|
|
|
|
if (*it != '}') on_error("missing '}' in format string");
|
|
|
|
|
repl.arg_id = part_.part_kind == part::kind::arg_index
|
|
|
|
|
? arg_ref<Char>(part_.val.arg_index)
|
|
|
|
|
: arg_ref<Char>(part_.val.str);
|
|
|
|
|
auto part = part::make_replacement(repl);
|
|
|
|
|
part.arg_id_end = begin;
|
|
|
|
|
handler_(part);
|
|
|
|
|
return it;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
PartsContainer& parts_;
|
|
|
|
|
basic_string_view<Char> format_;
|
|
|
|
|
basic_parse_context<Char> parse_context_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <typename Format, typename PreparedPartsProvider, typename... Args>
|
|
|
|
|
class prepared_format {
|
|
|
|
|
public:
|
|
|
|
|
using char_type = char_t<Format>;
|
|
|
|
|
using format_part_t = format_part<char_type>;
|
|
|
|
|
|
|
|
|
|
constexpr prepared_format(Format f)
|
|
|
|
|
: format_(std::move(f)), parts_provider_(to_string_view(format_)) {}
|
|
|
|
|
|
|
|
|
|
prepared_format() = delete;
|
|
|
|
|
|
|
|
|
|
using context = buffer_context<char_type>;
|
|
|
|
|
|
|
|
|
|
template <typename Range, typename Context>
|
|
|
|
|
auto vformat_to(Range out, basic_format_args<Context> args) const ->
|
|
|
|
|
typename Context::iterator {
|
|
|
|
|
const auto format_view = internal::to_string_view(format_);
|
|
|
|
|
basic_parse_context<char_type> parse_ctx(format_view);
|
|
|
|
|
Context ctx(out.begin(), args);
|
|
|
|
|
|
|
|
|
|
const auto& parts = parts_provider_.parts();
|
|
|
|
|
for (auto part_it = parts.begin(); part_it != parts.end(); ++part_it) {
|
|
|
|
|
const auto& part = *part_it;
|
|
|
|
|
const auto& value = part.val;
|
|
|
|
|
|
|
|
|
|
switch (part.which) {
|
|
|
|
|
case format_part_t::kind::text: {
|
|
|
|
|
const auto text = value.text.to_view(format_view.data());
|
|
|
|
|
auto output = ctx.out();
|
|
|
|
|
auto&& it = internal::reserve(output, text.size());
|
|
|
|
|
it = std::copy_n(text.begin(), text.size(), it);
|
|
|
|
|
ctx.advance_to(output);
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
|
|
case format_part_t::kind::argument_id: {
|
|
|
|
|
advance_parse_context_to_specification(parse_ctx, part);
|
|
|
|
|
format_arg<Range>(parse_ctx, ctx, value.arg_id);
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
|
|
case format_part_t::kind::named_argument_id: {
|
|
|
|
|
advance_parse_context_to_specification(parse_ctx, part);
|
|
|
|
|
const auto named_arg_id =
|
|
|
|
|
value.named_arg_id.to_view(format_view.data());
|
|
|
|
|
format_arg<Range>(parse_ctx, ctx, named_arg_id);
|
|
|
|
|
} break;
|
|
|
|
|
case format_part_t::kind::specification: {
|
|
|
|
|
const auto& arg_id_value = value.spec.arg_id.val;
|
|
|
|
|
const auto arg = value.spec.arg_id.which ==
|
|
|
|
|
format_part_t::argument_id::which_arg_id::index
|
|
|
|
|
? ctx.arg(arg_id_value.index)
|
|
|
|
|
: ctx.arg(arg_id_value.named_index.to_view(
|
|
|
|
|
to_string_view(format_).data()));
|
|
|
|
|
|
|
|
|
|
auto specs = value.spec.parsed_specs;
|
|
|
|
|
|
|
|
|
|
handle_dynamic_spec<internal::width_checker>(
|
|
|
|
|
specs.width, specs.width_ref, ctx, format_view.begin());
|
|
|
|
|
handle_dynamic_spec<internal::precision_checker>(
|
|
|
|
|
specs.precision, specs.precision_ref, ctx, format_view.begin());
|
|
|
|
|
|
|
|
|
|
check_prepared_specs(specs, arg.type());
|
|
|
|
|
advance_parse_context_to_specification(parse_ctx, part);
|
|
|
|
|
ctx.advance_to(
|
|
|
|
|
visit_format_arg(arg_formatter<Range>(ctx, nullptr, &specs), arg));
|
|
|
|
|
} break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 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));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ctx.out();
|
|
|
|
|
}
|
|
|
|
|
template <typename Range, typename Context, typename Id>
|
|
|
|
|
void format_arg(
|
|
|
|
|
basic_format_parse_context<typename Range::value_type>& parse_ctx,
|
|
|
|
|
Context& ctx, Id arg_id) {
|
|
|
|
|
ctx.advance_to(
|
|
|
|
|
visit_format_arg(arg_formatter<Range>(ctx, &parse_ctx), ctx.arg(arg_id)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
void advance_parse_context_to_specification(
|
|
|
|
|
basic_parse_context<char_type>& parse_ctx,
|
|
|
|
|
const format_part_t& part) const {
|
|
|
|
|
const auto view = to_string_view(format_);
|
|
|
|
|
const auto specification_begin = view.data() + part.end_of_argument_id;
|
|
|
|
|
advance_to(parse_ctx, specification_begin);
|
|
|
|
|
}
|
|
|
|
|
// vformat_to is defined in a subnamespace to prevent ADL.
|
|
|
|
|
namespace cf {
|
|
|
|
|
template <typename Context, typename Range, typename CompiledFormat>
|
|
|
|
|
auto vformat_to(Range out, CompiledFormat& cf, basic_format_args<Context> args)
|
|
|
|
|
-> typename Context::iterator {
|
|
|
|
|
using char_type = typename Context::char_type;
|
|
|
|
|
basic_format_parse_context<char_type> parse_ctx(
|
|
|
|
|
to_string_view(cf.format_str_));
|
|
|
|
|
Context ctx(out.begin(), args);
|
|
|
|
|
|
|
|
|
|
const auto& parts = cf.parts();
|
|
|
|
|
for (auto part_it = std::begin(parts); part_it != std::end(parts);
|
|
|
|
|
++part_it) {
|
|
|
|
|
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);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename Range, typename Context, typename Id>
|
|
|
|
|
void format_arg(basic_parse_context<char_type>& parse_ctx, Context& ctx,
|
|
|
|
|
Id arg_id) const {
|
|
|
|
|
parse_ctx.check_arg_id(arg_id);
|
|
|
|
|
const auto stopped_at =
|
|
|
|
|
visit_format_arg(arg_formatter<Range>(ctx), ctx.arg(arg_id));
|
|
|
|
|
ctx.advance_to(stopped_at);
|
|
|
|
|
case format_part_t::kind::arg_index:
|
|
|
|
|
advance_to(parse_ctx, part.arg_id_end);
|
|
|
|
|
internal::format_arg<Range>(parse_ctx, ctx, value.arg_index);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case format_part_t::kind::arg_name:
|
|
|
|
|
advance_to(parse_ctx, part.arg_id_end);
|
|
|
|
|
internal::format_arg<Range>(parse_ctx, ctx, value.str);
|
|
|
|
|
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);
|
|
|
|
|
ctx.advance_to(
|
|
|
|
|
visit_format_arg(arg_formatter<Range>(ctx, nullptr, &specs), arg));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ctx.out();
|
|
|
|
|
}
|
|
|
|
|
} // namespace cf
|
|
|
|
|
|
|
|
|
|
template <typename Char>
|
|
|
|
|
void check_prepared_specs(const basic_format_specs<Char>& specs,
|
|
|
|
|
internal::type arg_type) const {
|
|
|
|
|
internal::error_handler h;
|
|
|
|
|
numeric_specs_checker<internal::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();
|
|
|
|
|
}
|
|
|
|
|
struct basic_compiled_format {};
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
Format format_;
|
|
|
|
|
PreparedPartsProvider parts_provider_;
|
|
|
|
|
};
|
|
|
|
|
template <typename S, typename = void>
|
|
|
|
|
struct compiled_format_base : basic_compiled_format {
|
|
|
|
|
using char_type = char_t<S>;
|
|
|
|
|
using parts_container = std::vector<internal::format_part<char_type>>;
|
|
|
|
|
|
|
|
|
|
template <typename Char> struct part_counter {
|
|
|
|
|
unsigned num_parts = 0;
|
|
|
|
|
parts_container compiled_parts;
|
|
|
|
|
|
|
|
|
|
FMT_CONSTEXPR void on_text(const Char* begin, const Char* end) {
|
|
|
|
|
if (begin != end) ++num_parts;
|
|
|
|
|
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);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FMT_CONSTEXPR void on_arg_id() { ++num_parts; }
|
|
|
|
|
FMT_CONSTEXPR void on_arg_id(unsigned) { ++num_parts; }
|
|
|
|
|
FMT_CONSTEXPR void on_arg_id(basic_string_view<Char>) { ++num_parts; }
|
|
|
|
|
const parts_container& parts() const { return compiled_parts; }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
FMT_CONSTEXPR void on_replacement_field(const Char*) {}
|
|
|
|
|
template <typename Char, unsigned N> struct format_part_array {
|
|
|
|
|
format_part<Char> data[N] = {};
|
|
|
|
|
FMT_CONSTEXPR format_part_array() = default;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
FMT_CONSTEXPR const Char* on_format_specs(const Char* begin,
|
|
|
|
|
const Char* end) {
|
|
|
|
|
// Find the matching brace.
|
|
|
|
|
unsigned braces_counter = 0;
|
|
|
|
|
for (; begin != end; ++begin) {
|
|
|
|
|
if (*begin == '{') {
|
|
|
|
|
++braces_counter;
|
|
|
|
|
} else if (*begin == '}') {
|
|
|
|
|
if (braces_counter == 0u) break;
|
|
|
|
|
--braces_counter;
|
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
// This is not a lambda for compatibility with older compilers.
|
|
|
|
|
struct {
|
|
|
|
|
format_part<Char>* parts;
|
|
|
|
|
unsigned* counter;
|
|
|
|
|
FMT_CONSTEXPR void operator()(const format_part<Char>& part) {
|
|
|
|
|
parts[(*counter)++] = part;
|
|
|
|
|
}
|
|
|
|
|
return begin;
|
|
|
|
|
} 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>());
|
|
|
|
|
}
|
|
|
|
|
return parts;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FMT_CONSTEXPR void on_error(const char*) {}
|
|
|
|
|
};
|
|
|
|
|
template <typename T> constexpr const T& constexpr_max(const T& a, const T& b) {
|
|
|
|
|
return (a < b) ? b : a;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename Format> class compiletime_prepared_parts_type_provider {
|
|
|
|
|
private:
|
|
|
|
|
using char_type = char_t<Format>;
|
|
|
|
|
template <typename S>
|
|
|
|
|
struct compiled_format_base<S, enable_if_t<is_compile_string<S>::value>>
|
|
|
|
|
: basic_compiled_format {
|
|
|
|
|
using char_type = char_t<S>;
|
|
|
|
|
|
|
|
|
|
static FMT_CONSTEXPR unsigned count_parts() {
|
|
|
|
|
FMT_CONSTEXPR_DECL const auto text = to_string_view(Format{});
|
|
|
|
|
part_counter<char_type> counter;
|
|
|
|
|
internal::parse_format_string</*IS_CONSTEXPR=*/true>(text, counter);
|
|
|
|
|
return counter.num_parts;
|
|
|
|
|
}
|
|
|
|
|
FMT_CONSTEXPR explicit compiled_format_base(basic_string_view<char_type>) {}
|
|
|
|
|
|
|
|
|
|
// Workaround for old compilers. Compiletime parts preparation will not be
|
|
|
|
|
// performed with them anyway.
|
|
|
|
|
// Workaround for old compilers. Format string compilation will not be
|
|
|
|
|
// performed there anyway.
|
|
|
|
|
#if FMT_USE_CONSTEXPR
|
|
|
|
|
static FMT_CONSTEXPR_DECL const unsigned number_of_format_parts =
|
|
|
|
|
compiletime_prepared_parts_type_provider::count_parts();
|
|
|
|
|
static FMT_CONSTEXPR_DECL const unsigned num_format_parts =
|
|
|
|
|
constexpr_max(count_parts(to_string_view(S())), 1u);
|
|
|
|
|
#else
|
|
|
|
|
static const unsigned number_of_format_parts = 0u;
|
|
|
|
|
static const unsigned num_format_parts = 1;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
using parts_container = format_part<char_type>[num_format_parts];
|
|
|
|
|
|
|
|
|
|
const parts_container& parts() const {
|
|
|
|
|
static FMT_CONSTEXPR_DECL const auto compiled_parts =
|
|
|
|
|
compile_to_parts<char_type, num_format_parts>(
|
|
|
|
|
internal::to_string_view(S()));
|
|
|
|
|
return compiled_parts.data;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <typename S, typename... Args>
|
|
|
|
|
class compiled_format : private compiled_format_base<S> {
|
|
|
|
|
public:
|
|
|
|
|
template <unsigned N> struct format_parts_array {
|
|
|
|
|
using value_type = format_part<char_type>;
|
|
|
|
|
using typename compiled_format_base<S>::char_type;
|
|
|
|
|
|
|
|
|
|
FMT_CONSTEXPR format_parts_array() : arr{} {}
|
|
|
|
|
private:
|
|
|
|
|
basic_string_view<char_type> format_str_;
|
|
|
|
|
|
|
|
|
|
FMT_CONSTEXPR value_type& operator[](unsigned ind) { return arr[ind]; }
|
|
|
|
|
template <typename Context, typename Range, typename CompiledFormat>
|
|
|
|
|
friend auto cf::vformat_to(Range out, CompiledFormat& cf,
|
|
|
|
|
basic_format_args<Context> args) ->
|
|
|
|
|
typename Context::iterator;
|
|
|
|
|
|
|
|
|
|
FMT_CONSTEXPR const value_type* begin() const { return arr; }
|
|
|
|
|
FMT_CONSTEXPR const value_type* end() const { return begin() + N; }
|
|
|
|
|
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) {}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
value_type arr[N];
|
|
|
|
|
};
|
|
|
|
|
#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>
|
|
|
|
|
constexpr const auto& get(const T& first, const Args&... rest) {
|
|
|
|
|
static_assert(N < 1 + sizeof...(Args), "index is out of bounds");
|
|
|
|
|
if constexpr (N == 0)
|
|
|
|
|
return first;
|
|
|
|
|
else
|
|
|
|
|
return get<N - 1>(rest...);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct empty {
|
|
|
|
|
// Parts preparator will search for it
|
|
|
|
|
using value_type = format_part<char_type>;
|
|
|
|
|
};
|
|
|
|
|
template <int N, typename> struct get_type_impl;
|
|
|
|
|
|
|
|
|
|
using type = conditional_t<number_of_format_parts != 0,
|
|
|
|
|
format_parts_array<number_of_format_parts>, empty>;
|
|
|
|
|
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 <typename Parts> class compiletime_prepared_parts_collector {
|
|
|
|
|
private:
|
|
|
|
|
using format_part = typename Parts::value_type;
|
|
|
|
|
template <int N, typename T>
|
|
|
|
|
using get_type = typename get_type_impl<N, T>::type;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
FMT_CONSTEXPR explicit compiletime_prepared_parts_collector(Parts& parts)
|
|
|
|
|
: parts_{parts}, counter_{0u} {}
|
|
|
|
|
template <typename Char> struct text {
|
|
|
|
|
basic_string_view<Char> data;
|
|
|
|
|
using char_type = Char;
|
|
|
|
|
|
|
|
|
|
FMT_CONSTEXPR void push_back(format_part part) { parts_[counter_++] = part; }
|
|
|
|
|
template <typename OutputIt, typename... Args>
|
|
|
|
|
OutputIt format(OutputIt out, const Args&...) const {
|
|
|
|
|
// TODO: reserve
|
|
|
|
|
return copy_str<Char>(data.begin(), data.end(), out);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
FMT_CONSTEXPR format_part& back() { return parts_[counter_ - 1]; }
|
|
|
|
|
template <typename Char>
|
|
|
|
|
constexpr text<Char> make_text(basic_string_view<Char> s, size_t pos,
|
|
|
|
|
size_t size) {
|
|
|
|
|
return {{&s[pos], size}};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
Parts& parts_;
|
|
|
|
|
unsigned counter_;
|
|
|
|
|
};
|
|
|
|
|
template <typename Char, typename OutputIt, typename T,
|
|
|
|
|
std::enable_if_t<std::is_integral_v<T>, int> = 0>
|
|
|
|
|
OutputIt format_default(OutputIt out, T value) {
|
|
|
|
|
// TODO: reserve
|
|
|
|
|
format_int fi(value);
|
|
|
|
|
return std::copy(fi.data(), fi.data() + fi.size(), out);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename PartsContainer, typename Char>
|
|
|
|
|
FMT_CONSTEXPR PartsContainer prepare_parts(basic_string_view<Char> format) {
|
|
|
|
|
PartsContainer parts;
|
|
|
|
|
internal::parse_format_string</*IS_CONSTEXPR=*/false>(
|
|
|
|
|
format, format_preparation_handler<Char, PartsContainer>(format, parts));
|
|
|
|
|
return parts;
|
|
|
|
|
template <typename Char, typename OutputIt>
|
|
|
|
|
OutputIt format_default(OutputIt out, double value) {
|
|
|
|
|
writer w(out);
|
|
|
|
|
w.write(value);
|
|
|
|
|
return w.out();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename PartsContainer, typename Char>
|
|
|
|
|
FMT_CONSTEXPR PartsContainer
|
|
|
|
|
prepare_compiletime_parts(basic_string_view<Char> format) {
|
|
|
|
|
using collector = compiletime_prepared_parts_collector<PartsContainer>;
|
|
|
|
|
template <typename Char, typename OutputIt>
|
|
|
|
|
OutputIt format_default(OutputIt out, Char value) {
|
|
|
|
|
*out++ = value;
|
|
|
|
|
return out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PartsContainer parts;
|
|
|
|
|
collector c(parts);
|
|
|
|
|
internal::parse_format_string</*IS_CONSTEXPR=*/true>(
|
|
|
|
|
format, format_preparation_handler<Char, collector>(format, c));
|
|
|
|
|
return parts;
|
|
|
|
|
template <typename Char, typename OutputIt>
|
|
|
|
|
OutputIt format_default(OutputIt out, const Char* value) {
|
|
|
|
|
auto length = std::char_traits<Char>::length(value);
|
|
|
|
|
return copy_str<Char>(value, value + length, out);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename PartsContainer> class runtime_parts_provider {
|
|
|
|
|
public:
|
|
|
|
|
runtime_parts_provider() = delete;
|
|
|
|
|
template <typename Char>
|
|
|
|
|
runtime_parts_provider(basic_string_view<Char> format)
|
|
|
|
|
: parts_(prepare_parts<PartsContainer>(format)) {}
|
|
|
|
|
// A replacement field that refers to argument N.
|
|
|
|
|
template <typename Char, typename T, int N> struct field {
|
|
|
|
|
using char_type = Char;
|
|
|
|
|
|
|
|
|
|
const PartsContainer& parts() const { return parts_; }
|
|
|
|
|
template <typename OutputIt, typename... Args>
|
|
|
|
|
OutputIt format(OutputIt out, const Args&... args) const {
|
|
|
|
|
// This ensures that the argument type is convertile to `const T&`.
|
|
|
|
|
const T& arg = get<N>(args...);
|
|
|
|
|
return format_default<Char>(out, arg);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
PartsContainer parts_;
|
|
|
|
|
template <typename L, typename R> struct concat {
|
|
|
|
|
L lhs;
|
|
|
|
|
R rhs;
|
|
|
|
|
using char_type = typename L::char_type;
|
|
|
|
|
|
|
|
|
|
template <typename OutputIt, typename... Args>
|
|
|
|
|
OutputIt format(OutputIt out, const Args&... args) const {
|
|
|
|
|
out = lhs.format(out, args...);
|
|
|
|
|
return rhs.format(out, args...);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <typename Format, typename PartsContainer>
|
|
|
|
|
struct compiletime_parts_provider {
|
|
|
|
|
compiletime_parts_provider() = delete;
|
|
|
|
|
template <typename Char>
|
|
|
|
|
FMT_CONSTEXPR compiletime_parts_provider(basic_string_view<Char>) {}
|
|
|
|
|
template <typename L, typename R>
|
|
|
|
|
constexpr concat<L, R> make_concat(L lhs, R rhs) {
|
|
|
|
|
return {lhs, rhs};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const PartsContainer& parts() const {
|
|
|
|
|
static FMT_CONSTEXPR_DECL const PartsContainer prepared_parts =
|
|
|
|
|
prepare_compiletime_parts<PartsContainer>(
|
|
|
|
|
internal::to_string_view(Format{}));
|
|
|
|
|
struct unknown_format {};
|
|
|
|
|
|
|
|
|
|
return prepared_parts;
|
|
|
|
|
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>
|
|
|
|
|
constexpr auto parse_tail(T head, S format_str) {
|
|
|
|
|
if constexpr (POS != to_string_view(format_str).size()) {
|
|
|
|
|
constexpr auto tail = compile_format_string<Args, POS, ID>(format_str);
|
|
|
|
|
if constexpr (std::is_same<remove_cvref_t<decltype(tail)>,
|
|
|
|
|
unknown_format>())
|
|
|
|
|
return tail;
|
|
|
|
|
else
|
|
|
|
|
return make_concat(head, tail);
|
|
|
|
|
} else {
|
|
|
|
|
return head;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Compiles a non-empty format string and returns the compiled representation
|
|
|
|
|
// or unknown_format() on unrecognized input.
|
|
|
|
|
template <typename Args, size_t POS, int ID, typename S>
|
|
|
|
|
constexpr auto compile_format_string(S format_str) {
|
|
|
|
|
using char_type = typename S::char_type;
|
|
|
|
|
constexpr basic_string_view<char_type> str = format_str;
|
|
|
|
|
if constexpr (str[POS] == '{') {
|
|
|
|
|
if (POS + 1 == str.size())
|
|
|
|
|
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);
|
|
|
|
|
} else if constexpr (str[POS + 1] == '}') {
|
|
|
|
|
using type = get_type<ID, Args>;
|
|
|
|
|
if constexpr (std::is_same<type, int>::value) {
|
|
|
|
|
return parse_tail<Args, POS + 2, ID + 1>(field<char_type, type, ID>(),
|
|
|
|
|
format_str);
|
|
|
|
|
} else {
|
|
|
|
|
return unknown_format();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return unknown_format();
|
|
|
|
|
}
|
|
|
|
|
} else if constexpr (str[POS] == '}') {
|
|
|
|
|
if (POS + 1 == str.size())
|
|
|
|
|
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);
|
|
|
|
|
return parse_tail<Args, end, ID>(make_text(str, POS, end - POS),
|
|
|
|
|
format_str);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif // __cpp_if_constexpr
|
|
|
|
|
} // namespace internal
|
|
|
|
|
|
|
|
|
|
#if FMT_USE_CONSTEXPR
|
|
|
|
|
# ifdef __cpp_if_constexpr
|
|
|
|
|
template <typename... Args, typename S,
|
|
|
|
|
FMT_ENABLE_IF(is_compile_string<S>::value)>
|
|
|
|
|
FMT_CONSTEXPR auto compile(S format_str) -> internal::prepared_format<
|
|
|
|
|
S,
|
|
|
|
|
internal::compiletime_parts_provider<
|
|
|
|
|
S,
|
|
|
|
|
typename internal::compiletime_prepared_parts_type_provider<S>::type>,
|
|
|
|
|
Args...> {
|
|
|
|
|
return format_str;
|
|
|
|
|
constexpr auto compile(S format_str) {
|
|
|
|
|
constexpr basic_string_view<typename S::char_type> str = format_str;
|
|
|
|
|
if constexpr (str.size() == 0) {
|
|
|
|
|
return internal::make_text(str, 0, 0);
|
|
|
|
|
} else {
|
|
|
|
|
constexpr auto result =
|
|
|
|
|
internal::compile_format_string<internal::type_list<Args...>, 0, 0>(
|
|
|
|
|
format_str);
|
|
|
|
|
if constexpr (std::is_same<remove_cvref_t<decltype(result)>,
|
|
|
|
|
internal::unknown_format>()) {
|
|
|
|
|
return internal::compiled_format<S, Args...>(to_string_view(format_str));
|
|
|
|
|
} else {
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
template <typename CompiledFormat, typename... Args,
|
|
|
|
|
typename Char = typename CompiledFormat::char_type,
|
|
|
|
|
FMT_ENABLE_IF(!std::is_base_of<internal::basic_compiled_format,
|
|
|
|
|
CompiledFormat>::value)>
|
|
|
|
|
std::basic_string<Char> format(const CompiledFormat& cf, const Args&... args) {
|
|
|
|
|
basic_memory_buffer<Char> buffer;
|
|
|
|
|
using range = buffer_range<Char>;
|
|
|
|
|
using context = buffer_context<Char>;
|
|
|
|
|
cf.format(std::back_inserter(buffer), args...);
|
|
|
|
|
return to_string(buffer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename OutputIt, typename CompiledFormat, typename... Args,
|
|
|
|
|
FMT_ENABLE_IF(!std::is_base_of<internal::basic_compiled_format,
|
|
|
|
|
CompiledFormat>::value)>
|
|
|
|
|
OutputIt format_to(OutputIt out, const CompiledFormat& cf,
|
|
|
|
|
const Args&... args) {
|
|
|
|
|
return cf.format(out, args...);
|
|
|
|
|
}
|
|
|
|
|
# else
|
|
|
|
|
template <typename... Args, typename S,
|
|
|
|
|
FMT_ENABLE_IF(is_compile_string<S>::value)>
|
|
|
|
|
constexpr auto compile(S format_str) -> internal::compiled_format<S, Args...> {
|
|
|
|
|
return internal::compiled_format<S, Args...>(to_string_view(format_str));
|
|
|
|
|
}
|
|
|
|
|
# endif // __cpp_if_constexpr
|
|
|
|
|
#endif // FMT_USE_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]) -> internal::prepared_format<
|
|
|
|
|
std::basic_string<Char>,
|
|
|
|
|
internal::runtime_parts_provider<std::vector<internal::format_part<Char>>>,
|
|
|
|
|
Args...> {
|
|
|
|
|
return std::basic_string<Char>(format_str, N - 1);
|
|
|
|
|
auto compile(const Char (&format_str)[N])
|
|
|
|
|
-> internal::compiled_format<const Char*, Args...> {
|
|
|
|
|
return internal::compiled_format<const Char*, Args...>(
|
|
|
|
|
basic_string_view<Char>(format_str, N - 1));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename CompiledFormat, typename... Args,
|
|
|
|
|
typename Char = typename CompiledFormat::char_type>
|
|
|
|
|
typename Char = typename CompiledFormat::char_type,
|
|
|
|
|
FMT_ENABLE_IF(std::is_base_of<internal::basic_compiled_format,
|
|
|
|
|
CompiledFormat>::value)>
|
|
|
|
|
std::basic_string<Char> format(const CompiledFormat& cf, const Args&... args) {
|
|
|
|
|
basic_memory_buffer<Char> buffer;
|
|
|
|
|
using range = internal::buffer_range<Char>;
|
|
|
|
|
using range = buffer_range<Char>;
|
|
|
|
|
using context = buffer_context<Char>;
|
|
|
|
|
cf.template vformat_to<range, context>(range(buffer),
|
|
|
|
|
{make_format_args<context>(args...)});
|
|
|
|
|
internal::cf::vformat_to<context>(range(buffer), cf,
|
|
|
|
|
{make_format_args<context>(args...)});
|
|
|
|
|
return to_string(buffer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename OutputIt, typename CompiledFormat, typename... Args>
|
|
|
|
|
template <typename OutputIt, typename CompiledFormat, typename... Args,
|
|
|
|
|
FMT_ENABLE_IF(std::is_base_of<internal::basic_compiled_format,
|
|
|
|
|
CompiledFormat>::value)>
|
|
|
|
|
OutputIt format_to(OutputIt out, const CompiledFormat& cf,
|
|
|
|
|
const Args&... args) {
|
|
|
|
|
using char_type = typename CompiledFormat::char_type;
|
|
|
|
|
using range = internal::output_range<OutputIt, char_type>;
|
|
|
|
|
using context = format_context_t<OutputIt, char_type>;
|
|
|
|
|
return cf.template vformat_to<range, context>(
|
|
|
|
|
range(out), {make_format_args<context>(args...)});
|
|
|
|
|
return internal::cf::vformat_to<context>(
|
|
|
|
|
range(out), cf, {make_format_args<context>(args...)});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename OutputIt, typename CompiledFormat, typename... Args,
|
|
|
|
@ -455,10 +579,7 @@ format_to_n_result<OutputIt> format_to_n(OutputIt out, size_t n,
|
|
|
|
|
|
|
|
|
|
template <typename CompiledFormat, typename... Args>
|
|
|
|
|
std::size_t formatted_size(const CompiledFormat& cf, const Args&... args) {
|
|
|
|
|
return fmt::format_to(
|
|
|
|
|
internal::counting_iterator<typename CompiledFormat::char_type>(),
|
|
|
|
|
cf, args...)
|
|
|
|
|
.count();
|
|
|
|
|
return format_to(internal::counting_iterator(), cf, args...).count();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FMT_END_NAMESPACE
|
|
|
|
|