Пример #1
0
        public static void MutableStringBuffer(MemoryWriter <char> writer)
        {
            using (writer)
            {
                writer.Write("Hello, world");
                writer.Write('!');
                writer.WriteLine("!!");
                writer.WriteInt32(42, provider: InvariantCulture);
                writer.WriteUInt32(56U, provider: InvariantCulture);
                writer.WriteByte(10, provider: InvariantCulture);
                writer.WriteSByte(22, provider: InvariantCulture);
                writer.WriteInt16(88, provider: InvariantCulture);
                writer.WriteUInt16(99, provider: InvariantCulture);
                writer.WriteInt64(77, provider: InvariantCulture);
                writer.WriteUInt64(66, provider: InvariantCulture);

                var guid = Guid.NewGuid();
                writer.WriteGuid(guid);

                var dt = DateTime.Now;
                writer.WriteDateTime(dt, provider: InvariantCulture);

                var dto = DateTimeOffset.Now;
                writer.WriteDateTimeOffset(dto, provider: InvariantCulture);

                writer.WriteDecimal(42.5M, provider: InvariantCulture);
                writer.WriteSingle(32.2F, provider: InvariantCulture);
                writer.WriteDouble(56.6D, provider: InvariantCulture);

                Equal("Hello, world!!!" + Environment.NewLine + "4256102288997766" + guid + dt.ToString(InvariantCulture) + dto.ToString(InvariantCulture) + "42.532.256.6", writer.BuildString());
            }
        }
Пример #2
0
        private static void WriteReadUsingMemory(MemoryWriter <byte> writer)
        {
            True(writer.WrittenMemory.IsEmpty);
            Equal(0, writer.WrittenCount);

            var memory = writer.GetMemory(100);

            new byte[] { 10, 20, 30 }.AsMemory().CopyTo(memory);
            writer.Advance(3);

            var result = writer.WrittenMemory.Span;

            Equal(3, result.Length);
            Equal(3, writer.WrittenCount);
            Equal(10, result[0]);
            Equal(20, result[1]);
            Equal(30, result[2]);

            memory = writer.GetMemory(3);
            new byte[] { 40, 50, 60 }.AsMemory().CopyTo(memory);
            writer.Advance(3);

            result = writer.WrittenMemory.Span;
            Equal(6, writer.WrittenCount);
            Equal(6, result.Length);
            Equal(10, result[0]);
            Equal(20, result[1]);
            Equal(30, result[2]);
            Equal(40, result[3]);
            Equal(50, result[4]);
            Equal(60, result[5]);
        }
