diff options
| author | Kevin Cantu <me@kevincantu.org> | 2012-02-11 16:31:13 -0800 |
|---|---|---|
| committer | Kevin Cantu <me@kevincantu.org> | 2012-02-11 16:39:39 -0800 |
| commit | e0af23b664a1307fe376f2638bb7a69f04e2ac1c (patch) | |
| tree | ae0e96d893abf8e5df0b79e75b9fb13b9f0b9b3c /src/libstd | |
| parent | 50360873f8f7abbe7232cdd8f89d5ce691711acc (diff) | |
| download | rust-e0af23b664a1307fe376f2638bb7a69f04e2ac1c.tar.gz rust-e0af23b664a1307fe376f2638bb7a69f04e2ac1c.zip | |
using str::rindex...
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/fs.rs | 37 |
1 files changed, 20 insertions, 17 deletions
diff --git a/src/libstd/fs.rs b/src/libstd/fs.rs index 874d92cc4b9..de4789fdd39 100644 --- a/src/libstd/fs.rs +++ b/src/libstd/fs.rs @@ -32,6 +32,22 @@ A path or fragment of a filesystem path */ type path = str; +fn splitDirnameBasename (pp: path) -> {dirname: str, basename: str} { + let ii; + alt str::rindex(pp, os_fs::path_sep) { + option::some(xx) { ii = xx; } + option::none { + alt str::rindex(pp, os_fs::alt_path_sep) { + option::some(xx) { ii = xx; } + option::none { ret {dirname: ".", basename: pp}; } + } + } + } + + ret {dirname: str::slice(pp, 0u, ii), + basename: str::slice(pp, ii + 1u, str::char_len(pp))}; +} + /* Function: dirname @@ -43,13 +59,8 @@ The dirname of "/usr/share" will be "/usr", but the dirname of If the path is not prefixed with a directory, then "." is returned. */ -fn dirname(p: path) -> path unsafe { - let i: int = str::rindex_byte(p, os_fs::path_sep as u8); - if i == -1 { - i = str::rindex_byte(p, os_fs::alt_path_sep as u8); - if i == -1 { ret "."; } - } - ret str::unsafe::slice_bytes(p, 0u, i as uint); +fn dirname(pp: path) -> path { + ret splitDirnameBasename(pp).dirname; } /* @@ -63,18 +74,10 @@ path separators in the path then the returned path is identical to the provided path. If an empty path is provided or the path ends with a path separator then an empty path is returned. */ -fn basename(p: path) -> path unsafe { - let i: int = str::rindex_byte(p, os_fs::path_sep as u8); - if i == -1 { - i = str::rindex_byte(p, os_fs::alt_path_sep as u8); - if i == -1 { ret p; } - } - let len = str::byte_len(p); - if (i + 1) as uint >= len { ret p; } - ret str::unsafe::slice_bytes(p, (i + 1) as uint, len); +fn basename(pp: path) -> path { + ret splitDirnameBasename(pp).basename; } - // FIXME: Need some typestate to avoid bounds check when len(pre) == 0 /* Function: connect |
