mirror of
https://github.com/fmtlib/fmt.git
synced 2025-07-30 02:37:36 +02:00
Improve API safety
This commit is contained in:
@ -1817,15 +1817,14 @@ class format_arg_store
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
template <typename... T>
|
template <typename... T>
|
||||||
FMT_CONSTEXPR FMT_INLINE format_arg_store(T&&... args)
|
FMT_CONSTEXPR FMT_INLINE format_arg_store(T&... args)
|
||||||
:
|
:
|
||||||
#if FMT_GCC_VERSION && FMT_GCC_VERSION < 409
|
#if FMT_GCC_VERSION && FMT_GCC_VERSION < 409
|
||||||
basic_format_args<Context>(*this),
|
basic_format_args<Context>(*this),
|
||||||
#endif
|
#endif
|
||||||
data_{detail::make_arg<
|
data_{detail::make_arg<
|
||||||
is_packed, Context,
|
is_packed, Context,
|
||||||
detail::mapped_type_constant<remove_cvref_t<T>, Context>::value>(
|
detail::mapped_type_constant<Args, Context>::value>(args)...} {
|
||||||
FMT_FORWARD(args))...} {
|
|
||||||
detail::init_named_args(data_.named_args(), 0, 0, args...);
|
detail::init_named_args(data_.named_args(), 0, 0, args...);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -529,8 +529,9 @@ TEST(printf_test, wide_string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST(printf_test, vprintf) {
|
TEST(printf_test, vprintf) {
|
||||||
fmt::format_arg_store<fmt::printf_context, int> as{42};
|
int n = 42;
|
||||||
fmt::basic_format_args<fmt::printf_context> args(as);
|
auto store = fmt::format_arg_store<fmt::printf_context, int>(n);
|
||||||
|
auto args = fmt::basic_format_args<fmt::printf_context>(store);
|
||||||
EXPECT_EQ(fmt::vsprintf("%d", args), "42");
|
EXPECT_EQ(fmt::vsprintf("%d", args), "42");
|
||||||
EXPECT_WRITE(stdout, fmt::vprintf("%d", args), "42");
|
EXPECT_WRITE(stdout, fmt::vprintf("%d", args), "42");
|
||||||
EXPECT_WRITE(stdout, fmt::vfprintf(stdout, "%d", args), "42");
|
EXPECT_WRITE(stdout, fmt::vfprintf(stdout, "%d", args), "42");
|
||||||
@ -549,30 +550,10 @@ TEST(printf_test, fixed_large_exponent) {
|
|||||||
EXPECT_EQ("1000000000000000000000", fmt::sprintf("%.*f", -13, 1e21));
|
EXPECT_EQ("1000000000000000000000", fmt::sprintf("%.*f", -13, 1e21));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(printf_test, vsprintf_make_args_example) {
|
TEST(printf_test, make_printf_args) {
|
||||||
fmt::format_arg_store<fmt::printf_context, int, const char*> as{42,
|
|
||||||
"something"};
|
|
||||||
fmt::basic_format_args<fmt::printf_context> args(as);
|
|
||||||
EXPECT_EQ("[42] something happened", fmt::vsprintf("[%d] %s happened", args));
|
|
||||||
auto as2 = fmt::make_printf_args(42, "something");
|
|
||||||
fmt::basic_format_args<fmt::printf_context> args2(as2);
|
|
||||||
EXPECT_EQ("[42] something happened",
|
|
||||||
fmt::vsprintf("[%d] %s happened", args2));
|
|
||||||
EXPECT_EQ("[42] something happened",
|
EXPECT_EQ("[42] something happened",
|
||||||
fmt::vsprintf("[%d] %s happened",
|
fmt::vsprintf("[%d] %s happened",
|
||||||
{fmt::make_printf_args(42, "something")}));
|
{fmt::make_printf_args(42, "something")}));
|
||||||
}
|
|
||||||
|
|
||||||
TEST(printf_test, vsprintf_make_wargs_example) {
|
|
||||||
fmt::format_arg_store<fmt::wprintf_context, int, const wchar_t*> as{
|
|
||||||
42, L"something"};
|
|
||||||
fmt::basic_format_args<fmt::wprintf_context> args(as);
|
|
||||||
EXPECT_EQ(L"[42] something happened",
|
|
||||||
fmt::vsprintf(L"[%d] %s happened", args));
|
|
||||||
auto as2 = fmt::make_wprintf_args(42, L"something");
|
|
||||||
fmt::basic_format_args<fmt::wprintf_context> args2(as2);
|
|
||||||
EXPECT_EQ(L"[42] something happened",
|
|
||||||
fmt::vsprintf(L"[%d] %s happened", args2));
|
|
||||||
EXPECT_EQ(L"[42] something happened",
|
EXPECT_EQ(L"[42] something happened",
|
||||||
fmt::vsprintf(L"[%d] %s happened",
|
fmt::vsprintf(L"[%d] %s happened",
|
||||||
{fmt::make_wprintf_args(42, L"something")}));
|
{fmt::make_wprintf_args(42, L"something")}));
|
||||||
|
@ -442,8 +442,8 @@ TEST(locale_test, format) {
|
|||||||
EXPECT_EQ("1~234~567", fmt::format(loc, "{:L}", 1234567));
|
EXPECT_EQ("1~234~567", fmt::format(loc, "{:L}", 1234567));
|
||||||
EXPECT_EQ("-1~234~567", fmt::format(loc, "{:L}", -1234567));
|
EXPECT_EQ("-1~234~567", fmt::format(loc, "{:L}", -1234567));
|
||||||
EXPECT_EQ("-256", fmt::format(loc, "{:L}", -256));
|
EXPECT_EQ("-256", fmt::format(loc, "{:L}", -256));
|
||||||
fmt::format_arg_store<fmt::format_context, int> as{1234567};
|
auto n = 1234567;
|
||||||
EXPECT_EQ("1~234~567", fmt::vformat(loc, "{:L}", fmt::format_args(as)));
|
EXPECT_EQ("1~234~567", fmt::vformat(loc, "{:L}", fmt::make_format_args(n)));
|
||||||
auto s = std::string();
|
auto s = std::string();
|
||||||
fmt::format_to(std::back_inserter(s), loc, "{:L}", 1234567);
|
fmt::format_to(std::back_inserter(s), loc, "{:L}", 1234567);
|
||||||
EXPECT_EQ("1~234~567", s);
|
EXPECT_EQ("1~234~567", s);
|
||||||
@ -477,9 +477,9 @@ TEST(locale_test, wformat) {
|
|||||||
EXPECT_EQ(L"1234567", fmt::format(std::locale(), L"{:L}", 1234567));
|
EXPECT_EQ(L"1234567", fmt::format(std::locale(), L"{:L}", 1234567));
|
||||||
EXPECT_EQ(L"1~234~567", fmt::format(loc, L"{:L}", 1234567));
|
EXPECT_EQ(L"1~234~567", fmt::format(loc, L"{:L}", 1234567));
|
||||||
using wcontext = fmt::buffer_context<wchar_t>;
|
using wcontext = fmt::buffer_context<wchar_t>;
|
||||||
fmt::format_arg_store<wcontext, int> as{1234567};
|
int n = 1234567;
|
||||||
EXPECT_EQ(L"1~234~567",
|
EXPECT_EQ(L"1~234~567",
|
||||||
fmt::vformat(loc, L"{:L}", fmt::basic_format_args<wcontext>(as)));
|
fmt::vformat(loc, L"{:L}", fmt::make_wformat_args(n)));
|
||||||
EXPECT_EQ(L"1234567", fmt::format(std::locale("C"), L"{:L}", 1234567));
|
EXPECT_EQ(L"1234567", fmt::format(std::locale("C"), L"{:L}", 1234567));
|
||||||
|
|
||||||
auto no_grouping_loc = std::locale(std::locale(), new no_grouping<wchar_t>());
|
auto no_grouping_loc = std::locale(std::locale(), new no_grouping<wchar_t>());
|
||||||
|
Reference in New Issue
Block a user