Skip to content

Commit

Permalink
Added case-insensitive support for the wildcard algorithm and correct…
Browse files Browse the repository at this point in the history
…ed case-insensitive comparison for excludes

	modified:   src/Shared/FileMatcher.cs
  • Loading branch information
maca88 committed Oct 8, 2017
1 parent 2900c73 commit edbe8f5
Show file tree
Hide file tree
Showing 2 changed files with 298 additions and 14 deletions.
79 changes: 66 additions & 13 deletions src/Shared/FileMatcher.cs
Expand Up @@ -152,7 +152,7 @@ private static ImmutableArray<string> GetAccessibleFilesAndDirectories(string pa
{
return (ShouldEnforceMatching(pattern)
? Directory.EnumerateFileSystemEntries(path, pattern)
.Where(o => IsMatch(Path.GetFileName(o), pattern))
.Where(o => IsMatch(Path.GetFileName(o), pattern, true))
: Directory.EnumerateFileSystemEntries(path, pattern)
).ToImmutableArray();
}
Expand Down Expand Up @@ -231,7 +231,7 @@ bool stripProjectDirectory
files = Directory.EnumerateFiles(dir, filespec);
if (ShouldEnforceMatching(filespec))
{
files = files.Where(o => IsMatch(Path.GetFileName(o), filespec));
files = files.Where(o => IsMatch(Path.GetFileName(o), filespec, true));
}
}
// If the Item is based on a relative path we need to strip
Expand Down Expand Up @@ -292,7 +292,7 @@ string pattern
directories = Directory.EnumerateDirectories((path.Length == 0) ? s_thisDirectory : path, pattern);
if (ShouldEnforceMatching(pattern))
{
directories = directories.Where(o => IsMatch(Path.GetFileName(o), pattern));
directories = directories.Where(o => IsMatch(Path.GetFileName(o), pattern, true));
}
}

