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 pub struct StringFormat<'a> {
25 pub line_start: &'a str,
26 pub line_end: &'a str,
29 pub config: &'a Config,
32 impl<'a> StringFormat<'a> {
33 pub fn new(shape: Shape, config: &'a Config) -> StringFormat<'a> {
45 /// Returns the maximum number of graphemes that is possible on a line while taking the
46 /// indentation into account.
48 /// If we cannot put at least a single character per line, the rewrite won't succeed.
49 fn max_chars_with_indent(&self) -> Option<usize> {
53 .checked_sub(self.opener.len() + self.line_end.len() + 1)?
58 /// Like max_chars_with_indent but the indentation is not substracted.
59 /// This allows to fit more graphemes from the string on a line when
60 /// SnippetState::Overflow.
61 fn max_chars_without_indent(&self) -> Option<usize> {
62 Some(self.config.max_width().checked_sub(self.line_end.len())?)
66 pub fn rewrite_string<'a>(orig: &str, fmt: &StringFormat<'a>) -> Option<String> {
67 let max_chars_with_indent = fmt.max_chars_with_indent()?;
68 let max_chars_without_indent = fmt.max_chars_without_indent()?;
69 let indent = fmt.shape.indent.to_string_with_newline(fmt.config);
72 // With this regex applied, all remaining whitespaces are significant
73 let strip_line_breaks_re = Regex::new(r"([^\\](\\\\)*)\\[\n\r][[:space:]]*").unwrap();
74 let stripped_str = strip_line_breaks_re.replace_all(orig, "$1");
76 let graphemes = UnicodeSegmentation::graphemes(&*stripped_str, false).collect::<Vec<&str>>();
78 // `cur_start` is the position in `orig` of the start of the current line.
79 let mut cur_start = 0;
80 let mut result = String::with_capacity(
83 .checked_next_power_of_two()
84 .unwrap_or(usize::max_value()),
86 result.push_str(fmt.opener);
88 // Snip a line at a time from `stripped_str` until it is used up. Push the snippet
90 let mut cur_max_chars = max_chars_with_indent;
92 // All the input starting at cur_start fits on the current line
93 if graphemes.len() - cur_start <= cur_max_chars {
94 result.push_str(&graphemes[cur_start..].join(""));
98 // The input starting at cur_start needs to be broken
99 match break_string(cur_max_chars, fmt.trim_end, &graphemes[cur_start..]) {
100 SnippetState::LineEnd(line, len) => {
101 result.push_str(&line);
102 result.push_str(fmt.line_end);
103 result.push_str(&indent);
104 result.push_str(fmt.line_start);
105 cur_max_chars = max_chars_with_indent;
108 SnippetState::Overflow(line, len) => {
109 result.push_str(&line);
110 cur_max_chars = max_chars_without_indent;
113 SnippetState::EndOfInput(line) => {
114 result.push_str(&line);
120 result.push_str(fmt.closer);
121 wrap_str(result, fmt.config.max_width(), fmt.shape)
124 /// Result of breaking a string so it fits in a line and the state it ended in.
125 /// The state informs about what to do with the snippet and how to continue the breaking process.
126 #[derive(Debug, PartialEq)]
128 /// The input could not be broken and so rewriting the string is finished.
130 /// The input could be broken and the returned snippet should be ended with a
131 /// `[StringFormat::line_end]`. The next snippet needs to be indented.
132 LineEnd(String, usize),
133 /// The input could be broken but the returned snippet should not be ended with a
134 /// `[StringFormat::line_end]` because the whitespace is significant. Therefore, the next
135 /// snippet should not be indented.
136 Overflow(String, usize),
139 /// Break the input string at a boundary character around the offset `max_chars`. A boundary
140 /// character is either a punctuation or a whitespace.
141 fn break_string(max_chars: usize, trim_end: bool, input: &[&str]) -> SnippetState {
142 let break_at = |index /* grapheme at index is included */| {
143 // Take in any whitespaces to the left/right of `input[index]` and
144 // check if there is a line feed, in which case whitespaces needs to be kept.
145 let mut index_minus_ws = index;
146 for (i, grapheme) in input[0..=index].iter().enumerate().rev() {
147 if !trim_end && is_line_feed(grapheme) {
148 return SnippetState::Overflow(input[0..=i].join("").to_string(), i + 1);
149 } else if !is_whitespace(grapheme) {
154 let mut index_plus_ws = index;
155 for (i, grapheme) in input[index + 1..].iter().enumerate() {
156 if !trim_end && is_line_feed(grapheme) {
157 return SnippetState::Overflow(
158 input[0..=index + 1 + i].join("").to_string(),
161 } else if !is_whitespace(grapheme) {
162 index_plus_ws = index + i;
168 SnippetState::LineEnd(
169 input[0..=index_minus_ws].join("").to_string(),
173 SnippetState::LineEnd(
174 input[0..=index_plus_ws].join("").to_string(),
180 // Find the position in input for breaking the string
181 match input[0..max_chars]
183 .rposition(|grapheme| is_whitespace(grapheme))
185 // Found a whitespace and what is on its left side is big enough.
186 Some(index) if index >= MIN_STRING => break_at(index),
187 // No whitespace found, try looking for a punctuation instead
188 _ => match input[0..max_chars]
190 .rposition(|grapheme| is_punctuation(grapheme))
192 // Found a punctuation and what is on its left side is big enough.
193 Some(index) if index >= MIN_STRING => break_at(index),
194 // Either no boundary character was found to the left of `input[max_chars]`, or the line
195 // got too small. We try searching for a boundary character to the right.
196 _ => match input[max_chars..]
198 .position(|grapheme| is_whitespace(grapheme) || is_punctuation(grapheme))
200 // A boundary was found after the line limit
201 Some(index) => break_at(max_chars + index),
202 // No boundary to the right, the input cannot be broken
203 None => SnippetState::EndOfInput(input.join("").to_string()),
209 fn is_line_feed(grapheme: &str) -> bool {
210 grapheme.as_bytes()[0] == b'\n'
213 fn is_whitespace(grapheme: &str) -> bool {
214 grapheme.chars().all(|c| c.is_whitespace())
217 fn is_punctuation(grapheme: &str) -> bool {
218 match grapheme.as_bytes()[0] {
219 b':' | b',' | b';' | b'.' => true,
226 use super::{break_string, rewrite_string, SnippetState, StringFormat};
227 use shape::{Indent, Shape};
228 use unicode_segmentation::UnicodeSegmentation;
232 let config = Default::default();
233 let fmt = StringFormat::new(Shape::legacy(2, Indent::empty()), &config);
234 rewrite_string("eq_", &fmt);
238 fn should_break_on_whitespace() {
239 let string = "Placerat felis. Mauris porta ante sagittis purus.";
240 let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
242 break_string(20, false, &graphemes[..]),
243 SnippetState::LineEnd("Placerat felis. ".to_string(), 16)
246 break_string(20, true, &graphemes[..]),
247 SnippetState::LineEnd("Placerat felis.".to_string(), 16)
252 fn should_break_on_punctuation() {
253 let string = "Placerat_felis._Mauris_porta_ante_sagittis_purus.";
254 let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
256 break_string(20, false, &graphemes[..]),
257 SnippetState::LineEnd("Placerat_felis.".to_string(), 15)
262 fn should_break_forward() {
263 let string = "Venenatis_tellus_vel_tellus. Aliquam aliquam dolor at justo.";
264 let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
266 break_string(20, false, &graphemes[..]),
267 SnippetState::LineEnd("Venenatis_tellus_vel_tellus. ".to_string(), 29)
270 break_string(20, true, &graphemes[..]),
271 SnippetState::LineEnd("Venenatis_tellus_vel_tellus.".to_string(), 29)
276 fn nothing_to_break() {
277 let string = "Venenatis_tellus_vel_tellus";
278 let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
280 break_string(20, false, &graphemes[..]),
281 SnippetState::EndOfInput("Venenatis_tellus_vel_tellus".to_string())
286 fn significant_whitespaces() {
287 let string = "Neque in sem. \n Pellentesque tellus augue.";
288 let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
290 break_string(15, false, &graphemes[..]),
291 SnippetState::Overflow("Neque in sem. \n".to_string(), 20)
294 break_string(25, false, &graphemes[..]),
295 SnippetState::Overflow("Neque in sem. \n".to_string(), 20)
297 // if `StringFormat::line_end` is true, then the line feed does not matter anymore
299 break_string(15, true, &graphemes[..]),
300 SnippetState::LineEnd("Neque in sem.".to_string(), 26)
303 break_string(25, true, &graphemes[..]),
304 SnippetState::LineEnd("Neque in sem.".to_string(), 26)
309 fn big_whitespace() {
310 let string = "Neque in sem. Pellentesque tellus augue.";
311 let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
313 break_string(20, false, &graphemes[..]),
314 SnippetState::LineEnd("Neque in sem. ".to_string(), 25)
317 break_string(20, true, &graphemes[..]),
318 SnippetState::LineEnd("Neque in sem.".to_string(), 25)