Skip to content

Commit

Permalink
[api] Clean up bitap and fix searching with ' ', '-', and '+'
Browse files Browse the repository at this point in the history
  • Loading branch information
da3dsoul committed Feb 8, 2017
1 parent 7238593 commit 19c9863
Showing 1 changed file with 11 additions and 39 deletions.
50 changes: 11 additions & 39 deletions JMMServer/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,29 +167,8 @@ public static int LevenshteinDistance(string s, string t)
public static string FilterCharacters(this string value, char[] allowed, bool blacklist = false)
{
StringBuilder sb = new StringBuilder(value);
int start = 0;
while (start < sb.Length)
{
if (blacklist ^ !allowed.Contains(sb[start]))
start++;
else
break;
}
if (start == sb.Length)
{
sb.Length = 0;
return "";
}
int end = sb.Length - 1;
while (end >= 0)
{
if (blacklist ^ !allowed.Contains(sb[end]))
end--;
else
break;
}
int dest = 0;
for (int i = start; i <= end; i++)
for (int i = 0; i <= sb.Length-1; i++)
{
if (blacklist ^ allowed.Contains(sb[i]))
{
Expand Down Expand Up @@ -227,42 +206,31 @@ private static void CompactWhitespaces(StringBuilder sb)
else
break;
}

// if [sb] has only whitespaces, then return empty string

if (start == sb.Length)
{
sb.Length = 0;
return;
}

// set [end] to last not-whitespace char

int end = sb.Length - 1;

while (end >= 0)
{
if (Char.IsWhiteSpace(sb[end]))
if (char.IsWhiteSpace(sb[end]))
end--;
else
break;
}

// compact string

int dest = 0;
bool previousIsWhitespace = false;

for (int i = start; i <= end; i++)
{
if (Char.IsWhiteSpace(sb[i]))
if (char.IsWhiteSpace(sb[i]))
{
if (!previousIsWhitespace)
{
previousIsWhitespace = true;
sb[dest] = ' ';
dest++;
}
if (previousIsWhitespace) continue;
previousIsWhitespace = true;
sb[dest] = ' ';
dest++;
}
else
{
Expand Down Expand Up @@ -294,6 +262,8 @@ public static int BitapFuzzySearch32(string text, string pattern, int k, out int
string query = pattern.FilterCharacters(AllowedSearchCharacters);
inputString = inputString.Replace('_', ' ').Replace('-', ' ');
query = query.Replace('_', ' ').Replace('-', ' ');
query = query.CompactWhitespaces();
inputString = inputString.CompactWhitespaces();
// Case insensitive. We just removed the fancy characters, so latin alphabet lowercase is all we should have
query = query.ToLowerInvariant();
inputString = inputString.ToLowerInvariant();
Expand Down Expand Up @@ -362,6 +332,8 @@ public static int BitapFuzzySearch64(string text, string pattern, int k, out int
string query = pattern.FilterCharacters(AllowedSearchCharacters);
inputString = inputString.Replace('_', ' ').Replace('-', ' ');
query = query.Replace('_', ' ').Replace('-', ' ');
query = query.CompactWhitespaces();
inputString = inputString.CompactWhitespaces();
// Case insensitive. We just removed the fancy characters, so latin alphabet lowercase is all we should have
query = query.ToLowerInvariant();
inputString = inputString.ToLowerInvariant();
Expand Down

0 comments on commit 19c9863

Please sign in to comment.