Skip to content

Commit 8ae8758

Browse files
committed
refactor: remove unnecessary inline annotations and rename cursor helper function names for better readability
1 parent ef2fa3b commit 8ae8758

File tree

2 files changed

+13
-54
lines changed

2 files changed

+13
-54
lines changed

compiler/rustc_lexer/src/cursor.rs

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ pub struct Cursor<'a> {
2121
pub(crate) const EOF_CHAR: char = '\0';
2222

2323
impl<'a> Cursor<'a> {
24-
#[inline]
2524
pub fn new(input: &'a str, frontmatter_allowed: FrontmatterAllowed) -> Cursor<'a> {
2625
Cursor {
2726
len_remaining: input.len(),
@@ -32,7 +31,6 @@ impl<'a> Cursor<'a> {
3231
}
3332
}
3433

35-
#[inline]
3634
pub fn as_str(&self) -> &'a str {
3735
self.chars.as_str()
3836
}
@@ -55,14 +53,12 @@ impl<'a> Cursor<'a> {
5553
/// If requested position doesn't exist, `EOF_CHAR` is returned.
5654
/// However, getting `EOF_CHAR` doesn't always mean actual end of file,
5755
/// it should be checked with `is_eof` method.
58-
#[inline]
5956
pub fn first(&self) -> char {
6057
// `.next()` optimizes better than `.nth(0)`
6158
self.chars.clone().next().unwrap_or(EOF_CHAR)
6259
}
6360

6461
/// Peeks the second symbol from the input stream without consuming it.
65-
#[inline]
6662
pub(crate) fn second(&self) -> char {
6763
// `.next()` optimizes better than `.nth(1)`
6864
let mut iter = self.chars.clone();
@@ -71,7 +67,6 @@ impl<'a> Cursor<'a> {
7167
}
7268

7369
/// Peeks the third symbol from the input stream without consuming it.
74-
#[inline]
7570
pub fn third(&self) -> char {
7671
// `.next()` optimizes better than `.nth(2)`
7772
let mut iter = self.chars.clone();
@@ -81,25 +76,21 @@ impl<'a> Cursor<'a> {
8176
}
8277

8378
/// Checks if there is nothing more to consume.
84-
#[inline]
8579
pub(crate) fn is_eof(&self) -> bool {
8680
self.chars.as_str().is_empty()
8781
}
8882

8983
/// Returns amount of already consumed symbols.
90-
#[inline]
9184
pub(crate) fn pos_within_token(&self) -> u32 {
9285
(self.len_remaining - self.chars.as_str().len()) as u32
9386
}
9487

9588
/// Resets the number of bytes consumed to 0.
96-
#[inline]
9789
pub(crate) fn reset_pos_within_token(&mut self) {
9890
self.len_remaining = self.chars.as_str().len();
9991
}
10092

10193
/// Moves to the next character.
102-
#[inline]
10394
pub(crate) fn bump(&mut self) -> Option<char> {
10495
let c = self.chars.next()?;
10596

@@ -111,10 +102,9 @@ impl<'a> Cursor<'a> {
111102
Some(c)
112103
}
113104

114-
#[inline]
115-
pub(crate) fn bump_if(&mut self, expected: char) -> bool {
105+
pub(crate) fn bump_if(&mut self, byte: char) -> bool {
116106
let mut chars = self.chars.clone();
117-
if chars.next() == Some(expected) {
107+
if chars.next() == Some(byte) {
118108
self.chars = chars;
119109
true
120110
} else {
@@ -123,11 +113,10 @@ impl<'a> Cursor<'a> {
123113
}
124114

125115
/// Bumps the cursor if the next character is either of the two expected characters.
126-
#[inline]
127-
pub(crate) fn bump_if2(&mut self, expected1: char, expected2: char) -> bool {
116+
pub(crate) fn bump_if_either(&mut self, byte1: char, byte2: char) -> bool {
128117
let mut chars = self.chars.clone();
129118
if let Some(c) = chars.next()
130-
&& (c == expected1 || c == expected2)
119+
&& (c == byte1 || c == byte2)
131120
{
132121
self.chars = chars;
133122
return true;
@@ -136,13 +125,11 @@ impl<'a> Cursor<'a> {
136125
}
137126

138127
/// Moves to a substring by a number of bytes.
139-
#[inline]
140128
pub(crate) fn bump_bytes(&mut self, n: usize) {
141-
self.chars = self.as_str().get(n..).unwrap_or("").chars();
129+
self.chars = self.as_str()[n..].chars();
142130
}
143131

144132
/// Eats symbols while predicate returns true or until the end of file is reached.
145-
#[inline]
146133
pub(crate) fn eat_while(&mut self, mut predicate: impl FnMut(char) -> bool) {
147134
// It was tried making optimized version of this for eg. line comments, but
148135
// LLVM can inline all of this and compile it down to fast iteration over bytes.
@@ -152,7 +139,6 @@ impl<'a> Cursor<'a> {
152139
}
153140
/// Eats characters until the given byte is found.
154141
/// Returns true if the byte was found, false if end of file was reached.
155-
#[inline]
156142
pub(crate) fn eat_until(&mut self, byte: u8) -> bool {
157143
match memchr::memchr(byte, self.as_str().as_bytes()) {
158144
Some(index) => {
@@ -168,8 +154,7 @@ impl<'a> Cursor<'a> {
168154

169155
/// Eats characters until any of the given bytes is found, then consumes past it.
170156
/// Returns the found byte if any, or None if end of file was reached.
171-
#[inline]
172-
pub(crate) fn eat_past2(&mut self, byte1: u8, byte2: u8) -> Option<u8> {
157+
pub(crate) fn eat_past_either(&mut self, byte1: u8, byte2: u8) -> Option<u8> {
173158
let bytes = self.as_str().as_bytes();
174159
match memchr::memchr2(byte1, byte2, bytes) {
175160
Some(index) => {

compiler/rustc_lexer/src/lib.rs

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -563,30 +563,10 @@ impl Cursor<'_> {
563563
self.eat_while(|ch| ch != '\n' && is_horizontal_whitespace(ch));
564564
let invalid_infostring = self.first() != '\n';
565565

566-
#[inline]
567-
fn find_closing_fence(s: &str, dash_count: usize) -> Option<usize> {
568-
let bytes = s.as_bytes();
569-
let mut i = 0;
570-
while i < bytes.len() {
571-
if let Some(newline_pos) = memchr::memchr(b'\n', &bytes[i..]) {
572-
i += newline_pos + 1;
573-
let start = i;
574-
if start + dash_count <= bytes.len() {
575-
let slice = &bytes[start..start + dash_count];
576-
if slice.iter().all(|&b| b == b'-') {
577-
return Some(start + dash_count);
578-
}
579-
}
580-
} else {
581-
break;
582-
}
583-
}
584-
None
585-
}
586-
587-
if let Some(closing) = find_closing_fence(self.as_str(), length_opening as usize) {
566+
let nl_fence_pattern = format!("\n{:-<1$}", "", length_opening as usize);
567+
if let Some(closing) = self.as_str().find(&nl_fence_pattern) {
588568
// candidate found
589-
self.bump_bytes(closing);
569+
self.bump_bytes(closing + nl_fence_pattern.len());
590570
// in case like
591571
// ---cargo
592572
// --- blahblah
@@ -672,7 +652,7 @@ impl Cursor<'_> {
672652
};
673653

674654
let mut depth = 1usize;
675-
while let Some(c) = self.eat_past2(b'/', b'*') {
655+
while let Some(c) = self.eat_past_either(b'/', b'*') {
676656
match c {
677657
b'/' => {
678658
if self.bump_if('*') {
@@ -953,18 +933,12 @@ impl Cursor<'_> {
953933
/// if string is terminated.
954934
fn double_quoted_string(&mut self) -> bool {
955935
debug_assert!(self.prev() == '"');
956-
while let Some(c) = self.eat_past2(b'"', b'\\') {
936+
while let Some(c) = self.eat_past_either(b'"', b'\\') {
957937
match c {
958938
b'"' => {
959939
return true;
960940
}
961-
b'\\' => {
962-
let first = self.first();
963-
if first == '\\' || first == '"' {
964-
// Bump to skip escaped character.
965-
self.bump();
966-
}
967-
}
941+
b'\\' => _ = self.bump_if_either('\\', '"'),
968942
_ => unreachable!(),
969943
}
970944
}
@@ -1133,7 +1107,7 @@ impl Cursor<'_> {
11331107
/// and returns false otherwise.
11341108
fn eat_float_exponent(&mut self) -> bool {
11351109
debug_assert!(self.prev() == 'e' || self.prev() == 'E');
1136-
self.bump_if2('-', '+');
1110+
self.bump_if_either('-', '+');
11371111
self.eat_decimal_digits()
11381112
}
11391113

0 commit comments

Comments
 (0)