Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
ElementalCrisis committed Feb 11, 2017
2 parents f8b6ae6 + 19c9863 commit 3061036
Showing 1 changed file with 13 additions and 41 deletions.
54 changes: 13 additions & 41 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 All @@ -308,7 +278,7 @@ public static int BitapFuzzySearch32(string text, string pattern, int k, out int
int result = -1;
int m = query.Length;
int[] R;
int[] patternMask = new int[AllowedSearchCharacters.Length+1];
int[] patternMask = new int[128];
int i, d;
dist = k + 1;

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 All @@ -376,7 +348,7 @@ public static int BitapFuzzySearch64(string text, string pattern, int k, out int
int result = -1;
int m = query.Length;
ulong[] R;
ulong[] patternMask = new ulong[AllowedSearchCharacters.Length+1];
ulong[] patternMask = new ulong[128];
int i, d;
dist = text.Length;

Expand Down

0 comments on commit 3061036

Please sign in to comment.