aboutsummaryrefslogtreecommitdiffstats
path: root/packages/contracts/src
diff options
context:
space:
mode:
authorRemco Bloemen <remco@wicked.ventures>2018-06-13 23:34:35 +0800
committerRemco Bloemen <remco@wicked.ventures>2018-06-23 19:53:38 +0800
commitc83ee04662177407f1d484175d8a76dddf3c4e85 (patch)
treee036e5bd3b057e064059838c8621b465bd94ecf7 /packages/contracts/src
parent2054cd78da9c39e5572cd76bb9fff7a4cd581903 (diff)
downloaddexon-sol-tools-c83ee04662177407f1d484175d8a76dddf3c4e85.tar
dexon-sol-tools-c83ee04662177407f1d484175d8a76dddf3c4e85.tar.gz
dexon-sol-tools-c83ee04662177407f1d484175d8a76dddf3c4e85.tar.bz2
dexon-sol-tools-c83ee04662177407f1d484175d8a76dddf3c4e85.tar.lz
dexon-sol-tools-c83ee04662177407f1d484175d8a76dddf3c4e85.tar.xz
dexon-sol-tools-c83ee04662177407f1d484175d8a76dddf3c4e85.tar.zst
dexon-sol-tools-c83ee04662177407f1d484175d8a76dddf3c4e85.zip
Add slice and sliceDestructive
Diffstat (limited to 'packages/contracts/src')
-rw-r--r--packages/contracts/src/contracts/current/utils/LibBytes/LibBytes.sol46
1 files changed, 46 insertions, 0 deletions
diff --git a/packages/contracts/src/contracts/current/utils/LibBytes/LibBytes.sol b/packages/contracts/src/contracts/current/utils/LibBytes/LibBytes.sol
index d824449a2..58ce31c70 100644
--- a/packages/contracts/src/contracts/current/utils/LibBytes/LibBytes.sol
+++ b/packages/contracts/src/contracts/current/utils/LibBytes/LibBytes.sol
@@ -28,6 +28,8 @@ library LibBytes {
string constant GREATER_OR_EQUAL_TO_32_LENGTH_REQUIRED = "GREATER_OR_EQUAL_TO_32_LENGTH_REQUIRED";
string constant GREATER_OR_EQUAL_TO_NESTED_BYTES_LENGTH_REQUIRED = "GREATER_OR_EQUAL_TO_NESTED_BYTES_LENGTH_REQUIRED";
string constant GREATER_OR_EQUAL_TO_SOURCE_BYTES_LENGTH_REQUIRED = "GREATER_OR_EQUAL_TO_SOURCE_BYTES_LENGTH_REQUIRED";
+ string constant FROM_LESS_THAN_TO_REQUIRED = "FROM_LESS_THAN_TO_REQUIRED";
+ string constant TO_LESS_THAN_LENGTH_REQUIRED = "TO_LESS_THAN_LENGTH_REQUIRED";
/// @dev Gets the memory address for a byte array.
/// @param input Byte array to lookup.
@@ -163,6 +165,50 @@ library LibBytes {
}
}
}
+
+ /// @dev Returns a slices from a byte array.
+ /// @param b The byte array to take a slice from.
+ /// @param from The starting index for the slice (inclusive).
+ /// @param to The final index for the slice (exclusive).
+ /// @return result The slice containing bytes at indices [from, to)
+ function slice(bytes memory b, uint256 from, uint256 to)
+ internal
+ pure
+ returns (bytes memory result)
+ {
+ require(from <= to, FROM_LESS_THAN_TO_REQUIRED);
+ require(to < b.length, TO_LESS_THAN_LENGTH_REQUIRED);
+
+ // Create a new bytes structure and copy contents
+ result = new bytes(to - from);
+ memCopy(
+ result.contentAddress(),
+ b.contentAddress() + from,
+ result.length);
+ return result;
+ }
+
+ /// @dev Returns a slices from a byte array without preserving the input.
+ /// @param b The byte array to take a slice from. Will be destroyed in the process.
+ /// @param from The starting index for the slice (inclusive).
+ /// @param to The final index for the slice (exclusive).
+ /// @return result The slice containing bytes at indices [from, to)
+ /// @dev When `from == 0`, the original array will match the slice. In other cases it's state will be corrupted.
+ function sliceDestructive(bytes memory b, uint256 from, uint256 to)
+ internal
+ pure
+ returns (bytes memory result)
+ {
+ require(from <= to, FROM_LESS_THAN_TO_REQUIRED);
+ require(to < b.length, TO_LESS_THAN_LENGTH_REQUIRED);
+
+ // Create a new bytes structure around [from, to) in-place.
+ assembly {
+ result := add(b, from)
+ mstore(result, sub(to, from))
+ }
+ return result;
+ }
/// @dev Pops the last byte off of a byte array by modifying its length.
/// @param b Byte array that will be modified.