|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | + |
| 4 | +namespace Open.Text |
| 5 | +{ |
| 6 | + public struct StringComparable : IEquatable<StringComparable>, IEquatable<string> |
| 7 | + { |
| 8 | + public StringComparable(string source, StringComparison type) |
| 9 | + { |
| 10 | + Source = source ?? throw new ArgumentNullException(nameof(source)); |
| 11 | + Type = type; |
| 12 | + } |
| 13 | + |
| 14 | + public string Source { get; } |
| 15 | + public StringComparison Type { get; } |
| 16 | + |
| 17 | + public int Length => Source.Length; |
| 18 | + |
| 19 | + public bool Equals(string? other) |
| 20 | + => Source.Equals(other, Type); |
| 21 | + |
| 22 | + public bool Equals(ReadOnlySpan<char> other) |
| 23 | + => Source.Equals(other, Type); |
| 24 | + |
| 25 | + public bool Equals(StringComparable other) |
| 26 | + => Source.Equals(other.Source, Type); |
| 27 | + |
| 28 | + public override bool Equals(object obj) |
| 29 | + { |
| 30 | + return obj switch |
| 31 | + { |
| 32 | + StringComparable sc => Equals(sc), |
| 33 | + string s => Equals(s), |
| 34 | + _ => false |
| 35 | + }; |
| 36 | + } |
| 37 | + |
| 38 | + public override int GetHashCode() |
| 39 | + { |
| 40 | + int hashCode = 141257509; |
| 41 | + hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(Source); |
| 42 | + hashCode = hashCode * -1521134295 + Type.GetHashCode(); |
| 43 | + return hashCode; |
| 44 | + } |
| 45 | + |
| 46 | + public static bool operator ==(StringComparable a, StringComparable? b) => a.Equals(b); |
| 47 | + public static bool operator !=(StringComparable a, StringComparable? b) => !a.Equals(b); |
| 48 | + |
| 49 | + public static bool operator ==(StringComparable a, string? b) => a.Equals(b); |
| 50 | + public static bool operator !=(StringComparable a, string? b) => !a.Equals(b); |
| 51 | + |
| 52 | + public static bool operator ==(StringComparable a, ReadOnlySpan<char> b) => a.Equals(b); |
| 53 | + public static bool operator !=(StringComparable a, ReadOnlySpan<char> b) => !a.Equals(b); |
| 54 | + |
| 55 | + } |
| 56 | + |
| 57 | + public static class StringComparableExtensions |
| 58 | + { |
| 59 | + /// <inheritdoc cref="AsCaseInsensitive(string)"/> |
| 60 | + /// <summary> |
| 61 | + /// Prepares a string for a specific StringComparison. |
| 62 | + /// </summary> |
| 63 | + /// <param name="type">The type of StrinComparison to perform.</param> |
| 64 | + public static StringComparable AsComparable(this string source, StringComparison type) |
| 65 | + => new(source, type); |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// Prepares a string to be case insensitive when comparing equality. |
| 69 | + /// </summary> |
| 70 | + /// <param name="source">The source string.</param> |
| 71 | + /// <returns>A StringComparable that can be compared (== or !=) against other StringComparables, strings, and ReadOnlySpan<char>.</returns> |
| 72 | + public static StringComparable AsCaseInsensitive(this string source) |
| 73 | + => new(source, StringComparison.OrdinalIgnoreCase); |
| 74 | + |
| 75 | + /// <inheritdoc cref="AsCaseInsensitive(string)"/> |
| 76 | + /// <summary> |
| 77 | + /// Prepares a string to be invariant culture and case insensitive when comparing equality. |
| 78 | + /// </summary> |
| 79 | + public static StringComparable AsCaseInsensitiveInvariantCulture(this string source) |
| 80 | + => new(source, StringComparison.InvariantCultureIgnoreCase); |
| 81 | + |
| 82 | + /// <inheritdoc cref="AsCaseInsensitive(string)"/> |
| 83 | + /// <summary> |
| 84 | + /// Prepares a string to be invariant culture and case insensitive when comparing equality. |
| 85 | + /// </summary> |
| 86 | + public static StringComparable AsCaseInsensitiveCurrentCulture(this string source) |
| 87 | + => new(source, StringComparison.CurrentCultureIgnoreCase); |
| 88 | + |
| 89 | + /// <inheritdoc cref="AsCaseInsensitive(string)"/> |
| 90 | + /// <summary> |
| 91 | + /// Prepares a string to be current culture and case sensitive when comparing equality. |
| 92 | + /// </summary> |
| 93 | + public static StringComparable AsCurrentCulture(this string source) |
| 94 | + => new(source, StringComparison.CurrentCulture); |
| 95 | + |
| 96 | + /// <inheritdoc cref="AsCaseInsensitive(string)"/> |
| 97 | + /// <summary> |
| 98 | + /// Prepares a string to be invariant culture and case sensitive when comparing equality. |
| 99 | + /// </summary> |
| 100 | + public static StringComparable AsInvariantCulture(this string source) |
| 101 | + => new(source, StringComparison.InvariantCulture); |
| 102 | + |
| 103 | + } |
| 104 | +} |
0 commit comments