|
|
|
@ -2,6 +2,7 @@
@@ -2,6 +2,7 @@
|
|
|
|
|
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
|
|
|
|
|
|
|
|
|
|
using System; |
|
|
|
|
using System.Text; |
|
|
|
|
using System.Text.RegularExpressions; |
|
|
|
|
using System.Windows.Controls; |
|
|
|
|
|
|
|
|
@ -15,15 +16,23 @@ namespace ICSharpCode.AvalonEdit.Search
@@ -15,15 +16,23 @@ namespace ICSharpCode.AvalonEdit.Search
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a default ISearchStrategy with the given parameters.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static ISearchStrategy Create(string searchPattern, bool ignoreCase, bool useRegularExpressions, bool matchWholeWords) |
|
|
|
|
public static ISearchStrategy Create(string searchPattern, bool ignoreCase, bool matchWholeWords, SearchType searchType) |
|
|
|
|
{ |
|
|
|
|
if (searchPattern == null) |
|
|
|
|
throw new ArgumentNullException("searchPattern"); |
|
|
|
|
RegexOptions options = RegexOptions.Compiled | RegexOptions.Multiline; |
|
|
|
|
if (ignoreCase) |
|
|
|
|
options |= RegexOptions.IgnoreCase; |
|
|
|
|
if (!useRegularExpressions) |
|
|
|
|
searchPattern = Regex.Escape(searchPattern); |
|
|
|
|
|
|
|
|
|
switch (searchType) { |
|
|
|
|
case SearchType.Normal: |
|
|
|
|
searchPattern = Regex.Escape(searchPattern); |
|
|
|
|
break; |
|
|
|
|
case SearchType.Wildcard: |
|
|
|
|
searchPattern = ConvertWildcardsToRegex(searchPattern); |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (matchWholeWords) |
|
|
|
|
searchPattern = "\\b" + searchPattern + "\\b"; |
|
|
|
|
try { |
|
|
|
@ -33,5 +42,29 @@ namespace ICSharpCode.AvalonEdit.Search
@@ -33,5 +42,29 @@ namespace ICSharpCode.AvalonEdit.Search
|
|
|
|
|
throw new SearchPatternException(ex.Message, ex); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static string ConvertWildcardsToRegex(string searchPattern) |
|
|
|
|
{ |
|
|
|
|
if (string.IsNullOrEmpty(searchPattern)) |
|
|
|
|
return ""; |
|
|
|
|
|
|
|
|
|
StringBuilder builder = new StringBuilder(); |
|
|
|
|
|
|
|
|
|
foreach (char ch in searchPattern) { |
|
|
|
|
switch (ch) { |
|
|
|
|
case '?': |
|
|
|
|
builder.Append("."); |
|
|
|
|
break; |
|
|
|
|
case '*': |
|
|
|
|
builder.Append(".*"); |
|
|
|
|
break; |
|
|
|
|
default: |
|
|
|
|
builder.Append(Regex.Escape(ch.ToString())); |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return builder.ToString(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|