diff --git a/test/libsolidity/semanticTests/isoltestFormatting.sol b/test/libsolidity/semanticTests/isoltestFormatting.sol new file mode 100644 index 000000000..9380dc015 --- /dev/null +++ b/test/libsolidity/semanticTests/isoltestFormatting.sol @@ -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 diff --git a/test/libsolidity/util/BytesUtils.cpp b/test/libsolidity/util/BytesUtils.cpp index 6377fbe2e..fd5cb7f49 100644 --- a/test/libsolidity/util/BytesUtils.cpp +++ b/test/libsolidity/util/BytesUtils.cpp @@ -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(); + 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 frequencies; + for (char c: str) + frequencies[c]++; + for (auto p: frequencies) + { + double freq = static_cast(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);