/// <summary>Parses the specified text, using the specified format.</summary>
/// <remarks>The created <see cref="SubtitleCollection" /> will have its <see cref="SubtitleProperties" /> property set to null.
/// It is mandatory to use <see cref="SubtitleCollection.SetPropertiesForAll" /> after.</remarks>
internal ParsingProperties Parse (string text, SubtitleFormat format, float inputFrameRate,
out SubtitleCollection collection, out IncompleteSubtitleCollection incompleteSubtitles){
collection = new SubtitleCollection();
incompleteSubtitles = new IncompleteSubtitleCollection();
ParsingProperties properties = new ParsingProperties();
properties.InputFrameRate = inputFrameRate;
Regex subtitleRegex = null;
int bodyIndex = 0;
text = ClearComments(text, format);
/* Read the headers if available */
if (format.Mode == SubtitleMode.Both) {
//Read headers to know if format is using Times or Frames
bodyIndex = text.Length;
int lastIndex = ReadHeaders(text, bodyIndex, format, properties);
subtitleRegex = CreateSubtitleRegex(format, properties.TimingMode);
/* Detect body index from matching the first subtitle or the end of headers */
bodyIndex = FindBodyIndex(text, format, subtitleRegex);
if (lastIndex > bodyIndex)
bodyIndex = lastIndex;
}
else {
//End of headers is detected by start of subtitles' body
properties.TimingMode = format.ModeAsTimingMode;
subtitleRegex = CreateSubtitleRegex(format);
bodyIndex = FindBodyIndex(text, format, subtitleRegex);
ReadHeaders(text, bodyIndex, format, properties);
}
/* Get properties from the whole input, if available */
format.GlobalInputGetProperties(text, properties);
int textLength = text.Length;
/* Read the subtitles */
bodyIndex = ReadSubtitles(text, bodyIndex, textLength, subtitleRegex, format,
properties, collection, incompleteSubtitles);
/* Read the end text of the subtitles */
bodyIndex = ReadBodyEnd(text, bodyIndex, format, collection, incompleteSubtitles);
/* Check if there's still text remaining */
if ((bodyIndex < textLength) && includeIncompleteSubtitles)
AddIncompleteSubtitle(incompleteSubtitles, text.Substring(bodyIndex), collection.Count);
return properties;
}