From dccaf5b741b2feb4cc4c7e40d3f350c0a2f5c666 Mon Sep 17 00:00:00 2001 From: 1010adigupta <1010adigupta@gmail.com> Date: Wed, 24 Jan 2024 21:24:27 +0530 Subject: [PATCH 1/7] string utils cheatcodes created --- crates/cheatcodes/spec/src/vm.rs | 16 ++++++++++++ crates/cheatcodes/src/string.rs | 42 ++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/crates/cheatcodes/spec/src/vm.rs b/crates/cheatcodes/spec/src/vm.rs index 8daca4ef31d2..b57f31670ffc 100644 --- a/crates/cheatcodes/spec/src/vm.rs +++ b/crates/cheatcodes/spec/src/vm.rs @@ -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); + /// 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); + /// 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 -------- diff --git a/crates/cheatcodes/src/string.rs b/crates/cheatcodes/src/string.rs index f55350592ea1..eeaee05e6974 100644 --- a/crates/cheatcodes/src/string.rs +++ b/crates/cheatcodes/src/string.rs @@ -94,6 +94,48 @@ 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()) } From 4c785d138e22358dee495fa3de132798b38dad1d Mon Sep 17 00:00:00 2001 From: 1010adigupta <1010adigupta@gmail.com> Date: Wed, 24 Jan 2024 21:41:17 +0530 Subject: [PATCH 2/7] fmt: run rustfmt --- crates/cheatcodes/src/string.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/crates/cheatcodes/src/string.rs b/crates/cheatcodes/src/string.rs index eeaee05e6974..a154d25eca3d 100644 --- a/crates/cheatcodes/src/string.rs +++ b/crates/cheatcodes/src/string.rs @@ -135,7 +135,6 @@ impl Cheatcode for splitCall { } } - pub(super) fn parse(s: &str, ty: &DynSolType) -> Result { parse_value(s, ty).map(|v| v.abi_encode()) } From 304e175c332f72a7ddecec0b6aa9cdf891cddc07 Mon Sep 17 00:00:00 2001 From: 1010adigupta <1010adigupta@gmail.com> Date: Thu, 25 Jan 2024 00:43:08 +0530 Subject: [PATCH 3/7] tests: solidity tests added --- testdata/cheats/StringUtils.t.sol | 42 +++++++++++++++++++++++++++++++ testdata/cheats/Vm.sol | 5 ++++ 2 files changed, 47 insertions(+) create mode 100644 testdata/cheats/StringUtils.t.sol diff --git a/testdata/cheats/StringUtils.t.sol b/testdata/cheats/StringUtils.t.sol new file mode 100644 index 000000000000..4fe8bba01447 --- /dev/null +++ b/testdata/cheats/StringUtils.t.sol @@ -0,0 +1,42 @@ +// SPDX-License-Identifier: MIT OR Apache-2.0 +pragma solidity 0.8.18; + +import "ds-test/test.sol"; +import "./Vm.sol"; + +contract StringManipulationTest is DSTest { + Vm constant vm = Vm(HEVM_ADDRESS); + + function testToLowercase() public { + string memory original = "Hello World"; + string memory lowercased = vm.toLowercase(original); + assertEq("hello world", lowercased); + } + + function testToUppercase() public { + string memory original = "Hello World"; + string memory uppercased = vm.toUppercase(original); + assertEq("HELLO WORLD", uppercased); + } + + function testTrim() public { + string memory original = " Hello World "; + string memory trimmed = vm.trim(original); + assertEq("Hello World", trimmed); + } + + function testReplace() public { + string memory original = "Hello World"; + string memory replaced = vm.replace(original, "World", "Reth"); + assertEq("Hello Reth", replaced); + } + + function testSplit() public { + string memory original = "Hello,World,Reth"; + string[] memory splitResult = vm.split(original, ","); + assertEq(3, splitResult.length); + assertEq("Hello", splitResult[0]); + assertEq("World", splitResult[1]); + assertEq("Reth", splitResult[2]); + } +} diff --git a/testdata/cheats/Vm.sol b/testdata/cheats/Vm.sol index 08c8f570bd3e..772096e8cc2d 100644 --- a/testdata/cheats/Vm.sol +++ b/testdata/cheats/Vm.sol @@ -239,6 +239,11 @@ interface Vm { function toString(bool value) external pure returns (string memory stringifiedValue); function toString(uint256 value) external pure returns (string memory stringifiedValue); function toString(int256 value) external pure returns (string memory stringifiedValue); + function toLowercase(string calldata value) external pure returns (string memory stringifiedValue); + function toUppercase(string calldata value) external pure returns (string memory stringifiedValue); + function trim(string calldata value) external pure returns (string memory stringifiedValue); + function replace(string calldata value, string calldata from, string calldata to) external pure returns (string memory stringifiedValue); + function split(string calldata value, string calldata delimiter) external pure returns (string[] memory stringifiedValues); function transact(bytes32 txHash) external; function transact(uint256 forkId, bytes32 txHash) external; function tryFfi(string[] calldata commandInput) external returns (FfiResult memory result); From 7fe9a27f5a4c8a32c976c7b0d6e4c894ef3f32ae Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Wed, 24 Jan 2024 20:56:49 +0100 Subject: [PATCH 4/7] cargo cheats --- crates/cheatcodes/assets/cheatcodes.json | 100 +++++++++++++++++++++++ testdata/cheats/Vm.sol | 10 +-- 2 files changed, 105 insertions(+), 5 deletions(-) diff --git a/crates/cheatcodes/assets/cheatcodes.json b/crates/cheatcodes/assets/cheatcodes.json index 3225893d14c3..e8e2484551bd 100644 --- a/crates/cheatcodes/assets/cheatcodes.json +++ b/crates/cheatcodes/assets/cheatcodes.json @@ -3593,6 +3593,26 @@ "status": "stable", "safety": "safe" }, + { + "func": { + "id": "replace", + "description": "Replaces occurrences of `from` in the given `string` with `to`.", + "declaration": "function replace(string calldata stringifiedValue, string calldata from, string calldata to) external pure returns (string memory stringifiedValue);", + "visibility": "external", + "mutability": "pure", + "signature": "replace(string,string,string)", + "selector": "0xe00ad03e", + "selectorBytes": [ + 224, + 10, + 208, + 62 + ] + }, + "group": "string", + "status": "stable", + "safety": "safe" + }, { "func": { "id": "resetNonce", @@ -4393,6 +4413,26 @@ "status": "stable", "safety": "unsafe" }, + { + "func": { + "id": "split", + "description": "Splits the given `string` into an array of strings divided by the `delimiter`.", + "declaration": "function split(string calldata stringifiedValue, string calldata delimiter) external pure returns (string[] memory stringifiedValues);", + "visibility": "external", + "mutability": "pure", + "signature": "split(string,string)", + "selector": "0x8bb75533", + "selectorBytes": [ + 139, + 183, + 85, + 51 + ] + }, + "group": "string", + "status": "stable", + "safety": "safe" + }, { "func": { "id": "startBroadcast_0", @@ -4713,6 +4753,26 @@ "status": "stable", "safety": "safe" }, + { + "func": { + "id": "toLowercase", + "description": "Converts the given `string` value to Lowercase.", + "declaration": "function toLowercase(string calldata stringifiedValue) external pure returns (string memory stringifiedValue);", + "visibility": "external", + "mutability": "pure", + "signature": "toLowercase(string)", + "selector": "0x50bb0884", + "selectorBytes": [ + 80, + 187, + 8, + 132 + ] + }, + "group": "string", + "status": "stable", + "safety": "safe" + }, { "func": { "id": "toString_0", @@ -4833,6 +4893,26 @@ "status": "stable", "safety": "safe" }, + { + "func": { + "id": "toUppercase", + "description": "Converts the given `string` value to Uppercase.", + "declaration": "function toUppercase(string calldata stringifiedValue) external pure returns (string memory stringifiedValue);", + "visibility": "external", + "mutability": "pure", + "signature": "toUppercase(string)", + "selector": "0x074ae3d7", + "selectorBytes": [ + 7, + 74, + 227, + 215 + ] + }, + "group": "string", + "status": "stable", + "safety": "safe" + }, { "func": { "id": "transact_0", @@ -4873,6 +4953,26 @@ "status": "stable", "safety": "unsafe" }, + { + "func": { + "id": "trim", + "description": "Trims leading and trailing whitespace from the given `string` value.", + "declaration": "function trim(string calldata stringifiedValue) external pure returns (string memory stringifiedValue);", + "visibility": "external", + "mutability": "pure", + "signature": "trim(string)", + "selector": "0xb2dad155", + "selectorBytes": [ + 178, + 218, + 209, + 85 + ] + }, + "group": "string", + "status": "stable", + "safety": "safe" + }, { "func": { "id": "tryFfi", diff --git a/testdata/cheats/Vm.sol b/testdata/cheats/Vm.sol index 772096e8cc2d..1a9c5fd0f8c2 100644 --- a/testdata/cheats/Vm.sol +++ b/testdata/cheats/Vm.sol @@ -177,6 +177,7 @@ interface Vm { function rememberKey(uint256 privateKey) external returns (address keyAddr); function removeDir(string calldata path, bool recursive) external; function removeFile(string calldata path) external; + function replace(string calldata stringifiedValue, string calldata from, string calldata to) external pure returns (string memory stringifiedValue); function resetNonce(address account) external; function resumeGasMetering() external; function revertTo(uint256 snapshotId) external returns (bool success); @@ -217,6 +218,7 @@ interface Vm { function skip(bool skipTest) external; function sleep(uint256 duration) external; function snapshot() external returns (uint256 snapshotId); + function split(string calldata stringifiedValue, string calldata delimiter) external pure returns (string[] memory stringifiedValues); function startBroadcast() external; function startBroadcast(address signer) external; function startBroadcast(uint256 privateKey) external; @@ -233,19 +235,17 @@ interface Vm { function toBase64URL(string calldata data) external pure returns (string memory); function toBase64(bytes calldata data) external pure returns (string memory); function toBase64(string calldata data) external pure returns (string memory); + function toLowercase(string calldata stringifiedValue) external pure returns (string memory stringifiedValue); function toString(address value) external pure returns (string memory stringifiedValue); function toString(bytes calldata value) external pure returns (string memory stringifiedValue); function toString(bytes32 value) external pure returns (string memory stringifiedValue); function toString(bool value) external pure returns (string memory stringifiedValue); function toString(uint256 value) external pure returns (string memory stringifiedValue); function toString(int256 value) external pure returns (string memory stringifiedValue); - function toLowercase(string calldata value) external pure returns (string memory stringifiedValue); - function toUppercase(string calldata value) external pure returns (string memory stringifiedValue); - function trim(string calldata value) external pure returns (string memory stringifiedValue); - function replace(string calldata value, string calldata from, string calldata to) external pure returns (string memory stringifiedValue); - function split(string calldata value, string calldata delimiter) external pure returns (string[] memory stringifiedValues); + function toUppercase(string calldata stringifiedValue) external pure returns (string memory stringifiedValue); function transact(bytes32 txHash) external; function transact(uint256 forkId, bytes32 txHash) external; + function trim(string calldata stringifiedValue) external pure returns (string memory stringifiedValue); function tryFfi(string[] calldata commandInput) external returns (FfiResult memory result); function txGasPrice(uint256 newGasPrice) external; function unixTime() external returns (uint256 milliseconds); From ae26a18be350e4ff83ea14bc06151e54936b7650 Mon Sep 17 00:00:00 2001 From: 1010adigupta <1010adigupta@gmail.com> Date: Thu, 25 Jan 2024 05:22:18 +0530 Subject: [PATCH 5/7] update --- crates/cheatcodes/spec/src/vm.rs | 10 +++++----- crates/cheatcodes/src/string.rs | 20 ++++++++++---------- testdata/cheats/Vm.sol | 10 +++++----- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/crates/cheatcodes/spec/src/vm.rs b/crates/cheatcodes/spec/src/vm.rs index b57f31670ffc..8d8b16328825 100644 --- a/crates/cheatcodes/spec/src/vm.rs +++ b/crates/cheatcodes/spec/src/vm.rs @@ -1079,19 +1079,19 @@ interface Vm { /// Converts the given `string` value to Lowercase. #[cheatcode(group = String)] - function toLowercase(string calldata stringifiedValue) external pure returns (string memory stringifiedValue); + function toLowercase(string calldata inputString) external pure returns (string memory outputString); /// Converts the given `string` value to Uppercase. #[cheatcode(group = String)] - function toUppercase(string calldata stringifiedValue) external pure returns (string memory stringifiedValue); + function toUppercase(string calldata inputString) external pure returns (string memory outputString); /// Trims leading and trailing whitespace from the given `string` value. #[cheatcode(group = String)] - function trim(string calldata stringifiedValue) external pure returns (string memory stringifiedValue); + function trim(string calldata inputString) external pure returns (string memory outputString); /// 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); + function replace(string calldata inputString, string calldata from, string calldata to) external pure returns (string memory outputString); /// 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); + function split(string calldata inputString, string calldata delimiter) external pure returns (string[] memory outputStrings); // ======== JSON Parsing and Manipulation ======== diff --git a/crates/cheatcodes/src/string.rs b/crates/cheatcodes/src/string.rs index a154d25eca3d..df21056d6f6e 100644 --- a/crates/cheatcodes/src/string.rs +++ b/crates/cheatcodes/src/string.rs @@ -97,40 +97,40 @@ 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()) + let Self { inputString } = self; + Ok(inputString.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()) + let Self { inputString } = self; + Ok(inputString.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()) + let Self { inputString } = self; + Ok(inputString.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()) + let Self { inputString, from, to } = self; + Ok(inputString.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(); + let Self { inputString, delimiter } = self; + let parts: Vec<&str> = inputString.split(delimiter).collect(); Ok(parts.abi_encode()) } } diff --git a/testdata/cheats/Vm.sol b/testdata/cheats/Vm.sol index 1a9c5fd0f8c2..48bf1f961e2e 100644 --- a/testdata/cheats/Vm.sol +++ b/testdata/cheats/Vm.sol @@ -177,7 +177,7 @@ interface Vm { function rememberKey(uint256 privateKey) external returns (address keyAddr); function removeDir(string calldata path, bool recursive) external; function removeFile(string calldata path) external; - function replace(string calldata stringifiedValue, string calldata from, string calldata to) external pure returns (string memory stringifiedValue); + function replace(string calldata inputString, string calldata from, string calldata to) external pure returns (string memory outputString); function resetNonce(address account) external; function resumeGasMetering() external; function revertTo(uint256 snapshotId) external returns (bool success); @@ -218,7 +218,8 @@ interface Vm { function skip(bool skipTest) external; function sleep(uint256 duration) external; function snapshot() external returns (uint256 snapshotId); - function split(string calldata stringifiedValue, string calldata delimiter) external pure returns (string[] memory stringifiedValues); + function split(string calldata inputString, string calldata delimiter) external pure returns (string[] memory outputStrings); + function toUppercase(string calldata inputString) external pure returns (string memory outputString); function startBroadcast() external; function startBroadcast(address signer) external; function startBroadcast(uint256 privateKey) external; @@ -235,17 +236,16 @@ interface Vm { function toBase64URL(string calldata data) external pure returns (string memory); function toBase64(bytes calldata data) external pure returns (string memory); function toBase64(string calldata data) external pure returns (string memory); - function toLowercase(string calldata stringifiedValue) external pure returns (string memory stringifiedValue); + function toLowercase(string calldata inputString) external pure returns (string memory outputString); function toString(address value) external pure returns (string memory stringifiedValue); function toString(bytes calldata value) external pure returns (string memory stringifiedValue); function toString(bytes32 value) external pure returns (string memory stringifiedValue); function toString(bool value) external pure returns (string memory stringifiedValue); function toString(uint256 value) external pure returns (string memory stringifiedValue); function toString(int256 value) external pure returns (string memory stringifiedValue); - function toUppercase(string calldata stringifiedValue) external pure returns (string memory stringifiedValue); function transact(bytes32 txHash) external; function transact(uint256 forkId, bytes32 txHash) external; - function trim(string calldata stringifiedValue) external pure returns (string memory stringifiedValue); + function trim(string calldata inputString) external pure returns (string memory outputString); function tryFfi(string[] calldata commandInput) external returns (FfiResult memory result); function txGasPrice(uint256 newGasPrice) external; function unixTime() external returns (uint256 milliseconds); From 4589f84a5be857a70e058151dfe89f900e590172 Mon Sep 17 00:00:00 2001 From: 1010adigupta <1010adigupta@gmail.com> Date: Thu, 25 Jan 2024 05:59:01 +0530 Subject: [PATCH 6/7] update --- crates/cheatcodes/spec/src/vm.rs | 10 ++--- crates/cheatcodes/src/string.rs | 65 +++++++++++++++++--------------- testdata/cheats/Vm.sol | 10 ++--- 3 files changed, 44 insertions(+), 41 deletions(-) diff --git a/crates/cheatcodes/spec/src/vm.rs b/crates/cheatcodes/spec/src/vm.rs index 8d8b16328825..3433de846f39 100644 --- a/crates/cheatcodes/spec/src/vm.rs +++ b/crates/cheatcodes/spec/src/vm.rs @@ -1079,19 +1079,19 @@ interface Vm { /// Converts the given `string` value to Lowercase. #[cheatcode(group = String)] - function toLowercase(string calldata inputString) external pure returns (string memory outputString); + 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 inputString) external pure returns (string memory outputString); + function toUppercase(string calldata input) external pure returns (string memory output); /// Trims leading and trailing whitespace from the given `string` value. #[cheatcode(group = String)] - function trim(string calldata inputString) external pure returns (string memory outputString); + function trim(string calldata input) external pure returns (string memory output); /// Replaces occurrences of `from` in the given `string` with `to`. #[cheatcode(group = String)] - function replace(string calldata inputString, string calldata from, string calldata to) external pure returns (string memory outputString); + function replace(string calldata input, string calldata from, string calldata to) external pure returns (string memory output); /// Splits the given `string` into an array of strings divided by the `delimiter`. #[cheatcode(group = String)] - function split(string calldata inputString, string calldata delimiter) external pure returns (string[] memory outputStrings); + function split(string calldata input, string calldata delimiter) external pure returns (string[] memory outputs); // ======== JSON Parsing and Manipulation ======== diff --git a/crates/cheatcodes/src/string.rs b/crates/cheatcodes/src/string.rs index df21056d6f6e..bfb735d8af73 100644 --- a/crates/cheatcodes/src/string.rs +++ b/crates/cheatcodes/src/string.rs @@ -1,7 +1,7 @@ //! Implementations of [`String`](crate::Group::String) cheatcodes. -use crate::{Cheatcode, Cheatcodes, Result, Vm::*}; -use alloy_dyn_abi::{DynSolType, DynSolValue}; +use crate::{ Cheatcode, Cheatcodes, Result, Vm::* }; +use alloy_dyn_abi::{ DynSolType, DynSolValue }; use alloy_sol_types::SolValue; // address @@ -97,40 +97,40 @@ impl Cheatcode for parseBoolCall { // toLowercase impl Cheatcode for toLowercaseCall { fn apply(&self, _state: &mut Cheatcodes) -> Result { - let Self { inputString } = self; - Ok(inputString.to_lowercase().abi_encode()) + let Self { input } = self; + Ok(input.to_lowercase().abi_encode()) } } // toUppercase impl Cheatcode for toUppercaseCall { fn apply(&self, _state: &mut Cheatcodes) -> Result { - let Self { inputString } = self; - Ok(inputString.to_uppercase().abi_encode()) + let Self { input } = self; + Ok(input.to_uppercase().abi_encode()) } } // trim impl Cheatcode for trimCall { fn apply(&self, _state: &mut Cheatcodes) -> Result { - let Self { inputString } = self; - Ok(inputString.trim().abi_encode()) + let Self { input } = self; + Ok(input.trim().abi_encode()) } } // Replace impl Cheatcode for replaceCall { fn apply(&self, _state: &mut Cheatcodes) -> Result { - let Self { inputString, from, to } = self; - Ok(inputString.replace(from, to).abi_encode()) + let Self { input, from, to } = self; + Ok(input.replace(from, to).abi_encode()) } } // Split impl Cheatcode for splitCall { fn apply(&self, _state: &mut Cheatcodes) -> Result { - let Self { inputString, delimiter } = self; - let parts: Vec<&str> = inputString.split(delimiter).collect(); + let Self { input, delimiter } = self; + let parts: Vec<&str> = input.split(delimiter).collect(); Ok(parts.abi_encode()) } } @@ -140,17 +140,17 @@ pub(super) fn parse(s: &str, ty: &DynSolType) -> Result { } pub(super) fn parse_array(values: I, ty: &DynSolType) -> Result -where - I: IntoIterator, - S: AsRef, + where I: IntoIterator, S: AsRef { let mut values = values.into_iter(); match values.next() { - Some(first) if !first.as_ref().is_empty() => std::iter::once(first) - .chain(values) - .map(|s| parse_value(s.as_ref(), ty)) - .collect::, _>>() - .map(|vec| DynSolValue::Array(vec).abi_encode()), + Some(first) if !first.as_ref().is_empty() => + std::iter + ::once(first) + .chain(values) + .map(|s| parse_value(s.as_ref(), ty)) + .collect::, _>>() + .map(|vec| DynSolValue::Array(vec).abi_encode()), // return the empty encoded Bytes when values is empty or the first element is empty _ => Ok("".abi_encode()), } @@ -160,11 +160,12 @@ where fn parse_value(s: &str, ty: &DynSolType) -> Result { match ty.coerce_str(s) { Ok(value) => Ok(value), - Err(e) => match parse_value_fallback(s, ty) { - Some(Ok(value)) => Ok(value), - Some(Err(e2)) => Err(fmt_err!("failed parsing {s:?} as type `{ty}`: {e2}")), - None => Err(fmt_err!("failed parsing {s:?} as type `{ty}`: {e}")), - }, + Err(e) => + match parse_value_fallback(s, ty) { + Some(Ok(value)) => Ok(value), + Some(Err(e2)) => Err(fmt_err!("failed parsing {s:?} as type `{ty}`: {e2}")), + None => Err(fmt_err!("failed parsing {s:?} as type `{ty}`: {e}")), + } } } @@ -177,16 +178,18 @@ fn parse_value_fallback(s: &str, ty: &DynSolType) -> Option false, s if s.eq_ignore_ascii_case("true") => true, s if s.eq_ignore_ascii_case("false") => false, - _ => return None, + _ => { + return None; + } }; return Some(Ok(DynSolValue::Bool(b))); } - DynSolType::Int(_) | - DynSolType::Uint(_) | - DynSolType::FixedBytes(_) | - DynSolType::Bytes => { + | DynSolType::Int(_) + | DynSolType::Uint(_) + | DynSolType::FixedBytes(_) + | DynSolType::Bytes => { if !s.starts_with("0x") && s.chars().all(|c| c.is_ascii_hexdigit()) { - return Some(Err("missing hex prefix (\"0x\") for hex string")) + return Some(Err("missing hex prefix (\"0x\") for hex string")); } } _ => {} diff --git a/testdata/cheats/Vm.sol b/testdata/cheats/Vm.sol index 48bf1f961e2e..1059722b1145 100644 --- a/testdata/cheats/Vm.sol +++ b/testdata/cheats/Vm.sol @@ -177,7 +177,7 @@ interface Vm { function rememberKey(uint256 privateKey) external returns (address keyAddr); function removeDir(string calldata path, bool recursive) external; function removeFile(string calldata path) external; - function replace(string calldata inputString, string calldata from, string calldata to) external pure returns (string memory outputString); + function replace(string calldata input, string calldata from, string calldata to) external pure returns (string memory output); function resetNonce(address account) external; function resumeGasMetering() external; function revertTo(uint256 snapshotId) external returns (bool success); @@ -218,8 +218,8 @@ interface Vm { function skip(bool skipTest) external; function sleep(uint256 duration) external; function snapshot() external returns (uint256 snapshotId); - function split(string calldata inputString, string calldata delimiter) external pure returns (string[] memory outputStrings); - function toUppercase(string calldata inputString) external pure returns (string memory outputString); + function split(string calldata input, string calldata delimiter) external pure returns (string[] memory outputs); + function toUppercase(string calldata input) external pure returns (string memory output); function startBroadcast() external; function startBroadcast(address signer) external; function startBroadcast(uint256 privateKey) external; @@ -236,7 +236,7 @@ interface Vm { function toBase64URL(string calldata data) external pure returns (string memory); function toBase64(bytes calldata data) external pure returns (string memory); function toBase64(string calldata data) external pure returns (string memory); - function toLowercase(string calldata inputString) external pure returns (string memory outputString); + function toLowercase(string calldata input) external pure returns (string memory output); function toString(address value) external pure returns (string memory stringifiedValue); function toString(bytes calldata value) external pure returns (string memory stringifiedValue); function toString(bytes32 value) external pure returns (string memory stringifiedValue); @@ -245,7 +245,7 @@ interface Vm { function toString(int256 value) external pure returns (string memory stringifiedValue); function transact(bytes32 txHash) external; function transact(uint256 forkId, bytes32 txHash) external; - function trim(string calldata inputString) external pure returns (string memory outputString); + function trim(string calldata input) external pure returns (string memory output); function tryFfi(string[] calldata commandInput) external returns (FfiResult memory result); function txGasPrice(uint256 newGasPrice) external; function unixTime() external returns (uint256 milliseconds); From 59a15ff4c702783b00355fd91ddc77be9da61d75 Mon Sep 17 00:00:00 2001 From: Enrique Ortiz Date: Wed, 24 Jan 2024 21:47:46 -0400 Subject: [PATCH 7/7] chore: update defs --- crates/cheatcodes/assets/cheatcodes.json | 10 +++--- crates/cheatcodes/src/string.rs | 39 ++++++++++++------------ testdata/cheats/Vm.sol | 2 +- 3 files changed, 25 insertions(+), 26 deletions(-) diff --git a/crates/cheatcodes/assets/cheatcodes.json b/crates/cheatcodes/assets/cheatcodes.json index e8e2484551bd..d6c22a37e3ea 100644 --- a/crates/cheatcodes/assets/cheatcodes.json +++ b/crates/cheatcodes/assets/cheatcodes.json @@ -3597,7 +3597,7 @@ "func": { "id": "replace", "description": "Replaces occurrences of `from` in the given `string` with `to`.", - "declaration": "function replace(string calldata stringifiedValue, string calldata from, string calldata to) external pure returns (string memory stringifiedValue);", + "declaration": "function replace(string calldata input, string calldata from, string calldata to) external pure returns (string memory output);", "visibility": "external", "mutability": "pure", "signature": "replace(string,string,string)", @@ -4417,7 +4417,7 @@ "func": { "id": "split", "description": "Splits the given `string` into an array of strings divided by the `delimiter`.", - "declaration": "function split(string calldata stringifiedValue, string calldata delimiter) external pure returns (string[] memory stringifiedValues);", + "declaration": "function split(string calldata input, string calldata delimiter) external pure returns (string[] memory outputs);", "visibility": "external", "mutability": "pure", "signature": "split(string,string)", @@ -4757,7 +4757,7 @@ "func": { "id": "toLowercase", "description": "Converts the given `string` value to Lowercase.", - "declaration": "function toLowercase(string calldata stringifiedValue) external pure returns (string memory stringifiedValue);", + "declaration": "function toLowercase(string calldata input) external pure returns (string memory output);", "visibility": "external", "mutability": "pure", "signature": "toLowercase(string)", @@ -4897,7 +4897,7 @@ "func": { "id": "toUppercase", "description": "Converts the given `string` value to Uppercase.", - "declaration": "function toUppercase(string calldata stringifiedValue) external pure returns (string memory stringifiedValue);", + "declaration": "function toUppercase(string calldata input) external pure returns (string memory output);", "visibility": "external", "mutability": "pure", "signature": "toUppercase(string)", @@ -4957,7 +4957,7 @@ "func": { "id": "trim", "description": "Trims leading and trailing whitespace from the given `string` value.", - "declaration": "function trim(string calldata stringifiedValue) external pure returns (string memory stringifiedValue);", + "declaration": "function trim(string calldata input) external pure returns (string memory output);", "visibility": "external", "mutability": "pure", "signature": "trim(string)", diff --git a/crates/cheatcodes/src/string.rs b/crates/cheatcodes/src/string.rs index bfb735d8af73..b7992b9450fd 100644 --- a/crates/cheatcodes/src/string.rs +++ b/crates/cheatcodes/src/string.rs @@ -1,7 +1,7 @@ //! Implementations of [`String`](crate::Group::String) cheatcodes. -use crate::{ Cheatcode, Cheatcodes, Result, Vm::* }; -use alloy_dyn_abi::{ DynSolType, DynSolValue }; +use crate::{Cheatcode, Cheatcodes, Result, Vm::*}; +use alloy_dyn_abi::{DynSolType, DynSolValue}; use alloy_sol_types::SolValue; // address @@ -140,17 +140,17 @@ pub(super) fn parse(s: &str, ty: &DynSolType) -> Result { } pub(super) fn parse_array(values: I, ty: &DynSolType) -> Result - where I: IntoIterator, S: AsRef +where + I: IntoIterator, + S: AsRef, { let mut values = values.into_iter(); match values.next() { - Some(first) if !first.as_ref().is_empty() => - std::iter - ::once(first) - .chain(values) - .map(|s| parse_value(s.as_ref(), ty)) - .collect::, _>>() - .map(|vec| DynSolValue::Array(vec).abi_encode()), + Some(first) if !first.as_ref().is_empty() => std::iter::once(first) + .chain(values) + .map(|s| parse_value(s.as_ref(), ty)) + .collect::, _>>() + .map(|vec| DynSolValue::Array(vec).abi_encode()), // return the empty encoded Bytes when values is empty or the first element is empty _ => Ok("".abi_encode()), } @@ -160,12 +160,11 @@ pub(super) fn parse_array(values: I, ty: &DynSolType) -> Result fn parse_value(s: &str, ty: &DynSolType) -> Result { match ty.coerce_str(s) { Ok(value) => Ok(value), - Err(e) => - match parse_value_fallback(s, ty) { - Some(Ok(value)) => Ok(value), - Some(Err(e2)) => Err(fmt_err!("failed parsing {s:?} as type `{ty}`: {e2}")), - None => Err(fmt_err!("failed parsing {s:?} as type `{ty}`: {e}")), - } + Err(e) => match parse_value_fallback(s, ty) { + Some(Ok(value)) => Ok(value), + Some(Err(e2)) => Err(fmt_err!("failed parsing {s:?} as type `{ty}`: {e2}")), + None => Err(fmt_err!("failed parsing {s:?} as type `{ty}`: {e}")), + }, } } @@ -184,10 +183,10 @@ fn parse_value_fallback(s: &str, ty: &DynSolType) -> Option { + DynSolType::Int(_) | + DynSolType::Uint(_) | + DynSolType::FixedBytes(_) | + DynSolType::Bytes => { if !s.starts_with("0x") && s.chars().all(|c| c.is_ascii_hexdigit()) { return Some(Err("missing hex prefix (\"0x\") for hex string")); } diff --git a/testdata/cheats/Vm.sol b/testdata/cheats/Vm.sol index 1059722b1145..a06e087d8788 100644 --- a/testdata/cheats/Vm.sol +++ b/testdata/cheats/Vm.sol @@ -219,7 +219,6 @@ interface Vm { function sleep(uint256 duration) external; function snapshot() external returns (uint256 snapshotId); function split(string calldata input, string calldata delimiter) external pure returns (string[] memory outputs); - function toUppercase(string calldata input) external pure returns (string memory output); function startBroadcast() external; function startBroadcast(address signer) external; function startBroadcast(uint256 privateKey) external; @@ -243,6 +242,7 @@ interface Vm { function toString(bool value) external pure returns (string memory stringifiedValue); function toString(uint256 value) external pure returns (string memory stringifiedValue); function toString(int256 value) external pure returns (string memory stringifiedValue); + function toUppercase(string calldata input) external pure returns (string memory output); function transact(bytes32 txHash) external; function transact(uint256 forkId, bytes32 txHash) external; function trim(string calldata input) external pure returns (string memory output);