Skip to content

Commit b532e66

Browse files
committed
Revert "Remove redundant checks that cause warnings in modern .NET"
This reverts commit 5300e81.
1 parent 5300e81 commit b532e66

8 files changed

Lines changed: 94 additions & 15 deletions

File tree

src/F23.StringSimilarity/Damerau.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
namespace F23.StringSimilarity
3232
{
3333
/// <summary>
34-
/// Implementation of Damerau-Levenshtein distance with transposition (also
34+
/// Implementation of Damerau-Levenshtein distance with transposition (also
3535
/// sometimes calls unrestricted Damerau-Levenshtein distance).
3636
/// It is the minimum number of operations needed to transform one string into
3737
/// the other, where an operation is defined as an insertion, deletion, or
@@ -59,6 +59,16 @@ public double Distance(string s1, string s2)
5959
public double Distance<T>(ReadOnlySpan<T> s1, ReadOnlySpan<T> s2)
6060
where T : IEquatable<T>
6161
{
62+
if (s1 == null)
63+
{
64+
throw new ArgumentNullException(nameof(s1));
65+
}
66+
67+
if (s2 == null)
68+
{
69+
throw new ArgumentNullException(nameof(s2));
70+
}
71+
6272
if (s1.SequenceEqual(s2))
6373
{
6474
return 0;

src/F23.StringSimilarity/F23.StringSimilarity.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
<IncludeSymbols>true</IncludeSymbols>
1818
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
1919
<Version>8.0.0-alpha</Version>
20-
<LangVersion>14</LangVersion>
2120
</PropertyGroup>
2221
<PropertyGroup Label="Assembly signing" Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
2322
<DefineConstants>FEATURE_SIGNED_ASSEMBLY;$(DefineConstants)</DefineConstants>

src/F23.StringSimilarity/JaroWinkler.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ public class JaroWinkler : INormalizedStringSimilarity, INormalizedStringDistanc
4949
/// The current value of the threshold used for adding the Winkler bonus. The default value is 0.7.
5050
/// </summary>
5151
private double Threshold { get; }
52-
52+
5353
/// <summary>
5454
/// Creates a new instance with default threshold (0.7)
5555
/// </summary>
5656
public JaroWinkler()
5757
{
5858
Threshold = DEFAULT_THRESHOLD;
5959
}
60-
60+
6161
/// <summary>
6262
/// Creates a new instance with given threshold to determine when Winkler bonus should
6363
/// be used. Set threshold to a negative value to get the Jaro distance.
@@ -77,10 +77,20 @@ public JaroWinkler(double threshold)
7777
/// <exception cref="ArgumentNullException">If s1 or s2 is null.</exception>
7878
public double Similarity(string s1, string s2)
7979
=> Similarity(s1.AsSpan(), s2.AsSpan());
80-
80+
8181
public double Similarity<T>(ReadOnlySpan<T> s1, ReadOnlySpan<T> s2)
8282
where T : IEquatable<T>
8383
{
84+
if (s1 == null)
85+
{
86+
throw new ArgumentNullException(nameof(s1));
87+
}
88+
89+
if (s2 == null)
90+
{
91+
throw new ArgumentNullException(nameof(s2));
92+
}
93+
8494
if (s1.SequenceEqual(s2))
8595
{
8696
return 1f;
@@ -112,7 +122,7 @@ public double Similarity<T>(ReadOnlySpan<T> s1, ReadOnlySpan<T> s2)
112122
/// <exception cref="ArgumentNullException">If s1 or s2 is null.</exception>
113123
public double Distance(string s1, string s2)
114124
=> 1.0 - Similarity(s1, s2);
115-
125+
116126
public double Distance<T>(ReadOnlySpan<T> s1, ReadOnlySpan<T> s2)
117127
where T : IEquatable<T>
118128
=> 1.0 - Similarity(s1, s2);
@@ -192,7 +202,7 @@ private static int[] Matches<T>(ReadOnlySpan<T> s1, ReadOnlySpan<T> s2)
192202
break;
193203
}
194204
}
195-
return [matches, transpositions / 2, prefix, max.Length];
205+
return new[] { matches, transpositions / 2, prefix, max.Length };
196206
}
197207
}
198208
}

