Skip to content

Commit 9c394b4

Browse files
committed
Address cppcoreguidelines-avoid-magic-numbers warnings
1 parent 7e79103 commit 9c394b4

File tree

7 files changed

+60
-44
lines changed

7 files changed

+60
-44
lines changed

include/skyr/core/schemes.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ constexpr auto is_special(std::string_view scheme) noexcept -> bool {
2222
/// \returns
2323
constexpr auto default_port(std::string_view scheme) noexcept -> std::optional<std::uint16_t> {
2424
if (scheme == "ftp") {
25-
return 21;
25+
return 21; // NOLINT(cppcoreguidelines-avoid-magic-numbers)
2626
} else if ((scheme == "http") || (scheme == "ws")) {
27-
return 80;
27+
return 80; // NOLINT(cppcoreguidelines-avoid-magic-numbers)
2828
} else if ((scheme == "https") || (scheme == "wss")) {
29-
return 443;
29+
return 443; // NOLINT(cppcoreguidelines-avoid-magic-numbers)
3030
}
3131
return std::nullopt;
3232
}

include/skyr/domain/punycode.hpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ constexpr auto adapt(uint32_t delta, uint32_t numpoints, bool firsttime) -> std:
4141
delta /= base - tmin;
4242
k += base;
4343
}
44-
return k + (base - tmin + 1ul) * delta / (delta + skew);
44+
return k + ((base - tmin + 1ul) * delta / (delta + skew));
4545
}
4646
} // namespace punycode
4747

