private static bool TryFindEncodedCodePointBytesCountGoingBackwards(Span<byte> buffer, out int encodedBytes)
{
encodedBytes = 1;
Span<byte> it = buffer;
// TODO: Should we have something like: Span<byte>.(Slice from the back)
for (; encodedBytes <= UnicodeConstants.Utf8MaxCodeUnitsPerCodePoint; encodedBytes++, it = it.Slice(0, it.Length - 1))
{
if (it.Length == 0)
{
encodedBytes = default(int);
return false;
}
// TODO: Should we have Span<byte>.Last?
if (Utf8CodeUnit.IsFirstCodeUnitInEncodedCodePoint((Utf8CodeUnit)it[it.Length - 1]))
{
// output: encodedBytes
return true;
}
}
// Invalid unicode character or stream prematurely ended (which is still invalid character in that stream)
encodedBytes = default(int);
return false;
}