Skip to content

Commit

Permalink
Improved Regex search performance and added handling of search options
Browse files Browse the repository at this point in the history
  • Loading branch information
Klocman committed Dec 14, 2016
1 parent 7abf7da commit d6305a2
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions MainApplication/Controls/FilterBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ private bool CheckGroupMatch(SongFileEntry entry)
break;

case ComparisonMethod.Regex:
if (regexIsValid)
compFun = src => Regex.IsMatch(src, filterText, RegexOptions.CultureInvariant);
if (_compiledRegex != null)
compFun = src => _compiledRegex.IsMatch(src);
else
compFun = src => false;
break;
Expand Down Expand Up @@ -176,25 +176,39 @@ private void groupFilterComboBox_SelectedIndexChanged(object sender, EventArgs e

public event EventHandler FilterChanged;

private bool regexIsValid = true;
private Regex _compiledRegex;

private void OnFilterChanged(object sender, EventArgs eventArgs)
{
if (SelectedComparisonMethod == ComparisonMethod.Regex)
{
try
{
new Regex(searchBox1.SearchString);
regexIsValid = true;
// Compile the Regex expression now to check if it's valid.
_compiledRegex = new Regex(searchBox1.SearchString, GetRegexOptions());
}
catch (ArgumentException)
{
regexIsValid = false;
_compiledRegex = null;
}
}
FilterChanged?.Invoke(sender, eventArgs);
}

private RegexOptions GetRegexOptions()
{
// TODO Check if there are enough entries to search through to warrant compiling?
var options = RegexOptions.Compiled;

if (!checkBoxExact.Checked)
options |= RegexOptions.IgnoreCase | RegexOptions.CultureInvariant;

// Names and comments are all single line.
options |= searchInsideFilesCheckBox.Checked ? RegexOptions.Multiline : RegexOptions.Singleline;

return options;
}

private void searchBox1_SearchTextChanged(SearchBox arg1, EventArgs arg2)
{
//TODO Reposition and enable the clear button?
Expand Down

0 comments on commit d6305a2

Please sign in to comment.