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

Feature request: String.format with specifiers #5002

Open
PaulRBerg opened this issue May 22, 2023 · 11 comments
Open

Feature request: String.format with specifiers #5002

PaulRBerg opened this issue May 22, 2023 · 11 comments
Assignees
Labels
A-cheatcodes Area: cheatcodes C-forge Command: forge first issue A good way to start contributing T-feature Type: feature

Comments

@PaulRBerg
Copy link
Contributor

Console logging supports format specifiers like %s and %d.

It would be helpful to have something similar for formatting strings, e.g. equivalent to Java's String.format.

As per #2313, I gather that the format specifier logic is implemented in Rust, not Solidity. But I went ahead and created this issue because I think it's a highly desirable feature, especially for use cases like NFT SVGs.

@mds1
Copy link
Collaborator

mds1 commented May 22, 2023

IIUC this seems like a cheatcode that should be implemented in foundry, so transferring this issue, but let me know if I misunderstood

@mds1 mds1 transferred this issue from foundry-rs/forge-std May 22, 2023
@gakonst gakonst added this to Foundry May 22, 2023
@github-project-automation github-project-automation bot moved this to Todo in Foundry May 22, 2023
@PaulRBerg
Copy link
Contributor Author

Nah, I was referring to a pure Solidity implementation of format specifiers, meant to be used in production.

Of course, I imagine that this would be difficult to implement.

@mds1
Copy link
Collaborator

mds1 commented May 23, 2023

Production-level solidity string formatting is out of scope for forge-std since it's a development-focused repo. However, solady is being audited soon and it has a lot of string manipulation utils, so you may be able to get something like this in there then have it audited. I'll leave this open in the foundry repo since this still seems like it could be useful as a cheatcode

@zerosnacks
Copy link
Member

Related: #6891

Could be extended to support format

@zerosnacks zerosnacks added T-feature Type: feature A-cheatcodes Area: cheatcodes C-forge Command: forge labels Jul 2, 2024
@zerosnacks zerosnacks added this to the v1.0.0 milestone Jul 26, 2024
@grandizzy grandizzy added the first issue A good way to start contributing label Oct 10, 2024
@grandizzy grandizzy removed this from the v1.0.0 milestone Oct 10, 2024
@leovct
Copy link
Contributor

leovct commented Nov 5, 2024

Is this still needed? If so, I can work on that.

cc @grandizzy @zerosnacks

@grandizzy
Copy link
Collaborator

@leovct yes, that would be a nice addition

@leovct
Copy link
Contributor

leovct commented Nov 25, 2024

I know it's been a while, I was quite busy at work. I implemented a first tiny version, can you guys check if this is what you're expecting? cc @grandizzy @zerosnacks

@zerosnacks
Copy link
Member

Hey @leovct, not a problem! I think this is in line with what @mds1 is proposing. One concern I have it the amount of functions there would be to sufficiently cover every case, see: https://github.com/foundry-rs/forge-std/blob/master/src/console.sol

@leovct
Copy link
Contributor

leovct commented Nov 25, 2024

Hey @leovct, not a problem! I think this is in line with what @mds1 is proposing. One concern I have it the amount of functions there would be to sufficiently cover every case, see: https://github.com/foundry-rs/forge-std/blob/master/src/console.sol

That's also a concern I have, it will require a lot of code to implement such feature. I'm not a rust expert but maybe using macros seems like a better approach to reduce code duplication?

@zerosnacks
Copy link
Member

cc @DaniPopes do you see a clever macro trick we could do here w/ cheatcodes to avoid polluting the concise Vm.sol interface for a relatively niche feature?

@DaniPopes
Copy link
Member

DaniPopes commented Nov 25, 2024

I would prefer a similar approach to console, where the full exposed API is in a separate library and Vm provides a formatRaw(string spec, string ty, bytes data), with ty + data = Vec<DynSolValue> to mimic variadics; it could even be used as "...".format() with using-for.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-cheatcodes Area: cheatcodes C-forge Command: forge first issue A good way to start contributing T-feature Type: feature
Projects
Status: Todo
Development

Successfully merging a pull request may close this issue.

6 participants