forked from ethereum/solidity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBytesUtils.h
136 lines (111 loc) · 5.14 KB
/
BytesUtils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <test/libsolidity/util/SoltestTypes.h>
#include <libsolutil/CommonData.h>
namespace solidity::frontend::test
{
/**
* Utility class that aids conversions from parsed strings to an
* isoltest-internal, ABI-based bytes representation and vice-versa.
*/
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.
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.
static bytes convertNumber(std::string const& _literal);
/// Tries to convert \param _literal to an unpadded `bytes`
/// representation of the decimal number literal. Throws if conversion fails.
static bytes convertFixedPoint(std::string const& _literal, size_t& o_fractionalDigits);
/// Tries to convert \param _literal to an unpadded `bytes`
/// representation of the hex literal. Throws if conversion fails.
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.
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.
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.
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.
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.
/// The _shorten flag is used to trim leading and trailing zeros.
static std::string formatHex(bytes const& _bytes, bool _shorten = false);
/// Converts \param _bytes to a soltest-compliant and human-readable
/// string representation of a byte array which is assumed to hold
/// a hexString value.
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
/// a string value.
static std::string formatString(bytes const& _bytes, size_t _cutOff);
static std::string formatString(bytes const& _bytes)
{
return formatString(_bytes, _bytes.size());
}
/// Converts \param _bytes to a soltest-compliant and human-readable
/// decimal string representation of a byte array. Format of \param _bytes is binary.
static std::string formatFixedPoint(bytes const& _bytes, bool _signed, size_t _fractionalDigits);
/// Used to print returned bytes from function calls to the commandline.
/// Returns a string representation of given _bytes in ranges of 32 bytes.
/// If _withSignature is true, the first 4 bytes will be formatted separately.
static std::string formatRawBytes(
bytes const& _bytes,
ParameterList const& _parameters,
std::string _linePrefix = ""
);
/// Formats given _bytes with type information passed in _abiType.
static std::string formatBytes(bytes const& _bytes, ABIType const& _abiType);
/// Formats given _bytes with type information passed in _abiTypes.
/// Prints obtained result if it does not match the expectation
/// and prints the expected result otherwise.
/// Highlights parameter only if it does not match.
static std::string formatBytesRange(
bytes _bytes,
ParameterList const& _parameters,
bool _highlight
);
/// Count the number of zeros between the last non-zero byte and the end of
/// \param _bytes.
static size_t countRightPaddedZeros(bytes const& _bytes);
};
}