@@ -54,16 +54,18 @@ constexpr auto adapt(uint32_t delta, uint32_t numpoints, bool firsttime) -> std:
5454
inline auto punycode_encode(std::u32string_view input, std::string* output) -> std::expected<void, domain_errc> {
5555
using namespace punycode::constants;
5656

57-
constexpr auto is_ascii_value = [](auto c) { return c < 0x80; };
57+
constexpr auto is_ascii_value = [](auto c) { return c < 0x80; }; // NOLINT(cppcoreguidelines-avoid-magic-numbers)
5858
constexpr auto to_char = [](auto c) { return static_cast<char>(c); };
5959

6060
// encode_digit(d,flag) returns the basic code point whose value
6161
// (when used for representing integers) is d, which needs to be in
6262
// the range 0 to base-1. The lowercase form is used unless flag is
6363
// nonzero, in which case the uppercase form is used. The behavior
6464
// is undefined if flag is nonzero and digit d has no uppercase form.
65-
constexpr auto encode_digit = [](std::uint32_t d, std::uint32_t flag) -> char {
66-
return d + 0x16ul + (0x4bul * (d < 0x1aul)) - ((flag != 0ul) << 0x5ul);
65+
constexpr auto encode_digit = [](const std::uint32_t d, const std::uint32_t flag) -> char {
66+
return static_cast<char>(d + 0x16ul + (0x4bul * (d < 0x1aul)) -
67+
((flag != 0ul) << 0x5ul) // NOLINT(cppcoreguidelines-avoid-magic-numbers)
68+
);
6769
// 0..25 map to ASCII a..z or A..Z
6870
// 26..35 map to ASCII 0..9
6971
};

include/skyr/network/ipv4_address.hpp

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,23 +57,27 @@ class ipv4_address {
5757
/// The address in bytes in network byte order
5858
/// \returns The address in bytes
5959
[[nodiscard]] constexpr auto to_bytes() const noexcept -> std::array<unsigned char, 4> {
60-
auto addr = address();
61-
return {{static_cast<unsigned char>(addr >> 24u), static_cast<unsigned char>(addr >> 16u),
62-
static_cast<unsigned char>(addr >> 8u), static_cast<unsigned char>(addr)}};
60+
const auto addr = address();
61+
return {{static_cast<unsigned char>(addr >> 24u),
62+
static_cast<unsigned char>(addr >> 16u), // NOLINT(cppcoreguidelines-avoid-magic-numbers)
63+
static_cast<unsigned char>(addr >> 8u),
64+
static_cast<unsigned char>(addr)}}; // NOLINT(cppcoreguidelines-avoid-magic-numbers)
6365
}
6466

6567
/// \returns The address as a string
6668
[[nodiscard]] auto serialize() const -> std::string {
6769
using namespace std::string_literals;
6870
using namespace std::string_view_literals;
6971

70-
constexpr auto separator = [](auto i) { return (i < 4) ? "."sv : ""sv; };
72+
constexpr auto separator = [](auto i) {
73+
return (i < 4) ? "."sv : ""sv;
74+
}; // NOLINT(cppcoreguidelines-avoid-magic-numbers)
7175

7276
auto output = ""s;
7377
auto n = address();
7478
for (auto i = 1U; i <= 4U; ++i) {
7579
output = std::format("{}{}{}", separator(i), n % 256, output);
76-
n >>= 8;
80+
n >>= 8; // NOLINT(cppcoreguidelines-avoid-magic-numbers)
7781
}
7882
return output;
7983
}
@@ -82,21 +86,21 @@ class ipv4_address {
8286
namespace details {
8387
/// Computes 256^exp efficiently using bit shifts (256 = 2^8, so 256^n = 2^(8n))
8488
constexpr auto pow256(unsigned int exp) noexcept -> std::uint64_t {
85-
return 1ULL << (exp * 8);
89+
return 1ULL << (exp * 8); // NOLINT(cppcoreguidelines-avoid-magic-numbers)
8690
}
8791

8892
constexpr auto parse_ipv4_number(std::string_view input, bool* validation_error)
8993
-> std::expected<std::uint64_t, ipv4_address_errc> {
90-
auto base = 10;
94+
auto base = 10; // NOLINT(cppcoreguidelines-avoid-magic-numbers)
9195

9296
if ((input.size() >= 2) && (input[0] == '0') && (std::tolower(input[1], std::locale::classic()) == 'x')) {
9397
*validation_error |= true;
9498
input = input.substr(2);
95-
base = 16;
99+
base = 16; // NOLINT(cppcoreguidelines-avoid-magic-numbers)
96100
} else if ((input.size() >= 2) && (input[0] == '0')) {
97101
*validation_error |= true;
98102
input = input.substr(1);
99-
base = 8;
103+
base = 8; // NOLINT(cppcoreguidelines-avoid-magic-numbers)
100104
}
101105

102106
if (input.empty()) {
@@ -162,7 +166,9 @@ constexpr auto parse_ipv4_address(std::string_view input, bool* validation_error
162166
numbers.push_back(number.value());
163167
}
164168

165-
constexpr auto greater_than_255 = [](auto number) { return number > 255; };
169+
constexpr auto greater_than_255 = [](auto number) {
170+
return number > 255;
171+
}; // NOLINT(cppcoreguidelines-avoid-magic-numbers)
166172

167173
if (std::ranges::cend(numbers) != std::ranges::find_if(numbers, greater_than_255)) {
168174
*validation_error |= true;
@@ -176,7 +182,7 @@ constexpr auto parse_ipv4_address(std::string_view input, bool* validation_error
176182
return std::unexpected(ipv4_address_errc::overflow);
177183
}
178184

179-
if (numbers.back() >= details::pow256(5 - numbers.size())) {
185+
if (numbers.back() >= details::pow256(5 - numbers.size())) { // NOLINT(cppcoreguidelines-avoid-magic-numbers)
180186
*validation_error |= true;
181187
return std::unexpected(ipv4_address_errc::overflow);
182188
}
@@ -186,7 +192,7 @@ constexpr auto parse_ipv4_address(std::string_view input, bool* validation_error
186192

187193
auto counter = 0UL;
188194
for (auto&& number : numbers) {
189-
ipv4 += number * details::pow256(3 - counter);
195+
ipv4 += number * details::pow256(3 - counter); // NOLINT(cppcoreguidelines-avoid-magic-numbers)
190196
++counter;
191197
}
192198
return ipv4_address(static_cast<unsigned int>(ipv4));

include/skyr/network/ipv6_address.hpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ class ipv6_address {
120120
}
121121

122122
auto ignore0 = false;
123-
for (auto i = 0UL; i <= 7UL; ++i) {
123+
for (auto i = 0UL; i <= 7UL; ++i) { // NOLINT(cppcoreguidelines-avoid-magic-numbers)
124124
if (ignore0 && (address[i] == 0)) { // NOLINT
125125
continue;
126126
} else if (ignore0) {
@@ -181,7 +181,7 @@ constexpr auto parse_ipv6_address(std::string_view input, bool* validation_error
181181
}
182182

183183
while (it != last) {
184-
if (piece_index == 8) {
184+
if (piece_index == 8) { // NOLINT(cppcoreguidelines-avoid-magic-numbers)
185185
*validation_error |= true;
186186
return std::unexpected(ipv6_address_errc::invalid_piece);
187187
}
@@ -202,7 +202,8 @@ constexpr auto parse_ipv6_address(std::string_view input, bool* validation_error
202202
auto length = 0;
203203

204204
while ((it != last) && ((length < 4) && std::isxdigit(*it, std::locale::classic()))) {
205-
value = value * 0x10 + details::hex_to_dec<decltype(value)>(*it);
205+
value =
206+
(value * 0x10) + details::hex_to_dec<decltype(value)>(*it); // NOLINT(cppcoreguidelines-avoid-magic-numbers)
206207
++it;
207208
++length;
208209
}
@@ -215,7 +216,7 @@ constexpr auto parse_ipv6_address(std::string_view input, bool* validation_error
215216

216217
std::ranges::advance(it, -length);
217218

218-
if (piece_index > 6) {
219+
if (piece_index > 6) { // NOLINT(cppcoreguidelines-avoid-magic-numbers)
219220
*validation_error |= true;
220221
return std::unexpected(ipv6_address_errc::invalid_ipv4_segment_number);
221222
}
@@ -226,7 +227,7 @@ constexpr auto parse_ipv6_address(std::string_view input, bool* validation_error
226227
auto ipv4_piece = std::optional<int>();
227228

228229
if (numbers_seen > 0) {
229-
if ((*it == '.') && (numbers_seen < 4)) {
230+
if ((*it == '.') && (numbers_seen < 4)) { // NOLINT(cppcoreguidelines-avoid-magic-numbers)
230231
++it;
231232
} else {
232233
*validation_error |= true;
@@ -247,18 +248,19 @@ constexpr auto parse_ipv6_address(std::string_view input, bool* validation_error
247248
*validation_error |= true;
248249
return std::unexpected(ipv6_address_errc::invalid_ipv4_segment_number);
249250
} else {
250-
ipv4_piece = ipv4_piece.value() * 10 + number;
251+
ipv4_piece = (ipv4_piece.value() * 10) + number; // NOLINT(cppcoreguidelines-avoid-magic-numbers)
251252
}
252253

253-
if (ipv4_piece.value() > 255) {
254+
if (ipv4_piece.value() > 255) { // NOLINT(cppcoreguidelines-avoid-magic-numbers)
254255
*validation_error |= true;
255256
return std::unexpected(ipv6_address_errc::invalid_ipv4_segment_number);
256257
}
257258

258259
++it;
259260
}
260261

261-
address[piece_index] = static_cast<std::uint16_t>((address[piece_index] << 8) + ipv4_piece.value()); // NOLINT
262+
address[piece_index] = static_cast<std::uint16_t>(
263+
(address[piece_index] << 8) + ipv4_piece.value()); // NOLINT(cppcoreguidelines-avoid-magic-numbers)
262264
++numbers_seen;
263265

264266
if ((numbers_seen == 2) || (numbers_seen == 4)) {
@@ -288,13 +290,14 @@ constexpr auto parse_ipv6_address(std::string_view input, bool* validation_error
288290

289291
if (compress) {
290292
auto swaps = piece_index - compress.value();
291-
piece_index = 7;
293+
piece_index = 7; // NOLINT(cppcoreguidelines-avoid-magic-numbers)
292294
while ((piece_index != 0) && (swaps > 0)) {
293-
std::swap(address[piece_index], address[compress.value() + swaps - 1]); // NOLINT
295+
std::swap(address[piece_index],
296+
address[compress.value() + swaps - 1]); // NOLINT(cppcoreguidelines-avoid-magic-numbers)
294297
--piece_index;
295298
--swaps;
296299
}
297-
} else if (!compress && (piece_index != 8)) {
300+
} else if (!compress && (piece_index != 8)) { // NOLINT(cppcoreguidelines-avoid-magic-numbers)
298301
*validation_error |= true;
299302
return std::unexpected(ipv6_address_errc::compress_expected);
300303
}

include/skyr/unicode/code_points/u16.hpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ class u16_code_point_t {
2727
}
2828

2929
constexpr u16_code_point_t(char16_t lead_value, char16_t trail_value)
30-
: code_point_((lead_value << 10U) + trail_value + constants::surrogates::offset) {
30+
: code_point_((lead_value << 10U) + trail_value +
31+
constants::surrogates::offset) { // NOLINT(cppcoreguidelines-avoid-magic-numbers)
3132
}
3233

3334
///
@@ -39,14 +40,18 @@ class u16_code_point_t {
3940
///
4041
/// \return
4142
[[nodiscard]] auto lead_value() const {
42-
return is_surrogate_pair() ? static_cast<char16_t>((code_point_ >> 10U) + constants::surrogates::lead_offset)
43-
: static_cast<char16_t>(code_point_);
43+
return is_surrogate_pair()
44+
? static_cast<char16_t>(
45+
(code_point_ >> 10U) +
46+
constants::surrogates::lead_offset) // NOLINT(cppcoreguidelines-avoid-magic-numbers)
47+
: static_cast<char16_t>(code_point_);
4448
}
4549

4650
///
4751
/// \return
4852
[[nodiscard]] constexpr auto trail_value() const {
49-
return is_surrogate_pair() ? static_cast<char16_t>((code_point_ & 0x3ffU) + constants::surrogates::trail_min) : 0;
53+
return is_surrogate_pair() ? static_cast<char16_t>((code_point_ & 0x3ffU) + constants::surrogates::trail_min)
54+
: 0; // NOLINT(cppcoreguidelines-avoid-magic-numbers)
5055
}
5156

5257
[[nodiscard]] constexpr auto u32_value() const noexcept -> std::expected<char32_t, unicode_errc> {

include/skyr/unicode/core.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ namespace skyr::unicode {
2121
template <class uintT>
2222
constexpr auto mask8(uintT value) {
2323
static_assert(std::is_unsigned_v<uintT>, "unsigned integral types only");
24-
return static_cast<uintT>(0xffu & value);
24+
return static_cast<uintT>(0xffu & value); // NOLINT(cppcoreguidelines-avoid-magic-numbers)
2525
}
2626

2727
///
@@ -30,14 +30,14 @@ constexpr auto mask8(uintT value) {
3030
template <class uintT>
3131
constexpr auto mask16(uintT value) {
3232
static_assert(std::is_unsigned_v<uintT>, "unsigned integral types only");
33-
return static_cast<uintT>(0xffffu & value);
33+
return static_cast<uintT>(0xffffu & value); // NOLINT(cppcoreguidelines-avoid-magic-numbers)
3434
}
3535

3636
///
3737
/// \param octet
3838
/// \return
3939
constexpr auto is_trail(uint8_t octet) {
40-
return ((mask8(octet) >> 6u) == 0x2u);
40+
return ((mask8(octet) >> 6u) == 0x2u); // NOLINT(cppcoreguidelines-avoid-magic-numbers)
4141
}
4242

4343
///
@@ -73,13 +73,13 @@ constexpr auto is_valid_code_point(char32_t code_point) {
7373
/// \return 1, 2, 3 or 4
7474
constexpr auto sequence_length(uint8_t lead_value) {
7575
auto lead = mask8(lead_value);
76-
if (lead < 0x80u) {
76+
if (lead < 0x80u) { // NOLINT(cppcoreguidelines-avoid-magic-numbers)
7777
return 1;
78-
} else if ((lead >> 5u) == 0x6u) {
78+
} else if ((lead >> 5u) == 0x6u) { // NOLINT(cppcoreguidelines-avoid-magic-numbers)
7979
return 2;
80-
} else if ((lead >> 4u) == 0xeu) {
80+
} else if ((lead >> 4u) == 0xeu) { // NOLINT(cppcoreguidelines-avoid-magic-numbers)
8181
return 3;
82-
} else if ((lead >> 3u) == 0x1eu) {
82+
} else if ((lead >> 3u) == 0x1eu) { // NOLINT(cppcoreguidelines-avoid-magic-numbers)
8383
return 4;
8484
}
8585
return 0;

include/skyr/unicode/ranges/transforms/u8_transform.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,11 @@ class u8_transform_iterator {
121121
private:
122122
constexpr void increment() {
123123
constexpr auto octet_count = [](char32_t code_point) {
124-
if (code_point < 0x80u) {
124+
if (code_point < 0x80u) { // NOLINT(cppcoreguidelines-avoid-magic-numbers)
125125
return 1;
126-
} else if (code_point < 0x800u) {
126+
} else if (code_point < 0x800u) { // NOLINT(cppcoreguidelines-avoid-magic-numbers)
127127
return 2;
128-
} else if (code_point < 0x10000u) {
128+
} else if (code_point < 0x10000u) { // NOLINT(cppcoreguidelines-avoid-magic-numbers)
129129
return 3;
130130
} else {
131131
return 4;

0 commit comments

Comments
 (0)