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

native string utils cheatcodes #6891

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
16 changes: 16 additions & 0 deletions crates/cheatcodes/spec/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1077,6 +1077,22 @@ interface Vm {
#[cheatcode(group = String)]
function parseBool(string calldata stringifiedValue) external pure returns (bool parsedValue);

/// Converts the given `string` value to Lowercase.
#[cheatcode(group = String)]
function toLowercase(string calldata stringifiedValue) external pure returns (string memory stringifiedValue);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested in forge-std to confirm, and this will error with identifier already declared in solidity since the stringifiedValue name is reused. Suggested var names for all sigs:

Suggested change
function toLowercase(string calldata stringifiedValue) external pure returns (string memory stringifiedValue);
function toLowercase(string calldata input) external pure returns (string memory output);

/// Converts the given `string` value to Uppercase.
#[cheatcode(group = String)]
function toUppercase(string calldata stringifiedValue) external pure returns (string memory stringifiedValue);
/// Trims leading and trailing whitespace from the given `string` value.
#[cheatcode(group = String)]
function trim(string calldata stringifiedValue) external pure returns (string memory stringifiedValue);
/// Replaces occurrences of `from` in the given `string` with `to`.
#[cheatcode(group = String)]
function replace(string calldata stringifiedValue, string calldata from, string calldata to) external pure returns (string memory stringifiedValue);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as https://github.com/foundry-rs/foundry/pull/6891/files#r1465480622
These are not stringifed values, but just strings, so s or str is more suitable

/// Splits the given `string` into an array of strings divided by the `delimiter`.
#[cheatcode(group = String)]
function split(string calldata stringifiedValue, string calldata delimiter) external pure returns (string[] memory stringifiedValues);

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

// -------- Reading --------
Expand Down
41 changes: 41 additions & 0 deletions crates/cheatcodes/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,47 @@ impl Cheatcode for parseBoolCall {
}
}

// toLowercase
impl Cheatcode for toLowercaseCall {
fn apply(&self, _state: &mut Cheatcodes) -> Result {
let Self { stringifiedValue } = self;
Ok(stringifiedValue.to_lowercase().abi_encode())
}
}

// toUppercase
impl Cheatcode for toUppercaseCall {
fn apply(&self, _state: &mut Cheatcodes) -> Result {
let Self { stringifiedValue } = self;
Ok(stringifiedValue.to_uppercase().abi_encode())
}
}

// trim
impl Cheatcode for trimCall {
fn apply(&self, _state: &mut Cheatcodes) -> Result {
let Self { stringifiedValue } = self;
Ok(stringifiedValue.trim().abi_encode())
}
}

// Replace
impl Cheatcode for replaceCall {
fn apply(&self, _state: &mut Cheatcodes) -> Result {
let Self { stringifiedValue, from, to } = self;
Ok(stringifiedValue.replace(from, to).abi_encode())
}
}

// Split
impl Cheatcode for splitCall {
fn apply(&self, _state: &mut Cheatcodes) -> Result {
let Self { stringifiedValue, delimiter } = self;
let parts: Vec<&str> = stringifiedValue.split(delimiter).collect();
Ok(parts.abi_encode())
}
}

pub(super) fn parse(s: &str, ty: &DynSolType) -> Result {
parse_value(s, ty).map(|v| v.abi_encode())
}
Expand Down
Loading