diff --git a/src/format/parse.rs b/src/format/parse.rs index 7ff5ca0c35..0e2db8fae9 100644 --- a/src/format/parse.rs +++ b/src/format/parse.rs @@ -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 diff --git a/src/format/scan.rs b/src/format/scan.rs index cf139c0ad1..9a40903436 100644 --- a/src/format/scan.rs +++ b/src/format/scan.rs @@ -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), @@ -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("")), @@ -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 + ); } }