Ejemplo n.º 1
0
        public void CodePagesBitsRequited()
        {
            var bits      = BitBuffer.BitsRequired("0".AsSpan(), 1);
            var bitBuffer = new BitBufferWriter <SevenBitEncoding <u32ArrayMemory> >();

            bitBuffer.c("0".AsSpan());
            Assert.AreEqual(bits, bitBuffer.BitsWritten);
            var result = bitBuffer.ToArray();
        }
        public void u32(u32 value, u32 min, u32 max)
        {
#if !NO_EXCEPTIONS
            if (min >= max)
            {
                Throw.Argument("min should be lower than max");
            }
            if (value < min || value > max)
            {
                Throw.ArgumentOutOfRange(nameof(value), $"Value should be withing provided {min} and {max} range");
            }
#endif
            var bits = BitBuffer.BitsRequired(min, max);
            u32(value - min, bits);
        }
        public u32 u32(u32 min, u32 max)
        {
#if !NO_EXCEPTIONS
            if (min > max)
            {
                Throw.Argument("min should not be not lower than max");
            }
#endif
            var bits = (u8)BitBuffer.BitsRequired(min, max);
#if !NO_EXCEPTIONS
            if (BitsRead + bits > totalNumberBits)
            {
                Throw.ArgumentOutOfRange("Reading too many bits for requested range");
            }
#endif
            return(u32(bits) + min);
        }
        public void c(ReadOnlySpan <char> value)
        {
            if (value.Length > config.CharSpanLengthMax)
            {
                Throw.ArgumentOutOfRange($"String too long, raise the {nameof(config.CharSpanBitsLength)} value or split the string.");
            }

            var length = value.Length;

            if (length * 17 + 10 > (totalNumberBits - BitsWritten)) // possible overflow
            {
                if (BitBuffer.BitsRequired(value, length) > (totalNumberBits - BitsWritten))
                {
                    Throw.ArgumentOutOfRange("String would not fit in bitstream.");
                }
            }

            var codePage = CodePage.Ascii;

            for (var i = 0; i < length; i++)
            {
                var val = value[i];
                if (val > 127)
                {
                    codePage = CodePage.Latin1;
                    if (val > 255)
                    {
                        codePage = CodePage.LatinExtended;
                        if (val > 511)
                        {
                            codePage = CodePage.UTF16;
                            break;
                        }
                    }
                }
            }

            u32((u32)codePage, BitBuffer.codePageBitsRequired);
            u32((u32)length, config.CharSpanBitsLength);

            switch (codePage)
            {
            case CodePage.Ascii:
                for (var i = 0; i < length; i++)
                {
                    u32(value[i], BitBuffer.bitsASCII);
                }
                break;

            case CodePage.Latin1:
                for (var i = 0; i < length; i++)
                {
                    u32(value[i], BitBuffer.bitsLATIN1);
                }
                break;

            case CodePage.LatinExtended:
                for (var i = 0; i < length; i++)
                {
                    u32(value[i], BitBuffer.bitsLATINEXT);
                }
                break;

            default:
                for (var i = 0; i < length; i++)
                {
                    if (value[i] > 127)
                    {
                        u32(1, 1);
                        u32(value[i], BitBuffer.bitsUTF16);
                    }
                    else
                    {
                        u32(0, 1);
                        u32(value[i], BitBuffer.bitsASCII);
                    }
                }
                break;
            }
        }
Ejemplo n.º 5
0
 public void StringNullBitsRequited()
 {
     BitBuffer.BitsRequired(null, 42);
 }
Ejemplo n.º 6
0
 public void BitsRequired()
 {
     Assert.AreEqual(Unsafe.SizeOf <i32>() * 8, BitBuffer.BitsRequired(i32.MinValue, i32.MaxValue));
     Assert.AreEqual(Unsafe.SizeOf <i8>() * 8, BitBuffer.BitsRequired(i8.MinValue, i8.MaxValue));
     Assert.AreEqual(2, BitBuffer.BitsRequired(1, 3));
 }