Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement eat_until leveraging memchr in lexer #136585

Merged
merged 1 commit into from
Feb 6, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
@@ -4064,6 +4064,7 @@ name = "rustc_lexer"
version = "0.0.0"
dependencies = [
"expect-test",
"memchr",
"unicode-properties",
"unicode-xid",
]
1 change: 1 addition & 0 deletions compiler/rustc_lexer/Cargo.toml
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@ Rust lexer used by rustc. No stability guarantees are provided.

# Note that this crate purposefully does not depend on other rustc crates
[dependencies]
memchr = "2.7.4"
unicode-xid = "0.2.0"

[dependencies.unicode-properties]
7 changes: 7 additions & 0 deletions compiler/rustc_lexer/src/cursor.rs
Original file line number Diff line number Diff line change
@@ -103,4 +103,11 @@ impl<'a> Cursor<'a> {
self.bump();
}
}

pub(crate) fn eat_until(&mut self, byte: u8) {
self.chars = match memchr::memchr(byte, self.as_str().as_bytes()) {
Some(index) => self.as_str()[index..].chars(),
None => "".chars(),
}
}
}
4 changes: 2 additions & 2 deletions compiler/rustc_lexer/src/lib.rs
Original file line number Diff line number Diff line change
@@ -483,7 +483,7 @@ impl Cursor<'_> {
_ => None,
};

self.eat_while(|c| c != '\n');
self.eat_until(b'\n');
LineComment { doc_style }
}

@@ -888,7 +888,7 @@ impl Cursor<'_> {
// Skip the string contents and on each '#' character met, check if this is
// a raw string termination.
loop {
self.eat_while(|c| c != '"');
self.eat_until(b'"');

if self.is_eof() {
return Err(RawStrError::NoTerminator {
Loading