mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-23 04:14:28 +00:00
Merge pull request #109 from marcusklaas/fix-string-lit
Fix off-by-one errors in rewrite_string_lit
This commit is contained in:
commit
b74cdb7b53
15
src/expr.rs
15
src/expr.rs
@ -20,7 +20,6 @@ use syntax::print::pprust;
|
|||||||
use MIN_STRING;
|
use MIN_STRING;
|
||||||
|
|
||||||
impl<'a> FmtVisitor<'a> {
|
impl<'a> FmtVisitor<'a> {
|
||||||
// TODO NEEDS TESTS
|
|
||||||
fn rewrite_string_lit(&mut self, s: &str, span: Span, width: usize, offset: usize) -> String {
|
fn rewrite_string_lit(&mut self, s: &str, span: Span, width: usize, offset: usize) -> String {
|
||||||
// FIXME I bet this stomps unicode escapes in the source string
|
// FIXME I bet this stomps unicode escapes in the source string
|
||||||
|
|
||||||
@ -40,12 +39,17 @@ impl<'a> FmtVisitor<'a> {
|
|||||||
let indent = make_indent(offset);
|
let indent = make_indent(offset);
|
||||||
let indent = &indent;
|
let indent = &indent;
|
||||||
|
|
||||||
let max_chars = width - 1;
|
|
||||||
|
|
||||||
let mut cur_start = 0;
|
let mut cur_start = 0;
|
||||||
let mut result = String::new();
|
let mut result = String::with_capacity(round_up_to_power_of_two(s.len()));
|
||||||
result.push('"');
|
result.push('"');
|
||||||
loop {
|
loop {
|
||||||
|
let max_chars = if cur_start == 0 {
|
||||||
|
// First line.
|
||||||
|
width - 2 // 2 = " + \
|
||||||
|
} else {
|
||||||
|
config!(max_width) - offset - 1 // 1 = either \ or ;
|
||||||
|
};
|
||||||
|
|
||||||
let mut cur_end = cur_start + max_chars;
|
let mut cur_end = cur_start + max_chars;
|
||||||
|
|
||||||
if cur_end >= s.len() {
|
if cur_end >= s.len() {
|
||||||
@ -64,9 +68,10 @@ impl<'a> FmtVisitor<'a> {
|
|||||||
// We can't break at whitespace, fall back to splitting
|
// We can't break at whitespace, fall back to splitting
|
||||||
// anywhere that doesn't break an escape sequence
|
// anywhere that doesn't break an escape sequence
|
||||||
cur_end = next_char(&s, cur_start + max_chars);
|
cur_end = next_char(&s, cur_start + max_chars);
|
||||||
while s.char_at(cur_end) == '\\' {
|
while s.char_at(prev_char(&s, cur_end)) == '\\' {
|
||||||
cur_end = prev_char(&s, cur_end);
|
cur_end = prev_char(&s, cur_end);
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Make sure there is no whitespace to the right of the break.
|
// Make sure there is no whitespace to the right of the break.
|
||||||
|
21
tests/source/string-lit.rs
Normal file
21
tests/source/string-lit.rs
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
// Long string literals
|
||||||
|
|
||||||
|
fn main() -> &'static str {
|
||||||
|
let str = "AAAAAAAAAAAAAAaAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAaAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAaAa";
|
||||||
|
let str = "AAAAAAAAAAAAAAaAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAaAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAaAa";
|
||||||
|
let str = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
|
||||||
|
|
||||||
|
let too_many_lines = "H\
|
||||||
|
e\
|
||||||
|
l\
|
||||||
|
l\
|
||||||
|
o";
|
||||||
|
|
||||||
|
// Make sure we don't break after an escape character.
|
||||||
|
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";
|
||||||
|
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";
|
||||||
|
|
||||||
|
let really_long_variable_name = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
|
||||||
|
|
||||||
|
"stuff"
|
||||||
|
}
|
23
tests/target/string-lit.rs
Normal file
23
tests/target/string-lit.rs
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
// Long string literals
|
||||||
|
|
||||||
|
fn main() -> &'static str {
|
||||||
|
let str = "AAAAAAAAAAAAAAaAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAaAA \
|
||||||
|
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAaAa";
|
||||||
|
let str = "AAAAAAAAAAAAAAaAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAaAAAAAAAAAAAAAAAAAAAAAA\
|
||||||
|
AAAAAAAAAAAaAa";
|
||||||
|
let str = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
|
||||||
|
|
||||||
|
let too_many_lines = "Hello";
|
||||||
|
|
||||||
|
// Make sure we don't break after an escape character.
|
||||||
|
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";
|
||||||
|
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";
|
||||||
|
|
||||||
|
let really_long_variable_name = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\
|
||||||
|
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\
|
||||||
|
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
|
||||||
|
|
||||||
|
"stuff"
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user