Restrict fmt::compile to literal strings
to make sure we don't break the API too much when making all of this compile-time only.
This commit is contained in:
parent
544b537334
commit
e8219952c6
@ -613,44 +613,30 @@ template <typename... Args>
|
||||
using prepared_format_t =
|
||||
typename basic_prepared_format<std::string, parts_container<char>,
|
||||
Args...>::type;
|
||||
} // namespace internal
|
||||
|
||||
#if FMT_USE_CONSTEXPR
|
||||
|
||||
template <typename... Args, typename S>
|
||||
FMT_CONSTEXPR auto compile(S format_str) {
|
||||
return internal::do_compile<S, Args...>(
|
||||
typename internal::format_tag<S>::type{}, std::move(format_str));
|
||||
}
|
||||
#else
|
||||
|
||||
template <typename... Args, typename S>
|
||||
auto compile(S format_str) ->
|
||||
typename internal::preparator<S, Args...>::prepared_format_type {
|
||||
return internal::preparator<S, Args...>::prepare(std::move(format_str));
|
||||
}
|
||||
} // namespace internal
|
||||
|
||||
#if FMT_USE_CONSTEXPR
|
||||
|
||||
template <typename... Args, typename S,
|
||||
FMT_ENABLE_IF(is_compile_string<S>::value)>
|
||||
FMT_CONSTEXPR auto compile(S format_str) {
|
||||
return internal::do_compile<S, Args...>(
|
||||
typename internal::format_tag<S>::type{}, std::move(format_str));
|
||||
}
|
||||
#endif
|
||||
|
||||
template <typename... Args, typename Char>
|
||||
auto compile(const Char* format_str) ->
|
||||
typename internal::preparator<std::basic_string<Char>,
|
||||
Args...>::prepared_format_type {
|
||||
return compile<Args...>(internal::to_runtime_format(format_str));
|
||||
}
|
||||
|
||||
template <typename... Args, typename Char, unsigned N>
|
||||
auto compile(const Char(format_str)[N]) ->
|
||||
auto compile(const Char (&format_str)[N]) ->
|
||||
typename internal::preparator<std::basic_string<Char>,
|
||||
Args...>::prepared_format_type {
|
||||
const auto view = basic_string_view<Char>(format_str, N);
|
||||
return compile<Args...>(internal::to_runtime_format(view));
|
||||
}
|
||||
|
||||
template <typename... Args, typename Char>
|
||||
auto compile(basic_string_view<Char> format_str) ->
|
||||
typename internal::preparator<std::basic_string<Char>,
|
||||
Args...>::prepared_format_type {
|
||||
return compile<Args...>(internal::to_runtime_format(format_str));
|
||||
const auto view = basic_string_view<Char>(format_str, N - 1);
|
||||
return internal::compile<Args...>(internal::to_runtime_format(view));
|
||||
}
|
||||
|
||||
template <typename CompiledFormat, typename... Args,
|
||||
|
@ -433,72 +433,6 @@ TEST(PrepareTest, CompileTimePreparedPartsTypeProvider) {
|
||||
}
|
||||
#endif
|
||||
|
||||
// Use the struct instead of a function to workaround GCC 4.4's 'sorry,
|
||||
// unimplemented: mangling template_id_expr' issue.
|
||||
template <typename... Args> struct copied_prepared_format_creator {
|
||||
static decltype(fmt::compile<Args...>(std::declval<std::string>())) make(
|
||||
std::string format_str) {
|
||||
auto prepared_format = fmt::compile<Args...>(std::move(format_str));
|
||||
auto copied_prepared_format = prepared_format;
|
||||
prepared_format = fmt::compile<Args...>("");
|
||||
|
||||
return copied_prepared_format;
|
||||
}
|
||||
};
|
||||
|
||||
TEST(PrepareTest, CopyPreparedFormat_InternalStringViewsAreNotInvalidated) {
|
||||
auto prepared = copied_prepared_format_creator<int, std::string>::make(
|
||||
"before {} middle {} after");
|
||||
EXPECT_EQ("before 42 middle text after", fmt::format(prepared, 42, "text"));
|
||||
|
||||
prepared = copied_prepared_format_creator<int, std::string>::make(
|
||||
"before {0} middle {1} after");
|
||||
EXPECT_EQ("before 42 middle text after", fmt::format(prepared, 42, "text"));
|
||||
|
||||
{
|
||||
typedef decltype(fmt::arg("first", 42)) argument0;
|
||||
typedef decltype(fmt::arg("second", "text")) argument1;
|
||||
auto named_prepared =
|
||||
copied_prepared_format_creator<argument0, argument1>::make(
|
||||
"before {first} middle {second} after");
|
||||
EXPECT_EQ("before 42 middle text after",
|
||||
fmt::format(named_prepared, fmt::arg("first", 42),
|
||||
fmt::arg("second", "text")));
|
||||
}
|
||||
{
|
||||
typedef decltype(fmt::arg("value", "12345")) argument0;
|
||||
typedef decltype(fmt::arg("width", 10)) argument1;
|
||||
auto named_prepared =
|
||||
copied_prepared_format_creator<argument0, argument1>::make(
|
||||
">>>{value:>{width}}<<<");
|
||||
EXPECT_EQ(">>> 12345<<<",
|
||||
fmt::format(named_prepared, fmt::arg("value", "12345"),
|
||||
fmt::arg("width", 10)));
|
||||
}
|
||||
}
|
||||
|
||||
TEST(PrepareTest, ReusedPreparedFormatType) {
|
||||
using prepared_format = fmt::internal::prepared_format_t<std::string, int>;
|
||||
|
||||
prepared_format prepared = fmt::compile<prepared_format>("The {} is {}.");
|
||||
EXPECT_EQ("The answer is 42.", fmt::format(prepared, "answer", 42));
|
||||
prepared = fmt::compile<prepared_format>("40 {} 2 = {}");
|
||||
EXPECT_EQ("40 + 2 = 42", fmt::format(prepared, "+", 42));
|
||||
}
|
||||
|
||||
TEST(PrepareTest, UserProvidedPartsContainerUnderlyingContainer) {
|
||||
typedef fmt::internal::format_part<char> format_part;
|
||||
typedef fmt::internal::parts_container<char, std::list<format_part>>
|
||||
parts_container;
|
||||
typedef fmt::internal::basic_prepared_format<std::string, parts_container, std::string,
|
||||
int>::type prepared_format;
|
||||
|
||||
prepared_format prepared = fmt::compile<prepared_format>("The {} is {}.");
|
||||
EXPECT_EQ("The answer is 42.", fmt::format(prepared, "answer", 42));
|
||||
prepared = fmt::compile<prepared_format>("40 {} 2 = {}");
|
||||
EXPECT_EQ("40 + 2 = 42", fmt::format(prepared, "+", 42));
|
||||
}
|
||||
|
||||
class custom_parts_container {
|
||||
public:
|
||||
typedef fmt::internal::format_part<char> format_part_type;
|
||||
@ -533,43 +467,6 @@ class custom_parts_container {
|
||||
parts parts_;
|
||||
};
|
||||
|
||||
TEST(PrepareTest, UserProvidedPartsContainer) {
|
||||
typedef fmt::internal::basic_prepared_format<std::string, custom_parts_container,
|
||||
std::string, int>::type prepared_format;
|
||||
|
||||
prepared_format prepared = fmt::compile<prepared_format>("The {} is {}.");
|
||||
EXPECT_EQ("The answer is 42.", fmt::format(prepared, "answer", 42));
|
||||
prepared = fmt::compile<prepared_format>("40 {} 2 = {}");
|
||||
EXPECT_EQ("40 + 2 = 42", fmt::format(prepared, "+", 42));
|
||||
}
|
||||
|
||||
TEST(PrepareTest, PassConstCharPointerFormat) {
|
||||
const char* c_format = "test {}";
|
||||
const auto prepared = fmt::compile<int>(c_format);
|
||||
EXPECT_EQ("test 42", fmt::format(prepared, 42));
|
||||
const wchar_t* wc_format = L"test {}";
|
||||
const auto wprepared = fmt::compile<int>(wc_format);
|
||||
EXPECT_EQ(L"test 42", fmt::format(wprepared, 42));
|
||||
}
|
||||
|
||||
TEST(PrepareTest, PassCharArrayFormat) {
|
||||
char c_format[] = "test {}";
|
||||
const auto prepared = fmt::compile<int>(c_format);
|
||||
EXPECT_EQ("test 42", fmt::format(prepared, 42));
|
||||
wchar_t wc_format[] = L"test {}";
|
||||
const auto wprepared = fmt::compile<int>(wc_format);
|
||||
EXPECT_EQ(L"test 42", fmt::format(wprepared, 42));
|
||||
}
|
||||
|
||||
TEST(PrepareTest, PassConstCharArrayFormat) {
|
||||
const char c_format[] = "test {}";
|
||||
const auto prepared = fmt::compile<int>(c_format);
|
||||
EXPECT_EQ("test 42", fmt::format(prepared, 42));
|
||||
const wchar_t wc_format[] = L"test {}";
|
||||
const auto wprepared = fmt::compile<int>(wc_format);
|
||||
EXPECT_EQ(L"test 42", fmt::format(wprepared, 42));
|
||||
}
|
||||
|
||||
TEST(PrepareTest, PassStringLiteralFormat) {
|
||||
const auto prepared = fmt::compile<int>("test {}");
|
||||
EXPECT_EQ("test 42", fmt::format(prepared, 42));
|
||||
@ -577,22 +474,6 @@ TEST(PrepareTest, PassStringLiteralFormat) {
|
||||
EXPECT_EQ(L"test 42", fmt::format(wprepared, 42));
|
||||
}
|
||||
|
||||
TEST(PrepareTest, PassStringViewFormat) {
|
||||
const auto prepared =
|
||||
fmt::compile<int>(fmt::basic_string_view<char>("test {}"));
|
||||
EXPECT_EQ("test 42", fmt::format(prepared, 42));
|
||||
const auto wprepared =
|
||||
fmt::compile<int>(fmt::basic_string_view<wchar_t>(L"test {}"));
|
||||
EXPECT_EQ(L"test 42", fmt::format(wprepared, 42));
|
||||
}
|
||||
|
||||
TEST(PrepareTest, PassBasicStringFormat) {
|
||||
const auto prepared = fmt::compile<int>(std::string("test {}"));
|
||||
EXPECT_EQ("test 42", fmt::format(prepared, 42));
|
||||
const auto wprepared = fmt::compile<int>(std::wstring(L"test {}"));
|
||||
EXPECT_EQ(L"test 42", fmt::format(wprepared, 42));
|
||||
}
|
||||
|
||||
#if FMT_USE_CONSTEXPR
|
||||
TEST(PrepareTest, PassCompileString) {
|
||||
const auto prepared = fmt::compile<int>(FMT_STRING("test {}"));
|
||||
@ -602,44 +483,6 @@ TEST(PrepareTest, PassCompileString) {
|
||||
}
|
||||
#endif
|
||||
|
||||
template <typename T> struct user_allocator {
|
||||
typedef T value_type;
|
||||
typedef value_type* pointer;
|
||||
typedef const value_type* const_pointer;
|
||||
typedef value_type& reference;
|
||||
typedef const value_type& const_reference;
|
||||
typedef std::size_t size_type;
|
||||
typedef std::ptrdiff_t difference_type;
|
||||
|
||||
template <typename U> struct rebind { typedef user_allocator<U> other; };
|
||||
|
||||
user_allocator() = default;
|
||||
~user_allocator() = default;
|
||||
template <typename U> user_allocator(const user_allocator<U>&) {}
|
||||
|
||||
pointer allocate(
|
||||
size_type cnt,
|
||||
typename std::allocator<fmt::monostate>::const_pointer = nullptr) {
|
||||
return new value_type[cnt];
|
||||
}
|
||||
|
||||
void deallocate(pointer p, size_type) { delete[] p; }
|
||||
|
||||
void construct(pointer p, const value_type& val) { new (p) value_type(val); }
|
||||
|
||||
void destroy(pointer p) { (*p).~value_type(); }
|
||||
|
||||
bool operator==(const user_allocator&) const { return true; }
|
||||
bool operator!=(const user_allocator&) const { return false; }
|
||||
};
|
||||
|
||||
TEST(PrepareTest, PassUserTypeFormat) {
|
||||
typedef std::basic_string<char, std::char_traits<char>, user_allocator<char>>
|
||||
user_format;
|
||||
const auto prepared = fmt::compile<int>(user_format("test {}"));
|
||||
EXPECT_EQ("test 42", fmt::format(prepared, 42));
|
||||
}
|
||||
|
||||
TEST(PrepareTest, FormatToArrayOfChars) {
|
||||
char buffer[32] = {0};
|
||||
const auto prepared = fmt::compile<int>("4{}");
|
||||
|
Loading…
Reference in New Issue
Block a user