Skip to content

Commit

Permalink
Materialize enumerable early (except where guaranteed only one enumer…
Browse files Browse the repository at this point in the history
…ation)
  • Loading branch information
ladipro committed Mar 3, 2021
1 parent 6a51a75 commit da01245
Showing 1 changed file with 18 additions and 19 deletions.
37 changes: 18 additions & 19 deletions src/Shared/FileMatcher.cs
Expand Up @@ -130,13 +130,13 @@ internal FileMatcher(IFileSystem fileSystem, GetFileSystemEntries getFileSystemE
path,
"*",
directory,
false).ToArray());
false));
IEnumerable<string> filteredEntriesForPath = (pattern != null && pattern != "*")
? allEntriesForPath.Where(o => IsMatch(Path.GetFileName(o), pattern))
: allEntriesForPath;
return stripProjectDirectory
? RemoveProjectDirectory(filteredEntriesForPath, directory)
: filteredEntriesForPath;
? RemoveProjectDirectory(filteredEntriesForPath, directory).ToArray()
: filteredEntriesForPath.ToArray();
}
else
{
Expand Down Expand Up @@ -178,7 +178,7 @@ internal enum FileSystemEntity
/// <param name="projectDirectory"></param>
/// <param name="stripProjectDirectory"></param>
/// <returns>An enumerable of filesystem entries.</returns>
internal delegate IEnumerable<string> GetFileSystemEntries(FileSystemEntity entityType, string path, string pattern, string projectDirectory, bool stripProjectDirectory);
internal delegate IReadOnlyList<string> GetFileSystemEntries(FileSystemEntity entityType, string path, string pattern, string projectDirectory, bool stripProjectDirectory);

internal static void ClearFileEnumerationsCache()
{
Expand Down Expand Up @@ -237,7 +237,7 @@ internal static bool HasPropertyOrItemReferences(string filespec)
/// <param name="stripProjectDirectory">If true the project directory should be stripped</param>
/// <param name="fileSystem">The file system abstraction to use that implements file system operations</param>
/// <returns></returns>
private static IEnumerable<string> GetAccessibleFileSystemEntries(IFileSystem fileSystem, FileSystemEntity entityType, string path, string pattern, string projectDirectory, bool stripProjectDirectory)
private static IReadOnlyList<string> GetAccessibleFileSystemEntries(IFileSystem fileSystem, FileSystemEntity entityType, string path, string pattern, string projectDirectory, bool stripProjectDirectory)
{
path = FileUtilities.FixFilePath(path);
switch (entityType)
Expand All @@ -260,7 +260,7 @@ private static IEnumerable<string> GetAccessibleFileSystemEntries(IFileSystem fi
/// <param name="pattern"></param>
/// <param name="fileSystem">The file system abstraction to use that implements file system operations</param>
/// <returns>An enumerable of matching file system entries (can be empty).</returns>
private static IEnumerable<string> GetAccessibleFilesAndDirectories(IFileSystem fileSystem, string path, string pattern)
private static IReadOnlyList<string> GetAccessibleFilesAndDirectories(IFileSystem fileSystem, string path, string pattern)
{
if (fileSystem.DirectoryExists(path))
{
Expand All @@ -270,7 +270,7 @@ private static IEnumerable<string> GetAccessibleFilesAndDirectories(IFileSystem
? fileSystem.EnumerateFileSystemEntries(path, pattern)
.Where(o => IsMatch(Path.GetFileName(o), pattern))
: fileSystem.EnumerateFileSystemEntries(path, pattern)
);
).ToArray();
}
// for OS security
catch (UnauthorizedAccessException)
Expand Down Expand Up @@ -326,7 +326,7 @@ private static bool ShouldEnforceMatching(string searchPattern)
/// <param name="stripProjectDirectory"></param>
/// <param name="fileSystem">The file system abstraction to use that implements file system operations</param>
/// <returns>Files that can be accessed.</returns>
private static IEnumerable<string> GetAccessibleFiles
private static IReadOnlyList<string> GetAccessibleFiles
(
IFileSystem fileSystem,
string path,
Expand Down Expand Up @@ -370,7 +370,7 @@ bool stripProjectDirectory
files = RemoveInitialDotSlash(files);
}

return files;
return files.ToArray();
}
catch (System.Security.SecurityException)
{
Expand All @@ -394,7 +394,7 @@ bool stripProjectDirectory
/// <param name="pattern">Pattern to match</param>
/// <param name="fileSystem">The file system abstraction to use that implements file system operations</param>
/// <returns>Accessible directories.</returns>
private static IEnumerable<string> GetAccessibleDirectories
private static IReadOnlyList<string> GetAccessibleDirectories
(
IFileSystem fileSystem,
string path,
Expand Down Expand Up @@ -428,7 +428,7 @@ string pattern
directories = RemoveInitialDotSlash(directories);
}

return directories;
return directories.ToArray();
}
catch (System.Security.SecurityException)
{
Expand Down Expand Up @@ -528,11 +528,10 @@ GetFileSystemEntries getFileSystemEntries
}
else
{
// getFileSystemEntries(...) returns an empty array if longPath doesn't exist.
IEnumerable<string> entries = getFileSystemEntries(FileSystemEntity.FilesAndDirectories, longPath, parts[i], null, false);
// getFileSystemEntries(...) returns an empty enumerable if longPath doesn't exist.
IReadOnlyList<string> entries = getFileSystemEntries(FileSystemEntity.FilesAndDirectories, longPath, parts[i], null, false);

int entriesCount = entries.Count();
if (0 == entriesCount)
if (0 == entries.Count)
{
// The next part doesn't exist. Therefore, no more of the path will exist.
// Just return the rest.
Expand All @@ -543,13 +542,13 @@ GetFileSystemEntries getFileSystemEntries
break;
}

// Since we know there are no wild cards, this should be length one.
ErrorUtilities.VerifyThrow(entriesCount == 1,
// Since we know there are no wild cards, this should be length one, i.e. MoveNext should return false.
ErrorUtilities.VerifyThrow(entries.Count == 1,
"Unexpected number of entries ({3}) found when enumerating '{0}' under '{1}'. Original path was '{2}'",
parts[i], longPath, path, entriesCount);
parts[i], longPath, path, entries.Count);

// Entries[0] contains the full path.
longPath = entries.First();
longPath = entries[0];

// We just want the trailing node.
longParts[i - startingElement] = Path.GetFileName(longPath);
Expand Down

0 comments on commit da01245

Please sign in to comment.