Skip to content

Commit 063bbc4

Browse files
authored
Rollup merge of #74220 - lzutao:windows-path-com, r=LukasKalbertodt
Refactor Windows `parse_prefix` These changes make me feel more readable. See the commit messages for more details.
2 parents b9a0f58 + e31898b commit 063bbc4

File tree

4 files changed

+103
-69
lines changed

4 files changed

+103
-69
lines changed

src/libstd/lib.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,8 @@
242242
#![feature(atomic_mut_ptr)]
243243
#![feature(box_syntax)]
244244
#![feature(c_variadic)]
245-
#![feature(cfg_accessible)]
246245
#![feature(can_vector)]
246+
#![feature(cfg_accessible)]
247247
#![feature(cfg_target_has_atomic)]
248248
#![feature(cfg_target_thread_local)]
249249
#![feature(char_error_internals)]
@@ -276,8 +276,8 @@
276276
#![feature(hashmap_internals)]
277277
#![feature(int_error_internals)]
278278
#![feature(int_error_matching)]
279-
#![feature(into_future)]
280279
#![feature(integer_atomics)]
280+
#![feature(into_future)]
281281
#![feature(lang_items)]
282282
#![feature(libc)]
283283
#![feature(link_args)]
@@ -286,6 +286,7 @@
286286
#![feature(log_syntax)]
287287
#![feature(maybe_uninit_ref)]
288288
#![feature(maybe_uninit_slice)]
289+
#![feature(min_specialization)]
289290
#![feature(needs_panic_runtime)]
290291
#![feature(negative_impls)]
291292
#![feature(never_type)]
@@ -305,7 +306,7 @@
305306
#![feature(shrink_to)]
306307
#![feature(slice_concat_ext)]
307308
#![feature(slice_internals)]
308-
#![feature(min_specialization)]
309+
#![feature(slice_strip)]
309310
#![feature(staged_api)]
310311
#![feature(std_internals)]
311312
#![feature(stdsimd)]

src/libstd/sys/windows/path.rs

+76-63
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@ use crate::ffi::OsStr;
22
use crate::mem;
33
use crate::path::Prefix;
44

5+
#[cfg(test)]
6+
mod tests;
7+
8+
pub const MAIN_SEP_STR: &str = "\\";
9+
pub const MAIN_SEP: char = '\\';
10+
11+
// The unsafety here stems from converting between `&OsStr` and `&[u8]`
12+
// and back. This is safe to do because (1) we only look at ASCII
13+
// contents of the encoding and (2) new &OsStr values are produced
14+
// only from ASCII-bounded slices of existing &OsStr values.
515
fn os_str_as_u8_slice(s: &OsStr) -> &[u8] {
616
unsafe { mem::transmute(s) }
717
}
@@ -19,76 +29,79 @@ pub fn is_verbatim_sep(b: u8) -> bool {
1929
b == b'\\'
2030
}
2131

