public unsafe void WriteString(string value)
{
if (value == null)
{
WriteByteZero();
return;
}
var l = value.Length;
if (l == 0)
{
WriteUInt8(1);
return;
}
var buf = Buf;
var pos = Pos;
var end = End;
fixed (char* strPtrStart = value)
{
var strPtr = strPtrStart;
var strPtrEnd = strPtrStart + l;
var toEncode = (uint)(l + 1);
doEncode:
var toEncodeLen = PackUnpack.LengthVUInt(toEncode);
if (pos + toEncodeLen <= end)
{
PackUnpack.PackVUInt(buf, ref pos, toEncode);
}
else
{
Pos = pos;
WriteVUInt32(toEncode);
buf = Buf;
pos = Pos;
end = End;
}
while (strPtr != strPtrEnd)
{
var c = *strPtr++;
if (c < 0x80)
{
if (pos >= end)
{
Pos = pos;
FlushBuffer();
buf = Buf;
pos = Pos;
end = End;
}
buf[pos++] = (byte)c;
}
else
{
if (char.IsHighSurrogate(c) && strPtr != strPtrEnd)
{
var c2 = *strPtr;
if (char.IsLowSurrogate(c2))
{
toEncode = (uint)((c - 0xD800) * 0x400 + (c2 - 0xDC00) + 0x10000);
strPtr++;
goto doEncode;
}
}
toEncode = c;
goto doEncode;
}
}
Pos = pos;
}
}