Пример #1
0
        /// <inheritdoc/>
        public override int GetCharCount(ReadOnlySpan <byte> bytes)
        {
            var         encoder      = new Base85Encoder(Settings);
            Span <char> outputBuffer = stackalloc char[1024];

            int           inputOffset  = 0;
            int           inputCount   = bytes.Length;
            int           outputLength = 0;
            ConvertStatus convertStatus;

            do
            {
                convertStatus = encoder.ConvertData(bytes, inputOffset, inputCount,
                                                    outputBuffer, 0, outputBuffer.Length,
                                                    true,
                                                    out var inputUsed,
                                                    out var outputUsed);

                outputLength += outputUsed;
                inputOffset  += inputUsed;
                inputCount   -= inputUsed;
            } while (!convertStatus);

            return(outputLength);
        }
Пример #2
0
        /// <inheritdoc/>
        public override char[] GetChars(ReadOnlySpan <byte> bytes)
        {
            // GetChars process the data in two passes, in order to avoid allocating
            // temporary storage for the result (which would have to be resized)

            int charsRequired = GetCharCount(bytes);

            if (charsRequired == 0)
            {
                return(Array.Empty <char>());
            }

            var outputChars = new char[charsRequired];

            var encoder       = new Base85Encoder(Settings);
            var convertStatus = encoder.ConvertData(bytes, 0, bytes.Length,
                                                    outputChars.AsSpan(), 0, outputChars.Length,
                                                    true,
                                                    out var inputUsed,
                                                    out var outputUsed);

            System.Diagnostics.Debug.Assert(convertStatus);
            System.Diagnostics.Debug.Assert(inputUsed == bytes.Length);
            System.Diagnostics.Debug.Assert(outputUsed == outputChars.Length);

            return(outputChars);
        }
Пример #3
0
        /// <inheritdoc/>
        public override unsafe string GetString(ReadOnlySpan <byte> bytes)
        {
            var           encoder      = new Base85Encoder(Settings);
            Span <char>   outputBuffer = stackalloc char[1024];
            StringBuilder sb           = new StringBuilder();

            int           inputOffset = 0;
            int           inputCount  = bytes.Length;
            ConvertStatus convertStatus;

            do
            {
                convertStatus = encoder.ConvertData(bytes, inputOffset, inputCount,
                                                    outputBuffer, 0, outputBuffer.Length,
                                                    true,
                                                    out var inputUsed,
                                                    out var outputUsed);

                if (outputUsed > 0)
                {
                    fixed(char *charPtr = &outputBuffer[0])
                    sb.Append(charPtr, outputUsed);
                }

                inputOffset += inputUsed;
                inputCount  -= inputUsed;
            } while (!convertStatus);

            return(sb.ToString());
        }
 protected override ConvertStatus ConvertData(ReadOnlySpan <byte> inputData, int inputIndex, int inputCount, Span <char> outputData, int outputIndex, int outputCount, bool flush, out int inputUsed, out int outputUsed)
 {
     return(_encoder.ConvertData(inputData, inputIndex, inputCount,
                                 outputData, outputIndex, outputCount,
                                 flush,
                                 out inputUsed,
                                 out outputUsed));
 }