From f711078126f95bd580c69c39af9fc16e3f83c859 Mon Sep 17 00:00:00 2001 From: Pazzaz Date: Thu, 24 May 2018 20:08:29 +0200 Subject: [PATCH] Seperate and optimize grapheme conditions --- src/string.rs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/string.rs b/src/string.rs index f683eab7dc7..a5453c0a8ed 100644 --- a/src/string.rs +++ b/src/string.rs @@ -56,7 +56,6 @@ pub fn rewrite_string<'a>( let graphemes = UnicodeSegmentation::graphemes(&*stripped_str, false).collect::>(); let shape = fmt.shape; let indent = shape.indent.to_string_with_newline(fmt.config); - let punctuation = ":,;."; // `cur_start` is the position in `orig` of the start of the current line. let mut cur_start = 0; @@ -90,20 +89,20 @@ pub fn rewrite_string<'a>( } // Push cur_end left until we reach whitespace (or the line is too small). - while !graphemes[cur_end - 1].trim().is_empty() { + while !is_whitespace(graphemes[cur_end - 1]) { cur_end -= 1; if cur_end < cur_start + MIN_STRING { // We couldn't find whitespace before the string got too small. // So start again at the max length and look for punctuation. cur_end = cur_start + max_chars; - while !punctuation.contains(graphemes[cur_end - 1]) { + while !is_punctuation(graphemes[cur_end - 1]) { cur_end -= 1; // If we can't break at whitespace or punctuation, grow the string instead. if cur_end < cur_start + MIN_STRING { cur_end = cur_start + max_chars; - while !(punctuation.contains(graphemes[cur_end - 1]) - || graphemes[cur_end - 1].trim().is_empty()) + while !(is_punctuation(graphemes[cur_end - 1]) + || is_whitespace(graphemes[cur_end - 1])) { cur_end += 1; if cur_end == graphemes.len() { @@ -119,7 +118,7 @@ pub fn rewrite_string<'a>( } } // Make sure there is no whitespace to the right of the break. - while cur_end < stripped_str.len() && graphemes[cur_end].trim().is_empty() { + while cur_end < stripped_str.len() && is_whitespace(graphemes[cur_end]) { cur_end += 1; } @@ -148,6 +147,17 @@ pub fn rewrite_string<'a>( wrap_str(result, fmt.config.max_width(), fmt.shape) } +fn is_whitespace(grapheme: &str) -> bool { + grapheme.chars().all(|c| c.is_whitespace()) +} + +fn is_punctuation(grapheme: &str) -> bool { + match grapheme.as_bytes()[0] { + b':' | b',' | b';' | b'.' => true, + _ => false, + } +} + #[cfg(test)] mod test { use super::{rewrite_string, StringFormat};