private int AddUTF8Token(int start)
{
int end = start + NextInt(20);
if (buffer.Length < 1 + end)
{
char[] newBuffer = new char[(int) ((1 + end) * 1.25)];
Array.Copy(buffer, 0, newBuffer, 0, buffer.Length);
buffer = newBuffer;
}
for (int i = start; i < end; i++)
{
int t = NextInt(6);
if (0 == t && i < end - 1)
{
// Make a surrogate pair
// High surrogate
buffer[i++] = (char) NextInt(0xd800, 0xdc00);
// Low surrogate
buffer[i] = (char) NextInt(0xdc00, 0xe000);
}
else if (t <= 1)
buffer[i] = (char) NextInt(0x80);
else if (2 == t)
buffer[i] = (char) NextInt(0x80, 0x800);
else if (3 == t)
buffer[i] = (char) NextInt(0x800, 0xd800);
else if (4 == t)
buffer[i] = (char) NextInt(0xe000, 0xffff);
else if (5 == t)
{
// Illegal unpaired surrogate
if (r.NextDouble() > 0.5)
buffer[i] = (char) NextInt(0xd800, 0xdc00);
else
buffer[i] = (char) NextInt(0xdc00, 0xe000);
}
}
buffer[end] = ' ';
return 1 + end;
}