other: preallocate space for ellipsis (#1336)

This commit is contained in:
Clement Tsang 2023-11-27 09:55:21 +00:00 committed by GitHub
parent 590f03bd69
commit 46b7881fb0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -106,7 +106,8 @@ enum AsciiIterationResult {
fn greedy_ascii_add(content: &str, width: NonZeroUsize) -> (String, AsciiIterationResult) {
let width: usize = width.into();
let mut text = Vec::with_capacity(width);
const SIZE_OF_ELLIPSIS: usize = 3;
let mut text = Vec::with_capacity(width - 1 + SIZE_OF_ELLIPSIS);
let s = content.as_bytes();
@ -134,7 +135,7 @@ fn greedy_ascii_add(content: &str, width: NonZeroUsize) -> (String, AsciiIterati
debug_assert!(text.is_ascii());
let current_index = if s[current_index].is_ascii() {
let mut ellipsis = [0; 3];
let mut ellipsis = [0; SIZE_OF_ELLIPSIS];
'…'.encode_utf8(&mut ellipsis);
text.extend_from_slice(&ellipsis);
AsciiIterationResult::Complete