diff --git a/Source/TailBlazer.Domain/FileHandling/Search/SearchEx.cs b/Source/TailBlazer.Domain/FileHandling/Search/SearchEx.cs index 8847aef1..58670778 100644 --- a/Source/TailBlazer.Domain/FileHandling/Search/SearchEx.cs +++ b/Source/TailBlazer.Domain/FileHandling/Search/SearchEx.cs @@ -7,22 +7,15 @@ public static class SearchEx { public static Func BuildPredicate(this SearchMetadata source) { - const RegexOptions caseInsensitiveOptions = RegexOptions.IgnorePatternWhitespace - | RegexOptions.Compiled - | RegexOptions.IgnoreCase; - - const RegexOptions caseSensitiveOptions = RegexOptions.IgnorePatternWhitespace - | RegexOptions.Compiled; - Func predicate; if (!source.UseRegex) { - var stringComparison = source.IgnoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal; + var stringComparison = source.StringComparison; predicate = s => !string.IsNullOrEmpty(s) && s.Contains(source.SearchText, stringComparison); } else { - var options = source.IgnoreCase ? caseInsensitiveOptions : caseSensitiveOptions; + var options = source.RegexOptions; var regex = new Regex(source.SearchText, options); predicate = s => regex.IsMatch(s); } @@ -31,18 +24,9 @@ public static Func BuildPredicate(this SearchMetadata source) public static Optional BuildRegEx(this SearchMetadata source) { - - const RegexOptions caseInsensitiveOptions = RegexOptions.IgnorePatternWhitespace - | RegexOptions.Compiled - | RegexOptions.IgnoreCase; - - const RegexOptions caseSensitiveOptions = RegexOptions.IgnorePatternWhitespace - | RegexOptions.Compiled; - - if (source.UseRegex) { - var options = source.IgnoreCase ? caseInsensitiveOptions : caseSensitiveOptions; + var options = source.RegexOptions; return new Regex(source.SearchText, options); } return Optional.None; diff --git a/Source/TailBlazer.Domain/FileHandling/Search/SearchMetadata.cs b/Source/TailBlazer.Domain/FileHandling/Search/SearchMetadata.cs index 4cb6e16a..dc595faf 100644 --- a/Source/TailBlazer.Domain/FileHandling/Search/SearchMetadata.cs +++ b/Source/TailBlazer.Domain/FileHandling/Search/SearchMetadata.cs @@ -7,6 +7,13 @@ namespace TailBlazer.Domain.FileHandling.Search; public class SearchMetadata : IEquatable { + const RegexOptions caseInsensitiveOptions = RegexOptions.IgnorePatternWhitespace + | RegexOptions.Compiled + | RegexOptions.IgnoreCase; + + const RegexOptions caseSensitiveOptions = RegexOptions.IgnorePatternWhitespace + | RegexOptions.Compiled; + public int Position { get; } public string SearchText { get; } public bool Filter { get; } @@ -19,6 +26,9 @@ public class SearchMetadata : IEquatable public string IconKind { get; } public bool IsGlobal { get; } public bool IsExclusion { get; } + public RegexOptions RegexOptions => IgnoreCase ? caseInsensitiveOptions : caseSensitiveOptions; + public StringComparison StringComparison => IgnoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal; + public StringComparer StringComparer => IgnoreCase ? StringComparer.OrdinalIgnoreCase : StringComparer.Ordinal; public SearchMetadata([NotNull] SearchMetadata searchMetadata, int newPosition) { @@ -144,7 +154,7 @@ public bool Equals(SearchMetadata x, SearchMetadata y) if (ReferenceEquals(y, null)) return false; if (x.GetType() != y.GetType()) return false; - var stringComparison = x.IgnoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal; + var stringComparison = x.StringComparison; return string.Equals(x.SearchText, y.SearchText, stringComparison) && x.Highlight == y.Highlight && x.HighlightHue == y.HighlightHue @@ -158,7 +168,7 @@ public int GetHashCode(SearchMetadata obj) { unchecked { - var comparer = obj.IgnoreCase ? StringComparer.OrdinalIgnoreCase : StringComparer.Ordinal; + var comparer = obj.StringComparer; var hashCode = (obj.SearchText != null ? comparer.GetHashCode(obj.SearchText) : 0); hashCode = (hashCode*397) ^ obj.Highlight.GetHashCode(); hashCode = (hashCode * 397) ^ obj.HighlightHue.GetHashCode(); @@ -182,7 +192,7 @@ public bool Equals(SearchMetadata x, SearchMetadata y) if (ReferenceEquals(y, null)) return false; if (x.GetType() != y.GetType()) return false; - var stringComparison = x.IgnoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal; + var stringComparison = x.StringComparison; return string.Equals(x.SearchText, y.SearchText, stringComparison) && x.Filter == y.Filter && x.UseRegex == y.UseRegex @@ -194,7 +204,7 @@ public int GetHashCode(SearchMetadata obj) { unchecked { - var comparer = obj.IgnoreCase ? StringComparer.OrdinalIgnoreCase : StringComparer.Ordinal; + var comparer = obj.StringComparer; var hashCode = (obj.SearchText != null ? comparer.GetHashCode(obj.SearchText) : 0); hashCode = (hashCode*397) ^ obj.Filter.GetHashCode(); hashCode = (hashCode*397) ^ obj.UseRegex.GetHashCode(); diff --git a/Source/TailBlazer.Domain/Formatting/MatchedStringEnumerator.cs b/Source/TailBlazer.Domain/Formatting/MatchedStringEnumerator.cs index b8e13d6c..7bc8cd68 100644 --- a/Source/TailBlazer.Domain/Formatting/MatchedStringEnumerator.cs +++ b/Source/TailBlazer.Domain/Formatting/MatchedStringEnumerator.cs @@ -63,7 +63,7 @@ private IEnumerable Yield(string input, string tomatch) foreach (var item in split) { - if (item.Equals(tomatch, StringComparison.OrdinalIgnoreCase)) + if (item.Equals(tomatch, _tomatch.StringComparison)) { yield return new MatchedString(item, _tomatch); } diff --git a/Source/TailBlazer.Domain/Formatting/SearchMetadataEnumerator.cs b/Source/TailBlazer.Domain/Formatting/SearchMetadataEnumerator.cs index 095af796..d1d01f36 100644 --- a/Source/TailBlazer.Domain/Formatting/SearchMetadataEnumerator.cs +++ b/Source/TailBlazer.Domain/Formatting/SearchMetadataEnumerator.cs @@ -39,13 +39,6 @@ public IEnumerator GetEnumerator() } - private const RegexOptions CaseInsensitiveOptions = RegexOptions.IgnorePatternWhitespace - | RegexOptions.Compiled - | RegexOptions.IgnoreCase; - - private const RegexOptions CaseSensitiveOptions = RegexOptions.IgnorePatternWhitespace - | RegexOptions.Compiled; - private static IEnumerable Yield(string input, SearchMetadata tomatch) { @@ -109,13 +102,13 @@ private static IEnumerable Yield(string input, SearchMetadata tom private static IEnumerable Yield2(string input, SearchMetadata meta) { var tomatch = meta.SearchText; - var ignoreCase = meta.IgnoreCase; + var options = meta.RegexOptions; if (string.IsNullOrEmpty(input)) yield break; string pattern = "(" + Regex.Escape(tomatch) + ")"; - var split = Regex.Split(input, pattern, ignoreCase ? CaseInsensitiveOptions : CaseSensitiveOptions); + var split = Regex.Split(input, pattern, options); var length = split.Length; if (length == 0) yield break;