< Summary

Information
Class: MoreStructures.Utilities.CharOrTerminatorComparer
Assembly: MoreStructures
File(s): /home/runner/work/MoreStructures/MoreStructures/MoreStructures/Utilities/CharOrTerminatorComparer.cs
Line coverage
100%
Covered lines: 18
Uncovered lines: 0
Coverable lines: 18
Total lines: 69
Line coverage: 100%
Branch coverage
100%
Covered branches: 10
Total branches: 10
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
get_Terminator()100%1100%
.ctor(...)100%1100%
.cctor()100%1100%
Build(...)100%1100%
Compare(...)100%8100%
Equals(...)100%2100%
GetHashCode()100%1100%

File(s)

/home/runner/work/MoreStructures/MoreStructures/MoreStructures/Utilities/CharOrTerminatorComparer.cs

#LineLine coverage
 1using System.Collections.Concurrent;
 2
 3namespace MoreStructures.Utilities;
 4
 5/// <summary>
 6/// An implementation of <see cref="IComparer{T}"/> for <see cref="char"/>, which compares chars taking into account
 7/// the fact that the char may be a special char, called terminator, which has to be considered smaller than any other
 8/// char.
 9/// </summary>
 10public class CharOrTerminatorComparer : IComparer<char>
 11{
 12    /// <summary>
 13    /// The character acting as terminator, and which has to be considered smaller than any other char.
 14    /// </summary>
 110391015    public char Terminator { get; }
 16
 417    private CharOrTerminatorComparer(char terminator)
 418    {
 419        Terminator = terminator;
 420    }
 21
 122    private static readonly ConcurrentDictionary<char, CharOrTerminatorComparer> _instances = new();
 23
 24    /// <summary>
 25    /// Builds a <see cref="CharOrTerminatorComparer"/> with the provided <paramref name="terminator"/>. Caches
 26    /// instances.
 27    /// </summary>
 28    /// <param name="terminator"><inheritdoc cref="Terminator" path="/summary"/></param>
 29    /// <returns>An instance of <see cref="CharOrTerminatorComparer"/>, new or previously created and cached.</returns>
 30    public static CharOrTerminatorComparer Build(char terminator)
 1155331    {
 1155732        return _instances.GetOrAdd(terminator, t => new CharOrTerminatorComparer(t));
 1155333    }
 34
 35    /// <inheritdoc path="//*[not(self::summary or self::remarks)]"/>
 36    /// <summary>
 37    ///     <inheritdoc/>
 38    /// </summary>
 39    /// <remarks>
 40    ///     Special rules applied by <see cref="CharOrTerminatorComparer"/>:
 41    ///     <br/>
 42    ///     - If one char is the terminator, and the other isn't, the one which is the terminator is smaller.
 43    ///       <br/>
 44    ///     - If none of the cases above applies, <see cref="char.CompareTo(char)"/> is used on <paramref name="x"/>
 45    ///       and <paramref name="y"/>.
 46    /// </remarks>
 47    public int Compare(char x, char y)
 37816948    {
 37816949        if (x == Terminator && y != Terminator)
 3066050            return -1;
 34750951        if (x != Terminator && y == Terminator)
 539252            return 1;
 34211753        return x.CompareTo(y);
 37816954    }
 55
 56    /// <inheritdoc path="//*[not(self::remarks)]"/>
 57    /// <remarks>
 58    /// Two <see cref="CharOrTerminatorComparer"/> are equal if they have the same <see cref="Terminator"/>.
 59    /// </remarks>
 60    public override bool Equals(object? obj) =>
 3061        obj is CharOrTerminatorComparer other && other.Terminator == Terminator;
 62
 63    /// <inheritdoc path="//*[not(self::remarks)]"/>
 64    /// <remarks>
 65    /// The <see cref="GetHashCode"/> is based on <see cref="Terminator"/> hash.
 66    /// </remarks>
 67    public override int GetHashCode() =>
 468        Terminator.GetHashCode();
 69}