Skip to content

Commit

Permalink
Merge pull request #3 from lucas-deangelis/master
Browse files Browse the repository at this point in the history
Avoid crashes for files with position information following the timestamp
  • Loading branch information
GavalasDev authored Jun 4, 2022
2 parents 0fe3b0e + 5640d5b commit 6150e7c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "srtlib"
version = "0.1.1"
version = "0.1.2"
authors = ["Konstantinos Gavalas <[email protected]>"]
edition = "2018"
description = "A simple library for handling .srt subtitle files"
Expand Down
22 changes: 19 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,9 @@ impl Subtitle {
}
}

/// Construct a new subtitle by parsing a string with the format "num\nstart --> end\ntext"
/// where start and end are timestamps using the format hours:minutes:seconds,milliseconds.
/// Construct a new subtitle by parsing a string with the format "num\nstart --> end\ntext" or the format
/// "num\nstart --> end position_information\ntext" where start and end are timestamps using the format
/// hours:minutes:seconds,milliseconds ; and position_information is position information of any format
///
/// # Errors
///
Expand All @@ -337,8 +338,10 @@ impl Subtitle {
.next()
.ok_or(ParsingError::BadSubtitleStructure(num))?,
)?;
let end_with_possible_position_info = time_iter.next().ok_or(ParsingError::BadSubtitleStructure(num))?;
let end = Timestamp::parse(
time_iter
end_with_possible_position_info
.split(" ")
.next()
.ok_or(ParsingError::BadSubtitleStructure(num))?,
)?;
Expand Down Expand Up @@ -771,4 +774,17 @@ mod tests {
let subs = Subtitles::parse_from_str(String::new()).expect("Failed to parse empty subs");
assert_eq!(subs.len(), 0);
}

#[test]
fn subtitle_with_position_information() {
let input = "1\n00:00:07,001 --> 00:00:09,015 position:50,00%,middle align:middle size:80,00% line:84,67%\nThis is a subtitle text";
let result = Subtitle::new(
1,
Timestamp::new(0, 0, 7, 1),
Timestamp::new(0, 0, 9, 15),
"This is a subtitle text".to_string(),
);

assert_eq!(Subtitle::parse(input.to_string()).unwrap(), result);
}
}

0 comments on commit 6150e7c

Please sign in to comment.