save progress
This commit is contained in:
@@ -46,16 +46,31 @@ public sealed class VirtualFileSystem : IVirtualFileSystem
|
||||
|
||||
public VirtualFileSystem(IEnumerable<string> files)
|
||||
{
|
||||
_files = new HashSet<string>(files, StringComparer.OrdinalIgnoreCase);
|
||||
ArgumentNullException.ThrowIfNull(files);
|
||||
|
||||
_files = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
||||
_directories = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
foreach (var file in _files)
|
||||
foreach (var file in files)
|
||||
{
|
||||
var dir = Path.GetDirectoryName(file);
|
||||
var normalizedFile = NormalizePath(file);
|
||||
if (string.IsNullOrWhiteSpace(normalizedFile))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
_files.Add(normalizedFile);
|
||||
|
||||
var dir = GetDirectoryName(normalizedFile);
|
||||
while (!string.IsNullOrEmpty(dir))
|
||||
{
|
||||
_directories.Add(dir);
|
||||
dir = Path.GetDirectoryName(dir);
|
||||
var normalizedDir = NormalizePath(dir);
|
||||
if (!string.IsNullOrEmpty(normalizedDir))
|
||||
{
|
||||
_directories.Add(normalizedDir);
|
||||
}
|
||||
|
||||
dir = GetParentDirectory(dir);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -68,13 +83,53 @@ public sealed class VirtualFileSystem : IVirtualFileSystem
|
||||
var normalizedDir = NormalizePath(directory);
|
||||
return _files.Where(f =>
|
||||
{
|
||||
var fileDir = Path.GetDirectoryName(f);
|
||||
var fileDir = GetDirectoryName(f);
|
||||
return string.Equals(fileDir, normalizedDir, StringComparison.OrdinalIgnoreCase);
|
||||
});
|
||||
}
|
||||
|
||||
private static string NormalizePath(string path) =>
|
||||
path.Replace('\\', '/').TrimEnd('/');
|
||||
TrimEndDirectorySeparators(path.Replace('\\', '/'));
|
||||
|
||||
private static string TrimEndDirectorySeparators(string path)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(path))
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
var normalized = path;
|
||||
while (normalized.Length > 1 && normalized.EndsWith("/", StringComparison.Ordinal))
|
||||
{
|
||||
normalized = normalized[..^1];
|
||||
}
|
||||
|
||||
return normalized;
|
||||
}
|
||||
|
||||
private static string GetDirectoryName(string path)
|
||||
{
|
||||
var normalized = NormalizePath(path);
|
||||
var lastSlash = normalized.LastIndexOf('/');
|
||||
if (lastSlash <= 0)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
return normalized[..lastSlash];
|
||||
}
|
||||
|
||||
private static string GetParentDirectory(string directory)
|
||||
{
|
||||
var normalized = NormalizePath(directory);
|
||||
var lastSlash = normalized.LastIndexOf('/');
|
||||
if (lastSlash <= 0)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
return normalized[..lastSlash];
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user