Skip to content

Commit 74f050b

Browse files
committed
Fix off-by-one bugs in rewrite_string_lit
Multi-line literals would typically have a character too many. Splitting of escape sequences also wasn't working correctly.
1 parent c7ecf74 commit 74f050b

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/expr.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use syntax::print::pprust;
2020
use MIN_STRING;
2121

2222
impl<'a> FmtVisitor<'a> {
23-
// TODO NEEDS TESTS
2423
fn rewrite_string_lit(&mut self, s: &str, span: Span, width: usize, offset: usize) -> String {
2524
// FIXME I bet this stomps unicode escapes in the source string
2625

@@ -40,12 +39,17 @@ impl<'a> FmtVisitor<'a> {
4039
let indent = make_indent(offset);
4140
let indent = &indent;
4241

43-
let max_chars = width - 1;
44-
4542
let mut cur_start = 0;
46-
let mut result = String::new();
43+
let mut result = String::with_capacity(round_up_to_power_of_two(s.len()));
4744
result.push('"');
4845
loop {
46+
let max_chars = if cur_start == 0 {
47+
// First line.
48+
width - 2 // 2 = " + \
49+
} else {
50+
config!(max_width) - offset - 1 // 1 = either \ or ;
51+
};
52+
4953
let mut cur_end = cur_start + max_chars;
5054

5155
if cur_end >= s.len() {
@@ -64,9 +68,10 @@ impl<'a> FmtVisitor<'a> {
6468
// We can't break at whitespace, fall back to splitting
6569
// anywhere that doesn't break an escape sequence
6670
cur_end = next_char(&s, cur_start + max_chars);
67-
while s.char_at(cur_end) == '\\' {
71+
while s.char_at(prev_char(&s, cur_end)) == '\\' {
6872
cur_end = prev_char(&s, cur_end);
6973
}
74+
break;
7075
}
7176
}
7277
// Make sure there is no whitespace to the right of the break.

0 commit comments

Comments
 (0)