Skip to content

Commit

Permalink
Merge pull request #72 from ykafia/ykafia-patch-extension
Browse files Browse the repository at this point in the history
Correction GetFirstDirectory
  • Loading branch information
xoofx committed Jan 24, 2023
2 parents 930974d + 80998d6 commit 38c01ea
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
29 changes: 29 additions & 0 deletions src/Zio.Tests/TestUPathExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;

namespace Zio.Tests
{
public class TestUPathExtension
{


[Theory]
[InlineData("/a/b", "a", "b")]
[InlineData("/a/b/c", "a", "b/c")]
[InlineData("a/b", "a", "b")]
[InlineData("a/b/c", "a", "b/c")]
[InlineData("", "", "")]
[InlineData("/z","z","")]

public void TestGetFirstDirectory(string path, string expectedFirstDir, string expectedRest)
{
var pathInfo = new UPath(path);
var firstDir = pathInfo.GetFirstDirectory(out var rest);
Assert.Equal(expectedFirstDir,firstDir);
Assert.Equal(expectedRest,rest);
}
}
}

9 changes: 5 additions & 4 deletions src/Zio/UPathExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,15 @@ public static string GetFirstDirectory(this UPath path, out UPath remainingPath)

string firstDirectory;
var fullname = path.FullName;
var index = fullname.IndexOf(UPath.DirectorySeparator, 1);
var offset = path.IsRelative ? 0 : 1;
var index = fullname.IndexOf(UPath.DirectorySeparator, offset);
if (index < 0)
{
firstDirectory = fullname.Substring(1, fullname.Length - 1);
firstDirectory = fullname.Substring(offset, fullname.Length - offset);
}
else
{
firstDirectory = fullname.Substring(1, index - 1);
firstDirectory = fullname.Substring(offset, index - offset);
if (index + 1 < fullname.Length)
{
remainingPath = fullname.Substring(index + 1);
Expand Down Expand Up @@ -265,4 +266,4 @@ public static UPath AssertAbsolute(this UPath path, string name = "path")
return path.FullName;
}
}
}
}

0 comments on commit 38c01ea

Please sign in to comment.