@@ -17,7 +17,7 @@ import {Math} from "./math/Math.sol";
1717
1818const sort = type => `\
1919/**
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.
2121 *
2222 * This function does the sorting "in place", meaning that it overrides the input. The object is returned for
2323 * 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 => `\
3030 * IMPORTANT: Consider memory side-effects when using custom comparator functions that access memory in an unsafe way.
3131 */
3232function 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) {
3636 ${
37- type === 'uint256'
37+ type . name === 'uint256'
3838 ? '_quickSort(_begin(array), _end(array), comp);'
3939 : 'sort(_castToUint256Array(array), _castToUint256Comp(comp));'
4040 }
4141 return array;
4242}
4343
4444/**
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.
4646 */
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);' }
4949 return array;
5050}
5151` ;
@@ -126,18 +126,18 @@ function _swap(uint256 ptr1, uint256 ptr2) private pure {
126126` ;
127127
128128const 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) {
131131 assembly {
132132 output := input
133133 }
134134}
135135` ;
136136
137137const 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
139139function _castToUint256Comp(
140- function(${ type } , ${ type } ) pure returns (bool) input
140+ function(${ type . name } , ${ type . name } ) pure returns (bool) input
141141) private pure returns (function(uint256, uint256) pure returns (bool) output) {
142142 assembly {
143143 output := input
@@ -320,14 +320,14 @@ const unsafeAccessStorage = type => `\
320320 *
321321 * WARNING: Only use if you are certain \`pos\` is lower than the array length.
322322 */
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 ,
325325) } Slot storage) {
326326 bytes32 slot;
327327 assembly ("memory-safe") {
328328 slot := arr.slot
329329 }
330- return slot.deriveArray().offset(pos).get${ capitalize ( type ) } Slot();
330+ return slot.deriveArray().offset(pos).get${ capitalize ( type . name ) } Slot();
331331}
332332` ;
333333
@@ -337,7 +337,9 @@ const unsafeAccessMemory = type => `\
337337 *
338338 * WARNING: Only use if you are certain \`pos\` is lower than the array length.
339339 */
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) {
341343 assembly {
342344 res := mload(add(add(arr, 0x20), mul(pos, 0x20)))
343345 }
@@ -350,7 +352,7 @@ const unsafeSetLength = type => `\
350352 *
351353 * WARNING: this does not clear elements if length is reduced, of initialize elements if length is increased.
352354 */
353- function unsafeSetLength(${ type } [] storage array, uint256 len) internal {
355+ function unsafeSetLength(${ type . name } [] storage array, uint256 len) internal {
354356 assembly ("memory-safe") {
355357 sstore(array.slot, len)
356358 }
@@ -367,11 +369,11 @@ module.exports = format(
367369 'using StorageSlot for bytes32;' ,
368370 '' ,
369371 // 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 ) ,
372374 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 ) ,
375377 // lookup
376378 search ,
377379 // unsafe (direct) storage and memory access
0 commit comments