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

This commit is contained in:
Stéphane Campinas 2019-10-07 09:43:50 +02:00 committed by Seiichi Uchida
parent 160c3aafc5
commit 8073244420
3 changed files with 30 additions and 7 deletions

View File

@ -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),

View 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!");
}

View 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!");
}