Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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.

4 changes: 4 additions & 0 deletions crates/cheatcodes/spec/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1678,6 +1678,10 @@ 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.
Comment thread
kamuik16 marked this conversation as resolved.
#[cheatcode(group = String)]
function indexOf(string memory input, string memory key) external pure returns (uint256);
Comment thread
kamuik16 marked this conversation as resolved.

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

Expand Down
14 changes: 14 additions & 0 deletions crates/cheatcodes/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,20 @@ impl Cheatcode for splitCall {
}
}

// indexOf
impl Cheatcode for indexOfCall {
fn apply(&self, _state: &mut Cheatcodes) -> Result {
let Self { input, key } = self;
let Some(index) = input.find(key) else {
return parse(
"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
&DynSolType::Uint(256),
);
Comment thread
kamuik16 marked this conversation as resolved.
Outdated
};
parse(index.to_string().as_str(), &DynSolType::Uint(256))
}
}

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 {
Comment thread
DaniPopes marked this conversation as 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), 12);
assertEq(vm.indexOf(input, key4), type(uint256).max);
}
}