Skip to content

Commit b74cdb7

Browse files
committed
Merge pull request #109 from marcusklaas/fix-string-lit
Fix off-by-one errors in rewrite_string_lit
2 parents 129e2f1 + 963eafe commit b74cdb7

File tree

3 files changed

+54
-5
lines changed

3 files changed

+54
-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.

tests/source/string-lit.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Long string literals
2+
3+
fn main() -> &'static str {
4+
let str = "AAAAAAAAAAAAAAaAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAaAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAaAa";
5+
let str = "AAAAAAAAAAAAAAaAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAaAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAaAa";
6+
let str = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
7+
8+
let too_many_lines = "H\
9+
e\
10+
l\
11+
l\
12+
o";
13+
14+
// Make sure we don't break after an escape character.
15+
let odd_length_name = "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
16+
let even_length_name = "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
17+
18+
let really_long_variable_name = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
19+
20+
"stuff"
21+
}

tests/target/string-lit.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Long string literals
2+
3+
fn main() -> &'static str {
4+
let str = "AAAAAAAAAAAAAAaAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAaAA \
5+
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAaAa";
6+
let str = "AAAAAAAAAAAAAAaAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAaAAAAAAAAAAAAAAAAAAAAAA\
7+
AAAAAAAAAAAaAa";
8+
let str = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
9+
10+
let too_many_lines = "Hello";
11+
12+
// Make sure we don't break after an escape character.
13+
let odd_length_name = "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\
14+
\n\n";
15+
let even_length_name = "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\
16+
\n\n\n";
17+
18+
let really_long_variable_name = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\
19+
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\
20+
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
21+
22+
"stuff"
23+
}

0 commit comments

Comments
 (0)