093b39ca5 Update docs for meson (#4291) 2c3a5698e Simplify a copying the fill from basic_specs fc1b0f348 Clarify use of FMT_THROW in a comment 1d066890c Resolve C4702 unreachable code warnings dad323751 Fix a bug when copying the fill from basic_specs 880e1494d Improve xchar support for std::bitset formatter e3ddede6c Update version e9ec4fdc8 Bump version feb72126b Readd FMT_NO_UNIQUE_ADDRESS 8d517e54c Update changelog 563fc74ae Update changelog 3e04222d5 Restore ABI compatibility with 11.0.2 853df39d0 Mention compile-time formatting 11742a09c Clarify that format_string should be used instead of fstring da24fac10 Document fstring 5fa4bdd75 Define CMake components to allow docs to be installed separately (#4276) 3c8aad8df Update the release script 0e8aad961 Update version debe784aa Update changelog f6d112567 Update changelog 73d0d3f75 Fix github API call 08f60f1ef Update changelog faf3f8408 Bump version f3a41441d Replace requests with urllib 3f33cb21d Update changelog b07a90386 Update changelog a6fba5177 Update changelog 25e292998 Update changelog 00ab2e98b Update changelog a3ef285ae Always inline const_check to improve debug codegen in clang 28d1abc9d Update changelog 90704b9ef Update changelog 86dae01c2 Fix compatibility with older versions of VS (#4271) d8a79eafd Document formatting of bit-fields and fields of packed structs 7c3d0152e Use the _MSVC_STL_UPDATE macro to detect STL (#4267) 7c50da538 Allow getting size of dynamic format arg store (#4270) 873670ba3 Make parameter basic_memory_buffer<char, SIZE>& buf of to_string const 735d4cc05 Update changelog 141380172 Allow disabling <filesystem> by define FMT_CPP_LIB_FILESYSTEM=0 (#4259) 4302d7429 Update changelog 0f51ea79d Update changelog 9600fee02 Include <filesystem> only if FMT_CPP_LIB_FILESYSTEM is set (#4258) 47a66c5ec Bump msys2/setup-msys2 from 2.24.0 to 2.25.0 (#4250) 385c01dc7 Allow bit_cast to work for 80bit long double (#4246) df249d8ad Remove an old workaround dfad80d1c Remove an old workaround 536cabd56 Export all range join overloads (#4239) b1a054706 Remove more MSVC 2015 workarounds and fix string_view checks bfd95392c Remove MSVC 2015 workaround 9ced61bca Replace std::forward for clang-tidy (#4236) 75e5be6ad Sort specifiers a169d7fa4 Fix chrono formatting syntax doc (#4235) a6c45dfea Fix modular build a35389b3c Corrently handle buffer flush 5a3576acc Implement fmt::join for tuple-like objects (#4230) 542600013 Suppress MSVC warnings "C4127: conditional expression is constant" by used const_check (#4233) 720da57ba Remove reference to unused intrinsic 680db66c3 Explicitly export symbols from detail 56ce41ef6 Remove initializer_list dependency cf50e4d6a Fix const[expr] in context API 6580d7b80 Cleanup the format API 7e73566ce Minor cleanup 8523dba2d Make constexpr precede explicit consistently e3d3b24fc Minor cleanup 1521bba70 Use consistent types for argument count 00649552a Bump github/codeql-action from 3.26.6 to 3.27.0 (#4223) 4b8e2838f More cleanup 7d4662f7a Remove FMT_BUILTIN_CTZ 27110bc47 Minor cleanup 68f315376 Fix narrowing conversion warning in struct fstring (#4210) 168df9a06 Implement fmt::format_to into std::vector<char> (#4211) 4daa3d591 Fix error: cannot use 'try' with exceptions disabled in Win LLVM Clang (#4208) e9eaa27e5 Add std::exception to the docs 2b6a786e3 Use standard context in print a16ff5787 Add support for code units > 0xFFFF in fill 601be1cbe Add support for code units > 0xFFFF in fill 58c185b63 Changing type of data_ to size_t to avoid compilation warnings (#4200) a0a9ba2af Fix hashes cc2ba8f9e Cleanup cifuzz action a18d42b20 Simplify lint (#4197) 4046f9727 Fix -Wmissing-noreturn warning (#4194) 6bdc12a19 detail_exported -> detail 786a4b096 Cleanup fixed_string 2cb3b7c64 Update README.md e9cba6905 Update README.md 02537548f Cleanup an example c68c5fa7c Test FMT_BUILTIN_TYPES 22701d5f6 Address build failures when using Tip-of-Tree clang. (#4187) e62c41ffb Conform `std::iterator_traits<fmt::appender>` to [iterator.traits]/1 (#4185) 18792893d Silencing Wextra-semi warning (#4188) c90bc9186 Bump actions/checkout from 4.1.6 to 4.2.0 (#4182) c95722ad6 Improve naming consistency db06b0df8 Use countl_zero in bigint b9ec48d9c Cleanup bigint 3faf6f181 Add min_of/max_of d64b100a3 Relax constexpr ff9ee0461 Fix handling FMT_BUILTIN_TYPES 1c5883bef Test nondeterministic conversion to format string cacc3108c Don't assume repeated evaluation of string literal produce the same pointer fade652ad Require clang >=15 for _BitInt support (#4176) 96dca569a Module linkage fixes for shared build (#4169) 891c9a73a Cleanup format API 9282222b7 Export more e5b20ff0d Deprecate detail::locale_ref ff9222354 Simplify locale handling 80c4d42c6 Cleanup format.h git-subtree-dir: external/fmt git-subtree-split: 093b39ca5eea129b111060839602bcfaf295125a
552 lines
19 KiB
C++
552 lines
19 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_
|
|
|
|
#ifndef FMT_MODULE
|
|
# include <iterator> // std::back_inserter
|
|
#endif
|
|
|
|
#include "format.h"
|
|
|
|
FMT_BEGIN_NAMESPACE
|
|
|
|
// A compile-time string which is compiled into fast formatting code.
|
|
FMT_EXPORT class compiled_string {};
|
|
|
|
namespace detail {
|
|
|
|
template <typename S>
|
|
struct is_compiled_string : std::is_base_of<compiled_string, S> {};
|
|
|
|
/**
|
|
* 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);
|
|
*/
|
|
#if defined(__cpp_if_constexpr) && defined(__cpp_return_type_deduction)
|
|
# define FMT_COMPILE(s) FMT_STRING_IMPL(s, fmt::compiled_string)
|
|
#else
|
|
# define FMT_COMPILE(s) FMT_STRING(s)
|
|
#endif
|
|
|
|
#if FMT_USE_NONTYPE_TEMPLATE_ARGS
|
|
template <typename Char, size_t N, fmt::detail::fixed_string<Char, N> Str>
|
|
struct udl_compiled_string : compiled_string {
|
|
using char_type = Char;
|
|
constexpr explicit operator basic_string_view<char_type>() const {
|
|
return {Str.data, N - 1};
|
|
}
|
|
};
|
|
#endif
|
|
|
|
template <typename T, typename... Tail>
|
|
auto first(const T& value, const Tail&...) -> const T& {
|
|
return value;
|
|
}
|
|
|
|
#if defined(__cpp_if_constexpr) && defined(__cpp_return_type_deduction)
|
|
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([[maybe_unused]] const T& first,
|
|
[[maybe_unused]] const Args&... rest) {
|
|
static_assert(N < 1 + sizeof...(Args), "index is out of bounds");
|
|
if constexpr (N == 0)
|
|
return first;
|
|
else
|
|
return detail::get<N - 1>(rest...);
|
|
}
|
|
|
|
# if FMT_USE_NONTYPE_TEMPLATE_ARGS
|
|
template <int N, typename T, typename... Args, typename Char>
|
|
constexpr auto get_arg_index_by_name(basic_string_view<Char> name) -> int {
|
|
if constexpr (is_static_named_arg<T>()) {
|
|
if (name == T::name) return N;
|
|
}
|
|
if constexpr (sizeof...(Args) > 0)
|
|
return get_arg_index_by_name<N + 1, Args...>(name);
|
|
(void)name; // Workaround an MSVC bug about "unused" parameter.
|
|
return -1;
|
|
}
|
|
# endif
|
|
|
|
template <typename... Args, typename Char>
|
|
FMT_CONSTEXPR auto get_arg_index_by_name(basic_string_view<Char> name) -> int {
|
|
# if FMT_USE_NONTYPE_TEMPLATE_ARGS
|
|
if constexpr (sizeof...(Args) > 0)
|
|
return get_arg_index_by_name<0, Args...>(name);
|
|
# endif
|
|
(void)name;
|
|
return -1;
|
|
}
|
|
|
|
template <typename Char, typename... Args>
|
|
constexpr int get_arg_index_by_name(basic_string_view<Char> name,
|
|
type_list<Args...>) {
|
|
return get_arg_index_by_name<Args...>(name);
|
|
}
|
|
|
|
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(detail::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>
|
|
constexpr 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}};
|
|
}
|
|
|
|
template <typename Char> struct code_unit {
|
|
Char value;
|
|
using char_type = Char;
|
|
|
|
template <typename OutputIt, typename... Args>
|
|
constexpr OutputIt format(OutputIt out, const Args&...) const {
|
|
*out++ = value;
|
|
return out;
|
|
}
|
|
};
|
|
|
|
// This ensures that the argument type is convertible to `const T&`.
|
|
template <typename T, int N, typename... Args>
|
|
constexpr const T& get_arg_checked(const Args&... args) {
|
|
const auto& arg = detail::get<N>(args...);
|
|
if constexpr (detail::is_named_arg<remove_cvref_t<decltype(arg)>>()) {
|
|
return arg.value;
|
|
} else {
|
|
return arg;
|
|
}
|
|
}
|
|
|
|
template <typename Char>
|
|
struct is_compiled_format<code_unit<Char>> : std::true_type {};
|
|
|
|
// 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>
|
|
constexpr OutputIt format(OutputIt out, const Args&... args) const {
|
|
const T& arg = get_arg_checked<T, N>(args...);
|
|
if constexpr (std::is_convertible<T, basic_string_view<Char>>::value) {
|
|
auto s = basic_string_view<Char>(arg);
|
|
return copy<Char>(s.begin(), s.end(), out);
|
|
} else {
|
|
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 with name.
|
|
template <typename Char> struct runtime_named_field {
|
|
using char_type = Char;
|
|
basic_string_view<Char> name;
|
|
|
|
template <typename OutputIt, typename T>
|
|
constexpr static bool try_format_argument(
|
|
OutputIt& out,
|
|
// [[maybe_unused]] due to unused-but-set-parameter warning in GCC 7,8,9
|
|
[[maybe_unused]] basic_string_view<Char> arg_name, const T& arg) {
|
|
if constexpr (is_named_arg<typename std::remove_cv<T>::type>::value) {
|
|
if (arg_name == arg.name) {
|
|
out = write<Char>(out, arg.value);
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
template <typename OutputIt, typename... Args>
|
|
constexpr OutputIt format(OutputIt out, const Args&... args) const {
|
|
bool found = (try_format_argument(out, name, args) || ...);
|
|
if (!found) {
|
|
FMT_THROW(format_error("argument with specified name is not found"));
|
|
}
|
|
return out;
|
|
}
|
|
};
|
|
|
|
template <typename Char>
|
|
struct is_compiled_format<runtime_named_field<Char>> : 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;
|
|
formatter<T, Char> fmt;
|
|
|
|
template <typename OutputIt, typename... Args>
|
|
constexpr FMT_INLINE OutputIt format(OutputIt out,
|
|
const Args&... args) const {
|
|
const auto& vargs =
|
|
fmt::make_format_args<basic_format_context<OutputIt, Char>>(args...);
|
|
basic_format_context<OutputIt, Char> ctx(out, vargs);
|
|
return fmt.format(get_arg_checked<T, N>(args...), 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>
|
|
constexpr 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 fmt);
|
|
|
|
template <typename Args, size_t POS, int ID, typename T, typename S>
|
|
constexpr auto parse_tail(T head, S fmt) {
|
|
if constexpr (POS != basic_string_view<typename S::char_type>(fmt).size()) {
|
|
constexpr auto tail = compile_format_string<Args, POS, ID>(fmt);
|
|
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;
|
|
int next_arg_id;
|
|
};
|
|
|
|
enum { manual_indexing_id = -1 };
|
|
|
|
template <typename T, typename Char>
|
|
constexpr parse_specs_result<T, Char> parse_specs(basic_string_view<Char> str,
|
|
size_t pos, int next_arg_id) {
|
|
str.remove_prefix(pos);
|
|
auto ctx =
|
|
compile_parse_context<Char>(str, max_value<int>(), nullptr, next_arg_id);
|
|
auto f = formatter<T, Char>();
|
|
auto end = f.parse(ctx);
|
|
return {f, pos + fmt::detail::to_unsigned(end - str.data()),
|
|
next_arg_id == 0 ? manual_indexing_id : ctx.next_arg_id()};
|
|
}
|
|
|
|
template <typename Char> struct arg_id_handler {
|
|
arg_id_kind kind;
|
|
arg_ref<Char> arg_id;
|
|
|
|
constexpr int on_auto() {
|
|
FMT_ASSERT(false, "handler cannot be used with automatic indexing");
|
|
return 0;
|
|
}
|
|
constexpr int on_index(int id) {
|
|
kind = arg_id_kind::index;
|
|
arg_id = arg_ref<Char>(id);
|
|
return 0;
|
|
}
|
|
constexpr int on_name(basic_string_view<Char> id) {
|
|
kind = arg_id_kind::name;
|
|
arg_id = arg_ref<Char>(id);
|
|
return 0;
|
|
}
|
|
};
|
|
|
|
template <typename Char> struct parse_arg_id_result {
|
|
arg_id_kind kind;
|
|
arg_ref<Char> arg_id;
|
|
const Char* arg_id_end;
|
|
};
|
|
|
|
template <int ID, typename Char>
|
|
constexpr auto parse_arg_id(const Char* begin, const Char* end) {
|
|
auto handler = arg_id_handler<Char>{arg_id_kind::none, arg_ref<Char>{}};
|
|
auto arg_id_end = parse_arg_id(begin, end, handler);
|
|
return parse_arg_id_result<Char>{handler.kind, handler.arg_id, arg_id_end};
|
|
}
|
|
|
|
template <typename T, typename Enable = void> struct field_type {
|
|
using type = remove_cvref_t<T>;
|
|
};
|
|
|
|
template <typename T>
|
|
struct field_type<T, enable_if_t<detail::is_named_arg<T>::value>> {
|
|
using type = remove_cvref_t<decltype(T::value)>;
|
|
};
|
|
|
|
template <typename T, typename Args, size_t END_POS, int ARG_INDEX, int NEXT_ID,
|
|
typename S>
|
|
constexpr auto parse_replacement_field_then_tail(S fmt) {
|
|
using char_type = typename S::char_type;
|
|
constexpr auto str = basic_string_view<char_type>(fmt);
|
|
constexpr char_type c = END_POS != str.size() ? str[END_POS] : char_type();
|
|
if constexpr (c == '}') {
|
|
return parse_tail<Args, END_POS + 1, NEXT_ID>(
|
|
field<char_type, typename field_type<T>::type, ARG_INDEX>(), fmt);
|
|
} else if constexpr (c != ':') {
|
|
FMT_THROW(format_error("expected ':'"));
|
|
} else {
|
|
constexpr auto result = parse_specs<typename field_type<T>::type>(
|
|
str, END_POS + 1, NEXT_ID == manual_indexing_id ? 0 : NEXT_ID);
|
|
if constexpr (result.end >= str.size() || str[result.end] != '}') {
|
|
FMT_THROW(format_error("expected '}'"));
|
|
return 0;
|
|
} else {
|
|
return parse_tail<Args, result.end + 1, result.next_arg_id>(
|
|
spec_field<char_type, typename field_type<T>::type, ARG_INDEX>{
|
|
result.fmt},
|
|
fmt);
|
|
}
|
|
}
|
|
}
|
|
|
|
// 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 fmt) {
|
|
using char_type = typename S::char_type;
|
|
constexpr auto str = basic_string_view<char_type>(fmt);
|
|
if constexpr (str[POS] == '{') {
|
|
if constexpr (POS + 1 == str.size())
|
|
FMT_THROW(format_error("unmatched '{' in format string"));
|
|
if constexpr (str[POS + 1] == '{') {
|
|
return parse_tail<Args, POS + 2, ID>(make_text(str, POS, 1), fmt);
|
|
} else if constexpr (str[POS + 1] == '}' || str[POS + 1] == ':') {
|
|
static_assert(ID != manual_indexing_id,
|
|
"cannot switch from manual to automatic argument indexing");
|
|
constexpr auto next_id =
|
|
ID != manual_indexing_id ? ID + 1 : manual_indexing_id;
|
|
return parse_replacement_field_then_tail<get_type<ID, Args>, Args,
|
|
POS + 1, ID, next_id>(fmt);
|
|
} else {
|
|
constexpr auto arg_id_result =
|
|
parse_arg_id<ID>(str.data() + POS + 1, str.data() + str.size());
|
|
constexpr auto arg_id_end_pos = arg_id_result.arg_id_end - str.data();
|
|
constexpr char_type c =
|
|
arg_id_end_pos != str.size() ? str[arg_id_end_pos] : char_type();
|
|
static_assert(c == '}' || c == ':', "missing '}' in format string");
|
|
if constexpr (arg_id_result.kind == arg_id_kind::index) {
|
|
static_assert(
|
|
ID == manual_indexing_id || ID == 0,
|
|
"cannot switch from automatic to manual argument indexing");
|
|
constexpr auto arg_index = arg_id_result.arg_id.index;
|
|
return parse_replacement_field_then_tail<get_type<arg_index, Args>,
|
|
Args, arg_id_end_pos,
|
|
arg_index, manual_indexing_id>(
|
|
fmt);
|
|
} else if constexpr (arg_id_result.kind == arg_id_kind::name) {
|
|
constexpr auto arg_index =
|
|
get_arg_index_by_name(arg_id_result.arg_id.name, Args{});
|
|
if constexpr (arg_index >= 0) {
|
|
constexpr auto next_id =
|
|
ID != manual_indexing_id ? ID + 1 : manual_indexing_id;
|
|
return parse_replacement_field_then_tail<
|
|
decltype(get_type<arg_index, Args>::value), Args, arg_id_end_pos,
|
|
arg_index, next_id>(fmt);
|
|
} else if constexpr (c == '}') {
|
|
return parse_tail<Args, arg_id_end_pos + 1, ID>(
|
|
runtime_named_field<char_type>{arg_id_result.arg_id.name}, fmt);
|
|
} else if constexpr (c == ':') {
|
|
return unknown_format(); // no type info for specs parsing
|
|
}
|
|
}
|
|
}
|
|
} else if constexpr (str[POS] == '}') {
|
|
if constexpr (POS + 1 == str.size())
|
|
FMT_THROW(format_error("unmatched '}' in format string"));
|
|
return parse_tail<Args, POS + 2, ID>(make_text(str, POS, 1), fmt);
|
|
} else {
|
|
constexpr auto end = parse_text(str, POS + 1);
|
|
if constexpr (end - POS > 1) {
|
|
return parse_tail<Args, end, ID>(make_text(str, POS, end - POS), fmt);
|
|
} else {
|
|
return parse_tail<Args, end, ID>(code_unit<char_type>{str[POS]}, fmt);
|
|
}
|
|
}
|
|
}
|
|
|
|
template <typename... Args, typename S,
|
|
FMT_ENABLE_IF(detail::is_compiled_string<S>::value)>
|
|
constexpr auto compile(S fmt) {
|
|
constexpr auto str = basic_string_view<typename S::char_type>(fmt);
|
|
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>(fmt);
|
|
return result;
|
|
}
|
|
}
|
|
#endif // defined(__cpp_if_constexpr) && defined(__cpp_return_type_deduction)
|
|
} // namespace detail
|
|
|
|
FMT_BEGIN_EXPORT
|
|
|
|
#if defined(__cpp_if_constexpr) && defined(__cpp_return_type_deduction)
|
|
|
|
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) {
|
|
auto s = std::basic_string<Char>();
|
|
cf.format(std::back_inserter(s), args...);
|
|
return s;
|
|
}
|
|
|
|
template <typename OutputIt, typename CompiledFormat, typename... Args,
|
|
FMT_ENABLE_IF(detail::is_compiled_format<CompiledFormat>::value)>
|
|
constexpr FMT_INLINE OutputIt format_to(OutputIt out, const CompiledFormat& cf,
|
|
const Args&... args) {
|
|
return cf.format(out, args...);
|
|
}
|
|
|
|
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) {
|
|
if constexpr (std::is_same<typename S::char_type, char>::value) {
|
|
constexpr auto str = basic_string_view<typename S::char_type>(S());
|
|
if constexpr (str.size() == 2 && str[0] == '{' && str[1] == '}') {
|
|
const auto& first = detail::first(args...);
|
|
if constexpr (detail::is_named_arg<
|
|
remove_cvref_t<decltype(first)>>::value) {
|
|
return fmt::to_string(first.value);
|
|
} else {
|
|
return fmt::to_string(first);
|
|
}
|
|
}
|
|
}
|
|
constexpr auto compiled = detail::compile<Args...>(S());
|
|
if constexpr (std::is_same<remove_cvref_t<decltype(compiled)>,
|
|
detail::unknown_format>()) {
|
|
return fmt::format(
|
|
static_cast<basic_string_view<typename S::char_type>>(S()),
|
|
std::forward<Args>(args)...);
|
|
} else {
|
|
return fmt::format(compiled, std::forward<Args>(args)...);
|
|
}
|
|
}
|
|
|
|
template <typename OutputIt, typename S, typename... Args,
|
|
FMT_ENABLE_IF(detail::is_compiled_string<S>::value)>
|
|
FMT_CONSTEXPR OutputIt format_to(OutputIt out, const S&, Args&&... args) {
|
|
constexpr auto compiled = detail::compile<Args...>(S());
|
|
if constexpr (std::is_same<remove_cvref_t<decltype(compiled)>,
|
|
detail::unknown_format>()) {
|
|
return fmt::format_to(
|
|
out, static_cast<basic_string_view<typename S::char_type>>(S()),
|
|
std::forward<Args>(args)...);
|
|
} else {
|
|
return fmt::format_to(out, compiled, std::forward<Args>(args)...);
|
|
}
|
|
}
|
|
#endif
|
|
|
|
template <typename OutputIt, typename S, typename... Args,
|
|
FMT_ENABLE_IF(detail::is_compiled_string<S>::value)>
|
|
auto format_to_n(OutputIt out, size_t n, const S& fmt, Args&&... args)
|
|
-> format_to_n_result<OutputIt> {
|
|
using traits = detail::fixed_buffer_traits;
|
|
auto buf = detail::iterator_buffer<OutputIt, char, traits>(out, n);
|
|
fmt::format_to(std::back_inserter(buf), fmt, std::forward<Args>(args)...);
|
|
return {buf.out(), buf.count()};
|
|
}
|
|
|
|
template <typename S, typename... Args,
|
|
FMT_ENABLE_IF(detail::is_compiled_string<S>::value)>
|
|
FMT_CONSTEXPR20 auto formatted_size(const S& fmt, const Args&... args)
|
|
-> size_t {
|
|
auto buf = detail::counting_buffer<>();
|
|
fmt::format_to(appender(buf), fmt, args...);
|
|
return buf.count();
|
|
}
|
|
|
|
template <typename S, typename... Args,
|
|
FMT_ENABLE_IF(detail::is_compiled_string<S>::value)>
|
|
void print(std::FILE* f, const S& fmt, const Args&... args) {
|
|
auto buf = memory_buffer();
|
|
fmt::format_to(appender(buf), fmt, args...);
|
|
detail::print(f, {buf.data(), buf.size()});
|
|
}
|
|
|
|
template <typename S, typename... Args,
|
|
FMT_ENABLE_IF(detail::is_compiled_string<S>::value)>
|
|
void print(const S& fmt, const Args&... args) {
|
|
print(stdout, fmt, args...);
|
|
}
|
|
|
|
#if FMT_USE_NONTYPE_TEMPLATE_ARGS
|
|
inline namespace literals {
|
|
template <detail::fixed_string Str> constexpr auto operator""_cf() {
|
|
using char_t = remove_cvref_t<decltype(Str.data[0])>;
|
|
return detail::udl_compiled_string<char_t, sizeof(Str.data) / sizeof(char_t),
|
|
Str>();
|
|
}
|
|
} // namespace literals
|
|
#endif
|
|
|
|
FMT_END_EXPORT
|
|
FMT_END_NAMESPACE
|
|
|
|
#endif // FMT_COMPILE_H_
|