mirror of
https://github.com/fmtlib/fmt.git
synced 2024-12-26 00:21:13 +00:00
Add _format literal
This commit is contained in:
parent
50e9487e18
commit
a63a24f2d7
38
format.h
38
format.h
@ -177,6 +177,16 @@ inline uint32_t clzll(uint64_t x) {
|
|||||||
TypeName& operator=(const TypeName&)
|
TypeName& operator=(const TypeName&)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef FMT_USE_USER_DEFINED_LITERALS
|
||||||
|
// All compilers which support UDLs also support variadic templates. This
|
||||||
|
// makes the fmt::literals implementation easier. However, an explicit check
|
||||||
|
// for variadic templates is added here just in case.
|
||||||
|
# define FMT_USE_USER_DEFINED_LITERALS \
|
||||||
|
FMT_USE_VARIADIC_TEMPLATES && \
|
||||||
|
(FMT_HAS_FEATURE(cxx_user_literals) || \
|
||||||
|
(FMT_GCC_VERSION >= 407 && FMT_HAS_GXX_CXX11) || _MSC_VER >= 1900)
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifndef FMT_ASSERT
|
#ifndef FMT_ASSERT
|
||||||
# define FMT_ASSERT(condition, message) assert((condition) && message)
|
# define FMT_ASSERT(condition, message) assert((condition) && message)
|
||||||
#endif
|
#endif
|
||||||
@ -3005,6 +3015,34 @@ FMT_VARIADIC(int, printf, CStringRef)
|
|||||||
FMT_VARIADIC(int, fprintf, std::FILE *, CStringRef)
|
FMT_VARIADIC(int, fprintf, std::FILE *, CStringRef)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if FMT_USE_USER_DEFINED_LITERALS
|
||||||
|
namespace fmt {
|
||||||
|
namespace internal {
|
||||||
|
|
||||||
|
template <typename Char>
|
||||||
|
struct UdlFormat {
|
||||||
|
const Char *str;
|
||||||
|
|
||||||
|
template <typename... Args>
|
||||||
|
auto operator()(Args && ... args) const
|
||||||
|
-> decltype(format(str, std::forward<Args>(args)...)) {
|
||||||
|
return format(str, std::forward<Args>(args)...);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace internal
|
||||||
|
|
||||||
|
inline namespace literals {
|
||||||
|
|
||||||
|
inline internal::UdlFormat<char>
|
||||||
|
operator"" _format(const char *s, std::size_t) { return {s}; }
|
||||||
|
inline internal::UdlFormat<wchar_t>
|
||||||
|
operator"" _format(const wchar_t *s, std::size_t) { return {s}; }
|
||||||
|
|
||||||
|
} // inline namespace literals
|
||||||
|
} // namespace fmt
|
||||||
|
#endif // FMT_USE_USER_DEFINED_LITERALS
|
||||||
|
|
||||||
// Restore warnings.
|
// Restore warnings.
|
||||||
#if FMT_GCC_VERSION >= 406
|
#if FMT_GCC_VERSION >= 406
|
||||||
# pragma GCC diagnostic pop
|
# pragma GCC diagnostic pop
|
||||||
|
@ -1602,3 +1602,12 @@ TEST(FormatTest, MaxArgs) {
|
|||||||
fmt::format("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
|
fmt::format("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
|
||||||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'a', 'b', 'c', 'd', 'e'));
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'a', 'b', 'c', 'd', 'e'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if FMT_USE_USER_DEFINED_LITERALS
|
||||||
|
using namespace fmt::literals;
|
||||||
|
|
||||||
|
TEST(LiteralsTest, Format) {
|
||||||
|
EXPECT_EQ(format("{}c{}", "ab", 1), "{}c{}"_format("ab", 1));
|
||||||
|
EXPECT_EQ(format(L"{}c{}", L"ab", 1), L"{}c{}"_format(L"ab", 1));
|
||||||
|
}
|
||||||
|
#endif // FMT_USE_USER_DEFINED_LITERALS
|
||||||
|
Loading…
Reference in New Issue
Block a user