CultureSpecificCharComparer
using System;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Runtime.InteropServices;
namespace DotNext
{
internal sealed class CultureSpecificCharComparer : CharComparer
{
private readonly CompareInfo comparison;
private readonly CompareOptions options;
internal CultureSpecificCharComparer(CultureInfo culture, CompareOptions options)
{
comparison = culture.CompareInfo;
this.options = options;
}
public override bool Equals(char x, char y)
{
return Compare(x, y) == 0;
}
public override int Compare(char x, char y)
{
return comparison.Compare(MemoryMarshal.CreateReadOnlySpan(ref x, 1), MemoryMarshal.CreateReadOnlySpan(ref y, 1), options);
}
public override int GetHashCode(char ch)
{
return comparison.GetHashCode(MemoryMarshal.CreateReadOnlySpan(ref ch, 1), options);
}
public override bool Equals([NotNullWhen(true)] object other)
{
CultureSpecificCharComparer cultureSpecificCharComparer = other as CultureSpecificCharComparer;
if (cultureSpecificCharComparer != null && object.Equals(comparison, cultureSpecificCharComparer.comparison))
return options == cultureSpecificCharComparer.options;
return false;
}
public override int GetHashCode()
{
HashCode hashCode = default(HashCode);
hashCode.Add(comparison);
hashCode.Add(options);
return hashCode.ToHashCode();
}
}
}