Skip to content

Commit 553297b

Browse files
Update Like method
Update Like method Issue: #30
1 parent 030cf4f commit 553297b

File tree

5 files changed

+39
-15
lines changed

5 files changed

+39
-15
lines changed

Main/Source/NMemory.Lab/Form1.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public Form1()
2020
MyDatabase myDatabase = new MyDatabase();
2121

2222
myDatabase.Members.Insert(new Member() {Id = "1", Name = "abc"});
23-
var list = myDatabase.Members.Where(x => x.Name == "Abc").ToList();
23+
var list = myDatabase.Members.Where(x => Functions.Like(x.Name, "%e%")).ToList();
2424

2525
}
2626

Main/Source/NMemory.Net45/NMemory.Net45.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
<DocumentationFile>bin\Release\NMemory.xml</DocumentationFile>
3434
</PropertyGroup>
3535
<PropertyGroup>
36-
<SignAssembly>true</SignAssembly>
36+
<SignAssembly>false</SignAssembly>
3737
</PropertyGroup>
3838
<PropertyGroup>
3939
<AssemblyOriginatorKeyFile>Key.snk</AssemblyOriginatorKeyFile>

Main/Source/NMemory.Net45/Properties/AssemblyVisibility.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@
2424

2525
using System.Runtime.CompilerServices;
2626

27-
//[assembly: InternalsVisibleTo("NMemory.Test")]
28-
[assembly: InternalsVisibleTo("NMemory.Test, PublicKey=002400000480000094000000060200000024000052534131000400000100010007D1FA57C4AED9F0A32E84AA0FAEFD0DE9E8FD6AEC8F87FB03766C834C99921EB23BE79AD9D5DCC1DD9AD236132102900B723CF980957FC4E177108FC607774F29E8320E92EA05ECE4E821C0A5EFE8F1645C4C0C93C1AB99285D622CAA652C1DFAD63D745D6F2DE5F17E5EAF0FC4963D261C8A12436518206DC093344D5AD293")]
27+
[assembly: InternalsVisibleTo("NMemory.Test")]
28+
//[assembly: InternalsVisibleTo("NMemory.Test, PublicKey=002400000480000094000000060200000024000052534131000400000100010007D1FA57C4AED9F0A32E84AA0FAEFD0DE9E8FD6AEC8F87FB03766C834C99921EB23BE79AD9D5DCC1DD9AD236132102900B723CF980957FC4E177108FC607774F29E8320E92EA05ECE4E821C0A5EFE8F1645C4C0C93C1AB99285D622CAA652C1DFAD63D745D6F2DE5F17E5EAF0FC4963D261C8A12436518206DC093344D5AD293")]

Main/Source/NMemory.Shared/Functions.cs

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
// </copyright>
2323
// ----------------------------------------------------------------------------------
2424

25+
using System.Text;
26+
2527
namespace NMemory
2628
{
2729
using System.Text.RegularExpressions;
@@ -30,20 +32,42 @@ public static class Functions
3032
{
3133
public static bool Like(string input, string pattern)
3234
{
33-
// http://stackoverflow.com/questions/5663655/like-operator-in-linq-to-objects
35+
// https://stackoverflow.com/a/5663768/5619143
36+
37+
var regex = new Regex(ConvertLikeToRegex(pattern), RegexOptions.IgnoreCase);
38+
return IsRegexMatch(input, regex);
39+
}
40+
41+
internal static bool IsRegexMatch(string input, Regex regex)
42+
{
43+
if (input == null)
44+
return false;
45+
46+
return regex.IsMatch(input);
47+
}
48+
49+
internal static string ConvertLikeToRegex(string pattern)
50+
{
51+
StringBuilder builder = new StringBuilder();
52+
// Turn "off" all regular expression related syntax in the pattern string
53+
// and add regex begining of and end of line tokens so '%abc' and 'abc%' work as expected
54+
builder.Append("^").Append(Regex.Escape(pattern)).Append("$");
3455

35-
// Turn "off" all regular expression related syntax in the pattern string.
36-
pattern = Regex.Escape(pattern);
56+
/* Replace the SQL LIKE wildcard metacharacters with the
57+
* equivalent regular expression metacharacters. */
58+
builder.Replace("%", ".*").Replace("_", ".");
3759

38-
// Replace the SQL LIKE wildcard metacharacters with the equivalent regular expression metacharacters.
39-
pattern = pattern.Replace("%", ".*?").Replace("_", ".");
60+
/* The previous call to Regex.Escape actually turned off
61+
* too many metacharacters, i.e. those which are recognized by
62+
* both the regular expression engine and the SQL LIKE
63+
* statement ([...] and [^...]). Those metacharacters have
64+
* to be manually unescaped here. */
65+
builder.Replace(@"\[", "[").Replace(@"\]", "]").Replace(@"\^", "^");
4066

41-
// The previous call to Regex.Escape actually turned off too many metacharacters, i.e. those which
42-
// are recognized by both the regular expression engine and the SQL LIKE statement ([...] and [^...]).
43-
// Those metacharacters have to be manually unescaped here.
44-
pattern = pattern.Replace(@"\[", "[").Replace(@"\]", "]").Replace(@"\^", "^");
67+
// put SQL LIKE wildcard literals back
68+
builder.Replace("[.*]", "[%]").Replace("[.]", "[_]");
4569

46-
return Regex.IsMatch(input, pattern, RegexOptions.IgnoreCase);
70+
return builder.ToString();
4771
}
4872
}
4973
}

Main/Source/NMemory.Test/NMemory.Test.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
<Prefer32Bit>false</Prefer32Bit>
3838
</PropertyGroup>
3939
<PropertyGroup>
40-
<SignAssembly>true</SignAssembly>
40+
<SignAssembly>false</SignAssembly>
4141
</PropertyGroup>
4242
<PropertyGroup>
4343
<AssemblyOriginatorKeyFile>FinalPublicKey.snk</AssemblyOriginatorKeyFile>

0 commit comments

Comments
 (0)