2016-10-25 12:02:04 -07:00
|
|
|
|
using System;
|
2015-04-09 14:11:57 -07:00
|
|
|
|
using System.Collections.Generic;
|
2015-04-15 08:41:42 -07:00
|
|
|
|
using System.Globalization;
|
2015-04-09 14:11:57 -07:00
|
|
|
|
using System.Linq;
|
2016-10-23 12:14:57 -07:00
|
|
|
|
using MediaBrowser.Controller.Extensions;
|
2016-10-21 19:08:34 -07:00
|
|
|
|
using MediaBrowser.Model.Extensions;
|
2015-04-09 14:11:57 -07:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Controller.Library
|
|
|
|
|
{
|
|
|
|
|
public static class NameExtensions
|
|
|
|
|
{
|
2015-04-15 08:41:42 -07:00
|
|
|
|
public static bool EqualsAny(IEnumerable<string> names, string x)
|
2015-04-09 14:11:57 -07:00
|
|
|
|
{
|
2015-04-15 08:41:42 -07:00
|
|
|
|
x = NormalizeForComparison(x);
|
2015-04-09 14:11:57 -07:00
|
|
|
|
|
2016-10-25 12:02:04 -07:00
|
|
|
|
return names.Any(y => string.Compare(x, y, StringComparison.OrdinalIgnoreCase) == 0);
|
|
|
|
|
//return names.Any(y => string.Compare(x, y, CultureInfo.InvariantCulture, CompareOptions.IgnoreCase | CompareOptions.IgnoreNonSpace) == 0);
|
2015-04-09 14:11:57 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string NormalizeForComparison(string name)
|
|
|
|
|
{
|
2015-04-15 08:41:42 -07:00
|
|
|
|
if (name == null)
|
|
|
|
|
{
|
|
|
|
|
return string.Empty;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return name;
|
|
|
|
|
//return name.RemoveDiacritics();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string RemoveDiacritics(string name)
|
|
|
|
|
{
|
|
|
|
|
if (name == null)
|
2015-04-09 14:11:57 -07:00
|
|
|
|
{
|
|
|
|
|
return string.Empty;
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-15 08:41:42 -07:00
|
|
|
|
//return name;
|
2015-04-09 14:11:57 -07:00
|
|
|
|
return name.RemoveDiacritics();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static IEnumerable<string> DistinctNames(this IEnumerable<string> names)
|
|
|
|
|
{
|
2015-04-15 08:41:42 -07:00
|
|
|
|
return names.DistinctBy(RemoveDiacritics, StringComparer.OrdinalIgnoreCase);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-05 22:02:05 -07:00
|
|
|
|
public class DistinctNameComparer : IComparer<string>, IEqualityComparer<string>
|
2015-04-15 08:41:42 -07:00
|
|
|
|
{
|
|
|
|
|
public int Compare(string x, string y)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(x) && string.IsNullOrWhiteSpace(y))
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-05 22:02:05 -07:00
|
|
|
|
return string.Compare(x.RemoveDiacritics(), y.RemoveDiacritics(), StringComparison.OrdinalIgnoreCase);
|
2015-04-15 08:41:42 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Equals(string x, string y)
|
|
|
|
|
{
|
|
|
|
|
return Compare(x, y) == 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int GetHashCode(string obj)
|
|
|
|
|
{
|
|
|
|
|
return (obj ?? string.Empty).GetHashCode();
|
2015-04-09 14:11:57 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|