Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix regex case sensitive #215

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 3 additions & 19 deletions Source/TailBlazer.Domain/FileHandling/Search/SearchEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,15 @@ public static class SearchEx
{
public static Func<string, bool> BuildPredicate(this SearchMetadata source)
{
const RegexOptions caseInsensitiveOptions = RegexOptions.IgnorePatternWhitespace
| RegexOptions.Compiled
| RegexOptions.IgnoreCase;

const RegexOptions caseSensitiveOptions = RegexOptions.IgnorePatternWhitespace
| RegexOptions.Compiled;

Func<string, bool> 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);
}
Expand All @@ -31,18 +24,9 @@ public static Func<string, bool> BuildPredicate(this SearchMetadata source)

public static Optional<Regex> 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<Regex>.None;
Expand Down
18 changes: 14 additions & 4 deletions Source/TailBlazer.Domain/FileHandling/Search/SearchMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ namespace TailBlazer.Domain.FileHandling.Search;

public class SearchMetadata : IEquatable<SearchMetadata>
{
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; }
Expand All @@ -19,6 +26,9 @@ public class SearchMetadata : IEquatable<SearchMetadata>
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)
{
Expand Down Expand Up @@ -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
Expand All @@ -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();
Expand All @@ -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
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private IEnumerable<MatchedString> 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);
}
Expand Down
11 changes: 2 additions & 9 deletions Source/TailBlazer.Domain/Formatting/SearchMetadataEnumerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,6 @@ public IEnumerator<MatchedString> GetEnumerator()

}

private const RegexOptions CaseInsensitiveOptions = RegexOptions.IgnorePatternWhitespace
| RegexOptions.Compiled
| RegexOptions.IgnoreCase;

private const RegexOptions CaseSensitiveOptions = RegexOptions.IgnorePatternWhitespace
| RegexOptions.Compiled;

private static IEnumerable<MatchedString> Yield(string input, SearchMetadata tomatch)
{

Expand Down Expand Up @@ -109,13 +102,13 @@ private static IEnumerable<MatchedString> Yield(string input, SearchMetadata tom
private static IEnumerable<MatchedString> 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;
Expand Down