1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 // Format string literals.
14 use unicode_categories::UnicodeCategories;
15 use unicode_segmentation::UnicodeSegmentation;
17 use crate::config::Config;
18 use crate::shape::Shape;
19 use crate::utils::{unicode_str_width, wrap_str};
21 const MIN_STRING: usize = 10;
23 /// Describes the layout of a piece of text.
24 pub struct StringFormat<'a> {
25 /// The opening sequence of characters for the piece of text
27 /// The closing sequence of characters for the piece of text
29 /// The opening sequence of characters for a line
30 pub line_start: &'a str,
31 /// The closing sequence of characters for a line
32 pub line_end: &'a str,
33 /// The allocated box to fit the text into
35 /// Trim trailing whitespaces
37 pub config: &'a Config,
40 impl<'a> StringFormat<'a> {
41 pub fn new(shape: Shape, config: &'a Config) -> StringFormat<'a> {
53 /// Returns the maximum number of graphemes that is possible on a line while taking the
54 /// indentation into account.
56 /// If we cannot put at least a single character per line, the rewrite won't succeed.
57 fn max_width_with_indent(&self) -> Option<usize> {
61 .checked_sub(self.opener.len() + self.line_end.len() + 1)?
66 /// Like max_width_with_indent but the indentation is not subtracted.
67 /// This allows to fit more graphemes from the string on a line when
68 /// SnippetState::EndWithLineFeed.
69 fn max_width_without_indent(&self) -> Option<usize> {
70 Some(self.config.max_width().checked_sub(self.line_end.len())?)
74 pub fn rewrite_string<'a>(
76 fmt: &StringFormat<'a>,
77 newline_max_chars: usize,
79 let max_width_with_indent = fmt.max_width_with_indent()?;
80 let max_width_without_indent = fmt.max_width_without_indent()?;
81 let indent_with_newline = fmt.shape.indent.to_string_with_newline(fmt.config);
82 let indent_without_newline = fmt.shape.indent.to_string(fmt.config);
85 // With this regex applied, all remaining whitespaces are significant
86 let strip_line_breaks_re = Regex::new(r"([^\\](\\\\)*)\\[\n\r][[:space:]]*").unwrap();
87 let stripped_str = strip_line_breaks_re.replace_all(orig, "$1");
89 let graphemes = UnicodeSegmentation::graphemes(&*stripped_str, false).collect::<Vec<&str>>();
91 // `cur_start` is the position in `orig` of the start of the current line.
92 let mut cur_start = 0;
93 let mut result = String::with_capacity(
96 .checked_next_power_of_two()
97 .unwrap_or(usize::max_value()),
99 result.push_str(fmt.opener);
101 // Snip a line at a time from `stripped_str` until it is used up. Push the snippet
103 let mut cur_max_width = max_width_with_indent;
104 let is_bareline_ok = fmt.line_start.is_empty() || is_whitespace(fmt.line_start);
106 // All the input starting at cur_start fits on the current line
107 if graphemes_width(&graphemes[cur_start..]) <= cur_max_width {
108 for (i, grapheme) in graphemes[cur_start..].iter().enumerate() {
109 if is_new_line(grapheme) {
110 // take care of blank lines
111 result = trim_end_but_line_feed(fmt.trim_end, result);
112 result.push_str("\n");
113 if !is_bareline_ok && cur_start + i + 1 < graphemes.len() {
114 result.push_str(&indent_without_newline);
115 result.push_str(fmt.line_start);
118 result.push_str(grapheme);
121 result = trim_end_but_line_feed(fmt.trim_end, result);
125 // The input starting at cur_start needs to be broken
130 &graphemes[cur_start..],
132 SnippetState::LineEnd(line, len) => {
133 result.push_str(&line);
134 result.push_str(fmt.line_end);
135 result.push_str(&indent_with_newline);
136 result.push_str(fmt.line_start);
137 cur_max_width = newline_max_chars;
140 SnippetState::EndWithLineFeed(line, len) => {
141 if line == "\n" && fmt.trim_end {
142 result = result.trim_end().to_string();
144 result.push_str(&line);
146 // the next line can benefit from the full width
147 cur_max_width = max_width_without_indent;
149 result.push_str(&indent_without_newline);
150 result.push_str(fmt.line_start);
151 cur_max_width = max_width_with_indent;
155 SnippetState::EndOfInput(line) => {
156 result.push_str(&line);
162 result.push_str(fmt.closer);
163 wrap_str(result, fmt.config.max_width(), fmt.shape)
166 /// Returns the index to the end of the url if the given string includes an
167 /// URL or alike. Otherwise, returns None;
168 fn detect_url(s: &[&str], index: usize) -> Option<usize> {
169 let start = match s[..=index].iter().rposition(|g| is_whitespace(g)) {
170 Some(pos) => pos + 1,
173 if s.len() < start + 8 {
176 let prefix = s[start..start + 8].concat();
177 if prefix.starts_with("https://")
178 || prefix.starts_with("http://")
179 || prefix.starts_with("ftp://")
180 || prefix.starts_with("file://")
182 match s[index..].iter().position(|g| is_whitespace(g)) {
183 Some(pos) => Some(index + pos - 1),
184 None => Some(s.len() - 1),
191 /// Trims whitespaces to the right except for the line feed character.
192 fn trim_end_but_line_feed(trim_end: bool, result: String) -> String {
193 let whitespace_except_line_feed = |c: char| c.is_whitespace() && c != '\n';
194 if trim_end && result.ends_with(whitespace_except_line_feed) {
196 .trim_end_matches(whitespace_except_line_feed)
203 /// Result of breaking a string so it fits in a line and the state it ended in.
204 /// The state informs about what to do with the snippet and how to continue the breaking process.
205 #[derive(Debug, PartialEq)]
207 /// The input could not be broken and so rewriting the string is finished.
209 /// The input could be broken and the returned snippet should be ended with a
210 /// `[StringFormat::line_end]`. The next snippet needs to be indented.
212 /// The returned string is the line to print out and the number is the length that got read in
213 /// the text being rewritten. That length may be greater than the returned string if trailing
214 /// whitespaces got trimmed.
215 LineEnd(String, usize),
216 /// The input could be broken but a newline is present that cannot be trimmed. The next snippet
217 /// to be rewritten *could* use more width than what is specified by the given shape. For
218 /// example with a multiline string, the next snippet does not need to be indented, allowing
219 /// more characters to be fit within a line.
221 /// The returned string is the line to print out and the number is the length that got read in
222 /// the text being rewritten.
223 EndWithLineFeed(String, usize),
226 fn not_whitespace_except_line_feed(g: &str) -> bool {
227 is_new_line(g) || !is_whitespace(g)
230 /// Break the input string at a boundary character around the offset `max_width`. A boundary
231 /// character is either a punctuation or a whitespace.
232 /// FIXME(issue#3281): We must follow UAX#14 algorithm instead of this.
233 fn break_string(max_width: usize, trim_end: bool, line_end: &str, input: &[&str]) -> SnippetState {
234 let break_at = |index /* grapheme at index is included */| {
235 // Take in any whitespaces to the left/right of `input[index]` while
236 // preserving line feeds
237 let index_minus_ws = input[0..=index]
239 .rposition(|grapheme| not_whitespace_except_line_feed(grapheme))
241 // Take into account newlines occurring in input[0..=index], i.e., the possible next new
242 // line. If there is one, then text after it could be rewritten in a way that the available
243 // space is fully used.
244 for (i, grapheme) in input[0..=index].iter().enumerate() {
245 if is_new_line(grapheme) {
246 if i <= index_minus_ws {
247 let mut line = &input[0..i].concat()[..];
249 line = line.trim_end();
251 return SnippetState::EndWithLineFeed(format!("{}\n", line), i + 1);
257 let mut index_plus_ws = index;
258 for (i, grapheme) in input[index + 1..].iter().enumerate() {
259 if !trim_end && is_new_line(grapheme) {
260 return SnippetState::EndWithLineFeed(
261 input[0..=index + 1 + i].concat(),
264 } else if not_whitespace_except_line_feed(grapheme) {
265 index_plus_ws = index + i;
271 SnippetState::LineEnd(input[0..=index_minus_ws].concat(), index_plus_ws + 1)
273 SnippetState::LineEnd(input[0..=index_plus_ws].concat(), index_plus_ws + 1)
277 // find a first index where the unicode width of input[0..x] become > max_width
278 let max_width_index_in_input = {
279 let mut cur_width = 0;
280 let mut cur_index = 0;
281 for (i, grapheme) in input.iter().enumerate() {
282 cur_width += unicode_str_width(grapheme);
284 if cur_width > max_width {
291 // Find the position in input for breaking the string
292 if line_end.is_empty()
294 && !is_whitespace(input[max_width_index_in_input - 1])
295 && is_whitespace(input[max_width_index_in_input])
297 // At a breaking point already
298 // The line won't invalidate the rewriting because:
299 // - no extra space needed for the line_end character
300 // - extra whitespaces to the right can be trimmed
301 return break_at(max_width_index_in_input - 1);
303 if let Some(url_index_end) = detect_url(input, max_width_index_in_input) {
304 let index_plus_ws = url_index_end
305 + input[url_index_end..]
308 .position(|grapheme| not_whitespace_except_line_feed(grapheme))
311 SnippetState::LineEnd(input[..=url_index_end].concat(), index_plus_ws + 1)
313 return SnippetState::LineEnd(input[..=index_plus_ws].concat(), index_plus_ws + 1);
317 match input[0..max_width_index_in_input]
319 .rposition(|grapheme| is_whitespace(grapheme))
321 // Found a whitespace and what is on its left side is big enough.
322 Some(index) if index >= MIN_STRING => break_at(index),
323 // No whitespace found, try looking for a punctuation instead
324 _ => match input[0..max_width_index_in_input]
326 .rposition(|grapheme| is_punctuation(grapheme))
328 // Found a punctuation and what is on its left side is big enough.
329 Some(index) if index >= MIN_STRING => break_at(index),
330 // Either no boundary character was found to the left of `input[max_chars]`, or the line
331 // got too small. We try searching for a boundary character to the right.
332 _ => match input[max_width_index_in_input..]
334 .position(|grapheme| is_whitespace(grapheme) || is_punctuation(grapheme))
336 // A boundary was found after the line limit
337 Some(index) => break_at(max_width_index_in_input + index),
338 // No boundary to the right, the input cannot be broken
339 None => SnippetState::EndOfInput(input.concat()),
345 fn is_new_line(grapheme: &str) -> bool {
346 let bytes = grapheme.as_bytes();
347 bytes.starts_with(b"\n") || bytes.starts_with(b"\r\n")
350 fn is_whitespace(grapheme: &str) -> bool {
351 grapheme.chars().all(|c| c.is_whitespace())
354 fn is_punctuation(grapheme: &str) -> bool {
355 grapheme.chars().all(|c| c.is_punctuation_other())
358 fn graphemes_width(graphemes: &[&str]) -> usize {
359 graphemes.iter().map(|s| unicode_str_width(s)).sum()
364 use super::{break_string, detect_url, rewrite_string, SnippetState, StringFormat};
365 use crate::config::Config;
366 use crate::shape::{Indent, Shape};
367 use unicode_segmentation::UnicodeSegmentation;
371 let config = Default::default();
372 let fmt = StringFormat::new(Shape::legacy(2, Indent::empty()), &config);
373 rewrite_string("eq_", &fmt, 2);
377 fn should_break_on_whitespace() {
378 let string = "Placerat felis. Mauris porta ante sagittis purus.";
379 let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
381 break_string(20, false, "", &graphemes[..]),
382 SnippetState::LineEnd("Placerat felis. ".to_string(), 16)
385 break_string(20, true, "", &graphemes[..]),
386 SnippetState::LineEnd("Placerat felis.".to_string(), 16)
391 fn should_break_on_punctuation() {
392 let string = "Placerat_felis._Mauris_porta_ante_sagittis_purus.";
393 let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
395 break_string(20, false, "", &graphemes[..]),
396 SnippetState::LineEnd("Placerat_felis.".to_string(), 15)
401 fn should_break_forward() {
402 let string = "Venenatis_tellus_vel_tellus. Aliquam aliquam dolor at justo.";
403 let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
405 break_string(20, false, "", &graphemes[..]),
406 SnippetState::LineEnd("Venenatis_tellus_vel_tellus. ".to_string(), 29)
409 break_string(20, true, "", &graphemes[..]),
410 SnippetState::LineEnd("Venenatis_tellus_vel_tellus.".to_string(), 29)
415 fn nothing_to_break() {
416 let string = "Venenatis_tellus_vel_tellus";
417 let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
419 break_string(20, false, "", &graphemes[..]),
420 SnippetState::EndOfInput("Venenatis_tellus_vel_tellus".to_string())
425 fn significant_whitespaces() {
426 let string = "Neque in sem. \n Pellentesque tellus augue.";
427 let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
429 break_string(15, false, "", &graphemes[..]),
430 SnippetState::EndWithLineFeed("Neque in sem. \n".to_string(), 20)
433 break_string(25, false, "", &graphemes[..]),
434 SnippetState::EndWithLineFeed("Neque in sem. \n".to_string(), 20)
438 break_string(15, true, "", &graphemes[..]),
439 SnippetState::LineEnd("Neque in sem.".to_string(), 19)
442 break_string(25, true, "", &graphemes[..]),
443 SnippetState::EndWithLineFeed("Neque in sem.\n".to_string(), 20)
448 fn big_whitespace() {
449 let string = "Neque in sem. Pellentesque tellus augue.";
450 let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
452 break_string(20, false, "", &graphemes[..]),
453 SnippetState::LineEnd("Neque in sem. ".to_string(), 25)
456 break_string(20, true, "", &graphemes[..]),
457 SnippetState::LineEnd("Neque in sem.".to_string(), 25)
462 fn newline_in_candidate_line() {
463 let string = "Nulla\nconsequat erat at massa. Vivamus id mi.";
465 let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
467 break_string(25, false, "", &graphemes[..]),
468 SnippetState::EndWithLineFeed("Nulla\n".to_string(), 6)
471 break_string(25, true, "", &graphemes[..]),
472 SnippetState::EndWithLineFeed("Nulla\n".to_string(), 6)
475 let mut config: Config = Default::default();
476 config.set().max_width(27);
477 let fmt = StringFormat::new(Shape::legacy(25, Indent::empty()), &config);
478 let rewritten_string = rewrite_string(string, &fmt, 27);
481 Some("\"Nulla\nconsequat erat at massa. \\\n Vivamus id mi.\"".to_string())
486 fn last_line_fit_with_trailing_whitespaces() {
487 let string = "Vivamus id mi. ";
488 let config: Config = Default::default();
489 let mut fmt = StringFormat::new(Shape::legacy(25, Indent::empty()), &config);
492 let rewritten_string = rewrite_string(string, &fmt, 25);
493 assert_eq!(rewritten_string, Some("\"Vivamus id mi.\"".to_string()));
495 fmt.trim_end = false; // default value of trim_end
496 let rewritten_string = rewrite_string(string, &fmt, 25);
497 assert_eq!(rewritten_string, Some("\"Vivamus id mi. \"".to_string()));
501 fn last_line_fit_with_newline() {
502 let string = "Vivamus id mi.\nVivamus id mi.";
503 let config: Config = Default::default();
504 let fmt = StringFormat {
509 shape: Shape::legacy(100, Indent::from_width(&config, 4)),
514 let rewritten_string = rewrite_string(string, &fmt, 100);
517 Some("Vivamus id mi.\n // Vivamus id mi.".to_string())
522 fn overflow_in_non_string_content() {
523 let comment = "Aenean metus.\nVestibulum ac lacus. Vivamus porttitor";
524 let config: Config = Default::default();
525 let fmt = StringFormat {
530 shape: Shape::legacy(30, Indent::from_width(&config, 8)),
536 rewrite_string(comment, &fmt, 30),
538 "Aenean metus.\n // Vestibulum ac lacus. Vivamus\n // porttitor"
545 fn overflow_in_non_string_content_with_line_end() {
546 let comment = "Aenean metus.\nVestibulum ac lacus. Vivamus porttitor";
547 let config: Config = Default::default();
548 let fmt = StringFormat {
553 shape: Shape::legacy(30, Indent::from_width(&config, 8)),
559 rewrite_string(comment, &fmt, 30),
561 "Aenean metus.\n // Vestibulum ac lacus. Vivamus@\n // porttitor"
568 fn blank_line_with_non_empty_line_start() {
569 let config: Config = Default::default();
570 let mut fmt = StringFormat {
575 shape: Shape::legacy(30, Indent::from_width(&config, 4)),
580 let comment = "Aenean metus. Vestibulum\n\nac lacus. Vivamus porttitor";
582 rewrite_string(comment, &fmt, 30),
584 "Aenean metus. Vestibulum\n //\n // ac lacus. Vivamus porttitor".to_string()
588 fmt.shape = Shape::legacy(15, Indent::from_width(&config, 4));
589 let comment = "Aenean\n\nmetus. Vestibulum ac lacus. Vivamus porttitor";
591 rewrite_string(comment, &fmt, 15),
604 fn retain_blank_lines() {
605 let config: Config = Default::default();
606 let fmt = StringFormat {
611 shape: Shape::legacy(20, Indent::from_width(&config, 4)),
616 let comment = "Aenean\n\nmetus. Vestibulum ac lacus.\n\n";
618 rewrite_string(comment, &fmt, 20),
620 "Aenean\n //\n // metus. Vestibulum ac\n // lacus.\n //\n".to_string()
624 let comment = "Aenean\n\nmetus. Vestibulum ac lacus.\n";
626 rewrite_string(comment, &fmt, 20),
627 Some("Aenean\n //\n // metus. Vestibulum ac\n // lacus.\n".to_string())
630 let comment = "Aenean\n \nmetus. Vestibulum ac lacus.";
632 rewrite_string(comment, &fmt, 20),
633 Some("Aenean\n //\n // metus. Vestibulum ac\n // lacus.".to_string())
638 fn boundary_on_edge() {
639 let config: Config = Default::default();
640 let mut fmt = StringFormat {
645 shape: Shape::legacy(13, Indent::from_width(&config, 4)),
650 let comment = "Aenean metus. Vestibulum ac lacus.";
652 rewrite_string(comment, &fmt, 13),
653 Some("Aenean metus.\n // Vestibulum ac\n // lacus.".to_string())
656 fmt.trim_end = false;
657 let comment = "Vestibulum ac lacus.";
659 rewrite_string(comment, &fmt, 13),
660 Some("Vestibulum \n // ac lacus.".to_string())
665 let comment = "Vestibulum ac lacus.";
667 rewrite_string(comment, &fmt, 13),
668 Some("Vestibulum\\\n // ac lacus.".to_string())
674 let string = "aaa http://example.org something";
675 let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
676 assert_eq!(detect_url(&graphemes, 8), Some(21));
678 let string = "https://example.org something";
679 let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
680 assert_eq!(detect_url(&graphemes, 0), Some(18));
682 let string = "aaa ftp://example.org something";
683 let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
684 assert_eq!(detect_url(&graphemes, 8), Some(20));
686 let string = "aaa file://example.org something";
687 let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
688 assert_eq!(detect_url(&graphemes, 8), Some(21));
690 let string = "aaa http not an url";
691 let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
692 assert_eq!(detect_url(&graphemes, 6), None);
694 let string = "aaa file://example.org";
695 let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
696 assert_eq!(detect_url(&graphemes, 8), Some(21));