internal static Utf8String CreateFromRune(Rune value)
        {
            Utf8String newString    = FastAllocate(value.Utf8SequenceLength);
            int        bytesWritten = value.EncodeToUtf8(new Span <byte>(ref newString.DangerousGetMutableReference(), newString.Length));

            Debug.Assert(bytesWritten == value.Utf8SequenceLength);

            return(newString);
        }
        // Ordinal search
        public bool StartsWith(Rune value)
        {
            // TODO_UTF8STRING: This should be split into two methods:
            // One which operates on a single-byte (ASCII) search value,
            // the other which operates on a multi-byte (non-ASCII) search value.

            Span <byte> runeBytes        = stackalloc byte[Utf8Utility.MaxBytesPerScalar];
            int         runeBytesWritten = value.EncodeToUtf8(runeBytes);

            return(this.AsBytes().StartsWith(runeBytes.Slice(0, runeBytesWritten)));
        }
        // Ordinal search
        public int IndexOf(Rune value)
        {
            // TODO_UTF8STRING: This should be split into two methods:
            // One which operates on a single-byte (ASCII) search value,
            // the other which operates on a multi-byte (non-ASCII) search value.

            Span <byte> runeBytes        = stackalloc byte[Utf8Utility.MaxBytesPerScalar];
            int         runeBytesWritten = value.EncodeToUtf8(runeBytes);

            return(SpanHelpers.IndexOf(
                       ref DangerousGetMutableReference(), Length,
                       ref MemoryMarshal.GetReference(runeBytes), runeBytesWritten));
        }
Example #4
0
        /// <summary>
        /// Returns a value stating whether the current <see cref="Utf8String"/> instance contains
        /// the specified <see cref="Rune"/>. An ordinal comparison is used.
        /// </summary>
        public bool Contains(Rune value)
        {
            // TODO_UTF8STRING: This should be split into two methods:
            // One which operates on a single-byte (ASCII) search value,
            // the other which operates on a multi-byte (non-ASCII) search value.

            Span <byte> runeBytes        = stackalloc byte[Utf8Utility.MaxBytesPerScalar];
            int         runeBytesWritten = value.EncodeToUtf8(runeBytes);

#if SYSTEM_PRIVATE_CORELIB
            return(SpanHelpers.IndexOf(
                       ref DangerousGetMutableReference(), Length,
                       ref MemoryMarshal.GetReference(runeBytes), runeBytesWritten) >= 0);
#else
            return(GetSpan()
                   .IndexOf(runeBytes.Slice(0, runeBytesWritten)) >= 0);
#endif
        }
Example #5
0
        internal static Utf8String CreateFromRune(Rune value)
        {
            // Can skip zero-init since we're going to populate the entire buffer.

            Utf8String newString = FastAllocateSkipZeroInit(value.Utf8SequenceLength);

            if (value.IsAscii)
            {
                // Fast path: If an ASCII value, just allocate the one-byte string and fill in the single byte contents.

                newString.DangerousGetMutableReference() = (byte)value.Value;
                return(newString);
            }
            else
            {
                // Slow path: If not ASCII, allocate a string of the appropriate length and fill in the multi-byte contents.

                int bytesWritten = value.EncodeToUtf8(newString.DangerousGetMutableSpan());
                Debug.Assert(newString.Length == bytesWritten);
                return(newString);
            }
        }