Skip to content

Commit

Permalink
util: optimize HexStr
Browse files Browse the repository at this point in the history
  • Loading branch information
div72 committed Jun 19, 2021
1 parent 5f52104 commit 9d1e285
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/util/strencodings.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,19 @@ constexpr inline bool IsSpace(char c) noexcept {
template<typename T>
std::string HexStr(const T itbegin, const T itend)
{
std::string rv;
std::string rv(std::distance(itbegin, itend) * 2, '\0');

static const char hexmap[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
rv.reserve(std::distance(itbegin, itend) * 2);
for(T it = itbegin; it < itend; ++it)

auto rvit = rv.begin();
for (T it = itbegin; it < itend; ++it)
{
unsigned char val = (unsigned char)(*it);
rv.push_back(hexmap[val>>4]);
rv.push_back(hexmap[val&15]);
*rvit++ = hexmap[val >> 4];
*rvit++ = hexmap[val & 15];
}
assert(rvit == rv.end());
return rv;
}

Expand Down

0 comments on commit 9d1e285

Please sign in to comment.