(core::str) add a safe byte slice and maybe_slice

This commit is contained in:
Kevin Cantu 2012-02-22 00:49:05 -08:00 committed by Marijn Haverbeke
parent 454b53a7c2
commit 7c78b7dfed
1 changed files with 77 additions and 0 deletions

View File

@ -394,6 +394,31 @@ fn substr(s: str, begin: uint, len: uint) -> str {
ret slice_chars(s, begin, begin + len);
}
// Function: slice
//
// Return a slice of the given string from the byte range [`begin`..`end`)
// or else fail when `begin` and `end` do not point to valid characters or
// beyond the last character of the string
fn slice(ss: str, begin: uint, end: uint) -> str {
alt maybe_slice(ss, begin, end) {
none { fail "slice requires a valid start and end"; }
some(sli) { ret sli; }
}
}
// Function: maybe_slice
//
// Like slice, only returns an option<str>
fn maybe_slice(ss: str, begin: uint, end: uint) -> option<str> unsafe {
let sli = unsafe::slice_bytes(ss, begin, end);
if is_utf8(bytes(sli)) {
ret some(sli);
} else {
ret none;
}
}
/*
Function: slice_chars
@ -1968,6 +1993,58 @@ mod tests {
assert (replace(data, d, repl) == data);
}
#[test]
fn test_slice() {
assert (eq("ab", slice("abc", 0u, 2u)));
assert (eq("bc", slice("abc", 1u, 3u)));
assert (eq("", slice("abc", 1u, 1u)));
assert (eq("\u65e5", slice("\u65e5\u672c", 0u, 3u)));
let data = "ประเทศไทย中华";
assert (eq("", slice(data, 0u, 3u)));
assert (eq("", slice(data, 3u, 6u)));
assert (eq("", slice(data, 1u, 1u)));
assert (eq("", slice(data, 30u, 33u)));
fn a_million_letter_X() -> str {
let i = 0;
let rs = "";
while i < 100000 { rs += "华华华华华华华华华华"; i += 1; }
ret rs;
}
fn half_a_million_letter_X() -> str {
let i = 0;
let rs = "";
while i < 100000 { rs += "华华华华华"; i += 1; }
ret rs;
}
assert (eq(half_a_million_letter_X(),
slice(a_million_letter_X(), 0u, (3u * 500000u))));
}
#[test]
fn test_maybe_slice() {
let ss = "中华Việt Nam";
assert none == maybe_slice(ss, 0u, 2u);
assert none == maybe_slice(ss, 1u, 3u);
assert none == maybe_slice(ss, 1u, 2u);
assert some("") == maybe_slice(ss, 3u, 6u);
assert some("Việt Nam") == maybe_slice(ss, 6u, 16u);
assert none == maybe_slice(ss, 4u, 16u);
/* 0: 中
3:
6: V
7: i
8:
11: t
12:
13: N
14: a
15: m */
}
#[test]
fn test_slice_chars() {
assert (eq("ab", slice_chars("abc", 0u, 2u)));