Skip to content

Commit

Permalink
Refactor implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
vbreuss committed Apr 20, 2024
1 parent bf4424b commit c7c20f7
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 179 deletions.
29 changes: 10 additions & 19 deletions Source/Testably.Abstractions.Testing/Helpers/Execute.LinuxPath.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text;
using System.Linq;
using System.Text;

namespace Testably.Abstractions.Testing.Helpers;

Expand Down Expand Up @@ -75,33 +76,23 @@ protected override bool IsEffectivelyEmpty(string path)
return path;
}

// Make a pass to see if we need to normalize so we can potentially skip allocating
bool normalized = true;

for (int i = 0; i < path.Length; i++)
{
if (IsDirectorySeparator(path[i])
&& i + 1 < path.Length && IsDirectorySeparator(path[i + 1]))
{
normalized = false;
break;
}
}

if (normalized)
bool isAlreadyNormalized = Enumerable
.Range(0, path.Length - 1)
.All(i => !IsDirectorySeparator(path[i]) ||
!IsDirectorySeparator(path[i + 1]));
if (isAlreadyNormalized)
{
return path;
}

StringBuilder builder = new(path.Length);

for (int i = 0; i < path.Length; i++)
for (int j = 0; j < path.Length - 1; j++)
{
char current = path[i];
char current = path[j];

// Skip if we have another separator following
if (IsDirectorySeparator(current)
&& i + 1 < path.Length && IsDirectorySeparator(path[i + 1]))
&& IsDirectorySeparator(path[j + 1]))
{
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,58 +162,29 @@ public ReadOnlySpan<char> GetDirectoryName(ReadOnlySpan<char> path)
/// <inheritdoc cref="IPath.GetDirectoryName(string)" />
public string? GetDirectoryName(string? path)
{
int GetDirectoryNameOffset(string p)
if (path == null || IsEffectivelyEmpty(path))
{
int rootLength = GetRootLength(p);
int end = p.Length;
if (end <= rootLength)
{
return -1;
}

while (end > rootLength && !IsDirectorySeparator(p[--end]))
{
// Do nothing
}

// Trim off any remaining separators (to deal with C:\foo\\bar)
while (end > rootLength && IsDirectorySeparator(p[end - 1]))
{
end--;
}

return end;
return null;
}

if (path == null || IsEffectivelyEmpty(path))
int rootLength = GetRootLength(path);
if (path.Length <= rootLength)
{
return null;
}

int end = GetDirectoryNameOffset(path);
if (end >= 0)
int end = path.Length;
while (end > rootLength && !IsDirectorySeparator(path[end - 1]))
{
return NormalizeDirectorySeparators(path.Substring(0, end));
end--;
}

return null;
//if (path == null || IsEffectivelyEmpty(path))
//{
// return null;
//}

//int rootLength = GetRootLength(path);
//for (int i = path.Length - 1; i >= 0; i--)
//{
// char ch = path[i];

// if (IsDirectorySeparator(ch) && i > rootLength)
// {
// return path.Substring(0, i);
// }
//}
while (end > rootLength && IsDirectorySeparator(path[end - 1]))
{
end--;
}

//return null;
return NormalizeDirectorySeparators(path.Substring(0, end));
}

#if FEATURE_SPAN
Expand Down
Loading

0 comments on commit c7c20f7

Please sign in to comment.