jellyfin/MediaBrowser.Controller/Library/NameExtensions.cs

80 lines
2.2 KiB
C#
Raw Normal View History

2015-04-09 14:11:57 -07:00
using MediaBrowser.Common.Extensions;
using MoreLinq;
using System;
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;
namespace MediaBrowser.Controller.Library
{
public static class NameExtensions
{
2015-04-15 08:41:42 -07:00
public static bool AreEqual(string x, string y)
2015-04-09 14:11:57 -07:00
{
2015-04-15 08:41:42 -07:00
if (string.IsNullOrWhiteSpace(x) && string.IsNullOrWhiteSpace(y))
{
return true;
}
2015-04-09 14:11:57 -07:00
2015-04-15 08:41:42 -07:00
return string.Compare(x, y, CultureInfo.InvariantCulture, CompareOptions.IgnoreCase | CompareOptions.IgnoreNonSpace) == 0;
2015-04-09 14:11:57 -07:00
}
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
2015-04-15 08:41:42 -07:00
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
}
}
}