diff --git a/src/attr.rs b/src/attr.rs index 2e4a46326201..409a61ab544e 100644 --- a/src/attr.rs +++ b/src/attr.rs @@ -167,19 +167,17 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { fn has_newlines_before_after_comment(comment: &str) -> (&str, &str) { // Look at before and after comment and see if there are any empty lines. - let comment_begin = comment.chars().position(|c| c == '/'); + let comment_begin = comment.find('/'); let len = comment_begin.unwrap_or_else(|| comment.len()); let mlb = count_newlines(&comment[..len]) > 1; let mla = if comment_begin.is_none() { mlb } else { - let comment_end = comment.chars().rev().position(|c| !c.is_whitespace()); - let len = comment_end.unwrap(); comment .chars() .rev() - .take(len) - .filter(|c| *c == '\n') + .take_while(|c| c.is_whitespace()) + .filter(|&c| c == '\n') .count() > 1 }; (if mlb { "\n" } else { "" }, if mla { "\n" } else { "" }) diff --git a/src/comment.rs b/src/comment.rs index d53d5a9d4aa3..05b423d1898d 100644 --- a/src/comment.rs +++ b/src/comment.rs @@ -187,7 +187,7 @@ pub fn combine_strs_with_missing_comments( // expression and the second expression or the missing comment. We will preserve the original // layout whenever possible. let original_snippet = context.snippet(span); - let prefer_same_line = if let Some(pos) = original_snippet.chars().position(|c| c == '/') { + let prefer_same_line = if let Some(pos) = original_snippet.find('/') { !original_snippet[..pos].contains('\n') } else { !original_snippet.contains('\n') @@ -523,7 +523,7 @@ pub fn recover_missing_comment_in_span( Some(String::new()) } else { let missing_snippet = context.snippet(span); - let pos = missing_snippet.chars().position(|c| c == '/').unwrap_or(0); + let pos = missing_snippet.find('/').unwrap_or(0); // 1 = ` ` let total_width = missing_comment.len() + used_width + 1; let force_new_line_before_comment = diff --git a/src/issues.rs b/src/issues.rs index 75ad30b6f2a9..ad7babca1181 100644 --- a/src/issues.rs +++ b/src/issues.rs @@ -227,13 +227,13 @@ fn check_fail(text: &str, failing_pos: usize) { let mut seeker = BadIssueSeeker::new(ReportTactic::Unnumbered, ReportTactic::Unnumbered); assert_eq!( Some(failing_pos), - text.chars().position(|c| seeker.inspect(c).is_some()) + text.find(|c| seeker.inspect(c).is_some()) ); } fn check_pass(text: &str) { let mut seeker = BadIssueSeeker::new(ReportTactic::Unnumbered, ReportTactic::Unnumbered); - assert_eq!(None, text.chars().position(|c| seeker.inspect(c).is_some())); + assert_eq!(None, text.find(|c| seeker.inspect(c).is_some())); } check_fail("TODO\n", 4); diff --git a/src/items.rs b/src/items.rs index 2e0e869d12a5..377d777c4099 100644 --- a/src/items.rs +++ b/src/items.rs @@ -977,7 +977,7 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent) result.push_str(&where_clause_str); } else { let item_snippet = context.snippet(item.span); - if let Some(lo) = item_snippet.chars().position(|c| c == '/') { + if let Some(lo) = item_snippet.find('/') { // 1 = `{` let comment_hi = body_lo - BytePos(1); let comment_lo = item.span.lo() + BytePos(lo as u32); diff --git a/src/lib.rs b/src/lib.rs index 4f9d985962a0..3f4b96b0b6be 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -154,12 +154,14 @@ pub fn format_len(&self) -> (usize, usize) { match self.kind { ErrorKind::LineOverflow(found, max) => (max, found - max), ErrorKind::TrailingWhitespace => { - let trailing_ws_len = self.line_buffer - .chars() - .rev() - .take_while(|c| c.is_whitespace()) - .count(); - (self.line_buffer.len() - trailing_ws_len, trailing_ws_len) + let trailing_ws_start = self.line_buffer + .rfind(|c: char| !c.is_whitespace()) + .map(|pos| pos + 1) + .unwrap_or(0); + ( + trailing_ws_start, + self.line_buffer.len() - trailing_ws_start, + ) } _ => unreachable!(), } diff --git a/src/utils.rs b/src/utils.rs index d1d526efe369..f2202c4dcf22 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -251,7 +251,8 @@ pub fn stmt_expr(stmt: &ast::Stmt) -> Option<&ast::Expr> { #[inline] pub fn count_newlines(input: &str) -> usize { - input.chars().filter(|&c| c == '\n').count() + // Using `as_bytes` to omit UTF-8 decoding + input.as_bytes().iter().filter(|&&c| c == b'\n').count() } macro_rules! msg {