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()); }
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()); }
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); }
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 }
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()); }
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(); } }
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 } }
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); }
public void AsBytes_FromUtf8String_netcoreapp() { Utf8String theString = u8("Hello"); Assert.True(MemoryMarshal.CreateReadOnlySpan(ref Unsafe.AsRef(in theString.GetPinnableReference()), 5) == theString.AsBytes()); }
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(); } }