Skip to content

Commit 27a966a

Browse files
committed
Make use of slice::strip_prefix and slice pattern
1 parent 90a7d24 commit 27a966a

File tree

2 files changed

+25
-27
lines changed

2 files changed

+25
-27
lines changed

src/libstd/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@
306306
#![feature(shrink_to)]
307307
#![feature(slice_concat_ext)]
308308
#![feature(slice_internals)]
309+
#![feature(slice_strip)]
309310
#![feature(staged_api)]
310311
#![feature(std_internals)]
311312
#![feature(stdsimd)]

src/libstd/sys/windows/path.rs

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,20 @@ pub fn is_valid_drive_letter(disk: u8) -> bool {
2929
}
3030

3131
pub fn parse_prefix(path: &OsStr) -> Option<Prefix<'_>> {
32-
use crate::path::Prefix::*;
32+
use Prefix::{DeviceNS, Disk, Verbatim, VerbatimDisk, VerbatimUNC, UNC};
3333
unsafe {
3434
// The unsafety here stems from converting between &OsStr and &[u8]
3535
// and back. This is safe to do because (1) we only look at ASCII
3636
// contents of the encoding and (2) new &OsStr values are produced
3737
// only from ASCII-bounded slices of existing &OsStr values.
38-
let mut path = os_str_as_u8_slice(path);
38+
let path = os_str_as_u8_slice(path);
3939

40-
if path.starts_with(br"\\") {
41-
// \\
42-
path = &path[2..];
43-
if path.starts_with(br"?\") {
44-
// \\?\
45-
path = &path[2..];
46-
if path.starts_with(br"UNC\") {
47-
// \\?\UNC\server\share
48-
path = &path[4..];
40+
// \\
41+
if let Some(path) = path.strip_prefix(br"\\") {
42+
// \\?\
43+
if let Some(path) = path.strip_prefix(br"?\") {
44+
// \\?\UNC\server\share
45+
if let Some(path) = path.strip_prefix(br"UNC\") {
4946
let (server, share) = match parse_two_comps(path, is_verbatim_sep) {
5047
Some((server, share)) => {
5148
(u8_slice_as_os_str(server), u8_slice_as_os_str(share))
@@ -55,35 +52,35 @@ pub fn parse_prefix(path: &OsStr) -> Option<Prefix<'_>> {
5552
return Some(VerbatimUNC(server, share));
5653
} else {
5754
// \\?\path
58-
let idx = path.iter().position(|&b| b == b'\\');
59-
if idx == Some(2) && path[1] == b':' {
60-
let c = path[0];
61-
if is_valid_drive_letter(c) {
62-
// \\?\C:\ path
55+
match path {
56+
// \\?\C:\path
57+
[c, b':', b'\\', ..] if is_valid_drive_letter(*c) => {
6358
return Some(VerbatimDisk(c.to_ascii_uppercase()));
6459
}
60+
// \\?\cat_pics
61+
_ => {
62+
let idx = path.iter().position(|&b| b == b'\\').unwrap_or(path.len());
63+
let slice = &path[..idx];
64+
return Some(Verbatim(u8_slice_as_os_str(slice)));
65+
}
6566
}
66-
let slice = &path[..idx.unwrap_or(path.len())];
67-
return Some(Verbatim(u8_slice_as_os_str(slice)));
6867
}
69-
} else if path.starts_with(b".\\") {
70-
// \\.\path
71-
path = &path[2..];
72-
let pos = path.iter().position(|&b| b == b'\\');
73-
let slice = &path[..pos.unwrap_or(path.len())];
68+
} else if let Some(path) = path.strip_prefix(b".\\") {
69+
// \\.\COM42
70+
let idx = path.iter().position(|&b| b == b'\\').unwrap_or(path.len());
71+
let slice = &path[..idx];
7472
return Some(DeviceNS(u8_slice_as_os_str(slice)));
7573
}
7674
match parse_two_comps(path, is_sep_byte) {
7775
Some((server, share)) if !server.is_empty() && !share.is_empty() => {
7876
// \\server\share
7977
return Some(UNC(u8_slice_as_os_str(server), u8_slice_as_os_str(share)));
8078
}
81-
_ => (),
79+
_ => {}
8280
}
83-
} else if path.get(1) == Some(&b':') {
81+
} else if let [c, b':', ..] = path {
8482
// C:
85-
let c = path[0];
86-
if is_valid_drive_letter(c) {
83+
if is_valid_drive_letter(*c) {
8784
return Some(Disk(c.to_ascii_uppercase()));
8885
}
8986
}

0 commit comments

Comments
 (0)