aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorchriseth <chris@ethereum.org>2018-12-06 17:04:58 +0800
committerGitHub <noreply@github.com>2018-12-06 17:04:58 +0800
commit5a08ae5e719820ba6d34a66187304747cf5c061f (patch)
tree8c7cc409fd8155580947ecf7a0f5fd385fe0336f
parent15e28fa444843d6b8e5bef81ae9fc73a41ae97f6 (diff)
parent936c67415ae058984c5666c44bf68fd1ebfaf23a (diff)
downloaddexon-solidity-5a08ae5e719820ba6d34a66187304747cf5c061f.tar.gz
dexon-solidity-5a08ae5e719820ba6d34a66187304747cf5c061f.tar.zst
dexon-solidity-5a08ae5e719820ba6d34a66187304747cf5c061f.zip
Merge pull request #5600 from ethereum/simplifyToHex
Simplify toHex()
-rw-r--r--libdevcore/CommonData.cpp22
-rw-r--r--libdevcore/CommonData.h25
-rw-r--r--libdevcore/FixedHash.h2
-rw-r--r--libdevcore/StringUtils.h6
-rw-r--r--libevmasm/AssemblyItem.cpp2
-rw-r--r--libsolidity/ast/ASTJsonConverter.cpp2
-rw-r--r--libsolidity/interface/CompilerStack.cpp2
-rw-r--r--test/libdevcore/CommonData.cpp2
-rw-r--r--test/libevmasm/Assembler.cpp2
-rw-r--r--test/liblll/EndToEndTest.cpp2
-rw-r--r--test/libsolidity/StandardCompiler.cpp6
11 files changed, 38 insertions, 35 deletions
diff --git a/libdevcore/CommonData.cpp b/libdevcore/CommonData.cpp
index 8d2639c9..1b0e9720 100644
--- a/libdevcore/CommonData.cpp
+++ b/libdevcore/CommonData.cpp
@@ -29,6 +29,28 @@
using namespace std;
using namespace dev;
+string dev::toHex(bytes const& _data, HexPrefix _prefix, HexCase _case)
+{
+ std::ostringstream ret;
+ if (_prefix == HexPrefix::Add)
+ ret << "0x";
+
+ int rix = _data.size() - 1;
+ for (uint8_t c: _data)
+ {
+ // switch hex case every four hexchars
+ auto hexcase = std::nouppercase;
+ if (_case == HexCase::Upper)
+ hexcase = std::uppercase;
+ else if (_case == HexCase::Mixed)
+ hexcase = (rix-- & 2) == 0 ? std::nouppercase : std::uppercase;
+
+ ret << std::hex << hexcase << std::setfill('0') << std::setw(2) << size_t(c);
+ }
+
+ return ret.str();
+}
+
int dev::fromHex(char _i, WhenError _throw)
{
if (_i >= '0' && _i <= '9')
diff --git a/libdevcore/CommonData.h b/libdevcore/CommonData.h
index 4118907c..7c59c505 100644
--- a/libdevcore/CommonData.h
+++ b/libdevcore/CommonData.h
@@ -61,26 +61,7 @@ enum class HexCase
/// 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.
/// @example toHex("A\x69") == "4169"
-template <class T>
-std::string toHex(T const& _data, int _w = 2, HexPrefix _prefix = HexPrefix::DontAdd, HexCase _case = HexCase::Lower)
-{
- std::ostringstream ret;
- int rix = _data.size() - 1;
- for (auto datum: _data)
- {
- // switch hex case every four hexchars
- auto hexcase = std::nouppercase;
- if (_case == HexCase::Upper)
- hexcase = std::uppercase;
- else if (_case == HexCase::Mixed)
- hexcase = (rix-- & 2) == 0 ? std::nouppercase : std::uppercase;
-
- ret << std::hex << hexcase << std::setfill('0') << std::setw(_w)
- << +static_cast<typename std::make_unsigned<decltype(datum)>::type>(datum);
- }
-
- return (_prefix == HexPrefix::Add) ? "0x" + ret.str() : ret.str();
-}
+std::string toHex(bytes const& _data, HexPrefix _prefix = HexPrefix::DontAdd, HexCase _case = HexCase::Lower);
/// Converts a (printable) ASCII hex character into the correspnding integer value.
/// @example fromHex('A') == 10 && fromHex('f') == 15 && fromHex('5') == 5
@@ -172,7 +153,7 @@ inline std::string formatNumber(bigint const& _value)
if (_value < 0)
return "-" + formatNumber(-_value);
if (_value > 0x1000000)
- return toHex(toCompactBigEndian(_value), 2, HexPrefix::Add);
+ return toHex(toCompactBigEndian(_value), HexPrefix::Add);
else
return _value.str();
}
@@ -180,7 +161,7 @@ inline std::string formatNumber(bigint const& _value)
inline std::string formatNumber(u256 const& _value)
{
if (_value > 0x1000000)
- return toHex(toCompactBigEndian(_value), 2, HexPrefix::Add);
+ return toHex(toCompactBigEndian(_value), HexPrefix::Add);
else
return _value.str();
}
diff --git a/libdevcore/FixedHash.h b/libdevcore/FixedHash.h
index 24b89840..9245d726 100644
--- a/libdevcore/FixedHash.h
+++ b/libdevcore/FixedHash.h
@@ -95,7 +95,7 @@ public:
uint8_t operator[](unsigned _i) const { return m_data[_i]; }
/// @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.
bytesRef ref() { return bytesRef(m_data.data(), N); }
diff --git a/libdevcore/StringUtils.h b/libdevcore/StringUtils.h
index 43aa4bd1..64044cb2 100644
--- a/libdevcore/StringUtils.h
+++ b/libdevcore/StringUtils.h
@@ -111,7 +111,7 @@ inline std::string formatNumberReadable(
// 0x100 yields 2**8 (N is 1 and redundant)
if (v == 1)
return "2**" + std::to_string(i * 8);
- return toHex(toCompactBigEndian(v), 2, prefix, hexcase) +
+ return toHex(toCompactBigEndian(v), prefix, hexcase) +
" * 2**" +
std::to_string(i * 8);
}
@@ -125,12 +125,12 @@ inline std::string formatNumberReadable(
// 0xFF yields 2**8 - 1 (v is 0 in that case)
if (v == 0)
return "2**" + std::to_string(i * 8) + " - 1";
- return toHex(toCompactBigEndian(T(v + 1)), 2, prefix, hexcase) +
+ return toHex(toCompactBigEndian(T(v + 1)), prefix, hexcase) +
" * 2**" + std::to_string(i * 8) +
" - 1";
}
- std::string str = toHex(toCompactBigEndian(_value), 2, prefix, hexcase);
+ std::string str = toHex(toCompactBigEndian(_value), prefix, hexcase);
if (_useTruncation)
{
// return as interior-truncated hex.
diff --git a/libevmasm/AssemblyItem.cpp b/libevmasm/AssemblyItem.cpp
index 52f246d1..be3b1026 100644
--- a/libevmasm/AssemblyItem.cpp
+++ b/libevmasm/AssemblyItem.cpp
@@ -168,7 +168,7 @@ string AssemblyItem::toAssemblyText() const
break;
}
case Push:
- text = toHex(toCompactBigEndian(data(), 1), 1, HexPrefix::Add);
+ text = toHex(toCompactBigEndian(data(), 1), HexPrefix::Add);
break;
case PushString:
text = string("data_") + toHex(data());
diff --git a/libsolidity/ast/ASTJsonConverter.cpp b/libsolidity/ast/ASTJsonConverter.cpp
index cfb13271..e92134a8 100644
--- a/libsolidity/ast/ASTJsonConverter.cpp
+++ b/libsolidity/ast/ASTJsonConverter.cpp
@@ -724,7 +724,7 @@ bool ASTJsonConverter::visit(Literal const& _node)
std::vector<pair<string, Json::Value>> attributes = {
make_pair(m_legacy ? "token" : "kind", literalTokenKind(_node.token())),
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(
"subdenomination",
subdenomination == Token::Illegal ?
diff --git a/libsolidity/interface/CompilerStack.cpp b/libsolidity/interface/CompilerStack.cpp
index 610caea1..1e6e2330 100644
--- a/libsolidity/interface/CompilerStack.cpp
+++ b/libsolidity/interface/CompilerStack.cpp
@@ -552,7 +552,7 @@ Json::Value CompilerStack::methodIdentifiers(string const& _contractName) const
{
Json::Value methodIdentifiers(Json::objectValue);
for (auto const& it: contractDefinition(_contractName).interfaceFunctions())
- methodIdentifiers[it.second->externalSignature()] = toHex(it.first.ref());
+ methodIdentifiers[it.second->externalSignature()] = it.first.hex();
return methodIdentifiers;
}
diff --git a/test/libdevcore/CommonData.cpp b/test/libdevcore/CommonData.cpp
index 2020ddb0..8da937de 100644
--- a/test/libdevcore/CommonData.cpp
+++ b/test/libdevcore/CommonData.cpp
@@ -37,7 +37,7 @@ BOOST_AUTO_TEST_SUITE(CommonData)
BOOST_AUTO_TEST_CASE(test_to_hex)
{
- BOOST_CHECK_EQUAL(toHex(fromHex("FF"), 2, HexPrefix::DontAdd, HexCase::Lower), "ff");
+ BOOST_CHECK_EQUAL(toHex(fromHex("FF"), HexPrefix::DontAdd, HexCase::Lower), "ff");
}
BOOST_AUTO_TEST_CASE(test_format_number)
diff --git a/test/libevmasm/Assembler.cpp b/test/libevmasm/Assembler.cpp
index bece2be4..6976755f 100644
--- a/test/libevmasm/Assembler.cpp
+++ b/test/libevmasm/Assembler.cpp
@@ -106,7 +106,7 @@ BOOST_AUTO_TEST_CASE(all_assembly_items)
_assembly.assemblyString(),
" /* \"root.asm\":1:3 */\n"
"tag_1:\n"
- " keccak256(0x2, 0x1)\n"
+ " keccak256(0x02, 0x01)\n"
" bytecodeSize\n"
" linkerSymbol(\"bf005014d9d0f534b8fcb268bd84c491a2380f4acd260d1ccfe9cd8201f7e994\")\n"
" jump(tag_1)\n"
diff --git a/test/liblll/EndToEndTest.cpp b/test/liblll/EndToEndTest.cpp
index aad89b91..85ce65a1 100644
--- a/test/liblll/EndToEndTest.cpp
+++ b/test/liblll/EndToEndTest.cpp
@@ -1008,7 +1008,7 @@ BOOST_AUTO_TEST_CASE(sub_assemblies)
compileAndRun(sourceCode);
bytes ret = callFallback();
BOOST_REQUIRE(ret.size() == 32);
- u256 rVal = u256(toHex(ret, 2, HexPrefix::Add));
+ u256 rVal = u256(toHex(ret, HexPrefix::Add));
BOOST_CHECK(rVal != 0);
BOOST_CHECK(rVal < u256("0x10000000000000000000000000000000000000000"));
}
diff --git a/test/libsolidity/StandardCompiler.cpp b/test/libsolidity/StandardCompiler.cpp
index 1570a9d2..118d8210 100644
--- a/test/libsolidity/StandardCompiler.cpp
+++ b/test/libsolidity/StandardCompiler.cpp
@@ -333,11 +333,11 @@ BOOST_AUTO_TEST_CASE(basic_compilation)
" /* \"fileA\":0:14 contract A { } */\n mstore(0x40, 0x80)\n "
"callvalue\n /* \"--CODEGEN--\":8:17 */\n dup1\n "
"/* \"--CODEGEN--\":5:7 */\n iszero\n tag_1\n jumpi\n "
- "/* \"--CODEGEN--\":30:31 */\n 0x0\n /* \"--CODEGEN--\":27:28 */\n "
+ "/* \"--CODEGEN--\":30:31 */\n 0x00\n /* \"--CODEGEN--\":27:28 */\n "
"dup1\n /* \"--CODEGEN--\":20:32 */\n revert\n /* \"--CODEGEN--\":5:7 */\n"
"tag_1:\n /* \"fileA\":0:14 contract A { } */\n pop\n dataSize(sub_0)\n dup1\n "
- "dataOffset(sub_0)\n 0x0\n codecopy\n 0x0\n return\nstop\n\nsub_0: assembly {\n "
- "/* \"fileA\":0:14 contract A { } */\n mstore(0x40, 0x80)\n 0x0\n "
+ "dataOffset(sub_0)\n 0x00\n codecopy\n 0x00\n return\nstop\n\nsub_0: assembly {\n "
+ "/* \"fileA\":0:14 contract A { } */\n mstore(0x40, 0x80)\n 0x00\n "
"dup1\n revert\n\n auxdata: 0xa165627a7a72305820"
) == 0);
BOOST_CHECK(contract["evm"]["gasEstimates"].isObject());