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> {
46 // FIXME: simplify this!
47 pub fn rewrite_string<'a>(
49 fmt: &StringFormat<'a>,
50 max_width: Option<usize>,
53 let re = Regex::new(r"([^\\](\\\\)*)\\[\n\r][[:space:]]*").unwrap();
54 let stripped_str = re.replace_all(orig, "$1");
56 let graphemes = UnicodeSegmentation::graphemes(&*stripped_str, false).collect::<Vec<&str>>();
57 let shape = fmt.shape;
58 let indent = shape.indent.to_string_with_newline(fmt.config);
60 // `cur_start` is the position in `orig` of the start of the current line.
61 let mut cur_start = 0;
62 let mut result = String::with_capacity(
65 .checked_next_power_of_two()
66 .unwrap_or(usize::max_value()),
68 result.push_str(fmt.opener);
70 let ender_length = fmt.line_end.len();
71 // If we cannot put at least a single character per line, the rewrite won't
73 let mut max_chars = shape
75 .checked_sub(fmt.opener.len() + ender_length + 1)? + 1;
77 // Snip a line at a time from `orig` until it is used up. Push the snippet
80 // `cur_end` will be where we break the line, as an offset into `orig`.
81 // Initialised to the maximum it could be (which may be beyond `orig`).
82 let mut cur_end = cur_start + max_chars;
84 // We can fit the rest of the string on this line, so we're done.
85 if cur_end >= graphemes.len() {
86 let line = &graphemes[cur_start..].join("");
87 result.push_str(line);
91 // Push cur_end left until we reach whitespace (or the line is too small).
92 while !is_whitespace(graphemes[cur_end - 1]) {
94 if cur_end < cur_start + MIN_STRING {
95 // We couldn't find whitespace before the string got too small.
96 // So start again at the max length and look for punctuation.
97 cur_end = cur_start + max_chars;
98 while !is_punctuation(graphemes[cur_end - 1]) {
101 // If we can't break at whitespace or punctuation, grow the string instead.
102 if cur_end < cur_start + MIN_STRING {
103 cur_end = cur_start + max_chars;
104 while !(is_punctuation(graphemes[cur_end - 1])
105 || is_whitespace(graphemes[cur_end - 1]))
108 if cur_end == graphemes.len() {
109 let line = &graphemes[cur_start..].join("");
110 result.push_str(line);
120 // Make sure there is no whitespace to the right of the break.
121 while cur_end < stripped_str.len() && is_whitespace(graphemes[cur_end]) {
125 // Make the current line and add it on to result.
126 let raw_line = graphemes[cur_start..cur_end].join("");
127 let line = if fmt.trim_end {
133 result.push_str(line);
134 result.push_str(fmt.line_end);
135 result.push_str(&indent);
136 result.push_str(fmt.line_start);
138 // The next line starts where the current line ends.
141 if let Some(new_max_chars) = max_width {
142 max_chars = new_max_chars.checked_sub(fmt.opener.len() + ender_length + 1)? + 1;
146 result.push_str(fmt.closer);
147 wrap_str(result, fmt.config.max_width(), fmt.shape)
150 fn is_whitespace(grapheme: &str) -> bool {
151 grapheme.chars().all(|c| c.is_whitespace())
154 fn is_punctuation(grapheme: &str) -> bool {
155 match grapheme.as_bytes()[0] {
156 b':' | b',' | b';' | b'.' => true,
163 use super::{rewrite_string, StringFormat};
164 use shape::{Indent, Shape};
168 let config = Default::default();
169 let fmt = StringFormat::new(Shape::legacy(2, Indent::empty()), &config);
170 rewrite_string("eq_", &fmt, None);