Skip to content

Commit ae63041

Browse files
committed
Fix UnifiedPath bugs
1 parent de108db commit ae63041

File tree

1 file changed

+29
-18
lines changed

1 file changed

+29
-18
lines changed

src/EasySign/UnifiedPath/OSPath.cs

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace SAPTeam.EasySign.UnifiedPath
44
{
55
/// <summary>
6-
/// Represents an operating system path and provides methods for path manipulation.
6+
/// Represents an operating system path and provides methods for path conversion and manipulation.
77
/// </summary>
88
public class OSPath
99
{
@@ -27,18 +27,15 @@ public class OSPath
2727
/// Implicitly converts a string to an <see cref="OSPath"/>.
2828
/// </summary>
2929
/// <param name="text">The path text.</param>
30-
public static implicit operator OSPath(string text) => text == null ? null : new OSPath(text);
30+
public static implicit operator OSPath(string text) => new OSPath(text);
3131

3232
/// <summary>
3333
/// Implicitly converts an <see cref="OSPath"/> to a string.
3434
/// </summary>
3535
/// <param name="path">The OSPath instance.</param>
36-
public static implicit operator string(OSPath path) => path?.Normalized;
36+
public static implicit operator string(OSPath path) => path.Normalized;
3737

38-
/// <summary>
39-
/// Returns the normalized path as a string.
40-
/// </summary>
41-
/// <returns>The normalized path.</returns>
38+
/// <inheritdoc/>
4239
public override string ToString() => Normalized;
4340

4441
/// <summary>
@@ -62,15 +59,10 @@ public class OSPath
6259
public string Unix => Simplified.Text.Replace('\\', '/');
6360

6461
/// <summary>
65-
/// Gets the relative path.
62+
/// Gets the path without the root or drive letter.
6663
/// </summary>
6764
public OSPath Relative => Simplified.Text.TrimStart('/', '\\');
6865

69-
/// <summary>
70-
/// Gets the absolute path.
71-
/// </summary>
72-
public OSPath Absolute => IsAbsolute ? this : "/" + Relative;
73-
7466
/// <summary>
7567
/// Gets a value indicating whether the path is absolute.
7668
/// </summary>
@@ -82,27 +74,46 @@ public class OSPath
8274
public bool IsRooted => Text.Length >= 1 && (Text[0] == '/' || Text[0] == '\\');
8375

8476
/// <summary>
85-
/// Gets a value indicating whether the path has a volume.
77+
/// Gets a value indicating whether the path has a drive letter.
8678
/// </summary>
8779
public bool HasVolume => Text.Length >= 2 && Text[1] == ':';
8880

8981
/// <summary>
90-
/// Gets the simplified path.
82+
/// Gets the rooted path without the drive letter.
9183
/// </summary>
9284
public OSPath Simplified => HasVolume ? Text.Substring(2) : Text;
9385

9486
/// <summary>
9587
/// Gets the parent directory of the path.
9688
/// </summary>
97-
public OSPath Parent => GetDirectoryName(Text);
89+
public OSPath Parent
90+
{
91+
get
92+
{
93+
var parent = GetDirectoryName(Text);
94+
95+
if (parent == null)
96+
{
97+
var root = GetPathRoot(Text);
98+
99+
if (root == null)
100+
{
101+
return Empty;
102+
}
103+
104+
return root;
105+
}
106+
107+
return parent;
108+
}
109+
}
98110

99111
/// <summary>
100112
/// Determines whether the current path contains the specified path.
101113
/// </summary>
102114
/// <param name="path">The path to check.</param>
103115
/// <returns><c>true</c> if the current path contains the specified path; otherwise, <c>false</c>.</returns>
104-
public bool Contains(OSPath path) =>
105-
Normalized.StartsWith(path);
116+
public bool Contains(OSPath path) => Normalized.StartsWith(path);
106117

107118
/// <summary>
108119
/// Concatenates two paths.

0 commit comments

Comments
 (0)