using str::rindex...

This commit is contained in:
Kevin Cantu 2012-02-11 16:31:13 -08:00
parent 50360873f8
commit e0af23b664
2 changed files with 23 additions and 21 deletions

View File

@ -283,10 +283,9 @@ fn check_variants_T<T: copy>(
}
}
fn last_part(filename: str) -> str unsafe {
let ix = str::rindex_byte(filename, 47u8 /* '/' */);
assert ix >= 0;
str::unsafe::slice_bytes(filename, ix as uint + 1u, str::byte_len(filename) - 3u)
fn last_part(filename: str) -> str {
let ix = option::get(str::rindex(filename, '/'));
str::slice(filename, ix + 1u, str::char_len(filename) - 3u)
}
enum happiness { passed, cleanly_rejected(str), known_bug(str), failed(str), }

View File

@ -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,17 +74,9 @@ 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; }
fn basename(pp: path) -> path {
ret splitDirnameBasename(pp).basename;
}
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);
}
// FIXME: Need some typestate to avoid bounds check when len(pre) == 0
/*