jellyfin-server/MediaBrowser.Controller/Library/NameExtensions.cs

71 lines
2.0 KiB
C#
Raw Normal View History

2016-10-25 19:02:04 +00:00
using System;
2015-04-09 21:11:57 +00:00
using System.Collections.Generic;
2015-04-15 15:41:42 +00:00
using System.Globalization;
2015-04-09 21:11:57 +00:00
using System.Linq;
using MediaBrowser.Controller.Extensions;
2016-10-22 02:08:34 +00:00
using MediaBrowser.Model.Extensions;
2015-04-09 21:11:57 +00:00
namespace MediaBrowser.Controller.Library
{
public static class NameExtensions
{
2015-04-15 15:41:42 +00:00
public static bool EqualsAny(IEnumerable<string> names, string x)
2015-04-09 21:11:57 +00:00
{
2015-04-15 15:41:42 +00:00
x = NormalizeForComparison(x);
2015-04-09 21:11:57 +00:00
2016-10-25 19:02:04 +00: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 21:11:57 +00:00
}
private static string NormalizeForComparison(string name)
{
2015-04-15 15:41:42 +00:00
if (name == null)
{
return string.Empty;
}
return name;
//return name.RemoveDiacritics();
}
private static string RemoveDiacritics(string name)
{
if (name == null)
2015-04-09 21:11:57 +00:00
{
return string.Empty;
}
2015-04-15 15:41:42 +00:00
//return name;
2015-04-09 21:11:57 +00:00
return name.RemoveDiacritics();
}
public static IEnumerable<string> DistinctNames(this IEnumerable<string> names)
{
2015-04-15 15:41:42 +00:00
return names.DistinctBy(RemoveDiacritics, StringComparer.OrdinalIgnoreCase);
}
}
2016-09-06 05:02:05 +00:00
public class DistinctNameComparer : IComparer<string>, IEqualityComparer<string>
2015-04-15 15:41:42 +00:00
{
public int Compare(string x, string y)
{
if (string.IsNullOrWhiteSpace(x) && string.IsNullOrWhiteSpace(y))
{
return 0;
}
2016-09-06 05:02:05 +00:00
return string.Compare(x.RemoveDiacritics(), y.RemoveDiacritics(), StringComparison.OrdinalIgnoreCase);
2015-04-15 15:41:42 +00: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 21:11:57 +00:00
}
}
}