/// <inheritdoc />
            internal override unsafe void ToVector4(Span <RgbaVector> sourceColors, Span <Vector4> destVectors, int count)
            {
                Guard.MustBeSizedAtLeast(sourceColors, count, nameof(sourceColors));
                Guard.MustBeSizedAtLeast(destVectors, count, nameof(destVectors));

                SpanHelper.Copy(sourceColors.AsBytes(), destVectors.AsBytes(), count * sizeof(Vector4));
            }
Esempio n. 2
0
 public void Write(char character)
 {
     if (character <= 127)
     {
         var free = Free;
         if (free.Length < 1)
         {
             Resize();
             free = Free;
         }
         free[0] = (byte)character;
         _written++;
     }
     else
     {
         // TODO: this needs to be optimized.
         Span <byte> utf8Span  = stackalloc byte[4];
         Span <char> utf16Span = stackalloc char[1];
         utf16Span[0] = character;
         if (Encodings.Utf8.FromUtf16(utf16Span.AsBytes(), utf8Span, out int consumed, out int written) == OperationStatus.Done)
         {
             var encoded = utf8Span.Slice(0, written);
             while (!encoded.TryCopyTo(Free))
             {
                 Resize();
             }
             _written += written;
         }
     }
 }
Esempio n. 3
0
        /// <summary>
        /// Attempts to parse cluster member identifier.
        /// </summary>
        /// <param name="identifier">The hexadecimal representation of identifier.</param>
        /// <param name="value">The parsed identifier.</param>
        /// <returns><see langword="true"/> if identifier parsed successfully; otherwise, <see langword="false"/>.</returns>
        public static bool TryParse(ReadOnlySpan <char> identifier, out ClusterMemberId value)
        {
            value = default;
            var bytes = Span.AsBytes(ref value);

            return(identifier.FromHex(bytes) == bytes.Length);
        }
Esempio n. 4
0
        public static void AsBytesUIntToByte()
        {
            uint[]      a       = { 0x44332211, 0x88776655 };
            Span <uint> span    = new Span <uint>(a);
            Span <byte> asBytes = span.AsBytes <uint>();

            Assert.True(Unsafe.AreSame <byte>(ref Unsafe.As <uint, byte>(ref span.DangerousGetPinnableReference()), ref asBytes.DangerousGetPinnableReference()));
            asBytes.Validate <byte>(0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88);
        }
Esempio n. 5
0
        public static void AsBytesUIntToByte()
        {
            uint[] a = { 0x44332211, 0x88776655 };
            Span<uint> span = new Span<uint>(a);
            Span<byte> asBytes = span.AsBytes<uint>();

            Assert.True(Unsafe.AreSame<byte>(ref Unsafe.As<uint, byte>(ref span.DangerousGetPinnableReference()), ref asBytes.DangerousGetPinnableReference()));
            asBytes.Validate<byte>(0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88);
        }
Esempio n. 6
0
        public static void AsBytesUIntToByte()
        {
            uint[]      a       = { 0x44332211, 0x88776655 };
            Span <uint> span    = new Span <uint>(a);
            Span <byte> asBytes = span.AsBytes <uint>();

            Assert.True(Unsafe.AreSame <byte>(ref Unsafe.As <uint, byte>(ref MemoryMarshal.GetReference(span)), ref MemoryMarshal.GetReference(asBytes)));
            asBytes.Validate <byte>(0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88);
        }
