2015-06-23 08:58:58 -05:00
|
|
|
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
// Format string literals.
|
|
|
|
|
2015-09-03 22:38:12 -05:00
|
|
|
use regex::Regex;
|
2017-07-13 04:42:14 -05:00
|
|
|
use unicode_segmentation::UnicodeSegmentation;
|
2015-09-03 22:38:12 -05:00
|
|
|
|
2015-09-06 00:39:28 -05:00
|
|
|
use config::Config;
|
2017-09-17 01:23:25 -05:00
|
|
|
use shape::Shape;
|
2017-01-05 21:35:28 -06:00
|
|
|
use utils::wrap_str;
|
2015-06-23 08:58:58 -05:00
|
|
|
|
2017-07-13 04:42:14 -05:00
|
|
|
const MIN_STRING: usize = 10;
|
2015-06-23 08:58:58 -05:00
|
|
|
|
|
|
|
pub struct StringFormat<'a> {
|
|
|
|
pub opener: &'a str,
|
|
|
|
pub closer: &'a str,
|
|
|
|
pub line_start: &'a str,
|
|
|
|
pub line_end: &'a str,
|
2017-01-30 13:28:48 -06:00
|
|
|
pub shape: Shape,
|
2015-06-23 08:58:58 -05:00
|
|
|
pub trim_end: bool,
|
2015-09-06 00:39:28 -05:00
|
|
|
pub config: &'a Config,
|
2015-06-23 08:58:58 -05:00
|
|
|
}
|
|
|
|
|
2017-10-01 05:39:00 -05:00
|
|
|
impl<'a> StringFormat<'a> {
|
|
|
|
pub fn new(shape: Shape, config: &'a Config) -> StringFormat<'a> {
|
|
|
|
StringFormat {
|
|
|
|
opener: "\"",
|
|
|
|
closer: "\"",
|
|
|
|
line_start: " ",
|
|
|
|
line_end: "\\",
|
2018-01-21 22:05:18 -06:00
|
|
|
shape,
|
2017-10-01 05:39:00 -05:00
|
|
|
trim_end: false,
|
2018-01-21 22:05:18 -06:00
|
|
|
config,
|
2017-10-01 05:39:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-12 09:20:53 -06:00
|
|
|
// FIXME: simplify this!
|
2017-10-31 01:04:50 -05:00
|
|
|
pub fn rewrite_string<'a>(
|
|
|
|
orig: &str,
|
|
|
|
fmt: &StringFormat<'a>,
|
|
|
|
max_width: Option<usize>,
|
|
|
|
) -> Option<String> {
|
2015-09-03 22:38:12 -05:00
|
|
|
// Strip line breaks.
|
2017-02-26 08:09:09 -06:00
|
|
|
let re = Regex::new(r"([^\\](\\\\)*)\\[\n\r][[:space:]]*").unwrap();
|
2015-12-25 11:50:40 -06:00
|
|
|
let stripped_str = re.replace_all(orig, "$1");
|
2015-09-03 22:38:12 -05:00
|
|
|
|
2017-05-25 02:08:08 -05:00
|
|
|
let graphemes = UnicodeSegmentation::graphemes(&*stripped_str, false).collect::<Vec<&str>>();
|
2017-10-31 01:03:46 -05:00
|
|
|
let shape = fmt.shape;
|
2018-02-18 21:47:54 -06:00
|
|
|
let indent = shape.indent.to_string_with_newline(fmt.config);
|
2015-09-29 09:38:52 -05:00
|
|
|
let punctuation = ":,;.";
|
2015-06-23 08:58:58 -05:00
|
|
|
|
2016-04-11 13:45:47 -05:00
|
|
|
// `cur_start` is the position in `orig` of the start of the current line.
|
2015-06-23 08:58:58 -05:00
|
|
|
let mut cur_start = 0;
|
2017-06-15 18:49:49 -05:00
|
|
|
let mut result = String::with_capacity(
|
|
|
|
stripped_str
|
|
|
|
.len()
|
|
|
|
.checked_next_power_of_two()
|
|
|
|
.unwrap_or(usize::max_value()),
|
|
|
|
);
|
2015-06-23 08:58:58 -05:00
|
|
|
result.push_str(fmt.opener);
|
|
|
|
|
|
|
|
let ender_length = fmt.line_end.len();
|
2015-09-25 05:53:25 -05:00
|
|
|
// If we cannot put at least a single character per line, the rewrite won't
|
|
|
|
// succeed.
|
2017-10-31 01:04:50 -05:00
|
|
|
let mut max_chars = shape
|
2017-10-18 08:01:59 -05:00
|
|
|
.width
|
|
|
|
.checked_sub(fmt.opener.len() + ender_length + 1)? + 1;
|
2015-09-10 17:52:16 -05:00
|
|
|
|
2016-04-11 13:45:47 -05:00
|
|
|
// Snip a line at a time from `orig` until it is used up. Push the snippet
|
|
|
|
// onto result.
|
|
|
|
'outer: loop {
|
|
|
|
// `cur_end` will be where we break the line, as an offset into `orig`.
|
|
|
|
// Initialised to the maximum it could be (which may be beyond `orig`).
|
2015-06-23 08:58:58 -05:00
|
|
|
let mut cur_end = cur_start + max_chars;
|
|
|
|
|
2016-04-11 13:45:47 -05:00
|
|
|
// We can fit the rest of the string on this line, so we're done.
|
2015-09-03 22:38:12 -05:00
|
|
|
if cur_end >= graphemes.len() {
|
|
|
|
let line = &graphemes[cur_start..].join("");
|
|
|
|
result.push_str(line);
|
2016-04-11 13:45:47 -05:00
|
|
|
break 'outer;
|
2015-06-23 08:58:58 -05:00
|
|
|
}
|
2015-09-25 05:53:25 -05:00
|
|
|
|
2016-04-11 13:45:47 -05:00
|
|
|
// Push cur_end left until we reach whitespace (or the line is too small).
|
2015-09-25 05:53:25 -05:00
|
|
|
while !graphemes[cur_end - 1].trim().is_empty() {
|
2015-09-03 22:38:12 -05:00
|
|
|
cur_end -= 1;
|
2016-04-11 13:45:47 -05:00
|
|
|
if cur_end < cur_start + MIN_STRING {
|
|
|
|
// We couldn't find whitespace before the string got too small.
|
|
|
|
// So start again at the max length and look for punctuation.
|
2015-09-03 22:38:12 -05:00
|
|
|
cur_end = cur_start + max_chars;
|
2016-04-11 13:45:47 -05:00
|
|
|
while !punctuation.contains(graphemes[cur_end - 1]) {
|
2015-10-02 11:13:07 -05:00
|
|
|
cur_end -= 1;
|
2016-04-11 13:45:47 -05:00
|
|
|
|
|
|
|
// If we can't break at whitespace or punctuation, grow the string instead.
|
|
|
|
if cur_end < cur_start + MIN_STRING {
|
|
|
|
cur_end = cur_start + max_chars;
|
2017-09-14 22:10:58 -05:00
|
|
|
while !(punctuation.contains(graphemes[cur_end - 1])
|
|
|
|
|| graphemes[cur_end - 1].trim().is_empty())
|
2017-06-11 22:58:58 -05:00
|
|
|
{
|
2016-04-11 13:45:47 -05:00
|
|
|
if cur_end >= graphemes.len() {
|
|
|
|
let line = &graphemes[cur_start..].join("");
|
|
|
|
result.push_str(line);
|
|
|
|
break 'outer;
|
|
|
|
}
|
|
|
|
cur_end += 1;
|
|
|
|
}
|
|
|
|
break;
|
2015-09-29 09:38:52 -05:00
|
|
|
}
|
2015-06-23 08:58:58 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Make sure there is no whitespace to the right of the break.
|
2015-12-25 11:50:40 -06:00
|
|
|
while cur_end < stripped_str.len() && graphemes[cur_end].trim().is_empty() {
|
2015-09-03 22:38:12 -05:00
|
|
|
cur_end += 1;
|
2015-06-23 08:58:58 -05:00
|
|
|
}
|
2016-04-11 13:45:47 -05:00
|
|
|
|
|
|
|
// Make the current line and add it on to result.
|
2015-09-03 22:38:12 -05:00
|
|
|
let raw_line = graphemes[cur_start..cur_end].join("");
|
2015-09-09 06:56:56 -05:00
|
|
|
let line = if fmt.trim_end {
|
|
|
|
raw_line.trim()
|
2015-06-23 08:58:58 -05:00
|
|
|
} else {
|
2016-04-11 13:45:47 -05:00
|
|
|
raw_line.as_str()
|
2015-06-23 08:58:58 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
result.push_str(line);
|
|
|
|
result.push_str(fmt.line_end);
|
2015-09-25 05:53:25 -05:00
|
|
|
result.push_str(&indent);
|
2015-06-23 08:58:58 -05:00
|
|
|
result.push_str(fmt.line_start);
|
|
|
|
|
2016-04-11 13:45:47 -05:00
|
|
|
// The next line starts where the current line ends.
|
2015-06-23 08:58:58 -05:00
|
|
|
cur_start = cur_end;
|
2017-10-31 01:04:50 -05:00
|
|
|
|
|
|
|
if let Some(new_max_chars) = max_width {
|
|
|
|
max_chars = new_max_chars.checked_sub(fmt.opener.len() + ender_length + 1)? + 1;
|
|
|
|
}
|
2015-06-23 08:58:58 -05:00
|
|
|
}
|
|
|
|
|
2016-04-11 13:45:47 -05:00
|
|
|
result.push_str(fmt.closer);
|
2017-05-16 03:47:09 -05:00
|
|
|
wrap_str(result, fmt.config.max_width(), fmt.shape)
|
2015-09-25 05:53:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2017-07-13 04:42:14 -05:00
|
|
|
use super::{rewrite_string, StringFormat};
|
2017-09-17 01:23:25 -05:00
|
|
|
use shape::{Indent, Shape};
|
2015-09-25 05:53:25 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue343() {
|
|
|
|
let config = Default::default();
|
2017-10-01 05:39:00 -05:00
|
|
|
let fmt = StringFormat::new(Shape::legacy(2, Indent::empty()), &config);
|
2017-10-31 01:04:50 -05:00
|
|
|
rewrite_string("eq_", &fmt, None);
|
2015-09-25 05:53:25 -05:00
|
|
|
}
|
2015-06-23 08:58:58 -05:00
|
|
|
}
|