private List<ScriptLine> GetScriptLines(string scriptText)
{
var currentLineBuilder = new StringBuilder();
var result = new List<ScriptLine>();
var currentLine = new ScriptLine();
if (scriptText.Length > 0)
{
result.Add(currentLine);
}
for (var i = 0; i < scriptText.Length; i++)
{
var currChar = scriptText[i];
var separatorLength = 1;
// this is either a legacy maCOs newline, or it will be followed by \n for the windows one
// we could also be at the last character of the file when that isn't a newline
if ((currChar == '\r' || currChar == '\n') || i == scriptText.Length - 1)
{
if (currChar != '\r' && currChar != '\n')
{
currentLineBuilder.Append(scriptText[i]);
}
if (currChar == '\r' && i < scriptText.Length - 1 && scriptText[i + 1] == '\n')
{
// skip the next character since it's part of an \r\n sequence
i++;
separatorLength = 2;
}
var prevIndex = 0;
var prevCount = 0;
var prevSeparator = 0;
// if we're not still processing the first line, look at the prev line's starting index
if (result.Count > 1)
{
var rpev = result[result.Count - 2];
prevIndex = rpev.StartingIndex;
prevCount = rpev.LineText.Length;
prevSeparator = rpev.NewLineLength;
}
currentLine.LineText = currentLineBuilder.ToString();
currentLine.StartingIndex = prevIndex + prevCount + prevSeparator;
currentLine.NewLineLength = separatorLength;
currentLine = new ScriptLine();
result.Add(currentLine);
currentLineBuilder.Clear();
}
else
{
currentLineBuilder.Append(scriptText[i]);
}
}
if (currentLine.LineText == null && currentLine.StartingIndex == 0)
{
result.Remove(currentLine);
}
return result;
}