From 2f7acf00e5d02ae795332c1fa83e41939d6361d7 Mon Sep 17 00:00:00 2001 From: sezna Date: Tue, 29 Sep 2015 09:38:52 -0500 Subject: [PATCH] Added punctuation preference Create test.rs Delete test.rs Fixed compile error. Trying a possible fix on an arithmetic overflow another try at the test failure... passed all tests. Added tests and cleaned up logic as per nrc's critiques Delete string.rs.old Delete string.rs.bk Made changes as per nrc's requests. Update string_punctuation.rs Update string_punctuation.rs fixed logical redundancy --- src/string.rs | 20 ++++++++++++++++---- tests/source/string_punctuation.rs | 5 +++++ tests/target/string_punctuation.rs | 14 ++++++++++++++ 3 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 tests/source/string_punctuation.rs create mode 100644 tests/target/string_punctuation.rs diff --git a/src/string.rs b/src/string.rs index ffaf52d3380..59ba4992863 100644 --- a/src/string.rs +++ b/src/string.rs @@ -39,6 +39,7 @@ pub fn rewrite_string<'a>(s: &str, fmt: &StringFormat<'a>) -> Option { let graphemes = UnicodeSegmentation::graphemes(&*stripped_str, false).collect::>(); let indent = fmt.offset.to_string(fmt.config); + let punctuation = ":,;."; let mut cur_start = 0; let mut result = String::with_capacity(round_up_to_power_of_two(s.len())); @@ -62,11 +63,22 @@ pub fn rewrite_string<'a>(s: &str, fmt: &StringFormat<'a>) -> Option { while !graphemes[cur_end - 1].trim().is_empty() { cur_end -= 1; if cur_end - cur_start < MIN_STRING { - // We can't break at whitespace, fall back to splitting - // anywhere that doesn't break an escape sequence. cur_end = cur_start + max_chars; - while graphemes[cur_end - 1] == "\\" { - cur_end -= 1; + // Look for punctuation to break on. + while (!punctuation.contains(graphemes[cur_end - 1])) && cur_end > 1 { + if cur_end > 1 { + cur_end -= 1; + } + } + // We can't break at whitespace or punctuation, fall back to splitting + // anywhere that doesn't break an escape sequence. + if cur_end < cur_start + MIN_STRING { + cur_end = cur_start + max_chars; + while graphemes[cur_end - 1] == "\\" { + if cur_end > 1 { + cur_end -= 1; + } + } } break; } diff --git a/tests/source/string_punctuation.rs b/tests/source/string_punctuation.rs new file mode 100644 index 00000000000..0a02077f7eb --- /dev/null +++ b/tests/source/string_punctuation.rs @@ -0,0 +1,5 @@ +fn main() { + println!("ThisIsAReallyLongStringWithNoSpaces.It_should_prefer_to_break_onpunctuation:Likethisssssssssssss"); + format!("{}__{}__{}ItShouldOnlyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyNoticeSemicolonsPeriodsColonsAndCommasAndResortToMid-CharBreaksAfterPunctuation{}{}",x,y,z,a,b); + println!("aaaaaaaaaaaaaaaaaaaaaaaaaaaaalhijalfhiigjapdighjapdigjapdighdapighapdighpaidhg;adopgihadoguaadbadgad,qeoihapethae8t0aet8haetadbjtaeg;ooeouthaoeutgadlgajduabgoiuadogabudogubaodugbadgadgadga;adoughaoeugbaouea"); +} diff --git a/tests/target/string_punctuation.rs b/tests/target/string_punctuation.rs new file mode 100644 index 00000000000..626bace0f5f --- /dev/null +++ b/tests/target/string_punctuation.rs @@ -0,0 +1,14 @@ +fn main() { + println!("ThisIsAReallyLongStringWithNoSpaces.It_should_prefer_to_break_onpunctuation:\ + Likethisssssssssssss"); + format!("{}__{}__{}ItShouldOnlyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyNoticeSemicolonsPeriodsColo\ + nsAndCommasAndResortToMid-CharBreaksAfterPunctuation{}{}", + x, + y, + z, + a, + b); + println!("aaaaaaaaaaaaaaaaaaaaaaaaaaaaalhijalfhiigjapdighjapdigjapdighdapighapdighpaidhg;\ + adopgihadoguaadbadgad,qeoihapethae8t0aet8haetadbjtaeg;\ + ooeouthaoeutgadlgajduabgoiuadogabudogubaodugbadgadgadga;adoughaoeugbaouea"); +}