2020-08-22 19:56:24 +00:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2019-01-27 09:20:05 +00:00
|
|
|
using System;
|
2021-05-20 19:28:18 +00:00
|
|
|
using System.Collections.Generic;
|
2019-01-27 09:20:05 +00:00
|
|
|
using System.Globalization;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Text;
|
|
|
|
using System.Text.RegularExpressions;
|
2018-12-27 23:27:57 +00:00
|
|
|
|
|
|
|
namespace MediaBrowser.Controller.Extensions
|
|
|
|
{
|
|
|
|
/// <summary>
|
2020-06-15 22:37:52 +00:00
|
|
|
/// Class BaseExtensions.
|
2018-12-27 23:27:57 +00:00
|
|
|
/// </summary>
|
|
|
|
public static class StringExtensions
|
|
|
|
{
|
|
|
|
public static string RemoveDiacritics(this string text)
|
|
|
|
{
|
2019-01-27 09:20:05 +00:00
|
|
|
var chars = Normalize(text, NormalizationForm.FormD)
|
|
|
|
.Where(ch => CharUnicodeInfo.GetUnicodeCategory(ch) != UnicodeCategory.NonSpacingMark);
|
|
|
|
|
|
|
|
return Normalize(string.Concat(chars), NormalizationForm.FormC);
|
|
|
|
}
|
2021-05-23 22:30:41 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Counts the number of occurrences of [needle] in the string.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="value">The haystack to search in.</param>
|
|
|
|
/// <param name="needle">The character to search for.</param>
|
|
|
|
/// <returns>The number of occurrences of the [needle] character.</returns>
|
|
|
|
public static int CountOccurrences(this ReadOnlySpan<char> value, char needle)
|
|
|
|
{
|
|
|
|
var count = 0;
|
|
|
|
var length = value.Length;
|
|
|
|
for (var i = 0; i < length; i++)
|
|
|
|
{
|
|
|
|
if (value[i] == needle)
|
|
|
|
{
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
2019-01-27 09:20:05 +00:00
|
|
|
|
|
|
|
private static string Normalize(string text, NormalizationForm form, bool stripStringOnFailure = true)
|
|
|
|
{
|
|
|
|
if (stripStringOnFailure)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return text.Normalize(form);
|
|
|
|
}
|
|
|
|
catch (ArgumentException)
|
|
|
|
{
|
|
|
|
// will throw if input contains invalid unicode chars
|
|
|
|
// https://mnaoumov.wordpress.com/2014/06/14/stripping-invalid-characters-from-utf-16-strings/
|
2021-05-13 13:32:02 +00:00
|
|
|
text = Regex.Replace(text, "([\ud800-\udbff](?![\udc00-\udfff]))|((?<![\ud800-\udbff])[\udc00-\udfff])", string.Empty);
|
2019-01-27 09:20:05 +00:00
|
|
|
return Normalize(text, form, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return text.Normalize(form);
|
|
|
|
}
|
|
|
|
catch (ArgumentException)
|
|
|
|
{
|
|
|
|
// if it still fails, return the original text
|
|
|
|
return text;
|
|
|
|
}
|
2018-12-27 23:27:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|