Add std::char_slice

This commit is contained in:
Brian Anderson 2011-08-01 18:15:18 -07:00
parent 0c7a95fde5
commit 598e25e091
2 changed files with 13 additions and 0 deletions

View File

@ -53,6 +53,7 @@ export unsafe_from_bytes_ivec;
export is_empty;
export is_not_empty;
export replace;
export char_slice;
native "rust" mod rustrt {
type sbuf;
@ -518,6 +519,11 @@ fn replace(s: str, from: str, to: str) : is_not_empty(from) -> str {
}
}
// FIXME: Also not efficient
fn char_slice(s: &str, begin: uint, end: uint) -> str {
from_chars(vec::slice(to_chars(s), begin, end))
}
// Local Variables:
// mode: rust;
// fill-column: 78;

View File

@ -154,6 +154,13 @@ fn test_replace() {
assert (str::replace(" test test ", test, "") == " ");
}
#[test]
fn test_char_slice() {
assert (str::eq("ab", str::char_slice("abc", 0u, 2u)));
assert (str::eq("bc", str::char_slice("abc", 1u, 3u)));
assert (str::eq("", str::char_slice("abc", 1u, 1u)));
assert (str::eq("\u65e5", str::char_slice("\u65e5\u672c", 0u, 1u)));
}
// Local Variables:
// mode: rust;