forked from fmtlib/fmt
Move formatter<std::error_code> from fmt/os.h to fmt/std.h (#3125)
Signed-off-by: Vladislav Shchapov <vladislav@shchapov.ru> Signed-off-by: Vladislav Shchapov <vladislav@shchapov.ru>
This commit is contained in:
committed by
GitHub
parent
ad91cab374
commit
662adf4f33
@@ -120,24 +120,6 @@ template <typename Char> class basic_cstring_view {
|
|||||||
using cstring_view = basic_cstring_view<char>;
|
using cstring_view = basic_cstring_view<char>;
|
||||||
using wcstring_view = basic_cstring_view<wchar_t>;
|
using wcstring_view = basic_cstring_view<wchar_t>;
|
||||||
|
|
||||||
template <typename Char> struct formatter<std::error_code, Char> {
|
|
||||||
template <typename ParseContext>
|
|
||||||
FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
|
|
||||||
return ctx.begin();
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename FormatContext>
|
|
||||||
FMT_CONSTEXPR auto format(const std::error_code& ec, FormatContext& ctx) const
|
|
||||||
-> decltype(ctx.out()) {
|
|
||||||
auto out = ctx.out();
|
|
||||||
out = detail::write_bytes(out, ec.category().name(),
|
|
||||||
basic_format_specs<Char>());
|
|
||||||
out = detail::write<Char>(out, Char(':'));
|
|
||||||
out = detail::write<Char>(out, ec.value());
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
FMT_API const std::error_category& system_category() noexcept;
|
FMT_API const std::error_category& system_category() noexcept;
|
||||||
|
|
||||||
|
@@ -180,6 +180,25 @@ FMT_END_NAMESPACE
|
|||||||
#endif // __cpp_lib_variant
|
#endif // __cpp_lib_variant
|
||||||
|
|
||||||
FMT_BEGIN_NAMESPACE
|
FMT_BEGIN_NAMESPACE
|
||||||
|
|
||||||
|
template <typename Char> struct formatter<std::error_code, Char> {
|
||||||
|
template <typename ParseContext>
|
||||||
|
FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
|
||||||
|
return ctx.begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename FormatContext>
|
||||||
|
FMT_CONSTEXPR auto format(const std::error_code& ec, FormatContext& ctx) const
|
||||||
|
-> decltype(ctx.out()) {
|
||||||
|
auto out = ctx.out();
|
||||||
|
out = detail::write_bytes(out, ec.category().name(),
|
||||||
|
basic_format_specs<Char>());
|
||||||
|
out = detail::write<Char>(out, Char(':'));
|
||||||
|
out = detail::write<Char>(out, ec.value());
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
template <typename T, typename Char>
|
template <typename T, typename Char>
|
||||||
struct formatter<
|
struct formatter<
|
||||||
T, Char,
|
T, Char,
|
||||||
|
@@ -64,18 +64,6 @@ TEST(util_test, utf16_to_utf8_convert) {
|
|||||||
u.convert(wstring_view(L"foo", INT_MAX + 1u)));
|
u.convert(wstring_view(L"foo", INT_MAX + 1u)));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(os_test, format_std_error_code) {
|
|
||||||
EXPECT_EQ("generic:42",
|
|
||||||
fmt::format(FMT_STRING("{0}"),
|
|
||||||
std::error_code(42, std::generic_category())));
|
|
||||||
EXPECT_EQ("system:42",
|
|
||||||
fmt::format(FMT_STRING("{0}"),
|
|
||||||
std::error_code(42, fmt::system_category())));
|
|
||||||
EXPECT_EQ("system:-42",
|
|
||||||
fmt::format(FMT_STRING("{0}"),
|
|
||||||
std::error_code(-42, fmt::system_category())));
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(os_test, format_windows_error) {
|
TEST(os_test, format_windows_error) {
|
||||||
LPWSTR message = nullptr;
|
LPWSTR message = nullptr;
|
||||||
auto result = FormatMessageW(
|
auto result = FormatMessageW(
|
||||||
|
@@ -10,6 +10,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include "fmt/os.h" // fmt::system_category
|
||||||
#include "fmt/ranges.h"
|
#include "fmt/ranges.h"
|
||||||
#include "gtest-extra.h" // StartsWith
|
#include "gtest-extra.h" // StartsWith
|
||||||
|
|
||||||
@@ -83,6 +84,18 @@ TEST(std_test, variant) {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(std_test, error_code) {
|
||||||
|
EXPECT_EQ("generic:42",
|
||||||
|
fmt::format(FMT_STRING("{0}"),
|
||||||
|
std::error_code(42, std::generic_category())));
|
||||||
|
EXPECT_EQ("system:42",
|
||||||
|
fmt::format(FMT_STRING("{0}"),
|
||||||
|
std::error_code(42, fmt::system_category())));
|
||||||
|
EXPECT_EQ("system:-42",
|
||||||
|
fmt::format(FMT_STRING("{0}"),
|
||||||
|
std::error_code(-42, fmt::system_category())));
|
||||||
|
}
|
||||||
|
|
||||||
template <typename Catch> void exception_test() {
|
template <typename Catch> void exception_test() {
|
||||||
try {
|
try {
|
||||||
throw std::runtime_error("Test Exception");
|
throw std::runtime_error("Test Exception");
|
||||||
|
Reference in New Issue
Block a user