32+
// In most DOS systems, it is not possible to have more than 26 drive letters.
33+
// See <https://en.wikipedia.org/wiki/Drive_letter_assignment#Common_assignments>.
34+
pub fn is_valid_drive_letter(disk: u8) -> bool {
35+
disk.is_ascii_alphabetic()
36+
}
37+
2238
pub fn parse_prefix(path: &OsStr) -> Option<Prefix<'_>> {
23-
use crate::path::Prefix::*;
24-
unsafe {
25-
// The unsafety here stems from converting between &OsStr and &[u8]
26-
// and back. This is safe to do because (1) we only look at ASCII
27-
// contents of the encoding and (2) new &OsStr values are produced
28-
// only from ASCII-bounded slices of existing &OsStr values.
29-
let mut path = os_str_as_u8_slice(path);
39+
use Prefix::{DeviceNS, Disk, Verbatim, VerbatimDisk, VerbatimUNC, UNC};
40+
41+
let path = os_str_as_u8_slice(path);
3042

31-
if path.starts_with(br"\\") {
32-
// \\
33-
path = &path[2..];
34-
if path.starts_with(br"?\") {
35-
// \\?\
36-
path = &path[2..];
37-
if path.starts_with(br"UNC\") {
38-
// \\?\UNC\server\share
39-
path = &path[4..];
40-
let (server, share) = match parse_two_comps(path, is_verbatim_sep) {
41-
Some((server, share)) => {
42-
(u8_slice_as_os_str(server), u8_slice_as_os_str(share))
43-
}
44-
None => (u8_slice_as_os_str(path), u8_slice_as_os_str(&[])),
45-
};
46-
return Some(VerbatimUNC(server, share));
47-
} else {
48-
// \\?\path
49-
let idx = path.iter().position(|&b| b == b'\\');
50-
if idx == Some(2) && path[1] == b':' {
51-
let c = path[0];
52-
if c.is_ascii() && (c as char).is_alphabetic() {
53-
// \\?\C:\ path
54-
return Some(VerbatimDisk(c.to_ascii_uppercase()));
55-
}
43+
// \\
44+
if let Some(path) = path.strip_prefix(br"\\") {
45+
// \\?\
46+
if let Some(path) = path.strip_prefix(br"?\") {
47+
// \\?\UNC\server\share
48+
if let Some(path) = path.strip_prefix(br"UNC\") {
49+
let (server, share) = match get_first_two_components(path, is_verbatim_sep) {
50+
Some((server, share)) => unsafe {
51+
(u8_slice_as_os_str(server), u8_slice_as_os_str(share))
52+
},
53+
None => (unsafe { u8_slice_as_os_str(path) }, OsStr::new("")),
54+
};
55+
return Some(VerbatimUNC(server, share));
56+
} else {
57+
// \\?\path
58+
match path {
59+
// \\?\C:\path
60+
[c, b':', b'\\', ..] if is_valid_drive_letter(*c) => {
61+
return Some(VerbatimDisk(c.to_ascii_uppercase()));
62+
}
63+
// \\?\cat_pics
64+
_ => {
65+
let idx = path.iter().position(|&b| b == b'\\').unwrap_or(path.len());
66+
let slice = &path[..idx];
67+
return Some(Verbatim(unsafe { u8_slice_as_os_str(slice) }));
5668
}
57-
let slice = &path[..idx.unwrap_or(path.len())];
58-
return Some(Verbatim(u8_slice_as_os_str(slice)));
59-
}
60-
} else if path.starts_with(b".\\") {
61-
// \\.\path
62-
path = &path[2..];
63-
let pos = path.iter().position(|&b| b == b'\\');
64-
let slice = &path[..pos.unwrap_or(path.len())];
65-
return Some(DeviceNS(u8_slice_as_os_str(slice)));
66-
}
67-
match parse_two_comps(path, is_sep_byte) {
68-
Some((server, share)) if !server.is_empty() && !share.is_empty() => {
69-
// \\server\share
70-
return Some(UNC(u8_slice_as_os_str(server), u8_slice_as_os_str(share)));
7169
}
72-
_ => (),
7370
}
74-
} else if path.get(1) == Some(&b':') {
75-
// C:
76-
let c = path[0];
77-
if c.is_ascii() && (c as char).is_alphabetic() {
78-
return Some(Disk(c.to_ascii_uppercase()));
71+
} else if let Some(path) = path.strip_prefix(b".\\") {
72+
// \\.\COM42
73+
let idx = path.iter().position(|&b| b == b'\\').unwrap_or(path.len());
74+
let slice = &path[..idx];
75+
return Some(DeviceNS(unsafe { u8_slice_as_os_str(slice) }));
76+
}
77+
match get_first_two_components(path, is_sep_byte) {
78+
Some((server, share)) if !server.is_empty() && !share.is_empty() => {
79+
// \\server\share
80+
return Some(unsafe { UNC(u8_slice_as_os_str(server), u8_slice_as_os_str(share)) });
7981
}
82+
_ => {}
83+
}
84+
} else if let [c, b':', ..] = path {
85+
// C:
86+
if is_valid_drive_letter(*c) {
87+
return Some(Disk(c.to_ascii_uppercase()));
8088
}
81-
return None;
82-
}
83-
84-
fn parse_two_comps(mut path: &[u8], f: fn(u8) -> bool) -> Option<(&[u8], &[u8])> {
85-
let first = &path[..path.iter().position(|x| f(*x))?];
86-
path = &path[(first.len() + 1)..];
87-
let idx = path.iter().position(|x| f(*x));
88-
let second = &path[..idx.unwrap_or(path.len())];
89-
Some((first, second))
9089
}
90+
None
9191
}
9292

93-
pub const MAIN_SEP_STR: &str = "\\";
94-
pub const MAIN_SEP: char = '\\';
93+
/// Returns the first two path components with predicate `f`.
94+
///
95+
/// The two components returned will be use by caller
96+
/// to construct `VerbatimUNC` or `UNC` Windows path prefix.
97+
///
98+
/// Returns [`None`] if there are no separators in path.
99+
fn get_first_two_components(path: &[u8], f: fn(u8) -> bool) -> Option<(&[u8], &[u8])> {
100+
let idx = path.iter().position(|&x| f(x))?;
101+
// Panic safe
102+
// The max `idx+1` is `path.len()` and `path[path.len()..]` is a valid index.
103+
let (first, path) = (&path[..idx], &path[idx + 1..]);
104+
let idx = path.iter().position(|&x| f(x)).unwrap_or(path.len());
105+
let second = &path[..idx];
106+
Some((first, second))
107+
}

src/libstd/sys/windows/path/tests.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use super::*;
2+
3+
#[test]
4+
fn test_get_first_two_components() {
5+
assert_eq!(
6+
get_first_two_components(br"server\share", is_verbatim_sep),
7+
Some((&b"server"[..], &b"share"[..])),
8+
);
9+
10+
assert_eq!(
11+
get_first_two_components(br"server\", is_verbatim_sep),
12+
Some((&b"server"[..], &b""[..]))
13+
);
14+
15+
assert_eq!(
16+
get_first_two_components(br"\server\", is_verbatim_sep),
17+
Some((&b""[..], &b"server"[..]))
18+
);
19+
20+
assert_eq!(get_first_two_components(br"there are no separators here", is_verbatim_sep), None,);
21+
}

src/libstd/thread/mod.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -641,9 +641,8 @@ where
641641
#[stable(feature = "rust1", since = "1.0.0")]
642642
pub fn current() -> Thread {
643643
thread_info::current_thread().expect(
644-
"use of std::thread::current() is not \
645-
possible after the thread's local \
646-
data has been destroyed",
644+
"use of std::thread::current() is not possible \
645+
after the thread's local data has been destroyed",
647646
)
648647
}
649648

0 commit comments

Comments
 (0)