RuneExtensions.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using System.Globalization;
  2. using System.Text;
  3. using Wcwidth;
  4. namespace Terminal.Gui;
  5. /// <summary>
  6. /// Extends <see cref="System.Text.Rune"/> to support TUI text manipulation.
  7. /// </summary>
  8. public static class RuneExtensions {
  9. /// <summary>
  10. /// Maximum Unicode code point.
  11. /// </summary>
  12. public static int MaxUnicodeCodePoint = 0x10FFFF;
  13. /// <summary>
  14. /// Gets the number of columns the rune occupies in the terminal.
  15. /// </summary>
  16. /// <remarks>
  17. /// This is a Terminal.Gui extension method to <see cref="System.Text.Rune"/> to support TUI text manipulation.
  18. /// </remarks>
  19. /// <param name="rune">The rune to measure.</param>
  20. /// <returns>
  21. /// The number of columns required to fit the rune, 0 if the argument is the null character, or
  22. /// -1 if the value is not printable,
  23. /// otherwise the number of columns that the rune occupies.
  24. /// </returns>
  25. public static int GetColumns (this Rune rune)
  26. {
  27. return UnicodeCalculator.GetWidth (rune);
  28. }
  29. /// <summary>
  30. /// Returns <see langword="true"/> if the rune is a combining character.
  31. /// </summary>
  32. /// <remarks>
  33. /// This is a Terminal.Gui extension method to <see cref="System.Text.Rune"/> to support TUI text manipulation.
  34. /// </remarks>
  35. /// <param name="rune"></param>
  36. /// <returns></returns>
  37. public static bool IsCombiningMark (this System.Text.Rune rune)
  38. {
  39. UnicodeCategory category = Rune.GetUnicodeCategory (rune);
  40. return Rune.GetUnicodeCategory (rune) == UnicodeCategory.NonSpacingMark
  41. || category == UnicodeCategory.SpacingCombiningMark
  42. || category == UnicodeCategory.EnclosingMark;
  43. }
  44. /// <summary>
  45. /// Ensures the rune is not a control character and can be displayed by translating characters below 0x20
  46. /// to equivalent, printable, Unicode chars.
  47. /// </summary>
  48. /// <remarks>
  49. /// This is a Terminal.Gui extension method to <see cref="System.Text.Rune"/> to support TUI text manipulation.
  50. /// </remarks>
  51. /// <param name="rune"></param>
  52. /// <returns></returns>
  53. public static Rune MakePrintable (this System.Text.Rune rune) => Rune.IsControl (rune) ? new Rune (rune.Value + 0x2400) : rune;
  54. /// <summary>
  55. /// Get number of bytes required to encode the rune, based on the provided encoding.
  56. /// </summary>
  57. /// <remarks>
  58. /// This is a Terminal.Gui extension method to <see cref="System.Text.Rune"/> to support TUI text manipulation.
  59. /// </remarks>
  60. /// <param name="rune">The rune to probe.</param>
  61. /// <param name="encoding">The encoding used; the default is UTF8.</param>
  62. /// <returns>The number of bytes required.</returns>
  63. public static int GetEncodingLength (this Rune rune, Encoding encoding = null)
  64. {
  65. encoding ??= Encoding.UTF8;
  66. var bytes = encoding.GetBytes (rune.ToString ().ToCharArray ());
  67. var offset = 0;
  68. if (bytes [^1] == 0) {
  69. offset++;
  70. }
  71. return bytes.Length - offset;
  72. }
  73. /// <summary>
  74. /// Writes into the destination buffer starting at offset the UTF8 encoded version of the rune.
  75. /// </summary>
  76. /// <remarks>
  77. /// This is a Terminal.Gui extension method to <see cref="System.Text.Rune"/> to support TUI text manipulation.
  78. /// </remarks>
  79. /// <param name="rune">The rune to encode.</param>
  80. /// <param name="dest">The destination buffer.</param>
  81. /// <param name="start">Starting offset to look into.</param>
  82. /// <param name="count">Number of bytes valid in the buffer, or -1 to make it the length of the buffer.</param>
  83. /// <returns>he number of bytes written into the destination buffer.</returns>
  84. public static int Encode (this Rune rune, byte [] dest, int start = 0, int count = -1)
  85. {
  86. var bytes = Encoding.UTF8.GetBytes (rune.ToString ());
  87. var length = 0;
  88. for (var i = 0; i < (count == -1 ? bytes.Length : count); i++) {
  89. if (bytes [i] == 0) {
  90. break;
  91. }
  92. dest [start + i] = bytes [i];
  93. length++;
  94. }
  95. return length;
  96. }
  97. /// <summary>
  98. /// Attempts to decode the rune as a surrogate pair to UTF-16.
  99. /// </summary>
  100. /// <remarks>
  101. /// This is a Terminal.Gui extension method to <see cref="System.Text.Rune"/> to support TUI text manipulation.
  102. /// </remarks>
  103. /// <param name="rune">The rune to decode.</param>
  104. /// <param name="chars">The chars if the rune is a surrogate pair. Null otherwise.</param>
  105. /// <returns><see langword="true"/> if the rune is a valid surrogate pair; <see langword="false"/> otherwise.</returns>
  106. public static bool DecodeSurrogatePair (this Rune rune, out char [] chars)
  107. {
  108. if (rune.IsSurrogatePair ()) {
  109. chars = rune.ToString ().ToCharArray ();
  110. return true;
  111. }
  112. chars = null;
  113. return false;
  114. }
  115. /// <summary>
  116. /// Attempts to encode (as UTF-16) a surrogate pair.
  117. /// </summary>
  118. /// <param name="highSurrogate">The high surrogate code point.</param>
  119. /// <param name="lowSurrogate">The low surrogate code point.</param>
  120. /// <param name="result">The encoded rune.</param>
  121. /// <returns><see langword="true"/> if the encoding succeeded; <see langword="false"/> otherwise.</returns>
  122. public static bool EncodeSurrogatePair (char highSurrogate, char lowSurrogate, out Rune result)
  123. {
  124. result = default;
  125. if (char.IsSurrogatePair (highSurrogate, lowSurrogate)) {
  126. result = (Rune)char.ConvertToUtf32 (highSurrogate, lowSurrogate);
  127. return true;
  128. }
  129. return false;
  130. }
  131. /// <summary>
  132. /// Reports whether a rune is a surrogate code point.
  133. /// </summary>
  134. /// <remarks>
  135. /// This is a Terminal.Gui extension method to <see cref="System.Text.Rune"/> to support TUI text manipulation.
  136. /// </remarks>
  137. /// <param name="rune">The rune to probe.</param>
  138. /// <returns><see langword="true"/> if the rune is a surrogate code point; <see langword="false"/> otherwise.</returns>
  139. public static bool IsSurrogatePair (this Rune rune)
  140. {
  141. return char.IsSurrogatePair (rune.ToString (), 0);
  142. }
  143. /// <summary>
  144. /// Reports if the provided array of bytes can be encoded as UTF-8.
  145. /// </summary>
  146. /// <param name="buffer">The byte array to probe.</param>
  147. /// <value><c>true</c> if is valid; otherwise, <c>false</c>.</value>
  148. public static bool CanBeEncodedAsRune (byte [] buffer)
  149. {
  150. var str = Encoding.Unicode.GetString (buffer);
  151. foreach (var rune in str.EnumerateRunes ()) {
  152. if (rune == Rune.ReplacementChar) {
  153. return false;
  154. }
  155. }
  156. return true;
  157. }
  158. }