synth icon indicating copy to clipboard operation
synth copied to clipboard

Feature: Allow more extensive string manipulation in `format`

Open vlushn opened this issue 4 years ago • 1 comments

Currently, string formatter can:

  • concatenate strings
  • reference component strings from other fields via same_as
  • truncate component string lengths via truncate

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:

  • string slicing: {name[2..]} or {mac_addr[8..12]}
  • regex extraction: {ip4cidr@^[0-9]\.[0-9]\.[0-9].\[0-9]$@{2}} to extract the third octet

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

vlushn avatar Sep 28 '21 16:09 vlushn

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.

christos-h avatar Oct 04 '21 14:10 christos-h