Skip to content

Commit

Permalink
Use bytes instead of vector<uint8_t> in helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast authored and axic committed Apr 29, 2019
1 parent bebd1fb commit 2cfd419
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
14 changes: 7 additions & 7 deletions src/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ string toHex(evmc_uint256be const& value) {
return "0x" + os.str();
}

string bytesAsHexStr(const uint8_t *bytes, const size_t length) {
string bytesAsHexStr(bytes_view bytes) {
stringstream ret;
ret << hex << "0x";
for (size_t i = 0; i < length; ++i) {
ret << setw(2) << setfill('0') << static_cast<int>(bytes[i]);
for (auto const b : bytes) {
ret << setw(2) << setfill('0') << static_cast<int>(b);
}
return ret.str();
}
Expand All @@ -72,15 +72,15 @@ bool nibble2value(unsigned input, unsigned& output) {
// Returns an empty vector if input is invalid (odd number of characters or invalid nibbles).
// Assumes input is whitespace free, therefore if input is non-zero long an empty output
// signals an error.
vector<uint8_t> parseHexString(const string& input) {
bytes parseHexString(const string& input) {
size_t len = input.length();
if (len % 2 != 0)
return vector<uint8_t>{};
vector<uint8_t> ret;
return {};
bytes ret;
for (size_t i = 0; i <= len - 2; i += 2) {
unsigned lo, hi;
if (!nibble2value(unsigned(input[i]), hi) || !nibble2value(unsigned(input[i + 1]), lo))
return vector<uint8_t>{};
return {};
ret.push_back(static_cast<uint8_t>((hi << 4) | lo));
}
return ret;
Expand Down
4 changes: 2 additions & 2 deletions src/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ bytes loadFileContents(std::string const& path);
std::string toHex(evmc_uint256be const& value);

// Returns a formatted string (with prefix "0x") representing the bytes of an array.
std::string bytesAsHexStr(const uint8_t *bytes, const size_t length);
std::string bytesAsHexStr(bytes_view bytes);

std::vector<uint8_t> parseHexString(std::string const& input);
bytes parseHexString(std::string const& input);

bool hasWasmPreamble(bytes_view _input);

Expand Down
2 changes: 1 addition & 1 deletion src/hera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ bool hera_parse_sys_option(hera_instance *hera, string const& _name, string cons

if (name.find("0x") == 0) {
// hex address
vector<uint8_t> ret = parseHexString(name.substr(2, string::npos));
bytes ret = parseHexString(name.substr(2, string::npos));
if (ret.empty()) {
HERA_DEBUG << "Failed to parse hex address: " << name << "\n";
return false;
Expand Down

0 comments on commit 2cfd419

Please sign in to comment.