Skip to content

Commit 20806b0

Browse files
committed
---
yaml --- r: 11234 b: refs/heads/master c: faa513b h: refs/heads/master v: v3
1 parent c53f594 commit 20806b0

File tree

2 files changed

+63
-6
lines changed

2 files changed

+63
-6
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: 2ba44e24d5fccfc6fe7c28a742d18e03b6acafae
2+
refs/heads/master: faa513b1f616fd831ca7ba2d3a8ad987f83d4a16
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 4a81779abd786ff22d71434c6d9a5917ea4cdfff
55
refs/heads/try: 2898dcc5d97da9427ac367542382b6239d9c0bbf

trunk/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)