Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 8073244

Browse files
scampitopecongiro
authored andcommitted
improve detection of URL inside a string that is being rewritten. (rust-lang#3809)
1 parent 160c3aa commit 8073244

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

src/string.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -153,21 +153,22 @@ pub(crate) fn rewrite_string<'a>(
153153
wrap_str(result, fmt.config.max_width(), fmt.shape)
154154
}
155155

156-
/// Returns the index to the end of the URL if the given string includes an
157-
/// URL or alike. Otherwise, returns `None`;
156+
/// Returns the index to the end of the URL if the split at index of the given string includes an
157+
/// URL or alike. Otherwise, returns `None`.
158158
fn detect_url(s: &[&str], index: usize) -> Option<usize> {
159159
let start = match s[..=index].iter().rposition(|g| is_whitespace(g)) {
160160
Some(pos) => pos + 1,
161161
None => 0,
162162
};
163+
// 8 = minimum length for a string to contain a URL
163164
if s.len() < start + 8 {
164165
return None;
165166
}
166-
let prefix = s[start..start + 8].concat();
167-
if prefix.starts_with("https://")
168-
|| prefix.starts_with("http://")
169-
|| prefix.starts_with("ftp://")
170-
|| prefix.starts_with("file://")
167+
let split = s[start..].concat();
168+
if split.contains("https://")
169+
|| split.contains("http://")
170+
|| split.contains("ftp://")
171+
|| split.contains("file://")
171172
{
172173
match s[index..].iter().position(|g| is_whitespace(g)) {
173174
Some(pos) => Some(index + pos - 1),

tests/source/issue-3787.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// rustfmt-wrap_comments: true
2+
3+
//! URLs in items
4+
//! * [This is a link with a very loooooooooooooooooooooooooooooooooooooooooong URL.](https://example.com/This/is/a/link/with/a/very/loooooooooooooooooooooooooooooooooooooooooong/URL)
5+
//! * This is a [link](https://example.com/This/is/a/link/with/a/very/loooooooooooooooooooooooooooooooooooooooooong/URL) with a very loooooooooooooooooooooooooooooooooooooooooong URL.
6+
//! * 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.
7+
fn main() {
8+
println!("Hello, world!");
9+
}

tests/target/issue-3787.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// rustfmt-wrap_comments: true
2+
3+
//! URLs in items
4+
//! * [This is a link with a very loooooooooooooooooooooooooooooooooooooooooong URL.](https://example.com/This/is/a/link/with/a/very/loooooooooooooooooooooooooooooooooooooooooong/URL)
5+
//! * This is a [link](https://example.com/This/is/a/link/with/a/very/loooooooooooooooooooooooooooooooooooooooooong/URL)
6+
//! with a very loooooooooooooooooooooooooooooooooooooooooong URL.
7+
//! * there is no link here: In hac habitasse platea dictumst. Maecenas in
8+
//! ligula. Duis tincidunt odio sollicitudin quam. Nullam non mauris.
9+
//! Phasellus lacinia, velit sit amet bibendum euismod, leo diam interdum
10+
//! ligula, eu scelerisque sem purus in tellus.
11+
fn main() {
12+
println!("Hello, world!");
13+
}

0 commit comments

Comments
 (0)