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
|
|
|
|
|
|
|
#include <vector>
|
2019-07-25 17:27:27 +00:00
|
|
|
#include "format.h"
|
2018-11-27 10:52:00 +00:00
|
|
|
|
|
|
|
FMT_BEGIN_NAMESPACE
|
2019-07-26 06:02:45 +00:00
|
|
|
namespace internal {
|
2018-11-27 10:52:00 +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 {
|
|
|
|
unsigned 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
|
|
|
|
|
|
|
FMT_CONSTEXPR value(unsigned 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 16:05:24 +00:00
|
|
|
std::size_t arg_id_end = 0; // Position past the end of the argument id.
|
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) {}
|
|
|
|
|
|
|
|
static FMT_CONSTEXPR format_part make_arg_index(unsigned index) {
|
|
|
|
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
|
|
|
|
2019-09-01 16:05:24 +00:00
|
|
|
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; }
|
2018-11-27 10:52:00 +00:00
|
|
|
|
2019-09-01 16:05:24 +00:00
|
|
|
FMT_CONSTEXPR void on_replacement_field(const Char*) {}
|
2018-11-27 10:52:00 +00:00
|
|
|
|
2019-09-01 16:05:24 +00:00
|
|
|
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;
|
|
|
|
}
|
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-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
|
|
|
}
|
|
|
|
|
|
|
|
FMT_CONSTEXPR void on_arg_id() {
|
2019-09-01 16:05:24 +00:00
|
|
|
part_ = part::make_arg_index(parse_context_.next_arg_id());
|
2018-11-27 10:52:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FMT_CONSTEXPR void on_arg_id(unsigned id) {
|
|
|
|
parse_context_.check_arg_id(id);
|
2019-09-01 16:05:24 +00:00
|
|
|
part_ = part::make_arg_index(id);
|
2018-11-27 10:52:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FMT_CONSTEXPR void on_arg_id(basic_string_view<Char> id) {
|
2019-09-01 18:10:49 +00:00
|
|
|
part_ = part::make_arg_name(id);
|
2018-11-27 10:52:00 +00:00
|
|
|
}
|
|
|
|
|
2019-01-13 02:27:38 +00:00
|
|
|
FMT_CONSTEXPR void on_replacement_field(const Char* ptr) {
|
2019-09-01 16:05:24 +00:00
|
|
|
part_.arg_id_end = ptr - format_str_.begin();
|
|
|
|
handler_(part_);
|
2018-11-27 10:52:00 +00:00
|
|
|
}
|
|
|
|
|
2019-01-13 02:27:38 +00:00
|
|
|
FMT_CONSTEXPR const Char* on_format_specs(const Char* begin,
|
|
|
|
const Char* end) {
|
2019-09-01 16:05:24 +00:00
|
|
|
const auto specs_offset = to_unsigned(begin - format_str_.begin());
|
2018-11-27 10:52:00 +00:00
|
|
|
|
2019-09-01 16:05:24 +00:00
|
|
|
auto repl = typename part::replacement();
|
|
|
|
dynamic_specs_handler<basic_parse_context<Char>> handler(repl.specs,
|
|
|
|
parse_context_);
|
2018-11-27 10:52:00 +00:00
|
|
|
begin = parse_format_specs(begin, end, handler);
|
2019-08-04 18:15:07 +00:00
|
|
|
if (*begin != '}') on_error("missing '}' in format string");
|
2018-11-27 10:52:00 +00:00
|
|
|
|
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);
|
|
|
|
auto part = part::make_replacement(repl);
|
|
|
|
part.arg_id_end = specs_offset;
|
|
|
|
handler_(part);
|
2018-11-27 10:52:00 +00:00
|
|
|
return begin;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2019-09-01 16:05:24 +00:00
|
|
|
PartHandler handler_;
|
|
|
|
part part_;
|
|
|
|
basic_string_view<Char> format_str_;
|
2018-11-27 10:52:00 +00:00
|
|
|
basic_parse_context<Char> parse_context_;
|
|
|
|
};
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2018-11-27 10:52:00 +00:00
|
|
|
template <typename Format, typename PreparedPartsProvider, typename... Args>
|
2019-08-31 17:23:42 +00:00
|
|
|
class compiled_format {
|
2018-11-27 10:52:00 +00:00
|
|
|
public:
|
2019-06-01 19:32:24 +00:00
|
|
|
using char_type = char_t<Format>;
|
|
|
|
using format_part_t = format_part<char_type>;
|
2018-11-27 10:52:00 +00:00
|
|
|
|
2019-08-31 17:23:42 +00:00
|
|
|
constexpr compiled_format(Format f)
|
2018-11-27 10:52:00 +00:00
|
|
|
: format_(std::move(f)), parts_provider_(to_string_view(format_)) {}
|
|
|
|
|
2019-08-31 17:23:42 +00:00
|
|
|
compiled_format() = delete;
|
2018-11-27 10:52:00 +00:00
|
|
|
|
2019-05-21 19:03:57 +00:00
|
|
|
template <typename Range, typename Context>
|
|
|
|
auto vformat_to(Range out, basic_format_args<Context> args) const ->
|
|
|
|
typename Context::iterator {
|
2018-11-27 10:52:00 +00:00
|
|
|
const auto format_view = internal::to_string_view(format_);
|
2019-02-10 03:34:42 +00:00
|
|
|
basic_parse_context<char_type> parse_ctx(format_view);
|
2019-05-21 19:03:57 +00:00
|
|
|
Context ctx(out.begin(), args);
|
2018-11-27 10:52:00 +00:00
|
|
|
|
2019-01-13 02:27:38 +00:00
|
|
|
const auto& parts = parts_provider_.parts();
|
2018-11-27 10:52:00 +00:00
|
|
|
for (auto part_it = parts.begin(); part_it != parts.end(); ++part_it) {
|
2019-01-13 02:27:38 +00:00
|
|
|
const auto& part = *part_it;
|
|
|
|
const auto& value = part.val;
|
2018-11-27 10:52:00 +00:00
|
|
|
|
2019-08-31 17:23:42 +00:00
|
|
|
switch (part.part_kind) {
|
2019-08-25 15:08:46 +00:00
|
|
|
case format_part_t::kind::text: {
|
2019-09-01 18:10:49 +00:00
|
|
|
const auto text = value.str;
|
2019-01-13 08:06:51 +00:00
|
|
|
auto output = ctx.out();
|
|
|
|
auto&& it = internal::reserve(output, text.size());
|
2018-11-27 10:52:00 +00:00
|
|
|
it = std::copy_n(text.begin(), text.size(), it);
|
2019-01-13 08:06:51 +00:00
|
|
|
ctx.advance_to(output);
|
2018-11-27 10:52:00 +00:00
|
|
|
} break;
|
|
|
|
|
2019-08-31 17:23:42 +00:00
|
|
|
case format_part_t::kind::arg_index: {
|
2019-02-10 03:34:42 +00:00
|
|
|
advance_parse_context_to_specification(parse_ctx, part);
|
2019-08-31 17:23:42 +00:00
|
|
|
format_arg<Range>(parse_ctx, ctx, value.arg_index);
|
2018-11-27 10:52:00 +00:00
|
|
|
} break;
|
|
|
|
|
2019-08-31 17:23:42 +00:00
|
|
|
case format_part_t::kind::arg_name: {
|
2019-02-10 03:34:42 +00:00
|
|
|
advance_parse_context_to_specification(parse_ctx, part);
|
2019-09-01 18:48:01 +00:00
|
|
|
format_arg<Range>(parse_ctx, ctx, value.str);
|
2018-11-27 10:52:00 +00:00
|
|
|
} break;
|
2019-08-31 17:23:42 +00:00
|
|
|
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
|
2019-06-05 23:50:04 +00:00
|
|
|
? ctx.arg(arg_id_value.index)
|
2019-09-01 18:10:49 +00:00
|
|
|
: ctx.arg(arg_id_value.name);
|
2018-11-27 10:52:00 +00:00
|
|
|
|
2019-09-01 16:05:24 +00:00
|
|
|
auto specs = value.repl.specs;
|
2018-11-27 10:52:00 +00:00
|
|
|
|
2019-02-10 03:34:42 +00:00
|
|
|
handle_dynamic_spec<internal::width_checker>(
|
2019-07-07 00:57:57 +00:00
|
|
|
specs.width, specs.width_ref, ctx, format_view.begin());
|
2018-11-27 10:52:00 +00:00
|
|
|
handle_dynamic_spec<internal::precision_checker>(
|
2019-02-10 03:34:42 +00:00
|
|
|
specs.precision, specs.precision_ref, ctx, format_view.begin());
|
2018-11-27 10:52:00 +00:00
|
|
|
|
|
|
|
check_prepared_specs(specs, arg.type());
|
2019-02-10 03:34:42 +00:00
|
|
|
advance_parse_context_to_specification(parse_ctx, part);
|
2018-11-27 10:52:00 +00:00
|
|
|
ctx.advance_to(
|
2019-05-30 14:01:31 +00:00
|
|
|
visit_format_arg(arg_formatter<Range>(ctx, nullptr, &specs), arg));
|
2018-11-27 10:52:00 +00:00
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctx.out();
|
|
|
|
}
|
|
|
|
|
2019-08-03 13:28:31 +00:00
|
|
|
private:
|
2019-02-10 03:34:42 +00:00
|
|
|
void advance_parse_context_to_specification(
|
|
|
|
basic_parse_context<char_type>& parse_ctx,
|
|
|
|
const format_part_t& part) const {
|
2018-11-27 10:52:00 +00:00
|
|
|
const auto view = to_string_view(format_);
|
2019-09-01 16:05:24 +00:00
|
|
|
const auto specification_begin = view.data() + part.arg_id_end;
|
2019-05-12 17:03:10 +00:00
|
|
|
advance_to(parse_ctx, specification_begin);
|
2018-11-27 10:52:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Range, typename Context, typename Id>
|
2019-02-10 03:34:42 +00:00
|
|
|
void format_arg(basic_parse_context<char_type>& parse_ctx, Context& ctx,
|
|
|
|
Id arg_id) const {
|
2019-09-01 18:48:01 +00:00
|
|
|
ctx.advance_to(visit_format_arg(arg_formatter<Range>(ctx, &parse_ctx),
|
|
|
|
ctx.arg(arg_id)));
|
2018-11-27 10:52:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Char>
|
2019-01-13 02:27:38 +00:00
|
|
|
void check_prepared_specs(const basic_format_specs<Char>& specs,
|
|
|
|
internal::type arg_type) const {
|
2018-11-27 10:52:00 +00:00
|
|
|
internal::error_handler h;
|
|
|
|
numeric_specs_checker<internal::error_handler> checker(h, arg_type);
|
2019-07-07 00:57:57 +00:00
|
|
|
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();
|
2018-11-27 10:52:00 +00:00
|
|
|
}
|
|
|
|
|
2019-01-13 02:27:38 +00:00
|
|
|
private:
|
2018-11-27 10:52:00 +00:00
|
|
|
Format format_;
|
|
|
|
PreparedPartsProvider parts_provider_;
|
|
|
|
};
|
|
|
|
|
2019-08-25 15:19:49 +00:00
|
|
|
template <typename Format> class compiletime_prepared_parts_type_provider {
|
|
|
|
private:
|
|
|
|
using char_type = char_t<Format>;
|
2018-11-27 10:52:00 +00:00
|
|
|
|
|
|
|
// Workaround for old compilers. Compiletime parts preparation will not be
|
|
|
|
// performed with them anyway.
|
|
|
|
#if FMT_USE_CONSTEXPR
|
|
|
|
static FMT_CONSTEXPR_DECL const unsigned number_of_format_parts =
|
2019-09-01 16:05:24 +00:00
|
|
|
count_parts(to_string_view(Format()));
|
2018-11-27 10:52:00 +00:00
|
|
|
#else
|
|
|
|
static const unsigned number_of_format_parts = 0u;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
public:
|
|
|
|
template <unsigned N> struct format_parts_array {
|
2019-08-04 18:15:07 +00:00
|
|
|
using value_type = format_part<char_type>;
|
2018-11-27 10:52:00 +00:00
|
|
|
|
|
|
|
FMT_CONSTEXPR format_parts_array() : arr{} {}
|
|
|
|
|
2019-01-13 02:27:38 +00:00
|
|
|
FMT_CONSTEXPR value_type& operator[](unsigned ind) { return arr[ind]; }
|
2018-11-27 10:52:00 +00:00
|
|
|
|
2019-01-13 02:27:38 +00:00
|
|
|
FMT_CONSTEXPR const value_type* begin() const { return arr; }
|
|
|
|
FMT_CONSTEXPR const value_type* end() const { return begin() + N; }
|
2018-11-27 10:52:00 +00:00
|
|
|
|
2019-01-13 02:27:38 +00:00
|
|
|
private:
|
2018-11-27 10:52:00 +00:00
|
|
|
value_type arr[N];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct empty {
|
|
|
|
// Parts preparator will search for it
|
2019-08-04 18:15:07 +00:00
|
|
|
using value_type = format_part<char_type>;
|
2018-11-27 10:52:00 +00:00
|
|
|
};
|
|
|
|
|
2019-08-11 13:53:03 +00:00
|
|
|
using type = conditional_t<number_of_format_parts != 0,
|
2019-06-05 01:50:30 +00:00
|
|
|
format_parts_array<number_of_format_parts>, empty>;
|
2018-11-27 10:52:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename PartsContainer, typename Char>
|
|
|
|
FMT_CONSTEXPR PartsContainer
|
2019-09-01 16:05:24 +00:00
|
|
|
prepare_compiletime_parts(basic_string_view<Char> format_str) {
|
|
|
|
// This is not a lambda for compatibility with older compilers.
|
|
|
|
struct collector {
|
|
|
|
PartsContainer& parts;
|
|
|
|
unsigned counter = 0;
|
|
|
|
FMT_CONSTEXPR void operator()(const format_part<Char>& part) {
|
|
|
|
parts[counter++] = part;
|
|
|
|
}
|
|
|
|
};
|
2018-11-27 10:52:00 +00:00
|
|
|
PartsContainer parts;
|
2019-09-01 16:05:24 +00:00
|
|
|
compile_format_string<true>(format_str, collector{parts});
|
2018-11-27 10:52:00 +00:00
|
|
|
return parts;
|
|
|
|
}
|
|
|
|
|
2019-08-31 17:23:42 +00:00
|
|
|
template <typename Char> class runtime_parts_provider {
|
2018-11-27 10:52:00 +00:00
|
|
|
public:
|
2019-08-31 17:23:42 +00:00
|
|
|
using parts_container = std::vector<internal::format_part<Char>>;
|
|
|
|
|
2019-09-01 16:05:24 +00:00
|
|
|
runtime_parts_provider(basic_string_view<Char> format_str) {
|
|
|
|
compile_format_string<false>(
|
|
|
|
format_str,
|
|
|
|
[this](const format_part<Char>& part) { parts_.push_back(part); });
|
2019-08-31 17:23:42 +00:00
|
|
|
}
|
2018-11-27 10:52:00 +00:00
|
|
|
|
2019-08-31 17:23:42 +00:00
|
|
|
const parts_container& parts() const { return parts_; }
|
2018-11-27 10:52:00 +00:00
|
|
|
|
|
|
|
private:
|
2019-08-31 17:23:42 +00:00
|
|
|
parts_container parts_;
|
2018-11-27 10:52:00 +00:00
|
|
|
};
|
|
|
|
|
2019-08-31 17:23:42 +00:00
|
|
|
template <typename Format> struct compiletime_parts_provider {
|
|
|
|
using parts_container =
|
|
|
|
typename internal::compiletime_prepared_parts_type_provider<Format>::type;
|
|
|
|
|
2018-11-27 10:52:00 +00:00
|
|
|
template <typename Char>
|
|
|
|
FMT_CONSTEXPR compiletime_parts_provider(basic_string_view<Char>) {}
|
|
|
|
|
2019-08-31 17:23:42 +00:00
|
|
|
const parts_container& parts() const {
|
|
|
|
static FMT_CONSTEXPR_DECL const parts_container prepared_parts =
|
|
|
|
prepare_compiletime_parts<parts_container>(
|
|
|
|
internal::to_string_view(Format()));
|
2018-11-27 10:52:00 +00:00
|
|
|
return prepared_parts;
|
|
|
|
}
|
|
|
|
};
|
2019-07-26 04:44:23 +00:00
|
|
|
} // namespace internal
|
2018-11-27 10:52:00 +00:00
|
|
|
|
|
|
|
#if FMT_USE_CONSTEXPR
|
2019-08-10 20:18:11 +00:00
|
|
|
template <typename... Args, typename S,
|
|
|
|
FMT_ENABLE_IF(is_compile_string<S>::value)>
|
2019-08-31 17:23:42 +00:00
|
|
|
FMT_CONSTEXPR auto compile(S format_str)
|
|
|
|
-> internal::compiled_format<S, internal::compiletime_parts_provider<S>,
|
|
|
|
Args...> {
|
2019-08-25 14:57:56 +00:00
|
|
|
return format_str;
|
2018-11-27 10:52:00 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-09-01 18:48:01 +00:00
|
|
|
// Compiles the format string which must be a string literal.
|
2019-08-24 15:36:14 +00:00
|
|
|
template <typename... Args, typename Char, size_t N>
|
2019-08-31 17:23:42 +00:00
|
|
|
auto compile(const Char (&format_str)[N]) -> internal::compiled_format<
|
|
|
|
std::basic_string<Char>, internal::runtime_parts_provider<Char>, Args...> {
|
2019-08-25 14:48:09 +00:00
|
|
|
return std::basic_string<Char>(format_str, N - 1);
|
2018-11-27 10:52:00 +00:00
|
|
|
}
|
|
|
|
|
2019-08-03 13:28:31 +00:00
|
|
|
template <typename CompiledFormat, typename... Args,
|
|
|
|
typename Char = typename CompiledFormat::char_type>
|
|
|
|
std::basic_string<Char> format(const CompiledFormat& cf, const Args&... args) {
|
|
|
|
basic_memory_buffer<Char> buffer;
|
2019-08-28 13:18:34 +00:00
|
|
|
using range = buffer_range<Char>;
|
2019-08-03 13:28:31 +00:00
|
|
|
using context = buffer_context<Char>;
|
|
|
|
cf.template vformat_to<range, context>(range(buffer),
|
|
|
|
{make_format_args<context>(args...)});
|
|
|
|
return to_string(buffer);
|
|
|
|
}
|
|
|
|
|
2019-08-03 15:35:02 +00:00
|
|
|
template <typename OutputIt, typename CompiledFormat, typename... Args>
|
|
|
|
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...)});
|
|
|
|
}
|
|
|
|
|
2019-08-03 14:54:49 +00:00
|
|
|
template <typename OutputIt, typename CompiledFormat, typename... Args,
|
|
|
|
FMT_ENABLE_IF(internal::is_output_iterator<OutputIt>::value)>
|
2019-08-24 15:36:14 +00:00
|
|
|
format_to_n_result<OutputIt> format_to_n(OutputIt out, size_t n,
|
2019-08-03 14:54:49 +00:00
|
|
|
const CompiledFormat& cf,
|
|
|
|
const Args&... args) {
|
|
|
|
auto it =
|
2019-08-25 13:38:41 +00:00
|
|
|
format_to(internal::truncating_iterator<OutputIt>(out, n), cf, args...);
|
2019-08-03 14:54:49 +00:00
|
|
|
return {it.base(), it.count()};
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename CompiledFormat, typename... Args>
|
|
|
|
std::size_t formatted_size(const CompiledFormat& cf, const Args&... args) {
|
2019-08-25 13:38:41 +00:00
|
|
|
return fmt::format_to(
|
|
|
|
internal::counting_iterator<typename CompiledFormat::char_type>(),
|
|
|
|
cf, args...)
|
2019-08-03 14:54:49 +00:00
|
|
|
.count();
|
|
|
|
}
|
|
|
|
|
2018-11-27 10:52:00 +00:00
|
|
|
FMT_END_NAMESPACE
|
|
|
|
|
2019-07-25 16:01:21 +00:00
|
|
|
#endif // FMT_COMPILE_H_
|