Skip to content

Commit faa513b

Browse files
committed
(core::str) fixed replace, fixed starts_with, and added more find/contains/replace test cases
1 parent 2ba44e2 commit faa513b

File tree

1 file changed

+62
-5
lines changed

1 file changed

+62
-5
lines changed

src/libcore/str.rs

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -667,9 +667,10 @@ fn replace(s: str, from: str, to: str) : is_not_empty(from) -> str unsafe {
667667
if idx == -1 {
668668
ret s;
669669
}
670-
ret slice(s, 0u, idx as uint) + to +
671-
replace(slice(s, idx as uint + len(from), len(s)),
672-
from, to);
670+
let before = unsafe::slice_bytes(s, 0u, idx as uint);
671+
let after = unsafe::slice_bytes(s, idx as uint + len_bytes(from),
672+
len_bytes(s));
673+
ret before + to + replace(after, from, to);
673674
}
674675
}
675676

@@ -932,8 +933,8 @@ haystack - The string to look in
932933
needle - The string to look for
933934
*/
934935
fn starts_with(haystack: str, needle: str) -> bool {
935-
let haystack_len: uint = len_bytes(haystack);
936-
let needle_len: uint = len_bytes(needle);
936+
let haystack_len: uint = len(haystack);
937+
let needle_len: uint = len(needle);
937938
if needle_len == 0u { ret true; }
938939
if needle_len > haystack_len { ret false; }
939940
ret eq(substr(haystack, 0u, needle_len), needle);
@@ -1715,6 +1716,13 @@ mod tests {
17151716
t("this is a simple", "", 0);
17161717
t("this is a simple", "simple", 10);
17171718
t("this", "simple", -1);
1719+
1720+
// FIXME: return option<char> position instead
1721+
let data = "ประเทศไทย中华Việt Nam";
1722+
assert (find(data, "ประเ") == 0);
1723+
assert (find(data, "ะเ") == 6); // byte position
1724+
assert (find(data, "中华") == 27); // byte position
1725+
assert (find(data, "ไท华") == -1);
17181726
}
17191727

17201728
#[test]
@@ -1832,6 +1840,49 @@ mod tests {
18321840
assert (replace(" test test ", test, "") == " ");
18331841
}
18341842

1843+
#[test]
1844+
fn test_replace_2a() {
1845+
let data = "ประเทศไทย中华";
1846+
let repl = "دولة الكويت";
1847+
1848+
let a = "ประเ";
1849+
let A = "دولة الكويتทศไทย中华";
1850+
check is_not_empty(a);
1851+
assert (replace(data, a, repl) == A);
1852+
}
1853+
1854+
#[test]
1855+
fn test_replace_2b() {
1856+
let data = "ประเทศไทย中华";
1857+
let repl = "دولة الكويت";
1858+
1859+
let b = "ะเ";
1860+
let B = "ปรدولة الكويتทศไทย中华";
1861+
check is_not_empty(b);
1862+
assert (replace(data, b, repl) == B);
1863+
}
1864+
1865+
#[test]
1866+
fn test_replace_2c() {
1867+
let data = "ประเทศไทย中华";
1868+
let repl = "دولة الكويت";
1869+
1870+
let c = "中华";
1871+
let C = "ประเทศไทยدولة الكويت";
1872+
check is_not_empty(c);
1873+
assert (replace(data, c, repl) == C);
1874+
}
1875+
1876+
#[test]
1877+
fn test_replace_2d() {
1878+
let data = "ประเทศไทย中华";
1879+
let repl = "دولة الكويت";
1880+
1881+
let d = "ไท华";
1882+
check is_not_empty(d);
1883+
assert (replace(data, d, repl) == data);
1884+
}
1885+
18351886
#[test]
18361887
fn test_slice() {
18371888
assert (eq("ab", slice("abc", 0u, 2u)));
@@ -2032,6 +2083,12 @@ mod tests {
20322083
assert contains("", "");
20332084
assert !contains("abcde", "def");
20342085
assert !contains("", "a");
2086+
2087+
let data = "ประเทศไทย中华Việt Nam";
2088+
assert contains(data, "ประเ");
2089+
assert contains(data, "ะเ");
2090+
assert contains(data, "中华");
2091+
assert !contains(data, "ไท华");
20352092
}
20362093

20372094
#[test]

0 commit comments

Comments
 (0)