Fix indexing panic on unicode whitespaces

This commit is contained in:
Shotaro Yamada 2019-03-29 20:12:45 +09:00
parent 09940a70d0
commit c0ff894a22
3 changed files with 21 additions and 2 deletions

View File

@ -651,7 +651,7 @@ pub fn get_comment_end(
if let Some(i) = block_open_index {
match post_snippet.find('/') {
Some(j) if j < i => block_open_index = None,
_ if i > 0 && &post_snippet[i - 1..i] == "/" => block_open_index = None,
_ if post_snippet[..i].chars().last() == Some('/') => block_open_index = None,
_ => (),
}
}
@ -692,8 +692,13 @@ pub fn has_extra_newline(post_snippet: &str, comment_end: usize) -> bool {
return false;
}
let len_last = post_snippet[..comment_end]
.chars()
.last()
.unwrap()
.len_utf8();
// Everything from the separator to the next item.
let test_snippet = &post_snippet[comment_end - 1..];
let test_snippet = &post_snippet[comment_end - len_last..];
let first_newline = test_snippet
.find('\n')
.unwrap_or_else(|| test_snippet.len());

View File

@ -0,0 +1,7 @@
fn issue_2995() {
// '\u{2028}' is inserted in the code below.
[0, 1];
[0, /* */ 1];
[0,1];
}

View File

@ -0,0 +1,7 @@
fn issue_2995() {
// '\u{2028}' is inserted in the code below.
[0, 1];
[0, /* */ 1];
[0, 1];
}