Пример #3
0
        public static void EncodeAsString(MemoryWriter <byte> writer, Encoding encoding)
        {
            var encodingContext = new EncodingContext(encoding, true);

            using (writer)
            {
                var g   = Guid.NewGuid();
                var dt  = DateTime.Now;
                var dto = DateTimeOffset.Now;
                writer.WriteInt64(42L, StringLengthEncoding.Plain, in encodingContext, provider: InvariantCulture);
                writer.WriteUInt64(12UL, StringLengthEncoding.PlainLittleEndian, in encodingContext, provider: InvariantCulture);
                writer.WriteInt32(34, StringLengthEncoding.PlainBigEndian, in encodingContext, provider: InvariantCulture);
                writer.WriteUInt32(78, StringLengthEncoding.Plain, in encodingContext, provider: InvariantCulture);
                writer.WriteInt16(90, StringLengthEncoding.Plain, in encodingContext, provider: InvariantCulture);
                writer.WriteUInt16(12, StringLengthEncoding.Plain, in encodingContext, format: "X", provider: InvariantCulture);
                writer.WriteUInt16(12, StringLengthEncoding.Plain, in encodingContext, provider: InvariantCulture);
                writer.WriteByte(10, StringLengthEncoding.Plain, in encodingContext, format: "X", provider: InvariantCulture);
                writer.WriteSByte(11, StringLengthEncoding.Plain, in encodingContext, format: "X", provider: InvariantCulture);
                writer.WriteByte(10, StringLengthEncoding.Plain, in encodingContext, provider: InvariantCulture);
                writer.WriteSByte(11, StringLengthEncoding.Plain, in encodingContext, provider: InvariantCulture);
                writer.WriteGuid(g, StringLengthEncoding.Plain, in encodingContext);
                writer.WriteGuid(g, StringLengthEncoding.Plain, in encodingContext, format: "X");
                writer.WriteDateTime(dt, StringLengthEncoding.Plain, in encodingContext, format: "O", provider: InvariantCulture);
                writer.WriteDateTimeOffset(dto, StringLengthEncoding.Plain, in encodingContext, format: "O", provider: InvariantCulture);
                writer.WriteDateTime(dt, StringLengthEncoding.Plain, in encodingContext, format: "O", provider: InvariantCulture);
                writer.WriteDateTimeOffset(dto, StringLengthEncoding.Plain, in encodingContext, format: "O", provider: InvariantCulture);
                writer.WriteDecimal(42.5M, StringLengthEncoding.Plain, in encodingContext, provider: InvariantCulture);
                writer.WriteSingle(32.2F, StringLengthEncoding.Plain, in encodingContext, provider: InvariantCulture);
                writer.WriteDouble(56.6D, StringLengthEncoding.Plain, in encodingContext, provider: InvariantCulture);

                var decodingContext = new DecodingContext(encoding, true);
                var reader          = IAsyncBinaryReader.Create(writer.WrittenMemory);
                Equal(42L, reader.ReadInt64(StringLengthEncoding.Plain, in decodingContext, provider: InvariantCulture));
                Equal(12UL, reader.ReadUInt64(StringLengthEncoding.PlainLittleEndian, in decodingContext, provider: InvariantCulture));
                Equal(34, reader.ReadInt32(StringLengthEncoding.PlainBigEndian, in decodingContext, provider: InvariantCulture));
                Equal(78U, reader.ReadUInt32(StringLengthEncoding.Plain, in decodingContext, provider: InvariantCulture));
                Equal(90, reader.ReadInt16(StringLengthEncoding.Plain, in decodingContext, provider: InvariantCulture));
                Equal("C", reader.ReadString(StringLengthEncoding.Plain, in decodingContext));
                Equal(12, reader.ReadUInt16(StringLengthEncoding.Plain, in decodingContext, provider: InvariantCulture));
                Equal("A", reader.ReadString(StringLengthEncoding.Plain, in decodingContext));
                Equal("B", reader.ReadString(StringLengthEncoding.Plain, in decodingContext));
                Equal(10, reader.ReadByte(StringLengthEncoding.Plain, in decodingContext, provider: InvariantCulture));
                Equal(11, reader.ReadSByte(StringLengthEncoding.Plain, in decodingContext, provider: InvariantCulture));
                Equal(g, reader.ReadGuid(StringLengthEncoding.Plain, in decodingContext));
                Equal(g, reader.ReadGuid(StringLengthEncoding.Plain, in decodingContext, "X"));
                Equal(dt, reader.ReadDateTime(StringLengthEncoding.Plain, in decodingContext, style: DateTimeStyles.RoundtripKind, provider: InvariantCulture));
                Equal(dto, reader.ReadDateTimeOffset(StringLengthEncoding.Plain, in decodingContext, style: DateTimeStyles.RoundtripKind, provider: InvariantCulture));
                Equal(dt, reader.ReadDateTime(StringLengthEncoding.Plain, in decodingContext, formats: new[] { "O" }, style: DateTimeStyles.RoundtripKind, provider: InvariantCulture));
                Equal(dto, reader.ReadDateTimeOffset(StringLengthEncoding.Plain, in decodingContext, formats: new[] { "O" }, style: DateTimeStyles.RoundtripKind, provider: InvariantCulture));
                Equal(42.5M, reader.ReadDecimal(StringLengthEncoding.Plain, in decodingContext, provider: InvariantCulture));
                Equal(32.2F, reader.ReadSingle(StringLengthEncoding.Plain, in decodingContext, provider: InvariantCulture));
                Equal(56.6D, reader.ReadDouble(StringLengthEncoding.Plain, in decodingContext, provider: InvariantCulture));
            }
        }