Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid crashes for files with position information following the timestamp #3

Merged
merged 2 commits into from
Jun 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 <contact@gavalas.dev>"]
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);
}
}