File: Infrastructure\StringArrayComparer.cs
Web Access
Project: src\aspnetcore\src\Mvc\Mvc.Core\src\Microsoft.AspNetCore.Mvc.Core.csproj (Microsoft.AspNetCore.Mvc.Core)
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.AspNetCore.Mvc.Infrastructure;

internal sealed class StringArrayComparer : IEqualityComparer<string[]>
{
    public static readonly StringArrayComparer Ordinal = new StringArrayComparer(StringComparer.Ordinal);

    public static readonly StringArrayComparer OrdinalIgnoreCase = new StringArrayComparer(StringComparer.OrdinalIgnoreCase);

    private readonly StringComparer _valueComparer;

    private StringArrayComparer(StringComparer valueComparer)
    {
        _valueComparer = valueComparer;
    }

    public bool Equals(string[]? x, string[]? y)
    {
        if (object.ReferenceEquals(x, y))
        {
            return true;
        }

        if (x == null && y == null)
        {
            return true;
        }

        if (x == null || y == null)
        {
            return false;
        }

        if (x.Length != y.Length)
        {
            return false;
        }

        for (var i = 0; i < x.Length; i++)
        {
            if (string.IsNullOrEmpty(x[i]) && string.IsNullOrEmpty(y[i]))
            {
                continue;
            }

            if (!_valueComparer.Equals(x[i], y[i]))
            {
                return false;
            }
        }

        return true;
    }

    public int GetHashCode(string[] obj)
    {
        if (obj == null)
        {
            return 0;
        }

        var hash = new HashCode();
        for (var i = 0; i < obj.Length; i++)
        {
            // Route values define null and "" to be equivalent.
            hash.Add(obj[i] ?? string.Empty, _valueComparer);
        }

        return hash.ToHashCode();
    }
}