UnicodeDebug.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. // See the LICENSE file in the project root for more information.
  4. using System.Diagnostics;
  5. namespace System.Text
  6. {
  7. internal static class UnicodeDebug
  8. {
  9. [Conditional("DEBUG")]
  10. internal static void AssertIsHighSurrogateCodePoint(uint codePoint)
  11. {
  12. Debug.Assert(UnicodeUtility.IsHighSurrogateCodePoint(codePoint), $"The value {ToHexString(codePoint)} is not a valid UTF-16 high surrogate code point.");
  13. }
  14. [Conditional("DEBUG")]
  15. internal static void AssertIsLowSurrogateCodePoint(uint codePoint)
  16. {
  17. Debug.Assert(UnicodeUtility.IsLowSurrogateCodePoint(codePoint), $"The value {ToHexString(codePoint)} is not a valid UTF-16 low surrogate code point.");
  18. }
  19. [Conditional("DEBUG")]
  20. internal static void AssertIsValidCodePoint(uint codePoint)
  21. {
  22. Debug.Assert(UnicodeUtility.IsValidCodePoint(codePoint), $"The value {ToHexString(codePoint)} is not a valid Unicode code point.");
  23. }
  24. [Conditional("DEBUG")]
  25. internal static void AssertIsValidScalar(uint scalarValue)
  26. {
  27. Debug.Assert(UnicodeUtility.IsValidUnicodeScalar(scalarValue), $"The value {ToHexString(scalarValue)} is not a valid Unicode scalar value.");
  28. }
  29. [Conditional("DEBUG")]
  30. internal static void AssertIsValidSupplementaryPlaneScalar(uint scalarValue)
  31. {
  32. Debug.Assert(UnicodeUtility.IsValidUnicodeScalar(scalarValue) && !UnicodeUtility.IsBmpCodePoint(scalarValue), $"The value {ToHexString(scalarValue)} is not a valid supplementary plane Unicode scalar value.");
  33. }
  34. /// <summary>
  35. /// Formats a code point as the hex string "U+XXXX".
  36. /// </summary>
  37. /// <remarks>
  38. /// The input value doesn't have to be a real code point in the Unicode codespace. It can be any integer.
  39. /// </remarks>
  40. private static string ToHexString(uint codePoint)
  41. {
  42. return FormattableString.Invariant($"U+{codePoint:X4}");
  43. }
  44. }
  45. }