From 163e8cf1a074cb02f180e647277a31ddee62d199 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Mon, 13 Apr 2020 14:43:42 +0300 Subject: [PATCH] closedcaption/sccparse: Don't strictly require empty lines between each caption line There is some broken software out there not inserting the empty lines and we don't really need them for proper parsing. Only require an empty line between header and the first caption line. --- video/closedcaption/src/scc_parser.rs | 31 ++++++++++----------------- 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/video/closedcaption/src/scc_parser.rs b/video/closedcaption/src/scc_parser.rs index 2757251b..a203b479 100644 --- a/video/closedcaption/src/scc_parser.rs +++ b/video/closedcaption/src/scc_parser.rs @@ -40,10 +40,9 @@ pub enum SccLine { #[derive(Clone, Copy, Debug, PartialEq, Eq)] enum State { - Init, Header, Empty, - Captions, + CaptionOrEmpty, } #[derive(Debug)] @@ -213,11 +212,13 @@ where /// SCC parser the parses line-by-line and keeps track of the current state in the file. impl SccParser { pub fn new() -> Self { - Self { state: State::Init } + Self { + state: State::Header, + } } pub fn reset(&mut self) { - self.state = State::Init; + self.state = State::Header; } pub fn parse_line<'a>( @@ -225,34 +226,24 @@ impl SccParser { line: &'a [u8], ) -> Result> { match self.state { - State::Init => header() - .message("while in Init state") - .easy_parse(line) - .map(|v| { - self.state = State::Header; - v.0 - }), - State::Header => empty_line() + State::Header => header() .message("while in Header state") .easy_parse(line) .map(|v| { self.state = State::Empty; v.0 }), - State::Empty => caption() + State::Empty => empty_line() .message("while in Empty state") .easy_parse(line) .map(|v| { - self.state = State::Captions; + self.state = State::CaptionOrEmpty; v.0 }), - State::Captions => empty_line() - .message("while in Captions state") + State::CaptionOrEmpty => choice!(caption(), empty_line()) + .message("while in CaptionOrEmpty state") .easy_parse(line) - .map(|v| { - self.state = State::Empty; - v.0 - }), + .map(|v| v.0), } } }