Skip to content

Commit

Permalink
Merge pull request Arachnid#30 from axic/memory-pointer
Browse files Browse the repository at this point in the history
Add default data location (memory)
  • Loading branch information
Arachnid authored Jul 25, 2018
2 parents d2e4eef + b9a6f66 commit 3c63f18
Showing 1 changed file with 26 additions and 28 deletions.
54 changes: 26 additions & 28 deletions src/strings.sol
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ library strings {
* @param self The string to make a slice from.
* @return A newly allocated slice containing the entire string.
*/
function toSlice(string self) internal pure returns (slice) {
function toSlice(string memory self) internal pure returns (slice memory) {
uint ptr;
assembly {
ptr := add(self, 0x20)
Expand Down Expand Up @@ -112,7 +112,7 @@ library strings {
* @return A new slice containing the value of the input argument up to the
* first null.
*/
function toSliceB32(bytes32 self) internal pure returns (slice ret) {
function toSliceB32(bytes32 self) internal pure returns (slice memory ret) {
// Allocate space for `self` in memory, copy it there, and point ret at it
assembly {
let ptr := mload(0x40)
Expand All @@ -128,7 +128,7 @@ library strings {
* @param self The slice to copy.
* @return A new slice containing the same data as `self`.
*/
function copy(slice self) internal pure returns (slice) {
function copy(slice memory self) internal pure returns (slice memory) {
return slice(self._len, self._ptr);
}

Expand All @@ -137,7 +137,7 @@ library strings {
* @param self The slice to copy.
* @return A newly allocated string containing the slice's text.
*/
function toString(slice self) internal pure returns (string) {
function toString(slice memory self) internal pure returns (string memory) {
string memory ret = new string(self._len);
uint retptr;
assembly { retptr := add(ret, 32) }
Expand All @@ -154,7 +154,7 @@ library strings {
* @param self The slice to operate on.
* @return The length of the slice in runes.
*/
function len(slice self) internal pure returns (uint l) {
function len(slice memory self) internal pure returns (uint l) {
// Starting at ptr-31 means the LSB will be the byte we care about
uint ptr = self._ptr - 31;
uint end = ptr + self._len;
Expand Down Expand Up @@ -182,7 +182,7 @@ library strings {
* @param self The slice to operate on.
* @return True if the slice is empty, False otherwise.
*/
function empty(slice self) internal pure returns (bool) {
function empty(slice memory self) internal pure returns (bool) {
return self._len == 0;
}

Expand All @@ -195,7 +195,7 @@ library strings {
* @param other The second slice to compare.
* @return The result of the comparison.
*/
function compare(slice self, slice other) internal pure returns (int) {
function compare(slice memory self, slice memory other) internal pure returns (int) {
uint shortest = self._len;
if (other._len < self._len)
shortest = other._len;
Expand Down Expand Up @@ -231,7 +231,7 @@ library strings {
* @param self The second slice to compare.
* @return True if the slices are equal, false otherwise.
*/
function equals(slice self, slice other) internal pure returns (bool) {
function equals(slice memory self, slice memory other) internal pure returns (bool) {
return compare(self, other) == 0;
}

Expand All @@ -242,7 +242,7 @@ library strings {
* @param rune The slice that will contain the first rune.
* @return `rune`.
*/
function nextRune(slice self, slice rune) internal pure returns (slice) {
function nextRune(slice memory self, slice memory rune) internal pure returns (slice memory) {
rune._ptr = self._ptr;

if (self._len == 0) {
Expand Down Expand Up @@ -284,7 +284,7 @@ library strings {
* @param self The slice to operate on.
* @return A slice containing only the first rune from `self`.
*/
function nextRune(slice self) internal pure returns (slice ret) {
function nextRune(slice memory self) internal pure returns (slice memory ret) {
nextRune(self, ret);
}

Expand All @@ -293,7 +293,7 @@ library strings {
* @param self The slice to operate on.
* @return The number of the first codepoint in the slice.
*/
function ord(slice self) internal pure returns (uint ret) {
function ord(slice memory self) internal pure returns (uint ret) {
if (self._len == 0) {
return 0;
}
Expand Down Expand Up @@ -342,7 +342,7 @@ library strings {
* @param self The slice to hash.
* @return The hash of the slice.
*/
function keccak(slice self) internal pure returns (bytes32 ret) {
function keccak(slice memory self) internal pure returns (bytes32 ret) {
assembly {
ret := keccak256(mload(add(self, 32)), mload(self))
}
Expand All @@ -354,7 +354,7 @@ library strings {
* @param needle The slice to search for.
* @return True if the slice starts with the provided text, false otherwise.
*/
function startsWith(slice self, slice needle) internal pure returns (bool) {
function startsWith(slice memory self, slice memory needle) internal pure returns (bool) {
if (self._len < needle._len) {
return false;
}
Expand All @@ -380,7 +380,7 @@ library strings {
* @param needle The slice to search for.
* @return `self`
*/
function beyond(slice self, slice needle) internal pure returns (slice) {
function beyond(slice memory self, slice memory needle) internal pure returns (slice memory) {
if (self._len < needle._len) {
return self;
}
Expand Down Expand Up @@ -409,7 +409,7 @@ library strings {
* @param needle The slice to search for.
* @return True if the slice starts with the provided text, false otherwise.
*/
function endsWith(slice self, slice needle) internal pure returns (bool) {
function endsWith(slice memory self, slice memory needle) internal pure returns (bool) {
if (self._len < needle._len) {
return false;
}
Expand Down Expand Up @@ -437,7 +437,7 @@ library strings {
* @param needle The slice to search for.
* @return `self`
*/
function until(slice self, slice needle) internal pure returns (slice) {
function until(slice memory self, slice memory needle) internal pure returns (slice memory) {
if (self._len < needle._len) {
return self;
}
Expand All @@ -459,8 +459,6 @@ library strings {
return self;
}

event log_bytemask(bytes32 mask);

// Returns the memory address of the first byte of the first occurrence of
// `needle` in `self`, or the first byte after `self` if not found.
function findPtr(uint selflen, uint selfptr, uint needlelen, uint needleptr) private pure returns (uint) {
Expand Down Expand Up @@ -550,7 +548,7 @@ library strings {
* @param needle The text to search for.
* @return `self`.
*/
function find(slice self, slice needle) internal pure returns (slice) {
function find(slice memory self, slice memory needle) internal pure returns (slice memory) {
uint ptr = findPtr(self._len, self._ptr, needle._len, needle._ptr);
self._len -= ptr - self._ptr;
self._ptr = ptr;
Expand All @@ -565,7 +563,7 @@ library strings {
* @param needle The text to search for.
* @return `self`.
*/
function rfind(slice self, slice needle) internal pure returns (slice) {
function rfind(slice memory self, slice memory needle) internal pure returns (slice memory) {
uint ptr = rfindPtr(self._len, self._ptr, needle._len, needle._ptr);
self._len = ptr - self._ptr;
return self;
Expand All @@ -581,7 +579,7 @@ library strings {
* @param token An output parameter to which the first token is written.
* @return `token`.
*/
function split(slice self, slice needle, slice token) internal pure returns (slice) {
function split(slice memory self, slice memory needle, slice memory token) internal pure returns (slice memory) {
uint ptr = findPtr(self._len, self._ptr, needle._len, needle._ptr);
token._ptr = self._ptr;
token._len = ptr - self._ptr;
Expand All @@ -604,7 +602,7 @@ library strings {
* @param needle The text to search for in `self`.
* @return The part of `self` up to the first occurrence of `delim`.
*/
function split(slice self, slice needle) internal pure returns (slice token) {
function split(slice memory self, slice memory needle) internal pure returns (slice memory token) {
split(self, needle, token);
}

Expand All @@ -618,7 +616,7 @@ library strings {
* @param token An output parameter to which the first token is written.
* @return `token`.
*/
function rsplit(slice self, slice needle, slice token) internal pure returns (slice) {
function rsplit(slice memory self, slice memory needle, slice memory token) internal pure returns (slice memory) {
uint ptr = rfindPtr(self._len, self._ptr, needle._len, needle._ptr);
token._ptr = ptr;
token._len = self._len - (ptr - self._ptr);
Expand All @@ -640,7 +638,7 @@ library strings {
* @param needle The text to search for in `self`.
* @return The part of `self` after the last occurrence of `delim`.
*/
function rsplit(slice self, slice needle) internal pure returns (slice token) {
function rsplit(slice memory self, slice memory needle) internal pure returns (slice memory token) {
rsplit(self, needle, token);
}

Expand All @@ -650,7 +648,7 @@ library strings {
* @param needle The text to search for in `self`.
* @return The number of occurrences of `needle` found in `self`.
*/
function count(slice self, slice needle) internal pure returns (uint cnt) {
function count(slice memory self, slice memory needle) internal pure returns (uint cnt) {
uint ptr = findPtr(self._len, self._ptr, needle._len, needle._ptr) + needle._len;
while (ptr <= self._ptr + self._len) {
cnt++;
Expand All @@ -664,7 +662,7 @@ library strings {
* @param needle The text to search for in `self`.
* @return True if `needle` is found in `self`, false otherwise.
*/
function contains(slice self, slice needle) internal pure returns (bool) {
function contains(slice memory self, slice memory needle) internal pure returns (bool) {
return rfindPtr(self._len, self._ptr, needle._len, needle._ptr) != self._ptr;
}

Expand All @@ -675,7 +673,7 @@ library strings {
* @param other The second slice to concatenate.
* @return The concatenation of the two strings.
*/
function concat(slice self, slice other) internal pure returns (string) {
function concat(slice memory self, slice memory other) internal pure returns (string memory) {
string memory ret = new string(self._len + other._len);
uint retptr;
assembly { retptr := add(ret, 32) }
Expand All @@ -692,7 +690,7 @@ library strings {
* @return A newly allocated string containing all the slices in `parts`,
* joined with `self`.
*/
function join(slice self, slice[] parts) internal pure returns (string) {
function join(slice memory self, slice[] memory parts) internal pure returns (string memory) {
if (parts.length == 0)
return "";

Expand Down

0 comments on commit 3c63f18

Please sign in to comment.