Skip to content

improve detection of URL inside a string that is being rewritten. #3809

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

Merged
merged 1 commit into from
Oct 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
9 changes: 9 additions & 0 deletions tests/source/issue-3787.rs
Original file line number Diff line number Diff line change
@@ -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 changes: 13 additions & 0 deletions tests/target/issue-3787.rs
Original file line number Diff line number Diff line change
@@ -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!");
}