Add a Calldata library with `emptyBytes` and `emptyString` functions (#5422)
Co-authored-by: Ernesto García <ernestognw@gmail.com>pull/5427/head
parent
7b74442c5e
commit
352ab13687
@ -0,0 +1,5 @@ |
||||
--- |
||||
"openzeppelin-solidity": minor |
||||
--- |
||||
|
||||
`Calldata`: Library with `emptyBytes` and `emptyString` functions to generate empty `bytes` and `string` calldata types. |
@ -0,0 +1,24 @@ |
||||
// SPDX-License-Identifier: MIT |
||||
|
||||
pragma solidity ^0.8.20; |
||||
|
||||
/** |
||||
* @dev Helper library for manipulating objects in calldata. |
||||
*/ |
||||
library Calldata { |
||||
// slither-disable-next-line write-after-write |
||||
function emptyBytes() internal pure returns (bytes calldata result) { |
||||
assembly ("memory-safe") { |
||||
result.offset := 0 |
||||
result.length := 0 |
||||
} |
||||
} |
||||
|
||||
// slither-disable-next-line write-after-write |
||||
function emptyString() internal pure returns (string calldata result) { |
||||
assembly ("memory-safe") { |
||||
result.offset := 0 |
||||
result.length := 0 |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,22 @@ |
||||
const { ethers } = require('hardhat'); |
||||
const { expect } = require('chai'); |
||||
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers'); |
||||
|
||||
async function fixture() { |
||||
const mock = await ethers.deployContract('$Calldata'); |
||||
return { mock }; |
||||
} |
||||
|
||||
describe('Calldata utilities', function () { |
||||
beforeEach(async function () { |
||||
Object.assign(this, await loadFixture(fixture)); |
||||
}); |
||||
|
||||
it('emptyBytes', async function () { |
||||
await expect(this.mock.$emptyBytes()).to.eventually.equal('0x'); |
||||
}); |
||||
|
||||
it('emptyString', async function () { |
||||
await expect(this.mock.$emptyString()).to.eventually.equal(''); |
||||
}); |
||||
}); |
Loading…
Reference in new issue