@@ -32,6 +32,8 @@ trait GenericPath {
32
32
pure fn push_rel ( ( & self ) ) -> self ;
33
33
pure fn push_many ( ( & [ ~str ] ) ) -> self ;
34
34
pure fn pop ( ) -> self ;
35
+
36
+ pure fn normalize ( ) -> self ;
35
37
}
36
38
37
39
#[ cfg ( windows ) ]
@@ -68,7 +70,7 @@ impl PosixPath : GenericPath {
68
70
let mut components = str :: split_nonempty( s , |c | c == '/' ) ;
69
71
let is_absolute = ( s. len ( ) != 0 && s[ 0 ] == '/' as u8 ) ;
70
72
return PosixPath { is_absolute : is_absolute,
71
- components : normalize ( components) }
73
+ components : components }
72
74
}
73
75
74
76
pure fn dirname ( ) -> ~str {
@@ -175,14 +177,13 @@ impl PosixPath : GenericPath {
175
177
}
176
178
177
179
pure fn push_many ( cs : & [ ~str ] ) -> PosixPath {
178
- return PosixPath { components : normalize ( self . components + cs) ,
180
+ return PosixPath { components : self . components + cs,
179
181
..self }
180
182
}
181
183
182
184
pure fn push ( s : & str ) -> PosixPath {
183
185
let mut cs = self . components ;
184
186
unchecked { vec : : push ( cs, move str:: from_slice ( s) ) ; }
185
- cs = normalize ( cs) ;
186
187
return PosixPath { components : move cs,
187
188
..self }
188
189
}
@@ -194,6 +195,13 @@ impl PosixPath : GenericPath {
194
195
}
195
196
return PosixPath { components : move cs, ..self }
196
197
}
198
+
199
+ pure fn normalize ( ) -> PosixPath {
200
+ return PosixPath {
201
+ components : normalize ( self . components ) ,
202
+ ..self
203
+ }
204
+ }
197
205
}
198
206
199
207
@@ -251,7 +259,7 @@ impl WindowsPath : GenericPath {
251
259
return WindowsPath { host : host,
252
260
device : device,
253
261
is_absolute : is_absolute,
254
- components : normalize ( components) }
262
+ components : components }
255
263
}
256
264
257
265
pure fn dirname ( ) -> ~str {
@@ -358,14 +366,13 @@ impl WindowsPath : GenericPath {
358
366
}
359
367
360
368
pure fn push_many ( cs : & [ ~str ] ) -> WindowsPath {
361
- return WindowsPath { components : normalize ( self . components + cs) ,
369
+ return WindowsPath { components : self . components + cs,
362
370
..self }
363
371
}
364
372
365
373
pure fn push ( s : & str ) -> WindowsPath {
366
374
let mut cs = self . components ;
367
375
unchecked { vec : : push ( cs, move str:: from_slice ( s) ) ; }
368
- cs = normalize ( cs) ;
369
376
return WindowsPath { components : move cs,
370
377
..self }
371
378
}
@@ -377,6 +384,13 @@ impl WindowsPath : GenericPath {
377
384
}
378
385
return WindowsPath { components : move cs, ..self }
379
386
}
387
+
388
+ pure fn normalize ( ) -> WindowsPath {
389
+ return WindowsPath {
390
+ components : normalize ( self . components ) ,
391
+ ..self
392
+ }
393
+ }
380
394
}
381
395
382
396
@@ -399,19 +413,22 @@ pure fn normalize(components: &[~str]) -> ~[~str] {
399
413
400
414
mod posix {
401
415
402
- #[test]
403
- fn test_posix_paths() {
404
- fn mk(s: &str) -> PosixPath { from_str::<PosixPath>(s) }
405
- fn t(wp: &PosixPath, s: &str) {
406
- let ss = wp.to_str();
407
- let sss = str::from_slice(s );
408
- if (ss != sss) {
409
- debug!(" got %s" , ss ) ;
410
- debug ! ( "expected %s" , sss ) ;
411
- assert ss == sss;
412
- }
416
+ #[cfg( test) ]
417
+ fn mk(s: &str) -> PosixPath { from_str::<PosixPath>(s) }
418
+
419
+ #[cfg(test)]
420
+ fn t(wp: &PosixPath, s: &str) {
421
+ let ss = wp.to_str( );
422
+ let sss = str::from_slice(s);
423
+ if (ss != sss) {
424
+ debug!(" got %s", ss ) ;
425
+ debug ! ( "expected %s" , sss) ;
426
+ assert ss == sss ;
413
427
}
428
+ }
414
429
430
+ #[ test]
431
+ fn test_posix_paths( ) {
415
432
t ( & ( mk ( "hi" ) ) , "hi" ) ;
416
433
t ( & ( mk ( "/lib" ) ) , "/lib" ) ;
417
434
t ( & ( mk ( "hi/there" ) ) , "hi/there" ) ;
@@ -425,13 +442,10 @@ mod posix {
425
442
. with_dirname ( "hi" ) ) , "hi/there.txt" ) ;
426
443
427
444
t ( & ( mk ( "hi/there.txt" )
428
- . with_dirname ( "." ) ) , "there.txt" ) ;
429
-
430
- t ( & ( mk ( "a/b/../c/././/../foo.txt/" ) ) ,
431
- "a/foo.txt" ) ;
445
+ . with_dirname ( "." ) ) , "./there.txt" ) ;
432
446
433
447
t ( & ( mk ( "a/b/c" )
434
- . push ( ".." ) ) , "a/b" ) ;
448
+ . push ( ".." ) ) , "a/b/c/.. " ) ;
435
449
436
450
t ( & ( mk ( "there.txt" )
437
451
. with_filetype ( "o" ) ) , "there.o" ) ;
@@ -461,6 +475,17 @@ mod posix {
461
475
462
476
}
463
477
478
+ #[ test]
479
+ fn test_normalize ( ) {
480
+ t ( & ( mk ( "hi/there.txt" )
481
+ . with_dirname ( "." ) . normalize ( ) ) , "there.txt" ) ;
482
+
483
+ t ( & ( mk ( "a/b/../c/././/../foo.txt/" ) . normalize ( ) ) ,
484
+ "a/foo.txt" ) ;
485
+
486
+ t ( & ( mk ( "a/b/c" )
487
+ . push ( ".." ) . normalize ( ) ) , "a/b" ) ;
488
+ }
464
489
}
465
490
466
491
// Various windows helpers, and tests for the impl.
0 commit comments