Esempio n. 7
0
            public void AsBytes_Read(int length, int start, int index, int byteOffset)
            {
                TestStructs.Foo[]      a    = TestStructs.Foo.CreateArray(length);
                Span <TestStructs.Foo> span = new Span <TestStructs.Foo>(a, start);

                Span <byte> bytes = span.AsBytes();

                byte actual = bytes[index * Unsafe.SizeOf <TestStructs.Foo>() + byteOffset];

                ref byte baseRef  = ref Unsafe.As <TestStructs.Foo, byte>(ref a[0]);
Esempio n. 8
0
        internal ClusterMemberId(IPEndPoint endpoint)
        {
            address = default;
            var bytes = Span.AsBytes(ref address);

            if (!endpoint.Address.TryWriteBytes(bytes, out length))
            {
                throw new ArgumentException(ExceptionMessages.UnsupportedAddressFamily, nameof(endpoint));
            }
            port   = endpoint.Port;
            family = (int)endpoint.AddressFamily;
        }
Esempio n. 9
0
        public decimal ReadDecimal()
        {
            Span <int> parts = stackalloc int[4];

            ReadBytes(parts.AsBytes());

            var sign = (parts[3] & 0x80000000) != 0;

            var scale    = (byte)((parts[3] >> 16) & 0x7F);
            var newValue = new decimal(parts[0], parts[1], parts[2], sign, scale);

            return(newValue);
        }
        protected override void OnDataMessage(NetIncomingMessage message, DataMessageType type)
        {
            switch (type)
            {
            case DataMessageType.ChunkData:
            {
                ChunkPosition position = message.ReadPoint32();
                int           length   = message.ReadUInt16();

                Span <byte> compressedChunkBytes = stackalloc byte[length];
                message.Read(compressedChunkBytes);

                Span <Tile> tiles = stackalloc Tile[Chunk.Size * Chunk.Size];
                LZ4Codec.Decode(compressedChunkBytes, tiles.AsBytes());

                OnChunkData(new ChunkData(position, tiles));
                break;
            }

            case DataMessageType.BuildOrders:
            {
                int updateCount = message.ReadByte() + 1;
                for (int i = 0; i < updateCount; i++)
                {
                    TilePosition position = message.ReadPoint32();
                    ushort       tileID   = message.ReadUInt16();

                    var tile = new Tile(tileID);
                    BuildOrders.Enqueue(new BuildOrder(position, tile));

                    //var chunkPos = ChunkPosition.FromTile(order.Position);
                    //var chunk = ClientGame._chunks[chunkPos.X, chunkPos.Y];
                    //if (chunk == null)
                    //{
                    //    // TODO: build order may arrive before chunk data so
                    //    // add orders to a set and discard them if they're not used in time
                    //
                    //    Log.Warning($"Tried to update unloaded chunk {chunkPos}.");
                    //    return;
                    //}
                    //
                    //if (!chunk.TrySetTile(order.Position, order.Tile))
                    //    Log.Warning($"Invalid chunk update at position {order.Position} to chunk {chunkPos}.");
                }
                break;
            }
            }
        }
Esempio n. 11
0
            public void IntToBytes(int count)
            {
                int destCount = count * sizeof(int);

                int[]  source = CreateTestInts(count + 2);
                byte[] dest   = new byte[destCount + sizeof(int) + 1];

                var apSource = new Span <int>(source);
                var apDest   = new Span <byte>(dest);

                apSource.AsBytes().Slice(0, count * sizeof(int)).CopyTo(apDest);

                AssertNotDefault(source, 1);

                Assert.True((bool)ElementsAreEqual(source, dest, 0));
                Assert.True((bool)ElementsAreEqual(source, dest, count - 1));
                Assert.False((bool)ElementsAreEqual(source, dest, count));
            }
Esempio n. 12
0
            public void BytesToGeneric(int count)
            {
                int srcCount = count * sizeof(TestStructs.Foo);

                byte[] source = CreateTestBytes(srcCount);
                var    dest   = new TestStructs.Foo[count + 2];

                var apSource = new Span <byte>(source);
                var apDest   = new Span <TestStructs.Foo>(dest);

                apSource.Slice(0, count * sizeof(TestStructs.Foo)).CopyTo(apDest.AsBytes());

                AssertNotDefault(source, sizeof(TestStructs.Foo) + 1);
                AssertNotDefault(dest, 1);

                Assert.True((bool)ElementsAreEqual(dest, source, 0));
                Assert.True((bool)ElementsAreEqual(dest, source, 1));
                Assert.True((bool)ElementsAreEqual(dest, source, count - 1));
                Assert.False((bool)ElementsAreEqual(dest, source, count));
            }
Esempio n. 13
0
            public void GenericToBytes_Aligned(int count)
            {
                int destCount = count * sizeof(TestStructs.Foo);

                TestStructs.AlignedFoo[] source = TestStructs.AlignedFoo.CreateArray(count + 2);
                byte[] dest = new byte[destCount + sizeof(TestStructs.AlignedFoo) * 2];

                var apSource = new Span <TestStructs.AlignedFoo>(source, 1, source.Length - 1);
                var apDest   = new Span <byte>(dest, sizeof(TestStructs.AlignedFoo), dest.Length - sizeof(TestStructs.AlignedFoo));

                apSource.AsBytes().Slice(0, (count - 1) * sizeof(TestStructs.AlignedFoo)).CopyTo(apDest);

                AssertNotDefault(source, 1);

                Assert.False((bool)ElementsAreEqual(source, dest, 0));
                Assert.True((bool)ElementsAreEqual(source, dest, 1));
                Assert.True((bool)ElementsAreEqual(source, dest, 2));
                Assert.True((bool)ElementsAreEqual(source, dest, count - 1));
                Assert.False((bool)ElementsAreEqual(source, dest, count));
            }
Esempio n. 14
0
        public void Writable()
        {
            Span <byte> buffer = stackalloc byte[256];
            var         writer = Text.BufferWriter.Create(buffer);

            var ulonger = new UInt128();

            ulonger.Lower = ulong.MaxValue;
            ulonger.Upper = 1;

            writer.WriteBytes(ulonger, s_base64);
            var result = Encodings.Utf8.ToString(writer.Written);

            Assert.Equal("//////////8BAAAAAAAAAA==", result);

            var ulongerSpan = new Span <UInt128>(new UInt128[1]);

            Assert.Equal(OperationStatus.Done, Base64.DecodeFromUtf8(writer.Written, ulongerSpan.AsBytes(), out int consumed, out int written));
            Assert.Equal(ulongerSpan[0].Lower, ulonger.Lower);
            Assert.Equal(ulongerSpan[0].Upper, ulonger.Upper);
        }
Esempio n. 15
0
        public override int GetHashCode()
        {
            if (_hashCode != 0)
            {
                return(_hashCode);
            }

            // For IPv6 addresses, we calculate the hashcode by using Marvin
            // on a stack-allocated array containing the Address bytes and ScopeId.
            int hashCode;

            if (IsIPv6)
            {
                const int   addressAndScopeIdLength = IPAddressParserStatics.IPv6AddressBytes + sizeof(uint);
                Span <byte> addressAndScopeIdSpan   = stackalloc byte[addressAndScopeIdLength];

                new ReadOnlySpan <ushort>(_numbers).AsBytes().CopyTo(addressAndScopeIdSpan);
                Span <byte> scopeIdSpan  = addressAndScopeIdSpan.Slice(IPAddressParserStatics.IPv6AddressBytes);
                bool        scopeWritten = BitConverter.TryWriteBytes(scopeIdSpan, _addressOrScopeId);
                Debug.Assert(scopeWritten);

                hashCode = Marvin.ComputeHash32(
                    addressAndScopeIdSpan,
                    Marvin.DefaultSeed);
            }
            else
            {
                Span <uint> addressOrScopeIdSpan = stackalloc uint[1];
                addressOrScopeIdSpan[0] = _addressOrScopeId;

                // For IPv4 addresses, we use Marvin on the integer representation of the Address.
                hashCode = Marvin.ComputeHash32(
                    addressOrScopeIdSpan.AsBytes(),
                    Marvin.DefaultSeed);
            }

            _hashCode = hashCode;
            return(_hashCode);
        }
Esempio n. 16
0
    static void MustNotCastSpanOfValueTypesWithPointers()
    {
        var spanOfValueTypeWithPointers = new Span <ValueTypeWithPointers>(new[] { new ValueTypeWithPointers(new object()) });

        try
        {
            var impossible = spanOfValueTypeWithPointers.AsBytes();
            AssertTrue(false, "Expected exception for wrong type not thrown");
        }
        catch (System.ArgumentException ex)
        {
            AssertTrue(ex.Message == "Cannot use type 'ValueTypeWithPointers'. Only value types without pointers or references are supported.",
                       "Exception message is incorrect");
        }

        try
        {
            var impossible = spanOfValueTypeWithPointers.NonPortableCast <ValueTypeWithPointers, byte>();
            AssertTrue(false, "Expected exception for wrong type not thrown");
        }
        catch (System.ArgumentException ex)
        {
            AssertTrue(ex.Message == "Cannot use type 'ValueTypeWithPointers'. Only value types without pointers or references are supported.",
                       "Exception message is incorrect");
        }

        var spanOfBytes = new Span <byte>(new byte[10]);

        try
        {
            var impossible = spanOfBytes.NonPortableCast <byte, ValueTypeWithPointers>();
            AssertTrue(false, "Expected exception for wrong type not thrown");
        }
        catch (System.ArgumentException ex)
        {
            AssertTrue(ex.Message == "Cannot use type 'ValueTypeWithPointers'. Only value types without pointers or references are supported.",
                       "Exception message is incorrect");
        }
    }
Esempio n. 17
0
        // TODO: this should be moved to System.Text.Primitives. Probably to Utf8 class
        static string Utf8ToString(ReadOnlySpan <byte> utf8)
        {
            var result = Utf8.ToUtf16Length(utf8, out int bytesNeeded);

            if (result == TransformationStatus.InvalidData || result == TransformationStatus.NeedMoreSourceData)
            {
                throw new Exception("invalid UTF8 byte");
            }
            var str = new string(' ', bytesNeeded / sizeof(char));

            unsafe
            {
                fixed(char *pStr = str)
                {
                    var strSpan = new Span <char>(pStr, str.Length);

                    if (Utf8.ToUtf16(utf8, strSpan.AsBytes(), out int consumed, out int written) != TransformationStatus.Done)
                    {
                        throw new Exception();
                    }
                }
            }
            return(str);
        }
Esempio n. 18
0
 void IBufferReader <T> .Append(ReadOnlySpan <byte> block, ref int consumedBytes)
 {
     block.CopyTo(Span.AsBytes(ref result).Slice(offset));
     offset += block.Length;
 }
Esempio n. 19
0
        public static void Save(this Stream s, Point[] points)
        {
            Span <Point> span = points;

            s.Write(span.AsBytes());
        }
Esempio n. 20
0
        public static void Load(this Stream s, Point[] points)
        {
            Span <Point> span = points;

            s.Read(span.AsBytes());
        }
Esempio n. 21
0
 public static void Lerp(ReadOnlySpan <Alpha8> left, ReadOnlySpan <Alpha8> right, int amount, Span <Alpha8> dst)
 {
     Vector4Streaming.Lerp(left.AsBytes(), right.AsBytes(), amount, dst.AsBytes());
 }