shuttle-hq / synth

The Declarative Data Generator
https://www.getsynth.com/
Apache License 2.0
1.39k stars 109 forks source link

Feature: Allow more extensive string manipulation in `format` #176

Open vlushn opened 3 years ago

vlushn commented 3 years ago

Currently, string formatter can:

This is a good start but not enough for a robust string manipulation mechanism. It would be great to have support for string slicing (i.e. take the first 5 characters or between the 7th and 10th character) and regex extraction (take the first capture group). For that we also need to think about what the format string language looks like.

Examples:

It doesn't look like dynfmt currently supports more advanced formats so something might need to change there.

christos-h commented 3 years ago

This makes a lot of sense.

To me this looks like doing a re-haul of the format generator to have a proper parser for the format string. We can use a parser-combinator like nom for this. We can then use standard Rust crates to do the heavy lifting (regex, string slicing etc.). A lot of this should be covered by https://docs.rs/regex/1.5.4/regex/.

A lot of this also seems upstreamable into dynfmt- I've opened an issue there to see if there is any plan to implement this.