Skip to content

Commit

Permalink
Merge pull request #652 from ethereum/literals_no_0
Browse files Browse the repository at this point in the history
C++: Remove 0_{address,bytes32} literals
  • Loading branch information
chfast committed Jun 1, 2022
2 parents 118166f + eaafbd7 commit 2e25a9a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion examples/example_host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class ExampleHost : public evmc::Host

return (number < current_block_number && number >= current_block_number - 256) ?
0xb10c8a5fb10c8a5fb10c8a5fb10c8a5fb10c8a5fb10c8a5fb10c8a5fb10c8a5f_bytes32 :
0_bytes32;
0x0000000000000000000000000000000000000000000000000000000000000000_bytes32;
}

void emit_log(const evmc::address& addr,
Expand Down
8 changes: 3 additions & 5 deletions include/evmc/evmc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,14 +322,12 @@ constexpr T from_literal() noexcept
{
constexpr auto size = sizeof...(c);
constexpr char literal[] = {c...};
constexpr bool is_simple_zero = size == 1 && literal[0] == '0';

static_assert(is_simple_zero || (literal[0] == '0' && literal[1] == 'x'),
static_assert(size > 2 && literal[0] == '0' && literal[1] == 'x',
"literal must be in hexadecimal notation");
static_assert(is_simple_zero || size == 2 * sizeof(T) + 2,
"literal must match the result type size");
static_assert(size == 2 * sizeof(T) + 2, "literal must match the result type size");

return is_simple_zero ? T{} : from_hex<T>(&literal[2]);
return from_hex<T>(&literal[2]);
}
} // namespace internal

Expand Down
10 changes: 6 additions & 4 deletions test/unittests/cpp_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,9 @@ TEST(cpp, literals)
constexpr auto address1 = 0xa0a1a2a3a4a5a6a7a8a9d0d1d2d3d4d5d6d7d8d9_address;
constexpr auto hash1 =
0x01020304050607080910a1a2a3a4a5a6a7a8a9b0c1c2c3c4c5c6c7c8c9d0d1d2_bytes32;
constexpr auto zero_address = 0_address;
constexpr auto zero_hash = 0_bytes32;
constexpr auto zero_address = 0x0000000000000000000000000000000000000000_address;
constexpr auto zero_hash =
0x0000000000000000000000000000000000000000000000000000000000000000_bytes32;

static_assert(address1.bytes[0] == 0xa0);
static_assert(address1.bytes[9] == 0xa9);
Expand All @@ -361,8 +362,9 @@ TEST(cpp, literals)
static_assert(zero_address == evmc::address{});
static_assert(zero_hash == evmc::bytes32{});

EXPECT_EQ(0_address, evmc::address{});
EXPECT_EQ(0_bytes32, evmc::bytes32{});
EXPECT_EQ(0x0000000000000000000000000000000000000000_address, evmc::address{});
EXPECT_EQ(0x0000000000000000000000000000000000000000000000000000000000000000_bytes32,
evmc::bytes32{});

auto a1 = 0xa0a1a2a3a4a5a6a7a8a9d0d1d2d3d4d5d6d7d8d9_address;
evmc::address e1{{{0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9,
Expand Down

0 comments on commit 2e25a9a

Please sign in to comment.