[isoltest] Cleans up BytesUtils.

This commit is contained in:
Erik Kundt 2019-07-21 15:58:12 +02:00
parent a7a8ba73f9
commit d9b98bf7af
3 changed files with 87 additions and 84 deletions

View File

@ -17,6 +17,8 @@
#include <test/libsolidity/util/BytesUtils.h>
#include <test/libsolidity/util/SoltestErrors.h>
#include <liblangutil/Common.h>
#include <boost/algorithm/string.hpp>
@ -34,6 +36,40 @@ using namespace dev::solidity::test;
using namespace std;
using namespace soltest;
bytes BytesUtils::alignLeft(bytes _bytes)
{
soltestAssert(_bytes.size() <= 32, "");
size_t size = _bytes.size();
return std::move(_bytes) + bytes(32 - size, 0);
}
bytes BytesUtils::alignRight(bytes _bytes)
{
soltestAssert(_bytes.size() <= 32, "");
return bytes(32 - _bytes.size(), 0) + std::move(_bytes);
}
bytes BytesUtils::applyAlign(
Parameter::Alignment _alignment,
ABIType& _abiType,
bytes _bytes
)
{
if (_alignment != Parameter::Alignment::None)
_abiType.alignDeclared = true;
switch (_alignment)
{
case Parameter::Alignment::Left:
_abiType.align = ABIType::AlignLeft;
return alignLeft(std::move(_bytes));
case Parameter::Alignment::Right:
default:
_abiType.align = ABIType::AlignRight;
return alignRight(std::move(_bytes));
}
}
bytes BytesUtils::convertBoolean(string const& _literal)
{
if (_literal == "true")
@ -83,10 +119,21 @@ bytes BytesUtils::convertString(string const& _literal)
}
}
string BytesUtils::formatUnsigned(bytes const& _bytes) const
string BytesUtils::formatUnsigned(bytes const& _bytes)
{
stringstream os;
soltestAssert(!_bytes.empty() && _bytes.size() <= 32, "");
return fromBigEndian<u256>(_bytes).str();
}
string BytesUtils::formatSigned(bytes const& _bytes)
{
stringstream os;
soltestAssert(!_bytes.empty() && _bytes.size() <= 32, "");
if (*_bytes.begin() & 0x80)
os << u2s(fromBigEndian<u256>(_bytes));
else
@ -95,19 +142,7 @@ string BytesUtils::formatUnsigned(bytes const& _bytes) const
return os.str();
}
string BytesUtils::formatSigned(bytes const& _bytes) const
{
stringstream os;
if (*_bytes.begin() & 0x80)
os << u2s(fromBigEndian<u256>(_bytes));
else
os << fromBigEndian<u256>(_bytes);
return os.str();
}
string BytesUtils::formatBoolean(bytes const& _bytes) const
string BytesUtils::formatBoolean(bytes const& _bytes)
{
stringstream os;
u256 result = fromBigEndian<u256>(_bytes);
@ -122,7 +157,7 @@ string BytesUtils::formatBoolean(bytes const& _bytes) const
return os.str();
}
string BytesUtils::formatHex(bytes const& _bytes) const
string BytesUtils::formatHex(bytes const& _bytes)
{
stringstream os;
@ -133,7 +168,7 @@ string BytesUtils::formatHex(bytes const& _bytes) const
return os.str();
}
string BytesUtils::formatHexString(bytes const& _bytes) const
string BytesUtils::formatHexString(bytes const& _bytes)
{
stringstream os;
@ -170,36 +205,3 @@ string BytesUtils::formatString(bytes const& _bytes, size_t _cutOff) const
return os.str();
}
bytes BytesUtils::alignLeft(bytes _bytes) const
{
return std::move(_bytes) + bytes(32 - _bytes.size(), 0);
}
bytes BytesUtils::alignRight(bytes _bytes) const
{
return bytes(32 - _bytes.size(), 0) + std::move(_bytes);
}
bytes BytesUtils::applyAlign(
Parameter::Alignment _alignment,
ABIType& _abiType,
bytes _bytes
) const
{
if (_alignment != Parameter::Alignment::None)
_abiType.alignDeclared = true;
switch (_alignment)
{
case Parameter::Alignment::Left:
_abiType.align = ABIType::AlignLeft;
return alignLeft(std::move(_bytes));
case Parameter::Alignment::Right:
_abiType.align = ABIType::AlignRight;
return alignRight(std::move(_bytes));
default:
_abiType.align = ABIType::AlignRight;
return alignRight(std::move(_bytes));
}
}

View File

