TryEncodeCodePoint() public static method

public static TryEncodeCodePoint ( UnicodeCodePoint codePoint, Span buffer, int &encodedBytes ) : bool
codePoint UnicodeCodePoint
buffer Span
encodedBytes int
return bool
示例#1
0
        public unsafe Utf8EncodedCodePoint(char highSurrogate, char lowSurrogate) : this()
        {
            UnicodeCodePoint codePoint = (UnicodeCodePoint)(uint)char.ConvertToUtf32(highSurrogate, lowSurrogate);

            fixed(byte *encodedData = &_byte0)
            {
                Span <byte> buffer = new Span <byte>(encodedData, 4);

                if (!Utf8Encoder.TryEncodeCodePoint(codePoint, buffer, out _length))
                {
                    // TODO: Change exception type
                    throw new Exception("Internal error: this should never happen as codePoint should be within acceptable range");
                }
            }
        }
示例#2
0
        // TODO: reevaluate implementation
        public Utf8String(IEnumerable <UnicodeCodePoint> codePoints)
        {
            int len     = GetUtf8LengthInBytes(codePoints);
            var newSpan = new Span <byte>(new byte[len]);

            _buffer = newSpan;
            foreach (UnicodeCodePoint codePoint in codePoints)
            {
                int encodedBytes;
                if (!Utf8Encoder.TryEncodeCodePoint(codePoint, newSpan, out encodedBytes))
                {
                    throw new ArgumentException("Invalid code point", "codePoints");
                }
                newSpan = newSpan.Slice(encodedBytes);
            }
        }
示例#3
0
        // TODO: reevaluate implementation
        public Utf8String(IEnumerable <uint> codePoints)
        {
            var len    = GetUtf8LengthInBytes(codePoints);
            var buffer = new Span <byte>(new byte[len]);
            var index  = 0;

            foreach (uint codePoint in codePoints)
            {
                int written;
                if (!Utf8Encoder.TryEncodeCodePoint(codePoint, buffer, index, out written))
                {
                    throw new ArgumentException("Invalid code point", "codePoints");
                }

                index += written;
            }

            _buffer = buffer;
        }
示例#4
0
        public unsafe Utf8EncodedCodePoint(char character) : this()
        {
            if (char.IsSurrogate(character))
            {
                throw new ArgumentOutOfRangeException("character", "Surrogate characters are not allowed");
            }

            UnicodeCodePoint codePoint = (UnicodeCodePoint)(uint)character;

            fixed(byte *encodedData = &_byte0)
            {
                ByteSpan buffer = new ByteSpan(encodedData, 4);

                if (!Utf8Encoder.TryEncodeCodePoint(codePoint, buffer, out _length))
                {
                    // TODO: Change exception type
                    throw new Exception("Internal error: this should never happen as codePoint is within acceptable range and is not surrogate");
                }
            }
        }
示例#5
0
        // TODO: This should return Utf16CodeUnits which should wrap byte[]/Span<byte>, same for other encoders
        private static byte[] GetUtf8BytesFromString(string s)
        {
            int len = 0;

            for (int i = 0; i < s.Length; /* intentionally no increment */)
            {
                UnicodeCodePoint codePoint;
                int encodedChars;
                if (!Utf16LittleEndianEncoder.TryDecodeCodePointFromString(s, i, out codePoint, out encodedChars))
                {
                    throw new ArgumentException("s", "Invalid surrogate pair in the string.");
                }

                if (encodedChars <= 0)
                {
                    // TODO: Fix exception type
                    throw new Exception("internal error");
                }

                int encodedBytes = Utf8Encoder.GetNumberOfEncodedBytes(codePoint);
                if (encodedBytes == 0)
                {
                    // TODO: Fix exception type
                    throw new Exception("Internal error: Utf16Decoder somehow got CodePoint out of range");
                }
                len += encodedBytes;

                i += encodedChars;
            }

            byte[] bytes = new byte[len];
            unsafe
            {
                fixed(byte *array_pinned = bytes)
                {
                    Span <byte> p = new Span <byte>(array_pinned, len);

                    for (int i = 0; i < s.Length; /* intentionally no increment */)
                    {
                        UnicodeCodePoint codePoint;
                        int encodedChars;
                        if (Utf16LittleEndianEncoder.TryDecodeCodePointFromString(s, i, out codePoint, out encodedChars))
                        {
                            i += encodedChars;
                            int encodedBytes;
                            if (Utf8Encoder.TryEncodeCodePoint(codePoint, p, out encodedBytes))
                            {
                                p = p.Slice(encodedBytes);
                            }
                            else
                            {
                                // TODO: Fix exception type
                                throw new Exception("Internal error: Utf16Decoder somehow got CodePoint out of range or the buffer is too small");
                            }
                        }
                        else
                        {
                            // TODO: Fix exception type
                            throw new Exception("Internal error: we did pre-validation of the string, nothing should go wrong");
                        }
                    }
                }
            }

            return(bytes);
        }