use regex::Regex;
use unicode_segmentation::UnicodeSegmentation;
use config::Config;
use shape::Shape;
use utils::wrap_str;
const MIN_STRING: usize = 10;
pub struct StringFormat<'a> {
pub opener: &'a str,
pub closer: &'a str,
pub line_start: &'a str,
pub line_end: &'a str,
pub shape: Shape,
pub trim_end: bool,
pub config: &'a Config,
}
impl<'a> StringFormat<'a> {
pub fn new(shape: Shape, config: &'a Config) -> StringFormat<'a> {
StringFormat {
opener: "\"",
closer: "\"",
line_start: " ",
line_end: "\\",
shape,
trim_end: false,
config,
}
}
}
pub fn rewrite_string<'a>(
orig: &str,
fmt: &StringFormat<'a>,
max_width: Option<usize>,
) -> Option<String> {
let re = Regex::new(r"([^\\](\\\\)*)\\[\n\r][[:space:]]*").unwrap();
let stripped_str = re.replace_all(orig, "$1");
let graphemes = UnicodeSegmentation::graphemes(&*stripped_str, false).collect::<Vec<&str>>();
let shape = fmt.shape;
let indent = shape.indent.to_string(fmt.config);
let punctuation = ":,;.";
let mut cur_start = 0;
let mut result = String::with_capacity(
stripped_str
.len()
.checked_next_power_of_two()
.unwrap_or(usize::max_value()),
);
result.push_str(fmt.opener);
let ender_length = fmt.line_end.len();
let mut max_chars = shape
.width
.checked_sub(fmt.opener.len() + ender_length + 1)? + 1;
'outer: loop {
let mut cur_end = cur_start + max_chars;
if cur_end >= graphemes.len() {
let line = &graphemes[cur_start..].join("");
result.push_str(line);
break 'outer;
}
while !graphemes[cur_end - 1].trim().is_empty() {
cur_end -= 1;
if cur_end < cur_start + MIN_STRING {
cur_end = cur_start + max_chars;
while !punctuation.contains(graphemes[cur_end - 1]) {
cur_end -= 1;
if cur_end < cur_start + MIN_STRING {
cur_end = cur_start + max_chars;
while !(punctuation.contains(graphemes[cur_end - 1])
|| graphemes[cur_end - 1].trim().is_empty())
{
if cur_end >= graphemes.len() {
let line = &graphemes[cur_start..].join("");
result.push_str(line);
break 'outer;
}
cur_end += 1;
}
break;
}
}
break;
}
}
while cur_end < stripped_str.len() && graphemes[cur_end].trim().is_empty() {
cur_end += 1;
}
let raw_line = graphemes[cur_start..cur_end].join("");
let line = if fmt.trim_end {
raw_line.trim()
} else {
raw_line.as_str()
};
result.push_str(line);
result.push_str(fmt.line_end);
result.push('\n');
result.push_str(&indent);
result.push_str(fmt.line_start);
cur_start = cur_end;
if let Some(new_max_chars) = max_width {
max_chars = new_max_chars.checked_sub(fmt.opener.len() + ender_length + 1)? + 1;
}
}
result.push_str(fmt.closer);
wrap_str(result, fmt.config.max_width(), fmt.shape)
}
#[cfg(test)]
mod test {
use super::{rewrite_string, StringFormat};
use shape::{Indent, Shape};
#[test]
fn issue343() {
let config = Default::default();
let fmt = StringFormat::new(Shape::legacy(2, Indent::empty()), &config);
rewrite_string("eq_", &fmt, None);
}
}