Skip to content

Commit

Permalink
Add conversion of \r\n line breaks to \n in (byte) string literals
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasKalbertodt committed Jun 4, 2021
1 parent 47bd6cf commit b07aedb
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ All notable changes to this project will be documented in this file.

## [Unreleased]
### Chaned
- Fixed string parsing by correctly handling "string continue" sequences
- Fixed (byte) string literal parsing by:
- Correctly handling "string continue" sequences
- Correctly converting `\n\r` into `\n`


## [0.2.1] - 2021-06-04
Expand Down
14 changes: 13 additions & 1 deletion src/bytestr/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn simple() {

#[test]
fn special_whitespace() {
let strings = ["\n", "\t", "foo\tbar", "baz\n", "\r\n"];
let strings = ["\n", "\t", "foo\tbar", "baz\n"];

for &s in &strings {
let input = format!(r#"b"{}""#, s);
Expand Down Expand Up @@ -97,6 +97,18 @@ bar", true, None);
bar", false, Some(0));
}

#[test]
fn crlf_newlines() {
let lit = ByteStringLit::parse("b\"foo\r\nbar\"").expect("failed to parse");
assert_eq!(lit.value(), b"foo\nbar");

let lit = ByteStringLit::parse("b\"\r\nbar\"").expect("failed to parse");
assert_eq!(lit.value(), b"\nbar");

let lit = ByteStringLit::parse("b\"foo\r\n\"").expect("failed to parse");
assert_eq!(lit.value(), b"foo\n");
}

#[test]
fn raw_byte_string() {
check!(br"", false, Some(0));
Expand Down
12 changes: 10 additions & 2 deletions src/escape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,16 @@ pub(crate) fn unescape_string<E: Escapee>(
i += len;
end_last_escape = i;
}
b'\r' if input.as_bytes()[i + 1] != b'\n'
=> return Err(perr(i, IsolatedCr)),
b'\r' => {
if input.as_bytes()[i + 1] == b'\n' {
value.push_str(&input[end_last_escape..i]);
value.push('\n');
i += 2;
end_last_escape = i;
} else {
return Err(perr(i, IsolatedCr))
}
}
b'"' => return Err(perr(i + 1..input.len(), UnexpectedChar)),
b if !E::SUPPORTS_UNICODE && !b.is_ascii()
=> return Err(perr(i, NonAsciiInByteLiteral)),
Expand Down
14 changes: 13 additions & 1 deletion src/string/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn simple() {

#[test]
fn special_whitespace() {
let strings = ["\n", "\t", "foo\tbar", "🦊\n", "\r\n"];
let strings = ["\n", "\t", "foo\tbar", "🦊\n"];

for &s in &strings {
let input = format!(r#""{}""#, s);
Expand Down Expand Up @@ -131,6 +131,18 @@ fn string_continue() {
bar", false, Some(0));
}

#[test]
fn crlf_newlines() {
let lit = StringLit::parse("\"foo\r\nbar\"").expect("failed to parse");
assert_eq!(lit.value(), "foo\nbar");

let lit = StringLit::parse("\"\r\nbar\"").expect("failed to parse");
assert_eq!(lit.value(), "\nbar");

let lit = StringLit::parse("\"лиса\r\n\"").expect("failed to parse");
assert_eq!(lit.value(), "лиса\n");
}

#[test]
fn raw_string() {
check!(r"", false, Some(0));
Expand Down

0 comments on commit b07aedb

Please sign in to comment.