Skip to content

Commit 713034c

Browse files
authored
Merge pull request #3119 from Jack251970/dev4
Improve windows index search with special character
2 parents 86fc68b + 5b344fb commit 713034c

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

Plugins/Flow.Launcher.Plugin.Explorer/Search/WindowsIndex/QueryConstructor.cs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
using System;
2+
using System.Text.RegularExpressions;
23
using Microsoft.Search.Interop;
34

45
namespace Flow.Launcher.Plugin.Explorer.Search.WindowsIndex
56
{
67
public class QueryConstructor
78
{
9+
private static Regex _specialCharacterMatcher = new(@"[\@\@\#\#\&\&*_;,\%\|\!\(\)\{\}\[\]\^\~\?\\""\/\:\=\-]+", RegexOptions.Compiled);
10+
private static Regex _multiWhiteSpacesMatcher = new(@"\s+", RegexOptions.Compiled);
11+
812
private Settings settings { get; }
913

1014
private const string SystemIndex = "SystemIndex";
@@ -76,8 +80,39 @@ public string FilesAndFolders(ReadOnlySpan<char> userSearchString)
7680
if (userSearchString.IsWhiteSpace())
7781
userSearchString = "*";
7882

83+
// Remove any special characters that might cause issues with the query
84+
var replacedSearchString = ReplaceSpecialCharacterWithTwoSideWhiteSpace(userSearchString);
85+
7986
// Generate SQL from constructed parameters, converting the userSearchString from AQS->WHERE clause
80-
return $"{CreateBaseQuery().GenerateSQLFromUserQuery(userSearchString.ToString())} AND {RestrictionsForAllFilesAndFoldersSearch} ORDER BY {FileName}";
87+
return $"{CreateBaseQuery().GenerateSQLFromUserQuery(replacedSearchString)} AND {RestrictionsForAllFilesAndFoldersSearch} ORDER BY {FileName}";
88+
}
89+
90+
/// <summary>
91+
/// If one special character have white space on one side, replace it with one white space.
92+
/// So command will not have "[special character]+*" which will cause OLEDB exception.
93+
/// </summary>
94+
private static string ReplaceSpecialCharacterWithTwoSideWhiteSpace(ReadOnlySpan<char> input)
95+
{
96+
const string whiteSpace = " ";
97+
98+
var inputString = input.ToString();
99+
100+
// Use regex to match special characters with whitespace on one side
101+
// and replace them with a single space
102+
var result = _specialCharacterMatcher.Replace(inputString, match =>
103+
{
104+
// Check if the match has whitespace on one side
105+
bool hasLeadingWhitespace = match.Index > 0 && char.IsWhiteSpace(inputString[match.Index - 1]);
106+
bool hasTrailingWhitespace = match.Index + match.Length < inputString.Length && char.IsWhiteSpace(inputString[match.Index + match.Length]);
107+
if (hasLeadingWhitespace || hasTrailingWhitespace)
108+
{
109+
return whiteSpace;
110+
}
111+
return match.Value;
112+
});
113+
114+
// Remove any extra spaces that might have been introduced
115+
return _multiWhiteSpacesMatcher.Replace(result, whiteSpace).Trim();
81116
}
82117

83118
///<summary>

0 commit comments

Comments
 (0)