public static void Normalize(string utf16Source, string utf16Expected, NormalizationForm normalizationForm)
        {
            using BoundedUtf8Span boundedSpan = new BoundedUtf8Span(utf16Source);
            Utf8Span utf8Source = boundedSpan.Span;

            // Quick IsNormalized tests

            Assert.Equal(utf16Source == utf16Expected, utf8Source.IsNormalized(normalizationForm));

            // Normalize and return new Utf8String instances

            ustring utf8Normalized = utf8Source.Normalize(normalizationForm);

            Assert.True(ustring.AreEquivalent(utf8Normalized, utf16Expected));

            // Normalize to byte arrays which are too small, expect -1 (failure)

            Assert.Equal(-1, utf8Source.Normalize(new byte[utf8Normalized.Length - 1], normalizationForm));

            // Normalize to byte arrays which are the correct length, expect success,
            // then compare buffer contents for ordinal equality.

            foreach (int bufferLength in new int[] { utf8Normalized.Length /* just right */, utf8Normalized.Length + 1 /* with extra room */ })
            {
                byte[] dest = new byte[bufferLength];
                Assert.Equal(utf8Normalized.Length, utf8Source.Normalize(dest, normalizationForm));
                Utf8Span normalizedSpan = Utf8Span.UnsafeCreateWithoutValidation(dest.AsSpan().Slice(0, utf8Normalized.Length));
                Assert.True(utf8Normalized.AsSpan() == normalizedSpan); // ordinal equality
                Assert.True(normalizedSpan.IsNormalized(normalizationForm));
            }
        }
        public static void Ctor_UnsafeFromByteSpan_NonEmpty()
        {
            // Arrange

            ReadOnlySpan <byte> original = new byte[] { 1, 2, 3, 4, 5 };

            // Act

            Utf8Span span = Utf8Span.UnsafeCreateWithoutValidation(original);

            // Assert

            Assert.False(span.IsEmpty);
            Assert.True(Unsafe.AreSame(ref Unsafe.AsRef(in original.GetPinnableReference()), ref Unsafe.AsRef(in span.GetPinnableReference())));
            Assert.True(Unsafe.AreSame(ref Unsafe.AsRef(in original.GetPinnableReference()), ref MemoryMarshal.GetReference(span.Bytes)));
            Assert.Equal(5, span.Length);
        }
        public static void Ctor_UnsafeFromByteSpan_NonNullEmptyArray()
        {
            // Arrange

            ReadOnlySpan <byte> original = new byte[0];

            // Act

            Utf8Span span = Utf8Span.UnsafeCreateWithoutValidation(original);

            // Assert
            // GetPinnableReference should be 'null' to match behavior of empty ROS<byte>.GetPinnableReference();

            Assert.True(span.IsEmpty);
            Assert.Equal(IntPtr.Zero, (IntPtr)(void *)Unsafe.AsPointer(ref Unsafe.AsRef(in span.GetPinnableReference())));
            Assert.True(Unsafe.AreSame(ref MemoryMarshal.GetReference(original), ref MemoryMarshal.GetReference(span.Bytes)));
            Assert.Equal(0, span.Length);
        }
        public static void Equals_Ordinal()
        {
            // First make sure referential equality passes

            Utf8Span span1 = u8("Hello!");
            Utf8Span span2 = span1;

            AssertEqualOrdinal(span1, span2);

            // Now make sure deep equality passes

            span2 = Utf8Span.UnsafeCreateWithoutValidation(Encoding.UTF8.GetBytes("Hello!"));
            AssertEqualOrdinal(span1, span2);

            // Now mutate one of the inputs and make sure they're inequal

            span2 = u8("Bello!");
            AssertNotEqualOrdinal(span1, span2);

            // Finally, make sure null / null and null / empty are treated as the same

            AssertEqualOrdinal(Utf8Span.Empty, Utf8Span.Empty);
            AssertEqualOrdinal(Utf8Span.Empty, u8(""));
        }
Beispiel #5
0
 public static void BytesProperty_FromCustomBytes()
 {
     byte[] bytes = Encoding.UTF8.GetBytes("Hello!");
     Assert.True(bytes.AsSpan() == Utf8Span.UnsafeCreateWithoutValidation(bytes).Bytes);
 }
Beispiel #6
0
 internal Utf8Span AsSpanSkipNullCheck()
 {
     return(Utf8Span.UnsafeCreateWithoutValidation(this.AsBytesSkipNullCheck()));
 }