Skip to content

Commit e590520

Browse files
committed
Code formatting and assert/message improvements
Reformat code and tighten checks across several modules: - common/utils/utils_hex.h: Update copyright years; reformat namespace, functions and templates; compact FromChar; make FromHex stricter with a compile-time static_assert for invalid hex digits; minor whitespace and comment adjustments. - lib-artnet/src/controller/artnetcontroller.cpp: Reflow braces and spacing, unify formatting, and simplify some expressions; no behavioral logic changes intended. - lib-network/src/core/network_memory.h: Improve assert diagnostics for invalid pointers and double-free detection. - lib-rdm/src/controller/rdm_discovery_statemachine.cpp: Reflow formatting, compact printf calls, and improve assert messages; cleanup namespace closing. Overall these are stylistic and safety-focused changes; functionality should remain unchanged except for stricter compile-time checks in the hex utilities.
1 parent 15aea27 commit e590520

4 files changed

Lines changed: 114 additions & 224 deletions

File tree

common/include/common/utils/utils_hex.h

Lines changed: 22 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @file utils_hex.h
33
*
44
*/
5-
/* Copyright (C) 2025 by Arjan van Vught mailto:info@gd32-dmx.org
5+
/* Copyright (C) 2025-2026 by Arjan van Vught mailto:info@gd32-dmx.org
66
*
77
* Permission is hereby granted, free of charge, to any person obtaining a copy
88
* of this software and associated documentation files (the "Software"), to deal
@@ -30,41 +30,29 @@
3030
#include <cstddef>
3131
#include <cassert>
3232

33-
namespace common::hex
34-
{
35-
constexpr char ToCharLowercase(uint32_t value)
36-
{
33+
namespace common::hex {
34+
constexpr char ToCharLowercase(uint32_t value) {
3735
return "0123456789abcdef"[value & 0xf];
3836
}
3937

40-
constexpr char ToCharUppercase(uint32_t value)
41-
{
38+
constexpr char ToCharUppercase(uint32_t value) {
4239
return "0123456789ABCDEF"[value & 0xf];
4340
}
4441

45-
enum class Case
46-
{
47-
kLower,
48-
kUpper
49-
};
42+
enum class Case { kLower, kUpper };
5043

51-
template <Case letter_case = Case::kLower, size_t N>
52-
char* ToString(char (&string)[N + 1], uint32_t value)
53-
{
44+
template <Case letter_case = Case::kLower, size_t N>
45+
char* ToString(char (&string)[N + 1], uint32_t value) {
5446
static_assert(N % 2 == 0, "Hex string length must be even");
5547
static_assert(N <= 8, "Cannot represent more than 32 bits");
5648

57-
for (size_t i = 0; i < N; ++i)
58-
{
49+
for (size_t i = 0; i < N; ++i) {
5950
size_t shift = (N - 1 - i) * 4;
6051
uint32_t nybble = (value >> shift);
6152

62-
if constexpr (letter_case == Case::kLower)
63-
{
53+
if constexpr (letter_case == Case::kLower) {
6454
string[i] = hex::ToCharLowercase(nybble);
65-
}
66-
else
67-
{
55+
} else {
6856
string[i] = hex::ToCharUppercase(nybble);
6957
}
7058
}
@@ -73,45 +61,36 @@ char* ToString(char (&string)[N + 1], uint32_t value)
7361
return string;
7462
}
7563

76-
template <size_t N>
77-
char* ToStringLower(char (&s)[N + 1], uint32_t v)
78-
{
64+
template <size_t N>
65+
char* ToStringLower(char (&s)[N + 1], uint32_t v) {
7966
return ToString<Case::kLower, N>(s, v);
8067
}
8168

82-
template <size_t N>
83-
char* ToStringUpper(char (&s)[N + 1], uint32_t v)
84-
{
69+
template <size_t N> char*
70+
ToStringUpper(char (&s)[N + 1], uint32_t v) {
8571
return ToString<Case::kUpper, N>(s, v);
8672
}
8773

88-
constexpr uint8_t FromChar(char c)
89-
{
90-
return (c >= '0' && c <= '9') ? static_cast<uint8_t>(c - '0')
91-
: (c >= 'a' && c <= 'f') ? static_cast<uint8_t>(c - 'a' + 10)
92-
: (c >= 'A' && c <= 'F') ? static_cast<uint8_t>(c - 'A' + 10)
93-
: 0xFF; // Invalid
74+
constexpr uint8_t FromChar(char c) {
75+
return (c >= '0' && c <= '9') ? static_cast<uint8_t>(c - '0') : (c >= 'a' && c <= 'f') ? static_cast<uint8_t>(c - 'a' + 10) : (c >= 'A' && c <= 'F') ? static_cast<uint8_t>(c - 'A' + 10) : 0xFF; // Invalid
9476
}
9577

96-
template <size_t N> constexpr uint32_t FromHex(const char (&string)[N])
97-
{
78+
template <size_t N>
79+
constexpr uint32_t FromHex(const char (&string)[N]) {
9880
static_assert(N > 1, "Hex string must not be empty (must include \\0)");
9981
static_assert(N <= 9, "Too many hex digits for uint32_t");
10082
assert(string[N - 1] == '\0');
10183

10284
uint32_t result = 0;
103-
for (size_t i = 0; i < N - 1; ++i)
104-
{
85+
for (size_t i = 0; i < N - 1; ++i) {
10586
const uint8_t kNibble = FromChar(string[i]);
106-
if (kNibble == 0xFF)
107-
{
108-
return 0; // Or: static_assert(false, "Invalid hex digit"); if desired
87+
if constexpr (kNibble == 0xFF) {
88+
static_assert(false, "Invalid hex digit");
10989
}
11090
result = (result << 4) | kNibble;
11191
}
11292
return result;
11393
}
114-
11594
} // namespace common::hex
11695

117-
#endif // COMMON_UTILS_UTILS_HEX_H_
96+
#endif // COMMON_UTILS_UTILS_HEX_H_

0 commit comments

Comments
 (0)