Skip to content

Commit

Permalink
Speed up Resolve-Path relative path resolution (PowerShell#19171)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinGC94 committed Mar 15, 2023
1 parent d616478 commit 8b964de
Showing 1 changed file with 15 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ protected override void ProcessRecord()

if (_relative)
{
ReadOnlySpan<char> baseCache = null;
ReadOnlySpan<char> adjustedBaseCache = null;
foreach (PathInfo currentPath in result)
{
// When result path and base path is on different PSDrive
Expand All @@ -116,8 +118,18 @@ protected override void ProcessRecord()
continue;
}

int leafIndex = currentPath.Path.LastIndexOf(currentPath.Provider.ItemSeparator);
var basePath = currentPath.Path.AsSpan(0, leafIndex);
if (basePath == baseCache)
{
WriteObject(string.Concat(adjustedBaseCache, currentPath.Path.AsSpan(leafIndex + 1)), enumerateCollection: false);
continue;
}

baseCache = basePath;
string adjustedPath = SessionState.Path.NormalizeRelativePath(currentPath.Path,
SessionState.Path.CurrentLocation.ProviderPath);

// Do not insert './' if result path is not relative
if (!adjustedPath.StartsWith(
currentPath.Drive?.Root ?? currentPath.Path, StringComparison.OrdinalIgnoreCase) &&
Expand All @@ -126,6 +138,9 @@ protected override void ProcessRecord()
adjustedPath = SessionState.Path.Combine(".", adjustedPath);
}

leafIndex = adjustedPath.LastIndexOf(currentPath.Provider.ItemSeparator);
adjustedBaseCache = adjustedPath.AsSpan(0, leafIndex + 1);

WriteObject(adjustedPath, enumerateCollection: false);
}
}
Expand Down

0 comments on commit 8b964de

Please sign in to comment.