3 use crate::path::Prefix;
5 fn os_str_as_u8_slice(s: &OsStr) -> &[u8] {
6 unsafe { mem::transmute(s) }
8 unsafe fn u8_slice_as_os_str(s: &[u8]) -> &OsStr {
13 pub fn is_sep_byte(b: u8) -> bool {
14 b == b'/' || b == b'\\'
18 pub fn is_verbatim_sep(b: u8) -> bool {
22 pub fn parse_prefix(path: &OsStr) -> Option<Prefix<'_>> {
23 use crate::path::Prefix::*;
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);
31 if path.starts_with(br"\\") {
34 if path.starts_with(br"?\") {
37 if path.starts_with(br"UNC\") {
38 // \\?\UNC\server\share
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))
44 None => (u8_slice_as_os_str(path), u8_slice_as_os_str(&[])),
46 return Some(VerbatimUNC(server, share));
49 let idx = path.iter().position(|&b| b == b'\\');
50 if idx == Some(2) && path[1] == b':' {
52 if c.is_ascii() && (c as char).is_alphabetic() {
54 return Some(VerbatimDisk(c.to_ascii_uppercase()));
57 let slice = &path[..idx.unwrap_or(path.len())];
58 return Some(Verbatim(u8_slice_as_os_str(slice)));
60 } else if path.starts_with(b".\\") {
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)));
67 match parse_two_comps(path, is_sep_byte) {
68 Some((server, share)) if !server.is_empty() && !share.is_empty() => {
70 return Some(UNC(u8_slice_as_os_str(server), u8_slice_as_os_str(share)));
74 } else if path.get(1) == Some(&b':') {
77 if c.is_ascii() && (c as char).is_alphabetic() {
78 return Some(Disk(c.to_ascii_uppercase()));
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())];
93 pub const MAIN_SEP_STR: &str = "\\";
94 pub const MAIN_SEP: char = '\\';