public unsafe static bool TryEncodeCodePoint(UnicodeCodePoint codePoint, char* buffer, out int encodedChars)
{
if (!UnicodeCodePoint.IsSupportedCodePoint(codePoint))
{
encodedChars = default(int);
return false;
}
// TODO: Can we add this in UnicodeCodePoint class?
// Should be represented as Surrogate?
encodedChars = ((uint)codePoint >= 0x10000) ? 2 : 1;
/*
Never happens. Max encodedBytes = 4 bytes = 2 chars. We already preallocate 2 chars for every UTF8 byte.
if (buffer.Length < encodedBytes)
{
codePoint = default(UnicodeCodePoint);
encodedBytes = default(int);
// buffer too small
return false;
}
*/
if (encodedChars == 1)
{
unchecked
{
Write(buffer, (ushort)codePoint);
}
}
else
{
unchecked
{
uint highSurrogate = ((uint)(codePoint.Value - 0x10000) >> 10) + UnicodeConstants.Utf16HighSurrogateFirstCodePoint;
uint lowSurrogate = ((uint)codePoint & MaskLow10Bits) + UnicodeConstants.Utf16LowSurrogateFirstCodePoint;
Write(buffer, highSurrogate | (lowSurrogate << 16));
}
}
return true;
}