Skip to content

Commit 3bc4d2b

Browse files
committed
Add sys::windows::compare_case_insensitive
1 parent dc08641 commit 3bc4d2b

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

library/std/src/sys/windows/c.rs

+12
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ pub type ADDRESS_FAMILY = USHORT;
6868
pub const TRUE: BOOL = 1;
6969
pub const FALSE: BOOL = 0;
7070

71+
pub const CSTR_LESS_THAN: c_int = 1;
72+
pub const CSTR_EQUAL: c_int = 2;
73+
pub const CSTR_GREATER_THAN: c_int = 3;
74+
7175
pub const FILE_ATTRIBUTE_READONLY: DWORD = 0x1;
7276
pub const FILE_ATTRIBUTE_DIRECTORY: DWORD = 0x10;
7377
pub const FILE_ATTRIBUTE_REPARSE_POINT: DWORD = 0x400;
@@ -996,6 +1000,14 @@ extern "system" {
9961000
pub fn ReleaseSRWLockShared(SRWLock: PSRWLOCK);
9971001
pub fn TryAcquireSRWLockExclusive(SRWLock: PSRWLOCK) -> BOOLEAN;
9981002
pub fn TryAcquireSRWLockShared(SRWLock: PSRWLOCK) -> BOOLEAN;
1003+
1004+
pub fn CompareStringOrdinal(
1005+
lpString1: LPCWSTR,
1006+
cchCount1: c_int,
1007+
lpString2: LPCWSTR,
1008+
cchCount2: c_int,
1009+
bIgnoreCase: BOOL,
1010+
) -> c_int;
9991011
}
10001012

10011013
#[link(name = "ws2_32")]

library/std/src/sys/windows/mod.rs

+19
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,25 @@ pub fn to_u16s<S: AsRef<OsStr>>(s: S) -> crate::io::Result<Vec<u16>> {
151151
inner(s.as_ref())
152152
}
153153

154+
pub fn compare_case_insensitive<A: AsRef<OsStr>, B: AsRef<OsStr>>(
155+
a: A,
156+
b: B,
157+
) -> crate::io::Result<crate::cmp::Ordering> {
158+
let a = crate::sys::to_u16s(a.as_ref())?;
159+
let b = crate::sys::to_u16s(b.as_ref())?;
160+
161+
let result = unsafe {
162+
c::CompareStringOrdinal(a.as_ptr(), a.len() as _, b.as_ptr(), b.len() as _, c::TRUE)
163+
};
164+
165+
match result {
166+
c::CSTR_LESS_THAN => Ok(crate::cmp::Ordering::Less),
167+
c::CSTR_EQUAL => Ok(crate::cmp::Ordering::Equal),
168+
c::CSTR_GREATER_THAN => Ok(crate::cmp::Ordering::Greater),
169+
_ => Err(crate::io::Error::last_os_error()),
170+
}
171+
}
172+
154173
// Many Windows APIs follow a pattern of where we hand a buffer and then they
155174
// will report back to us how large the buffer should be or how many bytes
156175
// currently reside in the buffer. This function is an abstraction over these

0 commit comments

Comments
 (0)