System.Text.Utf16.Utf16LittleEndianEncoder.TryEncodeCodePoint C# (CSharp) Метод

TryEncodeCodePoint() публичный статический Метод

public static TryEncodeCodePoint ( System.Text.UnicodeCodePoint codePoint, char buffer, int &encodedChars ) : bool
codePoint System.Text.UnicodeCodePoint
buffer char
encodedChars int
Результат bool
        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;
        }

Usage Example

Пример #1
0
        public override bool TryEncodeFromUtf8(ReadOnlySpan <byte> utf8, Span <byte> buffer, out int bytesWritten)
        {
            bytesWritten = 0;
            int justWritten;

            foreach (var cp in new Utf8String(utf8).CodePoints)
            {
                if (!Utf16LittleEndianEncoder.TryEncodeCodePoint(cp, buffer.Slice(bytesWritten), out justWritten))
                {
                    bytesWritten = 0;
                    return(false);
                }
                bytesWritten += justWritten;
            }
            return(true);
        }
All Usage Examples Of System.Text.Utf16.Utf16LittleEndianEncoder::TryEncodeCodePoint