Replace error strings with custom errors (#4261)
This commit is contained in:
@ -13,6 +13,11 @@ library Strings {
|
||||
bytes16 private constant _SYMBOLS = "0123456789abcdef";
|
||||
uint8 private constant _ADDRESS_LENGTH = 20;
|
||||
|
||||
/**
|
||||
* @dev The `value` string doesn't fit in the specified `length`.
|
||||
*/
|
||||
error StringsInsufficientHexLength(uint256 value, uint256 length);
|
||||
|
||||
/**
|
||||
* @dev Converts a `uint256` to its ASCII `string` decimal representation.
|
||||
*/
|
||||
@ -58,14 +63,17 @@ library Strings {
|
||||
* @dev Converts a `uint256` to its ASCII `string` hexadecimal representation with fixed length.
|
||||
*/
|
||||
function toHexString(uint256 value, uint256 length) internal pure returns (string memory) {
|
||||
uint256 localValue = value;
|
||||
bytes memory buffer = new bytes(2 * length + 2);
|
||||
buffer[0] = "0";
|
||||
buffer[1] = "x";
|
||||
for (uint256 i = 2 * length + 1; i > 1; --i) {
|
||||
buffer[i] = _SYMBOLS[value & 0xf];
|
||||
value >>= 4;
|
||||
buffer[i] = _SYMBOLS[localValue & 0xf];
|
||||
localValue >>= 4;
|
||||
}
|
||||
if (localValue != 0) {
|
||||
revert StringsInsufficientHexLength(value, length);
|
||||
}
|
||||
require(value == 0, "Strings: hex length insufficient");
|
||||
return string(buffer);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user