Repository navigation
windows: remove redundant prefix parsing in PathBuf::_push
#165720
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
8d25514
64b7a13
2e60508
1c5a731
aa90b4f
bc8dbd4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1344,33 +1344,36 @@ impl PathBuf { | |
| let buf = self.inner.as_encoded_bytes(); | ||
| let mut need_sep = buf.last().map(|c| !is_sep_byte(*c)).unwrap_or(false); | ||
|
|
||
| // in the special case of `C:` on Windows, do *not* add a separator | ||
| let comps = self.components(); | ||
| let self_comps = self.components(); | ||
| let self_prefix_len = self_comps.prefix_len(); | ||
|
|
||
| if comps.prefix_len() > 0 | ||
| && comps.prefix_len() == comps.path.len() | ||
| && comps.prefix.unwrap().is_drive() | ||
| // in the special case of `C:` on Windows, do *not* add a separator | ||
| if self_prefix_len > 0 | ||
| && self_prefix_len == self_comps.path.len() | ||
| && self_comps.prefix.unwrap().is_drive() | ||
| { | ||
| need_sep = false | ||
| } | ||
|
|
||
| let need_clear = if cfg!(target_os = "cygwin") { | ||
| // If path is absolute and its prefix is none, it is like `/foo`, | ||
| // and will be handled below. | ||
| path.prefix().is_some() | ||
| let path_comps = path.components(); | ||
|
|
||
| let need_clear = if cfg!(any(target_os = "windows", target_os = "cygwin")) { | ||
| // Windows/Cygwin: If path is absolute and its prefix is none, it is like `\foo` | ||
| // (Windows) or `/foo` (Cygwin), and will be handled below. | ||
| path_comps.prefix.is_some() | ||
| } else { | ||
| // On Unix: prefix is always None. | ||
| path.is_absolute() || path.prefix().is_some() | ||
| path.is_absolute() || path_comps.prefix.is_some() | ||
| }; | ||
|
|
||
| // absolute `path` replaces `self` | ||
| if need_clear { | ||
| self.inner.clear(); | ||
|
|
||
| // verbatim paths need . and .. removed | ||
| } else if comps.prefix_verbatim() && !path.inner.is_empty() { | ||
| let mut buf: Vec<_> = comps.collect(); | ||
| for c in path.components() { | ||
| } else if self_comps.prefix_verbatim() && !path.inner.is_empty() { | ||
| let mut buf: Vec<_> = self_comps.collect(); | ||
| for c in path_comps { | ||
| match c { | ||
| Component::RootDir => { | ||
| buf.truncate(1); | ||
|
|
@@ -1394,23 +1397,15 @@ impl PathBuf { | |
| res.push(MAIN_SEPARATOR_STR); | ||
| } | ||
| res.push(c.as_os_str()); | ||
|
|
||
| need_sep = match c { | ||
| Component::RootDir => false, | ||
| Component::Prefix(prefix) => { | ||
| !prefix.parsed.is_drive() && prefix.parsed.len() > 0 | ||
| } | ||
| _ => true, | ||
| } | ||
| need_sep = c != Component::RootDir; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this a correct change?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh, for all three of these, i did put the full explanation into the commit message of the commit that introduced each, should I reproduce that somewhere else as well?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i'll just move them here as well i guess ^^
|
||
| } | ||
|
|
||
| self.inner = res; | ||
| return; | ||
|
|
||
| // `path` has a root but no prefix, e.g., `\windows` (Windows only) | ||
| } else if path.has_root() { | ||
| let prefix_len = self.components().prefix_remaining(); | ||
| self.inner.truncate(prefix_len); | ||
| } else if path_comps.has_root() { | ||
| self.inner.truncate(self_prefix_len); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly, why is this an identical change? Is prefix_remaining() here the same as prefix_len() above?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The entire point is to truncate to just the prefix, which can also only be the first component, so these are all equivalent, and I'd argue this is now ~more correct. |
||
|
|
||
| // `path` is a pure relative path | ||
| } else if need_sep { | ||
|
|
@@ -2575,6 +2570,7 @@ impl Path { | |
| !self.is_absolute() | ||
| } | ||
|
|
||
| #[cfg(not(target_os = "uefi"))] | ||
| pub(crate) fn prefix(&self) -> Option<Prefix<'_>> { | ||
| self.components().prefix | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this change correct?
View changes since the review
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(rewritten the commit message to refer to the original naming)
path.is_absolute() || path.prefix().is_some()expands to
(path.has_root() && path.prefix().is_some()) || path.prefix().is_some(),which simplifies to just the prefix check.
Pushing
C:fooshould clear, pushing\footruncates to the existing prefix, just like on cygwin.