Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++: Support zero literal (0x0_address) #682

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion include/evmc/evmc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,8 @@ namespace literals
template <typename T>
constexpr T parse(std::string_view s) noexcept
{
return from_hex<T>(s).value();
auto const v = from_hex<T>(s);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The failure case is also used for parsing errors, including odd nibbles.

Need to rethink.

return v.has_value() ? v.value() : T{};
}

/// Literal for evmc::address.
Expand Down
6 changes: 6 additions & 0 deletions test/unittests/cpp_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ TEST(cpp, std_hash)
std::fill_n(eb.bytes, sizeof(eb), uint8_t{0xee});
EXPECT_EQ(std::hash<evmc::bytes32>{}(eb), static_cast<size_t>(0xbb14e5c56b477375));

const auto zero_address = 0x0_address;
EXPECT_EQ(zero_address, evmc::address{});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These can be static_asserts I think.


const auto zero_bytes32 = 0x0_bytes32;
EXPECT_EQ(zero_bytes32, evmc::bytes32{});

const auto rand_address_1 = 0xaa00bb00cc00dd00ee00ff001100220033004400_address;
EXPECT_EQ(std::hash<evmc::address>{}(rand_address_1), static_cast<size_t>(0x30022347e325524e));

Expand Down