mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
[isoltest] Cleans up BytesUtils.
This commit is contained in:
parent
a7a8ba73f9
commit
d9b98bf7af
@ -17,6 +17,8 @@
|
|||||||
|
|
||||||
#include <test/libsolidity/util/BytesUtils.h>
|
#include <test/libsolidity/util/BytesUtils.h>
|
||||||
|
|
||||||
|
#include <test/libsolidity/util/SoltestErrors.h>
|
||||||
|
|
||||||
#include <liblangutil/Common.h>
|
#include <liblangutil/Common.h>
|
||||||
|
|
||||||
#include <boost/algorithm/string.hpp>
|
#include <boost/algorithm/string.hpp>
|
||||||
@ -34,6 +36,40 @@ using namespace dev::solidity::test;
|
|||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace soltest;
|
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)
|
bytes BytesUtils::convertBoolean(string const& _literal)
|
||||||
{
|
{
|
||||||
if (_literal == "true")
|
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;
|
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)
|
if (*_bytes.begin() & 0x80)
|
||||||
os << u2s(fromBigEndian<u256>(_bytes));
|
os << u2s(fromBigEndian<u256>(_bytes));
|
||||||
else
|
else
|
||||||
@ -95,19 +142,7 @@ string BytesUtils::formatUnsigned(bytes const& _bytes) const
|
|||||||
return os.str();
|
return os.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
string BytesUtils::formatSigned(bytes const& _bytes) const
|
string BytesUtils::formatBoolean(bytes const& _bytes)
|
||||||
{
|
|
||||||
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
|
|
||||||
{
|
{
|
||||||
stringstream os;
|
stringstream os;
|
||||||
u256 result = fromBigEndian<u256>(_bytes);
|
u256 result = fromBigEndian<u256>(_bytes);
|
||||||
@ -122,7 +157,7 @@ string BytesUtils::formatBoolean(bytes const& _bytes) const
|
|||||||
return os.str();
|
return os.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
string BytesUtils::formatHex(bytes const& _bytes) const
|
string BytesUtils::formatHex(bytes const& _bytes)
|
||||||
{
|
{
|
||||||
stringstream os;
|
stringstream os;
|
||||||
|
|
||||||
@ -133,7 +168,7 @@ string BytesUtils::formatHex(bytes const& _bytes) const
|
|||||||
return os.str();
|
return os.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
string BytesUtils::formatHexString(bytes const& _bytes) const
|
string BytesUtils::formatHexString(bytes const& _bytes)
|
||||||
{
|
{
|
||||||
stringstream os;
|
stringstream os;
|
||||||
|
|
||||||
@ -170,36 +205,3 @@ string BytesUtils::formatString(bytes const& _bytes, size_t _cutOff) const
|
|||||||
|
|
||||||
return os.str();
|
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));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -32,46 +32,64 @@ namespace test
|
|||||||
class BytesUtils
|
class BytesUtils
|
||||||
{
|
{
|
||||||
public:
|
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`
|
/// Tries to convert \param _literal to an unpadded `bytes`
|
||||||
/// representation of the boolean number literal. Throws if conversion fails.
|
/// 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`
|
/// Tries to convert \param _literal to an unpadded `bytes`
|
||||||
/// representation of the decimal number literal. Throws if conversion fails.
|
/// 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`
|
/// Tries to convert \param _literal to an unpadded `bytes`
|
||||||
/// representation of the hex literal. Throws if conversion fails.
|
/// 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`
|
/// Tries to convert \param _literal to an unpadded `bytes`
|
||||||
/// representation of the string literal. Throws if conversion fails.
|
/// 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
|
/// Converts \param _bytes to a soltest-compliant and human-readable
|
||||||
/// string representation of a byte array which is assumed to hold
|
/// string representation of a byte array which is assumed to hold
|
||||||
/// an unsigned value.
|
/// 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
|
/// Converts \param _bytes to a soltest-compliant and human-readable
|
||||||
/// string representation of a byte array which is assumed to hold
|
/// string representation of a byte array which is assumed to hold
|
||||||
/// a signed value.
|
/// 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
|
/// Converts \param _bytes to a soltest-compliant and human-readable
|
||||||
/// string representation of a byte array which is assumed to hold
|
/// string representation of a byte array which is assumed to hold
|
||||||
/// a boolean value.
|
/// 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
|
/// Converts \param _bytes to a soltest-compliant and human-readable
|
||||||
/// string representation of a byte array which is assumed to hold
|
/// string representation of a byte array which is assumed to hold
|
||||||
/// a hex value.
|
/// 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
|
/// Converts \param _bytes to a soltest-compliant and human-readable
|
||||||
/// string representation of a byte array which is assumed to hold
|
/// string representation of a byte array which is assumed to hold
|
||||||
/// a hexString value.
|
/// 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
|
/// Converts \param _bytes to a soltest-compliant and human-readable
|
||||||
/// string representation of a byte array which is assumed to hold
|
/// string representation of a byte array which is assumed to hold
|
||||||
@ -82,22 +100,6 @@ public:
|
|||||||
{
|
{
|
||||||
return formatString(_bytes, _bytes.size());
|
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;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -236,7 +236,6 @@ Parameter TestFileParser::parseParameter()
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool isSigned = false;
|
bool isSigned = false;
|
||||||
|
|
||||||
if (accept(Token::Left, true))
|
if (accept(Token::Left, true))
|
||||||
{
|
{
|
||||||
parameter.rawString += formatToken(Token::Left);
|
parameter.rawString += formatToken(Token::Left);
|
||||||
@ -267,10 +266,10 @@ Parameter TestFileParser::parseParameter()
|
|||||||
parameter.abiType = ABIType{ABIType::Boolean, ABIType::AlignRight, 32};
|
parameter.abiType = ABIType{ABIType::Boolean, ABIType::AlignRight, 32};
|
||||||
string parsed = parseBoolean();
|
string parsed = parseBoolean();
|
||||||
parameter.rawString += parsed;
|
parameter.rawString += parsed;
|
||||||
parameter.rawBytes = BytesUtils().applyAlign(
|
parameter.rawBytes = BytesUtils::applyAlign(
|
||||||
parameter.alignment,
|
parameter.alignment,
|
||||||
parameter.abiType,
|
parameter.abiType,
|
||||||
BytesUtils().convertBoolean(parsed)
|
BytesUtils::convertBoolean(parsed)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
else if (accept(Token::HexNumber))
|
else if (accept(Token::HexNumber))
|
||||||
@ -281,10 +280,10 @@ Parameter TestFileParser::parseParameter()
|
|||||||
parameter.abiType = ABIType{ABIType::Hex, ABIType::AlignRight, 32};
|
parameter.abiType = ABIType{ABIType::Hex, ABIType::AlignRight, 32};
|
||||||
string parsed = parseHexNumber();
|
string parsed = parseHexNumber();
|
||||||
parameter.rawString += parsed;
|
parameter.rawString += parsed;
|
||||||
parameter.rawBytes = BytesUtils().applyAlign(
|
parameter.rawBytes = BytesUtils::applyAlign(
|
||||||
parameter.alignment,
|
parameter.alignment,
|
||||||
parameter.abiType,
|
parameter.abiType,
|
||||||
BytesUtils().convertHexNumber(parsed)
|
BytesUtils::convertHexNumber(parsed)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
else if (accept(Token::Hex, true))
|
else if (accept(Token::Hex, true))
|
||||||
@ -296,7 +295,7 @@ Parameter TestFileParser::parseParameter()
|
|||||||
|
|
||||||
string parsed = parseString();
|
string parsed = parseString();
|
||||||
parameter.rawString += "hex\"" + parsed + "\"";
|
parameter.rawString += "hex\"" + parsed + "\"";
|
||||||
parameter.rawBytes = BytesUtils().convertHexNumber(parsed);
|
parameter.rawBytes = BytesUtils::convertHexNumber(parsed);
|
||||||
parameter.abiType = ABIType{
|
parameter.abiType = ABIType{
|
||||||
ABIType::HexString, ABIType::AlignNone, parameter.rawBytes.size()
|
ABIType::HexString, ABIType::AlignNone, parameter.rawBytes.size()
|
||||||
};
|
};
|
||||||
@ -311,10 +310,10 @@ Parameter TestFileParser::parseParameter()
|
|||||||
string parsed = parseString();
|
string parsed = parseString();
|
||||||
parameter.abiType = {ABIType::String, ABIType::AlignLeft, parsed.size()};
|
parameter.abiType = {ABIType::String, ABIType::AlignLeft, parsed.size()};
|
||||||
parameter.rawString += "\"" + parsed + "\"";
|
parameter.rawString += "\"" + parsed + "\"";
|
||||||
parameter.rawBytes = BytesUtils().applyAlign(
|
parameter.rawBytes = BytesUtils::applyAlign(
|
||||||
Parameter::Alignment::Left,
|
Parameter::Alignment::Left,
|
||||||
parameter.abiType,
|
parameter.abiType,
|
||||||
BytesUtils().convertString(parsed)
|
BytesUtils::convertString(parsed)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
else if (accept(Token::Number))
|
else if (accept(Token::Number))
|
||||||
@ -327,10 +326,10 @@ Parameter TestFileParser::parseParameter()
|
|||||||
if (isSigned)
|
if (isSigned)
|
||||||
parsed = "-" + parsed;
|
parsed = "-" + parsed;
|
||||||
|
|
||||||
parameter.rawBytes = BytesUtils().applyAlign(
|
parameter.rawBytes = BytesUtils::applyAlign(
|
||||||
parameter.alignment,
|
parameter.alignment,
|
||||||
parameter.abiType,
|
parameter.abiType,
|
||||||
BytesUtils().convertNumber(parsed)
|
BytesUtils::convertNumber(parsed)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
else if (accept(Token::Failure, true))
|
else if (accept(Token::Failure, true))
|
||||||
|
Loading…
Reference in New Issue
Block a user