/// <inheritdoc/>
        public override int GetCharCount(ReadOnlySpan <byte> bytes)
        {
            var         encoder      = new Base32Encoder(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);
        }
        /// <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 Base32Encoder(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);
        }
        /// <inheritdoc/>
        public override unsafe string GetString(ReadOnlySpan <byte> bytes)
        {
            var           encoder      = new Base32Encoder(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());
        }
Exemple #4
0
 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));
 }