Example #1
0
        public unsafe void StringEquals(string text)
        {
            byte[] textArray = Encoding.UTF8.GetBytes(text);
            byte[] buffer    = new byte[textArray.Length];

            fixed(byte *p = textArray)
            fixed(byte *pBuffer = buffer)
            {
                Span <byte> byteSpan       = new Span <byte>(pBuffer, buffer.Length);
                Utf8String  strFromArray   = new Utf8String(textArray);
                Utf8String  strFromPointer = new Utf8String(new Span <byte>(p, textArray.Length));

                TestHelper.Validate(strFromArray, strFromPointer);

                Array.Clear(buffer, 0, buffer.Length);
                strFromArray.CopyTo(byteSpan);
                Assert.Equal(textArray, buffer);

                Array.Clear(buffer, 0, buffer.Length);
                strFromPointer.CopyTo(byteSpan);
                Assert.Equal(textArray, buffer);

                Array.Clear(buffer, 0, buffer.Length);
                strFromArray.CopyTo(buffer);
                Assert.Equal(textArray, buffer);

                Array.Clear(buffer, 0, buffer.Length);
                strFromPointer.CopyTo(buffer);
                Assert.Equal(textArray, buffer);
            }
        }
 private static ArraySegment<byte> StringToUtf8BufferWithEmptySpace(string testString, int emptySpaceSize = 2048)
 {
     var utf8Bytes = new Utf8String(testString).Bytes;
     var buffer = new byte[utf8Bytes.Length + emptySpaceSize];
     utf8Bytes.CopyTo(buffer);
     return new ArraySegment<byte>(buffer, 0, utf8Bytes.Length);
 }
Example #3
0
        public static void Main(string[] args)
        {
            var buffer = new byte[1024];
            var quote  = new Utf8String("Insanity: doing the same thing over and over again and expecting different results. - Albert Einstein");;

            var loop = new UVLoop();

            var listener = new TcpListener("0.0.0.0", 17, loop);

            listener.ConnectionAccepted += (Tcp connection) =>
            {
                connection.ReadCompleted += (data) =>
                {
                    quote.CopyTo(buffer);
                    connection.TryWrite(buffer, quote.Length);
                };

                connection.ReadStart();
            };

            listener.Listen();
            loop.Run();
        }
Example #4
0
 public void Write(Utf8String value)
 {
     this.RequireBuffer(value.Length);
     value.CopyTo(this._buffer.Slice(this._count));
     this._count += value.Length;
 }
Example #5
0
 private static ArraySegment<byte> StringToUtf8BufferWithEmptySpace(string testString, int emptySpaceSize = 2048)
 {
     var utf8Bytes = new Utf8String(testString).Bytes;
     var buffer = new byte[utf8Bytes.Length + emptySpaceSize];
     utf8Bytes.CopyTo(buffer);
     return new ArraySegment<byte>(buffer, 0, utf8Bytes.Length);
 }
Example #6
0
        public unsafe void StringEquals(string text)
        {
            byte[] textArray = Encoding.UTF8.GetBytes(text);
            byte[] buffer = new byte[textArray.Length];

            fixed (byte* p = textArray)
            fixed (byte* pBuffer = buffer)
            {
                Span<byte> byteSpan = new Span<byte>(pBuffer, buffer.Length);
                Utf8String strFromArray = new Utf8String(textArray);
                Utf8String strFromPointer = new Utf8String(new Span<byte>(p, textArray.Length));
                Assert.Equal(strFromArray, strFromPointer);

                Array.Clear(buffer, 0, buffer.Length);
                strFromArray.CopyTo(byteSpan);         
                Assert.Equal(textArray, buffer);

                Array.Clear(buffer, 0, buffer.Length);
                strFromPointer.CopyTo(byteSpan);
                Assert.Equal(textArray, buffer);

                Array.Clear(buffer, 0, buffer.Length);
                strFromArray.CopyTo(buffer);
                Assert.Equal(textArray, buffer);

                Array.Clear(buffer, 0, buffer.Length);
                strFromPointer.CopyTo(buffer);
                Assert.Equal(textArray, buffer);
            }
        }