Skip to content

Commit 720a3a4

Browse files
committed
Slightly improve HasCommonSubstring
1 parent 0872d8c commit 720a3a4

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

SabreTools.Hashing/SpamSum/Comparisons.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,27 +105,25 @@ public static int FuzzyCompare(string? first, string? second)
105105
/// <returns>False if there is no common substring of 7 or more characters, true if there is.</returns>
106106
private static bool HasCommmonSubstring(string first, string second)
107107
{
108-
var firstLength = first.Length;
109-
var secondLength = second.Length;
110-
var largestSubstring = 0;
108+
// If either string is less than 7 characters
109+
if (first.Length < 7 || second.Length < 7)
110+
return false;
111111

112-
for (var i = 0; i < firstLength; i++)
112+
for (var i = 0; i < first.Length; i++)
113113
{
114-
for (var j = 0; j < secondLength; j++)
114+
for (var j = 0; j < second.Length; j++)
115115
{
116116
var currentIndex = 0;
117-
while ((i + currentIndex) < firstLength && (j + currentIndex) < secondLength && first[i + currentIndex] == second[j + currentIndex])
117+
while ((i + currentIndex) < first.Length && (j + currentIndex) < second.Length && first[i + currentIndex] == second[j + currentIndex])
118118
{
119119
currentIndex++;
120120
}
121121

122-
largestSubstring = Math.Max(largestSubstring, currentIndex);
122+
if (currentIndex >= 7)
123+
return true;
123124
}
124125
}
125126

126-
if (largestSubstring >= 7)
127-
return true;
128-
129127
return false;
130128
}
131129

0 commit comments

Comments
 (0)