|
| 1 | +using Microsoft.Extensions.Primitives; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Runtime.CompilerServices; |
| 5 | + |
| 6 | +namespace Open.Text; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// A comparer for <see cref="StringSegment"/> that uses existing <see cref="StringSegment.Compare(StringSegment, StringSegment, StringComparison)"/> |
| 10 | +/// and <see cref="StringSegment.Equals(StringSegment, StringSegment, StringComparison)"/> for its comparisons |
| 11 | +/// and <see cref="TextExtensions.GetHashCodeFromChars(ReadOnlySpan{char}, StringComparison, int)"/> for hash codes. |
| 12 | +/// </summary> |
| 13 | +public class StringSegmentComparer |
| 14 | + : IComparer<StringSegment>, IEqualityComparer<StringSegment> |
| 15 | +{ |
| 16 | + /// <summary> |
| 17 | + /// A <see cref="StringSegmentComparer"/> that uses <see cref="StringComparison.Ordinal"/>. |
| 18 | + /// </summary> |
| 19 | + public static readonly StringSegmentComparer Ordinal = new(); |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// A <see cref="StringSegmentComparer"/> that uses <see cref="StringComparison.OrdinalIgnoreCase"/>. |
| 23 | + /// </summary> |
| 24 | + public static readonly StringSegmentComparer OrdinalIgnoreCase = new(StringComparison.OrdinalIgnoreCase); |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// A <see cref="StringSegmentComparer"/> that uses <see cref="StringComparison.CurrentCulture"/>. |
| 28 | + /// </summary> |
| 29 | + public static readonly StringSegmentComparer CurrentCulture = new(StringComparison.CurrentCulture); |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// A <see cref="StringSegmentComparer"/> that uses <see cref="StringComparison.CurrentCultureIgnoreCase"/>. |
| 33 | + /// </summary> |
| 34 | + public static readonly StringSegmentComparer CurrentCultureIgnoreCase = new(StringComparison.CurrentCultureIgnoreCase); |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// A <see cref="StringSegmentComparer"/> that uses <see cref="StringComparison.InvariantCulture"/>. |
| 38 | + /// </summary> |
| 39 | + public static readonly StringSegmentComparer InvariantCulture = new(StringComparison.InvariantCulture); |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// A <see cref="StringSegmentComparer"/> that uses <see cref="StringComparison.InvariantCultureIgnoreCase"/>. |
| 43 | + /// </summary> |
| 44 | + public static readonly StringSegmentComparer InvariantCultureIgnoreCase = new(StringComparison.InvariantCultureIgnoreCase); |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// Creates a new instance of <see cref="StringSegmentComparer"/> |
| 48 | + /// with the specified <paramref name="comparisonType"/> |
| 49 | + /// and <paramref name="maxHashChars"/>. |
| 50 | + /// </summary> |
| 51 | + /// <param name="comparisonType">The string comparison type.</param> |
| 52 | + /// <param name="maxHashChars"> |
| 53 | + /// The max number of characters to generate a hash from. |
| 54 | + /// See <see cref="TextExtensions.GetHashCodeFromChars(ReadOnlySpan{char}, StringComparison, int)"/> for more details. |
| 55 | + /// </param> |
| 56 | + public StringSegmentComparer(StringComparison comparisonType = StringComparison.Ordinal, int maxHashChars = int.MaxValue) |
| 57 | + { |
| 58 | + ComparisonType = comparisonType; |
| 59 | + MaxHashChars = maxHashChars; |
| 60 | + } |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// The string comparison type. |
| 64 | + /// </summary> |
| 65 | + public StringComparison ComparisonType { get; } |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// The max number of characters to generate a hash from. |
| 69 | + /// </summary> |
| 70 | + public int MaxHashChars { get; } |
| 71 | + |
| 72 | + /// <inheritdoc /> |
| 73 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 74 | + public int Compare(StringSegment x, StringSegment y) |
| 75 | + => StringSegment.Compare(x, y, ComparisonType); |
| 76 | + |
| 77 | + /// <inheritdoc /> |
| 78 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 79 | + public bool Equals(StringSegment x, StringSegment y) |
| 80 | + => StringSegment.Equals(x, y, ComparisonType); |
| 81 | + |
| 82 | + /// <inheritdoc /> |
| 83 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 84 | + public int GetHashCode(StringSegment obj) |
| 85 | + => obj.GetHashCodeFromChars(ComparisonType, MaxHashChars); |
| 86 | +} |
0 commit comments