From 0f4269a14130bbd8df1e0460402a0aca7e4843ca Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Thu, 19 Jan 2023 13:42:43 +0900 Subject: [PATCH] templater: micro-optimize short/shortest hex operation --- src/templater.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/templater.rs b/src/templater.rs index 2807560c2..d48dbe2ce 100644 --- a/src/templater.rs +++ b/src/templater.rs @@ -422,16 +422,18 @@ impl CommitOrChangeId { } pub fn short(&self) -> String { - self.hex()[..12].to_string() + let mut hex = self.hex(); + hex.truncate(12); + hex } pub fn short_prefix_and_brackets(&self, repo: RepoRef) -> String { - highlight_shortest_prefix(&self.hex(), 12, repo) + highlight_shortest_prefix(self.hex(), 12, repo) } } -fn highlight_shortest_prefix(hex: &str, total_len: usize, repo: RepoRef) -> String { - let prefix_len = repo.base_repo().shortest_unique_prefix_length(hex); +fn highlight_shortest_prefix(mut hex: String, total_len: usize, repo: RepoRef) -> String { + let prefix_len = repo.base_repo().shortest_unique_prefix_length(&hex); if prefix_len < total_len - 1 { format!( "{}[{}]", @@ -439,7 +441,8 @@ fn highlight_shortest_prefix(hex: &str, total_len: usize, repo: RepoRef) -> Stri &hex[prefix_len..total_len - 1] ) } else { - hex[0..total_len].to_string() + hex.truncate(total_len); + hex } }