Apply coding conventions and use constexpr

This commit is contained in:
Victor Zverovich
2025-03-02 08:02:14 -08:00
parent 864bdf9638
commit 9212ff6ca1
4 changed files with 30 additions and 32 deletions

View File

@ -1064,16 +1064,13 @@ template <typename Char> struct named_arg_info {
int id; int id;
}; };
// named_args is non-const to suppress a bogus -Wmaybe-uninitalized in gcc 13.
template <typename Char> template <typename Char>
FMT_CONSTEXPR void check_for_duplicate(named_arg_info<Char>* named_args, FMT_CONSTEXPR void check_for_duplicate(named_arg_info<Char>* named_args,
int named_arg_index, int named_arg_index,
basic_string_view<Char> arg_name) { basic_string_view<Char> arg_name) {
if (named_arg_index <= 0) return; for (int i = 0; i < named_arg_index; ++i) {
if (named_args[i].name == arg_name) report_error("duplicate named arg");
for (auto i = 0; i < named_arg_index; ++i) {
if (basic_string_view<Char>(named_args[i].name) == arg_name) {
report_error("duplicate named args found");
}
} }
} }
@ -1081,7 +1078,6 @@ template <typename Char, typename T, FMT_ENABLE_IF(!is_named_arg<T>::value)>
void init_named_arg(named_arg_info<Char>*, int& arg_index, int&, const T&) { void init_named_arg(named_arg_info<Char>*, int& arg_index, int&, const T&) {
++arg_index; ++arg_index;
} }
template <typename Char, typename T, FMT_ENABLE_IF(is_named_arg<T>::value)> template <typename Char, typename T, FMT_ENABLE_IF(is_named_arg<T>::value)>
void init_named_arg(named_arg_info<Char>* named_args, int& arg_index, void init_named_arg(named_arg_info<Char>* named_args, int& arg_index,
int& named_arg_index, const T& arg) { int& named_arg_index, const T& arg) {

View File

@ -190,11 +190,11 @@ enum class emphasis : uint8_t {
// rgb is a struct for red, green and blue colors. // rgb is a struct for red, green and blue colors.
// Using the name "rgb" makes some editors show the color in a tooltip. // Using the name "rgb" makes some editors show the color in a tooltip.
struct rgb { struct rgb {
FMT_CONSTEXPR rgb() : r(0), g(0), b(0) {} constexpr rgb() : r(0), g(0), b(0) {}
FMT_CONSTEXPR rgb(uint8_t r_, uint8_t g_, uint8_t b_) : r(r_), g(g_), b(b_) {} constexpr rgb(uint8_t r_, uint8_t g_, uint8_t b_) : r(r_), g(g_), b(b_) {}
FMT_CONSTEXPR rgb(uint32_t hex) constexpr rgb(uint32_t hex)
: r((hex >> 16) & 0xFF), g((hex >> 8) & 0xFF), b(hex & 0xFF) {} : r((hex >> 16) & 0xFF), g((hex >> 8) & 0xFF), b(hex & 0xFF) {}
FMT_CONSTEXPR rgb(color hex) constexpr rgb(color hex)
: r((uint32_t(hex) >> 16) & 0xFF), : r((uint32_t(hex) >> 16) & 0xFF),
g((uint32_t(hex) >> 8) & 0xFF), g((uint32_t(hex) >> 8) & 0xFF),
b(uint32_t(hex) & 0xFF) {} b(uint32_t(hex) & 0xFF) {}
@ -205,30 +205,30 @@ struct rgb {
namespace detail { namespace detail {
// a bit-packed variant of an RGB color, a terminal color, or unset color. // A bit-packed variant of an RGB color, a terminal color, or unset color.
// see text_style for the bit-packing scheme. // see text_style for the bit-packing scheme.
struct color_type { struct color_type {
FMT_CONSTEXPR color_type() noexcept = default; constexpr color_type() noexcept = default;
FMT_CONSTEXPR color_type(color rgb_color) noexcept constexpr color_type(color rgb_color) noexcept
: value_(static_cast<uint32_t>(rgb_color) | (1 << 24)) {} : value_(static_cast<uint32_t>(rgb_color) | (1 << 24)) {}
FMT_CONSTEXPR color_type(rgb rgb_color) noexcept constexpr color_type(rgb rgb_color) noexcept
: color_type(static_cast<color>( : color_type(static_cast<color>(
(static_cast<uint32_t>(rgb_color.r) << 16) | (static_cast<uint32_t>(rgb_color.r) << 16) |
(static_cast<uint32_t>(rgb_color.g) << 8) | rgb_color.b)) {} (static_cast<uint32_t>(rgb_color.g) << 8) | rgb_color.b)) {}
FMT_CONSTEXPR color_type(terminal_color term_color) noexcept constexpr color_type(terminal_color term_color) noexcept
: value_(static_cast<uint32_t>(term_color) | (3 << 24)) {} : value_(static_cast<uint32_t>(term_color) | (3 << 24)) {}
FMT_CONSTEXPR auto is_terminal_color() const noexcept -> bool { constexpr auto is_terminal_color() const noexcept -> bool {
return (value_ & (1 << 25)) != 0; return (value_ & (1 << 25)) != 0;
} }
FMT_CONSTEXPR auto value() const noexcept -> uint32_t { constexpr auto value() const noexcept -> uint32_t {
return value_ & 0xFFFFFF; return value_ & 0xFFFFFF;
} }
FMT_CONSTEXPR color_type(uint32_t value) noexcept : value_(value) {} constexpr color_type(uint32_t value) noexcept : value_(value) {}
uint32_t value_{}; uint32_t value_ = 0;
}; };
} // namespace detail } // namespace detail
@ -341,7 +341,7 @@ class text_style {
friend FMT_CONSTEXPR auto bg(detail::color_type background) noexcept friend FMT_CONSTEXPR auto bg(detail::color_type background) noexcept
-> text_style; -> text_style;
uint64_t style_{}; uint64_t style_ = 0;
}; };
/// Creates a text style from the foreground (text) color. /// Creates a text style from the foreground (text) color.
@ -490,7 +490,7 @@ void vformat_to(buffer<Char>& buf, text_style ts, basic_string_view<Char> fmt,
buf.append(background.begin(), background.end()); buf.append(background.begin(), background.end());
} }
vformat_to(buf, fmt, args); vformat_to(buf, fmt, args);
if (ts != text_style{}) reset_color<Char>(buf); if (ts != text_style()) reset_color<Char>(buf);
} }
} // namespace detail } // namespace detail

View File

@ -12,9 +12,9 @@
#include "gtest-extra.h" // EXPECT_WRITE, EXPECT_THROW_MSG #include "gtest-extra.h" // EXPECT_WRITE, EXPECT_THROW_MSG
TEST(color_test, text_style) { TEST(color_test, text_style) {
EXPECT_FALSE(fmt::text_style{}.has_foreground()); EXPECT_FALSE(fmt::text_style().has_foreground());
EXPECT_FALSE(fmt::text_style{}.has_background()); EXPECT_FALSE(fmt::text_style().has_background());
EXPECT_FALSE(fmt::text_style{}.has_emphasis()); EXPECT_FALSE(fmt::text_style().has_emphasis());
EXPECT_TRUE(fg(fmt::rgb(0)).has_foreground()); EXPECT_TRUE(fg(fmt::rgb(0)).has_foreground());
EXPECT_FALSE(fg(fmt::rgb(0)).has_background()); EXPECT_FALSE(fg(fmt::rgb(0)).has_background());
@ -57,18 +57,20 @@ TEST(color_test, text_style) {
EXPECT_NO_THROW(fg(fmt::terminal_color::white) | EXPECT_NO_THROW(fg(fmt::terminal_color::white) |
bg(fmt::terminal_color::white)); bg(fmt::terminal_color::white));
EXPECT_NO_THROW(fg(fmt::terminal_color::white) | bg(fmt::rgb(0xFFFFFF))); EXPECT_NO_THROW(fg(fmt::terminal_color::white) | bg(fmt::rgb(0xFFFFFF)));
EXPECT_NO_THROW(fg(fmt::terminal_color::white) | fmt::text_style{}); EXPECT_NO_THROW(fg(fmt::terminal_color::white) | fmt::text_style());
EXPECT_NO_THROW(bg(fmt::terminal_color::white) | fmt::text_style{}); EXPECT_NO_THROW(bg(fmt::terminal_color::white) | fmt::text_style());
} }
TEST(color_test, format) { TEST(color_test, format) {
EXPECT_EQ(fmt::format(fmt::text_style{}, "no style"), "no style"); EXPECT_EQ(fmt::format(fmt::text_style(), "no style"), "no style");
EXPECT_EQ(fmt::format(fg(fmt::rgb(255, 20, 30)), "rgb(255,20,30)"), EXPECT_EQ(fmt::format(fg(fmt::rgb(255, 20, 30)), "rgb(255,20,30)"),
"\x1b[38;2;255;020;030mrgb(255,20,30)\x1b[0m"); "\x1b[38;2;255;020;030mrgb(255,20,30)\x1b[0m");
EXPECT_EQ(fmt::format(fg(fmt::rgb(255, 0, 0)) | fg(fmt::rgb(0, 20, 30)), "rgb(255,20,30)"), EXPECT_EQ(fmt::format(fg(fmt::rgb(255, 0, 0)) | fg(fmt::rgb(0, 20, 30)),
"rgb(255,20,30)"),
"\x1b[38;2;255;020;030mrgb(255,20,30)\x1b[0m"); "\x1b[38;2;255;020;030mrgb(255,20,30)\x1b[0m");
EXPECT_EQ(fmt::format(fg(fmt::rgb(0, 0, 0)) | fg(fmt::rgb(0, 0, 0)), "rgb(0,0,0)"), EXPECT_EQ(
"\x1b[38;2;000;000;000mrgb(0,0,0)\x1b[0m"); fmt::format(fg(fmt::rgb(0, 0, 0)) | fg(fmt::rgb(0, 0, 0)), "rgb(0,0,0)"),
"\x1b[38;2;000;000;000mrgb(0,0,0)\x1b[0m");
EXPECT_EQ(fmt::format(fg(fmt::color::blue), "blue"), EXPECT_EQ(fmt::format(fg(fmt::color::blue), "blue"),
"\x1b[38;2;000;000;255mblue\x1b[0m"); "\x1b[38;2;000;000;255mblue\x1b[0m");
EXPECT_EQ( EXPECT_EQ(

View File

@ -600,7 +600,7 @@ TEST(format_test, named_arg) {
format_error, format_error,
"cannot switch from manual to automatic argument indexing"); "cannot switch from manual to automatic argument indexing");
EXPECT_THROW_MSG((void)fmt::format("{a}", fmt::arg("a", 1), EXPECT_THROW_MSG((void)fmt::format("{a}", fmt::arg("a", 1),
fmt::arg("a", 10)), format_error, "duplicate named args found"); fmt::arg("a", 10)), format_error, "duplicate named arg");
} }
TEST(format_test, auto_arg_index) { TEST(format_test, auto_arg_index) {