public static void TryFind_Char_Ordinal(ustring source, char searchTerm, Range?expectedForwardMatch, Range?expectedBackwardMatch)
        {
            using BoundedUtf8Span boundedSpan = new BoundedUtf8Span(source.AsBytes());
            Utf8Span searchSpan = boundedSpan.Span;

            source = null; // to avoid accidentally using this for the remainder of the test

            // First, search forward

            bool wasFound = searchSpan.TryFind(searchTerm, out Range actualForwardMatch);

            Assert.Equal(expectedForwardMatch.HasValue, wasFound);

            if (wasFound)
            {
                AssertRangesEqual(searchSpan.Length, expectedForwardMatch.Value, actualForwardMatch);
            }

            // Also check Contains / StartsWith / SplitOn

            Assert.Equal(wasFound, searchSpan.Contains(searchTerm));
            Assert.Equal(wasFound && searchSpan.Bytes[..actualForwardMatch.Start].IsEmpty, searchSpan.StartsWith(searchTerm));

            (var before, var after) = searchSpan.SplitOn(searchTerm);
            if (wasFound)
            {
                Assert.True(searchSpan.Bytes[..actualForwardMatch.Start] == before.Bytes); // check for referential equality
Beispiel #2
0
        public static void BytesProperty_FromUtf8String()
        {
            ustring  ustr  = u8("Hello!");
            Utf8Span uspan = new Utf8Span(ustr);

            Assert.True(ustr.AsBytes() == uspan.Bytes);
        }
Beispiel #3
0
        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.GetByteLength() - 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.GetByteLength() /* just right */, utf8Normalized.GetByteLength() + 1 /* with extra room */ })
            {
                byte[] dest = new byte[bufferLength];
                Assert.Equal(utf8Normalized.GetByteLength(), utf8Source.Normalize(dest, normalizationForm));
                Utf8Span normalizedSpan = Utf8Span.UnsafeCreateWithoutValidation(dest[..utf8Normalized.GetByteLength()]);
        private static bool TryParseSearchTermAsUtf8String(object searchTerm, out ustring parsed)
        {
            if (searchTerm is char ch)
            {
                if (Rune.TryCreate(ch, out Rune rune))
                {
                    parsed = rune.ToUtf8String();
                    return(true);
                }
            }
            else if (searchTerm is Rune r)
            {
                parsed = r.ToUtf8String();
                return(true);
            }
            else if (searchTerm is string str)
            {
                if (ustring.TryCreateFrom(str, out parsed))
                {
                    return(true);
                }
            }
            else if (searchTerm is ustring ustr)
            {
                parsed = ustr;
                return(true);
            }

            parsed = default; // failed to turn the search term into a ustring
            return(false);
        }
        public static void TryFind_Char_Ordinal(ustring source, char searchTerm, Range?expectedForwardMatch, Range?expectedBackwardMatch)
        {
            if (source is null)
            {
                return; // don't null ref
            }

            // First, search forward

            bool wasFound = source.TryFind(searchTerm, out Range actualForwardMatch);

            Assert.Equal(expectedForwardMatch.HasValue, wasFound);

            if (wasFound)
            {
                AssertRangesEqual(source.Length, expectedForwardMatch.Value, actualForwardMatch);
            }

            // Also check Contains / StartsWith / SplitOn

            Assert.Equal(wasFound, source.Contains(searchTerm));
            Assert.Equal(wasFound && source[..actualForwardMatch.Start].Length == 0, source.StartsWith(searchTerm));

            (var before, var after) = source.SplitOn(searchTerm);
            if (wasFound)
            {
                Assert.Equal(source[..actualForwardMatch.Start], before);
Beispiel #6
0
        private static bool TryParseSearchTermAsUtf8String(object searchTerm, out ustring parsed)
        {
            if (searchTerm is char ch)
            {
                if (Rune.TryCreate(ch, out Rune rune))
                {
                    parsed = rune.ToUtf8String();
                    return(true);
                }
            }
            else if (searchTerm is Rune r)
            {
                parsed = r.ToUtf8String();
                return(true);
            }
            else if (searchTerm is string str)
            {
                ustring asUtf8 = new ustring(str);
                if (asUtf8.ToString() == str) // make sure this round-trips properly
                {
                    parsed = asUtf8;
                    return(true);
                }
            }
            else if (searchTerm is ustring ustr)
            {
                parsed = ustr;
                return(true);
            }

            parsed = default; // failed to turn the search term into a ustring
            return(false);
        }
            static void RunTest(string testData, string expected, CultureInfo culture)
            {
                using BoundedUtf8Span boundedSpan = new BoundedUtf8Span(testData);
                Utf8Span inputSpan = boundedSpan.Span;

                // First try the allocating APIs

                ustring expectedUtf8 = u8(expected) ?? ustring.Empty;
                ustring actualUtf8;

                if (culture is null)
                {
                    actualUtf8 = inputSpan.ToLowerInvariant();
                }
                else
                {
                    actualUtf8 = inputSpan.ToLower(culture);
                }

                Assert.Equal(expectedUtf8, actualUtf8);

                // Next, try the non-allocating APIs with too small a buffer

                if (expectedUtf8.Length > 0)
                {
                    byte[] bufferTooSmall = new byte[expectedUtf8.Length - 1];

                    if (culture is null)
                    {
                        Assert.Equal(-1, inputSpan.ToLowerInvariant(bufferTooSmall));
                    }
                    else
                    {
                        Assert.Equal(-1, inputSpan.ToLower(bufferTooSmall, culture));
                    }
                }

                // Then the non-allocating APIs with a properly sized buffer

                foreach (int bufferSize in new[] { expectedUtf8.Length, expectedUtf8.Length + 1 })
                {
                    byte[] buffer = new byte[expectedUtf8.Length];

                    if (culture is null)
                    {
                        Assert.Equal(expectedUtf8.Length, inputSpan.ToLowerInvariant(buffer));
                    }
                    else
                    {
                        Assert.Equal(expectedUtf8.Length, inputSpan.ToLower(buffer, culture));
                    }

                    Assert.True(expectedUtf8.AsBytes().SequenceEqual(buffer));
                }
            }
        public static void Ctor_NonEmptyUtf8String()
        {
            // Arrange

            ustring str = u8("Hello!");

            // Act

            Utf8Span span = new Utf8Span(str);

            // Assert

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

            ustring str = null;

            // Act

            Utf8Span span = new Utf8Span(str);

            // 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.Equal(IntPtr.Zero, (IntPtr)(void *)Unsafe.AsPointer(ref MemoryMarshal.GetReference(span.Bytes)));
            Assert.Equal(0, span.Length);
        }
 public bool TryFindLast(System.Utf8String value, out System.Range range)
 {
     throw null;
 }
 public bool StartsWith(System.Utf8String value, System.StringComparison comparison)
 {
     throw null;
 }
 public bool StartsWith(System.Utf8String value)
 {
     throw null;
 }
 public SplitOnResult SplitOnLast(System.Utf8String separator, System.StringComparison comparisonType)
 {
     throw null;
 }
Beispiel #14
0
 public static System.ReadOnlyMemory <byte> AsMemoryBytes(this System.Utf8String text, int start, int length)
 {
     throw null;
 }
 public Utf8StringContent(System.Utf8String content)
 {
 }
 public bool Contains(System.Utf8String value, System.StringComparison comparison)
 {
     throw null;
 }
 public bool Contains(System.Utf8String value)
 {
     throw null;
 }
Beispiel #18
0
 public static System.ReadOnlySpan <System.Char8> AsSpan(this System.Utf8String text, int start, int length)
 {
     throw null;
 }
Beispiel #19
0
 public static System.ReadOnlyMemory <byte> AsMemoryBytes(this System.Utf8String text, System.Range range)
 {
     throw null;
 }
 public bool TryFindLast(System.Utf8String value, System.StringComparison comparisonType, out System.Range range)
 {
     throw null;
 }
 public void Deconstruct(out System.Utf8String before, out System.Utf8String?after)
 {
     throw null;
 }
 public bool EndsWith(System.Utf8String value)
 {
     throw null;
 }
 public Utf8StringContent(System.Utf8String content, string?mediaType)
 {
 }
Beispiel #24
0
 public static System.ReadOnlyMemory <System.Char8> AsMemory(this System.Utf8String text, System.Range range)
 {
     throw null;
 }
Beispiel #25
0
 public static System.ReadOnlyMemory <System.Char8> AsMemory(this System.Utf8String text, System.Index startIndex)
 {
     throw null;
 }
Beispiel #26
0
 public static System.ReadOnlyMemory <byte> AsMemoryBytes(this System.Utf8String text, System.Index startIndex)
 {
     throw null;
 }
 public SplitResult Split(System.Utf8String separator, System.Utf8StringSplitOptions options = System.Utf8StringSplitOptions.None)
 {
     throw null;
 }
Beispiel #28
0
 public static System.ReadOnlyMemory <System.Char8> AsMemory(this System.Utf8String text, int start, int length)
 {
     throw null;
 }
 public SplitOnResult SplitOnLast(System.Utf8String separator)
 {
     throw null;
 }
Beispiel #30
0
 public static System.ReadOnlySpan <byte> AsBytes(this System.Utf8String text, int start)
 {
     throw null;
 }