Ejemplo n.º 1
0
 /// <summary>
 /// Returns a <see cref="string"/> representation of this <see cref="Rune"/> instance.
 /// </summary>
 public override string ToString()
 {
     if (IsBmp)
     {
         return(string.CreateFromChar((char)_value));
     }
     else
     {
         UnicodeUtility.GetUtf16SurrogatesFromSupplementaryPlaneScalar(_value, out char high, out char low);
         return(string.CreateFromChar(high, low));
     }
 }
Ejemplo n.º 2
0
                public bool MoveNext()
                {
                    // We don't need to worry about tearing since this enumerator is a ref struct.

                    if (_currentCharPair > char.MaxValue)
                    {
                        // There was a surrogate pair smuggled in here from a previous operation.
                        // Shift out the high surrogate value and return immediately.

                        _currentCharPair >>= 16;
                        return(true);
                    }

                    if (_remainingUtf8Bytes.IsEmpty)
                    {
                        return(false);
                    }

                    // TODO_UTF8STRING: Since we assume Utf8String instances are well-formed, we may instead
                    // call an optimized version of the "decode" routine below which skips well-formedness checks.

                    OperationStatus status = Rune.DecodeFromUtf8(_remainingUtf8Bytes, out Rune currentRune, out int bytesConsumed);

                    Debug.Assert(status == OperationStatus.Done, "Somebody fed us invalid data?");

                    if (currentRune.IsBmp)
                    {
                        // Common case - BMP scalar value.

                        _currentCharPair = (uint)currentRune.Value;
                    }
                    else
                    {
                        // Uncommon case - supplementary plane (astral) scalar value.
                        // We'll smuggle the two UTF-16 code units into a single 32-bit value,
                        // with the leading surrogate packed into the low 16 bits of the value,
                        // and the trailing surrogate packed into the high 16 bits of the value.

                        UnicodeUtility.GetUtf16SurrogatesFromSupplementaryPlaneScalar((uint)currentRune.Value, out char leadingCodeUnit, out char trailingCodeUnit);
                        _currentCharPair = (uint)leadingCodeUnit + ((uint)trailingCodeUnit << 16);
                    }

                    // TODO_UTF8STRING: We can consider unsafe slicing below if we wish since we know we're
                    // not going to overrun the end of the span.

                    _remainingUtf8Bytes = _remainingUtf8Bytes.Slice(bytesConsumed);
                    return(true);
                }
Ejemplo n.º 3
0
        /// <summary>
        /// Encodes this <see cref="Rune"/> to a UTF-16 destination buffer.
        /// </summary>
        /// <param name="destination">The buffer to which to write this value as UTF-16.</param>
        /// <param name="charsWritten">
        /// The number of <see cref="char"/>s written to <paramref name="destination"/>,
        /// or 0 if the destination buffer is not large enough to contain the output.</param>
        /// <returns>True if the value was written to the buffer; otherwise, false.</returns>
        /// <remarks>
        /// The <see cref="Utf16SequenceLength"/> property can be queried ahead of time to determine
        /// the required size of the <paramref name="destination"/> buffer.
        /// </remarks>
        public bool TryEncode(Span <char> destination, out int charsWritten)
        {
            if (destination.Length >= 1)
            {
                if (IsBmp)
                {
                    destination[0] = (char)_value;
                    charsWritten   = 1;
                    return(true);
                }
                else if (destination.Length >= 2)
                {
                    UnicodeUtility.GetUtf16SurrogatesFromSupplementaryPlaneScalar(_value, out destination[0], out destination[1]);
                    charsWritten = 2;
                    return(true);
                }
            }

            // Destination buffer not large enough

            charsWritten = default;
            return(false);
        }