Skip to content

Commit 9212ff6

Browse files
committed
Apply coding conventions and use constexpr
1 parent 864bdf9 commit 9212ff6

File tree

4 files changed

+30
-32
lines changed

4 files changed

+30
-32
lines changed

include/fmt/base.h

+3-7
Original file line numberDiff line numberDiff line change
@@ -1064,24 +1064,20 @@ template <typename Char> struct named_arg_info {
10641064
int id;
10651065
};
10661066

1067+
// named_args is non-const to suppress a bogus -Wmaybe-uninitalized in gcc 13.
10671068
template <typename Char>
10681069
FMT_CONSTEXPR void check_for_duplicate(named_arg_info<Char>* named_args,
10691070
int named_arg_index,
10701071
basic_string_view<Char> arg_name) {
1071-
if (named_arg_index <= 0) return;
1072-
1073-
for (auto i = 0; i < named_arg_index; ++i) {
1074-
if (basic_string_view<Char>(named_args[i].name) == arg_name) {
1075-
report_error("duplicate named args found");
1076-
}
1072+
for (int i = 0; i < named_arg_index; ++i) {
1073+
if (named_args[i].name == arg_name) report_error("duplicate named arg");
10771074
}
10781075
}
10791076

10801077
template <typename Char, typename T, FMT_ENABLE_IF(!is_named_arg<T>::value)>
10811078
void init_named_arg(named_arg_info<Char>*, int& arg_index, int&, const T&) {
10821079
++arg_index;
10831080
}
1084-
10851081
template <typename Char, typename T, FMT_ENABLE_IF(is_named_arg<T>::value)>
10861082
void init_named_arg(named_arg_info<Char>* named_args, int& arg_index,
10871083
int& named_arg_index, const T& arg) {

include/fmt/color.h

+15-15
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,11 @@ enum class emphasis : uint8_t {
190190
// rgb is a struct for red, green and blue colors.
191191
// Using the name "rgb" makes some editors show the color in a tooltip.
192192
struct rgb {
193-
FMT_CONSTEXPR rgb() : r(0), g(0), b(0) {}
194-
FMT_CONSTEXPR rgb(uint8_t r_, uint8_t g_, uint8_t b_) : r(r_), g(g_), b(b_) {}
195-
FMT_CONSTEXPR rgb(uint32_t hex)
193+
constexpr rgb() : r(0), g(0), b(0) {}
194+
constexpr rgb(uint8_t r_, uint8_t g_, uint8_t b_) : r(r_), g(g_), b(b_) {}
195+
constexpr rgb(uint32_t hex)
196196
: r((hex >> 16) & 0xFF), g((hex >> 8) & 0xFF), b(hex & 0xFF) {}
197-
FMT_CONSTEXPR rgb(color hex)
197+
constexpr rgb(color hex)
198198
: r((uint32_t(hex) >> 16) & 0xFF),
199199
g((uint32_t(hex) >> 8) & 0xFF),
200200
b(uint32_t(hex) & 0xFF) {}
@@ -205,30 +205,30 @@ struct rgb {
205205

206206
namespace detail {
207207

208-
// a bit-packed variant of an RGB color, a terminal color, or unset color.
208+
// A bit-packed variant of an RGB color, a terminal color, or unset color.
209209
// see text_style for the bit-packing scheme.
210210
struct color_type {
211-
FMT_CONSTEXPR color_type() noexcept = default;
212-
FMT_CONSTEXPR color_type(color rgb_color) noexcept
211+
constexpr color_type() noexcept = default;
212+
constexpr color_type(color rgb_color) noexcept
213213
: value_(static_cast<uint32_t>(rgb_color) | (1 << 24)) {}
214-
FMT_CONSTEXPR color_type(rgb rgb_color) noexcept
214+
constexpr color_type(rgb rgb_color) noexcept
215215
: color_type(static_cast<color>(
216216
(static_cast<uint32_t>(rgb_color.r) << 16) |
217217
(static_cast<uint32_t>(rgb_color.g) << 8) | rgb_color.b)) {}
218-
FMT_CONSTEXPR color_type(terminal_color term_color) noexcept
218+
constexpr color_type(terminal_color term_color) noexcept
219219
: value_(static_cast<uint32_t>(term_color) | (3 << 24)) {}
220220

221-
FMT_CONSTEXPR auto is_terminal_color() const noexcept -> bool {
221+
constexpr auto is_terminal_color() const noexcept -> bool {
222222
return (value_ & (1 << 25)) != 0;
223223
}
224224

225-
FMT_CONSTEXPR auto value() const noexcept -> uint32_t {
225+
constexpr auto value() const noexcept -> uint32_t {
226226
return value_ & 0xFFFFFF;
227227
}
228228

229-
FMT_CONSTEXPR color_type(uint32_t value) noexcept : value_(value) {}
229+
constexpr color_type(uint32_t value) noexcept : value_(value) {}
230230

231-
uint32_t value_{};
231+
uint32_t value_ = 0;
232232
};
233233
} // namespace detail
234234

@@ -341,7 +341,7 @@ class text_style {
341341
friend FMT_CONSTEXPR auto bg(detail::color_type background) noexcept
342342
-> text_style;
343343

344-
uint64_t style_{};
344+
uint64_t style_ = 0;
345345
};
346346

347347
/// 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,
490490
buf.append(background.begin(), background.end());
491491
}
492492
vformat_to(buf, fmt, args);
493-
if (ts != text_style{}) reset_color<Char>(buf);
493+
if (ts != text_style()) reset_color<Char>(buf);
494494
}
495495
} // namespace detail
496496

test/color-test.cc

+11-9
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
#include "gtest-extra.h" // EXPECT_WRITE, EXPECT_THROW_MSG
1313

1414
TEST(color_test, text_style) {
15-
EXPECT_FALSE(fmt::text_style{}.has_foreground());
16-
EXPECT_FALSE(fmt::text_style{}.has_background());
17-
EXPECT_FALSE(fmt::text_style{}.has_emphasis());
15+
EXPECT_FALSE(fmt::text_style().has_foreground());
16+
EXPECT_FALSE(fmt::text_style().has_background());
17+
EXPECT_FALSE(fmt::text_style().has_emphasis());
1818

1919
EXPECT_TRUE(fg(fmt::rgb(0)).has_foreground());
2020
EXPECT_FALSE(fg(fmt::rgb(0)).has_background());
@@ -57,18 +57,20 @@ TEST(color_test, text_style) {
5757
EXPECT_NO_THROW(fg(fmt::terminal_color::white) |
5858
bg(fmt::terminal_color::white));
5959
EXPECT_NO_THROW(fg(fmt::terminal_color::white) | bg(fmt::rgb(0xFFFFFF)));
60-
EXPECT_NO_THROW(fg(fmt::terminal_color::white) | fmt::text_style{});
61-
EXPECT_NO_THROW(bg(fmt::terminal_color::white) | fmt::text_style{});
60+
EXPECT_NO_THROW(fg(fmt::terminal_color::white) | fmt::text_style());
61+
EXPECT_NO_THROW(bg(fmt::terminal_color::white) | fmt::text_style());
6262
}
6363

6464
TEST(color_test, format) {
65-
EXPECT_EQ(fmt::format(fmt::text_style{}, "no style"), "no style");
65+
EXPECT_EQ(fmt::format(fmt::text_style(), "no style"), "no style");
6666
EXPECT_EQ(fmt::format(fg(fmt::rgb(255, 20, 30)), "rgb(255,20,30)"),
6767
"\x1b[38;2;255;020;030mrgb(255,20,30)\x1b[0m");
68-
EXPECT_EQ(fmt::format(fg(fmt::rgb(255, 0, 0)) | fg(fmt::rgb(0, 20, 30)), "rgb(255,20,30)"),
68+
EXPECT_EQ(fmt::format(fg(fmt::rgb(255, 0, 0)) | fg(fmt::rgb(0, 20, 30)),
69+
"rgb(255,20,30)"),
6970
"\x1b[38;2;255;020;030mrgb(255,20,30)\x1b[0m");
70-
EXPECT_EQ(fmt::format(fg(fmt::rgb(0, 0, 0)) | fg(fmt::rgb(0, 0, 0)), "rgb(0,0,0)"),
71-
"\x1b[38;2;000;000;000mrgb(0,0,0)\x1b[0m");
71+
EXPECT_EQ(
72+
fmt::format(fg(fmt::rgb(0, 0, 0)) | fg(fmt::rgb(0, 0, 0)), "rgb(0,0,0)"),
73+
"\x1b[38;2;000;000;000mrgb(0,0,0)\x1b[0m");
7274
EXPECT_EQ(fmt::format(fg(fmt::color::blue), "blue"),
7375
"\x1b[38;2;000;000;255mblue\x1b[0m");
7476
EXPECT_EQ(

test/format-test.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ TEST(format_test, named_arg) {
600600
format_error,
601601
"cannot switch from manual to automatic argument indexing");
602602
EXPECT_THROW_MSG((void)fmt::format("{a}", fmt::arg("a", 1),
603-
fmt::arg("a", 10)), format_error, "duplicate named args found");
603+
fmt::arg("a", 10)), format_error, "duplicate named arg");
604604
}
605605

606606
TEST(format_test, auto_arg_index) {

0 commit comments

Comments
 (0)