mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
[isoltest] Improve parameter formatting.
This commit is contained in:
parent
142a6b0d4f
commit
410986e00f
13
test/libsolidity/semanticTests/isoltestFormatting.sol
Normal file
13
test/libsolidity/semanticTests/isoltestFormatting.sol
Normal file
@ -0,0 +1,13 @@
|
||||
contract C {
|
||||
function f() public returns (uint[5] memory) {
|
||||
uint[5] memory a = [4, 11, 0x111, uint(3355443), 2222222222222222222];
|
||||
return a;
|
||||
}
|
||||
function g() public returns (uint[5] memory) {
|
||||
uint[5] memory a = [16, 256, 257, uint(0x333333), 0x1ed6eb565788e38e];
|
||||
return a;
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// f() -> 4, 11, 0x0111, 0x333333, 2222222222222222222
|
||||
// g() -> 0x10, 0x0100, 0x0101, 0x333333, 2222222222222222222
|
@ -252,7 +252,35 @@ string BytesUtils::formatBytes(
|
||||
if (*_bytes.begin() & 0x80)
|
||||
os << formatSigned(_bytes);
|
||||
else
|
||||
os << formatUnsigned(_bytes);
|
||||
{
|
||||
std::string decimal(formatUnsigned(_bytes));
|
||||
std::string hexadecimal(formatHex(_bytes));
|
||||
unsigned int value = u256(_bytes).convert_to<unsigned int>();
|
||||
if (value < 0x10)
|
||||
os << decimal;
|
||||
else if (value >= 0x10 && value <= 0xff) {
|
||||
os << hexadecimal;
|
||||
}
|
||||
else
|
||||
{
|
||||
auto entropy = [](std::string const& str) -> double {
|
||||
double result = 0;
|
||||
map<char, int> frequencies;
|
||||
for (char c: str)
|
||||
frequencies[c]++;
|
||||
for (auto p: frequencies)
|
||||
{
|
||||
double freq = static_cast<double>(p.second) / str.length();
|
||||
result -= freq * (log(freq) / log(2));
|
||||
}
|
||||
return result;
|
||||
};
|
||||
if (entropy(decimal) < entropy(hexadecimal.substr(2, hexadecimal.length())))
|
||||
os << decimal;
|
||||
else
|
||||
os << hexadecimal;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case ABIType::SignedDec:
|
||||
os << formatSigned(_bytes);
|
||||
|
Loading…
Reference in New Issue
Block a user