Skip to content

Commit f3c9450

Browse files
committed
feat: added windows path support
1 parent 63fb9f6 commit f3c9450

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

Source/SuperiorIO/SuperiorPath.cs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections;
2+
using System.Text.RegularExpressions;
23

34
namespace SuperiorIO;
45

@@ -8,15 +9,31 @@ public class SuperiorPath : IEnumerable<PathElement>
89
public readonly bool IsFolder;
910
public readonly bool IsFile;
1011
private readonly IList<PathElement> _elements;
12+
public readonly bool IsWindowsPath;
13+
public readonly string WindowsDriveLetter;
1114

1215
private SuperiorPath(bool isRooted, IEnumerable<PathElement> elements)
1316
{
17+
IsWindowsPath = false;
18+
WindowsDriveLetter = "";
1419
IsRooted = isRooted;
1520
_elements = elements as IList<PathElement> ?? new List<PathElement>(elements);
1621
var lastElement = _elements.LastOrDefault();
1722
IsFolder = lastElement.ElementType == PathElementType.Folder;
1823
IsFile = lastElement.ElementType == PathElementType.File;
1924
}
25+
26+
private SuperiorPath(string windowsDriveLetter, IEnumerable<PathElement> elements)
27+
{
28+
IsWindowsPath = true;
29+
WindowsDriveLetter = windowsDriveLetter;
30+
IsRooted = true;
31+
_elements = elements as IList<PathElement> ?? new List<PathElement>(elements);
32+
var lastElement = _elements.LastOrDefault();
33+
IsFolder = lastElement.ElementType == PathElementType.Folder;
34+
IsFile = lastElement.ElementType == PathElementType.File;
35+
}
36+
2037

2138
// New method to concatenate a string to SuperiorPath
2239
public SuperiorPath Concatenate(string path)
@@ -42,6 +59,14 @@ public string FullPath
4259
{
4360
get
4461
{
62+
if (IsWindowsPath)
63+
{
64+
var winPrefix = IsRooted ? WindowsDriveLetter + ":\\" : "";
65+
var winPostfix = IsFolder ? "\\" : "";
66+
return winPrefix + string.Join("\\", _elements.Select(i => i.Name)) + winPostfix;
67+
68+
}
69+
4570
var prefix = IsRooted ? "/" : "";
4671
var postfix = IsFolder ? "/" : "";
4772
return prefix + string.Join("/", _elements.Select(i => i.Name)) + postfix;
@@ -83,6 +108,45 @@ IEnumerator IEnumerable.GetEnumerator()
83108
public static SuperiorPath Root = Create("/");
84109

85110
public static SuperiorPath Create(string filePath)
111+
{
112+
var m = Regex.Match(filePath, @"^(?<drive>[A-Z]):\\");
113+
if (m.Success)
114+
{
115+
var windowsDriveLetter = m.Groups["drive"].Value;
116+
filePath = filePath[2..];
117+
var elements = filePath.Split('\\', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries);
118+
var lastIndex = elements.Length - 1;
119+
var pathElements = elements.Select((itm, index) =>
120+
{
121+
switch (itm)
122+
{
123+
case "..":
124+
return new PathElement(itm, PathElementType.Parent);
125+
case ".":
126+
return new PathElement(".", PathElementType.Current);
127+
}
128+
129+
if (index < lastIndex)
130+
{
131+
return new PathElement(itm, PathElementType.Folder);
132+
}
133+
134+
if (index == lastIndex && !filePath.EndsWith("\\"))
135+
{
136+
return new PathElement(itm, PathElementType.File);
137+
}
138+
return new PathElement(itm, PathElementType.Unknown);
139+
}).ToArray();
140+
141+
return new SuperiorPath(windowsDriveLetter: windowsDriveLetter, pathElements);
142+
}
143+
else
144+
{
145+
return CreateMacOSPath(filePath);
146+
}
147+
}
148+
149+
private static SuperiorPath CreateMacOSPath(string filePath)
86150
{
87151
var elements = filePath.Split('/', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries);
88152
var isRooted = filePath.StartsWith("/");

0 commit comments

Comments
 (0)