mirror of
https://github.com/fmtlib/fmt.git
synced 2025-07-31 19:24:48 +02:00
Implement the s specifier for error_code
This commit is contained in:
@@ -422,6 +422,12 @@ template <> struct formatter<std::error_code> {
|
||||
char c = *it;
|
||||
if ((c >= '0' && c <= '9') || c == '{')
|
||||
it = detail::parse_width(it, end, specs_, width_ref_, ctx);
|
||||
if (it == end) return it;
|
||||
|
||||
if (*it == 's') {
|
||||
specs_.set_type(presentation_type::string);
|
||||
++it;
|
||||
}
|
||||
return it;
|
||||
}
|
||||
|
||||
@@ -431,10 +437,14 @@ template <> struct formatter<std::error_code> {
|
||||
auto specs = specs_;
|
||||
detail::handle_dynamic_spec(specs.dynamic_width(), specs.width, width_ref_,
|
||||
ctx);
|
||||
memory_buffer buf;
|
||||
auto buf = memory_buffer();
|
||||
if (specs_.type() == presentation_type::string) {
|
||||
buf.append(ec.message());
|
||||
} else {
|
||||
buf.append(string_view(ec.category().name()));
|
||||
buf.push_back(':');
|
||||
detail::write<char>(appender(buf), ec.value());
|
||||
}
|
||||
return detail::write<char>(ctx.out(), string_view(buf.data(), buf.size()),
|
||||
specs);
|
||||
}
|
||||
|
@@ -49,7 +49,7 @@ TEST(std_test, path) {
|
||||
}
|
||||
|
||||
// Intentionally delayed include to test #4303
|
||||
#include "fmt/ranges.h"
|
||||
# include "fmt/ranges.h"
|
||||
|
||||
// Test ambiguity problem described in #2954.
|
||||
TEST(ranges_std_test, format_vector_path) {
|
||||
@@ -276,18 +276,16 @@ TEST(std_test, variant) {
|
||||
|
||||
TEST(std_test, error_code) {
|
||||
auto& generic = std::generic_category();
|
||||
EXPECT_EQ("generic:42",
|
||||
fmt::format(FMT_STRING("{0}"), std::error_code(42, generic)));
|
||||
EXPECT_EQ(" generic:42",
|
||||
fmt::format(FMT_STRING("{:>12}"), std::error_code(42, generic)));
|
||||
EXPECT_EQ("generic:42 ",
|
||||
fmt::format(FMT_STRING("{:12}"), std::error_code(42, generic)));
|
||||
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())));
|
||||
EXPECT_EQ(fmt::format("{}", std::error_code(42, generic)), "generic:42");
|
||||
EXPECT_EQ(fmt::format("{:>12}", std::error_code(42, generic)),
|
||||
" generic:42");
|
||||
EXPECT_EQ(fmt::format("{:12}", std::error_code(42, generic)), "generic:42 ");
|
||||
EXPECT_EQ(fmt::format("{}", std::error_code(42, fmt::system_category())),
|
||||
"system:42");
|
||||
EXPECT_EQ(fmt::format("{}", std::error_code(-42, fmt::system_category())),
|
||||
"system:-42");
|
||||
auto ec = std::make_error_code(std::errc::value_too_large);
|
||||
EXPECT_EQ(fmt::format("{:s}", ec), ec.message());
|
||||
}
|
||||
|
||||
template <typename Catch> void exception_test() {
|
||||
|
Reference in New Issue
Block a user