Skip to content

Commit dd5e9d3

Browse files
committed
C++: Refactor {address,bytes32} constructors
1 parent ee3abdb commit dd5e9d3

File tree

1 file changed

+12
-58
lines changed

1 file changed

+12
-58
lines changed

include/evmc/evmc.hpp

Lines changed: 12 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -31,34 +31,17 @@ struct address : evmc_address
3131
/// Default and converting constructor.
3232
///
3333
/// Initializes bytes to zeros if not other @p init value provided.
34-
constexpr address(evmc_address init = {}) noexcept : evmc_address{init} {}
34+
inline constexpr address(evmc_address init = {}) noexcept : evmc_address{init} {}
3535

3636
/// Converting constructor from unsigned integer value.
3737
///
3838
/// This constructor assigns the @p v value to the last 8 bytes [12:19]
3939
/// in big-endian order.
40-
constexpr explicit address(uint64_t v) noexcept
41-
: evmc_address{{0,
42-
0,
43-
0,
44-
0,
45-
0,
46-
0,
47-
0,
48-
0,
49-
0,
50-
0,
51-
0,
52-
0,
53-
static_cast<uint8_t>(v >> 56),
54-
static_cast<uint8_t>(v >> 48),
55-
static_cast<uint8_t>(v >> 40),
56-
static_cast<uint8_t>(v >> 32),
57-
static_cast<uint8_t>(v >> 24),
58-
static_cast<uint8_t>(v >> 16),
59-
static_cast<uint8_t>(v >> 8),
60-
static_cast<uint8_t>(v >> 0)}}
61-
{}
40+
inline constexpr explicit address(uint64_t v) noexcept : evmc_address{}
41+
{
42+
for (size_t i = sizeof(bytes) - sizeof(v); i < sizeof(bytes); ++i)
43+
bytes[i] = static_cast<uint8_t>(v >> (8 * (sizeof(bytes) - 1 - i)));
44+
}
6245

6346
/// Explicit operator converting to bool.
6447
inline constexpr explicit operator bool() const noexcept;
@@ -75,46 +58,17 @@ struct bytes32 : evmc_bytes32
7558
/// Default and converting constructor.
7659
///
7760
/// Initializes bytes to zeros if not other @p init value provided.
78-
constexpr bytes32(evmc_bytes32 init = {}) noexcept : evmc_bytes32{init} {}
61+
inline constexpr bytes32(evmc_bytes32 init = {}) noexcept : evmc_bytes32{init} {}
7962

8063
/// Converting constructor from unsigned integer value.
8164
///
8265
/// This constructor assigns the @p v value to the last 8 bytes [24:31]
8366
/// in big-endian order.
84-
constexpr explicit bytes32(uint64_t v) noexcept
85-
: evmc_bytes32{{0,
86-
0,
87-
0,
88-
0,
89-
0,
90-
0,
91-
0,
92-
0,
93-
0,
94-
0,
95-
0,
96-
0,
97-
0,
98-
0,
99-
0,
100-
0,
101-
0,
102-
0,
103-
0,
104-
0,
105-
0,
106-
0,
107-
0,
108-
0,
109-
static_cast<uint8_t>(v >> 56),
110-
static_cast<uint8_t>(v >> 48),
111-
static_cast<uint8_t>(v >> 40),
112-
static_cast<uint8_t>(v >> 32),
113-
static_cast<uint8_t>(v >> 24),
114-
static_cast<uint8_t>(v >> 16),
115-
static_cast<uint8_t>(v >> 8),
116-
static_cast<uint8_t>(v >> 0)}}
117-
{}
67+
inline constexpr explicit bytes32(uint64_t v) noexcept : evmc_bytes32{}
68+
{
69+
for (size_t i = sizeof(bytes) - sizeof(v); i < sizeof(bytes); ++i)
70+
bytes[i] = static_cast<uint8_t>(v >> (8 * (sizeof(bytes) - 1 - i)));
71+
}
11872

11973
/// Explicit operator converting to bool.
12074
inline constexpr explicit operator bool() const noexcept;

0 commit comments

Comments
 (0)