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 { fn last_part(filename: str) -> str {
let ix = str::rindex_byte(filename, 47u8 /* '/' */); let ix = option::get(str::rindex(filename, '/'));
assert ix >= 0; str::slice(filename, ix + 1u, str::char_len(filename) - 3u)
str::unsafe::slice_bytes(filename, ix as uint + 1u, str::byte_len(filename) - 3u)
} }
enum happiness { passed, cleanly_rejected(str), known_bug(str), failed(str), } 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; 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 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. If the path is not prefixed with a directory, then "." is returned.
*/ */
fn dirname(p: path) -> path unsafe { fn dirname(pp: path) -> path {
let i: int = str::rindex_byte(p, os_fs::path_sep as u8); ret splitDirnameBasename(pp).dirname;
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);
} }
/* /*
@ -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 the provided path. If an empty path is provided or the path ends
with a path separator then an empty path is returned. with a path separator then an empty path is returned.
*/ */
fn basename(p: path) -> path unsafe { fn basename(pp: path) -> path {
let i: int = str::rindex_byte(p, os_fs::path_sep as u8); ret splitDirnameBasename(pp).basename;
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);
} }
// FIXME: Need some typestate to avoid bounds check when len(pre) == 0 // FIXME: Need some typestate to avoid bounds check when len(pre) == 0
/* /*
Function: connect Function: connect