Skip to content

Commit

Permalink
Feat: Index cheatcode for Strings (#7539)
Browse files Browse the repository at this point in the history
* feat: index cheatcode

* some nits to make it work

* nit: use as_str()

* final changes

* chore: reviewed changes
  • Loading branch information
kamuik16 authored Apr 4, 2024
1 parent 1631c5c commit 1281421
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 0 deletions.
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 @@ -1680,6 +1680,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.
/// 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 {
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);
}
}

0 comments on commit 1281421

Please sign in to comment.