Skip to content

Instantly share code, notes, and snippets.

@jasdev
Last active January 26, 2021 01:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasdev/68979b0e302fc1d46ca2c20f1fe68b7b to your computer and use it in GitHub Desktop.
Save jasdev/68979b0e302fc1d46ca2c20f1fe68b7b to your computer and use it in GitHub Desktop.
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