public void ConstructorAlphabetTest()
        {
            char[]        testAlphabet = new char[] { 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', 'A', 'S', 'D', 'F', 'G', 'H' };
            Base16Encoder encoder      = new Base16Encoder(testAlphabet);

            Common.AreEqual(testAlphabet, encoder.Alphabet);
        }
Esempio n. 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 Base16Encoder(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);
        }
Esempio n. 3
0
        /// <inheritdoc/>
        public override int GetCharCount(ReadOnlySpan <byte> bytes)
        {
            var         encoder      = new Base16Encoder(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);
        }
Esempio n. 4
0
        /// <inheritdoc/>
        public override unsafe string GetString(ReadOnlySpan <byte> bytes)
        {
            var           encoder      = new Base16Encoder(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());
        }
        public void GetOutputEncodingTest()
        {
            Base16Encoder encoder = new Base16Encoder();

            System.Text.Encoding encoding = encoder.GetOutputEncoding();
            Assert.IsNotNull(encoding);
            Assert.AreEqual(System.Text.Encoding.ASCII, encoding);
        }
        public void GetDecoderTest()
        {
            Base16Encoder encoder = new Base16Encoder();
            Decoder       decoder = encoder.GetDecoder();

            Assert.IsNotNull(decoder);
            Assert.IsInstanceOf(typeof(Base16Decoder), decoder);
        }
Esempio n. 7
0
            public Encoder(Base16Settings settings)
            {
                if (settings == null)
                {
                    throw new ArgumentNullException(nameof(settings));
                }

                _encoder = new Base16Encoder(settings);
            }
        public void ConstructorDefaultTest()
        {
            Base16Encoder encoder = new Base16Encoder();

            Common.AreEqual(Base16Encoder.StandardAlphabet, encoder.Alphabet);
        }
        public void AlphabetShortTest()
        {
            Base16Encoder encoder = new Base16Encoder();

            encoder.Alphabet = new char[1];
        }
        public void AlphabetNullTest()
        {
            Base16Encoder encoder = new Base16Encoder();

            encoder.Alphabet = null;
        }