Skip to content

Commit

Permalink
Avoid copying the message three times in ProgressBar::message (#105)
Browse files Browse the repository at this point in the history
String::replace doesn't modify the original String, so `to_owned` can be dropped. It also accepts
&[char] as a pattern and replaces any of the chars found. The two calls to `replace` can thus be
combined into one.

This brings us from three copies with two scan passes down to one copy with one scan pass.
  • Loading branch information
LingMan committed Jun 21, 2022
1 parent c520a21 commit 54d068b
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/pb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ impl<T: Write> ProgressBar<T> {
///
/// ```
pub fn message(&mut self, message: &str) {
self.message = message.to_owned().replace("\n", " ").replace("\r", " ")
self.message = message.replace(['\n', '\r'], " ")
}

/// Set tick format for the progressBar, default is \\|/-
Expand Down

0 comments on commit 54d068b

Please sign in to comment.