@ -32,46 +32,64 @@ namespace test
class BytesUtils
{
public:
/// Left-aligns and pads given _bytes and returns a new
/// bytes array.
static bytes alignLeft(bytes _bytes);
/// Right-aligns and pads given _bytes and returns a new
/// bytes array.
static bytes alignRight(bytes _bytes);
/// Applies given _alignment to _bytes and returns a new
/// bytes array.
/// TODO: Remove abiType reference from parameter list
/// and return the new alignment instead.
static bytes applyAlign(
Parameter::Alignment _alignment,
ABIType& _abiType,
bytes _bytes
);
/// Tries to convert \param _literal to an unpadded `bytes`
/// representation of the boolean number literal. Throws if conversion fails.
bytes convertBoolean(std::string const& _literal);
static bytes convertBoolean(std::string const& _literal);
/// Tries to convert \param _literal to an unpadded `bytes`
/// representation of the decimal number literal. Throws if conversion fails.
bytes convertNumber(std::string const& _literal);
static bytes convertNumber(std::string const& _literal);
/// Tries to convert \param _literal to an unpadded `bytes`
/// representation of the hex literal. Throws if conversion fails.
bytes convertHexNumber(std::string const& _literal);
static bytes convertHexNumber(std::string const& _literal);
/// Tries to convert \param _literal to an unpadded `bytes`
/// representation of the string literal. Throws if conversion fails.
bytes convertString(std::string const& _literal);
static bytes convertString(std::string const& _literal);
/// Converts \param _bytes to a soltest-compliant and human-readable
/// string representation of a byte array which is assumed to hold
/// an unsigned value.
std::string formatUnsigned(bytes const& _bytes) const;
static std::string formatUnsigned(bytes const& _bytes);
/// Converts \param _bytes to a soltest-compliant and human-readable
/// string representation of a byte array which is assumed to hold
/// a signed value.
std::string formatSigned(bytes const& _bytes) const;
static std::string formatSigned(bytes const& _bytes);
/// Converts \param _bytes to a soltest-compliant and human-readable
/// string representation of a byte array which is assumed to hold
/// a boolean value.
std::string formatBoolean(bytes const& _bytes) const;
static std::string formatBoolean(bytes const& _bytes);
/// Converts \param _bytes to a soltest-compliant and human-readable
/// string representation of a byte array which is assumed to hold
/// a hex value.
std::string formatHex(bytes const& _bytes) const;
static std::string formatHex(bytes const& _bytes);
/// Converts \param _bytes to a soltest-compliant and human-readable
/// string representation of a byte array which is assumed to hold
/// a hexString value.
std::string formatHexString(bytes const& _bytes) const;
static std::string formatHexString(bytes const& _bytes);
/// Converts \param _bytes to a soltest-compliant and human-readable
/// string representation of a byte array which is assumed to hold
@ -82,22 +100,6 @@ public:
{
return formatString(_bytes, _bytes.size());
}
/// Left-aligns and pads given _bytes and returns a new
/// bytes array.
bytes alignLeft(bytes _bytes) const;
/// Right-aligns and pads given _bytes and returns a new
/// bytes array.
bytes alignRight(bytes _bytes) const;
/// Applies given _alignment to _bytes and returns a new
/// bytes array.
bytes applyAlign(
Parameter::Alignment _alignment,
ABIType& _abiType,
bytes _bytes
) const;
};
}

View File

@ -236,7 +236,6 @@ Parameter TestFileParser::parseParameter()
}
bool isSigned = false;
if (accept(Token::Left, true))
{
parameter.rawString += formatToken(Token::Left);
@ -267,10 +266,10 @@ Parameter TestFileParser::parseParameter()
parameter.abiType = ABIType{ABIType::Boolean, ABIType::AlignRight, 32};
string parsed = parseBoolean();
parameter.rawString += parsed;
parameter.rawBytes = BytesUtils().applyAlign(
parameter.rawBytes = BytesUtils::applyAlign(
parameter.alignment,
parameter.abiType,
BytesUtils().convertBoolean(parsed)
BytesUtils::convertBoolean(parsed)
);
}
else if (accept(Token::HexNumber))
@ -281,10 +280,10 @@ Parameter TestFileParser::parseParameter()
parameter.abiType = ABIType{ABIType::Hex, ABIType::AlignRight, 32};
string parsed = parseHexNumber();
parameter.rawString += parsed;
parameter.rawBytes = BytesUtils().applyAlign(
parameter.rawBytes = BytesUtils::applyAlign(
parameter.alignment,
parameter.abiType,
BytesUtils().convertHexNumber(parsed)
BytesUtils::convertHexNumber(parsed)
);
}
else if (accept(Token::Hex, true))
@ -296,7 +295,7 @@ Parameter TestFileParser::parseParameter()
string parsed = parseString();
parameter.rawString += "hex\"" + parsed + "\"";
parameter.rawBytes = BytesUtils().convertHexNumber(parsed);
parameter.rawBytes = BytesUtils::convertHexNumber(parsed);
parameter.abiType = ABIType{
ABIType::HexString, ABIType::AlignNone, parameter.rawBytes.size()
};
@ -311,10 +310,10 @@ Parameter TestFileParser::parseParameter()
string parsed = parseString();
parameter.abiType = {ABIType::String, ABIType::AlignLeft, parsed.size()};
parameter.rawString += "\"" + parsed + "\"";
parameter.rawBytes = BytesUtils().applyAlign(
parameter.rawBytes = BytesUtils::applyAlign(
Parameter::Alignment::Left,
parameter.abiType,
BytesUtils().convertString(parsed)
BytesUtils::convertString(parsed)
);
}
else if (accept(Token::Number))
@ -327,10 +326,10 @@ Parameter TestFileParser::parseParameter()
if (isSigned)
parsed = "-" + parsed;
parameter.rawBytes = BytesUtils().applyAlign(
parameter.rawBytes = BytesUtils::applyAlign(
parameter.alignment,
parameter.abiType,
BytesUtils().convertNumber(parsed)
BytesUtils::convertNumber(parsed)
);
}
else if (accept(Token::Failure, true))