direct indexing into the slice

This commit is contained in:
Folkert 2022-05-15 00:17:12 +02:00
parent 8f97c217a5
commit c6b13984ed
No known key found for this signature in database
GPG key ID: 1F17F6FFD112B97C

View file

@ -279,6 +279,7 @@ fn eat_line_comment<'a>(
let mut index = 0; let mut index = 0;
let bytes = state.bytes(); let bytes = state.bytes();
let length = bytes.len();
'outer: loop { 'outer: loop {
let is_doc_comment = if let Some(b'#') = bytes.get(index) { let is_doc_comment = if let Some(b'#') = bytes.get(index) {
@ -351,15 +352,12 @@ fn eat_line_comment<'a>(
let loop_start = index; let loop_start = index;
let initial = state.bytes(); while index < length {
let mut it = initial.iter(); match bytes[index] {
while let Some(c) = it.next() {
match c {
b'\t' => return HasTab(state), b'\t' => return HasTab(state),
b'\n' => { b'\n' => {
let delta = initial.len() - state.bytes().len(); let comment =
let comment = unsafe { std::str::from_utf8_unchecked(&initial[..delta]) }; unsafe { std::str::from_utf8_unchecked(&bytes[loop_start..index]) };
if is_doc_comment { if is_doc_comment {
comments_and_newlines.push(CommentOrNewline::DocComment(comment)); comments_and_newlines.push(CommentOrNewline::DocComment(comment));
@ -370,8 +368,8 @@ fn eat_line_comment<'a>(
multiline = true; multiline = true;
index += 1; index += 1;
while let Some(c) = it.next() { while index < length {
match c { match bytes[index] {
b' ' => { b' ' => {
state = state.advance(1); state = state.advance(1);
} }
@ -415,8 +413,7 @@ fn eat_line_comment<'a>(
} }
// We made it to the end of the bytes. This means there's a comment without a trailing newline. // We made it to the end of the bytes. This means there's a comment without a trailing newline.
let delta = initial.len() - state.bytes().len(); let comment = unsafe { std::str::from_utf8_unchecked(&bytes[loop_start..index]) };
let comment = unsafe { std::str::from_utf8_unchecked(&bytes[loop_start..delta]) };
if is_doc_comment { if is_doc_comment {
comments_and_newlines.push(CommentOrNewline::DocComment(comment)); comments_and_newlines.push(CommentOrNewline::DocComment(comment));