d4c6fa3122
cd4af11ef Update version 1ebc2f7cc Bump version f4c997062 Fix changelog 72920ba30 Update changelog 0907c08ae Fix handling of default alignmment with locale (#1801) 37c8f4eaf Don't use 128 bit integers with clang-cl (#1800) eaaaec999 Workaround a bug in msvc ccf8561cb Workaround broken numeric_limites, part 2 (#1787) 0cc73ebf7 Report error on missing named argument (#1796) 33efc3c94 Fix handling of iterators in locale-specific formatting (#1782) b9d749095 Update version 86b63bb71 Bump version cbf6be960 Update changelog 229ee9b46 Workaround broken numeric_limits (#1725) 2b7a146fa Fix a regression in handling digit separators (#1782) 89d0c7124 Fix compatibility with CMake 3.4 (#1779) f19b1a521 Update version 5c67fefb2 Fix a changelog entry 1d2a556e1 Fix undefined reference error 04c9b62fb Merge release branch 6be6762e5 Fix date f1dd2eb3c Bump version fbf3b943c Workaround a bug in gcc a29a01d30 Fix docs 9f0b3afb7 Bump version in namespace 86b2f99f8 Fix the docs c472ff12d Update version 5173a76ba Update version 1614af352 Minor corrections in the changelog 569a9b3a7 Bump version 4e7e3c65a Update docs 0f7a6bfa1 Add a section on std::format compatibility 4faec5a5e Update README.rst 7dbc8ac71 Update changelog c87dd746f Update changelog 372175caf Revert changelog changes 904754876 Add ClickHouse to the list of projects (#1751) d30bca64e Revert changelog conversion since GFM is not supported there d6047cdc4 Update changelog 810241b36 Convert changlog to markdown 661c47473 Rename changelog 7c33059fa Update ChangeLog.rst 9e20883ab Update README.rst 41899d522 Update changelog f42f45908 Update changelog 2381df654 Update readme 7ae816563 Update README.rst c56cf3d07 Update changelog and readme 01309a34a Deprecate arg_formatter a62d06055 Update changelog 23e3a2eee Update changelog d8e0554b9 Disable numeric formatting by default 1e8eea4f4 Update changelog 44bd5384a Fix formatting 20e19387a Update changelog 56fed7814 FMT_NUMERIC_ALIGN -> FMT_DEPRECATED_NUMERIC_ALIGN 56e63078f Make the n specifier an opt-in 31ce6bc70 Fix a conversion warning with Clang10 on Windows (#1750) c9c5b90da Fix a typo. Thanks Tracy Chapman from TripleChecker 1f3f84631 Fix a typo 5de62af60 Fix possible infinite recursion in FMT_ASSERT (#1744) cbddab2fe Use consistent include style f69b6eaab Add a simple buffered stream with no sync ba363b3a2 Use digit pairs as in unrolledlut a6f8e7d86 Update changelog e753244ab Update changelog 98a7a8b40 Update changelog and disable internal 3135d95fd Don't use non-portable attribute 8630a8f5f Tweak the docs cc3a88e6b Extract docs from compile.h 79c4b6bd7 Apply clang-format d130ee070 Document format string compilation d0f90b5be Spelling fixes 6e080660d Update README.rst 31c3a2426 Spelling fixes 613b3b459 Spelling fixes 978521bb8 Fix a compile error introduced in #1738 4e94c649f Deprecate compile 1a83443e6 Add user-defined type support to compilation 8bef1c3b3 Tweaks for EDG based compilers (Intel, nVidia, MCST/Elbrus, etc). b287c37c6 Do not use -Wl,--as-needed with emscripten. 2cac8a9d2 Reintroduce UDT support to fmt::to_string and test ADL 9a4cc8842 Add FMT_COMPILE support to format_to 5ddf9ee1b Streamline default FP formatting 0b3a83f7f Update README.rst 5aa5c9873 Added #define WIN32_LEAN_AND_MEAN before including windows.h (#1729) 397ad1bec Optimize common case 7431165f3 Make to_string bypass format ee4d4c7fd Inline compiled format ab2f8484e Finish text::format e900d735b Re-enable assert in format_decimal f4de7b684 Fix ambiguity 1f8f5450b Reuse format_decimal d702a68df Fix formatting of bool with FMT_COMPILE and add more tests e956a14e9 Use write instead of format_int in to_string 98dcc251e Undo branching reduction 5b8641ddd Undo branching reduction 8c88abde6 Fix sign handling in 'L' 23b976a61 Reduce branching 9edee0e72 Optimize small string parsing a909d42b7 Fix a warning 16637341b Enable compilation for all types 2d71d7e03 Add a simple format string compilation API d259fcfb0 Tweak comments 704ed557a Move project in order to solve a CMake warning 8603bd20d Update README.rst 547f12ae6 Fix a warning (#1722) f904e8a1b c++11 use formatting user-defined types (#1721) 100e8af08 Update README.rst c11d0f056 Update README.rst 2453ee576 Improve default formatting 47ae52155 MINGW cross compiler fixes 936a1833c Add default_arg_formatter f2c9cb624 Fix a UB d3107f855 Cleanup arg_formatter_base 5e7c70e20 Simplify arg_formatter_base 38cc68b3e Inline visitor 6732ea500 Make symbols readable 57ddc77ce Make advance_to a noop for back_insert_iterator 50bad7d62 Optimize format string parsing 8f7a824e4 Inline visit f11e96870 Optimize format string parsing 09737dd83 Optimize format handler d9e3d6e6e Move format_handler to detail 795b47a7b Fix a warning (#1712) 95c6ac0cc fix typo which caused the loss of the counting information when using a printf context with a truncating_iterator 21409cfdd Fix warnings 88c8d534e Move digits10 to where they belong and add comments 0f3eaeac0 Fix a warning 344218510 Ignore /doc/node_modules directory 16aec0617 Cleanup arg_formatter_base 1e1193590 Fix format_decimal overloads 0893c9c2e Inline parse_format_string 3245145a4 Remove undocumented buffer_range and output_range 57fc44907 Increase VM disk size 7d22bebb6 Remove uses of buffer_range 8f2b5fe74 Don't install sphinx cache files f095c67b6 Remove uses of buffer_range 5aabf1f71 Simplify copy_str 19c5b5d15 Simplify arg_formatter 519571ede Simplify arg_formatter_base ac8dfd841 Improve handling of separators 2c6165a22 Reduce the number of comparisons 28639969e Use memcpy for copying digits f5fa1dee5 Support custom FMT_INC_DIR in pkgconfig and cmake configs (#1702) 51bf9cfac Fix Mingw support 1a716caf5 Optimize common case 98d4bbf81 Update README.rst 8c8f74a87 fix zero flag for char types and make zero flag ignored if a precision is specified bc1b89da2 Temporarily revert parsing changes a7fb321ac Remove a redundant branch 8cadb9650 fix max/min macro (#1697) 297c3b2ed Fix an example (thanks Alexey Kuzmenko) 943532fec Make ostream formatter work with compile-time format strings (#1692) bd8804019 Update README.rst f230300ac Knuth is using fmt library (#1691) a265e25b7 Optimize small string parsing 2aa2526f6 Optimize small string concatenation 8d78045e7 Move void_t to where it's used 7aafa6bc6 Update analytics c66aae165 Adding sentinel support to fmt::join(). (#1689) 6d66de380 Add c specifier support to integral types (#1652) 6b219a58d fix interaction of space flag and '+' flag, as well as '-' flag and '0' flag (#1687) eee2023c2 Update signatures c5ed73aab Add fmt::detail::buffer to the docs (#704) ea1cd9638 Fix apidoc d3964d7b1 Merge branch 'master' of github.com:fmtlib/fmt d18c6723a Update docs 96c18b26c make plus flag for printf not be ignored for char argument (#1683) ba25baeb9 Apply doc patch to 6.2.1 981b517cc nested replacement fields may omit arg_id (#1681) 922ea924b Make dynamic_format_arg_store reusable and add reserve() (#1677) e0d98923c Update version 806926537 internal -> detail (#1538) 963ee0831 Simplify named arguments 02a6fe59f Named arguments go brrr de290f5c4 Ditch internal::arg_map d0623de51 Bump version 73e335ed3 Make implicit capture explicit for C++20 (#1669) b4d46e398 Update changelog a182f7341 Update changelog 68201831a Support named args in dynamic_format_arg_store (#1655). (#1663) 7f723fbcb Consistently namespace qualify size_t c06851456 Purge basic_writer 2f05054dd Purge basic_writer f0ce21164 Revert enum change 44639b11f Fix some warnings (#1667) 1c86a99e8 Purge basic_writer 8f511fc12 Make copyfmt not throw (#1666) 59fe455f3 Remove compatibility stubs b0f47a13e Separate nonfinite formatting d6cea50d0 Remove deprecated APIs 40bc7163f Move FMT_MAYBE_UNUSED to where it's actually used 080e44d0b Fix inconsistent type detection (#1662) 7e57cace5 Exclude std::abort from compilation when compiling CUDA with Clang (#1661) 7b66e2f21 Inherit arg_formatter_base from basic_writer bab3f5800 Refactor pointer formatting 9cc7edfdd Move int_writer to the namespace scope 8d9d528bf Improve handling of alignment 8efd1a8ef Improve handling of alignment a71bc9c82 Use '0' fill with numeric align for consistency with std::format 60d85d598 Suppress ubsan warning c3099beb6 Cleanup cbb4cb899 Remove undocumented deprecated APIs b85e9ac38 Simplify vformat_to e3710ab97 FMT_CONSTEXPR -> constexpr d59751f0f Update date formatting example to use threadsafe localtime d6abb2fa0 Reduce library size e9fdea90b Update README.rst 44b6584f2 Update README.rst 78f041ab5 build: Fix installation paths 7ca89bf87 Reduce template bloat in write_int 3c114d091 Fix a shadowing warning (#1658) e2ef12a8c Allow to avoid inclusion of os.cc in fmt target bca82719a Pass iterator by value 99da38962 Make write_padded non-members f19d66794 Bump fuzzer allocation limit 3e6984761 Reduce branching in write_padded 9ac1eebd4 Reduce library size e2ff91067 Replace FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION with fmt-specific macro (#1650) f2ed03b91 Fix a warning (#1649) 9dde9f013 Reduce library size b1af642d1 Reduce library size 4a617f25c Clarify encoding conversion in chrono 6f435f55c Improve compile time by using extern template (#1452) cb475cb88 Clarify why we don't check argument id 1e1ac6e96 Check dynamic width/precision id at compile time (#1614) e51c449fe Revert "Check dynamic widht/precision id at compile time (#1614)" 0463665ef Don't access a C string past precision in printf (#1595) 7d748a6f8 Check dynamic widht/precision id at compile time (#1614) 2b75bd7ce Get rid of do_check_format_string 4a1d5931c Simplify udl_formatter with FMT_STRING 811b0f905 Enable compile-time error tests 450e8eed9 Fix markup b8fbcec1b Clarify formatter reuse 56bc86ffa Suppress bogus MSVC analysis warnings 3f79357ef Fix a recent regression in handling max packed arguments 8a11148f9 Add Facebook Folly to the list of projects e371e8b68 Tweak readme 813732fed Improve readme formatting 3670d5b3f README: add vectorized.io/redpanda in the list of users 9e2ad7cf6 Add windows terminal to the projects using {fmt} 63479c851 Use a delegating ctor and add inlines 5944fcad3 Remove remaining wchar_t instantiation e253b371b Don't generate RTTI for allocator 0c86f467b Fix build on ancient gcc 1929df4bc Simplify format_args a13822181 Always inline arg_data functions 04e0dfd4b Always inline value ctors 04cde756b Simplify checks c9a57b9a8 Fix incorrect assumptions about nul termination f46f5ecaf Reenable constexpr _compile on GCC 9 6e8d7e277 Don't use constexpr on Intel compiler (#1628) 567ed03f8 Merge arg overloads and cleanup c3fa33314 Remove warning in core.h with when compiling with gcc and -Wshadow 84898b462 Remove warning in format.h when compiling with gcc and -Wshadow 538d83fd0 Cleanup named arguments 8a4630686 Improve handling of named arguments a9d62d3f3 Add check for CompiledFormat to avoid ambiguous call fdcf7870a Add stack-based named argument storage 5899267c4 Fix a clang-tidy warning 07b4c246e Fix a typo e99809f29 Fix ostream support in sprintf (#1631) 3cd5179f3 Fixed clang tidy warning -multiple declarations in a single statement reduces readability 7404e33a7 Fix clang warning about explicit ctor 3aab2171e Clean up basic_format_args 7645ca072 Clean up printf e30d8391e Suppress an MSVC warning (#1622) 8cd8ef03e Simplify warning suppression bbb6b357c Add floating-point L specifier (#1624) 36ea32640 Suppress a bogus MSVC warning 141a00d64 Define FMT_EXTERN_TEMPLATE_API on export 3860edc5d Bump version 7d01859ef Fix handling of unsigned char strings in printf 63b23e786 Merge branch 'master' of github.com:fmtlib/fmt 4999796c1 Fix the docs 34b3f7b7a Avoid windows issue with min() max() macros 27e3c0fe9 Update signature in the docs git-subtree-dir: externals/fmt git-subtree-split: cd4af11efc9c622896a3e4cb599fa28668ca3d05
665 lines
22 KiB
C++
665 lines
22 KiB
C++
// Formatting library for C++ - experimental format string compilation
|
|
//
|
|
// Copyright (c) 2012 - present, Victor Zverovich and fmt contributors
|
|
// All rights reserved.
|
|
//
|
|
// For the license information refer to format.h.
|
|
|
|
#ifndef FMT_COMPILE_H_
|
|
#define FMT_COMPILE_H_
|
|
|
|
#include <vector>
|
|
|
|
#include "format.h"
|
|
|
|
FMT_BEGIN_NAMESPACE
|
|
namespace detail {
|
|
|
|
// 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> {};
|
|
|
|
/**
|
|
\rst
|
|
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.
|
|
|
|
**Example**::
|
|
|
|
// Converts 42 into std::string using the most efficient method and no
|
|
// runtime format string processing.
|
|
std::string s = fmt::format(FMT_COMPILE("{}"), 42);
|
|
\endrst
|
|
*/
|
|
#define FMT_COMPILE(s) FMT_STRING_IMPL(s, fmt::detail::compiled_string)
|
|
|
|
template <typename T, typename... Tail>
|
|
const T& first(const T& value, const Tail&...) {
|
|
return value;
|
|
}
|
|
|
|
// Part of a compiled format string. It can be either literal text or a
|
|
// replacement field.
|
|
template <typename Char> struct format_part {
|
|
enum class kind { arg_index, arg_name, text, replacement };
|
|
|
|
struct replacement {
|
|
arg_ref<Char> arg_id;
|
|
dynamic_format_specs<Char> specs;
|
|
};
|
|
|
|
kind part_kind;
|
|
union value {
|
|
int arg_index;
|
|
basic_string_view<Char> str;
|
|
replacement repl;
|
|
|
|
FMT_CONSTEXPR value(int 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;
|
|
|
|
FMT_CONSTEXPR format_part(kind k = kind::arg_index, value v = {})
|
|
: part_kind(k), val(v) {}
|
|
|
|
static FMT_CONSTEXPR format_part make_arg_index(int 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);
|
|
}
|
|
};
|
|
|
|
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;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
FMT_CONSTEXPR void on_replacement_field(int, const Char*) {}
|
|
|
|
FMT_CONSTEXPR const Char* on_format_specs(int, 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;
|
|
}
|
|
|
|
FMT_CONSTEXPR void on_error(const char*) {}
|
|
};
|
|
|
|
// 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>;
|
|
|
|
PartHandler handler_;
|
|
part part_;
|
|
basic_string_view<Char> format_str_;
|
|
basic_format_parse_context<Char> parse_context_;
|
|
|
|
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)
|
|
handler_(part::make_text({begin, to_unsigned(end - begin)}));
|
|
}
|
|
|
|
FMT_CONSTEXPR int on_arg_id() {
|
|
part_ = part::make_arg_index(parse_context_.next_arg_id());
|
|
return 0;
|
|
}
|
|
|
|
FMT_CONSTEXPR int on_arg_id(int id) {
|
|
parse_context_.check_arg_id(id);
|
|
part_ = part::make_arg_index(id);
|
|
return 0;
|
|
}
|
|
|
|
FMT_CONSTEXPR int on_arg_id(basic_string_view<Char> id) {
|
|
part_ = part::make_arg_name(id);
|
|
return 0;
|
|
}
|
|
|
|
FMT_CONSTEXPR void on_replacement_field(int, const Char* ptr) {
|
|
part_.arg_id_end = ptr;
|
|
handler_(part_);
|
|
}
|
|
|
|
FMT_CONSTEXPR const Char* on_format_specs(int, const Char* begin,
|
|
const Char* end) {
|
|
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;
|
|
}
|
|
};
|
|
|
|
// 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));
|
|
}
|
|
|
|
template <typename OutputIt, typename Context, typename Id>
|
|
void format_arg(
|
|
basic_format_parse_context<typename Context::char_type>& parse_ctx,
|
|
Context& ctx, Id arg_id) {
|
|
ctx.advance_to(visit_format_arg(
|
|
arg_formatter<OutputIt, typename Context::char_type>(ctx, &parse_ctx),
|
|
ctx.arg(arg_id)));
|
|
}
|
|
|
|
// vformat_to is defined in a subnamespace to prevent ADL.
|
|
namespace cf {
|
|
template <typename Context, typename OutputIt, typename CompiledFormat>
|
|
auto vformat_to(OutputIt 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, 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;
|
|
}
|
|
|
|
case format_part_t::kind::arg_index:
|
|
advance_to(parse_ctx, part.arg_id_end);
|
|
detail::format_arg<OutputIt>(parse_ctx, ctx, value.arg_index);
|
|
break;
|
|
|
|
case format_part_t::kind::arg_name:
|
|
advance_to(parse_ctx, part.arg_id_end);
|
|
detail::format_arg<OutputIt>(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<OutputIt, typename Context::char_type>(
|
|
ctx, nullptr, &specs),
|
|
arg));
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
return ctx.out();
|
|
}
|
|
} // namespace cf
|
|
|
|
struct basic_compiled_format {};
|
|
|
|
template <typename S, typename = void>
|
|
struct compiled_format_base : basic_compiled_format {
|
|
using char_type = char_t<S>;
|
|
using parts_container = std::vector<detail::format_part<char_type>>;
|
|
|
|
parts_container compiled_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);
|
|
});
|
|
}
|
|
|
|
const parts_container& parts() const { return compiled_parts; }
|
|
};
|
|
|
|
template <typename Char, unsigned N> struct format_part_array {
|
|
format_part<Char> data[N] = {};
|
|
FMT_CONSTEXPR format_part_array() = default;
|
|
};
|
|
|
|
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;
|
|
}
|
|
} 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;
|
|
}
|
|
|
|
template <typename T> constexpr const T& constexpr_max(const T& a, const T& b) {
|
|
return (a < b) ? b : a;
|
|
}
|
|
|
|
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>;
|
|
|
|
FMT_CONSTEXPR explicit compiled_format_base(basic_string_view<char_type>) {}
|
|
|
|
// 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
|
|
|
|
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>(
|
|
detail::to_string_view(S()));
|
|
return compiled_parts.data;
|
|
}
|
|
};
|
|
|
|
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_;
|
|
|
|
template <typename Context, typename OutputIt, typename CompiledFormat>
|
|
friend auto cf::vformat_to(OutputIt out, CompiledFormat& cf,
|
|
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) {}
|
|
};
|
|
|
|
#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...);
|
|
}
|
|
|
|
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;
|
|
|
|
template <typename T> struct is_compiled_format : std::false_type {};
|
|
|
|
template <typename Char> struct text {
|
|
basic_string_view<Char> data;
|
|
using char_type = Char;
|
|
|
|
template <typename OutputIt, typename... Args>
|
|
OutputIt format(OutputIt out, const Args&...) const {
|
|
return write<Char>(out, data);
|
|
}
|
|
};
|
|
|
|
template <typename Char>
|
|
struct is_compiled_format<text<Char>> : std::true_type {};
|
|
|
|
template <typename Char>
|
|
constexpr text<Char> make_text(basic_string_view<Char> s, size_t pos,
|
|
size_t size) {
|
|
return {{&s[pos], size}};
|
|
}
|
|
|
|
// A replacement field that refers to argument N.
|
|
template <typename Char, typename T, int N> struct field {
|
|
using char_type = Char;
|
|
|
|
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 write<Char>(out, arg);
|
|
}
|
|
};
|
|
|
|
template <typename Char, typename T, int N>
|
|
struct is_compiled_format<field<Char, T, N>> : std::true_type {};
|
|
|
|
// 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;
|
|
mutable formatter<T, Char> fmt;
|
|
|
|
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...);
|
|
basic_format_context<OutputIt, Char> ctx(out, {});
|
|
return fmt.format(arg, ctx);
|
|
}
|
|
};
|
|
|
|
template <typename Char, typename T, int N>
|
|
struct is_compiled_format<spec_field<Char, T, N>> : std::true_type {};
|
|
|
|
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 L, typename R>
|
|
struct is_compiled_format<concat<L, R>> : std::true_type {};
|
|
|
|
template <typename L, typename R>
|
|
constexpr concat<L, R> make_concat(L lhs, R rhs) {
|
|
return {lhs, rhs};
|
|
}
|
|
|
|
struct unknown_format {};
|
|
|
|
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 !=
|
|
basic_string_view<typename S::char_type>(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;
|
|
}
|
|
}
|
|
|
|
template <typename T, typename Char> struct parse_specs_result {
|
|
formatter<T, Char> fmt;
|
|
size_t end;
|
|
};
|
|
|
|
template <typename T, typename Char>
|
|
constexpr parse_specs_result<T, Char> parse_specs(basic_string_view<Char> str,
|
|
size_t pos) {
|
|
str.remove_prefix(pos);
|
|
auto ctx = basic_format_parse_context<Char>(str);
|
|
auto f = formatter<T, Char>();
|
|
auto end = f.parse(ctx);
|
|
return {f, pos + (end - str.data()) + 1};
|
|
}
|
|
|
|
// 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>;
|
|
return parse_tail<Args, POS + 2, ID + 1>(field<char_type, type, ID>(),
|
|
format_str);
|
|
} else if constexpr (str[POS + 1] == ':') {
|
|
using type = get_type<ID, Args>;
|
|
constexpr auto result = parse_specs<type>(str, POS + 2);
|
|
return parse_tail<Args, result.end, ID + 1>(
|
|
spec_field<char_type, type, ID>{result.fmt}, format_str);
|
|
} 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);
|
|
}
|
|
}
|
|
|
|
template <typename... Args, typename S,
|
|
FMT_ENABLE_IF(is_compile_string<S>::value ||
|
|
detail::is_compiled_string<S>::value)>
|
|
constexpr auto compile(S format_str) {
|
|
constexpr basic_string_view<typename S::char_type> str = format_str;
|
|
if constexpr (str.size() == 0) {
|
|
return detail::make_text(str, 0, 0);
|
|
} else {
|
|
constexpr auto result =
|
|
detail::compile_format_string<detail::type_list<Args...>, 0, 0>(
|
|
format_str);
|
|
if constexpr (std::is_same<remove_cvref_t<decltype(result)>,
|
|
detail::unknown_format>()) {
|
|
return detail::compiled_format<S, Args...>(to_string_view(format_str));
|
|
} else {
|
|
return result;
|
|
}
|
|
}
|
|
}
|
|
#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>
|
|
FMT_DEPRECATED auto compile(const Args&... args)
|
|
-> decltype(detail::compile(args...)) {
|
|
return detail::compile(args...);
|
|
}
|
|
|
|
#if FMT_USE_CONSTEXPR
|
|
# ifdef __cpp_if_constexpr
|
|
|
|
template <typename CompiledFormat, typename... Args,
|
|
typename Char = typename CompiledFormat::char_type,
|
|
FMT_ENABLE_IF(detail::is_compiled_format<CompiledFormat>::value)>
|
|
FMT_INLINE std::basic_string<Char> format(const CompiledFormat& cf,
|
|
const Args&... args) {
|
|
basic_memory_buffer<Char> buffer;
|
|
detail::buffer<Char>& base = buffer;
|
|
cf.format(std::back_inserter(base), args...);
|
|
return to_string(buffer);
|
|
}
|
|
|
|
template <typename OutputIt, typename CompiledFormat, typename... Args,
|
|
FMT_ENABLE_IF(detail::is_compiled_format<CompiledFormat>::value)>
|
|
OutputIt format_to(OutputIt out, const CompiledFormat& cf,
|
|
const Args&... args) {
|
|
return cf.format(out, args...);
|
|
}
|
|
# endif // __cpp_if_constexpr
|
|
#endif // FMT_USE_CONSTEXPR
|
|
|
|
template <typename CompiledFormat, typename... Args,
|
|
typename Char = typename CompiledFormat::char_type,
|
|
FMT_ENABLE_IF(std::is_base_of<detail::basic_compiled_format,
|
|
CompiledFormat>::value)>
|
|
std::basic_string<Char> format(const CompiledFormat& cf, const Args&... args) {
|
|
basic_memory_buffer<Char> buffer;
|
|
using context = buffer_context<Char>;
|
|
detail::buffer<Char>& base = buffer;
|
|
detail::cf::vformat_to<context>(std::back_inserter(base), cf,
|
|
make_format_args<context>(args...));
|
|
return to_string(buffer);
|
|
}
|
|
|
|
template <typename S, typename... Args,
|
|
FMT_ENABLE_IF(detail::is_compiled_string<S>::value)>
|
|
FMT_INLINE std::basic_string<typename S::char_type> format(const S&,
|
|
Args&&... args) {
|
|
constexpr basic_string_view<typename S::char_type> str = S();
|
|
if (str.size() == 2 && str[0] == '{' && str[1] == '}')
|
|
return fmt::to_string(detail::first(args...));
|
|
constexpr auto compiled = detail::compile<Args...>(S());
|
|
return format(compiled, std::forward<Args>(args)...);
|
|
}
|
|
|
|
template <typename OutputIt, typename CompiledFormat, typename... Args,
|
|
FMT_ENABLE_IF(std::is_base_of<detail::basic_compiled_format,
|
|
CompiledFormat>::value)>
|
|
OutputIt format_to(OutputIt out, const CompiledFormat& cf,
|
|
const Args&... args) {
|
|
using char_type = typename CompiledFormat::char_type;
|
|
using context = format_context_t<OutputIt, char_type>;
|
|
return detail::cf::vformat_to<context>(out, cf,
|
|
make_format_args<context>(args...));
|
|
}
|
|
|
|
template <typename OutputIt, typename S, typename... Args,
|
|
FMT_ENABLE_IF(detail::is_compiled_string<S>::value)>
|
|
OutputIt format_to(OutputIt out, const S&, const Args&... args) {
|
|
constexpr auto compiled = detail::compile<Args...>(S());
|
|
return format_to(out, compiled, args...);
|
|
}
|
|
|
|
template <
|
|
typename OutputIt, typename CompiledFormat, typename... Args,
|
|
FMT_ENABLE_IF(detail::is_output_iterator<OutputIt>::value&& std::is_base_of<
|
|
detail::basic_compiled_format, CompiledFormat>::value)>
|
|
format_to_n_result<OutputIt> format_to_n(OutputIt out, size_t n,
|
|
const CompiledFormat& cf,
|
|
const Args&... args) {
|
|
auto it =
|
|
format_to(detail::truncating_iterator<OutputIt>(out, n), cf, args...);
|
|
return {it.base(), it.count()};
|
|
}
|
|
|
|
template <typename CompiledFormat, typename... Args>
|
|
size_t formatted_size(const CompiledFormat& cf, const Args&... args) {
|
|
return format_to(detail::counting_iterator(), cf, args...).count();
|
|
}
|
|
|
|
FMT_END_NAMESPACE
|
|
|
|
#endif // FMT_COMPILE_H_
|