aatxe / irc

the irc crate – usable, async IRC for Rust
Mozilla Public License 2.0
528 stars 97 forks source link

Make stripping color codes efficiently #145

Closed Kroisse closed 5 years ago

Kroisse commented 6 years ago

It also fixes infinite recursion described in https://github.com/aatxe/irc/pull/137#issuecomment-414094604.

Previously, FormattedStringExt::strip_formatting always copied whole string even if its return type is Cow<'_, str>. Its method signature prevents the impl for String to reuse self because there is no way to take the ownership from the caller.

For this reason, I have to change the interface of FormattedStringExt trait. Theoretically it is backward-incompatible, but I think it's critical only if this trait was implemented in the outside.

Instead the new implementation gains the benefit of memory usages. for str, stripping will be almost no-op for unformatted text. And for String, it will allocate no additional memory in most of cases.

aatxe commented 5 years ago

Sorry for the delay in review! I've been very busy as of late, and wanted to give this a good look through. Thanks for the contribution! I love the little test macro. 😄