X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Fstring.rs;h=d45f15a1c9bab59cb85710d453c096347cd6a748;hb=6fb188bd43840f4a99c6a4b4cdbdb21ccf3304e7;hp=2fe13db2139cf46d1077af470c9be1dacbedc2a4;hpb=375c87820f97c03fec4ec1a23803b00df1b1d5dc;p=rust.git diff --git a/src/string.rs b/src/string.rs index 2fe13db2139..d45f15a1c9b 100644 --- a/src/string.rs +++ b/src/string.rs @@ -70,7 +70,11 @@ fn max_chars_without_indent(&self) -> Option { } } -pub fn rewrite_string<'a>(orig: &str, fmt: &StringFormat<'a>) -> Option { +pub fn rewrite_string<'a>( + orig: &str, + fmt: &StringFormat<'a>, + newline_max_chars: usize, +) -> Option { let max_chars_with_indent = fmt.max_chars_with_indent()?; let max_chars_without_indent = fmt.max_chars_without_indent()?; let indent_with_newline = fmt.shape.indent.to_string_with_newline(fmt.config); @@ -129,7 +133,7 @@ pub fn rewrite_string<'a>(orig: &str, fmt: &StringFormat<'a>) -> Option result.push_str(fmt.line_end); result.push_str(&indent_with_newline); result.push_str(fmt.line_start); - cur_max_chars = max_chars_with_indent; + cur_max_chars = newline_max_chars; cur_start += len; } SnippetState::EndWithLineFeed(line, len) => { @@ -168,7 +172,7 @@ fn detect_url(s: &[&str], index: usize) -> Option { if s.len() < start + 8 { return None; } - let prefix = s[start..start + 8].join(""); + let prefix = s[start..start + 8].concat(); if prefix.starts_with("https://") || prefix.starts_with("http://") || prefix.starts_with("ftp://") @@ -238,9 +242,9 @@ fn break_string(max_chars: usize, trim_end: bool, line_end: &str, input: &[&str] for (i, grapheme) in input[0..=index].iter().enumerate() { if is_line_feed(grapheme) { if i <= index_minus_ws { - let mut line = input[0..i].join(""); + let mut line = &input[0..i].concat()[..]; if trim_end { - line = line.trim_right().to_string(); + line = line.trim_right(); } return SnippetState::EndWithLineFeed(format!("{}\n", line), i + 1); } @@ -252,7 +256,7 @@ fn break_string(max_chars: usize, trim_end: bool, line_end: &str, input: &[&str] for (i, grapheme) in input[index + 1..].iter().enumerate() { if !trim_end && is_line_feed(grapheme) { return SnippetState::EndWithLineFeed( - input[0..=index + 1 + i].join("").to_string(), + input[0..=index + 1 + i].concat(), index + 2 + i, ); } else if not_whitespace_except_line_feed(grapheme) { @@ -262,15 +266,9 @@ fn break_string(max_chars: usize, trim_end: bool, line_end: &str, input: &[&str] } if trim_end { - SnippetState::LineEnd( - input[0..=index_minus_ws].join("").to_string(), - index_plus_ws + 1, - ) + SnippetState::LineEnd(input[0..=index_minus_ws].concat(), index_plus_ws + 1) } else { - SnippetState::LineEnd( - input[0..=index_plus_ws].join("").to_string(), - index_plus_ws + 1, - ) + SnippetState::LineEnd(input[0..=index_plus_ws].concat(), index_plus_ws + 1) } }; @@ -293,15 +291,9 @@ fn break_string(max_chars: usize, trim_end: bool, line_end: &str, input: &[&str] .position(|grapheme| not_whitespace_except_line_feed(grapheme)) .unwrap_or(0); return if trim_end { - SnippetState::LineEnd( - input[..=url_index_end].join("").to_string(), - index_plus_ws + 1, - ) + SnippetState::LineEnd(input[..=url_index_end].concat(), index_plus_ws + 1) } else { - return SnippetState::LineEnd( - input[..=index_plus_ws].join("").to_string(), - index_plus_ws + 1, - ); + return SnippetState::LineEnd(input[..=index_plus_ws].concat(), index_plus_ws + 1); }; } match input[0..max_chars] @@ -326,7 +318,7 @@ fn break_string(max_chars: usize, trim_end: bool, line_end: &str, input: &[&str] // A boundary was found after the line limit Some(index) => break_at(max_chars + index), // No boundary to the right, the input cannot be broken - None => SnippetState::EndOfInput(input.join("").to_string()), + None => SnippetState::EndOfInput(input.concat()), }, }, } @@ -358,7 +350,7 @@ mod test { fn issue343() { let config = Default::default(); let fmt = StringFormat::new(Shape::legacy(2, Indent::empty()), &config); - rewrite_string("eq_", &fmt); + rewrite_string("eq_", &fmt, 2); } #[test] @@ -463,7 +455,7 @@ fn newline_in_candidate_line() { let mut config: Config = Default::default(); config.set().max_width(27); let fmt = StringFormat::new(Shape::legacy(25, Indent::empty()), &config); - let rewritten_string = rewrite_string(string, &fmt); + let rewritten_string = rewrite_string(string, &fmt, 27); assert_eq!( rewritten_string, Some("\"Nulla\nconsequat erat at massa. \\\n Vivamus id mi.\"".to_string()) @@ -477,11 +469,11 @@ fn last_line_fit_with_trailing_whitespaces() { let mut fmt = StringFormat::new(Shape::legacy(25, Indent::empty()), &config); fmt.trim_end = true; - let rewritten_string = rewrite_string(string, &fmt); + let rewritten_string = rewrite_string(string, &fmt, 25); assert_eq!(rewritten_string, Some("\"Vivamus id mi.\"".to_string())); fmt.trim_end = false; // default value of trim_end - let rewritten_string = rewrite_string(string, &fmt); + let rewritten_string = rewrite_string(string, &fmt, 25); assert_eq!(rewritten_string, Some("\"Vivamus id mi. \"".to_string())); } @@ -499,7 +491,7 @@ fn last_line_fit_with_newline() { config: &config, }; - let rewritten_string = rewrite_string(string, &fmt); + let rewritten_string = rewrite_string(string, &fmt, 100); assert_eq!( rewritten_string, Some("Vivamus id mi.\n // Vivamus id mi.".to_string()) @@ -521,7 +513,7 @@ fn overflow_in_non_string_content() { }; assert_eq!( - rewrite_string(comment, &fmt), + rewrite_string(comment, &fmt, 30), Some( "Aenean metus.\n // Vestibulum ac lacus. Vivamus\n // porttitor" .to_string() @@ -544,7 +536,7 @@ fn overflow_in_non_string_content_with_line_end() { }; assert_eq!( - rewrite_string(comment, &fmt), + rewrite_string(comment, &fmt, 30), Some( "Aenean metus.\n // Vestibulum ac lacus. Vivamus@\n // porttitor" .to_string() @@ -567,7 +559,7 @@ fn blank_line_with_non_empty_line_start() { let comment = "Aenean metus. Vestibulum\n\nac lacus. Vivamus porttitor"; assert_eq!( - rewrite_string(comment, &fmt), + rewrite_string(comment, &fmt, 30), Some( "Aenean metus. Vestibulum\n //\n // ac lacus. Vivamus porttitor".to_string() ) @@ -576,7 +568,7 @@ fn blank_line_with_non_empty_line_start() { fmt.shape = Shape::legacy(15, Indent::from_width(&config, 4)); let comment = "Aenean\n\nmetus. Vestibulum ac lacus. Vivamus porttitor"; assert_eq!( - rewrite_string(comment, &fmt), + rewrite_string(comment, &fmt, 15), Some( r#"Aenean // @@ -603,7 +595,7 @@ fn retain_blank_lines() { let comment = "Aenean\n\nmetus. Vestibulum ac lacus.\n\n"; assert_eq!( - rewrite_string(comment, &fmt), + rewrite_string(comment, &fmt, 20), Some( "Aenean\n //\n // metus. Vestibulum ac\n // lacus.\n //\n".to_string() ) @@ -611,13 +603,13 @@ fn retain_blank_lines() { let comment = "Aenean\n\nmetus. Vestibulum ac lacus.\n"; assert_eq!( - rewrite_string(comment, &fmt), + rewrite_string(comment, &fmt, 20), Some("Aenean\n //\n // metus. Vestibulum ac\n // lacus.\n".to_string()) ); let comment = "Aenean\n \nmetus. Vestibulum ac lacus."; assert_eq!( - rewrite_string(comment, &fmt), + rewrite_string(comment, &fmt, 20), Some("Aenean\n //\n // metus. Vestibulum ac\n // lacus.".to_string()) ); } @@ -637,14 +629,14 @@ fn boundary_on_edge() { let comment = "Aenean metus. Vestibulum ac lacus."; assert_eq!( - rewrite_string(comment, &fmt), + rewrite_string(comment, &fmt, 13), Some("Aenean metus.\n // Vestibulum ac\n // lacus.".to_string()) ); fmt.trim_end = false; let comment = "Vestibulum ac lacus."; assert_eq!( - rewrite_string(comment, &fmt), + rewrite_string(comment, &fmt, 13), Some("Vestibulum \n // ac lacus.".to_string()) ); @@ -652,7 +644,7 @@ fn boundary_on_edge() { fmt.line_end = "\\"; let comment = "Vestibulum ac lacus."; assert_eq!( - rewrite_string(comment, &fmt), + rewrite_string(comment, &fmt, 13), Some("Vestibulum\\\n // ac lacus.".to_string()) ); }