예제 #1
0
        public void AsBytes_FromUtf8String()
        {
            Assert.True(default(ReadOnlySpan <byte>) == ((Utf8String)null).AsBytes());

            Utf8String theString = u8("Hello");

            Assert.True(MemoryMarshal.CreateReadOnlySpan(ref Unsafe.AsRef(in theString.GetPinnableReference()), 5) == theString.AsBytes());
        }
예제 #2
0
        public void AsBytes_FromSpan_Default_netcoreapp()
        {
            // a span wrapping data should become a span wrapping that same data.

            Utf8String theString = u8("Hello");

            Assert.True(MemoryMarshal.CreateReadOnlySpan(ref Unsafe.AsRef(in theString.GetPinnableReference()), 5) == (theString.AsMemory().Span).AsBytes());
        }
예제 #3
0
        public void AsSpan_FromUtf8String()
        {
            Assert.True(((Utf8String)null).AsSpan().Bytes == default); // referential equality check

            Utf8String theString = u8("Hello");

            Assert.True(Unsafe.AreSame(ref Unsafe.AsRef(in theString.GetPinnableReference()), ref Unsafe.AsRef(in theString.AsSpan().GetPinnableReference())));
            Assert.Equal(5, theString.AsSpan().Bytes.Length);
        }
예제 #4
0
        private static void AssertSameAsEmpty(Utf8String value)
        {
#if NETFRAMEWORK
            // When OOB, we can't change the actual object returned from a constructor.
            // So just assert the underlying "_bytes" is the same.
            Assert.Equal(0, value.Length);
            Assert.True(Unsafe.AreSame(
                            ref Unsafe.AsRef(in Utf8String.Empty.GetPinnableReference()),
                            ref Unsafe.AsRef(in value.GetPinnableReference())));
#else
            Assert.Same(Utf8String.Empty, value);
#endif
        }
예제 #5
0
        public unsafe void AsBytes_FromSpan_Default()
        {
            // First, a default span should become a default span.

            Assert.True(default(ReadOnlySpan <byte>) == new ReadOnlySpan <Char8>().AsBytes());

            // Next, an empty but non-default span should become an empty but non-default span.

            Assert.True(new ReadOnlySpan <byte>((void *)0x12345, 0) == new ReadOnlySpan <Char8>((void *)0x12345, 0).AsBytes());

            // Finally, a span wrapping data should become a span wrapping that same data.

            Utf8String theString = u8("Hello");

            Assert.True(MemoryMarshal.CreateReadOnlySpan(ref Unsafe.AsRef(in theString.GetPinnableReference()), 5) == ((ReadOnlySpan <Char8>)theString).AsBytes());
        }
예제 #6
0
        public static unsafe void ReadOnlyMemoryOfByte_WithUtf8String_Pin()
        {
            Utf8String            theString = u8("Hello");
            ReadOnlyMemory <byte> rom       = theString.AsMemoryBytes();
            MemoryHandle          memHandle = default;

            try
            {
                memHandle = rom.Pin();
                Assert.True(memHandle.Pointer == Unsafe.AsPointer(ref Unsafe.AsRef(in theString.GetPinnableReference())));
            }
            finally
            {
                memHandle.Dispose();
            }
        }
예제 #7
0
        private static IEnumerable <Utf8String> SplitIntoWords(Utf8String text)
        {
            int startIndex = 0;

            while (true)
            {
                var span         = MemoryMarshal.CreateReadOnlySpan(ref Unsafe.Add(ref Unsafe.AsRef <byte>(in text.GetPinnableReference()), startIndex), text.Length - startIndex);
                int indexOfSpace = span.IndexOf((byte)' ');
                if (indexOfSpace < 0)
                {
                    yield return(text.Substring(startIndex).Trim()); // return last of the text

                    yield break;
                }

                yield return(text.Substring(startIndex, indexOfSpace).Trim());

                startIndex += indexOfSpace + 1; // we know we can skip over a space character
            }
        }
예제 #8
0
        public void AsMemory_FromUtf8String()
        {
            Assert.True(default(ReadOnlyMemory <Char8>).Equals(((Utf8String)null).AsMemory()));

            Utf8String theString = u8("Hello");

            Assert.True(MemoryMarshal.CreateReadOnlySpan(ref Unsafe.As <byte, Char8>(ref Unsafe.AsRef(in theString.GetPinnableReference())), 5) == theString.AsMemory().Span);
        }
예제 #9
0
        public void AsBytes_FromUtf8String_netcoreapp()
        {
            Utf8String theString = u8("Hello");

            Assert.True(MemoryMarshal.CreateReadOnlySpan(ref Unsafe.AsRef(in theString.GetPinnableReference()), 5) == theString.AsBytes());
        }
예제 #10
0
        public unsafe static void MemoryOfChar8_WithUtf8String_Pin()
        {
            Utf8String             theString = u8("Hello");
            ReadOnlyMemory <Char8> rom       = theString.AsMemory();
            MemoryHandle           memHandle = default;

            try
            {
                memHandle = Unsafe.As <ReadOnlyMemory <Char8>, Memory <Char8> >(ref rom).Pin();
                Assert.True(memHandle.Pointer == Unsafe.AsPointer(ref Unsafe.AsRef(in theString.GetPinnableReference())));
            }
            finally
            {
                memHandle.Dispose();
            }
        }