@@ -667,9 +667,10 @@ fn replace(s: str, from: str, to: str) : is_not_empty(from) -> str unsafe {
667
667
if idx == -1 {
668
668
ret s;
669
669
}
670
- ret slice( s, 0 u, 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, 0 u, 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) ;
673
674
}
674
675
}
675
676
@@ -932,8 +933,8 @@ haystack - The string to look in
932
933
needle - The string to look for
933
934
*/
934
935
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) ;
937
938
if needle_len == 0 u { ret true ; }
938
939
if needle_len > haystack_len { ret false ; }
939
940
ret eq( substr ( haystack, 0 u, needle_len) , needle) ;
@@ -1715,6 +1716,13 @@ mod tests {
1715
1716
t ( "this is a simple" , "" , 0 ) ;
1716
1717
t ( "this is a simple" , "simple" , 10 ) ;
1717
1718
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 ) ;
1718
1726
}
1719
1727
1720
1728
#[ test]
@@ -1832,6 +1840,49 @@ mod tests {
1832
1840
assert ( replace ( " test test " , test, "" ) == " " ) ;
1833
1841
}
1834
1842
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
+
1835
1886
#[ test]
1836
1887
fn test_slice ( ) {
1837
1888
assert ( eq ( "ab" , slice ( "abc" , 0 u, 2 u) ) ) ;
@@ -2032,6 +2083,12 @@ mod tests {
2032
2083
assert contains ( "" , "" ) ;
2033
2084
assert ! contains( "abcde" , "def" ) ;
2034
2085
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, "ไท华" ) ;
2035
2092
}
2036
2093
2037
2094
#[ test]
0 commit comments