File tree Expand file tree Collapse file tree 2 files changed +16
-8
lines changed
Expand file tree Collapse file tree 2 files changed +16
-8
lines changed Original file line number Diff line number Diff line change @@ -157,7 +157,8 @@ fn span_to_lines(sp: span, cm: codemap::codemap) -> @file_lines {
157157
158158fn get_line ( fm : filemap , line : int ) -> str unsafe {
159159 let begin: uint = fm. lines [ line] . byte - fm. start_pos . byte ;
160- let end = alt str:: byte_index ( * fm. src , '\n' as u8 , begin) {
160+ let end = alt str:: byte_index_from ( * fm. src , '\n' as u8 , begin,
161+ str:: len ( * fm. src ) ) {
161162 some ( e) { e }
162163 none { str : : len ( * fm. src ) }
163164 } ;
Original file line number Diff line number Diff line change 7171 // Searching
7272 index,
7373 byte_index,
74+ byte_index_from,
7475 rindex,
7576 find,
7677 find_bytes,
@@ -859,13 +860,19 @@ fn index(ss: str, cc: char) -> option<uint> {
859860//
860861// Returns the index of the first matching byte
861862// (as option some/none)
862- fn byte_index ( s : str , b : u8 , start : uint ) -> option < uint > {
863- let i = start, l = len_bytes ( s) ;
864- while i < l {
865- if s[ i] == b { ret some ( i) ; }
866- i += 1 u;
867- }
868- ret none;
863+ fn byte_index ( s : str , b : u8 ) -> option < uint > {
864+ byte_index_from ( s, b, 0 u, len_bytes ( s) )
865+ }
866+
867+ // Function: byte_index_from
868+ //
869+ // Returns the index of the first matching byte within the range [`start`,
870+ // `end`).
871+ // (as option some/none)
872+ fn byte_index_from ( s : str , b : u8 , start : uint , end : uint ) -> option < uint > {
873+ assert end <= len_bytes ( s) ;
874+
875+ str:: as_bytes ( s) { |v| vec:: position_from ( v, start, end) { |x| x == b } }
869876}
870877
871878// Function: rindex
You can’t perform that action at this time.
0 commit comments