mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-27 17:24:06 +00:00
improve detection of URL inside a string that is being rewritten. (#3809)
This commit is contained in:
parent
160c3aafc5
commit
8073244420
@ -153,21 +153,22 @@ pub(crate) fn rewrite_string<'a>(
|
||||
wrap_str(result, fmt.config.max_width(), fmt.shape)
|
||||
}
|
||||
|
||||
/// Returns the index to the end of the URL if the given string includes an
|
||||
/// URL or alike. Otherwise, returns `None`;
|
||||
/// Returns the index to the end of the URL if the split at index of the given string includes an
|
||||
/// URL or alike. Otherwise, returns `None`.
|
||||
fn detect_url(s: &[&str], index: usize) -> Option<usize> {
|
||||
let start = match s[..=index].iter().rposition(|g| is_whitespace(g)) {
|
||||
Some(pos) => pos + 1,
|
||||
None => 0,
|
||||
};
|
||||
// 8 = minimum length for a string to contain a URL
|
||||
if s.len() < start + 8 {
|
||||
return None;
|
||||
}
|
||||
let prefix = s[start..start + 8].concat();
|
||||
if prefix.starts_with("https://")
|
||||
|| prefix.starts_with("http://")
|
||||
|| prefix.starts_with("ftp://")
|
||||
|| prefix.starts_with("file://")
|
||||
let split = s[start..].concat();
|
||||
if split.contains("https://")
|
||||
|| split.contains("http://")
|
||||
|| split.contains("ftp://")
|
||||
|| split.contains("file://")
|
||||
{
|
||||
match s[index..].iter().position(|g| is_whitespace(g)) {
|
||||
Some(pos) => Some(index + pos - 1),
|
||||
|
9
tests/source/issue-3787.rs
Normal file
9
tests/source/issue-3787.rs
Normal file
@ -0,0 +1,9 @@
|
||||
// rustfmt-wrap_comments: true
|
||||
|
||||
//! URLs in items
|
||||
//! * [This is a link with a very loooooooooooooooooooooooooooooooooooooooooong URL.](https://example.com/This/is/a/link/with/a/very/loooooooooooooooooooooooooooooooooooooooooong/URL)
|
||||
//! * This is a [link](https://example.com/This/is/a/link/with/a/very/loooooooooooooooooooooooooooooooooooooooooong/URL) with a very loooooooooooooooooooooooooooooooooooooooooong URL.
|
||||
//! * there is no link here: In hac habitasse platea dictumst. Maecenas in ligula. Duis tincidunt odio sollicitudin quam. Nullam non mauris. Phasellus lacinia, velit sit amet bibendum euismod, leo diam interdum ligula, eu scelerisque sem purus in tellus.
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
13
tests/target/issue-3787.rs
Normal file
13
tests/target/issue-3787.rs
Normal file
@ -0,0 +1,13 @@
|
||||
// rustfmt-wrap_comments: true
|
||||
|
||||
//! URLs in items
|
||||
//! * [This is a link with a very loooooooooooooooooooooooooooooooooooooooooong URL.](https://example.com/This/is/a/link/with/a/very/loooooooooooooooooooooooooooooooooooooooooong/URL)
|
||||
//! * This is a [link](https://example.com/This/is/a/link/with/a/very/loooooooooooooooooooooooooooooooooooooooooong/URL)
|
||||
//! with a very loooooooooooooooooooooooooooooooooooooooooong URL.
|
||||
//! * there is no link here: In hac habitasse platea dictumst. Maecenas in
|
||||
//! ligula. Duis tincidunt odio sollicitudin quam. Nullam non mauris.
|
||||
//! Phasellus lacinia, velit sit amet bibendum euismod, leo diam interdum
|
||||
//! ligula, eu scelerisque sem purus in tellus.
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
Loading…
Reference in New Issue
Block a user