@@ -33,12 +33,12 @@ pub fn is_verbatim_sep(b: u8) -> bool {
33
33
34
34
/// Returns true if `path` looks like a lone filename.
35
35
pub ( crate ) fn is_file_name ( path : & OsStr ) -> bool {
36
- !path. bytes ( ) . iter ( ) . copied ( ) . any ( is_sep_byte)
36
+ !path. as_os_str_bytes ( ) . iter ( ) . copied ( ) . any ( is_sep_byte)
37
37
}
38
38
pub ( crate ) fn has_trailing_slash ( path : & OsStr ) -> bool {
39
- let is_verbatim = path. bytes ( ) . starts_with ( br"\\?\" ) ;
39
+ let is_verbatim = path. as_os_str_bytes ( ) . starts_with ( br"\\?\" ) ;
40
40
let is_separator = if is_verbatim { is_verbatim_sep } else { is_sep_byte } ;
41
- if let Some ( & c) = path. bytes ( ) . last ( ) { is_separator ( c) } else { false }
41
+ if let Some ( & c) = path. as_os_str_bytes ( ) . last ( ) { is_separator ( c) } else { false }
42
42
}
43
43
44
44
/// Appends a suffix to a path.
@@ -60,7 +60,7 @@ impl<'a, const LEN: usize> PrefixParser<'a, LEN> {
60
60
fn get_prefix ( path : & OsStr ) -> [ u8 ; LEN ] {
61
61
let mut prefix = [ 0 ; LEN ] ;
62
62
// SAFETY: Only ASCII characters are modified.
63
- for ( i, & ch) in path. bytes ( ) . iter ( ) . take ( LEN ) . enumerate ( ) {
63
+ for ( i, & ch) in path. as_os_str_bytes ( ) . iter ( ) . take ( LEN ) . enumerate ( ) {
64
64
prefix[ i] = if ch == b'/' { b'\\' } else { ch } ;
65
65
}
66
66
prefix
@@ -93,15 +93,15 @@ impl<'a> PrefixParserSlice<'a, '_> {
93
93
}
94
94
95
95
fn prefix_bytes ( & self ) -> & ' a [ u8 ] {
96
- & self . path . bytes ( ) [ ..self . index ]
96
+ & self . path . as_os_str_bytes ( ) [ ..self . index ]
97
97
}
98
98
99
99
fn finish ( self ) -> & ' a OsStr {
100
100
// SAFETY: The unsafety here stems from converting between &OsStr and
101
101
// &[u8] and back. This is safe to do because (1) we only look at ASCII
102
102
// contents of the encoding and (2) new &OsStr values are produced only
103
103
// from ASCII-bounded slices of existing &OsStr values.
104
- unsafe { bytes_as_os_str ( & self . path . bytes ( ) [ self . index ..] ) }
104
+ unsafe { bytes_as_os_str ( & self . path . as_os_str_bytes ( ) [ self . index ..] ) }
105
105
}
106
106
}
107
107
@@ -173,7 +173,7 @@ fn parse_drive(path: &OsStr) -> Option<u8> {
173
173
drive. is_ascii_alphabetic ( )
174
174
}
175
175
176
- match path. bytes ( ) {
176
+ match path. as_os_str_bytes ( ) {
177
177
[ drive, b':' , ..] if is_valid_drive_letter ( drive) => Some ( drive. to_ascii_uppercase ( ) ) ,
178
178
_ => None ,
179
179
}
@@ -182,7 +182,7 @@ fn parse_drive(path: &OsStr) -> Option<u8> {
182
182
// Parses a drive prefix exactly, e.g. "C:"
183
183
fn parse_drive_exact ( path : & OsStr ) -> Option < u8 > {
184
184
// only parse two bytes: the drive letter and the drive separator
185
- if path. bytes ( ) . get ( 2 ) . map ( |& x| is_sep_byte ( x) ) . unwrap_or ( true ) {
185
+ if path. as_os_str_bytes ( ) . get ( 2 ) . map ( |& x| is_sep_byte ( x) ) . unwrap_or ( true ) {
186
186
parse_drive ( path)
187
187
} else {
188
188
None
@@ -196,15 +196,15 @@ fn parse_drive_exact(path: &OsStr) -> Option<u8> {
196
196
fn parse_next_component ( path : & OsStr , verbatim : bool ) -> ( & OsStr , & OsStr ) {
197
197
let separator = if verbatim { is_verbatim_sep } else { is_sep_byte } ;
198
198
199
- match path. bytes ( ) . iter ( ) . position ( |& x| separator ( x) ) {
199
+ match path. as_os_str_bytes ( ) . iter ( ) . position ( |& x| separator ( x) ) {
200
200
Some ( separator_start) => {
201
201
let separator_end = separator_start + 1 ;
202
202
203
- let component = & path. bytes ( ) [ ..separator_start] ;
203
+ let component = & path. as_os_str_bytes ( ) [ ..separator_start] ;
204
204
205
205
// Panic safe
206
206
// The max `separator_end` is `bytes.len()` and `bytes[bytes.len()..]` is a valid index.
207
- let path = & path. bytes ( ) [ separator_end..] ;
207
+ let path = & path. as_os_str_bytes ( ) [ separator_end..] ;
208
208
209
209
// SAFETY: `path` is a valid wtf8 encoded slice and each of the separators ('/', '\')
210
210
// is encoded in a single byte, therefore `bytes[separator_start]` and
@@ -329,7 +329,7 @@ pub(crate) fn absolute(path: &Path) -> io::Result<PathBuf> {
329
329
// Verbatim paths should not be modified.
330
330
if prefix. map ( |x| x. is_verbatim ( ) ) . unwrap_or ( false ) {
331
331
// NULs in verbatim paths are rejected for consistency.
332
- if path. bytes ( ) . contains ( & 0 ) {
332
+ if path. as_os_str_bytes ( ) . contains ( & 0 ) {
333
333
return Err ( io:: const_io_error!(
334
334
io:: ErrorKind :: InvalidInput ,
335
335
"strings passed to WinAPI cannot contain NULs" ,
0 commit comments