| | | 1 | | using System.Collections.Concurrent; |
| | | 2 | | |
| | | 3 | | namespace MoreStructures.Utilities; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// An implementation of <see cref="IComparer{T}"/> for <see cref="string"/>, which compares string taking into account |
| | | 7 | | /// the fact that the string may contain (at any index, not necessarily at then end) a special char, called terminator, |
| | | 8 | | /// which has to be considered smaller than any other char. |
| | | 9 | | /// </summary> |
| | | 10 | | public class StringIncludingTerminatorComparer : IComparer<string> |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// The character acting as terminator, and which has to be considered smaller than any other char. |
| | | 14 | | /// </summary> |
| | 91560 | 15 | | public char Terminator { get; } |
| | | 16 | | |
| | 7 | 17 | | private StringIncludingTerminatorComparer(char terminator) |
| | 7 | 18 | | { |
| | 7 | 19 | | Terminator = terminator; |
| | 7 | 20 | | } |
| | | 21 | | |
| | 1 | 22 | | private static readonly ConcurrentDictionary<char, StringIncludingTerminatorComparer> _instances = new(); |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Builds a <see cref="StringIncludingTerminatorComparer"/> with the provided <paramref name="terminator"/>. |
| | | 26 | | /// Caches instances. |
| | | 27 | | /// </summary> |
| | | 28 | | /// <param name="terminator"><inheritdoc cref="Terminator" path="/summary"/></param> |
| | | 29 | | /// <returns> |
| | | 30 | | /// An instance of <see cref="StringIncludingTerminatorComparer"/>, new or previously created and cached. |
| | | 31 | | /// </returns> |
| | | 32 | | public static StringIncludingTerminatorComparer Build(char terminator) |
| | 130 | 33 | | { |
| | 137 | 34 | | return _instances.GetOrAdd(terminator, t => new StringIncludingTerminatorComparer(t)); |
| | 130 | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <inheritdoc path="//*[not(self::summary or self::remarks)]"/> |
| | | 38 | | /// <summary> |
| | | 39 | | /// <inheritdoc/> |
| | | 40 | | /// </summary> |
| | | 41 | | /// <remarks> |
| | | 42 | | /// Special rules applied by <see cref="StringIncludingTerminatorComparer"/>: |
| | | 43 | | /// <br/> |
| | | 44 | | /// - If either string is null or empty, the standard <see cref="string.Compare(string?, string?)"/> is used. |
| | | 45 | | /// <br/> |
| | | 46 | | /// - If one string starts with the terminator, and the other doesn't, the one which does is smaller. |
| | | 47 | | /// <br/> |
| | | 48 | | /// - If none of the cases above applies, <see cref="string.Compare(string?, string?)"/> is used on the |
| | | 49 | | /// substring starting from index 1 of each of the strings <paramref name="x"/> and <paramref name="y"/>. |
| | | 50 | | /// </remarks> |
| | | 51 | | public int Compare(string? x, string? y) |
| | 31867 | 52 | | { |
| | 31867 | 53 | | if (x == null || y == null || x.Length == 0 || y.Length == 0) |
| | 261 | 54 | | return string.Compare(x, y); |
| | | 55 | | |
| | 31606 | 56 | | if (x[0] == Terminator && y[0] != Terminator) |
| | 3259 | 57 | | return -1; |
| | | 58 | | |
| | 28347 | 59 | | if (x[0] != Terminator && y[0] == Terminator) |
| | 359 | 60 | | return 1; |
| | | 61 | | |
| | 27988 | 62 | | var firstCharDifference = x[0] - y[0]; |
| | 27988 | 63 | | if (firstCharDifference != 0) |
| | 22595 | 64 | | return firstCharDifference; |
| | | 65 | | |
| | 5393 | 66 | | return Compare(x[1..], y[1..]); |
| | 31867 | 67 | | } |
| | | 68 | | } |