Expand Down Expand Up @@ -818,7 +818,7 @@ TaskOptions taskOptions
for (int i = 0; i < excludeNextSteps.Length; i++)
{
if (excludeNextSteps[i].NeedsDirectoryRecursion &&
(excludeNextSteps[i].DirectoryPattern == null || IsMatch(Path.GetFileName(subdir), excludeNextSteps[i].DirectoryPattern)))
(excludeNextSteps[i].DirectoryPattern == null || IsMatch(Path.GetFileName(subdir), excludeNextSteps[i].DirectoryPattern, true)))
{
RecursionState thisExcludeStep = searchesToExclude[i];
thisExcludeStep.BaseDirectory = subdir;
Expand Down Expand Up @@ -938,7 +938,7 @@ private static bool MatchFileRecursionStep(RecursionState recursionState, string
{
if (recursionState.SearchData.Filespec != null)
{
return IsMatch(Path.GetFileName(file), recursionState.SearchData.Filespec);
return IsMatch(Path.GetFileName(file), recursionState.SearchData.Filespec, true);
}

// if no file-spec provided, match the file to the regular expression
Expand Down Expand Up @@ -1428,7 +1428,8 @@ internal Result()
/// </summary>
/// <param name="input">String which is matched against the pattern.</param>
/// <param name="pattern">Pattern against which string is matched.</param>
internal static bool IsMatch(string input, string pattern)
/// <param name="ignoreCase">Determines whether ignoring case when comparing two characters</param>
internal static bool IsMatch(string input, string pattern, bool ignoreCase)
{
if (input == null)
{
Expand All @@ -1454,6 +1455,44 @@ internal static bool IsMatch(string input, string pattern)
// Store the information whether the tail was checked when a pattern "*?" occurred
bool tailChecked = false;

#if MONO // MONO doesn't support local functions
Func<char, char, int, int, bool> CompareIgnoreCase = (inputChar, patternChar, iIndex, pIndex) =>
#else
// Function for comparing two characters, ignoring case
// PERF NOTE:
// Having a local function instead of a variable increases the speed by approx. 2 times.
// Passing inputChar and patternChar increases the speed by approx. 10%, when comparing
// to using the string indexer. The iIndex and pIndex parameters are only used
// when we have to compare two non ASCII characters. Using just string.Compare for
// character comparison, would reduce the speed by approx. 5 times.
bool CompareIgnoreCase(char inputChar, char patternChar, int iIndex, int pIndex)
#endif
{
// We will mostly be comparing ASCII characters, check this first
if (inputChar < 128 && patternChar < 128)
{
if (inputChar >= 'A' && inputChar <= 'Z' && patternChar >= 'a' && patternChar <= 'z')
{
return inputChar + 32 == patternChar;
}
if (inputChar >= 'a' && inputChar <= 'z' && patternChar >= 'A' && patternChar <= 'Z')
{
return inputChar == patternChar + 32;
}
return inputChar == patternChar;
}
if (inputChar > 128 && patternChar > 128)
{
return string.Compare(input, iIndex, pattern, pIndex, 1, StringComparison.OrdinalIgnoreCase) == 0;
}
// We don't need to compare, an ASCII character cannot have its lowercase/uppercase outside the ASCII table
// and a non ASCII character cannot have its lowercase/uppercase inside the ASCII table
return false;
}
#if MONO
; // The end of the CompareIgnoreCase anonymous function
#endif

while (inputIndex < inputLength)
{
if (patternIndex < patternLength)
Expand Down Expand Up @@ -1483,10 +1522,19 @@ internal static bool IsMatch(string input, string pattern)
inputTailIndex--;
// If we encountered a * wildcard we are not sure if it matches as there can be zero or more than one characters
// so we have to fallback to the standard procedure e.g. ("aaaabaaad", "*?b*d")
if (pattern[patternTailIndex] == '*' || pattern[patternTailIndex] != input[inputTailIndex] && pattern[patternTailIndex] != '?')
if (pattern[patternTailIndex] == '*')
{
break;
}
// If the tail doesn't match, we can safely return e.g. ("aaa", "*b")
if ((
(!ignoreCase && input[inputTailIndex] != pattern[patternTailIndex]) ||
(ignoreCase && !CompareIgnoreCase(input[inputTailIndex], pattern[patternTailIndex], patternTailIndex, inputTailIndex))
) &&
pattern[patternTailIndex] != '?')
{
return false;
}
if (patternIndex == patternTailIndex)
{
return true;
Expand All @@ -1502,7 +1550,9 @@ internal static bool IsMatch(string input, string pattern)
// The ? wildcard cannot be skipped as we will have a wrong result for e.g. ("aab" "*?b")
if (pattern[patternIndex] != '?')
{
while (input[inputIndex] != pattern[patternIndex])
while (
(!ignoreCase && input[inputIndex] != pattern[patternIndex]) ||
(ignoreCase && !CompareIgnoreCase(input[inputIndex], pattern[patternIndex], inputIndex, patternIndex)))
{
// Return if there is no character that match e.g. ("aa", "*b")
if (++inputIndex >= inputLength)
Expand All @@ -1517,7 +1567,10 @@ internal static bool IsMatch(string input, string pattern)
}

// If we have a match, step to the next character
if (pattern[patternIndex] == input[inputIndex] || pattern[patternIndex] == '?')
if (
(!ignoreCase && input[inputIndex] == pattern[patternIndex]) ||
(ignoreCase && CompareIgnoreCase(input[inputIndex], pattern[patternIndex], inputIndex, patternIndex)) ||
pattern[patternIndex] == '?')
{
patternIndex++;
inputIndex++;
Expand Down Expand Up @@ -2040,7 +2093,7 @@ DirectoryExists directoryExists
var excludeBaseDirectory = excludeState.BaseDirectory;
var includeBaseDirectory = state.BaseDirectory;

if (excludeBaseDirectory != includeBaseDirectory)
if (string.Compare(excludeBaseDirectory, includeBaseDirectory, StringComparison.OrdinalIgnoreCase) != 0)
{
// What to do if the BaseDirectory for the exclude search doesn't match the one for inclusion?
// - If paths don't match (one isn't a prefix of the other), then ignore the exclude search. Examples:
Expand All @@ -2054,7 +2107,7 @@ DirectoryExists directoryExists
}
else if (excludeBaseDirectory.Length > includeBaseDirectory.Length)
{
if (!excludeBaseDirectory.StartsWith(includeBaseDirectory))
if (!excludeBaseDirectory.StartsWith(includeBaseDirectory, StringComparison.OrdinalIgnoreCase))
{
// Exclude path is longer, but doesn't start with include path. So ignore it.
continue;
Expand All @@ -2069,7 +2122,7 @@ DirectoryExists directoryExists

if (searchesToExcludeInSubdirs == null)
{
searchesToExcludeInSubdirs = new Dictionary<string, List<RecursionState>>();
searchesToExcludeInSubdirs = new Dictionary<string, List<RecursionState>>(StringComparer.OrdinalIgnoreCase);
}
List<RecursionState> listForSubdir;
if (!searchesToExcludeInSubdirs.TryGetValue(excludeBaseDirectory, out listForSubdir))
Expand All @@ -2083,7 +2136,7 @@ DirectoryExists directoryExists
else
{
// Exclude base directory length is less than include base directory length.
if (!state.BaseDirectory.StartsWith(excludeState.BaseDirectory))
if (!state.BaseDirectory.StartsWith(excludeState.BaseDirectory, StringComparison.OrdinalIgnoreCase))
{
// Include path is longer, but doesn't start with the exclude path. So ignore exclude path
// (since it won't match anything under the include path)
Expand Down

0 comments on commit edbe8f5

Please sign in to comment.