UnicodeDebug.cs 2.2 KB

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