From 1bbe585d0d4d7f45d15c2099f7fccf6beacdc73f Mon Sep 17 00:00:00 2001 From: ClementTsang <34804052+ClementTsang@users.noreply.github.com> Date: Fri, 24 Nov 2023 02:48:27 -0500 Subject: [PATCH] refactor: add fast branch if the entire string is definitely not truncated --- src/utils/gen_util.rs | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/utils/gen_util.rs b/src/utils/gen_util.rs index 97c6dbc19..effd5a6bc 100644 --- a/src/utils/gen_util.rs +++ b/src/utils/gen_util.rs @@ -101,20 +101,19 @@ fn grapheme_width(g: &str) -> usize { fn truncate_str>(content: &str, width: U) -> String { let width = width.into(); - if width > 0 { + if content.len() <= width { + // If the entire string fits in the width, it'll definitely fit. + content.to_owned() + } else if width > 0 { if content.is_ascii() { // If the entire string is ASCII, we can use a much simpler approach. - if content.len() <= width { - content.to_owned() - } else { - let mut text = String::with_capacity(width); - let (keep, _throw) = content.split_at(width - 1); - text.push_str(keep); - text.push('…'); + let mut text = String::with_capacity(width); + let (keep, _throw) = content.split_at(width - 1); + text.push_str(keep); + text.push('…'); - text - } + text } else { let mut text = String::with_capacity(width); let mut curr_width = 0;