mirror of
https://github.com/fmtlib/fmt.git
synced 2024-11-22 10:30:05 +00:00
Coding conventions and minor fixes
This commit is contained in:
parent
71144eeaff
commit
c70e7b7473
@ -2096,17 +2096,17 @@ class year_month_day {
|
|||||||
year_month_day() = default;
|
year_month_day() = default;
|
||||||
constexpr year_month_day(const year& y, const month& m, const day& d) noexcept
|
constexpr year_month_day(const year& y, const month& m, const day& d) noexcept
|
||||||
: year_(y), month_(m), day_(d) {}
|
: year_(y), month_(m), day_(d) {}
|
||||||
constexpr fmt::year year() const noexcept { return year_; }
|
constexpr auto year() const noexcept -> fmt::year { return year_; }
|
||||||
constexpr fmt::month month() const noexcept { return month_; }
|
constexpr auto month() const noexcept -> fmt::month { return month_; }
|
||||||
constexpr fmt::day day() const noexcept { return day_; }
|
constexpr auto day() const noexcept -> fmt::day { return day_; }
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
template <typename Char>
|
template <typename Char>
|
||||||
struct formatter<weekday, Char> : private formatter<std::tm, Char> {
|
struct formatter<weekday, Char> : private formatter<std::tm, Char> {
|
||||||
private:
|
private:
|
||||||
bool localized_{false};
|
bool localized_ = false;
|
||||||
bool use_tm_formatter_{false};
|
bool use_tm_formatter_ = false;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
FMT_CONSTEXPR auto parse(basic_format_parse_context<Char>& ctx)
|
FMT_CONSTEXPR auto parse(basic_format_parse_context<Char>& ctx)
|
||||||
@ -2125,9 +2125,7 @@ struct formatter<weekday, Char> : private formatter<std::tm, Char> {
|
|||||||
auto format(weekday wd, FormatContext& ctx) const -> decltype(ctx.out()) {
|
auto format(weekday wd, FormatContext& ctx) const -> decltype(ctx.out()) {
|
||||||
auto time = std::tm();
|
auto time = std::tm();
|
||||||
time.tm_wday = static_cast<int>(wd.c_encoding());
|
time.tm_wday = static_cast<int>(wd.c_encoding());
|
||||||
if (use_tm_formatter_) {
|
if (use_tm_formatter_) return formatter<std::tm, Char>::format(time, ctx);
|
||||||
return formatter<std::tm, Char>::format(time, ctx);
|
|
||||||
}
|
|
||||||
detail::get_locale loc(localized_, ctx.locale());
|
detail::get_locale loc(localized_, ctx.locale());
|
||||||
auto w = detail::tm_writer<decltype(ctx.out()), Char>(loc, ctx.out(), time);
|
auto w = detail::tm_writer<decltype(ctx.out()), Char>(loc, ctx.out(), time);
|
||||||
w.on_abbr_weekday();
|
w.on_abbr_weekday();
|
||||||
@ -2138,7 +2136,7 @@ struct formatter<weekday, Char> : private formatter<std::tm, Char> {
|
|||||||
template <typename Char>
|
template <typename Char>
|
||||||
struct formatter<day, Char> : private formatter<std::tm, Char> {
|
struct formatter<day, Char> : private formatter<std::tm, Char> {
|
||||||
private:
|
private:
|
||||||
bool use_tm_formatter_{false};
|
bool use_tm_formatter_ = false;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
FMT_CONSTEXPR auto parse(basic_format_parse_context<Char>& ctx)
|
FMT_CONSTEXPR auto parse(basic_format_parse_context<Char>& ctx)
|
||||||
@ -2152,9 +2150,7 @@ struct formatter<day, Char> : private formatter<std::tm, Char> {
|
|||||||
auto format(day d, FormatContext& ctx) const -> decltype(ctx.out()) {
|
auto format(day d, FormatContext& ctx) const -> decltype(ctx.out()) {
|
||||||
auto time = std::tm();
|
auto time = std::tm();
|
||||||
time.tm_mday = static_cast<int>(static_cast<unsigned>(d));
|
time.tm_mday = static_cast<int>(static_cast<unsigned>(d));
|
||||||
if (use_tm_formatter_) {
|
if (use_tm_formatter_) return formatter<std::tm, Char>::format(time, ctx);
|
||||||
return formatter<std::tm, Char>::format(time, ctx);
|
|
||||||
}
|
|
||||||
detail::get_locale loc(false, ctx.locale());
|
detail::get_locale loc(false, ctx.locale());
|
||||||
auto w = detail::tm_writer<decltype(ctx.out()), Char>(loc, ctx.out(), time);
|
auto w = detail::tm_writer<decltype(ctx.out()), Char>(loc, ctx.out(), time);
|
||||||
w.on_day_of_month(detail::numeric_system::standard);
|
w.on_day_of_month(detail::numeric_system::standard);
|
||||||
@ -2165,8 +2161,8 @@ struct formatter<day, Char> : private formatter<std::tm, Char> {
|
|||||||
template <typename Char>
|
template <typename Char>
|
||||||
struct formatter<month, Char> : private formatter<std::tm, Char> {
|
struct formatter<month, Char> : private formatter<std::tm, Char> {
|
||||||
private:
|
private:
|
||||||
bool localized_{false};
|
bool localized_ = false;
|
||||||
bool use_tm_formatter_{false};
|
bool use_tm_formatter_ = false;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
FMT_CONSTEXPR auto parse(basic_format_parse_context<Char>& ctx)
|
FMT_CONSTEXPR auto parse(basic_format_parse_context<Char>& ctx)
|
||||||
@ -2185,9 +2181,7 @@ struct formatter<month, Char> : private formatter<std::tm, Char> {
|
|||||||
auto format(month m, FormatContext& ctx) const -> decltype(ctx.out()) {
|
auto format(month m, FormatContext& ctx) const -> decltype(ctx.out()) {
|
||||||
auto time = std::tm();
|
auto time = std::tm();
|
||||||
time.tm_mon = static_cast<int>(static_cast<unsigned>(m)) - 1;
|
time.tm_mon = static_cast<int>(static_cast<unsigned>(m)) - 1;
|
||||||
if (use_tm_formatter_) {
|
if (use_tm_formatter_) return formatter<std::tm, Char>::format(time, ctx);
|
||||||
return formatter<std::tm, Char>::format(time, ctx);
|
|
||||||
}
|
|
||||||
detail::get_locale loc(localized_, ctx.locale());
|
detail::get_locale loc(localized_, ctx.locale());
|
||||||
auto w = detail::tm_writer<decltype(ctx.out()), Char>(loc, ctx.out(), time);
|
auto w = detail::tm_writer<decltype(ctx.out()), Char>(loc, ctx.out(), time);
|
||||||
w.on_abbr_month();
|
w.on_abbr_month();
|
||||||
@ -2198,7 +2192,7 @@ struct formatter<month, Char> : private formatter<std::tm, Char> {
|
|||||||
template <typename Char>
|
template <typename Char>
|
||||||
struct formatter<year, Char> : private formatter<std::tm, Char> {
|
struct formatter<year, Char> : private formatter<std::tm, Char> {
|
||||||
private:
|
private:
|
||||||
bool use_tm_formatter_{false};
|
bool use_tm_formatter_ = false;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
FMT_CONSTEXPR auto parse(basic_format_parse_context<Char>& ctx)
|
FMT_CONSTEXPR auto parse(basic_format_parse_context<Char>& ctx)
|
||||||
@ -2212,9 +2206,7 @@ struct formatter<year, Char> : private formatter<std::tm, Char> {
|
|||||||
auto format(year y, FormatContext& ctx) const -> decltype(ctx.out()) {
|
auto format(year y, FormatContext& ctx) const -> decltype(ctx.out()) {
|
||||||
auto time = std::tm();
|
auto time = std::tm();
|
||||||
time.tm_year = static_cast<int>(y) - 1900;
|
time.tm_year = static_cast<int>(y) - 1900;
|
||||||
if (use_tm_formatter_) {
|
if (use_tm_formatter_) return formatter<std::tm, Char>::format(time, ctx);
|
||||||
return formatter<std::tm, Char>::format(time, ctx);
|
|
||||||
}
|
|
||||||
detail::get_locale loc(false, ctx.locale());
|
detail::get_locale loc(false, ctx.locale());
|
||||||
auto w = detail::tm_writer<decltype(ctx.out()), Char>(loc, ctx.out(), time);
|
auto w = detail::tm_writer<decltype(ctx.out()), Char>(loc, ctx.out(), time);
|
||||||
w.on_year(detail::numeric_system::standard);
|
w.on_year(detail::numeric_system::standard);
|
||||||
@ -2225,7 +2217,7 @@ struct formatter<year, Char> : private formatter<std::tm, Char> {
|
|||||||
template <typename Char>
|
template <typename Char>
|
||||||
struct formatter<year_month_day, Char> : private formatter<std::tm, Char> {
|
struct formatter<year_month_day, Char> : private formatter<std::tm, Char> {
|
||||||
private:
|
private:
|
||||||
bool use_tm_formatter_{false};
|
bool use_tm_formatter_ = false;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
FMT_CONSTEXPR auto parse(basic_format_parse_context<Char>& ctx)
|
FMT_CONSTEXPR auto parse(basic_format_parse_context<Char>& ctx)
|
||||||
@ -2242,9 +2234,7 @@ struct formatter<year_month_day, Char> : private formatter<std::tm, Char> {
|
|||||||
time.tm_year = static_cast<int>(val.year()) - 1900;
|
time.tm_year = static_cast<int>(val.year()) - 1900;
|
||||||
time.tm_mon = static_cast<int>(static_cast<unsigned>(val.month())) - 1;
|
time.tm_mon = static_cast<int>(static_cast<unsigned>(val.month())) - 1;
|
||||||
time.tm_mday = static_cast<int>(static_cast<unsigned>(val.day()));
|
time.tm_mday = static_cast<int>(static_cast<unsigned>(val.day()));
|
||||||
if (use_tm_formatter_) {
|
if (use_tm_formatter_) return formatter<std::tm, Char>::format(time, ctx);
|
||||||
return formatter<std::tm, Char>::format(time, ctx);
|
|
||||||
}
|
|
||||||
detail::get_locale loc(true, ctx.locale());
|
detail::get_locale loc(true, ctx.locale());
|
||||||
auto w = detail::tm_writer<decltype(ctx.out()), Char>(loc, ctx.out(), time);
|
auto w = detail::tm_writer<decltype(ctx.out()), Char>(loc, ctx.out(), time);
|
||||||
w.on_iso_date();
|
w.on_iso_date();
|
||||||
|
@ -754,14 +754,19 @@ TEST(chrono_test, weekday) {
|
|||||||
|
|
||||||
auto sat = fmt::weekday(6);
|
auto sat = fmt::weekday(6);
|
||||||
|
|
||||||
|
auto tm = std::tm();
|
||||||
|
tm.tm_wday = static_cast<int>(sat.c_encoding());
|
||||||
|
|
||||||
EXPECT_EQ(fmt::format("{}", sat), "Sat");
|
EXPECT_EQ(fmt::format("{}", sat), "Sat");
|
||||||
EXPECT_EQ(fmt::format("{:%a}", sat), "Sat");
|
EXPECT_EQ(fmt::format("{:%a}", sat), "Sat");
|
||||||
EXPECT_EQ(fmt::format("{:%A}", sat), "Saturday");
|
EXPECT_EQ(fmt::format("{:%A}", sat), "Saturday");
|
||||||
|
EXPECT_EQ(fmt::format("{:%a}", tm), "Sat");
|
||||||
|
|
||||||
if (loc != std::locale::classic()) {
|
if (loc != std::locale::classic()) {
|
||||||
auto saturdays = std::vector<std::string>{"sáb", "sá."};
|
auto saturdays = std::vector<std::string>{"sáb", "sá."};
|
||||||
EXPECT_THAT(saturdays, Contains(fmt::format(loc, "{:L}", sat)));
|
EXPECT_THAT(saturdays, Contains(fmt::format(loc, "{:L}", sat)));
|
||||||
EXPECT_THAT(saturdays, Contains(fmt::format(loc, "{:%a}", sat)));
|
EXPECT_THAT(saturdays, Contains(fmt::format(loc, "{:%a}", sat)));
|
||||||
|
EXPECT_THAT(saturdays, Contains(fmt::format(loc, "{:%a}", tm)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1013,6 +1018,9 @@ TEST(chrono_test, out_of_range) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST(chrono_test, year_month_day) {
|
TEST(chrono_test, year_month_day) {
|
||||||
|
auto loc = get_locale("es_ES.UTF-8");
|
||||||
|
std::locale::global(loc);
|
||||||
|
|
||||||
auto year = fmt::year(2024);
|
auto year = fmt::year(2024);
|
||||||
auto month = fmt::month(1);
|
auto month = fmt::month(1);
|
||||||
auto day = fmt::day(1);
|
auto day = fmt::day(1);
|
||||||
@ -1035,8 +1043,6 @@ TEST(chrono_test, year_month_day) {
|
|||||||
EXPECT_EQ(fmt::format("{:%Y-%b-%d}", ymd), "2024-Jan-01");
|
EXPECT_EQ(fmt::format("{:%Y-%b-%d}", ymd), "2024-Jan-01");
|
||||||
EXPECT_EQ(fmt::format("{:%Y-%B-%d}", ymd), "2024-January-01");
|
EXPECT_EQ(fmt::format("{:%Y-%B-%d}", ymd), "2024-January-01");
|
||||||
|
|
||||||
auto loc = get_locale("es_ES.UTF-8");
|
|
||||||
std::locale::global(loc);
|
|
||||||
if (loc != std::locale::classic()) {
|
if (loc != std::locale::classic()) {
|
||||||
auto months = std::vector<std::string>{"ene.", "ene"};
|
auto months = std::vector<std::string>{"ene.", "ene"};
|
||||||
EXPECT_THAT(months, Contains(fmt::format(loc, "{:L}", month)));
|
EXPECT_THAT(months, Contains(fmt::format(loc, "{:L}", month)));
|
||||||
|
Loading…
Reference in New Issue
Block a user