Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Index cheatcode for Strings #7539

Merged
merged 5 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions crates/cheatcodes/assets/cheatcodes.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions crates/cheatcodes/spec/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1678,6 +1678,11 @@ interface Vm {
/// Splits the given `string` into an array of strings divided by the `delimiter`.
#[cheatcode(group = String)]
function split(string calldata input, string calldata delimiter) external pure returns (string[] memory outputs);
/// Returns the index of the first occurrence of a `key` in an `input` string.
/// Returns `NOT_FOUND` (i.e. `type(uint256).max`) if the `key` is not found.
kamuik16 marked this conversation as resolved.
Show resolved Hide resolved
/// Returns 0 in case of an empty `key`.
#[cheatcode(group = String)]
function indexOf(string memory input, string memory key) external pure returns (uint256);

// ======== JSON Parsing and Manipulation ========

Expand Down
9 changes: 9 additions & 0 deletions crates/cheatcodes/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use crate::{Cheatcode, Cheatcodes, Result, Vm::*};
use alloy_dyn_abi::{DynSolType, DynSolValue};
use alloy_primitives::U256;
use alloy_sol_types::SolValue;

// address
Expand Down Expand Up @@ -135,6 +136,14 @@ impl Cheatcode for splitCall {
}
}

// indexOf
impl Cheatcode for indexOfCall {
fn apply(&self, _state: &mut Cheatcodes) -> Result {
let Self { input, key } = self;
Ok(input.find(key).map(U256::from).unwrap_or(U256::MAX).abi_encode())
}
}

pub(super) fn parse(s: &str, ty: &DynSolType) -> Result {
parse_value(s, ty).map(|v| v.abi_encode())
}
Expand Down
1 change: 1 addition & 0 deletions testdata/cheats/Vm.sol

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions testdata/default/cheats/StringUtils.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,16 @@ contract StringManipulationTest is DSTest {
assertEq("World", splitResult[1]);
assertEq("Reth", splitResult[2]);
}

function testIndexOf() public {
DaniPopes marked this conversation as resolved.
Show resolved Hide resolved
string memory input = "Hello, World!";
string memory key1 = "Hello,";
string memory key2 = "World!";
string memory key3 = "";
string memory key4 = "foundry";
assertEq(vm.indexOf(input, key1), 0);
assertEq(vm.indexOf(input, key2), 7);
assertEq(vm.indexOf(input, key3), 0);
assertEq(vm.indexOf(input, key4), type(uint256).max);
}
}
Loading