src/F23.StringSimilarity/Levenshtein.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,24 @@ public class Levenshtein : IMetricStringDistance, IMetricSpanDistance
7373
/// <exception cref="ArgumentNullException">If s1 or s2 is null.</exception>
7474
public double Distance(string s1, string s2, int limit)
7575
=> Distance(s1.AsSpan(), s2.AsSpan(), limit);
76-
76+
7777
public double Distance<T>(ReadOnlySpan<T> s1, ReadOnlySpan<T> s2)
7878
where T : IEquatable<T>
7979
=> Distance(s1, s2, int.MaxValue);
80-
80+
8181
public double Distance<T>(ReadOnlySpan<T> s1, ReadOnlySpan<T> s2, int limit)
8282
where T : IEquatable<T>
8383
{
84+
if (s1 == null)
85+
{
86+
throw new ArgumentNullException(nameof(s1));
87+
}
88+
89+
if (s2 == null)
90+
{
91+
throw new ArgumentNullException(nameof(s2));
92+
}
93+
8494
if (s1.SequenceEqual(s2))
8595
{
8696
return 0;

src/F23.StringSimilarity/LongestCommonSubsequence.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,20 @@ public class LongestCommonSubsequence : IStringDistance, ISpanDistance
5959
/// <exception cref="ArgumentNullException">If s1 or s2 is null.</exception>
6060
public double Distance(string s1, string s2)
6161
=> Distance(s1.AsSpan(), s2.AsSpan());
62-
62+
6363
public double Distance<T>(ReadOnlySpan<T> s1, ReadOnlySpan<T> s2)
6464
where T : IEquatable<T>
6565
{
66+
if (s1 == null)
67+
{
68+
throw new ArgumentNullException(nameof(s1));
69+
}
70+
71+
if (s2 == null)
72+
{
73+
throw new ArgumentNullException(nameof(s2));
74+
}
75+
6676
if (s1.SequenceEqual(s2))
6777
{
6878
return 0;
@@ -81,10 +91,20 @@ public double Distance<T>(ReadOnlySpan<T> s1, ReadOnlySpan<T> s2)
8191
/// <exception cref="ArgumentNullException">If s1 or s2 is null.</exception>
8292
public int Length(string s1, string s2)
8393
=> Length(s1.AsSpan(), s2.AsSpan());
84-
94+
8595
internal static int Length<T>(ReadOnlySpan<T> s1, ReadOnlySpan<T> s2)
8696
where T : IEquatable<T>
8797
{
98+
if (s1 == null)
99+
{
100+
throw new ArgumentNullException(nameof(s1));
101+
}
102+
103+
if (s2 == null)
104+
{
105+
throw new ArgumentNullException(nameof(s2));
106+
}
107+
88108
/* function LCSLength(X[1..m], Y[1..n])
89109
C = array(0..m, 0..n)
90110
for i := 0..m

src/F23.StringSimilarity/MetricLCS.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,20 @@ public class MetricLCS : IMetricStringDistance, INormalizedStringDistance, IMetr
4343
/// <exception cref="ArgumentNullException">If s1 or s2 is null.</exception>
4444
public double Distance(string s1, string s2)
4545
=> Distance(s1.AsSpan(), s2.AsSpan());
46-
46+
4747
public double Distance<T>(ReadOnlySpan<T> s1, ReadOnlySpan<T> s2)
4848
where T : IEquatable<T>
4949
{
50+
if (s1 == null)
51+
{
52+
throw new ArgumentNullException(nameof(s1));
53+
}
54+
55+
if (s2 == null)
56+
{
57+
throw new ArgumentNullException(nameof(s2));
58+
}
59+
5060
if (s1.SequenceEqual(s2))
5161
{
5262
return 0;

src/F23.StringSimilarity/NormalizedLevenshtein.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,20 @@ public class NormalizedLevenshtein : INormalizedStringDistance, INormalizedStrin
4444
/// <exception cref="ArgumentNullException">If s1 or s2 is null.</exception>
4545
public double Distance(string s1, string s2)
4646
=> Distance(s1.AsSpan(), s2.AsSpan());
47-
47+
4848
public double Distance<T>(ReadOnlySpan<T> s1, ReadOnlySpan<T> s2)
4949
where T : IEquatable<T>
5050
{
51+
if (s1 == null)
52+
{
53+
throw new ArgumentNullException(nameof(s1));
54+
}
55+
56+
if (s2 == null)
57+
{
58+
throw new ArgumentNullException(nameof(s2));
59+
}
60+
5161
if (s1.SequenceEqual(s2))
5262
{
5363
return 0.0;
@@ -72,7 +82,7 @@ public double Distance<T>(ReadOnlySpan<T> s1, ReadOnlySpan<T> s2)
7282
/// <exception cref="ArgumentNullException">If s1 or s2 is null.</exception>
7383
public double Similarity(string s1, string s2)
7484
=> 1.0 - Distance(s1, s2);
75-
85+
7686
public double Similarity<T>(ReadOnlySpan<T> s1, ReadOnlySpan<T> s2)
7787
where T : IEquatable<T>
7888
=> 1.0 - Distance(s1, s2);

src/F23.StringSimilarity/OptimalStringAlignment.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,20 @@ public sealed class OptimalStringAlignment : IStringDistance, ISpanDistance
4343
/// <exception cref="ArgumentNullException">If s1 or s2 is null.</exception>
4444
public double Distance(string s1, string s2)
4545
=> Distance(s1.AsSpan(), s2.AsSpan());
46-
46+
4747
public double Distance<T>(ReadOnlySpan<T> s1, ReadOnlySpan<T> s2)
4848
where T : IEquatable<T>
4949
{
50+
if (s1 == null)
51+
{
52+
throw new ArgumentNullException(nameof(s1));
53+
}
54+
55+
if (s2 == null)
56+
{
57+
throw new ArgumentNullException(nameof(s2));
58+
}
59+
5060
if (s1.SequenceEqual(s2))
5161
{
5262
return 0;

0 commit comments

Comments
 (0)