diff options
author | Amir Bandeali <abandeali1@gmail.com> | 2018-05-10 07:27:50 +0800 |
---|---|---|
committer | Amir Bandeali <abandeali1@gmail.com> | 2018-05-31 08:11:30 +0800 |
commit | a5a7217c8f24fe98275fd9927ca8c5e5f140c6f3 (patch) | |
tree | f58846c928d05047265b471638eab11d627bdc99 /packages/contracts/src | |
parent | c0cf55b40bb4a13cfd94a506bf125f6eb57c6767 (diff) | |
download | dexon-0x-contracts-a5a7217c8f24fe98275fd9927ca8c5e5f140c6f3.tar.gz dexon-0x-contracts-a5a7217c8f24fe98275fd9927ca8c5e5f140c6f3.tar.zst dexon-0x-contracts-a5a7217c8f24fe98275fd9927ca8c5e5f140c6f3.zip |
Add deepCopyBytes method to LibBytes
Diffstat (limited to 'packages/contracts/src')
-rw-r--r-- | packages/contracts/src/contracts/current/test/TestLibBytes/TestLibBytes.sol | 17 | ||||
-rw-r--r-- | packages/contracts/src/contracts/current/utils/LibBytes/LibBytes.sol | 41 |
2 files changed, 58 insertions, 0 deletions
diff --git a/packages/contracts/src/contracts/current/test/TestLibBytes/TestLibBytes.sol b/packages/contracts/src/contracts/current/test/TestLibBytes/TestLibBytes.sol index 5a6801262..b3ed1f620 100644 --- a/packages/contracts/src/contracts/current/test/TestLibBytes/TestLibBytes.sol +++ b/packages/contracts/src/contracts/current/test/TestLibBytes/TestLibBytes.sol @@ -25,6 +25,23 @@ contract TestLibBytes is LibBytes { + /// @dev Performs a deep copy of a section of a byte array. + /// @param b Byte array that will be sliced. + /// @param startIndex Index of first byte to copy. + /// @param endIndex Index of last byte to copy + 1. + /// @return A deep copy of b from startIndex to endIndex. + function publicDeepCopyBytes( + bytes memory b, + uint256 startIndex, + uint256 endIndex) + public + pure + returns (bytes memory copy) + { + copy = deepCopyBytes(b, startIndex, endIndex); + return copy; + } + /// @dev Tests equality of two byte arrays. /// @param lhs First byte array to compare. /// @param rhs Second byte array to compare. diff --git a/packages/contracts/src/contracts/current/utils/LibBytes/LibBytes.sol b/packages/contracts/src/contracts/current/utils/LibBytes/LibBytes.sol index 975565773..527f7a7a9 100644 --- a/packages/contracts/src/contracts/current/utils/LibBytes/LibBytes.sol +++ b/packages/contracts/src/contracts/current/utils/LibBytes/LibBytes.sol @@ -21,9 +21,50 @@ pragma solidity ^0.4.24; contract LibBytes { // Revert reasons + string constant GT_ZERO_LENGTH_REQUIRED = "Length must be greater than 0."; string constant GTE_4_LENGTH_REQUIRED = "Length must be greater than or equal to 4."; string constant GTE_20_LENGTH_REQUIRED = "Length must be greater than or equal to 20."; string constant GTE_32_LENGTH_REQUIRED = "Length must be greater than or equal to 32."; + string constant INDEX_OUT_OF_BOUNDS = "Specified array index is out of bounds."; + + /// @dev Performs a deep copy of a section of a byte array. + /// @param b Byte array that will be copied. + /// @param index Index of first byte to copy. + /// @param len Number of bytes to copy starting from index. + /// @return A deep copy `len` bytes of b starting from index. + function deepCopyBytes( + bytes memory b, + uint256 index, + uint256 len) + internal + pure + returns (bytes memory) + { + require( + len > 0, + GT_ZERO_LENGTH_REQUIRED + ); + require( + b.length >= index + len, + INDEX_OUT_OF_BOUNDS + ); + + bytes memory copy = new bytes(len); + + // Arrays are prefixed by a 256 bit length parameter + index += 32; + + assembly { + // Start storing onto copy after length field + let storeStartIndex := add(copy, 32) + // Start loading from b at index + let startLoadIndex := add(b, index) + for {let i := 0} lt(i, len) {i := add(i, 32)} { + mstore(add(storeStartIndex, i), mload(add(startLoadIndex, i))) + } + } + return copy; + } /// @dev Tests equality of two byte arrays. /// @param lhs First byte array to compare. |