@@ -32,6 +32,22 @@ A path or fragment of a filesystem path
32
32
*/
33
33
type path = str ;
34
34
35
+ fn splitDirnameBasename ( pp : path ) -> { dirname : str , basename : str } {
36
+ let ii;
37
+ alt str:: rindex ( pp, os_fs:: path_sep) {
38
+ option:: some ( xx) { ii = xx; }
39
+ option:: none {
40
+ alt str:: rindex ( pp, os_fs:: alt_path_sep) {
41
+ option:: some ( xx) { ii = xx; }
42
+ option:: none { ret { dirname : "." , basename : pp} ; }
43
+ }
44
+ }
45
+ }
46
+
47
+ ret { dirname : str:: slice ( pp, 0 u, ii) ,
48
+ basename : str:: slice ( pp, ii + 1 u, str:: char_len ( pp) ) } ;
49
+ }
50
+
35
51
/*
36
52
Function: dirname
37
53
@@ -43,13 +59,8 @@ The dirname of "/usr/share" will be "/usr", but the dirname of
43
59
44
60
If the path is not prefixed with a directory, then "." is returned.
45
61
*/
46
- fn dirname ( p : path ) -> path unsafe {
47
- let i: int = str:: rindex_byte ( p, os_fs:: path_sep as u8 ) ;
48
- if i == -1 {
49
- i = str:: rindex_byte ( p, os_fs:: alt_path_sep as u8 ) ;
50
- if i == -1 { ret "." ; }
51
- }
52
- ret str:: unsafe:: slice_bytes ( p, 0 u, i as uint ) ;
62
+ fn dirname ( pp : path ) -> path {
63
+ ret splitDirnameBasename ( pp) . dirname ;
53
64
}
54
65
55
66
/*
@@ -63,18 +74,10 @@ path separators in the path then the returned path is identical to
63
74
the provided path. If an empty path is provided or the path ends
64
75
with a path separator then an empty path is returned.
65
76
*/
66
- fn basename ( p : path ) -> path unsafe {
67
- let i: int = str:: rindex_byte ( p, os_fs:: path_sep as u8 ) ;
68
- if i == -1 {
69
- i = str:: rindex_byte ( p, os_fs:: alt_path_sep as u8 ) ;
70
- if i == -1 { ret p; }
71
- }
72
- let len = str:: byte_len ( p) ;
73
- if ( i + 1 ) as uint >= len { ret p; }
74
- ret str:: unsafe:: slice_bytes ( p, ( i + 1 ) as uint , len) ;
77
+ fn basename ( pp : path ) -> path {
78
+ ret splitDirnameBasename ( pp) . basename ;
75
79
}
76
80
77
-
78
81
// FIXME: Need some typestate to avoid bounds check when len(pre) == 0
79
82
/*
80
83
Function: connect
0 commit comments