Skip to content

Commit

Permalink
refactor: add fast branch if the entire string is definitely not trun…
Browse files Browse the repository at this point in the history
…cated
  • Loading branch information
ClementTsang committed Nov 24, 2023
1 parent a93521d commit 1bbe585
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/utils/gen_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,19 @@ fn grapheme_width(g: &str) -> usize {
fn truncate_str<U: Into<usize>>(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;
Expand Down

0 comments on commit 1bbe585

Please sign in to comment.