From daa9ba291ae3e37ba6db0adc85e325693da72330 Mon Sep 17 00:00:00 2001 From: Youness KAFIA <32330908+ykafia@users.noreply.github.com> Date: Wed, 28 Dec 2022 15:31:07 +0100 Subject: [PATCH 1/6] Correction GetFirstDirectory --- src/Zio/UPathExtensions.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Zio/UPathExtensions.cs b/src/Zio/UPathExtensions.cs index 84fbf10..d3d9fa6 100644 --- a/src/Zio/UPathExtensions.cs +++ b/src/Zio/UPathExtensions.cs @@ -88,6 +88,7 @@ public static string GetFirstDirectory(this UPath path, out UPath remainingPath) string firstDirectory; var fullname = path.FullName; + var isRelative = path.IsRelative; var index = fullname.IndexOf(UPath.DirectorySeparator, 1); if (index < 0) { @@ -95,7 +96,7 @@ public static string GetFirstDirectory(this UPath path, out UPath remainingPath) } else { - firstDirectory = fullname.Substring(1, index - 1); + firstDirectory = fullname.Substring(isRelative ? 0 : 1, index - 1); if (index + 1 < fullname.Length) { remainingPath = fullname.Substring(index + 1); @@ -265,4 +266,4 @@ public static UPath AssertAbsolute(this UPath path, string name = "path") return path.FullName; } } -} \ No newline at end of file +} From dc407651df49d2f0deb73c1be4ef18c5515138eb Mon Sep 17 00:00:00 2001 From: Youness KAFIA <32330908+ykafia@users.noreply.github.com> Date: Wed, 28 Dec 2022 15:48:10 +0100 Subject: [PATCH 2/6] Update UPathExtensions.cs --- src/Zio/UPathExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Zio/UPathExtensions.cs b/src/Zio/UPathExtensions.cs index d3d9fa6..64c45be 100644 --- a/src/Zio/UPathExtensions.cs +++ b/src/Zio/UPathExtensions.cs @@ -92,7 +92,7 @@ public static string GetFirstDirectory(this UPath path, out UPath remainingPath) var index = fullname.IndexOf(UPath.DirectorySeparator, 1); if (index < 0) { - firstDirectory = fullname.Substring(1, fullname.Length - 1); + firstDirectory = fullname.Substring(isRelative ? 0 : 1, fullname.Length - 1); } else { From 9c0bd2f1681918dd567a2e47c587d3e3a9078678 Mon Sep 17 00:00:00 2001 From: Youness KAFIA <32330908+ykafia@users.noreply.github.com> Date: Wed, 28 Dec 2022 15:55:44 +0100 Subject: [PATCH 3/6] Update with use of offset --- src/Zio/UPathExtensions.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Zio/UPathExtensions.cs b/src/Zio/UPathExtensions.cs index 64c45be..f7c27ba 100644 --- a/src/Zio/UPathExtensions.cs +++ b/src/Zio/UPathExtensions.cs @@ -88,15 +88,15 @@ public static string GetFirstDirectory(this UPath path, out UPath remainingPath) string firstDirectory; var fullname = path.FullName; - var isRelative = path.IsRelative; - 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(isRelative ? 0 : 1, fullname.Length - 1); + firstDirectory = fullname.Substring(offset, fullname.Length); } else { - firstDirectory = fullname.Substring(isRelative ? 0 : 1, index - 1); + firstDirectory = fullname.Substring(offset, index - 1); if (index + 1 < fullname.Length) { remainingPath = fullname.Substring(index + 1); From a4698bbc568c8e7315aa20ccda698fede8f7c865 Mon Sep 17 00:00:00 2001 From: Youness KAFIA <32330908+ykafia@users.noreply.github.com> Date: Wed, 28 Dec 2022 16:11:52 +0100 Subject: [PATCH 4/6] Correction first directory offset --- src/Zio/UPathExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Zio/UPathExtensions.cs b/src/Zio/UPathExtensions.cs index f7c27ba..c164891 100644 --- a/src/Zio/UPathExtensions.cs +++ b/src/Zio/UPathExtensions.cs @@ -96,7 +96,7 @@ public static string GetFirstDirectory(this UPath path, out UPath remainingPath) } else { - firstDirectory = fullname.Substring(offset, index - 1); + firstDirectory = fullname.Substring(offset, index - offset); if (index + 1 < fullname.Length) { remainingPath = fullname.Substring(index + 1); From 32bb7c4c8a90848cc4a7f8148c73ff14d28a086b Mon Sep 17 00:00:00 2001 From: Youness KAFIA <32330908+ykafia@users.noreply.github.com> Date: Mon, 23 Jan 2023 12:10:28 +0100 Subject: [PATCH 5/6] Added tests for extension --- src/Zio.Tests/TestUPathExtension.cs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/Zio.Tests/TestUPathExtension.cs diff --git a/src/Zio.Tests/TestUPathExtension.cs b/src/Zio.Tests/TestUPathExtension.cs new file mode 100644 index 0000000..ad64157 --- /dev/null +++ b/src/Zio.Tests/TestUPathExtension.cs @@ -0,0 +1,26 @@ +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")] + 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); + } + } +} + From 80998d64ba24e2182f3d5ceeabfae30c5b165243 Mon Sep 17 00:00:00 2001 From: Youness KAFIA <32330908+ykafia@users.noreply.github.com> Date: Tue, 24 Jan 2023 10:12:47 +0100 Subject: [PATCH 6/6] Correction offset --- src/Zio.Tests/TestUPathExtension.cs | 11 +++++++---- src/Zio/UPathExtensions.cs | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Zio.Tests/TestUPathExtension.cs b/src/Zio.Tests/TestUPathExtension.cs index ad64157..b778875 100644 --- a/src/Zio.Tests/TestUPathExtension.cs +++ b/src/Zio.Tests/TestUPathExtension.cs @@ -10,10 +10,13 @@ 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("/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); diff --git a/src/Zio/UPathExtensions.cs b/src/Zio/UPathExtensions.cs index c164891..7f0a616 100644 --- a/src/Zio/UPathExtensions.cs +++ b/src/Zio/UPathExtensions.cs @@ -92,7 +92,7 @@ public static string GetFirstDirectory(this UPath path, out UPath remainingPath) var index = fullname.IndexOf(UPath.DirectorySeparator, offset); if (index < 0) { - firstDirectory = fullname.Substring(offset, fullname.Length); + firstDirectory = fullname.Substring(offset, fullname.Length - offset); } else {