diff --git a/src/F23.StringSimilarity/F23.StringSimilarity.csproj b/src/F23.StringSimilarity/F23.StringSimilarity.csproj index cd57f41..7115879 100644 --- a/src/F23.StringSimilarity/F23.StringSimilarity.csproj +++ b/src/F23.StringSimilarity/F23.StringSimilarity.csproj @@ -1,9 +1,7 @@ - - + netstandard2.0 F23.StringSimilarity - 5.0.0 string;similarity;distance;levenshtein;jaro-winkler;lcs;cosine StringSimilarity.NET James Blair, Paul Irwin @@ -18,18 +16,16 @@ true true snupkg + 5.1.0 - + - - bin\Release\netstandard2.0\F23.StringSimilarity.xml - \ No newline at end of file diff --git a/src/F23.StringSimilarity/RatcliffObershelp.cs b/src/F23.StringSimilarity/RatcliffObershelp.cs index 7dd2ba1..4a59af4 100644 --- a/src/F23.StringSimilarity/RatcliffObershelp.cs +++ b/src/F23.StringSimilarity/RatcliffObershelp.cs @@ -45,7 +45,7 @@ public double Similarity(string s1, string s2) return 1.0d; } - var matches = GetMatchList(s1, s2); + var matches = GetMatchList(s1.AsSpan(), s2.AsSpan()); int sumOfMatches = 0; foreach (var match in matches) @@ -68,22 +68,22 @@ public double Distance(string s1, string s2) return 1.0d - Similarity(s1, s2); } - private static IList GetMatchList(string s1, string s2) + private static IList GetMatchList(ReadOnlySpan s1, ReadOnlySpan s2) { var list = new List(); var match = FrontMaxMatch(s1, s2); if (match.Length > 0) { - var frontSource = s1.Substring(0, s1.IndexOf(match, StringComparison.Ordinal)); - var frontTarget = s2.Substring(0, s2.IndexOf(match, StringComparison.Ordinal)); + var frontSource = s1.Slice(0, s1.IndexOf(match, StringComparison.Ordinal)); + var frontTarget = s2.Slice(0, s2.IndexOf(match, StringComparison.Ordinal)); var frontQueue = GetMatchList(frontSource, frontTarget); - var endSource = s1.Substring(s1.IndexOf(match, StringComparison.Ordinal) + match.Length); - var endTarget = s2.Substring(s2.IndexOf(match, StringComparison.Ordinal) + match.Length); + var endSource = s1.Slice(s1.IndexOf(match, StringComparison.Ordinal) + match.Length); + var endTarget = s2.Slice(s2.IndexOf(match, StringComparison.Ordinal) + match.Length); var endQueue = GetMatchList(endSource, endTarget); - list.Add(match); + list.Add(match.ToString()); list.AddRange(frontQueue); list.AddRange(endQueue); } @@ -91,17 +91,17 @@ private static IList GetMatchList(string s1, string s2) return list; } - private static string FrontMaxMatch(string s1, string s2) + private static ReadOnlySpan FrontMaxMatch(ReadOnlySpan s1, ReadOnlySpan s2) { int longest = 0; - var longestSubstring = ""; + ReadOnlySpan longestSubstring = ReadOnlySpan.Empty; for (int i = 0; i < s1.Length; ++i) { for (int j = i + 1; j <= s1.Length; ++j) { - var substring = s1.Substring(i, j - i); - if (s2.Contains(substring) && substring.Length > longest) + var substring = s1.Slice(i, j - i); + if (s2.Contains(substring, StringComparison.Ordinal) && substring.Length > longest) { longest = substring.Length; longestSubstring = substring;