2
0

StringExtensions.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. using System.Buffers;
  2. using System.Globalization;
  3. namespace Terminal.Gui.Text;
  4. /// <summary>Extensions to <see cref="string"/> to support TUI text manipulation.</summary>
  5. public static class StringExtensions
  6. {
  7. /// <summary>Unpacks the last UTF-8 encoding in the string.</summary>
  8. /// <remarks>This is a Terminal.Gui extension method to <see cref="string"/> to support TUI text manipulation.</remarks>
  9. /// <param name="str">The string to decode.</param>
  10. /// <param name="end">Index in string to stop at; if -1, use the buffer length.</param>
  11. /// <returns></returns>
  12. public static (Rune rune, int size) DecodeLastRune (this string str, int end = -1)
  13. {
  14. Rune rune = str.EnumerateRunes ().ToArray () [end == -1 ? ^1 : end];
  15. byte [] bytes = Encoding.UTF8.GetBytes (rune.ToString ());
  16. OperationStatus operationStatus = Rune.DecodeFromUtf8 (bytes, out rune, out int bytesConsumed);
  17. if (operationStatus == OperationStatus.Done)
  18. {
  19. return (rune, bytesConsumed);
  20. }
  21. return (Rune.ReplacementChar, 1);
  22. }
  23. /// <summary>Unpacks the first UTF-8 encoding in the string and returns the rune and its width in bytes.</summary>
  24. /// <remarks>This is a Terminal.Gui extension method to <see cref="string"/> to support TUI text manipulation.</remarks>
  25. /// <param name="str">The string to decode.</param>
  26. /// <param name="start">Starting offset.</param>
  27. /// <param name="count">Number of bytes in the buffer, or -1 to make it the length of the buffer.</param>
  28. /// <returns></returns>
  29. public static (Rune Rune, int Size) DecodeRune (this string str, int start = 0, int count = -1)
  30. {
  31. Rune rune = str.EnumerateRunes ().ToArray () [start];
  32. byte [] bytes = Encoding.UTF8.GetBytes (rune.ToString ());
  33. if (count == -1)
  34. {
  35. count = bytes.Length;
  36. }
  37. OperationStatus operationStatus = Rune.DecodeFromUtf8 (bytes, out rune, out int bytesConsumed);
  38. if (operationStatus == OperationStatus.Done && bytesConsumed >= count)
  39. {
  40. return (rune, bytesConsumed);
  41. }
  42. return (Rune.ReplacementChar, 1);
  43. }
  44. /// <summary>Gets the number of columns the string occupies in the terminal.</summary>
  45. /// <remarks>This is a Terminal.Gui extension method to <see cref="string"/> to support TUI text manipulation.</remarks>
  46. /// <param name="str">The string to measure.</param>
  47. /// <returns></returns>
  48. public static int GetColumns (this string str)
  49. {
  50. if (string.IsNullOrEmpty (str))
  51. {
  52. return 0;
  53. }
  54. var total = 0;
  55. TextElementEnumerator enumerator = StringInfo.GetTextElementEnumerator (str);
  56. while (enumerator.MoveNext ())
  57. {
  58. string element = enumerator.GetTextElement ();
  59. // Get the maximum rune width within this grapheme cluster
  60. int width = element
  61. .EnumerateRunes ()
  62. .Max (r => Math.Max (r.GetColumns (), 0));
  63. total += width;
  64. }
  65. return total;
  66. }
  67. /// <summary>Gets the number of runes in the string.</summary>
  68. /// <remarks>This is a Terminal.Gui extension method to <see cref="string"/> to support TUI text manipulation.</remarks>
  69. /// <param name="str">The string to count.</param>
  70. /// <returns></returns>
  71. public static int GetRuneCount (this string str) { return str.EnumerateRunes ().Count (); }
  72. /// <summary>
  73. /// Determines if this <see cref="ReadOnlySpan{T}"/> of <see langword="char"/> is composed entirely of ASCII
  74. /// digits.
  75. /// </summary>
  76. /// <param name="stringSpan">A <see cref="ReadOnlySpan{T}"/> of <see langword="char"/> to check.</param>
  77. /// <returns>
  78. /// A <see langword="bool"/> indicating if all elements of the <see cref="ReadOnlySpan{T}"/> are ASCII digits (
  79. /// <see langword="true"/>) or not (<see langword="false"/>
  80. /// </returns>
  81. public static bool IsAllAsciiDigits (this ReadOnlySpan<char> stringSpan) { return stringSpan.ToString ().All (char.IsAsciiDigit); }
  82. /// <summary>
  83. /// Determines if this <see cref="ReadOnlySpan{T}"/> of <see langword="char"/> is composed entirely of ASCII
  84. /// digits.
  85. /// </summary>
  86. /// <param name="stringSpan">A <see cref="ReadOnlySpan{T}"/> of <see langword="char"/> to check.</param>
  87. /// <returns>
  88. /// A <see langword="bool"/> indicating if all elements of the <see cref="ReadOnlySpan{T}"/> are ASCII digits (
  89. /// <see langword="true"/>) or not (<see langword="false"/>
  90. /// </returns>
  91. public static bool IsAllAsciiHexDigits (this ReadOnlySpan<char> stringSpan) { return stringSpan.ToString ().All (char.IsAsciiHexDigit); }
  92. /// <summary>Repeats the string <paramref name="n"/> times.</summary>
  93. /// <remarks>This is a Terminal.Gui extension method to <see cref="string"/> to support TUI text manipulation.</remarks>
  94. /// <param name="str">The text to repeat.</param>
  95. /// <param name="n">Number of times to repeat the text.</param>
  96. /// <returns>The text repeated if <paramref name="n"/> is greater than zero, otherwise <see langword="null"/>.</returns>
  97. public static string? Repeat (this string str, int n)
  98. {
  99. if (n <= 0)
  100. {
  101. return null;
  102. }
  103. if (string.IsNullOrEmpty (str) || n == 1)
  104. {
  105. return str;
  106. }
  107. return new StringBuilder (str.Length * n)
  108. .Insert (0, str, n)
  109. .ToString ();
  110. }
  111. /// <summary>Converts the string into a <see cref="List{Rune}"/>.</summary>
  112. /// <remarks>This is a Terminal.Gui extension method to <see cref="string"/> to support TUI text manipulation.</remarks>
  113. /// <param name="str">The string to convert.</param>
  114. /// <returns></returns>
  115. public static List<Rune> ToRuneList (this string str) { return str.EnumerateRunes ().ToList (); }
  116. /// <summary>Converts the string into a <see cref="Rune"/> array.</summary>
  117. /// <remarks>This is a Terminal.Gui extension method to <see cref="string"/> to support TUI text manipulation.</remarks>
  118. /// <param name="str">The string to convert.</param>
  119. /// <returns></returns>
  120. public static Rune [] ToRunes (this string str) { return str.EnumerateRunes ().ToArray (); }
  121. /// <summary>Converts a <see cref="Rune"/> generic collection into a string.</summary>
  122. /// <param name="runes">The enumerable rune to convert.</param>
  123. /// <returns></returns>
  124. public static string ToString (IEnumerable<Rune> runes)
  125. {
  126. const int maxCharsPerRune = 2;
  127. const int maxStackallocTextBufferSize = 1048; // ~2 kB
  128. // If rune count is easily available use stackalloc buffer or alternatively rented array.
  129. if (runes.TryGetNonEnumeratedCount (out int count))
  130. {
  131. if (count == 0)
  132. {
  133. return string.Empty;
  134. }
  135. char[]? rentedBufferArray = null;
  136. try
  137. {
  138. int maxRequiredTextBufferSize = count * maxCharsPerRune;
  139. Span<char> textBuffer = maxRequiredTextBufferSize <= maxStackallocTextBufferSize
  140. ? stackalloc char[maxRequiredTextBufferSize]
  141. : (rentedBufferArray = ArrayPool<char>.Shared.Rent(maxRequiredTextBufferSize));
  142. Span<char> remainingBuffer = textBuffer;
  143. foreach (Rune rune in runes)
  144. {
  145. int charsWritten = rune.EncodeToUtf16 (remainingBuffer);
  146. remainingBuffer = remainingBuffer [charsWritten..];
  147. }
  148. ReadOnlySpan<char> text = textBuffer[..^remainingBuffer.Length];
  149. return text.ToString ();
  150. }
  151. finally
  152. {
  153. if (rentedBufferArray != null)
  154. {
  155. ArrayPool<char>.Shared.Return (rentedBufferArray);
  156. }
  157. }
  158. }
  159. // Fallback to StringBuilder append.
  160. StringBuilder stringBuilder = new();
  161. Span<char> runeBuffer = stackalloc char[maxCharsPerRune];
  162. foreach (Rune rune in runes)
  163. {
  164. int charsWritten = rune.EncodeToUtf16 (runeBuffer);
  165. ReadOnlySpan<char> runeChars = runeBuffer [..charsWritten];
  166. stringBuilder.Append (runeChars);
  167. }
  168. return stringBuilder.ToString ();
  169. }
  170. /// <summary>Converts a byte generic collection into a string in the provided encoding (default is UTF8)</summary>
  171. /// <param name="bytes">The enumerable byte to convert.</param>
  172. /// <param name="encoding">The encoding to be used.</param>
  173. /// <returns></returns>
  174. public static string ToString (IEnumerable<byte> bytes, Encoding? encoding = null)
  175. {
  176. encoding ??= Encoding.UTF8;
  177. return encoding.GetString (bytes.ToArray ());
  178. }
  179. }