Skip to content

Instantly share code, notes, and snippets.

@jasdev
Last active Jan 26, 2021
Embed
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)
.skip(Newline())
.take(timecodeLineParser)
.skip(Newline())
.take(
PrefixUpTo("\n\n")
.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
SubtitleGroup(
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