From 46b7881fb0d13034567278af6712ac601319bc59 Mon Sep 17 00:00:00 2001 From: Clement Tsang <34804052+ClementTsang@users.noreply.github.com> Date: Mon, 27 Nov 2023 09:55:21 +0000 Subject: [PATCH] other: preallocate space for ellipsis (#1336) --- src/utils/gen_util.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/utils/gen_util.rs b/src/utils/gen_util.rs index 4b6f8e4e..20df9bb7 100644 --- a/src/utils/gen_util.rs +++ b/src/utils/gen_util.rs @@ -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