Skip to content

Commit e0af23b

Browse files
committed
using str::rindex...
1 parent 5036087 commit e0af23b

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

src/fuzzer/fuzzer.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,10 +283,9 @@ fn check_variants_T<T: copy>(
283283
}
284284
}
285285

286-
fn last_part(filename: str) -> str unsafe {
287-
let ix = str::rindex_byte(filename, 47u8 /* '/' */);
288-
assert ix >= 0;
289-
str::unsafe::slice_bytes(filename, ix as uint + 1u, str::byte_len(filename) - 3u)
286+
fn last_part(filename: str) -> str {
287+
let ix = option::get(str::rindex(filename, '/'));
288+
str::slice(filename, ix + 1u, str::char_len(filename) - 3u)
290289
}
291290

292291
enum happiness { passed, cleanly_rejected(str), known_bug(str), failed(str), }

src/libstd/fs.rs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,22 @@ A path or fragment of a filesystem path
3232
*/
3333
type path = str;
3434

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, 0u, ii),
48+
basename: str::slice(pp, ii + 1u, str::char_len(pp))};
49+
}
50+
3551
/*
3652
Function: dirname
3753
@@ -43,13 +59,8 @@ The dirname of "/usr/share" will be "/usr", but the dirname of
4359
4460
If the path is not prefixed with a directory, then "." is returned.
4561
*/
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, 0u, i as uint);
62+
fn dirname(pp: path) -> path {
63+
ret splitDirnameBasename(pp).dirname;
5364
}
5465

5566
/*
@@ -63,18 +74,10 @@ path separators in the path then the returned path is identical to
6374
the provided path. If an empty path is provided or the path ends
6475
with a path separator then an empty path is returned.
6576
*/
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;
7579
}
7680

77-
7881
// FIXME: Need some typestate to avoid bounds check when len(pre) == 0
7982
/*
8083
Function: connect

0 commit comments

Comments
 (0)