Skip to content

Commit ccbe94b

Browse files
committedAug 19, 2020
Simplify search for bare \r in doc comments
Outer `if` is the fast path -- it calls into hyperoptimized memchr. The inner loop is just the simplest code possible -- it doesn't generated the tightest code, but that shouldn't matter if we are going to error anyhow.
1 parent 39197e6 commit ccbe94b

File tree

1 file changed

+11
-14
lines changed
  • src/librustc_parse/lexer

1 file changed

+11
-14
lines changed
 

Diff for: ‎src/librustc_parse/lexer/mod.rs

+11-14
Original file line numberDiff line numberDiff line change
@@ -323,20 +323,17 @@ impl<'a> StringReader<'a> {
323323
comment_kind: CommentKind,
324324
doc_style: DocStyle,
325325
) -> TokenKind {
326-
let mut idx = 0;
327-
loop {
328-
idx = match content[idx..].find('\r') {
329-
None => break,
330-
Some(it) => idx + it + 1,
331-
};
332-
self.err_span_(
333-
content_start + BytePos(idx as u32 - 1),
334-
content_start + BytePos(idx as u32),
335-
match comment_kind {
336-
CommentKind::Line => "bare CR not allowed in doc-comment",
337-
CommentKind::Block => "bare CR not allowed in block doc-comment",
338-
},
339-
);
326+
if content.contains('\r') {
327+
for (idx, _) in content.char_indices().filter(|&(_, c)| c == '\r') {
328+
self.err_span_(
329+
content_start + BytePos(idx as u32),
330+
content_start + BytePos(idx as u32 + 1),
331+
match comment_kind {
332+
CommentKind::Line => "bare CR not allowed in doc-comment",
333+
CommentKind::Block => "bare CR not allowed in block doc-comment",
334+
},
335+
);
336+
}
340337
}
341338

342339
let attr_style = match doc_style {

0 commit comments

Comments
 (0)