1
Fork 0

shift no characters when using raw string literals

remove `find_skips`

remove unnecessary variables
This commit is contained in:
Takayuki Maeda 2022-11-09 22:40:51 +09:00
parent 0aaad9e757
commit 9857de218f
5 changed files with 115 additions and 88 deletions

View file

@ -818,13 +818,18 @@ fn find_skips_from_snippet(
_ => return (vec![], false), _ => return (vec![], false),
}; };
fn find_skips(snippet: &str, is_raw: bool) -> Vec<usize> { if str_style.is_some() {
return (vec![], true);
}
let snippet = &snippet[1..snippet.len() - 1];
let mut s = snippet.char_indices(); let mut s = snippet.char_indices();
let mut skips = vec![]; let mut skips = vec![];
while let Some((pos, c)) = s.next() { while let Some((pos, c)) = s.next() {
match (c, s.clone().next()) { match (c, s.clone().next()) {
// skip whitespace and empty lines ending in '\\' // skip whitespace and empty lines ending in '\\'
('\\', Some((next_pos, '\n'))) if !is_raw => { ('\\', Some((next_pos, '\n'))) => {
skips.push(pos); skips.push(pos);
skips.push(next_pos); skips.push(next_pos);
let _ = s.next(); let _ = s.next();
@ -842,7 +847,7 @@ fn find_skips_from_snippet(
skips.push(next_pos); skips.push(next_pos);
let _ = s.next(); let _ = s.next();
} }
('\\', Some((_, 'x'))) if !is_raw => { ('\\', Some((_, 'x'))) => {
for _ in 0..3 { for _ in 0..3 {
// consume `\xAB` literal // consume `\xAB` literal
if let Some((pos, _)) = s.next() { if let Some((pos, _)) = s.next() {
@ -852,7 +857,7 @@ fn find_skips_from_snippet(
} }
} }
} }
('\\', Some((_, 'u'))) if !is_raw => { ('\\', Some((_, 'u'))) => {
if let Some((pos, _)) = s.next() { if let Some((pos, _)) = s.next() {
skips.push(pos); skips.push(pos);
} }
@ -873,8 +878,7 @@ fn find_skips_from_snippet(
// exclude as many digits as it is greater than 1 byte // exclude as many digits as it is greater than 1 byte
// //
// So for a 3 byte character, exclude 2 digits // So for a 3 byte character, exclude 2 digits
let required_skips = let required_skips = digits_len.saturating_sub(len_utf8.saturating_sub(1));
digits_len.saturating_sub(len_utf8.saturating_sub(1));
// skip '{' and '}' also // skip '{' and '}' also
for pos in (next_pos..).take(required_skips + 2) { for pos in (next_pos..).take(required_skips + 2) {
@ -901,13 +905,7 @@ fn find_skips_from_snippet(
_ => {} _ => {}
} }
} }
skips (skips, true)
}
let r_start = str_style.map_or(0, |r| r + 1);
let r_end = str_style.unwrap_or(0);
let s = &snippet[r_start + 1..snippet.len() - r_end - 1];
(find_skips(s, str_style.is_some()), true)
} }
#[cfg(test)] #[cfg(test)]

View file

@ -0,0 +1,3 @@
fn main() {
println!(r#"\'\'\'\'\'\'\'\'\'\'\'\'\'\'}"#); //~ ERROR invalid format string: unmatched `}` found
}

View file

@ -0,0 +1,10 @@
error: invalid format string: unmatched `}` found
--> $DIR/format-raw-string-error.rs:2:45
|
LL | println!(r#"\'\'\'\'\'\'\'\'\'\'\'\'\'\'}"#);
| ^ unmatched `}` in format string
|
= note: if you intended to print `}`, you can escape it using `}}`
error: aborting due to previous error

View file

@ -0,0 +1,6 @@
fn main() {
println!(
r#"
\"\'}、"# //~ ERROR invalid format string: unmatched `}` found
);
}

View file

@ -0,0 +1,10 @@
error: invalid format string: unmatched `}` found
--> $DIR/issue-104142.rs:4:9
|
LL | \"\'}、"#
| ^ unmatched `}` in format string
|
= note: if you intended to print `}`, you can escape it using `}}`
error: aborting due to previous error