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.
This commit is contained in:
Sebastian Dröge 2020-04-13 14:43:42 +03:00
parent bd90d1d9a6
commit 163e8cf1a0

View file

@ -40,10 +40,9 @@ pub enum SccLine {
#[derive(Clone, Copy, Debug, PartialEq, Eq)] #[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum State { enum State {
Init,
Header, Header,
Empty, Empty,
Captions, CaptionOrEmpty,
} }
#[derive(Debug)] #[derive(Debug)]
@ -213,11 +212,13 @@ where
/// SCC parser the parses line-by-line and keeps track of the current state in the file. /// SCC parser the parses line-by-line and keeps track of the current state in the file.
impl SccParser { impl SccParser {
pub fn new() -> Self { pub fn new() -> Self {
Self { state: State::Init } Self {
state: State::Header,
}
} }
pub fn reset(&mut self) { pub fn reset(&mut self) {
self.state = State::Init; self.state = State::Header;
} }
pub fn parse_line<'a>( pub fn parse_line<'a>(
@ -225,34 +226,24 @@ impl SccParser {
line: &'a [u8], line: &'a [u8],
) -> Result<SccLine, combine::easy::Errors<u8, &'a [u8], combine::stream::PointerOffset>> { ) -> Result<SccLine, combine::easy::Errors<u8, &'a [u8], combine::stream::PointerOffset>> {
match self.state { match self.state {
State::Init => header() State::Header => header()
.message("while in Init state")
.easy_parse(line)
.map(|v| {
self.state = State::Header;
v.0
}),
State::Header => empty_line()
.message("while in Header state") .message("while in Header state")
.easy_parse(line) .easy_parse(line)
.map(|v| { .map(|v| {
self.state = State::Empty; self.state = State::Empty;
v.0 v.0
}), }),
State::Empty => caption() State::Empty => empty_line()
.message("while in Empty state") .message("while in Empty state")
.easy_parse(line) .easy_parse(line)
.map(|v| { .map(|v| {
self.state = State::Captions; self.state = State::CaptionOrEmpty;
v.0 v.0
}), }),
State::Captions => empty_line() State::CaptionOrEmpty => choice!(caption(), empty_line())
.message("while in Captions state") .message("while in CaptionOrEmpty state")
.easy_parse(line) .easy_parse(line)
.map(|v| { .map(|v| v.0),
self.state = State::Empty;
v.0
}),
} }
} }
} }