public int GetEscapedChars(int offset, int length, char[] chars)
{
byte[] buffer = _buffer;
int charCount = 0;
int textOffset = offset;
int offsetMax = offset + length;
while (true)
{
while (offset < offsetMax && IsAttrChar(buffer[offset]))
offset++;
charCount += GetChars(textOffset, offset - textOffset, chars, charCount);
if (offset == offsetMax)
break;
textOffset = offset;
if (buffer[offset] == '&')
{
while (offset < offsetMax && buffer[offset] != ';')
offset++;
offset++;
int ch = GetCharEntity(textOffset, offset - textOffset);
textOffset = offset;
if (ch > char.MaxValue)
{
SurrogateChar surrogate = new SurrogateChar(ch);
chars[charCount++] = surrogate.HighChar;
chars[charCount++] = surrogate.LowChar;
}
else
{
chars[charCount++] = (char)ch;
}
}
else if (buffer[offset] == '\n' || buffer[offset] == '\t')
{
chars[charCount++] = ' ';
offset++;
textOffset = offset;
}
else // '\r'
{
chars[charCount++] = ' ';
offset++;
if (offset < offsetMax && buffer[offset] == '\n')
offset++;
textOffset = offset;
}
}
return charCount;
}