File tree Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change @@ -870,6 +870,30 @@ fn lines_iter(ss: str, ff: fn(&&str)) {
870
870
Section: Searching
871
871
*/
872
872
873
+ // Function: index
874
+ //
875
+ // Returns the index of the first matching char
876
+ // (as option some/none)
877
+ fn index ( ss : str , cc : char ) -> option < uint > {
878
+ let bii = 0 u;
879
+ let cii = 0 u;
880
+ let len = byte_len ( ss) ;
881
+ while bii < len {
882
+ let { ch, next} = char_range_at ( ss, bii) ;
883
+
884
+ // found here?
885
+ if ch == cc {
886
+ ret option:: some ( cii) ;
887
+ }
888
+
889
+ cii += 1 u;
890
+ bii = next;
891
+ }
892
+
893
+ // wasn't found
894
+ ret option:: none;
895
+ }
896
+
873
897
/*
874
898
Function: index
875
899
@@ -1448,6 +1472,9 @@ mod tests {
1448
1472
assert ( index_byte ( "hello" , 'e' as u8 ) == 1 ) ;
1449
1473
assert ( index_byte ( "hello" , 'o' as u8 ) == 4 ) ;
1450
1474
assert ( index_byte ( "hello" , 'z' as u8 ) == -1 ) ;
1475
+ assert ( index ( "hello" , 'e' ) == option:: some ( 1 u) ) ;
1476
+ assert ( index ( "hello" , 'o' ) == option:: some ( 4 u) ) ;
1477
+ assert ( index ( "hello" , 'z' ) == option:: none) ;
1451
1478
assert ( rindex_byte ( "hello" , 'l' as u8 ) == 3 ) ;
1452
1479
assert ( rindex_byte ( "hello" , 'h' as u8 ) == 0 ) ;
1453
1480
assert ( rindex_byte ( "hello" , 'z' as u8 ) == -1 ) ;
You can’t perform that action at this time.
0 commit comments