Skip to content

Commit

Permalink
Fix InMemory SearchExtension bug (#391)
Browse files Browse the repository at this point in the history
* Fixed bug for in-memory LIKE impementation.

* Cleanup.
  • Loading branch information
fiseni authored Mar 16, 2024
1 parent 8d297e5 commit 04bdf0f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace Ardalis.Specification;

Expand All @@ -11,16 +12,34 @@ public static bool Like(this string input, string pattern)
{
return SqlLike(input, pattern);
}
catch (Exception)
catch (Exception ex)
{
throw new InvalidSearchPatternException(pattern);
throw new InvalidSearchPatternException(pattern, ex);
}
}

private static bool SqlLike(this string input, string pattern)
{
// Escape special regex characters, excluding those handled separately
var regexPattern = Regex.Escape(pattern)
.Replace("%", ".*") // Translate SQL LIKE wildcard '%' to regex '.*'
.Replace("_", ".") // Translate SQL LIKE wildcard '_' to regex '.'
.Replace(@"\[", "[") // Unescape '[' as it's used for character classes/ranges
.Replace(@"\^", "^"); // Unescape '^' as it can be used for negation in character classes

// Ensure the pattern matches the entire string
regexPattern = "^" + regexPattern + "$";
var regex = new Regex(regexPattern, RegexOptions.IgnoreCase);

return regex.IsMatch(input);
}

// This C# implementation of SQL Like operator is based on the following SO post https://stackoverflow.com/a/8583383/10577116
// It covers almost all of the scenarios, and it's faster than regex based implementations.
// It may fail/throw in some very specific and edge cases, hence, wrap it in try/catch.
private static bool SqlLike(string str, string pattern)
// UPDATE: it returns incorrect results for some obvious cases.
// More details in this issue https://github.com/ardalis/Specification/issues/390
private static bool SqlLikeOption2(string str, string pattern)
{
var isMatch = true;
var isWildCardOn = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ public class SearchExtension_Like
[InlineData(false, "_Stuff_.txt_", "1Stuff.txt4")]
[InlineData(false, "_Stuff_.txt_", "1Stuff3.txt")]
[InlineData(false, "_Stuff_.txt_", "Stuff3.txt4")]
[InlineData(true, "%ab%", "ab")]
[InlineData(true, "%ab%", "abb")]
[InlineData(true, "%ab%", "aaab")]
[InlineData(true, "%ab%", "aaaab")]
[InlineData(true, "%ab%", "aaaaab")]
[InlineData(true, "%ab%", "aab")]
public void ReturnsExpectedResult_GivenPatternAndInput(bool expectedResult, string pattern, string input)
{
var result = input.Like(pattern);
Expand Down

0 comments on commit 04bdf0f

Please sign in to comment.