Skip to content

Instantly share code, notes, and snippets.

Last active Jan 26, 2021
What would you like to do?
The final part of the SRT subtitle group parser.
struct SubtitleGroup {
var sequenceNumber: Int
var startTimecode: TimeInterval
var endTimecode: TimeInterval
var substring: String
let srtGroupParser = Int.parser(isSigned: false)
.orElse(PrefixUpTo("\r\n\r\n")) // To handle blank lines on Windows.
.orElse(Rest()) // For the last group, since there might not be a trailing blank line.
.utf8 // The trailing `.utf8` here is important. It allows the subtitle body to be parsed
// as a `Substring`, to get fuller Unicode handling before converting back out to an
// `Parsers.SubstringToUTF8View`.
.map { sequenceNumber, timecodes, substring in
sequenceNumber: sequenceNumber,
startTimecode: timecodes.0,
endTimecode: timecodes.1,
substring: String(substring)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment