• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

loresoft / FluentCommand / 36888666826

01 Oct 2026 04:00PM UTC coverage: 61.179% (-4.7%) from 65.903%
36888666826

push

github

pwelter34
Improve MERGE SQL generation and literals

1624 of 3516 branches covered (46.19%)

Branch coverage included in aggregate %.

49 of 70 new or added lines in 1 file covered. (70.0%)

373 existing lines in 32 files now uncovered.

5422 of 8001 relevant lines covered (67.77%)

292.72 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

42.42
/src/FluentCommand/Internal/HashCode.cs
1
namespace FluentCommand.Internal;
2

3
/// <summary>
4
/// Represents an immutable structure for combining and generating hash codes.
5
/// </summary>
6
/// <remarks>
7
/// Implements the Jon Skeet recommended pattern for <see cref="object.GetHashCode()"/>.
8
/// Provides deterministic and composable hash code generation for complex objects.
9
/// </remarks>
10
public readonly struct HashCode : IFormattable, IEquatable<HashCode>
11
{
12
    /// <summary>
13
    /// The prime multiplier used to combine hash codes.
14
    /// </summary>
15
    public const int Multiplier = 31;
16

17
    /// <summary>
18
    /// The default seed value used for initializing random number generators or hash functions.
19
    /// </summary>
20
    public const int DefaultSeed = 17;
21

22
    private readonly int _hashCode;
23

24
    /// <summary>
25
    /// Initializes a new instance of the <see cref="HashCode"/> struct with the specified hash code value.
26
    /// </summary>
27
    /// <param name="hashCode">The initial hash code value.</param>
28
    public HashCode(int hashCode)
29
    {
30
        _hashCode = hashCode;
54 ✔
31
    }
54 ✔
32

33
    /// <summary>
34
    /// Gets the initial seed value for combining hash codes.
35
    /// </summary>
36
    /// <value>
37
    /// The initial hash code seed value.
38
    /// </value>
39
    public static HashCode Seed => new(DefaultSeed);
6 ✔
40

41
    /// <summary>
42
    /// Combines this hash code with the hash code of the specified value.
43
    /// </summary>
44
    /// <typeparam name="TValue">The type of the value to combine.</typeparam>
45
    /// <param name="value">The value whose hash code will be combined.</param>
46
    /// <returns>
47
    /// A new <see cref="HashCode"/> instance representing the combined hash code.
48
    /// </returns>
49
    public HashCode Combine<TValue>(TValue? value)
50
    {
51
        var hashCode = value is null ? 0 : EqualityComparer<TValue>.Default.GetHashCode(value);
22 !
52
        unchecked
53
        {
54
            hashCode = (_hashCode * Multiplier) + hashCode;
22 ✔
55
        }
56

57
        return new HashCode(hashCode);
22 ✔
58
    }
59

60
    /// <summary>
61
    /// Combines this hash code with the deterministic hash code of the specified string value.
62
    /// </summary>
63
    /// <param name="value">The string value whose hash code will be combined.</param>
64
    /// <returns>
65
    /// A new <see cref="HashCode"/> instance representing the combined hash code.
66
    /// </returns>
67
    public HashCode Combine(string? value)
68
    {
69
        // need to handle string values deterministically
70
        var hashCode = HashString(value);
26 ✔
71
        unchecked
72
        {
73
            hashCode = (_hashCode * Multiplier) + hashCode;
26 ✔
74
        }
75

76
        return new HashCode(hashCode);
26 ✔
77
    }
78

79
    /// <summary>
80
    /// Combines this hash code with the hash code of the specified object.
81
    /// If the object is a string, uses a deterministic string hash; otherwise, uses the object's hash code.
82
    /// </summary>
83
    /// <param name="value">The object whose hash code will be combined.</param>
84
    /// <returns>
85
    /// A new <see cref="HashCode"/> instance representing the combined hash code.
86
    /// </returns>
87
    public HashCode Combine(object? value)
88
    {
89
        // need to handle string values deterministically
90
        return value switch
10 ✔
91
        {
10 ✔
92
            string text => Combine(text),
4 ✔
93
            _ => Combine(value?.GetHashCode() ?? 0),
6 !
94
        };
10 ✔
95
    }
96

97
    /// <summary>
98
    /// Combines this hash code with the hash codes of all items in the specified sequence.
99
    /// </summary>
100
    /// <typeparam name="TValue">The type of the values to combine.</typeparam>
101
    /// <param name="values">The sequence of values whose hash codes will be combined.</param>
102
    /// <returns>
103
    /// A new <see cref="HashCode"/> instance representing the combined hash code.
104
    /// </returns>
105
    public HashCode CombineAll<TValue>(IEnumerable<TValue> values)
106
    {
UNCOV
107
        if (values == null)
×
108
            return this;
×
109

UNCOV
110
        var comparer = EqualityComparer<TValue>.Default;
×
UNCOV
111
        var current = _hashCode;
×
112

UNCOV
113
        foreach (var value in values)
×
114
        {
UNCOV
115
            var hashCode = value switch
×
UNCOV
116
            {
×
UNCOV
117
                string text => HashString(text),
×
UNCOV
118
                TValue instance => comparer.GetHashCode(instance),
×
119
                _ => 0
×
UNCOV
120
            };
×
121

122
            unchecked
123
            {
UNCOV
124
                hashCode = (current * Multiplier) + hashCode;
×
125
            }
126

UNCOV
127
            current = hashCode;
×
128
        }
129

UNCOV
130
        return new HashCode(current);
×
131
    }
132

133
    /// <summary>
134
    /// Returns the hash code value for this instance.
135
    /// </summary>
136
    /// <returns>
137
    /// The hash code value for this instance.
138
    /// </returns>
UNCOV
139
    public override int GetHashCode() => _hashCode;
×
140

141
    /// <summary>
142
    /// Converts the numeric value of this instance to its equivalent string representation.
143
    /// </summary>
144
    /// <returns>
145
    /// The string representation of the hash code value.
146
    /// </returns>
UNCOV
147
    public override string ToString() => _hashCode.ToString();
×
148

149
    /// <summary>
150
    /// Converts the numeric value of this instance to its equivalent string representation using the specified culture-specific format information.
151
    /// </summary>
152
    /// <param name="provider">An object that supplies culture-specific formatting information.</param>
153
    /// <returns>
154
    /// The string representation of the hash code value as specified by <paramref name="provider"/>.
155
    /// </returns>
UNCOV
156
    public string ToString(IFormatProvider? provider) => _hashCode.ToString(provider);
×
157

158
    /// <summary>
159
    /// Converts the numeric value of this instance to its equivalent string representation using the specified format.
160
    /// </summary>
161
    /// <param name="format">A standard or custom numeric format string.</param>
162
    /// <returns>
163
    /// The string representation of the hash code value as specified by <paramref name="format"/>.
164
    /// </returns>
UNCOV
165
    public string ToString(string format) => _hashCode.ToString(format);
×
166

167
    /// <summary>
168
    /// Converts the numeric value of this instance to its equivalent string representation using the specified format and culture-specific format information.
169
    /// </summary>
170
    /// <param name="format">A standard or custom numeric format string.</param>
171
    /// <param name="provider">An object that supplies culture-specific formatting information.</param>
172
    /// <returns>
173
    /// The string representation of the hash code value as specified by <paramref name="format"/> and <paramref name="provider"/>.
174
    /// </returns>
175
    public string ToString(string? format, IFormatProvider? provider) => _hashCode.ToString(format, provider);
6 ✔
176

177
    /// <summary>
178
    /// Determines whether the specified object is equal to this instance.
179
    /// </summary>
180
    /// <param name="obj">The object to compare with this instance.</param>
181
    /// <returns>
182
    /// <c>true</c> if the specified object is a <see cref="HashCode"/> and is equal to this instance; otherwise, <c>false</c>.
183
    /// </returns>
UNCOV
184
    public override bool Equals(object? obj) => obj is HashCode code && Equals(code);
×
185

186
    /// <summary>
187
    /// Indicates whether the current object is equal to another <see cref="HashCode"/> instance.
188
    /// </summary>
189
    /// <param name="other">A <see cref="HashCode"/> instance to compare with this instance.</param>
190
    /// <returns>
191
    /// <c>true</c> if the current object is equal to the <paramref name="other"/> parameter; otherwise, <c>false</c>.
192
    /// </returns>
UNCOV
193
    public bool Equals(HashCode other) => _hashCode == other._hashCode;
×
194

195
    /// <summary>
196
    /// Performs an implicit conversion from <see cref="HashCode"/> to <see cref="int"/>.
197
    /// </summary>
198
    /// <param name="hashCode">The <see cref="HashCode"/> instance to convert.</param>
199
    /// <returns>
200
    /// The integer value of the hash code.
201
    /// </returns>
UNCOV
202
    public static implicit operator int(HashCode hashCode) => hashCode._hashCode;
×
203

204
    /// <summary>
205
    /// Determines whether two <see cref="HashCode"/> instances are equal.
206
    /// </summary>
207
    /// <param name="left">The first <see cref="HashCode"/> to compare.</param>
208
    /// <param name="right">The second <see cref="HashCode"/> to compare.</param>
209
    /// <returns>
210
    /// <c>true</c> if <paramref name="left"/> is equal to <paramref name="right"/>; otherwise, <c>false</c>.
211
    /// </returns>
UNCOV
212
    public static bool operator ==(HashCode left, HashCode right) => left.Equals(right);
×
213

214
    /// <summary>
215
    /// Determines whether two <see cref="HashCode"/> instances are not equal.
216
    /// </summary>
217
    /// <param name="left">The first <see cref="HashCode"/> to compare.</param>
218
    /// <param name="right">The second <see cref="HashCode"/> to compare.</param>
219
    /// <returns>
220
    /// <c>true</c> if <paramref name="left"/> is not equal to <paramref name="right"/>; otherwise, <c>false</c>.
221
    /// </returns>
UNCOV
222
    public static bool operator !=(HashCode left, HashCode right) => !(left == right);
×
223

224
    /// <summary>
225
    /// Computes a deterministic hash code for the specified string.
226
    /// </summary>
227
    /// <param name="text">The string to hash.</param>
228
    /// <returns>
229
    /// A 32-bit signed integer hash code for the string, or 0 if the string is null or empty.
230
    /// </returns>
231
    public static int HashString(string? text)
232
    {
233
        if (text == null || string.IsNullOrEmpty(text))
26 !
UNCOV
234
            return 0;
×
235

236
        int hash = DefaultSeed;
26 ✔
237

238
        unchecked
239
        {
240
            // ReSharper disable once LoopCanBeConvertedToQuery
241
            // ReSharper disable once ForCanBeConvertedToForeach
242
            for (var index = 0; index < text.Length; index++)
1,948 ✔
243
                hash = (hash * Multiplier) + text[index];
948 ✔
244

245
        }
246

247
        return hash;
26 ✔
248
    }
249
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc