Restrict toHex to bytes.

This commit is contained in:
chriseth 2018-12-05 20:34:27 +01:00
parent 15e28fa444
commit 3a378eae1a
4 changed files with 6 additions and 7 deletions

View File

@ -61,12 +61,11 @@ enum class HexCase
/// Convert a series of bytes to the corresponding string of hex duplets. /// Convert a series of bytes to the corresponding string of hex duplets.
/// @param _w specifies the width of the first of the elements. Defaults to two - enough to represent a byte. /// @param _w specifies the width of the first of the elements. Defaults to two - enough to represent a byte.
/// @example toHex("A\x69") == "4169" /// @example toHex("A\x69") == "4169"
template <class T> inline std::string toHex(bytes const& _data, int _w = 2, HexPrefix _prefix = HexPrefix::DontAdd, HexCase _case = HexCase::Lower)
std::string toHex(T const& _data, int _w = 2, HexPrefix _prefix = HexPrefix::DontAdd, HexCase _case = HexCase::Lower)
{ {
std::ostringstream ret; std::ostringstream ret;
int rix = _data.size() - 1; int rix = _data.size() - 1;
for (auto datum: _data) for (uint8_t c: _data)
{ {
// switch hex case every four hexchars // switch hex case every four hexchars
auto hexcase = std::nouppercase; auto hexcase = std::nouppercase;
@ -76,7 +75,7 @@ std::string toHex(T const& _data, int _w = 2, HexPrefix _prefix = HexPrefix::Don
hexcase = (rix-- & 2) == 0 ? std::nouppercase : std::uppercase; hexcase = (rix-- & 2) == 0 ? std::nouppercase : std::uppercase;
ret << std::hex << hexcase << std::setfill('0') << std::setw(_w) ret << std::hex << hexcase << std::setfill('0') << std::setw(_w)
<< +static_cast<typename std::make_unsigned<decltype(datum)>::type>(datum); << size_t(c);
} }
return (_prefix == HexPrefix::Add) ? "0x" + ret.str() : ret.str(); return (_prefix == HexPrefix::Add) ? "0x" + ret.str() : ret.str();

View File

@ -95,7 +95,7 @@ public:
uint8_t operator[](unsigned _i) const { return m_data[_i]; } uint8_t operator[](unsigned _i) const { return m_data[_i]; }
/// @returns the hash as a user-readable hex string. /// @returns the hash as a user-readable hex string.
std::string hex() const { return toHex(ref()); } std::string hex() const { return toHex(asBytes()); }
/// @returns a mutable byte vector_ref to the object's data. /// @returns a mutable byte vector_ref to the object's data.
bytesRef ref() { return bytesRef(m_data.data(), N); } bytesRef ref() { return bytesRef(m_data.data(), N); }

View File

@ -724,7 +724,7 @@ bool ASTJsonConverter::visit(Literal const& _node)
std::vector<pair<string, Json::Value>> attributes = { std::vector<pair<string, Json::Value>> attributes = {
make_pair(m_legacy ? "token" : "kind", literalTokenKind(_node.token())), make_pair(m_legacy ? "token" : "kind", literalTokenKind(_node.token())),
make_pair("value", value), make_pair("value", value),
make_pair(m_legacy ? "hexvalue" : "hexValue", toHex(_node.value())), make_pair(m_legacy ? "hexvalue" : "hexValue", toHex(asBytes(_node.value()))),
make_pair( make_pair(
"subdenomination", "subdenomination",
subdenomination == Token::Illegal ? subdenomination == Token::Illegal ?

View File

@ -552,7 +552,7 @@ Json::Value CompilerStack::methodIdentifiers(string const& _contractName) const
{ {
Json::Value methodIdentifiers(Json::objectValue); Json::Value methodIdentifiers(Json::objectValue);
for (auto const& it: contractDefinition(_contractName).interfaceFunctions()) for (auto const& it: contractDefinition(_contractName).interfaceFunctions())
methodIdentifiers[it.second->externalSignature()] = toHex(it.first.ref()); methodIdentifiers[it.second->externalSignature()] = it.first.hex();
return methodIdentifiers; return methodIdentifiers;
} }