Example #1
0
        public void Enum_HandleBitEncoder32Test()
        {
            var ss = new Memory.Strings.StringStorage(
                Memory.Strings.StringStorageWidthType.UTF32,
                Memory.Strings.StringStorageType.CharArray,
                Shell.EndianFormat.Big, 16);
            var encoder = new Bitwise.HandleBitEncoder();

            encoder.Encode32(ss.WidthType, TypeExtensions.BitEncoders.StringStorageWidthType);
            encoder.Encode32(ss.Type, TypeExtensions.BitEncoders.StringStorageType);
            encoder.Encode32(ss.ByteOrder, TypeExtensions.BitEncoders.EndianFormat);
            encoder.Encode32((uint)ss.FixedLength, 0x7FFF);

            var decoder = new Bitwise.HandleBitEncoder(encoder.GetHandle32());

            decoder.Decode32(out Memory.Strings.StringStorageWidthType widthType, TypeExtensions.BitEncoders.StringStorageWidthType);
            decoder.Decode32(out Memory.Strings.StringStorageType type, TypeExtensions.BitEncoders.StringStorageType);
            decoder.Decode32(out Shell.EndianFormat byteOrder, TypeExtensions.BitEncoders.EndianFormat);
            decoder.Decode32(out uint fixedLength, 0x7FFF);

            Assert.AreEqual(ss.WidthType, widthType);
            Assert.AreEqual(ss.Type, type);
            Assert.AreEqual(ss.ByteOrder, byteOrder);
            Assert.AreEqual(ss.FixedLength, (short)fixedLength);
        }
Example #2
0
        public void Enum_BitEncoderHashCode32Test()
        {
            var ss = new Memory.Strings.StringStorage(
                Memory.Strings.StringStorageWidthType.UTF32,
                Memory.Strings.StringStorageType.CharArray,
                Shell.EndianFormat.Big, 16);
            uint bits      = 0;
            int  bit_index = 0;

            TypeExtensions.BitEncoders.StringStorageWidthType.BitEncode(ss.WidthType, ref bits, ref bit_index);
            TypeExtensions.BitEncoders.StringStorageType.BitEncode(ss.Type, ref bits, ref bit_index);
            TypeExtensions.BitEncoders.EndianFormat.BitEncode(ss.ByteOrder, ref bits, ref bit_index);
            Bits.BitEncodeEnum((uint)ss.FixedLength, ref bits, ref bit_index, 0x7FFF);

            bits      = (uint)ss.GetHashCode();
            bit_index = 0;
            var widthType   = TypeExtensions.BitEncoders.StringStorageWidthType.BitDecode(bits, ref bit_index);
            var type        = TypeExtensions.BitEncoders.StringStorageType.BitDecode(bits, ref bit_index);
            var byteOrder   = TypeExtensions.BitEncoders.EndianFormat.BitDecode(bits, ref bit_index);
            var fixedLength = (short)Bits.BitDecode(bits, ref bit_index, 0x7FFF);

            Assert.AreEqual(ss.WidthType, widthType);
            Assert.AreEqual(ss.Type, type);
            Assert.AreEqual(ss.ByteOrder, byteOrder);
            Assert.AreEqual(ss.FixedLength, fixedLength);
        }
Example #3
0
        /// <summary>Writes a string based on a <see cref="Memory.Strings.StringStorage"/> definition</summary>
        /// <param name="value">String value to write. Null defaults to an empty string</param>
        /// <param name="storage">Definition for how we're streaming the string</param>
        public void Write(string value, Memory.Strings.StringStorage storage)
        {
            var sse = Text.StringStorageEncoding.TryAndGetStaticEncoding(storage);

            byte[] bytes = sse.GetBytes(value ?? string.Empty);
            base.Write(bytes);
        }
        /// <summary>Read a string using a <see cref="Memory.Strings.StringStorage"/> definition and a provided character length</summary>
        /// <param name="storage">Definition for the string's characteristics</param>
        /// <param name="length">Length, in characters, of the string.</param>
        /// <returns></returns>
        /// <remarks>
        /// Length can be non-positive if <paramref name="storage"/> defines or
        /// doesn't require an explicit character length. If you do provide the
        /// length, this operation will perform faster in some cases.
        /// </remarks>
        public string ReadString(Memory.Strings.StringStorage storage, int length)
        {
            ValidateStringStorageForStreaming(storage, length);

            var sse = Text.StringStorageEncoding.TryAndGetStaticEncoding(storage);

            return(sse.ReadString(this, length));
        }
 // Verify that we have enough information to correctly stream a string
 static void ValidateStringStorageForStreaming(Memory.Strings.StringStorage s, int length)
 {
     // There are going to be issues if we try to read back a willy nilly char array string
     if (s.Type == Memory.Strings.StringStorageType.CharArray && !s.IsFixedLength && length <= 0)
     {
         throw new InvalidDataException(string.Format(Util.InvariantCultureInfo,
                                                      "Provided string storage and length is invalid for Endian streaming: {0}, {1}",
                                                      s.ToString(), length.ToString(Util.InvariantCultureInfo)));
     }
 }
        public static void Assert(IO.EndianReader s, string expected, Memory.Strings.StringStorage storage)
        {
            Contract.Requires(s != null);
            Contract.Requires(!string.IsNullOrEmpty(expected));

            string signature = s.ReadString(storage, expected.Length);

            if (signature != expected)
            {
                throw new SignatureMismatchException(s.BaseStream,
                                                     expected, signature);
            }
        }
Example #7
0
        public EndianStream Stream(ref string value, Memory.Strings.StringStorage storage, int length)
        {
            if (IsReading)
            {
                value = Reader.ReadString(storage, length);
            }
            else if (IsWriting)
            {
                Writer.Write(value, storage);
            }

            return(this);
        }
Example #8
0
        public EndianStream StreamSignature(string signature, Memory.Strings.StringStorage storage)
        {
            Contract.Requires(!string.IsNullOrEmpty(signature));

            if (IsReading)
            {
                SignatureMismatchException.Assert(Reader, signature, storage);
            }
            else if (IsWriting)
            {
                Writer.Write(signature, storage);
            }

            return(this);
        }
 /// <summary>
 /// Read a string using a <see cref="Memory.Strings.StringStorage"/> definition.
 /// String length defaults to <see cref="Memory.Strings.StringStorage.FixedLegnth"/>
 /// </summary>
 /// <param name="storage">Definition for the string's characteristics</param>
 /// <returns></returns>
 public string ReadString(Memory.Strings.StringStorage storage)
 {
     return(ReadString(storage, storage.FixedLength));
 }