Skip to content

Commit

Permalink
Merge pull request #57 from celeron533/fuzzy_search
Browse files Browse the repository at this point in the history
Switch the internal match logic to Regex
  • Loading branch information
celeron533 authored Jan 26, 2024
2 parents 8900961 + 763231e commit 2c1270d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 52 deletions.
1 change: 1 addition & 0 deletions DicomGrepCore/Enums/MatchPatternEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace DicomGrepCore.Enums
public enum MatchPatternEnum
{
Normal,
Wildcard,
Regex
}
}
15 changes: 0 additions & 15 deletions DicomGrepCore/Extensions/StringExtensions.cs

This file was deleted.

65 changes: 28 additions & 37 deletions DicomGrepCore/Services/SearchService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using DicomGrepCore.Extensions;
using DicomGrepCore.Entities;
using DicomGrepCore.Entities;
using DicomGrepCore.Services.EventArgs;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -295,52 +294,44 @@ private bool CompareString(string refString, SearchCriteria _criteria, bool isDi
return false;
}

return CompareString(refString, _criteria.SearchText, _criteria.CaseSensitive, _criteria.WholeWord);
string testString = _criteria.SearchText;

}

private bool CompareString(string refString, string testString, bool caseSensitive, bool wholeWord)
{
bool result = false;
switch (criteria.MatchPattern)
{
default:
case MatchPatternEnum.Normal:
if (wholeWord)
{
result = refString.Equals(testString, caseSensitive ? StringComparison.InvariantCulture : StringComparison.InvariantCultureIgnoreCase);
}
else
{
result = refString.CaseInsensitiveContains(testString, caseSensitive ? StringComparison.InvariantCulture : StringComparison.InvariantCultureIgnoreCase);
}
testString = Regex.Escape(testString);
break;
case MatchPatternEnum.Wildcard:
testString = Regex.Escape(testString);
testString = testString.Replace(@"\*", ".*").Replace(@"\?", ".{1}");
break;
case MatchPatternEnum.Regex:
RegexOptions options = RegexOptions.Compiled;
if (!caseSensitive)
{
options |= RegexOptions.IgnoreCase;
}
break;
}

if (wholeWord)
{
// regex match start
if (!testString.StartsWith('^'))
{
testString = "^" + testString;
}
RegexOptions options = RegexOptions.Compiled | RegexOptions.Singleline;
if (!_criteria.CaseSensitive)
{
options |= RegexOptions.IgnoreCase;
}

// regex match end
if (!testString.EndsWith('$'))
{
testString += "$";
}
}
if (_criteria.WholeWord)
{
// regex match start
if (!testString.StartsWith('^'))
{
testString = "^" + testString;
}

result = Regex.IsMatch(refString, testString, options);
break;
// regex match end
if (!testString.EndsWith('$'))
{
testString += "$";
}
}
return result;

return Regex.IsMatch(refString, testString, options);
}

}
Expand Down

0 comments on commit 2c1270d

Please sign in to comment.