private static unsafe int GetNextUnicodeScalarValueFromUtf16Surrogate(ref char* pch, ref int charsRemaining)
{
// invariants
Debug.Assert(charsRemaining >= 1);
Debug.Assert(Char.IsSurrogate(*pch));
if (charsRemaining <= 1)
{
// not enough characters remaining to resurrect the original scalar value
return UnicodeReplacementChar;
}
char leadingSurrogate = pch[0];
char trailingSurrogate = pch[1];
if (Char.IsSurrogatePair(leadingSurrogate, trailingSurrogate))
{
// we're going to consume an extra char
pch++;
charsRemaining--;
// below code is from Char.ConvertToUtf32, but without the checks (since we just performed them)
return (((leadingSurrogate - HIGH_SURROGATE_START) * 0x400) + (trailingSurrogate - LOW_SURROGATE_START) + UNICODE_PLANE01_START);
}
else
{
// unmatched surrogate
return UnicodeReplacementChar;
}
}