Beispiel #1
0
        public override bool TryComputeEncodedBytes(ReadOnlySpan <byte> utf8, out int bytesNeeded)
        {
            var result = Utf8Encoder.TryComputeStringLength(utf8, out int characters);

            bytesNeeded = characters * sizeof(char);
            return(result);
        }
Beispiel #2
0
        public override bool TryDecode(ReadOnlySpan <byte> encodedBytes, out string text, out int bytesConsumed)
        {
            int len;

            if (!Utf8Encoder.TryComputeStringLength(encodedBytes, out len))
            {
                text          = string.Empty;
                bytesConsumed = 0;
                return(false);
            }

            text = new string(' ', len);

            unsafe
            {
                fixed(char *p = text)
                {
                    var charSpan = new Span <char>(p, len);

                    int written;

                    return(Utf8Encoder.TryDecode(encodedBytes, charSpan, out bytesConsumed, out written));
                }
            }
        }