diff options
| author | Kevin Cantu <me@kevincantu.org> | 2012-02-12 06:14:49 -0800 |
|---|---|---|
| committer | Kevin Cantu <me@kevincantu.org> | 2012-02-12 15:30:21 -0800 |
| commit | faa513b1f616fd831ca7ba2d3a8ad987f83d4a16 (patch) | |
| tree | fce58bb8fc7d495ecc79ca0f9e4ecf3be05d5e73 | |
| parent | 2ba44e24d5fccfc6fe7c28a742d18e03b6acafae (diff) | |
| download | rust-faa513b1f616fd831ca7ba2d3a8ad987f83d4a16.tar.gz rust-faa513b1f616fd831ca7ba2d3a8ad987f83d4a16.zip | |
(core::str) fixed replace, fixed starts_with, and added more find/contains/replace test cases
| -rw-r--r-- | src/libcore/str.rs | 67 |
1 files changed, 62 insertions, 5 deletions
diff --git a/src/libcore/str.rs b/src/libcore/str.rs index 522ae89d395..a527a79935c 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -667,9 +667,10 @@ fn replace(s: str, from: str, to: str) : is_not_empty(from) -> str unsafe { if idx == -1 { ret s; } - ret slice(s, 0u, idx as uint) + to + - replace(slice(s, idx as uint + len(from), len(s)), - from, to); + let before = unsafe::slice_bytes(s, 0u, idx as uint); + let after = unsafe::slice_bytes(s, idx as uint + len_bytes(from), + len_bytes(s)); + ret before + to + replace(after, from, to); } } @@ -932,8 +933,8 @@ haystack - The string to look in needle - The string to look for */ fn starts_with(haystack: str, needle: str) -> bool { - let haystack_len: uint = len_bytes(haystack); - let needle_len: uint = len_bytes(needle); + let haystack_len: uint = len(haystack); + let needle_len: uint = len(needle); if needle_len == 0u { ret true; } if needle_len > haystack_len { ret false; } ret eq(substr(haystack, 0u, needle_len), needle); @@ -1715,6 +1716,13 @@ mod tests { t("this is a simple", "", 0); t("this is a simple", "simple", 10); t("this", "simple", -1); + + // FIXME: return option<char> position instead + let data = "ประเทศไทย中华Việt Nam"; + assert (find(data, "ประเ") == 0); + assert (find(data, "ะเ") == 6); // byte position + assert (find(data, "中华") == 27); // byte position + assert (find(data, "ไท华") == -1); } #[test] @@ -1833,6 +1841,49 @@ mod tests { } #[test] + fn test_replace_2a() { + let data = "ประเทศไทย中华"; + let repl = "دولة الكويت"; + + let a = "ประเ"; + let A = "دولة الكويتทศไทย中华"; + check is_not_empty(a); + assert (replace(data, a, repl) == A); + } + + #[test] + fn test_replace_2b() { + let data = "ประเทศไทย中华"; + let repl = "دولة الكويت"; + + let b = "ะเ"; + let B = "ปรدولة الكويتทศไทย中华"; + check is_not_empty(b); + assert (replace(data, b, repl) == B); + } + + #[test] + fn test_replace_2c() { + let data = "ประเทศไทย中华"; + let repl = "دولة الكويت"; + + let c = "中华"; + let C = "ประเทศไทยدولة الكويت"; + check is_not_empty(c); + assert (replace(data, c, repl) == C); + } + + #[test] + fn test_replace_2d() { + let data = "ประเทศไทย中华"; + let repl = "دولة الكويت"; + + let d = "ไท华"; + check is_not_empty(d); + assert (replace(data, d, repl) == data); + } + + #[test] fn test_slice() { assert (eq("ab", slice("abc", 0u, 2u))); assert (eq("bc", slice("abc", 1u, 3u))); @@ -2032,6 +2083,12 @@ mod tests { assert contains("", ""); assert !contains("abcde", "def"); assert !contains("", "a"); + + let data = "ประเทศไทย中华Việt Nam"; + assert contains(data, "ประเ"); + assert contains(data, "ะเ"); + assert contains(data, "中华"); + assert !contains(data, "ไท华"); } #[test] |
