Skip to content

Commit

Permalink
perf: improve consume-line-comments and consume-block-comments (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
IWANABETHATGUY authored Jul 20, 2024
1 parent 80dab57 commit befb6da
Showing 1 changed file with 41 additions and 13 deletions.
54 changes: 41 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ fn consume_comment_whitespace_until_maybe_bracket(
it: &mut IterMut<u8>,
settings: CommentSettings,
) -> Result<bool> {
for c in it.by_ref() {
while let Some(c) = it.next() {
*state = match state {
Top => {
*state = top(c, settings);
Expand All @@ -142,9 +142,9 @@ fn consume_comment_whitespace_until_maybe_bracket(
InString => in_string(*c),
StringEscape => InString,
InComment => in_comment(c, settings)?,
InBlockComment => in_block_comment(c),
InBlockComment => consume_block_comments(it),
MaybeCommentEnd => maybe_comment_end(c),
InLineComment => in_line_comment(c),
InLineComment => consume_line_comments(it),
};
}
Ok(false)
Expand Down Expand Up @@ -174,13 +174,49 @@ fn strip_buf(
InComment => in_comment(c, settings)?,
InBlockComment => in_block_comment(c),
MaybeCommentEnd => maybe_comment_end(c),
InLineComment => in_line_comment(c),
InLineComment => {
if *c == b'\n' {
Top
} else {
*c = b' ';
consume_line_comments(&mut it)
}
}
}
}
}
Ok(())
}

#[inline]
fn consume_line_comments(it: &mut IterMut<u8>) -> State {
let mut ret = InLineComment;
for c in it.by_ref() {
if *c == b'\n' {
ret = Top;
break;
} else {
*c = b' ';
}
}
ret
}

#[inline]
fn consume_block_comments(it: &mut IterMut<u8>) -> State {
let mut ret = InBlockComment;
for c in it.by_ref() {
if *c == b'*' {
*c = b' ';
ret = MaybeCommentEnd;
break;
} else {
*c = b' ';
}
}
ret
}

/// Strips comments from a string in place, replacing it with whitespaces.
///
/// /// ## Example
Expand Down Expand Up @@ -314,6 +350,7 @@ impl CommentSettings {
}
}

#[inline]
fn top(c: &mut u8, settings: CommentSettings) -> State {
match *c {
b'"' => InString,
Expand Down Expand Up @@ -367,15 +404,6 @@ fn maybe_comment_end(c: &mut u8) -> State {
}
}

fn in_line_comment(c: &mut u8) -> State {
if *c == b'\n' {
Top
} else {
*c = b' ';
InLineComment
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit befb6da

Please sign in to comment.