private int[] ParseLineStarts() { // Corner case check if (0 == this.Length) { return(new[] { 0 }); } var lineStarts = ArrayBuilder <int> .GetInstance(); lineStarts.Add(0); // there is always the first line var lastWasCR = false; // The following loop goes through every character in the text. It is highly // performance critical, and thus inlines knowledge about common line breaks // and non-line breaks. EnumerateChars((int position, char[] buffer, int length) => { var index = 0; if (lastWasCR) { if (length > 0 && buffer[0] == '\n') { index++; } lineStarts.Add(position + index); lastWasCR = false; } while (index < length) { char c = buffer[index]; index++; // Common case - ASCII & not a line break // if (c > '\r' && c <= 127) // if (c >= ('\r'+1) && c <= 127) const uint bias = '\r' + 1; if (unchecked (c - bias) <= (127 - bias)) { continue; } // Assumes that the only 2-char line break sequence is CR+LF if (c == '\r') { if (index < length && buffer[index] == '\n') { index++; } else if (index >= length) { lastWasCR = true; continue; } } else if (!TextUtilities.IsAnyLineBreakCharacter(c)) { continue; } // next line starts at index lineStarts.Add(position + index); } }); return(lineStarts.ToArrayAndFree()); }