From 720a3a4c2bcf11312ae19ef2f372765896a83eba Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Wed, 24 Sep 2025 22:35:40 -0400 Subject: [PATCH] Slightly improve HasCommonSubstring --- SabreTools.Hashing/SpamSum/Comparisons.cs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/SabreTools.Hashing/SpamSum/Comparisons.cs b/SabreTools.Hashing/SpamSum/Comparisons.cs index f2c77e2..f3322e3 100644 --- a/SabreTools.Hashing/SpamSum/Comparisons.cs +++ b/SabreTools.Hashing/SpamSum/Comparisons.cs @@ -105,27 +105,25 @@ internal static class Comparisons /// False if there is no common substring of 7 or more characters, true if there is. private static bool HasCommmonSubstring(string first, string second) { - var firstLength = first.Length; - var secondLength = second.Length; - var largestSubstring = 0; + // If either string is less than 7 characters + if (first.Length < 7 || second.Length < 7) + return false; - for (var i = 0; i < firstLength; i++) + for (var i = 0; i < first.Length; i++) { - for (var j = 0; j < secondLength; j++) + for (var j = 0; j < second.Length; j++) { var currentIndex = 0; - while ((i + currentIndex) < firstLength && (j + currentIndex) < secondLength && first[i + currentIndex] == second[j + currentIndex]) + while ((i + currentIndex) < first.Length && (j + currentIndex) < second.Length && first[i + currentIndex] == second[j + currentIndex]) { currentIndex++; } - largestSubstring = Math.Max(largestSubstring, currentIndex); + if (currentIndex >= 7) + return true; } } - if (largestSubstring >= 7) - return true; - return false; }