Rename bytes.rawAddress and add bytes.contentAddress

This commit is contained in:
Remco Bloemen 2018-06-13 17:20:18 +02:00
parent 88982f98ff
commit 2054cd78da
2 changed files with 25 additions and 8 deletions

View File

@ -243,7 +243,7 @@ contract TestLibBytes {
require(dest + length <= mem.length); require(dest + length <= mem.length);
// Get pointer to memory contents // Get pointer to memory contents
uint256 offset = mem.getMemAddress() + 32; uint256 offset = mem.contentAddress();
// Execute memCopy adjusted for memory array location // Execute memCopy adjusted for memory array location
LibBytes.memCopy(offset + dest, offset + source, length); LibBytes.memCopy(offset + dest, offset + source, length);

View File

@ -19,6 +19,7 @@
pragma solidity ^0.4.24; pragma solidity ^0.4.24;
library LibBytes { library LibBytes {
using LibBytes for bytes;
// Revert reasons // Revert reasons
string constant GREATER_THAN_ZERO_LENGTH_REQUIRED = "GREATER_THAN_ZERO_LENGTH_REQUIRED"; string constant GREATER_THAN_ZERO_LENGTH_REQUIRED = "GREATER_THAN_ZERO_LENGTH_REQUIRED";
@ -30,8 +31,10 @@ library LibBytes {
/// @dev Gets the memory address for a byte array. /// @dev Gets the memory address for a byte array.
/// @param input Byte array to lookup. /// @param input Byte array to lookup.
/// @return memoryAddress Memory address of byte array. /// @return memoryAddress Memory address of byte array. This
function getMemAddress(bytes memory input) /// points to the header of the byte array which contains
/// the length.
function rawAddress(bytes memory input)
internal internal
pure pure
returns (uint256 memoryAddress) returns (uint256 memoryAddress)
@ -41,6 +44,20 @@ library LibBytes {
} }
return memoryAddress; return memoryAddress;
} }
/// @dev Gets the memory address for the contents of a byte array.
/// @param input Byte array to lookup.
/// @return memoryAddress Memory address of the contents of the byte array.
function contentAddress(bytes memory input)
internal
pure
returns (uint256 memoryAddress)
{
assembly {
memoryAddress := add(input, 32)
}
return memoryAddress;
}
/// @dev Copies `length` bytes from memory location `source` to `dest`. /// @dev Copies `length` bytes from memory location `source` to `dest`.
/// @param dest memory address to copy bytes to. /// @param dest memory address to copy bytes to.
@ -393,8 +410,8 @@ library LibBytes {
// Allocate memory and copy value to result // Allocate memory and copy value to result
result = new bytes(nestedBytesLength); result = new bytes(nestedBytesLength);
memCopy( memCopy(
getMemAddress(result) + 32, // +32 skips array length result.contentAddress(),
getMemAddress(b) + index + 32, b.contentAddress() + index,
nestedBytesLength nestedBytesLength
); );
@ -422,9 +439,9 @@ library LibBytes {
// Copy <input> into <b> // Copy <input> into <b>
memCopy( memCopy(
getMemAddress(b) + 32 + index, // +32 to skip length of <b> b.contentAddress() + index,
getMemAddress(input), // includes length of <input> input.rawAddress(), // includes length of <input>
input.length + 32 // +32 bytes to store <input> length input.length + 32 // +32 bytes to store <input> length
); );
} }