diff options
author | mo8it | 2024-02-29 23:55:00 +0000 |
---|---|---|
committer | Blaž Hrastnik | 2024-03-19 05:39:46 +0000 |
commit | 0f5430ab9eb7049c26ee65e27f14812f91a232d5 (patch) | |
tree | c3d3ec18c35155840e03e4b424610d3c9c3ff75a /helix-stdx/src/path.rs | |
parent | e91ec8e880062c1822d08f47dac48dffeab4548f (diff) |
Optimize get_truncated_path
Diffstat (limited to 'helix-stdx/src/path.rs')
-rw-r--r-- | helix-stdx/src/path.rs | 23 |
1 files changed, 11 insertions, 12 deletions
diff --git a/helix-stdx/src/path.rs b/helix-stdx/src/path.rs index c514f9f2..ff2bffae 100644 --- a/helix-stdx/src/path.rs +++ b/helix-stdx/src/path.rs @@ -181,21 +181,20 @@ where /// pub fn get_truncated_path(path: impl AsRef<Path>) -> PathBuf { let cwd = current_working_dir(); - let path = path - .as_ref() - .strip_prefix(cwd) - .unwrap_or_else(|_| path.as_ref()); + let path = path.as_ref(); + let path = path.strip_prefix(cwd).unwrap_or(path); let file = path.file_name().unwrap_or_default(); let base = path.parent().unwrap_or_else(|| Path::new("")); - let mut ret = PathBuf::new(); + let mut ret = PathBuf::with_capacity(file.len()); + // A char can't be directly pushed to a PathBuf + let mut first_char_buffer = String::new(); for d in base { - ret.push( - d.to_string_lossy() - .chars() - .next() - .unwrap_or_default() - .to_string(), - ); + let Some(first_char) = d.to_string_lossy().chars().next() else { + break; + }; + first_char_buffer.push(first_char); + ret.push(&first_char_buffer); + first_char_buffer.clear(); } ret.push(file); ret |