mirror of
https://github.com/shadps4-emu/ext-fmt.git
synced 2025-02-09 03:46:27 +00:00
Disallow passing non-string-literals to FMT_STRING
This commit is contained in:
parent
419db8baa1
commit
09a13244c8
@ -3449,13 +3449,10 @@ template <typename Char> struct udl_arg {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// A constexpr version of strlen.
|
template <typename Char, size_t N>
|
||||||
template <typename Char> FMT_CONSTEXPR size_t compute_length(const Char* s) {
|
FMT_CONSTEXPR basic_string_view<Char> literal_to_view(const Char (&s)[N]) {
|
||||||
size_t len = 0;
|
return {s, N - 1};
|
||||||
while (*s++) ++len;
|
|
||||||
return len;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace internal
|
} // namespace internal
|
||||||
|
|
||||||
inline namespace literals {
|
inline namespace literals {
|
||||||
@ -3519,7 +3516,8 @@ FMT_END_NAMESPACE
|
|||||||
using char_type = fmt::remove_cvref_t<decltype(*s)>; \
|
using char_type = fmt::remove_cvref_t<decltype(*s)>; \
|
||||||
__VA_ARGS__ FMT_CONSTEXPR \
|
__VA_ARGS__ FMT_CONSTEXPR \
|
||||||
operator fmt::basic_string_view<char_type>() const { \
|
operator fmt::basic_string_view<char_type>() const { \
|
||||||
return {s, fmt::internal::compute_length(s)}; \
|
/* FMT_STRING only accepts string literals. */ \
|
||||||
|
return fmt::internal::literal_to_view(s); \
|
||||||
} \
|
} \
|
||||||
}; \
|
}; \
|
||||||
return FMT_STRING(); \
|
return FMT_STRING(); \
|
||||||
@ -3527,7 +3525,7 @@ FMT_END_NAMESPACE
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
\rst
|
\rst
|
||||||
Constructs a compile-time format string.
|
Constructs a compile-time format string from a string literal *s*.
|
||||||
|
|
||||||
**Example**::
|
**Example**::
|
||||||
|
|
||||||
|
@ -1849,10 +1849,7 @@ TEST(FormatTest, UnpackedArgs) {
|
|||||||
struct string_like {};
|
struct string_like {};
|
||||||
fmt::string_view to_string_view(string_like) { return "foo"; }
|
fmt::string_view to_string_view(string_like) { return "foo"; }
|
||||||
|
|
||||||
FMT_CONSTEXPR_DECL const char* format_str_ptr = "0123456789";
|
|
||||||
|
|
||||||
TEST(FormatTest, CompileTimeString) {
|
TEST(FormatTest, CompileTimeString) {
|
||||||
EXPECT_EQ(format_str_ptr, fmt::format(FMT_STRING(format_str_ptr)));
|
|
||||||
EXPECT_EQ("42", fmt::format(FMT_STRING("{}"), 42));
|
EXPECT_EQ("42", fmt::format(FMT_STRING("{}"), 42));
|
||||||
EXPECT_EQ(L"42", fmt::format(FMT_STRING(L"{}"), 42));
|
EXPECT_EQ(L"42", fmt::format(FMT_STRING(L"{}"), 42));
|
||||||
EXPECT_EQ("foo", fmt::format(FMT_STRING("{}"), string_like()));
|
EXPECT_EQ("foo", fmt::format(FMT_STRING("{}"), string_like()));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user