Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 79f82ad

Browse files
committedFeb 6, 2025
Auto merge of #136585 - gvozdvmozgu:memchr-eat-until-lexer, r=lcnr
implement `eat_until` leveraging memchr in lexer
2 parents 2f92f05 + 9f469eb commit 79f82ad

File tree

4 files changed

+11
-2
lines changed

4 files changed

+11
-2
lines changed
 

‎Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -4064,6 +4064,7 @@ name = "rustc_lexer"
40644064
version = "0.0.0"
40654065
dependencies = [
40664066
"expect-test",
4067+
"memchr",
40674068
"unicode-properties",
40684069
"unicode-xid",
40694070
]

‎compiler/rustc_lexer/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Rust lexer used by rustc. No stability guarantees are provided.
1414

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

1920
[dependencies.unicode-properties]

‎compiler/rustc_lexer/src/cursor.rs

+7
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,11 @@ impl<'a> Cursor<'a> {
103103
self.bump();
104104
}
105105
}
106+
107+
pub(crate) fn eat_until(&mut self, byte: u8) {
108+
self.chars = match memchr::memchr(byte, self.as_str().as_bytes()) {
109+
Some(index) => self.as_str()[index..].chars(),
110+
None => "".chars(),
111+
}
112+
}
106113
}

‎compiler/rustc_lexer/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ impl Cursor<'_> {
483483
_ => None,
484484
};
485485

486-
self.eat_while(|c| c != '\n');
486+
self.eat_until(b'\n');
487487
LineComment { doc_style }
488488
}
489489

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

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

0 commit comments

Comments
 (0)
Failed to load comments.