Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Several fixes for Path #99

Merged
merged 1 commit into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions System.IO.FileSystem.UnitTests/PathUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ namespace System.IO.FileSystem.UnitTests
[TestClass]
internal class PathUnitTests
{
private string RootDirectory => "\\";

[TestMethod]
public void ChangeExtension_adds_extension()
{
Expand Down Expand Up @@ -318,21 +316,21 @@ public void GetFullPath0()
{
string fullPath = Path.GetFullPath(@"dir1\dir2\file.ext");

Assert.AreEqual(RootDirectory + @"\dir1\dir2\file.ext", fullPath);
Assert.AreEqual(@"\dir1\dir2\file.ext", fullPath);
}

[TestMethod]
public void GetFullPath1()
{
string fullPath = Path.GetFullPath(RootDirectory + @"\dir1\dir2\file.ext");
Assert.AreEqual(RootDirectory + @"\dir1\dir2\file.ext", fullPath);
string fullPath = Path.GetFullPath(@"\dir1\dir2\file.ext");
Assert.AreEqual(@"\dir1\dir2\file.ext", fullPath);
}

[TestMethod]
public void GetFullPath2()
{
string fullPath = Path.GetFullPath(RootDirectory + @"\dir1\..\dir2\file.ext");
Assert.AreEqual(RootDirectory + @"\dir1\dir2\file.ext", fullPath);
string fullPath = Path.GetFullPath(@"\dir1\..\dir2\file.ext");
Assert.AreEqual(@"\dir1\..\dir2\file.ext", fullPath);
}

[TestMethod]
Expand Down
40 changes: 28 additions & 12 deletions System.IO.FileSystem/Path.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,20 +114,22 @@ public static string Combine(string path1, string path2)
throw new ArgumentNullException();
}

return CombineInternal(path1, path2);
}
if (string.IsNullOrEmpty(path1))
{
return path2;
}

private static string CombineInternal(string first, string second)
{
ValidateNullOrEmpty(first);
ValidateNullOrEmpty(second);
if (string.IsNullOrEmpty(path2))
{
return path1;
}

if (IsPathRooted(second))
if (IsPathRooted(path2))
{
return second;
return path2;
}

return JoinInternal(first, second);
return JoinInternal(path1, path2);
}

/// <summary>
Expand Down Expand Up @@ -186,7 +188,10 @@ internal static int GetDirectoryNameOffset(string path)
[return: NotNullIfNotNull("path")]
public static string GetExtension(string path)
{
ValidateNullOrEmpty(path);
if (path is null)
{
return null;
}

var length = path.Length;

Expand Down Expand Up @@ -225,7 +230,10 @@ public static string GetExtension(string path)
[return: NotNullIfNotNull("path")]
public static string GetFileName(string path)
{
ValidateNullOrEmpty(path);
if (path is null)
{
return null;
}

var root = GetPathRoot(path).Length;

Expand All @@ -251,7 +259,10 @@ public static string GetFileName(string path)
[return: NotNullIfNotNull("path")]
public static string GetFileNameWithoutExtension(string path)
{
ValidateNullOrEmpty(path);
if (path is null)
{
return null;
}

var fileName = GetFileName(path);
var lastPeriod = fileName.LastIndexOf('.');
Expand Down Expand Up @@ -393,6 +404,11 @@ private static string JoinInternal(string first, string second)
var hasSeparator = PathInternal.IsDirectorySeparator(first[first.Length - 1])
|| PathInternal.IsDirectorySeparator(second[0]);

if (first.Equals(PathInternal.DirectorySeparatorCharAsString))
{
first = string.Empty;
}

return hasSeparator ?
string.Concat(first, second) :
string.Concat(first, PathInternal.DirectorySeparatorCharAsString, second);
Expand Down