]> git.lizzy.rs Git - rust.git/blob - src/string.rs
Merge pull request #2790 from DevOrc/master
[rust.git] / src / string.rs
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.
4 //
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.
10
11 // Format string literals.
12
13 use regex::Regex;
14 use unicode_segmentation::UnicodeSegmentation;
15
16 use config::Config;
17 use shape::Shape;
18 use utils::wrap_str;
19
20 const MIN_STRING: usize = 10;
21
22 pub struct StringFormat<'a> {
23     pub opener: &'a str,
24     pub closer: &'a str,
25     pub line_start: &'a str,
26     pub line_end: &'a str,
27     pub shape: Shape,
28     pub trim_end: bool,
29     pub config: &'a Config,
30 }
31
32 impl<'a> StringFormat<'a> {
33     pub fn new(shape: Shape, config: &'a Config) -> StringFormat<'a> {
34         StringFormat {
35             opener: "\"",
36             closer: "\"",
37             line_start: " ",
38             line_end: "\\",
39             shape,
40             trim_end: false,
41             config,
42         }
43     }
44
45     /// Returns the maximum number of graphemes that is possible on a line while taking the
46     /// indentation into account.
47     ///
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> {
50         Some(
51             self.shape
52                 .width
53                 .checked_sub(self.opener.len() + self.line_end.len() + 1)?
54                 + 1,
55         )
56     }
57
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())?)
63     }
64 }
65
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);
70
71     // Strip line breaks.
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");
75
76     let graphemes = UnicodeSegmentation::graphemes(&*stripped_str, false).collect::<Vec<&str>>();
77
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(
81         stripped_str
82             .len()
83             .checked_next_power_of_two()
84             .unwrap_or(usize::max_value()),
85     );
86     result.push_str(fmt.opener);
87
88     // Snip a line at a time from `stripped_str` until it is used up. Push the snippet
89     // onto result.
90     let mut cur_max_chars = max_chars_with_indent;
91     loop {
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(""));
95             break;
96         }
97
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;
106                 cur_start += len;
107             }
108             SnippetState::Overflow(line, len) => {
109                 result.push_str(&line);
110                 cur_max_chars = max_chars_without_indent;
111                 cur_start += len;
112             }
113             SnippetState::EndOfInput(line) => {
114                 result.push_str(&line);
115                 break;
116             }
117         }
118     }
119
120     result.push_str(fmt.closer);
121     wrap_str(result, fmt.config.max_width(), fmt.shape)
122 }
123
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)]
127 enum SnippetState {
128     /// The input could not be broken and so rewriting the string is finished.
129     EndOfInput(String),
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),
137 }
138
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) {
150                 index_minus_ws = i;
151                 break;
152             }
153         }
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(),
159                     index + 2 + i,
160                 );
161             } else if !is_whitespace(grapheme) {
162                 index_plus_ws = index + i;
163                 break;
164             }
165         }
166
167         if trim_end {
168             SnippetState::LineEnd(
169                 input[0..=index_minus_ws].join("").to_string(),
170                 index_plus_ws + 1,
171             )
172         } else {
173             SnippetState::LineEnd(
174                 input[0..=index_plus_ws].join("").to_string(),
175                 index_plus_ws + 1,
176             )
177         }
178     };
179
180     // Find the position in input for breaking the string
181     match input[0..max_chars]
182         .iter()
183         .rposition(|grapheme| is_whitespace(grapheme))
184     {
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]
189             .iter()
190             .rposition(|grapheme| is_punctuation(grapheme))
191         {
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..]
197                 .iter()
198                 .position(|grapheme| is_whitespace(grapheme) || is_punctuation(grapheme))
199             {
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()),
204             },
205         },
206     }
207 }
208
209 fn is_line_feed(grapheme: &str) -> bool {
210     grapheme.as_bytes()[0] == b'\n'
211 }
212
213 fn is_whitespace(grapheme: &str) -> bool {
214     grapheme.chars().all(|c| c.is_whitespace())
215 }
216
217 fn is_punctuation(grapheme: &str) -> bool {
218     match grapheme.as_bytes()[0] {
219         b':' | b',' | b';' | b'.' => true,
220         _ => false,
221     }
222 }
223
224 #[cfg(test)]
225 mod test {
226     use super::{break_string, rewrite_string, SnippetState, StringFormat};
227     use shape::{Indent, Shape};
228     use unicode_segmentation::UnicodeSegmentation;
229
230     #[test]
231     fn issue343() {
232         let config = Default::default();
233         let fmt = StringFormat::new(Shape::legacy(2, Indent::empty()), &config);
234         rewrite_string("eq_", &fmt);
235     }
236
237     #[test]
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>>();
241         assert_eq!(
242             break_string(20, false, &graphemes[..]),
243             SnippetState::LineEnd("Placerat felis. ".to_string(), 16)
244         );
245         assert_eq!(
246             break_string(20, true, &graphemes[..]),
247             SnippetState::LineEnd("Placerat felis.".to_string(), 16)
248         );
249     }
250
251     #[test]
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>>();
255         assert_eq!(
256             break_string(20, false, &graphemes[..]),
257             SnippetState::LineEnd("Placerat_felis.".to_string(), 15)
258         );
259     }
260
261     #[test]
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>>();
265         assert_eq!(
266             break_string(20, false, &graphemes[..]),
267             SnippetState::LineEnd("Venenatis_tellus_vel_tellus. ".to_string(), 29)
268         );
269         assert_eq!(
270             break_string(20, true, &graphemes[..]),
271             SnippetState::LineEnd("Venenatis_tellus_vel_tellus.".to_string(), 29)
272         );
273     }
274
275     #[test]
276     fn nothing_to_break() {
277         let string = "Venenatis_tellus_vel_tellus";
278         let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
279         assert_eq!(
280             break_string(20, false, &graphemes[..]),
281             SnippetState::EndOfInput("Venenatis_tellus_vel_tellus".to_string())
282         );
283     }
284
285     #[test]
286     fn significant_whitespaces() {
287         let string = "Neque in sem.      \n      Pellentesque tellus augue.";
288         let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
289         assert_eq!(
290             break_string(15, false, &graphemes[..]),
291             SnippetState::Overflow("Neque in sem.      \n".to_string(), 20)
292         );
293         assert_eq!(
294             break_string(25, false, &graphemes[..]),
295             SnippetState::Overflow("Neque in sem.      \n".to_string(), 20)
296         );
297         // if `StringFormat::line_end` is true, then the line feed does not matter anymore
298         assert_eq!(
299             break_string(15, true, &graphemes[..]),
300             SnippetState::LineEnd("Neque in sem.".to_string(), 26)
301         );
302         assert_eq!(
303             break_string(25, true, &graphemes[..]),
304             SnippetState::LineEnd("Neque in sem.".to_string(), 26)
305         );
306     }
307
308     #[test]
309     fn big_whitespace() {
310         let string = "Neque in sem.            Pellentesque tellus augue.";
311         let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
312         assert_eq!(
313             break_string(20, false, &graphemes[..]),
314             SnippetState::LineEnd("Neque in sem.            ".to_string(), 25)
315         );
316         assert_eq!(
317             break_string(20, true, &graphemes[..]),
318             SnippetState::LineEnd("Neque in sem.".to_string(), 25)
319         );
320     }
321 }