/// <summary>
        /// Converts this <see cref="Utf8Span"/> to the desired Unicode normalization form, writing the
        /// UTF-8 result to the buffer <paramref name="destination"/>.
        /// </summary>
        /// <returns>
        /// The number of bytes written to <paramref name="destination"/>, or -1 if <paramref name="destination"/>
        /// is not large enough to hold the result of the normalization operation.
        /// </returns>
        /// <remarks>
        /// The original <see cref="Utf8Span"/> is left unchanged by this operation. Note that the the required
        /// length of <paramref name="destination"/> may be longer or shorter (in terms of UTF-8 byte count)
        /// than the input <see cref="Utf8Span"/>.
        /// </remarks>
        public int Normalize(Span <byte> destination, NormalizationForm normalizationForm = NormalizationForm.FormC)
        {
            // TODO_UTF8STRING: Reduce allocations in this code path.

            ReadOnlySpan <char> normalized = this.ToString().Normalize(normalizationForm).AsSpan();
            OperationStatus     status     = Utf8.FromUtf16(normalized, destination, out int _, out int bytesWritten, replaceInvalidSequences: false, isFinalBlock: true);

            Debug.Assert(status == OperationStatus.Done || status == OperationStatus.DestinationTooSmall, "Normalize shouldn't have produced malformed Unicode string.");

            if (status != OperationStatus.Done)
            {
                bytesWritten = -1; // "destination too small"
            }

            return(bytesWritten);
        }
        /// <summary>
        /// Converts this <see cref="Utf8Span"/> to uppercase using the invariant culture, writing the
        /// UTF-8 result to the buffer <paramref name="destination"/>.
        /// </summary>
        /// <returns>
        /// The number of bytes written to <paramref name="destination"/>, or -1 if <paramref name="destination"/>
        /// is not large enough to hold the result of the case conversion operation.
        /// </returns>
        /// <remarks>
        /// The original <see cref="Utf8Span"/> is left unchanged by this operation. For more information on the
        /// invariant culture, see the <see cref="CultureInfo.InvariantCulture"/> property. Note that the the required
        /// length of <paramref name="destination"/> may be longer or shorter (in terms of UTF-8 byte count)
        /// than the input <see cref="Utf8Span"/>.
        /// </remarks>
        public int ToUpperInvariant(Span <byte> destination)
        {
            // TODO_UTF8STRING: Avoid intermediate allocations.

            ReadOnlySpan <char> asUpperInvariant = this.ToString().ToUpperInvariant().AsSpan();
            OperationStatus     status           = Utf8.FromUtf16(asUpperInvariant, destination, out int _, out int bytesWritten, replaceInvalidSequences: false, isFinalBlock: true);

            Debug.Assert(status == OperationStatus.Done || status == OperationStatus.DestinationTooSmall, "ToUpperInvariant shouldn't have produced malformed Unicode string.");

            if (status != OperationStatus.Done)
            {
                bytesWritten = -1; // "destination too small"
            }

            return(bytesWritten);
        }
Example #3
0
        /// <summary>
        /// Converts this <see cref="Utf8Span"/> to uppercase using <paramref name="culture"/>, writing the
        /// UTF-8 result to the buffer <paramref name="destination"/>.
        /// </summary>
        /// <returns>
        /// The number of bytes written to <paramref name="destination"/>, or -1 if <paramref name="destination"/>
        /// is not large enough to hold the result of the case conversion operation.
        /// </returns>
        /// <remarks>
        /// The original <see cref="Utf8Span"/> is left unchanged by this operation. Note that the the required
        /// length of <paramref name="destination"/> may be longer or shorter (in terms of UTF-8 byte count)
        /// than the input <see cref="Utf8Span"/>.
        /// </remarks>
        public int ToUpper(Span <byte> destination, CultureInfo culture)
        {
            // TODO_UTF8STRING: Avoid intermediate allocations.

            if (culture is null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.culture);
            }

            ReadOnlySpan <char> asUpper = this.ToString().ToUpper(culture).AsSpan();
            OperationStatus     status  = Utf8.FromUtf16(asUpper, destination, out int _, out int bytesWritten, replaceInvalidSequences: false, isFinalBlock: true);

            Debug.Assert(status == OperationStatus.Done || status == OperationStatus.DestinationTooSmall, "ToUpper shouldn't have produced malformed Unicode string.");

            if (status != OperationStatus.Done)
            {
                bytesWritten = -1; // "destination too small"
            }

            return(bytesWritten);
        }