72
72
index_chars,
73
73
byte_index,
74
74
byte_index_from,
75
+ //rindex,
75
76
rindex_chars,
76
- find ,
77
+ find_chars ,
77
78
find_bytes,
78
79
find_from_bytes,
79
80
contains,
@@ -861,7 +862,36 @@ Section: Searching
861
862
862
863
// Function: index
863
864
//
864
- // Returns the index of the first matching char
865
+ // Returns the byte index of the first matching char
866
+ // (as option some/none)
867
+ fn index ( ss : str , cc : char ) -> option < uint > {
868
+ index_from ( ss, cc, 0 u, len_bytes ( ss) )
869
+ }
870
+
871
+ // Function: index_from
872
+ //
873
+ // Returns the byte index of the first matching char
874
+ // (as option some/none), starting at `nn`
875
+ fn index_from ( ss : str , cc : char , start : uint , end : uint ) -> option < uint > {
876
+ let bii = start;
877
+ while bii < end {
878
+ let { ch, next} = char_range_at ( ss, bii) ;
879
+
880
+ // found here?
881
+ if ch == cc {
882
+ ret some ( bii) ;
883
+ }
884
+
885
+ bii = next;
886
+ }
887
+
888
+ // wasn't found
889
+ ret none;
890
+ }
891
+
892
+ // Function: index_chars
893
+ //
894
+ // Returns the char index of the first matching char
865
895
// (as option some/none)
866
896
fn index_chars ( ss : str , cc : char ) -> option < uint > {
867
897
let bii = 0 u;
@@ -887,6 +917,7 @@ fn index_chars(ss: str, cc: char) -> option<uint> {
887
917
//
888
918
// Returns the index of the first matching byte
889
919
// (as option some/none)
920
+ // FIXME: delete
890
921
fn byte_index ( s : str , b : u8 ) -> option < uint > {
891
922
byte_index_from ( s, b, 0 u, len_bytes ( s) )
892
923
}
@@ -896,15 +927,36 @@ fn byte_index(s: str, b: u8) -> option<uint> {
896
927
// Returns the index of the first matching byte within the range [`start`,
897
928
// `end`).
898
929
// (as option some/none)
930
+ // FIXME: delete
899
931
fn byte_index_from ( s : str , b : u8 , start : uint , end : uint ) -> option < uint > {
900
932
assert end <= len_bytes ( s) ;
901
933
902
934
str:: as_bytes ( s) { |v| vec:: position_from ( v, start, end) { |x| x == b } }
903
935
}
904
936
937
+ // Function: rindex
938
+ //
939
+ // Returns the byte index of the first matching char
940
+ // (as option some/none)
941
+ fn rindex ( ss : str , cc : char ) -> option < uint > {
942
+ let bii = len_bytes ( ss) ;
943
+ while bii > 0 u {
944
+ let { ch, prev} = char_range_at_reverse ( ss, bii) ;
945
+ bii = prev;
946
+
947
+ // found here?
948
+ if ch == cc {
949
+ ret some ( bii) ;
950
+ }
951
+ }
952
+
953
+ // wasn't found
954
+ ret none;
955
+ }
956
+
905
957
// Function: rindex_chars
906
958
//
907
- // Returns the index of the first matching char
959
+ // Returns the char index of the first matching char
908
960
// (as option some/none)
909
961
fn rindex_chars ( ss : str , cc : char ) -> option < uint > {
910
962
let bii = len_bytes ( ss) ;
@@ -926,15 +978,15 @@ fn rindex_chars(ss: str, cc: char) -> option<uint> {
926
978
927
979
//Function: find_bytes
928
980
//
929
- // Find the char position of the first instance of one string
981
+ // Find the byte position of the first instance of one string
930
982
// within another, or return option::none
931
983
fn find_bytes ( haystack : str , needle : str ) -> option < uint > {
932
984
find_from_bytes ( haystack, needle, 0 u, len_bytes ( haystack) )
933
985
}
934
986
935
987
//Function: find_from_bytes
936
988
//
937
- // Find the char position of the first instance of one string
989
+ // Find the byte position of the first instance of one string
938
990
// within another, or return option::none
939
991
//
940
992
// FIXME: Boyer-Moore should be significantly faster
@@ -962,11 +1014,11 @@ fn find_from_bytes(haystack: str, needle: str, start: uint, end:uint)
962
1014
ret none;
963
1015
}
964
1016
965
- // Function: find
1017
+ // Function: find_chars
966
1018
//
967
1019
// Find the char position of the first instance of one string
968
1020
// within another, or return option::none
969
- fn find ( haystack : str , needle : str ) -> option < uint > {
1021
+ fn find_chars ( haystack : str , needle : str ) -> option < uint > {
970
1022
alt find_bytes ( haystack, needle) {
971
1023
none { ret none; }
972
1024
some ( nn) { ret some ( b2c_pos ( haystack, nn) ) ; }
@@ -1570,6 +1622,15 @@ mod tests {
1570
1622
assert ( index_chars ( "hello" , 'z' ) == none) ;
1571
1623
}
1572
1624
1625
+ #[ test]
1626
+ fn test_rindex ( ) {
1627
+ assert rindex ( "hello" , 'l' ) == some ( 3 u) ;
1628
+ assert rindex ( "hello" , 'o' ) == some ( 4 u) ;
1629
+ assert rindex ( "hello" , 'h' ) == some ( 0 u) ;
1630
+ assert rindex ( "hello" , 'z' ) == none;
1631
+ assert rindex ( "ประเทศไทย中华Việt Nam" , '华' ) == some ( 30 u) ;
1632
+ }
1633
+
1573
1634
#[ test]
1574
1635
fn test_rindex_chars ( ) {
1575
1636
assert ( rindex_chars ( "hello" , 'l' ) == some ( 3 u) ) ;
@@ -1820,17 +1881,17 @@ mod tests {
1820
1881
}
1821
1882
1822
1883
#[ test]
1823
- fn test_find ( ) {
1884
+ fn test_find_chars ( ) {
1824
1885
// char positions
1825
- assert ( find ( "banana" , "apple pie" ) == none) ;
1826
- assert ( find ( "" , "" ) == some ( 0 u) ) ;
1886
+ assert ( find_chars ( "banana" , "apple pie" ) == none) ;
1887
+ assert ( find_chars ( "" , "" ) == some ( 0 u) ) ;
1827
1888
1828
1889
let data = "ประเทศไทย中华Việt Nam" ;
1829
- assert ( find ( data, "" ) == some ( 0 u) ) ;
1830
- assert ( find ( data, "ประเ" ) == some ( 0 u) ) ;
1831
- assert ( find ( data, "ะเ" ) == some ( 2 u) ) ;
1832
- assert ( find ( data, "中华" ) == some ( 9 u) ) ;
1833
- assert ( find ( data, "ไท华" ) == none) ;
1890
+ assert ( find_chars ( data, "" ) == some ( 0 u) ) ;
1891
+ assert ( find_chars ( data, "ประเ" ) == some ( 0 u) ) ;
1892
+ assert ( find_chars ( data, "ะเ" ) == some ( 2 u) ) ;
1893
+ assert ( find_chars ( data, "中华" ) == some ( 9 u) ) ;
1894
+ assert ( find_chars ( data, "ไท华" ) == none) ;
1834
1895
}
1835
1896
1836
1897
#[ test]
0 commit comments