Skip to content

Commit 042a522

Browse files
committed
core: rewrite str::byte_index to use vec functions
1 parent d1c9b16 commit 042a522

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

src/comp/syntax/codemap.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ fn span_to_lines(sp: span, cm: codemap::codemap) -> @file_lines {
157157

158158
fn 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
};

src/libcore/str.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export
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 += 1u;
867-
}
868-
ret none;
863+
fn byte_index(s: str, b: u8) -> option<uint> {
864+
byte_index_from(s, b, 0u, 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

0 commit comments

Comments
 (0)