Skip to content
Merged
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
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