-
-
Save jasdev/68979b0e302fc1d46ca2c20f1fe68b7b to your computer and use it in GitHub Desktop.
The final part of the SRT subtitle group parser.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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