@@ -158,15 +158,15 @@ pub trait GenericPath: Clone + GenericPathUnsafe {
158
158
/// See individual Path impls for additional restrictions.
159
159
#[ inline]
160
160
fn new < T : BytesContainer > ( path : T ) -> Self {
161
- assert ! ( !contains_nul( path. container_as_bytes ( ) ) ) ;
161
+ assert ! ( !contains_nul( & path) ) ;
162
162
unsafe { GenericPathUnsafe :: new_unchecked ( path) }
163
163
}
164
164
165
165
/// Creates a new Path from a byte vector or string, if possible.
166
166
/// The resulting Path will always be normalized.
167
167
#[ inline]
168
168
fn new_opt < T : BytesContainer > ( path : T ) -> Option < Self > {
169
- if contains_nul ( path. container_as_bytes ( ) ) {
169
+ if contains_nul ( & path) {
170
170
None
171
171
} else {
172
172
Some ( unsafe { GenericPathUnsafe :: new_unchecked ( path) } )
@@ -274,7 +274,7 @@ pub trait GenericPath: Clone + GenericPathUnsafe {
274
274
/// Fails the task if the filename contains a NUL.
275
275
#[ inline]
276
276
fn set_filename < T : BytesContainer > ( & mut self , filename : T ) {
277
- assert ! ( !contains_nul( filename. container_as_bytes ( ) ) ) ;
277
+ assert ! ( !contains_nul( & filename) ) ;
278
278
unsafe { self . set_filename_unchecked ( filename) }
279
279
}
280
280
/// Replaces the extension with the given byte vector or string.
@@ -286,43 +286,30 @@ pub trait GenericPath: Clone + GenericPathUnsafe {
286
286
///
287
287
/// Fails the task if the extension contains a NUL.
288
288
fn set_extension < T : BytesContainer > ( & mut self , extension : T ) {
289
- assert ! ( !contains_nul( extension. container_as_bytes( ) ) ) ;
290
- // borrowck causes problems here too
291
- let val = {
292
- match self . filename ( ) {
293
- None => None ,
294
- Some ( name) => {
295
- let dot = '.' as u8 ;
296
- match name. rposition_elem ( & dot) {
297
- None | Some ( 0 ) => {
298
- if extension. container_as_bytes ( ) . is_empty ( ) {
299
- None
300
- } else {
301
- let mut v;
302
- let extension = extension. container_as_bytes ( ) ;
303
- v = slice:: with_capacity ( name. len ( ) + extension. len ( ) + 1 ) ;
304
- v. push_all ( name) ;
305
- v. push ( dot) ;
306
- v. push_all ( extension) ;
307
- Some ( v)
308
- }
309
- }
310
- Some ( idx) => {
311
- if extension. container_as_bytes ( ) . is_empty ( ) {
312
- Some ( name. slice_to ( idx) . to_owned ( ) )
313
- } else {
314
- let mut v;
315
- let extension = extension. container_as_bytes ( ) ;
316
- v = slice:: with_capacity ( idx + extension. len ( ) + 1 ) ;
317
- v. push_all ( name. slice_to ( idx+1 ) ) ;
318
- v. push_all ( extension) ;
319
- Some ( v)
320
- }
321
- }
322
- }
289
+ assert ! ( !contains_nul( & extension) ) ;
290
+
291
+ let val = self . filename ( ) . and_then ( |name| {
292
+ let dot = '.' as u8 ;
293
+ let extlen = extension. container_as_bytes ( ) . len ( ) ;
294
+ match ( name. rposition_elem ( & dot) , extlen) {
295
+ ( None , 0 ) | ( Some ( 0 ) , 0 ) => None ,
296
+ ( Some ( idx) , 0 ) => Some ( name. slice_to ( idx) . to_owned ( ) ) ,
297
+ ( idx, extlen) => {
298
+ let idx = match idx {
299
+ None | Some ( 0 ) => name. len ( ) ,
300
+ Some ( val) => val
301
+ } ;
302
+
303
+ let mut v;
304
+ v = slice:: with_capacity ( idx + extlen + 1 ) ;
305
+ v. push_all ( name. slice_to ( idx) ) ;
306
+ v. push ( dot) ;
307
+ v. push_all ( extension. container_as_bytes ( ) ) ;
308
+ Some ( v)
323
309
}
324
310
}
325
- } ;
311
+ } ) ;
312
+
326
313
match val {
327
314
None => ( ) ,
328
315
Some ( v) => unsafe { self . set_filename_unchecked ( v) }
@@ -376,7 +363,7 @@ pub trait GenericPath: Clone + GenericPathUnsafe {
376
363
/// Fails the task if the path contains a NUL.
377
364
#[ inline]
378
365
fn push < T : BytesContainer > ( & mut self , path : T ) {
379
- assert ! ( !contains_nul( path. container_as_bytes ( ) ) ) ;
366
+ assert ! ( !contains_nul( & path) ) ;
380
367
unsafe { self . push_unchecked ( path) }
381
368
}
382
369
/// Pushes multiple paths (as byte vectors or strings) onto `self`.
@@ -589,8 +576,8 @@ impl<'a> BytesContainer for str::MaybeOwned<'a> {
589
576
}
590
577
591
578
#[ inline( always) ]
592
- fn contains_nul ( v : & [ u8 ] ) -> bool {
593
- v. iter ( ) . any ( |& x| x == 0 )
579
+ fn contains_nul < T : BytesContainer > ( v : & T ) -> bool {
580
+ v. container_as_bytes ( ) . iter ( ) . any ( |& x| x == 0 )
594
581
}
595
582
596
583
#[ cfg( test) ]
0 commit comments