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_segmentation::UnicodeSegmentation;
20 const MIN_STRING: usize = 10;
22 /// Describes the layout of a piece of text.
23 pub struct StringFormat<'a> {
24 /// The opening sequence of characters for the piece of text
26 /// The closing sequence of characters for the piece of text
28 /// The opening sequence of characters for a line
29 pub line_start: &'a str,
30 /// The closing sequence of characters for a line
31 pub line_end: &'a str,
32 /// The allocated box to fit the text into
34 /// Trim trailing whitespaces
36 pub config: &'a Config,
39 impl<'a> StringFormat<'a> {
40 pub fn new(shape: Shape, config: &'a Config) -> StringFormat<'a> {
52 /// Returns the maximum number of graphemes that is possible on a line while taking the
53 /// indentation into account.
55 /// If we cannot put at least a single character per line, the rewrite won't succeed.
56 fn max_chars_with_indent(&self) -> Option<usize> {
60 .checked_sub(self.opener.len() + self.line_end.len() + 1)?
65 /// Like max_chars_with_indent but the indentation is not substracted.
66 /// This allows to fit more graphemes from the string on a line when
67 /// SnippetState::EndWithLineFeed.
68 fn max_chars_without_indent(&self) -> Option<usize> {
69 Some(self.config.max_width().checked_sub(self.line_end.len())?)
73 pub fn rewrite_string<'a>(
75 fmt: &StringFormat<'a>,
76 newline_max_chars: usize,
78 let max_chars_with_indent = fmt.max_chars_with_indent()?;
79 let max_chars_without_indent = fmt.max_chars_without_indent()?;
80 let indent_with_newline = fmt.shape.indent.to_string_with_newline(fmt.config);
81 let indent_without_newline = fmt.shape.indent.to_string(fmt.config);
84 // With this regex applied, all remaining whitespaces are significant
85 let strip_line_breaks_re = Regex::new(r"([^\\](\\\\)*)\\[\n\r][[:space:]]*").unwrap();
86 let stripped_str = strip_line_breaks_re.replace_all(orig, "$1");
88 let graphemes = UnicodeSegmentation::graphemes(&*stripped_str, false).collect::<Vec<&str>>();
90 // `cur_start` is the position in `orig` of the start of the current line.
91 let mut cur_start = 0;
92 let mut result = String::with_capacity(
95 .checked_next_power_of_two()
96 .unwrap_or(usize::max_value()),
98 result.push_str(fmt.opener);
100 // Snip a line at a time from `stripped_str` until it is used up. Push the snippet
102 let mut cur_max_chars = max_chars_with_indent;
103 let is_bareline_ok = fmt.line_start.is_empty() || is_whitespace(fmt.line_start);
105 // All the input starting at cur_start fits on the current line
106 if graphemes.len() - cur_start <= cur_max_chars {
107 for (i, grapheme) in graphemes[cur_start..].iter().enumerate() {
108 if is_line_feed(grapheme) {
109 // take care of blank lines
110 result = trim_right_but_line_feed(fmt.trim_end, result);
111 result.push_str("\n");
112 if !is_bareline_ok && cur_start + i + 1 < graphemes.len() {
113 result.push_str(&indent_without_newline);
114 result.push_str(fmt.line_start);
117 result.push_str(grapheme);
120 result = trim_right_but_line_feed(fmt.trim_end, result);
124 // The input starting at cur_start needs to be broken
129 &graphemes[cur_start..],
131 SnippetState::LineEnd(line, len) => {
132 result.push_str(&line);
133 result.push_str(fmt.line_end);
134 result.push_str(&indent_with_newline);
135 result.push_str(fmt.line_start);
136 cur_max_chars = newline_max_chars;
139 SnippetState::EndWithLineFeed(line, len) => {
140 if line == "\n" && fmt.trim_end {
141 result = result.trim_right().to_string();
143 result.push_str(&line);
145 // the next line can benefit from the full width
146 cur_max_chars = max_chars_without_indent;
148 result.push_str(&indent_without_newline);
149 result.push_str(fmt.line_start);
150 cur_max_chars = max_chars_with_indent;
154 SnippetState::EndOfInput(line) => {
155 result.push_str(&line);
161 result.push_str(fmt.closer);
162 wrap_str(result, fmt.config.max_width(), fmt.shape)
165 /// Returns the index to the end of the url if the given string includes an
166 /// URL or alike. Otherwise, returns None;
167 fn detect_url(s: &[&str], index: usize) -> Option<usize> {
168 let start = match s[..=index].iter().rposition(|g| is_whitespace(g)) {
169 Some(pos) => pos + 1,
172 if s.len() < start + 8 {
175 let prefix = s[start..start + 8].join("");
176 if prefix.starts_with("https://")
177 || prefix.starts_with("http://")
178 || prefix.starts_with("ftp://")
179 || prefix.starts_with("file://")
181 match s[index..].iter().position(|g| is_whitespace(g)) {
182 Some(pos) => Some(index + pos - 1),
183 None => Some(s.len() - 1),
190 /// Trims whitespaces to the right except for the line feed character.
191 fn trim_right_but_line_feed(trim_end: bool, result: String) -> String {
192 let whitespace_except_line_feed = |c: char| c.is_whitespace() && c != '\n';
193 if trim_end && result.ends_with(whitespace_except_line_feed) {
195 .trim_right_matches(whitespace_except_line_feed)
202 /// Result of breaking a string so it fits in a line and the state it ended in.
203 /// The state informs about what to do with the snippet and how to continue the breaking process.
204 #[derive(Debug, PartialEq)]
206 /// The input could not be broken and so rewriting the string is finished.
208 /// The input could be broken and the returned snippet should be ended with a
209 /// `[StringFormat::line_end]`. The next snippet needs to be indented.
211 /// The returned string is the line to print out and the number is the length that got read in
212 /// the text being rewritten. That length may be greater than the returned string if trailing
213 /// whitespaces got trimmed.
214 LineEnd(String, usize),
215 /// The input could be broken but a newline is present that cannot be trimmed. The next snippet
216 /// to be rewritten *could* use more width than what is specified by the given shape. For
217 /// example with a multiline string, the next snippet does not need to be indented, allowing
218 /// more characters to be fit within a line.
220 /// The returned string is the line to print out and the number is the length that got read in
221 /// the text being rewritten.
222 EndWithLineFeed(String, usize),
225 fn not_whitespace_except_line_feed(g: &str) -> bool {
226 is_line_feed(g) || !is_whitespace(g)
229 /// Break the input string at a boundary character around the offset `max_chars`. A boundary
230 /// character is either a punctuation or a whitespace.
231 fn break_string(max_chars: usize, trim_end: bool, line_end: &str, input: &[&str]) -> SnippetState {
232 let break_at = |index /* grapheme at index is included */| {
233 // Take in any whitespaces to the left/right of `input[index]` while
234 // preserving line feeds
235 let index_minus_ws = input[0..=index]
237 .rposition(|grapheme| not_whitespace_except_line_feed(grapheme))
239 // Take into account newlines occuring in input[0..=index], i.e., the possible next new
240 // line. If there is one, then text after it could be rewritten in a way that the available
241 // space is fully used.
242 for (i, grapheme) in input[0..=index].iter().enumerate() {
243 if is_line_feed(grapheme) {
244 if i <= index_minus_ws {
245 let mut line = input[0..i].join("");
247 line = line.trim_right().to_string();
249 return SnippetState::EndWithLineFeed(format!("{}\n", line), i + 1);
255 let mut index_plus_ws = index;
256 for (i, grapheme) in input[index + 1..].iter().enumerate() {
257 if !trim_end && is_line_feed(grapheme) {
258 return SnippetState::EndWithLineFeed(
259 input[0..=index + 1 + i].join("").to_string(),
262 } else if not_whitespace_except_line_feed(grapheme) {
263 index_plus_ws = index + i;
269 SnippetState::LineEnd(
270 input[0..=index_minus_ws].join("").to_string(),
274 SnippetState::LineEnd(
275 input[0..=index_plus_ws].join("").to_string(),
281 // Find the position in input for breaking the string
282 if line_end.is_empty()
284 && !is_whitespace(input[max_chars - 1])
285 && is_whitespace(input[max_chars])
287 // At a breaking point already
288 // The line won't invalidate the rewriting because:
289 // - no extra space needed for the line_end character
290 // - extra whitespaces to the right can be trimmed
291 return break_at(max_chars - 1);
293 if let Some(url_index_end) = detect_url(input, max_chars) {
294 let index_plus_ws = url_index_end + input[url_index_end..]
297 .position(|grapheme| not_whitespace_except_line_feed(grapheme))
300 SnippetState::LineEnd(
301 input[..=url_index_end].join("").to_string(),
305 return SnippetState::LineEnd(
306 input[..=index_plus_ws].join("").to_string(),
311 match input[0..max_chars]
313 .rposition(|grapheme| is_whitespace(grapheme))
315 // Found a whitespace and what is on its left side is big enough.
316 Some(index) if index >= MIN_STRING => break_at(index),
317 // No whitespace found, try looking for a punctuation instead
318 _ => match input[0..max_chars]
320 .rposition(|grapheme| is_punctuation(grapheme))
322 // Found a punctuation and what is on its left side is big enough.
323 Some(index) if index >= MIN_STRING => break_at(index),
324 // Either no boundary character was found to the left of `input[max_chars]`, or the line
325 // got too small. We try searching for a boundary character to the right.
326 _ => match input[max_chars..]
328 .position(|grapheme| is_whitespace(grapheme) || is_punctuation(grapheme))
330 // A boundary was found after the line limit
331 Some(index) => break_at(max_chars + index),
332 // No boundary to the right, the input cannot be broken
333 None => SnippetState::EndOfInput(input.join("").to_string()),
339 fn is_line_feed(grapheme: &str) -> bool {
340 grapheme.as_bytes()[0] == b'\n'
343 fn is_whitespace(grapheme: &str) -> bool {
344 grapheme.chars().all(|c| c.is_whitespace())
347 fn is_punctuation(grapheme: &str) -> bool {
348 match grapheme.as_bytes()[0] {
349 b':' | b',' | b';' | b'.' => true,
356 use super::{break_string, detect_url, rewrite_string, SnippetState, StringFormat};
358 use shape::{Indent, Shape};
359 use unicode_segmentation::UnicodeSegmentation;
363 let config = Default::default();
364 let fmt = StringFormat::new(Shape::legacy(2, Indent::empty()), &config);
365 rewrite_string("eq_", &fmt, 2);
369 fn should_break_on_whitespace() {
370 let string = "Placerat felis. Mauris porta ante sagittis purus.";
371 let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
373 break_string(20, false, "", &graphemes[..]),
374 SnippetState::LineEnd("Placerat felis. ".to_string(), 16)
377 break_string(20, true, "", &graphemes[..]),
378 SnippetState::LineEnd("Placerat felis.".to_string(), 16)
383 fn should_break_on_punctuation() {
384 let string = "Placerat_felis._Mauris_porta_ante_sagittis_purus.";
385 let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
387 break_string(20, false, "", &graphemes[..]),
388 SnippetState::LineEnd("Placerat_felis.".to_string(), 15)
393 fn should_break_forward() {
394 let string = "Venenatis_tellus_vel_tellus. Aliquam aliquam dolor at justo.";
395 let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
397 break_string(20, false, "", &graphemes[..]),
398 SnippetState::LineEnd("Venenatis_tellus_vel_tellus. ".to_string(), 29)
401 break_string(20, true, "", &graphemes[..]),
402 SnippetState::LineEnd("Venenatis_tellus_vel_tellus.".to_string(), 29)
407 fn nothing_to_break() {
408 let string = "Venenatis_tellus_vel_tellus";
409 let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
411 break_string(20, false, "", &graphemes[..]),
412 SnippetState::EndOfInput("Venenatis_tellus_vel_tellus".to_string())
417 fn significant_whitespaces() {
418 let string = "Neque in sem. \n Pellentesque tellus augue.";
419 let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
421 break_string(15, false, "", &graphemes[..]),
422 SnippetState::EndWithLineFeed("Neque in sem. \n".to_string(), 20)
425 break_string(25, false, "", &graphemes[..]),
426 SnippetState::EndWithLineFeed("Neque in sem. \n".to_string(), 20)
430 break_string(15, true, "", &graphemes[..]),
431 SnippetState::LineEnd("Neque in sem.".to_string(), 19)
434 break_string(25, true, "", &graphemes[..]),
435 SnippetState::EndWithLineFeed("Neque in sem.\n".to_string(), 20)
440 fn big_whitespace() {
441 let string = "Neque in sem. Pellentesque tellus augue.";
442 let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
444 break_string(20, false, "", &graphemes[..]),
445 SnippetState::LineEnd("Neque in sem. ".to_string(), 25)
448 break_string(20, true, "", &graphemes[..]),
449 SnippetState::LineEnd("Neque in sem.".to_string(), 25)
454 fn newline_in_candidate_line() {
455 let string = "Nulla\nconsequat erat at massa. Vivamus id mi.";
457 let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
459 break_string(25, false, "", &graphemes[..]),
460 SnippetState::EndWithLineFeed("Nulla\n".to_string(), 6)
463 break_string(25, true, "", &graphemes[..]),
464 SnippetState::EndWithLineFeed("Nulla\n".to_string(), 6)
467 let mut config: Config = Default::default();
468 config.set().max_width(27);
469 let fmt = StringFormat::new(Shape::legacy(25, Indent::empty()), &config);
470 let rewritten_string = rewrite_string(string, &fmt, 27);
473 Some("\"Nulla\nconsequat erat at massa. \\\n Vivamus id mi.\"".to_string())
478 fn last_line_fit_with_trailing_whitespaces() {
479 let string = "Vivamus id mi. ";
480 let config: Config = Default::default();
481 let mut fmt = StringFormat::new(Shape::legacy(25, Indent::empty()), &config);
484 let rewritten_string = rewrite_string(string, &fmt, 25);
485 assert_eq!(rewritten_string, Some("\"Vivamus id mi.\"".to_string()));
487 fmt.trim_end = false; // default value of trim_end
488 let rewritten_string = rewrite_string(string, &fmt, 25);
489 assert_eq!(rewritten_string, Some("\"Vivamus id mi. \"".to_string()));
493 fn last_line_fit_with_newline() {
494 let string = "Vivamus id mi.\nVivamus id mi.";
495 let config: Config = Default::default();
496 let fmt = StringFormat {
501 shape: Shape::legacy(100, Indent::from_width(&config, 4)),
506 let rewritten_string = rewrite_string(string, &fmt, 100);
509 Some("Vivamus id mi.\n // Vivamus id mi.".to_string())
514 fn overflow_in_non_string_content() {
515 let comment = "Aenean metus.\nVestibulum ac lacus. Vivamus porttitor";
516 let config: Config = Default::default();
517 let fmt = StringFormat {
522 shape: Shape::legacy(30, Indent::from_width(&config, 8)),
528 rewrite_string(comment, &fmt, 30),
530 "Aenean metus.\n // Vestibulum ac lacus. Vivamus\n // porttitor"
537 fn overflow_in_non_string_content_with_line_end() {
538 let comment = "Aenean metus.\nVestibulum ac lacus. Vivamus porttitor";
539 let config: Config = Default::default();
540 let fmt = StringFormat {
545 shape: Shape::legacy(30, Indent::from_width(&config, 8)),
551 rewrite_string(comment, &fmt, 30),
553 "Aenean metus.\n // Vestibulum ac lacus. Vivamus@\n // porttitor"
560 fn blank_line_with_non_empty_line_start() {
561 let config: Config = Default::default();
562 let mut fmt = StringFormat {
567 shape: Shape::legacy(30, Indent::from_width(&config, 4)),
572 let comment = "Aenean metus. Vestibulum\n\nac lacus. Vivamus porttitor";
574 rewrite_string(comment, &fmt, 30),
576 "Aenean metus. Vestibulum\n //\n // ac lacus. Vivamus porttitor".to_string()
580 fmt.shape = Shape::legacy(15, Indent::from_width(&config, 4));
581 let comment = "Aenean\n\nmetus. Vestibulum ac lacus. Vivamus porttitor";
583 rewrite_string(comment, &fmt, 15),
596 fn retain_blank_lines() {
597 let config: Config = Default::default();
598 let fmt = StringFormat {
603 shape: Shape::legacy(20, Indent::from_width(&config, 4)),
608 let comment = "Aenean\n\nmetus. Vestibulum ac lacus.\n\n";
610 rewrite_string(comment, &fmt, 20),
612 "Aenean\n //\n // metus. Vestibulum ac\n // lacus.\n //\n".to_string()
616 let comment = "Aenean\n\nmetus. Vestibulum ac lacus.\n";
618 rewrite_string(comment, &fmt, 20),
619 Some("Aenean\n //\n // metus. Vestibulum ac\n // lacus.\n".to_string())
622 let comment = "Aenean\n \nmetus. Vestibulum ac lacus.";
624 rewrite_string(comment, &fmt, 20),
625 Some("Aenean\n //\n // metus. Vestibulum ac\n // lacus.".to_string())
630 fn boundary_on_edge() {
631 let config: Config = Default::default();
632 let mut fmt = StringFormat {
637 shape: Shape::legacy(13, Indent::from_width(&config, 4)),
642 let comment = "Aenean metus. Vestibulum ac lacus.";
644 rewrite_string(comment, &fmt, 13),
645 Some("Aenean metus.\n // Vestibulum ac\n // lacus.".to_string())
648 fmt.trim_end = false;
649 let comment = "Vestibulum ac lacus.";
651 rewrite_string(comment, &fmt, 13),
652 Some("Vestibulum \n // ac lacus.".to_string())
657 let comment = "Vestibulum ac lacus.";
659 rewrite_string(comment, &fmt, 13),
660 Some("Vestibulum\\\n // ac lacus.".to_string())
666 let string = "aaa http://example.org something";
667 let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
668 assert_eq!(detect_url(&graphemes, 8), Some(21));
670 let string = "https://example.org something";
671 let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
672 assert_eq!(detect_url(&graphemes, 0), Some(18));
674 let string = "aaa ftp://example.org something";
675 let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
676 assert_eq!(detect_url(&graphemes, 8), Some(20));
678 let string = "aaa file://example.org something";
679 let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
680 assert_eq!(detect_url(&graphemes, 8), Some(21));
682 let string = "aaa http not an url";
683 let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
684 assert_eq!(detect_url(&graphemes, 6), None);
686 let string = "aaa file://example.org";
687 let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
688 assert_eq!(detect_url(&graphemes, 8), Some(21));