@@ -17,7 +17,7 @@ import {Math} from "./math/Math.sol";
17
17
18
18
const sort = type => `\
19
19
/**
20
- * @dev Sort an array of ${ type } (in memory) following the provided comparator function.
20
+ * @dev Sort an array of ${ type . name } (in memory) following the provided comparator function.
21
21
*
22
22
* This function does the sorting "in place", meaning that it overrides the input. The object is returned for
23
23
* convenience, but that returned value can be discarded safely if the caller has a memory pointer to the array.
@@ -30,22 +30,22 @@ const sort = type => `\
30
30
* IMPORTANT: Consider memory side-effects when using custom comparator functions that access memory in an unsafe way.
31
31
*/
32
32
function sort(
33
- ${ type } [] memory array,
34
- function(${ type } , ${ type } ) pure returns (bool) comp
35
- ) internal pure returns (${ type } [] memory) {
33
+ ${ type . name } [] memory array,
34
+ function(${ type . name } , ${ type . name } ) pure returns (bool) comp
35
+ ) internal pure returns (${ type . name } [] memory) {
36
36
${
37
- type === 'uint256'
37
+ type . name === 'uint256'
38
38
? '_quickSort(_begin(array), _end(array), comp);'
39
39
: 'sort(_castToUint256Array(array), _castToUint256Comp(comp));'
40
40
}
41
41
return array;
42
42
}
43
43
44
44
/**
45
- * @dev Variant of {sort} that sorts an array of ${ type } in increasing order.
45
+ * @dev Variant of {sort} that sorts an array of ${ type . name } in increasing order.
46
46
*/
47
- function sort(${ type } [] memory array) internal pure returns (${ type } [] memory) {
48
- ${ type === 'uint256' ? 'sort(array, Comparators.lt);' : 'sort(_castToUint256Array(array), Comparators.lt);' }
47
+ function sort(${ type . name } [] memory array) internal pure returns (${ type . name } [] memory) {
48
+ ${ type . name === 'uint256' ? 'sort(array, Comparators.lt);' : 'sort(_castToUint256Array(array), Comparators.lt);' }
49
49
return array;
50
50
}
51
51
` ;
@@ -126,18 +126,18 @@ function _swap(uint256 ptr1, uint256 ptr2) private pure {
126
126
` ;
127
127
128
128
const castArray = type => `\
129
- /// @dev Helper: low level cast ${ type } memory array to uint256 memory array
130
- function _castToUint256Array(${ type } [] memory input) private pure returns (uint256[] memory output) {
129
+ /// @dev Helper: low level cast ${ type . name } memory array to uint256 memory array
130
+ function _castToUint256Array(${ type . name } [] memory input) private pure returns (uint256[] memory output) {
131
131
assembly {
132
132
output := input
133
133
}
134
134
}
135
135
` ;
136
136
137
137
const castComparator = type => `\
138
- /// @dev Helper: low level cast ${ type } comp function to uint256 comp function
138
+ /// @dev Helper: low level cast ${ type . name } comp function to uint256 comp function
139
139
function _castToUint256Comp(
140
- function(${ type } , ${ type } ) pure returns (bool) input
140
+ function(${ type . name } , ${ type . name } ) pure returns (bool) input
141
141
) private pure returns (function(uint256, uint256) pure returns (bool) output) {
142
142
assembly {
143
143
output := input
@@ -320,14 +320,14 @@ const unsafeAccessStorage = type => `\
320
320
*
321
321
* WARNING: Only use if you are certain \`pos\` is lower than the array length.
322
322
*/
323
- function unsafeAccess(${ type } [] storage arr, uint256 pos) internal pure returns (StorageSlot.${ capitalize (
324
- type ,
323
+ function unsafeAccess(${ type . name } [] storage arr, uint256 pos) internal pure returns (StorageSlot.${ capitalize (
324
+ type . name ,
325
325
) } Slot storage) {
326
326
bytes32 slot;
327
327
assembly ("memory-safe") {
328
328
slot := arr.slot
329
329
}
330
- return slot.deriveArray().offset(pos).get${ capitalize ( type ) } Slot();
330
+ return slot.deriveArray().offset(pos).get${ capitalize ( type . name ) } Slot();
331
331
}
332
332
` ;
333
333
@@ -337,7 +337,9 @@ const unsafeAccessMemory = type => `\
337
337
*
338
338
* WARNING: Only use if you are certain \`pos\` is lower than the array length.
339
339
*/
340
- function unsafeMemoryAccess(${ type } [] memory arr, uint256 pos) internal pure returns (${ type } res) {
340
+ function unsafeMemoryAccess(${ type . name } [] memory arr, uint256 pos) internal pure returns (${ type . name } ${
341
+ type . isValueType ? '' : ' memory'
342
+ } res) {
341
343
assembly {
342
344
res := mload(add(add(arr, 0x20), mul(pos, 0x20)))
343
345
}
@@ -350,7 +352,7 @@ const unsafeSetLength = type => `\
350
352
*
351
353
* WARNING: this does not clear elements if length is reduced, of initialize elements if length is increased.
352
354
*/
353
- function unsafeSetLength(${ type } [] storage array, uint256 len) internal {
355
+ function unsafeSetLength(${ type . name } [] storage array, uint256 len) internal {
354
356
assembly ("memory-safe") {
355
357
sstore(array.slot, len)
356
358
}
@@ -367,11 +369,11 @@ module.exports = format(
367
369
'using StorageSlot for bytes32;' ,
368
370
'' ,
369
371
// sorting, comparator, helpers and internal
370
- sort ( 'uint256' ) ,
371
- TYPES . filter ( type => type !== 'uint256' ) . map ( sort ) ,
372
+ sort ( { name : 'uint256' } ) ,
373
+ TYPES . filter ( type => type . isValueType && type . name !== 'uint256' ) . map ( sort ) ,
372
374
quickSort ,
373
- TYPES . filter ( type => type !== 'uint256' ) . map ( castArray ) ,
374
- TYPES . filter ( type => type !== 'uint256' ) . map ( castComparator ) ,
375
+ TYPES . filter ( type => type . isValueType && type . name !== 'uint256' ) . map ( castArray ) ,
376
+ TYPES . filter ( type => type . isValueType && type . name !== 'uint256' ) . map ( castComparator ) ,
375
377
// lookup
376
378
search ,
377
379
// unsafe (direct) storage and memory access
0 commit comments