2020-09-20 05:02:41 -07:00
|
|
|
#nullable enable
|
2020-08-22 12:56:24 -07:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2019-01-27 02:20:05 -07:00
|
|
|
using System;
|
|
|
|
using System.Globalization;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Text;
|
|
|
|
using System.Text.RegularExpressions;
|
2018-12-27 16:27:57 -07:00
|
|
|
|
|
|
|
namespace MediaBrowser.Controller.Extensions
|
|
|
|
{
|
|
|
|
/// <summary>
|
2020-06-15 15:37:52 -07:00
|
|
|
/// Class BaseExtensions.
|
2018-12-27 16:27:57 -07:00
|
|
|
/// </summary>
|
|
|
|
public static class StringExtensions
|
|
|
|
{
|
|
|
|
public static string RemoveDiacritics(this string text)
|
|
|
|
{
|
2019-01-27 02:20:05 -07:00
|
|
|
var chars = Normalize(text, NormalizationForm.FormD)
|
|
|
|
.Where(ch => CharUnicodeInfo.GetUnicodeCategory(ch) != UnicodeCategory.NonSpacingMark);
|
|
|
|
|
|
|
|
return Normalize(string.Concat(chars), NormalizationForm.FormC);
|
|
|
|
}
|
|
|
|
|
|
|
|
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/
|
|
|
|
text = Regex.Replace(text, "([\ud800-\udbff](?![\udc00-\udfff]))|((?<![\ud800-\udbff])[\udc00-\udfff])", "");
|
|
|
|
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 16:27:57 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|