core: rewrite str::byte_index to use vec functions
This commit is contained in:
parent
d1c9b160ad
commit
042a5222d1
2 changed files with 16 additions and 8 deletions
|
|
@ -157,7 +157,8 @@ fn span_to_lines(sp: span, cm: codemap::codemap) -> @file_lines {
|
|||
|
||||
fn get_line(fm: filemap, line: int) -> str unsafe {
|
||||
let begin: uint = fm.lines[line].byte - fm.start_pos.byte;
|
||||
let end = alt str::byte_index(*fm.src, '\n' as u8, begin) {
|
||||
let end = alt str::byte_index_from(*fm.src, '\n' as u8, begin,
|
||||
str::len(*fm.src)) {
|
||||
some(e) { e }
|
||||
none { str::len(*fm.src) }
|
||||
};
|
||||
|
|
|
|||
|
|
@ -71,6 +71,7 @@ export
|
|||
// Searching
|
||||
index,
|
||||
byte_index,
|
||||
byte_index_from,
|
||||
rindex,
|
||||
find,
|
||||
find_bytes,
|
||||
|
|
@ -859,13 +860,19 @@ fn index(ss: str, cc: char) -> option<uint> {
|
|||
//
|
||||
// Returns the index of the first matching byte
|
||||
// (as option some/none)
|
||||
fn byte_index(s: str, b: u8, start: uint) -> option<uint> {
|
||||
let i = start, l = len_bytes(s);
|
||||
while i < l {
|
||||
if s[i] == b { ret some(i); }
|
||||
i += 1u;
|
||||
}
|
||||
ret none;
|
||||
fn byte_index(s: str, b: u8) -> option<uint> {
|
||||
byte_index_from(s, b, 0u, len_bytes(s))
|
||||
}
|
||||
|
||||
// Function: byte_index_from
|
||||
//
|
||||
// Returns the index of the first matching byte within the range [`start`,
|
||||
// `end`).
|
||||
// (as option some/none)
|
||||
fn byte_index_from(s: str, b: u8, start: uint, end: uint) -> option<uint> {
|
||||
assert end <= len_bytes(s);
|
||||
|
||||
str::as_bytes(s) { |v| vec::position_from(v, start, end) { |x| x == b } }
|
||||
}
|
||||
|
||||
// Function: rindex
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue