|
5 | 5 |
|
6 | 6 | #include <evmc/evmc.h>
|
7 | 7 | #include <evmc/helpers.h>
|
| 8 | +#include <evmc/hex.hpp> |
8 | 9 |
|
9 | 10 | #include <functional>
|
10 | 11 | #include <initializer_list>
|
@@ -280,69 +281,27 @@ inline constexpr bytes32::operator bool() const noexcept
|
280 | 281 |
|
281 | 282 | namespace literals
|
282 | 283 | {
|
283 |
| -namespace internal |
284 |
| -{ |
285 |
| -constexpr int from_hex(char c) noexcept |
286 |
| -{ |
287 |
| - return (c >= 'a' && c <= 'f') ? c - ('a' - 10) : |
288 |
| - (c >= 'A' && c <= 'F') ? c - ('A' - 10) : |
289 |
| - c - '0'; |
290 |
| -} |
291 |
| - |
292 |
| -constexpr uint8_t byte(const char* s, size_t i) noexcept |
293 |
| -{ |
294 |
| - return static_cast<uint8_t>((from_hex(s[2 * i]) << 4) | from_hex(s[2 * i + 1])); |
295 |
| -} |
296 |
| - |
| 284 | +/// Converts a raw literal into value of type T. |
| 285 | +/// |
| 286 | +/// This function is expected to be used on literals in constexpr context only. |
| 287 | +/// In case the input is invalid the std::terminate() is called. |
| 288 | +/// TODO(c++20): Use consteval. |
297 | 289 | template <typename T>
|
298 |
| -T from_hex(const char*) noexcept; |
299 |
| - |
300 |
| -template <> |
301 |
| -constexpr bytes32 from_hex<bytes32>(const char* s) noexcept |
| 290 | +constexpr T parse(std::string_view s) noexcept |
302 | 291 | {
|
303 |
| - return { |
304 |
| - {{byte(s, 0), byte(s, 1), byte(s, 2), byte(s, 3), byte(s, 4), byte(s, 5), byte(s, 6), |
305 |
| - byte(s, 7), byte(s, 8), byte(s, 9), byte(s, 10), byte(s, 11), byte(s, 12), byte(s, 13), |
306 |
| - byte(s, 14), byte(s, 15), byte(s, 16), byte(s, 17), byte(s, 18), byte(s, 19), byte(s, 20), |
307 |
| - byte(s, 21), byte(s, 22), byte(s, 23), byte(s, 24), byte(s, 25), byte(s, 26), byte(s, 27), |
308 |
| - byte(s, 28), byte(s, 29), byte(s, 30), byte(s, 31)}}}; |
| 292 | + return from_hex<T>(s).value(); |
309 | 293 | }
|
310 | 294 |
|
311 |
| -template <> |
312 |
| -constexpr address from_hex<address>(const char* s) noexcept |
313 |
| -{ |
314 |
| - return { |
315 |
| - {{byte(s, 0), byte(s, 1), byte(s, 2), byte(s, 3), byte(s, 4), byte(s, 5), byte(s, 6), |
316 |
| - byte(s, 7), byte(s, 8), byte(s, 9), byte(s, 10), byte(s, 11), byte(s, 12), byte(s, 13), |
317 |
| - byte(s, 14), byte(s, 15), byte(s, 16), byte(s, 17), byte(s, 18), byte(s, 19)}}}; |
318 |
| -} |
319 |
| - |
320 |
| -template <typename T, char... c> |
321 |
| -constexpr T from_literal() noexcept |
322 |
| -{ |
323 |
| - constexpr auto size = sizeof...(c); |
324 |
| - constexpr char literal[] = {c...}; |
325 |
| - |
326 |
| - static_assert(size > 2 && literal[0] == '0' && literal[1] == 'x', |
327 |
| - "literal must be in hexadecimal notation"); |
328 |
| - static_assert(size == 2 * sizeof(T) + 2, "literal must match the result type size"); |
329 |
| - |
330 |
| - return from_hex<T>(&literal[2]); |
331 |
| -} |
332 |
| -} // namespace internal |
333 |
| - |
334 | 295 | /// Literal for evmc::address.
|
335 |
| -template <char... c> |
336 |
| -constexpr address operator""_address() noexcept |
| 296 | +constexpr address operator""_address(const char* s) noexcept |
337 | 297 | {
|
338 |
| - return internal::from_literal<address, c...>(); |
| 298 | + return parse<address>(s); |
339 | 299 | }
|
340 | 300 |
|
341 | 301 | /// Literal for evmc::bytes32.
|
342 |
| -template <char... c> |
343 |
| -constexpr bytes32 operator""_bytes32() noexcept |
| 302 | +constexpr bytes32 operator""_bytes32(const char* s) noexcept |
344 | 303 | {
|
345 |
| - return internal::from_literal<bytes32, c...>(); |
| 304 | + return parse<bytes32>(s); |
346 | 305 | }
|
347 | 306 | } // namespace literals
|
348 | 307 |
|
|
0 commit comments