| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Diagnostics; |
| | | 5 | | |
| | | 6 | | namespace System.Text |
| | | 7 | | { |
| | | 8 | | internal static class UnicodeDebug |
| | | 9 | | { |
| | | 10 | | [Conditional("DEBUG")] |
| | | 11 | | internal static void AssertIsBmpCodePoint(uint codePoint) |
| | | 12 | | { |
| | | 13 | | if (!UnicodeUtility.IsBmpCodePoint(codePoint)) |
| | | 14 | | { |
| | | 15 | | Debug.Fail($"The value {ToHexString(codePoint)} is not a valid BMP code point."); |
| | | 16 | | } |
| | | 17 | | } |
| | | 18 | | |
| | | 19 | | [Conditional("DEBUG")] |
| | | 20 | | internal static void AssertIsHighSurrogateCodePoint(uint codePoint) |
| | | 21 | | { |
| | 0 | 22 | | if (!UnicodeUtility.IsHighSurrogateCodePoint(codePoint)) |
| | | 23 | | { |
| | 0 | 24 | | Debug.Fail($"The value {ToHexString(codePoint)} is not a valid UTF-16 high surrogate code point."); |
| | | 25 | | } |
| | 0 | 26 | | } |
| | | 27 | | |
| | | 28 | | [Conditional("DEBUG")] |
| | | 29 | | internal static void AssertIsLowSurrogateCodePoint(uint codePoint) |
| | | 30 | | { |
| | 0 | 31 | | if (!UnicodeUtility.IsLowSurrogateCodePoint(codePoint)) |
| | | 32 | | { |
| | 0 | 33 | | Debug.Fail($"The value {ToHexString(codePoint)} is not a valid UTF-16 low surrogate code point."); |
| | | 34 | | } |
| | 0 | 35 | | } |
| | | 36 | | |
| | | 37 | | [Conditional("DEBUG")] |
| | | 38 | | internal static void AssertIsValidCodePoint(uint codePoint) |
| | | 39 | | { |
| | 0 | 40 | | if (!UnicodeUtility.IsValidCodePoint(codePoint)) |
| | | 41 | | { |
| | 0 | 42 | | Debug.Fail($"The value {ToHexString(codePoint)} is not a valid Unicode code point."); |
| | | 43 | | } |
| | 0 | 44 | | } |
| | | 45 | | |
| | | 46 | | [Conditional("DEBUG")] |
| | | 47 | | internal static void AssertIsValidScalar(uint scalarValue) |
| | | 48 | | { |
| | 5257057 | 49 | | if (!UnicodeUtility.IsValidUnicodeScalar(scalarValue)) |
| | | 50 | | { |
| | 0 | 51 | | Debug.Fail($"The value {ToHexString(scalarValue)} is not a valid Unicode scalar value."); |
| | | 52 | | } |
| | 5257057 | 53 | | } |
| | | 54 | | |
| | | 55 | | [Conditional("DEBUG")] |
| | | 56 | | internal static void AssertIsValidSupplementaryPlaneScalar(uint scalarValue) |
| | | 57 | | { |
| | 6380 | 58 | | if (!UnicodeUtility.IsValidUnicodeScalar(scalarValue) || UnicodeUtility.IsBmpCodePoint(scalarValue)) |
| | | 59 | | { |
| | 0 | 60 | | Debug.Fail($"The value {ToHexString(scalarValue)} is not a valid supplementary plane Unicode scalar valu |
| | | 61 | | } |
| | 6380 | 62 | | } |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// Formats a code point as the hex string "U+XXXX". |
| | | 66 | | /// </summary> |
| | | 67 | | /// <remarks> |
| | | 68 | | /// The input value doesn't have to be a real code point in the Unicode codespace. It can be any integer. |
| | | 69 | | /// </remarks> |
| | | 70 | | private static string ToHexString(uint codePoint) |
| | | 71 | | { |
| | 0 | 72 | | return FormattableString.Invariant($"U+{codePoint:X4}"); |
| | | 73 | | } |
| | | 74 | | } |
| | | 75 | | } |
| | | 76 | | |