From 228578b9c7a3aced361e814dfd2dbabf4d131435 Mon Sep 17 00:00:00 2001 From: Seiichi Uchida Date: Tue, 5 Dec 2017 15:17:40 +0900 Subject: [PATCH] Add count_newlines() utility function --- src/comment.rs | 4 ++-- src/expr.rs | 8 ++------ src/lists.rs | 4 ++-- src/missed_spans.rs | 6 +++--- src/utils.rs | 5 +++++ src/visitor.rs | 6 +++--- 6 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/comment.rs b/src/comment.rs index 4b16199d826..ac7779d0b21 100644 --- a/src/comment.rs +++ b/src/comment.rs @@ -18,7 +18,7 @@ use rewrite::RewriteContext; use shape::{Indent, Shape}; use string::{rewrite_string, StringFormat}; -use utils::{first_line_width, last_line_width}; +use utils::{count_newlines, first_line_width, last_line_width}; fn is_custom_comment(comment: &str) -> bool { if !comment.starts_with("//") { @@ -296,7 +296,7 @@ fn rewrite_comment_inner( config: config, }; - let line_breaks = orig.trim_right().chars().filter(|&c| c == '\n').count(); + let line_breaks = count_newlines(orig.trim_right()); let lines = orig.lines() .enumerate() .map(|(i, mut line)| { diff --git a/src/expr.rs b/src/expr.rs index afa31983046..fe3dfca6270 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -2712,12 +2712,8 @@ pub fn choose_rhs( } fn prefer_next_line(orig_rhs: &str, next_line_rhs: &str) -> bool { - fn count_line_breaks(src: &str) -> usize { - src.chars().filter(|&x| x == '\n').count() - } - - !next_line_rhs.contains('\n') - || count_line_breaks(orig_rhs) > count_line_breaks(next_line_rhs) + 1 + use utils::count_newlines; + !next_line_rhs.contains('\n') || count_newlines(orig_rhs) > count_newlines(next_line_rhs) + 1 } fn rewrite_expr_addrof( diff --git a/src/lists.rs b/src/lists.rs index f3e1c362dc1..dde8cb54b28 100644 --- a/src/lists.rs +++ b/src/lists.rs @@ -17,7 +17,7 @@ use config::{Config, IndentStyle}; use rewrite::RewriteContext; use shape::{Indent, Shape}; -use utils::{first_line_width, last_line_width, mk_sp, starts_with_newline}; +use utils::{count_newlines, first_line_width, last_line_width, mk_sp, starts_with_newline}; /// Formatting tactic for lists. This will be cast down to a /// `DefinitiveListTactic` depending on the number and length of the items and @@ -651,7 +651,7 @@ fn next(&mut self) -> Option { // From the end of the first line of comments to the next non-whitespace char. let test_snippet = &test_snippet[..first]; - if test_snippet.chars().filter(|c| c == &'\n').count() > 1 { + if count_newlines(test_snippet) > 1 { // There were multiple line breaks which got trimmed to nothing. new_lines = true; } diff --git a/src/missed_spans.rs b/src/missed_spans.rs index 101d49305ff..ffdc4e56e44 100644 --- a/src/missed_spans.rs +++ b/src/missed_spans.rs @@ -16,7 +16,7 @@ use comment::{rewrite_comment, CodeCharKind, CommentCodeSlices}; use config::WriteMode; use shape::{Indent, Shape}; -use utils::mk_sp; +use utils::{count_newlines, mk_sp}; use visitor::FmtVisitor; impl<'a> FmtVisitor<'a> { @@ -86,7 +86,7 @@ fn format_missing_inner( } fn push_vertical_spaces(&mut self, original: &str) { - let mut newline_count = original.chars().filter(|&c| c == '\n').count(); + let mut newline_count = count_newlines(original); let newline_upper_bound = self.config.blank_lines_upper_bound() + 1; let newline_lower_bound = self.config.blank_lines_lower_bound() + 1; if newline_count > newline_upper_bound { @@ -171,7 +171,7 @@ fn replace_chars<'a>(string: &'a str) -> Cow<'a, str> { let fix_indent = last_char.map_or(true, |rev_c| ['{', '\n'].contains(&rev_c)); - let subslice_num_lines = subslice.chars().filter(|c| *c == '\n').count(); + let subslice_num_lines = count_newlines(subslice); if rewrite_next_comment && !self.config.file_lines().intersects_range( diff --git a/src/utils.rs b/src/utils.rs index d851dfda79d..7d3fcf73d56 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -262,6 +262,11 @@ 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() +} + #[inline] pub fn trim_newlines(input: &str) -> &str { match input.find(|c| c != '\n' && c != '\r') { diff --git a/src/visitor.rs b/src/visitor.rs index f278ec35d47..6db1999ee23 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -31,7 +31,7 @@ use regex::Regex; use rewrite::{Rewrite, RewriteContext}; use shape::{Indent, Shape}; -use utils::{self, contains_skip, inner_attributes, mk_sp, ptr_vec_to_ref_vec}; +use utils::{self, contains_skip, count_newlines, inner_attributes, mk_sp, ptr_vec_to_ref_vec}; fn is_use_item(item: &ast::Item) -> bool { match item.node { @@ -833,7 +833,7 @@ fn take_while_with_pred<'a, P>( // Extract comments between two attributes. let span_between_attr = mk_sp(attr.span.hi(), next_attr.span.lo()); let snippet = context.snippet(span_between_attr); - if snippet.chars().filter(|c| *c == '\n').count() >= 2 || snippet.contains('/') { + if count_newlines(&snippet) >= 2 || snippet.contains('/') { break; } } @@ -886,7 +886,7 @@ 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 len = comment_begin.unwrap_or_else(|| comment.len()); - let mlb = comment.chars().take(len).filter(|c| *c == '\n').count() > 1; + let mlb = count_newlines(&comment[..len]) > 1; let mla = if comment_begin.is_none() { mlb } else {