@@ -9,8 +9,6 @@ for correctness, but some UTF-8 unsafe functions are also provided.
9
9
For some heavy-duty uses, we recommend trying std::rope.
10
10
*/
11
11
12
- import option:: { some, none} ;
13
-
14
12
export
15
13
// Creating a string
16
14
from_bytes,
@@ -71,11 +69,9 @@ export
71
69
// Searching
72
70
index,
73
71
byte_index,
74
- byte_index_from,
75
72
rindex,
76
73
find,
77
74
find_bytes,
78
- find_from_bytes,
79
75
contains,
80
76
starts_with,
81
77
ends_with,
@@ -669,8 +665,8 @@ fn replace(s: str, from: str, to: str) : is_not_empty(from) -> str unsafe {
669
665
} else {
670
666
let idx;
671
667
alt find_bytes( s, from) {
672
- some ( x) { idx = x; }
673
- none { ret s; }
668
+ option :: some ( x) { idx = x; }
669
+ option :: none { ret s; }
674
670
}
675
671
let before = unsafe :: slice_bytes ( s, 0 u, idx as uint ) ;
676
672
let after = unsafe :: slice_bytes ( s, idx as uint + len_bytes ( from) ,
@@ -846,34 +842,28 @@ fn index(ss: str, cc: char) -> option<uint> {
846
842
847
843
// found here?
848
844
if ch == cc {
849
- ret some ( cii) ;
845
+ ret option :: some ( cii) ;
850
846
}
851
847
852
848
cii += 1 u;
853
849
bii = next;
854
850
}
855
851
856
852
// wasn't found
857
- ret none;
853
+ ret option :: none;
858
854
}
859
855
860
856
// Function: byte_index
861
857
//
862
858
// Returns the index of the first matching byte
863
859
// (as option some/none)
864
- fn byte_index ( s : str , b : u8 ) -> option < uint > {
865
- byte_index_from ( s, b, 0 u, len_bytes ( s) )
866
- }
867
-
868
- // Function: byte_index_from
869
- //
870
- // Returns the index of the first matching byte within the range [`start`,
871
- // `end`).
872
- // (as option some/none)
873
- fn byte_index_from ( s : str , b : u8 , start : uint , end : uint ) -> option < uint > {
874
- assert end <= len_bytes ( s) ;
875
-
876
- str:: as_bytes ( s) { |v| vec:: position_from ( v, start, end) { |x| x == b } }
860
+ fn byte_index ( s : str , b : u8 , start : uint ) -> option < uint > {
861
+ let i = start, l = len_bytes ( s) ;
862
+ while i < l {
863
+ if s[ i] == b { ret some ( i) ; }
864
+ i += 1 u;
865
+ }
866
+ ret none;
877
867
}
878
868
879
869
// Function: rindex
@@ -890,50 +880,40 @@ fn rindex(ss: str, cc: char) -> option<uint> {
890
880
891
881
// found here?
892
882
if ch == cc {
893
- ret some ( cii) ;
883
+ ret option :: some ( cii) ;
894
884
}
895
885
}
896
886
897
887
// wasn't found
898
- ret none;
888
+ ret option :: none;
899
889
}
900
890
901
891
//Function: find_bytes
902
892
//
903
893
// Find the char position of the first instance of one string
904
894
// within another, or return option::none
905
- fn find_bytes ( haystack : str , needle : str ) -> option < uint > {
906
- find_from_bytes ( haystack, needle, 0 u, len_bytes ( haystack) )
907
- }
908
-
909
- //Function: find_from_bytes
910
- //
911
- // Find the char position of the first instance of one string
912
- // within another, or return option::none
913
895
//
914
896
// FIXME: Boyer-Moore should be significantly faster
915
- fn find_from_bytes ( haystack : str , needle : str , start : uint , end : uint )
916
- -> option < uint > {
917
- assert end <= len_bytes ( haystack) ;
918
-
919
- let needle_len = len_bytes ( needle) ;
897
+ fn find_bytes ( haystack : str , needle : str ) -> option < uint > {
898
+ let haystack_len = len_bytes ( haystack) ;
899
+ let needle_len = len_bytes ( needle) ;
920
900
921
- if needle_len == 0 u { ret some ( start ) ; }
922
- if needle_len > end { ret none; }
901
+ if needle_len == 0 u { ret option :: some ( 0 u ) ; }
902
+ if needle_len > haystack_len { ret option :: none; }
923
903
924
904
fn match_at ( haystack : str , needle : str , ii : uint ) -> bool {
925
905
let jj = ii;
926
906
for c: u8 in needle { if haystack[ jj] != c { ret false ; } jj += 1 u; }
927
907
ret true;
928
908
}
929
909
930
- let ii = start ;
931
- while ii <= end - needle_len {
932
- if match_at ( haystack, needle, ii) { ret some ( ii) ; }
910
+ let ii = 0 u ;
911
+ while ii <= haystack_len - needle_len {
912
+ if match_at ( haystack, needle, ii) { ret option :: some ( ii) ; }
933
913
ii += 1 u;
934
914
}
935
915
936
- ret none;
916
+ ret option :: none;
937
917
}
938
918
939
919
// Function: find
@@ -942,8 +922,8 @@ fn find_from_bytes(haystack: str, needle: str, start: uint, end:uint)
942
922
// within another, or return option::none
943
923
fn find ( haystack : str , needle : str ) -> option < uint > {
944
924
alt find_bytes ( haystack, needle) {
945
- none { ret none; }
946
- some ( nn) { ret some ( b2c_pos ( haystack, nn) ) ; }
925
+ option :: none { ret option :: none; }
926
+ option :: some ( nn) { ret option :: some ( b2c_pos ( haystack, nn) ) ; }
947
927
}
948
928
}
949
929
@@ -1542,18 +1522,18 @@ mod tests {
1542
1522
1543
1523
#[ test]
1544
1524
fn test_index ( ) {
1545
- assert ( index ( "hello" , 'h' ) == some ( 0 u) ) ;
1546
- assert ( index ( "hello" , 'e' ) == some ( 1 u) ) ;
1547
- assert ( index ( "hello" , 'o' ) == some ( 4 u) ) ;
1548
- assert ( index ( "hello" , 'z' ) == none) ;
1525
+ assert ( index ( "hello" , 'h' ) == option :: some ( 0 u) ) ;
1526
+ assert ( index ( "hello" , 'e' ) == option :: some ( 1 u) ) ;
1527
+ assert ( index ( "hello" , 'o' ) == option :: some ( 4 u) ) ;
1528
+ assert ( index ( "hello" , 'z' ) == option :: none) ;
1549
1529
}
1550
1530
1551
1531
#[ test]
1552
1532
fn test_rindex ( ) {
1553
- assert ( rindex ( "hello" , 'l' ) == some ( 3 u) ) ;
1554
- assert ( rindex ( "hello" , 'o' ) == some ( 4 u) ) ;
1555
- assert ( rindex ( "hello" , 'h' ) == some ( 0 u) ) ;
1556
- assert ( rindex ( "hello" , 'z' ) == none) ;
1533
+ assert ( rindex ( "hello" , 'l' ) == option :: some ( 3 u) ) ;
1534
+ assert ( rindex ( "hello" , 'o' ) == option :: some ( 4 u) ) ;
1535
+ assert ( rindex ( "hello" , 'h' ) == option :: some ( 0 u) ) ;
1536
+ assert ( rindex ( "hello" , 'z' ) == option :: none) ;
1557
1537
}
1558
1538
1559
1539
#[ test]
@@ -1757,57 +1737,29 @@ mod tests {
1757
1737
#[ test]
1758
1738
fn test_find_bytes ( ) {
1759
1739
// byte positions
1760
- assert ( find_bytes ( "banana" , "apple pie" ) == none) ;
1761
- assert ( find_bytes ( "" , "" ) == some ( 0 u) ) ;
1740
+ assert ( find_bytes ( "banana" , "apple pie" ) == option :: none) ;
1741
+ assert ( find_bytes ( "" , "" ) == option :: some ( 0 u) ) ;
1762
1742
1763
1743
let data = "ประเทศไทย中华Việt Nam" ;
1764
- assert ( find_bytes ( data, "" ) == some ( 0 u) ) ;
1765
- assert ( find_bytes ( data, "ประเ" ) == some ( 0 u) ) ;
1766
- assert ( find_bytes ( data, "ะเ" ) == some ( 6 u) ) ;
1767
- assert ( find_bytes ( data, "中华" ) == some ( 27 u) ) ;
1768
- assert ( find_bytes ( data, "ไท华" ) == none) ;
1769
- }
1770
-
1771
- #[ test]
1772
- fn test_find_from_bytes ( ) {
1773
- // byte positions
1774
- assert ( find_from_bytes ( "" , "" , 0 u, 0 u) == some ( 0 u) ) ;
1775
-
1776
- let data = "abcabc" ;
1777
- assert find_from_bytes ( data, "ab" , 0 u, 6 u) == some ( 0 u) ;
1778
- assert find_from_bytes ( data, "ab" , 2 u, 6 u) == some ( 3 u) ;
1779
- assert find_from_bytes ( data, "ab" , 2 u, 4 u) == none;
1780
-
1781
- let data = "ประเทศไทย中华Việt Nam" ;
1782
- data += data;
1783
- assert find_from_bytes ( data, "" , 0 u, 43 u) == some ( 0 u) ;
1784
- assert find_from_bytes ( data, "" , 6 u, 43 u) == some ( 6 u) ;
1785
-
1786
- assert find_from_bytes ( data, "ประ" , 0 u, 43 u) == some ( 0 u) ;
1787
- assert find_from_bytes ( data, "ทศไ" , 0 u, 43 u) == some ( 12 u) ;
1788
- assert find_from_bytes ( data, "ย中" , 0 u, 43 u) == some ( 24 u) ;
1789
- assert find_from_bytes ( data, "iệt" , 0 u, 43 u) == some ( 34 u) ;
1790
- assert find_from_bytes ( data, "Nam" , 0 u, 43 u) == some ( 40 u) ;
1791
-
1792
- assert find_from_bytes ( data, "ประ" , 43 u, 86 u) == some ( 43 u) ;
1793
- assert find_from_bytes ( data, "ทศไ" , 43 u, 86 u) == some ( 55 u) ;
1794
- assert find_from_bytes ( data, "ย中" , 43 u, 86 u) == some ( 67 u) ;
1795
- assert find_from_bytes ( data, "iệt" , 43 u, 86 u) == some ( 77 u) ;
1796
- assert find_from_bytes ( data, "Nam" , 43 u, 86 u) == some ( 83 u) ;
1744
+ assert ( find_bytes ( data, "" ) == option:: some ( 0 u) ) ;
1745
+ assert ( find_bytes ( data, "ประเ" ) == option:: some ( 0 u) ) ;
1746
+ assert ( find_bytes ( data, "ะเ" ) == option:: some ( 6 u) ) ;
1747
+ assert ( find_bytes ( data, "中华" ) == option:: some ( 27 u) ) ;
1748
+ assert ( find_bytes ( data, "ไท华" ) == option:: none) ;
1797
1749
}
1798
1750
1799
1751
#[ test]
1800
1752
fn test_find ( ) {
1801
1753
// char positions
1802
- assert ( find ( "banana" , "apple pie" ) == none) ;
1803
- assert ( find ( "" , "" ) == some ( 0 u) ) ;
1754
+ assert ( find ( "banana" , "apple pie" ) == option :: none) ;
1755
+ assert ( find ( "" , "" ) == option :: some ( 0 u) ) ;
1804
1756
1805
1757
let data = "ประเทศไทย中华Việt Nam" ;
1806
- assert ( find ( data, "" ) == some ( 0 u) ) ;
1807
- assert ( find ( data, "ประเ" ) == some ( 0 u) ) ;
1808
- assert ( find ( data, "ะเ" ) == some ( 2 u) ) ;
1809
- assert ( find ( data, "中华" ) == some ( 9 u) ) ;
1810
- assert ( find ( data, "ไท华" ) == none) ;
1758
+ assert ( find ( data, "" ) == option :: some ( 0 u) ) ;
1759
+ assert ( find ( data, "ประเ" ) == option :: some ( 0 u) ) ;
1760
+ assert ( find ( data, "ะเ" ) == option :: some ( 2 u) ) ;
1761
+ assert ( find ( data, "中华" ) == option :: some ( 9 u) ) ;
1762
+ assert ( find ( data, "ไท华" ) == option :: none) ;
1811
1763
}
1812
1764
1813
1765
#[ test]
0 commit comments