Mercurial > hg-stable
changeset 50216:ee63c87a0cac
rhg: in path_encode, simplify a bit more
Use the slices for `basename` and `ext` instead of dealing with offsets.
author | Arseniy Alekseyev <aalekseyev@janestreet.com> |
---|---|
date | Fri, 17 Feb 2023 16:48:11 +0000 |
parents | 6ea3b1acb5de |
children | 596a6b9b0570 |
files | rust/hg-core/src/revlog/path_encode.rs |
diffstat | 1 files changed, 12 insertions(+), 25 deletions(-) [+] |
line wrap: on
line diff
--- a/rust/hg-core/src/revlog/path_encode.rs Fri Feb 17 13:29:39 2023 +0000 +++ b/rust/hg-core/src/revlog/path_encode.rs Fri Feb 17 16:48:11 2023 +0000 @@ -527,9 +527,14 @@ let maxshortdirslen = 68; let last_slash = src.iter().rposition(|b| *b == b'/'); - let last_dot: Option<usize> = { - let s = last_slash.unwrap_or(0); - src[s..].iter().rposition(|b| *b == b'.').map(|i| i + s) + let basename_start = match last_slash { + Some(slash) => slash + 1, + None => 0, + }; + let basename = &src[basename_start..]; + let ext = match basename.iter().rposition(|b| *b == b'.') { + None => &[], + Some(dot) => &basename[dot..], }; let mut dest = Vec::with_capacity(MAXSTOREPATHLEN); @@ -547,35 +552,17 @@ } } - let used = dest.len() + 40 + { - if let Some(l) = last_dot { - src.len() - l - } else { - 0 - } - }; + let used = dest.len() + 40 + ext.len(); if MAXSTOREPATHLEN > used { let slop = MAXSTOREPATHLEN - used; - let basenamelen = match last_slash { - Some(l) => src.len() - l - 1, - None => src.len(), - }; - let basenamelen = std::cmp::min(basenamelen, slop); - if basenamelen > 0 { - let start = match last_slash { - Some(l) => l + 1, - None => 0, - }; - dest.write_bytes(&src[start..][..basenamelen]) - } + let len = std::cmp::min(basename.len(), slop); + dest.write_bytes(&basename[..len]) } for c in sha { hexencode(&mut dest, *c); } - if let Some(l) = last_dot { - dest.write_bytes(&src[l..]); - } + dest.write_bytes(ext); dest.shrink_to_fit(); dest }