forked from fmtlib/fmt
Make format a non-member
This commit is contained in:
@ -206,15 +206,6 @@ class prepared_format {
|
|||||||
return {it.base(), it.count()};
|
return {it.base(), it.count()};
|
||||||
}
|
}
|
||||||
|
|
||||||
std::basic_string<char_type> format(const Args&... args) const {
|
|
||||||
basic_memory_buffer<char_type> buffer;
|
|
||||||
using range = buffer_range<char_type>;
|
|
||||||
this->vformat_to(range(buffer),
|
|
||||||
basic_format_args<context>{
|
|
||||||
make_args_checked<Args...>(format_, args...)});
|
|
||||||
return to_string(buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename Container, FMT_ENABLE_IF(is_contiguous<Container>::value)>
|
template <typename Container, FMT_ENABLE_IF(is_contiguous<Container>::value)>
|
||||||
inline std::back_insert_iterator<Container> format_to(
|
inline std::back_insert_iterator<Container> format_to(
|
||||||
std::back_insert_iterator<Container> out, Args&&... args) const {
|
std::back_insert_iterator<Container> out, Args&&... args) const {
|
||||||
@ -243,7 +234,6 @@ class prepared_format {
|
|||||||
make_args_checked<Args...>(format_, args...)});
|
make_args_checked<Args...>(format_, args...)});
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
|
||||||
typedef buffer_context<char_type> context;
|
typedef buffer_context<char_type> context;
|
||||||
|
|
||||||
template <typename Range, typename Context>
|
template <typename Range, typename Context>
|
||||||
@ -304,6 +294,7 @@ class prepared_format {
|
|||||||
return ctx.out();
|
return ctx.out();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
void advance_parse_context_to_specification(
|
void advance_parse_context_to_specification(
|
||||||
basic_parse_context<char_type>& parse_ctx,
|
basic_parse_context<char_type>& parse_ctx,
|
||||||
const format_part_t& part) const {
|
const format_part_t& part) const {
|
||||||
@ -677,9 +668,10 @@ auto do_compile(const Format& format)
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
template <typename... Args> using prepared_format_t =
|
template <typename... Args>
|
||||||
typename basic_prepared_format<
|
using prepared_format_t =
|
||||||
std::string, parts_container<char>, Args...>::type;
|
typename basic_prepared_format<std::string, parts_container<char>,
|
||||||
|
Args...>::type;
|
||||||
} // namespace internal
|
} // namespace internal
|
||||||
|
|
||||||
#if FMT_USE_CONSTEXPR
|
#if FMT_USE_CONSTEXPR
|
||||||
@ -720,6 +712,17 @@ auto compile(basic_string_view<Char> format_str) ->
|
|||||||
return compile<Args...>(internal::to_runtime_format(format_str));
|
return compile<Args...>(internal::to_runtime_format(format_str));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
using range = internal::buffer_range<Char>;
|
||||||
|
using context = buffer_context<Char>;
|
||||||
|
cf.template vformat_to<range, context>(range(buffer),
|
||||||
|
{make_format_args<context>(args...)});
|
||||||
|
return to_string(buffer);
|
||||||
|
}
|
||||||
|
|
||||||
FMT_END_NAMESPACE
|
FMT_END_NAMESPACE
|
||||||
|
|
||||||
#endif // FMT_COMPILE_H_
|
#endif // FMT_COMPILE_H_
|
||||||
|
@ -449,11 +449,11 @@ template <typename... Args> struct copied_prepared_format_creator {
|
|||||||
TEST(PrepareTest, CopyPreparedFormat_InternalStringViewsAreNotInvalidated) {
|
TEST(PrepareTest, CopyPreparedFormat_InternalStringViewsAreNotInvalidated) {
|
||||||
auto prepared = copied_prepared_format_creator<int, std::string>::make(
|
auto prepared = copied_prepared_format_creator<int, std::string>::make(
|
||||||
"before {} middle {} after");
|
"before {} middle {} after");
|
||||||
EXPECT_EQ("before 42 middle text after", prepared.format(42, "text"));
|
EXPECT_EQ("before 42 middle text after", fmt::format(prepared, 42, "text"));
|
||||||
|
|
||||||
prepared = copied_prepared_format_creator<int, std::string>::make(
|
prepared = copied_prepared_format_creator<int, std::string>::make(
|
||||||
"before {0} middle {1} after");
|
"before {0} middle {1} after");
|
||||||
EXPECT_EQ("before 42 middle text after", prepared.format(42, "text"));
|
EXPECT_EQ("before 42 middle text after", fmt::format(prepared, 42, "text"));
|
||||||
|
|
||||||
{
|
{
|
||||||
typedef decltype(fmt::arg("first", 42)) argument0;
|
typedef decltype(fmt::arg("first", 42)) argument0;
|
||||||
@ -462,7 +462,7 @@ TEST(PrepareTest, CopyPreparedFormat_InternalStringViewsAreNotInvalidated) {
|
|||||||
copied_prepared_format_creator<argument0, argument1>::make(
|
copied_prepared_format_creator<argument0, argument1>::make(
|
||||||
"before {first} middle {second} after");
|
"before {first} middle {second} after");
|
||||||
EXPECT_EQ("before 42 middle text after",
|
EXPECT_EQ("before 42 middle text after",
|
||||||
named_prepared.format(fmt::arg("first", 42),
|
fmt::format(named_prepared, fmt::arg("first", 42),
|
||||||
fmt::arg("second", "text")));
|
fmt::arg("second", "text")));
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
@ -472,7 +472,7 @@ TEST(PrepareTest, CopyPreparedFormat_InternalStringViewsAreNotInvalidated) {
|
|||||||
copied_prepared_format_creator<argument0, argument1>::make(
|
copied_prepared_format_creator<argument0, argument1>::make(
|
||||||
">>>{value:>{width}}<<<");
|
">>>{value:>{width}}<<<");
|
||||||
EXPECT_EQ(">>> 12345<<<",
|
EXPECT_EQ(">>> 12345<<<",
|
||||||
named_prepared.format(fmt::arg("value", "12345"),
|
fmt::format(named_prepared, fmt::arg("value", "12345"),
|
||||||
fmt::arg("width", 10)));
|
fmt::arg("width", 10)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -481,9 +481,9 @@ TEST(PrepareTest, ReusedPreparedFormatType) {
|
|||||||
using prepared_format = fmt::internal::prepared_format_t<std::string, int>;
|
using prepared_format = fmt::internal::prepared_format_t<std::string, int>;
|
||||||
|
|
||||||
prepared_format prepared = fmt::compile<prepared_format>("The {} is {}.");
|
prepared_format prepared = fmt::compile<prepared_format>("The {} is {}.");
|
||||||
EXPECT_EQ("The answer is 42.", prepared.format("answer", 42));
|
EXPECT_EQ("The answer is 42.", fmt::format(prepared, "answer", 42));
|
||||||
prepared = fmt::compile<prepared_format>("40 {} 2 = {}");
|
prepared = fmt::compile<prepared_format>("40 {} 2 = {}");
|
||||||
EXPECT_EQ("40 + 2 = 42", prepared.format("+", 42));
|
EXPECT_EQ("40 + 2 = 42", fmt::format(prepared, "+", 42));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(PrepareTest, UserProvidedPartsContainerUnderlyingContainer) {
|
TEST(PrepareTest, UserProvidedPartsContainerUnderlyingContainer) {
|
||||||
@ -494,9 +494,9 @@ TEST(PrepareTest, UserProvidedPartsContainerUnderlyingContainer) {
|
|||||||
int>::type prepared_format;
|
int>::type prepared_format;
|
||||||
|
|
||||||
prepared_format prepared = fmt::compile<prepared_format>("The {} is {}.");
|
prepared_format prepared = fmt::compile<prepared_format>("The {} is {}.");
|
||||||
EXPECT_EQ("The answer is 42.", prepared.format("answer", 42));
|
EXPECT_EQ("The answer is 42.", fmt::format(prepared, "answer", 42));
|
||||||
prepared = fmt::compile<prepared_format>("40 {} 2 = {}");
|
prepared = fmt::compile<prepared_format>("40 {} 2 = {}");
|
||||||
EXPECT_EQ("40 + 2 = 42", prepared.format("+", 42));
|
EXPECT_EQ("40 + 2 = 42", fmt::format(prepared, "+", 42));
|
||||||
}
|
}
|
||||||
|
|
||||||
class custom_parts_container {
|
class custom_parts_container {
|
||||||
@ -538,67 +538,67 @@ TEST(PrepareTest, UserProvidedPartsContainer) {
|
|||||||
std::string, int>::type prepared_format;
|
std::string, int>::type prepared_format;
|
||||||
|
|
||||||
prepared_format prepared = fmt::compile<prepared_format>("The {} is {}.");
|
prepared_format prepared = fmt::compile<prepared_format>("The {} is {}.");
|
||||||
EXPECT_EQ("The answer is 42.", prepared.format("answer", 42));
|
EXPECT_EQ("The answer is 42.", fmt::format(prepared, "answer", 42));
|
||||||
prepared = fmt::compile<prepared_format>("40 {} 2 = {}");
|
prepared = fmt::compile<prepared_format>("40 {} 2 = {}");
|
||||||
EXPECT_EQ("40 + 2 = 42", prepared.format("+", 42));
|
EXPECT_EQ("40 + 2 = 42", fmt::format(prepared, "+", 42));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(PrepareTest, PassConstCharPointerFormat) {
|
TEST(PrepareTest, PassConstCharPointerFormat) {
|
||||||
const char* c_format = "test {}";
|
const char* c_format = "test {}";
|
||||||
const auto prepared = fmt::compile<int>(c_format);
|
const auto prepared = fmt::compile<int>(c_format);
|
||||||
EXPECT_EQ("test 42", prepared.format(42));
|
EXPECT_EQ("test 42", fmt::format(prepared, 42));
|
||||||
const wchar_t* wc_format = L"test {}";
|
const wchar_t* wc_format = L"test {}";
|
||||||
const auto wprepared = fmt::compile<int>(wc_format);
|
const auto wprepared = fmt::compile<int>(wc_format);
|
||||||
EXPECT_EQ(L"test 42", wprepared.format(42));
|
EXPECT_EQ(L"test 42", fmt::format(wprepared, 42));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(PrepareTest, PassCharArrayFormat) {
|
TEST(PrepareTest, PassCharArrayFormat) {
|
||||||
char c_format[] = "test {}";
|
char c_format[] = "test {}";
|
||||||
const auto prepared = fmt::compile<int>(c_format);
|
const auto prepared = fmt::compile<int>(c_format);
|
||||||
EXPECT_EQ("test 42", prepared.format(42));
|
EXPECT_EQ("test 42", fmt::format(prepared, 42));
|
||||||
wchar_t wc_format[] = L"test {}";
|
wchar_t wc_format[] = L"test {}";
|
||||||
const auto wprepared = fmt::compile<int>(wc_format);
|
const auto wprepared = fmt::compile<int>(wc_format);
|
||||||
EXPECT_EQ(L"test 42", wprepared.format(42));
|
EXPECT_EQ(L"test 42", fmt::format(wprepared, 42));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(PrepareTest, PassConstCharArrayFormat) {
|
TEST(PrepareTest, PassConstCharArrayFormat) {
|
||||||
const char c_format[] = "test {}";
|
const char c_format[] = "test {}";
|
||||||
const auto prepared = fmt::compile<int>(c_format);
|
const auto prepared = fmt::compile<int>(c_format);
|
||||||
EXPECT_EQ("test 42", prepared.format(42));
|
EXPECT_EQ("test 42", fmt::format(prepared, 42));
|
||||||
const wchar_t wc_format[] = L"test {}";
|
const wchar_t wc_format[] = L"test {}";
|
||||||
const auto wprepared = fmt::compile<int>(wc_format);
|
const auto wprepared = fmt::compile<int>(wc_format);
|
||||||
EXPECT_EQ(L"test 42", wprepared.format(42));
|
EXPECT_EQ(L"test 42", fmt::format(wprepared, 42));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(PrepareTest, PassStringLiteralFormat) {
|
TEST(PrepareTest, PassStringLiteralFormat) {
|
||||||
const auto prepared = fmt::compile<int>("test {}");
|
const auto prepared = fmt::compile<int>("test {}");
|
||||||
EXPECT_EQ("test 42", prepared.format(42));
|
EXPECT_EQ("test 42", fmt::format(prepared, 42));
|
||||||
const auto wprepared = fmt::compile<int>(L"test {}");
|
const auto wprepared = fmt::compile<int>(L"test {}");
|
||||||
EXPECT_EQ(L"test 42", wprepared.format(42));
|
EXPECT_EQ(L"test 42", fmt::format(wprepared, 42));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(PrepareTest, PassStringViewFormat) {
|
TEST(PrepareTest, PassStringViewFormat) {
|
||||||
const auto prepared =
|
const auto prepared =
|
||||||
fmt::compile<int>(fmt::basic_string_view<char>("test {}"));
|
fmt::compile<int>(fmt::basic_string_view<char>("test {}"));
|
||||||
EXPECT_EQ("test 42", prepared.format(42));
|
EXPECT_EQ("test 42", fmt::format(prepared, 42));
|
||||||
const auto wprepared =
|
const auto wprepared =
|
||||||
fmt::compile<int>(fmt::basic_string_view<wchar_t>(L"test {}"));
|
fmt::compile<int>(fmt::basic_string_view<wchar_t>(L"test {}"));
|
||||||
EXPECT_EQ(L"test 42", wprepared.format(42));
|
EXPECT_EQ(L"test 42", fmt::format(wprepared, 42));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(PrepareTest, PassBasicStringFormat) {
|
TEST(PrepareTest, PassBasicStringFormat) {
|
||||||
const auto prepared = fmt::compile<int>(std::string("test {}"));
|
const auto prepared = fmt::compile<int>(std::string("test {}"));
|
||||||
EXPECT_EQ("test 42", prepared.format(42));
|
EXPECT_EQ("test 42", fmt::format(prepared, 42));
|
||||||
const auto wprepared = fmt::compile<int>(std::wstring(L"test {}"));
|
const auto wprepared = fmt::compile<int>(std::wstring(L"test {}"));
|
||||||
EXPECT_EQ(L"test 42", wprepared.format(42));
|
EXPECT_EQ(L"test 42", fmt::format(wprepared, 42));
|
||||||
}
|
}
|
||||||
|
|
||||||
#if FMT_USE_CONSTEXPR
|
#if FMT_USE_CONSTEXPR
|
||||||
TEST(PrepareTest, PassCompileString) {
|
TEST(PrepareTest, PassCompileString) {
|
||||||
const auto prepared = fmt::compile<int>(FMT_STRING("test {}"));
|
const auto prepared = fmt::compile<int>(FMT_STRING("test {}"));
|
||||||
EXPECT_EQ("test 42", prepared.format(42));
|
EXPECT_EQ("test 42", fmt::format(prepared, 42));
|
||||||
const auto wprepared = fmt::compile<int>(FMT_STRING(L"test {}"));
|
const auto wprepared = fmt::compile<int>(FMT_STRING(L"test {}"));
|
||||||
EXPECT_EQ(L"test 42", wprepared.format(42));
|
EXPECT_EQ(L"test 42", fmt::format(wprepared, 42));
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -637,7 +637,7 @@ TEST(PrepareTest, PassUserTypeFormat) {
|
|||||||
typedef std::basic_string<char, std::char_traits<char>, user_allocator<char>>
|
typedef std::basic_string<char, std::char_traits<char>, user_allocator<char>>
|
||||||
user_format;
|
user_format;
|
||||||
const auto prepared = fmt::compile<int>(user_format("test {}"));
|
const auto prepared = fmt::compile<int>(user_format("test {}"));
|
||||||
EXPECT_EQ("test 42", prepared.format(42));
|
EXPECT_EQ("test 42", fmt::format(prepared, 42));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(PrepareTest, FormatToArrayOfChars) {
|
TEST(PrepareTest, FormatToArrayOfChars) {
|
||||||
|
Reference in New Issue
Block a user