StringExtensions.cs 3.4 KB

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