-
Notifications
You must be signed in to change notification settings - Fork 314
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++: Rework hex literals parsing #644
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
|
||
#include <evmc/evmc.h> | ||
#include <evmc/helpers.h> | ||
#include <evmc/hex.hpp> | ||
|
||
#include <functional> | ||
#include <initializer_list> | ||
|
@@ -280,69 +281,27 @@ inline constexpr bytes32::operator bool() const noexcept | |
|
||
namespace literals | ||
{ | ||
namespace internal | ||
{ | ||
constexpr int from_hex(char c) noexcept | ||
{ | ||
return (c >= 'a' && c <= 'f') ? c - ('a' - 10) : | ||
(c >= 'A' && c <= 'F') ? c - ('A' - 10) : | ||
c - '0'; | ||
} | ||
|
||
constexpr uint8_t byte(const char* s, size_t i) noexcept | ||
{ | ||
return static_cast<uint8_t>((from_hex(s[2 * i]) << 4) | from_hex(s[2 * i + 1])); | ||
} | ||
|
||
/// Converts a raw literal into value of type T. | ||
/// | ||
/// This function is expected to be used on literals in constexpr context only. | ||
/// In case the input is invalid the std::terminate() is called. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this comment still valid? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately, clang-tidy also notice this and report warning |
||
/// TODO(c++20): Use consteval. | ||
template <typename T> | ||
T from_hex(const char*) noexcept; | ||
|
||
template <> | ||
constexpr bytes32 from_hex<bytes32>(const char* s) noexcept | ||
constexpr T parse(std::string_view s) noexcept | ||
{ | ||
return { | ||
{{byte(s, 0), byte(s, 1), byte(s, 2), byte(s, 3), byte(s, 4), byte(s, 5), byte(s, 6), | ||
byte(s, 7), byte(s, 8), byte(s, 9), byte(s, 10), byte(s, 11), byte(s, 12), byte(s, 13), | ||
byte(s, 14), byte(s, 15), byte(s, 16), byte(s, 17), byte(s, 18), byte(s, 19), byte(s, 20), | ||
byte(s, 21), byte(s, 22), byte(s, 23), byte(s, 24), byte(s, 25), byte(s, 26), byte(s, 27), | ||
byte(s, 28), byte(s, 29), byte(s, 30), byte(s, 31)}}}; | ||
return from_hex<T>(s).value(); | ||
} | ||
|
||
template <> | ||
constexpr address from_hex<address>(const char* s) noexcept | ||
{ | ||
return { | ||
{{byte(s, 0), byte(s, 1), byte(s, 2), byte(s, 3), byte(s, 4), byte(s, 5), byte(s, 6), | ||
byte(s, 7), byte(s, 8), byte(s, 9), byte(s, 10), byte(s, 11), byte(s, 12), byte(s, 13), | ||
byte(s, 14), byte(s, 15), byte(s, 16), byte(s, 17), byte(s, 18), byte(s, 19)}}}; | ||
} | ||
|
||
template <typename T, char... c> | ||
constexpr T from_literal() noexcept | ||
{ | ||
constexpr auto size = sizeof...(c); | ||
constexpr char literal[] = {c...}; | ||
|
||
static_assert(size > 2 && literal[0] == '0' && literal[1] == 'x', | ||
"literal must be in hexadecimal notation"); | ||
static_assert(size == 2 * sizeof(T) + 2, "literal must match the result type size"); | ||
|
||
return from_hex<T>(&literal[2]); | ||
} | ||
} // namespace internal | ||
|
||
/// Literal for evmc::address. | ||
template <char... c> | ||
constexpr address operator""_address() noexcept | ||
constexpr address operator""_address(const char* s) noexcept | ||
{ | ||
return internal::from_literal<address, c...>(); | ||
return parse<address>(s); | ||
} | ||
|
||
/// Literal for evmc::bytes32. | ||
template <char... c> | ||
constexpr bytes32 operator""_bytes32() noexcept | ||
constexpr bytes32 operator""_bytes32(const char* s) noexcept | ||
{ | ||
return internal::from_literal<bytes32, c...>(); | ||
return parse<bytes32>(s); | ||
} | ||
} // namespace literals | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why this? it's still
noexcept
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I see https://github.com/ethereum/evmc/pull/644/files#r887984740