示例#1
0
        public unsafe void RawStringSlice()
        {
            const int memLen = 5;
            var       mem    = stackalloc byte[memLen]
            {
                (byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e',
            };
            var abcde = new RawString((IntPtr)mem, memLen);

            var abc = new RawString((IntPtr)mem, 3);
            var cd  = new RawString((IntPtr)(mem + 2), 2);
            var de  = new RawString((IntPtr)(mem + 3), 2);

            AssertEqual(abcde.Slice(0, abcde.Length), abcde);
            AssertEqual(abcde.Slice(0, 3), abc);
            AssertEqual(abcde.Slice(2, 2), cd);
            AssertEqual(abcde.Slice(3, 2), de);
            AssertEqual(abcde.Slice(3), de);
            AssertEqual(abcde.Slice(0), abcde);
            Assert.True(abcde.Slice(5).IsEmpty);
            Assert.True(abcde.Slice(5, 0).IsEmpty);

            AssertEqual(abcde.AsSpan(0, abcde.Length), abcde);
            AssertEqual(abcde.AsSpan(0, 3), abc);
            AssertEqual(abcde.AsSpan(2, 2), cd);
            AssertEqual(abcde.AsSpan(3, 2), de);
            AssertEqual(abcde.AsSpan(3), de);
            AssertEqual(abcde.AsSpan(0), abcde);
            Assert.True(abcde.AsSpan(5).IsEmpty);
            Assert.True(abcde.AsSpan(5, 0).IsEmpty);
        }
        public int LastIndexOf(UnicodeScalar unicodeScalar, int startIndex, int count)
        {
            Span <char> span = stackalloc char[unicodeScalar.Utf16CodeUnitCount];

            unicodeScalar.CopyUtf16CodeUnitsTo(span);
            return(RawString.AsSpan().Slice(startIndex, count).LastIndexOf(span));
        }
        public int IndexOf(UnicodeScalar unicodeScalar)
        {
            Span <char> span = stackalloc char[unicodeScalar.Utf16CodeUnitCount];

            unicodeScalar.CopyUtf16CodeUnitsTo(span);
            return(RawString.AsSpan().IndexOf(span, StringComparison.Ordinal));
        }
        public bool StartsWith(UnicodeScalar unicodeScalar)
        {
            Span <char> span = stackalloc char[unicodeScalar.Utf16CodeUnitCount];

            unicodeScalar.CopyUtf16CodeUnitsTo(span);
            return(RawString.AsSpan().StartsWith(span, StringComparison.Ordinal));
        }
        public Utf16SharedString PadRight(int totalWidth, UnicodeScalar paddingScalar)
        {
            if (totalWidth <= Length)
            {
                return(this);
            }

            int paddingScalarWidth = paddingScalar.Utf16CodeUnitCount;
            int insertScalarCount  = (totalWidth - Length) / paddingScalarWidth;
            int paddedLength       = Length + insertScalarCount * paddingScalarWidth;

            Span <char> paddingSpan = stackalloc char[paddingScalarWidth];

            paddingScalar.CopyUtf16CodeUnitsTo(paddingSpan);

            Span <char> span = paddedLength <= maxStackallocSize ? stackalloc char[paddedLength] : new char[paddedLength];

            RawString.AsSpan().CopyTo(span);
            for (int i = Length; i < paddedLength; i += paddingScalarWidth)
            {
                for (int j = 0; j < paddingScalarWidth; j++)
                {
                    span[i + j] = paddingSpan[j];
                }
            }

            return(new Utf16SharedString(new string(span.ToArray()), Spans));
        }
示例#6
0
 public static bool Contains(this RawString rawString, byte c)
 {
     foreach (var x in rawString.AsSpan())
     {
         if (x == c)
         {
             return(true);
         }
     }
     return(false);
 }
示例#7
0
 private static void AssertEqual(ReadOnlySpan <byte> str1, RawString str2)
 {
     Assert.True(str1.SequenceEqual(str2.AsSpan()));
 }
示例#8
0
 private static void AssertEqual(RawString str1, RawString str2)
 {
     Assert.True(str1.SequenceEqual(str2));
     Assert.True(str1.AsSpan().SequenceEqual(str2.AsSpan()));
 }
 public Utf16SharedString Trim()
 {
     return(trimResult(RawString.AsSpan().Trim()));
 }
 public Utf16SharedString TrimEnd(ReadOnlySpan <UnicodeScalar> trimScalars)
 {
     return(trimResult(RawString.AsSpan().TrimEnd(trimScalars)));
 }
 public Utf16SharedString TrimEnd(UnicodeScalar trimScalar)
 {
     return(trimResult(RawString.AsSpan().TrimEnd(trimScalar)));
 }