templater: micro-optimize short/shortest hex operation

This commit is contained in:
Yuya Nishihara 2023-01-19 13:42:43 +09:00
parent 38e6924482
commit 0f4269a141

View file

@ -422,16 +422,18 @@ impl CommitOrChangeId {
} }
pub fn short(&self) -> String { 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 { 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 { fn highlight_shortest_prefix(mut hex: String, total_len: usize, repo: RepoRef) -> String {
let prefix_len = repo.base_repo().shortest_unique_prefix_length(hex); let prefix_len = repo.base_repo().shortest_unique_prefix_length(&hex);
if prefix_len < total_len - 1 { if prefix_len < total_len - 1 {
format!( format!(
"{}[{}]", "{}[{}]",
@ -439,7 +441,8 @@ fn highlight_shortest_prefix(hex: &str, total_len: usize, repo: RepoRef) -> Stri
&hex[prefix_len..total_len - 1] &hex[prefix_len..total_len - 1]
) )
} else { } else {
hex[0..total_len].to_string() hex.truncate(total_len);
hex
} }
} }