regex icon indicating copy to clipboard operation
regex copied to clipboard

escape into a formatter

Open jethrogb opened this issue 8 years ago • 2 comments

Currently, escape allocates a new string for its output. It's often the case that you want to use the output of escape in a larger string to specify the regex. It would be more convenient if the escaping method implements Display so it can be used directly in a format string without allocation. To get an allocated string, one can then always use the to_string method.

Suggested API:

struct Escape { ... }

impl Escape {
  fn new(text: &str) -> Self { ... }
}

impl Display for Escape { ... }

jethrogb avatar Dec 29 '17 16:12 jethrogb

I guess in principle I agree, but can you actually come up with a benchmark where this matters? I'd be surprised if you could. The benchmark should include regex compilation. My instinct is that these sorts of allocations would be dwarfed by the compilation process itself.

BurntSushi avatar Dec 29 '17 16:12 BurntSushi

Alternatively, it could return an iterator like std library does for EscapeDefault and EscapeDebug, then you can either .collect() to String or .extend existing String (and it will be more consistent with std).

RReverser avatar Feb 23 '18 13:02 RReverser

If someone can give me a good use case for avoiding allocation for something like this, then I'd be happy to revisit an API like this. But otherwise I don't think it's worth doing.

BurntSushi avatar Mar 06 '23 18:03 BurntSushi