-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Use str::strip_{prefix,suffix}
when possible
#74048
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,27 +29,31 @@ pub struct Comment { | |
} | ||
|
||
pub fn is_line_doc_comment(s: &str) -> bool { | ||
let res = (s.starts_with("///") && *s.as_bytes().get(3).unwrap_or(&b' ') != b'/') | ||
|| s.starts_with("//!"); | ||
debug!("is {:?} a doc comment? {}", s, res); | ||
res | ||
let yes = match s.as_bytes() { | ||
[b'/', b'/', b'/', c, ..] => *c != b'/', | ||
[b'/', b'/', b'/', ..] => true, | ||
[b'/', b'/', b'!', ..] => true, | ||
_ => false, | ||
}; | ||
debug!("is {:?} a line doc comment? {}", s, yes); | ||
yes | ||
} | ||
|
||
pub fn is_block_doc_comment(s: &str) -> bool { | ||
// Prevent `/**/` from being parsed as a doc comment | ||
let res = ((s.starts_with("/**") && *s.as_bytes().get(3).unwrap_or(&b' ') != b'*') | ||
|| s.starts_with("/*!")) | ||
&& s.len() >= 5; | ||
debug!("is {:?} a doc comment? {}", s, res); | ||
res | ||
// Previously, `/**/` was incorrectly regarded as a doc comment because it | ||
// starts with `/**` and ends with `*/`. However, this caused an ICE | ||
// because some code assumed that the length of a doc comment is at least 5. | ||
let yes = match s.as_bytes() { | ||
[b'/', b'*', b'*', c, _, ..] => *c != b'*', | ||
[b'/', b'*', b'!', _, _, ..] => true, | ||
_ => false, | ||
}; | ||
debug!("is {:?} a block doc comment? {}", s, yes); | ||
yes | ||
} | ||
|
||
// FIXME(#64197): Try to privatize this again. | ||
pub fn is_doc_comment(s: &str) -> bool { | ||
(s.starts_with("///") && is_line_doc_comment(s)) | ||
|| s.starts_with("//!") | ||
|| (s.starts_with("/**") && is_block_doc_comment(s)) | ||
|| s.starts_with("/*!") | ||
is_line_doc_comment(s) || is_block_doc_comment(s) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems to be a behavior change. In particular, |
||
} | ||
|
||
pub fn doc_comment_style(comment: &str) -> ast::AttrStyle { | ||
|
@@ -127,22 +131,26 @@ pub fn strip_doc_comment_decoration(comment: &str) -> String { | |
const ONELINERS: &[&str] = &["///!", "///", "//!", "//"]; | ||
|
||
for prefix in ONELINERS { | ||
if comment.starts_with(*prefix) { | ||
return (&comment[prefix.len()..]).to_string(); | ||
if let Some(tail) = comment.strip_prefix(*prefix) { | ||
return tail.to_owned(); | ||
} | ||
} | ||
|
||
if comment.starts_with("/*") { | ||
let lines = | ||
comment[3..comment.len() - 2].lines().map(|s| s.to_string()).collect::<Vec<String>>(); | ||
match comment | ||
.strip_prefix("/**") | ||
.or_else(|| comment.strip_prefix("/*!")) | ||
.and_then(|s| s.strip_suffix("*/")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This also looks like a behavior change -- I don't think we cared what the third character was before, and certainly not the last two characters. |
||
{ | ||
Some(doc) => { | ||
let lines = doc.lines().map(|s| s.to_string()).collect::<Vec<String>>(); | ||
|
||
let lines = vertical_trim(lines); | ||
let lines = horizontal_trim(lines); | ||
let lines = vertical_trim(lines); | ||
let lines = horizontal_trim(lines); | ||
|
||
return lines.join("\n"); | ||
lines.join("\n") | ||
} | ||
_ => panic!("not a doc-comment: {}", comment), | ||
} | ||
|
||
panic!("not a doc-comment: {}", comment); | ||
} | ||
|
||
/// Returns `None` if the first `col` chars of `s` contain a non-whitespace char. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like this shouldn't be removed? It does look like it could be made private -- only use outside of here seems to be in src/librustc_expand/parse/lexer/tests.rs and those tests could be moved into this crate relatively easily I imagine.