From 30a325685ff506ad81dee2eb815cdbec76745370 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Mon, 6 Jul 2020 13:16:03 +0800 Subject: [PATCH 1/2] Avoid unnecessary copies if there is nothing to escape --- src/lib.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 444b9fc..e6ecef0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -42,6 +42,7 @@ use std::convert::From; use std::fmt; use std::ops::Deref; use std::string::String; +use std::borrow::Cow; pub use style::{Style, Styles}; @@ -394,9 +395,9 @@ impl ColoredString { res } - fn escape_inner_reset_sequences(&self) -> String { + fn escape_inner_reset_sequences(&self) -> Cow { if !self.has_colors() || self.is_plain() { - return self.input.clone(); + return self.input.as_str().into(); } // TODO: BoyScoutRule @@ -407,6 +408,9 @@ impl ColoredString { .match_indices(reset) .map(|(idx, _)| idx) .collect(); + if matches.len() == 0 { + return self.input.as_str().into() + } let mut input = self.input.clone(); input.reserve(matches.len() * style.len()); @@ -422,7 +426,7 @@ impl ColoredString { } } - input + input.into() } } @@ -575,7 +579,7 @@ impl fmt::Display for ColoredString { let escaped_input = self.escape_inner_reset_sequences(); f.write_str(&self.compute_style())?; - ::fmt(&escaped_input, f)?; + escaped_input.fmt(f)?; f.write_str("\x1B[0m")?; Ok(()) } From e75ec77988cff0294ea733a0f5ecb77d1e0ce4be Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Wed, 8 Jul 2020 14:27:43 +0800 Subject: [PATCH 2/2] Pacify clippy so CI goes green --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index e6ecef0..51fc457 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -408,7 +408,7 @@ impl ColoredString { .match_indices(reset) .map(|(idx, _)| idx) .collect(); - if matches.len() == 0 { + if matches.is_empty() { return self.input.as_str().into() }