-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Add os::windows::ffi::OsStrExt::system_cmp
and system_eq
#86008
Conversation
r? @kennytm (rust-highfive has picked a reviewer for you, use r? to override) |
library/std/src/os/windows/ffi.rs
Outdated
/// This is the correct way to compare various strings on Windows: | ||
/// environment variable keys, registry keys, file paths and resource handle names are all case-insensitive. |
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.
As per #86007 (comment) this might still not be the correct way for dealing with file paths. What should we say about this in the documentation?
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.
Personally, I'm uncertain. Perhaps remove "file paths" from that list but then add a note that file paths may have different rules? I think system_cmp
could still perhaps be useful as a heuristic (i.e. these two paths may be equal) so long as it isn't relied upon for file paths.
But I don't think there's any OS support for comparing file paths other than opening both files and seeing if they point to the same file. Which obviously can't be done if one of the paths doesn't yet exist (or they can't be opened for some other reason). I mean, system_cmp
may work for file paths most of the time but when it doesn't it could be a footgun. Or at least surprising when two "equal" paths point to different files.
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.
Yeah comparing file paths is more complex, also due to the possibility of relative paths, verbatim paths, UNC prefixes, symlinks, etc; there are many ways in which two different paths can be equivalent.
It seems more appropriate to find a separate correct solution for that, so I'll leave it out of the documentation in this PR.
8c328bd
to
8392bd3
Compare
I believe that the case sensitivity is a filesystem (and nowadays, per-directory) property, rather than a OS-specified one, so naming them |
Yes for paths it seems better to offer a |
/// let b = OsString::from("PATH"); | ||
/// | ||
/// assert!(a.eq(&b) == false); | ||
/// assert!(a.system_eq(&b).unwrap() == true); |
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.
I think you're missing a closing ```
?
@CDirkx can you address the comment from Camelid |
ping from triage: returning to author to address comments |
☔ The latest upstream changes (presumably #89174) made this pull request unmergeable. Please resolve the merge conflicts. |
Adds two methods to the extension trait
OsStrExt
on Windows for dealing with case-insensitive strings:system_cmp
andsystem_eq
. These methods use the system case-insensitive implementationCompareStringOrdinal
withbIgnoreCase
, which differs from a pure Unicode case-insensitive comparison (which is why I explicitly named these methodssystem_
instead of something likecase_insensitive_
).These methods are the correct way for comparing environment variable keys, registry keys, resource handle names etc. on Windows, e.g.
"Path"
and"PATH"
are treated the same by the system and should compare equal.This is the minimal way I thought of for enabling the use of Windows specific case-insensitive comparisons. There are several ways this could be extended further if necessary:
Also have extension traits with(This is not the correct way to compare paths: Tracking Issue forsystem_cmp
andsystem_eq
forPath
,[u8]
?,[u16]
?windows_case_insensitive
#86007 (comment))Introduce a traitSystemOrd
andSystemEq
instd::os::windows
.num::Wrapping
forOsStr
that overrides theOrd
andEq
implementation.Inspired by #85270
Tracking issue: #86007