-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ee27a6b
commit f8e6edc
Showing
3 changed files
with
125 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace LibgenDesktop.Models.Database | ||
{ | ||
internal class SearchQueryParser | ||
{ | ||
private readonly string originalSearchQuery; | ||
|
||
private SearchQueryParser(string originalSearchQuery) | ||
{ | ||
this.originalSearchQuery = originalSearchQuery; | ||
} | ||
|
||
public static string GetEscapedQuery(string originalSearchQuery) | ||
{ | ||
return new SearchQueryParser(originalSearchQuery).GetEscapedQuery(); | ||
} | ||
|
||
private string GetEscapedQuery() | ||
{ | ||
List<string> searchQueryBuilder = new List<string>(); | ||
bool isInQuotes = false; | ||
int currentIndex = 0; | ||
string currentQueryPart = String.Empty; | ||
while (currentIndex < originalSearchQuery.Length) | ||
{ | ||
char currentChar = originalSearchQuery[currentIndex]; | ||
switch (currentChar) | ||
{ | ||
case '"': | ||
if (isInQuotes) | ||
{ | ||
currentQueryPart += currentChar; | ||
isInQuotes = false; | ||
if ((currentIndex < originalSearchQuery.Length - 1) && (originalSearchQuery[currentIndex + 1] == '*')) | ||
{ | ||
currentIndex++; | ||
currentQueryPart += '*'; | ||
} | ||
if (currentQueryPart.Length > 0) | ||
{ | ||
AddSearchQueryPart(searchQueryBuilder, currentQueryPart); | ||
currentQueryPart = String.Empty; | ||
} | ||
} | ||
else | ||
{ | ||
if (currentQueryPart.Length > 0) | ||
{ | ||
AddSearchQueryPart(searchQueryBuilder, currentQueryPart); | ||
} | ||
currentQueryPart = currentChar.ToString(); | ||
isInQuotes = true; | ||
} | ||
break; | ||
case ' ': | ||
if (isInQuotes) | ||
{ | ||
currentQueryPart += currentChar; | ||
} | ||
else | ||
{ | ||
if (currentQueryPart.Length > 0) | ||
{ | ||
AddSearchQueryPart(searchQueryBuilder, currentQueryPart); | ||
currentQueryPart = String.Empty; | ||
} | ||
} | ||
break; | ||
default: | ||
currentQueryPart += currentChar; | ||
break; | ||
} | ||
currentIndex++; | ||
} | ||
if (currentQueryPart.Length > 0) | ||
{ | ||
if (isInQuotes) | ||
{ | ||
currentQueryPart = currentQueryPart.Substring(1); | ||
} | ||
foreach (string remainingQueryPart in currentQueryPart.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)) | ||
{ | ||
AddSearchQueryPart(searchQueryBuilder, remainingQueryPart); | ||
} | ||
} | ||
return String.Join(" ", searchQueryBuilder); | ||
} | ||
|
||
private void AddSearchQueryPart(List<string> searchQueryBuilder, string searchQueryPart) | ||
{ | ||
if (searchQueryPart.StartsWith("\"")) | ||
{ | ||
searchQueryBuilder.Add(searchQueryPart); | ||
} | ||
else | ||
{ | ||
switch (searchQueryPart) | ||
{ | ||
case "AND": | ||
case "OR": | ||
case "NOT": | ||
searchQueryBuilder.Add(searchQueryPart); | ||
break; | ||
default: | ||
if (searchQueryPart.Length > 1 && searchQueryPart.EndsWith("*")) | ||
{ | ||
searchQueryBuilder.Add($"\"{searchQueryPart.Substring(0, searchQueryPart.Length - 1)}\"*"); | ||
} | ||
else | ||
{ | ||
searchQueryBuilder.Add($"\"{searchQueryPart}\""); | ||
} | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
} |