public static void GetUtf16SurrogatesFromSupplementaryPlaneScalar(uint value, out char highSurrogateCodePoint, out char lowSurrogateCodePoint) { UnicodeDebug.AssertIsValidSupplementaryPlaneScalar(value); // This calculation comes from the Unicode specification, Table 3-5. highSurrogateCodePoint = (char)((value + ((0xD800u - 0x40u) << 10)) >> 10); lowSurrogateCodePoint = (char)((value & 0x3FFu) + 0xDC00u); }
/// <summary> /// Given a Unicode scalar value, gets the number of UTF-16 code units required to represent this value. /// </summary> public static int GetUtf16SequenceLength(uint value) { UnicodeDebug.AssertIsValidScalar(value); value -= 0x10000; // if value < 0x10000, high byte = 0xFF; else high byte = 0x00 value += (2 << 24); // if value < 0x10000, high byte = 0x01; else high byte = 0x02 value >>= 24; // shift high byte down return((int)value); // and return it }
/// <summary> /// Returns a Unicode scalar value from two code points representing a UTF-16 surrogate pair. /// </summary> public static uint GetScalarFromUtf16SurrogatePair(uint highSurrogateCodePoint, uint lowSurrogateCodePoint) { UnicodeDebug.AssertIsHighSurrogateCodePoint(highSurrogateCodePoint); UnicodeDebug.AssertIsLowSurrogateCodePoint(lowSurrogateCodePoint); // This calculation comes from the Unicode specification, Table 3-5. // Need to remove the D800 marker from the high surrogate and the DC00 marker from the low surrogate, // then fix up the "wwww = uuuuu - 1" section of the bit distribution. The code is written as below // to become just two instructions: shl, lea. return((highSurrogateCodePoint << 10) + lowSurrogateCodePoint - ((0xD800U << 10) + 0xDC00U - (1 << 16))); }
/// <summary> /// Given a Unicode scalar value, gets the number of UTF-8 code units required to represent this value. /// </summary> public static int GetUtf8SequenceLength(uint value) { UnicodeDebug.AssertIsValidScalar(value); // The logic below can handle all valid scalar values branchlessly. // It gives generally good performance across all inputs, and on x86 // it's only six instructions: lea, sar, xor, add, shr, lea. // 'a' will be -1 if input is < 0x800; else 'a' will be 0 // => 'a' will be -1 if input is 1 or 2 UTF-8 code units; else 'a' will be 0 int a = ((int)value - 0x0800) >> 31; // The number of UTF-8 code units for a given scalar is as follows: // - U+0000..U+007F => 1 code unit // - U+0080..U+07FF => 2 code units // - U+0800..U+FFFF => 3 code units // - U+10000+ => 4 code units // // If we XOR the incoming scalar with 0xF800, the chart mutates: // - U+0000..U+F7FF => 3 code units // - U+F800..U+F87F => 1 code unit // - U+F880..U+FFFF => 2 code units // - U+10000+ => 4 code units // // Since the 1- and 3-code unit cases are now clustered, they can // both be checked together very cheaply. value ^= 0xF800u; value -= 0xF880u; // if scalar is 1 or 3 code units, high byte = 0xFF; else high byte = 0x00 value += (4 << 24); // if scalar is 1 or 3 code units, high byte = 0x03; else high byte = 0x04 value >>= 24; // shift high byte down // Final return value: // - U+0000..U+007F => 3 + (-1) * 2 = 1 // - U+0080..U+07FF => 4 + (-1) * 2 = 2 // - U+0800..U+FFFF => 3 + ( 0) * 2 = 3 // - U+10000+ => 4 + ( 0) * 2 = 4 return((int)value + (a * 2)); }
// non-validating ctor private Rune(uint scalarValue, bool unused) { UnicodeDebug.AssertIsValidScalar(scalarValue); _value = scalarValue; }
/// <summary> /// Returns the Unicode plane (0 through 16, inclusive) which contains this code point. /// </summary> public static int GetPlane(uint codePoint) { UnicodeDebug.AssertIsValidCodePoint(codePoint); return((int)(codePoint >> 16)); }
/// <summary> /// Decodes the <see cref="Rune"/> at the beginning of the provided UTF-8 source buffer. /// </summary> /// <returns> /// <para> /// If the source buffer begins with a valid UTF-8 encoded scalar value, returns <see cref="OperationStatus.Done"/>, /// and outs via <paramref name="result"/> the decoded <see cref="Rune"/> and via <paramref name="bytesConsumed"/> the /// number of <see langword="byte"/>s used in the input buffer to encode the <see cref="Rune"/>. /// </para> /// <para> /// If the source buffer is empty or contains only a partial UTF-8 subsequence, returns <see cref="OperationStatus.NeedMoreData"/>, /// and outs via <paramref name="result"/> <see cref="ReplacementChar"/> and via <paramref name="bytesConsumed"/> the length of the input buffer. /// </para> /// <para> /// If the source buffer begins with an ill-formed UTF-8 encoded scalar value, returns <see cref="OperationStatus.InvalidData"/>, /// and outs via <paramref name="result"/> <see cref="ReplacementChar"/> and via <paramref name="bytesConsumed"/> the number of /// <see langword="char"/>s used in the input buffer to encode the ill-formed sequence. /// </para> /// </returns> /// <remarks> /// The general calling convention is to call this method in a loop, slicing the <paramref name="source"/> buffer by /// <paramref name="bytesConsumed"/> elements on each iteration of the loop. On each iteration of the loop <paramref name="result"/> /// will contain the real scalar value if successfully decoded, or it will contain <see cref="ReplacementChar"/> if /// the data could not be successfully decoded. This pattern provides convenient automatic U+FFFD substitution of /// invalid sequences while iterating through the loop. /// </remarks> public static OperationStatus DecodeFromUtf8(ReadOnlySpan <byte> source, out Rune result, out int bytesConsumed) { // This method follows the Unicode Standard's recommendation for detecting // the maximal subpart of an ill-formed subsequence. See The Unicode Standard, // Ch. 3.9 for more details. In summary, when reporting an invalid subsequence, // it tries to consume as many code units as possible as long as those code // units constitute the beginning of a longer well-formed subsequence per Table 3-7. int index = 0; // Try reading input[0]. if ((uint)index >= (uint)source.Length) { goto NeedsMoreData; } uint tempValue = source[index]; if (!UnicodeUtility.IsAsciiCodePoint(tempValue)) { goto NotAscii; } Finish: bytesConsumed = index + 1; Debug.Assert(1 <= bytesConsumed && bytesConsumed <= 4); // Valid subsequences are always length [1..4] result = UnsafeCreate(tempValue); return(OperationStatus.Done); NotAscii: // Per Table 3-7, the beginning of a multibyte sequence must be a code unit in // the range [C2..F4]. If it's outside of that range, it's either a standalone // continuation byte, or it's an overlong two-byte sequence, or it's an out-of-range // four-byte sequence. if (!UnicodeUtility.IsInRangeInclusive(tempValue, 0xC2, 0xF4)) { goto FirstByteInvalid; } tempValue = (tempValue - 0xC2) << 6; // Try reading input[1]. index++; if ((uint)index >= (uint)source.Length) { goto NeedsMoreData; } // Continuation bytes are of the form [10xxxxxx], which means that their two's // complement representation is in the range [-65..-128]. This allows us to // perform a single comparison to see if a byte is a continuation byte. int thisByteSignExtended = (sbyte)source[index]; if (thisByteSignExtended >= -64) { goto Invalid; } tempValue += (uint)thisByteSignExtended; tempValue += 0x80; // remove the continuation byte marker tempValue += (0xC2 - 0xC0) << 6; // remove the leading byte marker if (tempValue < 0x0800) { Debug.Assert(UnicodeUtility.IsInRangeInclusive(tempValue, 0x0080, 0x07FF)); goto Finish; // this is a valid 2-byte sequence } // This appears to be a 3- or 4-byte sequence. Since per Table 3-7 we now have // enough information (from just two code units) to detect overlong or surrogate // sequences, we need to perform these checks now. if (!UnicodeUtility.IsInRangeInclusive(tempValue, ((0xE0 - 0xC0) << 6) + (0xA0 - 0x80), ((0xF4 - 0xC0) << 6) + (0x8F - 0x80))) { // The first two bytes were not in the range [[E0 A0]..[F4 8F]]. // This is an overlong 3-byte sequence or an out-of-range 4-byte sequence. goto Invalid; } if (UnicodeUtility.IsInRangeInclusive(tempValue, ((0xED - 0xC0) << 6) + (0xA0 - 0x80), ((0xED - 0xC0) << 6) + (0xBF - 0x80))) { // This is a UTF-16 surrogate code point, which is invalid in UTF-8. goto Invalid; } if (UnicodeUtility.IsInRangeInclusive(tempValue, ((0xF0 - 0xC0) << 6) + (0x80 - 0x80), ((0xF0 - 0xC0) << 6) + (0x8F - 0x80))) { // This is an overlong 4-byte sequence. goto Invalid; } // The first two bytes were just fine. We don't need to perform any other checks // on the remaining bytes other than to see that they're valid continuation bytes. // Try reading input[2]. index++; if ((uint)index >= (uint)source.Length) { goto NeedsMoreData; } thisByteSignExtended = (sbyte)source[index]; if (thisByteSignExtended >= -64) { goto Invalid; // this byte is not a UTF-8 continuation byte } tempValue <<= 6; tempValue += (uint)thisByteSignExtended; tempValue += 0x80; // remove the continuation byte marker tempValue -= (0xE0 - 0xC0) << 12; // remove the leading byte marker if (tempValue <= 0xFFFF) { Debug.Assert(UnicodeUtility.IsInRangeInclusive(tempValue, 0x0800, 0xFFFF)); goto Finish; // this is a valid 3-byte sequence } // Try reading input[3]. index++; if ((uint)index >= (uint)source.Length) { goto NeedsMoreData; } thisByteSignExtended = (sbyte)source[index]; if (thisByteSignExtended >= -64) { goto Invalid; // this byte is not a UTF-8 continuation byte } tempValue <<= 6; tempValue += (uint)thisByteSignExtended; tempValue += 0x80; // remove the continuation byte marker tempValue -= (0xF0 - 0xE0) << 18; // remove the leading byte marker UnicodeDebug.AssertIsValidSupplementaryPlaneScalar(tempValue); goto Finish; // this is a valid 4-byte sequence FirstByteInvalid: index = 1; // Invalid subsequences are always at least length 1. Invalid: Debug.Assert(1 <= index && index <= 3); // Invalid subsequences are always length 1..3 bytesConsumed = index; result = ReplacementChar; return(OperationStatus.InvalidData); NeedsMoreData: Debug.Assert(0 <= index && index <= 3); // Incomplete subsequences are always length 0..3 bytesConsumed = index; result = ReplacementChar; return(OperationStatus.NeedMoreData); }