Add bytes6 to the Packing library. (#5077)

This commit is contained in:
Hadrien Croubois
2024-06-13 00:23:35 +02:00
committed by GitHub
parent dc62599257
commit 53b5d84212
5 changed files with 329 additions and 32 deletions

View File

@ -36,43 +36,51 @@ pragma solidity ^0.8.20;
`;
const errors = `\
error OutOfRangeAccess();`;
error OutOfRangeAccess();
`;
const pack = (left, right) => `
const pack = (left, right) => `\
function pack_${left}_${right}(bytes${left} left, bytes${right} right) internal pure returns (bytes${
left + right
} result) {
assembly ("memory-safe") {
result := or(left, shr(${8 * left}, right))
}
}`;
}
`;
const extract = (outer, inner) => `
const extract = (outer, inner) => `\
function extract_${outer}_${inner}(bytes${outer} self, uint8 offset) internal pure returns (bytes${inner} result) {
if (offset > ${outer - inner}) revert OutOfRangeAccess();
assembly ("memory-safe") {
result := and(shl(mul(8, offset), self), shl(${256 - 8 * inner}, not(0)))
}
}`;
}
`;
const replace = (outer, inner) => `
const replace = (outer, inner) => `\
function replace_${outer}_${inner}(bytes${outer} self, bytes${inner} value, uint8 offset) internal pure returns (bytes${outer} result) {
bytes${inner} oldValue = extract_${outer}_${inner}(self, offset);
assembly ("memory-safe") {
result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
}
}`;
}
`;
// GENERATE
module.exports = format(
header.trimEnd(),
'library Packing {',
errors,
product(SIZES, SIZES)
.filter(([left, right]) => SIZES.includes(left + right))
.map(([left, right]) => pack(left, right)),
product(SIZES, SIZES)
.filter(([outer, inner]) => outer > inner)
.flatMap(([outer, inner]) => [extract(outer, inner), replace(outer, inner)]),
format(
[].concat(
errors,
product(SIZES, SIZES)
.filter(([left, right]) => SIZES.includes(left + right))
.map(([left, right]) => pack(left, right)),
product(SIZES, SIZES)
.filter(([outer, inner]) => outer > inner)
.flatMap(([outer, inner]) => [extract(outer, inner), replace(outer, inner)]),
),
).trimEnd(),
'}',
);