EVMC
hex.hpp
1 // EVMC: Ethereum Client-VM Connector API.
2 // Copyright 2021 The EVMC Authors.
3 // Licensed under the Apache License, Version 2.0.
4 #pragma once
5 
6 #include <cstdint>
7 #include <iterator>
8 #include <string>
9 #include <string_view>
10 #include <system_error>
11 
12 namespace evmc
13 {
15 using bytes = std::basic_string<uint8_t>;
16 
18 using bytes_view = std::basic_string_view<uint8_t>;
19 
21 enum class hex_errc
22 {
25 
28 };
29 
31 const std::error_category& hex_category() noexcept;
32 
34 inline std::error_code make_error_code(hex_errc errc) noexcept
35 {
36  return {static_cast<int>(errc), hex_category()};
37 }
38 
40 struct hex_error : std::system_error
41 {
42  using system_error::system_error;
43 };
44 
46 inline std::string hex(uint8_t b) noexcept
47 {
48  static constexpr auto hex_chars = "0123456789abcdef";
49  return {hex_chars[b >> 4], hex_chars[b & 0xf]};
50 }
51 
53 std::error_code validate_hex(std::string_view hex) noexcept;
54 
58 bytes from_hex(std::string_view hex);
59 
61 std::string hex(bytes_view bs);
62 } // namespace evmc
63 
64 namespace std
65 {
68 template <>
69 struct is_error_code_enum<evmc::hex_errc> : true_type
70 {};
71 } // namespace std
EVMC C++ API - wrappers and bindings for C++.
Definition: evmc.hpp:21
std::basic_string< uint8_t > bytes
String of uint8_t chars.
Definition: hex.hpp:15
std::string hex(uint8_t b) noexcept
Encode a byte to a hex string.
Definition: hex.hpp:46
hex_errc
Hex decoding error codes.
Definition: hex.hpp:22
@ invalid_hex_digit
Invalid hex digit encountered during decoding.
@ incomplete_hex_byte_pair
Input contains incomplete hex byte (length is odd).
bytes from_hex(std::string_view hex)
Decodes hex encoded string to bytes.
std::basic_string_view< uint8_t > bytes_view
String view of uint8_t chars.
Definition: hex.hpp:18
std::error_code validate_hex(std::string_view hex) noexcept
Validates hex encoded string.
std::error_code make_error_code(hex_errc errc) noexcept
Creates error_code object out of a hex error code value.
Definition: hex.hpp:34
const std::error_category & hex_category() noexcept
Obtains a reference to the static error category object for hex errors.
Hex decoding exception.
Definition: hex.hpp:41