Skip to content

Commit

Permalink
Replace substraction underflow check with early termination
Browse files Browse the repository at this point in the history
  • Loading branch information
scampi committed Jan 30, 2022
1 parent 35284ca commit 94ac33d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,11 +278,14 @@ fn break_string(max_width: usize, trim_end: bool, line_end: &str, input: &[&str]
}
cur_index
};
if max_width_index_in_input == 0 {
return SnippetState::EndOfInput(input.concat());
}

// Find the position in input for breaking the string
if line_end.is_empty()
&& trim_end
&& !is_whitespace(input[max_width_index_in_input.checked_sub(1).unwrap_or(0)])
&& !is_whitespace(input[max_width_index_in_input - 1])
&& is_whitespace(input[max_width_index_in_input])
{
// At a breaking point already
Expand All @@ -301,7 +304,7 @@ fn break_string(max_width: usize, trim_end: bool, line_end: &str, input: &[&str]
return if trim_end {
SnippetState::LineEnd(input[..=url_index_end].concat(), index_plus_ws + 1)
} else {
return SnippetState::LineEnd(input[..=index_plus_ws].concat(), index_plus_ws + 1);
SnippetState::LineEnd(input[..=index_plus_ws].concat(), index_plus_ws + 1)
};
}

Expand Down
22 changes: 22 additions & 0 deletions tests/source/issue-5023.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// rustfmt-wrap_comments: true

/// A comment to test special unicode characters on boundaries
/// 是,是,是,是,是,是,是,是,是,是,是,是 it should break right here this goes to the next line
fn main() {
if xxx {
let xxx = xxx
.into_iter()
.filter(|(xxx, xxx)| {
if let Some(x) = Some(1) {
// xxxxxxxxxxxxxxxxxx, xxxxxxxxxxxx, xxxxxxxxxxxxxxxxxxxx xxx xxxxxxx, xxxxx xxx
// xxxxxxxxxx. xxxxxxxxxxxxxxxx,xxxxxxxxxxxxxxxxx xxx xxxxxxx
// 是sdfadsdfxxxxxxxxx,sdfaxxxxxx_xxxxx_masdfaonxxx,
if false {
return true;
}
}
false
})
.collect();
}
}
4 changes: 4 additions & 0 deletions tests/target/issue-5023.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
// rustfmt-wrap_comments: true

/// A comment to test special unicode characters on boundaries
/// 是,是,是,是,是,是,是,是,是,是,是,是 it should break right here
/// this goes to the next line
fn main() {
if xxx {
let xxx = xxx
Expand Down

0 comments on commit 94ac33d

Please sign in to comment.