StringExtensions.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System;
  2. using System.Diagnostics;
  3. using System.Globalization;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. namespace Jellyfin.Extensions
  7. {
  8. /// <summary>
  9. /// Provides extensions methods for <see cref="string" />.
  10. /// </summary>
  11. public static class StringExtensions
  12. {
  13. // Matches non-conforming unicode chars
  14. // https://mnaoumov.wordpress.com/2014/06/14/stripping-invalid-characters-from-utf-16-strings/
  15. private static readonly Regex _nonConformingUnicode = new Regex("([\ud800-\udbff](?![\udc00-\udfff]))|((?<![\ud800-\udbff])[\udc00-\udfff])|(\ufffd)");
  16. /// <summary>
  17. /// Removes the diacritics character from the strings.
  18. /// </summary>
  19. /// <param name="text">The string to act on.</param>
  20. /// <returns>The string without diacritics character.</returns>
  21. public static string RemoveDiacritics(this string text)
  22. {
  23. string withDiactritics = _nonConformingUnicode
  24. .Replace(text, string.Empty)
  25. .Normalize(NormalizationForm.FormD);
  26. var withoutDiactritics = new StringBuilder();
  27. foreach (char c in withDiactritics)
  28. {
  29. UnicodeCategory uc = CharUnicodeInfo.GetUnicodeCategory(c);
  30. if (uc != UnicodeCategory.NonSpacingMark)
  31. {
  32. withoutDiactritics.Append(c);
  33. }
  34. }
  35. return withoutDiactritics.ToString().Normalize(NormalizationForm.FormC);
  36. }
  37. /// <summary>
  38. /// Checks wether or not the specified string has diacritics in it.
  39. /// </summary>
  40. /// <param name="text">The string to check.</param>
  41. /// <returns>True if the string has diacritics, false otherwise.</returns>
  42. public static bool HasDiacritics(this string text)
  43. {
  44. return !string.Equals(text, text.RemoveDiacritics(), StringComparison.Ordinal);
  45. }
  46. /// <summary>
  47. /// Counts the number of occurrences of [needle] in the string.
  48. /// </summary>
  49. /// <param name="value">The haystack to search in.</param>
  50. /// <param name="needle">The character to search for.</param>
  51. /// <returns>The number of occurrences of the [needle] character.</returns>
  52. public static int Count(this ReadOnlySpan<char> value, char needle)
  53. {
  54. var count = 0;
  55. var length = value.Length;
  56. for (var i = 0; i < length; i++)
  57. {
  58. if (value[i] == needle)
  59. {
  60. count++;
  61. }
  62. }
  63. return count;
  64. }
  65. /// <summary>
  66. /// Returns the part on the left of the <c>needle</c>.
  67. /// </summary>
  68. /// <param name="haystack">The string to seek.</param>
  69. /// <param name="needle">The needle to find.</param>
  70. /// <returns>The part left of the <paramref name="needle" />.</returns>
  71. public static ReadOnlySpan<char> LeftPart(this ReadOnlySpan<char> haystack, char needle)
  72. {
  73. var pos = haystack.IndexOf(needle);
  74. return pos == -1 ? haystack : haystack[..pos];
  75. }
  76. /// <summary>
  77. /// Returns the part on the right of the <c>needle</c>.
  78. /// </summary>
  79. /// <param name="haystack">The string to seek.</param>
  80. /// <param name="needle">The needle to find.</param>
  81. /// <returns>The part right of the <paramref name="needle" />.</returns>
  82. public static ReadOnlySpan<char> RightPart(this ReadOnlySpan<char> haystack, char needle)
  83. {
  84. var pos = haystack.LastIndexOf(needle);
  85. if (pos == -1)
  86. {
  87. return haystack;
  88. }
  89. if (pos == haystack.Length - 1)
  90. {
  91. return ReadOnlySpan<char>.Empty;
  92. }
  93. return haystack[(pos + 1)..];
  94. }
  95. }
  96. }