Skip to content

Commit

Permalink
Fixed incorrect whitespace parsing; added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Finomnis authored and djc committed Jul 26, 2022
1 parent b2430c2 commit 5eacc0a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/format/parse.rs
Expand Up @@ -832,6 +832,11 @@ fn test_rfc2822() {
Ok("Tue, 20 Jan 2015 17:35:20 -0800"),
), // complex trailing comment
(r"Tue, 20 Jan 2015 17:35:20 -0800 (UTC\)", Err(TOO_LONG)), // incorrect comment, not enough closing parentheses
(
"Tue, 20 Jan 2015 17:35:20 -0800 (UTC)\t \r\n(Anothercomment)",
Ok("Tue, 20 Jan 2015 17:35:20 -0800"),
), // multiple comments
("Tue, 20 Jan 2015 17:35:20 -0800 (UTC) ", Err(TOO_LONG)), // trailing whitespace after comment
("20 Jan 2015 17:35:20 -0800", Ok("Tue, 20 Jan 2015 17:35:20 -0800")), // no day of week
("20 JAN 2015 17:35:20 -0800", Ok("Tue, 20 Jan 2015 17:35:20 -0800")), // upper case month
("Tue, 20 Jan 2015 17:35 -0800", Ok("Tue, 20 Jan 2015 17:35:00 -0800")), // no second
Expand Down
12 changes: 10 additions & 2 deletions src/format/scan.rs
Expand Up @@ -355,10 +355,11 @@ pub(super) fn timezone_name_skip(s: &str) -> ParseResult<(&str, ())> {
pub(super) fn comment_2822(s: &str) -> ParseResult<(&str, ())> {
use CommentState::*;

let s = s.trim_start();

let mut state = Start;
for (i, c) in s.bytes().enumerate() {
state = match (state, c) {
(Start, b' ') => Start,
(Start, b'(') => Next(1),
(Next(1), b')') => return Ok((&s[i + 1..], ())),
(Next(depth), b'\\') => Escape(depth),
Expand All @@ -383,9 +384,12 @@ enum CommentState {
fn test_rfc2822_comments() {
let testdata = [
("", Err(TOO_SHORT)),
(" ", Err(TOO_SHORT)),
("x", Err(INVALID)),
("(", Err(TOO_SHORT)),
("()", Ok("")),
(" \r\n\t()", Ok("")),
("() ", Ok(" ")),
("()z", Ok("z")),
("(x)", Ok("")),
("(())", Ok("")),
Expand All @@ -402,6 +406,10 @@ fn test_rfc2822_comments() {

for (test_in, expected) in testdata {
let actual = comment_2822(test_in).map(|(s, _)| s);
assert_eq!(expected, actual);
assert_eq!(
expected, actual,
"{:?} expected to produce {:?}, but produced {:?}.",
test_in, expected, actual
);
}
}

0 comments on commit 5eacc0a

Please sign in to comment.